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 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 // TEST: audio sync tweaking
66 mlt_properties_set_double( properties
, "discrepancy", 1 );
68 // Register our get_frame implementation
69 this->get_frame
= producer_get_frame
;
72 if ( producer_open( this, file
) != 0 )
75 mlt_producer_close( this );
84 /** Find the default streams.
87 static void find_default_streams( AVFormatContext
*context
, int *audio_index
, int *video_index
)
91 // Allow for multiple audio and video streams in the file and select first of each (if available)
92 for( i
= 0; i
< context
->nb_streams
; i
++ )
94 // Get the codec context
95 AVCodecContext
*codec_context
= &context
->streams
[ i
]->codec
;
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 // Only if there is not a protocol specification that avformat can handle
177 if ( mrl
&& !url_exist( file
) )
179 // 'file' becomes format abbreviation
183 format
= av_find_input_format( file
);
185 // Eat the format designator
191 params
= calloc( sizeof( AVFormatParameters
), 1 );
193 // These are required by video4linux (defaults)
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;
204 mrl
= strchr( file
, '?' );
208 char *name
= strdup( ++mrl
);
209 char *value
= strchr( name
, ':' );
214 char *t
= strchr( value
, '&' );
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" ) )
231 standard
= strdup( value
);
232 params
->standard
= standard
;
236 mrl
= strchr( mrl
, '&' );
240 // Now attempt to open the file
241 error
= av_open_input_file( &context
, file
, format
, 0, params
);
244 // Cleanup AVFormatParameters
248 // If successful, then try to get additional info
251 // Get the stream info
252 error
= av_find_stream_info( context
) < 0;
254 // Continue if no error
257 // We will default to the first audio and video streams found
258 int audio_index
= -1;
259 int video_index
= -1;
261 // Now set properties where we can (use default unknowns if required)
262 if ( context
->duration
!= AV_NOPTS_VALUE
)
264 // This isn't going to be accurate for all formats
265 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
);
266 mlt_properties_set_position( properties
, "out", frames
- 2 );
267 mlt_properties_set_position( properties
, "length", frames
- 1 );
270 // Find default audio and video streams
271 find_default_streams( context
, &audio_index
, &video_index
);
273 // Store selected audio and video indexes on properties
274 mlt_properties_set_int( properties
, "audio_index", audio_index
);
275 mlt_properties_set_int( properties
, "video_index", video_index
);
277 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
278 if ( audio_index
!= -1 && video_index
!= -1 )
280 // We'll use the open one as our video_context
281 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
283 // And open again for our audio context
284 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
285 av_find_stream_info( context
);
288 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
290 else if ( video_index
!= -1 )
292 // We only have a video context
293 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
295 else if ( audio_index
!= -1 )
297 // We only have an audio context
298 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
302 // Something has gone wrong
308 // Unlock the mutex now
314 /** Convert a frame position to a time code.
317 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
319 // Get the properties
320 mlt_properties properties
= mlt_producer_properties( this );
323 double fps
= mlt_properties_get_double( properties
, "fps" );
326 return ( double )position
/ fps
;
329 /** Get an image from a frame.
332 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
334 // Get the properties from the frame
335 mlt_properties frame_properties
= mlt_frame_properties( frame
);
337 // Obtain the frame number of this frame
338 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
341 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
343 // Get the producer properties
344 mlt_properties properties
= mlt_producer_properties( this );
346 // Fetch the video_context
347 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
349 // Get the video_index
350 int index
= mlt_properties_get_int( properties
, "video_index" );
352 // Obtain the expected frame numer
353 mlt_position expected
= mlt_properties_get_position( properties
, "video_expected" );
355 // Calculate the real time code
356 double real_timecode
= producer_time_of_frame( this, position
);
358 // Get the video stream
359 AVStream
*stream
= context
->streams
[ index
];
362 AVCodecContext
*codec_context
= &stream
->codec
;
367 // Get the conversion frame
368 AVPicture
*output
= mlt_properties_get_data( properties
, "video_output_frame", NULL
);
370 // Special case pause handling flag
373 // Special case ffwd handling
376 // Current time calcs
377 double current_time
= mlt_properties_get_double( properties
, "current_time" );
379 // We may want to use the source fps if available
380 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
382 // Set the result arguments that we know here (only *buffer is now required)
383 *format
= mlt_image_yuv422
;
384 *width
= codec_context
->width
;
385 *height
= codec_context
->height
;
387 // Set this on the frame properties
388 mlt_properties_set_int( frame_properties
, "width", *width
);
389 mlt_properties_set_int( frame_properties
, "height", *height
);
391 // Lock the mutex now
394 // Construct an AVFrame for YUV422 conversion
395 if ( output
== NULL
)
397 int size
= avpicture_get_size( PIX_FMT_YUV422
, *width
, *height
+ 1 );
398 uint8_t *buf
= mlt_pool_alloc( size
);
399 output
= mlt_pool_alloc( sizeof( AVPicture
) );
400 avpicture_fill( output
, buf
, PIX_FMT_YUV422
, *width
, *height
);
401 mlt_properties_set_data( properties
, "video_output_frame", output
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
402 mlt_properties_set_data( properties
, "video_output_buffer", buf
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
406 if ( position
!= expected
)
408 if ( position
+ 1 == expected
)
410 // We're paused - use last image
413 else if ( position
> expected
&& ( position
- expected
) < 250 )
415 // Fast forward - seeking is inefficient for small distances - just ignore following frames
416 ignore
= position
- expected
;
420 // Set to the real timecode
421 av_seek_frame( context
, -1, real_timecode
* 1000000.0 );
423 // Remove the cached info relating to the previous position
424 mlt_properties_set_double( properties
, "current_time", real_timecode
);
425 mlt_properties_set_data( properties
, "current_image", NULL
, 0, NULL
, NULL
);
429 // Duplicate the last image if necessary
430 if ( mlt_properties_get_data( properties
, "current_image", NULL
) != NULL
&&
431 ( paused
|| mlt_properties_get_double( properties
, "current_time" ) >= real_timecode
) )
433 // Get current image and size
435 uint8_t *image
= mlt_properties_get_data( properties
, "current_image", &size
);
438 *buffer
= mlt_pool_alloc( size
);
439 memcpy( *buffer
, image
, size
);
441 // Set this on the frame properties
442 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
450 memset( &pkt
, 0, sizeof( pkt
) );
451 memset( &frame
, 0, sizeof( frame
) );
453 while( ret
>= 0 && !got_picture
)
456 ret
= av_read_frame( context
, &pkt
);
458 // We only deal with video from the selected video_index
459 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
462 ret
= avcodec_decode_video( codec_context
, &frame
, &got_picture
, pkt
.data
, pkt
.size
);
466 if ( pkt
.pts
!= AV_NOPTS_VALUE
&& pkt
.pts
!= 0 )
467 current_time
= ( double )pkt
.pts
/ 1000000.0;
469 current_time
= real_timecode
;
472 if ( ( int )( current_time
* 100 ) < ( int )( real_timecode
* 100 ) - 7 )
477 else if ( current_time
>= real_timecode
)
479 //current_time = real_timecode;
482 else if ( ignore
-- )
489 // We're finished with this packet regardless
490 av_free_packet( &pkt
);
493 // Now handle the picture if we have one
496 // Get current image and size
498 uint8_t *image
= mlt_properties_get_data( properties
, "current_image", &size
);
500 if ( image
== NULL
|| size
!= *width
* *height
* 2 )
502 size
= *width
* ( *height
+ 1 ) * 2;
503 image
= mlt_pool_alloc( size
);
504 mlt_properties_set_data( properties
, "current_image", image
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
507 *buffer
= mlt_pool_alloc( size
);
509 // EXPERIMENTAL IMAGE NORMALISATIONS
510 if ( codec_context
->pix_fmt
== PIX_FMT_YUV420P
)
513 register int half
= *width
>> 1;
514 register uint8_t *Y
= ( ( AVPicture
* )&frame
)->data
[ 0 ];
515 register uint8_t *U
= ( ( AVPicture
* )&frame
)->data
[ 1 ];
516 register uint8_t *V
= ( ( AVPicture
* )&frame
)->data
[ 2 ];
517 register uint8_t *d
= *buffer
;
518 register uint8_t *y
, *u
, *v
;
535 Y
+= ( ( AVPicture
* )&frame
)->linesize
[ 0 ];
548 Y
+= ( ( AVPicture
* )&frame
)->linesize
[ 0 ];
549 U
+= ( ( AVPicture
* )&frame
)->linesize
[ 1 ];
550 V
+= ( ( AVPicture
* )&frame
)->linesize
[ 2 ];
555 img_convert( output
, PIX_FMT_YUV422
, (AVPicture
*)&frame
, codec_context
->pix_fmt
, *width
, *height
);
556 memcpy( *buffer
, output
->data
[ 0 ], size
);
559 memcpy( image
, *buffer
, size
);
560 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
562 if ( current_time
== 0 && source_fps
!= 0 )
564 double fps
= mlt_properties_get_double( properties
, "fps" );
565 current_time
= ceil( source_fps
* ( double )position
/ fps
) * ( 1 / source_fps
);
566 mlt_properties_set_double( properties
, "current_time", current_time
);
570 mlt_properties_set_double( properties
, "current_time", current_time
);
575 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
576 mlt_properties_set_position( properties
, "video_expected", position
+ 1 );
578 // Unlock the mutex now
584 /** Set up video handling.
587 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
589 // Get the properties
590 mlt_properties properties
= mlt_producer_properties( this );
592 // Fetch the video_context
593 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
595 // Get the video_index
596 int index
= mlt_properties_get_int( properties
, "video_index" );
598 // Get the frame properties
599 mlt_properties frame_properties
= mlt_frame_properties( frame
);
601 // Lock the mutex now
604 if ( context
!= NULL
&& index
!= -1 )
606 // Get the video stream
607 AVStream
*stream
= context
->streams
[ index
];
610 AVCodecContext
*codec_context
= &stream
->codec
;
613 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
615 // Initialise the codec if necessary
619 codec
= avcodec_find_decoder( codec_context
->codec_id
);
621 // If we don't have a codec and we can't initialise it, we can't do much more...
622 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
624 // Now store the codec with its destructor
625 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
629 // Remember that we can't use this later
630 mlt_properties_set_int( properties
, "video_index", -1 );
634 // No codec, no show...
637 double aspect_ratio
= 1;
638 double source_fps
= 0;
641 if ( codec_context
->sample_aspect_ratio
.num
> 0 )
642 aspect_ratio
= av_q2d( codec_context
->sample_aspect_ratio
);
644 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
645 //fprintf( stderr, "AVFORMAT: sample aspect %f %dx%d\n", av_q2d( codec_context->sample_aspect_ratio ), codec_context->width, codec_context->height );
648 source_fps
= ( double )codec_context
->frame_rate
/ ( codec_context
->frame_rate_base
== 0 ?
1 : codec_context
->frame_rate_base
);
650 // We'll use fps if it's available
651 if ( source_fps
> 0 && source_fps
< 30 )
652 mlt_properties_set_double( properties
, "source_fps", source_fps
);
654 // Set the width and height
655 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
656 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
658 mlt_frame_push_get_image( frame
, producer_get_image
);
659 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
663 mlt_properties_set_int( frame_properties
, "test_image", 1 );
668 mlt_properties_set_int( frame_properties
, "test_image", 1 );
671 // Unlock the mutex now
675 /** Get the audio from a frame.
678 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
680 // Get the properties from the frame
681 mlt_properties frame_properties
= mlt_frame_properties( frame
);
683 // Obtain the frame number of this frame
684 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
687 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
689 // Get the producer properties
690 mlt_properties properties
= mlt_producer_properties( this );
692 // Fetch the audio_context
693 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
695 // Get the audio_index
696 int index
= mlt_properties_get_int( properties
, "audio_index" );
698 // Obtain the expected frame numer
699 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
701 // Obtain the resample context if it exists (not always needed)
702 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
704 // Obtain the audio buffer
705 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
707 // Get amount of audio used
708 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
710 // Calculate the real time code
711 double real_timecode
= producer_time_of_frame( this, position
);
713 // Get the audio stream
714 AVStream
*stream
= context
->streams
[ index
];
717 AVCodecContext
*codec_context
= &stream
->codec
;
722 // Number of frames to ignore (for ffwd)
725 // Flag for paused (silence)
729 // Lock the mutex now
732 // Check for resample and create if necessary
733 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
735 // Create the resampler
736 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
738 // And store it on properties
739 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
741 else if ( resample
== NULL
)
743 *channels
= codec_context
->channels
;
744 *frequency
= codec_context
->sample_rate
;
747 // Check for audio buffer and create if necessary
748 if ( audio_buffer
== NULL
)
750 // Allocate the audio buffer
751 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
753 // And store it on properties for reuse
754 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
758 if ( position
!= expected
)
760 if ( position
+ 1 == expected
)
762 // We're paused - silence required
765 else if ( position
> expected
&& ( position
- expected
) < 250 )
767 // Fast forward - seeking is inefficient for small distances - just ignore following frames
768 ignore
= position
- expected
;
772 // Set to the real timecode
773 av_seek_frame( context
, -1, real_timecode
* 1000000.0 );
775 // Clear the usage in the audio buffer
782 // Get the audio if required
787 int16_t *temp
= mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
789 memset( &pkt
, 0, sizeof( pkt
) );
791 while( ret
>= 0 && !got_audio
)
793 // Check if the buffer already contains the samples required
794 if ( audio_used
>= *samples
&& ignore
== 0 )
801 ret
= av_read_frame( context
, &pkt
);
804 uint8_t *ptr
= pkt
.data
;
807 // We only deal with audio from the selected audio_index
808 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
811 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
824 if ( resample
!= NULL
)
826 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
830 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
831 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
835 while ( ignore
&& audio_used
> *samples
)
838 audio_used
-= *samples
;
839 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
843 // If we're behind, ignore this packet
844 float current_pts
= (float)pkt
.pts
/ 1000000.0;
845 double discrepancy
= mlt_properties_get_double( properties
, "discrepancy" );
846 if ( current_pts
!= 0 && real_timecode
!= 0 )
848 if ( discrepancy
!= 1 )
849 discrepancy
= ( discrepancy
+ ( real_timecode
/ current_pts
) ) / 2;
851 discrepancy
= real_timecode
/ current_pts
;
852 if ( discrepancy
> 0.9 && discrepancy
< 1.1 )
855 discrepancy
= floor( discrepancy
+ 0.5 );
857 if ( discrepancy
== 0 )
860 mlt_properties_set_double( properties
, "discrepancy", discrepancy
);
863 if ( !ignore
&& discrepancy
* current_pts
<= ( real_timecode
- 0.02 ) )
867 // We're finished with this packet regardless
868 av_free_packet( &pkt
);
871 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
872 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
874 // Now handle the audio if we have enough
875 if ( audio_used
>= *samples
)
877 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
878 audio_used
-= *samples
;
879 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
883 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
886 // Store the number of audio samples still available
887 mlt_properties_set_int( properties
, "audio_used", audio_used
);
889 // Release the temporary audio
890 mlt_pool_release( temp
);
894 // Get silence and don't touch the context
895 frame
->get_audio
= NULL
;
896 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
899 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
900 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
902 // Unlock the mutex now
908 /** Set up audio handling.
911 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
913 // Get the properties
914 mlt_properties properties
= mlt_producer_properties( this );
916 // Fetch the audio_context
917 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
919 // Get the audio_index
920 int index
= mlt_properties_get_int( properties
, "audio_index" );
922 // Lock the mutex now
925 // Deal with audio context
926 if ( context
!= NULL
&& index
!= -1 )
928 // Get the frame properties
929 mlt_properties frame_properties
= mlt_frame_properties( frame
);
931 // Get the audio stream
932 AVStream
*stream
= context
->streams
[ index
];
935 AVCodecContext
*codec_context
= &stream
->codec
;
938 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
940 // Initialise the codec if necessary
944 codec
= avcodec_find_decoder( codec_context
->codec_id
);
946 // If we don't have a codec and we can't initialise it, we can't do much more...
947 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
949 // Now store the codec with its destructor
950 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
955 // Remember that we can't use this later
956 mlt_properties_set_int( properties
, "audio_index", -1 );
960 // No codec, no show...
963 frame
->get_audio
= producer_get_audio
;
964 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
968 // Unlock the mutex now
972 /** Our get frame implementation.
975 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
977 // Create an empty frame
978 *frame
= mlt_frame_init( );
980 // Update timecode on the frame we're creating
981 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
983 // Set the position of this producer
984 mlt_properties_set_position( mlt_frame_properties( *frame
), "avformat_position", mlt_producer_get_in( this ) + mlt_producer_position( this ) );
987 producer_set_up_video( this, *frame
);
990 producer_set_up_audio( this, *frame
);
992 // Set the aspect_ratio
993 mlt_properties_set_double( mlt_frame_properties( *frame
), "aspect_ratio", mlt_properties_get_double( mlt_producer_properties( this ), "aspect_ratio" ) );
995 // Calculate the next timecode
996 mlt_producer_prepare_next( this );