refactored highgui_specificvideo* tests; fixed repeated libswscale context creation in videocapture & videowriter (ticket #1541)

This commit is contained in:
Vadim Pisarevsky 2012-04-25 12:56:09 +00:00
parent 8d130ff10c
commit c0fcfcc7d8
3 changed files with 463 additions and 637 deletions

View File

@ -63,6 +63,10 @@ extern "C" {
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
#else #else
#ifndef HAVE_FFMPEG_SWSCALE
#error "libswscale is necessary to build the newer OpenCV ffmpeg wrapper"
#endif
// if the header path is not specified explicitly, let's deduce it // if the header path is not specified explicitly, let's deduce it
#if !defined HAVE_FFMPEG_AVCODEC_H && !defined HAVE_LIBAVCODEC_AVCODEC_H #if !defined HAVE_FFMPEG_AVCODEC_H && !defined HAVE_LIBAVCODEC_AVCODEC_H
@ -232,9 +236,7 @@ struct CvCapture_FFMPEG
AVPacket packet; AVPacket packet;
Image_FFMPEG frame; Image_FFMPEG frame;
#if defined(HAVE_FFMPEG_SWSCALE)
struct SwsContext *img_convert_ctx; struct SwsContext *img_convert_ctx;
#endif
int64_t frame_number; int64_t frame_number;
@ -260,9 +262,7 @@ void CvCapture_FFMPEG::init()
memset( &frame, 0, sizeof(frame) ); memset( &frame, 0, sizeof(frame) );
filename = 0; filename = 0;
packet.data = NULL; packet.data = NULL;
#if defined(HAVE_FFMPEG_SWSCALE)
img_convert_ctx = 0; img_convert_ctx = 0;
#endif
avcodec = 0; avcodec = 0;
frame_number = 0; frame_number = 0;
@ -272,6 +272,12 @@ void CvCapture_FFMPEG::init()
void CvCapture_FFMPEG::close() void CvCapture_FFMPEG::close()
{ {
if( img_convert_ctx )
{
sws_freeContext(img_convert_ctx);
img_convert_ctx = 0;
}
if( picture ) if( picture )
av_free(picture); av_free(picture);
@ -545,22 +551,21 @@ bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int*
if( !video_st || !picture->data[0] ) if( !video_st || !picture->data[0] )
return false; return false;
avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24, video_st->codec->width, video_st->codec->height); avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
video_st->codec->width, video_st->codec->height);
if( img_convert_ctx == NULL ||
frame.width != video_st->codec->width ||
frame.height != video_st->codec->height )
{
if( img_convert_ctx )
sws_freeContext(img_convert_ctx);
frame.width = video_st->codec->width; frame.width = video_st->codec->width;
frame.height = video_st->codec->height; frame.height = video_st->codec->height;
img_convert_ctx = sws_getContext(
video_st->codec->width, video_st->codec->height,
video_st->codec->pix_fmt,
video_st->codec->width, video_st->codec->height,
PIX_FMT_BGR24,
SWS_BICUBIC,
NULL, NULL, NULL
);
img_convert_ctx = sws_getCachedContext( img_convert_ctx = sws_getCachedContext(
img_convert_ctx, NULL,
video_st->codec->width, video_st->codec->height, video_st->codec->width, video_st->codec->height,
video_st->codec->pix_fmt, video_st->codec->pix_fmt,
video_st->codec->width, video_st->codec->height, video_st->codec->width, video_st->codec->height,
@ -571,6 +576,7 @@ bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int*
if (img_convert_ctx == NULL) if (img_convert_ctx == NULL)
return false;//CV_Error(0, "Cannot initialize the conversion context!"); return false;//CV_Error(0, "Cannot initialize the conversion context!");
}
sws_scale( sws_scale(
img_convert_ctx, img_convert_ctx,
@ -581,10 +587,6 @@ bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int*
rgb_picture.linesize rgb_picture.linesize
); );
sws_freeContext(img_convert_ctx);
frame_number++; frame_number++;
*data = frame.data; *data = frame.data;
@ -674,7 +676,8 @@ double CvCapture_FFMPEG::get_duration_sec()
if (sec < eps_zero) if (sec < eps_zero)
{ {
sec = static_cast<double>(static_cast<int64_t>(ic->streams[video_stream]->duration)) * r2d(ic->streams[video_stream]->time_base); sec = static_cast<double>(static_cast<int64_t>(ic->streams[video_stream]->duration)) *
r2d(ic->streams[video_stream]->time_base);
} }
return sec; return sec;
@ -721,7 +724,8 @@ int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
double CvCapture_FFMPEG::dts_to_sec(int64_t dts) double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
{ {
return static_cast<double>(dts - ic->streams[video_stream]->start_time) * r2d(ic->streams[video_stream]->time_base); return static_cast<double>(dts - ic->streams[video_stream]->start_time) *
r2d(ic->streams[video_stream]->time_base);
} }
void CvCapture_FFMPEG::seek(int64_t _frame_number) void CvCapture_FFMPEG::seek(int64_t _frame_number)
@ -828,9 +832,7 @@ struct CvVideoWriter_FFMPEG
Image_FFMPEG temp_image; Image_FFMPEG temp_image;
int frame_width, frame_height; int frame_width, frame_height;
bool ok; bool ok;
#if defined(HAVE_FFMPEG_SWSCALE)
struct SwsContext *img_convert_ctx; struct SwsContext *img_convert_ctx;
#endif
}; };
static const char * icvFFMPEGErrStr(int err) static const char * icvFFMPEGErrStr(int err)
@ -904,9 +906,7 @@ void CvVideoWriter_FFMPEG::init()
video_st = 0; video_st = 0;
input_pix_fmt = 0; input_pix_fmt = 0;
memset(&temp_image, 0, sizeof(temp_image)); memset(&temp_image, 0, sizeof(temp_image));
#if defined(HAVE_FFMPEG_SWSCALE)
img_convert_ctx = 0; img_convert_ctx = 0;
#endif
frame_width = frame_height = 0; frame_width = frame_height = 0;
ok = false; ok = false;
} }
@ -1190,14 +1190,8 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int
avpicture_fill((AVPicture *)input_picture, (uint8_t *) data, avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
(PixelFormat)input_pix_fmt, width, height); (PixelFormat)input_pix_fmt, width, height);
#if !defined(HAVE_FFMPEG_SWSCALE) if( !img_convert_ctx )
// convert to the color format needed by the codec {
if( img_convert((AVPicture *)picture, c->pix_fmt,
(AVPicture *)input_picture, (PixelFormat)input_pix_fmt,
width, height) < 0){
return false;
}
#else
img_convert_ctx = sws_getContext(width, img_convert_ctx = sws_getContext(width,
height, height,
(PixelFormat)input_pix_fmt, (PixelFormat)input_pix_fmt,
@ -1206,17 +1200,16 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int
c->pix_fmt, c->pix_fmt,
SWS_BICUBIC, SWS_BICUBIC,
NULL, NULL, NULL); NULL, NULL, NULL);
if( !img_convert_ctx )
return false;
}
if ( sws_scale(img_convert_ctx, input_picture->data, if ( sws_scale(img_convert_ctx, input_picture->data,
input_picture->linesize, 0, input_picture->linesize, 0,
height, height,
picture->data, picture->linesize) < 0 ) picture->data, picture->linesize) < 0 )
{
return false; return false;
} }
sws_freeContext(img_convert_ctx);
#endif
}
else{ else{
avpicture_fill((AVPicture *)picture, (uint8_t *) data, avpicture_fill((AVPicture *)picture, (uint8_t *) data,
(PixelFormat)input_pix_fmt, width, height); (PixelFormat)input_pix_fmt, width, height);
@ -1244,7 +1237,7 @@ void CvVideoWriter_FFMPEG::close()
/* write the trailer, if any */ /* write the trailer, if any */
if(ok && oc) if(ok && oc)
{ {
if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
{ {
for(;;) for(;;)
{ {
@ -1256,21 +1249,27 @@ void CvVideoWriter_FFMPEG::close()
av_write_trailer(oc); av_write_trailer(oc);
} }
if( img_convert_ctx )
{
sws_freeContext(img_convert_ctx);
img_convert_ctx = 0;
}
// free pictures // free pictures
#if LIBAVFORMAT_BUILD > 4628 #if LIBAVFORMAT_BUILD > 4628
if( video_st->codec->pix_fmt != input_pix_fmt){ if( video_st->codec->pix_fmt != input_pix_fmt)
#else #else
if( video_st->codec.pix_fmt != input_pix_fmt){ if( video_st->codec.pix_fmt != input_pix_fmt)
#endif #endif
{
if(picture->data[0]) if(picture->data[0])
free(picture->data[0]); free(picture->data[0]);
picture->data[0] = 0; picture->data[0] = 0;
} }
av_free(picture); av_free(picture);
if (input_picture) { if (input_picture)
av_free(input_picture); av_free(input_picture);
}
/* close codec */ /* close codec */
#if LIBAVFORMAT_BUILD > 4628 #if LIBAVFORMAT_BUILD > 4628
@ -1282,12 +1281,14 @@ void CvVideoWriter_FFMPEG::close()
av_free(outbuf); av_free(outbuf);
/* free the streams */ /* free the streams */
for(i = 0; i < oc->nb_streams; i++) { for(i = 0; i < oc->nb_streams; i++)
{
av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]); av_freep(&oc->streams[i]);
} }
if (!(fmt->flags & AVFMT_NOFILE)) { if (!(fmt->flags & AVFMT_NOFILE))
{
/* close the output file */ /* close the output file */
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0) #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)

View File

@ -57,5 +57,11 @@
# define BUILD_WITH_VIDEO_OUTPUT_SUPPORT 0 # define BUILD_WITH_VIDEO_OUTPUT_SUPPORT 0
#endif #endif
namespace cvtest
{
string fourccToString(int fourcc);
}
#endif #endif

View File

@ -46,76 +46,76 @@
using namespace cv; using namespace cv;
using namespace std; using namespace std;
string cvtest::fourccToString(int fourcc)
{
return format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
}
struct VideoFmt
{
VideoFmt() { fourcc = -1; }
VideoFmt(const string& _ext, int _fourcc) : ext(_ext), fourcc(_fourcc) {}
bool empty() const { return ext.empty(); }
string ext;
int fourcc;
};
static const VideoFmt specific_fmt_list[] =
{
VideoFmt("avi", CV_FOURCC('m', 'p', 'e', 'g')),
VideoFmt("avi", CV_FOURCC('M', 'J', 'P', 'G')),
VideoFmt("avi", CV_FOURCC('I', 'Y', 'U', 'V')),
VideoFmt("mkv", CV_FOURCC('X', 'V', 'I', 'D')),
VideoFmt("mov", CV_FOURCC('m', 'p', '4', 'v')),
VideoFmt()
};
class CV_HighGuiTest : public cvtest::BaseTest class CV_HighGuiTest : public cvtest::BaseTest
{ {
protected: protected:
void ImageTest(const string& dir); void ImageTest(const string& dir);
void VideoTest (const string& dir, int fourcc); void VideoTest (const string& dir, const VideoFmt& fmt);
void SpecificImageTest (const string& dir); void SpecificImageTest (const string& dir);
void SpecificVideoFileTest (const string& dir, const char codecchars[4]); void SpecificVideoTest (const string& dir, const VideoFmt& fmt);
void SpecificVideoCameraTest (const string& dir, const char codecchars[4]);
public: CV_HighGuiTest() {}
CV_HighGuiTest(); ~CV_HighGuiTest() {}
~CV_HighGuiTest();
virtual void run(int) = 0; virtual void run(int) = 0;
}; };
class CV_ImageTest : public CV_HighGuiTest class CV_ImageTest : public CV_HighGuiTest
{ {
public: public:
CV_ImageTest(); CV_ImageTest() {}
~CV_ImageTest(); ~CV_ImageTest() {}
void run(int); void run(int);
}; };
class CV_SpecificImageTest : public CV_HighGuiTest class CV_SpecificImageTest : public CV_HighGuiTest
{ {
public: public:
CV_SpecificImageTest(); CV_SpecificImageTest() {}
~CV_SpecificImageTest(); ~CV_SpecificImageTest() {}
void run(int); void run(int);
}; };
class CV_VideoTest : public CV_HighGuiTest class CV_VideoTest : public CV_HighGuiTest
{ {
public: public:
CV_VideoTest(); CV_VideoTest() {}
~CV_VideoTest(); ~CV_VideoTest() {}
void run(int); void run(int);
}; };
class CV_SpecificVideoFileTest : public CV_HighGuiTest class CV_SpecificVideoTest : public CV_HighGuiTest
{ {
public: public:
CV_SpecificVideoFileTest(); CV_SpecificVideoTest() {}
~CV_SpecificVideoFileTest(); ~CV_SpecificVideoTest() {}
void run(int); void run(int);
}; };
class CV_SpecificVideoCameraTest : public CV_HighGuiTest
{
public:
CV_SpecificVideoCameraTest();
~CV_SpecificVideoCameraTest();
void run(int);
};
CV_HighGuiTest::CV_HighGuiTest() {}
CV_HighGuiTest::~CV_HighGuiTest() {}
CV_ImageTest::CV_ImageTest() : CV_HighGuiTest() {}
CV_VideoTest::CV_VideoTest() : CV_HighGuiTest() {}
CV_SpecificImageTest::CV_SpecificImageTest() : CV_HighGuiTest() {}
CV_SpecificVideoFileTest::CV_SpecificVideoFileTest() : CV_HighGuiTest() {}
CV_SpecificVideoCameraTest::CV_SpecificVideoCameraTest() : CV_HighGuiTest() {}
CV_ImageTest::~CV_ImageTest() {}
CV_VideoTest::~CV_VideoTest() {}
CV_SpecificImageTest::~CV_SpecificImageTest() {}
CV_SpecificVideoFileTest::~CV_SpecificVideoFileTest() {}
CV_SpecificVideoCameraTest::~CV_SpecificVideoCameraTest() {}
double PSNR(const Mat& m1, const Mat& m2) double PSNR(const Mat& m1, const Mat& m2)
{ {
Mat tmp; Mat tmp;
@ -232,10 +232,11 @@ void CV_HighGuiTest::ImageTest(const string& dir)
ts->set_failed_test_info(ts->OK); ts->set_failed_test_info(ts->OK);
} }
void CV_HighGuiTest::VideoTest(const string& dir, int fourcc)
void CV_HighGuiTest::VideoTest(const string& dir, const VideoFmt& fmt)
{ {
string src_file = dir + "../cv/shared/video_for_test.avi"; string src_file = dir + "../cv/shared/video_for_test.avi";
string tmp_name = "video.avi"; string tmp_name = format("video.%s", fmt.ext.c_str());
ts->printf(ts->LOG, "reading video : %s\n", src_file.c_str()); ts->printf(ts->LOG, "reading video : %s\n", src_file.c_str());
@ -258,10 +259,11 @@ void CV_HighGuiTest::VideoTest(const string& dir, int fourcc)
if (writer == 0) if (writer == 0)
{ {
writer = cvCreateVideoWriter(tmp_name.c_str(), fourcc, 24, cvGetSize(img)); writer = cvCreateVideoWriter(tmp_name.c_str(), fmt.fourcc, 24, cvGetSize(img));
if (writer == 0) if (writer == 0)
{ {
ts->printf(ts->LOG, "can't create writer (with fourcc : %d)\n", fourcc); ts->printf(ts->LOG, "can't create writer (with fourcc : %d)\n",
cvtest::fourccToString(fmt.fourcc).c_str());
cvReleaseCapture( &cap ); cvReleaseCapture( &cap );
ts->set_failed_test_info(ts->FAIL_MISMATCH); ts->set_failed_test_info(ts->FAIL_MISMATCH);
return; return;
@ -325,7 +327,7 @@ void CV_HighGuiTest::SpecificImageTest(const string& dir)
return; return;
} }
cv::resize(image, image, cv::Size(968, 757), 0.0, 0.0, cv::INTER_CUBIC); resize(image, image, Size(968, 757), 0.0, 0.0, INTER_CUBIC);
stringstream s_digit; s_digit << i; stringstream s_digit; s_digit << i;
@ -394,81 +396,63 @@ void CV_HighGuiTest::SpecificImageTest(const string& dir)
ts->set_failed_test_info(ts->OK); ts->set_failed_test_info(ts->OK);
} }
void CV_HighGuiTest::SpecificVideoFileTest(const string& dir, const char codecchars[4])
void CV_HighGuiTest::SpecificVideoTest(const string& dir, const VideoFmt& fmt)
{ {
const string exts[] = {"avi", "mov", "mpg", "wmv"}; string ext = fmt.ext;
const size_t n = sizeof(exts)/sizeof(exts[0]); int fourcc = fmt.fourcc;
int fourcc0 = CV_FOURCC(codecchars[0], codecchars[1], codecchars[2], codecchars[3]);
for (size_t j = 0; j < n; ++j) string fourcc_str = cvtest::fourccToString(fourcc);
{
string ext = exts[j];
int fourcc = fourcc0;
if( (ext == "mov" && fourcc != CV_FOURCC('M', 'J', 'P', 'G')) ||
(ext == "mpg" && fourcc != CV_FOURCC('m', 'p', 'e', 'g')) ||
(ext == "wmv" && fourcc != CV_FOURCC('M', 'J', 'P', 'G')))
continue;
if( ext == "mov" )
fourcc = CV_FOURCC('m', 'p', '4', 'v');
string fourcc_str = format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
const string video_file = "video_" + fourcc_str + "." + ext; const string video_file = "video_" + fourcc_str + "." + ext;
Size frame_size(968 & -2, 757 & -2); Size frame_size(968 & -2, 757 & -2);
//Size frame_size(968 & -16, 757 & -16);
//Size frame_size(640, 480);
VideoWriter writer(video_file, fourcc, 25, frame_size, true); VideoWriter writer(video_file, fourcc, 25, frame_size, true);
if (!writer.isOpened()) if (!writer.isOpened())
{ {
// call it repeatedly for easier debugging
VideoWriter writer(video_file, fourcc, 25, frame_size, true); VideoWriter writer(video_file, fourcc, 25, frame_size, true);
ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str()); ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
ts->printf(ts->LOG, "Cannot create VideoWriter object with codec %s.\n", fourcc_str.c_str()); ts->printf(ts->LOG, "Cannot create VideoWriter object with codec %s.\n", fourcc_str.c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH); ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue; return;
} }
const size_t IMAGE_COUNT = 30; const size_t IMAGE_COUNT = 30;
vector<Mat> images;
for( size_t i = 0; i < IMAGE_COUNT; ++i ) for( size_t i = 0; i < IMAGE_COUNT; ++i )
{ {
stringstream s_digit; string file_path = format("%s../python/images/QCIF_%02d.bmp", dir.c_str(), i);
if (i < 10) {s_digit << "0"; s_digit << i;} Mat img = imread(file_path, CV_LOAD_IMAGE_COLOR);
else s_digit << i;
const string file_path = dir+"../python/images/QCIF_"+s_digit.str()+".bmp";
cv::Mat img = imread(file_path, CV_LOAD_IMAGE_COLOR);
if (img.empty()) if (img.empty())
{ {
ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str()); ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
ts->printf(ts->LOG, "Error: cannot read frame from %s.\n", (ts->get_data_path()+"../python/images/QCIF_"+s_digit.str()+".bmp").c_str()); ts->printf(ts->LOG, "Error: cannot read frame from %s.\n", file_path.c_str());
ts->printf(ts->LOG, "Continue creating the video file...\n"); ts->printf(ts->LOG, "Continue creating the video file...\n");
ts->set_failed_test_info(ts->FAIL_INVALID_TEST_DATA); ts->set_failed_test_info(ts->FAIL_INVALID_TEST_DATA);
break;//continue; break;
} }
cv::resize(img, img, frame_size, 0.0, 0.0, cv::INTER_CUBIC);
for (int k = 0; k < img.rows; ++k) for (int k = 0; k < img.rows; ++k)
for (int l = 0; l < img.cols; ++l) for (int l = 0; l < img.cols; ++l)
if (img.at<Vec3b>(k, l) == Vec3b::all(0)) if (img.at<Vec3b>(k, l) == Vec3b::all(0))
img.at<Vec3b>(k, l) = Vec3b(0, 255, 0); img.at<Vec3b>(k, l) = Vec3b(0, 255, 0);
else img.at<Vec3b>(k, l) = Vec3b(0, 0, 255); else img.at<Vec3b>(k, l) = Vec3b(0, 0, 255);
imwrite("QCIF_"+s_digit.str()+".bmp", img); resize(img, img, frame_size, 0.0, 0.0, INTER_CUBIC);
images.push_back(img);
writer << img; writer << img;
} }
writer.release(); writer.release();
cv::VideoCapture cap(video_file); VideoCapture cap(video_file);
size_t FRAME_COUNT = (size_t)cap.get(CV_CAP_PROP_FRAME_COUNT); size_t FRAME_COUNT = (size_t)cap.get(CV_CAP_PROP_FRAME_COUNT);
if (FRAME_COUNT != IMAGE_COUNT && ext != "mpg" ) if (FRAME_COUNT != IMAGE_COUNT )
{ {
ts->printf(ts->LOG, "\nFrame count checking for video_%s.%s...\n", fourcc_str.c_str(), ext.c_str()); ts->printf(ts->LOG, "\nFrame count checking for video_%s.%s...\n", fourcc_str.c_str(), ext.c_str());
ts->printf(ts->LOG, "Video codec: %s\n", fourcc_str.c_str()); ts->printf(ts->LOG, "Video codec: %s\n", fourcc_str.c_str());
@ -476,13 +460,12 @@ void CV_HighGuiTest::SpecificVideoFileTest(const string& dir, const char codecch
ts->printf(ts->LOG, "Error: Incorrect frame count in the video.\n"); ts->printf(ts->LOG, "Error: Incorrect frame count in the video.\n");
ts->printf(ts->LOG, "Continue checking...\n"); ts->printf(ts->LOG, "Continue checking...\n");
ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY); ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
return;
} }
//cap.set(CV_CAP_PROP_POS_FRAMES, -1); for (int i = 0; i < FRAME_COUNT; i++)
for (int i = 0; i < (int)std::min<size_t>(FRAME_COUNT, IMAGE_COUNT)-1; i++)
{ {
cv::Mat frame; cap >> frame; Mat frame; cap >> frame;
if (frame.empty()) if (frame.empty())
{ {
ts->printf(ts->LOG, "\nVideo file directory: %s\n", "."); ts->printf(ts->LOG, "\nVideo file directory: %s\n", ".");
@ -493,21 +476,9 @@ void CV_HighGuiTest::SpecificVideoFileTest(const string& dir, const char codecch
break; break;
} }
stringstream s_digit; Mat img = images[i];
if (i+1 < 10) {s_digit << "0"; s_digit << i+1;}
else s_digit << i+1;
cv::Mat img = imread("QCIF_"+s_digit.str()+".bmp", CV_LOAD_IMAGE_COLOR);
if (img.empty())
{
ts->printf(ts->LOG, "\nError: cannot read an image from %s.\n", ("QCIF_"+s_digit.str()+".bmp").c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
const double thresDbell = 40; const double thresDbell = 40;
double psnr = PSNR(img, frame); double psnr = PSNR(img, frame);
if (psnr > thresDbell) if (psnr > thresDbell)
@ -521,123 +492,6 @@ void CV_HighGuiTest::SpecificVideoFileTest(const string& dir, const char codecch
} }
} }
} }
}
void CV_HighGuiTest::SpecificVideoCameraTest(const string& dir, const char codecchars[4])
{
const string ext[] = {"avi", "mov", "mp4", "mpg", "wmv"};
const size_t n = sizeof(ext)/sizeof(ext[0]);
const int IMAGE_COUNT = 125;
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
ts->printf(ts->LOG, "\nError: cannot start working with device.\n");
ts->set_failed_test_info(ts->OK);
return;
}
for (size_t i = 0; i < n; ++i)
if ((ext[i]!="mp4")||(string(&codecchars[0], 4)!="IYUV"))
#if defined WIN32 || defined _WIN32
if (((ext[i]!="mov")||(string(&codecchars[0], 4)=="XVID"))&&(ext[i]!="mp4"))
#endif
{
Mat frame; int framecount = 0;
cv::VideoWriter writer;
std::vector <cv::Mat> tmp_img(IMAGE_COUNT);
writer.open("video_"+string(&codecchars[0], 4)+"."+ext[i], CV_FOURCC(codecchars[0], codecchars[1], codecchars[2], codecchars[3]), 25, Size(968, 757), true);
if (!writer.isOpened())
{
ts->printf(ts->LOG, "\nVideo file directory: %s\n", ".");
ts->printf(ts->LOG, "Video codec: %s\n", std::string(&codecchars[0], 4).c_str());
ts->printf(ts->LOG, "Error: cannot create VideoWriter object for video_%s.%s.\n", string(&codecchars[0]).c_str(), ext[i].c_str());
ts->set_failed_test_info(ts->FAIL_EXCEPTION);
continue;
}
for (;;)
{
cap >> frame;
if (frame.empty())
{
ts->printf(ts->LOG, "\nVideo file directory: %s\n", ".");
ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
ts->printf(ts->LOG, "Error: cannot read next frame with index %d from the device.\n", framecount);
break;
}
cv::resize(frame, frame, Size(968, 757), 0, 0, INTER_CUBIC);
writer << frame; tmp_img[framecount] = frame;
framecount++;
if (framecount == IMAGE_COUNT) break;
}
cv::VideoCapture vcap(dir+"video_"+string(&codecchars[0], 4)+"."+ext[i]);
if (!vcap.isOpened())
{
ts->printf(ts->LOG, "\nVideo file directory: %s\n", ".");
ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
ts->printf(ts->LOG, "Error: cannot open video file.\n");
continue;
}
int FRAME_COUNT = (int)vcap.get(CV_CAP_PROP_FRAME_COUNT);
if (FRAME_COUNT != IMAGE_COUNT)
{
ts->printf(ts->LOG, "\nChecking frame count...\n");
ts->printf(ts->LOG, "Video file directory: %s\n", ".");
ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
ts->printf(ts->LOG, "Required frame count: %d Returned frame count: %d\n", IMAGE_COUNT, FRAME_COUNT);
ts->printf(ts->LOG, "Error: required and returned frame count are not matched.\n");
ts->printf(ts->LOG, "Continue checking...\n");
ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
}
cv::Mat img; framecount = 0;
vcap.set(CV_CAP_PROP_POS_FRAMES, 0);
for ( ; framecount < std::min<int>(FRAME_COUNT, IMAGE_COUNT); framecount++ )
{
vcap >> img;
if (img.empty())
{
ts->printf(ts->LOG, "\nVideo file directory: %s\n", ".");
ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
ts->printf(ts->LOG, "Error: cannot read frame with index %d from the video.\n", framecount);
break;
}
const double thresDbell = 20;
double psnr = PSNR(img, tmp_img[framecount]);
if (psnr > thresDbell)
{
ts->printf(ts->LOG, "\nReading frame from the file video_%s.%s...\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
ts->printf(ts->LOG, "Frame index: %d\n", framecount);
ts->printf(ts->LOG, "Difference between saved and original images: %g\n", psnr);
ts->printf(ts->LOG, "Maximum allowed difference: %g\n", thresDbell);
ts->printf(ts->LOG, "Error: too big difference between saved and original images.\n");
continue;
}
}
}
}
void CV_ImageTest::run(int) void CV_ImageTest::run(int)
{ {
@ -651,48 +505,17 @@ void CV_SpecificImageTest::run(int)
void CV_VideoTest::run(int) void CV_VideoTest::run(int)
{ {
const char codecs[][4] = { {'I', 'Y', 'U', 'V'}, for (int i = 0; !specific_fmt_list[i].empty(); ++i)
{'X', 'V', 'I', 'D'},
{'m', 'p', 'e', 'g'},
{'M', 'J', 'P', 'G'} };
printf("%s", ts->get_data_path().c_str());
int count = sizeof(codecs)/(4*sizeof(char));
for (int i = 0; i < count; ++i)
{ {
VideoTest(ts->get_data_path(), CV_FOURCC(codecs[i][0], codecs[i][1], codecs[i][2], codecs[i][3])); VideoTest(ts->get_data_path(), specific_fmt_list[i]);
} }
} }
void CV_SpecificVideoFileTest::run(int) void CV_SpecificVideoTest::run(int)
{ {
const char codecs[][4] = { {'m', 'p', 'e', 'g'}, for (int i = 0; !specific_fmt_list[i].empty(); ++i)
{'X', 'V', 'I', 'D'},
{'M', 'J', 'P', 'G'},
{'I', 'Y', 'U', 'V'} };
int count = sizeof(codecs)/(4*sizeof(char));
for (int i = 0; i < count; ++i)
{ {
SpecificVideoFileTest(ts->get_data_path(), codecs[i]); SpecificVideoTest(ts->get_data_path(), specific_fmt_list[i]);
}
}
void CV_SpecificVideoCameraTest::run(int)
{
const char codecs[][4] = { {'m', 'p', 'e', 'g'},
{'X', 'V', 'I', 'D'},
{'M', 'J', 'P', 'G'},
{'I', 'Y', 'U', 'V'} };
int count = sizeof(codecs)/(4*sizeof(char));
for (int i = 0; i < count; ++i)
{
SpecificVideoCameraTest(ts->get_data_path(), codecs[i]);
} }
} }
@ -702,11 +525,7 @@ TEST(Highgui_Image, regression) { CV_ImageTest test; test.safe_run(); }
#if BUILD_WITH_VIDEO_INPUT_SUPPORT && BUILD_WITH_VIDEO_OUTPUT_SUPPORT #if BUILD_WITH_VIDEO_INPUT_SUPPORT && BUILD_WITH_VIDEO_OUTPUT_SUPPORT
TEST(Highgui_Video, regression) { CV_VideoTest test; test.safe_run(); } TEST(Highgui_Video, regression) { CV_VideoTest test; test.safe_run(); }
TEST(Highgui_SpecificVideoFile, regression) { CV_SpecificVideoFileTest test; test.safe_run(); } TEST(Highgui_SpecificVideoFile, regression) { CV_SpecificVideoTest test; test.safe_run(); }
#endif
#if BUILD_WITH_VIDEO_INPUT_SUPPORT && BUILD_WITH_VIDEO_OUTPUT_SUPPORT && BUILD_WITH_CAMERA_SUPPORT
TEST(Highgui_SpecificVideoCamera, regression) { CV_SpecificVideoCameraTest test; test.safe_run(); }
#endif #endif
TEST(Highgui_SpecificImage, regression) { CV_SpecificImageTest test; test.safe_run(); } TEST(Highgui_SpecificImage, regression) { CV_SpecificImageTest test; test.safe_run(); }