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