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