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