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