]> git.sesse.net Git - mlt/blob - src/modules/avformat/factory.c
Merge branch 'videostab' of https://github.com/gmarco/mlt into videostab
[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 >= 53
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 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(13<<8))
105                 avformat_network_init();
106 #endif
107                 mlt_factory_register_for_clean_up( NULL, avformat_destroy );
108                 av_log_set_level( mlt_log_get_level() );
109         }
110 }
111
112 static void *create_service( mlt_profile profile, mlt_service_type type, const char *id, void *arg )
113 {
114         avformat_init( );
115 #ifdef CODECS
116         if ( !strncmp( id, "avformat", 8 ) )
117         {
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 );
122         }
123 #endif
124 #ifdef FILTERS
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 );
133 #ifdef SWSCALE
134         if ( !strcmp( id, "swscale" ) )
135                 return filter_swscale_init( profile, arg );
136 #endif
137 #endif
138         return NULL;
139 }
140
141 static void add_parameters( mlt_properties params, void *object, int req_flags, const char *unit, const char *subclass )
142 {
143         const AVOption *opt = NULL;
144
145         // For each AVOption on the AVClass object
146         while ( ( opt = av_next_option( object, opt ) ) )
147         {
148                 // If matches flags and not a binary option (not supported by Mlt)
149                 if ( !( opt->flags & req_flags ) || ( opt->type == FF_OPT_TYPE_BINARY ) )
150             continue;
151
152                 // Ignore constants (keyword values)
153                 if ( !unit && opt->type == FF_OPT_TYPE_CONST )
154                         continue;
155                 // When processing a groups of options (unit)...
156                 // ...ignore non-constants
157                 else if ( unit && opt->type != FF_OPT_TYPE_CONST )
158                         continue;
159                 // ...ignore constants not in this group
160                 else if ( unit && opt->type == FF_OPT_TYPE_CONST && strcmp( unit, opt->unit ) )
161                         continue;
162                 // ..add constants to the 'values' sequence
163                 else if ( unit && opt->type == FF_OPT_TYPE_CONST )
164                 {
165                         char key[20];
166                         snprintf( key, 20, "%d", mlt_properties_count( params ) );
167                         mlt_properties_set( params, key, opt->name );
168                         continue;
169                 }
170
171                 // Create a map for this option.
172                 mlt_properties p = mlt_properties_new();
173                 char key[20];
174                 snprintf( key, 20, "%d", mlt_properties_count( params ) );
175                 // Add the map to the 'parameters' sequence.
176                 mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
177
178                 // Add the parameter metadata for this AVOption.
179                 mlt_properties_set( p, "identifier", opt->name );
180                 if ( opt->help )
181                 {
182                         if ( subclass )
183                         {
184                                 char *s = malloc( strlen( opt->help ) + strlen( subclass ) + 4 );
185                                 strcpy( s, opt->help );
186                                 strcat( s, " (" );
187                                 strcat( s, subclass );
188                                 strcat( s, ")" );
189                                 mlt_properties_set( p, "description", s );
190                                 free( s );
191                         }
192                         else
193                                 mlt_properties_set( p, "description", opt->help );
194                 }
195
196         switch ( opt->type )
197                 {
198                 case FF_OPT_TYPE_FLAGS:
199                         mlt_properties_set( p, "type", "string" );
200                         mlt_properties_set( p, "format", "flags" );
201                         break;
202                 case FF_OPT_TYPE_INT:
203                         if ( !opt->unit )
204                         {
205                                 mlt_properties_set( p, "type", "integer" );
206                                 if ( opt->min != INT_MIN )
207                                         mlt_properties_set_int( p, "minimum", (int) opt->min );
208                                 if ( opt->max != INT_MAX )
209                                         mlt_properties_set_int( p, "maximum", (int) opt->max );
210 #if LIBAVUTIL_VERSION_MAJOR > 50
211                                 mlt_properties_set_int( p, "default", (int) opt->default_val.dbl );
212 #endif
213                         }
214                         else
215                         {
216                                 mlt_properties_set( p, "type", "string" );
217                                 mlt_properties_set( p, "format", "integer or keyword" );
218                         }
219                         break;
220                 case FF_OPT_TYPE_INT64:
221                         mlt_properties_set( p, "type", "integer" );
222                         mlt_properties_set( p, "format", "64-bit" );
223                         if ( opt->min != INT64_MIN )
224                                 mlt_properties_set_int64( p, "minimum", (int64_t) opt->min );
225                         if ( opt->max != INT64_MAX )
226                         mlt_properties_set_int64( p, "maximum", (int64_t) opt->max );
227 #if LIBAVUTIL_VERSION_MAJOR > 50
228                         mlt_properties_set_int64( p, "default", (int64_t) opt->default_val.dbl );
229 #endif
230                         break;
231                 case FF_OPT_TYPE_FLOAT:
232                         mlt_properties_set( p, "type", "float" );
233                         if ( opt->min != FLT_MIN && opt->min != -340282346638528859811704183484516925440.0 )
234                                 mlt_properties_set_double( p, "minimum", opt->min );
235                         if ( opt->max != FLT_MAX )
236                                 mlt_properties_set_double( p, "maximum", opt->max );
237 #if LIBAVUTIL_VERSION_MAJOR > 50
238                         mlt_properties_set_double( p, "default", opt->default_val.dbl );
239 #endif
240                         break;
241                 case FF_OPT_TYPE_DOUBLE:
242                         mlt_properties_set( p, "type", "float" );
243                         mlt_properties_set( p, "format", "double" );
244                         if ( opt->min != DBL_MIN )
245                                 mlt_properties_set_double( p, "minimum", opt->min );
246                         if ( opt->max != DBL_MAX )
247                                 mlt_properties_set_double( p, "maximum", opt->max );
248 #if LIBAVUTIL_VERSION_MAJOR > 50
249                         mlt_properties_set_double( p, "default", opt->default_val.dbl );
250 #endif
251                         break;
252                 case FF_OPT_TYPE_STRING:
253                         mlt_properties_set( p, "type", "string" );
254 #if LIBAVUTIL_VERSION_MAJOR > 50
255                         mlt_properties_set( p, "default", opt->default_val.str );
256 #endif
257                         break;
258                 case FF_OPT_TYPE_RATIONAL:
259                         mlt_properties_set( p, "type", "string" );
260                         mlt_properties_set( p, "format", "numerator:denominator" );
261                         break;
262                 case FF_OPT_TYPE_CONST:
263                 default:
264                         mlt_properties_set( p, "type", "integer" );
265                         mlt_properties_set( p, "format", "constant" );
266                         break;
267         }
268                 // If the option belongs to a group (unit) and is not a constant (keyword value)
269                 if ( opt->unit && opt->type != FF_OPT_TYPE_CONST )
270                 {
271                         // Create a 'values' sequence.
272                         mlt_properties values = mlt_properties_new();
273
274                         // Recurse to add constants in this group to the 'values' sequence.
275                         add_parameters( values, object, req_flags, opt->unit, NULL );
276                         if ( mlt_properties_count( values ) )
277                                 mlt_properties_set_data( p, "values", values, 0, (mlt_destructor) mlt_properties_close, NULL );
278                         else
279                                 mlt_properties_close( values );
280                 }
281         }
282 }
283
284 static mlt_properties avformat_metadata( mlt_service_type type, const char *id, void *data )
285 {
286         char file[ PATH_MAX ];
287         const char *service_type = NULL;
288         mlt_properties result = NULL;
289
290         // Convert the service type to a string.
291         switch ( type )
292         {
293                 case consumer_type:
294                         service_type = "consumer";
295                         break;
296                 case filter_type:
297                         service_type = "filter";
298                         break;
299                 case producer_type:
300                         service_type = "producer";
301                         break;
302                 case transition_type:
303                         service_type = "transition";
304                         break;
305                 default:
306                         return NULL;
307         }
308         // Load the yaml file
309         snprintf( file, PATH_MAX, "%s/avformat/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
310         result = mlt_properties_parse_yaml( file );
311         if ( result && ( type == consumer_type || type == producer_type ) )
312         {
313                 // Annotate the yaml properties with AVOptions.
314                 mlt_properties params = (mlt_properties) mlt_properties_get_data( result, "parameters", NULL );
315                 AVFormatContext *avformat = avformat_alloc_context();
316                 AVCodecContext *avcodec = avcodec_alloc_context();
317                 int flags = ( type == consumer_type )? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
318
319                 add_parameters( params, avformat, flags, NULL, NULL );
320 #if LIBAVFORMAT_VERSION_MAJOR >= 53
321                 avformat_init();
322                 if ( type == producer_type )
323                 {
324                         AVInputFormat *f = NULL;
325                         while ( ( f = av_iformat_next( f ) ) )
326                                 if ( f->priv_class )
327                                         add_parameters( params, &f->priv_class, flags, NULL, f->name );
328                 }
329                 else
330                 {
331                         AVOutputFormat *f = NULL;
332                         while ( ( f = av_oformat_next( f ) ) )
333                                 if ( f->priv_class )
334                                         add_parameters( params, &f->priv_class, flags, NULL, f->name );
335                 }
336 #endif
337
338                 add_parameters( params, avcodec, flags, NULL, NULL );
339 #if LIBAVCODEC_VERSION_MAJOR >= 53
340                 AVCodec *c = NULL;
341                 while ( ( c = av_codec_next( c ) ) )
342                         if ( c->priv_class )
343                                 add_parameters( params, &c->priv_class, flags, NULL, c->name );
344 #endif
345
346                 av_free( avformat );
347                 av_free( avcodec );
348         }
349         return result;
350 }
351
352 MLT_REPOSITORY
353 {
354 #ifdef CODECS
355         MLT_REGISTER( consumer_type, "avformat", create_service );
356         MLT_REGISTER( producer_type, "avformat", create_service );
357         MLT_REGISTER( producer_type, "avformat-novalidate", create_service );
358         MLT_REGISTER_METADATA( consumer_type, "avformat", avformat_metadata, NULL );
359         MLT_REGISTER_METADATA( producer_type, "avformat", avformat_metadata, NULL );
360 #endif
361 #ifdef FILTERS
362         MLT_REGISTER( filter_type, "avcolour_space", create_service );
363         MLT_REGISTER( filter_type, "avcolor_space", create_service );
364         MLT_REGISTER( filter_type, "avdeinterlace", create_service );
365         MLT_REGISTER( filter_type, "avresample", create_service );
366 #ifdef SWSCALE
367         MLT_REGISTER( filter_type, "swscale", create_service );
368 #endif
369 #endif
370 }