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