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 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 );
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 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
56 int mlt_producer_init( mlt_producer self, void *child )
58 // Check that we haven't received NULL
59 int error = self == NULL;
61 // Continue if no error
64 #ifdef _MLT_PRODUCER_CHECKS_
68 // Initialise the producer
69 memset( self, 0, sizeof( struct mlt_producer_s ) );
71 // Associate with the child
74 // Initialise the service
75 if ( mlt_service_init( &self->parent, self ) == 0 )
77 // The parent is the service
78 mlt_service parent = &self->parent;
80 // Define the parent close
81 parent->close = ( mlt_destructor )mlt_producer_close;
82 parent->close_object = self;
84 // For convenience, we'll assume the close_object is self
85 self->close_object = self;
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, 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 );
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 self the producer
120 * \param name the property that changed
123 static void mlt_producer_property_changed( mlt_service owner, mlt_producer self, char *name )
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 );
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 self the producer
138 static void mlt_producer_service_changed( mlt_service owner, mlt_producer self )
140 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( self ) ), "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 self = malloc( sizeof( struct mlt_producer_s ) );
154 if ( mlt_producer_init( self, NULL ) == 0 )
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 ) );
163 /** Determine if producer is a cut.
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
171 int mlt_producer_is_cut( mlt_producer self )
173 return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( self ), "_cut" );
176 /** Determine if producer is a mix.
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.
184 int mlt_producer_is_mix( mlt_producer self )
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;
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 self a producer
196 * \return true if \p self is a "blank" producer
197 * \see mlt_playlist_insert_blank
200 int mlt_producer_is_blank( mlt_producer self )
202 return self == NULL || !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( self ) ), "resource" ), "blank" );
205 /** Obtain the parent producer.
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.
212 mlt_producer mlt_producer_cut_parent( mlt_producer self )
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 );
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 self 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 self, int in, int out )
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 );
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( self ) )
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 self a producer
261 * \return the service parent class
262 * \see MLT_PRODUCER_SERVICE
265 mlt_service mlt_producer_service( mlt_producer self )
267 return self != NULL ? &self->parent : NULL;
270 /** Get the producer properties.
272 * \public \memberof mlt_producer_s
273 * \param self a producer
274 * \return the producer's property list
275 * \see MLT_PRODUCER_PROPERTIES
278 mlt_properties mlt_producer_properties( mlt_producer self )
280 return MLT_SERVICE_PROPERTIES( &self->parent );
283 /** Seek to a specified position.
285 * \public \memberof mlt_producer_s
286 * \param self a producer
287 * \param position set the "play head" position of the producer
289 * \todo Document how the properties affect behavior.
290 * \see mlt_producer_seek_time
293 int mlt_producer_seek( mlt_producer self, mlt_position position )
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" );
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 ) );
306 if ( position < 0 || mlt_producer_get_playtime( self ) == 0 )
310 else if ( use_points && ( eof == NULL || !strcmp( eof, "pause" ) ) && position >= mlt_producer_get_playtime( self ) )
312 mlt_producer_set_speed( self, 0 );
313 position = mlt_producer_get_playtime( self ) - 1;
315 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( self ) )
317 position = (int)position % (int)mlt_producer_get_playtime( self );
321 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( self ), "_position", position );
323 // Calculate the absolute frame
324 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( self ), "_frame", use_points * mlt_producer_get_in( self ) + position );
329 /** Seek to a specified time string.
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
335 * \see mlt_producer_seek
338 int mlt_producer_seek_time( mlt_producer self, const char* time )
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 );
345 /** Get the current position (relative to in point).
347 * \public \memberof mlt_producer_s
348 * \param self a producer
349 * \return the position of the "play head" relative to its beginning
352 mlt_position mlt_producer_position( mlt_producer self )
354 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "_position" );
357 /** Get the current position (relative to start of producer).
359 * \public \memberof mlt_producer_s
360 * \param self a producer
361 * \return the position of the "play head" regardless of the in point
364 mlt_position mlt_producer_frame( mlt_producer self )
366 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "_frame" );
369 /** Get the current position (relative to start of producer) as a time string.
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
377 char* mlt_producer_frame_time( mlt_producer self, mlt_time_format format )
379 return mlt_properties_get_time( MLT_PRODUCER_PROPERTIES( self ), "_frame", format );
382 /** Set the playing speed.
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
390 int mlt_producer_set_speed( mlt_producer self, double speed )
392 return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( self ), "_speed", speed );
395 /** Get the playing speed.
397 * \public \memberof mlt_producer_s
398 * \param self a producer
399 * \return the speed as a relative factor (1.0 = normal)
402 double mlt_producer_get_speed( mlt_producer self )
404 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( self ), "_speed" );
407 /** Get the frames per second.
409 * This is determined by the producer's profile.
411 * \public \memberof mlt_producer_s
412 * \param self a producer
413 * \return the video refresh rate
416 double mlt_producer_get_fps( mlt_producer self )
418 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self ) );
419 return mlt_profile_fps( profile );
422 /** Set the in and out points.
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
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
437 int mlt_producer_set_in_and_out( mlt_producer self, mlt_position in, mlt_position out )
439 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
441 // Correct ins and outs if necessary
444 else if ( in >= mlt_producer_get_length( self ) )
445 in = mlt_producer_get_length( self ) - 1;
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 );
454 // Swap ins and outs if wrong
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 );
471 /** Physically reduce the producer (typically a cut) to a 0 length.
472 * Essentially, all 0 length cuts should be immediately removed by containers.
474 * \public \memberof mlt_producer_s
475 * \param self a producer
479 int mlt_producer_clear( mlt_producer self )
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 );
492 /** Get the in point.
494 * \public \memberof mlt_producer_s
495 * \param self a producer
496 * \return the in point
499 mlt_position mlt_producer_get_in( mlt_producer self )
501 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "in" );
504 /** Get the out point.
506 * \public \memberof mlt_producer_s
507 * \param self a producer
508 * \return the out point
511 mlt_position mlt_producer_get_out( mlt_producer self )
513 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "out" );
516 /** Get the total play time.
518 * \public \memberof mlt_producer_s
519 * \param self a producer
520 * \return the playable (based on in and out points) duration
523 mlt_position mlt_producer_get_playtime( mlt_producer self )
525 return mlt_producer_get_out( self ) - mlt_producer_get_in( self ) + 1;
528 /** Get the total, unedited length of the producer.
530 * The value returned by a live streaming producer is unknown.
532 * \public \memberof mlt_producer_s
533 * \param self a producer
534 * \return the duration of the producer regardless of in and out points
537 mlt_position mlt_producer_get_length( mlt_producer self )
539 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( self ), "length" );
542 /** Get the total, unedited length of the producer as a time string.
544 * The value returned by a live streaming producer is unknown.
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
552 char* mlt_producer_get_length_time( mlt_producer self, mlt_time_format format )
554 return mlt_properties_get_time( MLT_PRODUCER_PROPERTIES( self ), "length", format );
557 /** Prepare for next frame.
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.
562 * \public \memberof mlt_producer_s
563 * \param self a producer
566 void mlt_producer_prepare_next( mlt_producer self )
568 if ( mlt_producer_get_speed( self ) != 0 )
569 mlt_producer_seek( self, mlt_producer_position( self ) + mlt_producer_get_speed( self ) );
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.
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
588 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
591 mlt_producer self = service != NULL ? service->child : NULL;
593 if ( self != NULL && !mlt_producer_is_cut( self ) )
595 // Get the properties of this producer
596 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
598 // Determine eof handling
599 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( self ), "eof" );
601 // Get the speed of the producer
602 double speed = mlt_producer_get_speed( self );
604 // We need to use the clone if it's specified
605 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
607 // If no clone is specified, use self
608 clone = clone == NULL ? self : clone;
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 ) ) )
613 // Generate a test frame
614 *frame = mlt_frame_init( service );
617 result = mlt_frame_set_position( *frame, mlt_producer_position( self ) );
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 );
623 // Calculate the next position
624 mlt_producer_prepare_next( self );
628 // Get the frame from the implementation
629 result = self->get_frame( clone, frame, index );
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 );
640 else if ( self != NULL )
642 // Get the speed of the cut
643 double speed = mlt_producer_get_speed( self );
645 // Get the parent of the cut
646 mlt_producer parent = mlt_producer_cut_parent( self );
648 // Get the properties of the parent
649 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
651 // Get the properties of the cut
652 mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
654 // Determine the clone index
655 int clone_index = mlt_properties_get_int( properties, "_clone" );
657 // Determine the clone to use
658 mlt_producer clone = self;
660 if ( clone_index > 0 )
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;
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" ) );
676 // Assign the clone property to the parent
677 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
679 // Now get the frame from the parents service
680 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
682 // We're done with the clone now
683 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
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 );
689 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
690 mlt_producer_prepare_next( self );
694 *frame = mlt_frame_init( service );
698 // Pass on all meta properties from the producer/cut on to the frame
699 if ( *frame != NULL && self != NULL )
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 ++ )
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 ) );
714 mlt_properties_unlock( p_props );
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
728 int mlt_producer_attach( mlt_producer self, mlt_filter filter )
730 return mlt_service_attach( MLT_PRODUCER_SERVICE( self ), filter );
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
741 int mlt_producer_detach( mlt_producer self, mlt_filter filter )
743 return mlt_service_detach( MLT_PRODUCER_SERVICE( self ), filter );
746 /** Retrieve a filter.
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
754 mlt_filter mlt_producer_filter( mlt_producer self, int index )
756 return mlt_service_filter( MLT_PRODUCER_SERVICE( self ), index );
759 /** Clone a producer.
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
767 static mlt_producer mlt_producer_clone( mlt_producer self )
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 ) );
775 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
777 if ( service != NULL )
778 clone = mlt_factory_producer( profile, service, resource );
780 if ( clone == NULL && resource != NULL )
781 clone = mlt_factory_producer( profile, NULL, resource );
784 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
786 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
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
799 static void mlt_producer_set_clones( mlt_producer self, int clones )
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" );
807 // If the number of existing clones is different, then create/remove as necessary
808 if ( existing != clones )
810 if ( existing < clones )
812 for ( i = existing; i < clones; i ++ )
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 );
821 for ( i = clones; i < existing; i ++ )
823 sprintf( key, "_clone.%d", i );
824 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
829 // Ensure all properties on the parent are passed to the clones
830 for ( i = 0; i < clones; i ++ )
832 mlt_producer clone = NULL;
833 sprintf( key, "_clone.%d", i );
834 clone = mlt_properties_get_data( properties, key, NULL );
836 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
839 // Update the number of clones on the properties
840 mlt_properties_set_int( properties, "_clones", clones );
843 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
855 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
865 static int intersect( clip_references *a, clip_references *b )
867 int diff = ( a->start - b->start ) + ( a->end - b->end );
868 return diff >= 0 && diff < ( a->end - a->start + 1 );
871 static int push( mlt_parser self, int multitrack, int track, int position )
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;
878 info->position = position;
881 return mlt_deque_push_back( stack, info );
884 static track_info *pop( mlt_parser self )
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 );
891 static track_info *peek( mlt_parser self )
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 );
898 static int on_start_multitrack( mlt_parser self, mlt_multitrack object )
900 track_info *info = peek( self );
901 return push( self, info->multitrack ++, info->track, info->position );
904 static int on_start_track( mlt_parser self )
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 );
912 static int on_start_producer( mlt_parser self, mlt_producer object )
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 ) )
920 clip_references *old_refs = NULL;
921 clip_references *refs = NULL;
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 );
943 static int on_end_track( mlt_parser self )
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;
954 static int on_end_multitrack( mlt_parser self, mlt_multitrack object )
956 track_info *multi = pop( self );
957 track_info *track = peek( self );
958 track->position += multi->length;
959 track->length += multi->length;
964 /** Optimise for overlapping cuts from the same clip.
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
972 int mlt_producer_optimise( mlt_producer self )
975 mlt_parser parser = mlt_parser_new( );
976 if ( parser != NULL )
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 ++ )
994 char *name = mlt_properties_get_name( producers, k );
998 mlt_producer producer = mlt_properties_get_data_at( producers, k, &count );
999 if ( producer != NULL && count > 1 )
1001 clip_references *refs = mlt_properties_get_data( properties, name, &count );
1002 for ( i = 0; i < count; i ++ )
1005 for ( j = i + 1; j < count; j ++ )
1007 if ( intersect( &refs[ i ], &refs[ j ] ) )
1010 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
1013 if ( clones > max_clones )
1014 max_clones = clones;
1017 for ( i = 0; i < count; i ++ )
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 );
1024 mlt_producer_set_clones( producer, max_clones );
1026 else if ( producer != NULL )
1028 clip_references *refs = mlt_properties_get_data( properties, name, &count );
1029 for ( i = 0; i < count; i ++ )
1031 mlt_producer cut = refs[ i ].cut;
1032 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
1034 mlt_producer_set_clones( producer, 0 );
1037 mlt_parser_close( parser );
1042 /** Close the producer.
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.
1049 * \public \memberof mlt_producer_s
1050 * \param self a producer
1053 void mlt_producer_close( mlt_producer self )
1055 if ( self != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( self ) ) <= 0 )
1057 self->parent.close = NULL;
1059 if ( self->close != NULL )
1061 self->close( self->close_object );
1065 int destroy = mlt_producer_is_cut( self );
1067 #if _MLT_PRODUCER_CHECKS_ == 1
1069 mlt_properties_debug( MLT_PRODUCER_PROPERTIES( self ), "Producer closing", stderr );
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 );
1077 mlt_service_close( &self->parent );