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 );
57 /** Construct a playlist.
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
65 mlt_playlist mlt_playlist_init( )
67 mlt_playlist self = calloc( sizeof( struct mlt_playlist_s ), 1 );
70 mlt_producer producer = &self->parent;
72 // Construct the producer
73 mlt_producer_init( producer, self );
75 // Override the producer get_frame
76 producer->get_frame = producer_get_frame;
78 // Define the destructor
79 producer->close = ( mlt_destructor )mlt_playlist_close;
80 producer->close_object = self;
83 mlt_producer_init( &self->blank, NULL );
84 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &self->blank ), "mlt_service", "blank" );
85 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &self->blank ), "resource", "blank" );
87 // Indicate that this producer is a playlist
88 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "playlist", self, 0, NULL, NULL );
90 // Specify the eof condition
91 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "eof", "pause" );
92 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "resource", "<playlist>" );
93 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "mlt_type", "mlt_producer" );
94 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "in", 0 );
95 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "out", -1 );
96 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "length", 0 );
99 self->list = malloc( self->size * sizeof( playlist_entry * ) );
105 /** Get the producer associated to this playlist.
107 * \public \memberof mlt_playlist_s
108 * \param self a playlist
109 * \return the producer interface
110 * \see MLT_PLAYLIST_PRODUCER
113 mlt_producer mlt_playlist_producer( mlt_playlist self )
115 return self != NULL ? &self->parent : NULL;
118 /** Get the service associated to this playlist.
120 * \public \memberof mlt_playlist_s
121 * \param self a playlist
122 * \return the service interface
123 * \see MLT_PLAYLIST_SERVICE
126 mlt_service mlt_playlist_service( mlt_playlist self )
128 return MLT_PRODUCER_SERVICE( &self->parent );
131 /** Get the properties associated to this playlist.
133 * \public \memberof mlt_playlist_s
134 * \param self a playlist
135 * \return the playlist's properties list
136 * \see MLT_PLAYLIST_PROPERTIES
139 mlt_properties mlt_playlist_properties( mlt_playlist self )
141 return MLT_PRODUCER_PROPERTIES( &self->parent );
144 /** Refresh the playlist after a clip has been changed.
146 * \private \memberof mlt_playlist_s
147 * \param self a playlist
151 static int mlt_playlist_virtual_refresh( mlt_playlist self )
153 // Obtain the properties
154 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
156 mlt_position frame_count = 0;
158 for ( i = 0; i < self->count; i ++ )
161 mlt_producer producer = self->list[ i ]->producer;
164 int current_length = mlt_producer_get_playtime( producer );
166 // Check if the length of the producer has changed
167 if ( self->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
168 self->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
170 // This clip should be removed...
171 if ( current_length < 1 )
173 self->list[ i ]->frame_in = 0;
174 self->list[ i ]->frame_out = -1;
175 self->list[ i ]->frame_count = 0;
179 self->list[ i ]->frame_in = mlt_producer_get_in( producer );
180 self->list[ i ]->frame_out = mlt_producer_get_out( producer );
181 self->list[ i ]->frame_count = current_length;
184 // Update the producer_length
185 self->list[ i ]->producer_length = current_length;
189 // Calculate the frame_count
190 self->list[ i ]->frame_count = ( self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1 ) * self->list[ i ]->repeat;
192 // Update the frame_count for self clip
193 frame_count += self->list[ i ]->frame_count;
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 );
205 /** Listener for producers on the playlist.
207 * Refreshes the playlist whenever an entry receives producer-changed.
208 * \private \memberof mlt_playlist_s
209 * \param producer a producer
210 * \param self a playlist
213 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist self )
215 mlt_playlist_virtual_refresh( self );
218 /** Append to the virtual playlist.
220 * \private \memberof mlt_playlist_s
221 * \param self 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
228 static int mlt_playlist_virtual_append( mlt_playlist self, mlt_producer source, mlt_position in, mlt_position out )
230 mlt_producer producer = NULL;
231 mlt_properties properties = NULL;
232 mlt_properties parent = NULL;
234 // If we have a cut, then use the in/out points from the cut
235 if ( mlt_producer_is_blank( source ) )
237 mlt_position length = out - in + 1;
239 // Make sure the blank is long enough to accomodate the length specified
240 if ( length > mlt_producer_get_length( &self->blank ) )
242 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
243 mlt_events_block( blank_props, blank_props );
244 mlt_producer_set_in_and_out( &self->blank, in, out );
245 mlt_events_unblock( blank_props, blank_props );
248 // Now make sure the cut comes from this self->blank
249 if ( source == NULL )
251 producer = mlt_producer_cut( &self->blank, in, out );
253 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &self->blank )
255 producer = mlt_producer_cut( &self->blank, in, out );
260 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
263 properties = MLT_PRODUCER_PROPERTIES( producer );
265 // Make sure this cut of blank is long enough
266 if ( length > mlt_producer_get_length( producer ) )
267 mlt_properties_set_int( properties, "length", length );
269 else if ( mlt_producer_is_cut( source ) )
273 in = mlt_producer_get_in( producer );
274 if ( out < 0 || out > mlt_producer_get_out( producer ) )
275 out = mlt_producer_get_out( producer );
276 properties = MLT_PRODUCER_PROPERTIES( producer );
277 mlt_properties_inc_ref( properties );
281 producer = mlt_producer_cut( source, in, out );
282 if ( in < 0 || in < mlt_producer_get_in( producer ) )
283 in = mlt_producer_get_in( producer );
284 if ( out < 0 || out > mlt_producer_get_out( producer ) )
285 out = mlt_producer_get_out( producer );
286 properties = MLT_PRODUCER_PROPERTIES( producer );
289 // Fetch the cuts parent properties
290 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
292 // Remove loader normalisers for fx cuts
293 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
295 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
296 mlt_filter filter = mlt_service_filter( service, 0 );
297 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_loader" ) )
299 mlt_service_detach( service, filter );
300 filter = mlt_service_filter( service, 0 );
302 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
305 // Check that we have room
306 if ( self->count >= self->size )
309 self->list = realloc( self->list, ( self->size + 10 ) * sizeof( playlist_entry * ) );
310 for ( i = self->size; i < self->size + 10; i ++ ) self->list[ i ] = NULL;
315 self->list[ self->count ] = calloc( sizeof( playlist_entry ), 1 );
316 if ( self->list[ self->count ] != NULL )
318 self->list[ self->count ]->producer = producer;
319 self->list[ self->count ]->frame_in = in;
320 self->list[ self->count ]->frame_out = out;
321 self->list[ self->count ]->frame_count = out - in + 1;
322 self->list[ self->count ]->repeat = 1;
323 self->list[ self->count ]->producer_length = mlt_producer_get_playtime( producer );
324 self->list[ self->count ]->event = mlt_events_listen( parent, self, "producer-changed", ( mlt_listener )mlt_playlist_listener );
325 mlt_event_inc_ref( self->list[ self->count ]->event );
326 mlt_properties_set( properties, "eof", "pause" );
327 mlt_producer_set_speed( producer, 0 );
331 return mlt_playlist_virtual_refresh( self );
334 /** Locate a producer by index.
336 * \private \memberof mlt_playlist_s
337 * \param self a playlist
338 * \param[in, out] position the time at which to locate the producer, returns the time relative to the producer's starting point
339 * \param[out] clip the index of the playlist entry
340 * \param[out] total the duration of the playlist up to and including this producer
341 * \return a producer or NULL if not found
344 static mlt_producer mlt_playlist_locate( mlt_playlist self, mlt_position *position, int *clip, int *total )
346 // Default producer to NULL
347 mlt_producer producer = NULL;
349 // Loop for each producer until found
350 for ( *clip = 0; *clip < self->count; *clip += 1 )
352 // Increment the total
353 *total += self->list[ *clip ]->frame_count;
355 // Check if the position indicates that we have found the clip
356 // Note that 0 length clips get skipped automatically
357 if ( *position < self->list[ *clip ]->frame_count )
359 // Found it, now break
360 producer = self->list[ *clip ]->producer;
365 // Decrement position by length of self entry
366 *position -= self->list[ *clip ]->frame_count;
373 /** Seek in the virtual playlist.
375 * This gets the producer at the current position and seeks on the producer
376 * while doing repeat and end-of-file handling. This is also responsible for
377 * closing producers previous to the preceding playlist if the autoclose
379 * \private \memberof mlt_playlist_s
380 * \param self a playlist
381 * \param[out] progressive true if the producer should be displayed progressively
382 * \return the service interface of the producer at the play head
383 * \see producer_get_frame
386 static mlt_service mlt_playlist_virtual_seek( mlt_playlist self, int *progressive )
388 // Map playlist position to real producer in virtual playlist
389 mlt_position position = mlt_producer_frame( &self->parent );
391 // Keep the original position since we change it while iterating through the list
392 mlt_position original = position;
394 // Clip index and total
398 // Locate the producer for the position
399 mlt_producer producer = mlt_playlist_locate( self, &position, &i, &total );
401 // Get the properties
402 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
404 // Automatically close previous producers if requested
405 if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
406 && position < 2 // tolerate off-by-one error on going to next clip
407 && mlt_properties_get_int( properties, "autoclose" ) )
410 // They might have jumped ahead!
411 for ( j = 0; j < i - 1; j++ )
413 mlt_service_lock( MLT_PRODUCER_SERVICE( self->list[ j ]->producer ) );
414 mlt_producer p = self->list[ j ]->producer;
417 self->list[ j ]->producer = NULL;
418 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
419 mlt_producer_close( p );
421 // If p is null, the lock will not have been "taken"
425 // Get the eof handling
426 char *eof = mlt_properties_get( properties, "eof" );
428 // Seek in real producer to relative position
429 if ( producer != NULL )
431 int count = self->list[ i ]->frame_count / self->list[ i ]->repeat;
432 *progressive = count == 1;
433 mlt_producer_seek( producer, (int)position % count );
435 else if ( !strcmp( eof, "pause" ) && total > 0 )
437 playlist_entry *entry = self->list[ self->count - 1 ];
438 int count = entry->frame_count / entry->repeat;
439 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
440 mlt_producer_seek( self_producer, original - 1 );
441 producer = entry->producer;
442 mlt_producer_seek( producer, (int)entry->frame_out % count );
443 mlt_producer_set_speed( self_producer, 0 );
444 mlt_producer_set_speed( producer, 0 );
445 *progressive = count == 1;
447 else if ( !strcmp( eof, "loop" ) && total > 0 )
449 playlist_entry *entry = self->list[ 0 ];
450 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
451 mlt_producer_seek( self_producer, 0 );
452 producer = entry->producer;
453 mlt_producer_seek( producer, 0 );
457 producer = &self->blank;
460 return MLT_PRODUCER_SERVICE( producer );
463 /** Invoked when a producer indicates that it has prematurely reached its end.
465 * \private \memberof mlt_playlist_s
466 * \param self a playlist
468 * \see producer_get_frame
471 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist self )
473 // Default producer to blank
474 mlt_producer producer = &self->blank;
476 // Map playlist position to real producer in virtual playlist
477 mlt_position position = mlt_producer_frame( &self->parent );
479 // Loop through the virtual playlist
482 for ( i = 0; i < self->count; i ++ )
484 if ( position < self->list[ i ]->frame_count )
486 // Found it, now break
487 producer = self->list[ i ]->producer;
492 // Decrement position by length of this entry
493 position -= self->list[ i ]->frame_count;
497 // Seek in real producer to relative position
498 if ( i < self->count && self->list[ i ]->frame_out != position )
500 // Update the frame_count for the changed clip (hmmm)
501 self->list[ i ]->frame_out = position;
502 self->list[ i ]->frame_count = self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1;
504 // Refresh the playlist
505 mlt_playlist_virtual_refresh( self );
511 /** Obtain the current clips index.
513 * \public \memberof mlt_playlist_s
514 * \param self a playlist
515 * \return the index of the playlist entry at the current position
518 int mlt_playlist_current_clip( mlt_playlist self )
520 // Map playlist position to real producer in virtual playlist
521 mlt_position position = mlt_producer_frame( &self->parent );
523 // Loop through the virtual playlist
526 for ( i = 0; i < self->count; i ++ )
528 if ( position < self->list[ i ]->frame_count )
530 // Found it, now break
535 // Decrement position by length of this entry
536 position -= self->list[ i ]->frame_count;
543 /** Obtain the current clips producer.
545 * \public \memberof mlt_playlist_s
546 * \param self a playlist
547 * \return the producer at the current position
550 mlt_producer mlt_playlist_current( mlt_playlist self )
552 int i = mlt_playlist_current_clip( self );
553 if ( i < self->count )
554 return self->list[ i ]->producer;
559 /** Get the position which corresponds to the start of the next clip.
561 * \public \memberof mlt_playlist_s
562 * \param self a playlist
563 * \param whence the location from which to make the index relative:
564 * start of playlist, end of playlist, or current position
565 * \param index the playlist entry index relative to whence
566 * \return the time at which the referenced clip starts
569 mlt_position mlt_playlist_clip( mlt_playlist self, mlt_whence whence, int index )
571 mlt_position position = 0;
572 int absolute_clip = index;
575 // Determine the absolute clip
578 case mlt_whence_relative_start:
579 absolute_clip = index;
582 case mlt_whence_relative_current:
583 absolute_clip = mlt_playlist_current_clip( self ) + index;
586 case mlt_whence_relative_end:
587 absolute_clip = self->count - index;
591 // Check that we're in a valid range
592 if ( absolute_clip < 0 )
594 else if ( absolute_clip > self->count )
595 absolute_clip = self->count;
597 // Now determine the position
598 for ( i = 0; i < absolute_clip; i ++ )
599 position += self->list[ i ]->frame_count;
604 /** Get all the info about the clip specified.
606 * \public \memberof mlt_playlist_s
607 * \param self a playlist
608 * \param info a clip info struct
609 * \param index a playlist entry index
610 * \return true if there was an error
613 int mlt_playlist_get_clip_info( mlt_playlist self, mlt_playlist_clip_info *info, int index )
615 int error = index < 0 || index >= self->count || self->list[ index ]->producer == NULL;
616 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
619 mlt_producer producer = mlt_producer_cut_parent( self->list[ index ]->producer );
620 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
622 info->producer = producer;
623 info->cut = self->list[ index ]->producer;
624 info->start = mlt_playlist_clip( self, mlt_whence_relative_start, index );
625 info->resource = mlt_properties_get( properties, "resource" );
626 info->frame_in = self->list[ index ]->frame_in;
627 info->frame_out = self->list[ index ]->frame_out;
628 info->frame_count = self->list[ index ]->frame_count;
629 info->repeat = self->list[ index ]->repeat;
630 info->length = mlt_producer_get_length( producer );
631 info->fps = mlt_producer_get_fps( producer );
637 /** Get number of clips in the playlist.
639 * \public \memberof mlt_playlist_s
640 * \param self a playlist
641 * \return the number of playlist entries
644 int mlt_playlist_count( mlt_playlist self )
649 /** Clear the playlist.
651 * \public \memberof mlt_playlist_s
652 * \param self a playlist
653 * \return true if there was an error
656 int mlt_playlist_clear( mlt_playlist self )
659 for ( i = 0; i < self->count; i ++ )
661 mlt_event_close( self->list[ i ]->event );
662 mlt_producer_close( self->list[ i ]->producer );
665 return mlt_playlist_virtual_refresh( self );
668 /** Append a producer to the playlist.
670 * \public \memberof mlt_playlist_s
671 * \param self a playlist
672 * \param producer the producer to append
673 * \return true if there was an error
676 int mlt_playlist_append( mlt_playlist self, mlt_producer producer )
678 // Append to virtual list
679 return mlt_playlist_virtual_append( self, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
682 /** Append a producer to the playlist with in/out points.
684 * \public \memberof mlt_playlist_s
685 * \param self a playlist
686 * \param producer the producer to append
687 * \param in the starting point on the producer; a negative value is the same as 0
688 * \param out the ending point on the producer; a negative value is the same as producer length - 1
689 * \return true if there was an error
692 int mlt_playlist_append_io( mlt_playlist self, mlt_producer producer, mlt_position in, mlt_position out )
694 // Append to virtual list
695 if ( in < 0 && out < 0 )
696 return mlt_playlist_append( self, producer );
698 return mlt_playlist_virtual_append( self, producer, in, out );
701 /** Append a blank to the playlist of a given length.
703 * \public \memberof mlt_playlist_s
704 * \param self a playlist
705 * \param length the ending time of the blank entry, not its duration
706 * \return true if there was an error
709 int mlt_playlist_blank( mlt_playlist self, mlt_position length )
711 // Append to the virtual list
713 return mlt_playlist_virtual_append( self, &self->blank, 0, length );
718 /** Insert a producer into the playlist.
720 * \public \memberof mlt_playlist_s
721 * \param self a playlist
722 * \param producer the producer to insert
723 * \param where the producer's playlist entry index
724 * \param in the starting point on the producer
725 * \param out the ending point on the producer
726 * \return true if there was an error
729 int mlt_playlist_insert( mlt_playlist self, mlt_producer producer, int where, mlt_position in, mlt_position out )
732 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
733 mlt_playlist_append_io( self, producer, in, out );
735 // Move to the position specified
736 mlt_playlist_move( self, self->count - 1, where );
737 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
739 return mlt_playlist_virtual_refresh( self );
742 /** Remove an entry in the playlist.
744 * \public \memberof mlt_playlist_s
745 * \param self a playlist
746 * \param where the playlist entry index
747 * \return true if there was an error
750 int mlt_playlist_remove( mlt_playlist self, int where )
752 int error = where < 0 || where >= self->count;
753 if ( error == 0 && mlt_playlist_unmix( self, where ) != 0 )
755 // We need to know the current clip and the position within the playlist
756 int current = mlt_playlist_current_clip( self );
757 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
759 // We need all the details about the clip we're removing
760 mlt_playlist_clip_info where_info;
761 playlist_entry *entry = self->list[ where ];
762 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
768 mlt_playlist_get_clip_info( self, &where_info, where );
770 // Make sure the clip to be removed is valid and correct if necessary
773 if ( where >= self->count )
774 where = self->count - 1;
776 // Reorganise the list
777 for ( i = where + 1; i < self->count; i ++ )
778 self->list[ i - 1 ] = self->list[ i ];
781 if ( entry->preservation_hack == 0 )
783 // Decouple from mix_in/out if necessary
784 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
786 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
787 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
789 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
791 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
792 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
795 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
796 mlt_producer_clear( entry->producer );
799 // Close the producer associated to the clip info
800 mlt_event_close( entry->event );
801 mlt_producer_close( entry->producer );
804 if ( where == current )
805 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), where_info.start );
806 else if ( where < current && self->count > 0 )
807 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), position - where_info.frame_count );
808 else if ( self->count == 0 )
809 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), 0 );
814 // Refresh the playlist
815 mlt_playlist_virtual_refresh( self );
821 /** Move an entry in the playlist.
823 * \public \memberof mlt_playlist_s
824 * \param self a playlist
825 * \param src an entry index
826 * \param dest an entry index
830 int mlt_playlist_move( mlt_playlist self, int src, int dest )
834 /* We need to ensure that the requested indexes are valid and correct it as necessary */
837 if ( src >= self->count )
838 src = self->count - 1;
842 if ( dest >= self->count )
843 dest = self->count - 1;
845 if ( src != dest && self->count > 1 )
847 int current = mlt_playlist_current_clip( self );
848 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
849 playlist_entry *src_entry = NULL;
851 // We need all the details about the current clip
852 mlt_playlist_clip_info current_info;
854 mlt_playlist_get_clip_info( self, ¤t_info, current );
855 position -= current_info.start;
857 if ( current == src )
859 else if ( current > src && current < dest )
861 else if ( current == dest )
864 src_entry = self->list[ src ];
867 for ( i = src; i > dest; i -- )
868 self->list[ i ] = self->list[ i - 1 ];
872 for ( i = src; i < dest; i ++ )
873 self->list[ i ] = self->list[ i + 1 ];
875 self->list[ dest ] = src_entry;
877 mlt_playlist_get_clip_info( self, ¤t_info, current );
878 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), current_info.start + position );
879 mlt_playlist_virtual_refresh( self );
885 /** Repeat the specified clip n times.
887 * \public \memberof mlt_playlist_s
888 * \param self a playlist
889 * \param clip a playlist entry index
890 * \param repeat the number of times to repeat the clip
891 * \return true if there was an error
894 int mlt_playlist_repeat_clip( mlt_playlist self, int clip, int repeat )
896 int error = repeat < 1 || clip < 0 || clip >= self->count;
899 playlist_entry *entry = self->list[ clip ];
900 entry->repeat = repeat;
901 mlt_playlist_virtual_refresh( self );
906 /** Resize the specified clip.
908 * \public \memberof mlt_playlist_s
909 * \param self a playlist
910 * \param clip the index of the playlist entry
911 * \param in the new starting time on the clip's producer; a negative value is the same as 0
912 * \param out the new ending time on the clip's producer; a negative value is the same as length - 1
913 * \return true if there was an error
916 int mlt_playlist_resize_clip( mlt_playlist self, int clip, mlt_position in, mlt_position out )
918 int error = clip < 0 || clip >= self->count;
919 if ( error == 0 && mlt_playlist_resize_mix( self, clip, in, out ) != 0 )
921 playlist_entry *entry = self->list[ clip ];
922 mlt_producer producer = entry->producer;
923 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
925 mlt_events_block( properties, properties );
927 if ( mlt_producer_is_blank( producer ) )
929 mlt_position length = out - in + 1;
931 // Make sure the parent blank is long enough to accomodate the length specified
932 if ( length > mlt_producer_get_length( &self->blank ) )
934 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
935 mlt_properties_set_int( blank_props, "length", length );
936 mlt_producer_set_in_and_out( &self->blank, 0, out - in );
938 // Make sure this cut of blank is long enough
939 if ( length > mlt_producer_get_length( producer ) )
940 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", length );
945 if ( out < 0 || out >= mlt_producer_get_length( producer ) )
946 out = mlt_producer_get_length( producer ) - 1;
955 mlt_producer_set_in_and_out( producer, in, out );
956 mlt_events_unblock( properties, properties );
957 mlt_playlist_virtual_refresh( self );
962 /** Split a clip on the playlist at the given position.
964 * This splits after the specified frame.
965 * \public \memberof mlt_playlist_s
966 * \param self a playlist
967 * \param clip the index of the playlist entry
968 * \param position the time at which to split relative to the beginning of the clip or its end if negative
969 * \return true if there was an error
972 int mlt_playlist_split( mlt_playlist self, int clip, mlt_position position )
974 int error = clip < 0 || clip >= self->count;
977 playlist_entry *entry = self->list[ clip ];
978 position = position < 0 ? entry->frame_count + position - 1 : position;
979 if ( position >= 0 && position < entry->frame_count - 1 )
981 int in = entry->frame_in;
982 int out = entry->frame_out;
983 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
984 mlt_playlist_resize_clip( self, clip, in, in + position );
985 if ( !mlt_producer_is_blank( entry->producer ) )
988 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
989 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
990 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
991 mlt_playlist_insert( self, split, clip + 1, 0, -1 );
992 mlt_properties_lock( entry_properties );
993 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
995 char *name = mlt_properties_get_name( entry_properties, i );
996 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
997 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
999 mlt_properties_unlock( entry_properties );
1000 mlt_producer_close( split );
1004 mlt_playlist_insert( self, &self->blank, clip + 1, 0, out - position - 1 );
1006 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1007 mlt_playlist_virtual_refresh( self );
1017 /** Split the playlist at the absolute position.
1019 * \public \memberof mlt_playlist_s
1020 * \param self a playlist
1021 * \param position the time at which to split relative to the beginning of the clip
1022 * \param left true to split before the frame starting at position
1023 * \return true if there was an error
1026 int mlt_playlist_split_at( mlt_playlist self, mlt_position position, int left )
1028 int result = self == NULL ? -1 : 0;
1031 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) ) )
1033 int clip = mlt_playlist_get_clip_index_at( self, position );
1034 mlt_playlist_clip_info info;
1035 mlt_playlist_get_clip_info( self, &info, clip );
1036 if ( left && position != info.start )
1037 mlt_playlist_split( self, clip, position - info.start - 1 );
1039 mlt_playlist_split( self, clip, position - info.start );
1042 else if ( position <= 0 )
1048 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1054 /** Join 1 or more consecutive clips.
1056 * \public \memberof mlt_playlist_s
1057 * \param self a playlist
1058 * \param clip the starting playlist entry index
1059 * \param count the number of entries to merge
1060 * \param merge ignored
1061 * \return true if there was an error
1064 int mlt_playlist_join( mlt_playlist self, int clip, int count, int merge )
1066 int error = clip < 0 || clip >= self->count;
1070 mlt_playlist new_clip = mlt_playlist_init( );
1071 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1072 if ( clip + count >= self->count )
1073 count = self->count - clip - 1;
1074 for ( i = 0; i <= count; i ++ )
1076 playlist_entry *entry = self->list[ clip ];
1077 mlt_playlist_append( new_clip, entry->producer );
1078 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
1079 entry->preservation_hack = 1;
1080 mlt_playlist_remove( self, clip );
1082 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1083 mlt_playlist_insert( self, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
1084 mlt_playlist_close( new_clip );
1089 /** Mix consecutive clips for a specified length and apply transition if specified.
1091 * \public \memberof mlt_playlist_s
1092 * \param self a playlist
1093 * \param clip the index of the playlist entry
1094 * \param length the number of frames over which to create the mix
1095 * \param transition the transition to use for the mix
1096 * \return true if there was an error
1099 int mlt_playlist_mix( mlt_playlist self, int clip, int length, mlt_transition transition )
1101 int error = ( clip < 0 || clip + 1 >= self->count );
1104 playlist_entry *clip_a = self->list[ clip ];
1105 playlist_entry *clip_b = self->list[ clip + 1 ];
1106 mlt_producer track_a = NULL;
1107 mlt_producer track_b = NULL;
1108 mlt_tractor tractor = mlt_tractor_new( );
1109 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1111 // Check length is valid for both clips and resize if necessary.
1112 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
1113 length = length > max_size ? max_size : length;
1115 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
1116 if ( length != clip_a->frame_count )
1117 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
1119 track_a = clip_a->producer;
1121 if ( length != clip_b->frame_count )
1122 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
1124 track_b = clip_b->producer;
1126 // Set the tracks on the tractor
1127 mlt_tractor_set_track( tractor, track_a, 0 );
1128 mlt_tractor_set_track( tractor, track_b, 1 );
1130 // Insert the mix object into the playlist
1131 mlt_playlist_insert( self, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
1132 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
1134 // Attach the transition
1135 if ( transition != NULL )
1137 mlt_field field = mlt_tractor_field( tractor );
1138 mlt_field_plant_transition( field, transition, 0, 1 );
1139 mlt_transition_set_in_and_out( transition, 0, length - 1 );
1142 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
1143 if ( track_a != clip_a->producer )
1144 mlt_producer_close( track_a );
1145 if ( track_b != clip_b->producer )
1146 mlt_producer_close( track_b );
1148 // Check if we have anything left on the right hand clip
1149 if ( track_b == clip_b->producer )
1151 clip_b->preservation_hack = 1;
1152 mlt_playlist_remove( self, clip + 2 );
1154 else if ( clip_b->frame_out - clip_b->frame_in > length )
1156 mlt_playlist_resize_clip( self, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
1157 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
1158 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
1162 mlt_producer_clear( clip_b->producer );
1163 mlt_playlist_remove( self, clip + 2 );
1166 // Check if we have anything left on the left hand clip
1167 if ( track_a == clip_a->producer )
1169 clip_a->preservation_hack = 1;
1170 mlt_playlist_remove( self, clip );
1172 else if ( clip_a->frame_out - clip_a->frame_in > length )
1174 mlt_playlist_resize_clip( self, clip, clip_a->frame_in, clip_a->frame_out - length );
1175 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
1176 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
1180 mlt_producer_clear( clip_a->producer );
1181 mlt_playlist_remove( self, clip );
1184 // Unblock and force a fire off of change events to listeners
1185 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1186 mlt_playlist_virtual_refresh( self );
1187 mlt_tractor_close( tractor );
1192 /** Add a transition to an existing mix.
1194 * \public \memberof mlt_playlist_s
1195 * \param self a playlist
1196 * \param clip the index of the playlist entry
1197 * \param transition a transition
1198 * \return true if there was an error
1201 int mlt_playlist_mix_add( mlt_playlist self, int clip, mlt_transition transition )
1203 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1204 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1205 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1206 int error = transition == NULL || tractor == NULL;
1209 mlt_field field = mlt_tractor_field( tractor );
1210 mlt_field_plant_transition( field, transition, 0, 1 );
1211 mlt_transition_set_in_and_out( transition, 0, self->list[ clip ]->frame_count - 1 );
1216 /** Return the clip at the clip index.
1218 * \public \memberof mlt_playlist_s
1219 * \param self a playlist
1220 * \param clip the index of a playlist entry
1221 * \return a producer or NULL if there was an error
1224 mlt_producer mlt_playlist_get_clip( mlt_playlist self, int clip )
1226 if ( clip >= 0 && clip < self->count )
1227 return self->list[ clip ]->producer;
1231 /** Return the clip at the specified position.
1233 * \public \memberof mlt_playlist_s
1234 * \param self a playlist
1235 * \param position a time relative to the beginning of the playlist
1236 * \return a producer or NULL if not found
1239 mlt_producer mlt_playlist_get_clip_at( mlt_playlist self, mlt_position position )
1241 int index = 0, total = 0;
1242 return mlt_playlist_locate( self, &position, &index, &total );
1245 /** Return the clip index of the specified position.
1247 * \public \memberof mlt_playlist_s
1248 * \param self a playlist
1249 * \param position a time relative to the beginning of the playlist
1250 * \return the index of the playlist entry
1253 int mlt_playlist_get_clip_index_at( mlt_playlist self, mlt_position position )
1255 int index = 0, total = 0;
1256 mlt_playlist_locate( self, &position, &index, &total );
1260 /** Determine if the clip is a mix.
1262 * \public \memberof mlt_playlist_s
1263 * \param self a playlist
1264 * \param clip the index of the playlist entry
1265 * \return true if the producer is a mix
1268 int mlt_playlist_clip_is_mix( mlt_playlist self, int clip )
1270 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1271 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1272 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1273 return tractor != NULL;
1276 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1277 * back correctly on to the playlist.
1279 * \private \memberof mlt_playlist_s
1280 * \param self a playlist
1281 * \param clip the index of the playlist entry
1282 * \return true if there was an error
1285 static int mlt_playlist_unmix( mlt_playlist self, int clip )
1287 int error = ( clip < 0 || clip >= self->count );
1289 // Ensure that the clip request is actually a mix
1292 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1293 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1294 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1295 self->list[ clip ]->preservation_hack;
1300 playlist_entry *mix = self->list[ clip ];
1301 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1302 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1303 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1304 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1305 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1306 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1308 if ( clip_a != NULL )
1310 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1314 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1315 mlt_playlist_insert( self, cut, clip, -1, -1 );
1319 if ( clip_b != NULL )
1321 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1325 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1326 mlt_playlist_insert( self, cut, clip + 1, -1, -1 );
1329 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1330 mlt_playlist_remove( self, clip );
1331 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1332 mlt_playlist_virtual_refresh( self );
1337 /** Resize a mix clip.
1339 * \private \memberof mlt_playlist_s
1340 * \param self a playlist
1341 * \param clip the index of the playlist entry
1342 * \param in the new starting point
1343 * \param out the new ending point
1344 * \return true if there was an error
1347 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out )
1349 int error = ( clip < 0 || clip >= self->count );
1351 // Ensure that the clip request is actually a mix
1354 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1355 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1356 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1361 playlist_entry *mix = self->list[ clip ];
1362 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1363 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1364 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1365 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1366 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1367 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1368 int length = out - in + 1;
1369 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1370 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1372 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_diff );
1375 if ( clip_b != NULL )
1376 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1378 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1379 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1380 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1381 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1382 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1383 mlt_producer_set_in_and_out( mix->producer, in, out );
1385 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1386 mlt_playlist_virtual_refresh( self );
1391 /** Consolidate adjacent blank producers.
1393 * \public \memberof mlt_playlist_s
1394 * \param self a playlist
1395 * \param keep_length set false to remove the last entry if it is blank
1398 void mlt_playlist_consolidate_blanks( mlt_playlist self, int keep_length )
1403 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1405 mlt_events_block( properties, properties );
1406 for ( i = 1; i < self->count; i ++ )
1408 playlist_entry *left = self->list[ i - 1 ];
1409 playlist_entry *right = self->list[ i ];
1411 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1413 mlt_playlist_resize_clip( self, i - 1, 0, left->frame_count + right->frame_count - 1 );
1414 mlt_playlist_remove( self, i -- );
1418 if ( !keep_length && self->count > 0 )
1420 playlist_entry *last = self->list[ self->count - 1 ];
1421 if ( mlt_producer_is_blank( last->producer ) )
1422 mlt_playlist_remove( self, self->count - 1 );
1425 mlt_events_unblock( properties, properties );
1426 mlt_playlist_virtual_refresh( self );
1430 /** Determine if the specified clip index is a blank.
1432 * \public \memberof mlt_playlist_s
1433 * \param self a playlist
1434 * \param clip the index of the playlist entry
1435 * \return true if there was an error
1438 int mlt_playlist_is_blank( mlt_playlist self, int clip )
1440 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( self, clip ) );
1443 /** Determine if the specified position is a blank.
1445 * \public \memberof mlt_playlist_s
1446 * \param self a playlist
1447 * \param position a time relative to the start or end (negative) of the playlist
1448 * \return true if there was an error
1451 int mlt_playlist_is_blank_at( mlt_playlist self, mlt_position position )
1453 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( self, position ) );
1456 /** Replace the specified clip with a blank and return the clip.
1458 * \public \memberof mlt_playlist_s
1459 * \param self a playlist
1460 * \param clip the index of the playlist entry
1461 * \return a producer or NULL if there was an error
1464 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist self, int clip )
1466 mlt_producer producer = NULL;
1467 if ( !mlt_playlist_is_blank( self, clip ) )
1469 playlist_entry *entry = self->list[ clip ];
1470 int in = entry->frame_in;
1471 int out = entry->frame_out;
1472 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1473 producer = entry->producer;
1474 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1475 mlt_events_block( properties, properties );
1476 mlt_playlist_remove( self, clip );
1477 mlt_playlist_blank( self, out - in );
1478 mlt_playlist_move( self, self->count - 1, clip );
1479 mlt_events_unblock( properties, properties );
1480 mlt_playlist_virtual_refresh( self );
1481 mlt_producer_set_in_and_out( producer, in, out );
1486 /** Insert blank space.
1488 * \public \memberof mlt_playlist_s
1489 * \param self a playlist
1490 * \param clip the index of the new blank section
1491 * \param length the ending time of the new blank section (duration - 1)
1494 void mlt_playlist_insert_blank( mlt_playlist self, int clip, int length )
1496 if ( self != NULL && length >= 0 )
1498 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1499 mlt_events_block( properties, properties );
1500 mlt_playlist_blank( self, length );
1501 mlt_playlist_move( self, self->count - 1, clip );
1502 mlt_events_unblock( properties, properties );
1503 mlt_playlist_virtual_refresh( self );
1507 /** Resize a blank entry.
1509 * \public \memberof mlt_playlist_s
1510 * \param self a playlist
1511 * \param position the time at which the blank entry exists relative to the start or end (negative) of the playlist.
1512 * \param length the additional amount of blank frames to add
1513 * \param find true to fist locate the blank after the clip at position
1515 void mlt_playlist_pad_blanks( mlt_playlist self, mlt_position position, int length, int find )
1517 if ( self != NULL && length != 0 )
1519 int clip = mlt_playlist_get_clip_index_at( self, position );
1520 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1521 mlt_events_block( properties, properties );
1522 if ( find && clip < self->count && !mlt_playlist_is_blank( self, clip ) )
1524 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1526 mlt_playlist_clip_info info;
1527 mlt_playlist_get_clip_info( self, &info, clip );
1528 if ( info.frame_out + length > info.frame_in )
1529 mlt_playlist_resize_clip( self, clip, info.frame_in, info.frame_out + length );
1531 mlt_playlist_remove( self, clip );
1533 else if ( find && clip < self->count && length > 0 )
1535 mlt_playlist_insert_blank( self, clip, length );
1537 mlt_events_unblock( properties, properties );
1538 mlt_playlist_virtual_refresh( self );
1542 /** Insert a clip at a specific time.
1544 * \public \memberof mlt_playlist_s
1545 * \param self a playlist
1546 * \param position the time at which to insert
1547 * \param producer the producer to insert
1548 * \param mode true if you want to overwrite any blank section
1549 * \return true if there was an error
1552 int mlt_playlist_insert_at( mlt_playlist self, mlt_position position, mlt_producer producer, int mode )
1554 int ret = self == NULL || position < 0 || producer == NULL;
1557 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1558 int length = mlt_producer_get_playtime( producer );
1559 int clip = mlt_playlist_get_clip_index_at( self, position );
1560 mlt_playlist_clip_info info;
1561 mlt_playlist_get_clip_info( self, &info, clip );
1562 mlt_events_block( properties, self );
1563 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1565 // Split and move to new clip if need be
1566 if ( position != info.start && mlt_playlist_split( self, clip, position - info.start - 1 ) == 0 )
1567 mlt_playlist_get_clip_info( self, &info, ++ clip );
1569 // Split again if need be
1570 if ( length < info.frame_count )
1571 mlt_playlist_split( self, clip, length - 1 );
1574 mlt_playlist_remove( self, clip );
1577 mlt_playlist_insert( self, producer, clip, -1, -1 );
1580 else if ( clip < self->count )
1582 if ( position > info.start + info.frame_count / 2 )
1584 if ( mode == 1 && clip < self->count && mlt_playlist_is_blank( self, clip ) )
1586 mlt_playlist_get_clip_info( self, &info, clip );
1587 if ( length < info.frame_count )
1588 mlt_playlist_split( self, clip, length );
1589 mlt_playlist_remove( self, clip );
1591 mlt_playlist_insert( self, producer, clip, -1, -1 );
1597 if ( position == info.start )
1598 mlt_playlist_remove( self, clip );
1600 mlt_playlist_blank( self, position - mlt_properties_get_int( properties, "length" ) - 1 );
1602 mlt_playlist_append( self, producer );
1603 ret = self->count - 1;
1605 mlt_events_unblock( properties, self );
1606 mlt_playlist_virtual_refresh( self );
1615 /** Get the time at which the clip starts relative to the playlist.
1617 * \public \memberof mlt_playlist_s
1618 * \param self a playlist
1619 * \param clip the index of the playlist entry
1620 * \return the starting time
1623 int mlt_playlist_clip_start( mlt_playlist self, int clip )
1625 mlt_playlist_clip_info info;
1626 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1628 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1631 /** Get the playable duration of the clip.
1633 * \public \memberof mlt_playlist_s
1634 * \param self a playlist
1635 * \param clip the index of the playlist entry
1636 * \return the duration of the playlist entry
1639 int mlt_playlist_clip_length( mlt_playlist self, int clip )
1641 mlt_playlist_clip_info info;
1642 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1643 return info.frame_count;
1647 /** Get the duration of a blank space.
1649 * \public \memberof mlt_playlist_s
1650 * \param self a playlist
1651 * \param clip the index of the playlist entry
1652 * \param bounded the maximum number of blank entries or 0 for all
1653 * \return the duration of a blank section
1656 int mlt_playlist_blanks_from( mlt_playlist self, int clip, int bounded )
1659 mlt_playlist_clip_info info;
1660 if ( self != NULL && clip < self->count )
1662 mlt_playlist_get_clip_info( self, &info, clip );
1663 if ( mlt_playlist_is_blank( self, clip ) )
1664 count += info.frame_count;
1666 bounded = self->count;
1667 for ( clip ++; clip < self->count && bounded >= 0; clip ++ )
1669 mlt_playlist_get_clip_info( self, &info, clip );
1670 if ( mlt_playlist_is_blank( self, clip ) )
1671 count += info.frame_count;
1679 /** Remove a portion of the playlist by time.
1681 * \public \memberof mlt_playlist_s
1682 * \param self a playlist
1683 * \param position the starting time
1684 * \param length the duration of time to remove
1685 * \return the new entry index at the position
1688 int mlt_playlist_remove_region( mlt_playlist self, mlt_position position, int length )
1690 int index = mlt_playlist_get_clip_index_at( self, position );
1691 if ( index >= 0 && index < self->count )
1693 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1694 int clip_start = mlt_playlist_clip_start( self, index );
1695 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1696 mlt_events_block( properties, self );
1698 if ( position + length > list_length )
1699 length -= ( position + length - list_length );
1701 if ( clip_start < position )
1703 mlt_playlist_split( self, index ++, position - clip_start - 1 );
1708 if ( mlt_playlist_clip_length( self, index ) > length )
1709 mlt_playlist_split( self, index, length - 1 );
1710 length -= mlt_playlist_clip_length( self, index );
1711 mlt_playlist_remove( self, index );
1714 mlt_playlist_consolidate_blanks( self, 0 );
1715 mlt_events_unblock( properties, self );
1716 mlt_playlist_virtual_refresh( self );
1718 // Just to be sure, we'll get the clip index again...
1719 index = mlt_playlist_get_clip_index_at( self, position );
1726 * \deprecated not implemented
1727 * \public \memberof mlt_playlist_s
1731 * \param new_position
1735 int mlt_playlist_move_region( mlt_playlist self, mlt_position position, int length, int new_position )
1743 /** Get the current frame.
1745 * The implementation of the get_frame virtual function.
1746 * \private \memberof mlt_playlist_s
1747 * \param producer a producer
1748 * \param frame a frame by reference
1749 * \param index the time at which to get the frame
1753 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1755 // Check that we have a producer
1756 if ( producer == NULL )
1762 // Get this mlt_playlist
1763 mlt_playlist self = producer->child;
1765 // Need to ensure the frame is deinterlaced when repeating 1 frame
1766 int progressive = 0;
1768 // Get the real producer
1769 mlt_service real = mlt_playlist_virtual_seek( self, &progressive );
1771 // Check that we have a producer
1774 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1779 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1781 mlt_service_get_frame( real, frame, index );
1785 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1786 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1787 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1788 mlt_frame_push_service( *frame, NULL );
1789 mlt_frame_push_audio( *frame, NULL );
1790 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1791 mlt_service_apply_filters( real, *frame, 0 );
1792 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1793 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1796 // Check if we're at the end of the clip
1797 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1798 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1799 mlt_playlist_virtual_set_out( self );
1801 // Set the consumer progressive property
1804 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1805 mlt_properties_set_int( properties, "test_audio", 1 );
1808 // Check for notifier and call with appropriate argument
1809 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1810 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1811 if ( notifier != NULL )
1813 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1814 notifier( argument );
1817 // Update position on the frame we're creating
1818 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1820 // Position ourselves on the next frame
1821 mlt_producer_prepare_next( producer );
1826 /** Close the playlist.
1828 * \public \memberof mlt_playlist_s
1829 * \param self a playlist
1832 void mlt_playlist_close( mlt_playlist self )
1834 if ( self != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( self ) ) <= 0 )
1837 self->parent.close = NULL;
1838 for ( i = 0; i < self->count; i ++ )
1840 mlt_event_close( self->list[ i ]->event );
1841 mlt_producer_close( self->list[ i ]->producer );
1842 free( self->list[ i ] );
1844 mlt_producer_close( &self->blank );
1845 mlt_producer_close( &self->parent );