Mutex locking in the get frame
[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 "mlt_parser.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <math.h>
30
31 /** Forward references.
32 */
33
34 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
35 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
36 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
37
38 //#define _MLT_PRODUCER_CHECKS_ 1
39
40 #ifdef _MLT_PRODUCER_CHECKS_
41 static int producers_created = 0;
42 static int producers_destroyed = 0;
43 #endif
44
45 /** Constructor
46 */
47
48 int mlt_producer_init( mlt_producer this, void *child )
49 {
50 // Check that we haven't received NULL
51 int error = this == NULL;
52
53 // Continue if no error
54 if ( error == 0 )
55 {
56 #ifdef _MLT_PRODUCER_CHECKS_
57 producers_created ++;
58 #endif
59
60 // Initialise the producer
61 memset( this, 0, sizeof( struct mlt_producer_s ) );
62
63 // Associate with the child
64 this->child = child;
65
66 // Initialise the service
67 if ( mlt_service_init( &this->parent, this ) == 0 )
68 {
69 // Get the normalisation preference
70 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
71
72 // The parent is the service
73 mlt_service parent = &this->parent;
74
75 // Define the parent close
76 parent->close = ( mlt_destructor )mlt_producer_close;
77 parent->close_object = this;
78
79 // For convenience, we'll assume the close_object is this
80 this->close_object = this;
81
82 // Get the properties of the parent
83 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
84
85 // Set the default properties
86 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
87 mlt_properties_set_position( properties, "_position", 0.0 );
88 mlt_properties_set_double( properties, "_frame", 0 );
89 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
90 {
91 mlt_properties_set_double( properties, "fps", 25.0 );
92 mlt_properties_set_double( properties, "aspect_ratio", 59.0 / 54.0 );
93 }
94 else
95 {
96 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
97 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
98 }
99 mlt_properties_set_double( properties, "_speed", 1.0 );
100 mlt_properties_set_position( properties, "in", 0 );
101 mlt_properties_set_position( properties, "out", 14999 );
102 mlt_properties_set_position( properties, "length", 15000 );
103 mlt_properties_set( properties, "eof", "pause" );
104 mlt_properties_set( properties, "resource", "<producer>" );
105
106 // Override service get_frame
107 parent->get_frame = producer_get_frame;
108
109 mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
110 mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
111 mlt_events_register( properties, "producer-changed", NULL );
112 }
113 }
114
115 return error;
116 }
117
118 /** Listener for property changes.
119 */
120
121 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
122 {
123 if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
124 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
125 }
126
127 /** Listener for service changes.
128 */
129
130 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
131 {
132 mlt_events_fire( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "producer-changed", NULL );
133 }
134
135 /** Create a new producer.
136 */
137
138 mlt_producer mlt_producer_new( )
139 {
140 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
141 mlt_producer_init( this, NULL );
142 return this;
143 }
144
145 /** Determine if producer is a cut.
146 */
147
148 int mlt_producer_is_cut( mlt_producer this )
149 {
150 return mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( this ), "_cut" );
151 }
152
153 /** Determine if producer is a mix.
154 */
155
156 int mlt_producer_is_mix( mlt_producer this )
157 {
158 mlt_properties properties = this != NULL ? MLT_PRODUCER_PROPERTIES( this ) : NULL;
159 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
160 return tractor != NULL;
161 }
162
163 /** Determine if the producer is a blank [from a playlist].
164 */
165
166 int mlt_producer_is_blank( mlt_producer this )
167 {
168 return this == NULL || !strcmp( mlt_properties_get( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), "resource" ), "blank" );
169 }
170
171 /** Obtain the parent producer.
172 */
173
174 mlt_producer mlt_producer_cut_parent( mlt_producer this )
175 {
176 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
177 if ( mlt_producer_is_cut( this ) )
178 return mlt_properties_get_data( properties, "_cut_parent", NULL );
179 else
180 return this;
181 }
182
183 /** Create a cut of this producer
184 */
185
186 mlt_producer mlt_producer_cut( mlt_producer this, int in, int out )
187 {
188 mlt_producer result = mlt_producer_new( );
189 mlt_producer parent = mlt_producer_cut_parent( this );
190 mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
191 mlt_properties parent_props = MLT_PRODUCER_PROPERTIES( parent );
192
193 mlt_events_block( MLT_PRODUCER_PROPERTIES( result ), MLT_PRODUCER_PROPERTIES( result ) );
194 // Special case - allow for a cut of the entire producer (this will squeeze all other cuts to 0)
195 if ( in <= 0 )
196 in = 0;
197 if ( ( out < 0 || out >= mlt_producer_get_playtime( parent ) ) && !mlt_producer_is_blank( this ) )
198 out = mlt_producer_get_playtime( parent ) - 1;
199
200 mlt_properties_inc_ref( parent_props );
201 mlt_properties_set_int( properties, "_cut", 1 );
202 mlt_properties_set_data( properties, "_cut_parent", parent, 0, ( mlt_destructor )mlt_producer_close, NULL );
203 mlt_properties_set_position( properties, "length", mlt_properties_get_position( parent_props, "length" ) );
204 mlt_producer_set_in_and_out( result, in, out );
205
206 // Mini fezzik :-/
207 mlt_filter filter = mlt_factory_filter( "data_feed", "attr_check" );
208 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik", 1 );
209 mlt_producer_attach( result, filter );
210 mlt_filter_close( filter );
211 mlt_events_unblock( MLT_PRODUCER_PROPERTIES( result ), MLT_PRODUCER_PROPERTIES( result ) );
212
213 return result;
214 }
215
216 /** Get the parent service object.
217 */
218
219 mlt_service mlt_producer_service( mlt_producer this )
220 {
221 return this != NULL ? &this->parent : NULL;
222 }
223
224 /** Get the producer properties.
225 */
226
227 mlt_properties mlt_producer_properties( mlt_producer this )
228 {
229 return MLT_SERVICE_PROPERTIES( &this->parent );
230 }
231
232 /** Seek to a specified position.
233 */
234
235 int mlt_producer_seek( mlt_producer this, mlt_position position )
236 {
237 // Determine eof handling
238 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
239 char *eof = mlt_properties_get( properties, "eof" );
240 int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
241
242 // Recursive behaviour for cuts - repositions parent and then repositions cut
243 // hence no return on this condition
244 if ( mlt_producer_is_cut( this ) )
245 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
246
247 // Check bounds
248 if ( position < 0 )
249 {
250 position = 0;
251 }
252 else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
253 {
254 mlt_producer_set_speed( this, 0 );
255 position = mlt_producer_get_playtime( this ) - 1;
256 }
257 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
258 {
259 position = position % mlt_producer_get_playtime( this );
260 }
261
262 // Set the position
263 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_position", position );
264
265 // Calculate the absolute frame
266 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
267
268 return 0;
269 }
270
271 /** Get the current position (relative to in point).
272 */
273
274 mlt_position mlt_producer_position( mlt_producer this )
275 {
276 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_position" );
277 }
278
279 /** Get the current position (relative to start of producer).
280 */
281
282 mlt_position mlt_producer_frame( mlt_producer this )
283 {
284 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_frame" );
285 }
286
287 /** Set the playing speed.
288 */
289
290 int mlt_producer_set_speed( mlt_producer this, double speed )
291 {
292 return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( this ), "_speed", speed );
293 }
294
295 /** Get the playing speed.
296 */
297
298 double mlt_producer_get_speed( mlt_producer this )
299 {
300 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "_speed" );
301 }
302
303 /** Get the frames per second.
304 */
305
306 double mlt_producer_get_fps( mlt_producer this )
307 {
308 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "fps" );
309 }
310
311 /** Set the in and out points.
312 */
313
314 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
315 {
316 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
317
318 // Correct ins and outs if necessary
319 if ( in < 0 )
320 in = 0;
321 else if ( in >= mlt_producer_get_length( this ) )
322 in = mlt_producer_get_length( this ) - 1;
323
324 if ( out < 0 )
325 out = 0;
326 else if ( out >= mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
327 out = mlt_producer_get_length( this ) - 1;
328 else if ( out >= mlt_producer_get_length( this ) && mlt_producer_is_blank( this ) )
329 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "length", out + 1 );
330
331 // Swap ins and outs if wrong
332 if ( out < in )
333 {
334 mlt_position t = in;
335 in = out;
336 out = t;
337 }
338
339 // Set the values
340 mlt_events_block( properties, properties );
341 mlt_properties_set_position( properties, "in", in );
342 mlt_events_unblock( properties, properties );
343 mlt_properties_set_position( properties, "out", out );
344
345 return 0;
346 }
347
348 /** Physically reduce the producer (typically a cut) to a 0 length.
349 Essentially, all 0 length cuts should be immediately removed by containers.
350 */
351
352 int mlt_producer_clear( mlt_producer this )
353 {
354 if ( this != NULL )
355 {
356 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
357 mlt_events_block( properties, properties );
358 mlt_properties_set_position( properties, "in", 0 );
359 mlt_events_unblock( properties, properties );
360 mlt_properties_set_position( properties, "out", -1 );
361 }
362 return 0;
363 }
364
365 /** Get the in point.
366 */
367
368 mlt_position mlt_producer_get_in( mlt_producer this )
369 {
370 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "in" );
371 }
372
373 /** Get the out point.
374 */
375
376 mlt_position mlt_producer_get_out( mlt_producer this )
377 {
378 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "out" );
379 }
380
381 /** Get the total play time.
382 */
383
384 mlt_position mlt_producer_get_playtime( mlt_producer this )
385 {
386 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
387 }
388
389 /** Get the total length of the producer.
390 */
391
392 mlt_position mlt_producer_get_length( mlt_producer this )
393 {
394 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "length" );
395 }
396
397 /** Prepare for next frame.
398 */
399
400 void mlt_producer_prepare_next( mlt_producer this )
401 {
402 if ( mlt_producer_get_speed( this ) != 0 )
403 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
404 }
405
406 /** Get a frame.
407 */
408
409 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
410 {
411 int result = 1;
412 mlt_producer this = service != NULL ? service->child : NULL;
413
414 if ( this != NULL && !mlt_producer_is_cut( this ) )
415 {
416 // Get the properties of this producer
417 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
418
419 // Determine eof handling
420 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( this ), "eof" );
421
422 // Get the speed of the producer
423 double speed = mlt_producer_get_speed( this );
424
425 // We need to use the clone if it's specified
426 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
427
428 // If no clone is specified, use this
429 clone = clone == NULL ? this : clone;
430
431 // A properly instatiated producer will have a get_frame method...
432 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
433 {
434 // Generate a test frame
435 *frame = mlt_frame_init( );
436
437 // Set the position
438 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
439
440 // Mark as a test card
441 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 1 );
442 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", 1 );
443
444 // Calculate the next position
445 mlt_producer_prepare_next( this );
446 }
447 else
448 {
449 // Get the frame from the implementation
450 result = this->get_frame( clone, frame, index );
451 }
452
453 // Copy the fps and speed of the producer onto the frame
454 properties = MLT_FRAME_PROPERTIES( *frame );
455 mlt_properties_set_double( properties, "_speed", speed );
456 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
457 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
458 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
459 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
460 mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
461 }
462 else if ( this != NULL )
463 {
464 // Get the speed of the cut
465 double speed = mlt_producer_get_speed( this );
466
467 // Get the parent of this cut
468 mlt_producer parent = mlt_producer_cut_parent( this );
469
470 // Get the properties of the parent
471 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
472
473 // Get the properties of the cut
474 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
475
476 // Determine the clone index
477 int clone_index = mlt_properties_get_int( properties, "_clone" );
478
479 // Determine the clone to use
480 mlt_producer clone = this;
481
482 if ( clone_index > 0 )
483 {
484 char key[ 25 ];
485 sprintf( key, "_clone.%d", clone_index - 1 );
486 clone = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), key, NULL );
487 if ( clone == NULL ) fprintf( stderr, "requested clone doesn't exist %d\n", clone_index );
488 clone = clone == NULL ? this : clone;
489 }
490 else
491 {
492 clone = parent;
493 }
494
495 // We need to seek to the correct position in the clone
496 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
497
498 // Assign the clone property to the parent
499 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
500
501 // Now get the frame from the parents service
502 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
503
504 // We're done with the clone now
505 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
506
507 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
508 mlt_producer_prepare_next( this );
509 }
510 else
511 {
512 *frame = mlt_frame_init( );
513 result = 0;
514 }
515
516 // Pass on all meta properties from the producer/cut on to the frame
517 if ( *frame != NULL && this != NULL )
518 {
519 int i = 0;
520 mlt_properties p_props = MLT_PRODUCER_PROPERTIES( this );
521 mlt_properties f_props = MLT_FRAME_PROPERTIES( *frame );
522 int count = mlt_properties_count( p_props );
523 for ( i = 0; i < count; i ++ )
524 {
525 char *name = mlt_properties_get_name( p_props, i );
526 if ( !strncmp( name, "meta.", 5 ) )
527 mlt_properties_set( f_props, name, mlt_properties_get( p_props, name ) );
528 }
529 }
530
531 return result;
532 }
533
534 /** Attach a filter.
535 */
536
537 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
538 {
539 return mlt_service_attach( MLT_PRODUCER_SERVICE( this ), filter );
540 }
541
542 /** Detach a filter.
543 */
544
545 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
546 {
547 return mlt_service_detach( MLT_PRODUCER_SERVICE( this ), filter );
548 }
549
550 /** Retrieve a filter.
551 */
552
553 mlt_filter mlt_producer_filter( mlt_producer this, int index )
554 {
555 return mlt_service_filter( MLT_PRODUCER_SERVICE( this ), index );
556 }
557
558 /** Clone this producer.
559 */
560
561 static mlt_producer mlt_producer_clone( mlt_producer this )
562 {
563 mlt_producer clone = NULL;
564 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
565 char *resource = mlt_properties_get( properties, "resource" );
566 char *service = mlt_properties_get( properties, "mlt_service" );
567
568 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
569
570 if ( service != NULL )
571 clone = mlt_factory_producer( service, resource );
572
573 if ( clone == NULL && resource != NULL )
574 clone = mlt_factory_producer( "fezzik", resource );
575
576 if ( clone != NULL )
577 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
578
579 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
580
581 return clone;
582 }
583
584 /** Create clones.
585 */
586
587 static void mlt_producer_set_clones( mlt_producer this, int clones )
588 {
589 mlt_producer parent = mlt_producer_cut_parent( this );
590 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
591 int existing = mlt_properties_get_int( properties, "_clones" );
592 int i = 0;
593 char key[ 25 ];
594
595 // If the number of existing clones is different, then create/remove as necessary
596 if ( existing != clones )
597 {
598 if ( existing < clones )
599 {
600 for ( i = existing; i < clones; i ++ )
601 {
602 mlt_producer clone = mlt_producer_clone( parent );
603 sprintf( key, "_clone.%d", i );
604 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
605 }
606 }
607 else
608 {
609 for ( i = clones; i < existing; i ++ )
610 {
611 sprintf( key, "_clone.%d", i );
612 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
613 }
614 }
615 }
616
617 // Ensure all properties on the parent are passed to the clones
618 for ( i = 0; i < clones; i ++ )
619 {
620 mlt_producer clone = NULL;
621 sprintf( key, "_clone.%d", i );
622 clone = mlt_properties_get_data( properties, key, NULL );
623 if ( clone != NULL )
624 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
625 }
626
627 // Update the number of clones on the properties
628 mlt_properties_set_int( properties, "_clones", clones );
629 }
630
631 /** Optimise for overlapping cuts from the same clip.
632 */
633
634 typedef struct
635 {
636 int multitrack;
637 int track;
638 int position;
639 int length;
640 int offset;
641 }
642 track_info;
643
644 typedef struct
645 {
646 mlt_producer cut;
647 int start;
648 int end;
649 }
650 clip_references;
651
652 static int intersect( clip_references *a, clip_references *b )
653 {
654 int diff = ( a->start - b->start ) + ( a->end - b->end );
655 return diff >= 0 && diff < ( a->end - a->start + 1 );
656 }
657
658 static int push( mlt_parser this, int multitrack, int track, int position )
659 {
660 mlt_properties properties = mlt_parser_properties( this );
661 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
662 track_info *info = malloc( sizeof( track_info ) );
663 info->multitrack = multitrack;
664 info->track = track;
665 info->position = position;
666 info->length = 0;
667 info->offset = 0;
668 return mlt_deque_push_back( stack, info );
669 }
670
671 static track_info *pop( mlt_parser this )
672 {
673 mlt_properties properties = mlt_parser_properties( this );
674 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
675 return mlt_deque_pop_back( stack );
676 }
677
678 static track_info *peek( mlt_parser this )
679 {
680 mlt_properties properties = mlt_parser_properties( this );
681 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
682 return mlt_deque_peek_back( stack );
683 }
684
685 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
686 {
687 track_info *info = peek( this );
688 return push( this, info->multitrack ++, info->track, info->position );
689 }
690
691 static int on_start_track( mlt_parser this )
692 {
693 track_info *info = peek( this );
694 info->position -= info->offset;
695 info->length -= info->offset;
696 return push( this, info->multitrack, info->track ++, info->position );
697 }
698
699 static int on_start_producer( mlt_parser this, mlt_producer object )
700 {
701 mlt_properties properties = mlt_parser_properties( this );
702 mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
703 mlt_producer parent = mlt_producer_cut_parent( object );
704 if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
705 {
706 int ref_count = 0;
707 clip_references *old_refs = NULL;
708 clip_references *refs = NULL;
709 char key[ 50 ];
710 int count = 0;
711 track_info *info = peek( this );
712 sprintf( key, "%p", parent );
713 mlt_properties_get_data( producers, key, &count );
714 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
715 old_refs = mlt_properties_get_data( properties, key, &ref_count );
716 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
717 if ( old_refs != NULL )
718 memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
719 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object ), "_clone", -1 );
720 refs[ ref_count ].cut = object;
721 refs[ ref_count ].start = info->position;
722 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
723 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
724 info->position += mlt_producer_get_playtime( object );
725 info->length += mlt_producer_get_playtime( object );
726 }
727 return 0;
728 }
729
730 static int on_end_track( mlt_parser this )
731 {
732 track_info *track = pop( this );
733 track_info *multi = peek( this );
734 multi->length += track->length;
735 multi->position += track->length;
736 multi->offset = track->length;
737 free( track );
738 return 0;
739 }
740
741 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
742 {
743 track_info *multi = pop( this );
744 track_info *track = peek( this );
745 track->position += multi->length;
746 track->length += multi->length;
747 free( multi );
748 return 0;
749 }
750
751 int mlt_producer_optimise( mlt_producer this )
752 {
753 int error = 1;
754 mlt_parser parser = mlt_parser_new( );
755 if ( parser != NULL )
756 {
757 int i = 0, j = 0, k = 0;
758 mlt_properties properties = mlt_parser_properties( parser );
759 mlt_properties producers = mlt_properties_new( );
760 mlt_deque stack = mlt_deque_init( );
761 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
762 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
763 parser->on_start_producer = on_start_producer;
764 parser->on_start_track = on_start_track;
765 parser->on_end_track = on_end_track;
766 parser->on_start_multitrack = on_start_multitrack;
767 parser->on_end_multitrack = on_end_multitrack;
768 push( parser, 0, 0, 0 );
769 mlt_parser_start( parser, MLT_PRODUCER_SERVICE( this ) );
770 free( pop( parser ) );
771 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
772 {
773 char *name = mlt_properties_get_name( producers, k );
774 int count = 0;
775 int clones = 0;
776 int max_clones = 0;
777 mlt_producer producer = mlt_properties_get_data( producers, name, &count );
778 if ( producer != NULL && count > 1 )
779 {
780 clip_references *refs = mlt_properties_get_data( properties, name, &count );
781 for ( i = 0; i < count; i ++ )
782 {
783 clones = 0;
784 for ( j = i + 1; j < count; j ++ )
785 {
786 if ( intersect( &refs[ i ], &refs[ j ] ) )
787 {
788 clones ++;
789 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
790 }
791 }
792 if ( clones > max_clones )
793 max_clones = clones;
794 }
795
796 for ( i = 0; i < count; i ++ )
797 {
798 mlt_producer cut = refs[ i ].cut;
799 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone" ) == -1 )
800 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
801 }
802
803 mlt_producer_set_clones( producer, max_clones );
804 }
805 else if ( producer != NULL )
806 {
807 clip_references *refs = mlt_properties_get_data( properties, name, &count );
808 for ( i = 0; i < count; i ++ )
809 {
810 mlt_producer cut = refs[ i ].cut;
811 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
812 }
813 mlt_producer_set_clones( producer, 0 );
814 }
815 }
816 mlt_parser_close( parser );
817 }
818 return error;
819 }
820
821 /** Close the producer.
822 */
823
824 void mlt_producer_close( mlt_producer this )
825 {
826 if ( this != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( this ) ) <= 0 )
827 {
828 this->parent.close = NULL;
829
830 if ( this->close != NULL )
831 {
832 this->close( this->close_object );
833 }
834 else
835 {
836 #if _MLT_PRODUCER_CHECKS_ == 1
837 // Show debug info
838 mlt_properties_debug( MLT_PRODUCER_PROPERTIES( this ), "Producer closing", stderr );
839 #endif
840
841 #ifdef _MLT_PRODUCER_CHECKS_
842 // Increment destroyed count
843 producers_destroyed ++;
844
845 // Show current stats - these should match when the app is closed
846 fprintf( stderr, "Producers created %d, destroyed %d\n", producers_created, producers_destroyed );
847 #endif
848
849 mlt_service_close( &this->parent );
850 }
851 }
852 }