cap_ffmpeg_impl.hpp 41.8 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.
//
//
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
10
//                          License Agreement
11 12
//                For Open Source Computer Vision Library
//
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
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.
//
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
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 43 44
//     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 "cap_ffmpeg_api.hpp"
#include <assert.h>
45
#include <algorithm>
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
46
#include <limits>
47 48 49 50 51 52 53 54 55

#if defined _MSC_VER && _MSC_VER >= 1200
#pragma warning( disable: 4244 4510 4512 4610 )
#endif

#ifdef __cplusplus
extern "C" {
#endif

56 57
#include "ffmpeg_codecs.hpp"

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
58 59
#include <libavutil/mathematics.h>

60 61 62 63 64 65
#ifdef WIN32
  #define HAVE_FFMPEG_SWSCALE 1
  #include <libavcodec/avcodec.h>
  #include <libswscale/swscale.h>
#else

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
66 67 68 69
#ifndef HAVE_FFMPEG_SWSCALE
    #error "libswscale is necessary to build the newer OpenCV ffmpeg wrapper"
#endif
    
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
// if the header path is not specified explicitly, let's deduce it
#if !defined HAVE_FFMPEG_AVCODEC_H && !defined HAVE_LIBAVCODEC_AVCODEC_H

#if defined(HAVE_GENTOO_FFMPEG)
  #define HAVE_LIBAVCODEC_AVCODEC_H 1
  #if defined(HAVE_FFMPEG_SWSCALE)
    #define HAVE_LIBSWSCALE_SWSCALE_H 1
  #endif
#elif defined HAVE_FFMPEG
  #define HAVE_FFMPEG_AVCODEC_H 1
  #if defined(HAVE_FFMPEG_SWSCALE)
    #define HAVE_FFMPEG_SWSCALE_H 1
  #endif
#endif

#endif

#if defined(HAVE_FFMPEG_AVCODEC_H)
  #include <ffmpeg/avcodec.h>
#endif
#if defined(HAVE_FFMPEG_SWSCALE_H)
  #include <ffmpeg/swscale.h>
#endif

#if defined(HAVE_LIBAVCODEC_AVCODEC_H)
  #include <libavcodec/avcodec.h>
#endif
#if defined(HAVE_LIBSWSCALE_SWSCALE_H)
  #include <libswscale/swscale.h>
#endif

#endif

#ifdef __cplusplus
}
#endif

#if defined _MSC_VER && _MSC_VER >= 1200
#pragma warning( default: 4244 4510 4512 4610 )
#endif

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

/* PIX_FMT_RGBA32 macro changed in newer ffmpeg versions */
#ifndef PIX_FMT_RGBA32
#define PIX_FMT_RGBA32 PIX_FMT_RGB32
#endif

122
#define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )
123

124 125 126 127 128
#if defined WIN32 || defined _WIN32
    #include <windows.h>
#elif defined __linux__ || defined __APPLE__
    #include <unistd.h>
    #include <stdio.h>
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
129
    #include <sys/types.h>
130 131 132
    #include <sys/sysctl.h>
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
133 134 135 136 137 138 139 140 141 142
#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

143 144
int get_number_of_cpus(void)
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
145 146 147
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
    return 1;
#elif defined WIN32 || defined _WIN32
148 149
    SYSTEM_INFO sysinfo;
    GetSystemInfo( &sysinfo );
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
150

151 152 153 154 155 156
    return (int)sysinfo.dwNumberOfProcessors;
#elif defined __linux__
    return (int)sysconf( _SC_NPROCESSORS_ONLN );
#elif defined __APPLE__
    int numCPU=0;
    int mib[4];
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
157 158 159
    size_t len = sizeof(numCPU);

    // set the mib for hw.ncpu
160 161
    mib[0] = CTL_HW;
    mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
162 163

    // get the number of CPUs from the system
164
    sysctl(mib, 2, &numCPU, &len, NULL, 0);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
165 166

    if( numCPU < 1 )
167 168 169
    {
        mib[1] = HW_NCPU;
        sysctl( mib, 2, &numCPU, &len, NULL, 0 );
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
170

171 172 173 174 175 176 177 178 179 180 181
        if( numCPU < 1 )
            numCPU = 1;
    }

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


182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
struct Image_FFMPEG
{
    unsigned char* data;
    int step;
    int width;
    int height;
    int cn;
};


inline void _opencv_ffmpeg_free(void** ptr)
{
    if(*ptr) free(*ptr);
    *ptr = 0;
}


struct CvCapture_FFMPEG
{
    bool open( const char* filename );
    void close();

    double getProperty(int);
    bool setProperty(int, double);
    bool grabFrame();
    bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);

    void init();
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

    void    seek(int64_t frame_number);
    void    seek(double sec);
    bool 	slowSeek( int framenumber );

    int64_t get_total_frames();
    double  get_duration_sec();
    double  get_fps();
    int     get_bitrate();

    double  r2d(AVRational r);
    int64_t dts_to_frame_number(int64_t dts);
    double  dts_to_sec(int64_t dts);

    AVFormatContext * ic;
    AVCodec         * avcodec;
    int               video_stream;
    AVStream        * video_st;
    AVFrame         * picture;        
    AVFrame           rgb_picture;    
    int64_t           picture_pts;

    AVPacket          packet;
    Image_FFMPEG      frame;
234
    struct SwsContext *img_convert_ctx;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
235 236 237 238

    int64_t frame_number, first_frame_number;

    double eps_zero;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/*
   '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;
};

void CvCapture_FFMPEG::init()
{
    ic = 0;
    video_stream = -1;
    video_st = 0;
    picture = 0;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
255 256
    picture_pts = AV_NOPTS_VALUE_;
    first_frame_number = -1;
257 258 259
    memset( &rgb_picture, 0, sizeof(rgb_picture) );
    memset( &frame, 0, sizeof(frame) );
    filename = 0;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
260 261
    memset(&packet, 0, sizeof(packet));
    av_init_packet(&packet);
262
    img_convert_ctx = 0;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
263 264 265 266

    avcodec = 0;
    frame_number = 0;
    eps_zero = 0.000025;
267 268 269 270 271
}


void CvCapture_FFMPEG::close()
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
272 273 274 275 276 277
    if( img_convert_ctx )
    {
        sws_freeContext(img_convert_ctx);
        img_convert_ctx = 0;
    }
    
278
    if( picture )
279
        av_free(picture);
280 281 282 283 284

    if( video_st )
    {
#if LIBAVFORMAT_BUILD > 4628
        avcodec_close( video_st->codec );
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
285

286
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
287 288
        avcodec_close( &(video_st->codec) );

289 290 291 292 293 294
#endif
        video_st = NULL;
    }

    if( ic )
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
295 296 297 298 299 300
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
		av_close_input_file(ic);
#else
		avformat_close_input(&ic);
#endif

301 302 303 304 305 306 307 308 309 310 311 312
        ic = NULL;
    }

    if( rgb_picture.data[0] )
    {
        free( rgb_picture.data[0] );
        rgb_picture.data[0] = 0;
    }

    // free last packet if exist
    if (packet.data) {
        av_free_packet (&packet);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
313
        packet.data = NULL;
314 315 316 317 318 319 320
    }

    init();
}


#ifndef AVSEEK_FLAG_FRAME
321
#define AVSEEK_FLAG_FRAME 0
322
#endif
Andrey Morozov's avatar
Andrey Morozov committed
323
#ifndef AVSEEK_FLAG_ANY
324
#define AVSEEK_FLAG_ANY 1
325
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

static void icvInitFFMPEG_internal()
{
    static volatile bool initialized = false;
    if( !initialized )
    {
    #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
        avformat_network_init();
    #endif

        /* register all codecs, demux and protocols */
        av_register_all();

        av_log_set_level(AV_LOG_ERROR);
        
        initialized = true;
    }
}
344 345 346

bool CvCapture_FFMPEG::open( const char* _filename )
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
347 348
    icvInitFFMPEG_internal();
    
349 350 351 352
    unsigned i;
    bool valid = false;

    close();
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
353 354 355 356
    
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    int err = avformat_open_input(&ic, _filename, NULL, NULL);
#else
357
    int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
358 359
#endif    
    
360
    if (err < 0) {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
361 362
        CV_WARN("Error opening file");
        goto exit_func;
363
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
364 365 366 367 368 369
    err =
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 3, 0)
    avformat_find_stream_info(ic, NULL);
#else
    av_find_stream_info(ic);
#endif
370
    if (err < 0) {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
371 372
        CV_WARN("Could not find codec parameters");
        goto exit_func;
373
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
374 375
    for(i = 0; i < ic->nb_streams; i++)
    {
376 377 378 379 380 381
#if LIBAVFORMAT_BUILD > 4628
        AVCodecContext *enc = ic->streams[i]->codec;
#else
        AVCodecContext *enc = &ic->streams[i]->codec;
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
382
#ifdef FF_API_THREAD_INIT
383
        avcodec_thread_init(enc, get_number_of_cpus());
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
384 385 386
#else
        enc->thread_count = get_number_of_cpus();
#endif
387

388 389 390
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
391

392
        if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0) {
393
            AVCodec *codec = avcodec_find_decoder(enc->codec_id);
394
            if (!codec ||
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
395 396 397 398 399 400 401
#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
                avcodec_open2(enc, codec, NULL)
#else
                avcodec_open(enc, codec)
#endif
                < 0) goto exit_func;

402 403 404 405 406
            video_stream = i;
            video_st = ic->streams[i];
            picture = avcodec_alloc_frame();

            rgb_picture.data[0] = (uint8_t*)malloc(
407 408
                    avpicture_get_size( PIX_FMT_BGR24,
                                        enc->width, enc->height ));
409
            avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
410
                            PIX_FMT_BGR24, enc->width, enc->height );
411 412 413 414 415 416 417 418 419 420 421 422

            frame.width = enc->width;
            frame.height = enc->height;
            frame.cn = 3;
            frame.step = rgb_picture.linesize[0];
            frame.data = rgb_picture.data[0];
            break;
        }
    }

    if(video_stream >= 0) valid = true;

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
423
exit_func:
424 425 426 427 428 429 430 431 432 433 434 435 436

    if( !valid )
        close();

    return valid;
}


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

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
437 438
    int count_errs = 0;
    const int max_number_of_attempts = 1 << 16;
439

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
440
    if( !ic || !video_st )  return false;
441

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
442 443 444
    av_free_packet (&packet);
    
    picture_pts = AV_NOPTS_VALUE_;
445 446

    // get the next frame
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
447 448
    while (!valid)
    {
449
        int ret = av_read_frame(ic, &packet);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
450 451 452 453 454 455
        if (ret == AVERROR(EAGAIN)) continue;

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

        if( packet.stream_index != video_stream )
        {
456
            av_free_packet (&packet);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
457 458 459
            count_errs++;
            if (count_errs > max_number_of_attempts)
                break;
460 461
            continue;
        }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
462 463 464 465 466 467 468 469 470 471 472 473 474
        
        // 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
475

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489
        // Did we get a video frame?
        if(got_picture)
        {
            //picture_pts = picture->best_effort_timestamp;
            if( picture_pts == AV_NOPTS_VALUE_ )
                picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
            frame_number++;
            valid = true;
        }
        else
        {
            count_errs++;
            if (count_errs > max_number_of_attempts)
                break;
490
        }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
491 492

        av_free_packet (&packet);
493 494
    }

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
495 496 497
    if( valid && first_frame_number < 0 )
        first_frame_number = dts_to_frame_number(picture_pts);
    
498 499 500 501 502 503 504 505 506 507
    // 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;

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
                   video_st->codec->width, video_st->codec->height);

    if( img_convert_ctx == NULL ||
        frame.width != video_st->codec->width ||
        frame.height != video_st->codec->height )
    {
        if( img_convert_ctx )
            sws_freeContext(img_convert_ctx);
        
        frame.width = video_st->codec->width;
        frame.height = video_st->codec->height;

        img_convert_ctx = sws_getCachedContext(
                NULL,
                video_st->codec->width, video_st->codec->height,
                video_st->codec->pix_fmt,
                video_st->codec->width, video_st->codec->height,
                PIX_FMT_BGR24,
                SWS_BICUBIC,
                NULL, NULL, NULL
                );

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

    sws_scale(
            img_convert_ctx,
            picture->data,
            picture->linesize,
            0, video_st->codec->height,
            rgb_picture.data,
            rgb_picture.linesize
            );

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    *data = frame.data;
    *step = frame.step;
    *width = frame.width;
    *height = frame.height;
    *cn = frame.cn;

    return true;
}


double CvCapture_FFMPEG::getProperty( int property_id )
{
    if( !video_st ) return 0;

    switch( property_id )
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
560 561
    case CV_FFMPEG_CAP_PROP_POS_MSEC:
        return 1000.0*(double)frame_number/get_fps();
562
    case CV_FFMPEG_CAP_PROP_POS_FRAMES:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
563
        return (double)frame_number;
564
    case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
565
        return r2d(ic->streams[video_stream]->time_base);
566
    case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
567
        return (double)get_total_frames();
568 569 570 571 572 573
    case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
        return (double)frame.width;
    case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
        return (double)frame.height;
    case CV_FFMPEG_CAP_PROP_FPS:
#if LIBAVCODEC_BUILD > 4753
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
574
        return av_q2d(video_st->r_frame_rate);
575 576
#else
        return (double)video_st->codec.frame_rate
577
                / (double)video_st->codec.frame_rate_base;
578 579 580 581 582 583 584
#endif
    case CV_FFMPEG_CAP_PROP_FOURCC:
#if LIBAVFORMAT_BUILD > 4628
        return (double)video_st->codec->codec_tag;
#else
        return (double)video_st->codec.codec_tag;
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
585
    default:
586
        break;
587
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
588

589 590 591
    return 0;
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
592 593 594 595 596 597
double CvCapture_FFMPEG::r2d(AVRational r)
{
    return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}

double CvCapture_FFMPEG::get_duration_sec()
598
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
599 600 601
    double sec = (double)ic->duration / (double)AV_TIME_BASE;

    if (sec < eps_zero)
602
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
603
        sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
604
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
605 606

    if (sec < eps_zero)
607
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
608
        sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
609
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
610 611

    return sec;
612 613
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
614
int CvCapture_FFMPEG::get_bitrate()
615
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
616 617 618 619 620 621 622 623 624 625 626
    return ic->bit_rate;
}

double CvCapture_FFMPEG::get_fps()
{
    double fps = r2d(ic->streams[video_stream]->r_frame_rate);

#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
    if (fps < eps_zero)
    {
        fps = r2d(ic->streams[video_stream]->avg_frame_rate);
627
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
#endif    

    if (fps < eps_zero)
    {
        fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
    }

    return fps;
}

int64_t CvCapture_FFMPEG::get_total_frames()
{
    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);
}

double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
{
    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;
    
    // 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
    if( first_frame_number < 0 )
669
        grabFrame();
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
670 671 672 673 674 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 713 714 715
    
    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);
        av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
        avcodec_flush_buffers(ic->streams[video_stream]->codec);
        if( _frame_number > 0 )
        {
            grabFrame();
            
            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);
                
                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;
        }
716
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
717 718 719 720 721
}

void CvCapture_FFMPEG::seek(double sec)
{
    seek((int64_t)(sec * get_fps() + 0.5));
722
}
723 724 725 726 727 728 729 730 731 732 733 734 735 736

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

    switch( property_id )
    {
    case CV_FFMPEG_CAP_PROP_POS_MSEC:
    case CV_FFMPEG_CAP_PROP_POS_FRAMES:
    case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
        {
            switch( property_id )
            {
            case CV_FFMPEG_CAP_PROP_POS_FRAMES:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
737
                seek((int64_t)value);
738 739 740
                break;

            case CV_FFMPEG_CAP_PROP_POS_MSEC:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
741
                seek(value/1000.0);
742 743 744
                break;

            case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
745
                seek((int64_t)(value*ic->duration));
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
                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,
764
               double fps, int width, int height, bool isColor );
765 766 767 768 769
    void close();
    bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );

    void init();

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
770 771
    AVOutputFormat 	* fmt;
    AVFormatContext * oc;
772 773 774 775 776 777 778 779 780
    uint8_t         * outbuf;
    uint32_t          outbuf_size;
    FILE            * outfile;
    AVFrame         * picture;
    AVFrame         * input_picture;
    uint8_t         * picbuf;
    AVStream        * video_st;
    int               input_pix_fmt;
    Image_FFMPEG      temp_image;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
781 782
    int               frame_width, frame_height;
    bool              ok;
783 784 785 786 787
    struct SwsContext *img_convert_ctx;
};

static const char * icvFFMPEGErrStr(int err)
{
788
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
789
    switch(err) {
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    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;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
818
    }
819
#else
820 821
    switch(err) {
    case AVERROR_NUMEXPECTED:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
822
        return "Incorrect filename syntax";
823
    case AVERROR_INVALIDDATA:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
824
        return "Invalid data in header";
825
    case AVERROR_NOFMT:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
826
        return "Unknown format";
827
    case AVERROR_IO:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
828
        return "I/O error occurred";
829
    case AVERROR_NOMEM:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
830
        return "Memory allocation error";
831
    default:
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
832
        break;
833
    }
834 835
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
836
    return "Unspecified error";
837 838 839 840
}

/* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
extern "C" {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
841
    enum CodecID codec_get_bmp_id(unsigned int tag);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
}

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;
    memset(&temp_image, 0, sizeof(temp_image));
    img_convert_ctx = 0;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
858 859
    frame_width = frame_height = 0;
    ok = false;
860 861 862 863 864 865 866 867
}

/**
 * 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)
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    AVFrame * picture;
    uint8_t * picture_buf;
    int size;

    picture = avcodec_alloc_frame();
    if (!picture)
        return NULL;
    size = avpicture_get_size( (PixelFormat) pix_fmt, width, height);
    if(alloc){
        picture_buf = (uint8_t *) malloc(size);
        if (!picture_buf)
        {
            av_free(picture);
            return NULL;
        }
        avpicture_fill((AVPicture *)picture, picture_buf,
884
                       (PixelFormat) pix_fmt, width, height);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
885 886 887 888
    }
    else {
    }
    return picture;
889 890 891 892
}

/* add a video output stream to the container */
static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
893 894 895
                                             CodecID codec_id,
                                             int w, int h, int bitrate,
                                             double fps, int pixel_format)
896
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
897 898 899 900
    AVCodecContext *c;
    AVStream *st;
    int frame_rate, frame_rate_base;
    AVCodec *codec;
901

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
902 903 904 905 906
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
    st = avformat_new_stream(oc, 0);
#else
    st = av_new_stream(oc, 0);
#endif
907

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
908 909 910 911
    if (!st) {
        CV_WARN("Could not allocate stream");
        return NULL;
    }
912 913

#if LIBAVFORMAT_BUILD > 4628
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
914
    c = st->codec;
915
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
916
    c = &(st->codec);
917 918 919
#endif

#if LIBAVFORMAT_BUILD > 4621
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
920
    c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
921
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
922
    c->codec_id = oc->oformat->video_codec;
923 924
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
925 926 927
    if(codec_id != CODEC_ID_NONE){
        c->codec_id = codec_id;
    }
928 929

    //if(codec_tag) c->codec_tag=codec_tag;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
930
    codec = avcodec_find_encoder(c->codec_id);
931

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
932
    c->codec_type = AVMEDIA_TYPE_VIDEO;
933

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
934 935 936 937 938
    /* 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;
939

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
940 941 942
    // took advice from
    // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
    c->qmin = 3;
943

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
944 945 946 947 948 949
    /* 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,
950 951
       timebase should be 1/framerate and timestamp increments should be
       identically 1. */
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
952 953 954 955 956 957
    frame_rate=(int)(fps+0.5);
    frame_rate_base=1;
    while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
        frame_rate_base*=10;
        frame_rate=(int)(fps*frame_rate_base + 0.5);
    }
958 959 960
#if LIBAVFORMAT_BUILD > 4752
    c->time_base.den = frame_rate;
    c->time_base.num = frame_rate_base;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
961 962 963
    /* adjust time base for supported framerates */
    if(codec && codec->supported_framerates){
        const AVRational *p= codec->supported_framerates;
964
        AVRational req = {frame_rate, frame_rate_base};
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
965 966 967 968 969 970 971 972 973 974 975 976 977
        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;
            }
        }
        c->time_base.den= best->num;
        c->time_base.num= best->den;
    }
978
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
979 980
    c->frame_rate = frame_rate;
    c->frame_rate_base = frame_rate_base;
981 982
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
983 984
    c->gop_size = 12; /* emit one intra frame every twelve frames at most */
    c->pix_fmt = (PixelFormat) pixel_format;
985

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
986
    if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
987 988 989 990 991 992
        c->max_b_frames = 2;
    }
    if (c->codec_id == CODEC_ID_MPEG1VIDEO || c->codec_id == CODEC_ID_MSMPEG4V3){
        /* needed to avoid using macroblocks in which some coeffs overflow
           this doesnt happen with normal video, it just happens here as the
           motion of the chroma plane doesnt match the luma plane */
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
993
        /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        c->mb_decision=2;
    }
#if LIBAVCODEC_VERSION_INT>0x000409
    // some formats want stream headers to be seperate
    if(oc->oformat->flags & AVFMT_GLOBALHEADER)
    {
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }
#endif

    return st;
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1007 1008
static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;

1009 1010 1011
int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture )
{
#if LIBAVFORMAT_BUILD > 4628
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1012
    AVCodecContext * c = video_st->codec;
1013
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1014
    AVCodecContext * c = &(video_st->codec);
1015
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1016 1017
    int out_size;
    int ret = 0;
1018 1019 1020 1021 1022 1023 1024

    if (oc->oformat->flags & AVFMT_RAWPICTURE) {
        /* raw video case. The API will change slightly in the near
           futur for that */
        AVPacket pkt;
        av_init_packet(&pkt);

1025 1026 1027
#ifndef PKT_FLAG_KEY
#define PKT_FLAG_KEY AV_PKT_FLAG_KEY
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1028 1029

        pkt.flags |= PKT_FLAG_KEY;
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
        pkt.stream_index= video_st->index;
        pkt.data= (uint8_t *)picture;
        pkt.size= sizeof(AVPicture);

        ret = av_write_frame(oc, &pkt);
    } else {
        /* encode the image */
        out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
        /* if zero size, it means the image was buffered */
        if (out_size > 0) {
            AVPacket pkt;
            av_init_packet(&pkt);

#if LIBAVFORMAT_BUILD > 4752
1044
            if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1045
				pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1046
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1047
            pkt.pts = c->coded_frame->pts;
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
#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);
        } else {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1058
            ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1059 1060
        }
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1061
    return ret;
1062 1063 1064 1065 1066
}

/// write a frame with FFMPEG
bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1067 1068 1069 1070 1071 1072
    bool ret = false;
    
    if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
        return false;
    width = frame_width;
    height = frame_height;
1073

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1074
    // typecast from opaque data type to implemented struct
1075 1076 1077
#if LIBAVFORMAT_BUILD > 4628
    AVCodecContext *c = video_st->codec;
#else
1078
    AVCodecContext *c = &(video_st->codec);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
#endif

#if LIBAVFORMAT_BUILD < 5231
    // It is not needed in the latest versions of the ffmpeg
    if( c->codec_id == CODEC_ID_RAWVIDEO && origin != 1 )
    {
        if( !temp_image.data )
        {
            temp_image.step = (width*cn + 3) & -4;
            temp_image.width = width;
            temp_image.height = height;
            temp_image.cn = cn;
Alexander Shishkov's avatar
Alexander Shishkov committed
1091
            temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1092 1093 1094 1095 1096 1097
        }
        for( int y = 0; y < height; y++ )
            memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
        data = temp_image.data;
        step = temp_image.step;
    }
Alexander Shishkov's avatar
Alexander Shishkov committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
#else
    if( width*cn != step )
    {
        if( !temp_image.data )
        {
            temp_image.step = width*cn;
            temp_image.width = width;
            temp_image.height = height;
            temp_image.cn = cn;
            temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
        }
        if (origin == 1)
            for( int y = 0; y < height; y++ )
                memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
        else
            for( int y = 0; y < height; y++ )
                memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
        data = temp_image.data;
        step = temp_image.step;
    }
1118 1119 1120 1121 1122 1123 1124 1125
#endif

    // check parameters
    if (input_pix_fmt == PIX_FMT_BGR24) {
        if (cn != 3) {
            return false;
        }
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1126
    else if (input_pix_fmt == PIX_FMT_GRAY8) {
1127 1128 1129 1130
        if (cn != 1) {
            return false;
        }
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1131
    else {
1132 1133 1134
        assert(false);
    }

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1135 1136 1137 1138
    if ( c->pix_fmt != input_pix_fmt ) {
        assert( input_picture );
        // let input_picture point to the raw data buffer of 'image'
        avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1139
                       (PixelFormat)input_pix_fmt, width, height);
1140

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
        if( !img_convert_ctx )
        {
            img_convert_ctx = sws_getContext(width,
                                             height,
                                             (PixelFormat)input_pix_fmt,
                                             c->width,
                                             c->height,
                                             c->pix_fmt,
                                             SWS_BICUBIC,
                                             NULL, NULL, NULL);
            if( !img_convert_ctx )
                return false;
        }
1154 1155 1156 1157 1158 1159

        if ( sws_scale(img_convert_ctx, input_picture->data,
                       input_picture->linesize, 0,
                       height,
                       picture->data, picture->linesize) < 0 )
            return false;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1160 1161 1162
    }
    else{
        avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1163
                       (PixelFormat)input_pix_fmt, width, height);
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1164
    }
1165

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1166
    ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1167

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1168
    return ret;
1169 1170 1171 1172 1173
}

/// close video output stream and free associated memory
void CvVideoWriter_FFMPEG::close()
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1174
    unsigned i;
1175

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1176 1177 1178 1179 1180 1181 1182 1183
    // nothing to do if already released
    if ( !picture )
        return;
    
    /* 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?
1184

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    /* write the trailer, if any */
    if(ok && oc)
    {
        if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
        {
            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);
    }
    
    if( img_convert_ctx )
    {
        sws_freeContext(img_convert_ctx);
        img_convert_ctx = 0;
    }
1205

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1206
    // free pictures
1207
#if LIBAVFORMAT_BUILD > 4628
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1208
    if( video_st->codec->pix_fmt != input_pix_fmt)
1209
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1210
    if( video_st->codec.pix_fmt != input_pix_fmt)
1211
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1212 1213 1214 1215 1216 1217
    {
        if(picture->data[0])
            free(picture->data[0]);
        picture->data[0] = 0;
    }
    av_free(picture);
1218

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1219 1220
    if (input_picture)
        av_free(input_picture);
1221

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1222
    /* close codec */
1223
#if LIBAVFORMAT_BUILD > 4628
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1224
    avcodec_close(video_st->codec);
1225
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1226
    avcodec_close(&(video_st->codec));
1227 1228
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1229
    av_free(outbuf);
1230

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1231 1232 1233 1234 1235 1236
    /* free the streams */
    for(i = 0; i < oc->nb_streams; i++)
    {
        av_freep(&oc->streams[i]->codec);
        av_freep(&oc->streams[i]);
    }
1237

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1238 1239 1240
    if (!(fmt->flags & AVFMT_NOFILE))
    {
        /* close the output file */
1241

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1242
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1243
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1244
        url_fclose(oc->pb);
1245
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1246 1247 1248 1249
        url_fclose(&oc->pb);
#endif
#else
        avio_close(oc->pb);
1250 1251
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1252
    }
1253

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1254 1255
    /* free the stream */
    av_free(oc);
1256

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1257 1258 1259 1260
    if( temp_image.data )
    {
        free(temp_image.data);
        temp_image.data = 0;
1261
    }
1262

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1263 1264
    init();
}
1265

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1266 1267 1268 1269 1270 1271 1272 1273 1274
/// 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 )
{
    icvInitFFMPEG_internal();
    
    CodecID codec_id = CODEC_ID_NONE;
    int err, codec_pix_fmt;
    double bitrate_scale = 1;
1275

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1276
    close();
1277

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
    // check arguments
    if( !filename )
        return false;
    if(fps <= 0)
        return false;
    
    // 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;
1291

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1292
    /* auto detect the output format from the name and fourcc code. */
1293

1294
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1295
    fmt = av_guess_format(NULL, filename, NULL);
1296
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1297
    fmt = guess_format(NULL, filename, NULL);
1298
#endif
1299

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1300 1301
    if (!fmt)
        return false;
1302

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1303 1304 1305 1306 1307 1308 1309
    /* determine optimal pixel format */
    if (is_color) {
        input_pix_fmt = PIX_FMT_BGR24;
    }
    else {
        input_pix_fmt = PIX_FMT_GRAY8;
    }
1310

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1311
    /* Lookup codec_id for given fourcc */
1312
#if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1313 1314
    if( (codec_id = codec_get_bmp_id( fourcc )) == CODEC_ID_NONE )
        return false;
1315
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1316 1317 1318
    const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
    if( (codec_id = av_codec_get_id(tags, fourcc)) == CODEC_ID_NONE )
        return false;
1319 1320
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1321
    // alloc memory for context
1322
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1323
    oc = avformat_alloc_context();
1324
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1325
    oc = av_alloc_format_context();
1326
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1327
    assert (oc);
1328

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1329 1330 1331
    /* set file name */
    oc->oformat = fmt;
    snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1332

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1333 1334
    /* set some options */
    oc->max_delay = (int)(0.7*AV_TIME_BASE);  /* This reduces buffer underrun warnings with MPEG */
1335

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1336 1337
    // set a few optimal pixel formats for lossless codecs of interest..
    switch (codec_id) {
1338
#if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1339 1340 1341 1342
    case CODEC_ID_JPEGLS:
        // BGR24 or GRAY8 depending on is_color...
        codec_pix_fmt = input_pix_fmt;
        break;
1343
#endif
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
    case CODEC_ID_HUFFYUV:
        codec_pix_fmt = PIX_FMT_YUV422P;
        break;
    case CODEC_ID_MJPEG:
    case CODEC_ID_LJPEG:
        codec_pix_fmt = PIX_FMT_YUVJ420P;
        bitrate_scale = 3;
        break;
    case CODEC_ID_RAWVIDEO:
        codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
                        input_pix_fmt == PIX_FMT_GRAY16LE ||
                        input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
        break;
    default:
        // good for lossy formats, MPEG, etc.
        codec_pix_fmt = PIX_FMT_YUV420P;
        break;
    }
    
    double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1364

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1365 1366 1367 1368
    // 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);
1369

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1370 1371 1372 1373 1374 1375 1376
    /* 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
1377

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1378 1379 1380
#if 0
#if FF_API_DUMP_FORMAT
    dump_format(oc, 0, filename, 1);
1381
#else
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1382 1383
    av_dump_format(oc, 0, filename, 1);
#endif
1384 1385
#endif

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1386 1387 1388 1389
    /* 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;
1390
    }
1391

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1392 1393
    AVCodec *codec;
    AVCodecContext *c;
1394

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1395 1396 1397 1398 1399
#if LIBAVFORMAT_BUILD > 4628
    c = (video_st->codec);
#else
    c = &(video_st->codec);
#endif
1400

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
    c->codec_tag = fourcc;
    /* find the video encoder */
    codec = avcodec_find_encoder(c->codec_id);
    if (!codec) {
        fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
        #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
                AVERROR_ENCODER_NOT_FOUND
        #else
                -1
        #endif
                ));
        return false;
1413
    }
1414

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1415 1416 1417 1418 1419
    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;
1420

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    /* 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) {
        fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
        return false;
1431
    }
1432

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1433
    outbuf = NULL;
1434

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1435 1436 1437 1438 1439
    if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
        /* 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);
1440
    }
1441

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1442 1443
    bool need_color_convert;
    need_color_convert = (c->pix_fmt != input_pix_fmt);
1444

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1445 1446 1447 1448
    /* allocate the encoded raw picture */
    picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
    if (!picture) {
        return false;
1449
    }
1450

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1451 1452 1453 1454 1455 1456 1457 1458
    /* 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;
1459
        }
1460 1461
    }

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    /* 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;
        }
1472
    }
1473

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1474 1475 1476 1477 1478 1479
#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 );
#endif    
1480
    
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1481
    if(err < 0)
1482
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1483 1484 1485
        close();
        remove(filename);
        return false;
1486
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1487 1488 1489 1490
    frame_width = width;
    frame_height = height;
    ok = true;
    return true;
1491 1492 1493 1494
}



Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1495
CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1496
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1497 1498 1499 1500 1501 1502 1503
    CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
    capture->init();
    if( capture->open( filename ))
        return capture;
    capture->close();
    free(capture);
    return 0;
1504 1505
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1506 1507

void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1508
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1509
    if( capture && *capture )
1510
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1511 1512 1513
        (*capture)->close();
        free(*capture);
        *capture = 0;
1514 1515 1516
    }
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1517
int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1518
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1519
    return capture->setProperty(prop_id, value);
1520 1521
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1522
double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1523
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1524
    return capture->getProperty(prop_id);
1525 1526
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1527
int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1528
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1529
    return capture->grabFrame();
1530
}
1531

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1532
int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1533
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1534
    return capture->retrieveFrame(0, data, step, width, height, cn);
1535 1536
}

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1537 1538
CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
                                                  int width, int height, int isColor )
1539
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1540 1541 1542 1543 1544 1545 1546
    CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
    writer->init();
    if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
        return writer;
    writer->close();
    free(writer);
    return 0;
1547 1548 1549
}


Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1550 1551 1552
void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
{
    if( writer && *writer )
1553
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1554 1555 1556
        (*writer)->close();
        free(*writer);
        *writer = 0;
1557 1558 1559 1560
    }
}


Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1561 1562 1563
int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
                         const unsigned char* data, int step,
                         int width, int height, int cn, int origin)
1564
{
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
1565
    return writer->writeFrame(data, step, width, height, cn, origin);
1566 1567
}