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