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