2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2009 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>
34 #include <libavcodec/opt.h>
36 # include <libswscale/swscale.h>
38 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
39 # include "audioconvert.h"
42 #include <libavcodec/vdpau.h>
45 // System header files
51 #if LIBAVUTIL_VERSION_INT < (50<<16)
52 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
53 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
56 #define POSITION_INITIAL (-2)
57 #define POSITION_INVALID (-1)
59 #define MAX_AUDIO_STREAMS (10)
60 #define MAX_VDPAU_SURFACES (10)
62 void avformat_lock( );
63 void avformat_unlock( );
65 struct producer_avformat_s
68 AVFormatContext *dummy_context;
69 AVFormatContext *audio_format;
70 AVFormatContext *video_format;
71 AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ];
72 AVCodecContext *video_codec;
74 ReSampleContext *audio_resample[ MAX_AUDIO_STREAMS ];
75 mlt_position audio_expected;
76 mlt_position video_expected;
86 int16_t *audio_buffer[ MAX_AUDIO_STREAMS ];
87 size_t audio_buffer_size[ MAX_AUDIO_STREAMS ];
88 int16_t *decode_buffer[ MAX_AUDIO_STREAMS ];
89 int audio_used[ MAX_AUDIO_STREAMS ];
95 unsigned int invalid_pts_counter;
96 double resample_factor;
97 mlt_cache image_cache;
103 struct vdpau_render_state render_states[MAX_VDPAU_SURFACES];
114 typedef struct producer_avformat_s *producer_avformat;
116 // Forward references.
117 static int producer_open( producer_avformat this, mlt_profile profile, char *file );
118 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
119 static void producer_avformat_close( producer_avformat );
120 static void producer_close( mlt_producer parent );
126 /** Constructor for libavformat.
129 mlt_producer producer_avformat_init( mlt_profile profile, const char *service, char *file )
133 // Report information about available demuxers and codecs as YAML Tiny
134 if ( file && strstr( file, "f-list" ) )
136 fprintf( stderr, "---\nformats:\n" );
137 AVInputFormat *format = NULL;
138 while ( ( format = av_iformat_next( format ) ) )
139 fprintf( stderr, " - %s\n", format->name );
140 fprintf( stderr, "...\n" );
143 if ( file && strstr( file, "acodec-list" ) )
145 fprintf( stderr, "---\naudio_codecs:\n" );
146 AVCodec *codec = NULL;
147 while ( ( codec = av_codec_next( codec ) ) )
148 if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
149 fprintf( stderr, " - %s\n", codec->name );
150 fprintf( stderr, "...\n" );
153 if ( file && strstr( file, "vcodec-list" ) )
155 fprintf( stderr, "---\nvideo_codecs:\n" );
156 AVCodec *codec = NULL;
157 while ( ( codec = av_codec_next( codec ) ) )
158 if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
159 fprintf( stderr, " - %s\n", codec->name );
160 fprintf( stderr, "...\n" );
164 // Check that we have a non-NULL argument
167 // Construct the producer
168 mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
169 producer_avformat this = calloc( 1, sizeof( struct producer_avformat_s ) );
172 if ( mlt_producer_init( producer, this ) == 0 )
174 this->parent = producer;
176 // Get the properties
177 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
179 // Set the resource property (required for all producers)
180 mlt_properties_set( properties, "resource", file );
182 // Register transport implementation with the producer
183 producer->close = (mlt_destructor) producer_close;
185 // Register our get_frame implementation
186 producer->get_frame = producer_get_frame;
188 if ( strcmp( service, "avformat-novalidate" ) )
191 if ( producer_open( this, profile, file ) != 0 )
194 mlt_producer_close( producer );
199 // Close the file to release resources for large playlists - reopen later as needed
201 if ( this->dummy_context )
202 av_close_input_file( this->dummy_context );
203 this->dummy_context = NULL;
204 if ( this->audio_format )
205 av_close_input_file( this->audio_format );
206 this->audio_format = NULL;
207 if ( this->video_format )
208 av_close_input_file( this->video_format );
209 this->video_format = NULL;
212 // Default the user-selectable indices from the auto-detected indices
213 mlt_properties_set_int( properties, "audio_index", this->audio_index );
214 mlt_properties_set_int( properties, "video_index", this->video_index );
217 mlt_service_cache_set_size( MLT_PRODUCER_SERVICE(producer), "producer_avformat", 5 );
219 mlt_service_cache_put( MLT_PRODUCER_SERVICE(producer), "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
225 mlt_service_cache_set_size( MLT_PRODUCER_SERVICE(producer), "producer_avformat", 5 );
227 mlt_service_cache_put( MLT_PRODUCER_SERVICE(producer), "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
235 /** Find the default streams.
238 static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatContext *context, int *audio_index, int *video_index )
243 mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
245 // Allow for multiple audio and video streams in the file and select first of each (if available)
246 for( i = 0; i < context->nb_streams; i++ )
248 // Get the codec context
249 AVStream *stream = context->streams[ i ];
250 if ( ! stream ) continue;
251 AVCodecContext *codec_context = stream->codec;
252 if ( ! codec_context ) continue;
253 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
254 if ( ! codec ) continue;
256 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
258 // Determine the type and obtain the first index of each type
259 switch( codec_context->codec_type )
261 case CODEC_TYPE_VIDEO:
262 if ( *video_index < 0 )
264 mlt_properties_set( meta_media, key, "video" );
265 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
266 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
267 double ffmpeg_fps = av_q2d( context->streams[ i ]->avg_frame_rate );
268 if (ffmpeg_fps == 0) ffmpeg_fps = av_q2d( context->streams[ i ]->r_frame_rate );
269 mlt_properties_set_double( meta_media, key, ffmpeg_fps );
271 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
274 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
275 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
276 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
278 snprintf( key, sizeof(key), "meta.media.%d.codec.frame_rate", i );
279 mlt_properties_set_double( meta_media, key, (double) codec_context->time_base.den /
280 ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num ) );
281 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
282 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
283 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
284 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
285 snprintf( key, sizeof(key), "meta.media.%d.codec.colorspace", i );
286 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
287 mlt_properties_set_int( meta_media, key, codec_context->colorspace );
290 case CODEC_TYPE_AUDIO:
291 if ( *audio_index < 0 )
293 mlt_properties_set( meta_media, key, "audio" );
294 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
295 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
296 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
298 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
299 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
300 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
301 mlt_properties_set_int( meta_media, key, codec_context->channels );
306 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
307 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
308 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
309 mlt_properties_set( meta_media, key, codec->name );
310 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
311 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
312 mlt_properties_set( meta_media, key, codec->long_name );
314 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
315 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
316 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
317 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
318 // snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
319 // mlt_properties_set_int( meta_media, key, codec_context->profile );
320 // snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
321 // mlt_properties_set_int( meta_media, key, codec_context->level );
327 static inline int dv_is_pal( AVPacket *pkt )
329 return pkt->data[3] & 0x80;
332 static int dv_is_wide( AVPacket *pkt )
334 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
336 for ( ; i < pkt->size; i += 5 /* packet size */ )
338 if ( pkt->data[ i ] == 0x61 )
340 uint8_t x = pkt->data[ i + 2 ] & 0x7;
341 return ( x == 2 ) || ( x == 7 );
347 static double get_aspect_ratio( mlt_properties properties, AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
349 double aspect_ratio = 1.0;
351 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
355 if ( dv_is_pal( pkt ) )
357 if ( dv_is_wide( pkt ) )
359 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 64 );
360 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 45 );
364 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 16 );
365 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 15 );
370 if ( dv_is_wide( pkt ) )
372 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 32 );
373 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 27 );
377 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 8 );
378 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 9 );
385 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
386 stream->sample_aspect_ratio;
388 codec_context->sample_aspect_ratio;
390 // Override FFmpeg's notion of DV aspect ratios, which are
391 // based upon a width of 704. Since we do not have a normaliser
392 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
393 // we just coerce the values to facilitate a passive behaviour through
394 // the rescale normaliser when using equivalent producers and consumers.
395 // = display_aspect / (width * height)
396 if ( ar.num == 10 && ar.den == 11 )
399 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 8 );
400 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 9 );
402 else if ( ar.num == 59 && ar.den == 54 )
405 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 16 );
406 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 15 );
408 else if ( ar.num == 40 && ar.den == 33 )
411 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 32 );
412 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 27 );
414 else if ( ar.num == 118 && ar.den == 81 )
417 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 64 );
418 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 45 );
424 AVRational codec_sar = codec_context->sample_aspect_ratio;
425 AVRational stream_sar =
426 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
427 stream->sample_aspect_ratio;
431 if ( codec_sar.num > 0 )
433 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", codec_sar.num );
434 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", codec_sar.den );
436 else if ( stream_sar.num > 0 )
438 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", stream_sar.num );
439 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", stream_sar.den );
443 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 1 );
444 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 1 );
447 AVRational ar = { mlt_properties_get_double( properties, "meta.media.sample_aspect_num" ), mlt_properties_get_double( properties, "meta.media.sample_aspect_den" ) };
448 aspect_ratio = av_q2d( ar );
449 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
457 static int producer_open( producer_avformat this, mlt_profile profile, char *file )
459 // Return an error code (0 == no error)
462 // Context for avformat
463 AVFormatContext *context = NULL;
465 // Get the properties
466 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this->parent );
468 // We will treat everything with the producer fps
469 double fps = mlt_profile_fps( profile );
471 // Lock the mutex now
474 // If "MRL", then create AVInputFormat
475 AVInputFormat *format = NULL;
476 AVFormatParameters *params = NULL;
477 char *standard = NULL;
478 char *mrl = strchr( file, ':' );
480 // AV option (0 = both, 1 = video, 2 = audio)
483 // Only if there is not a protocol specification that avformat can handle
484 if ( mrl && !url_exist( file ) )
486 // 'file' becomes format abbreviation
490 format = av_find_input_format( file );
492 // Eat the format designator
498 params = calloc( sizeof( AVFormatParameters ), 1 );
500 // These are required by video4linux (defaults)
502 params->height = 480;
503 params->time_base= (AVRational){1,25};
504 // params->device = file;
505 params->channels = 2;
506 params->sample_rate = 48000;
510 mrl = strchr( file, '?' );
514 char *name = strdup( ++mrl );
515 char *value = strchr( name, ':' );
520 char *t = strchr( value, '&' );
523 if ( !strcmp( name, "frame_rate" ) )
524 params->time_base.den = atoi( value );
525 else if ( !strcmp( name, "frame_rate_base" ) )
526 params->time_base.num = atoi( value );
527 else if ( !strcmp( name, "sample_rate" ) )
528 params->sample_rate = atoi( value );
529 else if ( !strcmp( name, "channels" ) )
530 params->channels = atoi( value );
531 else if ( !strcmp( name, "width" ) )
532 params->width = atoi( value );
533 else if ( !strcmp( name, "height" ) )
534 params->height = atoi( value );
535 else if ( !strcmp( name, "standard" ) )
537 standard = strdup( value );
538 params->standard = standard;
540 else if ( !strcmp( name, "av" ) )
544 mrl = strchr( mrl, '&' );
548 // Now attempt to open the file
549 error = av_open_input_file( &context, file, format, 0, params ) < 0;
551 // Cleanup AVFormatParameters
555 // If successful, then try to get additional info
558 // Get the stream info
559 error = av_find_stream_info( context ) < 0;
561 // Continue if no error
564 // We will default to the first audio and video streams found
565 int audio_index = -1;
566 int video_index = -1;
568 // Now set properties where we can (use default unknowns if required)
569 if ( context->duration != AV_NOPTS_VALUE )
571 // This isn't going to be accurate for all formats
572 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
573 mlt_properties_set_position( properties, "out", frames - 1 );
574 mlt_properties_set_position( properties, "length", frames );
577 // Find default audio and video streams
578 find_default_streams( properties, context, &audio_index, &video_index );
580 if ( context->start_time != AV_NOPTS_VALUE )
581 this->start_time = context->start_time;
583 // Check if we're seekable (something funny about mpeg here :-/)
584 if ( strncmp( file, "pipe:", 5 ) &&
585 strncmp( file, "/dev/", 5 ) &&
586 strncmp( file, "http:", 5 ) &&
587 strncmp( file, "udp:", 4 ) &&
588 strncmp( file, "tcp:", 4 ) &&
589 strncmp( file, "rtsp:", 5 ) &&
590 strncmp( file, "rtp:", 4 ) )
592 this->seekable = av_seek_frame( context, -1, this->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
593 mlt_properties_set_int( properties, "seekable", this->seekable );
594 this->dummy_context = context;
595 av_open_input_file( &context, file, NULL, 0, NULL );
596 av_find_stream_info( context );
599 // Store selected audio and video indexes on properties
600 this->audio_index = audio_index;
601 this->video_index = video_index;
602 this->first_pts = -1;
603 this->last_position = POSITION_INITIAL;
605 // Fetch the width, height and aspect ratio
606 if ( video_index != -1 )
608 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
609 mlt_properties_set_int( properties, "width", codec_context->width );
610 mlt_properties_set_int( properties, "height", codec_context->height );
612 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
614 // Fetch the first frame of DV so we can read it directly
619 ret = av_read_frame( context, &pkt );
620 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
622 get_aspect_ratio( properties, context->streams[ video_index ], codec_context, &pkt );
629 get_aspect_ratio( properties, context->streams[ video_index ], codec_context, NULL );
632 struct SwsContext *context = sws_getContext( codec_context->width, codec_context->height, codec_context->pix_fmt,
633 codec_context->width, codec_context->height, PIX_FMT_YUYV422, SWS_BILINEAR, NULL, NULL, NULL);
635 sws_freeContext( context );
642 if ( context->title )
643 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
644 if ( context->author )
645 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
646 if ( context->copyright )
647 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
648 if ( context->comment )
649 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
650 if ( context->album )
651 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
653 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
654 if ( context->track )
655 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
657 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
658 if ( av == 0 && audio_index != -1 && video_index != -1 )
660 // We'll use the open one as our video_format
661 this->video_format = context;
663 // And open again for our audio context
664 av_open_input_file( &context, file, NULL, 0, NULL );
665 av_find_stream_info( context );
668 this->audio_format = context;
670 else if ( av != 2 && video_index != -1 )
672 // We only have a video context
673 this->video_format = context;
675 else if ( audio_index != -1 )
677 // We only have an audio context
678 this->audio_format = context;
682 // Something has gone wrong
688 // Unlock the mutex now
694 /** Convert a frame position to a time code.
697 static double producer_time_of_frame( mlt_producer this, mlt_position position )
699 return ( double )position / mlt_producer_get_fps( this );
702 // Collect information about all audio streams
704 static void get_audio_streams_info( producer_avformat this )
706 // Fetch the audio format context
707 AVFormatContext *context = this->audio_format;
711 i < context->nb_streams;
714 if ( context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
716 AVCodecContext *codec_context = context->streams[i]->codec;
717 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
719 // If we don't have a codec and we can't initialise it, we can't do much more...
721 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
723 this->audio_streams++;
724 this->audio_max_stream = i;
725 this->total_channels += codec_context->channels;
726 if ( codec_context->channels > this->max_channel )
727 this->max_channel = codec_context->channels;
728 if ( codec_context->sample_rate > this->max_frequency )
729 this->max_frequency = codec_context->sample_rate;
730 avcodec_close( codec_context );
735 mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
736 this->audio_streams, this->audio_max_stream, this->total_channels, this->max_channel );
738 // Other audio-specific initializations
739 this->resample_factor = 1.0;
742 static void set_luma_transfer( struct SwsContext *context, int colorspace, int use_full_range )
744 #if defined(SWSCALE) && (LIBSWSCALE_VERSION_INT >= ((0<<16)+(7<<8)+2))
747 int brightness, contrast, saturation;
749 if ( sws_getColorspaceDetails( context, &coefficients, &full_range, &coefficients, &full_range,
750 &brightness, &contrast, &saturation ) != -1 )
752 // Don't change these from defaults unless explicitly told to.
753 if ( use_full_range >= 0 )
754 full_range = use_full_range;
755 switch ( colorspace )
761 coefficients = sws_getCoefficients( SWS_CS_ITU601 );
764 coefficients = sws_getCoefficients( SWS_CS_SMPTE240M );
767 coefficients = sws_getCoefficients( SWS_CS_ITU709 );
770 sws_setColorspaceDetails( context, coefficients, full_range, coefficients, full_range,
771 brightness, contrast, saturation );
776 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
777 mlt_image_format *format, int width, int height, int colorspace )
781 int flags = SWS_BILINEAR | SWS_ACCURATE_RND;
784 flags |= SWS_CPU_CAPS_MMX;
787 flags |= SWS_CPU_CAPS_MMX2;
790 if ( pix_fmt == PIX_FMT_RGB32 )
792 *format = mlt_image_rgb24a;
793 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
794 width, height, PIX_FMT_RGBA, flags, NULL, NULL, NULL);
796 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
797 set_luma_transfer( context, colorspace, full_range );
798 sws_scale( context, frame->data, frame->linesize, 0, height,
799 output.data, output.linesize);
800 sws_freeContext( context );
802 else if ( *format == mlt_image_yuv420p )
804 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
805 width, height, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
807 output.data[0] = buffer;
808 output.data[1] = buffer + width * height;
809 output.data[2] = buffer + ( 5 * width * height ) / 4;
810 output.linesize[0] = width;
811 output.linesize[1] = width >> 1;
812 output.linesize[2] = width >> 1;
813 set_luma_transfer( context, colorspace, full_range );
814 sws_scale( context, frame->data, frame->linesize, 0, height,
815 output.data, output.linesize);
816 sws_freeContext( context );
818 else if ( *format == mlt_image_rgb24 )
820 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
821 width, height, PIX_FMT_RGB24, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
823 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
824 set_luma_transfer( context, colorspace, full_range );
825 sws_scale( context, frame->data, frame->linesize, 0, height,
826 output.data, output.linesize);
827 sws_freeContext( context );
829 else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
831 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
832 width, height, PIX_FMT_RGBA, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
834 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
835 set_luma_transfer( context, colorspace, full_range );
836 sws_scale( context, frame->data, frame->linesize, 0, height,
837 output.data, output.linesize);
838 sws_freeContext( context );
842 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
843 width, height, PIX_FMT_YUYV422, flags | SWS_FULL_CHR_H_INP, NULL, NULL, NULL);
845 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
846 set_luma_transfer( context, colorspace, full_range );
847 sws_scale( context, frame->data, frame->linesize, 0, height,
848 output.data, output.linesize);
849 sws_freeContext( context );
852 if ( *format == mlt_image_yuv420p )
855 pict.data[0] = buffer;
856 pict.data[1] = buffer + width * height;
857 pict.data[2] = buffer + ( 5 * width * height ) / 4;
858 pict.linesize[0] = width;
859 pict.linesize[1] = width >> 1;
860 pict.linesize[2] = width >> 1;
861 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
863 else if ( *format == mlt_image_rgb24 )
866 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
867 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
869 else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
872 avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
873 img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
878 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
879 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
884 /** Allocate the image buffer and set it on the frame.
887 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
891 if ( codec_context->width == 0 || codec_context->height == 0 )
894 *width = codec_context->width;
895 *height = codec_context->height;
897 if ( codec_context->pix_fmt == PIX_FMT_RGB32 )
898 size = *width * ( *height + 1 ) * 4;
899 else switch ( *format )
901 case mlt_image_yuv420p:
902 size = *width * 3 * ( *height + 1 ) / 2;
904 case mlt_image_rgb24:
905 size = *width * ( *height + 1 ) * 3;
907 case mlt_image_rgb24a:
908 case mlt_image_opengl:
909 size = *width * ( *height + 1 ) * 4;
912 *format = mlt_image_yuv422;
913 size = *width * ( *height + 1 ) * 2;
917 // Construct the output image
918 *buffer = mlt_pool_alloc( size );
920 mlt_properties_set_data( frame_properties, "image", *buffer, size, mlt_pool_release, NULL );
927 /** Get an image from a frame.
930 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
933 producer_avformat this = mlt_frame_pop_service( frame );
934 mlt_producer producer = this->parent;
936 // Get the properties from the frame
937 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
939 // Obtain the frame number of this frame
940 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
942 // Get the producer properties
943 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
945 // Fetch the video format context
946 AVFormatContext *context = this->video_format;
948 // Get the video stream
949 AVStream *stream = context->streams[ this->video_index ];
952 AVCodecContext *codec_context = stream->codec;
954 // Get the image cache
955 if ( ! this->image_cache && ! mlt_properties_get_int( properties, "noimagecache" ) )
956 this->image_cache = mlt_cache_init();
957 if ( this->image_cache )
959 mlt_cache_item item = mlt_cache_get( this->image_cache, (void*) position );
960 *buffer = mlt_cache_item_data( item, (int*) format );
963 // Set the resolution
964 *width = codec_context->width;
965 *height = codec_context->height;
967 // Workaround 1088 encodings missing cropping info.
968 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
975 case mlt_image_yuv420p:
976 size = *width * 3 * ( *height + 1 ) / 2;
978 case mlt_image_rgb24:
979 size = *width * ( *height + 1 ) * 3;
981 case mlt_image_rgb24a:
982 case mlt_image_opengl:
983 size = *width * ( *height + 1 ) * 4;
986 *format = mlt_image_yuv422;
987 size = *width * ( *height + 1 ) * 2;
990 mlt_properties_set_data( frame_properties, "avformat.image_cache", item, 0, ( mlt_destructor )mlt_cache_item_close, NULL );
991 mlt_properties_set_data( frame_properties, "image", *buffer, size, NULL, NULL );
992 // this->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
993 this->got_picture = 1;
1006 // Special case pause handling flag
1009 // Special case ffwd handling
1012 // We may want to use the source fps if available
1013 double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) /
1014 mlt_properties_get_double( properties, "meta.media.frame_rate_den" );
1015 double fps = mlt_producer_get_fps( producer );
1017 // This is the physical frame position in the source
1018 int req_position = ( int )( position / fps * source_fps + 0.5 );
1020 // Determines if we have to decode all frames in a sequence
1021 // Temporary hack to improve intra frame only
1022 int must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
1023 strcmp( codec_context->codec->name, "dvvideo" ) &&
1024 strcmp( codec_context->codec->name, "huffyuv" ) &&
1025 strcmp( codec_context->codec->name, "mjpeg" ) &&
1026 strcmp( codec_context->codec->name, "rawvideo" );
1028 int last_position = this->last_position;
1030 // Turn on usage of new seek API and PTS for seeking
1031 int use_new_seek = codec_context->codec_id == CODEC_ID_H264 && !strcmp( context->iformat->name, "mpegts" );
1032 if ( mlt_properties_get( properties, "new_seek" ) )
1033 use_new_seek = mlt_properties_get_int( properties, "new_seek" );
1035 // Seek if necessary
1036 if ( position != this->video_expected || last_position < 0 )
1038 if ( this->av_frame && position + 1 == this->video_expected )
1040 // We're paused - use last image
1043 else if ( !this->seekable && position > this->video_expected && ( position - this->video_expected ) < 250 )
1045 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1046 ignore = ( int )( ( position - this->video_expected ) / fps * source_fps );
1047 codec_context->skip_loop_filter = AVDISCARD_NONREF;
1049 else if ( this->seekable && ( position < this->video_expected || position - this->video_expected >= 12 || last_position < 0 ) )
1051 if ( use_new_seek && last_position == POSITION_INITIAL )
1053 // find first key frame
1057 while ( ret >= 0 && toscan-- > 0 )
1059 ret = av_read_frame( context, &pkt );
1060 if ( ret >= 0 && ( pkt.flags & PKT_FLAG_KEY ) && pkt.stream_index == this->video_index )
1062 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "first_pts %lld dts %lld pts_dts_delta %d\n", pkt.pts, pkt.dts, (int)(pkt.pts - pkt.dts) );
1063 this->first_pts = pkt.pts;
1066 av_free_packet( &pkt );
1069 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
1072 // Calculate the timestamp for the requested frame
1076 timestamp = ( req_position - 0.1 / source_fps ) /
1077 ( av_q2d( stream->time_base ) * source_fps );
1078 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "pos %d pts %lld ", req_position, timestamp );
1079 if ( this->first_pts > 0 )
1080 timestamp += this->first_pts;
1081 else if ( context->start_time != AV_NOPTS_VALUE )
1082 timestamp += context->start_time;
1086 timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
1087 if ( context->start_time != AV_NOPTS_VALUE )
1088 timestamp += context->start_time;
1091 timestamp -= AV_TIME_BASE;
1092 if ( timestamp < 0 )
1094 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %lld position %d expected %d last_pos %d\n",
1095 timestamp, position, this->video_expected, last_position );
1097 // Seek to the timestamp
1100 codec_context->skip_loop_filter = AVDISCARD_NONREF;
1101 av_seek_frame( context, this->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
1105 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
1108 // Remove the cached info relating to the previous position
1109 this->current_position = POSITION_INVALID;
1110 this->last_position = POSITION_INVALID;
1111 av_freep( &this->av_frame );
1115 // flush any pictures still in decode buffer
1116 avcodec_flush_buffers( codec_context );
1121 // Duplicate the last image if necessary (see comment on rawvideo below)
1122 if ( this->av_frame && this->av_frame->linesize[0] && this->got_picture && this->seekable
1124 || this->current_position == req_position
1125 || ( !use_new_seek && this->current_position > req_position ) ) )
1128 if ( ( image_size = allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) ) )
1130 // Workaround 1088 encodings missing cropping info.
1131 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1134 if ( this->vdpau && this->vdpau->buffer )
1137 picture.data[0] = this->vdpau->buffer;
1138 picture.data[2] = this->vdpau->buffer + codec_context->width * codec_context->height;
1139 picture.data[1] = this->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1140 picture.linesize[0] = codec_context->width;
1141 picture.linesize[1] = codec_context->width / 2;
1142 picture.linesize[2] = codec_context->width / 2;
1143 convert_image( (AVFrame*) &picture, *buffer,
1144 PIX_FMT_YUV420P, format, *width, *height, this->colorspace );
1148 convert_image( this->av_frame, *buffer, codec_context->pix_fmt,
1149 format, *width, *height, this->colorspace );
1152 mlt_frame_get_image( frame, buffer, format, width, height, writable );
1157 int int_position = 0;
1158 int decode_errors = 0;
1159 int got_picture = 0;
1161 av_init_packet( &pkt );
1163 // Construct an AVFrame for YUV422 conversion
1164 if ( !this->av_frame )
1165 this->av_frame = avcodec_alloc_frame( );
1167 while( ret >= 0 && !got_picture )
1170 ret = av_read_frame( context, &pkt );
1172 // We only deal with video from the selected video_index
1173 if ( ret >= 0 && pkt.stream_index == this->video_index && pkt.size > 0 )
1175 // Determine time code of the packet
1178 int64_t pts = pkt.pts;
1179 if ( this->first_pts > 0 )
1180 pts -= this->first_pts;
1181 else if ( context->start_time != AV_NOPTS_VALUE )
1182 pts -= context->start_time;
1183 int_position = ( int )( av_q2d( stream->time_base ) * pts * source_fps + 0.1 );
1184 if ( pkt.pts == AV_NOPTS_VALUE )
1186 this->invalid_pts_counter++;
1187 if ( this->invalid_pts_counter > 20 )
1189 mlt_log_panic( MLT_PRODUCER_SERVICE(producer), "\ainvalid PTS; DISABLING NEW_SEEK!\n" );
1190 mlt_properties_set_int( properties, "new_seek", 0 );
1191 int_position = req_position;
1197 this->invalid_pts_counter = 0;
1199 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.pts %llu req_pos %d cur_pos %d pkt_pos %d\n",
1200 pkt.pts, req_position, this->current_position, int_position );
1204 if ( pkt.dts != AV_NOPTS_VALUE )
1206 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
1207 if ( context->start_time != AV_NOPTS_VALUE )
1208 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
1209 last_position = this->last_position;
1210 if ( int_position == last_position )
1211 int_position = last_position + 1;
1215 int_position = req_position;
1217 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d\n",
1218 pkt.dts, req_position, this->current_position, int_position );
1219 // Make a dumb assumption on streams that contain wild timestamps
1220 if ( abs( req_position - int_position ) > 999 )
1222 int_position = req_position;
1223 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!" );
1226 this->last_position = int_position;
1229 if ( must_decode || int_position >= req_position )
1232 if ( g_vdpau && this->vdpau )
1234 if ( g_vdpau->producer != this )
1236 vdpau_decoder_close();
1237 vdpau_decoder_init( this );
1240 this->vdpau->is_decoded = 0;
1243 codec_context->reordered_opaque = pkt.pts;
1244 if ( int_position >= req_position )
1245 codec_context->skip_loop_filter = AVDISCARD_NONE;
1246 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1247 ret = avcodec_decode_video2( codec_context, this->av_frame, &got_picture, &pkt );
1249 ret = avcodec_decode_video( codec_context, this->av_frame, &got_picture, pkt.data, pkt.size );
1251 // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1254 if ( ++decode_errors <= 10 )
1267 // Determine time code of the packet
1268 int64_t pts = this->av_frame->reordered_opaque;
1269 if ( this->first_pts > 0 )
1270 pts -= this->first_pts;
1271 else if ( context->start_time != AV_NOPTS_VALUE )
1272 pts -= context->start_time;
1273 int_position = ( int )( av_q2d( stream->time_base) * pts * source_fps + 0.1 );
1274 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "got frame %d, key %d\n", int_position, this->av_frame->key_frame );
1277 if ( int_position < req_position )
1282 else if ( int_position >= req_position )
1285 codec_context->skip_loop_filter = AVDISCARD_NONE;
1287 else if ( ignore -- )
1292 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
1293 av_free_packet( &pkt );
1295 else if ( ret >= 0 )
1297 av_free_packet( &pkt );
1300 // Now handle the picture if we have one
1303 if ( ( image_size = allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) ) )
1305 // Workaround 1088 encodings missing cropping info.
1306 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1311 if ( this->vdpau->is_decoded )
1313 struct vdpau_render_state *render = (struct vdpau_render_state*) this->av_frame->data[0];
1315 uint32_t pitches[3];
1316 VdpYCbCrFormat dest_format = VDP_YCBCR_FORMAT_YV12;
1319 if ( !this->vdpau->buffer )
1320 this->vdpau->buffer = mlt_pool_alloc( codec_context->width * codec_context->height * 3 / 2 );
1321 picture.data[0] = planes[0] = this->vdpau->buffer;
1322 picture.data[2] = planes[1] = this->vdpau->buffer + codec_context->width * codec_context->height;
1323 picture.data[1] = planes[2] = this->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1324 picture.linesize[0] = pitches[0] = codec_context->width;
1325 picture.linesize[1] = pitches[1] = codec_context->width / 2;
1326 picture.linesize[2] = pitches[2] = codec_context->width / 2;
1328 VdpStatus status = vdp_surface_get_bits( render->surface, dest_format, planes, pitches );
1329 if ( status == VDP_STATUS_OK )
1331 convert_image( (AVFrame*) &picture, *buffer, PIX_FMT_YUV420P,
1332 format, *width, *height, this->colorspace );
1336 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU Error: %s\n", vdp_get_error_string( status ) );
1337 this->vdpau->is_decoded = 0;
1342 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU error in VdpDecoderRender\n" );
1348 convert_image( this->av_frame, *buffer, codec_context->pix_fmt,
1349 format, *width, *height, this->colorspace );
1350 this->top_field_first |= this->av_frame->top_field_first;
1351 this->current_position = int_position;
1352 this->got_picture = 1;
1364 if ( this->got_picture && image_size > 0 && this->image_cache )
1366 // Copy buffer to image cache
1367 uint8_t *image = mlt_pool_alloc( image_size );
1368 memcpy( image, *buffer, image_size );
1369 mlt_cache_put( this->image_cache, (void*) position, image, *format, mlt_pool_release );
1372 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
1373 this->video_expected = position + 1;
1376 // Set the progressive flag
1377 if ( mlt_properties_get( properties, "force_progressive" ) )
1378 mlt_properties_set_int( frame_properties, "progressive", !!mlt_properties_get_int( properties, "force_progressive" ) );
1379 else if ( this->av_frame )
1380 mlt_properties_set_int( frame_properties, "progressive", !this->av_frame->interlaced_frame );
1382 // Set the field order property for this frame
1383 if ( mlt_properties_get( properties, "force_tff" ) )
1384 mlt_properties_set_int( frame_properties, "top_field_first", !!mlt_properties_get_int( properties, "force_tff" ) );
1386 mlt_properties_set_int( frame_properties, "top_field_first", this->top_field_first );
1388 // Set immutable properties of the selected track's (or overridden) source attributes.
1389 mlt_properties_set_int( properties, "meta.media.top_field_first", this->top_field_first );
1390 mlt_properties_set_int( properties, "meta.media.progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
1392 return !this->got_picture;
1395 /** Process properties as AVOptions and apply to AV context obj
1398 static void apply_properties( void *obj, mlt_properties properties, int flags )
1401 int count = mlt_properties_count( properties );
1402 for ( i = 0; i < count; i++ )
1404 const char *opt_name = mlt_properties_get_name( properties, i );
1405 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
1406 if ( opt_name && mlt_properties_get( properties, opt_name ) )
1409 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
1410 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
1411 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
1412 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1414 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
1420 /** Initialize the video codec context.
1423 static int video_codec_init( producer_avformat this, int index, mlt_properties properties )
1425 // Initialise the codec if necessary
1426 if ( !this->video_codec )
1428 // Get the video stream
1429 AVStream *stream = this->video_format->streams[ index ];
1431 // Get codec context
1432 AVCodecContext *codec_context = stream->codec;
1435 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1437 if ( codec_context->codec_id == CODEC_ID_H264 )
1439 if ( ( codec = avcodec_find_decoder_by_name( "h264_vdpau" ) ) )
1441 if ( vdpau_init( this ) )
1443 this->video_codec = codec_context;
1444 if ( !vdpau_decoder_init( this ) )
1445 vdpau_decoder_close();
1449 codec = avcodec_find_decoder( codec_context->codec_id );
1453 // Initialise multi-threading
1454 int thread_count = mlt_properties_get_int( properties, "threads" );
1455 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1456 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1457 if ( thread_count > 1 )
1459 avcodec_thread_init( codec_context, thread_count );
1460 codec_context->thread_count = thread_count;
1463 // If we don't have a codec and we can't initialise it, we can't do much more...
1465 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1467 // Now store the codec with its destructor
1468 this->video_codec = codec_context;
1472 // Remember that we can't use this later
1473 this->video_index = -1;
1477 // Process properties as AVOptions
1478 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1480 // Reset some image properties
1481 mlt_properties_set_int( properties, "width", this->video_codec->width );
1482 mlt_properties_set_int( properties, "height", this->video_codec->height );
1483 // For DV, we'll just use the saved aspect ratio
1484 if ( codec_context->codec_id != CODEC_ID_DVVIDEO )
1485 get_aspect_ratio( properties, stream, this->video_codec, NULL );
1487 // Determine the fps first from the codec
1488 double source_fps = (double) this->video_codec->time_base.den /
1489 ( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
1491 if ( mlt_properties_get( properties, "force_fps" ) )
1493 source_fps = mlt_properties_get_double( properties, "force_fps" );
1494 stream->time_base = av_d2q( source_fps, 1024 );
1495 mlt_properties_set_int( properties, "meta.media.frame_rate_num", stream->time_base.num );
1496 mlt_properties_set_int( properties, "meta.media.frame_rate_den", stream->time_base.den );
1500 // If the muxer reports a frame rate different than the codec
1501 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1502 double muxer_fps = av_q2d( stream->avg_frame_rate );
1503 if (muxer_fps == 0) muxer_fps = av_q2d( stream->r_frame_rate );
1505 double muxer_fps = av_q2d( stream->r_frame_rate );
1507 // Choose the lesser - the wrong tends to be off by some multiple of 10
1508 source_fps = FFMIN( source_fps, muxer_fps );
1509 if ( source_fps >= 1.0 && source_fps < muxer_fps )
1511 mlt_properties_set_int( properties, "meta.media.frame_rate_num", this->video_codec->time_base.num );
1512 mlt_properties_set_int( properties, "meta.media.frame_rate_den", this->video_codec->time_base.den );
1514 else if ( muxer_fps > 0 )
1516 AVRational frame_rate = stream->r_frame_rate;
1517 // With my samples when r_frame_rate != 1000 but avg_frame_rate is valid,
1518 // avg_frame_rate gives some approximate value that does not well match the media.
1519 // Also, on my sample where r_frame_rate = 1000, using avg_frame_rate directly
1520 // results in some very choppy output, but some value slightly different works
1522 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1523 if ( av_q2d( stream->r_frame_rate ) >= 1000 && av_q2d( stream->avg_frame_rate ) > 0 )
1524 frame_rate = av_d2q( av_q2d( stream->avg_frame_rate ), 1024 );
1526 mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
1527 mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
1531 source_fps = mlt_producer_get_fps( this->parent );
1532 AVRational frame_rate = av_d2q( source_fps, 255 );
1533 mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
1534 mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
1538 // source_fps is deprecated in favor of meta.media.frame_rate_num and .frame_rate_den
1539 if ( source_fps > 0 )
1540 mlt_properties_set_double( properties, "source_fps", source_fps );
1542 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this->parent ) );
1544 // Set the YUV colorspace from override or detect
1545 this->colorspace = mlt_properties_get_int( properties, "force_colorspace" );
1546 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
1547 if ( ! this->colorspace )
1549 switch ( this->video_codec->colorspace )
1551 case AVCOL_SPC_SMPTE240M:
1552 this->colorspace = 240;
1554 case AVCOL_SPC_BT470BG:
1555 case AVCOL_SPC_SMPTE170M:
1556 this->colorspace = 601;
1558 case AVCOL_SPC_BT709:
1559 this->colorspace = 709;
1562 // This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
1563 this->colorspace = this->video_codec->width * this->video_codec->height > 750000 ? 709 : 601;
1568 // Let apps get chosen colorspace
1569 mlt_properties_set_int( properties, "colorspace", this->colorspace );
1571 return this->video_codec && this->video_index > -1;
1574 /** Set up video handling.
1577 static void producer_set_up_video( producer_avformat this, mlt_frame frame )
1580 mlt_producer producer = this->parent;
1582 // Get the properties
1583 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1585 // Fetch the video format context
1586 AVFormatContext *context = this->video_format;
1588 // Get the video_index
1589 int index = mlt_properties_get_int( properties, "video_index" );
1591 // Reopen the file if necessary
1592 if ( !context && index > -1 )
1594 mlt_events_block( properties, producer );
1595 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1596 mlt_properties_get( properties, "resource" ) );
1597 context = this->video_format;
1598 if ( this->dummy_context )
1601 av_close_input_file( this->dummy_context );
1604 this->dummy_context = NULL;
1605 mlt_events_unblock( properties, producer );
1606 if ( this->audio_format )
1607 get_audio_streams_info( this );
1609 // Process properties as AVOptions
1610 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
1613 // Exception handling for video_index
1614 if ( context && index >= (int) context->nb_streams )
1616 // Get the last video stream
1617 for ( index = context->nb_streams - 1;
1618 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO;
1620 mlt_properties_set_int( properties, "video_index", index );
1622 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
1624 // Invalidate the video stream
1626 mlt_properties_set_int( properties, "video_index", index );
1629 // Update the video properties if the index changed
1630 if ( index != this->video_index )
1632 // Reset the video properties if the index changed
1633 this->video_index = index;
1634 if ( this->video_codec )
1637 avcodec_close( this->video_codec );
1640 this->video_codec = NULL;
1643 // Get the frame properties
1644 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1647 if ( context && index > -1 && video_codec_init( this, index, properties ) )
1649 // Set the frame properties
1650 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1651 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1652 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1654 // Set the width and height
1655 mlt_properties_set_int( frame_properties, "width", this->video_codec->width );
1656 mlt_properties_set_int( frame_properties, "height", this->video_codec->height );
1657 // real_width and real_height are deprecated in favor of meta.media.width and .height
1658 mlt_properties_set_int( properties, "meta.media.width", this->video_codec->width );
1659 mlt_properties_set_int( properties, "meta.media.height", this->video_codec->height );
1660 mlt_properties_set_int( frame_properties, "real_width", this->video_codec->width );
1661 mlt_properties_set_int( frame_properties, "real_height", this->video_codec->height );
1662 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1663 mlt_properties_set_int( frame_properties, "colorspace", this->colorspace );
1665 // Workaround 1088 encodings missing cropping info.
1666 if ( this->video_codec->height == 1088 && aspect_ratio * this->video_codec->width / 1080 == 16.0/9.0 )
1668 mlt_properties_set_int( properties, "meta.media.height", 1080 );
1669 mlt_properties_set_int( frame_properties, "real_height", 1080 );
1672 // Add our image operation
1673 mlt_frame_push_service( frame, this );
1674 mlt_frame_push_get_image( frame, producer_get_image );
1678 // If something failed, use test card image
1679 mlt_properties_set_int( frame_properties, "test_image", 1 );
1683 static int seek_audio( producer_avformat this, mlt_position position, double timecode, int *ignore )
1687 // Seek if necessary
1688 if ( position != this->audio_expected )
1690 if ( position + 1 == this->audio_expected )
1692 // We're paused - silence required
1695 else if ( !this->seekable && position > this->audio_expected && ( position - this->audio_expected ) < 250 )
1697 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1698 *ignore = position - this->audio_expected;
1700 else if ( position < this->audio_expected || position - this->audio_expected >= 12 )
1702 AVFormatContext *context = this->audio_format;
1703 int64_t timestamp = ( int64_t )( timecode * AV_TIME_BASE + 0.5 );
1704 if ( context->start_time != AV_NOPTS_VALUE )
1705 timestamp += context->start_time;
1706 if ( timestamp < 0 )
1709 // Set to the real timecode
1710 if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
1713 // Clear the usage in the audio buffer
1714 int i = MAX_AUDIO_STREAMS + 1;
1716 this->audio_used[i - 1] = 0;
1722 static int decode_audio( producer_avformat this, int *ignore, AVPacket pkt, int channels, int samples, double timecode, double fps )
1724 // Fetch the audio_format
1725 AVFormatContext *context = this->audio_format;
1727 // Get the current stream index
1728 int index = pkt.stream_index;
1730 // Get codec context
1731 AVCodecContext *codec_context = this->audio_codec[ index ];
1733 // Obtain the resample context if it exists (not always needed)
1734 ReSampleContext *resample = this->audio_resample[ index ];
1736 // Obtain the audio buffers
1737 int16_t *audio_buffer = this->audio_buffer[ index ];
1738 int16_t *decode_buffer = this->decode_buffer[ index ];
1740 int audio_used = this->audio_used[ index ];
1741 uint8_t *ptr = pkt.data;
1745 while ( ptr && ret >= 0 && len > 0 )
1747 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1750 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1751 ret = avcodec_decode_audio3( codec_context, decode_buffer, &data_size, &pkt );
1752 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1753 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1755 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1759 mlt_log_warning( MLT_PRODUCER_SERVICE(this->parent), "audio decoding error %d\n", ret );
1763 pkt.size = len -= ret;
1764 pkt.data = ptr += ret;
1766 // If decoded successfully
1767 if ( data_size > 0 )
1769 // Figure out how many samples will be needed after resampling
1770 int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
1771 int samples_needed = this->resample_factor * convert_samples + 1;
1773 // Resize audio buffer to prevent overflow
1774 if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
1776 this->audio_buffer_size[ index ] *= 2;
1777 audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
1781 // Copy to audio buffer while resampling
1782 int16_t *source = decode_buffer;
1783 int16_t *dest = &audio_buffer[ audio_used * channels ];
1784 audio_used += audio_resample( resample, dest, source, convert_samples );
1788 // Straight copy to audio buffer
1789 memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
1790 audio_used += convert_samples;
1794 while ( *ignore && audio_used > samples )
1797 audio_used -= samples;
1798 memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
1799 audio_used * sizeof( int16_t ) );
1804 // If we're behind, ignore this packet
1807 double current_pts = av_q2d( context->streams[ index ]->time_base ) * pkt.pts;
1808 int req_position = ( int )( timecode * fps + 0.5 );
1809 int int_position = ( int )( current_pts * fps + 0.5 );
1810 if ( context->start_time != AV_NOPTS_VALUE )
1811 int_position -= ( int )( fps * context->start_time / AV_TIME_BASE + 0.5 );
1813 if ( this->seekable && *ignore == 0 )
1815 if ( int_position < req_position )
1816 // We are behind, so skip some
1818 else if ( int_position > req_position + 2 )
1819 // We are ahead, so seek backwards some more
1820 seek_audio( this, req_position, timecode - 1.0, ignore );
1824 this->audio_used[ index ] = audio_used;
1829 /** Get the audio from a frame.
1832 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1835 producer_avformat this = mlt_frame_pop_audio( frame );
1837 // Obtain the frame number of this frame
1838 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" );
1840 // Calculate the real time code
1841 double real_timecode = producer_time_of_frame( this->parent, position );
1843 // Get the producer fps
1844 double fps = mlt_producer_get_fps( this->parent );
1846 // Number of frames to ignore (for ffwd)
1849 // Flag for paused (silence)
1850 int paused = seek_audio( this, position, real_timecode, &ignore );
1852 // Fetch the audio_format
1853 AVFormatContext *context = this->audio_format;
1855 // Determine the tracks to use
1856 int index = this->audio_index;
1857 int index_max = this->audio_index + 1;
1858 if ( this->audio_index == INT_MAX )
1861 index_max = context->nb_streams;
1862 *channels = this->total_channels;
1863 *frequency = this->max_frequency;
1866 // Initialize the resamplers and buffers
1867 for ( ; index < index_max; index++ )
1869 // Get codec context
1870 AVCodecContext *codec_context = this->audio_codec[ index ];
1872 if ( codec_context && !this->audio_buffer[ index ] )
1874 // Check for resample and create if necessary
1875 if ( codec_context->channels <= 2 )
1877 // Determine by how much resampling will increase number of samples
1878 double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
1879 resample_factor *= (double) *frequency / codec_context->sample_rate;
1880 if ( resample_factor > this->resample_factor )
1881 this->resample_factor = resample_factor;
1883 // Create the resampler
1884 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
1885 this->audio_resample[ index ] = av_audio_resample_init(
1886 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1887 codec_context->channels, *frequency, codec_context->sample_rate,
1888 SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
1890 this->audio_resample[ index ] = audio_resample_init(
1891 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1892 codec_context->channels, *frequency, codec_context->sample_rate );
1897 codec_context->request_channels = this->audio_index == INT_MAX ? codec_context->channels : *channels;
1900 // Check for audio buffer and create if necessary
1901 this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1902 this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
1904 // Check for decoder buffer and create if necessary
1905 this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1909 // Get the audio if required
1916 av_init_packet( &pkt );
1918 // If not resampling, give consumer more than requested.
1919 // It requested number samples based on requested frame rate.
1920 // Do not clean this up with a samples *= ...!
1921 if ( this->audio_index != INT_MAX && ! this->audio_resample[ this->audio_index ] )
1922 *samples = *samples * this->audio_codec[ this->audio_index ]->sample_rate / *frequency;
1924 while ( ret >= 0 && !got_audio )
1926 // Check if the buffer already contains the samples required
1927 if ( this->audio_index != INT_MAX && this->audio_used[ this->audio_index ] >= *samples && ignore == 0 )
1934 ret = av_read_frame( context, &pkt );
1936 // We only deal with audio from the selected audio index
1937 if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
1938 ( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
1940 int channels2 = this->audio_index == INT_MAX ? this->audio_codec[pkt.stream_index]->channels : *channels;
1941 ret = decode_audio( this, &ignore, pkt, channels2, *samples, real_timecode, fps );
1943 av_free_packet( &pkt );
1945 if ( this->audio_index == INT_MAX && ret >= 0 )
1947 // Determine if there is enough audio for all streams
1949 for ( index = 0; index < context->nb_streams; index++ )
1951 if ( this->audio_codec[ index ] && this->audio_used[ index ] < *samples )
1957 // Allocate and set the frame's audio buffer
1958 int size = *samples * *channels * sizeof(int16_t);
1959 *buffer = mlt_pool_alloc( size );
1960 *format = mlt_audio_s16;
1961 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
1963 // Interleave tracks if audio_index=all
1964 if ( this->audio_index == INT_MAX )
1966 int16_t *dest = *buffer;
1968 for ( i = 0; i < *samples; i++ )
1970 for ( index = 0; index < index_max; index++ )
1971 if ( this->audio_codec[ index ] )
1973 int current_channels = this->audio_codec[ index ]->channels;
1974 int16_t *src = this->audio_buffer[ index ] + i * current_channels;
1975 memcpy( dest, src, current_channels * sizeof(int16_t) );
1976 dest += current_channels;
1979 for ( index = 0; index < index_max; index++ )
1980 if ( this->audio_codec[ index ] && this->audio_used[ index ] >= *samples )
1982 int current_channels = this->audio_codec[ index ]->channels;
1983 int16_t *src = this->audio_buffer[ index ] + *samples * current_channels;
1984 this->audio_used[index] -= *samples;
1985 memmove( this->audio_buffer[ index ], src, this->audio_used[ index ] * current_channels * sizeof(int16_t) );
1988 // Copy a single track to the output buffer
1991 index = this->audio_index;
1993 // Now handle the audio if we have enough
1994 if ( this->audio_used[ index ] > 0 )
1996 int16_t *src = this->audio_buffer[ index ];
1997 *samples = this->audio_used[ index ] < *samples ? this->audio_used[ index ] : *samples;
1998 memcpy( *buffer, src, *samples * *channels * sizeof(int16_t) );
1999 this->audio_used[ index ] -= *samples;
2000 memmove( src, &src[ *samples * *channels ], this->audio_used[ index ] * *channels * sizeof(int16_t) );
2004 // Otherwise fill with silence
2005 memset( *buffer, 0, *samples * *channels * sizeof(int16_t) );
2007 if ( !this->audio_resample[ index ] )
2009 // TODO: uncomment and remove following line when full multi-channel support is ready
2010 // *channels = codec_context->channels;
2011 *frequency = this->audio_codec[ index ]->sample_rate;
2017 // Get silence and don't touch the context
2018 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
2021 // Regardless of speed (other than paused), we expect to get the next frame
2023 this->audio_expected = position + 1;
2028 /** Initialize the audio codec context.
2031 static int audio_codec_init( producer_avformat this, int index, mlt_properties properties )
2033 // Initialise the codec if necessary
2034 if ( !this->audio_codec[ index ] )
2036 // Get codec context
2037 AVCodecContext *codec_context = this->audio_format->streams[index]->codec;
2040 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
2042 // If we don't have a codec and we can't initialise it, we can't do much more...
2044 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
2046 // Now store the codec with its destructor
2047 if ( this->audio_codec[ index ] )
2048 avcodec_close( this->audio_codec[ index ] );
2049 this->audio_codec[ index ] = codec_context;
2053 // Remember that we can't use this later
2054 this->audio_index = -1;
2058 // Process properties as AVOptions
2059 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2061 return this->audio_codec[ index ] && this->audio_index > -1;
2064 /** Set up audio handling.
2067 static void producer_set_up_audio( producer_avformat this, mlt_frame frame )
2070 mlt_producer producer = this->parent;
2072 // Get the properties
2073 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
2075 // Fetch the audio format context
2076 AVFormatContext *context = this->audio_format;
2078 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
2080 // Get the audio_index
2081 int index = mlt_properties_get_int( properties, "audio_index" );
2083 // Handle all audio tracks
2084 if ( this->audio_index > -1 &&
2085 mlt_properties_get( properties, "audio_index" ) &&
2086 !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
2089 // Reopen the file if necessary
2090 if ( !context && this->audio_index > -1 && index > -1 )
2092 mlt_events_block( properties, producer );
2093 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
2094 mlt_properties_get( properties, "resource" ) );
2095 context = this->audio_format;
2096 if ( this->dummy_context )
2099 av_close_input_file( this->dummy_context );
2102 this->dummy_context = NULL;
2103 mlt_events_unblock( properties, producer );
2104 get_audio_streams_info( this );
2107 // Exception handling for audio_index
2108 if ( context && index >= (int) context->nb_streams && index < INT_MAX )
2110 for ( index = context->nb_streams - 1;
2111 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO;
2113 mlt_properties_set_int( properties, "audio_index", index );
2115 if ( context && index > -1 && index < INT_MAX &&
2116 context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
2118 index = this->audio_index;
2119 mlt_properties_set_int( properties, "audio_index", index );
2122 // Update the audio properties if the index changed
2123 if ( context && index > -1 && index != this->audio_index )
2125 if ( this->audio_codec[ this->audio_index ] )
2128 avcodec_close( this->audio_codec[ this->audio_index ] );
2131 this->audio_codec[ this->audio_index ] = NULL;
2133 if ( this->audio_index != -1 )
2134 this->audio_index = index;
2139 if ( context && index == INT_MAX )
2141 mlt_properties_set_int( frame_properties, "frequency", this->max_frequency );
2142 mlt_properties_set_int( frame_properties, "channels", this->total_channels );
2143 for ( index = 0; index < context->nb_streams; index++ )
2145 if ( context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO )
2146 audio_codec_init( this, index, properties );
2149 else if ( context && index > -1 && audio_codec_init( this, index, properties ) )
2151 // Set the frame properties
2152 if ( index < INT_MAX )
2154 mlt_properties_set_int( frame_properties, "frequency", this->audio_codec[ index ]->sample_rate );
2155 mlt_properties_set_int( frame_properties, "channels", this->audio_codec[ index ]->channels );
2158 if ( context && index > -1 )
2160 // Add our audio operation
2161 mlt_frame_push_audio( frame, this );
2162 mlt_frame_push_audio( frame, producer_get_audio );
2166 /** Our get frame implementation.
2169 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
2171 // Access the private data
2172 mlt_service service = MLT_PRODUCER_SERVICE( producer );
2173 mlt_cache_item cache_item = mlt_service_cache_get( service, "producer_avformat" );
2174 producer_avformat this = mlt_cache_item_data( cache_item, NULL );
2179 this = calloc( 1, sizeof( struct producer_avformat_s ) );
2180 producer->child = this;
2181 this->parent = producer;
2182 mlt_service_cache_put( service, "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
2183 cache_item = mlt_service_cache_get( service, "producer_avformat" );
2186 // Create an empty frame
2187 *frame = mlt_frame_init( service);
2191 mlt_properties_set_data( MLT_FRAME_PROPERTIES(*frame), "avformat_cache", cache_item, 0, (mlt_destructor) mlt_cache_item_close, NULL );
2195 mlt_cache_item_close( cache_item );
2199 // Update timecode on the frame we're creating
2200 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
2202 // Set the position of this producer
2203 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( producer ) );
2206 producer_set_up_video( this, *frame );
2209 producer_set_up_audio( this, *frame );
2211 // Calculate the next timecode
2212 mlt_producer_prepare_next( producer );
2217 static void producer_avformat_close( producer_avformat this )
2219 mlt_log_debug( NULL, "producer_avformat_close\n" );
2221 av_free( this->av_frame );
2224 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
2226 if ( this->audio_resample[i] )
2227 audio_resample_close( this->audio_resample[i] );
2228 mlt_pool_release( this->audio_buffer[i] );
2229 av_free( this->decode_buffer[i] );
2230 if ( this->audio_codec[i] )
2231 avcodec_close( this->audio_codec[i] );
2233 if ( this->video_codec )
2234 avcodec_close( this->video_codec );
2235 if ( this->dummy_context )
2236 av_close_input_file( this->dummy_context );
2237 if ( this->audio_format )
2238 av_close_input_file( this->audio_format );
2239 if ( this->video_format )
2240 av_close_input_file( this->video_format );
2243 vdpau_producer_close( this );
2245 if ( this->image_cache )
2246 mlt_cache_close( this->image_cache );
2250 static void producer_close( mlt_producer parent )
2253 parent->close = NULL;
2254 mlt_producer_close( parent );