Constness changes
[melted] / src / framework / mlt_producer.c
1 /**
2 * \file mlt_producer.c
3 * \brief abstraction for all producer services
4 * \see mlt_producer_s
5 *
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "mlt_producer.h"
25 #include "mlt_factory.h"
26 #include "mlt_frame.h"
27 #include "mlt_parser.h"
28 #include "mlt_profile.h"
29 #include "mlt_log.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <math.h>
35
36 /* Forward references. */
37
38 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
39 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
40 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
41
42 /* for debugging */
43 //#define _MLT_PRODUCER_CHECKS_ 1
44 #ifdef _MLT_PRODUCER_CHECKS_
45 static int producers_created = 0;
46 static int producers_destroyed = 0;
47 #endif
48
49 /** Initialize a producer service.
50 *
51 * \public \memberof mlt_producer_s
52 * \param this the producer structure to initialize
53 * \param child a pointer to the child object for the subclass
54 * \return true if there was an error
55 */
56
57 int mlt_producer_init( mlt_producer this, void *child )
58 {
59 // Check that we haven't received NULL
60 int error = this == NULL;
61
62 // Continue if no error
63 if ( error == 0 )
64 {
65 #ifdef _MLT_PRODUCER_CHECKS_
66 producers_created ++;
67 #endif
68
69 // Initialise the producer
70 memset( this, 0, sizeof( struct mlt_producer_s ) );
71
72 // Associate with the child
73 this->child = child;
74
75 // Initialise the service
76 if ( mlt_service_init( &this->parent, this ) == 0 )
77 {
78 // The parent is the service
79 mlt_service parent = &this->parent;
80
81 // Define the parent close
82 parent->close = ( mlt_destructor )mlt_producer_close;
83 parent->close_object = this;
84
85 // For convenience, we'll assume the close_object is this
86 this->close_object = this;
87
88 // Get the properties of the parent
89 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
90
91 // Set the default properties
92 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
93 mlt_properties_set_position( properties, "_position", 0.0 );
94 mlt_properties_set_double( properties, "_frame", 0 );
95 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
96 mlt_properties_set_double( properties, "_speed", 1.0 );
97 mlt_properties_set_position( properties, "in", 0 );
98 mlt_properties_set_position( properties, "out", 14999 );
99 mlt_properties_set_position( properties, "length", 15000 );
100 mlt_properties_set( properties, "eof", "pause" );
101 mlt_properties_set( properties, "resource", "<producer>" );
102
103 // Override service get_frame
104 parent->get_frame = producer_get_frame;
105
106 mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
107 mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
108 mlt_events_register( properties, "producer-changed", NULL );
109 }
110 }
111
112 return error;
113 }
114
115 /** Listener for property changes.
116 *
117 * If the in, out, or length properties changed, fire a "producer-changed" event.
118 *
119 * \private \memberof mlt_producer_s
120 * \param owner a service (ignored)
121 * \param this the producer
122 * \param name the property that changed
123 */
124
125 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
126 {
127 if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
128 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
129 }
130
131 /** Listener for service changes.
132 *
133 * Fires the "producer-changed" event.
134 *
135 * \private \memberof mlt_producer_s
136 * \param owner a service (ignored)
137 * \param this the producer
138 */
139
140 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
141 {
142 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
143 }
144
145 /** Create and initialize a new producer.
146 *
147 * \public \memberof mlt_producer_s
148 * \return the new producer
149 */
150
151 mlt_producer mlt_producer_new( )
152 {
153 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
154 mlt_producer_init( this, NULL );
155 return this;
156 }
157
158 /** Determine if producer is a cut.
159 *
160 * \public \memberof mlt_producer_s
161 * \param this a producer
162 * \return true if \p this is a "cut" producer
163 * \see mlt_producer_cut
164 */
165
166 int mlt_producer_is_cut( mlt_producer this )
167 {
168 return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "_cut" );
169 }
170
171 /** Determine if producer is a mix.
172 *
173 * \public \memberof mlt_producer_s
174 * \param this a producer
175 * \return true if this is a "mix" producer
176 * \todo Define a mix producer.
177 */
178
179 int mlt_producer_is_mix( mlt_producer this )
180 {
181 mlt_properties properties = this != NULL ? MLT_PRODUCER_PROPERTIES( this ) : NULL;
182 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
183 return tractor != NULL;
184 }
185
186 /** Determine if the producer is a blank.
187 *
188 * Blank producers should only appear as an item in a playlist.
189 * \public \memberof mlt_producer_s
190 * \param this a producer
191 * \return true if this is a "blank" producer
192 * \see mlt_playlist_insert_blank
193 */
194
195 int mlt_producer_is_blank( mlt_producer this )
196 {
197 return this == NULL || !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
198 }
199
200 /** Obtain the parent producer.
201 *
202 * \public \memberof mlt_producer_s
203 * \param this a producer
204 * \return either the parent producer if this is a "cut" producer or \p this otherwise.
205 */
206
207 mlt_producer mlt_producer_cut_parent( mlt_producer this )
208 {
209 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
210 if ( mlt_producer_is_cut( this ) )
211 return mlt_properties_get_data( properties, "_cut_parent", NULL );
212 else
213 return this;
214 }
215
216 /** Create a cut of this producer.
217 *
218 * A "cut" is a portion of another (parent) producer.
219 *
220 * \public \memberof mlt_producer_s
221 * \param this a producer
222 * \param in the beginning
223 * \param out the end
224 * \return the new producer
225 * \todo Expand on the value of a cut.
226 */
227
228 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
229 {
230 mlt_producer result = mlt_producer_new( );
231 mlt_producer parent = mlt_producer_cut_parent( this );
232 mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
233 mlt_properties parent_props = MLT_PRODUCER_PROPERTIES( parent );
234
235 mlt_events_block( MLT_PRODUCER_PROPERTIES( result ), MLT_PRODUCER_PROPERTIES( result ) );
236 // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
237 if ( in <= 0 )
238 in = 0;
239 if ( ( out < 0 || out >= mlt_producer_get_length( parent ) ) && !mlt_producer_is_blank( this ) )
240 out = mlt_producer_get_length( parent ) - 1;
241
242 mlt_properties_inc_ref( parent_props );
243 mlt_properties_set_int( properties, "_cut", 1 );
244 mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
245 mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
246 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( parent_props, "aspect_ratio" ) );
247 mlt_producer_set_in_and_out( result, in, out );
248
249 return result;
250 }
251
252 /** Get the parent service object.
253 *
254 * \public \memberof mlt_producer_s
255 * \param this a producer
256 * \return the service parent class
257 * \see MLT_PRODUCER_SERVICE
258 */
259
260 mlt_service mlt_producer_service( mlt_producer this )
261 {
262 return this != NULL ? &this->parent : NULL;
263 }
264
265 /** Get the producer properties.
266 *
267 * \public \memberof mlt_producer_s
268 * \param this a producer
269 * \return the producer's property list
270 * \see MLT_PRODUCER_PROPERTIES
271 */
272
273 mlt_properties mlt_producer_properties( mlt_producer this )
274 {
275 return MLT_SERVICE_PROPERTIES( &this->parent );
276 }
277
278 /** Seek to a specified position.
279 *
280 * \public \memberof mlt_producer_s
281 * \param this a producer
282 * \param position set the "play head" position of the producer
283 * \return false
284 * \todo Document how the properties affect behavior.
285 */
286
287 int mlt_producer_seek( mlt_producer this, mlt_position position )
288 {
289 // Determine eof handling
290 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
291 char *eof = mlt_properties_get( properties, "eof" );
292 int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
293
294 // Recursive behaviour for cuts - repositions parent and then repositions cut
295 // hence no return on this condition
296 if ( mlt_producer_is_cut( this ) )
297 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
298
299 // Check bounds
300 if ( position < 0 || mlt_producer_get_playtime( this ) == 0 )
301 {
302 position = 0;
303 }
304 else if ( use_points && ( eof == NULL || !strcmp( eof, "pause" ) ) && position >= mlt_producer_get_playtime( this ) )
305 {
306 mlt_producer_set_speed( this, 0 );
307 position = mlt_producer_get_playtime( this ) - 1;
308 }
309 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
310 {
311 position = (int)position % (int)mlt_producer_get_playtime( this );
312 }
313
314 // Set the position
315 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_position", position );
316
317 // Calculate the absolute frame
318 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
319
320 return 0;
321 }
322
323 /** Get the current position (relative to in point).
324 *
325 * \public \memberof mlt_producer_s
326 * \param this a producer
327 * \return the position of the "play head" relative to its beginning
328 */
329
330 mlt_position mlt_producer_position( mlt_producer this )
331 {
332 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_position" );
333 }
334
335 /** Get the current position (relative to start of producer).
336 *
337 * \public \memberof mlt_producer_s
338 * \param this a producer
339 * \return the position of the "play head" regardless of the in point
340 */
341
342 mlt_position mlt_producer_frame( mlt_producer this )
343 {
344 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_frame" );
345 }
346
347 /** Set the playing speed.
348 *
349 * \public \memberof mlt_producer_s
350 * \param this a producer
351 * \param speed the new speed as a relative factor (1.0 = normal)
352 * \return
353 */
354
355 int mlt_producer_set_speed( mlt_producer this, double speed )
356 {
357 return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( this ), "_speed", speed );
358 }
359
360 /** Get the playing speed.
361 *
362 * \public \memberof mlt_producer_s
363 * \param this a producer
364 * \return the speed as a relative factor (1.0 = normal)
365 */
366
367 double mlt_producer_get_speed( mlt_producer this )
368 {
369 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "_speed" );
370 }
371
372 /** Get the frames per second.
373 *
374 * This is determined by the producer's profile.
375 *
376 * \public \memberof mlt_producer_s
377 * \param this a producer
378 * \return the video refresh rate
379 */
380
381 double mlt_producer_get_fps( mlt_producer this )
382 {
383 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
384 return mlt_profile_fps( profile );
385 }
386
387 /** Set the in and out points.
388 *
389 * The in point is where play out should start relative to the natural start
390 * of the underlying file. The out point is where play out should end, also
391 * relative to the start of the underlying file. If the underlying resource is
392 * a live stream, then the in point is an offset relative to first usable
393 * sample.
394 *
395 * \public \memberof mlt_producer_s
396 * \param this a producer
397 * \param in the relative starting time
398 * \param out the relative ending time
399 * \return false
400 */
401
402 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
403 {
404 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
405
406 // Correct ins and outs if necessary
407 if ( in < 0 )
408 in = 0;
409 else if ( in >= mlt_producer_get_length( this ) )
410 in = mlt_producer_get_length( this ) - 1;
411
412 if ( out < 0 )
413 out = 0;
414 else if ( out >= mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
415 out = mlt_producer_get_length( this ) - 1;
416 else if ( out >= mlt_producer_get_length( this ) && mlt_producer_is_blank( this ) )
417 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "length", out + 1 );
418
419 // Swap ins and outs if wrong
420 if ( out < in )
421 {
422 mlt_position t = in;
423 in = out;
424 out = t;
425 }
426
427 // Set the values
428 mlt_events_block( properties, properties );
429 mlt_properties_set_position( properties, "in", in );
430 mlt_events_unblock( properties, properties );
431 mlt_properties_set_position( properties, "out", out );
432
433 return 0;
434 }
435
436 /** Physically reduce the producer (typically a cut) to a 0 length.
437 * Essentially, all 0 length cuts should be immediately removed by containers.
438 *
439 * \public \memberof mlt_producer_s
440 * \param this a producer
441 * \return false
442 */
443
444 int mlt_producer_clear( mlt_producer this )
445 {
446 if ( this != NULL )
447 {
448 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
449 mlt_events_block( properties, properties );
450 mlt_properties_set_position( properties, "in", 0 );
451 mlt_events_unblock( properties, properties );
452 mlt_properties_set_position( properties, "out", -1 );
453 }
454 return 0;
455 }
456
457 /** Get the in point.
458 *
459 * \public \memberof mlt_producer_s
460 * \param this a producer
461 * \return the in point
462 */
463
464 mlt_position mlt_producer_get_in( mlt_producer this )
465 {
466 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "in" );
467 }
468
469 /** Get the out point.
470 *
471 * \public \memberof mlt_producer_s
472 * \param this a producer
473 * \return the out point
474 */
475
476 mlt_position mlt_producer_get_out( mlt_producer this )
477 {
478 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "out" );
479 }
480
481 /** Get the total play time.
482 *
483 * \public \memberof mlt_producer_s
484 * \param this a producer
485 * \return the playable (based on in and out points) duration
486 */
487
488 mlt_position mlt_producer_get_playtime( mlt_producer this )
489 {
490 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
491 }
492
493 /** Get the total, unedited length of the producer.
494 *
495 * The value returned by a live streaming producer is unknown.
496 *
497 * \public \memberof mlt_producer_s
498 * \param this a producer
499 * \return the duration of the producer regardless of in and out points
500 */
501
502 mlt_position mlt_producer_get_length( mlt_producer this )
503 {
504 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "length" );
505 }
506
507 /** Prepare for next frame.
508 *
509 * Advance the play out position. If the speed is less than zero, it will
510 * move the play out position in the reverse direction.
511 *
512 * \public \memberof mlt_producer_s
513 * \param this a producer
514 */
515
516 void mlt_producer_prepare_next( mlt_producer this )
517 {
518 if ( mlt_producer_get_speed( this ) != 0 )
519 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
520 }
521
522 /** Get a frame.
523 *
524 * This is the implementation of the \p get_frame virtual function.
525 * It requests a new frame object from the actual producer for the current
526 * play out position. The producer and its filters can add information and
527 * operations to the frame object in their get_frame handlers.
528 *
529 * \private \memberof mlt_producer_s
530 * \param service a service
531 * \param[out] frame a frame by reference
532 * \param index as determined by the actual producer
533 * \return true if there was an error
534 * \todo Learn more about the details and document how certain properties affect
535 * its behavior.
536 */
537
538 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
539 {
540 int result = 1;
541 mlt_producer this = service != NULL ? service->child : NULL;
542
543 if ( this != NULL && !mlt_producer_is_cut( this ) )
544 {
545 // Get the properties of this producer
546 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
547
548 // Determine eof handling
549 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( this ), "eof" );
550
551 // Get the speed of the producer
552 double speed = mlt_producer_get_speed( this );
553
554 // We need to use the clone if it's specified
555 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
556
557 // If no clone is specified, use this
558 clone = clone == NULL ? this : clone;
559
560 // A properly instatiated producer will have a get_frame method...
561 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
562 {
563 // Generate a test frame
564 *frame = mlt_frame_init( service );
565
566 // Set the position
567 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
568
569 // Mark as a test card
570 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 1 );
571 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", 1 );
572
573 // Calculate the next position
574 mlt_producer_prepare_next( this );
575 }
576 else
577 {
578 // Get the frame from the implementation
579 result = this->get_frame( clone, frame, index );
580 }
581
582 // Copy the fps and speed of the producer onto the frame
583 properties = MLT_FRAME_PROPERTIES( *frame );
584 mlt_properties_set_double( properties, "_speed", speed );
585 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
586 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
587 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
588 mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
589 }
590 else if ( this != NULL )
591 {
592 // Get the speed of the cut
593 double speed = mlt_producer_get_speed( this );
594
595 // Get the parent of this cut
596 mlt_producer parent = mlt_producer_cut_parent( this );
597
598 // Get the properties of the parent
599 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
600
601 // Get the properties of the cut
602 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
603
604 // Determine the clone index
605 int clone_index = mlt_properties_get_int( properties, "_clone" );
606
607 // Determine the clone to use
608 mlt_producer clone = this;
609
610 if ( clone_index > 0 )
611 {
612 char key[ 25 ];
613 sprintf( key, "_clone.%d", clone_index - 1 );
614 clone = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), key, NULL );
615 if ( clone == NULL ) mlt_log( service, MLT_LOG_ERROR, "requested clone doesn't exist %d\n", clone_index );
616 clone = clone == NULL ? this : clone;
617 }
618 else
619 {
620 clone = parent;
621 }
622
623 // We need to seek to the correct position in the clone
624 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
625
626 // Assign the clone property to the parent
627 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
628
629 // Now get the frame from the parents service
630 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
631
632 // We're done with the clone now
633 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
634
635 // This is useful and required by always_active transitions to determine in/out points of the cut
636 if ( mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", NULL ) == MLT_PRODUCER_SERVICE( parent ) )
637 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", this, 0, NULL, NULL );
638
639 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
640 mlt_producer_prepare_next( this );
641 }
642 else
643 {
644 *frame = mlt_frame_init( service );
645 result = 0;
646 }
647
648 // Pass on all meta properties from the producer/cut on to the frame
649 if ( *frame != NULL && this != NULL )
650 {
651 int i = 0;
652 mlt_properties p_props = MLT_PRODUCER_PROPERTIES( this );
653 mlt_properties f_props = MLT_FRAME_PROPERTIES( *frame );
654 int count = mlt_properties_count( p_props );
655 for ( i = 0; i < count; i ++ )
656 {
657 char *name = mlt_properties_get_name( p_props, i );
658 if ( !strncmp( name, "meta.", 5 ) )
659 mlt_properties_set( f_props, name, mlt_properties_get( p_props, name ) );
660 else if ( !strncmp( name, "set.", 4 ) )
661 mlt_properties_set( f_props, name + 4, mlt_properties_get( p_props, name ) );
662 }
663 }
664
665 return result;
666 }
667
668 /** Attach a filter.
669 *
670 * \public \memberof mlt_producer_s
671 * \param this a producer
672 * \param filter the filter to attach
673 * \return true if there was an error
674 */
675
676 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
677 {
678 return mlt_service_attach( MLT_PRODUCER_SERVICE( this ), filter );
679 }
680
681 /** Detach a filter.
682 *
683 * \public \memberof mlt_producer_s
684 * \param this a service
685 * \param filter the filter to detach
686 * \return true if there was an error
687 */
688
689 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
690 {
691 return mlt_service_detach( MLT_PRODUCER_SERVICE( this ), filter );
692 }
693
694 /** Retrieve a filter.
695 *
696 * \public \memberof mlt_producer_s
697 * \param this a service
698 * \param index which filter to retrieve
699 * \return the filter or null if there was an error
700 */
701
702 mlt_filter mlt_producer_filter( mlt_producer this, int index )
703 {
704 return mlt_service_filter( MLT_PRODUCER_SERVICE( this ), index );
705 }
706
707 /** Clone this producer.
708 *
709 * \private \memberof mlt_producer_s
710 * \param this a producer
711 * \return a new producer that is a copy of \p this
712 * \see mlt_producer_set_clones
713 */
714
715 static mlt_producer mlt_producer_clone( mlt_producer this )
716 {
717 mlt_producer clone = NULL;
718 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
719 char *resource = mlt_properties_get( properties, "resource" );
720 char *service = mlt_properties_get( properties, "mlt_service" );
721 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( this ) );
722
723 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
724
725 if ( service != NULL )
726 clone = mlt_factory_producer( profile, service, resource );
727
728 if ( clone == NULL && resource != NULL )
729 clone = mlt_factory_producer( profile, mlt_environment( "MLT_PRODUCER" ), resource );
730
731 if ( clone != NULL )
732 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
733
734 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
735
736 return clone;
737 }
738
739 /** Create clones.
740 *
741 * \private \memberof mlt_producer_s
742 * \param this a producer
743 * \param clones the number of copies to make
744 * \see mlt_producer_optimise
745 */
746
747 static void mlt_producer_set_clones( mlt_producer this, int clones )
748 {
749 mlt_producer parent = mlt_producer_cut_parent( this );
750 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
751 int existing = mlt_properties_get_int( properties, "_clones" );
752 int i = 0;
753 char key[ 25 ];
754
755 // If the number of existing clones is different, then create/remove as necessary
756 if ( existing != clones )
757 {
758 if ( existing < clones )
759 {
760 for ( i = existing; i < clones; i ++ )
761 {
762 mlt_producer clone = mlt_producer_clone( parent );
763 sprintf( key, "_clone.%d", i );
764 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
765 }
766 }
767 else
768 {
769 for ( i = clones; i < existing; i ++ )
770 {
771 sprintf( key, "_clone.%d", i );
772 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
773 }
774 }
775 }
776
777 // Ensure all properties on the parent are passed to the clones
778 for ( i = 0; i < clones; i ++ )
779 {
780 mlt_producer clone = NULL;
781 sprintf( key, "_clone.%d", i );
782 clone = mlt_properties_get_data( properties, key, NULL );
783 if ( clone != NULL )
784 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
785 }
786
787 // Update the number of clones on the properties
788 mlt_properties_set_int( properties, "_clones", clones );
789 }
790
791 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
792
793 typedef struct
794 {
795 int multitrack;
796 int track;
797 int position;
798 int length;
799 int offset;
800 }
801 track_info;
802
803 /** \brief private to mlt_producer_s, used by mlt_producer_optimise() */
804
805 typedef struct
806 {
807 mlt_producer cut;
808 int start;
809 int end;
810 }
811 clip_references;
812
813 static int intersect( clip_references *a, clip_references *b )
814 {
815 int diff = ( a->start - b->start ) + ( a->end - b->end );
816 return diff >= 0 && diff < ( a->end - a->start + 1 );
817 }
818
819 static int push( mlt_parser this, int multitrack, int track, int position )
820 {
821 mlt_properties properties = mlt_parser_properties( this );
822 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
823 track_info *info = malloc( sizeof( track_info ) );
824 info->multitrack = multitrack;
825 info->track = track;
826 info->position = position;
827 info->length = 0;
828 info->offset = 0;
829 return mlt_deque_push_back( stack, info );
830 }
831
832 static track_info *pop( mlt_parser this )
833 {
834 mlt_properties properties = mlt_parser_properties( this );
835 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
836 return mlt_deque_pop_back( stack );
837 }
838
839 static track_info *peek( mlt_parser this )
840 {
841 mlt_properties properties = mlt_parser_properties( this );
842 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
843 return mlt_deque_peek_back( stack );
844 }
845
846 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
847 {
848 track_info *info = peek( this );
849 return push( this, info->multitrack ++, info->track, info->position );
850 }
851
852 static int on_start_track( mlt_parser this )
853 {
854 track_info *info = peek( this );
855 info->position -= info->offset;
856 info->length -= info->offset;
857 return push( this, info->multitrack, info->track ++, info->position );
858 }
859
860 static int on_start_producer( mlt_parser this, mlt_producer object )
861 {
862 mlt_properties properties = mlt_parser_properties( this );
863 mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
864 mlt_producer parent = mlt_producer_cut_parent( object );
865 if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
866 {
867 int ref_count = 0;
868 clip_references *old_refs = NULL;
869 clip_references *refs = NULL;
870 char key[ 50 ];
871 int count = 0;
872 track_info *info = peek( this );
873 sprintf( key, "%p", parent );
874 mlt_properties_get_data( producers, key, &count );
875 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
876 old_refs = mlt_properties_get_data( properties, key, &ref_count );
877 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
878 if ( old_refs != NULL )
879 memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
880 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object ), "_clone", -1 );
881 refs[ ref_count ].cut = object;
882 refs[ ref_count ].start = info->position;
883 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
884 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
885 info->position += mlt_producer_get_playtime( object );
886 info->length += mlt_producer_get_playtime( object );
887 }
888 return 0;
889 }
890
891 static int on_end_track( mlt_parser this )
892 {
893 track_info *track = pop( this );
894 track_info *multi = peek( this );
895 multi->length += track->length;
896 multi->position += track->length;
897 multi->offset = track->length;
898 free( track );
899 return 0;
900 }
901
902 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
903 {
904 track_info *multi = pop( this );
905 track_info *track = peek( this );
906 track->position += multi->length;
907 track->length += multi->length;
908 free( multi );
909 return 0;
910 }
911
912 /** Optimise for overlapping cuts from the same clip.
913 *
914 * \todo learn more about this
915 * \public \memberof mlt_producer_s
916 * \param this a producer
917 * \return true if there was an error
918 */
919
920 int mlt_producer_optimise( mlt_producer this )
921 {
922 int error = 1;
923 mlt_parser parser = mlt_parser_new( );
924 if ( parser != NULL )
925 {
926 int i = 0, j = 0, k = 0;
927 mlt_properties properties = mlt_parser_properties( parser );
928 mlt_properties producers = mlt_properties_new( );
929 mlt_deque stack = mlt_deque_init( );
930 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
931 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
932 parser->on_start_producer = on_start_producer;
933 parser->on_start_track = on_start_track;
934 parser->on_end_track = on_end_track;
935 parser->on_start_multitrack = on_start_multitrack;
936 parser->on_end_multitrack = on_end_multitrack;
937 push( parser, 0, 0, 0 );
938 mlt_parser_start( parser, MLT_PRODUCER_SERVICE( this ) );
939 free( pop( parser ) );
940 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
941 {
942 char *name = mlt_properties_get_name( producers, k );
943 int count = 0;
944 int clones = 0;
945 int max_clones = 0;
946 mlt_producer producer = mlt_properties_get_data( producers, name, &count );
947 if ( producer != NULL && count > 1 )
948 {
949 clip_references *refs = mlt_properties_get_data( properties, name, &count );
950 for ( i = 0; i < count; i ++ )
951 {
952 clones = 0;
953 for ( j = i + 1; j < count; j ++ )
954 {
955 if ( intersect( &refs[ i ], &refs[ j ] ) )
956 {
957 clones ++;
958 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
959 }
960 }
961 if ( clones > max_clones )
962 max_clones = clones;
963 }
964
965 for ( i = 0; i < count; i ++ )
966 {
967 mlt_producer cut = refs[ i ].cut;
968 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone" ) == -1 )
969 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
970 }
971
972 mlt_producer_set_clones( producer, max_clones );
973 }
974 else if ( producer != NULL )
975 {
976 clip_references *refs = mlt_properties_get_data( properties, name, &count );
977 for ( i = 0; i < count; i ++ )
978 {
979 mlt_producer cut = refs[ i ].cut;
980 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
981 }
982 mlt_producer_set_clones( producer, 0 );
983 }
984 }
985 mlt_parser_close( parser );
986 }
987 return error;
988 }
989
990 /** Close the producer.
991 *
992 * Destroys the producer and deallocates its resources managed by its
993 * properties list. This will call the close virtual function. Therefore, a
994 * subclass that defines its own close function should set its virtual close
995 * function to NULL prior to calling this to avoid circular calls.
996 *
997 * \public \memberof mlt_producer_s
998 * \param this a producer
999 */
1000
1001 void mlt_producer_close( mlt_producer this )
1002 {
1003 if ( this != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( this ) ) <= 0 )
1004 {
1005 this->parent.close = NULL;
1006
1007 if ( this->close != NULL )
1008 {
1009 this->close( this->close_object );
1010 }
1011 else
1012 {
1013 int destroy = mlt_producer_is_cut( this );
1014
1015 #if _MLT_PRODUCER_CHECKS_ == 1
1016 // Show debug info
1017 mlt_properties_debug( MLT_PRODUCER_PROPERTIES( this ), "Producer closing", stderr );
1018 #endif
1019
1020 #ifdef _MLT_PRODUCER_CHECKS_
1021 // Show current stats - these should match when the app is closed
1022 mlt_log( MLT_PRODUCER_SERVICE( this ), MLT_LOG_DEBUG, "Producers created %d, destroyed %d\n", producers_created, ++producers_destroyed );
1023 #endif
1024
1025 mlt_service_close( &this->parent );
1026
1027 if ( destroy )
1028 free( this );
1029 }
1030 }
1031 }