]> git.sesse.net Git - mlt/blob - src/modules/fezzik/producer_fezzik.c
816c1bd70a2210f44a904f5acdf6049bec6fb0db
[mlt] / src / modules / fezzik / producer_fezzik.c
1 /*
2  * producer_fezzik.c -- a normalising filter
3  * Copyright (C) 2003-2004 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                 // We only need to load the dictionary once
74                 if ( dictionary == NULL )
75                 {
76                         char temp[ 1024 ];
77                         sprintf( temp, "%s/fezzik.dict", mlt_environment( "MLT_DATA" ) );
78                         dictionary = mlt_properties_load( temp );
79                         mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
80                 }
81
82                 // Convert the lookup string to lower case
83                 while ( *p )
84                 {
85                         *p = tolower( *p );
86                         p ++;
87                 }
88
89                 // Iterate through the dictionary
90                 for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
91                 {
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 ) );
95                 }
96
97                 free( lookup );
98         }
99
100         // Finally, try just loading as service
101         if ( result == NULL )
102                 result = mlt_factory_producer( profile, file, NULL );
103
104         return result;
105 }
106
107 static void create_filter( mlt_profile profile, mlt_producer producer, char *effect, int *created )
108 {
109         // The swscale filter can not handle images with a width > 2048 and the
110         // sdl_image producer does not scale on its own
111         if ( strncmp( effect, "swscale", 7 ) == 0 &&
112              mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( producer ), "_real_width" ) > 2048 &&
113              strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" ), "sdl_image" ) == 0 )
114                 return;
115
116         char *id = strdup( effect );
117         char *arg = strchr( id, ':' );
118         if ( arg != NULL )
119                 *arg ++ = '\0';
120         mlt_filter filter = mlt_factory_filter( profile, id, arg );
121         if ( filter != NULL )
122         {
123                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik", 1 );
124                 mlt_producer_attach( producer, filter );
125                 mlt_filter_close( filter );
126                 *created = 1;
127         }
128         free( id );
129 }
130
131 static void attach_normalisers( mlt_profile profile, mlt_producer producer )
132 {
133         // Loop variable
134         int i;
135
136         // Tokeniser
137         mlt_tokeniser tokeniser = mlt_tokeniser_init( );
138
139         // We only need to load the normalising properties once
140         if ( normalisers == NULL )
141         {
142                 char temp[ 1024 ];
143                 sprintf( temp, "%s/fezzik.ini", mlt_environment( "MLT_DATA" ) );
144                 normalisers = mlt_properties_load( temp );
145                 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
146         }
147
148         // Apply normalisers
149         for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
150         {
151                 int j = 0;
152                 int created = 0;
153                 char *value = mlt_properties_get_value( normalisers, i );
154                 mlt_tokeniser_parse_new( tokeniser, value, "," );
155                 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
156                         create_filter( profile, producer, mlt_tokeniser_get_string( tokeniser, j ), &created );
157         }
158
159         // Close the tokeniser
160         mlt_tokeniser_close( tokeniser );
161 }
162
163 mlt_producer producer_fezzik_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
164 {
165         // Create the producer 
166         mlt_producer producer = NULL;
167         mlt_properties properties = NULL;
168
169         if ( arg != NULL )
170                 producer = create_producer( profile, arg );
171
172         if ( producer != NULL )
173                 properties = MLT_PRODUCER_PROPERTIES( producer );
174
175         // Attach filters if we have a producer and it isn't already westley'd :-)
176         if ( producer != NULL && mlt_properties_get( properties, "westley" ) == NULL && \
177                 mlt_properties_get( properties, "_westley" ) == NULL && \
178                 mlt_properties_get( properties, "fezzik_normalised" ) == NULL )
179                 attach_normalisers( profile, producer );
180
181         // Now make sure we don't lose our identity
182         if ( properties != NULL )
183                 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
184
185         // Return the producer
186         return producer;
187 }