]> git.sesse.net Git - mlt/blob - src/modules/westley/producer_westley.c
avformat/factory.c, jackrack/jack_rack.c, jackrack/plugin_settings.c, vmfx/filter_chr...
[mlt] / 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 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 // TODO: destroy unreferenced producers (they are currently destroyed
22 //       when the returned producer is closed).
23
24 #include "producer_westley.h"
25 #include <framework/mlt.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <unistd.h>
31
32 #include <libxml/parser.h>
33 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
34 #include <libxml/tree.h>
35
36 #define STACK_SIZE 1000
37 #define BRANCH_SIG_LEN 4000
38
39 #define _x (xmlChar*)
40 #define _s (char*)
41
42 #undef DEBUG
43 #ifdef DEBUG
44 extern xmlDocPtr westley_make_doc( mlt_service service );
45 #endif
46
47 enum service_type
48 {
49         mlt_invalid_type,
50         mlt_unknown_type,
51         mlt_producer_type,
52         mlt_playlist_type,
53         mlt_entry_type,
54         mlt_tractor_type,
55         mlt_multitrack_type,
56         mlt_filter_type,
57         mlt_transition_type,
58         mlt_consumer_type,
59         mlt_field_type,
60         mlt_services_type,
61         mlt_dummy_filter_type,
62         mlt_dummy_transition_type,
63         mlt_dummy_producer_type,
64 };
65
66 struct deserialise_context_s
67 {
68         enum service_type stack_types[ STACK_SIZE ];
69         mlt_service stack_service[ STACK_SIZE ];
70         int stack_service_size;
71         mlt_properties producer_map;
72         mlt_properties destructors;
73         char *property;
74         int is_value;
75         xmlDocPtr value_doc;
76         xmlNodePtr stack_node[ STACK_SIZE ];
77         int stack_node_size;
78         xmlDocPtr entity_doc;
79         int entity_is_replace;
80         int depth;
81         int branch[ STACK_SIZE ];
82         const xmlChar *publicId;
83         const xmlChar *systemId;
84         mlt_properties params;
85 };
86 typedef struct deserialise_context_s *deserialise_context;
87
88 /** Convert the numerical current branch address to a dot-delimited string.
89 */
90 static char *serialise_branch( deserialise_context this, char *s )
91 {
92         int i;
93         
94         s[0] = 0;
95         for ( i = 0; i < this->depth; i++ )
96         {
97                 int len = strlen( s );
98                 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", this->branch[ i ] );
99         }
100         return s;
101 }
102
103 /** Push a service.
104 */
105
106 static int context_push_service( deserialise_context this, mlt_service that, enum service_type type )
107 {
108         int ret = this->stack_service_size >= STACK_SIZE - 1;
109         if ( ret == 0 )
110         {
111                 this->stack_service[ this->stack_service_size ] = that;
112                 this->stack_types[ this->stack_service_size++ ] = type;
113                 
114                 // Record the tree branch on which this service lives
115                 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_westley_branch" ) == NULL )
116                 {
117                         char s[ BRANCH_SIG_LEN ];
118                         mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_westley_branch", serialise_branch( this, s ) );
119                 }
120         }
121         return ret;
122 }
123
124 /** Pop a service.
125 */
126
127 static mlt_service context_pop_service( deserialise_context this, enum service_type *type )
128 {
129         mlt_service result = NULL;
130         if ( this->stack_service_size > 0 )
131         {
132                 result = this->stack_service[ -- this->stack_service_size ];
133                 if ( type != NULL )
134                         *type = this->stack_types[ this->stack_service_size ];
135         }
136         return result;
137 }
138
139 /** Push a node.
140 */
141
142 static int context_push_node( deserialise_context this, xmlNodePtr node )
143 {
144         int ret = this->stack_node_size >= STACK_SIZE - 1;
145         if ( ret == 0 )
146                 this->stack_node[ this->stack_node_size ++ ] = node;
147         return ret;
148 }
149
150 /** Pop a node.
151 */
152
153 static xmlNodePtr context_pop_node( deserialise_context this )
154 {
155         xmlNodePtr result = NULL;
156         if ( this->stack_node_size > 0 )
157                 result = this->stack_node[ -- this->stack_node_size ];
158         return result;
159 }
160
161
162 // Set the destructor on a new service
163 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
164 {
165         int registered = mlt_properties_get_int( properties, "registered" );
166         char *key = mlt_properties_get( properties, "registered" );
167         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
168         mlt_properties_set_int( properties, "registered", ++ registered );
169 }
170
171
172 // Prepend the property value with the document root
173 static inline void qualify_property( deserialise_context context, mlt_properties properties, char *name )
174 {
175         char *resource = mlt_properties_get( properties, name );
176         if ( resource != NULL )
177         {
178                 // Qualify file name properties 
179                 char *root = mlt_properties_get( context->producer_map, "root" );
180                 if ( root != NULL && strcmp( root, "" ) )
181                 {
182                         char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
183                         if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
184                         {
185                                 strcpy( full_resource, root );
186                                 strcat( full_resource, "/" );
187                                 strcat( full_resource, resource );
188                         }
189                         else
190                         {
191                                 strcpy( full_resource, resource );
192                         }
193                         mlt_properties_set( properties, name, full_resource );
194                         free( full_resource );
195                 }
196         }
197 }
198
199
200 /** This function adds a producer to a playlist or multitrack when
201     there is no entry or track element.
202 */
203
204 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
205 {
206         // Return value (0 = service remains top of stack, 1 means it can be removed)
207         int result = 0;
208
209         // Get the parent producer
210         enum service_type type;
211         mlt_service container = context_pop_service( context, &type );
212         int contained = 0;
213
214         if ( service != NULL && container != NULL )
215         {
216                 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_westley_branch" );
217                 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_westley_branch" );
218                 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
219         }
220
221         if ( contained )
222         {
223                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
224                 char *hide_s = mlt_properties_get( properties, "hide" );
225
226                 // Indicate that this service is no longer top of stack
227                 result = 1;
228
229                 switch( type )
230                 {
231                         case mlt_tractor_type: 
232                                 {
233                                         mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
234                                         mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
235                                 }
236                                 break;
237                         case mlt_multitrack_type:
238                                 {
239                                         mlt_multitrack_connect( MLT_MULTITRACK( container ),
240                                                 MLT_PRODUCER( service ),
241                                                 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
242                                 }
243                                 break;
244                         case mlt_playlist_type:
245                                 {
246                                         mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
247                                 }
248                                 break;
249                         default:
250                                 result = 0;
251                                 fprintf( stderr, "Producer defined inside something that isn't a container\n" );
252                                 break;
253                 };
254
255                 // Set the hide state of the track producer
256                 if ( hide_s != NULL )
257                 {
258                         if ( strcmp( hide_s, "video" ) == 0 )
259                                 mlt_properties_set_int( properties, "hide", 1 );
260                         else if ( strcmp( hide_s, "audio" ) == 0 )
261                                 mlt_properties_set_int( properties, "hide", 2 );
262                         else if ( strcmp( hide_s, "both" ) == 0 )
263                                 mlt_properties_set_int( properties, "hide", 3 );
264                 }
265         }
266
267         // Put the parent producer back
268         if ( container != NULL )
269                 context_push_service( context, container, type );
270
271         return result;
272 }
273
274 /** Attach filters defined on that to this.
275 */
276
277 static void attach_filters( mlt_service this, mlt_service that )
278 {
279         if ( that != NULL )
280         {
281                 int i = 0;
282                 mlt_filter filter = NULL;
283                 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
284                 {
285                         mlt_service_attach( this, filter );
286                         attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
287                 }
288         }
289 }
290
291 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
292 {
293         mlt_tractor tractor = mlt_tractor_new( );
294         mlt_service service = MLT_TRACTOR_SERVICE( tractor );
295         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
296
297         track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
298
299         for ( ; atts != NULL && *atts != NULL; atts += 2 )
300                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (char*) atts[0], atts[1] == NULL ? "" : (char*) atts[1] );
301
302         mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
303
304         if ( mlt_properties_get( properties, "id" ) != NULL )
305                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
306         
307         context_push_service( context, service, mlt_tractor_type );
308 }
309
310 static void on_end_tractor( deserialise_context context, const xmlChar *name )
311 {
312         // Get the tractor
313         enum service_type type;
314         mlt_service tractor = context_pop_service( context, &type );
315
316         if ( tractor != NULL && type == mlt_tractor_type )
317         {
318                 // See if the tractor should be added to a playlist or multitrack
319                 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
320                         context_push_service( context, tractor, type );
321         }
322         else
323         {
324                 fprintf( stderr, "Invalid state for tractor\n" );
325         }
326 }
327
328 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
329 {
330         enum service_type type;
331         mlt_service parent = context_pop_service( context, &type );
332
333         // If we don't have a parent, then create one now, providing we're in a state where we can
334         if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
335         {
336                 mlt_tractor tractor = NULL;
337                 // Push the parent back
338                 if ( parent != NULL )
339                         context_push_service( context, parent, type );
340
341                 // Create a tractor to contain the multitrack
342                 tractor = mlt_tractor_new( );
343                 parent = MLT_TRACTOR_SERVICE( tractor );
344                 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
345                 type = mlt_tractor_type;
346
347                 // Flag it as a synthesised tractor for clean up later
348                 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "fezzik_synth", 1 );
349         }
350
351         if ( type == mlt_tractor_type )
352         {
353                 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
354                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
355                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
356                         mlt_properties_set( properties, (char*) atts[0], atts[1] == NULL ? "" : (char*) atts[1] );
357
358                 if ( mlt_properties_get( properties, "id" ) != NULL )
359                         mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
360
361                 context_push_service( context, parent, type );
362                 context_push_service( context, service, mlt_multitrack_type );
363         }
364         else
365         {
366                 fprintf( stderr, "Invalid multitrack position\n" );
367         }
368 }
369
370 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
371 {
372         // Get the multitrack from the stack
373         enum service_type type;
374         mlt_service service = context_pop_service( context, &type );
375
376         if ( service == NULL || type != mlt_multitrack_type )
377                 fprintf( stderr, "End multitrack in the wrong state...\n" );
378 }
379
380 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
381 {
382         mlt_playlist playlist = mlt_playlist_init( );
383         mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
384         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
385
386         track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
387
388         for ( ; atts != NULL && *atts != NULL; atts += 2 )
389         {
390                 mlt_properties_set( properties, (char*) atts[0], atts[1] == NULL ? "" : (char*) atts[1] );
391
392                 // Out will be overwritten later as we append, so we need to save it
393                 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
394                         mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
395         }
396
397         if ( mlt_properties_get( properties, "id" ) != NULL )
398                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
399
400         context_push_service( context, service, mlt_playlist_type );
401 }
402
403 static void on_end_playlist( deserialise_context context, const xmlChar *name )
404 {
405         // Get the playlist from the stack
406         enum service_type type;
407         mlt_service service = context_pop_service( context, &type );
408
409         if ( service != NULL && type == mlt_playlist_type )
410         {
411                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
412                 mlt_position in = mlt_properties_get_position( properties, "in" );
413                 mlt_position out = mlt_properties_get_position( properties, "out" );
414
415                 // See if the playlist should be added to a playlist or multitrack
416                 if ( add_producer( context, service, in, out ) == 0 )
417                         context_push_service( context, service, type );
418         }
419         else
420         {
421                 fprintf( stderr, "Invalid state of playlist end\n" );
422         }
423 }
424
425 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
426 {
427         // use a dummy service to hold properties to allow arbitrary nesting
428         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
429         mlt_service_init( service, NULL );
430
431         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
432
433         context_push_service( context, service, mlt_dummy_producer_type );
434
435         for ( ; atts != NULL && *atts != NULL; atts += 2 )
436                 mlt_properties_set( properties, (char*) atts[0], atts[1] == NULL ? "" : (char*) atts[1] );
437 }
438
439 // Parse a SMIL clock value (as produced by Kino 0.9.1) and return position in frames
440 static mlt_position parse_clock_value( char *value, double fps )
441 {
442         // This implementation expects a fully specified clock value - no optional
443         // parts (e.g. 1:05)
444         char *pos, *copy = strdup( value );
445         int hh, mm, ss, ms;
446         mlt_position result = -1;
447
448         value = copy;
449         pos = strchr( value, ':' );
450         if ( !pos )
451                 return result;
452         *pos = '\0';
453         hh = atoi( value );
454         value = pos + 1;
455
456         pos = strchr( value, ':' );
457         if ( !pos )
458                 return result;
459         *pos = '\0';
460         mm = atoi( value );
461         value = pos + 1;
462         
463         pos = strchr( value, '.' );
464         if ( !pos )
465                 return result;
466         *pos = '\0';
467         ss = atoi( value );
468         value = pos + 1;
469         
470         ms = atoi( value );
471         free( copy );
472         result = ( fps * ( ( (hh * 3600) + (mm * 60) + ss ) * 1000  + ms ) / 1000 + 0.5 );
473         
474         return result;
475 }
476
477 static void on_end_producer( deserialise_context context, const xmlChar *name )
478 {
479         enum service_type type;
480         mlt_service service = context_pop_service( context, &type );
481         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
482
483         if ( service != NULL && type == mlt_dummy_producer_type )
484         {
485                 mlt_service producer = NULL;
486
487                 qualify_property( context, properties, "resource" );
488                 char *resource = mlt_properties_get( properties, "resource" );
489
490                 // Let Kino-SMIL src be a synonym for resource
491                 if ( resource == NULL )
492                 {
493                         qualify_property( context, properties, "src" );
494                         resource = mlt_properties_get( properties, "src" );
495                 }
496
497                 // Instantiate the producer
498                 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
499                 {
500                         char temp[ 1024 ];
501                         strncpy( temp, mlt_properties_get( properties, "mlt_service" ), 1024 );
502                         if ( resource != NULL )
503                         {
504                                 strcat( temp, ":" );
505                                 strncat( temp, resource, 1023 - strlen( temp ) );
506                         }
507                         producer = MLT_SERVICE( mlt_factory_producer( "fezzik", temp ) );
508                 }
509
510                 // Just in case the plugin requested doesn't exist...
511                 if ( producer == NULL && resource != NULL )
512                         producer = MLT_SERVICE( mlt_factory_producer( "fezzik", resource ) );
513         
514                 if ( producer == NULL )
515                         producer = MLT_SERVICE( mlt_factory_producer( "fezzik", "+INVALID.txt" ) );
516
517                 if ( producer == NULL )
518                         producer = MLT_SERVICE( mlt_factory_producer( "fezzik", "colour:red" ) );
519
520                 // Track this producer
521                 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
522
523                 // Propogate the properties
524                 qualify_property( context, properties, "resource" );
525                 qualify_property( context, properties, "luma" );
526                 qualify_property( context, properties, "luma.resource" );
527                 qualify_property( context, properties, "composite.luma" );
528                 qualify_property( context, properties, "producer.resource" );
529
530                 // Handle in/out properties separately
531                 mlt_position in = -1;
532                 mlt_position out = -1;
533         
534                 // Get in
535                 if ( mlt_properties_get( properties, "in" ) != NULL )
536                         in = mlt_properties_get_position( properties, "in" );
537                 // Let Kino-SMIL clipBegin be a synonym for in
538                 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
539                 {
540                         if ( strchr( mlt_properties_get( properties, "clipBegin" ), ':' ) )
541                                 // Parse clock value
542                                 in = parse_clock_value( mlt_properties_get( properties, "clipBegin" ),
543                                         mlt_properties_get_double( mlt_producer_properties( MLT_PRODUCER( producer ) ), "fps" ) );
544                         else
545                                 // Parse frames value
546                                 in = mlt_properties_get_position( properties, "clipBegin" );
547                 }
548                 // Get out
549                 if ( mlt_properties_get( properties, "out" ) != NULL )
550                         out = mlt_properties_get_position( properties, "out" );
551                 // Let Kino-SMIL clipEnd be a synonym for out
552                 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
553                 {
554                         if ( strchr( mlt_properties_get( properties, "clipEnd" ), ':' ) )
555                                 // Parse clock value
556                                 out = parse_clock_value( mlt_properties_get( properties, "clipEnd" ),
557                                         mlt_properties_get_double( mlt_producer_properties( MLT_PRODUCER( producer ) ), "fps" ) );
558                         else
559                                 // Parse frames value
560                                 out = mlt_properties_get_position( properties, "clipEnd" );
561                 }
562                 // Remove in and out
563                 mlt_properties_set( properties, "in", NULL );
564                 mlt_properties_set( properties, "out", NULL );
565
566                 // Inherit the properties
567                 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
568
569                 // Attach all filters from service onto producer
570                 attach_filters( producer, service );
571
572                 // Add the producer to the producer map
573                 if ( mlt_properties_get( properties, "id" ) != NULL )
574                         mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
575
576                 // See if the producer should be added to a playlist or multitrack
577                 if ( add_producer( context, producer, in, out ) == 0 )
578                 {
579                         // Otherwise, set in and out on...
580                         if ( in != -1 || out != -1 )
581                         {
582                                 // Get the parent service
583                                 enum service_type type;
584                                 mlt_service parent = context_pop_service( context, &type );
585                                 if ( parent != NULL )
586                                 {
587                                         // Get the parent properties
588                                         properties = MLT_SERVICE_PROPERTIES( parent );
589                                 
590                                         char *resource = mlt_properties_get( properties, "resource" );
591                                 
592                                         // Put the parent producer back
593                                         context_push_service( context, parent, type );
594                                         
595                                         // If the parent is a track or entry
596                                         if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
597                                         {
598                                                 mlt_properties_set_position( properties, "in", in );
599                                                 mlt_properties_set_position( properties, "out", out );
600                                         }
601                                         else
602                                         {
603                                                 // Otherwise, set in and out on producer directly
604                                                 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
605                                         }
606                                 }
607                                 else
608                                 {
609                                         // Otherwise, set in and out on producer directly
610                                         mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
611                                 }
612                         }
613
614                         // Push the producer onto the stack
615                         context_push_service( context, producer, mlt_producer_type );
616                 }
617
618                 mlt_service_close( service );
619         }
620 }
621
622 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
623 {
624         // Get the playlist from the stack
625         enum service_type type;
626         mlt_service service = context_pop_service( context, &type );
627         mlt_position length = 0;
628         
629         if ( type == mlt_playlist_type && service != NULL )
630         {
631                 // Look for the length attribute
632                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
633                 {
634                         if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
635                         {
636                                 length = atoll( _s(atts[1]) );
637                                 break;
638                         }
639                 }
640
641                 // Append a blank to the playlist
642                 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
643
644                 // Push the playlist back onto the stack
645                 context_push_service( context, service, type );
646         }
647         else
648         {
649                 fprintf( stderr, "blank without a playlist - a definite no no\n" );
650         }
651 }
652
653 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
654 {
655         mlt_producer entry = NULL;
656         mlt_properties temp = mlt_properties_new( );
657
658         for ( ; atts != NULL && *atts != NULL; atts += 2 )
659         {
660                 mlt_properties_set( temp, (char*) atts[0], atts[1] == NULL ? "" : (char*) atts[1] );
661                 
662                 // Look for the producer attribute
663                 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
664                 {
665                         mlt_producer producer = mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL );
666                         if ( producer !=  NULL )
667                                 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
668                 }
669         }
670
671         // If we have a valid entry
672         if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
673         {
674                 mlt_playlist_clip_info info;
675                 enum service_type parent_type = invalid_type;
676                 mlt_service parent = context_pop_service( context, &parent_type );
677                 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
678
679                 if ( parent_type == mlt_playlist_type )
680                 {
681                         // Append the producer to the playlist
682                         if ( mlt_properties_get( temp, "in" ) != NULL || mlt_properties_get( temp, "out" ) != NULL )
683                         {
684                                 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer,
685                                         mlt_properties_get_position( temp, "in" ), 
686                                         mlt_properties_get_position( temp, "out" ) );
687                         }
688                         else
689                         {
690                                 mlt_playlist_append( MLT_PLAYLIST( parent ), producer );
691                         }
692
693                         // Handle the repeat property
694                         if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
695                         {
696                                 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
697                                                                                   mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
698                                                                                   mlt_properties_get_int( temp, "repeat" ) );
699                         }
700
701                         mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
702                         entry = info.cut;
703                 }
704                 else
705                 {
706                         fprintf( stderr, "Entry not part of a playlist...\n" );
707                 }
708
709                 context_push_service( context, parent, parent_type );
710         }
711
712         // Push the cut onto the stack
713         context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
714
715         mlt_properties_close( temp );
716 }
717
718 static void on_end_entry( deserialise_context context, const xmlChar *name )
719 {
720         // Get the entry from the stack
721         enum service_type entry_type = invalid_type;
722         mlt_service entry = context_pop_service( context, &entry_type );
723
724         if ( entry == NULL && entry_type != mlt_entry_type )
725         {
726                 fprintf( stderr, "Invalid state at end of entry\n" );
727         }
728 }
729
730 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
731 {
732         // use a dummy service to hold properties to allow arbitrary nesting
733         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
734         mlt_service_init( service, NULL );
735
736         // Push the dummy service onto the stack
737         context_push_service( context, service, mlt_entry_type );
738         
739         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
740         
741         for ( ; atts != NULL && *atts != NULL; atts += 2 )
742         {
743                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (char*) atts[0], atts[1] == NULL ? "" : (char*) atts[1] );
744                 
745                 // Look for the producer attribute
746                 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
747                 {
748                         mlt_producer producer = mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL );
749                         if ( producer !=  NULL )
750                                 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
751                 }
752         }
753 }
754
755 static void on_end_track( deserialise_context context, const xmlChar *name )
756 {
757         // Get the track from the stack
758         enum service_type track_type;
759         mlt_service track = context_pop_service( context, &track_type );
760
761         if ( track != NULL && track_type == mlt_entry_type )
762         {
763                 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
764                 enum service_type parent_type = invalid_type;
765                 mlt_service parent = context_pop_service( context, &parent_type );
766                 mlt_multitrack multitrack = NULL;
767
768                 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
769                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
770
771                 if ( parent_type == mlt_tractor_type )
772                         multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
773                 else if ( parent_type == mlt_multitrack_type )
774                         multitrack = MLT_MULTITRACK( parent );
775                 else
776                         fprintf( stderr, "track contained in an invalid container\n" );
777
778                 if ( multitrack != NULL )
779                 {
780                         // Set producer i/o if specified
781                         if ( mlt_properties_get( track_props, "in" ) != NULL ||
782                                  mlt_properties_get( track_props, "out" ) != NULL )
783                         {
784                                 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ),
785                                         mlt_properties_get_position( track_props, "in" ),
786                                         mlt_properties_get_position( track_props, "out" ) );
787                                 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
788                                 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
789                                 track_props = MLT_PRODUCER_PROPERTIES( cut );
790                                 mlt_producer_close( cut );
791                         }
792                         else
793                         {
794                                 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
795                         }
796
797                         // Set the hide state of the track producer
798                         char *hide_s = mlt_properties_get( track_props, "hide" );
799                         if ( hide_s != NULL )
800                         {
801                                 if ( strcmp( hide_s, "video" ) == 0 )
802                                         mlt_properties_set_int( producer_props, "hide", 1 );
803                                 else if ( strcmp( hide_s, "audio" ) == 0 )
804                                         mlt_properties_set_int( producer_props, "hide", 2 );
805                                 else if ( strcmp( hide_s, "both" ) == 0 )
806                                         mlt_properties_set_int( producer_props, "hide", 3 );
807                         }
808                 }
809
810                 if ( parent != NULL )
811                         context_push_service( context, parent, parent_type );
812
813                 mlt_service_close( track );
814         }
815         else
816         {
817                 fprintf( stderr, "Invalid state at end of track\n" );
818         }
819 }
820
821 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
822 {
823         // use a dummy service to hold properties to allow arbitrary nesting
824         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
825         mlt_service_init( service, NULL );
826
827         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
828
829         context_push_service( context, service, mlt_dummy_filter_type );
830
831         // Set the properties
832         for ( ; atts != NULL && *atts != NULL; atts += 2 )
833                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
834 }
835
836 static void on_end_filter( deserialise_context context, const xmlChar *name )
837 {
838         enum service_type type;
839         mlt_service service = context_pop_service( context, &type );
840         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
841
842         enum service_type parent_type = invalid_type;
843         mlt_service parent = context_pop_service( context, &parent_type );
844
845         if ( service != NULL && type == mlt_dummy_filter_type )
846         {
847                 mlt_service filter = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
848                 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
849
850                 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
851
852                 // Propogate the properties
853                 qualify_property( context, properties, "resource" );
854                 qualify_property( context, properties, "luma" );
855                 qualify_property( context, properties, "luma.resource" );
856                 qualify_property( context, properties, "composite.luma" );
857                 qualify_property( context, properties, "producer.resource" );
858                 mlt_properties_inherit( filter_props, properties );
859
860                 // Attach all filters from service onto filter
861                 attach_filters( filter, service );
862
863                 // Associate the filter with the parent
864                 if ( parent != NULL )
865                 {
866                         if ( parent_type == mlt_tractor_type )
867                         {
868                                 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
869                                 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
870                                 mlt_filter_set_in_and_out( MLT_FILTER( filter ), 
871                                                                                    mlt_properties_get_int( properties, "in" ),
872                                                                                    mlt_properties_get_int( properties, "out" ) );
873                         }
874                         else
875                         {
876                                 mlt_service_attach( parent, MLT_FILTER( filter ) );
877                         }
878
879                         // Put the parent back on the stack
880                         context_push_service( context, parent, parent_type );
881                 }
882                 else
883                 {
884                         fprintf( stderr, "filter closed with invalid parent...\n" );
885                 }
886
887                 // Close the dummy filter service
888                 mlt_service_close( service );
889         }
890         else
891         {
892                 fprintf( stderr, "Invalid top of stack on filter close\n" );
893         }
894 }
895
896 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
897 {
898         // use a dummy service to hold properties to allow arbitrary nesting
899         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
900         mlt_service_init( service, NULL );
901
902         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
903
904         context_push_service( context, service, mlt_dummy_transition_type );
905
906         // Set the properties
907         for ( ; atts != NULL && *atts != NULL; atts += 2 )
908                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
909 }
910
911 static void on_end_transition( deserialise_context context, const xmlChar *name )
912 {
913         enum service_type type;
914         mlt_service service = context_pop_service( context, &type );
915         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
916
917         enum service_type parent_type = invalid_type;
918         mlt_service parent = context_pop_service( context, &parent_type );
919
920         if ( service != NULL && type == mlt_dummy_transition_type )
921         {
922                 mlt_service effect = MLT_SERVICE( mlt_factory_transition(mlt_properties_get(properties,"mlt_service"), NULL ) );
923                 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
924
925                 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
926
927                 // Propogate the properties
928                 qualify_property( context, properties, "resource" );
929                 qualify_property( context, properties, "luma" );
930                 qualify_property( context, properties, "luma.resource" );
931                 qualify_property( context, properties, "composite.luma" );
932                 qualify_property( context, properties, "producer.resource" );
933                 mlt_properties_inherit( effect_props, properties );
934
935                 // Attach all filters from service onto effect
936                 attach_filters( effect, service );
937
938                 // Associate the filter with the parent
939                 if ( parent != NULL )
940                 {
941                         if ( parent_type == mlt_tractor_type )
942                         {
943                                 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
944                                 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
945                                         mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
946                                 mlt_field_plant_transition( field, MLT_TRANSITION( effect ), 
947                                                                                         mlt_properties_get_int( properties, "a_track" ),
948                                                                                         mlt_properties_get_int( properties, "b_track" ) );
949                                 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ), 
950                                                                                    mlt_properties_get_int( properties, "in" ),
951                                                                                    mlt_properties_get_int( properties, "out" ) );
952                         }
953                         else
954                         {
955                                 fprintf( stderr, "Misplaced transition - ignoring\n" );
956                         }
957
958                         // Put the parent back on the stack
959                         context_push_service( context, parent, parent_type );
960                 }
961                 else
962                 {
963                         fprintf( stderr, "transition closed with invalid parent...\n" );
964                 }
965
966                 // Close the dummy filter service
967                 mlt_service_close( service );
968         }
969         else
970         {
971                 fprintf( stderr, "Invalid top of stack on transition close\n" );
972         }
973 }
974
975 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
976 {
977         enum service_type type;
978         mlt_service service = context_pop_service( context, &type );
979         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
980         char *value = NULL;
981
982         if ( service != NULL )
983         {
984                 // Set the properties
985                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
986                 {
987                         if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
988                                 context->property = strdup( _s(atts[ 1 ]) );
989                         else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
990                                 value = _s(atts[ 1 ]);
991                 }
992
993                 if ( context->property != NULL )
994                         mlt_properties_set( properties, context->property, value == NULL ? "" : value );
995
996                 // Tell parser to collect any further nodes for serialisation
997                 context->is_value = 1;
998
999                 context_push_service( context, service, type );
1000         }
1001         else
1002         {
1003                 fprintf( stderr, "Property without a service '%s'?\n", ( char * )name );
1004         }
1005 }
1006
1007 static void on_end_property( deserialise_context context, const xmlChar *name )
1008 {
1009         enum service_type type;
1010         mlt_service service = context_pop_service( context, &type );
1011         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1012
1013         if ( service != NULL )
1014         {
1015                 // Tell parser to stop building a tree
1016                 context->is_value = 0;
1017         
1018                 // See if there is a xml tree for the value
1019                 if ( context->property != NULL && context->value_doc != NULL )
1020                 {
1021                         xmlChar *value;
1022                         int size;
1023                 
1024                         // Serialise the tree to get value
1025                         xmlDocDumpMemory( context->value_doc, &value, &size );
1026                         mlt_properties_set( properties, context->property, _s(value) );
1027                         xmlFree( value );
1028                         xmlFreeDoc( context->value_doc );
1029                         context->value_doc = NULL;
1030                 }
1031
1032                 // Close this property handling
1033                 free( context->property );
1034                 context->property = NULL;
1035
1036                 context_push_service( context, service, type );
1037         }
1038         else
1039         {
1040                 fprintf( stderr, "Property without a service '%s'??\n", (char *)name );
1041         }
1042 }
1043
1044 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1045 {
1046         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1047         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1048         
1049 //printf("on_start_element: %s\n", name );
1050         context->branch[ context->depth ] ++;
1051         context->depth ++;
1052         
1053         // Build a tree from nodes within a property value
1054         if ( context->is_value == 1 )
1055         {
1056                 xmlNodePtr node = xmlNewNode( NULL, name );
1057                 
1058                 if ( context->value_doc == NULL )
1059                 {
1060                         // Start a new tree
1061                         context->value_doc = xmlNewDoc( _x("1.0") );
1062                         xmlDocSetRootElement( context->value_doc, node );
1063                 }
1064                 else
1065                 {
1066                         // Append child to tree
1067                         xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1068                 }
1069                 context_push_node( context, node );
1070                 
1071                 // Set the attributes
1072                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1073                         xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1074         }
1075         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1076                 on_start_tractor( context, name, atts );
1077         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1078                 on_start_multitrack( context, name, atts );
1079         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1080                 on_start_playlist( context, name, atts );
1081         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1082                 on_start_producer( context, name, atts );
1083         else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1084                 on_start_blank( context, name, atts );
1085         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1086                 on_start_entry( context, name, atts );
1087         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1088                 on_start_track( context, name, atts );
1089         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1090                 on_start_filter( context, name, atts );
1091         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1092                 on_start_transition( context, name, atts );
1093         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1094                 on_start_property( context, name, atts );
1095         else if ( xmlStrcmp( name, _x("westley") ) == 0 )
1096                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1097                         mlt_properties_set( context->producer_map, ( char * )atts[ 0 ], ( char * )atts[ 1 ] );
1098 }
1099
1100 static void on_end_element( void *ctx, const xmlChar *name )
1101 {
1102         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1103         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1104         
1105 //printf("on_end_element: %s\n", name );
1106         if ( context->is_value == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1107                 context_pop_node( context );
1108         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1109                 on_end_multitrack( context, name );
1110         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1111                 on_end_playlist( context, name );
1112         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1113                 on_end_track( context, name );
1114         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1115                 on_end_entry( context, name );
1116         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1117                 on_end_tractor( context, name );
1118         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1119                 on_end_property( context, name );
1120         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1121                 on_end_producer( context, name );
1122         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1123                 on_end_filter( context, name );
1124         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1125                 on_end_transition( context, name );
1126
1127         context->branch[ context->depth ] = 0;
1128         context->depth --;
1129 }
1130
1131 static void on_characters( void *ctx, const xmlChar *ch, int len )
1132 {
1133         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1134         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1135         char *value = calloc( len + 1, 1 );
1136         enum service_type type;
1137         mlt_service service = context_pop_service( context, &type );
1138         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1139
1140         if ( service != NULL )
1141                 context_push_service( context, service, type );
1142
1143         value[ len ] = 0;
1144         strncpy( value, (const char*) ch, len );
1145
1146         if ( context->stack_node_size > 0 )
1147                 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1148
1149         // libxml2 generates an on_characters immediately after a get_entity within
1150         // an element value, and we ignore it because it is called again during
1151         // actual substitution.
1152         else if ( context->property != NULL && context->entity_is_replace == 0 )
1153         {
1154                 char *s = mlt_properties_get( properties, context->property );
1155                 if ( s != NULL )
1156                 {
1157                         // Append new text to existing content
1158                         char *new = calloc( strlen( s ) + len + 1, 1 );
1159                         strcat( new, s );
1160                         strcat( new, value );
1161                         mlt_properties_set( properties, context->property, new );
1162                         free( new );
1163                 }
1164                 else
1165                         mlt_properties_set( properties, context->property, value );
1166         }
1167         context->entity_is_replace = 0;
1168         
1169         free( value);
1170 }
1171
1172 /** Convert parameters parsed from resource into entity declarations.
1173 */
1174 static void params_to_entities( deserialise_context context )
1175 {
1176         if ( context->params != NULL )
1177         {       
1178                 int i;
1179                 
1180                 // Add our params as entitiy declarations
1181                 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1182                 {
1183                         xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1184                         xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1185                                 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1186                 }
1187
1188                 // Flag completion
1189                 mlt_properties_close( context->params );
1190                 context->params = NULL;
1191         }
1192 }
1193
1194 // The following 3 facilitate entity substitution in the SAX parser
1195 static void on_internal_subset( void *ctx, const xmlChar* name,
1196         const xmlChar* publicId, const xmlChar* systemId )
1197 {
1198         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1199         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1200         
1201         context->publicId = publicId;
1202         context->systemId = systemId;
1203         xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1204         
1205         // Override default entities with our parameters
1206         params_to_entities( context );
1207 }
1208
1209 // TODO: Check this with Dan... I think this is for westley parameterisation
1210 // but it's breaking standard escaped entities (like &lt; etc).
1211 static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
1212         const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1213 {
1214         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1215         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1216         
1217         xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1218 }
1219
1220 // TODO: Check this functionality (see on_entity_declaration)
1221 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1222 {
1223         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1224         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1225         xmlEntityPtr e = NULL;
1226
1227         // Setup for entity declarations if not ready
1228         if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1229         {
1230                 xmlCreateIntSubset( context->entity_doc, _x("westley"), _x(""), _x("") );
1231                 context->publicId = _x("");
1232                 context->systemId = _x("");
1233         }
1234
1235         // Add our parameters if not already
1236         params_to_entities( context );
1237         
1238         e = xmlGetPredefinedEntity( name );
1239         
1240         // Send signal to on_characters that an entity substitutin is pending
1241         if ( e == NULL )
1242         {
1243                 e = xmlGetDocEntity( context->entity_doc, name );
1244                 if ( e != NULL )
1245                         context->entity_is_replace = 1;
1246         }
1247         
1248         return e;
1249 }
1250
1251 /** Convert a hexadecimal character to its value.
1252 */
1253 static int tohex( char p )
1254 {
1255         return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1256 }
1257
1258 /** Decode a url-encoded string containing hexadecimal character sequences.
1259 */
1260 static char *url_decode( char *dest, char *src )
1261 {
1262         char *p = dest;
1263         
1264         while ( *src )
1265         {
1266                 if ( *src == '%' )
1267                 {
1268                         *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1269                         src += 3;
1270                 }
1271                 else
1272                 {
1273                         *p ++ = *src ++;
1274                 }
1275         }
1276
1277         *p = *src;
1278         return dest;
1279 }
1280
1281 /** Extract the filename from a URL attaching parameters to a properties list.
1282 */
1283 static void parse_url( mlt_properties properties, char *url )
1284 {
1285         int i;
1286         int n = strlen( url );
1287         char *name = NULL;
1288         char *value = NULL;
1289         
1290         for ( i = 0; i < n; i++ )
1291         {
1292                 switch ( url[ i ] )
1293                 {
1294                         case '?':
1295                                 url[ i++ ] = '\0';
1296                                 name = &url[ i ];
1297                                 break;
1298                         
1299                         case ':':
1300                         case '=':
1301                                 url[ i++ ] = '\0';
1302                                 value = &url[ i ];
1303                                 break;
1304                         
1305                         case '&':
1306                                 url[ i++ ] = '\0';
1307                                 if ( name != NULL && value != NULL )
1308                                         mlt_properties_set( properties, name, value );
1309                                 name = &url[ i ];
1310                                 value = NULL;
1311                                 break;
1312                 }
1313         }
1314         if ( name != NULL && value != NULL )
1315                 mlt_properties_set( properties, name, value );
1316 }
1317
1318 // Quick workaround to avoid unecessary libxml2 warnings
1319 static int file_exists( char *file )
1320 {
1321         char *name = strdup( file );
1322         int exists = 0;
1323         if ( name != NULL && strchr( name, '?' ) )
1324                 *( strchr( name, '?' ) ) = '\0';
1325         if ( name != NULL )
1326         {
1327                 FILE *f = fopen( name, "r" );
1328                 exists = f != NULL;
1329                 if ( exists ) fclose( f );
1330         }
1331         free( name );
1332         return exists;
1333 }
1334
1335 mlt_producer producer_westley_init( int info, char *data )
1336 {
1337         xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1338         struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1339         mlt_properties properties = NULL;
1340         int i = 0;
1341         struct _xmlParserCtxt *xmlcontext;
1342         int well_formed = 0;
1343         char *filename = NULL;
1344
1345         if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1346                 return NULL;
1347
1348         context = calloc( 1, sizeof( struct deserialise_context_s ) );
1349         if ( context == NULL )
1350                 return NULL;
1351
1352         context->producer_map = mlt_properties_new();
1353         context->destructors = mlt_properties_new();
1354         context->params = mlt_properties_new();
1355
1356         // Decode URL and parse parameters
1357         mlt_properties_set( context->producer_map, "root", "" );
1358         if ( info == 0 )
1359         {
1360                 filename = strdup( data );
1361                 parse_url( context->params, url_decode( filename, data ) );
1362
1363                 // We need the directory prefix which was used for the westley
1364                 if ( strchr( filename, '/' ) )
1365                 {
1366                         char *root = NULL;
1367                         mlt_properties_set( context->producer_map, "root", filename );
1368                         root = mlt_properties_get( context->producer_map, "root" );
1369                         *( strrchr( root, '/' ) ) = '\0';
1370
1371                         // If we don't have an absolute path here, we're heading for disaster...
1372                         if ( root[ 0 ] != '/' )
1373                         {
1374                                 char *cwd = getcwd( NULL, 0 );
1375                                 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1376                                 sprintf( real, "%s/%s", cwd, root );
1377                                 mlt_properties_set( context->producer_map, "root", real );
1378                                 free( real );
1379                                 free( cwd );
1380                         }
1381                 }
1382         }
1383
1384         // We need to track the number of registered filters
1385         mlt_properties_set_int( context->destructors, "registered", 0 );
1386
1387         // Setup SAX callbacks
1388         sax->startElement = on_start_element;
1389         sax->endElement = on_end_element;
1390         sax->characters = on_characters;
1391         sax->cdataBlock = on_characters;
1392         sax->internalSubset = on_internal_subset;
1393         sax->entityDecl = on_entity_declaration;
1394         sax->getEntity = on_get_entity;
1395
1396         // Setup libxml2 SAX parsing
1397         xmlInitParser(); 
1398         xmlSubstituteEntitiesDefault( 1 );
1399         // This is used to facilitate entity substitution in the SAX parser
1400         context->entity_doc = xmlNewDoc( _x("1.0") );
1401         if ( info == 0 )
1402                 xmlcontext = xmlCreateFileParserCtxt( filename );
1403         else
1404                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1405
1406         // Invalid context - clean up and return NULL
1407         if ( xmlcontext == NULL )
1408         {
1409                 mlt_properties_close( context->producer_map );
1410                 mlt_properties_close( context->destructors );
1411                 mlt_properties_close( context->params );
1412                 free( context );
1413                 free( sax );
1414                 free( filename );
1415                 return NULL;
1416         }
1417
1418         xmlcontext->sax = sax;
1419         xmlcontext->_private = ( void* )context;
1420         
1421         // Parse
1422         xmlParseDocument( xmlcontext );
1423         well_formed = xmlcontext->wellFormed;
1424         
1425         // Cleanup after parsing
1426         xmlFreeDoc( context->entity_doc );
1427         free( sax );
1428         xmlcontext->sax = NULL;
1429         xmlcontext->_private = NULL;
1430         xmlFreeParserCtxt( xmlcontext );
1431         xmlMemoryDump( ); // for debugging
1432
1433         // Get the last producer on the stack
1434         enum service_type type;
1435         mlt_service service = context_pop_service( context, &type );
1436         if ( well_formed && service != NULL )
1437         {
1438                 // Verify it is a producer service (mlt_type="mlt_producer")
1439                 // (producer, playlist, multitrack)
1440                 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1441                 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1442                         service = NULL;
1443         }
1444
1445 #ifdef DEBUG
1446         xmlDocPtr doc = westley_make_doc( service );
1447         xmlDocFormatDump( stdout, doc, 1 );
1448         xmlFreeDoc( doc );
1449         service = NULL;
1450 #endif
1451         
1452         if ( well_formed && service != NULL )
1453         {
1454                 char *title = mlt_properties_get( context->producer_map, "title" );
1455                 
1456                 // Need the complete producer list for various reasons
1457                 properties = context->destructors;
1458
1459                 // Now make sure we don't have a reference to the service in the properties
1460                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1461                 {
1462                         char *name = mlt_properties_get_name( properties, i );
1463                         if ( mlt_properties_get_data( properties, name, NULL ) == service )
1464                         {
1465                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1466                                 break;
1467                         }
1468                 }
1469
1470                 // We are done referencing destructor property list
1471                 // Set this var to service properties for convenience
1472                 properties = MLT_SERVICE_PROPERTIES( service );
1473         
1474                 // Assign the title
1475                 mlt_properties_set( properties, "title", title );
1476
1477                 // Optimise for overlapping producers
1478                 mlt_producer_optimise( MLT_PRODUCER( service ) );
1479
1480                 // Handle deep copies
1481                 if ( getenv( "MLT_WESTLEY_DEEP" ) == NULL )
1482                 {
1483                         // Now assign additional properties
1484                         if ( info == 0 )
1485                                 mlt_properties_set( properties, "resource", data );
1486
1487                         // This tells consumer_westley not to deep copy
1488                         mlt_properties_set( properties, "westley", "was here" );
1489                 }
1490                 else
1491                 {
1492                         // Allow the project to be edited
1493                         mlt_properties_set( properties, "_westley", "was here" );
1494                         mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1495                 }
1496         }
1497         else
1498         {
1499                 // Return null if not well formed
1500                 service = NULL;
1501         }
1502
1503         // Clean up
1504         mlt_properties_close( context->producer_map );
1505         if ( context->params != NULL )
1506                 mlt_properties_close( context->params );
1507         mlt_properties_close( context->destructors );
1508         free( context );
1509         free( filename );
1510
1511         return MLT_PRODUCER( service );
1512 }