framework/mlt_playlist.c: check length before inserting blank, which fixes one frame...
[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 || this->list[ index ]->producer == NULL;
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 if (length >= 0)
597 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
598 else
599 return 1;
600 }
601
602 /** Insert a producer into the playlist.
603 */
604
605 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
606 {
607 // Append to end
608 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
609 mlt_playlist_append_io( this, producer, in, out );
610
611 // Move to the position specified
612 mlt_playlist_move( this, this->count - 1, where );
613 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
614
615 return mlt_playlist_virtual_refresh( this );
616 }
617
618 /** Remove an entry in the playlist.
619 */
620
621 int mlt_playlist_remove( mlt_playlist this, int where )
622 {
623 int error = where < 0 || where >= this->count;
624 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
625 {
626 // We need to know the current clip and the position within the playlist
627 int current = mlt_playlist_current_clip( this );
628 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
629
630 // We need all the details about the clip we're removing
631 mlt_playlist_clip_info where_info;
632 playlist_entry *entry = this->list[ where ];
633 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
634
635 // Loop variable
636 int i = 0;
637
638 // Get the clip info
639 mlt_playlist_get_clip_info( this, &where_info, where );
640
641 // Make sure the clip to be removed is valid and correct if necessary
642 if ( where < 0 )
643 where = 0;
644 if ( where >= this->count )
645 where = this->count - 1;
646
647 // Reorganise the list
648 for ( i = where + 1; i < this->count; i ++ )
649 this->list[ i - 1 ] = this->list[ i ];
650 this->count --;
651
652 if ( entry->preservation_hack == 0 )
653 {
654 // Decouple from mix_in/out if necessary
655 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
656 {
657 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
658 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
659 }
660 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
661 {
662 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
663 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
664 }
665
666 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
667 mlt_producer_clear( entry->producer );
668 }
669
670 // Close the producer associated to the clip info
671 mlt_event_close( entry->event );
672 mlt_producer_close( entry->producer );
673
674 // Correct position
675 if ( where == current )
676 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
677 else if ( where < current && this->count > 0 )
678 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
679 else if ( this->count == 0 )
680 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
681
682 // Free the entry
683 free( entry );
684
685 // Refresh the playlist
686 mlt_playlist_virtual_refresh( this );
687 }
688
689 return error;
690 }
691
692 /** Move an entry in the playlist.
693 */
694
695 int mlt_playlist_move( mlt_playlist this, int src, int dest )
696 {
697 int i;
698
699 /* We need to ensure that the requested indexes are valid and correct it as necessary */
700 if ( src < 0 )
701 src = 0;
702 if ( src >= this->count )
703 src = this->count - 1;
704
705 if ( dest < 0 )
706 dest = 0;
707 if ( dest >= this->count )
708 dest = this->count - 1;
709
710 if ( src != dest && this->count > 1 )
711 {
712 int current = mlt_playlist_current_clip( this );
713 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
714 playlist_entry *src_entry = NULL;
715
716 // We need all the details about the current clip
717 mlt_playlist_clip_info current_info;
718
719 mlt_playlist_get_clip_info( this, &current_info, current );
720 position -= current_info.start;
721
722 if ( current == src )
723 current = dest;
724 else if ( current > src && current < dest )
725 current ++;
726 else if ( current == dest )
727 current = src;
728
729 src_entry = this->list[ src ];
730 if ( src > dest )
731 {
732 for ( i = src; i > dest; i -- )
733 this->list[ i ] = this->list[ i - 1 ];
734 }
735 else
736 {
737 for ( i = src; i < dest; i ++ )
738 this->list[ i ] = this->list[ i + 1 ];
739 }
740 this->list[ dest ] = src_entry;
741
742 mlt_playlist_get_clip_info( this, &current_info, current );
743 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
744 mlt_playlist_virtual_refresh( this );
745 }
746
747 return 0;
748 }
749
750 /** Repeat the specified clip n times.
751 */
752
753 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
754 {
755 int error = repeat < 1 || clip < 0 || clip >= this->count;
756 if ( error == 0 )
757 {
758 playlist_entry *entry = this->list[ clip ];
759 entry->repeat = repeat;
760 mlt_playlist_virtual_refresh( this );
761 }
762 return error;
763 }
764
765 /** Resize the specified clip.
766 */
767
768 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
769 {
770 int error = clip < 0 || clip >= this->count;
771 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
772 {
773 playlist_entry *entry = this->list[ clip ];
774 mlt_producer producer = entry->producer;
775 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
776
777 mlt_events_block( properties, properties );
778
779 if ( mlt_producer_is_blank( producer ) )
780 {
781 // Make sure the blank is long enough to accomodate the length specified
782 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
783 {
784 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
785 mlt_properties_set_int( blank_props, "length", out - in + 1 );
786 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
787 mlt_producer_set_in_and_out( &this->blank, 0, out - in );
788 }
789 }
790
791 if ( in <= -1 )
792 in = 0;
793 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
794 out = mlt_producer_get_length( producer ) - 1;
795
796 if ( out < in )
797 {
798 mlt_position t = in;
799 in = out;
800 out = t;
801 }
802
803 mlt_producer_set_in_and_out( producer, in, out );
804 mlt_events_unblock( properties, properties );
805 mlt_playlist_virtual_refresh( this );
806 }
807 return error;
808 }
809
810 /** Split a clip on the playlist at the given position.
811 */
812
813 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
814 {
815 int error = clip < 0 || clip >= this->count;
816 if ( error == 0 )
817 {
818 playlist_entry *entry = this->list[ clip ];
819 position = position < 0 ? entry->frame_count + position - 1 : position;
820 if ( position >= 0 && position < entry->frame_count - 1 )
821 {
822 int in = entry->frame_in;
823 int out = entry->frame_out;
824 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
825 mlt_playlist_resize_clip( this, clip, in, in + position );
826 if ( !mlt_producer_is_blank( entry->producer ) )
827 {
828 int i = 0;
829 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
830 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
831 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
832 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
833 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
834 {
835 char *name = mlt_properties_get_name( entry_properties, i );
836 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
837 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
838 }
839 mlt_producer_close( split );
840 }
841 else
842 {
843 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
844 }
845 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
846 mlt_playlist_virtual_refresh( this );
847 }
848 else
849 {
850 error = 1;
851 }
852 }
853 return error;
854 }
855
856 /** Split the playlist at the absolute position.
857 */
858
859 int mlt_playlist_split_at( mlt_playlist this, mlt_position position, int left )
860 {
861 int result = this == NULL ? -1 : 0;
862 if ( !result )
863 {
864 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) ) )
865 {
866 int clip = mlt_playlist_get_clip_index_at( this, position );
867 mlt_playlist_clip_info info;
868 mlt_playlist_get_clip_info( this, &info, clip );
869 if ( left && position != info.start )
870 mlt_playlist_split( this, clip, position - info.start - 1 );
871 else if ( !left )
872 mlt_playlist_split( this, clip, position - info.start );
873 result = position;
874 }
875 else if ( position <= 0 )
876 {
877 result = 0;
878 }
879 else
880 {
881 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
882 }
883 }
884 return result;
885 }
886
887 /** Join 1 or more consecutive clips.
888 */
889
890 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
891 {
892 int error = clip < 0 || clip >= this->count;
893 if ( error == 0 )
894 {
895 int i = clip;
896 mlt_playlist new_clip = mlt_playlist_init( );
897 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
898 if ( clip + count >= this->count )
899 count = this->count - clip - 1;
900 for ( i = 0; i <= count; i ++ )
901 {
902 playlist_entry *entry = this->list[ clip ];
903 mlt_playlist_append( new_clip, entry->producer );
904 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
905 entry->preservation_hack = 1;
906 mlt_playlist_remove( this, clip );
907 }
908 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
909 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
910 mlt_playlist_close( new_clip );
911 }
912 return error;
913 }
914
915 /** Mix consecutive clips for a specified length and apply transition if specified.
916 */
917
918 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
919 {
920 int error = ( clip < 0 || clip + 1 >= this->count );
921 if ( error == 0 )
922 {
923 playlist_entry *clip_a = this->list[ clip ];
924 playlist_entry *clip_b = this->list[ clip + 1 ];
925 mlt_producer track_a = NULL;
926 mlt_producer track_b = NULL;
927 mlt_tractor tractor = mlt_tractor_new( );
928 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
929
930 // Check length is valid for both clips and resize if necessary.
931 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
932 length = length > max_size ? max_size : length;
933
934 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
935 if ( length != clip_a->frame_count )
936 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
937 else
938 track_a = clip_a->producer;
939
940 if ( length != clip_b->frame_count )
941 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
942 else
943 track_b = clip_b->producer;
944
945 // Set the tracks on the tractor
946 mlt_tractor_set_track( tractor, track_a, 0 );
947 mlt_tractor_set_track( tractor, track_b, 1 );
948
949 // Insert the mix object into the playlist
950 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
951 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
952
953 // Attach the transition
954 if ( transition != NULL )
955 {
956 mlt_field field = mlt_tractor_field( tractor );
957 mlt_field_plant_transition( field, transition, 0, 1 );
958 mlt_transition_set_in_and_out( transition, 0, length - 1 );
959 }
960
961 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
962 if ( track_a != clip_a->producer )
963 mlt_producer_close( track_a );
964 if ( track_b != clip_b->producer )
965 mlt_producer_close( track_b );
966
967 // Check if we have anything left on the right hand clip
968 if ( track_b == clip_b->producer )
969 {
970 clip_b->preservation_hack = 1;
971 mlt_playlist_remove( this, clip + 2 );
972 }
973 else if ( clip_b->frame_out - clip_b->frame_in > length )
974 {
975 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
976 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
977 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
978 }
979 else
980 {
981 mlt_producer_clear( clip_b->producer );
982 mlt_playlist_remove( this, clip + 2 );
983 }
984
985 // Check if we have anything left on the left hand clip
986 if ( track_a == clip_a->producer )
987 {
988 clip_a->preservation_hack = 1;
989 mlt_playlist_remove( this, clip );
990 }
991 else if ( clip_a->frame_out - clip_a->frame_in > length )
992 {
993 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
994 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
995 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
996 }
997 else
998 {
999 mlt_producer_clear( clip_a->producer );
1000 mlt_playlist_remove( this, clip );
1001 }
1002
1003 // Unblock and force a fire off of change events to listeners
1004 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1005 mlt_playlist_virtual_refresh( this );
1006 mlt_tractor_close( tractor );
1007 }
1008 return error;
1009 }
1010
1011 /** Add a transition to an existing mix.
1012 */
1013
1014 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
1015 {
1016 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1017 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1018 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1019 int error = transition == NULL || tractor == NULL;
1020 if ( error == 0 )
1021 {
1022 mlt_field field = mlt_tractor_field( tractor );
1023 mlt_field_plant_transition( field, transition, 0, 1 );
1024 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
1025 }
1026 return error;
1027 }
1028
1029 /** Return the clip at the clip index.
1030 */
1031
1032 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
1033 {
1034 if ( clip >= 0 && clip < this->count )
1035 return this->list[ clip ]->producer;
1036 return NULL;
1037 }
1038
1039 /** Return the clip at the specified position.
1040 */
1041
1042 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, mlt_position position )
1043 {
1044 int index = 0, total = 0;
1045 return mlt_playlist_locate( this, &position, &index, &total );
1046 }
1047
1048 /** Return the clip index of the specified position.
1049 */
1050
1051 int mlt_playlist_get_clip_index_at( mlt_playlist this, mlt_position position )
1052 {
1053 int index = 0, total = 0;
1054 mlt_playlist_locate( this, &position, &index, &total );
1055 return index;
1056 }
1057
1058 /** Determine if the clip is a mix.
1059 */
1060
1061 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1062 {
1063 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1064 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1065 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1066 return tractor != NULL;
1067 }
1068
1069 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1070 back correctly on to the playlist.
1071 */
1072
1073 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1074 {
1075 int error = ( clip < 0 || clip >= this->count );
1076
1077 // Ensure that the clip request is actually a mix
1078 if ( error == 0 )
1079 {
1080 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1081 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1082 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1083 this->list[ clip ]->preservation_hack;
1084 }
1085
1086 if ( error == 0 )
1087 {
1088 playlist_entry *mix = this->list[ clip ];
1089 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1090 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1091 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1092 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1093 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1094 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1095
1096 if ( clip_a != NULL )
1097 {
1098 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1099 }
1100 else
1101 {
1102 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1103 mlt_playlist_insert( this, cut, clip, -1, -1 );
1104 clip ++;
1105 }
1106
1107 if ( clip_b != NULL )
1108 {
1109 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1110 }
1111 else
1112 {
1113 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1114 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1115 }
1116
1117 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1118 mlt_playlist_remove( this, clip );
1119 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1120 mlt_playlist_virtual_refresh( this );
1121 }
1122 return error;
1123 }
1124
1125 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1126 {
1127 int error = ( clip < 0 || clip >= this->count );
1128
1129 // Ensure that the clip request is actually a mix
1130 if ( error == 0 )
1131 {
1132 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1133 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1134 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1135 }
1136
1137 if ( error == 0 )
1138 {
1139 playlist_entry *mix = this->list[ clip ];
1140 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1141 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1142 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1143 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1144 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1145 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1146 int length = out - in + 1;
1147 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1148 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1149
1150 if ( clip_a != NULL )
1151 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1152
1153 if ( clip_b != NULL )
1154 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1155
1156 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1157 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1158 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1159 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1160 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1161 mlt_producer_set_in_and_out( mix->producer, in, out );
1162
1163 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1164 mlt_playlist_virtual_refresh( this );
1165 }
1166 return error;
1167 }
1168
1169 /** Consolodate adjacent blank producers.
1170 */
1171
1172 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1173 {
1174 if ( this != NULL )
1175 {
1176 int i = 0;
1177 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1178
1179 mlt_events_block( properties, properties );
1180 for ( i = 1; i < this->count; i ++ )
1181 {
1182 playlist_entry *left = this->list[ i - 1 ];
1183 playlist_entry *right = this->list[ i ];
1184
1185 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1186 {
1187 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1188 mlt_playlist_remove( this, i -- );
1189 }
1190 }
1191
1192 if ( !keep_length && this->count > 0 )
1193 {
1194 playlist_entry *last = this->list[ this->count - 1 ];
1195 if ( mlt_producer_is_blank( last->producer ) )
1196 mlt_playlist_remove( this, this->count - 1 );
1197 }
1198
1199 mlt_events_unblock( properties, properties );
1200 mlt_playlist_virtual_refresh( this );
1201 }
1202 }
1203
1204 /** Determine if the specified clip index is a blank.
1205 */
1206
1207 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1208 {
1209 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1210 }
1211
1212 /** Determine if the specified position is a blank.
1213 */
1214
1215 int mlt_playlist_is_blank_at( mlt_playlist this, mlt_position position )
1216 {
1217 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( this, position ) );
1218 }
1219
1220 /** Replace the specified clip with a blank and return the clip.
1221 */
1222
1223 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1224 {
1225 mlt_producer producer = NULL;
1226 if ( !mlt_playlist_is_blank( this, clip ) )
1227 {
1228 playlist_entry *entry = this->list[ clip ];
1229 int in = entry->frame_in;
1230 int out = entry->frame_out;
1231 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1232 producer = entry->producer;
1233 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1234 mlt_events_block( properties, properties );
1235 mlt_playlist_remove( this, clip );
1236 mlt_playlist_blank( this, out - in );
1237 mlt_playlist_move( this, this->count - 1, clip );
1238 mlt_events_unblock( properties, properties );
1239 mlt_playlist_virtual_refresh( this );
1240 mlt_producer_set_in_and_out( producer, in, out );
1241 }
1242 return producer;
1243 }
1244
1245 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1246 {
1247 if ( this != NULL && length >= 0 )
1248 {
1249 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1250 mlt_events_block( properties, properties );
1251 mlt_playlist_blank( this, length );
1252 mlt_playlist_move( this, this->count - 1, clip );
1253 mlt_events_unblock( properties, properties );
1254 mlt_playlist_virtual_refresh( this );
1255 }
1256 }
1257
1258 void mlt_playlist_pad_blanks( mlt_playlist this, mlt_position position, int length, int find )
1259 {
1260 if ( this != NULL && length != 0 )
1261 {
1262 int clip = mlt_playlist_get_clip_index_at( this, position );
1263 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1264 mlt_events_block( properties, properties );
1265 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1266 clip ++;
1267 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1268 {
1269 mlt_playlist_clip_info info;
1270 mlt_playlist_get_clip_info( this, &info, clip );
1271 if ( info.frame_out + length > info.frame_in )
1272 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1273 else
1274 mlt_playlist_remove( this, clip );
1275 }
1276 else if ( find && clip < this->count && length > 0 )
1277 {
1278 mlt_playlist_insert_blank( this, clip, length );
1279 }
1280 mlt_events_unblock( properties, properties );
1281 mlt_playlist_virtual_refresh( this );
1282 }
1283 }
1284
1285 int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_producer producer, int mode )
1286 {
1287 int ret = this == NULL || position < 0 || producer == NULL;
1288 if ( ret == 0 )
1289 {
1290 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1291 int length = mlt_producer_get_playtime( producer );
1292 int clip = mlt_playlist_get_clip_index_at( this, position );
1293 mlt_playlist_clip_info info;
1294 mlt_playlist_get_clip_info( this, &info, clip );
1295 mlt_events_block( properties, this );
1296 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1297 {
1298 // Split and move to new clip if need be
1299 if ( position != info.start && mlt_playlist_split( this, clip, position - info.start - 1 ) == 0 )
1300 mlt_playlist_get_clip_info( this, &info, ++ clip );
1301
1302 // Split again if need be
1303 if ( length < info.frame_count )
1304 mlt_playlist_split( this, clip, length - 1 );
1305
1306 // Remove
1307 mlt_playlist_remove( this, clip );
1308
1309 // Insert
1310 mlt_playlist_insert( this, producer, clip, -1, -1 );
1311 ret = clip;
1312 }
1313 else if ( clip < this->count )
1314 {
1315 if ( position > info.start + info.frame_count / 2 )
1316 clip ++;
1317 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1318 {
1319 mlt_playlist_get_clip_info( this, &info, clip );
1320 if ( length < info.frame_count )
1321 mlt_playlist_split( this, clip, length );
1322 mlt_playlist_remove( this, clip );
1323 }
1324 mlt_playlist_insert( this, producer, clip, -1, -1 );
1325 ret = clip;
1326 }
1327 else
1328 {
1329 if ( mode == 1 ) {
1330 if ( position == info.start )
1331 mlt_playlist_remove( this, clip );
1332 else
1333 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) - 1 );
1334 }
1335 mlt_playlist_append( this, producer );
1336 ret = this->count - 1;
1337 }
1338 mlt_events_unblock( properties, this );
1339 mlt_playlist_virtual_refresh( this );
1340 }
1341 else
1342 {
1343 ret = -1;
1344 }
1345 return ret;
1346 }
1347
1348 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1349 {
1350 mlt_playlist_clip_info info;
1351 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1352 return info.start;
1353 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1354 }
1355
1356 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1357 {
1358 mlt_playlist_clip_info info;
1359 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1360 return info.frame_count;
1361 return 0;
1362 }
1363
1364 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1365 {
1366 int count = 0;
1367 mlt_playlist_clip_info info;
1368 if ( this != NULL && clip < this->count )
1369 {
1370 mlt_playlist_get_clip_info( this, &info, clip );
1371 if ( mlt_playlist_is_blank( this, clip ) )
1372 count += info.frame_count;
1373 if ( bounded == 0 )
1374 bounded = this->count;
1375 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1376 {
1377 mlt_playlist_get_clip_info( this, &info, clip );
1378 if ( mlt_playlist_is_blank( this, clip ) )
1379 count += info.frame_count;
1380 else
1381 bounded --;
1382 }
1383 }
1384 return count;
1385 }
1386
1387 int mlt_playlist_remove_region( mlt_playlist this, mlt_position position, int length )
1388 {
1389 int index = mlt_playlist_get_clip_index_at( this, position );
1390 if ( index >= 0 && index < this->count )
1391 {
1392 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1393 int clip_start = mlt_playlist_clip_start( this, index );
1394 int clip_length = mlt_playlist_clip_length( this, index );
1395 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1396 mlt_events_block( properties, this );
1397
1398 if ( position + length > list_length )
1399 length -= ( position + length - list_length );
1400
1401 if ( clip_start < position )
1402 {
1403 mlt_playlist_split( this, index ++, position - clip_start );
1404 clip_length -= position - clip_start;
1405 }
1406
1407 while( length > 0 )
1408 {
1409 if ( mlt_playlist_clip_length( this, index ) > length )
1410 mlt_playlist_split( this, index, length );
1411 length -= mlt_playlist_clip_length( this, index );
1412 mlt_playlist_remove( this, index );
1413 }
1414
1415 mlt_playlist_consolidate_blanks( this, 0 );
1416 mlt_events_unblock( properties, this );
1417 mlt_playlist_virtual_refresh( this );
1418
1419 // Just to be sure, we'll get the clip index again...
1420 index = mlt_playlist_get_clip_index_at( this, position );
1421 }
1422 return index;
1423 }
1424
1425 int mlt_playlist_move_region( mlt_playlist this, mlt_position position, int length, int new_position )
1426 {
1427 if ( this != NULL )
1428 {
1429 }
1430 return 0;
1431 }
1432
1433 /** Get the current frame.
1434 */
1435
1436 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1437 {
1438 // Check that we have a producer
1439 if ( producer == NULL )
1440 {
1441 *frame = NULL;
1442 return -1;
1443 }
1444
1445 // Get this mlt_playlist
1446 mlt_playlist this = producer->child;
1447
1448 // Need to ensure the frame is deinterlaced when repeating 1 frame
1449 int progressive = 0;
1450
1451 // Get the real producer
1452 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1453
1454 // Check that we have a producer
1455 if ( real == NULL )
1456 {
1457 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1458 return 0;
1459 }
1460
1461 // Get the frame
1462 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1463 {
1464 mlt_service_get_frame( real, frame, index );
1465 }
1466 else
1467 {
1468 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1469 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1470 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1471 mlt_frame_push_service( *frame, NULL );
1472 mlt_frame_push_audio( *frame, NULL );
1473 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1474 mlt_service_apply_filters( real, *frame, 0 );
1475 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1476 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1477 }
1478
1479 // Check if we're at the end of the clip
1480 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1481 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1482 mlt_playlist_virtual_set_out( this );
1483
1484 // Set the consumer progressive property
1485 if ( progressive )
1486 {
1487 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1488 mlt_properties_set_int( properties, "test_audio", 1 );
1489 }
1490
1491 // Check for notifier and call with appropriate argument
1492 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1493 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1494 if ( notifier != NULL )
1495 {
1496 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1497 notifier( argument );
1498 }
1499
1500 // Update position on the frame we're creating
1501 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1502
1503 // Position ourselves on the next frame
1504 mlt_producer_prepare_next( producer );
1505
1506 return 0;
1507 }
1508
1509 /** Close the playlist.
1510 */
1511
1512 void mlt_playlist_close( mlt_playlist this )
1513 {
1514 if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1515 {
1516 int i = 0;
1517 this->parent.close = NULL;
1518 for ( i = 0; i < this->count; i ++ )
1519 {
1520 mlt_event_close( this->list[ i ]->event );
1521 mlt_producer_close( this->list[ i ]->producer );
1522 free( this->list[ i ] );
1523 }
1524 mlt_producer_close( &this->blank );
1525 mlt_producer_close( &this->parent );
1526 free( this->list );
1527 free( this );
1528 }
1529 }