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 );
38 int mlt_producer_init( mlt_producer this, void *child )
40 // Check that we haven't received NULL
41 int error = this == NULL;
43 // Continue if no error
46 // Initialise the producer
47 memset( this, 0, sizeof( struct mlt_producer_s ) );
49 // Associate with the child
52 // Initialise the service
53 if ( mlt_service_init( &this->parent, this ) == 0 )
55 // Get the normalisation preference
56 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
58 // The parent is the service
59 mlt_service parent = &this->parent;
61 // Get the properties of the parent
62 mlt_properties properties = mlt_service_properties( parent );
64 // Set the default properties
65 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
66 mlt_properties_set_position( properties, "_position", 0.0 );
67 mlt_properties_set_double( properties, "_frame", 0 );
68 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
70 mlt_properties_set_double( properties, "fps", 25.0 );
71 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
75 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
76 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
78 mlt_properties_set_double( properties, "_speed", 1.0 );
79 mlt_properties_set_position( properties, "in", 0 );
80 mlt_properties_set_position( properties, "out", 14999 );
81 mlt_properties_set_position( properties, "length", 15000 );
82 mlt_properties_set( properties, "eof", "pause" );
83 mlt_properties_set( properties, "resource", "<producer>" );
85 // Override service get_frame
86 parent->get_frame = producer_get_frame;
93 /** Get the parent service object.
96 mlt_service mlt_producer_service( mlt_producer this )
101 /** Get the producer properties.
104 mlt_properties mlt_producer_properties( mlt_producer this )
106 return mlt_service_properties( &this->parent );
109 /** Seek to a specified position.
112 int mlt_producer_seek( mlt_producer this, mlt_position position )
114 // Determine eof handling
115 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
122 else if ( !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
124 mlt_producer_set_speed( this, 0 );
125 position = mlt_producer_get_playtime( this ) - 1;
127 else if ( !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
129 position = position % mlt_producer_get_playtime( this );
133 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
135 // Calculate the absolute frame
136 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", mlt_producer_get_in( this ) + position );
141 /** Get the current position (relative to in point).
144 mlt_position mlt_producer_position( mlt_producer this )
146 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
149 /** Get the current position (relative to start of producer).
152 mlt_position mlt_producer_frame( mlt_producer this )
154 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
157 /** Set the playing speed.
160 int mlt_producer_set_speed( mlt_producer this, double speed )
162 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
165 /** Get the playing speed.
168 double mlt_producer_get_speed( mlt_producer this )
170 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
173 /** Get the frames per second.
176 double mlt_producer_get_fps( mlt_producer this )
178 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
181 /** Set the in and out points.
184 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
186 // Correct ins and outs if necessary
189 else if ( in > mlt_producer_get_length( this ) )
190 in = mlt_producer_get_length( this );
194 else if ( out > mlt_producer_get_length( this ) )
195 out = mlt_producer_get_length( this );
197 // Swap ins and outs if wrong
206 mlt_properties_set_position( mlt_producer_properties( this ), "in", in );
207 mlt_properties_set_position( mlt_producer_properties( this ), "out", out );
212 /** Get the in point.
215 mlt_position mlt_producer_get_in( mlt_producer this )
217 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
220 /** Get the out point.
223 mlt_position mlt_producer_get_out( mlt_producer this )
225 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
228 /** Get the total play time.
231 mlt_position mlt_producer_get_playtime( mlt_producer this )
233 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
236 /** Get the total length of the producer.
239 mlt_position mlt_producer_get_length( mlt_producer this )
241 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
244 /** Prepare for next frame.
247 void mlt_producer_prepare_next( mlt_producer this )
249 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
255 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
258 mlt_producer this = service->child;
260 // Determine eof handling
261 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
263 // A properly instatiated producer will have a get_frame method...
264 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
266 // Generate a test frame
267 *frame = mlt_frame_init( );
270 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
272 // Mark as a test card
273 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
274 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
276 // Calculate the next position
277 mlt_producer_prepare_next( this );
281 // Get the frame from the implementation
282 result = this->get_frame( this, frame, index );
285 // Copy the fps and speed of the producer onto the frame
286 mlt_properties properties = mlt_frame_properties( *frame );
287 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
288 double speed = mlt_producer_get_speed( this );
289 mlt_properties_set_double( properties, "_speed", speed );
294 /** Close the producer.
297 void mlt_producer_close( mlt_producer this )
299 if ( this->close != NULL )
302 mlt_service_close( &this->parent );