2 * producer_inigo.c -- simple inigo test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
25 #include <framework/mlt.h>
27 mlt_producer producer_inigo_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv );
29 mlt_producer producer_inigo_file_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
31 FILE *input = fopen( file, "r" );
32 char **args = calloc( sizeof( char * ), 1000 );
38 while( fgets( temp, 2048, input ) )
40 temp[ strlen( temp ) - 1 ] = '\0';
41 if ( strcmp( temp, "" ) )
42 args[ count ++ ] = strdup( temp );
46 mlt_producer result = producer_inigo_init( profile, type, id, args );
50 mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
51 mlt_properties_set( properties, "resource", file );
55 free( args[ count ] );
61 static void track_service( mlt_field field, void *service, mlt_destructor destructor )
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 );
70 static mlt_producer create_producer( mlt_profile profile, mlt_field field, char *file )
72 mlt_producer result = mlt_factory_producer( profile, "fezzik", file );
75 track_service( field, result, ( mlt_destructor )mlt_producer_close );
80 static mlt_filter create_attach( mlt_profile profile, mlt_field field, char *id, int track )
82 char *temp = strdup( id );
83 char *arg = strchr( temp, ':' );
86 mlt_filter filter = mlt_factory_filter( profile, temp, arg );
88 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
93 static mlt_filter create_filter( mlt_profile profile, mlt_field field, char *id, int track )
95 char *temp = strdup( id );
96 char *arg = strchr( temp, ':' );
99 mlt_filter filter = mlt_factory_filter( profile, temp, arg );
100 if ( filter != NULL )
102 mlt_field_plant_filter( field, filter, track );
103 track_service( field, filter, ( mlt_destructor )mlt_filter_close );
109 static mlt_transition create_transition( mlt_profile profile, mlt_field field, char *id, int track )
111 char *arg = strchr( id, ':' );
114 mlt_transition transition = mlt_factory_transition( profile, id, arg );
115 if ( transition != NULL )
117 mlt_field_plant_transition( field, transition, track, track + 1 );
118 track_service( field, transition, ( mlt_destructor )mlt_transition_close );
123 mlt_producer producer_inigo_init( mlt_profile profile, mlt_service_type type, const char *id, char **argv )
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 );
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 );
141 // We need to track the number of registered filters
142 mlt_properties_set_int( field_properties, "registered", 0 );
144 // Parse the arguments
146 for ( i = 0; argv[ i ] != NULL; i ++ )
148 if ( !strcmp( argv[ i ], "-group" ) )
150 if ( mlt_properties_count( group ) != 0 )
152 mlt_properties_close( group );
153 group = mlt_properties_new( );
158 else if ( !strcmp( argv[ i ], "-attach" ) ||
159 !strcmp( argv[ i ], "-attach-cut" ) ||
160 !strcmp( argv[ i ], "-attach-track" ) ||
161 !strcmp( argv[ i ], "-attach-clip" ) )
163 int type = !strcmp( argv[ i ], "-attach" ) ? 0 :
164 !strcmp( argv[ i ], "-attach-cut" ) ? 1 :
165 !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
166 mlt_filter filter = create_attach( profile, field, argv[ ++ i ], track );
167 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
169 mlt_playlist_clip_info info;
170 mlt_playlist_append( playlist, producer );
171 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
175 if ( type == 1 || type == 2 )
177 mlt_playlist_clip_info info;
178 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
182 if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
185 mlt_service_attach( ( mlt_service )properties, filter );
186 else if ( type == 1 )
187 mlt_service_attach( ( mlt_service )producer, filter );
188 else if ( type == 2 )
189 mlt_service_attach( ( mlt_service )playlist, filter );
190 else if ( type == 3 )
191 mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
193 properties = MLT_FILTER_PROPERTIES( filter );
194 mlt_properties_inherit( properties, group );
196 else if ( filter != NULL )
198 mlt_service_attach( ( mlt_service )playlist, filter );
199 properties = MLT_FILTER_PROPERTIES( filter );
200 mlt_properties_inherit( properties, group );
203 else if ( !strcmp( argv[ i ], "-repeat" ) )
205 int repeat = atoi( argv[ ++ i ] );
206 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
207 mlt_playlist_append( playlist, producer );
209 if ( mlt_playlist_count( playlist ) > 0 )
211 mlt_playlist_clip_info info;
212 mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
213 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
215 properties = MLT_PRODUCER_PROPERTIES( producer );
218 else if ( !strcmp( argv[ i ], "-split" ) )
220 int split = atoi( argv[ ++ i ] );
221 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
222 mlt_playlist_append( playlist, producer );
224 if ( mlt_playlist_count( playlist ) > 0 )
226 mlt_playlist_clip_info info;
227 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
228 split = split < 0 ? info.frame_out + split : split;
229 mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
230 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
232 properties = MLT_PRODUCER_PROPERTIES( producer );
235 else if ( !strcmp( argv[ i ], "-swap" ) )
237 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
238 mlt_playlist_append( playlist, producer );
240 if ( mlt_playlist_count( playlist ) >= 2 )
242 mlt_playlist_clip_info info;
243 mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
244 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
246 properties = MLT_PRODUCER_PROPERTIES( producer );
249 else if ( !strcmp( argv[ i ], "-join" ) )
251 int clips = atoi( argv[ ++ i ] );
252 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
253 mlt_playlist_append( playlist, producer );
255 if ( mlt_playlist_count( playlist ) > 0 )
257 mlt_playlist_clip_info info;
258 int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
259 if ( clip < 0 ) clip = 0;
260 if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
261 if ( clips < 0 ) clips = mlt_playlist_count( playlist ) - 1;
262 mlt_playlist_join( playlist, clip, clips, 0 );
263 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
265 properties = MLT_PRODUCER_PROPERTIES( producer );
268 else if ( !strcmp( argv[ i ], "-remove" ) )
270 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
271 mlt_playlist_append( playlist, producer );
273 if ( mlt_playlist_count( playlist ) > 0 )
275 mlt_playlist_clip_info info;
276 mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
277 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
279 properties = MLT_PRODUCER_PROPERTIES( producer );
282 else if ( !strcmp( argv[ i ], "-mix" ) )
284 int length = atoi( argv[ ++ i ] );
285 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
286 mlt_playlist_append( playlist, producer );
288 if ( mlt_playlist_count( playlist ) >= 2 )
290 if ( mlt_playlist_mix( playlist, mlt_playlist_count( playlist ) - 2, length, NULL ) == 0 )
292 mlt_playlist_clip_info info;
293 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
294 if ( mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL ) == NULL )
295 mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 2 );
296 mix = ( mlt_tractor )mlt_properties_get_data( ( mlt_properties )info.producer, "mlt_mix", NULL );
301 fprintf( stderr, "Mix failed?\n" );
306 fprintf( stderr, "Invalid position for a mix...\n" );
309 else if ( !strcmp( argv[ i ], "-mixer" ) )
313 char *id = strdup( argv[ ++ i ] );
314 char *arg = strchr( id, ':' );
315 mlt_field field = mlt_tractor_field( mix );
316 mlt_transition transition = NULL;
319 transition = mlt_factory_transition( profile, id, arg );
320 if ( transition != NULL )
322 properties = MLT_TRANSITION_PROPERTIES( transition );
323 mlt_properties_inherit( properties, group );
324 mlt_field_plant_transition( field, transition, 0, 1 );
325 mlt_properties_set_position( properties, "in", 0 );
326 mlt_properties_set_position( properties, "out", mlt_producer_get_out( ( mlt_producer )mix ) );
327 mlt_transition_close( transition );
333 fprintf( stderr, "Invalid mixer...\n" );
336 else if ( !strcmp( argv[ i ], "-filter" ) )
338 mlt_filter filter = create_filter( profile, field, argv[ ++ i ], track );
339 if ( filter != NULL )
341 properties = MLT_FILTER_PROPERTIES( filter );
342 mlt_properties_inherit( properties, group );
345 else if ( !strcmp( argv[ i ], "-transition" ) )
347 mlt_transition transition = create_transition( profile, field, argv[ ++ i ], track - 1 );
348 if ( transition != NULL )
350 properties = MLT_TRANSITION_PROPERTIES( transition );
351 mlt_properties_inherit( properties, group );
354 else if ( !strcmp( argv[ i ], "-blank" ) )
356 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
357 mlt_playlist_append( playlist, producer );
359 mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
361 else if ( !strcmp( argv[ i ], "-track" ) ||
362 !strcmp( argv[ i ], "-null-track" ) ||
363 !strcmp( argv[ i ], "-video-track" ) ||
364 !strcmp( argv[ i ], "-audio-track" ) ||
365 !strcmp( argv[ i ], "-hide-track" ) ||
366 !strcmp( argv[ i ], "-hide-video" ) ||
367 !strcmp( argv[ i ], "-hide-audio" ) )
369 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
370 mlt_playlist_append( playlist, producer );
372 if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first" ) ||
373 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
375 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
376 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
377 playlist = mlt_playlist_init( );
379 if ( playlist != NULL )
381 properties = MLT_PLAYLIST_PROPERTIES( playlist );
382 if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
383 mlt_properties_set_int( properties, "hide", 3 );
384 else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
385 mlt_properties_set_int( properties, "hide", 1 );
386 else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
387 mlt_properties_set_int( properties, "hide", 2 );
390 else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
392 mlt_properties_parse( properties, argv[ i ] );
394 else if ( argv[ i ][ 0 ] != '-' )
396 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
397 mlt_playlist_append( playlist, producer );
398 if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
400 producer = create_producer( profile, field, argv[ i ] );
401 if ( producer != NULL )
403 properties = MLT_PRODUCER_PROPERTIES( producer );
404 mlt_properties_inherit( properties, group );
408 fprintf( stderr, "Failed to load \"%s\"\n", argv[ i ] );
414 if ( !strcmp( argv[ i ], "-serialise" ) ||
415 !strcmp( argv[ i ], "-consumer" ) ||
416 !strcmp( argv[ i ], "-profile" ) )
422 while ( argv[ i ] != NULL && strchr( argv[ i ], '=' ) )
432 // Connect last producer to playlist
433 if ( producer != NULL && !mlt_producer_is_cut( producer ) )
434 mlt_playlist_append( playlist, producer );
436 // Track the last playlist too
437 track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
439 // We must have a playlist to connect
440 if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first" ) ||
441 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
442 mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
444 mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
445 mlt_producer_optimise( prod );
446 mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
447 mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
448 mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
449 mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
451 mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );