Merge pull request #1670 from jet47:cuda-cmake-fix
[profile/ivi/opencv.git] / modules / highgui / test / test_ffmpeg.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
46 using namespace cv;
47
48 #ifdef HAVE_FFMPEG
49
50 using namespace std;
51
52 class CV_FFmpegWriteBigVideoTest : public cvtest::BaseTest
53 {
54 public:
55     void run(int)
56     {
57         const int img_r = 4096;
58         const int img_c = 4096;
59         const double fps0 = 15;
60         const double time_sec = 1;
61
62         const int tags[] = {
63             0,
64             //CV_FOURCC('D', 'I', 'V', '3'),
65             //CV_FOURCC('D', 'I', 'V', 'X'),
66             CV_FOURCC('D', 'X', '5', '0'),
67             CV_FOURCC('F', 'L', 'V', '1'),
68             CV_FOURCC('H', '2', '6', '1'),
69             CV_FOURCC('H', '2', '6', '3'),
70             CV_FOURCC('I', '4', '2', '0'),
71             //CV_FOURCC('j', 'p', 'e', 'g'),
72             CV_FOURCC('M', 'J', 'P', 'G'),
73             CV_FOURCC('m', 'p', '4', 'v'),
74             CV_FOURCC('M', 'P', 'E', 'G'),
75             //CV_FOURCC('W', 'M', 'V', '1'),
76             //CV_FOURCC('W', 'M', 'V', '2'),
77             CV_FOURCC('X', 'V', 'I', 'D'),
78             //CV_FOURCC('Y', 'U', 'Y', '2'),
79         };
80
81         const size_t n = sizeof(tags)/sizeof(tags[0]);
82
83         bool created = false;
84
85         for (size_t j = 0; j < n; ++j)
86         {
87             int tag = tags[j];
88             stringstream s;
89             s << tag;
90
91             const string filename = "output_"+s.str()+".avi";
92
93             try
94             {
95                 double fps = fps0;
96                 Size frame_s = Size(img_c, img_r);
97
98                 if( tag == CV_FOURCC('H', '2', '6', '1') )
99                     frame_s = Size(352, 288);
100                 else if( tag == CV_FOURCC('H', '2', '6', '3') )
101                     frame_s = Size(704, 576);
102                 /*else if( tag == CV_FOURCC('M', 'J', 'P', 'G') ||
103                          tag == CV_FOURCC('j', 'p', 'e', 'g') )
104                     frame_s = Size(1920, 1080);*/
105
106                 if( tag == CV_FOURCC('M', 'P', 'E', 'G') )
107                 {
108                     frame_s = Size(720, 576);
109                     fps = 25;
110                 }
111
112                 VideoWriter writer(filename, tag, fps, frame_s);
113
114                 if (writer.isOpened() == false)
115                 {
116                     ts->printf(ts->LOG, "\n\nFile name: %s\n", filename.c_str());
117                     ts->printf(ts->LOG, "Codec id: %d   Codec tag: %c%c%c%c\n", j,
118                                tag & 255, (tag >> 8) & 255, (tag >> 16) & 255, (tag >> 24) & 255);
119                     ts->printf(ts->LOG, "Error: cannot create video file.");
120                     ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
121                 }
122                 else
123                 {
124                     Mat img(frame_s, CV_8UC3, Scalar::all(0));
125                     const int coeff = cvRound(min(frame_s.width, frame_s.height)/(fps0 * time_sec));
126
127                     for (int i = 0 ; i < static_cast<int>(fps * time_sec); i++ )
128                     {
129                         //circle(img, Point2i(img_c / 2, img_r / 2), min(img_r, img_c) / 2 * (i + 1), Scalar(255, 0, 0, 0), 2);
130                         rectangle(img, Point2i(coeff * i, coeff * i), Point2i(coeff * (i + 1), coeff * (i + 1)),
131                                   Scalar::all(255 * (1.0 - static_cast<double>(i) / (fps * time_sec * 2) )), -1);
132                         writer << img;
133                     }
134
135                     if (!created) created = true;
136                     else remove(filename.c_str());
137                 }
138             }
139             catch(...)
140             {
141                 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
142             }
143             ts->set_failed_test_info(cvtest::TS::OK);
144         }
145     }
146 };
147
148 TEST(Highgui_Video, ffmpeg_writebig) { CV_FFmpegWriteBigVideoTest test; test.safe_run(); }
149
150 class CV_FFmpegReadImageTest : public cvtest::BaseTest
151 {
152 public:
153     void run(int)
154     {
155         try
156         {
157             string filename = ts->get_data_path() + "../cv/features2d/tsukuba.png";
158             VideoCapture cap(filename);
159             Mat img0 = imread(filename, 1);
160             Mat img, img_next;
161             cap >> img;
162             cap >> img_next;
163
164             CV_Assert( !img0.empty() && !img.empty() && img_next.empty() );
165
166             double diff = norm(img0, img, CV_C);
167             CV_Assert( diff == 0 );
168         }
169         catch(...)
170         {
171             ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
172         }
173         ts->set_failed_test_info(cvtest::TS::OK);
174     }
175 };
176
177 TEST(Highgui_Video, ffmpeg_image) { CV_FFmpegReadImageTest test; test.safe_run(); }
178
179 #endif
180
181 #if defined(HAVE_FFMPEG)
182
183 //////////////////////////////// Parallel VideoWriters and VideoCaptures ////////////////////////////////////
184
185 class CreateVideoWriterInvoker :
186     public ParallelLoopBody
187 {
188 public:
189     const static Size FrameSize;
190     static std::string TmpDirectory;
191
192     CreateVideoWriterInvoker(std::vector<VideoWriter*>& _writers, std::vector<std::string>& _files) :
193         ParallelLoopBody(), writers(&_writers), files(&_files)
194     {
195     }
196
197     virtual void operator() (const Range& range) const
198     {
199         for (int i = range.start; i != range.end; ++i)
200         {
201             std::ostringstream stream;
202             stream << i << ".avi";
203             std::string fileName = tempfile(stream.str().c_str());
204
205             files->operator[](i) = fileName;
206             writers->operator[](i) = new VideoWriter(fileName, CV_FOURCC('X','V','I','D'), 25.0f, FrameSize);
207
208             CV_Assert(writers->operator[](i)->isOpened());
209         }
210     }
211
212 private:
213     std::vector<VideoWriter*>* writers;
214     std::vector<std::string>* files;
215 };
216
217 std::string CreateVideoWriterInvoker::TmpDirectory;
218 const Size CreateVideoWriterInvoker::FrameSize(1020, 900);
219
220 class WriteVideo_Invoker :
221     public ParallelLoopBody
222 {
223 public:
224     enum { FrameCount = 300 };
225
226     static const Scalar ObjectColor;
227     static const Point Center;
228
229     WriteVideo_Invoker(const std::vector<VideoWriter*>& _writers) :
230         ParallelLoopBody(), writers(&_writers)
231     {
232     }
233
234     static void GenerateFrame(Mat& frame, unsigned int i)
235     {
236         frame = Scalar::all(i % 255);
237
238         std::string text = to_string(i);
239         putText(frame, text, Point(50, Center.y), FONT_HERSHEY_SIMPLEX, 5.0, ObjectColor, 5, CV_AA);
240         circle(frame, Center, i + 2, ObjectColor, 2, CV_AA);
241     }
242
243     virtual void operator() (const Range& range) const
244     {
245         for (int j = range.start; j < range.end; ++j)
246         {
247             VideoWriter* writer = writers->operator[](j);
248             CV_Assert(writer != NULL);
249             CV_Assert(writer->isOpened());
250
251             Mat frame(CreateVideoWriterInvoker::FrameSize, CV_8UC3);
252             for (unsigned int i = 0; i < FrameCount; ++i)
253             {
254                 GenerateFrame(frame, i);
255                 writer->operator<< (frame);
256             }
257         }
258     }
259
260 protected:
261     static std::string to_string(unsigned int i)
262     {
263         std::stringstream stream(std::ios::out);
264         stream << "frame #" << i;
265         return stream.str();
266     }
267
268 private:
269     const std::vector<VideoWriter*>* writers;
270 };
271
272 const Scalar WriteVideo_Invoker::ObjectColor(Scalar::all(0));
273 const Point WriteVideo_Invoker::Center(CreateVideoWriterInvoker::FrameSize.height / 2,
274     CreateVideoWriterInvoker::FrameSize.width / 2);
275
276 class CreateVideoCaptureInvoker :
277     public ParallelLoopBody
278 {
279 public:
280     CreateVideoCaptureInvoker(std::vector<VideoCapture*>& _readers, const std::vector<std::string>& _files) :
281         ParallelLoopBody(), readers(&_readers), files(&_files)
282     {
283     }
284
285     virtual void operator() (const Range& range) const
286     {
287         for (int i = range.start; i != range.end; ++i)
288         {
289             readers->operator[](i) = new VideoCapture(files->operator[](i));
290             CV_Assert(readers->operator[](i)->isOpened());
291         }
292     }
293 private:
294     std::vector<VideoCapture*>* readers;
295     const std::vector<std::string>* files;
296 };
297
298 class ReadImageAndTest :
299     public ParallelLoopBody
300 {
301 public:
302     ReadImageAndTest(const std::vector<VideoCapture*>& _readers, cvtest::TS* _ts) :
303         ParallelLoopBody(), readers(&_readers), ts(_ts)
304     {
305     }
306
307     virtual void operator() (const Range& range) const
308     {
309         for (int j = range.start; j < range.end; ++j)
310         {
311             VideoCapture* capture = readers->operator[](j);
312             CV_Assert(capture != NULL);
313             CV_Assert(capture->isOpened());
314
315             const static double eps = 23.0;
316             unsigned int frameCount = static_cast<unsigned int>(capture->get(CV_CAP_PROP_FRAME_COUNT));
317             CV_Assert(frameCount == WriteVideo_Invoker::FrameCount);
318             Mat reference(CreateVideoWriterInvoker::FrameSize, CV_8UC3);
319
320             for (unsigned int i = 0; i < frameCount && next; ++i)
321             {
322                 Mat actual;
323                 (*capture) >> actual;
324
325                 WriteVideo_Invoker::GenerateFrame(reference, i);
326
327                 EXPECT_EQ(reference.cols, actual.cols);
328                 EXPECT_EQ(reference.rows, actual.rows);
329                 EXPECT_EQ(reference.depth(), actual.depth());
330                 EXPECT_EQ(reference.channels(), actual.channels());
331
332                 double psnr = PSNR(actual, reference);
333                 if (psnr < eps)
334                 {
335     #define SUM cvtest::TS::SUMMARY
336                     ts->printf(SUM, "\nPSNR: %lf\n", psnr);
337                     ts->printf(SUM, "Video #: %d\n", range.start);
338                     ts->printf(SUM, "Frame #: %d\n", i);
339     #undef SUM
340                     ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
341                     ts->set_gtest_status();
342
343                     Mat diff;
344                     absdiff(actual, reference, diff);
345
346                     EXPECT_EQ(countNonZero(diff.reshape(1) > 1), 0);
347
348                     next = false;
349                 }
350             }
351         }
352     }
353
354     static bool next;
355
356 private:
357     const std::vector<VideoCapture*>* readers;
358     cvtest::TS* ts;
359 };
360
361 bool ReadImageAndTest::next;
362
363 TEST(Highgui_Video_parallel_writers_and_readers, accuracy)
364 {
365     const unsigned int threadsCount = 4;
366     cvtest::TS* ts = cvtest::TS::ptr();
367
368     // creating VideoWriters
369     std::vector<VideoWriter*> writers(threadsCount);
370     Range range(0, threadsCount);
371     std::vector<std::string> files(threadsCount);
372     CreateVideoWriterInvoker invoker1(writers, files);
373     parallel_for_(range, invoker1);
374
375     // write a video
376     parallel_for_(range, WriteVideo_Invoker(writers));
377
378     // deleting the writers
379     for (std::vector<VideoWriter*>::iterator i = writers.begin(), end = writers.end(); i != end; ++i)
380         delete *i;
381     writers.clear();
382
383     std::vector<VideoCapture*> readers(threadsCount);
384     CreateVideoCaptureInvoker invoker2(readers, files);
385     parallel_for_(range, invoker2);
386
387     ReadImageAndTest::next = true;
388
389     parallel_for_(range, ReadImageAndTest(readers, ts));
390
391     // deleting tmp video files
392     for (std::vector<std::string>::const_iterator i = files.begin(), end = files.end(); i != end; ++i)
393     {
394         int code = remove(i->c_str());
395         if (code == 1)
396             std::cerr << "Couldn't delete " << *i << std::endl;
397     }
398 }
399
400 #endif