3 * \brief abstraction for all producer services
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
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.
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.
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
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"
35 /* Forward references. */
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 );
42 //#define _MLT_PRODUCER_CHECKS_ 1
43 #ifdef _MLT_PRODUCER_CHECKS_
44 static int producers_created = 0;
45 static int producers_destroyed = 0;
48 /** Initialize a producer service.
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
56 int mlt_producer_init( mlt_producer this, void *child )
58 // Check that we haven't received NULL
59 int error = this == NULL;
61 // Continue if no error
64 #ifdef _MLT_PRODUCER_CHECKS_
68 // Initialise the producer
69 memset( this, 0, sizeof( struct mlt_producer_s ) );
71 // Associate with the child
74 // Initialise the service
75 if ( mlt_service_init( &this->parent, this ) == 0 )
77 // The parent is the service
78 mlt_service parent = &this->parent;
80 // Define the parent close
81 parent->close = ( mlt_destructor )mlt_producer_close;
82 parent->close_object = this;
84 // For convenience, we'll assume the close_object is this
85 this->close_object = this;
87 // Get the properties of the parent
88 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
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>" );
101 // Override service get_frame
102 parent->get_frame = producer_get_frame;
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 );
113 /** Listener for property changes.
115 * If the in, out, or length properties changed, fire a "producer-changed" event.
117 * \private \memberof mlt_producer_s
118 * \param owner a service (ignored)
119 * \param this the producer
120 * \param name the property that changed
123 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
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 );
129 /** Listener for service changes.
131 * Fires the "producer-changed" event.
133 * \private \memberof mlt_producer_s
134 * \param owner a service (ignored)
135 * \param this the producer
138 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
140 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
143 /** Create and initialize a new producer.
145 * \public \memberof mlt_producer_s
146 * \return the new producer
149 mlt_producer mlt_producer_new( mlt_profile profile )
151 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
154 if ( mlt_producer_init( this, NULL ) == 0 )
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 ) );
163 /** Determine if producer is a cut.
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
171 int mlt_producer_is_cut( mlt_producer this )
173 return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "_cut" );
176 /** Determine if producer is a mix.
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.
184 int mlt_producer_is_mix( mlt_producer this )
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;
191 /** Determine if the producer is a blank.
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
200 int mlt_producer_is_blank( mlt_producer this )
202 return this == NULL || !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
205 /** Obtain the parent producer.
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.
212 mlt_producer mlt_producer_cut_parent( mlt_producer this )
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 );
221 /** Create a cut of this producer.
223 * A "cut" is a portion of another (parent) producer.
225 * \public \memberof mlt_producer_s
226 * \param this a producer
227 * \param in the beginning
229 * \return the new producer
230 * \todo Expand on the value of a cut.
233 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
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 );
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)
244 if ( ( out < 0 || out >= mlt_producer_get_length( parent ) ) && !mlt_producer_is_blank( this ) )
245 out = mlt_producer_get_length( parent ) - 1;
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 );
257 /** Get the parent service object.
259 * \public \memberof mlt_producer_s
260 * \param this a producer
261 * \return the service parent class
262 * \see MLT_PRODUCER_SERVICE
265 mlt_service mlt_producer_service( mlt_producer this )
267 return this != NULL ? &this->parent : NULL;
270 /** Get the producer properties.
272 * \public \memberof mlt_producer_s
273 * \param this a producer
274 * \return the producer's property list
275 * \see MLT_PRODUCER_PROPERTIES
278 mlt_properties mlt_producer_properties( mlt_producer this )
280 return MLT_SERVICE_PROPERTIES( &this->parent );
283 /** Seek to a specified position.
285 * \public \memberof mlt_producer_s
286 * \param this a producer
287 * \param position set the "play head" position of the producer
289 * \todo Document how the properties affect behavior.
292 int mlt_producer_seek( mlt_producer this, mlt_position position )
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" );
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 ) );
305 if ( position < 0 || mlt_producer_get_playtime( this ) == 0 )
309 else if ( use_points && ( eof == NULL || !strcmp( eof, "pause" ) ) && position >= mlt_producer_get_playtime( this ) )
311 mlt_producer_set_speed( this, 0 );
312 position = mlt_producer_get_playtime( this ) - 1;
314 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
316 position = (int)position % (int)mlt_producer_get_playtime( this );
320 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_position", position );
322 // Calculate the absolute frame
323 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
328 /** Get the current position (relative to in point).
330 * \public \memberof mlt_producer_s
331 * \param this a producer
332 * \return the position of the "play head" relative to its beginning
335 mlt_position mlt_producer_position( mlt_producer this )
337 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_position" );
340 /** Get the current position (relative to start of producer).
342 * \public \memberof mlt_producer_s
343 * \param this a producer
344 * \return the position of the "play head" regardless of the in point
347 mlt_position mlt_producer_frame( mlt_producer this )
349 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_frame" );
352 /** Set the playing speed.
354 * \public \memberof mlt_producer_s
355 * \param this a producer
356 * \param speed the new speed as a relative factor (1.0 = normal)
360 int mlt_producer_set_speed( mlt_producer this, double speed )
362 return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( this ), "_speed", speed );
365 /** Get the playing speed.
367 * \public \memberof mlt_producer_s
368 * \param this a producer
369 * \return the speed as a relative factor (1.0 = normal)
372 double mlt_producer_get_speed( mlt_producer this )
374 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "_speed" );
377 /** Get the frames per second.
379 * This is determined by the producer's profile.
381 * \public \memberof mlt_producer_s
382 * \param this a producer
383 * \return the video refresh rate
386 double mlt_producer_get_fps( mlt_producer this )
388 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
389 return mlt_profile_fps( profile );
392 /** Set the in and out points.
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
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
407 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
409 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
411 // Correct ins and outs if necessary
414 else if ( in >= mlt_producer_get_length( this ) )
415 in = mlt_producer_get_length( this ) - 1;
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 );
424 // Swap ins and outs if wrong
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 );
441 /** Physically reduce the producer (typically a cut) to a 0 length.
442 * Essentially, all 0 length cuts should be immediately removed by containers.
444 * \public \memberof mlt_producer_s
445 * \param this a producer
449 int mlt_producer_clear( mlt_producer this )
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 );
462 /** Get the in point.
464 * \public \memberof mlt_producer_s
465 * \param this a producer
466 * \return the in point
469 mlt_position mlt_producer_get_in( mlt_producer this )
471 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "in" );
474 /** Get the out point.
476 * \public \memberof mlt_producer_s
477 * \param this a producer
478 * \return the out point
481 mlt_position mlt_producer_get_out( mlt_producer this )
483 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "out" );
486 /** Get the total play time.
488 * \public \memberof mlt_producer_s
489 * \param this a producer
490 * \return the playable (based on in and out points) duration
493 mlt_position mlt_producer_get_playtime( mlt_producer this )
495 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
498 /** Get the total, unedited length of the producer.
500 * The value returned by a live streaming producer is unknown.
502 * \public \memberof mlt_producer_s
503 * \param this a producer
504 * \return the duration of the producer regardless of in and out points
507 mlt_position mlt_producer_get_length( mlt_producer this )
509 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "length" );
512 /** Prepare for next frame.
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.
517 * \public \memberof mlt_producer_s
518 * \param this a producer
521 void mlt_producer_prepare_next( mlt_producer this )
523 if ( mlt_producer_get_speed( this ) != 0 )
524 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
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.
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
543 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
546 mlt_producer this = service != NULL ? service->child : NULL;
548 if ( this != NULL && !mlt_producer_is_cut( this ) )
550 // Get the properties of this producer
551 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
553 // Determine eof handling
554 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( this ), "eof" );
556 // Get the speed of the producer
557 double speed = mlt_producer_get_speed( this );
559 // We need to use the clone if it's specified
560 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
562 // If no clone is specified, use this
563 clone = clone == NULL ? this : clone;
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 ) ) )
568 // Generate a test frame
569 *frame = mlt_frame_init( service );
572 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
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 );
578 // Calculate the next position
579 mlt_producer_prepare_next( this );
583 // Get the frame from the implementation
584 result = this->get_frame( clone, frame, index );
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 );
595 else if ( this != NULL )
597 // Get the speed of the cut
598 double speed = mlt_producer_get_speed( this );
600 // Get the parent of this cut
601 mlt_producer parent = mlt_producer_cut_parent( this );
603 // Get the properties of the parent
604 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
606 // Get the properties of the cut
607 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
609 // Determine the clone index
610 int clone_index = mlt_properties_get_int( properties, "_clone" );
612 // Determine the clone to use
613 mlt_producer clone = this;
615 if ( clone_index > 0 )
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;
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" ) );
631 // Assign the clone property to the parent
632 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
634 // Now get the frame from the parents service
635 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
637 // We're done with the clone now
638 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
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 );
644 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
645 mlt_producer_prepare_next( this );
649 *frame = mlt_frame_init( service );
653 // Pass on all meta properties from the producer/cut on to the frame
654 if ( *frame != NULL && this != NULL )
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 ++ )
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 ) );
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
681 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
683 return mlt_service_attach( MLT_PRODUCER_SERVICE( this ), filter );
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
694 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
696 return mlt_service_detach( MLT_PRODUCER_SERVICE( this ), filter );
699 /** Retrieve a filter.
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
707 mlt_filter mlt_producer_filter( mlt_producer this, int index )
709 return mlt_service_filter( MLT_PRODUCER_SERVICE( this ), index );
712 /** Clone this producer.
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
720 static mlt_producer mlt_producer_clone( mlt_producer this )
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 ) );
728 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
730 if ( service != NULL )
731 clone = mlt_factory_producer( profile, service, resource );
733 if ( clone == NULL && resource != NULL )
734 clone = mlt_factory_producer( profile, NULL, resource );
737 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
739 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
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
752 static void mlt_producer_set_clones( mlt_producer this, int clones )
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" );
760 // If the number of existing clones is different, then create/remove as necessary
761 if ( existing != clones )
763 if ( existing < clones )
765 for ( i = existing; i < clones; i ++ )
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 );
774 for ( i = clones; i < existing; i ++ )
776 sprintf( key, "_clone.%d", i );
777 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
782 // Ensure all properties on the parent are passed to the clones
783 for ( i = 0; i < clones; i ++ )
785 mlt_producer clone = NULL;
786 sprintf( key, "_clone.%d", i );
787 clone = mlt_properties_get_data( properties, key, NULL );
789 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
792 // Update the number of clones on the properties
793 mlt_properties_set_int( properties, "_clones", clones );
796 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
808 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
818 static int intersect( clip_references *a, clip_references *b )
820 int diff = ( a->start - b->start ) + ( a->end - b->end );
821 return diff >= 0 && diff < ( a->end - a->start + 1 );
824 static int push( mlt_parser this, int multitrack, int track, int position )
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;
831 info->position = position;
834 return mlt_deque_push_back( stack, info );
837 static track_info *pop( mlt_parser this )
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 );
844 static track_info *peek( mlt_parser this )
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 );
851 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
853 track_info *info = peek( this );
854 return push( this, info->multitrack ++, info->track, info->position );
857 static int on_start_track( mlt_parser this )
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 );
865 static int on_start_producer( mlt_parser this, mlt_producer object )
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 ) )
873 clip_references *old_refs = NULL;
874 clip_references *refs = NULL;
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 );
896 static int on_end_track( mlt_parser this )
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;
907 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
909 track_info *multi = pop( this );
910 track_info *track = peek( this );
911 track->position += multi->length;
912 track->length += multi->length;
917 /** Optimise for overlapping cuts from the same clip.
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
925 int mlt_producer_optimise( mlt_producer this )
928 mlt_parser parser = mlt_parser_new( );
929 if ( parser != NULL )
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 ++ )
947 char *name = mlt_properties_get_name( producers, k );
951 mlt_producer producer = mlt_properties_get_data( producers, name, &count );
952 if ( producer != NULL && count > 1 )
954 clip_references *refs = mlt_properties_get_data( properties, name, &count );
955 for ( i = 0; i < count; i ++ )
958 for ( j = i + 1; j < count; j ++ )
960 if ( intersect( &refs[ i ], &refs[ j ] ) )
963 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
966 if ( clones > max_clones )
970 for ( i = 0; i < count; i ++ )
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 );
977 mlt_producer_set_clones( producer, max_clones );
979 else if ( producer != NULL )
981 clip_references *refs = mlt_properties_get_data( properties, name, &count );
982 for ( i = 0; i < count; i ++ )
984 mlt_producer cut = refs[ i ].cut;
985 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
987 mlt_producer_set_clones( producer, 0 );
990 mlt_parser_close( parser );
995 /** Close the producer.
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.
1002 * \public \memberof mlt_producer_s
1003 * \param this a producer
1006 void mlt_producer_close( mlt_producer this )
1008 if ( this != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( this ) ) <= 0 )
1010 this->parent.close = NULL;
1012 if ( this->close != NULL )
1014 this->close( this->close_object );
1018 int destroy = mlt_producer_is_cut( this );
1020 #if _MLT_PRODUCER_CHECKS_ == 1
1022 mlt_properties_debug( MLT_PRODUCER_PROPERTIES( this ), "Producer closing", stderr );
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 );
1030 mlt_service_close( &this->parent );