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