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(WINCE))
51 #ifndef __OPENCV_BUILD
52 #define CV_FOURCC(c1, c2, c3, c4) (((c1) & 255) + (((c2) & 255) << 8) + (((c3) & 255) << 16) + (((c4) & 255) << 24))
55 #define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )
57 #if defined _MSC_VER && _MSC_VER >= 1200
58 #pragma warning( disable: 4244 4510 4610 )
62 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
65 #ifndef CV_UNUSED // Required for standalone compilation mode (OpenCV defines this in base.hpp)
66 #define CV_UNUSED(name) (void)name
73 #include "ffmpeg_codecs.hpp"
75 #include <libavutil/mathematics.h>
77 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
78 #include <libavutil/opt.h>
81 #if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
82 ? CALC_FFMPEG_VERSION(51, 63, 100) : CALC_FFMPEG_VERSION(54, 6, 0))
83 #include <libavutil/imgutils.h>
86 #include <libavcodec/avcodec.h>
87 #include <libswscale/swscale.h>
93 #if defined _MSC_VER && _MSC_VER >= 1200
94 #pragma warning( default: 4244 4510 4610 )
98 #define CV_WARN(message)
100 #define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
105 #if defined _MSC_VER && _MSC_VER < 1900
112 #elif defined __linux__ || defined __APPLE__ || defined __HAIKU__
115 #include <sys/types.h>
116 #include <sys/time.h>
117 #if defined __APPLE__
118 #include <sys/sysctl.h>
119 #include <mach/clock.h>
120 #include <mach/mach.h>
125 #define MIN(a, b) ((a) < (b) ? (a) : (b))
128 #if defined(__APPLE__)
129 #define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
131 #define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
135 #define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
138 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
139 # define CV_CODEC_ID AVCodecID
140 # define CV_CODEC(name) AV_##name
142 # define CV_CODEC_ID CodecID
143 # define CV_CODEC(name) name
146 #if LIBAVUTIL_BUILD < (LIBAVUTIL_VERSION_MICRO >= 100 \
147 ? CALC_FFMPEG_VERSION(51, 74, 100) : CALC_FFMPEG_VERSION(51, 42, 0))
148 #define AVPixelFormat PixelFormat
149 #define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
150 #define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
151 #define AV_PIX_FMT_GRAY8 PIX_FMT_GRAY8
152 #define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
153 #define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
154 #define AV_PIX_FMT_YUV444P PIX_FMT_YUV444P
155 #define AV_PIX_FMT_YUVJ420P PIX_FMT_YUVJ420P
156 #define AV_PIX_FMT_GRAY16LE PIX_FMT_GRAY16LE
157 #define AV_PIX_FMT_GRAY16BE PIX_FMT_GRAY16BE
161 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
164 #if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
165 ? CALC_FFMPEG_VERSION(52, 38, 100) : CALC_FFMPEG_VERSION(52, 13, 0))
166 #define USE_AV_FRAME_GET_BUFFER 1
168 #define USE_AV_FRAME_GET_BUFFER 0
169 #ifndef AV_NUM_DATA_POINTERS // required for 0.7.x/0.8.x ffmpeg releases
170 #define AV_NUM_DATA_POINTERS 4
175 #ifndef USE_AV_INTERRUPT_CALLBACK
176 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 21, 0)
177 #define USE_AV_INTERRUPT_CALLBACK 1
179 #define USE_AV_INTERRUPT_CALLBACK 0
183 #if USE_AV_INTERRUPT_CALLBACK
184 #define LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS 30000
185 #define LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS 30000
188 // http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
191 inline LARGE_INTEGER get_filetime_offset()
204 SystemTimeToFileTime(&s, &f);
205 t.QuadPart = f.dwHighDateTime;
207 t.QuadPart |= f.dwLowDateTime;
212 inline void get_monotonic_time(timespec *tv)
217 static LARGE_INTEGER offset;
218 static double frequencyToMicroseconds;
219 static int initialized = 0;
220 static BOOL usePerformanceCounter = 0;
224 LARGE_INTEGER performanceFrequency;
226 usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
227 if (usePerformanceCounter)
229 QueryPerformanceCounter(&offset);
230 frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
234 offset = get_filetime_offset();
235 frequencyToMicroseconds = 10.;
239 if (usePerformanceCounter)
241 QueryPerformanceCounter(&t);
243 GetSystemTimeAsFileTime(&f);
244 t.QuadPart = f.dwHighDateTime;
246 t.QuadPart |= f.dwLowDateTime;
249 t.QuadPart -= offset.QuadPart;
250 microseconds = (double)t.QuadPart / frequencyToMicroseconds;
251 t.QuadPart = microseconds;
252 tv->tv_sec = t.QuadPart / 1000000;
253 tv->tv_nsec = (t.QuadPart % 1000000) * 1000;
257 inline void get_monotonic_time(timespec *time)
259 #if defined(__APPLE__) && defined(__MACH__)
262 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
263 clock_get_time(cclock, &mts);
264 mach_port_deallocate(mach_task_self(), cclock);
265 time->tv_sec = mts.tv_sec;
266 time->tv_nsec = mts.tv_nsec;
268 clock_gettime(CLOCK_MONOTONIC, time);
274 inline timespec get_monotonic_time_diff(timespec start, timespec end)
277 if (end.tv_nsec - start.tv_nsec < 0)
279 temp.tv_sec = end.tv_sec - start.tv_sec - 1;
280 temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
284 temp.tv_sec = end.tv_sec - start.tv_sec;
285 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
291 inline double get_monotonic_time_diff_ms(timespec time1, timespec time2)
293 timespec delta = get_monotonic_time_diff(time1, time2);
294 double milliseconds = delta.tv_sec * 1000 + (double)delta.tv_nsec / 1000000.0;
298 #endif // USE_AV_INTERRUPT_CALLBACK
300 static int get_number_of_cpus(void)
302 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
306 GetSystemInfo( &sysinfo );
308 return (int)sysinfo.dwNumberOfProcessors;
309 #elif defined __linux__ || defined __HAIKU__
310 return (int)sysconf( _SC_NPROCESSORS_ONLN );
311 #elif defined __APPLE__
314 size_t len = sizeof(numCPU);
316 // set the mib for hw.ncpu
318 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
320 // get the number of CPUs from the system
321 sysctl(mib, 2, &numCPU, &len, NULL, 0);
326 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
349 #if USE_AV_INTERRUPT_CALLBACK
350 struct AVInterruptCallbackMetadata
353 unsigned int timeout_after_ms;
357 // https://github.com/opencv/opencv/pull/12693#issuecomment-426236731
359 inline const char* _opencv_avcodec_get_name(AVCodecID id)
361 #if LIBAVCODEC_VERSION_MICRO >= 100 \
362 && LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(53, 47, 100)
363 return avcodec_get_name(id);
365 const AVCodecDescriptor *cd;
368 if (id == AV_CODEC_ID_NONE)
372 cd = avcodec_descriptor_get(id);
377 codec = avcodec_find_decoder(id);
382 codec = avcodec_find_encoder(id);
388 return "unknown_codec";
393 inline void _opencv_ffmpeg_free(void** ptr)
400 inline int _opencv_ffmpeg_interrupt_callback(void *ptr)
402 AVInterruptCallbackMetadata* metadata = (AVInterruptCallbackMetadata*)ptr;
405 if (metadata->timeout_after_ms == 0)
407 return 0; // timeout is disabled
411 get_monotonic_time(&now);
413 metadata->timeout = get_monotonic_time_diff_ms(metadata->value, now) > metadata->timeout_after_ms;
415 return metadata->timeout ? -1 : 0;
420 inline void _opencv_ffmpeg_av_packet_unref(AVPacket *pkt)
422 #if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
423 ? CALC_FFMPEG_VERSION(55, 25, 100) : CALC_FFMPEG_VERSION(55, 16, 0))
424 av_packet_unref(pkt);
431 inline void _opencv_ffmpeg_av_image_fill_arrays(void *frame, uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
433 #if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
434 ? CALC_FFMPEG_VERSION(51, 63, 100) : CALC_FFMPEG_VERSION(54, 6, 0))
435 av_image_fill_arrays(((AVFrame*)frame)->data, ((AVFrame*)frame)->linesize, ptr, pix_fmt, width, height, 1);
437 avpicture_fill((AVPicture*)frame, ptr, pix_fmt, width, height);
442 inline int _opencv_ffmpeg_av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height)
444 #if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \
445 ? CALC_FFMPEG_VERSION(51, 63, 100) : CALC_FFMPEG_VERSION(54, 6, 0))
446 return av_image_get_buffer_size(pix_fmt, width, height, 1);
448 return avpicture_get_size(pix_fmt, width, height);
452 static AVRational _opencv_ffmpeg_get_sample_aspect_ratio(AVStream *stream)
454 #if LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_BUILD >= CALC_FFMPEG_VERSION(54, 5, 100)
455 return av_guess_sample_aspect_ratio(NULL, stream, NULL);
457 AVRational undef = {0, 1};
460 AVRational ratio = stream ? stream->sample_aspect_ratio : undef;
461 av_reduce(&ratio.num, &ratio.den, ratio.num, ratio.den, INT_MAX);
462 if (ratio.num > 0 && ratio.den > 0)
466 ratio = stream && stream->codec ? stream->codec->sample_aspect_ratio : undef;
467 av_reduce(&ratio.num, &ratio.den, ratio.num, ratio.den, INT_MAX);
468 if (ratio.num > 0 && ratio.den > 0)
476 struct CvCapture_FFMPEG
478 bool open( const char* filename );
481 double getProperty(int) const;
482 bool setProperty(int, double);
484 bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
488 void seek(int64_t frame_number);
489 void seek(double sec);
490 bool slowSeek( int framenumber );
492 int64_t get_total_frames() const;
493 double get_duration_sec() const;
494 double get_fps() const;
495 int get_bitrate() const;
497 double r2d(AVRational r) const;
498 int64_t dts_to_frame_number(int64_t dts);
499 double dts_to_sec(int64_t dts) const;
501 AVFormatContext * ic;
511 struct SwsContext *img_convert_ctx;
513 int64_t frame_number, first_frame_number;
517 'filename' contains the filename of the videosource,
518 'filename==NULL' indicates that ffmpeg's seek support works
519 for the particular file.
520 'filename!=NULL' indicates that the slow fallback function is used for seeking,
521 and so the filename is needed to reopen the file on backward seeking.
525 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
528 #if USE_AV_INTERRUPT_CALLBACK
529 AVInterruptCallbackMetadata interrupt_metadata;
533 void CvCapture_FFMPEG::init()
539 picture_pts = AV_NOPTS_VALUE_;
540 first_frame_number = -1;
541 memset( &rgb_picture, 0, sizeof(rgb_picture) );
542 memset( &frame, 0, sizeof(frame) );
544 memset(&packet, 0, sizeof(packet));
545 av_init_packet(&packet);
552 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
558 void CvCapture_FFMPEG::close()
560 if( img_convert_ctx )
562 sws_freeContext(img_convert_ctx);
568 #if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
569 ? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
570 av_frame_free(&picture);
571 #elif LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
572 ? CALC_FFMPEG_VERSION(54, 59, 100) : CALC_FFMPEG_VERSION(54, 28, 0))
573 avcodec_free_frame(&picture);
581 #if LIBAVFORMAT_BUILD > 4628
582 avcodec_close( video_st->codec );
585 avcodec_close( &(video_st->codec) );
593 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
594 av_close_input_file(ic);
596 avformat_close_input(&ic);
602 #if USE_AV_FRAME_GET_BUFFER
603 av_frame_unref(&rgb_picture);
605 if( rgb_picture.data[0] )
607 free( rgb_picture.data[0] );
608 rgb_picture.data[0] = 0;
612 // free last packet if exist
614 _opencv_ffmpeg_av_packet_unref (&packet);
618 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
627 #ifndef AVSEEK_FLAG_FRAME
628 #define AVSEEK_FLAG_FRAME 0
630 #ifndef AVSEEK_FLAG_ANY
631 #define AVSEEK_FLAG_ANY 1
637 ImplMutex() { init(); }
638 ~ImplMutex() { destroy(); }
652 ImplMutex(const ImplMutex&);
653 ImplMutex& operator = (const ImplMutex& m);
656 #if defined _WIN32 || defined WINCE
658 struct ImplMutex::Impl
662 #if (_WIN32_WINNT >= 0x0600)
663 ::InitializeCriticalSectionEx(&cs, 1000, 0);
665 ::InitializeCriticalSection(&cs);
669 void destroy() { DeleteCriticalSection(&cs); }
671 void lock() { EnterCriticalSection(&cs); }
672 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
673 void unlock() { LeaveCriticalSection(&cs); }
680 static int _interlockedExchangeAdd(int* addr, int delta)
682 #if defined _MSC_VER && _MSC_VER >= 1500
683 return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
685 return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
690 #elif defined __APPLE__
692 #include <libkern/OSAtomic.h>
694 struct ImplMutex::Impl
696 void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
699 void lock() { OSSpinLockLock(&sl); }
700 bool trylock() { return OSSpinLockTry(&sl); }
701 void unlock() { OSSpinLockUnlock(&sl); }
707 #elif defined __linux__ && !defined __ANDROID__
709 struct ImplMutex::Impl
711 void init() { pthread_spin_init(&sl, 0); refcount = 1; }
712 void destroy() { pthread_spin_destroy(&sl); }
714 void lock() { pthread_spin_lock(&sl); }
715 bool trylock() { return pthread_spin_trylock(&sl) == 0; }
716 void unlock() { pthread_spin_unlock(&sl); }
718 pthread_spinlock_t sl;
724 struct ImplMutex::Impl
726 void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
727 void destroy() { pthread_mutex_destroy(&sl); }
729 void lock() { pthread_mutex_lock(&sl); }
730 bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
731 void unlock() { pthread_mutex_unlock(&sl); }
739 void ImplMutex::init()
744 void ImplMutex::destroy()
750 void ImplMutex::lock() { impl->lock(); }
751 void ImplMutex::unlock() { impl->unlock(); }
752 bool ImplMutex::trylock() { return impl->trylock(); }
754 static int LockCallBack(void **mutex, AVLockOp op)
756 ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
760 localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
773 case AV_LOCK_RELEASE:
774 localMutex->unlock();
777 case AV_LOCK_DESTROY:
778 localMutex->destroy();
787 static ImplMutex _mutex;
788 static bool _initialized = false;
793 AutoLock(ImplMutex& m) : mutex(&m) { mutex->lock(); }
794 ~AutoLock() { mutex->unlock(); }
798 AutoLock(const AutoLock&); // disabled
799 AutoLock& operator = (const AutoLock&); // disabled
802 static void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list vargs)
804 static bool skip_header = false;
805 static int prev_level = -1;
807 if (!skip_header || level != prev_level) printf("[OPENCV:FFMPEG:%02d] ", level);
809 size_t fmt_len = strlen(fmt);
810 skip_header = fmt_len > 0 && fmt[fmt_len - 1] != '\n';
814 class InternalFFMpegRegister
817 InternalFFMpegRegister()
819 AutoLock lock(_mutex);
822 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
823 avformat_network_init();
826 /* register all codecs, demux and protocols */
829 /* register a callback function for synchronization */
830 av_lockmgr_register(&LockCallBack);
833 char* debug_option = getenv("OPENCV_FFMPEG_DEBUG");
834 if (debug_option != NULL)
836 av_log_set_level(AV_LOG_VERBOSE);
837 av_log_set_callback(ffmpeg_log_callback);
842 av_log_set_level(AV_LOG_ERROR);
849 ~InternalFFMpegRegister()
851 _initialized = false;
852 av_lockmgr_register(NULL);
856 static InternalFFMpegRegister _init;
858 bool CvCapture_FFMPEG::open( const char* _filename )
860 AutoLock lock(_mutex);
866 #if USE_AV_INTERRUPT_CALLBACK
867 /* interrupt callback */
868 interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS;
869 get_monotonic_time(&interrupt_metadata.value);
871 ic = avformat_alloc_context();
872 ic->interrupt_callback.callback = _opencv_ffmpeg_interrupt_callback;
873 ic->interrupt_callback.opaque = &interrupt_metadata;
876 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
878 char* options = getenv("OPENCV_FFMPEG_CAPTURE_OPTIONS");
881 av_dict_set(&dict, "rtsp_transport", "tcp", 0);
885 #if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 ? CALC_FFMPEG_VERSION(52, 17, 100) : CALC_FFMPEG_VERSION(52, 7, 0))
886 av_dict_parse_string(&dict, options, ";", "|", 0);
888 av_dict_set(&dict, "rtsp_transport", "tcp", 0);
892 av_dict_set(&dict, "rtsp_transport", "tcp", 0);
894 AVInputFormat* input_format = NULL;
895 AVDictionaryEntry* entry = av_dict_get(dict, "input_format", NULL, 0);
898 input_format = av_find_input_format(entry->value);
901 int err = avformat_open_input(&ic, _filename, input_format, &dict);
903 int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
908 CV_WARN("Error opening file");
913 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
914 avformat_find_stream_info(ic, NULL);
916 av_find_stream_info(ic);
920 CV_WARN("Could not find codec parameters");
923 for(i = 0; i < ic->nb_streams; i++)
925 #if LIBAVFORMAT_BUILD > 4628
926 AVCodecContext *enc = ic->streams[i]->codec;
928 AVCodecContext *enc = &ic->streams[i]->codec;
931 //#ifdef FF_API_THREAD_INIT
932 // avcodec_thread_init(enc, get_number_of_cpus());
934 enc->thread_count = get_number_of_cpus();
937 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
938 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
941 if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
943 // backup encoder' width/height
944 int enc_width = enc->width;
945 int enc_height = enc->height;
948 if(av_dict_get(dict, "video_codec", NULL, 0) == NULL) {
949 codec = avcodec_find_decoder(enc->codec_id);
951 codec = avcodec_find_decoder_by_name(av_dict_get(dict, "video_codec", NULL, 0)->value);
954 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
955 avcodec_open2(enc, codec, NULL)
957 avcodec_open(enc, codec)
962 // checking width/height (since decoder can sometimes alter it, eg. vp6f)
963 if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
964 if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
967 video_st = ic->streams[i];
968 #if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
969 ? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
970 picture = av_frame_alloc();
972 picture = avcodec_alloc_frame();
975 frame.width = enc->width;
976 frame.height = enc->height;
984 if(video_stream >= 0) valid = true;
988 #if USE_AV_INTERRUPT_CALLBACK
989 // deactivate interrupt callback
990 interrupt_metadata.timeout_after_ms = 0;
1000 bool CvCapture_FFMPEG::grabFrame()
1006 const int max_number_of_attempts = 1 << 9;
1008 if( !ic || !video_st ) return false;
1010 if( ic->streams[video_stream]->nb_frames > 0 &&
1011 frame_number > ic->streams[video_stream]->nb_frames )
1014 picture_pts = AV_NOPTS_VALUE_;
1016 #if USE_AV_INTERRUPT_CALLBACK
1017 // activate interrupt callback
1018 get_monotonic_time(&interrupt_metadata.value);
1019 interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS;
1022 // get the next frame
1026 _opencv_ffmpeg_av_packet_unref (&packet);
1028 #if USE_AV_INTERRUPT_CALLBACK
1029 if (interrupt_metadata.timeout)
1036 int ret = av_read_frame(ic, &packet);
1037 if (ret == AVERROR(EAGAIN)) continue;
1039 /* else if (ret < 0) break; */
1041 if( packet.stream_index != video_stream )
1043 _opencv_ffmpeg_av_packet_unref (&packet);
1045 if (count_errs > max_number_of_attempts)
1050 // Decode video frame
1051 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1052 avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
1053 #elif LIBAVFORMAT_BUILD > 4628
1054 avcodec_decode_video(video_st->codec,
1055 picture, &got_picture,
1056 packet.data, packet.size);
1058 avcodec_decode_video(&video_st->codec,
1059 picture, &got_picture,
1060 packet.data, packet.size);
1063 // Did we get a video frame?
1066 //picture_pts = picture->best_effort_timestamp;
1067 if( picture_pts == AV_NOPTS_VALUE_ )
1068 picture_pts = picture->pkt_pts != AV_NOPTS_VALUE_ && picture->pkt_pts != 0 ? picture->pkt_pts : picture->pkt_dts;
1076 if (count_errs > max_number_of_attempts)
1081 if( valid && first_frame_number < 0 )
1082 first_frame_number = dts_to_frame_number(picture_pts);
1084 #if USE_AV_INTERRUPT_CALLBACK
1085 // deactivate interrupt callback
1086 interrupt_metadata.timeout_after_ms = 0;
1089 // return if we have a new picture or not
1094 bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
1096 if( !video_st || !picture->data[0] )
1099 if( img_convert_ctx == NULL ||
1100 frame.width != video_st->codec->width ||
1101 frame.height != video_st->codec->height ||
1102 frame.data == NULL )
1104 // Some sws_scale optimizations have some assumptions about alignment of data/step/width/height
1105 // Also we use coded_width/height to workaround problem with legacy ffmpeg versions (like n0.8)
1106 int buffer_width = video_st->codec->coded_width, buffer_height = video_st->codec->coded_height;
1108 img_convert_ctx = sws_getCachedContext(
1110 buffer_width, buffer_height,
1111 video_st->codec->pix_fmt,
1112 buffer_width, buffer_height,
1118 if (img_convert_ctx == NULL)
1119 return false;//CV_Error(0, "Cannot initialize the conversion context!");
1121 #if USE_AV_FRAME_GET_BUFFER
1122 av_frame_unref(&rgb_picture);
1123 rgb_picture.format = AV_PIX_FMT_BGR24;
1124 rgb_picture.width = buffer_width;
1125 rgb_picture.height = buffer_height;
1126 if (0 != av_frame_get_buffer(&rgb_picture, 32))
1128 CV_WARN("OutOfMemory");
1132 int aligns[AV_NUM_DATA_POINTERS];
1133 avcodec_align_dimensions2(video_st->codec, &buffer_width, &buffer_height, aligns);
1134 rgb_picture.data[0] = (uint8_t*)realloc(rgb_picture.data[0],
1135 _opencv_ffmpeg_av_image_get_buffer_size( AV_PIX_FMT_BGR24,
1136 buffer_width, buffer_height ));
1137 _opencv_ffmpeg_av_image_fill_arrays(&rgb_picture, rgb_picture.data[0],
1138 AV_PIX_FMT_BGR24, buffer_width, buffer_height );
1140 frame.width = video_st->codec->width;
1141 frame.height = video_st->codec->height;
1143 frame.data = rgb_picture.data[0];
1144 frame.step = rgb_picture.linesize[0];
1151 0, video_st->codec->coded_height,
1153 rgb_picture.linesize
1158 *width = frame.width;
1159 *height = frame.height;
1166 double CvCapture_FFMPEG::getProperty( int property_id ) const
1168 if( !video_st ) return 0;
1170 double codec_tag = 0;
1171 AVCodecID codec_id = AV_CODEC_ID_NONE;
1172 const char* codec_fourcc = NULL;
1174 switch( property_id )
1176 case CV_FFMPEG_CAP_PROP_POS_MSEC:
1177 if (picture_pts == AV_NOPTS_VALUE_)
1181 return (dts_to_sec(picture_pts) * 1000);
1182 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
1183 return (double)frame_number;
1184 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
1185 return r2d(ic->streams[video_stream]->time_base);
1186 case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
1187 return (double)get_total_frames();
1188 case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
1189 return (double)frame.width;
1190 case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
1191 return (double)frame.height;
1192 case CV_FFMPEG_CAP_PROP_FPS:
1194 case CV_FFMPEG_CAP_PROP_FOURCC:
1195 #if LIBAVFORMAT_BUILD > 4628
1196 codec_id = video_st->codec->codec_id;
1197 codec_tag = (double) video_st->codec->codec_tag;
1199 codec_id = video_st->codec.codec_id;
1200 codec_tag = (double)video_st->codec.codec_tag;
1203 if(codec_tag || codec_id == AV_CODEC_ID_NONE)
1208 codec_fourcc = _opencv_avcodec_get_name(codec_id);
1209 if(!codec_fourcc || strlen(codec_fourcc) < 4 || strcmp(codec_fourcc, "unknown_codec") == 0)
1214 return (double) CV_FOURCC(codec_fourcc[0], codec_fourcc[1], codec_fourcc[2], codec_fourcc[3]);
1215 case CV_FFMPEG_CAP_PROP_SAR_NUM:
1216 return _opencv_ffmpeg_get_sample_aspect_ratio(ic->streams[video_stream]).num;
1217 case CV_FFMPEG_CAP_PROP_SAR_DEN:
1218 return _opencv_ffmpeg_get_sample_aspect_ratio(ic->streams[video_stream]).den;
1226 double CvCapture_FFMPEG::r2d(AVRational r) const
1228 return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
1231 double CvCapture_FFMPEG::get_duration_sec() const
1233 double sec = (double)ic->duration / (double)AV_TIME_BASE;
1237 sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
1243 int CvCapture_FFMPEG::get_bitrate() const
1245 return ic->bit_rate;
1248 double CvCapture_FFMPEG::get_fps() const
1250 #if 0 && LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 1, 100) && LIBAVFORMAT_VERSION_MICRO >= 100
1251 double fps = r2d(av_guess_frame_rate(ic, ic->streams[video_stream], NULL));
1253 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1254 double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
1256 double fps = r2d(ic->streams[video_stream]->r_frame_rate);
1259 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1262 fps = r2d(ic->streams[video_stream]->avg_frame_rate);
1268 fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
1274 int64_t CvCapture_FFMPEG::get_total_frames() const
1276 int64_t nbf = ic->streams[video_stream]->nb_frames;
1280 nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
1285 int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
1287 double sec = dts_to_sec(dts);
1288 return (int64_t)(get_fps() * sec + 0.5);
1291 double CvCapture_FFMPEG::dts_to_sec(int64_t dts) const
1293 return (double)(dts - ic->streams[video_stream]->start_time) *
1294 r2d(ic->streams[video_stream]->time_base);
1297 void CvCapture_FFMPEG::seek(int64_t _frame_number)
1299 _frame_number = std::min(_frame_number, get_total_frames());
1302 // if we have not grabbed a single frame before first seek, let's read the first frame
1303 // and get some valuable information during the process
1304 if( first_frame_number < 0 && get_total_frames() > 1 )
1309 int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
1310 double sec = (double)_frame_number_temp / get_fps();
1311 int64_t time_stamp = ic->streams[video_stream]->start_time;
1312 double time_base = r2d(ic->streams[video_stream]->time_base);
1313 time_stamp += (int64_t)(sec / time_base + 0.5);
1314 if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
1315 avcodec_flush_buffers(ic->streams[video_stream]->codec);
1316 if( _frame_number > 0 )
1320 if( _frame_number > 1 )
1322 frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
1323 //printf("_frame_number = %d, frame_number = %d, delta = %d\n",
1324 // (int)_frame_number, (int)frame_number, delta);
1326 if( frame_number < 0 || frame_number > _frame_number-1 )
1328 if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
1330 delta = delta < 16 ? delta*2 : delta*3/2;
1333 while( frame_number < _frame_number-1 )
1355 void CvCapture_FFMPEG::seek(double sec)
1357 seek((int64_t)(sec * get_fps() + 0.5));
1360 bool CvCapture_FFMPEG::setProperty( int property_id, double value )
1362 if( !video_st ) return false;
1364 switch( property_id )
1366 case CV_FFMPEG_CAP_PROP_POS_MSEC:
1367 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
1368 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
1370 switch( property_id )
1372 case CV_FFMPEG_CAP_PROP_POS_FRAMES:
1373 seek((int64_t)value);
1376 case CV_FFMPEG_CAP_PROP_POS_MSEC:
1380 case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
1381 seek((int64_t)(value*ic->duration));
1385 picture_pts=(int64_t)value;
1396 ///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
1397 struct CvVideoWriter_FFMPEG
1399 bool open( const char* filename, int fourcc,
1400 double fps, int width, int height, bool isColor );
1402 bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
1406 AVOutputFormat * fmt;
1407 AVFormatContext * oc;
1409 uint32_t outbuf_size;
1412 AVFrame * input_picture;
1414 AVStream * video_st;
1416 unsigned char * aligned_input;
1417 size_t aligned_input_size;
1418 int frame_width, frame_height;
1421 struct SwsContext *img_convert_ctx;
1424 static const char * icvFFMPEGErrStr(int err)
1426 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1428 case AVERROR_BSF_NOT_FOUND:
1429 return "Bitstream filter not found";
1430 case AVERROR_DECODER_NOT_FOUND:
1431 return "Decoder not found";
1432 case AVERROR_DEMUXER_NOT_FOUND:
1433 return "Demuxer not found";
1434 case AVERROR_ENCODER_NOT_FOUND:
1435 return "Encoder not found";
1437 return "End of file";
1439 return "Immediate exit was requested; the called function should not be restarted";
1440 case AVERROR_FILTER_NOT_FOUND:
1441 return "Filter not found";
1442 case AVERROR_INVALIDDATA:
1443 return "Invalid data found when processing input";
1444 case AVERROR_MUXER_NOT_FOUND:
1445 return "Muxer not found";
1446 case AVERROR_OPTION_NOT_FOUND:
1447 return "Option not found";
1448 case AVERROR_PATCHWELCOME:
1449 return "Not yet implemented in FFmpeg, patches welcome";
1450 case AVERROR_PROTOCOL_NOT_FOUND:
1451 return "Protocol not found";
1452 case AVERROR_STREAM_NOT_FOUND:
1453 return "Stream not found";
1459 case AVERROR_NUMEXPECTED:
1460 return "Incorrect filename syntax";
1461 case AVERROR_INVALIDDATA:
1462 return "Invalid data in header";
1464 return "Unknown format";
1466 return "I/O error occurred";
1468 return "Memory allocation error";
1474 return "Unspecified error";
1477 /* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1479 enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1482 void CvVideoWriter_FFMPEG::init()
1494 aligned_input = NULL;
1495 aligned_input_size = 0;
1496 img_convert_ctx = 0;
1497 frame_width = frame_height = 0;
1503 * the following function is a modified version of code
1504 * found in ffmpeg-0.4.9-pre1/output_example.c
1506 static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1509 uint8_t * picture_buf = 0;
1512 #if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
1513 ? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
1514 picture = av_frame_alloc();
1516 picture = avcodec_alloc_frame();
1521 picture->format = pix_fmt;
1522 picture->width = width;
1523 picture->height = height;
1525 size = _opencv_ffmpeg_av_image_get_buffer_size( (AVPixelFormat) pix_fmt, width, height);
1527 picture_buf = (uint8_t *) malloc(size);
1533 _opencv_ffmpeg_av_image_fill_arrays(picture, picture_buf,
1534 (AVPixelFormat) pix_fmt, width, height);
1540 /* add a video output stream to the container */
1541 static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1542 CV_CODEC_ID codec_id,
1543 int w, int h, int bitrate,
1544 double fps, int pixel_format)
1548 int frame_rate, frame_rate_base;
1551 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1552 st = avformat_new_stream(oc, 0);
1554 st = av_new_stream(oc, 0);
1558 CV_WARN("Could not allocate stream");
1562 #if LIBAVFORMAT_BUILD > 4628
1568 #if LIBAVFORMAT_BUILD > 4621
1569 c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1571 c->codec_id = oc->oformat->video_codec;
1574 if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1575 c->codec_id = codec_id;
1578 //if(codec_tag) c->codec_tag=codec_tag;
1579 codec = avcodec_find_encoder(c->codec_id);
1581 c->codec_type = AVMEDIA_TYPE_VIDEO;
1583 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
1584 // Set per-codec defaults
1585 AVCodecID c_id = c->codec_id;
1586 avcodec_get_context_defaults3(c, codec);
1587 // avcodec_get_context_defaults3 erases codec_id for some reason
1591 /* put sample parameters */
1592 int64_t lbit_rate = (int64_t)bitrate;
1593 lbit_rate += (bitrate / 2);
1594 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1595 c->bit_rate = lbit_rate;
1598 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1601 /* resolution must be a multiple of two */
1605 /* time base: this is the fundamental unit of time (in seconds) in terms
1606 of which frame timestamps are represented. for fixed-fps content,
1607 timebase should be 1/framerate and timestamp increments should be
1609 frame_rate=(int)(fps+0.5);
1611 while (fabs(((double)frame_rate/frame_rate_base) - fps) > 0.001){
1612 frame_rate_base*=10;
1613 frame_rate=(int)(fps*frame_rate_base + 0.5);
1615 #if LIBAVFORMAT_BUILD > 4752
1616 c->time_base.den = frame_rate;
1617 c->time_base.num = frame_rate_base;
1618 /* adjust time base for supported framerates */
1619 if(codec && codec->supported_framerates){
1620 const AVRational *p= codec->supported_framerates;
1621 AVRational req = {frame_rate, frame_rate_base};
1622 const AVRational *best=NULL;
1623 AVRational best_error= {INT_MAX, 1};
1624 for(; p->den!=0; p++){
1625 AVRational error= av_sub_q(req, *p);
1626 if(error.num <0) error.num *= -1;
1627 if(av_cmp_q(error, best_error) < 0){
1634 c->time_base.den= best->num;
1635 c->time_base.num= best->den;
1638 c->frame_rate = frame_rate;
1639 c->frame_rate_base = frame_rate_base;
1642 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1643 c->pix_fmt = (AVPixelFormat) pixel_format;
1645 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1646 c->max_b_frames = 2;
1648 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1649 /* needed to avoid using macroblocks in which some coeffs overflow
1650 this doesn't happen with normal video, it just happens here as the
1651 motion of the chroma plane doesn't match the luma plane */
1652 /* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1656 #if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
1657 /* Some settings for libx264 encoding, restore dummy values for gop_size
1658 and qmin since they will be set to reasonable defaults by the libx264
1659 preset system. Also, use a crf encode with the default quality rating,
1660 this seems easier than finding an appropriate default bitrate. */
1661 if (c->codec_id == AV_CODEC_ID_H264) {
1666 av_opt_set(c->priv_data,"crf","23", 0);
1670 #if LIBAVCODEC_VERSION_INT>0x000409
1671 // some formats want stream headers to be separate
1672 if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1674 #if LIBAVCODEC_BUILD > CALC_FFMPEG_VERSION(56, 35, 0)
1675 c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1677 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1682 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(52, 42, 0)
1683 #if defined(_MSC_VER)
1684 AVRational avg_frame_rate = {frame_rate, frame_rate_base};
1685 st->avg_frame_rate = avg_frame_rate;
1687 st->avg_frame_rate = (AVRational){frame_rate, frame_rate_base};
1690 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 20, 0)
1691 st->time_base = c->time_base;
1697 static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1699 static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
1700 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1701 uint8_t *, uint32_t,
1703 uint8_t * outbuf, uint32_t outbuf_size,
1707 #if LIBAVFORMAT_BUILD > 4628
1708 AVCodecContext * c = video_st->codec;
1710 AVCodecContext * c = &(video_st->codec);
1712 int ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1714 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
1715 if (oc->oformat->flags & AVFMT_RAWPICTURE)
1717 /* raw video case. The API will change slightly in the near
1720 av_init_packet(&pkt);
1722 pkt.flags |= PKT_FLAG_KEY;
1723 pkt.stream_index= video_st->index;
1724 pkt.data= (uint8_t *)picture;
1725 pkt.size= sizeof(AVPicture);
1727 ret = av_write_frame(oc, &pkt);
1732 /* encode the image */
1734 av_init_packet(&pkt);
1735 #if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1739 ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
1742 else if (got_output) {
1743 if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
1744 pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
1745 if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
1746 pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
1748 pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
1749 pkt.stream_index= video_st->index;
1750 ret = av_write_frame(oc, &pkt);
1751 _opencv_ffmpeg_av_packet_unref(&pkt);
1754 ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1756 int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1757 /* if zero size, it means the image was buffered */
1759 #if LIBAVFORMAT_BUILD > 4752
1760 if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1761 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1763 pkt.pts = c->coded_frame->pts;
1765 if(c->coded_frame->key_frame)
1766 pkt.flags |= PKT_FLAG_KEY;
1767 pkt.stream_index= video_st->index;
1771 /* write the compressed frame in the media file */
1772 ret = av_write_frame(oc, &pkt);
1779 /// write a frame with FFMPEG
1780 bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1783 if (input_pix_fmt == AV_PIX_FMT_BGR24) {
1788 else if (input_pix_fmt == AV_PIX_FMT_GRAY8) {
1797 if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1799 width = frame_width;
1800 height = frame_height;
1802 // typecast from opaque data type to implemented struct
1803 #if LIBAVFORMAT_BUILD > 4628
1804 AVCodecContext *c = video_st->codec;
1806 AVCodecContext *c = &(video_st->codec);
1809 // FFmpeg contains SIMD optimizations which can sometimes read data past
1810 // the supplied input buffer.
1811 // Related info: https://trac.ffmpeg.org/ticket/6763
1812 // 1. To ensure that doesn't happen, we pad the step to a multiple of 32
1813 // (that's the minimal alignment for which Valgrind doesn't raise any warnings).
1814 // 2. (dataend - SIMD_SIZE) and (dataend + SIMD_SIZE) is from the same 4k page
1815 const int CV_STEP_ALIGNMENT = 32;
1816 const size_t CV_SIMD_SIZE = 32;
1817 const size_t CV_PAGE_MASK = ~(4096 - 1);
1818 const unsigned char* dataend = data + ((size_t)height * step);
1819 if (step % CV_STEP_ALIGNMENT != 0 ||
1820 (((size_t)dataend - CV_SIMD_SIZE) & CV_PAGE_MASK) != (((size_t)dataend + CV_SIMD_SIZE) & CV_PAGE_MASK))
1822 int aligned_step = (step + CV_STEP_ALIGNMENT - 1) & ~(CV_STEP_ALIGNMENT - 1);
1824 size_t new_size = (aligned_step * height + CV_SIMD_SIZE);
1826 if (!aligned_input || aligned_input_size < new_size)
1829 av_freep(&aligned_input);
1830 aligned_input_size = new_size;
1831 aligned_input = (unsigned char*)av_mallocz(aligned_input_size);
1835 for( int y = 0; y < height; y++ )
1836 memcpy(aligned_input + y*aligned_step, data + (height-1-y)*step, step);
1838 for( int y = 0; y < height; y++ )
1839 memcpy(aligned_input + y*aligned_step, data + y*step, step);
1841 data = aligned_input;
1842 step = aligned_step;
1845 if ( c->pix_fmt != input_pix_fmt ) {
1846 assert( input_picture );
1847 // let input_picture point to the raw data buffer of 'image'
1848 _opencv_ffmpeg_av_image_fill_arrays(input_picture, (uint8_t *) data,
1849 (AVPixelFormat)input_pix_fmt, width, height);
1850 input_picture->linesize[0] = step;
1852 if( !img_convert_ctx )
1854 img_convert_ctx = sws_getContext(width,
1856 (AVPixelFormat)input_pix_fmt,
1862 if( !img_convert_ctx )
1866 if ( sws_scale(img_convert_ctx, input_picture->data,
1867 input_picture->linesize, 0,
1869 picture->data, picture->linesize) < 0 )
1873 _opencv_ffmpeg_av_image_fill_arrays(picture, (uint8_t *) data,
1874 (AVPixelFormat)input_pix_fmt, width, height);
1875 picture->linesize[0] = step;
1878 picture->pts = frame_idx;
1879 bool ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1885 /// close video output stream and free associated memory
1886 void CvVideoWriter_FFMPEG::close()
1888 // nothing to do if already released
1892 /* no more frame to compress. The codec has a latency of a few
1893 frames if using B frames, so we get the last frames by
1894 passing the same picture again */
1895 // TODO -- do we need to account for latency here?
1897 /* write the trailer, if any */
1900 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
1901 if (!(oc->oformat->flags & AVFMT_RAWPICTURE))
1906 int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1907 if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1911 av_write_trailer(oc);
1914 if( img_convert_ctx )
1916 sws_freeContext(img_convert_ctx);
1917 img_convert_ctx = 0;
1921 #if LIBAVFORMAT_BUILD > 4628
1922 if( video_st->codec->pix_fmt != input_pix_fmt)
1924 if( video_st->codec.pix_fmt != input_pix_fmt)
1927 if(picture->data[0])
1928 free(picture->data[0]);
1929 picture->data[0] = 0;
1934 av_free(input_picture);
1937 #if LIBAVFORMAT_BUILD > 4628
1938 avcodec_close(video_st->codec);
1940 avcodec_close(&(video_st->codec));
1947 if (!(fmt->flags & AVFMT_NOFILE))
1949 /* close the output file */
1951 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1952 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1955 url_fclose(&oc->pb);
1963 /* free the stream */
1964 avformat_free_context(oc);
1967 av_freep(&aligned_input);
1972 #define CV_PRINTABLE_CHAR(ch) ((ch) < 32 ? '?' : (ch))
1973 #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)
1975 static inline bool cv_ff_codec_tag_match(const AVCodecTag *tags, CV_CODEC_ID id, unsigned int tag)
1977 while (tags->id != AV_CODEC_ID_NONE)
1979 if (tags->id == id && tags->tag == tag)
1986 static inline bool cv_ff_codec_tag_list_match(const AVCodecTag *const *tags, CV_CODEC_ID id, unsigned int tag)
1989 for (i = 0; tags && tags[i]; i++) {
1990 bool res = cv_ff_codec_tag_match(tags[i], id, tag);
1998 static inline void cv_ff_codec_tag_dump(const AVCodecTag *const *tags)
2001 for (i = 0; tags && tags[i]; i++) {
2002 const AVCodecTag * ptags = tags[i];
2003 while (ptags->id != AV_CODEC_ID_NONE)
2005 unsigned int tag = ptags->tag;
2006 printf("fourcc tag 0x%08x/'%c%c%c%c' codec_id %04X\n", tag, CV_TAG_TO_PRINTABLE_CHAR4(tag), ptags->id);
2012 /// Create a video writer object that uses FFMPEG
2013 bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
2014 double fps, int width, int height, bool is_color )
2016 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
2017 int err, codec_pix_fmt;
2018 double bitrate_scale = 1;
2028 // we allow frames of odd width or height, but in this case we truncate
2029 // the rightmost column/the bottom row. Probably, this should be handled more elegantly,
2030 // but some internal functions inside FFMPEG swscale require even width/height.
2033 if( width <= 0 || height <= 0 )
2036 /* auto detect the output format from the name and fourcc code. */
2038 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2039 fmt = av_guess_format(NULL, filename, NULL);
2041 fmt = guess_format(NULL, filename, NULL);
2047 /* determine optimal pixel format */
2049 input_pix_fmt = AV_PIX_FMT_BGR24;
2052 input_pix_fmt = AV_PIX_FMT_GRAY8;
2057 fprintf(stderr,"OpenCV: FFMPEG: format %s / %s\n", fmt->name, fmt->long_name);
2058 cv_ff_codec_tag_dump(fmt->codec_tag);
2062 /* Lookup codec_id for given fourcc */
2063 #if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
2064 if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
2067 if( (codec_id = av_codec_get_id(fmt->codec_tag, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
2069 const struct AVCodecTag * fallback_tags[] = {
2070 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
2072 // 2012-01-31 - dd6d3b0 - lavf 54.01.0
2073 // Add avformat_get_riff_video_tags() and avformat_get_riff_audio_tags().
2074 avformat_get_riff_video_tags(),
2076 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 25, 100) && defined LIBAVFORMAT_VERSION_MICRO && LIBAVFORMAT_VERSION_MICRO >= 100
2077 // APIchanges: ffmpeg only
2078 // 2014-01-19 - 1a193c4 - lavf 55.25.100 - avformat.h
2079 // Add avformat_get_mov_video_tags() and avformat_get_mov_audio_tags().
2080 avformat_get_mov_video_tags(),
2082 codec_bmp_tags, // fallback for avformat < 54.1
2084 if( (codec_id = av_codec_get_id(fallback_tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
2087 fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not found (format '%s / %s')'\n",
2088 fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
2089 fmt->name, fmt->long_name);
2096 if (cv_ff_codec_tag_list_match(fmt->codec_tag, codec_id, fourcc) == false)
2099 fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not supported with codec id %d and format '%s / %s'\n",
2100 fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
2101 codec_id, fmt->name, fmt->long_name);
2103 if( (supported_tag = av_codec_get_tag(fmt->codec_tag, codec_id)) != 0 )
2105 fprintf(stderr, "OpenCV: FFMPEG: fallback to use tag 0x%08x/'%c%c%c%c'\n",
2106 supported_tag, CV_TAG_TO_PRINTABLE_CHAR4(supported_tag));
2107 fourcc = supported_tag;
2112 // alloc memory for context
2113 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2114 oc = avformat_alloc_context();
2116 oc = av_alloc_format_context();
2122 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
2124 /* set some options */
2125 oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
2127 // set a few optimal pixel formats for lossless codecs of interest..
2129 #if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
2130 case CV_CODEC(CODEC_ID_JPEGLS):
2131 // BGR24 or GRAY8 depending on is_color...
2132 // supported: bgr24 rgb24 gray gray16le
2133 // as of version 3.4.1
2134 codec_pix_fmt = input_pix_fmt;
2137 case CV_CODEC(CODEC_ID_HUFFYUV):
2138 // supported: yuv422p rgb24 bgra
2139 // as of version 3.4.1
2140 switch(input_pix_fmt)
2142 case AV_PIX_FMT_RGB24:
2143 case AV_PIX_FMT_BGRA:
2144 codec_pix_fmt = input_pix_fmt;
2146 case AV_PIX_FMT_BGR24:
2147 codec_pix_fmt = AV_PIX_FMT_RGB24;
2150 codec_pix_fmt = AV_PIX_FMT_YUV422P;
2154 case CV_CODEC(CODEC_ID_PNG):
2155 // supported: rgb24 rgba rgb48be rgba64be pal8 gray ya8 gray16be ya16be monob
2156 // as of version 3.4.1
2157 switch(input_pix_fmt)
2159 case AV_PIX_FMT_GRAY8:
2160 case AV_PIX_FMT_GRAY16BE:
2161 case AV_PIX_FMT_RGB24:
2162 case AV_PIX_FMT_BGRA:
2163 codec_pix_fmt = input_pix_fmt;
2165 case AV_PIX_FMT_GRAY16LE:
2166 codec_pix_fmt = AV_PIX_FMT_GRAY16BE;
2168 case AV_PIX_FMT_BGR24:
2169 codec_pix_fmt = AV_PIX_FMT_RGB24;
2172 codec_pix_fmt = AV_PIX_FMT_YUV422P;
2176 case CV_CODEC(CODEC_ID_FFV1):
2178 // as of version 3.4.1
2179 switch(input_pix_fmt)
2181 case AV_PIX_FMT_GRAY8:
2182 case AV_PIX_FMT_GRAY16LE:
2183 #ifdef AV_PIX_FMT_BGR0
2184 case AV_PIX_FMT_BGR0:
2186 case AV_PIX_FMT_BGRA:
2187 codec_pix_fmt = input_pix_fmt;
2189 case AV_PIX_FMT_GRAY16BE:
2190 codec_pix_fmt = AV_PIX_FMT_GRAY16LE;
2192 case AV_PIX_FMT_BGR24:
2193 case AV_PIX_FMT_RGB24:
2194 #ifdef AV_PIX_FMT_BGR0
2195 codec_pix_fmt = AV_PIX_FMT_BGR0;
2197 codec_pix_fmt = AV_PIX_FMT_BGRA;
2201 codec_pix_fmt = AV_PIX_FMT_YUV422P;
2205 case CV_CODEC(CODEC_ID_MJPEG):
2206 case CV_CODEC(CODEC_ID_LJPEG):
2207 codec_pix_fmt = AV_PIX_FMT_YUVJ420P;
2210 case CV_CODEC(CODEC_ID_RAWVIDEO):
2211 // RGBA is the only RGB fourcc supported by AVI and MKV format
2212 if(fourcc == CV_FOURCC('R','G','B','A'))
2214 codec_pix_fmt = AV_PIX_FMT_RGBA;
2218 switch(input_pix_fmt)
2220 case AV_PIX_FMT_GRAY8:
2221 case AV_PIX_FMT_GRAY16LE:
2222 case AV_PIX_FMT_GRAY16BE:
2223 codec_pix_fmt = input_pix_fmt;
2226 codec_pix_fmt = AV_PIX_FMT_YUV420P;
2232 // good for lossy formats, MPEG, etc.
2233 codec_pix_fmt = AV_PIX_FMT_YUV420P;
2237 double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
2239 // TODO -- safe to ignore output audio stream?
2240 video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
2241 width, height, (int)(bitrate + 0.5),
2242 fps, codec_pix_fmt);
2244 /* set the output parameters (must be done even if no
2246 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2247 if (av_set_parameters(oc, NULL) < 0) {
2253 #if FF_API_DUMP_FORMAT
2254 dump_format(oc, 0, filename, 1);
2256 av_dump_format(oc, 0, filename, 1);
2260 /* now that all the parameters are set, we can open the audio and
2261 video codecs and allocate the necessary encode buffers */
2269 #if LIBAVFORMAT_BUILD > 4628
2270 c = (video_st->codec);
2272 c = &(video_st->codec);
2275 c->codec_tag = fourcc;
2276 /* find the video encoder */
2277 codec = avcodec_find_encoder(c->codec_id);
2279 fprintf(stderr, "Could not find encoder for codec id %d: %s\n", c->codec_id, icvFFMPEGErrStr(
2280 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2281 AVERROR_ENCODER_NOT_FOUND
2289 int64_t lbit_rate = (int64_t)c->bit_rate;
2290 lbit_rate += (bitrate / 2);
2291 lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
2292 c->bit_rate_tolerance = (int)lbit_rate;
2293 c->bit_rate = (int)lbit_rate;
2295 /* open the codec */
2297 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
2298 avcodec_open2(c, codec, NULL)
2300 avcodec_open(c, codec)
2303 fprintf(stderr, "Could not open codec '%s': %s\n", codec->name, icvFFMPEGErrStr(err));
2310 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
2311 if (!(oc->oformat->flags & AVFMT_RAWPICTURE))
2314 /* allocate output buffer */
2315 /* assume we will never get codec output with more than 4 bytes per pixel... */
2316 outbuf_size = width*height*4;
2317 outbuf = (uint8_t *) av_malloc(outbuf_size);
2320 bool need_color_convert;
2321 need_color_convert = (c->pix_fmt != input_pix_fmt);
2323 /* allocate the encoded raw picture */
2324 picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
2329 /* if the output format is not our input format, then a temporary
2330 picture of the input format is needed too. It is then converted
2331 to the required output format */
2332 input_picture = NULL;
2333 if ( need_color_convert ) {
2334 input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
2335 if (!input_picture) {
2340 /* open the output file, if needed */
2341 if (!(fmt->flags & AVFMT_NOFILE)) {
2342 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2343 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
2345 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
2352 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
2353 /* write the stream header, if any */
2354 err=avformat_write_header(oc, NULL);
2356 err=av_write_header( oc );
2365 frame_width = width;
2366 frame_height = height;
2375 CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
2377 CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
2381 if( capture->open( filename ))
2390 void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
2392 if( capture && *capture )
2394 (*capture)->close();
2400 int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
2402 return capture->setProperty(prop_id, value);
2405 double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
2407 return capture->getProperty(prop_id);
2410 int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
2412 return capture->grabFrame();
2415 int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
2417 return capture->retrieveFrame(0, data, step, width, height, cn);
2420 CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
2421 int width, int height, int isColor )
2423 CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
2427 if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
2434 void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
2436 if( writer && *writer )
2445 int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
2446 const unsigned char* data, int step,
2447 int width, int height, int cn, int origin)
2449 return writer->writeFrame(data, step, width, height, cn, origin);
2458 struct OutputMediaStream_FFMPEG
2460 bool open(const char* fileName, int width, int height, double fps);
2463 void write(unsigned char* data, int size, int keyFrame);
2465 // add a video output stream to the container
2466 static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, AVPixelFormat pixel_format);
2468 AVOutputFormat* fmt_;
2469 AVFormatContext* oc_;
2470 AVStream* video_st_;
2473 void OutputMediaStream_FFMPEG::close()
2475 // no more frame to compress. The codec has a latency of a few
2476 // frames if using B frames, so we get the last frames by
2477 // passing the same picture again
2479 // TODO -- do we need to account for latency here?
2483 // write the trailer, if any
2484 av_write_trailer(oc_);
2487 for (unsigned int i = 0; i < oc_->nb_streams; ++i)
2489 av_freep(&oc_->streams[i]->codec);
2490 av_freep(&oc_->streams[i]);
2493 if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
2495 // close the output file
2497 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
2498 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
2499 url_fclose(oc_->pb);
2501 url_fclose(&oc_->pb);
2504 avio_close(oc_->pb);
2513 AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, AVPixelFormat pixel_format)
2515 AVCodec* codec = avcodec_find_encoder(codec_id);
2518 fprintf(stderr, "Could not find encoder for codec id %d\n", codec_id);
2522 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
2523 AVStream* st = avformat_new_stream(oc, 0);
2525 AVStream* st = av_new_stream(oc, 0);
2530 #if LIBAVFORMAT_BUILD > 4628
2531 AVCodecContext* c = st->codec;
2533 AVCodecContext* c = &(st->codec);
2536 c->codec_id = codec_id;
2537 c->codec_type = AVMEDIA_TYPE_VIDEO;
2539 // put sample parameters
2540 c->bit_rate = bitrate;
2543 // http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
2546 // resolution must be a multiple of two
2550 // time base: this is the fundamental unit of time (in seconds) in terms
2551 // of which frame timestamps are represented. for fixed-fps content,
2552 // timebase should be 1/framerate and timestamp increments should be
2555 int frame_rate = static_cast<int>(fps+0.5);
2556 int frame_rate_base = 1;
2557 while (fabs((static_cast<double>(frame_rate)/frame_rate_base) - fps) > 0.001)
2559 frame_rate_base *= 10;
2560 frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
2562 c->time_base.den = frame_rate;
2563 c->time_base.num = frame_rate_base;
2565 #if LIBAVFORMAT_BUILD > 4752
2566 // adjust time base for supported framerates
2567 if (codec && codec->supported_framerates)
2569 AVRational req = {frame_rate, frame_rate_base};
2570 const AVRational* best = NULL;
2571 AVRational best_error = {INT_MAX, 1};
2573 for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
2575 AVRational error = av_sub_q(req, *p);
2580 if (av_cmp_q(error, best_error) < 0)
2589 c->time_base.den= best->num;
2590 c->time_base.num= best->den;
2594 c->gop_size = 12; // emit one intra frame every twelve frames at most
2595 c->pix_fmt = pixel_format;
2597 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
2598 c->max_b_frames = 2;
2600 if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
2602 // needed to avoid using macroblocks in which some coeffs overflow
2603 // this doesn't happen with normal video, it just happens here as the
2604 // motion of the chroma plane doesn't match the luma plane
2606 // avoid FFMPEG warning 'clipping 1 dct coefficients...'
2611 #if LIBAVCODEC_VERSION_INT > 0x000409
2612 // some formats want stream headers to be separate
2613 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
2615 #if LIBAVCODEC_BUILD > CALC_FFMPEG_VERSION(56, 35, 0)
2616 c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
2618 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
2626 bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
2632 // auto detect the output format from the name and fourcc code
2633 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2634 fmt_ = av_guess_format(NULL, fileName, NULL);
2636 fmt_ = guess_format(NULL, fileName, NULL);
2641 CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
2643 // alloc memory for context
2644 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2645 oc_ = avformat_alloc_context();
2647 oc_ = av_alloc_format_context();
2653 oc_->oformat = fmt_;
2654 snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
2656 oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
2658 // set a few optimal pixel formats for lossless codecs of interest..
2659 AVPixelFormat codec_pix_fmt = AV_PIX_FMT_YUV420P;
2660 int bitrate_scale = 64;
2662 // TODO -- safe to ignore output audio stream?
2663 video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
2667 // set the output parameters (must be done even if no parameters)
2668 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2669 if (av_set_parameters(oc_, NULL) < 0)
2673 // now that all the parameters are set, we can open the audio and
2674 // video codecs and allocate the necessary encode buffers
2676 #if LIBAVFORMAT_BUILD > 4628
2677 AVCodecContext* c = (video_st_->codec);
2679 AVCodecContext* c = &(video_st_->codec);
2682 c->codec_tag = MKTAG('H', '2', '6', '4');
2683 c->bit_rate_tolerance = c->bit_rate;
2685 // open the output file, if needed
2686 if (!(fmt_->flags & AVFMT_NOFILE))
2688 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2689 int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
2691 int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
2698 // write the stream header, if any
2700 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2701 av_write_header(oc_);
2703 avformat_write_header(oc_, NULL);
2705 if (header_err != 0)
2711 void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2713 // if zero size, it means the image was buffered
2717 av_init_packet(&pkt);
2720 pkt.flags |= PKT_FLAG_KEY;
2722 pkt.stream_index = video_st_->index;
2726 // write the compressed frame in the media file
2727 av_write_frame(oc_, &pkt);
2731 struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2733 OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2737 if (stream->open(fileName, width, height, fps))
2746 void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2752 void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2754 stream->write(data, size, keyFrame);
2763 VideoCodec_MPEG1 = 0,
2769 VideoCodec_H264_SVC,
2770 VideoCodec_H264_MVC,
2773 VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2774 VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2775 VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2776 VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2777 VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')) // UYVY (4:2:2)
2782 VideoChromaFormat_Monochrome = 0,
2783 VideoChromaFormat_YUV420,
2784 VideoChromaFormat_YUV422,
2785 VideoChromaFormat_YUV444
2788 struct InputMediaStream_FFMPEG
2791 bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2794 bool read(unsigned char** data, int* size, int* endOfFile);
2797 InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2798 InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2800 AVFormatContext* ctx_;
2801 int video_stream_id_;
2804 #if USE_AV_INTERRUPT_CALLBACK
2805 AVInterruptCallbackMetadata interrupt_metadata;
2809 bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2814 video_stream_id_ = -1;
2815 memset(&pkt_, 0, sizeof(AVPacket));
2817 #if USE_AV_INTERRUPT_CALLBACK
2818 /* interrupt callback */
2819 interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS;
2820 get_monotonic_time(&interrupt_metadata.value);
2822 ctx_ = avformat_alloc_context();
2823 ctx_->interrupt_callback.callback = _opencv_ffmpeg_interrupt_callback;
2824 ctx_->interrupt_callback.opaque = &interrupt_metadata;
2827 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2828 avformat_network_init();
2831 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2832 err = avformat_open_input(&ctx_, fileName, 0, 0);
2834 err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2839 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2840 err = avformat_find_stream_info(ctx_, 0);
2842 err = av_find_stream_info(ctx_);
2847 for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2849 #if LIBAVFORMAT_BUILD > 4628
2850 AVCodecContext *enc = ctx_->streams[i]->codec;
2852 AVCodecContext *enc = &ctx_->streams[i]->codec;
2855 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2857 video_stream_id_ = static_cast<int>(i);
2859 switch (enc->codec_id)
2861 case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2862 *codec = ::VideoCodec_MPEG1;
2865 case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2866 *codec = ::VideoCodec_MPEG2;
2869 case CV_CODEC(CODEC_ID_MPEG4):
2870 *codec = ::VideoCodec_MPEG4;
2873 case CV_CODEC(CODEC_ID_VC1):
2874 *codec = ::VideoCodec_VC1;
2877 case CV_CODEC(CODEC_ID_H264):
2878 *codec = ::VideoCodec_H264;
2885 switch (enc->pix_fmt)
2887 case AV_PIX_FMT_YUV420P:
2888 *chroma_format = ::VideoChromaFormat_YUV420;
2891 case AV_PIX_FMT_YUV422P:
2892 *chroma_format = ::VideoChromaFormat_YUV422;
2895 case AV_PIX_FMT_YUV444P:
2896 *chroma_format = ::VideoChromaFormat_YUV444;
2903 *width = enc->coded_width;
2904 *height = enc->coded_height;
2910 if (video_stream_id_ < 0)
2913 av_init_packet(&pkt_);
2915 #if USE_AV_INTERRUPT_CALLBACK
2916 // deactivate interrupt callback
2917 interrupt_metadata.timeout_after_ms = 0;
2923 void InputMediaStream_FFMPEG::close()
2927 #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2928 avformat_close_input(&ctx_);
2930 av_close_input_file(ctx_);
2934 // free last packet if exist
2936 _opencv_ffmpeg_av_packet_unref(&pkt_);
2939 bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2941 bool result = false;
2943 #if USE_AV_INTERRUPT_CALLBACK
2944 // activate interrupt callback
2945 get_monotonic_time(&interrupt_metadata.value);
2946 interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS;
2949 // free last packet if exist
2951 _opencv_ffmpeg_av_packet_unref(&pkt_);
2953 // get the next frame
2956 #if USE_AV_INTERRUPT_CALLBACK
2957 if(interrupt_metadata.timeout)
2963 int ret = av_read_frame(ctx_, &pkt_);
2965 if (ret == AVERROR(EAGAIN))
2970 if (ret == (int)AVERROR_EOF)
2975 if (pkt_.stream_index != video_stream_id_)
2977 _opencv_ffmpeg_av_packet_unref(&pkt_);
2985 #if USE_AV_INTERRUPT_CALLBACK
2986 // deactivate interrupt callback
2987 interrupt_metadata.timeout_after_ms = 0;
3000 InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
3002 InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
3006 if (stream && stream->open(fileName, codec, chroma_format, width, height))
3015 void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
3021 int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
3023 return stream->read(data, size, endOfFile);