3 * \brief playlist service class
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
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.
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.
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
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"
35 /** \brief Virtual playlist entry used by mlt_playlist_s
38 struct playlist_entry_s
40 mlt_producer producer;
41 mlt_position frame_in;
42 mlt_position frame_out;
43 mlt_position frame_count;
45 mlt_position producer_length;
47 int preservation_hack;
50 /* Forward declarations
53 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
54 static int mlt_playlist_unmix( mlt_playlist self, int clip );
55 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out );
56 static void mlt_playlist_next( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
58 /** Construct a playlist.
60 * Sets the resource property to "<playlist>".
61 * Set the mlt_type to property to "mlt_producer".
62 * \public \memberof mlt_playlist_s
63 * \return a new playlist
66 mlt_playlist mlt_playlist_init( )
68 mlt_playlist self = calloc( 1, sizeof( struct mlt_playlist_s ) );
71 mlt_producer producer = &self->parent;
73 // Construct the producer
74 if ( mlt_producer_init( producer, self ) != 0 ) goto error1;
76 // Override the producer get_frame
77 producer->get_frame = producer_get_frame;
79 // Define the destructor
80 producer->close = ( mlt_destructor )mlt_playlist_close;
81 producer->close_object = self;
84 if ( mlt_producer_init( &self->blank, NULL ) != 0 ) goto error1;
85 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &self->blank ), "mlt_service", "blank" );
86 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &self->blank ), "resource", "blank" );
88 // Indicate that this producer is a playlist
89 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "playlist", self, 0, NULL, NULL );
91 // Specify the eof condition
92 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "eof", "pause" );
93 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "resource", "<playlist>" );
94 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "mlt_type", "mlt_producer" );
95 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "in", 0 );
96 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "out", -1 );
97 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "length", 0 );
100 self->list = calloc( self->size, sizeof( playlist_entry * ) );
101 if ( self->list == NULL ) goto error2;
103 mlt_events_register( MLT_PLAYLIST_PROPERTIES( self ), "playlist-next", (mlt_transmitter) mlt_playlist_next );
114 /** Construct a playlist with a profile.
116 * Sets the resource property to "<playlist>".
117 * Set the mlt_type to property to "mlt_producer".
118 * \public \memberof mlt_playlist_s
119 * \param profile the profile to use with the profile
120 * \return a new playlist
123 mlt_playlist mlt_playlist_new( mlt_profile profile )
125 mlt_playlist self = mlt_playlist_init();
127 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "_profile", profile, 0, NULL, NULL );
131 /** Get the producer associated to this playlist.
133 * \public \memberof mlt_playlist_s
134 * \param self a playlist
135 * \return the producer interface
136 * \see MLT_PLAYLIST_PRODUCER
139 mlt_producer mlt_playlist_producer( mlt_playlist self )
141 return self != NULL ? &self->parent : NULL;
144 /** Get the service associated to this playlist.
146 * \public \memberof mlt_playlist_s
147 * \param self a playlist
148 * \return the service interface
149 * \see MLT_PLAYLIST_SERVICE
152 mlt_service mlt_playlist_service( mlt_playlist self )
154 return MLT_PRODUCER_SERVICE( &self->parent );
157 /** Get the properties associated to this playlist.
159 * \public \memberof mlt_playlist_s
160 * \param self a playlist
161 * \return the playlist's properties list
162 * \see MLT_PLAYLIST_PROPERTIES
165 mlt_properties mlt_playlist_properties( mlt_playlist self )
167 return MLT_PRODUCER_PROPERTIES( &self->parent );
170 /** Refresh the playlist after a clip has been changed.
172 * \private \memberof mlt_playlist_s
173 * \param self a playlist
177 static int mlt_playlist_virtual_refresh( mlt_playlist self )
179 // Obtain the properties
180 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
182 mlt_position frame_count = 0;
184 for ( i = 0; i < self->count; i ++ )
187 mlt_producer producer = self->list[ i ]->producer;
190 int current_length = mlt_producer_get_playtime( producer );
192 // Check if the length of the producer has changed
193 if ( self->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
194 self->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
196 // This clip should be removed...
197 if ( current_length < 1 )
199 self->list[ i ]->frame_in = 0;
200 self->list[ i ]->frame_out = -1;
201 self->list[ i ]->frame_count = 0;
205 self->list[ i ]->frame_in = mlt_producer_get_in( producer );
206 self->list[ i ]->frame_out = mlt_producer_get_out( producer );
207 self->list[ i ]->frame_count = current_length;
210 // Update the producer_length
211 self->list[ i ]->producer_length = current_length;
215 // Calculate the frame_count
216 self->list[ i ]->frame_count = ( self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1 ) * self->list[ i ]->repeat;
218 // Update the frame_count for self clip
219 frame_count += self->list[ i ]->frame_count;
222 // Refresh all properties
223 mlt_events_block( properties, properties );
224 mlt_properties_set_position( properties, "length", frame_count );
225 mlt_events_unblock( properties, properties );
226 mlt_properties_set_position( properties, "out", frame_count - 1 );
231 /** Listener for producers on the playlist.
233 * Refreshes the playlist whenever an entry receives producer-changed.
234 * \private \memberof mlt_playlist_s
235 * \param producer a producer
236 * \param self a playlist
239 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist self )
241 mlt_playlist_virtual_refresh( self );
244 /** Append to the virtual playlist.
246 * \private \memberof mlt_playlist_s
247 * \param self a playlist
248 * \param source a producer
249 * \param in the producer's starting time
250 * \param out the producer's ending time
251 * \return true if there was an error
254 static int mlt_playlist_virtual_append( mlt_playlist self, mlt_producer source, mlt_position in, mlt_position out )
256 mlt_producer producer = NULL;
257 mlt_properties properties = NULL;
258 mlt_properties parent = NULL;
260 // If we have a cut, then use the in/out points from the cut
261 if ( mlt_producer_is_blank( source ) )
263 mlt_position length = out - in + 1;
265 // Make sure the blank is long enough to accomodate the length specified
266 if ( length > mlt_producer_get_length( &self->blank ) )
268 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
269 mlt_events_block( blank_props, blank_props );
270 mlt_producer_set_in_and_out( &self->blank, in, out );
271 mlt_events_unblock( blank_props, blank_props );
274 // Now make sure the cut comes from this self->blank
275 if ( source == NULL )
277 producer = mlt_producer_cut( &self->blank, in, out );
279 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &self->blank )
281 producer = mlt_producer_cut( &self->blank, in, out );
286 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
289 properties = MLT_PRODUCER_PROPERTIES( producer );
291 // Make sure this cut of blank is long enough
292 if ( length > mlt_producer_get_length( producer ) )
293 mlt_properties_set_int( properties, "length", length );
295 else if ( mlt_producer_is_cut( source ) )
299 in = mlt_producer_get_in( producer );
300 if ( out < 0 || out > mlt_producer_get_out( producer ) )
301 out = mlt_producer_get_out( producer );
302 properties = MLT_PRODUCER_PROPERTIES( producer );
303 mlt_properties_inc_ref( properties );
307 producer = mlt_producer_cut( source, in, out );
308 if ( in < 0 || in < mlt_producer_get_in( producer ) )
309 in = mlt_producer_get_in( producer );
310 if ( out < 0 || out > mlt_producer_get_out( producer ) )
311 out = mlt_producer_get_out( producer );
312 properties = MLT_PRODUCER_PROPERTIES( producer );
315 // Fetch the cuts parent properties
316 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
318 // Remove loader normalisers for fx cuts
319 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
321 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
322 mlt_filter filter = mlt_service_filter( service, 0 );
323 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_loader" ) )
325 mlt_service_detach( service, filter );
326 filter = mlt_service_filter( service, 0 );
328 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
331 // Check that we have room
332 if ( self->count >= self->size )
335 self->list = realloc( self->list, ( self->size + 10 ) * sizeof( playlist_entry * ) );
336 for ( i = self->size; i < self->size + 10; i ++ ) self->list[ i ] = NULL;
341 self->list[ self->count ] = calloc( 1, sizeof( playlist_entry ) );
342 if ( self->list[ self->count ] != NULL )
344 self->list[ self->count ]->producer = producer;
345 self->list[ self->count ]->frame_in = in;
346 self->list[ self->count ]->frame_out = out;
347 self->list[ self->count ]->frame_count = out - in + 1;
348 self->list[ self->count ]->repeat = 1;
349 self->list[ self->count ]->producer_length = mlt_producer_get_playtime( producer );
350 self->list[ self->count ]->event = mlt_events_listen( parent, self, "producer-changed", ( mlt_listener )mlt_playlist_listener );
351 mlt_event_inc_ref( self->list[ self->count ]->event );
352 mlt_properties_set( properties, "eof", "pause" );
353 mlt_producer_set_speed( producer, 0 );
357 return mlt_playlist_virtual_refresh( self );
360 /** Locate a producer by index.
362 * \private \memberof mlt_playlist_s
363 * \param self a playlist
364 * \param[in, out] position the time at which to locate the producer, returns the time relative to the producer's starting point
365 * \param[out] clip the index of the playlist entry
366 * \param[out] total the duration of the playlist up to and including this producer
367 * \return a producer or NULL if not found
370 static mlt_producer mlt_playlist_locate( mlt_playlist self, mlt_position *position, int *clip, int *total )
372 // Default producer to NULL
373 mlt_producer producer = NULL;
375 // Loop for each producer until found
376 for ( *clip = 0; *clip < self->count; *clip += 1 )
378 // Increment the total
379 *total += self->list[ *clip ]->frame_count;
381 // Check if the position indicates that we have found the clip
382 // Note that 0 length clips get skipped automatically
383 if ( *position < self->list[ *clip ]->frame_count )
385 // Found it, now break
386 producer = self->list[ *clip ]->producer;
391 // Decrement position by length of self entry
392 *position -= self->list[ *clip ]->frame_count;
399 /** The transmitter for the producer-next event
401 * Invokes the listener.
403 * \private \memberof mlt_playlist_s
404 * \param listener a function pointer that will be invoked
405 * \param owner the events object that will be passed to \p listener
406 * \param self a service that will be passed to \p listener
407 * \param args an array of pointers.
410 static void mlt_playlist_next( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
413 listener( owner, self, args[ 0 ] );
417 /** Seek in the virtual playlist.
419 * This gets the producer at the current position and seeks on the producer
420 * while doing repeat and end-of-file handling. This is also responsible for
421 * closing producers previous to the preceding playlist if the autoclose
423 * \private \memberof mlt_playlist_s
424 * \param self a playlist
425 * \param[out] progressive true if the producer should be displayed progressively
426 * \return the service interface of the producer at the play head
427 * \see producer_get_frame
430 static mlt_service mlt_playlist_virtual_seek( mlt_playlist self, int *progressive )
432 // Map playlist position to real producer in virtual playlist
433 mlt_position position = mlt_producer_frame( &self->parent );
435 // Keep the original position since we change it while iterating through the list
436 mlt_position original = position;
438 // Clip index and total
442 // Locate the producer for the position
443 mlt_producer producer = mlt_playlist_locate( self, &position, &i, &total );
445 // Get the properties
446 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
448 // Automatically close previous producers if requested
449 if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
450 && position < 2 // tolerate off-by-one error on going to next clip
451 && mlt_properties_get_int( properties, "autoclose" ) )
454 // They might have jumped ahead!
455 for ( j = 0; j < i - 1; j++ )
457 mlt_service_lock( MLT_PRODUCER_SERVICE( self->list[ j ]->producer ) );
458 mlt_producer p = self->list[ j ]->producer;
461 self->list[ j ]->producer = NULL;
462 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
463 mlt_producer_close( p );
465 // If p is null, the lock will not have been "taken"
469 // Get the eof handling
470 char *eof = mlt_properties_get( properties, "eof" );
472 // Seek in real producer to relative position
473 if ( producer != NULL )
475 int count = self->list[ i ]->frame_count / self->list[ i ]->repeat;
476 *progressive = count == 1;
477 mlt_producer_seek( producer, (int)position % count );
479 else if ( !strcmp( eof, "pause" ) && total > 0 )
481 playlist_entry *entry = self->list[ self->count - 1 ];
482 int count = entry->frame_count / entry->repeat;
483 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
484 mlt_producer_seek( self_producer, original - 1 );
485 producer = entry->producer;
486 mlt_producer_seek( producer, (int)entry->frame_out % count );
487 mlt_producer_set_speed( self_producer, 0 );
488 mlt_producer_set_speed( producer, 0 );
489 *progressive = count == 1;
491 else if ( !strcmp( eof, "loop" ) && total > 0 )
493 playlist_entry *entry = self->list[ 0 ];
494 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
495 mlt_producer_seek( self_producer, 0 );
496 producer = entry->producer;
497 mlt_producer_seek( producer, 0 );
501 producer = &self->blank;
504 // Determine if we have moved to the next entry in the playlist.
505 if ( original == total - 2 )
506 mlt_events_fire( properties, "playlist-next", i, NULL );
508 return MLT_PRODUCER_SERVICE( producer );
511 /** Invoked when a producer indicates that it has prematurely reached its end.
513 * \private \memberof mlt_playlist_s
514 * \param self a playlist
516 * \see producer_get_frame
519 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist self )
521 // Default producer to blank
522 mlt_producer producer = &self->blank;
524 // Map playlist position to real producer in virtual playlist
525 mlt_position position = mlt_producer_frame( &self->parent );
527 // Loop through the virtual playlist
530 for ( i = 0; i < self->count; i ++ )
532 if ( position < self->list[ i ]->frame_count )
534 // Found it, now break
535 producer = self->list[ i ]->producer;
540 // Decrement position by length of this entry
541 position -= self->list[ i ]->frame_count;
545 // Seek in real producer to relative position
546 if ( i < self->count && self->list[ i ]->frame_out != position )
548 // Update the frame_count for the changed clip (hmmm)
549 self->list[ i ]->frame_out = position;
550 self->list[ i ]->frame_count = self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1;
552 // Refresh the playlist
553 mlt_playlist_virtual_refresh( self );
559 /** Obtain the current clips index.
561 * \public \memberof mlt_playlist_s
562 * \param self a playlist
563 * \return the index of the playlist entry at the current position
566 int mlt_playlist_current_clip( mlt_playlist self )
568 // Map playlist position to real producer in virtual playlist
569 mlt_position position = mlt_producer_frame( &self->parent );
571 // Loop through the virtual playlist
574 for ( i = 0; i < self->count; i ++ )
576 if ( position < self->list[ i ]->frame_count )
578 // Found it, now break
583 // Decrement position by length of this entry
584 position -= self->list[ i ]->frame_count;
591 /** Obtain the current clips producer.
593 * \public \memberof mlt_playlist_s
594 * \param self a playlist
595 * \return the producer at the current position
598 mlt_producer mlt_playlist_current( mlt_playlist self )
600 int i = mlt_playlist_current_clip( self );
601 if ( i < self->count )
602 return self->list[ i ]->producer;
607 /** Get the position which corresponds to the start of the next clip.
609 * \public \memberof mlt_playlist_s
610 * \param self a playlist
611 * \param whence the location from which to make the index relative:
612 * start of playlist, end of playlist, or current position
613 * \param index the playlist entry index relative to whence
614 * \return the time at which the referenced clip starts
617 mlt_position mlt_playlist_clip( mlt_playlist self, mlt_whence whence, int index )
619 mlt_position position = 0;
620 int absolute_clip = index;
623 // Determine the absolute clip
626 case mlt_whence_relative_start:
627 absolute_clip = index;
630 case mlt_whence_relative_current:
631 absolute_clip = mlt_playlist_current_clip( self ) + index;
634 case mlt_whence_relative_end:
635 absolute_clip = self->count - index;
639 // Check that we're in a valid range
640 if ( absolute_clip < 0 )
642 else if ( absolute_clip > self->count )
643 absolute_clip = self->count;
645 // Now determine the position
646 for ( i = 0; i < absolute_clip; i ++ )
647 position += self->list[ i ]->frame_count;
652 /** Get all the info about the clip specified.
654 * \public \memberof mlt_playlist_s
655 * \param self a playlist
656 * \param info a clip info struct
657 * \param index a playlist entry index
658 * \return true if there was an error
661 int mlt_playlist_get_clip_info( mlt_playlist self, mlt_playlist_clip_info *info, int index )
663 int error = index < 0 || index >= self->count || self->list[ index ]->producer == NULL;
664 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
667 mlt_producer producer = mlt_producer_cut_parent( self->list[ index ]->producer );
668 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
670 info->producer = producer;
671 info->cut = self->list[ index ]->producer;
672 info->start = mlt_playlist_clip( self, mlt_whence_relative_start, index );
673 info->resource = mlt_properties_get( properties, "resource" );
674 info->frame_in = self->list[ index ]->frame_in;
675 info->frame_out = self->list[ index ]->frame_out;
676 info->frame_count = self->list[ index ]->frame_count;
677 info->repeat = self->list[ index ]->repeat;
678 info->length = mlt_producer_get_length( producer );
679 info->fps = mlt_producer_get_fps( producer );
685 /** Get number of clips in the playlist.
687 * \public \memberof mlt_playlist_s
688 * \param self a playlist
689 * \return the number of playlist entries
692 int mlt_playlist_count( mlt_playlist self )
697 /** Clear the playlist.
699 * \public \memberof mlt_playlist_s
700 * \param self a playlist
701 * \return true if there was an error
704 int mlt_playlist_clear( mlt_playlist self )
707 for ( i = 0; i < self->count; i ++ )
709 mlt_event_close( self->list[ i ]->event );
710 mlt_producer_close( self->list[ i ]->producer );
713 return mlt_playlist_virtual_refresh( self );
716 /** Append a producer to the playlist.
718 * \public \memberof mlt_playlist_s
719 * \param self a playlist
720 * \param producer the producer to append
721 * \return true if there was an error
724 int mlt_playlist_append( mlt_playlist self, mlt_producer producer )
726 // Append to virtual list
727 return mlt_playlist_virtual_append( self, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
730 /** Append a producer to the playlist with in/out points.
732 * \public \memberof mlt_playlist_s
733 * \param self a playlist
734 * \param producer the producer to append
735 * \param in the starting point on the producer; a negative value is the same as 0
736 * \param out the ending point on the producer; a negative value is the same as producer length - 1
737 * \return true if there was an error
740 int mlt_playlist_append_io( mlt_playlist self, mlt_producer producer, mlt_position in, mlt_position out )
742 // Append to virtual list
743 if ( in < 0 && out < 0 )
744 return mlt_playlist_append( self, producer );
746 return mlt_playlist_virtual_append( self, producer, in, out );
749 /** Append a blank to the playlist of a given length.
751 * \public \memberof mlt_playlist_s
752 * \param self a playlist
753 * \param out the ending time of the blank entry, not its duration
754 * \return true if there was an error
757 int mlt_playlist_blank( mlt_playlist self, mlt_position out )
759 // Append to the virtual list
761 return mlt_playlist_virtual_append( self, &self->blank, 0, out );
766 /** Append a blank item to the playlist with duration as a time string.
768 * \public \memberof mlt_playlist_s
769 * \param self a playlist
770 * \param length the duration of the blank entry as a time string
771 * \return true if there was an error
774 int mlt_playlist_blank_time( mlt_playlist self, const char* length )
776 if ( self && length )
778 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
779 mlt_properties_set( properties , "_blank_time", length );
780 mlt_position duration = mlt_properties_get_position( properties, "_blank_time" );
781 return mlt_playlist_blank( self, duration - 1 );
787 /** Insert a producer into the playlist.
789 * \public \memberof mlt_playlist_s
790 * \param self a playlist
791 * \param producer the producer to insert
792 * \param where the producer's playlist entry index
793 * \param in the starting point on the producer
794 * \param out the ending point on the producer
795 * \return true if there was an error
798 int mlt_playlist_insert( mlt_playlist self, mlt_producer producer, int where, mlt_position in, mlt_position out )
801 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
802 mlt_playlist_append_io( self, producer, in, out );
804 // Move to the position specified
805 mlt_playlist_move( self, self->count - 1, where );
806 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
808 return mlt_playlist_virtual_refresh( self );
811 /** Remove an entry in the playlist.
813 * \public \memberof mlt_playlist_s
814 * \param self a playlist
815 * \param where the playlist entry index
816 * \return true if there was an error
819 int mlt_playlist_remove( mlt_playlist self, int where )
821 int error = where < 0 || where >= self->count;
822 if ( error == 0 && mlt_playlist_unmix( self, where ) != 0 )
824 // We need to know the current clip and the position within the playlist
825 int current = mlt_playlist_current_clip( self );
826 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
828 // We need all the details about the clip we're removing
829 mlt_playlist_clip_info where_info;
830 playlist_entry *entry = self->list[ where ];
831 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
837 mlt_playlist_get_clip_info( self, &where_info, where );
839 // Reorganise the list
840 for ( i = where + 1; i < self->count; i ++ )
841 self->list[ i - 1 ] = self->list[ i ];
844 if ( entry->preservation_hack == 0 )
846 // Decouple from mix_in/out if necessary
847 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
849 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
850 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
852 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
854 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
855 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
858 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
859 mlt_producer_clear( entry->producer );
862 // Close the producer associated to the clip info
863 mlt_event_close( entry->event );
864 mlt_producer_close( entry->producer );
867 if ( where == current )
868 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), where_info.start );
869 else if ( where < current && self->count > 0 )
870 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), position - where_info.frame_count );
871 else if ( self->count == 0 )
872 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), 0 );
877 // Refresh the playlist
878 mlt_playlist_virtual_refresh( self );
884 /** Move an entry in the playlist.
886 * \public \memberof mlt_playlist_s
887 * \param self a playlist
888 * \param src an entry index
889 * \param dest an entry index
893 int mlt_playlist_move( mlt_playlist self, int src, int dest )
897 /* We need to ensure that the requested indexes are valid and correct it as necessary */
900 if ( src >= self->count )
901 src = self->count - 1;
905 if ( dest >= self->count )
906 dest = self->count - 1;
908 if ( src != dest && self->count > 1 )
910 int current = mlt_playlist_current_clip( self );
911 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
912 playlist_entry *src_entry = NULL;
914 // We need all the details about the current clip
915 mlt_playlist_clip_info current_info;
917 mlt_playlist_get_clip_info( self, ¤t_info, current );
918 position -= current_info.start;
920 if ( current == src )
922 else if ( current > src && current < dest )
924 else if ( current == dest )
927 src_entry = self->list[ src ];
930 for ( i = src; i > dest; i -- )
931 self->list[ i ] = self->list[ i - 1 ];
935 for ( i = src; i < dest; i ++ )
936 self->list[ i ] = self->list[ i + 1 ];
938 self->list[ dest ] = src_entry;
940 mlt_playlist_get_clip_info( self, ¤t_info, current );
941 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), current_info.start + position );
942 mlt_playlist_virtual_refresh( self );
948 /** Repeat the specified clip n times.
950 * \public \memberof mlt_playlist_s
951 * \param self a playlist
952 * \param clip a playlist entry index
953 * \param repeat the number of times to repeat the clip
954 * \return true if there was an error
957 int mlt_playlist_repeat_clip( mlt_playlist self, int clip, int repeat )
959 int error = repeat < 1 || clip < 0 || clip >= self->count;
962 playlist_entry *entry = self->list[ clip ];
963 entry->repeat = repeat;
964 mlt_playlist_virtual_refresh( self );
969 /** Resize the specified clip.
971 * \public \memberof mlt_playlist_s
972 * \param self a playlist
973 * \param clip the index of the playlist entry
974 * \param in the new starting time on the clip's producer; a negative value is the same as 0
975 * \param out the new ending time on the clip's producer; a negative value is the same as length - 1
976 * \return true if there was an error
979 int mlt_playlist_resize_clip( mlt_playlist self, int clip, mlt_position in, mlt_position out )
981 int error = clip < 0 || clip >= self->count;
982 if ( error == 0 && mlt_playlist_resize_mix( self, clip, in, out ) != 0 )
984 playlist_entry *entry = self->list[ clip ];
985 mlt_producer producer = entry->producer;
986 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
988 mlt_events_block( properties, properties );
990 if ( mlt_producer_is_blank( producer ) )
992 mlt_position length = out - in + 1;
994 // Make sure the parent blank is long enough to accomodate the length specified
995 if ( length > mlt_producer_get_length( &self->blank ) )
997 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
998 mlt_properties_set_int( blank_props, "length", length );
999 mlt_producer_set_in_and_out( &self->blank, 0, out - in );
1001 // Make sure this cut of blank is long enough
1002 if ( length > mlt_producer_get_length( producer ) )
1003 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", length );
1008 if ( out < 0 || out >= mlt_producer_get_length( producer ) )
1009 out = mlt_producer_get_length( producer ) - 1;
1013 mlt_position t = in;
1018 mlt_producer_set_in_and_out( producer, in, out );
1019 mlt_events_unblock( properties, properties );
1020 mlt_playlist_virtual_refresh( self );
1025 /** Split a clip on the playlist at the given position.
1027 * This splits after the specified frame.
1028 * \public \memberof mlt_playlist_s
1029 * \param self a playlist
1030 * \param clip the index of the playlist entry
1031 * \param position the time at which to split relative to the beginning of the clip or its end if negative
1032 * \return true if there was an error
1035 int mlt_playlist_split( mlt_playlist self, int clip, mlt_position position )
1037 int error = clip < 0 || clip >= self->count;
1040 playlist_entry *entry = self->list[ clip ];
1041 position = position < 0 ? entry->frame_count + position - 1 : position;
1042 if ( position >= 0 && position < entry->frame_count - 1 )
1044 int in = entry->frame_in;
1045 int out = entry->frame_out;
1046 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1047 mlt_playlist_resize_clip( self, clip, in, in + position );
1048 if ( !mlt_producer_is_blank( entry->producer ) )
1051 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
1052 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
1053 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
1054 mlt_playlist_insert( self, split, clip + 1, 0, -1 );
1055 mlt_properties_lock( entry_properties );
1056 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
1058 char *name = mlt_properties_get_name( entry_properties, i );
1059 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
1060 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
1062 mlt_properties_unlock( entry_properties );
1063 mlt_producer_close( split );
1067 mlt_playlist_insert( self, &self->blank, clip + 1, 0, out - position - 1 );
1069 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1070 mlt_playlist_virtual_refresh( self );
1080 /** Split the playlist at the absolute position.
1082 * \public \memberof mlt_playlist_s
1083 * \param self a playlist
1084 * \param position the time at which to split relative to the beginning of the clip
1085 * \param left true to split before the frame starting at position
1086 * \return true if there was an error
1089 int mlt_playlist_split_at( mlt_playlist self, mlt_position position, int left )
1091 int result = self == NULL ? -1 : 0;
1094 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) ) )
1096 int clip = mlt_playlist_get_clip_index_at( self, position );
1097 mlt_playlist_clip_info info;
1098 mlt_playlist_get_clip_info( self, &info, clip );
1099 if ( left && position != info.start )
1100 mlt_playlist_split( self, clip, position - info.start - 1 );
1102 mlt_playlist_split( self, clip, position - info.start );
1105 else if ( position <= 0 )
1111 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1117 /** Join 1 or more consecutive clips.
1119 * \public \memberof mlt_playlist_s
1120 * \param self a playlist
1121 * \param clip the starting playlist entry index
1122 * \param count the number of entries to merge
1123 * \param merge ignored
1124 * \return true if there was an error
1127 int mlt_playlist_join( mlt_playlist self, int clip, int count, int merge )
1129 int error = clip < 0 || clip >= self->count;
1133 mlt_playlist new_clip = mlt_playlist_new( mlt_service_profile( MLT_PLAYLIST_SERVICE(self) ) );
1134 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1135 if ( clip + count >= self->count )
1136 count = self->count - clip - 1;
1137 for ( i = 0; i <= count; i ++ )
1139 playlist_entry *entry = self->list[ clip ];
1140 mlt_playlist_append( new_clip, entry->producer );
1141 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
1142 entry->preservation_hack = 1;
1143 mlt_playlist_remove( self, clip );
1145 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1146 mlt_playlist_insert( self, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
1147 mlt_playlist_close( new_clip );
1152 /** Mix consecutive clips for a specified length and apply transition if specified.
1154 * \public \memberof mlt_playlist_s
1155 * \param self a playlist
1156 * \param clip the index of the playlist entry
1157 * \param length the number of frames over which to create the mix
1158 * \param transition the transition to use for the mix
1159 * \return true if there was an error
1162 int mlt_playlist_mix( mlt_playlist self, int clip, int length, mlt_transition transition )
1164 int error = ( clip < 0 || clip + 1 >= self->count );
1167 playlist_entry *clip_a = self->list[ clip ];
1168 playlist_entry *clip_b = self->list[ clip + 1 ];
1169 mlt_producer track_a = NULL;
1170 mlt_producer track_b = NULL;
1171 mlt_tractor tractor = mlt_tractor_new( );
1172 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1174 // Check length is valid for both clips and resize if necessary.
1175 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
1176 length = length > max_size ? max_size : length;
1178 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
1179 if ( length != clip_a->frame_count )
1180 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
1182 track_a = clip_a->producer;
1184 if ( length != clip_b->frame_count )
1185 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
1187 track_b = clip_b->producer;
1189 // Set the tracks on the tractor
1190 mlt_tractor_set_track( tractor, track_a, 0 );
1191 mlt_tractor_set_track( tractor, track_b, 1 );
1193 // Insert the mix object into the playlist
1194 mlt_playlist_insert( self, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
1195 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
1197 // Attach the transition
1198 if ( transition != NULL )
1200 mlt_field field = mlt_tractor_field( tractor );
1201 mlt_field_plant_transition( field, transition, 0, 1 );
1202 mlt_transition_set_in_and_out( transition, 0, length - 1 );
1205 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
1206 if ( track_a != clip_a->producer )
1207 mlt_producer_close( track_a );
1208 if ( track_b != clip_b->producer )
1209 mlt_producer_close( track_b );
1211 // Check if we have anything left on the right hand clip
1212 if ( track_b == clip_b->producer )
1214 clip_b->preservation_hack = 1;
1215 mlt_playlist_remove( self, clip + 2 );
1217 else if ( clip_b->frame_out - clip_b->frame_in > length )
1219 mlt_playlist_resize_clip( self, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
1220 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
1221 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
1225 mlt_producer_clear( clip_b->producer );
1226 mlt_playlist_remove( self, clip + 2 );
1229 // Check if we have anything left on the left hand clip
1230 if ( track_a == clip_a->producer )
1232 clip_a->preservation_hack = 1;
1233 mlt_playlist_remove( self, clip );
1235 else if ( clip_a->frame_out - clip_a->frame_in > length )
1237 mlt_playlist_resize_clip( self, clip, clip_a->frame_in, clip_a->frame_out - length );
1238 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
1239 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
1243 mlt_producer_clear( clip_a->producer );
1244 mlt_playlist_remove( self, clip );
1247 // Unblock and force a fire off of change events to listeners
1248 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1249 mlt_playlist_virtual_refresh( self );
1250 mlt_tractor_close( tractor );
1255 /** Add a transition to an existing mix.
1257 * \public \memberof mlt_playlist_s
1258 * \param self a playlist
1259 * \param clip the index of the playlist entry
1260 * \param transition a transition
1261 * \return true if there was an error
1264 int mlt_playlist_mix_add( mlt_playlist self, int clip, mlt_transition transition )
1266 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1267 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1268 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1269 int error = transition == NULL || tractor == NULL;
1272 mlt_field field = mlt_tractor_field( tractor );
1273 mlt_field_plant_transition( field, transition, 0, 1 );
1274 mlt_transition_set_in_and_out( transition, 0, self->list[ clip ]->frame_count - 1 );
1279 /** Return the clip at the clip index.
1281 * \public \memberof mlt_playlist_s
1282 * \param self a playlist
1283 * \param clip the index of a playlist entry
1284 * \return a producer or NULL if there was an error
1287 mlt_producer mlt_playlist_get_clip( mlt_playlist self, int clip )
1289 if ( clip >= 0 && clip < self->count )
1290 return self->list[ clip ]->producer;
1294 /** Return the clip at the specified position.
1296 * \public \memberof mlt_playlist_s
1297 * \param self a playlist
1298 * \param position a time relative to the beginning of the playlist
1299 * \return a producer or NULL if not found
1302 mlt_producer mlt_playlist_get_clip_at( mlt_playlist self, mlt_position position )
1304 int index = 0, total = 0;
1305 return mlt_playlist_locate( self, &position, &index, &total );
1308 /** Return the clip index of the specified position.
1310 * \public \memberof mlt_playlist_s
1311 * \param self a playlist
1312 * \param position a time relative to the beginning of the playlist
1313 * \return the index of the playlist entry
1316 int mlt_playlist_get_clip_index_at( mlt_playlist self, mlt_position position )
1318 int index = 0, total = 0;
1319 mlt_playlist_locate( self, &position, &index, &total );
1323 /** Determine if the clip is a mix.
1325 * \public \memberof mlt_playlist_s
1326 * \param self a playlist
1327 * \param clip the index of the playlist entry
1328 * \return true if the producer is a mix
1331 int mlt_playlist_clip_is_mix( mlt_playlist self, int clip )
1333 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1334 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1335 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1336 return tractor != NULL;
1339 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1340 * back correctly on to the playlist.
1342 * \private \memberof mlt_playlist_s
1343 * \param self a playlist
1344 * \param clip the index of the playlist entry
1345 * \return true if there was an error
1348 static int mlt_playlist_unmix( mlt_playlist self, int clip )
1350 int error = ( clip < 0 || clip >= self->count );
1352 // Ensure that the clip request is actually a mix
1355 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1356 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1357 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1358 self->list[ clip ]->preservation_hack;
1363 playlist_entry *mix = self->list[ clip ];
1364 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1365 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1366 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1367 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1368 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1369 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1371 if ( clip_a != NULL )
1373 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1377 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1378 mlt_playlist_insert( self, cut, clip, -1, -1 );
1382 if ( clip_b != NULL )
1384 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1388 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1389 mlt_playlist_insert( self, cut, clip + 1, -1, -1 );
1392 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1393 mlt_playlist_remove( self, clip );
1394 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1395 mlt_playlist_virtual_refresh( self );
1400 /** Resize a mix clip.
1402 * \private \memberof mlt_playlist_s
1403 * \param self a playlist
1404 * \param clip the index of the playlist entry
1405 * \param in the new starting point
1406 * \param out the new ending point
1407 * \return true if there was an error
1410 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out )
1412 int error = ( clip < 0 || clip >= self->count );
1414 // Ensure that the clip request is actually a mix
1417 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1418 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1419 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1424 playlist_entry *mix = self->list[ clip ];
1425 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1426 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1427 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1428 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1429 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1430 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1431 int length = out - in + 1;
1432 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1433 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1435 if ( clip_a != NULL )
1436 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1438 if ( clip_b != NULL )
1439 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1441 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1442 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1443 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1444 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1445 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1446 mlt_producer_set_in_and_out( mix->producer, in, out );
1448 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1449 mlt_playlist_virtual_refresh( self );
1454 /** Consolidate adjacent blank producers.
1456 * \public \memberof mlt_playlist_s
1457 * \param self a playlist
1458 * \param keep_length set false to remove the last entry if it is blank
1461 void mlt_playlist_consolidate_blanks( mlt_playlist self, int keep_length )
1466 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1468 mlt_events_block( properties, properties );
1469 for ( i = 1; i < self->count; i ++ )
1471 playlist_entry *left = self->list[ i - 1 ];
1472 playlist_entry *right = self->list[ i ];
1474 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1476 mlt_playlist_resize_clip( self, i - 1, 0, left->frame_count + right->frame_count - 1 );
1477 mlt_playlist_remove( self, i -- );
1481 if ( !keep_length && self->count > 0 )
1483 playlist_entry *last = self->list[ self->count - 1 ];
1484 if ( mlt_producer_is_blank( last->producer ) )
1485 mlt_playlist_remove( self, self->count - 1 );
1488 mlt_events_unblock( properties, properties );
1489 mlt_playlist_virtual_refresh( self );
1493 /** Determine if the specified clip index is a blank.
1495 * \public \memberof mlt_playlist_s
1496 * \param self a playlist
1497 * \param clip the index of the playlist entry
1498 * \return true if there was an error
1501 int mlt_playlist_is_blank( mlt_playlist self, int clip )
1503 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( self, clip ) );
1506 /** Determine if the specified position is a blank.
1508 * \public \memberof mlt_playlist_s
1509 * \param self a playlist
1510 * \param position a time relative to the start or end (negative) of the playlist
1511 * \return true if there was an error
1514 int mlt_playlist_is_blank_at( mlt_playlist self, mlt_position position )
1516 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( self, position ) );
1519 /** Replace the specified clip with a blank and return the clip.
1521 * \public \memberof mlt_playlist_s
1522 * \param self a playlist
1523 * \param clip the index of the playlist entry
1524 * \return a producer or NULL if there was an error
1527 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist self, int clip )
1529 mlt_producer producer = NULL;
1530 if ( !mlt_playlist_is_blank( self, clip ) )
1532 playlist_entry *entry = self->list[ clip ];
1533 int in = entry->frame_in;
1534 int out = entry->frame_out;
1535 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1536 producer = entry->producer;
1537 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1538 mlt_events_block( properties, properties );
1539 mlt_playlist_remove( self, clip );
1540 mlt_playlist_blank( self, out - in );
1541 mlt_playlist_move( self, self->count - 1, clip );
1542 mlt_events_unblock( properties, properties );
1543 mlt_playlist_virtual_refresh( self );
1544 mlt_producer_set_in_and_out( producer, in, out );
1549 /** Insert blank space.
1551 * \public \memberof mlt_playlist_s
1552 * \param self a playlist
1553 * \param clip the index of the new blank section
1554 * \param length the ending time of the new blank section (duration - 1)
1557 void mlt_playlist_insert_blank( mlt_playlist self, int clip, int length )
1559 if ( self != NULL && length >= 0 )
1561 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1562 mlt_events_block( properties, properties );
1563 mlt_playlist_blank( self, length );
1564 mlt_playlist_move( self, self->count - 1, clip );
1565 mlt_events_unblock( properties, properties );
1566 mlt_playlist_virtual_refresh( self );
1570 /** Resize a blank entry.
1572 * \public \memberof mlt_playlist_s
1573 * \param self a playlist
1574 * \param position the time at which the blank entry exists relative to the start or end (negative) of the playlist.
1575 * \param length the additional amount of blank frames to add
1576 * \param find true to fist locate the blank after the clip at position
1578 void mlt_playlist_pad_blanks( mlt_playlist self, mlt_position position, int length, int find )
1580 if ( self != NULL && length != 0 )
1582 int clip = mlt_playlist_get_clip_index_at( self, position );
1583 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1584 mlt_events_block( properties, properties );
1585 if ( find && clip < self->count && !mlt_playlist_is_blank( self, clip ) )
1587 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1589 mlt_playlist_clip_info info;
1590 mlt_playlist_get_clip_info( self, &info, clip );
1591 if ( info.frame_out + length > info.frame_in )
1592 mlt_playlist_resize_clip( self, clip, info.frame_in, info.frame_out + length );
1594 mlt_playlist_remove( self, clip );
1596 else if ( find && clip < self->count && length > 0 )
1598 mlt_playlist_insert_blank( self, clip, length );
1600 mlt_events_unblock( properties, properties );
1601 mlt_playlist_virtual_refresh( self );
1605 /** Insert a clip at a specific time.
1607 * \public \memberof mlt_playlist_s
1608 * \param self a playlist
1609 * \param position the time at which to insert
1610 * \param producer the producer to insert
1611 * \param mode true if you want to overwrite any blank section
1612 * \return true if there was an error
1615 int mlt_playlist_insert_at( mlt_playlist self, mlt_position position, mlt_producer producer, int mode )
1617 int ret = self == NULL || position < 0 || producer == NULL;
1620 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1621 int length = mlt_producer_get_playtime( producer );
1622 int clip = mlt_playlist_get_clip_index_at( self, position );
1623 mlt_playlist_clip_info info;
1624 mlt_playlist_get_clip_info( self, &info, clip );
1625 mlt_events_block( properties, self );
1626 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1628 // Split and move to new clip if need be
1629 if ( position != info.start && mlt_playlist_split( self, clip, position - info.start - 1 ) == 0 )
1630 mlt_playlist_get_clip_info( self, &info, ++ clip );
1632 // Split again if need be
1633 if ( length < info.frame_count )
1634 mlt_playlist_split( self, clip, length - 1 );
1637 mlt_playlist_remove( self, clip );
1640 mlt_playlist_insert( self, producer, clip, -1, -1 );
1643 else if ( clip < self->count )
1645 if ( position > info.start + info.frame_count / 2 )
1647 if ( mode == 1 && clip < self->count && mlt_playlist_is_blank( self, clip ) )
1649 mlt_playlist_get_clip_info( self, &info, clip );
1650 if ( length < info.frame_count )
1651 mlt_playlist_split( self, clip, length );
1652 mlt_playlist_remove( self, clip );
1654 mlt_playlist_insert( self, producer, clip, -1, -1 );
1660 if ( position == info.start )
1661 mlt_playlist_remove( self, clip );
1663 mlt_playlist_blank( self, position - mlt_properties_get_int( properties, "length" ) - 1 );
1665 mlt_playlist_append( self, producer );
1666 ret = self->count - 1;
1668 mlt_events_unblock( properties, self );
1669 mlt_playlist_virtual_refresh( self );
1678 /** Get the time at which the clip starts relative to the playlist.
1680 * \public \memberof mlt_playlist_s
1681 * \param self a playlist
1682 * \param clip the index of the playlist entry
1683 * \return the starting time
1686 int mlt_playlist_clip_start( mlt_playlist self, int clip )
1688 mlt_playlist_clip_info info;
1689 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1691 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1694 /** Get the playable duration of the clip.
1696 * \public \memberof mlt_playlist_s
1697 * \param self a playlist
1698 * \param clip the index of the playlist entry
1699 * \return the duration of the playlist entry
1702 int mlt_playlist_clip_length( mlt_playlist self, int clip )
1704 mlt_playlist_clip_info info;
1705 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1706 return info.frame_count;
1710 /** Get the duration of a blank space.
1712 * \public \memberof mlt_playlist_s
1713 * \param self a playlist
1714 * \param clip the index of the playlist entry
1715 * \param bounded the maximum number of blank entries or 0 for all
1716 * \return the duration of a blank section
1719 int mlt_playlist_blanks_from( mlt_playlist self, int clip, int bounded )
1722 mlt_playlist_clip_info info;
1723 if ( self != NULL && clip < self->count )
1725 mlt_playlist_get_clip_info( self, &info, clip );
1726 if ( mlt_playlist_is_blank( self, clip ) )
1727 count += info.frame_count;
1729 bounded = self->count;
1730 for ( clip ++; clip < self->count && bounded >= 0; clip ++ )
1732 mlt_playlist_get_clip_info( self, &info, clip );
1733 if ( mlt_playlist_is_blank( self, clip ) )
1734 count += info.frame_count;
1742 /** Remove a portion of the playlist by time.
1744 * \public \memberof mlt_playlist_s
1745 * \param self a playlist
1746 * \param position the starting time
1747 * \param length the duration of time to remove
1748 * \return the new entry index at the position
1751 int mlt_playlist_remove_region( mlt_playlist self, mlt_position position, int length )
1753 int index = mlt_playlist_get_clip_index_at( self, position );
1754 if ( index >= 0 && index < self->count )
1756 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1757 int clip_start = mlt_playlist_clip_start( self, index );
1758 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1759 mlt_events_block( properties, self );
1761 if ( position + length > list_length )
1762 length -= ( position + length - list_length );
1764 if ( clip_start < position )
1766 mlt_playlist_split( self, index ++, position - clip_start - 1 );
1771 if ( mlt_playlist_clip_length( self, index ) > length )
1772 mlt_playlist_split( self, index, length - 1 );
1773 length -= mlt_playlist_clip_length( self, index );
1774 mlt_playlist_remove( self, index );
1777 mlt_playlist_consolidate_blanks( self, 0 );
1778 mlt_events_unblock( properties, self );
1779 mlt_playlist_virtual_refresh( self );
1781 // Just to be sure, we'll get the clip index again...
1782 index = mlt_playlist_get_clip_index_at( self, position );
1789 * \deprecated not implemented
1790 * \public \memberof mlt_playlist_s
1794 * \param new_position
1798 int mlt_playlist_move_region( mlt_playlist self, mlt_position position, int length, int new_position )
1806 /** Get the current frame.
1808 * The implementation of the get_frame virtual function.
1809 * \private \memberof mlt_playlist_s
1810 * \param producer a producer
1811 * \param frame a frame by reference
1812 * \param index the time at which to get the frame
1816 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1818 // Check that we have a producer
1819 if ( producer == NULL )
1825 // Get this mlt_playlist
1826 mlt_playlist self = producer->child;
1828 // Need to ensure the frame is deinterlaced when repeating 1 frame
1829 int progressive = 0;
1831 // Get the real producer
1832 mlt_service real = mlt_playlist_virtual_seek( self, &progressive );
1834 // Check that we have a producer
1837 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1842 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1844 mlt_service_get_frame( real, frame, index );
1848 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1849 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1850 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1851 mlt_frame_push_service( *frame, NULL );
1852 mlt_frame_push_audio( *frame, NULL );
1853 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1854 mlt_service_apply_filters( real, *frame, 0 );
1855 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1856 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1859 // Check if we're at the end of the clip
1860 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1861 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1862 mlt_playlist_virtual_set_out( self );
1864 // Set the consumer progressive property
1867 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1868 mlt_properties_set_int( properties, "test_audio", 1 );
1871 // Check for notifier and call with appropriate argument
1872 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1873 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1874 if ( notifier != NULL )
1876 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1877 notifier( argument );
1880 // Update position on the frame we're creating
1881 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1883 // Position ourselves on the next frame
1884 mlt_producer_prepare_next( producer );
1889 /** Close the playlist.
1891 * \public \memberof mlt_playlist_s
1892 * \param self a playlist
1895 void mlt_playlist_close( mlt_playlist self )
1897 if ( self != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( self ) ) <= 0 )
1900 self->parent.close = NULL;
1901 for ( i = 0; i < self->count; i ++ )
1903 mlt_event_close( self->list[ i ]->event );
1904 mlt_producer_close( self->list[ i ]->producer );
1905 free( self->list[ i ] );
1907 mlt_producer_close( &self->blank );
1908 mlt_producer_close( &self->parent );