1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "cap_ffmpeg_api.hpp"
48 #if defined _MSC_VER && _MSC_VER >= 1200
49 #pragma warning( disable: 4244 4510 4512 4610 )
53 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
60 #include "ffmpeg_codecs.hpp"
62 #include <libavutil/mathematics.h>
65 #define HAVE_FFMPEG_SWSCALE 1
66 #include <libavcodec/avcodec.h>
67 #include <libswscale/swscale.h>
70 #ifndef HAVE_FFMPEG_SWSCALE
71 #error "libswscale is necessary to build the newer OpenCV ffmpeg wrapper"
74 // if the header path is not specified explicitly, let's deduce it
75 #if !defined HAVE_FFMPEG_AVCODEC_H && !defined HAVE_LIBAVCODEC_AVCODEC_H
77 #if defined(HAVE_GENTOO_FFMPEG)
78 #define HAVE_LIBAVCODEC_AVCODEC_H 1
79 #if defined(HAVE_FFMPEG_SWSCALE)
80 #define HAVE_LIBSWSCALE_SWSCALE_H 1
82 #elif defined HAVE_FFMPEG
83 #define HAVE_FFMPEG_AVCODEC_H 1
84 #if defined(HAVE_FFMPEG_SWSCALE)
85 #define HAVE_FFMPEG_SWSCALE_H 1
91 #if defined(HAVE_FFMPEG_AVCODEC_H)
92 #include <ffmpeg/avcodec.h>
94 #if defined(HAVE_FFMPEG_SWSCALE_H)
95 #include <ffmpeg/swscale.h>
98 #if defined(HAVE_LIBAVCODEC_AVCODEC_H)
99 #include <libavcodec/avcodec.h>
101 #if defined(HAVE_LIBSWSCALE_SWSCALE_H)
102 #include <libswscale/swscale.h>
111 #if defined _MSC_VER && _MSC_VER >= 1200
112 #pragma warning( default: 4244 4510 4512 4610 )
116 #define CV_WARN(message)
118 #define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
121 /* PIX_FMT_RGBA32 macro changed in newer ffmpeg versions */
122 #ifndef PIX_FMT_RGBA32
123 #define PIX_FMT_RGBA32 PIX_FMT_RGB32
126 #define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )
128 #if defined WIN32 || defined _WIN32
130 #elif defined __linux__ || defined __APPLE__
133 #include <sys/types.h>
134 #include <sys/sysctl.h>
138 #define MIN(a, b) ((a) < (b) ? (a) : (b))
141 #if defined(__APPLE__)
142 #define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
144 #define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
148 #define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
151 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
152 # define CV_CODEC_ID AVCodecID
153 # define CV_CODEC(name) AV_##name
155 # define CV_CODEC_ID CodecID
156 # define CV_CODEC(name) name
159 static int get_number_of_cpus(void)
161 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
163 #elif defined WIN32 || defined _WIN32
165 GetSystemInfo( &sysinfo );
167 return (int)sysinfo.dwNumberOfProcessors;
168 #elif defined __linux__
169 return (int)sysconf( _SC_NPROCESSORS_ONLN );
170 #elif defined __APPLE__
173 size_t len = sizeof(numCPU);
175 // set the mib for hw.ncpu
177 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
179 // get the number of CPUs from the system
180 sysctl(mib, 2, &numCPU, &len, NULL, 0);
185 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
208 inline void _opencv_ffmpeg_free(void** ptr)
215 struct CvCapture_FFMPEG
217 bool open( const char* filename );
220 double getProperty(int);
221 bool setProperty(int, double);
223 bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
227 void seek(int64_t frame_number);
228 void seek(double sec);
229 bool slowSeek( int framenumber );
231 int64_t get_total_frames();
232 double get_duration_sec();
236 double r2d(AVRational r);
237 int64_t dts_to_frame_number(int64_t dts);
238 double dts_to_sec(int64_t dts);
240 AVFormatContext * ic;
250 struct SwsContext *img_convert_ctx;
252 int64_t frame_number, first_frame_number;
256 'filename' contains the filename of the videosource,
257 'filename==NULL' indicates that ffmpeg's seek support works
258 for the particular file.
259 'filename!=NULL' indicates that the slow fallback function is used for seeking,
260 and so the filename is needed to reopen the file on backward seeking.
265 void CvCapture_FFMPEG::init()
271 picture_pts = AV_NOPTS_VALUE_;
272 first_frame_number = -1;
273 memset( &rgb_picture, 0, sizeof(rgb_picture) );
274 memset( &frame, 0, sizeof(frame) );
276 memset(&packet, 0, sizeof(packet));
277 av_init_packet(&packet);
286 void CvCapture_FFMPEG::close()
288 if( img_convert_ctx )
290 sws_freeContext(img_convert_ctx);
299 #if LIBAVFORMAT_BUILD > 4628
300 avcodec_close( video_st->codec );
303 avcodec_close( &(video_st->codec) );
311 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
312 av_close_input_file(ic);
314 avformat_close_input(&ic);
320 if( rgb_picture.data[0] )
322 free( rgb_picture.data[0] );
323 rgb_picture.data[0] = 0;
326 // free last packet if exist
328 av_free_packet (&packet);
336 #ifndef AVSEEK_FLAG_FRAME
337 #define AVSEEK_FLAG_FRAME 0
339 #ifndef AVSEEK_FLAG_ANY
340 #define AVSEEK_FLAG_ANY 1
346 ImplMutex() { init(); }
347 ~ImplMutex() { destroy(); }
361 ImplMutex(const ImplMutex&);
362 ImplMutex& operator = (const ImplMutex& m);
365 #if defined WIN32 || defined _WIN32 || defined WINCE
367 struct ImplMutex::Impl
369 void init() { InitializeCriticalSection(&cs); refcount = 1; }
370 void destroy() { DeleteCriticalSection(&cs); }
372 void lock() { EnterCriticalSection(&cs); }
373 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
374 void unlock() { LeaveCriticalSection(&cs); }
381 static int _interlockedExchangeAdd(int* addr, int delta)
383 #if defined _MSC_VER && _MSC_VER >= 1500
384 return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
386 return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
391 #elif defined __APPLE__
393 #include <libkern/OSAtomic.h>
395 struct ImplMutex::Impl
397 void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
400 void lock() { OSSpinLockLock(&sl); }
401 bool trylock() { return OSSpinLockTry(&sl); }
402 void unlock() { OSSpinLockUnlock(&sl); }
408 #elif defined __linux__ && !defined ANDROID
410 struct ImplMutex::Impl
412 void init() { pthread_spin_init(&sl, 0); refcount = 1; }
413 void destroy() { pthread_spin_destroy(&sl); }
415 void lock() { pthread_spin_lock(&sl); }
416 bool trylock() { return pthread_spin_trylock(&sl) == 0; }
417 void unlock() { pthread_spin_unlock(&sl); }
419 pthread_spinlock_t sl;
425 struct ImplMutex::Impl
427 void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
428 void destroy() { pthread_mutex_destroy(&sl); }
430 void lock() { pthread_mutex_lock(&sl); }
431 bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
432 void unlock() { pthread_mutex_unlock(&sl); }
440 void ImplMutex::init()
442 impl = (Impl*)malloc(sizeof(Impl));
445 void ImplMutex::destroy()
451 void ImplMutex::lock() { impl->lock(); }
452 void ImplMutex::unlock() { impl->unlock(); }
453 bool ImplMutex::trylock() { return impl->trylock(); }
455 static int LockCallBack(void **mutex, AVLockOp op)
457 ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
461 localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
472 case AV_LOCK_RELEASE:
473 localMutex->unlock();
476 case AV_LOCK_DESTROY:
477 localMutex->destroy();
485 static ImplMutex _mutex;
486 static bool _initialized = false;
488 class InternalFFMpegRegister
491 InternalFFMpegRegister()
496 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
497 avformat_network_init();
500 /* register all codecs, demux and protocols */
503 /* register a callback function for synchronization */
504 av_lockmgr_register(&LockCallBack);
506 av_log_set_level(AV_LOG_ERROR);
513 ~InternalFFMpegRegister()
515 _initialized = false;
516 av_lockmgr_register(NULL);
520 static InternalFFMpegRegister _init;
522 bool CvCapture_FFMPEG::open( const char* _filename )
529 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
530 int err = avformat_open_input(&ic, _filename, NULL, NULL);
532 int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
537 CV_WARN("Error opening file");
541 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 3, 0)
542 avformat_find_stream_info(ic, NULL);
544 av_find_stream_info(ic);
548 CV_WARN("Could not find codec parameters");
551 for(i = 0; i < ic->nb_streams; i++)
553 #if LIBAVFORMAT_BUILD > 4628
554 AVCodecContext *enc = ic->streams[i]->codec;
556 AVCodecContext *enc = &ic->streams[i]->codec;
559 //#ifdef FF_API_THREAD_INIT
560 // avcodec_thread_init(enc, get_number_of_cpus());
562 enc->thread_count = get_number_of_cpus();
565 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
566 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
569 if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
571 // backup encoder' width/height
572 int enc_width = enc->width;
573 int enc_height = enc->height;
575 AVCodec *codec = avcodec_find_decoder(enc->codec_id);
577 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
578 avcodec_open2(enc, codec, NULL)
580 avcodec_open(enc, codec)
585 // checking width/height (since decoder can sometimes alter it, eg. vp6f)
586 if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
587 if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
590 video_st = ic->streams[i];
591 picture = avcodec_alloc_frame();
593 rgb_picture.data[0] = (uint8_t*)malloc(
594 avpicture_get_size( PIX_FMT_BGR24,
595 enc->width, enc->height ));
596 avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
597 PIX_FMT_BGR24, enc->width, enc->height );
599 frame.width = enc->width;
600 frame.height = enc->height;
602 frame.step = rgb_picture.linesize[0];
603 frame.data = rgb_picture.data[0];
608 if(video_stream >= 0) valid = true;
619 bool CvCapture_FFMPEG::grabFrame()
625 const int max_number_of_attempts = 1 << 16;
627 if( !ic || !video_st ) return false;
629 if( ic->streams[video_stream]->nb_frames > 0 &&
630 frame_number > ic->streams[video_stream]->nb_frames )
633 av_free_packet (&packet);
635 picture_pts = AV_NOPTS_VALUE_;
637 // get the next frame
640 int ret = av_read_frame(ic, &packet);
641 if (ret == AVERROR(EAGAIN)) continue;
643 /* else if (ret < 0) break; */
645 if( packet.stream_index != video_stream )
647 av_free_packet (&packet);
649 if (count_errs > max_number_of_attempts)
654 // Decode video frame
655 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
656 avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
657 #elif LIBAVFORMAT_BUILD > 4628
658 avcodec_decode_video(video_st->codec,
659 picture, &got_picture,
660 packet.data, packet.size);
662 avcodec_decode_video(&video_st->codec,
663 picture, &got_picture,
664 packet.data, packet.size);
667 // Did we get a video frame?
670 //picture_pts = picture->best_effort_timestamp;
671 if( picture_pts == AV_NOPTS_VALUE_ )
672 picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
679 if (count_errs > max_number_of_attempts)
683 av_free_packet (&packet);
686 if( valid && first_frame_number < 0 )
687 first_frame_number = dts_to_frame_number(picture_pts);
689 // return if we have a new picture or not
694 bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
696 if( !video_st || !picture->data[0] )
699 avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
700 video_st->codec->width, video_st->codec->height);
702 if( img_convert_ctx == NULL ||
703 frame.width != video_st->codec->width ||
704 frame.height != video_st->codec->height )
706 if( img_convert_ctx )
707 sws_freeContext(img_convert_ctx);
709 frame.width = video_st->codec->width;
710 frame.height = video_st->codec->height;
712 img_convert_ctx = sws_getCachedContext(
714 video_st->codec->width, video_st->codec->height,
715 video_st->codec->pix_fmt,
716 video_st->codec->width, video_st->codec->height,
722 if (img_convert_ctx == NULL)
723 return false;//CV_Error(0, "Cannot initialize the conversion context!");
730 0, video_st->codec->height,
737 *width = frame.width;
738 *height = frame.height;
745 double CvCapture_FFMPEG::getProperty( int property_id )
747 if( !video_st ) return 0;
749 switch( property_id )
751 case CV_FFMPEG_CAP_PROP_POS_MSEC:
752 return 1000.0*(double)frame_number/get_fps();
753 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
754 return (double)frame_number;
755 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
756 return r2d(ic->streams[video_stream]->time_base);
757 case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
758 return (double)get_total_frames();
759 case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
760 return (double)frame.width;
761 case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
762 return (double)frame.height;
763 case CV_FFMPEG_CAP_PROP_FPS:
764 #if LIBAVCODEC_BUILD > 4753
765 return av_q2d(video_st->r_frame_rate);
767 return (double)video_st->codec.frame_rate
768 / (double)video_st->codec.frame_rate_base;
770 case CV_FFMPEG_CAP_PROP_FOURCC:
771 #if LIBAVFORMAT_BUILD > 4628
772 return (double)video_st->codec->codec_tag;
774 return (double)video_st->codec.codec_tag;
783 double CvCapture_FFMPEG::r2d(AVRational r)
785 return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
788 double CvCapture_FFMPEG::get_duration_sec()
790 double sec = (double)ic->duration / (double)AV_TIME_BASE;
794 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
799 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
805 int CvCapture_FFMPEG::get_bitrate()
810 double CvCapture_FFMPEG::get_fps()
812 double fps = r2d(ic->streams[video_stream]->r_frame_rate);
814 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
817 fps = r2d(ic->streams[video_stream]->avg_frame_rate);
823 fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
829 int64_t CvCapture_FFMPEG::get_total_frames()
831 int64_t nbf = ic->streams[video_stream]->nb_frames;
835 nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
840 int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
842 double sec = dts_to_sec(dts);
843 return (int64_t)(get_fps() * sec + 0.5);
846 double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
848 return (double)(dts - ic->streams[video_stream]->start_time) *
849 r2d(ic->streams[video_stream]->time_base);
852 void CvCapture_FFMPEG::seek(int64_t _frame_number)
854 _frame_number = std::min(_frame_number, get_total_frames());
857 // if we have not grabbed a single frame before first seek, let's read the first frame
858 // and get some valuable information during the process
859 if( first_frame_number < 0 && get_total_frames() > 1 )
864 int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
865 double sec = (double)_frame_number_temp / get_fps();
866 int64_t time_stamp = ic->streams[video_stream]->start_time;
867 double time_base = r2d(ic->streams[video_stream]->time_base);
868 time_stamp += (int64_t)(sec / time_base + 0.5);
869 if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
870 avcodec_flush_buffers(ic->streams[video_stream]->codec);
871 if( _frame_number > 0 )
875 if( _frame_number > 1 )
877 frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
878 //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
879 // (int)_frame_number, (int)frame_number, delta);
881 if( frame_number < 0 || frame_number > _frame_number-1 )
883 if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
885 delta = delta < 16 ? delta*2 : delta*3/2;
888 while( frame_number < _frame_number-1 )
910 void CvCapture_FFMPEG::seek(double sec)
912 seek((int64_t)(sec * get_fps() + 0.5));
915 bool CvCapture_FFMPEG::setProperty( int property_id, double value )
917 if( !video_st ) return false;
919 switch( property_id )
921 case CV_FFMPEG_CAP_PROP_POS_MSEC:
922 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
923 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
925 switch( property_id )
927 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
928 seek((int64_t)value);
931 case CV_FFMPEG_CAP_PROP_POS_MSEC:
935 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
936 seek((int64_t)(value*ic->duration));
940 picture_pts=(int64_t)value;
951 ///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
952 struct CvVideoWriter_FFMPEG
954 bool open( const char* filename, int fourcc,
955 double fps, int width, int height, bool isColor );
957 bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
961 AVOutputFormat * fmt;
962 AVFormatContext * oc;
964 uint32_t outbuf_size;
967 AVFrame * input_picture;
971 Image_FFMPEG temp_image;
972 int frame_width, frame_height;
974 struct SwsContext *img_convert_ctx;
977 static const char * icvFFMPEGErrStr(int err)
979 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
981 case AVERROR_BSF_NOT_FOUND:
982 return "Bitstream filter not found";
983 case AVERROR_DECODER_NOT_FOUND:
984 return "Decoder not found";
985 case AVERROR_DEMUXER_NOT_FOUND:
986 return "Demuxer not found";
987 case AVERROR_ENCODER_NOT_FOUND:
988 return "Encoder not found";
990 return "End of file";
992 return "Immediate exit was requested; the called function should not be restarted";
993 case AVERROR_FILTER_NOT_FOUND:
994 return "Filter not found";
995 case AVERROR_INVALIDDATA:
996 return "Invalid data found when processing input";
997 case AVERROR_MUXER_NOT_FOUND:
998 return "Muxer not found";
999 case AVERROR_OPTION_NOT_FOUND:
1000 return "Option not found";
1001 case AVERROR_PATCHWELCOME:
1002 return "Not yet implemented in FFmpeg, patches welcome";
1003 case AVERROR_PROTOCOL_NOT_FOUND:
1004 return "Protocol not found";
1005 case AVERROR_STREAM_NOT_FOUND:
1006 return "Stream not found";
1012 case AVERROR_NUMEXPECTED:
1013 return "Incorrect filename syntax";
1014 case AVERROR_INVALIDDATA:
1015 return "Invalid data in header";
1017 return "Unknown format";
1019 return "I/O error occurred";
1021 return "Memory allocation error";
1027 return "Unspecified error";
1030 /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1032 enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1035 void CvVideoWriter_FFMPEG::init()
1047 memset(&temp_image, 0, sizeof(temp_image));
1048 img_convert_ctx = 0;
1049 frame_width = frame_height = 0;
1054 * the following function is a modified version of code
1055 * found in ffmpeg-0.4.9-pre1/output_example.c
1057 static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1060 uint8_t * picture_buf;
1063 picture = avcodec_alloc_frame();
1066 size = avpicture_get_size( (PixelFormat) pix_fmt, width, height);
1068 picture_buf = (uint8_t *) malloc(size);
1074 avpicture_fill((AVPicture *)picture, picture_buf,
1075 (PixelFormat) pix_fmt, width, height);
1082 /* add a video output stream to the container */
1083 static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1084 CV_CODEC_ID codec_id,
1085 int w, int h, int bitrate,
1086 double fps, int pixel_format)
1090 int frame_rate, frame_rate_base;
1093 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1094 st = avformat_new_stream(oc, 0);
1096 st = av_new_stream(oc, 0);
1100 CV_WARN("Could not allocate stream");
1104 #if LIBAVFORMAT_BUILD > 4628
1110 #if LIBAVFORMAT_BUILD > 4621
1111 c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1113 c->codec_id = oc->oformat->video_codec;
1116 if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1117 c->codec_id = codec_id;
1120 //if(codec_tag) c->codec_tag=codec_tag;
1121 codec = avcodec_find_encoder(c->codec_id);
1123 c->codec_type = AVMEDIA_TYPE_VIDEO;
1125 /* put sample parameters */
1126 int64_t lbit_rate = (int64_t)bitrate;
1127 lbit_rate += (bitrate / 2);
1128 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1129 c->bit_rate = lbit_rate;
1132 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1135 /* resolution must be a multiple of two */
1139 /* time base: this is the fundamental unit of time (in seconds) in terms
1140 of which frame timestamps are represented. for fixed-fps content,
1141 timebase should be 1/framerate and timestamp increments should be
1143 frame_rate=(int)(fps+0.5);
1145 while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
1146 frame_rate_base*=10;
1147 frame_rate=(int)(fps*frame_rate_base + 0.5);
1149 #if LIBAVFORMAT_BUILD > 4752
1150 c->time_base.den = frame_rate;
1151 c->time_base.num = frame_rate_base;
1152 /* adjust time base for supported framerates */
1153 if(codec && codec->supported_framerates){
1154 const AVRational *p= codec->supported_framerates;
1155 AVRational req = {frame_rate, frame_rate_base};
1156 const AVRational *best=NULL;
1157 AVRational best_error= {INT_MAX, 1};
1158 for(; p->den!=0; p++){
1159 AVRational error= av_sub_q(req, *p);
1160 if(error.num <0) error.num *= -1;
1161 if(av_cmp_q(error, best_error) < 0){
1166 c->time_base.den= best->num;
1167 c->time_base.num= best->den;
1170 c->frame_rate = frame_rate;
1171 c->frame_rate_base = frame_rate_base;
1174 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1175 c->pix_fmt = (PixelFormat) pixel_format;
1177 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1178 c->max_b_frames = 2;
1180 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1181 /* needed to avoid using macroblocks in which some coeffs overflow
1182 this doesnt happen with normal video, it just happens here as the
1183 motion of the chroma plane doesnt match the luma plane */
1184 /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1187 #if LIBAVCODEC_VERSION_INT>0x000409
1188 // some formats want stream headers to be seperate
1189 if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1191 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1198 static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1200 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture )
1202 #if LIBAVFORMAT_BUILD > 4628
1203 AVCodecContext * c = video_st->codec;
1205 AVCodecContext * c = &(video_st->codec);
1210 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
1211 /* raw video case. The API will change slightly in the near
1214 av_init_packet(&pkt);
1216 #ifndef PKT_FLAG_KEY
1217 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
1220 pkt.flags |= PKT_FLAG_KEY;
1221 pkt.stream_index= video_st->index;
1222 pkt.data= (uint8_t *)picture;
1223 pkt.size= sizeof(AVPicture);
1225 ret = av_write_frame(oc, &pkt);
1227 /* encode the image */
1228 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1229 /* if zero size, it means the image was buffered */
1232 av_init_packet(&pkt);
1234 #if LIBAVFORMAT_BUILD > 4752
1235 if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1236 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1238 pkt.pts = c->coded_frame->pts;
1240 if(c->coded_frame->key_frame)
1241 pkt.flags |= PKT_FLAG_KEY;
1242 pkt.stream_index= video_st->index;
1246 /* write the compressed frame in the media file */
1247 ret = av_write_frame(oc, &pkt);
1249 ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1255 /// write a frame with FFMPEG
1256 bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1260 if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1262 width = frame_width;
1263 height = frame_height;
1265 // typecast from opaque data type to implemented struct
1266 #if LIBAVFORMAT_BUILD > 4628
1267 AVCodecContext *c = video_st->codec;
1269 AVCodecContext *c = &(video_st->codec);
1272 #if LIBAVFORMAT_BUILD < 5231
1273 // It is not needed in the latest versions of the ffmpeg
1274 if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
1276 if( !temp_image.data )
1278 temp_image.step = (width*cn + 3) & -4;
1279 temp_image.width = width;
1280 temp_image.height = height;
1282 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1284 for( int y = 0; y < height; y++ )
1285 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
1286 data = temp_image.data;
1287 step = temp_image.step;
1290 if( width*cn != step )
1292 if( !temp_image.data )
1294 temp_image.step = width*cn;
1295 temp_image.width = width;
1296 temp_image.height = height;
1298 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1301 for( int y = 0; y < height; y++ )
1302 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
1304 for( int y = 0; y < height; y++ )
1305 memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
1306 data = temp_image.data;
1307 step = temp_image.step;
1312 if (input_pix_fmt == PIX_FMT_BGR24) {
1317 else if (input_pix_fmt == PIX_FMT_GRAY8) {
1326 if ( c->pix_fmt != input_pix_fmt ) {
1327 assert( input_picture );
1328 // let input_picture point to the raw data buffer of 'image'
1329 avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1330 (PixelFormat)input_pix_fmt, width, height);
1332 if( !img_convert_ctx )
1334 img_convert_ctx = sws_getContext(width,
1336 (PixelFormat)input_pix_fmt,
1342 if( !img_convert_ctx )
1346 if ( sws_scale(img_convert_ctx, input_picture->data,
1347 input_picture->linesize, 0,
1349 picture->data, picture->linesize) < 0 )
1353 avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1354 (PixelFormat)input_pix_fmt, width, height);
1357 ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1362 /// close video output stream and free associated memory
1363 void CvVideoWriter_FFMPEG::close()
1365 // nothing to do if already released
1369 /* no more frame to compress. The codec has a latency of a few
1370 frames if using B frames, so we get the last frames by
1371 passing the same picture again */
1372 // TODO -- do we need to account for latency here?
1374 /* write the trailer, if any */
1377 if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1381 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1382 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1386 av_write_trailer(oc);
1389 if( img_convert_ctx )
1391 sws_freeContext(img_convert_ctx);
1392 img_convert_ctx = 0;
1396 #if LIBAVFORMAT_BUILD > 4628
1397 if( video_st->codec->pix_fmt != input_pix_fmt)
1399 if( video_st->codec.pix_fmt != input_pix_fmt)
1402 if(picture->data[0])
1403 free(picture->data[0]);
1404 picture->data[0] = 0;
1409 av_free(input_picture);
1412 #if LIBAVFORMAT_BUILD > 4628
1413 avcodec_close(video_st->codec);
1415 avcodec_close(&(video_st->codec));
1420 if (!(fmt->flags & AVFMT_NOFILE))
1422 /* close the output file */
1424 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1425 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1428 url_fclose(&oc->pb);
1436 /* free the stream */
1437 avformat_free_context(oc);
1439 if( temp_image.data )
1441 free(temp_image.data);
1442 temp_image.data = 0;
1448 /// Create a video writer object that uses FFMPEG
1449 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1450 double fps, int width, int height, bool is_color )
1452 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1453 int err, codec_pix_fmt;
1454 double bitrate_scale = 1;
1464 // we allow frames of odd width or height, but in this case we truncate
1465 // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1466 // but some internal functions inside FFMPEG swscale require even width/height.
1469 if( width <= 0 || height <= 0 )
1472 /* auto detect the output format from the name and fourcc code. */
1474 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1475 fmt = av_guess_format(NULL, filename, NULL);
1477 fmt = guess_format(NULL, filename, NULL);
1483 /* determine optimal pixel format */
1485 input_pix_fmt = PIX_FMT_BGR24;
1488 input_pix_fmt = PIX_FMT_GRAY8;
1491 /* Lookup codec_id for given fourcc */
1492 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1493 if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1496 const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
1497 if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1501 // alloc memory for context
1502 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1503 oc = avformat_alloc_context();
1505 oc = av_alloc_format_context();
1511 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1513 /* set some options */
1514 oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
1516 // set a few optimal pixel formats for lossless codecs of interest..
1518 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1519 case CV_CODEC(CODEC_ID_JPEGLS):
1520 // BGR24 or GRAY8 depending on is_color...
1521 codec_pix_fmt = input_pix_fmt;
1524 case CV_CODEC(CODEC_ID_HUFFYUV):
1525 codec_pix_fmt = PIX_FMT_YUV422P;
1527 case CV_CODEC(CODEC_ID_MJPEG):
1528 case CV_CODEC(CODEC_ID_LJPEG):
1529 codec_pix_fmt = PIX_FMT_YUVJ420P;
1532 case CV_CODEC(CODEC_ID_RAWVIDEO):
1533 codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
1534 input_pix_fmt == PIX_FMT_GRAY16LE ||
1535 input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
1538 // good for lossy formats, MPEG, etc.
1539 codec_pix_fmt = PIX_FMT_YUV420P;
1543 double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1545 // TODO -- safe to ignore output audio stream?
1546 video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1547 width, height, (int)(bitrate + 0.5),
1548 fps, codec_pix_fmt);
1550 /* set the output parameters (must be done even if no
1552 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1553 if (av_set_parameters(oc, NULL) < 0) {
1559 #if FF_API_DUMP_FORMAT
1560 dump_format(oc, 0, filename, 1);
1562 av_dump_format(oc, 0, filename, 1);
1566 /* now that all the parameters are set, we can open the audio and
1567 video codecs and allocate the necessary encode buffers */
1575 #if LIBAVFORMAT_BUILD > 4628
1576 c = (video_st->codec);
1578 c = &(video_st->codec);
1581 c->codec_tag = fourcc;
1582 /* find the video encoder */
1583 codec = avcodec_find_encoder(c->codec_id);
1585 fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
1586 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1587 AVERROR_ENCODER_NOT_FOUND
1595 int64_t lbit_rate = (int64_t)c->bit_rate;
1596 lbit_rate += (bitrate / 2);
1597 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1598 c->bit_rate_tolerance = (int)lbit_rate;
1599 c->bit_rate = (int)lbit_rate;
1601 /* open the codec */
1603 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1604 avcodec_open2(c, codec, NULL)
1606 avcodec_open(c, codec)
1609 fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
1615 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1616 /* allocate output buffer */
1617 /* assume we will never get codec output with more than 4 bytes per pixel... */
1618 outbuf_size = width*height*4;
1619 outbuf = (uint8_t *) av_malloc(outbuf_size);
1622 bool need_color_convert;
1623 need_color_convert = (c->pix_fmt != input_pix_fmt);
1625 /* allocate the encoded raw picture */
1626 picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1631 /* if the output format is not our input format, then a temporary
1632 picture of the input format is needed too. It is then converted
1633 to the required output format */
1634 input_picture = NULL;
1635 if ( need_color_convert ) {
1636 input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1637 if (!input_picture) {
1642 /* open the output file, if needed */
1643 if (!(fmt->flags & AVFMT_NOFILE)) {
1644 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1645 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1647 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1654 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1655 /* write the stream header, if any */
1656 err=avformat_write_header(oc, NULL);
1658 err=av_write_header( oc );
1667 frame_width = width;
1668 frame_height = height;
1676 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1678 CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1680 if( capture->open( filename ))
1689 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1691 if( capture && *capture )
1693 (*capture)->close();
1699 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1701 return capture->setProperty(prop_id, value);
1704 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1706 return capture->getProperty(prop_id);
1709 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1711 return capture->grabFrame();
1714 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1716 return capture->retrieveFrame(0, data, step, width, height, cn);
1719 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1720 int width, int height, int isColor )
1722 CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1724 if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1731 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1733 if( writer && *writer )
1742 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1743 const unsigned char* data, int step,
1744 int width, int height, int cn, int origin)
1746 return writer->writeFrame(data, step, width, height, cn, origin);
1755 struct OutputMediaStream_FFMPEG
1757 bool open(const char* fileName, int width, int height, double fps);
1760 void write(unsigned char* data, int size, int keyFrame);
1762 // add a video output stream to the container
1763 static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
1765 AVOutputFormat* fmt_;
1766 AVFormatContext* oc_;
1767 AVStream* video_st_;
1770 void OutputMediaStream_FFMPEG::close()
1772 // no more frame to compress. The codec has a latency of a few
1773 // frames if using B frames, so we get the last frames by
1774 // passing the same picture again
1776 // TODO -- do we need to account for latency here?
1780 // write the trailer, if any
1781 av_write_trailer(oc_);
1784 for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1786 av_freep(&oc_->streams[i]->codec);
1787 av_freep(&oc_->streams[i]);
1790 if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
1792 // close the output file
1794 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1795 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1796 url_fclose(oc_->pb);
1798 url_fclose(&oc_->pb);
1801 avio_close(oc_->pb);
1810 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
1812 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1813 AVStream* st = avformat_new_stream(oc, 0);
1815 AVStream* st = av_new_stream(oc, 0);
1820 #if LIBAVFORMAT_BUILD > 4628
1821 AVCodecContext* c = st->codec;
1823 AVCodecContext* c = &(st->codec);
1826 c->codec_id = codec_id;
1827 c->codec_type = AVMEDIA_TYPE_VIDEO;
1829 // put sample parameters
1830 unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
1831 lbit_rate += (bitrate / 4);
1832 lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
1833 c->bit_rate = bitrate;
1836 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1839 // resolution must be a multiple of two
1843 AVCodec* codec = avcodec_find_encoder(c->codec_id);
1845 // time base: this is the fundamental unit of time (in seconds) in terms
1846 // of which frame timestamps are represented. for fixed-fps content,
1847 // timebase should be 1/framerate and timestamp increments should be
1850 int frame_rate = static_cast<int>(fps+0.5);
1851 int frame_rate_base = 1;
1852 while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
1854 frame_rate_base *= 10;
1855 frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
1857 c->time_base.den = frame_rate;
1858 c->time_base.num = frame_rate_base;
1860 #if LIBAVFORMAT_BUILD > 4752
1861 // adjust time base for supported framerates
1862 if (codec && codec->supported_framerates)
1864 AVRational req = {frame_rate, frame_rate_base};
1865 const AVRational* best = NULL;
1866 AVRational best_error = {INT_MAX, 1};
1868 for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
1870 AVRational error = av_sub_q(req, *p);
1875 if (av_cmp_q(error, best_error) < 0)
1882 c->time_base.den= best->num;
1883 c->time_base.num= best->den;
1887 c->gop_size = 12; // emit one intra frame every twelve frames at most
1888 c->pix_fmt = pixel_format;
1890 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
1891 c->max_b_frames = 2;
1893 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
1895 // needed to avoid using macroblocks in which some coeffs overflow
1896 // this doesnt happen with normal video, it just happens here as the
1897 // motion of the chroma plane doesnt match the luma plane
1899 // avoid FFMPEG warning 'clipping 1 dct coefficients...'
1904 #if LIBAVCODEC_VERSION_INT > 0x000409
1905 // some formats want stream headers to be seperate
1906 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1908 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1915 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
1921 // auto detect the output format from the name and fourcc code
1922 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1923 fmt_ = av_guess_format(NULL, fileName, NULL);
1925 fmt_ = guess_format(NULL, fileName, NULL);
1930 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
1932 // alloc memory for context
1933 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1934 oc_ = avformat_alloc_context();
1936 oc_ = av_alloc_format_context();
1942 oc_->oformat = fmt_;
1943 snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
1945 oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
1947 // set a few optimal pixel formats for lossless codecs of interest..
1948 PixelFormat codec_pix_fmt = PIX_FMT_YUV420P;
1949 int bitrate_scale = 64;
1951 // TODO -- safe to ignore output audio stream?
1952 video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
1956 // set the output parameters (must be done even if no parameters)
1957 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1958 if (av_set_parameters(oc_, NULL) < 0)
1962 // now that all the parameters are set, we can open the audio and
1963 // video codecs and allocate the necessary encode buffers
1965 #if LIBAVFORMAT_BUILD > 4628
1966 AVCodecContext* c = (video_st_->codec);
1968 AVCodecContext* c = &(video_st_->codec);
1971 c->codec_tag = MKTAG('H', '2', '6', '4');
1972 c->bit_rate_tolerance = c->bit_rate;
1974 // open the output file, if needed
1975 if (!(fmt_->flags & AVFMT_NOFILE))
1977 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1978 int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
1980 int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
1987 // write the stream header, if any
1988 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1989 av_write_header(oc_);
1991 avformat_write_header(oc_, NULL);
1997 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
1999 // if zero size, it means the image was buffered
2003 av_init_packet(&pkt);
2006 pkt.flags |= PKT_FLAG_KEY;
2008 pkt.stream_index = video_st_->index;
2012 // write the compressed frame in the media file
2013 av_write_frame(oc_, &pkt);
2017 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2019 OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2021 if (stream->open(fileName, width, height, fps))
2030 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2036 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2038 stream->write(data, size, keyFrame);
2047 VideoCodec_MPEG1 = 0,
2053 VideoCodec_H264_SVC,
2054 VideoCodec_H264_MVC,
2057 VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2058 VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2059 VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2060 VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2061 VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')), // UYVY (4:2:2)
2066 VideoChromaFormat_Monochrome = 0,
2067 VideoChromaFormat_YUV420,
2068 VideoChromaFormat_YUV422,
2069 VideoChromaFormat_YUV444,
2072 struct InputMediaStream_FFMPEG
2075 bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2078 bool read(unsigned char** data, int* size, int* endOfFile);
2081 InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2082 InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2084 AVFormatContext* ctx_;
2085 int video_stream_id_;
2089 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2094 video_stream_id_ = -1;
2095 memset(&pkt_, 0, sizeof(AVPacket));
2097 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2098 avformat_network_init();
2101 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2102 err = avformat_open_input(&ctx_, fileName, 0, 0);
2104 err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2109 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 3, 0)
2110 err = avformat_find_stream_info(ctx_, 0);
2112 err = av_find_stream_info(ctx_);
2117 for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2119 #if LIBAVFORMAT_BUILD > 4628
2120 AVCodecContext *enc = ctx_->streams[i]->codec;
2122 AVCodecContext *enc = &ctx_->streams[i]->codec;
2125 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2127 video_stream_id_ = static_cast<int>(i);
2129 switch (enc->codec_id)
2131 case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2132 *codec = ::VideoCodec_MPEG1;
2135 case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2136 *codec = ::VideoCodec_MPEG2;
2139 case CV_CODEC(CODEC_ID_MPEG4):
2140 *codec = ::VideoCodec_MPEG4;
2143 case CV_CODEC(CODEC_ID_VC1):
2144 *codec = ::VideoCodec_VC1;
2147 case CV_CODEC(CODEC_ID_H264):
2148 *codec = ::VideoCodec_H264;
2155 switch (enc->pix_fmt)
2157 case PIX_FMT_YUV420P:
2158 *chroma_format = ::VideoChromaFormat_YUV420;
2161 case PIX_FMT_YUV422P:
2162 *chroma_format = ::VideoChromaFormat_YUV422;
2165 case PIX_FMT_YUV444P:
2166 *chroma_format = ::VideoChromaFormat_YUV444;
2173 *width = enc->coded_width;
2174 *height = enc->coded_height;
2180 if (video_stream_id_ < 0)
2183 av_init_packet(&pkt_);
2188 void InputMediaStream_FFMPEG::close()
2192 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2193 avformat_close_input(&ctx_);
2195 av_close_input_file(ctx_);
2199 // free last packet if exist
2201 av_free_packet(&pkt_);
2204 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2206 // free last packet if exist
2208 av_free_packet(&pkt_);
2210 // get the next frame
2213 int ret = av_read_frame(ctx_, &pkt_);
2215 if (ret == AVERROR(EAGAIN))
2220 if (ret == (int)AVERROR_EOF)
2225 if (pkt_.stream_index != video_stream_id_)
2227 av_free_packet(&pkt_);
2241 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2243 InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2245 if (stream && stream->open(fileName, codec, chroma_format, width, height))
2254 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2260 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2262 return stream->read(data, size, endOfFile);