2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2012 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Author: Dan Dennedy <dan@dennedy.org>
6 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <framework/mlt_producer.h>
25 #include <framework/mlt_frame.h>
26 #include <framework/mlt_profile.h>
27 #include <framework/mlt_log.h>
28 #include <framework/mlt_deque.h>
29 #include <framework/mlt_factory.h>
30 #include <framework/mlt_cache.h>
32 // ffmpeg Header files
33 #include <libavformat/avformat.h>
35 # include <libswscale/swscale.h>
38 #if LIBAVUTIL_VERSION_INT >= ((50<<16)+(38<<8)+0)
39 # include <libavutil/samplefmt.h>
41 # define AV_SAMPLE_FMT_S16 SAMPLE_FMT_S16
42 # define AV_SAMPLE_FMT_S32 SAMPLE_FMT_S32
43 # define AV_SAMPLE_FMT_FLT SAMPLE_FMT_FLT
44 # if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
45 const char *avcodec_get_sample_fmt_name(int sample_fmt);
50 # include <libavcodec/vdpau.h>
52 #if (LIBAVUTIL_VERSION_INT > ((50<<16)+(7<<8)+0))
53 # include <libavutil/pixdesc.h>
55 #if (LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0))
56 # include <libavutil/dict.h>
59 // System header files
65 #if LIBAVUTIL_VERSION_INT < (50<<16)
66 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
67 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
70 #if LIBAVCODEC_VERSION_MAJOR >= 53
71 #include <libavutil/opt.h>
72 #define CODEC_TYPE_VIDEO AVMEDIA_TYPE_VIDEO
73 #define CODEC_TYPE_AUDIO AVMEDIA_TYPE_AUDIO
74 #define PKT_FLAG_KEY AV_PKT_FLAG_KEY
76 #include <libavcodec/opt.h>
79 #define POSITION_INITIAL (-2)
80 #define POSITION_INVALID (-1)
82 #define MAX_AUDIO_STREAMS (10)
83 #define MAX_VDPAU_SURFACES (10)
85 struct producer_avformat_s
88 AVFormatContext *dummy_context;
89 AVFormatContext *audio_format;
90 AVFormatContext *video_format;
91 AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ];
92 AVCodecContext *video_codec;
94 ReSampleContext *audio_resample[ MAX_AUDIO_STREAMS ];
95 mlt_position audio_expected;
96 mlt_position video_expected;
100 int64_t last_position;
102 int64_t current_position;
103 mlt_position nonseek_position;
105 uint8_t *audio_buffer[ MAX_AUDIO_STREAMS ];
106 size_t audio_buffer_size[ MAX_AUDIO_STREAMS ];
107 uint8_t *decode_buffer[ MAX_AUDIO_STREAMS ];
108 int audio_used[ MAX_AUDIO_STREAMS ];
110 int audio_max_stream;
114 unsigned int invalid_pts_counter;
115 double resample_factor;
116 mlt_cache image_cache;
118 pthread_mutex_t video_mutex;
119 pthread_mutex_t audio_mutex;
122 pthread_mutex_t packets_mutex;
123 pthread_mutex_t open_mutex;
128 struct vdpau_render_state render_states[MAX_VDPAU_SURFACES];
142 typedef struct producer_avformat_s *producer_avformat;
144 // Forward references.
145 static int list_components( char* file );
146 static int producer_open( producer_avformat self, mlt_profile profile, const char *URL, int take_lock );
147 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
148 static void producer_avformat_close( producer_avformat );
149 static void producer_close( mlt_producer parent );
150 static void producer_set_up_video( producer_avformat self, mlt_frame frame );
151 static void producer_set_up_audio( producer_avformat self, mlt_frame frame );
152 static void apply_properties( void *obj, mlt_properties properties, int flags );
153 static int video_codec_init( producer_avformat self, int index, mlt_properties properties );
154 static void get_audio_streams_info( producer_avformat self );
160 /** Constructor for libavformat.
163 mlt_producer producer_avformat_init( mlt_profile profile, const char *service, char *file )
165 if ( list_components( file ) )
168 mlt_producer producer = NULL;
170 // Check that we have a non-NULL argument
173 // Construct the producer
174 producer_avformat self = calloc( 1, sizeof( struct producer_avformat_s ) );
175 producer = calloc( 1, sizeof( struct mlt_producer_s ) );
178 if ( mlt_producer_init( producer, self ) == 0 )
180 self->parent = producer;
182 // Get the properties
183 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
185 // Set the resource property (required for all producers)
186 mlt_properties_set( properties, "resource", file );
188 // Register transport implementation with the producer
189 producer->close = (mlt_destructor) producer_close;
191 // Register our get_frame implementation
192 producer->get_frame = producer_get_frame;
194 if ( strcmp( service, "avformat-novalidate" ) )
197 if ( producer_open( self, profile, file, 1 ) != 0 )
200 mlt_producer_close( producer );
202 producer_avformat_close( self );
204 else if ( self->seekable )
206 // Close the file to release resources for large playlists - reopen later as needed
207 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
208 if ( self->audio_format )
209 avformat_close_input( &self->audio_format );
210 if ( self->video_format )
211 avformat_close_input( &self->video_format );
213 if ( self->audio_format )
214 av_close_input_file( self->audio_format );
215 if ( self->video_format )
216 av_close_input_file( self->video_format );
218 self->audio_format = NULL;
219 self->video_format = NULL;
224 // Default the user-selectable indices from the auto-detected indices
225 mlt_properties_set_int( properties, "audio_index", self->audio_index );
226 mlt_properties_set_int( properties, "video_index", self->video_index );
228 mlt_service_cache_set_size( MLT_PRODUCER_SERVICE(producer), "producer_avformat", 5 );
230 mlt_service_cache_put( MLT_PRODUCER_SERVICE(producer), "producer_avformat", self, 0, (mlt_destructor) producer_avformat_close );
237 int list_components( char* file )
241 // Report information about available demuxers and codecs as YAML Tiny
242 if ( file && strstr( file, "f-list" ) )
244 fprintf( stderr, "---\nformats:\n" );
245 AVInputFormat *format = NULL;
246 while ( ( format = av_iformat_next( format ) ) )
247 fprintf( stderr, " - %s\n", format->name );
248 fprintf( stderr, "...\n" );
251 if ( file && strstr( file, "acodec-list" ) )
253 fprintf( stderr, "---\naudio_codecs:\n" );
254 AVCodec *codec = NULL;
255 while ( ( codec = av_codec_next( codec ) ) )
256 if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
257 fprintf( stderr, " - %s\n", codec->name );
258 fprintf( stderr, "...\n" );
261 if ( file && strstr( file, "vcodec-list" ) )
263 fprintf( stderr, "---\nvideo_codecs:\n" );
264 AVCodec *codec = NULL;
265 while ( ( codec = av_codec_next( codec ) ) )
266 if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
267 fprintf( stderr, " - %s\n", codec->name );
268 fprintf( stderr, "...\n" );
275 /** Find the default streams.
278 static mlt_properties find_default_streams( producer_avformat self )
282 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
283 AVDictionaryEntry *tag = NULL;
285 AVMetadataTag *tag = NULL;
287 AVFormatContext *context = self->video_format;
288 mlt_properties meta_media = MLT_PRODUCER_PROPERTIES( self->parent );
290 // Default to the first audio and video streams found
291 self->audio_index = -1;
292 self->video_index = -1;
294 mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
296 // Allow for multiple audio and video streams in the file and select first of each (if available)
297 for( i = 0; i < context->nb_streams; i++ )
299 // Get the codec context
300 AVStream *stream = context->streams[ i ];
301 if ( ! stream ) continue;
302 AVCodecContext *codec_context = stream->codec;
303 if ( ! codec_context ) continue;
304 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
305 if ( ! codec ) continue;
307 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
309 // Determine the type and obtain the first index of each type
310 switch( codec_context->codec_type )
312 case CODEC_TYPE_VIDEO:
313 // Use first video stream
314 if ( self->video_index < 0 )
315 self->video_index = i;
316 mlt_properties_set( meta_media, key, "video" );
317 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
318 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
319 double ffmpeg_fps = av_q2d( context->streams[ i ]->avg_frame_rate );
320 if ( isnan( ffmpeg_fps ) || ffmpeg_fps == 0 )
321 ffmpeg_fps = av_q2d( context->streams[ i ]->r_frame_rate );
322 mlt_properties_set_double( meta_media, key, ffmpeg_fps );
324 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
327 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
328 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
329 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
331 snprintf( key, sizeof(key), "meta.media.%d.codec.width", i );
332 mlt_properties_set_int( meta_media, key, codec_context->width );
333 snprintf( key, sizeof(key), "meta.media.%d.codec.height", i );
334 mlt_properties_set_int( meta_media, key, codec_context->height );
335 snprintf( key, sizeof(key), "meta.media.%d.codec.frame_rate", i );
336 mlt_properties_set_double( meta_media, key, (double) codec_context->time_base.den /
337 ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num ) );
338 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
339 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(3<<8)+0)
340 mlt_properties_set( meta_media, key, av_get_pix_fmt_name( codec_context->pix_fmt ) );
342 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
344 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
345 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
346 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
347 snprintf( key, sizeof(key), "meta.media.%d.codec.colorspace", i );
348 switch ( codec_context->colorspace )
350 case AVCOL_SPC_SMPTE240M:
351 mlt_properties_set_int( meta_media, key, 240 );
353 case AVCOL_SPC_BT470BG:
354 case AVCOL_SPC_SMPTE170M:
355 mlt_properties_set_int( meta_media, key, 601 );
357 case AVCOL_SPC_BT709:
358 mlt_properties_set_int( meta_media, key, 709 );
361 // This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
362 mlt_properties_set_int( meta_media, key, codec_context->width * codec_context->height > 750000 ? 709 : 601 );
367 case CODEC_TYPE_AUDIO:
368 if ( !codec_context->channels )
370 // Use first audio stream
371 if ( self->audio_index < 0 )
372 self->audio_index = i;
373 mlt_properties_set( meta_media, key, "audio" );
374 #if LIBAVUTIL_VERSION_INT >= ((50<<16)+(38<<8)+0)
375 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
376 mlt_properties_set( meta_media, key, av_get_sample_fmt_name( codec_context->sample_fmt ) );
377 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
378 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
379 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
381 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
382 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
383 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
384 mlt_properties_set_int( meta_media, key, codec_context->channels );
389 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
390 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
391 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
392 mlt_properties_set( meta_media, key, codec->name );
393 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
394 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
395 mlt_properties_set( meta_media, key, codec->long_name );
397 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
398 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
399 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
400 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
401 // snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
402 // mlt_properties_set_int( meta_media, key, codec_context->profile );
403 // snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
404 // mlt_properties_set_int( meta_media, key, codec_context->level );
407 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
408 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
409 while ( ( tag = av_dict_get( stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
411 while ( ( tag = av_metadata_get( stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX ) ) )
414 if ( tag->value && strcmp( tag->value, "" ) && strcmp( tag->value, "und" ) )
416 snprintf( key, sizeof(key), "meta.attr.%d.stream.%s.markup", i, tag->key );
417 mlt_properties_set( meta_media, key, tag->value );
422 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
423 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
424 while ( ( tag = av_dict_get( context->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
426 while ( ( tag = av_metadata_get( context->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX ) ) )
429 if ( tag->value && strcmp( tag->value, "" ) && strcmp( tag->value, "und" ) )
431 snprintf( key, sizeof(key), "meta.attr.%s.markup", tag->key );
432 mlt_properties_set( meta_media, key, tag->value );
436 if ( context->title && strcmp( context->title, "" ) )
437 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
438 if ( context->author && strcmp( context->author, "" ) )
439 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
440 if ( context->copyright && strcmp( context->copyright, "" ) )
441 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
442 if ( context->comment )
443 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
444 if ( context->album )
445 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
447 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
448 if ( context->track )
449 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
455 static inline int dv_is_pal( AVPacket *pkt )
457 return pkt->data[3] & 0x80;
460 static int dv_is_wide( AVPacket *pkt )
462 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
464 for ( ; i < pkt->size; i += 5 /* packet size */ )
466 if ( pkt->data[ i ] == 0x61 )
468 uint8_t x = pkt->data[ i + 2 ] & 0x7;
469 return ( x == 2 ) || ( x == 7 );
475 static double get_aspect_ratio( mlt_properties properties, AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
477 double aspect_ratio = 1.0;
479 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
483 if ( dv_is_pal( pkt ) )
485 if ( dv_is_wide( pkt ) )
487 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 64 );
488 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 45 );
492 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 16 );
493 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 15 );
498 if ( dv_is_wide( pkt ) )
500 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 32 );
501 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 27 );
505 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 8 );
506 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 9 );
513 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
514 stream->sample_aspect_ratio;
516 codec_context->sample_aspect_ratio;
518 // Override FFmpeg's notion of DV aspect ratios, which are
519 // based upon a width of 704. Since we do not have a normaliser
520 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
521 // we just coerce the values to facilitate a passive behaviour through
522 // the rescale normaliser when using equivalent producers and consumers.
523 // = display_aspect / (width * height)
524 if ( ar.num == 10 && ar.den == 11 )
527 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 8 );
528 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 9 );
530 else if ( ar.num == 59 && ar.den == 54 )
533 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 16 );
534 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 15 );
536 else if ( ar.num == 40 && ar.den == 33 )
539 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 32 );
540 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 27 );
542 else if ( ar.num == 118 && ar.den == 81 )
545 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 64 );
546 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 45 );
552 AVRational codec_sar = codec_context->sample_aspect_ratio;
553 AVRational stream_sar =
554 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
555 stream->sample_aspect_ratio;
559 if ( codec_sar.num > 0 )
561 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", codec_sar.num );
562 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", codec_sar.den );
564 else if ( stream_sar.num > 0 )
566 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", stream_sar.num );
567 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", stream_sar.den );
571 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 1 );
572 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 1 );
575 AVRational ar = { mlt_properties_get_double( properties, "meta.media.sample_aspect_num" ), mlt_properties_get_double( properties, "meta.media.sample_aspect_den" ) };
576 aspect_ratio = av_q2d( ar );
577 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
582 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
583 static char* parse_url( mlt_profile profile, const char* URL, AVInputFormat **format, AVDictionary **params )
585 static char* parse_url( mlt_profile profile, const char* URL, AVInputFormat **format, AVFormatParameters *params )
588 if ( !URL ) return NULL;
591 char *protocol = strdup( URL );
592 char *url = strchr( protocol, ':' );
594 // Only if there is not a protocol specification that avformat can handle
595 #if LIBAVFORMAT_VERSION_MAJOR >= 53
596 if ( url && avio_check( URL, 0 ) < 0 )
598 if ( url && !url_exist( URL ) )
601 // Truncate protocol string
603 mlt_log_debug( NULL, "%s: protocol=%s resource=%s\n", __FUNCTION__, protocol, url + 1 );
606 *format = av_find_input_format( protocol );
608 // Eat the format designator
613 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
614 // support for legacy width and height parameters
618 // These are required by video4linux2 (defaults)
619 params->width = profile->width;
620 params->height = profile->height;
621 if ( !strstr( URL, "&frame_rate" ) )
622 params->time_base = (AVRational){ profile->frame_rate_den, profile->frame_rate_num };
623 params->channels = 2;
624 params->sample_rate = 48000;
628 url = strchr( url, '?' );
632 char *name = strdup( ++url );
633 char *value = strchr( name, '=' );
635 // Also accept : as delimiter for backwards compatibility.
636 value = strchr( name, ':' );
641 char *t = strchr( value, '&' );
644 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
645 // translate old parameters to new av_dict names
646 if ( !strcmp( name, "frame_rate" ) )
647 av_dict_set( params, "framerate", value, 0 );
648 else if ( !strcmp( name, "pix_fmt" ) )
649 av_dict_set( params, "pixel_format", value, 0 );
650 else if ( !strcmp( name, "width" ) )
651 width = strdup( value );
652 else if ( !strcmp( name, "height" ) )
653 height = strdup( value );
655 // generic demux/device option support
656 av_dict_set( params, name, value, 0 );
658 if ( !strcmp( name, "frame_rate" ) )
659 params->time_base.den = atoi( value );
660 else if ( !strcmp( name, "frame_rate_base" ) )
661 params->time_base.num = atoi( value );
662 else if ( !strcmp( name, "sample_rate" ) )
663 params->sample_rate = atoi( value );
664 else if ( !strcmp( name, "channel" ) )
665 params->channel = atoi( value );
666 else if ( !strcmp( name, "channels" ) )
667 params->channels = atoi( value );
668 #if (LIBAVUTIL_VERSION_INT > ((50<<16)+(7<<8)+0))
669 else if ( !strcmp( name, "pix_fmt" ) )
670 params->pix_fmt = av_get_pix_fmt( value );
672 else if ( !strcmp( name, "width" ) )
673 params->width = atoi( value );
674 else if ( !strcmp( name, "height" ) )
675 params->height = atoi( value );
676 else if ( !strcmp( name, "standard" ) )
677 params->standard = strdup( value );
681 url = strchr( url, '&' );
683 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
684 // continued support for legacy width and height parameters
685 if ( width && height )
687 char *s = malloc( strlen( width ) + strlen( height ) + 2 );
691 av_dict_set( params, "video_size", s, 0 );
694 if ( width ) free( width );
695 if ( height ) free ( height );
698 result = strdup( result );
702 result = strdup( URL );
708 static int get_basic_info( producer_avformat self, mlt_profile profile, const char *filename )
712 // Get the properties
713 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self->parent );
715 AVFormatContext *format = self->video_format;
717 // We will treat everything with the producer fps.
718 // TODO: make this more flexible.
719 double fps = mlt_profile_fps( profile );
722 if ( !mlt_properties_get_int( properties, "_length_computed" ) )
724 // The _length_computed flag prevents overwriting explicity set length/out/eof properties
725 // when producer_open is called after initial call when restoring or reseting the producer.
726 if ( format->duration != AV_NOPTS_VALUE )
728 // This isn't going to be accurate for all formats
729 mlt_position frames = ( mlt_position )( ( ( double )format->duration / ( double )AV_TIME_BASE ) * fps );
730 mlt_properties_set_position( properties, "out", frames - 1 );
731 mlt_properties_set_position( properties, "length", frames );
732 mlt_properties_set_int( properties, "_length_computed", 1 );
736 // Set live sources to run forever
737 mlt_properties_set_position( properties, "length", INT_MAX );
738 mlt_properties_set_position( properties, "out", INT_MAX - 1 );
739 mlt_properties_set( properties, "eof", "loop" );
740 mlt_properties_set_int( properties, "_length_computed", 1 );
744 // Check if we're seekable
745 // avdevices are typically AVFMT_NOFILE and not seekable
746 self->seekable = !format->iformat || !( format->iformat->flags & AVFMT_NOFILE );
749 // protocols can indicate if they support seeking
750 #if LIBAVFORMAT_VERSION_MAJOR >= 53
751 self->seekable = format->pb->seekable;
753 URLContext *uc = url_fileno( format->pb );
755 self->seekable = !uc->is_streamed;
758 if ( self->seekable )
760 // Do a more rigourous test of seekable on a disposable context
761 self->seekable = av_seek_frame( format, -1, format->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
762 mlt_properties_set_int( properties, "seekable", self->seekable );
763 self->dummy_context = format;
764 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
765 self->video_format = NULL;
766 avformat_open_input( &self->video_format, filename, NULL, NULL );
767 avformat_find_stream_info( self->video_format, NULL );
769 av_open_input_file( &self->video_format, filename, NULL, 0, NULL );
770 av_find_stream_info( self->video_format );
772 format = self->video_format;
775 // Fetch the width, height and aspect ratio
776 if ( self->video_index != -1 )
778 AVCodecContext *codec_context = format->streams[ self->video_index ]->codec;
779 mlt_properties_set_int( properties, "width", codec_context->width );
780 mlt_properties_set_int( properties, "height", codec_context->height );
782 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
784 // Fetch the first frame of DV so we can read it directly
789 ret = av_read_frame( format, &pkt );
790 if ( ret >= 0 && pkt.stream_index == self->video_index && pkt.size > 0 )
792 get_aspect_ratio( properties, format->streams[ self->video_index ], codec_context, &pkt );
793 av_free_packet(&pkt);
797 av_free_packet(&pkt);
802 get_aspect_ratio( properties, format->streams[ self->video_index ], codec_context, NULL );
806 // Verify that we can convert this to YUV 4:2:2
807 // TODO: we can now also return RGB and RGBA and quite possibly more in the future.
808 struct SwsContext *context = sws_getContext( codec_context->width, codec_context->height, codec_context->pix_fmt,
809 codec_context->width, codec_context->height, PIX_FMT_YUYV422, SWS_BILINEAR, NULL, NULL, NULL);
811 sws_freeContext( context );
822 static int producer_open( producer_avformat self, mlt_profile profile, const char *URL, int take_lock )
824 // Return an error code (0 == no error)
826 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self->parent );
831 pthread_mutex_init( &self->audio_mutex, NULL );
832 pthread_mutex_init( &self->video_mutex, NULL );
833 pthread_mutex_init( &self->packets_mutex, NULL );
834 pthread_mutex_init( &self->open_mutex, NULL );
835 pthread_mutex_lock( &self->audio_mutex );
836 pthread_mutex_lock( &self->video_mutex );
838 mlt_events_block( properties, self->parent );
841 AVInputFormat *format = NULL;
842 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
843 AVDictionary *params = NULL;
845 AVFormatParameters params;
846 memset( ¶ms, 0, sizeof(params) );
848 char *filename = parse_url( profile, URL, &format, ¶ms );
850 // Now attempt to open the file or device with filename
851 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
852 error = avformat_open_input( &self->video_format, filename, format, ¶ms ) < 0;
854 // If the URL is a network stream URL, then we probably need to open with full URL
855 error = avformat_open_input( &self->video_format, URL, format, ¶ms ) < 0;
857 error = av_open_input_file( &self->video_format, filename, format, 0, ¶ms ) < 0;
859 // If the URL is a network stream URL, then we probably need to open with full URL
860 error = av_open_input_file( &self->video_format, URL, format, 0, ¶ms ) < 0;
863 // Set MLT properties onto video AVFormatContext
864 if ( !error && self->video_format )
866 apply_properties( self->video_format, properties, AV_OPT_FLAG_DECODING_PARAM );
867 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(110<<8)+0)
868 if ( self->video_format->iformat && self->video_format->iformat->priv_class && self->video_format->priv_data )
869 apply_properties( self->video_format->priv_data, properties, AV_OPT_FLAG_DECODING_PARAM );
873 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
874 av_dict_free( ¶ms );
876 // Cleanup AVFormatParameters
877 if ( params.standard )
878 free( (void*) params.standard );
881 // If successful, then try to get additional info
882 if ( !error && self->video_format )
884 // Get the stream info
885 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
886 error = avformat_find_stream_info( self->video_format, NULL ) < 0;
888 error = av_find_stream_info( self->video_format ) < 0;
891 // Continue if no error
892 if ( !error && self->video_format )
894 // Find default audio and video streams
895 find_default_streams( self );
896 error = get_basic_info( self, profile, filename );
898 // Initialize position info
899 self->first_pts = AV_NOPTS_VALUE;
900 self->last_position = POSITION_INITIAL;
902 if ( !self->audio_format )
904 // We're going to cheat here - for seekable A/V files, we will have separate contexts
905 // to support independent seeking of audio from video.
906 // TODO: Is this really necessary?
907 if ( self->audio_index != -1 && self->video_index != -1 )
909 if ( self->seekable )
911 // And open again for our audio context
912 #if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
913 avformat_open_input( &self->audio_format, filename, NULL, NULL );
914 apply_properties( self->audio_format, properties, AV_OPT_FLAG_DECODING_PARAM );
915 if ( self->audio_format->iformat && self->audio_format->iformat->priv_class && self->audio_format->priv_data )
916 apply_properties( self->audio_format->priv_data, properties, AV_OPT_FLAG_DECODING_PARAM );
917 avformat_find_stream_info( self->audio_format, NULL );
919 av_open_input_file( &self->audio_format, filename, NULL, 0, NULL );
920 apply_properties( self->audio_format, properties, AV_OPT_FLAG_DECODING_PARAM );
921 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(110<<8)+0)
922 if ( self->audio_format->iformat && self->audio_format->iformat->priv_class && self->audio_format->priv_data )
923 apply_properties( self->audio_format->priv_data, properties, AV_OPT_FLAG_DECODING_PARAM );
925 av_find_stream_info( self->audio_format );
930 self->audio_format = self->video_format;
933 else if ( self->audio_index != -1 )
935 // We only have an audio context
936 self->audio_format = self->video_format;
937 self->video_format = NULL;
939 else if ( self->video_index == -1 )
941 // Something has gone wrong
944 if ( self->audio_format && !self->audio_streams )
945 get_audio_streams_info( self );
953 self->apackets = mlt_deque_init();
954 self->vpackets = mlt_deque_init();
957 if ( self->dummy_context )
959 pthread_mutex_lock( &self->open_mutex );
960 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
961 avformat_close_input( &self->dummy_context );
963 av_close_input_file( self->dummy_context );
965 self->dummy_context = NULL;
966 pthread_mutex_unlock( &self->open_mutex );
969 // Unlock the service
972 pthread_mutex_unlock( &self->audio_mutex );
973 pthread_mutex_unlock( &self->video_mutex );
975 mlt_events_unblock( properties, self->parent );
980 static void reopen_video( producer_avformat self, mlt_producer producer )
982 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
983 mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
984 pthread_mutex_lock( &self->audio_mutex );
985 pthread_mutex_lock( &self->open_mutex );
987 if ( self->video_codec )
988 avcodec_close( self->video_codec );
989 self->video_codec = NULL;
990 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
991 if ( self->dummy_context )
992 avformat_close_input( &self->dummy_context );
993 if ( self->video_format )
994 avformat_close_input( &self->video_format );
996 if ( self->dummy_context )
997 av_close_input_file( self->dummy_context );
998 if ( self->video_format )
999 av_close_input_file( self->video_format );
1001 self->dummy_context = NULL;
1002 self->video_format = NULL;
1003 pthread_mutex_unlock( &self->open_mutex );
1005 int audio_index = self->audio_index;
1006 int video_index = self->video_index;
1008 producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1009 mlt_properties_get( properties, "resource" ), 0 );
1011 self->audio_index = audio_index;
1012 if ( self->video_format && video_index > -1 )
1014 self->video_index = video_index;
1015 video_codec_init( self, video_index, properties );
1018 pthread_mutex_unlock( &self->audio_mutex );
1019 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
1022 static int seek_video( producer_avformat self, mlt_position position,
1023 int64_t req_position, int preseek, int use_pts )
1025 mlt_producer producer = self->parent;
1028 if ( self->seekable && ( position != self->video_expected || self->last_position < 0 ) )
1030 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1032 // Fetch the video format context
1033 AVFormatContext *context = self->video_format;
1035 // Get the video stream
1036 AVStream *stream = context->streams[ self->video_index ];
1038 // Get codec context
1039 AVCodecContext *codec_context = stream->codec;
1041 // We may want to use the source fps if available
1042 double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) /
1043 mlt_properties_get_double( properties, "meta.media.frame_rate_den" );
1046 if ( self->last_position == POSITION_INITIAL )
1052 while ( ret >= 0 && toscan-- > 0 )
1054 ret = av_read_frame( context, &pkt );
1055 if ( ret >= 0 && pkt.stream_index == self->video_index )
1057 mlt_log_debug( MLT_PRODUCER_SERVICE(producer),
1058 "first_pts %"PRId64" dts %"PRId64" pts_dts_delta %d\n",
1059 pkt.pts, pkt.dts, (int)(pkt.pts - pkt.dts) );
1060 self->first_pts = use_pts? pkt.pts : pkt.dts;
1061 if ( self->first_pts != AV_NOPTS_VALUE )
1064 av_free_packet( &pkt );
1066 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
1069 if ( self->av_frame && position + 1 == self->video_expected )
1071 // We're paused - use last image
1074 else if ( self->seekable && ( position < self->video_expected || position - self->video_expected >= 12 || self->last_position < 0 ) )
1076 // Calculate the timestamp for the requested frame
1077 int64_t timestamp = req_position / ( av_q2d( stream->time_base ) * source_fps );
1078 if ( req_position <= 0 )
1080 else if ( self->first_pts != AV_NOPTS_VALUE )
1081 timestamp += self->first_pts;
1082 else if ( context->start_time != AV_NOPTS_VALUE )
1083 timestamp += context->start_time;
1084 if ( preseek && av_q2d( stream->time_base ) != 0 )
1085 timestamp -= 2 / av_q2d( stream->time_base );
1086 if ( timestamp < 0 )
1088 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %"PRId64" position %d expected %d last_pos %"PRId64"\n",
1089 timestamp, position, self->video_expected, self->last_position );
1091 // Seek to the timestamp
1092 // NOTE: reopen_video is disabled at this time because it is causing trouble with A/V sync.
1093 if ( 1 || use_pts || req_position > 0 || self->last_position <= 0 )
1095 codec_context->skip_loop_filter = AVDISCARD_NONREF;
1096 av_seek_frame( context, self->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
1098 // flush any pictures still in decode buffer
1099 avcodec_flush_buffers( codec_context );
1103 // Re-open video stream when rewinding to beginning from somewhere else.
1104 // This is rather ugly, and I prefer not to do it this way, but ffmpeg is
1105 // not reliably seeking to the first frame across formats.
1106 reopen_video( self, producer );
1109 // Remove the cached info relating to the previous position
1110 self->current_position = POSITION_INVALID;
1111 self->last_position = POSITION_INVALID;
1112 av_freep( &self->av_frame );
1118 /** Convert a frame position to a time code.
1121 static double producer_time_of_frame( mlt_producer producer, mlt_position position )
1123 return ( double )position / mlt_producer_get_fps( producer );
1126 // Collect information about all audio streams
1128 static void get_audio_streams_info( producer_avformat self )
1130 // Fetch the audio format context
1131 AVFormatContext *context = self->audio_format;
1135 i < context->nb_streams;
1138 if ( context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
1140 AVCodecContext *codec_context = context->streams[i]->codec;
1141 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1143 // If we don't have a codec and we can't initialise it, we can't do much more...
1144 pthread_mutex_lock( &self->open_mutex );
1145 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1146 if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
1148 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1151 self->audio_streams++;
1152 self->audio_max_stream = i;
1153 self->total_channels += codec_context->channels;
1154 if ( codec_context->channels > self->max_channel )
1155 self->max_channel = codec_context->channels;
1156 if ( codec_context->sample_rate > self->max_frequency )
1157 self->max_frequency = codec_context->sample_rate;
1158 avcodec_close( codec_context );
1160 pthread_mutex_unlock( &self->open_mutex );
1163 mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
1164 self->audio_streams, self->audio_max_stream, self->total_channels, self->max_channel );
1166 // Other audio-specific initializations
1167 self->resample_factor = 1.0;
1170 static void set_luma_transfer( struct SwsContext *context, int colorspace, int use_full_range )
1172 #if defined(SWSCALE) && (LIBSWSCALE_VERSION_INT >= ((0<<16)+(7<<8)+2))
1174 const int *new_coefficients;
1176 int brightness, contrast, saturation;
1178 if ( sws_getColorspaceDetails( context, &coefficients, &full_range, &coefficients, &full_range,
1179 &brightness, &contrast, &saturation ) != -1 )
1181 // Don't change these from defaults unless explicitly told to.
1182 if ( use_full_range >= 0 )
1183 full_range = use_full_range;
1184 switch ( colorspace )
1190 new_coefficients = sws_getCoefficients( SWS_CS_ITU601 );
1193 new_coefficients = sws_getCoefficients( SWS_CS_SMPTE240M );
1196 new_coefficients = sws_getCoefficients( SWS_CS_ITU709 );
1199 new_coefficients = coefficients;
1202 sws_setColorspaceDetails( context, new_coefficients, full_range, new_coefficients, full_range,
1203 brightness, contrast, saturation );
1208 static mlt_image_format pick_format( enum PixelFormat pix_fmt )
1216 return mlt_image_rgb24a;
1217 case PIX_FMT_YUV420P:
1218 case PIX_FMT_YUVJ420P:
1219 case PIX_FMT_YUVA420P:
1220 return mlt_image_yuv420p;
1224 case PIX_FMT_MONOWHITE:
1225 case PIX_FMT_MONOBLACK:
1228 return mlt_image_rgb24;
1230 return mlt_image_yuv422;
1234 static void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
1235 mlt_image_format *format, int width, int height, int colorspace, uint8_t **alpha )
1238 int full_range = -1;
1239 int flags = SWS_BICUBIC | SWS_ACCURATE_RND;
1242 flags |= SWS_CPU_CAPS_MMX;
1245 flags |= SWS_CPU_CAPS_MMX2;
1248 // extract alpha from planar formats
1249 if ( ( pix_fmt == PIX_FMT_YUVA420P
1250 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(35<<8)+101)
1251 || pix_fmt == PIX_FMT_YUVA444P
1254 *format != mlt_image_rgb24a && *format != mlt_image_opengl &&
1255 frame->data[3] && frame->linesize[3] )
1260 dst = *alpha = mlt_pool_alloc( width * height );
1261 src = frame->data[3];
1263 for ( i = 0; i < height; dst += width, src += frame->linesize[3], i++ )
1264 memcpy( dst, src, FFMIN( width, frame->linesize[3] ) );
1267 if ( *format == mlt_image_yuv420p )
1269 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
1270 width, height, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
1272 output.data[0] = buffer;
1273 output.data[1] = buffer + width * height;
1274 output.data[2] = buffer + ( 5 * width * height ) / 4;
1275 output.linesize[0] = width;
1276 output.linesize[1] = width >> 1;
1277 output.linesize[2] = width >> 1;
1278 set_luma_transfer( context, colorspace, full_range );
1279 sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1280 output.data, output.linesize);
1281 sws_freeContext( context );
1283 else if ( *format == mlt_image_rgb24 )
1285 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
1286 width, height, PIX_FMT_RGB24, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
1288 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
1289 set_luma_transfer( context, colorspace, full_range );
1290 sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1291 output.data, output.linesize);
1292 sws_freeContext( context );
1294 else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
1296 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
1297 width, height, PIX_FMT_RGBA, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
1299 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
1300 set_luma_transfer( context, colorspace, full_range );
1301 sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1302 output.data, output.linesize);
1303 sws_freeContext( context );
1307 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
1308 width, height, PIX_FMT_YUYV422, flags | SWS_FULL_CHR_H_INP, NULL, NULL, NULL);
1310 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
1311 set_luma_transfer( context, colorspace, full_range );
1312 sws_scale( context, (const uint8_t* const*) frame->data, frame->linesize, 0, height,
1313 output.data, output.linesize);
1314 sws_freeContext( context );
1317 if ( *format == mlt_image_yuv420p )
1320 pict.data[0] = buffer;
1321 pict.data[1] = buffer + width * height;
1322 pict.data[2] = buffer + ( 5 * width * height ) / 4;
1323 pict.linesize[0] = width;
1324 pict.linesize[1] = width >> 1;
1325 pict.linesize[2] = width >> 1;
1326 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
1328 else if ( *format == mlt_image_rgb24 )
1331 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
1332 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
1334 else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
1337 avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
1338 img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
1343 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
1344 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
1349 /** Allocate the image buffer and set it on the frame.
1352 static int allocate_buffer( mlt_frame frame, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
1356 if ( codec_context->width == 0 || codec_context->height == 0 )
1358 *width = codec_context->width;
1359 *height = codec_context->height;
1360 size = mlt_image_format_size( *format, *width, *height, NULL );
1361 *buffer = mlt_pool_alloc( size );
1363 mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
1370 /** Get an image from a frame.
1373 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
1376 producer_avformat self = mlt_frame_pop_service( frame );
1377 mlt_producer producer = self->parent;
1379 // Get the properties from the frame
1380 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1382 // Obtain the frame number of this frame
1383 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
1385 // Get the producer properties
1386 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1388 pthread_mutex_lock( &self->video_mutex );
1390 // Fetch the video format context
1391 AVFormatContext *context = self->video_format;
1393 // Get the video stream
1394 AVStream *stream = context->streams[ self->video_index ];
1396 // Get codec context
1397 AVCodecContext *codec_context = stream->codec;
1399 uint8_t *alpha = NULL;
1400 int got_picture = 0;
1403 // Get the image cache
1404 if ( ! self->image_cache && ! mlt_properties_get_int( properties, "noimagecache" ) )
1406 self->image_cache = mlt_cache_init();
1408 if ( self->image_cache )
1410 mlt_frame original = mlt_cache_get_frame( self->image_cache, mlt_frame_get_position( frame ) );
1413 mlt_properties orig_props = MLT_FRAME_PROPERTIES( original );
1416 *buffer = mlt_properties_get_data( orig_props, "alpha", &size );
1418 mlt_frame_set_alpha( frame, *buffer, size, NULL );
1419 *buffer = mlt_properties_get_data( orig_props, "image", &size );
1420 mlt_frame_set_image( frame, *buffer, size, NULL );
1421 mlt_properties_set_data( frame_properties, "avformat.image_cache", original, 0, (mlt_destructor) mlt_frame_close, NULL );
1422 *format = mlt_properties_get_int( orig_props, "format" );
1424 // Set the resolution
1425 *width = codec_context->width;
1426 *height = codec_context->height;
1428 // Workaround 1088 encodings missing cropping info.
1429 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1433 goto exit_get_image;
1441 // We may want to use the source fps if available
1442 double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) /
1443 mlt_properties_get_double( properties, "meta.media.frame_rate_den" );
1445 // This is the physical frame position in the source
1446 int64_t req_position = ( int64_t )( position / mlt_producer_get_fps( producer ) * source_fps + 0.5 );
1448 // Determines if we have to decode all frames in a sequence
1449 // Temporary hack to improve intra frame only
1450 int must_decode = !( codec_context->codec && codec_context->codec->name ) || (
1451 strcmp( codec_context->codec->name, "dnxhd" ) &&
1452 strcmp( codec_context->codec->name, "dvvideo" ) &&
1453 strcmp( codec_context->codec->name, "huffyuv" ) &&
1454 strcmp( codec_context->codec->name, "mjpeg" ) &&
1455 strcmp( codec_context->codec->name, "rawvideo" ) );
1457 // Turn on usage of new seek API and PTS for seeking
1458 int use_pts = self->seekable &&
1459 codec_context->codec_id == CODEC_ID_H264 && codec_context->has_b_frames;
1460 if ( mlt_properties_get( properties, "use_pts" ) )
1461 use_pts = mlt_properties_get_int( properties, "use_pts" );
1462 double delay = mlt_properties_get_double( properties, "video_delay" );
1464 // Seek if necessary
1465 const char *interp = mlt_properties_get( frame_properties, "rescale.interp" );
1466 int preseek = must_decode
1467 #if defined(FFUDIV) && LIBAVFORMAT_VERSION_INT >= ((53<<16)+(24<<8)+2)
1468 && ( !use_pts || ( interp && strcmp( interp, "nearest" ) ) )
1470 && codec_context->has_b_frames;
1471 int paused = seek_video( self, position, req_position, preseek, use_pts );
1473 // Seek might have reopened the file
1474 context = self->video_format;
1475 stream = context->streams[ self->video_index ];
1476 codec_context = stream->codec;
1477 if ( *format == mlt_image_none ||
1478 codec_context->pix_fmt == PIX_FMT_ARGB ||
1479 codec_context->pix_fmt == PIX_FMT_RGBA ||
1480 codec_context->pix_fmt == PIX_FMT_ABGR ||
1481 codec_context->pix_fmt == PIX_FMT_BGRA )
1482 *format = pick_format( codec_context->pix_fmt );
1484 // Duplicate the last image if necessary
1485 if ( self->av_frame && self->av_frame->linesize[0]
1486 && ( paused || self->current_position >= req_position ) )
1489 if ( ( image_size = allocate_buffer( frame, codec_context, buffer, format, width, height ) ) )
1491 // Workaround 1088 encodings missing cropping info.
1492 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1495 if ( self->vdpau && self->vdpau->buffer )
1498 picture.data[0] = self->vdpau->buffer;
1499 picture.data[2] = self->vdpau->buffer + codec_context->width * codec_context->height;
1500 picture.data[1] = self->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1501 picture.linesize[0] = codec_context->width;
1502 picture.linesize[1] = codec_context->width / 2;
1503 picture.linesize[2] = codec_context->width / 2;
1504 convert_image( (AVFrame*) &picture, *buffer,
1505 PIX_FMT_YUV420P, format, *width, *height, self->colorspace, &alpha );
1509 convert_image( self->av_frame, *buffer, codec_context->pix_fmt,
1510 format, *width, *height, self->colorspace, &alpha );
1517 int64_t int_position = 0;
1518 int decode_errors = 0;
1520 av_init_packet( &pkt );
1522 // Construct an AVFrame for YUV422 conversion
1523 if ( !self->av_frame )
1524 self->av_frame = avcodec_alloc_frame( );
1526 while( ret >= 0 && !got_picture )
1529 pthread_mutex_lock( &self->packets_mutex );
1530 if ( mlt_deque_count( self->vpackets ) )
1532 AVPacket *tmp = (AVPacket*) mlt_deque_pop_front( self->vpackets );
1538 ret = av_read_frame( context, &pkt );
1539 if ( ret >= 0 && !self->seekable && pkt.stream_index == self->audio_index )
1541 if ( !av_dup_packet( &pkt ) )
1543 AVPacket *tmp = malloc( sizeof(AVPacket) );
1545 mlt_deque_push_back( self->apackets, tmp );
1549 pthread_mutex_unlock( &self->packets_mutex );
1551 // We only deal with video from the selected video_index
1552 if ( ret >= 0 && pkt.stream_index == self->video_index && pkt.size > 0 )
1554 // Determine time code of the packet
1555 if ( use_pts && pkt.pts == AV_NOPTS_VALUE )
1557 self->invalid_pts_counter++;
1558 if ( self->invalid_pts_counter > 20 )
1560 mlt_log_warning( MLT_PRODUCER_SERVICE(producer), "PTS invalid; using DTS instead\n" );
1561 mlt_properties_set_int( properties, "use_pts", 0 );
1567 self->invalid_pts_counter = 0;
1569 int64_t pts = ( use_pts && pkt.pts != AV_NOPTS_VALUE )? pkt.pts : pkt.dts;
1570 if ( pts != AV_NOPTS_VALUE )
1572 if ( !self->seekable && self->first_pts == AV_NOPTS_VALUE )
1573 self->first_pts = pts;
1574 if ( self->first_pts != AV_NOPTS_VALUE )
1575 pts -= self->first_pts;
1576 else if ( context->start_time != AV_NOPTS_VALUE )
1577 pts -= context->start_time;
1578 int_position = ( int64_t )( ( av_q2d( stream->time_base ) * pts + delay ) * source_fps + 0.5 );
1579 if ( int_position == self->last_position )
1580 int_position = self->last_position + 1;
1582 mlt_log_debug( MLT_PRODUCER_SERVICE(producer),
1583 "V pkt.pts %"PRId64" pkt.dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n",
1584 pkt.pts, pkt.dts, req_position, self->current_position, int_position );
1586 // Make a dumb assumption on streams that contain wild timestamps
1587 if ( abs( req_position - int_position ) > 999 )
1589 int_position = req_position;
1590 mlt_log_warning( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!\n" );
1592 self->last_position = int_position;
1595 if ( must_decode || int_position >= req_position )
1600 if ( self->vdpau->decoder == VDP_INVALID_HANDLE )
1602 vdpau_decoder_init( self );
1604 self->vdpau->is_decoded = 0;
1607 codec_context->reordered_opaque = int_position;
1608 if ( int_position >= req_position )
1609 codec_context->skip_loop_filter = AVDISCARD_NONE;
1610 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1611 ret = avcodec_decode_video2( codec_context, self->av_frame, &got_picture, &pkt );
1613 ret = avcodec_decode_video( codec_context, self->av_frame, &got_picture, pkt.data, pkt.size );
1615 // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1618 if ( ++decode_errors <= 10 )
1630 // Get position of reordered frame
1631 int_position = self->av_frame->reordered_opaque;
1633 if ( int_position < req_position )
1635 else if ( int_position >= req_position )
1636 codec_context->skip_loop_filter = AVDISCARD_NONE;
1638 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
1641 // Now handle the picture if we have one
1644 if ( ( image_size = allocate_buffer( frame, codec_context, buffer, format, width, height ) ) )
1646 // Workaround 1088 encodings missing cropping info.
1647 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1652 if ( self->vdpau->is_decoded )
1654 struct vdpau_render_state *render = (struct vdpau_render_state*) self->av_frame->data[0];
1656 uint32_t pitches[3];
1657 VdpYCbCrFormat dest_format = VDP_YCBCR_FORMAT_YV12;
1659 if ( !self->vdpau->buffer )
1660 self->vdpau->buffer = mlt_pool_alloc( codec_context->width * codec_context->height * 3 / 2 );
1661 self->av_frame->data[0] = planes[0] = self->vdpau->buffer;
1662 self->av_frame->data[2] = planes[1] = self->vdpau->buffer + codec_context->width * codec_context->height;
1663 self->av_frame->data[1] = planes[2] = self->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1664 self->av_frame->linesize[0] = pitches[0] = codec_context->width;
1665 self->av_frame->linesize[1] = pitches[1] = codec_context->width / 2;
1666 self->av_frame->linesize[2] = pitches[2] = codec_context->width / 2;
1668 VdpStatus status = vdp_surface_get_bits( render->surface, dest_format, planes, pitches );
1669 if ( status == VDP_STATUS_OK )
1671 convert_image( self->av_frame, *buffer, PIX_FMT_YUV420P,
1672 format, *width, *height, self->colorspace, &alpha );
1676 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU Error: %s\n", vdp_get_error_string( status ) );
1677 image_size = self->vdpau->is_decoded = 0;
1682 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU error in VdpDecoderRender\n" );
1683 image_size = got_picture = 0;
1688 convert_image( self->av_frame, *buffer, codec_context->pix_fmt,
1689 format, *width, *height, self->colorspace, &alpha );
1690 self->top_field_first |= self->av_frame->top_field_first;
1691 self->current_position = int_position;
1698 if ( self->seekable || pkt.stream_index != self->audio_index )
1699 av_free_packet( &pkt );
1705 mlt_frame_set_alpha( frame, alpha, (*width) * (*height), mlt_pool_release );
1707 if ( image_size > 0 && self->image_cache )
1709 mlt_properties_set_int( frame_properties, "format", *format );
1710 mlt_cache_put_frame( self->image_cache, frame );
1713 // Try to duplicate last image if there was a decoding failure
1714 // TODO: with multithread decoding a partial frame decoding resulting
1715 // in failure also resets av_frame making test below fail.
1716 if ( !image_size && self->av_frame && self->av_frame->linesize[0] )
1719 if ( ( image_size = allocate_buffer( frame, codec_context, buffer, format, width, height ) ) )
1721 // Workaround 1088 encodings missing cropping info.
1722 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1725 if ( self->vdpau && self->vdpau->buffer )
1728 picture.data[0] = self->vdpau->buffer;
1729 picture.data[2] = self->vdpau->buffer + codec_context->width * codec_context->height;
1730 picture.data[1] = self->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1731 picture.linesize[0] = codec_context->width;
1732 picture.linesize[1] = codec_context->width / 2;
1733 picture.linesize[2] = codec_context->width / 2;
1734 convert_image( (AVFrame*) &picture, *buffer,
1735 PIX_FMT_YUV420P, format, *width, *height, self->colorspace, &alpha );
1739 convert_image( self->av_frame, *buffer, codec_context->pix_fmt,
1740 format, *width, *height, self->colorspace, &alpha );
1745 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
1746 self->video_expected = position + 1;
1750 pthread_mutex_unlock( &self->video_mutex );
1752 // Set the progressive flag
1753 if ( mlt_properties_get( properties, "force_progressive" ) )
1754 mlt_properties_set_int( frame_properties, "progressive", !!mlt_properties_get_int( properties, "force_progressive" ) );
1755 else if ( self->av_frame )
1756 mlt_properties_set_int( frame_properties, "progressive", !self->av_frame->interlaced_frame );
1758 // Set the field order property for this frame
1759 if ( mlt_properties_get( properties, "force_tff" ) )
1760 mlt_properties_set_int( frame_properties, "top_field_first", !!mlt_properties_get_int( properties, "force_tff" ) );
1762 mlt_properties_set_int( frame_properties, "top_field_first", self->top_field_first );
1764 // Set immutable properties of the selected track's (or overridden) source attributes.
1765 mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
1766 mlt_properties_set_int( properties, "meta.media.top_field_first", self->top_field_first );
1767 mlt_properties_set_int( properties, "meta.media.progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
1768 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
1770 return !got_picture;
1773 /** Process properties as AVOptions and apply to AV context obj
1776 static void apply_properties( void *obj, mlt_properties properties, int flags )
1779 int count = mlt_properties_count( properties );
1780 for ( i = 0; i < count; i++ )
1782 const char *opt_name = mlt_properties_get_name( properties, i );
1783 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(10<<8)+0)
1784 const AVOption *opt = av_opt_find( obj, opt_name, NULL, flags, flags );
1786 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
1788 if ( opt_name && mlt_properties_get( properties, opt_name ) )
1791 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(12<<8)+0)
1792 av_opt_set( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1793 #elif LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
1794 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
1795 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
1796 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1798 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
1804 /** Initialize the video codec context.
1807 static int video_codec_init( producer_avformat self, int index, mlt_properties properties )
1809 // Initialise the codec if necessary
1810 if ( !self->video_codec )
1812 // Get the video stream
1813 AVStream *stream = self->video_format->streams[ index ];
1815 // Get codec context
1816 AVCodecContext *codec_context = stream->codec;
1819 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1821 if ( codec_context->codec_id == CODEC_ID_H264 )
1823 if ( ( codec = avcodec_find_decoder_by_name( "h264_vdpau" ) ) )
1825 if ( vdpau_init( self ) )
1827 self->video_codec = codec_context;
1828 if ( !vdpau_decoder_init( self ) )
1833 codec = avcodec_find_decoder( codec_context->codec_id );
1837 // Initialise multi-threading
1838 int thread_count = mlt_properties_get_int( properties, "threads" );
1839 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1840 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1841 if ( thread_count > 1 )
1842 codec_context->thread_count = thread_count;
1844 // If we don't have a codec and we can't initialise it, we can't do much more...
1845 pthread_mutex_lock( &self->open_mutex );
1846 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
1847 if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
1849 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1852 // Now store the codec with its destructor
1853 self->video_codec = codec_context;
1857 // Remember that we can't use this later
1858 self->video_index = -1;
1859 pthread_mutex_unlock( &self->open_mutex );
1862 pthread_mutex_unlock( &self->open_mutex );
1864 // Process properties as AVOptions
1865 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1866 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(122<<8)+0)
1867 if ( codec->priv_class && codec_context->priv_data )
1868 apply_properties( codec_context->priv_data, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1871 // Reset some image properties
1872 mlt_properties_set_int( properties, "width", self->video_codec->width );
1873 mlt_properties_set_int( properties, "height", self->video_codec->height );
1874 // For DV, we'll just use the saved aspect ratio
1875 if ( codec_context->codec_id != CODEC_ID_DVVIDEO )
1876 get_aspect_ratio( properties, stream, self->video_codec, NULL );
1878 // Determine the fps first from the codec
1879 double source_fps = (double) self->video_codec->time_base.den /
1880 ( self->video_codec->time_base.num == 0 ? 1 : self->video_codec->time_base.num );
1882 if ( mlt_properties_get( properties, "force_fps" ) )
1884 source_fps = mlt_properties_get_double( properties, "force_fps" );
1885 stream->time_base = av_d2q( source_fps, 1024 );
1886 mlt_properties_set_int( properties, "meta.media.frame_rate_num", stream->time_base.num );
1887 mlt_properties_set_int( properties, "meta.media.frame_rate_den", stream->time_base.den );
1891 // If the muxer reports a frame rate different than the codec
1892 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1893 double muxer_fps = av_q2d( stream->avg_frame_rate );
1894 if ( isnan( muxer_fps ) || muxer_fps == 0 )
1895 muxer_fps = av_q2d( stream->r_frame_rate );
1897 double muxer_fps = av_q2d( stream->r_frame_rate );
1899 // Choose the lesser - the wrong tends to be off by some multiple of 10
1900 source_fps = FFMIN( source_fps, muxer_fps );
1901 if ( source_fps >= 1.0 && ( source_fps < muxer_fps || isnan( muxer_fps ) ) )
1903 mlt_properties_set_int( properties, "meta.media.frame_rate_num", self->video_codec->time_base.den );
1904 mlt_properties_set_int( properties, "meta.media.frame_rate_den", self->video_codec->time_base.num == 0 ? 1 : self->video_codec->time_base.num );
1906 else if ( muxer_fps > 0 )
1908 AVRational frame_rate = stream->r_frame_rate;
1909 // With my samples when r_frame_rate != 1000 but avg_frame_rate is valid,
1910 // avg_frame_rate gives some approximate value that does not well match the media.
1911 // Also, on my sample where r_frame_rate = 1000, using avg_frame_rate directly
1912 // results in some very choppy output, but some value slightly different works
1914 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1915 if ( av_q2d( stream->r_frame_rate ) >= 1000 && av_q2d( stream->avg_frame_rate ) > 0 )
1916 frame_rate = av_d2q( av_q2d( stream->avg_frame_rate ), 1024 );
1918 mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
1919 mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
1923 source_fps = mlt_producer_get_fps( self->parent );
1924 AVRational frame_rate = av_d2q( source_fps, 255 );
1925 mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
1926 mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
1930 // Set the YUV colorspace from override or detect
1931 self->colorspace = mlt_properties_get_int( properties, "force_colorspace" );
1932 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
1933 if ( ! self->colorspace )
1935 switch ( self->video_codec->colorspace )
1937 case AVCOL_SPC_SMPTE240M:
1938 self->colorspace = 240;
1940 case AVCOL_SPC_BT470BG:
1941 case AVCOL_SPC_SMPTE170M:
1942 self->colorspace = 601;
1944 case AVCOL_SPC_BT709:
1945 self->colorspace = 709;
1948 // This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
1949 self->colorspace = self->video_codec->width * self->video_codec->height > 750000 ? 709 : 601;
1954 // Let apps get chosen colorspace
1955 mlt_properties_set_int( properties, "meta.media.colorspace", self->colorspace );
1957 return self->video_codec && self->video_index > -1;
1960 /** Set up video handling.
1963 static void producer_set_up_video( producer_avformat self, mlt_frame frame )
1966 mlt_producer producer = self->parent;
1968 // Get the properties
1969 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1971 // Fetch the video format context
1972 AVFormatContext *context = self->video_format;
1974 // Get the video_index
1975 int index = mlt_properties_get_int( properties, "video_index" );
1977 // Reopen the file if necessary
1978 if ( !context && index > -1 )
1980 producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1981 mlt_properties_get( properties, "resource" ), 1 );
1982 context = self->video_format;
1985 // Exception handling for video_index
1986 if ( context && index >= (int) context->nb_streams )
1988 // Get the last video stream
1989 for ( index = context->nb_streams - 1;
1990 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO;
1992 mlt_properties_set_int( properties, "video_index", index );
1994 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
1996 // Invalidate the video stream
1998 mlt_properties_set_int( properties, "video_index", index );
2001 // Update the video properties if the index changed
2002 if ( index != self->video_index )
2004 // Reset the video properties if the index changed
2005 self->video_index = index;
2006 pthread_mutex_lock( &self->open_mutex );
2007 if ( self->video_codec )
2008 avcodec_close( self->video_codec );
2009 self->video_codec = NULL;
2010 pthread_mutex_unlock( &self->open_mutex );
2013 // Get the frame properties
2014 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
2017 if ( context && index > -1 && video_codec_init( self, index, properties ) )
2019 // Set the frame properties
2020 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
2021 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
2022 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
2024 // Set the width and height
2025 mlt_properties_set_int( frame_properties, "width", self->video_codec->width );
2026 mlt_properties_set_int( frame_properties, "height", self->video_codec->height );
2027 mlt_properties_set_int( properties, "meta.media.width", self->video_codec->width );
2028 mlt_properties_set_int( properties, "meta.media.height", self->video_codec->height );
2029 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
2030 mlt_properties_set_int( frame_properties, "colorspace", self->colorspace );
2032 // Workaround 1088 encodings missing cropping info.
2033 if ( self->video_codec->height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
2035 mlt_properties_set_int( properties, "meta.media.height", 1080 );
2038 // Add our image operation
2039 mlt_frame_push_service( frame, self );
2040 mlt_frame_push_get_image( frame, producer_get_image );
2044 // If something failed, use test card image
2045 mlt_properties_set_int( frame_properties, "test_image", 1 );
2049 static int seek_audio( producer_avformat self, mlt_position position, double timecode )
2053 // Seek if necessary
2054 if ( self->seekable && position != self->audio_expected )
2056 if ( position + 1 == self->audio_expected )
2058 // We're paused - silence required
2061 else if ( position < self->audio_expected || position - self->audio_expected >= 12 )
2063 AVFormatContext *context = self->audio_format;
2064 int64_t timestamp = ( int64_t )( timecode * AV_TIME_BASE + 0.5 );
2065 if ( context->start_time != AV_NOPTS_VALUE )
2066 timestamp += context->start_time;
2067 if ( timestamp < 0 )
2070 // Set to the real timecode
2071 if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
2074 // Clear the usage in the audio buffer
2075 int i = MAX_AUDIO_STREAMS + 1;
2077 self->audio_used[i - 1] = 0;
2083 static int sample_bytes( AVCodecContext *context )
2085 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
2086 return av_get_bytes_per_sample( context->sample_fmt );
2087 #elif LIBAVCODEC_VERSION_MAJOR >= 53
2088 return av_get_bits_per_sample_fmt( context->sample_fmt ) / 8;
2090 return av_get_bits_per_sample_format( context->sample_fmt ) / 8;
2094 static int decode_audio( producer_avformat self, int *ignore, AVPacket pkt, int channels, int samples, double timecode, double fps )
2096 // Fetch the audio_format
2097 AVFormatContext *context = self->audio_format;
2099 // Get the current stream index
2100 int index = pkt.stream_index;
2102 // Get codec context
2103 AVCodecContext *codec_context = self->audio_codec[ index ];
2105 // Obtain the resample context if it exists (not always needed)
2106 ReSampleContext *resample = self->audio_resample[ index ];
2108 // Obtain the audio buffers
2109 uint8_t *audio_buffer = self->audio_buffer[ index ];
2110 uint8_t *decode_buffer = self->decode_buffer[ index ];
2112 int audio_used = self->audio_used[ index ];
2113 uint8_t *ptr = pkt.data;
2117 while ( ptr && ret >= 0 && len > 0 )
2119 int sizeof_sample = resample? sizeof( int16_t ) : sample_bytes( codec_context );
2120 int data_size = self->audio_buffer_size[ index ];
2123 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
2124 ret = avcodec_decode_audio3( codec_context, (int16_t*) decode_buffer, &data_size, &pkt );
2125 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
2126 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
2128 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
2132 mlt_log_warning( MLT_PRODUCER_SERVICE(self->parent), "audio decoding error %d\n", ret );
2136 pkt.size = len -= ret;
2137 pkt.data = ptr += ret;
2139 // If decoded successfully
2140 if ( data_size > 0 )
2142 // Figure out how many samples will be needed after resampling
2143 int convert_samples = data_size / codec_context->channels / sample_bytes( codec_context );
2144 int samples_needed = self->resample_factor * convert_samples;
2146 // Resize audio buffer to prevent overflow
2147 if ( ( audio_used + samples_needed ) * channels * sizeof_sample > self->audio_buffer_size[ index ] )
2149 self->audio_buffer_size[ index ] = ( audio_used + samples_needed * 2 ) * channels * sizeof_sample;
2150 audio_buffer = self->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, self->audio_buffer_size[ index ] );
2154 // Copy to audio buffer while resampling
2155 uint8_t *source = decode_buffer;
2156 uint8_t *dest = &audio_buffer[ audio_used * channels * sizeof_sample ];
2157 audio_used += audio_resample( resample, (short*) dest, (short*) source, convert_samples );
2161 // Straight copy to audio buffer
2162 memcpy( &audio_buffer[ audio_used * codec_context->channels * sizeof_sample ], decode_buffer, data_size );
2163 audio_used += convert_samples;
2167 while ( *ignore && audio_used )
2170 audio_used -= audio_used > samples ? samples : audio_used;
2171 memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) * sizeof_sample ],
2172 audio_used * sizeof_sample );
2177 // If we're behind, ignore this packet
2178 // Skip this on non-seekable, audio-only inputs.
2179 if ( pkt.pts >= 0 && ( self->seekable || self->video_format ) && *ignore == 0 && audio_used > samples / 2 )
2181 int64_t pts = pkt.pts;
2182 if ( self->first_pts != 0 )
2183 pts -= self->first_pts;
2184 else if ( context->start_time != AV_NOPTS_VALUE )
2185 pts -= context->start_time;
2186 double timebase = av_q2d( context->streams[ index ]->time_base );
2187 int64_t int_position = ( int64_t )( timebase * pts * fps + 0.5 );
2188 int64_t req_position = ( int64_t )( timecode * fps + 0.5 );
2190 mlt_log_debug( MLT_PRODUCER_SERVICE(self->parent),
2191 "A pkt.pts %"PRId64" pkt.dts %"PRId64" req_pos %"PRId64" cur_pos %"PRId64" pkt_pos %"PRId64"\n",
2192 pkt.pts, pkt.dts, req_position, self->current_position, int_position );
2194 if ( int_position > 0 )
2196 if ( int_position < req_position )
2197 // We are behind, so skip some
2198 *ignore = req_position - int_position;
2199 else if ( self->audio_index != INT_MAX && int_position > req_position + 2 )
2200 // We are ahead, so seek backwards some more
2201 seek_audio( self, req_position, timecode - 1.0 );
2205 self->audio_used[ index ] = audio_used;
2210 /** Get the audio from a frame.
2212 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
2215 producer_avformat self = mlt_frame_pop_audio( frame );
2217 pthread_mutex_lock( &self->audio_mutex );
2219 // Obtain the frame number of this frame
2220 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" );
2222 // Calculate the real time code
2223 double real_timecode = producer_time_of_frame( self->parent, position );
2225 // Get the producer fps
2226 double fps = mlt_producer_get_fps( self->parent );
2228 // Number of frames to ignore (for ffwd)
2229 int ignore[ MAX_AUDIO_STREAMS ] = { 0 };
2231 // Flag for paused (silence)
2232 int paused = seek_audio( self, position, real_timecode );
2234 // Initialize ignore for all streams from the seek return value
2235 int i = MAX_AUDIO_STREAMS;
2237 ignore[i] = ignore[0];
2239 // Fetch the audio_format
2240 AVFormatContext *context = self->audio_format;
2242 int sizeof_sample = sizeof( int16_t );
2244 // Determine the tracks to use
2245 int index = self->audio_index;
2246 int index_max = self->audio_index + 1;
2247 if ( self->audio_index == INT_MAX )
2250 index_max = context->nb_streams;
2251 *channels = self->total_channels;
2252 *samples = *samples * FFMAX( self->max_frequency, *frequency ) / *frequency;
2253 *frequency = FFMAX( self->max_frequency, *frequency );
2256 // Initialize the resamplers and buffers
2257 for ( ; index < index_max; index++ )
2259 // Get codec context
2260 AVCodecContext *codec_context = self->audio_codec[ index ];
2262 if ( codec_context && !self->audio_buffer[ index ] )
2264 // Check for resample and create if necessary
2265 if ( codec_context->channels <= 2 )
2267 // Determine by how much resampling will increase number of samples
2268 double resample_factor = self->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
2269 resample_factor *= (double) *frequency / codec_context->sample_rate;
2270 if ( resample_factor > self->resample_factor )
2271 self->resample_factor = resample_factor;
2273 // Create the resampler
2274 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
2275 self->audio_resample[ index ] = av_audio_resample_init(
2276 self->audio_index == INT_MAX ? codec_context->channels : *channels,
2277 codec_context->channels, *frequency, codec_context->sample_rate,
2278 AV_SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
2280 self->audio_resample[ index ] = audio_resample_init(
2281 self->audio_index == INT_MAX ? codec_context->channels : *channels,
2282 codec_context->channels, *frequency, codec_context->sample_rate );
2287 codec_context->request_channels = self->audio_index == INT_MAX ? codec_context->channels : *channels;
2288 sizeof_sample = sample_bytes( codec_context );
2291 // Check for audio buffer and create if necessary
2292 self->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof_sample;
2293 self->audio_buffer[ index ] = mlt_pool_alloc( self->audio_buffer_size[ index ] );
2295 // Check for decoder buffer and create if necessary
2296 self->decode_buffer[ index ] = av_malloc( self->audio_buffer_size[ index ] );
2300 // Get the audio if required
2307 av_init_packet( &pkt );
2309 // If not resampling, give consumer more than requested.
2310 // It requested number samples based on requested frame rate.
2311 // Do not clean this up with a samples *= ...!
2312 if ( self->audio_index != INT_MAX && ! self->audio_resample[ self->audio_index ] )
2313 *samples = *samples * self->audio_codec[ self->audio_index ]->sample_rate / *frequency;
2315 while ( ret >= 0 && !got_audio )
2317 // Check if the buffer already contains the samples required
2318 if ( self->audio_index != INT_MAX &&
2319 self->audio_used[ self->audio_index ] >= *samples &&
2320 ignore[ self->audio_index ] == 0 )
2325 else if ( self->audio_index == INT_MAX )
2327 // Check if there is enough audio for all streams
2329 for ( index = 0; got_audio && index < context->nb_streams; index++ )
2330 if ( ( self->audio_codec[ index ] && self->audio_used[ index ] < *samples ) || ignore[ index ] )
2337 pthread_mutex_lock( &self->packets_mutex );
2338 if ( mlt_deque_count( self->apackets ) )
2340 AVPacket *tmp = (AVPacket*) mlt_deque_pop_front( self->apackets );
2346 ret = av_read_frame( context, &pkt );
2347 if ( ret >= 0 && !self->seekable && pkt.stream_index == self->video_index )
2349 if ( !av_dup_packet( &pkt ) )
2351 AVPacket *tmp = malloc( sizeof(AVPacket) );
2353 mlt_deque_push_back( self->vpackets, tmp );
2357 pthread_mutex_unlock( &self->packets_mutex );
2359 // We only deal with audio from the selected audio index
2360 index = pkt.stream_index;
2361 if ( ret >= 0 && pkt.data && pkt.size > 0 && ( index == self->audio_index ||
2362 ( self->audio_index == INT_MAX && context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
2364 int channels2 = ( self->audio_index == INT_MAX || !self->audio_resample[index] ) ?
2365 self->audio_codec[index]->channels : *channels;
2366 ret = decode_audio( self, &ignore[index], pkt, channels2, *samples, real_timecode, fps );
2369 if ( self->seekable || index != self->video_index )
2370 av_free_packet( &pkt );
2374 // Set some additional return values
2375 *format = mlt_audio_s16;
2376 if ( self->audio_index != INT_MAX && !self->audio_resample[ self->audio_index ] )
2378 index = self->audio_index;
2379 *channels = self->audio_codec[ index ]->channels;
2380 *frequency = self->audio_codec[ index ]->sample_rate;
2381 *format = self->audio_codec[ index ]->sample_fmt == AV_SAMPLE_FMT_S32 ? mlt_audio_s32le
2382 : self->audio_codec[ index ]->sample_fmt == AV_SAMPLE_FMT_FLT ? mlt_audio_f32le
2384 sizeof_sample = sample_bytes( self->audio_codec[ index ] );
2386 else if ( self->audio_index == INT_MAX )
2388 // This only works if all audio tracks have the same sample format.
2389 for ( index = 0; index < index_max; index++ )
2390 if ( self->audio_codec[ index ] && !self->audio_resample[ index ] )
2392 *format = self->audio_codec[ index ]->sample_fmt == AV_SAMPLE_FMT_S32 ? mlt_audio_s32le
2393 : self->audio_codec[ index ]->sample_fmt == AV_SAMPLE_FMT_FLT ? mlt_audio_f32le
2395 sizeof_sample = sample_bytes( self->audio_codec[ index ] );
2400 // Allocate and set the frame's audio buffer
2401 int size = mlt_audio_format_size( *format, *samples, *channels );
2402 *buffer = mlt_pool_alloc( size );
2403 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
2405 // Interleave tracks if audio_index=all
2406 if ( self->audio_index == INT_MAX )
2408 uint8_t *dest = *buffer;
2410 for ( i = 0; i < *samples; i++ )
2412 for ( index = 0; index < index_max; index++ )
2413 if ( self->audio_codec[ index ] )
2415 int current_channels = self->audio_codec[ index ]->channels;
2416 uint8_t *src = self->audio_buffer[ index ] + i * current_channels * sizeof_sample;
2417 memcpy( dest, src, current_channels * sizeof_sample );
2418 dest += current_channels * sizeof_sample;
2421 for ( index = 0; index < index_max; index++ )
2422 if ( self->audio_codec[ index ] && self->audio_used[ index ] >= *samples )
2424 int current_channels = self->audio_codec[ index ]->channels;
2425 uint8_t *src = self->audio_buffer[ index ] + *samples * current_channels * sizeof_sample;
2426 self->audio_used[index] -= *samples;
2427 memmove( self->audio_buffer[ index ], src, self->audio_used[ index ] * current_channels * sizeof_sample );
2430 // Copy a single track to the output buffer
2433 index = self->audio_index;
2435 // Now handle the audio if we have enough
2436 if ( self->audio_used[ index ] > 0 )
2438 uint8_t *src = self->audio_buffer[ index ];
2439 // copy samples from audio_buffer
2440 size = self->audio_used[ index ] < *samples ? self->audio_used[ index ] : *samples;
2441 memcpy( *buffer, src, size * *channels * sizeof_sample );
2442 // supply the remaining requested samples as silence
2443 if ( *samples > self->audio_used[ index ] )
2444 memset( *buffer + size * *channels * sizeof_sample, 0, ( *samples - self->audio_used[ index ] ) * *channels * sizeof_sample );
2445 // reposition the samples within audio_buffer
2446 self->audio_used[ index ] -= size;
2447 memmove( src, src + size * *channels * sizeof_sample, self->audio_used[ index ] * *channels * sizeof_sample );
2451 // Otherwise fill with silence
2452 memset( *buffer, 0, *samples * *channels * sizeof_sample );
2458 // Get silence and don't touch the context
2459 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
2462 // Regardless of speed (other than paused), we expect to get the next frame
2464 self->audio_expected = position + 1;
2466 pthread_mutex_unlock( &self->audio_mutex );
2471 /** Initialize the audio codec context.
2474 static int audio_codec_init( producer_avformat self, int index, mlt_properties properties )
2476 // Initialise the codec if necessary
2477 if ( !self->audio_codec[ index ] )
2479 // Get codec context
2480 AVCodecContext *codec_context = self->audio_format->streams[index]->codec;
2483 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
2485 // If we don't have a codec and we can't initialise it, we can't do much more...
2486 pthread_mutex_lock( &self->open_mutex );
2487 #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
2488 if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
2490 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
2493 // Now store the codec with its destructor
2494 if ( self->audio_codec[ index ] )
2495 avcodec_close( self->audio_codec[ index ] );
2496 self->audio_codec[ index ] = codec_context;
2500 // Remember that we can't use self later
2501 self->audio_index = -1;
2503 pthread_mutex_unlock( &self->open_mutex );
2505 // Process properties as AVOptions
2506 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2507 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(122<<8)+0)
2508 if ( codec && codec->priv_class && codec_context->priv_data )
2509 apply_properties( codec_context->priv_data, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2512 return self->audio_codec[ index ] && self->audio_index > -1;
2515 /** Set up audio handling.
2518 static void producer_set_up_audio( producer_avformat self, mlt_frame frame )
2521 mlt_producer producer = self->parent;
2523 // Get the properties
2524 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
2526 // Fetch the audio format context
2527 AVFormatContext *context = self->audio_format;
2529 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
2531 // Get the audio_index
2532 int index = mlt_properties_get_int( properties, "audio_index" );
2534 // Handle all audio tracks
2535 if ( self->audio_index > -1 &&
2536 mlt_properties_get( properties, "audio_index" ) &&
2537 !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
2540 // Reopen the file if necessary
2541 if ( !context && self->audio_index > -1 && index > -1 )
2543 producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
2544 mlt_properties_get( properties, "resource" ), 1 );
2545 context = self->audio_format;
2548 // Exception handling for audio_index
2549 if ( context && index >= (int) context->nb_streams && index < INT_MAX )
2551 for ( index = context->nb_streams - 1;
2552 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO;
2554 mlt_properties_set_int( properties, "audio_index", index );
2556 if ( context && index > -1 && index < INT_MAX &&
2557 context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
2559 index = self->audio_index;
2560 mlt_properties_set_int( properties, "audio_index", index );
2563 // Update the audio properties if the index changed
2564 if ( context && index > -1 && index != self->audio_index )
2566 pthread_mutex_lock( &self->open_mutex );
2567 if ( self->audio_codec[ self->audio_index ] )
2568 avcodec_close( self->audio_codec[ self->audio_index ] );
2569 self->audio_codec[ self->audio_index ] = NULL;
2570 pthread_mutex_unlock( &self->open_mutex );
2572 if ( self->audio_index != -1 )
2573 self->audio_index = index;
2578 if ( context && index == INT_MAX )
2580 mlt_properties_set_int( frame_properties, "audio_frequency", self->max_frequency );
2581 mlt_properties_set_int( frame_properties, "audio_channels", self->total_channels );
2582 for ( index = 0; index < context->nb_streams; index++ )
2584 if ( context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO )
2585 audio_codec_init( self, index, properties );
2588 else if ( context && index > -1 && audio_codec_init( self, index, properties ) )
2590 // Set the frame properties
2591 if ( index < INT_MAX )
2593 mlt_properties_set_int( frame_properties, "frequency", self->audio_codec[ index ]->sample_rate );
2594 mlt_properties_set_int( frame_properties, "channels", self->audio_codec[ index ]->channels );
2597 if ( context && index > -1 )
2599 // Add our audio operation
2600 mlt_frame_push_audio( frame, self );
2601 mlt_frame_push_audio( frame, producer_get_audio );
2605 /** Our get frame implementation.
2608 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
2610 // Access the private data
2611 mlt_service service = MLT_PRODUCER_SERVICE( producer );
2612 mlt_cache_item cache_item = mlt_service_cache_get( service, "producer_avformat" );
2613 producer_avformat self = mlt_cache_item_data( cache_item, NULL );
2618 self = calloc( 1, sizeof( struct producer_avformat_s ) );
2619 producer->child = self;
2620 self->parent = producer;
2621 mlt_service_cache_put( service, "producer_avformat", self, 0, (mlt_destructor) producer_avformat_close );
2622 cache_item = mlt_service_cache_get( service, "producer_avformat" );
2625 // Create an empty frame
2626 *frame = mlt_frame_init( service);
2630 mlt_properties_set_data( MLT_FRAME_PROPERTIES(*frame), "avformat_cache", cache_item, 0, (mlt_destructor) mlt_cache_item_close, NULL );
2634 mlt_cache_item_close( cache_item );
2638 // Update timecode on the frame we're creating
2639 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
2642 producer_set_up_video( self, *frame );
2645 producer_set_up_audio( self, *frame );
2647 // Set the position of this producer
2648 mlt_position position = self->seekable ? mlt_producer_frame( producer ) : self->nonseek_position++;
2649 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", position );
2651 // Calculate the next timecode
2652 mlt_producer_prepare_next( producer );
2657 static void producer_avformat_close( producer_avformat self )
2659 mlt_log_debug( NULL, "producer_avformat_close\n" );
2661 // Cleanup av contexts
2662 av_free( self->av_frame );
2663 pthread_mutex_lock( &self->open_mutex );
2665 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
2667 if ( self->audio_resample[i] )
2668 audio_resample_close( self->audio_resample[i] );
2669 mlt_pool_release( self->audio_buffer[i] );
2670 av_free( self->decode_buffer[i] );
2671 if ( self->audio_codec[i] )
2672 avcodec_close( self->audio_codec[i] );
2673 self->audio_codec[i] = NULL;
2675 if ( self->video_codec )
2676 avcodec_close( self->video_codec );
2677 self->video_codec = NULL;
2679 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
2680 if ( self->dummy_context )
2681 avformat_close_input( &self->dummy_context );
2682 if ( self->seekable && self->audio_format )
2683 avformat_close_input( &self->audio_format );
2684 if ( self->video_format )
2685 avformat_close_input( &self->video_format );
2687 if ( self->dummy_context )
2688 av_close_input_file( self->dummy_context );
2689 if ( self->seekable && self->audio_format )
2690 av_close_input_file( self->audio_format );
2691 if ( self->video_format )
2692 av_close_input_file( self->video_format );
2694 pthread_mutex_unlock( &self->open_mutex );
2696 vdpau_producer_close( self );
2698 if ( self->image_cache )
2699 mlt_cache_close( self->image_cache );
2701 // Cleanup the mutexes
2702 pthread_mutex_destroy( &self->audio_mutex );
2703 pthread_mutex_destroy( &self->video_mutex );
2704 pthread_mutex_destroy( &self->packets_mutex );
2705 pthread_mutex_destroy( &self->open_mutex );
2707 // Cleanup the packet queues
2709 if ( self->apackets )
2711 while ( ( pkt = mlt_deque_pop_back( self->apackets ) ) )
2713 av_free_packet( pkt );
2716 mlt_deque_close( self->apackets );
2717 self->apackets = NULL;
2719 if ( self->vpackets )
2721 while ( ( pkt = mlt_deque_pop_back( self->vpackets ) ) )
2723 av_free_packet( pkt );
2726 mlt_deque_close( self->vpackets );
2727 self->vpackets = NULL;
2733 static void producer_close( mlt_producer parent )
2735 // Remove this instance from the cache
2736 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
2739 parent->close = NULL;
2740 mlt_producer_close( parent );