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>
25 #include <framework/mlt_profile.h>
26 #include <framework/mlt_log.h>
28 // ffmpeg Header files
34 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
35 # include "audioconvert.h"
38 // System header files
44 #if LIBAVUTIL_VERSION_INT < (50<<16)
45 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
46 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
49 #define POSITION_INITIAL (-2)
50 #define POSITION_INVALID (-1)
52 #define MAX_AUDIO_STREAMS (8)
54 void avformat_lock( );
55 void avformat_unlock( );
57 struct producer_avformat_s
59 struct mlt_producer_s parent;
60 AVFormatContext *dummy_context;
61 AVFormatContext *audio_format;
62 AVFormatContext *video_format;
63 AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ];
64 AVCodecContext *video_codec;
66 ReSampleContext *audio_resample[ MAX_AUDIO_STREAMS ];
67 mlt_position audio_expected;
68 mlt_position video_expected;
78 int16_t *audio_buffer[ MAX_AUDIO_STREAMS ];
79 size_t audio_buffer_size[ MAX_AUDIO_STREAMS ];
80 int16_t *decode_buffer[ MAX_AUDIO_STREAMS ];
81 int audio_used[ MAX_AUDIO_STREAMS ];
87 unsigned int invalid_pts_counter;
88 double resample_factor;
90 typedef struct producer_avformat_s *producer_avformat;
92 // Forward references.
93 static int producer_open( producer_avformat this, mlt_profile profile, char *file );
94 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
95 static void producer_format_close( void *context );
96 static void producer_close( mlt_producer parent );
98 /** Constructor for libavformat.
101 mlt_producer producer_avformat_init( mlt_profile profile, char *file )
105 // Report information about available demuxers and codecs as YAML Tiny
106 if ( file && strstr( file, "f-list" ) )
108 fprintf( stderr, "---\nformats:\n" );
109 AVInputFormat *format = NULL;
110 while ( ( format = av_iformat_next( format ) ) )
111 fprintf( stderr, " - %s\n", format->name );
112 fprintf( stderr, "...\n" );
115 if ( file && strstr( file, "acodec-list" ) )
117 fprintf( stderr, "---\naudio_codecs:\n" );
118 AVCodec *codec = NULL;
119 while ( ( codec = av_codec_next( codec ) ) )
120 if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
121 fprintf( stderr, " - %s\n", codec->name );
122 fprintf( stderr, "...\n" );
125 if ( file && strstr( file, "vcodec-list" ) )
127 fprintf( stderr, "---\nvideo_codecs:\n" );
128 AVCodec *codec = NULL;
129 while ( ( codec = av_codec_next( codec ) ) )
130 if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
131 fprintf( stderr, " - %s\n", codec->name );
132 fprintf( stderr, "...\n" );
136 // Check that we have a non-NULL argument
139 // Construct the producer
140 producer_avformat this = calloc( 1, sizeof( struct producer_avformat_s ) );
143 if ( mlt_producer_init( &this->parent, this ) == 0 )
145 mlt_producer producer = &this->parent;
147 // Get the properties
148 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
150 // Set the resource property (required for all producers)
151 mlt_properties_set( properties, "resource", file );
153 // Register transport implementation with the producer
154 producer->close = (mlt_destructor) producer_close;
156 // Register our get_frame implementation
157 producer->get_frame = producer_get_frame;
159 this->resample_factor = 1.0;
162 if ( producer_open( this, profile, file ) != 0 )
165 mlt_producer_close( producer );
170 // Close the file to release resources for large playlists - reopen later as needed
171 producer_format_close( this->dummy_context );
172 this->dummy_context = NULL;
173 producer_format_close( this->audio_format );
174 this->audio_format = NULL;
175 producer_format_close( this->video_format );
176 this->video_format = NULL;
178 // Default the user-selectable indices from the auto-detected indices
179 mlt_properties_set_int( properties, "audio_index", this->audio_index );
180 mlt_properties_set_int( properties, "video_index", this->video_index );
188 /** Find the default streams.
191 static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatContext *context, int *audio_index, int *video_index )
196 mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
198 // Allow for multiple audio and video streams in the file and select first of each (if available)
199 for( i = 0; i < context->nb_streams; i++ )
201 // Get the codec context
202 AVStream *stream = context->streams[ i ];
203 if ( ! stream ) continue;
204 AVCodecContext *codec_context = stream->codec;
205 if ( ! codec_context ) continue;
206 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
207 if ( ! codec ) continue;
209 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
211 // Determine the type and obtain the first index of each type
212 switch( codec_context->codec_type )
214 case CODEC_TYPE_VIDEO:
215 if ( *video_index < 0 )
217 mlt_properties_set( meta_media, key, "video" );
218 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
219 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
220 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
221 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
222 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
224 snprintf( key, sizeof(key), "meta.media.%d.codec.frame_rate", i );
225 mlt_properties_set_double( meta_media, key, (double) codec_context->time_base.den /
226 ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num ) );
227 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
228 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
229 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
230 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
232 case CODEC_TYPE_AUDIO:
233 if ( *audio_index < 0 )
235 mlt_properties_set( meta_media, key, "audio" );
236 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
237 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
238 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
240 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
241 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
242 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
243 mlt_properties_set_int( meta_media, key, codec_context->channels );
248 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
249 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
250 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
251 mlt_properties_set( meta_media, key, codec->name );
252 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
253 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
254 mlt_properties_set( meta_media, key, codec->long_name );
256 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
257 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
258 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
259 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
260 // snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
261 // mlt_properties_set_int( meta_media, key, codec_context->profile );
262 // snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
263 // mlt_properties_set_int( meta_media, key, codec_context->level );
269 /** Producer file destructor.
272 static void producer_format_close( void *context )
276 // Lock the mutex now
280 av_close_input_file( context );
282 // Unlock the mutex now
287 /** Producer file destructor.
290 static void producer_codec_close( void *codec )
294 // Lock the mutex now
298 avcodec_close( codec );
300 // Unlock the mutex now
305 static inline int dv_is_pal( AVPacket *pkt )
307 return pkt->data[3] & 0x80;
310 static int dv_is_wide( AVPacket *pkt )
312 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
314 for ( ; i < pkt->size; i += 5 /* packet size */ )
316 if ( pkt->data[ i ] == 0x61 )
318 uint8_t x = pkt->data[ i + 2 ] & 0x7;
319 return ( x == 2 ) || ( x == 7 );
325 static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
327 double aspect_ratio = 1.0;
329 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
333 if ( dv_is_pal( pkt ) )
335 aspect_ratio = dv_is_wide( pkt )
336 ? 64.0/45.0 // 16:9 PAL
337 : 16.0/15.0; // 4:3 PAL
341 aspect_ratio = dv_is_wide( pkt )
342 ? 32.0/27.0 // 16:9 NTSC
343 : 8.0/9.0; // 4:3 NTSC
349 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
350 stream->sample_aspect_ratio;
352 codec_context->sample_aspect_ratio;
354 // Override FFmpeg's notion of DV aspect ratios, which are
355 // based upon a width of 704. Since we do not have a normaliser
356 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
357 // we just coerce the values to facilitate a passive behaviour through
358 // the rescale normaliser when using equivalent producers and consumers.
359 // = display_aspect / (width * height)
360 if ( ar.num == 10 && ar.den == 11 )
361 aspect_ratio = 8.0/9.0; // 4:3 NTSC
362 else if ( ar.num == 59 && ar.den == 54 )
363 aspect_ratio = 16.0/15.0; // 4:3 PAL
364 else if ( ar.num == 40 && ar.den == 33 )
365 aspect_ratio = 32.0/27.0; // 16:9 NTSC
366 else if ( ar.num == 118 && ar.den == 81 )
367 aspect_ratio = 64.0/45.0; // 16:9 PAL
372 AVRational codec_sar = codec_context->sample_aspect_ratio;
373 AVRational stream_sar =
374 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
375 stream->sample_aspect_ratio;
379 if ( codec_sar.num > 0 )
380 aspect_ratio = av_q2d( codec_sar );
381 else if ( stream_sar.num > 0 )
382 aspect_ratio = av_q2d( stream_sar );
390 static int producer_open( producer_avformat this, mlt_profile profile, char *file )
392 // Return an error code (0 == no error)
395 // Context for avformat
396 AVFormatContext *context = NULL;
398 // Get the properties
399 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
401 // We will treat everything with the producer fps
402 double fps = mlt_profile_fps( profile );
404 // Lock the mutex now
407 // If "MRL", then create AVInputFormat
408 AVInputFormat *format = NULL;
409 AVFormatParameters *params = NULL;
410 char *standard = NULL;
411 char *mrl = strchr( file, ':' );
413 // AV option (0 = both, 1 = video, 2 = audio)
416 // Only if there is not a protocol specification that avformat can handle
417 if ( mrl && !url_exist( file ) )
419 // 'file' becomes format abbreviation
423 format = av_find_input_format( file );
425 // Eat the format designator
431 params = calloc( sizeof( AVFormatParameters ), 1 );
433 // These are required by video4linux (defaults)
435 params->height = 480;
436 params->time_base= (AVRational){1,25};
437 // params->device = file;
438 params->channels = 2;
439 params->sample_rate = 48000;
442 // XXX: this does not work anymore since avdevice
443 // TODO: make producer_avddevice?
445 mrl = strchr( file, '?' );
449 char *name = strdup( ++mrl );
450 char *value = strchr( name, ':' );
455 char *t = strchr( value, '&' );
458 if ( !strcmp( name, "frame_rate" ) )
459 params->time_base.den = atoi( value );
460 else if ( !strcmp( name, "frame_rate_base" ) )
461 params->time_base.num = atoi( value );
462 else if ( !strcmp( name, "sample_rate" ) )
463 params->sample_rate = atoi( value );
464 else if ( !strcmp( name, "channels" ) )
465 params->channels = atoi( value );
466 else if ( !strcmp( name, "width" ) )
467 params->width = atoi( value );
468 else if ( !strcmp( name, "height" ) )
469 params->height = atoi( value );
470 else if ( !strcmp( name, "standard" ) )
472 standard = strdup( value );
473 params->standard = standard;
475 else if ( !strcmp( name, "av" ) )
479 mrl = strchr( mrl, '&' );
483 // Now attempt to open the file
484 error = av_open_input_file( &context, file, format, 0, params ) < 0;
486 // Cleanup AVFormatParameters
490 // If successful, then try to get additional info
493 // Get the stream info
494 error = av_find_stream_info( context ) < 0;
496 // Continue if no error
499 // We will default to the first audio and video streams found
500 int audio_index = -1;
501 int video_index = -1;
503 // Now set properties where we can (use default unknowns if required)
504 if ( context->duration != AV_NOPTS_VALUE )
506 // This isn't going to be accurate for all formats
507 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
508 mlt_properties_set_position( properties, "out", frames - 1 );
509 mlt_properties_set_position( properties, "length", frames );
512 // Find default audio and video streams
513 find_default_streams( properties, context, &audio_index, &video_index );
515 if ( context->start_time != AV_NOPTS_VALUE )
516 this->start_time = context->start_time;
518 // Check if we're seekable (something funny about mpeg here :-/)
519 if ( strncmp( file, "pipe:", 5 ) &&
520 strncmp( file, "http:", 5 ) &&
521 strncmp( file, "udp:", 4 ) &&
522 strncmp( file, "tcp:", 4 ) &&
523 strncmp( file, "rtsp:", 5 ) &&
524 strncmp( file, "rtp:", 4 ) )
526 this->seekable = av_seek_frame( context, -1, this->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
527 mlt_properties_set_int( properties, "seekable", this->seekable );
528 producer_format_close( this->dummy_context );
529 this->dummy_context = context;
530 av_open_input_file( &context, file, NULL, 0, NULL );
531 av_find_stream_info( context );
534 // Store selected audio and video indexes on properties
535 this->audio_index = audio_index;
536 this->video_index = video_index;
537 this->first_pts = -1;
538 this->last_position = POSITION_INITIAL;
540 // Fetch the width, height and aspect ratio
541 if ( video_index != -1 )
543 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
544 mlt_properties_set_int( properties, "width", codec_context->width );
545 mlt_properties_set_int( properties, "height", codec_context->height );
547 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
549 // Fetch the first frame of DV so we can read it directly
554 ret = av_read_frame( context, &pkt );
555 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
557 mlt_properties_set_double( properties, "aspect_ratio",
558 get_aspect_ratio( context->streams[ video_index ], codec_context, &pkt ) );
565 mlt_properties_set_double( properties, "aspect_ratio",
566 get_aspect_ratio( context->streams[ video_index ], codec_context, NULL ) );
571 if ( context->title )
572 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
573 if ( context->author )
574 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
575 if ( context->copyright )
576 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
577 if ( context->comment )
578 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
579 if ( context->album )
580 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
582 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
583 if ( context->track )
584 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
586 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
587 if ( av == 0 && audio_index != -1 && video_index != -1 )
589 // We'll use the open one as our video_format
591 producer_format_close( this->video_format );
593 this->video_format = context;
595 // And open again for our audio context
596 av_open_input_file( &context, file, NULL, 0, NULL );
597 av_find_stream_info( context );
601 producer_format_close( this->audio_format );
603 this->audio_format = context;
605 else if ( av != 2 && video_index != -1 )
607 // We only have a video context
609 producer_format_close( this->video_format );
611 this->video_format = context;
613 else if ( audio_index != -1 )
615 // We only have an audio context
617 producer_format_close( this->audio_format );
619 this->audio_format = context;
623 // Something has gone wrong
629 // Unlock the mutex now
635 /** Convert a frame position to a time code.
638 static double producer_time_of_frame( mlt_producer this, mlt_position position )
640 return ( double )position / mlt_producer_get_fps( this );
643 // Collect information about all audio streams
645 static void get_audio_streams_info( producer_avformat this )
647 // Fetch the audio format context
648 AVFormatContext *context = this->audio_format;
652 i < context->nb_streams;
655 if ( context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
657 AVCodecContext *codec_context = context->streams[i]->codec;
658 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
660 // If we don't have a codec and we can't initialise it, we can't do much more...
662 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
664 this->audio_streams++;
665 this->audio_max_stream = i;
666 this->total_channels += codec_context->channels;
667 if ( codec_context->channels > this->max_channel )
668 this->max_channel = codec_context->channels;
669 if ( codec_context->sample_rate > this->max_frequency )
670 this->max_frequency = codec_context->sample_rate;
671 avcodec_close( codec_context );
676 mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
677 this->audio_streams, this->audio_max_stream, this->total_channels, this->max_channel );
680 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format *format, int width, int height )
683 if ( pix_fmt == PIX_FMT_RGB32 )
685 *format = mlt_image_rgb24a;
686 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
687 width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
689 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
690 sws_scale( context, frame->data, frame->linesize, 0, height,
691 output.data, output.linesize);
692 sws_freeContext( context );
694 else if ( *format == mlt_image_yuv420p )
696 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
697 width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
699 output.data[0] = buffer;
700 output.data[1] = buffer + width * height;
701 output.data[2] = buffer + ( 5 * width * height ) / 4;
702 output.linesize[0] = width;
703 output.linesize[1] = width >> 1;
704 output.linesize[2] = width >> 1;
705 sws_scale( context, frame->data, frame->linesize, 0, height,
706 output.data, output.linesize);
707 sws_freeContext( context );
709 else if ( *format == mlt_image_rgb24 )
711 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
712 width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
714 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
715 sws_scale( context, frame->data, frame->linesize, 0, height,
716 output.data, output.linesize);
717 sws_freeContext( context );
719 else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
721 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
722 width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
724 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
725 sws_scale( context, frame->data, frame->linesize, 0, height,
726 output.data, output.linesize);
727 sws_freeContext( context );
731 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
732 width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
734 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
735 sws_scale( context, frame->data, frame->linesize, 0, height,
736 output.data, output.linesize);
737 sws_freeContext( context );
740 if ( *format == mlt_image_yuv420p )
743 pict.data[0] = buffer;
744 pict.data[1] = buffer + width * height;
745 pict.data[2] = buffer + ( 5 * width * height ) / 4;
746 pict.linesize[0] = width;
747 pict.linesize[1] = width >> 1;
748 pict.linesize[2] = width >> 1;
749 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
751 else if ( *format == mlt_image_rgb24 )
754 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
755 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
757 else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
760 avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
761 img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
766 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
767 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
772 /** Allocate the image buffer and set it on the frame.
775 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
779 if ( codec_context->width == 0 || codec_context->height == 0 )
782 *width = codec_context->width;
783 *height = codec_context->height;
784 mlt_properties_set_int( frame_properties, "width", *width );
785 mlt_properties_set_int( frame_properties, "height", *height );
787 if ( codec_context->pix_fmt == PIX_FMT_RGB32 )
788 size = *width * ( *height + 1 ) * 4;
789 else switch ( *format )
791 case mlt_image_yuv420p:
792 size = *width * 3 * ( *height + 1 ) / 2;
794 case mlt_image_rgb24:
795 size = *width * ( *height + 1 ) * 3;
797 case mlt_image_rgb24a:
798 case mlt_image_opengl:
799 size = *width * ( *height + 1 ) * 4;
802 *format = mlt_image_yuv422;
803 size = *width * ( *height + 1 ) * 2;
807 // Construct the output image
808 *buffer = mlt_pool_alloc( size );
810 mlt_properties_set_data( frame_properties, "image", *buffer, size, mlt_pool_release, NULL );
817 /** Get an image from a frame.
820 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
823 producer_avformat this = mlt_frame_pop_service( frame );
824 mlt_producer producer = &this->parent;
826 // Get the properties from the frame
827 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
829 // Obtain the frame number of this frame
830 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
832 // Get the producer properties
833 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
837 // Fetch the video format context
838 AVFormatContext *context = this->video_format;
840 // Get the video stream
841 AVStream *stream = context->streams[ this->video_index ];
844 AVCodecContext *codec_context = stream->codec;
849 // Special case pause handling flag
852 // Special case ffwd handling
855 // We may want to use the source fps if available
856 double source_fps = mlt_properties_get_double( properties, "source_fps" );
857 double fps = mlt_producer_get_fps( producer );
859 // This is the physical frame position in the source
860 int req_position = ( int )( position / fps * source_fps + 0.5 );
862 // Determines if we have to decode all frames in a sequence
863 // Temporary hack to improve intra frame only
864 int must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
865 strcmp( codec_context->codec->name, "dvvideo" ) &&
866 strcmp( codec_context->codec->name, "huffyuv" ) &&
867 strcmp( codec_context->codec->name, "mjpeg" ) &&
868 strcmp( codec_context->codec->name, "rawvideo" );
870 int last_position = this->last_position;
872 // Turn on usage of new seek API and PTS for seeking
873 int use_new_seek = codec_context->codec_id == CODEC_ID_H264 && !strcmp( context->iformat->name, "mpegts" );
874 if ( mlt_properties_get( properties, "new_seek" ) )
875 use_new_seek = mlt_properties_get_int( properties, "new_seek" );
878 if ( position != this->video_expected || last_position < 0 )
880 if ( this->av_frame && position + 1 == this->video_expected )
882 // We're paused - use last image
885 else if ( !this->seekable && position > this->video_expected && ( position - this->video_expected ) < 250 )
887 // Fast forward - seeking is inefficient for small distances - just ignore following frames
888 ignore = ( int )( ( position - this->video_expected ) / fps * source_fps );
889 codec_context->skip_loop_filter = AVDISCARD_NONREF;
891 else if ( this->seekable && ( position < this->video_expected || position - this->video_expected >= 12 || last_position < 0 ) )
893 if ( use_new_seek && last_position == POSITION_INITIAL )
895 // find first key frame
899 while ( ret >= 0 && toscan-- > 0 )
901 ret = av_read_frame( context, &pkt );
902 if ( ret >= 0 && ( pkt.flags & PKT_FLAG_KEY ) && pkt.stream_index == this->video_index )
904 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "first_pts %lld dts %lld pts_dts_delta %d\n", pkt.pts, pkt.dts, (int)(pkt.pts - pkt.dts) );
905 this->first_pts = pkt.pts;
908 av_free_packet( &pkt );
911 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
914 // Calculate the timestamp for the requested frame
918 timestamp = ( req_position - 0.1 / source_fps ) /
919 ( av_q2d( stream->time_base ) * source_fps );
920 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "pos %d pts %lld ", req_position, timestamp );
921 if ( this->first_pts > 0 )
922 timestamp += this->first_pts;
923 else if ( context->start_time != AV_NOPTS_VALUE )
924 timestamp += context->start_time;
928 timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
929 if ( context->start_time != AV_NOPTS_VALUE )
930 timestamp += context->start_time;
933 timestamp -= AV_TIME_BASE;
936 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %lld position %d expected %d last_pos %d\n",
937 timestamp, position, this->video_expected, last_position );
939 // Seek to the timestamp
942 codec_context->skip_loop_filter = AVDISCARD_NONREF;
943 av_seek_frame( context, this->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
947 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
950 // Remove the cached info relating to the previous position
951 this->current_position = POSITION_INVALID;
952 this->last_position = POSITION_INVALID;
953 av_freep( &this->av_frame );
957 // flush any pictures still in decode buffer
958 avcodec_flush_buffers( codec_context );
963 // Duplicate the last image if necessary (see comment on rawvideo below)
964 if ( this->av_frame && this->got_picture && this->seekable
966 || this->current_position == req_position
967 || ( !use_new_seek && this->current_position > req_position ) ) )
970 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
971 convert_image( this->av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
973 mlt_frame_get_image( frame, buffer, format, width, height, writable );
978 int int_position = 0;
979 int decode_errors = 0;
982 av_init_packet( &pkt );
984 // Construct an AVFrame for YUV422 conversion
985 if ( !this->av_frame )
986 this->av_frame = avcodec_alloc_frame( );
988 while( ret >= 0 && !got_picture )
991 ret = av_read_frame( context, &pkt );
993 // We only deal with video from the selected video_index
994 if ( ret >= 0 && pkt.stream_index == this->video_index && pkt.size > 0 )
996 // Determine time code of the packet
999 int64_t pts = pkt.pts;
1000 if ( this->first_pts > 0 )
1001 pts -= this->first_pts;
1002 else if ( context->start_time != AV_NOPTS_VALUE )
1003 pts -= context->start_time;
1004 int_position = ( int )( av_q2d( stream->time_base ) * pts * source_fps + 0.1 );
1005 if ( pkt.pts == AV_NOPTS_VALUE )
1007 this->invalid_pts_counter++;
1008 if ( this->invalid_pts_counter > 20 )
1010 mlt_log_panic( MLT_PRODUCER_SERVICE(producer), "\ainvalid PTS; DISABLING NEW_SEEK!\n" );
1011 mlt_properties_set_int( properties, "new_seek", 0 );
1012 int_position = req_position;
1018 this->invalid_pts_counter = 0;
1020 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.pts %llu req_pos %d cur_pos %d pkt_pos %d",
1021 pkt.pts, req_position, this->current_position, int_position );
1025 if ( pkt.dts != AV_NOPTS_VALUE )
1027 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
1028 if ( context->start_time != AV_NOPTS_VALUE )
1029 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
1030 last_position = this->last_position;
1031 if ( int_position == last_position )
1032 int_position = last_position + 1;
1036 int_position = req_position;
1038 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d",
1039 pkt.dts, req_position, this->current_position, int_position );
1040 // Make a dumb assumption on streams that contain wild timestamps
1041 if ( abs( req_position - int_position ) > 999 )
1043 int_position = req_position;
1044 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!" );
1047 this->last_position = int_position;
1050 if ( must_decode || int_position >= req_position )
1052 codec_context->reordered_opaque = pkt.pts;
1053 if ( int_position >= req_position )
1054 codec_context->skip_loop_filter = AVDISCARD_NONE;
1055 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1056 ret = avcodec_decode_video2( codec_context, this->av_frame, &got_picture, &pkt );
1058 ret = avcodec_decode_video( codec_context, this->av_frame, &got_picture, pkt.data, pkt.size );
1060 // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1063 if ( ++decode_errors <= 10 )
1076 // Determine time code of the packet
1077 int64_t pts = this->av_frame->reordered_opaque;
1078 if ( this->first_pts > 0 )
1079 pts -= this->first_pts;
1080 else if ( context->start_time != AV_NOPTS_VALUE )
1081 pts -= context->start_time;
1082 int_position = ( int )( av_q2d( stream->time_base) * pts * source_fps + 0.1 );
1083 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "got frame %d, key %d\n", int_position, this->av_frame->key_frame );
1086 if ( int_position < req_position )
1091 else if ( int_position >= req_position )
1094 codec_context->skip_loop_filter = AVDISCARD_NONE;
1096 else if ( ignore -- )
1101 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
1102 av_free_packet( &pkt );
1104 else if ( ret >= 0 )
1106 av_free_packet( &pkt );
1109 // Now handle the picture if we have one
1112 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
1114 convert_image( this->av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
1115 if ( !mlt_properties_get( properties, "force_progressive" ) )
1116 mlt_properties_set_int( frame_properties, "progressive", !this->av_frame->interlaced_frame );
1117 this->top_field_first |= this->av_frame->top_field_first;
1118 this->current_position = int_position;
1119 this->got_picture = 1;
1128 mlt_frame_get_image( frame, buffer, format, width, height, writable );
1131 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
1132 // above will break the pause behaviour - so we wipe the frame now
1133 if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
1134 av_freep( &this->av_frame );
1138 // Set the field order property for this frame
1139 mlt_properties_set_int( frame_properties, "top_field_first", this->top_field_first );
1141 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
1142 this->video_expected = position + 1;
1147 /** Process properties as AVOptions and apply to AV context obj
1150 static void apply_properties( void *obj, mlt_properties properties, int flags )
1153 int count = mlt_properties_count( properties );
1154 for ( i = 0; i < count; i++ )
1156 const char *opt_name = mlt_properties_get_name( properties, i );
1157 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
1159 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
1160 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
1161 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
1162 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1164 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
1169 /** Initialize the video codec context.
1172 static int video_codec_init( producer_avformat this, int index, mlt_properties properties )
1174 // Initialise the codec if necessary
1175 if ( !this->video_codec )
1177 // Get the video stream
1178 AVStream *stream = this->video_format->streams[ index ];
1180 // Get codec context
1181 AVCodecContext *codec_context = stream->codec;
1184 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1186 // Initialise multi-threading
1187 int thread_count = mlt_properties_get_int( properties, "threads" );
1188 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1189 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1190 if ( thread_count > 1 )
1192 avcodec_thread_init( codec_context, thread_count );
1193 codec_context->thread_count = thread_count;
1196 // If we don't have a codec and we can't initialise it, we can't do much more...
1198 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1200 // Now store the codec with its destructor
1201 producer_codec_close( this->video_codec );
1202 this->video_codec = codec_context;
1206 // Remember that we can't use this later
1207 this->video_index = -1;
1211 // Process properties as AVOptions
1212 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1214 // Reset some image properties
1215 mlt_properties_set_int( properties, "width", this->video_codec->width );
1216 mlt_properties_set_int( properties, "height", this->video_codec->height );
1217 mlt_properties_set_double( properties, "aspect_ratio", get_aspect_ratio( stream, this->video_codec, NULL ) );
1219 // Determine the fps first from the codec
1220 double source_fps = (double) this->video_codec->time_base.den /
1221 ( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
1223 if ( mlt_properties_get( properties, "force_fps" ) )
1225 source_fps = mlt_properties_get_double( properties, "force_fps" );
1226 stream->time_base = av_d2q( source_fps, 255 );
1230 // If the muxer reports a frame rate different than the codec
1231 double muxer_fps = av_q2d( stream->r_frame_rate );
1232 // Choose the lesser - the wrong tends to be off by some multiple of 10
1233 source_fps = FFMIN( source_fps, muxer_fps );
1236 // We'll use fps if it's available
1237 if ( source_fps > 0 )
1238 mlt_properties_set_double( properties, "source_fps", source_fps );
1240 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( &this->parent ) );
1242 return this->video_codec && this->video_index > -1;
1245 /** Set up video handling.
1248 static void producer_set_up_video( producer_avformat this, mlt_frame frame )
1251 mlt_producer producer = &this->parent;
1253 // Get the properties
1254 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1256 // Fetch the video format context
1257 AVFormatContext *context = this->video_format;
1259 // Get the video_index
1260 int index = mlt_properties_get_int( properties, "video_index" );
1262 // Reopen the file if necessary
1263 if ( !context && index > -1 )
1265 mlt_events_block( properties, producer );
1266 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1267 mlt_properties_get( properties, "resource" ) );
1268 context = this->video_format;
1269 producer_format_close( this->dummy_context );
1270 this->dummy_context = NULL;
1271 mlt_events_unblock( properties, producer );
1272 if ( this->audio_format )
1273 get_audio_streams_info( this );
1275 // Process properties as AVOptions
1276 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
1279 // Exception handling for video_index
1280 if ( context && index >= (int) context->nb_streams )
1282 // Get the last video stream
1283 for ( index = context->nb_streams - 1;
1284 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO;
1286 mlt_properties_set_int( properties, "video_index", index );
1288 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
1290 // Invalidate the video stream
1292 mlt_properties_set_int( properties, "video_index", index );
1295 // Update the video properties if the index changed
1296 if ( index != this->video_index )
1298 // Reset the video properties if the index changed
1299 this->video_index = index;
1300 producer_codec_close( this->video_codec );
1301 this->video_codec = NULL;
1304 // Get the frame properties
1305 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1308 if ( context && index > -1 && video_codec_init( this, index, properties ) )
1310 // Set the frame properties
1311 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1312 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1313 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1315 // Set the width and height
1316 mlt_properties_set_int( frame_properties, "width", this->video_codec->width );
1317 mlt_properties_set_int( frame_properties, "height", this->video_codec->height );
1318 mlt_properties_set_int( frame_properties, "real_width", this->video_codec->width );
1319 mlt_properties_set_int( frame_properties, "real_height", this->video_codec->height );
1320 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1321 if ( mlt_properties_get( properties, "force_progressive" ) )
1322 mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( properties, "force_progressive" ) );
1324 // Add our image operation
1325 mlt_frame_push_service( frame, this );
1326 mlt_frame_push_get_image( frame, producer_get_image );
1330 // If something failed, use test card image
1331 mlt_properties_set_int( frame_properties, "test_image", 1 );
1335 static int seek_audio( producer_avformat this, mlt_position position, double timecode, int *ignore )
1339 // Fetch the audio_format
1340 AVFormatContext *context = this->audio_format;
1342 // Seek if necessary
1343 if ( position != this->audio_expected )
1345 if ( position + 1 == this->audio_expected )
1347 // We're paused - silence required
1350 else if ( !this->seekable && position > this->audio_expected && ( position - this->audio_expected ) < 250 )
1352 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1353 *ignore = position - this->audio_expected;
1355 else if ( position < this->audio_expected || position - this->audio_expected >= 12 )
1357 int64_t timestamp = ( int64_t )( timecode * AV_TIME_BASE + 0.5 );
1358 if ( context->start_time != AV_NOPTS_VALUE )
1359 timestamp += context->start_time;
1360 if ( timestamp < 0 )
1363 // Set to the real timecode
1364 if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
1367 // Clear the usage in the audio buffer
1368 int i = MAX_AUDIO_STREAMS + 1;
1370 this->audio_used[i - 1] = 0;
1376 static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int channels, int samples, double timecode, double source_fps )
1378 // Fetch the audio_format
1379 AVFormatContext *context = this->audio_format;
1381 // Get the current stream index
1382 int index = pkt->stream_index;
1384 // Get codec context
1385 AVCodecContext *codec_context = this->audio_codec[ index ];
1387 // Obtain the resample context if it exists (not always needed)
1388 ReSampleContext *resample = this->audio_resample[ index ];
1390 // Obtain the audio buffers
1391 int16_t *audio_buffer = this->audio_buffer[ index ];
1392 int16_t *decode_buffer = this->decode_buffer[ index ];
1394 int audio_used = this->audio_used[ index ];
1395 uint8_t *ptr = pkt->data;
1396 int len = pkt->size;
1399 while ( ptr && ret >= 0 && len > 0 )
1401 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1404 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1405 ret = avcodec_decode_audio3( codec_context, decode_buffer, &data_size, pkt );
1406 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1407 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1409 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1420 // If decoded successfully
1421 if ( data_size > 0 )
1423 // Figure out how many samples will be needed after resampling
1424 int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
1425 int samples_needed = this->resample_factor * convert_samples + 1;
1427 // Resize audio buffer to prevent overflow
1428 if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
1430 this->audio_buffer_size[ index ] *= 2;
1431 audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
1435 // Copy to audio buffer while resampling
1436 int16_t *source = decode_buffer;
1437 int16_t *dest = &audio_buffer[ audio_used * channels ];
1438 audio_used += audio_resample( resample, dest, source, convert_samples );
1442 // Straight copy to audio buffer
1443 memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
1444 audio_used += convert_samples;
1448 while ( *ignore && audio_used > samples )
1451 audio_used -= samples;
1452 memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
1453 audio_used * sizeof( int16_t ) );
1458 // If we're behind, ignore this packet
1459 if ( pkt->pts >= 0 )
1461 double current_pts = av_q2d( context->streams[ index ]->time_base ) * pkt->pts;
1462 int req_position = ( int )( timecode * source_fps + 0.5 );
1463 int int_position = ( int )( current_pts * source_fps + 0.5 );
1465 if ( context->start_time != AV_NOPTS_VALUE )
1466 int_position -= ( int )( source_fps * context->start_time / AV_TIME_BASE + 0.5 );
1467 if ( this->seekable && *ignore == 0 && int_position < req_position )
1471 this->audio_used[ index ] = audio_used;
1476 /** Get the audio from a frame.
1479 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1482 producer_avformat this = mlt_frame_pop_audio( frame );
1484 // Obtain the frame number of this frame
1485 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" );
1487 // Calculate the real time code
1488 double real_timecode = producer_time_of_frame( &this->parent, position );
1490 // Get the source fps
1491 double source_fps = mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( &this->parent ), "source_fps" );
1493 // Number of frames to ignore (for ffwd)
1496 // Flag for paused (silence)
1497 int paused = seek_audio( this, position, real_timecode, &ignore );
1499 // Fetch the audio_format
1500 AVFormatContext *context = this->audio_format;
1502 // Determine the tracks to use
1503 int index = this->audio_index;
1504 int index_max = this->audio_index + 1;
1505 if ( this->audio_index == INT_MAX )
1508 index_max = context->nb_streams;
1509 *channels = this->total_channels;
1510 *frequency = this->max_frequency;
1513 // Initialize the resamplers and buffers
1514 for ( ; index < index_max; index++ )
1516 // Get codec context
1517 AVCodecContext *codec_context = this->audio_codec[ index ];
1519 if ( codec_context && !this->audio_buffer[ index ] )
1521 // Check for resample and create if necessary
1522 if ( codec_context->channels <= 2 )
1524 // Determine by how much resampling will increase number of samples
1525 double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
1526 resample_factor *= (double) *frequency / codec_context->sample_rate;
1527 if ( resample_factor > this->resample_factor )
1528 this->resample_factor = resample_factor;
1530 // Create the resampler
1531 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
1532 this->audio_resample[ index ] = av_audio_resample_init(
1533 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1534 codec_context->channels, *frequency, codec_context->sample_rate,
1535 SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
1537 this->audio_resample[ index ] = audio_resample_init(
1538 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1539 codec_context->channels, *frequency, codec_context->sample_rate );
1544 codec_context->request_channels = this->audio_index == INT_MAX ? codec_context->channels : *channels;
1547 // Check for audio buffer and create if necessary
1548 this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1549 this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
1551 // Check for decoder buffer and create if necessary
1552 this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1556 // Get the audio if required
1563 av_init_packet( &pkt );
1565 while ( ret >= 0 && !got_audio )
1567 // Check if the buffer already contains the samples required
1568 if ( this->audio_index != INT_MAX && this->audio_used[ this->audio_index ] >= *samples && ignore == 0 )
1575 ret = av_read_frame( context, &pkt );
1577 // We only deal with audio from the selected audio index
1578 if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
1579 ( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
1580 ret = decode_audio( this, &ignore, &pkt, *channels, *samples, real_timecode, source_fps );
1581 av_free_packet( &pkt );
1583 if ( this->audio_index == INT_MAX && ret >= 0 )
1585 // Determine if there is enough audio for all streams
1587 for ( index = 0; index < context->nb_streams; index++ )
1589 if ( this->audio_codec[ index ] && this->audio_used[ index ] < *samples )
1595 // Allocate and set the frame's audio buffer
1596 int size = *samples * *channels * sizeof(int16_t);
1597 *buffer = mlt_pool_alloc( size );
1598 *format = mlt_audio_s16;
1599 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
1601 // Interleave tracks if audio_index=all
1602 if ( this->audio_index == INT_MAX )
1604 int16_t *dest = *buffer;
1606 for ( i = 0; i < *samples; i++ )
1608 for ( index = 0; index < index_max; index++ )
1609 if ( this->audio_codec[ index ] )
1611 int current_channels = this->audio_codec[ index ]->channels;
1612 int16_t *src = this->audio_buffer[ index ] + i * current_channels;
1613 memcpy( dest, src, current_channels * sizeof(int16_t) );
1614 dest += current_channels;
1617 for ( index = 0; index < index_max; index++ )
1618 if ( this->audio_codec[ index ] )
1620 int current_channels = this->audio_codec[ index ]->channels;
1621 int16_t *src = this->audio_buffer[ index ] + *samples * current_channels;
1622 this->audio_used[index] -= *samples;
1623 memmove( this->audio_buffer[ index ], src, this->audio_used[ index ] * current_channels * sizeof(int16_t) );
1626 // Copy a single track to the output buffer
1629 index = this->audio_index;
1631 // Now handle the audio if we have enough
1632 if ( this->audio_used[ index ] >= *samples )
1634 int16_t *src = this->audio_buffer[ index ];
1635 memcpy( *buffer, src, *samples * *channels * sizeof(int16_t) );
1636 this->audio_used[ index ] -= *samples;
1637 memmove( src, &src[ *samples * *channels ], this->audio_used[ index ] * *channels * sizeof(int16_t) );
1641 // Otherwise fill with silence
1642 memset( *buffer, 0, *samples * *channels * sizeof(int16_t) );
1644 if ( !this->audio_resample[ index ] )
1646 // TODO: uncomment and remove following line when full multi-channel support is ready
1647 // *channels = codec_context->channels;
1648 *frequency = this->audio_codec[ index ]->sample_rate;
1654 // Get silence and don't touch the context
1655 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1658 // Regardless of speed (other than paused), we expect to get the next frame
1660 this->audio_expected = position + 1;
1665 /** Initialize the audio codec context.
1668 static int audio_codec_init( producer_avformat this, int index, mlt_properties properties )
1670 // Initialise the codec if necessary
1671 if ( !this->audio_codec[ index ] )
1673 // Get codec context
1674 AVCodecContext *codec_context = this->audio_format->streams[index]->codec;
1677 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1679 // If we don't have a codec and we can't initialise it, we can't do much more...
1681 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1683 // Now store the codec with its destructor
1685 producer_codec_close( this->audio_codec[index] );
1686 this->audio_codec[ index ] = codec_context;
1690 // Remember that we can't use this later
1691 this->audio_index = -1;
1695 // Process properties as AVOptions
1696 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1698 return this->audio_codec[ index ] && this->audio_index > -1;
1701 /** Set up audio handling.
1704 static void producer_set_up_audio( producer_avformat this, mlt_frame frame )
1707 mlt_producer producer = &this->parent;
1709 // Get the properties
1710 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1712 // Fetch the audio format context
1713 AVFormatContext *context = this->audio_format;
1715 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1717 // Get the audio_index
1718 int index = mlt_properties_get_int( properties, "audio_index" );
1720 // Handle all audio tracks
1721 if ( mlt_properties_get( properties, "audio_index" ) &&
1722 !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
1725 // Reopen the file if necessary
1726 if ( !context && index > -1 )
1728 mlt_events_block( properties, producer );
1729 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1730 mlt_properties_get( properties, "resource" ) );
1731 context = this->audio_format;
1732 producer_format_close( this->dummy_context );
1733 this->dummy_context = NULL;
1734 mlt_events_unblock( properties, producer );
1735 get_audio_streams_info( this );
1738 // Exception handling for audio_index
1739 if ( context && index >= (int) context->nb_streams && index < INT_MAX )
1741 for ( index = context->nb_streams - 1;
1742 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO;
1744 mlt_properties_set_int( properties, "audio_index", index );
1746 if ( context && index > -1 && index < INT_MAX &&
1747 context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
1750 mlt_properties_set_int( properties, "audio_index", index );
1753 // Update the audio properties if the index changed
1754 if ( index > -1 && index != this->audio_index )
1756 producer_codec_close( this->audio_codec[ this->audio_index ] );
1757 this->audio_codec[ this->audio_index ] = NULL;
1759 this->audio_index = index;
1762 if ( context && index == INT_MAX )
1764 mlt_properties_set_int( frame_properties, "frequency", this->max_frequency );
1765 mlt_properties_set_int( frame_properties, "channels", this->total_channels );
1766 for ( index = 0; index < context->nb_streams; index++ )
1768 if ( context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO )
1769 audio_codec_init( this, index, properties );
1772 else if ( context && index > -1 && audio_codec_init( this, index, properties ) )
1774 // Set the frame properties
1775 if ( index < INT_MAX )
1777 mlt_properties_set_int( frame_properties, "frequency", this->audio_codec[ index ]->sample_rate );
1778 mlt_properties_set_int( frame_properties, "channels", this->audio_codec[ index ]->channels );
1781 if ( context && index > -1 )
1783 // Add our audio operation
1784 mlt_frame_push_audio( frame, this );
1785 mlt_frame_push_audio( frame, producer_get_audio );
1789 /** Our get frame implementation.
1792 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1794 // Access the private data
1795 producer_avformat this = producer->child;
1797 // Create an empty frame
1798 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1800 // Update timecode on the frame we're creating
1801 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
1803 // Set the position of this producer
1804 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( producer ) );
1807 producer_set_up_video( this, *frame );
1810 producer_set_up_audio( this, *frame );
1812 // Calculate the next timecode
1813 mlt_producer_prepare_next( producer );
1818 static void producer_close( mlt_producer parent )
1821 producer_avformat this = parent->child;
1824 av_free( this->av_frame );
1826 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
1828 if ( this->audio_resample[i] )
1829 audio_resample_close( this->audio_resample[i] );
1830 mlt_pool_release( this->audio_buffer[i] );
1831 av_free( this->decode_buffer[i] );
1832 producer_codec_close( this->audio_codec[i] );
1834 producer_codec_close( this->video_codec );
1835 producer_format_close( this->dummy_context );
1836 producer_format_close( this->audio_format );
1837 producer_format_close( this->video_format );
1840 parent->close = NULL;
1841 mlt_producer_close( parent );