]> git.sesse.net Git - mlt/blob - src/modules/avformat/factory.c
Fix metadata for avformat demuxer/device-specific options.
[mlt] / src / modules / avformat / factory.c
1 /*
2  * factory.c -- the factory method interfaces
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <string.h>
22 #include <pthread.h>
23 #include <limits.h>
24 #include <float.h>
25
26 #include <framework/mlt.h>
27
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 );
34
35 // ffmpeg Header files
36 #include <libavformat/avformat.h>
37 #ifdef AVDEVICE
38 #include <libavdevice/avdevice.h>
39 #endif
40 #if LIBAVCODEC_VERSION_MAJOR > 52
41 #include <libavutil/opt.h>
42 #else
43 #include <libavcodec/opt.h>
44 #endif
45
46 // A static flag used to determine if avformat has been initialised
47 static int avformat_initialised = 0;
48
49 // A locking mutex
50 static pthread_mutex_t avformat_mutex;
51
52 #if 0
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)
55
56 void *av_malloc( unsigned int size )
57 {
58         return mlt_pool_alloc( size );
59 }
60
61 void *av_realloc( void *ptr, unsigned int size )
62 {
63         return mlt_pool_realloc( ptr, size );
64 }
65
66 void av_free( void *ptr )
67 {
68         return mlt_pool_release( ptr );
69 }
70 #endif
71
72 void avformat_destroy( void *ignore )
73 {
74         // Clean up
75         // av_free_static( ); -XXX this is deprecated
76
77         // Destroy the mutex
78         pthread_mutex_destroy( &avformat_mutex );
79 }
80
81 void avformat_lock( )
82 {
83         // Lock the mutex now
84         pthread_mutex_lock( &avformat_mutex );
85 }
86
87 void avformat_unlock( )
88 {
89         // Unlock the mutex now
90         pthread_mutex_unlock( &avformat_mutex );
91 }
92
93 static void avformat_init( )
94 {
95         // Initialise avformat if necessary
96         if ( avformat_initialised == 0 )
97         {
98                 avformat_initialised = 1;
99                 pthread_mutex_init( &avformat_mutex, NULL );
100                 av_register_all( );
101 #ifdef AVDEVICE
102                 avdevice_register_all();
103 #endif
104                 mlt_factory_register_for_clean_up( NULL, avformat_destroy );
105                 av_log_set_level( mlt_log_get_level() );
106         }
107 }
108
109 static void *create_service( mlt_profile profile, mlt_service_type type, const char *id, void *arg )
110 {
111         avformat_init( );
112 #ifdef CODECS
113         if ( !strncmp( id, "avformat", 8 ) )
114         {
115                 if ( type == producer_type )
116                         return producer_avformat_init( profile, id, arg );
117                 else if ( type == consumer_type )
118                         return consumer_avformat_init( profile, arg );
119         }
120 #endif
121 #ifdef FILTERS
122         if ( !strcmp( id, "avcolor_space" ) )
123                 return filter_avcolour_space_init( arg );
124         if ( !strcmp( id, "avcolour_space" ) )
125                 return filter_avcolour_space_init( arg );
126         if ( !strcmp( id, "avdeinterlace" ) )
127                 return filter_avdeinterlace_init( arg );
128         if ( !strcmp( id, "avresample" ) )
129                 return filter_avresample_init( arg );
130 #ifdef SWSCALE
131         if ( !strcmp( id, "swscale" ) )
132                 return filter_swscale_init( profile, arg );
133 #endif
134 #endif
135         return NULL;
136 }
137
138 static void add_parameters( mlt_properties params, void *object, int req_flags, const char *unit, const char *subclass )
139 {
140         const AVOption *opt = NULL;
141
142         // For each AVOption on the AVClass object
143         while ( ( opt = av_next_option( object, opt ) ) )
144         {
145                 // If matches flags and not a binary option (not supported by Mlt)
146                 if ( !( opt->flags & req_flags ) || ( opt->type == FF_OPT_TYPE_BINARY ) )
147             continue;
148
149                 // Ignore constants (keyword values)
150                 if ( !unit && opt->type == FF_OPT_TYPE_CONST )
151                         continue;
152                 // When processing a groups of options (unit)...
153                 // ...ignore non-constants
154                 else if ( unit && opt->type != FF_OPT_TYPE_CONST )
155                         continue;
156                 // ...ignore constants not in this group
157                 else if ( unit && opt->type == FF_OPT_TYPE_CONST && strcmp( unit, opt->unit ) )
158                         continue;
159                 // ..add constants to the 'values' sequence
160                 else if ( unit && opt->type == FF_OPT_TYPE_CONST )
161                 {
162                         char key[20];
163                         snprintf( key, 20, "%d", mlt_properties_count( params ) );
164                         mlt_properties_set( params, key, opt->name );
165                         continue;
166                 }
167
168                 // Create a map for this option.
169                 mlt_properties p = mlt_properties_new();
170                 char key[20];
171                 snprintf( key, 20, "%d", mlt_properties_count( params ) );
172                 // Add the map to the 'parameters' sequence.
173                 mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
174
175                 // Add the parameter metadata for this AVOption.
176                 mlt_properties_set( p, "identifier", opt->name );
177                 if ( opt->help )
178                 {
179                         if ( subclass )
180                         {
181                                 char *s = malloc( strlen( opt->help ) + strlen( subclass ) + 4 );
182                                 strcpy( s, opt->help );
183                                 strcat( s, " (" );
184                                 strcat( s, subclass );
185                                 strcat( s, ")" );
186                                 mlt_properties_set( p, "description", s );
187                                 free( s );
188                         }
189                         else
190                                 mlt_properties_set( p, "description", opt->help );
191                 }
192
193         switch ( opt->type )
194                 {
195                 case FF_OPT_TYPE_FLAGS:
196                         mlt_properties_set( p, "type", "string" );
197                         mlt_properties_set( p, "format", "flags" );
198                         break;
199                 case FF_OPT_TYPE_INT:
200                         if ( !opt->unit )
201                         {
202                                 mlt_properties_set( p, "type", "integer" );
203                                 if ( opt->min != INT_MIN )
204                                         mlt_properties_set_int( p, "minimum", (int) opt->min );
205                                 if ( opt->max != INT_MAX )
206                                         mlt_properties_set_int( p, "maximum", (int) opt->max );
207 #if LIBAVUTIL_VERSION_MAJOR > 50
208                                 mlt_properties_set_int( p, "default", (int) opt->default_val.dbl );
209 #endif
210                         }
211                         else
212                         {
213                                 mlt_properties_set( p, "type", "string" );
214                                 mlt_properties_set( p, "format", "integer or keyword" );
215                         }
216                         break;
217                 case FF_OPT_TYPE_INT64:
218                         mlt_properties_set( p, "type", "integer" );
219                         mlt_properties_set( p, "format", "64-bit" );
220                         if ( opt->min != INT64_MIN )
221                                 mlt_properties_set_int64( p, "minimum", (int64_t) opt->min );
222                         if ( opt->max != INT64_MAX )
223                         mlt_properties_set_int64( p, "maximum", (int64_t) opt->max );
224 #if LIBAVUTIL_VERSION_MAJOR > 50
225                         mlt_properties_set_int64( p, "default", (int64_t) opt->default_val.dbl );
226 #endif
227                         break;
228                 case FF_OPT_TYPE_FLOAT:
229                         mlt_properties_set( p, "type", "float" );
230                         if ( opt->min != FLT_MIN && opt->min != -340282346638528859811704183484516925440.0 )
231                                 mlt_properties_set_double( p, "minimum", opt->min );
232                         if ( opt->max != FLT_MAX )
233                                 mlt_properties_set_double( p, "maximum", opt->max );
234 #if LIBAVUTIL_VERSION_MAJOR > 50
235                         mlt_properties_set_double( p, "default", opt->default_val.dbl );
236 #endif
237                         break;
238                 case FF_OPT_TYPE_DOUBLE:
239                         mlt_properties_set( p, "type", "float" );
240                         mlt_properties_set( p, "format", "double" );
241                         if ( opt->min != DBL_MIN )
242                                 mlt_properties_set_double( p, "minimum", opt->min );
243                         if ( opt->max != DBL_MAX )
244                                 mlt_properties_set_double( p, "maximum", opt->max );
245 #if LIBAVUTIL_VERSION_MAJOR > 50
246                         mlt_properties_set_double( p, "default", opt->default_val.dbl );
247 #endif
248                         break;
249                 case FF_OPT_TYPE_STRING:
250                         mlt_properties_set( p, "type", "string" );
251 #if LIBAVUTIL_VERSION_MAJOR > 50
252                         mlt_properties_set( p, "default", opt->default_val.str );
253 #endif
254                         break;
255                 case FF_OPT_TYPE_RATIONAL:
256                         mlt_properties_set( p, "type", "string" );
257                         mlt_properties_set( p, "format", "numerator:denominator" );
258                         break;
259                 case FF_OPT_TYPE_CONST:
260                 default:
261                         mlt_properties_set( p, "type", "integer" );
262                         mlt_properties_set( p, "format", "constant" );
263                         break;
264         }
265                 // If the option belongs to a group (unit) and is not a constant (keyword value)
266                 if ( opt->unit && opt->type != FF_OPT_TYPE_CONST )
267                 {
268                         // Create a 'values' sequence.
269                         mlt_properties values = mlt_properties_new();
270
271                         // Recurse to add constants in this group to the 'values' sequence.
272                         add_parameters( values, object, req_flags, opt->unit, NULL );
273                         if ( mlt_properties_count( values ) )
274                                 mlt_properties_set_data( p, "values", values, 0, (mlt_destructor) mlt_properties_close, NULL );
275                         else
276                                 mlt_properties_close( values );
277                 }
278         }
279 }
280
281 static mlt_properties avformat_metadata( mlt_service_type type, const char *id, void *data )
282 {
283         char file[ PATH_MAX ];
284         const char *service_type = NULL;
285         mlt_properties result = NULL;
286
287         // Convert the service type to a string.
288         switch ( type )
289         {
290                 case consumer_type:
291                         service_type = "consumer";
292                         break;
293                 case filter_type:
294                         service_type = "filter";
295                         break;
296                 case producer_type:
297                         service_type = "producer";
298                         break;
299                 case transition_type:
300                         service_type = "transition";
301                         break;
302                 default:
303                         return NULL;
304         }
305         // Load the yaml file
306         snprintf( file, PATH_MAX, "%s/avformat/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
307         result = mlt_properties_parse_yaml( file );
308         if ( result && ( type == consumer_type || type == producer_type ) )
309         {
310                 // Annotate the yaml properties with AVOptions.
311                 mlt_properties params = (mlt_properties) mlt_properties_get_data( result, "parameters", NULL );
312                 AVFormatContext *avformat = avformat_alloc_context();
313                 AVCodecContext *avcodec = avcodec_alloc_context();
314                 int flags = ( type == consumer_type )? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
315
316                 add_parameters( params, avformat, flags, NULL, NULL );
317 #if LIBAVFORMAT_VERSION_MAJOR > 52
318                 avformat_init();
319                 if ( type == producer_type )
320                 {
321                         AVInputFormat *f = NULL;
322                         while ( ( f = av_iformat_next( f ) ) )
323                                 if ( f->priv_class )
324                                         add_parameters( params, &f->priv_class, flags, NULL, f->name );
325                 }
326                 else
327                 {
328                         AVOutputFormat *f = NULL;
329                         while ( ( f = av_oformat_next( f ) ) )
330                                 if ( f->priv_class )
331                                         add_parameters( params, &f->priv_class, flags, NULL, f->name );
332                 }
333 #endif
334
335                 add_parameters( params, avcodec, flags, NULL, NULL );
336 #if LIBAVCODEC_VERSION_MAJOR > 52
337                 AVCodec *c = NULL;
338                 while ( ( c = av_codec_next( c ) ) )
339                         if ( c->priv_class )
340                                 add_parameters( params, &c->priv_class, flags, NULL, c->name );
341 #endif
342
343                 av_free( avformat );
344                 av_free( avcodec );
345         }
346         return result;
347 }
348
349 MLT_REPOSITORY
350 {
351 #ifdef CODECS
352         MLT_REGISTER( consumer_type, "avformat", create_service );
353         MLT_REGISTER( producer_type, "avformat", create_service );
354         MLT_REGISTER( producer_type, "avformat-novalidate", create_service );
355         MLT_REGISTER_METADATA( consumer_type, "avformat", avformat_metadata, NULL );
356         MLT_REGISTER_METADATA( producer_type, "avformat", avformat_metadata, NULL );
357 #endif
358 #ifdef FILTERS
359         MLT_REGISTER( filter_type, "avcolour_space", create_service );
360         MLT_REGISTER( filter_type, "avcolor_space", create_service );
361         MLT_REGISTER( filter_type, "avdeinterlace", create_service );
362         MLT_REGISTER( filter_type, "avresample", create_service );
363 #ifdef SWSCALE
364         MLT_REGISTER( filter_type, "swscale", create_service );
365 #endif
366 #endif
367 }