snprintf( key, sizeof(key), "meta.media.%d.codec.height", i );
mlt_properties_set_int( meta_media, key, codec_context->height );
snprintf( key, sizeof(key), "meta.media.%d.codec.frame_rate", i );
- AVRational frame_rate = av_inv_q( codec_context->time_base );
- frame_rate.den *= codec_context->ticks_per_frame;
+ AVRational frame_rate = { codec_context->time_base.den, codec_context->time_base.num * codec_context->ticks_per_frame };
mlt_properties_set_double( meta_media, key, av_q2d( frame_rate ) );
snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(3<<8)+0)
return error;
}
-static void reopen_video( producer_avformat self, mlt_producer producer )
+static void prepare_reopen( producer_avformat self )
{
- mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
- mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
+ mlt_service_lock( MLT_PRODUCER_SERVICE( self->parent ) );
pthread_mutex_lock( &self->audio_mutex );
pthread_mutex_lock( &self->open_mutex );
+ int i;
+ for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
+ {
+ mlt_pool_release( self->audio_buffer[i] );
+ self->audio_buffer[i] = NULL;
+ av_free( self->decode_buffer[i] );
+ self->decode_buffer[i] = NULL;
+ if ( self->audio_codec[i] )
+ avcodec_close( self->audio_codec[i] );
+ self->audio_codec[i] = NULL;
+ }
if ( self->video_codec )
avcodec_close( self->video_codec );
self->video_codec = NULL;
+
#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
- if ( self->dummy_context )
- avformat_close_input( &self->dummy_context );
+ if ( self->seekable && self->audio_format )
+ avformat_close_input( &self->audio_format );
if ( self->video_format )
avformat_close_input( &self->video_format );
#else
- if ( self->dummy_context )
- av_close_input_file( self->dummy_context );
+ if ( self->seekable && self->audio_format )
+ av_close_input_file( self->audio_format );
if ( self->video_format )
av_close_input_file( self->video_format );
#endif
- self->dummy_context = NULL;
+ self->audio_format = NULL;
self->video_format = NULL;
pthread_mutex_unlock( &self->open_mutex );
- int audio_index = self->audio_index;
- int video_index = self->video_index;
-
- producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
- mlt_properties_get( properties, "resource" ), 0 );
-
- self->audio_index = audio_index;
- if ( self->video_format && video_index > -1 )
+ // Cleanup the packet queues
+ AVPacket *pkt;
+ if ( self->apackets )
{
- self->video_index = video_index;
- video_codec_init( self, video_index, properties );
+ while ( ( pkt = mlt_deque_pop_back( self->apackets ) ) )
+ {
+ av_free_packet( pkt );
+ free( pkt );
+ }
+ mlt_deque_close( self->apackets );
+ self->apackets = NULL;
+ }
+ if ( self->vpackets )
+ {
+ while ( ( pkt = mlt_deque_pop_back( self->vpackets ) ) )
+ {
+ av_free_packet( pkt );
+ free( pkt );
+ }
+ mlt_deque_close( self->vpackets );
+ self->vpackets = NULL;
}
-
pthread_mutex_unlock( &self->audio_mutex );
- mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
+ mlt_service_unlock( MLT_PRODUCER_SERVICE( self->parent ) );
}
static int64_t best_pts( producer_avformat self, int64_t pts, int64_t dts )
timestamp -= 2 / av_q2d( self->video_time_base );
if ( timestamp < 0 )
timestamp = 0;
- mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %"PRId64" position %d expected %d last_pos %"PRId64"\n",
+ mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %"PRId64" position " MLT_POSITION_FMT " expected "MLT_POSITION_FMT" last_pos %"PRId64"\n",
timestamp, position, self->video_expected, self->last_position );
// Seek to the timestamp
- // NOTE: reopen_video is disabled at this time because it is causing trouble with A/V sync.
- if ( 1 || req_position > 0 || self->last_position <= 0 )
- {
- codec_context->skip_loop_filter = AVDISCARD_NONREF;
- av_seek_frame( context, self->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
+ codec_context->skip_loop_filter = AVDISCARD_NONREF;
+ av_seek_frame( context, self->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
- // flush any pictures still in decode buffer
- avcodec_flush_buffers( codec_context );
- }
- else
- {
- // Re-open video stream when rewinding to beginning from somewhere else.
- // This is rather ugly, and I prefer not to do it this way, but ffmpeg is
- // not reliably seeking to the first frame across formats.
- reopen_video( self, producer );
- }
+ // flush any pictures still in decode buffer
+ avcodec_flush_buffers( codec_context );
// Remove the cached info relating to the previous position
self->current_position = POSITION_INVALID;
pthread_mutex_lock( &self->video_mutex );
+ uint8_t *alpha = NULL;
+ int got_picture = 0;
+ int image_size = 0;
+
// Fetch the video format context
AVFormatContext *context = self->video_format;
+ if ( !context )
+ goto exit_get_image;
// Get the video stream
AVStream *stream = context->streams[ self->video_index ];
// Get codec context
AVCodecContext *codec_context = stream->codec;
- uint8_t *alpha = NULL;
- int got_picture = 0;
- int image_size = 0;
-
// Get the image cache
if ( ! self->image_cache )
{
mlt_deque_push_back( self->apackets, tmp );
}
}
+ else if ( ret < 0 )
+ {
+ mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "av_read_frame returned error %d inside get_image\n", ret );
+ if ( !self->seekable && mlt_properties_get_int( properties, "reconnect" ) )
+ {
+ // Try to reconnect to live sources by closing context and codecs,
+ // and letting next call to get_frame() reopen.
+ prepare_reopen( self );
+ pthread_mutex_unlock( &self->packets_mutex );
+ goto exit_get_image;
+ }
+ if ( !self->seekable && mlt_properties_get_int( properties, "exit_on_disconnect" ) )
+ {
+ mlt_log_fatal( MLT_PRODUCER_SERVICE(producer), "Exiting with error due to disconnected source.\n" );
+ exit( EXIT_FAILURE );
+ }
+ }
}
pthread_mutex_unlock( &self->packets_mutex );
{
if ( ++decode_errors <= 10 )
ret = 0;
+ else
+ mlt_log_warning( MLT_PRODUCER_SERVICE(producer), "video decoding error %d\n", ret );
}
else
{
if ( isnan( fps ) || isinf( fps ) || fps < 1.0 )
{
// Get the frame rate from the codec.
- frame_rate = av_inv_q( self->video_codec->time_base );
- frame_rate.den *= self->video_codec->ticks_per_frame;
+ frame_rate.num = self->video_codec->time_base.den;
+ frame_rate.den = self->video_codec->time_base.num * self->video_codec->ticks_per_frame;
fps = av_q2d( frame_rate );
}
if ( isnan( fps ) || isinf( fps ) || fps < 1.0 )
if ( mlt_properties_get( properties, "force_fps" ) )
{
AVRational force_fps = av_d2q( mlt_properties_get_double( properties, "force_fps" ), 1024 );
- self->video_time_base.num *= frame_rate.num * force_fps.den;
- self->video_time_base.den *= frame_rate.den * force_fps.num;
+ self->video_time_base = av_mul_q( stream->time_base, av_div_q( frame_rate, force_fps ) );
frame_rate = force_fps;
}
mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
// Get the video_index
int index = mlt_properties_get_int( properties, "video_index" );
+ int unlock_needed = 0;
+
// Reopen the file if necessary
if ( !context && index > -1 )
{
+ unlock_needed = 1;
+ pthread_mutex_lock( &self->video_mutex );
producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
- mlt_properties_get( properties, "resource" ), 1 );
+ mlt_properties_get( properties, "resource" ), 0 );
context = self->video_format;
}
// If something failed, use test card image
mlt_properties_set_int( frame_properties, "test_image", 1 );
}
+ if ( unlock_needed )
+ pthread_mutex_unlock( &self->video_mutex );
}
static int seek_audio( producer_avformat self, mlt_position position, double timecode )
#endif
}
-static void planar_to_interleaved( uint8_t *dest, uint8_t *src, int samples, int channels, int bytes_per_sample )
+#if LIBAVCODEC_VERSION_MAJOR >= 55
+static void planar_to_interleaved( uint8_t *dest, AVFrame *src, int samples, int channels, int bytes_per_sample )
{
int s, c;
for ( s = 0; s < samples; s++ )
{
for ( c = 0; c < channels; c++ )
{
- memcpy( dest, src + ( c * samples + s ) * bytes_per_sample, bytes_per_sample );
+ memcpy( dest, &src->data[c][s * bytes_per_sample], bytes_per_sample );
dest += bytes_per_sample;
}
}
}
-
-static void planar_to_interleaved2( uint8_t *dest, AVFrame *src, int samples, int channels, int bytes_per_sample )
+#else
+static void planar_to_interleaved( uint8_t *dest, uint8_t *src, int samples, int channels, int bytes_per_sample )
{
int s, c;
for ( s = 0; s < samples; s++ )
{
for ( c = 0; c < channels; c++ )
{
- memcpy( dest, &src->data[c][s * bytes_per_sample], bytes_per_sample );
+ memcpy( dest, src + ( c * samples + s ) * bytes_per_sample, bytes_per_sample );
dest += bytes_per_sample;
}
}
}
+#endif
static int decode_audio( producer_avformat self, int *ignore, AVPacket pkt, int channels, int samples, double timecode, double fps )
{
case AV_SAMPLE_FMT_S32P:
case AV_SAMPLE_FMT_FLTP:
#if LIBAVCODEC_VERSION_MAJOR >= 55
- planar_to_interleaved2( dest, self->audio_frame, convert_samples, codec_context->channels, sizeof_sample );
+ planar_to_interleaved( dest, self->audio_frame, convert_samples, codec_context->channels, sizeof_sample );
#else
planar_to_interleaved( dest, decode_buffer, convert_samples, codec_context->channels, sizeof_sample );
#endif
// Fetch the audio_format
AVFormatContext *context = self->audio_format;
+ if ( !context )
+ goto exit_get_audio;
int sizeof_sample = sizeof( int16_t );
mlt_deque_push_back( self->vpackets, tmp );
}
}
+ else if ( ret < 0 )
+ {
+ mlt_producer producer = self->parent;
+ mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
+ mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "av_read_frame returned error %d inside get_audio\n", ret );
+ if ( !self->seekable && mlt_properties_get_int( properties, "reconnect" ) )
+ {
+ // Try to reconnect to live sources by closing context and codecs,
+ // and letting next call to get_frame() reopen.
+ prepare_reopen( self );
+ pthread_mutex_unlock( &self->packets_mutex );
+ goto exit_get_audio;
+ }
+ if ( !self->seekable && mlt_properties_get_int( properties, "exit_on_disconnect" ) )
+ {
+ mlt_log_fatal( MLT_PRODUCER_SERVICE(producer), "Exiting with error due to disconnected source.\n" );
+ exit( EXIT_FAILURE );
+ }
+ }
}
pthread_mutex_unlock( &self->packets_mutex );
}
else
{
+exit_get_audio:
// Get silence and don't touch the context
mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
}
audio_codec_init( self, index, properties );
}
}
- else if ( context && index > -1 && audio_codec_init( self, index, properties ) )
+ else if ( context && index > -1 && index < MAX_AUDIO_STREAMS &&
+ audio_codec_init( self, index, properties ) )
{
- // Set the frame properties
- if ( index < MAX_AUDIO_STREAMS )
- {
- mlt_properties_set_int( frame_properties, "audio_frequency", self->audio_codec[ index ]->sample_rate );
- mlt_properties_set_int( frame_properties, "audio_channels", self->audio_codec[ index ]->channels );
- }
+ mlt_properties_set_int( frame_properties, "audio_frequency", self->audio_codec[ index ]->sample_rate );
+ mlt_properties_set_int( frame_properties, "audio_channels", self->audio_codec[ index ]->channels );
}
if ( context && index > -1 )
{