/*
* consumer_avformat.c -- an encoder based on avformat
- * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
+ * Copyright (C) 2003-2012 Ushodaya Enterprises Limited
* Author: Charles Yates <charles.yates@pandora.be>
+ * Author: Dan Dennedy <dan@dennedy.org>
* Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
#if LIBAVUTIL_VERSION_INT >= ((50<<16)+(8<<8)+0)
#include <libavutil/pixdesc.h>
#endif
+#include <libavutil/mathematics.h>
#if LIBAVUTIL_VERSION_INT < (50<<16)
#define PIX_FMT_RGB32 PIX_FMT_RGBA32
{
int i;
int count = mlt_properties_count( properties );
+#if LIBAVUTIL_VERSION_INT < ((51<<16)+(12<<8)+0)
int alloc = 1;
+#endif
for ( i = 0; i < count; i++ )
{
const char *opt_name = mlt_properties_get_name( properties, i );
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(7<<8)+0)
+ const AVOption *opt = av_opt_find( obj, opt_name, NULL, flags, flags );
+#else
const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
+#endif
// If option not found, see if it was prefixed with a or v (-vb)
if ( !opt && (
( opt_name[0] == 'v' && ( flags & AV_OPT_FLAG_VIDEO_PARAM ) ) ||
( opt_name[0] == 'a' && ( flags & AV_OPT_FLAG_AUDIO_PARAM ) ) ) )
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(7<<8)+0)
+ opt = av_opt_find( obj, ++opt_name, NULL, flags, flags );
+#else
opt = av_find_opt( obj, ++opt_name, NULL, flags, flags );
+#endif
// Apply option if found
if ( opt )
-#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(12<<8)+0)
+ av_opt_set( obj, opt_name, mlt_properties_get_value( properties, i), 0 );
+#elif LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
av_set_string3( obj, opt_name, mlt_properties_get_value( properties, i), alloc, NULL );
#elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
av_set_string2( obj, opt_name, mlt_properties_get_value( properties, i), alloc );
mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
// Create a new stream
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(10<<8)+0)
+ AVStream *st = avformat_new_stream( oc, codec );
+#else
AVStream *st = av_new_stream( oc, oc->nb_streams );
+#endif
// If created, then initialise from properties
if ( st != NULL )
if ( audio_qscale > QSCALE_NONE )
{
c->flags |= CODEC_FLAG_QSCALE;
- c->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
+ c->global_quality = FF_QP2LAMBDA * audio_qscale;
+#if LIBAVFORMAT_VERSION_MAJOR < 53
+ st->quality = c->global_quality;
+#endif
}
// Set parameters controlled by MLT
if ( mlt_properties_get( properties, "alang" ) != NULL )
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(43<<8)+0)
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
+ av_dict_set( &oc->metadata, "language", mlt_properties_get( properties, "alang" ), 0 );
+#else
av_metadata_set2( &oc->metadata, "language", mlt_properties_get( properties, "alang" ), 0 );
+#endif
#else
strncpy( st->language, mlt_properties_get( properties, "alang" ), sizeof( st->language ) );
avformat_lock();
// Continue if codec found and we can open it
- if ( codec != NULL && avcodec_open( c, codec ) >= 0 )
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+ if ( codec && avcodec_open2( c, codec, NULL ) >= 0 )
+#else
+ if ( codec && avcodec_open( c, codec ) >= 0 )
+#endif
{
// ugly hack for PCM codecs (will be removed ASAP with new PCM
// support to compute the input frame size in samples
mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
// Create a new stream
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(10<<8)+0)
+ AVStream *st = avformat_new_stream( oc, codec );
+#else
AVStream *st = av_new_stream( oc, oc->nb_streams );
+#endif
if ( st != NULL )
{
if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
{
c->flags |= CODEC_FLAG_QSCALE;
- st->quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
+ c->global_quality = FF_QP2LAMBDA * mlt_properties_get_double( properties, "qscale" );
+#if LIBAVFORMAT_VERSION_MAJOR < 53
+ st->quality = c->global_quality;
+#endif
}
// Allow the user to override the video fourcc
// Open the codec safely
avformat_lock();
- int result = codec != NULL && avcodec_open( video_enc, codec ) >= 0;
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+ int result = codec && avcodec_open2( video_enc, codec, NULL ) >= 0;
+#else
+ int result = codec && avcodec_open( video_enc, codec ) >= 0;
+#endif
avformat_unlock();
return result;
markup[0] = '\0';
if ( !strstr( key, ".stream." ) )
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(43<<8)+0)
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
+ av_dict_set( &oc->metadata, key, mlt_properties_get_value( properties, i ), 0 );
+#else
av_metadata_set2( &oc->metadata, key, mlt_properties_get_value( properties, i ), 0 );
+#endif
#else
av_metadata_set( &oc->metadata, key, mlt_properties_get_value( properties, i ) );
#endif
mlt_properties_set_int( properties, "channels", total_channels );
// Set the parameters (even though we have none...)
- if ( av_set_parameters(oc, NULL) >= 0 )
+#if LIBAVFORMAT_VERSION_INT < ((53<<16)+(2<<8)+0)
+ if ( av_set_parameters(oc, NULL) >= 0 )
+#endif
{
+#if LIBAVFORMAT_VERSION_MAJOR >= 53
+ if ( mlt_properties_get( properties, "muxpreload" ) && ! mlt_properties_get( properties, "preload" ) )
+ mlt_properties_set_double( properties, "preload", mlt_properties_get_double( properties, "muxpreload" ) );
+#else
oc->preload = ( int )( mlt_properties_get_double( properties, "muxpreload" ) * AV_TIME_BASE );
+#endif
oc->max_delay= ( int )( mlt_properties_get_double( properties, "muxdelay" ) * AV_TIME_BASE );
// Process properties as AVOptions
// Write the stream header.
if ( mlt_properties_get_int( properties, "running" ) )
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(2<<8)+0)
+ avformat_write_header( oc, NULL );
+#else
av_write_header( oc );
+#endif
}
+#if LIBAVFORMAT_VERSION_INT < ((53<<16)+(2<<8)+0)
else
{
mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Invalid output format parameters\n" );
mlt_properties_set_int( properties, "running", 0 );
}
+#endif
// Allocate picture
if ( video_st )
else
{
// Set the quality
- output->quality = video_st->quality;
+ output->quality = c->global_quality;
// Set frame interlace hints
output->interlaced_frame = !mlt_properties_get_int( frame_properties, "progressive" );
/*
* producer_avformat.c -- avformat producer
- * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
+ * Copyright (C) 2003-2012 Ushodaya Enterprises Limited
* Author: Charles Yates <charles.yates@pandora.be>
* Author: Dan Dennedy <dan@dennedy.org>
* Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
#if (LIBAVUTIL_VERSION_INT > ((50<<16)+(7<<8)+0))
# include <libavutil/pixdesc.h>
#endif
+#if (LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0))
+# include <libavutil/dict.h>
+#endif
// System header files
#include <stdlib.h>
{
// Close the file to release resources for large playlists - reopen later as needed
avformat_lock();
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
+ if ( self->audio_format )
+ avformat_close_input( &self->audio_format );
+ if ( self->video_format )
+ avformat_close_input( &self->video_format );
+#else
if ( self->audio_format )
av_close_input_file( self->audio_format );
- self->audio_format = NULL;
if ( self->video_format )
av_close_input_file( self->video_format );
+#endif
+ self->audio_format = NULL;
self->video_format = NULL;
avformat_unlock();
}
{
int i;
char key[200];
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
+ AVDictionaryEntry *tag = NULL;
+#else
AVMetadataTag *tag = NULL;
+#endif
AVFormatContext *context = self->video_format;
mlt_properties meta_media = MLT_PRODUCER_PROPERTIES( self->parent );
mlt_properties_set_double( meta_media, key, (double) codec_context->time_base.den /
( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num ) );
snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(3<<8)+0)
+ mlt_properties_set( meta_media, key, av_get_pix_fmt_name( codec_context->pix_fmt ) );
+#else
mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
+#endif
snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
#if LIBAVCODEC_VERSION_INT > ((52<<16)+(28<<8)+0)
// Read Metadata
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
+ while ( ( tag = av_dict_get( stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
+#else
while ( ( tag = av_metadata_get( stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX ) ) )
+#endif
{
if ( tag->value && strcmp( tag->value, "" ) && strcmp( tag->value, "und" ) )
{
#endif
}
#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
+ while ( ( tag = av_dict_get( context->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
+#else
while ( ( tag = av_metadata_get( context->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX ) ) )
+#endif
{
if ( tag->value && strcmp( tag->value, "" ) && strcmp( tag->value, "und" ) )
{
return aspect_ratio;
}
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+static char* parse_url( mlt_profile profile, const char* URL, AVInputFormat **format, AVDictionary **params )
+#else
static char* parse_url( mlt_profile profile, const char* URL, AVInputFormat **format, AVFormatParameters *params )
+#endif
{
if ( !URL ) return NULL;
- const char *result = URL;
+ char *result = NULL;
char *protocol = strdup( URL );
char *url = strchr( protocol, ':' );
if ( *format )
{
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ // support for legacy width and height parameters
+ char *width = NULL;
+ char *height = NULL;
+#else
// These are required by video4linux2 (defaults)
params->width = profile->width;
params->height = profile->height;
params->time_base = (AVRational){ profile->frame_rate_den, profile->frame_rate_num };
params->channels = 2;
params->sample_rate = 48000;
+#endif
// Parse out params
url = strchr( url, '?' );
char *t = strchr( value, '&' );
if ( t )
t[0] = 0;
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ // translate old parameters to new av_dict names
+ if ( !strcmp( name, "frame_rate" ) )
+ av_dict_set( params, "framerate", value, 0 );
+ else if ( !strcmp( name, "pix_fmt" ) )
+ av_dict_set( params, "pixel_format", value, 0 );
+ else if ( !strcmp( name, "width" ) )
+ width = strdup( value );
+ else if ( !strcmp( name, "height" ) )
+ height = strdup( value );
+ else
+ // generic demux/device option support
+ av_dict_set( params, name, value, 0 );
+#else
if ( !strcmp( name, "frame_rate" ) )
params->time_base.den = atoi( value );
else if ( !strcmp( name, "frame_rate_base" ) )
params->height = atoi( value );
else if ( !strcmp( name, "standard" ) )
params->standard = strdup( value );
+#endif
}
free( name );
url = strchr( url, '&' );
}
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ // continued support for legacy width and height parameters
+ if ( width && height )
+ {
+ char *s = malloc( strlen( width ) + strlen( height ) + 2 );
+ strcpy( s, width );
+ strcat( s, "x");
+ strcat( s, height );
+ av_dict_set( params, "video_size", s, 0 );
+ free( s );
+ }
+ if ( width ) free( width );
+ if ( height ) free ( height );
+#endif
}
+ result = strdup( result );
+ }
+ else
+ {
+ result = strdup( URL );
}
- result = strdup( result );
free( protocol );
return result;
}
self->seekable = av_seek_frame( format, -1, format->start_time, AVSEEK_FLAG_BACKWARD ) >= 0;
mlt_properties_set_int( properties, "seekable", self->seekable );
self->dummy_context = format;
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ self->video_format = NULL;
+ avformat_open_input( &self->video_format, filename, NULL, NULL );
+ avformat_find_stream_info( self->video_format, NULL );
+#else
av_open_input_file( &self->video_format, filename, NULL, 0, NULL );
+ av_find_stream_info( self->video_format );
+#endif
format = self->video_format;
- av_find_stream_info( format );
}
// Fetch the width, height and aspect ratio
// Parse URL
AVInputFormat *format = NULL;
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ AVDictionary *params = NULL;
+#else
AVFormatParameters params;
memset( ¶ms, 0, sizeof(params) );
+#endif
char *filename = parse_url( profile, URL, &format, ¶ms );
// Now attempt to open the file or device with filename
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ error = avformat_open_input( &self->video_format, filename, format, ¶ms ) < 0;
+ if ( error )
+ // If the URL is a network stream URL, then we probably need to open with full URL
+ error = avformat_open_input( &self->video_format, URL, format, ¶ms ) < 0;
+#else
error = av_open_input_file( &self->video_format, filename, format, 0, ¶ms ) < 0;
if ( error )
// If the URL is a network stream URL, then we probably need to open with full URL
error = av_open_input_file( &self->video_format, URL, format, 0, ¶ms ) < 0;
+#endif
// Set MLT properties onto video AVFormatContext
if ( !error && self->video_format )
#endif
}
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ av_dict_free( ¶ms );
+#else
// Cleanup AVFormatParameters
if ( params.standard )
free( (void*) params.standard );
+#endif
// If successful, then try to get additional info
if ( !error && self->video_format )
{
// Get the stream info
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ error = avformat_find_stream_info( self->video_format, NULL ) < 0;
+#else
error = av_find_stream_info( self->video_format ) < 0;
+#endif
// Continue if no error
if ( !error && self->video_format )
if ( self->seekable )
{
// And open again for our audio context
- av_open_input_file( &self->audio_format, filename, NULL, 0, NULL );
+#if LIBAVFORMAT_VERSION_INT > ((53<<16)+(6<<8)+0)
+ avformat_open_input( &self->audio_format, filename, NULL, NULL );
apply_properties( self->audio_format, properties, AV_OPT_FLAG_DECODING_PARAM );
-#if LIBAVFORMAT_VERSION_MAJOR >= 53
if ( self->audio_format->iformat && self->audio_format->iformat->priv_class && self->audio_format->priv_data )
apply_properties( self->audio_format->priv_data, properties, AV_OPT_FLAG_DECODING_PARAM );
-#endif
+ avformat_find_stream_info( self->audio_format, NULL );
+#else
+ av_open_input_file( &self->audio_format, filename, NULL, 0, NULL );
+ apply_properties( self->audio_format, properties, AV_OPT_FLAG_DECODING_PARAM );
av_find_stream_info( self->audio_format );
+#endif
}
else
{
if ( self->dummy_context )
{
avformat_lock();
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
+ avformat_close_input( &self->dummy_context );
+#else
av_close_input_file( self->dummy_context );
+#endif
avformat_unlock();
self->dummy_context = NULL;
}
if ( self->video_codec )
avcodec_close( self->video_codec );
self->video_codec = NULL;
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
+ if ( self->dummy_context )
+ avformat_close_input( &self->dummy_context );
+ if ( self->video_format )
+ avformat_close_input( &self->video_format );
+#else
if ( self->dummy_context )
av_close_input_file( self->dummy_context );
- self->dummy_context = NULL;
if ( self->video_format )
av_close_input_file( self->video_format );
+#endif
+ self->dummy_context = NULL;
self->video_format = NULL;
avformat_unlock();
// If we don't have a codec and we can't initialise it, we can't do much more...
avformat_lock( );
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+ if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
+#else
if ( codec && avcodec_open( codec_context, codec ) >= 0 )
+#endif
{
self->audio_streams++;
self->audio_max_stream = i;
for ( i = 0; i < count; i++ )
{
const char *opt_name = mlt_properties_get_name( properties, i );
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(7<<8)+0)
+ const AVOption *opt = av_opt_find( obj, opt_name, NULL, flags, flags );
+#else
const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
+#endif
if ( opt_name && mlt_properties_get( properties, opt_name ) )
{
if ( opt )
-#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(12<<8)+0)
+ av_opt_set( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
+#elif LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
#elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
// If we don't have a codec and we can't initialise it, we can't do much more...
avformat_lock( );
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+ if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
+#else
if ( codec && avcodec_open( codec_context, codec ) >= 0 )
+#endif
{
// Now store the codec with its destructor
self->video_codec = codec_context;
static int sample_bytes( AVCodecContext *context )
{
-#if LIBAVCODEC_VERSION_MAJOR >= 53
+#if LIBAVUTIL_VERSION_INT >= ((51<<16)+(8<<8)+0)
+ return av_get_bytes_per_sample( context->sample_fmt );
+#elif LIBAVCODEC_VERSION_MAJOR >= 53
return av_get_bits_per_sample_fmt( context->sample_fmt ) / 8;
#else
return av_get_bits_per_sample_format( context->sample_fmt ) / 8;
// If we don't have a codec and we can't initialise it, we can't do much more...
avformat_lock( );
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+ if ( codec && avcodec_open2( codec_context, codec, NULL ) >= 0 )
+#else
if ( codec && avcodec_open( codec_context, codec ) >= 0 )
+#endif
{
// Now store the codec with its destructor
if ( self->audio_codec[ index ] )
avcodec_close( self->video_codec );
self->video_codec = NULL;
// Close the file
+#if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(17<<8)+0)
+ if ( self->dummy_context )
+ avformat_close_input( &self->dummy_context );
+ if ( self->seekable && self->audio_format )
+ avformat_close_input( &self->audio_format );
+ if ( self->video_format )
+ avformat_close_input( &self->video_format );
+#else
if ( self->dummy_context )
av_close_input_file( self->dummy_context );
if ( self->seekable && self->audio_format )
av_close_input_file( self->audio_format );
if ( self->video_format )
av_close_input_file( self->video_format );
+#endif
avformat_unlock();
#ifdef VDPAU
vdpau_producer_close( self );