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_frame.h"
29 /** Forward references.
32 static int producer_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
);
37 int mlt_producer_init( mlt_producer
this, void *child
)
39 // Initialise the producer
40 memset( this, 0, sizeof( struct mlt_producer_s
) );
42 // Associate with the child
45 // Initialise the service
46 if ( mlt_service_init( &this->parent
, this ) == 0 )
48 // The parent is the service
49 mlt_service parent
= &this->parent
;
51 // Get the properties of the parent
52 mlt_properties properties
= mlt_service_properties( parent
);
54 // Set the default properties
55 mlt_properties_set( properties
, "mlt_type", "mlt_producer" );
56 mlt_properties_set_position( properties
, "position", 0.0 );
57 mlt_properties_set_double( properties
, "frame", 0 );
58 mlt_properties_set_double( properties
, "fps", 25.0 );
59 mlt_properties_set_double( properties
, "speed", 1.0 );
60 mlt_properties_set_position( properties
, "in", 0.0 );
61 mlt_properties_set_position( properties
, "out", 179999 );
62 mlt_properties_set_position( properties
, "length", 180000 );
63 mlt_properties_set_double( properties
, "aspect_ratio", 4.0 / 3.0 );
64 mlt_properties_set( properties
, "log_id", "multitrack" );
66 // Override service get_frame
67 parent
->get_frame
= producer_get_frame
;
73 /** Get the parent service object.
76 mlt_service
mlt_producer_service( mlt_producer
this )
81 /** Get the producer properties.
84 mlt_properties
mlt_producer_properties( mlt_producer
this )
86 return mlt_service_properties( &this->parent
);
89 /** Convert frame position to position.
93 mlt_position mlt_producer_time( mlt_producer this, int64_t frame )
98 return ( mlt_position )frame / mlt_producer_get_fps( this );
102 /** Convert position to frame position.
106 int64_t mlt_producer_frame_position( mlt_producer this, mlt_position position )
111 return ( int64_t )( floor( position * mlt_producer_get_fps( this ) + 0.5 ) );
115 /** Seek to a specified position.
118 int mlt_producer_seek( mlt_producer
this, mlt_position position
)
123 else if ( position
> mlt_producer_get_playtime( this ) )
124 position
= mlt_producer_get_playtime( this ) - 1;
127 mlt_properties_set_position( mlt_producer_properties( this ), "position", position
);
129 // Calculate the absolute frame
130 mlt_properties_set_position( mlt_producer_properties( this ), "frame", mlt_producer_get_in( this ) + position
);
135 /** Get the current position (relative to in point).
138 mlt_position
mlt_producer_position( mlt_producer
this )
140 return mlt_properties_get_position( mlt_producer_properties( this ), "position" );
143 /** Get the current position (relative to start of producer).
146 mlt_position
mlt_producer_frame( mlt_producer
this )
148 return mlt_properties_get_double( mlt_producer_properties( this ), "frame" );
151 /** Set the playing speed.
154 int mlt_producer_set_speed( mlt_producer
this, double speed
)
156 return mlt_properties_set_double( mlt_producer_properties( this ), "speed", speed
);
159 /** Get the playing speed.
162 double mlt_producer_get_speed( mlt_producer
this )
164 return mlt_properties_get_double( mlt_producer_properties( this ), "speed" );
167 /** Get the frames per second.
170 double mlt_producer_get_fps( mlt_producer
this )
172 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
175 /** Set the in and out points.
178 int mlt_producer_set_in_and_out( mlt_producer
this, mlt_position in
, mlt_position out
)
180 // Correct ins and outs if necessary
183 else if ( in
> mlt_producer_get_length( this ) )
184 in
= mlt_producer_get_length( this );
188 else if ( out
> mlt_producer_get_length( this ) )
189 out
= mlt_producer_get_length( this );
191 // Swap ins and outs if wrong
200 mlt_properties_set_position( mlt_producer_properties( this ), "in", in
);
201 mlt_properties_set_position( mlt_producer_properties( this ), "out", out
);
206 /** Get the in point.
209 mlt_position
mlt_producer_get_in( mlt_producer
this )
211 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
214 /** Get the out point.
217 mlt_position
mlt_producer_get_out( mlt_producer
this )
219 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
222 /** Get the total play time.
225 mlt_position
mlt_producer_get_playtime( mlt_producer
this )
227 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
230 /** Get the total length of the producer.
233 mlt_position
mlt_producer_get_length( mlt_producer
this )
235 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
238 /** Prepare for next frame.
241 void mlt_producer_prepare_next( mlt_producer
this )
243 mlt_producer_seek( this, mlt_producer_frame( this ) + mlt_producer_get_speed( this ) );
249 static int producer_get_frame( mlt_service service
, mlt_frame_ptr frame
, int index
)
252 mlt_producer
this = service
->child
;
254 // A properly instatiated producer will have a get_frame method...
255 if ( this->get_frame
!= NULL
)
257 // Get the frame from the implementation
258 result
= this->get_frame( this, frame
, index
);
262 // Generate a test frame
263 *frame
= mlt_frame_init( );
266 result
= mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
268 // Calculate the next position
269 mlt_producer_prepare_next( this );
272 // Copy the fps and speed of the producer onto the frame
273 mlt_properties properties
= mlt_frame_properties( *frame
);
274 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( this ) );
275 double speed
= mlt_producer_get_speed( this );
276 mlt_properties_set_double( properties
, "speed", speed
);
281 /** Close the producer.
284 void mlt_producer_close( mlt_producer
this )
286 if ( this->close
!= NULL
)
289 mlt_service_close( &this->parent
);