1b86f6c98c0f08e5b0cf4f9baff7e183747f706e
[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_length( parent ) ) && !mlt_producer_is_blank( this ) )
198 out = mlt_producer_get_length( 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
212 return result;
213 }
214
215 /** Get the parent service object.
216 */
217
218 mlt_service mlt_producer_service( mlt_producer this )
219 {
220 return this != NULL ? &this->parent : NULL;
221 }
222
223 /** Get the producer properties.
224 */
225
226 mlt_properties mlt_producer_properties( mlt_producer this )
227 {
228 return MLT_SERVICE_PROPERTIES( &this->parent );
229 }
230
231 /** Seek to a specified position.
232 */
233
234 int mlt_producer_seek( mlt_producer this, mlt_position position )
235 {
236 // Determine eof handling
237 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
238 char *eof = mlt_properties_get( properties, "eof" );
239 int use_points = 1 - mlt_properties_get_int( properties, "ignore_points" );
240
241 // Recursive behaviour for cuts - repositions parent and then repositions cut
242 // hence no return on this condition
243 if ( mlt_producer_is_cut( this ) )
244 mlt_producer_seek( mlt_producer_cut_parent( this ), position + mlt_producer_get_in( this ) );
245
246 // Check bounds
247 if ( position < 0 )
248 {
249 position = 0;
250 }
251 else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
252 {
253 mlt_producer_set_speed( this, 0 );
254 position = mlt_producer_get_playtime( this ) - 1;
255 }
256 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
257 {
258 position = position % mlt_producer_get_playtime( this );
259 }
260
261 // Set the position
262 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_position", position );
263
264 // Calculate the absolute frame
265 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
266
267 return 0;
268 }
269
270 /** Get the current position (relative to in point).
271 */
272
273 mlt_position mlt_producer_position( mlt_producer this )
274 {
275 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_position" );
276 }
277
278 /** Get the current position (relative to start of producer).
279 */
280
281 mlt_position mlt_producer_frame( mlt_producer this )
282 {
283 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "_frame" );
284 }
285
286 /** Set the playing speed.
287 */
288
289 int mlt_producer_set_speed( mlt_producer this, double speed )
290 {
291 return mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( this ), "_speed", speed );
292 }
293
294 /** Get the playing speed.
295 */
296
297 double mlt_producer_get_speed( mlt_producer this )
298 {
299 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "_speed" );
300 }
301
302 /** Get the frames per second.
303 */
304
305 double mlt_producer_get_fps( mlt_producer this )
306 {
307 return mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "fps" );
308 }
309
310 /** Set the in and out points.
311 */
312
313 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
314 {
315 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
316
317 // Correct ins and outs if necessary
318 if ( in < 0 )
319 in = 0;
320 else if ( in >= mlt_producer_get_length( this ) )
321 in = mlt_producer_get_length( this ) - 1;
322
323 if ( out < 0 )
324 out = 0;
325 else if ( out >= mlt_producer_get_length( this ) && !mlt_producer_is_blank( this ) )
326 out = mlt_producer_get_length( this ) - 1;
327 else if ( out >= mlt_producer_get_length( this ) && mlt_producer_is_blank( this ) )
328 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( this ), "length", out + 1 );
329
330 // Swap ins and outs if wrong
331 if ( out < in )
332 {
333 mlt_position t = in;
334 in = out;
335 out = t;
336 }
337
338 // Set the values
339 mlt_events_block( properties, properties );
340 mlt_properties_set_position( properties, "in", in );
341 mlt_events_unblock( properties, properties );
342 mlt_properties_set_position( properties, "out", out );
343
344 return 0;
345 }
346
347 /** Physically reduce the producer (typically a cut) to a 0 length.
348 Essentially, all 0 length cuts should be immediately removed by containers.
349 */
350
351 int mlt_producer_clear( mlt_producer this )
352 {
353 if ( this != NULL )
354 {
355 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
356 mlt_events_block( properties, properties );
357 mlt_properties_set_position( properties, "in", 0 );
358 mlt_events_unblock( properties, properties );
359 mlt_properties_set_position( properties, "out", -1 );
360 }
361 return 0;
362 }
363
364 /** Get the in point.
365 */
366
367 mlt_position mlt_producer_get_in( mlt_producer this )
368 {
369 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "in" );
370 }
371
372 /** Get the out point.
373 */
374
375 mlt_position mlt_producer_get_out( mlt_producer this )
376 {
377 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "out" );
378 }
379
380 /** Get the total play time.
381 */
382
383 mlt_position mlt_producer_get_playtime( mlt_producer this )
384 {
385 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
386 }
387
388 /** Get the total length of the producer.
389 */
390
391 mlt_position mlt_producer_get_length( mlt_producer this )
392 {
393 return mlt_properties_get_position( MLT_PRODUCER_PROPERTIES( this ), "length" );
394 }
395
396 /** Prepare for next frame.
397 */
398
399 void mlt_producer_prepare_next( mlt_producer this )
400 {
401 if ( mlt_producer_get_speed( this ) != 0 )
402 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
403 }
404
405 /** Get a frame.
406 */
407
408 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
409 {
410 int result = 1;
411 mlt_producer this = service != NULL ? service->child : NULL;
412
413 if ( this != NULL && !mlt_producer_is_cut( this ) )
414 {
415 // Get the properties of this producer
416 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
417
418 // Determine eof handling
419 char *eof = mlt_properties_get( MLT_PRODUCER_PROPERTIES( this ), "eof" );
420
421 // Get the speed of the producer
422 double speed = mlt_producer_get_speed( this );
423
424 // We need to use the clone if it's specified
425 mlt_producer clone = mlt_properties_get_data( properties, "use_clone", NULL );
426
427 // If no clone is specified, use this
428 clone = clone == NULL ? this : clone;
429
430 // A properly instatiated producer will have a get_frame method...
431 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
432 {
433 // Generate a test frame
434 *frame = mlt_frame_init( );
435
436 // Set the position
437 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
438
439 // Mark as a test card
440 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 1 );
441 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_audio", 1 );
442
443 // Calculate the next position
444 mlt_producer_prepare_next( this );
445 }
446 else
447 {
448 // Get the frame from the implementation
449 result = this->get_frame( clone, frame, index );
450 }
451
452 // Copy the fps and speed of the producer onto the frame
453 properties = MLT_FRAME_PROPERTIES( *frame );
454 mlt_properties_set_double( properties, "_speed", speed );
455 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
456 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
457 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
458 if ( mlt_properties_get_data( properties, "_producer", NULL ) == NULL )
459 mlt_properties_set_data( properties, "_producer", service, 0, NULL, NULL );
460 }
461 else if ( this != NULL )
462 {
463 // Get the speed of the cut
464 double speed = mlt_producer_get_speed( this );
465
466 // Get the parent of this cut
467 mlt_producer parent = mlt_producer_cut_parent( this );
468
469 // Get the properties of the parent
470 mlt_properties parent_properties = MLT_PRODUCER_PROPERTIES( parent );
471
472 // Get the properties of the cut
473 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
474
475 // Determine the clone index
476 int clone_index = mlt_properties_get_int( properties, "_clone" );
477
478 // Determine the clone to use
479 mlt_producer clone = this;
480
481 if ( clone_index > 0 )
482 {
483 char key[ 25 ];
484 sprintf( key, "_clone.%d", clone_index - 1 );
485 clone = mlt_properties_get_data( MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( this ) ), key, NULL );
486 if ( clone == NULL ) fprintf( stderr, "requested clone doesn't exist %d\n", clone_index );
487 clone = clone == NULL ? this : clone;
488 }
489 else
490 {
491 clone = parent;
492 }
493
494 // We need to seek to the correct position in the clone
495 mlt_producer_seek( clone, mlt_producer_get_in( this ) + mlt_properties_get_int( properties, "_position" ) );
496
497 // Assign the clone property to the parent
498 mlt_properties_set_data( parent_properties, "use_clone", clone, 0, NULL, NULL );
499
500 // Now get the frame from the parents service
501 result = mlt_service_get_frame( MLT_PRODUCER_SERVICE( parent ), frame, index );
502
503 // We're done with the clone now
504 mlt_properties_set_data( parent_properties, "use_clone", NULL, 0, NULL, NULL );
505
506 // This is useful and required by always_active transitions to determine in/out points of the cut
507 if ( mlt_properties_get_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", NULL ) == MLT_PRODUCER_SERVICE( parent ) )
508 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "_producer", this, 0, NULL, NULL );
509
510 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "_speed", speed );
511 mlt_producer_prepare_next( this );
512 }
513 else
514 {
515 *frame = mlt_frame_init( );
516 result = 0;
517 }
518
519 // Pass on all meta properties from the producer/cut on to the frame
520 if ( *frame != NULL && this != NULL )
521 {
522 int i = 0;
523 mlt_properties p_props = MLT_PRODUCER_PROPERTIES( this );
524 mlt_properties f_props = MLT_FRAME_PROPERTIES( *frame );
525 int count = mlt_properties_count( p_props );
526 for ( i = 0; i < count; i ++ )
527 {
528 char *name = mlt_properties_get_name( p_props, i );
529 if ( !strncmp( name, "meta.", 5 ) )
530 mlt_properties_set( f_props, name, mlt_properties_get( p_props, name ) );
531 }
532 }
533
534 return result;
535 }
536
537 /** Attach a filter.
538 */
539
540 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
541 {
542 return mlt_service_attach( MLT_PRODUCER_SERVICE( this ), filter );
543 }
544
545 /** Detach a filter.
546 */
547
548 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
549 {
550 return mlt_service_detach( MLT_PRODUCER_SERVICE( this ), filter );
551 }
552
553 /** Retrieve a filter.
554 */
555
556 mlt_filter mlt_producer_filter( mlt_producer this, int index )
557 {
558 return mlt_service_filter( MLT_PRODUCER_SERVICE( this ), index );
559 }
560
561 /** Clone this producer.
562 */
563
564 static mlt_producer mlt_producer_clone( mlt_producer this )
565 {
566 mlt_producer clone = NULL;
567 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
568 char *resource = mlt_properties_get( properties, "resource" );
569 char *service = mlt_properties_get( properties, "mlt_service" );
570
571 mlt_events_block( mlt_factory_event_object( ), mlt_factory_event_object( ) );
572
573 if ( service != NULL )
574 clone = mlt_factory_producer( service, resource );
575
576 if ( clone == NULL && resource != NULL )
577 clone = mlt_factory_producer( "fezzik", resource );
578
579 if ( clone != NULL )
580 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( clone ), properties );
581
582 mlt_events_unblock( mlt_factory_event_object( ), mlt_factory_event_object( ) );
583
584 return clone;
585 }
586
587 /** Create clones.
588 */
589
590 static void mlt_producer_set_clones( mlt_producer this, int clones )
591 {
592 mlt_producer parent = mlt_producer_cut_parent( this );
593 mlt_properties properties = MLT_PRODUCER_PROPERTIES( parent );
594 int existing = mlt_properties_get_int( properties, "_clones" );
595 int i = 0;
596 char key[ 25 ];
597
598 // If the number of existing clones is different, then create/remove as necessary
599 if ( existing != clones )
600 {
601 if ( existing < clones )
602 {
603 for ( i = existing; i < clones; i ++ )
604 {
605 mlt_producer clone = mlt_producer_clone( parent );
606 sprintf( key, "_clone.%d", i );
607 mlt_properties_set_data( properties, key, clone, 0, ( mlt_destructor )mlt_producer_close, NULL );
608 }
609 }
610 else
611 {
612 for ( i = clones; i < existing; i ++ )
613 {
614 sprintf( key, "_clone.%d", i );
615 mlt_properties_set_data( properties, key, NULL, 0, NULL, NULL );
616 }
617 }
618 }
619
620 // Ensure all properties on the parent are passed to the clones
621 for ( i = 0; i < clones; i ++ )
622 {
623 mlt_producer clone = NULL;
624 sprintf( key, "_clone.%d", i );
625 clone = mlt_properties_get_data( properties, key, NULL );
626 if ( clone != NULL )
627 mlt_properties_pass( MLT_PRODUCER_PROPERTIES( clone ), properties, "" );
628 }
629
630 // Update the number of clones on the properties
631 mlt_properties_set_int( properties, "_clones", clones );
632 }
633
634 /** Optimise for overlapping cuts from the same clip.
635 */
636
637 typedef struct
638 {
639 int multitrack;
640 int track;
641 int position;
642 int length;
643 int offset;
644 }
645 track_info;
646
647 typedef struct
648 {
649 mlt_producer cut;
650 int start;
651 int end;
652 }
653 clip_references;
654
655 static int intersect( clip_references *a, clip_references *b )
656 {
657 int diff = ( a->start - b->start ) + ( a->end - b->end );
658 return diff >= 0 && diff < ( a->end - a->start + 1 );
659 }
660
661 static int push( mlt_parser this, int multitrack, int track, int position )
662 {
663 mlt_properties properties = mlt_parser_properties( this );
664 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
665 track_info *info = malloc( sizeof( track_info ) );
666 info->multitrack = multitrack;
667 info->track = track;
668 info->position = position;
669 info->length = 0;
670 info->offset = 0;
671 return mlt_deque_push_back( stack, info );
672 }
673
674 static track_info *pop( mlt_parser this )
675 {
676 mlt_properties properties = mlt_parser_properties( this );
677 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
678 return mlt_deque_pop_back( stack );
679 }
680
681 static track_info *peek( mlt_parser this )
682 {
683 mlt_properties properties = mlt_parser_properties( this );
684 mlt_deque stack = mlt_properties_get_data( properties, "stack", NULL );
685 return mlt_deque_peek_back( stack );
686 }
687
688 static int on_start_multitrack( mlt_parser this, mlt_multitrack object )
689 {
690 track_info *info = peek( this );
691 return push( this, info->multitrack ++, info->track, info->position );
692 }
693
694 static int on_start_track( mlt_parser this )
695 {
696 track_info *info = peek( this );
697 info->position -= info->offset;
698 info->length -= info->offset;
699 return push( this, info->multitrack, info->track ++, info->position );
700 }
701
702 static int on_start_producer( mlt_parser this, mlt_producer object )
703 {
704 mlt_properties properties = mlt_parser_properties( this );
705 mlt_properties producers = mlt_properties_get_data( properties, "producers", NULL );
706 mlt_producer parent = mlt_producer_cut_parent( object );
707 if ( mlt_service_identify( ( mlt_service )mlt_producer_cut_parent( object ) ) == producer_type && mlt_producer_is_cut( object ) )
708 {
709 int ref_count = 0;
710 clip_references *old_refs = NULL;
711 clip_references *refs = NULL;
712 char key[ 50 ];
713 int count = 0;
714 track_info *info = peek( this );
715 sprintf( key, "%p", parent );
716 mlt_properties_get_data( producers, key, &count );
717 mlt_properties_set_data( producers, key, parent, ++ count, NULL, NULL );
718 old_refs = mlt_properties_get_data( properties, key, &ref_count );
719 refs = malloc( ( ref_count + 1 ) * sizeof( clip_references ) );
720 if ( old_refs != NULL )
721 memcpy( refs, old_refs, ref_count * sizeof( clip_references ) );
722 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( object ), "_clone", -1 );
723 refs[ ref_count ].cut = object;
724 refs[ ref_count ].start = info->position;
725 refs[ ref_count ].end = info->position + mlt_producer_get_playtime( object ) - 1;
726 mlt_properties_set_data( properties, key, refs, ++ ref_count, free, NULL );
727 info->position += mlt_producer_get_playtime( object );
728 info->length += mlt_producer_get_playtime( object );
729 }
730 return 0;
731 }
732
733 static int on_end_track( mlt_parser this )
734 {
735 track_info *track = pop( this );
736 track_info *multi = peek( this );
737 multi->length += track->length;
738 multi->position += track->length;
739 multi->offset = track->length;
740 free( track );
741 return 0;
742 }
743
744 static int on_end_multitrack( mlt_parser this, mlt_multitrack object )
745 {
746 track_info *multi = pop( this );
747 track_info *track = peek( this );
748 track->position += multi->length;
749 track->length += multi->length;
750 free( multi );
751 return 0;
752 }
753
754 int mlt_producer_optimise( mlt_producer this )
755 {
756 int error = 1;
757 mlt_parser parser = mlt_parser_new( );
758 if ( parser != NULL )
759 {
760 int i = 0, j = 0, k = 0;
761 mlt_properties properties = mlt_parser_properties( parser );
762 mlt_properties producers = mlt_properties_new( );
763 mlt_deque stack = mlt_deque_init( );
764 mlt_properties_set_data( properties, "producers", producers, 0, ( mlt_destructor )mlt_properties_close, NULL );
765 mlt_properties_set_data( properties, "stack", stack, 0, ( mlt_destructor )mlt_deque_close, NULL );
766 parser->on_start_producer = on_start_producer;
767 parser->on_start_track = on_start_track;
768 parser->on_end_track = on_end_track;
769 parser->on_start_multitrack = on_start_multitrack;
770 parser->on_end_multitrack = on_end_multitrack;
771 push( parser, 0, 0, 0 );
772 mlt_parser_start( parser, MLT_PRODUCER_SERVICE( this ) );
773 free( pop( parser ) );
774 for ( k = 0; k < mlt_properties_count( producers ); k ++ )
775 {
776 char *name = mlt_properties_get_name( producers, k );
777 int count = 0;
778 int clones = 0;
779 int max_clones = 0;
780 mlt_producer producer = mlt_properties_get_data( producers, name, &count );
781 if ( producer != NULL && count > 1 )
782 {
783 clip_references *refs = mlt_properties_get_data( properties, name, &count );
784 for ( i = 0; i < count; i ++ )
785 {
786 clones = 0;
787 for ( j = i + 1; j < count; j ++ )
788 {
789 if ( intersect( &refs[ i ], &refs[ j ] ) )
790 {
791 clones ++;
792 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( refs[ j ].cut ), "_clone", clones );
793 }
794 }
795 if ( clones > max_clones )
796 max_clones = clones;
797 }
798
799 for ( i = 0; i < count; i ++ )
800 {
801 mlt_producer cut = refs[ i ].cut;
802 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone" ) == -1 )
803 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
804 }
805
806 mlt_producer_set_clones( producer, max_clones );
807 }
808 else if ( producer != NULL )
809 {
810 clip_references *refs = mlt_properties_get_data( properties, name, &count );
811 for ( i = 0; i < count; i ++ )
812 {
813 mlt_producer cut = refs[ i ].cut;
814 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( cut ), "_clone", 0 );
815 }
816 mlt_producer_set_clones( producer, 0 );
817 }
818 }
819 mlt_parser_close( parser );
820 }
821 return error;
822 }
823
824 /** Close the producer.
825 */
826
827 void mlt_producer_close( mlt_producer this )
828 {
829 if ( this != NULL && mlt_properties_dec_ref( MLT_PRODUCER_PROPERTIES( this ) ) <= 0 )
830 {
831 this->parent.close = NULL;
832
833 if ( this->close != NULL )
834 {
835 this->close( this->close_object );
836 }
837 else
838 {
839 int destroy = mlt_producer_is_cut( this );
840
841 #if _MLT_PRODUCER_CHECKS_ == 1
842 // Show debug info
843 mlt_properties_debug( MLT_PRODUCER_PROPERTIES( this ), "Producer closing", stderr );
844 #endif
845
846 #ifdef _MLT_PRODUCER_CHECKS_
847 // Increment destroyed count
848 producers_destroyed ++;
849
850 // Show current stats - these should match when the app is closed
851 fprintf( stderr, "Producers created %d, destroyed %d\n", producers_created, producers_destroyed );
852 #endif
853
854 mlt_service_close( &this->parent );
855
856 if ( destroy )
857 free( this );
858 }
859 }
860 }