producer_avformat.c: make better test for existence for avcodec_decode_audio2
[melted] / 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 {
289 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
290 mlt_properties_set_data( properties, "dummy_context", context, 0, producer_file_close, NULL );
291 av_open_input_file( &context, file, NULL, 0, NULL );
292 av_find_stream_info( context );
293 }
294 else
295 av_bypass = 1;
296
297 // Store selected audio and video indexes on properties
298 mlt_properties_set_int( properties, "audio_index", audio_index );
299 mlt_properties_set_int( properties, "video_index", video_index );
300 mlt_properties_set_int( properties, "_last_position", -1 );
301
302 // Fetch the width, height and aspect ratio
303 if ( video_index != -1 )
304 {
305 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
306 mlt_properties_set_int( properties, "width", codec_context->width );
307 mlt_properties_set_int( properties, "height", codec_context->height );
308 mlt_properties_set_double( properties, "aspect_ratio", av_q2d( codec_context->sample_aspect_ratio ) );
309 }
310
311 // Read Metadata
312 if (context->title != NULL)
313 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
314 if (context->author != NULL)
315 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
316 if (context->copyright != NULL)
317 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
318 if (context->comment != NULL)
319 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
320 if (context->album != NULL)
321 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
322 if (context->year != 0)
323 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
324 if (context->track != 0)
325 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
326
327 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
328 if ( av == 0 && !av_bypass && audio_index != -1 && video_index != -1 )
329 {
330 // We'll use the open one as our video_context
331 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
332
333 // And open again for our audio context
334 av_open_input_file( &context, file, NULL, 0, NULL );
335 av_find_stream_info( context );
336
337 // Audio context
338 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
339 }
340 else if ( av != 2 && video_index != -1 )
341 {
342 // We only have a video context
343 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
344 }
345 else if ( audio_index != -1 )
346 {
347 // We only have an audio context
348 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
349 }
350 else
351 {
352 // Something has gone wrong
353 error = -1;
354 }
355
356 mlt_properties_set_int( properties, "av_bypass", av_bypass );
357 }
358 }
359
360 // Unlock the mutex now
361 avformat_unlock( );
362
363 return error;
364 }
365
366 /** Convert a frame position to a time code.
367 */
368
369 static double producer_time_of_frame( mlt_producer this, mlt_position position )
370 {
371 // Get the properties
372 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
373
374 // Obtain the fps
375 double fps = mlt_properties_get_double( properties, "fps" );
376
377 // Do the calc
378 return ( double )position / fps;
379 }
380
381 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
382 {
383 #ifdef SWSCALE
384 if ( format == mlt_image_yuv420p )
385 {
386 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
387 width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
388 AVPicture output;
389 output.data[0] = buffer;
390 output.data[1] = buffer + width * height;
391 output.data[2] = buffer + ( 3 * width * height ) / 2;
392 output.linesize[0] = width;
393 output.linesize[1] = width >> 1;
394 output.linesize[2] = width >> 1;
395 sws_scale( context, frame->data, frame->linesize, 0, height,
396 output.data, output.linesize);
397 sws_freeContext( context );
398 }
399 else if ( format == mlt_image_rgb24 )
400 {
401 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
402 width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
403 AVPicture output;
404 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
405 sws_scale( context, frame->data, frame->linesize, 0, height,
406 output.data, output.linesize);
407 sws_freeContext( context );
408 }
409 else
410 {
411 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
412 width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
413 AVPicture output;
414 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
415 sws_scale( context, frame->data, frame->linesize, 0, height,
416 output.data, output.linesize);
417 sws_freeContext( context );
418 }
419 #else
420 if ( format == mlt_image_yuv420p )
421 {
422 AVPicture pict;
423 pict.data[0] = buffer;
424 pict.data[1] = buffer + width * height;
425 pict.data[2] = buffer + ( 3 * width * height ) / 2;
426 pict.linesize[0] = width;
427 pict.linesize[1] = width >> 1;
428 pict.linesize[2] = width >> 1;
429 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
430 }
431 else if ( format == mlt_image_rgb24 )
432 {
433 AVPicture output;
434 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
435 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
436 }
437 else
438 {
439 AVPicture output;
440 avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
441 img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
442 }
443 #endif
444 }
445
446 /** Get an image from a frame.
447 */
448
449 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
450 {
451 // Get the properties from the frame
452 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
453
454 // Obtain the frame number of this frame
455 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
456
457 // Get the producer
458 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
459
460 // Get the producer properties
461 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
462
463 // Fetch the video_context
464 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
465
466 // Get the video_index
467 int index = mlt_properties_get_int( properties, "video_index" );
468
469 // Obtain the expected frame numer
470 mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
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 // We may want to use the source fps if available
491 double source_fps = mlt_properties_get_double( properties, "source_fps" );
492 double fps = mlt_properties_get_double( properties, "fps" );
493
494 // This is the physical frame position in the source
495 int req_position = ( int )( position / fps * source_fps );
496
497 // Get the seekable status
498 int seekable = mlt_properties_get_int( properties, "seekable" );
499
500 // Generate the size in bytes
501 int size = 0;
502
503 // Hopefully provide better support for streams...
504 int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
505
506 // Determines if we have to decode all frames in a sequence
507 int must_decode = 1;
508
509 // Set the result arguments that we know here (only *buffer is now required)
510 *width = codec_context->width;
511 *height = codec_context->height;
512
513 switch ( *format )
514 {
515 case mlt_image_yuv420p:
516 size = *width * 3 * ( *height + 1 ) / 2;
517 break;
518 case mlt_image_rgb24:
519 size = *width * ( *height + 1 ) * 3;
520 break;
521 default:
522 *format = mlt_image_yuv422;
523 size = *width * ( *height + 1 ) * 2;
524 break;
525 }
526
527 // Set this on the frame properties
528 mlt_properties_set_int( frame_properties, "width", *width );
529 mlt_properties_set_int( frame_properties, "height", *height );
530
531 // Construct the output image
532 *buffer = mlt_pool_alloc( size );
533
534 // Temporary hack to improve intra frame only
535 must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
536 strcmp( codec_context->codec->name, "rawvideo" ) &&
537 strcmp( codec_context->codec->name, "dvvideo" );
538
539 // Seek if necessary
540 if ( position != expected )
541 {
542 if ( av_frame != NULL && position + 1 == expected )
543 {
544 // We're paused - use last image
545 paused = 1;
546 }
547 else if ( !seekable && position > expected && ( position - expected ) < 250 )
548 {
549 // Fast forward - seeking is inefficient for small distances - just ignore following frames
550 ignore = ( int )( ( position - expected ) / fps * source_fps );
551 }
552 else if ( seekable && ( position < expected || position - expected >= 12 ) )
553 {
554 // Calculate the timestamp for the requested frame
555 int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE );
556 if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
557 timestamp += context->start_time;
558 if ( must_decode )
559 timestamp -= AV_TIME_BASE;
560 if ( timestamp < 0 )
561 timestamp = 0;
562
563 // Set to the timestamp
564 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
565
566 // Remove the cached info relating to the previous position
567 mlt_properties_set_int( properties, "_current_position", -1 );
568 mlt_properties_set_int( properties, "_last_position", -1 );
569 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
570 av_frame = NULL;
571 }
572 }
573
574 // Duplicate the last image if necessary (see comment on rawvideo below)
575 if ( av_frame != NULL && ( paused || mlt_properties_get_int( properties, "_current_position" ) >= req_position ) && av_bypass == 0 )
576 {
577 // Duplicate it
578 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
579
580 // Set this on the frame properties
581 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
582 }
583 else
584 {
585 int ret = 0;
586 int got_picture = 0;
587 int int_position = 0;
588
589 av_init_packet( &pkt );
590
591 // Construct an AVFrame for YUV422 conversion
592 if ( av_frame == NULL )
593 {
594 av_frame = avcodec_alloc_frame( );
595 mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
596 }
597
598 while( ret >= 0 && !got_picture )
599 {
600 // Read a packet
601 ret = av_read_frame( context, &pkt );
602
603 // We only deal with video from the selected video_index
604 if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
605 {
606 // Determine time code of the packet
607 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps );
608 if ( context->start_time != AV_NOPTS_VALUE )
609 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE );
610
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 {
619 #ifdef FF_INPUT_BUFFER_PADDING_SIZE
620 uint8_t *inbuf = mlt_pool_alloc( pkt.size + FF_INPUT_BUFFER_PADDING_SIZE );
621 memcpy( inbuf, pkt.data, pkt.size );
622 inbuf[ pkt.size ] = 0;
623 ret = avcodec_decode_video( codec_context, av_frame, &got_picture, inbuf, pkt.size );
624 mlt_pool_release( inbuf );
625 #else
626 ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
627 #endif
628 }
629 if ( got_picture )
630 {
631 // Handle ignore
632 if ( int_position < req_position )
633 {
634 ignore = 0;
635 got_picture = 0;
636 }
637 else if ( int_position >= req_position )
638 {
639 ignore = 0;
640 }
641 else if ( ignore -- )
642 {
643 got_picture = 0;
644 }
645 }
646 }
647
648 // Now handle the picture if we have one
649 if ( got_picture )
650 {
651 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
652 mlt_properties_set_int( frame_properties, "top_field_first", av_frame->top_field_first );
653 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
654 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
655 mlt_properties_set_int( properties, "_current_position", int_position );
656 }
657
658 // We're finished with this packet regardless
659 av_free_packet( &pkt );
660 }
661 }
662
663 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
664 // above will break the pause behaviour - so we wipe the frame now
665 if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
666 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
667
668 // Set the field order property for this frame
669 mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
670
671 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
672 mlt_properties_set_position( properties, "_video_expected", position + 1 );
673
674 return 0;
675 }
676
677 /** Set up video handling.
678 */
679
680 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
681 {
682 // Get the properties
683 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
684
685 // Fetch the video_context
686 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
687
688 // Get the video_index
689 int index = mlt_properties_get_int( properties, "video_index" );
690
691 // Get the frame properties
692 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
693
694 if ( context != NULL && index != -1 )
695 {
696 // Get the video stream
697 AVStream *stream = context->streams[ index ];
698
699 // Get codec context
700 AVCodecContext *codec_context = stream->codec;
701
702 // Get the codec
703 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
704
705 // Initialise the codec if necessary
706 if ( codec == NULL )
707 {
708 // Find the codec
709 codec = avcodec_find_decoder( codec_context->codec_id );
710
711 // If we don't have a codec and we can't initialise it, we can't do much more...
712 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
713 {
714 // Now store the codec with its destructor
715 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
716 }
717 else
718 {
719 // Remember that we can't use this later
720 mlt_properties_set_int( properties, "video_index", -1 );
721 }
722 }
723
724 // No codec, no show...
725 if ( codec != NULL )
726 {
727 double source_fps = 0;
728 int norm_aspect_ratio = mlt_properties_get_int( properties, "norm_aspect_ratio" );
729 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
730 double aspect_ratio;
731
732 // XXX: We won't know the real aspect ratio until an image is decoded
733 // but we do need it now (to satisfy filter_resize) - take a guess based
734 // on pal/ntsc
735 if ( force_aspect_ratio > 0.0 )
736 {
737 aspect_ratio = force_aspect_ratio;
738 }
739 else if ( !norm_aspect_ratio && codec_context->sample_aspect_ratio.num > 0 )
740 {
741 aspect_ratio = av_q2d( codec_context->sample_aspect_ratio );
742 }
743 else
744 {
745 int is_pal = mlt_properties_get_double( properties, "fps" ) == 25.0;
746 aspect_ratio = is_pal ? 59.0/54.0 : 10.0/11.0;
747 }
748
749 // Determine the fps
750 source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
751
752 // We'll use fps if it's available
753 if ( source_fps > 0 && source_fps < 30 )
754 mlt_properties_set_double( properties, "source_fps", source_fps );
755 else
756 mlt_properties_set_double( properties, "source_fps", mlt_properties_get_double( properties, "fps" ) );
757 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
758
759 // Set the width and height
760 mlt_properties_set_int( frame_properties, "width", codec_context->width );
761 mlt_properties_set_int( frame_properties, "height", codec_context->height );
762 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
763
764 mlt_frame_push_get_image( frame, producer_get_image );
765 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
766 }
767 else
768 {
769 mlt_properties_set_int( frame_properties, "test_image", 1 );
770 }
771 }
772 else
773 {
774 mlt_properties_set_int( frame_properties, "test_image", 1 );
775 }
776 }
777
778 /** Get the audio from a frame.
779 */
780
781 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
782 {
783 // Get the properties from the frame
784 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
785
786 // Obtain the frame number of this frame
787 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
788
789 // Get the producer
790 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
791
792 // Get the producer properties
793 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
794
795 // Fetch the audio_context
796 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
797
798 // Get the audio_index
799 int index = mlt_properties_get_int( properties, "audio_index" );
800
801 // Get the seekable status
802 int seekable = mlt_properties_get_int( properties, "seekable" );
803
804 // Obtain the expected frame numer
805 mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
806
807 // Obtain the resample context if it exists (not always needed)
808 ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
809
810 // Obtain the audio buffer
811 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
812
813 // Get amount of audio used
814 int audio_used = mlt_properties_get_int( properties, "_audio_used" );
815
816 // Calculate the real time code
817 double real_timecode = producer_time_of_frame( this, position );
818
819 // Get the audio stream
820 AVStream *stream = context->streams[ index ];
821
822 // Get codec context
823 AVCodecContext *codec_context = stream->codec;
824
825 // Packet
826 AVPacket pkt;
827
828 // Number of frames to ignore (for ffwd)
829 int ignore = 0;
830
831 // Flag for paused (silence)
832 int paused = 0;
833
834 // Check for resample and create if necessary
835 if ( resample == NULL && codec_context->channels <= 2 )
836 {
837 // Create the resampler
838 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
839
840 // And store it on properties
841 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
842 }
843 else if ( resample == NULL )
844 {
845 *channels = codec_context->channels;
846 *frequency = codec_context->sample_rate;
847 }
848
849 // Check for audio buffer and create if necessary
850 if ( audio_buffer == NULL )
851 {
852 // Allocate the audio buffer
853 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
854
855 // And store it on properties for reuse
856 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
857 }
858
859 // Seek if necessary
860 if ( position != expected )
861 {
862 if ( position + 1 == expected )
863 {
864 // We're paused - silence required
865 paused = 1;
866 }
867 else if ( !seekable && position > expected && ( position - expected ) < 250 )
868 {
869 // Fast forward - seeking is inefficient for small distances - just ignore following frames
870 ignore = position - expected;
871 }
872 else if ( position < expected || position - expected >= 12 )
873 {
874 // Set to the real timecode
875 if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
876 paused = 1;
877
878 // Clear the usage in the audio buffer
879 audio_used = 0;
880 }
881 }
882
883 // Get the audio if required
884 if ( !paused )
885 {
886 int ret = 0;
887 int got_audio = 0;
888 int16_t *temp = mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
889
890 av_init_packet( &pkt );
891
892 while( ret >= 0 && !got_audio )
893 {
894 // Check if the buffer already contains the samples required
895 if ( audio_used >= *samples && ignore == 0 )
896 {
897 got_audio = 1;
898 break;
899 }
900
901 // Read a packet
902 ret = av_read_frame( context, &pkt );
903
904 int len = pkt.size;
905 uint8_t *ptr = pkt.data;
906 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
907
908 // We only deal with audio from the selected audio_index
909 while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
910 {
911 // Decode the audio
912 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
913 uint8_t *inbuf = mlt_pool_alloc( len + FF_INPUT_BUFFER_PADDING_SIZE );
914 memcpy( inbuf, ptr, len );
915 inbuf[ len ] = 0;
916 ret = avcodec_decode_audio2( codec_context, temp, &data_size, inbuf, len );
917 mlt_pool_release( inbuf );
918 #else
919 ret = avcodec_decode_audio( codec_context, temp, &data_size, ptr, len );
920 #endif
921
922 if ( ret < 0 )
923 {
924 ret = 0;
925 break;
926 }
927
928 len -= ret;
929 ptr += ret;
930
931 if ( data_size > 0 )
932 {
933 if ( resample != NULL )
934 {
935 audio_used += audio_resample( resample, &audio_buffer[ audio_used * *channels ], temp, data_size / ( codec_context->channels * sizeof( int16_t ) ) );
936 }
937 else
938 {
939 memcpy( &audio_buffer[ audio_used * *channels ], temp, data_size );
940 audio_used += data_size / ( codec_context->channels * sizeof( int16_t ) );
941 }
942
943 // Handle ignore
944 while ( ignore && audio_used > *samples )
945 {
946 ignore --;
947 audio_used -= *samples;
948 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
949 }
950 }
951
952 // If we're behind, ignore this packet
953 float current_pts = av_q2d( stream->time_base ) * pkt.pts;
954 if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
955 ignore = 1;
956 }
957
958 // We're finished with this packet regardless
959 av_free_packet( &pkt );
960 }
961
962 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
963 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
964
965 // Now handle the audio if we have enough
966 if ( audio_used >= *samples )
967 {
968 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
969 audio_used -= *samples;
970 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
971 }
972 else
973 {
974 memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
975 }
976
977 // Store the number of audio samples still available
978 mlt_properties_set_int( properties, "_audio_used", audio_used );
979
980 // Release the temporary audio
981 mlt_pool_release( temp );
982 }
983 else
984 {
985 // Get silence and don't touch the context
986 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
987 }
988
989 // Regardless of speed (other than paused), we expect to get the next frame
990 if ( !paused )
991 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
992
993 return 0;
994 }
995
996 /** Set up audio handling.
997 */
998
999 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
1000 {
1001 // Get the properties
1002 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1003
1004 // Fetch the audio_context
1005 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1006
1007 // Get the audio_index
1008 int index = mlt_properties_get_int( properties, "audio_index" );
1009
1010 // Deal with audio context
1011 if ( context != NULL && index != -1 )
1012 {
1013 // Get the frame properties
1014 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1015
1016 // Get the audio stream
1017 AVStream *stream = context->streams[ index ];
1018
1019 // Get codec context
1020 AVCodecContext *codec_context = stream->codec;
1021
1022 // Get the codec
1023 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
1024
1025 // Initialise the codec if necessary
1026 if ( codec == NULL )
1027 {
1028 // Find the codec
1029 codec = avcodec_find_decoder( codec_context->codec_id );
1030
1031 // If we don't have a codec and we can't initialise it, we can't do much more...
1032 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
1033 {
1034 // Now store the codec with its destructor
1035 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
1036
1037 }
1038 else
1039 {
1040 // Remember that we can't use this later
1041 mlt_properties_set_int( properties, "audio_index", -1 );
1042 }
1043 }
1044
1045 // No codec, no show...
1046 if ( codec != NULL )
1047 {
1048 mlt_frame_push_audio( frame, producer_get_audio );
1049 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1050 mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
1051 mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
1052 }
1053 }
1054 }
1055
1056 /** Our get frame implementation.
1057 */
1058
1059 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1060 {
1061 // Create an empty frame
1062 *frame = mlt_frame_init( );
1063
1064 // Update timecode on the frame we're creating
1065 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1066
1067 // Set the position of this producer
1068 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1069
1070 // Set up the video
1071 producer_set_up_video( this, *frame );
1072
1073 // Set up the audio
1074 producer_set_up_audio( this, *frame );
1075
1076 // Set the aspect_ratio
1077 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1078
1079 // Calculate the next timecode
1080 mlt_producer_prepare_next( this );
1081
1082 return 0;
1083 }