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
371 #if (_WIN32_WINNT >= 0x0600)
372 ::InitializeCriticalSectionEx(&cs, 1000, 0);
374 ::InitializeCriticalSection(&cs);
378 void destroy() { DeleteCriticalSection(&cs); }
380 void lock() { EnterCriticalSection(&cs); }
381 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
382 void unlock() { LeaveCriticalSection(&cs); }
389 static int _interlockedExchangeAdd(int* addr, int delta)
391 #if defined _MSC_VER && _MSC_VER >= 1500
392 return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
394 return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
399 #elif defined __APPLE__
401 #include <libkern/OSAtomic.h>
403 struct ImplMutex::Impl
405 void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
408 void lock() { OSSpinLockLock(&sl); }
409 bool trylock() { return OSSpinLockTry(&sl); }
410 void unlock() { OSSpinLockUnlock(&sl); }
416 #elif defined __linux__ && !defined ANDROID
418 struct ImplMutex::Impl
420 void init() { pthread_spin_init(&sl, 0); refcount = 1; }
421 void destroy() { pthread_spin_destroy(&sl); }
423 void lock() { pthread_spin_lock(&sl); }
424 bool trylock() { return pthread_spin_trylock(&sl) == 0; }
425 void unlock() { pthread_spin_unlock(&sl); }
427 pthread_spinlock_t sl;
433 struct ImplMutex::Impl
435 void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
436 void destroy() { pthread_mutex_destroy(&sl); }
438 void lock() { pthread_mutex_lock(&sl); }
439 bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
440 void unlock() { pthread_mutex_unlock(&sl); }
448 void ImplMutex::init()
450 impl = (Impl*)malloc(sizeof(Impl));
453 void ImplMutex::destroy()
459 void ImplMutex::lock() { impl->lock(); }
460 void ImplMutex::unlock() { impl->unlock(); }
461 bool ImplMutex::trylock() { return impl->trylock(); }
463 static int LockCallBack(void **mutex, AVLockOp op)
465 ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
469 localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
480 case AV_LOCK_RELEASE:
481 localMutex->unlock();
484 case AV_LOCK_DESTROY:
485 localMutex->destroy();
493 static ImplMutex _mutex;
494 static bool _initialized = false;
496 class InternalFFMpegRegister
499 InternalFFMpegRegister()
504 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
505 avformat_network_init();
508 /* register all codecs, demux and protocols */
511 /* register a callback function for synchronization */
512 av_lockmgr_register(&LockCallBack);
514 av_log_set_level(AV_LOG_ERROR);
521 ~InternalFFMpegRegister()
523 _initialized = false;
524 av_lockmgr_register(NULL);
528 static InternalFFMpegRegister _init;
530 bool CvCapture_FFMPEG::open( const char* _filename )
537 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
538 int err = avformat_open_input(&ic, _filename, NULL, NULL);
540 int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
545 CV_WARN("Error opening file");
549 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
550 avformat_find_stream_info(ic, NULL);
552 av_find_stream_info(ic);
556 CV_WARN("Could not find codec parameters");
559 for(i = 0; i < ic->nb_streams; i++)
561 #if LIBAVFORMAT_BUILD > 4628
562 AVCodecContext *enc = ic->streams[i]->codec;
564 AVCodecContext *enc = &ic->streams[i]->codec;
567 //#ifdef FF_API_THREAD_INIT
568 // avcodec_thread_init(enc, get_number_of_cpus());
570 enc->thread_count = get_number_of_cpus();
573 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
574 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
577 if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
579 // backup encoder' width/height
580 int enc_width = enc->width;
581 int enc_height = enc->height;
583 AVCodec *codec = avcodec_find_decoder(enc->codec_id);
585 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
586 avcodec_open2(enc, codec, NULL)
588 avcodec_open(enc, codec)
593 // checking width/height (since decoder can sometimes alter it, eg. vp6f)
594 if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
595 if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
598 video_st = ic->streams[i];
599 picture = avcodec_alloc_frame();
601 rgb_picture.data[0] = (uint8_t*)malloc(
602 avpicture_get_size( PIX_FMT_BGR24,
603 enc->width, enc->height ));
604 avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
605 PIX_FMT_BGR24, enc->width, enc->height );
607 frame.width = enc->width;
608 frame.height = enc->height;
610 frame.step = rgb_picture.linesize[0];
611 frame.data = rgb_picture.data[0];
616 if(video_stream >= 0) valid = true;
627 bool CvCapture_FFMPEG::grabFrame()
633 const int max_number_of_attempts = 1 << 16;
635 if( !ic || !video_st ) return false;
637 if( ic->streams[video_stream]->nb_frames > 0 &&
638 frame_number > ic->streams[video_stream]->nb_frames )
641 av_free_packet (&packet);
643 picture_pts = AV_NOPTS_VALUE_;
645 // get the next frame
648 int ret = av_read_frame(ic, &packet);
649 if (ret == AVERROR(EAGAIN)) continue;
651 /* else if (ret < 0) break; */
653 if( packet.stream_index != video_stream )
655 av_free_packet (&packet);
657 if (count_errs > max_number_of_attempts)
662 // Decode video frame
663 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
664 avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
665 #elif LIBAVFORMAT_BUILD > 4628
666 avcodec_decode_video(video_st->codec,
667 picture, &got_picture,
668 packet.data, packet.size);
670 avcodec_decode_video(&video_st->codec,
671 picture, &got_picture,
672 packet.data, packet.size);
675 // Did we get a video frame?
678 //picture_pts = picture->best_effort_timestamp;
679 if( picture_pts == AV_NOPTS_VALUE_ )
680 picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
687 if (count_errs > max_number_of_attempts)
691 av_free_packet (&packet);
694 if( valid && first_frame_number < 0 )
695 first_frame_number = dts_to_frame_number(picture_pts);
697 // return if we have a new picture or not
702 bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
704 if( !video_st || !picture->data[0] )
707 avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
708 video_st->codec->width, video_st->codec->height);
710 if( img_convert_ctx == NULL ||
711 frame.width != video_st->codec->width ||
712 frame.height != video_st->codec->height )
714 if( img_convert_ctx )
715 sws_freeContext(img_convert_ctx);
717 frame.width = video_st->codec->width;
718 frame.height = video_st->codec->height;
720 img_convert_ctx = sws_getCachedContext(
722 video_st->codec->width, video_st->codec->height,
723 video_st->codec->pix_fmt,
724 video_st->codec->width, video_st->codec->height,
730 if (img_convert_ctx == NULL)
731 return false;//CV_Error(0, "Cannot initialize the conversion context!");
738 0, video_st->codec->height,
745 *width = frame.width;
746 *height = frame.height;
753 double CvCapture_FFMPEG::getProperty( int property_id )
755 if( !video_st ) return 0;
757 switch( property_id )
759 case CV_FFMPEG_CAP_PROP_POS_MSEC:
760 return 1000.0*(double)frame_number/get_fps();
761 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
762 return (double)frame_number;
763 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
764 return r2d(ic->streams[video_stream]->time_base);
765 case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
766 return (double)get_total_frames();
767 case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
768 return (double)frame.width;
769 case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
770 return (double)frame.height;
771 case CV_FFMPEG_CAP_PROP_FPS:
772 #if LIBAVCODEC_BUILD > 4753
773 return av_q2d(video_st->r_frame_rate);
775 return (double)video_st->codec.frame_rate
776 / (double)video_st->codec.frame_rate_base;
778 case CV_FFMPEG_CAP_PROP_FOURCC:
779 #if LIBAVFORMAT_BUILD > 4628
780 return (double)video_st->codec->codec_tag;
782 return (double)video_st->codec.codec_tag;
791 double CvCapture_FFMPEG::r2d(AVRational r)
793 return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
796 double CvCapture_FFMPEG::get_duration_sec()
798 double sec = (double)ic->duration / (double)AV_TIME_BASE;
802 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
807 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
813 int CvCapture_FFMPEG::get_bitrate()
818 double CvCapture_FFMPEG::get_fps()
820 double fps = r2d(ic->streams[video_stream]->r_frame_rate);
822 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
825 fps = r2d(ic->streams[video_stream]->avg_frame_rate);
831 fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
837 int64_t CvCapture_FFMPEG::get_total_frames()
839 int64_t nbf = ic->streams[video_stream]->nb_frames;
843 nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
848 int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
850 double sec = dts_to_sec(dts);
851 return (int64_t)(get_fps() * sec + 0.5);
854 double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
856 return (double)(dts - ic->streams[video_stream]->start_time) *
857 r2d(ic->streams[video_stream]->time_base);
860 void CvCapture_FFMPEG::seek(int64_t _frame_number)
862 _frame_number = std::min(_frame_number, get_total_frames());
865 // if we have not grabbed a single frame before first seek, let's read the first frame
866 // and get some valuable information during the process
867 if( first_frame_number < 0 && get_total_frames() > 1 )
872 int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
873 double sec = (double)_frame_number_temp / get_fps();
874 int64_t time_stamp = ic->streams[video_stream]->start_time;
875 double time_base = r2d(ic->streams[video_stream]->time_base);
876 time_stamp += (int64_t)(sec / time_base + 0.5);
877 if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
878 avcodec_flush_buffers(ic->streams[video_stream]->codec);
879 if( _frame_number > 0 )
883 if( _frame_number > 1 )
885 frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
886 //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
887 // (int)_frame_number, (int)frame_number, delta);
889 if( frame_number < 0 || frame_number > _frame_number-1 )
891 if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
893 delta = delta < 16 ? delta*2 : delta*3/2;
896 while( frame_number < _frame_number-1 )
918 void CvCapture_FFMPEG::seek(double sec)
920 seek((int64_t)(sec * get_fps() + 0.5));
923 bool CvCapture_FFMPEG::setProperty( int property_id, double value )
925 if( !video_st ) return false;
927 switch( property_id )
929 case CV_FFMPEG_CAP_PROP_POS_MSEC:
930 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
931 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
933 switch( property_id )
935 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
936 seek((int64_t)value);
939 case CV_FFMPEG_CAP_PROP_POS_MSEC:
943 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
944 seek((int64_t)(value*ic->duration));
948 picture_pts=(int64_t)value;
959 ///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
960 struct CvVideoWriter_FFMPEG
962 bool open( const char* filename, int fourcc,
963 double fps, int width, int height, bool isColor );
965 bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
969 AVOutputFormat * fmt;
970 AVFormatContext * oc;
972 uint32_t outbuf_size;
975 AVFrame * input_picture;
979 Image_FFMPEG temp_image;
980 int frame_width, frame_height;
982 struct SwsContext *img_convert_ctx;
985 static const char * icvFFMPEGErrStr(int err)
987 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
989 case AVERROR_BSF_NOT_FOUND:
990 return "Bitstream filter not found";
991 case AVERROR_DECODER_NOT_FOUND:
992 return "Decoder not found";
993 case AVERROR_DEMUXER_NOT_FOUND:
994 return "Demuxer not found";
995 case AVERROR_ENCODER_NOT_FOUND:
996 return "Encoder not found";
998 return "End of file";
1000 return "Immediate exit was requested; the called function should not be restarted";
1001 case AVERROR_FILTER_NOT_FOUND:
1002 return "Filter not found";
1003 case AVERROR_INVALIDDATA:
1004 return "Invalid data found when processing input";
1005 case AVERROR_MUXER_NOT_FOUND:
1006 return "Muxer not found";
1007 case AVERROR_OPTION_NOT_FOUND:
1008 return "Option not found";
1009 case AVERROR_PATCHWELCOME:
1010 return "Not yet implemented in FFmpeg, patches welcome";
1011 case AVERROR_PROTOCOL_NOT_FOUND:
1012 return "Protocol not found";
1013 case AVERROR_STREAM_NOT_FOUND:
1014 return "Stream not found";
1020 case AVERROR_NUMEXPECTED:
1021 return "Incorrect filename syntax";
1022 case AVERROR_INVALIDDATA:
1023 return "Invalid data in header";
1025 return "Unknown format";
1027 return "I/O error occurred";
1029 return "Memory allocation error";
1035 return "Unspecified error";
1038 /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1040 enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1043 void CvVideoWriter_FFMPEG::init()
1055 memset(&temp_image, 0, sizeof(temp_image));
1056 img_convert_ctx = 0;
1057 frame_width = frame_height = 0;
1062 * the following function is a modified version of code
1063 * found in ffmpeg-0.4.9-pre1/output_example.c
1065 static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1068 uint8_t * picture_buf;
1071 picture = avcodec_alloc_frame();
1074 size = avpicture_get_size( (PixelFormat) pix_fmt, width, height);
1076 picture_buf = (uint8_t *) malloc(size);
1082 avpicture_fill((AVPicture *)picture, picture_buf,
1083 (PixelFormat) pix_fmt, width, height);
1090 /* add a video output stream to the container */
1091 static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1092 CV_CODEC_ID codec_id,
1093 int w, int h, int bitrate,
1094 double fps, int pixel_format)
1098 int frame_rate, frame_rate_base;
1101 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1102 st = avformat_new_stream(oc, 0);
1104 st = av_new_stream(oc, 0);
1108 CV_WARN("Could not allocate stream");
1112 #if LIBAVFORMAT_BUILD > 4628
1118 #if LIBAVFORMAT_BUILD > 4621
1119 c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1121 c->codec_id = oc->oformat->video_codec;
1124 if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1125 c->codec_id = codec_id;
1128 //if(codec_tag) c->codec_tag=codec_tag;
1129 codec = avcodec_find_encoder(c->codec_id);
1131 c->codec_type = AVMEDIA_TYPE_VIDEO;
1133 /* put sample parameters */
1134 int64_t lbit_rate = (int64_t)bitrate;
1135 lbit_rate += (bitrate / 2);
1136 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1137 c->bit_rate = lbit_rate;
1140 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1143 /* resolution must be a multiple of two */
1147 /* time base: this is the fundamental unit of time (in seconds) in terms
1148 of which frame timestamps are represented. for fixed-fps content,
1149 timebase should be 1/framerate and timestamp increments should be
1151 frame_rate=(int)(fps+0.5);
1153 while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
1154 frame_rate_base*=10;
1155 frame_rate=(int)(fps*frame_rate_base + 0.5);
1157 #if LIBAVFORMAT_BUILD > 4752
1158 c->time_base.den = frame_rate;
1159 c->time_base.num = frame_rate_base;
1160 /* adjust time base for supported framerates */
1161 if(codec && codec->supported_framerates){
1162 const AVRational *p= codec->supported_framerates;
1163 AVRational req = {frame_rate, frame_rate_base};
1164 const AVRational *best=NULL;
1165 AVRational best_error= {INT_MAX, 1};
1166 for(; p->den!=0; p++){
1167 AVRational error= av_sub_q(req, *p);
1168 if(error.num <0) error.num *= -1;
1169 if(av_cmp_q(error, best_error) < 0){
1174 c->time_base.den= best->num;
1175 c->time_base.num= best->den;
1178 c->frame_rate = frame_rate;
1179 c->frame_rate_base = frame_rate_base;
1182 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1183 c->pix_fmt = (PixelFormat) pixel_format;
1185 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1186 c->max_b_frames = 2;
1188 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1189 /* needed to avoid using macroblocks in which some coeffs overflow
1190 this doesnt happen with normal video, it just happens here as the
1191 motion of the chroma plane doesnt match the luma plane */
1192 /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1195 #if LIBAVCODEC_VERSION_INT>0x000409
1196 // some formats want stream headers to be seperate
1197 if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1199 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1206 static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1208 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture )
1210 #if LIBAVFORMAT_BUILD > 4628
1211 AVCodecContext * c = video_st->codec;
1213 AVCodecContext * c = &(video_st->codec);
1218 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
1219 /* raw video case. The API will change slightly in the near
1222 av_init_packet(&pkt);
1224 #ifndef PKT_FLAG_KEY
1225 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
1228 pkt.flags |= PKT_FLAG_KEY;
1229 pkt.stream_index= video_st->index;
1230 pkt.data= (uint8_t *)picture;
1231 pkt.size= sizeof(AVPicture);
1233 ret = av_write_frame(oc, &pkt);
1235 /* encode the image */
1236 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1237 /* if zero size, it means the image was buffered */
1240 av_init_packet(&pkt);
1242 #if LIBAVFORMAT_BUILD > 4752
1243 if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1244 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1246 pkt.pts = c->coded_frame->pts;
1248 if(c->coded_frame->key_frame)
1249 pkt.flags |= PKT_FLAG_KEY;
1250 pkt.stream_index= video_st->index;
1254 /* write the compressed frame in the media file */
1255 ret = av_write_frame(oc, &pkt);
1257 ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1263 /// write a frame with FFMPEG
1264 bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1268 if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1270 width = frame_width;
1271 height = frame_height;
1273 // typecast from opaque data type to implemented struct
1274 #if LIBAVFORMAT_BUILD > 4628
1275 AVCodecContext *c = video_st->codec;
1277 AVCodecContext *c = &(video_st->codec);
1280 #if LIBAVFORMAT_BUILD < 5231
1281 // It is not needed in the latest versions of the ffmpeg
1282 if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
1284 if( !temp_image.data )
1286 temp_image.step = (width*cn + 3) & -4;
1287 temp_image.width = width;
1288 temp_image.height = height;
1290 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1292 for( int y = 0; y < height; y++ )
1293 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
1294 data = temp_image.data;
1295 step = temp_image.step;
1298 if( width*cn != step )
1300 if( !temp_image.data )
1302 temp_image.step = width*cn;
1303 temp_image.width = width;
1304 temp_image.height = height;
1306 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1309 for( int y = 0; y < height; y++ )
1310 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
1312 for( int y = 0; y < height; y++ )
1313 memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
1314 data = temp_image.data;
1315 step = temp_image.step;
1320 if (input_pix_fmt == PIX_FMT_BGR24) {
1325 else if (input_pix_fmt == PIX_FMT_GRAY8) {
1334 if ( c->pix_fmt != input_pix_fmt ) {
1335 assert( input_picture );
1336 // let input_picture point to the raw data buffer of 'image'
1337 avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1338 (PixelFormat)input_pix_fmt, width, height);
1340 if( !img_convert_ctx )
1342 img_convert_ctx = sws_getContext(width,
1344 (PixelFormat)input_pix_fmt,
1350 if( !img_convert_ctx )
1354 if ( sws_scale(img_convert_ctx, input_picture->data,
1355 input_picture->linesize, 0,
1357 picture->data, picture->linesize) < 0 )
1361 avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1362 (PixelFormat)input_pix_fmt, width, height);
1365 ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1370 /// close video output stream and free associated memory
1371 void CvVideoWriter_FFMPEG::close()
1373 // nothing to do if already released
1377 /* no more frame to compress. The codec has a latency of a few
1378 frames if using B frames, so we get the last frames by
1379 passing the same picture again */
1380 // TODO -- do we need to account for latency here?
1382 /* write the trailer, if any */
1385 if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1389 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1390 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1394 av_write_trailer(oc);
1397 if( img_convert_ctx )
1399 sws_freeContext(img_convert_ctx);
1400 img_convert_ctx = 0;
1404 #if LIBAVFORMAT_BUILD > 4628
1405 if( video_st->codec->pix_fmt != input_pix_fmt)
1407 if( video_st->codec.pix_fmt != input_pix_fmt)
1410 if(picture->data[0])
1411 free(picture->data[0]);
1412 picture->data[0] = 0;
1417 av_free(input_picture);
1420 #if LIBAVFORMAT_BUILD > 4628
1421 avcodec_close(video_st->codec);
1423 avcodec_close(&(video_st->codec));
1428 if (!(fmt->flags & AVFMT_NOFILE))
1430 /* close the output file */
1432 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1433 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1436 url_fclose(&oc->pb);
1444 /* free the stream */
1445 avformat_free_context(oc);
1447 if( temp_image.data )
1449 free(temp_image.data);
1450 temp_image.data = 0;
1456 /// Create a video writer object that uses FFMPEG
1457 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1458 double fps, int width, int height, bool is_color )
1460 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1461 int err, codec_pix_fmt;
1462 double bitrate_scale = 1;
1472 // we allow frames of odd width or height, but in this case we truncate
1473 // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1474 // but some internal functions inside FFMPEG swscale require even width/height.
1477 if( width <= 0 || height <= 0 )
1480 /* auto detect the output format from the name and fourcc code. */
1482 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1483 fmt = av_guess_format(NULL, filename, NULL);
1485 fmt = guess_format(NULL, filename, NULL);
1491 /* determine optimal pixel format */
1493 input_pix_fmt = PIX_FMT_BGR24;
1496 input_pix_fmt = PIX_FMT_GRAY8;
1499 /* Lookup codec_id for given fourcc */
1500 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1501 if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1504 const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
1505 if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1509 // alloc memory for context
1510 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1511 oc = avformat_alloc_context();
1513 oc = av_alloc_format_context();
1519 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1521 /* set some options */
1522 oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
1524 // set a few optimal pixel formats for lossless codecs of interest..
1526 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1527 case CV_CODEC(CODEC_ID_JPEGLS):
1528 // BGR24 or GRAY8 depending on is_color...
1529 codec_pix_fmt = input_pix_fmt;
1532 case CV_CODEC(CODEC_ID_HUFFYUV):
1533 codec_pix_fmt = PIX_FMT_YUV422P;
1535 case CV_CODEC(CODEC_ID_MJPEG):
1536 case CV_CODEC(CODEC_ID_LJPEG):
1537 codec_pix_fmt = PIX_FMT_YUVJ420P;
1540 case CV_CODEC(CODEC_ID_RAWVIDEO):
1541 codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
1542 input_pix_fmt == PIX_FMT_GRAY16LE ||
1543 input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
1546 // good for lossy formats, MPEG, etc.
1547 codec_pix_fmt = PIX_FMT_YUV420P;
1551 double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1553 // TODO -- safe to ignore output audio stream?
1554 video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1555 width, height, (int)(bitrate + 0.5),
1556 fps, codec_pix_fmt);
1558 /* set the output parameters (must be done even if no
1560 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1561 if (av_set_parameters(oc, NULL) < 0) {
1567 #if FF_API_DUMP_FORMAT
1568 dump_format(oc, 0, filename, 1);
1570 av_dump_format(oc, 0, filename, 1);
1574 /* now that all the parameters are set, we can open the audio and
1575 video codecs and allocate the necessary encode buffers */
1583 #if LIBAVFORMAT_BUILD > 4628
1584 c = (video_st->codec);
1586 c = &(video_st->codec);
1589 c->codec_tag = fourcc;
1590 /* find the video encoder */
1591 codec = avcodec_find_encoder(c->codec_id);
1593 fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
1594 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1595 AVERROR_ENCODER_NOT_FOUND
1603 int64_t lbit_rate = (int64_t)c->bit_rate;
1604 lbit_rate += (bitrate / 2);
1605 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1606 c->bit_rate_tolerance = (int)lbit_rate;
1607 c->bit_rate = (int)lbit_rate;
1609 /* open the codec */
1611 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1612 avcodec_open2(c, codec, NULL)
1614 avcodec_open(c, codec)
1617 fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
1623 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1624 /* allocate output buffer */
1625 /* assume we will never get codec output with more than 4 bytes per pixel... */
1626 outbuf_size = width*height*4;
1627 outbuf = (uint8_t *) av_malloc(outbuf_size);
1630 bool need_color_convert;
1631 need_color_convert = (c->pix_fmt != input_pix_fmt);
1633 /* allocate the encoded raw picture */
1634 picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1639 /* if the output format is not our input format, then a temporary
1640 picture of the input format is needed too. It is then converted
1641 to the required output format */
1642 input_picture = NULL;
1643 if ( need_color_convert ) {
1644 input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1645 if (!input_picture) {
1650 /* open the output file, if needed */
1651 if (!(fmt->flags & AVFMT_NOFILE)) {
1652 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1653 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1655 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1662 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1663 /* write the stream header, if any */
1664 err=avformat_write_header(oc, NULL);
1666 err=av_write_header( oc );
1675 frame_width = width;
1676 frame_height = height;
1684 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1686 CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1688 if( capture->open( filename ))
1697 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1699 if( capture && *capture )
1701 (*capture)->close();
1707 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1709 return capture->setProperty(prop_id, value);
1712 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1714 return capture->getProperty(prop_id);
1717 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1719 return capture->grabFrame();
1722 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1724 return capture->retrieveFrame(0, data, step, width, height, cn);
1727 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1728 int width, int height, int isColor )
1730 CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1732 if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1739 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1741 if( writer && *writer )
1750 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1751 const unsigned char* data, int step,
1752 int width, int height, int cn, int origin)
1754 return writer->writeFrame(data, step, width, height, cn, origin);
1763 struct OutputMediaStream_FFMPEG
1765 bool open(const char* fileName, int width, int height, double fps);
1768 void write(unsigned char* data, int size, int keyFrame);
1770 // add a video output stream to the container
1771 static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
1773 AVOutputFormat* fmt_;
1774 AVFormatContext* oc_;
1775 AVStream* video_st_;
1778 void OutputMediaStream_FFMPEG::close()
1780 // no more frame to compress. The codec has a latency of a few
1781 // frames if using B frames, so we get the last frames by
1782 // passing the same picture again
1784 // TODO -- do we need to account for latency here?
1788 // write the trailer, if any
1789 av_write_trailer(oc_);
1792 for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1794 av_freep(&oc_->streams[i]->codec);
1795 av_freep(&oc_->streams[i]);
1798 if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
1800 // close the output file
1802 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1803 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1804 url_fclose(oc_->pb);
1806 url_fclose(&oc_->pb);
1809 avio_close(oc_->pb);
1818 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
1820 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1821 AVStream* st = avformat_new_stream(oc, 0);
1823 AVStream* st = av_new_stream(oc, 0);
1828 #if LIBAVFORMAT_BUILD > 4628
1829 AVCodecContext* c = st->codec;
1831 AVCodecContext* c = &(st->codec);
1834 c->codec_id = codec_id;
1835 c->codec_type = AVMEDIA_TYPE_VIDEO;
1837 // put sample parameters
1838 unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
1839 lbit_rate += (bitrate / 4);
1840 lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
1841 c->bit_rate = bitrate;
1844 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1847 // resolution must be a multiple of two
1851 AVCodec* codec = avcodec_find_encoder(c->codec_id);
1853 // time base: this is the fundamental unit of time (in seconds) in terms
1854 // of which frame timestamps are represented. for fixed-fps content,
1855 // timebase should be 1/framerate and timestamp increments should be
1858 int frame_rate = static_cast<int>(fps+0.5);
1859 int frame_rate_base = 1;
1860 while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
1862 frame_rate_base *= 10;
1863 frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
1865 c->time_base.den = frame_rate;
1866 c->time_base.num = frame_rate_base;
1868 #if LIBAVFORMAT_BUILD > 4752
1869 // adjust time base for supported framerates
1870 if (codec && codec->supported_framerates)
1872 AVRational req = {frame_rate, frame_rate_base};
1873 const AVRational* best = NULL;
1874 AVRational best_error = {INT_MAX, 1};
1876 for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
1878 AVRational error = av_sub_q(req, *p);
1883 if (av_cmp_q(error, best_error) < 0)
1890 c->time_base.den= best->num;
1891 c->time_base.num= best->den;
1895 c->gop_size = 12; // emit one intra frame every twelve frames at most
1896 c->pix_fmt = pixel_format;
1898 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
1899 c->max_b_frames = 2;
1901 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
1903 // needed to avoid using macroblocks in which some coeffs overflow
1904 // this doesnt happen with normal video, it just happens here as the
1905 // motion of the chroma plane doesnt match the luma plane
1907 // avoid FFMPEG warning 'clipping 1 dct coefficients...'
1912 #if LIBAVCODEC_VERSION_INT > 0x000409
1913 // some formats want stream headers to be seperate
1914 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1916 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1923 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
1929 // auto detect the output format from the name and fourcc code
1930 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1931 fmt_ = av_guess_format(NULL, fileName, NULL);
1933 fmt_ = guess_format(NULL, fileName, NULL);
1938 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
1940 // alloc memory for context
1941 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1942 oc_ = avformat_alloc_context();
1944 oc_ = av_alloc_format_context();
1950 oc_->oformat = fmt_;
1951 snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
1953 oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
1955 // set a few optimal pixel formats for lossless codecs of interest..
1956 PixelFormat codec_pix_fmt = PIX_FMT_YUV420P;
1957 int bitrate_scale = 64;
1959 // TODO -- safe to ignore output audio stream?
1960 video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
1964 // set the output parameters (must be done even if no parameters)
1965 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1966 if (av_set_parameters(oc_, NULL) < 0)
1970 // now that all the parameters are set, we can open the audio and
1971 // video codecs and allocate the necessary encode buffers
1973 #if LIBAVFORMAT_BUILD > 4628
1974 AVCodecContext* c = (video_st_->codec);
1976 AVCodecContext* c = &(video_st_->codec);
1979 c->codec_tag = MKTAG('H', '2', '6', '4');
1980 c->bit_rate_tolerance = c->bit_rate;
1982 // open the output file, if needed
1983 if (!(fmt_->flags & AVFMT_NOFILE))
1985 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1986 int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
1988 int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
1995 // write the stream header, if any
1996 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1997 av_write_header(oc_);
1999 avformat_write_header(oc_, NULL);
2005 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2007 // if zero size, it means the image was buffered
2011 av_init_packet(&pkt);
2014 pkt.flags |= PKT_FLAG_KEY;
2016 pkt.stream_index = video_st_->index;
2020 // write the compressed frame in the media file
2021 av_write_frame(oc_, &pkt);
2025 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2027 OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2029 if (stream->open(fileName, width, height, fps))
2038 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2044 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2046 stream->write(data, size, keyFrame);
2055 VideoCodec_MPEG1 = 0,
2061 VideoCodec_H264_SVC,
2062 VideoCodec_H264_MVC,
2065 VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2066 VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2067 VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2068 VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2069 VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')), // UYVY (4:2:2)
2074 VideoChromaFormat_Monochrome = 0,
2075 VideoChromaFormat_YUV420,
2076 VideoChromaFormat_YUV422,
2077 VideoChromaFormat_YUV444,
2080 struct InputMediaStream_FFMPEG
2083 bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2086 bool read(unsigned char** data, int* size, int* endOfFile);
2089 InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2090 InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2092 AVFormatContext* ctx_;
2093 int video_stream_id_;
2097 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2102 video_stream_id_ = -1;
2103 memset(&pkt_, 0, sizeof(AVPacket));
2105 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2106 avformat_network_init();
2109 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2110 err = avformat_open_input(&ctx_, fileName, 0, 0);
2112 err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2117 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2118 err = avformat_find_stream_info(ctx_, 0);
2120 err = av_find_stream_info(ctx_);
2125 for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2127 #if LIBAVFORMAT_BUILD > 4628
2128 AVCodecContext *enc = ctx_->streams[i]->codec;
2130 AVCodecContext *enc = &ctx_->streams[i]->codec;
2133 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2135 video_stream_id_ = static_cast<int>(i);
2137 switch (enc->codec_id)
2139 case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2140 *codec = ::VideoCodec_MPEG1;
2143 case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2144 *codec = ::VideoCodec_MPEG2;
2147 case CV_CODEC(CODEC_ID_MPEG4):
2148 *codec = ::VideoCodec_MPEG4;
2151 case CV_CODEC(CODEC_ID_VC1):
2152 *codec = ::VideoCodec_VC1;
2155 case CV_CODEC(CODEC_ID_H264):
2156 *codec = ::VideoCodec_H264;
2163 switch (enc->pix_fmt)
2165 case PIX_FMT_YUV420P:
2166 *chroma_format = ::VideoChromaFormat_YUV420;
2169 case PIX_FMT_YUV422P:
2170 *chroma_format = ::VideoChromaFormat_YUV422;
2173 case PIX_FMT_YUV444P:
2174 *chroma_format = ::VideoChromaFormat_YUV444;
2181 *width = enc->coded_width;
2182 *height = enc->coded_height;
2188 if (video_stream_id_ < 0)
2191 av_init_packet(&pkt_);
2196 void InputMediaStream_FFMPEG::close()
2200 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2201 avformat_close_input(&ctx_);
2203 av_close_input_file(ctx_);
2207 // free last packet if exist
2209 av_free_packet(&pkt_);
2212 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2214 // free last packet if exist
2216 av_free_packet(&pkt_);
2218 // get the next frame
2221 int ret = av_read_frame(ctx_, &pkt_);
2223 if (ret == AVERROR(EAGAIN))
2228 if (ret == (int)AVERROR_EOF)
2233 if (pkt_.stream_index != video_stream_id_)
2235 av_free_packet(&pkt_);
2249 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2251 InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2253 if (stream && stream->open(fileName, codec, chroma_format, width, height))
2262 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2268 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2270 return stream->read(data, size, endOfFile);