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