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