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