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