test_video_io.cpp 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "test_precomp.hpp"
44
#include "opencv2/videoio/videoio_c.h"
45 46
#include "opencv2/highgui.hpp"
#include <cstdio>
47 48 49

using namespace cv;
using namespace std;
50
using namespace std::tr1;
51

52
class Videoio_Test_Base
53
{
A
Andrey Kamaev 已提交
54
protected:
55 56
    string ext;
    string video_file;
A
Alexander Nesterov 已提交
57
    int apiPref;
58 59 60 61 62 63
protected:
    Videoio_Test_Base() {}
    virtual ~Videoio_Test_Base() {}
    virtual void checkFrameContent(Mat &, int) {}
    virtual void checkFrameCount(int &) {}
    void checkFrameRead(int idx, VideoCapture & cap)
A
Andrey Kamaev 已提交
64
    {
65
        //int frameID = (int)cap.get(CAP_PROP_POS_FRAMES);
66
        Mat img; cap >> img;
67 68
        //std::cout << "idx=" << idx << " img=" << img.size() << " frameID=" << frameID << std::endl;
        ASSERT_FALSE(img.empty()) << "idx=" << idx;
69
        checkFrameContent(img, idx);
A
Andrey Kamaev 已提交
70
    }
71
    void checkFrameSeek(int idx, VideoCapture & cap)
A
Andrey Kamaev 已提交
72
    {
73 74 75 76 77 78 79
        bool canSeek = cap.set(CAP_PROP_POS_FRAMES, idx);
        if (!canSeek)
        {
            std::cout << "Seek to frame '" << idx << "' is not supported. SKIP." << std::endl;
            return;
        }
        EXPECT_EQ(idx, (int)cap.get(CAP_PROP_POS_FRAMES));
80 81 82 83 84
        checkFrameRead(idx, cap);
    }
public:
    void doTest()
    {
A
Alexander Nesterov 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        if (apiPref == CAP_AVFOUNDATION)
        {
            // TODO: fix this backend
            std::cout << "SKIP test: AVFoundation backend returns invalid frame count" << std::endl;
            return;
        } else if (apiPref == CAP_VFW) {
            // TODO: fix this backend
            std::cout << "SKIP test: Video for Windows backend not open files" << std::endl;
            return;
        } else if (apiPref == CAP_GSTREAMER) {
            // TODO: fix this backend
            std::cout << "SKIP test: Gstreamer failed with read critical error" << std::endl;
            return;
        }

        VideoCapture cap(video_file, apiPref);
101 102 103 104 105
        if (!cap.isOpened())
        {
            std::cout << "SKIP test: Can't open video: " << video_file << std::endl;
            return;
        }
106
        int n_frames = (int)cap.get(CAP_PROP_FRAME_COUNT);
107 108 109 110 111 112 113 114 115 116
        if (n_frames > 0)
        {
            ASSERT_GT(n_frames, 0);
            checkFrameCount(n_frames);
        }
        else
        {
            std::cout << "CAP_PROP_FRAME_COUNT is not supported by backend. Assume 50 frames." << std::endl;
            n_frames = 50;
        }
A
Andrey Kamaev 已提交
117 118

        {
119 120 121 122 123
            SCOPED_TRACE("consecutive read");
            for (int k = 0; k < n_frames; ++k)
            {
                checkFrameRead(k, cap);
            }
A
Andrey Kamaev 已提交
124
        }
125 126
        bool canSeek = cap.set(CAP_PROP_POS_FRAMES, 0);
        if (!canSeek)
A
Andrey Kamaev 已提交
127
        {
128 129
            std::cout << "Seek to frame '0' is not supported. SKIP all 'seek' tests." << std::endl;
            return;
A
Andrey Kamaev 已提交
130 131
        }

132
        if (ext != "wmv")
A
Andrey Kamaev 已提交
133
        {
134 135
            SCOPED_TRACE("progressive seek");
            ASSERT_TRUE(cap.set(CAP_PROP_POS_FRAMES, 0));
136
            for (int k = 0; k < n_frames; k += 20)
137 138 139
            {
                checkFrameSeek(k, cap);
            }
A
Andrey Kamaev 已提交
140
        }
141 142 143 144 145 146 147 148 149 150

        if (ext != "mpg" && ext != "wmv")
        {
            SCOPED_TRACE("random seek");
            ASSERT_TRUE(cap.set(CAP_PROP_POS_FRAMES, 0));
            for (int k = 0; k < 10; ++k)
            {
                checkFrameSeek(cvtest::TS::ptr()->get_rng().uniform(0, n_frames), cap);
            }
        }
A
Andrey Kamaev 已提交
151
    }
152
};
A
Andrey Kamaev 已提交
153

154
//==================================================================================================
A
Alexander Nesterov 已提交
155
typedef tuple<string, int> Backend_Type_Params;
V
Vadim Pisarevsky 已提交
156

A
Alexander Nesterov 已提交
157
class Videoio_Bunny : public Videoio_Test_Base, public testing::TestWithParam<Backend_Type_Params>
158
{
159 160
public:
    Videoio_Bunny()
A
Andrey Kamaev 已提交
161
    {
A
Alexander Nesterov 已提交
162 163 164
        ext = get<0>(GetParam());
        apiPref = get<1>(GetParam());

165
        video_file = cvtest::TS::ptr()->get_data_path() + "video/big_buck_bunny." + ext;
A
Andrey Kamaev 已提交
166
    }
167
    void doFrameCountTest()
A
Andrey Kamaev 已提交
168
    {
A
Alexander Nesterov 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        if (apiPref == CAP_AVFOUNDATION)
        {
            // TODO: fix this backend
            std::cout << "SKIP test: AVFoundation backend returns invalid frame count" << std::endl;
            return;
        } else if (apiPref == CAP_VFW) {
            // TODO: fix this backend
            std::cout << "SKIP test: Video for Windows backend not open files" << std::endl;
            return;
        } else if (apiPref == CAP_GSTREAMER) {
            // TODO: fix this backend
            std::cout << "SKIP test: Gstreamer failed with read critical error" << std::endl;
            return;
        }

        VideoCapture cap(video_file, apiPref);
185 186 187 188 189
        if (!cap.isOpened())
        {
            std::cout << "SKIP test: Can't open video: " << video_file << std::endl;
            return;
        }
190 191 192 193 194 195 196 197 198 199

        const int width_gt = 672;
        const int height_gt = 384;
        const int fps_gt = 24;
        const double time_gt = 5.21;
        const int count_gt = cvRound(fps_gt * time_gt); // 5.21 sec * 24 fps

        EXPECT_EQ(width_gt, cap.get(CAP_PROP_FRAME_WIDTH));
        EXPECT_EQ(height_gt, cap.get(CAP_PROP_FRAME_HEIGHT));

200 201 202 203 204
        double fps_prop = cap.get(CAP_PROP_FPS);
        if (fps_prop > 0)
            EXPECT_NEAR(fps_prop, fps_gt, 1);
        else
            std::cout << "FPS is not available. SKIP check." << std::endl;
205

A
Alexander Nesterov 已提交
206 207
        int count_prop = 0;
        count_prop = (int)cap.get(CAP_PROP_FRAME_COUNT);
208 209 210
        // mpg file reports 5.08 sec * 24 fps => property returns 122 frames
        // but actual number of frames returned is 125
        if (ext != "mpg")
A
Andrey Kamaev 已提交
211
        {
212
            if (count_prop > 0)
213
            {
214
                EXPECT_EQ(count_gt, count_prop);
215
            }
A
Andrey Kamaev 已提交
216 217
        }

218 219 220 221 222 223 224 225 226 227 228
        int count_actual = 0;
        while (cap.isOpened())
        {
            Mat frame;
            cap >> frame;
            if (frame.empty())
                break;
            EXPECT_EQ(width_gt, frame.cols);
            EXPECT_EQ(height_gt, frame.rows);
            count_actual += 1;
        }
229
        if (count_prop > 0)
230
        {
231
            EXPECT_NEAR(count_gt, count_actual, 1);
232
        }
233 234
        else
            std::cout << "Frames counter is not available. Actual frames: " << count_actual << ". SKIP check." << std::endl;
A
Andrey Kamaev 已提交
235
    }
236
};
A
Andrey Kamaev 已提交
237

A
Alexander Nesterov 已提交
238
typedef tuple<string, string, float, int> Ext_Fourcc_PSNR;
239
typedef tuple<Size, Ext_Fourcc_PSNR> Size_Ext_Fourcc_PSNR;
A
Andrey Kamaev 已提交
240

241 242 243 244 245 246 247 248 249
class Videoio_Synthetic : public Videoio_Test_Base, public testing::TestWithParam<Size_Ext_Fourcc_PSNR>
{
    Size frame_size;
    int fourcc;
    float PSNR_GT;
    int frame_count;
    double fps;
public:
    Videoio_Synthetic()
A
Andrey Kamaev 已提交
250
    {
251 252 253 254 255 256 257 258
        frame_size = get<0>(GetParam());
        const Ext_Fourcc_PSNR &param = get<1>(GetParam());
        ext = get<0>(param);
        fourcc = fourccFromString(get<1>(param));
        PSNR_GT = get<2>(param);
        video_file = cv::tempfile((fourccToString(fourcc) + "." + ext).c_str());
        frame_count = 100;
        fps = 25.;
A
Alexander Nesterov 已提交
259
        apiPref = get<3>(param);
A
Andrey Kamaev 已提交
260
    }
261
    void SetUp()
A
Andrey Kamaev 已提交
262
    {
A
Alexander Nesterov 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

        if (apiPref == CAP_AVFOUNDATION)
        {
            // TODO: fix this backend
            std::cout << "SKIP test: AVFoundation backend can not write video" << std::endl;
            return;
        } else if (apiPref == CAP_VFW) {
            // TODO: fix this backend
            std::cout << "SKIP test: Video for Windows backend not open files" << std::endl;
            return;
        } else if (apiPref == CAP_GSTREAMER) {
            // TODO: fix this backend
            std::cout << "SKIP test: Gstreamer failed with write critical error" << std::endl;
            return;
        }
278
        Mat img(frame_size, CV_8UC3);
A
Alexander Nesterov 已提交
279
        VideoWriter writer(video_file, apiPref, fourcc, fps, frame_size, true);
280 281
        ASSERT_TRUE(writer.isOpened());
        for(int i = 0; i < frame_count; ++i )
A
Andrey Kamaev 已提交
282
        {
283 284
            generateFrame(i, frame_count, img);
            writer << img;
A
Andrey Kamaev 已提交
285
        }
286
        writer.release();
A
Andrey Kamaev 已提交
287
    }
288
    void TearDown()
A
Alexander Reshetnikov 已提交
289
    {
290
        remove(video_file.c_str());
A
Alexander Reshetnikov 已提交
291
    }
292
    virtual void checkFrameContent(Mat & img, int idx)
V
Vadim Pisarevsky 已提交
293
    {
294 295 296 297
        Mat imgGT(frame_size, CV_8UC3);
        generateFrame(idx, frame_count, imgGT);
        double psnr = cvtest::PSNR(img, imgGT);
        ASSERT_GT(psnr, PSNR_GT) << "frame " << idx;
V
Vadim Pisarevsky 已提交
298
    }
299
    virtual void checkFrameCount(int &actual)
V
Vadim Pisarevsky 已提交
300
    {
301 302 303 304 305 306 307 308 309 310 311 312
        Range expected_frame_count = Range(frame_count, frame_count);

        // Hack! Newer FFmpeg versions in this combination produce a file
        // whose reported duration is one frame longer than needed, and so
        // the calculated frame count is also off by one. Ideally, we'd want
        // to fix both writing (to produce the correct duration) and reading
        // (to correctly report frame count for such files), but I don't know
        // how to do either, so this is a workaround for now.
        if (fourcc == VideoWriter::fourcc('M', 'P', 'E', 'G') && ext == "mkv")
            expected_frame_count.end += 1;

        // Hack! Some GStreamer encoding pipelines drop last frame in the video
A
Alexander Nesterov 已提交
313 314 315
        // #ifdef HAVE_GSTREAMER
        //         expected_frame_count.start -= 1;
        // #endif
A
Andrey Kamaev 已提交
316

317 318
        ASSERT_LE(expected_frame_count.start, actual);
        ASSERT_GE(expected_frame_count.end, actual);
A
Alexander Reshetnikov 已提交
319

320
        actual = expected_frame_count.start; // adjust actual frame boundary to possible minimum
A
Alexander Reshetnikov 已提交
321
    }
322
};
A
Alexander Reshetnikov 已提交
323

324
//==================================================================================================
325

A
Alexander Nesterov 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
int backend_params[] = {
#ifdef HAVE_QUICKTIME
    CAP_QT,
#endif

#ifdef HAVE_AVFOUNDATION
    CAP_AVFOUNDATION,
#endif

#ifdef HAVE_MSMF
    CAP_MSMF,
#endif

#ifdef HAVE_VFW
    CAP_VFW,
#endif

#ifdef HAVE_GSTREAMER
    CAP_GSTREAMER,
#endif

#ifdef HAVE_FFMPEG
    CAP_FFMPEG,
#endif
    CAP_OPENCV_MJPEG
    // CAP_INTEL_MFX
};
353

354 355
string bunny_params[] = {
#ifdef HAVE_VIDEO_INPUT
A
Alexander Nesterov 已提交
356
    string("wmv"),
357 358 359
    string("mov"),
    string("mp4"),
    string("mpg"),
A
Alexander Nesterov 已提交
360
    string("avi"),
361
#endif
362 363
    string("mjpg.avi")
};
364

365
TEST_P(Videoio_Bunny, read_position) { doTest(); }
A
Alexander Smorkalov 已提交
366

367
TEST_P(Videoio_Bunny, frame_count) { doFrameCountTest(); }
A
Alexander Reshetnikov 已提交
368

369
INSTANTIATE_TEST_CASE_P(videoio, Videoio_Bunny,
A
Alexander Nesterov 已提交
370 371 372
                          testing::Combine(
                              testing::ValuesIn(bunny_params),
                              testing::ValuesIn(backend_params)));
A
Alexander Reshetnikov 已提交
373 374


375
//==================================================================================================
A
Alexander Reshetnikov 已提交
376

A
Alexander Nesterov 已提交
377
inline Ext_Fourcc_PSNR makeParam(const char * ext, const char * fourcc, float psnr, int apipref)
A
Alexander Reshetnikov 已提交
378
{
A
Alexander Nesterov 已提交
379
    return make_tuple(string(ext), string(fourcc), (float)psnr, (int)apipref);
A
Alexander Reshetnikov 已提交
380 381
}

382
Ext_Fourcc_PSNR synthetic_params[] = {
A
Alexander Reshetnikov 已提交
383

384
#ifdef HAVE_MSMF
A
Alexander Nesterov 已提交
385 386 387 388 389 390 391 392 393
#if !defined(_M_ARM)
    makeParam("wmv", "WMV1", 30.f, CAP_MSMF),
    makeParam("wmv", "WMV2", 30.f, CAP_MSMF),
#endif
    makeParam("wmv", "WMV3", 30.f, CAP_MSMF),
    makeParam("wmv", "WVC1", 30.f, CAP_MSMF),
    makeParam("avi", "H264", 30.f, CAP_MSMF),
    makeParam("avi", "MJPG", 30.f, CAP_MSMF),
#endif
394

A
Alexander Nesterov 已提交
395
#ifdef HAVE_VFW
396
#if !defined(_M_ARM)
A
Alexander Nesterov 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    makeParam("wmv", "WMV1", 30.f, CAP_VFW),
    makeParam("wmv", "WMV2", 30.f, CAP_VFW),
#endif
    makeParam("wmv", "WMV3", 30.f, CAP_VFW),
    makeParam("wmv", "WVC1", 30.f, CAP_VFW),
    makeParam("avi", "H264", 30.f, CAP_VFW),
    makeParam("avi", "MJPG", 30.f, CAP_VFW),
#endif

#ifdef HAVE_QUICKTIME
    makeParam("mov", "mp4v", 30.f, CAP_QT),
    makeParam("avi", "XVID", 30.f, CAP_QT),
    makeParam("avi", "MPEG", 30.f, CAP_QT),
    makeParam("avi", "IYUV", 30.f, CAP_QT),
    makeParam("avi", "MJPG", 30.f, CAP_QT),

    makeParam("mkv", "XVID", 30.f, CAP_QT),
    makeParam("mkv", "MPEG", 30.f, CAP_QT),
    makeParam("mkv", "MJPG", 30.f, CAP_QT),
#endif

#ifdef HAVE_AVFOUNDATION
    makeParam("mov", "mp4v", 30.f, CAP_AVFOUNDATION),
    makeParam("avi", "XVID", 30.f, CAP_AVFOUNDATION),
    makeParam("avi", "MPEG", 30.f, CAP_AVFOUNDATION),
    makeParam("avi", "IYUV", 30.f, CAP_AVFOUNDATION),
    makeParam("avi", "MJPG", 30.f, CAP_AVFOUNDATION),

    makeParam("mkv", "XVID", 30.f, CAP_AVFOUNDATION),
    makeParam("mkv", "MPEG", 30.f, CAP_AVFOUNDATION),
    makeParam("mkv", "MJPG", 30.f, CAP_AVFOUNDATION),
428
#endif
A
Alexander Nesterov 已提交
429 430 431 432 433 434 435 436 437 438

#ifdef HAVE_FFMPEG
    makeParam("avi", "XVID", 30.f, CAP_FFMPEG),
    makeParam("avi", "MPEG", 30.f, CAP_FFMPEG),
    makeParam("avi", "IYUV", 30.f, CAP_FFMPEG),
    makeParam("avi", "MJPG", 30.f, CAP_FFMPEG),

    makeParam("mkv", "XVID", 30.f, CAP_FFMPEG),
    makeParam("mkv", "MPEG", 30.f, CAP_FFMPEG),
    makeParam("mkv", "MJPG", 30.f, CAP_FFMPEG),
V
Vadim Pisarevsky 已提交
439 440
#endif

A
Alexander Nesterov 已提交
441 442 443 444 445 446
#ifdef HAVE_GSTREAMER
    makeParam("avi", "XVID", 30.f, CAP_GSTREAMER),
    makeParam("avi", "MPEG", 30.f, CAP_GSTREAMER),
    makeParam("avi", "IYUV", 30.f, CAP_GSTREAMER),
    makeParam("avi", "MJPG", 30.f, CAP_GSTREAMER),
    makeParam("avi", "H264", 30.f, CAP_GSTREAMER),
447

A
Alexander Nesterov 已提交
448 449 450
    makeParam("mkv", "XVID", 30.f, CAP_GSTREAMER),
    makeParam("mkv", "MPEG", 30.f, CAP_GSTREAMER),
    makeParam("mkv", "MJPG", 30.f, CAP_GSTREAMER),
451

A
Alexander Nesterov 已提交
452 453
#endif
    makeParam("avi", "MJPG", 30.f, CAP_OPENCV_MJPEG),
454 455
};

A
Alexander Nesterov 已提交
456

457 458 459 460 461 462 463 464 465 466 467
Size all_sizes[] = {
    Size(640, 480),
    Size(976, 768)
};

TEST_P(Videoio_Synthetic, write_read_position) { doTest(); }

INSTANTIATE_TEST_CASE_P(videoio, Videoio_Synthetic,
                        testing::Combine(
                            testing::ValuesIn(all_sizes),
                            testing::ValuesIn(synthetic_params)));