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
5 #include "test_precomp.hpp"
7 namespace opencv_test { namespace {
9 TEST(videoio_mfx, read_invalid)
11 if (!videoio_registry::hasBackend(CAP_INTEL_MFX))
12 throw SkipTestException("MediaSDK backend was not found");
15 ASSERT_NO_THROW(cap.open("nonexistent-file", CAP_INTEL_MFX));
16 ASSERT_FALSE(cap.isOpened());
18 ASSERT_NO_THROW(cap >> img);
19 ASSERT_TRUE(img.empty());
22 TEST(videoio_mfx, write_invalid)
24 if (!videoio_registry::hasBackend(CAP_INTEL_MFX))
25 throw SkipTestException("MediaSDK backend was not found");
27 const string filename = cv::tempfile(".264");
30 ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(641, 480), true));
32 EXPECT_FALSE(writer.isOpened());
33 ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 481), true));
35 EXPECT_FALSE(writer.isOpened());
36 ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('A', 'B', 'C', 'D'), 1, Size(640, 480), true));
38 EXPECT_FALSE(writer.isOpened());
39 ASSERT_NO_THROW(res = writer.open(String(), CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 480), true));
41 EXPECT_FALSE(writer.isOpened());
42 ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 0, Size(640, 480), true));
44 EXPECT_FALSE(writer.isOpened());
46 ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 30, Size(640, 480), true));
48 ASSERT_TRUE(writer.isOpened());
50 // write some bad frames
51 t = Mat(Size(1024, 768), CV_8UC3);
52 EXPECT_NO_THROW(writer << t);
53 t = Mat(Size(320, 240), CV_8UC3);
54 EXPECT_NO_THROW(writer << t);
55 t = Mat(Size(640, 480), CV_8UC2);
56 EXPECT_NO_THROW(writer << t);
59 ASSERT_NO_THROW(writer.release());
60 remove(filename.c_str());
64 //==================================================================================================
66 const int FRAME_COUNT = 20;
68 inline void generateFrame(int i, Mat & frame)
70 ::generateFrame(i, FRAME_COUNT, frame);
73 inline int fourccByExt(const String &ext)
76 return VideoWriter::fourcc('M', 'P', 'G', '2');
77 else if (ext == ".264")
78 return VideoWriter::fourcc('H', '2', '6', '4');
79 else if (ext == ".265")
80 return VideoWriter::fourcc('H', '2', '6', '5');
84 //==================================================================================================
86 typedef tuple<Size, double, const char *> Size_FPS_Ext;
87 typedef testing::TestWithParam< Size_FPS_Ext > videoio_mfx;
89 TEST_P(videoio_mfx, read_write_raw)
91 if (!videoio_registry::hasBackend(CAP_INTEL_MFX))
92 throw SkipTestException("MediaSDK backend was not found");
94 const Size FRAME_SIZE = get<0>(GetParam());
95 const double FPS = get<1>(GetParam());
96 const char *ext = get<2>(GetParam());
97 const String filename = cv::tempfile(ext);
98 const int fourcc = fourccByExt(ext);
101 std::queue<Mat> goodFrames;
105 writer.open(filename, CAP_INTEL_MFX, fourcc, FPS, FRAME_SIZE, isColor);
106 ASSERT_TRUE(writer.isOpened());
107 Mat frame(FRAME_SIZE, CV_8UC3);
108 for (int i = 0; i < FRAME_COUNT; ++i)
110 generateFrame(i, frame);
111 goodFrames.push(frame.clone());
115 EXPECT_FALSE(writer.isOpened());
119 cap.open(filename, CAP_INTEL_MFX);
120 ASSERT_TRUE(cap.isOpened());
121 for (int i = 0; i < FRAME_COUNT; ++i)
123 ASSERT_TRUE(cap.read(frame));
124 ASSERT_FALSE(frame.empty());
125 ASSERT_EQ(FRAME_SIZE.width, frame.cols);
126 ASSERT_EQ(FRAME_SIZE.height, frame.rows);
128 ASSERT_NE(goodFrames.size(), 0u);
129 const Mat &goodFrame = goodFrames.front();
130 EXPECT_EQ(goodFrame.depth(), frame.depth());
131 EXPECT_EQ(goodFrame.channels(), frame.channels());
132 EXPECT_EQ(goodFrame.type(), frame.type());
133 double psnr = cvtest::PSNR(goodFrame, frame);
134 if (fourcc == VideoWriter::fourcc('M', 'P', 'G', '2'))
135 EXPECT_GT(psnr, 31); // experimentally chosen value
137 EXPECT_GT(psnr, 33); // experimentally chosen value
140 EXPECT_FALSE(cap.read(frame));
141 EXPECT_TRUE(frame.empty());
143 EXPECT_FALSE(cap.isOpened());
144 remove(filename.c_str());
147 INSTANTIATE_TEST_CASE_P(videoio, videoio_mfx,
149 testing::Values(Size(640, 480), Size(638, 478), Size(636, 476), Size(1920, 1080)),
150 testing::Values(1, 30, 100),
151 testing::Values(".mpeg2", ".264", ".265")));