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