2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Author: Dan Dennedy <dan@dennedy.org>
6 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <framework/mlt_producer.h>
25 #include <framework/mlt_frame.h>
26 #include <framework/mlt_profile.h>
27 #include <framework/mlt_log.h>
28 #include <framework/mlt_deque.h>
29 #include <framework/mlt_factory.h>
30 #include <framework/mlt_cache.h>
32 // ffmpeg Header files
38 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
39 # include "audioconvert.h"
45 // System header files
51 #if LIBAVUTIL_VERSION_INT < (50<<16)
52 #define PIX_FMT_RGB32 PIX_FMT_RGBA32
53 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
56 #define POSITION_INITIAL (-2)
57 #define POSITION_INVALID (-1)
59 #define MAX_AUDIO_STREAMS (10)
60 #define MAX_VDPAU_SURFACES (10)
62 void avformat_lock( );
63 void avformat_unlock( );
65 struct producer_avformat_s
68 AVFormatContext *dummy_context;
69 AVFormatContext *audio_format;
70 AVFormatContext *video_format;
71 AVCodecContext *audio_codec[ MAX_AUDIO_STREAMS ];
72 AVCodecContext *video_codec;
74 ReSampleContext *audio_resample[ MAX_AUDIO_STREAMS ];
75 mlt_position audio_expected;
76 mlt_position video_expected;
86 int16_t *audio_buffer[ MAX_AUDIO_STREAMS ];
87 size_t audio_buffer_size[ MAX_AUDIO_STREAMS ];
88 int16_t *decode_buffer[ MAX_AUDIO_STREAMS ];
89 int audio_used[ MAX_AUDIO_STREAMS ];
95 unsigned int invalid_pts_counter;
96 double resample_factor;
97 mlt_cache image_cache;
103 struct vdpau_render_state render_states[MAX_VDPAU_SURFACES];
114 typedef struct producer_avformat_s *producer_avformat;
116 // Forward references.
117 static int producer_open( producer_avformat this, mlt_profile profile, char *file );
118 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
119 static void producer_avformat_close( producer_avformat );
120 static void producer_close( mlt_producer parent );
126 /** Constructor for libavformat.
129 mlt_producer producer_avformat_init( mlt_profile profile, const char *service, char *file )
133 // Report information about available demuxers and codecs as YAML Tiny
134 if ( file && strstr( file, "f-list" ) )
136 fprintf( stderr, "---\nformats:\n" );
137 AVInputFormat *format = NULL;
138 while ( ( format = av_iformat_next( format ) ) )
139 fprintf( stderr, " - %s\n", format->name );
140 fprintf( stderr, "...\n" );
143 if ( file && strstr( file, "acodec-list" ) )
145 fprintf( stderr, "---\naudio_codecs:\n" );
146 AVCodec *codec = NULL;
147 while ( ( codec = av_codec_next( codec ) ) )
148 if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
149 fprintf( stderr, " - %s\n", codec->name );
150 fprintf( stderr, "...\n" );
153 if ( file && strstr( file, "vcodec-list" ) )
155 fprintf( stderr, "---\nvideo_codecs:\n" );
156 AVCodec *codec = NULL;
157 while ( ( codec = av_codec_next( codec ) ) )
158 if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
159 fprintf( stderr, " - %s\n", codec->name );
160 fprintf( stderr, "...\n" );
164 // Check that we have a non-NULL argument
167 // Construct the producer
168 mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
169 producer_avformat this = calloc( 1, sizeof( struct producer_avformat_s ) );
172 if ( mlt_producer_init( producer, this ) == 0 )
174 this->parent = producer;
176 // Get the properties
177 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
179 // Set the resource property (required for all producers)
180 mlt_properties_set( properties, "resource", file );
182 // Register transport implementation with the producer
183 producer->close = (mlt_destructor) producer_close;
185 // Register our get_frame implementation
186 producer->get_frame = producer_get_frame;
188 if ( strcmp( service, "avformat-novalidate" ) )
191 if ( producer_open( this, profile, file ) != 0 )
194 mlt_producer_close( producer );
199 // Close the file to release resources for large playlists - reopen later as needed
201 if ( this->dummy_context )
202 av_close_input_file( this->dummy_context );
203 this->dummy_context = NULL;
204 if ( this->audio_format )
205 av_close_input_file( this->audio_format );
206 this->audio_format = NULL;
207 if ( this->video_format )
208 av_close_input_file( this->video_format );
209 this->video_format = NULL;
212 // Default the user-selectable indices from the auto-detected indices
213 mlt_properties_set_int( properties, "audio_index", this->audio_index );
214 mlt_properties_set_int( properties, "video_index", this->video_index );
217 mlt_service_cache_set_size( MLT_PRODUCER_SERVICE(producer), "producer_avformat", 5 );
219 mlt_service_cache_put( MLT_PRODUCER_SERVICE(producer), "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
225 mlt_service_cache_set_size( MLT_PRODUCER_SERVICE(producer), "producer_avformat", 5 );
227 mlt_service_cache_put( MLT_PRODUCER_SERVICE(producer), "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
235 /** Find the default streams.
238 static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatContext *context, int *audio_index, int *video_index )
243 mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
245 // Allow for multiple audio and video streams in the file and select first of each (if available)
246 for( i = 0; i < context->nb_streams; i++ )
248 // Get the codec context
249 AVStream *stream = context->streams[ i ];
250 if ( ! stream ) continue;
251 AVCodecContext *codec_context = stream->codec;
252 if ( ! codec_context ) continue;
253 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
254 if ( ! codec ) continue;
256 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
258 // Determine the type and obtain the first index of each type
259 switch( codec_context->codec_type )
261 case CODEC_TYPE_VIDEO:
262 if ( *video_index < 0 )
264 mlt_properties_set( meta_media, key, "video" );
265 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
266 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
267 double ffmpeg_fps = av_q2d( context->streams[ i ]->avg_frame_rate );
268 if (ffmpeg_fps == 0) ffmpeg_fps = av_q2d( context->streams[ i ]->r_frame_rate );
269 mlt_properties_set_double( meta_media, key, ffmpeg_fps );
271 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
274 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
275 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
276 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
278 snprintf( key, sizeof(key), "meta.media.%d.codec.frame_rate", i );
279 mlt_properties_set_double( meta_media, key, (double) codec_context->time_base.den /
280 ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num ) );
281 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
282 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
283 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
284 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
285 snprintf( key, sizeof(key), "meta.media.%d.codec.colorspace", i );
286 mlt_properties_set_int( meta_media, key, codec_context->colorspace );
288 case CODEC_TYPE_AUDIO:
289 if ( *audio_index < 0 )
291 mlt_properties_set( meta_media, key, "audio" );
292 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
293 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
294 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
296 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
297 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
298 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
299 mlt_properties_set_int( meta_media, key, codec_context->channels );
304 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
305 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
306 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
307 mlt_properties_set( meta_media, key, codec->name );
308 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
309 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
310 mlt_properties_set( meta_media, key, codec->long_name );
312 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
313 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
314 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
315 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
316 // snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
317 // mlt_properties_set_int( meta_media, key, codec_context->profile );
318 // snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
319 // mlt_properties_set_int( meta_media, key, codec_context->level );
325 static inline int dv_is_pal( AVPacket *pkt )
327 return pkt->data[3] & 0x80;
330 static int dv_is_wide( AVPacket *pkt )
332 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
334 for ( ; i < pkt->size; i += 5 /* packet size */ )
336 if ( pkt->data[ i ] == 0x61 )
338 uint8_t x = pkt->data[ i + 2 ] & 0x7;
339 return ( x == 2 ) || ( x == 7 );
345 static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
347 double aspect_ratio = 1.0;
349 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
353 if ( dv_is_pal( pkt ) )
355 aspect_ratio = dv_is_wide( pkt )
356 ? 64.0/45.0 // 16:9 PAL
357 : 16.0/15.0; // 4:3 PAL
361 aspect_ratio = dv_is_wide( pkt )
362 ? 32.0/27.0 // 16:9 NTSC
363 : 8.0/9.0; // 4:3 NTSC
369 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
370 stream->sample_aspect_ratio;
372 codec_context->sample_aspect_ratio;
374 // Override FFmpeg's notion of DV aspect ratios, which are
375 // based upon a width of 704. Since we do not have a normaliser
376 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
377 // we just coerce the values to facilitate a passive behaviour through
378 // the rescale normaliser when using equivalent producers and consumers.
379 // = display_aspect / (width * height)
380 if ( ar.num == 10 && ar.den == 11 )
381 aspect_ratio = 8.0/9.0; // 4:3 NTSC
382 else if ( ar.num == 59 && ar.den == 54 )
383 aspect_ratio = 16.0/15.0; // 4:3 PAL
384 else if ( ar.num == 40 && ar.den == 33 )
385 aspect_ratio = 32.0/27.0; // 16:9 NTSC
386 else if ( ar.num == 118 && ar.den == 81 )
387 aspect_ratio = 64.0/45.0; // 16:9 PAL
392 AVRational codec_sar = codec_context->sample_aspect_ratio;
393 AVRational stream_sar =
394 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
395 stream->sample_aspect_ratio;
399 if ( codec_sar.num > 0 )
400 aspect_ratio = av_q2d( codec_sar );
401 else if ( stream_sar.num > 0 )
402 aspect_ratio = av_q2d( stream_sar );
410 static int producer_open( producer_avformat this, mlt_profile profile, char *file )
412 // Return an error code (0 == no error)
415 // Context for avformat
416 AVFormatContext *context = NULL;
418 // Get the properties
419 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this->parent );
421 // We will treat everything with the producer fps
422 double fps = mlt_profile_fps( profile );
424 // Lock the mutex now
427 // If "MRL", then create AVInputFormat
428 AVInputFormat *format = NULL;
429 AVFormatParameters *params = NULL;
430 char *standard = NULL;
431 char *mrl = strchr( file, ':' );
433 // AV option (0 = both, 1 = video, 2 = audio)
436 // Only if there is not a protocol specification that avformat can handle
437 if ( mrl && !url_exist( file ) )
439 // 'file' becomes format abbreviation
443 format = av_find_input_format( file );
445 // Eat the format designator
451 params = calloc( sizeof( AVFormatParameters ), 1 );
453 // These are required by video4linux (defaults)
455 params->height = 480;
456 params->time_base= (AVRational){1,25};
457 // params->device = file;
458 params->channels = 2;
459 params->sample_rate = 48000;
463 mrl = strchr( file, '?' );
467 char *name = strdup( ++mrl );
468 char *value = strchr( name, ':' );
473 char *t = strchr( value, '&' );
476 if ( !strcmp( name, "frame_rate" ) )
477 params->time_base.den = atoi( value );
478 else if ( !strcmp( name, "frame_rate_base" ) )
479 params->time_base.num = atoi( value );
480 else if ( !strcmp( name, "sample_rate" ) )
481 params->sample_rate = atoi( value );
482 else if ( !strcmp( name, "channels" ) )
483 params->channels = atoi( value );
484 else if ( !strcmp( name, "width" ) )
485 params->width = atoi( value );
486 else if ( !strcmp( name, "height" ) )
487 params->height = atoi( value );
488 else if ( !strcmp( name, "standard" ) )
490 standard = strdup( value );
491 params->standard = standard;
493 else if ( !strcmp( name, "av" ) )
497 mrl = strchr( mrl, '&' );
501 // Now attempt to open the file
502 error = av_open_input_file( &context, file, format, 0, params ) < 0;
504 // Cleanup AVFormatParameters
508 // If successful, then try to get additional info
511 // Get the stream info
512 error = av_find_stream_info( context ) < 0;
514 // Continue if no error
517 // We will default to the first audio and video streams found
518 int audio_index = -1;
519 int video_index = -1;
521 // Now set properties where we can (use default unknowns if required)
522 if ( context->duration != AV_NOPTS_VALUE )
524 // This isn't going to be accurate for all formats
525 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
526 mlt_properties_set_position( properties, "out", frames - 1 );
527 mlt_properties_set_position( properties, "length", frames );
530 // Find default audio and video streams
531 find_default_streams( properties, context, &audio_index, &video_index );
533 if ( context->start_time != AV_NOPTS_VALUE )
534 this->start_time = context->start_time;
536 // Check if we're seekable (something funny about mpeg here :-/)
537 if ( strncmp( file, "pipe:", 5 ) &&
538 strncmp( file, "/dev/", 5 ) &&
539 strncmp( file, "http:", 5 ) &&
540 strncmp( file, "udp:", 4 ) &&
541 strncmp( file, "tcp:", 4 ) &&
542 strncmp( file, "rtsp:", 5 ) &&
543 strncmp( file, "rtp:", 4 ) )
545 this->seekable = av_seek_frame( context, -1, this->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
546 mlt_properties_set_int( properties, "seekable", this->seekable );
547 this->dummy_context = context;
548 av_open_input_file( &context, file, NULL, 0, NULL );
549 av_find_stream_info( context );
552 // Store selected audio and video indexes on properties
553 this->audio_index = audio_index;
554 this->video_index = video_index;
555 this->first_pts = -1;
556 this->last_position = POSITION_INITIAL;
558 // Fetch the width, height and aspect ratio
559 if ( video_index != -1 )
561 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
562 mlt_properties_set_int( properties, "width", codec_context->width );
563 mlt_properties_set_int( properties, "height", codec_context->height );
565 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
567 // Fetch the first frame of DV so we can read it directly
572 ret = av_read_frame( context, &pkt );
573 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
575 mlt_properties_set_double( properties, "aspect_ratio",
576 get_aspect_ratio( context->streams[ video_index ], codec_context, &pkt ) );
583 mlt_properties_set_double( properties, "aspect_ratio",
584 get_aspect_ratio( context->streams[ video_index ], codec_context, NULL ) );
586 struct SwsContext *context = sws_getContext( codec_context->width, codec_context->height, codec_context->pix_fmt,
587 codec_context->width, codec_context->height, PIX_FMT_YUYV422, SWS_BILINEAR, NULL, NULL, NULL);
589 sws_freeContext( context );
595 if ( context->title )
596 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
597 if ( context->author )
598 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
599 if ( context->copyright )
600 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
601 if ( context->comment )
602 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
603 if ( context->album )
604 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
606 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
607 if ( context->track )
608 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
610 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
611 if ( av == 0 && audio_index != -1 && video_index != -1 )
613 // We'll use the open one as our video_format
614 this->video_format = context;
616 // And open again for our audio context
617 av_open_input_file( &context, file, NULL, 0, NULL );
618 av_find_stream_info( context );
621 this->audio_format = context;
623 else if ( av != 2 && video_index != -1 )
625 // We only have a video context
626 this->video_format = context;
628 else if ( audio_index != -1 )
630 // We only have an audio context
631 this->audio_format = context;
635 // Something has gone wrong
641 // Unlock the mutex now
647 /** Convert a frame position to a time code.
650 static double producer_time_of_frame( mlt_producer this, mlt_position position )
652 return ( double )position / mlt_producer_get_fps( this );
655 // Collect information about all audio streams
657 static void get_audio_streams_info( producer_avformat this )
659 // Fetch the audio format context
660 AVFormatContext *context = this->audio_format;
664 i < context->nb_streams;
667 if ( context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
669 AVCodecContext *codec_context = context->streams[i]->codec;
670 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
672 // If we don't have a codec and we can't initialise it, we can't do much more...
674 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
676 this->audio_streams++;
677 this->audio_max_stream = i;
678 this->total_channels += codec_context->channels;
679 if ( codec_context->channels > this->max_channel )
680 this->max_channel = codec_context->channels;
681 if ( codec_context->sample_rate > this->max_frequency )
682 this->max_frequency = codec_context->sample_rate;
683 avcodec_close( codec_context );
688 mlt_log_verbose( NULL, "[producer avformat] audio: total_streams %d max_stream %d total_channels %d max_channels %d\n",
689 this->audio_streams, this->audio_max_stream, this->total_channels, this->max_channel );
691 // Other audio-specific initializations
692 this->resample_factor = 1.0;
695 static void set_luma_transfer( struct SwsContext *context, int colorspace, int no_scale )
699 int brightness, contrast, saturation;
701 if ( sws_getColorspaceDetails( context, &coefficients, &range, &coefficients, &range,
702 &brightness, &contrast, &saturation ) != -1 )
704 // Don't change these from defaults unless explicitly told to.
707 switch ( colorspace )
713 coefficients = sws_getCoefficients( SWS_CS_ITU601 );
716 coefficients = sws_getCoefficients( SWS_CS_SMPTE240M );
719 coefficients = sws_getCoefficients( SWS_CS_ITU709 );
722 sws_setColorspaceDetails( context, coefficients, range, coefficients, range,
723 brightness, contrast, saturation );
727 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
728 mlt_image_format *format, int width, int height, int colorspace )
732 int flags = SWS_BILINEAR | SWS_ACCURATE_RND;
735 flags |= SWS_CPU_CAPS_MMX;
738 flags |= SWS_CPU_CAPS_MMX2;
741 if ( pix_fmt == PIX_FMT_RGB32 )
743 *format = mlt_image_rgb24a;
744 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
745 width, height, PIX_FMT_RGBA, flags, NULL, NULL, NULL);
747 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
748 set_luma_transfer( context, colorspace, luma );
749 sws_scale( context, frame->data, frame->linesize, 0, height,
750 output.data, output.linesize);
751 sws_freeContext( context );
753 else if ( *format == mlt_image_yuv420p )
755 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
756 width, height, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
758 output.data[0] = buffer;
759 output.data[1] = buffer + width * height;
760 output.data[2] = buffer + ( 5 * width * height ) / 4;
761 output.linesize[0] = width;
762 output.linesize[1] = width >> 1;
763 output.linesize[2] = width >> 1;
764 set_luma_transfer( context, colorspace, luma );
765 sws_scale( context, frame->data, frame->linesize, 0, height,
766 output.data, output.linesize);
767 sws_freeContext( context );
769 else if ( *format == mlt_image_rgb24 )
771 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
772 width, height, PIX_FMT_RGB24, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
774 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
775 set_luma_transfer( context, colorspace, luma );
776 sws_scale( context, frame->data, frame->linesize, 0, height,
777 output.data, output.linesize);
778 sws_freeContext( context );
780 else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
782 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
783 width, height, PIX_FMT_RGBA, flags | SWS_FULL_CHR_H_INT, NULL, NULL, NULL);
785 avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
786 set_luma_transfer( context, colorspace, luma );
787 sws_scale( context, frame->data, frame->linesize, 0, height,
788 output.data, output.linesize);
789 sws_freeContext( context );
793 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
794 width, height, PIX_FMT_YUYV422, flags | SWS_FULL_CHR_H_INP, NULL, NULL, NULL);
796 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
797 set_luma_transfer( context, colorspace, luma );
798 sws_scale( context, frame->data, frame->linesize, 0, height,
799 output.data, output.linesize);
800 sws_freeContext( context );
803 if ( *format == mlt_image_yuv420p )
806 pict.data[0] = buffer;
807 pict.data[1] = buffer + width * height;
808 pict.data[2] = buffer + ( 5 * width * height ) / 4;
809 pict.linesize[0] = width;
810 pict.linesize[1] = width >> 1;
811 pict.linesize[2] = width >> 1;
812 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
814 else if ( *format == mlt_image_rgb24 )
817 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
818 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
820 else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
823 avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
824 img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
829 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
830 img_convert( &output, PIX_FMT_YUYV422, (AVPicture *)frame, pix_fmt, width, height );
835 /** Allocate the image buffer and set it on the frame.
838 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
842 if ( codec_context->width == 0 || codec_context->height == 0 )
845 *width = codec_context->width;
846 *height = codec_context->height;
848 if ( codec_context->pix_fmt == PIX_FMT_RGB32 )
849 size = *width * ( *height + 1 ) * 4;
850 else switch ( *format )
852 case mlt_image_yuv420p:
853 size = *width * 3 * ( *height + 1 ) / 2;
855 case mlt_image_rgb24:
856 size = *width * ( *height + 1 ) * 3;
858 case mlt_image_rgb24a:
859 case mlt_image_opengl:
860 size = *width * ( *height + 1 ) * 4;
863 *format = mlt_image_yuv422;
864 size = *width * ( *height + 1 ) * 2;
868 // Construct the output image
869 *buffer = mlt_pool_alloc( size );
871 mlt_properties_set_data( frame_properties, "image", *buffer, size, mlt_pool_release, NULL );
878 /** Get an image from a frame.
881 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
884 producer_avformat this = mlt_frame_pop_service( frame );
885 mlt_producer producer = this->parent;
887 // Get the properties from the frame
888 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
890 // Obtain the frame number of this frame
891 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
893 // Get the producer properties
894 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
896 // Fetch the video format context
897 AVFormatContext *context = this->video_format;
899 // Get the video stream
900 AVStream *stream = context->streams[ this->video_index ];
903 AVCodecContext *codec_context = stream->codec;
905 // Get the image cache
906 if ( ! this->image_cache && ! mlt_properties_get_int( properties, "noimagecache" ) )
907 this->image_cache = mlt_cache_init();
908 if ( this->image_cache )
910 mlt_cache_item item = mlt_cache_get( this->image_cache, (void*) position );
911 *buffer = mlt_cache_item_data( item, (int*) format );
914 // Set the resolution
915 *width = codec_context->width;
916 *height = codec_context->height;
918 // Workaround 1088 encodings missing cropping info.
919 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
926 case mlt_image_yuv420p:
927 size = *width * 3 * ( *height + 1 ) / 2;
929 case mlt_image_rgb24:
930 size = *width * ( *height + 1 ) * 3;
932 case mlt_image_rgb24a:
933 case mlt_image_opengl:
934 size = *width * ( *height + 1 ) * 4;
937 *format = mlt_image_yuv422;
938 size = *width * ( *height + 1 ) * 2;
941 mlt_properties_set_data( frame_properties, "avformat.image_cache", item, 0, ( mlt_destructor )mlt_cache_item_close, NULL );
942 mlt_properties_set_data( frame_properties, "image", *buffer, size, NULL, NULL );
943 // this->top_field_first = mlt_properties_get_int( frame_properties, "top_field_first" );
944 this->got_picture = 1;
957 // Special case pause handling flag
960 // Special case ffwd handling
963 // We may want to use the source fps if available
964 double source_fps = mlt_properties_get_double( properties, "source_fps" );
965 double fps = mlt_producer_get_fps( producer );
967 // This is the physical frame position in the source
968 int req_position = ( int )( position / fps * source_fps + 0.5 );
970 // Determines if we have to decode all frames in a sequence
971 // Temporary hack to improve intra frame only
972 int must_decode = strcmp( codec_context->codec->name, "dnxhd" ) &&
973 strcmp( codec_context->codec->name, "dvvideo" ) &&
974 strcmp( codec_context->codec->name, "huffyuv" ) &&
975 strcmp( codec_context->codec->name, "mjpeg" ) &&
976 strcmp( codec_context->codec->name, "rawvideo" );
978 int last_position = this->last_position;
980 // Turn on usage of new seek API and PTS for seeking
981 int use_new_seek = codec_context->codec_id == CODEC_ID_H264 && !strcmp( context->iformat->name, "mpegts" );
982 if ( mlt_properties_get( properties, "new_seek" ) )
983 use_new_seek = mlt_properties_get_int( properties, "new_seek" );
986 if ( position != this->video_expected || last_position < 0 )
988 if ( this->av_frame && position + 1 == this->video_expected )
990 // We're paused - use last image
993 else if ( !this->seekable && position > this->video_expected && ( position - this->video_expected ) < 250 )
995 // Fast forward - seeking is inefficient for small distances - just ignore following frames
996 ignore = ( int )( ( position - this->video_expected ) / fps * source_fps );
997 codec_context->skip_loop_filter = AVDISCARD_NONREF;
999 else if ( this->seekable && ( position < this->video_expected || position - this->video_expected >= 12 || last_position < 0 ) )
1001 if ( use_new_seek && last_position == POSITION_INITIAL )
1003 // find first key frame
1007 while ( ret >= 0 && toscan-- > 0 )
1009 ret = av_read_frame( context, &pkt );
1010 if ( ret >= 0 && ( pkt.flags & PKT_FLAG_KEY ) && pkt.stream_index == this->video_index )
1012 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) );
1013 this->first_pts = pkt.pts;
1016 av_free_packet( &pkt );
1019 av_seek_frame( context, -1, 0, AVSEEK_FLAG_BACKWARD );
1022 // Calculate the timestamp for the requested frame
1026 timestamp = ( req_position - 0.1 / source_fps ) /
1027 ( av_q2d( stream->time_base ) * source_fps );
1028 mlt_log_verbose( MLT_PRODUCER_SERVICE(producer), "pos %d pts %lld ", req_position, timestamp );
1029 if ( this->first_pts > 0 )
1030 timestamp += this->first_pts;
1031 else if ( context->start_time != AV_NOPTS_VALUE )
1032 timestamp += context->start_time;
1036 timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
1037 if ( context->start_time != AV_NOPTS_VALUE )
1038 timestamp += context->start_time;
1041 timestamp -= AV_TIME_BASE;
1042 if ( timestamp < 0 )
1044 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "seeking timestamp %lld position %d expected %d last_pos %d\n",
1045 timestamp, position, this->video_expected, last_position );
1047 // Seek to the timestamp
1050 codec_context->skip_loop_filter = AVDISCARD_NONREF;
1051 av_seek_frame( context, this->video_index, timestamp, AVSEEK_FLAG_BACKWARD );
1055 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
1058 // Remove the cached info relating to the previous position
1059 this->current_position = POSITION_INVALID;
1060 this->last_position = POSITION_INVALID;
1061 av_freep( &this->av_frame );
1065 // flush any pictures still in decode buffer
1066 avcodec_flush_buffers( codec_context );
1071 // Duplicate the last image if necessary (see comment on rawvideo below)
1072 if ( this->av_frame && this->av_frame->linesize[0] && this->got_picture && this->seekable
1074 || this->current_position == req_position
1075 || ( !use_new_seek && this->current_position > req_position ) ) )
1078 if ( ( image_size = allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) ) )
1080 // Workaround 1088 encodings missing cropping info.
1081 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1084 if ( this->vdpau && this->vdpau->buffer )
1087 picture.data[0] = this->vdpau->buffer;
1088 picture.data[2] = this->vdpau->buffer + codec_context->width * codec_context->height;
1089 picture.data[1] = this->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1090 picture.linesize[0] = codec_context->width;
1091 picture.linesize[1] = codec_context->width / 2;
1092 picture.linesize[2] = codec_context->width / 2;
1093 convert_image( (AVFrame*) &picture, *buffer,
1094 PIX_FMT_YUV420P, format, *width, *height, this->colorspace );
1098 convert_image( this->av_frame, *buffer, codec_context->pix_fmt,
1099 format, *width, *height, this->colorspace );
1102 mlt_frame_get_image( frame, buffer, format, width, height, writable );
1107 int int_position = 0;
1108 int decode_errors = 0;
1109 int got_picture = 0;
1111 av_init_packet( &pkt );
1113 // Construct an AVFrame for YUV422 conversion
1114 if ( !this->av_frame )
1115 this->av_frame = avcodec_alloc_frame( );
1117 while( ret >= 0 && !got_picture )
1120 ret = av_read_frame( context, &pkt );
1122 // We only deal with video from the selected video_index
1123 if ( ret >= 0 && pkt.stream_index == this->video_index && pkt.size > 0 )
1125 // Determine time code of the packet
1128 int64_t pts = pkt.pts;
1129 if ( this->first_pts > 0 )
1130 pts -= this->first_pts;
1131 else if ( context->start_time != AV_NOPTS_VALUE )
1132 pts -= context->start_time;
1133 int_position = ( int )( av_q2d( stream->time_base ) * pts * source_fps + 0.1 );
1134 if ( pkt.pts == AV_NOPTS_VALUE )
1136 this->invalid_pts_counter++;
1137 if ( this->invalid_pts_counter > 20 )
1139 mlt_log_panic( MLT_PRODUCER_SERVICE(producer), "\ainvalid PTS; DISABLING NEW_SEEK!\n" );
1140 mlt_properties_set_int( properties, "new_seek", 0 );
1141 int_position = req_position;
1147 this->invalid_pts_counter = 0;
1149 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.pts %llu req_pos %d cur_pos %d pkt_pos %d\n",
1150 pkt.pts, req_position, this->current_position, int_position );
1154 if ( pkt.dts != AV_NOPTS_VALUE )
1156 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
1157 if ( context->start_time != AV_NOPTS_VALUE )
1158 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
1159 last_position = this->last_position;
1160 if ( int_position == last_position )
1161 int_position = last_position + 1;
1165 int_position = req_position;
1167 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d\n",
1168 pkt.dts, req_position, this->current_position, int_position );
1169 // Make a dumb assumption on streams that contain wild timestamps
1170 if ( abs( req_position - int_position ) > 999 )
1172 int_position = req_position;
1173 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " WILD TIMESTAMP!" );
1176 this->last_position = int_position;
1179 if ( must_decode || int_position >= req_position )
1182 if ( g_vdpau && this->vdpau )
1184 if ( g_vdpau->producer != this )
1186 vdpau_decoder_close();
1187 vdpau_decoder_init( this );
1190 this->vdpau->is_decoded = 0;
1193 codec_context->reordered_opaque = pkt.pts;
1194 if ( int_position >= req_position )
1195 codec_context->skip_loop_filter = AVDISCARD_NONE;
1196 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1197 ret = avcodec_decode_video2( codec_context, this->av_frame, &got_picture, &pkt );
1199 ret = avcodec_decode_video( codec_context, this->av_frame, &got_picture, pkt.data, pkt.size );
1201 // Note: decode may fail at the beginning of MPEGfile (B-frames referencing before first I-frame), so allow a few errors.
1204 if ( ++decode_errors <= 10 )
1217 // Determine time code of the packet
1218 int64_t pts = this->av_frame->reordered_opaque;
1219 if ( this->first_pts > 0 )
1220 pts -= this->first_pts;
1221 else if ( context->start_time != AV_NOPTS_VALUE )
1222 pts -= context->start_time;
1223 int_position = ( int )( av_q2d( stream->time_base) * pts * source_fps + 0.1 );
1224 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), "got frame %d, key %d\n", int_position, this->av_frame->key_frame );
1227 if ( int_position < req_position )
1232 else if ( int_position >= req_position )
1235 codec_context->skip_loop_filter = AVDISCARD_NONE;
1237 else if ( ignore -- )
1242 mlt_log_debug( MLT_PRODUCER_SERVICE(producer), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
1243 av_free_packet( &pkt );
1245 else if ( ret >= 0 )
1247 av_free_packet( &pkt );
1250 // Now handle the picture if we have one
1253 if ( ( image_size = allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) ) )
1255 // Workaround 1088 encodings missing cropping info.
1256 if ( *height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1261 if ( this->vdpau->is_decoded )
1263 struct vdpau_render_state *render = (struct vdpau_render_state*) this->av_frame->data[0];
1265 uint32_t pitches[3];
1266 VdpYCbCrFormat dest_format = VDP_YCBCR_FORMAT_YV12;
1269 if ( !this->vdpau->buffer )
1270 this->vdpau->buffer = mlt_pool_alloc( codec_context->width * codec_context->height * 3 / 2 );
1271 picture.data[0] = planes[0] = this->vdpau->buffer;
1272 picture.data[2] = planes[1] = this->vdpau->buffer + codec_context->width * codec_context->height;
1273 picture.data[1] = planes[2] = this->vdpau->buffer + codec_context->width * codec_context->height * 5 / 4;
1274 picture.linesize[0] = pitches[0] = codec_context->width;
1275 picture.linesize[1] = pitches[1] = codec_context->width / 2;
1276 picture.linesize[2] = pitches[2] = codec_context->width / 2;
1278 VdpStatus status = vdp_surface_get_bits( render->surface, dest_format, planes, pitches );
1279 if ( status == VDP_STATUS_OK )
1281 convert_image( (AVFrame*) &picture, *buffer, PIX_FMT_YUV420P,
1282 format, *width, *height, this->colorspace );
1286 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU Error: %s\n", vdp_get_error_string( status ) );
1287 this->vdpau->is_decoded = 0;
1292 mlt_log_error( MLT_PRODUCER_SERVICE(producer), "VDPAU error in VdpDecoderRender\n" );
1298 convert_image( this->av_frame, *buffer, codec_context->pix_fmt,
1299 format, *width, *height, this->colorspace );
1300 this->top_field_first |= this->av_frame->top_field_first;
1301 this->current_position = int_position;
1302 this->got_picture = 1;
1314 if ( this->got_picture && image_size > 0 && this->image_cache )
1316 // Copy buffer to image cache
1317 uint8_t *image = mlt_pool_alloc( image_size );
1318 memcpy( image, *buffer, image_size );
1319 mlt_cache_put( this->image_cache, (void*) position, image, *format, mlt_pool_release );
1322 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
1323 this->video_expected = position + 1;
1326 // Set the progressive flag
1327 if ( mlt_properties_get( properties, "force_progressive" ) )
1328 mlt_properties_set_int( frame_properties, "progressive", !!mlt_properties_get_int( properties, "force_progressive" ) );
1329 else if ( this->av_frame )
1330 mlt_properties_set_int( frame_properties, "progressive", !this->av_frame->interlaced_frame );
1332 // Set the field order property for this frame
1333 if ( mlt_properties_get( properties, "force_tff" ) )
1334 mlt_properties_set_int( frame_properties, "top_field_first", !!mlt_properties_get_int( properties, "force_tff" ) );
1336 mlt_properties_set_int( frame_properties, "top_field_first", this->top_field_first );
1338 return !this->got_picture;
1341 /** Process properties as AVOptions and apply to AV context obj
1344 static void apply_properties( void *obj, mlt_properties properties, int flags )
1347 int count = mlt_properties_count( properties );
1348 for ( i = 0; i < count; i++ )
1350 const char *opt_name = mlt_properties_get_name( properties, i );
1351 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
1352 if ( opt_name && mlt_properties_get( properties, opt_name ) )
1355 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
1356 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
1357 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
1358 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
1360 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
1366 /** Initialize the video codec context.
1369 static int video_codec_init( producer_avformat this, int index, mlt_properties properties )
1371 // Initialise the codec if necessary
1372 if ( !this->video_codec )
1374 // Get the video stream
1375 AVStream *stream = this->video_format->streams[ index ];
1377 // Get codec context
1378 AVCodecContext *codec_context = stream->codec;
1381 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1383 if ( codec_context->codec_id == CODEC_ID_H264 )
1385 if ( ( codec = avcodec_find_decoder_by_name( "h264_vdpau" ) ) )
1387 if ( vdpau_init( this ) )
1389 this->video_codec = codec_context;
1390 if ( !vdpau_decoder_init( this ) )
1391 vdpau_decoder_close();
1395 codec = avcodec_find_decoder( codec_context->codec_id );
1399 // Initialise multi-threading
1400 int thread_count = mlt_properties_get_int( properties, "threads" );
1401 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
1402 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
1403 if ( thread_count > 1 )
1405 avcodec_thread_init( codec_context, thread_count );
1406 codec_context->thread_count = thread_count;
1409 // If we don't have a codec and we can't initialise it, we can't do much more...
1411 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1413 // Now store the codec with its destructor
1414 this->video_codec = codec_context;
1418 // Remember that we can't use this later
1419 this->video_index = -1;
1423 // Process properties as AVOptions
1424 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1426 // Reset some image properties
1427 mlt_properties_set_int( properties, "width", this->video_codec->width );
1428 mlt_properties_set_int( properties, "height", this->video_codec->height );
1429 // For DV, we'll just use the saved aspect ratio
1430 if ( codec_context->codec_id != CODEC_ID_DVVIDEO )
1431 mlt_properties_set_double( properties, "aspect_ratio", get_aspect_ratio( stream, this->video_codec, NULL ) );
1433 // Determine the fps first from the codec
1434 double source_fps = (double) this->video_codec->time_base.den /
1435 ( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
1437 if ( mlt_properties_get( properties, "force_fps" ) )
1439 source_fps = mlt_properties_get_double( properties, "force_fps" );
1440 stream->time_base = av_d2q( source_fps, 255 );
1444 // If the muxer reports a frame rate different than the codec
1445 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(42<<8)+0)
1446 double muxer_fps = av_q2d( stream->avg_frame_rate );
1447 if (muxer_fps == 0) muxer_fps = av_q2d( stream->r_frame_rate );
1449 double muxer_fps = av_q2d( stream->r_frame_rate );
1451 // Choose the lesser - the wrong tends to be off by some multiple of 10
1452 source_fps = FFMIN( source_fps, muxer_fps );
1455 // We'll use fps if it's available
1456 if ( source_fps > 0 )
1457 mlt_properties_set_double( properties, "source_fps", source_fps );
1459 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this->parent ) );
1461 // Set the YUV colorspace from override or detect
1462 this->colorspace = mlt_properties_get_int( properties, "force_colorspace" );
1463 if ( ! this->colorspace )
1465 switch ( this->video_codec->colorspace )
1467 case AVCOL_SPC_SMPTE240M:
1468 this->colorspace = 240;
1470 case AVCOL_SPC_BT470BG:
1471 case AVCOL_SPC_SMPTE170M:
1472 this->colorspace = 601;
1474 case AVCOL_SPC_BT709:
1475 this->colorspace = 709;
1478 // This is a heuristic Charles Poynton suggests in "Digital Video and HDTV"
1479 this->colorspace = this->video_codec->width * this->video_codec->height > 750000 ? 709 : 601;
1483 // Let apps get chosen colorspace
1484 mlt_properties_set_int( properties, "colorspace", this->colorspace );
1486 return this->video_codec && this->video_index > -1;
1489 /** Set up video handling.
1492 static void producer_set_up_video( producer_avformat this, mlt_frame frame )
1495 mlt_producer producer = this->parent;
1497 // Get the properties
1498 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1500 // Fetch the video format context
1501 AVFormatContext *context = this->video_format;
1503 // Get the video_index
1504 int index = mlt_properties_get_int( properties, "video_index" );
1506 // Reopen the file if necessary
1507 if ( !context && index > -1 )
1509 mlt_events_block( properties, producer );
1510 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
1511 mlt_properties_get( properties, "resource" ) );
1512 context = this->video_format;
1513 if ( this->dummy_context )
1516 av_close_input_file( this->dummy_context );
1519 this->dummy_context = NULL;
1520 mlt_events_unblock( properties, producer );
1521 if ( this->audio_format )
1522 get_audio_streams_info( this );
1524 // Process properties as AVOptions
1525 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
1528 // Exception handling for video_index
1529 if ( context && index >= (int) context->nb_streams )
1531 // Get the last video stream
1532 for ( index = context->nb_streams - 1;
1533 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO;
1535 mlt_properties_set_int( properties, "video_index", index );
1537 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
1539 // Invalidate the video stream
1541 mlt_properties_set_int( properties, "video_index", index );
1544 // Update the video properties if the index changed
1545 if ( index != this->video_index )
1547 // Reset the video properties if the index changed
1548 this->video_index = index;
1549 if ( this->video_codec )
1552 avcodec_close( this->video_codec );
1555 this->video_codec = NULL;
1558 // Get the frame properties
1559 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1562 if ( context && index > -1 && video_codec_init( this, index, properties ) )
1564 // Set the frame properties
1565 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1566 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1567 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1569 // Set the width and height
1570 mlt_properties_set_int( frame_properties, "width", this->video_codec->width );
1571 mlt_properties_set_int( frame_properties, "height", this->video_codec->height );
1572 mlt_properties_set_int( frame_properties, "real_width", this->video_codec->width );
1573 mlt_properties_set_int( frame_properties, "real_height", this->video_codec->height );
1574 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1575 mlt_properties_set_int( frame_properties, "colorspace", this->colorspace );
1577 // Workaround 1088 encodings missing cropping info.
1578 if ( this->video_codec->height == 1088 && mlt_profile_dar( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) ) == 16.0/9.0 )
1579 mlt_properties_set_int( frame_properties, "real_height", 1080 );
1581 // Add our image operation
1582 mlt_frame_push_service( frame, this );
1583 mlt_frame_push_get_image( frame, producer_get_image );
1587 // If something failed, use test card image
1588 mlt_properties_set_int( frame_properties, "test_image", 1 );
1592 static int seek_audio( producer_avformat this, mlt_position position, double timecode, int *ignore )
1596 // Seek if necessary
1597 if ( position != this->audio_expected )
1599 if ( position + 1 == this->audio_expected )
1601 // We're paused - silence required
1604 else if ( !this->seekable && position > this->audio_expected && ( position - this->audio_expected ) < 250 )
1606 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1607 *ignore = position - this->audio_expected;
1609 else if ( position < this->audio_expected || position - this->audio_expected >= 12 )
1611 AVFormatContext *context = this->audio_format;
1612 int64_t timestamp = ( int64_t )( timecode * AV_TIME_BASE + 0.5 );
1613 if ( context->start_time != AV_NOPTS_VALUE )
1614 timestamp += context->start_time;
1615 if ( timestamp < 0 )
1618 // Set to the real timecode
1619 if ( av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD ) != 0 )
1622 // Clear the usage in the audio buffer
1623 int i = MAX_AUDIO_STREAMS + 1;
1625 this->audio_used[i - 1] = 0;
1631 static int decode_audio( producer_avformat this, int *ignore, AVPacket pkt, int channels, int samples, double timecode, double fps )
1633 // Fetch the audio_format
1634 AVFormatContext *context = this->audio_format;
1636 // Get the current stream index
1637 int index = pkt.stream_index;
1639 // Get codec context
1640 AVCodecContext *codec_context = this->audio_codec[ index ];
1642 // Obtain the resample context if it exists (not always needed)
1643 ReSampleContext *resample = this->audio_resample[ index ];
1645 // Obtain the audio buffers
1646 int16_t *audio_buffer = this->audio_buffer[ index ];
1647 int16_t *decode_buffer = this->decode_buffer[ index ];
1649 int audio_used = this->audio_used[ index ];
1650 uint8_t *ptr = pkt.data;
1654 while ( ptr && ret >= 0 && len > 0 )
1656 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1659 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(26<<8)+0))
1660 ret = avcodec_decode_audio3( codec_context, decode_buffer, &data_size, &pkt );
1661 #elif (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1662 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1664 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1668 mlt_log_warning( MLT_PRODUCER_SERVICE(this->parent), "audio decoding error %d\n", ret );
1672 pkt.size = len -= ret;
1673 pkt.data = ptr += ret;
1675 // If decoded successfully
1676 if ( data_size > 0 )
1678 // Figure out how many samples will be needed after resampling
1679 int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
1680 int samples_needed = this->resample_factor * convert_samples + 1;
1682 // Resize audio buffer to prevent overflow
1683 if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
1685 this->audio_buffer_size[ index ] *= 2;
1686 audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
1690 // Copy to audio buffer while resampling
1691 int16_t *source = decode_buffer;
1692 int16_t *dest = &audio_buffer[ audio_used * channels ];
1693 audio_used += audio_resample( resample, dest, source, convert_samples );
1697 // Straight copy to audio buffer
1698 memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
1699 audio_used += convert_samples;
1703 while ( *ignore && audio_used > samples )
1706 audio_used -= samples;
1707 memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
1708 audio_used * sizeof( int16_t ) );
1713 // If we're behind, ignore this packet
1716 double current_pts = av_q2d( context->streams[ index ]->time_base ) * pkt.pts;
1717 int req_position = ( int )( timecode * fps + 0.5 );
1718 int int_position = ( int )( current_pts * fps + 0.5 );
1719 if ( context->start_time != AV_NOPTS_VALUE )
1720 int_position -= ( int )( fps * context->start_time / AV_TIME_BASE + 0.5 );
1722 if ( this->seekable && *ignore == 0 )
1724 if ( int_position < req_position )
1725 // We are behind, so skip some
1727 else if ( int_position > req_position + 2 )
1728 // We are ahead, so seek backwards some more
1729 seek_audio( this, req_position, timecode - 1.0, ignore );
1733 this->audio_used[ index ] = audio_used;
1738 /** Get the audio from a frame.
1741 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1744 producer_avformat this = mlt_frame_pop_audio( frame );
1746 // Obtain the frame number of this frame
1747 mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), "avformat_position" );
1749 // Calculate the real time code
1750 double real_timecode = producer_time_of_frame( this->parent, position );
1752 // Get the producer fps
1753 double fps = mlt_producer_get_fps( this->parent );
1755 // Number of frames to ignore (for ffwd)
1758 // Flag for paused (silence)
1759 int paused = seek_audio( this, position, real_timecode, &ignore );
1761 // Fetch the audio_format
1762 AVFormatContext *context = this->audio_format;
1764 // Determine the tracks to use
1765 int index = this->audio_index;
1766 int index_max = this->audio_index + 1;
1767 if ( this->audio_index == INT_MAX )
1770 index_max = context->nb_streams;
1771 *channels = this->total_channels;
1772 *frequency = this->max_frequency;
1775 // Initialize the resamplers and buffers
1776 for ( ; index < index_max; index++ )
1778 // Get codec context
1779 AVCodecContext *codec_context = this->audio_codec[ index ];
1781 if ( codec_context && !this->audio_buffer[ index ] )
1783 // Check for resample and create if necessary
1784 if ( codec_context->channels <= 2 )
1786 // Determine by how much resampling will increase number of samples
1787 double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
1788 resample_factor *= (double) *frequency / codec_context->sample_rate;
1789 if ( resample_factor > this->resample_factor )
1790 this->resample_factor = resample_factor;
1792 // Create the resampler
1793 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
1794 this->audio_resample[ index ] = av_audio_resample_init(
1795 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1796 codec_context->channels, *frequency, codec_context->sample_rate,
1797 SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
1799 this->audio_resample[ index ] = audio_resample_init(
1800 this->audio_index == INT_MAX ? codec_context->channels : *channels,
1801 codec_context->channels, *frequency, codec_context->sample_rate );
1806 codec_context->request_channels = this->audio_index == INT_MAX ? codec_context->channels : *channels;
1809 // Check for audio buffer and create if necessary
1810 this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1811 this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
1813 // Check for decoder buffer and create if necessary
1814 this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1818 // Get the audio if required
1825 av_init_packet( &pkt );
1827 // If not resampling, give consumer more than requested.
1828 // It requested number samples based on requested frame rate.
1829 // Do not clean this up with a samples *= ...!
1830 if ( this->audio_index != INT_MAX && ! this->audio_resample[ this->audio_index ] )
1831 *samples = *samples * this->audio_codec[ this->audio_index ]->sample_rate / *frequency;
1833 while ( ret >= 0 && !got_audio )
1835 // Check if the buffer already contains the samples required
1836 if ( this->audio_index != INT_MAX && this->audio_used[ this->audio_index ] >= *samples && ignore == 0 )
1843 ret = av_read_frame( context, &pkt );
1845 // We only deal with audio from the selected audio index
1846 if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
1847 ( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
1849 int channels2 = this->audio_index == INT_MAX ? this->audio_codec[pkt.stream_index]->channels : *channels;
1850 ret = decode_audio( this, &ignore, pkt, channels2, *samples, real_timecode, fps );
1852 av_free_packet( &pkt );
1854 if ( this->audio_index == INT_MAX && ret >= 0 )
1856 // Determine if there is enough audio for all streams
1858 for ( index = 0; index < context->nb_streams; index++ )
1860 if ( this->audio_codec[ index ] && this->audio_used[ index ] < *samples )
1866 // Allocate and set the frame's audio buffer
1867 int size = *samples * *channels * sizeof(int16_t);
1868 *buffer = mlt_pool_alloc( size );
1869 *format = mlt_audio_s16;
1870 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
1872 // Interleave tracks if audio_index=all
1873 if ( this->audio_index == INT_MAX )
1875 int16_t *dest = *buffer;
1877 for ( i = 0; i < *samples; i++ )
1879 for ( index = 0; index < index_max; index++ )
1880 if ( this->audio_codec[ index ] )
1882 int current_channels = this->audio_codec[ index ]->channels;
1883 int16_t *src = this->audio_buffer[ index ] + i * current_channels;
1884 memcpy( dest, src, current_channels * sizeof(int16_t) );
1885 dest += current_channels;
1888 for ( index = 0; index < index_max; index++ )
1889 if ( this->audio_codec[ index ] && this->audio_used[ index ] >= *samples )
1891 int current_channels = this->audio_codec[ index ]->channels;
1892 int16_t *src = this->audio_buffer[ index ] + *samples * current_channels;
1893 this->audio_used[index] -= *samples;
1894 memmove( this->audio_buffer[ index ], src, this->audio_used[ index ] * current_channels * sizeof(int16_t) );
1897 // Copy a single track to the output buffer
1900 index = this->audio_index;
1902 // Now handle the audio if we have enough
1903 if ( this->audio_used[ index ] > 0 )
1905 int16_t *src = this->audio_buffer[ index ];
1906 *samples = this->audio_used[ index ] < *samples ? this->audio_used[ index ] : *samples;
1907 memcpy( *buffer, src, *samples * *channels * sizeof(int16_t) );
1908 this->audio_used[ index ] -= *samples;
1909 memmove( src, &src[ *samples * *channels ], this->audio_used[ index ] * *channels * sizeof(int16_t) );
1913 // Otherwise fill with silence
1914 memset( *buffer, 0, *samples * *channels * sizeof(int16_t) );
1916 if ( !this->audio_resample[ index ] )
1918 // TODO: uncomment and remove following line when full multi-channel support is ready
1919 // *channels = codec_context->channels;
1920 *frequency = this->audio_codec[ index ]->sample_rate;
1926 // Get silence and don't touch the context
1927 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1930 // Regardless of speed (other than paused), we expect to get the next frame
1932 this->audio_expected = position + 1;
1937 /** Initialize the audio codec context.
1940 static int audio_codec_init( producer_avformat this, int index, mlt_properties properties )
1942 // Initialise the codec if necessary
1943 if ( !this->audio_codec[ index ] )
1945 // Get codec context
1946 AVCodecContext *codec_context = this->audio_format->streams[index]->codec;
1949 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
1951 // If we don't have a codec and we can't initialise it, we can't do much more...
1953 if ( codec && avcodec_open( codec_context, codec ) >= 0 )
1955 // Now store the codec with its destructor
1956 if ( this->audio_codec[ index ] )
1957 avcodec_close( this->audio_codec[ index ] );
1958 this->audio_codec[ index ] = codec_context;
1962 // Remember that we can't use this later
1963 this->audio_index = -1;
1967 // Process properties as AVOptions
1968 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1970 return this->audio_codec[ index ] && this->audio_index > -1;
1973 /** Set up audio handling.
1976 static void producer_set_up_audio( producer_avformat this, mlt_frame frame )
1979 mlt_producer producer = this->parent;
1981 // Get the properties
1982 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1984 // Fetch the audio format context
1985 AVFormatContext *context = this->audio_format;
1987 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1989 // Get the audio_index
1990 int index = mlt_properties_get_int( properties, "audio_index" );
1992 // Handle all audio tracks
1993 if ( this->audio_index > -1 &&
1994 mlt_properties_get( properties, "audio_index" ) &&
1995 !strcmp( mlt_properties_get( properties, "audio_index" ), "all" ) )
1998 // Reopen the file if necessary
1999 if ( !context && this->audio_index > -1 && index > -1 )
2001 mlt_events_block( properties, producer );
2002 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
2003 mlt_properties_get( properties, "resource" ) );
2004 context = this->audio_format;
2005 if ( this->dummy_context )
2008 av_close_input_file( this->dummy_context );
2011 this->dummy_context = NULL;
2012 mlt_events_unblock( properties, producer );
2013 get_audio_streams_info( this );
2016 // Exception handling for audio_index
2017 if ( context && index >= (int) context->nb_streams && index < INT_MAX )
2019 for ( index = context->nb_streams - 1;
2020 index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO;
2022 mlt_properties_set_int( properties, "audio_index", index );
2024 if ( context && index > -1 && index < INT_MAX &&
2025 context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
2027 index = this->audio_index;
2028 mlt_properties_set_int( properties, "audio_index", index );
2031 // Update the audio properties if the index changed
2032 if ( context && index > -1 && index != this->audio_index )
2034 if ( this->audio_codec[ this->audio_index ] )
2037 avcodec_close( this->audio_codec[ this->audio_index ] );
2040 this->audio_codec[ this->audio_index ] = NULL;
2042 if ( this->audio_index != -1 )
2043 this->audio_index = index;
2048 if ( context && index == INT_MAX )
2050 mlt_properties_set_int( frame_properties, "frequency", this->max_frequency );
2051 mlt_properties_set_int( frame_properties, "channels", this->total_channels );
2052 for ( index = 0; index < context->nb_streams; index++ )
2054 if ( context->streams[ index ]->codec->codec_type == CODEC_TYPE_AUDIO )
2055 audio_codec_init( this, index, properties );
2058 else if ( context && index > -1 && audio_codec_init( this, index, properties ) )
2060 // Set the frame properties
2061 if ( index < INT_MAX )
2063 mlt_properties_set_int( frame_properties, "frequency", this->audio_codec[ index ]->sample_rate );
2064 mlt_properties_set_int( frame_properties, "channels", this->audio_codec[ index ]->channels );
2067 if ( context && index > -1 )
2069 // Add our audio operation
2070 mlt_frame_push_audio( frame, this );
2071 mlt_frame_push_audio( frame, producer_get_audio );
2075 /** Our get frame implementation.
2078 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
2080 // Access the private data
2081 mlt_service service = MLT_PRODUCER_SERVICE( producer );
2082 mlt_cache_item cache_item = mlt_service_cache_get( service, "producer_avformat" );
2083 producer_avformat this = mlt_cache_item_data( cache_item, NULL );
2088 this = calloc( 1, sizeof( struct producer_avformat_s ) );
2089 producer->child = this;
2090 this->parent = producer;
2091 mlt_service_cache_put( service, "producer_avformat", this, 0, (mlt_destructor) producer_avformat_close );
2092 cache_item = mlt_service_cache_get( service, "producer_avformat" );
2095 // Create an empty frame
2096 *frame = mlt_frame_init( service);
2100 mlt_properties_set_data( MLT_FRAME_PROPERTIES(*frame), "avformat_cache", cache_item, 0, (mlt_destructor) mlt_cache_item_close, NULL );
2104 mlt_cache_item_close( cache_item );
2108 // Update timecode on the frame we're creating
2109 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
2111 // Set the position of this producer
2112 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( producer ) );
2115 producer_set_up_video( this, *frame );
2118 producer_set_up_audio( this, *frame );
2120 // Calculate the next timecode
2121 mlt_producer_prepare_next( producer );
2126 static void producer_avformat_close( producer_avformat this )
2128 mlt_log_debug( NULL, "producer_avformat_close\n" );
2130 av_free( this->av_frame );
2133 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
2135 if ( this->audio_resample[i] )
2136 audio_resample_close( this->audio_resample[i] );
2137 mlt_pool_release( this->audio_buffer[i] );
2138 av_free( this->decode_buffer[i] );
2139 if ( this->audio_codec[i] )
2140 avcodec_close( this->audio_codec[i] );
2142 if ( this->video_codec )
2143 avcodec_close( this->video_codec );
2144 if ( this->dummy_context )
2145 av_close_input_file( this->dummy_context );
2146 if ( this->audio_format )
2147 av_close_input_file( this->audio_format );
2148 if ( this->video_format )
2149 av_close_input_file( this->video_format );
2152 vdpau_producer_close( this );
2154 if ( this->image_cache )
2155 mlt_cache_close( this->image_cache );
2159 static void producer_close( mlt_producer parent )
2162 parent->close = NULL;
2163 mlt_producer_close( parent );