]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
framework: remove global profile, rather share one mlt_profile across a service netwo...
[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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #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 "mlt_profile.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <math.h>
32
33 /** Forward references.
34 */
35
36 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
37 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
38 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
39
40 //#define _MLT_PRODUCER_CHECKS_ 1
41
42 #ifdef _MLT_PRODUCER_CHECKS_
43 static int producers_created = 0;
44 static int producers_destroyed = 0;
45 #endif
46
47 /** Constructor
48 */
49
50 int mlt_producer_init( mlt_producer this, void *child )
51 {
52         // Check that we haven't received NULL
53         int error = this == NULL;
54
55         // Continue if no error
56         if ( error == 0 )
57         {
58 #ifdef _MLT_PRODUCER_CHECKS_
59                 producers_created ++;
60 #endif
61
62                 // Initialise the producer
63                 memset( this, 0, sizeof( struct mlt_producer_s ) );
64         
65                 // Associate with the child
66                 this->child = child;
67
68                 // Initialise the service
69                 if ( mlt_service_init( &this->parent, this ) == 0 )
70                 {
71                         // The parent is the service
72                         mlt_service parent = &this->parent;
73         
74                         // Define the parent close
75                         parent->close = ( mlt_destructor )mlt_producer_close;
76                         parent->close_object = this;
77
78                         // For convenience, we'll assume the close_object is this
79                         this->close_object = this;
80
81                         // Get the properties of the parent
82                         mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
83         
84                         // Set the default properties
85                         mlt_properties_set( properties, "mlt_type", "mlt_producer" );
86                         mlt_properties_set_position( properties, "_position", 0.0 );
87                         mlt_properties_set_double( properties, "_frame", 0 );
88                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
89                         mlt_properties_set_double( properties, "_speed", 1.0 );
90                         mlt_properties_set_position( properties, "in", 0 );
91                         mlt_properties_set_position( properties, "out", 14999 );
92                         mlt_properties_set_position( properties, "length", 15000 );
93                         mlt_properties_set( properties, "eof", "pause" );
94                         mlt_properties_set( properties, "resource", "<producer>" );
95
96                         // Override service get_frame
97                         parent->get_frame = producer_get_frame;
98
99                         mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
100                         mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
101                         mlt_events_register( properties, "producer-changed", NULL );
102                 }
103         }
104
105         return error;
106 }
107
108 /** Listener for property changes.
109 */
110
111 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
112 {
113         if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
114                 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
115 }
116
117 /** Listener for service changes.
118 */
119
120 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
121 {
122         mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
123 }
124
125 /** Create a new producer.
126 */
127
128 mlt_producer mlt_producer_new( )
129 {
130         mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
131         mlt_producer_init( this, NULL );
132         return this;
133 }
134
135 /** Determine if producer is a cut.
136 */
137
138 int mlt_producer_is_cut( mlt_producer this )
139 {
140         return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "_cut" );
141 }
142
143 /** Determine if producer is a mix.
144 */
145
146 int mlt_producer_is_mix( mlt_producer this )
147 {
148         mlt_properties properties = this != NULL ? MLT_PRODUCER_PROPERTIES( this ) : NULL;
149         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
150         return tractor != NULL;
151 }
152
153 /** Determine if the producer is a blank [from a playlist].
154 */
155
156 int mlt_producer_is_blank( mlt_producer this )
157 {
158         return this == NULL || !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
159 }
160
161 /** Obtain the parent producer.
162 */
163
164 mlt_producer mlt_producer_cut_parent( mlt_producer this )
165 {
166         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
167         if ( mlt_producer_is_cut( this ) )
168                 return mlt_properties_get_data( properties, "_cut_parent", NULL );
169         else
170                 return this;
171 }
172
173 /** Create a cut of this producer
174 */
175
176 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
177 {
178         mlt_producer result = mlt_producer_new( );
179         mlt_producer parent = mlt_producer_cut_parent( this );
180         mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
181         mlt_properties parent_props = MLT_PRODUCER_PROPERTIES( parent );
182
183         mlt_events_block( MLT_PRODUCER_PROPERTIES( result ), MLT_PRODUCER_PROPERTIES( result ) );
184         // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
185         if ( in <= 0 )
186                 in = 0;
187         if ( ( out < 0 || out >= mlt_producer_get_length( parent ) ) && !mlt_producer_is_blank( this ) )
188                 out = mlt_producer_get_length( parent ) - 1;
189
190         mlt_properties_inc_ref( parent_props );
191         mlt_properties_set_int( properties, "_cut", 1 );
192         mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
193         mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
194         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( parent_props, "aspect_ratio" ) );
195         mlt_producer_set_in_and_out( result, in, out );
196
197         return result;
198 }
199
200 /** Get the parent service object.
201 */
202
203 mlt_service mlt_producer_service( mlt_producer this )
204 {
205         return this != NULL ? &this->parent : NULL;
206 }
207
208 /** Get the producer properties.
209 */
210
211 mlt_properties mlt_producer_properties( mlt_producer this )
212 {
213         return MLT_SERVICE_PROPERTIES( &this->parent );
214 }
215
216 /** Seek to a specified position.
217 */
218
219 int mlt_producer_seek( mlt_producer this, mlt_position position )
220 {
221         // Determine eof handling
222         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
223         char *eof = mlt_properties_get( properties, "eof" );
224         int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
225
226         // Recursive behaviour for cuts - repositions parent and then repositions cut
227         // hence no return on this condition
228         if ( mlt_producer_is_cut( this ) )
229                 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
230
231         // Check bounds
232         if ( position < 0 || mlt_producer_get_playtime( this ) == 0 )
233         {
234                 position = 0;
235         }
236         else if ( use_points && ( eof == NULL || !strcmp( eof, "pause" ) ) && position >= mlt_producer_get_playtime( this ) )
237         {
238                 mlt_producer_set_speed( this, 0 );
239                 position = mlt_producer_get_playtime( this ) - 1;
240         }
241         else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
242         {
243                 position = (int)position % (int)mlt_producer_get_playtime( this );
244         }
245
246         // Set the position
247         mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_position", position );
248
249         // Calculate the absolute frame
250         mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
251
252         return 0;
253 }
254
255 /** Get the current position (relative to in point).
256 */
257
258 mlt_position mlt_producer_position( mlt_producer this )
259 {
260         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_position" );
261 }
262
263 /** Get the current position (relative to start of producer).
264 */
265
266 mlt_position mlt_producer_frame( mlt_producer this )
267 {
268         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_frame" );
269 }
270
271 /** Set the playing speed.
272 */
273
274 int mlt_producer_set_speed( mlt_producer this, double speed )
275 {
276         return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( this ), "_speed", speed );
277 }
278
279 /** Get the playing speed.
280 */
281
282 double mlt_producer_get_speed( mlt_producer this )
283 {
284         return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "_speed" );
285 }
286
287 /** Get the frames per second.
288 */
289
290 double mlt_producer_get_fps( mlt_producer this )
291 {
292         mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
293         return mlt_profile_fps( profile );
294 }
295
296 /** Set the in and out points.
297 */
298
299 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
300 {
301         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
302
303         // Correct ins and outs if necessary
304         if ( in < 0 )
305                 in = 0;
306         else if ( in >= mlt_producer_get_length( this ) )
307                 in = mlt_producer_get_length( this ) - 1;
308
309         if ( out < 0 )
310                 out = 0;
311         else if ( out >= mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
312                 out = mlt_producer_get_length( this ) - 1;
313         else if ( out >= mlt_producer_get_length( this ) && mlt_producer_is_blank( this ) )
314                 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "length", out + 1 );
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         if ( mlt_producer_get_speed( this ) != 0 )
388                 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
389 }
390
391 /** Get a frame.
392 */
393
394 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
395 {
396         int result = 1;
397         mlt_producer this = service != NULL ? service->child : NULL;
398
399         if ( this != NULL && !mlt_producer_is_cut( this ) )
400         {
401                 // Get the properties of this producer
402                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
403
404                 // Determine eof handling
405                 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( this ), "eof" );
406
407                 // Get the speed of the producer
408                 double speed = mlt_producer_get_speed( this );
409
410                 // We need to use the clone if it's specified
411                 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
412
413                 // If no clone is specified, use this
414                 clone = clone == NULL ? this : clone;
415
416                 // A properly instatiated producer will have a get_frame method...
417                 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
418                 {
419                         // Generate a test frame
420                         *frame = mlt_frame_init( service );
421
422                         // Set the position
423                         result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
424
425                         // Mark as a test card
426                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 1 );
427                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", 1 );
428
429                         // Calculate the next position
430                         mlt_producer_prepare_next( this );
431                 }
432                 else
433                 {
434                         // Get the frame from the implementation
435                         result = this->get_frame( clone, frame, index );
436                 }
437
438                 // Copy the fps and speed of the producer onto the frame
439                 properties = MLT_FRAME_PROPERTIES( *frame );
440                 mlt_properties_set_double( properties, "_speed", speed );
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 if ( this != NULL )
447         {
448                 // Get the speed of the cut
449                 double speed = mlt_producer_get_speed( this );
450
451                 // Get the parent of this cut
452                 mlt_producer parent = mlt_producer_cut_parent( this );
453
454                 // Get the properties of the parent
455                 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
456
457                 // Get the properties of the cut
458                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
459
460                 // Determine the clone index
461                 int clone_index = mlt_properties_get_int( properties, "_clone" );
462
463                 // Determine the clone to use
464                 mlt_producer clone = this;
465
466                 if ( clone_index > 0 )
467                 {
468                         char key[ 25 ];
469                         sprintf( key, "_clone.%d", clone_index - 1 );
470                         clone = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), key, NULL );
471                         if ( clone == NULL ) fprintf( stderr, "requested clone doesn't exist %d\n", clone_index );
472                         clone = clone == NULL ? this : clone;
473                 }
474                 else
475                 {
476                         clone = parent;
477                 }
478
479                 // We need to seek to the correct position in the clone
480                 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
481
482                 // Assign the clone property to the parent
483                 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
484
485                 // Now get the frame from the parents service
486                 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
487
488                 // We're done with the clone now
489                 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
490
491                 // This is useful and required by always_active transitions to determine in/out points of the cut
492                 if ( mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", NULL ) == MLT_PRODUCER_SERVICE( parent ) )
493                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", this, 0, NULL, NULL );
494
495                 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
496                 mlt_producer_prepare_next( this );
497         }
498         else
499         {
500                 *frame = mlt_frame_init( service );
501                 result = 0;
502         }
503
504         // Pass on all meta properties from the producer/cut on to the frame
505         if ( *frame != NULL && this != NULL )
506         {
507                 int i = 0;
508                 mlt_properties p_props = MLT_PRODUCER_PROPERTIES( this );
509                 mlt_properties f_props = MLT_FRAME_PROPERTIES( *frame );
510                 int count = mlt_properties_count( p_props );
511                 for ( i = 0; i < count; i ++ )
512                 {
513                         char *name = mlt_properties_get_name( p_props, i );
514                         if ( !strncmp( name, "meta.", 5 ) )
515                                 mlt_properties_set( f_props, name, mlt_properties_get( p_props, name ) );
516                         else if ( !strncmp( name, "set.", 4 ) )
517                                 mlt_properties_set( f_props, name + 4, mlt_properties_get( p_props, name ) );
518                 }
519         }
520
521         return result;
522 }
523
524 /** Attach a filter.
525 */
526
527 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
528 {
529         return mlt_service_attach( MLT_PRODUCER_SERVICE( this ), filter );
530 }
531
532 /** Detach a filter.
533 */
534
535 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
536 {
537         return mlt_service_detach( MLT_PRODUCER_SERVICE( this ), filter );
538 }
539
540 /** Retrieve a filter.
541 */
542
543 mlt_filter mlt_producer_filter( mlt_producer this, int index )
544 {
545         return mlt_service_filter( MLT_PRODUCER_SERVICE( this ), index );
546 }
547
548 /** Clone this producer.
549 */
550
551 static mlt_producer mlt_producer_clone( mlt_producer this )
552 {
553         mlt_producer clone = NULL;
554         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
555         char *resource = mlt_properties_get( properties, "resource" );
556         char *service = mlt_properties_get( properties, "mlt_service" );
557         mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
558
559         mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
560
561         if ( service != NULL )
562                 clone = mlt_factory_producer( profile, service, resource );
563
564         if ( clone == NULL && resource != NULL )
565                 clone = mlt_factory_producer( profile, mlt_environment( "MLT_PRODUCER" ), resource );
566
567         if ( clone != NULL )
568                 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
569
570         mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
571
572         return clone;
573 }
574
575 /** Create clones.
576 */
577
578 static void mlt_producer_set_clones( mlt_producer this, int clones )
579 {
580         mlt_producer parent = mlt_producer_cut_parent( this );
581         mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
582         int existing = mlt_properties_get_int( properties, "_clones" );
583         int i = 0;
584         char key[ 25 ];
585
586         // If the number of existing clones is different, then create/remove as necessary
587         if ( existing != clones )
588         {
589                 if ( existing < clones )
590                 {
591                         for ( i = existing; i < clones; i ++ )
592                         {
593                                 mlt_producer clone = mlt_producer_clone( parent );
594                                 sprintf( key, "_clone.%d", i );
595                                 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
596                         }
597                 }
598                 else
599                 {
600                         for ( i = clones; i < existing; i ++ )
601                         {
602                                 sprintf( key, "_clone.%d", i );
603                                 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
604                         }
605                 }
606         }
607
608         // Ensure all properties on the parent are passed to the clones
609         for ( i = 0; i < clones; i ++ )
610         {
611                 mlt_producer clone = NULL;
612                 sprintf( key, "_clone.%d", i );
613                 clone = mlt_properties_get_data( properties, key, NULL );
614                 if ( clone != NULL )
615                         mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
616         }
617
618         // Update the number of clones on the properties
619         mlt_properties_set_int( properties, "_clones", clones );
620 }
621
622 /** Optimise for overlapping cuts from the same clip.
623 */
624
625 typedef struct 
626 {
627         int multitrack;
628         int track;
629         int position;
630         int length;
631         int offset;
632 }
633 track_info;
634
635 typedef struct
636 {
637         mlt_producer cut;
638         int start;
639         int end;
640 }
641 clip_references;
642
643 static int intersect( clip_references *a, clip_references *b )
644 {
645         int diff = ( a->start - b->start ) + ( a->end - b->end );
646         return diff >= 0 && diff < ( a->end - a->start + 1 );
647 }
648
649 static int push( mlt_parser this, int multitrack, int track, int position )
650 {
651         mlt_properties properties = mlt_parser_properties( this );
652         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
653         track_info *info = malloc( sizeof( track_info ) );
654         info->multitrack = multitrack;
655         info->track = track;
656         info->position = position;
657         info->length = 0;
658         info->offset = 0;
659         return mlt_deque_push_back( stack, info );
660 }
661
662 static track_info *pop( mlt_parser this )
663 {
664         mlt_properties properties = mlt_parser_properties( this );
665         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
666         return mlt_deque_pop_back( stack );
667 }
668
669 static track_info *peek( mlt_parser this )
670 {
671         mlt_properties properties = mlt_parser_properties( this );
672         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
673         return mlt_deque_peek_back( stack );
674 }
675
676 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
677 {
678         track_info *info = peek( this );
679         return push( this, info->multitrack ++, info->track, info->position );
680 }
681
682 static int on_start_track( mlt_parser this )
683 {
684         track_info *info = peek( this );
685         info->position -= info->offset;
686         info->length -= info->offset;
687         return push( this, info->multitrack, info->track ++, info->position );
688 }
689
690 static int on_start_producer( mlt_parser this, mlt_producer object )
691 {
692         mlt_properties properties = mlt_parser_properties( this );
693         mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
694         mlt_producer parent = mlt_producer_cut_parent( object );
695         if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
696         {
697                 int ref_count = 0;
698                 clip_references *old_refs = NULL;
699                 clip_references *refs = NULL;
700                 char key[ 50 ];
701                 int count = 0;
702                 track_info *info = peek( this );
703                 sprintf( key, "%p", parent );
704                 mlt_properties_get_data( producers, key, &count );
705                 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
706                 old_refs = mlt_properties_get_data( properties, key, &ref_count );
707                 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
708                 if ( old_refs != NULL )
709                         memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
710                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object ), "_clone", -1 );
711                 refs[ ref_count ].cut = object;
712                 refs[ ref_count ].start = info->position;
713                 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
714                 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
715                 info->position += mlt_producer_get_playtime( object );
716                 info->length += mlt_producer_get_playtime( object );
717         }
718         return 0;
719 }
720
721 static int on_end_track( mlt_parser this )
722 {
723         track_info *track = pop( this );
724         track_info *multi = peek( this );
725         multi->length += track->length;
726         multi->position += track->length;
727         multi->offset = track->length;
728         free( track );
729         return 0;
730 }
731
732 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
733 {
734         track_info *multi = pop( this );
735         track_info *track = peek( this );
736         track->position += multi->length;
737         track->length += multi->length;
738         free( multi );
739         return 0;
740 }
741
742 int mlt_producer_optimise( mlt_producer this )
743 {
744         int error = 1;
745         mlt_parser parser = mlt_parser_new( );
746         if ( parser != NULL )
747         {
748                 int i = 0, j = 0, k = 0;
749                 mlt_properties properties = mlt_parser_properties( parser );
750                 mlt_properties producers = mlt_properties_new( );
751                 mlt_deque stack = mlt_deque_init( );
752                 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
753                 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
754                 parser->on_start_producer = on_start_producer;
755                 parser->on_start_track = on_start_track;
756                 parser->on_end_track = on_end_track;
757                 parser->on_start_multitrack = on_start_multitrack;
758                 parser->on_end_multitrack = on_end_multitrack;
759                 push( parser, 0, 0, 0 );
760                 mlt_parser_start( parser, MLT_PRODUCER_SERVICE( this ) );
761                 free( pop( parser ) );
762                 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
763                 {
764                         char *name = mlt_properties_get_name( producers, k );
765                         int count = 0;
766                         int clones = 0;
767                         int max_clones = 0;
768                         mlt_producer producer = mlt_properties_get_data( producers, name, &count );
769                         if ( producer != NULL && count > 1 )
770                         {
771                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
772                                 for ( i = 0; i < count; i ++ )
773                                 {
774                                         clones = 0;
775                                         for ( j = i + 1; j < count; j ++ )
776                                         {
777                                                 if ( intersect( &refs[ i ], &refs[ j ] ) )
778                                                 {
779                                                         clones ++;
780                                                         mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
781                                                 }
782                                         }
783                                         if ( clones > max_clones )
784                                                 max_clones = clones;
785                                 }
786
787                                 for ( i = 0; i < count; i ++ )
788                                 {
789                                         mlt_producer cut = refs[ i ].cut;
790                                         if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone" ) == -1 )
791                                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
792                                 }
793
794                                 mlt_producer_set_clones( producer, max_clones );
795                         }
796                         else if ( producer != NULL )
797                         {
798                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
799                                 for ( i = 0; i < count; i ++ )
800                                 {
801                                         mlt_producer cut = refs[ i ].cut;
802                                         mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
803                                 }
804                                 mlt_producer_set_clones( producer, 0 );
805                         }
806                 }
807                 mlt_parser_close( parser );
808         }
809         return error;
810 }
811
812 /** Close the producer.
813 */
814
815 void mlt_producer_close( mlt_producer this )
816 {
817         if ( this != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( this ) ) <= 0 )
818         {
819                 this->parent.close = NULL;
820
821                 if ( this->close != NULL )
822                 {
823                         this->close( this->close_object );
824                 }
825                 else
826                 {
827                         int destroy = mlt_producer_is_cut( this );
828
829 #if _MLT_PRODUCER_CHECKS_ == 1
830                         // Show debug info
831                         mlt_properties_debug( MLT_PRODUCER_PROPERTIES( this ), "Producer closing", stderr );
832 #endif
833
834 #ifdef _MLT_PRODUCER_CHECKS_
835                         // Increment destroyed count
836                         producers_destroyed ++;
837
838                         // Show current stats - these should match when the app is closed
839                         fprintf( stderr, "Producers created %d, destroyed %d\n", producers_created, producers_destroyed );
840 #endif
841
842                         mlt_service_close( &this->parent );
843
844                         if ( destroy )
845                                 free( this );
846                 }
847         }
848 }