1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44 #include "opencv2/videoio.hpp"
45
46 using namespace cv;
47 using namespace std;
48
49 class CV_PositioningTest : public cvtest::BaseTest
50 {
51 public:
CV_PositioningTest()52 CV_PositioningTest()
53 {
54 framesize = Size(640, 480);
55 }
56
drawFrame(int i)57 Mat drawFrame(int i)
58 {
59 Mat mat = Mat::zeros(framesize, CV_8UC3);
60
61 mat = Scalar(fabs(cos(i*0.08)*255), fabs(sin(i*0.05)*255), i);
62 putText(mat, format("%03d", i), Point(10, 350), 0, 10, Scalar(128, 255, 255), 15);
63 return mat;
64 }
65
getFilename(const cvtest::VideoFormat & fmt)66 string getFilename(const cvtest::VideoFormat& fmt)
67 {
68 return cv::tempfile((cvtest::fourccToString(fmt.fourcc) + "." + fmt.ext).c_str());
69 }
70
CreateTestVideo(const cvtest::VideoFormat & fmt,int framecount,string filename)71 bool CreateTestVideo(const cvtest::VideoFormat& fmt, int framecount, string filename)
72 {
73 VideoWriter writer(filename, fmt.fourcc, 25, framesize, true);
74 if( !writer.isOpened() )
75 return false;
76
77 for (int i = 0; i < framecount; ++i)
78 {
79 Mat img = drawFrame(i);
80 writer << img;
81 }
82 return true;
83 }
84
run(int)85 void run(int)
86 {
87 int n_frames = 100;
88
89 for( int testcase = 0; ; testcase++ )
90 {
91 const cvtest::VideoFormat& fmt = cvtest::g_specific_fmt_list[testcase];
92 if( fmt.empty() )
93 break;
94 string filename = getFilename(fmt);
95 ts->printf(ts->LOG, "\nFile: %s\n", filename.c_str());
96
97 if( !CreateTestVideo(fmt, n_frames, filename) )
98 {
99 ts->printf(ts->LOG, "\nError: cannot create video file");
100 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
101 return;
102 }
103
104 VideoCapture cap(filename);
105
106 if (!cap.isOpened())
107 {
108 ts->printf(ts->LOG, "\nError: cannot read video file.");
109 ts->set_failed_test_info(ts->FAIL_INVALID_TEST_DATA);
110 return;
111 }
112
113 int N0 = (int)cap.get(CAP_PROP_FRAME_COUNT);
114 cap.set(CAP_PROP_POS_FRAMES, 0);
115 int N = (int)cap.get(CAP_PROP_FRAME_COUNT);
116
117 // See the same hack in CV_VideoIOTest::SpecificVideoTest for explanation.
118 int allowed_extra_frames = 0;
119 if (fmt.fourcc == VideoWriter::fourcc('M', 'P', 'E', 'G') && fmt.ext == "mkv")
120 allowed_extra_frames = 1;
121
122 if (N < n_frames || N > n_frames + allowed_extra_frames || N != N0)
123 {
124 ts->printf(ts->LOG, "\nError: returned frame count (N0=%d, N=%d) is different from the reference number %d\n", N0, N, n_frames);
125 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
126 return;
127 }
128
129 for (int k = 0; k < n_frames; ++k)
130 {
131 int idx = theRNG().uniform(0, n_frames);
132
133 if( !cap.set(CAP_PROP_POS_FRAMES, idx) )
134 {
135 ts->printf(ts->LOG, "\nError: cannot seek to frame %d.\n", idx);
136 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
137 return;
138 }
139
140 int idx1 = (int)cap.get(CAP_PROP_POS_FRAMES);
141
142 Mat img; cap >> img;
143 Mat img0 = drawFrame(idx);
144
145 if( idx != idx1 )
146 {
147 ts->printf(ts->LOG, "\nError: the current position (%d) after seek is different from specified (%d)\n",
148 idx1, idx);
149 ts->printf(ts->LOG, "Saving both frames ...\n");
150 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
151 return;
152 }
153
154 if (img.empty())
155 {
156 ts->printf(ts->LOG, "\nError: cannot read a frame at position %d.\n", idx);
157 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
158 return;
159 }
160
161 double err = cvtest::PSNR(img, img0);
162
163 if( err < 20 )
164 {
165 ts->printf(ts->LOG, "The frame read after positioning to %d is incorrect (PSNR=%g)\n", idx, err);
166 ts->printf(ts->LOG, "Saving both frames ...\n");
167 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
168 return;
169 }
170 }
171 }
172 }
173
174 Size framesize;
175 };
176
177 #if BUILD_WITH_VIDEO_INPUT_SUPPORT && BUILD_WITH_VIDEO_OUTPUT_SUPPORT && defined HAVE_FFMPEG
TEST(Videoio_Video,seek_random_synthetic)178 TEST(Videoio_Video, seek_random_synthetic) { CV_PositioningTest test; test.safe_run(); }
179 #endif
180