cap_ffmpeg_impl.hpp 67.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*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.
//
//
V
Vadim Pisarevsky 已提交
10
//                          License Agreement
11 12
//                For Open Source Computer Vision Library
//
V
Vadim Pisarevsky 已提交
13 14
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 16 17 18 19 20 21 22 23 24 25 26
// 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.
//
V
Vadim Pisarevsky 已提交
27
//   * The name of the copyright holders may not be used to endorse or promote products
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
//     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*/

43 44 45 46
#include "cap_ffmpeg_legacy_api.hpp"

using namespace cv;

47
#if !(defined(_WIN32) || defined(WINCE))
48 49
# include <pthread.h>
#endif
50
#include <assert.h>
51
#include <algorithm>
V
Vadim Pisarevsky 已提交
52
#include <limits>
53

54 55 56 57
#ifndef __OPENCV_BUILD
#define CV_FOURCC(c1, c2, c3, c4) (((c1) & 255) + (((c2) & 255) << 8) + (((c3) & 255) << 16) + (((c4) & 255) << 24))
#endif

58 59
#define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )

60
#if defined _MSC_VER && _MSC_VER >= 1200
T
Tomoaki Teshima 已提交
61
#pragma warning( disable: 4244 4510 4610 )
62 63
#endif

A
Andrey Kamaev 已提交
64 65 66 67
#ifdef __GNUC__
#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif

A
Alexander Alekhin 已提交
68 69 70 71
#ifndef CV_UNUSED  // Required for standalone compilation mode (OpenCV defines this in base.hpp)
#define CV_UNUSED(name) (void)name
#endif

72 73 74 75
#ifdef __cplusplus
extern "C" {
#endif

76 77
#include "ffmpeg_codecs.hpp"

V
Vadim Pisarevsky 已提交
78 79
#include <libavutil/mathematics.h>

80 81 82 83
#if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
  #include <libavutil/opt.h>
#endif

P
Peter Rekdal Sunde 已提交
84 85 86 87 88
#if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(51, 63, 100) : CALC_FFMPEG_VERSION(54, 6, 0))
#include <libavutil/imgutils.h>
#endif

89 90
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
91 92 93 94 95 96

#ifdef __cplusplus
}
#endif

#if defined _MSC_VER && _MSC_VER >= 1200
T
Tomoaki Teshima 已提交
97
#pragma warning( default: 4244 4510 4610 )
98 99 100 101 102 103 104 105
#endif

#ifdef NDEBUG
#define CV_WARN(message)
#else
#define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
#endif

106
#if defined _WIN32
107
    #include <windows.h>
108 109 110 111 112 113 114
    #if defined _MSC_VER && _MSC_VER < 1900
    struct timespec
    {
        time_t tv_sec;
        long   tv_nsec;
    };
  #endif
115
#elif defined __linux__ || defined __APPLE__ || defined __HAIKU__
116 117
    #include <unistd.h>
    #include <stdio.h>
V
Vadim Pisarevsky 已提交
118
    #include <sys/types.h>
119
    #include <sys/time.h>
120
#if defined __APPLE__
121
    #include <sys/sysctl.h>
122 123
    #include <mach/clock.h>
    #include <mach/mach.h>
124
#endif
125
#endif
126

V
Vadim Pisarevsky 已提交
127 128 129 130 131 132 133 134 135 136
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

#if defined(__APPLE__)
#define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
#else
#define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
#endif

137 138 139 140
#ifndef AVERROR_EOF
#define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
#endif

141 142 143 144 145 146 147 148
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
#  define CV_CODEC_ID AVCodecID
#  define CV_CODEC(name) AV_##name
#else
#  define CV_CODEC_ID CodecID
#  define CV_CODEC(name) name
#endif

J
jisli 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
#if LIBAVUTIL_BUILD < (LIBAVUTIL_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(51, 74, 100) : CALC_FFMPEG_VERSION(51, 42, 0))
#define AVPixelFormat PixelFormat
#define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
#define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
#define AV_PIX_FMT_GRAY8 PIX_FMT_GRAY8
#define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
#define AV_PIX_FMT_YUV444P PIX_FMT_YUV444P
#define AV_PIX_FMT_YUVJ420P PIX_FMT_YUVJ420P
#define AV_PIX_FMT_GRAY16LE PIX_FMT_GRAY16LE
#define AV_PIX_FMT_GRAY16BE PIX_FMT_GRAY16BE
#endif

163 164 165 166
#ifndef PKT_FLAG_KEY
#define PKT_FLAG_KEY AV_PKT_FLAG_KEY
#endif

167 168 169 170 171 172 173 174 175 176
#if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(52, 38, 100) : CALC_FFMPEG_VERSION(52, 13, 0))
#define USE_AV_FRAME_GET_BUFFER 1
#else
#define USE_AV_FRAME_GET_BUFFER 0
#ifndef AV_NUM_DATA_POINTERS // required for 0.7.x/0.8.x ffmpeg releases
#define AV_NUM_DATA_POINTERS 4
#endif
#endif

177

178 179 180 181 182 183 184 185 186
#ifndef USE_AV_INTERRUPT_CALLBACK
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 21, 0)
#define USE_AV_INTERRUPT_CALLBACK 1
#else
#define USE_AV_INTERRUPT_CALLBACK 0
#endif
#endif

#if USE_AV_INTERRUPT_CALLBACK
187 188
#define LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS 30000
#define LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS 30000
189

190
#ifdef _WIN32
191 192
// http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows

193
static
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
inline LARGE_INTEGER get_filetime_offset()
{
    SYSTEMTIME s;
    FILETIME f;
    LARGE_INTEGER t;

    s.wYear = 1970;
    s.wMonth = 1;
    s.wDay = 1;
    s.wHour = 0;
    s.wMinute = 0;
    s.wSecond = 0;
    s.wMilliseconds = 0;
    SystemTimeToFileTime(&s, &f);
    t.QuadPart = f.dwHighDateTime;
    t.QuadPart <<= 32;
    t.QuadPart |= f.dwLowDateTime;
    return t;
}

214
static
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
inline void get_monotonic_time(timespec *tv)
{
    LARGE_INTEGER           t;
    FILETIME				f;
    double                  microseconds;
    static LARGE_INTEGER    offset;
    static double           frequencyToMicroseconds;
    static int              initialized = 0;
    static BOOL             usePerformanceCounter = 0;

    if (!initialized)
    {
        LARGE_INTEGER performanceFrequency;
        initialized = 1;
        usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
        if (usePerformanceCounter)
        {
            QueryPerformanceCounter(&offset);
            frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
        }
        else
        {
            offset = get_filetime_offset();
            frequencyToMicroseconds = 10.;
        }
    }

    if (usePerformanceCounter)
    {
        QueryPerformanceCounter(&t);
    } else {
        GetSystemTimeAsFileTime(&f);
        t.QuadPart = f.dwHighDateTime;
        t.QuadPart <<= 32;
        t.QuadPart |= f.dwLowDateTime;
    }

    t.QuadPart -= offset.QuadPart;
    microseconds = (double)t.QuadPart / frequencyToMicroseconds;
    t.QuadPart = microseconds;
    tv->tv_sec = t.QuadPart / 1000000;
    tv->tv_nsec = (t.QuadPart % 1000000) * 1000;
}
#else
259
static
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
inline void get_monotonic_time(timespec *time)
{
#if defined(__APPLE__) && defined(__MACH__)
    clock_serv_t cclock;
    mach_timespec_t mts;
    host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
    clock_get_time(cclock, &mts);
    mach_port_deallocate(mach_task_self(), cclock);
    time->tv_sec = mts.tv_sec;
    time->tv_nsec = mts.tv_nsec;
#else
    clock_gettime(CLOCK_MONOTONIC, time);
#endif
}
#endif

276
static
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
inline timespec get_monotonic_time_diff(timespec start, timespec end)
{
    timespec temp;
    if (end.tv_nsec - start.tv_nsec < 0)
    {
        temp.tv_sec = end.tv_sec - start.tv_sec - 1;
        temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
    }
    else
    {
        temp.tv_sec = end.tv_sec - start.tv_sec;
        temp.tv_nsec = end.tv_nsec - start.tv_nsec;
    }
    return temp;
}

293
static
294 295 296 297 298 299 300
inline double get_monotonic_time_diff_ms(timespec time1, timespec time2)
{
    timespec delta = get_monotonic_time_diff(time1, time2);
    double milliseconds = delta.tv_sec * 1000 + (double)delta.tv_nsec / 1000000.0;

    return milliseconds;
}
301
#endif // USE_AV_INTERRUPT_CALLBACK
302

303
static int get_number_of_cpus(void)
304
{
V
Vadim Pisarevsky 已提交
305 306
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
    return 1;
307
#elif defined _WIN32
308 309
    SYSTEM_INFO sysinfo;
    GetSystemInfo( &sysinfo );
V
Vadim Pisarevsky 已提交
310

311
    return (int)sysinfo.dwNumberOfProcessors;
312
#elif defined __linux__ || defined __HAIKU__
313 314 315 316
    return (int)sysconf( _SC_NPROCESSORS_ONLN );
#elif defined __APPLE__
    int numCPU=0;
    int mib[4];
V
Vadim Pisarevsky 已提交
317 318 319
    size_t len = sizeof(numCPU);

    // set the mib for hw.ncpu
320 321
    mib[0] = CTL_HW;
    mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
V
Vadim Pisarevsky 已提交
322 323

    // get the number of CPUs from the system
324
    sysctl(mib, 2, &numCPU, &len, NULL, 0);
V
Vadim Pisarevsky 已提交
325 326

    if( numCPU < 1 )
327 328 329
    {
        mib[1] = HW_NCPU;
        sysctl( mib, 2, &numCPU, &len, NULL, 0 );
V
Vadim Pisarevsky 已提交
330

331 332 333 334 335 336 337 338 339 340 341
        if( numCPU < 1 )
            numCPU = 1;
    }

    return (int)numCPU;
#else
    return 1;
#endif
}


342 343 344 345 346 347 348 349 350 351
struct Image_FFMPEG
{
    unsigned char* data;
    int step;
    int width;
    int height;
    int cn;
};


352
#if USE_AV_INTERRUPT_CALLBACK
353 354 355 356 357 358 359
struct AVInterruptCallbackMetadata
{
    timespec value;
    unsigned int timeout_after_ms;
    int timeout;
};

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
// https://github.com/opencv/opencv/pull/12693#issuecomment-426236731
static
inline const char* _opencv_avcodec_get_name(AVCodecID id)
{
#if LIBAVCODEC_VERSION_MICRO >= 100 \
    && LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(53, 47, 100)
    return avcodec_get_name(id);
#else
    const AVCodecDescriptor *cd;
    AVCodec *codec;

    if (id == AV_CODEC_ID_NONE)
    {
        return "none";
    }
    cd = avcodec_descriptor_get(id);
    if (cd)
    {
        return cd->name;
    }
    codec = avcodec_find_decoder(id);
    if (codec)
    {
        return codec->name;
    }
    codec = avcodec_find_encoder(id);
    if (codec)
    {
        return codec->name;
    }

    return "unknown_codec";
#endif
}

395
static
396 397 398 399 400 401
inline void _opencv_ffmpeg_free(void** ptr)
{
    if(*ptr) free(*ptr);
    *ptr = 0;
}

402
static
403 404 405 406 407
inline int _opencv_ffmpeg_interrupt_callback(void *ptr)
{
    AVInterruptCallbackMetadata* metadata = (AVInterruptCallbackMetadata*)ptr;
    assert(metadata);

408 409 410 411 412
    if (metadata->timeout_after_ms == 0)
    {
        return 0; // timeout is disabled
    }

413 414 415 416 417 418 419
    timespec now;
    get_monotonic_time(&now);

    metadata->timeout = get_monotonic_time_diff_ms(metadata->value, now) > metadata->timeout_after_ms;

    return metadata->timeout ? -1 : 0;
}
420
#endif
421

P
Peter Rekdal Sunde 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
static
inline void _opencv_ffmpeg_av_packet_unref(AVPacket *pkt)
{
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(55, 25, 100) : CALC_FFMPEG_VERSION(55, 16, 0))
    av_packet_unref(pkt);
#else
    av_free_packet(pkt);
#endif
};

static
inline void _opencv_ffmpeg_av_image_fill_arrays(void *frame, uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
{
#if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(51, 63, 100) : CALC_FFMPEG_VERSION(54, 6, 0))
    av_image_fill_arrays(((AVFrame*)frame)->data, ((AVFrame*)frame)->linesize, ptr, pix_fmt, width, height, 1);
#else
    avpicture_fill((AVPicture*)frame, ptr, pix_fmt, width, height);
#endif
};

static
inline int _opencv_ffmpeg_av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height)
{
#if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(51, 63, 100) : CALC_FFMPEG_VERSION(54, 6, 0))
    return av_image_get_buffer_size(pix_fmt, width, height, 1);
#else
    return avpicture_get_size(pix_fmt, width, height);
#endif
};

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static AVRational _opencv_ffmpeg_get_sample_aspect_ratio(AVStream *stream)
{
#if LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_BUILD >= CALC_FFMPEG_VERSION(54, 5, 100)
    return av_guess_sample_aspect_ratio(NULL, stream, NULL);
#else
    AVRational undef = {0, 1};

    // stream
    AVRational ratio = stream ? stream->sample_aspect_ratio : undef;
    av_reduce(&ratio.num, &ratio.den, ratio.num, ratio.den, INT_MAX);
    if (ratio.num > 0 && ratio.den > 0)
        return ratio;

    // codec
    ratio  = stream && stream->codec ? stream->codec->sample_aspect_ratio : undef;
    av_reduce(&ratio.num, &ratio.den, ratio.num, ratio.den, INT_MAX);
    if (ratio.num > 0 && ratio.den > 0)
        return ratio;

    return undef;
#endif
}

478

479 480 481 482 483
struct CvCapture_FFMPEG
{
    bool open( const char* filename );
    void close();

484
    double getProperty(int) const;
485 486 487 488 489
    bool setProperty(int, double);
    bool grabFrame();
    bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);

    void init();
V
Vadim Pisarevsky 已提交
490 491 492

    void    seek(int64_t frame_number);
    void    seek(double sec);
493
    bool    slowSeek( int framenumber );
V
Vadim Pisarevsky 已提交
494

495 496 497 498
    int64_t get_total_frames() const;
    double  get_duration_sec() const;
    double  get_fps() const;
    int     get_bitrate() const;
V
Vadim Pisarevsky 已提交
499

500
    double  r2d(AVRational r) const;
V
Vadim Pisarevsky 已提交
501
    int64_t dts_to_frame_number(int64_t dts);
502
    double  dts_to_sec(int64_t dts) const;
V
Vadim Pisarevsky 已提交
503 504 505 506 507

    AVFormatContext * ic;
    AVCodec         * avcodec;
    int               video_stream;
    AVStream        * video_st;
508 509
    AVFrame         * picture;
    AVFrame           rgb_picture;
V
Vadim Pisarevsky 已提交
510 511 512 513
    int64_t           picture_pts;

    AVPacket          packet;
    Image_FFMPEG      frame;
514
    struct SwsContext *img_convert_ctx;
V
Vadim Pisarevsky 已提交
515 516 517 518

    int64_t frame_number, first_frame_number;

    double eps_zero;
519 520 521 522 523 524 525 526
/*
   'filename' contains the filename of the videosource,
   'filename==NULL' indicates that ffmpeg's seek support works
   for the particular file.
   'filename!=NULL' indicates that the slow fallback function is used for seeking,
   and so the filename is needed to reopen the file on backward seeking.
*/
    char              * filename;
I
Ilya Lavrenov 已提交
527 528 529 530

#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    AVDictionary *dict;
#endif
531
#if USE_AV_INTERRUPT_CALLBACK
532
    AVInterruptCallbackMetadata interrupt_metadata;
533
#endif
534 535 536 537 538 539 540 541
};

void CvCapture_FFMPEG::init()
{
    ic = 0;
    video_stream = -1;
    video_st = 0;
    picture = 0;
V
Vadim Pisarevsky 已提交
542 543
    picture_pts = AV_NOPTS_VALUE_;
    first_frame_number = -1;
544 545 546
    memset( &rgb_picture, 0, sizeof(rgb_picture) );
    memset( &frame, 0, sizeof(frame) );
    filename = 0;
V
Vadim Pisarevsky 已提交
547 548
    memset(&packet, 0, sizeof(packet));
    av_init_packet(&packet);
549
    img_convert_ctx = 0;
V
Vadim Pisarevsky 已提交
550 551 552 553

    avcodec = 0;
    frame_number = 0;
    eps_zero = 0.000025;
I
Ilya Lavrenov 已提交
554 555 556 557

#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    dict = NULL;
#endif
558 559 560 561 562
}


void CvCapture_FFMPEG::close()
{
V
Vadim Pisarevsky 已提交
563 564 565 566 567
    if( img_convert_ctx )
    {
        sws_freeContext(img_convert_ctx);
        img_convert_ctx = 0;
    }
568

569
    if( picture )
570 571
    {
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
J
jisli 已提交
572 573 574
    ? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
        av_frame_free(&picture);
#elif LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
575 576 577
    ? CALC_FFMPEG_VERSION(54, 59, 100) : CALC_FFMPEG_VERSION(54, 28, 0))
        avcodec_free_frame(&picture);
#else
578
        av_free(picture);
579 580
#endif
    }
581 582 583 584 585

    if( video_st )
    {
#if LIBAVFORMAT_BUILD > 4628
        avcodec_close( video_st->codec );
V
Vadim Pisarevsky 已提交
586

587
#else
V
Vadim Pisarevsky 已提交
588 589
        avcodec_close( &(video_st->codec) );

590 591 592 593 594 595
#endif
        video_st = NULL;
    }

    if( ic )
    {
V
Vadim Pisarevsky 已提交
596
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
597
        av_close_input_file(ic);
V
Vadim Pisarevsky 已提交
598
#else
599
        avformat_close_input(&ic);
V
Vadim Pisarevsky 已提交
600 601
#endif

602 603 604
        ic = NULL;
    }

605 606 607
#if USE_AV_FRAME_GET_BUFFER
    av_frame_unref(&rgb_picture);
#else
608 609 610 611 612
    if( rgb_picture.data[0] )
    {
        free( rgb_picture.data[0] );
        rgb_picture.data[0] = 0;
    }
613
#endif
614 615 616

    // free last packet if exist
    if (packet.data) {
P
Peter Rekdal Sunde 已提交
617
        _opencv_ffmpeg_av_packet_unref (&packet);
V
Vadim Pisarevsky 已提交
618
        packet.data = NULL;
619 620
    }

I
Ilya Lavrenov 已提交
621 622 623 624 625
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    if (dict != NULL)
       av_dict_free(&dict);
#endif

626 627 628 629 630
    init();
}


#ifndef AVSEEK_FLAG_FRAME
631
#define AVSEEK_FLAG_FRAME 0
632
#endif
A
Andrey Morozov 已提交
633
#ifndef AVSEEK_FLAG_ANY
634
#define AVSEEK_FLAG_ANY 1
635
#endif
V
Vadim Pisarevsky 已提交
636

637 638 639
#if defined(__OPENCV_BUILD) || defined(BUILD_PLUGIN)
typedef cv::Mutex ImplMutex;
#else
I
Ilya Lavrenov 已提交
640
class ImplMutex
V
Vadim Pisarevsky 已提交
641
{
I
Ilya Lavrenov 已提交
642
public:
A
Andrey Kamaev 已提交
643 644 645
    ImplMutex() { init(); }
    ~ImplMutex() { destroy(); }

I
Ilya Lavrenov 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    void init();
    void destroy();

    void lock();
    bool trylock();
    void unlock();

    struct Impl;
protected:
    Impl* impl;

private:
    ImplMutex(const ImplMutex&);
    ImplMutex& operator = (const ImplMutex& m);
};

662
#if defined _WIN32 || defined WINCE
I
Ilya Lavrenov 已提交
663 664 665

struct ImplMutex::Impl
{
666 667 668 669 670 671 672 673 674
    void init()
    {
#if (_WIN32_WINNT >= 0x0600)
        ::InitializeCriticalSectionEx(&cs, 1000, 0);
#else
        ::InitializeCriticalSection(&cs);
#endif
        refcount = 1;
    }
I
Ilya Lavrenov 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    void destroy() { DeleteCriticalSection(&cs); }

    void lock() { EnterCriticalSection(&cs); }
    bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
    void unlock() { LeaveCriticalSection(&cs); }

    CRITICAL_SECTION cs;
    int refcount;
};

#ifndef __GNUC__
static int _interlockedExchangeAdd(int* addr, int delta)
{
#if defined _MSC_VER && _MSC_VER >= 1500
    return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
#else
    return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
#endif
}
#endif // __GNUC__

#elif defined __APPLE__

#include <libkern/OSAtomic.h>

struct ImplMutex::Impl
{
    void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
    void destroy() { }

    void lock() { OSSpinLockLock(&sl); }
    bool trylock() { return OSSpinLockTry(&sl); }
    void unlock() { OSSpinLockUnlock(&sl); }

    OSSpinLock sl;
    int refcount;
};

713
#elif defined __linux__ && !defined __ANDROID__
I
Ilya Lavrenov 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

struct ImplMutex::Impl
{
    void init() { pthread_spin_init(&sl, 0); refcount = 1; }
    void destroy() { pthread_spin_destroy(&sl); }

    void lock() { pthread_spin_lock(&sl); }
    bool trylock() { return pthread_spin_trylock(&sl) == 0; }
    void unlock() { pthread_spin_unlock(&sl); }

    pthread_spinlock_t sl;
    int refcount;
};

#else

struct ImplMutex::Impl
{
    void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
    void destroy() { pthread_mutex_destroy(&sl); }

    void lock() { pthread_mutex_lock(&sl); }
    bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
    void unlock() { pthread_mutex_unlock(&sl); }

    pthread_mutex_t sl;
    int refcount;
};

#endif

void ImplMutex::init()
{
747
    impl = new Impl();
A
Andrey Kamaev 已提交
748
    impl->init();
I
Ilya Lavrenov 已提交
749
}
A
Andrey Kamaev 已提交
750
void ImplMutex::destroy()
I
Ilya Lavrenov 已提交
751
{
A
Andrey Kamaev 已提交
752
    impl->destroy();
753
    delete(impl);
A
Andrey Kamaev 已提交
754
    impl = NULL;
I
Ilya Lavrenov 已提交
755 756 757 758 759
}
void ImplMutex::lock() { impl->lock(); }
void ImplMutex::unlock() { impl->unlock(); }
bool ImplMutex::trylock() { return impl->trylock(); }

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
class AutoLock
{
public:
    AutoLock(ImplMutex& m) : mutex(&m) { mutex->lock(); }
    ~AutoLock() { mutex->unlock(); }
protected:
    ImplMutex* mutex;
private:
    AutoLock(const AutoLock&); // disabled
    AutoLock& operator = (const AutoLock&); // disabled
};
#endif

static ImplMutex _mutex;

I
Ilya Lavrenov 已提交
775 776 777 778
static int LockCallBack(void **mutex, AVLockOp op)
{
    ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
    switch (op)
779
    {
I
Ilya Lavrenov 已提交
780
        case AV_LOCK_CREATE:
781
            localMutex = new ImplMutex();
C
cyy 已提交
782 783
            if (!localMutex)
                return 1;
I
Ilya Lavrenov 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797
            *mutex = localMutex;
            if (!*mutex)
                return 1;
        break;

        case AV_LOCK_OBTAIN:
            localMutex->lock();
        break;

        case AV_LOCK_RELEASE:
            localMutex->unlock();
        break;

        case AV_LOCK_DESTROY:
798
            delete localMutex;
I
Ilya Lavrenov 已提交
799
            localMutex = NULL;
800
            *mutex = NULL;
I
Ilya Lavrenov 已提交
801 802 803 804 805
        break;
    }
    return 0;
}

806

807 808 809 810
static void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list vargs)
{
    static bool skip_header = false;
    static int prev_level = -1;
H
Hamdi Sahloul 已提交
811
    CV_UNUSED(ptr);
812 813 814 815 816 817
    if (!skip_header || level != prev_level) printf("[OPENCV:FFMPEG:%02d] ", level);
    vprintf(fmt, vargs);
    size_t fmt_len = strlen(fmt);
    skip_header = fmt_len > 0 && fmt[fmt_len - 1] != '\n';
    prev_level = level;
}
818

I
Ilya Lavrenov 已提交
819 820 821
class InternalFFMpegRegister
{
public:
822
    static void init()
823
    {
824
        AutoLock lock(_mutex);
825 826 827 828 829 830 831
        static InternalFFMpegRegister instance;
    }
    InternalFFMpegRegister()
    {
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
        avformat_network_init();
#endif
V
Vadim Pisarevsky 已提交
832

833 834
        /* register all codecs, demux and protocols */
        av_register_all();
V
Vadim Pisarevsky 已提交
835

836 837
        /* register a callback function for synchronization */
        av_lockmgr_register(&LockCallBack);
838

839
#ifndef NO_GETENV
840 841 842 843 844 845 846
        char* debug_option = getenv("OPENCV_FFMPEG_DEBUG");
        if (debug_option != NULL)
        {
            av_log_set_level(AV_LOG_VERBOSE);
            av_log_set_callback(ffmpeg_log_callback);
        }
        else
847
#endif
848 849
        {
            av_log_set_level(AV_LOG_ERROR);
I
Ilya Lavrenov 已提交
850
        }
V
Vadim Pisarevsky 已提交
851
    }
I
Ilya Lavrenov 已提交
852 853 854
    ~InternalFFMpegRegister()
    {
        av_lockmgr_register(NULL);
V
Vadim Pisarevsky 已提交
855
    }
I
Ilya Lavrenov 已提交
856 857
};

858 859
bool CvCapture_FFMPEG::open( const char* _filename )
{
860
    InternalFFMpegRegister::init();
861
    AutoLock lock(_mutex);
862 863 864 865
    unsigned i;
    bool valid = false;

    close();
866

867
#if USE_AV_INTERRUPT_CALLBACK
868
    /* interrupt callback */
869
    interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS;
870 871 872 873 874
    get_monotonic_time(&interrupt_metadata.value);

    ic = avformat_alloc_context();
    ic->interrupt_callback.callback = _opencv_ffmpeg_interrupt_callback;
    ic->interrupt_callback.opaque = &interrupt_metadata;
875
#endif
876

V
Vadim Pisarevsky 已提交
877
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
#ifndef NO_GETENV
    char* options = getenv("OPENCV_FFMPEG_CAPTURE_OPTIONS");
    if(options == NULL)
    {
        av_dict_set(&dict, "rtsp_transport", "tcp", 0);
    }
    else
    {
#if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 ? CALC_FFMPEG_VERSION(52, 17, 100) : CALC_FFMPEG_VERSION(52, 7, 0))
        av_dict_parse_string(&dict, options, ";", "|", 0);
#else
        av_dict_set(&dict, "rtsp_transport", "tcp", 0);
#endif
    }
#else
I
Ilya Lavrenov 已提交
893
    av_dict_set(&dict, "rtsp_transport", "tcp", 0);
894
#endif
895 896 897 898 899 900 901 902
    AVInputFormat* input_format = NULL;
    AVDictionaryEntry* entry = av_dict_get(dict, "input_format", NULL, 0);
    if (entry != 0)
    {
      input_format = av_find_input_format(entry->value);
    }

    int err = avformat_open_input(&ic, _filename, input_format, &dict);
V
Vadim Pisarevsky 已提交
903
#else
904
    int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
905 906
#endif

I
Ilya Lavrenov 已提交
907 908
    if (err < 0)
    {
V
Vadim Pisarevsky 已提交
909
        CV_WARN("Error opening file");
910
        CV_WARN(_filename);
V
Vadim Pisarevsky 已提交
911
        goto exit_func;
912
    }
V
Vadim Pisarevsky 已提交
913
    err =
R
Roman Donchenko 已提交
914
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
V
Vadim Pisarevsky 已提交
915 916 917 918
    avformat_find_stream_info(ic, NULL);
#else
    av_find_stream_info(ic);
#endif
I
Ilya Lavrenov 已提交
919 920
    if (err < 0)
    {
V
Vadim Pisarevsky 已提交
921 922
        CV_WARN("Could not find codec parameters");
        goto exit_func;
923
    }
V
Vadim Pisarevsky 已提交
924 925
    for(i = 0; i < ic->nb_streams; i++)
    {
926 927 928 929 930 931
#if LIBAVFORMAT_BUILD > 4628
        AVCodecContext *enc = ic->streams[i]->codec;
#else
        AVCodecContext *enc = &ic->streams[i]->codec;
#endif

I
Ilya Lavrenov 已提交
932 933 934
//#ifdef FF_API_THREAD_INIT
//        avcodec_thread_init(enc, get_number_of_cpus());
//#else
V
Vadim Pisarevsky 已提交
935
        enc->thread_count = get_number_of_cpus();
I
Ilya Lavrenov 已提交
936
//#endif
937

938 939 940
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
#endif
V
Vadim Pisarevsky 已提交
941

I
Ilya Lavrenov 已提交
942 943
        if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
        {
G
gferry 已提交
944 945 946 947
            // backup encoder' width/height
            int enc_width = enc->width;
            int enc_height = enc->height;

948 949 950 951 952 953
            AVCodec *codec;
            if(av_dict_get(dict, "video_codec", NULL, 0) == NULL) {
                codec = avcodec_find_decoder(enc->codec_id);
            } else {
                codec = avcodec_find_decoder_by_name(av_dict_get(dict, "video_codec", NULL, 0)->value);
            }
954
            if (!codec ||
V
Vadim Pisarevsky 已提交
955 956 957 958 959
#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
                avcodec_open2(enc, codec, NULL)
#else
                avcodec_open(enc, codec)
#endif
I
Ilya Lavrenov 已提交
960 961
                < 0)
                goto exit_func;
V
Vadim Pisarevsky 已提交
962

G
gferry 已提交
963 964 965 966
            // checking width/height (since decoder can sometimes alter it, eg. vp6f)
            if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
            if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }

967 968
            video_stream = i;
            video_st = ic->streams[i];
J
jisli 已提交
969 970 971 972
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
            picture = av_frame_alloc();
#else
973
            picture = avcodec_alloc_frame();
J
jisli 已提交
974
#endif
975 976 977 978

            frame.width = enc->width;
            frame.height = enc->height;
            frame.cn = 3;
979 980
            frame.step = 0;
            frame.data = NULL;
981 982 983 984 985 986
            break;
        }
    }

    if(video_stream >= 0) valid = true;

V
Vadim Pisarevsky 已提交
987
exit_func:
988

989 990 991 992 993
#if USE_AV_INTERRUPT_CALLBACK
    // deactivate interrupt callback
    interrupt_metadata.timeout_after_ms = 0;
#endif

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    if( !valid )
        close();

    return valid;
}


bool CvCapture_FFMPEG::grabFrame()
{
    bool valid = false;
    int got_picture;

V
Vadim Pisarevsky 已提交
1006
    int count_errs = 0;
J
jormansa 已提交
1007
    const int max_number_of_attempts = 1 << 9;
1008

V
Vadim Pisarevsky 已提交
1009
    if( !ic || !video_st )  return false;
1010

1011 1012 1013
    if( ic->streams[video_stream]->nb_frames > 0 &&
        frame_number > ic->streams[video_stream]->nb_frames )
        return false;
1014

V
Vadim Pisarevsky 已提交
1015
    picture_pts = AV_NOPTS_VALUE_;
1016

1017 1018 1019 1020 1021 1022
#if USE_AV_INTERRUPT_CALLBACK
    // activate interrupt callback
    get_monotonic_time(&interrupt_metadata.value);
    interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS;
#endif

1023
    // get the next frame
V
Vadim Pisarevsky 已提交
1024 1025
    while (!valid)
    {
H
hahne 已提交
1026

P
Peter Rekdal Sunde 已提交
1027
        _opencv_ffmpeg_av_packet_unref (&packet);
1028

1029
#if USE_AV_INTERRUPT_CALLBACK
1030 1031 1032 1033 1034
        if (interrupt_metadata.timeout)
        {
            valid = false;
            break;
        }
1035
#endif
1036

1037
        int ret = av_read_frame(ic, &packet);
V
Vadim Pisarevsky 已提交
1038 1039 1040 1041 1042 1043
        if (ret == AVERROR(EAGAIN)) continue;

        /* else if (ret < 0) break; */

        if( packet.stream_index != video_stream )
        {
P
Peter Rekdal Sunde 已提交
1044
            _opencv_ffmpeg_av_packet_unref (&packet);
V
Vadim Pisarevsky 已提交
1045 1046 1047
            count_errs++;
            if (count_errs > max_number_of_attempts)
                break;
1048 1049
            continue;
        }
1050

V
Vadim Pisarevsky 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
        // Decode video frame
        #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
            avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
        #elif LIBAVFORMAT_BUILD > 4628
                avcodec_decode_video(video_st->codec,
                                     picture, &got_picture,
                                     packet.data, packet.size);
        #else
                avcodec_decode_video(&video_st->codec,
                                     picture, &got_picture,
                                     packet.data, packet.size);
        #endif
1063

V
Vadim Pisarevsky 已提交
1064 1065 1066 1067 1068
        // Did we get a video frame?
        if(got_picture)
        {
            //picture_pts = picture->best_effort_timestamp;
            if( picture_pts == AV_NOPTS_VALUE_ )
1069 1070
                picture_pts = picture->pkt_pts != AV_NOPTS_VALUE_ && picture->pkt_pts != 0 ? picture->pkt_pts : picture->pkt_dts;

V
Vadim Pisarevsky 已提交
1071 1072 1073 1074 1075 1076 1077 1078
            frame_number++;
            valid = true;
        }
        else
        {
            count_errs++;
            if (count_errs > max_number_of_attempts)
                break;
1079 1080 1081
        }
    }

V
Vadim Pisarevsky 已提交
1082 1083
    if( valid && first_frame_number < 0 )
        first_frame_number = dts_to_frame_number(picture_pts);
1084

1085 1086 1087 1088 1089
#if USE_AV_INTERRUPT_CALLBACK
    // deactivate interrupt callback
    interrupt_metadata.timeout_after_ms = 0;
#endif

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    // return if we have a new picture or not
    return valid;
}


bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
{
    if( !video_st || !picture->data[0] )
        return false;

V
Vadim Pisarevsky 已提交
1100 1101
    if( img_convert_ctx == NULL ||
        frame.width != video_st->codec->width ||
1102 1103
        frame.height != video_st->codec->height ||
        frame.data == NULL )
V
Vadim Pisarevsky 已提交
1104
    {
1105 1106 1107
        // Some sws_scale optimizations have some assumptions about alignment of data/step/width/height
        // Also we use coded_width/height to workaround problem with legacy ffmpeg versions (like n0.8)
        int buffer_width = video_st->codec->coded_width, buffer_height = video_st->codec->coded_height;
V
Vadim Pisarevsky 已提交
1108 1109

        img_convert_ctx = sws_getCachedContext(
1110 1111
                img_convert_ctx,
                buffer_width, buffer_height,
V
Vadim Pisarevsky 已提交
1112
                video_st->codec->pix_fmt,
1113
                buffer_width, buffer_height,
J
jisli 已提交
1114
                AV_PIX_FMT_BGR24,
V
Vadim Pisarevsky 已提交
1115 1116 1117 1118 1119 1120
                SWS_BICUBIC,
                NULL, NULL, NULL
                );

        if (img_convert_ctx == NULL)
            return false;//CV_Error(0, "Cannot initialize the conversion context!");
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
#if USE_AV_FRAME_GET_BUFFER
        av_frame_unref(&rgb_picture);
        rgb_picture.format = AV_PIX_FMT_BGR24;
        rgb_picture.width = buffer_width;
        rgb_picture.height = buffer_height;
        if (0 != av_frame_get_buffer(&rgb_picture, 32))
        {
            CV_WARN("OutOfMemory");
            return false;
        }
#else
        int aligns[AV_NUM_DATA_POINTERS];
        avcodec_align_dimensions2(video_st->codec, &buffer_width, &buffer_height, aligns);
1135
        rgb_picture.data[0] = (uint8_t*)realloc(rgb_picture.data[0],
P
Peter Rekdal Sunde 已提交
1136
                _opencv_ffmpeg_av_image_get_buffer_size( AV_PIX_FMT_BGR24,
1137
                                    buffer_width, buffer_height ));
P
Peter Rekdal Sunde 已提交
1138
        _opencv_ffmpeg_av_image_fill_arrays(&rgb_picture, rgb_picture.data[0],
1139 1140 1141 1142 1143
                        AV_PIX_FMT_BGR24, buffer_width, buffer_height );
#endif
        frame.width = video_st->codec->width;
        frame.height = video_st->codec->height;
        frame.cn = 3;
1144
        frame.data = rgb_picture.data[0];
1145
        frame.step = rgb_picture.linesize[0];
V
Vadim Pisarevsky 已提交
1146 1147 1148 1149 1150 1151
    }

    sws_scale(
            img_convert_ctx,
            picture->data,
            picture->linesize,
1152
            0, video_st->codec->coded_height,
V
Vadim Pisarevsky 已提交
1153 1154 1155 1156
            rgb_picture.data,
            rgb_picture.linesize
            );

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    *data = frame.data;
    *step = frame.step;
    *width = frame.width;
    *height = frame.height;
    *cn = frame.cn;

    return true;
}


1167
double CvCapture_FFMPEG::getProperty( int property_id ) const
1168 1169 1170
{
    if( !video_st ) return 0;

1171 1172 1173 1174
    double codec_tag = 0;
    AVCodecID codec_id = AV_CODEC_ID_NONE;
    const char* codec_fourcc = NULL;

1175 1176
    switch( property_id )
    {
1177
    case CAP_PROP_POS_MSEC:
1178 1179 1180 1181 1182
        if (picture_pts == AV_NOPTS_VALUE_)
        {
            return 0;
        }
        return (dts_to_sec(picture_pts) * 1000);
1183
    case CAP_PROP_POS_FRAMES:
V
Vadim Pisarevsky 已提交
1184
        return (double)frame_number;
1185
    case CAP_PROP_POS_AVI_RATIO:
V
Vadim Pisarevsky 已提交
1186
        return r2d(ic->streams[video_stream]->time_base);
1187
    case CAP_PROP_FRAME_COUNT:
V
Vadim Pisarevsky 已提交
1188
        return (double)get_total_frames();
1189
    case CAP_PROP_FRAME_WIDTH:
1190
        return (double)frame.width;
1191
    case CAP_PROP_FRAME_HEIGHT:
1192
        return (double)frame.height;
1193
    case CAP_PROP_FPS:
1194
        return get_fps();
1195
    case CAP_PROP_FOURCC:
1196
#if LIBAVFORMAT_BUILD > 4628
1197 1198
        codec_id = video_st->codec->codec_id;
        codec_tag = (double) video_st->codec->codec_tag;
1199
#else
1200 1201
        codec_id = video_st->codec.codec_id;
        codec_tag = (double)video_st->codec.codec_tag;
1202
#endif
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214

        if(codec_tag || codec_id == AV_CODEC_ID_NONE)
        {
            return codec_tag;
        }

        codec_fourcc = _opencv_avcodec_get_name(codec_id);
        if(!codec_fourcc || strlen(codec_fourcc) < 4 || strcmp(codec_fourcc, "unknown_codec") == 0)
        {
            return codec_tag;
        }

1215
        return (double) CV_FOURCC(codec_fourcc[0], codec_fourcc[1], codec_fourcc[2], codec_fourcc[3]);
1216
    case CAP_PROP_SAR_NUM:
1217
        return _opencv_ffmpeg_get_sample_aspect_ratio(ic->streams[video_stream]).num;
1218
    case CAP_PROP_SAR_DEN:
1219
        return _opencv_ffmpeg_get_sample_aspect_ratio(ic->streams[video_stream]).den;
V
Vadim Pisarevsky 已提交
1220
    default:
1221
        break;
1222
    }
V
Vadim Pisarevsky 已提交
1223

1224 1225 1226
    return 0;
}

1227
double CvCapture_FFMPEG::r2d(AVRational r) const
V
Vadim Pisarevsky 已提交
1228 1229 1230 1231
{
    return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}

1232
double CvCapture_FFMPEG::get_duration_sec() const
1233
{
V
Vadim Pisarevsky 已提交
1234 1235 1236
    double sec = (double)ic->duration / (double)AV_TIME_BASE;

    if (sec < eps_zero)
1237
    {
V
Vadim Pisarevsky 已提交
1238
        sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
1239
    }
V
Vadim Pisarevsky 已提交
1240 1241

    return sec;
1242 1243
}

1244
int CvCapture_FFMPEG::get_bitrate() const
1245
{
V
Vadim Pisarevsky 已提交
1246 1247 1248
    return ic->bit_rate;
}

1249
double CvCapture_FFMPEG::get_fps() const
V
Vadim Pisarevsky 已提交
1250
{
A
Alexander Alekhin 已提交
1251 1252 1253 1254 1255 1256
#if 0 && LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 1, 100) && LIBAVFORMAT_VERSION_MICRO >= 100
    double fps = r2d(av_guess_frame_rate(ic, ic->streams[video_stream], NULL));
#else
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
    double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
#else
V
Vadim Pisarevsky 已提交
1257
    double fps = r2d(ic->streams[video_stream]->r_frame_rate);
A
Alexander Alekhin 已提交
1258
#endif
V
Vadim Pisarevsky 已提交
1259 1260 1261 1262 1263

#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    if (fps < eps_zero)
    {
        fps = r2d(ic->streams[video_stream]->avg_frame_rate);
1264
    }
1265
#endif
V
Vadim Pisarevsky 已提交
1266 1267 1268 1269 1270

    if (fps < eps_zero)
    {
        fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
    }
A
Alexander Alekhin 已提交
1271
#endif
V
Vadim Pisarevsky 已提交
1272 1273 1274
    return fps;
}

1275
int64_t CvCapture_FFMPEG::get_total_frames() const
V
Vadim Pisarevsky 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
{
    int64_t nbf = ic->streams[video_stream]->nb_frames;

    if (nbf == 0)
    {
        nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
    }
    return nbf;
}

int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
{
    double sec = dts_to_sec(dts);
    return (int64_t)(get_fps() * sec + 0.5);
}

1292
double CvCapture_FFMPEG::dts_to_sec(int64_t dts) const
V
Vadim Pisarevsky 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301
{
    return (double)(dts - ic->streams[video_stream]->start_time) *
        r2d(ic->streams[video_stream]->time_base);
}

void CvCapture_FFMPEG::seek(int64_t _frame_number)
{
    _frame_number = std::min(_frame_number, get_total_frames());
    int delta = 16;
1302

V
Vadim Pisarevsky 已提交
1303 1304
    // if we have not grabbed a single frame before first seek, let's read the first frame
    // and get some valuable information during the process
1305
    if( first_frame_number < 0 && get_total_frames() > 1 )
1306
        grabFrame();
1307

V
Vadim Pisarevsky 已提交
1308 1309 1310 1311 1312 1313 1314
    for(;;)
    {
        int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
        double sec = (double)_frame_number_temp / get_fps();
        int64_t time_stamp = ic->streams[video_stream]->start_time;
        double  time_base  = r2d(ic->streams[video_stream]->time_base);
        time_stamp += (int64_t)(sec / time_base + 0.5);
1315
        if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
V
Vadim Pisarevsky 已提交
1316 1317 1318 1319
        avcodec_flush_buffers(ic->streams[video_stream]->codec);
        if( _frame_number > 0 )
        {
            grabFrame();
1320

V
Vadim Pisarevsky 已提交
1321 1322 1323 1324 1325
            if( _frame_number > 1 )
            {
                frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
                //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
                //       (int)_frame_number, (int)frame_number, delta);
1326

V
Vadim Pisarevsky 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
                if( frame_number < 0 || frame_number > _frame_number-1 )
                {
                    if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
                        break;
                    delta = delta < 16 ? delta*2 : delta*3/2;
                    continue;
                }
                while( frame_number < _frame_number-1 )
                {
                    if(!grabFrame())
                        break;
                }
                frame_number++;
                break;
            }
            else
            {
                frame_number = 1;
                break;
            }
        }
        else
        {
            frame_number = 0;
            break;
        }
1353
    }
V
Vadim Pisarevsky 已提交
1354 1355 1356 1357 1358
}

void CvCapture_FFMPEG::seek(double sec)
{
    seek((int64_t)(sec * get_fps() + 0.5));
1359
}
1360 1361 1362 1363 1364 1365 1366

bool CvCapture_FFMPEG::setProperty( int property_id, double value )
{
    if( !video_st ) return false;

    switch( property_id )
    {
1367 1368 1369
    case CAP_PROP_POS_MSEC:
    case CAP_PROP_POS_FRAMES:
    case CAP_PROP_POS_AVI_RATIO:
1370 1371 1372
        {
            switch( property_id )
            {
1373
            case CAP_PROP_POS_FRAMES:
V
Vadim Pisarevsky 已提交
1374
                seek((int64_t)value);
1375 1376
                break;

1377
            case CAP_PROP_POS_MSEC:
V
Vadim Pisarevsky 已提交
1378
                seek(value/1000.0);
1379 1380
                break;

1381
            case CAP_PROP_POS_AVI_RATIO:
V
Vadim Pisarevsky 已提交
1382
                seek((int64_t)(value*ic->duration));
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
                break;
            }

            picture_pts=(int64_t)value;
        }
        break;
    default:
        return false;
    }

    return true;
}


///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
struct CvVideoWriter_FFMPEG
{
    bool open( const char* filename, int fourcc,
1401
               double fps, int width, int height, bool isColor );
1402 1403 1404 1405 1406
    void close();
    bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );

    void init();

1407
    AVOutputFormat  * fmt;
V
Vadim Pisarevsky 已提交
1408
    AVFormatContext * oc;
1409 1410 1411 1412 1413 1414 1415 1416
    uint8_t         * outbuf;
    uint32_t          outbuf_size;
    FILE            * outfile;
    AVFrame         * picture;
    AVFrame         * input_picture;
    uint8_t         * picbuf;
    AVStream        * video_st;
    int               input_pix_fmt;
1417
    unsigned char   * aligned_input;
1418
    size_t            aligned_input_size;
V
Vadim Pisarevsky 已提交
1419
    int               frame_width, frame_height;
1420
    int               frame_idx;
V
Vadim Pisarevsky 已提交
1421
    bool              ok;
1422 1423 1424 1425 1426
    struct SwsContext *img_convert_ctx;
};

static const char * icvFFMPEGErrStr(int err)
{
1427
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1428
    switch(err) {
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    case AVERROR_BSF_NOT_FOUND:
        return "Bitstream filter not found";
    case AVERROR_DECODER_NOT_FOUND:
        return "Decoder not found";
    case AVERROR_DEMUXER_NOT_FOUND:
        return "Demuxer not found";
    case AVERROR_ENCODER_NOT_FOUND:
        return "Encoder not found";
    case AVERROR_EOF:
        return "End of file";
    case AVERROR_EXIT:
        return "Immediate exit was requested; the called function should not be restarted";
    case AVERROR_FILTER_NOT_FOUND:
        return "Filter not found";
    case AVERROR_INVALIDDATA:
        return "Invalid data found when processing input";
    case AVERROR_MUXER_NOT_FOUND:
        return "Muxer not found";
    case AVERROR_OPTION_NOT_FOUND:
        return "Option not found";
    case AVERROR_PATCHWELCOME:
        return "Not yet implemented in FFmpeg, patches welcome";
    case AVERROR_PROTOCOL_NOT_FOUND:
        return "Protocol not found";
    case AVERROR_STREAM_NOT_FOUND:
        return "Stream not found";
    default:
        break;
V
Vadim Pisarevsky 已提交
1457
    }
1458
#else
1459 1460
    switch(err) {
    case AVERROR_NUMEXPECTED:
V
Vadim Pisarevsky 已提交
1461
        return "Incorrect filename syntax";
1462
    case AVERROR_INVALIDDATA:
V
Vadim Pisarevsky 已提交
1463
        return "Invalid data in header";
1464
    case AVERROR_NOFMT:
V
Vadim Pisarevsky 已提交
1465
        return "Unknown format";
1466
    case AVERROR_IO:
V
Vadim Pisarevsky 已提交
1467
        return "I/O error occurred";
1468
    case AVERROR_NOMEM:
V
Vadim Pisarevsky 已提交
1469
        return "Memory allocation error";
1470
    default:
V
Vadim Pisarevsky 已提交
1471
        break;
1472
    }
1473 1474
#endif

V
Vadim Pisarevsky 已提交
1475
    return "Unspecified error";
1476 1477 1478 1479
}

/* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
extern "C" {
1480
    enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
}

void CvVideoWriter_FFMPEG::init()
{
    fmt = 0;
    oc = 0;
    outbuf = 0;
    outbuf_size = 0;
    outfile = 0;
    picture = 0;
    input_picture = 0;
    picbuf = 0;
    video_st = 0;
    input_pix_fmt = 0;
1495
    aligned_input = NULL;
1496
    aligned_input_size = 0;
1497
    img_convert_ctx = 0;
V
Vadim Pisarevsky 已提交
1498
    frame_width = frame_height = 0;
1499
    frame_idx = 0;
V
Vadim Pisarevsky 已提交
1500
    ok = false;
1501 1502 1503 1504 1505 1506 1507 1508
}

/**
 * the following function is a modified version of code
 * found in ffmpeg-0.4.9-pre1/output_example.c
 */
static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
{
V
Vadim Pisarevsky 已提交
1509
    AVFrame * picture;
1510
    uint8_t * picture_buf = 0;
V
Vadim Pisarevsky 已提交
1511 1512
    int size;

J
jisli 已提交
1513 1514 1515 1516
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
    ? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
    picture = av_frame_alloc();
#else
V
Vadim Pisarevsky 已提交
1517
    picture = avcodec_alloc_frame();
J
jisli 已提交
1518
#endif
V
Vadim Pisarevsky 已提交
1519 1520
    if (!picture)
        return NULL;
1521 1522 1523 1524 1525

    picture->format = pix_fmt;
    picture->width = width;
    picture->height = height;

P
Peter Rekdal Sunde 已提交
1526
    size = _opencv_ffmpeg_av_image_get_buffer_size( (AVPixelFormat) pix_fmt, width, height);
V
Vadim Pisarevsky 已提交
1527 1528 1529 1530 1531 1532 1533
    if(alloc){
        picture_buf = (uint8_t *) malloc(size);
        if (!picture_buf)
        {
            av_free(picture);
            return NULL;
        }
P
Peter Rekdal Sunde 已提交
1534
        _opencv_ffmpeg_av_image_fill_arrays(picture, picture_buf,
J
jisli 已提交
1535
                       (AVPixelFormat) pix_fmt, width, height);
V
Vadim Pisarevsky 已提交
1536
    }
1537

V
Vadim Pisarevsky 已提交
1538
    return picture;
1539 1540 1541 1542
}

/* add a video output stream to the container */
static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1543
                                             CV_CODEC_ID codec_id,
V
Vadim Pisarevsky 已提交
1544 1545
                                             int w, int h, int bitrate,
                                             double fps, int pixel_format)
1546
{
V
Vadim Pisarevsky 已提交
1547 1548 1549 1550
    AVCodecContext *c;
    AVStream *st;
    int frame_rate, frame_rate_base;
    AVCodec *codec;
1551

V
Vadim Pisarevsky 已提交
1552 1553 1554 1555 1556
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
    st = avformat_new_stream(oc, 0);
#else
    st = av_new_stream(oc, 0);
#endif
1557

V
Vadim Pisarevsky 已提交
1558 1559 1560 1561
    if (!st) {
        CV_WARN("Could not allocate stream");
        return NULL;
    }
1562 1563

#if LIBAVFORMAT_BUILD > 4628
V
Vadim Pisarevsky 已提交
1564
    c = st->codec;
1565
#else
V
Vadim Pisarevsky 已提交
1566
    c = &(st->codec);
1567 1568 1569
#endif

#if LIBAVFORMAT_BUILD > 4621
V
Vadim Pisarevsky 已提交
1570
    c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1571
#else
V
Vadim Pisarevsky 已提交
1572
    c->codec_id = oc->oformat->video_codec;
1573 1574
#endif

1575
    if(codec_id != CV_CODEC(CODEC_ID_NONE)){
V
Vadim Pisarevsky 已提交
1576 1577
        c->codec_id = codec_id;
    }
1578 1579

    //if(codec_tag) c->codec_tag=codec_tag;
V
Vadim Pisarevsky 已提交
1580
    codec = avcodec_find_encoder(c->codec_id);
1581

V
Vadim Pisarevsky 已提交
1582
    c->codec_type = AVMEDIA_TYPE_VIDEO;
1583

1584
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
1585 1586 1587 1588 1589
    // Set per-codec defaults
    AVCodecID c_id = c->codec_id;
    avcodec_get_context_defaults3(c, codec);
    // avcodec_get_context_defaults3 erases codec_id for some reason
    c->codec_id = c_id;
1590
#endif
1591

V
Vadim Pisarevsky 已提交
1592 1593 1594 1595 1596
    /* put sample parameters */
    int64_t lbit_rate = (int64_t)bitrate;
    lbit_rate += (bitrate / 2);
    lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
    c->bit_rate = lbit_rate;
1597

V
Vadim Pisarevsky 已提交
1598 1599 1600
    // took advice from
    // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
    c->qmin = 3;
1601

V
Vadim Pisarevsky 已提交
1602 1603 1604 1605 1606 1607
    /* resolution must be a multiple of two */
    c->width = w;
    c->height = h;

    /* time base: this is the fundamental unit of time (in seconds) in terms
       of which frame timestamps are represented. for fixed-fps content,
1608 1609
       timebase should be 1/framerate and timestamp increments should be
       identically 1. */
V
Vadim Pisarevsky 已提交
1610 1611
    frame_rate=(int)(fps+0.5);
    frame_rate_base=1;
1612
    while (fabs(((double)frame_rate/frame_rate_base) - fps) > 0.001){
V
Vadim Pisarevsky 已提交
1613 1614 1615
        frame_rate_base*=10;
        frame_rate=(int)(fps*frame_rate_base + 0.5);
    }
1616 1617 1618
#if LIBAVFORMAT_BUILD > 4752
    c->time_base.den = frame_rate;
    c->time_base.num = frame_rate_base;
V
Vadim Pisarevsky 已提交
1619 1620 1621
    /* adjust time base for supported framerates */
    if(codec && codec->supported_framerates){
        const AVRational *p= codec->supported_framerates;
1622
        AVRational req = {frame_rate, frame_rate_base};
V
Vadim Pisarevsky 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
        const AVRational *best=NULL;
        AVRational best_error= {INT_MAX, 1};
        for(; p->den!=0; p++){
            AVRational error= av_sub_q(req, *p);
            if(error.num <0) error.num *= -1;
            if(av_cmp_q(error, best_error) < 0){
                best_error= error;
                best= p;
            }
        }
1633 1634
        if (best == NULL)
            return NULL;
V
Vadim Pisarevsky 已提交
1635 1636 1637
        c->time_base.den= best->num;
        c->time_base.num= best->den;
    }
1638
#else
V
Vadim Pisarevsky 已提交
1639 1640
    c->frame_rate = frame_rate;
    c->frame_rate_base = frame_rate_base;
1641 1642
#endif

V
Vadim Pisarevsky 已提交
1643
    c->gop_size = 12; /* emit one intra frame every twelve frames at most */
J
jisli 已提交
1644
    c->pix_fmt = (AVPixelFormat) pixel_format;
1645

1646
    if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1647 1648
        c->max_b_frames = 2;
    }
1649
    if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1650
        /* needed to avoid using macroblocks in which some coeffs overflow
L
luz.paz 已提交
1651 1652
           this doesn't happen with normal video, it just happens here as the
           motion of the chroma plane doesn't match the luma plane */
V
Vadim Pisarevsky 已提交
1653
        /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1654 1655
        c->mb_decision=2;
    }
1656 1657

#if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
1658 1659
    /* Some settings for libx264 encoding, restore dummy values for gop_size
     and qmin since they will be set to reasonable defaults by the libx264
1660
     preset system. Also, use a crf encode with the default quality rating,
1661
     this seems easier than finding an appropriate default bitrate. */
1662
    if (c->codec_id == AV_CODEC_ID_H264) {
1663 1664 1665
      c->gop_size = -1;
      c->qmin = -1;
      c->bit_rate = 0;
1666 1667
      if (c->priv_data)
          av_opt_set(c->priv_data,"crf","23", 0);
1668
    }
1669 1670
#endif

1671
#if LIBAVCODEC_VERSION_INT>0x000409
L
luz.paz 已提交
1672
    // some formats want stream headers to be separate
1673 1674
    if(oc->oformat->flags & AVFMT_GLOBALHEADER)
    {
1675 1676 1677
#if LIBAVCODEC_BUILD > CALC_FFMPEG_VERSION(56, 35, 0)
        c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
#else
1678
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1679
#endif
1680 1681 1682
    }
#endif

A
Alexander Alekhin 已提交
1683
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(52, 42, 0)
1684 1685 1686 1687
#if defined(_MSC_VER)
    AVRational avg_frame_rate = {frame_rate, frame_rate_base};
    st->avg_frame_rate = avg_frame_rate;
#else
A
Alexander Alekhin 已提交
1688 1689
    st->avg_frame_rate = (AVRational){frame_rate, frame_rate_base};
#endif
1690
#endif
1691 1692 1693
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 20, 0)
    st->time_base = c->time_base;
#endif
A
Alexander Alekhin 已提交
1694

1695 1696 1697
    return st;
}

V
Vadim Pisarevsky 已提交
1698 1699
static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;

1700 1701 1702 1703 1704 1705 1706
static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
                                      uint8_t *, uint32_t,
#else
                                      uint8_t * outbuf, uint32_t outbuf_size,
#endif
                                      AVFrame * picture )
1707 1708
{
#if LIBAVFORMAT_BUILD > 4628
V
Vadim Pisarevsky 已提交
1709
    AVCodecContext * c = video_st->codec;
1710
#else
V
Vadim Pisarevsky 已提交
1711
    AVCodecContext * c = &(video_st->codec);
1712
#endif
1713
    int ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1714

1715 1716 1717
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
    if (oc->oformat->flags & AVFMT_RAWPICTURE)
    {
1718 1719 1720 1721 1722
        /* raw video case. The API will change slightly in the near
           futur for that */
        AVPacket pkt;
        av_init_packet(&pkt);

V
Vadim Pisarevsky 已提交
1723
        pkt.flags |= PKT_FLAG_KEY;
1724 1725 1726 1727 1728
        pkt.stream_index= video_st->index;
        pkt.data= (uint8_t *)picture;
        pkt.size= sizeof(AVPicture);

        ret = av_write_frame(oc, &pkt);
1729 1730 1731 1732
    }
    else
#endif
    {
1733
        /* encode the image */
1734 1735 1736 1737 1738 1739 1740 1741
        AVPacket pkt;
        av_init_packet(&pkt);
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
        int got_output = 0;
        pkt.data = NULL;
        pkt.size = 0;
        ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
        if (ret < 0)
1742
            ;
1743
        else if (got_output) {
1744 1745 1746 1747 1748 1749
            if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
                pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
            if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
                pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
            if (pkt.duration)
                pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
1750 1751
            pkt.stream_index= video_st->index;
            ret = av_write_frame(oc, &pkt);
P
Peter Rekdal Sunde 已提交
1752
            _opencv_ffmpeg_av_packet_unref(&pkt);
1753 1754 1755 1756 1757
        }
        else
            ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
#else
        int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1758 1759 1760
        /* if zero size, it means the image was buffered */
        if (out_size > 0) {
#if LIBAVFORMAT_BUILD > 4752
1761
            if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1762
                pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1763
#else
V
Vadim Pisarevsky 已提交
1764
            pkt.pts = c->coded_frame->pts;
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
#endif
            if(c->coded_frame->key_frame)
                pkt.flags |= PKT_FLAG_KEY;
            pkt.stream_index= video_st->index;
            pkt.data= outbuf;
            pkt.size= out_size;

            /* write the compressed frame in the media file */
            ret = av_write_frame(oc, &pkt);
        }
1775
#endif
1776
    }
V
Vadim Pisarevsky 已提交
1777
    return ret;
1778 1779 1780 1781 1782
}

/// write a frame with FFMPEG
bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
{
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
    // check parameters
    if (input_pix_fmt == AV_PIX_FMT_BGR24) {
        if (cn != 3) {
            return false;
        }
    }
    else if (input_pix_fmt == AV_PIX_FMT_GRAY8) {
        if (cn != 1) {
            return false;
        }
    }
    else {
        assert(false);
    }
1797

V
Vadim Pisarevsky 已提交
1798 1799 1800 1801
    if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
        return false;
    width = frame_width;
    height = frame_height;
1802

V
Vadim Pisarevsky 已提交
1803
    // typecast from opaque data type to implemented struct
1804 1805 1806
#if LIBAVFORMAT_BUILD > 4628
    AVCodecContext *c = video_st->codec;
#else
A
Alexander Shishkov 已提交
1807
    AVCodecContext *c = &(video_st->codec);
1808 1809
#endif

1810
    // FFmpeg contains SIMD optimizations which can sometimes read data past
1811 1812 1813 1814 1815 1816 1817 1818
    // the supplied input buffer.
    // Related info: https://trac.ffmpeg.org/ticket/6763
    // 1. To ensure that doesn't happen, we pad the step to a multiple of 32
    // (that's the minimal alignment for which Valgrind doesn't raise any warnings).
    // 2. (dataend - SIMD_SIZE) and (dataend + SIMD_SIZE) is from the same 4k page
    const int CV_STEP_ALIGNMENT = 32;
    const size_t CV_SIMD_SIZE = 32;
    const size_t CV_PAGE_MASK = ~(4096 - 1);
A
Alexander Alekhin 已提交
1819
    const unsigned char* dataend = data + ((size_t)height * step);
1820 1821
    if (step % CV_STEP_ALIGNMENT != 0 ||
        (((size_t)dataend - CV_SIMD_SIZE) & CV_PAGE_MASK) != (((size_t)dataend + CV_SIMD_SIZE) & CV_PAGE_MASK))
A
Alexander Shishkov 已提交
1822
    {
1823
        int aligned_step = (step + CV_STEP_ALIGNMENT - 1) & ~(CV_STEP_ALIGNMENT - 1);
1824

1825 1826 1827
        size_t new_size = (aligned_step * height + CV_SIMD_SIZE);

        if (!aligned_input || aligned_input_size < new_size)
A
Alexander Shishkov 已提交
1828
        {
1829 1830 1831 1832
            if (aligned_input)
                av_freep(&aligned_input);
            aligned_input_size = new_size;
            aligned_input = (unsigned char*)av_mallocz(aligned_input_size);
A
Alexander Shishkov 已提交
1833
        }
1834

A
Alexander Shishkov 已提交
1835 1836
        if (origin == 1)
            for( int y = 0; y < height; y++ )
1837
                memcpy(aligned_input + y*aligned_step, data + (height-1-y)*step, step);
A
Alexander Shishkov 已提交
1838 1839
        else
            for( int y = 0; y < height; y++ )
1840
                memcpy(aligned_input + y*aligned_step, data + y*step, step);
1841

1842 1843
        data = aligned_input;
        step = aligned_step;
1844 1845
    }

V
Vadim Pisarevsky 已提交
1846 1847 1848
    if ( c->pix_fmt != input_pix_fmt ) {
        assert( input_picture );
        // let input_picture point to the raw data buffer of 'image'
P
Peter Rekdal Sunde 已提交
1849
        _opencv_ffmpeg_av_image_fill_arrays(input_picture, (uint8_t *) data,
J
jisli 已提交
1850
                       (AVPixelFormat)input_pix_fmt, width, height);
1851
        input_picture->linesize[0] = step;
1852

V
Vadim Pisarevsky 已提交
1853 1854 1855 1856
        if( !img_convert_ctx )
        {
            img_convert_ctx = sws_getContext(width,
                                             height,
J
jisli 已提交
1857
                                             (AVPixelFormat)input_pix_fmt,
V
Vadim Pisarevsky 已提交
1858 1859 1860 1861 1862 1863 1864 1865
                                             c->width,
                                             c->height,
                                             c->pix_fmt,
                                             SWS_BICUBIC,
                                             NULL, NULL, NULL);
            if( !img_convert_ctx )
                return false;
        }
1866 1867 1868 1869 1870 1871

        if ( sws_scale(img_convert_ctx, input_picture->data,
                       input_picture->linesize, 0,
                       height,
                       picture->data, picture->linesize) < 0 )
            return false;
V
Vadim Pisarevsky 已提交
1872 1873
    }
    else{
P
Peter Rekdal Sunde 已提交
1874
        _opencv_ffmpeg_av_image_fill_arrays(picture, (uint8_t *) data,
J
jisli 已提交
1875
                       (AVPixelFormat)input_pix_fmt, width, height);
1876
        picture->linesize[0] = step;
V
Vadim Pisarevsky 已提交
1877
    }
1878

1879
    picture->pts = frame_idx;
1880
    bool ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1881
    frame_idx++;
1882

V
Vadim Pisarevsky 已提交
1883
    return ret;
1884 1885 1886 1887 1888
}

/// close video output stream and free associated memory
void CvVideoWriter_FFMPEG::close()
{
V
Vadim Pisarevsky 已提交
1889 1890 1891
    // nothing to do if already released
    if ( !picture )
        return;
1892

V
Vadim Pisarevsky 已提交
1893 1894 1895 1896
    /* no more frame to compress. The codec has a latency of a few
       frames if using B frames, so we get the last frames by
       passing the same picture again */
    // TODO -- do we need to account for latency here?
1897

V
Vadim Pisarevsky 已提交
1898 1899 1900
    /* write the trailer, if any */
    if(ok && oc)
    {
1901 1902 1903
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
        if (!(oc->oformat->flags & AVFMT_RAWPICTURE))
#endif
V
Vadim Pisarevsky 已提交
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
        {
            for(;;)
            {
                int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
                if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
                    break;
            }
        }
        av_write_trailer(oc);
    }
1914

V
Vadim Pisarevsky 已提交
1915 1916 1917 1918 1919
    if( img_convert_ctx )
    {
        sws_freeContext(img_convert_ctx);
        img_convert_ctx = 0;
    }
1920

V
Vadim Pisarevsky 已提交
1921
    // free pictures
1922
#if LIBAVFORMAT_BUILD > 4628
V
Vadim Pisarevsky 已提交
1923
    if( video_st->codec->pix_fmt != input_pix_fmt)
1924
#else
V
Vadim Pisarevsky 已提交
1925
    if( video_st->codec.pix_fmt != input_pix_fmt)
1926
#endif
V
Vadim Pisarevsky 已提交
1927 1928 1929 1930 1931 1932
    {
        if(picture->data[0])
            free(picture->data[0]);
        picture->data[0] = 0;
    }
    av_free(picture);
1933

V
Vadim Pisarevsky 已提交
1934 1935
    if (input_picture)
        av_free(input_picture);
1936

V
Vadim Pisarevsky 已提交
1937
    /* close codec */
1938
#if LIBAVFORMAT_BUILD > 4628
V
Vadim Pisarevsky 已提交
1939
    avcodec_close(video_st->codec);
1940
#else
V
Vadim Pisarevsky 已提交
1941
    avcodec_close(&(video_st->codec));
1942 1943
#endif

V
Vadim Pisarevsky 已提交
1944
    av_free(outbuf);
1945

1946
    if (oc)
V
Vadim Pisarevsky 已提交
1947
    {
1948 1949 1950
        if (!(fmt->flags & AVFMT_NOFILE))
        {
            /* close the output file */
1951

V
Vadim Pisarevsky 已提交
1952
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1953
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1954
            url_fclose(oc->pb);
1955
#else
1956
            url_fclose(&oc->pb);
V
Vadim Pisarevsky 已提交
1957 1958
#endif
#else
1959
            avio_close(oc->pb);
1960 1961
#endif

1962
        }
1963

1964 1965 1966
        /* free the stream */
        avformat_free_context(oc);
    }
1967

1968
    av_freep(&aligned_input);
1969

V
Vadim Pisarevsky 已提交
1970 1971
    init();
}
1972

1973 1974 1975
#define CV_PRINTABLE_CHAR(ch) ((ch) < 32 ? '?' : (ch))
#define CV_TAG_TO_PRINTABLE_CHAR4(tag) CV_PRINTABLE_CHAR((tag) & 255), CV_PRINTABLE_CHAR(((tag) >> 8) & 255), CV_PRINTABLE_CHAR(((tag) >> 16) & 255), CV_PRINTABLE_CHAR(((tag) >> 24) & 255)

1976
static inline bool cv_ff_codec_tag_match(const AVCodecTag *tags, CV_CODEC_ID id, unsigned int tag)
1977 1978 1979 1980 1981 1982 1983 1984 1985
{
    while (tags->id != AV_CODEC_ID_NONE)
    {
        if (tags->id == id && tags->tag == tag)
            return true;
        tags++;
    }
    return false;
}
E
Emanuele Ruffaldi 已提交
1986

1987
static inline bool cv_ff_codec_tag_list_match(const AVCodecTag *const *tags, CV_CODEC_ID id, unsigned int tag)
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
{
    int i;
    for (i = 0; tags && tags[i]; i++) {
        bool res = cv_ff_codec_tag_match(tags[i], id, tag);
        if (res)
            return res;
    }
    return false;
}

E
Emanuele Ruffaldi 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

static inline void cv_ff_codec_tag_dump(const AVCodecTag *const *tags)
{
    int i;
    for (i = 0; tags && tags[i]; i++) {
        const AVCodecTag * ptags = tags[i];
        while (ptags->id != AV_CODEC_ID_NONE)
        {
            unsigned int tag = ptags->tag;
            printf("fourcc tag 0x%08x/'%c%c%c%c' codec_id %04X\n", tag, CV_TAG_TO_PRINTABLE_CHAR4(tag), ptags->id);
            ptags++;
        }
    }
}

V
Vadim Pisarevsky 已提交
2013 2014 2015 2016
/// Create a video writer object that uses FFMPEG
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
                                 double fps, int width, int height, bool is_color )
{
2017
    InternalFFMpegRegister::init();
2018
    CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
V
Vadim Pisarevsky 已提交
2019 2020
    int err, codec_pix_fmt;
    double bitrate_scale = 1;
2021

V
Vadim Pisarevsky 已提交
2022
    close();
2023

V
Vadim Pisarevsky 已提交
2024 2025 2026 2027 2028
    // check arguments
    if( !filename )
        return false;
    if(fps <= 0)
        return false;
2029

V
Vadim Pisarevsky 已提交
2030 2031 2032 2033 2034 2035 2036
    // we allow frames of odd width or height, but in this case we truncate
    // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
    // but some internal functions inside FFMPEG swscale require even width/height.
    width &= -2;
    height &= -2;
    if( width <= 0 || height <= 0 )
        return false;
2037

V
Vadim Pisarevsky 已提交
2038
    /* auto detect the output format from the name and fourcc code. */
2039

2040
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
V
Vadim Pisarevsky 已提交
2041
    fmt = av_guess_format(NULL, filename, NULL);
2042
#else
V
Vadim Pisarevsky 已提交
2043
    fmt = guess_format(NULL, filename, NULL);
2044
#endif
2045

V
Vadim Pisarevsky 已提交
2046 2047
    if (!fmt)
        return false;
2048

V
Vadim Pisarevsky 已提交
2049 2050
    /* determine optimal pixel format */
    if (is_color) {
J
jisli 已提交
2051
        input_pix_fmt = AV_PIX_FMT_BGR24;
V
Vadim Pisarevsky 已提交
2052 2053
    }
    else {
J
jisli 已提交
2054
        input_pix_fmt = AV_PIX_FMT_GRAY8;
V
Vadim Pisarevsky 已提交
2055
    }
2056

E
Emanuele Ruffaldi 已提交
2057 2058 2059 2060 2061 2062 2063
    if (fourcc == -1)
    {
        fprintf(stderr,"OpenCV: FFMPEG: format %s / %s\n", fmt->name, fmt->long_name);
        cv_ff_codec_tag_dump(fmt->codec_tag);
        return false;
    }

V
Vadim Pisarevsky 已提交
2064
    /* Lookup codec_id for given fourcc */
2065
#if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
2066
    if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
V
Vadim Pisarevsky 已提交
2067
        return false;
2068
#else
2069 2070 2071
    if( (codec_id = av_codec_get_id(fmt->codec_tag, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
    {
        const struct AVCodecTag * fallback_tags[] = {
2072 2073 2074 2075
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
// APIchanges:
// 2012-01-31 - dd6d3b0 - lavf 54.01.0
//   Add avformat_get_riff_video_tags() and avformat_get_riff_audio_tags().
2076
                avformat_get_riff_video_tags(),
2077 2078
#endif
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 25, 100) && defined LIBAVFORMAT_VERSION_MICRO && LIBAVFORMAT_VERSION_MICRO >= 100
2079 2080 2081
// APIchanges: ffmpeg only
// 2014-01-19 - 1a193c4 - lavf 55.25.100 - avformat.h
//   Add avformat_get_mov_video_tags() and avformat_get_mov_audio_tags().
2082
                avformat_get_mov_video_tags(),
2083
#endif
2084 2085
                codec_bmp_tags, // fallback for avformat < 54.1
                NULL };
2086 2087 2088 2089 2090 2091 2092 2093 2094
        if( (codec_id = av_codec_get_id(fallback_tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
        {
            fflush(stdout);
            fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not found (format '%s / %s')'\n",
                    fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
                    fmt->name, fmt->long_name);
            return false;
        }
    }
E
Emanuele Ruffaldi 已提交
2095 2096


2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
    // validate tag
    if (cv_ff_codec_tag_list_match(fmt->codec_tag, codec_id, fourcc) == false)
    {
        fflush(stdout);
        fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not supported with codec id %d and format '%s / %s'\n",
                fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
                codec_id, fmt->name, fmt->long_name);
        int supported_tag;
        if( (supported_tag = av_codec_get_tag(fmt->codec_tag, codec_id)) != 0 )
        {
            fprintf(stderr, "OpenCV: FFMPEG: fallback to use tag 0x%08x/'%c%c%c%c'\n",
                    supported_tag, CV_TAG_TO_PRINTABLE_CHAR4(supported_tag));
            fourcc = supported_tag;
        }
    }
2112 2113
#endif

V
Vadim Pisarevsky 已提交
2114
    // alloc memory for context
2115
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
V
Vadim Pisarevsky 已提交
2116
    oc = avformat_alloc_context();
2117
#else
V
Vadim Pisarevsky 已提交
2118
    oc = av_alloc_format_context();
2119
#endif
V
Vadim Pisarevsky 已提交
2120
    assert (oc);
2121

V
Vadim Pisarevsky 已提交
2122 2123 2124
    /* set file name */
    oc->oformat = fmt;
    snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
2125

V
Vadim Pisarevsky 已提交
2126 2127
    /* set some options */
    oc->max_delay = (int)(0.7*AV_TIME_BASE);  /* This reduces buffer underrun warnings with MPEG */
2128

V
Vadim Pisarevsky 已提交
2129 2130
    // set a few optimal pixel formats for lossless codecs of interest..
    switch (codec_id) {
2131
#if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
2132
    case CV_CODEC(CODEC_ID_JPEGLS):
V
Vadim Pisarevsky 已提交
2133
        // BGR24 or GRAY8 depending on is_color...
E
Emanuele Ruffaldi 已提交
2134 2135
        // supported: bgr24 rgb24 gray gray16le
        // as of version 3.4.1
V
Vadim Pisarevsky 已提交
2136 2137
        codec_pix_fmt = input_pix_fmt;
        break;
2138
#endif
2139
    case CV_CODEC(CODEC_ID_HUFFYUV):
E
Emanuele Ruffaldi 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
        // supported: yuv422p rgb24 bgra
        // as of version 3.4.1
        switch(input_pix_fmt)
        {
            case AV_PIX_FMT_RGB24:
            case AV_PIX_FMT_BGRA:
                codec_pix_fmt = input_pix_fmt;
                break;
            case AV_PIX_FMT_BGR24:
                codec_pix_fmt = AV_PIX_FMT_RGB24;
                break;
            default:
                codec_pix_fmt = AV_PIX_FMT_YUV422P;
                break;
        }
        break;
    case CV_CODEC(CODEC_ID_PNG):
        // supported: rgb24 rgba rgb48be rgba64be pal8 gray ya8 gray16be ya16be monob
        // as of version 3.4.1
        switch(input_pix_fmt)
        {
            case AV_PIX_FMT_GRAY8:
            case AV_PIX_FMT_GRAY16BE:
            case AV_PIX_FMT_RGB24:
            case AV_PIX_FMT_BGRA:
                codec_pix_fmt = input_pix_fmt;
                break;
            case AV_PIX_FMT_GRAY16LE:
                codec_pix_fmt = AV_PIX_FMT_GRAY16BE;
                break;
            case AV_PIX_FMT_BGR24:
                codec_pix_fmt = AV_PIX_FMT_RGB24;
                break;
            default:
                codec_pix_fmt = AV_PIX_FMT_YUV422P;
                break;
        }
        break;
    case CV_CODEC(CODEC_ID_FFV1):
        // supported: MANY
        // as of version 3.4.1
        switch(input_pix_fmt)
        {
            case AV_PIX_FMT_GRAY8:
            case AV_PIX_FMT_GRAY16LE:
#ifdef AV_PIX_FMT_BGR0
            case AV_PIX_FMT_BGR0:
#endif
            case AV_PIX_FMT_BGRA:
                codec_pix_fmt = input_pix_fmt;
                break;
            case AV_PIX_FMT_GRAY16BE:
                codec_pix_fmt = AV_PIX_FMT_GRAY16LE;
                break;
            case AV_PIX_FMT_BGR24:
            case AV_PIX_FMT_RGB24:
#ifdef AV_PIX_FMT_BGR0
                codec_pix_fmt = AV_PIX_FMT_BGR0;
#else
                codec_pix_fmt = AV_PIX_FMT_BGRA;
#endif
                break;
            default:
                codec_pix_fmt = AV_PIX_FMT_YUV422P;
                break;
        }
V
Vadim Pisarevsky 已提交
2206
        break;
2207 2208
    case CV_CODEC(CODEC_ID_MJPEG):
    case CV_CODEC(CODEC_ID_LJPEG):
J
jisli 已提交
2209
        codec_pix_fmt = AV_PIX_FMT_YUVJ420P;
V
Vadim Pisarevsky 已提交
2210 2211
        bitrate_scale = 3;
        break;
2212
    case CV_CODEC(CODEC_ID_RAWVIDEO):
E
Emanuele Ruffaldi 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
        // RGBA is the only RGB fourcc supported by AVI and MKV format
        if(fourcc == CV_FOURCC('R','G','B','A'))
        {
            codec_pix_fmt = AV_PIX_FMT_RGBA;
        }
        else
        {
            switch(input_pix_fmt)
            {
                case AV_PIX_FMT_GRAY8:
                case AV_PIX_FMT_GRAY16LE:
                case AV_PIX_FMT_GRAY16BE:
                    codec_pix_fmt = input_pix_fmt;
                    break;
                default:
                    codec_pix_fmt = AV_PIX_FMT_YUV420P;
                    break;
            }
        }
V
Vadim Pisarevsky 已提交
2232 2233 2234
        break;
    default:
        // good for lossy formats, MPEG, etc.
J
jisli 已提交
2235
        codec_pix_fmt = AV_PIX_FMT_YUV420P;
V
Vadim Pisarevsky 已提交
2236 2237
        break;
    }
2238

V
Vadim Pisarevsky 已提交
2239
    double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
2240

V
Vadim Pisarevsky 已提交
2241 2242 2243 2244
    // TODO -- safe to ignore output audio stream?
    video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
                                           width, height, (int)(bitrate + 0.5),
                                           fps, codec_pix_fmt);
2245

V
Vadim Pisarevsky 已提交
2246 2247 2248 2249 2250 2251 2252
    /* set the output parameters (must be done even if no
   parameters). */
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
    if (av_set_parameters(oc, NULL) < 0) {
        return false;
    }
#endif
2253

V
Vadim Pisarevsky 已提交
2254 2255 2256
#if 0
#if FF_API_DUMP_FORMAT
    dump_format(oc, 0, filename, 1);
2257
#else
V
Vadim Pisarevsky 已提交
2258 2259
    av_dump_format(oc, 0, filename, 1);
#endif
2260 2261
#endif

V
Vadim Pisarevsky 已提交
2262 2263 2264 2265
    /* now that all the parameters are set, we can open the audio and
     video codecs and allocate the necessary encode buffers */
    if (!video_st){
        return false;
2266
    }
2267

V
Vadim Pisarevsky 已提交
2268 2269
    AVCodec *codec;
    AVCodecContext *c;
2270

V
Vadim Pisarevsky 已提交
2271 2272 2273 2274 2275
#if LIBAVFORMAT_BUILD > 4628
    c = (video_st->codec);
#else
    c = &(video_st->codec);
#endif
2276

V
Vadim Pisarevsky 已提交
2277 2278 2279 2280
    c->codec_tag = fourcc;
    /* find the video encoder */
    codec = avcodec_find_encoder(c->codec_id);
    if (!codec) {
2281
        fprintf(stderr, "Could not find encoder for codec id %d: %s\n", c->codec_id, icvFFMPEGErrStr(
V
Vadim Pisarevsky 已提交
2282 2283 2284 2285 2286 2287 2288
        #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
                AVERROR_ENCODER_NOT_FOUND
        #else
                -1
        #endif
                ));
        return false;
2289
    }
2290

V
Vadim Pisarevsky 已提交
2291 2292 2293 2294 2295
    int64_t lbit_rate = (int64_t)c->bit_rate;
    lbit_rate += (bitrate / 2);
    lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
    c->bit_rate_tolerance = (int)lbit_rate;
    c->bit_rate = (int)lbit_rate;
2296

V
Vadim Pisarevsky 已提交
2297 2298 2299 2300 2301 2302 2303 2304
    /* open the codec */
    if ((err=
#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
         avcodec_open2(c, codec, NULL)
#else
         avcodec_open(c, codec)
#endif
         ) < 0) {
2305
        fprintf(stderr, "Could not open codec '%s': %s\n", codec->name, icvFFMPEGErrStr(err));
V
Vadim Pisarevsky 已提交
2306
        return false;
2307
    }
2308

V
Vadim Pisarevsky 已提交
2309
    outbuf = NULL;
2310

2311 2312 2313 2314 2315

#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
    if (!(oc->oformat->flags & AVFMT_RAWPICTURE))
#endif
    {
V
Vadim Pisarevsky 已提交
2316 2317 2318 2319
        /* allocate output buffer */
        /* assume we will never get codec output with more than 4 bytes per pixel... */
        outbuf_size = width*height*4;
        outbuf = (uint8_t *) av_malloc(outbuf_size);
2320
    }
2321

V
Vadim Pisarevsky 已提交
2322 2323
    bool need_color_convert;
    need_color_convert = (c->pix_fmt != input_pix_fmt);
2324

V
Vadim Pisarevsky 已提交
2325 2326 2327 2328
    /* allocate the encoded raw picture */
    picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
    if (!picture) {
        return false;
2329
    }
2330

V
Vadim Pisarevsky 已提交
2331 2332 2333 2334 2335 2336 2337 2338
    /* if the output format is not our input format, then a temporary
   picture of the input format is needed too. It is then converted
   to the required output format */
    input_picture = NULL;
    if ( need_color_convert ) {
        input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
        if (!input_picture) {
            return false;
2339
        }
2340 2341
    }

V
Vadim Pisarevsky 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
    /* open the output file, if needed */
    if (!(fmt->flags & AVFMT_NOFILE)) {
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
        if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
#else
            if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
#endif
            {
            return false;
        }
2352
    }
2353

V
Vadim Pisarevsky 已提交
2354 2355 2356 2357 2358
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    /* write the stream header, if any */
    err=avformat_write_header(oc, NULL);
#else
    err=av_write_header( oc );
2359 2360
#endif

V
Vadim Pisarevsky 已提交
2361
    if(err < 0)
V
Vladislav Vinogradov 已提交
2362
    {
V
Vadim Pisarevsky 已提交
2363 2364 2365
        close();
        remove(filename);
        return false;
V
Vladislav Vinogradov 已提交
2366
    }
V
Vadim Pisarevsky 已提交
2367 2368
    frame_width = width;
    frame_height = height;
2369
    frame_idx = 0;
V
Vadim Pisarevsky 已提交
2370
    ok = true;
I
Ilya Lavrenov 已提交
2371

V
Vadim Pisarevsky 已提交
2372
    return true;
V
Vladislav Vinogradov 已提交
2373 2374 2375 2376
}



V
Vadim Pisarevsky 已提交
2377
CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
V
Vladislav Vinogradov 已提交
2378
{
V
Vadim Pisarevsky 已提交
2379
    CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
2380 2381
    if (!capture)
        return 0;
V
Vadim Pisarevsky 已提交
2382 2383 2384
    capture->init();
    if( capture->open( filename ))
        return capture;
I
Ilya Lavrenov 已提交
2385

V
Vadim Pisarevsky 已提交
2386 2387 2388
    capture->close();
    free(capture);
    return 0;
V
Vladislav Vinogradov 已提交
2389 2390
}

V
Vadim Pisarevsky 已提交
2391 2392

void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
V
Vladislav Vinogradov 已提交
2393
{
V
Vadim Pisarevsky 已提交
2394
    if( capture && *capture )
V
Vladislav Vinogradov 已提交
2395
    {
V
Vadim Pisarevsky 已提交
2396 2397 2398
        (*capture)->close();
        free(*capture);
        *capture = 0;
V
Vladislav Vinogradov 已提交
2399 2400 2401
    }
}

V
Vadim Pisarevsky 已提交
2402
int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
V
Vladislav Vinogradov 已提交
2403
{
V
Vadim Pisarevsky 已提交
2404
    return capture->setProperty(prop_id, value);
V
Vladislav Vinogradov 已提交
2405 2406
}

V
Vadim Pisarevsky 已提交
2407
double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
V
Vladislav Vinogradov 已提交
2408
{
V
Vadim Pisarevsky 已提交
2409
    return capture->getProperty(prop_id);
V
Vladislav Vinogradov 已提交
2410 2411
}

V
Vadim Pisarevsky 已提交
2412
int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
V
Vladislav Vinogradov 已提交
2413
{
V
Vadim Pisarevsky 已提交
2414
    return capture->grabFrame();
V
Vladislav Vinogradov 已提交
2415
}
V
Vladislav Vinogradov 已提交
2416

V
Vadim Pisarevsky 已提交
2417
int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
V
Vladislav Vinogradov 已提交
2418
{
V
Vadim Pisarevsky 已提交
2419
    return capture->retrieveFrame(0, data, step, width, height, cn);
V
Vladislav Vinogradov 已提交
2420 2421
}

V
Vadim Pisarevsky 已提交
2422 2423
CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
                                                  int width, int height, int isColor )
V
Vladislav Vinogradov 已提交
2424
{
V
Vadim Pisarevsky 已提交
2425
    CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
2426 2427
    if (!writer)
        return 0;
V
Vadim Pisarevsky 已提交
2428 2429 2430 2431 2432 2433
    writer->init();
    if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
        return writer;
    writer->close();
    free(writer);
    return 0;
V
Vladislav Vinogradov 已提交
2434 2435
}

V
Vadim Pisarevsky 已提交
2436 2437 2438
void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
{
    if( writer && *writer )
V
Vladislav Vinogradov 已提交
2439
    {
V
Vadim Pisarevsky 已提交
2440 2441 2442
        (*writer)->close();
        free(*writer);
        *writer = 0;
V
Vladislav Vinogradov 已提交
2443 2444 2445 2446
    }
}


V
Vadim Pisarevsky 已提交
2447 2448 2449
int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
                         const unsigned char* data, int step,
                         int width, int height, int cn, int origin)
V
Vladislav Vinogradov 已提交
2450
{
V
Vadim Pisarevsky 已提交
2451
    return writer->writeFrame(data, step, width, height, cn, origin);
V
Vladislav Vinogradov 已提交
2452
}