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