f5350c82aac18339d9b6df9ad5c6768fd1ce1137
[melted] / 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
27 #include <framework/mlt.h>
28
29 static mlt_properties dictionary = NULL;
30 static mlt_properties normalisers = NULL;
31
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 producer = mlt_factory_producer( profile, service, file );
43 service = p;
44 }
45 while ( producer == NULL && service != NULL );
46 free( temp );
47 return producer;
48 }
49
50 static mlt_producer create_producer( mlt_profile profile, char *file )
51 {
52 mlt_producer result = NULL;
53
54 // 1st Line - check for service:resource handling
55 if ( strchr( file, ':' ) )
56 {
57 char *temp = strdup( file );
58 char *service = temp;
59 char *resource = strchr( temp, ':' );
60 *resource ++ = '\0';
61 result = mlt_factory_producer( profile, service, resource );
62 free( temp );
63 }
64
65 // 2nd Line preferences
66 if ( result == NULL )
67 {
68 int i = 0;
69 char *lookup = strdup( file );
70 char *p = lookup;
71
72 // We only need to load the dictionary once
73 if ( dictionary == NULL )
74 {
75 char temp[ 1024 ];
76 sprintf( temp, "%s/fezzik.dict", mlt_factory_prefix( ) );
77 dictionary = mlt_properties_load( temp );
78 mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
79 }
80
81 // Convert the lookup string to lower case
82 while ( *p )
83 {
84 *p = tolower( *p );
85 p ++;
86 }
87
88 // Iterate through the dictionary
89 for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
90 {
91 char *name = mlt_properties_get_name( dictionary, i );
92 if ( fnmatch( name, lookup, 0 ) == 0 )
93 result = create_from( profile, file, mlt_properties_get_value( dictionary, i ) );
94 }
95
96 free( lookup );
97 }
98
99 // Finally, try just loading as service
100 if ( result == NULL )
101 result = mlt_factory_producer( profile, file, NULL );
102
103 return result;
104 }
105
106 static void create_filter( mlt_profile profile, mlt_producer producer, char *effect, int *created )
107 {
108 char *id = strdup( effect );
109 char *arg = strchr( id, ':' );
110 if ( arg != NULL )
111 *arg ++ = '\0';
112 mlt_filter filter = mlt_factory_filter( profile, id, arg );
113 if ( filter != NULL )
114 {
115 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik", 1 );
116 mlt_producer_attach( producer, filter );
117 mlt_filter_close( filter );
118 *created = 1;
119 }
120 free( id );
121 }
122
123 static void attach_normalisers( mlt_profile profile, mlt_producer producer )
124 {
125 // Loop variable
126 int i;
127
128 // Tokeniser
129 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
130
131 // We only need to load the normalising properties once
132 if ( normalisers == NULL )
133 {
134 char temp[ 1024 ];
135 sprintf( temp, "%s/fezzik.ini", mlt_factory_prefix( ) );
136 normalisers = mlt_properties_load( temp );
137 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
138 }
139
140 // Apply normalisers
141 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
142 {
143 int j = 0;
144 int created = 0;
145 char *value = mlt_properties_get_value( normalisers, i );
146 mlt_tokeniser_parse_new( tokeniser, value, "," );
147 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
148 create_filter( profile, producer, mlt_tokeniser_get_string( tokeniser, j ), &created );
149 }
150
151 // Close the tokeniser
152 mlt_tokeniser_close( tokeniser );
153 }
154
155 mlt_producer producer_fezzik_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
156 {
157 // Create the producer
158 mlt_producer producer = NULL;
159 mlt_properties properties = NULL;
160
161 if ( arg != NULL )
162 producer = create_producer( profile, arg );
163
164 if ( producer != NULL )
165 properties = MLT_PRODUCER_PROPERTIES( producer );
166
167 // Attach filters if we have a producer and it isn't already westley'd :-)
168 if ( producer != NULL && mlt_properties_get( properties, "westley" ) == NULL && mlt_properties_get( properties, "_westley" ) == NULL )
169 attach_normalisers( profile, producer );
170
171 // Now make sure we don't lose our identity
172 if ( properties != NULL )
173 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
174
175 // Return the producer
176 return producer;
177 }