]> git.sesse.net Git - mlt/blob - src/modules/avformat/producer_avformat.c
ec20a7a7ccc4ccedd48fe4db76e55b7631bf7663
[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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, 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 <avformat.h>
29 #ifdef SWSCALE
30 #include <swscale.h>
31 #endif
32
33 // System header files
34 #include <stdlib.h>
35 #include <string.h>
36 #include <pthread.h>
37 #include <math.h>
38
39 void avformat_lock( );
40 void avformat_unlock( );
41
42 // Forward references.
43 static int producer_open( mlt_producer this, char *file );
44 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
45
46 /** Constructor for libavformat.
47 */
48
49 mlt_producer producer_avformat_init( char *file )
50 {
51         mlt_producer this = NULL;
52
53         // Check that we have a non-NULL argument
54         if ( file != NULL )
55         {
56                 // Construct the producer
57                 this = calloc( 1, sizeof( struct mlt_producer_s ) );
58
59                 // Initialise it
60                 if ( mlt_producer_init( this, NULL ) == 0 )
61                 {
62                         // Get the properties
63                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
64
65                         // Set the resource property (required for all producers)
66                         mlt_properties_set( properties, "resource", file );
67
68                         // Register our get_frame implementation
69                         this->get_frame = producer_get_frame;
70
71                         // Open the file
72                         if ( producer_open( this, file ) != 0 )
73                         {
74                                 // Clean up
75                                 mlt_producer_close( this );
76                                 this = NULL;
77                         }
78                 }
79         }
80
81         return this;
82 }
83
84 /** Find the default streams.
85 */
86
87 static void find_default_streams( AVFormatContext *context, int *audio_index, int *video_index )
88 {
89         int i;
90
91         // Allow for multiple audio and video streams in the file and select first of each (if available)
92         for( i = 0; i < context->nb_streams; i++ ) 
93         {
94                 // Get the codec context
95                 AVCodecContext *codec_context = context->streams[ i ]->codec;
96
97                 if ( avcodec_find_decoder( codec_context->codec_id ) == NULL )
98                         continue;
99
100                 // Determine the type and obtain the first index of each type
101                 switch( codec_context->codec_type ) 
102                 {
103                         case CODEC_TYPE_VIDEO:
104                                 if ( *video_index < 0 )
105                                         *video_index = i;
106                                 break;
107                         case CODEC_TYPE_AUDIO:
108                                 if ( *audio_index < 0 )
109                                         *audio_index = i;
110                                 break;
111                         default:
112                                 break;
113                 }
114         }
115 }
116
117 /** Producer file destructor.
118 */
119
120 static void producer_file_close( void *context )
121 {
122         if ( context != NULL )
123         {
124                 // Lock the mutex now
125                 avformat_lock( );
126
127                 // Close the file
128                 av_close_input_file( context );
129
130                 // Unlock the mutex now
131                 avformat_unlock( );
132         }
133 }
134
135 /** Producer file destructor.
136 */
137
138 static void producer_codec_close( void *codec )
139 {
140         if ( codec != NULL )
141         {
142                 // Lock the mutex now
143                 avformat_lock( );
144
145                 // Close the file
146                 avcodec_close( codec );
147
148                 // Unlock the mutex now
149                 avformat_unlock( );
150         }
151 }
152
153 /** Open the file.
154 */
155
156 static int producer_open( mlt_producer this, char *file )
157 {
158         // Return an error code (0 == no error)
159         int error = 0;
160
161         // Context for avformat
162         AVFormatContext *context = NULL;
163
164         // Get the properties
165         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
166
167         // We will treat everything with the producer fps
168         double fps = mlt_producer_get_fps( this );
169
170         // Lock the mutex now
171         avformat_lock( );
172         
173         // If "MRL", then create AVInputFormat
174         AVInputFormat *format = NULL;
175         AVFormatParameters *params = NULL;
176         char *standard = NULL;
177         char *mrl = strchr( file, ':' );
178
179         // AV option (0 = both, 1 = video, 2 = audio)
180         int av = 0;
181         
182         // Setting lowest log level
183         av_log_set_level( -1 );
184
185         // Only if there is not a protocol specification that avformat can handle
186         if ( mrl && !url_exist( file ) )
187         {
188                 // 'file' becomes format abbreviation
189                 mrl[0] = 0;
190         
191                 // Lookup the format
192                 format = av_find_input_format( file );
193                 
194                 // Eat the format designator
195                 file = ++mrl;
196                 
197                 if ( format )
198                 {
199                         // Allocate params
200                         params = calloc( sizeof( AVFormatParameters ), 1 );
201                         
202                         // These are required by video4linux (defaults)
203                         params->width = 640;
204                         params->height = 480;
205                         params->time_base= (AVRational){1,25};
206                         // params->device = file;
207                         params->channels = 2;
208                         params->sample_rate = 48000;
209                 }
210                 
211                 // XXX: this does not work anymore since avdevice
212                 // TODO: make producer_avddevice?
213                 // Parse out params
214                 mrl = strchr( file, '?' );
215                 while ( mrl )
216                 {
217                         mrl[0] = 0;
218                         char *name = strdup( ++mrl );
219                         char *value = strchr( name, ':' );
220                         if ( value )
221                         {
222                                 value[0] = 0;
223                                 value++;
224                                 char *t = strchr( value, '&' );
225                                 if ( t )
226                                         t[0] = 0;
227                                 if ( !strcmp( name, "frame_rate" ) )
228                                         params->time_base.den = atoi( value );
229                                 else if ( !strcmp( name, "frame_rate_base" ) )
230                                         params->time_base.num = atoi( value );
231                                 else if ( !strcmp( name, "sample_rate" ) )
232                                         params->sample_rate = atoi( value );
233                                 else if ( !strcmp( name, "channels" ) )
234                                         params->channels = atoi( value );
235                                 else if ( !strcmp( name, "width" ) )
236                                         params->width = atoi( value );
237                                 else if ( !strcmp( name, "height" ) )
238                                         params->height = atoi( value );
239                                 else if ( !strcmp( name, "standard" ) )
240                                 {
241                                         standard = strdup( value );
242                                         params->standard = standard;
243                                 }
244                                 else if ( !strcmp( name, "av" ) )
245                                         av = atoi( value );
246                         }
247                         free( name );
248                         mrl = strchr( mrl, '&' );
249                 }
250         }
251
252         // Now attempt to open the file
253         error = av_open_input_file( &context, file, format, 0, params ) < 0;
254         
255         // Cleanup AVFormatParameters
256         free( standard );
257         free( params );
258
259         // If successful, then try to get additional info
260         if ( error == 0 )
261         {
262                 // Get the stream info
263                 error = av_find_stream_info( context ) < 0;
264
265                 // Continue if no error
266                 if ( error == 0 )
267                 {
268                         // We will default to the first audio and video streams found
269                         int audio_index = -1;
270                         int video_index = -1;
271                         int av_bypass = 0;
272
273                         // Now set properties where we can (use default unknowns if required)
274                         if ( context->duration != AV_NOPTS_VALUE ) 
275                         {
276                                 // This isn't going to be accurate for all formats
277                                 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
278                                 mlt_properties_set_position( properties, "out", frames - 1 );
279                                 mlt_properties_set_position( properties, "length", frames );
280                         }
281
282                         // Find default audio and video streams
283                         find_default_streams( context, &audio_index, &video_index );
284
285             if ( context->start_time != AV_NOPTS_VALUE )
286                 mlt_properties_set_double( properties, "_start_time", context->start_time );
287                         
288                         // Check if we're seekable (something funny about mpeg here :-/)
289                         if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 ) )
290                         {
291                                 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
292                                 mlt_properties_set_data( properties, "dummy_context", context, 0, producer_file_close, NULL );
293                                 av_open_input_file( &context, file, NULL, 0, NULL );
294                                 av_find_stream_info( context );
295                         }
296                         else
297                                 av_bypass = 1;
298
299                         // Store selected audio and video indexes on properties
300                         mlt_properties_set_int( properties, "audio_index", audio_index );
301                         mlt_properties_set_int( properties, "video_index", video_index );
302                         mlt_properties_set_int( properties, "_last_position", -1 );
303
304                         // Fetch the width, height and aspect ratio
305                         if ( video_index != -1 )
306                         {
307                                 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
308                                 mlt_properties_set_int( properties, "width", codec_context->width );
309                                 mlt_properties_set_int( properties, "height", codec_context->height );
310                                 mlt_properties_set_double( properties, "aspect_ratio", av_q2d( codec_context->sample_aspect_ratio ) );
311                         }
312
313                         // Read Metadata
314                         if (context->title != NULL) 
315                                 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
316                         if (context->author != NULL) 
317                                 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
318                         if (context->copyright != NULL) 
319                                 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
320                         if (context->comment != NULL) 
321                                 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
322                         if (context->album != NULL) 
323                                 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
324                         if (context->year != 0) 
325                                 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
326                         if (context->track != 0) 
327                                 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
328                         
329                         // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
330                         if ( av == 0 && !av_bypass && audio_index != -1 && video_index != -1 )
331                         {
332                                 // We'll use the open one as our video_context
333                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
334
335                                 // And open again for our audio context
336                                 av_open_input_file( &context, file, NULL, 0, NULL );
337                                 av_find_stream_info( context );
338
339                                 // Audio context
340                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
341                         }
342                         else if ( av != 2 && video_index != -1 )
343                         {
344                                 // We only have a video context
345                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
346                         }
347                         else if ( audio_index != -1 )
348                         {
349                                 // We only have an audio context
350                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
351                         }
352                         else
353                         {
354                                 // Something has gone wrong
355                                 error = -1;
356                         }
357
358                         mlt_properties_set_int( properties, "av_bypass", av_bypass );
359                 }
360         }
361
362         // Unlock the mutex now
363         avformat_unlock( );
364
365         return error;
366 }
367
368 /** Convert a frame position to a time code.
369 */
370
371 static double producer_time_of_frame( mlt_producer this, mlt_position position )
372 {
373         return ( double )position / mlt_producer_get_fps( this );
374 }
375
376 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
377 {
378 #ifdef SWSCALE
379         if ( format == mlt_image_yuv420p )
380         {
381                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
382                         width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
383                 AVPicture output;
384                 output.data[0] = buffer;
385                 output.data[1] = buffer + width * height;
386                 output.data[2] = buffer + ( 3 * width * height ) / 2;
387                 output.linesize[0] = width;
388                 output.linesize[1] = width >> 1;
389                 output.linesize[2] = width >> 1;
390                 sws_scale( context, frame->data, frame->linesize, 0, height,
391                         output.data, output.linesize);
392                 sws_freeContext( context );
393         }
394         else if ( format == mlt_image_rgb24 )
395         {
396                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
397                         width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
398                 AVPicture output;
399                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
400                 sws_scale( context, frame->data, frame->linesize, 0, height,
401                         output.data, output.linesize);
402                 sws_freeContext( context );
403         }
404         else
405         {
406                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
407                         width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
408                 AVPicture output;
409                 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
410                 sws_scale( context, frame->data, frame->linesize, 0, height,
411                         output.data, output.linesize);
412                 sws_freeContext( context );
413         }
414 #else
415         if ( format == mlt_image_yuv420p )
416         {
417                 AVPicture pict;
418                 pict.data[0] = buffer;
419                 pict.data[1] = buffer + width * height;
420                 pict.data[2] = buffer + ( 3 * width * height ) / 2;
421                 pict.linesize[0] = width;
422                 pict.linesize[1] = width >> 1;
423                 pict.linesize[2] = width >> 1;
424                 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
425         }
426         else if ( format == mlt_image_rgb24 )
427         {
428                 AVPicture output;
429                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
430                 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
431         }
432         else
433         {
434                 AVPicture output;
435                 avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
436                 img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
437         }
438 #endif
439 }
440
441 /** Get an image from a frame.
442 */
443
444 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
445 {
446         // Get the properties from the frame
447         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
448
449         // Obtain the frame number of this frame
450         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
451
452         // Get the producer 
453         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
454
455         // Get the producer properties
456         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
457
458         // Fetch the video_context
459         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
460
461         // Get the video_index
462         int index = mlt_properties_get_int( properties, "video_index" );
463
464         // Obtain the expected frame numer
465         mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
466
467         // Get the video stream
468         AVStream *stream = context->streams[ index ];
469
470         // Get codec context
471         AVCodecContext *codec_context = stream->codec;
472
473         // Packet
474         AVPacket pkt;
475
476         // Get the conversion frame
477         AVFrame *av_frame = mlt_properties_get_data( properties, "av_frame", NULL );
478
479         // Special case pause handling flag
480         int paused = 0;
481
482         // Special case ffwd handling
483         int ignore = 0;
484
485         // We may want to use the source fps if available
486         double source_fps = mlt_properties_get_double( properties, "source_fps" );
487         double fps = mlt_producer_get_fps( this );
488
489         // This is the physical frame position in the source
490         int req_position = ( int )( position / fps * source_fps + 0.5 );
491
492         // Get the seekable status
493         int seekable = mlt_properties_get_int( properties, "seekable" );
494
495         // Generate the size in bytes
496         int size = 0; 
497
498         // Hopefully provide better support for streams...
499         int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
500
501         // Determines if we have to decode all frames in a sequence
502         int must_decode = 1;
503
504         // Set the result arguments that we know here (only *buffer is now required)
505         *width = codec_context->width;
506         *height = codec_context->height;
507
508         switch ( *format )
509         {
510                 case mlt_image_yuv420p:
511                         size = *width * 3 * ( *height + 1 ) / 2;
512                         break;
513                 case mlt_image_rgb24:
514                         size = *width * ( *height + 1 ) * 3;
515                         break;
516                 default:
517                         *format = mlt_image_yuv422;
518                         size = *width * ( *height + 1 ) * 2;
519                         break;
520         }
521
522         // Set this on the frame properties
523         mlt_properties_set_int( frame_properties, "width", *width );
524         mlt_properties_set_int( frame_properties, "height", *height );
525
526         // Construct the output image
527         *buffer = mlt_pool_alloc( size );
528
529         // Temporary hack to improve intra frame only
530         must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
531                                   strcmp( codec_context->codec->name, "rawvideo" ) &&
532                                   strcmp( codec_context->codec->name, "dvvideo" );
533
534         // Seek if necessary
535         if ( position != expected )
536         {
537                 if ( av_frame != NULL && position + 1 == expected )
538                 {
539                         // We're paused - use last image
540                         paused = 1;
541                 }
542                 else if ( !seekable && position > expected && ( position - expected ) < 250 )
543                 {
544                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
545                         ignore = ( int )( ( position - expected ) / fps * source_fps );
546                 }
547                 else if ( seekable && ( position < expected || position - expected >= 12 ) )
548                 {
549                         // Calculate the timestamp for the requested frame
550                         int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
551                         if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
552                                 timestamp += context->start_time;
553                         if ( must_decode )
554                                 timestamp -= AV_TIME_BASE;
555                         if ( timestamp < 0 )
556                                 timestamp = 0;
557
558                         // Set to the timestamp
559                         av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
560         
561                         // Remove the cached info relating to the previous position
562                         mlt_properties_set_int( properties, "_current_position", -1 );
563                         mlt_properties_set_int( properties, "_last_position", -1 );
564                         mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
565                         av_frame = NULL;
566                 }
567         }
568
569         // Duplicate the last image if necessary (see comment on rawvideo below)
570         int current_position = mlt_properties_get_int( properties, "_current_position" );
571         int got_picture = mlt_properties_get_int( properties, "_got_picture" );
572         if ( av_frame != NULL && got_picture && ( paused || current_position >= req_position ) && av_bypass == 0 )
573         {
574                 // Duplicate it
575                 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
576
577                 // Set this on the frame properties
578                 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
579         }
580         else
581         {
582                 int ret = 0;
583                 int int_position = 0;
584                 got_picture = 0;
585
586                 av_init_packet( &pkt );
587
588                 // Construct an AVFrame for YUV422 conversion
589                 if ( av_frame == NULL )
590                 {
591                         av_frame = avcodec_alloc_frame( );
592                         mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
593                 }
594
595                 while( ret >= 0 && !got_picture )
596                 {
597                         // Read a packet
598                         ret = av_read_frame( context, &pkt );
599
600                         // We only deal with video from the selected video_index
601                         if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
602                         {
603                                 // Determine time code of the packet
604                                 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
605                                 if ( context->start_time != AV_NOPTS_VALUE )
606                                         int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
607                                 int last_position = mlt_properties_get_int( properties, "_last_position" );
608                                 if ( int_position == last_position )
609                                         int_position = last_position + 1;
610                                 mlt_properties_set_int( properties, "_last_position", int_position );
611
612                                 // Decode the image
613                                 if ( must_decode || int_position >= req_position )
614                                         ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
615
616                                 if ( got_picture )
617                                 {
618                                         // Handle ignore
619                                         if ( int_position < req_position )
620                                         {
621                                                 ignore = 0;
622                                                 got_picture = 0;
623                                         }
624                                         else if ( int_position >= req_position )
625                                         {
626                                                 ignore = 0;
627                                         }
628                                         else if ( ignore -- )
629                                         {
630                                                 got_picture = 0;
631                                         }
632                                 }
633                                 av_free_packet( &pkt );
634                         }
635                         else if ( ret >= 0 )
636                         {
637                                 av_free_packet( &pkt );
638                         }
639
640                         // Now handle the picture if we have one
641                         if ( got_picture )
642                         {
643                                 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
644                                 mlt_properties_set_int( frame_properties, "top_field_first", av_frame->top_field_first );
645                                 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
646                                 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
647                                 mlt_properties_set_int( properties, "_current_position", int_position );
648                                 mlt_properties_set_int( properties, "_got_picture", 1 );
649                         }
650                 }
651         }
652
653         // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
654         // above will break the pause behaviour - so we wipe the frame now
655         if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
656                 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
657
658         // Set the field order property for this frame
659         mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
660
661         // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
662         mlt_properties_set_position( properties, "_video_expected", position + 1 );
663
664         return 0;
665 }
666
667 /** Set up video handling.
668 */
669
670 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
671 {
672         // Get the properties
673         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
674
675         // Fetch the video_context
676         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
677
678         // Get the video_index
679         int index = mlt_properties_get_int( properties, "video_index" );
680
681         // Get the frame properties
682         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
683
684         if ( context != NULL && index != -1 )
685         {
686                 // Get the video stream
687                 AVStream *stream = context->streams[ index ];
688
689                 // Get codec context
690                 AVCodecContext *codec_context = stream->codec;
691
692                 // Get the codec
693                 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
694
695                 // Initialise the codec if necessary
696                 if ( codec == NULL )
697                 {
698                         // Find the codec
699                         codec = avcodec_find_decoder( codec_context->codec_id );
700
701                         // If we don't have a codec and we can't initialise it, we can't do much more...
702                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
703                         {
704                                 // Now store the codec with its destructor
705                                 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
706                         }
707                         else
708                         {
709                                 // Remember that we can't use this later
710                                 mlt_properties_set_int( properties, "video_index", -1 );
711                         }
712                 }
713
714                 // No codec, no show...
715                 if ( codec != NULL )
716                 {
717                         double source_fps = 0;
718                         int norm_aspect_ratio = mlt_properties_get_int( properties, "norm_aspect_ratio" );
719                         double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
720                         double aspect_ratio;
721
722                         // XXX: We won't know the real aspect ratio until an image is decoded
723                         // but we do need it now (to satisfy filter_resize) - take a guess based
724                         // on pal/ntsc
725                         if ( force_aspect_ratio > 0.0 )
726                         {
727                                 aspect_ratio = force_aspect_ratio;
728                         }
729                         else if ( !norm_aspect_ratio && codec_context->sample_aspect_ratio.num > 0 )
730                         {
731                                 aspect_ratio = av_q2d( codec_context->sample_aspect_ratio );
732                         }
733                         else
734                         {
735                                 int is_pal = mlt_producer_get_fps( this ) == 25.0;
736                                 aspect_ratio = is_pal ? 59.0/54.0 : 10.0/11.0;
737                         }
738
739                         // Determine the fps
740                         source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
741
742                         // We'll use fps if it's available
743                         if ( source_fps > 0 )
744                                 mlt_properties_set_double( properties, "source_fps", source_fps );
745                         else
746                                 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this ) );
747                         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
748                         
749                         // Set the width and height
750                         mlt_properties_set_int( frame_properties, "width", codec_context->width );
751                         mlt_properties_set_int( frame_properties, "height", codec_context->height );
752                         mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
753
754                         mlt_frame_push_get_image( frame, producer_get_image );
755                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
756                 }
757                 else
758                 {
759                         mlt_properties_set_int( frame_properties, "test_image", 1 );
760                 }
761         }
762         else
763         {
764                 mlt_properties_set_int( frame_properties, "test_image", 1 );
765         }
766 }
767
768 /** Get the audio from a frame.
769 */
770
771 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
772 {
773         // Get the properties from the frame
774         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
775
776         // Obtain the frame number of this frame
777         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
778
779         // Get the producer 
780         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
781
782         // Get the producer properties
783         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
784
785         // Fetch the audio_context
786         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
787
788         // Get the audio_index
789         int index = mlt_properties_get_int( properties, "audio_index" );
790
791         // Get the seekable status
792         int seekable = mlt_properties_get_int( properties, "seekable" );
793
794         // Obtain the expected frame numer
795         mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
796
797         // Obtain the resample context if it exists (not always needed)
798         ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
799
800         // Obtain the audio buffer
801         int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
802
803         // Get amount of audio used
804         int audio_used =  mlt_properties_get_int( properties, "_audio_used" );
805
806         // Calculate the real time code
807         double real_timecode = producer_time_of_frame( this, position );
808
809         // Get the audio stream
810         AVStream *stream = context->streams[ index ];
811
812         // Get codec context
813         AVCodecContext *codec_context = stream->codec;
814
815         // Packet
816         AVPacket pkt;
817
818         // Number of frames to ignore (for ffwd)
819         int ignore = 0;
820
821         // Flag for paused (silence) 
822         int paused = 0;
823
824         // Check for resample and create if necessary
825         if ( resample == NULL && codec_context->channels <= 2 )
826         {
827                 // Create the resampler
828                 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
829
830                 // And store it on properties
831                 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
832         }
833         else if ( resample == NULL )
834         {
835                 *channels = codec_context->channels;
836                 *frequency = codec_context->sample_rate;
837         }
838
839         // Check for audio buffer and create if necessary
840         if ( audio_buffer == NULL )
841         {
842                 // Allocate the audio buffer
843                 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
844
845                 // And store it on properties for reuse
846                 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
847         }
848
849         // Seek if necessary
850         if ( position != expected )
851         {
852                 if ( position + 1 == expected )
853                 {
854                         // We're paused - silence required
855                         paused = 1;
856                 }
857                 else if ( !seekable && position > expected && ( position - expected ) < 250 )
858                 {
859                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
860                         ignore = position - expected;
861                 }
862                 else if ( position < expected || position - expected >= 12 )
863                 {
864                         // Set to the real timecode
865                         if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
866                                 paused = 1;
867
868                         // Clear the usage in the audio buffer
869                         audio_used = 0;
870                 }
871         }
872
873         // Get the audio if required
874         if ( !paused )
875         {
876                 int ret = 0;
877                 int got_audio = 0;
878                 int16_t *temp = av_malloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
879
880                 av_init_packet( &pkt );
881
882                 while( ret >= 0 && !got_audio )
883                 {
884                         // Check if the buffer already contains the samples required
885                         if ( audio_used >= *samples && ignore == 0 )
886                         {
887                                 got_audio = 1;
888                                 break;
889                         }
890
891                         // Read a packet
892                         ret = av_read_frame( context, &pkt );
893
894                         int len = pkt.size;
895                         uint8_t *ptr = pkt.data;
896
897                         // We only deal with audio from the selected audio_index
898                         while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
899                         {
900                                 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
901
902                                 // Decode the audio
903 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
904                                 ret = avcodec_decode_audio2( codec_context, temp, &data_size, ptr, len );
905 #else
906                                 ret = avcodec_decode_audio( codec_context, temp, &data_size, ptr, len );
907 #endif
908                                 if ( ret < 0 )
909                                 {
910                                         ret = 0;
911                                         break;
912                                 }
913
914                                 len -= ret;
915                                 ptr += ret;
916
917                                 if ( data_size > 0 )
918                                 {
919                                         if ( resample != NULL )
920                                         {
921                                                 audio_used += audio_resample( resample, &audio_buffer[ audio_used * *channels ], temp, data_size / ( codec_context->channels * sizeof( int16_t ) ) );
922                                         }
923                                         else
924                                         {
925                                                 memcpy( &audio_buffer[ audio_used * *channels ], temp, data_size );
926                                                 audio_used += data_size / ( codec_context->channels * sizeof( int16_t ) );
927                                         }
928
929                                         // Handle ignore
930                                         while ( ignore && audio_used > *samples )
931                                         {
932                                                 ignore --;
933                                                 audio_used -= *samples;
934                                                 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
935                                         }
936                                 }
937
938                                 // If we're behind, ignore this packet
939                                 float current_pts = av_q2d( stream->time_base ) * pkt.pts;
940                                 if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
941                                         ignore = 1;
942                         }
943
944                         // We're finished with this packet regardless
945                         av_free_packet( &pkt );
946                 }
947
948                 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
949                 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
950
951                 // Now handle the audio if we have enough
952                 if ( audio_used >= *samples )
953                 {
954                         memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
955                         audio_used -= *samples;
956                         memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
957                 }
958                 else
959                 {
960                         memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
961                 }
962                 
963                 // Store the number of audio samples still available
964                 mlt_properties_set_int( properties, "_audio_used", audio_used );
965
966                 // Release the temporary audio
967                 av_free( temp );
968         }
969         else
970         {
971                 // Get silence and don't touch the context
972                 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
973         }
974
975         // Regardless of speed (other than paused), we expect to get the next frame
976         if ( !paused )
977                 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
978
979         return 0;
980 }
981
982 /** Set up audio handling.
983 */
984
985 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
986 {
987         // Get the properties
988         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
989
990         // Fetch the audio_context
991         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
992
993         // Get the audio_index
994         int index = mlt_properties_get_int( properties, "audio_index" );
995
996         // Deal with audio context
997         if ( context != NULL && index != -1 )
998         {
999                 // Get the frame properties
1000                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1001
1002                 // Get the audio stream
1003                 AVStream *stream = context->streams[ index ];
1004
1005                 // Get codec context
1006                 AVCodecContext *codec_context = stream->codec;
1007
1008                 // Get the codec
1009                 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
1010
1011                 // Initialise the codec if necessary
1012                 if ( codec == NULL )
1013                 {
1014                         // Find the codec
1015                         codec = avcodec_find_decoder( codec_context->codec_id );
1016
1017                         // If we don't have a codec and we can't initialise it, we can't do much more...
1018                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
1019                         {
1020                                 // Now store the codec with its destructor
1021                                 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
1022
1023                         }
1024                         else
1025                         {
1026                                 // Remember that we can't use this later
1027                                 mlt_properties_set_int( properties, "audio_index", -1 );
1028                         }
1029                 }
1030
1031                 // No codec, no show...
1032                 if ( codec != NULL )
1033                 {
1034                         mlt_frame_push_audio( frame, producer_get_audio );
1035                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1036                         mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
1037                         mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
1038                 }
1039         }
1040 }
1041
1042 /** Our get frame implementation.
1043 */
1044
1045 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1046 {
1047         // Create an empty frame
1048         *frame = mlt_frame_init( );
1049
1050         // Update timecode on the frame we're creating
1051         mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1052
1053         // Set the position of this producer
1054         mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1055
1056         // Set up the video
1057         producer_set_up_video( this, *frame );
1058
1059         // Set up the audio
1060         producer_set_up_audio( this, *frame );
1061
1062         // Set the aspect_ratio
1063         mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1064
1065         // Calculate the next timecode
1066         mlt_producer_prepare_next( this );
1067
1068         return 0;
1069 }