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 /** Obtain the parent producer.
168 mlt_producer
mlt_producer_cut_parent( mlt_producer
this )
170 mlt_properties properties
= mlt_producer_properties( this );
171 if ( mlt_producer_is_cut( this ) )
172 return mlt_properties_get_data( properties
, "_cut_parent", NULL
);
177 /** Create a cut of this producer
180 mlt_producer
mlt_producer_cut( mlt_producer
this, int in
, int out
)
182 mlt_producer result
= mlt_producer_new( );
183 mlt_producer parent
= mlt_producer_cut_parent( this );
184 mlt_properties properties
= mlt_producer_properties( result
);
185 mlt_properties parent_props
= mlt_producer_properties( parent
);
187 // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
190 if ( out
>= mlt_producer_get_playtime( parent
) )
191 out
= mlt_producer_get_playtime( parent
) - 1;
193 mlt_properties_inc_ref( parent_props
);
194 mlt_properties_set_int( properties
, "_cut", 1 );
195 mlt_properties_set_data( properties
, "_cut_parent", parent
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
196 mlt_properties_set_position( properties
, "length", mlt_properties_get_position( parent_props
, "length" ) );
197 mlt_properties_set_position( properties
, "in", 0 );
198 mlt_properties_set_position( properties
, "out", 0 );
199 mlt_producer_set_in_and_out( result
, in
, out
);
204 /** Get the parent service object.
207 mlt_service
mlt_producer_service( mlt_producer
this )
209 return this != NULL ?
&this->parent
: NULL
;
212 /** Get the producer properties.
215 mlt_properties
mlt_producer_properties( mlt_producer
this )
217 return mlt_service_properties( &this->parent
);
220 /** Seek to a specified position.
223 int mlt_producer_seek( mlt_producer
this, mlt_position position
)
225 // Determine eof handling
226 mlt_properties properties
= mlt_producer_properties( this );
227 char *eof
= mlt_properties_get( properties
, "eof" );
228 int use_points
= 1 - mlt_properties_get_int( properties
, "ignore_points" );
230 // Recursive behaviour for cuts...
231 if ( mlt_producer_is_cut( this ) )
232 mlt_producer_seek( mlt_producer_cut_parent( this ), position
+ mlt_producer_get_in( this ) );
239 else if ( use_points
&& !strcmp( eof
, "pause" ) && position
>= mlt_producer_get_playtime( this ) )
241 mlt_producer_set_speed( this, 0 );
242 position
= mlt_producer_get_playtime( this ) - 1;
244 else if ( use_points
&& !strcmp( eof
, "loop" ) && position
>= mlt_producer_get_playtime( this ) )
246 position
= position
% mlt_producer_get_playtime( this );
250 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position
);
252 // Calculate the absolute frame
253 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points
* mlt_producer_get_in( this ) + position
);
258 /** Get the current position (relative to in point).
261 mlt_position
mlt_producer_position( mlt_producer
this )
263 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
266 /** Get the current position (relative to start of producer).
269 mlt_position
mlt_producer_frame( mlt_producer
this )
271 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
274 /** Set the playing speed.
277 int mlt_producer_set_speed( mlt_producer
this, double speed
)
279 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed
);
282 /** Get the playing speed.
285 double mlt_producer_get_speed( mlt_producer
this )
287 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
290 /** Get the frames per second.
293 double mlt_producer_get_fps( mlt_producer
this )
295 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
298 /** Set the in and out points.
301 int mlt_producer_set_in_and_out( mlt_producer
this, mlt_position in
, mlt_position out
)
303 mlt_properties properties
= mlt_producer_properties( this );
305 // Correct ins and outs if necessary
308 else if ( in
> mlt_producer_get_length( this ) )
309 in
= mlt_producer_get_length( this );
313 else if ( out
> mlt_producer_get_length( this ) )
314 out
= mlt_producer_get_length( this );
316 // Swap ins and outs if wrong
325 mlt_events_block( properties
, properties
);
326 mlt_properties_set_position( properties
, "in", in
);
327 mlt_events_unblock( properties
, properties
);
328 mlt_properties_set_position( properties
, "out", out
);
333 /** Physically reduce the producer (typically a cut) to a 0 length.
334 Essentially, all 0 length cuts should be immediately removed by containers.
337 int mlt_producer_clear( mlt_producer
this )
341 mlt_properties properties
= mlt_producer_properties( this );
342 mlt_events_block( properties
, properties
);
343 mlt_properties_set_position( properties
, "in", 0 );
344 mlt_events_unblock( properties
, properties
);
345 mlt_properties_set_position( properties
, "out", -1 );
350 /** Get the in point.
353 mlt_position
mlt_producer_get_in( mlt_producer
this )
355 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
358 /** Get the out point.
361 mlt_position
mlt_producer_get_out( mlt_producer
this )
363 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
366 /** Get the total play time.
369 mlt_position
mlt_producer_get_playtime( mlt_producer
this )
371 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
374 /** Get the total length of the producer.
377 mlt_position
mlt_producer_get_length( mlt_producer
this )
379 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
382 /** Prepare for next frame.
385 void mlt_producer_prepare_next( mlt_producer
this )
387 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
393 static int producer_get_frame( mlt_service service
, mlt_frame_ptr frame
, int index
)
396 mlt_producer
this = service
->child
;
398 if ( !mlt_producer_is_cut( this ) )
400 // Get the properties of this producer
401 mlt_properties properties
= mlt_producer_properties( this );
403 // Determine eof handling
404 char *eof
= mlt_properties_get( mlt_producer_properties( this ), "eof" );
406 // Get the speed of the producer
407 double speed
= mlt_producer_get_speed( this );
409 // We need to use the clone if it's specified
410 mlt_producer clone
= mlt_properties_get_data( properties
, "use_clone", NULL
);
412 // If no clone is specified, use this
413 clone
= clone
== NULL ?
this : clone
;
415 // A properly instatiated producer will have a get_frame method...
416 if ( this->get_frame
== NULL
|| ( !strcmp( eof
, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
418 // Generate a test frame
419 *frame
= mlt_frame_init( );
422 result
= mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
424 // Mark as a test card
425 mlt_properties_set_int( mlt_frame_properties( *frame
), "test_image", 1 );
426 mlt_properties_set_int( mlt_frame_properties( *frame
), "test_audio", 1 );
428 // Calculate the next position
429 mlt_producer_prepare_next( this );
433 // Get the frame from the implementation
434 result
= this->get_frame( clone
, frame
, index
);
437 // Copy the fps and speed of the producer onto the frame
438 properties
= mlt_frame_properties( *frame
);
439 mlt_properties_set_double( properties
, "_speed", speed
);
440 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( this ) );
441 mlt_properties_set_int( properties
, "test_audio", mlt_frame_is_test_audio( *frame
) );
442 mlt_properties_set_int( properties
, "test_image", mlt_frame_is_test_card( *frame
) );
443 if ( mlt_properties_get_data( properties
, "_producer", NULL
) == NULL
)
444 mlt_properties_set_data( properties
, "_producer", service
, 0, NULL
, NULL
);
448 // Get the speed of the cut
449 double speed
= mlt_producer_get_speed( this );
451 // Get the parent of this cut
452 mlt_producer parent
= mlt_producer_cut_parent( this );
454 // Get the properties of the parent
455 mlt_properties parent_properties
= mlt_producer_properties( parent
);
457 // Get the properties of the cut
458 mlt_properties properties
= mlt_producer_properties( this );
460 // Determine the clone index
461 int clone_index
= mlt_properties_get_int( properties
, "_clone" );
463 // Determine the clone to use
464 mlt_producer clone
= this;
466 if ( clone_index
> 0 )
469 sprintf( key
, "_clone.%d", clone_index
- 1 );
470 clone
= mlt_properties_get_data( mlt_producer_properties( mlt_producer_cut_parent( this ) ), key
, NULL
);
471 if ( clone
== NULL
) fprintf( stderr
, "requested clone doesn't exist %d\n", clone_index
);
472 clone
= clone
== NULL ?
this : clone
;
479 // We need to seek to the correct position in the clone
480 mlt_producer_seek( clone
, mlt_producer_get_in( this ) + mlt_properties_get_int( properties
, "_position" ) );
482 // Assign the clone property to the parent
483 mlt_properties_set_data( parent_properties
, "use_clone", clone
, 0, NULL
, NULL
);
485 // Now get the frame from the parents service
486 result
= mlt_service_get_frame( mlt_producer_service( parent
), frame
, index
);
488 // We're done with the clone now
489 mlt_properties_set_data( parent_properties
, "use_clone", NULL
, 0, NULL
, NULL
);
491 mlt_properties_set_double( mlt_frame_properties( *frame
), "_speed", speed
);
492 mlt_producer_prepare_next( this );
501 int mlt_producer_attach( mlt_producer
this, mlt_filter filter
)
503 return mlt_service_attach( mlt_producer_service( this ), filter
);
509 int mlt_producer_detach( mlt_producer
this, mlt_filter filter
)
511 return mlt_service_detach( mlt_producer_service( this ), filter
);
514 /** Retrieve a filter.
517 mlt_filter
mlt_producer_filter( mlt_producer
this, int index
)
519 return mlt_service_filter( mlt_producer_service( this ), index
);
522 /** Clone this producer.
525 static mlt_producer
mlt_producer_clone( mlt_producer
this )
527 mlt_producer clone
= NULL
;
528 mlt_properties properties
= mlt_producer_properties( this );
529 char *resource
= mlt_properties_get( properties
, "resource" );
530 char *service
= mlt_properties_get( properties
, "mlt_service" );
532 if ( service
!= NULL
)
533 clone
= mlt_factory_producer( service
, resource
);
535 if ( clone
== NULL
&& resource
!= NULL
)
536 clone
= mlt_factory_producer( "fezzik", resource
);
539 mlt_properties_inherit( mlt_producer_properties( clone
), properties
);
547 static void mlt_producer_set_clones( mlt_producer
this, int clones
)
549 mlt_producer parent
= mlt_producer_cut_parent( this );
550 mlt_properties properties
= mlt_producer_properties( parent
);
551 int existing
= mlt_properties_get_int( properties
, "_clones" );
555 // If the number of existing clones is different, then create/remove as necessary
556 if ( existing
!= clones
)
558 if ( existing
< clones
)
560 for ( i
= existing
; i
< clones
; i
++ )
562 mlt_producer clone
= mlt_producer_clone( parent
);
563 sprintf( key
, "_clone.%d", i
);
564 mlt_properties_set_data( properties
, key
, clone
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
569 for ( i
= clones
; i
< existing
; i
++ )
571 sprintf( key
, "_clone.%d", i
);
572 mlt_properties_set_data( properties
, key
, NULL
, 0, NULL
, NULL
);
577 // Ensure all properties on the parent are passed to the clones
578 for ( i
= 0; i
< clones
; i
++ )
580 mlt_producer clone
= NULL
;
581 sprintf( key
, "_clone.%d", i
);
582 clone
= mlt_properties_get_data( properties
, key
, NULL
);
584 mlt_properties_pass( mlt_producer_properties( clone
), properties
, "" );
587 // Update the number of clones on the properties
588 mlt_properties_set_int( properties
, "_clones", clones
);
591 /** Optimise for overlapping cuts from the same clip.
612 static int intersect( clip_references
*a
, clip_references
*b
)
614 int diff
= ( a
->start
- b
->start
) + ( a
->end
- b
->end
);
615 return diff
>= 0 && diff
< ( a
->end
- a
->start
+ 1 );
618 static int push( mlt_parser
this, int multitrack
, int track
, int position
)
620 mlt_properties properties
= mlt_parser_properties( this );
621 mlt_deque stack
= mlt_properties_get_data( properties
, "stack", NULL
);
622 track_info
*info
= malloc( sizeof( track_info
) );
623 info
->multitrack
= multitrack
;
625 info
->position
= position
;
628 return mlt_deque_push_back( stack
, info
);
631 static track_info
*pop( mlt_parser
this )
633 mlt_properties properties
= mlt_parser_properties( this );
634 mlt_deque stack
= mlt_properties_get_data( properties
, "stack", NULL
);
635 return mlt_deque_pop_back( stack
);
638 static track_info
*peek( mlt_parser
this )
640 mlt_properties properties
= mlt_parser_properties( this );
641 mlt_deque stack
= mlt_properties_get_data( properties
, "stack", NULL
);
642 return mlt_deque_peek_back( stack
);
645 static int on_start_multitrack( mlt_parser
this, mlt_multitrack object
)
647 track_info
*info
= peek( this );
648 return push( this, info
->multitrack
++, info
->track
, info
->position
);
651 static int on_start_track( mlt_parser
this )
653 track_info
*info
= peek( this );
654 info
->position
-= info
->offset
;
655 info
->length
-= info
->offset
;
656 return push( this, info
->multitrack
, info
->track
++, info
->position
);
659 static int on_start_producer( mlt_parser
this, mlt_producer object
)
661 mlt_properties properties
= mlt_parser_properties( this );
662 mlt_properties producers
= mlt_properties_get_data( properties
, "producers", NULL
);
663 mlt_producer parent
= mlt_producer_cut_parent( object
);
664 if ( mlt_service_identify( ( mlt_service
)mlt_producer_cut_parent( object
) ) == producer_type
&& mlt_producer_is_cut( object
) )
667 clip_references
*old_refs
= NULL
;
668 clip_references
*refs
= NULL
;
671 track_info
*info
= peek( this );
672 sprintf( key
, "%p", parent
);
673 mlt_properties_get_data( producers
, key
, &count
);
674 mlt_properties_set_data( producers
, key
, parent
, ++ count
, NULL
, NULL
);
675 old_refs
= mlt_properties_get_data( properties
, key
, &ref_count
);
676 refs
= malloc( ( ref_count
+ 1 ) * sizeof( clip_references
) );
677 if ( old_refs
!= NULL
)
678 memcpy( refs
, old_refs
, ref_count
* sizeof( clip_references
) );
679 mlt_properties_set_int( mlt_producer_properties( object
), "_clone", -1 );
680 refs
[ ref_count
].cut
= object
;
681 refs
[ ref_count
].start
= info
->position
;
682 refs
[ ref_count
].end
= info
->position
+ mlt_producer_get_playtime( object
) - 1;
683 mlt_properties_set_data( properties
, key
, refs
, ++ ref_count
, free
, NULL
);
684 info
->position
+= mlt_producer_get_playtime( object
);
685 info
->length
+= mlt_producer_get_playtime( object
);
690 static int on_end_track( mlt_parser
this )
692 track_info
*track
= pop( this );
693 track_info
*multi
= peek( this );
694 multi
->length
+= track
->length
;
695 multi
->position
+= track
->length
;
696 multi
->offset
= track
->length
;
701 static int on_end_multitrack( mlt_parser
this, mlt_multitrack object
)
703 track_info
*multi
= pop( this );
704 track_info
*track
= peek( this );
705 track
->position
+= multi
->length
;
706 track
->length
+= multi
->length
;
711 int mlt_producer_optimise( mlt_producer
this )
714 mlt_parser parser
= mlt_parser_new( );
715 if ( parser
!= NULL
)
717 int i
= 0, j
= 0, k
= 0;
718 mlt_properties properties
= mlt_parser_properties( parser
);
719 mlt_properties producers
= mlt_properties_new( );
720 mlt_deque stack
= mlt_deque_init( );
721 mlt_properties_set_data( properties
, "producers", producers
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
722 mlt_properties_set_data( properties
, "stack", stack
, 0, ( mlt_destructor
)mlt_deque_close
, NULL
);
723 parser
->on_start_producer
= on_start_producer
;
724 parser
->on_start_track
= on_start_track
;
725 parser
->on_end_track
= on_end_track
;
726 parser
->on_start_multitrack
= on_start_multitrack
;
727 parser
->on_end_multitrack
= on_end_multitrack
;
728 push( parser
, 0, 0, 0 );
729 mlt_parser_start( parser
, mlt_producer_service( this ) );
730 free( pop( parser
) );
731 for ( k
= 0; k
< mlt_properties_count( producers
); k
++ )
733 char *name
= mlt_properties_get_name( producers
, k
);
737 mlt_producer producer
= mlt_properties_get_data( producers
, name
, &count
);
738 if ( producer
!= NULL
&& count
> 1 )
740 clip_references
*refs
= mlt_properties_get_data( properties
, name
, &count
);
741 for ( i
= 0; i
< count
; i
++ )
744 for ( j
= i
+ 1; j
< count
; j
++ )
746 if ( intersect( &refs
[ i
], &refs
[ j
] ) )
749 mlt_properties_set_int( mlt_producer_properties( refs
[ j
].cut
), "_clone", clones
);
752 if ( clones
> max_clones
)
756 for ( i
= 0; i
< count
; i
++ )
758 mlt_producer cut
= refs
[ i
].cut
;
759 if ( mlt_properties_get_int( mlt_producer_properties( cut
), "_clone" ) == -1 )
760 mlt_properties_set_int( mlt_producer_properties( cut
), "_clone", 0 );
763 mlt_producer_set_clones( producer
, max_clones
);
765 else if ( producer
!= NULL
)
767 clip_references
*refs
= mlt_properties_get_data( properties
, name
, &count
);
768 for ( i
= 0; i
< count
; i
++ )
770 mlt_producer cut
= refs
[ i
].cut
;
771 mlt_properties_set_int( mlt_producer_properties( cut
), "_clone", 0 );
773 mlt_producer_set_clones( producer
, 0 );
776 mlt_parser_close( parser
);
781 /** Close the producer.
784 void mlt_producer_close( mlt_producer
this )
786 if ( this != NULL
&& mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
788 this->parent
.close
= NULL
;
790 if ( this->close
!= NULL
)
791 this->close( this->close_object
);
793 mlt_service_close( &this->parent
);