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 // Determine the type and obtain the first index of each type
95 switch( codec_context
->codec_type
)
97 case CODEC_TYPE_VIDEO
:
98 if ( *video_index
< 0 )
101 case CODEC_TYPE_AUDIO
:
102 if ( *audio_index
< 0 )
111 /** Producer file destructor.
114 static void producer_file_close( void *context
)
116 if ( context
!= NULL
)
118 // Lock the mutex now
122 av_close_input_file( context
);
124 // Unlock the mutex now
129 /** Producer file destructor.
132 static void producer_codec_close( void *codec
)
136 // Lock the mutex now
140 avcodec_close( codec
);
142 // Unlock the mutex now
150 static int producer_open( mlt_producer
this, char *file
)
152 // Return an error code (0 == no error)
155 // Context for avformat
156 AVFormatContext
*context
= NULL
;
158 // Get the properties
159 mlt_properties properties
= mlt_producer_properties( this );
161 // We will treat everything with the producer fps
162 double fps
= mlt_properties_get_double( properties
, "fps" );
164 // Lock the mutex now
167 // If "MRL", then create AVInputFormat
168 AVInputFormat
*format
= NULL
;
169 AVFormatParameters
*params
= NULL
;
170 char *standard
= NULL
;
171 char *mrl
= strchr( file
, ':' );
173 // AV option (0 = both, 1 = video, 2 = audio)
176 // Setting lowest log level
177 av_log_set_level( -1 );
179 // Only if there is not a protocol specification that avformat can handle
180 if ( mrl
&& !url_exist( file
) )
182 // 'file' becomes format abbreviation
186 format
= av_find_input_format( file
);
188 // Eat the format designator
194 params
= calloc( sizeof( AVFormatParameters
), 1 );
196 // These are required by video4linux (defaults)
198 params
->height
= 480;
199 params
->frame_rate
= 25;
200 params
->frame_rate_base
= 1;
201 params
->device
= file
;
202 params
->channels
= 2;
203 params
->sample_rate
= 48000;
207 mrl
= strchr( file
, '?' );
211 char *name
= strdup( ++mrl
);
212 char *value
= strchr( name
, ':' );
217 char *t
= strchr( value
, '&' );
220 if ( !strcmp( name
, "frame_rate" ) )
221 params
->frame_rate
= atoi( value
);
222 else if ( !strcmp( name
, "frame_rate_base" ) )
223 params
->frame_rate_base
= atoi( value
);
224 else if ( !strcmp( name
, "sample_rate" ) )
225 params
->sample_rate
= atoi( value
);
226 else if ( !strcmp( name
, "channels" ) )
227 params
->channels
= atoi( value
);
228 else if ( !strcmp( name
, "width" ) )
229 params
->width
= atoi( value
);
230 else if ( !strcmp( name
, "height" ) )
231 params
->height
= atoi( value
);
232 else if ( !strcmp( name
, "standard" ) )
234 standard
= strdup( value
);
235 params
->standard
= standard
;
237 else if ( !strcmp( name
, "av" ) )
241 mrl
= strchr( mrl
, '&' );
245 // Now attempt to open the file
246 error
= av_open_input_file( &context
, file
, format
, 0, params
);
249 // Cleanup AVFormatParameters
253 // If successful, then try to get additional info
256 // Get the stream info
257 error
= av_find_stream_info( context
) < 0;
259 // Continue if no error
262 // We will default to the first audio and video streams found
263 int audio_index
= -1;
264 int video_index
= -1;
267 // Now set properties where we can (use default unknowns if required)
268 if ( context
->duration
!= AV_NOPTS_VALUE
)
270 // This isn't going to be accurate for all formats
271 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
);
272 mlt_properties_set_position( properties
, "out", frames
- 2 );
273 mlt_properties_set_position( properties
, "length", frames
- 1 );
276 // Find default audio and video streams
277 find_default_streams( context
, &audio_index
, &video_index
);
279 if ( context
->start_time
!= AV_NOPTS_VALUE
)
280 mlt_properties_set_double( properties
, "start_time", context
->start_time
);
282 // Check if we're seekable (something funny about mpeg here :-/)
283 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) )
284 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "start_time" ) ) >= 0 );
288 // Store selected audio and video indexes on properties
289 mlt_properties_set_int( properties
, "audio_index", audio_index
);
290 mlt_properties_set_int( properties
, "video_index", video_index
);
292 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
293 if ( av
== 0 && !av_bypass
&& audio_index
!= -1 && video_index
!= -1 )
295 // We'll use the open one as our video_context
296 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
298 // And open again for our audio context
299 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
300 av_find_stream_info( context
);
303 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
305 else if ( av
!= 2 && video_index
!= -1 )
307 // We only have a video context
308 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
310 else if ( audio_index
!= -1 )
312 // We only have an audio context
313 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
317 // Something has gone wrong
321 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
325 // Unlock the mutex now
331 /** Convert a frame position to a time code.
334 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
336 // Get the properties
337 mlt_properties properties
= mlt_producer_properties( this );
340 double fps
= mlt_properties_get_double( properties
, "fps" );
343 return ( double )position
/ fps
;
346 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
348 // EXPERIMENTAL IMAGE NORMALISATIONS
349 if ( pix_fmt
== PIX_FMT_YUV420P
&& format
== mlt_image_yuv422
)
352 register int half
= width
>> 1;
353 register uint8_t *Y
= ( ( AVPicture
* )frame
)->data
[ 0 ];
354 register uint8_t *U
= ( ( AVPicture
* )frame
)->data
[ 1 ];
355 register uint8_t *V
= ( ( AVPicture
* )frame
)->data
[ 2 ];
356 register uint8_t *d
= buffer
;
357 register uint8_t *y
, *u
, *v
;
374 Y
+= ( ( AVPicture
* )frame
)->linesize
[ 0 ];
387 Y
+= ( ( AVPicture
* )frame
)->linesize
[ 0 ];
388 U
+= ( ( AVPicture
* )frame
)->linesize
[ 1 ];
389 V
+= ( ( AVPicture
* )frame
)->linesize
[ 2 ];
392 else if ( format
== mlt_image_yuv420p
)
395 pict
.data
[0] = buffer
;
396 pict
.data
[1] = buffer
+ width
* height
;
397 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
398 pict
.linesize
[0] = width
;
399 pict
.linesize
[1] = width
>> 1;
400 pict
.linesize
[2] = width
>> 1;
401 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
403 else if ( format
== mlt_image_rgb24
)
406 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
407 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
412 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
413 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
417 /** Get an image from a frame.
420 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
422 // Get the properties from the frame
423 mlt_properties frame_properties
= mlt_frame_properties( frame
);
425 // Obtain the frame number of this frame
426 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
429 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
431 // Get the producer properties
432 mlt_properties properties
= mlt_producer_properties( this );
434 // Fetch the video_context
435 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
437 // Get the video_index
438 int index
= mlt_properties_get_int( properties
, "video_index" );
440 // Obtain the expected frame numer
441 mlt_position expected
= mlt_properties_get_position( properties
, "video_expected" );
443 // Calculate the real time code
444 double real_timecode
= producer_time_of_frame( this, position
);
446 // Get the video stream
447 AVStream
*stream
= context
->streams
[ index
];
450 AVCodecContext
*codec_context
= &stream
->codec
;
455 // Get the conversion frame
456 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
458 // Special case pause handling flag
461 // Special case ffwd handling
464 // Current time calcs
465 double current_time
= mlt_properties_get_double( properties
, "current_time" );
467 // We may want to use the source fps if available
468 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
470 // Get the seekable status
471 int seekable
= mlt_properties_get_int( properties
, "seekable" );
473 // Generate the size in bytes
476 // Hopefully provide better support for streams...
477 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
479 // Set the result arguments that we know here (only *buffer is now required)
480 *width
= codec_context
->width
;
481 *height
= codec_context
->height
;
485 case mlt_image_yuv420p
:
486 size
= *width
* 3 * ( *height
+ 1 ) / 2;
488 case mlt_image_rgb24
:
489 size
= *width
* ( *height
+ 1 ) * 3;
492 *format
= mlt_image_yuv422
;
493 size
= *width
* ( *height
+ 1 ) * 2;
497 // Set this on the frame properties
498 mlt_properties_set_int( frame_properties
, "width", *width
);
499 mlt_properties_set_int( frame_properties
, "height", *height
);
501 // Construct the output image
502 *buffer
= mlt_pool_alloc( size
);
505 if ( position
!= expected
)
507 if ( position
+ 1 == expected
)
509 // We're paused - use last image
512 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
514 // Fast forward - seeking is inefficient for small distances - just ignore following frames
515 ignore
= position
- expected
;
517 else if ( codec_context
->has_b_frames
== 0 || ( position
< expected
|| position
- expected
>= 12 ) )
519 // Set to the real timecode
520 av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "start_time" ) + real_timecode
* 1000000.0 );
522 // Remove the cached info relating to the previous position
523 mlt_properties_set_double( properties
, "current_time", real_timecode
);
525 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
530 // Duplicate the last image if necessary
531 if ( av_frame
!= NULL
&& ( paused
|| mlt_properties_get_double( properties
, "current_time" ) >= real_timecode
) &&
535 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
537 // Set this on the frame properties
538 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
545 memset( &pkt
, 0, sizeof( pkt
) );
547 // Construct an AVFrame for YUV422 conversion
548 if ( av_frame
== NULL
)
550 av_frame
= calloc( 1, sizeof( AVFrame
) );
551 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, free
, NULL
);
554 while( ret
>= 0 && !got_picture
)
557 ret
= av_read_frame( context
, &pkt
);
559 // We only deal with video from the selected video_index
560 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
563 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
567 if ( pkt
.pts
!= AV_NOPTS_VALUE
)
568 current_time
= ( double )pkt
.pts
/ 1000000.0;
570 current_time
= real_timecode
;
573 if ( ( int )( current_time
* 100 ) < ( int )( real_timecode
* 100 ) - 7 )
578 else if ( current_time
>= real_timecode
)
580 //current_time = real_timecode;
583 else if ( ignore
-- )
587 mlt_properties_set_int( properties
, "top_field_first", av_frame
->top_field_first
);
591 // We're finished with this packet regardless
592 av_free_packet( &pkt
);
595 // Now handle the picture if we have one
598 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
600 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
602 if ( current_time
== 0 && source_fps
!= 0 )
604 double fps
= mlt_properties_get_double( properties
, "fps" );
605 current_time
= ceil( source_fps
* ( double )position
/ fps
) * ( 1 / source_fps
);
606 mlt_properties_set_double( properties
, "current_time", current_time
);
610 mlt_properties_set_double( properties
, "current_time", current_time
);
615 // Set the field order property for this frame
616 mlt_properties_set_int( frame_properties
, "top_field_first",
617 mlt_properties_get_int( properties
, "top_field_first" ) );
619 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
620 mlt_properties_set_position( properties
, "video_expected", position
+ 1 );
625 /** Set up video handling.
628 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
630 // Get the properties
631 mlt_properties properties
= mlt_producer_properties( this );
633 // Fetch the video_context
634 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
636 // Get the video_index
637 int index
= mlt_properties_get_int( properties
, "video_index" );
639 // Get the frame properties
640 mlt_properties frame_properties
= mlt_frame_properties( frame
);
642 if ( context
!= NULL
&& index
!= -1 )
644 // Get the video stream
645 AVStream
*stream
= context
->streams
[ index
];
648 AVCodecContext
*codec_context
= &stream
->codec
;
651 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
653 // Initialise the codec if necessary
657 codec
= avcodec_find_decoder( codec_context
->codec_id
);
659 // If we don't have a codec and we can't initialise it, we can't do much more...
660 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
662 // Now store the codec with its destructor
663 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
667 // Remember that we can't use this later
668 mlt_properties_set_int( properties
, "video_index", -1 );
672 // No codec, no show...
675 double source_fps
= 0;
677 // XXX: We won't know the real aspect ratio until an image is decoded
678 // but we do need it now (to satisfy filter_resize) - take a guess based
680 if ( codec_context
->sample_aspect_ratio
.num
> 0 )
682 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
686 int is_pal
= mlt_properties_get_double( properties
, "fps" ) == 25.0;
687 mlt_properties_set_double( properties
, "aspect_ratio", is_pal ?
128.0/117.0 : 72.0/79.0 );
690 //fprintf( stderr, "AVFORMAT: sample aspect %f %dx%d\n", av_q2d( codec_context->sample_aspect_ratio ), codec_context->width, codec_context->height );
693 source_fps
= ( double )codec_context
->frame_rate
/ ( codec_context
->frame_rate_base
== 0 ?
1 : codec_context
->frame_rate_base
);
695 // We'll use fps if it's available
696 if ( source_fps
> 0 && source_fps
< 30 )
697 mlt_properties_set_double( properties
, "source_fps", source_fps
);
699 // Set the width and height
700 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
701 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
703 mlt_frame_push_get_image( frame
, producer_get_image
);
704 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
708 mlt_properties_set_int( frame_properties
, "test_image", 1 );
713 mlt_properties_set_int( frame_properties
, "test_image", 1 );
717 /** Get the audio from a frame.
720 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
722 // Get the properties from the frame
723 mlt_properties frame_properties
= mlt_frame_properties( frame
);
725 // Obtain the frame number of this frame
726 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
729 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
731 // Get the producer properties
732 mlt_properties properties
= mlt_producer_properties( this );
734 // Fetch the audio_context
735 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
737 // Get the audio_index
738 int index
= mlt_properties_get_int( properties
, "audio_index" );
740 // Get the seekable status
741 int seekable
= mlt_properties_get_int( properties
, "seekable" );
743 // Obtain the expected frame numer
744 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
746 // Obtain the resample context if it exists (not always needed)
747 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
749 // Obtain the audio buffer
750 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
752 // Get amount of audio used
753 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
755 // Calculate the real time code
756 double real_timecode
= producer_time_of_frame( this, position
);
758 // Get the audio stream
759 AVStream
*stream
= context
->streams
[ index
];
762 AVCodecContext
*codec_context
= &stream
->codec
;
767 // Number of frames to ignore (for ffwd)
770 // Flag for paused (silence)
773 // Check for resample and create if necessary
774 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
776 // Create the resampler
777 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
779 // And store it on properties
780 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
782 else if ( resample
== NULL
)
784 *channels
= codec_context
->channels
;
785 *frequency
= codec_context
->sample_rate
;
788 // Check for audio buffer and create if necessary
789 if ( audio_buffer
== NULL
)
791 // Allocate the audio buffer
792 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
794 // And store it on properties for reuse
795 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
799 if ( position
!= expected
)
801 if ( position
+ 1 == expected
)
803 // We're paused - silence required
806 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
808 // Fast forward - seeking is inefficient for small distances - just ignore following frames
809 ignore
= position
- expected
;
811 else if ( position
< expected
|| position
- expected
>= 12 )
813 // Set to the real timecode
814 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "start_time" ) + real_timecode
* 1000000.0 ) != 0 )
817 // Clear the usage in the audio buffer
822 // Get the audio if required
827 int16_t *temp
= mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
829 memset( &pkt
, 0, sizeof( pkt
) );
831 while( ret
>= 0 && !got_audio
)
833 // Check if the buffer already contains the samples required
834 if ( audio_used
>= *samples
&& ignore
== 0 )
841 ret
= av_read_frame( context
, &pkt
);
844 uint8_t *ptr
= pkt
.data
;
847 // We only deal with audio from the selected audio_index
848 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
851 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
864 if ( resample
!= NULL
)
866 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
870 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
871 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
875 while ( ignore
&& audio_used
> *samples
)
878 audio_used
-= *samples
;
879 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
883 // If we're behind, ignore this packet
884 float current_pts
= (float)pkt
.pts
/ 1000000.0;
885 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
889 // We're finished with this packet regardless
890 av_free_packet( &pkt
);
893 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
894 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
896 // Now handle the audio if we have enough
897 if ( audio_used
>= *samples
)
899 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
900 audio_used
-= *samples
;
901 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
905 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
908 // Store the number of audio samples still available
909 mlt_properties_set_int( properties
, "audio_used", audio_used
);
911 // Release the temporary audio
912 mlt_pool_release( temp
);
916 // Get silence and don't touch the context
917 frame
->get_audio
= NULL
;
918 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
921 // Regardless of speed (other than paused), we expect to get the next frame
923 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
928 /** Set up audio handling.
931 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
933 // Get the properties
934 mlt_properties properties
= mlt_producer_properties( this );
936 // Fetch the audio_context
937 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
939 // Get the audio_index
940 int index
= mlt_properties_get_int( properties
, "audio_index" );
942 // Deal with audio context
943 if ( context
!= NULL
&& index
!= -1 )
945 // Get the frame properties
946 mlt_properties frame_properties
= mlt_frame_properties( frame
);
948 // Get the audio stream
949 AVStream
*stream
= context
->streams
[ index
];
952 AVCodecContext
*codec_context
= &stream
->codec
;
955 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
957 // Initialise the codec if necessary
961 codec
= avcodec_find_decoder( codec_context
->codec_id
);
963 // If we don't have a codec and we can't initialise it, we can't do much more...
964 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
966 // Now store the codec with its destructor
967 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
972 // Remember that we can't use this later
973 mlt_properties_set_int( properties
, "audio_index", -1 );
977 // No codec, no show...
980 frame
->get_audio
= producer_get_audio
;
981 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
986 /** Our get frame implementation.
989 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
991 // Create an empty frame
992 *frame
= mlt_frame_init( );
994 // Update timecode on the frame we're creating
995 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
997 // Set the position of this producer
998 mlt_properties_set_position( mlt_frame_properties( *frame
), "avformat_position", mlt_producer_position( this ) );
1001 producer_set_up_video( this, *frame
);
1004 producer_set_up_audio( this, *frame
);
1006 // Set the aspect_ratio
1007 mlt_properties_set_double( mlt_frame_properties( *frame
), "aspect_ratio", mlt_properties_get_double( mlt_producer_properties( this ), "aspect_ratio" ) );
1009 // Calculate the next timecode
1010 mlt_producer_prepare_next( this );