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