2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
22 #include "producer_avformat.h"
25 #include <framework/mlt_frame.h>
27 // ffmpeg Header files
30 // System header files
36 void avformat_lock( );
37 void avformat_unlock( );
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 );
43 /** Constructor for libavformat.
46 mlt_producer producer_avformat_init( char *file )
48 mlt_producer this = NULL;
50 // Check that we have a non-NULL argument
53 // Construct the producer
54 this = calloc( 1, sizeof( struct mlt_producer_s ) );
57 if ( mlt_producer_init( this, NULL ) == 0 )
60 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
62 // Set the resource property (required for all producers)
63 mlt_properties_set( properties, "resource", file );
65 // Register our get_frame implementation
66 this->get_frame = producer_get_frame;
69 if ( producer_open( this, file ) != 0 )
72 mlt_producer_close( this );
81 /** Find the default streams.
84 static void find_default_streams( AVFormatContext *context, int *audio_index, int *video_index )
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++ )
91 // Get the codec context
92 AVCodecContext *codec_context = context->streams[ i ]->codec;
94 if ( avcodec_find_decoder( codec_context->codec_id ) == NULL )
97 // Determine the type and obtain the first index of each type
98 switch( codec_context->codec_type )
100 case CODEC_TYPE_VIDEO:
101 if ( *video_index < 0 )
104 case CODEC_TYPE_AUDIO:
105 if ( *audio_index < 0 )
114 /** Producer file destructor.
117 static void producer_file_close( void *context )
119 if ( context != NULL )
121 // Lock the mutex now
125 av_close_input_file( context );
127 // Unlock the mutex now
132 /** Producer file destructor.
135 static void producer_codec_close( void *codec )
139 // Lock the mutex now
143 avcodec_close( codec );
145 // Unlock the mutex now
153 static int producer_open( mlt_producer this, char *file )
155 // Return an error code (0 == no error)
158 // Context for avformat
159 AVFormatContext *context = NULL;
161 // Get the properties
162 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
164 // We will treat everything with the producer fps
165 double fps = mlt_properties_get_double( properties, "fps" );
167 // Lock the mutex now
170 // If "MRL", then create AVInputFormat
171 AVInputFormat *format = NULL;
172 AVFormatParameters *params = NULL;
173 char *standard = NULL;
174 char *mrl = strchr( file, ':' );
176 // AV option (0 = both, 1 = video, 2 = audio)
179 // Setting lowest log level
180 av_log_set_level( -1 );
182 // Only if there is not a protocol specification that avformat can handle
183 if ( mrl && !url_exist( file ) )
185 // 'file' becomes format abbreviation
189 format = av_find_input_format( file );
191 // Eat the format designator
197 params = calloc( sizeof( AVFormatParameters ), 1 );
199 // These are required by video4linux (defaults)
201 params->height = 480;
202 params->time_base= (AVRational){1,25};
203 params->device = file;
204 params->channels = 2;
205 params->sample_rate = 48000;
209 mrl = strchr( file, '?' );
213 char *name = strdup( ++mrl );
214 char *value = strchr( name, ':' );
219 char *t = strchr( value, '&' );
222 if ( !strcmp( name, "frame_rate" ) )
223 params->time_base.den = atoi( value );
224 else if ( !strcmp( name, "frame_rate_base" ) )
225 params->time_base.num = atoi( value );
226 else if ( !strcmp( name, "sample_rate" ) )
227 params->sample_rate = atoi( value );
228 else if ( !strcmp( name, "channels" ) )
229 params->channels = atoi( value );
230 else if ( !strcmp( name, "width" ) )
231 params->width = atoi( value );
232 else if ( !strcmp( name, "height" ) )
233 params->height = atoi( value );
234 else if ( !strcmp( name, "standard" ) )
236 standard = strdup( value );
237 params->standard = standard;
239 else if ( !strcmp( name, "av" ) )
243 mrl = strchr( mrl, '&' );
247 // Now attempt to open the file
248 error = av_open_input_file( &context, file, format, 0, params ) < 0;
250 // Cleanup AVFormatParameters
254 // If successful, then try to get additional info
257 // Get the stream info
258 error = av_find_stream_info( context ) < 0;
260 // Continue if no error
263 // We will default to the first audio and video streams found
264 int audio_index = -1;
265 int video_index = -1;
268 // Now set properties where we can (use default unknowns if required)
269 if ( context->duration != AV_NOPTS_VALUE )
271 // This isn't going to be accurate for all formats
272 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps );
273 mlt_properties_set_position( properties, "out", frames - 2 );
274 mlt_properties_set_position( properties, "length", frames - 1 );
277 // Find default audio and video streams
278 find_default_streams( context, &audio_index, &video_index );
280 if ( context->start_time != AV_NOPTS_VALUE )
281 mlt_properties_set_double( properties, "_start_time", context->start_time );
283 // Check if we're seekable (something funny about mpeg here :-/)
284 if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 ) )
285 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
289 // Store selected audio and video indexes on properties
290 mlt_properties_set_int( properties, "audio_index", audio_index );
291 mlt_properties_set_int( properties, "video_index", video_index );
292 mlt_properties_set_int( properties, "_last_position", -1 );
294 // Fetch the width, height and aspect ratio
295 if ( video_index != -1 )
297 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
298 mlt_properties_set_int( properties, "width", codec_context->width );
299 mlt_properties_set_int( properties, "height", codec_context->height );
300 mlt_properties_set_double( properties, "aspect_ratio", av_q2d( codec_context->sample_aspect_ratio ) );
303 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
304 if ( av == 0 && !av_bypass && audio_index != -1 && video_index != -1 )
306 // We'll use the open one as our video_context
307 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
308 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
310 // And open again for our audio context
311 av_open_input_file( &context, file, NULL, 0, NULL );
312 av_find_stream_info( context );
315 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
317 else if ( av != 2 && video_index != -1 )
319 // We only have a video context
320 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
321 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
323 else if ( audio_index != -1 )
325 // We only have an audio context
326 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
330 // Something has gone wrong
334 mlt_properties_set_int( properties, "av_bypass", av_bypass );
338 // Unlock the mutex now
344 /** Convert a frame position to a time code.
347 static double producer_time_of_frame( mlt_producer this, mlt_position position )
349 // Get the properties
350 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
353 double fps = mlt_properties_get_double( properties, "fps" );
356 return ( double )position / fps;
359 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
361 if ( format == mlt_image_yuv420p )
364 pict.data[0] = buffer;
365 pict.data[1] = buffer + width * height;
366 pict.data[2] = buffer + ( 3 * width * height ) / 2;
367 pict.linesize[0] = width;
368 pict.linesize[1] = width >> 1;
369 pict.linesize[2] = width >> 1;
370 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
372 else if ( format == mlt_image_rgb24 )
375 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
376 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
381 avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
382 img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
386 /** Get an image from a frame.
389 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
391 // Get the properties from the frame
392 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
394 // Obtain the frame number of this frame
395 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
398 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
400 // Get the producer properties
401 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
403 // Fetch the video_context
404 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
406 // Get the video_index
407 int index = mlt_properties_get_int( properties, "video_index" );
409 // Obtain the expected frame numer
410 mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
412 // Calculate the real time code
413 double real_timecode = producer_time_of_frame( this, position );
415 // Get the video stream
416 AVStream *stream = context->streams[ index ];
419 AVCodecContext *codec_context = stream->codec;
424 // Get the conversion frame
425 AVFrame *av_frame = mlt_properties_get_data( properties, "av_frame", NULL );
427 // Special case pause handling flag
430 // Special case ffwd handling
433 // Current time calcs
434 int current_position = mlt_properties_get_double( properties, "_current_position" );
436 // We may want to use the source fps if available
437 double source_fps = mlt_properties_get_double( properties, "source_fps" );
438 double fps = mlt_properties_get_double( properties, "fps" );
440 // This is the physical frame position in the source
441 int req_position = ( int )( position / fps * source_fps );
443 // Get the seekable status
444 int seekable = mlt_properties_get_int( properties, "seekable" );
446 // Generate the size in bytes
449 // Hopefully provide better support for streams...
450 int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
452 // Determines if we have to decode all frames in a sequence
455 // Set the result arguments that we know here (only *buffer is now required)
456 *width = codec_context->width;
457 *height = codec_context->height;
461 case mlt_image_yuv420p:
462 size = *width * 3 * ( *height + 1 ) / 2;
464 case mlt_image_rgb24:
465 size = *width * ( *height + 1 ) * 3;
468 *format = mlt_image_yuv422;
469 size = *width * ( *height + 1 ) * 2;
473 // Set this on the frame properties
474 mlt_properties_set_int( frame_properties, "width", *width );
475 mlt_properties_set_int( frame_properties, "height", *height );
477 // Construct the output image
478 *buffer = mlt_pool_alloc( size );
480 // Temporary hack to improve intra frame only
481 must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
482 strcmp( codec_context->codec->name, "rawvideo" ) &&
483 strcmp( codec_context->codec->name, "dvvideo" );
486 if ( position != expected )
488 if ( av_frame != NULL && position + 1 == expected )
490 // We're paused - use last image
493 else if ( !seekable && position > expected && ( position - expected ) < 250 )
495 // Fast forward - seeking is inefficient for small distances - just ignore following frames
496 ignore = ( int )( ( position - expected ) / fps * source_fps );
498 else if ( seekable && ( position < expected || position - expected >= 12 ) )
500 // Calculate the timestamp for the requested frame
501 int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE );
502 if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
503 timestamp += context->start_time;
505 timestamp -= AV_TIME_BASE;
509 // Set to the timestamp
510 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
512 // Remove the cached info relating to the previous position
513 mlt_properties_set_int( properties, "_current_position", -1 );
514 mlt_properties_set_int( properties, "_last_position", -1 );
515 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
520 // Duplicate the last image if necessary (see comment on rawvideo below)
521 if ( av_frame != NULL && ( paused || mlt_properties_get_int( properties, "_current_position" ) >= req_position ) && av_bypass == 0 )
524 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
526 // Set this on the frame properties
527 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
533 int int_position = 0;
535 av_init_packet( &pkt );
537 // Construct an AVFrame for YUV422 conversion
538 if ( av_frame == NULL )
540 av_frame = avcodec_alloc_frame( );
541 mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
544 while( ret >= 0 && !got_picture )
547 ret = av_read_frame( context, &pkt );
549 // We only deal with video from the selected video_index
550 if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
552 // Determine time code of the packet
553 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps );
554 if ( context->start_time != AV_NOPTS_VALUE )
555 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE );
557 int last_position = mlt_properties_get_int( properties, "_last_position" );
558 if ( int_position == last_position )
559 int_position = last_position + 1;
560 mlt_properties_set_int( properties, "_last_position", int_position );
563 if ( must_decode || int_position >= req_position )
564 ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
569 if ( int_position < req_position )
574 else if ( int_position >= req_position )
578 else if ( ignore -- )
585 // Now handle the picture if we have one
588 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
589 mlt_properties_set_int( frame_properties, "top_field_first", av_frame->top_field_first );
590 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
591 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
592 mlt_properties_set_double( properties, "_current_position", int_position );
595 // We're finished with this packet regardless
596 av_free_packet( &pkt );
600 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
601 // above will break the pause behaviour - so we wipe the frame now
602 if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
603 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
605 // Set the field order property for this frame
606 mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
608 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
609 mlt_properties_set_position( properties, "_video_expected", position + 1 );
614 /** Set up video handling.
617 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
619 // Get the properties
620 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
622 // Fetch the video_context
623 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
625 // Get the video_index
626 int index = mlt_properties_get_int( properties, "video_index" );
628 // Get the frame properties
629 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
631 if ( context != NULL && index != -1 )
633 // Get the video stream
634 AVStream *stream = context->streams[ index ];
637 AVCodecContext *codec_context = stream->codec;
640 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
642 // Initialise the codec if necessary
646 codec = avcodec_find_decoder( codec_context->codec_id );
648 // If we don't have a codec and we can't initialise it, we can't do much more...
649 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
651 // Now store the codec with its destructor
652 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
656 // Remember that we can't use this later
657 mlt_properties_set_int( properties, "video_index", -1 );
661 // No codec, no show...
664 double source_fps = 0;
665 int norm_aspect_ratio = mlt_properties_get_int( properties, "norm_aspect_ratio" );
666 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
669 // XXX: We won't know the real aspect ratio until an image is decoded
670 // but we do need it now (to satisfy filter_resize) - take a guess based
672 if ( force_aspect_ratio > 0.0 )
674 aspect_ratio = force_aspect_ratio;
676 else if ( !norm_aspect_ratio && codec_context->sample_aspect_ratio.num > 0 )
678 aspect_ratio = av_q2d( codec_context->sample_aspect_ratio );
682 int is_pal = mlt_properties_get_double( properties, "fps" ) == 25.0;
683 aspect_ratio = is_pal ? 59.0/54.0 : 10.0/11.0;
687 source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
689 // We'll use fps if it's available
690 if ( source_fps > 0 && source_fps < 30 )
691 mlt_properties_set_double( properties, "source_fps", source_fps );
693 mlt_properties_set_double( properties, "source_fps", mlt_properties_get_double( properties, "fps" ) );
694 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
696 // Set the width and height
697 mlt_properties_set_int( frame_properties, "width", codec_context->width );
698 mlt_properties_set_int( frame_properties, "height", codec_context->height );
699 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
701 mlt_frame_push_get_image( frame, producer_get_image );
702 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
706 mlt_properties_set_int( frame_properties, "test_image", 1 );
711 mlt_properties_set_int( frame_properties, "test_image", 1 );
715 /** Get the audio from a frame.
718 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
720 // Get the properties from the frame
721 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
723 // Obtain the frame number of this frame
724 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
727 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
729 // Get the producer properties
730 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
732 // Fetch the audio_context
733 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
735 // Get the audio_index
736 int index = mlt_properties_get_int( properties, "audio_index" );
738 // Get the seekable status
739 int seekable = mlt_properties_get_int( properties, "seekable" );
741 // Obtain the expected frame numer
742 mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
744 // Obtain the resample context if it exists (not always needed)
745 ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
747 // Obtain the audio buffer
748 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
750 // Get amount of audio used
751 int audio_used = mlt_properties_get_int( properties, "_audio_used" );
753 // Calculate the real time code
754 double real_timecode = producer_time_of_frame( this, position );
756 // Get the audio stream
757 AVStream *stream = context->streams[ index ];
760 AVCodecContext *codec_context = stream->codec;
765 // Number of frames to ignore (for ffwd)
768 // Flag for paused (silence)
771 // Check for resample and create if necessary
772 if ( resample == NULL && codec_context->channels <= 2 )
774 // Create the resampler
775 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
777 // And store it on properties
778 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
780 else if ( resample == NULL )
782 *channels = codec_context->channels;
783 *frequency = codec_context->sample_rate;
786 // Check for audio buffer and create if necessary
787 if ( audio_buffer == NULL )
789 // Allocate the audio buffer
790 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
792 // And store it on properties for reuse
793 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
797 if ( position != expected )
799 if ( position + 1 == expected )
801 // We're paused - silence required
804 else if ( !seekable && position > expected && ( position - expected ) < 250 )
806 // Fast forward - seeking is inefficient for small distances - just ignore following frames
807 ignore = position - expected;
809 else if ( position < expected || position - expected >= 12 )
811 // Set to the real timecode
812 if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
815 // Clear the usage in the audio buffer
820 // Get the audio if required
825 int16_t *temp = mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
827 av_init_packet( &pkt );
829 while( ret >= 0 && !got_audio )
831 // Check if the buffer already contains the samples required
832 if ( audio_used >= *samples && ignore == 0 )
839 ret = av_read_frame( context, &pkt );
842 uint8_t *ptr = pkt.data;
845 // We only deal with audio from the selected audio_index
846 while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
849 ret = avcodec_decode_audio( codec_context, temp, &data_size, ptr, len );
862 if ( resample != NULL )
864 audio_used += audio_resample( resample, &audio_buffer[ audio_used * *channels ], temp, data_size / ( codec_context->channels * sizeof( int16_t ) ) );
868 memcpy( &audio_buffer[ audio_used * *channels ], temp, data_size );
869 audio_used += data_size / ( codec_context->channels * sizeof( int16_t ) );
873 while ( ignore && audio_used > *samples )
876 audio_used -= *samples;
877 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
881 // If we're behind, ignore this packet
882 float current_pts = av_q2d( stream->time_base ) * pkt.pts;
883 if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
887 // We're finished with this packet regardless
888 av_free_packet( &pkt );
891 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
892 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
894 // Now handle the audio if we have enough
895 if ( audio_used >= *samples )
897 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
898 audio_used -= *samples;
899 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
903 memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
906 // Store the number of audio samples still available
907 mlt_properties_set_int( properties, "_audio_used", audio_used );
909 // Release the temporary audio
910 mlt_pool_release( temp );
914 // Get silence and don't touch the context
915 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
918 // Regardless of speed (other than paused), we expect to get the next frame
920 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
925 /** Set up audio handling.
928 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
930 // Get the properties
931 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
933 // Fetch the audio_context
934 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
936 // Get the audio_index
937 int index = mlt_properties_get_int( properties, "audio_index" );
939 // Deal with audio context
940 if ( context != NULL && index != -1 )
942 // Get the frame properties
943 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
945 // Get the audio stream
946 AVStream *stream = context->streams[ index ];
949 AVCodecContext *codec_context = stream->codec;
952 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
954 // Initialise the codec if necessary
958 codec = avcodec_find_decoder( codec_context->codec_id );
960 // If we don't have a codec and we can't initialise it, we can't do much more...
961 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
963 // Now store the codec with its destructor
964 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
969 // Remember that we can't use this later
970 mlt_properties_set_int( properties, "audio_index", -1 );
974 // No codec, no show...
977 mlt_frame_push_audio( frame, producer_get_audio );
978 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
979 mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
980 mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
985 /** Our get frame implementation.
988 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
990 // Create an empty frame
991 *frame = mlt_frame_init( );
993 // Update timecode on the frame we're creating
994 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
996 // Set the position of this producer
997 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1000 producer_set_up_video( this, *frame );
1003 producer_set_up_audio( this, *frame );
1005 // Set the aspect_ratio
1006 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1008 // Calculate the next timecode
1009 mlt_producer_prepare_next( this );