+ *frame = mlt_frame_init( service );
+ result = 0;
+ }
+
+ // Pass on all meta properties from the producer/cut on to the frame
+ if ( *frame != NULL && self != NULL )
+ {
+ int i = 0;
+ mlt_properties p_props = MLT_PRODUCER_PROPERTIES( self );
+ mlt_properties f_props = MLT_FRAME_PROPERTIES( *frame );
+ mlt_properties_lock( p_props );
+ int count = mlt_properties_count( p_props );
+ for ( i = 0; i < count; i ++ )
+ {
+ char *name = mlt_properties_get_name( p_props, i );
+ if ( !strncmp( name, "meta.", 5 ) )
+ mlt_properties_set( f_props, name, mlt_properties_get_value( p_props, i ) );
+ else if ( !strncmp( name, "set.", 4 ) )
+ mlt_properties_set( f_props, name + 4, mlt_properties_get_value( p_props, i ) );
+ }
+ mlt_properties_unlock( p_props );
+ }
+
+ return result;
+}
+
+/** Attach a filter.
+ *
+ * \public \memberof mlt_producer_s
+ * \param self a producer
+ * \param filter the filter to attach
+ * \return true if there was an error
+ */
+
+int mlt_producer_attach( mlt_producer self, mlt_filter filter )
+{
+ return mlt_service_attach( MLT_PRODUCER_SERVICE( self ), filter );
+}
+
+/** Detach a filter.
+ *
+ * \public \memberof mlt_producer_s
+ * \param self a service
+ * \param filter the filter to detach
+ * \return true if there was an error
+ */
+
+int mlt_producer_detach( mlt_producer self, mlt_filter filter )
+{
+ return mlt_service_detach( MLT_PRODUCER_SERVICE( self ), filter );
+}
+
+/** Retrieve a filter.
+ *
+ * \public \memberof mlt_producer_s
+ * \param self a service
+ * \param index which filter to retrieve
+ * \return the filter or null if there was an error
+ */
+
+mlt_filter mlt_producer_filter( mlt_producer self, int index )
+{
+ return mlt_service_filter( MLT_PRODUCER_SERVICE( self ), index );
+}
+
+/** Clone a producer.
+ *
+ * \private \memberof mlt_producer_s
+ * \param self a producer
+ * \return a new producer that is a copy of \p self
+ * \see mlt_producer_set_clones
+ */
+
+static mlt_producer mlt_producer_clone( mlt_producer self )
+{
+ mlt_producer clone = NULL;
+ mlt_properties properties = MLT_PRODUCER_PROPERTIES( self );
+ char *resource = mlt_properties_get( properties, "resource" );
+ char *service = mlt_properties_get( properties, "mlt_service" );
+ mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( self ) );
+
+ mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
+
+ if ( service != NULL )
+ clone = mlt_factory_producer( profile, service, resource );
+
+ if ( clone == NULL && resource != NULL )
+ clone = mlt_factory_producer( profile, NULL, resource );
+
+ if ( clone != NULL )
+ mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
+
+ mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
+
+ return clone;
+}
+
+/** Create clones.
+ *
+ * \private \memberof mlt_producer_s
+ * \param self a producer
+ * \param clones the number of copies to make
+ * \see mlt_producer_optimise
+ */
+
+static void mlt_producer_set_clones( mlt_producer self, int clones )
+{
+ mlt_producer parent = mlt_producer_cut_parent( self );
+ mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
+ int existing = mlt_properties_get_int( properties, "_clones" );
+ int i = 0;
+ char key[ 25 ];
+
+ // If the number of existing clones is different, then create/remove as necessary
+ if ( existing != clones )
+ {
+ if ( existing < clones )
+ {
+ for ( i = existing; i < clones; i ++ )
+ {
+ mlt_producer clone = mlt_producer_clone( parent );
+ sprintf( key, "_clone.%d", i );
+ mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
+ }
+ }
+ else
+ {
+ for ( i = clones; i < existing; i ++ )
+ {
+ sprintf( key, "_clone.%d", i );
+ mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
+ }
+ }
+ }
+
+ // Ensure all properties on the parent are passed to the clones
+ for ( i = 0; i < clones; i ++ )
+ {
+ mlt_producer clone = NULL;
+ sprintf( key, "_clone.%d", i );
+ clone = mlt_properties_get_data( properties, key, NULL );
+ if ( clone != NULL )
+ mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
+ }
+
+ // Update the number of clones on the properties
+ mlt_properties_set_int( properties, "_clones", clones );
+}
+
+/** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
+
+typedef struct
+{
+ int multitrack;
+ int track;
+ int position;
+ int length;
+ int offset;
+}
+track_info;
+
+/** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
+
+typedef struct
+{
+ mlt_producer cut;
+ int start;
+ int end;
+}
+clip_references;
+
+static int intersect( clip_references *a, clip_references *b )
+{
+ int diff = ( a->start - b->start ) + ( a->end - b->end );
+ return diff >= 0 && diff < ( a->end - a->start + 1 );
+}
+
+static int push( mlt_parser self, int multitrack, int track, int position )
+{
+ mlt_properties properties = mlt_parser_properties( self );
+ mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
+ track_info *info = malloc( sizeof( track_info ) );
+ info->multitrack = multitrack;
+ info->track = track;
+ info->position = position;
+ info->length = 0;
+ info->offset = 0;
+ return mlt_deque_push_back( stack, info );
+}
+
+static track_info *pop( mlt_parser self )
+{
+ mlt_properties properties = mlt_parser_properties( self );
+ mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
+ return mlt_deque_pop_back( stack );
+}
+
+static track_info *peek( mlt_parser self )
+{
+ mlt_properties properties = mlt_parser_properties( self );
+ mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
+ return mlt_deque_peek_back( stack );
+}
+
+static int on_start_multitrack( mlt_parser self, mlt_multitrack object )
+{
+ track_info *info = peek( self );
+ return push( self, info->multitrack ++, info->track, info->position );
+}
+
+static int on_start_track( mlt_parser self )
+{
+ track_info *info = peek( self );
+ info->position -= info->offset;
+ info->length -= info->offset;
+ return push( self, info->multitrack, info->track ++, info->position );
+}
+
+static int on_start_producer( mlt_parser self, mlt_producer object )
+{
+ mlt_properties properties = mlt_parser_properties( self );
+ mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
+ mlt_producer parent = mlt_producer_cut_parent( object );
+ if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
+ {
+ int ref_count = 0;
+ clip_references *old_refs = NULL;
+ clip_references *refs = NULL;
+ char key[ 50 ];
+ int count = 0;
+ track_info *info = peek( self );
+ sprintf( key, "%p", parent );
+ mlt_properties_get_data( producers, key, &count );
+ mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
+ old_refs = mlt_properties_get_data( properties, key, &ref_count );
+ refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
+ if ( old_refs != NULL )
+ memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
+ mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object ), "_clone", -1 );
+ refs[ ref_count ].cut = object;
+ refs[ ref_count ].start = info->position;
+ refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
+ mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
+ info->position += mlt_producer_get_playtime( object );
+ info->length += mlt_producer_get_playtime( object );