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
28 #include <ffmpeg/avformat.h>
30 // System header files
36 // Forward references.
37 static int producer_open( mlt_producer this, char *file );
38 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
40 // A static flag used to determine if avformat has been initialised
41 static int avformat_initialised = 0;
42 static pthread_mutex_t avformat_mutex;
45 void *av_malloc( unsigned int size )
47 return mlt_pool_alloc( size );
50 void *av_realloc( void *ptr, unsigned int size )
52 return mlt_pool_realloc( ptr, size );
55 void av_free( void *ptr )
57 return mlt_pool_release( ptr );
61 /** Constructor for libavformat.
64 mlt_producer producer_avformat_init( char *file )
66 mlt_producer this = NULL;
68 // Check that we have a non-NULL argument
71 // Construct the producer
72 this = calloc( 1, sizeof( struct mlt_producer_s ) );
75 if ( mlt_producer_init( this, NULL ) == 0 )
78 mlt_properties properties = mlt_producer_properties( this );
80 // Set the resource property (required for all producers)
81 mlt_properties_set( properties, "resource", file );
83 // TEST: audio sync tweaking
84 mlt_properties_set_double( properties, "discrepancy", 1 );
86 // Register our get_frame implementation
87 this->get_frame = producer_get_frame;
89 // Initialise avformat if necessary
90 if ( avformat_initialised == 0 )
92 avformat_initialised = 1;
93 pthread_mutex_init( &avformat_mutex, NULL );
98 if ( producer_open( this, file ) != 0 )
101 mlt_producer_close( this );
110 /** Find the default streams.
113 static void find_default_streams( AVFormatContext *context, int *audio_index, int *video_index )
117 // Allow for multiple audio and video streams in the file and select first of each (if available)
118 for( i = 0; i < context->nb_streams; i++ )
120 // Get the codec context
121 AVCodecContext *codec_context = &context->streams[ i ]->codec;
123 // Determine the type and obtain the first index of each type
124 switch( codec_context->codec_type )
126 case CODEC_TYPE_VIDEO:
127 if ( *video_index < 0 )
130 case CODEC_TYPE_AUDIO:
131 if ( *audio_index < 0 )
140 /** Producer file destructor.
143 static void producer_file_close( void *context )
145 if ( context != NULL )
147 // Lock the mutex now
148 pthread_mutex_lock( &avformat_mutex );
151 av_close_input_file( context );
153 // Unlock the mutex now
154 pthread_mutex_unlock( &avformat_mutex );
158 /** Producer file destructor.
161 static void producer_codec_close( void *codec )
165 // Lock the mutex now
166 pthread_mutex_lock( &avformat_mutex );
169 avcodec_close( codec );
171 // Unlock the mutex now
172 pthread_mutex_unlock( &avformat_mutex );
179 static int producer_open( mlt_producer this, char *file )
181 // Return an error code (0 == no error)
184 // Context for avformat
185 AVFormatContext *context = NULL;
187 // Get the properties
188 mlt_properties properties = mlt_producer_properties( this );
190 // We will treat everything with the producer fps
191 double fps = mlt_properties_get_double( properties, "fps" );
193 // Lock the mutex now
194 pthread_mutex_lock( &avformat_mutex );
196 // If "MRL", then create AVInputFormat
197 AVInputFormat *format = NULL;
198 AVFormatParameters *params = NULL;
199 char *standard = NULL;
200 char *mrl = strchr( file, ':' );
202 // Only if there is not a protocol specification that avformat can handle
203 if ( mrl && !url_exist( file ) )
205 // 'file' becomes format abbreviation
209 format = av_find_input_format( file );
211 // Eat the format designator
217 params = calloc( sizeof( AVFormatParameters ), 1 );
219 // These are required by video4linux (defaults)
221 params->height = 480;
222 params->frame_rate = 25;
223 params->frame_rate_base = 1;
224 params->device = file;
225 params->channels = 2;
226 params->sample_rate = 48000;
230 mrl = strchr( file, '?' );
234 char *name = strdup( ++mrl );
235 char *value = strchr( name, '=' );
240 char *t = strchr( value, '&' );
243 if ( !strcmp( name, "frame_rate" ) )
244 params->frame_rate = atoi( value );
245 else if ( !strcmp( name, "frame_rate_base" ) )
246 params->frame_rate_base = atoi( value );
247 else if ( !strcmp( name, "sample_rate" ) )
248 params->sample_rate = atoi( value );
249 else if ( !strcmp( name, "channels" ) )
250 params->channels = atoi( value );
251 else if ( !strcmp( name, "width" ) )
252 params->width = atoi( value );
253 else if ( !strcmp( name, "height" ) )
254 params->height = atoi( value );
255 else if ( !strcmp( name, "standard" ) )
257 standard = strdup( value );
258 params->standard = standard;
262 mrl = strchr( mrl, '&' );
266 // Now attempt to open the file
267 error = av_open_input_file( &context, file, format, 0, params );
270 // Cleanup AVFormatParameters
274 // If successful, then try to get additional info
277 // Get the stream info
278 error = av_find_stream_info( context ) < 0;
280 // Continue if no error
283 // We will default to the first audio and video streams found
284 int audio_index = -1;
285 int video_index = -1;
287 // Now set properties where we can (use default unknowns if required)
288 if ( context->duration != AV_NOPTS_VALUE )
290 // This isn't going to be accurate for all formats
291 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps );
292 mlt_properties_set_position( properties, "out", frames - 2 );
293 mlt_properties_set_position( properties, "length", frames - 1 );
296 // Find default audio and video streams
297 find_default_streams( context, &audio_index, &video_index );
299 // Store selected audio and video indexes on properties
300 mlt_properties_set_int( properties, "audio_index", audio_index );
301 mlt_properties_set_int( properties, "video_index", video_index );
303 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
304 if ( 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 );
309 // And open again for our audio context
310 av_open_input_file( &context, file, NULL, 0, NULL );
311 av_find_stream_info( context );
314 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
316 else if ( video_index != -1 )
318 // We only have a video context
319 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
321 else if ( audio_index != -1 )
323 // We only have an audio context
324 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
328 // Something has gone wrong
334 // Unlock the mutex now
335 pthread_mutex_unlock( &avformat_mutex );
340 /** Convert a frame position to a time code.
343 static double producer_time_of_frame( mlt_producer this, mlt_position position )
345 // Get the properties
346 mlt_properties properties = mlt_producer_properties( this );
349 double fps = mlt_properties_get_double( properties, "fps" );
352 return ( double )position / fps;
355 /** Get an image from a frame.
358 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
360 // Get the properties from the frame
361 mlt_properties frame_properties = mlt_frame_properties( frame );
363 // Obtain the frame number of this frame
364 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
367 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
369 // Get the producer properties
370 mlt_properties properties = mlt_producer_properties( this );
372 // Fetch the video_context
373 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
375 // Get the video_index
376 int index = mlt_properties_get_int( properties, "video_index" );
378 // Obtain the expected frame numer
379 mlt_position expected = mlt_properties_get_position( properties, "video_expected" );
381 // Calculate the real time code
382 double real_timecode = producer_time_of_frame( this, position ) + mlt_properties_get_double( properties, "_v_pts_offset" );
384 // Get the video stream
385 AVStream *stream = context->streams[ index ];
388 AVCodecContext *codec_context = &stream->codec;
393 // Get the conversion frame
394 AVPicture *output = mlt_properties_get_data( properties, "video_output_frame", NULL );
396 // Special case pause handling flag
399 // Special case ffwd handling
402 // Current time calcs
403 double current_time = mlt_properties_get_double( properties, "current_time" );
405 // We may want to use the source fps if available
406 double source_fps = mlt_properties_get_double( properties, "source_fps" );
408 // Set the result arguments that we know here (only *buffer is now required)
409 *format = mlt_image_yuv422;
410 *width = codec_context->width;
411 *height = codec_context->height;
413 // Set this on the frame properties
414 mlt_properties_set_int( frame_properties, "width", *width );
415 mlt_properties_set_int( frame_properties, "height", *height );
417 // Lock the mutex now
418 pthread_mutex_lock( &avformat_mutex );
420 // Construct an AVFrame for YUV422 conversion
421 if ( output == NULL )
423 int size = avpicture_get_size( PIX_FMT_YUV422, *width, *height );
425 uint8_t *buf = mlt_pool_alloc( size );
426 output = mlt_pool_alloc( sizeof( AVPicture ) );
427 //memset( output, 0, sizeof( AVPicture ) );
428 avpicture_fill( output, buf, PIX_FMT_YUV422, *width, *height );
429 mlt_properties_set_data( properties, "video_output_frame", output, 0, ( mlt_destructor )mlt_pool_release, NULL );
430 mlt_properties_set_data( properties, "video_output_buffer", buf, 0, ( mlt_destructor )mlt_pool_release, NULL );
434 if ( position != expected )
436 if ( position + 1 == expected )
438 // We're paused - use last image
441 else if ( position > expected && ( position - expected ) < 250 )
443 // Fast forward - seeking is inefficient for small distances - just ignore following frames
444 ignore = position - expected;
448 // Set to the real timecode
449 av_seek_frame( context, -1, real_timecode * 1000000.0 );
451 // Remove the cached info relating to the previous position
452 mlt_properties_set_double( properties, "current_time", real_timecode );
453 mlt_properties_set_data( properties, "current_image", NULL, 0, NULL, NULL );
457 // Duplicate the last image if necessary
458 if ( mlt_properties_get_data( properties, "current_image", NULL ) != NULL &&
459 ( paused || mlt_properties_get_double( properties, "current_time" ) >= real_timecode ) )
461 // Get current image and size
463 uint8_t *image = mlt_properties_get_data( properties, "current_image", &size );
466 *buffer = mlt_pool_alloc( size );
467 memcpy( *buffer, image, size );
469 // Set this on the frame properties
470 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
478 memset( &pkt, 0, sizeof( pkt ) );
479 memset( &frame, 0, sizeof( frame ) );
481 while( ret >= 0 && !got_picture )
484 ret = av_read_frame( context, &pkt );
486 // We only deal with video from the selected video_index
487 if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
490 ret = avcodec_decode_video( codec_context, &frame, &got_picture, pkt.data, pkt.size );
494 if ( pkt.pts != AV_NOPTS_VALUE && pkt.pts != 0 )
496 if ( current_time == 0 )
498 mlt_properties_set_double( properties, "_v_pts_offset", ( double )( pkt.pts / 1000000 ) );
499 real_timecode += pkt.pts / 1000000;
501 current_time = ( double )pkt.pts / 1000000.0;
504 current_time = real_timecode;
507 if ( current_time < real_timecode )
512 else if ( current_time >= real_timecode )
514 //current_time = real_timecode;
517 else if ( ignore -- )
524 // We're finished with this packet regardless
525 av_free_packet( &pkt );
528 // Now handle the picture if we have one
531 // Get current image and size
533 uint8_t *image = mlt_properties_get_data( properties, "current_image", &size );
535 if ( image == NULL || size != *width * *height * 2 )
537 size = *width * ( *height + 1 ) * 2;
538 image = mlt_pool_alloc( size );
539 mlt_properties_set_data( properties, "current_image", image, size, ( mlt_destructor )mlt_pool_release, NULL );
542 *buffer = mlt_pool_alloc( size );
544 // EXPERIMENTAL IMAGE NORMALISATIONS
545 if ( codec_context->pix_fmt == PIX_FMT_YUV420P )
548 register int half = *width >> 1;
549 register uint8_t *Y = ( ( AVPicture * )&frame )->data[ 0 ];
550 register uint8_t *U = ( ( AVPicture * )&frame )->data[ 1 ];
551 register uint8_t *V = ( ( AVPicture * )&frame )->data[ 2 ];
552 register uint8_t *d = *buffer;
553 register uint8_t *y, *u, *v;
570 Y += ( ( AVPicture * )&frame )->linesize[ 0 ];
583 Y += ( ( AVPicture * )&frame )->linesize[ 0 ];
584 U += ( ( AVPicture * )&frame )->linesize[ 1 ];
585 V += ( ( AVPicture * )&frame )->linesize[ 2 ];
590 img_convert( output, PIX_FMT_YUV422, (AVPicture *)&frame, codec_context->pix_fmt, *width, *height );
591 memcpy( *buffer, output->data[ 0 ], size );
594 memcpy( image, *buffer, size );
595 mlt_properties_set_data( frame_properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
597 if ( current_time == 0 && source_fps != 0 )
599 double fps = mlt_properties_get_double( properties, "fps" );
600 current_time = ceil( source_fps * ( double )position / fps ) * ( 1 / source_fps );
601 mlt_properties_set_double( properties, "current_time", current_time );
605 mlt_properties_set_double( properties, "current_time", current_time );
610 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
611 mlt_properties_set_position( properties, "video_expected", position + 1 );
613 // Unlock the mutex now
614 pthread_mutex_unlock( &avformat_mutex );
619 /** Set up video handling.
622 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
624 // Get the properties
625 mlt_properties properties = mlt_producer_properties( this );
627 // Fetch the video_context
628 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
630 // Get the video_index
631 int index = mlt_properties_get_int( properties, "video_index" );
633 // Get the frame properties
634 mlt_properties frame_properties = mlt_frame_properties( frame );
636 // Lock the mutex now
637 pthread_mutex_lock( &avformat_mutex );
639 if ( context != NULL && index != -1 )
641 // Get the video stream
642 AVStream *stream = context->streams[ index ];
645 AVCodecContext *codec_context = &stream->codec;
648 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
650 // Initialise the codec if necessary
654 codec = avcodec_find_decoder( codec_context->codec_id );
656 // If we don't have a codec and we can't initialise it, we can't do much more...
657 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
659 // Now store the codec with its destructor
660 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
664 // Remember that we can't use this later
665 mlt_properties_set_int( properties, "video_index", -1 );
669 // No codec, no show...
672 double aspect_ratio = 1;
673 double source_fps = 0;
676 if ( codec_context->sample_aspect_ratio.num > 0 )
677 aspect_ratio = av_q2d( codec_context->sample_aspect_ratio );
679 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
680 //fprintf( stderr, "AVFORMAT: sample aspect %f %dx%d\n", av_q2d( codec_context->sample_aspect_ratio ), codec_context->width, codec_context->height );
683 source_fps = ( double )codec_context->frame_rate / ( codec_context->frame_rate_base == 0 ? 1 : codec_context->frame_rate_base );
685 // We'll use fps if it's available
686 if ( source_fps > 0 && source_fps < 30 )
687 mlt_properties_set_double( properties, "source_fps", source_fps );
689 // Set the width and height
690 mlt_properties_set_int( frame_properties, "width", codec_context->width );
691 mlt_properties_set_int( frame_properties, "height", codec_context->height );
693 mlt_frame_push_get_image( frame, producer_get_image );
694 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
698 mlt_properties_set_int( frame_properties, "test_image", 1 );
703 mlt_properties_set_int( frame_properties, "test_image", 1 );
706 // Unlock the mutex now
707 pthread_mutex_unlock( &avformat_mutex );
710 /** Get the audio from a frame.
713 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
715 // Get the properties from the frame
716 mlt_properties frame_properties = mlt_frame_properties( frame );
718 // Obtain the frame number of this frame
719 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
722 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
724 // Get the producer properties
725 mlt_properties properties = mlt_producer_properties( this );
727 // Fetch the audio_context
728 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
730 // Get the audio_index
731 int index = mlt_properties_get_int( properties, "audio_index" );
733 // Obtain the expected frame numer
734 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
736 // Obtain the resample context if it exists (not always needed)
737 ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
739 // Obtain the audio buffer
740 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
742 // Get amount of audio used
743 int audio_used = mlt_properties_get_int( properties, "audio_used" );
745 // Calculate the real time code
746 double real_timecode = producer_time_of_frame( this, position );
748 // Get the audio stream
749 AVStream *stream = context->streams[ index ];
752 AVCodecContext *codec_context = &stream->codec;
757 // Number of frames to ignore (for ffwd)
760 // Flag for paused (silence)
764 // Lock the mutex now
765 pthread_mutex_lock( &avformat_mutex );
767 // Check for resample and create if necessary
768 if ( resample == NULL )
770 // Create the resampler
771 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
773 // And store it on properties
774 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
777 // Check for audio buffer and create if necessary
778 if ( audio_buffer == NULL )
780 // Allocate the audio buffer
781 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
783 // And store it on properties for reuse
784 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
788 if ( position != expected )
790 if ( position + 1 == expected )
792 // We're paused - silence required
795 else if ( position > expected && ( position - expected ) < 250 )
797 // Fast forward - seeking is inefficient for small distances - just ignore following frames
798 ignore = position - expected;
802 // Set to the real timecode
803 av_seek_frame( context, -1, real_timecode * 1000000.0 );
805 // Clear the usage in the audio buffer
812 // Get the audio if required
817 int16_t *temp = mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
819 memset( &pkt, 0, sizeof( pkt ) );
821 while( ret >= 0 && !got_audio )
823 // Check if the buffer already contains the samples required
824 if ( audio_used >= *samples && ignore == 0 )
831 ret = av_read_frame( context, &pkt );
834 uint8_t *ptr = pkt.data;
837 // We only deal with video from the selected video_index
838 while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
841 ret = avcodec_decode_audio( codec_context, temp, &data_size, ptr, len );
854 int size_out = audio_resample( resample, &audio_buffer[ audio_used * *channels ], temp, data_size / ( codec_context->channels * sizeof( int16_t ) ) );
856 audio_used += size_out;
859 while ( ignore && audio_used > *samples )
862 audio_used -= *samples;
863 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
867 // If we're behind, ignore this packet
868 float current_pts = (float)pkt.pts / 1000000.0;
869 double discrepancy = mlt_properties_get_double( properties, "discrepancy" );
870 if ( current_pts != 0 && real_timecode != 0 )
872 if ( discrepancy != 1 )
873 discrepancy = ( discrepancy + ( real_timecode / current_pts ) ) / 2;
875 discrepancy = real_timecode / current_pts;
876 if ( discrepancy > 0.9 && discrepancy < 1.1 )
879 discrepancy = floor( discrepancy + 0.5 );
881 if ( discrepancy == 0 )
884 mlt_properties_set_double( properties, "discrepancy", discrepancy );
887 if ( !ignore && discrepancy * current_pts <= ( real_timecode - 0.02 ) )
891 // We're finished with this packet regardless
892 av_free_packet( &pkt );
895 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
896 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
898 // Now handle the audio if we have enough
899 if ( audio_used >= *samples )
901 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
902 audio_used -= *samples;
903 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
907 memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
910 // Store the number of audio samples still available
911 mlt_properties_set_int( properties, "audio_used", audio_used );
913 // Release the temporary audio
914 mlt_pool_release( temp );
918 // Get silence and don't touch the context
919 frame->get_audio = NULL;
920 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
923 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
924 mlt_properties_set_position( properties, "audio_expected", position + 1 );
926 // Unlock the mutex now
927 pthread_mutex_unlock( &avformat_mutex );
932 /** Set up audio handling.
935 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
937 // Get the properties
938 mlt_properties properties = mlt_producer_properties( this );
940 // Fetch the audio_context
941 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
943 // Get the audio_index
944 int index = mlt_properties_get_int( properties, "audio_index" );
946 // Lock the mutex now
947 pthread_mutex_lock( &avformat_mutex );
949 // Deal with audio context
950 if ( context != NULL && index != -1 )
952 // Get the frame properties
953 mlt_properties frame_properties = mlt_frame_properties( frame );
955 // Get the audio stream
956 AVStream *stream = context->streams[ index ];
959 AVCodecContext *codec_context = &stream->codec;
962 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
964 // Initialise the codec if necessary
968 codec = avcodec_find_decoder( codec_context->codec_id );
970 // If we don't have a codec and we can't initialise it, we can't do much more...
971 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
973 // Now store the codec with its destructor
974 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
979 // Remember that we can't use this later
980 mlt_properties_set_int( properties, "audio_index", -1 );
984 // No codec, no show...
987 frame->get_audio = producer_get_audio;
988 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
992 // Unlock the mutex now
993 pthread_mutex_unlock( &avformat_mutex );
996 /** Our get frame implementation.
999 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1001 // Create an empty frame
1002 *frame = mlt_frame_init( );
1004 // Update timecode on the frame we're creating
1005 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1007 // Set the position of this producer
1008 mlt_properties_set_position( mlt_frame_properties( *frame ), "avformat_position", mlt_producer_get_in( this ) + mlt_producer_position( this ) );
1011 producer_set_up_video( this, *frame );
1014 producer_set_up_audio( this, *frame );
1016 // Set the aspect_ratio
1017 mlt_properties_set_double( mlt_frame_properties( *frame ), "aspect_ratio", mlt_properties_get_double( mlt_producer_properties( this ), "aspect_ratio" ) );
1019 // Calculate the next timecode
1020 mlt_producer_prepare_next( this );