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