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