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 #if defined __APPLE__
143 #include <sys/sysctl.h>
148 #define MIN(a, b) ((a) < (b) ? (a) : (b))
151 #if defined(__APPLE__)
152 #define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
154 #define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
158 #define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
161 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
162 # define CV_CODEC_ID AVCodecID
163 # define CV_CODEC(name) AV_##name
165 # define CV_CODEC_ID CodecID
166 # define CV_CODEC(name) name
169 static int get_number_of_cpus(void)
171 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
173 #elif defined WIN32 || defined _WIN32
175 GetSystemInfo( &sysinfo );
177 return (int)sysinfo.dwNumberOfProcessors;
178 #elif defined __linux__
179 return (int)sysconf( _SC_NPROCESSORS_ONLN );
180 #elif defined __APPLE__
183 size_t len = sizeof(numCPU);
185 // set the mib for hw.ncpu
187 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
189 // get the number of CPUs from the system
190 sysctl(mib, 2, &numCPU, &len, NULL, 0);
195 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
218 inline void _opencv_ffmpeg_free(void** ptr)
225 struct CvCapture_FFMPEG
227 bool open( const char* filename );
230 double getProperty(int) const;
231 bool setProperty(int, double);
233 bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
237 void seek(int64_t frame_number);
238 void seek(double sec);
239 bool slowSeek( int framenumber );
241 int64_t get_total_frames() const;
242 double get_duration_sec() const;
243 double get_fps() const;
244 int get_bitrate() const;
246 double r2d(AVRational r) const;
247 int64_t dts_to_frame_number(int64_t dts);
248 double dts_to_sec(int64_t dts);
250 AVFormatContext * ic;
260 struct SwsContext *img_convert_ctx;
262 int64_t frame_number, first_frame_number;
266 'filename' contains the filename of the videosource,
267 'filename==NULL' indicates that ffmpeg's seek support works
268 for the particular file.
269 'filename!=NULL' indicates that the slow fallback function is used for seeking,
270 and so the filename is needed to reopen the file on backward seeking.
274 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
279 void CvCapture_FFMPEG::init()
285 picture_pts = AV_NOPTS_VALUE_;
286 first_frame_number = -1;
287 memset( &rgb_picture, 0, sizeof(rgb_picture) );
288 memset( &frame, 0, sizeof(frame) );
290 memset(&packet, 0, sizeof(packet));
291 av_init_packet(&packet);
298 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
304 void CvCapture_FFMPEG::close()
306 if( img_convert_ctx )
308 sws_freeContext(img_convert_ctx);
314 // FFmpeg and Libav added avcodec_free_frame in different versions.
315 #if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
316 ? CALC_FFMPEG_VERSION(54, 59, 100) : CALC_FFMPEG_VERSION(54, 28, 0))
317 avcodec_free_frame(&picture);
325 #if LIBAVFORMAT_BUILD > 4628
326 avcodec_close( video_st->codec );
329 avcodec_close( &(video_st->codec) );
337 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
338 av_close_input_file(ic);
340 avformat_close_input(&ic);
346 if( rgb_picture.data[0] )
348 free( rgb_picture.data[0] );
349 rgb_picture.data[0] = 0;
352 // free last packet if exist
354 av_free_packet (&packet);
358 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
367 #ifndef AVSEEK_FLAG_FRAME
368 #define AVSEEK_FLAG_FRAME 0
370 #ifndef AVSEEK_FLAG_ANY
371 #define AVSEEK_FLAG_ANY 1
377 ImplMutex() { init(); }
378 ~ImplMutex() { destroy(); }
392 ImplMutex(const ImplMutex&);
393 ImplMutex& operator = (const ImplMutex& m);
396 #if defined WIN32 || defined _WIN32 || defined WINCE
398 struct ImplMutex::Impl
402 #if (_WIN32_WINNT >= 0x0600)
403 ::InitializeCriticalSectionEx(&cs, 1000, 0);
405 ::InitializeCriticalSection(&cs);
409 void destroy() { DeleteCriticalSection(&cs); }
411 void lock() { EnterCriticalSection(&cs); }
412 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
413 void unlock() { LeaveCriticalSection(&cs); }
420 static int _interlockedExchangeAdd(int* addr, int delta)
422 #if defined _MSC_VER && _MSC_VER >= 1500
423 return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
425 return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
430 #elif defined __APPLE__
432 #include <libkern/OSAtomic.h>
434 struct ImplMutex::Impl
436 void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
439 void lock() { OSSpinLockLock(&sl); }
440 bool trylock() { return OSSpinLockTry(&sl); }
441 void unlock() { OSSpinLockUnlock(&sl); }
447 #elif defined __linux__ && !defined ANDROID
449 struct ImplMutex::Impl
451 void init() { pthread_spin_init(&sl, 0); refcount = 1; }
452 void destroy() { pthread_spin_destroy(&sl); }
454 void lock() { pthread_spin_lock(&sl); }
455 bool trylock() { return pthread_spin_trylock(&sl) == 0; }
456 void unlock() { pthread_spin_unlock(&sl); }
458 pthread_spinlock_t sl;
464 struct ImplMutex::Impl
466 void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
467 void destroy() { pthread_mutex_destroy(&sl); }
469 void lock() { pthread_mutex_lock(&sl); }
470 bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
471 void unlock() { pthread_mutex_unlock(&sl); }
479 void ImplMutex::init()
481 impl = (Impl*)malloc(sizeof(Impl));
484 void ImplMutex::destroy()
490 void ImplMutex::lock() { impl->lock(); }
491 void ImplMutex::unlock() { impl->unlock(); }
492 bool ImplMutex::trylock() { return impl->trylock(); }
494 static int LockCallBack(void **mutex, AVLockOp op)
496 ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
500 localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
511 case AV_LOCK_RELEASE:
512 localMutex->unlock();
515 case AV_LOCK_DESTROY:
516 localMutex->destroy();
525 static ImplMutex _mutex;
526 static bool _initialized = false;
528 class InternalFFMpegRegister
531 InternalFFMpegRegister()
536 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
537 avformat_network_init();
540 /* register all codecs, demux and protocols */
543 /* register a callback function for synchronization */
544 av_lockmgr_register(&LockCallBack);
546 av_log_set_level(AV_LOG_ERROR);
553 ~InternalFFMpegRegister()
555 _initialized = false;
556 av_lockmgr_register(NULL);
560 static InternalFFMpegRegister _init;
562 bool CvCapture_FFMPEG::open( const char* _filename )
569 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
570 av_dict_set(&dict, "rtsp_transport", "tcp", 0);
571 int err = avformat_open_input(&ic, _filename, NULL, NULL);
573 int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
578 CV_WARN("Error opening file");
582 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
583 avformat_find_stream_info(ic, NULL);
585 av_find_stream_info(ic);
589 CV_WARN("Could not find codec parameters");
592 for(i = 0; i < ic->nb_streams; i++)
594 #if LIBAVFORMAT_BUILD > 4628
595 AVCodecContext *enc = ic->streams[i]->codec;
597 AVCodecContext *enc = &ic->streams[i]->codec;
600 //#ifdef FF_API_THREAD_INIT
601 // avcodec_thread_init(enc, get_number_of_cpus());
603 enc->thread_count = get_number_of_cpus();
606 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
607 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
610 if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
612 // backup encoder' width/height
613 int enc_width = enc->width;
614 int enc_height = enc->height;
616 AVCodec *codec = avcodec_find_decoder(enc->codec_id);
618 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
619 avcodec_open2(enc, codec, NULL)
621 avcodec_open(enc, codec)
626 // checking width/height (since decoder can sometimes alter it, eg. vp6f)
627 if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
628 if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
631 video_st = ic->streams[i];
632 picture = avcodec_alloc_frame();
634 rgb_picture.data[0] = (uint8_t*)malloc(
635 avpicture_get_size( PIX_FMT_BGR24,
636 enc->width, enc->height ));
637 avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
638 PIX_FMT_BGR24, enc->width, enc->height );
640 frame.width = enc->width;
641 frame.height = enc->height;
643 frame.step = rgb_picture.linesize[0];
644 frame.data = rgb_picture.data[0];
649 if(video_stream >= 0) valid = true;
660 bool CvCapture_FFMPEG::grabFrame()
666 const int max_number_of_attempts = 1 << 9;
668 if( !ic || !video_st ) return false;
670 if( ic->streams[video_stream]->nb_frames > 0 &&
671 frame_number > ic->streams[video_stream]->nb_frames )
674 picture_pts = AV_NOPTS_VALUE_;
676 // get the next frame
680 av_free_packet (&packet);
681 int ret = av_read_frame(ic, &packet);
682 if (ret == AVERROR(EAGAIN)) continue;
684 /* else if (ret < 0) break; */
686 if( packet.stream_index != video_stream )
688 av_free_packet (&packet);
690 if (count_errs > max_number_of_attempts)
695 // Decode video frame
696 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
697 avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
698 #elif LIBAVFORMAT_BUILD > 4628
699 avcodec_decode_video(video_st->codec,
700 picture, &got_picture,
701 packet.data, packet.size);
703 avcodec_decode_video(&video_st->codec,
704 picture, &got_picture,
705 packet.data, packet.size);
708 // Did we get a video frame?
711 //picture_pts = picture->best_effort_timestamp;
712 if( picture_pts == AV_NOPTS_VALUE_ )
713 picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
720 if (count_errs > max_number_of_attempts)
725 if( valid && first_frame_number < 0 )
726 first_frame_number = dts_to_frame_number(picture_pts);
728 // return if we have a new picture or not
733 bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
735 if( !video_st || !picture->data[0] )
738 avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24,
739 video_st->codec->width, video_st->codec->height);
741 if( img_convert_ctx == NULL ||
742 frame.width != video_st->codec->width ||
743 frame.height != video_st->codec->height )
745 if( img_convert_ctx )
746 sws_freeContext(img_convert_ctx);
748 frame.width = video_st->codec->width;
749 frame.height = video_st->codec->height;
751 img_convert_ctx = sws_getCachedContext(
753 video_st->codec->width, video_st->codec->height,
754 video_st->codec->pix_fmt,
755 video_st->codec->width, video_st->codec->height,
761 if (img_convert_ctx == NULL)
762 return false;//CV_Error(0, "Cannot initialize the conversion context!");
769 0, video_st->codec->height,
776 *width = frame.width;
777 *height = frame.height;
784 double CvCapture_FFMPEG::getProperty( int property_id ) const
786 if( !video_st ) return 0;
788 switch( property_id )
790 case CV_FFMPEG_CAP_PROP_POS_MSEC:
791 return 1000.0*(double)frame_number/get_fps();
792 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
793 return (double)frame_number;
794 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
795 return r2d(ic->streams[video_stream]->time_base);
796 case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
797 return (double)get_total_frames();
798 case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
799 return (double)frame.width;
800 case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
801 return (double)frame.height;
802 case CV_FFMPEG_CAP_PROP_FPS:
803 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
804 return av_q2d(video_st->avg_frame_rate);
805 #elif LIBAVCODEC_BUILD > 4753
806 return av_q2d(video_st->r_frame_rate);
808 return (double)video_st->codec.frame_rate
809 / (double)video_st->codec.frame_rate_base;
811 case CV_FFMPEG_CAP_PROP_FOURCC:
812 #if LIBAVFORMAT_BUILD > 4628
813 return (double)video_st->codec->codec_tag;
815 return (double)video_st->codec.codec_tag;
824 double CvCapture_FFMPEG::r2d(AVRational r) const
826 return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
829 double CvCapture_FFMPEG::get_duration_sec() const
831 double sec = (double)ic->duration / (double)AV_TIME_BASE;
835 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
840 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
846 int CvCapture_FFMPEG::get_bitrate() const
851 double CvCapture_FFMPEG::get_fps() const
853 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
854 double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
856 double fps = r2d(ic->streams[video_stream]->r_frame_rate);
859 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
862 fps = r2d(ic->streams[video_stream]->avg_frame_rate);
868 fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
874 int64_t CvCapture_FFMPEG::get_total_frames() const
876 int64_t nbf = ic->streams[video_stream]->nb_frames;
880 nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
885 int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
887 double sec = dts_to_sec(dts);
888 return (int64_t)(get_fps() * sec + 0.5);
891 double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
893 return (double)(dts - ic->streams[video_stream]->start_time) *
894 r2d(ic->streams[video_stream]->time_base);
897 void CvCapture_FFMPEG::seek(int64_t _frame_number)
899 _frame_number = std::min(_frame_number, get_total_frames());
902 // if we have not grabbed a single frame before first seek, let's read the first frame
903 // and get some valuable information during the process
904 if( first_frame_number < 0 && get_total_frames() > 1 )
909 int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
910 double sec = (double)_frame_number_temp / get_fps();
911 int64_t time_stamp = ic->streams[video_stream]->start_time;
912 double time_base = r2d(ic->streams[video_stream]->time_base);
913 time_stamp += (int64_t)(sec / time_base + 0.5);
914 if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
915 avcodec_flush_buffers(ic->streams[video_stream]->codec);
916 if( _frame_number > 0 )
920 if( _frame_number > 1 )
922 frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
923 //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
924 // (int)_frame_number, (int)frame_number, delta);
926 if( frame_number < 0 || frame_number > _frame_number-1 )
928 if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
930 delta = delta < 16 ? delta*2 : delta*3/2;
933 while( frame_number < _frame_number-1 )
955 void CvCapture_FFMPEG::seek(double sec)
957 seek((int64_t)(sec * get_fps() + 0.5));
960 bool CvCapture_FFMPEG::setProperty( int property_id, double value )
962 if( !video_st ) return false;
964 switch( property_id )
966 case CV_FFMPEG_CAP_PROP_POS_MSEC:
967 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
968 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
970 switch( property_id )
972 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
973 seek((int64_t)value);
976 case CV_FFMPEG_CAP_PROP_POS_MSEC:
980 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
981 seek((int64_t)(value*ic->duration));
985 picture_pts=(int64_t)value;
996 ///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
997 struct CvVideoWriter_FFMPEG
999 bool open( const char* filename, int fourcc,
1000 double fps, int width, int height, bool isColor );
1002 bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
1006 AVOutputFormat * fmt;
1007 AVFormatContext * oc;
1009 uint32_t outbuf_size;
1012 AVFrame * input_picture;
1014 AVStream * video_st;
1016 Image_FFMPEG temp_image;
1017 int frame_width, frame_height;
1020 struct SwsContext *img_convert_ctx;
1023 static const char * icvFFMPEGErrStr(int err)
1025 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1027 case AVERROR_BSF_NOT_FOUND:
1028 return "Bitstream filter not found";
1029 case AVERROR_DECODER_NOT_FOUND:
1030 return "Decoder not found";
1031 case AVERROR_DEMUXER_NOT_FOUND:
1032 return "Demuxer not found";
1033 case AVERROR_ENCODER_NOT_FOUND:
1034 return "Encoder not found";
1036 return "End of file";
1038 return "Immediate exit was requested; the called function should not be restarted";
1039 case AVERROR_FILTER_NOT_FOUND:
1040 return "Filter not found";
1041 case AVERROR_INVALIDDATA:
1042 return "Invalid data found when processing input";
1043 case AVERROR_MUXER_NOT_FOUND:
1044 return "Muxer not found";
1045 case AVERROR_OPTION_NOT_FOUND:
1046 return "Option not found";
1047 case AVERROR_PATCHWELCOME:
1048 return "Not yet implemented in FFmpeg, patches welcome";
1049 case AVERROR_PROTOCOL_NOT_FOUND:
1050 return "Protocol not found";
1051 case AVERROR_STREAM_NOT_FOUND:
1052 return "Stream not found";
1058 case AVERROR_NUMEXPECTED:
1059 return "Incorrect filename syntax";
1060 case AVERROR_INVALIDDATA:
1061 return "Invalid data in header";
1063 return "Unknown format";
1065 return "I/O error occurred";
1067 return "Memory allocation error";
1073 return "Unspecified error";
1076 /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1078 enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1081 void CvVideoWriter_FFMPEG::init()
1093 memset(&temp_image, 0, sizeof(temp_image));
1094 img_convert_ctx = 0;
1095 frame_width = frame_height = 0;
1101 * the following function is a modified version of code
1102 * found in ffmpeg-0.4.9-pre1/output_example.c
1104 static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1107 uint8_t * picture_buf;
1110 picture = avcodec_alloc_frame();
1113 size = avpicture_get_size( (PixelFormat) pix_fmt, width, height);
1115 picture_buf = (uint8_t *) malloc(size);
1121 avpicture_fill((AVPicture *)picture, picture_buf,
1122 (PixelFormat) pix_fmt, width, height);
1129 /* add a video output stream to the container */
1130 static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1131 CV_CODEC_ID codec_id,
1132 int w, int h, int bitrate,
1133 double fps, int pixel_format)
1137 int frame_rate, frame_rate_base;
1140 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1141 st = avformat_new_stream(oc, 0);
1143 st = av_new_stream(oc, 0);
1147 CV_WARN("Could not allocate stream");
1151 #if LIBAVFORMAT_BUILD > 4628
1157 #if LIBAVFORMAT_BUILD > 4621
1158 c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1160 c->codec_id = oc->oformat->video_codec;
1163 if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1164 c->codec_id = codec_id;
1167 //if(codec_tag) c->codec_tag=codec_tag;
1168 codec = avcodec_find_encoder(c->codec_id);
1170 c->codec_type = AVMEDIA_TYPE_VIDEO;
1172 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
1173 // Set per-codec defaults
1174 AVCodecID c_id = c->codec_id;
1175 avcodec_get_context_defaults3(c, codec);
1176 // avcodec_get_context_defaults3 erases codec_id for some reason
1180 /* put sample parameters */
1181 int64_t lbit_rate = (int64_t)bitrate;
1182 lbit_rate += (bitrate / 2);
1183 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1184 c->bit_rate = lbit_rate;
1187 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1190 /* resolution must be a multiple of two */
1194 /* time base: this is the fundamental unit of time (in seconds) in terms
1195 of which frame timestamps are represented. for fixed-fps content,
1196 timebase should be 1/framerate and timestamp increments should be
1198 frame_rate=(int)(fps+0.5);
1200 while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
1201 frame_rate_base*=10;
1202 frame_rate=(int)(fps*frame_rate_base + 0.5);
1204 #if LIBAVFORMAT_BUILD > 4752
1205 c->time_base.den = frame_rate;
1206 c->time_base.num = frame_rate_base;
1207 /* adjust time base for supported framerates */
1208 if(codec && codec->supported_framerates){
1209 const AVRational *p= codec->supported_framerates;
1210 AVRational req = {frame_rate, frame_rate_base};
1211 const AVRational *best=NULL;
1212 AVRational best_error= {INT_MAX, 1};
1213 for(; p->den!=0; p++){
1214 AVRational error= av_sub_q(req, *p);
1215 if(error.num <0) error.num *= -1;
1216 if(av_cmp_q(error, best_error) < 0){
1221 c->time_base.den= best->num;
1222 c->time_base.num= best->den;
1225 c->frame_rate = frame_rate;
1226 c->frame_rate_base = frame_rate_base;
1229 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1230 c->pix_fmt = (PixelFormat) pixel_format;
1232 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1233 c->max_b_frames = 2;
1235 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1236 /* needed to avoid using macroblocks in which some coeffs overflow
1237 this doesnt happen with normal video, it just happens here as the
1238 motion of the chroma plane doesnt match the luma plane */
1239 /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1243 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
1244 /* Some settings for libx264 encoding, restore dummy values for gop_size
1245 and qmin since they will be set to reasonable defaults by the libx264
1246 preset system. Also, use a crf encode with the default quality rating,
1247 this seems easier than finding an appropriate default bitrate. */
1248 if (c->codec_id == AV_CODEC_ID_H264) {
1252 av_opt_set(c->priv_data,"crf","23", 0);
1256 #if LIBAVCODEC_VERSION_INT>0x000409
1257 // some formats want stream headers to be seperate
1258 if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1260 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1267 static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1269 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
1270 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1271 uint8_t *, uint32_t,
1273 uint8_t * outbuf, uint32_t outbuf_size,
1277 #if LIBAVFORMAT_BUILD > 4628
1278 AVCodecContext * c = video_st->codec;
1280 AVCodecContext * c = &(video_st->codec);
1282 int ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1284 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
1285 /* raw video case. The API will change slightly in the near
1288 av_init_packet(&pkt);
1290 #ifndef PKT_FLAG_KEY
1291 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
1294 pkt.flags |= PKT_FLAG_KEY;
1295 pkt.stream_index= video_st->index;
1296 pkt.data= (uint8_t *)picture;
1297 pkt.size= sizeof(AVPicture);
1299 ret = av_write_frame(oc, &pkt);
1301 /* encode the image */
1303 av_init_packet(&pkt);
1304 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1308 ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
1311 else if (got_output) {
1312 if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
1313 pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
1314 if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
1315 pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
1317 pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
1318 pkt.stream_index= video_st->index;
1319 ret = av_write_frame(oc, &pkt);
1320 av_free_packet(&pkt);
1323 ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1325 int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1326 /* if zero size, it means the image was buffered */
1328 #if LIBAVFORMAT_BUILD > 4752
1329 if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1330 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1332 pkt.pts = c->coded_frame->pts;
1334 if(c->coded_frame->key_frame)
1335 pkt.flags |= PKT_FLAG_KEY;
1336 pkt.stream_index= video_st->index;
1340 /* write the compressed frame in the media file */
1341 ret = av_write_frame(oc, &pkt);
1348 /// write a frame with FFMPEG
1349 bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1353 if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1355 width = frame_width;
1356 height = frame_height;
1358 // typecast from opaque data type to implemented struct
1359 #if LIBAVFORMAT_BUILD > 4628
1360 AVCodecContext *c = video_st->codec;
1362 AVCodecContext *c = &(video_st->codec);
1365 #if LIBAVFORMAT_BUILD < 5231
1366 // It is not needed in the latest versions of the ffmpeg
1367 if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
1369 if( !temp_image.data )
1371 temp_image.step = (width*cn + 3) & -4;
1372 temp_image.width = width;
1373 temp_image.height = height;
1375 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1377 for( int y = 0; y < height; y++ )
1378 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
1379 data = temp_image.data;
1380 step = temp_image.step;
1383 if( width*cn != step )
1385 if( !temp_image.data )
1387 temp_image.step = width*cn;
1388 temp_image.width = width;
1389 temp_image.height = height;
1391 temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1394 for( int y = 0; y < height; y++ )
1395 memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
1397 for( int y = 0; y < height; y++ )
1398 memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
1399 data = temp_image.data;
1400 step = temp_image.step;
1405 if (input_pix_fmt == PIX_FMT_BGR24) {
1410 else if (input_pix_fmt == PIX_FMT_GRAY8) {
1419 if ( c->pix_fmt != input_pix_fmt ) {
1420 assert( input_picture );
1421 // let input_picture point to the raw data buffer of 'image'
1422 avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1423 (PixelFormat)input_pix_fmt, width, height);
1425 if( !img_convert_ctx )
1427 img_convert_ctx = sws_getContext(width,
1429 (PixelFormat)input_pix_fmt,
1435 if( !img_convert_ctx )
1439 if ( sws_scale(img_convert_ctx, input_picture->data,
1440 input_picture->linesize, 0,
1442 picture->data, picture->linesize) < 0 )
1446 avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1447 (PixelFormat)input_pix_fmt, width, height);
1450 picture->pts = frame_idx;
1451 ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1457 /// close video output stream and free associated memory
1458 void CvVideoWriter_FFMPEG::close()
1460 // nothing to do if already released
1464 /* no more frame to compress. The codec has a latency of a few
1465 frames if using B frames, so we get the last frames by
1466 passing the same picture again */
1467 // TODO -- do we need to account for latency here?
1469 /* write the trailer, if any */
1472 if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1476 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1477 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1481 av_write_trailer(oc);
1484 if( img_convert_ctx )
1486 sws_freeContext(img_convert_ctx);
1487 img_convert_ctx = 0;
1491 #if LIBAVFORMAT_BUILD > 4628
1492 if( video_st->codec->pix_fmt != input_pix_fmt)
1494 if( video_st->codec.pix_fmt != input_pix_fmt)
1497 if(picture->data[0])
1498 free(picture->data[0]);
1499 picture->data[0] = 0;
1504 av_free(input_picture);
1507 #if LIBAVFORMAT_BUILD > 4628
1508 avcodec_close(video_st->codec);
1510 avcodec_close(&(video_st->codec));
1515 if (!(fmt->flags & AVFMT_NOFILE))
1517 /* close the output file */
1519 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1520 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1523 url_fclose(&oc->pb);
1531 /* free the stream */
1532 avformat_free_context(oc);
1534 if( temp_image.data )
1536 free(temp_image.data);
1537 temp_image.data = 0;
1543 #define CV_PRINTABLE_CHAR(ch) ((ch) < 32 ? '?' : (ch))
1544 #define CV_TAG_TO_PRINTABLE_CHAR4(tag) CV_PRINTABLE_CHAR((tag) & 255), CV_PRINTABLE_CHAR(((tag) >> 8) & 255), CV_PRINTABLE_CHAR(((tag) >> 16) & 255), CV_PRINTABLE_CHAR(((tag) >> 24) & 255)
1546 static inline bool cv_ff_codec_tag_match(const AVCodecTag *tags, enum AVCodecID id, unsigned int tag)
1548 while (tags->id != AV_CODEC_ID_NONE)
1550 if (tags->id == id && tags->tag == tag)
1556 static inline bool cv_ff_codec_tag_list_match(const AVCodecTag *const *tags, enum AVCodecID id, unsigned int tag)
1559 for (i = 0; tags && tags[i]; i++) {
1560 bool res = cv_ff_codec_tag_match(tags[i], id, tag);
1567 /// Create a video writer object that uses FFMPEG
1568 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1569 double fps, int width, int height, bool is_color )
1571 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1572 int err, codec_pix_fmt;
1573 double bitrate_scale = 1;
1583 // we allow frames of odd width or height, but in this case we truncate
1584 // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1585 // but some internal functions inside FFMPEG swscale require even width/height.
1588 if( width <= 0 || height <= 0 )
1591 /* auto detect the output format from the name and fourcc code. */
1593 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1594 fmt = av_guess_format(NULL, filename, NULL);
1596 fmt = guess_format(NULL, filename, NULL);
1602 /* determine optimal pixel format */
1604 input_pix_fmt = PIX_FMT_BGR24;
1607 input_pix_fmt = PIX_FMT_GRAY8;
1610 /* Lookup codec_id for given fourcc */
1611 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1612 if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1614 #elif LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1616 // 2012-01-31 - dd6d3b0 - lavf 54.01.0
1617 // Add avformat_get_riff_video_tags() and avformat_get_riff_audio_tags().
1618 if( (codec_id = av_codec_get_id(fmt->codec_tag, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1620 const struct AVCodecTag * fallback_tags[] = {
1621 avformat_get_riff_video_tags(),
1622 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 25, 100)
1623 // APIchanges: ffmpeg only
1624 // 2014-01-19 - 1a193c4 - lavf 55.25.100 - avformat.h
1625 // Add avformat_get_mov_video_tags() and avformat_get_mov_audio_tags().
1626 // TODO ffmpeg only, need to skip libav: avformat_get_mov_video_tags(),
1628 codec_bmp_tags, NULL };
1629 if( (codec_id = av_codec_get_id(fallback_tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1632 fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not found (format '%s / %s')'\n",
1633 fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
1634 fmt->name, fmt->long_name);
1639 if (cv_ff_codec_tag_list_match(fmt->codec_tag, codec_id, fourcc) == false)
1642 fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not supported with codec id %d and format '%s / %s'\n",
1643 fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
1644 codec_id, fmt->name, fmt->long_name);
1646 if( (supported_tag = av_codec_get_tag(fmt->codec_tag, codec_id)) != 0 )
1648 fprintf(stderr, "OpenCV: FFMPEG: fallback to use tag 0x%08x/'%c%c%c%c'\n",
1649 supported_tag, CV_TAG_TO_PRINTABLE_CHAR4(supported_tag));
1650 fourcc = supported_tag;
1654 const struct AVCodecTag * tags[] = { codec_bmp_tags, NULL};
1655 if( (codec_id = av_codec_get_id(tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1659 // alloc memory for context
1660 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1661 oc = avformat_alloc_context();
1663 oc = av_alloc_format_context();
1669 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1671 /* set some options */
1672 oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
1674 // set a few optimal pixel formats for lossless codecs of interest..
1676 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1677 case CV_CODEC(CODEC_ID_JPEGLS):
1678 // BGR24 or GRAY8 depending on is_color...
1679 codec_pix_fmt = input_pix_fmt;
1682 case CV_CODEC(CODEC_ID_HUFFYUV):
1683 codec_pix_fmt = PIX_FMT_YUV422P;
1685 case CV_CODEC(CODEC_ID_MJPEG):
1686 case CV_CODEC(CODEC_ID_LJPEG):
1687 codec_pix_fmt = PIX_FMT_YUVJ420P;
1690 case CV_CODEC(CODEC_ID_RAWVIDEO):
1691 codec_pix_fmt = input_pix_fmt == PIX_FMT_GRAY8 ||
1692 input_pix_fmt == PIX_FMT_GRAY16LE ||
1693 input_pix_fmt == PIX_FMT_GRAY16BE ? input_pix_fmt : PIX_FMT_YUV420P;
1696 // good for lossy formats, MPEG, etc.
1697 codec_pix_fmt = PIX_FMT_YUV420P;
1701 double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1703 // TODO -- safe to ignore output audio stream?
1704 video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1705 width, height, (int)(bitrate + 0.5),
1706 fps, codec_pix_fmt);
1708 /* set the output parameters (must be done even if no
1710 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1711 if (av_set_parameters(oc, NULL) < 0) {
1717 #if FF_API_DUMP_FORMAT
1718 dump_format(oc, 0, filename, 1);
1720 av_dump_format(oc, 0, filename, 1);
1724 /* now that all the parameters are set, we can open the audio and
1725 video codecs and allocate the necessary encode buffers */
1733 #if LIBAVFORMAT_BUILD > 4628
1734 c = (video_st->codec);
1736 c = &(video_st->codec);
1739 c->codec_tag = fourcc;
1740 /* find the video encoder */
1741 codec = avcodec_find_encoder(c->codec_id);
1743 fprintf(stderr, "Could not find encoder for codec id %d: %s", c->codec_id, icvFFMPEGErrStr(
1744 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1745 AVERROR_ENCODER_NOT_FOUND
1753 int64_t lbit_rate = (int64_t)c->bit_rate;
1754 lbit_rate += (bitrate / 2);
1755 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1756 c->bit_rate_tolerance = (int)lbit_rate;
1757 c->bit_rate = (int)lbit_rate;
1759 /* open the codec */
1761 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1762 avcodec_open2(c, codec, NULL)
1764 avcodec_open(c, codec)
1767 fprintf(stderr, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
1773 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1774 /* allocate output buffer */
1775 /* assume we will never get codec output with more than 4 bytes per pixel... */
1776 outbuf_size = width*height*4;
1777 outbuf = (uint8_t *) av_malloc(outbuf_size);
1780 bool need_color_convert;
1781 need_color_convert = (c->pix_fmt != input_pix_fmt);
1783 /* allocate the encoded raw picture */
1784 picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1789 /* if the output format is not our input format, then a temporary
1790 picture of the input format is needed too. It is then converted
1791 to the required output format */
1792 input_picture = NULL;
1793 if ( need_color_convert ) {
1794 input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1795 if (!input_picture) {
1800 /* open the output file, if needed */
1801 if (!(fmt->flags & AVFMT_NOFILE)) {
1802 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1803 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1805 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1812 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1813 /* write the stream header, if any */
1814 err=avformat_write_header(oc, NULL);
1816 err=av_write_header( oc );
1825 frame_width = width;
1826 frame_height = height;
1835 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1837 CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1839 if( capture->open( filename ))
1848 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1850 if( capture && *capture )
1852 (*capture)->close();
1858 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1860 return capture->setProperty(prop_id, value);
1863 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1865 return capture->getProperty(prop_id);
1868 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1870 return capture->grabFrame();
1873 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1875 return capture->retrieveFrame(0, data, step, width, height, cn);
1878 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1879 int width, int height, int isColor )
1881 CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1883 if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1890 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1892 if( writer && *writer )
1901 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1902 const unsigned char* data, int step,
1903 int width, int height, int cn, int origin)
1905 return writer->writeFrame(data, step, width, height, cn, origin);
1914 struct OutputMediaStream_FFMPEG
1916 bool open(const char* fileName, int width, int height, double fps);
1919 void write(unsigned char* data, int size, int keyFrame);
1921 // add a video output stream to the container
1922 static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format);
1924 AVOutputFormat* fmt_;
1925 AVFormatContext* oc_;
1926 AVStream* video_st_;
1929 void OutputMediaStream_FFMPEG::close()
1931 // no more frame to compress. The codec has a latency of a few
1932 // frames if using B frames, so we get the last frames by
1933 // passing the same picture again
1935 // TODO -- do we need to account for latency here?
1939 // write the trailer, if any
1940 av_write_trailer(oc_);
1943 for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1945 av_freep(&oc_->streams[i]->codec);
1946 av_freep(&oc_->streams[i]);
1949 if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
1951 // close the output file
1953 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1954 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1955 url_fclose(oc_->pb);
1957 url_fclose(&oc_->pb);
1960 avio_close(oc_->pb);
1969 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, PixelFormat pixel_format)
1971 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1972 AVStream* st = avformat_new_stream(oc, 0);
1974 AVStream* st = av_new_stream(oc, 0);
1979 #if LIBAVFORMAT_BUILD > 4628
1980 AVCodecContext* c = st->codec;
1982 AVCodecContext* c = &(st->codec);
1985 c->codec_id = codec_id;
1986 c->codec_type = AVMEDIA_TYPE_VIDEO;
1988 // put sample parameters
1989 unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
1990 lbit_rate += (bitrate / 4);
1991 lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
1992 c->bit_rate = bitrate;
1995 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1998 // resolution must be a multiple of two
2002 AVCodec* codec = avcodec_find_encoder(c->codec_id);
2004 // time base: this is the fundamental unit of time (in seconds) in terms
2005 // of which frame timestamps are represented. for fixed-fps content,
2006 // timebase should be 1/framerate and timestamp increments should be
2009 int frame_rate = static_cast<int>(fps+0.5);
2010 int frame_rate_base = 1;
2011 while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
2013 frame_rate_base *= 10;
2014 frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
2016 c->time_base.den = frame_rate;
2017 c->time_base.num = frame_rate_base;
2019 #if LIBAVFORMAT_BUILD > 4752
2020 // adjust time base for supported framerates
2021 if (codec && codec->supported_framerates)
2023 AVRational req = {frame_rate, frame_rate_base};
2024 const AVRational* best = NULL;
2025 AVRational best_error = {INT_MAX, 1};
2027 for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
2029 AVRational error = av_sub_q(req, *p);
2034 if (av_cmp_q(error, best_error) < 0)
2041 c->time_base.den= best->num;
2042 c->time_base.num= best->den;
2046 c->gop_size = 12; // emit one intra frame every twelve frames at most
2047 c->pix_fmt = pixel_format;
2049 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
2050 c->max_b_frames = 2;
2052 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
2054 // needed to avoid using macroblocks in which some coeffs overflow
2055 // this doesnt happen with normal video, it just happens here as the
2056 // motion of the chroma plane doesnt match the luma plane
2058 // avoid FFMPEG warning 'clipping 1 dct coefficients...'
2063 #if LIBAVCODEC_VERSION_INT > 0x000409
2064 // some formats want stream headers to be seperate
2065 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
2067 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
2074 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
2080 // auto detect the output format from the name and fourcc code
2081 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2082 fmt_ = av_guess_format(NULL, fileName, NULL);
2084 fmt_ = guess_format(NULL, fileName, NULL);
2089 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
2091 // alloc memory for context
2092 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2093 oc_ = avformat_alloc_context();
2095 oc_ = av_alloc_format_context();
2101 oc_->oformat = fmt_;
2102 snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
2104 oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
2106 // set a few optimal pixel formats for lossless codecs of interest..
2107 PixelFormat codec_pix_fmt = PIX_FMT_YUV420P;
2108 int bitrate_scale = 64;
2110 // TODO -- safe to ignore output audio stream?
2111 video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
2115 // set the output parameters (must be done even if no parameters)
2116 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2117 if (av_set_parameters(oc_, NULL) < 0)
2121 // now that all the parameters are set, we can open the audio and
2122 // video codecs and allocate the necessary encode buffers
2124 #if LIBAVFORMAT_BUILD > 4628
2125 AVCodecContext* c = (video_st_->codec);
2127 AVCodecContext* c = &(video_st_->codec);
2130 c->codec_tag = MKTAG('H', '2', '6', '4');
2131 c->bit_rate_tolerance = c->bit_rate;
2133 // open the output file, if needed
2134 if (!(fmt_->flags & AVFMT_NOFILE))
2136 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2137 int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
2139 int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
2146 // write the stream header, if any
2147 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2148 av_write_header(oc_);
2150 avformat_write_header(oc_, NULL);
2156 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2158 // if zero size, it means the image was buffered
2162 av_init_packet(&pkt);
2165 pkt.flags |= PKT_FLAG_KEY;
2167 pkt.stream_index = video_st_->index;
2171 // write the compressed frame in the media file
2172 av_write_frame(oc_, &pkt);
2176 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2178 OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2180 if (stream->open(fileName, width, height, fps))
2189 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2195 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2197 stream->write(data, size, keyFrame);
2206 VideoCodec_MPEG1 = 0,
2212 VideoCodec_H264_SVC,
2213 VideoCodec_H264_MVC,
2216 VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2217 VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2218 VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2219 VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2220 VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')) // UYVY (4:2:2)
2225 VideoChromaFormat_Monochrome = 0,
2226 VideoChromaFormat_YUV420,
2227 VideoChromaFormat_YUV422,
2228 VideoChromaFormat_YUV444
2231 struct InputMediaStream_FFMPEG
2234 bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2237 bool read(unsigned char** data, int* size, int* endOfFile);
2240 InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2241 InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2243 AVFormatContext* ctx_;
2244 int video_stream_id_;
2248 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2253 video_stream_id_ = -1;
2254 memset(&pkt_, 0, sizeof(AVPacket));
2256 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2257 avformat_network_init();
2260 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2261 err = avformat_open_input(&ctx_, fileName, 0, 0);
2263 err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2268 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2269 err = avformat_find_stream_info(ctx_, 0);
2271 err = av_find_stream_info(ctx_);
2276 for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2278 #if LIBAVFORMAT_BUILD > 4628
2279 AVCodecContext *enc = ctx_->streams[i]->codec;
2281 AVCodecContext *enc = &ctx_->streams[i]->codec;
2284 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2286 video_stream_id_ = static_cast<int>(i);
2288 switch (enc->codec_id)
2290 case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2291 *codec = ::VideoCodec_MPEG1;
2294 case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2295 *codec = ::VideoCodec_MPEG2;
2298 case CV_CODEC(CODEC_ID_MPEG4):
2299 *codec = ::VideoCodec_MPEG4;
2302 case CV_CODEC(CODEC_ID_VC1):
2303 *codec = ::VideoCodec_VC1;
2306 case CV_CODEC(CODEC_ID_H264):
2307 *codec = ::VideoCodec_H264;
2314 switch (enc->pix_fmt)
2316 case PIX_FMT_YUV420P:
2317 *chroma_format = ::VideoChromaFormat_YUV420;
2320 case PIX_FMT_YUV422P:
2321 *chroma_format = ::VideoChromaFormat_YUV422;
2324 case PIX_FMT_YUV444P:
2325 *chroma_format = ::VideoChromaFormat_YUV444;
2332 *width = enc->coded_width;
2333 *height = enc->coded_height;
2339 if (video_stream_id_ < 0)
2342 av_init_packet(&pkt_);
2347 void InputMediaStream_FFMPEG::close()
2351 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2352 avformat_close_input(&ctx_);
2354 av_close_input_file(ctx_);
2358 // free last packet if exist
2360 av_free_packet(&pkt_);
2363 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2365 // free last packet if exist
2367 av_free_packet(&pkt_);
2369 // get the next frame
2372 int ret = av_read_frame(ctx_, &pkt_);
2374 if (ret == AVERROR(EAGAIN))
2379 if (ret == (int)AVERROR_EOF)
2384 if (pkt_.stream_index != video_stream_id_)
2386 av_free_packet(&pkt_);
2400 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2402 InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2404 if (stream && stream->open(fileName, codec, chroma_format, width, height))
2413 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2419 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2421 return stream->read(data, size, endOfFile);