2 * mlt_producer.c -- abstraction for all producer services
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "mlt_producer.h"
23 #include "mlt_factory.h"
24 #include "mlt_frame.h"
25 #include "mlt_parser.h"
31 /** Forward references.
34 static int producer_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
);
35 static void mlt_producer_property_changed( mlt_service owner
, mlt_producer
this, char *name
);
36 static void mlt_producer_service_changed( mlt_service owner
, mlt_producer
this );
38 //#define _MLT_PRODUCER_CHECKS_ 1
40 #ifdef _MLT_PRODUCER_CHECKS_
41 static int producers_created
= 0;
42 static int producers_destroyed
= 0;
48 int mlt_producer_init( mlt_producer
this, void *child
)
50 // Check that we haven't received NULL
51 int error
= this == NULL
;
53 // Continue if no error
56 #ifdef _MLT_PRODUCER_CHECKS_
60 // Initialise the producer
61 memset( this, 0, sizeof( struct mlt_producer_s
) );
63 // Associate with the child
66 // Initialise the service
67 if ( mlt_service_init( &this->parent
, this ) == 0 )
69 // Get the normalisation preference
70 char *normalisation
= mlt_environment( "MLT_NORMALISATION" );
72 // The parent is the service
73 mlt_service parent
= &this->parent
;
75 // Define the parent close
76 parent
->close
= ( mlt_destructor
)mlt_producer_close
;
77 parent
->close_object
= this;
79 // For convenience, we'll assume the close_object is this
80 this->close_object
= this;
82 // Get the properties of the parent
83 mlt_properties properties
= MLT_SERVICE_PROPERTIES( parent
);
85 // Set the default properties
86 mlt_properties_set( properties
, "mlt_type", "mlt_producer" );
87 mlt_properties_set_position( properties
, "_position", 0.0 );
88 mlt_properties_set_double( properties
, "_frame", 0 );
89 if ( normalisation
== NULL
|| strcmp( normalisation
, "NTSC" ) )
91 mlt_properties_set_double( properties
, "fps", 25.0 );
92 mlt_properties_set_int( properties
, "frame_rate_num", 25 );
93 mlt_properties_set_int( properties
, "frame_rate_den", 1 );
94 mlt_properties_set_double( properties
, "aspect_ratio", 59.0 / 54.0 );
98 mlt_properties_set_double( properties
, "fps", 30000.0 / 1001.0 );
99 mlt_properties_set_int( properties
, "frame_rate_num", 30000 );
100 mlt_properties_set_int( properties
, "frame_rate_den", 1001 );
101 mlt_properties_set_double( properties
, "aspect_ratio", 10.0 / 11.0 );
103 mlt_properties_set_double( properties
, "_speed", 1.0 );
104 mlt_properties_set_position( properties
, "in", 0 );
105 mlt_properties_set_position( properties
, "out", 14999 );
106 mlt_properties_set_position( properties
, "length", 15000 );
107 mlt_properties_set( properties
, "eof", "pause" );
108 mlt_properties_set( properties
, "resource", "<producer>" );
110 // Override service get_frame
111 parent
->get_frame
= producer_get_frame
;
113 mlt_events_listen( properties
, this, "service-changed", ( mlt_listener
)mlt_producer_service_changed
);
114 mlt_events_listen( properties
, this, "property-changed", ( mlt_listener
)mlt_producer_property_changed
);
115 mlt_events_register( properties
, "producer-changed", NULL
);
122 /** Listener for property changes.
125 static void mlt_producer_property_changed( mlt_service owner
, mlt_producer
this, char *name
)
127 if ( !strcmp( name
, "in" ) || !strcmp( name
, "out" ) || !strcmp( name
, "length" ) )
128 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL
);
131 /** Listener for service changes.
134 static void mlt_producer_service_changed( mlt_service owner
, mlt_producer
this )
136 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL
);
139 /** Create a new producer.
142 mlt_producer
mlt_producer_new( )
144 mlt_producer
this = malloc( sizeof( struct mlt_producer_s
) );
145 mlt_producer_init( this, NULL
);
149 /** Determine if producer is a cut.
152 int mlt_producer_is_cut( mlt_producer
this )
154 return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "_cut" );
157 /** Determine if producer is a mix.
160 int mlt_producer_is_mix( mlt_producer
this )
162 mlt_properties properties
= this != NULL ?
MLT_PRODUCER_PROPERTIES( this ) : NULL
;
163 mlt_tractor tractor
= properties
!= NULL ?
mlt_properties_get_data( properties
, "mlt_mix", NULL
) : NULL
;
164 return tractor
!= NULL
;
167 /** Determine if the producer is a blank [from a playlist].
170 int mlt_producer_is_blank( mlt_producer
this )
172 return this == NULL
|| !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
175 /** Obtain the parent producer.
178 mlt_producer
mlt_producer_cut_parent( mlt_producer
this )
180 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
181 if ( mlt_producer_is_cut( this ) )
182 return mlt_properties_get_data( properties
, "_cut_parent", NULL
);
187 /** Create a cut of this producer
190 mlt_producer
mlt_producer_cut( mlt_producer
this, int in
, int out
)
192 mlt_producer result
= mlt_producer_new( );
193 mlt_producer parent
= mlt_producer_cut_parent( this );
194 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( result
);
195 mlt_properties parent_props
= MLT_PRODUCER_PROPERTIES( parent
);
197 mlt_events_block( MLT_PRODUCER_PROPERTIES( result
), MLT_PRODUCER_PROPERTIES( result
) );
198 // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
201 if ( ( out
< 0 || out
>= mlt_producer_get_length( parent
) ) && !mlt_producer_is_blank( this ) )
202 out
= mlt_producer_get_length( parent
) - 1;
204 mlt_properties_inc_ref( parent_props
);
205 mlt_properties_set_int( properties
, "_cut", 1 );
206 mlt_properties_set_data( properties
, "_cut_parent", parent
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
207 mlt_properties_set_position( properties
, "length", mlt_properties_get_position( parent_props
, "length" ) );
208 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( parent_props
, "aspect_ratio" ) );
209 mlt_producer_set_in_and_out( result
, in
, out
);
214 /** Get the parent service object.
217 mlt_service
mlt_producer_service( mlt_producer
this )
219 return this != NULL ?
&this->parent
: NULL
;
222 /** Get the producer properties.
225 mlt_properties
mlt_producer_properties( mlt_producer
this )
227 return MLT_SERVICE_PROPERTIES( &this->parent
);
230 /** Seek to a specified position.
233 int mlt_producer_seek( mlt_producer
this, mlt_position position
)
235 // Determine eof handling
236 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
237 char *eof
= mlt_properties_get( properties
, "eof" );
238 int use_points
= 1 - mlt_properties_get_int( properties
, "ignore_points" );
240 // Recursive behaviour for cuts - repositions parent and then repositions cut
241 // hence no return on this condition
242 if ( mlt_producer_is_cut( this ) )
243 mlt_producer_seek( mlt_producer_cut_parent( this ), position
+ mlt_producer_get_in( this ) );
246 if ( position
< 0 || mlt_producer_get_playtime( this ) == 0 )
250 else if ( use_points
&& ( eof
== NULL
|| !strcmp( eof
, "pause" ) ) && position
>= mlt_producer_get_playtime( this ) )
252 mlt_producer_set_speed( this, 0 );
253 position
= mlt_producer_get_playtime( this ) - 1;
255 else if ( use_points
&& !strcmp( eof
, "loop" ) && position
>= mlt_producer_get_playtime( this ) )
257 position
= (int)position
% (int)mlt_producer_get_playtime( this );
261 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_position", position
);
263 // Calculate the absolute frame
264 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_frame", use_points
* mlt_producer_get_in( this ) + position
);
269 /** Get the current position (relative to in point).
272 mlt_position
mlt_producer_position( mlt_producer
this )
274 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_position" );
277 /** Get the current position (relative to start of producer).
280 mlt_position
mlt_producer_frame( mlt_producer
this )
282 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_frame" );
285 /** Set the playing speed.
288 int mlt_producer_set_speed( mlt_producer
this, double speed
)
290 return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( this ), "_speed", speed
);
293 /** Get the playing speed.
296 double mlt_producer_get_speed( mlt_producer
this )
298 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "_speed" );
301 /** Get the frames per second.
304 double mlt_producer_get_fps( mlt_producer
this )
306 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "fps" );
309 /** Set the in and out points.
312 int mlt_producer_set_in_and_out( mlt_producer
this, mlt_position in
, mlt_position out
)
314 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
316 // Correct ins and outs if necessary
319 else if ( in
>= mlt_producer_get_length( this ) )
320 in
= mlt_producer_get_length( this ) - 1;
324 else if ( out
>= mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
325 out
= mlt_producer_get_length( this ) - 1;
326 else if ( out
>= mlt_producer_get_length( this ) && mlt_producer_is_blank( this ) )
327 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "length", out
+ 1 );
329 // Swap ins and outs if wrong
338 mlt_events_block( properties
, properties
);
339 mlt_properties_set_position( properties
, "in", in
);
340 mlt_events_unblock( properties
, properties
);
341 mlt_properties_set_position( properties
, "out", out
);
346 /** Physically reduce the producer (typically a cut) to a 0 length.
347 Essentially, all 0 length cuts should be immediately removed by containers.
350 int mlt_producer_clear( mlt_producer
this )
354 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
355 mlt_events_block( properties
, properties
);
356 mlt_properties_set_position( properties
, "in", 0 );
357 mlt_events_unblock( properties
, properties
);
358 mlt_properties_set_position( properties
, "out", -1 );
363 /** Get the in point.
366 mlt_position
mlt_producer_get_in( mlt_producer
this )
368 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "in" );
371 /** Get the out point.
374 mlt_position
mlt_producer_get_out( mlt_producer
this )
376 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "out" );
379 /** Get the total play time.
382 mlt_position
mlt_producer_get_playtime( mlt_producer
this )
384 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
387 /** Get the total length of the producer.
390 mlt_position
mlt_producer_get_length( mlt_producer
this )
392 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "length" );
395 /** Prepare for next frame.
398 void mlt_producer_prepare_next( mlt_producer
this )
400 if ( mlt_producer_get_speed( this ) != 0 )
401 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
407 static int producer_get_frame( mlt_service service
, mlt_frame_ptr frame
, int index
)
410 mlt_producer
this = service
!= NULL ? service
->child
: NULL
;
412 if ( this != NULL
&& !mlt_producer_is_cut( this ) )
414 // Get the properties of this producer
415 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
417 // Determine eof handling
418 char *eof
= mlt_properties_get( MLT_PRODUCER_PROPERTIES( this ), "eof" );
420 // Get the speed of the producer
421 double speed
= mlt_producer_get_speed( this );
423 // We need to use the clone if it's specified
424 mlt_producer clone
= mlt_properties_get_data( properties
, "use_clone", NULL
);
426 // If no clone is specified, use this
427 clone
= clone
== NULL ?
this : clone
;
429 // A properly instatiated producer will have a get_frame method...
430 if ( this->get_frame
== NULL
|| ( !strcmp( eof
, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
432 // Generate a test frame
433 *frame
= mlt_frame_init( );
436 result
= mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
438 // Mark as a test card
439 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame
), "test_image", 1 );
440 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame
), "test_audio", 1 );
442 // Calculate the next position
443 mlt_producer_prepare_next( this );
447 // Get the frame from the implementation
448 result
= this->get_frame( clone
, frame
, index
);
451 // Copy the fps and speed of the producer onto the frame
452 properties
= MLT_FRAME_PROPERTIES( *frame
);
453 mlt_properties_set_double( properties
, "_speed", speed
);
454 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( this ) );
455 mlt_properties_set_int( properties
, "test_audio", mlt_frame_is_test_audio( *frame
) );
456 mlt_properties_set_int( properties
, "test_image", mlt_frame_is_test_card( *frame
) );
457 if ( mlt_properties_get_data( properties
, "_producer", NULL
) == NULL
)
458 mlt_properties_set_data( properties
, "_producer", service
, 0, NULL
, NULL
);
460 else if ( this != NULL
)
462 // Get the speed of the cut
463 double speed
= mlt_producer_get_speed( this );
465 // Get the parent of this cut
466 mlt_producer parent
= mlt_producer_cut_parent( this );
468 // Get the properties of the parent
469 mlt_properties parent_properties
= MLT_PRODUCER_PROPERTIES( parent
);
471 // Get the properties of the cut
472 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
474 // Determine the clone index
475 int clone_index
= mlt_properties_get_int( properties
, "_clone" );
477 // Determine the clone to use
478 mlt_producer clone
= this;
480 if ( clone_index
> 0 )
483 sprintf( key
, "_clone.%d", clone_index
- 1 );
484 clone
= mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), key
, NULL
);
485 if ( clone
== NULL
) fprintf( stderr
, "requested clone doesn't exist %d\n", clone_index
);
486 clone
= clone
== NULL ?
this : clone
;
493 // We need to seek to the correct position in the clone
494 mlt_producer_seek( clone
, mlt_producer_get_in( this ) + mlt_properties_get_int( properties
, "_position" ) );
496 // Assign the clone property to the parent
497 mlt_properties_set_data( parent_properties
, "use_clone", clone
, 0, NULL
, NULL
);
499 // Now get the frame from the parents service
500 result
= mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent
), frame
, index
);
502 // We're done with the clone now
503 mlt_properties_set_data( parent_properties
, "use_clone", NULL
, 0, NULL
, NULL
);
505 // This is useful and required by always_active transitions to determine in/out points of the cut
506 if ( mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame
), "_producer", NULL
) == MLT_PRODUCER_SERVICE( parent
) )
507 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame
), "_producer", this, 0, NULL
, NULL
);
509 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "_speed", speed
);
510 mlt_producer_prepare_next( this );
514 *frame
= mlt_frame_init( );
518 // Pass on all meta properties from the producer/cut on to the frame
519 if ( *frame
!= NULL
&& this != NULL
)
522 mlt_properties p_props
= MLT_PRODUCER_PROPERTIES( this );
523 mlt_properties f_props
= MLT_FRAME_PROPERTIES( *frame
);
524 int count
= mlt_properties_count( p_props
);
525 for ( i
= 0; i
< count
; i
++ )
527 char *name
= mlt_properties_get_name( p_props
, i
);
528 if ( !strncmp( name
, "meta.", 5 ) )
529 mlt_properties_set( f_props
, name
, mlt_properties_get( p_props
, name
) );
530 else if ( !strncmp( name
, "set.", 4 ) )
531 mlt_properties_set( f_props
, name
+ 4, mlt_properties_get( p_props
, name
) );
541 int mlt_producer_attach( mlt_producer
this, mlt_filter filter
)
543 return mlt_service_attach( MLT_PRODUCER_SERVICE( this ), filter
);
549 int mlt_producer_detach( mlt_producer
this, mlt_filter filter
)
551 return mlt_service_detach( MLT_PRODUCER_SERVICE( this ), filter
);
554 /** Retrieve a filter.
557 mlt_filter
mlt_producer_filter( mlt_producer
this, int index
)
559 return mlt_service_filter( MLT_PRODUCER_SERVICE( this ), index
);
562 /** Clone this producer.
565 static mlt_producer
mlt_producer_clone( mlt_producer
this )
567 mlt_producer clone
= NULL
;
568 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
569 char *resource
= mlt_properties_get( properties
, "resource" );
570 char *service
= mlt_properties_get( properties
, "mlt_service" );
572 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
574 if ( service
!= NULL
)
575 clone
= mlt_factory_producer( service
, resource
);
577 if ( clone
== NULL
&& resource
!= NULL
)
578 clone
= mlt_factory_producer( "fezzik", resource
);
581 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone
), properties
);
583 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
591 static void mlt_producer_set_clones( mlt_producer
this, int clones
)
593 mlt_producer parent
= mlt_producer_cut_parent( this );
594 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( parent
);
595 int existing
= mlt_properties_get_int( properties
, "_clones" );
599 // If the number of existing clones is different, then create/remove as necessary
600 if ( existing
!= clones
)
602 if ( existing
< clones
)
604 for ( i
= existing
; i
< clones
; i
++ )
606 mlt_producer clone
= mlt_producer_clone( parent
);
607 sprintf( key
, "_clone.%d", i
);
608 mlt_properties_set_data( properties
, key
, clone
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
613 for ( i
= clones
; i
< existing
; i
++ )
615 sprintf( key
, "_clone.%d", i
);
616 mlt_properties_set_data( properties
, key
, NULL
, 0, NULL
, NULL
);
621 // Ensure all properties on the parent are passed to the clones
622 for ( i
= 0; i
< clones
; i
++ )
624 mlt_producer clone
= NULL
;
625 sprintf( key
, "_clone.%d", i
);
626 clone
= mlt_properties_get_data( properties
, key
, NULL
);
628 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone
), properties
, "" );
631 // Update the number of clones on the properties
632 mlt_properties_set_int( properties
, "_clones", clones
);
635 /** Optimise for overlapping cuts from the same clip.
656 static int intersect( clip_references
*a
, clip_references
*b
)
658 int diff
= ( a
->start
- b
->start
) + ( a
->end
- b
->end
);
659 return diff
>= 0 && diff
< ( a
->end
- a
->start
+ 1 );
662 static int push( mlt_parser
this, int multitrack
, int track
, int position
)
664 mlt_properties properties
= mlt_parser_properties( this );
665 mlt_deque stack
= mlt_properties_get_data( properties
, "stack", NULL
);
666 track_info
*info
= malloc( sizeof( track_info
) );
667 info
->multitrack
= multitrack
;
669 info
->position
= position
;
672 return mlt_deque_push_back( stack
, info
);
675 static track_info
*pop( mlt_parser
this )
677 mlt_properties properties
= mlt_parser_properties( this );
678 mlt_deque stack
= mlt_properties_get_data( properties
, "stack", NULL
);
679 return mlt_deque_pop_back( stack
);
682 static track_info
*peek( mlt_parser
this )
684 mlt_properties properties
= mlt_parser_properties( this );
685 mlt_deque stack
= mlt_properties_get_data( properties
, "stack", NULL
);
686 return mlt_deque_peek_back( stack
);
689 static int on_start_multitrack( mlt_parser
this, mlt_multitrack object
)
691 track_info
*info
= peek( this );
692 return push( this, info
->multitrack
++, info
->track
, info
->position
);
695 static int on_start_track( mlt_parser
this )
697 track_info
*info
= peek( this );
698 info
->position
-= info
->offset
;
699 info
->length
-= info
->offset
;
700 return push( this, info
->multitrack
, info
->track
++, info
->position
);
703 static int on_start_producer( mlt_parser
this, mlt_producer object
)
705 mlt_properties properties
= mlt_parser_properties( this );
706 mlt_properties producers
= mlt_properties_get_data( properties
, "producers", NULL
);
707 mlt_producer parent
= mlt_producer_cut_parent( object
);
708 if ( mlt_service_identify( ( mlt_service
)mlt_producer_cut_parent( object
) ) == producer_type
&& mlt_producer_is_cut( object
) )
711 clip_references
*old_refs
= NULL
;
712 clip_references
*refs
= NULL
;
715 track_info
*info
= peek( this );
716 sprintf( key
, "%p", parent
);
717 mlt_properties_get_data( producers
, key
, &count
);
718 mlt_properties_set_data( producers
, key
, parent
, ++ count
, NULL
, NULL
);
719 old_refs
= mlt_properties_get_data( properties
, key
, &ref_count
);
720 refs
= malloc( ( ref_count
+ 1 ) * sizeof( clip_references
) );
721 if ( old_refs
!= NULL
)
722 memcpy( refs
, old_refs
, ref_count
* sizeof( clip_references
) );
723 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object
), "_clone", -1 );
724 refs
[ ref_count
].cut
= object
;
725 refs
[ ref_count
].start
= info
->position
;
726 refs
[ ref_count
].end
= info
->position
+ mlt_producer_get_playtime( object
) - 1;
727 mlt_properties_set_data( properties
, key
, refs
, ++ ref_count
, free
, NULL
);
728 info
->position
+= mlt_producer_get_playtime( object
);
729 info
->length
+= mlt_producer_get_playtime( object
);
734 static int on_end_track( mlt_parser
this )
736 track_info
*track
= pop( this );
737 track_info
*multi
= peek( this );
738 multi
->length
+= track
->length
;
739 multi
->position
+= track
->length
;
740 multi
->offset
= track
->length
;
745 static int on_end_multitrack( mlt_parser
this, mlt_multitrack object
)
747 track_info
*multi
= pop( this );
748 track_info
*track
= peek( this );
749 track
->position
+= multi
->length
;
750 track
->length
+= multi
->length
;
755 int mlt_producer_optimise( mlt_producer
this )
758 mlt_parser parser
= mlt_parser_new( );
759 if ( parser
!= NULL
)
761 int i
= 0, j
= 0, k
= 0;
762 mlt_properties properties
= mlt_parser_properties( parser
);
763 mlt_properties producers
= mlt_properties_new( );
764 mlt_deque stack
= mlt_deque_init( );
765 mlt_properties_set_data( properties
, "producers", producers
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
766 mlt_properties_set_data( properties
, "stack", stack
, 0, ( mlt_destructor
)mlt_deque_close
, NULL
);
767 parser
->on_start_producer
= on_start_producer
;
768 parser
->on_start_track
= on_start_track
;
769 parser
->on_end_track
= on_end_track
;
770 parser
->on_start_multitrack
= on_start_multitrack
;
771 parser
->on_end_multitrack
= on_end_multitrack
;
772 push( parser
, 0, 0, 0 );
773 mlt_parser_start( parser
, MLT_PRODUCER_SERVICE( this ) );
774 free( pop( parser
) );
775 for ( k
= 0; k
< mlt_properties_count( producers
); k
++ )
777 char *name
= mlt_properties_get_name( producers
, k
);
781 mlt_producer producer
= mlt_properties_get_data( producers
, name
, &count
);
782 if ( producer
!= NULL
&& count
> 1 )
784 clip_references
*refs
= mlt_properties_get_data( properties
, name
, &count
);
785 for ( i
= 0; i
< count
; i
++ )
788 for ( j
= i
+ 1; j
< count
; j
++ )
790 if ( intersect( &refs
[ i
], &refs
[ j
] ) )
793 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs
[ j
].cut
), "_clone", clones
);
796 if ( clones
> max_clones
)
800 for ( i
= 0; i
< count
; i
++ )
802 mlt_producer cut
= refs
[ i
].cut
;
803 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( cut
), "_clone" ) == -1 )
804 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut
), "_clone", 0 );
807 mlt_producer_set_clones( producer
, max_clones
);
809 else if ( producer
!= NULL
)
811 clip_references
*refs
= mlt_properties_get_data( properties
, name
, &count
);
812 for ( i
= 0; i
< count
; i
++ )
814 mlt_producer cut
= refs
[ i
].cut
;
815 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut
), "_clone", 0 );
817 mlt_producer_set_clones( producer
, 0 );
820 mlt_parser_close( parser
);
825 /** Close the producer.
828 void mlt_producer_close( mlt_producer
this )
830 if ( this != NULL
&& mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( this ) ) <= 0 )
832 this->parent
.close
= NULL
;
834 if ( this->close
!= NULL
)
836 this->close( this->close_object
);
840 int destroy
= mlt_producer_is_cut( this );
842 #if _MLT_PRODUCER_CHECKS_ == 1
844 mlt_properties_debug( MLT_PRODUCER_PROPERTIES( this ), "Producer closing", stderr
);
847 #ifdef _MLT_PRODUCER_CHECKS_
848 // Increment destroyed count
849 producers_destroyed
++;
851 // Show current stats - these should match when the app is closed
852 fprintf( stderr
, "Producers created %d, destroyed %d\n", producers_created
, producers_destroyed
);
855 mlt_service_close( &this->parent
);