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
#if defined WIN32 || defined _WIN32
132
#elif defined __linux__ || defined __APPLE__
135
#include <sys/types.h>
136
#if defined __APPLE__
137
#include <sys/sysctl.h>
142
#define MIN(a, b) ((a) < (b) ? (a) : (b))
145
#if defined(__APPLE__)
146
#define AV_NOPTS_VALUE_ ((int64_t)0x8000000000000000LL)
148
#define AV_NOPTS_VALUE_ ((int64_t)AV_NOPTS_VALUE)
152
#define AVERROR_EOF (-MKTAG( 'E','O','F',' '))
155
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
156
# define CV_CODEC_ID AVCodecID
157
# define CV_CODEC(name) AV_##name
159
# define CV_CODEC_ID CodecID
160
# define CV_CODEC(name) name
163
#if LIBAVUTIL_BUILD < (LIBAVUTIL_VERSION_MICRO >= 100 \
164
? CALC_FFMPEG_VERSION(51, 74, 100) : CALC_FFMPEG_VERSION(51, 42, 0))
165
#define AVPixelFormat PixelFormat
166
#define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
167
#define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
168
#define AV_PIX_FMT_GRAY8 PIX_FMT_GRAY8
169
#define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
170
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
171
#define AV_PIX_FMT_YUV444P PIX_FMT_YUV444P
172
#define AV_PIX_FMT_YUVJ420P PIX_FMT_YUVJ420P
173
#define AV_PIX_FMT_GRAY16LE PIX_FMT_GRAY16LE
174
#define AV_PIX_FMT_GRAY16BE PIX_FMT_GRAY16BE
177
static int get_number_of_cpus(void)
179
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0)
181
#elif defined WIN32 || defined _WIN32
183
GetSystemInfo( &sysinfo );
185
return (int)sysinfo.dwNumberOfProcessors;
186
#elif defined __linux__
187
return (int)sysconf( _SC_NPROCESSORS_ONLN );
188
#elif defined __APPLE__
191
size_t len = sizeof(numCPU);
193
// set the mib for hw.ncpu
195
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
197
// get the number of CPUs from the system
198
sysctl(mib, 2, &numCPU, &len, NULL, 0);
203
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
226
inline void _opencv_ffmpeg_free(void** ptr)
233
struct CvCapture_FFMPEG
235
bool open( const char* filename );
238
double getProperty(int) const;
239
bool setProperty(int, double);
241
bool retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn);
245
void seek(int64_t frame_number);
246
void seek(double sec);
247
bool slowSeek( int framenumber );
249
int64_t get_total_frames() const;
250
double get_duration_sec() const;
251
double get_fps() const;
252
int get_bitrate() const;
253
AVRational get_sample_aspect_ratio(AVStream *stream) const;
255
double r2d(AVRational r) const;
256
int64_t dts_to_frame_number(int64_t dts);
257
double dts_to_sec(int64_t dts);
259
AVFormatContext * ic;
269
struct SwsContext *img_convert_ctx;
271
int64_t frame_number, first_frame_number;
275
'filename' contains the filename of the videosource,
276
'filename==NULL' indicates that ffmpeg's seek support works
277
for the particular file.
278
'filename!=NULL' indicates that the slow fallback function is used for seeking,
279
and so the filename is needed to reopen the file on backward seeking.
283
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
288
void CvCapture_FFMPEG::init()
294
picture_pts = AV_NOPTS_VALUE_;
295
first_frame_number = -1;
296
memset( &rgb_picture, 0, sizeof(rgb_picture) );
297
memset( &frame, 0, sizeof(frame) );
299
memset(&packet, 0, sizeof(packet));
300
av_init_packet(&packet);
307
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
313
void CvCapture_FFMPEG::close()
315
if( img_convert_ctx )
317
sws_freeContext(img_convert_ctx);
323
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
324
? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
325
av_frame_free(&picture);
326
#elif LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
327
? CALC_FFMPEG_VERSION(54, 59, 100) : CALC_FFMPEG_VERSION(54, 28, 0))
328
avcodec_free_frame(&picture);
336
#if LIBAVFORMAT_BUILD > 4628
337
avcodec_close( video_st->codec );
340
avcodec_close( &(video_st->codec) );
348
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 24, 2)
349
av_close_input_file(ic);
351
avformat_close_input(&ic);
357
if( rgb_picture.data[0] )
359
free( rgb_picture.data[0] );
360
rgb_picture.data[0] = 0;
363
// free last packet if exist
365
av_free_packet (&packet);
369
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
378
#ifndef AVSEEK_FLAG_FRAME
379
#define AVSEEK_FLAG_FRAME 0
381
#ifndef AVSEEK_FLAG_ANY
382
#define AVSEEK_FLAG_ANY 1
388
ImplMutex() { init(); }
389
~ImplMutex() { destroy(); }
403
ImplMutex(const ImplMutex&);
404
ImplMutex& operator = (const ImplMutex& m);
407
#if defined WIN32 || defined _WIN32 || defined WINCE
409
struct ImplMutex::Impl
413
#if (_WIN32_WINNT >= 0x0600)
414
::InitializeCriticalSectionEx(&cs, 1000, 0);
416
::InitializeCriticalSection(&cs);
420
void destroy() { DeleteCriticalSection(&cs); }
422
void lock() { EnterCriticalSection(&cs); }
423
bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
424
void unlock() { LeaveCriticalSection(&cs); }
431
static int _interlockedExchangeAdd(int* addr, int delta)
433
#if defined _MSC_VER && _MSC_VER >= 1500
434
return (int)_InterlockedExchangeAdd((long volatile*)addr, delta);
436
return (int)InterlockedExchangeAdd((long volatile*)addr, delta);
441
#elif defined __APPLE__
443
#include <libkern/OSAtomic.h>
445
struct ImplMutex::Impl
447
void init() { sl = OS_SPINLOCK_INIT; refcount = 1; }
450
void lock() { OSSpinLockLock(&sl); }
451
bool trylock() { return OSSpinLockTry(&sl); }
452
void unlock() { OSSpinLockUnlock(&sl); }
458
#elif defined __linux__ && !defined ANDROID
460
struct ImplMutex::Impl
462
void init() { pthread_spin_init(&sl, 0); refcount = 1; }
463
void destroy() { pthread_spin_destroy(&sl); }
465
void lock() { pthread_spin_lock(&sl); }
466
bool trylock() { return pthread_spin_trylock(&sl) == 0; }
467
void unlock() { pthread_spin_unlock(&sl); }
469
pthread_spinlock_t sl;
475
struct ImplMutex::Impl
477
void init() { pthread_mutex_init(&sl, 0); refcount = 1; }
478
void destroy() { pthread_mutex_destroy(&sl); }
480
void lock() { pthread_mutex_lock(&sl); }
481
bool trylock() { return pthread_mutex_trylock(&sl) == 0; }
482
void unlock() { pthread_mutex_unlock(&sl); }
490
void ImplMutex::init()
492
impl = (Impl*)malloc(sizeof(Impl));
495
void ImplMutex::destroy()
501
void ImplMutex::lock() { impl->lock(); }
502
void ImplMutex::unlock() { impl->unlock(); }
503
bool ImplMutex::trylock() { return impl->trylock(); }
505
static int LockCallBack(void **mutex, AVLockOp op)
507
ImplMutex* localMutex = reinterpret_cast<ImplMutex*>(*mutex);
511
localMutex = reinterpret_cast<ImplMutex*>(malloc(sizeof(ImplMutex)));
522
case AV_LOCK_RELEASE:
523
localMutex->unlock();
526
case AV_LOCK_DESTROY:
527
localMutex->destroy();
536
static ImplMutex _mutex;
537
static bool _initialized = false;
539
class InternalFFMpegRegister
542
InternalFFMpegRegister()
547
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
548
avformat_network_init();
551
/* register all codecs, demux and protocols */
554
/* register a callback function for synchronization */
555
av_lockmgr_register(&LockCallBack);
557
av_log_set_level(AV_LOG_ERROR);
564
~InternalFFMpegRegister()
566
_initialized = false;
567
av_lockmgr_register(NULL);
571
static InternalFFMpegRegister _init;
573
bool CvCapture_FFMPEG::open( const char* _filename )
580
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
581
av_dict_set(&dict, "rtsp_transport", "tcp", 0);
582
int err = avformat_open_input(&ic, _filename, NULL, &dict);
584
int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
589
CV_WARN("Error opening file");
594
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
595
avformat_find_stream_info(ic, NULL);
597
av_find_stream_info(ic);
601
CV_WARN("Could not find codec parameters");
604
for(i = 0; i < ic->nb_streams; i++)
606
#if LIBAVFORMAT_BUILD > 4628
607
AVCodecContext *enc = ic->streams[i]->codec;
609
AVCodecContext *enc = &ic->streams[i]->codec;
612
//#ifdef FF_API_THREAD_INIT
613
// avcodec_thread_init(enc, get_number_of_cpus());
615
enc->thread_count = get_number_of_cpus();
618
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
619
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
622
if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0)
624
// backup encoder' width/height
625
int enc_width = enc->width;
626
int enc_height = enc->height;
628
AVCodec *codec = avcodec_find_decoder(enc->codec_id);
630
#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
631
avcodec_open2(enc, codec, NULL)
633
avcodec_open(enc, codec)
638
// checking width/height (since decoder can sometimes alter it, eg. vp6f)
639
if (enc_width && (enc->width != enc_width)) { enc->width = enc_width; }
640
if (enc_height && (enc->height != enc_height)) { enc->height = enc_height; }
643
video_st = ic->streams[i];
644
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
645
? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
646
picture = av_frame_alloc();
648
picture = avcodec_alloc_frame();
651
rgb_picture.data[0] = (uint8_t*)malloc(
652
avpicture_get_size( AV_PIX_FMT_BGR24,
653
enc->width, enc->height ));
654
avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0],
655
AV_PIX_FMT_BGR24, enc->width, enc->height );
657
frame.width = enc->width;
658
frame.height = enc->height;
660
frame.step = rgb_picture.linesize[0];
661
frame.data = rgb_picture.data[0];
666
if(video_stream >= 0) valid = true;
677
bool CvCapture_FFMPEG::grabFrame()
683
const int max_number_of_attempts = 1 << 9;
685
if( !ic || !video_st ) return false;
687
if( ic->streams[video_stream]->nb_frames > 0 &&
688
frame_number > ic->streams[video_stream]->nb_frames )
691
picture_pts = AV_NOPTS_VALUE_;
693
// get the next frame
697
av_free_packet (&packet);
698
int ret = av_read_frame(ic, &packet);
699
if (ret == AVERROR(EAGAIN)) continue;
701
/* else if (ret < 0) break; */
703
if( packet.stream_index != video_stream )
705
av_free_packet (&packet);
707
if (count_errs > max_number_of_attempts)
712
// Decode video frame
713
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
714
avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
715
#elif LIBAVFORMAT_BUILD > 4628
716
avcodec_decode_video(video_st->codec,
717
picture, &got_picture,
718
packet.data, packet.size);
720
avcodec_decode_video(&video_st->codec,
721
picture, &got_picture,
722
packet.data, packet.size);
725
// Did we get a video frame?
728
//picture_pts = picture->best_effort_timestamp;
729
if( picture_pts == AV_NOPTS_VALUE_ )
730
picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;
737
if (count_errs > max_number_of_attempts)
742
if( valid && first_frame_number < 0 )
743
first_frame_number = dts_to_frame_number(picture_pts);
745
// return if we have a new picture or not
750
bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* width, int* height, int* cn)
752
if( !video_st || !picture->data[0] )
755
if( img_convert_ctx == NULL ||
756
frame.width != video_st->codec->width ||
757
frame.height != video_st->codec->height )
759
if( img_convert_ctx )
760
sws_freeContext(img_convert_ctx);
762
frame.width = video_st->codec->width;
763
frame.height = video_st->codec->height;
765
img_convert_ctx = sws_getCachedContext(
767
video_st->codec->width, video_st->codec->height,
768
video_st->codec->pix_fmt,
769
video_st->codec->width, video_st->codec->height,
775
if (img_convert_ctx == NULL)
776
return false;//CV_Error(0, "Cannot initialize the conversion context!");
778
rgb_picture.data[0] = (uint8_t*)realloc(rgb_picture.data[0],
779
avpicture_get_size( AV_PIX_FMT_BGR24,
780
video_st->codec->width, video_st->codec->height ));
781
frame.data = rgb_picture.data[0];
784
avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], AV_PIX_FMT_RGB24,
785
video_st->codec->width, video_st->codec->height);
786
frame.step = rgb_picture.linesize[0];
792
0, video_st->codec->height,
799
*width = frame.width;
800
*height = frame.height;
807
double CvCapture_FFMPEG::getProperty( int property_id ) const
809
if( !video_st ) return 0;
811
switch( property_id )
813
case CV_FFMPEG_CAP_PROP_POS_MSEC:
814
return 1000.0*(double)frame_number/get_fps();
815
case CV_FFMPEG_CAP_PROP_POS_FRAMES:
816
return (double)frame_number;
817
case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
818
return r2d(ic->streams[video_stream]->time_base);
819
case CV_FFMPEG_CAP_PROP_FRAME_COUNT:
820
return (double)get_total_frames();
821
case CV_FFMPEG_CAP_PROP_FRAME_WIDTH:
822
return (double)frame.width;
823
case CV_FFMPEG_CAP_PROP_FRAME_HEIGHT:
824
return (double)frame.height;
825
case CV_FFMPEG_CAP_PROP_FPS:
827
case CV_FFMPEG_CAP_PROP_FOURCC:
828
#if LIBAVFORMAT_BUILD > 4628
829
return (double)video_st->codec->codec_tag;
831
return (double)video_st->codec.codec_tag;
833
case CV_FFMPEG_CAP_PROP_SAR_NUM:
834
return get_sample_aspect_ratio(ic->streams[video_stream]).num;
835
case CV_FFMPEG_CAP_PROP_SAR_DEN:
836
return get_sample_aspect_ratio(ic->streams[video_stream]).den;
844
double CvCapture_FFMPEG::r2d(AVRational r) const
846
return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
849
double CvCapture_FFMPEG::get_duration_sec() const
851
double sec = (double)ic->duration / (double)AV_TIME_BASE;
855
sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
860
sec = (double)ic->streams[video_stream]->duration * r2d(ic->streams[video_stream]->time_base);
866
int CvCapture_FFMPEG::get_bitrate() const
871
double CvCapture_FFMPEG::get_fps() const
873
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
874
double fps = r2d(ic->streams[video_stream]->avg_frame_rate);
876
double fps = r2d(ic->streams[video_stream]->r_frame_rate);
879
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
882
fps = r2d(ic->streams[video_stream]->avg_frame_rate);
888
fps = 1.0 / r2d(ic->streams[video_stream]->codec->time_base);
894
int64_t CvCapture_FFMPEG::get_total_frames() const
896
int64_t nbf = ic->streams[video_stream]->nb_frames;
900
nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
905
int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
907
double sec = dts_to_sec(dts);
908
return (int64_t)(get_fps() * sec + 0.5);
911
AVRational CvCapture_FFMPEG::get_sample_aspect_ratio(AVStream *stream) const
913
AVRational undef = {0, 1};
914
AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
915
AVRational frame_sample_aspect_ratio = stream && stream->codec ? stream->codec->sample_aspect_ratio : undef;
917
av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
918
stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
919
if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
920
stream_sample_aspect_ratio = undef;
922
av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
923
frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
924
if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
925
frame_sample_aspect_ratio = undef;
927
if (stream_sample_aspect_ratio.num)
928
return stream_sample_aspect_ratio;
930
return frame_sample_aspect_ratio;
933
double CvCapture_FFMPEG::dts_to_sec(int64_t dts)
935
return (double)(dts - ic->streams[video_stream]->start_time) *
936
r2d(ic->streams[video_stream]->time_base);
939
void CvCapture_FFMPEG::seek(int64_t _frame_number)
941
_frame_number = std::min(_frame_number, get_total_frames());
944
// if we have not grabbed a single frame before first seek, let's read the first frame
945
// and get some valuable information during the process
946
if( first_frame_number < 0 && get_total_frames() > 1 )
951
int64_t _frame_number_temp = std::max(_frame_number-delta, (int64_t)0);
952
double sec = (double)_frame_number_temp / get_fps();
953
int64_t time_stamp = ic->streams[video_stream]->start_time;
954
double time_base = r2d(ic->streams[video_stream]->time_base);
955
time_stamp += (int64_t)(sec / time_base + 0.5);
956
if (get_total_frames() > 1) av_seek_frame(ic, video_stream, time_stamp, AVSEEK_FLAG_BACKWARD);
957
avcodec_flush_buffers(ic->streams[video_stream]->codec);
958
if( _frame_number > 0 )
962
if( _frame_number > 1 )
964
frame_number = dts_to_frame_number(picture_pts) - first_frame_number;
965
//printf("_frame_number = %d, frame_number = %d, delta = %d\n",
966
// (int)_frame_number, (int)frame_number, delta);
968
if( frame_number < 0 || frame_number > _frame_number-1 )
970
if( _frame_number_temp == 0 || delta >= INT_MAX/4 )
972
delta = delta < 16 ? delta*2 : delta*3/2;
975
while( frame_number < _frame_number-1 )
997
void CvCapture_FFMPEG::seek(double sec)
999
seek((int64_t)(sec * get_fps() + 0.5));
1002
bool CvCapture_FFMPEG::setProperty( int property_id, double value )
1004
if( !video_st ) return false;
1006
switch( property_id )
1008
case CV_FFMPEG_CAP_PROP_POS_MSEC:
1009
case CV_FFMPEG_CAP_PROP_POS_FRAMES:
1010
case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
1012
switch( property_id )
1014
case CV_FFMPEG_CAP_PROP_POS_FRAMES:
1015
seek((int64_t)value);
1018
case CV_FFMPEG_CAP_PROP_POS_MSEC:
1022
case CV_FFMPEG_CAP_PROP_POS_AVI_RATIO:
1023
seek((int64_t)(value*ic->duration));
1027
picture_pts=(int64_t)value;
1038
///////////////// FFMPEG CvVideoWriter implementation //////////////////////////
1039
struct CvVideoWriter_FFMPEG
1041
bool open( const char* filename, int fourcc,
1042
double fps, int width, int height, bool isColor );
1044
bool writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin );
1048
AVOutputFormat * fmt;
1049
AVFormatContext * oc;
1051
uint32_t outbuf_size;
1054
AVFrame * input_picture;
1056
AVStream * video_st;
1058
Image_FFMPEG temp_image;
1059
int frame_width, frame_height;
1062
struct SwsContext *img_convert_ctx;
1065
static const char * icvFFMPEGErrStr(int err)
1067
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1069
case AVERROR_BSF_NOT_FOUND:
1070
return "Bitstream filter not found";
1071
case AVERROR_DECODER_NOT_FOUND:
1072
return "Decoder not found";
1073
case AVERROR_DEMUXER_NOT_FOUND:
1074
return "Demuxer not found";
1075
case AVERROR_ENCODER_NOT_FOUND:
1076
return "Encoder not found";
1078
return "End of file";
1080
return "Immediate exit was requested; the called function should not be restarted";
1081
case AVERROR_FILTER_NOT_FOUND:
1082
return "Filter not found";
1083
case AVERROR_INVALIDDATA:
1084
return "Invalid data found when processing input";
1085
case AVERROR_MUXER_NOT_FOUND:
1086
return "Muxer not found";
1087
case AVERROR_OPTION_NOT_FOUND:
1088
return "Option not found";
1089
case AVERROR_PATCHWELCOME:
1090
return "Not yet implemented in FFmpeg, patches welcome";
1091
case AVERROR_PROTOCOL_NOT_FOUND:
1092
return "Protocol not found";
1093
case AVERROR_STREAM_NOT_FOUND:
1094
return "Stream not found";
1100
case AVERROR_NUMEXPECTED:
1101
return "Incorrect filename syntax";
1102
case AVERROR_INVALIDDATA:
1103
return "Invalid data in header";
1105
return "Unknown format";
1107
return "I/O error occurred";
1109
return "Memory allocation error";
1115
return "Unspecified error";
1118
/* function internal to FFMPEG (libavformat/riff.c) to lookup codec id by fourcc tag*/
1120
enum CV_CODEC_ID codec_get_bmp_id(unsigned int tag);
1123
void CvVideoWriter_FFMPEG::init()
1135
memset(&temp_image, 0, sizeof(temp_image));
1136
img_convert_ctx = 0;
1137
frame_width = frame_height = 0;
1143
* the following function is a modified version of code
1144
* found in ffmpeg-0.4.9-pre1/output_example.c
1146
static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, bool alloc)
1149
uint8_t * picture_buf;
1152
#if LIBAVCODEC_BUILD >= (LIBAVCODEC_VERSION_MICRO >= 100 \
1153
? CALC_FFMPEG_VERSION(55, 45, 101) : CALC_FFMPEG_VERSION(55, 28, 1))
1154
picture = av_frame_alloc();
1156
picture = avcodec_alloc_frame();
1161
picture->format = pix_fmt;
1162
picture->width = width;
1163
picture->height = height;
1165
size = avpicture_get_size( (AVPixelFormat) pix_fmt, width, height);
1167
picture_buf = (uint8_t *) malloc(size);
1173
avpicture_fill((AVPicture *)picture, picture_buf,
1174
(AVPixelFormat) pix_fmt, width, height);
1181
/* add a video output stream to the container */
1182
static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
1183
CV_CODEC_ID codec_id,
1184
int w, int h, int bitrate,
1185
double fps, int pixel_format)
1189
int frame_rate, frame_rate_base;
1192
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
1193
st = avformat_new_stream(oc, 0);
1195
st = av_new_stream(oc, 0);
1199
CV_WARN("Could not allocate stream");
1203
#if LIBAVFORMAT_BUILD > 4628
1209
#if LIBAVFORMAT_BUILD > 4621
1210
c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
1212
c->codec_id = oc->oformat->video_codec;
1215
if(codec_id != CV_CODEC(CODEC_ID_NONE)){
1216
c->codec_id = codec_id;
1219
//if(codec_tag) c->codec_tag=codec_tag;
1220
codec = avcodec_find_encoder(c->codec_id);
1222
c->codec_type = AVMEDIA_TYPE_VIDEO;
1224
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54,25,0)
1225
// Set per-codec defaults
1226
AVCodecID c_id = c->codec_id;
1227
avcodec_get_context_defaults3(c, codec);
1228
// avcodec_get_context_defaults3 erases codec_id for some reason
1232
/* put sample parameters */
1233
int64_t lbit_rate = (int64_t)bitrate;
1234
lbit_rate += (bitrate / 2);
1235
lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1236
c->bit_rate = lbit_rate;
1239
// http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
1242
/* resolution must be a multiple of two */
1246
/* time base: this is the fundamental unit of time (in seconds) in terms
1247
of which frame timestamps are represented. for fixed-fps content,
1248
timebase should be 1/framerate and timestamp increments should be
1250
frame_rate=(int)(fps+0.5);
1252
while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){
1253
frame_rate_base*=10;
1254
frame_rate=(int)(fps*frame_rate_base + 0.5);
1256
#if LIBAVFORMAT_BUILD > 4752
1257
c->time_base.den = frame_rate;
1258
c->time_base.num = frame_rate_base;
1259
/* adjust time base for supported framerates */
1260
if(codec && codec->supported_framerates){
1261
const AVRational *p= codec->supported_framerates;
1262
AVRational req = {frame_rate, frame_rate_base};
1263
const AVRational *best=NULL;
1264
AVRational best_error= {INT_MAX, 1};
1265
for(; p->den!=0; p++){
1266
AVRational error= av_sub_q(req, *p);
1267
if(error.num <0) error.num *= -1;
1268
if(av_cmp_q(error, best_error) < 0){
1273
c->time_base.den= best->num;
1274
c->time_base.num= best->den;
1277
c->frame_rate = frame_rate;
1278
c->frame_rate_base = frame_rate_base;
1281
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
1282
c->pix_fmt = (AVPixelFormat) pixel_format;
1284
if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO)) {
1285
c->max_b_frames = 2;
1287
if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3)){
1288
/* needed to avoid using macroblocks in which some coeffs overflow
1289
this doesnt happen with normal video, it just happens here as the
1290
motion of the chroma plane doesnt match the luma plane */
1291
/* avoid FFMPEG warning 'clipping 1 dct coefficients...' */
1295
#if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
1296
/* Some settings for libx264 encoding, restore dummy values for gop_size
1297
and qmin since they will be set to reasonable defaults by the libx264
1298
preset system. Also, use a crf encode with the default quality rating,
1299
this seems easier than finding an appropriate default bitrate. */
1300
if (c->codec_id == AV_CODEC_ID_H264) {
1305
av_opt_set(c->priv_data,"crf","23", 0);
1309
#if LIBAVCODEC_VERSION_INT>0x000409
1310
// some formats want stream headers to be seperate
1311
if(oc->oformat->flags & AVFMT_GLOBALHEADER)
1313
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1320
static const int OPENCV_NO_FRAMES_WRITTEN_CODE = 1000;
1322
static int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st,
1323
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1324
uint8_t *, uint32_t,
1326
uint8_t * outbuf, uint32_t outbuf_size,
1330
#if LIBAVFORMAT_BUILD > 4628
1331
AVCodecContext * c = video_st->codec;
1333
AVCodecContext * c = &(video_st->codec);
1335
int ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1337
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
1338
/* raw video case. The API will change slightly in the near
1341
av_init_packet(&pkt);
1343
#ifndef PKT_FLAG_KEY
1344
#define PKT_FLAG_KEY AV_PKT_FLAG_KEY
1347
pkt.flags |= PKT_FLAG_KEY;
1348
pkt.stream_index= video_st->index;
1349
pkt.data= (uint8_t *)picture;
1350
pkt.size= sizeof(AVPicture);
1352
ret = av_write_frame(oc, &pkt);
1354
/* encode the image */
1356
av_init_packet(&pkt);
1357
#if LIBAVCODEC_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1361
ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
1364
else if (got_output) {
1365
if (pkt.pts != (int64_t)AV_NOPTS_VALUE)
1366
pkt.pts = av_rescale_q(pkt.pts, c->time_base, video_st->time_base);
1367
if (pkt.dts != (int64_t)AV_NOPTS_VALUE)
1368
pkt.dts = av_rescale_q(pkt.dts, c->time_base, video_st->time_base);
1370
pkt.duration = av_rescale_q(pkt.duration, c->time_base, video_st->time_base);
1371
pkt.stream_index= video_st->index;
1372
ret = av_write_frame(oc, &pkt);
1373
av_free_packet(&pkt);
1376
ret = OPENCV_NO_FRAMES_WRITTEN_CODE;
1378
int out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
1379
/* if zero size, it means the image was buffered */
1381
#if LIBAVFORMAT_BUILD > 4752
1382
if(c->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
1383
pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
1385
pkt.pts = c->coded_frame->pts;
1387
if(c->coded_frame->key_frame)
1388
pkt.flags |= PKT_FLAG_KEY;
1389
pkt.stream_index= video_st->index;
1393
/* write the compressed frame in the media file */
1394
ret = av_write_frame(oc, &pkt);
1401
/// write a frame with FFMPEG
1402
bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int width, int height, int cn, int origin )
1406
if( (width & -2) != frame_width || (height & -2) != frame_height || !data )
1408
width = frame_width;
1409
height = frame_height;
1411
// typecast from opaque data type to implemented struct
1412
#if LIBAVFORMAT_BUILD > 4628
1413
AVCodecContext *c = video_st->codec;
1415
AVCodecContext *c = &(video_st->codec);
1418
#if LIBAVFORMAT_BUILD < 5231
1419
// It is not needed in the latest versions of the ffmpeg
1420
if( c->codec_id == CV_CODEC(CODEC_ID_RAWVIDEO) && origin != 1 )
1422
if( !temp_image.data )
1424
temp_image.step = (width*cn + 3) & -4;
1425
temp_image.width = width;
1426
temp_image.height = height;
1428
temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1430
for( int y = 0; y < height; y++ )
1431
memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, width*cn);
1432
data = temp_image.data;
1433
step = temp_image.step;
1436
if( width*cn != step )
1438
if( !temp_image.data )
1440
temp_image.step = width*cn;
1441
temp_image.width = width;
1442
temp_image.height = height;
1444
temp_image.data = (unsigned char*)malloc(temp_image.step*temp_image.height);
1447
for( int y = 0; y < height; y++ )
1448
memcpy(temp_image.data + y*temp_image.step, data + (height-1-y)*step, temp_image.step);
1450
for( int y = 0; y < height; y++ )
1451
memcpy(temp_image.data + y*temp_image.step, data + y*step, temp_image.step);
1452
data = temp_image.data;
1453
step = temp_image.step;
1458
if (input_pix_fmt == AV_PIX_FMT_BGR24) {
1463
else if (input_pix_fmt == AV_PIX_FMT_GRAY8) {
1472
if ( c->pix_fmt != input_pix_fmt ) {
1473
assert( input_picture );
1474
// let input_picture point to the raw data buffer of 'image'
1475
avpicture_fill((AVPicture *)input_picture, (uint8_t *) data,
1476
(AVPixelFormat)input_pix_fmt, width, height);
1478
if( !img_convert_ctx )
1480
img_convert_ctx = sws_getContext(width,
1482
(AVPixelFormat)input_pix_fmt,
1488
if( !img_convert_ctx )
1492
if ( sws_scale(img_convert_ctx, input_picture->data,
1493
input_picture->linesize, 0,
1495
picture->data, picture->linesize) < 0 )
1499
avpicture_fill((AVPicture *)picture, (uint8_t *) data,
1500
(AVPixelFormat)input_pix_fmt, width, height);
1503
picture->pts = frame_idx;
1504
ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, picture) >= 0;
1510
/// close video output stream and free associated memory
1511
void CvVideoWriter_FFMPEG::close()
1513
// nothing to do if already released
1517
/* no more frame to compress. The codec has a latency of a few
1518
frames if using B frames, so we get the last frames by
1519
passing the same picture again */
1520
// TODO -- do we need to account for latency here?
1522
/* write the trailer, if any */
1525
if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
1529
int ret = icv_av_write_frame_FFMPEG( oc, video_st, outbuf, outbuf_size, NULL);
1530
if( ret == OPENCV_NO_FRAMES_WRITTEN_CODE || ret < 0 )
1534
av_write_trailer(oc);
1537
if( img_convert_ctx )
1539
sws_freeContext(img_convert_ctx);
1540
img_convert_ctx = 0;
1544
#if LIBAVFORMAT_BUILD > 4628
1545
if( video_st->codec->pix_fmt != input_pix_fmt)
1547
if( video_st->codec.pix_fmt != input_pix_fmt)
1550
if(picture->data[0])
1551
free(picture->data[0]);
1552
picture->data[0] = 0;
1557
av_free(input_picture);
1560
#if LIBAVFORMAT_BUILD > 4628
1561
avcodec_close(video_st->codec);
1563
avcodec_close(&(video_st->codec));
1568
if (!(fmt->flags & AVFMT_NOFILE))
1570
/* close the output file */
1572
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
1573
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
1576
url_fclose(&oc->pb);
1584
/* free the stream */
1585
avformat_free_context(oc);
1587
if( temp_image.data )
1589
free(temp_image.data);
1590
temp_image.data = 0;
1596
#define CV_PRINTABLE_CHAR(ch) ((ch) < 32 ? '?' : (ch))
1597
#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)
1599
static inline bool cv_ff_codec_tag_match(const AVCodecTag *tags, CV_CODEC_ID id, unsigned int tag)
1601
while (tags->id != AV_CODEC_ID_NONE)
1603
if (tags->id == id && tags->tag == tag)
1609
static inline bool cv_ff_codec_tag_list_match(const AVCodecTag *const *tags, CV_CODEC_ID id, unsigned int tag)
1612
for (i = 0; tags && tags[i]; i++) {
1613
bool res = cv_ff_codec_tag_match(tags[i], id, tag);
1620
/// Create a video writer object that uses FFMPEG
1621
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
1622
double fps, int width, int height, bool is_color )
1624
CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_NONE);
1625
int err, codec_pix_fmt;
1626
double bitrate_scale = 1;
1636
// we allow frames of odd width or height, but in this case we truncate
1637
// the rightmost column/the bottom row. Probably, this should be handled more elegantly,
1638
// but some internal functions inside FFMPEG swscale require even width/height.
1641
if( width <= 0 || height <= 0 )
1644
/* auto detect the output format from the name and fourcc code. */
1646
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1647
fmt = av_guess_format(NULL, filename, NULL);
1649
fmt = guess_format(NULL, filename, NULL);
1655
/* determine optimal pixel format */
1657
input_pix_fmt = AV_PIX_FMT_BGR24;
1660
input_pix_fmt = AV_PIX_FMT_GRAY8;
1663
/* Lookup codec_id for given fourcc */
1664
#if LIBAVCODEC_VERSION_INT<((51<<16)+(49<<8)+0)
1665
if( (codec_id = codec_get_bmp_id( fourcc )) == CV_CODEC(CODEC_ID_NONE) )
1668
if( (codec_id = av_codec_get_id(fmt->codec_tag, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1670
const struct AVCodecTag * fallback_tags[] = {
1671
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(54, 1, 0)
1673
// 2012-01-31 - dd6d3b0 - lavf 54.01.0
1674
// Add avformat_get_riff_video_tags() and avformat_get_riff_audio_tags().
1675
avformat_get_riff_video_tags(),
1677
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 25, 100) && defined LIBAVFORMAT_VERSION_MICRO && LIBAVFORMAT_VERSION_MICRO >= 100
1678
// APIchanges: ffmpeg only
1679
// 2014-01-19 - 1a193c4 - lavf 55.25.100 - avformat.h
1680
// Add avformat_get_mov_video_tags() and avformat_get_mov_audio_tags().
1681
avformat_get_mov_video_tags(),
1683
codec_bmp_tags, // fallback for avformat < 54.1
1685
if( (codec_id = av_codec_get_id(fallback_tags, fourcc)) == CV_CODEC(CODEC_ID_NONE) )
1688
fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not found (format '%s / %s')'\n",
1689
fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
1690
fmt->name, fmt->long_name);
1695
if (cv_ff_codec_tag_list_match(fmt->codec_tag, codec_id, fourcc) == false)
1698
fprintf(stderr, "OpenCV: FFMPEG: tag 0x%08x/'%c%c%c%c' is not supported with codec id %d and format '%s / %s'\n",
1699
fourcc, CV_TAG_TO_PRINTABLE_CHAR4(fourcc),
1700
codec_id, fmt->name, fmt->long_name);
1702
if( (supported_tag = av_codec_get_tag(fmt->codec_tag, codec_id)) != 0 )
1704
fprintf(stderr, "OpenCV: FFMPEG: fallback to use tag 0x%08x/'%c%c%c%c'\n",
1705
supported_tag, CV_TAG_TO_PRINTABLE_CHAR4(supported_tag));
1706
fourcc = supported_tag;
1711
// alloc memory for context
1712
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1713
oc = avformat_alloc_context();
1715
oc = av_alloc_format_context();
1721
snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
1723
/* set some options */
1724
oc->max_delay = (int)(0.7*AV_TIME_BASE); /* This reduces buffer underrun warnings with MPEG */
1726
// set a few optimal pixel formats for lossless codecs of interest..
1728
#if LIBAVCODEC_VERSION_INT>((50<<16)+(1<<8)+0)
1729
case CV_CODEC(CODEC_ID_JPEGLS):
1730
// BGR24 or GRAY8 depending on is_color...
1731
codec_pix_fmt = input_pix_fmt;
1734
case CV_CODEC(CODEC_ID_HUFFYUV):
1735
codec_pix_fmt = AV_PIX_FMT_YUV422P;
1737
case CV_CODEC(CODEC_ID_MJPEG):
1738
case CV_CODEC(CODEC_ID_LJPEG):
1739
codec_pix_fmt = AV_PIX_FMT_YUVJ420P;
1742
case CV_CODEC(CODEC_ID_RAWVIDEO):
1743
codec_pix_fmt = input_pix_fmt == AV_PIX_FMT_GRAY8 ||
1744
input_pix_fmt == AV_PIX_FMT_GRAY16LE ||
1745
input_pix_fmt == AV_PIX_FMT_GRAY16BE ? input_pix_fmt : AV_PIX_FMT_YUV420P;
1748
// good for lossy formats, MPEG, etc.
1749
codec_pix_fmt = AV_PIX_FMT_YUV420P;
1753
double bitrate = MIN(bitrate_scale*fps*width*height, (double)INT_MAX/2);
1755
// TODO -- safe to ignore output audio stream?
1756
video_st = icv_add_video_stream_FFMPEG(oc, codec_id,
1757
width, height, (int)(bitrate + 0.5),
1758
fps, codec_pix_fmt);
1760
/* set the output parameters (must be done even if no
1762
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1763
if (av_set_parameters(oc, NULL) < 0) {
1769
#if FF_API_DUMP_FORMAT
1770
dump_format(oc, 0, filename, 1);
1772
av_dump_format(oc, 0, filename, 1);
1776
/* now that all the parameters are set, we can open the audio and
1777
video codecs and allocate the necessary encode buffers */
1785
#if LIBAVFORMAT_BUILD > 4628
1786
c = (video_st->codec);
1788
c = &(video_st->codec);
1791
c->codec_tag = fourcc;
1792
/* find the video encoder */
1793
codec = avcodec_find_encoder(c->codec_id);
1795
fprintf(stderr, "Could not find encoder for codec id %d: %s\n", c->codec_id, icvFFMPEGErrStr(
1796
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
1797
AVERROR_ENCODER_NOT_FOUND
1805
int64_t lbit_rate = (int64_t)c->bit_rate;
1806
lbit_rate += (bitrate / 2);
1807
lbit_rate = std::min(lbit_rate, (int64_t)INT_MAX);
1808
c->bit_rate_tolerance = (int)lbit_rate;
1809
c->bit_rate = (int)lbit_rate;
1811
/* open the codec */
1813
#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1814
avcodec_open2(c, codec, NULL)
1816
avcodec_open(c, codec)
1819
fprintf(stderr, "Could not open codec '%s': %s\n", codec->name, icvFFMPEGErrStr(err));
1825
if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
1826
/* allocate output buffer */
1827
/* assume we will never get codec output with more than 4 bytes per pixel... */
1828
outbuf_size = width*height*4;
1829
outbuf = (uint8_t *) av_malloc(outbuf_size);
1832
bool need_color_convert;
1833
need_color_convert = (c->pix_fmt != input_pix_fmt);
1835
/* allocate the encoded raw picture */
1836
picture = icv_alloc_picture_FFMPEG(c->pix_fmt, c->width, c->height, need_color_convert);
1841
/* if the output format is not our input format, then a temporary
1842
picture of the input format is needed too. It is then converted
1843
to the required output format */
1844
input_picture = NULL;
1845
if ( need_color_convert ) {
1846
input_picture = icv_alloc_picture_FFMPEG(input_pix_fmt, c->width, c->height, false);
1847
if (!input_picture) {
1852
/* open the output file, if needed */
1853
if (!(fmt->flags & AVFMT_NOFILE)) {
1854
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
1855
if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
1857
if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
1864
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
1865
/* write the stream header, if any */
1866
err=avformat_write_header(oc, NULL);
1868
err=av_write_header( oc );
1877
frame_width = width;
1878
frame_height = height;
1887
CvCapture_FFMPEG* cvCreateFileCapture_FFMPEG( const char* filename )
1889
CvCapture_FFMPEG* capture = (CvCapture_FFMPEG*)malloc(sizeof(*capture));
1891
if( capture->open( filename ))
1900
void cvReleaseCapture_FFMPEG(CvCapture_FFMPEG** capture)
1902
if( capture && *capture )
1904
(*capture)->close();
1910
int cvSetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id, double value)
1912
return capture->setProperty(prop_id, value);
1915
double cvGetCaptureProperty_FFMPEG(CvCapture_FFMPEG* capture, int prop_id)
1917
return capture->getProperty(prop_id);
1920
int cvGrabFrame_FFMPEG(CvCapture_FFMPEG* capture)
1922
return capture->grabFrame();
1925
int cvRetrieveFrame_FFMPEG(CvCapture_FFMPEG* capture, unsigned char** data, int* step, int* width, int* height, int* cn)
1927
return capture->retrieveFrame(0, data, step, width, height, cn);
1930
CvVideoWriter_FFMPEG* cvCreateVideoWriter_FFMPEG( const char* filename, int fourcc, double fps,
1931
int width, int height, int isColor )
1933
CvVideoWriter_FFMPEG* writer = (CvVideoWriter_FFMPEG*)malloc(sizeof(*writer));
1935
if( writer->open( filename, fourcc, fps, width, height, isColor != 0 ))
1942
void cvReleaseVideoWriter_FFMPEG( CvVideoWriter_FFMPEG** writer )
1944
if( writer && *writer )
1953
int cvWriteFrame_FFMPEG( CvVideoWriter_FFMPEG* writer,
1954
const unsigned char* data, int step,
1955
int width, int height, int cn, int origin)
1957
return writer->writeFrame(data, step, width, height, cn, origin);
1966
struct OutputMediaStream_FFMPEG
1968
bool open(const char* fileName, int width, int height, double fps);
1971
void write(unsigned char* data, int size, int keyFrame);
1973
// add a video output stream to the container
1974
static AVStream* addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, AVPixelFormat pixel_format);
1976
AVOutputFormat* fmt_;
1977
AVFormatContext* oc_;
1978
AVStream* video_st_;
1981
void OutputMediaStream_FFMPEG::close()
1983
// no more frame to compress. The codec has a latency of a few
1984
// frames if using B frames, so we get the last frames by
1985
// passing the same picture again
1987
// TODO -- do we need to account for latency here?
1991
// write the trailer, if any
1992
av_write_trailer(oc_);
1995
for (unsigned int i = 0; i < oc_->nb_streams; ++i)
1997
av_freep(&oc_->streams[i]->codec);
1998
av_freep(&oc_->streams[i]);
2001
if (!(fmt_->flags & AVFMT_NOFILE) && oc_->pb)
2003
// close the output file
2005
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
2006
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
2007
url_fclose(oc_->pb);
2009
url_fclose(&oc_->pb);
2012
avio_close(oc_->pb);
2021
AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext *oc, CV_CODEC_ID codec_id, int w, int h, int bitrate, double fps, AVPixelFormat pixel_format)
2023
AVCodec* codec = avcodec_find_encoder(codec_id);
2026
fprintf(stderr, "Could not find encoder for codec id %d\n", codec_id);
2030
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
2031
AVStream* st = avformat_new_stream(oc, 0);
2033
AVStream* st = av_new_stream(oc, 0);
2038
#if LIBAVFORMAT_BUILD > 4628
2039
AVCodecContext* c = st->codec;
2041
AVCodecContext* c = &(st->codec);
2044
c->codec_id = codec_id;
2045
c->codec_type = AVMEDIA_TYPE_VIDEO;
2047
// put sample parameters
2048
unsigned long long lbit_rate = static_cast<unsigned long long>(bitrate);
2049
lbit_rate += (bitrate / 4);
2050
lbit_rate = std::min(lbit_rate, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
2051
c->bit_rate = bitrate;
2054
// http://ffmpeg-users.933282.n4.nabble.com/warning-clipping-1-dct-coefficients-to-127-127-td934297.html
2057
// resolution must be a multiple of two
2061
// time base: this is the fundamental unit of time (in seconds) in terms
2062
// of which frame timestamps are represented. for fixed-fps content,
2063
// timebase should be 1/framerate and timestamp increments should be
2066
int frame_rate = static_cast<int>(fps+0.5);
2067
int frame_rate_base = 1;
2068
while (fabs(static_cast<double>(frame_rate)/frame_rate_base) - fps > 0.001)
2070
frame_rate_base *= 10;
2071
frame_rate = static_cast<int>(fps*frame_rate_base + 0.5);
2073
c->time_base.den = frame_rate;
2074
c->time_base.num = frame_rate_base;
2076
#if LIBAVFORMAT_BUILD > 4752
2077
// adjust time base for supported framerates
2078
if (codec && codec->supported_framerates)
2080
AVRational req = {frame_rate, frame_rate_base};
2081
const AVRational* best = NULL;
2082
AVRational best_error = {INT_MAX, 1};
2084
for (const AVRational* p = codec->supported_framerates; p->den!=0; ++p)
2086
AVRational error = av_sub_q(req, *p);
2091
if (av_cmp_q(error, best_error) < 0)
2098
c->time_base.den= best->num;
2099
c->time_base.num= best->den;
2103
c->gop_size = 12; // emit one intra frame every twelve frames at most
2104
c->pix_fmt = pixel_format;
2106
if (c->codec_id == CV_CODEC(CODEC_ID_MPEG2VIDEO))
2107
c->max_b_frames = 2;
2109
if (c->codec_id == CV_CODEC(CODEC_ID_MPEG1VIDEO) || c->codec_id == CV_CODEC(CODEC_ID_MSMPEG4V3))
2111
// needed to avoid using macroblocks in which some coeffs overflow
2112
// this doesnt happen with normal video, it just happens here as the
2113
// motion of the chroma plane doesnt match the luma plane
2115
// avoid FFMPEG warning 'clipping 1 dct coefficients...'
2120
#if LIBAVCODEC_VERSION_INT > 0x000409
2121
// some formats want stream headers to be seperate
2122
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
2124
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
2131
bool OutputMediaStream_FFMPEG::open(const char* fileName, int width, int height, double fps)
2137
// auto detect the output format from the name and fourcc code
2138
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2139
fmt_ = av_guess_format(NULL, fileName, NULL);
2141
fmt_ = guess_format(NULL, fileName, NULL);
2146
CV_CODEC_ID codec_id = CV_CODEC(CODEC_ID_H264);
2148
// alloc memory for context
2149
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
2150
oc_ = avformat_alloc_context();
2152
oc_ = av_alloc_format_context();
2158
oc_->oformat = fmt_;
2159
snprintf(oc_->filename, sizeof(oc_->filename), "%s", fileName);
2161
oc_->max_delay = (int)(0.7 * AV_TIME_BASE); // This reduces buffer underrun warnings with MPEG
2163
// set a few optimal pixel formats for lossless codecs of interest..
2164
AVPixelFormat codec_pix_fmt = AV_PIX_FMT_YUV420P;
2165
int bitrate_scale = 64;
2167
// TODO -- safe to ignore output audio stream?
2168
video_st_ = addVideoStream(oc_, codec_id, width, height, width * height * bitrate_scale, fps, codec_pix_fmt);
2172
// set the output parameters (must be done even if no parameters)
2173
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2174
if (av_set_parameters(oc_, NULL) < 0)
2178
// now that all the parameters are set, we can open the audio and
2179
// video codecs and allocate the necessary encode buffers
2181
#if LIBAVFORMAT_BUILD > 4628
2182
AVCodecContext* c = (video_st_->codec);
2184
AVCodecContext* c = &(video_st_->codec);
2187
c->codec_tag = MKTAG('H', '2', '6', '4');
2188
c->bit_rate_tolerance = c->bit_rate;
2190
// open the output file, if needed
2191
if (!(fmt_->flags & AVFMT_NOFILE))
2193
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2194
int err = url_fopen(&oc_->pb, fileName, URL_WRONLY);
2196
int err = avio_open(&oc_->pb, fileName, AVIO_FLAG_WRITE);
2203
// write the stream header, if any
2204
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
2205
av_write_header(oc_);
2207
avformat_write_header(oc_, NULL);
2213
void OutputMediaStream_FFMPEG::write(unsigned char* data, int size, int keyFrame)
2215
// if zero size, it means the image was buffered
2219
av_init_packet(&pkt);
2222
pkt.flags |= PKT_FLAG_KEY;
2224
pkt.stream_index = video_st_->index;
2228
// write the compressed frame in the media file
2229
av_write_frame(oc_, &pkt);
2233
struct OutputMediaStream_FFMPEG* create_OutputMediaStream_FFMPEG(const char* fileName, int width, int height, double fps)
2235
OutputMediaStream_FFMPEG* stream = (OutputMediaStream_FFMPEG*) malloc(sizeof(OutputMediaStream_FFMPEG));
2237
if (stream->open(fileName, width, height, fps))
2246
void release_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream)
2252
void write_OutputMediaStream_FFMPEG(struct OutputMediaStream_FFMPEG* stream, unsigned char* data, int size, int keyFrame)
2254
stream->write(data, size, keyFrame);
2263
VideoCodec_MPEG1 = 0,
2269
VideoCodec_H264_SVC,
2270
VideoCodec_H264_MVC,
2273
VideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2274
VideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2275
VideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2276
VideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2277
VideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')) // UYVY (4:2:2)
2282
VideoChromaFormat_Monochrome = 0,
2283
VideoChromaFormat_YUV420,
2284
VideoChromaFormat_YUV422,
2285
VideoChromaFormat_YUV444
2288
struct InputMediaStream_FFMPEG
2291
bool open(const char* fileName, int* codec, int* chroma_format, int* width, int* height);
2294
bool read(unsigned char** data, int* size, int* endOfFile);
2297
InputMediaStream_FFMPEG(const InputMediaStream_FFMPEG&);
2298
InputMediaStream_FFMPEG& operator =(const InputMediaStream_FFMPEG&);
2300
AVFormatContext* ctx_;
2301
int video_stream_id_;
2305
bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2310
video_stream_id_ = -1;
2311
memset(&pkt_, 0, sizeof(AVPacket));
2313
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
2314
avformat_network_init();
2317
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2318
err = avformat_open_input(&ctx_, fileName, 0, 0);
2320
err = av_open_input_file(&ctx_, fileName, 0, 0, 0);
2325
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 6, 0)
2326
err = avformat_find_stream_info(ctx_, 0);
2328
err = av_find_stream_info(ctx_);
2333
for (unsigned int i = 0; i < ctx_->nb_streams; ++i)
2335
#if LIBAVFORMAT_BUILD > 4628
2336
AVCodecContext *enc = ctx_->streams[i]->codec;
2338
AVCodecContext *enc = &ctx_->streams[i]->codec;
2341
if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
2343
video_stream_id_ = static_cast<int>(i);
2345
switch (enc->codec_id)
2347
case CV_CODEC(CODEC_ID_MPEG1VIDEO):
2348
*codec = ::VideoCodec_MPEG1;
2351
case CV_CODEC(CODEC_ID_MPEG2VIDEO):
2352
*codec = ::VideoCodec_MPEG2;
2355
case CV_CODEC(CODEC_ID_MPEG4):
2356
*codec = ::VideoCodec_MPEG4;
2359
case CV_CODEC(CODEC_ID_VC1):
2360
*codec = ::VideoCodec_VC1;
2363
case CV_CODEC(CODEC_ID_H264):
2364
*codec = ::VideoCodec_H264;
2371
switch (enc->pix_fmt)
2373
case AV_PIX_FMT_YUV420P:
2374
*chroma_format = ::VideoChromaFormat_YUV420;
2377
case AV_PIX_FMT_YUV422P:
2378
*chroma_format = ::VideoChromaFormat_YUV422;
2381
case AV_PIX_FMT_YUV444P:
2382
*chroma_format = ::VideoChromaFormat_YUV444;
2389
*width = enc->coded_width;
2390
*height = enc->coded_height;
2396
if (video_stream_id_ < 0)
2399
av_init_packet(&pkt_);
2404
void InputMediaStream_FFMPEG::close()
2408
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
2409
avformat_close_input(&ctx_);
2411
av_close_input_file(ctx_);
2415
// free last packet if exist
2417
av_free_packet(&pkt_);
2420
bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile)
2422
// free last packet if exist
2424
av_free_packet(&pkt_);
2426
// get the next frame
2429
int ret = av_read_frame(ctx_, &pkt_);
2431
if (ret == AVERROR(EAGAIN))
2436
if (ret == (int)AVERROR_EOF)
2441
if (pkt_.stream_index != video_stream_id_)
2443
av_free_packet(&pkt_);
2457
InputMediaStream_FFMPEG* create_InputMediaStream_FFMPEG(const char* fileName, int* codec, int* chroma_format, int* width, int* height)
2459
InputMediaStream_FFMPEG* stream = (InputMediaStream_FFMPEG*) malloc(sizeof(InputMediaStream_FFMPEG));
2461
if (stream && stream->open(fileName, codec, chroma_format, width, height))
2470
void release_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream)
2476
int read_InputMediaStream_FFMPEG(InputMediaStream_FFMPEG* stream, unsigned char** data, int* size, int* endOfFile)
2478
return stream->read(data, size, endOfFile);