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" ), AVSEEK_FLAG_BACKWARD
) >= 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 ( seekable
&& ( 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, AVSEEK_FLAG_BACKWARD
);
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
);
546 // Temporary hack to improve intra frame only
547 if ( !strcmp( codec_context
->codec
->name
, "mjpeg" ) )
550 memset( &pkt
, 0, sizeof( pkt
) );
552 // Construct an AVFrame for YUV422 conversion
553 if ( av_frame
== NULL
)
555 av_frame
= calloc( 1, sizeof( AVFrame
) );
556 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, free
, NULL
);
559 while( ret
>= 0 && !got_picture
)
562 ret
= av_read_frame( context
, &pkt
);
564 // We only deal with video from the selected video_index
565 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
567 // Determine time code of the packet
568 if ( pkt
.pts
!= AV_NOPTS_VALUE
)
569 current_time
= ( double )pkt
.pts
/ 1000000.0;
571 current_time
= real_timecode
;
574 if ( must_decode
|| current_time
>= real_timecode
)
575 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
580 if ( ( int )( current_time
* 100 ) < ( int )( real_timecode
* 100 ) - 7 )
585 else if ( current_time
>= real_timecode
)
589 else if ( ignore
-- )
593 mlt_properties_set_int( properties
, "top_field_first", av_frame
->top_field_first
);
597 // We're finished with this packet regardless
598 av_free_packet( &pkt
);
601 // Now handle the picture if we have one
604 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
606 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
608 if ( current_time
== 0 && source_fps
!= 0 )
610 double fps
= mlt_properties_get_double( properties
, "fps" );
611 current_time
= ceil( source_fps
* ( double )position
/ fps
) * ( 1 / source_fps
);
612 mlt_properties_set_double( properties
, "current_time", current_time
);
616 mlt_properties_set_double( properties
, "current_time", current_time
);
621 // Set the field order property for this frame
622 mlt_properties_set_int( frame_properties
, "top_field_first",
623 mlt_properties_get_int( properties
, "top_field_first" ) );
625 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
626 mlt_properties_set_position( properties
, "video_expected", position
+ 1 );
631 /** Set up video handling.
634 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
636 // Get the properties
637 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
639 // Fetch the video_context
640 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
642 // Get the video_index
643 int index
= mlt_properties_get_int( properties
, "video_index" );
645 // Get the frame properties
646 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
648 if ( context
!= NULL
&& index
!= -1 )
650 // Get the video stream
651 AVStream
*stream
= context
->streams
[ index
];
654 AVCodecContext
*codec_context
= &stream
->codec
;
657 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
659 // Initialise the codec if necessary
663 codec
= avcodec_find_decoder( codec_context
->codec_id
);
665 // If we don't have a codec and we can't initialise it, we can't do much more...
666 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
668 // Now store the codec with its destructor
669 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
673 // Remember that we can't use this later
674 mlt_properties_set_int( properties
, "video_index", -1 );
678 // No codec, no show...
681 double source_fps
= 0;
682 int norm_aspect_ratio
= mlt_properties_get_int( properties
, "norm_aspect_ratio" );
684 // XXX: We won't know the real aspect ratio until an image is decoded
685 // but we do need it now (to satisfy filter_resize) - take a guess based
687 if ( !norm_aspect_ratio
&& codec_context
->sample_aspect_ratio
.num
> 0 )
689 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
693 int is_pal
= mlt_properties_get_double( properties
, "fps" ) == 25.0;
694 mlt_properties_set_double( properties
, "aspect_ratio", is_pal ?
59.0/54.0 : 10.0/11.0 );
697 //fprintf( stderr, "AVFORMAT: sample aspect %f %dx%d\n", av_q2d( codec_context->sample_aspect_ratio ), codec_context->width, codec_context->height );
700 source_fps
= ( double )codec_context
->frame_rate
/ ( codec_context
->frame_rate_base
== 0 ?
1 : codec_context
->frame_rate_base
);
702 // We'll use fps if it's available
703 if ( source_fps
> 0 && source_fps
< 30 )
704 mlt_properties_set_double( properties
, "source_fps", source_fps
);
706 // Set the width and height
707 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
708 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
710 mlt_frame_push_get_image( frame
, producer_get_image
);
711 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
715 mlt_properties_set_int( frame_properties
, "test_image", 1 );
720 mlt_properties_set_int( frame_properties
, "test_image", 1 );
724 /** Get the audio from a frame.
727 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
729 // Get the properties from the frame
730 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
732 // Obtain the frame number of this frame
733 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
736 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
738 // Get the producer properties
739 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
741 // Fetch the audio_context
742 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
744 // Get the audio_index
745 int index
= mlt_properties_get_int( properties
, "audio_index" );
747 // Get the seekable status
748 int seekable
= mlt_properties_get_int( properties
, "seekable" );
750 // Obtain the expected frame numer
751 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
753 // Obtain the resample context if it exists (not always needed)
754 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
756 // Obtain the audio buffer
757 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
759 // Get amount of audio used
760 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
762 // Calculate the real time code
763 double real_timecode
= producer_time_of_frame( this, position
);
765 // Get the audio stream
766 AVStream
*stream
= context
->streams
[ index
];
769 AVCodecContext
*codec_context
= &stream
->codec
;
774 // Number of frames to ignore (for ffwd)
777 // Flag for paused (silence)
780 // Check for resample and create if necessary
781 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
783 // Create the resampler
784 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
786 // And store it on properties
787 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
789 else if ( resample
== NULL
)
791 *channels
= codec_context
->channels
;
792 *frequency
= codec_context
->sample_rate
;
795 // Check for audio buffer and create if necessary
796 if ( audio_buffer
== NULL
)
798 // Allocate the audio buffer
799 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
801 // And store it on properties for reuse
802 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
806 if ( position
!= expected
)
808 if ( position
+ 1 == expected
)
810 // We're paused - silence required
813 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
815 // Fast forward - seeking is inefficient for small distances - just ignore following frames
816 ignore
= position
- expected
;
818 else if ( position
< expected
|| position
- expected
>= 12 )
820 // Set to the real timecode
821 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
824 // Clear the usage in the audio buffer
829 // Get the audio if required
834 int16_t *temp
= mlt_pool_alloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
836 memset( &pkt
, 0, sizeof( pkt
) );
838 while( ret
>= 0 && !got_audio
)
840 // Check if the buffer already contains the samples required
841 if ( audio_used
>= *samples
&& ignore
== 0 )
848 ret
= av_read_frame( context
, &pkt
);
851 uint8_t *ptr
= pkt
.data
;
854 // We only deal with audio from the selected audio_index
855 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
858 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
871 if ( resample
!= NULL
)
873 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
877 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
878 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
882 while ( ignore
&& audio_used
> *samples
)
885 audio_used
-= *samples
;
886 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
890 // If we're behind, ignore this packet
891 float current_pts
= (float)pkt
.pts
/ 1000000.0;
892 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
896 // We're finished with this packet regardless
897 av_free_packet( &pkt
);
900 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
901 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
903 // Now handle the audio if we have enough
904 if ( audio_used
>= *samples
)
906 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
907 audio_used
-= *samples
;
908 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
912 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
915 // Store the number of audio samples still available
916 mlt_properties_set_int( properties
, "audio_used", audio_used
);
918 // Release the temporary audio
919 mlt_pool_release( temp
);
923 // Get silence and don't touch the context
924 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
927 // Regardless of speed (other than paused), we expect to get the next frame
929 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
934 /** Set up audio handling.
937 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
939 // Get the properties
940 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
942 // Fetch the audio_context
943 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
945 // Get the audio_index
946 int index
= mlt_properties_get_int( properties
, "audio_index" );
948 // Deal with audio context
949 if ( context
!= NULL
&& index
!= -1 )
951 // Get the frame properties
952 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
954 // Get the audio stream
955 AVStream
*stream
= context
->streams
[ index
];
958 AVCodecContext
*codec_context
= &stream
->codec
;
961 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
963 // Initialise the codec if necessary
967 codec
= avcodec_find_decoder( codec_context
->codec_id
);
969 // If we don't have a codec and we can't initialise it, we can't do much more...
970 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
972 // Now store the codec with its destructor
973 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
978 // Remember that we can't use this later
979 mlt_properties_set_int( properties
, "audio_index", -1 );
983 // No codec, no show...
986 mlt_frame_push_audio( frame
, producer_get_audio
);
987 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
992 /** Our get frame implementation.
995 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
997 // Create an empty frame
998 *frame
= mlt_frame_init( );
1000 // Update timecode on the frame we're creating
1001 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1003 // Set the position of this producer
1004 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1007 producer_set_up_video( this, *frame
);
1010 producer_set_up_audio( this, *frame
);
1012 // Set the aspect_ratio
1013 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1015 // Calculate the next timecode
1016 mlt_producer_prepare_next( this );