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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 );
41 int mlt_producer_init( mlt_producer this, void *child )
43 // Check that we haven't received NULL
44 int error = this == NULL;
46 // Continue if no error
49 // Initialise the producer
50 memset( this, 0, sizeof( struct mlt_producer_s ) );
52 // Associate with the child
55 // Initialise the service
56 if ( mlt_service_init( &this->parent, this ) == 0 )
58 // Get the normalisation preference
59 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
61 // The parent is the service
62 mlt_service parent = &this->parent;
64 // Define the parent close
65 parent->close = ( mlt_destructor )mlt_producer_close;
66 parent->close_object = this;
68 // For convenience, we'll assume the close_object is this
69 this->close_object = this;
71 // Get the properties of the parent
72 mlt_properties properties = mlt_service_properties( parent );
74 // Set the default properties
75 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
76 mlt_properties_set_position( properties, "_position", 0.0 );
77 mlt_properties_set_double( properties, "_frame", 0 );
78 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
80 mlt_properties_set_double( properties, "fps", 25.0 );
81 mlt_properties_set_double( properties, "aspect_ratio", 59.0 / 54.0 );
85 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
86 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
88 mlt_properties_set_double( properties, "_speed", 1.0 );
89 mlt_properties_set_position( properties, "in", 0 );
90 mlt_properties_set_position( properties, "out", 14999 );
91 mlt_properties_set_position( properties, "length", 15000 );
92 mlt_properties_set( properties, "eof", "pause" );
93 mlt_properties_set( properties, "resource", "<producer>" );
95 // Override service get_frame
96 parent->get_frame = producer_get_frame;
98 mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
99 mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
100 mlt_events_register( properties, "producer-changed", NULL );
107 /** Listener for property changes.
110 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
112 if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
113 mlt_events_fire( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
116 /** Listener for service changes.
119 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
121 mlt_events_fire( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
124 /** Special case destructor
127 static void mlt_cut_destroy( void *obj )
129 mlt_producer this = obj;
131 this->parent.close = NULL;
132 mlt_service_close( &this->parent );
136 /** Create a new producer.
139 mlt_producer mlt_producer_new( )
141 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
142 mlt_producer_init( this, NULL );
143 this->close = mlt_cut_destroy;
147 /** Determine if producer is a cut.
150 int mlt_producer_is_cut( mlt_producer this )
152 return mlt_properties_get_int( mlt_producer_properties( this ), "_cut" );
155 /** Determine if producer is a mix.
158 int mlt_producer_is_mix( mlt_producer this )
160 mlt_properties properties = this != NULL ? mlt_producer_properties( this ) : NULL;
161 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
162 return tractor != NULL;
165 /** Determine if the producer is a blank [from a playlist].
168 int mlt_producer_is_blank( mlt_producer this )
170 return this == NULL || !strcmp( mlt_properties_get( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
173 /** Obtain the parent producer.
176 mlt_producer mlt_producer_cut_parent( mlt_producer this )
178 mlt_properties properties = mlt_producer_properties( this );
179 if ( mlt_producer_is_cut( this ) )
180 return mlt_properties_get_data( properties, "_cut_parent", NULL );
185 /** Create a cut of this producer
188 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
190 mlt_producer result = mlt_producer_new( );
191 mlt_producer parent = mlt_producer_cut_parent( this );
192 mlt_properties properties = mlt_producer_properties( result );
193 mlt_properties parent_props = mlt_producer_properties( parent );
195 // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
198 if ( ( out < 0 || out >= mlt_producer_get_playtime( parent ) ) && !mlt_producer_is_blank( this ) )
199 out = mlt_producer_get_playtime( parent ) - 1;
201 mlt_properties_inc_ref( parent_props );
202 mlt_properties_set_int( properties, "_cut", 1 );
203 mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
204 mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
205 mlt_producer_set_in_and_out( result, in, out );
210 /** Get the parent service object.
213 mlt_service mlt_producer_service( mlt_producer this )
215 return this != NULL ? &this->parent : NULL;
218 /** Get the producer properties.
221 mlt_properties mlt_producer_properties( mlt_producer this )
223 return mlt_service_properties( &this->parent );
226 /** Seek to a specified position.
229 int mlt_producer_seek( mlt_producer this, mlt_position position )
231 // Determine eof handling
232 mlt_properties properties = mlt_producer_properties( this );
233 char *eof = mlt_properties_get( properties, "eof" );
234 int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
236 // Recursive behaviour for cuts...
237 if ( mlt_producer_is_cut( this ) )
238 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
245 else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
247 mlt_producer_set_speed( this, 0 );
248 position = mlt_producer_get_playtime( this ) - 1;
250 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
252 position = position % mlt_producer_get_playtime( this );
256 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
258 // Calculate the absolute frame
259 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
264 /** Get the current position (relative to in point).
267 mlt_position mlt_producer_position( mlt_producer this )
269 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
272 /** Get the current position (relative to start of producer).
275 mlt_position mlt_producer_frame( mlt_producer this )
277 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
280 /** Set the playing speed.
283 int mlt_producer_set_speed( mlt_producer this, double speed )
285 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
288 /** Get the playing speed.
291 double mlt_producer_get_speed( mlt_producer this )
293 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
296 /** Get the frames per second.
299 double mlt_producer_get_fps( mlt_producer this )
301 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
304 /** Set the in and out points.
307 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
309 mlt_properties properties = mlt_producer_properties( this );
311 // Correct ins and outs if necessary
314 else if ( in >= mlt_producer_get_length( this ) )
315 in = mlt_producer_get_length( this ) - 1;
319 else if ( out >= mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
320 out = mlt_producer_get_length( this ) - 1;
321 else if ( out >= mlt_producer_get_length( this ) && mlt_producer_is_blank( this ) )
322 mlt_properties_set_position( mlt_producer_properties( this ), "length", out + 1 );
324 // Swap ins and outs if wrong
333 mlt_events_block( properties, properties );
334 mlt_properties_set_position( properties, "in", in );
335 mlt_events_unblock( properties, properties );
336 mlt_properties_set_position( properties, "out", out );
341 /** Physically reduce the producer (typically a cut) to a 0 length.
342 Essentially, all 0 length cuts should be immediately removed by containers.
345 int mlt_producer_clear( mlt_producer this )
349 mlt_properties properties = mlt_producer_properties( this );
350 mlt_events_block( properties, properties );
351 mlt_properties_set_position( properties, "in", 0 );
352 mlt_events_unblock( properties, properties );
353 mlt_properties_set_position( properties, "out", -1 );
358 /** Get the in point.
361 mlt_position mlt_producer_get_in( mlt_producer this )
363 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
366 /** Get the out point.
369 mlt_position mlt_producer_get_out( mlt_producer this )
371 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
374 /** Get the total play time.
377 mlt_position mlt_producer_get_playtime( mlt_producer this )
379 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
382 /** Get the total length of the producer.
385 mlt_position mlt_producer_get_length( mlt_producer this )
387 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
390 /** Prepare for next frame.
393 void mlt_producer_prepare_next( mlt_producer this )
395 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
401 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
404 mlt_producer this = service != NULL ? service->child : NULL;
406 if ( this != NULL && !mlt_producer_is_cut( this ) )
408 // Get the properties of this producer
409 mlt_properties properties = mlt_producer_properties( this );
411 // Determine eof handling
412 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
414 // Get the speed of the producer
415 double speed = mlt_producer_get_speed( this );
417 // We need to use the clone if it's specified
418 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
420 // If no clone is specified, use this
421 clone = clone == NULL ? this : clone;
423 // A properly instatiated producer will have a get_frame method...
424 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
426 // Generate a test frame
427 *frame = mlt_frame_init( );
430 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
432 // Mark as a test card
433 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
434 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
436 // Calculate the next position
437 mlt_producer_prepare_next( this );
441 // Get the frame from the implementation
442 result = this->get_frame( clone, frame, index );
445 // Copy the fps and speed of the producer onto the frame
446 properties = mlt_frame_properties( *frame );
447 mlt_properties_set_double( properties, "_speed", speed );
448 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
449 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
450 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
451 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
452 mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
454 else if ( this != NULL )
456 // Get the speed of the cut
457 double speed = mlt_producer_get_speed( this );
459 // Get the parent of this cut
460 mlt_producer parent = mlt_producer_cut_parent( this );
462 // Get the properties of the parent
463 mlt_properties parent_properties = mlt_producer_properties( parent );
465 // Get the properties of the cut
466 mlt_properties properties = mlt_producer_properties( this );
468 // Determine the clone index
469 int clone_index = mlt_properties_get_int( properties, "_clone" );
471 // Determine the clone to use
472 mlt_producer clone = this;
474 if ( clone_index > 0 )
477 sprintf( key, "_clone.%d", clone_index - 1 );
478 clone = mlt_properties_get_data( mlt_producer_properties( mlt_producer_cut_parent( this ) ), key, NULL );
479 if ( clone == NULL ) fprintf( stderr, "requested clone doesn't exist %d\n", clone_index );
480 clone = clone == NULL ? this : clone;
487 // We need to seek to the correct position in the clone
488 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
490 // Assign the clone property to the parent
491 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
493 // Now get the frame from the parents service
494 result = mlt_service_get_frame( mlt_producer_service( parent ), frame, index );
496 // We're done with the clone now
497 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
499 mlt_properties_set_double( mlt_frame_properties( *frame ), "_speed", speed );
500 mlt_producer_prepare_next( this );
504 *frame = mlt_frame_init( );
514 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
516 return mlt_service_attach( mlt_producer_service( this ), filter );
522 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
524 return mlt_service_detach( mlt_producer_service( this ), filter );
527 /** Retrieve a filter.
530 mlt_filter mlt_producer_filter( mlt_producer this, int index )
532 return mlt_service_filter( mlt_producer_service( this ), index );
535 /** Clone this producer.
538 static mlt_producer mlt_producer_clone( mlt_producer this )
540 mlt_producer clone = NULL;
541 mlt_properties properties = mlt_producer_properties( this );
542 char *resource = mlt_properties_get( properties, "resource" );
543 char *service = mlt_properties_get( properties, "mlt_service" );
545 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
547 if ( service != NULL )
548 clone = mlt_factory_producer( service, resource );
550 if ( clone == NULL && resource != NULL )
551 clone = mlt_factory_producer( "fezzik", resource );
554 mlt_properties_inherit( mlt_producer_properties( clone ), properties );
556 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
564 static void mlt_producer_set_clones( mlt_producer this, int clones )
566 mlt_producer parent = mlt_producer_cut_parent( this );
567 mlt_properties properties = mlt_producer_properties( parent );
568 int existing = mlt_properties_get_int( properties, "_clones" );
572 // If the number of existing clones is different, then create/remove as necessary
573 if ( existing != clones )
575 if ( existing < clones )
577 for ( i = existing; i < clones; i ++ )
579 mlt_producer clone = mlt_producer_clone( parent );
580 sprintf( key, "_clone.%d", i );
581 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
586 for ( i = clones; i < existing; i ++ )
588 sprintf( key, "_clone.%d", i );
589 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
594 // Ensure all properties on the parent are passed to the clones
595 for ( i = 0; i < clones; i ++ )
597 mlt_producer clone = NULL;
598 sprintf( key, "_clone.%d", i );
599 clone = mlt_properties_get_data( properties, key, NULL );
601 mlt_properties_pass( mlt_producer_properties( clone ), properties, "" );
604 // Update the number of clones on the properties
605 mlt_properties_set_int( properties, "_clones", clones );
608 /** Optimise for overlapping cuts from the same clip.
629 static int intersect( clip_references *a, clip_references *b )
631 int diff = ( a->start - b->start ) + ( a->end - b->end );
632 return diff >= 0 && diff < ( a->end - a->start + 1 );
635 static int push( mlt_parser this, int multitrack, int track, int position )
637 mlt_properties properties = mlt_parser_properties( this );
638 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
639 track_info *info = malloc( sizeof( track_info ) );
640 info->multitrack = multitrack;
642 info->position = position;
645 return mlt_deque_push_back( stack, info );
648 static track_info *pop( mlt_parser this )
650 mlt_properties properties = mlt_parser_properties( this );
651 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
652 return mlt_deque_pop_back( stack );
655 static track_info *peek( mlt_parser this )
657 mlt_properties properties = mlt_parser_properties( this );
658 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
659 return mlt_deque_peek_back( stack );
662 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
664 track_info *info = peek( this );
665 return push( this, info->multitrack ++, info->track, info->position );
668 static int on_start_track( mlt_parser this )
670 track_info *info = peek( this );
671 info->position -= info->offset;
672 info->length -= info->offset;
673 return push( this, info->multitrack, info->track ++, info->position );
676 static int on_start_producer( mlt_parser this, mlt_producer object )
678 mlt_properties properties = mlt_parser_properties( this );
679 mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
680 mlt_producer parent = mlt_producer_cut_parent( object );
681 if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
684 clip_references *old_refs = NULL;
685 clip_references *refs = NULL;
688 track_info *info = peek( this );
689 sprintf( key, "%p", parent );
690 mlt_properties_get_data( producers, key, &count );
691 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
692 old_refs = mlt_properties_get_data( properties, key, &ref_count );
693 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
694 if ( old_refs != NULL )
695 memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
696 mlt_properties_set_int( mlt_producer_properties( object ), "_clone", -1 );
697 refs[ ref_count ].cut = object;
698 refs[ ref_count ].start = info->position;
699 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
700 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
701 info->position += mlt_producer_get_playtime( object );
702 info->length += mlt_producer_get_playtime( object );
707 static int on_end_track( mlt_parser this )
709 track_info *track = pop( this );
710 track_info *multi = peek( this );
711 multi->length += track->length;
712 multi->position += track->length;
713 multi->offset = track->length;
718 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
720 track_info *multi = pop( this );
721 track_info *track = peek( this );
722 track->position += multi->length;
723 track->length += multi->length;
728 int mlt_producer_optimise( mlt_producer this )
731 mlt_parser parser = mlt_parser_new( );
732 if ( parser != NULL )
734 int i = 0, j = 0, k = 0;
735 mlt_properties properties = mlt_parser_properties( parser );
736 mlt_properties producers = mlt_properties_new( );
737 mlt_deque stack = mlt_deque_init( );
738 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
739 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
740 parser->on_start_producer = on_start_producer;
741 parser->on_start_track = on_start_track;
742 parser->on_end_track = on_end_track;
743 parser->on_start_multitrack = on_start_multitrack;
744 parser->on_end_multitrack = on_end_multitrack;
745 push( parser, 0, 0, 0 );
746 mlt_parser_start( parser, mlt_producer_service( this ) );
747 free( pop( parser ) );
748 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
750 char *name = mlt_properties_get_name( producers, k );
754 mlt_producer producer = mlt_properties_get_data( producers, name, &count );
755 if ( producer != NULL && count > 1 )
757 clip_references *refs = mlt_properties_get_data( properties, name, &count );
758 for ( i = 0; i < count; i ++ )
761 for ( j = i + 1; j < count; j ++ )
763 if ( intersect( &refs[ i ], &refs[ j ] ) )
766 mlt_properties_set_int( mlt_producer_properties( refs[ j ].cut ), "_clone", clones );
769 if ( clones > max_clones )
773 for ( i = 0; i < count; i ++ )
775 mlt_producer cut = refs[ i ].cut;
776 if ( mlt_properties_get_int( mlt_producer_properties( cut ), "_clone" ) == -1 )
777 mlt_properties_set_int( mlt_producer_properties( cut ), "_clone", 0 );
780 mlt_producer_set_clones( producer, max_clones );
782 else if ( producer != NULL )
784 clip_references *refs = mlt_properties_get_data( properties, name, &count );
785 for ( i = 0; i < count; i ++ )
787 mlt_producer cut = refs[ i ].cut;
788 mlt_properties_set_int( mlt_producer_properties( cut ), "_clone", 0 );
790 mlt_producer_set_clones( producer, 0 );
793 mlt_parser_close( parser );
798 /** Close the producer.
801 void mlt_producer_close( mlt_producer this )
803 if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
805 this->parent.close = NULL;
807 if ( this->close != NULL )
808 this->close( this->close_object );
810 mlt_service_close( &this->parent );