]> git.sesse.net Git - mlt/blob - src/framework/mlt_producer.c
Fix crash on null eof string.
[mlt] / src / framework / mlt_producer.c
1 /**
2  * \file mlt_producer.c
3  * \brief abstraction for all producer services
4  * \see mlt_producer_s
5  *
6  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "mlt_producer.h"
25 #include "mlt_factory.h"
26 #include "mlt_frame.h"
27 #include "mlt_parser.h"
28 #include "mlt_profile.h"
29 #include "mlt_log.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 /* Forward references. */
36
37 static int producer_get_frame( mlt_service self, mlt_frame_ptr frame, int index );
38 static void mlt_producer_property_changed( mlt_service owner, mlt_producer self, char *name );
39 static void mlt_producer_service_changed( mlt_service owner, mlt_producer self );
40
41 /* for debugging */
42 //#define _MLT_PRODUCER_CHECKS_ 1
43 #ifdef _MLT_PRODUCER_CHECKS_
44 static int producers_created = 0;
45 static int producers_destroyed = 0;
46 #endif
47
48 /** Initialize a producer service.
49  *
50  * \public \memberof mlt_producer_s
51  * \param self the producer structure to initialize
52  * \param child a pointer to the child object for the subclass
53  * \return true if there was an error
54  */
55
56 int mlt_producer_init( mlt_producer self, void *child )
57 {
58         // Check that we haven't received NULL
59         int error = self == NULL;
60
61         // Continue if no error
62         if ( error == 0 )
63         {
64 #ifdef _MLT_PRODUCER_CHECKS_
65                 producers_created ++;
66 #endif
67
68                 // Initialise the producer
69                 memset( self, 0, sizeof( struct mlt_producer_s ) );
70
71                 // Associate with the child
72                 self->child = child;
73
74                 // Initialise the service
75                 if ( mlt_service_init( &self->parent, self ) == 0 )
76                 {
77                         // The parent is the service
78                         mlt_service parent = &self->parent;
79
80                         // Define the parent close
81                         parent->close = ( mlt_destructor )mlt_producer_close;
82                         parent->close_object = self;
83
84                         // For convenience, we'll assume the close_object is self
85                         self->close_object = self;
86
87                         // Get the properties of the parent
88                         mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
89
90                         // Set the default properties
91                         mlt_properties_set( properties, "mlt_type", "mlt_producer" );
92                         mlt_properties_set_position( properties, "_position", 0.0 );
93                         mlt_properties_set_double( properties, "_frame", 0 );
94                         mlt_properties_set_double( properties, "_speed", 1.0 );
95                         mlt_properties_set_position( properties, "in", 0 );
96                         mlt_properties_set_position( properties, "out", 14999 );
97                         mlt_properties_set_position( properties, "length", 15000 );
98                         mlt_properties_set( properties, "eof", "pause" );
99                         mlt_properties_set( properties, "resource", "<producer>" );
100
101                         // Override service get_frame
102                         parent->get_frame = producer_get_frame;
103
104                         mlt_events_listen( properties, self, "service-changed", ( mlt_listener )mlt_producer_service_changed );
105                         mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_producer_property_changed );
106                         mlt_events_register( properties, "producer-changed", NULL );
107                 }
108         }
109
110         return error;
111 }
112
113 /** Listener for property changes.
114  *
115  * If the in, out, or length properties changed, fire a "producer-changed" event.
116  *
117  * \private \memberof mlt_producer_s
118  * \param owner a service (ignored)
119  * \param self the producer
120  * \param name the property that changed
121  */
122
123 static void mlt_producer_property_changed( mlt_service owner, mlt_producer self, char *name )
124 {
125         if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
126                 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( self ) ), "producer-changed", NULL );
127 }
128
129 /** Listener for service changes.
130  *
131  * Fires the "producer-changed" event.
132  *
133  * \private \memberof mlt_producer_s
134  * \param owner a service (ignored)
135  * \param self the producer
136  */
137
138 static void mlt_producer_service_changed( mlt_service owner, mlt_producer self )
139 {
140         mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( self ) ), "producer-changed", NULL );
141 }
142
143 /** Create and initialize a new producer.
144  *
145  * \public \memberof mlt_producer_s
146  * \return the new producer
147  */
148
149 mlt_producer mlt_producer_new( mlt_profile profile )
150 {
151         mlt_producer self = malloc( sizeof( struct mlt_producer_s ) );
152         if ( self )
153         {
154                 if ( mlt_producer_init( self, NULL ) == 0 )
155                 {
156                         mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( self ), "_profile", profile, 0, NULL, NULL );
157                         mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( self ), "aspect_ratio", mlt_profile_sar( profile ) );
158                 }
159                 else
160                 {
161                         free( self );
162                         return NULL;
163                 }
164         }
165         return self;
166 }
167
168 /** Determine if producer is a cut.
169  *
170  * \public \memberof mlt_producer_s
171  * \param self a producer
172  * \return true if \p self is a "cut" producer
173  * \see mlt_producer_cut
174  */
175
176 int mlt_producer_is_cut( mlt_producer self )
177 {
178         return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( self ), "_cut" );
179 }
180
181 /** Determine if producer is a mix.
182  *
183  * \public \memberof mlt_producer_s
184  * \param self a producer
185  * \return true if \p self is a "mix" producer
186  * \todo Define a mix producer.
187  */
188
189 int mlt_producer_is_mix( mlt_producer self )
190 {
191         mlt_properties properties = self != NULL ? MLT_PRODUCER_PROPERTIES( self ) : NULL;
192         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
193         return tractor != NULL;
194 }
195
196 /** Determine if the producer is a blank.
197  *
198  * Blank producers should only appear as an item in a playlist.
199  * \public \memberof mlt_producer_s
200  * \param self a producer
201  * \return true if \p self is a "blank" producer
202  * \see mlt_playlist_insert_blank
203  */
204
205 int mlt_producer_is_blank( mlt_producer self )
206 {
207         return self == NULL || !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( self ) ), "resource" ), "blank" );
208 }
209
210 /** Obtain the parent producer.
211  *
212  * \public \memberof mlt_producer_s
213  * \param self a producer
214  * \return either the parent producer if \p self is a "cut" producer or \p self otherwise.
215  */
216
217 mlt_producer mlt_producer_cut_parent( mlt_producer self )
218 {
219         mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
220         if ( mlt_producer_is_cut( self ) )
221                 return mlt_properties_get_data( properties, "_cut_parent", NULL );
222         else
223                 return self;
224 }
225
226 /** Create a cut of this producer.
227  *
228  * A "cut" is a portion of another (parent) producer.
229  *
230  * \public \memberof mlt_producer_s
231  * \param self a producer
232  * \param in the beginning
233  * \param out the end
234  * \return the new producer
235  * \todo Expand on the value of a cut.
236  */
237
238 mlt_producer mlt_producer_cut( mlt_producer self, int in, int out )
239 {
240         mlt_producer result = mlt_producer_new( mlt_service_profile( MLT_PRODUCER_SERVICE( self ) ) );
241         mlt_producer parent = mlt_producer_cut_parent( self );
242         mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
243         mlt_properties parent_props = MLT_PRODUCER_PROPERTIES( parent );
244
245         mlt_events_block( MLT_PRODUCER_PROPERTIES( result ), MLT_PRODUCER_PROPERTIES( result ) );
246         // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
247         if ( in <= 0 )
248                 in = 0;
249         if ( ( out < 0 || out >= mlt_producer_get_length( parent ) ) && !mlt_producer_is_blank( self ) )
250                 out = mlt_producer_get_length( parent ) - 1;
251
252         mlt_properties_inc_ref( parent_props );
253         mlt_properties_set_int( properties, "_cut", 1 );
254         mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
255         mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
256         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( parent_props, "aspect_ratio" ) );
257         mlt_producer_set_in_and_out( result, in, out );
258
259         return result;
260 }
261
262 /** Get the parent service object.
263  *
264  * \public \memberof mlt_producer_s
265  * \param self a producer
266  * \return the service parent class
267  * \see MLT_PRODUCER_SERVICE
268  */
269
270 mlt_service mlt_producer_service( mlt_producer self )
271 {
272         return self != NULL ? &self->parent : NULL;
273 }
274
275 /** Get the producer properties.
276  *
277  * \public \memberof mlt_producer_s
278  * \param self a producer
279  * \return the producer's property list
280  * \see MLT_PRODUCER_PROPERTIES
281  */
282
283 mlt_properties mlt_producer_properties( mlt_producer self )
284 {
285         return MLT_SERVICE_PROPERTIES( &self->parent );
286 }
287
288 /** Seek to a specified position.
289  *
290  * \public \memberof mlt_producer_s
291  * \param self a producer
292  * \param position set the "play head" position of the producer
293  * \return false
294  * \todo Document how the properties affect behavior.
295  * \see mlt_producer_seek_time
296  */
297
298 int mlt_producer_seek( mlt_producer self, mlt_position position )
299 {
300         // Determine eof handling
301         mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
302         char *eof = mlt_properties_get( properties, "eof" );
303         int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
304
305         // Recursive behaviour for cuts - repositions parent and then repositions cut
306         // hence no return on this condition
307         if ( mlt_producer_is_cut( self ) )
308                 mlt_producer_seek( mlt_producer_cut_parent( self ), position + mlt_producer_get_in( self ) );
309
310         // Check bounds
311         if ( position < 0 || mlt_producer_get_playtime( self ) == 0 )
312         {
313                 position = 0;
314         }
315         else if ( use_points && ( eof == NULL || !strcmp( eof, "pause" ) ) && position >= mlt_producer_get_playtime( self ) )
316         {
317                 mlt_producer_set_speed( self, 0 );
318                 position = mlt_producer_get_playtime( self ) - 1;
319         }
320         else if ( use_points && eof && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( self ) )
321         {
322                 position = (int)position % (int)mlt_producer_get_playtime( self );
323         }
324
325         // Set the position
326         mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( self ), "_position", position );
327
328         // Calculate the absolute frame
329         mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( self ), "_frame", use_points * mlt_producer_get_in( self ) + position );
330
331         return 0;
332 }
333
334 /** Seek to a specified time string.
335  *
336  * \public \memberof mlt_producer_s
337  * \param self a producer
338  * \param time set the "play head" position of the producer to the time string
339  * \return false
340  * \see mlt_producer_seek
341  */
342
343 int mlt_producer_seek_time( mlt_producer self, const char* time )
344 {
345     mlt_properties_set( MLT_PRODUCER_PROPERTIES(self), "_seek_time", time );
346     mlt_position position = mlt_properties_get_position( MLT_PRODUCER_PROPERTIES(self), "_seek_time" );
347     return mlt_producer_seek( self, position );
348 }
349
350 /** Get the current position (relative to in point).
351  *
352  * \public \memberof mlt_producer_s
353  * \param self a producer
354  * \return the position of the "play head" relative to its beginning
355  */
356
357 mlt_position mlt_producer_position( mlt_producer self )
358 {
359         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "_position" );
360 }
361
362 /** Get the current position (relative to start of producer).
363  *
364  * \public \memberof mlt_producer_s
365  * \param self a producer
366  * \return the position of the "play head" regardless of the in point
367  */
368
369 mlt_position mlt_producer_frame( mlt_producer self )
370 {
371         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "_frame" );
372 }
373
374 /** Get the current position (relative to start of producer) as a time string.
375  *
376  * \public \memberof mlt_producer_s
377  * \param self a producer
378  * \param format the time value format
379  * \return the position of the "play head" regardless of the in point
380  */
381
382 char* mlt_producer_frame_time( mlt_producer self, mlt_time_format format )
383 {
384     return mlt_properties_get_time( MLT_PRODUCER_PROPERTIES( self ), "_frame", format );
385 }
386
387 /** Set the playing speed.
388  *
389  * \public \memberof mlt_producer_s
390  * \param self a producer
391  * \param speed the new speed as a relative factor (1.0 = normal)
392  * \return true if error
393  */
394
395 int mlt_producer_set_speed( mlt_producer self, double speed )
396 {
397         return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( self ), "_speed", speed );
398 }
399
400 /** Get the playing speed.
401  *
402  * \public \memberof mlt_producer_s
403  * \param self a producer
404  * \return the speed as a relative factor (1.0 = normal)
405  */
406
407 double mlt_producer_get_speed( mlt_producer self )
408 {
409         return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( self ), "_speed" );
410 }
411
412 /** Get the frames per second.
413  *
414  * This is determined by the producer's profile.
415  *
416  * \public \memberof mlt_producer_s
417  * \param self a producer
418  * \return the video refresh rate
419  */
420
421 double mlt_producer_get_fps( mlt_producer self )
422 {
423         mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self ) );
424         return mlt_profile_fps( profile );
425 }
426
427 /** Set the in and out points.
428  *
429  * The in point is where play out should start relative to the natural start
430  * of the underlying file. The out point is where play out should end, also
431  * relative to the start of the underlying file. If the underlying resource is
432  * a live stream, then the in point is an offset relative to first usable
433  * sample.
434  *
435  * \public \memberof mlt_producer_s
436  * \param self a producer
437  * \param in the relative starting time; a negative value is the same as 0
438  * \param out the relative ending time; a negative value is the same as length - 1
439  * \return false
440  */
441
442 int mlt_producer_set_in_and_out( mlt_producer self, mlt_position in, mlt_position out )
443 {
444         mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
445
446         // Correct ins and outs if necessary
447         if ( in < 0 )
448                 in = 0;
449         else if ( in >= mlt_producer_get_length( self ) )
450                 in = mlt_producer_get_length( self ) - 1;
451
452         if ( ( out < 0 || out >= mlt_producer_get_length( self ) ) && !mlt_producer_is_blank( self ) )
453                 out = mlt_producer_get_length( self ) - 1;
454         else if ( ( out < 0 || out >= mlt_producer_get_length( self ) ) && mlt_producer_is_blank( self ) )
455                 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( self ), "length", out + 1 );
456         else if ( out < 0 )
457                 out = 0;
458
459         // Swap ins and outs if wrong
460         if ( out < in )
461         {
462                 mlt_position t = in;
463                 in = out;
464                 out = t;
465         }
466
467         // Set the values
468         mlt_events_block( properties, properties );
469         mlt_properties_set_position( properties, "in", in );
470         mlt_events_unblock( properties, properties );
471         mlt_properties_set_position( properties, "out", out );
472
473         return 0;
474 }
475
476 /** Physically reduce the producer (typically a cut) to a 0 length.
477  *  Essentially, all 0 length cuts should be immediately removed by containers.
478  *
479  * \public \memberof mlt_producer_s
480  * \param self a producer
481  * \return false
482  */
483
484 int mlt_producer_clear( mlt_producer self )
485 {
486         if ( self != NULL )
487         {
488                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
489                 mlt_events_block( properties, properties );
490                 mlt_properties_set_position( properties, "in", 0 );
491                 mlt_events_unblock( properties, properties );
492                 mlt_properties_set_position( properties, "out", -1 );
493         }
494         return 0;
495 }
496
497 /** Get the in point.
498  *
499  * \public \memberof mlt_producer_s
500  * \param self a producer
501  * \return the in point
502  */
503
504 mlt_position mlt_producer_get_in( mlt_producer self )
505 {
506         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "in" );
507 }
508
509 /** Get the out point.
510  *
511  * \public \memberof mlt_producer_s
512  * \param self a producer
513  * \return the out point
514  */
515
516 mlt_position mlt_producer_get_out( mlt_producer self )
517 {
518         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "out" );
519 }
520
521 /** Get the total play time.
522  *
523  * \public \memberof mlt_producer_s
524  * \param self a producer
525  * \return the playable (based on in and out points) duration
526  */
527
528 mlt_position mlt_producer_get_playtime( mlt_producer self )
529 {
530         return mlt_producer_get_out( self ) - mlt_producer_get_in( self ) + 1;
531 }
532
533 /** Get the total, unedited length of the producer.
534  *
535  * The value returned by a live streaming producer is unknown.
536  *
537  * \public \memberof mlt_producer_s
538  * \param self a producer
539  * \return the duration of the producer regardless of in and out points
540  */
541
542 mlt_position mlt_producer_get_length( mlt_producer self )
543 {
544         return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "length" );
545 }
546
547 /** Get the total, unedited length of the producer as a time string.
548  *
549  * The value returned by a live streaming producer is unknown.
550  *
551  * \public \memberof mlt_producer_s
552  * \param self a producer
553  * \param format the time value format
554  * \return the duration of the producer regardless of in and out points
555  */
556
557 char* mlt_producer_get_length_time( mlt_producer self, mlt_time_format format )
558 {
559     return mlt_properties_get_time( MLT_PRODUCER_PROPERTIES( self ), "length", format );
560 }
561
562 /** Prepare for next frame.
563  *
564  * Advance the play out position. If the speed is less than zero, it will
565  * move the play out position in the reverse direction.
566  *
567  * \public \memberof mlt_producer_s
568  * \param self a producer
569  */
570
571 void mlt_producer_prepare_next( mlt_producer self )
572 {
573         if ( mlt_producer_get_speed( self ) != 0 )
574                 mlt_producer_seek( self, mlt_producer_position( self ) + mlt_producer_get_speed( self ) );
575 }
576
577 /** Get a frame.
578  *
579  * This is the implementation of the \p get_frame virtual function.
580  * It requests a new frame object from the actual producer for the current
581  * play out position. The producer and its filters can add information and
582  * operations to the frame object in their get_frame handlers.
583  *
584  * \private \memberof mlt_producer_s
585  * \param service a service
586  * \param[out] frame a frame by reference
587  * \param index as determined by the actual producer
588  * \return true if there was an error
589  * \todo Learn more about the details and document how certain properties affect
590  * its behavior.
591  */
592
593 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
594 {
595         int result = 1;
596         mlt_producer self = service != NULL ? service->child : NULL;
597
598         if ( self != NULL && !mlt_producer_is_cut( self ) )
599         {
600                 // Get the properties of this producer
601                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
602
603                 // Determine eof handling
604                 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( self ), "eof" );
605
606                 // Get the speed of the producer
607                 double speed = mlt_producer_get_speed( self );
608
609                 // We need to use the clone if it's specified
610                 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
611
612                 // If no clone is specified, use self
613                 clone = clone == NULL ? self : clone;
614
615                 // A properly instatiated producer will have a get_frame method...
616                 if ( self->get_frame == NULL || ( eof && !strcmp( eof, "continue" ) && mlt_producer_position( self ) > mlt_producer_get_out( self ) ) )
617                 {
618                         // Generate a test frame
619                         *frame = mlt_frame_init( service );
620
621                         // Set the position
622                         result = mlt_frame_set_position( *frame, mlt_producer_position( self ) );
623
624                         // Mark as a test card
625                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 1 );
626                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", 1 );
627
628                         // Calculate the next position
629                         mlt_producer_prepare_next( self );
630                 }
631                 else
632                 {
633                         // Get the frame from the implementation
634                         result = self->get_frame( clone, frame, index );
635                 }
636
637                 // Copy the fps and speed of the producer onto the frame
638                 properties = MLT_FRAME_PROPERTIES( *frame );
639                 mlt_properties_set_double( properties, "_speed", speed );
640                 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
641                 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
642                 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
643                         mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
644         }
645         else if ( self != NULL )
646         {
647                 // Get the speed of the cut
648                 double speed = mlt_producer_get_speed( self );
649
650                 // Get the parent of the cut
651                 mlt_producer parent = mlt_producer_cut_parent( self );
652
653                 // Get the properties of the parent
654                 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
655
656                 // Get the properties of the cut
657                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
658
659                 // Determine the clone index
660                 int clone_index = mlt_properties_get_int( properties, "_clone" );
661
662                 // Determine the clone to use
663                 mlt_producer clone = self;
664
665                 if ( clone_index > 0 )
666                 {
667                         char key[ 25 ];
668                         sprintf( key, "_clone.%d", clone_index - 1 );
669                         clone = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( self ) ), key, NULL );
670                         if ( clone == NULL ) mlt_log( service, MLT_LOG_ERROR, "requested clone doesn't exist %d\n", clone_index );
671                         clone = clone == NULL ? self : clone;
672                 }
673                 else
674                 {
675                         clone = parent;
676                 }
677
678                 // We need to seek to the correct position in the clone
679                 mlt_producer_seek( clone, mlt_producer_get_in( self ) + mlt_properties_get_int( properties, "_position" ) );
680
681                 // Assign the clone property to the parent
682                 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
683
684                 // Now get the frame from the parents service
685                 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
686
687                 // We're done with the clone now
688                 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
689
690                 // This is useful and required by always_active transitions to determine in/out points of the cut
691                 if ( mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", NULL ) == MLT_PRODUCER_SERVICE( parent ) )
692                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", self, 0, NULL, NULL );
693
694                 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
695                 mlt_producer_prepare_next( self );
696         }
697         else
698         {
699                 *frame = mlt_frame_init( service );
700                 result = 0;
701         }
702
703         // Pass on all meta properties from the producer/cut on to the frame
704         if ( *frame != NULL && self != NULL )
705         {
706                 int i = 0;
707                 mlt_properties p_props = MLT_PRODUCER_PROPERTIES( self );
708                 mlt_properties f_props = MLT_FRAME_PROPERTIES( *frame );
709                 mlt_properties_lock( p_props );
710                 int count = mlt_properties_count( p_props );
711                 for ( i = 0; i < count; i ++ )
712                 {
713                         char *name = mlt_properties_get_name( p_props, i );
714                         if ( !strncmp( name, "meta.", 5 ) )
715                                 mlt_properties_set( f_props, name, mlt_properties_get_value( p_props, i ) );
716                         else if ( !strncmp( name, "set.", 4 ) )
717                                 mlt_properties_set( f_props, name + 4, mlt_properties_get_value( p_props, i ) );
718                 }
719                 mlt_properties_unlock( p_props );
720         }
721
722         return result;
723 }
724
725 /** Attach a filter.
726  *
727  * \public \memberof mlt_producer_s
728  * \param self a producer
729  * \param filter the filter to attach
730  * \return true if there was an error
731  */
732
733 int mlt_producer_attach( mlt_producer self, mlt_filter filter )
734 {
735         return mlt_service_attach( MLT_PRODUCER_SERVICE( self ), filter );
736 }
737
738 /** Detach a filter.
739  *
740  * \public \memberof mlt_producer_s
741  * \param self a service
742  * \param filter the filter to detach
743  * \return true if there was an error
744  */
745
746 int mlt_producer_detach( mlt_producer self, mlt_filter filter )
747 {
748         return mlt_service_detach( MLT_PRODUCER_SERVICE( self ), filter );
749 }
750
751 /** Retrieve a filter.
752  *
753  * \public \memberof mlt_producer_s
754  * \param self a service
755  * \param index which filter to retrieve
756  * \return the filter or null if there was an error
757  */
758
759 mlt_filter mlt_producer_filter( mlt_producer self, int index )
760 {
761         return mlt_service_filter( MLT_PRODUCER_SERVICE( self ), index );
762 }
763
764 /** Clone a producer.
765  *
766  * \private \memberof mlt_producer_s
767  * \param self a producer
768  * \return a new producer that is a copy of \p self
769  * \see mlt_producer_set_clones
770  */
771
772 static mlt_producer mlt_producer_clone( mlt_producer self )
773 {
774         mlt_producer clone = NULL;
775         mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
776         char *resource = mlt_properties_get( properties, "resource" );
777         char *service = mlt_properties_get( properties, "mlt_service" );
778         mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self ) );
779
780         mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
781
782         if ( service != NULL )
783                 clone = mlt_factory_producer( profile, service, resource );
784
785         if ( clone == NULL && resource != NULL )
786                 clone = mlt_factory_producer( profile, NULL, resource );
787
788         if ( clone != NULL )
789                 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
790
791         mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
792
793         return clone;
794 }
795
796 /** Create clones.
797  *
798  * \private \memberof mlt_producer_s
799  * \param self a producer
800  * \param clones the number of copies to make
801  * \see mlt_producer_optimise
802  */
803
804 static void mlt_producer_set_clones( mlt_producer self, int clones )
805 {
806         mlt_producer parent = mlt_producer_cut_parent( self );
807         mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
808         int existing = mlt_properties_get_int( properties, "_clones" );
809         int i = 0;
810         char key[ 25 ];
811
812         // If the number of existing clones is different, then create/remove as necessary
813         if ( existing != clones )
814         {
815                 if ( existing < clones )
816                 {
817                         for ( i = existing; i < clones; i ++ )
818                         {
819                                 mlt_producer clone = mlt_producer_clone( parent );
820                                 sprintf( key, "_clone.%d", i );
821                                 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
822                         }
823                 }
824                 else
825                 {
826                         for ( i = clones; i < existing; i ++ )
827                         {
828                                 sprintf( key, "_clone.%d", i );
829                                 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
830                         }
831                 }
832         }
833
834         // Ensure all properties on the parent are passed to the clones
835         for ( i = 0; i < clones; i ++ )
836         {
837                 mlt_producer clone = NULL;
838                 sprintf( key, "_clone.%d", i );
839                 clone = mlt_properties_get_data( properties, key, NULL );
840                 if ( clone != NULL )
841                         mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
842         }
843
844         // Update the number of clones on the properties
845         mlt_properties_set_int( properties, "_clones", clones );
846 }
847
848 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
849
850 typedef struct
851 {
852         int multitrack;
853         int track;
854         int position;
855         int length;
856         int offset;
857 }
858 track_info;
859
860 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
861
862 typedef struct
863 {
864         mlt_producer cut;
865         int start;
866         int end;
867 }
868 clip_references;
869
870 static int intersect( clip_references *a, clip_references *b )
871 {
872         int diff = ( a->start - b->start ) + ( a->end - b->end );
873         return diff >= 0 && diff < ( a->end - a->start + 1 );
874 }
875
876 static int push( mlt_parser self, int multitrack, int track, int position )
877 {
878         mlt_properties properties = mlt_parser_properties( self );
879         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
880         track_info *info = malloc( sizeof( track_info ) );
881         info->multitrack = multitrack;
882         info->track = track;
883         info->position = position;
884         info->length = 0;
885         info->offset = 0;
886         return mlt_deque_push_back( stack, info );
887 }
888
889 static track_info *pop( mlt_parser self )
890 {
891         mlt_properties properties = mlt_parser_properties( self );
892         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
893         return mlt_deque_pop_back( stack );
894 }
895
896 static track_info *peek( mlt_parser self )
897 {
898         mlt_properties properties = mlt_parser_properties( self );
899         mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
900         return mlt_deque_peek_back( stack );
901 }
902
903 static int on_start_multitrack( mlt_parser self, mlt_multitrack object )
904 {
905         track_info *info = peek( self );
906         return push( self, info->multitrack ++, info->track, info->position );
907 }
908
909 static int on_start_track( mlt_parser self )
910 {
911         track_info *info = peek( self );
912         info->position -= info->offset;
913         info->length -= info->offset;
914         return push( self, info->multitrack, info->track ++, info->position );
915 }
916
917 static int on_start_producer( mlt_parser self, mlt_producer object )
918 {
919         mlt_properties properties = mlt_parser_properties( self );
920         mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
921         mlt_producer parent = mlt_producer_cut_parent( object );
922         if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
923         {
924                 int ref_count = 0;
925                 clip_references *old_refs = NULL;
926                 clip_references *refs = NULL;
927                 char key[ 50 ];
928                 int count = 0;
929                 track_info *info = peek( self );
930                 sprintf( key, "%p", parent );
931                 mlt_properties_get_data( producers, key, &count );
932                 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
933                 old_refs = mlt_properties_get_data( properties, key, &ref_count );
934                 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
935                 if ( old_refs != NULL )
936                         memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
937                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object ), "_clone", -1 );
938                 refs[ ref_count ].cut = object;
939                 refs[ ref_count ].start = info->position;
940                 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
941                 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
942                 info->position += mlt_producer_get_playtime( object );
943                 info->length += mlt_producer_get_playtime( object );
944         }
945         return 0;
946 }
947
948 static int on_end_track( mlt_parser self )
949 {
950         track_info *track = pop( self );
951         track_info *multi = peek( self );
952         multi->length += track->length;
953         multi->position += track->length;
954         multi->offset = track->length;
955         free( track );
956         return 0;
957 }
958
959 static int on_end_multitrack( mlt_parser self, mlt_multitrack object )
960 {
961         track_info *multi = pop( self );
962         track_info *track = peek( self );
963         track->position += multi->length;
964         track->length += multi->length;
965         free( multi );
966         return 0;
967 }
968
969 /** Optimise for overlapping cuts from the same clip.
970  *
971  * \todo learn more about this
972  * \public \memberof mlt_producer_s
973  * \param self a producer
974  * \return true if there was an error
975  */
976
977 int mlt_producer_optimise( mlt_producer self )
978 {
979         int error = 1;
980         mlt_parser parser = mlt_parser_new( );
981         if ( parser != NULL )
982         {
983                 int i = 0, j = 0, k = 0;
984                 mlt_properties properties = mlt_parser_properties( parser );
985                 mlt_properties producers = mlt_properties_new( );
986                 mlt_deque stack = mlt_deque_init( );
987                 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
988                 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
989                 parser->on_start_producer = on_start_producer;
990                 parser->on_start_track = on_start_track;
991                 parser->on_end_track = on_end_track;
992                 parser->on_start_multitrack = on_start_multitrack;
993                 parser->on_end_multitrack = on_end_multitrack;
994                 push( parser, 0, 0, 0 );
995                 mlt_parser_start( parser, MLT_PRODUCER_SERVICE( self ) );
996                 free( pop( parser ) );
997                 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
998                 {
999                         char *name = mlt_properties_get_name( producers, k );
1000                         int count = 0;
1001                         int clones = 0;
1002                         int max_clones = 0;
1003                         mlt_producer producer = mlt_properties_get_data_at( producers, k, &count );
1004                         if ( producer != NULL && count > 1 )
1005                         {
1006                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
1007                                 for ( i = 0; i < count; i ++ )
1008                                 {
1009                                         clones = 0;
1010                                         for ( j = i + 1; j < count; j ++ )
1011                                         {
1012                                                 if ( intersect( &refs[ i ], &refs[ j ] ) )
1013                                                 {
1014                                                         clones ++;
1015                                                         mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
1016                                                 }
1017                                         }
1018                                         if ( clones > max_clones )
1019                                                 max_clones = clones;
1020                                 }
1021
1022                                 for ( i = 0; i < count; i ++ )
1023                                 {
1024                                         mlt_producer cut = refs[ i ].cut;
1025                                         if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone" ) == -1 )
1026                                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
1027                                 }
1028
1029                                 mlt_producer_set_clones( producer, max_clones );
1030                         }
1031                         else if ( producer != NULL )
1032                         {
1033                                 clip_references *refs = mlt_properties_get_data( properties, name, &count );
1034                                 for ( i = 0; i < count; i ++ )
1035                                 {
1036                                         mlt_producer cut = refs[ i ].cut;
1037                                         mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
1038                                 }
1039                                 mlt_producer_set_clones( producer, 0 );
1040                         }
1041                 }
1042                 mlt_parser_close( parser );
1043         }
1044         return error;
1045 }
1046
1047 /** Close the producer.
1048  *
1049  * Destroys the producer and deallocates its resources managed by its
1050  * properties list. This will call the close virtual function. Therefore, a
1051  * subclass that defines its own close function should set its virtual close
1052  * function to NULL prior to calling this to avoid circular calls.
1053  *
1054  * \public \memberof mlt_producer_s
1055  * \param self a producer
1056  */
1057
1058 void mlt_producer_close( mlt_producer self )
1059 {
1060         if ( self != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( self ) ) <= 0 )
1061         {
1062                 self->parent.close = NULL;
1063
1064                 if ( self->close != NULL )
1065                 {
1066                         self->close( self->close_object );
1067                 }
1068                 else
1069                 {
1070                         int destroy = mlt_producer_is_cut( self );
1071
1072 #if _MLT_PRODUCER_CHECKS_ == 1
1073                         // Show debug info
1074                         mlt_properties_debug( MLT_PRODUCER_PROPERTIES( self ), "Producer closing", stderr );
1075 #endif
1076
1077 #ifdef _MLT_PRODUCER_CHECKS_
1078                         // Show current stats - these should match when the app is closed
1079                         mlt_log( MLT_PRODUCER_SERVICE( self ), MLT_LOG_DEBUG, "Producers created %d, destroyed %d\n", producers_created, ++producers_destroyed );
1080 #endif
1081
1082                         mlt_service_close( &self->parent );
1083
1084                         if ( destroy )
1085                                 free( self );
1086                 }
1087         }
1088 }