some bugfixes, westley property handling reorg, make rescale respect the aspect ratio...
[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 mlt_properties producer_map;
38 mlt_properties destructors;
39 char *property;
40 mlt_properties producer_properties;
41 };
42 typedef struct deserialise_context_s *deserialise_context;
43
44
45 /** Push a service.
46 */
47
48 static int context_push_service( deserialise_context this, mlt_service that )
49 {
50 int ret = this->stack_service_size >= STACK_SIZE;
51 if ( ret == 0 )
52 this->stack_service[ this->stack_service_size ++ ] = that;
53 return ret;
54 }
55
56 /** Pop a service.
57 */
58
59 static mlt_service context_pop_service( deserialise_context this )
60 {
61 mlt_service result = NULL;
62 if ( this->stack_service_size > 0 )
63 result = this->stack_service[ -- this->stack_service_size ];
64 return result;
65 }
66
67 // Set the destructor on a new service
68 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
69 {
70 int registered = mlt_properties_get_int( properties, "registered" );
71 char *key = mlt_properties_get( properties, "registered" );
72 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
73 mlt_properties_set_int( properties, "registered", ++ registered );
74 }
75
76 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
77 {
78 mlt_service service = mlt_tractor_service( mlt_tractor_init() );
79 mlt_properties properties = mlt_service_properties( service );
80
81 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
82
83 mlt_properties_set_position( properties, "length", 0 );
84
85 for ( ; atts != NULL && *atts != NULL; atts += 2 )
86 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
87
88 if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
89 {
90 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
91 mlt_properties_set_position( properties, "length", length );
92 }
93
94 context_push_service( context, service );
95 }
96
97 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
98 {
99 mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
100 mlt_properties properties = mlt_service_properties( service );
101
102 track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
103
104 mlt_properties_set_position( properties, "length", 0 );
105
106 for ( ; atts != NULL && *atts != NULL; atts += 2 )
107 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
108
109 context_push_service( context, service );
110 }
111
112 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
113 {
114 mlt_service service = mlt_playlist_service( mlt_playlist_init() );
115 mlt_properties properties = mlt_service_properties( service );
116
117 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
118
119 mlt_properties_set_position( properties, "length", 0 );
120
121 for ( ; atts != NULL && *atts != NULL; atts += 2 )
122 {
123 mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
124 if ( strcmp( atts[ 0 ], "out" ) == 0 )
125 mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
126 }
127
128 if ( mlt_properties_get( properties, "id" ) != NULL )
129 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
130
131 context_push_service( context, service );
132 }
133
134 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
135 {
136 mlt_properties properties = context->producer_properties = mlt_properties_new();
137
138 for ( ; atts != NULL && *atts != NULL; atts += 2 )
139 {
140 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
141 }
142 }
143
144 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
145 {
146 // Get the playlist from the stack
147 mlt_service service = context_pop_service( context );
148 mlt_position length = 0;
149
150 // Look for the length attribute
151 for ( ; atts != NULL && *atts != NULL; atts += 2 )
152 {
153 if ( strcmp( atts[0], "length" ) == 0 )
154 {
155 length = atoll( atts[1] );
156 break;
157 }
158 }
159
160 // Append a blank to the playlist
161 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
162
163 // Push the playlist back onto the stack
164 context_push_service( context, service );
165 }
166
167 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
168 {
169 // Look for the producer attribute
170 for ( ; atts != NULL && *atts != NULL; atts += 2 )
171 {
172 if ( strcmp( atts[0], "producer" ) == 0 )
173 {
174 if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) != NULL )
175 // Push the referenced producer onto the stack
176 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
177 break;
178 }
179 }
180 }
181
182 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
183 {
184 mlt_properties properties = context->producer_properties = mlt_properties_new();
185
186 // Set the properties
187 for ( ; atts != NULL && *atts != NULL; atts += 2 )
188 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
189 }
190
191 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
192 {
193 mlt_properties properties = context->producer_properties = mlt_properties_new();
194
195 // Set the properties
196 for ( ; atts != NULL && *atts != NULL; atts += 2 )
197 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
198 }
199
200 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
201 {
202 mlt_properties properties = context->producer_properties;
203 char *value = NULL;
204
205 if ( properties == NULL )
206 return;
207
208 // Set the properties
209 for ( ; atts != NULL && *atts != NULL; atts += 2 )
210 {
211 if ( strcmp( atts[ 0 ], "name" ) == 0 )
212 {
213 context->property = strdup( atts[ 1 ] );
214 }
215 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
216 {
217 value = (char*) atts[ 1 ];
218 }
219 }
220
221 if ( context->property != NULL && value != NULL )
222 mlt_properties_set( properties, context->property, value );
223 }
224
225 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
226 {
227 // Get the producer (multitrack) from the stack
228 mlt_service producer = context_pop_service( context );
229
230 // Get the tractor from the stack
231 mlt_service service = context_pop_service( context );
232
233 // Connect the tractor to the producer
234 mlt_tractor_connect( MLT_TRACTOR( service ), producer );
235 mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
236 MLT_MULTITRACK( producer ), 0, NULL, NULL );
237
238 // Push the tractor back onto the stack
239 context_push_service( context, service );
240
241 // Push the producer back onto the stack
242 context_push_service( context, producer );
243 }
244
245 static void on_end_playlist( deserialise_context context, const xmlChar *name )
246 {
247 // Get the playlist from the stack
248 mlt_service service = context_pop_service( context );
249 mlt_properties properties = mlt_service_properties( service );
250
251 mlt_position in = mlt_properties_get_position( properties, "in" );
252 mlt_position out;
253
254 if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
255 out = mlt_properties_get_position( properties, "_westley.out" );
256 else
257 out = mlt_properties_get_position( properties, "length" ) - 1;
258
259 if ( mlt_properties_get_position( properties, "length" ) < out )
260 mlt_properties_set_position( properties, "length", out + 1 );
261
262 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
263
264 // Push the playlist back onto the stack
265 context_push_service( context, service );
266 }
267
268 static void on_end_track( deserialise_context context, const xmlChar *name )
269 {
270 // Get the producer from the stack
271 mlt_service producer = context_pop_service( context );
272
273 // Get the multitrack from the stack
274 mlt_service service = context_pop_service( context );
275
276 // Set the track on the multitrack
277 mlt_multitrack_connect( MLT_MULTITRACK( service ),
278 MLT_PRODUCER( producer ),
279 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
280
281 // Push the multitrack back onto the stack
282 context_push_service( context, service );
283 }
284
285 static void on_end_entry( deserialise_context context, const xmlChar *name )
286 {
287 // Get the producer from the stack
288 mlt_service producer = context_pop_service( context );
289
290 // Get the playlist from the stack
291 mlt_service service = context_pop_service( context );
292
293 // Append the producer to the playlist
294 mlt_playlist_append_io( MLT_PLAYLIST( service ),
295 MLT_PRODUCER( producer ),
296 mlt_properties_get_position( mlt_service_properties( producer ), "in" ),
297 mlt_properties_get_position( mlt_service_properties( producer ), "out" ) );
298
299 // Push the playlist back onto the stack
300 context_push_service( context, service );
301 }
302
303 static void on_end_tractor( deserialise_context context, const xmlChar *name )
304 {
305 // Get and discard the last producer
306 mlt_producer multitrack = MLT_PRODUCER( context_pop_service( context ) );
307
308 // Get the tractor
309 mlt_service tractor = context_pop_service( context );
310 multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
311
312 // Inherit the producer's properties
313 mlt_properties properties = mlt_producer_properties( multitrack );
314 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
315 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
316 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
317
318 // Push the playlist back onto the stack
319 context_push_service( context, tractor );
320 }
321
322 static void on_end_property( deserialise_context context, const xmlChar *name )
323 {
324 // Close this property handling
325 free( context->property );
326 context->property = NULL;
327 }
328
329 static void on_end_producer( deserialise_context context, const xmlChar *name )
330 {
331 mlt_properties properties = context->producer_properties;
332 mlt_service service = NULL;
333
334 if ( properties == NULL )
335 return;
336
337 // Instatiate the producer
338 service = MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties, "resource" ) ) );
339
340 if ( service == NULL && mlt_properties_get( properties, "mlt_service" ) != NULL )
341 {
342 service = MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties, "mlt_service" ),
343 mlt_properties_get( properties, "resource" ) ) );
344 }
345
346 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
347
348 // Add the producer to the producer map
349 if ( mlt_properties_get( properties, "id" ) != NULL )
350 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
351
352 mlt_properties_inherit( mlt_service_properties( service ), properties );
353 mlt_properties_close( properties );
354 context->producer_properties = NULL;
355 properties = mlt_service_properties( service );
356
357 // Set in and out
358 mlt_producer_set_in_and_out( MLT_PRODUCER( service ),
359 mlt_properties_get_position( properties, "in" ),
360 mlt_properties_get_position( properties, "out" ) );
361
362 // Push the new producer onto the stack
363 context_push_service( context, service );
364 }
365
366 static void on_end_filter( deserialise_context context, const xmlChar *name )
367 {
368 mlt_properties properties = context->producer_properties;
369 if ( properties == NULL )
370 return;
371
372 char *id;
373 char key[11];
374 key[ 10 ] = '\0';
375
376 // Get the producer from the stack
377 mlt_service producer = context_pop_service( context );
378 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
379
380 // Create the filter
381 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
382
383 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
384
385 // Connect the filter to the producer
386 mlt_filter_connect( MLT_FILTER( service ), producer,
387 mlt_properties_get_int( properties, "track" ) );
388
389 // Set in and out from producer if non existant
390 if ( mlt_properties_get( properties, "in" ) == NULL )
391 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
392 if ( mlt_properties_get( properties, "out" ) == NULL )
393 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
394
395 // Propogate the properties
396 mlt_properties_inherit( mlt_service_properties( service ), properties );
397 mlt_properties_close( properties );
398 context->producer_properties = NULL;
399 properties = mlt_service_properties( service );
400
401 // Set in and out
402 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
403 mlt_filter_set_in_and_out( MLT_FILTER( service ),
404 mlt_properties_get_position( properties, "in" ),
405 mlt_properties_get_position( properties, "out" ) );
406
407 // Get the parent producer from the stack
408 mlt_service tractor = context_pop_service( context );
409
410 if ( tractor != NULL )
411 {
412 //fprintf( stderr, "connecting tractor %s to filter\n", mlt_properties_get( mlt_service_properties( tractor ), "resource" ) );
413 // Connect the tractor to the filter
414 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor ), "resource" ), "<tractor>" ) == 0 )
415 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
416
417 // Push the parent producer back onto the stack
418 context_push_service( context, tractor );
419 }
420
421 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
422 // If a producer alias is in the producer_map, get it
423 snprintf( key, 10, "%p", producer );
424 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
425 producer = mlt_properties_get_data( context->producer_map, key, NULL );
426
427 // Put the producer in the producer map
428 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
429 if ( id != NULL )
430 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
431
432 // For filter chain support, add an alias to the producer map
433 snprintf( key, 10, "%p", service );
434 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
435
436 // Push the filter onto the stack
437 context_push_service( context, service );
438
439 }
440
441 static void on_end_transition( deserialise_context context, const xmlChar *name )
442 {
443 mlt_properties properties = context->producer_properties;
444 if ( properties == NULL )
445 return;
446
447 // Get the producer from the stack
448 mlt_service producer = context_pop_service( context );
449
450 // Create the transition
451 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
452
453 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
454
455 // Propogate the properties
456 mlt_properties_inherit( mlt_service_properties( service ), properties );
457 mlt_properties_close( properties );
458 context->producer_properties = NULL;
459 properties = mlt_service_properties( service );
460
461 // Set in and out
462 mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
463 mlt_properties_get_position( properties, "in" ),
464 mlt_properties_get_position( properties, "out" ) );
465
466 // Connect the filter to the producer
467 mlt_transition_connect( MLT_TRANSITION( service ), producer,
468 mlt_properties_get_int( properties, "a_track" ),
469 mlt_properties_get_int( properties, "b_track" ) );
470
471 // Get the tractor from the stack
472 mlt_service tractor = context_pop_service( context );
473
474 // Connect the tractor to the transition
475 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
476
477 // Push the tractor back onto the stack
478 context_push_service( context, tractor );
479
480 // Push the transition onto the stack
481 context_push_service( context, service );
482 }
483
484 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
485 {
486 deserialise_context context = ( deserialise_context ) ctx;
487
488 if ( strcmp( name, "tractor" ) == 0 )
489 on_start_tractor( context, name, atts );
490 else if ( strcmp( name, "multitrack" ) == 0 )
491 on_start_multitrack( context, name, atts );
492 else if ( strcmp( name, "playlist" ) == 0 )
493 on_start_playlist( context, name, atts );
494 else if ( strcmp( name, "producer" ) == 0 )
495 on_start_producer( context, name, atts );
496 else if ( strcmp( name, "blank" ) == 0 )
497 on_start_blank( context, name, atts );
498 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
499 on_start_entry_track( context, name, atts );
500 else if ( strcmp( name, "filter" ) == 0 )
501 on_start_filter( context, name, atts );
502 else if ( strcmp( name, "transition" ) == 0 )
503 on_start_transition( context, name, atts );
504 else if ( strcmp( name, "property" ) == 0 )
505 on_start_property( context, name, atts );
506 }
507
508 static void on_end_element( void *ctx, const xmlChar *name )
509 {
510 deserialise_context context = ( deserialise_context ) ctx;
511
512 if ( strcmp( name, "multitrack" ) == 0 )
513 on_end_multitrack( context, name );
514 else if ( strcmp( name, "playlist" ) == 0 )
515 on_end_playlist( context, name );
516 else if ( strcmp( name, "track" ) == 0 )
517 on_end_track( context, name );
518 else if ( strcmp( name, "entry" ) == 0 )
519 on_end_entry( context, name );
520 else if ( strcmp( name, "tractor" ) == 0 )
521 on_end_tractor( context, name );
522 else if ( strcmp( name, "property" ) == 0 )
523 on_end_property( context, name );
524 else if ( strcmp( name, "producer" ) == 0 )
525 on_end_producer( context, name );
526 else if ( strcmp( name, "filter" ) == 0 )
527 on_end_filter( context, name );
528 else if ( strcmp( name, "transition" ) == 0 )
529 on_end_transition( context, name );
530 }
531
532 static void on_characters( void *ctx, const xmlChar *ch, int len )
533 {
534 deserialise_context context = ( deserialise_context ) ctx;
535 char *value = calloc( len + 1, 1 );
536
537 value[ len ] = 0;
538 strncpy( value, (const char*) ch, len );
539
540 if ( context->property != NULL && context->producer_properties != NULL )
541 mlt_properties_set( context->producer_properties, context->property, value );
542
543 free( value);
544 }
545
546 mlt_producer producer_westley_init( char *filename )
547 {
548 static int init = 0;
549 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
550 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
551 mlt_properties properties = NULL;
552 int i = 0;
553
554 context->producer_map = mlt_properties_new();
555 context->destructors = mlt_properties_new();
556 // We need to track the number of registered filters
557 mlt_properties_set_int( context->destructors, "registered", 0 );
558 sax->startElement = on_start_element;
559 sax->endElement = on_end_element;
560 sax->characters = on_characters;
561
562 if ( !init )
563 {
564 xmlInitParser();
565 //init = 1;
566 }
567
568 xmlSAXUserParseFile( sax, context, filename );
569
570 // Need the complete producer list for various reasons
571 properties = context->destructors;
572
573 // Get the last producer on the stack
574 mlt_service service = context_pop_service( context );
575
576 // Do we actually have a producer here?
577 if ( service != NULL )
578 {
579 // Now make sure we don't have a reference to the service in the properties
580 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
581 {
582 char *name = mlt_properties_get_name( properties, i );
583 if ( mlt_properties_get_data( properties, name, NULL ) == service )
584 {
585 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
586 break;
587 }
588 }
589
590 // We are done referencing destructor property list
591 // Set this var to service properties for convenience
592 properties = mlt_service_properties( service );
593
594 // make the returned service destroy the connected services
595 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
596
597 // Now assign additional properties
598 mlt_properties_set( properties, "resource", filename );
599
600 // This tells consumer_westley not to deep copy
601 mlt_properties_set( properties, "westley", "was here" );
602 }
603 else
604 {
605 // Clean up
606 mlt_properties_close( properties );
607 }
608
609 free( context->stack_service );
610 mlt_properties_close( context->producer_map );
611 //free( context );
612 free( sax );
613 xmlCleanupParser();
614 xmlMemoryDump( );
615
616
617 return MLT_PRODUCER( service );
618 }
619