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