Add OpenCV source code
[platform/upstream/opencv.git] / modules / highgui / src / cap_ffmpeg_impl.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "cap_ffmpeg_api.hpp"
44 #if !(defined(WIN32) || defined(_WIN32) || defined(WINCE))
45 # include <pthread.h>
46 #endif
47 #include <assert.h>
48 #include <algorithm>
49 #include <limits>
50
51 #define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )
52
53 #if defined _MSC_VER && _MSC_VER >= 1200
54 #pragma warning( disable: 4244 4510 4512 4610 )
55 #endif
56
57 #ifdef __GNUC__
58 #  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59 #endif
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #include "ffmpeg_codecs.hpp"
66
67 #include <libavutil/mathematics.h>
68
69 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
70   #include <libavutil/opt.h>
71 #endif
72
73 #ifdef WIN32
74   #define HAVE_FFMPEG_SWSCALE 1
75   #include <libavcodec/avcodec.h>
76   #include <libswscale/swscale.h>
77 #else
78
79 #ifndef HAVE_FFMPEG_SWSCALE
80     #error "libswscale is necessary to build the newer OpenCV ffmpeg wrapper"
81 #endif
82
83 // if the header path is not specified explicitly, let's deduce it
84 #if !defined HAVE_FFMPEG_AVCODEC_H && !defined HAVE_LIBAVCODEC_AVCODEC_H
85
86 #if defined(HAVE_GENTOO_FFMPEG)
87   #define HAVE_LIBAVCODEC_AVCODEC_H 1
88   #if defined(HAVE_FFMPEG_SWSCALE)
89     #define HAVE_LIBSWSCALE_SWSCALE_H 1
90   #endif
91 #elif defined HAVE_FFMPEG
92   #define HAVE_FFMPEG_AVCODEC_H 1
93   #if defined(HAVE_FFMPEG_SWSCALE)
94     #define HAVE_FFMPEG_SWSCALE_H 1
95   #endif
96 #endif
97
98 #endif
99
100 #if defined(HAVE_FFMPEG_AVCODEC_H)
101   #include <ffmpeg/avcodec.h>
102 #endif
103 #if defined(HAVE_FFMPEG_SWSCALE_H)
104   #include <ffmpeg/swscale.h>
105 #endif
106
107 #if defined(HAVE_LIBAVCODEC_AVCODEC_H)
108   #include <libavcodec/avcodec.h>
109 #endif
110 #if defined(HAVE_LIBSWSCALE_SWSCALE_H)
111   #include <libswscale/swscale.h>
112 #endif
113
114 #endif
115
116 #ifdef __cplusplus
117 }
118 #endif
119
120 #if defined _MSC_VER && _MSC_VER >= 1200
121 #pragma warning( default: 4244 4510 4512 4610 )
122 #endif
123
124 #ifdef NDEBUG
125 #define CV_WARN(message)
126 #else
127 #define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
128 #endif
129
130 /* PIX_FMT_RGBA32 macro changed in newer ffmpeg versions */
131 #ifndef PIX_FMT_RGBA32
132 #define PIX_FMT_RGBA32 PIX_FMT_RGB32
133 #endif
134
135
136 #if defined WIN32 || defined _WIN32
137     #include <windows.h>
138 #elif defined __linux__ || defined __APPLE__
139     #include <unistd.h>
140     #include <stdio.h>
141     #include <sys/types.h>
142 #if defined __APPLE__
143     #include <sys/sysctl.h>
144 #endif
145 #endif
146
147 #ifndef MIN
148 #define MIN(a, b) ((a) < (b) ? (a) : (b))
149 #endif
150
151 #if defined(__APPLE__)
152 #define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
153 #else
154 #define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
155 #endif
156
157 #ifndef AVERROR_EOF
158 #define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
159 #endif
160
161 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
162 #  define CV_CODEC_ID AVCodecID
163 #  define CV_CODEC(name) AV_##name
164 #else
165 #  define CV_CODEC_ID CodecID
166 #  define CV_CODEC(name) name
167 #endif
168
169 static int get_number_of_cpus(void)
170 {
171 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
172     return 1;
173 #elif defined WIN32 || defined _WIN32
174     SYSTEM_INFO sysinfo;
175     GetSystemInfo( &sysinfo );
176
177     return (int)sysinfo.dwNumberOfProcessors;
178 #elif defined __linux__
179     return (int)sysconf( _SC_NPROCESSORS_ONLN );
180 #elif defined __APPLE__
181     int numCPU=0;
182     int mib[4];
183     size_t len = sizeof(numCPU);
184
185     // set the mib for hw.ncpu
186     mib[0] = CTL_HW;
187     mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
188
189     // get the number of CPUs from the system
190     sysctl(mib, 2, &numCPU, &len, NULL, 0);
191
192     if( numCPU < 1 )
193     {
194         mib[1] = HW_NCPU;
195         sysctl( mib, 2, &numCPU, &len, NULL, 0 );
196
197         if( numCPU < 1 )
198             numCPU = 1;
199     }
200
201     return (int)numCPU;
202 #else
203     return 1;
204 #endif
205 }
206
207
208 struct Image_FFMPEG
209 {
210     unsigned char* data;
211     int step;
212     int width;
213     int height;
214     int cn;
215 };
216
217
218 inline void _opencv_ffmpeg_free(void** ptr)
219 {
220     if(*ptr) free(*ptr);
221     *ptr = 0;
222 }
223
224
225 struct CvCapture_FFMPEG
226 {
227     bool open( const char* filename );
228     void close();
229
230     double getProperty(int) const;
231     bool setProperty(int, double);
232     bool grabFrame();
233     bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
234
235     void init();
236
237     void    seek(int64_t frame_number);
238     void    seek(double sec);
239     bool    slowSeek( int framenumber );
240
241     int64_t get_total_frames() const;
242     double  get_duration_sec() const;
243     double  get_fps() const;
244     int     get_bitrate() const;
245
246     double  r2d(AVRational r) const;
247     int64_t dts_to_frame_number(int64_t dts);
248     double  dts_to_sec(int64_t dts);
249
250     AVFormatContext * ic;
251     AVCodec         * avcodec;
252     int               video_stream;
253     AVStream        * video_st;
254     AVFrame         * picture;
255     AVFrame           rgb_picture;
256     int64_t           picture_pts;
257
258     AVPacket          packet;
259     Image_FFMPEG      frame;
260     struct SwsContext *img_convert_ctx;
261
262     int64_t frame_number, first_frame_number;
263
264     double eps_zero;
265 /*
266    'filename' contains the filename of the videosource,
267    'filename==NULL' indicates that ffmpeg's seek support works
268    for the particular file.
269    'filename!=NULL' indicates that the slow fallback function is used for seeking,
270    and so the filename is needed to reopen the file on backward seeking.
271 */
272     char              * filename;
273
274 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
275     AVDictionary *dict;
276 #endif
277 };
278
279 void CvCapture_FFMPEG::init()
280 {
281     ic = 0;
282     video_stream = -1;
283     video_st = 0;
284     picture = 0;
285     picture_pts = AV_NOPTS_VALUE_;
286     first_frame_number = -1;
287     memset( &rgb_picture, 0, sizeof(rgb_picture) );
288     memset( &frame, 0, sizeof(frame) );
289     filename = 0;
290     memset(&packet, 0, sizeof(packet));
291     av_init_packet(&packet);
292     img_convert_ctx = 0;
293
294     avcodec = 0;
295     frame_number = 0;
296     eps_zero = 0.000025;
297
298 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
299     dict = NULL;
300 #endif
301 }
302
303
304 void CvCapture_FFMPEG::close()
305 {
306     if( img_convert_ctx )
307     {
308         sws_freeContext(img_convert_ctx);
309         img_convert_ctx = 0;
310     }
311
312     if( picture )
313     {
314         // FFmpeg and Libav added avcodec_free_frame in different versions.
315 #if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
316     ? CALC_FFMPEG_VERSION(54, 59, 100) : CALC_FFMPEG_VERSION(54, 28, 0))
317         avcodec_free_frame(&picture);
318 #else
319         av_free(picture);
320 #endif
321     }
322
323     if( video_st )
324     {
325 #if LIBAVFORMAT_BUILD > 4628
326         avcodec_close( video_st->codec );
327
328 #else
329         avcodec_close( &(video_st->codec) );
330
331 #endif
332         video_st = NULL;
333     }
334
335     if( ic )
336     {
337 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
338         av_close_input_file(ic);
339 #else
340         avformat_close_input(&ic);
341 #endif
342
343         ic = NULL;
344     }
345
346     if( rgb_picture.data[0] )
347     {
348         free( rgb_picture.data[0] );
349         rgb_picture.data[0] = 0;
350     }
351
352     // free last packet if exist
353     if (packet.data) {
354         av_free_packet (&packet);
355         packet.data = NULL;
356     }
357
358 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
359     if (dict != NULL)
360        av_dict_free(&dict);
361 #endif
362
363     init();
364 }
365
366
367 #ifndef AVSEEK_FLAG_FRAME
368 #define AVSEEK_FLAG_FRAME 0
369 #endif
370 #ifndef AVSEEK_FLAG_ANY
371 #define AVSEEK_FLAG_ANY 1
372 #endif
373
374 class ImplMutex
375 {
376 public:
377     ImplMutex() { init(); }
378     ~ImplMutex() { destroy(); }
379
380     void init();
381     void destroy();
382
383     void lock();
384     bool trylock();
385     void unlock();
386
387     struct Impl;
388 protected:
389     Impl* impl;
390
391 private:
392     ImplMutex(const ImplMutex&);
393     ImplMutex& operator = (const ImplMutex& m);
394 };
395
396 #if defined WIN32 || defined _WIN32 || defined WINCE
397
398 struct ImplMutex::Impl
399 {
400     void init()
401     {
402 #if (_WIN32_WINNT >= 0x0600)
403         ::InitializeCriticalSectionEx(&cs, 1000, 0);
404 #else
405         ::InitializeCriticalSection(&cs);
406 #endif
407         refcount = 1;
408     }
409     void destroy() { DeleteCriticalSection(&cs); }
410
411     void lock() { EnterCriticalSection(&cs); }
412     bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
413     void unlock() { LeaveCriticalSection(&cs); }
414
415     CRITICAL_SECTION cs;
416     int refcount;
417 };
418
419 #ifndef __GNUC__
420 static int _interlockedExchangeAdd(int* addr, int delta)
421 {
422 #if defined _MSC_VER && _MSC_VER >= 1500
423     return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
424 #else
425     return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
426 #endif
427 }
428 #endif // __GNUC__
429
430 #elif defined __APPLE__
431
432 #include <libkern/OSAtomic.h>
433
434 struct ImplMutex::Impl
435 {
436     void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
437     void destroy() { }
438
439     void lock() { OSSpinLockLock(&sl); }
440     bool trylock() { return OSSpinLockTry(&sl); }
441     void unlock() { OSSpinLockUnlock(&sl); }
442
443     OSSpinLock sl;
444     int refcount;
445 };
446
447 #elif defined __linux__ && !defined ANDROID
448
449 struct ImplMutex::Impl
450 {
451     void init() { pthread_spin_init(&sl, 0); refcount = 1; }
452     void destroy() { pthread_spin_destroy(&sl); }
453
454     void lock() { pthread_spin_lock(&sl); }
455     bool trylock() { return pthread_spin_trylock(&sl) == 0; }
456     void unlock() { pthread_spin_unlock(&sl); }
457
458     pthread_spinlock_t sl;
459     int refcount;
460 };
461
462 #else
463
464 struct ImplMutex::Impl
465 {
466     void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
467     void destroy() { pthread_mutex_destroy(&sl); }
468
469     void lock() { pthread_mutex_lock(&sl); }
470     bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
471     void unlock() { pthread_mutex_unlock(&sl); }
472
473     pthread_mutex_t sl;
474     int refcount;
475 };
476
477 #endif
478
479 void ImplMutex::init()
480 {
481     impl = (Impl*)malloc(sizeof(Impl));
482     impl->init();
483 }
484 void ImplMutex::destroy()
485 {
486     impl->destroy();
487     free(impl);
488     impl = NULL;
489 }
490 void ImplMutex::lock() { impl->lock(); }
491 void ImplMutex::unlock() { impl->unlock(); }
492 bool ImplMutex::trylock() { return impl->trylock(); }
493
494 static int LockCallBack(void **mutex, AVLockOp op)
495 {
496     ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
497     switch (op)
498     {
499         case AV_LOCK_CREATE:
500             localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
501             localMutex->init();
502             *mutex = localMutex;
503             if (!*mutex)
504                 return 1;
505         break;
506
507         case AV_LOCK_OBTAIN:
508             localMutex->lock();
509         break;
510
511         case AV_LOCK_RELEASE:
512             localMutex->unlock();
513         break;
514
515         case AV_LOCK_DESTROY:
516             localMutex->destroy();
517             free(localMutex);
518             localMutex = NULL;
519             *mutex = NULL;
520         break;
521     }
522     return 0;
523 }
524
525 static ImplMutex _mutex;
526 static bool _initialized = false;
527
528 class InternalFFMpegRegister
529 {
530 public:
531     InternalFFMpegRegister()
532     {
533         _mutex.lock();
534         if (!_initialized)
535         {
536     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
537             avformat_network_init();
538     #endif
539
540             /* register all codecs, demux and protocols */
541             av_register_all();
542
543             /* register a callback function for synchronization */
544             av_lockmgr_register(&LockCallBack);
545
546             av_log_set_level(AV_LOG_ERROR);
547
548             _initialized = true;
549         }
550         _mutex.unlock();
551     }
552
553     ~InternalFFMpegRegister()
554     {
555         _initialized = false;
556         av_lockmgr_register(NULL);
557     }
558 };
559
560 static InternalFFMpegRegister _init;
561
562 bool CvCapture_FFMPEG::open( const char* _filename )
563 {
564     unsigned i;
565     bool valid = false;
566
567     close();
568
569 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
570     av_dict_set(&dict, "rtsp_transport", "tcp", 0);
571     int err = avformat_open_input(&ic, _filename, NULL, NULL);
572 #else
573     int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
574 #endif
575
576     if (err < 0)
577     {
578         CV_WARN("Error opening file");
579         goto exit_func;
580     }
581     err =
582 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
583     avformat_find_stream_info(ic, NULL);
584 #else
585     av_find_stream_info(ic);
586 #endif
587     if (err < 0)
588     {
589         CV_WARN("Could not find codec parameters");
590         goto exit_func;
591     }
592     for(i = 0; i < ic->nb_streams; i++)
593     {
594 #if LIBAVFORMAT_BUILD > 4628
595         AVCodecContext *enc = ic->streams[i]->codec;
596 #else
597         AVCodecContext *enc = &ic->streams[i]->codec;
598 #endif
599
600 //#ifdef FF_API_THREAD_INIT
601 //        avcodec_thread_init(enc, get_number_of_cpus());
602 //#else
603         enc->thread_count = get_number_of_cpus();
604 //#endif
605
606 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
607 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
608 #endif
609
610         if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
611         {
612             // backup encoder' width/height
613             int enc_width = enc->width;
614             int enc_height = enc->height;
615
616             AVCodec *codec = avcodec_find_decoder(enc->codec_id);
617             if (!codec ||
618 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
619                 avcodec_open2(enc, codec, NULL)
620 #else
621                 avcodec_open(enc, codec)
622 #endif
623                 < 0)
624                 goto exit_func;
625
626             // checking width/height (since decoder can sometimes alter it, eg. vp6f)
627             if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
628             if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
629
630             video_stream = i;
631             video_st = ic->streams[i];
632             picture = avcodec_alloc_frame();
633
634             rgb_picture.data[0] = (uint8_t*)malloc(
635                     avpicture_get_size( PIX_FMT_BGR24,
636                                         enc->width, enc->height ));
637             avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
638                             PIX_FMT_BGR24, enc->width, enc->height );
639
640             frame.width = enc->width;
641             frame.height = enc->height;
642             frame.cn = 3;
643             frame.step = rgb_picture.linesize[0];
644             frame.data = rgb_picture.data[0];
645             break;
646         }
647     }
648
649     if(video_stream >= 0) valid = true;
650
651 exit_func:
652
653     if( !valid )
654         close();
655
656     return valid;
657 }
658
659
660 bool CvCapture_FFMPEG::grabFrame()
661 {
662     bool valid = false;
663     int got_picture;
664
665     int count_errs = 0;
666     const int max_number_of_attempts = 1 << 9;
667
668     if( !ic || !video_st )  return false;
669
670     if( ic->streams[video_stream]->nb_frames > 0 &&
671         frame_number > ic->streams[video_stream]->nb_frames )
672         return false;
673
674     picture_pts = AV_NOPTS_VALUE_;
675
676     // get the next frame
677     while (!valid)
678     {
679
680         av_free_packet (&packet);
681         int ret = av_read_frame(ic, &packet);
682         if (ret == AVERROR(EAGAIN)) continue;
683
684         /* else if (ret < 0) break; */
685
686         if( packet.stream_index != video_stream )
687         {
688             av_free_packet (&packet);
689             count_errs++;
690             if (count_errs > max_number_of_attempts)
691                 break;
692             continue;
693         }
694
695         // Decode video frame
696         #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
697             avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
698         #elif LIBAVFORMAT_BUILD > 4628
699                 avcodec_decode_video(video_st->codec,
700                                      picture, &got_picture,
701                                      packet.data, packet.size);
702         #else
703                 avcodec_decode_video(&video_st->codec,
704                                      picture, &got_picture,
705                                      packet.data, packet.size);
706         #endif
707
708         // Did we get a video frame?
709         if(got_picture)
710         {
711             //picture_pts = picture->best_effort_timestamp;
712             if( picture_pts == AV_NOPTS_VALUE_ )
713                 picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
714             frame_number++;
715             valid = true;
716         }
717         else
718         {
719             count_errs++;
720             if (count_errs > max_number_of_attempts)
721                 break;
722         }
723     }
724
725     if( valid && first_frame_number < 0 )
726         first_frame_number = dts_to_frame_number(picture_pts);
727
728     // return if we have a new picture or not
729     return valid;
730 }
731
732
733 bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
734 {
735     if( !video_st || !picture->data[0] )
736         return false;
737
738     avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
739                    video_st->codec->width, video_st->codec->height);
740
741     if( img_convert_ctx == NULL ||
742         frame.width != video_st->codec->width ||
743         frame.height != video_st->codec->height )
744     {
745         if( img_convert_ctx )
746             sws_freeContext(img_convert_ctx);
747
748         frame.width = video_st->codec->width;
749         frame.height = video_st->codec->height;
750
751         img_convert_ctx = sws_getCachedContext(
752                 NULL,
753                 video_st->codec->width, video_st->codec->height,
754                 video_st->codec->pix_fmt,
755                 video_st->codec->width, video_st->codec->height,
756                 PIX_FMT_BGR24,
757                 SWS_BICUBIC,
758                 NULL, NULL, NULL
759                 );
760
761         if (img_convert_ctx == NULL)
762             return false;//CV_Error(0, "Cannot initialize the conversion context!");
763     }
764
765     sws_scale(
766             img_convert_ctx,
767             picture->data,
768             picture->linesize,
769             0, video_st->codec->height,
770             rgb_picture.data,
771             rgb_picture.linesize
772             );
773
774     *data = frame.data;
775     *step = frame.step;
776     *width = frame.width;
777     *height = frame.height;
778     *cn = frame.cn;
779
780     return true;
781 }
782
783
784 double CvCapture_FFMPEG::getProperty( int property_id ) const
785 {
786     if( !video_st ) return 0;
787
788     switch( property_id )
789     {
790     case CV_FFMPEG_CAP_PROP_POS_MSEC:
791         return 1000.0*(double)frame_number/get_fps();
792     case CV_FFMPEG_CAP_PROP_POS_FRAMES:
793         return (double)frame_number;
794     case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
795         return r2d(ic->streams[video_stream]->time_base);
796     case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
797         return (double)get_total_frames();
798     case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
799         return (double)frame.width;
800     case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
801         return (double)frame.height;
802     case CV_FFMPEG_CAP_PROP_FPS:
803 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
804         return av_q2d(video_st->avg_frame_rate);
805 #elif LIBAVCODEC_BUILD > 4753
806         return av_q2d(video_st->r_frame_rate);
807 #else
808         return (double)video_st->codec.frame_rate
809                 / (double)video_st->codec.frame_rate_base;
810 #endif
811     case CV_FFMPEG_CAP_PROP_FOURCC:
812 #if LIBAVFORMAT_BUILD > 4628
813         return (double)video_st->codec->codec_tag;
814 #else
815         return (double)video_st->codec.codec_tag;
816 #endif
817     default:
818         break;
819     }
820
821     return 0;
822 }
823
824 double CvCapture_FFMPEG::r2d(AVRational r) const
825 {
826     return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
827 }
828
829 double CvCapture_FFMPEG::get_duration_sec() const
830 {
831     double sec = (double)ic->duration / (double)AV_TIME_BASE;
832
833     if (sec < eps_zero)
834     {
835         sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
836     }
837
838     if (sec < eps_zero)
839     {
840         sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
841     }
842
843     return sec;
844 }
845
846 int CvCapture_FFMPEG::get_bitrate() const
847 {
848     return ic->bit_rate;
849 }
850
851 double CvCapture_FFMPEG::get_fps() const
852 {
853 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
854     double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
855 #else
856     double fps = r2d(ic->streams[video_stream]->r_frame_rate);
857 #endif
858
859 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
860     if (fps < eps_zero)
861     {
862         fps = r2d(ic->streams[video_stream]->avg_frame_rate);
863     }
864 #endif
865
866     if (fps < eps_zero)
867     {
868         fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
869     }
870
871     return fps;
872 }
873
874 int64_t CvCapture_FFMPEG::get_total_frames() const
875 {
876     int64_t nbf = ic->streams[video_stream]->nb_frames;
877
878     if (nbf == 0)
879     {
880         nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
881     }
882     return nbf;
883 }
884
885 int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
886 {
887     double sec = dts_to_sec(dts);
888     return (int64_t)(get_fps() * sec + 0.5);
889 }
890
891 double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
892 {
893     return (double)(dts - ic->streams[video_stream]->start_time) *
894         r2d(ic->streams[video_stream]->time_base);
895 }
896
897 void CvCapture_FFMPEG::seek(int64_t _frame_number)
898 {
899     _frame_number = std::min(_frame_number, get_total_frames());
900     int delta = 16;
901
902     // if we have not grabbed a single frame before first seek, let's read the first frame
903     // and get some valuable information during the process
904     if( first_frame_number < 0 && get_total_frames() > 1 )
905         grabFrame();
906
907     for(;;)
908     {
909         int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
910         double sec = (double)_frame_number_temp / get_fps();
911         int64_t time_stamp = ic->streams[video_stream]->start_time;
912         double  time_base  = r2d(ic->streams[video_stream]->time_base);
913         time_stamp += (int64_t)(sec / time_base + 0.5);
914         if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
915         avcodec_flush_buffers(ic->streams[video_stream]->codec);
916         if( _frame_number > 0 )
917         {
918             grabFrame();
919
920             if( _frame_number > 1 )
921             {
922                 frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
923                 //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
924                 //       (int)_frame_number, (int)frame_number, delta);
925
926                 if( frame_number < 0 || frame_number > _frame_number-1 )
927                 {
928                     if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
929                         break;
930                     delta = delta < 16 ? delta*2 : delta*3/2;
931                     continue;
932                 }
933                 while( frame_number < _frame_number-1 )
934                 {
935                     if(!grabFrame())
936                         break;
937                 }
938                 frame_number++;
939                 break;
940             }
941             else
942             {
943                 frame_number = 1;
944                 break;
945             }
946         }
947         else
948         {
949             frame_number = 0;
950             break;
951         }
952     }
953 }
954
955 void CvCapture_FFMPEG::seek(double sec)
956 {
957     seek((int64_t)(sec * get_fps() + 0.5));
958 }
959
960 bool CvCapture_FFMPEG::setProperty( int property_id, double value )
961 {
962     if( !video_st ) return false;
963
964     switch( property_id )
965     {
966     case CV_FFMPEG_CAP_PROP_POS_MSEC:
967     case CV_FFMPEG_CAP_PROP_POS_FRAMES:
968     case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
969         {
970             switch( property_id )
971             {
972             case CV_FFMPEG_CAP_PROP_POS_FRAMES:
973                 seek((int64_t)value);
974                 break;
975
976             case CV_FFMPEG_CAP_PROP_POS_MSEC:
977                 seek(value/1000.0);
978                 break;
979
980             case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
981                 seek((int64_t)(value*ic->duration));
982                 break;
983             }
984
985             picture_pts=(int64_t)value;
986         }
987         break;
988     default:
989         return false;
990     }
991
992     return true;
993 }
994
995
996 ///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
997 struct CvVideoWriter_FFMPEG
998 {
999     bool open( const char* filename, int fourcc,
1000                double fps, int width, int height, bool isColor );
1001     void close();
1002     bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
1003
1004     void init();
1005
1006     AVOutputFormat  * fmt;
1007     AVFormatContext * oc;
1008     uint8_t         * outbuf;
1009     uint32_t          outbuf_size;
1010     FILE            * outfile;
1011     AVFrame         * picture;
1012     AVFrame         * input_picture;
1013     uint8_t         * picbuf;
1014     AVStream        * video_st;
1015     int               input_pix_fmt;
1016     Image_FFMPEG      temp_image;
1017     int               frame_width, frame_height;
1018     int               frame_idx;
1019     bool              ok;
1020     struct SwsContext *img_convert_ctx;
1021 };
1022
1023 static const char * icvFFMPEGErrStr(int err)
1024 {
1025 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1026     switch(err) {
1027     case AVERROR_BSF_NOT_FOUND:
1028         return "Bitstream filter not found";
1029     case AVERROR_DECODER_NOT_FOUND:
1030         return "Decoder not found";
1031     case AVERROR_DEMUXER_NOT_FOUND:
1032         return "Demuxer not found";
1033     case AVERROR_ENCODER_NOT_FOUND:
1034         return "Encoder not found";
1035     case AVERROR_EOF:
1036         return "End of file";
1037     case AVERROR_EXIT:
1038         return "Immediate exit was requested; the called function should not be restarted";
1039     case AVERROR_FILTER_NOT_FOUND:
1040         return "Filter not found";
1041     case AVERROR_INVALIDDATA:
1042         return "Invalid data found when processing input";
1043     case AVERROR_MUXER_NOT_FOUND:
1044         return "Muxer not found";
1045     case AVERROR_OPTION_NOT_FOUND:
1046         return "Option not found";
1047     case AVERROR_PATCHWELCOME:
1048         return "Not yet implemented in FFmpeg, patches welcome";
1049     case AVERROR_PROTOCOL_NOT_FOUND:
1050         return "Protocol not found";
1051     case AVERROR_STREAM_NOT_FOUND:
1052         return "Stream not found";
1053     default:
1054         break;
1055     }
1056 #else
1057     switch(err) {
1058     case AVERROR_NUMEXPECTED:
1059         return "Incorrect filename syntax";
1060     case AVERROR_INVALIDDATA:
1061         return "Invalid data in header";
1062     case AVERROR_NOFMT:
1063         return "Unknown format";
1064     case AVERROR_IO:
1065         return "I/O error occurred";
1066     case AVERROR_NOMEM:
1067         return "Memory allocation error";
1068     default:
1069         break;
1070     }
1071 #endif
1072
1073     return "Unspecified error";
1074 }
1075
1076 /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1077 extern "C" {
1078     enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1079 }
1080
1081 void CvVideoWriter_FFMPEG::init()
1082 {
1083     fmt = 0;
1084     oc = 0;
1085     outbuf = 0;
1086     outbuf_size = 0;
1087     outfile = 0;
1088     picture = 0;
1089     input_picture = 0;
1090     picbuf = 0;
1091     video_st = 0;
1092     input_pix_fmt = 0;
1093     memset(&temp_image, 0, sizeof(temp_image));
1094     img_convert_ctx = 0;
1095     frame_width = frame_height = 0;
1096     frame_idx = 0;
1097     ok = false;
1098 }
1099
1100 /**
1101  * the following function is a modified version of code
1102  * found in ffmpeg-0.4.9-pre1/output_example.c
1103  */
1104 static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1105 {
1106     AVFrame * picture;
1107     uint8_t * picture_buf;
1108     int size;
1109
1110     picture = avcodec_alloc_frame();
1111     if (!picture)
1112         return NULL;
1113     size = avpicture_get_size( (PixelFormat) pix_fmt, width, height);
1114     if(alloc){
1115         picture_buf = (uint8_t *) malloc(size);
1116         if (!picture_buf)
1117         {
1118             av_free(picture);
1119             return NULL;
1120         }
1121         avpicture_fill((AVPicture *)picture, picture_buf,
1122                        (PixelFormat) pix_fmt, width, height);
1123     }
1124     else {
1125     }
1126     return picture;
1127 }
1128
1129 /* add a video output stream to the container */
1130 static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1131                                              CV_CODEC_ID codec_id,
1132                                              int w, int h, int bitrate,
1133                                              double fps, int pixel_format)
1134 {
1135     AVCodecContext *c;
1136     AVStream *st;
1137     int frame_rate, frame_rate_base;
1138     AVCodec *codec;
1139
1140 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1141     st = avformat_new_stream(oc, 0);
1142 #else
1143     st = av_new_stream(oc, 0);
1144 #endif
1145
1146     if (!st) {
1147         CV_WARN("Could not allocate stream");
1148         return NULL;
1149     }
1150
1151 #if LIBAVFORMAT_BUILD > 4628
1152     c = st->codec;
1153 #else
1154     c = &(st->codec);
1155 #endif
1156
1157 #if LIBAVFORMAT_BUILD > 4621
1158     c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1159 #else
1160     c->codec_id = oc->oformat->video_codec;
1161 #endif
1162
1163     if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1164         c->codec_id = codec_id;
1165     }
1166
1167     //if(codec_tag) c->codec_tag=codec_tag;
1168     codec = avcodec_find_encoder(c->codec_id);
1169
1170     c->codec_type = AVMEDIA_TYPE_VIDEO;
1171
1172 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
1173     // Set per-codec defaults
1174     AVCodecID c_id = c->codec_id;
1175     avcodec_get_context_defaults3(c, codec);
1176     // avcodec_get_context_defaults3 erases codec_id for some reason
1177     c->codec_id = c_id;
1178 #endif
1179
1180     /* put sample parameters */
1181     int64_t lbit_rate = (int64_t)bitrate;
1182     lbit_rate += (bitrate / 2);
1183     lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1184     c->bit_rate = lbit_rate;
1185
1186     // took advice from
1187     // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1188     c->qmin = 3;
1189
1190     /* resolution must be a multiple of two */
1191     c->width = w;
1192     c->height = h;
1193
1194     /* time base: this is the fundamental unit of time (in seconds) in terms
1195        of which frame timestamps are represented. for fixed-fps content,
1196        timebase should be 1/framerate and timestamp increments should be
1197        identically 1. */
1198     frame_rate=(int)(fps+0.5);
1199     frame_rate_base=1;
1200     while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
1201         frame_rate_base*=10;
1202         frame_rate=(int)(fps*frame_rate_base + 0.5);
1203     }
1204 #if LIBAVFORMAT_BUILD > 4752
1205     c->time_base.den = frame_rate;
1206     c->time_base.num = frame_rate_base;
1207     /* adjust time base for supported framerates */
1208     if(codec && codec->supported_framerates){
1209         const AVRational *p= codec->supported_framerates;
1210         AVRational req = {frame_rate, frame_rate_base};
1211         const AVRational *best=NULL;
1212         AVRational best_error= {INT_MAX, 1};
1213         for(; p->den!=0; p++){
1214             AVRational error= av_sub_q(req, *p);
1215             if(error.num <0) error.num *= -1;
1216             if(av_cmp_q(error, best_error) < 0){
1217                 best_error= error;
1218                 best= p;
1219             }
1220         }
1221         c->time_base.den= best->num;
1222         c->time_base.num= best->den;
1223     }
1224 #else
1225     c->frame_rate = frame_rate;
1226     c->frame_rate_base = frame_rate_base;
1227 #endif
1228
1229     c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1230     c->pix_fmt = (PixelFormat) pixel_format;
1231
1232     if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1233         c->max_b_frames = 2;
1234     }
1235     if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1236         /* needed to avoid using macroblocks in which some coeffs overflow
1237            this doesnt happen with normal video, it just happens here as the
1238            motion of the chroma plane doesnt match the luma plane */
1239         /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1240         c->mb_decision=2;
1241     }
1242
1243 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
1244     /* Some settings for libx264 encoding, restore dummy values for gop_size
1245      and qmin since they will be set to reasonable defaults by the libx264
1246      preset system. Also, use a crf encode with the default quality rating,
1247      this seems easier than finding an appropriate default bitrate. */
1248     if (c->codec_id == AV_CODEC_ID_H264) {
1249       c->gop_size = -1;
1250       c->qmin = -1;
1251       c->bit_rate = 0;
1252       av_opt_set(c->priv_data,"crf","23", 0);
1253     }
1254 #endif
1255
1256 #if LIBAVCODEC_VERSION_INT>0x000409
1257     // some formats want stream headers to be seperate
1258     if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1259     {
1260         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1261     }
1262 #endif
1263
1264     return st;
1265 }
1266
1267 static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1268
1269 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
1270 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1271                                       uint8_t *, uint32_t,
1272 #else
1273                                       uint8_t * outbuf, uint32_t outbuf_size,
1274 #endif
1275                                       AVFrame * picture )
1276 {
1277 #if LIBAVFORMAT_BUILD > 4628
1278     AVCodecContext * c = video_st->codec;
1279 #else
1280     AVCodecContext * c = &(video_st->codec);
1281 #endif
1282     int ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1283
1284     if (oc->oformat->flags & AVFMT_RAWPICTURE) {
1285         /* raw video case. The API will change slightly in the near
1286            futur for that */
1287         AVPacket pkt;
1288         av_init_packet(&pkt);
1289
1290 #ifndef PKT_FLAG_KEY
1291 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
1292 #endif
1293
1294         pkt.flags |= PKT_FLAG_KEY;
1295         pkt.stream_index= video_st->index;
1296         pkt.data= (uint8_t *)picture;
1297         pkt.size= sizeof(AVPicture);
1298
1299         ret = av_write_frame(oc, &pkt);
1300     } else {
1301         /* encode the image */
1302         AVPacket pkt;
1303         av_init_packet(&pkt);
1304 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1305         int got_output = 0;
1306         pkt.data = NULL;
1307         pkt.size = 0;
1308         ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
1309         if (ret < 0)
1310             ;
1311         else if (got_output) {
1312             if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
1313                 pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
1314             if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
1315                 pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
1316             if (pkt.duration)
1317                 pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
1318             pkt.stream_index= video_st->index;
1319             ret = av_write_frame(oc, &pkt);
1320             av_free_packet(&pkt);
1321         }
1322         else
1323             ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1324 #else
1325         int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1326         /* if zero size, it means the image was buffered */
1327         if (out_size > 0) {
1328 #if LIBAVFORMAT_BUILD > 4752
1329             if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1330                 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1331 #else
1332             pkt.pts = c->coded_frame->pts;
1333 #endif
1334             if(c->coded_frame->key_frame)
1335                 pkt.flags |= PKT_FLAG_KEY;
1336             pkt.stream_index= video_st->index;
1337             pkt.data= outbuf;
1338             pkt.size= out_size;
1339
1340             /* write the compressed frame in the media file */
1341             ret = av_write_frame(oc, &pkt);
1342         }
1343 #endif
1344     }
1345     return ret;
1346 }
1347
1348 /// write a frame with FFMPEG
1349 bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1350 {
1351     bool ret = false;
1352
1353     if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1354         return false;
1355     width = frame_width;
1356     height = frame_height;
1357
1358     // typecast from opaque data type to implemented struct
1359 #if LIBAVFORMAT_BUILD > 4628
1360     AVCodecContext *c = video_st->codec;
1361 #else
1362     AVCodecContext *c = &(video_st->codec);
1363 #endif
1364
1365 #if LIBAVFORMAT_BUILD < 5231
1366     // It is not needed in the latest versions of the ffmpeg
1367     if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
1368     {
1369         if( !temp_image.data )
1370         {
1371             temp_image.step = (width*cn + 3) & -4;
1372             temp_image.width = width;
1373             temp_image.height = height;
1374             temp_image.cn = cn;
1375             temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1376         }
1377         for( int y = 0; y < height; y++ )
1378             memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
1379         data = temp_image.data;
1380         step = temp_image.step;
1381     }
1382 #else
1383     if( width*cn != step )
1384     {
1385         if( !temp_image.data )
1386         {
1387             temp_image.step = width*cn;
1388             temp_image.width = width;
1389             temp_image.height = height;
1390             temp_image.cn = cn;
1391             temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1392         }
1393         if (origin == 1)
1394             for( int y = 0; y < height; y++ )
1395                 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
1396         else
1397             for( int y = 0; y < height; y++ )
1398                 memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
1399         data = temp_image.data;
1400         step = temp_image.step;
1401     }
1402 #endif
1403
1404     // check parameters
1405     if (input_pix_fmt == PIX_FMT_BGR24) {
1406         if (cn != 3) {
1407             return false;
1408         }
1409     }
1410     else if (input_pix_fmt == PIX_FMT_GRAY8) {
1411         if (cn != 1) {
1412             return false;
1413         }
1414     }
1415     else {
1416         assert(false);
1417     }
1418
1419     if ( c->pix_fmt != input_pix_fmt ) {
1420         assert( input_picture );
1421         // let input_picture point to the raw data buffer of 'image'
1422         avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1423                        (PixelFormat)input_pix_fmt, width, height);
1424
1425         if( !img_convert_ctx )
1426         {
1427             img_convert_ctx = sws_getContext(width,
1428                                              height,
1429                                              (PixelFormat)input_pix_fmt,
1430                                              c->width,
1431                                              c->height,
1432                                              c->pix_fmt,
1433                                              SWS_BICUBIC,
1434                                              NULL, NULL, NULL);
1435             if( !img_convert_ctx )
1436                 return false;
1437         }
1438
1439         if ( sws_scale(img_convert_ctx, input_picture->data,
1440                        input_picture->linesize, 0,
1441                        height,
1442                        picture->data, picture->linesize) < 0 )
1443             return false;
1444     }
1445     else{
1446         avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1447                        (PixelFormat)input_pix_fmt, width, height);
1448     }
1449
1450     picture->pts = frame_idx;
1451     ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1452     frame_idx++;
1453
1454     return ret;
1455 }
1456
1457 /// close video output stream and free associated memory
1458 void CvVideoWriter_FFMPEG::close()
1459 {
1460     // nothing to do if already released
1461     if ( !picture )
1462         return;
1463
1464     /* no more frame to compress. The codec has a latency of a few
1465        frames if using B frames, so we get the last frames by
1466        passing the same picture again */
1467     // TODO -- do we need to account for latency here?
1468
1469     /* write the trailer, if any */
1470     if(ok && oc)
1471     {
1472         if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1473         {
1474             for(;;)
1475             {
1476                 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1477                 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1478                     break;
1479             }
1480         }
1481         av_write_trailer(oc);
1482     }
1483
1484     if( img_convert_ctx )
1485     {
1486         sws_freeContext(img_convert_ctx);
1487         img_convert_ctx = 0;
1488     }
1489
1490     // free pictures
1491 #if LIBAVFORMAT_BUILD > 4628
1492     if( video_st->codec->pix_fmt != input_pix_fmt)
1493 #else
1494     if( video_st->codec.pix_fmt != input_pix_fmt)
1495 #endif
1496     {
1497         if(picture->data[0])
1498             free(picture->data[0]);
1499         picture->data[0] = 0;
1500     }
1501     av_free(picture);
1502
1503     if (input_picture)
1504         av_free(input_picture);
1505
1506     /* close codec */
1507 #if LIBAVFORMAT_BUILD > 4628
1508     avcodec_close(video_st->codec);
1509 #else
1510     avcodec_close(&(video_st->codec));
1511 #endif
1512
1513     av_free(outbuf);
1514
1515     if (!(fmt->flags & AVFMT_NOFILE))
1516     {
1517         /* close the output file */
1518
1519 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1520 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1521         url_fclose(oc->pb);
1522 #else
1523         url_fclose(&oc->pb);
1524 #endif
1525 #else
1526         avio_close(oc->pb);
1527 #endif
1528
1529     }
1530
1531     /* free the stream */
1532     avformat_free_context(oc);
1533
1534     if( temp_image.data )
1535     {
1536         free(temp_image.data);
1537         temp_image.data = 0;
1538     }
1539
1540     init();
1541 }
1542
1543 #define CV_PRINTABLE_CHAR(ch) ((ch) < 32 ? '?' : (ch))
1544 #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)
1545
1546 static inline bool cv_ff_codec_tag_match(const AVCodecTag *tags, enum AVCodecID id, unsigned int tag)
1547 {
1548     while (tags->id != AV_CODEC_ID_NONE)
1549     {
1550         if (tags->id == id && tags->tag == tag)
1551             return true;
1552         tags++;
1553     }
1554     return false;
1555 }
1556 static inline bool cv_ff_codec_tag_list_match(const AVCodecTag *const *tags, enum AVCodecID id, unsigned int tag)
1557 {
1558     int i;
1559     for (i = 0; tags && tags[i]; i++) {
1560         bool res = cv_ff_codec_tag_match(tags[i], id, tag);
1561         if (res)
1562             return res;
1563     }
1564     return false;
1565 }
1566
1567 /// Create a video writer object that uses FFMPEG
1568 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1569                                  double fps, int width, int height, bool is_color )
1570 {
1571     CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1572     int err, codec_pix_fmt;
1573     double bitrate_scale = 1;
1574
1575     close();
1576
1577     // check arguments
1578     if( !filename )
1579         return false;
1580     if(fps <= 0)
1581         return false;
1582
1583     // we allow frames of odd width or height, but in this case we truncate
1584     // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1585     // but some internal functions inside FFMPEG swscale require even width/height.
1586     width &= -2;
1587     height &= -2;
1588     if( width <= 0 || height <= 0 )
1589         return false;
1590
1591     /* auto detect the output format from the name and fourcc code. */
1592
1593 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1594     fmt = av_guess_format(NULL, filename, NULL);
1595 #else
1596     fmt = guess_format(NULL, filename, NULL);
1597 #endif
1598
1599     if (!fmt)
1600         return false;
1601
1602     /* determine optimal pixel format */
1603     if (is_color) {
1604         input_pix_fmt = PIX_FMT_BGR24;
1605     }
1606     else {
1607         input_pix_fmt = PIX_FMT_GRAY8;
1608     }
1609
1610     /* Lookup codec_id for given fourcc */
1611 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1612     if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1613         return false;
1614 #elif LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1615 // APIchnages:
1616 // 2012-01-31 - dd6d3b0 - lavf 54.01.0
1617 //   Add avformat_get_riff_video_tags() and avformat_get_riff_audio_tags().
1618     if( (codec_id = av_codec_get_id(fmt->codec_tag, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1619     {
1620         const struct AVCodecTag * fallback_tags[] = {
1621                 avformat_get_riff_video_tags(),
1622 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 25, 100)
1623 // APIchanges: ffmpeg only
1624 // 2014-01-19 - 1a193c4 - lavf 55.25.100 - avformat.h
1625 //   Add avformat_get_mov_video_tags() and avformat_get_mov_audio_tags().
1626                 // TODO ffmpeg only, need to skip libav: avformat_get_mov_video_tags(),
1627 #endif
1628                 codec_bmp_tags, NULL };
1629         if( (codec_id = av_codec_get_id(fallback_tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1630         {
1631             fflush(stdout);
1632             fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not found (format '%s / %s')'\n",
1633                     fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
1634                     fmt->name, fmt->long_name);
1635             return false;
1636         }
1637     }
1638     // validate tag
1639     if (cv_ff_codec_tag_list_match(fmt->codec_tag, codec_id, fourcc) == false)
1640     {
1641         fflush(stdout);
1642         fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not supported with codec id %d and format '%s / %s'\n",
1643                 fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
1644                 codec_id, fmt->name, fmt->long_name);
1645         int supported_tag;
1646         if( (supported_tag = av_codec_get_tag(fmt->codec_tag, codec_id)) != 0 )
1647         {
1648             fprintf(stderr, "OpenCV: FFMPEG: fallback to use tag 0x%08x/'%c%c%c%c'\n",
1649                     supported_tag, CV_TAG_TO_PRINTABLE_CHAR4(supported_tag));
1650             fourcc = supported_tag;
1651         }
1652     }
1653 #else
1654     const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
1655     if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1656         return false;
1657 #endif
1658
1659     // alloc memory for context
1660 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1661     oc = avformat_alloc_context();
1662 #else
1663     oc = av_alloc_format_context();
1664 #endif
1665     assert (oc);
1666
1667     /* set file name */
1668     oc->oformat = fmt;
1669     snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1670
1671     /* set some options */
1672     oc->max_delay = (int)(0.7*AV_TIME_BASE);  /* This reduces buffer underrun warnings with MPEG */
1673
1674     // set a few optimal pixel formats for lossless codecs of interest..
1675     switch (codec_id) {
1676 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1677     case CV_CODEC(CODEC_ID_JPEGLS):
1678         // BGR24 or GRAY8 depending on is_color...
1679         codec_pix_fmt = input_pix_fmt;
1680         break;
1681 #endif
1682     case CV_CODEC(CODEC_ID_HUFFYUV):
1683         codec_pix_fmt = PIX_FMT_YUV422P;
1684         break;
1685     case CV_CODEC(CODEC_ID_MJPEG):
1686     case CV_CODEC(CODEC_ID_LJPEG):
1687         codec_pix_fmt = PIX_FMT_YUVJ420P;
1688         bitrate_scale = 3;
1689         break;
1690     case CV_CODEC(CODEC_ID_RAWVIDEO):
1691         codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
1692                         input_pix_fmt == PIX_FMT_GRAY16LE ||
1693                         input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
1694         break;
1695     default:
1696         // good for lossy formats, MPEG, etc.
1697         codec_pix_fmt = PIX_FMT_YUV420P;
1698         break;
1699     }
1700
1701     double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1702
1703     // TODO -- safe to ignore output audio stream?
1704     video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1705                                            width, height, (int)(bitrate + 0.5),
1706                                            fps, codec_pix_fmt);
1707
1708     /* set the output parameters (must be done even if no
1709    parameters). */
1710 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1711     if (av_set_parameters(oc, NULL) < 0) {
1712         return false;
1713     }
1714 #endif
1715
1716 #if 0
1717 #if FF_API_DUMP_FORMAT
1718     dump_format(oc, 0, filename, 1);
1719 #else
1720     av_dump_format(oc, 0, filename, 1);
1721 #endif
1722 #endif
1723
1724     /* now that all the parameters are set, we can open the audio and
1725      video codecs and allocate the necessary encode buffers */
1726     if (!video_st){
1727         return false;
1728     }
1729
1730     AVCodec *codec;
1731     AVCodecContext *c;
1732
1733 #if LIBAVFORMAT_BUILD > 4628
1734     c = (video_st->codec);
1735 #else
1736     c = &(video_st->codec);
1737 #endif
1738
1739     c->codec_tag = fourcc;
1740     /* find the video encoder */
1741     codec = avcodec_find_encoder(c->codec_id);
1742     if (!codec) {
1743         fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
1744         #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1745                 AVERROR_ENCODER_NOT_FOUND
1746         #else
1747                 -1
1748         #endif
1749                 ));
1750         return false;
1751     }
1752
1753     int64_t lbit_rate = (int64_t)c->bit_rate;
1754     lbit_rate += (bitrate / 2);
1755     lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1756     c->bit_rate_tolerance = (int)lbit_rate;
1757     c->bit_rate = (int)lbit_rate;
1758
1759     /* open the codec */
1760     if ((err=
1761 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1762          avcodec_open2(c, codec, NULL)
1763 #else
1764          avcodec_open(c, codec)
1765 #endif
1766          ) < 0) {
1767         fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
1768         return false;
1769     }
1770
1771     outbuf = NULL;
1772
1773     if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1774         /* allocate output buffer */
1775         /* assume we will never get codec output with more than 4 bytes per pixel... */
1776         outbuf_size = width*height*4;
1777         outbuf = (uint8_t *) av_malloc(outbuf_size);
1778     }
1779
1780     bool need_color_convert;
1781     need_color_convert = (c->pix_fmt != input_pix_fmt);
1782
1783     /* allocate the encoded raw picture */
1784     picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1785     if (!picture) {
1786         return false;
1787     }
1788
1789     /* if the output format is not our input format, then a temporary
1790    picture of the input format is needed too. It is then converted
1791    to the required output format */
1792     input_picture = NULL;
1793     if ( need_color_convert ) {
1794         input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1795         if (!input_picture) {
1796             return false;
1797         }
1798     }
1799
1800     /* open the output file, if needed */
1801     if (!(fmt->flags & AVFMT_NOFILE)) {
1802 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1803         if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1804 #else
1805             if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1806 #endif
1807             {
1808             return false;
1809         }
1810     }
1811
1812 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1813     /* write the stream header, if any */
1814     err=avformat_write_header(oc, NULL);
1815 #else
1816     err=av_write_header( oc );
1817 #endif
1818
1819     if(err < 0)
1820     {
1821         close();
1822         remove(filename);
1823         return false;
1824     }
1825     frame_width = width;
1826     frame_height = height;
1827     frame_idx = 0;
1828     ok = true;
1829
1830     return true;
1831 }
1832
1833
1834
1835 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1836 {
1837     CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1838     capture->init();
1839     if( capture->open( filename ))
1840         return capture;
1841
1842     capture->close();
1843     free(capture);
1844     return 0;
1845 }
1846
1847
1848 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1849 {
1850     if( capture && *capture )
1851     {
1852         (*capture)->close();
1853         free(*capture);
1854         *capture = 0;
1855     }
1856 }
1857
1858 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1859 {
1860     return capture->setProperty(prop_id, value);
1861 }
1862
1863 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1864 {
1865     return capture->getProperty(prop_id);
1866 }
1867
1868 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1869 {
1870     return capture->grabFrame();
1871 }
1872
1873 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1874 {
1875     return capture->retrieveFrame(0, data, step, width, height, cn);
1876 }
1877
1878 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1879                                                   int width, int height, int isColor )
1880 {
1881     CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1882     writer->init();
1883     if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1884         return writer;
1885     writer->close();
1886     free(writer);
1887     return 0;
1888 }
1889
1890 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1891 {
1892     if( writer && *writer )
1893     {
1894         (*writer)->close();
1895         free(*writer);
1896         *writer = 0;
1897     }
1898 }
1899
1900
1901 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1902                          const unsigned char* data, int step,
1903                          int width, int height, int cn, int origin)
1904 {
1905     return writer->writeFrame(data, step, width, height, cn, origin);
1906 }
1907
1908
1909
1910 /*
1911  * For CUDA encoder
1912  */
1913
1914 struct OutputMediaStream_FFMPEG
1915 {
1916     bool open(const char* fileName, int width, int height, double fps);
1917     void close();
1918
1919     void write(unsigned char* data, int size, int keyFrame);
1920
1921     // add a video output stream to the container
1922     static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
1923
1924     AVOutputFormat* fmt_;
1925     AVFormatContext* oc_;
1926     AVStream* video_st_;
1927 };
1928
1929 void OutputMediaStream_FFMPEG::close()
1930 {
1931     // no more frame to compress. The codec has a latency of a few
1932     // frames if using B frames, so we get the last frames by
1933     // passing the same picture again
1934
1935     // TODO -- do we need to account for latency here?
1936
1937     if (oc_)
1938     {
1939         // write the trailer, if any
1940         av_write_trailer(oc_);
1941
1942         // free the streams
1943         for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1944         {
1945             av_freep(&oc_->streams[i]->codec);
1946             av_freep(&oc_->streams[i]);
1947         }
1948
1949         if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
1950         {
1951             // close the output file
1952
1953             #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1954                 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1955                     url_fclose(oc_->pb);
1956                 #else
1957                     url_fclose(&oc_->pb);
1958                 #endif
1959             #else
1960                 avio_close(oc_->pb);
1961             #endif
1962         }
1963
1964         // free the stream
1965         av_free(oc_);
1966     }
1967 }
1968
1969 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
1970 {
1971     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1972         AVStream* st = avformat_new_stream(oc, 0);
1973     #else
1974         AVStream* st = av_new_stream(oc, 0);
1975     #endif
1976     if (!st)
1977         return 0;
1978
1979     #if LIBAVFORMAT_BUILD > 4628
1980         AVCodecContext* c = st->codec;
1981     #else
1982         AVCodecContext* c = &(st->codec);
1983     #endif
1984
1985     c->codec_id = codec_id;
1986     c->codec_type = AVMEDIA_TYPE_VIDEO;
1987
1988     // put sample parameters
1989     unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
1990     lbit_rate += (bitrate / 4);
1991     lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
1992     c->bit_rate = bitrate;
1993
1994     // took advice from
1995     // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1996     c->qmin = 3;
1997
1998     // resolution must be a multiple of two
1999     c->width = w;
2000     c->height = h;
2001
2002     AVCodec* codec = avcodec_find_encoder(c->codec_id);
2003
2004     // time base: this is the fundamental unit of time (in seconds) in terms
2005     // of which frame timestamps are represented. for fixed-fps content,
2006     // timebase should be 1/framerate and timestamp increments should be
2007     // identically 1
2008
2009     int frame_rate = static_cast<int>(fps+0.5);
2010     int frame_rate_base = 1;
2011     while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
2012     {
2013         frame_rate_base *= 10;
2014         frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
2015     }
2016     c->time_base.den = frame_rate;
2017     c->time_base.num = frame_rate_base;
2018
2019     #if LIBAVFORMAT_BUILD > 4752
2020         // adjust time base for supported framerates
2021         if (codec && codec->supported_framerates)
2022         {
2023             AVRational req = {frame_rate, frame_rate_base};
2024             const AVRational* best = NULL;
2025             AVRational best_error = {INT_MAX, 1};
2026
2027             for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
2028             {
2029                 AVRational error = av_sub_q(req, *p);
2030
2031                 if (error.num < 0)
2032                     error.num *= -1;
2033
2034                 if (av_cmp_q(error, best_error) < 0)
2035                 {
2036                     best_error= error;
2037                     best= p;
2038                 }
2039             }
2040
2041             c->time_base.den= best->num;
2042             c->time_base.num= best->den;
2043         }
2044     #endif
2045
2046     c->gop_size = 12; // emit one intra frame every twelve frames at most
2047     c->pix_fmt = pixel_format;
2048
2049     if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
2050         c->max_b_frames = 2;
2051
2052     if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
2053     {
2054         // needed to avoid using macroblocks in which some coeffs overflow
2055         // this doesnt happen with normal video, it just happens here as the
2056         // motion of the chroma plane doesnt match the luma plane
2057
2058         // avoid FFMPEG warning 'clipping 1 dct coefficients...'
2059
2060         c->mb_decision = 2;
2061     }
2062
2063     #if LIBAVCODEC_VERSION_INT > 0x000409
2064         // some formats want stream headers to be seperate
2065         if (oc->oformat->flags & AVFMT_GLOBALHEADER)
2066         {
2067             c->flags |= CODEC_FLAG_GLOBAL_HEADER;
2068         }
2069     #endif
2070
2071     return st;
2072 }
2073
2074 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
2075 {
2076     fmt_ = 0;
2077     oc_ = 0;
2078     video_st_ = 0;
2079
2080     // auto detect the output format from the name and fourcc code
2081     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2082         fmt_ = av_guess_format(NULL, fileName, NULL);
2083     #else
2084         fmt_ = guess_format(NULL, fileName, NULL);
2085     #endif
2086     if (!fmt_)
2087         return false;
2088
2089     CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
2090
2091     // alloc memory for context
2092     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2093         oc_ = avformat_alloc_context();
2094     #else
2095         oc_ = av_alloc_format_context();
2096     #endif
2097     if (!oc_)
2098         return false;
2099
2100     // set some options
2101     oc_->oformat = fmt_;
2102     snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
2103
2104     oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
2105
2106     // set a few optimal pixel formats for lossless codecs of interest..
2107     PixelFormat codec_pix_fmt = PIX_FMT_YUV420P;
2108     int bitrate_scale = 64;
2109
2110     // TODO -- safe to ignore output audio stream?
2111     video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
2112     if (!video_st_)
2113         return false;
2114
2115     // set the output parameters (must be done even if no parameters)
2116     #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2117         if (av_set_parameters(oc_, NULL) < 0)
2118             return false;
2119     #endif
2120
2121     // now that all the parameters are set, we can open the audio and
2122     // video codecs and allocate the necessary encode buffers
2123
2124     #if LIBAVFORMAT_BUILD > 4628
2125         AVCodecContext* c = (video_st_->codec);
2126     #else
2127         AVCodecContext* c = &(video_st_->codec);
2128     #endif
2129
2130     c->codec_tag = MKTAG('H', '2', '6', '4');
2131     c->bit_rate_tolerance = c->bit_rate;
2132
2133     // open the output file, if needed
2134     if (!(fmt_->flags & AVFMT_NOFILE))
2135     {
2136         #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2137             int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
2138         #else
2139             int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
2140         #endif
2141
2142         if (err != 0)
2143             return false;
2144     }
2145
2146     // write the stream header, if any
2147     #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2148         av_write_header(oc_);
2149     #else
2150         avformat_write_header(oc_, NULL);
2151     #endif
2152
2153     return true;
2154 }
2155
2156 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2157 {
2158     // if zero size, it means the image was buffered
2159     if (size > 0)
2160     {
2161         AVPacket pkt;
2162         av_init_packet(&pkt);
2163
2164         if (keyFrame)
2165             pkt.flags |= PKT_FLAG_KEY;
2166
2167         pkt.stream_index = video_st_->index;
2168         pkt.data = data;
2169         pkt.size = size;
2170
2171         // write the compressed frame in the media file
2172         av_write_frame(oc_, &pkt);
2173     }
2174 }
2175
2176 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2177 {
2178     OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2179
2180     if (stream->open(fileName, width, height, fps))
2181         return stream;
2182
2183     stream->close();
2184     free(stream);
2185
2186     return 0;
2187 }
2188
2189 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2190 {
2191     stream->close();
2192     free(stream);
2193 }
2194
2195 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2196 {
2197     stream->write(data, size, keyFrame);
2198 }
2199
2200 /*
2201  * For CUDA decoder
2202  */
2203
2204 enum
2205 {
2206     VideoCodec_MPEG1 = 0,
2207     VideoCodec_MPEG2,
2208     VideoCodec_MPEG4,
2209     VideoCodec_VC1,
2210     VideoCodec_H264,
2211     VideoCodec_JPEG,
2212     VideoCodec_H264_SVC,
2213     VideoCodec_H264_MVC,
2214
2215     // Uncompressed YUV
2216     VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')),   // Y,U,V (4:2:0)
2217     VideoCodec_YV12   = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')),   // Y,V,U (4:2:0)
2218     VideoCodec_NV12   = (('N'<<24)|('V'<<16)|('1'<<8)|('2')),   // Y,UV  (4:2:0)
2219     VideoCodec_YUYV   = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')),   // YUYV/YUY2 (4:2:2)
2220     VideoCodec_UYVY   = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y'))    // UYVY (4:2:2)
2221 };
2222
2223 enum
2224 {
2225     VideoChromaFormat_Monochrome = 0,
2226     VideoChromaFormat_YUV420,
2227     VideoChromaFormat_YUV422,
2228     VideoChromaFormat_YUV444
2229 };
2230
2231 struct InputMediaStream_FFMPEG
2232 {
2233 public:
2234     bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2235     void close();
2236
2237     bool read(unsigned char** data, int* size, int* endOfFile);
2238
2239 private:
2240     InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2241     InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2242
2243     AVFormatContext* ctx_;
2244     int video_stream_id_;
2245     AVPacket pkt_;
2246 };
2247
2248 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2249 {
2250     int err;
2251
2252     ctx_ = 0;
2253     video_stream_id_ = -1;
2254     memset(&pkt_, 0, sizeof(AVPacket));
2255
2256     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2257         avformat_network_init();
2258     #endif
2259
2260     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2261         err = avformat_open_input(&ctx_, fileName, 0, 0);
2262     #else
2263         err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2264     #endif
2265     if (err < 0)
2266         return false;
2267
2268     #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2269         err = avformat_find_stream_info(ctx_, 0);
2270     #else
2271         err = av_find_stream_info(ctx_);
2272     #endif
2273     if (err < 0)
2274         return false;
2275
2276     for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2277     {
2278         #if LIBAVFORMAT_BUILD > 4628
2279             AVCodecContext *enc = ctx_->streams[i]->codec;
2280         #else
2281             AVCodecContext *enc = &ctx_->streams[i]->codec;
2282         #endif
2283
2284         if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2285         {
2286             video_stream_id_ = static_cast<int>(i);
2287
2288             switch (enc->codec_id)
2289             {
2290             case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2291                 *codec = ::VideoCodec_MPEG1;
2292                 break;
2293
2294             case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2295                 *codec = ::VideoCodec_MPEG2;
2296                 break;
2297
2298             case CV_CODEC(CODEC_ID_MPEG4):
2299                 *codec = ::VideoCodec_MPEG4;
2300                 break;
2301
2302             case CV_CODEC(CODEC_ID_VC1):
2303                 *codec = ::VideoCodec_VC1;
2304                 break;
2305
2306             case CV_CODEC(CODEC_ID_H264):
2307                 *codec = ::VideoCodec_H264;
2308                 break;
2309
2310             default:
2311                 return false;
2312             };
2313
2314             switch (enc->pix_fmt)
2315             {
2316             case PIX_FMT_YUV420P:
2317                 *chroma_format = ::VideoChromaFormat_YUV420;
2318                 break;
2319
2320             case PIX_FMT_YUV422P:
2321                 *chroma_format = ::VideoChromaFormat_YUV422;
2322                 break;
2323
2324             case PIX_FMT_YUV444P:
2325                 *chroma_format = ::VideoChromaFormat_YUV444;
2326                 break;
2327
2328             default:
2329                 return false;
2330             }
2331
2332             *width = enc->coded_width;
2333             *height = enc->coded_height;
2334
2335             break;
2336         }
2337     }
2338
2339     if (video_stream_id_ < 0)
2340         return false;
2341
2342     av_init_packet(&pkt_);
2343
2344     return true;
2345 }
2346
2347 void InputMediaStream_FFMPEG::close()
2348 {
2349     if (ctx_)
2350     {
2351         #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2352             avformat_close_input(&ctx_);
2353         #else
2354             av_close_input_file(ctx_);
2355         #endif
2356     }
2357
2358     // free last packet if exist
2359     if (pkt_.data)
2360         av_free_packet(&pkt_);
2361 }
2362
2363 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2364 {
2365     // free last packet if exist
2366     if (pkt_.data)
2367         av_free_packet(&pkt_);
2368
2369     // get the next frame
2370     for (;;)
2371     {
2372         int ret = av_read_frame(ctx_, &pkt_);
2373
2374         if (ret == AVERROR(EAGAIN))
2375             continue;
2376
2377         if (ret < 0)
2378         {
2379             if (ret == (int)AVERROR_EOF)
2380                 *endOfFile = true;
2381             return false;
2382         }
2383
2384         if (pkt_.stream_index != video_stream_id_)
2385         {
2386             av_free_packet(&pkt_);
2387             continue;
2388         }
2389
2390         break;
2391     }
2392
2393     *data = pkt_.data;
2394     *size = pkt_.size;
2395     *endOfFile = false;
2396
2397     return true;
2398 }
2399
2400 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2401 {
2402     InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2403
2404     if (stream && stream->open(fileName, codec, chroma_format, width, height))
2405         return stream;
2406
2407     stream->close();
2408     free(stream);
2409
2410     return 0;
2411 }
2412
2413 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2414 {
2415     stream->close();
2416     free(stream);
2417 }
2418
2419 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2420 {
2421     return stream->read(data, size, endOfFile);
2422 }