]> git.sesse.net Git - mlt/blob - src/modules/xml/producer_xml.c
Convert producer_xml to use mlt_log rather than fprintf.
[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 <framework/mlt_log.h>
26 #include <stdlib.h>
27 #include <string.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                                 mlt_log_warning( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] 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                 // Propagate 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                 mlt_log_error( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
997                 }
998
999                 // Close the dummy filter service
1000                 mlt_service_close( service );
1001         }
1002         else
1003         {
1004                 mlt_log_error( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] 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                                 mlt_log_warning( NULL, "[producer_xml] 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                         mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1086                 }
1087
1088                 // Close the dummy filter service
1089                 mlt_service_close( service );
1090         }
1091         else
1092         {
1093                 mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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                 mlt_log_error( NULL, "[producer_xml] 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         if ( context->pass == 0 )
1250         {
1251                 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1252                      xmlStrcmp( name, _x("profile") ) == 0 ||
1253                      xmlStrcmp( name, _x("profileinfo") ) == 0 )
1254                         on_start_profile( context, name, atts );
1255                 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1256                         context->multi_consumer++;
1257                 return;
1258         }
1259         context->branch[ context->depth ] ++;
1260         context->depth ++;
1261         
1262         // Build a tree from nodes within a property value
1263         if ( context->is_value == 1 && context->pass == 1 )
1264         {
1265                 xmlNodePtr node = xmlNewNode( NULL, name );
1266                 
1267                 if ( context->value_doc == NULL )
1268                 {
1269                         // Start a new tree
1270                         context->value_doc = xmlNewDoc( _x("1.0") );
1271                         xmlDocSetRootElement( context->value_doc, node );
1272                 }
1273                 else
1274                 {
1275                         // Append child to tree
1276                         xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1277                 }
1278                 context_push_node( context, node );
1279                 
1280                 // Set the attributes
1281                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1282                         xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1283         }
1284         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1285                 on_start_tractor( context, name, atts );
1286         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1287                 on_start_multitrack( context, name, atts );
1288         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1289                 on_start_playlist( context, name, atts );
1290         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1291                 on_start_producer( context, name, atts );
1292         else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1293                 on_start_blank( context, name, atts );
1294         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1295                 on_start_entry( context, name, atts );
1296         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1297                 on_start_track( context, name, atts );
1298         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1299                 on_start_filter( context, name, atts );
1300         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1301                 on_start_transition( context, name, atts );
1302         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1303                 on_start_property( context, name, atts );
1304         else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1305                 on_start_consumer( context, name, atts );
1306         else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1307         {
1308                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1309                 {
1310                         if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1311                                 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1312                         else if ( !context->lc_numeric )
1313                                 context->lc_numeric = strdup( _s( atts[1] ) );
1314                 }
1315         }
1316 }
1317
1318 static void on_end_element( void *ctx, const xmlChar *name )
1319 {
1320         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1321         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1322         
1323         if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1324                 context_pop_node( context );
1325         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1326                 on_end_multitrack( context, name );
1327         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1328                 on_end_playlist( context, name );
1329         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1330                 on_end_track( context, name );
1331         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1332                 on_end_entry( context, name );
1333         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1334                 on_end_tractor( context, name );
1335         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1336                 on_end_property( context, name );
1337         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1338                 on_end_producer( context, name );
1339         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1340                 on_end_filter( context, name );
1341         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1342                 on_end_transition( context, name );
1343         else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1344                 on_end_consumer( context, name );
1345
1346         context->branch[ context->depth ] = 0;
1347         context->depth --;
1348 }
1349
1350 static void on_characters( void *ctx, const xmlChar *ch, int len )
1351 {
1352         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1353         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1354         char *value = calloc( len + 1, 1 );
1355         enum service_type type;
1356         mlt_service service = context_pop_service( context, &type );
1357         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1358
1359         if ( service != NULL )
1360                 context_push_service( context, service, type );
1361
1362         value[ len ] = 0;
1363         strncpy( value, (const char*) ch, len );
1364
1365         if ( context->stack_node_size > 0 )
1366                 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1367
1368         // libxml2 generates an on_characters immediately after a get_entity within
1369         // an element value, and we ignore it because it is called again during
1370         // actual substitution.
1371         else if ( context->property != NULL && context->entity_is_replace == 0 )
1372         {
1373                 char *s = mlt_properties_get( properties, context->property );
1374                 if ( s != NULL )
1375                 {
1376                         // Append new text to existing content
1377                         char *new = calloc( strlen( s ) + len + 1, 1 );
1378                         strcat( new, s );
1379                         strcat( new, value );
1380                         mlt_properties_set( properties, context->property, new );
1381                         free( new );
1382                 }
1383                 else
1384                         mlt_properties_set( properties, context->property, value );
1385         }
1386         context->entity_is_replace = 0;
1387         
1388         free( value);
1389 }
1390
1391 /** Convert parameters parsed from resource into entity declarations.
1392 */
1393 static void params_to_entities( deserialise_context context )
1394 {
1395         if ( context->params != NULL )
1396         {       
1397                 int i;
1398                 
1399                 // Add our params as entitiy declarations
1400                 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1401                 {
1402                         xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1403                         xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1404                                 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1405                 }
1406
1407                 // Flag completion
1408                 mlt_properties_close( context->params );
1409                 context->params = NULL;
1410         }
1411 }
1412
1413 // The following 3 facilitate entity substitution in the SAX parser
1414 static void on_internal_subset( void *ctx, const xmlChar* name,
1415         const xmlChar* publicId, const xmlChar* systemId )
1416 {
1417         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1418         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1419         
1420         context->publicId = publicId;
1421         context->systemId = systemId;
1422         xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1423         
1424         // Override default entities with our parameters
1425         params_to_entities( context );
1426 }
1427
1428 // TODO: Check this with Dan... I think this is for parameterisation
1429 // but it's breaking standard escaped entities (like &lt; etc).
1430 static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
1431         const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1432 {
1433         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1434         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1435         
1436         xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1437 }
1438
1439 // TODO: Check this functionality (see on_entity_declaration)
1440 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1441 {
1442         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1443         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1444         xmlEntityPtr e = NULL;
1445
1446         // Setup for entity declarations if not ready
1447         if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1448         {
1449                 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1450                 context->publicId = _x("");
1451                 context->systemId = _x("");
1452         }
1453
1454         // Add our parameters if not already
1455         params_to_entities( context );
1456         
1457         e = xmlGetPredefinedEntity( name );
1458         
1459         // Send signal to on_characters that an entity substitutin is pending
1460         if ( e == NULL )
1461         {
1462                 e = xmlGetDocEntity( context->entity_doc, name );
1463                 if ( e != NULL )
1464                         context->entity_is_replace = 1;
1465         }
1466         
1467         return e;
1468 }
1469
1470 static void     on_error( void * ctx, const char * msg, ... )
1471 {
1472         struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1473
1474         switch( err_ptr->level )
1475         {
1476         case XML_ERR_WARNING:
1477                 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1478                                          err_ptr->message, err_ptr->line, err_ptr->int2 );
1479                 break;
1480         case XML_ERR_ERROR:
1481                 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1482                                        err_ptr->message, err_ptr->line, err_ptr->int2 );
1483                 break;
1484         default:
1485         case XML_ERR_FATAL:
1486                 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1487                                        err_ptr->message, err_ptr->line, err_ptr->int2 );
1488                 break;
1489         }
1490 }
1491
1492 /** Convert a hexadecimal character to its value.
1493 */
1494 static int tohex( char p )
1495 {
1496         return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1497 }
1498
1499 /** Decode a url-encoded string containing hexadecimal character sequences.
1500 */
1501 static char *url_decode( char *dest, char *src )
1502 {
1503         char *p = dest;
1504         
1505         while ( *src )
1506         {
1507                 if ( *src == '%' )
1508                 {
1509                         *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1510                         src += 3;
1511                 }
1512                 else
1513                 {
1514                         *p ++ = *src ++;
1515                 }
1516         }
1517
1518         *p = *src;
1519         return dest;
1520 }
1521
1522 /** Extract the filename from a URL attaching parameters to a properties list.
1523 */
1524 static void parse_url( mlt_properties properties, char *url )
1525 {
1526         int i;
1527         int n = strlen( url );
1528         char *name = NULL;
1529         char *value = NULL;
1530         
1531         for ( i = 0; i < n; i++ )
1532         {
1533                 switch ( url[ i ] )
1534                 {
1535                         case '?':
1536                                 url[ i++ ] = '\0';
1537                                 name = &url[ i ];
1538                                 break;
1539                         
1540                         case ':':
1541                         case '=':
1542                                 url[ i++ ] = '\0';
1543                                 value = &url[ i ];
1544                                 break;
1545                         
1546                         case '&':
1547                                 url[ i++ ] = '\0';
1548                                 if ( name != NULL && value != NULL )
1549                                         mlt_properties_set( properties, name, value );
1550                                 name = &url[ i ];
1551                                 value = NULL;
1552                                 break;
1553                 }
1554         }
1555         if ( name != NULL && value != NULL )
1556                 mlt_properties_set( properties, name, value );
1557 }
1558
1559 // Quick workaround to avoid unecessary libxml2 warnings
1560 static int file_exists( char *file )
1561 {
1562         char *name = strdup( file );
1563         int exists = 0;
1564         if ( name != NULL && strchr( name, '?' ) )
1565                 *( strchr( name, '?' ) ) = '\0';
1566         if ( name != NULL )
1567         {
1568                 FILE *f = fopen( name, "r" );
1569                 exists = f != NULL;
1570                 if ( exists ) fclose( f );
1571         }
1572         free( name );
1573         return exists;
1574 }
1575
1576 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1577 {
1578         xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1579         struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1580         mlt_properties properties = NULL;
1581         int i = 0;
1582         struct _xmlParserCtxt *xmlcontext;
1583         int well_formed = 0;
1584         char *filename = NULL;
1585         int info = strcmp( id, "xml-string" ) ? 0 : 1;
1586
1587         if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1588                 return NULL;
1589
1590         context = calloc( 1, sizeof( struct deserialise_context_s ) );
1591         if ( context == NULL )
1592                 return NULL;
1593
1594         context->producer_map = mlt_properties_new();
1595         context->destructors = mlt_properties_new();
1596         context->params = mlt_properties_new();
1597         context->profile = profile;
1598
1599         // Decode URL and parse parameters
1600         mlt_properties_set( context->producer_map, "root", "" );
1601         if ( info == 0 )
1602         {
1603                 filename = strdup( data );
1604                 parse_url( context->params, url_decode( filename, data ) );
1605
1606                 // We need the directory prefix which was used for the xml
1607                 if ( strchr( filename, '/' ) )
1608                 {
1609                         char *root = NULL;
1610                         mlt_properties_set( context->producer_map, "root", filename );
1611                         root = mlt_properties_get( context->producer_map, "root" );
1612                         *( strrchr( root, '/' ) ) = '\0';
1613
1614                         // If we don't have an absolute path here, we're heading for disaster...
1615                         if ( root[ 0 ] != '/' )
1616                         {
1617                                 char *cwd = getcwd( NULL, 0 );
1618                                 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1619                                 sprintf( real, "%s/%s", cwd, root );
1620                                 mlt_properties_set( context->producer_map, "root", real );
1621                                 free( real );
1622                                 free( cwd );
1623                         }
1624                 }
1625         }
1626
1627         // We need to track the number of registered filters
1628         mlt_properties_set_int( context->destructors, "registered", 0 );
1629
1630         // Setup SAX callbacks for first pass
1631         sax->startElement = on_start_element;
1632         sax->warning = on_error;
1633         sax->error = on_error;
1634         sax->fatalError = on_error;
1635
1636         // Setup libxml2 SAX parsing
1637         xmlInitParser(); 
1638         xmlSubstituteEntitiesDefault( 1 );
1639         // This is used to facilitate entity substitution in the SAX parser
1640         context->entity_doc = xmlNewDoc( _x("1.0") );
1641         if ( info == 0 )
1642                 xmlcontext = xmlCreateFileParserCtxt( filename );
1643         else
1644                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1645
1646         // Invalid context - clean up and return NULL
1647         if ( xmlcontext == NULL )
1648         {
1649                 mlt_properties_close( context->producer_map );
1650                 mlt_properties_close( context->destructors );
1651                 mlt_properties_close( context->params );
1652                 free( context );
1653                 free( sax );
1654                 free( filename );
1655                 return NULL;
1656         }
1657
1658         // Parse
1659         xmlcontext->sax = sax;
1660         xmlcontext->_private = ( void* )context;        
1661         xmlParseDocument( xmlcontext );
1662         well_formed = xmlcontext->wellFormed;
1663         
1664         // Cleanup after parsing
1665         xmlcontext->sax = NULL;
1666         xmlcontext->_private = NULL;
1667         xmlFreeParserCtxt( xmlcontext );
1668         context->stack_node_size = 0;
1669         context->stack_service_size = 0;
1670
1671         // Bad xml - clean up and return NULL
1672         if ( !well_formed )
1673         {
1674                 mlt_properties_close( context->producer_map );
1675                 mlt_properties_close( context->destructors );
1676                 mlt_properties_close( context->params );
1677                 xmlFreeDoc( context->entity_doc );
1678                 free( context );
1679                 free( sax );
1680                 free( filename );
1681                 return NULL;
1682         }
1683
1684         // Setup the second pass
1685         context->pass ++;
1686         if ( info == 0 )
1687                 xmlcontext = xmlCreateFileParserCtxt( filename );
1688         else
1689                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1690
1691         // Invalid context - clean up and return NULL
1692         if ( xmlcontext == NULL )
1693         {
1694                 mlt_properties_close( context->producer_map );
1695                 mlt_properties_close( context->destructors );
1696                 mlt_properties_close( context->params );
1697                 xmlFreeDoc( context->entity_doc );
1698                 free( context );
1699                 free( sax );
1700                 free( filename );
1701                 return NULL;
1702         }
1703
1704         // Setup SAX callbacks for second pass
1705         sax->endElement = on_end_element;
1706         sax->characters = on_characters;
1707         sax->cdataBlock = on_characters;
1708         sax->internalSubset = on_internal_subset;
1709         sax->entityDecl = on_entity_declaration;
1710         sax->getEntity = on_get_entity;
1711
1712         // Parse
1713         xmlcontext->sax = sax;
1714         xmlcontext->_private = ( void* )context;
1715         xmlParseDocument( xmlcontext );
1716         well_formed = xmlcontext->wellFormed;
1717
1718         // Cleanup after parsing
1719         xmlFreeDoc( context->entity_doc );
1720         free( sax );
1721         xmlMemoryDump( ); // for debugging
1722
1723         // Get the last producer on the stack
1724         enum service_type type;
1725         mlt_service service = context_pop_service( context, &type );
1726         if ( well_formed && service != NULL )
1727         {
1728                 // Verify it is a producer service (mlt_type="mlt_producer")
1729                 // (producer, playlist, multitrack)
1730                 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1731                 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1732                         service = NULL;
1733         }
1734
1735 #ifdef DEBUG
1736         xmlDocPtr doc = xml_make_doc( service );
1737         xmlDocFormatDump( stdout, doc, 1 );
1738         xmlFreeDoc( doc );
1739         service = NULL;
1740 #endif
1741         
1742         if ( well_formed && service != NULL )
1743         {
1744                 char *title = mlt_properties_get( context->producer_map, "title" );
1745                 
1746                 // Need the complete producer list for various reasons
1747                 properties = context->destructors;
1748
1749                 // Now make sure we don't have a reference to the service in the properties
1750                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1751                 {
1752                         char *name = mlt_properties_get_name( properties, i );
1753                         if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1754                         {
1755                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1756                                 break;
1757                         }
1758                 }
1759
1760                 // We are done referencing destructor property list
1761                 // Set this var to service properties for convenience
1762                 properties = MLT_SERVICE_PROPERTIES( service );
1763         
1764                 // Assign the title
1765                 mlt_properties_set( properties, "title", title );
1766
1767                 // Optimise for overlapping producers
1768                 mlt_producer_optimise( MLT_PRODUCER( service ) );
1769
1770                 // Handle deep copies
1771                 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1772                 {
1773                         // Now assign additional properties
1774                         if ( info == 0 )
1775                                 mlt_properties_set( properties, "resource", data );
1776
1777                         // This tells consumer_xml not to deep copy
1778                         mlt_properties_set( properties, "xml", "was here" );
1779                 }
1780                 else
1781                 {
1782                         // Allow the project to be edited
1783                         mlt_properties_set( properties, "_xml", "was here" );
1784                         mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1785                 }
1786
1787                 // Make consumer available
1788                 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1789                 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1790                         (mlt_destructor) mlt_consumer_close, NULL );
1791         }
1792         else
1793         {
1794                 // Return null if not well formed
1795                 service = NULL;
1796         }
1797
1798         // Clean up
1799         mlt_properties_close( context->producer_map );
1800         if ( context->params != NULL )
1801                 mlt_properties_close( context->params );
1802         mlt_properties_close( context->destructors );
1803         if ( context->lc_numeric )
1804                 free( context->lc_numeric );
1805         free( context );
1806         free( filename );
1807
1808         return MLT_PRODUCER( service );
1809 }