Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/test/test_mfx.cpp
16344 views
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
5
#include "test_precomp.hpp"
6
7
#ifdef HAVE_MFX
8
9
namespace opencv_test { namespace {
10
11
TEST(Videoio_MFX, read_invalid)
12
{
13
VideoCapture cap;
14
ASSERT_NO_THROW(cap.open("nonexistent-file", CAP_INTEL_MFX));
15
ASSERT_FALSE(cap.isOpened());
16
Mat img;
17
ASSERT_NO_THROW(cap >> img);
18
ASSERT_TRUE(img.empty());
19
}
20
21
TEST(Videoio_MFX, write_invalid)
22
{
23
const string filename = cv::tempfile(".264");
24
VideoWriter writer;
25
bool res = true;
26
ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(641, 480), true));
27
EXPECT_FALSE(res);
28
EXPECT_FALSE(writer.isOpened());
29
ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 481), true));
30
EXPECT_FALSE(res);
31
EXPECT_FALSE(writer.isOpened());
32
ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('A', 'B', 'C', 'D'), 1, Size(640, 480), true));
33
EXPECT_FALSE(res);
34
EXPECT_FALSE(writer.isOpened());
35
ASSERT_NO_THROW(res = writer.open(String(), CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 480), true));
36
EXPECT_FALSE(res);
37
EXPECT_FALSE(writer.isOpened());
38
ASSERT_ANY_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 0, Size(640, 480), true));
39
EXPECT_FALSE(res);
40
EXPECT_FALSE(writer.isOpened());
41
42
ASSERT_NO_THROW(res = writer.open(filename, CAP_INTEL_MFX, VideoWriter::fourcc('H', '2', '6', '4'), 30, Size(640, 480), true));
43
ASSERT_TRUE(res);
44
ASSERT_TRUE(writer.isOpened());
45
Mat t;
46
// write some bad frames
47
t = Mat(Size(1024, 768), CV_8UC3);
48
EXPECT_NO_THROW(writer << t);
49
t = Mat(Size(320, 240), CV_8UC3);
50
EXPECT_NO_THROW(writer << t);
51
t = Mat(Size(640, 480), CV_8UC2);
52
EXPECT_NO_THROW(writer << t);
53
54
// cleanup
55
ASSERT_NO_THROW(writer.release());
56
remove(filename.c_str());
57
}
58
59
60
//==================================================================================================
61
62
const int FRAME_COUNT = 20;
63
64
inline void generateFrame(int i, Mat & frame)
65
{
66
::generateFrame(i, FRAME_COUNT, frame);
67
}
68
69
inline int fourccByExt(const String &ext)
70
{
71
if (ext == ".mpeg2")
72
return VideoWriter::fourcc('M', 'P', 'G', '2');
73
else if (ext == ".264")
74
return VideoWriter::fourcc('H', '2', '6', '4');
75
else if (ext == ".265")
76
return VideoWriter::fourcc('H', '2', '6', '5');
77
return -1;
78
}
79
80
//==================================================================================================
81
82
typedef tuple<Size, double, const char *> Size_FPS_Ext;
83
typedef testing::TestWithParam< Size_FPS_Ext > Videoio_MFX;
84
85
TEST_P(Videoio_MFX, read_write_raw)
86
{
87
const Size FRAME_SIZE = get<0>(GetParam());
88
const double FPS = get<1>(GetParam());
89
const char *ext = get<2>(GetParam());
90
const String filename = cv::tempfile(ext);
91
const int fourcc = fourccByExt(ext);
92
93
bool isColor = true;
94
std::queue<Mat> goodFrames;
95
96
// Write video
97
VideoWriter writer;
98
writer.open(filename, CAP_INTEL_MFX, fourcc, FPS, FRAME_SIZE, isColor);
99
ASSERT_TRUE(writer.isOpened());
100
Mat frame(FRAME_SIZE, CV_8UC3);
101
for (int i = 0; i < FRAME_COUNT; ++i)
102
{
103
generateFrame(i, frame);
104
goodFrames.push(frame.clone());
105
writer << frame;
106
}
107
writer.release();
108
EXPECT_FALSE(writer.isOpened());
109
110
// Read video
111
VideoCapture cap;
112
cap.open(filename, CAP_INTEL_MFX);
113
ASSERT_TRUE(cap.isOpened());
114
for (int i = 0; i < FRAME_COUNT; ++i)
115
{
116
ASSERT_TRUE(cap.read(frame));
117
ASSERT_FALSE(frame.empty());
118
ASSERT_EQ(FRAME_SIZE.width, frame.cols);
119
ASSERT_EQ(FRAME_SIZE.height, frame.rows);
120
// verify
121
ASSERT_NE(goodFrames.size(), 0u);
122
const Mat &goodFrame = goodFrames.front();
123
EXPECT_EQ(goodFrame.depth(), frame.depth());
124
EXPECT_EQ(goodFrame.channels(), frame.channels());
125
EXPECT_EQ(goodFrame.type(), frame.type());
126
double psnr = cvtest::PSNR(goodFrame, frame);
127
if (fourcc == VideoWriter::fourcc('M', 'P', 'G', '2'))
128
EXPECT_GT(psnr, 31); // experimentally chosen value
129
else
130
EXPECT_GT(psnr, 33); // experimentally chosen value
131
goodFrames.pop();
132
}
133
EXPECT_FALSE(cap.read(frame));
134
EXPECT_TRUE(frame.empty());
135
cap.release();
136
EXPECT_FALSE(cap.isOpened());
137
remove(filename.c_str());
138
}
139
140
INSTANTIATE_TEST_CASE_P(videoio, Videoio_MFX,
141
testing::Combine(
142
testing::Values(Size(640, 480), Size(638, 478), Size(636, 476), Size(1920, 1080)),
143
testing::Values(1, 30, 100),
144
testing::Values(".mpeg2", ".264", ".265")));
145
146
}} // namespace
147
148
#endif
149
150