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( 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( 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 /** Get the parent service object.
136 mlt_service
mlt_producer_service( mlt_producer
this )
138 return this != NULL ?
&this->parent
: NULL
;
141 /** Get the producer properties.
144 mlt_properties
mlt_producer_properties( mlt_producer
this )
146 return mlt_service_properties( &this->parent
);
149 /** Seek to a specified position.
152 int mlt_producer_seek( mlt_producer
this, mlt_position position
)
154 // Determine eof handling
155 char *eof
= mlt_properties_get( mlt_producer_properties( this ), "eof" );
156 int use_points
= 1 - mlt_properties_get_int( mlt_producer_properties( this ), "ignore_points" );
163 else if ( use_points
&& !strcmp( eof
, "pause" ) && position
>= mlt_producer_get_playtime( this ) )
165 mlt_producer_set_speed( this, 0 );
166 position
= mlt_producer_get_playtime( this ) - 1;
168 else if ( use_points
&& !strcmp( eof
, "loop" ) && position
>= mlt_producer_get_playtime( this ) )
170 position
= position
% mlt_producer_get_playtime( this );
174 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position
);
176 // Calculate the absolute frame
177 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points
* mlt_producer_get_in( this ) + position
);
182 /** Get the current position (relative to in point).
185 mlt_position
mlt_producer_position( mlt_producer
this )
187 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
190 /** Get the current position (relative to start of producer).
193 mlt_position
mlt_producer_frame( mlt_producer
this )
195 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
198 /** Set the playing speed.
201 int mlt_producer_set_speed( mlt_producer
this, double speed
)
203 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed
);
206 /** Get the playing speed.
209 double mlt_producer_get_speed( mlt_producer
this )
211 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
214 /** Get the frames per second.
217 double mlt_producer_get_fps( mlt_producer
this )
219 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
222 /** Set the in and out points.
225 int mlt_producer_set_in_and_out( mlt_producer
this, mlt_position in
, mlt_position out
)
227 mlt_properties properties
= mlt_producer_properties( this );
229 // Correct ins and outs if necessary
232 else if ( in
> mlt_producer_get_length( this ) )
233 in
= mlt_producer_get_length( this );
237 else if ( out
> mlt_producer_get_length( this ) )
238 out
= mlt_producer_get_length( this );
240 // Swap ins and outs if wrong
249 mlt_events_block( properties
, properties
);
250 mlt_properties_set_position( properties
, "in", in
);
251 mlt_properties_set_position( properties
, "out", out
);
252 mlt_events_unblock( properties
, properties
);
253 mlt_events_fire( properties
, "producer-changed", NULL
);
258 /** Get the in point.
261 mlt_position
mlt_producer_get_in( mlt_producer
this )
263 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
266 /** Get the out point.
269 mlt_position
mlt_producer_get_out( mlt_producer
this )
271 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
274 /** Get the total play time.
277 mlt_position
mlt_producer_get_playtime( mlt_producer
this )
279 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
282 /** Get the total length of the producer.
285 mlt_position
mlt_producer_get_length( mlt_producer
this )
287 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
290 /** Prepare for next frame.
293 void mlt_producer_prepare_next( mlt_producer
this )
295 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
301 static int producer_get_frame( mlt_service service
, mlt_frame_ptr frame
, int index
)
304 mlt_producer
this = service
->child
;
306 // Determine eof handling
307 char *eof
= mlt_properties_get( mlt_producer_properties( this ), "eof" );
309 // A properly instatiated producer will have a get_frame method...
310 if ( this->get_frame
== NULL
|| ( !strcmp( eof
, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
312 // Generate a test frame
313 *frame
= mlt_frame_init( );
316 result
= mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
318 // Mark as a test card
319 mlt_properties_set_int( mlt_frame_properties( *frame
), "test_image", 1 );
320 mlt_properties_set_int( mlt_frame_properties( *frame
), "test_audio", 1 );
322 // Calculate the next position
323 mlt_producer_prepare_next( this );
327 // Get the frame from the implementation
328 result
= this->get_frame( this, frame
, index
);
331 // Copy the fps and speed of the producer onto the frame
332 mlt_properties properties
= mlt_frame_properties( *frame
);
333 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( this ) );
334 double speed
= mlt_producer_get_speed( this );
335 mlt_properties_set_double( properties
, "_speed", speed
);
336 mlt_properties_set_int( properties
, "test_audio", mlt_frame_is_test_audio( *frame
) );
337 mlt_properties_set_int( properties
, "test_image", mlt_frame_is_test_card( *frame
) );
345 int mlt_producer_attach( mlt_producer
this, mlt_filter filter
)
347 return mlt_service_attach( mlt_producer_service( this ), filter
);
353 int mlt_producer_detach( mlt_producer
this, mlt_filter filter
)
355 return mlt_service_detach( mlt_producer_service( this ), filter
);
358 /** Retrieve a filter.
361 mlt_filter
mlt_producer_filter( mlt_producer
this, int index
)
363 return mlt_service_filter( mlt_producer_service( this ), index
);
366 /** Close the producer.
369 void mlt_producer_close( mlt_producer
this )
371 if ( this != NULL
&& mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
373 this->parent
.close
= NULL
;
375 if ( this->close
!= NULL
)
376 this->close( this->close_object
);
378 mlt_service_close( &this->parent
);