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