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()
1367 // nothing to do if already released
1371 /* no more frame to compress. The codec has a latency of a few
1372 frames if using B frames, so we get the last frames by
1373 passing the same picture again */
1374 // TODO -- do we need to account for latency here?
1376 /* write the trailer, if any */
1379 if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1383 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1384 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1388 av_write_trailer(oc);
1391 if( img_convert_ctx )
1393 sws_freeContext(img_convert_ctx);
1394 img_convert_ctx = 0;
1398 #if LIBAVFORMAT_BUILD > 4628
1399 if( video_st->codec->pix_fmt != input_pix_fmt)
1401 if( video_st->codec.pix_fmt != input_pix_fmt)
1404 if(picture->data[0])
1405 free(picture->data[0]);
1406 picture->data[0] = 0;
1411 av_free(input_picture);
1414 #if LIBAVFORMAT_BUILD > 4628
1415 avcodec_close(video_st->codec);
1417 avcodec_close(&(video_st->codec));
1422 /* free the streams */
1423 for(i = 0; i < oc->nb_streams; i++)
1425 av_freep(&oc->streams[i]->codec);
1426 av_freep(&oc->streams[i]);
1429 if (!(fmt->flags & AVFMT_NOFILE))
1431 /* close the output file */
1433 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1434 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1437 url_fclose(&oc->pb);
1445 /* free the stream */
1448 if( temp_image.data )
1450 free(temp_image.data);
1451 temp_image.data = 0;
1457 /// Create a video writer object that uses FFMPEG
1458 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1459 double fps, int width, int height, bool is_color )
1461 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1462 int err, codec_pix_fmt;
1463 double bitrate_scale = 1;
1473 // we allow frames of odd width or height, but in this case we truncate
1474 // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1475 // but some internal functions inside FFMPEG swscale require even width/height.
1478 if( width <= 0 || height <= 0 )
1481 /* auto detect the output format from the name and fourcc code. */
1483 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1484 fmt = av_guess_format(NULL, filename, NULL);
1486 fmt = guess_format(NULL, filename, NULL);
1492 /* determine optimal pixel format */
1494 input_pix_fmt = PIX_FMT_BGR24;
1497 input_pix_fmt = PIX_FMT_GRAY8;
1500 /* Lookup codec_id for given fourcc */
1501 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1502 if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1505 const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
1506 if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1510 // alloc memory for context
1511 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1512 oc = avformat_alloc_context();
1514 oc = av_alloc_format_context();
1520 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1522 /* set some options */
1523 oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
1525 // set a few optimal pixel formats for lossless codecs of interest..
1527 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1528 case CV_CODEC(CODEC_ID_JPEGLS):
1529 // BGR24 or GRAY8 depending on is_color...
1530 codec_pix_fmt = input_pix_fmt;
1533 case CV_CODEC(CODEC_ID_HUFFYUV):
1534 codec_pix_fmt = PIX_FMT_YUV422P;
1536 case CV_CODEC(CODEC_ID_MJPEG):
1537 case CV_CODEC(CODEC_ID_LJPEG):
1538 codec_pix_fmt = PIX_FMT_YUVJ420P;
1541 case CV_CODEC(CODEC_ID_RAWVIDEO):
1542 codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
1543 input_pix_fmt == PIX_FMT_GRAY16LE ||
1544 input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
1547 // good for lossy formats, MPEG, etc.
1548 codec_pix_fmt = PIX_FMT_YUV420P;
1552 double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1554 // TODO -- safe to ignore output audio stream?
1555 video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1556 width, height, (int)(bitrate + 0.5),
1557 fps, codec_pix_fmt);
1559 /* set the output parameters (must be done even if no
1561 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1562 if (av_set_parameters(oc, NULL) < 0) {
1568 #if FF_API_DUMP_FORMAT
1569 dump_format(oc, 0, filename, 1);
1571 av_dump_format(oc, 0, filename, 1);
1575 /* now that all the parameters are set, we can open the audio and
1576 video codecs and allocate the necessary encode buffers */
1584 #if LIBAVFORMAT_BUILD > 4628
1585 c = (video_st->codec);
1587 c = &(video_st->codec);
1590 c->codec_tag = fourcc;
1591 /* find the video encoder */
1592 codec = avcodec_find_encoder(c->codec_id);
1594 fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
1595 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1596 AVERROR_ENCODER_NOT_FOUND
1604 int64_t lbit_rate = (int64_t)c->bit_rate;
1605 lbit_rate += (bitrate / 2);
1606 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1607 c->bit_rate_tolerance = (int)lbit_rate;
1608 c->bit_rate = (int)lbit_rate;
1610 /* open the codec */
1612 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1613 avcodec_open2(c, codec, NULL)
1615 avcodec_open(c, codec)
1618 fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
1624 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1625 /* allocate output buffer */
1626 /* assume we will never get codec output with more than 4 bytes per pixel... */
1627 outbuf_size = width*height*4;
1628 outbuf = (uint8_t *) av_malloc(outbuf_size);
1631 bool need_color_convert;
1632 need_color_convert = (c->pix_fmt != input_pix_fmt);
1634 /* allocate the encoded raw picture */
1635 picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1640 /* if the output format is not our input format, then a temporary
1641 picture of the input format is needed too. It is then converted
1642 to the required output format */
1643 input_picture = NULL;
1644 if ( need_color_convert ) {
1645 input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1646 if (!input_picture) {
1651 /* open the output file, if needed */
1652 if (!(fmt->flags & AVFMT_NOFILE)) {
1653 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1654 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1656 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1663 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1664 /* write the stream header, if any */
1665 err=avformat_write_header(oc, NULL);
1667 err=av_write_header( oc );
1676 frame_width = width;
1677 frame_height = height;
1685 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1687 CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1689 if( capture->open( filename ))
1698 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1700 if( capture && *capture )
1702 (*capture)->close();
1708 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1710 return capture->setProperty(prop_id, value);
1713 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1715 return capture->getProperty(prop_id);
1718 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1720 return capture->grabFrame();
1723 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1725 return capture->retrieveFrame(0, data, step, width, height, cn);
1728 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1729 int width, int height, int isColor )
1731 CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1733 if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1740 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1742 if( writer && *writer )
1751 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1752 const unsigned char* data, int step,
1753 int width, int height, int cn, int origin)
1755 return writer->writeFrame(data, step, width, height, cn, origin);
1764 struct OutputMediaStream_FFMPEG
1766 bool open(const char* fileName, int width, int height, double fps);
1769 void write(unsigned char* data, int size, int keyFrame);
1771 // add a video output stream to the container
1772 static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
1774 AVOutputFormat* fmt_;
1775 AVFormatContext* oc_;
1776 AVStream* video_st_;
1779 void OutputMediaStream_FFMPEG::close()
1781 // no more frame to compress. The codec has a latency of a few
1782 // frames if using B frames, so we get the last frames by
1783 // passing the same picture again
1785 // TODO -- do we need to account for latency here?
1789 // write the trailer, if any
1790 av_write_trailer(oc_);
1793 for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1795 av_freep(&oc_->streams[i]->codec);
1796 av_freep(&oc_->streams[i]);
1799 if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
1801 // close the output file
1803 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1804 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1805 url_fclose(oc_->pb);
1807 url_fclose(&oc_->pb);
1810 avio_close(oc_->pb);
1819 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
1821 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1822 AVStream* st = avformat_new_stream(oc, 0);
1824 AVStream* st = av_new_stream(oc, 0);
1829 #if LIBAVFORMAT_BUILD > 4628
1830 AVCodecContext* c = st->codec;
1832 AVCodecContext* c = &(st->codec);
1835 c->codec_id = codec_id;
1836 c->codec_type = AVMEDIA_TYPE_VIDEO;
1838 // put sample parameters
1839 unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
1840 lbit_rate += (bitrate / 4);
1841 lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
1842 c->bit_rate = bitrate;
1845 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1848 // resolution must be a multiple of two
1852 AVCodec* codec = avcodec_find_encoder(c->codec_id);
1854 // time base: this is the fundamental unit of time (in seconds) in terms
1855 // of which frame timestamps are represented. for fixed-fps content,
1856 // timebase should be 1/framerate and timestamp increments should be
1859 int frame_rate = static_cast<int>(fps+0.5);
1860 int frame_rate_base = 1;
1861 while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
1863 frame_rate_base *= 10;
1864 frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
1866 c->time_base.den = frame_rate;
1867 c->time_base.num = frame_rate_base;
1869 #if LIBAVFORMAT_BUILD > 4752
1870 // adjust time base for supported framerates
1871 if (codec && codec->supported_framerates)
1873 AVRational req = {frame_rate, frame_rate_base};
1874 const AVRational* best = NULL;
1875 AVRational best_error = {INT_MAX, 1};
1877 for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
1879 AVRational error = av_sub_q(req, *p);
1884 if (av_cmp_q(error, best_error) < 0)
1891 c->time_base.den= best->num;
1892 c->time_base.num= best->den;
1896 c->gop_size = 12; // emit one intra frame every twelve frames at most
1897 c->pix_fmt = pixel_format;
1899 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
1900 c->max_b_frames = 2;
1902 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
1904 // needed to avoid using macroblocks in which some coeffs overflow
1905 // this doesnt happen with normal video, it just happens here as the
1906 // motion of the chroma plane doesnt match the luma plane
1908 // avoid FFMPEG warning 'clipping 1 dct coefficients...'
1913 #if LIBAVCODEC_VERSION_INT > 0x000409
1914 // some formats want stream headers to be seperate
1915 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1917 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1924 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
1930 // auto detect the output format from the name and fourcc code
1931 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1932 fmt_ = av_guess_format(NULL, fileName, NULL);
1934 fmt_ = guess_format(NULL, fileName, NULL);
1939 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
1941 // alloc memory for context
1942 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1943 oc_ = avformat_alloc_context();
1945 oc_ = av_alloc_format_context();
1951 oc_->oformat = fmt_;
1952 snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
1954 oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
1956 // set a few optimal pixel formats for lossless codecs of interest..
1957 PixelFormat codec_pix_fmt = PIX_FMT_YUV420P;
1958 int bitrate_scale = 64;
1960 // TODO -- safe to ignore output audio stream?
1961 video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
1965 // set the output parameters (must be done even if no parameters)
1966 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1967 if (av_set_parameters(oc_, NULL) < 0)
1971 // now that all the parameters are set, we can open the audio and
1972 // video codecs and allocate the necessary encode buffers
1974 #if LIBAVFORMAT_BUILD > 4628
1975 AVCodecContext* c = (video_st_->codec);
1977 AVCodecContext* c = &(video_st_->codec);
1980 c->codec_tag = MKTAG('H', '2', '6', '4');
1981 c->bit_rate_tolerance = c->bit_rate;
1983 // open the output file, if needed
1984 if (!(fmt_->flags & AVFMT_NOFILE))
1986 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1987 int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
1989 int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
1996 // write the stream header, if any
1997 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1998 av_write_header(oc_);
2000 avformat_write_header(oc_, NULL);
2006 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2008 // if zero size, it means the image was buffered
2012 av_init_packet(&pkt);
2015 pkt.flags |= PKT_FLAG_KEY;
2017 pkt.stream_index = video_st_->index;
2021 // write the compressed frame in the media file
2022 av_write_frame(oc_, &pkt);
2026 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2028 OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2030 if (stream->open(fileName, width, height, fps))
2039 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2045 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2047 stream->write(data, size, keyFrame);
2056 VideoCodec_MPEG1 = 0,
2062 VideoCodec_H264_SVC,
2063 VideoCodec_H264_MVC,
2066 VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2067 VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2068 VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2069 VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2070 VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')), // UYVY (4:2:2)
2075 VideoChromaFormat_Monochrome = 0,
2076 VideoChromaFormat_YUV420,
2077 VideoChromaFormat_YUV422,
2078 VideoChromaFormat_YUV444,
2081 struct InputMediaStream_FFMPEG
2084 bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2087 bool read(unsigned char** data, int* size, int* endOfFile);
2090 InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2091 InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2093 AVFormatContext* ctx_;
2094 int video_stream_id_;
2098 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2103 video_stream_id_ = -1;
2104 memset(&pkt_, 0, sizeof(AVPacket));
2106 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2107 avformat_network_init();
2110 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2111 err = avformat_open_input(&ctx_, fileName, 0, 0);
2113 err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2118 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 3, 0)
2119 err = avformat_find_stream_info(ctx_, 0);
2121 err = av_find_stream_info(ctx_);
2126 for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2128 #if LIBAVFORMAT_BUILD > 4628
2129 AVCodecContext *enc = ctx_->streams[i]->codec;
2131 AVCodecContext *enc = &ctx_->streams[i]->codec;
2134 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2136 video_stream_id_ = static_cast<int>(i);
2138 switch (enc->codec_id)
2140 case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2141 *codec = ::VideoCodec_MPEG1;
2144 case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2145 *codec = ::VideoCodec_MPEG2;
2148 case CV_CODEC(CODEC_ID_MPEG4):
2149 *codec = ::VideoCodec_MPEG4;
2152 case CV_CODEC(CODEC_ID_VC1):
2153 *codec = ::VideoCodec_VC1;
2156 case CV_CODEC(CODEC_ID_H264):
2157 *codec = ::VideoCodec_H264;
2164 switch (enc->pix_fmt)
2166 case PIX_FMT_YUV420P:
2167 *chroma_format = ::VideoChromaFormat_YUV420;
2170 case PIX_FMT_YUV422P:
2171 *chroma_format = ::VideoChromaFormat_YUV422;
2174 case PIX_FMT_YUV444P:
2175 *chroma_format = ::VideoChromaFormat_YUV444;
2182 *width = enc->coded_width;
2183 *height = enc->coded_height;
2189 if (video_stream_id_ < 0)
2192 av_init_packet(&pkt_);
2197 void InputMediaStream_FFMPEG::close()
2201 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2202 avformat_close_input(&ctx_);
2204 av_close_input_file(ctx_);
2208 // free last packet if exist
2210 av_free_packet(&pkt_);
2213 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2215 // free last packet if exist
2217 av_free_packet(&pkt_);
2219 // get the next frame
2222 int ret = av_read_frame(ctx_, &pkt_);
2224 if (ret == AVERROR(EAGAIN))
2229 if (ret == (int)AVERROR_EOF)
2234 if (pkt_.stream_index != video_stream_id_)
2236 av_free_packet(&pkt_);
2250 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2252 InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2254 if (stream && stream->open(fileName, codec, chroma_format, width, height))
2263 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2269 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2271 return stream->read(data, size, endOfFile);