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
->time_base
= (AVRational
){1,25};
200 params
->device
= file
;
201 params
->channels
= 2;
202 params
->sample_rate
= 48000;
206 mrl
= strchr( file
, '?' );
210 char *name
= strdup( ++mrl
);
211 char *value
= strchr( name
, ':' );
216 char *t
= strchr( value
, '&' );
219 if ( !strcmp( name
, "frame_rate" ) )
220 params
->time_base
.den
= atoi( value
);
221 else if ( !strcmp( name
, "frame_rate_base" ) )
222 params
->time_base
.num
= atoi( value
);
223 else if ( !strcmp( name
, "sample_rate" ) )
224 params
->sample_rate
= atoi( value
);
225 else if ( !strcmp( name
, "channels" ) )
226 params
->channels
= atoi( value
);
227 else if ( !strcmp( name
, "width" ) )
228 params
->width
= atoi( value
);
229 else if ( !strcmp( name
, "height" ) )
230 params
->height
= atoi( value
);
231 else if ( !strcmp( name
, "standard" ) )
233 standard
= strdup( value
);
234 params
->standard
= standard
;
236 else if ( !strcmp( name
, "av" ) )
240 mrl
= strchr( mrl
, '&' );
244 // Now attempt to open the file
245 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
247 // Cleanup AVFormatParameters
251 // If successful, then try to get additional info
254 // Get the stream info
255 error
= av_find_stream_info( context
) < 0;
257 // Continue if no error
260 // We will default to the first audio and video streams found
261 int audio_index
= -1;
262 int video_index
= -1;
265 // Now set properties where we can (use default unknowns if required)
266 if ( context
->duration
!= AV_NOPTS_VALUE
)
268 // This isn't going to be accurate for all formats
269 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
);
270 mlt_properties_set_position( properties
, "out", frames
- 2 );
271 mlt_properties_set_position( properties
, "length", frames
- 1 );
274 // Find default audio and video streams
275 find_default_streams( context
, &audio_index
, &video_index
);
277 if ( context
->start_time
!= AV_NOPTS_VALUE
)
278 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
280 // Check if we're seekable (something funny about mpeg here :-/)
281 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) )
282 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
286 // Store selected audio and video indexes on properties
287 mlt_properties_set_int( properties
, "audio_index", audio_index
);
288 mlt_properties_set_int( properties
, "video_index", video_index
);
290 // Fetch the width, height and aspect ratio
291 if ( video_index
!= -1 )
293 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
294 mlt_properties_set_int( properties
, "width", codec_context
->width
);
295 mlt_properties_set_int( properties
, "height", codec_context
->height
);
296 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
299 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
300 if ( av
== 0 && !av_bypass
&& audio_index
!= -1 && video_index
!= -1 )
302 // We'll use the open one as our video_context
303 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
305 // And open again for our audio context
306 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
307 av_find_stream_info( context
);
310 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
312 else if ( av
!= 2 && video_index
!= -1 )
314 // We only have a video context
315 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
317 else if ( audio_index
!= -1 )
319 // We only have an audio context
320 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
324 // Something has gone wrong
328 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
332 // Unlock the mutex now
338 /** Convert a frame position to a time code.
341 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
343 // Get the properties
344 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
347 double fps
= mlt_properties_get_double( properties
, "fps" );
350 return ( double )position
/ fps
;
353 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
355 if ( format
== mlt_image_yuv420p
)
358 pict
.data
[0] = buffer
;
359 pict
.data
[1] = buffer
+ width
* height
;
360 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
361 pict
.linesize
[0] = width
;
362 pict
.linesize
[1] = width
>> 1;
363 pict
.linesize
[2] = width
>> 1;
364 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
366 else if ( format
== mlt_image_rgb24
)
369 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
370 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
375 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
376 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
380 /** Get an image from a frame.
383 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
385 // Get the properties from the frame
386 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
388 // Obtain the frame number of this frame
389 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
392 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
394 // Get the producer properties
395 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
397 // Fetch the video_context
398 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
400 // Get the video_index
401 int index
= mlt_properties_get_int( properties
, "video_index" );
403 // Obtain the expected frame numer
404 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
406 // Calculate the real time code
407 double real_timecode
= producer_time_of_frame( this, position
);
409 // Get the video stream
410 AVStream
*stream
= context
->streams
[ index
];
413 AVCodecContext
*codec_context
= stream
->codec
;
418 // Get the conversion frame
419 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
421 // Special case pause handling flag
424 // Special case ffwd handling
427 // Current time calcs
428 double current_time
= mlt_properties_get_double( properties
, "_current_time" );
430 // We may want to use the source fps if available
431 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
433 // Get the seekable status
434 int seekable
= mlt_properties_get_int( properties
, "seekable" );
436 // Generate the size in bytes
439 // Hopefully provide better support for streams...
440 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
442 // Set the result arguments that we know here (only *buffer is now required)
443 *width
= codec_context
->width
;
444 *height
= codec_context
->height
;
448 case mlt_image_yuv420p
:
449 size
= *width
* 3 * ( *height
+ 1 ) / 2;
451 case mlt_image_rgb24
:
452 size
= *width
* ( *height
+ 1 ) * 3;
455 *format
= mlt_image_yuv422
;
456 size
= *width
* ( *height
+ 1 ) * 2;
460 // Set this on the frame properties
461 mlt_properties_set_int( frame_properties
, "width", *width
);
462 mlt_properties_set_int( frame_properties
, "height", *height
);
464 // Construct the output image
465 *buffer
= mlt_pool_alloc( size
);
468 if ( position
!= expected
)
470 if ( position
+ 1 == expected
)
472 // We're paused - use last image
475 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
477 // Fast forward - seeking is inefficient for small distances - just ignore following frames
478 ignore
= position
- expected
;
480 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
482 // Set to the real timecode
483 av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
);
485 // Remove the cached info relating to the previous position
486 mlt_properties_set_double( properties
, "_current_time", real_timecode
);
488 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
493 // Duplicate the last image if necessary
494 if ( av_frame
!= NULL
&& ( paused
|| mlt_properties_get_double( properties
, "_current_time" ) >= real_timecode
) &&
498 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
500 // Set this on the frame properties
501 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
509 // Temporary hack to improve intra frame only
510 if ( !strcmp( codec_context
->codec
->name
, "mjpeg" ) )
513 av_init_packet( &pkt
);
515 // Construct an AVFrame for YUV422 conversion
516 if ( av_frame
== NULL
)
518 av_frame
= avcodec_alloc_frame( );
519 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, av_free
, NULL
);
522 while( ret
>= 0 && !got_picture
)
525 ret
= av_read_frame( context
, &pkt
);
527 // We only deal with video from the selected video_index
528 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
530 // Determine time code of the packet
531 if ( pkt
.dts
!= AV_NOPTS_VALUE
)
532 current_time
= av_q2d( stream
->time_base
) * pkt
.dts
;
534 current_time
= real_timecode
;
537 if ( must_decode
|| current_time
>= real_timecode
)
538 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
543 if ( ( int )( current_time
* 100 ) < ( int )( real_timecode
* 100 ) - 7 )
548 else if ( current_time
>= real_timecode
)
552 else if ( ignore
-- )
559 // Now handle the picture if we have one
562 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
563 mlt_properties_set_int( frame_properties
, "top_field_first", av_frame
->top_field_first
);
565 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
567 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
569 if ( current_time
== 0 && source_fps
!= 0 )
571 double fps
= mlt_properties_get_double( properties
, "fps" );
572 current_time
= ceil( source_fps
* ( double )position
/ fps
) * ( 1 / source_fps
);
573 mlt_properties_set_double( properties
, "_current_time", current_time
);
577 mlt_properties_set_double( properties
, "_current_time", current_time
);
581 // We're finished with this packet regardless
582 av_free_packet( &pkt
);
586 // Set the field order property for this frame
587 mlt_properties_set_int( frame_properties
, "top_field_first",
588 mlt_properties_get_int( properties
, "top_field_first" ) );
590 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
591 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
596 /** Set up video handling.
599 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
601 // Get the properties
602 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
604 // Fetch the video_context
605 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
607 // Get the video_index
608 int index
= mlt_properties_get_int( properties
, "video_index" );
610 // Get the frame properties
611 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
613 if ( context
!= NULL
&& index
!= -1 )
615 // Get the video stream
616 AVStream
*stream
= context
->streams
[ index
];
619 AVCodecContext
*codec_context
= stream
->codec
;
622 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
624 // Initialise the codec if necessary
628 codec
= avcodec_find_decoder( codec_context
->codec_id
);
630 // If we don't have a codec and we can't initialise it, we can't do much more...
631 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
633 // Now store the codec with its destructor
634 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
638 // Remember that we can't use this later
639 mlt_properties_set_int( properties
, "video_index", -1 );
643 // No codec, no show...
646 double source_fps
= 0;
647 int norm_aspect_ratio
= mlt_properties_get_int( properties
, "norm_aspect_ratio" );
648 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
651 // XXX: We won't know the real aspect ratio until an image is decoded
652 // but we do need it now (to satisfy filter_resize) - take a guess based
654 if ( force_aspect_ratio
> 0.0 )
656 aspect_ratio
= force_aspect_ratio
;
658 else if ( !norm_aspect_ratio
&& codec_context
->sample_aspect_ratio
.num
> 0 )
660 aspect_ratio
= av_q2d( codec_context
->sample_aspect_ratio
);
664 int is_pal
= mlt_properties_get_double( properties
, "fps" ) == 25.0;
665 aspect_ratio
= is_pal ?
59.0/54.0 : 10.0/11.0;
669 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
671 // We'll use fps if it's available
672 if ( source_fps
> 0 && source_fps
< 30 )
673 mlt_properties_set_double( properties
, "source_fps", source_fps
);
674 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
676 // Set the width and height
677 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
678 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
679 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
681 mlt_frame_push_get_image( frame
, producer_get_image
);
682 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
686 mlt_properties_set_int( frame_properties
, "test_image", 1 );
691 mlt_properties_set_int( frame_properties
, "test_image", 1 );
695 /** Get the audio from a frame.
698 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
700 // Get the properties from the frame
701 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
703 // Obtain the frame number of this frame
704 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
707 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
709 // Get the producer properties
710 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
712 // Fetch the audio_context
713 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
715 // Get the audio_index
716 int index
= mlt_properties_get_int( properties
, "audio_index" );
718 // Get the seekable status
719 int seekable
= mlt_properties_get_int( properties
, "seekable" );
721 // Obtain the expected frame numer
722 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
724 // Obtain the resample context if it exists (not always needed)
725 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
727 // Obtain the audio buffer
728 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
730 // Get amount of audio used
731 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
733 // Calculate the real time code
734 double real_timecode
= producer_time_of_frame( this, position
);
736 // Get the audio stream
737 AVStream
*stream
= context
->streams
[ index
];
740 AVCodecContext
*codec_context
= stream
->codec
;
745 // Number of frames to ignore (for ffwd)
748 // Flag for paused (silence)
751 // Check for resample and create if necessary
752 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
754 // Create the resampler
755 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
757 // And store it on properties
758 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
760 else if ( resample
== NULL
)
762 *channels
= codec_context
->channels
;
763 *frequency
= codec_context
->sample_rate
;
766 // Check for audio buffer and create if necessary
767 if ( audio_buffer
== NULL
)
769 // Allocate the audio buffer
770 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
772 // And store it on properties for reuse
773 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
777 if ( position
!= expected
)
779 if ( position
+ 1 == expected
)
781 // We're paused - silence required
784 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
786 // Fast forward - seeking is inefficient for small distances - just ignore following frames
787 ignore
= position
- expected
;
789 else if ( position
< expected
|| position
- expected
>= 12 )
791 // Set to the real timecode
792 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
795 // Clear the usage in the audio buffer
800 // Get the audio if required
805 int16_t *temp
= mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
807 av_init_packet( &pkt
);
809 while( ret
>= 0 && !got_audio
)
811 // Check if the buffer already contains the samples required
812 if ( audio_used
>= *samples
&& ignore
== 0 )
819 ret
= av_read_frame( context
, &pkt
);
822 uint8_t *ptr
= pkt
.data
;
825 // We only deal with audio from the selected audio_index
826 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
829 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
842 if ( resample
!= NULL
)
844 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
848 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
849 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
853 while ( ignore
&& audio_used
> *samples
)
856 audio_used
-= *samples
;
857 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
861 // If we're behind, ignore this packet
862 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
863 if ( seekable
&& ( !ignore
&& 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 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
898 // Regardless of speed (other than paused), we expect to get the next frame
900 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
905 /** Set up audio handling.
908 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
910 // Get the properties
911 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
913 // Fetch the audio_context
914 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
916 // Get the audio_index
917 int index
= mlt_properties_get_int( properties
, "audio_index" );
919 // Deal with audio context
920 if ( context
!= NULL
&& index
!= -1 )
922 // Get the frame properties
923 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
925 // Get the audio stream
926 AVStream
*stream
= context
->streams
[ index
];
929 AVCodecContext
*codec_context
= stream
->codec
;
932 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
934 // Initialise the codec if necessary
938 codec
= avcodec_find_decoder( codec_context
->codec_id
);
940 // If we don't have a codec and we can't initialise it, we can't do much more...
941 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
943 // Now store the codec with its destructor
944 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
949 // Remember that we can't use this later
950 mlt_properties_set_int( properties
, "audio_index", -1 );
954 // No codec, no show...
957 mlt_frame_push_audio( frame
, producer_get_audio
);
958 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
959 mlt_properties_set_int( frame_properties
, "frequency", codec_context
->sample_rate
);
960 mlt_properties_set_int( frame_properties
, "channels", codec_context
->channels
);
965 /** Our get frame implementation.
968 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
970 // Create an empty frame
971 *frame
= mlt_frame_init( );
973 // Update timecode on the frame we're creating
974 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
976 // Set the position of this producer
977 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
980 producer_set_up_video( this, *frame
);
983 producer_set_up_audio( this, *frame
);
985 // Set the aspect_ratio
986 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
988 // Calculate the next timecode
989 mlt_producer_prepare_next( this );