]> git.sesse.net Git - mlt/blob - src/modules/avformat/producer_avformat.c
Cleanup license declarations and remove dv1394d references.
[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_properties_get_double( properties, "fps" );
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                 // Parse out params
212                 mrl = strchr( file, '?' );
213                 while ( mrl )
214                 {
215                         mrl[0] = 0;
216                         char *name = strdup( ++mrl );
217                         char *value = strchr( name, ':' );
218                         if ( value )
219                         {
220                                 value[0] = 0;
221                                 value++;
222                                 char *t = strchr( value, '&' );
223                                 if ( t )
224                                         t[0] = 0;
225                                 if ( !strcmp( name, "frame_rate" ) )
226                                         params->time_base.den = atoi( value );
227                                 else if ( !strcmp( name, "frame_rate_base" ) )
228                                         params->time_base.num = atoi( value );
229                                 else if ( !strcmp( name, "sample_rate" ) )
230                                         params->sample_rate = atoi( value );
231                                 else if ( !strcmp( name, "channels" ) )
232                                         params->channels = atoi( value );
233                                 else if ( !strcmp( name, "width" ) )
234                                         params->width = atoi( value );
235                                 else if ( !strcmp( name, "height" ) )
236                                         params->height = atoi( value );
237                                 else if ( !strcmp( name, "standard" ) )
238                                 {
239                                         standard = strdup( value );
240                                         params->standard = standard;
241                                 }
242                                 else if ( !strcmp( name, "av" ) )
243                                         av = atoi( value );
244                         }
245                         free( name );
246                         mrl = strchr( mrl, '&' );
247                 }
248         }
249
250         // Now attempt to open the file
251         error = av_open_input_file( &context, file, format, 0, params ) < 0;
252         
253         // Cleanup AVFormatParameters
254         free( standard );
255         free( params );
256
257         // If successful, then try to get additional info
258         if ( error == 0 )
259         {
260                 // Get the stream info
261                 error = av_find_stream_info( context ) < 0;
262
263                 // Continue if no error
264                 if ( error == 0 )
265                 {
266                         // We will default to the first audio and video streams found
267                         int audio_index = -1;
268                         int video_index = -1;
269                         int av_bypass = 0;
270
271                         // Now set properties where we can (use default unknowns if required)
272                         if ( context->duration != AV_NOPTS_VALUE ) 
273                         {
274                                 // This isn't going to be accurate for all formats
275                                 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps );
276                                 mlt_properties_set_position( properties, "out", frames - 2 );
277                                 mlt_properties_set_position( properties, "length", frames - 1 );
278                         }
279
280                         // Find default audio and video streams
281                         find_default_streams( context, &audio_index, &video_index );
282
283             if ( context->start_time != AV_NOPTS_VALUE )
284                 mlt_properties_set_double( properties, "_start_time", context->start_time );
285                         
286                         // Check if we're seekable (something funny about mpeg here :-/)
287                         if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 ) )
288                                 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
289                         else
290                                 av_bypass = 1;
291
292                         // Store selected audio and video indexes on properties
293                         mlt_properties_set_int( properties, "audio_index", audio_index );
294                         mlt_properties_set_int( properties, "video_index", video_index );
295                         mlt_properties_set_int( properties, "_last_position", -1 );
296
297                         // Fetch the width, height and aspect ratio
298                         if ( video_index != -1 )
299                         {
300                                 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
301                                 mlt_properties_set_int( properties, "width", codec_context->width );
302                                 mlt_properties_set_int( properties, "height", codec_context->height );
303                                 mlt_properties_set_double( properties, "aspect_ratio", av_q2d( codec_context->sample_aspect_ratio ) );
304                         }
305
306                         // Read Metadata
307                         if (context->title != NULL) 
308                                 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
309                         if (context->author != NULL) 
310                                 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
311                         if (context->copyright != NULL) 
312                                 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
313                         if (context->comment != NULL) 
314                                 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
315                         if (context->album != NULL) 
316                                 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
317                         if (context->year != 0) 
318                                 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
319                         if (context->track != 0) 
320                                 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
321                         
322                         // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
323                         if ( av == 0 && !av_bypass && audio_index != -1 && video_index != -1 )
324                         {
325                                 // We'll use the open one as our video_context
326                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
327                                 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
328
329                                 // And open again for our audio context
330                                 av_open_input_file( &context, file, NULL, 0, NULL );
331                                 av_find_stream_info( context );
332
333                                 // Audio context
334                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
335                         }
336                         else if ( av != 2 && video_index != -1 )
337                         {
338                                 // We only have a video context
339                                 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
340                                 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
341                         }
342                         else if ( audio_index != -1 )
343                         {
344                                 // We only have an audio context
345                                 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
346                         }
347                         else
348                         {
349                                 // Something has gone wrong
350                                 error = -1;
351                         }
352
353                         mlt_properties_set_int( properties, "av_bypass", av_bypass );
354                 }
355         }
356
357         // Unlock the mutex now
358         avformat_unlock( );
359
360         return error;
361 }
362
363 /** Convert a frame position to a time code.
364 */
365
366 static double producer_time_of_frame( mlt_producer this, mlt_position position )
367 {
368         // Get the properties
369         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
370
371         // Obtain the fps
372         double fps = mlt_properties_get_double( properties, "fps" );
373
374         // Do the calc
375         return ( double )position / fps;
376 }
377
378 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
379 {
380 #ifdef SWSCALE
381         if ( format == mlt_image_yuv420p )
382         {
383                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
384                         width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
385                 AVPicture output;
386                 output.data[0] = buffer;
387                 output.data[1] = buffer + width * height;
388                 output.data[2] = buffer + ( 3 * width * height ) / 2;
389                 output.linesize[0] = width;
390                 output.linesize[1] = width >> 1;
391                 output.linesize[2] = width >> 1;
392                 sws_scale( context, frame->data, frame->linesize, 0, height,
393                         output.data, output.linesize);
394                 sws_freeContext( context );
395         }
396         else if ( format == mlt_image_rgb24 )
397         {
398                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
399                         width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
400                 AVPicture output;
401                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
402                 sws_scale( context, frame->data, frame->linesize, 0, height,
403                         output.data, output.linesize);
404                 sws_freeContext( context );
405         }
406         else
407         {
408                 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
409                         width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
410                 AVPicture output;
411                 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
412                 sws_scale( context, frame->data, frame->linesize, 0, height,
413                         output.data, output.linesize);
414                 sws_freeContext( context );
415         }
416 #else
417         if ( format == mlt_image_yuv420p )
418         {
419                 AVPicture pict;
420                 pict.data[0] = buffer;
421                 pict.data[1] = buffer + width * height;
422                 pict.data[2] = buffer + ( 3 * width * height ) / 2;
423                 pict.linesize[0] = width;
424                 pict.linesize[1] = width >> 1;
425                 pict.linesize[2] = width >> 1;
426                 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
427         }
428         else if ( format == mlt_image_rgb24 )
429         {
430                 AVPicture output;
431                 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
432                 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
433         }
434         else
435         {
436                 AVPicture output;
437                 avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
438                 img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
439         }
440 #endif
441 }
442
443 /** Get an image from a frame.
444 */
445
446 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
447 {
448         // Get the properties from the frame
449         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
450
451         // Obtain the frame number of this frame
452         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
453
454         // Get the producer 
455         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
456
457         // Get the producer properties
458         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
459
460         // Fetch the video_context
461         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
462
463         // Get the video_index
464         int index = mlt_properties_get_int( properties, "video_index" );
465
466         // Obtain the expected frame numer
467         mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
468
469         // Calculate the real time code
470         double real_timecode = producer_time_of_frame( this, position );
471
472         // Get the video stream
473         AVStream *stream = context->streams[ index ];
474
475         // Get codec context
476         AVCodecContext *codec_context = stream->codec;
477
478         // Packet
479         AVPacket pkt;
480
481         // Get the conversion frame
482         AVFrame *av_frame = mlt_properties_get_data( properties, "av_frame", NULL );
483
484         // Special case pause handling flag
485         int paused = 0;
486
487         // Special case ffwd handling
488         int ignore = 0;
489
490         // Current time calcs
491         int current_position = mlt_properties_get_double( properties, "_current_position" );
492
493         // We may want to use the source fps if available
494         double source_fps = mlt_properties_get_double( properties, "source_fps" );
495         double fps = mlt_properties_get_double( properties, "fps" );
496
497         // This is the physical frame position in the source
498         int req_position = ( int )( position / fps * source_fps );
499
500         // Get the seekable status
501         int seekable = mlt_properties_get_int( properties, "seekable" );
502
503         // Generate the size in bytes
504         int size = 0; 
505
506         // Hopefully provide better support for streams...
507         int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
508
509         // Determines if we have to decode all frames in a sequence
510         int must_decode = 1;
511
512         // Set the result arguments that we know here (only *buffer is now required)
513         *width = codec_context->width;
514         *height = codec_context->height;
515
516         switch ( *format )
517         {
518                 case mlt_image_yuv420p:
519                         size = *width * 3 * ( *height + 1 ) / 2;
520                         break;
521                 case mlt_image_rgb24:
522                         size = *width * ( *height + 1 ) * 3;
523                         break;
524                 default:
525                         *format = mlt_image_yuv422;
526                         size = *width * ( *height + 1 ) * 2;
527                         break;
528         }
529
530         // Set this on the frame properties
531         mlt_properties_set_int( frame_properties, "width", *width );
532         mlt_properties_set_int( frame_properties, "height", *height );
533
534         // Construct the output image
535         *buffer = mlt_pool_alloc( size );
536
537         // Temporary hack to improve intra frame only
538         must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
539                                   strcmp( codec_context->codec->name, "rawvideo" ) &&
540                                   strcmp( codec_context->codec->name, "dvvideo" );
541
542         // Seek if necessary
543         if ( position != expected )
544         {
545                 if ( av_frame != NULL && position + 1 == expected )
546                 {
547                         // We're paused - use last image
548                         paused = 1;
549                 }
550                 else if ( !seekable && position > expected && ( position - expected ) < 250 )
551                 {
552                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
553                         ignore = ( int )( ( position - expected ) / fps * source_fps );
554                 }
555                 else if ( seekable && ( position < expected || position - expected >= 12 ) )
556                 {
557                         // Calculate the timestamp for the requested frame
558                         int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE );
559                         if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
560                                 timestamp += context->start_time;
561                         if ( must_decode )
562                                 timestamp -= AV_TIME_BASE;
563                         if ( timestamp < 0 )
564                                 timestamp = 0;
565
566                         // Set to the timestamp
567                         av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
568         
569                         // Remove the cached info relating to the previous position
570                         mlt_properties_set_int( properties, "_current_position", -1 );
571                         mlt_properties_set_int( properties, "_last_position", -1 );
572                         mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
573                         av_frame = NULL;
574                 }
575         }
576
577         // Duplicate the last image if necessary (see comment on rawvideo below)
578         if ( av_frame != NULL && ( paused || mlt_properties_get_int( properties, "_current_position" ) >= req_position ) && av_bypass == 0 )
579         {
580                 // Duplicate it
581                 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
582
583                 // Set this on the frame properties
584                 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
585         }
586         else
587         {
588                 int ret = 0;
589                 int got_picture = 0;
590                 int int_position = 0;
591
592                 av_init_packet( &pkt );
593
594                 // Construct an AVFrame for YUV422 conversion
595                 if ( av_frame == NULL )
596                 {
597                         av_frame = avcodec_alloc_frame( );
598                         mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
599                 }
600
601                 while( ret >= 0 && !got_picture )
602                 {
603                         // Read a packet
604                         ret = av_read_frame( context, &pkt );
605
606                         // We only deal with video from the selected video_index
607                         if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
608                         {
609                                 // Determine time code of the packet
610                                 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps );
611                                 if ( context->start_time != AV_NOPTS_VALUE )
612                                         int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE );
613
614                                 int last_position = mlt_properties_get_int( properties, "_last_position" );
615                                 if ( int_position == last_position )
616                                         int_position = last_position + 1;
617                                 mlt_properties_set_int( properties, "_last_position", int_position );
618
619                                 // Decode the image
620                                 if ( must_decode || int_position >= req_position )
621                                         ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
622
623                                 if ( got_picture )
624                                 {
625                                         // Handle ignore
626                                         if ( int_position < req_position )
627                                         {
628                                                 ignore = 0;
629                                                 got_picture = 0;
630                                         }
631                                         else if ( int_position >= req_position )
632                                         {
633                                                 ignore = 0;
634                                         }
635                                         else if ( ignore -- )
636                                         {
637                                                 got_picture = 0;
638                                         }
639                                 }
640                         }
641
642                         // Now handle the picture if we have one
643                         if ( got_picture )
644                         {
645                                 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
646                                 mlt_properties_set_int( frame_properties, "top_field_first", av_frame->top_field_first );
647                                 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
648                                 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
649                                 mlt_properties_set_double( properties, "_current_position", int_position );
650                         }
651
652                         // We're finished with this packet regardless
653                         av_free_packet( &pkt );
654                 }
655         }
656
657         // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
658         // above will break the pause behaviour - so we wipe the frame now
659         if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
660                 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
661
662         // Set the field order property for this frame
663         mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
664
665         // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
666         mlt_properties_set_position( properties, "_video_expected", position + 1 );
667
668         return 0;
669 }
670
671 /** Set up video handling.
672 */
673
674 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
675 {
676         // Get the properties
677         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
678
679         // Fetch the video_context
680         AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
681
682         // Get the video_index
683         int index = mlt_properties_get_int( properties, "video_index" );
684
685         // Get the frame properties
686         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
687
688         if ( context != NULL && index != -1 )
689         {
690                 // Get the video stream
691                 AVStream *stream = context->streams[ index ];
692
693                 // Get codec context
694                 AVCodecContext *codec_context = stream->codec;
695
696                 // Get the codec
697                 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
698
699                 // Initialise the codec if necessary
700                 if ( codec == NULL )
701                 {
702                         // Find the codec
703                         codec = avcodec_find_decoder( codec_context->codec_id );
704
705                         // If we don't have a codec and we can't initialise it, we can't do much more...
706                         if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
707                         {
708                                 // Now store the codec with its destructor
709                                 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
710                         }
711                         else
712                         {
713                                 // Remember that we can't use this later
714                                 mlt_properties_set_int( properties, "video_index", -1 );
715                         }
716                 }
717
718                 // No codec, no show...
719                 if ( codec != NULL )
720                 {
721                         double source_fps = 0;
722                         int norm_aspect_ratio = mlt_properties_get_int( properties, "norm_aspect_ratio" );
723                         double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
724                         double aspect_ratio;
725
726                         // XXX: We won't know the real aspect ratio until an image is decoded
727                         // but we do need it now (to satisfy filter_resize) - take a guess based
728                         // on pal/ntsc
729                         if ( force_aspect_ratio > 0.0 )
730                         {
731                                 aspect_ratio = force_aspect_ratio;
732                         }
733                         else if ( !norm_aspect_ratio && codec_context->sample_aspect_ratio.num > 0 )
734                         {
735                                 aspect_ratio = av_q2d( codec_context->sample_aspect_ratio );
736                         }
737                         else
738                         {
739                                 int is_pal = mlt_properties_get_double( properties, "fps" ) == 25.0;
740                                 aspect_ratio = is_pal ? 59.0/54.0 : 10.0/11.0;
741                         }
742
743                         // Determine the fps
744                         source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
745
746                         // We'll use fps if it's available
747                         if ( source_fps > 0 && source_fps < 30 )
748                                 mlt_properties_set_double( properties, "source_fps", source_fps );
749                         else
750                                 mlt_properties_set_double( properties, "source_fps", mlt_properties_get_double( properties, "fps" ) );
751                         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
752                         
753                         // Set the width and height
754                         mlt_properties_set_int( frame_properties, "width", codec_context->width );
755                         mlt_properties_set_int( frame_properties, "height", codec_context->height );
756                         mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
757
758                         mlt_frame_push_get_image( frame, producer_get_image );
759                         mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
760                 }
761                 else
762                 {
763                         mlt_properties_set_int( frame_properties, "test_image", 1 );
764                 }
765         }
766         else
767         {
768                 mlt_properties_set_int( frame_properties, "test_image", 1 );
769         }
770 }
771
772 /** Get the audio from a frame.
773 */
774
775 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
776 {
777         // Get the properties from the frame
778         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
779
780         // Obtain the frame number of this frame
781         mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
782
783         // Get the producer 
784         mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
785
786         // Get the producer properties
787         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
788
789         // Fetch the audio_context
790         AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
791
792         // Get the audio_index
793         int index = mlt_properties_get_int( properties, "audio_index" );
794
795         // Get the seekable status
796         int seekable = mlt_properties_get_int( properties, "seekable" );
797
798         // Obtain the expected frame numer
799         mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
800
801         // Obtain the resample context if it exists (not always needed)
802         ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
803
804         // Obtain the audio buffer
805         int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
806
807         // Get amount of audio used
808         int audio_used =  mlt_properties_get_int( properties, "_audio_used" );
809
810         // Calculate the real time code
811         double real_timecode = producer_time_of_frame( this, position );
812
813         // Get the audio stream
814         AVStream *stream = context->streams[ index ];
815
816         // Get codec context
817         AVCodecContext *codec_context = stream->codec;
818
819         // Packet
820         AVPacket pkt;
821
822         // Number of frames to ignore (for ffwd)
823         int ignore = 0;
824
825         // Flag for paused (silence) 
826         int paused = 0;
827
828         // Check for resample and create if necessary
829         if ( resample == NULL && codec_context->channels <= 2 )
830         {
831                 // Create the resampler
832                 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
833
834                 // And store it on properties
835                 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
836         }
837         else if ( resample == NULL )
838         {
839                 *channels = codec_context->channels;
840                 *frequency = codec_context->sample_rate;
841         }
842
843         // Check for audio buffer and create if necessary
844         if ( audio_buffer == NULL )
845         {
846                 // Allocate the audio buffer
847                 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
848
849                 // And store it on properties for reuse
850                 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
851         }
852
853         // Seek if necessary
854         if ( position != expected )
855         {
856                 if ( position + 1 == expected )
857                 {
858                         // We're paused - silence required
859                         paused = 1;
860                 }
861                 else if ( !seekable && position > expected && ( position - expected ) < 250 )
862                 {
863                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
864                         ignore = position - expected;
865                 }
866                 else if ( position < expected || position - expected >= 12 )
867                 {
868                         // Set to the real timecode
869                         if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
870                                 paused = 1;
871
872                         // Clear the usage in the audio buffer
873                         audio_used = 0;
874                 }
875         }
876
877         // Get the audio if required
878         if ( !paused )
879         {
880                 int ret = 0;
881                 int got_audio = 0;
882                 int16_t *temp = mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
883
884                 av_init_packet( &pkt );
885
886                 while( ret >= 0 && !got_audio )
887                 {
888                         // Check if the buffer already contains the samples required
889                         if ( audio_used >= *samples && ignore == 0 )
890                         {
891                                 got_audio = 1;
892                                 break;
893                         }
894
895                         // Read a packet
896                         ret = av_read_frame( context, &pkt );
897
898                 int len = pkt.size;
899                 uint8_t *ptr = pkt.data;
900                         int data_size;
901
902                         // We only deal with audio from the selected audio_index
903                         while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
904                         {
905                                 // Decode the audio
906                                 ret = avcodec_decode_audio( codec_context, temp, &data_size, ptr, len );
907
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                 mlt_pool_release( 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 }