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"
30 /** Forward references.
33 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
34 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
35 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
40 int mlt_producer_init( mlt_producer this, void *child )
42 // Check that we haven't received NULL
43 int error = this == NULL;
45 // Continue if no error
48 // Initialise the producer
49 memset( this, 0, sizeof( struct mlt_producer_s ) );
51 // Associate with the child
54 // Initialise the service
55 if ( mlt_service_init( &this->parent, this ) == 0 )
57 // Get the normalisation preference
58 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
60 // The parent is the service
61 mlt_service parent = &this->parent;
63 // Define the parent close
64 parent->close = ( mlt_destructor )mlt_producer_close;
65 parent->close_object = this;
67 // For convenience, we'll assume the close_object is this
68 this->close_object = this;
70 // Get the properties of the parent
71 mlt_properties properties = mlt_service_properties( parent );
73 // Set the default properties
74 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
75 mlt_properties_set_position( properties, "_position", 0.0 );
76 mlt_properties_set_double( properties, "_frame", 0 );
77 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
79 mlt_properties_set_double( properties, "fps", 25.0 );
80 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
84 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
85 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
87 mlt_properties_set_double( properties, "_speed", 1.0 );
88 mlt_properties_set_position( properties, "in", 0 );
89 mlt_properties_set_position( properties, "out", 14999 );
90 mlt_properties_set_position( properties, "length", 15000 );
91 mlt_properties_set( properties, "eof", "pause" );
92 mlt_properties_set( properties, "resource", "<producer>" );
94 // Override service get_frame
95 parent->get_frame = producer_get_frame;
97 mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
98 mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
99 mlt_events_register( properties, "producer-changed", NULL );
106 /** Listener for property changes.
109 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
111 if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
112 mlt_events_fire( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
115 /** Listener for service changes.
118 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
120 mlt_events_fire( mlt_producer_properties( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
123 /** Create a new producer.
126 mlt_producer mlt_producer_new( )
128 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
129 mlt_producer_init( this, NULL );
133 /** Determine if producer is a cut.
136 int mlt_producer_is_cut( mlt_producer this )
138 return mlt_properties_get_int( mlt_producer_properties( this ), "_cut" );
141 /** Obtain the parent producer.
144 mlt_producer mlt_producer_cut_parent( mlt_producer this )
146 mlt_properties properties = mlt_producer_properties( this );
147 if ( mlt_producer_is_cut( this ) )
148 return mlt_properties_get_data( properties, "_cut_parent", NULL );
153 /** Create a cut of this producer
156 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
158 mlt_producer result = mlt_producer_new( );
159 mlt_producer parent = mlt_producer_cut_parent( this );
160 mlt_properties properties = mlt_producer_properties( result );
161 mlt_properties parent_props = mlt_producer_properties( parent );
163 // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
166 if ( out >= mlt_producer_get_playtime( parent ) )
167 out = mlt_producer_get_playtime( parent ) - 1;
169 mlt_properties_inc_ref( parent_props );
170 mlt_properties_set_int( properties, "_cut", 1 );
171 mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
172 mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
173 mlt_properties_set_position( properties, "in", 0 );
174 mlt_properties_set_position( properties, "out", 0 );
175 mlt_producer_set_in_and_out( result, in, out );
180 /** Get the parent service object.
183 mlt_service mlt_producer_service( mlt_producer this )
185 return this != NULL ? &this->parent : NULL;
188 /** Get the producer properties.
191 mlt_properties mlt_producer_properties( mlt_producer this )
193 return mlt_service_properties( &this->parent );
196 /** Seek to a specified position.
199 int mlt_producer_seek( mlt_producer this, mlt_position position )
201 // Determine eof handling
202 mlt_properties properties = mlt_producer_properties( this );
203 char *eof = mlt_properties_get( properties, "eof" );
204 int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
206 // Recursive behaviour for cuts...
207 if ( mlt_producer_is_cut( this ) )
208 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
215 else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
217 mlt_producer_set_speed( this, 0 );
218 position = mlt_producer_get_playtime( this ) - 1;
220 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
222 position = position % mlt_producer_get_playtime( this );
226 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
228 // Calculate the absolute frame
229 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
234 /** Get the current position (relative to in point).
237 mlt_position mlt_producer_position( mlt_producer this )
239 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
242 /** Get the current position (relative to start of producer).
245 mlt_position mlt_producer_frame( mlt_producer this )
247 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
250 /** Set the playing speed.
253 int mlt_producer_set_speed( mlt_producer this, double speed )
255 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
258 /** Get the playing speed.
261 double mlt_producer_get_speed( mlt_producer this )
263 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
266 /** Get the frames per second.
269 double mlt_producer_get_fps( mlt_producer this )
271 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
274 /** Set the in and out points.
277 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
279 mlt_properties properties = mlt_producer_properties( this );
281 // Correct ins and outs if necessary
284 else if ( in > mlt_producer_get_length( this ) )
285 in = mlt_producer_get_length( this );
289 else if ( out > mlt_producer_get_length( this ) )
290 out = mlt_producer_get_length( this );
292 // Swap ins and outs if wrong
301 mlt_events_block( properties, properties );
302 mlt_properties_set_position( properties, "in", in );
303 mlt_events_unblock( properties, properties );
304 mlt_properties_set_position( properties, "out", out );
309 /** Physically reduce the producer (typically a cut) to a 0 length.
310 Essentially, all 0 length cuts should be immediately removed by containers.
313 int mlt_producer_clear( mlt_producer this )
317 mlt_properties properties = mlt_producer_properties( this );
318 mlt_events_block( properties, properties );
319 mlt_properties_set_position( properties, "in", 0 );
320 mlt_events_unblock( properties, properties );
321 mlt_properties_set_position( properties, "out", -1 );
326 /** Get the in point.
329 mlt_position mlt_producer_get_in( mlt_producer this )
331 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
334 /** Get the out point.
337 mlt_position mlt_producer_get_out( mlt_producer this )
339 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
342 /** Get the total play time.
345 mlt_position mlt_producer_get_playtime( mlt_producer this )
347 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
350 /** Get the total length of the producer.
353 mlt_position mlt_producer_get_length( mlt_producer this )
355 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
358 /** Prepare for next frame.
361 void mlt_producer_prepare_next( mlt_producer this )
363 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
369 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
372 mlt_producer this = service->child;
374 if ( !mlt_producer_is_cut( this ) )
376 // Determine eof handling
377 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
379 // A properly instatiated producer will have a get_frame method...
380 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
382 // Generate a test frame
383 *frame = mlt_frame_init( );
386 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
388 // Mark as a test card
389 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
390 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
392 // Calculate the next position
393 mlt_producer_prepare_next( this );
397 // Get the frame from the implementation
398 result = this->get_frame( this, frame, index );
401 // Copy the fps and speed of the producer onto the frame
402 mlt_properties properties = mlt_frame_properties( *frame );
403 double speed = mlt_producer_get_speed( this );
404 mlt_properties_set_double( properties, "_speed", speed );
405 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
406 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
407 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
411 mlt_properties properties = mlt_producer_properties( this );
412 mlt_producer_seek( this, mlt_properties_get_int( properties, "_position" ) );
413 result = producer_get_frame( mlt_producer_service( mlt_producer_cut_parent( this ) ), frame, index );
414 double speed = mlt_producer_get_speed( this );
415 mlt_properties_set_double( mlt_frame_properties( *frame ), "_speed", speed );
416 mlt_producer_prepare_next( this );
425 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
427 return mlt_service_attach( mlt_producer_service( this ), filter );
433 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
435 return mlt_service_detach( mlt_producer_service( this ), filter );
438 /** Retrieve a filter.
441 mlt_filter mlt_producer_filter( mlt_producer this, int index )
443 return mlt_service_filter( mlt_producer_service( this ), index );
446 /** Close the producer.
449 void mlt_producer_close( mlt_producer this )
451 if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
453 this->parent.close = NULL;
455 if ( this->close != NULL )
456 this->close( this->close_object );
458 mlt_service_close( &this->parent );