2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, 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
33 // System header files
39 void avformat_lock( );
40 void avformat_unlock( );
42 // Forward references.
43 static int producer_open( mlt_producer
this, char *file
);
44 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
46 /** Constructor for libavformat.
49 mlt_producer
producer_avformat_init( char *file
)
51 mlt_producer
this = NULL
;
53 // Check that we have a non-NULL argument
56 // Construct the producer
57 this = calloc( 1, sizeof( struct mlt_producer_s
) );
60 if ( mlt_producer_init( this, NULL
) == 0 )
63 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
65 // Set the resource property (required for all producers)
66 mlt_properties_set( properties
, "resource", file
);
68 // Register our get_frame implementation
69 this->get_frame
= producer_get_frame
;
72 if ( producer_open( this, file
) != 0 )
75 mlt_producer_close( this );
84 /** Find the default streams.
87 static void find_default_streams( AVFormatContext
*context
, int *audio_index
, int *video_index
)
91 // Allow for multiple audio and video streams in the file and select first of each (if available)
92 for( i
= 0; i
< context
->nb_streams
; i
++ )
94 // Get the codec context
95 AVCodecContext
*codec_context
= context
->streams
[ i
]->codec
;
97 if ( avcodec_find_decoder( codec_context
->codec_id
) == NULL
)
100 // Determine the type and obtain the first index of each type
101 switch( codec_context
->codec_type
)
103 case CODEC_TYPE_VIDEO
:
104 if ( *video_index
< 0 )
107 case CODEC_TYPE_AUDIO
:
108 if ( *audio_index
< 0 )
117 /** Producer file destructor.
120 static void producer_file_close( void *context
)
122 if ( context
!= NULL
)
124 // Lock the mutex now
128 av_close_input_file( context
);
130 // Unlock the mutex now
135 /** Producer file destructor.
138 static void producer_codec_close( void *codec
)
142 // Lock the mutex now
146 avcodec_close( codec
);
148 // Unlock the mutex now
156 static int producer_open( mlt_producer
this, char *file
)
158 // Return an error code (0 == no error)
161 // Context for avformat
162 AVFormatContext
*context
= NULL
;
164 // Get the properties
165 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
167 // We will treat everything with the producer fps
168 double fps
= mlt_properties_get_double( properties
, "fps" );
170 // Lock the mutex now
173 // If "MRL", then create AVInputFormat
174 AVInputFormat
*format
= NULL
;
175 AVFormatParameters
*params
= NULL
;
176 char *standard
= NULL
;
177 char *mrl
= strchr( file
, ':' );
179 // AV option (0 = both, 1 = video, 2 = audio)
182 // Setting lowest log level
183 av_log_set_level( -1 );
185 // Only if there is not a protocol specification that avformat can handle
186 if ( mrl
&& !url_exist( file
) )
188 // 'file' becomes format abbreviation
192 format
= av_find_input_format( file
);
194 // Eat the format designator
200 params
= calloc( sizeof( AVFormatParameters
), 1 );
202 // These are required by video4linux (defaults)
204 params
->height
= 480;
205 params
->time_base
= (AVRational
){1,25};
206 params
->device
= file
;
207 params
->channels
= 2;
208 params
->sample_rate
= 48000;
212 mrl
= strchr( file
, '?' );
216 char *name
= strdup( ++mrl
);
217 char *value
= strchr( name
, ':' );
222 char *t
= strchr( value
, '&' );
225 if ( !strcmp( name
, "frame_rate" ) )
226 params
->time_base
.den
= atoi( value
);
227 else if ( !strcmp( name
, "frame_rate_base" ) )
228 params
->time_base
.num
= atoi( value
);
229 else if ( !strcmp( name
, "sample_rate" ) )
230 params
->sample_rate
= atoi( value
);
231 else if ( !strcmp( name
, "channels" ) )
232 params
->channels
= atoi( value
);
233 else if ( !strcmp( name
, "width" ) )
234 params
->width
= atoi( value
);
235 else if ( !strcmp( name
, "height" ) )
236 params
->height
= atoi( value
);
237 else if ( !strcmp( name
, "standard" ) )
239 standard
= strdup( value
);
240 params
->standard
= standard
;
242 else if ( !strcmp( name
, "av" ) )
246 mrl
= strchr( mrl
, '&' );
250 // Now attempt to open the file
251 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
253 // Cleanup AVFormatParameters
257 // If successful, then try to get additional info
260 // Get the stream info
261 error
= av_find_stream_info( context
) < 0;
263 // Continue if no error
266 // We will default to the first audio and video streams found
267 int audio_index
= -1;
268 int video_index
= -1;
271 // Now set properties where we can (use default unknowns if required)
272 if ( context
->duration
!= AV_NOPTS_VALUE
)
274 // This isn't going to be accurate for all formats
275 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
+ 0.5 );
276 mlt_properties_set_position( properties
, "out", frames
- 1 );
277 mlt_properties_set_position( properties
, "length", frames
);
280 // Find default audio and video streams
281 find_default_streams( context
, &audio_index
, &video_index
);
283 if ( context
->start_time
!= AV_NOPTS_VALUE
)
284 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
286 // Check if we're seekable (something funny about mpeg here :-/)
287 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) )
289 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
290 mlt_properties_set_data( properties
, "dummy_context", context
, 0, producer_file_close
, NULL
);
291 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
292 av_find_stream_info( context
);
297 // Store selected audio and video indexes on properties
298 mlt_properties_set_int( properties
, "audio_index", audio_index
);
299 mlt_properties_set_int( properties
, "video_index", video_index
);
300 mlt_properties_set_int( properties
, "_last_position", -1 );
302 // Fetch the width, height and aspect ratio
303 if ( video_index
!= -1 )
305 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
306 mlt_properties_set_int( properties
, "width", codec_context
->width
);
307 mlt_properties_set_int( properties
, "height", codec_context
->height
);
308 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
312 if (context
->title
!= NULL
)
313 mlt_properties_set(properties
, "meta.attr.title.markup", context
->title
);
314 if (context
->author
!= NULL
)
315 mlt_properties_set(properties
, "meta.attr.author.markup", context
->author
);
316 if (context
->copyright
!= NULL
)
317 mlt_properties_set(properties
, "meta.attr.copyright.markup", context
->copyright
);
318 if (context
->comment
!= NULL
)
319 mlt_properties_set(properties
, "meta.attr.comment.markup", context
->comment
);
320 if (context
->album
!= NULL
)
321 mlt_properties_set(properties
, "meta.attr.album.markup", context
->album
);
322 if (context
->year
!= 0)
323 mlt_properties_set_int(properties
, "meta.attr.year.markup", context
->year
);
324 if (context
->track
!= 0)
325 mlt_properties_set_int(properties
, "meta.attr.track.markup", context
->track
);
327 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
328 if ( av
== 0 && !av_bypass
&& audio_index
!= -1 && video_index
!= -1 )
330 // We'll use the open one as our video_context
331 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
333 // And open again for our audio context
334 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
335 av_find_stream_info( context
);
338 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
340 else if ( av
!= 2 && video_index
!= -1 )
342 // We only have a video context
343 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
345 else if ( audio_index
!= -1 )
347 // We only have an audio context
348 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
352 // Something has gone wrong
356 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
360 // Unlock the mutex now
366 /** Convert a frame position to a time code.
369 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
371 // Get the properties
372 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
375 double fps
= mlt_properties_get_double( properties
, "fps" );
378 return ( double )position
/ fps
;
381 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
384 if ( format
== mlt_image_yuv420p
)
386 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
387 width
, height
, PIX_FMT_YUV420P
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
389 output
.data
[0] = buffer
;
390 output
.data
[1] = buffer
+ width
* height
;
391 output
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
392 output
.linesize
[0] = width
;
393 output
.linesize
[1] = width
>> 1;
394 output
.linesize
[2] = width
>> 1;
395 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
396 output
.data
, output
.linesize
);
397 sws_freeContext( context
);
399 else if ( format
== mlt_image_rgb24
)
401 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
402 width
, height
, PIX_FMT_RGB24
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
404 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
405 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
406 output
.data
, output
.linesize
);
407 sws_freeContext( context
);
411 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
412 width
, height
, PIX_FMT_YUYV422
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
414 avpicture_fill( &output
, buffer
, PIX_FMT_YUYV422
, width
, height
);
415 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
416 output
.data
, output
.linesize
);
417 sws_freeContext( context
);
420 if ( format
== mlt_image_yuv420p
)
423 pict
.data
[0] = buffer
;
424 pict
.data
[1] = buffer
+ width
* height
;
425 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
426 pict
.linesize
[0] = width
;
427 pict
.linesize
[1] = width
>> 1;
428 pict
.linesize
[2] = width
>> 1;
429 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
431 else if ( format
== mlt_image_rgb24
)
434 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
435 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
440 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
441 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
446 /** Get an image from a frame.
449 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
451 // Get the properties from the frame
452 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
454 // Obtain the frame number of this frame
455 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
458 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
460 // Get the producer properties
461 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
463 // Fetch the video_context
464 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
466 // Get the video_index
467 int index
= mlt_properties_get_int( properties
, "video_index" );
469 // Obtain the expected frame numer
470 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
472 // Get the video stream
473 AVStream
*stream
= context
->streams
[ index
];
476 AVCodecContext
*codec_context
= stream
->codec
;
481 // Get the conversion frame
482 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
484 // Special case pause handling flag
487 // Special case ffwd handling
490 // We may want to use the source fps if available
491 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
492 double fps
= mlt_properties_get_double( properties
, "fps" );
494 // This is the physical frame position in the source
495 int req_position
= ( int )( position
/ fps
* source_fps
+ 0.5 );
497 // Get the seekable status
498 int seekable
= mlt_properties_get_int( properties
, "seekable" );
500 // Generate the size in bytes
503 // Hopefully provide better support for streams...
504 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
506 // Determines if we have to decode all frames in a sequence
509 // Set the result arguments that we know here (only *buffer is now required)
510 *width
= codec_context
->width
;
511 *height
= codec_context
->height
;
515 case mlt_image_yuv420p
:
516 size
= *width
* 3 * ( *height
+ 1 ) / 2;
518 case mlt_image_rgb24
:
519 size
= *width
* ( *height
+ 1 ) * 3;
522 *format
= mlt_image_yuv422
;
523 size
= *width
* ( *height
+ 1 ) * 2;
527 // Set this on the frame properties
528 mlt_properties_set_int( frame_properties
, "width", *width
);
529 mlt_properties_set_int( frame_properties
, "height", *height
);
531 // Construct the output image
532 *buffer
= mlt_pool_alloc( size
);
534 // Temporary hack to improve intra frame only
535 must_decode
= strcmp( codec_context
->codec
->name
, "mjpeg" ) &&
536 strcmp( codec_context
->codec
->name
, "rawvideo" ) &&
537 strcmp( codec_context
->codec
->name
, "dvvideo" );
540 if ( position
!= expected
)
542 if ( av_frame
!= NULL
&& position
+ 1 == expected
)
544 // We're paused - use last image
547 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
549 // Fast forward - seeking is inefficient for small distances - just ignore following frames
550 ignore
= ( int )( ( position
- expected
) / fps
* source_fps
);
552 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
554 // Calculate the timestamp for the requested frame
555 int64_t timestamp
= ( int64_t )( ( double )req_position
/ source_fps
* AV_TIME_BASE
+ 0.5 );
556 if ( ( uint64_t )context
->start_time
!= AV_NOPTS_VALUE
)
557 timestamp
+= context
->start_time
;
559 timestamp
-= AV_TIME_BASE
;
563 // Set to the timestamp
564 av_seek_frame( context
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
566 // Remove the cached info relating to the previous position
567 mlt_properties_set_int( properties
, "_current_position", -1 );
568 mlt_properties_set_int( properties
, "_last_position", -1 );
569 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
574 // Duplicate the last image if necessary (see comment on rawvideo below)
575 int current_position
= mlt_properties_get_int( properties
, "_current_position" );
576 int got_picture
= mlt_properties_get_int( properties
, "_got_picture" );
577 if ( av_frame
!= NULL
&& got_picture
&& ( paused
|| current_position
>= req_position
) && av_bypass
== 0 )
580 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
582 // Set this on the frame properties
583 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
588 int int_position
= 0;
591 av_init_packet( &pkt
);
593 // Construct an AVFrame for YUV422 conversion
594 if ( av_frame
== NULL
)
596 av_frame
= avcodec_alloc_frame( );
597 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, av_free
, NULL
);
600 while( ret
>= 0 && !got_picture
)
603 ret
= av_read_frame( context
, &pkt
);
605 // We only deal with video from the selected video_index
606 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
608 // Determine time code of the packet
609 int_position
= ( int )( av_q2d( stream
->time_base
) * pkt
.dts
* source_fps
+ 0.5 );
610 if ( context
->start_time
!= AV_NOPTS_VALUE
)
611 int_position
-= ( int )( context
->start_time
* source_fps
/ AV_TIME_BASE
+ 0.5 );
612 int last_position
= mlt_properties_get_int( properties
, "_last_position" );
613 if ( int_position
== last_position
)
614 int_position
= last_position
+ 1;
615 mlt_properties_set_int( properties
, "_last_position", int_position
);
618 if ( must_decode
|| int_position
>= req_position
)
619 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
624 if ( int_position
< req_position
)
629 else if ( int_position
>= req_position
)
633 else if ( ignore
-- )
640 // Now handle the picture if we have one
643 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
644 mlt_properties_set_int( frame_properties
, "top_field_first", av_frame
->top_field_first
);
645 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
646 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
647 mlt_properties_set_int( properties
, "_current_position", int_position
);
648 mlt_properties_set_int( properties
, "_got_picture", 1 );
651 // We're finished with this packet regardless
652 av_free_packet( &pkt
);
656 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
657 // above will break the pause behaviour - so we wipe the frame now
658 if ( !strcmp( codec_context
->codec
->name
, "rawvideo" ) )
659 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
661 // Set the field order property for this frame
662 mlt_properties_set_int( frame_properties
, "top_field_first", mlt_properties_get_int( properties
, "top_field_first" ) );
664 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
665 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
670 /** Set up video handling.
673 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
675 // Get the properties
676 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
678 // Fetch the video_context
679 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
681 // Get the video_index
682 int index
= mlt_properties_get_int( properties
, "video_index" );
684 // Get the frame properties
685 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
687 if ( context
!= NULL
&& index
!= -1 )
689 // Get the video stream
690 AVStream
*stream
= context
->streams
[ index
];
693 AVCodecContext
*codec_context
= stream
->codec
;
696 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
698 // Initialise the codec if necessary
702 codec
= avcodec_find_decoder( codec_context
->codec_id
);
704 // If we don't have a codec and we can't initialise it, we can't do much more...
705 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
707 // Now store the codec with its destructor
708 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
712 // Remember that we can't use this later
713 mlt_properties_set_int( properties
, "video_index", -1 );
717 // No codec, no show...
720 double source_fps
= 0;
721 int norm_aspect_ratio
= mlt_properties_get_int( properties
, "norm_aspect_ratio" );
722 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
725 // XXX: We won't know the real aspect ratio until an image is decoded
726 // but we do need it now (to satisfy filter_resize) - take a guess based
728 if ( force_aspect_ratio
> 0.0 )
730 aspect_ratio
= force_aspect_ratio
;
732 else if ( !norm_aspect_ratio
&& codec_context
->sample_aspect_ratio
.num
> 0 )
734 aspect_ratio
= av_q2d( codec_context
->sample_aspect_ratio
);
738 int is_pal
= mlt_properties_get_double( properties
, "fps" ) == 25.0;
739 aspect_ratio
= is_pal ?
59.0/54.0 : 10.0/11.0;
743 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
745 // We'll use fps if it's available
746 if ( source_fps
> 0 && source_fps
< 30 )
747 mlt_properties_set_double( properties
, "source_fps", source_fps
);
749 mlt_properties_set_double( properties
, "source_fps", mlt_properties_get_double( properties
, "fps" ) );
750 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
752 // Set the width and height
753 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
754 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
755 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
757 mlt_frame_push_get_image( frame
, producer_get_image
);
758 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
762 mlt_properties_set_int( frame_properties
, "test_image", 1 );
767 mlt_properties_set_int( frame_properties
, "test_image", 1 );
771 /** Get the audio from a frame.
774 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
776 // Get the properties from the frame
777 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
779 // Obtain the frame number of this frame
780 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
783 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
785 // Get the producer properties
786 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
788 // Fetch the audio_context
789 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
791 // Get the audio_index
792 int index
= mlt_properties_get_int( properties
, "audio_index" );
794 // Get the seekable status
795 int seekable
= mlt_properties_get_int( properties
, "seekable" );
797 // Obtain the expected frame numer
798 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
800 // Obtain the resample context if it exists (not always needed)
801 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
803 // Obtain the audio buffer
804 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
806 // Get amount of audio used
807 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
809 // Calculate the real time code
810 double real_timecode
= producer_time_of_frame( this, position
);
812 // Get the audio stream
813 AVStream
*stream
= context
->streams
[ index
];
816 AVCodecContext
*codec_context
= stream
->codec
;
821 // Number of frames to ignore (for ffwd)
824 // Flag for paused (silence)
827 // Check for resample and create if necessary
828 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
830 // Create the resampler
831 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
833 // And store it on properties
834 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
836 else if ( resample
== NULL
)
838 *channels
= codec_context
->channels
;
839 *frequency
= codec_context
->sample_rate
;
842 // Check for audio buffer and create if necessary
843 if ( audio_buffer
== NULL
)
845 // Allocate the audio buffer
846 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
848 // And store it on properties for reuse
849 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
853 if ( position
!= expected
)
855 if ( position
+ 1 == expected
)
857 // We're paused - silence required
860 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
862 // Fast forward - seeking is inefficient for small distances - just ignore following frames
863 ignore
= position
- expected
;
865 else if ( position
< expected
|| position
- expected
>= 12 )
867 // Set to the real timecode
868 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
871 // Clear the usage in the audio buffer
876 // Get the audio if required
881 int16_t *temp
= av_malloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
883 av_init_packet( &pkt
);
885 while( ret
>= 0 && !got_audio
)
887 // Check if the buffer already contains the samples required
888 if ( audio_used
>= *samples
&& ignore
== 0 )
895 ret
= av_read_frame( context
, &pkt
);
898 uint8_t *ptr
= pkt
.data
;
900 // We only deal with audio from the selected audio_index
901 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
903 int data_size
= sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
;
906 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
907 ret
= avcodec_decode_audio2( codec_context
, temp
, &data_size
, ptr
, len
);
909 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
922 if ( resample
!= NULL
)
924 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
928 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
929 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
933 while ( ignore
&& audio_used
> *samples
)
936 audio_used
-= *samples
;
937 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
941 // If we're behind, ignore this packet
942 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
943 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
947 // We're finished with this packet regardless
948 av_free_packet( &pkt
);
951 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
952 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
954 // Now handle the audio if we have enough
955 if ( audio_used
>= *samples
)
957 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
958 audio_used
-= *samples
;
959 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
963 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
966 // Store the number of audio samples still available
967 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
969 // Release the temporary audio
974 // Get silence and don't touch the context
975 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
978 // Regardless of speed (other than paused), we expect to get the next frame
980 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
985 /** Set up audio handling.
988 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
990 // Get the properties
991 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
993 // Fetch the audio_context
994 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
996 // Get the audio_index
997 int index
= mlt_properties_get_int( properties
, "audio_index" );
999 // Deal with audio context
1000 if ( context
!= NULL
&& index
!= -1 )
1002 // Get the frame properties
1003 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1005 // Get the audio stream
1006 AVStream
*stream
= context
->streams
[ index
];
1008 // Get codec context
1009 AVCodecContext
*codec_context
= stream
->codec
;
1012 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
1014 // Initialise the codec if necessary
1015 if ( codec
== NULL
)
1018 codec
= avcodec_find_decoder( codec_context
->codec_id
);
1020 // If we don't have a codec and we can't initialise it, we can't do much more...
1021 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
1023 // Now store the codec with its destructor
1024 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
1029 // Remember that we can't use this later
1030 mlt_properties_set_int( properties
, "audio_index", -1 );
1034 // No codec, no show...
1035 if ( codec
!= NULL
)
1037 mlt_frame_push_audio( frame
, producer_get_audio
);
1038 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1039 mlt_properties_set_int( frame_properties
, "frequency", codec_context
->sample_rate
);
1040 mlt_properties_set_int( frame_properties
, "channels", codec_context
->channels
);
1045 /** Our get frame implementation.
1048 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
1050 // Create an empty frame
1051 *frame
= mlt_frame_init( );
1053 // Update timecode on the frame we're creating
1054 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1056 // Set the position of this producer
1057 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1060 producer_set_up_video( this, *frame
);
1063 producer_set_up_audio( this, *frame
);
1065 // Set the aspect_ratio
1066 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1068 // Calculate the next timecode
1069 mlt_producer_prepare_next( this );