Commit 5e2df0d6 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

some more fixes in ffmpeg wrapper; now it handles big videos better.

parent 24f8c21b
...@@ -255,7 +255,8 @@ void CvCapture_FFMPEG::init() ...@@ -255,7 +255,8 @@ void CvCapture_FFMPEG::init()
memset( &rgb_picture, 0, sizeof(rgb_picture) ); memset( &rgb_picture, 0, sizeof(rgb_picture) );
memset( &frame, 0, sizeof(frame) ); memset( &frame, 0, sizeof(frame) );
filename = 0; filename = 0;
packet.data = NULL; memset(&packet, 0, sizeof(packet));
av_init_packet(&packet);
img_convert_ctx = 0; img_convert_ctx = 0;
avcodec = 0; avcodec = 0;
...@@ -321,22 +322,33 @@ void CvCapture_FFMPEG::close() ...@@ -321,22 +322,33 @@ void CvCapture_FFMPEG::close()
#define AVSEEK_FLAG_ANY 1 #define AVSEEK_FLAG_ANY 1
#endif #endif
static void icvInitFFMPEG_internal()
{
static volatile bool initialized = false;
if( !initialized )
{
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
avformat_network_init();
#endif
/* register all codecs, demux and protocols */
av_register_all();
av_log_set_level(AV_LOG_ERROR);
initialized = true;
}
}
bool CvCapture_FFMPEG::open( const char* _filename ) bool CvCapture_FFMPEG::open( const char* _filename )
{ {
icvInitFFMPEG_internal();
unsigned i; unsigned i;
bool valid = false; bool valid = false;
close(); close();
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0)
avformat_network_init();
#endif
/* register all codecs, demux and protocols */
av_register_all();
av_log_set_level(AV_LOG_ERROR);
int err = avformat_open_input(&ic, _filename, NULL, NULL); int err = avformat_open_input(&ic, _filename, NULL, NULL);
if (err < 0) { if (err < 0) {
CV_WARN("Error opening file"); CV_WARN("Error opening file");
...@@ -416,15 +428,11 @@ bool CvCapture_FFMPEG::grabFrame() ...@@ -416,15 +428,11 @@ bool CvCapture_FFMPEG::grabFrame()
int got_picture; int got_picture;
int count_errs = 0; int count_errs = 0;
const int max_number_of_attempts = 64; const int max_number_of_attempts = 1 << 16;
if( !ic || !video_st ) return false; if( !ic || !video_st ) return false;
if (packet.data != NULL) av_free_packet (&packet);
{
av_free_packet (&packet);
packet.data = NULL;
}
picture_pts = AV_NOPTS_VALUE_; picture_pts = AV_NOPTS_VALUE_;
...@@ -439,10 +447,10 @@ bool CvCapture_FFMPEG::grabFrame() ...@@ -439,10 +447,10 @@ bool CvCapture_FFMPEG::grabFrame()
if( packet.stream_index != video_stream ) if( packet.stream_index != video_stream )
{ {
av_free_packet (&packet); av_free_packet (&packet);
packet.data = NULL;
count_errs++; count_errs++;
if (count_errs > max_number_of_attempts) if (count_errs > max_number_of_attempts)
break; break;
continue;
} }
// Decode video frame // Decode video frame
...@@ -464,11 +472,7 @@ bool CvCapture_FFMPEG::grabFrame() ...@@ -464,11 +472,7 @@ bool CvCapture_FFMPEG::grabFrame()
break; break;
} }
/*if (packet.data) av_free_packet (&packet);
{
av_free_packet (&packet);
packet.data = NULL;
}*/
} }
if( valid && first_frame_number < 0 ) if( valid && first_frame_number < 0 )
...@@ -1244,6 +1248,8 @@ void CvVideoWriter_FFMPEG::close() ...@@ -1244,6 +1248,8 @@ void CvVideoWriter_FFMPEG::close()
bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc, bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
double fps, int width, int height, bool is_color ) double fps, int width, int height, bool is_color )
{ {
icvInitFFMPEG_internal();
CodecID codec_id = CODEC_ID_NONE; CodecID codec_id = CODEC_ID_NONE;
int err, codec_pix_fmt; int err, codec_pix_fmt;
double bitrate_scale = 1; double bitrate_scale = 1;
...@@ -1264,10 +1270,6 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc, ...@@ -1264,10 +1270,6 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc,
if( width <= 0 || height <= 0 ) if( width <= 0 || height <= 0 )
return false; return false;
// tell FFMPEG to register codecs
av_register_all();
av_log_set_level(AV_LOG_ERROR);
/* auto detect the output format from the name and fourcc code. */ /* auto detect the output format from the name and fourcc code. */
#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0) #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment