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>
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.
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.
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
21 // TODO: destroy unreferenced producers (they are currently destroyed
22 // when the returned producer is closed).
24 #include <framework/mlt.h>
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
35 #define STACK_SIZE 1000
36 #define BRANCH_SIG_LEN 4000
38 #define _x (const xmlChar*)
39 #define _s (const char*)
43 extern xmlDocPtr xml_make_doc( mlt_service service );
60 mlt_dummy_filter_type,
61 mlt_dummy_transition_type,
62 mlt_dummy_producer_type,
65 struct deserialise_context_s
67 enum service_type stack_types[ STACK_SIZE ];
68 mlt_service stack_service[ STACK_SIZE ];
69 int stack_service_size;
70 mlt_properties producer_map;
71 mlt_properties destructors;
75 xmlNodePtr stack_node[ STACK_SIZE ];
78 int entity_is_replace;
80 int branch[ STACK_SIZE ];
81 const xmlChar *publicId;
82 const xmlChar *systemId;
83 mlt_properties params;
87 typedef struct deserialise_context_s *deserialise_context;
89 /** Convert the numerical current branch address to a dot-delimited string.
91 static char *serialise_branch( deserialise_context this, char *s )
96 for ( i = 0; i < this->depth; i++ )
98 int len = strlen( s );
99 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", this->branch[ i ] );
107 static int context_push_service( deserialise_context this, mlt_service that, enum service_type type )
109 int ret = this->stack_service_size >= STACK_SIZE - 1;
112 this->stack_service[ this->stack_service_size ] = that;
113 this->stack_types[ this->stack_service_size++ ] = type;
115 // Record the tree branch on which this service lives
116 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
118 char s[ BRANCH_SIG_LEN ];
119 mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( this, s ) );
128 static mlt_service context_pop_service( deserialise_context this, enum service_type *type )
130 mlt_service result = NULL;
131 if ( this->stack_service_size > 0 )
133 result = this->stack_service[ -- this->stack_service_size ];
135 *type = this->stack_types[ this->stack_service_size ];
143 static int context_push_node( deserialise_context this, xmlNodePtr node )
145 int ret = this->stack_node_size >= STACK_SIZE - 1;
147 this->stack_node[ this->stack_node_size ++ ] = node;
154 static xmlNodePtr context_pop_node( deserialise_context this )
156 xmlNodePtr result = NULL;
157 if ( this->stack_node_size > 0 )
158 result = this->stack_node[ -- this->stack_node_size ];
163 // Set the destructor on a new service
164 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
166 int registered = mlt_properties_get_int( properties, "registered" );
167 char *key = mlt_properties_get( properties, "registered" );
168 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
169 mlt_properties_set_int( properties, "registered", ++ registered );
173 // Prepend the property value with the document root
174 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
176 char *resource = mlt_properties_get( properties, name );
177 if ( resource != NULL && resource[0] )
179 // Qualify file name properties
180 char *root = mlt_properties_get( context->producer_map, "root" );
181 if ( root != NULL && strcmp( root, "" ) )
183 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
184 if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
186 strcpy( full_resource, root );
187 strcat( full_resource, "/" );
188 strcat( full_resource, resource );
192 strcpy( full_resource, resource );
194 mlt_properties_set( properties, name, full_resource );
195 free( full_resource );
201 /** This function adds a producer to a playlist or multitrack when
202 there is no entry or track element.
205 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
207 // Return value (0 = service remains top of stack, 1 means it can be removed)
210 // Get the parent producer
211 enum service_type type = mlt_invalid_type;
212 mlt_service container = context_pop_service( context, &type );
215 if ( service != NULL && container != NULL )
217 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
218 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
219 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
224 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
225 char *hide_s = mlt_properties_get( properties, "hide" );
227 // Indicate that this service is no longer top of stack
232 case mlt_tractor_type:
234 mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
235 mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
238 case mlt_multitrack_type:
240 mlt_multitrack_connect( MLT_MULTITRACK( container ),
241 MLT_PRODUCER( service ),
242 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
245 case mlt_playlist_type:
247 mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
252 fprintf( stderr, "Producer defined inside something that isn't a container\n" );
256 // Set the hide state of the track producer
257 if ( hide_s != NULL )
259 if ( strcmp( hide_s, "video" ) == 0 )
260 mlt_properties_set_int( properties, "hide", 1 );
261 else if ( strcmp( hide_s, "audio" ) == 0 )
262 mlt_properties_set_int( properties, "hide", 2 );
263 else if ( strcmp( hide_s, "both" ) == 0 )
264 mlt_properties_set_int( properties, "hide", 3 );
268 // Put the parent producer back
269 if ( container != NULL )
270 context_push_service( context, container, type );
275 /** Attach filters defined on that to this.
278 static void attach_filters( mlt_service this, mlt_service that )
283 mlt_filter filter = NULL;
284 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
286 mlt_service_attach( this, filter );
287 attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
292 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
294 mlt_profile p = context->profile;
295 for ( ; atts != NULL && *atts != NULL; atts += 2 )
297 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
299 mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
302 p->description = strdup( my_profile->description );
303 p->display_aspect_den = my_profile->display_aspect_den;
304 p->display_aspect_num = my_profile->display_aspect_num;
305 p->frame_rate_den = my_profile->frame_rate_den;
306 p->frame_rate_num = my_profile->frame_rate_num;
307 p->width = my_profile->width;
308 p->height = my_profile->height;
309 p->progressive = my_profile->progressive;
310 p->sample_aspect_den = my_profile->sample_aspect_den;
311 p->sample_aspect_num = my_profile->sample_aspect_num;
312 p->colorspace = my_profile->colorspace;
314 mlt_profile_close( my_profile );
317 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
319 if ( p->description )
320 free( p->description );
321 p->description = strdup( _s(atts[ 1 ]) );
324 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
325 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
326 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
327 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
328 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
329 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
330 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
331 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
332 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
333 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
334 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
335 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
336 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
337 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
338 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
339 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
340 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
341 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
342 else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
343 p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
347 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
349 mlt_tractor tractor = mlt_tractor_new( );
350 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
351 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
353 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
355 for ( ; atts != NULL && *atts != NULL; atts += 2 )
356 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
358 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
360 if ( mlt_properties_get( properties, "id" ) != NULL )
361 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
363 context_push_service( context, service, mlt_tractor_type );
366 static void on_end_tractor( deserialise_context context, const xmlChar *name )
369 enum service_type type;
370 mlt_service tractor = context_pop_service( context, &type );
372 if ( tractor != NULL && type == mlt_tractor_type )
374 // See if the tractor should be added to a playlist or multitrack
375 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
376 context_push_service( context, tractor, type );
380 fprintf( stderr, "Invalid state for tractor\n" );
384 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
386 enum service_type type;
387 mlt_service parent = context_pop_service( context, &type );
389 // If we don't have a parent, then create one now, providing we're in a state where we can
390 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
392 mlt_tractor tractor = NULL;
393 // Push the parent back
394 if ( parent != NULL )
395 context_push_service( context, parent, type );
397 // Create a tractor to contain the multitrack
398 tractor = mlt_tractor_new( );
399 parent = MLT_TRACTOR_SERVICE( tractor );
400 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
401 type = mlt_tractor_type;
403 // Flag it as a synthesised tractor for clean up later
404 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
407 if ( type == mlt_tractor_type )
409 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
410 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
411 for ( ; atts != NULL && *atts != NULL; atts += 2 )
412 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
414 if ( mlt_properties_get( properties, "id" ) != NULL )
415 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
417 context_push_service( context, parent, type );
418 context_push_service( context, service, mlt_multitrack_type );
422 fprintf( stderr, "Invalid multitrack position\n" );
426 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
428 // Get the multitrack from the stack
429 enum service_type type;
430 mlt_service service = context_pop_service( context, &type );
432 if ( service == NULL || type != mlt_multitrack_type )
433 fprintf( stderr, "End multitrack in the wrong state...\n" );
436 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
438 mlt_playlist playlist = mlt_playlist_init( );
439 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
440 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
442 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
444 for ( ; atts != NULL && *atts != NULL; atts += 2 )
446 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
448 // Out will be overwritten later as we append, so we need to save it
449 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
450 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
453 if ( mlt_properties_get( properties, "id" ) != NULL )
454 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
456 context_push_service( context, service, mlt_playlist_type );
459 static void on_end_playlist( deserialise_context context, const xmlChar *name )
461 // Get the playlist from the stack
462 enum service_type type;
463 mlt_service service = context_pop_service( context, &type );
465 if ( service != NULL && type == mlt_playlist_type )
467 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
468 mlt_position in = -1;
469 mlt_position out = -1;
471 if ( mlt_properties_get( properties, "in" ) )
472 in = mlt_properties_get_position( properties, "in" );
473 if ( mlt_properties_get( properties, "out" ) )
474 out = mlt_properties_get_position( properties, "out" );
476 // See if the playlist should be added to a playlist or multitrack
477 if ( add_producer( context, service, in, out ) == 0 )
478 context_push_service( context, service, type );
482 fprintf( stderr, "Invalid state of playlist end\n" );
486 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
488 // use a dummy service to hold properties to allow arbitrary nesting
489 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
490 mlt_service_init( service, NULL );
492 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
494 context_push_service( context, service, mlt_dummy_producer_type );
496 for ( ; atts != NULL && *atts != NULL; atts += 2 )
497 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
500 // Parse a SMIL clock value (as produced by Kino 0.9.1) and return position in frames
501 static mlt_position parse_clock_value( char *value, double fps )
503 // This implementation expects a fully specified clock value - no optional
505 char *pos, *copy = strdup( value );
507 mlt_position result = -1;
510 pos = strchr( value, ':' );
517 pos = strchr( value, ':' );
524 pos = strchr( value, '.' );
533 result = ( fps * ( ( (hh * 3600) + (mm * 60) + ss ) * 1000 + ms ) / 1000 + 0.5 );
538 static void on_end_producer( deserialise_context context, const xmlChar *name )
540 enum service_type type;
541 mlt_service service = context_pop_service( context, &type );
542 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
544 if ( service != NULL && type == mlt_dummy_producer_type )
546 mlt_service producer = NULL;
548 qualify_property( context, properties, "resource" );
549 char *resource = mlt_properties_get( properties, "resource" );
551 // Let Kino-SMIL src be a synonym for resource
552 if ( resource == NULL )
554 qualify_property( context, properties, "src" );
555 resource = mlt_properties_get( properties, "src" );
558 // Instantiate the producer
559 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
562 strncpy( temp, mlt_properties_get( properties, "mlt_service" ), 1024 );
563 if ( resource != NULL )
566 strncat( temp, resource, 1023 - strlen( temp ) );
568 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
571 // Just in case the plugin requested doesn't exist...
572 if ( producer == NULL && resource != NULL )
573 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
575 if ( producer == NULL )
576 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
578 if ( producer == NULL )
579 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
581 // Track this producer
582 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
584 // Propogate the properties
585 qualify_property( context, properties, "resource" );
586 qualify_property( context, properties, "luma" );
587 qualify_property( context, properties, "luma.resource" );
588 qualify_property( context, properties, "composite.luma" );
589 qualify_property( context, properties, "producer.resource" );
591 // Handle in/out properties separately
592 mlt_position in = -1;
593 mlt_position out = -1;
596 if ( mlt_properties_get( properties, "in" ) != NULL )
597 in = mlt_properties_get_position( properties, "in" );
598 // Let Kino-SMIL clipBegin be a synonym for in
599 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
601 if ( strchr( mlt_properties_get( properties, "clipBegin" ), ':' ) )
603 in = parse_clock_value( mlt_properties_get( properties, "clipBegin" ),
604 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
606 // Parse frames value
607 in = mlt_properties_get_position( properties, "clipBegin" );
610 if ( mlt_properties_get( properties, "out" ) != NULL )
611 out = mlt_properties_get_position( properties, "out" );
612 // Let Kino-SMIL clipEnd be a synonym for out
613 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
615 if ( strchr( mlt_properties_get( properties, "clipEnd" ), ':' ) )
617 out = parse_clock_value( mlt_properties_get( properties, "clipEnd" ),
618 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
620 // Parse frames value
621 out = mlt_properties_get_position( properties, "clipEnd" );
624 mlt_properties_set( properties, "in", NULL );
625 mlt_properties_set( properties, "out", NULL );
627 // Inherit the properties
628 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
630 // Attach all filters from service onto producer
631 attach_filters( producer, service );
633 // Add the producer to the producer map
634 if ( mlt_properties_get( properties, "id" ) != NULL )
635 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
637 // See if the producer should be added to a playlist or multitrack
638 if ( add_producer( context, producer, in, out ) == 0 )
640 // Otherwise, set in and out on...
641 if ( in != -1 || out != -1 )
643 // Get the parent service
644 enum service_type type;
645 mlt_service parent = context_pop_service( context, &type );
646 if ( parent != NULL )
648 // Get the parent properties
649 properties = MLT_SERVICE_PROPERTIES( parent );
651 char *resource = mlt_properties_get( properties, "resource" );
653 // Put the parent producer back
654 context_push_service( context, parent, type );
656 // If the parent is a track or entry
657 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
659 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
660 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
664 // Otherwise, set in and out on producer directly
665 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
670 // Otherwise, set in and out on producer directly
671 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
675 // Push the producer onto the stack
676 context_push_service( context, producer, mlt_producer_type );
679 mlt_service_close( service );
683 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
685 // Get the playlist from the stack
686 enum service_type type;
687 mlt_service service = context_pop_service( context, &type );
688 mlt_position length = 0;
690 if ( type == mlt_playlist_type && service != NULL )
692 // Look for the length attribute
693 for ( ; atts != NULL && *atts != NULL; atts += 2 )
695 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
697 length = atoll( _s(atts[1]) );
702 // Append a blank to the playlist
703 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
705 // Push the playlist back onto the stack
706 context_push_service( context, service, type );
710 fprintf( stderr, "blank without a playlist - a definite no no\n" );
714 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
716 mlt_producer entry = NULL;
717 mlt_properties temp = mlt_properties_new( );
719 for ( ; atts != NULL && *atts != NULL; atts += 2 )
721 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
723 // Look for the producer attribute
724 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
726 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
727 if ( producer != NULL )
728 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
732 // If we have a valid entry
733 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
735 mlt_playlist_clip_info info;
736 enum service_type parent_type = invalid_type;
737 mlt_service parent = context_pop_service( context, &parent_type );
738 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
740 if ( parent_type == mlt_playlist_type )
742 // Append the producer to the playlist
743 mlt_position in = -1;
744 mlt_position out = -1;
745 if ( mlt_properties_get( temp, "in" ) )
746 in = mlt_properties_get_position( temp, "in" );
747 if ( mlt_properties_get( temp, "out" ) )
748 out = mlt_properties_get_position( temp, "out" );
749 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
751 // Handle the repeat property
752 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
754 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
755 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
756 mlt_properties_get_int( temp, "repeat" ) );
759 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
764 fprintf( stderr, "Entry not part of a playlist...\n" );
767 context_push_service( context, parent, parent_type );
770 // Push the cut onto the stack
771 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
773 mlt_properties_close( temp );
776 static void on_end_entry( deserialise_context context, const xmlChar *name )
778 // Get the entry from the stack
779 enum service_type entry_type = invalid_type;
780 mlt_service entry = context_pop_service( context, &entry_type );
782 if ( entry == NULL && entry_type != mlt_entry_type )
784 fprintf( stderr, "Invalid state at end of entry\n" );
788 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
790 // use a dummy service to hold properties to allow arbitrary nesting
791 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
792 mlt_service_init( service, NULL );
794 // Push the dummy service onto the stack
795 context_push_service( context, service, mlt_entry_type );
797 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
799 for ( ; atts != NULL && *atts != NULL; atts += 2 )
801 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
803 // Look for the producer attribute
804 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
806 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
807 if ( producer != NULL )
808 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
813 static void on_end_track( deserialise_context context, const xmlChar *name )
815 // Get the track from the stack
816 enum service_type track_type;
817 mlt_service track = context_pop_service( context, &track_type );
819 if ( track != NULL && track_type == mlt_entry_type )
821 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
822 enum service_type parent_type = invalid_type;
823 mlt_service parent = context_pop_service( context, &parent_type );
824 mlt_multitrack multitrack = NULL;
826 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
827 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
829 if ( parent_type == mlt_tractor_type )
830 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
831 else if ( parent_type == mlt_multitrack_type )
832 multitrack = MLT_MULTITRACK( parent );
834 fprintf( stderr, "track contained in an invalid container\n" );
836 if ( multitrack != NULL )
838 // Set producer i/o if specified
839 if ( mlt_properties_get( track_props, "in" ) != NULL ||
840 mlt_properties_get( track_props, "out" ) != NULL )
842 mlt_position in = -1;
843 mlt_position out = -1;
844 if ( mlt_properties_get( track_props, "in" ) )
845 in = mlt_properties_get_position( track_props, "in" );
846 if ( mlt_properties_get( track_props, "out" ) )
847 out = mlt_properties_get_position( track_props, "out" );
848 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
849 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
850 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
851 track_props = MLT_PRODUCER_PROPERTIES( cut );
852 mlt_producer_close( cut );
856 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
859 // Set the hide state of the track producer
860 char *hide_s = mlt_properties_get( track_props, "hide" );
861 if ( hide_s != NULL )
863 if ( strcmp( hide_s, "video" ) == 0 )
864 mlt_properties_set_int( producer_props, "hide", 1 );
865 else if ( strcmp( hide_s, "audio" ) == 0 )
866 mlt_properties_set_int( producer_props, "hide", 2 );
867 else if ( strcmp( hide_s, "both" ) == 0 )
868 mlt_properties_set_int( producer_props, "hide", 3 );
872 if ( parent != NULL )
873 context_push_service( context, parent, parent_type );
875 mlt_service_close( track );
879 fprintf( stderr, "Invalid state at end of track\n" );
883 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
885 // use a dummy service to hold properties to allow arbitrary nesting
886 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
887 mlt_service_init( service, NULL );
889 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
891 context_push_service( context, service, mlt_dummy_filter_type );
893 // Set the properties
894 for ( ; atts != NULL && *atts != NULL; atts += 2 )
895 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
898 static void on_end_filter( deserialise_context context, const xmlChar *name )
900 enum service_type type;
901 mlt_service service = context_pop_service( context, &type );
902 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
904 enum service_type parent_type = invalid_type;
905 mlt_service parent = context_pop_service( context, &parent_type );
907 if ( service != NULL && type == mlt_dummy_filter_type )
909 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, mlt_properties_get( properties, "mlt_service" ), NULL ) );
910 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
912 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
914 // Propogate the properties
915 qualify_property( context, properties, "resource" );
916 qualify_property( context, properties, "luma" );
917 qualify_property( context, properties, "luma.resource" );
918 qualify_property( context, properties, "composite.luma" );
919 qualify_property( context, properties, "producer.resource" );
920 mlt_properties_inherit( filter_props, properties );
922 // Attach all filters from service onto filter
923 attach_filters( filter, service );
925 // Associate the filter with the parent
926 if ( parent != NULL )
928 if ( parent_type == mlt_tractor_type )
930 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
931 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
932 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
933 mlt_properties_get_int( properties, "in" ),
934 mlt_properties_get_int( properties, "out" ) );
938 mlt_service_attach( parent, MLT_FILTER( filter ) );
941 // Put the parent back on the stack
942 context_push_service( context, parent, parent_type );
946 fprintf( stderr, "filter closed with invalid parent...\n" );
949 // Close the dummy filter service
950 mlt_service_close( service );
954 fprintf( stderr, "Invalid top of stack on filter close\n" );
958 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
960 // use a dummy service to hold properties to allow arbitrary nesting
961 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
962 mlt_service_init( service, NULL );
964 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
966 context_push_service( context, service, mlt_dummy_transition_type );
968 // Set the properties
969 for ( ; atts != NULL && *atts != NULL; atts += 2 )
970 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
973 static void on_end_transition( deserialise_context context, const xmlChar *name )
975 enum service_type type;
976 mlt_service service = context_pop_service( context, &type );
977 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
979 enum service_type parent_type = invalid_type;
980 mlt_service parent = context_pop_service( context, &parent_type );
982 if ( service != NULL && type == mlt_dummy_transition_type )
984 char *id = mlt_properties_get( properties, "mlt_service" );
985 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
986 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
988 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
990 // Propogate the properties
991 qualify_property( context, properties, "resource" );
992 qualify_property( context, properties, "luma" );
993 qualify_property( context, properties, "luma.resource" );
994 qualify_property( context, properties, "composite.luma" );
995 qualify_property( context, properties, "producer.resource" );
996 mlt_properties_inherit( effect_props, properties );
998 // Attach all filters from service onto effect
999 attach_filters( effect, service );
1001 // Associate the filter with the parent
1002 if ( parent != NULL )
1004 if ( parent_type == mlt_tractor_type )
1006 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1007 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1008 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1009 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1010 mlt_properties_get_int( properties, "a_track" ),
1011 mlt_properties_get_int( properties, "b_track" ) );
1012 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1013 mlt_properties_get_int( properties, "in" ),
1014 mlt_properties_get_int( properties, "out" ) );
1018 fprintf( stderr, "Misplaced transition - ignoring\n" );
1021 // Put the parent back on the stack
1022 context_push_service( context, parent, parent_type );
1026 fprintf( stderr, "transition closed with invalid parent...\n" );
1029 // Close the dummy filter service
1030 mlt_service_close( service );
1034 fprintf( stderr, "Invalid top of stack on transition close\n" );
1038 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1040 enum service_type type;
1041 mlt_service service = context_pop_service( context, &type );
1042 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1043 const char *value = NULL;
1045 if ( service != NULL )
1047 // Set the properties
1048 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1050 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1051 context->property = strdup( _s(atts[ 1 ]) );
1052 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1053 value = _s(atts[ 1 ]);
1056 if ( context->property != NULL )
1057 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1059 // Tell parser to collect any further nodes for serialisation
1060 context->is_value = 1;
1062 context_push_service( context, service, type );
1066 fprintf( stderr, "Property without a service '%s'?\n", ( const char * )name );
1070 static void on_end_property( deserialise_context context, const xmlChar *name )
1072 enum service_type type;
1073 mlt_service service = context_pop_service( context, &type );
1074 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1076 if ( service != NULL )
1078 // Tell parser to stop building a tree
1079 context->is_value = 0;
1081 // See if there is a xml tree for the value
1082 if ( context->property != NULL && context->value_doc != NULL )
1087 // Serialise the tree to get value
1088 xmlDocDumpMemory( context->value_doc, &value, &size );
1089 mlt_properties_set( properties, context->property, _s(value) );
1091 xmlFreeDoc( context->value_doc );
1092 context->value_doc = NULL;
1095 // Close this property handling
1096 free( context->property );
1097 context->property = NULL;
1099 context_push_service( context, service, type );
1103 fprintf( stderr, "Property without a service '%s'??\n", (const char *)name );
1107 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1109 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1110 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1112 //printf("on_start_element: %s\n", name );
1113 if ( context->pass == 0 )
1115 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1116 xmlStrcmp( name, _x("profile") ) == 0 ||
1117 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1118 on_start_profile( context, name, atts );
1121 context->branch[ context->depth ] ++;
1124 // Build a tree from nodes within a property value
1125 if ( context->is_value == 1 )
1127 xmlNodePtr node = xmlNewNode( NULL, name );
1129 if ( context->value_doc == NULL )
1132 context->value_doc = xmlNewDoc( _x("1.0") );
1133 xmlDocSetRootElement( context->value_doc, node );
1137 // Append child to tree
1138 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1140 context_push_node( context, node );
1142 // Set the attributes
1143 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1144 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1146 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1147 on_start_tractor( context, name, atts );
1148 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1149 on_start_multitrack( context, name, atts );
1150 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1151 on_start_playlist( context, name, atts );
1152 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1153 on_start_producer( context, name, atts );
1154 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1155 on_start_blank( context, name, atts );
1156 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1157 on_start_entry( context, name, atts );
1158 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1159 on_start_track( context, name, atts );
1160 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1161 on_start_filter( context, name, atts );
1162 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1163 on_start_transition( context, name, atts );
1164 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1165 on_start_property( context, name, atts );
1166 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1167 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1168 mlt_properties_set( context->producer_map, ( const char * )atts[ 0 ], ( const char * )atts[ 1 ] );
1171 static void on_end_element( void *ctx, const xmlChar *name )
1173 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1174 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1176 //printf("on_end_element: %s\n", name );
1177 if ( context->is_value == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1178 context_pop_node( context );
1179 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1180 on_end_multitrack( context, name );
1181 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1182 on_end_playlist( context, name );
1183 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1184 on_end_track( context, name );
1185 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1186 on_end_entry( context, name );
1187 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1188 on_end_tractor( context, name );
1189 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1190 on_end_property( context, name );
1191 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1192 on_end_producer( context, name );
1193 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1194 on_end_filter( context, name );
1195 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1196 on_end_transition( context, name );
1198 context->branch[ context->depth ] = 0;
1202 static void on_characters( void *ctx, const xmlChar *ch, int len )
1204 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1205 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1206 char *value = calloc( len + 1, 1 );
1207 enum service_type type;
1208 mlt_service service = context_pop_service( context, &type );
1209 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1211 if ( service != NULL )
1212 context_push_service( context, service, type );
1215 strncpy( value, (const char*) ch, len );
1217 if ( context->stack_node_size > 0 )
1218 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1220 // libxml2 generates an on_characters immediately after a get_entity within
1221 // an element value, and we ignore it because it is called again during
1222 // actual substitution.
1223 else if ( context->property != NULL && context->entity_is_replace == 0 )
1225 char *s = mlt_properties_get( properties, context->property );
1228 // Append new text to existing content
1229 char *new = calloc( strlen( s ) + len + 1, 1 );
1231 strcat( new, value );
1232 mlt_properties_set( properties, context->property, new );
1236 mlt_properties_set( properties, context->property, value );
1238 context->entity_is_replace = 0;
1243 /** Convert parameters parsed from resource into entity declarations.
1245 static void params_to_entities( deserialise_context context )
1247 if ( context->params != NULL )
1251 // Add our params as entitiy declarations
1252 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1254 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1255 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1256 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1260 mlt_properties_close( context->params );
1261 context->params = NULL;
1265 // The following 3 facilitate entity substitution in the SAX parser
1266 static void on_internal_subset( void *ctx, const xmlChar* name,
1267 const xmlChar* publicId, const xmlChar* systemId )
1269 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1270 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1272 context->publicId = publicId;
1273 context->systemId = systemId;
1274 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1276 // Override default entities with our parameters
1277 params_to_entities( context );
1280 // TODO: Check this with Dan... I think this is for parameterisation
1281 // but it's breaking standard escaped entities (like < etc).
1282 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1283 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1285 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1286 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1288 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1291 // TODO: Check this functionality (see on_entity_declaration)
1292 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1294 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1295 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1296 xmlEntityPtr e = NULL;
1298 // Setup for entity declarations if not ready
1299 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1301 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1302 context->publicId = _x("");
1303 context->systemId = _x("");
1306 // Add our parameters if not already
1307 params_to_entities( context );
1309 e = xmlGetPredefinedEntity( name );
1311 // Send signal to on_characters that an entity substitutin is pending
1314 e = xmlGetDocEntity( context->entity_doc, name );
1316 context->entity_is_replace = 1;
1322 /** Convert a hexadecimal character to its value.
1324 static int tohex( char p )
1326 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1329 /** Decode a url-encoded string containing hexadecimal character sequences.
1331 static char *url_decode( char *dest, char *src )
1339 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1352 /** Extract the filename from a URL attaching parameters to a properties list.
1354 static void parse_url( mlt_properties properties, char *url )
1357 int n = strlen( url );
1361 for ( i = 0; i < n; i++ )
1378 if ( name != NULL && value != NULL )
1379 mlt_properties_set( properties, name, value );
1385 if ( name != NULL && value != NULL )
1386 mlt_properties_set( properties, name, value );
1389 // Quick workaround to avoid unecessary libxml2 warnings
1390 static int file_exists( char *file )
1392 char *name = strdup( file );
1394 if ( name != NULL && strchr( name, '?' ) )
1395 *( strchr( name, '?' ) ) = '\0';
1398 FILE *f = fopen( name, "r" );
1400 if ( exists ) fclose( f );
1406 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1408 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1409 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1410 mlt_properties properties = NULL;
1412 struct _xmlParserCtxt *xmlcontext;
1413 int well_formed = 0;
1414 char *filename = NULL;
1415 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1417 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1420 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1421 if ( context == NULL )
1424 context->producer_map = mlt_properties_new();
1425 context->destructors = mlt_properties_new();
1426 context->params = mlt_properties_new();
1427 context->profile = profile;
1429 // Decode URL and parse parameters
1430 mlt_properties_set( context->producer_map, "root", "" );
1433 filename = strdup( data );
1434 parse_url( context->params, url_decode( filename, data ) );
1436 // We need the directory prefix which was used for the xml
1437 if ( strchr( filename, '/' ) )
1440 mlt_properties_set( context->producer_map, "root", filename );
1441 root = mlt_properties_get( context->producer_map, "root" );
1442 *( strrchr( root, '/' ) ) = '\0';
1444 // If we don't have an absolute path here, we're heading for disaster...
1445 if ( root[ 0 ] != '/' )
1447 char *cwd = getcwd( NULL, 0 );
1448 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1449 sprintf( real, "%s/%s", cwd, root );
1450 mlt_properties_set( context->producer_map, "root", real );
1457 // We need to track the number of registered filters
1458 mlt_properties_set_int( context->destructors, "registered", 0 );
1460 // Setup SAX callbacks
1461 sax->startElement = on_start_element;
1463 // Setup libxml2 SAX parsing
1465 xmlSubstituteEntitiesDefault( 1 );
1466 // This is used to facilitate entity substitution in the SAX parser
1467 context->entity_doc = xmlNewDoc( _x("1.0") );
1469 xmlcontext = xmlCreateFileParserCtxt( filename );
1471 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1473 // Invalid context - clean up and return NULL
1474 if ( xmlcontext == NULL )
1476 mlt_properties_close( context->producer_map );
1477 mlt_properties_close( context->destructors );
1478 mlt_properties_close( context->params );
1486 xmlcontext->sax = sax;
1487 xmlcontext->_private = ( void* )context;
1488 xmlParseDocument( xmlcontext );
1490 // Cleanup after parsing
1491 xmlcontext->sax = NULL;
1492 xmlcontext->_private = NULL;
1493 xmlFreeParserCtxt( xmlcontext );
1495 // Setup the second pass
1498 xmlcontext = xmlCreateFileParserCtxt( filename );
1500 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1502 // Invalid context - clean up and return NULL
1503 if ( xmlcontext == NULL )
1505 mlt_properties_close( context->producer_map );
1506 mlt_properties_close( context->destructors );
1507 mlt_properties_close( context->params );
1508 xmlFreeDoc( context->entity_doc );
1515 // Setup SAX callbacks
1516 sax->endElement = on_end_element;
1517 sax->characters = on_characters;
1518 sax->cdataBlock = on_characters;
1519 sax->internalSubset = on_internal_subset;
1520 sax->entityDecl = on_entity_declaration;
1521 sax->getEntity = on_get_entity;
1524 xmlcontext->sax = sax;
1525 xmlcontext->_private = ( void* )context;
1526 xmlParseDocument( xmlcontext );
1527 well_formed = xmlcontext->wellFormed;
1529 // Cleanup after parsing
1530 xmlFreeDoc( context->entity_doc );
1532 xmlMemoryDump( ); // for debugging
1534 // Get the last producer on the stack
1535 enum service_type type;
1536 mlt_service service = context_pop_service( context, &type );
1537 if ( well_formed && service != NULL )
1539 // Verify it is a producer service (mlt_type="mlt_producer")
1540 // (producer, playlist, multitrack)
1541 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1542 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1547 xmlDocPtr doc = xml_make_doc( service );
1548 xmlDocFormatDump( stdout, doc, 1 );
1553 if ( well_formed && service != NULL )
1555 char *title = mlt_properties_get( context->producer_map, "title" );
1557 // Need the complete producer list for various reasons
1558 properties = context->destructors;
1560 // Now make sure we don't have a reference to the service in the properties
1561 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1563 char *name = mlt_properties_get_name( properties, i );
1564 if ( mlt_properties_get_data( properties, name, NULL ) == service )
1566 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1571 // We are done referencing destructor property list
1572 // Set this var to service properties for convenience
1573 properties = MLT_SERVICE_PROPERTIES( service );
1576 mlt_properties_set( properties, "title", title );
1578 // Optimise for overlapping producers
1579 mlt_producer_optimise( MLT_PRODUCER( service ) );
1581 // Handle deep copies
1582 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1584 // Now assign additional properties
1586 mlt_properties_set( properties, "resource", data );
1588 // This tells consumer_xml not to deep copy
1589 mlt_properties_set( properties, "xml", "was here" );
1593 // Allow the project to be edited
1594 mlt_properties_set( properties, "_xml", "was here" );
1595 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1600 // Return null if not well formed
1605 mlt_properties_close( context->producer_map );
1606 if ( context->params != NULL )
1607 mlt_properties_close( context->params );
1608 mlt_properties_close( context->destructors );
1612 return MLT_PRODUCER( service );