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