Fix for deep westleys and filter in/out points
[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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "producer_fezzik.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <fnmatch.h>
28
29 #include <framework/mlt.h>
30
31 static mlt_properties dictionary = NULL;
32 static mlt_properties normalisers = NULL;
33
34 static mlt_producer create_from( char *file, char *services )
35 {
36 mlt_producer producer = NULL;
37 char *temp = strdup( services );
38 char *service = temp;
39 do
40 {
41 char *p = strchr( service, ',' );
42 if ( p != NULL )
43 *p ++ = '\0';
44 producer = mlt_factory_producer( service, file );
45 service = p;
46 }
47 while ( producer == NULL && service != NULL );
48 free( temp );
49 return producer;
50 }
51
52 static mlt_producer create_producer( char *file )
53 {
54 mlt_producer result = NULL;
55
56 // 1st Line - check for service:resource handling
57 if ( strchr( file, ':' ) )
58 {
59 char *temp = strdup( file );
60 char *service = temp;
61 char *resource = strchr( temp, ':' );
62 *resource ++ = '\0';
63 result = mlt_factory_producer( 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 // We only need to load the dictionary once
75 if ( dictionary == NULL )
76 {
77 char temp[ 1024 ];
78 sprintf( temp, "%s/fezzik.dict", mlt_factory_prefix( ) );
79 dictionary = mlt_properties_load( temp );
80 mlt_factory_register_for_clean_up( dictionary, ( mlt_destructor )mlt_properties_close );
81 }
82
83 // Convert the lookup string to lower case
84 while ( *p )
85 {
86 *p = tolower( *p );
87 p ++;
88 }
89
90 // Iterate through the dictionary
91 for ( i = 0; result == NULL && i < mlt_properties_count( dictionary ); i ++ )
92 {
93 char *name = mlt_properties_get_name( dictionary, i );
94 if ( fnmatch( name, lookup, 0 ) == 0 )
95 result = create_from( file, mlt_properties_get_value( dictionary, i ) );
96 }
97
98 free( lookup );
99 }
100
101 // Finally, try just loading as service
102 if ( result == NULL )
103 result = mlt_factory_producer( file, NULL );
104
105 return result;
106 }
107
108 static void create_filter( mlt_producer producer, char *effect, int *created )
109 {
110 char *id = strdup( effect );
111 char *arg = strchr( id, ':' );
112 if ( arg != NULL )
113 *arg ++ = '\0';
114 mlt_filter filter = mlt_factory_filter( id, arg );
115 if ( filter != NULL )
116 {
117 mlt_properties_set_int( mlt_filter_properties( filter ), "_fezzik", 1 );
118 mlt_producer_attach( producer, filter );
119 mlt_filter_close( filter );
120 *created = 1;
121 }
122 free( id );
123 }
124
125 static void attach_normalisers( mlt_producer producer )
126 {
127 // Loop variable
128 int i;
129
130 // Tokeniser
131 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
132
133 // We only need to load the normalising properties once
134 if ( normalisers == NULL )
135 {
136 char temp[ 1024 ];
137 sprintf( temp, "%s/fezzik.ini", mlt_factory_prefix( ) );
138 normalisers = mlt_properties_load( temp );
139 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
140 }
141
142 // Apply normalisers
143 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
144 {
145 int j = 0;
146 int created = 0;
147 char *value = mlt_properties_get_value( normalisers, i );
148 mlt_tokeniser_parse_new( tokeniser, value, "," );
149 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
150 create_filter( producer, mlt_tokeniser_get_string( tokeniser, j ), &created );
151 }
152
153 // Close the tokeniser
154 mlt_tokeniser_close( tokeniser );
155 }
156
157 mlt_producer producer_fezzik_init( char *arg )
158 {
159 // Create the producer
160 mlt_producer producer = NULL;
161 mlt_properties properties = NULL;
162
163 if ( arg != NULL )
164 producer = create_producer( arg );
165
166 if ( producer != NULL )
167 properties = mlt_producer_properties( producer );
168
169 // Attach filters if we have a producer and it isn't already westley'd :-)
170 if ( producer != NULL && mlt_properties_get( properties, "westley" ) == NULL && mlt_properties_get( properties, "_westley" ) == NULL )
171 attach_normalisers( producer );
172
173 // Now make sure we don't lose our identity
174 if ( properties != NULL )
175 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
176
177 // Return the producer
178 return producer;
179 }