framework: remove global profile, rather share one mlt_profile across a service netwo...
[melted] / src / framework / mlt_playlist.c
1 /*
2 * mlt_playlist.c -- playlist service class
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
23 #include "mlt_playlist.h"
24 #include "mlt_tractor.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
27 #include "mlt_frame.h"
28 #include "mlt_transition.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 /** Virtual playlist entry.
35 */
36
37 struct playlist_entry_s
38 {
39 mlt_producer producer;
40 mlt_position frame_in;
41 mlt_position frame_out;
42 mlt_position frame_count;
43 int repeat;
44 mlt_position producer_length;
45 mlt_event event;
46 int preservation_hack;
47 };
48
49 /** Forward declarations
50 */
51
52 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
53 static int mlt_playlist_unmix( mlt_playlist this, int clip );
54 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out );
55
56 /** Constructor.
57 */
58
59 mlt_playlist mlt_playlist_init( )
60 {
61 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
62 if ( this != NULL )
63 {
64 mlt_producer producer = &this->parent;
65
66 // Construct the producer
67 mlt_producer_init( producer, this );
68
69 // Override the producer get_frame
70 producer->get_frame = producer_get_frame;
71
72 // Define the destructor
73 producer->close = ( mlt_destructor )mlt_playlist_close;
74 producer->close_object = this;
75
76 // Initialise blank
77 mlt_producer_init( &this->blank, NULL );
78 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "mlt_service", "blank" );
79 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "resource", "blank" );
80
81 // Indicate that this producer is a playlist
82 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( this ), "playlist", this, 0, NULL, NULL );
83
84 // Specify the eof condition
85 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "eof", "pause" );
86 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "resource", "<playlist>" );
87 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "mlt_type", "mlt_producer" );
88 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "in", 0 );
89 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "out", -1 );
90 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "length", 0 );
91
92 this->size = 10;
93 this->list = malloc( this->size * sizeof( playlist_entry * ) );
94 }
95
96 return this;
97 }
98
99 /** Get the producer associated to this playlist.
100 */
101
102 mlt_producer mlt_playlist_producer( mlt_playlist this )
103 {
104 return this != NULL ? &this->parent : NULL;
105 }
106
107 /** Get the service associated to this playlist.
108 */
109
110 mlt_service mlt_playlist_service( mlt_playlist this )
111 {
112 return MLT_PRODUCER_SERVICE( &this->parent );
113 }
114
115 /** Get the propertues associated to this playlist.
116 */
117
118 mlt_properties mlt_playlist_properties( mlt_playlist this )
119 {
120 return MLT_PRODUCER_PROPERTIES( &this->parent );
121 }
122
123 /** Refresh the playlist after a clip has been changed.
124 */
125
126 static int mlt_playlist_virtual_refresh( mlt_playlist this )
127 {
128 // Obtain the properties
129 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
130 int i = 0;
131 mlt_position frame_count = 0;
132
133 for ( i = 0; i < this->count; i ++ )
134 {
135 // Get the producer
136 mlt_producer producer = this->list[ i ]->producer;
137 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
138
139 // Check if the length of the producer has changed
140 if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
141 this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
142 {
143 // This clip should be removed...
144 if ( current_length < 1 )
145 {
146 this->list[ i ]->frame_in = 0;
147 this->list[ i ]->frame_out = -1;
148 this->list[ i ]->frame_count = 0;
149 }
150 else
151 {
152 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
153 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
154 this->list[ i ]->frame_count = current_length;
155 }
156
157 // Update the producer_length
158 this->list[ i ]->producer_length = current_length;
159 }
160
161 // Calculate the frame_count
162 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
163
164 // Update the frame_count for this clip
165 frame_count += this->list[ i ]->frame_count;
166 }
167
168 // Refresh all properties
169 mlt_events_block( properties, properties );
170 mlt_properties_set_position( properties, "length", frame_count );
171 mlt_events_unblock( properties, properties );
172 mlt_properties_set_position( properties, "out", frame_count - 1 );
173
174 return 0;
175 }
176
177 /** Listener for producers on the playlist.
178 */
179
180 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
181 {
182 mlt_playlist_virtual_refresh( this );
183 }
184
185 /** Append to the virtual playlist.
186 */
187
188 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
189 {
190 mlt_producer producer = NULL;
191 mlt_properties properties = NULL;
192 mlt_properties parent = NULL;
193
194 // If we have a cut, then use the in/out points from the cut
195 if ( mlt_producer_is_blank( source ) )
196 {
197 // Make sure the blank is long enough to accomodate the length specified
198 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
199 {
200 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
201 mlt_events_block( blank_props, blank_props );
202 mlt_producer_set_in_and_out( &this->blank, in, out );
203 mlt_events_unblock( blank_props, blank_props );
204 }
205
206 // Now make sure the cut comes from this this->blank
207 if ( source == NULL )
208 {
209 producer = mlt_producer_cut( &this->blank, in, out );
210 }
211 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
212 {
213 producer = mlt_producer_cut( &this->blank, in, out );
214 }
215 else
216 {
217 producer = source;
218 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
219 }
220
221 properties = MLT_PRODUCER_PROPERTIES( producer );
222 }
223 else if ( mlt_producer_is_cut( source ) )
224 {
225 producer = source;
226 if ( in == -1 )
227 in = mlt_producer_get_in( producer );
228 if ( out == -1 || out > mlt_producer_get_out( producer ) )
229 out = mlt_producer_get_out( producer );
230 properties = MLT_PRODUCER_PROPERTIES( producer );
231 mlt_properties_inc_ref( properties );
232 }
233 else
234 {
235 producer = mlt_producer_cut( source, in, out );
236 if ( in == -1 || in < mlt_producer_get_in( producer ) )
237 in = mlt_producer_get_in( producer );
238 if ( out == -1 || out > mlt_producer_get_out( producer ) )
239 out = mlt_producer_get_out( producer );
240 properties = MLT_PRODUCER_PROPERTIES( producer );
241 }
242
243 // Fetch the cuts parent properties
244 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
245
246 // Remove fezzik normalisers for fx cuts
247 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
248 {
249 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
250 mlt_filter filter = mlt_service_filter( service, 0 );
251 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik" ) )
252 {
253 mlt_service_detach( service, filter );
254 filter = mlt_service_filter( service, 0 );
255 }
256 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
257 }
258
259 // Check that we have room
260 if ( this->count >= this->size )
261 {
262 int i;
263 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
264 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
265 this->size += 10;
266 }
267
268 // Create the entry
269 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
270 if ( this->list[ this->count ] != NULL )
271 {
272 this->list[ this->count ]->producer = producer;
273 this->list[ this->count ]->frame_in = in;
274 this->list[ this->count ]->frame_out = out;
275 this->list[ this->count ]->frame_count = out - in + 1;
276 this->list[ this->count ]->repeat = 1;
277 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
278 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
279 mlt_event_inc_ref( this->list[ this->count ]->event );
280 mlt_properties_set( properties, "eof", "pause" );
281 mlt_producer_set_speed( producer, 0 );
282 this->count ++;
283 }
284
285 return mlt_playlist_virtual_refresh( this );
286 }
287
288 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
289 {
290 // Default producer to NULL
291 mlt_producer producer = NULL;
292
293 // Loop for each producer until found
294 for ( *clip = 0; *clip < this->count; *clip += 1 )
295 {
296 // Increment the total
297 *total += this->list[ *clip ]->frame_count;
298
299 // Check if the position indicates that we have found the clip
300 // Note that 0 length clips get skipped automatically
301 if ( *position < this->list[ *clip ]->frame_count )
302 {
303 // Found it, now break
304 producer = this->list[ *clip ]->producer;
305 break;
306 }
307 else
308 {
309 // Decrement position by length of this entry
310 *position -= this->list[ *clip ]->frame_count;
311 }
312 }
313
314 return producer;
315 }
316
317 /** Seek in the virtual playlist.
318 */
319
320 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
321 {
322 // Map playlist position to real producer in virtual playlist
323 mlt_position position = mlt_producer_frame( &this->parent );
324
325 // Keep the original position since we change it while iterating through the list
326 mlt_position original = position;
327
328 // Clip index and total
329 int i = 0;
330 int total = 0;
331
332 // Locate the producer for the position
333 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
334
335 // Get the properties
336 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
337
338 // Get the eof handling
339 char *eof = mlt_properties_get( properties, "eof" );
340
341 // Seek in real producer to relative position
342 if ( producer != NULL )
343 {
344 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
345 *progressive = count == 1;
346 mlt_producer_seek( producer, (int)position % count );
347 }
348 else if ( !strcmp( eof, "pause" ) && total > 0 )
349 {
350 playlist_entry *entry = this->list[ this->count - 1 ];
351 int count = entry->frame_count / entry->repeat;
352 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
353 mlt_producer_seek( this_producer, original - 1 );
354 producer = entry->producer;
355 mlt_producer_seek( producer, (int)entry->frame_out % count );
356 mlt_producer_set_speed( this_producer, 0 );
357 mlt_producer_set_speed( producer, 0 );
358 *progressive = count == 1;
359 }
360 else if ( !strcmp( eof, "loop" ) && total > 0 )
361 {
362 playlist_entry *entry = this->list[ 0 ];
363 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
364 mlt_producer_seek( this_producer, 0 );
365 producer = entry->producer;
366 mlt_producer_seek( producer, 0 );
367 }
368 else
369 {
370 producer = &this->blank;
371 }
372
373 return MLT_PRODUCER_SERVICE( producer );
374 }
375
376 /** Invoked when a producer indicates that it has prematurely reached its end.
377 */
378
379 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
380 {
381 // Default producer to blank
382 mlt_producer producer = &this->blank;
383
384 // Map playlist position to real producer in virtual playlist
385 mlt_position position = mlt_producer_frame( &this->parent );
386
387 // Loop through the virtual playlist
388 int i = 0;
389
390 for ( i = 0; i < this->count; i ++ )
391 {
392 if ( position < this->list[ i ]->frame_count )
393 {
394 // Found it, now break
395 producer = this->list[ i ]->producer;
396 break;
397 }
398 else
399 {
400 // Decrement position by length of this entry
401 position -= this->list[ i ]->frame_count;
402 }
403 }
404
405 // Seek in real producer to relative position
406 if ( i < this->count && this->list[ i ]->frame_out != position )
407 {
408 // Update the frame_count for the changed clip (hmmm)
409 this->list[ i ]->frame_out = position;
410 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
411
412 // Refresh the playlist
413 mlt_playlist_virtual_refresh( this );
414 }
415
416 return producer;
417 }
418
419 /** Obtain the current clips index.
420 */
421
422 int mlt_playlist_current_clip( mlt_playlist this )
423 {
424 // Map playlist position to real producer in virtual playlist
425 mlt_position position = mlt_producer_frame( &this->parent );
426
427 // Loop through the virtual playlist
428 int i = 0;
429
430 for ( i = 0; i < this->count; i ++ )
431 {
432 if ( position < this->list[ i ]->frame_count )
433 {
434 // Found it, now break
435 break;
436 }
437 else
438 {
439 // Decrement position by length of this entry
440 position -= this->list[ i ]->frame_count;
441 }
442 }
443
444 return i;
445 }
446
447 /** Obtain the current clips producer.
448 */
449
450 mlt_producer mlt_playlist_current( mlt_playlist this )
451 {
452 int i = mlt_playlist_current_clip( this );
453 if ( i < this->count )
454 return this->list[ i ]->producer;
455 else
456 return &this->blank;
457 }
458
459 /** Get the position which corresponds to the start of the next clip.
460 */
461
462 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
463 {
464 mlt_position position = 0;
465 int absolute_clip = index;
466 int i = 0;
467
468 // Determine the absolute clip
469 switch ( whence )
470 {
471 case mlt_whence_relative_start:
472 absolute_clip = index;
473 break;
474
475 case mlt_whence_relative_current:
476 absolute_clip = mlt_playlist_current_clip( this ) + index;
477 break;
478
479 case mlt_whence_relative_end:
480 absolute_clip = this->count - index;
481 break;
482 }
483
484 // Check that we're in a valid range
485 if ( absolute_clip < 0 )
486 absolute_clip = 0;
487 else if ( absolute_clip > this->count )
488 absolute_clip = this->count;
489
490 // Now determine the position
491 for ( i = 0; i < absolute_clip; i ++ )
492 position += this->list[ i ]->frame_count;
493
494 return position;
495 }
496
497 /** Get all the info about the clip specified.
498 */
499
500 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
501 {
502 int error = index < 0 || index >= this->count;
503 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
504 if ( !error )
505 {
506 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
507 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
508 info->clip = index;
509 info->producer = producer;
510 info->cut = this->list[ index ]->producer;
511 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
512 info->resource = mlt_properties_get( properties, "resource" );
513 info->frame_in = this->list[ index ]->frame_in;
514 info->frame_out = this->list[ index ]->frame_out;
515 info->frame_count = this->list[ index ]->frame_count;
516 info->repeat = this->list[ index ]->repeat;
517 info->length = mlt_producer_get_length( producer );
518 info->fps = mlt_producer_get_fps( producer );
519 }
520
521 return error;
522 }
523
524 /** Get number of clips in the playlist.
525 */
526
527 int mlt_playlist_count( mlt_playlist this )
528 {
529 return this->count;
530 }
531
532 /** Clear the playlist.
533 */
534
535 int mlt_playlist_clear( mlt_playlist this )
536 {
537 int i;
538 for ( i = 0; i < this->count; i ++ )
539 {
540 mlt_event_close( this->list[ i ]->event );
541 mlt_producer_close( this->list[ i ]->producer );
542 }
543 this->count = 0;
544 return mlt_playlist_virtual_refresh( this );
545 }
546
547 /** Append a producer to the playlist.
548 */
549
550 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
551 {
552 // Append to virtual list
553 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
554 }
555
556 /** Append a producer to the playlist with in/out points.
557 */
558
559 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
560 {
561 // Append to virtual list
562 if ( in != -1 && out != -1 )
563 return mlt_playlist_virtual_append( this, producer, in, out );
564 else
565 return mlt_playlist_append( this, producer );
566 }
567
568 /** Append a blank to the playlist of a given length.
569 */
570
571 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
572 {
573 // Append to the virtual list
574 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
575 }
576
577 /** Insert a producer into the playlist.
578 */
579
580 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
581 {
582 // Append to end
583 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
584 mlt_playlist_append_io( this, producer, in, out );
585
586 // Move to the position specified
587 mlt_playlist_move( this, this->count - 1, where );
588 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
589
590 return mlt_playlist_virtual_refresh( this );
591 }
592
593 /** Remove an entry in the playlist.
594 */
595
596 int mlt_playlist_remove( mlt_playlist this, int where )
597 {
598 int error = where < 0 || where >= this->count;
599 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
600 {
601 // We need to know the current clip and the position within the playlist
602 int current = mlt_playlist_current_clip( this );
603 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
604
605 // We need all the details about the clip we're removing
606 mlt_playlist_clip_info where_info;
607 playlist_entry *entry = this->list[ where ];
608 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
609
610 // Loop variable
611 int i = 0;
612
613 // Get the clip info
614 mlt_playlist_get_clip_info( this, &where_info, where );
615
616 // Make sure the clip to be removed is valid and correct if necessary
617 if ( where < 0 )
618 where = 0;
619 if ( where >= this->count )
620 where = this->count - 1;
621
622 // Reorganise the list
623 for ( i = where + 1; i < this->count; i ++ )
624 this->list[ i - 1 ] = this->list[ i ];
625 this->count --;
626
627 if ( entry->preservation_hack == 0 )
628 {
629 // Decouple from mix_in/out if necessary
630 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
631 {
632 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
633 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
634 }
635 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
636 {
637 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
638 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
639 }
640
641 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
642 mlt_producer_clear( entry->producer );
643 }
644
645 // Close the producer associated to the clip info
646 mlt_event_close( entry->event );
647 mlt_producer_close( entry->producer );
648
649 // Correct position
650 if ( where == current )
651 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
652 else if ( where < current && this->count > 0 )
653 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
654 else if ( this->count == 0 )
655 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
656
657 // Free the entry
658 free( entry );
659
660 // Refresh the playlist
661 mlt_playlist_virtual_refresh( this );
662 }
663
664 return error;
665 }
666
667 /** Move an entry in the playlist.
668 */
669
670 int mlt_playlist_move( mlt_playlist this, int src, int dest )
671 {
672 int i;
673
674 /* We need to ensure that the requested indexes are valid and correct it as necessary */
675 if ( src < 0 )
676 src = 0;
677 if ( src >= this->count )
678 src = this->count - 1;
679
680 if ( dest < 0 )
681 dest = 0;
682 if ( dest >= this->count )
683 dest = this->count - 1;
684
685 if ( src != dest && this->count > 1 )
686 {
687 int current = mlt_playlist_current_clip( this );
688 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
689 playlist_entry *src_entry = NULL;
690
691 // We need all the details about the current clip
692 mlt_playlist_clip_info current_info;
693
694 mlt_playlist_get_clip_info( this, &current_info, current );
695 position -= current_info.start;
696
697 if ( current == src )
698 current = dest;
699 else if ( current > src && current < dest )
700 current ++;
701 else if ( current == dest )
702 current = src;
703
704 src_entry = this->list[ src ];
705 if ( src > dest )
706 {
707 for ( i = src; i > dest; i -- )
708 this->list[ i ] = this->list[ i - 1 ];
709 }
710 else
711 {
712 for ( i = src; i < dest; i ++ )
713 this->list[ i ] = this->list[ i + 1 ];
714 }
715 this->list[ dest ] = src_entry;
716
717 mlt_playlist_get_clip_info( this, &current_info, current );
718 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
719 mlt_playlist_virtual_refresh( this );
720 }
721
722 return 0;
723 }
724
725 /** Repeat the specified clip n times.
726 */
727
728 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
729 {
730 int error = repeat < 1 || clip < 0 || clip >= this->count;
731 if ( error == 0 )
732 {
733 playlist_entry *entry = this->list[ clip ];
734 entry->repeat = repeat;
735 mlt_playlist_virtual_refresh( this );
736 }
737 return error;
738 }
739
740 /** Resize the specified clip.
741 */
742
743 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
744 {
745 int error = clip < 0 || clip >= this->count;
746 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
747 {
748 playlist_entry *entry = this->list[ clip ];
749 mlt_producer producer = entry->producer;
750 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
751
752 mlt_events_block( properties, properties );
753
754 if ( mlt_producer_is_blank( producer ) )
755 {
756 // Make sure the blank is long enough to accomodate the length specified
757 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
758 {
759 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
760 mlt_properties_set_int( blank_props, "length", out - in + 1 );
761 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
762 mlt_producer_set_in_and_out( &this->blank, 0, out - in );
763 }
764 }
765
766 if ( in <= -1 )
767 in = 0;
768 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
769 out = mlt_producer_get_length( producer ) - 1;
770
771 if ( out < in )
772 {
773 mlt_position t = in;
774 in = out;
775 out = t;
776 }
777
778 mlt_producer_set_in_and_out( producer, in, out );
779 mlt_events_unblock( properties, properties );
780 mlt_playlist_virtual_refresh( this );
781 }
782 return error;
783 }
784
785 /** Split a clip on the playlist at the given position.
786 */
787
788 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
789 {
790 int error = clip < 0 || clip >= this->count;
791 if ( error == 0 )
792 {
793 playlist_entry *entry = this->list[ clip ];
794 position = position < 0 ? entry->frame_count + position - 1 : position;
795 if ( position >= 0 && position < entry->frame_count - 1 )
796 {
797 int in = entry->frame_in;
798 int out = entry->frame_out;
799 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
800 mlt_playlist_resize_clip( this, clip, in, in + position );
801 if ( !mlt_producer_is_blank( entry->producer ) )
802 {
803 int i = 0;
804 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
805 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
806 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
807 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
808 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
809 {
810 char *name = mlt_properties_get_name( entry_properties, i );
811 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
812 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
813 }
814 mlt_producer_close( split );
815 }
816 else
817 {
818 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
819 }
820 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
821 mlt_playlist_virtual_refresh( this );
822 }
823 else
824 {
825 error = 1;
826 }
827 }
828 return error;
829 }
830
831 /** Split the playlist at the absolute position.
832 */
833
834 int mlt_playlist_split_at( mlt_playlist this, mlt_position position, int left )
835 {
836 int result = this == NULL ? -1 : 0;
837 if ( !result )
838 {
839 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) ) )
840 {
841 int clip = mlt_playlist_get_clip_index_at( this, position );
842 mlt_playlist_clip_info info;
843 mlt_playlist_get_clip_info( this, &info, clip );
844 if ( left && position != info.start )
845 mlt_playlist_split( this, clip, position - info.start - 1 );
846 else if ( !left )
847 mlt_playlist_split( this, clip, position - info.start );
848 result = position;
849 }
850 else if ( position <= 0 )
851 {
852 result = 0;
853 }
854 else
855 {
856 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
857 }
858 }
859 return result;
860 }
861
862 /** Join 1 or more consecutive clips.
863 */
864
865 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
866 {
867 int error = clip < 0 || clip >= this->count;
868 if ( error == 0 )
869 {
870 int i = clip;
871 mlt_playlist new_clip = mlt_playlist_init( );
872 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
873 if ( clip + count >= this->count )
874 count = this->count - clip - 1;
875 for ( i = 0; i <= count; i ++ )
876 {
877 playlist_entry *entry = this->list[ clip ];
878 mlt_playlist_append( new_clip, entry->producer );
879 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
880 entry->preservation_hack = 1;
881 mlt_playlist_remove( this, clip );
882 }
883 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
884 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
885 mlt_playlist_close( new_clip );
886 }
887 return error;
888 }
889
890 /** Mix consecutive clips for a specified length and apply transition if specified.
891 */
892
893 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
894 {
895 int error = ( clip < 0 || clip + 1 >= this->count );
896 if ( error == 0 )
897 {
898 playlist_entry *clip_a = this->list[ clip ];
899 playlist_entry *clip_b = this->list[ clip + 1 ];
900 mlt_producer track_a = NULL;
901 mlt_producer track_b = NULL;
902 mlt_tractor tractor = mlt_tractor_new( );
903 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
904
905 // Check length is valid for both clips and resize if necessary.
906 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
907 length = length > max_size ? max_size : length;
908
909 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
910 if ( length != clip_a->frame_count )
911 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
912 else
913 track_a = clip_a->producer;
914
915 if ( length != clip_b->frame_count )
916 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
917 else
918 track_b = clip_b->producer;
919
920 // Set the tracks on the tractor
921 mlt_tractor_set_track( tractor, track_a, 0 );
922 mlt_tractor_set_track( tractor, track_b, 1 );
923
924 // Insert the mix object into the playlist
925 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
926 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
927
928 // Attach the transition
929 if ( transition != NULL )
930 {
931 mlt_field field = mlt_tractor_field( tractor );
932 mlt_field_plant_transition( field, transition, 0, 1 );
933 mlt_transition_set_in_and_out( transition, 0, length - 1 );
934 }
935
936 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
937 if ( track_a != clip_a->producer )
938 mlt_producer_close( track_a );
939 if ( track_b != clip_b->producer )
940 mlt_producer_close( track_b );
941
942 // Check if we have anything left on the right hand clip
943 if ( track_b == clip_b->producer )
944 {
945 clip_b->preservation_hack = 1;
946 mlt_playlist_remove( this, clip + 2 );
947 }
948 else if ( clip_b->frame_out - clip_b->frame_in > length )
949 {
950 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
951 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
952 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
953 }
954 else
955 {
956 mlt_producer_clear( clip_b->producer );
957 mlt_playlist_remove( this, clip + 2 );
958 }
959
960 // Check if we have anything left on the left hand clip
961 if ( track_a == clip_a->producer )
962 {
963 clip_a->preservation_hack = 1;
964 mlt_playlist_remove( this, clip );
965 }
966 else if ( clip_a->frame_out - clip_a->frame_in > length )
967 {
968 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
969 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
970 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
971 }
972 else
973 {
974 mlt_producer_clear( clip_a->producer );
975 mlt_playlist_remove( this, clip );
976 }
977
978 // Unblock and force a fire off of change events to listeners
979 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
980 mlt_playlist_virtual_refresh( this );
981 mlt_tractor_close( tractor );
982 }
983 return error;
984 }
985
986 /** Add a transition to an existing mix.
987 */
988
989 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
990 {
991 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
992 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
993 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
994 int error = transition == NULL || tractor == NULL;
995 if ( error == 0 )
996 {
997 mlt_field field = mlt_tractor_field( tractor );
998 mlt_field_plant_transition( field, transition, 0, 1 );
999 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
1000 }
1001 return error;
1002 }
1003
1004 /** Return the clip at the clip index.
1005 */
1006
1007 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
1008 {
1009 if ( clip >= 0 && clip < this->count )
1010 return this->list[ clip ]->producer;
1011 return NULL;
1012 }
1013
1014 /** Return the clip at the specified position.
1015 */
1016
1017 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, mlt_position position )
1018 {
1019 int index = 0, total = 0;
1020 return mlt_playlist_locate( this, &position, &index, &total );
1021 }
1022
1023 /** Return the clip index of the specified position.
1024 */
1025
1026 int mlt_playlist_get_clip_index_at( mlt_playlist this, mlt_position position )
1027 {
1028 int index = 0, total = 0;
1029 mlt_playlist_locate( this, &position, &index, &total );
1030 return index;
1031 }
1032
1033 /** Determine if the clip is a mix.
1034 */
1035
1036 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1037 {
1038 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1039 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1040 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1041 return tractor != NULL;
1042 }
1043
1044 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1045 back correctly on to the playlist.
1046 */
1047
1048 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1049 {
1050 int error = ( clip < 0 || clip >= this->count );
1051
1052 // Ensure that the clip request is actually a mix
1053 if ( error == 0 )
1054 {
1055 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1056 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1057 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1058 this->list[ clip ]->preservation_hack;
1059 }
1060
1061 if ( error == 0 )
1062 {
1063 playlist_entry *mix = this->list[ clip ];
1064 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1065 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1066 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1067 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1068 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1069 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1070
1071 if ( clip_a != NULL )
1072 {
1073 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1074 }
1075 else
1076 {
1077 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1078 mlt_playlist_insert( this, cut, clip, -1, -1 );
1079 clip ++;
1080 }
1081
1082 if ( clip_b != NULL )
1083 {
1084 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1085 }
1086 else
1087 {
1088 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1089 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1090 }
1091
1092 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1093 mlt_playlist_remove( this, clip );
1094 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1095 mlt_playlist_virtual_refresh( this );
1096 }
1097 return error;
1098 }
1099
1100 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1101 {
1102 int error = ( clip < 0 || clip >= this->count );
1103
1104 // Ensure that the clip request is actually a mix
1105 if ( error == 0 )
1106 {
1107 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1108 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1109 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1110 }
1111
1112 if ( error == 0 )
1113 {
1114 playlist_entry *mix = this->list[ clip ];
1115 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1116 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1117 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1118 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1119 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1120 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1121 int length = out - in + 1;
1122 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1123 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1124
1125 if ( clip_a != NULL )
1126 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1127
1128 if ( clip_b != NULL )
1129 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1130
1131 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1132 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1133 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1134 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1135 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1136 mlt_producer_set_in_and_out( mix->producer, in, out );
1137
1138 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1139 mlt_playlist_virtual_refresh( this );
1140 }
1141 return error;
1142 }
1143
1144 /** Consolodate adjacent blank producers.
1145 */
1146
1147 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1148 {
1149 if ( this != NULL )
1150 {
1151 int i = 0;
1152 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1153
1154 mlt_events_block( properties, properties );
1155 for ( i = 1; i < this->count; i ++ )
1156 {
1157 playlist_entry *left = this->list[ i - 1 ];
1158 playlist_entry *right = this->list[ i ];
1159
1160 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1161 {
1162 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1163 mlt_playlist_remove( this, i -- );
1164 }
1165 }
1166
1167 if ( !keep_length && this->count > 0 )
1168 {
1169 playlist_entry *last = this->list[ this->count - 1 ];
1170 if ( mlt_producer_is_blank( last->producer ) )
1171 mlt_playlist_remove( this, this->count - 1 );
1172 }
1173
1174 mlt_events_unblock( properties, properties );
1175 mlt_playlist_virtual_refresh( this );
1176 }
1177 }
1178
1179 /** Determine if the specified clip index is a blank.
1180 */
1181
1182 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1183 {
1184 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1185 }
1186
1187 /** Determine if the specified position is a blank.
1188 */
1189
1190 int mlt_playlist_is_blank_at( mlt_playlist this, mlt_position position )
1191 {
1192 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( this, position ) );
1193 }
1194
1195 /** Replace the specified clip with a blank and return the clip.
1196 */
1197
1198 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1199 {
1200 mlt_producer producer = NULL;
1201 if ( !mlt_playlist_is_blank( this, clip ) )
1202 {
1203 playlist_entry *entry = this->list[ clip ];
1204 int in = entry->frame_in;
1205 int out = entry->frame_out;
1206 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1207 producer = entry->producer;
1208 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1209 mlt_events_block( properties, properties );
1210 mlt_playlist_remove( this, clip );
1211 mlt_playlist_blank( this, out - in );
1212 mlt_playlist_move( this, this->count - 1, clip );
1213 mlt_events_unblock( properties, properties );
1214 mlt_playlist_virtual_refresh( this );
1215 mlt_producer_set_in_and_out( producer, in, out );
1216 }
1217 return producer;
1218 }
1219
1220 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1221 {
1222 if ( this != NULL && length >= 0 )
1223 {
1224 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1225 mlt_events_block( properties, properties );
1226 mlt_playlist_blank( this, length );
1227 mlt_playlist_move( this, this->count - 1, clip );
1228 mlt_events_unblock( properties, properties );
1229 mlt_playlist_virtual_refresh( this );
1230 }
1231 }
1232
1233 void mlt_playlist_pad_blanks( mlt_playlist this, mlt_position position, int length, int find )
1234 {
1235 if ( this != NULL && length != 0 )
1236 {
1237 int clip = mlt_playlist_get_clip_index_at( this, position );
1238 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1239 mlt_events_block( properties, properties );
1240 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1241 clip ++;
1242 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1243 {
1244 mlt_playlist_clip_info info;
1245 mlt_playlist_get_clip_info( this, &info, clip );
1246 if ( info.frame_out + length > info.frame_in )
1247 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1248 else
1249 mlt_playlist_remove( this, clip );
1250 }
1251 else if ( find && clip < this->count && length > 0 )
1252 {
1253 mlt_playlist_insert_blank( this, clip, length );
1254 }
1255 mlt_events_unblock( properties, properties );
1256 mlt_playlist_virtual_refresh( this );
1257 }
1258 }
1259
1260 int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_producer producer, int mode )
1261 {
1262 int ret = this == NULL || position < 0 || producer == NULL;
1263 if ( ret == 0 )
1264 {
1265 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1266 int length = mlt_producer_get_playtime( producer );
1267 int clip = mlt_playlist_get_clip_index_at( this, position );
1268 mlt_playlist_clip_info info;
1269 mlt_playlist_get_clip_info( this, &info, clip );
1270 mlt_events_block( properties, this );
1271 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1272 {
1273 // Split and move to new clip if need be
1274 if ( position != info.start && mlt_playlist_split( this, clip, position - info.start - 1 ) == 0 )
1275 mlt_playlist_get_clip_info( this, &info, ++ clip );
1276
1277 // Split again if need be
1278 if ( length < info.frame_count )
1279 mlt_playlist_split( this, clip, length - 1 );
1280
1281 // Remove
1282 mlt_playlist_remove( this, clip );
1283
1284 // Insert
1285 mlt_playlist_insert( this, producer, clip, -1, -1 );
1286 ret = clip;
1287 }
1288 else if ( clip < this->count )
1289 {
1290 if ( position > info.start + info.frame_count / 2 )
1291 clip ++;
1292 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1293 {
1294 mlt_playlist_get_clip_info( this, &info, clip );
1295 if ( length < info.frame_count )
1296 mlt_playlist_split( this, clip, length );
1297 mlt_playlist_remove( this, clip );
1298 }
1299 mlt_playlist_insert( this, producer, clip, -1, -1 );
1300 ret = clip;
1301 }
1302 else
1303 {
1304 if ( mode == 1 ) {
1305 if ( position == info.start )
1306 mlt_playlist_remove( this, clip );
1307 else
1308 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) - 1 );
1309 }
1310 mlt_playlist_append( this, producer );
1311 ret = this->count - 1;
1312 }
1313 mlt_events_unblock( properties, this );
1314 mlt_playlist_virtual_refresh( this );
1315 }
1316 else
1317 {
1318 ret = -1;
1319 }
1320 return ret;
1321 }
1322
1323 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1324 {
1325 mlt_playlist_clip_info info;
1326 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1327 return info.start;
1328 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1329 }
1330
1331 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1332 {
1333 mlt_playlist_clip_info info;
1334 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1335 return info.frame_count;
1336 return 0;
1337 }
1338
1339 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1340 {
1341 int count = 0;
1342 mlt_playlist_clip_info info;
1343 if ( this != NULL && clip < this->count )
1344 {
1345 mlt_playlist_get_clip_info( this, &info, clip );
1346 if ( mlt_playlist_is_blank( this, clip ) )
1347 count += info.frame_count;
1348 if ( bounded == 0 )
1349 bounded = this->count;
1350 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1351 {
1352 mlt_playlist_get_clip_info( this, &info, clip );
1353 if ( mlt_playlist_is_blank( this, clip ) )
1354 count += info.frame_count;
1355 else
1356 bounded --;
1357 }
1358 }
1359 return count;
1360 }
1361
1362 int mlt_playlist_remove_region( mlt_playlist this, mlt_position position, int length )
1363 {
1364 int index = mlt_playlist_get_clip_index_at( this, position );
1365 if ( index >= 0 && index < this->count )
1366 {
1367 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1368 int clip_start = mlt_playlist_clip_start( this, index );
1369 int clip_length = mlt_playlist_clip_length( this, index );
1370 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1371 mlt_events_block( properties, this );
1372
1373 if ( position + length > list_length )
1374 length -= ( position + length - list_length );
1375
1376 if ( clip_start < position )
1377 {
1378 mlt_playlist_split( this, index ++, position - clip_start );
1379 clip_length -= position - clip_start;
1380 }
1381
1382 while( length > 0 )
1383 {
1384 if ( mlt_playlist_clip_length( this, index ) > length )
1385 mlt_playlist_split( this, index, length );
1386 length -= mlt_playlist_clip_length( this, index );
1387 mlt_playlist_remove( this, index );
1388 }
1389
1390 mlt_playlist_consolidate_blanks( this, 0 );
1391 mlt_events_unblock( properties, this );
1392 mlt_playlist_virtual_refresh( this );
1393
1394 // Just to be sure, we'll get the clip index again...
1395 index = mlt_playlist_get_clip_index_at( this, position );
1396 }
1397 return index;
1398 }
1399
1400 int mlt_playlist_move_region( mlt_playlist this, mlt_position position, int length, int new_position )
1401 {
1402 if ( this != NULL )
1403 {
1404 }
1405 return 0;
1406 }
1407
1408 /** Get the current frame.
1409 */
1410
1411 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1412 {
1413 // Check that we have a producer
1414 if ( producer == NULL )
1415 {
1416 *frame = NULL;
1417 return -1;
1418 }
1419
1420 // Get this mlt_playlist
1421 mlt_playlist this = producer->child;
1422
1423 // Need to ensure the frame is deinterlaced when repeating 1 frame
1424 int progressive = 0;
1425
1426 // Get the real producer
1427 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1428
1429 // Check that we have a producer
1430 if ( real == NULL )
1431 {
1432 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1433 return 0;
1434 }
1435
1436 // Get the frame
1437 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1438 {
1439 mlt_service_get_frame( real, frame, index );
1440 }
1441 else
1442 {
1443 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1444 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1445 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1446 mlt_frame_push_service( *frame, NULL );
1447 mlt_frame_push_audio( *frame, NULL );
1448 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1449 mlt_service_apply_filters( real, *frame, 0 );
1450 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1451 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1452 }
1453
1454 // Check if we're at the end of the clip
1455 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1456 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1457 mlt_playlist_virtual_set_out( this );
1458
1459 // Set the consumer progressive property
1460 if ( progressive )
1461 {
1462 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1463 mlt_properties_set_int( properties, "test_audio", 1 );
1464 }
1465
1466 // Check for notifier and call with appropriate argument
1467 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1468 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1469 if ( notifier != NULL )
1470 {
1471 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1472 notifier( argument );
1473 }
1474
1475 // Update position on the frame we're creating
1476 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1477
1478 // Position ourselves on the next frame
1479 mlt_producer_prepare_next( producer );
1480
1481 return 0;
1482 }
1483
1484 /** Close the playlist.
1485 */
1486
1487 void mlt_playlist_close( mlt_playlist this )
1488 {
1489 if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1490 {
1491 int i = 0;
1492 this->parent.close = NULL;
1493 for ( i = 0; i < this->count; i ++ )
1494 {
1495 mlt_event_close( this->list[ i ]->event );
1496 mlt_producer_close( this->list[ i ]->producer );
1497 free( this->list[ i ] );
1498 }
1499 mlt_producer_close( &this->blank );
1500 mlt_producer_close( &this->parent );
1501 free( this->list );
1502 free( this );
1503 }
1504 }