265475a064f32b4d8ef671263327f3e8a9d3c23c
[melted] / src / modules / westley / producer_westley.c
1 /*
2 * producer_westley.c -- a libxml2 parser of mlt service networks
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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 // TODO: destroy unreferenced producers
22
23 #include "producer_westley.h"
24 #include <framework/mlt.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <libxml/parser.h>
30
31 #define STACK_SIZE 1000
32
33 struct deserialise_context_s
34 {
35 mlt_service stack_service[ STACK_SIZE ];
36 int stack_service_size;
37 int track_count;
38 mlt_properties producer_map;
39 mlt_properties destructors;
40 };
41 typedef struct deserialise_context_s *deserialise_context;
42
43
44 /** Push a service.
45 */
46
47 static int context_push_service( deserialise_context this, mlt_service that )
48 {
49 int ret = this->stack_service_size >= STACK_SIZE;
50 if ( ret == 0 )
51 this->stack_service[ this->stack_service_size ++ ] = that;
52 return ret;
53 }
54
55 /** Pop a service.
56 */
57
58 static mlt_service context_pop_service( deserialise_context this )
59 {
60 mlt_service result = NULL;
61 if ( this->stack_service_size > 0 )
62 result = this->stack_service[ -- this->stack_service_size ];
63 return result;
64 }
65
66 // Set the destructor on a new service
67 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
68 {
69 int registered = mlt_properties_get_int( properties, "registered" );
70 char *key = mlt_properties_get( properties, "registered" );
71 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
72 mlt_properties_set_int( properties, "registered", ++ registered );
73 }
74
75 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
76 {
77 mlt_service service = mlt_tractor_service( mlt_tractor_init() );
78 mlt_properties properties = mlt_service_properties( service );
79
80 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
81
82 mlt_properties_set_position( properties, "length", 0 );
83
84 for ( ; atts != NULL && *atts != NULL; atts += 2 )
85 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
86
87 if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
88 {
89 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
90 mlt_properties_set_position( properties, "length", length );
91 }
92
93 context_push_service( context, service );
94 }
95
96 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
97 {
98 mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
99
100 track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
101
102 for ( ; atts != NULL && *atts != NULL; atts += 2 )
103 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
104
105 context_push_service( context, service );
106 }
107
108 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
109 {
110 mlt_service service = mlt_playlist_service( mlt_playlist_init() );
111 mlt_properties properties = mlt_service_properties( service );
112
113 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
114
115 mlt_properties_set_position( properties, "length", 0 );
116
117 for ( ; atts != NULL && *atts != NULL; atts += 2 )
118 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
119
120 if ( mlt_properties_get( properties, "id" ) != NULL )
121 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
122
123 if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
124 {
125 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
126 mlt_properties_set_position( properties, "length", length );
127 }
128
129 context_push_service( context, service );
130 }
131
132 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
133 {
134 mlt_properties properties = mlt_properties_new();
135 mlt_service service = NULL;
136
137 for ( ; atts != NULL && *atts != NULL; atts += 2 )
138 {
139 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
140 }
141
142 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
143 {
144 service = MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties, "mlt_service" ),
145 mlt_properties_get( properties, "resource" ) ) );
146 }
147 else
148 {
149 // Unspecified producer, use inigo
150 char *args[2] = { mlt_properties_get( properties, "resource" ), 0 };
151 service = MLT_SERVICE( mlt_factory_producer( "inigo", args ) );
152 }
153
154 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
155
156 // Add the producer to the producer map
157 if ( mlt_properties_get( properties, "id" ) != NULL )
158 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
159
160 mlt_properties_inherit( mlt_service_properties( service ), properties );
161 mlt_properties_close( properties );
162
163 context_push_service( context, service );
164 }
165
166 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
167 {
168 // Get the playlist from the stack
169 mlt_service service = context_pop_service( context );
170 mlt_position length = 0;
171
172 // Look for the length attribute
173 for ( ; atts != NULL && *atts != NULL; atts += 2 )
174 {
175 if ( strcmp( atts[0], "length" ) == 0 )
176 {
177 length = atoll( atts[1] );
178 break;
179 }
180 }
181
182 // Append a blank to the playlist
183 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
184
185 // Push the playlist back onto the stack
186 context_push_service( context, service );
187 }
188
189 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
190 {
191 // Look for the producer attribute
192 for ( ; atts != NULL && *atts != NULL; atts += 2 )
193 {
194 if ( strcmp( atts[0], "producer" ) == 0 )
195 {
196 if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) != NULL )
197 // Push the referenced producer onto the stack
198 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
199 break;
200 }
201 }
202 }
203
204 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
205 {
206 char *id;
207 mlt_properties properties = mlt_properties_new();
208 char key[11];
209 key[ 10 ] = '\0';
210
211 // Get the producer from the stack
212 mlt_service producer = context_pop_service( context );
213
214 // Set the properties
215 for ( ; atts != NULL && *atts != NULL; atts += 2 )
216 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
217
218 // Create the filter
219 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
220
221 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
222
223 // Connect the filter to the producer
224 mlt_filter_connect( MLT_FILTER( service ), producer,
225 mlt_properties_get_int( properties, "track" ) );
226
227 // Set in and out from producer if non existant
228 if ( mlt_properties_get( properties, "in" ) == NULL )
229 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
230 if ( mlt_properties_get( properties, "out" ) == NULL )
231 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
232
233 // Propogate the properties
234 mlt_properties_inherit( mlt_service_properties( service ), properties );
235 mlt_properties_close( properties );
236
237 // Get the parent producer from the stack
238 mlt_service tractor = context_pop_service( context );
239
240 if ( tractor != NULL )
241 {
242 // Connect the filter to the tractor
243 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor ), "resource" ), "<tractor>" ) == 0 )
244 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
245
246 // Push the parent producer back onto the stack
247 context_push_service( context, tractor );
248 }
249
250 // If a producer alias is in the producer_map, get it
251 snprintf( key, 10, "%p", producer );
252 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
253 producer = mlt_properties_get_data( context->producer_map, key, NULL );
254
255 // Put the producer in the producer map
256 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
257 if ( id != NULL )
258 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
259
260 // For filter chain support, add an alias to the producer map
261 snprintf( key, 10, "%p", service );
262 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
263
264 // Push the filter onto the stack
265 context_push_service( context, service );
266 }
267
268 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
269 {
270 mlt_properties properties = mlt_properties_new();
271
272 // Get the producer from the stack
273 mlt_service producer = context_pop_service( context );
274
275 // Set the properties
276 for ( ; atts != NULL && *atts != NULL; atts += 2 )
277 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
278
279 // Create the transition
280 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
281
282 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
283
284 // Propogate the properties
285 mlt_properties_inherit( mlt_service_properties( service ), properties );
286 mlt_properties_close( properties );
287
288 // Connect the filter to the producer
289 mlt_transition_connect( MLT_TRANSITION( service ), producer,
290 mlt_properties_get_int( mlt_service_properties( service ), "a_track" ),
291 mlt_properties_get_int( mlt_service_properties( service ), "b_track" ) );
292
293 // Get the tractor from the stack
294 mlt_service tractor = context_pop_service( context );
295
296 // Connect the tractor to the transition
297 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
298
299 // Push the tractor back onto the stack
300 context_push_service( context, tractor );
301
302 // Push the transition onto the stack
303 context_push_service( context, service );
304 }
305
306 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
307 {
308 // Get the producer (multitrack) from the stack
309 mlt_service producer = context_pop_service( context );
310
311 // Get the tractor from the stack
312 mlt_service service = context_pop_service( context );
313
314 // Connect the tractor to the producer
315 mlt_tractor_connect( MLT_TRACTOR( service ), producer );
316 mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
317 MLT_MULTITRACK( producer ), 0, NULL, NULL );
318
319 // Push the tractor back onto the stack
320 context_push_service( context, service );
321
322 // Push the producer back onto the stack
323 context_push_service( context, producer );
324 }
325
326 static void on_end_playlist( deserialise_context context, const xmlChar *name )
327 {
328 // Get the producer (playlist) from the stack
329 mlt_service producer = context_pop_service( context );
330
331 // Push the producer back onto the stack
332 context_push_service( context, producer );
333 }
334
335 static void on_end_track( deserialise_context context, const xmlChar *name )
336 {
337 // Get the producer from the stack
338 mlt_service producer = context_pop_service( context );
339
340 // Get the multitrack from the stack
341 mlt_service service = context_pop_service( context );
342
343 // Set the track on the multitrack
344 mlt_multitrack_connect( MLT_MULTITRACK( service ),
345 MLT_PRODUCER( producer ),
346 context->track_count++ );
347
348 // Push the multitrack back onto the stack
349 context_push_service( context, service );
350 }
351
352 static void on_end_entry( deserialise_context context, const xmlChar *name )
353 {
354 // Get the producer from the stack
355 mlt_service producer = context_pop_service( context );
356
357 // Get the playlist from the stack
358 mlt_service service = context_pop_service( context );
359
360 // Append the producer to the playlist
361 mlt_playlist_append_io( MLT_PLAYLIST( service ),
362 MLT_PRODUCER( producer ),
363 mlt_properties_get_position( mlt_service_properties( producer ), "in" ),
364 mlt_properties_get_position( mlt_service_properties( producer ), "out" ) );
365
366 // Push the playlist back onto the stack
367 context_push_service( context, service );
368 }
369
370 static void on_end_tractor( deserialise_context context, const xmlChar *name )
371 {
372 // Get and discard the last producer
373 mlt_producer multitrack = MLT_PRODUCER( context_pop_service( context ) );
374
375 // Inherit the producer's properties
376 mlt_properties properties = mlt_producer_properties( multitrack );
377 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
378 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
379 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
380 }
381
382 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
383 {
384 deserialise_context context = ( deserialise_context ) ctx;
385
386 if ( strcmp( name, "tractor" ) == 0 )
387 on_start_tractor( context, name, atts );
388 else if ( strcmp( name, "multitrack" ) == 0 )
389 on_start_multitrack( context, name, atts );
390 else if ( strcmp( name, "playlist" ) == 0 )
391 on_start_playlist( context, name, atts );
392 else if ( strcmp( name, "producer" ) == 0 )
393 on_start_producer( context, name, atts );
394 else if ( strcmp( name, "blank" ) == 0 )
395 on_start_blank( context, name, atts );
396 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
397 on_start_entry_track( context, name, atts );
398 else if ( strcmp( name, "filter" ) == 0 )
399 on_start_filter( context, name, atts );
400 else if ( strcmp( name, "transition" ) == 0 )
401 on_start_transition( context, name, atts );
402 }
403
404 static void on_end_element( void *ctx, const xmlChar *name )
405 {
406 deserialise_context context = ( deserialise_context ) ctx;
407
408 if ( strcmp( name, "multitrack" ) == 0 )
409 on_end_multitrack( context, name );
410 else if ( strcmp( name, "playlist" ) == 0 )
411 on_end_playlist( context, name );
412 else if ( strcmp( name, "track" ) == 0 )
413 on_end_track( context, name );
414 else if ( strcmp( name, "entry" ) == 0 )
415 on_end_entry( context, name );
416 else if ( strcmp( name, "tractor" ) == 0 )
417 on_end_tractor( context, name );
418 }
419
420
421 mlt_producer producer_westley_init( char *filename )
422 {
423 static int init = 0;
424 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
425 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
426 mlt_properties properties = NULL;
427 int i = 0;
428
429 context->producer_map = mlt_properties_new();
430 context->destructors = mlt_properties_new();
431 // We need to track the number of registered filters
432 mlt_properties_set_int( context->destructors, "registered", 0 );
433 sax->startElement = on_start_element;
434 sax->endElement = on_end_element;
435
436 if ( !init )
437 {
438 xmlInitParser();
439 //init = 1;
440 }
441
442 xmlSAXUserParseFile( sax, context, filename );
443
444 // Need the complete producer list for various reasons
445 properties = context->destructors;
446
447 // Get the last producer on the stack
448 mlt_service service = context_pop_service( context );
449
450 // Do we actually have a producer here?
451 if ( service != NULL )
452 {
453 // Now make sure we don't have a reference to the service in the properties
454 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
455 {
456 char *name = mlt_properties_get_name( properties, i );
457 if ( mlt_properties_get_data( properties, name, NULL ) == service )
458 {
459 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
460 break;
461 }
462 }
463
464 // We are done referencing destructor property list
465 // Set this var to service properties for convenience
466 properties = mlt_service_properties( service );
467
468 // make the returned service destroy the connected services
469 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
470
471 // Now assign additional properties
472 mlt_properties_set( properties, "resource", filename );
473
474 // This tells consumer_westley not to deep copy
475 mlt_properties_set( properties, "westley", "was here" );
476 }
477 else
478 {
479 // Clean up
480 mlt_properties_close( properties );
481 }
482
483 free( context->stack_service );
484 mlt_properties_close( context->producer_map );
485 //free( context );
486 free( sax );
487 xmlCleanupParser();
488 xmlMemoryDump( );
489
490
491 return MLT_PRODUCER( service );
492 }
493