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 // Check that we haven't received NULL
40 int error
= this == NULL
;
42 // Continue if no error
45 // Initialise the producer
46 memset( this, 0, sizeof( struct mlt_producer_s
) );
48 // Associate with the child
51 // Initialise the service
52 if ( mlt_service_init( &this->parent
, this ) == 0 )
54 // The parent is the service
55 mlt_service parent
= &this->parent
;
57 // Get the properties of the parent
58 mlt_properties properties
= mlt_service_properties( parent
);
60 // Set the default properties
61 mlt_properties_set( properties
, "mlt_type", "mlt_producer" );
62 mlt_properties_set_position( properties
, "position", 0.0 );
63 mlt_properties_set_double( properties
, "frame", 0 );
64 mlt_properties_set_double( properties
, "fps", 25.0 );
65 mlt_properties_set_double( properties
, "speed", 1.0 );
66 mlt_properties_set_position( properties
, "in", 0 );
67 mlt_properties_set_position( properties
, "out", 1799999 );
68 mlt_properties_set_position( properties
, "length", 1800000 );
69 mlt_properties_set_double( properties
, "aspect_ratio", 4.0 / 3.0 );
70 mlt_properties_set( properties
, "eof", "pause" );
71 mlt_properties_set( properties
, "resource", "<producer>" );
73 // Override service get_frame
74 parent
->get_frame
= producer_get_frame
;
81 /** Get the parent service object.
84 mlt_service
mlt_producer_service( mlt_producer
this )
89 /** Get the producer properties.
92 mlt_properties
mlt_producer_properties( mlt_producer
this )
94 return mlt_service_properties( &this->parent
);
97 /** Seek to a specified position.
100 int mlt_producer_seek( mlt_producer
this, mlt_position position
)
102 // Determine eof handling
103 char *eof
= mlt_properties_get( mlt_producer_properties( this ), "eof" );
108 else if ( !strcmp( eof
, "pause" ) && position
>= mlt_producer_get_playtime( this ) )
109 position
= mlt_producer_get_playtime( this ) - 1;
112 mlt_properties_set_position( mlt_producer_properties( this ), "position", position
);
114 // Calculate the absolute frame
115 mlt_properties_set_position( mlt_producer_properties( this ), "frame", mlt_producer_get_in( this ) + position
);
120 /** Get the current position (relative to in point).
123 mlt_position
mlt_producer_position( mlt_producer
this )
125 return mlt_properties_get_position( mlt_producer_properties( this ), "position" );
128 /** Get the current position (relative to start of producer).
131 mlt_position
mlt_producer_frame( mlt_producer
this )
133 return mlt_properties_get_position( mlt_producer_properties( this ), "frame" );
136 /** Set the playing speed.
139 int mlt_producer_set_speed( mlt_producer
this, double speed
)
141 return mlt_properties_set_double( mlt_producer_properties( this ), "speed", speed
);
144 /** Get the playing speed.
147 double mlt_producer_get_speed( mlt_producer
this )
149 return mlt_properties_get_double( mlt_producer_properties( this ), "speed" );
152 /** Get the frames per second.
155 double mlt_producer_get_fps( mlt_producer
this )
157 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
160 /** Set the in and out points.
163 int mlt_producer_set_in_and_out( mlt_producer
this, mlt_position in
, mlt_position out
)
165 // Correct ins and outs if necessary
168 else if ( in
> mlt_producer_get_length( this ) )
169 in
= mlt_producer_get_length( this );
173 else if ( out
> mlt_producer_get_length( this ) )
174 out
= mlt_producer_get_length( this );
176 // Swap ins and outs if wrong
185 mlt_properties_set_position( mlt_producer_properties( this ), "in", in
);
186 mlt_properties_set_position( mlt_producer_properties( this ), "out", out
);
191 /** Get the in point.
194 mlt_position
mlt_producer_get_in( mlt_producer
this )
196 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
199 /** Get the out point.
202 mlt_position
mlt_producer_get_out( mlt_producer
this )
204 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
207 /** Get the total play time.
210 mlt_position
mlt_producer_get_playtime( mlt_producer
this )
212 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
215 /** Get the total length of the producer.
218 mlt_position
mlt_producer_get_length( mlt_producer
this )
220 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
223 /** Prepare for next frame.
226 void mlt_producer_prepare_next( mlt_producer
this )
228 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
234 static int producer_get_frame( mlt_service service
, mlt_frame_ptr frame
, int index
)
237 mlt_producer
this = service
->child
;
239 // Determine eof handling
240 char *eof
= mlt_properties_get( mlt_producer_properties( this ), "eof" );
242 // A properly instatiated producer will have a get_frame method...
243 if ( this->get_frame
== NULL
|| ( !strcmp( eof
, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
245 // Generate a test frame
246 *frame
= mlt_frame_init( );
249 result
= mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
251 // Calculate the next position
252 mlt_producer_prepare_next( this );
256 // Get the frame from the implementation
257 result
= this->get_frame( this, frame
, index
);
260 // Copy the fps and speed of the producer onto the frame
261 mlt_properties properties
= mlt_frame_properties( *frame
);
262 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( this ) );
263 double speed
= mlt_producer_get_speed( this );
264 mlt_properties_set_double( properties
, "speed", speed
);
269 /** Close the producer.
272 void mlt_producer_close( mlt_producer
this )
274 if ( this->close
!= NULL
)
277 mlt_service_close( &this->parent
);