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