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