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