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
);
248 // Cleanup AVFormatParameters
252 // If successful, then try to get additional info
255 // Get the stream info
256 error
= av_find_stream_info( context
) < 0;
258 // Continue if no error
261 // We will default to the first audio and video streams found
262 int audio_index
= -1;
263 int video_index
= -1;
266 // Now set properties where we can (use default unknowns if required)
267 if ( context
->duration
!= AV_NOPTS_VALUE
)
269 // This isn't going to be accurate for all formats
270 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
);
271 mlt_properties_set_position( properties
, "out", frames
- 2 );
272 mlt_properties_set_position( properties
, "length", frames
- 1 );
275 // Find default audio and video streams
276 find_default_streams( context
, &audio_index
, &video_index
);
278 if ( context
->start_time
!= AV_NOPTS_VALUE
)
279 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
281 // Check if we're seekable (something funny about mpeg here :-/)
282 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) )
283 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
287 // Store selected audio and video indexes on properties
288 mlt_properties_set_int( properties
, "audio_index", audio_index
);
289 mlt_properties_set_int( properties
, "video_index", video_index
);
291 // Fetch the width, height and aspect ratio
292 if ( video_index
!= -1 )
294 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
295 mlt_properties_set_int( properties
, "width", codec_context
->width
);
296 mlt_properties_set_int( properties
, "height", codec_context
->height
);
297 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
300 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
301 if ( av
== 0 && !av_bypass
&& audio_index
!= -1 && video_index
!= -1 )
303 // We'll use the open one as our video_context
304 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
306 // And open again for our audio context
307 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
308 av_find_stream_info( context
);
311 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
313 else if ( av
!= 2 && video_index
!= -1 )
315 // We only have a video context
316 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
318 else if ( audio_index
!= -1 )
320 // We only have an audio context
321 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
325 // Something has gone wrong
329 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
333 // Unlock the mutex now
339 /** Convert a frame position to a time code.
342 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
344 // Get the properties
345 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
348 double fps
= mlt_properties_get_double( properties
, "fps" );
351 return ( double )position
/ fps
;
354 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
356 if ( format
== mlt_image_yuv420p
)
359 pict
.data
[0] = buffer
;
360 pict
.data
[1] = buffer
+ width
* height
;
361 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
362 pict
.linesize
[0] = width
;
363 pict
.linesize
[1] = width
>> 1;
364 pict
.linesize
[2] = width
>> 1;
365 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
367 else if ( format
== mlt_image_rgb24
)
370 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
371 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
376 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
377 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
381 /** Get an image from a frame.
384 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
386 // Get the properties from the frame
387 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
389 // Obtain the frame number of this frame
390 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
393 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
395 // Get the producer properties
396 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
398 // Fetch the video_context
399 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
401 // Get the video_index
402 int index
= mlt_properties_get_int( properties
, "video_index" );
404 // Obtain the expected frame numer
405 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
407 // Calculate the real time code
408 double real_timecode
= producer_time_of_frame( this, position
);
410 // Get the video stream
411 AVStream
*stream
= context
->streams
[ index
];
414 AVCodecContext
*codec_context
= stream
->codec
;
419 // Get the conversion frame
420 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
422 // Special case pause handling flag
425 // Special case ffwd handling
428 // Current time calcs
429 double current_time
= mlt_properties_get_double( properties
, "_current_time" );
431 // We may want to use the source fps if available
432 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
434 // Get the seekable status
435 int seekable
= mlt_properties_get_int( properties
, "seekable" );
437 // Generate the size in bytes
440 // Hopefully provide better support for streams...
441 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
443 // Set the result arguments that we know here (only *buffer is now required)
444 *width
= codec_context
->width
;
445 *height
= codec_context
->height
;
449 case mlt_image_yuv420p
:
450 size
= *width
* 3 * ( *height
+ 1 ) / 2;
452 case mlt_image_rgb24
:
453 size
= *width
* ( *height
+ 1 ) * 3;
456 *format
= mlt_image_yuv422
;
457 size
= *width
* ( *height
+ 1 ) * 2;
461 // Set this on the frame properties
462 mlt_properties_set_int( frame_properties
, "width", *width
);
463 mlt_properties_set_int( frame_properties
, "height", *height
);
465 // Construct the output image
466 *buffer
= mlt_pool_alloc( size
);
469 if ( position
!= expected
)
471 if ( position
+ 1 == expected
)
473 // We're paused - use last image
476 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
478 // Fast forward - seeking is inefficient for small distances - just ignore following frames
479 ignore
= position
- expected
;
481 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
483 // Set to the real timecode
484 av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
);
486 // Remove the cached info relating to the previous position
487 mlt_properties_set_double( properties
, "_current_time", real_timecode
);
489 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
494 // Duplicate the last image if necessary
495 if ( av_frame
!= NULL
&& ( paused
|| mlt_properties_get_double( properties
, "_current_time" ) >= real_timecode
) &&
499 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
501 // Set this on the frame properties
502 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
510 // Temporary hack to improve intra frame only
511 if ( !strcmp( codec_context
->codec
->name
, "mjpeg" ) )
514 memset( &pkt
, 0, sizeof( pkt
) );
516 // Construct an AVFrame for YUV422 conversion
517 if ( av_frame
== NULL
)
519 av_frame
= calloc( 1, sizeof( AVFrame
) );
520 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, free
, NULL
);
523 while( ret
>= 0 && !got_picture
)
526 ret
= av_read_frame( context
, &pkt
);
528 // We only deal with video from the selected video_index
529 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
531 // Determine time code of the packet
532 if ( pkt
.dts
!= AV_NOPTS_VALUE
)
533 current_time
= av_q2d( stream
->time_base
) * pkt
.dts
;
535 current_time
= real_timecode
;
538 if ( must_decode
|| current_time
>= real_timecode
)
539 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
544 if ( ( int )( current_time
* 100 ) < ( int )( real_timecode
* 100 ) - 7 )
549 else if ( current_time
>= real_timecode
)
553 else if ( ignore
-- )
560 // We're finished with this packet regardless
561 av_free_packet( &pkt
);
564 // Now handle the picture if we have one
567 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
568 mlt_properties_set_int( frame_properties
, "top_field_first", av_frame
->top_field_first
);
570 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
572 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
574 if ( current_time
== 0 && source_fps
!= 0 )
576 double fps
= mlt_properties_get_double( properties
, "fps" );
577 current_time
= ceil( source_fps
* ( double )position
/ fps
) * ( 1 / source_fps
);
578 mlt_properties_set_double( properties
, "_current_time", current_time
);
582 mlt_properties_set_double( properties
, "_current_time", current_time
);
587 // Set the field order property for this frame
588 mlt_properties_set_int( frame_properties
, "top_field_first",
589 mlt_properties_get_int( properties
, "top_field_first" ) );
591 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
592 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
597 /** Set up video handling.
600 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
602 // Get the properties
603 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
605 // Fetch the video_context
606 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
608 // Get the video_index
609 int index
= mlt_properties_get_int( properties
, "video_index" );
611 // Get the frame properties
612 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
614 if ( context
!= NULL
&& index
!= -1 )
616 // Get the video stream
617 AVStream
*stream
= context
->streams
[ index
];
620 AVCodecContext
*codec_context
= stream
->codec
;
623 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
625 // Initialise the codec if necessary
629 codec
= avcodec_find_decoder( codec_context
->codec_id
);
631 // If we don't have a codec and we can't initialise it, we can't do much more...
632 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
634 // Now store the codec with its destructor
635 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
639 // Remember that we can't use this later
640 mlt_properties_set_int( properties
, "video_index", -1 );
644 // No codec, no show...
647 double source_fps
= 0;
648 int norm_aspect_ratio
= mlt_properties_get_int( properties
, "norm_aspect_ratio" );
649 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
652 // XXX: We won't know the real aspect ratio until an image is decoded
653 // but we do need it now (to satisfy filter_resize) - take a guess based
655 if ( force_aspect_ratio
> 0.0 )
657 aspect_ratio
= force_aspect_ratio
;
659 else if ( !norm_aspect_ratio
&& codec_context
->sample_aspect_ratio
.num
> 0 )
661 aspect_ratio
= av_q2d( codec_context
->sample_aspect_ratio
);
665 int is_pal
= mlt_properties_get_double( properties
, "fps" ) == 25.0;
666 aspect_ratio
= is_pal ?
59.0/54.0 : 10.0/11.0;
670 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
672 // We'll use fps if it's available
673 if ( source_fps
> 0 && source_fps
< 30 )
674 mlt_properties_set_double( properties
, "source_fps", source_fps
);
675 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
677 // Set the width and height
678 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
679 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
680 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
682 mlt_frame_push_get_image( frame
, producer_get_image
);
683 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
687 mlt_properties_set_int( frame_properties
, "test_image", 1 );
692 mlt_properties_set_int( frame_properties
, "test_image", 1 );
696 /** Get the audio from a frame.
699 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
701 // Get the properties from the frame
702 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
704 // Obtain the frame number of this frame
705 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
708 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
710 // Get the producer properties
711 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
713 // Fetch the audio_context
714 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
716 // Get the audio_index
717 int index
= mlt_properties_get_int( properties
, "audio_index" );
719 // Get the seekable status
720 int seekable
= mlt_properties_get_int( properties
, "seekable" );
722 // Obtain the expected frame numer
723 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
725 // Obtain the resample context if it exists (not always needed)
726 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
728 // Obtain the audio buffer
729 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
731 // Get amount of audio used
732 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
734 // Calculate the real time code
735 double real_timecode
= producer_time_of_frame( this, position
);
737 // Get the audio stream
738 AVStream
*stream
= context
->streams
[ index
];
741 AVCodecContext
*codec_context
= stream
->codec
;
746 // Number of frames to ignore (for ffwd)
749 // Flag for paused (silence)
752 // Check for resample and create if necessary
753 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
755 // Create the resampler
756 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
758 // And store it on properties
759 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
761 else if ( resample
== NULL
)
763 *channels
= codec_context
->channels
;
764 *frequency
= codec_context
->sample_rate
;
767 // Check for audio buffer and create if necessary
768 if ( audio_buffer
== NULL
)
770 // Allocate the audio buffer
771 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
773 // And store it on properties for reuse
774 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
778 if ( position
!= expected
)
780 if ( position
+ 1 == expected
)
782 // We're paused - silence required
785 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
787 // Fast forward - seeking is inefficient for small distances - just ignore following frames
788 ignore
= position
- expected
;
790 else if ( position
< expected
|| position
- expected
>= 12 )
792 // Set to the real timecode
793 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
796 // Clear the usage in the audio buffer
801 // Get the audio if required
806 int16_t *temp
= mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
808 memset( &pkt
, 0, sizeof( pkt
) );
810 while( ret
>= 0 && !got_audio
)
812 // Check if the buffer already contains the samples required
813 if ( audio_used
>= *samples
&& ignore
== 0 )
820 ret
= av_read_frame( context
, &pkt
);
823 uint8_t *ptr
= pkt
.data
;
826 // We only deal with audio from the selected audio_index
827 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
830 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
843 if ( resample
!= NULL
)
845 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
849 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
850 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
854 while ( ignore
&& audio_used
> *samples
)
857 audio_used
-= *samples
;
858 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
862 // If we're behind, ignore this packet
863 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
864 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
868 // We're finished with this packet regardless
869 av_free_packet( &pkt
);
872 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
873 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
875 // Now handle the audio if we have enough
876 if ( audio_used
>= *samples
)
878 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
879 audio_used
-= *samples
;
880 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
884 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
887 // Store the number of audio samples still available
888 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
890 // Release the temporary audio
891 mlt_pool_release( temp
);
895 // Get silence and don't touch the context
896 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
899 // Regardless of speed (other than paused), we expect to get the next frame
901 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
906 /** Set up audio handling.
909 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
911 // Get the properties
912 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
914 // Fetch the audio_context
915 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
917 // Get the audio_index
918 int index
= mlt_properties_get_int( properties
, "audio_index" );
920 // Deal with audio context
921 if ( context
!= NULL
&& index
!= -1 )
923 // Get the frame properties
924 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
926 // Get the audio stream
927 AVStream
*stream
= context
->streams
[ index
];
930 AVCodecContext
*codec_context
= stream
->codec
;
933 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
935 // Initialise the codec if necessary
939 codec
= avcodec_find_decoder( codec_context
->codec_id
);
941 // If we don't have a codec and we can't initialise it, we can't do much more...
942 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
944 // Now store the codec with its destructor
945 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
950 // Remember that we can't use this later
951 mlt_properties_set_int( properties
, "audio_index", -1 );
955 // No codec, no show...
958 mlt_frame_push_audio( frame
, producer_get_audio
);
959 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
964 /** Our get frame implementation.
967 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
969 // Create an empty frame
970 *frame
= mlt_frame_init( );
972 // Update timecode on the frame we're creating
973 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
975 // Set the position of this producer
976 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
979 producer_set_up_video( this, *frame
);
982 producer_set_up_audio( this, *frame
);
984 // Set the aspect_ratio
985 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
987 // Calculate the next timecode
988 mlt_producer_prepare_next( this );