]> git.sesse.net Git - mlt/blob - src/modules/westley/producer_westley.c
add luma to composite.
[mlt] / src / modules / westley / producer_westley.c
1 /*
2  * producer_westley.c -- a libxml2 parser of mlt service networks
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * 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 // TODO: try using XmlReader interface to avoid global context issues in sax.
24  
25 #include "producer_westley.h"
26 #include <framework/mlt.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <libxml/parser.h>
32
33 #define STACK_SIZE 1000
34
35 struct deserialise_context_s
36 {
37         mlt_service stack_service[ STACK_SIZE ];
38         int stack_service_size;
39         mlt_properties producer_map;
40         mlt_properties destructors;
41         char *property;
42         mlt_properties producer_properties;
43 };
44 typedef struct deserialise_context_s *deserialise_context;
45
46
47 /** Push a service.
48 */
49
50 static int context_push_service( deserialise_context this, mlt_service that )
51 {
52         int ret = this->stack_service_size >= STACK_SIZE;
53         if ( ret == 0 )
54                 this->stack_service[ this->stack_service_size ++ ] = that;
55         return ret;
56 }
57
58 /** Pop a service.
59 */
60
61 static mlt_service context_pop_service( deserialise_context this )
62 {
63         mlt_service result = NULL;
64         if ( this->stack_service_size > 0 )
65                 result = this->stack_service[ -- this->stack_service_size ];
66         return result;
67 }
68
69 // Set the destructor on a new service
70 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
71 {
72         int registered = mlt_properties_get_int( properties, "registered" );
73         char *key = mlt_properties_get( properties, "registered" );
74         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
75         mlt_properties_set_int( properties, "registered", ++ registered );
76 }
77
78 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
79 {
80         mlt_service service = mlt_tractor_service( mlt_tractor_init() );
81         mlt_properties properties = mlt_service_properties( service );
82
83         track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
84
85         mlt_properties_set_position( properties, "length", 0 );
86
87         for ( ; atts != NULL && *atts != NULL; atts += 2 )
88                 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
89
90         if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
91         {
92                 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
93                 mlt_properties_set_position( properties, "length", length );
94         }
95
96         context_push_service( context, service );
97 }
98
99 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
100 {
101         mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
102         mlt_properties properties = mlt_service_properties( service );
103
104         track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
105
106         mlt_properties_set_position( properties, "length", 0 );
107
108         for ( ; atts != NULL && *atts != NULL; atts += 2 )
109                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
110
111         context_push_service( context, service );
112 }
113
114 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
115 {
116         mlt_service service = mlt_playlist_service( mlt_playlist_init() );
117         mlt_properties properties = mlt_service_properties( service );
118
119         track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
120
121         mlt_properties_set_position( properties, "length", 0 );
122
123         for ( ; atts != NULL && *atts != NULL; atts += 2 )
124         {
125                 mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
126
127                 // Out will be overwritten later as we append, so we need to save it
128                 if ( strcmp( atts[ 0 ], "out" ) == 0 )
129                         mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
130         }
131
132         if ( mlt_properties_get( properties, "id" ) != NULL )
133                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
134
135         context_push_service( context, service );
136 }
137
138 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
139 {
140         mlt_properties properties = context->producer_properties = mlt_properties_new();
141
142         for ( ; atts != NULL && *atts != NULL; atts += 2 )
143         {
144                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
145         }
146 }
147
148 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
149 {
150         // Get the playlist from the stack
151         mlt_service service = context_pop_service( context );
152         mlt_position length = 0;
153
154         // Look for the length attribute
155         for ( ; atts != NULL && *atts != NULL; atts += 2 )
156         {
157                 if ( strcmp( atts[0], "length" ) == 0 )
158                 {
159                         length = atoll( atts[1] );
160                         break;
161                 }
162         }
163
164         // Append a blank to the playlist
165         mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
166
167         // Push the playlist back onto the stack
168         context_push_service( context, service );
169 }
170
171 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
172 {
173         // Use a dummy service to hold properties to allow arbitratry nesting
174         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
175         mlt_service_init( service, NULL );
176
177         // Push the dummy service onto the stack
178         context_push_service( context, service );
179         
180         for ( ; atts != NULL && *atts != NULL; atts += 2 )
181         {
182                 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
183                 
184                 // Look for the producer attribute
185                 if ( strcmp( atts[ 0 ], "producer" ) == 0 )
186                 {
187                         if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) !=  NULL )
188                                 // Push the referenced producer onto the stack
189                                 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
190                 }
191         }
192 }
193
194 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
195 {
196         mlt_properties properties = context->producer_properties = mlt_properties_new();
197
198         // Set the properties
199         for ( ; atts != NULL && *atts != NULL; atts += 2 )
200                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
201 }
202
203 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
204 {
205         mlt_properties properties = context->producer_properties = mlt_properties_new();
206
207         // Set the properties
208         for ( ; atts != NULL && *atts != NULL; atts += 2 )
209                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
210 }
211
212 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
213 {
214         mlt_properties properties = context->producer_properties;
215         char *value = NULL;
216
217         if ( properties == NULL )
218                 return;
219         
220         // Set the properties
221         for ( ; atts != NULL && *atts != NULL; atts += 2 )
222         {
223                 if ( strcmp( atts[ 0 ], "name" ) == 0 )
224                 {
225                         context->property = strdup( atts[ 1 ] );
226                 }
227                 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
228                 {
229                         value = (char*) atts[ 1 ];
230                 }
231         }
232
233         if ( context->property != NULL && value != NULL )
234                 mlt_properties_set( properties, context->property, value );
235 }
236
237 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
238 {
239         // Get the producer (multitrack) from the stack
240         mlt_service producer = context_pop_service( context );
241
242         // Get the tractor from the stack
243         mlt_service service = context_pop_service( context );
244
245         // Connect the tractor to the producer
246         mlt_tractor_connect( MLT_TRACTOR( service ), producer );
247         mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
248                 MLT_MULTITRACK( producer ), 0, NULL, NULL );
249
250         // Push the tractor back onto the stack
251         context_push_service( context, service );
252
253         // Push the producer back onto the stack
254         context_push_service( context, producer );
255 }
256
257 static void on_end_playlist( deserialise_context context, const xmlChar *name )
258 {
259         // Get the playlist from the stack
260         mlt_service service = context_pop_service( context );
261         mlt_properties properties = mlt_service_properties( service );
262
263         mlt_position in = mlt_properties_get_position( properties, "in" );
264         mlt_position out;
265
266         if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
267                 out = mlt_properties_get_position( properties, "_westley.out" );
268         else
269                 out = mlt_properties_get_position( properties, "length" ) - 1;
270
271         if ( mlt_properties_get_position( properties, "length" ) < out )
272                 mlt_properties_set_position( properties, "length", out  + 1 );
273
274         mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
275
276         // Push the playlist back onto the stack
277         context_push_service( context, service );
278 }
279
280 static void on_end_track( deserialise_context context, const xmlChar *name )
281 {
282         // Get the producer from the stack
283         mlt_service producer = context_pop_service( context );
284
285         // Get the dummy track service from the stack
286         mlt_service track = context_pop_service( context );
287
288         // Get the multitrack from the stack
289         mlt_service service = context_pop_service( context );
290
291         // Set the track on the multitrack
292         mlt_multitrack_connect( MLT_MULTITRACK( service ),
293                 MLT_PRODUCER( producer ),
294                 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
295
296         // Set producer i/o if specified
297         if ( mlt_properties_get( mlt_service_properties( track ), "in" ) != NULL ||
298                 mlt_properties_get( mlt_service_properties( track ), "out" ) != NULL )
299         {
300                 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
301                         mlt_properties_get_position( mlt_service_properties( track ), "in" ),
302                         mlt_properties_get_position( mlt_service_properties( track ), "out" ) );
303         }
304
305         // Push the multitrack back onto the stack
306         context_push_service( context, service );
307
308         mlt_service_close( track );
309 }
310
311 static void on_end_entry( deserialise_context context, const xmlChar *name )
312 {
313         // Get the producer from the stack
314         mlt_service producer = context_pop_service( context );
315
316         // Get the dummy entry service from the stack
317         mlt_service entry = context_pop_service( context );
318
319         // Get the playlist from the stack
320         mlt_service service = context_pop_service( context );
321
322         // Append the producer to the playlist
323         if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
324                 mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
325         {
326                 mlt_playlist_append_io( MLT_PLAYLIST( service ),
327                         MLT_PRODUCER( producer ),
328                         mlt_properties_get_position( mlt_service_properties( entry ), "in" ), 
329                         mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
330         }
331         else
332         {
333                 mlt_playlist_append( MLT_PLAYLIST( service ), MLT_PRODUCER( producer ) );
334         }
335
336         // Push the playlist back onto the stack
337         context_push_service( context, service );
338
339         mlt_service_close( entry );
340 }
341
342 static void on_end_tractor( deserialise_context context, const xmlChar *name )
343 {
344         // Get and discard the last producer
345         mlt_producer multitrack = MLT_PRODUCER( context_pop_service( context ) );
346
347         // Get the tractor
348         mlt_service tractor = context_pop_service( context );
349         multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
350
351         // Inherit the producer's properties
352         mlt_properties properties = mlt_producer_properties( multitrack );
353         mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
354         mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
355         mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
356
357         // Push the playlist back onto the stack
358         context_push_service( context, tractor );
359 }
360
361 static void on_end_property( deserialise_context context, const xmlChar *name )
362 {
363         // Close this property handling
364         free( context->property );
365         context->property = NULL;
366 }
367
368 static void on_end_producer( deserialise_context context, const xmlChar *name )
369 {
370         mlt_properties properties = context->producer_properties;
371         mlt_service service = NULL;
372         
373         if ( properties == NULL )
374                 return;
375                 
376         // Instantiate the producer
377         if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
378         {
379                 service = MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties, "mlt_service" ) ) );
380         }
381         if ( service == NULL && mlt_properties_get( properties, "resource" ) != NULL )
382         {
383                 char *root = mlt_properties_get( context->producer_map, "_root" );
384                 char *resource = mlt_properties_get( properties, "resource" );
385                 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
386                 if ( resource[ 0 ] != '/' )
387                 {
388                         strcpy( full_resource, root );
389                         strcat( full_resource, resource );
390                 }
391                 else
392                 {
393                         strcpy( full_resource, resource );
394                 }
395                 service = MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource ) );
396                 free( full_resource );
397         }
398
399         track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
400
401         // Add the producer to the producer map
402         if ( mlt_properties_get( properties, "id" ) != NULL )
403                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
404
405         mlt_properties_inherit( mlt_service_properties( service ), properties );
406         mlt_properties_close( properties );
407         context->producer_properties = NULL;
408         properties = mlt_service_properties( service );
409
410         // Set in and out
411         mlt_producer_set_in_and_out( MLT_PRODUCER( service ),
412                 mlt_properties_get_position( properties, "in" ),
413                 mlt_properties_get_position( properties, "out" ) );
414
415         // Push the new producer onto the stack
416         context_push_service( context, service );
417 }
418
419 static void on_end_filter( deserialise_context context, const xmlChar *name )
420 {
421         mlt_properties properties = context->producer_properties;
422         if ( properties == NULL )
423                 return;
424
425         char *id;
426         char key[11];
427         key[ 10 ] = '\0';
428
429         // Get the producer from the stack
430         mlt_service producer = context_pop_service( context );
431 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
432
433         // Create the filter
434         mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
435
436         track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
437
438         // Connect the filter to the producer
439         mlt_filter_connect( MLT_FILTER( service ), producer,
440                 mlt_properties_get_int( properties, "track" ) );
441
442         // Set in and out from producer if non existant
443         if ( mlt_properties_get( properties, "in" ) == NULL )
444                 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
445         if ( mlt_properties_get( properties, "out" ) == NULL )
446                 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
447
448         // Propogate the properties
449         mlt_properties_inherit( mlt_service_properties( service ), properties );
450         mlt_properties_close( properties );
451         context->producer_properties = NULL;
452         properties = mlt_service_properties( service );
453
454         // Set in and out
455 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
456         mlt_filter_set_in_and_out( MLT_FILTER( service ), 
457                 mlt_properties_get_position( properties, "in" ),
458                 mlt_properties_get_position( properties, "out" ) );
459
460         // Get the parent producer from the stack
461         mlt_service tractor = context_pop_service( context );
462
463         if ( tractor != NULL )
464         {
465 //fprintf( stderr, "connecting tractor %s to filter\n", mlt_properties_get( mlt_service_properties( tractor ), "resource" ) );
466                 // Connect the tractor to the filter
467                 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor ), "resource" ), "<tractor>" ) == 0 )
468                         mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
469
470                 // Push the parent producer back onto the stack
471                 context_push_service( context, tractor );
472         }
473
474 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
475         // If a producer alias is in the producer_map, get it
476         snprintf( key, 10, "%p", producer );
477         if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
478                 producer = mlt_properties_get_data( context->producer_map, key, NULL );
479
480         // Put the producer in the producer map
481         id = mlt_properties_get( mlt_service_properties( producer ), "id" );
482         if ( id != NULL )
483                 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
484
485         // For filter chain support, add an alias to the producer map
486         snprintf( key, 10, "%p", service );
487         mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
488
489         // Push the filter onto the stack
490         context_push_service( context, service );
491
492 }
493
494 static void on_end_transition( deserialise_context context, const xmlChar *name )
495 {
496         mlt_properties properties = context->producer_properties;
497         if ( properties == NULL )
498                 return;
499
500         // Get the producer from the stack
501         mlt_service producer = context_pop_service( context );
502
503         // Create the transition
504         mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
505
506         track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
507
508         // Propogate the properties
509         mlt_properties_inherit( mlt_service_properties( service ), properties );
510         mlt_properties_close( properties );
511         context->producer_properties = NULL;
512         properties = mlt_service_properties( service );
513
514         // Set in and out
515         mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
516                 mlt_properties_get_position( properties, "in" ),
517                 mlt_properties_get_position( properties, "out" ) );
518
519         // Connect the filter to the producer
520         mlt_transition_connect( MLT_TRANSITION( service ), producer,
521                 mlt_properties_get_int( properties, "a_track" ),
522                 mlt_properties_get_int( properties, "b_track" ) );
523
524         // Get the tractor from the stack
525         mlt_service tractor = context_pop_service( context );
526
527         // Connect the tractor to the transition
528         mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
529
530         // Push the tractor back onto the stack
531         context_push_service( context, tractor );
532
533         // Push the transition onto the stack
534         context_push_service( context, service );
535 }
536
537 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
538 {
539         deserialise_context context = ( deserialise_context ) ctx;
540         
541         if ( strcmp( name, "tractor" ) == 0 )
542                 on_start_tractor( context, name, atts );
543         else if ( strcmp( name, "multitrack" ) == 0 )
544                 on_start_multitrack( context, name, atts );
545         else if ( strcmp( name, "playlist" ) == 0 )
546                 on_start_playlist( context, name, atts );
547         else if ( strcmp( name, "producer" ) == 0 )
548                 on_start_producer( context, name, atts );
549         else if ( strcmp( name, "blank" ) == 0 )
550                 on_start_blank( context, name, atts );
551         else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
552                 on_start_entry_track( context, name, atts );
553         else if ( strcmp( name, "filter" ) == 0 )
554                 on_start_filter( context, name, atts );
555         else if ( strcmp( name, "transition" ) == 0 )
556                 on_start_transition( context, name, atts );
557         else if ( strcmp( name, "property" ) == 0 )
558                 on_start_property( context, name, atts );
559 }
560
561 static void on_end_element( void *ctx, const xmlChar *name )
562 {
563         deserialise_context context = ( deserialise_context ) ctx;
564         
565         if ( strcmp( name, "multitrack" ) == 0 )
566                 on_end_multitrack( context, name );
567         else if ( strcmp( name, "playlist" ) == 0 )
568                 on_end_playlist( context, name );
569         else if ( strcmp( name, "track" ) == 0 )
570                 on_end_track( context, name );
571         else if ( strcmp( name, "entry" ) == 0 )
572                 on_end_entry( context, name );
573         else if ( strcmp( name, "tractor" ) == 0 )
574                 on_end_tractor( context, name );
575         else if ( strcmp( name, "property" ) == 0 )
576                 on_end_property( context, name );
577         else if ( strcmp( name, "producer" ) == 0 )
578                 on_end_producer( context, name );
579         else if ( strcmp( name, "filter" ) == 0 )
580                 on_end_filter( context, name );
581         else if ( strcmp( name, "transition" ) == 0 )
582                 on_end_transition( context, name );
583 }
584
585 static void on_characters( void *ctx, const xmlChar *ch, int len )
586 {
587         deserialise_context context = ( deserialise_context ) ctx;
588         char *value = calloc( len + 1, 1 );
589
590         value[ len ] = 0;
591         strncpy( value, (const char*) ch, len );
592
593         if ( context->property != NULL && context->producer_properties != NULL )
594                 mlt_properties_set( context->producer_properties, context->property, value );
595                 
596         free( value);
597 }
598
599 mlt_producer producer_westley_init( char *filename )
600 {
601         xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
602         struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
603         mlt_properties properties = NULL;
604         int i = 0;
605
606         context->producer_map = mlt_properties_new();
607         context->destructors = mlt_properties_new();
608
609         // We need to track the number of registered filters
610         mlt_properties_set_int( context->destructors, "registered", 0 );
611
612         // We need the directory prefix which was used for the westley
613         mlt_properties_set( context->producer_map, "_root", "" );
614         if ( strchr( filename, '/' ) )
615         {
616                 char *root = NULL;
617                 mlt_properties_set( context->producer_map, "_root", filename );
618                 root = mlt_properties_get( context->producer_map, "_root" );
619                 *( strrchr( root, '/' ) + 1 ) = '\0';
620         }
621
622         sax->startElement = on_start_element;
623         sax->endElement = on_end_element;
624         sax->characters = on_characters;
625         sax->cdataBlock = on_characters;
626
627         // I REALLY DON'T GET THIS - HOW THE HELL CAN YOU REFERENCE A WESTLEY IN A WESTLEY???
628         xmlInitParser();
629
630         xmlSAXUserParseFile( sax, context, filename );
631
632         // Need the complete producer list for various reasons
633         properties = context->destructors;
634
635         // Get the last producer on the stack
636         mlt_service service = context_pop_service( context );
637
638         // Do we actually have a producer here?
639         if ( service != NULL )
640         {
641                 // Now make sure we don't have a reference to the service in the properties
642                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
643                 {
644                         char *name = mlt_properties_get_name( properties, i );
645                         if ( mlt_properties_get_data( properties, name, NULL ) == service )
646                         {
647                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
648                                 break;
649                         }
650                 }
651
652                 // We are done referencing destructor property list
653                 // Set this var to service properties for convenience
654                 properties = mlt_service_properties( service );
655         
656                 // make the returned service destroy the connected services
657                 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
658
659                 // Now assign additional properties
660                 mlt_properties_set( properties, "resource", filename );
661
662                 // This tells consumer_westley not to deep copy
663                 mlt_properties_set( properties, "westley", "was here" );
664         }
665         else
666         {
667                 // Clean up
668                 mlt_properties_close( properties );
669         }
670
671         free( context->stack_service );
672         mlt_properties_close( context->producer_map );
673         //free( context );
674         free( sax );
675         //xmlCleanupParser();
676         xmlMemoryDump( );
677
678
679         return MLT_PRODUCER( service );
680 }