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