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 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
286 snprintf( key, sizeof(key), "meta.media.%d.codec.colorspace", i );
287 switch ( codec_context->colorspace )
289 case AVCOL_SPC_SMPTE240M:
290 mlt_properties_set_int( meta_media, key, 240 );
292 case AVCOL_SPC_BT470BG:
293 case AVCOL_SPC_SMPTE170M:
294 mlt_properties_set_int( meta_media, key, 601 );
296 case AVCOL_SPC_BT709:
297 mlt_properties_set_int( meta_media, key, 709 );
300 // This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
301 mlt_properties_set_int( meta_media, key, codec_context->width * codec_context->height > 750000 ? 709 : 601 );
306 case CODEC_TYPE_AUDIO:
307 if ( *audio_index < 0 )
309 mlt_properties_set( meta_media, key, "audio" );
310 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
311 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
312 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
314 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
315 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
316 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
317 mlt_properties_set_int( meta_media, key, codec_context->channels );
322 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
323 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
324 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
325 mlt_properties_set( meta_media, key, codec->name );
326 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
327 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
328 mlt_properties_set( meta_media, key, codec->long_name );
330 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
331 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
332 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
333 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
334 // snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
335 // mlt_properties_set_int( meta_media, key, codec_context->profile );
336 // snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
337 // mlt_properties_set_int( meta_media, key, codec_context->level );
343 static inline int dv_is_pal( AVPacket *pkt )
345 return pkt->data[3] & 0x80;
348 static int dv_is_wide( AVPacket *pkt )
350 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
352 for ( ; i < pkt->size; i += 5 /* packet size */ )
354 if ( pkt->data[ i ] == 0x61 )
356 uint8_t x = pkt->data[ i + 2 ] & 0x7;
357 return ( x == 2 ) || ( x == 7 );
363 static double get_aspect_ratio( mlt_properties properties, AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
365 double aspect_ratio = 1.0;
367 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
371 if ( dv_is_pal( pkt ) )
373 if ( dv_is_wide( pkt ) )
375 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 64 );
376 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 45 );
380 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 16 );
381 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 15 );
386 if ( dv_is_wide( pkt ) )
388 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 32 );
389 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 27 );
393 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 8 );
394 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 9 );
401 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
402 stream->sample_aspect_ratio;
404 codec_context->sample_aspect_ratio;
406 // Override FFmpeg's notion of DV aspect ratios, which are
407 // based upon a width of 704. Since we do not have a normaliser
408 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
409 // we just coerce the values to facilitate a passive behaviour through
410 // the rescale normaliser when using equivalent producers and consumers.
411 // = display_aspect / (width * height)
412 if ( ar.num == 10 && ar.den == 11 )
415 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 8 );
416 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 9 );
418 else if ( ar.num == 59 && ar.den == 54 )
421 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 16 );
422 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 15 );
424 else if ( ar.num == 40 && ar.den == 33 )
427 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 32 );
428 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 27 );
430 else if ( ar.num == 118 && ar.den == 81 )
433 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 64 );
434 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 45 );
440 AVRational codec_sar = codec_context->sample_aspect_ratio;
441 AVRational stream_sar =
442 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
443 stream->sample_aspect_ratio;
447 if ( codec_sar.num > 0 )
449 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", codec_sar.num );
450 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", codec_sar.den );
452 else if ( stream_sar.num > 0 )
454 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", stream_sar.num );
455 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", stream_sar.den );
459 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", 1 );
460 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", 1 );
463 AVRational ar = { mlt_properties_get_double( properties, "meta.media.sample_aspect_num" ), mlt_properties_get_double( properties, "meta.media.sample_aspect_den" ) };
464 aspect_ratio = av_q2d( ar );
465 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
473 static int producer_open( producer_avformat this, mlt_profile profile, char *file )
475 // Return an error code (0 == no error)
478 // Context for avformat
479 AVFormatContext *context = NULL;
481 // Get the properties
482 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this->parent );
484 // We will treat everything with the producer fps
485 double fps = mlt_profile_fps( profile );
487 // Lock the mutex now
490 // If "MRL", then create AVInputFormat
491 AVInputFormat *format = NULL;
492 AVFormatParameters *params = NULL;
493 char *standard = NULL;
494 char *mrl = strchr( file, ':' );
496 // AV option (0 = both, 1 = video, 2 = audio)
499 // Only if there is not a protocol specification that avformat can handle
500 if ( mrl && !url_exist( file ) )
502 // 'file' becomes format abbreviation
506 format = av_find_input_format( file );
508 // Eat the format designator
514 params = calloc( sizeof( AVFormatParameters ), 1 );
516 // These are required by video4linux (defaults)
518 params->height = 480;
519 params->time_base= (AVRational){1,25};
520 // params->device = file;
521 params->channels = 2;
522 params->sample_rate = 48000;
526 mrl = strchr( file, '?' );
530 char *name = strdup( ++mrl );
531 char *value = strchr( name, ':' );
536 char *t = strchr( value, '&' );
539 if ( !strcmp( name, "frame_rate" ) )
540 params->time_base.den = atoi( value );
541 else if ( !strcmp( name, "frame_rate_base" ) )
542 params->time_base.num = atoi( value );
543 else if ( !strcmp( name, "sample_rate" ) )
544 params->sample_rate = atoi( value );
545 else if ( !strcmp( name, "channels" ) )
546 params->channels = atoi( value );
547 else if ( !strcmp( name, "width" ) )
548 params->width = atoi( value );
549 else if ( !strcmp( name, "height" ) )
550 params->height = atoi( value );
551 else if ( !strcmp( name, "standard" ) )
553 standard = strdup( value );
554 params->standard = standard;
556 else if ( !strcmp( name, "av" ) )
560 mrl = strchr( mrl, '&' );
564 // Now attempt to open the file
565 error = av_open_input_file( &context, file, format, 0, params ) < 0;
567 // Cleanup AVFormatParameters
571 // If successful, then try to get additional info
574 // Get the stream info
575 error = av_find_stream_info( context ) < 0;
577 // Continue if no error
580 // We will default to the first audio and video streams found
581 int audio_index = -1;
582 int video_index = -1;
584 // Now set properties where we can (use default unknowns if required)
585 if ( context->duration != AV_NOPTS_VALUE )
587 // This isn't going to be accurate for all formats
588 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
589 mlt_properties_set_position( properties, "out", frames - 1 );
590 mlt_properties_set_position( properties, "length", frames );
593 // Find default audio and video streams
594 find_default_streams( properties, context, &audio_index, &video_index );
596 if ( context->start_time != AV_NOPTS_VALUE )
597 this->start_time = context->start_time;
599 // Check if we're seekable (something funny about mpeg here :-/)
600 if ( strncmp( file, "pipe:", 5 ) &&
601 strncmp( file, "/dev/", 5 ) &&
602 strncmp( file, "http:", 5 ) &&
603 strncmp( file, "udp:", 4 ) &&
604 strncmp( file, "tcp:", 4 ) &&
605 strncmp( file, "rtsp:", 5 ) &&
606 strncmp( file, "rtp:", 4 ) )
608 this->seekable = av_seek_frame( context, -1, this->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
609 mlt_properties_set_int( properties, "seekable", this->seekable );
610 this->dummy_context = context;
611 av_open_input_file( &context, file, NULL, 0, NULL );
612 av_find_stream_info( context );
615 // Store selected audio and video indexes on properties
616 this->audio_index = audio_index;
617 this->video_index = video_index;
618 this->first_pts = -1;
619 this->last_position = POSITION_INITIAL;
621 // Fetch the width, height and aspect ratio
622 if ( video_index != -1 )
624 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
625 mlt_properties_set_int( properties, "width", codec_context->width );
626 mlt_properties_set_int( properties, "height", codec_context->height );
628 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
630 // Fetch the first frame of DV so we can read it directly
635 ret = av_read_frame( context, &pkt );
636 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
638 get_aspect_ratio( properties, context->streams[ video_index ], codec_context, &pkt );
645 get_aspect_ratio( properties, context->streams[ video_index ], codec_context, NULL );
648 struct SwsContext *context = sws_getContext( codec_context->width, codec_context->height, codec_context->pix_fmt,
649 codec_context->width, codec_context->height, PIX_FMT_YUYV422, SWS_BILINEAR, NULL, NULL, NULL);
651 sws_freeContext( context );
658 if ( context->title )
659 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
660 if ( context->author )
661 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
662 if ( context->copyright )
663 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
664 if ( context->comment )
665 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
666 if ( context->album )
667 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
669 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
670 if ( context->track )
671 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
673 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
674 if ( av == 0 && audio_index != -1 && video_index != -1 )
676 // We'll use the open one as our video_format
677 this->video_format = context;
679 // And open again for our audio context
680 av_open_input_file( &context, file, NULL, 0, NULL );
681 av_find_stream_info( context );
684 this->audio_format = context;
686 else if ( av != 2 && video_index != -1 )
688 // We only have a video context
689 this->video_format = context;
691 else if ( audio_index != -1 )
693 // We only have an audio context
694 this->audio_format = context;
698 // Something has gone wrong
704 // Unlock the mutex now
710 /** Convert a frame position to a time code.
713 static double producer_time_of_frame( mlt_producer this, mlt_position position )
715 return ( double )position / mlt_producer_get_fps( this );
718 // Collect information about all audio streams
720 static void get_audio_streams_info( producer_avformat this )
722 // Fetch the audio format context
723 AVFormatContext *context = this->audio_format;
727 i < context->nb_streams;
730 if ( context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
732 AVCodecContext *codec_context = context->streams[i]->codec;
733 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
735 // If we don't have a codec and we can't initialise it, we can't do much more...
737 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
739 this->audio_streams++;
740 this->audio_max_stream = i;
741 this->total_channels += codec_context->channels;
742 if ( codec_context->channels > this->max_channel )
743 this->max_channel = codec_context->channels;
744 if ( codec_context->sample_rate > this->max_frequency )
745 this->max_frequency = codec_context->sample_rate;
746 avcodec_close( codec_context );
751 mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
752 this->audio_streams, this->audio_max_stream, this->total_channels, this->max_channel );
754 // Other audio-specific initializations
755 this->resample_factor = 1.0;
758 static void set_luma_transfer( struct SwsContext *context, int colorspace, int use_full_range )
760 #if defined(SWSCALE) && (LIBSWSCALE_VERSION_INT >= ((0<<16)+(7<<8)+2))
763 int brightness, contrast, saturation;
765 if ( sws_getColorspaceDetails( context, &coefficients, &full_range, &coefficients, &full_range,
766 &brightness, &contrast, &saturation ) != -1 )
768 // Don't change these from defaults unless explicitly told to.
769 if ( use_full_range >= 0 )
770 full_range = use_full_range;
771 switch ( colorspace )
777 coefficients = sws_getCoefficients( SWS_CS_ITU601 );
780 coefficients = sws_getCoefficients( SWS_CS_SMPTE240M );
783 coefficients = sws_getCoefficients( SWS_CS_ITU709 );
786 sws_setColorspaceDetails( context, coefficients, full_range, coefficients, full_range,
787 brightness, contrast, saturation );
792 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
793 mlt_image_format *format, int width, int height, int colorspace )
797 int flags = SWS_BILINEAR | SWS_ACCURATE_RND;
800 flags |= SWS_CPU_CAPS_MMX;
803 flags |= SWS_CPU_CAPS_MMX2;
806 if ( pix_fmt == PIX_FMT_RGB32 )
808 *format = mlt_image_rgb24a;
809 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
810 width, height, PIX_FMT_RGBA, flags, NULL, NULL, NULL);
812 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
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_yuv420p )
820 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
821 width, height, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
823 output.data[0] = buffer;
824 output.data[1] = buffer + width * height;
825 output.data[2] = buffer + ( 5 * width * height ) / 4;
826 output.linesize[0] = width;
827 output.linesize[1] = width >> 1;
828 output.linesize[2] = width >> 1;
829 set_luma_transfer( context, colorspace, full_range );
830 sws_scale( context, frame->data, frame->linesize, 0, height,
831 output.data, output.linesize);
832 sws_freeContext( context );
834 else if ( *format == mlt_image_rgb24 )
836 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
837 width, height, PIX_FMT_RGB24, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
839 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
840 set_luma_transfer( context, colorspace, full_range );
841 sws_scale( context, frame->data, frame->linesize, 0, height,
842 output.data, output.linesize);
843 sws_freeContext( context );
845 else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
847 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
848 width, height, PIX_FMT_RGBA, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
850 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
851 set_luma_transfer( context, colorspace, full_range );
852 sws_scale( context, frame->data, frame->linesize, 0, height,
853 output.data, output.linesize);
854 sws_freeContext( context );
858 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
859 width, height, PIX_FMT_YUYV422, flags | SWS_FULL_CHR_H_INP, NULL, NULL, NULL);
861 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
862 set_luma_transfer( context, colorspace, full_range );
863 sws_scale( context, frame->data, frame->linesize, 0, height,
864 output.data, output.linesize);
865 sws_freeContext( context );
868 if ( *format == mlt_image_yuv420p )
871 pict.data[0] = buffer;
872 pict.data[1] = buffer + width * height;
873 pict.data[2] = buffer + ( 5 * width * height ) / 4;
874 pict.linesize[0] = width;
875 pict.linesize[1] = width >> 1;
876 pict.linesize[2] = width >> 1;
877 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
879 else if ( *format == mlt_image_rgb24 )
882 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
883 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
885 else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
888 avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
889 img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
894 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
895 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
900 /** Allocate the image buffer and set it on the frame.
903 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
907 if ( codec_context->width == 0 || codec_context->height == 0 )
910 *width = codec_context->width;
911 *height = codec_context->height;
913 if ( codec_context->pix_fmt == PIX_FMT_RGB32 )
914 size = *width * ( *height + 1 ) * 4;
915 else switch ( *format )
917 case mlt_image_yuv420p:
918 size = *width * 3 * ( *height + 1 ) / 2;
920 case mlt_image_rgb24:
921 size = *width * ( *height + 1 ) * 3;
923 case mlt_image_rgb24a:
924 case mlt_image_opengl:
925 size = *width * ( *height + 1 ) * 4;
928 *format = mlt_image_yuv422;
929 size = *width * ( *height + 1 ) * 2;
933 // Construct the output image
934 *buffer = mlt_pool_alloc( size );
936 mlt_properties_set_data( frame_properties, "image", *buffer, size, mlt_pool_release, NULL );
943 /** Get an image from a frame.
946 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
949 producer_avformat this = mlt_frame_pop_service( frame );
950 mlt_service_lock( MLT_PRODUCER_SERVICE(this->parent) );
951 mlt_producer producer = this->parent;
953 // Get the properties from the frame
954 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
956 // Obtain the frame number of this frame
957 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
959 // Get the producer properties
960 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
962 // Fetch the video format context
963 AVFormatContext *context = this->video_format;
965 // Get the video stream
966 AVStream *stream = context->streams[ this->video_index ];
969 AVCodecContext *codec_context = stream->codec;
971 // Get the image cache
972 if ( ! this->image_cache && ! mlt_properties_get_int( properties, "noimagecache" ) )
973 this->image_cache = mlt_cache_init();
974 if ( this->image_cache )
976 mlt_cache_item item = mlt_cache_get( this->image_cache, (void*) position );
977 *buffer = mlt_cache_item_data( item, (int*) format );
980 // Set the resolution
981 *width = codec_context->width;
982 *height = codec_context->height;
984 // Workaround 1088 encodings missing cropping info.
985 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
992 case mlt_image_yuv420p:
993 size = *width * 3 * ( *height + 1 ) / 2;
995 case mlt_image_rgb24:
996 size = *width * ( *height + 1 ) * 3;
998 case mlt_image_rgb24a:
999 case mlt_image_opengl:
1000 size = *width * ( *height + 1 ) * 4;
1003 *format = mlt_image_yuv422;
1004 size = *width * ( *height + 1 ) * 2;
1007 mlt_properties_set_data( frame_properties, "avformat.image_cache", item, 0, ( mlt_destructor )mlt_cache_item_close, NULL );
1008 mlt_properties_set_data( frame_properties, "image", *buffer, size, NULL, NULL );
1009 // this->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
1010 this->got_picture = 1;
1012 goto exit_get_image;
1023 // Special case pause handling flag
1026 // Special case ffwd handling
1029 // We may want to use the source fps if available
1030 double source_fps = mlt_properties_get_double( properties, "meta.media.frame_rate_num" ) /
1031 mlt_properties_get_double( properties, "meta.media.frame_rate_den" );
1032 double fps = mlt_producer_get_fps( producer );
1034 // This is the physical frame position in the source
1035 int req_position = ( int )( position / fps * source_fps + 0.5 );
1037 // Determines if we have to decode all frames in a sequence
1038 // Temporary hack to improve intra frame only
1039 int must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
1040 strcmp( codec_context->codec->name, "dvvideo" ) &&
1041 strcmp( codec_context->codec->name, "huffyuv" ) &&
1042 strcmp( codec_context->codec->name, "mjpeg" ) &&
1043 strcmp( codec_context->codec->name, "rawvideo" );
1045 int last_position = this->last_position;
1047 // Turn on usage of new seek API and PTS for seeking
1048 int use_new_seek = codec_context->codec_id == CODEC_ID_H264 && !strcmp( context->iformat->name, "mpegts" );
1049 if ( mlt_properties_get( properties, "new_seek" ) )
1050 use_new_seek = mlt_properties_get_int( properties, "new_seek" );
1052 // Seek if necessary
1053 if ( position != this->video_expected || last_position < 0 )
1055 if ( this->av_frame && position + 1 == this->video_expected )
1057 // We're paused - use last image
1060 else if ( !this->seekable && position > this->video_expected && ( position - this->video_expected ) < 250 )
1062 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1063 ignore = ( int )( ( position - this->video_expected ) / fps * source_fps );
1064 codec_context->skip_loop_filter = AVDISCARD_NONREF;
1066 else if ( this->seekable && ( position < this->video_expected || position - this->video_expected >= 12 || last_position < 0 ) )
1068 if ( use_new_seek && last_position == POSITION_INITIAL )
1070 // find first key frame
1074 while ( ret >= 0 && toscan-- > 0 )
1076 ret = av_read_frame( context, &pkt );
1077 if ( ret >= 0 && ( pkt.flags & PKT_FLAG_KEY ) && pkt.stream_index == this->video_index )
1079 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) );
1080 this->first_pts = pkt.pts;
1083 av_free_packet( &pkt );
1086 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
1089 // Calculate the timestamp for the requested frame
1093 timestamp = ( req_position - 0.1 / source_fps ) /
1094 ( av_q2d( stream->time_base ) * source_fps );
1095 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "pos %d pts %lld ", req_position, timestamp );
1096 if ( this->first_pts > 0 )
1097 timestamp += this->first_pts;
1098 else if ( context->start_time != AV_NOPTS_VALUE )
1099 timestamp += context->start_time;
1103 timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
1104 if ( context->start_time != AV_NOPTS_VALUE )
1105 timestamp += context->start_time;
1108 timestamp -= AV_TIME_BASE;
1109 if ( timestamp < 0 )
1111 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %lld position %d expected %d last_pos %d\n",
1112 timestamp, position, this->video_expected, last_position );
1114 // Seek to the timestamp
1117 codec_context->skip_loop_filter = AVDISCARD_NONREF;
1118 av_seek_frame( context, this->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
1122 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
1125 // Remove the cached info relating to the previous position
1126 this->current_position = POSITION_INVALID;
1127 this->last_position = POSITION_INVALID;
1128 av_freep( &this->av_frame );
1132 // flush any pictures still in decode buffer
1133 avcodec_flush_buffers( codec_context );
1138 // Duplicate the last image if necessary
1139 if ( this->av_frame && this->av_frame->linesize[0] && this->got_picture && this->seekable
1141 || this->current_position == req_position
1142 || ( !use_new_seek && this->current_position > req_position ) ) )
1145 if ( ( image_size = allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) ) )
1147 // Workaround 1088 encodings missing cropping info.
1148 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1151 if ( this->vdpau && this->vdpau->buffer )
1154 picture.data[0] = this->vdpau->buffer;
1155 picture.data[2] = this->vdpau->buffer + codec_context->width * codec_context->height;
1156 picture.data[1] = this->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1157 picture.linesize[0] = codec_context->width;
1158 picture.linesize[1] = codec_context->width / 2;
1159 picture.linesize[2] = codec_context->width / 2;
1160 convert_image( (AVFrame*) &picture, *buffer,
1161 PIX_FMT_YUV420P, format, *width, *height, this->colorspace );
1165 convert_image( this->av_frame, *buffer, codec_context->pix_fmt,
1166 format, *width, *height, this->colorspace );
1169 mlt_frame_get_image( frame, buffer, format, width, height, writable );
1174 int int_position = 0;
1175 int decode_errors = 0;
1176 int got_picture = 0;
1178 av_init_packet( &pkt );
1180 // Construct an AVFrame for YUV422 conversion
1181 if ( !this->av_frame )
1182 this->av_frame = avcodec_alloc_frame( );
1184 while( ret >= 0 && !got_picture )
1187 ret = av_read_frame( context, &pkt );
1189 // We only deal with video from the selected video_index
1190 if ( ret >= 0 && pkt.stream_index == this->video_index && pkt.size > 0 )
1192 // Determine time code of the packet
1195 int64_t pts = pkt.pts;
1196 if ( this->first_pts > 0 )
1197 pts -= this->first_pts;
1198 else if ( context->start_time != AV_NOPTS_VALUE )
1199 pts -= context->start_time;
1200 int_position = ( int )( av_q2d( stream->time_base ) * pts * source_fps + 0.1 );
1201 if ( pkt.pts == AV_NOPTS_VALUE )
1203 this->invalid_pts_counter++;
1204 if ( this->invalid_pts_counter > 20 )
1206 mlt_log_panic( MLT_PRODUCER_SERVICE(producer), "\ainvalid PTS; DISABLING NEW_SEEK!\n" );
1207 mlt_properties_set_int( properties, "new_seek", 0 );
1208 int_position = req_position;
1214 this->invalid_pts_counter = 0;
1216 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.pts %llu req_pos %d cur_pos %d pkt_pos %d\n",
1217 pkt.pts, req_position, this->current_position, int_position );
1221 if ( pkt.dts != AV_NOPTS_VALUE )
1223 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
1224 if ( context->start_time != AV_NOPTS_VALUE )
1225 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
1226 last_position = this->last_position;
1227 if ( int_position == last_position )
1228 int_position = last_position + 1;
1232 int_position = req_position;
1234 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d\n",
1235 pkt.dts, req_position, this->current_position, int_position );
1236 // Make a dumb assumption on streams that contain wild timestamps
1237 if ( abs( req_position - int_position ) > 999 )
1239 int_position = req_position;
1240 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!" );
1243 this->last_position = int_position;
1246 if ( must_decode || int_position >= req_position )
1249 if ( g_vdpau && this->vdpau )
1251 if ( g_vdpau->producer != this )
1253 vdpau_decoder_close();
1254 vdpau_decoder_init( this );
1257 this->vdpau->is_decoded = 0;
1260 codec_context->reordered_opaque = pkt.pts;
1261 if ( int_position >= req_position )
1262 codec_context->skip_loop_filter = AVDISCARD_NONE;
1263 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1264 ret = avcodec_decode_video2( codec_context, this->av_frame, &got_picture, &pkt );
1266 ret = avcodec_decode_video( codec_context, this->av_frame, &got_picture, pkt.data, pkt.size );
1268 // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1271 if ( ++decode_errors <= 10 )
1284 // Determine time code of the packet
1285 int64_t pts = this->av_frame->reordered_opaque;
1286 if ( this->first_pts > 0 )
1287 pts -= this->first_pts;
1288 else if ( context->start_time != AV_NOPTS_VALUE )
1289 pts -= context->start_time;
1290 int_position = ( int )( av_q2d( stream->time_base) * pts * source_fps + 0.1 );
1291 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "got frame %d, key %d\n", int_position, this->av_frame->key_frame );
1294 if ( int_position < req_position )
1299 else if ( int_position >= req_position )
1302 codec_context->skip_loop_filter = AVDISCARD_NONE;
1304 else if ( ignore -- )
1309 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
1312 // Now handle the picture if we have one
1315 if ( ( image_size = allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) ) )
1317 // Workaround 1088 encodings missing cropping info.
1318 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1323 if ( this->vdpau->is_decoded )
1325 struct vdpau_render_state *render = (struct vdpau_render_state*) this->av_frame->data[0];
1327 uint32_t pitches[3];
1328 VdpYCbCrFormat dest_format = VDP_YCBCR_FORMAT_YV12;
1331 if ( !this->vdpau->buffer )
1332 this->vdpau->buffer = mlt_pool_alloc( codec_context->width * codec_context->height * 3 / 2 );
1333 picture.data[0] = planes[0] = this->vdpau->buffer;
1334 picture.data[2] = planes[1] = this->vdpau->buffer + codec_context->width * codec_context->height;
1335 picture.data[1] = planes[2] = this->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1336 picture.linesize[0] = pitches[0] = codec_context->width;
1337 picture.linesize[1] = pitches[1] = codec_context->width / 2;
1338 picture.linesize[2] = pitches[2] = codec_context->width / 2;
1340 VdpStatus status = vdp_surface_get_bits( render->surface, dest_format, planes, pitches );
1341 if ( status == VDP_STATUS_OK )
1343 convert_image( (AVFrame*) &picture, *buffer, PIX_FMT_YUV420P,
1344 format, *width, *height, this->colorspace );
1348 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU Error: %s\n", vdp_get_error_string( status ) );
1349 this->vdpau->is_decoded = 0;
1354 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU error in VdpDecoderRender\n" );
1360 convert_image( this->av_frame, *buffer, codec_context->pix_fmt,
1361 format, *width, *height, this->colorspace );
1362 this->top_field_first |= this->av_frame->top_field_first;
1363 this->current_position = int_position;
1364 this->got_picture = 1;
1372 av_free_packet( &pkt );
1378 if ( this->got_picture && image_size > 0 && this->image_cache )
1380 // Copy buffer to image cache
1381 uint8_t *image = mlt_pool_alloc( image_size );
1382 memcpy( image, *buffer, image_size );
1383 mlt_cache_put( this->image_cache, (void*) position, image, *format, mlt_pool_release );
1386 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
1387 this->video_expected = position + 1;
1390 // Set the progressive flag
1391 if ( mlt_properties_get( properties, "force_progressive" ) )
1392 mlt_properties_set_int( frame_properties, "progressive", !!mlt_properties_get_int( properties, "force_progressive" ) );
1393 else if ( this->av_frame )
1394 mlt_properties_set_int( frame_properties, "progressive", !this->av_frame->interlaced_frame );
1396 // Set the field order property for this frame
1397 if ( mlt_properties_get( properties, "force_tff" ) )
1398 mlt_properties_set_int( frame_properties, "top_field_first", !!mlt_properties_get_int( properties, "force_tff" ) );
1400 mlt_properties_set_int( frame_properties, "top_field_first", this->top_field_first );
1402 // Set immutable properties of the selected track's (or overridden) source attributes.
1403 mlt_properties_set_int( properties, "meta.media.top_field_first", this->top_field_first );
1404 mlt_properties_set_int( properties, "meta.media.progressive", mlt_properties_get_int( frame_properties, "progressive" ) );
1406 mlt_service_unlock( MLT_PRODUCER_SERVICE(this->parent) );
1408 return !this->got_picture;
1411 /** Process properties as AVOptions and apply to AV context obj
1414 static void apply_properties( void *obj, mlt_properties properties, int flags )
1417 int count = mlt_properties_count( properties );
1418 for ( i = 0; i < count; i++ )
1420 const char *opt_name = mlt_properties_get_name( properties, i );
1421 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
1422 if ( opt_name && mlt_properties_get( properties, opt_name ) )
1425 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
1426 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
1427 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
1428 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1430 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
1436 /** Initialize the video codec context.
1439 static int video_codec_init( producer_avformat this, int index, mlt_properties properties )
1441 // Initialise the codec if necessary
1442 if ( !this->video_codec )
1444 // Get the video stream
1445 AVStream *stream = this->video_format->streams[ index ];
1447 // Get codec context
1448 AVCodecContext *codec_context = stream->codec;
1451 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1453 if ( codec_context->codec_id == CODEC_ID_H264 )
1455 if ( ( codec = avcodec_find_decoder_by_name( "h264_vdpau" ) ) )
1457 if ( vdpau_init( this ) )
1459 this->video_codec = codec_context;
1460 if ( !vdpau_decoder_init( this ) )
1461 vdpau_decoder_close();
1465 codec = avcodec_find_decoder( codec_context->codec_id );
1469 // Initialise multi-threading
1470 int thread_count = mlt_properties_get_int( properties, "threads" );
1471 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1472 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1473 if ( thread_count > 1 )
1475 avcodec_thread_init( codec_context, thread_count );
1476 codec_context->thread_count = thread_count;
1479 // If we don't have a codec and we can't initialise it, we can't do much more...
1481 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1483 // Now store the codec with its destructor
1484 this->video_codec = codec_context;
1488 // Remember that we can't use this later
1489 this->video_index = -1;
1493 // Process properties as AVOptions
1494 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1496 // Reset some image properties
1497 mlt_properties_set_int( properties, "width", this->video_codec->width );
1498 mlt_properties_set_int( properties, "height", this->video_codec->height );
1499 // For DV, we'll just use the saved aspect ratio
1500 if ( codec_context->codec_id != CODEC_ID_DVVIDEO )
1501 get_aspect_ratio( properties, stream, this->video_codec, NULL );
1503 // Determine the fps first from the codec
1504 double source_fps = (double) this->video_codec->time_base.den /
1505 ( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
1507 if ( mlt_properties_get( properties, "force_fps" ) )
1509 source_fps = mlt_properties_get_double( properties, "force_fps" );
1510 stream->time_base = av_d2q( source_fps, 1024 );
1511 mlt_properties_set_int( properties, "meta.media.frame_rate_num", stream->time_base.num );
1512 mlt_properties_set_int( properties, "meta.media.frame_rate_den", stream->time_base.den );
1516 // If the muxer reports a frame rate different than the codec
1517 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1518 double muxer_fps = av_q2d( stream->avg_frame_rate );
1519 if ( muxer_fps == 0 ) muxer_fps = av_q2d( stream->r_frame_rate );
1521 double muxer_fps = av_q2d( stream->r_frame_rate );
1523 // Choose the lesser - the wrong tends to be off by some multiple of 10
1524 source_fps = FFMIN( source_fps, muxer_fps );
1525 if ( source_fps >= 1.0 && ( source_fps < muxer_fps || isnan( muxer_fps ) ) )
1527 mlt_properties_set_int( properties, "meta.media.frame_rate_num", this->video_codec->time_base.den );
1528 mlt_properties_set_int( properties, "meta.media.frame_rate_den", this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
1530 else if ( muxer_fps > 0 )
1532 AVRational frame_rate = stream->r_frame_rate;
1533 // With my samples when r_frame_rate != 1000 but avg_frame_rate is valid,
1534 // avg_frame_rate gives some approximate value that does not well match the media.
1535 // Also, on my sample where r_frame_rate = 1000, using avg_frame_rate directly
1536 // results in some very choppy output, but some value slightly different works
1538 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1539 if ( av_q2d( stream->r_frame_rate ) >= 1000 && av_q2d( stream->avg_frame_rate ) > 0 )
1540 frame_rate = av_d2q( av_q2d( stream->avg_frame_rate ), 1024 );
1542 mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
1543 mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
1547 source_fps = mlt_producer_get_fps( this->parent );
1548 AVRational frame_rate = av_d2q( source_fps, 255 );
1549 mlt_properties_set_int( properties, "meta.media.frame_rate_num", frame_rate.num );
1550 mlt_properties_set_int( properties, "meta.media.frame_rate_den", frame_rate.den );
1554 // source_fps is deprecated in favor of meta.media.frame_rate_num and .frame_rate_den
1555 if ( source_fps > 0 )
1556 mlt_properties_set_double( properties, "source_fps", source_fps );
1558 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this->parent ) );
1560 // Set the YUV colorspace from override or detect
1561 this->colorspace = mlt_properties_get_int( properties, "force_colorspace" );
1562 #if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
1563 if ( ! this->colorspace )
1565 switch ( this->video_codec->colorspace )
1567 case AVCOL_SPC_SMPTE240M:
1568 this->colorspace = 240;
1570 case AVCOL_SPC_BT470BG:
1571 case AVCOL_SPC_SMPTE170M:
1572 this->colorspace = 601;
1574 case AVCOL_SPC_BT709:
1575 this->colorspace = 709;
1578 // This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
1579 this->colorspace = this->video_codec->width * this->video_codec->height > 750000 ? 709 : 601;
1584 // Let apps get chosen colorspace
1585 mlt_properties_set_int( properties, "meta.media.colorspace", this->colorspace );
1587 return this->video_codec && this->video_index > -1;
1590 /** Set up video handling.
1593 static void producer_set_up_video( producer_avformat this, mlt_frame frame )
1596 mlt_producer producer = this->parent;
1598 // Get the properties
1599 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1601 // Fetch the video format context
1602 AVFormatContext *context = this->video_format;
1604 // Get the video_index
1605 int index = mlt_properties_get_int( properties, "video_index" );
1607 // Reopen the file if necessary
1608 if ( !context && index > -1 )
1610 mlt_events_block( properties, producer );
1611 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1612 mlt_properties_get( properties, "resource" ) );
1613 context = this->video_format;
1614 if ( this->dummy_context )
1617 av_close_input_file( this->dummy_context );
1620 this->dummy_context = NULL;
1621 mlt_events_unblock( properties, producer );
1622 if ( this->audio_format )
1623 get_audio_streams_info( this );
1625 // Process properties as AVOptions
1626 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
1629 // Exception handling for video_index
1630 if ( context && index >= (int) context->nb_streams )
1632 // Get the last video stream
1633 for ( index = context->nb_streams - 1;
1634 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO;
1636 mlt_properties_set_int( properties, "video_index", index );
1638 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
1640 // Invalidate the video stream
1642 mlt_properties_set_int( properties, "video_index", index );
1645 // Update the video properties if the index changed
1646 if ( index != this->video_index )
1648 // Reset the video properties if the index changed
1649 this->video_index = index;
1650 if ( this->video_codec )
1653 avcodec_close( this->video_codec );
1656 this->video_codec = NULL;
1659 // Get the frame properties
1660 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1663 if ( context && index > -1 && video_codec_init( this, index, properties ) )
1665 // Set the frame properties
1666 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1667 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1668 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1670 // Set the width and height
1671 mlt_properties_set_int( frame_properties, "width", this->video_codec->width );
1672 mlt_properties_set_int( frame_properties, "height", this->video_codec->height );
1673 // real_width and real_height are deprecated in favor of meta.media.width and .height
1674 mlt_properties_set_int( properties, "meta.media.width", this->video_codec->width );
1675 mlt_properties_set_int( properties, "meta.media.height", this->video_codec->height );
1676 mlt_properties_set_int( frame_properties, "real_width", this->video_codec->width );
1677 mlt_properties_set_int( frame_properties, "real_height", this->video_codec->height );
1678 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1679 mlt_properties_set_int( frame_properties, "colorspace", this->colorspace );
1681 // Workaround 1088 encodings missing cropping info.
1682 if ( this->video_codec->height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1684 mlt_properties_set_int( properties, "meta.media.height", 1080 );
1685 mlt_properties_set_int( frame_properties, "real_height", 1080 );
1688 // Add our image operation
1689 mlt_frame_push_service( frame, this );
1690 mlt_frame_push_get_image( frame, producer_get_image );
1694 // If something failed, use test card image
1695 mlt_properties_set_int( frame_properties, "test_image", 1 );
1699 static int seek_audio( producer_avformat this, mlt_position position, double timecode, int *ignore )
1703 // Seek if necessary
1704 if ( position != this->audio_expected )
1706 if ( position + 1 == this->audio_expected )
1708 // We're paused - silence required
1711 else if ( !this->seekable && position > this->audio_expected && ( position - this->audio_expected ) < 250 )
1713 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1714 *ignore = position - this->audio_expected;
1716 else if ( position < this->audio_expected || position - this->audio_expected >= 12 )
1718 AVFormatContext *context = this->audio_format;
1719 int64_t timestamp = ( int64_t )( timecode * AV_TIME_BASE + 0.5 );
1720 if ( context->start_time != AV_NOPTS_VALUE )
1721 timestamp += context->start_time;
1722 if ( timestamp < 0 )
1725 // Set to the real timecode
1727 if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
1731 // Clear the usage in the audio buffer
1732 int i = MAX_AUDIO_STREAMS + 1;
1734 this->audio_used[i - 1] = 0;
1740 static int decode_audio( producer_avformat this, int *ignore, AVPacket pkt, int channels, int samples, double timecode, double fps )
1742 // Fetch the audio_format
1743 AVFormatContext *context = this->audio_format;
1745 // Get the current stream index
1746 int index = pkt.stream_index;
1748 // Get codec context
1749 AVCodecContext *codec_context = this->audio_codec[ index ];
1751 // Obtain the resample context if it exists (not always needed)
1752 ReSampleContext *resample = this->audio_resample[ index ];
1754 // Obtain the audio buffers
1755 int16_t *audio_buffer = this->audio_buffer[ index ];
1756 int16_t *decode_buffer = this->decode_buffer[ index ];
1758 int audio_used = this->audio_used[ index ];
1759 uint8_t *ptr = pkt.data;
1763 while ( ptr && ret >= 0 && len > 0 )
1765 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1768 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1769 ret = avcodec_decode_audio3( codec_context, decode_buffer, &data_size, &pkt );
1770 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1771 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1773 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1777 mlt_log_warning( MLT_PRODUCER_SERVICE(this->parent), "audio decoding error %d\n", ret );
1781 pkt.size = len -= ret;
1782 pkt.data = ptr += ret;
1784 // If decoded successfully
1785 if ( data_size > 0 )
1787 // Figure out how many samples will be needed after resampling
1788 int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
1789 int samples_needed = this->resample_factor * convert_samples + 1;
1791 // Resize audio buffer to prevent overflow
1792 if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
1794 this->audio_buffer_size[ index ] *= 2;
1795 audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
1799 // Copy to audio buffer while resampling
1800 int16_t *source = decode_buffer;
1801 int16_t *dest = &audio_buffer[ audio_used * channels ];
1803 audio_used += audio_resample( resample, dest, source, convert_samples );
1808 // Straight copy to audio buffer
1809 memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
1810 audio_used += convert_samples;
1814 while ( *ignore && audio_used > samples )
1817 audio_used -= samples;
1818 memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
1819 audio_used * sizeof( int16_t ) );
1824 // If we're behind, ignore this packet
1827 double current_pts = av_q2d( context->streams[ index ]->time_base ) * pkt.pts;
1828 int req_position = ( int )( timecode * fps + 0.5 );
1829 int int_position = ( int )( current_pts * fps + 0.5 );
1830 if ( context->start_time != AV_NOPTS_VALUE )
1831 int_position -= ( int )( fps * context->start_time / AV_TIME_BASE + 0.5 );
1833 if ( this->seekable && *ignore == 0 )
1835 if ( int_position < req_position )
1836 // We are behind, so skip some
1838 else if ( int_position > req_position + 2 )
1839 // We are ahead, so seek backwards some more
1840 seek_audio( this, req_position, timecode - 1.0, ignore );
1844 this->audio_used[ index ] = audio_used;
1849 /** Get the audio from a frame.
1852 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1855 producer_avformat this = mlt_frame_pop_audio( frame );
1857 mlt_service_lock( MLT_PRODUCER_SERVICE(this->parent) );
1859 // Obtain the frame number of this frame
1860 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" );
1862 // Calculate the real time code
1863 double real_timecode = producer_time_of_frame( this->parent, position );
1865 // Get the producer fps
1866 double fps = mlt_producer_get_fps( this->parent );
1868 // Number of frames to ignore (for ffwd)
1871 // Flag for paused (silence)
1872 int paused = seek_audio( this, position, real_timecode, &ignore );
1874 // Fetch the audio_format
1875 AVFormatContext *context = this->audio_format;
1877 // Determine the tracks to use
1878 int index = this->audio_index;
1879 int index_max = this->audio_index + 1;
1880 if ( this->audio_index == INT_MAX )
1883 index_max = context->nb_streams;
1884 *channels = this->total_channels;
1885 *frequency = this->max_frequency;
1888 // Initialize the resamplers and buffers
1889 for ( ; index < index_max; index++ )
1891 // Get codec context
1892 AVCodecContext *codec_context = this->audio_codec[ index ];
1894 if ( codec_context && !this->audio_buffer[ index ] )
1896 // Check for resample and create if necessary
1897 if ( codec_context->channels <= 2 )
1899 // Determine by how much resampling will increase number of samples
1900 double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
1901 resample_factor *= (double) *frequency / codec_context->sample_rate;
1902 if ( resample_factor > this->resample_factor )
1903 this->resample_factor = resample_factor;
1905 // Create the resampler
1906 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
1907 this->audio_resample[ index ] = av_audio_resample_init(
1908 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1909 codec_context->channels, *frequency, codec_context->sample_rate,
1910 SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
1912 this->audio_resample[ index ] = audio_resample_init(
1913 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1914 codec_context->channels, *frequency, codec_context->sample_rate );
1919 codec_context->request_channels = this->audio_index == INT_MAX ? codec_context->channels : *channels;
1922 // Check for audio buffer and create if necessary
1923 this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1924 this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
1926 // Check for decoder buffer and create if necessary
1927 this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1931 // Get the audio if required
1938 av_init_packet( &pkt );
1940 // If not resampling, give consumer more than requested.
1941 // It requested number samples based on requested frame rate.
1942 // Do not clean this up with a samples *= ...!
1943 if ( this->audio_index != INT_MAX && ! this->audio_resample[ this->audio_index ] )
1944 *samples = *samples * this->audio_codec[ this->audio_index ]->sample_rate / *frequency;
1946 while ( ret >= 0 && !got_audio )
1948 // Check if the buffer already contains the samples required
1949 if ( this->audio_index != INT_MAX && this->audio_used[ this->audio_index ] >= *samples && ignore == 0 )
1957 ret = av_read_frame( context, &pkt );
1960 // We only deal with audio from the selected audio index
1961 if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
1962 ( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
1964 int channels2 = this->audio_index == INT_MAX ? this->audio_codec[pkt.stream_index]->channels : *channels;
1965 ret = decode_audio( this, &ignore, pkt, channels2, *samples, real_timecode, fps );
1967 av_free_packet( &pkt );
1969 if ( this->audio_index == INT_MAX && ret >= 0 )
1971 // Determine if there is enough audio for all streams
1973 for ( index = 0; index < context->nb_streams; index++ )
1975 if ( this->audio_codec[ index ] && this->audio_used[ index ] < *samples )
1981 // Allocate and set the frame's audio buffer
1982 int size = *samples * *channels * sizeof(int16_t);
1983 *buffer = mlt_pool_alloc( size );
1984 *format = mlt_audio_s16;
1985 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
1987 // Interleave tracks if audio_index=all
1988 if ( this->audio_index == INT_MAX )
1990 int16_t *dest = *buffer;
1992 for ( i = 0; i < *samples; i++ )
1994 for ( index = 0; index < index_max; index++ )
1995 if ( this->audio_codec[ index ] )
1997 int current_channels = this->audio_codec[ index ]->channels;
1998 int16_t *src = this->audio_buffer[ index ] + i * current_channels;
1999 memcpy( dest, src, current_channels * sizeof(int16_t) );
2000 dest += current_channels;
2003 for ( index = 0; index < index_max; index++ )
2004 if ( this->audio_codec[ index ] && this->audio_used[ index ] >= *samples )
2006 int current_channels = this->audio_codec[ index ]->channels;
2007 int16_t *src = this->audio_buffer[ index ] + *samples * current_channels;
2008 this->audio_used[index] -= *samples;
2009 memmove( this->audio_buffer[ index ], src, this->audio_used[ index ] * current_channels * sizeof(int16_t) );
2012 // Copy a single track to the output buffer
2015 index = this->audio_index;
2017 // Now handle the audio if we have enough
2018 if ( this->audio_used[ index ] > 0 )
2020 int16_t *src = this->audio_buffer[ index ];
2021 *samples = this->audio_used[ index ] < *samples ? this->audio_used[ index ] : *samples;
2022 memcpy( *buffer, src, *samples * *channels * sizeof(int16_t) );
2023 this->audio_used[ index ] -= *samples;
2024 memmove( src, &src[ *samples * *channels ], this->audio_used[ index ] * *channels * sizeof(int16_t) );
2028 // Otherwise fill with silence
2029 memset( *buffer, 0, *samples * *channels * sizeof(int16_t) );
2031 if ( !this->audio_resample[ index ] )
2033 // TODO: uncomment and remove following line when full multi-channel support is ready
2034 // *channels = codec_context->channels;
2035 *frequency = this->audio_codec[ index ]->sample_rate;
2041 // Get silence and don't touch the context
2042 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
2045 // Regardless of speed (other than paused), we expect to get the next frame
2047 this->audio_expected = position + 1;
2049 mlt_service_unlock( MLT_PRODUCER_SERVICE(this->parent) );
2054 /** Initialize the audio codec context.
2057 static int audio_codec_init( producer_avformat this, int index, mlt_properties properties )
2059 // Initialise the codec if necessary
2060 if ( !this->audio_codec[ index ] )
2062 // Get codec context
2063 AVCodecContext *codec_context = this->audio_format->streams[index]->codec;
2066 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
2068 // If we don't have a codec and we can't initialise it, we can't do much more...
2070 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
2072 // Now store the codec with its destructor
2073 if ( this->audio_codec[ index ] )
2074 avcodec_close( this->audio_codec[ index ] );
2075 this->audio_codec[ index ] = codec_context;
2079 // Remember that we can't use this later
2080 this->audio_index = -1;
2084 // Process properties as AVOptions
2085 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
2087 return this->audio_codec[ index ] && this->audio_index > -1;
2090 /** Set up audio handling.
2093 static void producer_set_up_audio( producer_avformat this, mlt_frame frame )
2096 mlt_producer producer = this->parent;
2098 // Get the properties
2099 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
2101 // Fetch the audio format context
2102 AVFormatContext *context = this->audio_format;
2104 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
2106 // Get the audio_index
2107 int index = mlt_properties_get_int( properties, "audio_index" );
2109 // Handle all audio tracks
2110 if ( this->audio_index > -1 &&
2111 mlt_properties_get( properties, "audio_index" ) &&
2112 !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
2115 // Reopen the file if necessary
2116 if ( !context && this->audio_index > -1 && index > -1 )
2118 mlt_events_block( properties, producer );
2119 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
2120 mlt_properties_get( properties, "resource" ) );
2121 context = this->audio_format;
2122 if ( this->dummy_context )
2125 av_close_input_file( this->dummy_context );
2128 this->dummy_context = NULL;
2129 mlt_events_unblock( properties, producer );
2130 get_audio_streams_info( this );
2133 // Exception handling for audio_index
2134 if ( context && index >= (int) context->nb_streams && index < INT_MAX )
2136 for ( index = context->nb_streams - 1;
2137 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO;
2139 mlt_properties_set_int( properties, "audio_index", index );
2141 if ( context && index > -1 && index < INT_MAX &&
2142 context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
2144 index = this->audio_index;
2145 mlt_properties_set_int( properties, "audio_index", index );
2148 // Update the audio properties if the index changed
2149 if ( context && index > -1 && index != this->audio_index )
2151 if ( this->audio_codec[ this->audio_index ] )
2154 avcodec_close( this->audio_codec[ this->audio_index ] );
2157 this->audio_codec[ this->audio_index ] = NULL;
2159 if ( this->audio_index != -1 )
2160 this->audio_index = index;
2165 if ( context && index == INT_MAX )
2167 mlt_properties_set_int( frame_properties, "frequency", this->max_frequency );
2168 mlt_properties_set_int( frame_properties, "channels", this->total_channels );
2169 for ( index = 0; index < context->nb_streams; index++ )
2171 if ( context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO )
2172 audio_codec_init( this, index, properties );
2175 else if ( context && index > -1 && audio_codec_init( this, index, properties ) )
2177 // Set the frame properties
2178 if ( index < INT_MAX )
2180 mlt_properties_set_int( frame_properties, "frequency", this->audio_codec[ index ]->sample_rate );
2181 mlt_properties_set_int( frame_properties, "channels", this->audio_codec[ index ]->channels );
2184 if ( context && index > -1 )
2186 // Add our audio operation
2187 mlt_frame_push_audio( frame, this );
2188 mlt_frame_push_audio( frame, producer_get_audio );
2192 /** Our get frame implementation.
2195 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
2197 // Access the private data
2198 mlt_service service = MLT_PRODUCER_SERVICE( producer );
2199 mlt_cache_item cache_item = mlt_service_cache_get( service, "producer_avformat" );
2200 producer_avformat this = mlt_cache_item_data( cache_item, NULL );
2205 this = calloc( 1, sizeof( struct producer_avformat_s ) );
2206 producer->child = this;
2207 this->parent = producer;
2208 mlt_service_cache_put( service, "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
2209 cache_item = mlt_service_cache_get( service, "producer_avformat" );
2212 // Create an empty frame
2213 *frame = mlt_frame_init( service);
2217 mlt_properties_set_data( MLT_FRAME_PROPERTIES(*frame), "avformat_cache", cache_item, 0, (mlt_destructor) mlt_cache_item_close, NULL );
2221 mlt_cache_item_close( cache_item );
2225 // Update timecode on the frame we're creating
2226 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
2228 // Set the position of this producer
2229 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( producer ) );
2232 producer_set_up_video( this, *frame );
2235 producer_set_up_audio( this, *frame );
2237 // Calculate the next timecode
2238 mlt_producer_prepare_next( producer );
2243 static void producer_avformat_close( producer_avformat this )
2245 mlt_log_debug( NULL, "producer_avformat_close\n" );
2247 av_free( this->av_frame );
2250 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
2252 if ( this->audio_resample[i] )
2253 audio_resample_close( this->audio_resample[i] );
2254 mlt_pool_release( this->audio_buffer[i] );
2255 av_free( this->decode_buffer[i] );
2256 if ( this->audio_codec[i] )
2257 avcodec_close( this->audio_codec[i] );
2259 if ( this->video_codec )
2260 avcodec_close( this->video_codec );
2261 if ( this->dummy_context )
2262 av_close_input_file( this->dummy_context );
2263 if ( this->audio_format )
2264 av_close_input_file( this->audio_format );
2265 if ( this->video_format )
2266 av_close_input_file( this->video_format );
2269 vdpau_producer_close( this );
2271 if ( this->image_cache )
2272 mlt_cache_close( this->image_cache );
2276 static void producer_close( mlt_producer parent )
2279 parent->close = NULL;
2280 mlt_producer_close( parent );