]> git.sesse.net Git - mlt/blob - src/modules/avformat/producer_avformat.c
service stack, various fixes
[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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 // Local header files
22 #include "producer_avformat.h"
23
24 // MLT Header files
25 #include <framework/mlt_frame.h>
26
27 // ffmpeg Header files
28 #include <ffmpeg/avformat.h>
29
30 // System header files
31 #include <stdlib.h>
32 #include <string.h>
33 #include <pthread.h>
34 #include <math.h>
35
36 // Forward references.
37 static int producer_open( mlt_producer this, char *file );
38 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
39
40 // A static flag used to determine if avformat has been initialised
41 static int avformat_initialised = 0;
42 static pthread_mutex_t avformat_mutex;
43
44 /** Constructor for libavformat.
45 */
46
47 mlt_producer producer_avformat_init( char *file )
48 {
49         mlt_producer this = NULL;
50
51         // Check that we have a non-NULL argument
52         if ( file != NULL )
53         {
54                 // Construct the producer
55                 this = calloc( 1, sizeof( struct mlt_producer_s ) );
56
57                 // Initialise it
58                 if ( mlt_producer_init( this, NULL ) == 0 )
59                 {
60                         // Get the properties
61                         mlt_properties properties = mlt_producer_properties( this );
62
63                         // Set the resource property (required for all producers)
64                         mlt_properties_set( properties, "resource", file );
65
66                         // TEST: audio sync tweaking
67                         mlt_properties_set_double( properties, "discrepancy", 1 );
68
69                         // Register our get_frame implementation
70                         this->get_frame = producer_get_frame;
71
72                         // Initialise avformat if necessary
73                         if ( avformat_initialised == 0 )
74                         {
75                                 pthread_mutex_init( &avformat_mutex, NULL );
76                                 avformat_initialised = 1;
77                                 av_register_all( );
78                         }
79
80                         // Open the file
81                         if ( producer_open( this, file ) != 0 )
82                         {
83                                 // Clean up
84                                 mlt_producer_close( this );
85                                 this = NULL;
86                         }
87                 }
88         }
89
90         return this;
91 }
92
93 /** Find the default streams.
94 */
95
96 static void find_default_streams( AVFormatContext *context, int *audio_index, int *video_index )
97 {
98         int i;
99
100         // Allow for multiple audio and video streams in the file and select first of each (if available)
101         for( i = 0; i < context->nb_streams; i++ ) 
102         {
103                 // Get the codec context
104                 AVCodecContext *codec_context = &context->streams[ i ]->codec;
105
106                 // Determine the type and obtain the first index of each type
107                 switch( codec_context->codec_type ) 
108                 {
109                         case CODEC_TYPE_VIDEO:
110                                 if ( *video_index < 0 )
111                                         *video_index = i;
112                                 break;
113                         case CODEC_TYPE_AUDIO:
114                                 if ( *audio_index < 0 )
115                                         *audio_index = i;
116                                 break;
117                         default:
118                                 break;
119                 }
120         }
121 }
122
123 /** Producer file destructor.
124 */
125
126 static void producer_file_close( void *context )
127 {
128         if ( context != NULL )
129         {
130                 // Lock the mutex now
131                 pthread_mutex_lock( &avformat_mutex );
132
133                 // Close the file
134                 av_close_input_file( context );
135
136                 // Unlock the mutex now
137                 pthread_mutex_unlock( &avformat_mutex );
138         }
139 }
140
141 /** Producer file destructor.
142 */
143
144 static void producer_codec_close( void *codec )
145 {
146         if ( codec != NULL )
147         {
148                 // Lock the mutex now
149                 pthread_mutex_lock( &avformat_mutex );
150
151                 // Close the file
152                 avcodec_close( codec );
153
154                 // Unlock the mutex now
155                 pthread_mutex_unlock( &avformat_mutex );
156         }
157 }
158
159 /** Open the file.
160 */
161
162 static int producer_open( mlt_producer this, char *file )
163 {
164         // Return an error code (0 == no error)
165         int error = 0;
166
167         // Context for avformat
168         AVFormatContext *context = NULL;
169
170         // Get the properties
171         mlt_properties properties = mlt_producer_properties( this );
172
173         // We will treat everything with the producer fps
174         double fps = mlt_properties_get_double( properties, "fps" );
175
176         // Lock the mutex now
177         pthread_mutex_lock( &avformat_mutex );
178
179         // Now attempt to open the file
180         error = av_open_input_file( &context, file, NULL, 0, NULL );
181         error = error < 0;
182
183         // If successful, then try to get additional info
184         if ( error == 0 )
185         {
186                 // Get the stream info
187                 error = av_find_stream_info( context ) < 0;
188
189                 // Continue if no error
190                 if ( error == 0 )
191                 {
192                         // We will default to the first audio and video streams found
193                         int audio_index = -1;
194                         int video_index = -1;
195
196                         // Now set properties where we can (use default unknowns if required)
197                         if ( context->duration != AV_NOPTS_VALUE ) 
198                         {
199                                 // This isn't going to be accurate for all formats
200                                 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps );
201                                 mlt_properties_set_position( properties, "out", frames - 2 );
202                                 mlt_properties_set_position( properties, "length", frames - 1 );
203                         }
204
205                         // Find default audio and video streams
206                         find_default_streams( context, &audio_index, &video_index );
207
208                         // Store selected audio and video indexes on properties
209                         mlt_properties_set_int( properties, "audio_index", audio_index );
210                         mlt_properties_set_int( properties, "video_index", video_index );
211
212                         // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
213                         if ( audio_index != -1 && video_index != -1 )
214                         {
215                                 // We'll use the open one as our video_context
216                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
217
218                                 // And open again for our audio context
219                                 av_open_input_file( &context, file, NULL, 0, NULL );
220                                 av_find_stream_info( context );
221
222                                 // Audio context
223                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
224                         }
225                         else if ( video_index != -1 )
226                         {
227                                 // We only have a video context
228                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
229                         }
230                         else if ( audio_index != -1 )
231                         {
232                                 // We only have an audio context
233                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
234                         }
235                         else
236                         {
237                                 // Something has gone wrong
238                                 error = -1;
239                         }
240                 }
241         }
242
243         // Unlock the mutex now
244         pthread_mutex_unlock( &avformat_mutex );
245
246         return error;
247 }
248
249 /** Convert a frame position to a time code.
250 */
251
252 static double producer_time_of_frame( mlt_producer this, mlt_position position )
253 {
254         // Get the properties
255         mlt_properties properties = mlt_producer_properties( this );
256
257         // Obtain the fps
258         double fps = mlt_properties_get_double( properties, "fps" );
259
260         // Do the calc
261         return ( double )position / fps;
262 }
263
264 /** Get an image from a frame.
265 */
266
267 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
268 {
269         // Get the properties from the frame
270         mlt_properties frame_properties = mlt_frame_properties( frame );
271
272         // Obtain the frame number of this frame
273         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
274
275         // Get the producer 
276         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
277
278         // Get the producer properties
279         mlt_properties properties = mlt_producer_properties( this );
280
281         // Fetch the video_context
282         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
283
284         // Get the video_index
285         int index = mlt_properties_get_int( properties, "video_index" );
286
287         // Obtain the expected frame numer
288         mlt_position expected = mlt_properties_get_position( properties, "video_expected" );
289
290         // Calculate the real time code
291         double real_timecode = producer_time_of_frame( this, position );
292
293         // Get the video stream
294         AVStream *stream = context->streams[ index ];
295
296         // Get codec context
297         AVCodecContext *codec_context = &stream->codec;
298
299         // Packet
300         AVPacket pkt;
301
302         // Get the conversion frame
303         AVPicture *output = mlt_properties_get_data( properties, "video_output_frame", NULL );
304
305         // Special case pause handling flag
306         int paused = 0;
307
308         // Special case ffwd handling
309         int ignore = 0;
310
311         // Current time calcs
312         double current_time = mlt_properties_get_double( properties, "current_time" );
313
314         // We may want to use the source fps if available
315         double source_fps = mlt_properties_get_double( properties, "source_fps" );
316
317         // Set the result arguments that we know here (only *buffer is now required)
318         *format = mlt_image_yuv422;
319         *width = codec_context->width;
320         *height = codec_context->height;
321
322         // Set this on the frame properties
323         mlt_properties_set_int( frame_properties, "width", *width );
324         mlt_properties_set_int( frame_properties, "height", *height );
325
326         // Lock the mutex now
327         pthread_mutex_lock( &avformat_mutex );
328
329         // Construct an AVFrame for YUV422 conversion
330         if ( output == NULL )
331         {
332                 int size = avpicture_get_size( PIX_FMT_YUV422, *width, *height );
333                 size += *width * 2;
334                 uint8_t *buf = mlt_pool_alloc( size );
335                 output = mlt_pool_alloc( sizeof( AVPicture ) );
336                 //memset( output, 0, sizeof( AVPicture ) );
337                 avpicture_fill( output, buf, PIX_FMT_YUV422, *width, *height );
338                 mlt_properties_set_data( properties, "video_output_frame", output, 0, ( mlt_destructor )mlt_pool_release, NULL );
339                 mlt_properties_set_data( properties, "video_output_buffer", buf, 0, ( mlt_destructor )mlt_pool_release, NULL );
340         }
341
342         // Seek if necessary
343         if ( position != expected )
344         {
345                 if ( position + 1 == expected )
346                 {
347                         // We're paused - use last image
348                         paused = 1;
349                 }
350                 else if ( position > expected && ( position - expected ) < 250 )
351                 {
352                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
353                         ignore = position - expected;
354                 }
355                 else
356                 {
357                         // Set to the real timecode
358                         av_seek_frame( context, -1, real_timecode * 1000000.0 );
359         
360                         // Remove the cached info relating to the previous position
361                         mlt_properties_set_double( properties, "current_time", real_timecode );
362                         mlt_properties_set_data( properties, "current_image", NULL, 0, NULL, NULL );
363                 }
364         }
365         
366         // Duplicate the last image if necessary
367         if ( mlt_properties_get_data( properties, "current_image", NULL ) != NULL &&
368                  ( paused || mlt_properties_get_double( properties, "current_time" ) >= real_timecode ) )
369         {
370                 // Get current image and size
371                 int size = 0;
372                 uint8_t *image = mlt_properties_get_data( properties, "current_image", &size );
373
374                 // Duplicate it
375                 *buffer = mlt_pool_alloc( size );
376                 memcpy( *buffer, image, size );
377
378                 // Set this on the frame properties
379                 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
380         }
381         else
382         {
383                 int ret = 0;
384                 int got_picture = 0;
385                 AVFrame frame;
386
387                 memset( &pkt, 0, sizeof( pkt ) );
388                 memset( &frame, 0, sizeof( frame ) );
389
390                 while( ret >= 0 && !got_picture )
391                 {
392                         // Read a packet
393                         ret = av_read_frame( context, &pkt );
394
395                         // We only deal with video from the selected video_index
396                         if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
397                         {
398                                 // Decode the image
399                                 ret = avcodec_decode_video( codec_context, &frame, &got_picture, pkt.data, pkt.size );
400
401                                 if ( got_picture )
402                                 {
403                                         if ( pkt.pts != AV_NOPTS_VALUE && pkt.pts != 0  )
404                                                 current_time = ( double )pkt.pts / 1000000.0;
405                                         else
406                                                 current_time = real_timecode;
407
408                                         // Handle ignore
409                                         if ( current_time < real_timecode )
410                                         {
411                                                 ignore = 0;
412                                                 got_picture = 0;
413                                         }
414                                         else if ( current_time >= real_timecode )
415                                         {
416                                                 //current_time = real_timecode;
417                                                 ignore = 0;
418                                         }
419                                         else if ( ignore -- )
420                                         {
421                                                 got_picture = 0;
422                                         }
423                                 }
424                         }
425
426                         // We're finished with this packet regardless
427                         av_free_packet( &pkt );
428                 }
429
430                 // Now handle the picture if we have one
431                 if ( got_picture )
432                 {
433                         // Get current image and size
434                         int size = 0;
435                         uint8_t *image = mlt_properties_get_data( properties, "current_image", &size );
436
437                         if ( image == NULL || size != *width * *height * 2 )
438                         {
439                                 size = *width * ( *height + 1 ) * 2;
440                                 image = mlt_pool_alloc( size );
441                                 mlt_properties_set_data( properties, "current_image", image, size, ( mlt_destructor )mlt_pool_release, NULL );
442                         }
443
444                         *buffer = mlt_pool_alloc( size );
445
446                         // EXPERIMENTAL IMAGE NORMALISATIONS
447                         if ( codec_context->pix_fmt == PIX_FMT_YUV420P )
448                         {
449                                 register int i, j;
450                                 register int half = *width >> 1;
451                                 register uint8_t *Y = ( ( AVPicture * )&frame )->data[ 0 ];
452                                 register uint8_t *U = ( ( AVPicture * )&frame )->data[ 1 ];
453                                 register uint8_t *V = ( ( AVPicture * )&frame )->data[ 2 ];
454                                 register uint8_t *d = *buffer;
455                                 register uint8_t *y, *u, *v;
456
457                                 i = *height >> 1;
458                                 while ( i -- )
459                                 {
460                                         y = Y;
461                                         u = U;
462                                         v = V;
463                                         j = half;
464                                         while ( j -- )
465                                         {
466                                                 *d ++ = *y ++;
467                                                 *d ++ = *u ++;
468                                                 *d ++ = *y ++;
469                                                 *d ++ = *v ++;
470                                         }
471
472                                         Y += ( ( AVPicture * )&frame )->linesize[ 0 ];
473                                         y = Y;
474                                         u = U;
475                                         v = V;
476                                         j = half;
477                                         while ( j -- )
478                                         {
479                                                 *d ++ = *y ++;
480                                                 *d ++ = *u ++;
481                                                 *d ++ = *y ++;
482                                                 *d ++ = *v ++;
483                                         }
484
485                                         Y += ( ( AVPicture * )&frame )->linesize[ 0 ];
486                                         U += ( ( AVPicture * )&frame )->linesize[ 1 ];
487                                         V += ( ( AVPicture * )&frame )->linesize[ 2 ];
488                                 }
489                         }
490                         else
491                         {
492                                 img_convert( output, PIX_FMT_YUV422, (AVPicture *)&frame, codec_context->pix_fmt, *width, *height );
493                                 memcpy( *buffer, output->data[ 0 ], size );
494                         }
495
496                         memcpy( image, *buffer, size );
497                         mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
498
499                         if ( current_time == 0 && source_fps != 0 )
500                         {
501                                 double fps = mlt_properties_get_double( properties, "fps" );
502                                 current_time = ceil( source_fps * ( double )position / fps ) * ( 1 / source_fps );
503                                 mlt_properties_set_double( properties, "current_time", current_time );
504                         }
505                         else
506                         {
507                                 mlt_properties_set_double( properties, "current_time", current_time );
508                         }
509                 }
510         }
511
512         // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
513         mlt_properties_set_position( properties, "video_expected", position + 1 );
514
515         // Unlock the mutex now
516         pthread_mutex_unlock( &avformat_mutex );
517
518         return 0;
519 }
520
521 /** Set up video handling.
522 */
523
524 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
525 {
526         // Get the properties
527         mlt_properties properties = mlt_producer_properties( this );
528
529         // Fetch the video_context
530         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
531
532         // Get the video_index
533         int index = mlt_properties_get_int( properties, "video_index" );
534
535         // Get the frame properties
536         mlt_properties frame_properties = mlt_frame_properties( frame );
537
538         // Lock the mutex now
539         pthread_mutex_lock( &avformat_mutex );
540
541         if ( context != NULL && index != -1 )
542         {
543                 // Get the video stream
544                 AVStream *stream = context->streams[ index ];
545
546                 // Get codec context
547                 AVCodecContext *codec_context = &stream->codec;
548
549                 // Get the codec
550                 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
551
552                 // Initialise the codec if necessary
553                 if ( codec == NULL )
554                 {
555                         // Find the codec
556                         codec = avcodec_find_decoder( codec_context->codec_id );
557
558                         // If we don't have a codec and we can't initialise it, we can't do much more...
559                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
560                         {
561                                 double aspect_ratio = 0;
562                                 double source_fps = 0;
563
564                                 // Set aspect ratio
565                                 if ( codec_context->sample_aspect_ratio.num == 0 ) 
566                                         aspect_ratio = 0;
567                                 else
568                                         aspect_ratio = av_q2d( codec_context->sample_aspect_ratio ) * codec_context->width / codec_context->height;
569
570                                 // XXX: This assumes square pixels!
571                         if (aspect_ratio <= 0.0)
572                                         aspect_ratio = ( double )codec_context->width / ( double )codec_context->height;
573
574                                 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
575                                 fprintf( stderr, "AVFORMAT: sample aspect %f computed display aspect %f\n", av_q2d( codec_context->sample_aspect_ratio ), aspect_ratio );
576
577                                 // Determine the fps
578                                 source_fps = ( double )codec_context->frame_rate / codec_context->frame_rate_base;
579
580                                 // We'll use fps if it's available
581                                 if ( source_fps > 0 && source_fps < 30 )
582                                         mlt_properties_set_double( properties, "source_fps", source_fps );
583
584                                 // Now store the codec with its destructor
585                                 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
586
587                                 // Set to the real timecode
588                                 av_seek_frame( context, -1, 0 );
589                         }
590                         else
591                         {
592                                 // Remember that we can't use this later
593                                 mlt_properties_set_int( properties, "video_index", -1 );
594                         }
595                 }
596
597                 // No codec, no show...
598                 if ( codec != NULL )
599                 {
600                         mlt_frame_push_get_image( frame, producer_get_image );
601                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
602                 }
603                 else
604                 {
605                         mlt_properties_set_int( frame_properties, "test_image", 1 );
606                 }
607         }
608         else
609         {
610                 mlt_properties_set_int( frame_properties, "test_image", 1 );
611         }
612
613         // Unlock the mutex now
614         pthread_mutex_unlock( &avformat_mutex );
615 }
616
617 /** Get the audio from a frame.
618 */
619
620 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
621 {
622         // Get the properties from the frame
623         mlt_properties frame_properties = mlt_frame_properties( frame );
624
625         // Obtain the frame number of this frame
626         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
627
628         // Get the producer 
629         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
630
631         // Get the producer properties
632         mlt_properties properties = mlt_producer_properties( this );
633
634         // Fetch the audio_context
635         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
636
637         // Get the audio_index
638         int index = mlt_properties_get_int( properties, "audio_index" );
639
640         // Obtain the expected frame numer
641         mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
642
643         // Obtain the resample context if it exists (not always needed)
644         ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
645
646         // Obtain the audio buffer
647         int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
648
649         // Get amount of audio used
650         int audio_used =  mlt_properties_get_int( properties, "audio_used" );
651
652         // Calculate the real time code
653         double real_timecode = producer_time_of_frame( this, position );
654
655         // Get the audio stream
656         AVStream *stream = context->streams[ index ];
657
658         // Get codec context
659         AVCodecContext *codec_context = &stream->codec;
660
661         // Packet
662         AVPacket pkt;
663
664         // Number of frames to ignore (for ffwd)
665         int ignore = 0;
666
667         // Flag for paused (silence) 
668         int paused = 0;
669         int locked = 0;
670
671         // Lock the mutex now
672         pthread_mutex_lock( &avformat_mutex );
673
674         // Check for resample and create if necessary
675         if ( resample == NULL )
676         {
677                 // Create the resampler
678                 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
679
680                 // And store it on properties
681                 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
682         }
683
684         // Check for audio buffer and create if necessary
685         if ( audio_buffer == NULL )
686         {
687                 // Allocate the audio buffer
688                 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
689
690                 // And store it on properties for reuse
691                 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
692         }
693
694         // Seek if necessary
695         if ( position != expected )
696         {
697                 if ( position + 1 == expected )
698                 {
699                         // We're paused - silence required
700                         paused = 1;
701                 }
702                 else if ( position > expected && ( position - expected ) < 250 )
703                 {
704                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
705                         ignore = position - expected;
706                 }
707                 else
708                 {
709                         // Set to the real timecode
710                         av_seek_frame( context, -1, real_timecode * 1000000.0 );
711
712                         // Clear the usage in the audio buffer
713                         audio_used = 0;
714
715                         locked = 1;
716                 }
717         }
718
719         // Get the audio if required
720         if ( !paused )
721         {
722                 int ret = 0;
723                 int got_audio = 0;
724                 int16_t *temp = mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
725
726                 memset( &pkt, 0, sizeof( pkt ) );
727
728                 while( ret >= 0 && !got_audio )
729                 {
730                         // Check if the buffer already contains the samples required
731                         if ( audio_used >= *samples && ignore == 0 )
732                         {
733                                 got_audio = 1;
734                                 break;
735                         }
736
737                         // Read a packet
738                         ret = av_read_frame( context, &pkt );
739
740                 int len = pkt.size;
741                 uint8_t *ptr = pkt.data;
742                         int data_size;
743
744                         // We only deal with video from the selected video_index
745                         while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
746                         {
747                                 // Decode the audio
748                                 ret = avcodec_decode_audio( codec_context, temp, &data_size, ptr, len );
749
750                                 if ( ret < 0 )
751                                 {
752                                         ret = 0;
753                                         break;
754                                 }
755
756                                 len -= ret;
757                                 ptr += ret;
758
759                                 if ( data_size > 0 )
760                                 {
761                                         int size_out = audio_resample( resample, &audio_buffer[ audio_used * *channels ], temp, data_size / ( codec_context->channels * sizeof( int16_t ) ) );
762
763                                         audio_used += size_out;
764
765                                         // Handle ignore
766                                         while ( ignore && audio_used > *samples )
767                                         {
768                                                 ignore --;
769                                                 audio_used -= *samples;
770                                                 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
771                                         }
772                                 }
773
774                                 // If we're behind, ignore this packet
775                                 float current_pts = (float)pkt.pts / 1000000.0;
776                                 double discrepancy = mlt_properties_get_double( properties, "discrepancy" );
777                                 if ( current_pts != 0 && real_timecode != 0 )
778                                 {
779                                         if ( discrepancy != 1 )
780                                                 discrepancy = ( discrepancy + ( real_timecode / current_pts ) ) / 2;
781                                         else
782                                                 discrepancy = real_timecode / current_pts;
783                                         if ( discrepancy > 0.9 && discrepancy < 1.1 )
784                                                 discrepancy = 1.0;
785                                         else
786                                                 discrepancy = floor( discrepancy + 0.5 );
787
788                                         if ( discrepancy == 0 )
789                                                 discrepancy = 1.0;
790
791                                         mlt_properties_set_double( properties, "discrepancy", discrepancy );
792                                 }
793
794                                 if ( !ignore && discrepancy * current_pts <= ( real_timecode - 0.02 ) )
795                                         ignore = 1;
796                         }
797
798                         // We're finished with this packet regardless
799                         av_free_packet( &pkt );
800                 }
801
802                 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
803                 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
804
805                 // Now handle the audio if we have enough
806                 if ( audio_used >= *samples )
807                 {
808                         memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
809                         audio_used -= *samples;
810                         memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
811                 }
812                 else
813                 {
814                         memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
815                 }
816                 
817                 // Store the number of audio samples still available
818                 mlt_properties_set_int( properties, "audio_used", audio_used );
819
820                 // Release the temporary audio
821                 mlt_pool_release( temp );
822         }
823         else
824         {
825                 // Get silence and don't touch the context
826                 frame->get_audio = NULL;
827                 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
828         }
829
830         // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
831         mlt_properties_set_position( properties, "audio_expected", position + 1 );
832
833         // Unlock the mutex now
834         pthread_mutex_unlock( &avformat_mutex );
835
836         return 0;
837 }
838
839 /** Set up audio handling.
840 */
841
842 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
843 {
844         // Get the properties
845         mlt_properties properties = mlt_producer_properties( this );
846
847         // Fetch the audio_context
848         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
849
850         // Get the audio_index
851         int index = mlt_properties_get_int( properties, "audio_index" );
852
853         // Lock the mutex now
854         pthread_mutex_lock( &avformat_mutex );
855
856         // Deal with audio context
857         if ( context != NULL && index != -1 )
858         {
859                 // Get the frame properties
860                 mlt_properties frame_properties = mlt_frame_properties( frame );
861
862                 // Get the audio stream
863                 AVStream *stream = context->streams[ index ];
864
865                 // Get codec context
866                 AVCodecContext *codec_context = &stream->codec;
867
868                 // Get the codec
869                 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
870
871                 // Initialise the codec if necessary
872                 if ( codec == NULL )
873                 {
874                         // Find the codec
875                         codec = avcodec_find_decoder( codec_context->codec_id );
876
877                         // If we don't have a codec and we can't initialise it, we can't do much more...
878                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
879                         {
880                                 // Now store the codec with its destructor
881                                 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
882
883                         }
884                         else
885                         {
886                                 // Remember that we can't use this later
887                                 mlt_properties_set_int( properties, "audio_index", -1 );
888                         }
889                 }
890
891                 // No codec, no show...
892                 if ( codec != NULL )
893                 {
894                         frame->get_audio = producer_get_audio;
895                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
896                 }
897         }
898
899         // Unlock the mutex now
900         pthread_mutex_unlock( &avformat_mutex );
901 }
902
903 /** Our get frame implementation.
904 */
905
906 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
907 {
908         // Create an empty frame
909         *frame = mlt_frame_init( );
910
911         // Update timecode on the frame we're creating
912         mlt_frame_set_position( *frame, mlt_producer_position( this ) );
913
914         // Set the position of this producer
915         mlt_properties_set_position( mlt_frame_properties( *frame ), "avformat_position", mlt_producer_get_in( this ) + mlt_producer_position( this ) );
916
917         // Set up the video
918         producer_set_up_video( this, *frame );
919
920         // Set up the audio
921         producer_set_up_audio( this, *frame );
922
923         // Set the aspect_ratio
924         mlt_properties_set_double( mlt_frame_properties( *frame ), "aspect_ratio", mlt_properties_get_double( mlt_producer_properties( this ), "aspect_ratio" ) );
925
926         // Calculate the next timecode
927         mlt_producer_prepare_next( this );
928
929         return 0;
930 }