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