2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
26 // ffmpeg Header files
32 // System header files
38 void avformat_lock( );
39 void avformat_unlock( );
41 // Forward references.
42 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
);
43 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
45 /** Constructor for libavformat.
48 mlt_producer
producer_avformat_init( mlt_profile profile
, char *file
)
50 mlt_producer
this = NULL
;
52 // Check that we have a non-NULL argument
55 // Construct the producer
56 this = calloc( 1, sizeof( struct mlt_producer_s
) );
59 if ( mlt_producer_init( this, NULL
) == 0 )
62 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
64 // Set the resource property (required for all producers)
65 mlt_properties_set( properties
, "resource", file
);
67 // Register our get_frame implementation
68 this->get_frame
= producer_get_frame
;
71 if ( producer_open( this, profile
, file
) != 0 )
74 mlt_producer_close( this );
83 /** Find the default streams.
86 static void find_default_streams( AVFormatContext
*context
, int *audio_index
, int *video_index
)
90 // Allow for multiple audio and video streams in the file and select first of each (if available)
91 for( i
= 0; i
< context
->nb_streams
; i
++ )
93 // Get the codec context
94 AVCodecContext
*codec_context
= context
->streams
[ i
]->codec
;
96 if ( avcodec_find_decoder( codec_context
->codec_id
) == NULL
)
99 // Determine the type and obtain the first index of each type
100 switch( codec_context
->codec_type
)
102 case CODEC_TYPE_VIDEO
:
103 if ( *video_index
< 0 )
106 case CODEC_TYPE_AUDIO
:
107 if ( *audio_index
< 0 )
116 /** Producer file destructor.
119 static void producer_file_close( void *context
)
121 if ( context
!= NULL
)
123 // Lock the mutex now
127 av_close_input_file( context
);
129 // Unlock the mutex now
134 /** Producer file destructor.
137 static void producer_codec_close( void *codec
)
141 // Lock the mutex now
145 avcodec_close( codec
);
147 // Unlock the mutex now
155 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
)
157 // Return an error code (0 == no error)
160 // Context for avformat
161 AVFormatContext
*context
= NULL
;
163 // Get the properties
164 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
166 // We will treat everything with the producer fps
167 double fps
= mlt_profile_fps( profile
);
169 // Lock the mutex now
172 // If "MRL", then create AVInputFormat
173 AVInputFormat
*format
= NULL
;
174 AVFormatParameters
*params
= NULL
;
175 char *standard
= NULL
;
176 char *mrl
= strchr( file
, ':' );
178 // AV option (0 = both, 1 = video, 2 = audio)
181 // Setting lowest log level
182 av_log_set_level( -1 );
184 // Only if there is not a protocol specification that avformat can handle
185 if ( mrl
&& !url_exist( file
) )
187 // 'file' becomes format abbreviation
191 format
= av_find_input_format( file
);
193 // Eat the format designator
199 params
= calloc( sizeof( AVFormatParameters
), 1 );
201 // These are required by video4linux (defaults)
203 params
->height
= 480;
204 params
->time_base
= (AVRational
){1,25};
205 // params->device = file;
206 params
->channels
= 2;
207 params
->sample_rate
= 48000;
210 // XXX: this does not work anymore since avdevice
211 // TODO: make producer_avddevice?
213 mrl
= strchr( file
, '?' );
217 char *name
= strdup( ++mrl
);
218 char *value
= strchr( name
, ':' );
223 char *t
= strchr( value
, '&' );
226 if ( !strcmp( name
, "frame_rate" ) )
227 params
->time_base
.den
= atoi( value
);
228 else if ( !strcmp( name
, "frame_rate_base" ) )
229 params
->time_base
.num
= atoi( value
);
230 else if ( !strcmp( name
, "sample_rate" ) )
231 params
->sample_rate
= atoi( value
);
232 else if ( !strcmp( name
, "channels" ) )
233 params
->channels
= atoi( value
);
234 else if ( !strcmp( name
, "width" ) )
235 params
->width
= atoi( value
);
236 else if ( !strcmp( name
, "height" ) )
237 params
->height
= atoi( value
);
238 else if ( !strcmp( name
, "standard" ) )
240 standard
= strdup( value
);
241 params
->standard
= standard
;
243 else if ( !strcmp( name
, "av" ) )
247 mrl
= strchr( mrl
, '&' );
251 // Now attempt to open the file
252 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
254 // Cleanup AVFormatParameters
258 // If successful, then try to get additional info
261 // Get the stream info
262 error
= av_find_stream_info( context
) < 0;
264 // Continue if no error
267 // We will default to the first audio and video streams found
268 int audio_index
= -1;
269 int video_index
= -1;
272 // Now set properties where we can (use default unknowns if required)
273 if ( context
->duration
!= AV_NOPTS_VALUE
)
275 // This isn't going to be accurate for all formats
276 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
+ 0.5 );
277 mlt_properties_set_position( properties
, "out", frames
- 1 );
278 mlt_properties_set_position( properties
, "length", frames
);
281 // Find default audio and video streams
282 find_default_streams( context
, &audio_index
, &video_index
);
284 if ( context
->start_time
!= AV_NOPTS_VALUE
)
285 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
287 // Check if we're seekable (something funny about mpeg here :-/)
288 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) )
290 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
291 mlt_properties_set_data( properties
, "dummy_context", context
, 0, producer_file_close
, NULL
);
292 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
293 av_find_stream_info( context
);
298 // Store selected audio and video indexes on properties
299 mlt_properties_set_int( properties
, "audio_index", audio_index
);
300 mlt_properties_set_int( properties
, "video_index", video_index
);
301 mlt_properties_set_int( properties
, "_last_position", -1 );
303 // Fetch the width, height and aspect ratio
304 if ( video_index
!= -1 )
306 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
307 mlt_properties_set_int( properties
, "width", codec_context
->width
);
308 mlt_properties_set_int( properties
, "height", codec_context
->height
);
309 mlt_properties_set_double( properties
, "aspect_ratio", av_q2d( codec_context
->sample_aspect_ratio
) );
313 if (context
->title
!= NULL
)
314 mlt_properties_set(properties
, "meta.attr.title.markup", context
->title
);
315 if (context
->author
!= NULL
)
316 mlt_properties_set(properties
, "meta.attr.author.markup", context
->author
);
317 if (context
->copyright
!= NULL
)
318 mlt_properties_set(properties
, "meta.attr.copyright.markup", context
->copyright
);
319 if (context
->comment
!= NULL
)
320 mlt_properties_set(properties
, "meta.attr.comment.markup", context
->comment
);
321 if (context
->album
!= NULL
)
322 mlt_properties_set(properties
, "meta.attr.album.markup", context
->album
);
323 if (context
->year
!= 0)
324 mlt_properties_set_int(properties
, "meta.attr.year.markup", context
->year
);
325 if (context
->track
!= 0)
326 mlt_properties_set_int(properties
, "meta.attr.track.markup", context
->track
);
328 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
329 if ( av
== 0 && !av_bypass
&& audio_index
!= -1 && video_index
!= -1 )
331 // We'll use the open one as our video_context
332 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
334 // And open again for our audio context
335 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
336 av_find_stream_info( context
);
339 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
341 else if ( av
!= 2 && video_index
!= -1 )
343 // We only have a video context
344 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
346 else if ( audio_index
!= -1 )
348 // We only have an audio context
349 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
353 // Something has gone wrong
357 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
361 // Unlock the mutex now
367 /** Convert a frame position to a time code.
370 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
372 return ( double )position
/ mlt_producer_get_fps( this );
375 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
378 if ( format
== mlt_image_yuv420p
)
380 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
381 width
, height
, PIX_FMT_YUV420P
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
383 output
.data
[0] = buffer
;
384 output
.data
[1] = buffer
+ width
* height
;
385 output
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
386 output
.linesize
[0] = width
;
387 output
.linesize
[1] = width
>> 1;
388 output
.linesize
[2] = width
>> 1;
389 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
390 output
.data
, output
.linesize
);
391 sws_freeContext( context
);
393 else if ( format
== mlt_image_rgb24
)
395 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
396 width
, height
, PIX_FMT_RGB24
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
398 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
399 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
400 output
.data
, output
.linesize
);
401 sws_freeContext( context
);
405 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
406 width
, height
, PIX_FMT_YUYV422
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
408 avpicture_fill( &output
, buffer
, PIX_FMT_YUYV422
, width
, height
);
409 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
410 output
.data
, output
.linesize
);
411 sws_freeContext( context
);
414 if ( format
== mlt_image_yuv420p
)
417 pict
.data
[0] = buffer
;
418 pict
.data
[1] = buffer
+ width
* height
;
419 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
420 pict
.linesize
[0] = width
;
421 pict
.linesize
[1] = width
>> 1;
422 pict
.linesize
[2] = width
>> 1;
423 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
425 else if ( format
== mlt_image_rgb24
)
428 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
429 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
434 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
435 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
440 /** Get an image from a frame.
443 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
445 // Get the properties from the frame
446 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
448 // Obtain the frame number of this frame
449 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
452 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
454 // Get the producer properties
455 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
457 // Fetch the video_context
458 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
460 // Get the video_index
461 int index
= mlt_properties_get_int( properties
, "video_index" );
463 // Obtain the expected frame numer
464 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
466 // Get the video stream
467 AVStream
*stream
= context
->streams
[ index
];
470 AVCodecContext
*codec_context
= stream
->codec
;
475 // Get the conversion frame
476 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
478 // Special case pause handling flag
481 // Special case ffwd handling
484 // We may want to use the source fps if available
485 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
486 double fps
= mlt_producer_get_fps( this );
488 // This is the physical frame position in the source
489 int req_position
= ( int )( position
/ fps
* source_fps
+ 0.5 );
491 // Get the seekable status
492 int seekable
= mlt_properties_get_int( properties
, "seekable" );
494 // Generate the size in bytes
497 // Hopefully provide better support for streams...
498 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
500 // Determines if we have to decode all frames in a sequence
503 // Set the result arguments that we know here (only *buffer is now required)
504 *width
= codec_context
->width
;
505 *height
= codec_context
->height
;
509 case mlt_image_yuv420p
:
510 size
= *width
* 3 * ( *height
+ 1 ) / 2;
512 case mlt_image_rgb24
:
513 size
= *width
* ( *height
+ 1 ) * 3;
516 *format
= mlt_image_yuv422
;
517 size
= *width
* ( *height
+ 1 ) * 2;
521 // Set this on the frame properties
522 mlt_properties_set_int( frame_properties
, "width", *width
);
523 mlt_properties_set_int( frame_properties
, "height", *height
);
525 // Construct the output image
526 *buffer
= mlt_pool_alloc( size
);
528 // Temporary hack to improve intra frame only
529 must_decode
= strcmp( codec_context
->codec
->name
, "mjpeg" ) &&
530 strcmp( codec_context
->codec
->name
, "rawvideo" ) &&
531 strcmp( codec_context
->codec
->name
, "dvvideo" );
534 if ( position
!= expected
)
536 if ( av_frame
!= NULL
&& position
+ 1 == expected
)
538 // We're paused - use last image
541 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
543 // Fast forward - seeking is inefficient for small distances - just ignore following frames
544 ignore
= ( int )( ( position
- expected
) / fps
* source_fps
);
546 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
548 // Calculate the timestamp for the requested frame
549 int64_t timestamp
= ( int64_t )( ( double )req_position
/ source_fps
* AV_TIME_BASE
+ 0.5 );
550 if ( ( uint64_t )context
->start_time
!= AV_NOPTS_VALUE
)
551 timestamp
+= context
->start_time
;
553 timestamp
-= AV_TIME_BASE
;
557 // Set to the timestamp
558 av_seek_frame( context
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
560 // Remove the cached info relating to the previous position
561 mlt_properties_set_int( properties
, "_current_position", -1 );
562 mlt_properties_set_int( properties
, "_last_position", -1 );
563 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
568 // Duplicate the last image if necessary (see comment on rawvideo below)
569 int current_position
= mlt_properties_get_int( properties
, "_current_position" );
570 int got_picture
= mlt_properties_get_int( properties
, "_got_picture" );
571 if ( av_frame
!= NULL
&& got_picture
&& ( paused
|| current_position
>= req_position
) && av_bypass
== 0 )
574 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
576 // Set this on the frame properties
577 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
582 int int_position
= 0;
585 av_init_packet( &pkt
);
587 // Construct an AVFrame for YUV422 conversion
588 if ( av_frame
== NULL
)
590 av_frame
= avcodec_alloc_frame( );
591 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, av_free
, NULL
);
594 while( ret
>= 0 && !got_picture
)
597 ret
= av_read_frame( context
, &pkt
);
599 // We only deal with video from the selected video_index
600 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
602 // Determine time code of the packet
603 int_position
= ( int )( av_q2d( stream
->time_base
) * pkt
.dts
* source_fps
+ 0.5 );
604 if ( context
->start_time
!= AV_NOPTS_VALUE
)
605 int_position
-= ( int )( context
->start_time
* source_fps
/ AV_TIME_BASE
+ 0.5 );
606 int last_position
= mlt_properties_get_int( properties
, "_last_position" );
607 if ( int_position
== last_position
)
608 int_position
= last_position
+ 1;
609 mlt_properties_set_int( properties
, "_last_position", int_position
);
612 if ( must_decode
|| int_position
>= req_position
)
613 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
618 if ( int_position
< req_position
)
623 else if ( int_position
>= req_position
)
627 else if ( ignore
-- )
632 av_free_packet( &pkt
);
636 av_free_packet( &pkt
);
639 // Now handle the picture if we have one
642 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
643 mlt_properties_set_int( frame_properties
, "top_field_first", av_frame
->top_field_first
);
644 mlt_properties_set_int( 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 );
653 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
654 // above will break the pause behaviour - so we wipe the frame now
655 if ( !strcmp( codec_context
->codec
->name
, "rawvideo" ) )
656 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
658 // Set the field order property for this frame
659 mlt_properties_set_int( frame_properties
, "top_field_first", mlt_properties_get_int( properties
, "top_field_first" ) );
661 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
662 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
667 /** Set up video handling.
670 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
672 // Get the properties
673 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
675 // Fetch the video_context
676 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
678 // Get the video_index
679 int index
= mlt_properties_get_int( properties
, "video_index" );
681 // Get the frame properties
682 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
684 if ( context
!= NULL
&& index
!= -1 )
686 // Get the video stream
687 AVStream
*stream
= context
->streams
[ index
];
690 AVCodecContext
*codec_context
= stream
->codec
;
693 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
695 // Initialise the codec if necessary
698 // Initialise multi-threading
699 int thread_count
= mlt_properties_get_int( properties
, "threads" );
700 if ( thread_count
== 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
701 thread_count
= atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
702 if ( thread_count
> 1 )
704 avcodec_thread_init( codec_context
, thread_count
);
705 codec_context
->thread_count
= thread_count
;
709 codec
= avcodec_find_decoder( codec_context
->codec_id
);
711 // If we don't have a codec and we can't initialise it, we can't do much more...
712 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
714 // Now store the codec with its destructor
715 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
719 // Remember that we can't use this later
720 mlt_properties_set_int( properties
, "video_index", -1 );
724 // No codec, no show...
727 double source_fps
= 0;
728 int norm_aspect_ratio
= mlt_properties_get_int( properties
, "norm_aspect_ratio" );
729 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
732 // XXX: We won't know the real aspect ratio until an image is decoded
733 // but we do need it now (to satisfy filter_resize) - take a guess based
735 if ( force_aspect_ratio
> 0.0 )
737 aspect_ratio
= force_aspect_ratio
;
739 else if ( !norm_aspect_ratio
&& codec_context
->sample_aspect_ratio
.num
> 0 )
741 aspect_ratio
= av_q2d( codec_context
->sample_aspect_ratio
);
745 int is_pal
= mlt_producer_get_fps( this ) == 25.0;
746 aspect_ratio
= is_pal ?
59.0/54.0 : 10.0/11.0;
750 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
752 // We'll use fps if it's available
753 if ( source_fps
> 0 )
754 mlt_properties_set_double( properties
, "source_fps", source_fps
);
756 mlt_properties_set_double( properties
, "source_fps", mlt_producer_get_fps( this ) );
757 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
759 // Set the width and height
760 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
761 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
762 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
764 mlt_frame_push_get_image( frame
, producer_get_image
);
765 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
769 mlt_properties_set_int( frame_properties
, "test_image", 1 );
774 mlt_properties_set_int( frame_properties
, "test_image", 1 );
778 /** Get the audio from a frame.
781 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
783 // Get the properties from the frame
784 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
786 // Obtain the frame number of this frame
787 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
790 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
792 // Get the producer properties
793 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
795 // Fetch the audio_context
796 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
798 // Get the audio_index
799 int index
= mlt_properties_get_int( properties
, "audio_index" );
801 // Get the seekable status
802 int seekable
= mlt_properties_get_int( properties
, "seekable" );
804 // Obtain the expected frame numer
805 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
807 // Obtain the resample context if it exists (not always needed)
808 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
810 // Obtain the audio buffer
811 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
813 // Get amount of audio used
814 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
816 // Calculate the real time code
817 double real_timecode
= producer_time_of_frame( this, position
);
819 // Get the audio stream
820 AVStream
*stream
= context
->streams
[ index
];
823 AVCodecContext
*codec_context
= stream
->codec
;
828 // Number of frames to ignore (for ffwd)
831 // Flag for paused (silence)
834 // Check for resample and create if necessary
835 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
837 // Create the resampler
838 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
840 // And store it on properties
841 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
843 else if ( resample
== NULL
)
845 *channels
= codec_context
->channels
;
846 *frequency
= codec_context
->sample_rate
;
849 // Check for audio buffer and create if necessary
850 if ( audio_buffer
== NULL
)
852 // Allocate the audio buffer
853 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
855 // And store it on properties for reuse
856 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
860 if ( position
!= expected
)
862 if ( position
+ 1 == expected
)
864 // We're paused - silence required
867 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
869 // Fast forward - seeking is inefficient for small distances - just ignore following frames
870 ignore
= position
- expected
;
872 else if ( position
< expected
|| position
- expected
>= 12 )
874 // Set to the real timecode
875 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
878 // Clear the usage in the audio buffer
883 // Get the audio if required
888 int16_t *temp
= av_malloc( sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
);
890 av_init_packet( &pkt
);
892 while( ret
>= 0 && !got_audio
)
894 // Check if the buffer already contains the samples required
895 if ( audio_used
>= *samples
&& ignore
== 0 )
902 ret
= av_read_frame( context
, &pkt
);
905 uint8_t *ptr
= pkt
.data
;
907 // We only deal with audio from the selected audio_index
908 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
910 int data_size
= sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
;
913 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
914 ret
= avcodec_decode_audio2( codec_context
, temp
, &data_size
, ptr
, len
);
916 ret
= avcodec_decode_audio( codec_context
, temp
, &data_size
, ptr
, len
);
929 if ( resample
!= NULL
)
931 audio_used
+= audio_resample( resample
, &audio_buffer
[ audio_used
* *channels
], temp
, data_size
/ ( codec_context
->channels
* sizeof( int16_t ) ) );
935 memcpy( &audio_buffer
[ audio_used
* *channels
], temp
, data_size
);
936 audio_used
+= data_size
/ ( codec_context
->channels
* sizeof( int16_t ) );
940 while ( ignore
&& audio_used
> *samples
)
943 audio_used
-= *samples
;
944 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
948 // If we're behind, ignore this packet
949 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
950 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
954 // We're finished with this packet regardless
955 av_free_packet( &pkt
);
958 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
959 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
961 // Now handle the audio if we have enough
962 if ( audio_used
>= *samples
)
964 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
965 audio_used
-= *samples
;
966 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
970 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
973 // Store the number of audio samples still available
974 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
976 // Release the temporary audio
981 // Get silence and don't touch the context
982 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
985 // Regardless of speed (other than paused), we expect to get the next frame
987 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
992 /** Set up audio handling.
995 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
997 // Get the properties
998 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1000 // Fetch the audio_context
1001 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1003 // Get the audio_index
1004 int index
= mlt_properties_get_int( properties
, "audio_index" );
1006 // Deal with audio context
1007 if ( context
!= NULL
&& index
!= -1 )
1009 // Get the frame properties
1010 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1012 // Get the audio stream
1013 AVStream
*stream
= context
->streams
[ index
];
1015 // Get codec context
1016 AVCodecContext
*codec_context
= stream
->codec
;
1019 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
1021 // Initialise the codec if necessary
1022 if ( codec
== NULL
)
1024 // Initialise multi-threading
1025 int thread_count
= mlt_properties_get_int( properties
, "threads" );
1026 if ( thread_count
== 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1027 thread_count
= atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1028 if ( thread_count
> 1 )
1030 avcodec_thread_init( codec_context
, thread_count
);
1031 codec_context
->thread_count
= thread_count
;
1035 codec
= avcodec_find_decoder( codec_context
->codec_id
);
1037 // If we don't have a codec and we can't initialise it, we can't do much more...
1038 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
1040 // Now store the codec with its destructor
1041 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
1046 // Remember that we can't use this later
1047 mlt_properties_set_int( properties
, "audio_index", -1 );
1051 // No codec, no show...
1052 if ( codec
!= NULL
)
1054 mlt_frame_push_audio( frame
, producer_get_audio
);
1055 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1056 mlt_properties_set_int( frame_properties
, "frequency", codec_context
->sample_rate
);
1057 mlt_properties_set_int( frame_properties
, "channels", codec_context
->channels
);
1062 /** Our get frame implementation.
1065 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
1067 // Create an empty frame
1068 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1070 // Update timecode on the frame we're creating
1071 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1073 // Set the position of this producer
1074 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1077 producer_set_up_video( this, *frame
);
1080 producer_set_up_audio( this, *frame
);
1082 // Set the aspect_ratio
1083 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1085 // Calculate the next timecode
1086 mlt_producer_prepare_next( this );