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