]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
Extendible blank producers
[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", 59.0 / 54.0 );
82                         }
83                         else
84                         {
85                                 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
86                                 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.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 /** Determine if the producer is a blank [from a playlist].
166 */
167
168 int mlt_producer_is_blank( mlt_producer this )
169 {
170         return this == NULL || !strcmp( mlt_properties_get( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
171 }
172
173 /** Obtain the parent producer.
174 */
175
176 mlt_producer mlt_producer_cut_parent( mlt_producer this )
177 {
178         mlt_properties properties = mlt_producer_properties( this );
179         if ( mlt_producer_is_cut( this ) )
180                 return mlt_properties_get_data( properties, "_cut_parent", NULL );
181         else
182                 return this;
183 }
184
185 /** Create a cut of this producer
186 */
187
188 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
189 {
190         mlt_producer result = mlt_producer_new( );
191         mlt_producer parent = mlt_producer_cut_parent( this );
192         mlt_properties properties = mlt_producer_properties( result );
193         mlt_properties parent_props = mlt_producer_properties( parent );
194
195         // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
196         if ( in <= 0 )
197                 in = 0;
198         if ( out >= mlt_producer_get_playtime( parent ) && !mlt_producer_is_blank( this ) )
199                 out = mlt_producer_get_playtime( parent ) - 1;
200
201         mlt_properties_inc_ref( parent_props );
202         mlt_properties_set_int( properties, "_cut", 1 );
203         mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
204         mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
205         mlt_properties_set_position( properties, "in", 0 );
206         mlt_properties_set_position( properties, "out", 0 );
207         mlt_producer_set_in_and_out( result, in, out );
208
209         return result;
210 }
211
212 /** Get the parent service object.
213 */
214
215 mlt_service mlt_producer_service( mlt_producer this )
216 {
217         return this != NULL ? &this->parent : NULL;
218 }
219
220 /** Get the producer properties.
221 */
222
223 mlt_properties mlt_producer_properties( mlt_producer this )
224 {
225         return mlt_service_properties( &this->parent );
226 }
227
228 /** Seek to a specified position.
229 */
230
231 int mlt_producer_seek( mlt_producer this, mlt_position position )
232 {
233         // Determine eof handling
234         mlt_properties properties = mlt_producer_properties( this );
235         char *eof = mlt_properties_get( properties, "eof" );
236         int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
237
238         // Recursive behaviour for cuts...
239         if ( mlt_producer_is_cut( this ) )
240                 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
241
242         // Check bounds
243         if ( position < 0 )
244         {
245                 position = 0;
246         }
247         else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
248         {
249                 mlt_producer_set_speed( this, 0 );
250                 position = mlt_producer_get_playtime( this ) - 1;
251         }
252         else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
253         {
254                 position = position % mlt_producer_get_playtime( this );
255         }
256
257         // Set the position
258         mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
259
260         // Calculate the absolute frame
261         mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
262
263         return 0;
264 }
265
266 /** Get the current position (relative to in point).
267 */
268
269 mlt_position mlt_producer_position( mlt_producer this )
270 {
271         return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
272 }
273
274 /** Get the current position (relative to start of producer).
275 */
276
277 mlt_position mlt_producer_frame( mlt_producer this )
278 {
279         return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
280 }
281
282 /** Set the playing speed.
283 */
284
285 int mlt_producer_set_speed( mlt_producer this, double speed )
286 {
287         return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
288 }
289
290 /** Get the playing speed.
291 */
292
293 double mlt_producer_get_speed( mlt_producer this )
294 {
295         return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
296 }
297
298 /** Get the frames per second.
299 */
300
301 double mlt_producer_get_fps( mlt_producer this )
302 {
303         return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
304 }
305
306 /** Set the in and out points.
307 */
308
309 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
310 {
311         mlt_properties properties = mlt_producer_properties( this );
312
313         // Correct ins and outs if necessary
314         if ( in < 0 )
315                 in = 0;
316         else if ( in > mlt_producer_get_length( this ) )
317                 in = mlt_producer_get_length( this );
318
319         if ( out < 0 )
320                 out = 0;
321         else if ( out > mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
322                 out = mlt_producer_get_length( this );
323         else if ( out >= mlt_producer_get_length( this ) - 1 && mlt_producer_is_blank( this ) )
324                 mlt_properties_set_position( mlt_producer_properties( this ), "length", out + 1 );
325
326         // Swap ins and outs if wrong
327         if ( out < in )
328         {
329                 mlt_position t = in;
330                 in = out;
331                 out = t;
332         }
333
334         // Set the values
335         mlt_events_block( properties, properties );
336         mlt_properties_set_position( properties, "in", in );
337         mlt_events_unblock( properties, properties );
338         mlt_properties_set_position( properties, "out", out );
339
340         return 0;
341 }
342
343 /** Physically reduce the producer (typically a cut) to a 0 length.
344         Essentially, all 0 length cuts should be immediately removed by containers.
345 */
346
347 int mlt_producer_clear( mlt_producer this )
348 {
349         if ( this != NULL )
350         {
351                 mlt_properties properties = mlt_producer_properties( this );
352                 mlt_events_block( properties, properties );
353                 mlt_properties_set_position( properties, "in", 0 );
354                 mlt_events_unblock( properties, properties );
355                 mlt_properties_set_position( properties, "out", -1 );
356         }
357         return 0;
358 }
359
360 /** Get the in point.
361 */
362
363 mlt_position mlt_producer_get_in( mlt_producer this )
364 {
365         return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
366 }
367
368 /** Get the out point.
369 */
370
371 mlt_position mlt_producer_get_out( mlt_producer this )
372 {
373         return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
374 }
375
376 /** Get the total play time.
377 */
378
379 mlt_position mlt_producer_get_playtime( mlt_producer this )
380 {
381         return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
382 }
383
384 /** Get the total length of the producer.
385 */
386
387 mlt_position mlt_producer_get_length( mlt_producer this )
388 {
389         return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
390 }
391
392 /** Prepare for next frame.
393 */
394
395 void mlt_producer_prepare_next( mlt_producer this )
396 {
397         mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
398 }
399
400 /** Get a frame.
401 */
402
403 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
404 {
405         int result = 1;
406         mlt_producer this = service != NULL ? service->child : NULL;
407
408         if ( this != NULL && !mlt_producer_is_cut( this ) )
409         {
410                 // Get the properties of this producer
411                 mlt_properties properties = mlt_producer_properties( this );
412
413                 // Determine eof handling
414                 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
415
416                 // Get the speed of the producer
417                 double speed = mlt_producer_get_speed( this );
418
419                 // We need to use the clone if it's specified
420                 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
421
422                 // If no clone is specified, use this
423                 clone = clone == NULL ? this : clone;
424
425                 // A properly instatiated producer will have a get_frame method...
426                 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
427                 {
428                         // Generate a test frame
429                         *frame = mlt_frame_init( );
430
431                         // Set the position
432                         result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
433
434                         // Mark as a test card
435                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
436                         mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
437
438                         // Calculate the next position
439                         mlt_producer_prepare_next( this );
440                 }
441                 else
442                 {
443                         // Get the frame from the implementation
444                         result = this->get_frame( clone, frame, index );
445                 }
446
447                 // Copy the fps and speed of the producer onto the frame
448                 properties = mlt_frame_properties( *frame );
449                 mlt_properties_set_double( properties, "_speed", speed );
450                 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
451                 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
452                 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
453                 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
454                         mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
455         }
456         else if ( this != NULL )
457         {
458                 // Get the speed of the cut
459                 double speed = mlt_producer_get_speed( this );
460
461                 // Get the parent of this cut
462                 mlt_producer parent = mlt_producer_cut_parent( this );
463
464                 // Get the properties of the parent
465                 mlt_properties parent_properties = mlt_producer_properties( parent );
466
467                 // Get the properties of the cut
468                 mlt_properties properties = mlt_producer_properties( this );
469
470                 // Determine the clone index
471                 int clone_index = mlt_properties_get_int( properties, "_clone" );
472
473                 // Determine the clone to use
474                 mlt_producer clone = this;
475
476                 if ( clone_index > 0 )
477                 {
478                         char key[ 25 ];
479                         sprintf( key, "_clone.%d", clone_index - 1 );
480                         clone = mlt_properties_get_data( mlt_producer_properties( mlt_producer_cut_parent( this ) ), key, NULL );
481                         if ( clone == NULL ) fprintf( stderr, "requested clone doesn't exist %d\n", clone_index );
482                         clone = clone == NULL ? this : clone;
483                 }
484                 else
485                 {
486                         clone = parent;
487                 }
488
489                 // We need to seek to the correct position in the clone
490                 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
491
492                 // Assign the clone property to the parent
493                 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
494
495                 // Now get the frame from the parents service
496                 result = mlt_service_get_frame( mlt_producer_service( parent ), frame, index );
497
498                 // We're done with the clone now
499                 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
500
501                 mlt_properties_set_double( mlt_frame_properties( *frame ), "_speed", speed );
502                 mlt_producer_prepare_next( this );
503         }
504         else
505         {
506                 *frame = mlt_frame_init( );
507                 result = 0;
508         }
509
510         return result;
511 }
512
513 /** Attach a filter.
514 */
515
516 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
517 {
518         return mlt_service_attach( mlt_producer_service( this ), filter );
519 }
520
521 /** Detach a filter.
522 */
523
524 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
525 {
526         return mlt_service_detach( mlt_producer_service( this ), filter );
527 }
528
529 /** Retrieve a filter.
530 */
531
532 mlt_filter mlt_producer_filter( mlt_producer this, int index )
533 {
534         return mlt_service_filter( mlt_producer_service( this ), index );
535 }
536
537 /** Clone this producer.
538 */
539
540 static mlt_producer mlt_producer_clone( mlt_producer this )
541 {
542         mlt_producer clone = NULL;
543         mlt_properties properties = mlt_producer_properties( this );
544         char *resource = mlt_properties_get( properties, "resource" );
545         char *service = mlt_properties_get( properties, "mlt_service" );
546
547         if ( service != NULL )
548                 clone = mlt_factory_producer( service, resource );
549
550         if ( clone == NULL && resource != NULL )
551                 clone = mlt_factory_producer( "fezzik", resource );
552
553         if ( clone != NULL )
554                 mlt_properties_inherit( mlt_producer_properties( clone ), properties );
555
556         return clone;
557 }
558
559 /** Create clones.
560 */
561
562 static void mlt_producer_set_clones( mlt_producer this, int clones )
563 {
564         mlt_producer parent = mlt_producer_cut_parent( this );
565         mlt_properties properties = mlt_producer_properties( parent );
566         int existing = mlt_properties_get_int( properties, "_clones" );
567         int i = 0;
568         char key[ 25 ];
569
570         // If the number of existing clones is different, then create/remove as necessary
571         if ( existing != clones )
572         {
573                 if ( existing < clones )
574                 {
575                         for ( i = existing; i < clones; i ++ )
576                         {
577                                 mlt_producer clone = mlt_producer_clone( parent );
578                                 sprintf( key, "_clone.%d", i );
579                                 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
580                         }
581                 }
582                 else
583                 {
584                         for ( i = clones; i < existing; i ++ )
585                         {
586                                 sprintf( key, "_clone.%d", i );
587                                 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
588                         }
589                 }
590         }
591
592         // Ensure all properties on the parent are passed to the clones
593         for ( i = 0; i < clones; i ++ )
594         {
595                 mlt_producer clone = NULL;
596                 sprintf( key, "_clone.%d", i );
597                 clone = mlt_properties_get_data( properties, key, NULL );
598                 if ( clone != NULL )
599                         mlt_properties_pass( mlt_producer_properties( clone ), properties, "" );
600         }
601
602         // Update the number of clones on the properties
603         mlt_properties_set_int( properties, "_clones", clones );
604 }
605
606 /** Optimise for overlapping cuts from the same clip.
607 */
608
609 typedef struct 
610 {
611         int multitrack;
612         int track;
613         int position;
614         int length;
615         int offset;
616 }
617 track_info;
618
619 typedef struct
620 {
621         mlt_producer cut;
622         int start;
623         int end;
624 }
625 clip_references;
626
627 static int intersect( clip_references *a, clip_references *b )
628 {
629         int diff = ( a->start - b->start ) + ( a->end - b->end );
630         return diff >= 0 && diff < ( a->end - a->start + 1 );
631 }
632
633 static int push( mlt_parser this, int multitrack, int track, int position )
634 {
635         mlt_properties properties = mlt_parser_properties( this );
636         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
637         track_info *info = malloc( sizeof( track_info ) );
638         info->multitrack = multitrack;
639         info->track = track;
640         info->position = position;
641         info->length = 0;
642         info->offset = 0;
643         return mlt_deque_push_back( stack, info );
644 }
645
646 static track_info *pop( mlt_parser this )
647 {
648         mlt_properties properties = mlt_parser_properties( this );
649         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
650         return mlt_deque_pop_back( stack );
651 }
652
653 static track_info *peek( mlt_parser this )
654 {
655         mlt_properties properties = mlt_parser_properties( this );
656         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
657         return mlt_deque_peek_back( stack );
658 }
659
660 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
661 {
662         track_info *info = peek( this );
663         return push( this, info->multitrack ++, info->track, info->position );
664 }
665
666 static int on_start_track( mlt_parser this )
667 {
668         track_info *info = peek( this );
669         info->position -= info->offset;
670         info->length -= info->offset;
671         return push( this, info->multitrack, info->track ++, info->position );
672 }
673
674 static int on_start_producer( mlt_parser this, mlt_producer object )
675 {
676         mlt_properties properties = mlt_parser_properties( this );
677         mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
678         mlt_producer parent = mlt_producer_cut_parent( object );
679         if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
680         {
681                 int ref_count = 0;
682                 clip_references *old_refs = NULL;
683                 clip_references *refs = NULL;
684                 char key[ 50 ];
685                 int count = 0;
686                 track_info *info = peek( this );
687                 sprintf( key, "%p", parent );
688                 mlt_properties_get_data( producers, key, &count );
689                 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
690                 old_refs = mlt_properties_get_data( properties, key, &ref_count );
691                 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
692                 if ( old_refs != NULL )
693                         memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
694                 mlt_properties_set_int( mlt_producer_properties( object ), "_clone", -1 );
695                 refs[ ref_count ].cut = object;
696                 refs[ ref_count ].start = info->position;
697                 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
698                 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
699                 info->position += mlt_producer_get_playtime( object );
700                 info->length += mlt_producer_get_playtime( object );
701         }
702         return 0;
703 }
704
705 static int on_end_track( mlt_parser this )
706 {
707         track_info *track = pop( this );
708         track_info *multi = peek( this );
709         multi->length += track->length;
710         multi->position += track->length;
711         multi->offset = track->length;
712         free( track );
713         return 0;
714 }
715
716 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
717 {
718         track_info *multi = pop( this );
719         track_info *track = peek( this );
720         track->position += multi->length;
721         track->length += multi->length;
722         free( multi );
723         return 0;
724 }
725
726 int mlt_producer_optimise( mlt_producer this )
727 {
728         int error = 1;
729         mlt_parser parser = mlt_parser_new( );
730         if ( parser != NULL )
731         {
732                 int i = 0, j = 0, k = 0;
733                 mlt_properties properties = mlt_parser_properties( parser );
734                 mlt_properties producers = mlt_properties_new( );
735                 mlt_deque stack = mlt_deque_init( );
736                 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
737                 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
738                 parser->on_start_producer = on_start_producer;
739                 parser->on_start_track = on_start_track;
740                 parser->on_end_track = on_end_track;
741                 parser->on_start_multitrack = on_start_multitrack;
742                 parser->on_end_multitrack = on_end_multitrack;
743                 push( parser, 0, 0, 0 );
744                 mlt_parser_start( parser, mlt_producer_service( this ) );
745                 free( pop( parser ) );
746                 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
747                 {
748                         char *name = mlt_properties_get_name( producers, k );
749                         int count = 0;
750                         int clones = 0;
751                         int max_clones = 0;
752                         mlt_producer producer = mlt_properties_get_data( producers, name, &count );
753                         if ( producer != NULL && count > 1 )
754                         {
755                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
756                                 for ( i = 0; i < count; i ++ )
757                                 {
758                                         clones = 0;
759                                         for ( j = i + 1; j < count; j ++ )
760                                         {
761                                                 if ( intersect( &refs[ i ], &refs[ j ] ) )
762                                                 {
763                                                         clones ++;
764                                                         mlt_properties_set_int( mlt_producer_properties( refs[ j ].cut ), "_clone", clones );
765                                                 }
766                                         }
767                                         if ( clones > max_clones )
768                                                 max_clones = clones;
769                                 }
770
771                                 for ( i = 0; i < count; i ++ )
772                                 {
773                                         mlt_producer cut = refs[ i ].cut;
774                                         if ( mlt_properties_get_int( mlt_producer_properties( cut ), "_clone" ) == -1 )
775                                                 mlt_properties_set_int( mlt_producer_properties( cut ), "_clone", 0 );
776                                 }
777
778                                 mlt_producer_set_clones( producer, max_clones );
779                         }
780                         else if ( producer != NULL )
781                         {
782                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
783                                 for ( i = 0; i < count; i ++ )
784                                 {
785                                         mlt_producer cut = refs[ i ].cut;
786                                         mlt_properties_set_int( mlt_producer_properties( cut ), "_clone", 0 );
787                                 }
788                                 mlt_producer_set_clones( producer, 0 );
789                         }
790                 }
791                 mlt_parser_close( parser );
792         }
793         return error;
794 }
795
796 /** Close the producer.
797 */
798
799 void mlt_producer_close( mlt_producer this )
800 {
801         if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
802         {
803                 this->parent.close = NULL;
804
805                 if ( this->close != NULL )
806                         this->close( this->close_object );
807                 else
808                         mlt_service_close( &this->parent );
809         }
810 }