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