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"
44 #if !(defined(WIN32) || defined(_WIN32) || defined(WINCE))
51 #define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )
53 #if defined _MSC_VER && _MSC_VER >= 1200
54 #pragma warning( disable: 4244 4510 4512 4610 )
58 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
65 #include "ffmpeg_codecs.hpp"
67 #include <libavutil/mathematics.h>
69 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
70 #include <libavutil/opt.h>
74 #define HAVE_FFMPEG_SWSCALE 1
75 #include <libavcodec/avcodec.h>
76 #include <libswscale/swscale.h>
79 #ifndef HAVE_FFMPEG_SWSCALE
80 #error "libswscale is necessary to build the newer OpenCV ffmpeg wrapper"
83 // if the header path is not specified explicitly, let's deduce it
84 #if !defined HAVE_FFMPEG_AVCODEC_H && !defined HAVE_LIBAVCODEC_AVCODEC_H
86 #if defined(HAVE_GENTOO_FFMPEG)
87 #define HAVE_LIBAVCODEC_AVCODEC_H 1
88 #if defined(HAVE_FFMPEG_SWSCALE)
89 #define HAVE_LIBSWSCALE_SWSCALE_H 1
91 #elif defined HAVE_FFMPEG
92 #define HAVE_FFMPEG_AVCODEC_H 1
93 #if defined(HAVE_FFMPEG_SWSCALE)
94 #define HAVE_FFMPEG_SWSCALE_H 1
100 #if defined(HAVE_FFMPEG_AVCODEC_H)
101 #include <ffmpeg/avcodec.h>
103 #if defined(HAVE_FFMPEG_SWSCALE_H)
104 #include <ffmpeg/swscale.h>
107 #if defined(HAVE_LIBAVCODEC_AVCODEC_H)
108 #include <libavcodec/avcodec.h>
110 #if defined(HAVE_LIBSWSCALE_SWSCALE_H)
111 #include <libswscale/swscale.h>
120 #if defined _MSC_VER && _MSC_VER >= 1200
121 #pragma warning( default: 4244 4510 4512 4610 )
125 #define CV_WARN(message)
127 #define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
130 /* PIX_FMT_RGBA32 macro changed in newer ffmpeg versions */
131 #ifndef PIX_FMT_RGBA32
132 #define PIX_FMT_RGBA32 PIX_FMT_RGB32
136 #if defined WIN32 || defined _WIN32
138 #elif defined __linux__ || defined __APPLE__
141 #include <sys/types.h>
142 #include <sys/sysctl.h>
146 #define MIN(a, b) ((a) < (b) ? (a) : (b))
149 #if defined(__APPLE__)
150 #define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
152 #define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
156 #define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
159 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
160 # define CV_CODEC_ID AVCodecID
161 # define CV_CODEC(name) AV_##name
163 # define CV_CODEC_ID CodecID
164 # define CV_CODEC(name) name
167 static int get_number_of_cpus(void)
169 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
171 #elif defined WIN32 || defined _WIN32
173 GetSystemInfo( &sysinfo );
175 return (int)sysinfo.dwNumberOfProcessors;
176 #elif defined __linux__
177 return (int)sysconf( _SC_NPROCESSORS_ONLN );
178 #elif defined __APPLE__
181 size_t len = sizeof(numCPU);
183 // set the mib for hw.ncpu
185 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
187 // get the number of CPUs from the system
188 sysctl(mib, 2, &numCPU, &len, NULL, 0);
193 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
216 inline void _opencv_ffmpeg_free(void** ptr)
223 struct CvCapture_FFMPEG
225 bool open( const char* filename );
228 double getProperty(int);
229 bool setProperty(int, double);
231 bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
235 void seek(int64_t frame_number);
236 void seek(double sec);
237 bool slowSeek( int framenumber );
239 int64_t get_total_frames();
240 double get_duration_sec();
244 double r2d(AVRational r);
245 int64_t dts_to_frame_number(int64_t dts);
246 double dts_to_sec(int64_t dts);
248 AVFormatContext * ic;
258 struct SwsContext *img_convert_ctx;
260 int64_t frame_number, first_frame_number;
264 'filename' contains the filename of the videosource,
265 'filename==NULL' indicates that ffmpeg's seek support works
266 for the particular file.
267 'filename!=NULL' indicates that the slow fallback function is used for seeking,
268 and so the filename is needed to reopen the file on backward seeking.
273 void CvCapture_FFMPEG::init()
279 picture_pts = AV_NOPTS_VALUE_;
280 first_frame_number = -1;
281 memset( &rgb_picture, 0, sizeof(rgb_picture) );
282 memset( &frame, 0, sizeof(frame) );
284 memset(&packet, 0, sizeof(packet));
285 av_init_packet(&packet);
294 void CvCapture_FFMPEG::close()
296 if( img_convert_ctx )
298 sws_freeContext(img_convert_ctx);
307 #if LIBAVFORMAT_BUILD > 4628
308 avcodec_close( video_st->codec );
311 avcodec_close( &(video_st->codec) );
319 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
320 av_close_input_file(ic);
322 avformat_close_input(&ic);
328 if( rgb_picture.data[0] )
330 free( rgb_picture.data[0] );
331 rgb_picture.data[0] = 0;
334 // free last packet if exist
336 av_free_packet (&packet);
344 #ifndef AVSEEK_FLAG_FRAME
345 #define AVSEEK_FLAG_FRAME 0
347 #ifndef AVSEEK_FLAG_ANY
348 #define AVSEEK_FLAG_ANY 1
354 ImplMutex() { init(); }
355 ~ImplMutex() { destroy(); }
369 ImplMutex(const ImplMutex&);
370 ImplMutex& operator = (const ImplMutex& m);
373 #if defined WIN32 || defined _WIN32 || defined WINCE
375 struct ImplMutex::Impl
379 #if (_WIN32_WINNT >= 0x0600)
380 ::InitializeCriticalSectionEx(&cs, 1000, 0);
382 ::InitializeCriticalSection(&cs);
386 void destroy() { DeleteCriticalSection(&cs); }
388 void lock() { EnterCriticalSection(&cs); }
389 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
390 void unlock() { LeaveCriticalSection(&cs); }
397 static int _interlockedExchangeAdd(int* addr, int delta)
399 #if defined _MSC_VER && _MSC_VER >= 1500
400 return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
402 return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
407 #elif defined __APPLE__
409 #include <libkern/OSAtomic.h>
411 struct ImplMutex::Impl
413 void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
416 void lock() { OSSpinLockLock(&sl); }
417 bool trylock() { return OSSpinLockTry(&sl); }
418 void unlock() { OSSpinLockUnlock(&sl); }
424 #elif defined __linux__ && !defined ANDROID
426 struct ImplMutex::Impl
428 void init() { pthread_spin_init(&sl, 0); refcount = 1; }
429 void destroy() { pthread_spin_destroy(&sl); }
431 void lock() { pthread_spin_lock(&sl); }
432 bool trylock() { return pthread_spin_trylock(&sl) == 0; }
433 void unlock() { pthread_spin_unlock(&sl); }
435 pthread_spinlock_t sl;
441 struct ImplMutex::Impl
443 void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
444 void destroy() { pthread_mutex_destroy(&sl); }
446 void lock() { pthread_mutex_lock(&sl); }
447 bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
448 void unlock() { pthread_mutex_unlock(&sl); }
456 void ImplMutex::init()
458 impl = (Impl*)malloc(sizeof(Impl));
461 void ImplMutex::destroy()
467 void ImplMutex::lock() { impl->lock(); }
468 void ImplMutex::unlock() { impl->unlock(); }
469 bool ImplMutex::trylock() { return impl->trylock(); }
471 static int LockCallBack(void **mutex, AVLockOp op)
473 ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
477 localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
488 case AV_LOCK_RELEASE:
489 localMutex->unlock();
492 case AV_LOCK_DESTROY:
493 localMutex->destroy();
501 static ImplMutex _mutex;
502 static bool _initialized = false;
504 class InternalFFMpegRegister
507 InternalFFMpegRegister()
512 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
513 avformat_network_init();
516 /* register all codecs, demux and protocols */
519 /* register a callback function for synchronization */
520 av_lockmgr_register(&LockCallBack);
522 av_log_set_level(AV_LOG_ERROR);
529 ~InternalFFMpegRegister()
531 _initialized = false;
532 av_lockmgr_register(NULL);
536 static InternalFFMpegRegister _init;
538 bool CvCapture_FFMPEG::open( const char* _filename )
545 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
546 int err = avformat_open_input(&ic, _filename, NULL, NULL);
548 int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
553 CV_WARN("Error opening file");
557 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
558 avformat_find_stream_info(ic, NULL);
560 av_find_stream_info(ic);
564 CV_WARN("Could not find codec parameters");
567 for(i = 0; i < ic->nb_streams; i++)
569 #if LIBAVFORMAT_BUILD > 4628
570 AVCodecContext *enc = ic->streams[i]->codec;
572 AVCodecContext *enc = &ic->streams[i]->codec;
575 //#ifdef FF_API_THREAD_INIT
576 // avcodec_thread_init(enc, get_number_of_cpus());
578 enc->thread_count = get_number_of_cpus();
581 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
582 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
585 if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
587 // backup encoder' width/height
588 int enc_width = enc->width;
589 int enc_height = enc->height;
591 AVCodec *codec = avcodec_find_decoder(enc->codec_id);
593 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
594 avcodec_open2(enc, codec, NULL)
596 avcodec_open(enc, codec)
601 // checking width/height (since decoder can sometimes alter it, eg. vp6f)
602 if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
603 if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
606 video_st = ic->streams[i];
607 picture = avcodec_alloc_frame();
609 rgb_picture.data[0] = (uint8_t*)malloc(
610 avpicture_get_size( PIX_FMT_BGR24,
611 enc->width, enc->height ));
612 avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
613 PIX_FMT_BGR24, enc->width, enc->height );
615 frame.width = enc->width;
616 frame.height = enc->height;
618 frame.step = rgb_picture.linesize[0];
619 frame.data = rgb_picture.data[0];
624 if(video_stream >= 0) valid = true;
635 bool CvCapture_FFMPEG::grabFrame()
641 const int max_number_of_attempts = 1 << 16;
643 if( !ic || !video_st ) return false;
645 if( ic->streams[video_stream]->nb_frames > 0 &&
646 frame_number > ic->streams[video_stream]->nb_frames )
649 av_free_packet (&packet);
651 picture_pts = AV_NOPTS_VALUE_;
653 // get the next frame
656 int ret = av_read_frame(ic, &packet);
657 if (ret == AVERROR(EAGAIN)) continue;
659 /* else if (ret < 0) break; */
661 if( packet.stream_index != video_stream )
663 av_free_packet (&packet);
665 if (count_errs > max_number_of_attempts)
670 // Decode video frame
671 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
672 avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
673 #elif LIBAVFORMAT_BUILD > 4628
674 avcodec_decode_video(video_st->codec,
675 picture, &got_picture,
676 packet.data, packet.size);
678 avcodec_decode_video(&video_st->codec,
679 picture, &got_picture,
680 packet.data, packet.size);
683 // Did we get a video frame?
686 //picture_pts = picture->best_effort_timestamp;
687 if( picture_pts == AV_NOPTS_VALUE_ )
688 picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
695 if (count_errs > max_number_of_attempts)
699 av_free_packet (&packet);
702 if( valid && first_frame_number < 0 )
703 first_frame_number = dts_to_frame_number(picture_pts);
705 // return if we have a new picture or not
710 bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
712 if( !video_st || !picture->data[0] )
715 avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
716 video_st->codec->width, video_st->codec->height);
718 if( img_convert_ctx == NULL ||
719 frame.width != video_st->codec->width ||
720 frame.height != video_st->codec->height )
722 if( img_convert_ctx )
723 sws_freeContext(img_convert_ctx);
725 frame.width = video_st->codec->width;
726 frame.height = video_st->codec->height;
728 img_convert_ctx = sws_getCachedContext(
730 video_st->codec->width, video_st->codec->height,
731 video_st->codec->pix_fmt,
732 video_st->codec->width, video_st->codec->height,
738 if (img_convert_ctx == NULL)
739 return false;//CV_Error(0, "Cannot initialize the conversion context!");
746 0, video_st->codec->height,
753 *width = frame.width;
754 *height = frame.height;
761 double CvCapture_FFMPEG::getProperty( int property_id )
763 if( !video_st ) return 0;
765 switch( property_id )
767 case CV_FFMPEG_CAP_PROP_POS_MSEC:
768 return 1000.0*(double)frame_number/get_fps();
769 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
770 return (double)frame_number;
771 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
772 return r2d(ic->streams[video_stream]->time_base);
773 case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
774 return (double)get_total_frames();
775 case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
776 return (double)frame.width;
777 case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
778 return (double)frame.height;
779 case CV_FFMPEG_CAP_PROP_FPS:
780 #if LIBAVCODEC_BUILD > 4753
781 return av_q2d(video_st->r_frame_rate);
783 return (double)video_st->codec.frame_rate
784 / (double)video_st->codec.frame_rate_base;
786 case CV_FFMPEG_CAP_PROP_FOURCC:
787 #if LIBAVFORMAT_BUILD > 4628
788 return (double)video_st->codec->codec_tag;
790 return (double)video_st->codec.codec_tag;
799 double CvCapture_FFMPEG::r2d(AVRational r)
801 return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
804 double CvCapture_FFMPEG::get_duration_sec()
806 double sec = (double)ic->duration / (double)AV_TIME_BASE;
810 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
815 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
821 int CvCapture_FFMPEG::get_bitrate()
826 double CvCapture_FFMPEG::get_fps()
828 double fps = r2d(ic->streams[video_stream]->r_frame_rate);
830 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
833 fps = r2d(ic->streams[video_stream]->avg_frame_rate);
839 fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
845 int64_t CvCapture_FFMPEG::get_total_frames()
847 int64_t nbf = ic->streams[video_stream]->nb_frames;
851 nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
856 int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
858 double sec = dts_to_sec(dts);
859 return (int64_t)(get_fps() * sec + 0.5);
862 double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
864 return (double)(dts - ic->streams[video_stream]->start_time) *
865 r2d(ic->streams[video_stream]->time_base);
868 void CvCapture_FFMPEG::seek(int64_t _frame_number)
870 _frame_number = std::min(_frame_number, get_total_frames());
873 // if we have not grabbed a single frame before first seek, let's read the first frame
874 // and get some valuable information during the process
875 if( first_frame_number < 0 && get_total_frames() > 1 )
880 int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
881 double sec = (double)_frame_number_temp / get_fps();
882 int64_t time_stamp = ic->streams[video_stream]->start_time;
883 double time_base = r2d(ic->streams[video_stream]->time_base);
884 time_stamp += (int64_t)(sec / time_base + 0.5);
885 if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
886 avcodec_flush_buffers(ic->streams[video_stream]->codec);
887 if( _frame_number > 0 )
891 if( _frame_number > 1 )
893 frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
894 //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
895 // (int)_frame_number, (int)frame_number, delta);
897 if( frame_number < 0 || frame_number > _frame_number-1 )
899 if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
901 delta = delta < 16 ? delta*2 : delta*3/2;
904 while( frame_number < _frame_number-1 )
926 void CvCapture_FFMPEG::seek(double sec)
928 seek((int64_t)(sec * get_fps() + 0.5));
931 bool CvCapture_FFMPEG::setProperty( int property_id, double value )
933 if( !video_st ) return false;
935 switch( property_id )
937 case CV_FFMPEG_CAP_PROP_POS_MSEC:
938 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
939 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
941 switch( property_id )
943 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
944 seek((int64_t)value);
947 case CV_FFMPEG_CAP_PROP_POS_MSEC:
951 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
952 seek((int64_t)(value*ic->duration));
956 picture_pts=(int64_t)value;
967 ///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
968 struct CvVideoWriter_FFMPEG
970 bool open( const char* filename, int fourcc,
971 double fps, int width, int height, bool isColor );
973 bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
977 AVOutputFormat * fmt;
978 AVFormatContext * oc;
980 uint32_t outbuf_size;
983 AVFrame * input_picture;
987 Image_FFMPEG temp_image;
988 int frame_width, frame_height;
990 struct SwsContext *img_convert_ctx;
993 static const char * icvFFMPEGErrStr(int err)
995 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
997 case AVERROR_BSF_NOT_FOUND:
998 return "Bitstream filter not found";
999 case AVERROR_DECODER_NOT_FOUND:
1000 return "Decoder not found";
1001 case AVERROR_DEMUXER_NOT_FOUND:
1002 return "Demuxer not found";
1003 case AVERROR_ENCODER_NOT_FOUND:
1004 return "Encoder not found";
1006 return "End of file";
1008 return "Immediate exit was requested; the called function should not be restarted";
1009 case AVERROR_FILTER_NOT_FOUND:
1010 return "Filter not found";
1011 case AVERROR_INVALIDDATA:
1012 return "Invalid data found when processing input";
1013 case AVERROR_MUXER_NOT_FOUND:
1014 return "Muxer not found";
1015 case AVERROR_OPTION_NOT_FOUND:
1016 return "Option not found";
1017 case AVERROR_PATCHWELCOME:
1018 return "Not yet implemented in FFmpeg, patches welcome";
1019 case AVERROR_PROTOCOL_NOT_FOUND:
1020 return "Protocol not found";
1021 case AVERROR_STREAM_NOT_FOUND:
1022 return "Stream not found";
1028 case AVERROR_NUMEXPECTED:
1029 return "Incorrect filename syntax";
1030 case AVERROR_INVALIDDATA:
1031 return "Invalid data in header";
1033 return "Unknown format";
1035 return "I/O error occurred";
1037 return "Memory allocation error";
1043 return "Unspecified error";
1046 /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1048 enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1051 void CvVideoWriter_FFMPEG::init()
1063 memset(&temp_image, 0, sizeof(temp_image));
1064 img_convert_ctx = 0;
1065 frame_width = frame_height = 0;
1070 * the following function is a modified version of code
1071 * found in ffmpeg-0.4.9-pre1/output_example.c
1073 static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1076 uint8_t * picture_buf;
1079 picture = avcodec_alloc_frame();
1082 size = avpicture_get_size( (PixelFormat) pix_fmt, width, height);
1084 picture_buf = (uint8_t *) malloc(size);
1090 avpicture_fill((AVPicture *)picture, picture_buf,
1091 (PixelFormat) pix_fmt, width, height);
1098 /* add a video output stream to the container */
1099 static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1100 CV_CODEC_ID codec_id,
1101 int w, int h, int bitrate,
1102 double fps, int pixel_format)
1106 int frame_rate, frame_rate_base;
1109 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1110 st = avformat_new_stream(oc, 0);
1112 st = av_new_stream(oc, 0);
1116 CV_WARN("Could not allocate stream");
1120 #if LIBAVFORMAT_BUILD > 4628
1126 #if LIBAVFORMAT_BUILD > 4621
1127 c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1129 c->codec_id = oc->oformat->video_codec;
1132 if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1133 c->codec_id = codec_id;
1136 //if(codec_tag) c->codec_tag=codec_tag;
1137 codec = avcodec_find_encoder(c->codec_id);
1139 c->codec_type = AVMEDIA_TYPE_VIDEO;
1141 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
1142 // Set per-codec defaults
1143 AVCodecID c_id = c->codec_id;
1144 avcodec_get_context_defaults3(c, codec);
1145 // avcodec_get_context_defaults3 erases codec_id for some reason
1149 /* put sample parameters */
1150 int64_t lbit_rate = (int64_t)bitrate;
1151 lbit_rate += (bitrate / 2);
1152 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1153 c->bit_rate = lbit_rate;
1156 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1159 /* resolution must be a multiple of two */
1163 /* time base: this is the fundamental unit of time (in seconds) in terms
1164 of which frame timestamps are represented. for fixed-fps content,
1165 timebase should be 1/framerate and timestamp increments should be
1167 frame_rate=(int)(fps+0.5);
1169 while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
1170 frame_rate_base*=10;
1171 frame_rate=(int)(fps*frame_rate_base + 0.5);
1173 #if LIBAVFORMAT_BUILD > 4752
1174 c->time_base.den = frame_rate;
1175 c->time_base.num = frame_rate_base;
1176 /* adjust time base for supported framerates */
1177 if(codec && codec->supported_framerates){
1178 const AVRational *p= codec->supported_framerates;
1179 AVRational req = {frame_rate, frame_rate_base};
1180 const AVRational *best=NULL;
1181 AVRational best_error= {INT_MAX, 1};
1182 for(; p->den!=0; p++){
1183 AVRational error= av_sub_q(req, *p);
1184 if(error.num <0) error.num *= -1;
1185 if(av_cmp_q(error, best_error) < 0){
1190 c->time_base.den= best->num;
1191 c->time_base.num= best->den;
1194 c->frame_rate = frame_rate;
1195 c->frame_rate_base = frame_rate_base;
1198 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1199 c->pix_fmt = (PixelFormat) pixel_format;
1201 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1202 c->max_b_frames = 2;
1204 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1205 /* needed to avoid using macroblocks in which some coeffs overflow
1206 this doesnt happen with normal video, it just happens here as the
1207 motion of the chroma plane doesnt match the luma plane */
1208 /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1212 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
1213 /* Some settings for libx264 encoding, restore dummy values for gop_size
1214 and qmin since they will be set to reasonable defaults by the libx264
1215 preset system. Also, use a crf encode with the default quality rating,
1216 this seems easier than finding an appropriate default bitrate. */
1217 if (c->codec_id == CODEC_ID_H264) {
1221 av_opt_set(c->priv_data,"crf","23", 0);
1225 #if LIBAVCODEC_VERSION_INT>0x000409
1226 // some formats want stream headers to be seperate
1227 if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1229 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1236 static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1238 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture )
1240 #if LIBAVFORMAT_BUILD > 4628
1241 AVCodecContext * c = video_st->codec;
1243 AVCodecContext * c = &(video_st->codec);
1248 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
1249 /* raw video case. The API will change slightly in the near
1252 av_init_packet(&pkt);
1254 #ifndef PKT_FLAG_KEY
1255 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
1258 pkt.flags |= PKT_FLAG_KEY;
1259 pkt.stream_index= video_st->index;
1260 pkt.data= (uint8_t *)picture;
1261 pkt.size= sizeof(AVPicture);
1263 ret = av_write_frame(oc, &pkt);
1265 /* encode the image */
1266 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1267 /* if zero size, it means the image was buffered */
1270 av_init_packet(&pkt);
1272 #if LIBAVFORMAT_BUILD > 4752
1273 if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1274 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1276 pkt.pts = c->coded_frame->pts;
1278 if(c->coded_frame->key_frame)
1279 pkt.flags |= PKT_FLAG_KEY;
1280 pkt.stream_index= video_st->index;
1284 /* write the compressed frame in the media file */
1285 ret = av_write_frame(oc, &pkt);
1287 ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1293 /// write a frame with FFMPEG
1294 bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1298 if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1300 width = frame_width;
1301 height = frame_height;
1303 // typecast from opaque data type to implemented struct
1304 #if LIBAVFORMAT_BUILD > 4628
1305 AVCodecContext *c = video_st->codec;
1307 AVCodecContext *c = &(video_st->codec);
1310 #if LIBAVFORMAT_BUILD < 5231
1311 // It is not needed in the latest versions of the ffmpeg
1312 if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
1314 if( !temp_image.data )
1316 temp_image.step = (width*cn + 3) & -4;
1317 temp_image.width = width;
1318 temp_image.height = height;
1320 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1322 for( int y = 0; y < height; y++ )
1323 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
1324 data = temp_image.data;
1325 step = temp_image.step;
1328 if( width*cn != step )
1330 if( !temp_image.data )
1332 temp_image.step = width*cn;
1333 temp_image.width = width;
1334 temp_image.height = height;
1336 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1339 for( int y = 0; y < height; y++ )
1340 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
1342 for( int y = 0; y < height; y++ )
1343 memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
1344 data = temp_image.data;
1345 step = temp_image.step;
1350 if (input_pix_fmt == PIX_FMT_BGR24) {
1355 else if (input_pix_fmt == PIX_FMT_GRAY8) {
1364 if ( c->pix_fmt != input_pix_fmt ) {
1365 assert( input_picture );
1366 // let input_picture point to the raw data buffer of 'image'
1367 avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1368 (PixelFormat)input_pix_fmt, width, height);
1370 if( !img_convert_ctx )
1372 img_convert_ctx = sws_getContext(width,
1374 (PixelFormat)input_pix_fmt,
1380 if( !img_convert_ctx )
1384 if ( sws_scale(img_convert_ctx, input_picture->data,
1385 input_picture->linesize, 0,
1387 picture->data, picture->linesize) < 0 )
1391 avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1392 (PixelFormat)input_pix_fmt, width, height);
1395 ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1400 /// close video output stream and free associated memory
1401 void CvVideoWriter_FFMPEG::close()
1403 // nothing to do if already released
1407 /* no more frame to compress. The codec has a latency of a few
1408 frames if using B frames, so we get the last frames by
1409 passing the same picture again */
1410 // TODO -- do we need to account for latency here?
1412 /* write the trailer, if any */
1415 if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1419 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1420 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1424 av_write_trailer(oc);
1427 if( img_convert_ctx )
1429 sws_freeContext(img_convert_ctx);
1430 img_convert_ctx = 0;
1434 #if LIBAVFORMAT_BUILD > 4628
1435 if( video_st->codec->pix_fmt != input_pix_fmt)
1437 if( video_st->codec.pix_fmt != input_pix_fmt)
1440 if(picture->data[0])
1441 free(picture->data[0]);
1442 picture->data[0] = 0;
1447 av_free(input_picture);
1450 #if LIBAVFORMAT_BUILD > 4628
1451 avcodec_close(video_st->codec);
1453 avcodec_close(&(video_st->codec));
1458 if (!(fmt->flags & AVFMT_NOFILE))
1460 /* close the output file */
1462 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1463 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1466 url_fclose(&oc->pb);
1474 /* free the stream */
1475 avformat_free_context(oc);
1477 if( temp_image.data )
1479 free(temp_image.data);
1480 temp_image.data = 0;
1486 /// Create a video writer object that uses FFMPEG
1487 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1488 double fps, int width, int height, bool is_color )
1490 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1491 int err, codec_pix_fmt;
1492 double bitrate_scale = 1;
1502 // we allow frames of odd width or height, but in this case we truncate
1503 // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1504 // but some internal functions inside FFMPEG swscale require even width/height.
1507 if( width <= 0 || height <= 0 )
1510 /* auto detect the output format from the name and fourcc code. */
1512 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1513 fmt = av_guess_format(NULL, filename, NULL);
1515 fmt = guess_format(NULL, filename, NULL);
1521 /* determine optimal pixel format */
1523 input_pix_fmt = PIX_FMT_BGR24;
1526 input_pix_fmt = PIX_FMT_GRAY8;
1529 /* Lookup codec_id for given fourcc */
1530 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1531 if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1534 const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
1535 if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1539 // alloc memory for context
1540 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1541 oc = avformat_alloc_context();
1543 oc = av_alloc_format_context();
1549 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1551 /* set some options */
1552 oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
1554 // set a few optimal pixel formats for lossless codecs of interest..
1556 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1557 case CV_CODEC(CODEC_ID_JPEGLS):
1558 // BGR24 or GRAY8 depending on is_color...
1559 codec_pix_fmt = input_pix_fmt;
1562 case CV_CODEC(CODEC_ID_HUFFYUV):
1563 codec_pix_fmt = PIX_FMT_YUV422P;
1565 case CV_CODEC(CODEC_ID_MJPEG):
1566 case CV_CODEC(CODEC_ID_LJPEG):
1567 codec_pix_fmt = PIX_FMT_YUVJ420P;
1570 case CV_CODEC(CODEC_ID_RAWVIDEO):
1571 codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
1572 input_pix_fmt == PIX_FMT_GRAY16LE ||
1573 input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
1576 // good for lossy formats, MPEG, etc.
1577 codec_pix_fmt = PIX_FMT_YUV420P;
1581 double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1583 // TODO -- safe to ignore output audio stream?
1584 video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1585 width, height, (int)(bitrate + 0.5),
1586 fps, codec_pix_fmt);
1588 /* set the output parameters (must be done even if no
1590 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1591 if (av_set_parameters(oc, NULL) < 0) {
1597 #if FF_API_DUMP_FORMAT
1598 dump_format(oc, 0, filename, 1);
1600 av_dump_format(oc, 0, filename, 1);
1604 /* now that all the parameters are set, we can open the audio and
1605 video codecs and allocate the necessary encode buffers */
1613 #if LIBAVFORMAT_BUILD > 4628
1614 c = (video_st->codec);
1616 c = &(video_st->codec);
1619 c->codec_tag = fourcc;
1620 /* find the video encoder */
1621 codec = avcodec_find_encoder(c->codec_id);
1623 fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
1624 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1625 AVERROR_ENCODER_NOT_FOUND
1633 int64_t lbit_rate = (int64_t)c->bit_rate;
1634 lbit_rate += (bitrate / 2);
1635 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1636 c->bit_rate_tolerance = (int)lbit_rate;
1637 c->bit_rate = (int)lbit_rate;
1639 /* open the codec */
1641 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1642 avcodec_open2(c, codec, NULL)
1644 avcodec_open(c, codec)
1647 fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
1653 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1654 /* allocate output buffer */
1655 /* assume we will never get codec output with more than 4 bytes per pixel... */
1656 outbuf_size = width*height*4;
1657 outbuf = (uint8_t *) av_malloc(outbuf_size);
1660 bool need_color_convert;
1661 need_color_convert = (c->pix_fmt != input_pix_fmt);
1663 /* allocate the encoded raw picture */
1664 picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1669 /* if the output format is not our input format, then a temporary
1670 picture of the input format is needed too. It is then converted
1671 to the required output format */
1672 input_picture = NULL;
1673 if ( need_color_convert ) {
1674 input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1675 if (!input_picture) {
1680 /* open the output file, if needed */
1681 if (!(fmt->flags & AVFMT_NOFILE)) {
1682 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1683 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1685 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1692 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1693 /* write the stream header, if any */
1694 err=avformat_write_header(oc, NULL);
1696 err=av_write_header( oc );
1705 frame_width = width;
1706 frame_height = height;
1714 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1716 CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1718 if( capture->open( filename ))
1727 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1729 if( capture && *capture )
1731 (*capture)->close();
1737 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1739 return capture->setProperty(prop_id, value);
1742 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1744 return capture->getProperty(prop_id);
1747 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1749 return capture->grabFrame();
1752 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1754 return capture->retrieveFrame(0, data, step, width, height, cn);
1757 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1758 int width, int height, int isColor )
1760 CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1762 if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1769 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1771 if( writer && *writer )
1780 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1781 const unsigned char* data, int step,
1782 int width, int height, int cn, int origin)
1784 return writer->writeFrame(data, step, width, height, cn, origin);
1793 struct OutputMediaStream_FFMPEG
1795 bool open(const char* fileName, int width, int height, double fps);
1798 void write(unsigned char* data, int size, int keyFrame);
1800 // add a video output stream to the container
1801 static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
1803 AVOutputFormat* fmt_;
1804 AVFormatContext* oc_;
1805 AVStream* video_st_;
1808 void OutputMediaStream_FFMPEG::close()
1810 // no more frame to compress. The codec has a latency of a few
1811 // frames if using B frames, so we get the last frames by
1812 // passing the same picture again
1814 // TODO -- do we need to account for latency here?
1818 // write the trailer, if any
1819 av_write_trailer(oc_);
1822 for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1824 av_freep(&oc_->streams[i]->codec);
1825 av_freep(&oc_->streams[i]);
1828 if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
1830 // close the output file
1832 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1833 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1834 url_fclose(oc_->pb);
1836 url_fclose(&oc_->pb);
1839 avio_close(oc_->pb);
1848 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
1850 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1851 AVStream* st = avformat_new_stream(oc, 0);
1853 AVStream* st = av_new_stream(oc, 0);
1858 #if LIBAVFORMAT_BUILD > 4628
1859 AVCodecContext* c = st->codec;
1861 AVCodecContext* c = &(st->codec);
1864 c->codec_id = codec_id;
1865 c->codec_type = AVMEDIA_TYPE_VIDEO;
1867 // put sample parameters
1868 unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
1869 lbit_rate += (bitrate / 4);
1870 lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
1871 c->bit_rate = bitrate;
1874 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1877 // resolution must be a multiple of two
1881 AVCodec* codec = avcodec_find_encoder(c->codec_id);
1883 // time base: this is the fundamental unit of time (in seconds) in terms
1884 // of which frame timestamps are represented. for fixed-fps content,
1885 // timebase should be 1/framerate and timestamp increments should be
1888 int frame_rate = static_cast<int>(fps+0.5);
1889 int frame_rate_base = 1;
1890 while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
1892 frame_rate_base *= 10;
1893 frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
1895 c->time_base.den = frame_rate;
1896 c->time_base.num = frame_rate_base;
1898 #if LIBAVFORMAT_BUILD > 4752
1899 // adjust time base for supported framerates
1900 if (codec && codec->supported_framerates)
1902 AVRational req = {frame_rate, frame_rate_base};
1903 const AVRational* best = NULL;
1904 AVRational best_error = {INT_MAX, 1};
1906 for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
1908 AVRational error = av_sub_q(req, *p);
1913 if (av_cmp_q(error, best_error) < 0)
1920 c->time_base.den= best->num;
1921 c->time_base.num= best->den;
1925 c->gop_size = 12; // emit one intra frame every twelve frames at most
1926 c->pix_fmt = pixel_format;
1928 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
1929 c->max_b_frames = 2;
1931 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
1933 // needed to avoid using macroblocks in which some coeffs overflow
1934 // this doesnt happen with normal video, it just happens here as the
1935 // motion of the chroma plane doesnt match the luma plane
1937 // avoid FFMPEG warning 'clipping 1 dct coefficients...'
1942 #if LIBAVCODEC_VERSION_INT > 0x000409
1943 // some formats want stream headers to be seperate
1944 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1946 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1953 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
1959 // auto detect the output format from the name and fourcc code
1960 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1961 fmt_ = av_guess_format(NULL, fileName, NULL);
1963 fmt_ = guess_format(NULL, fileName, NULL);
1968 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
1970 // alloc memory for context
1971 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1972 oc_ = avformat_alloc_context();
1974 oc_ = av_alloc_format_context();
1980 oc_->oformat = fmt_;
1981 snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
1983 oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
1985 // set a few optimal pixel formats for lossless codecs of interest..
1986 PixelFormat codec_pix_fmt = PIX_FMT_YUV420P;
1987 int bitrate_scale = 64;
1989 // TODO -- safe to ignore output audio stream?
1990 video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
1994 // set the output parameters (must be done even if no parameters)
1995 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1996 if (av_set_parameters(oc_, NULL) < 0)
2000 // now that all the parameters are set, we can open the audio and
2001 // video codecs and allocate the necessary encode buffers
2003 #if LIBAVFORMAT_BUILD > 4628
2004 AVCodecContext* c = (video_st_->codec);
2006 AVCodecContext* c = &(video_st_->codec);
2009 c->codec_tag = MKTAG('H', '2', '6', '4');
2010 c->bit_rate_tolerance = c->bit_rate;
2012 // open the output file, if needed
2013 if (!(fmt_->flags & AVFMT_NOFILE))
2015 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2016 int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
2018 int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
2025 // write the stream header, if any
2026 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2027 av_write_header(oc_);
2029 avformat_write_header(oc_, NULL);
2035 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2037 // if zero size, it means the image was buffered
2041 av_init_packet(&pkt);
2044 pkt.flags |= PKT_FLAG_KEY;
2046 pkt.stream_index = video_st_->index;
2050 // write the compressed frame in the media file
2051 av_write_frame(oc_, &pkt);
2055 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2057 OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2059 if (stream->open(fileName, width, height, fps))
2068 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2074 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2076 stream->write(data, size, keyFrame);
2085 VideoCodec_MPEG1 = 0,
2091 VideoCodec_H264_SVC,
2092 VideoCodec_H264_MVC,
2095 VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2096 VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2097 VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2098 VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2099 VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')), // UYVY (4:2:2)
2104 VideoChromaFormat_Monochrome = 0,
2105 VideoChromaFormat_YUV420,
2106 VideoChromaFormat_YUV422,
2107 VideoChromaFormat_YUV444,
2110 struct InputMediaStream_FFMPEG
2113 bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2116 bool read(unsigned char** data, int* size, int* endOfFile);
2119 InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2120 InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2122 AVFormatContext* ctx_;
2123 int video_stream_id_;
2127 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2132 video_stream_id_ = -1;
2133 memset(&pkt_, 0, sizeof(AVPacket));
2135 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2136 avformat_network_init();
2139 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2140 err = avformat_open_input(&ctx_, fileName, 0, 0);
2142 err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2147 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2148 err = avformat_find_stream_info(ctx_, 0);
2150 err = av_find_stream_info(ctx_);
2155 for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2157 #if LIBAVFORMAT_BUILD > 4628
2158 AVCodecContext *enc = ctx_->streams[i]->codec;
2160 AVCodecContext *enc = &ctx_->streams[i]->codec;
2163 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2165 video_stream_id_ = static_cast<int>(i);
2167 switch (enc->codec_id)
2169 case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2170 *codec = ::VideoCodec_MPEG1;
2173 case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2174 *codec = ::VideoCodec_MPEG2;
2177 case CV_CODEC(CODEC_ID_MPEG4):
2178 *codec = ::VideoCodec_MPEG4;
2181 case CV_CODEC(CODEC_ID_VC1):
2182 *codec = ::VideoCodec_VC1;
2185 case CV_CODEC(CODEC_ID_H264):
2186 *codec = ::VideoCodec_H264;
2193 switch (enc->pix_fmt)
2195 case PIX_FMT_YUV420P:
2196 *chroma_format = ::VideoChromaFormat_YUV420;
2199 case PIX_FMT_YUV422P:
2200 *chroma_format = ::VideoChromaFormat_YUV422;
2203 case PIX_FMT_YUV444P:
2204 *chroma_format = ::VideoChromaFormat_YUV444;
2211 *width = enc->coded_width;
2212 *height = enc->coded_height;
2218 if (video_stream_id_ < 0)
2221 av_init_packet(&pkt_);
2226 void InputMediaStream_FFMPEG::close()
2230 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2231 avformat_close_input(&ctx_);
2233 av_close_input_file(ctx_);
2237 // free last packet if exist
2239 av_free_packet(&pkt_);
2242 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2244 // free last packet if exist
2246 av_free_packet(&pkt_);
2248 // get the next frame
2251 int ret = av_read_frame(ctx_, &pkt_);
2253 if (ret == AVERROR(EAGAIN))
2258 if (ret == (int)AVERROR_EOF)
2263 if (pkt_.stream_index != video_stream_id_)
2265 av_free_packet(&pkt_);
2279 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2281 InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2283 if (stream && stream->open(fileName, codec, chroma_format, width, height))
2292 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2298 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2300 return stream->read(data, size, endOfFile);