Path: blob/master/modules/videoio/test/test_ffmpeg.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546#ifdef HAVE_FFMPEG4748using namespace std;4950static const char* AVI_EXT = ".avi";51static const char* MP4_EXT = ".mp4";5253class CV_FFmpegWriteBigVideoTest : public cvtest::BaseTest54{55struct TestFormatEntry {56int tag;57const char* ext;58bool required;59};6061static long int getFileSize(string filename)62{63FILE *p_file = NULL;64p_file = fopen(filename.c_str(), "rb");65if (p_file == NULL)66return -1;67fseek(p_file, 0, SEEK_END);68long int size = ftell(p_file);69fclose(p_file);70return size;71}72public:73void run(int)74{75const int img_r = 4096;76const int img_c = 4096;77const double fps0 = 15;78const double time_sec = 1;7980const TestFormatEntry entries[] = {81{0, AVI_EXT, true},82//{VideoWriter::fourcc('D', 'I', 'V', '3'), AVI_EXT, true},83//{VideoWriter::fourcc('D', 'I', 'V', 'X'), AVI_EXT, true},84{VideoWriter::fourcc('D', 'X', '5', '0'), AVI_EXT, true},85{VideoWriter::fourcc('F', 'L', 'V', '1'), AVI_EXT, true},86{VideoWriter::fourcc('H', '2', '6', '1'), AVI_EXT, true},87{VideoWriter::fourcc('H', '2', '6', '3'), AVI_EXT, true},88{VideoWriter::fourcc('I', '4', '2', '0'), AVI_EXT, true},89//{VideoWriter::fourcc('j', 'p', 'e', 'g'), AVI_EXT, true},90{VideoWriter::fourcc('M', 'J', 'P', 'G'), AVI_EXT, true},91{VideoWriter::fourcc('m', 'p', '4', 'v'), AVI_EXT, true},92{VideoWriter::fourcc('M', 'P', 'E', 'G'), AVI_EXT, true},93//{VideoWriter::fourcc('W', 'M', 'V', '1'), AVI_EXT, true},94//{VideoWriter::fourcc('W', 'M', 'V', '2'), AVI_EXT, true},95{VideoWriter::fourcc('X', 'V', 'I', 'D'), AVI_EXT, true},96//{VideoWriter::fourcc('Y', 'U', 'Y', '2'), AVI_EXT, true},97{VideoWriter::fourcc('H', '2', '6', '4'), MP4_EXT, false}98};99100const size_t n = sizeof(entries)/sizeof(entries[0]);101102for (size_t j = 0; j < n; ++j)103{104int tag = entries[j].tag;105const char* ext = entries[j].ext;106string s = cv::format("%08x%s", tag, ext);107108const string filename = tempfile(s.c_str());109110try111{112double fps = fps0;113Size frame_s = Size(img_c, img_r);114115if( tag == VideoWriter::fourcc('H', '2', '6', '1') )116frame_s = Size(352, 288);117else if( tag == VideoWriter::fourcc('H', '2', '6', '3') )118frame_s = Size(704, 576);119else if( tag == VideoWriter::fourcc('H', '2', '6', '4') )120// OpenH264 1.5.0 has resolution limitations, so lets use DCI 4K resolution121frame_s = Size(4096, 2160);122/*else if( tag == CV_FOURCC('M', 'J', 'P', 'G') ||123tag == CV_FOURCC('j', 'p', 'e', 'g') )124frame_s = Size(1920, 1080);*/125126if( tag == VideoWriter::fourcc('M', 'P', 'E', 'G') )127{128frame_s = Size(720, 576);129fps = 25;130}131132VideoWriter writer(filename, CAP_FFMPEG, tag, fps, frame_s);133134if (writer.isOpened() == false)135{136fprintf(stderr, "\n\nFile name: %s\n", filename.c_str());137fprintf(stderr, "Codec id: %d Codec tag: %c%c%c%c\n", (int)j,138tag & 255, (tag >> 8) & 255, (tag >> 16) & 255, (tag >> 24) & 255);139fprintf(stderr, "Error: cannot create video file.\n");140if (entries[j].required)141ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);142}143else144{145Mat img(frame_s, CV_8UC3, Scalar::all(0));146const int coeff = cvRound(min(frame_s.width, frame_s.height)/(fps0 * time_sec));147148for (int i = 0 ; i < static_cast<int>(fps * time_sec); i++ )149{150//circle(img, Point2i(img_c / 2, img_r / 2), min(img_r, img_c) / 2 * (i + 1), Scalar(255, 0, 0, 0), 2);151rectangle(img, Point2i(coeff * i, coeff * i), Point2i(coeff * (i + 1), coeff * (i + 1)),152Scalar::all(255 * (1.0 - static_cast<double>(i) / (fps * time_sec * 2) )), -1);153writer << img;154}155156writer.release();157long int sz = getFileSize(filename);158if (sz < 0)159{160fprintf(stderr, "ERROR: File name: %s was not created\n", filename.c_str());161if (entries[j].required)162ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);163}164else165{166printf("Case: '%s' (frame size %dx%d fps=%g). FileSize=%lld bytes\n",167s.c_str(), frame_s.width, frame_s.height, fps, (long long int)sz);168if (sz < 8192)169{170fprintf(stderr, "ERROR: File name: %s is very small (data write problems?)\n", filename.c_str());171if (entries[j].required)172ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);173}174remove(filename.c_str());175}176}177}178catch(...)179{180ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);181}182ts->set_failed_test_info(cvtest::TS::OK);183}184}185};186187TEST(Videoio_Video, ffmpeg_writebig) { CV_FFmpegWriteBigVideoTest test; test.safe_run(); }188189class CV_FFmpegReadImageTest : public cvtest::BaseTest190{191public:192void run(int)193{194try195{196string filename = ts->get_data_path() + "readwrite/ordinary.bmp";197VideoCapture cap(filename, CAP_FFMPEG);198Mat img0 = imread(filename, 1);199Mat img, img_next;200cap >> img;201cap >> img_next;202203CV_Assert( !img0.empty() && !img.empty() && img_next.empty() );204205double diff = cvtest::norm(img0, img, CV_C);206CV_Assert( diff == 0 );207}208catch(...)209{210ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);211}212ts->set_failed_test_info(cvtest::TS::OK);213}214};215216TEST(Videoio_Video, ffmpeg_image) { CV_FFmpegReadImageTest test; test.safe_run(); }217218#endif219220#if defined(HAVE_FFMPEG)221222//////////////////////////////// Parallel VideoWriters and VideoCaptures ////////////////////////////////////223224class CreateVideoWriterInvoker :225public ParallelLoopBody226{227public:228const static Size FrameSize;229static std::string TmpDirectory;230231CreateVideoWriterInvoker(std::vector<VideoWriter*>& _writers, std::vector<std::string>& _files) :232writers(_writers), files(_files)233{234}235236virtual void operator() (const Range& range) const CV_OVERRIDE237{238for (int i = range.start; i != range.end; ++i)239{240std::ostringstream stream;241stream << i << ".avi";242std::string fileName = tempfile(stream.str().c_str());243244files[i] = fileName;245writers[i] = new VideoWriter(fileName, CAP_FFMPEG, VideoWriter::fourcc('X','V','I','D'), 25.0f, FrameSize);246247CV_Assert(writers[i]->isOpened());248}249}250251private:252std::vector<VideoWriter*>& writers;253std::vector<std::string>& files;254};255256std::string CreateVideoWriterInvoker::TmpDirectory;257const Size CreateVideoWriterInvoker::FrameSize(1020, 900);258259class WriteVideo_Invoker :260public ParallelLoopBody261{262public:263enum { FrameCount = 300 };264265static const Scalar ObjectColor;266static const Point Center;267268WriteVideo_Invoker(const std::vector<VideoWriter*>& _writers) :269ParallelLoopBody(), writers(&_writers)270{271}272273static void GenerateFrame(Mat& frame, unsigned int i)274{275frame = Scalar::all(i % 255);276277std::string text = to_string(i);278putText(frame, text, Point(50, Center.y), FONT_HERSHEY_SIMPLEX, 5.0, ObjectColor, 5, CV_AA);279circle(frame, Center, i + 2, ObjectColor, 2, CV_AA);280}281282virtual void operator() (const Range& range) const CV_OVERRIDE283{284for (int j = range.start; j < range.end; ++j)285{286VideoWriter* writer = writers->operator[](j);287CV_Assert(writer != NULL);288CV_Assert(writer->isOpened());289290Mat frame(CreateVideoWriterInvoker::FrameSize, CV_8UC3);291for (unsigned int i = 0; i < FrameCount; ++i)292{293GenerateFrame(frame, i);294writer->operator<< (frame);295}296}297}298299protected:300static std::string to_string(unsigned int i)301{302std::stringstream stream(std::ios::out);303stream << "frame #" << i;304return stream.str();305}306307private:308const std::vector<VideoWriter*>* writers;309};310311const Scalar WriteVideo_Invoker::ObjectColor(Scalar::all(0));312const Point WriteVideo_Invoker::Center(CreateVideoWriterInvoker::FrameSize.height / 2,313CreateVideoWriterInvoker::FrameSize.width / 2);314315class CreateVideoCaptureInvoker :316public ParallelLoopBody317{318public:319CreateVideoCaptureInvoker(std::vector<VideoCapture*>& _readers, const std::vector<std::string>& _files) :320ParallelLoopBody(), readers(&_readers), files(&_files)321{322}323324virtual void operator() (const Range& range) const CV_OVERRIDE325{326for (int i = range.start; i != range.end; ++i)327{328readers->operator[](i) = new VideoCapture(files->operator[](i), CAP_FFMPEG);329CV_Assert(readers->operator[](i)->isOpened());330}331}332private:333std::vector<VideoCapture*>* readers;334const std::vector<std::string>* files;335};336337class ReadImageAndTest :338public ParallelLoopBody339{340public:341ReadImageAndTest(const std::vector<VideoCapture*>& _readers, cvtest::TS* _ts) :342ParallelLoopBody(), readers(&_readers), ts(_ts)343{344}345346virtual void operator() (const Range& range) const CV_OVERRIDE347{348for (int j = range.start; j < range.end; ++j)349{350VideoCapture* capture = readers->operator[](j);351CV_Assert(capture != NULL);352CV_Assert(capture->isOpened());353354const static double eps = 23.0;355unsigned int frameCount = static_cast<unsigned int>(capture->get(CAP_PROP_FRAME_COUNT));356CV_Assert(frameCount == WriteVideo_Invoker::FrameCount);357Mat reference(CreateVideoWriterInvoker::FrameSize, CV_8UC3);358359for (unsigned int i = 0; i < frameCount && next; ++i)360{361SCOPED_TRACE(cv::format("frame=%d/%d", (int)i, (int)frameCount));362363Mat actual;364(*capture) >> actual;365366WriteVideo_Invoker::GenerateFrame(reference, i);367368EXPECT_EQ(reference.cols, actual.cols);369EXPECT_EQ(reference.rows, actual.rows);370EXPECT_EQ(reference.depth(), actual.depth());371EXPECT_EQ(reference.channels(), actual.channels());372373double psnr = cvtest::PSNR(actual, reference);374if (psnr < eps)375{376#define SUM cvtest::TS::SUMMARY377ts->printf(SUM, "\nPSNR: %lf\n", psnr);378ts->printf(SUM, "Video #: %d\n", range.start);379ts->printf(SUM, "Frame #: %d\n", i);380#undef SUM381ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);382ts->set_gtest_status();383384Mat diff;385absdiff(actual, reference, diff);386387EXPECT_EQ(countNonZero(diff.reshape(1) > 1), 0);388389next = false;390}391}392}393}394395static bool next;396397private:398const std::vector<VideoCapture*>* readers;399cvtest::TS* ts;400};401402bool ReadImageAndTest::next;403404TEST(Videoio_Video_parallel_writers_and_readers, accuracy)405{406const unsigned int threadsCount = 4;407cvtest::TS* ts = cvtest::TS::ptr();408409// creating VideoWriters410std::vector<VideoWriter*> writers(threadsCount);411Range range(0, threadsCount);412std::vector<std::string> files(threadsCount);413CreateVideoWriterInvoker invoker1(writers, files);414parallel_for_(range, invoker1);415416// write a video417parallel_for_(range, WriteVideo_Invoker(writers));418419// deleting the writers420for (std::vector<VideoWriter*>::iterator i = writers.begin(), end = writers.end(); i != end; ++i)421delete *i;422writers.clear();423424std::vector<VideoCapture*> readers(threadsCount);425CreateVideoCaptureInvoker invoker2(readers, files);426parallel_for_(range, invoker2);427428ReadImageAndTest::next = true;429430parallel_for_(range, ReadImageAndTest(readers, ts));431432// deleting tmp video files433for (std::vector<std::string>::const_iterator i = files.begin(), end = files.end(); i != end; ++i)434{435int code = remove(i->c_str());436if (code == 1)437std::cerr << "Couldn't delete " << *i << std::endl;438}439440// delete the readers441for (std::vector<VideoCapture *>::iterator i = readers.begin(), end = readers.end(); i != end; ++i)442delete *i;443}444445#endif446}} // namespace447448449