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