test_audio.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. //file name, number of audio channels, epsilon, video type, weight, height, number of frame, number of audio samples, fps, psnr Threshold, backend
  7. typedef std::tuple<std::string, int, double, int, int, int, int, int, int, double, VideoCaptureAPIs> paramCombination;
  8. //file name, number of audio channels, number of audio samples, epsilon, backend
  9. typedef std::tuple<std::string, int, int, double, VideoCaptureAPIs> param;
  10. class AudioBaseTest
  11. {
  12. protected:
  13. AudioBaseTest(){};
  14. void getValidAudioData()
  15. {
  16. const double step = 3.14/22050;
  17. double value = 0;
  18. validAudioData.resize(expectedNumAudioCh);
  19. for (int nCh = 0; nCh < expectedNumAudioCh; nCh++)
  20. {
  21. value = 0;
  22. for(unsigned int i = 0; i < numberOfSamples; i++)
  23. {
  24. if (i != 0 && i % 44100 == 0)
  25. value = 0;
  26. validAudioData[nCh].push_back(sin(value));
  27. value += step;
  28. }
  29. }
  30. }
  31. void checkAudio()
  32. {
  33. getValidAudioData();
  34. ASSERT_EQ(expectedNumAudioCh, (int)audioData.size());
  35. for (unsigned int nCh = 0; nCh < audioData.size(); nCh++)
  36. {
  37. ASSERT_EQ(numberOfSamples, audioData[nCh].size()) << "nCh=" << nCh;
  38. for (unsigned int i = 0; i < numberOfSamples; i++)
  39. {
  40. EXPECT_NEAR(validAudioData[nCh][i], audioData[nCh][i], epsilon) << "sample index=" << i << " nCh=" << nCh;
  41. }
  42. }
  43. }
  44. protected:
  45. int expectedNumAudioCh;
  46. unsigned int numberOfSamples;
  47. double epsilon;
  48. VideoCaptureAPIs backend;
  49. std::string root;
  50. std::string fileName;
  51. std::vector<std::vector<double>> validAudioData;
  52. std::vector<std::vector<double>> audioData;
  53. std::vector<int> params;
  54. Mat audioFrame;
  55. VideoCapture cap;
  56. };
  57. class AudioTestFixture : public AudioBaseTest, public testing::TestWithParam <param>
  58. {
  59. public:
  60. AudioTestFixture()
  61. {
  62. fileName = get<0>(GetParam());
  63. expectedNumAudioCh = get<1>(GetParam());
  64. numberOfSamples = get<2>(GetParam());
  65. epsilon = get<3>(GetParam());
  66. backend = get<4>(GetParam());
  67. root = "audio/";
  68. params = { CAP_PROP_AUDIO_STREAM, 0,
  69. CAP_PROP_VIDEO_STREAM, -1,
  70. CAP_PROP_AUDIO_DATA_DEPTH, CV_16S };
  71. }
  72. void doTest()
  73. {
  74. ASSERT_TRUE(cap.open(findDataFile(root + fileName), backend, params));
  75. const int audioBaseIndex = static_cast<int>(cap.get(cv::CAP_PROP_AUDIO_BASE_INDEX));
  76. const int numberOfChannels = (int)cap.get(CAP_PROP_AUDIO_TOTAL_CHANNELS);
  77. ASSERT_EQ(expectedNumAudioCh, numberOfChannels);
  78. double f = 0;
  79. audioData.resize(numberOfChannels);
  80. for (;;)
  81. {
  82. if (cap.grab())
  83. {
  84. for (int nCh = 0; nCh < numberOfChannels; nCh++)
  85. {
  86. ASSERT_TRUE(cap.retrieve(audioFrame, audioBaseIndex + nCh));
  87. ASSERT_EQ(CV_16SC1, audioFrame.type()) << audioData[nCh].size();
  88. for (int i = 0; i < audioFrame.cols; i++)
  89. {
  90. f = ((double) audioFrame.at<signed short>(0,i)) / (double) 32768;
  91. audioData[nCh].push_back(f);
  92. }
  93. }
  94. }
  95. else { break; }
  96. }
  97. ASSERT_FALSE(audioData.empty());
  98. checkAudio();
  99. }
  100. };
  101. const param audioParams[] =
  102. {
  103. #ifdef _WIN32
  104. param("test_audio.wav", 1, 132300, 0.0001, cv::CAP_MSMF),
  105. param("test_mono_audio.mp3", 1, 133104, 0.12, cv::CAP_MSMF),
  106. param("test_stereo_audio.mp3", 2, 133104, 0.12, cv::CAP_MSMF),
  107. param("test_audio.mp4", 1, 133104, 0.15, cv::CAP_MSMF),
  108. #endif
  109. param("test_audio.wav", 1, 132300, 0.0001, cv::CAP_GSTREAMER),
  110. param("test_audio.mp4", 1, 132522, 0.15, cv::CAP_GSTREAMER),
  111. };
  112. class Audio : public AudioTestFixture{};
  113. TEST_P(Audio, audio)
  114. {
  115. if (!videoio_registry::hasBackend(cv::VideoCaptureAPIs(backend)))
  116. throw SkipTestException(cv::videoio_registry::getBackendName(backend) + " backend was not found");
  117. doTest();
  118. }
  119. INSTANTIATE_TEST_CASE_P(/**/, Audio, testing::ValuesIn(audioParams));
  120. class MediaTestFixture : public AudioBaseTest, public testing::TestWithParam <paramCombination>
  121. {
  122. public:
  123. MediaTestFixture():
  124. videoType(get<3>(GetParam())),
  125. height(get<4>(GetParam())),
  126. width(get<5>(GetParam())),
  127. numberOfFrames(get<6>(GetParam())),
  128. fps(get<8>(GetParam())),
  129. psnrThreshold(get<9>(GetParam()))
  130. {
  131. fileName = get<0>(GetParam());
  132. expectedNumAudioCh = get<1>(GetParam());
  133. numberOfSamples = get<7>(GetParam());
  134. epsilon = get<2>(GetParam());
  135. backend = get<10>(GetParam());
  136. root = "audio/";
  137. params = { CAP_PROP_AUDIO_STREAM, 0,
  138. CAP_PROP_VIDEO_STREAM, 0,
  139. CAP_PROP_AUDIO_DATA_DEPTH, CV_16S };
  140. };
  141. void doTest()
  142. {
  143. ASSERT_TRUE(cap.open(findDataFile(root + fileName), backend, params));
  144. const int audioBaseIndex = static_cast<int>(cap.get(cv::CAP_PROP_AUDIO_BASE_INDEX));
  145. const int numberOfChannels = (int)cap.get(CAP_PROP_AUDIO_TOTAL_CHANNELS);
  146. ASSERT_EQ(expectedNumAudioCh, numberOfChannels);
  147. const int samplePerSecond = (int)cap.get(CAP_PROP_AUDIO_SAMPLES_PER_SECOND);
  148. ASSERT_EQ(44100, samplePerSecond);
  149. int samplesPerFrame = (int)(1./fps*samplePerSecond);
  150. int audioSamplesTolerance = samplesPerFrame / 2;
  151. double audio0_timestamp = 0;
  152. Mat videoFrame;
  153. Mat img(height, width, videoType);
  154. audioData.resize(numberOfChannels);
  155. for (int frame = 0; frame < numberOfFrames; frame++)
  156. {
  157. SCOPED_TRACE(cv::format("frame=%d", frame));
  158. ASSERT_TRUE(cap.grab());
  159. if (frame == 0)
  160. {
  161. double audio_shift = cap.get(CAP_PROP_AUDIO_SHIFT_NSEC);
  162. double video0_timestamp = cap.get(CAP_PROP_POS_MSEC) * 1e-3;
  163. audio0_timestamp = video0_timestamp + audio_shift * 1e-9;
  164. std::cout << "video0 timestamp: " << video0_timestamp << " audio0 timestamp: " << audio0_timestamp << " (audio shift nanoseconds: " << audio_shift << " , seconds: " << audio_shift * 1e-9 << ")" << std::endl;
  165. }
  166. ASSERT_TRUE(cap.retrieve(videoFrame));
  167. if (epsilon >= 0)
  168. {
  169. generateFrame(frame, numberOfFrames, img);
  170. ASSERT_EQ(img.size, videoFrame.size);
  171. double psnr = cvtest::PSNR(img, videoFrame);
  172. EXPECT_GE(psnr, psnrThreshold);
  173. }
  174. int audioFrameCols = 0;
  175. for (int nCh = 0; nCh < numberOfChannels; nCh++)
  176. {
  177. ASSERT_TRUE(cap.retrieve(audioFrame, audioBaseIndex+nCh));
  178. if (audioFrame.empty())
  179. continue;
  180. ASSERT_EQ(CV_16SC1, audioFrame.type());
  181. if (nCh == 0)
  182. audioFrameCols = audioFrame.cols;
  183. else
  184. ASSERT_EQ(audioFrameCols, audioFrame.cols) << "channel "<< nCh;
  185. for (int i = 0; i < audioFrame.cols; i++)
  186. {
  187. double f = audioFrame.at<signed short>(0,i) / 32768.0;
  188. audioData[nCh].push_back(f);
  189. }
  190. }
  191. if (frame < 5 || frame >= numberOfFrames-5)
  192. std::cout << "frame=" << frame << ": audioFrameSize=" << audioFrameCols << " videoTimestamp=" << cap.get(CAP_PROP_POS_MSEC) << " ms" << std::endl;
  193. else if (frame == 6)
  194. std::cout << "frame..." << std::endl;
  195. if (audioFrameCols == 0)
  196. continue;
  197. if (frame != 0 && frame != numberOfFrames-1)
  198. {
  199. // validate audio position
  200. EXPECT_NEAR(
  201. cap.get(CAP_PROP_AUDIO_POS) / samplePerSecond + audio0_timestamp,
  202. cap.get(CAP_PROP_POS_MSEC) * 1e-3,
  203. (1.0 / fps) * 0.3)
  204. << "CAP_PROP_AUDIO_POS=" << cap.get(CAP_PROP_AUDIO_POS) << " CAP_PROP_POS_MSEC=" << cap.get(CAP_PROP_POS_MSEC);
  205. }
  206. if (frame != 0 && frame != numberOfFrames-1 && audioData[0].size() != (size_t)numberOfSamples)
  207. {
  208. // validate audio frame size
  209. EXPECT_NEAR(audioFrame.cols, samplesPerFrame, audioSamplesTolerance);
  210. }
  211. }
  212. ASSERT_FALSE(cap.grab());
  213. ASSERT_FALSE(audioData.empty());
  214. std::cout << "Total audio samples=" << audioData[0].size() << std::endl;
  215. if (epsilon >= 0)
  216. checkAudio();
  217. }
  218. protected:
  219. const int videoType;
  220. const int height;
  221. const int width;
  222. const int numberOfFrames;
  223. const int fps;
  224. const double psnrThreshold;
  225. };
  226. class Media : public MediaTestFixture{};
  227. TEST_P(Media, audio)
  228. {
  229. if (!videoio_registry::hasBackend(cv::VideoCaptureAPIs(backend)))
  230. throw SkipTestException(cv::videoio_registry::getBackendName(backend) + " backend was not found");
  231. doTest();
  232. }
  233. #ifdef _WIN32
  234. const paramCombination mediaParams[] =
  235. {
  236. #ifdef _WIN32
  237. paramCombination("test_audio.mp4", 1, 0.15, CV_8UC3, 240, 320, 90, 131819, 30, 30., cv::CAP_MSMF)
  238. #if 0
  239. // https://filesamples.com/samples/video/mp4/sample_960x400_ocean_with_audio.mp4
  240. , paramCombination("sample_960x400_ocean_with_audio.mp4", 2, -1/*eplsilon*/, CV_8UC3, 400, 960, 1116, 2056588, 30, 30., cv::CAP_MSMF)
  241. #endif
  242. #endif // _WIN32
  243. };
  244. INSTANTIATE_TEST_CASE_P(/**/, Media, testing::ValuesIn(mediaParams));
  245. #endif // _WIN32
  246. TEST(AudioOpenCheck, bad_arg_invalid_audio_stream)
  247. {
  248. std::string fileName = "audio/test_audio.wav";
  249. std::vector<int> params {
  250. CAP_PROP_AUDIO_STREAM, 1,
  251. CAP_PROP_VIDEO_STREAM, -1, // disabled
  252. CAP_PROP_AUDIO_DATA_DEPTH, CV_16S
  253. };
  254. VideoCapture cap;
  255. cap.open(findDataFile(fileName), cv::CAP_ANY, params);
  256. ASSERT_FALSE(cap.isOpened());
  257. }
  258. TEST(AudioOpenCheck, bad_arg_invalid_audio_stream_video)
  259. {
  260. std::string fileName = "audio/test_audio.mp4";
  261. std::vector<int> params {
  262. CAP_PROP_AUDIO_STREAM, 1,
  263. CAP_PROP_VIDEO_STREAM, 0,
  264. CAP_PROP_AUDIO_DATA_DEPTH, CV_16S
  265. };
  266. VideoCapture cap;
  267. cap.open(findDataFile(fileName), cv::CAP_ANY, params);
  268. ASSERT_FALSE(cap.isOpened());
  269. }
  270. #ifdef _WIN32
  271. TEST(AudioOpenCheck, MSMF_bad_arg_invalid_audio_sample_per_second)
  272. {
  273. std::string fileName = "audio/test_audio.mp4";
  274. std::vector<int> params {
  275. CAP_PROP_AUDIO_STREAM, 0,
  276. CAP_PROP_VIDEO_STREAM, -1, // disabled
  277. CAP_PROP_AUDIO_SAMPLES_PER_SECOND, (int)1e9
  278. };
  279. VideoCapture cap;
  280. cap.open(findDataFile(fileName), cv::CAP_MSMF, params);
  281. ASSERT_FALSE(cap.isOpened());
  282. }
  283. #endif
  284. TEST(AudioOpenCheck, bad_arg_invalid_audio_sample_per_second)
  285. {
  286. std::string fileName = "audio/test_audio.mp4";
  287. std::vector<int> params {
  288. CAP_PROP_AUDIO_STREAM, 0,
  289. CAP_PROP_VIDEO_STREAM, -1, // disabled
  290. CAP_PROP_AUDIO_SAMPLES_PER_SECOND, -1000
  291. };
  292. VideoCapture cap;
  293. cap.open(findDataFile(fileName), cv::CAP_ANY, params);
  294. ASSERT_FALSE(cap.isOpened());
  295. }
  296. }} //namespace