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