1 /*
2 * producer_loader.c -- auto-load producer by file name extension
3 * Copyright (C) 2003-2021 Meltytech, LLC
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <fnmatch.h>
25 #include <assert.h>
26
27 #include <framework/mlt.h>
28
29 static mlt_properties dictionary = NULL;
30 static mlt_properties normalisers = NULL;
31
create_from(mlt_profile profile,char * file,char * services)32 static mlt_producer create_from( mlt_profile profile, char *file, char *services )
33 {
34 mlt_producer producer = NULL;
35 char *temp = strdup( services );
36 char *service = temp;
37 do
38 {
39 char *p = strchr( service, ',' );
40 if ( p != NULL )
41 *p ++ = '\0';
42
43 // If the service name has a colon as field delimiter, then treat the
44 // second field as a prefix for the file/url.
45 char *prefix = strchr( service, ':' );
46 if ( prefix )
47 {
48 *prefix ++ = '\0';
49 char* prefix_file = calloc( 1, strlen( file ) + strlen( prefix ) + 1 );
50 strcpy( prefix_file, prefix );
51 strcat( prefix_file, file );
52 producer = mlt_factory_producer( profile, service, prefix_file );
53 free( prefix_file );
54 }
55 else
56 {
57 producer = mlt_factory_producer( profile, service, file );
58 }
59 service = p;
60 }
61 while ( producer == NULL && service != NULL );
62 free( temp );
63 return producer;
64 }
65
create_producer(mlt_profile profile,char * file)66 static mlt_producer create_producer( mlt_profile profile, char *file )
67 {
68 mlt_producer result = NULL;
69
70 // 1st Line - check for service:resource handling
71 // And ignore drive letters on Win32 - no single char services supported.
72 if ( strchr( file, ':' ) > file + 1 )
73 {
74 char *temp = strdup( file );
75 char *service = temp;
76 char *resource = strchr( temp, ':' );
77 *resource ++ = '\0';
78 result = mlt_factory_producer( profile, service, resource );
79 free( temp );
80 }
81
82 // 2nd Line preferences
83 if ( result == NULL )
84 {
85 int i = 0;
86 char *lookup = strdup( file );
87 char *p = lookup;
88
89 // Make backup of profile for determining if we need to use 'consumer' producer.
90 mlt_profile backup_profile = mlt_profile_clone( profile );
91
92 // We only need to load the dictionary once
93 if ( dictionary == NULL )
94 {
95 char temp[ 1024 ];
96 sprintf( temp, "%s/core/loader.dict", mlt_environment( "MLT_DATA" ) );
97 dictionary = mlt_properties_load( temp );
98 mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
99 }
100
101 // Convert the lookup string to lower case
102 while ( *p )
103 {
104 *p = tolower( *p );
105 p ++;
106 }
107
108 // Chop off the query string
109 p = strrchr( lookup, '?' );
110 if ( p && p > lookup && p[-1] == '\\' )
111 p[-1] = '\0';
112
113 // Strip file:// prefix
114 p = lookup;
115 if ( strncmp( lookup, "file://", 7 ) == 0 )
116 p += 7;
117
118 // Iterate through the dictionary
119 for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
120 {
121 char *name = mlt_properties_get_name( dictionary, i );
122 if ( fnmatch( name, p, 0 ) == 0 )
123 result = create_from( profile, file, mlt_properties_get_value( dictionary, i ) );
124 }
125
126 // Check if the producer changed the profile - xml does this.
127 // The consumer producer does not handle frame rate differences.
128 if ( result && backup_profile->is_explicit && (
129 profile->width != backup_profile->width ||
130 profile->height != backup_profile->height ||
131 profile->sample_aspect_num != backup_profile->sample_aspect_num ||
132 profile->sample_aspect_den != backup_profile->sample_aspect_den ||
133 profile->colorspace != backup_profile->colorspace ) )
134 {
135 // Restore the original profile attributes.
136 profile->display_aspect_den = backup_profile->display_aspect_den;
137 profile->display_aspect_num = backup_profile->display_aspect_num;
138 profile->frame_rate_den = backup_profile->frame_rate_den;
139 profile->frame_rate_num = backup_profile->frame_rate_num;
140 profile->height = backup_profile->height;
141 profile->progressive = backup_profile->progressive;
142 profile->sample_aspect_den = backup_profile->sample_aspect_den;
143 profile->sample_aspect_num = backup_profile->sample_aspect_num;
144 profile->width = backup_profile->width;
145
146 // Use the 'consumer' producer.
147 mlt_producer_close( result );
148 result = mlt_factory_producer( profile, "consumer", file );
149 }
150
151 mlt_profile_close( backup_profile );
152 free( lookup );
153 }
154
155 // Finally, try just loading as service
156 if ( result == NULL )
157 result = mlt_factory_producer( profile, file, NULL );
158
159 return result;
160 }
161
create_filter(mlt_profile profile,mlt_producer producer,char * effect,int * created)162 static void create_filter( mlt_profile profile, mlt_producer producer, char *effect, int *created )
163 {
164 mlt_filter filter;
165 char *id = strdup( effect );
166 char *arg = strchr( id, ':' );
167 if ( arg != NULL )
168 *arg ++ = '\0';
169
170 filter = mlt_factory_filter( profile, id, arg );
171 if ( filter )
172 {
173 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
174 mlt_producer_attach( producer, filter );
175 mlt_filter_close( filter );
176 *created = 1;
177 }
178 free( id );
179 }
180
attach_normalisers(mlt_profile profile,mlt_producer producer)181 static void attach_normalisers( mlt_profile profile, mlt_producer producer )
182 {
183 // Loop variable
184 int i;
185
186 // Tokeniser
187 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
188
189 // We only need to load the normalising properties once
190 if ( normalisers == NULL )
191 {
192 char temp[ 1024 ];
193 sprintf( temp, "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
194 normalisers = mlt_properties_load( temp );
195 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
196 }
197
198 // Apply normalisers
199 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
200 {
201 int j = 0;
202 int created = 0;
203 char *value = mlt_properties_get_value( normalisers, i );
204 mlt_tokeniser_parse_new( tokeniser, value, "," );
205 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
206 create_filter( profile, producer, mlt_tokeniser_get_string( tokeniser, j ), &created );
207 }
208
209 // Close the tokeniser
210 mlt_tokeniser_close( tokeniser );
211 }
212
producer_loader_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)213 mlt_producer producer_loader_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
214 {
215 // Create the producer
216 mlt_producer producer = NULL;
217 mlt_properties properties = NULL;
218
219 if ( arg != NULL )
220 producer = create_producer( profile, arg );
221
222 if ( producer != NULL )
223 properties = MLT_PRODUCER_PROPERTIES( producer );
224
225 // Attach filters if we have a producer and it isn't already xml'd :-)
226 if ( producer && strcmp( id, "abnormal" ) &&
227 strncmp( arg, "abnormal:", 9 ) &&
228 mlt_properties_get( properties, "xml" ) == NULL &&
229 mlt_properties_get( properties, "_xml" ) == NULL &&
230 mlt_properties_get( properties, "loader_normalised" ) == NULL )
231 attach_normalisers( profile, producer );
232
233 if ( producer && mlt_service_identify( MLT_PRODUCER_SERVICE( producer ) ) != mlt_service_chain_type )
234 {
235 // Always let the image and audio be converted
236 int created = 0;
237 // movit.convert skips setting the frame->convert_image pointer if GLSL cannot be used.
238 create_filter( profile, producer, "movit.convert", &created );
239 // avcolor_space and imageconvert only set frame->convert_image if it has not been set.
240 create_filter( profile, producer, "avcolor_space", &created );
241 if ( !created )
242 create_filter( profile, producer, "imageconvert", &created );
243 create_filter( profile, producer, "audioconvert", &created );
244 }
245
246 // Now make sure we don't lose our identity
247 if ( properties != NULL )
248 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
249
250 // Return the producer
251 return producer;
252 }
253