164ed59e5895d1b2d39feaf8f35c15c27e709f4d
[melted] / src / framework / mlt_producer.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "config.h"
22 #include "mlt_producer.h"
23 #include "mlt_frame.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 /** Forward references.
30 */
31
32 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
33
34 /** Constructor
35 */
36
37 int mlt_producer_init( mlt_producer this, void *child )
38 {
39 // Initialise the producer
40 memset( this, 0, sizeof( struct mlt_producer_s ) );
41
42 // Associate with the child
43 this->child = child;
44
45 // Initialise the service
46 if ( mlt_service_init( &this->parent, this ) == 0 )
47 {
48 // The parent is the service
49 mlt_service parent = &this->parent;
50
51 // Get the properties of the parent
52 mlt_properties properties = mlt_service_properties( parent );
53
54 // Set the default properties
55 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
56 mlt_properties_set_timecode( 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_timecode( properties, "in", 0.0 );
61 mlt_properties_set_timecode( properties, "out", 3600.0 );
62 mlt_properties_set_timecode( properties, "length", 3600.0 );
63 mlt_properties_set_int( properties, "known_length", 1 );
64 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
65
66 // Override service get_frame
67 parent->get_frame = producer_get_frame;
68 }
69
70 return 0;
71 }
72
73 /** Get the parent service object.
74 */
75
76 mlt_service mlt_producer_service( mlt_producer this )
77 {
78 return &this->parent;
79 }
80
81 /** Get the producer properties.
82 */
83
84 mlt_properties mlt_producer_properties( mlt_producer this )
85 {
86 return mlt_service_properties( &this->parent );
87 }
88
89 /** Seek to a specified time code.
90 */
91
92 int mlt_producer_seek( mlt_producer this, mlt_timecode timecode )
93 {
94 // Check bounds
95 if ( timecode < 0 )
96 timecode = 0;
97 if ( timecode > mlt_producer_get_playtime( this ) )
98 timecode = mlt_producer_get_playtime( this );
99
100 // Set the position
101 mlt_properties_set_timecode( mlt_producer_properties( this ), "position", timecode );
102
103 // Calculate the absolute frame
104 double frame = ( mlt_producer_get_in( this ) + timecode ) * mlt_producer_get_fps( this );
105 mlt_properties_set_double( mlt_producer_properties( this ), "frame", floor( frame + 0.5 ) );
106
107 return 0;
108 }
109
110 /** Seek to a specified absolute frame.
111 */
112
113 int mlt_producer_seek_frame( mlt_producer this, uint64_t frame )
114 {
115 // Calculate the time code
116 double timecode = ( frame / mlt_producer_get_fps( this ) ) - mlt_producer_get_in( this );
117
118 // If timecode is invalid, then seek on time
119 if ( timecode < 0 )
120 {
121 // Seek to the in point
122 mlt_producer_seek( this, 0 );
123 }
124 else if ( timecode > mlt_producer_get_playtime( this ) )
125 {
126 // Seek to the out point
127 mlt_producer_seek( this, mlt_producer_get_playtime( this ) );
128 }
129 else
130 {
131 // Set the position
132 mlt_properties_set_timecode( mlt_producer_properties( this ), "position", timecode );
133
134 // Set the absolute frame
135 mlt_properties_set_double( mlt_producer_properties( this ), "frame", frame );
136 }
137
138 return 0;
139 }
140
141 /** Get the current time code.
142 */
143
144 mlt_timecode mlt_producer_position( mlt_producer this )
145 {
146 return mlt_properties_get_timecode( mlt_producer_properties( this ), "position" );
147 }
148
149 /** Get the current frame.
150 */
151
152 uint64_t mlt_producer_frame( mlt_producer this )
153 {
154 return mlt_properties_get_double( mlt_producer_properties( this ), "frame" );
155 }
156
157 /** Set the playing speed.
158 */
159
160 int mlt_producer_set_speed( mlt_producer this, double speed )
161 {
162 return mlt_properties_set_double( mlt_producer_properties( this ), "speed", speed );
163 }
164
165 /** Get the playing speed.
166 */
167
168 double mlt_producer_get_speed( mlt_producer this )
169 {
170 return mlt_properties_get_double( mlt_producer_properties( this ), "speed" );
171 }
172
173 /** Get the frames per second.
174 */
175
176 double mlt_producer_get_fps( mlt_producer this )
177 {
178 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
179 }
180
181 /** Set the in and out points.
182 */
183
184 int mlt_producer_set_in_and_out( mlt_producer this, mlt_timecode in, mlt_timecode out )
185 {
186 // Correct ins and outs if necessary
187 if ( in < 0 )
188 in = 0;
189 if ( in > mlt_producer_get_length( this ) )
190 in = mlt_producer_get_length( this );
191 if ( out < 0 )
192 out = 0;
193 if ( out > mlt_producer_get_length( this ) )
194 out = mlt_producer_get_length( this );
195
196 // Swap ins and outs if wrong
197 if ( out < in )
198 {
199 mlt_timecode t = in;
200 in = out;
201 out = t;
202 }
203
204 // Set the values
205 mlt_properties_set_timecode( mlt_producer_properties( this ), "in", in );
206 mlt_properties_set_timecode( mlt_producer_properties( this ), "out", out );
207
208 // Seek to the in point
209 mlt_producer_seek( this, 0 );
210
211 return 0;
212 }
213
214 /** Get the in point.
215 */
216
217 mlt_timecode mlt_producer_get_in( mlt_producer this )
218 {
219 return mlt_properties_get_timecode( mlt_producer_properties( this ), "in" );
220 }
221
222 /** Get the out point.
223 */
224
225 mlt_timecode mlt_producer_get_out( mlt_producer this )
226 {
227 return mlt_properties_get_timecode( mlt_producer_properties( this ), "out" );
228 }
229
230 /** Get the total play time.
231 */
232
233 mlt_timecode mlt_producer_get_playtime( mlt_producer this )
234 {
235 return mlt_producer_get_out( this ) - mlt_producer_get_in( this );
236 }
237
238 /** Get the total length of the producer.
239 */
240
241 mlt_timecode mlt_producer_get_length( mlt_producer this )
242 {
243 return mlt_properties_get_timecode( mlt_producer_properties( this ), "length" );
244 }
245
246 /** Prepare for next frame.
247 */
248
249 void mlt_producer_prepare_next( mlt_producer this )
250 {
251 mlt_producer_seek_frame( this, mlt_producer_frame( this ) + mlt_producer_get_speed( this ) );
252 }
253
254 /** Get a frame.
255 */
256
257 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
258 {
259 int result = 1;
260 mlt_producer this = service->child;
261
262 // A properly instatiated producer will have a get_frame method...
263 if ( this->get_frame != NULL )
264 {
265 // Get the frame from the implementation
266 result = this->get_frame( this, frame, index );
267 }
268 else
269 {
270 // Generate a test frame
271 *frame = mlt_frame_init( );
272
273 // Set the timecode
274 result = mlt_frame_set_timecode( *frame, mlt_producer_position( this ) );
275
276 // Calculate the next timecode
277 mlt_producer_prepare_next( this );
278 }
279
280 // Copy the fps of the producer onto the frame
281 mlt_properties properties = mlt_frame_properties( *frame );
282 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
283
284 return 0;
285 }
286
287 /** Close the producer.
288 */
289
290 void mlt_producer_close( mlt_producer this )
291 {
292 if ( this->close != NULL )
293 this->close( this );
294 else
295 mlt_service_close( &this->parent );
296 }