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