2 * factory.c -- the factory method interfaces
3 * Copyright (C) 2003-2012 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <framework/mlt.h>
28 extern mlt_consumer consumer_avformat_init( mlt_profile profile, char *file );
29 extern mlt_filter filter_avcolour_space_init( void *arg );
30 extern mlt_filter filter_avdeinterlace_init( void *arg );
31 extern mlt_filter filter_avresample_init( char *arg );
32 extern mlt_filter filter_swscale_init( mlt_profile profile, char *arg );
33 extern mlt_producer producer_avformat_init( mlt_profile profile, const char *service, char *file );
35 // ffmpeg Header files
36 #include <libavformat/avformat.h>
38 #include <libavdevice/avdevice.h>
40 #if LIBAVCODEC_VERSION_MAJOR >= 53
41 #include <libavutil/opt.h>
43 #include <libavcodec/opt.h>
46 // A static flag used to determine if avformat has been initialised
47 static int avformat_initialised = 0;
50 static pthread_mutex_t avformat_mutex;
53 // These 3 functions should override the alloc functions in libavformat
54 // but some formats or codecs seem to crash when used (wmv in particular)
56 void *av_malloc( unsigned int size )
58 return mlt_pool_alloc( size );
61 void *av_realloc( void *ptr, unsigned int size )
63 return mlt_pool_realloc( ptr, size );
66 void av_free( void *ptr )
68 return mlt_pool_release( ptr );
72 void avformat_destroy( void *ignore )
75 // av_free_static( ); -XXX this is deprecated
78 pthread_mutex_destroy( &avformat_mutex );
84 pthread_mutex_lock( &avformat_mutex );
87 void avformat_unlock( )
89 // Unlock the mutex now
90 pthread_mutex_unlock( &avformat_mutex );
93 static void avformat_init( )
95 // Initialise avformat if necessary
96 if ( avformat_initialised == 0 )
98 avformat_initialised = 1;
99 pthread_mutex_init( &avformat_mutex, NULL );
102 avdevice_register_all();
104 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(13<<8))
105 avformat_network_init();
107 mlt_factory_register_for_clean_up( NULL, avformat_destroy );
108 av_log_set_level( mlt_log_get_level() );
112 static void *create_service( mlt_profile profile, mlt_service_type type, const char *id, void *arg )
116 if ( !strncmp( id, "avformat", 8 ) )
118 if ( type == producer_type )
119 return producer_avformat_init( profile, id, arg );
120 else if ( type == consumer_type )
121 return consumer_avformat_init( profile, arg );
125 if ( !strcmp( id, "avcolor_space" ) )
126 return filter_avcolour_space_init( arg );
127 if ( !strcmp( id, "avcolour_space" ) )
128 return filter_avcolour_space_init( arg );
129 if ( !strcmp( id, "avdeinterlace" ) )
130 return filter_avdeinterlace_init( arg );
131 if ( !strcmp( id, "avresample" ) )
132 return filter_avresample_init( arg );
134 if ( !strcmp( id, "swscale" ) )
135 return filter_swscale_init( profile, arg );
141 static void add_parameters( mlt_properties params, void *object, int req_flags, const char *unit, const char *subclass )
143 const AVOption *opt = NULL;
145 // For each AVOption on the AVClass object
146 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(12<<8)+0)
147 while ( ( opt = av_opt_next( object, opt ) ) )
149 while ( ( opt = av_next_option( object, opt ) ) )
152 // If matches flags and not a binary option (not supported by Mlt)
153 if ( !( opt->flags & req_flags ) || ( opt->type == FF_OPT_TYPE_BINARY ) )
156 // Ignore constants (keyword values)
157 if ( !unit && opt->type == FF_OPT_TYPE_CONST )
159 // When processing a groups of options (unit)...
160 // ...ignore non-constants
161 else if ( unit && opt->type != FF_OPT_TYPE_CONST )
163 // ...ignore constants not in this group
164 else if ( unit && opt->type == FF_OPT_TYPE_CONST && strcmp( unit, opt->unit ) )
166 // ..add constants to the 'values' sequence
167 else if ( unit && opt->type == FF_OPT_TYPE_CONST )
170 snprintf( key, 20, "%d", mlt_properties_count( params ) );
171 mlt_properties_set( params, key, opt->name );
175 // Create a map for this option.
176 mlt_properties p = mlt_properties_new();
178 snprintf( key, 20, "%d", mlt_properties_count( params ) );
179 // Add the map to the 'parameters' sequence.
180 mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
182 // Add the parameter metadata for this AVOption.
183 mlt_properties_set( p, "identifier", opt->name );
188 char *s = malloc( strlen( opt->help ) + strlen( subclass ) + 4 );
189 strcpy( s, opt->help );
191 strcat( s, subclass );
193 mlt_properties_set( p, "description", s );
197 mlt_properties_set( p, "description", opt->help );
202 case FF_OPT_TYPE_FLAGS:
203 mlt_properties_set( p, "type", "string" );
204 mlt_properties_set( p, "format", "flags" );
206 case FF_OPT_TYPE_INT:
209 mlt_properties_set( p, "type", "integer" );
210 if ( opt->min != INT_MIN )
211 mlt_properties_set_int( p, "minimum", (int) opt->min );
212 if ( opt->max != INT_MAX )
213 mlt_properties_set_int( p, "maximum", (int) opt->max );
214 #if LIBAVUTIL_VERSION_MAJOR > 50
215 mlt_properties_set_int( p, "default", (int) opt->default_val.dbl );
220 mlt_properties_set( p, "type", "string" );
221 mlt_properties_set( p, "format", "integer or keyword" );
224 case FF_OPT_TYPE_INT64:
225 mlt_properties_set( p, "type", "integer" );
226 mlt_properties_set( p, "format", "64-bit" );
227 if ( opt->min != INT64_MIN )
228 mlt_properties_set_int64( p, "minimum", (int64_t) opt->min );
229 if ( opt->max != INT64_MAX )
230 mlt_properties_set_int64( p, "maximum", (int64_t) opt->max );
231 #if LIBAVUTIL_VERSION_MAJOR > 50
232 mlt_properties_set_int64( p, "default", (int64_t) opt->default_val.dbl );
235 case FF_OPT_TYPE_FLOAT:
236 mlt_properties_set( p, "type", "float" );
237 if ( opt->min != FLT_MIN && opt->min != -340282346638528859811704183484516925440.0 )
238 mlt_properties_set_double( p, "minimum", opt->min );
239 if ( opt->max != FLT_MAX )
240 mlt_properties_set_double( p, "maximum", opt->max );
241 #if LIBAVUTIL_VERSION_MAJOR > 50
242 mlt_properties_set_double( p, "default", opt->default_val.dbl );
245 case FF_OPT_TYPE_DOUBLE:
246 mlt_properties_set( p, "type", "float" );
247 mlt_properties_set( p, "format", "double" );
248 if ( opt->min != DBL_MIN )
249 mlt_properties_set_double( p, "minimum", opt->min );
250 if ( opt->max != DBL_MAX )
251 mlt_properties_set_double( p, "maximum", opt->max );
252 #if LIBAVUTIL_VERSION_MAJOR > 50
253 mlt_properties_set_double( p, "default", opt->default_val.dbl );
256 case FF_OPT_TYPE_STRING:
257 mlt_properties_set( p, "type", "string" );
258 #if LIBAVUTIL_VERSION_MAJOR > 50
259 mlt_properties_set( p, "default", opt->default_val.str );
262 case FF_OPT_TYPE_RATIONAL:
263 mlt_properties_set( p, "type", "string" );
264 mlt_properties_set( p, "format", "numerator:denominator" );
266 case FF_OPT_TYPE_CONST:
268 mlt_properties_set( p, "type", "integer" );
269 mlt_properties_set( p, "format", "constant" );
272 // If the option belongs to a group (unit) and is not a constant (keyword value)
273 if ( opt->unit && opt->type != FF_OPT_TYPE_CONST )
275 // Create a 'values' sequence.
276 mlt_properties values = mlt_properties_new();
278 // Recurse to add constants in this group to the 'values' sequence.
279 add_parameters( values, object, req_flags, opt->unit, NULL );
280 if ( mlt_properties_count( values ) )
281 mlt_properties_set_data( p, "values", values, 0, (mlt_destructor) mlt_properties_close, NULL );
283 mlt_properties_close( values );
288 static mlt_properties avformat_metadata( mlt_service_type type, const char *id, void *data )
290 char file[ PATH_MAX ];
291 const char *service_type = NULL;
292 mlt_properties result = NULL;
294 // Convert the service type to a string.
298 service_type = "consumer";
301 service_type = "filter";
304 service_type = "producer";
306 case transition_type:
307 service_type = "transition";
312 // Load the yaml file
313 snprintf( file, PATH_MAX, "%s/avformat/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
314 result = mlt_properties_parse_yaml( file );
315 if ( result && ( type == consumer_type || type == producer_type ) )
317 // Annotate the yaml properties with AVOptions.
318 mlt_properties params = (mlt_properties) mlt_properties_get_data( result, "parameters", NULL );
319 AVFormatContext *avformat = avformat_alloc_context();
320 #if LIBAVCODEC_VERSION_INT > ((53<<16)+(8<<8)+0)
321 AVCodecContext *avcodec = avcodec_alloc_context3( NULL );
323 AVCodecContext *avcodec = avcodec_alloc_context();
325 int flags = ( type == consumer_type )? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
327 add_parameters( params, avformat, flags, NULL, NULL );
328 #if LIBAVFORMAT_VERSION_MAJOR >= 53
330 if ( type == producer_type )
332 AVInputFormat *f = NULL;
333 while ( ( f = av_iformat_next( f ) ) )
335 add_parameters( params, &f->priv_class, flags, NULL, f->name );
339 AVOutputFormat *f = NULL;
340 while ( ( f = av_oformat_next( f ) ) )
342 add_parameters( params, &f->priv_class, flags, NULL, f->name );
346 add_parameters( params, avcodec, flags, NULL, NULL );
347 #if LIBAVCODEC_VERSION_MAJOR >= 53
349 while ( ( c = av_codec_next( c ) ) )
351 add_parameters( params, &c->priv_class, flags, NULL, c->name );
363 MLT_REGISTER( consumer_type, "avformat", create_service );
364 MLT_REGISTER( producer_type, "avformat", create_service );
365 MLT_REGISTER( producer_type, "avformat-novalidate", create_service );
366 MLT_REGISTER_METADATA( consumer_type, "avformat", avformat_metadata, NULL );
367 MLT_REGISTER_METADATA( producer_type, "avformat", avformat_metadata, NULL );
370 MLT_REGISTER( filter_type, "avcolour_space", create_service );
371 MLT_REGISTER( filter_type, "avcolor_space", create_service );
372 MLT_REGISTER( filter_type, "avdeinterlace", create_service );
373 MLT_REGISTER( filter_type, "avresample", create_service );
375 MLT_REGISTER( filter_type, "swscale", create_service );