]> git.sesse.net Git - mlt/blob - src/modules/core/producer_loader.c
Remove unused string.h include
[mlt] / src / modules / core / producer_loader.c
1 /*
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>
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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <fnmatch.h>
26 #include <assert.h>
27
28 #include <framework/mlt.h>
29
30 static mlt_properties dictionary = NULL;
31 static mlt_properties normalisers = NULL;
32
33 static mlt_producer create_from( mlt_profile profile, char *file, char *services )
34 {
35         mlt_producer producer = NULL;
36         char *temp = strdup( services );
37         char *service = temp;
38         do
39         {
40                 char *p = strchr( service, ',' );
41                 if ( p != NULL )
42                         *p ++ = '\0';
43
44                 // If  the service name has a colon as field delimiter, then treat the
45                 // second field as a prefix for the file/url.
46                 char *prefix = strchr( service, ':' );
47                 if ( prefix )
48                 {
49                         *prefix ++ = '\0';
50                         char* prefix_file = calloc( 1, strlen( file ) + strlen( prefix ) + 1 );
51                         strcpy( prefix_file, prefix );
52                         strcat( prefix_file, file );
53                         producer = mlt_factory_producer( profile, service, prefix_file );
54                         free( prefix_file );
55                 }
56                 else
57                 {
58                         producer = mlt_factory_producer( profile, service, file );
59                 }
60                 service = p;
61         }
62         while ( producer == NULL && service != NULL );
63         free( temp );
64         return producer;
65 }
66
67 static mlt_producer create_producer( mlt_profile profile, char *file )
68 {
69         mlt_producer result = NULL;
70
71         // 1st Line - check for service:resource handling
72         // And ignore drive letters on Win32 - no single char services supported.
73         if ( strchr( file, ':' ) > file + 1 )
74         {
75                 char *temp = strdup( file );
76                 char *service = temp;
77                 char *resource = strchr( temp, ':' );
78                 *resource ++ = '\0';
79                 result = mlt_factory_producer( profile, service, resource );
80                 free( temp );
81         }
82
83         // 2nd Line preferences
84         if ( result == NULL )
85         {
86                 int i = 0;
87                 char *lookup = strdup( file );
88                 char *p = lookup;
89
90                 // Make backup of profile for determining if we need to use 'consumer' producer.
91                 mlt_profile backup_profile = mlt_profile_clone( profile );
92
93                 // We only need to load the dictionary once
94                 if ( dictionary == NULL )
95                 {
96                         char temp[ 1024 ];
97                         sprintf( temp, "%s/core/loader.dict", mlt_environment( "MLT_DATA" ) );
98                         dictionary = mlt_properties_load( temp );
99                         mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
100                 }
101
102                 // Convert the lookup string to lower case
103                 while ( *p )
104                 {
105                         *p = tolower( *p );
106                         p ++;
107                 }
108
109                 // Chop off the query string
110                 p = strrchr( lookup, '?' );
111                 if ( p )
112                         p[0] = '\0';
113
114                 // Strip file:// prefix
115                 p = lookup;
116                 if ( strncmp( lookup, "file://", 7 ) == 0 )
117                         p += 7;
118                         
119                 // Iterate through the dictionary
120                 for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
121                 {
122                         char *name = mlt_properties_get_name( dictionary, i );
123                         if ( fnmatch( name, p, 0 ) == 0 )
124                                 result = create_from( profile, file, mlt_properties_get_value( dictionary, i ) );
125                 }       
126
127                 // Check if the producer changed the profile - xml does this.
128                 // The consumer producer does not handle frame rate differences.
129                 if ( result && backup_profile->is_explicit && (
130                      profile->width != backup_profile->width ||
131                      profile->height != backup_profile->height ||
132                      profile->sample_aspect_num != backup_profile->sample_aspect_num ||
133                      profile->sample_aspect_den != backup_profile->sample_aspect_den ||
134                      profile->colorspace != backup_profile->colorspace ) )
135                 {
136                         // Restore the original profile attributes.
137                         profile->display_aspect_den = backup_profile->display_aspect_den;
138                         profile->display_aspect_num = backup_profile->display_aspect_num;
139                         profile->frame_rate_den = backup_profile->frame_rate_den;
140                         profile->frame_rate_num = backup_profile->frame_rate_num;
141                         profile->height = backup_profile->height;
142                         profile->progressive = backup_profile->progressive;
143                         profile->sample_aspect_den = backup_profile->sample_aspect_den;
144                         profile->sample_aspect_num = backup_profile->sample_aspect_num;
145                         profile->width = backup_profile->width;
146
147                         // Use the 'consumer' producer.
148                         mlt_producer_close( result );
149                         result = mlt_factory_producer( profile, "consumer", file );
150                 }
151
152                 mlt_profile_close( backup_profile );
153                 free( lookup );
154         }
155
156         // Finally, try just loading as service
157         if ( result == NULL )
158                 result = mlt_factory_producer( profile, file, NULL );
159
160         return result;
161 }
162
163 static void create_filter( mlt_profile profile, mlt_producer producer, char *effect, int *created )
164 {
165         mlt_filter filter;
166         char *id = strdup( effect );
167         char *arg = strchr( id, ':' );
168         if ( arg != NULL )
169                 *arg ++ = '\0';
170
171         // The swscale and avcolor_space filters require resolution as arg to test compatibility
172         if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
173         {
174                 int width = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.media.width" );
175                 filter = mlt_factory_filter( profile, id, &width );
176         }
177         else
178         {
179                 filter = mlt_factory_filter( profile, id, arg );
180         }
181         if ( filter )
182         {
183                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
184                 mlt_producer_attach( producer, filter );
185                 mlt_filter_close( filter );
186                 *created = 1;
187         }
188         free( id );
189 }
190
191 static void attach_normalisers( mlt_profile profile, mlt_producer producer )
192 {
193         // Loop variable
194         int i;
195
196         // Tokeniser
197         mlt_tokeniser tokeniser = mlt_tokeniser_init( );
198
199         // We only need to load the normalising properties once
200         if ( normalisers == NULL )
201         {
202                 char temp[ 1024 ];
203                 sprintf( temp, "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
204                 normalisers = mlt_properties_load( temp );
205                 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
206         }
207
208         // Apply normalisers
209         for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
210         {
211                 int j = 0;
212                 int created = 0;
213                 char *value = mlt_properties_get_value( normalisers, i );
214                 mlt_tokeniser_parse_new( tokeniser, value, "," );
215                 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
216                         create_filter( profile, producer, mlt_tokeniser_get_string( tokeniser, j ), &created );
217         }
218
219         // Close the tokeniser
220         mlt_tokeniser_close( tokeniser );
221 }
222
223 mlt_producer producer_loader_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
224 {
225         // Create the producer 
226         mlt_producer producer = NULL;
227         mlt_properties properties = NULL;
228
229         if ( arg != NULL )
230                 producer = create_producer( profile, arg );
231
232         if ( producer != NULL )
233                 properties = MLT_PRODUCER_PROPERTIES( producer );
234
235         // Attach filters if we have a producer and it isn't already xml'd :-)
236         if ( producer && strcmp( id, "abnormal" ) &&
237                 strncmp( arg, "abnormal:", 9 ) &&
238                 mlt_properties_get( properties, "xml" ) == NULL &&
239                 mlt_properties_get( properties, "_xml" ) == NULL &&
240                 mlt_properties_get( properties, "loader_normalised" ) == NULL )
241                 attach_normalisers( profile, producer );
242         
243         if ( producer )
244         {
245                 // Always let the image and audio be converted
246                 int created = 0;
247                 // movit.convert skips setting the frame->convert_image pointer if GLSL cannot be used.
248                 create_filter( profile, producer, "movit.convert", &created );
249                 // avcolor_space and imageconvert only set frame->convert_image if it has not been set.
250                 create_filter( profile, producer, "avcolor_space", &created );
251                 if ( !created )
252                         create_filter( profile, producer, "imageconvert", &created );
253                 create_filter( profile, producer, "audioconvert", &created );
254         }
255
256         // Now make sure we don't lose our identity
257         if ( properties != NULL )
258                 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
259
260         // Return the producer
261         return producer;
262 }