test_dynamic.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using namespace std;
  6. namespace opencv_test { namespace {
  7. const int FRAME_COUNT = 120;
  8. inline void generateFrame(int i, Mat & frame)
  9. {
  10. ::generateFrame(i, FRAME_COUNT, frame);
  11. }
  12. TEST(videoio_dynamic, basic_write)
  13. {
  14. const Size FRAME_SIZE(640, 480);
  15. const double FPS = 100;
  16. const String filename = cv::tempfile(".avi");
  17. const int fourcc = VideoWriter::fourcc('M', 'J', 'P', 'G');
  18. bool fileExists = false;
  19. {
  20. vector<VideoCaptureAPIs> backends = videoio_registry::getWriterBackends();
  21. for (VideoCaptureAPIs be : backends)
  22. {
  23. VideoWriter writer;
  24. writer.open(filename, be, fourcc, FPS, FRAME_SIZE, true);
  25. if (writer.isOpened())
  26. {
  27. Mat frame(FRAME_SIZE, CV_8UC3);
  28. for (int j = 0; j < FRAME_COUNT; ++j)
  29. {
  30. generateFrame(j, frame);
  31. writer << frame;
  32. }
  33. writer.release();
  34. fileExists = true;
  35. }
  36. EXPECT_FALSE(writer.isOpened());
  37. }
  38. }
  39. if (!fileExists)
  40. {
  41. cout << "None of backends has been able to write video file - SKIP reading part" << endl;
  42. return;
  43. }
  44. {
  45. vector<VideoCaptureAPIs> backends = videoio_registry::getStreamBackends();
  46. for (VideoCaptureAPIs be : backends)
  47. {
  48. VideoCapture cap;
  49. cap.open(filename, be);
  50. if(cap.isOpened())
  51. {
  52. int count = 0;
  53. while (true)
  54. {
  55. Mat frame;
  56. if (cap.grab())
  57. {
  58. if (cap.retrieve(frame))
  59. {
  60. ++count;
  61. continue;
  62. }
  63. }
  64. break;
  65. }
  66. EXPECT_EQ(count, FRAME_COUNT);
  67. cap.release();
  68. }
  69. EXPECT_FALSE(cap.isOpened());
  70. }
  71. }
  72. remove(filename.c_str());
  73. }
  74. TEST(videoio_dynamic, write_invalid)
  75. {
  76. vector<VideoCaptureAPIs> backends = videoio_registry::getWriterBackends();
  77. for (VideoCaptureAPIs be : backends)
  78. {
  79. SCOPED_TRACE(be);
  80. const string filename = cv::tempfile(".mkv");
  81. VideoWriter writer;
  82. bool res = true;
  83. // Bad FourCC
  84. EXPECT_NO_THROW(res = writer.open(filename, be, VideoWriter::fourcc('A', 'B', 'C', 'D'), 1, Size(640, 480), true));
  85. EXPECT_FALSE(res);
  86. EXPECT_FALSE(writer.isOpened());
  87. // Empty filename
  88. EXPECT_NO_THROW(res = writer.open(String(), be, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 480), true));
  89. EXPECT_FALSE(res);
  90. EXPECT_FALSE(writer.isOpened());
  91. EXPECT_NO_THROW(res = writer.open(String(), be, VideoWriter::fourcc('M', 'J', 'P', 'G'), 1, Size(640, 480), true));
  92. EXPECT_FALSE(res);
  93. EXPECT_FALSE(writer.isOpened());
  94. // zero FPS
  95. EXPECT_NO_THROW(res = writer.open(filename, be, VideoWriter::fourcc('H', '2', '6', '4'), 0, Size(640, 480), true));
  96. EXPECT_FALSE(res);
  97. EXPECT_FALSE(writer.isOpened());
  98. // cleanup
  99. EXPECT_NO_THROW(writer.release());
  100. remove(filename.c_str());
  101. }
  102. // Generic
  103. {
  104. VideoWriter writer;
  105. bool res = true;
  106. EXPECT_NO_THROW(res = writer.open(std::string(), VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 480)));
  107. EXPECT_FALSE(res);
  108. EXPECT_FALSE(writer.isOpened());
  109. EXPECT_NO_THROW(res = writer.open(std::string(), VideoWriter::fourcc('M', 'J', 'P', 'G'), 1, Size(640, 480)));
  110. EXPECT_FALSE(res);
  111. EXPECT_FALSE(writer.isOpened());
  112. }
  113. }
  114. }} // opencv_test::<anonymous>::