]> git.sesse.net Git - mlt/blob - src/modules/avformat/producer_avformat.c
2474f9a065794ea1d4e377723841d762063de8d1
[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
27 // ffmpeg Header files
28 #include <avformat.h>
29 #include <opt.h>
30 #ifdef SWSCALE
31 #  include <swscale.h>
32 #endif
33 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
34 #  include "audioconvert.h"
35 #endif
36
37 // System header files
38 #include <stdlib.h>
39 #include <string.h>
40 #include <pthread.h>
41 #include <math.h>
42
43 #if LIBAVUTIL_VERSION_INT < (50<<16)
44 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
45 #endif
46
47 void avformat_lock( );
48 void avformat_unlock( );
49
50 // Forward references.
51 static int producer_open( mlt_producer this, mlt_profile profile, char *file );
52 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
53
54 /** Constructor for libavformat.
55 */
56
57 mlt_producer producer_avformat_init( mlt_profile profile, char *file )
58 {
59         int error = 0;
60
61         // Report information about available demuxers and codecs as YAML Tiny
62         if ( file && strstr( file, "f-list" ) )
63         {
64                 fprintf( stderr, "---\nformats:\n" );
65                 AVInputFormat *format = NULL;
66                 while ( ( format = av_iformat_next( format ) ) )
67                         fprintf( stderr, "  - %s\n", format->name );
68                 fprintf( stderr, "...\n" );
69                 error = 1;
70         }
71         if ( file && strstr( file, "acodec-list" ) )
72         {
73                 fprintf( stderr, "---\naudio_codecs:\n" );
74                 AVCodec *codec = NULL;
75                 while ( ( codec = av_codec_next( codec ) ) )
76                         if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
77                                 fprintf( stderr, "  - %s\n", codec->name );
78                 fprintf( stderr, "...\n" );
79                 error = 1;
80         }
81         if ( file && strstr( file, "vcodec-list" ) )
82         {
83                 fprintf( stderr, "---\nvideo_codecs:\n" );
84                 AVCodec *codec = NULL;
85                 while ( ( codec = av_codec_next( codec ) ) )
86                         if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
87                                 fprintf( stderr, "  - %s\n", codec->name );
88                 fprintf( stderr, "...\n" );
89                 error = 1;
90         }
91         if ( error )
92                 return NULL;
93
94         mlt_producer this = NULL;
95
96         // Check that we have a non-NULL argument
97         if ( file != NULL )
98         {
99                 // Construct the producer
100                 this = calloc( 1, sizeof( struct mlt_producer_s ) );
101
102                 // Initialise it
103                 if ( mlt_producer_init( this, NULL ) == 0 )
104                 {
105                         // Get the properties
106                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
107
108                         // Set the resource property (required for all producers)
109                         mlt_properties_set( properties, "resource", file );
110
111                         // Register our get_frame implementation
112                         this->get_frame = producer_get_frame;
113
114                         // Open the file
115                         if ( producer_open( this, profile, file ) != 0 )
116                         {
117                                 // Clean up
118                                 mlt_producer_close( this );
119                                 this = NULL;
120                         }
121                         else
122                         {
123                                 // Close the file to release resources for large playlists - reopen later as needed
124                                 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
125                                 mlt_properties_set_data( properties, "audio_context", NULL, 0, NULL, NULL );
126                                 mlt_properties_set_data( properties, "video_context", NULL, 0, NULL, NULL );
127
128                                 // Default the user-selectable indices from the auto-detected indices
129                                 mlt_properties_set_int( properties, "audio_index",  mlt_properties_get_int( properties, "_audio_index" ) );
130                                 mlt_properties_set_int( properties, "video_index",  mlt_properties_get_int( properties, "_video_index" ) );
131                         }
132                 }
133         }
134
135         return this;
136 }
137
138 /** Find the default streams.
139 */
140
141 static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatContext *context, int *audio_index, int *video_index )
142 {
143         int i;
144         char key[200];
145
146         mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
147
148         // Allow for multiple audio and video streams in the file and select first of each (if available)
149         for( i = 0; i < context->nb_streams; i++ )
150         {
151                 // Get the codec context
152                 AVStream *stream = context->streams[ i ];
153                 if ( ! stream ) continue;
154                 AVCodecContext *codec_context = stream->codec;
155                 if ( ! codec_context ) continue;
156                 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
157                 if ( ! codec ) continue;
158
159                 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
160
161                 // Determine the type and obtain the first index of each type
162                 switch( codec_context->codec_type )
163                 {
164                         case CODEC_TYPE_VIDEO:
165                                 if ( *video_index < 0 )
166                                         *video_index = i;
167                                 mlt_properties_set( meta_media, key, "video" );
168                                 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
169                                 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
170 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
171                                 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
172                                 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
173 #endif
174                                 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
175                                 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
176                                 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
177                                 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
178                                 break;
179                         case CODEC_TYPE_AUDIO:
180                                 if ( *audio_index < 0 )
181                                         *audio_index = i;
182                                 mlt_properties_set( meta_media, key, "audio" );
183 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
184                                 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
185                                 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
186 #endif
187                                 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
188                                 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
189                                 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
190                                 mlt_properties_set_int( meta_media, key, codec_context->channels );
191                                 break;
192                         default:
193                                 break;
194                 }
195 //              snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
196 //              mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
197                 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
198                 mlt_properties_set( meta_media, key, codec->name );
199 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
200                 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
201                 mlt_properties_set( meta_media, key, codec->long_name );
202 #endif
203                 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
204                 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
205 //              snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
206 //              mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
207                 snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
208                 mlt_properties_set_int( meta_media, key, codec_context->profile );
209                 snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
210                 mlt_properties_set_int( meta_media, key, codec_context->level );
211         }
212
213         return meta_media;
214 }
215
216 /** Producer file destructor.
217 */
218
219 static void producer_file_close( void *context )
220 {
221         if ( context != NULL )
222         {
223                 // Lock the mutex now
224                 avformat_lock( );
225
226                 // Close the file
227                 av_close_input_file( context );
228
229                 // Unlock the mutex now
230                 avformat_unlock( );
231         }
232 }
233
234 /** Producer file destructor.
235 */
236
237 static void producer_codec_close( void *codec )
238 {
239         if ( codec != NULL )
240         {
241                 // Lock the mutex now
242                 avformat_lock( );
243
244                 // Close the file
245                 avcodec_close( codec );
246
247                 // Unlock the mutex now
248                 avformat_unlock( );
249         }
250 }
251
252 static inline int dv_is_pal( AVPacket *pkt )
253 {
254         return pkt->data[3] & 0x80;
255 }
256
257 static int dv_is_wide( AVPacket *pkt )
258 {
259         int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
260
261         for ( ; i < pkt->size; i += 5 /* packet size */ )
262         {
263                 if ( pkt->data[ i ] == 0x61 )
264                 {
265                         uint8_t x = pkt->data[ i + 2 ] & 0x7;
266                         return ( x == 2 ) || ( x == 7 );
267                 }
268         }
269         return 0;
270 }
271
272 static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
273 {
274         double aspect_ratio = 1.0;
275
276         if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
277         {
278                 if ( pkt )
279                 {
280                         if ( dv_is_pal( pkt ) )
281                         {
282                                 aspect_ratio = dv_is_wide( pkt )
283                                         ? 64.0/45.0 // 16:9 PAL
284                                         : 16.0/15.0; // 4:3 PAL
285                         }
286                         else
287                         {
288                                 aspect_ratio = dv_is_wide( pkt )
289                                         ? 32.0/27.0 // 16:9 NTSC
290                                         : 8.0/9.0; // 4:3 NTSC
291                         }
292                 }
293                 else
294                 {
295                         AVRational ar =
296 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
297                                 stream->sample_aspect_ratio;
298 #else
299                                 codec_context->sample_aspect_ratio;
300 #endif
301                         // Override FFmpeg's notion of DV aspect ratios, which are
302                         // based upon a width of 704. Since we do not have a normaliser
303                         // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
304                         // we just coerce the values to facilitate a passive behaviour through
305                         // the rescale normaliser when using equivalent producers and consumers.
306                         // = display_aspect / (width * height)
307                         if ( ar.num == 10 && ar.den == 11 )
308                                 aspect_ratio = 8.0/9.0; // 4:3 NTSC
309                         else if ( ar.num == 59 && ar.den == 54 )
310                                 aspect_ratio = 16.0/15.0; // 4:3 PAL
311                         else if ( ar.num == 40 && ar.den == 33 )
312                                 aspect_ratio = 32.0/27.0; // 16:9 NTSC
313                         else if ( ar.num == 118 && ar.den == 81 )
314                                 aspect_ratio = 64.0/45.0; // 16:9 PAL
315                 }
316         }
317         else
318         {
319                 AVRational codec_sar = codec_context->sample_aspect_ratio;
320                 AVRational stream_sar =
321 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
322                         stream->sample_aspect_ratio;
323 #else
324                         { 0, 1 };
325 #endif
326                 if ( codec_sar.num > 0 )
327                         aspect_ratio = av_q2d( codec_sar );
328                 else if ( stream_sar.num > 0 )
329                         aspect_ratio = av_q2d( stream_sar );
330         }
331         return aspect_ratio;
332 }
333
334 /** Open the file.
335 */
336
337 static int producer_open( mlt_producer this, mlt_profile profile, char *file )
338 {
339         // Return an error code (0 == no error)
340         int error = 0;
341
342         // Context for avformat
343         AVFormatContext *context = NULL;
344
345         // Get the properties
346         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
347
348         // We will treat everything with the producer fps
349         double fps = mlt_profile_fps( profile );
350
351         // Lock the mutex now
352         avformat_lock( );
353
354         // If "MRL", then create AVInputFormat
355         AVInputFormat *format = NULL;
356         AVFormatParameters *params = NULL;
357         char *standard = NULL;
358         char *mrl = strchr( file, ':' );
359
360         // AV option (0 = both, 1 = video, 2 = audio)
361         int av = 0;
362
363         // Setting lowest log level
364         av_log_set_level( -1 );
365
366         // Only if there is not a protocol specification that avformat can handle
367         if ( mrl && !url_exist( file ) )
368         {
369                 // 'file' becomes format abbreviation
370                 mrl[0] = 0;
371
372                 // Lookup the format
373                 format = av_find_input_format( file );
374
375                 // Eat the format designator
376                 file = ++mrl;
377
378                 if ( format )
379                 {
380                         // Allocate params
381                         params = calloc( sizeof( AVFormatParameters ), 1 );
382
383                         // These are required by video4linux (defaults)
384                         params->width = 640;
385                         params->height = 480;
386                         params->time_base= (AVRational){1,25};
387                         // params->device = file;
388                         params->channels = 2;
389                         params->sample_rate = 48000;
390                 }
391
392                 // XXX: this does not work anymore since avdevice
393                 // TODO: make producer_avddevice?
394                 // Parse out params
395                 mrl = strchr( file, '?' );
396                 while ( mrl )
397                 {
398                         mrl[0] = 0;
399                         char *name = strdup( ++mrl );
400                         char *value = strchr( name, ':' );
401                         if ( value )
402                         {
403                                 value[0] = 0;
404                                 value++;
405                                 char *t = strchr( value, '&' );
406                                 if ( t )
407                                         t[0] = 0;
408                                 if ( !strcmp( name, "frame_rate" ) )
409                                         params->time_base.den = atoi( value );
410                                 else if ( !strcmp( name, "frame_rate_base" ) )
411                                         params->time_base.num = atoi( value );
412                                 else if ( !strcmp( name, "sample_rate" ) )
413                                         params->sample_rate = atoi( value );
414                                 else if ( !strcmp( name, "channels" ) )
415                                         params->channels = atoi( value );
416                                 else if ( !strcmp( name, "width" ) )
417                                         params->width = atoi( value );
418                                 else if ( !strcmp( name, "height" ) )
419                                         params->height = atoi( value );
420                                 else if ( !strcmp( name, "standard" ) )
421                                 {
422                                         standard = strdup( value );
423                                         params->standard = standard;
424                                 }
425                                 else if ( !strcmp( name, "av" ) )
426                                         av = atoi( value );
427                         }
428                         free( name );
429                         mrl = strchr( mrl, '&' );
430                 }
431         }
432
433         // Now attempt to open the file
434         error = av_open_input_file( &context, file, format, 0, params ) < 0;
435
436         // Cleanup AVFormatParameters
437         free( standard );
438         free( params );
439
440         // If successful, then try to get additional info
441         if ( error == 0 )
442         {
443                 // Get the stream info
444                 error = av_find_stream_info( context ) < 0;
445
446                 // Continue if no error
447                 if ( error == 0 )
448                 {
449                         // We will default to the first audio and video streams found
450                         int audio_index = -1;
451                         int video_index = -1;
452                         int av_bypass = 0;
453
454                         // Now set properties where we can (use default unknowns if required)
455                         if ( context->duration != AV_NOPTS_VALUE )
456                         {
457                                 // This isn't going to be accurate for all formats
458                                 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
459                                 mlt_properties_set_position( properties, "out", frames - 1 );
460                                 mlt_properties_set_position( properties, "length", frames );
461                         }
462
463                         // Find default audio and video streams
464                         find_default_streams( properties, context, &audio_index, &video_index );
465
466                         if ( context->start_time != AV_NOPTS_VALUE )
467                                 mlt_properties_set_double( properties, "_start_time", context->start_time );
468
469                         // Check if we're seekable (something funny about mpeg here :-/)
470                         if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 )  && strncmp( file, "udp:", 4 )  && strncmp( file, "tcp:", 4 ) && strncmp( file, "rtsp:", 5 )  && strncmp( file, "rtp:", 4 ) )
471                         {
472                                 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
473                                 mlt_properties_set_data( properties, "dummy_context", context, 0, producer_file_close, NULL );
474                                 av_open_input_file( &context, file, NULL, 0, NULL );
475                                 av_find_stream_info( context );
476                         }
477                         else
478                                 av_bypass = 1;
479
480                         // Store selected audio and video indexes on properties
481                         mlt_properties_set_int( properties, "_audio_index", audio_index );
482                         mlt_properties_set_int( properties, "_video_index", video_index );
483                         mlt_properties_set_int( properties, "_last_position", -1 );
484
485                         // Fetch the width, height and aspect ratio
486                         if ( video_index != -1 )
487                         {
488                                 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
489                                 mlt_properties_set_int( properties, "width", codec_context->width );
490                                 mlt_properties_set_int( properties, "height", codec_context->height );
491
492                                 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
493                                 {
494                                         // Fetch the first frame of DV so we can read it directly
495                                         AVPacket pkt;
496                                         int ret = 0;
497                                         while ( ret >= 0 )
498                                         {
499                                                 ret = av_read_frame( context, &pkt );
500                                                 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
501                                                 {
502                                                         mlt_properties_set_double( properties, "aspect_ratio",
503                                                                 get_aspect_ratio( context->streams[ video_index ], codec_context, &pkt ) );
504                                                         break;
505                                                 }
506                                         }
507                                 }
508                                 else
509                                 {
510                                         mlt_properties_set_double( properties, "aspect_ratio",
511                                                 get_aspect_ratio( context->streams[ video_index ], codec_context, NULL ) );
512                                 }
513                         }
514
515                         // Read Metadata
516                         if (context->title != NULL)
517                                 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
518                         if (context->author != NULL)
519                                 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
520                         if (context->copyright != NULL)
521                                 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
522                         if (context->comment != NULL)
523                                 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
524                         if (context->album != NULL)
525                                 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
526                         if (context->year != 0)
527                                 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
528                         if (context->track != 0)
529                                 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
530
531                         // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
532                         if ( av == 0 && audio_index != -1 && video_index != -1 )
533                         {
534                                 // We'll use the open one as our video_context
535                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
536
537                                 // And open again for our audio context
538                                 av_open_input_file( &context, file, NULL, 0, NULL );
539                                 av_find_stream_info( context );
540
541                                 // Audio context
542                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
543                         }
544                         else if ( av != 2 && video_index != -1 )
545                         {
546                                 // We only have a video context
547                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
548                         }
549                         else if ( audio_index != -1 )
550                         {
551                                 // We only have an audio context
552                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
553                         }
554                         else
555                         {
556                                 // Something has gone wrong
557                                 error = -1;
558                         }
559
560                         mlt_properties_set_int( properties, "av_bypass", av_bypass );
561                 }
562         }
563
564         // Unlock the mutex now
565         avformat_unlock( );
566
567         return error;
568 }
569
570 /** Convert a frame position to a time code.
571 */
572
573 static double producer_time_of_frame( mlt_producer this, mlt_position position )
574 {
575         return ( double )position / mlt_producer_get_fps( this );
576 }
577
578 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
579 {
580 #ifdef SWSCALE
581         if ( format == mlt_image_yuv420p )
582         {
583                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
584                         width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
585                 AVPicture output;
586                 output.data[0] = buffer;
587                 output.data[1] = buffer + width * height;
588                 output.data[2] = buffer + ( 3 * width * height ) / 2;
589                 output.linesize[0] = width;
590                 output.linesize[1] = width >> 1;
591                 output.linesize[2] = width >> 1;
592                 sws_scale( context, frame->data, frame->linesize, 0, height,
593                         output.data, output.linesize);
594                 sws_freeContext( context );
595         }
596         else if ( format == mlt_image_rgb24 )
597         {
598                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
599                         width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
600                 AVPicture output;
601                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
602                 sws_scale( context, frame->data, frame->linesize, 0, height,
603                         output.data, output.linesize);
604                 sws_freeContext( context );
605         }
606         else
607         {
608                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
609                         width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
610                 AVPicture output;
611                 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
612                 sws_scale( context, frame->data, frame->linesize, 0, height,
613                         output.data, output.linesize);
614                 sws_freeContext( context );
615         }
616 #else
617         if ( format == mlt_image_yuv420p )
618         {
619                 AVPicture pict;
620                 pict.data[0] = buffer;
621                 pict.data[1] = buffer + width * height;
622                 pict.data[2] = buffer + ( 3 * width * height ) / 2;
623                 pict.linesize[0] = width;
624                 pict.linesize[1] = width >> 1;
625                 pict.linesize[2] = width >> 1;
626                 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
627         }
628         else if ( format == mlt_image_rgb24 )
629         {
630                 AVPicture output;
631                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
632                 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
633         }
634         else
635         {
636                 AVPicture output;
637                 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
638                 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
639         }
640 #endif
641 }
642
643 /** Allocate the image buffer and set it on the frame.
644 */
645
646 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
647 {
648         int size = 0;
649
650         if ( codec_context->width == 0 || codec_context->height == 0 )
651                 return size;
652
653         *width = codec_context->width;
654         *height = codec_context->height;
655         mlt_properties_set_int( frame_properties, "width", *width );
656         mlt_properties_set_int( frame_properties, "height", *height );
657
658         switch ( *format )
659         {
660                 case mlt_image_yuv420p:
661                         size = *width * 3 * ( *height + 1 ) / 2;
662                         break;
663                 case mlt_image_rgb24:
664                         size = *width * ( *height + 1 ) * 3;
665                         break;
666                 default:
667                         *format = mlt_image_yuv422;
668                         size = *width * ( *height + 1 ) * 2;
669                         break;
670         }
671
672         // Construct the output image
673         *buffer = mlt_pool_alloc( size );
674         if ( *buffer )
675                 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
676         else
677                 size = 0;
678
679         return size;
680 }
681
682 /** Get an image from a frame.
683 */
684
685 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
686 {
687         // Get the properties from the frame
688         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
689
690         // Obtain the frame number of this frame
691         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
692
693         // Get the producer
694         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
695
696         // Get the producer properties
697         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
698
699         // Fetch the video_context
700         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
701
702         // Get the video_index
703         int index = mlt_properties_get_int( properties, "video_index" );
704
705         // Obtain the expected frame numer
706         mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
707
708         // Get the video stream
709         AVStream *stream = context->streams[ index ];
710
711         // Get codec context
712         AVCodecContext *codec_context = stream->codec;
713
714         // Packet
715         AVPacket pkt;
716
717         // Get the conversion frame
718         AVFrame *av_frame = mlt_properties_get_data( properties, "av_frame", NULL );
719
720         // Special case pause handling flag
721         int paused = 0;
722
723         // Special case ffwd handling
724         int ignore = 0;
725
726         // We may want to use the source fps if available
727         double source_fps = mlt_properties_get_double( properties, "source_fps" );
728         double fps = mlt_producer_get_fps( this );
729
730         // This is the physical frame position in the source
731         int req_position = ( int )( position / fps * source_fps + 0.5 );
732
733         // Get the seekable status
734         int seekable = mlt_properties_get_int( properties, "seekable" );
735
736         // Hopefully provide better support for streams...
737         int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
738
739         // Determines if we have to decode all frames in a sequence
740         int must_decode = 1;
741
742         // Temporary hack to improve intra frame only
743         must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
744                                   strcmp( codec_context->codec->name, "dvvideo" ) &&
745                                   strcmp( codec_context->codec->name, "huffyuv" ) &&
746                                   strcmp( codec_context->codec->name, "mjpeg" ) &&
747                                   strcmp( codec_context->codec->name, "rawvideo" );
748
749         // Seek if necessary
750         if ( position != expected )
751         {
752                 if ( av_frame != NULL && position + 1 == expected )
753                 {
754                         // We're paused - use last image
755                         paused = 1;
756                 }
757                 else if ( !seekable && position > expected && ( position - expected ) < 250 )
758                 {
759                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
760                         ignore = ( int )( ( position - expected ) / fps * source_fps );
761                 }
762                 else if ( seekable && ( position < expected || position - expected >= 12 ) )
763                 {
764                         // Calculate the timestamp for the requested frame
765                         int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
766                         if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
767                                 timestamp += context->start_time;
768                         if ( must_decode )
769                                 timestamp -= AV_TIME_BASE;
770                         if ( timestamp < 0 )
771                                 timestamp = 0;
772
773                         // Set to the timestamp
774                         av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
775
776                         // Remove the cached info relating to the previous position
777                         mlt_properties_set_int( properties, "_current_position", -1 );
778                         mlt_properties_set_int( properties, "_last_position", -1 );
779                         mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
780                         av_frame = NULL;
781                 }
782         }
783
784         // Duplicate the last image if necessary (see comment on rawvideo below)
785         int current_position = mlt_properties_get_int( properties, "_current_position" );
786         int got_picture = mlt_properties_get_int( properties, "_got_picture" );
787         if ( av_frame != NULL && got_picture && ( paused || current_position >= req_position ) && av_bypass == 0 )
788         {
789                 // Duplicate it
790                 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
791                         convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
792                 else
793                         mlt_frame_get_image( frame, buffer, format, width, height, writable );
794         }
795         else
796         {
797                 int ret = 0;
798                 int int_position = 0;
799                 got_picture = 0;
800
801                 av_init_packet( &pkt );
802
803                 // Construct an AVFrame for YUV422 conversion
804                 if ( av_frame == NULL )
805                         av_frame = avcodec_alloc_frame( );
806
807                 while( ret >= 0 && !got_picture )
808                 {
809                         // Read a packet
810                         ret = av_read_frame( context, &pkt );
811
812                         // We only deal with video from the selected video_index
813                         if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
814                         {
815                                 // Determine time code of the packet
816                                 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
817                                 if ( context->start_time != AV_NOPTS_VALUE )
818                                         int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
819                                 int last_position = mlt_properties_get_int( properties, "_last_position" );
820                                 if ( int_position == last_position )
821                                         int_position = last_position + 1;
822                                 mlt_properties_set_int( properties, "_last_position", int_position );
823
824                                 // Decode the image
825                                 if ( must_decode || int_position >= req_position )
826                                         ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
827
828                                 if ( got_picture )
829                                 {
830                                         // Handle ignore
831                                         if ( int_position < req_position )
832                                         {
833                                                 ignore = 0;
834                                                 got_picture = 0;
835                                         }
836                                         else if ( int_position >= req_position )
837                                         {
838                                                 ignore = 0;
839                                         }
840                                         else if ( ignore -- )
841                                         {
842                                                 got_picture = 0;
843                                         }
844                                 }
845                                 av_free_packet( &pkt );
846                         }
847                         else if ( ret >= 0 )
848                         {
849                                 av_free_packet( &pkt );
850                         }
851
852                         // Now handle the picture if we have one
853                         if ( got_picture )
854                         {
855                                 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
856                                 {
857                                         convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
858                                         mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
859                                         mlt_properties_set_int( properties, "top_field_first", av_frame->top_field_first );
860                                         mlt_properties_set_int( properties, "_current_position", int_position );
861                                         mlt_properties_set_int( properties, "_got_picture", 1 );
862                                         mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
863                                 }
864                                 else
865                                 {
866                                         got_picture = 0;
867                                 }
868                         }
869                 }
870                 if ( !got_picture )
871                         mlt_frame_get_image( frame, buffer, format, width, height, writable );
872         }
873
874         // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
875         // above will break the pause behaviour - so we wipe the frame now
876         if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
877                 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
878
879         // Set the field order property for this frame
880         mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
881
882         // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
883         mlt_properties_set_position( properties, "_video_expected", position + 1 );
884
885         return 0;
886 }
887
888 /** Process properties as AVOptions and apply to AV context obj
889 */
890
891 static void apply_properties( void *obj, mlt_properties properties, int flags )
892 {
893         int i;
894         int count = mlt_properties_count( properties );
895         for ( i = 0; i < count; i++ )
896         {
897                 const char *opt_name = mlt_properties_get_name( properties, i );
898                 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
899                 if ( opt != NULL )
900 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
901                         av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
902 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
903                         av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
904 #else
905                         av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
906 #endif
907         }
908 }
909
910 /** Set up video handling.
911 */
912
913 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
914 {
915         // Get the properties
916         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
917
918         // Fetch the video_context
919         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
920
921         // Get the video_index
922         int index = mlt_properties_get_int( properties, "video_index" );
923
924         // Reopen the file if necessary
925         if ( !context && index > -1 )
926         {
927                 mlt_events_block( properties, this );
928                 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
929                         mlt_properties_get( properties, "resource" ) );
930                 context = mlt_properties_get_data( properties, "video_context", NULL );
931                 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
932                 mlt_events_unblock( properties, this );
933
934                 // Process properties as AVOptions
935                 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
936         }
937
938         // Exception handling for video_index
939         if ( context && index >= (int) context->nb_streams )
940         {
941                 // Get the last video stream
942                 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO; --index );
943                 mlt_properties_set_int( properties, "video_index", index );
944         }
945         if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
946         {
947                 // Invalidate the video stream
948                 index = -1;
949                 mlt_properties_set_int( properties, "video_index", index );
950         }
951
952         // Get the frame properties
953         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
954
955         if ( context && index > -1 )
956         {
957                 // Get the video stream
958                 AVStream *stream = context->streams[ index ];
959
960                 // Get codec context
961                 AVCodecContext *codec_context = stream->codec;
962
963                 // Get the codec
964                 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
965
966                 // Update the video properties if the index changed
967                 if ( index != mlt_properties_get_int( properties, "_video_index" ) )
968                 {
969                         // Reset the video properties if the index changed
970                         mlt_properties_set_int( properties, "_video_index", index );
971                         mlt_properties_set_data( properties, "video_codec", NULL, 0, NULL, NULL );
972                         mlt_properties_set_int( properties, "width", codec_context->width );
973                         mlt_properties_set_int( properties, "height", codec_context->height );
974                         // TODO: get the first usable AVPacket and reset the stream position
975                         mlt_properties_set_double( properties, "aspect_ratio",
976                                 get_aspect_ratio( context->streams[ index ], codec_context, NULL ) );
977                         codec = NULL;
978                 }
979
980                 // Initialise the codec if necessary
981                 if ( codec == NULL )
982                 {
983                         // Initialise multi-threading
984                         int thread_count = mlt_properties_get_int( properties, "threads" );
985                         if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
986                                 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
987                         if ( thread_count > 1 )
988                         {
989                                 avcodec_thread_init( codec_context, thread_count );
990                                 codec_context->thread_count = thread_count;
991                         }
992
993                         // Find the codec
994                         codec = avcodec_find_decoder( codec_context->codec_id );
995
996                         // If we don't have a codec and we can't initialise it, we can't do much more...
997                         avformat_lock( );
998                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
999                         {
1000                                 // Now store the codec with its destructor
1001                                 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
1002                         }
1003                         else
1004                         {
1005                                 // Remember that we can't use this later
1006                                 mlt_properties_set_int( properties, "video_index", -1 );
1007                                 index = -1;
1008                         }
1009                         avformat_unlock( );
1010
1011                         // Process properties as AVOptions
1012                         apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1013                 }
1014
1015                 // No codec, no show...
1016                 if ( codec && index > -1 )
1017                 {
1018                         double source_fps = 0;
1019                         double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1020                         double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1021                                 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1022
1023                         // Determine the fps
1024                         source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
1025
1026                         // We'll use fps if it's available
1027                         if ( source_fps > 0 )
1028                                 mlt_properties_set_double( properties, "source_fps", source_fps );
1029                         else
1030                                 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this ) );
1031                         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
1032
1033                         // Set the width and height
1034                         mlt_properties_set_int( frame_properties, "width", codec_context->width );
1035                         mlt_properties_set_int( frame_properties, "height", codec_context->height );
1036                         mlt_properties_set_int( frame_properties, "real_width", codec_context->width );
1037                         mlt_properties_set_int( frame_properties, "real_height", codec_context->height );
1038                         mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1039
1040                         mlt_frame_push_get_image( frame, producer_get_image );
1041                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1042                 }
1043                 else
1044                 {
1045                         mlt_properties_set_int( frame_properties, "test_image", 1 );
1046                 }
1047         }
1048         else
1049         {
1050                 mlt_properties_set_int( frame_properties, "test_image", 1 );
1051         }
1052 }
1053
1054 /** Get the audio from a frame.
1055 */
1056
1057 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1058 {
1059         // Get the properties from the frame
1060         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1061
1062         // Obtain the frame number of this frame
1063         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
1064
1065         // Get the producer
1066         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
1067
1068         // Get the producer properties
1069         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1070
1071         // Fetch the audio_context
1072         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1073
1074         // Get the audio_index
1075         int index = mlt_properties_get_int( properties, "audio_index" );
1076
1077         // Get the seekable status
1078         int seekable = mlt_properties_get_int( properties, "seekable" );
1079
1080         // Obtain the expected frame numer
1081         mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
1082
1083         // Obtain the resample context if it exists (not always needed)
1084         ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
1085
1086 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1087         // Get the format converter context if it exists
1088         AVAudioConvert *convert = mlt_properties_get_data( properties, "audio_convert", NULL );
1089 #endif
1090
1091         // Obtain the audio buffers
1092         int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
1093         int16_t *decode_buffer = mlt_properties_get_data( properties, "decode_buffer", NULL );
1094         int16_t *convert_buffer = mlt_properties_get_data( properties, "convert_buffer", NULL );
1095
1096         // Get amount of audio used
1097         int audio_used =  mlt_properties_get_int( properties, "_audio_used" );
1098
1099         // Calculate the real time code
1100         double real_timecode = producer_time_of_frame( this, position );
1101
1102         // Get the audio stream
1103         AVStream *stream = context->streams[ index ];
1104
1105         // Get codec context
1106         AVCodecContext *codec_context = stream->codec;
1107
1108         // Packet
1109         AVPacket pkt;
1110
1111         // Number of frames to ignore (for ffwd)
1112         int ignore = 0;
1113
1114         // Flag for paused (silence)
1115         int paused = 0;
1116
1117         // Check for resample and create if necessary
1118         if ( resample == NULL && codec_context->channels <= 2 )
1119         {
1120                 // Create the resampler
1121                 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
1122
1123                 // And store it on properties
1124                 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
1125         }
1126         else if ( resample == NULL )
1127         {
1128                 *channels = codec_context->channels;
1129                 *frequency = codec_context->sample_rate;
1130         }
1131
1132 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1133         // Check for audio format converter and create if necessary
1134         // TODO: support higher resolutions than 16-bit.
1135         if ( convert == NULL && codec_context->sample_fmt != SAMPLE_FMT_S16 )
1136         {
1137                 // Create single channel converter for interleaved with no mixing matrix
1138                 convert = av_audio_convert_alloc( SAMPLE_FMT_S16, 1, codec_context->sample_fmt, 1, NULL, 0 );
1139                 mlt_properties_set_data( properties, "audio_convert", convert, 0, ( mlt_destructor )av_audio_convert_free, NULL );
1140         }
1141 #endif
1142
1143         // Check for audio buffer and create if necessary
1144         if ( audio_buffer == NULL )
1145         {
1146                 // Allocate the audio buffer
1147                 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1148
1149                 // And store it on properties for reuse
1150                 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1151         }
1152
1153         // Check for decoder buffer and create if necessary
1154         if ( decode_buffer == NULL )
1155         {
1156                 // Allocate the audio buffer
1157                 decode_buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1158
1159                 // And store it on properties for reuse
1160                 mlt_properties_set_data( properties, "decode_buffer", decode_buffer, 0, ( mlt_destructor )av_free, NULL );
1161         }
1162
1163 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1164         // Check for format converter buffer and create if necessary
1165         if ( resample && convert && convert_buffer == NULL )
1166         {
1167                 // Allocate the audio buffer
1168                 convert_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1169
1170                 // And store it on properties for reuse
1171                 mlt_properties_set_data( properties, "convert_buffer", convert_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1172         }
1173 #endif
1174
1175         // Seek if necessary
1176         if ( position != expected )
1177         {
1178                 if ( position + 1 == expected )
1179                 {
1180                         // We're paused - silence required
1181                         paused = 1;
1182                 }
1183                 else if ( !seekable && position > expected && ( position - expected ) < 250 )
1184                 {
1185                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
1186                         ignore = position - expected;
1187                 }
1188                 else if ( position < expected || position - expected >= 12 )
1189                 {
1190                         int64_t timestamp = ( int64_t )( real_timecode * AV_TIME_BASE + 0.5 );
1191                         if ( context->start_time != AV_NOPTS_VALUE )
1192                                 timestamp += context->start_time;
1193                         if ( timestamp < 0 )
1194                                 timestamp = 0;
1195
1196                         // Set to the real timecode
1197                         if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
1198                                 paused = 1;
1199
1200                         // Clear the usage in the audio buffer
1201                         audio_used = 0;
1202                 }
1203         }
1204
1205         // Get the audio if required
1206         if ( !paused )
1207         {
1208                 int ret = 0;
1209                 int got_audio = 0;
1210
1211                 av_init_packet( &pkt );
1212
1213                 while( ret >= 0 && !got_audio )
1214                 {
1215                         // Check if the buffer already contains the samples required
1216                         if ( audio_used >= *samples && ignore == 0 )
1217                         {
1218                                 got_audio = 1;
1219                                 break;
1220                         }
1221
1222                         // Read a packet
1223                         ret = av_read_frame( context, &pkt );
1224
1225                         int len = pkt.size;
1226                         uint8_t *ptr = pkt.data;
1227
1228                         // We only deal with audio from the selected audio_index
1229                         while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
1230                         {
1231                                 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1232
1233                                 // Decode the audio
1234 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1235                                 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1236 #else
1237                                 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1238 #endif
1239                                 if ( ret < 0 )
1240                                 {
1241                                         ret = 0;
1242                                         break;
1243                                 }
1244
1245                                 len -= ret;
1246                                 ptr += ret;
1247
1248                                 if ( data_size > 0 )
1249                                 {
1250                                         int src_stride[6]= { av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 };
1251                                         int dst_stride[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16 ) / 8 };
1252
1253                                         if ( resample )
1254                                         {
1255                                                 int16_t *source = decode_buffer;
1256                                                 int16_t *dest = &audio_buffer[ audio_used * *channels ];
1257                                                 int convert_samples = data_size / src_stride[0];
1258
1259 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1260                                                 if ( convert )
1261                                                 {
1262                                                         const void *src_buf[6] = { decode_buffer };
1263                                                         void *dst_buf[6] = { convert_buffer };
1264                                                         av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, convert_samples );
1265                                                         source = convert_buffer;
1266                                                 }
1267 #endif
1268                                                 audio_used += audio_resample( resample, dest, source, convert_samples / codec_context->channels );
1269                                         }
1270                                         else
1271                                         {
1272 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1273                                                 if ( convert )
1274                                                 {
1275                                                         const void *src_buf[6] = { decode_buffer };
1276                                                         void *dst_buf[6] = { &audio_buffer[ audio_used * *channels ] };
1277                                                         av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, data_size / src_stride[0] );
1278                                                 }
1279                                                 else
1280 #endif
1281                                                 {
1282                                                         memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
1283                                                 }
1284                                                 audio_used += data_size / *channels / src_stride[0];
1285                                         }
1286
1287                                         // Handle ignore
1288                                         while ( ignore && audio_used > *samples )
1289                                         {
1290                                                 ignore --;
1291                                                 audio_used -= *samples;
1292                                                 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
1293                                         }
1294                                 }
1295
1296                                 // If we're behind, ignore this packet
1297                                 if ( pkt.pts >= 0 )
1298                                 {
1299                                         double current_pts = av_q2d( stream->time_base ) * pkt.pts;
1300                                         double source_fps = mlt_properties_get_double( properties, "source_fps" );
1301                                         int req_position = ( int )( real_timecode * source_fps + 0.5 );
1302                                         int int_position = ( int )( current_pts * source_fps + 0.5 );
1303
1304                                         if ( context->start_time != AV_NOPTS_VALUE )
1305                                                 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
1306                                         if ( seekable && !ignore && int_position < req_position )
1307                                                 ignore = 1;
1308                                 }
1309                         }
1310
1311                         // We're finished with this packet regardless
1312                         av_free_packet( &pkt );
1313                 }
1314
1315                 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
1316                 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1317
1318                 // Now handle the audio if we have enough
1319                 if ( audio_used >= *samples )
1320                 {
1321                         memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
1322                         audio_used -= *samples;
1323                         memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
1324                 }
1325                 else
1326                 {
1327                         memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
1328                 }
1329
1330                 // Store the number of audio samples still available
1331                 mlt_properties_set_int( properties, "_audio_used", audio_used );
1332         }
1333         else
1334         {
1335                 // Get silence and don't touch the context
1336                 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1337         }
1338
1339         // Regardless of speed (other than paused), we expect to get the next frame
1340         if ( !paused )
1341                 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
1342
1343         return 0;
1344 }
1345
1346 /** Set up audio handling.
1347 */
1348
1349 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
1350 {
1351         // Get the properties
1352         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1353
1354         // Fetch the audio_context
1355         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1356
1357         // Get the audio_index
1358         int index = mlt_properties_get_int( properties, "audio_index" );
1359
1360         // Reopen the file if necessary
1361         if ( !context && index > -1 )
1362         {
1363                 mlt_events_block( properties, this );
1364                 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1365                         mlt_properties_get( properties, "resource" ) );
1366                 context = mlt_properties_get_data( properties, "audio_context", NULL );
1367                 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
1368                 mlt_events_unblock( properties, this );
1369         }
1370
1371         // Exception handling for audio_index
1372         if ( context && index >= (int) context->nb_streams )
1373         {
1374                 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO; --index );
1375                 mlt_properties_set_int( properties, "audio_index", index );
1376         }
1377         if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
1378         {
1379                 index = -1;
1380                 mlt_properties_set_int( properties, "audio_index", index );
1381         }
1382
1383         // Update the audio properties if the index changed
1384         if ( index > -1 && index != mlt_properties_get_int( properties, "_audio_index" ) )
1385         {
1386                 mlt_properties_set_int( properties, "_audio_index", index );
1387                 mlt_properties_set_data( properties, "audio_codec", NULL, 0, NULL, NULL );
1388         }
1389
1390         // Deal with audio context
1391         if ( context != NULL && index > -1 )
1392         {
1393                 // Get the frame properties
1394                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1395
1396                 // Get the audio stream
1397                 AVStream *stream = context->streams[ index ];
1398
1399                 // Get codec context
1400                 AVCodecContext *codec_context = stream->codec;
1401
1402                 // Get the codec
1403                 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
1404
1405                 // Initialise the codec if necessary
1406                 if ( codec == NULL )
1407                 {
1408                         // Find the codec
1409                         codec = avcodec_find_decoder( codec_context->codec_id );
1410
1411                         // If we don't have a codec and we can't initialise it, we can't do much more...
1412                         avformat_lock( );
1413                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
1414                         {
1415                                 // Now store the codec with its destructor
1416                                 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
1417
1418                         }
1419                         else
1420                         {
1421                                 // Remember that we can't use this later
1422                                 mlt_properties_set_int( properties, "audio_index", -1 );
1423                                 index = -1;
1424                         }
1425                         avformat_unlock( );
1426
1427                         // Process properties as AVOptions
1428                         apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1429                 }
1430
1431                 // No codec, no show...
1432                 if ( codec && index > -1 )
1433                 {
1434                         mlt_frame_push_audio( frame, producer_get_audio );
1435                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1436                         mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
1437                         mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
1438                 }
1439         }
1440 }
1441
1442 /** Our get frame implementation.
1443 */
1444
1445 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1446 {
1447         // Create an empty frame
1448         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1449
1450         // Update timecode on the frame we're creating
1451         mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1452
1453         // Set the position of this producer
1454         mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1455
1456         // Set up the video
1457         producer_set_up_video( this, *frame );
1458
1459         // Set up the audio
1460         producer_set_up_audio( this, *frame );
1461
1462         // Set the aspect_ratio
1463         mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1464
1465         // Calculate the next timecode
1466         mlt_producer_prepare_next( this );
1467
1468         return 0;
1469 }