obscurer filter, consistency mods and bug fixes
[melted] / src / modules / inigo / producer_inigo.c
1 /*
2 * producer_inigo.c -- simple inigo test case
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_inigo.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <framework/mlt.h>
28
29 static mlt_producer parse_inigo( char *file )
30 {
31 FILE *input = fopen( file, "r" );
32 char **args = calloc( sizeof( char * ), 1000 );
33 int count = 0;
34 char temp[ 2048 ];
35
36 if ( input != NULL )
37 {
38 while( fgets( temp, 2048, input ) )
39 {
40 temp[ strlen( temp ) - 1 ] = '\0';
41 if ( strcmp( temp, "" ) )
42 args[ count ++ ] = strdup( temp );
43 }
44 }
45
46 mlt_producer result = producer_inigo_init( args );
47
48 if ( result != NULL )
49 {
50 mlt_properties properties = mlt_producer_properties( result );
51 mlt_properties_set( properties, "resource", file );
52 }
53
54 while( count -- )
55 free( args[ count ] );
56 free( args );
57
58 return result;
59 }
60
61 static mlt_producer create_producer( char *file )
62 {
63 mlt_producer result = NULL;
64
65 // 1st Line preferences
66 if ( strstr( file, ".inigo" ) )
67 result = parse_inigo( file );
68 else if ( strstr( file, ".mpg" ) )
69 result = mlt_factory_producer( "mcmpeg", file );
70 else if ( strstr( file, ".mpeg" ) )
71 result = mlt_factory_producer( "mcmpeg", file );
72 else if ( strstr( file, ".dv" ) )
73 result = mlt_factory_producer( "mcdv", file );
74 else if ( strstr( file, ".dif" ) )
75 result = mlt_factory_producer( "mcdv", file );
76 else if ( strstr( file, ".jpg" ) )
77 result = mlt_factory_producer( "pixbuf", file );
78 else if ( strstr( file, ".JPG" ) )
79 result = mlt_factory_producer( "pixbuf", file );
80 else if ( strstr( file, ".jpeg" ) )
81 result = mlt_factory_producer( "pixbuf", file );
82 else if ( strstr( file, ".png" ) )
83 result = mlt_factory_producer( "pixbuf", file );
84 else if ( strstr( file, ".txt" ) )
85 result = mlt_factory_producer( "pango", file );
86 else if ( strstr( file, ".westley" ) )
87 result = mlt_factory_producer( "westley", file );
88 else if ( strstr( file, ".ogg" ) )
89 result = mlt_factory_producer( "vorbis", file );
90
91 // 2nd Line fallbacks
92 if ( result == NULL && strstr( file, ".dv" ) )
93 result = mlt_factory_producer( "libdv", file );
94 else if ( result == NULL && strstr( file, ".dif" ) )
95 result = mlt_factory_producer( "libdv", file );
96
97 // 3rd line fallbacks
98 if ( result == NULL )
99 result = mlt_factory_producer( "avformat", file );
100
101 // 4th line fallbacks
102 if ( result == NULL )
103 result = mlt_factory_producer( "ffmpeg", file );
104
105 return result;
106 }
107
108 static void track_service( mlt_field field, void *service, mlt_destructor destructor )
109 {
110 mlt_properties properties = mlt_field_properties( field );
111 int registered = mlt_properties_get_int( properties, "registered" );
112 char *key = mlt_properties_get( properties, "registered" );
113 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
114 mlt_properties_set_int( properties, "registered", ++ registered );
115 }
116
117 static mlt_filter create_filter( mlt_field field, char *id, int track )
118 {
119 char *arg = strchr( id, ':' );
120 if ( arg != NULL )
121 *arg ++ = '\0';
122 mlt_filter filter = mlt_factory_filter( id, arg );
123 if ( filter != NULL )
124 {
125 mlt_field_plant_filter( field, filter, track );
126 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
127 }
128 return filter;
129 }
130
131 static mlt_transition create_transition( mlt_field field, char *id, int track )
132 {
133 char *arg = strchr( id, ':' );
134 if ( arg != NULL )
135 *arg ++ = '\0';
136 mlt_transition transition = mlt_factory_transition( id, arg );
137 if ( transition != NULL )
138 {
139 mlt_field_plant_transition( field, transition, track, track + 1 );
140 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
141 }
142 return transition;
143 }
144
145 mlt_producer producer_inigo_init( char **argv )
146 {
147 int i;
148 int track = 0;
149 mlt_producer producer = NULL;
150 mlt_playlist playlist = mlt_playlist_init( );
151 mlt_properties group = mlt_properties_new( );
152 mlt_properties properties = group;
153 mlt_field field = mlt_field_init( );
154 mlt_properties field_properties = mlt_field_properties( field );
155 mlt_multitrack multitrack = mlt_field_multitrack( field );
156
157 // We need to track the number of registered filters
158 mlt_properties_set_int( field_properties, "registered", 0 );
159
160 // Parse the arguments
161 for ( i = 0; argv[ i ] != NULL; i ++ )
162 {
163 if ( !strcmp( argv[ i ], "-group" ) )
164 {
165 if ( mlt_properties_count( group ) != 0 )
166 {
167 mlt_properties_close( group );
168 group = mlt_properties_new( );
169 }
170 if ( group != NULL )
171 properties = group;
172 }
173 else if ( !strcmp( argv[ i ], "-filter" ) )
174 {
175 mlt_filter filter = create_filter( field, argv[ ++ i ], track );
176 if ( filter != NULL )
177 {
178 properties = mlt_filter_properties( filter );
179 mlt_properties_inherit( properties, group );
180 }
181 }
182 else if ( !strcmp( argv[ i ], "-transition" ) )
183 {
184 mlt_transition transition = create_transition( field, argv[ ++ i ], track );
185 if ( transition != NULL )
186 {
187 properties = mlt_transition_properties( transition );
188 mlt_properties_inherit( properties, group );
189 }
190 }
191 else if ( !strcmp( argv[ i ], "-blank" ) )
192 {
193 if ( producer != NULL )
194 mlt_playlist_append( playlist, producer );
195 producer = NULL;
196 mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
197 }
198 else if ( !strcmp( argv[ i ], "-track" ) )
199 {
200 if ( producer != NULL )
201 mlt_playlist_append( playlist, producer );
202 producer = NULL;
203 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
204 playlist = mlt_playlist_init( );
205 }
206 else if ( strstr( argv[ i ], "=" ) )
207 {
208 mlt_properties_parse( properties, argv[ i ] );
209 }
210 else if ( argv[ i ][ 0 ] != '-' )
211 {
212 if ( producer != NULL )
213 mlt_playlist_append( playlist, producer );
214 producer = create_producer( argv[ i ] );
215 if ( producer != NULL )
216 {
217 properties = mlt_producer_properties( producer );
218 mlt_properties_inherit( properties, group );
219 }
220 }
221 else
222 {
223 if ( !strcmp( argv[ i ], "-serialise" ) )
224 i += 2;
225 else if ( !strcmp( argv[ i ], "-consumer" ) )
226 i += 2;
227
228 while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
229 i ++;
230
231 i --;
232 }
233 }
234
235 // Connect producer to playlist
236 if ( producer != NULL )
237 mlt_playlist_append( playlist, producer );
238
239 // We must have a producer at this point
240 if ( mlt_playlist_count( playlist ) > 0 )
241 {
242 // Connect multitrack to producer
243 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
244 }
245
246 mlt_tractor tractor = mlt_field_tractor( field );
247 mlt_producer prod = mlt_tractor_producer( tractor );
248 mlt_properties props = mlt_tractor_properties( tractor );
249 mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
250 mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
251 mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
252 mlt_properties_set_position( props, "length", mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) + 1 );
253 mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) );
254 mlt_properties_set_double( props, "fps", mlt_producer_get_fps( mlt_multitrack_producer( multitrack ) ) );
255
256 return mlt_tractor_producer( tractor );
257 }
258