1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "test_precomp.hpp"
44 #include "opencv2/highgui.hpp"
52 class CV_FFmpegWriteBigVideoTest : public cvtest::BaseTest
57 const int img_r = 4096;
58 const int img_c = 4096;
59 const double fps0 = 15;
60 const double time_sec = 1;
64 //VideoWriter::fourcc('D', 'I', 'V', '3'),
65 //VideoWriter::fourcc('D', 'I', 'V', 'X'),
66 VideoWriter::fourcc('D', 'X', '5', '0'),
67 VideoWriter::fourcc('F', 'L', 'V', '1'),
68 VideoWriter::fourcc('H', '2', '6', '1'),
69 VideoWriter::fourcc('H', '2', '6', '3'),
70 VideoWriter::fourcc('I', '4', '2', '0'),
71 //VideoWriter::fourcc('j', 'p', 'e', 'g'),
72 VideoWriter::fourcc('M', 'J', 'P', 'G'),
73 VideoWriter::fourcc('m', 'p', '4', 'v'),
74 VideoWriter::fourcc('M', 'P', 'E', 'G'),
75 //VideoWriter::fourcc('W', 'M', 'V', '1'),
76 //VideoWriter::fourcc('W', 'M', 'V', '2'),
77 VideoWriter::fourcc('X', 'V', 'I', 'D'),
78 //VideoWriter::fourcc('Y', 'U', 'Y', '2'),
81 const size_t n = sizeof(tags)/sizeof(tags[0]);
85 for (size_t j = 0; j < n; ++j)
91 const string filename = "output_"+s.str()+".avi";
96 Size frame_s = Size(img_c, img_r);
98 if( tag == VideoWriter::fourcc('H', '2', '6', '1') )
99 frame_s = Size(352, 288);
100 else if( tag == VideoWriter::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);*/
106 if( tag == VideoWriter::fourcc('M', 'P', 'E', 'G') )
108 frame_s = Size(720, 576);
112 VideoWriter writer(filename, tag, fps, frame_s);
114 if (writer.isOpened() == false)
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);
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));
127 for (int i = 0 ; i < static_cast<int>(fps * time_sec); i++ )
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);
135 if (!created) created = true;
136 else remove(filename.c_str());
141 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
143 ts->set_failed_test_info(cvtest::TS::OK);
149 TEST(Highgui_Video, ffmpeg_writebig) { CV_FFmpegWriteBigVideoTest test; test.safe_run(); }
151 class CV_FFmpegReadImageTest : public cvtest::BaseTest
158 string filename = ts->get_data_path() + "../cv/features2d/tsukuba.png";
159 VideoCapture cap(filename);
160 Mat img0 = imread(filename, 1);
165 CV_Assert( !img0.empty() && !img.empty() && img_next.empty() );
167 double diff = norm(img0, img, CV_C);
168 CV_Assert( diff == 0 );
172 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
174 ts->set_failed_test_info(cvtest::TS::OK);
178 TEST(Highgui_Video, ffmpeg_image) { CV_FFmpegReadImageTest test; test.safe_run(); }
182 #if defined(HAVE_FFMPEG)
184 //////////////////////////////// Parallel VideoWriters and VideoCaptures ////////////////////////////////////
186 class CreateVideoWriterInvoker :
187 public ParallelLoopBody
190 const static Size FrameSize;
191 static std::string TmpDirectory;
193 CreateVideoWriterInvoker(std::vector<VideoWriter*>& _writers, std::vector<std::string>& _files) :
194 ParallelLoopBody(), writers(&_writers), files(&_files)
198 virtual void operator() (const Range& range) const
200 for (int i = range.start; i != range.end; ++i)
202 std::ostringstream stream;
203 stream << i << ".avi";
204 std::string fileName = tempfile(stream.str().c_str());
206 files->operator[](i) = fileName;
207 writers->operator[](i) = new VideoWriter(fileName, VideoWriter::fourcc('X','V','I','D'), 25.0f, FrameSize);
209 CV_Assert(writers->operator[](i)->isOpened());
214 std::vector<VideoWriter*>* writers;
215 std::vector<std::string>* files;
218 std::string CreateVideoWriterInvoker::TmpDirectory;
219 const Size CreateVideoWriterInvoker::FrameSize(1020, 900);
221 class WriteVideo_Invoker :
222 public ParallelLoopBody
225 enum { FrameCount = 300 };
227 static const Scalar ObjectColor;
228 static const Point Center;
230 WriteVideo_Invoker(const std::vector<VideoWriter*>& _writers) :
231 ParallelLoopBody(), writers(&_writers)
235 static void GenerateFrame(Mat& frame, unsigned int i)
237 frame = Scalar::all(i % 255);
239 std::string text = to_string(i);
240 putText(frame, text, Point(50, Center.y), FONT_HERSHEY_SIMPLEX, 5.0, ObjectColor, 5, CV_AA);
241 circle(frame, Center, i + 2, ObjectColor, 2, CV_AA);
244 virtual void operator() (const Range& range) const
246 for (int j = range.start; j < range.end; ++j)
248 VideoWriter* writer = writers->operator[](j);
249 CV_Assert(writer != NULL);
250 CV_Assert(writer->isOpened());
252 Mat frame(CreateVideoWriterInvoker::FrameSize, CV_8UC3);
253 for (unsigned int i = 0; i < FrameCount; ++i)
255 GenerateFrame(frame, i);
256 writer->operator<< (frame);
262 static std::string to_string(unsigned int i)
264 std::stringstream stream(std::ios::out);
265 stream << "frame #" << i;
270 const std::vector<VideoWriter*>* writers;
273 const Scalar WriteVideo_Invoker::ObjectColor(Scalar::all(0));
274 const Point WriteVideo_Invoker::Center(CreateVideoWriterInvoker::FrameSize.height / 2,
275 CreateVideoWriterInvoker::FrameSize.width / 2);
277 class CreateVideoCaptureInvoker :
278 public ParallelLoopBody
281 CreateVideoCaptureInvoker(std::vector<VideoCapture*>& _readers, const std::vector<std::string>& _files) :
282 ParallelLoopBody(), readers(&_readers), files(&_files)
286 virtual void operator() (const Range& range) const
288 for (int i = range.start; i != range.end; ++i)
290 readers->operator[](i) = new VideoCapture(files->operator[](i));
291 CV_Assert(readers->operator[](i)->isOpened());
295 std::vector<VideoCapture*>* readers;
296 const std::vector<std::string>* files;
299 class ReadImageAndTest :
300 public ParallelLoopBody
303 ReadImageAndTest(const std::vector<VideoCapture*>& _readers, cvtest::TS* _ts) :
304 ParallelLoopBody(), readers(&_readers), ts(_ts)
308 virtual void operator() (const Range& range) const
310 for (int j = range.start; j < range.end; ++j)
312 VideoCapture* capture = readers->operator[](j);
313 CV_Assert(capture != NULL);
314 CV_Assert(capture->isOpened());
316 const static double eps = 23.0;
317 unsigned int frameCount = static_cast<unsigned int>(capture->get(CAP_PROP_FRAME_COUNT));
318 CV_Assert(frameCount == WriteVideo_Invoker::FrameCount);
319 Mat reference(CreateVideoWriterInvoker::FrameSize, CV_8UC3);
321 for (unsigned int i = 0; i < frameCount && next; ++i)
324 (*capture) >> actual;
326 WriteVideo_Invoker::GenerateFrame(reference, i);
328 EXPECT_EQ(reference.cols, actual.cols);
329 EXPECT_EQ(reference.rows, actual.rows);
330 EXPECT_EQ(reference.depth(), actual.depth());
331 EXPECT_EQ(reference.channels(), actual.channels());
333 double psnr = PSNR(actual, reference);
336 #define SUM cvtest::TS::SUMMARY
337 ts->printf(SUM, "\nPSNR: %lf\n", psnr);
338 ts->printf(SUM, "Video #: %d\n", range.start);
339 ts->printf(SUM, "Frame #: %d\n", i);
341 ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
342 ts->set_gtest_status();
345 absdiff(actual, reference, diff);
347 EXPECT_EQ(countNonZero(diff.reshape(1) > 1), 0);
358 const std::vector<VideoCapture*>* readers;
362 bool ReadImageAndTest::next;
364 TEST(Highgui_Video_parallel_writers_and_readers, accuracy)
366 const unsigned int threadsCount = 4;
367 cvtest::TS* ts = cvtest::TS::ptr();
369 // creating VideoWriters
370 std::vector<VideoWriter*> writers(threadsCount);
371 Range range(0, threadsCount);
372 std::vector<std::string> files(threadsCount);
373 CreateVideoWriterInvoker invoker1(writers, files);
374 parallel_for_(range, invoker1);
377 parallel_for_(range, WriteVideo_Invoker(writers));
379 // deleting the writers
380 for (std::vector<VideoWriter*>::iterator i = writers.begin(), end = writers.end(); i != end; ++i)
384 std::vector<VideoCapture*> readers(threadsCount);
385 CreateVideoCaptureInvoker invoker2(readers, files);
386 parallel_for_(range, invoker2);
388 ReadImageAndTest::next = true;
390 parallel_for_(range, ReadImageAndTest(readers, ts));
392 // deleting tmp video files
393 for (std::vector<std::string>::const_iterator i = files.begin(), end = files.end(); i != end; ++i)
395 int code = remove(i->c_str());
397 std::cerr << "Couldn't delete " << *i << std::endl;