Updated positioning test (removed frame reading)
[profile/ivi/opencv.git] / modules / highgui / test / test_positioning.cpp
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/highgui/highgui.hpp"
45 #include <stdio.h>
46
47 using namespace cv;
48 using namespace std;
49
50 enum NAVIGATION_METHOD {PROGRESSIVE, RANDOM};
51
52 class CV_VideoPositioningTest: public cvtest::BaseTest
53 {
54 public:
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:
70         CV_VideoProgressivePositioningTest() : CV_VideoPositioningTest() {};
71         ~CV_VideoProgressivePositioningTest();
72         void run(int);
73 };
74
75 class CV_VideoRandomPositioningTest: public CV_VideoPositioningTest
76 {
77 public:
78         CV_VideoRandomPositioningTest(): CV_VideoPositioningTest() {};
79         ~CV_VideoRandomPositioningTest();
80         void run(int);
81 };
82
83 CV_VideoPositioningTest::CV_VideoPositioningTest() {}
84 CV_VideoPositioningTest::~CV_VideoPositioningTest() {}
85 CV_VideoProgressivePositioningTest::~CV_VideoProgressivePositioningTest() {}
86 CV_VideoRandomPositioningTest::~CV_VideoRandomPositioningTest() {}
87
88 void CV_VideoPositioningTest::generate_idx_seq(CvCapture* cap, int method)
89 {
90         idx.clear();
91     int N = (int)cvGetCaptureProperty(cap, CV_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
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[] = {"mov", "avi", "mp4", "mpg", "wmv"};
126
127     size_t n = sizeof(ext)/sizeof(ext[0]);
128
129     int failed_videos = 0;
130
131         for (size_t i = 0; i < n; ++i)
132         {
133         string file_path = src_dir + "video/big_buck_bunny." + ext[i];
134
135         printf("\nReading video file in %s...\n", file_path.c_str());
136
137                 CvCapture* cap = cvCreateFileCapture(file_path.c_str());
138
139                 if (!cap)
140                 {
141                         ts->printf(cvtest::TS::LOG, "\nFile information (video %d): \n\nName: big_buck_bunny.%s\nFAILED\n\n", i+1, ext[i].c_str());
142             ts->printf(cvtest::TS::LOG, "Error: cannot read source video file.\n");
143                         ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
144             failed_videos++; continue;
145                 }
146
147                 cvSetCaptureProperty(cap, CV_CAP_PROP_POS_FRAMES, 0);
148
149                 generate_idx_seq(cap, method);
150
151         int N = idx.size(), failed_frames = 0, failed_positions = 0, failed_iterations = 0;
152
153         for (int j = 0; j < N; ++j)
154                 {
155             bool flag = false;
156
157             cvSetCaptureProperty(cap, CV_CAP_PROP_POS_FRAMES, idx.at(j));
158
159             /* IplImage* frame = cvRetrieveFrame(cap);
160
161             if (!frame)
162                         {
163                 if (!failed_frames)
164                 {
165                     ts->printf(cvtest::TS::LOG, "\nFile information (video %d): \n\nName: big_buck_bunny.%s\n", i+1, ext[i].c_str());
166                 }
167                 failed_frames++;
168                 ts->printf(cvtest::TS::LOG, "\nIteration: %d\n\nError: cannot read a frame with index %d.\n", j, idx.at(j));
169                 ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
170                 flag = !flag;
171             } */
172
173             int val = (int)cvGetCaptureProperty(cap, CV_CAP_PROP_POS_FRAMES);
174
175             if (idx.at(j) != val)
176                         {
177                 if (!(failed_frames||failed_positions))
178                 {
179                     ts->printf(cvtest::TS::LOG, "\nFile information (video %d): \n\nName: big_buck_bunny.%s\n", i+1, ext[i].c_str());
180                 }
181                 failed_positions++;
182                 if (!failed_frames)
183                 {
184                     ts->printf(cvtest::TS::LOG, "\nIteration: %d\n", j);
185                 }
186                 ts->printf(cvtest::TS::LOG, "Required pos: %d\nReturned pos: %d\n", idx.at(j), val);
187                 ts->printf(cvtest::TS::LOG, "Error: required and returned positions are not matched.\n");
188                                 ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
189                 if (!flag) flag = !flag;
190                         }
191
192             if (flag) failed_iterations++;
193                 }
194
195         ts->printf(cvtest::TS::LOG, "\nSuccessfull iterations: %d (%d%%)\n", idx.size()-failed_iterations, 100*(idx.size()-failed_iterations)/idx.size());
196         ts->printf(cvtest::TS::LOG, "Failed iterations: %d (%d%%)\n", failed_iterations, 100*failed_iterations/idx.size());
197
198         if (failed_frames||failed_positions)
199         {
200             ts->printf(cvtest::TS::LOG, "\nFAILED\n----------\n"); failed_videos++;
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
210 void CV_VideoProgressivePositioningTest::run(int) 
211 {
212 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
213 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
214
215         run_test(PROGRESSIVE);
216
217 #endif
218 #endif
219 }
220
221 void CV_VideoRandomPositioningTest::run(int)
222 {
223 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
224 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
225
226         run_test(RANDOM);
227
228 #endif
229 #endif
230 }
231
232 TEST (HighguiPositioning, progressive) { CV_VideoProgressivePositioningTest test; test.safe_run(); }
233 TEST (HighguiPositioning, random) { CV_VideoRandomPositioningTest test; test.safe_run(); }