2 * producer_loader.c -- auto-load producer by file name extension
3 * Copyright (C) 2003-2009 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
28 #include <framework/mlt.h>
30 static mlt_properties dictionary = NULL;
31 static mlt_properties normalisers = NULL;
33 static mlt_producer create_from( mlt_profile profile, char *file, char *services )
35 mlt_producer producer = NULL;
36 char *temp = strdup( services );
40 char *p = strchr( service, ',' );
43 producer = mlt_factory_producer( profile, service, file );
46 while ( producer == NULL && service != NULL );
51 static mlt_producer create_producer( mlt_profile profile, char *file )
53 mlt_producer result = NULL;
55 // 1st Line - check for service:resource handling
56 if ( strchr( file, ':' ) )
58 char *temp = strdup( file );
60 char *resource = strchr( temp, ':' );
62 result = mlt_factory_producer( profile, service, resource );
66 // 2nd Line preferences
70 char *lookup = strdup( file );
73 // We only need to load the dictionary once
74 if ( dictionary == NULL )
77 sprintf( temp, "%s/core/loader.dict", mlt_environment( "MLT_DATA" ) );
78 dictionary = mlt_properties_load( temp );
79 mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
82 // Convert the lookup string to lower case
89 // Iterate through the dictionary
90 for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
92 char *name = mlt_properties_get_name( dictionary, i );
93 if ( fnmatch( name, lookup, 0 ) == 0 )
94 result = create_from( profile, file, mlt_properties_get_value( dictionary, i ) );
100 // Finally, try just loading as service
101 if ( result == NULL )
102 result = mlt_factory_producer( profile, file, NULL );
107 static void create_filter( mlt_profile profile, mlt_producer producer, char *effect, int *created )
109 char *id = strdup( effect );
110 char *arg = strchr( id, ':' );
114 // The swscale and avcolor_space filters require resolution as arg to test compatibility
115 if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
116 arg = (char*) mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( producer ), "_real_width" );
118 mlt_filter filter = mlt_factory_filter( profile, id, arg );
119 if ( filter != NULL )
121 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
122 mlt_producer_attach( producer, filter );
123 mlt_filter_close( filter );
129 static void attach_normalisers( mlt_profile profile, mlt_producer producer )
135 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
137 // We only need to load the normalising properties once
138 if ( normalisers == NULL )
141 sprintf( temp, "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
142 normalisers = mlt_properties_load( temp );
143 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
147 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
151 char *value = mlt_properties_get_value( normalisers, i );
152 mlt_tokeniser_parse_new( tokeniser, value, "," );
153 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
154 create_filter( profile, producer, mlt_tokeniser_get_string( tokeniser, j ), &created );
157 // Close the tokeniser
158 mlt_tokeniser_close( tokeniser );
161 mlt_producer producer_loader_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
163 // Create the producer
164 mlt_producer producer = NULL;
165 mlt_properties properties = NULL;
168 producer = create_producer( profile, arg );
170 if ( producer != NULL )
171 properties = MLT_PRODUCER_PROPERTIES( producer );
173 // Attach filters if we have a producer and it isn't already xml'd :-)
174 if ( producer && strcmp( id, "abnormal" ) &&
175 mlt_properties_get( properties, "xml" ) == NULL &&
176 mlt_properties_get( properties, "_xml" ) == NULL &&
177 mlt_properties_get( properties, "loader_normalised" ) == NULL )
178 attach_normalisers( profile, producer );
182 // Always let the image and audio be converted
184 create_filter( profile, producer, "avcolor_space", &created );
186 create_filter( profile, producer, "imageconvert", &created );
187 create_filter( profile, producer, "audioconvert", &created );
190 // Now make sure we don't lose our identity
191 if ( properties != NULL )
192 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
194 // Return the producer