Filter attachments to services
[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 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
35
36 /** Constructor
37 */
38
39 int mlt_producer_init( mlt_producer this, void *child )
40 {
41 // Check that we haven't received NULL
42 int error = this == NULL;
43
44 // Continue if no error
45 if ( error == 0 )
46 {
47 // Initialise the producer
48 memset( this, 0, sizeof( struct mlt_producer_s ) );
49
50 // Associate with the child
51 this->child = child;
52
53 // Initialise the service
54 if ( mlt_service_init( &this->parent, this ) == 0 )
55 {
56 // Get the normalisation preference
57 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
58
59 // The parent is the service
60 mlt_service parent = &this->parent;
61
62 // Define the parent close
63 parent->close = ( mlt_destructor )mlt_producer_close;
64 parent->close_object = this;
65
66 // For convenience, we'll assume the close_object is this
67 this->close_object = this;
68
69 // Get the properties of the parent
70 mlt_properties properties = mlt_service_properties( parent );
71
72 // Set the default properties
73 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
74 mlt_properties_set_position( properties, "_position", 0.0 );
75 mlt_properties_set_double( properties, "_frame", 0 );
76 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
77 {
78 mlt_properties_set_double( properties, "fps", 25.0 );
79 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
80 }
81 else
82 {
83 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
84 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
85 }
86 mlt_properties_set_double( properties, "_speed", 1.0 );
87 mlt_properties_set_position( properties, "in", 0 );
88 mlt_properties_set_position( properties, "out", 14999 );
89 mlt_properties_set_position( properties, "length", 15000 );
90 mlt_properties_set( properties, "eof", "pause" );
91 mlt_properties_set( properties, "resource", "<producer>" );
92
93 // Override service get_frame
94 parent->get_frame = producer_get_frame;
95
96 mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
97 mlt_events_register( properties, "producer-changed", NULL );
98 }
99 }
100
101 return error;
102 }
103
104 /** Listener for property changes.
105 */
106
107 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
108 {
109 if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
110 mlt_events_fire( mlt_producer_properties( this ), "producer-changed", NULL );
111 }
112
113 /** Create a new producer.
114 */
115
116 mlt_producer mlt_producer_new( )
117 {
118 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
119 mlt_producer_init( this, NULL );
120 return this;
121 }
122
123 /** Get the parent service object.
124 */
125
126 mlt_service mlt_producer_service( mlt_producer this )
127 {
128 return this != NULL ? &this->parent : NULL;
129 }
130
131 /** Get the producer properties.
132 */
133
134 mlt_properties mlt_producer_properties( mlt_producer this )
135 {
136 return mlt_service_properties( &this->parent );
137 }
138
139 /** Seek to a specified position.
140 */
141
142 int mlt_producer_seek( mlt_producer this, mlt_position position )
143 {
144 // Determine eof handling
145 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
146 int use_points = 1 - mlt_properties_get_int( mlt_producer_properties( this ), "ignore_points" );
147
148 // Check bounds
149 if ( position < 0 )
150 {
151 position = 0;
152 }
153 else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
154 {
155 mlt_producer_set_speed( this, 0 );
156 position = mlt_producer_get_playtime( this ) - 1;
157 }
158 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
159 {
160 position = position % mlt_producer_get_playtime( this );
161 }
162
163 // Set the position
164 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
165
166 // Calculate the absolute frame
167 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
168
169 return 0;
170 }
171
172 /** Get the current position (relative to in point).
173 */
174
175 mlt_position mlt_producer_position( mlt_producer this )
176 {
177 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
178 }
179
180 /** Get the current position (relative to start of producer).
181 */
182
183 mlt_position mlt_producer_frame( mlt_producer this )
184 {
185 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
186 }
187
188 /** Set the playing speed.
189 */
190
191 int mlt_producer_set_speed( mlt_producer this, double speed )
192 {
193 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
194 }
195
196 /** Get the playing speed.
197 */
198
199 double mlt_producer_get_speed( mlt_producer this )
200 {
201 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
202 }
203
204 /** Get the frames per second.
205 */
206
207 double mlt_producer_get_fps( mlt_producer this )
208 {
209 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
210 }
211
212 /** Set the in and out points.
213 */
214
215 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
216 {
217 mlt_properties properties = mlt_producer_properties( this );
218
219 // Correct ins and outs if necessary
220 if ( in < 0 )
221 in = 0;
222 else if ( in > mlt_producer_get_length( this ) )
223 in = mlt_producer_get_length( this );
224
225 if ( out < 0 )
226 out = 0;
227 else if ( out > mlt_producer_get_length( this ) )
228 out = mlt_producer_get_length( this );
229
230 // Swap ins and outs if wrong
231 if ( out < in )
232 {
233 mlt_position t = in;
234 in = out;
235 out = t;
236 }
237
238 // Set the values
239 mlt_events_block( properties, properties );
240 mlt_properties_set_position( properties, "in", in );
241 mlt_properties_set_position( properties, "out", out );
242 mlt_events_unblock( properties, properties );
243 mlt_events_fire( properties, "producer-changed", NULL );
244
245 return 0;
246 }
247
248 /** Get the in point.
249 */
250
251 mlt_position mlt_producer_get_in( mlt_producer this )
252 {
253 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
254 }
255
256 /** Get the out point.
257 */
258
259 mlt_position mlt_producer_get_out( mlt_producer this )
260 {
261 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
262 }
263
264 /** Get the total play time.
265 */
266
267 mlt_position mlt_producer_get_playtime( mlt_producer this )
268 {
269 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
270 }
271
272 /** Get the total length of the producer.
273 */
274
275 mlt_position mlt_producer_get_length( mlt_producer this )
276 {
277 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
278 }
279
280 /** Prepare for next frame.
281 */
282
283 void mlt_producer_prepare_next( mlt_producer this )
284 {
285 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
286 }
287
288 /** Get a frame.
289 */
290
291 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
292 {
293 int result = 1;
294 mlt_producer this = service->child;
295
296 // Determine eof handling
297 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
298
299 // A properly instatiated producer will have a get_frame method...
300 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
301 {
302 // Generate a test frame
303 *frame = mlt_frame_init( );
304
305 // Set the position
306 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
307
308 // Mark as a test card
309 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
310 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
311
312 // Calculate the next position
313 mlt_producer_prepare_next( this );
314 }
315 else
316 {
317 // Get the frame from the implementation
318 result = this->get_frame( this, frame, index );
319 }
320
321 // Copy the fps and speed of the producer onto the frame
322 mlt_properties properties = mlt_frame_properties( *frame );
323 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
324 double speed = mlt_producer_get_speed( this );
325 mlt_properties_set_double( properties, "_speed", speed );
326 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
327 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
328
329 return 0;
330 }
331
332 /** Attach a filter.
333 */
334
335 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
336 {
337 return mlt_service_attach( mlt_producer_service( this ), filter );
338 }
339
340 /** Detach a filter.
341 */
342
343 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
344 {
345 return mlt_service_detach( mlt_producer_service( this ), filter );
346 }
347
348 /** Retrieve a filter.
349 */
350
351 mlt_filter mlt_producer_filter( mlt_producer this, int index )
352 {
353 return mlt_service_filter( mlt_producer_service( this ), index );
354 }
355
356 /** Close the producer.
357 */
358
359 void mlt_producer_close( mlt_producer this )
360 {
361 if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
362 {
363 this->parent.close = NULL;
364
365 if ( this->close != NULL )
366 this->close( this->close_object );
367 else
368 mlt_service_close( &this->parent );
369 }
370 }