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