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/videoio_c.h"
45 #include <stdio.h>
46
47 using namespace cv;
48 using namespace std;
49
50 class CV_VideoPositioningTest: public cvtest::BaseTest
51 {
52 public:
53 enum {PROGRESSIVE, RANDOM};
54
55 CV_VideoPositioningTest();
56 ~CV_VideoPositioningTest();
57 virtual void run(int) = 0;
58
59 protected:
60 vector <int> idx;
61 void run_test(int method);
62
63 private:
64 void generate_idx_seq(CvCapture *cap, int method);
65 };
66
67 class CV_VideoProgressivePositioningTest: public CV_VideoPositioningTest
68 {
69 public:
CV_VideoProgressivePositioningTest()70 CV_VideoProgressivePositioningTest() : CV_VideoPositioningTest() { }
71 ~CV_VideoProgressivePositioningTest();
72 void run(int);
73 };
74
75 class CV_VideoRandomPositioningTest: public CV_VideoPositioningTest
76 {
77 public:
CV_VideoRandomPositioningTest()78 CV_VideoRandomPositioningTest(): CV_VideoPositioningTest() { }
79 ~CV_VideoRandomPositioningTest();
80 void run(int);
81 };
82
CV_VideoPositioningTest()83 CV_VideoPositioningTest::CV_VideoPositioningTest() {}
~CV_VideoPositioningTest()84 CV_VideoPositioningTest::~CV_VideoPositioningTest() {}
~CV_VideoProgressivePositioningTest()85 CV_VideoProgressivePositioningTest::~CV_VideoProgressivePositioningTest() {}
~CV_VideoRandomPositioningTest()86 CV_VideoRandomPositioningTest::~CV_VideoRandomPositioningTest() {}
87
generate_idx_seq(CvCapture * cap,int method)88 void CV_VideoPositioningTest::generate_idx_seq(CvCapture* cap, int method)
89 {
90 idx.clear();
91 int N = (int)cvGetCaptureProperty(cap, CAP_PROP_FRAME_COUNT);
92 switch(method)
93 {
94 case PROGRESSIVE:
95 {
96 int pos = 1, step = 20;
97 do
98 {
99 idx.push_back(pos);
100 pos += step;
101 }
102 while (pos <= N);
103 break;
104 }
105 case RANDOM:
106 {
107 RNG rng(N);
108 idx.clear();
109 for( int i = 0; i < N-1; i++ )
110 idx.push_back(rng.uniform(0, N));
111 idx.push_back(N-1);
112 std::swap(idx.at(rng.uniform(0, N-1)), idx.at(N-1));
113 break;
114 }
115 default:break;
116 }
117 }
118
run_test(int method)119 void CV_VideoPositioningTest::run_test(int method)
120 {
121 const string& src_dir = ts->get_data_path();
122
123 ts->printf(cvtest::TS::LOG, "\n\nSource files directory: %s\n", (src_dir+"video/").c_str());
124
125 const string ext[] = {"avi", "mov", "mp4", "mpg"};
126
127 int n = (int)(sizeof(ext)/sizeof(ext[0]));
128
129 int failed_videos = 0;
130
131 for (int i = 0; i < n; ++i)
132 {
133 // skip random positioning test in plain mpegs
134 if( method == RANDOM && ext[i] == "mpg" )
135 continue;
136 string file_path = src_dir + "video/big_buck_bunny." + ext[i];
137
138 ts->printf(cvtest::TS::LOG, "\nReading video file in %s...\n", file_path.c_str());
139
140 CvCapture* cap = cvCreateFileCapture(file_path.c_str());
141
142 if (!cap)
143 {
144 ts->printf(cvtest::TS::LOG, "\nFile information (video %d): \n\nName: big_buck_bunny.%s\nFAILED\n\n", i+1, ext[i].c_str());
145 ts->printf(cvtest::TS::LOG, "Error: cannot read source video file.\n");
146 ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
147 failed_videos++; continue;
148 }
149
150 cvSetCaptureProperty(cap, CAP_PROP_POS_FRAMES, 0);
151
152 generate_idx_seq(cap, method);
153
154 int N = (int)idx.size(), failed_frames = 0, failed_positions = 0, failed_iterations = 0;
155
156 for (int j = 0; j < N; ++j)
157 {
158 bool flag = false;
159
160 cvSetCaptureProperty(cap, CAP_PROP_POS_FRAMES, idx.at(j));
161
162 /* IplImage* frame = cvRetrieveFrame(cap);
163
164 if (!frame)
165 {
166 if (!failed_frames)
167 {
168 ts->printf(cvtest::TS::LOG, "\nFile information (video %d): \n\nName: big_buck_bunny.%s\n", i+1, ext[i].c_str());
169 }
170 failed_frames++;
171 ts->printf(cvtest::TS::LOG, "\nIteration: %d\n\nError: cannot read a frame with index %d.\n", j, idx.at(j));
172 ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
173 flag = !flag;
174 } */
175
176 int val = (int)cvGetCaptureProperty(cap, CAP_PROP_POS_FRAMES);
177
178 if (idx.at(j) != val)
179 {
180 if (!(failed_frames||failed_positions))
181 {
182 ts->printf(cvtest::TS::LOG, "\nFile information (video %d): \n\nName: big_buck_bunny.%s\n", i+1, ext[i].c_str());
183 }
184 failed_positions++;
185 if (!failed_frames)
186 {
187 ts->printf(cvtest::TS::LOG, "\nIteration: %d\n", j);
188 }
189 ts->printf(cvtest::TS::LOG, "Required pos: %d\nReturned pos: %d\n", idx.at(j), val);
190 ts->printf(cvtest::TS::LOG, "Error: required and returned positions are not matched.\n");
191 ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
192 flag = true;
193 }
194
195 if (flag)
196 {
197 failed_iterations++;
198 failed_videos++;
199 break;
200 }
201 }
202
203 cvReleaseCapture(&cap);
204 }
205
206 ts->printf(cvtest::TS::LOG, "\nSuccessfull experiments: %d (%d%%)\n", n-failed_videos, 100*(n-failed_videos)/n);
207 ts->printf(cvtest::TS::LOG, "Failed experiments: %d (%d%%)\n", failed_videos, 100*failed_videos/n);
208 }
209
run(int)210 void CV_VideoProgressivePositioningTest::run(int)
211 {
212 run_test(PROGRESSIVE);
213 }
214
run(int)215 void CV_VideoRandomPositioningTest::run(int)
216 {
217 run_test(RANDOM);
218 }
219
220 #if BUILD_WITH_VIDEO_INPUT_SUPPORT && defined HAVE_FFMPEG
TEST(Videoio_Video,seek_progressive)221 TEST (Videoio_Video, seek_progressive) { CV_VideoProgressivePositioningTest test; test.safe_run(); }
TEST(Videoio_Video,seek_random)222 TEST (Videoio_Video, seek_random) { CV_VideoRandomPositioningTest test; test.safe_run(); }
223 #endif
224