]> git.sesse.net Git - mlt/blob - src/modules/avformat/producer_avformat.c
2d0be647bc3c5fe446c602d8cf597cd945eb70ee
[mlt] / src / modules / avformat / producer_avformat.c
1 /*
2  * producer_avformat.c -- avformat producer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 // MLT Header files
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
25 #include <framework/mlt_profile.h>
26 #include <framework/mlt_log.h>
27
28 // ffmpeg Header files
29 #include <avformat.h>
30 #include <opt.h>
31 #ifdef SWSCALE
32 #  include <swscale.h>
33 #endif
34 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
35 #  include "audioconvert.h"
36 #endif
37
38 // System header files
39 #include <stdlib.h>
40 #include <string.h>
41 #include <pthread.h>
42 #include <limits.h>
43
44 #if LIBAVUTIL_VERSION_INT < (50<<16)
45 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
46 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
47 #endif
48
49 #define POSITION_INITIAL (-2)
50 #define POSITION_INVALID (-1)
51
52 #define MAX_AUDIO_STREAMS (8)
53
54 void avformat_lock( );
55 void avformat_unlock( );
56
57 struct producer_avformat_s
58 {
59         struct mlt_producer_s parent;
60         AVFormatContext *dummy_context;
61         AVFormatContext *audio_format;
62         AVFormatContext *video_format;
63         AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ];
64         AVCodecContext *video_codec;
65         AVFrame *av_frame;
66         ReSampleContext *audio_resample[ MAX_AUDIO_STREAMS ];
67         mlt_position audio_expected;
68         mlt_position video_expected;
69         int audio_index;
70         int video_index;
71         double start_time;
72         int first_pts;
73         int last_position;
74         int seekable;
75         int current_position;
76         int got_picture;
77         int top_field_first;
78         int16_t *audio_buffer[ MAX_AUDIO_STREAMS ];
79         size_t audio_buffer_size[ MAX_AUDIO_STREAMS ];
80         int16_t *decode_buffer[ MAX_AUDIO_STREAMS ];
81         int audio_used[ MAX_AUDIO_STREAMS ];
82         int audio_streams;
83         int audio_max_stream;
84         int total_channels;
85         int max_channel;
86         int max_frequency;
87         unsigned int invalid_pts_counter;
88         double resample_factor;
89 };
90 typedef struct producer_avformat_s *producer_avformat;
91
92 // Forward references.
93 static int producer_open( producer_avformat this, mlt_profile profile, char *file );
94 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
95 static void producer_format_close( void *context );
96 static void producer_close( mlt_producer parent );
97
98 /** Constructor for libavformat.
99 */
100
101 mlt_producer producer_avformat_init( mlt_profile profile, char *file )
102 {
103         int skip = 0;
104
105         // Report information about available demuxers and codecs as YAML Tiny
106         if ( file && strstr( file, "f-list" ) )
107         {
108                 fprintf( stderr, "---\nformats:\n" );
109                 AVInputFormat *format = NULL;
110                 while ( ( format = av_iformat_next( format ) ) )
111                         fprintf( stderr, "  - %s\n", format->name );
112                 fprintf( stderr, "...\n" );
113                 skip = 1;
114         }
115         if ( file && strstr( file, "acodec-list" ) )
116         {
117                 fprintf( stderr, "---\naudio_codecs:\n" );
118                 AVCodec *codec = NULL;
119                 while ( ( codec = av_codec_next( codec ) ) )
120                         if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
121                                 fprintf( stderr, "  - %s\n", codec->name );
122                 fprintf( stderr, "...\n" );
123                 skip = 1;
124         }
125         if ( file && strstr( file, "vcodec-list" ) )
126         {
127                 fprintf( stderr, "---\nvideo_codecs:\n" );
128                 AVCodec *codec = NULL;
129                 while ( ( codec = av_codec_next( codec ) ) )
130                         if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
131                                 fprintf( stderr, "  - %s\n", codec->name );
132                 fprintf( stderr, "...\n" );
133                 skip = 1;
134         }
135
136         // Check that we have a non-NULL argument
137         if ( !skip && file )
138         {
139                 // Construct the producer
140                 producer_avformat this = calloc( 1, sizeof( struct producer_avformat_s ) );
141
142                 // Initialise it
143                 if ( mlt_producer_init( &this->parent, this ) == 0 )
144                 {
145                         mlt_producer producer = &this->parent;
146
147                         // Get the properties
148                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
149
150                         // Set the resource property (required for all producers)
151                         mlt_properties_set( properties, "resource", file );
152
153                         // Register transport implementation with the producer
154                         producer->close = (mlt_destructor) producer_close;
155
156                         // Register our get_frame implementation
157                         producer->get_frame = producer_get_frame;
158                         
159                         this->resample_factor = 1.0;
160
161                         // Open the file
162                         if ( producer_open( this, profile, file ) != 0 )
163                         {
164                                 // Clean up
165                                 mlt_producer_close( producer );
166                                 producer = NULL;
167                         }
168                         else
169                         {
170                                 // Close the file to release resources for large playlists - reopen later as needed
171                                 producer_format_close( this->dummy_context );
172                                 this->dummy_context = NULL;
173                                 producer_format_close( this->audio_format );
174                                 this->audio_format = NULL;
175                                 producer_format_close( this->video_format );
176                                 this->video_format = NULL;
177
178                                 // Default the user-selectable indices from the auto-detected indices
179                                 mlt_properties_set_int( properties, "audio_index",  this->audio_index );
180                                 mlt_properties_set_int( properties, "video_index",  this->video_index );
181                         }
182                         return producer;
183                 }
184         }
185         return NULL;
186 }
187
188 /** Find the default streams.
189 */
190
191 static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatContext *context, int *audio_index, int *video_index )
192 {
193         int i;
194         char key[200];
195
196         mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
197
198         // Allow for multiple audio and video streams in the file and select first of each (if available)
199         for( i = 0; i < context->nb_streams; i++ )
200         {
201                 // Get the codec context
202                 AVStream *stream = context->streams[ i ];
203                 if ( ! stream ) continue;
204                 AVCodecContext *codec_context = stream->codec;
205                 if ( ! codec_context ) continue;
206                 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
207                 if ( ! codec ) continue;
208
209                 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
210
211                 // Determine the type and obtain the first index of each type
212                 switch( codec_context->codec_type )
213                 {
214                         case CODEC_TYPE_VIDEO:
215                                 if ( *video_index < 0 )
216                                         *video_index = i;
217                                 mlt_properties_set( meta_media, key, "video" );
218                                 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
219                                 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
220 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
221                                 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
222                                 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
223 #endif
224                                 snprintf( key, sizeof(key), "meta.media.%d.codec.frame_rate", i );
225                                 mlt_properties_set_double( meta_media, key, (double) codec_context->time_base.den /
226                                                                                    ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num ) );
227                                 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
228                                 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
229                                 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
230                                 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
231                                 break;
232                         case CODEC_TYPE_AUDIO:
233                                 if ( *audio_index < 0 )
234                                         *audio_index = i;
235                                 mlt_properties_set( meta_media, key, "audio" );
236 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
237                                 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
238                                 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
239 #endif
240                                 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
241                                 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
242                                 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
243                                 mlt_properties_set_int( meta_media, key, codec_context->channels );
244                                 break;
245                         default:
246                                 break;
247                 }
248 //              snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
249 //              mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
250                 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
251                 mlt_properties_set( meta_media, key, codec->name );
252 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
253                 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
254                 mlt_properties_set( meta_media, key, codec->long_name );
255 #endif
256                 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
257                 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
258 //              snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
259 //              mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
260 //              snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
261 //              mlt_properties_set_int( meta_media, key, codec_context->profile );
262 //              snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
263 //              mlt_properties_set_int( meta_media, key, codec_context->level );
264         }
265
266         return meta_media;
267 }
268
269 /** Producer file destructor.
270 */
271
272 static void producer_format_close( void *context )
273 {
274         if ( context )
275         {
276                 // Lock the mutex now
277                 avformat_lock( );
278
279                 // Close the file
280                 av_close_input_file( context );
281
282                 // Unlock the mutex now
283                 avformat_unlock( );
284         }
285 }
286
287 /** Producer file destructor.
288 */
289
290 static void producer_codec_close( void *codec )
291 {
292         if ( codec )
293         {
294                 // Lock the mutex now
295                 avformat_lock( );
296
297                 // Close the file
298                 avcodec_close( codec );
299
300                 // Unlock the mutex now
301                 avformat_unlock( );
302         }
303 }
304
305 static inline int dv_is_pal( AVPacket *pkt )
306 {
307         return pkt->data[3] & 0x80;
308 }
309
310 static int dv_is_wide( AVPacket *pkt )
311 {
312         int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
313
314         for ( ; i < pkt->size; i += 5 /* packet size */ )
315         {
316                 if ( pkt->data[ i ] == 0x61 )
317                 {
318                         uint8_t x = pkt->data[ i + 2 ] & 0x7;
319                         return ( x == 2 ) || ( x == 7 );
320                 }
321         }
322         return 0;
323 }
324
325 static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
326 {
327         double aspect_ratio = 1.0;
328
329         if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
330         {
331                 if ( pkt )
332                 {
333                         if ( dv_is_pal( pkt ) )
334                         {
335                                 aspect_ratio = dv_is_wide( pkt )
336                                         ? 64.0/45.0 // 16:9 PAL
337                                         : 16.0/15.0; // 4:3 PAL
338                         }
339                         else
340                         {
341                                 aspect_ratio = dv_is_wide( pkt )
342                                         ? 32.0/27.0 // 16:9 NTSC
343                                         : 8.0/9.0; // 4:3 NTSC
344                         }
345                 }
346                 else
347                 {
348                         AVRational ar =
349 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
350                                 stream->sample_aspect_ratio;
351 #else
352                                 codec_context->sample_aspect_ratio;
353 #endif
354                         // Override FFmpeg's notion of DV aspect ratios, which are
355                         // based upon a width of 704. Since we do not have a normaliser
356                         // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
357                         // we just coerce the values to facilitate a passive behaviour through
358                         // the rescale normaliser when using equivalent producers and consumers.
359                         // = display_aspect / (width * height)
360                         if ( ar.num == 10 && ar.den == 11 )
361                                 aspect_ratio = 8.0/9.0; // 4:3 NTSC
362                         else if ( ar.num == 59 && ar.den == 54 )
363                                 aspect_ratio = 16.0/15.0; // 4:3 PAL
364                         else if ( ar.num == 40 && ar.den == 33 )
365                                 aspect_ratio = 32.0/27.0; // 16:9 NTSC
366                         else if ( ar.num == 118 && ar.den == 81 )
367                                 aspect_ratio = 64.0/45.0; // 16:9 PAL
368                 }
369         }
370         else
371         {
372                 AVRational codec_sar = codec_context->sample_aspect_ratio;
373                 AVRational stream_sar =
374 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
375                         stream->sample_aspect_ratio;
376 #else
377                         { 0, 1 };
378 #endif
379                 if ( codec_sar.num > 0 )
380                         aspect_ratio = av_q2d( codec_sar );
381                 else if ( stream_sar.num > 0 )
382                         aspect_ratio = av_q2d( stream_sar );
383         }
384         return aspect_ratio;
385 }
386
387 /** Open the file.
388 */
389
390 static int producer_open( producer_avformat this, mlt_profile profile, char *file )
391 {
392         // Return an error code (0 == no error)
393         int error = 0;
394
395         // Context for avformat
396         AVFormatContext *context = NULL;
397
398         // Get the properties
399         mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
400
401         // We will treat everything with the producer fps
402         double fps = mlt_profile_fps( profile );
403
404         // Lock the mutex now
405         avformat_lock( );
406
407         // If "MRL", then create AVInputFormat
408         AVInputFormat *format = NULL;
409         AVFormatParameters *params = NULL;
410         char *standard = NULL;
411         char *mrl = strchr( file, ':' );
412
413         // AV option (0 = both, 1 = video, 2 = audio)
414         int av = 0;
415
416         // Only if there is not a protocol specification that avformat can handle
417         if ( mrl && !url_exist( file ) )
418         {
419                 // 'file' becomes format abbreviation
420                 mrl[0] = 0;
421
422                 // Lookup the format
423                 format = av_find_input_format( file );
424
425                 // Eat the format designator
426                 file = ++mrl;
427
428                 if ( format )
429                 {
430                         // Allocate params
431                         params = calloc( sizeof( AVFormatParameters ), 1 );
432
433                         // These are required by video4linux (defaults)
434                         params->width = 640;
435                         params->height = 480;
436                         params->time_base= (AVRational){1,25};
437                         // params->device = file;
438                         params->channels = 2;
439                         params->sample_rate = 48000;
440                 }
441
442                 // XXX: this does not work anymore since avdevice
443                 // TODO: make producer_avddevice?
444                 // Parse out params
445                 mrl = strchr( file, '?' );
446                 while ( mrl )
447                 {
448                         mrl[0] = 0;
449                         char *name = strdup( ++mrl );
450                         char *value = strchr( name, ':' );
451                         if ( value )
452                         {
453                                 value[0] = 0;
454                                 value++;
455                                 char *t = strchr( value, '&' );
456                                 if ( t )
457                                         t[0] = 0;
458                                 if ( !strcmp( name, "frame_rate" ) )
459                                         params->time_base.den = atoi( value );
460                                 else if ( !strcmp( name, "frame_rate_base" ) )
461                                         params->time_base.num = atoi( value );
462                                 else if ( !strcmp( name, "sample_rate" ) )
463                                         params->sample_rate = atoi( value );
464                                 else if ( !strcmp( name, "channels" ) )
465                                         params->channels = atoi( value );
466                                 else if ( !strcmp( name, "width" ) )
467                                         params->width = atoi( value );
468                                 else if ( !strcmp( name, "height" ) )
469                                         params->height = atoi( value );
470                                 else if ( !strcmp( name, "standard" ) )
471                                 {
472                                         standard = strdup( value );
473                                         params->standard = standard;
474                                 }
475                                 else if ( !strcmp( name, "av" ) )
476                                         av = atoi( value );
477                         }
478                         free( name );
479                         mrl = strchr( mrl, '&' );
480                 }
481         }
482
483         // Now attempt to open the file
484         error = av_open_input_file( &context, file, format, 0, params ) < 0;
485
486         // Cleanup AVFormatParameters
487         free( standard );
488         free( params );
489
490         // If successful, then try to get additional info
491         if ( !error )
492         {
493                 // Get the stream info
494                 error = av_find_stream_info( context ) < 0;
495
496                 // Continue if no error
497                 if ( !error )
498                 {
499                         // We will default to the first audio and video streams found
500                         int audio_index = -1;
501                         int video_index = -1;
502
503                         // Now set properties where we can (use default unknowns if required)
504                         if ( context->duration != AV_NOPTS_VALUE )
505                         {
506                                 // This isn't going to be accurate for all formats
507                                 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
508                                 mlt_properties_set_position( properties, "out", frames - 1 );
509                                 mlt_properties_set_position( properties, "length", frames );
510                         }
511
512                         // Find default audio and video streams
513                         find_default_streams( properties, context, &audio_index, &video_index );
514
515                         if ( context->start_time != AV_NOPTS_VALUE )
516                                 this->start_time = context->start_time;
517
518                         // Check if we're seekable (something funny about mpeg here :-/)
519                         if ( strncmp( file, "pipe:", 5 ) &&
520                                  strncmp( file, "http:", 5 ) &&
521                                  strncmp( file, "udp:", 4 )  &&
522                                  strncmp( file, "tcp:", 4 )  &&
523                                  strncmp( file, "rtsp:", 5 ) &&
524                                  strncmp( file, "rtp:", 4 ) )
525                         {
526                                 this->seekable = av_seek_frame( context, -1, this->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
527                                 mlt_properties_set_int( properties, "seekable", this->seekable );
528                                 producer_format_close( this->dummy_context );
529                                 this->dummy_context = context;
530                                 av_open_input_file( &context, file, NULL, 0, NULL );
531                                 av_find_stream_info( context );
532                         }
533
534                         // Store selected audio and video indexes on properties
535                         this->audio_index = audio_index;
536                         this->video_index = video_index;
537                         this->first_pts = -1;
538                         this->last_position = POSITION_INITIAL;
539
540                         // Fetch the width, height and aspect ratio
541                         if ( video_index != -1 )
542                         {
543                                 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
544                                 mlt_properties_set_int( properties, "width", codec_context->width );
545                                 mlt_properties_set_int( properties, "height", codec_context->height );
546
547                                 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
548                                 {
549                                         // Fetch the first frame of DV so we can read it directly
550                                         AVPacket pkt;
551                                         int ret = 0;
552                                         while ( ret >= 0 )
553                                         {
554                                                 ret = av_read_frame( context, &pkt );
555                                                 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
556                                                 {
557                                                         mlt_properties_set_double( properties, "aspect_ratio",
558                                                                 get_aspect_ratio( context->streams[ video_index ], codec_context, &pkt ) );
559                                                         break;
560                                                 }
561                                         }
562                                 }
563                                 else
564                                 {
565                                         mlt_properties_set_double( properties, "aspect_ratio",
566                                                 get_aspect_ratio( context->streams[ video_index ], codec_context, NULL ) );
567                                 }
568                         }
569
570                         // Read Metadata
571                         if ( context->title )
572                                 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
573                         if ( context->author )
574                                 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
575                         if ( context->copyright )
576                                 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
577                         if ( context->comment )
578                                 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
579                         if ( context->album )
580                                 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
581                         if ( context->year )
582                                 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
583                         if ( context->track )
584                                 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
585
586                         // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
587                         if ( av == 0 && audio_index != -1 && video_index != -1 )
588                         {
589                                 // We'll use the open one as our video_format
590                                 avformat_unlock();
591                                 producer_format_close( this->video_format );
592                                 avformat_lock();
593                                 this->video_format = context;
594
595                                 // And open again for our audio context
596                                 av_open_input_file( &context, file, NULL, 0, NULL );
597                                 av_find_stream_info( context );
598
599                                 // Audio context
600                                 avformat_unlock();
601                                 producer_format_close( this->audio_format );
602                                 avformat_lock();
603                                 this->audio_format = context;
604                         }
605                         else if ( av != 2 && video_index != -1 )
606                         {
607                                 // We only have a video context
608                                 avformat_unlock();
609                                 producer_format_close( this->video_format );
610                                 avformat_lock();
611                                 this->video_format = context;
612                         }
613                         else if ( audio_index != -1 )
614                         {
615                                 // We only have an audio context
616                                 avformat_unlock();
617                                 producer_format_close( this->audio_format );
618                                 avformat_lock();
619                                 this->audio_format = context;
620                         }
621                         else
622                         {
623                                 // Something has gone wrong
624                                 error = -1;
625                         }
626                 }
627         }
628
629         // Unlock the mutex now
630         avformat_unlock( );
631
632         return error;
633 }
634
635 /** Convert a frame position to a time code.
636 */
637
638 static double producer_time_of_frame( mlt_producer this, mlt_position position )
639 {
640         return ( double )position / mlt_producer_get_fps( this );
641 }
642
643                 // Collect information about all audio streams
644
645 static void get_audio_streams_info( producer_avformat this )
646 {
647         // Fetch the audio format context
648         AVFormatContext *context = this->audio_format;
649         int i;
650
651         for ( i = 0;
652                   i < context->nb_streams;
653                   i++ )
654         {
655                 if ( context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
656                 {
657                         AVCodecContext *codec_context = context->streams[i]->codec;
658                         AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
659
660                         // If we don't have a codec and we can't initialise it, we can't do much more...
661                         avformat_lock( );
662                         if ( codec && avcodec_open( codec_context, codec ) >= 0 )
663                         {
664                                 this->audio_streams++;
665                                 this->audio_max_stream = i;
666                                 this->total_channels += codec_context->channels;
667                                 if ( codec_context->channels > this->max_channel )
668                                         this->max_channel = codec_context->channels;
669                                 if ( codec_context->sample_rate > this->max_frequency )
670                                         this->max_frequency = codec_context->sample_rate;
671                                 avcodec_close( codec_context );
672                         }
673                         avformat_unlock( );
674                 }
675         }
676         mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
677                 this->audio_streams, this->audio_max_stream, this->total_channels, this->max_channel );
678 }
679
680 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format *format, int width, int height )
681 {
682 #ifdef SWSCALE
683         if ( pix_fmt == PIX_FMT_RGB32 )
684         {
685                 *format = mlt_image_rgb24a;
686                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
687                         width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
688                 AVPicture output;
689                 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
690                 sws_scale( context, frame->data, frame->linesize, 0, height,
691                         output.data, output.linesize);
692                 sws_freeContext( context );
693         }
694         else if ( *format == mlt_image_yuv420p )
695         {
696                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
697                         width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
698                 AVPicture output;
699                 output.data[0] = buffer;
700                 output.data[1] = buffer + width * height;
701                 output.data[2] = buffer + ( 5 * width * height ) / 4;
702                 output.linesize[0] = width;
703                 output.linesize[1] = width >> 1;
704                 output.linesize[2] = width >> 1;
705                 sws_scale( context, frame->data, frame->linesize, 0, height,
706                         output.data, output.linesize);
707                 sws_freeContext( context );
708         }
709         else if ( *format == mlt_image_rgb24 )
710         {
711                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
712                         width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
713                 AVPicture output;
714                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
715                 sws_scale( context, frame->data, frame->linesize, 0, height,
716                         output.data, output.linesize);
717                 sws_freeContext( context );
718         }
719         else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
720         {
721                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
722                         width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
723                 AVPicture output;
724                 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
725                 sws_scale( context, frame->data, frame->linesize, 0, height,
726                         output.data, output.linesize);
727                 sws_freeContext( context );
728         }
729         else
730         {
731                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
732                         width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
733                 AVPicture output;
734                 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
735                 sws_scale( context, frame->data, frame->linesize, 0, height,
736                         output.data, output.linesize);
737                 sws_freeContext( context );
738         }
739 #else
740         if ( *format == mlt_image_yuv420p )
741         {
742                 AVPicture pict;
743                 pict.data[0] = buffer;
744                 pict.data[1] = buffer + width * height;
745                 pict.data[2] = buffer + ( 5 * width * height ) / 4;
746                 pict.linesize[0] = width;
747                 pict.linesize[1] = width >> 1;
748                 pict.linesize[2] = width >> 1;
749                 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
750         }
751         else if ( *format == mlt_image_rgb24 )
752         {
753                 AVPicture output;
754                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
755                 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
756         }
757         else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
758         {
759                 AVPicture output;
760                 avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
761                 img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
762         }
763         else
764         {
765                 AVPicture output;
766                 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
767                 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
768         }
769 #endif
770 }
771
772 /** Allocate the image buffer and set it on the frame.
773 */
774
775 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
776 {
777         int size = 0;
778
779         if ( codec_context->width == 0 || codec_context->height == 0 )
780                 return size;
781
782         *width = codec_context->width;
783         *height = codec_context->height;
784         mlt_properties_set_int( frame_properties, "width", *width );
785         mlt_properties_set_int( frame_properties, "height", *height );
786
787         if ( codec_context->pix_fmt == PIX_FMT_RGB32 )
788                 size = *width * ( *height + 1 ) * 4;
789         else switch ( *format )
790         {
791                 case mlt_image_yuv420p:
792                         size = *width * 3 * ( *height + 1 ) / 2;
793                         break;
794                 case mlt_image_rgb24:
795                         size = *width * ( *height + 1 ) * 3;
796                         break;
797                 case mlt_image_rgb24a:
798                 case mlt_image_opengl:
799                         size = *width * ( *height + 1 ) * 4;
800                         break;
801                 default:
802                         *format = mlt_image_yuv422;
803                         size = *width * ( *height + 1 ) * 2;
804                         break;
805         }
806
807         // Construct the output image
808         *buffer = mlt_pool_alloc( size );
809         if ( *buffer )
810                 mlt_properties_set_data( frame_properties, "image", *buffer, size, mlt_pool_release, NULL );
811         else
812                 size = 0;
813
814         return size;
815 }
816
817 /** Get an image from a frame.
818 */
819
820 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
821 {
822         // Get the producer
823         producer_avformat this = mlt_frame_pop_service( frame );
824         mlt_producer producer = &this->parent;
825
826         // Get the properties from the frame
827         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
828
829         // Obtain the frame number of this frame
830         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
831
832         // Get the producer properties
833         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
834
835         avformat_lock();
836
837         // Fetch the video format context
838         AVFormatContext *context = this->video_format;
839
840         // Get the video stream
841         AVStream *stream = context->streams[ this->video_index ];
842
843         // Get codec context
844         AVCodecContext *codec_context = stream->codec;
845
846         // Packet
847         AVPacket pkt;
848
849         // Special case pause handling flag
850         int paused = 0;
851
852         // Special case ffwd handling
853         int ignore = 0;
854
855         // We may want to use the source fps if available
856         double source_fps = mlt_properties_get_double( properties, "source_fps" );
857         double fps = mlt_producer_get_fps( producer );
858
859         // This is the physical frame position in the source
860         int req_position = ( int )( position / fps * source_fps + 0.5 );
861
862         // Determines if we have to decode all frames in a sequence
863         // Temporary hack to improve intra frame only
864         int must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
865                                   strcmp( codec_context->codec->name, "dvvideo" ) &&
866                                   strcmp( codec_context->codec->name, "huffyuv" ) &&
867                                   strcmp( codec_context->codec->name, "mjpeg" ) &&
868                                   strcmp( codec_context->codec->name, "rawvideo" );
869
870         int last_position = this->last_position;
871
872         // Turn on usage of new seek API and PTS for seeking
873         int use_new_seek = codec_context->codec_id == CODEC_ID_H264 && !strcmp( context->iformat->name, "mpegts" );
874         if ( mlt_properties_get( properties, "new_seek" ) )
875                 use_new_seek = mlt_properties_get_int( properties, "new_seek" );
876
877         // Seek if necessary
878         if ( position != this->video_expected || last_position < 0 )
879         {
880                 if ( this->av_frame && position + 1 == this->video_expected )
881                 {
882                         // We're paused - use last image
883                         paused = 1;
884                 }
885                 else if ( !this->seekable && position > this->video_expected && ( position - this->video_expected ) < 250 )
886                 {
887                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
888                         ignore = ( int )( ( position - this->video_expected ) / fps * source_fps );
889                         codec_context->skip_loop_filter = AVDISCARD_NONREF;
890                 }
891                 else if ( this->seekable && ( position < this->video_expected || position - this->video_expected >= 12 || last_position < 0 ) )
892                 {
893                         if ( use_new_seek && last_position == POSITION_INITIAL )
894                         {
895                                 // find first key frame
896                                 int ret = 0;
897                                 int toscan = 100;
898
899                                 while ( ret >= 0 && toscan-- > 0 )
900                                 {
901                                         ret = av_read_frame( context, &pkt );
902                                         if ( ret >= 0 && ( pkt.flags & PKT_FLAG_KEY ) && pkt.stream_index == this->video_index )
903                                         {
904                                                 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) );
905                                                 this->first_pts = pkt.pts;
906                                                 toscan = 0;
907                                         }
908                                         av_free_packet( &pkt );
909                                 }
910                                 // Rewind
911                                 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
912                         }
913
914                         // Calculate the timestamp for the requested frame
915                         int64_t timestamp;
916                         if ( use_new_seek )
917                         {
918                                 timestamp = ( req_position - 0.1 / source_fps ) /
919                                         ( av_q2d( stream->time_base ) * source_fps );
920                                 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "pos %d pts %lld ", req_position, timestamp );
921                                 if ( this->first_pts > 0 )
922                                         timestamp += this->first_pts;
923                                 else if ( context->start_time != AV_NOPTS_VALUE )
924                                         timestamp += context->start_time;
925                         }
926                         else
927                         {
928                                 timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
929                                 if ( context->start_time != AV_NOPTS_VALUE )
930                                         timestamp += context->start_time;
931                         }
932                         if ( must_decode )
933                                 timestamp -= AV_TIME_BASE;
934                         if ( timestamp < 0 )
935                                 timestamp = 0;
936                         mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %lld position %d expected %d last_pos %d\n",
937                                 timestamp, position, this->video_expected, last_position );
938
939                         // Seek to the timestamp
940                         if ( use_new_seek )
941                         {
942                                 codec_context->skip_loop_filter = AVDISCARD_NONREF;
943                                 av_seek_frame( context, this->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
944                         }
945                         else
946                         {
947                                 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
948                         }
949
950                         // Remove the cached info relating to the previous position
951                         this->current_position = POSITION_INVALID;
952                         this->last_position = POSITION_INVALID;
953                         av_freep( &this->av_frame );
954
955                         if ( use_new_seek )
956                         {
957                                 // flush any pictures still in decode buffer
958                                 avcodec_flush_buffers( codec_context );
959                         }
960                 }
961         }
962
963         // Duplicate the last image if necessary (see comment on rawvideo below)
964         if ( this->av_frame && this->got_picture && this->seekable
965                  && ( paused
966                           || this->current_position == req_position
967                           || ( !use_new_seek && this->current_position > req_position ) ) )
968         {
969                 // Duplicate it
970                 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
971                         convert_image( this->av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
972                 else
973                         mlt_frame_get_image( frame, buffer, format, width, height, writable );
974         }
975         else
976         {
977                 int ret = 0;
978                 int int_position = 0;
979                 int decode_errors = 0;
980                 int got_picture = 0;
981
982                 av_init_packet( &pkt );
983
984                 // Construct an AVFrame for YUV422 conversion
985                 if ( !this->av_frame )
986                         this->av_frame = avcodec_alloc_frame( );
987
988                 while( ret >= 0 && !got_picture )
989                 {
990                         // Read a packet
991                         ret = av_read_frame( context, &pkt );
992
993                         // We only deal with video from the selected video_index
994                         if ( ret >= 0 && pkt.stream_index == this->video_index && pkt.size > 0 )
995                         {
996                                 // Determine time code of the packet
997                                 if ( use_new_seek )
998                                 {
999                                         int64_t pts = pkt.pts;
1000                                         if ( this->first_pts > 0 )
1001                                                 pts -= this->first_pts;
1002                                         else if ( context->start_time != AV_NOPTS_VALUE )
1003                                                 pts -= context->start_time;
1004                                         int_position = ( int )( av_q2d( stream->time_base ) * pts * source_fps + 0.1 );
1005                                         if ( pkt.pts == AV_NOPTS_VALUE )
1006                                         {
1007                                                 this->invalid_pts_counter++;
1008                                                 if ( this->invalid_pts_counter > 20 )
1009                                                 {
1010                                                         mlt_log_panic( MLT_PRODUCER_SERVICE(producer), "\ainvalid PTS; DISABLING NEW_SEEK!\n" );
1011                                                         mlt_properties_set_int( properties, "new_seek", 0 );
1012                                                         int_position = req_position;
1013                                                         use_new_seek = 0;
1014                                                 }
1015                                         }
1016                                         else
1017                                         {
1018                                                 this->invalid_pts_counter = 0;
1019                                         }
1020                                         mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.pts %llu req_pos %d cur_pos %d pkt_pos %d",
1021                                                 pkt.pts, req_position, this->current_position, int_position );
1022                                 }
1023                                 else
1024                                 {
1025                                         if ( pkt.dts != AV_NOPTS_VALUE )
1026                                         {
1027                                                 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
1028                                                 if ( context->start_time != AV_NOPTS_VALUE )
1029                                                         int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
1030                                                 last_position = this->last_position;
1031                                                 if ( int_position == last_position )
1032                                                         int_position = last_position + 1;
1033                                         }
1034                                         else
1035                                         {
1036                                                 int_position = req_position;
1037                                         }
1038                                         mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d",
1039                                                 pkt.dts, req_position, this->current_position, int_position );
1040                                         // Make a dumb assumption on streams that contain wild timestamps
1041                                         if ( abs( req_position - int_position ) > 999 )
1042                                         {
1043                                                 int_position = req_position;
1044                                                 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!" );
1045                                         }
1046                                 }
1047                                 this->last_position = int_position;
1048
1049                                 // Decode the image
1050                                 if ( must_decode || int_position >= req_position )
1051                                 {
1052                                         codec_context->reordered_opaque = pkt.pts;
1053                                         if ( int_position >= req_position )
1054                                                 codec_context->skip_loop_filter = AVDISCARD_NONE;
1055 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1056                                         ret = avcodec_decode_video2( codec_context, this->av_frame, &got_picture, &pkt );
1057 #else
1058                                         ret = avcodec_decode_video( codec_context, this->av_frame, &got_picture, pkt.data, pkt.size );
1059 #endif
1060                                         // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1061                                         if ( ret < 0 )
1062                                         {
1063                                                 if ( ++decode_errors <= 10 )
1064                                                         ret = 0;
1065                                         }
1066                                         else
1067                                         {
1068                                                 decode_errors = 0;
1069                                         }
1070                                 }
1071
1072                                 if ( got_picture )
1073                                 {
1074                                         if ( use_new_seek )
1075                                         {
1076                                                 // Determine time code of the packet
1077                                                 int64_t pts = this->av_frame->reordered_opaque;
1078                                                 if ( this->first_pts > 0 )
1079                                                         pts -= this->first_pts;
1080                                                 else if ( context->start_time != AV_NOPTS_VALUE )
1081                                                         pts -= context->start_time;
1082                                                 int_position = ( int )( av_q2d( stream->time_base) * pts * source_fps + 0.1 );
1083                                                 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "got frame %d, key %d\n", int_position, this->av_frame->key_frame );
1084                                         }
1085                                         // Handle ignore
1086                                         if ( int_position < req_position )
1087                                         {
1088                                                 ignore = 0;
1089                                                 got_picture = 0;
1090                                         }
1091                                         else if ( int_position >= req_position )
1092                                         {
1093                                                 ignore = 0;
1094                                                 codec_context->skip_loop_filter = AVDISCARD_NONE;
1095                                         }
1096                                         else if ( ignore -- )
1097                                         {
1098                                                 got_picture = 0;
1099                                         }
1100                                 }
1101                                 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
1102                                 av_free_packet( &pkt );
1103                         }
1104                         else if ( ret >= 0 )
1105                         {
1106                                 av_free_packet( &pkt );
1107                         }
1108
1109                         // Now handle the picture if we have one
1110                         if ( got_picture )
1111                         {
1112                                 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
1113                                 {
1114                                         convert_image( this->av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
1115                                         if ( !mlt_properties_get( properties, "force_progressive" ) )
1116                                                 mlt_properties_set_int( frame_properties, "progressive", !this->av_frame->interlaced_frame );
1117                                         this->top_field_first |= this->av_frame->top_field_first;
1118                                         this->current_position = int_position;
1119                                         this->got_picture = 1;
1120                                 }
1121                                 else
1122                                 {
1123                                         got_picture = 0;
1124                                 }
1125                         }
1126                 }
1127                 if ( !got_picture )
1128                         mlt_frame_get_image( frame, buffer, format, width, height, writable );
1129         }
1130
1131         // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
1132         // above will break the pause behaviour - so we wipe the frame now
1133         if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
1134                 av_freep( &this->av_frame );
1135
1136         avformat_unlock();
1137
1138         // Set the field order property for this frame
1139         mlt_properties_set_int( frame_properties, "top_field_first", this->top_field_first );
1140
1141         // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
1142         this->video_expected = position + 1;
1143
1144         return 0;
1145 }
1146
1147 /** Process properties as AVOptions and apply to AV context obj
1148 */
1149
1150 static void apply_properties( void *obj, mlt_properties properties, int flags )
1151 {
1152         int i;
1153         int count = mlt_properties_count( properties );
1154         for ( i = 0; i < count; i++ )
1155         {
1156                 const char *opt_name = mlt_properties_get_name( properties, i );
1157                 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
1158                 if ( opt )
1159 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
1160                         av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
1161 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
1162                         av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1163 #else
1164                         av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
1165 #endif
1166         }
1167 }
1168
1169 /** Initialize the video codec context.
1170  */
1171
1172 static int video_codec_init( producer_avformat this, int index, mlt_properties properties )
1173 {
1174         // Initialise the codec if necessary
1175         if ( !this->video_codec )
1176         {
1177                 // Get the video stream
1178                 AVStream *stream = this->video_format->streams[ index ];
1179
1180                 // Get codec context
1181                 AVCodecContext *codec_context = stream->codec;
1182
1183                 // Find the codec
1184                 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1185
1186                 // Initialise multi-threading
1187                 int thread_count = mlt_properties_get_int( properties, "threads" );
1188                 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1189                         thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1190                 if ( thread_count > 1 )
1191                 {
1192                         avcodec_thread_init( codec_context, thread_count );
1193                         codec_context->thread_count = thread_count;
1194                 }
1195
1196                 // If we don't have a codec and we can't initialise it, we can't do much more...
1197                 avformat_lock( );
1198                 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1199                 {
1200                         // Now store the codec with its destructor
1201                         producer_codec_close( this->video_codec );
1202                         this->video_codec = codec_context;
1203                 }
1204                 else
1205                 {
1206                         // Remember that we can't use this later
1207                         this->video_index = -1;
1208                 }
1209                 avformat_unlock( );
1210
1211                 // Process properties as AVOptions
1212                 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1213
1214                 // Reset some image properties
1215                 mlt_properties_set_int( properties, "width", this->video_codec->width );
1216                 mlt_properties_set_int( properties, "height", this->video_codec->height );
1217                 mlt_properties_set_double( properties, "aspect_ratio", get_aspect_ratio( stream, this->video_codec, NULL ) );
1218
1219                 // Determine the fps first from the codec
1220                 double source_fps = (double) this->video_codec->time_base.den /
1221                                                                    ( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
1222                 
1223                 if ( mlt_properties_get( properties, "force_fps" ) )
1224                 {
1225                         source_fps = mlt_properties_get_double( properties, "force_fps" );
1226                         stream->time_base = av_d2q( source_fps, 255 );
1227                 }
1228                 else
1229                 {
1230                         // If the muxer reports a frame rate different than the codec
1231                         double muxer_fps = av_q2d( stream->r_frame_rate );
1232                         // Choose the lesser - the wrong tends to be off by some multiple of 10
1233                         source_fps = FFMIN( source_fps, muxer_fps );
1234                 }
1235
1236                 // We'll use fps if it's available
1237                 if ( source_fps > 0 )
1238                         mlt_properties_set_double( properties, "source_fps", source_fps );
1239                 else
1240                         mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( &this->parent ) );
1241         }
1242         return this->video_codec && this->video_index > -1;
1243 }
1244
1245 /** Set up video handling.
1246 */
1247
1248 static void producer_set_up_video( producer_avformat this, mlt_frame frame )
1249 {
1250         // Get the producer
1251         mlt_producer producer = &this->parent;
1252
1253         // Get the properties
1254         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1255
1256         // Fetch the video format context
1257         AVFormatContext *context = this->video_format;
1258
1259         // Get the video_index
1260         int index = mlt_properties_get_int( properties, "video_index" );
1261
1262         // Reopen the file if necessary
1263         if ( !context && index > -1 )
1264         {
1265                 mlt_events_block( properties, producer );
1266                 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1267                         mlt_properties_get( properties, "resource" ) );
1268                 context = this->video_format;
1269                 producer_format_close( this->dummy_context );
1270                 this->dummy_context = NULL;
1271                 mlt_events_unblock( properties, producer );
1272                 if ( this->audio_format )
1273                         get_audio_streams_info( this );
1274
1275                 // Process properties as AVOptions
1276                 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
1277         }
1278
1279         // Exception handling for video_index
1280         if ( context && index >= (int) context->nb_streams )
1281         {
1282                 // Get the last video stream
1283                 for ( index = context->nb_streams - 1;
1284                           index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO;
1285                           index-- );
1286                 mlt_properties_set_int( properties, "video_index", index );
1287         }
1288         if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
1289         {
1290                 // Invalidate the video stream
1291                 index = -1;
1292                 mlt_properties_set_int( properties, "video_index", index );
1293         }
1294
1295         // Update the video properties if the index changed
1296         if ( index != this->video_index )
1297         {
1298                 // Reset the video properties if the index changed
1299                 this->video_index = index;
1300                 producer_codec_close( this->video_codec );
1301                 this->video_codec = NULL;
1302         }
1303
1304         // Get the frame properties
1305         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1306
1307         // Get the codec
1308         if ( context && index > -1 && video_codec_init( this, index, properties ) )
1309         {
1310                 // Set the frame properties
1311                 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1312                 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1313                         force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1314
1315                 // Set the width and height
1316                 mlt_properties_set_int( frame_properties, "width", this->video_codec->width );
1317                 mlt_properties_set_int( frame_properties, "height", this->video_codec->height );
1318                 mlt_properties_set_int( frame_properties, "real_width", this->video_codec->width );
1319                 mlt_properties_set_int( frame_properties, "real_height", this->video_codec->height );
1320                 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1321                 if ( mlt_properties_get( properties, "force_progressive" ) )
1322                         mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( properties, "force_progressive" ) );
1323
1324                 // Add our image operation
1325                 mlt_frame_push_service( frame, this );
1326                 mlt_frame_push_get_image( frame, producer_get_image );
1327         }
1328         else
1329         {
1330                 // If something failed, use test card image
1331                 mlt_properties_set_int( frame_properties, "test_image", 1 );
1332         }
1333 }
1334
1335 static int seek_audio( producer_avformat this, mlt_position position, double timecode, int *ignore )
1336 {
1337         int paused = 0;
1338
1339         // Fetch the audio_format
1340         AVFormatContext *context = this->audio_format;
1341
1342         // Seek if necessary
1343         if ( position != this->audio_expected )
1344         {
1345                 if ( position + 1 == this->audio_expected )
1346                 {
1347                         // We're paused - silence required
1348                         paused = 1;
1349                 }
1350                 else if ( !this->seekable && position > this->audio_expected && ( position - this->audio_expected ) < 250 )
1351                 {
1352                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
1353                         *ignore = position - this->audio_expected;
1354                 }
1355                 else if ( position < this->audio_expected || position - this->audio_expected >= 12 )
1356                 {
1357                         int64_t timestamp = ( int64_t )( timecode * AV_TIME_BASE + 0.5 );
1358                         if ( context->start_time != AV_NOPTS_VALUE )
1359                                 timestamp += context->start_time;
1360                         if ( timestamp < 0 )
1361                                 timestamp = 0;
1362
1363                         // Set to the real timecode
1364                         if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
1365                                 paused = 1;
1366
1367                         // Clear the usage in the audio buffer
1368                         int i = MAX_AUDIO_STREAMS + 1;
1369                         while ( --i )
1370                                 this->audio_used[i - 1] = 0;
1371                 }
1372         }
1373         return paused;
1374 }
1375
1376 static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int channels, int samples, double timecode, double source_fps )
1377 {
1378         // Fetch the audio_format
1379         AVFormatContext *context = this->audio_format;
1380
1381         // Get the current stream index
1382         int index = pkt->stream_index;
1383
1384         // Get codec context
1385         AVCodecContext *codec_context = this->audio_codec[ index ];
1386
1387         // Obtain the resample context if it exists (not always needed)
1388         ReSampleContext *resample = this->audio_resample[ index ];
1389
1390         // Obtain the audio buffers
1391         int16_t *audio_buffer = this->audio_buffer[ index ];
1392         int16_t *decode_buffer = this->decode_buffer[ index ];
1393
1394         int audio_used = this->audio_used[ index ];
1395         uint8_t *ptr = pkt->data;
1396         int len = pkt->size;
1397         int ret = 0;
1398
1399         while ( ptr && ret >= 0 && len > 0 )
1400         {
1401                 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1402
1403                 // Decode the audio
1404 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1405                 ret = avcodec_decode_audio3( codec_context, decode_buffer, &data_size, pkt );
1406 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1407                 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1408 #else
1409                 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1410 #endif
1411                 if ( ret < 0 )
1412                 {
1413                         ret = 0;
1414                         break;
1415                 }
1416
1417                 len -= ret;
1418                 ptr += ret;
1419
1420                 // If decoded successfully
1421                 if ( data_size > 0 )
1422                 {
1423                         // Figure out how many samples will be needed after resampling
1424                         int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
1425                         int samples_needed = lrint( this->resample_factor * convert_samples );
1426                         
1427                         // Resize audio buffer to prevent overflow
1428                         if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
1429                         {
1430                                 this->audio_buffer_size[ index ] *= 2;
1431                                 audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
1432                         }
1433                         if ( resample )
1434                         {
1435                                 // Copy to audio buffer while resampling
1436                                 int16_t *source = decode_buffer;
1437                                 int16_t *dest = &audio_buffer[ audio_used * channels ];
1438                                 audio_used += audio_resample( resample, dest, source, convert_samples );
1439                         }
1440                         else
1441                         {
1442                                 // Straight copy to audio buffer
1443                                 memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
1444                                 audio_used += convert_samples;
1445                         }
1446
1447                         // Handle ignore
1448                         while ( *ignore && audio_used > samples )
1449                         {
1450                                 *ignore -= 1;
1451                                 audio_used -= samples;
1452                                 memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
1453                                          audio_used * sizeof( int16_t ) );
1454                         }
1455                 }
1456         }
1457
1458         // If we're behind, ignore this packet
1459         if ( pkt->pts >= 0 )
1460         {
1461                 double current_pts = av_q2d( context->streams[ index ]->time_base ) * pkt->pts;
1462                 int req_position = ( int )( timecode * source_fps + 0.5 );
1463                 int int_position = ( int )( current_pts * source_fps + 0.5 );
1464
1465                 if ( context->start_time != AV_NOPTS_VALUE )
1466                         int_position -= ( int )( source_fps * context->start_time / AV_TIME_BASE + 0.5 );
1467                 if ( this->seekable && *ignore == 0 && int_position < req_position )
1468                         *ignore = 1;
1469         }
1470
1471         this->audio_used[ index ] = audio_used;
1472
1473         return ret;
1474 }
1475
1476 /** Get the audio from a frame.
1477 */
1478
1479 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1480 {
1481         // Get the producer
1482         producer_avformat this = mlt_frame_pop_audio( frame );
1483
1484         // Obtain the frame number of this frame
1485         mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" );
1486
1487         // Calculate the real time code
1488         double real_timecode = producer_time_of_frame( &this->parent, position );
1489
1490         // Get the source fps
1491         double source_fps = mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( &this->parent ), "source_fps" );
1492
1493         // Number of frames to ignore (for ffwd)
1494         int ignore = 0;
1495
1496         // Flag for paused (silence)
1497         int paused = seek_audio( this, position, real_timecode, &ignore );
1498
1499         // Fetch the audio_format
1500         AVFormatContext *context = this->audio_format;
1501         
1502         // Determine the tracks to use
1503         int index = this->audio_index;
1504         int index_max = this->audio_index + 1;
1505         if ( this->audio_index == INT_MAX )
1506         {
1507                 index = 0;
1508                 index_max = context->nb_streams;
1509                 *channels = this->total_channels;
1510                 *frequency = this->max_frequency;
1511         }
1512
1513         // Initialize the resamplers and buffers
1514         for ( ; index < index_max; index++ )
1515         {
1516                 // Get codec context
1517                 AVCodecContext *codec_context = this->audio_codec[ index ];
1518
1519                 if ( codec_context && !this->audio_buffer[ index ] )
1520                 {
1521                         // Check for resample and create if necessary
1522                         if ( codec_context->channels <= 2 )
1523                         {
1524                                 // Determine by how much resampling will increase number of samples
1525                                 double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
1526                                 resample_factor *= (double) *frequency / codec_context->sample_rate;
1527                                 if ( resample_factor > this->resample_factor )
1528                                         this->resample_factor = resample_factor;
1529                                 
1530                                 // Create the resampler
1531 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
1532                                 this->audio_resample[ index ] = av_audio_resample_init(
1533                                         this->audio_index == INT_MAX ? codec_context->channels : *channels,
1534                                         codec_context->channels, *frequency, codec_context->sample_rate,
1535                                         SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
1536 #else
1537                                 this->audio_resample[ index ] = audio_resample_init(
1538                                         this->audio_index == INT_MAX ? codec_context->channels : *channels,
1539                                         codec_context->channels, *frequency, codec_context->sample_rate );
1540 #endif
1541                         }
1542                         else
1543                         {
1544                                 codec_context->request_channels = this->audio_index == INT_MAX ? codec_context->channels : *channels;
1545                         }
1546
1547                         // Check for audio buffer and create if necessary
1548                         this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1549                         this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
1550
1551                         // Check for decoder buffer and create if necessary
1552                         this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1553                 }
1554         }
1555
1556         // Get the audio if required
1557         if ( !paused )
1558         {
1559                 int ret = 0;
1560                 int got_audio = 0;
1561                 AVPacket pkt;
1562
1563                 av_init_packet( &pkt );
1564
1565                 while ( ret >= 0 && !got_audio )
1566                 {
1567                         // Check if the buffer already contains the samples required
1568                         if ( this->audio_index != INT_MAX && this->audio_used[ this->audio_index ] >= *samples && ignore == 0 )
1569                         {
1570                                 got_audio = 1;
1571                                 break;
1572                         }
1573
1574                         // Read a packet
1575                         ret = av_read_frame( context, &pkt );
1576
1577                         // We only deal with audio from the selected audio index
1578                         if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
1579                                  ( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
1580                                 ret = decode_audio( this, &ignore, &pkt, *channels, *samples, real_timecode, source_fps );
1581                         av_free_packet( &pkt );
1582
1583                         if ( this->audio_index == INT_MAX && ret >= 0 )
1584                         {
1585                                 // Determine if there is enough audio for all streams
1586                                 got_audio = 1;
1587                                 for ( index = 0; index < context->nb_streams; index++ )
1588                                 {
1589                                         if ( this->audio_codec[ index ] && this->audio_used[ index ] < *samples )
1590                                                 got_audio = 0;
1591                                 }
1592                         }
1593                 }
1594
1595                 // Allocate and set the frame's audio buffer
1596                 int size = *samples * *channels * sizeof(int16_t);
1597                 *buffer = mlt_pool_alloc( size );
1598                 *format = mlt_audio_s16;
1599                 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
1600
1601                 // Interleave tracks if audio_index=all
1602                 if ( this->audio_index == INT_MAX )
1603                 {
1604                         int16_t *dest = *buffer;
1605                         int i;
1606                         for ( i = 0; i < *samples; i++ )
1607                         {
1608                                 for ( index = 0; index < index_max; index++ )
1609                                 if ( this->audio_codec[ index ] )
1610                                 {
1611                                         int current_channels = this->audio_codec[ index ]->channels;
1612                                         int16_t *src = this->audio_buffer[ index ] + i * current_channels;
1613                                         memcpy( dest, src, current_channels * sizeof(int16_t) );
1614                                         dest += current_channels;
1615                                 }
1616                         }
1617                         for ( index = 0; index < index_max; index++ )
1618                         if ( this->audio_codec[ index ] )
1619                         {
1620                                 int current_channels = this->audio_codec[ index ]->channels;
1621                                 int16_t *src = this->audio_buffer[ index ] + *samples * current_channels;
1622                                 this->audio_used[index] -= *samples;
1623                                 memmove( this->audio_buffer[ index ], src, this->audio_used[ index ] * current_channels * sizeof(int16_t) );
1624                         }
1625                 }
1626                 // Copy a single track to the output buffer
1627                 else
1628                 {
1629                         index = this->audio_index;
1630
1631                         // Now handle the audio if we have enough
1632                         if ( this->audio_used[ index ] >= *samples )
1633                         {
1634                                 int16_t *src = this->audio_buffer[ index ];
1635                                 memcpy( *buffer, src, *samples * *channels * sizeof(int16_t) );
1636                                 this->audio_used[ index ] -= *samples;
1637                                 memmove( src, &src[ *samples * *channels ], this->audio_used[ index ] * *channels * sizeof(int16_t) );
1638                         }
1639                         else
1640                         {
1641                                 // Otherwise fill with silence
1642                                 memset( *buffer, 0, *samples * *channels * sizeof(int16_t) );
1643                         }
1644                         if ( !this->audio_resample[ index ] )
1645                         {
1646                                 // TODO: uncomment and remove following line when full multi-channel support is ready
1647                                 // *channels = codec_context->channels;
1648                                 *frequency = this->audio_codec[ index ]->sample_rate;
1649                         }
1650                 }
1651         }
1652         else
1653         {
1654                 // Get silence and don't touch the context
1655                 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1656         }
1657
1658         // Regardless of speed (other than paused), we expect to get the next frame
1659         if ( !paused )
1660                 this->audio_expected = position + 1;
1661
1662         return 0;
1663 }
1664
1665 /** Initialize the audio codec context.
1666 */
1667
1668 static int audio_codec_init( producer_avformat this, int index, mlt_properties properties )
1669 {
1670         // Initialise the codec if necessary
1671         if ( !this->audio_codec[ index ] )
1672         {
1673                 // Get codec context
1674                 AVCodecContext *codec_context = this->audio_format->streams[index]->codec;
1675
1676                 // Find the codec
1677                 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1678
1679                 // If we don't have a codec and we can't initialise it, we can't do much more...
1680                 avformat_lock( );
1681                 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1682                 {
1683                         // Now store the codec with its destructor
1684                         avformat_unlock();
1685                         producer_codec_close( this->audio_codec[index] );
1686                         this->audio_codec[ index ] = codec_context;
1687                 }
1688                 else
1689                 {
1690                         // Remember that we can't use this later
1691                         this->audio_index = -1;
1692                         avformat_unlock( );
1693                 }
1694
1695                 // Process properties as AVOptions
1696                 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1697         }
1698         return this->audio_codec[ index ] && this->audio_index > -1;
1699 }
1700
1701 /** Set up audio handling.
1702 */
1703
1704 static void producer_set_up_audio( producer_avformat this, mlt_frame frame )
1705 {
1706         // Get the producer
1707         mlt_producer producer = &this->parent;
1708
1709         // Get the properties
1710         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1711
1712         // Fetch the audio format context
1713         AVFormatContext *context = this->audio_format;
1714
1715         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1716
1717         // Get the audio_index
1718         int index = mlt_properties_get_int( properties, "audio_index" );
1719
1720         // Handle all audio tracks
1721         if ( mlt_properties_get( properties, "audio_index" ) &&
1722                  !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
1723                 index = INT_MAX;
1724
1725         // Reopen the file if necessary
1726         if ( !context && index > -1 )
1727         {
1728                 mlt_events_block( properties, producer );
1729                 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1730                         mlt_properties_get( properties, "resource" ) );
1731                 context = this->audio_format;
1732                 producer_format_close( this->dummy_context );
1733                 this->dummy_context = NULL;
1734                 mlt_events_unblock( properties, producer );
1735                 get_audio_streams_info( this );
1736         }
1737
1738         // Exception handling for audio_index
1739         if ( context && index >= (int) context->nb_streams && index < INT_MAX )
1740         {
1741                 for ( index = context->nb_streams - 1;
1742                           index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO;
1743                           index-- );
1744                 mlt_properties_set_int( properties, "audio_index", index );
1745         }
1746         if ( context && index > -1 && index < INT_MAX &&
1747                  context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
1748         {
1749                 index = -1;
1750                 mlt_properties_set_int( properties, "audio_index", index );
1751         }
1752
1753         // Update the audio properties if the index changed
1754         if ( index > -1 && index != this->audio_index )
1755         {
1756                 producer_codec_close( this->audio_codec[ this->audio_index ] );
1757                 this->audio_codec[ this->audio_index ] = NULL;
1758         }
1759         this->audio_index = index;
1760
1761         // Get the codec(s)
1762         if ( context && index == INT_MAX )
1763         {
1764                 mlt_properties_set_int( frame_properties, "frequency", this->max_frequency );
1765                 mlt_properties_set_int( frame_properties, "channels", this->total_channels );
1766                 for ( index = 0; index < context->nb_streams; index++ )
1767                 {
1768                         if ( context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO )
1769                                 audio_codec_init( this, index, properties );
1770                 }
1771         }
1772         else if ( context && index > -1 && audio_codec_init( this, index, properties ) )
1773         {
1774                 // Set the frame properties
1775                 if ( index < INT_MAX )
1776                 {
1777                         mlt_properties_set_int( frame_properties, "frequency", this->audio_codec[ index ]->sample_rate );
1778                         mlt_properties_set_int( frame_properties, "channels", this->audio_codec[ index ]->channels );
1779                 }
1780         }
1781         if ( context && index > -1 )
1782         {
1783                 // Add our audio operation
1784                 mlt_frame_push_audio( frame, this );
1785                 mlt_frame_push_audio( frame, producer_get_audio );
1786         }
1787 }
1788
1789 /** Our get frame implementation.
1790 */
1791
1792 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1793 {
1794         // Access the private data
1795         producer_avformat this = producer->child;
1796
1797         // Create an empty frame
1798         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1799
1800         // Update timecode on the frame we're creating
1801         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
1802
1803         // Set the position of this producer
1804         mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( producer ) );
1805
1806         // Set up the video
1807         producer_set_up_video( this, *frame );
1808
1809         // Set up the audio
1810         producer_set_up_audio( this, *frame );
1811
1812         // Calculate the next timecode
1813         mlt_producer_prepare_next( producer );
1814
1815         return 0;
1816 }
1817
1818 static void producer_close( mlt_producer parent )
1819 {
1820         // Obtain this
1821         producer_avformat this = parent->child;
1822
1823         // Close the file
1824         av_free( this->av_frame );
1825         int i;
1826         for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
1827         {
1828                 if ( this->audio_resample[i] )
1829                         audio_resample_close( this->audio_resample[i] );
1830                 mlt_pool_release( this->audio_buffer[i] );
1831                 av_free( this->decode_buffer[i] );
1832                 producer_codec_close( this->audio_codec[i] );
1833         }
1834         producer_codec_close( this->video_codec );
1835         producer_format_close( this->dummy_context );
1836         producer_format_close( this->audio_format );
1837         producer_format_close( this->video_format );
1838
1839         // Close the parent
1840         parent->close = NULL;
1841         mlt_producer_close( parent );
1842
1843         // Free the memory
1844         free( this );
1845 }