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