test_container_avi.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "opencv2/videoio/container_avi.private.hpp"
  6. #include <cstdio>
  7. using namespace cv;
  8. namespace opencv_test { namespace {
  9. TEST(videoio_builtin, basic_avi)
  10. {
  11. String filename = BunnyParameters::getFilename(".mjpg.avi");
  12. AVIReadContainer in;
  13. in.initStream(filename);
  14. frame_list frames;
  15. ASSERT_TRUE(in.parseRiff(frames));
  16. EXPECT_EQ(frames.size(), static_cast<unsigned>(BunnyParameters::getCount()));
  17. EXPECT_EQ(in.getWidth(), static_cast<unsigned>(BunnyParameters::getWidth()));
  18. EXPECT_EQ(in.getHeight(), static_cast<unsigned>(BunnyParameters::getHeight()));
  19. EXPECT_EQ(in.getFps(), static_cast<unsigned>(BunnyParameters::getFps()));
  20. }
  21. TEST(videoio_builtin, invalid_avi)
  22. {
  23. String filename = BunnyParameters::getFilename(".avi");
  24. AVIReadContainer in;
  25. in.initStream(filename);
  26. frame_list frames;
  27. EXPECT_FALSE(in.parseRiff(frames));
  28. EXPECT_EQ(frames.size(), static_cast<unsigned>(0));
  29. }
  30. TEST(videoio_builtin, read_write_avi)
  31. {
  32. const String filename = cv::tempfile("test.avi");
  33. const double fps = 100;
  34. const Size sz(800, 600);
  35. const size_t count = 10;
  36. const uchar data[count] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA};
  37. const Codecs codec = MJPEG;
  38. {
  39. AVIWriteContainer out;
  40. ASSERT_TRUE(out.initContainer(filename, fps, sz, true));
  41. ASSERT_TRUE(out.isOpenedStream());
  42. EXPECT_EQ(out.getWidth(), sz.width);
  43. EXPECT_EQ(out.getHeight(), sz.height);
  44. EXPECT_EQ(out.getChannels(), 3);
  45. out.startWriteAVI(1);
  46. {
  47. out.writeStreamHeader(codec); // starts LIST chunk
  48. size_t chunkPointer = out.getStreamPos();
  49. int avi_index = out.getAVIIndex(0, dc);
  50. {
  51. out.startWriteChunk(avi_index);
  52. out.putStreamBytes(data, count);
  53. size_t tempChunkPointer = out.getStreamPos();
  54. size_t moviPointer = out.getMoviPointer();
  55. out.pushFrameOffset(chunkPointer - moviPointer);
  56. out.pushFrameSize(tempChunkPointer - chunkPointer - 8);
  57. out.endWriteChunk();
  58. }
  59. out.endWriteChunk(); // ends LIST chunk
  60. }
  61. out.writeIndex(0, dc);
  62. out.finishWriteAVI();
  63. }
  64. {
  65. AVIReadContainer in;
  66. in.initStream(filename);
  67. frame_list frames;
  68. ASSERT_TRUE(in.parseRiff(frames));
  69. EXPECT_EQ(in.getFps(), fps);
  70. EXPECT_EQ(in.getWidth(), static_cast<unsigned>(sz.width));
  71. EXPECT_EQ(in.getHeight(), static_cast<unsigned>(sz.height));
  72. ASSERT_EQ(frames.size(), static_cast<unsigned>(1));
  73. std::vector<char> actual = in.readFrame(frames.begin());
  74. ASSERT_EQ(actual.size(), count);
  75. for (size_t i = 0; i < count; ++i)
  76. EXPECT_EQ(actual.at(i), data[i]) << "at index " << i;
  77. }
  78. remove(filename.c_str());
  79. }
  80. }} // opencv_test::<anonymous>::