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