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