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