X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmodules%2Favformat%2Fproducer_avformat.c;h=48150ed95ecd4854ee5eb80c6f1025d8ae332488;hb=ebf4bc55b05a201a6ecdd45ac4616797962d5863;hp=04b1ab6aa13bc7096a79448630982f5449c30b39;hpb=6fd6dd1b4e6a3c0dcee525084211d7751607a75e;p=mlt diff --git a/src/modules/avformat/producer_avformat.c b/src/modules/avformat/producer_avformat.c index 04b1ab6a..48150ed9 100644 --- a/src/modules/avformat/producer_avformat.c +++ b/src/modules/avformat/producer_avformat.c @@ -91,17 +91,17 @@ struct producer_avformat_s AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ]; AVCodecContext *video_codec; AVFrame *av_frame; + AVPacket pkt; ReSampleContext *audio_resample[ MAX_AUDIO_STREAMS ]; mlt_position audio_expected; mlt_position video_expected; int audio_index; int video_index; - int first_pts; + int64_t first_pts; int64_t last_position; int seekable; int64_t current_position; mlt_position nonseek_position; - int got_picture; int top_field_first; uint8_t *audio_buffer[ MAX_AUDIO_STREAMS ]; size_t audio_buffer_size[ MAX_AUDIO_STREAMS ]; @@ -113,9 +113,9 @@ struct producer_avformat_s int max_channel; int max_frequency; unsigned int invalid_pts_counter; + unsigned int invalid_dts_counter; double resample_factor; mlt_cache image_cache; - mlt_cache alpha_cache; int colorspace; pthread_mutex_t video_mutex; pthread_mutex_t audio_mutex; @@ -193,6 +193,16 @@ mlt_producer producer_avformat_init( mlt_profile profile, const char *service, c // Register our get_frame implementation producer->get_frame = producer_get_frame; + // init mutexes + pthread_mutex_init( &self->audio_mutex, NULL ); + pthread_mutex_init( &self->video_mutex, NULL ); + pthread_mutex_init( &self->packets_mutex, NULL ); + pthread_mutex_init( &self->open_mutex, NULL ); + + // init queues + self->apackets = mlt_deque_init(); + self->vpackets = mlt_deque_init(); + if ( strcmp( service, "avformat-novalidate" ) ) { // Open the file @@ -201,6 +211,7 @@ mlt_producer producer_avformat_init( mlt_profile profile, const char *service, c // Clean up mlt_producer_close( producer ); producer = NULL; + producer_avformat_close( self ); } else if ( self->seekable ) { @@ -366,6 +377,8 @@ static mlt_properties find_default_streams( producer_avformat self ) #endif break; case CODEC_TYPE_AUDIO: + if ( !codec_context->channels ) + break; // Use first audio stream if ( self->audio_index < 0 ) self->audio_index = i; @@ -789,8 +802,11 @@ static int get_basic_info( producer_avformat self, mlt_profile profile, const ch if ( ret >= 0 && pkt.stream_index == self->video_index && pkt.size > 0 ) { get_aspect_ratio( properties, format->streams[ self->video_index ], codec_context, &pkt ); + av_free_packet(&pkt); break; } + if ( ret >= 0 ) + av_free_packet(&pkt); } } else @@ -824,10 +840,6 @@ static int producer_open( producer_avformat self, mlt_profile profile, const cha // Lock the service if ( take_lock ) { - pthread_mutex_init( &self->audio_mutex, NULL ); - pthread_mutex_init( &self->video_mutex, NULL ); - pthread_mutex_init( &self->packets_mutex, NULL ); - pthread_mutex_init( &self->open_mutex, NULL ); pthread_mutex_lock( &self->audio_mutex ); pthread_mutex_lock( &self->video_mutex ); } @@ -892,7 +904,7 @@ static int producer_open( producer_avformat self, mlt_profile profile, const cha error = get_basic_info( self, profile, filename ); // Initialize position info - self->first_pts = -1; + self->first_pts = AV_NOPTS_VALUE; self->last_position = POSITION_INITIAL; if ( !self->audio_format ) @@ -944,11 +956,6 @@ static int producer_open( producer_avformat self, mlt_profile profile, const cha } if ( filename ) free( filename ); - if ( !error ) - { - self->apackets = mlt_deque_init(); - self->vpackets = mlt_deque_init(); - } if ( self->dummy_context ) { @@ -1015,8 +1022,19 @@ static void reopen_video( producer_avformat self, mlt_producer producer ) mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) ); } +static int64_t best_pts( producer_avformat self, int64_t pts, int64_t dts ) +{ + self->invalid_pts_counter += pts == AV_NOPTS_VALUE; + self->invalid_dts_counter += dts == AV_NOPTS_VALUE; + if ( ( self->invalid_pts_counter <= self->invalid_dts_counter + || dts == AV_NOPTS_VALUE ) && pts != AV_NOPTS_VALUE ) + return pts; + else + return dts; +} + static int seek_video( producer_avformat self, mlt_position position, - int64_t req_position, int must_decode, int use_new_seek, int *ignore ) + int64_t req_position, int preseek ) { mlt_producer producer = self->parent; int paused = 0; @@ -1038,75 +1056,61 @@ static int seek_video( producer_avformat self, mlt_position position, double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) / mlt_properties_get_double( properties, "meta.media.frame_rate_den" ); - if ( self->av_frame && position + 1 == self->video_expected ) - { - // We're paused - use last image - paused = 1; - } - else if ( !self->seekable && position > self->video_expected && ( position - self->video_expected ) < 250 ) + // find initial PTS + if ( self->last_position == POSITION_INITIAL ) { - // Fast forward - seeking is inefficient for small distances - just ignore following frames - *ignore = ( int )( ( position - self->video_expected ) / mlt_producer_get_fps( producer ) * source_fps ); - codec_context->skip_loop_filter = AVDISCARD_NONREF; - } - else if ( self->seekable && ( position < self->video_expected || position - self->video_expected >= 12 || self->last_position < 0 ) ) - { - if ( use_new_seek && self->last_position == POSITION_INITIAL ) - { - // find first key frame - int ret = 0; - int toscan = 100; - AVPacket pkt; + int ret = 0; + int toscan = 500; + AVPacket pkt; - while ( ret >= 0 && toscan-- > 0 ) + while ( ret >= 0 && toscan-- > 0 ) + { + ret = av_read_frame( context, &pkt ); + if ( ret >= 0 && pkt.stream_index == self->video_index && ( pkt.flags & PKT_FLAG_KEY ) ) { - ret = av_read_frame( context, &pkt ); - if ( ret >= 0 && ( pkt.flags & PKT_FLAG_KEY ) && pkt.stream_index == self->video_index ) - { - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "first_pts %"PRId64" dts %"PRId64" pts_dts_delta %d\n", pkt.pts, pkt.dts, (int)(pkt.pts - pkt.dts) ); - self->first_pts = pkt.pts; + mlt_log_debug( MLT_PRODUCER_SERVICE(producer), + "first_pts %"PRId64" dts %"PRId64" pts_dts_delta %d\n", + pkt.pts, pkt.dts, (int)(pkt.pts - pkt.dts) ); + self->first_pts = best_pts( self, pkt.pts, pkt.dts ); + if ( self->first_pts != AV_NOPTS_VALUE ) toscan = 0; - } - av_free_packet( &pkt ); } - // Rewind - av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD ); + av_free_packet( &pkt ); } + av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD ); + } + if ( self->av_frame && position + 1 == self->video_expected ) + { + // We're paused - use last image + paused = 1; + } + else if ( self->seekable && ( position < self->video_expected || position - self->video_expected >= 12 || self->last_position < 0 ) ) + { // Calculate the timestamp for the requested frame - int64_t timestamp; - if ( use_new_seek ) - { - timestamp = ( req_position - 0.1 / source_fps ) / - ( av_q2d( stream->time_base ) * source_fps ); - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pos %"PRId64" pts %"PRId64"\n", req_position, timestamp ); - if ( self->first_pts > 0 ) - timestamp += self->first_pts; - else if ( context->start_time != AV_NOPTS_VALUE ) - timestamp += context->start_time; - } - else - { - timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 ); - if ( context->start_time != AV_NOPTS_VALUE ) - timestamp += context->start_time; - } - if ( must_decode ) - timestamp -= AV_TIME_BASE; + int64_t timestamp = req_position / ( av_q2d( stream->time_base ) * source_fps ); + if ( req_position <= 0 ) + timestamp = 0; + else if ( self->first_pts != AV_NOPTS_VALUE ) + timestamp += self->first_pts; + else if ( context->start_time != AV_NOPTS_VALUE ) + timestamp += context->start_time; + if ( preseek && av_q2d( stream->time_base ) != 0 ) + timestamp -= 2 / av_q2d( stream->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", timestamp, position, self->video_expected, self->last_position ); // Seek to the timestamp - if ( use_new_seek ) + // 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 ); - } - else if ( req_position > 0 || self->last_position <= 0 ) - { - av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ); + + // flush any pictures still in decode buffer + avcodec_flush_buffers( codec_context ); } else { @@ -1120,12 +1124,6 @@ static int seek_video( producer_avformat self, mlt_position position, self->current_position = POSITION_INVALID; self->last_position = POSITION_INVALID; av_freep( &self->av_frame ); - - if ( use_new_seek ) - { - // flush any pictures still in decode buffer - avcodec_flush_buffers( codec_context ); - } } } return paused; @@ -1263,7 +1261,7 @@ static void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, // extract alpha from planar formats if ( ( pix_fmt == PIX_FMT_YUVA420P -#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(35<<8)+0) +#if defined(FFUDIV) && LIBAVUTIL_VERSION_INT >= ((51<<16)+(35<<8)+101) || pix_fmt == PIX_FMT_YUVA444P #endif ) && @@ -1347,7 +1345,7 @@ static void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height ); img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height ); } - else if ( format == mlt_image_rgb24a || format == mlt_image_opengl ) + else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) { AVPicture output; avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height ); @@ -1396,7 +1394,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame ); // Obtain the frame number of this frame - mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" ); + mlt_position position = mlt_frame_original_position( frame ); // Get the producer properties mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer ); @@ -1413,19 +1411,47 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form 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_properties_get_int( properties, "noimagecache" ) ) + if ( ! self->image_cache ) { - self->image_cache = mlt_cache_init(); - self->alpha_cache = mlt_cache_init(); + // if cache size supplied by environment variable + int cache_supplied = getenv( "MLT_AVFORMAT_CACHE" ) != NULL; + int cache_size = cache_supplied? atoi( getenv( "MLT_AVFORMAT_CACHE" ) ) : 0; + + // cache size supplied via property + if ( mlt_properties_get( properties, "cache" ) ) + { + cache_supplied = 1; + cache_size = mlt_properties_get_int( properties, "cache" ); + } + if ( mlt_properties_get_int( properties, "noimagecache" ) ) + cache_size = 0; + // create cache if not disabled + if ( !cache_supplied || cache_size > 0 ) + self->image_cache = mlt_cache_init(); + // set cache size if supplied + if ( self->image_cache && cache_supplied ) + mlt_cache_set_size( self->image_cache, cache_size ); } if ( self->image_cache ) { - mlt_cache_item item = mlt_cache_get( self->image_cache, (void*) position ); - uint8_t *original = mlt_cache_item_data( item, (int*) format ); + mlt_frame original = mlt_cache_get_frame( self->image_cache, position ); if ( original ) { + mlt_properties orig_props = MLT_FRAME_PROPERTIES( original ); + int size = 0; + + *buffer = mlt_properties_get_data( orig_props, "alpha", &size ); + if (*buffer) + mlt_frame_set_alpha( frame, *buffer, size, NULL ); + *buffer = mlt_properties_get_data( orig_props, "image", &size ); + mlt_frame_set_image( frame, *buffer, size, NULL ); + mlt_properties_set_data( frame_properties, "avformat.image_cache", original, 0, (mlt_destructor) mlt_frame_close, NULL ); + *format = mlt_properties_get_int( orig_props, "format" ); + // Set the resolution *width = codec_context->width; *height = codec_context->height; @@ -1434,44 +1460,11 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 ) *height = 1080; - // Cache hit - int size = mlt_image_format_size( *format, *width, *height, NULL ); - if ( writable ) - { - *buffer = mlt_pool_alloc( size ); - mlt_frame_set_image( frame, *buffer, size, mlt_pool_release ); - memcpy( *buffer, original, size ); - mlt_cache_item_close( item ); - } - else - { - *buffer = original; - mlt_properties_set_data( frame_properties, "avformat.image_cache", item, 0, ( mlt_destructor )mlt_cache_item_close, NULL ); - mlt_frame_set_image( frame, *buffer, size, NULL ); - } - self->got_picture = 1; - - // check for alpha - item = mlt_cache_get( self->alpha_cache, (void*) position ); - original = mlt_cache_item_data( item, &size ); - if ( original ) - { - alpha = mlt_pool_alloc( size ); - memcpy( alpha, original, size ); - mlt_cache_item_close( item ); - } - + got_picture = 1; goto exit_get_image; } } // Cache miss - int image_size = 0; - - // Packet - AVPacket pkt; - - // Special case ffwd handling - int ignore = 0; // We may want to use the source fps if available double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) / @@ -1489,15 +1482,16 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form strcmp( codec_context->codec->name, "mjpeg" ) && strcmp( codec_context->codec->name, "rawvideo" ) ); - // Turn on usage of new seek API and PTS for seeking - int use_new_seek = self->seekable && - codec_context->codec_id == CODEC_ID_H264 && !strcmp( context->iformat->name, "mpegts" ); - if ( mlt_properties_get( properties, "new_seek" ) ) - use_new_seek = mlt_properties_get_int( properties, "new_seek" ); double delay = mlt_properties_get_double( properties, "video_delay" ); // Seek if necessary - int paused = seek_video( self, position, req_position, must_decode, use_new_seek, &ignore ); + const char *interp = mlt_properties_get( frame_properties, "rescale.interp" ); + int preseek = must_decode +#if defined(FFUDIV) && LIBAVFORMAT_VERSION_INT >= ((53<<16)+(24<<8)+2) + && ( interp && strcmp( interp, "nearest" ) ) +#endif + && codec_context->has_b_frames; + int paused = seek_video( self, position, req_position, preseek ); // Seek might have reopened the file context = self->video_format; @@ -1511,10 +1505,8 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form *format = pick_format( codec_context->pix_fmt ); // Duplicate the last image if necessary - if ( self->av_frame && self->av_frame->linesize[0] && self->got_picture - && ( paused - || self->current_position == req_position - || ( !use_new_seek && self->current_position > req_position ) ) ) + if ( self->av_frame && self->av_frame->linesize[0] + && ( paused || self->current_position >= req_position ) ) { // Duplicate it if ( ( image_size = allocate_buffer( frame, codec_context, buffer, format, width, height ) ) ) @@ -1539,18 +1531,14 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form #endif convert_image( self->av_frame, *buffer, codec_context->pix_fmt, format, *width, *height, self->colorspace, &alpha ); + got_picture = 1; } - else - mlt_frame_get_image( frame, buffer, format, width, height, writable ); } else { int ret = 0; int64_t int_position = 0; int decode_errors = 0; - int got_picture = 0; - - av_init_packet( &pkt ); // Construct an AVFrame for YUV422 conversion if ( !self->av_frame ) @@ -1559,22 +1547,25 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form while( ret >= 0 && !got_picture ) { // Read a packet + if ( self->pkt.stream_index == self->video_index ) + av_free_packet( &self->pkt ); + av_init_packet( &self->pkt ); pthread_mutex_lock( &self->packets_mutex ); if ( mlt_deque_count( self->vpackets ) ) { AVPacket *tmp = (AVPacket*) mlt_deque_pop_front( self->vpackets ); - pkt = *tmp; + self->pkt = *tmp; free( tmp ); } else { - ret = av_read_frame( context, &pkt ); - if ( ret >= 0 && !self->seekable && pkt.stream_index == self->audio_index ) + ret = av_read_frame( context, &self->pkt ); + if ( ret >= 0 && !self->seekable && self->pkt.stream_index == self->audio_index ) { - if ( !av_dup_packet( &pkt ) ) + if ( !av_dup_packet( &self->pkt ) ) { AVPacket *tmp = malloc( sizeof(AVPacket) ); - *tmp = pkt; + *tmp = self->pkt; mlt_deque_push_back( self->apackets, tmp ); } } @@ -1582,53 +1573,30 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form pthread_mutex_unlock( &self->packets_mutex ); // We only deal with video from the selected video_index - if ( ret >= 0 && pkt.stream_index == self->video_index && pkt.size > 0 ) + if ( ret >= 0 && self->pkt.stream_index == self->video_index && self->pkt.size > 0 ) { - // Determine time code of the packet - if ( use_new_seek ) + int64_t pts = best_pts( self, self->pkt.pts, self->pkt.dts ); + if ( pts != AV_NOPTS_VALUE ) { - int64_t pts = pkt.pts; - if ( self->first_pts > 0 ) + if ( !self->seekable && self->first_pts == AV_NOPTS_VALUE ) + self->first_pts = pts; + if ( self->first_pts != AV_NOPTS_VALUE ) pts -= self->first_pts; else if ( context->start_time != AV_NOPTS_VALUE ) pts -= context->start_time; - int_position = ( int64_t )( ( av_q2d( stream->time_base ) * pts + delay ) * source_fps + 0.1 ); - if ( pkt.pts == AV_NOPTS_VALUE ) - { - self->invalid_pts_counter++; - if ( self->invalid_pts_counter > 20 ) - { - mlt_log_panic( MLT_PRODUCER_SERVICE(producer), "\ainvalid PTS; DISABLING NEW_SEEK!\n" ); - mlt_properties_set_int( properties, "new_seek", 0 ); - int_position = req_position; - use_new_seek = 0; - } - } - else - { - self->invalid_pts_counter = 0; - } - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.pts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n", - pkt.pts, req_position, self->current_position, int_position ); + int_position = ( int64_t )( ( av_q2d( stream->time_base ) * pts + delay ) * source_fps + 0.5 ); + if ( int_position == self->last_position ) + int_position = self->last_position + 1; } - else + mlt_log_debug( MLT_PRODUCER_SERVICE(producer), + "V pkt.pts %"PRId64" pkt.dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n", + self->pkt.pts, self->pkt.dts, req_position, self->current_position, int_position ); + + // Make a dumb assumption on streams that contain wild timestamps + if ( abs( req_position - int_position ) > 999 ) { - if ( pkt.dts != AV_NOPTS_VALUE ) - { - int_position = ( int64_t )( ( av_q2d( stream->time_base ) * pkt.dts + delay ) * source_fps + 0.5 ); - if ( context->start_time != AV_NOPTS_VALUE ) - int_position -= ( int64_t )( context->start_time * source_fps / AV_TIME_BASE + 0.5 ); - if ( int_position == self->last_position ) - int_position = self->last_position + 1; - } - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n", - pkt.dts, req_position, self->current_position, int_position ); - // Make a dumb assumption on streams that contain wild timestamps - if ( abs( req_position - int_position ) > 999 ) - { - int_position = req_position; - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!" ); - } + int_position = req_position; + mlt_log_warning( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!\n" ); } self->last_position = int_position; @@ -1645,13 +1613,13 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form self->vdpau->is_decoded = 0; } #endif - codec_context->reordered_opaque = pkt.pts; + codec_context->reordered_opaque = int_position; if ( int_position >= req_position ) codec_context->skip_loop_filter = AVDISCARD_NONE; #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0)) - ret = avcodec_decode_video2( codec_context, self->av_frame, &got_picture, &pkt ); + ret = avcodec_decode_video2( codec_context, self->av_frame, &got_picture, &self->pkt ); #else - ret = avcodec_decode_video( codec_context, self->av_frame, &got_picture, pkt.data, pkt.size ); + ret = avcodec_decode_video( codec_context, self->av_frame, &got_picture, self->pkt.data, self->pkt.size ); #endif // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors. if ( ret < 0 ) @@ -1667,34 +1635,26 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form if ( got_picture ) { - if ( use_new_seek ) + // Get position of reordered frame + int_position = self->av_frame->reordered_opaque; +#if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(106<<8)+0)) + pts = best_pts( self, self->av_frame->pkt_pts, self->av_frame->pkt_dts ); + if ( pts != AV_NOPTS_VALUE ) { - // Determine time code of the packet - int64_t pts = self->av_frame->reordered_opaque; - if ( self->first_pts > 0 ) + if ( self->first_pts != AV_NOPTS_VALUE ) pts -= self->first_pts; else if ( context->start_time != AV_NOPTS_VALUE ) pts -= context->start_time; - int_position = ( int64_t )( av_q2d( stream->time_base) * pts * source_fps + 0.1 ); - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "got frame %"PRId64", key %d\n", int_position, self->av_frame->key_frame ); + int_position = ( int64_t )( ( av_q2d( stream->time_base ) * pts + delay ) * source_fps + 0.5 ); } - // Handle ignore +#endif + if ( int_position < req_position ) - { - ignore = 0; got_picture = 0; - } else if ( int_position >= req_position ) - { - ignore = 0; codec_context->skip_loop_filter = AVDISCARD_NONE; - } - else if ( ignore -- ) - { - got_picture = 0; - } } - mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY ); + mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, self->pkt.flags & PKT_FLAG_KEY ); } // Now handle the picture if we have one @@ -1748,34 +1708,34 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form format, *width, *height, self->colorspace, &alpha ); self->top_field_first |= self->av_frame->top_field_first; self->current_position = int_position; - self->got_picture = 1; } else { got_picture = 0; } } - if ( self->seekable || pkt.stream_index != self->audio_index ) - av_free_packet( &pkt ); + + // Free packet data if not video and not live audio packet + if ( self->pkt.stream_index != self->video_index && + !( !self->seekable && self->pkt.stream_index == self->audio_index ) ) + av_free_packet( &self->pkt ); } } - if ( self->got_picture && image_size > 0 && self->image_cache ) + // set alpha + if ( alpha ) + mlt_frame_set_alpha( frame, alpha, (*width) * (*height), mlt_pool_release ); + + if ( image_size > 0 && self->image_cache ) { - // Copy buffer to image cache - uint8_t *image = mlt_pool_alloc( image_size ); - memcpy( image, *buffer, image_size ); - mlt_cache_put( self->image_cache, (void*) position, image, *format, mlt_pool_release ); - if ( alpha ) - { - int alpha_size = (*width) * (*height); - image = mlt_pool_alloc( alpha_size ); - memcpy( image, alpha, alpha_size ); - mlt_cache_put( self->alpha_cache, (void*) position, image, alpha_size, mlt_pool_release ); - } + mlt_properties_set_int( frame_properties, "format", *format ); + mlt_cache_put_frame( self->image_cache, frame ); } + // Try to duplicate last image if there was a decoding failure - else if ( !image_size && self->av_frame && self->av_frame->linesize[0] ) + // TODO: with multithread decoding a partial frame decoding resulting + // in failure also resets av_frame making test below fail. + if ( !image_size && self->av_frame && self->av_frame->linesize[0] ) { // Duplicate it if ( ( image_size = allocate_buffer( frame, codec_context, buffer, format, width, height ) ) ) @@ -1800,10 +1760,8 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form #endif convert_image( self->av_frame, *buffer, codec_context->pix_fmt, format, *width, *height, self->colorspace, &alpha ); - self->got_picture = 1; + got_picture = 1; } - else - mlt_frame_get_image( frame, buffer, format, width, height, writable ); } // Regardless of speed, we expect to get the next frame (cos we ain't too bright) @@ -1831,11 +1789,7 @@ exit_get_image: mlt_properties_set_int( properties, "meta.media.progressive", mlt_properties_get_int( frame_properties, "progressive" ) ); mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) ); - // set alpha - if ( alpha ) - mlt_frame_set_alpha( frame, alpha, (*width) * (*height), mlt_pool_release ); - - return !self->got_picture; + return !got_picture; } /** Process properties as AVOptions and apply to AV context obj @@ -1937,8 +1891,11 @@ static int video_codec_init( producer_avformat self, int index, mlt_properties p #endif // Reset some image properties - mlt_properties_set_int( properties, "width", self->video_codec->width ); - mlt_properties_set_int( properties, "height", self->video_codec->height ); + if ( self->video_codec ) + { + mlt_properties_set_int( properties, "width", self->video_codec->width ); + mlt_properties_set_int( properties, "height", self->video_codec->height ); + } // For DV, we'll just use the saved aspect ratio if ( codec_context->codec_id != CODEC_ID_DVVIDEO ) get_aspect_ratio( properties, stream, self->video_codec, NULL ); @@ -2114,7 +2071,7 @@ static void producer_set_up_video( producer_avformat self, mlt_frame frame ) } } -static int seek_audio( producer_avformat self, mlt_position position, double timecode, int *ignore ) +static int seek_audio( producer_avformat self, mlt_position position, double timecode ) { int paused = 0; @@ -2126,11 +2083,6 @@ static int seek_audio( producer_avformat self, mlt_position position, double tim // We're paused - silence required paused = 1; } - else if ( !self->seekable && position > self->audio_expected && ( position - self->audio_expected ) < 250 ) - { - // Fast forward - seeking is inefficient for small distances - just ignore following frames - *ignore = position - self->audio_expected; - } else if ( position < self->audio_expected || position - self->audio_expected >= 12 ) { AVFormatContext *context = self->audio_format; @@ -2251,11 +2203,18 @@ static int decode_audio( producer_avformat self, int *ignore, AVPacket pkt, int // Skip this on non-seekable, audio-only inputs. if ( pkt.pts >= 0 && ( self->seekable || self->video_format ) && *ignore == 0 && audio_used > samples / 2 ) { - double current_pts = av_q2d( context->streams[ index ]->time_base ) * pkt.pts; + int64_t pts = pkt.pts; + if ( self->first_pts != 0 ) + pts -= self->first_pts; + else if ( context->start_time != AV_NOPTS_VALUE ) + pts -= context->start_time; + double timebase = av_q2d( context->streams[ index ]->time_base ); + int64_t int_position = ( int64_t )( timebase * pts * fps + 0.5 ); int64_t req_position = ( int64_t )( timecode * fps + 0.5 ); - int64_t int_position = ( int64_t )( current_pts * fps + 0.5 ); - if ( context->start_time != AV_NOPTS_VALUE ) - int_position -= ( int64_t )( fps * context->start_time / AV_TIME_BASE + 0.5 ); + + mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent), + "A pkt.pts %"PRId64" pkt.dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n", + pkt.pts, pkt.dts, req_position, self->current_position, int_position ); if ( int_position > 0 ) { @@ -2264,7 +2223,7 @@ static int decode_audio( producer_avformat self, int *ignore, AVPacket pkt, int *ignore = req_position - int_position; else if ( self->audio_index != INT_MAX && int_position > req_position + 2 ) // We are ahead, so seek backwards some more - seek_audio( self, req_position, timecode - 1.0, ignore ); + seek_audio( self, req_position, timecode - 1.0 ); } } @@ -2283,7 +2242,7 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format pthread_mutex_lock( &self->audio_mutex ); // Obtain the frame number of this frame - mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" ); + mlt_position position = mlt_frame_original_position( frame ); // Calculate the real time code double real_timecode = producer_time_of_frame( self->parent, position ); @@ -2295,7 +2254,7 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format int ignore[ MAX_AUDIO_STREAMS ] = { 0 }; // Flag for paused (silence) - int paused = seek_audio( self, position, real_timecode, &ignore[0] ); + int paused = seek_audio( self, position, real_timecode ); // Initialize ignore for all streams from the seek return value int i = MAX_AUDIO_STREAMS; @@ -2313,14 +2272,14 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format if ( self->audio_index == INT_MAX ) { index = 0; - index_max = context->nb_streams; + index_max = FFMIN( MAX_AUDIO_STREAMS, context->nb_streams ); *channels = self->total_channels; *samples = *samples * FFMAX( self->max_frequency, *frequency ) / *frequency; *frequency = FFMAX( self->max_frequency, *frequency ); } // Initialize the resamplers and buffers - for ( ; index < index_max; index++ ) + for ( ; index < index_max && index < MAX_AUDIO_STREAMS; index++ ) { // Get codec context AVCodecContext *codec_context = self->audio_codec[ index ]; @@ -2392,7 +2351,7 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format { // Check if there is enough audio for all streams got_audio = 1; - for ( index = 0; got_audio && index < context->nb_streams; index++ ) + for ( index = 0; got_audio && index < index_max; index++ ) if ( ( self->audio_codec[ index ] && self->audio_used[ index ] < *samples ) || ignore[ index ] ) got_audio = 0; if ( got_audio ) @@ -2424,7 +2383,7 @@ static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format // We only deal with audio from the selected audio index index = pkt.stream_index; - if ( ret >= 0 && pkt.data && pkt.size > 0 && ( index == self->audio_index || + if ( index < MAX_AUDIO_STREAMS && ret >= 0 && pkt.data && pkt.size > 0 && ( index == self->audio_index || ( self->audio_index == INT_MAX && context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) ) { int channels2 = ( self->audio_index == INT_MAX || !self->audio_resample[index] ) ? @@ -2654,7 +2613,7 @@ static void producer_set_up_audio( producer_avformat self, mlt_frame frame ) else if ( context && index > -1 && audio_codec_init( self, index, properties ) ) { // Set the frame properties - if ( index < INT_MAX ) + if ( index < MAX_AUDIO_STREAMS ) { mlt_properties_set_int( frame_properties, "frequency", self->audio_codec[ index ]->sample_rate ); mlt_properties_set_int( frame_properties, "channels", self->audio_codec[ index ]->channels ); @@ -2712,7 +2671,7 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i // Set the position of this producer mlt_position position = self->seekable ? mlt_producer_frame( producer ) : self->nonseek_position++; - mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", position ); + mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "original_position", position ); // Calculate the next timecode mlt_producer_prepare_next( producer ); @@ -2725,6 +2684,7 @@ static void producer_avformat_close( producer_avformat self ) mlt_log_debug( NULL, "producer_avformat_close\n" ); // Cleanup av contexts + av_free_packet( &self->pkt ); av_free( self->av_frame ); pthread_mutex_lock( &self->open_mutex ); int i; @@ -2763,8 +2723,6 @@ static void producer_avformat_close( producer_avformat self ) #endif if ( self->image_cache ) mlt_cache_close( self->image_cache ); - if ( self->alpha_cache ) - mlt_cache_close( self->alpha_cache ); // Cleanup the mutexes pthread_mutex_destroy( &self->audio_mutex );