]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
b1c426993e4ff132c5c2a4bdbac0fe4e026bd102
[mlt] / src / framework / mlt_producer.c
1 /*
2  * mlt_producer.c -- abstraction for all producer services
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
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 #include "config.h"
22 #include "mlt_producer.h"
23 #include "mlt_factory.h"
24 #include "mlt_frame.h"
25 #include "mlt_parser.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <math.h>
30
31 /** Forward references.
32 */
33
34 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
35 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
36 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
37
38 /** Constructor
39 */
40
41 int mlt_producer_init( mlt_producer this, void *child )
42 {
43         // Check that we haven't received NULL
44         int error = this == NULL;
45
46         // Continue if no error
47         if ( error == 0 )
48         {
49                 // Initialise the producer
50                 memset( this, 0, sizeof( struct mlt_producer_s ) );
51         
52                 // Associate with the child
53                 this->child = child;
54
55                 // Initialise the service
56                 if ( mlt_service_init( &this->parent, this ) == 0 )
57                 {
58                         // Get the normalisation preference
59                         char *normalisation = mlt_environment( "MLT_NORMALISATION" );
60
61                         // The parent is the service
62                         mlt_service parent = &this->parent;
63         
64                         // Define the parent close
65                         parent->close = ( mlt_destructor )mlt_producer_close;
66                         parent->close_object = this;
67
68                         // For convenience, we'll assume the close_object is this
69                         this->close_object = this;
70
71                         // Get the properties of the parent
72                         mlt_properties properties = mlt_service_properties( parent );
73         
74                         // Set the default properties
75                         mlt_properties_set( properties, "mlt_type", "mlt_producer" );
76                         mlt_properties_set_position( properties, "_position", 0.0 );
77                         mlt_properties_set_double( properties, "_frame", 0 );
78                         if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
79                         {
80                                 mlt_properties_set_double( properties, "fps", 25.0 );
81                                 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
82                         }
83                         else
84                         {
85                                 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
86                                 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
87                         }
88                         mlt_properties_set_double( properties, "_speed", 1.0 );
89                         mlt_properties_set_position( properties, "in", 0 );
90                         mlt_properties_set_position( properties, "out", 14999 );
91                         mlt_properties_set_position( properties, "length", 15000 );
92                         mlt_properties_set( properties, "eof", "pause" );
93                         mlt_properties_set( properties, "resource", "<producer>" );
94
95                         // Override service get_frame
96                         parent->get_frame = producer_get_frame;
97
98                         mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
99                         mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
100                         mlt_events_register( properties, "producer-changed", NULL );
101                 }
102         }
103
104         return error;
105 }
106
107 /** Listener for property changes.
108 */
109
110 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
111 {
112         if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
113                 mlt_events_fire( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
114 }
115
116 /** Listener for service changes.
117 */
118
119 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
120 {
121         mlt_events_fire( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
122 }
123
124 /** Special case destructor
125 */
126
127 static void mlt_cut_destroy( void *obj )
128 {
129         mlt_producer this = obj;
130         this->close = NULL;
131         this->parent.close = NULL;
132         mlt_service_close( &this->parent );
133         free( this );
134 }
135
136 /** Create a new producer.
137 */
138
139 mlt_producer mlt_producer_new( )
140 {
141         mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
142         mlt_producer_init( this, NULL );
143         this->close = mlt_cut_destroy;
144         return this;
145 }
146
147 /** Determine if producer is a cut.
148 */
149
150 int mlt_producer_is_cut( mlt_producer this )
151 {
152         return mlt_properties_get_int( mlt_producer_properties( this ), "_cut" );
153 }
154
155 /** Determine if producer is a mix.
156 */
157
158 int mlt_producer_is_mix( mlt_producer this )
159 {
160         mlt_properties properties = this != NULL ? mlt_producer_properties( this ) : NULL;
161         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
162         return tractor != NULL;
163 }
164
165 /** Obtain the parent producer.
166 */
167
168 mlt_producer mlt_producer_cut_parent( mlt_producer this )
169 {
170         mlt_properties properties = mlt_producer_properties( this );
171         if ( mlt_producer_is_cut( this ) )
172                 return mlt_properties_get_data( properties, "_cut_parent", NULL );
173         else
174                 return this;
175 }
176
177 /** Create a cut of this producer
178 */
179
180 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
181 {
182         mlt_producer result = mlt_producer_new( );
183         mlt_producer parent = mlt_producer_cut_parent( this );
184         mlt_properties properties = mlt_producer_properties( result );
185         mlt_properties parent_props = mlt_producer_properties( parent );
186
187         // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
188         if ( in <= 0 )
189                 in = 0;
190         if ( out >= mlt_producer_get_playtime( parent ) )
191                 out = mlt_producer_get_playtime( parent ) - 1;
192
193         mlt_properties_inc_ref( parent_props );
194         mlt_properties_set_int( properties, "_cut", 1 );
195         mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
196         mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
197         mlt_properties_set_position( properties, "in", 0 );
198         mlt_properties_set_position( properties, "out", 0 );
199         mlt_producer_set_in_and_out( result, in, out );
200
201         return result;
202 }
203
204 /** Get the parent service object.
205 */
206
207 mlt_service mlt_producer_service( mlt_producer this )
208 {
209         return this != NULL ? &this->parent : NULL;
210 }
211
212 /** Get the producer properties.
213 */
214
215 mlt_properties mlt_producer_properties( mlt_producer this )
216 {
217         return mlt_service_properties( &this->parent );
218 }
219
220 /** Seek to a specified position.
221 */
222
223 int mlt_producer_seek( mlt_producer this, mlt_position position )
224 {
225         // Determine eof handling
226         mlt_properties properties = mlt_producer_properties( this );
227         char *eof = mlt_properties_get( properties, "eof" );
228         int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
229
230         // Recursive behaviour for cuts...
231         if ( mlt_producer_is_cut( this ) )
232                 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
233
234         // Check bounds
235         if ( position < 0 )
236         {
237                 position = 0;
238         }
239         else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
240         {
241                 mlt_producer_set_speed( this, 0 );
242                 position = mlt_producer_get_playtime( this ) - 1;
243         }
244         else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
245         {
246                 position = position % mlt_producer_get_playtime( this );
247         }
248
249         // Set the position
250         mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
251
252         // Calculate the absolute frame
253         mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
254
255         return 0;
256 }
257
258 /** Get the current position (relative to in point).
259 */
260
261 mlt_position mlt_producer_position( mlt_producer this )
262 {
263         return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
264 }
265
266 /** Get the current position (relative to start of producer).
267 */
268
269 mlt_position mlt_producer_frame( mlt_producer this )
270 {
271         return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
272 }
273
274 /** Set the playing speed.
275 */
276
277 int mlt_producer_set_speed( mlt_producer this, double speed )
278 {
279         return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
280 }
281
282 /** Get the playing speed.
283 */
284
285 double mlt_producer_get_speed( mlt_producer this )
286 {
287         return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
288 }
289
290 /** Get the frames per second.
291 */
292
293 double mlt_producer_get_fps( mlt_producer this )
294 {
295         return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
296 }
297
298 /** Set the in and out points.
299 */
300
301 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
302 {
303         mlt_properties properties = mlt_producer_properties( this );
304
305         // Correct ins and outs if necessary
306         if ( in < 0 )
307                 in = 0;
308         else if ( in > mlt_producer_get_length( this ) )
309                 in = mlt_producer_get_length( this );
310
311         if ( out < 0 )
312                 out = 0;
313         else if ( out > mlt_producer_get_length( this ) )
314                 out = mlt_producer_get_length( this );
315
316         // Swap ins and outs if wrong
317         if ( out < in )
318         {
319                 mlt_position t = in;
320                 in = out;
321                 out = t;
322         }
323
324         // Set the values
325         mlt_events_block( properties, properties );
326         mlt_properties_set_position( properties, "in", in );
327         mlt_events_unblock( properties, properties );
328         mlt_properties_set_position( properties, "out", out );
329
330         return 0;
331 }
332
333 /** Physically reduce the producer (typically a cut) to a 0 length.
334         Essentially, all 0 length cuts should be immediately removed by containers.
335 */
336
337 int mlt_producer_clear( mlt_producer this )
338 {
339         if ( this != NULL )
340         {
341                 mlt_properties properties = mlt_producer_properties( this );
342                 mlt_events_block( properties, properties );
343                 mlt_properties_set_position( properties, "in", 0 );
344                 mlt_events_unblock( properties, properties );
345                 mlt_properties_set_position( properties, "out", -1 );
346         }
347         return 0;
348 }
349
350 /** Get the in point.
351 */
352
353 mlt_position mlt_producer_get_in( mlt_producer this )
354 {
355         return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
356 }
357
358 /** Get the out point.
359 */
360
361 mlt_position mlt_producer_get_out( mlt_producer this )
362 {
363         return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
364 }
365
366 /** Get the total play time.
367 */
368
369 mlt_position mlt_producer_get_playtime( mlt_producer this )
370 {
371         return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
372 }
373
374 /** Get the total length of the producer.
375 */
376
377 mlt_position mlt_producer_get_length( mlt_producer this )
378 {
379         return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
380 }
381
382 /** Prepare for next frame.
383 */
384
385 void mlt_producer_prepare_next( mlt_producer this )
386 {
387         mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
388 }
389
390 /** Get a frame.
391 */
392
393 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
394 {
395         int result = 1;
396         mlt_producer this = service->child;
397
398         if ( !mlt_producer_is_cut( this ) )
399         {
400                 // Get the properties of this producer
401                 mlt_properties properties = mlt_producer_properties( this );
402
403                 // Determine eof handling
404                 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
405
406                 // Get the speed of the producer
407                 double speed = mlt_producer_get_speed( this );
408
409                 // We need to use the clone if it's specified
410                 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
411
412                 // If no clone is specified, use this
413                 clone = clone == NULL ? this : clone;
414
415                 // A properly instatiated producer will have a get_frame method...
416                 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
417                 {
418                         // Generate a test frame
419                         *frame = mlt_frame_init( );
420
421                         // Set the position
422                         result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
423
424                         // Mark as a test card
425                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
426                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
427
428                         // Calculate the next position
429                         mlt_producer_prepare_next( this );
430                 }
431                 else
432                 {
433                         // Get the frame from the implementation
434                         result = this->get_frame( clone, frame, index );
435                 }
436
437                 // Copy the fps and speed of the producer onto the frame
438                 properties = mlt_frame_properties( *frame );
439                 mlt_properties_set_double( properties, "_speed", speed );
440                 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
441                 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
442                 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
443                 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
444                         mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
445         }
446         else
447         {
448                 // Get the parent of this cut
449                 mlt_producer parent = mlt_producer_cut_parent( this );
450
451                 // Get the properties of the parent
452                 mlt_properties parent_properties = mlt_producer_properties( parent );
453
454                 // Get the properties of the cut
455                 mlt_properties properties = mlt_producer_properties( this );
456
457                 // Determine the clone index
458                 int clone_index = mlt_properties_get_int( properties, "_clone" );
459
460                 // Determine the clone to use
461                 mlt_producer clone = this;
462
463                 if ( clone_index > 0 )
464                 {
465                         char key[ 25 ];
466                         sprintf( key, "_clone.%d", clone_index - 1 );
467                         clone = mlt_properties_get_data( mlt_producer_properties( mlt_producer_cut_parent( this ) ), key, NULL );
468                         if ( clone == NULL ) fprintf( stderr, "requested clone doesn't exist %d\n", clone_index );
469                         clone = clone == NULL ? this : clone;
470                 }
471                 else
472                 {
473                         clone = parent;
474                 }
475
476                 // We need to seek to the correct position in the clone
477                 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
478
479                 // Assign the clone property to the parent
480                 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
481
482                 // Now get the frame from the parents service
483                 result = mlt_service_get_frame( mlt_producer_service( parent ), frame, index );
484
485                 // We're done with the clone now
486                 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
487         }
488
489         return result;
490 }
491
492 /** Attach a filter.
493 */
494
495 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
496 {
497         return mlt_service_attach( mlt_producer_service( this ), filter );
498 }
499
500 /** Detach a filter.
501 */
502
503 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
504 {
505         return mlt_service_detach( mlt_producer_service( this ), filter );
506 }
507
508 /** Retrieve a filter.
509 */
510
511 mlt_filter mlt_producer_filter( mlt_producer this, int index )
512 {
513         return mlt_service_filter( mlt_producer_service( this ), index );
514 }
515
516 /** Clone this producer.
517 */
518
519 static mlt_producer mlt_producer_clone( mlt_producer this )
520 {
521         mlt_producer clone = NULL;
522         mlt_properties properties = mlt_producer_properties( this );
523         char *resource = mlt_properties_get( properties, "resource" );
524         char *service = mlt_properties_get( properties, "mlt_service" );
525
526         if ( service != NULL )
527                 clone = mlt_factory_producer( service, resource );
528
529         if ( clone == NULL && resource != NULL )
530                 clone = mlt_factory_producer( "fezzik", resource );
531
532         if ( clone != NULL )
533                 mlt_properties_inherit( mlt_producer_properties( clone ), properties );
534
535         return clone;
536 }
537
538 /** Create clones.
539 */
540
541 static void mlt_producer_set_clones( mlt_producer this, int clones )
542 {
543         mlt_producer parent = mlt_producer_cut_parent( this );
544         mlt_properties properties = mlt_producer_properties( parent );
545         int existing = mlt_properties_get_int( properties, "_clones" );
546         int i = 0;
547         char key[ 25 ];
548
549         // If the number of existing clones is different, then create/remove as necessary
550         if ( existing != clones )
551         {
552                 if ( existing < clones )
553                 {
554                         for ( i = existing; i < clones; i ++ )
555                         {
556                                 mlt_producer clone = mlt_producer_clone( parent );
557                                 sprintf( key, "_clone.%d", i );
558                                 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
559                         }
560                 }
561                 else
562                 {
563                         for ( i = clones; i < existing; i ++ )
564                         {
565                                 sprintf( key, "_clone.%d", i );
566                                 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
567                         }
568                 }
569         }
570
571         // Ensure all properties on the parent are passed to the clones
572         for ( i = 0; i < clones; i ++ )
573         {
574                 mlt_producer clone = NULL;
575                 sprintf( key, "_clone.%d", i );
576                 clone = mlt_properties_get_data( properties, key, NULL );
577                 if ( clone != NULL )
578                         mlt_properties_pass( mlt_producer_properties( clone ), properties, "" );
579         }
580
581         // Update the number of clones on the properties
582         mlt_properties_set_int( properties, "_clones", clones );
583 }
584
585 /** Optimise for overlapping cuts from the same clip.
586 */
587
588 typedef struct 
589 {
590         int multitrack;
591         int track;
592         int position;
593         int length;
594         int offset;
595 }
596 track_info;
597
598 typedef struct
599 {
600         mlt_producer cut;
601         int start;
602         int end;
603 }
604 clip_references;
605
606 static int intersect( clip_references *a, clip_references *b )
607 {
608         int diff = ( a->start - b->start ) + ( a->end - b->end );
609         return diff >= 0 && diff < ( a->end - a->start + 1 );
610 }
611
612 static int push( mlt_parser this, int multitrack, int track, int position )
613 {
614         mlt_properties properties = mlt_parser_properties( this );
615         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
616         track_info *info = malloc( sizeof( track_info ) );
617         info->multitrack = multitrack;
618         info->track = track;
619         info->position = position;
620         info->length = 0;
621         info->offset = 0;
622         return mlt_deque_push_back( stack, info );
623 }
624
625 static track_info *pop( mlt_parser this )
626 {
627         mlt_properties properties = mlt_parser_properties( this );
628         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
629         return mlt_deque_pop_back( stack );
630 }
631
632 static track_info *peek( mlt_parser this )
633 {
634         mlt_properties properties = mlt_parser_properties( this );
635         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
636         return mlt_deque_peek_back( stack );
637 }
638
639 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
640 {
641         track_info *info = peek( this );
642         return push( this, info->multitrack ++, info->track, info->position );
643 }
644
645 static int on_start_track( mlt_parser this )
646 {
647         track_info *info = peek( this );
648         info->position -= info->offset;
649         info->length -= info->offset;
650         return push( this, info->multitrack, info->track ++, info->position );
651 }
652
653 static int on_start_producer( mlt_parser this, mlt_producer object )
654 {
655         mlt_properties properties = mlt_parser_properties( this );
656         mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
657         mlt_producer parent = mlt_producer_cut_parent( object );
658         if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
659         {
660                 int ref_count = 0;
661                 clip_references *old_refs = NULL;
662                 clip_references *refs = NULL;
663                 char key[ 50 ];
664                 int count = 0;
665                 track_info *info = peek( this );
666                 sprintf( key, "%p", parent );
667                 mlt_properties_get_data( producers, key, &count );
668                 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
669                 old_refs = mlt_properties_get_data( properties, key, &ref_count );
670                 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
671                 if ( old_refs != NULL )
672                         memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
673                 mlt_properties_set_int( mlt_producer_properties( object ), "_clone", -1 );
674                 refs[ ref_count ].cut = object;
675                 refs[ ref_count ].start = info->position;
676                 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
677                 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
678                 info->position += mlt_producer_get_playtime( object );
679                 info->length += mlt_producer_get_playtime( object );
680         }
681         return 0;
682 }
683
684 static int on_end_track( mlt_parser this )
685 {
686         track_info *track = pop( this );
687         track_info *multi = peek( this );
688         multi->length += track->length;
689         multi->position += track->length;
690         multi->offset = track->length;
691         free( track );
692         return 0;
693 }
694
695 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
696 {
697         track_info *multi = pop( this );
698         track_info *track = peek( this );
699         track->position += multi->length;
700         track->length += multi->length;
701         free( multi );
702         return 0;
703 }
704
705 int mlt_producer_optimise( mlt_producer this )
706 {
707         int error = 1;
708         mlt_parser parser = mlt_parser_new( );
709         if ( parser != NULL )
710         {
711                 int i = 0, j = 0, k = 0;
712                 mlt_properties properties = mlt_parser_properties( parser );
713                 mlt_properties producers = mlt_properties_new( );
714                 mlt_deque stack = mlt_deque_init( );
715                 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
716                 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
717                 parser->on_start_producer = on_start_producer;
718                 parser->on_start_track = on_start_track;
719                 parser->on_end_track = on_end_track;
720                 parser->on_start_multitrack = on_start_multitrack;
721                 parser->on_end_multitrack = on_end_multitrack;
722                 push( parser, 0, 0, 0 );
723                 mlt_parser_start( parser, mlt_producer_service( this ) );
724                 free( pop( parser ) );
725                 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
726                 {
727                         char *name = mlt_properties_get_name( producers, k );
728                         int count = 0;
729                         int clones = 0;
730                         int max_clones = 0;
731                         mlt_producer producer = mlt_properties_get_data( producers, name, &count );
732                         if ( producer != NULL && count > 1 )
733                         {
734                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
735                                 for ( i = 0; i < count; i ++ )
736                                 {
737                                         clones = 0;
738                                         for ( j = i + 1; j < count; j ++ )
739                                         {
740                                                 if ( intersect( &refs[ i ], &refs[ j ] ) )
741                                                 {
742                                                         clones ++;
743                                                         mlt_properties_set_int( mlt_producer_properties( refs[ j ].cut ), "_clone", clones );
744                                                 }
745                                         }
746                                         if ( clones > max_clones )
747                                                 max_clones = clones;
748                                 }
749
750                                 for ( i = 0; i < count; i ++ )
751                                 {
752                                         mlt_producer cut = refs[ i ].cut;
753                                         if ( mlt_properties_get_int( mlt_producer_properties( cut ), "_clone" ) == -1 )
754                                                 mlt_properties_set_int( mlt_producer_properties( cut ), "_clone", 0 );
755                                 }
756
757                                 mlt_producer_set_clones( producer, max_clones );
758                         }
759                         else if ( producer != NULL )
760                         {
761                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
762                                 for ( i = 0; i < count; i ++ )
763                                 {
764                                         mlt_producer cut = refs[ i ].cut;
765                                         mlt_properties_set_int( mlt_producer_properties( cut ), "_clone", 0 );
766                                 }
767                                 mlt_producer_set_clones( producer, 0 );
768                         }
769                 }
770                 mlt_parser_close( parser );
771         }
772         return error;
773 }
774
775 /** Close the producer.
776 */
777
778 void mlt_producer_close( mlt_producer this )
779 {
780         if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
781         {
782                 this->parent.close = NULL;
783
784                 if ( this->close != NULL )
785                         this->close( this->close_object );
786                 else
787                         mlt_service_close( &this->parent );
788         }
789 }