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