4fd82bd45d1bf01bc1d738321ca10d656e62afeb
[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 mlt_producer producer_inigo_file_init( 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 void track_service( mlt_field field, void *service, mlt_destructor destructor )
62 {
63 mlt_properties properties = mlt_field_properties( field );
64 int registered = mlt_properties_get_int( properties, "registered" );
65 char *key = mlt_properties_get( properties, "registered" );
66 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
67 mlt_properties_set_int( properties, "registered", ++ registered );
68 }
69
70 static mlt_producer create_producer( mlt_field field, char *file )
71 {
72 mlt_producer result = mlt_factory_producer( "fezzik", file );
73
74 if ( result != NULL )
75 track_service( field, result, ( mlt_destructor )mlt_producer_close );
76
77 return result;
78 }
79
80 static mlt_filter create_attach( mlt_field field, char *id, int track )
81 {
82 char *arg = strchr( id, ':' );
83 if ( arg != NULL )
84 *arg ++ = '\0';
85 mlt_filter filter = mlt_factory_filter( id, arg );
86 if ( filter != NULL )
87 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
88 return filter;
89 }
90
91 static mlt_filter create_filter( mlt_field field, char *id, int track )
92 {
93 char *arg = strchr( id, ':' );
94 if ( arg != NULL )
95 *arg ++ = '\0';
96 mlt_filter filter = mlt_factory_filter( id, arg );
97 if ( filter != NULL )
98 {
99 mlt_field_plant_filter( field, filter, track );
100 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
101 }
102 return filter;
103 }
104
105 static mlt_transition create_transition( mlt_field field, char *id, int track )
106 {
107 char *arg = strchr( id, ':' );
108 if ( arg != NULL )
109 *arg ++ = '\0';
110 mlt_transition transition = mlt_factory_transition( id, arg );
111 if ( transition != NULL )
112 {
113 mlt_field_plant_transition( field, transition, track, track + 1 );
114 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
115 }
116 return transition;
117 }
118
119 mlt_producer producer_inigo_init( char **argv )
120 {
121 int i;
122 int track = 0;
123 mlt_producer producer = NULL;
124 mlt_tractor mix = NULL;
125 mlt_playlist playlist = mlt_playlist_init( );
126 mlt_properties group = mlt_properties_new( );
127 mlt_properties properties = group;
128 mlt_tractor tractor = mlt_tractor_new( );
129 mlt_field field = mlt_tractor_field( tractor );
130 mlt_properties field_properties = mlt_field_properties( field );
131 mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
132 char *title = NULL;
133
134 // We need to track the number of registered filters
135 mlt_properties_set_int( field_properties, "registered", 0 );
136
137 // Parse the arguments
138 for ( i = 0; argv[ i ] != NULL; i ++ )
139 {
140 if ( !strcmp( argv[ i ], "-group" ) )
141 {
142 if ( mlt_properties_count( group ) != 0 )
143 {
144 mlt_properties_close( group );
145 group = mlt_properties_new( );
146 }
147 if ( group != NULL )
148 properties = group;
149 }
150 else if ( !strcmp( argv[ i ], "-attach" ) )
151 {
152 mlt_filter filter = create_attach( field, argv[ ++ i ], track );
153 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
154 mlt_playlist_append( playlist, producer );
155 producer = NULL;
156 if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
157 {
158 mlt_playlist_clip_info info;
159 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
160 producer = info.cut;
161 mlt_service_attach( ( mlt_service )producer, filter );
162 properties = mlt_filter_properties( filter );
163 mlt_properties_inherit( properties, group );
164 }
165 }
166 else if ( !strcmp( argv[ i ], "-repeat" ) )
167 {
168 int repeat = atoi( argv[ ++ i ] );
169 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
170 mlt_playlist_append( playlist, producer );
171 producer = NULL;
172 if ( mlt_playlist_count( playlist ) > 0 )
173 {
174 mlt_playlist_clip_info info;
175 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
176 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
177 producer = info.cut;
178 properties = mlt_producer_properties( producer );
179 }
180 }
181 else if ( !strcmp( argv[ i ], "-split" ) )
182 {
183 int split = atoi( argv[ ++ i ] );
184 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
185 mlt_playlist_append( playlist, producer );
186 producer = NULL;
187 if ( mlt_playlist_count( playlist ) > 0 )
188 {
189 mlt_playlist_clip_info info;
190 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
191 split = split < 0 ? info.frame_out + split : split;
192 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
193 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
194 producer = info.cut;
195 properties = mlt_producer_properties( producer );
196 }
197 }
198 else if ( !strcmp( argv[ i ], "-swap" ) )
199 {
200 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
201 mlt_playlist_append( playlist, producer );
202 producer = NULL;
203 if ( mlt_playlist_count( playlist ) >= 2 )
204 {
205 mlt_playlist_clip_info info;
206 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
207 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
208 producer = info.cut;
209 properties = mlt_producer_properties( producer );
210 }
211 }
212 else if ( !strcmp( argv[ i ], "-join" ) )
213 {
214 int clips = atoi( argv[ ++ i ] );
215 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
216 mlt_playlist_append( playlist, producer );
217 producer = NULL;
218 if ( mlt_playlist_count( playlist ) > 0 )
219 {
220 mlt_playlist_clip_info info;
221 mlt_playlist_join( playlist, mlt_playlist_count( playlist ) - clips - 1, clips, 0 );
222 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
223 producer = info.cut;
224 properties = mlt_producer_properties( producer );
225 }
226 }
227 else if ( !strcmp( argv[ i ], "-remove" ) )
228 {
229 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
230 mlt_playlist_append( playlist, producer );
231 producer = NULL;
232 if ( mlt_playlist_count( playlist ) > 0 )
233 {
234 mlt_playlist_clip_info info;
235 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
236 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
237 producer = info.cut;
238 properties = mlt_producer_properties( producer );
239 }
240 }
241 else if ( !strcmp( argv[ i ], "-mix" ) )
242 {
243 int length = atoi( argv[ ++ i ] );
244 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
245 mlt_playlist_append( playlist, producer );
246 producer = NULL;
247 if ( mlt_playlist_count( playlist ) >= 2 )
248 {
249 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
250 {
251 mlt_playlist_clip_info info;
252 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
253 if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
254 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
255 mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
256 properties = NULL;
257 }
258 else
259 {
260 fprintf( stderr, "Mix failed?\n" );
261 }
262 }
263 else
264 {
265 fprintf( stderr, "Invalid position for a mix...\n" );
266 }
267 }
268 else if ( !strcmp( argv[ i ], "-mixer" ) )
269 {
270 if ( mix != NULL )
271 {
272 char *id = strdup( argv[ ++ i ] );
273 char *arg = strchr( id, ':' );
274 mlt_field field = mlt_tractor_field( mix );
275 mlt_transition transition = NULL;
276 if ( arg != NULL )
277 *arg ++ = '\0';
278 transition = mlt_factory_transition( id, arg );
279 if ( transition != NULL )
280 {
281 properties = mlt_transition_properties( transition );
282 mlt_properties_inherit( properties, group );
283 mlt_field_plant_transition( field, transition, 0, 1 );
284 mlt_properties_set_position( properties, "in", 0 );
285 mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
286 mlt_transition_close( transition );
287 }
288 free( id );
289 }
290 else
291 {
292 fprintf( stderr, "Invalid mixer...\n" );
293 }
294 }
295 else if ( !strcmp( argv[ i ], "-filter" ) )
296 {
297 mlt_filter filter = create_filter( field, argv[ ++ i ], track );
298 if ( filter != NULL )
299 {
300 properties = mlt_filter_properties( filter );
301 mlt_properties_inherit( properties, group );
302 }
303 }
304 else if ( !strcmp( argv[ i ], "-transition" ) )
305 {
306 mlt_transition transition = create_transition( field, argv[ ++ i ], track - 1 );
307 if ( transition != NULL )
308 {
309 properties = mlt_transition_properties( transition );
310 mlt_properties_inherit( properties, group );
311 }
312 }
313 else if ( !strcmp( argv[ i ], "-blank" ) )
314 {
315 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
316 mlt_playlist_append( playlist, producer );
317 producer = NULL;
318 mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
319 }
320 else if ( !strcmp( argv[ i ], "-track" ) ||
321 !strcmp( argv[ i ], "-hide-track" ) ||
322 !strcmp( argv[ i ], "-hide-video" ) ||
323 !strcmp( argv[ i ], "-hide-audio" ) )
324 {
325 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
326 mlt_playlist_append( playlist, producer );
327 producer = NULL;
328 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
329 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
330 playlist = mlt_playlist_init( );
331 if ( playlist != NULL )
332 {
333 properties = mlt_playlist_properties( playlist );
334 if ( !strcmp( argv[ i ], "-hide-track" ) )
335 mlt_properties_set_int( properties, "hide", 3 );
336 else if ( !strcmp( argv[ i ], "-hide-video" ) )
337 mlt_properties_set_int( properties, "hide", 1 );
338 else if ( !strcmp( argv[ i ], "-hide-audio" ) )
339 mlt_properties_set_int( properties, "hide", 2 );
340 }
341 }
342 else if ( strchr( argv[ i ], '=' ) )
343 {
344 mlt_properties_parse( properties, argv[ i ] );
345 }
346 else if ( argv[ i ][ 0 ] != '-' )
347 {
348 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
349 mlt_playlist_append( playlist, producer );
350 if ( title == NULL )
351 title = argv[ i ];
352 producer = create_producer( field, argv[ i ] );
353 if ( producer != NULL )
354 {
355 properties = mlt_producer_properties( producer );
356 mlt_properties_inherit( properties, group );
357 }
358 }
359 else
360 {
361 if ( !strcmp( argv[ i ], "-serialise" ) )
362 i += 2;
363 else if ( !strcmp( argv[ i ], "-consumer" ) )
364 i += 2;
365
366 while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
367 i ++;
368
369 i --;
370 }
371 }
372
373 // Connect last producer to playlist
374 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
375 mlt_playlist_append( playlist, producer );
376
377 // Track the last playlist too
378 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
379
380 // We must have a playlist to connect
381 if ( mlt_playlist_count( playlist ) > 0 )
382 mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
383
384 mlt_producer prod = mlt_tractor_producer( tractor );
385 mlt_properties props = mlt_tractor_properties( tractor );
386 mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
387 mlt_properties_set_position( props, "length", mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) + 1 );
388 mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) );
389 mlt_properties_set_double( props, "fps", mlt_producer_get_fps( mlt_multitrack_producer( multitrack ) ) );
390 if ( title != NULL )
391 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );
392
393 return prod;
394 }