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 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
292 if ( av
== 0 && !av_bypass
&& audio_index
!= -1 && video_index
!= -1 )
294 // We'll use the open one as our video_context
295 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
297 // And open again for our audio context
298 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
299 av_find_stream_info( context
);
302 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
304 else if ( av
!= 2 && video_index
!= -1 )
306 // We only have a video context
307 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
309 else if ( audio_index
!= -1 )
311 // We only have an audio context
312 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
316 // Something has gone wrong
320 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
324 // Unlock the mutex now
330 /** Convert a frame position to a time code.
333 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
335 // Get the properties
336 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
339 double fps
= mlt_properties_get_double( properties
, "fps" );
342 return ( double )position
/ fps
;
345 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
347 // EXPERIMENTAL IMAGE NORMALISATIONS
348 if ( pix_fmt
== PIX_FMT_YUV420P
&& format
== mlt_image_yuv422
)
351 register int half
= width
>> 1;
352 register uint8_t *Y
= ( ( AVPicture
* )frame
)->data
[ 0 ];
353 register uint8_t *U
= ( ( AVPicture
* )frame
)->data
[ 1 ];
354 register uint8_t *V
= ( ( AVPicture
* )frame
)->data
[ 2 ];
355 register uint8_t *d
= buffer
;
356 register uint8_t *y
, *u
, *v
;
373 Y
+= ( ( AVPicture
* )frame
)->linesize
[ 0 ];
386 Y
+= ( ( AVPicture
* )frame
)->linesize
[ 0 ];
387 U
+= ( ( AVPicture
* )frame
)->linesize
[ 1 ];
388 V
+= ( ( AVPicture
* )frame
)->linesize
[ 2 ];
391 else if ( format
== mlt_image_yuv420p
)
394 pict
.data
[0] = buffer
;
395 pict
.data
[1] = buffer
+ width
* height
;
396 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
397 pict
.linesize
[0] = width
;
398 pict
.linesize
[1] = width
>> 1;
399 pict
.linesize
[2] = width
>> 1;
400 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
402 else if ( format
== mlt_image_rgb24
)
405 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
406 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
411 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
412 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
416 /** Get an image from a frame.
419 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
421 // Get the properties from the frame
422 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
424 // Obtain the frame number of this frame
425 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
428 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
430 // Get the producer properties
431 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
433 // Fetch the video_context
434 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
436 // Get the video_index
437 int index
= mlt_properties_get_int( properties
, "video_index" );
439 // Obtain the expected frame numer
440 mlt_position expected
= mlt_properties_get_position( properties
, "video_expected" );
442 // Calculate the real time code
443 double real_timecode
= producer_time_of_frame( this, position
);
445 // Get the video stream
446 AVStream
*stream
= context
->streams
[ index
];
449 AVCodecContext
*codec_context
= &stream
->codec
;
454 // Get the conversion frame
455 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
457 // Special case pause handling flag
460 // Special case ffwd handling
463 // Current time calcs
464 double current_time
= mlt_properties_get_double( properties
, "current_time" );
466 // We may want to use the source fps if available
467 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
469 // Get the seekable status
470 int seekable
= mlt_properties_get_int( properties
, "seekable" );
472 // Generate the size in bytes
475 // Hopefully provide better support for streams...
476 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
478 // Set the result arguments that we know here (only *buffer is now required)
479 *width
= codec_context
->width
;
480 *height
= codec_context
->height
;
484 case mlt_image_yuv420p
:
485 size
= *width
* 3 * ( *height
+ 1 ) / 2;
487 case mlt_image_rgb24
:
488 size
= *width
* ( *height
+ 1 ) * 3;
491 *format
= mlt_image_yuv422
;
492 size
= *width
* ( *height
+ 1 ) * 2;
496 // Set this on the frame properties
497 mlt_properties_set_int( frame_properties
, "width", *width
);
498 mlt_properties_set_int( frame_properties
, "height", *height
);
500 // Construct the output image
501 *buffer
= mlt_pool_alloc( size
);
504 if ( position
!= expected
)
506 if ( position
+ 1 == expected
)
508 // We're paused - use last image
511 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
513 // Fast forward - seeking is inefficient for small distances - just ignore following frames
514 ignore
= position
- expected
;
516 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
518 // Set to the real timecode
519 av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
);
521 // Remove the cached info relating to the previous position
522 mlt_properties_set_double( properties
, "current_time", real_timecode
);
524 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
529 // Duplicate the last image if necessary
530 if ( av_frame
!= NULL
&& ( paused
|| mlt_properties_get_double( properties
, "current_time" ) >= real_timecode
) &&
534 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
536 // Set this on the frame properties
537 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
545 // Temporary hack to improve intra frame only
546 if ( !strcmp( codec_context
->codec
->name
, "mjpeg" ) )
549 memset( &pkt
, 0, sizeof( pkt
) );
551 // Construct an AVFrame for YUV422 conversion
552 if ( av_frame
== NULL
)
554 av_frame
= calloc( 1, sizeof( AVFrame
) );
555 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, free
, NULL
);
558 while( ret
>= 0 && !got_picture
)
561 ret
= av_read_frame( context
, &pkt
);
563 // We only deal with video from the selected video_index
564 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
566 // Determine time code of the packet
567 if ( pkt
.dts
!= AV_NOPTS_VALUE
)
568 current_time
= av_q2d( stream
->time_base
) * pkt
.dts
;
570 current_time
= real_timecode
;
573 if ( must_decode
|| current_time
>= real_timecode
)
574 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
579 if ( ( int )( current_time
* 100 ) < ( int )( real_timecode
* 100 ) - 7 )
584 else if ( current_time
>= real_timecode
)
588 else if ( ignore
-- )
592 mlt_properties_set_int( properties
, "top_field_first", av_frame
->top_field_first
);
596 // We're finished with this packet regardless
597 av_free_packet( &pkt
);
600 // Now handle the picture if we have one
603 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
605 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
607 if ( current_time
== 0 && source_fps
!= 0 )
609 double fps
= mlt_properties_get_double( properties
, "fps" );
610 current_time
= ceil( source_fps
* ( double )position
/ fps
) * ( 1 / source_fps
);
611 mlt_properties_set_double( properties
, "current_time", current_time
);
615 mlt_properties_set_double( properties
, "current_time", current_time
);
620 // Set the field order property for this frame
621 mlt_properties_set_int( frame_properties
, "top_field_first",
622 mlt_properties_get_int( properties
, "top_field_first" ) );
624 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
625 mlt_properties_set_position( properties
, "video_expected", position
+ 1 );
630 /** Set up video handling.
633 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
635 // Get the properties
636 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
638 // Fetch the video_context
639 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
641 // Get the video_index
642 int index
= mlt_properties_get_int( properties
, "video_index" );
644 // Get the frame properties
645 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
647 if ( context
!= NULL
&& index
!= -1 )
649 // Get the video stream
650 AVStream
*stream
= context
->streams
[ index
];
653 AVCodecContext
*codec_context
= &stream
->codec
;
656 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
658 // Initialise the codec if necessary
662 codec
= avcodec_find_decoder( codec_context
->codec_id
);
664 // If we don't have a codec and we can't initialise it, we can't do much more...
665 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
667 // Now store the codec with its destructor
668 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
672 // Remember that we can't use this later
673 mlt_properties_set_int( properties
, "video_index", -1 );
677 // No codec, no show...
680 double source_fps
= 0;
681 int norm_aspect_ratio
= mlt_properties_get_int( properties
, "norm_aspect_ratio" );
683 // XXX: We won't know the real aspect ratio until an image is decoded
684 // but we do need it now (to satisfy filter_resize) - take a guess based
686 if ( !norm_aspect_ratio
&& codec_context
->sample_aspect_ratio
.num
> 0 )
688 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
692 int is_pal
= mlt_properties_get_double( properties
, "fps" ) == 25.0;
693 mlt_properties_set_double( properties
, "aspect_ratio", is_pal ?
59.0/54.0 : 10.0/11.0 );
697 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
699 // We'll use fps if it's available
700 if ( source_fps
> 0 && source_fps
< 30 )
701 mlt_properties_set_double( properties
, "source_fps", source_fps
);
703 // Set the width and height
704 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
705 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
707 mlt_frame_push_get_image( frame
, producer_get_image
);
708 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
712 mlt_properties_set_int( frame_properties
, "test_image", 1 );
717 mlt_properties_set_int( frame_properties
, "test_image", 1 );
721 /** Get the audio from a frame.
724 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
726 // Get the properties from the frame
727 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
729 // Obtain the frame number of this frame
730 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
733 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
735 // Get the producer properties
736 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
738 // Fetch the audio_context
739 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
741 // Get the audio_index
742 int index
= mlt_properties_get_int( properties
, "audio_index" );
744 // Get the seekable status
745 int seekable
= mlt_properties_get_int( properties
, "seekable" );
747 // Obtain the expected frame numer
748 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
750 // Obtain the resample context if it exists (not always needed)
751 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
753 // Obtain the audio buffer
754 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
756 // Get amount of audio used
757 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
759 // Calculate the real time code
760 double real_timecode
= producer_time_of_frame( this, position
);
762 // Get the audio stream
763 AVStream
*stream
= context
->streams
[ index
];
766 AVCodecContext
*codec_context
= &stream
->codec
;
771 // Number of frames to ignore (for ffwd)
774 // Flag for paused (silence)
777 // Check for resample and create if necessary
778 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
780 // Create the resampler
781 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
783 // And store it on properties
784 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
786 else if ( resample
== NULL
)
788 *channels
= codec_context
->channels
;
789 *frequency
= codec_context
->sample_rate
;
792 // Check for audio buffer and create if necessary
793 if ( audio_buffer
== NULL
)
795 // Allocate the audio buffer
796 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
798 // And store it on properties for reuse
799 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
803 if ( position
!= expected
)
805 if ( position
+ 1 == expected
)
807 // We're paused - silence required
810 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
812 // Fast forward - seeking is inefficient for small distances - just ignore following frames
813 ignore
= position
- expected
;
815 else if ( position
< expected
|| position
- expected
>= 12 )
817 // Set to the real timecode
818 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
821 // Clear the usage in the audio buffer
826 // Get the audio if required
831 int16_t *temp
= mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
833 memset( &pkt
, 0, sizeof( pkt
) );
835 while( ret
>= 0 && !got_audio
)
837 // Check if the buffer already contains the samples required
838 if ( audio_used
>= *samples
&& ignore
== 0 )
845 ret
= av_read_frame( context
, &pkt
);
848 uint8_t *ptr
= pkt
.data
;
851 // We only deal with audio from the selected audio_index
852 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
855 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
868 if ( resample
!= NULL
)
870 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
874 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
875 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
879 while ( ignore
&& audio_used
> *samples
)
882 audio_used
-= *samples
;
883 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
887 // If we're behind, ignore this packet
888 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
889 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
893 // We're finished with this packet regardless
894 av_free_packet( &pkt
);
897 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
898 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
900 // Now handle the audio if we have enough
901 if ( audio_used
>= *samples
)
903 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
904 audio_used
-= *samples
;
905 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
909 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
912 // Store the number of audio samples still available
913 mlt_properties_set_int( properties
, "audio_used", audio_used
);
915 // Release the temporary audio
916 mlt_pool_release( temp
);
920 // Get silence and don't touch the context
921 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
924 // Regardless of speed (other than paused), we expect to get the next frame
926 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
931 /** Set up audio handling.
934 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
936 // Get the properties
937 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
939 // Fetch the audio_context
940 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
942 // Get the audio_index
943 int index
= mlt_properties_get_int( properties
, "audio_index" );
945 // Deal with audio context
946 if ( context
!= NULL
&& index
!= -1 )
948 // Get the frame properties
949 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
951 // Get the audio stream
952 AVStream
*stream
= context
->streams
[ index
];
955 AVCodecContext
*codec_context
= &stream
->codec
;
958 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
960 // Initialise the codec if necessary
964 codec
= avcodec_find_decoder( codec_context
->codec_id
);
966 // If we don't have a codec and we can't initialise it, we can't do much more...
967 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
969 // Now store the codec with its destructor
970 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
975 // Remember that we can't use this later
976 mlt_properties_set_int( properties
, "audio_index", -1 );
980 // No codec, no show...
983 mlt_frame_push_audio( frame
, producer_get_audio
);
984 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
989 /** Our get frame implementation.
992 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
994 // Create an empty frame
995 *frame
= mlt_frame_init( );
997 // Update timecode on the frame we're creating
998 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1000 // Set the position of this producer
1001 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1004 producer_set_up_video( this, *frame
);
1007 producer_set_up_audio( this, *frame
);
1009 // Set the aspect_ratio
1010 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1012 // Calculate the next timecode
1013 mlt_producer_prepare_next( this );