Minor fixes
[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 // Check that we haven't received NULL
40 int error = this == NULL;
41
42 // Continue if no error
43 if ( error == 0 )
44 {
45 // Initialise the producer
46 memset( this, 0, sizeof( struct mlt_producer_s ) );
47
48 // Associate with the child
49 this->child = child;
50
51 // Initialise the service
52 if ( mlt_service_init( &this->parent, this ) == 0 )
53 {
54 // Get the normalisation preference
55 char *normalisation = getenv( "MLT_NORMALISATION" );
56
57 // The parent is the service
58 mlt_service parent = &this->parent;
59
60 // Get the properties of the parent
61 mlt_properties properties = mlt_service_properties( parent );
62
63 // Set the default properties
64 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
65 mlt_properties_set_position( properties, "_position", 0.0 );
66 mlt_properties_set_double( properties, "_frame", 0 );
67 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
68 mlt_properties_set_double( properties, "fps", 25.0 );
69 else
70 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
71 mlt_properties_set_double( properties, "_speed", 1.0 );
72 mlt_properties_set_position( properties, "in", 0 );
73 mlt_properties_set_position( properties, "out", 14999 );
74 mlt_properties_set_position( properties, "length", 15000 );
75 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
76 mlt_properties_set( properties, "eof", "pause" );
77 mlt_properties_set( properties, "resource", "<producer>" );
78
79 // Override service get_frame
80 parent->get_frame = producer_get_frame;
81 }
82 }
83
84 return error;
85 }
86
87 /** Get the parent service object.
88 */
89
90 mlt_service mlt_producer_service( mlt_producer this )
91 {
92 return &this->parent;
93 }
94
95 /** Get the producer properties.
96 */
97
98 mlt_properties mlt_producer_properties( mlt_producer this )
99 {
100 return mlt_service_properties( &this->parent );
101 }
102
103 /** Seek to a specified position.
104 */
105
106 int mlt_producer_seek( mlt_producer this, mlt_position position )
107 {
108 // Determine eof handling
109 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
110
111 // Check bounds
112 if ( position < 0 )
113 {
114 position = 0;
115 }
116 else if ( !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
117 {
118 mlt_producer_set_speed( this, 0 );
119 position = mlt_producer_get_playtime( this ) - 1;
120 }
121 else if ( !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
122 {
123 position = position % mlt_producer_get_playtime( this );
124 }
125
126 // Set the position
127 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
128
129 // Calculate the absolute frame
130 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", mlt_producer_get_in( this ) + position );
131
132 return 0;
133 }
134
135 /** Get the current position (relative to in point).
136 */
137
138 mlt_position mlt_producer_position( mlt_producer this )
139 {
140 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
141 }
142
143 /** Get the current position (relative to start of producer).
144 */
145
146 mlt_position mlt_producer_frame( mlt_producer this )
147 {
148 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
149 }
150
151 /** Set the playing speed.
152 */
153
154 int mlt_producer_set_speed( mlt_producer this, double speed )
155 {
156 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
157 }
158
159 /** Get the playing speed.
160 */
161
162 double mlt_producer_get_speed( mlt_producer this )
163 {
164 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
165 }
166
167 /** Get the frames per second.
168 */
169
170 double mlt_producer_get_fps( mlt_producer this )
171 {
172 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
173 }
174
175 /** Set the in and out points.
176 */
177
178 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
179 {
180 // Correct ins and outs if necessary
181 if ( in < 0 )
182 in = 0;
183 else if ( in > mlt_producer_get_length( this ) )
184 in = mlt_producer_get_length( this );
185
186 if ( out < 0 )
187 out = 0;
188 else if ( out > mlt_producer_get_length( this ) )
189 out = mlt_producer_get_length( this );
190
191 // Swap ins and outs if wrong
192 if ( out < in )
193 {
194 mlt_position t = in;
195 in = out;
196 out = t;
197 }
198
199 // Set the values
200 mlt_properties_set_position( mlt_producer_properties( this ), "in", in );
201 mlt_properties_set_position( mlt_producer_properties( this ), "out", out );
202
203 return 0;
204 }
205
206 /** Get the in point.
207 */
208
209 mlt_position mlt_producer_get_in( mlt_producer this )
210 {
211 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
212 }
213
214 /** Get the out point.
215 */
216
217 mlt_position mlt_producer_get_out( mlt_producer this )
218 {
219 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
220 }
221
222 /** Get the total play time.
223 */
224
225 mlt_position mlt_producer_get_playtime( mlt_producer this )
226 {
227 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
228 }
229
230 /** Get the total length of the producer.
231 */
232
233 mlt_position mlt_producer_get_length( mlt_producer this )
234 {
235 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
236 }
237
238 /** Prepare for next frame.
239 */
240
241 void mlt_producer_prepare_next( mlt_producer this )
242 {
243 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
244 }
245
246 /** Get a frame.
247 */
248
249 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
250 {
251 int result = 1;
252 mlt_producer this = service->child;
253
254 // Determine eof handling
255 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
256
257 // A properly instatiated producer will have a get_frame method...
258 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
259 {
260 // Generate a test frame
261 *frame = mlt_frame_init( );
262
263 // Set the position
264 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
265
266 // Mark as a test card
267 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
268 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
269
270 // Calculate the next position
271 mlt_producer_prepare_next( this );
272 }
273 else
274 {
275 // Get the frame from the implementation
276 result = this->get_frame( this, frame, index );
277 }
278
279 // Copy the fps and speed of the producer onto the frame
280 mlt_properties properties = mlt_frame_properties( *frame );
281 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
282 double speed = mlt_producer_get_speed( this );
283 mlt_properties_set_double( properties, "_speed", speed );
284
285 return 0;
286 }
287
288 /** Close the producer.
289 */
290
291 void mlt_producer_close( mlt_producer this )
292 {
293 if ( this->close != NULL )
294 this->close( this );
295 else
296 mlt_service_close( &this->parent );
297 }