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