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 /** Construct a playlist with a profile.
107 * Sets the resource property to "<playlist>".
108 * Set the mlt_type to property to "mlt_producer".
109 * \public \memberof mlt_playlist_s
110 * \param profile the profile to use with the profile
111 * \return a new playlist
114 mlt_playlist mlt_playlist_new( mlt_profile profile )
116 mlt_playlist self = mlt_playlist_init();
118 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "_profile", profile, 0, NULL, NULL );
122 /** Get the producer associated to this playlist.
124 * \public \memberof mlt_playlist_s
125 * \param self a playlist
126 * \return the producer interface
127 * \see MLT_PLAYLIST_PRODUCER
130 mlt_producer mlt_playlist_producer( mlt_playlist self )
132 return self != NULL ? &self->parent : NULL;
135 /** Get the service associated to this playlist.
137 * \public \memberof mlt_playlist_s
138 * \param self a playlist
139 * \return the service interface
140 * \see MLT_PLAYLIST_SERVICE
143 mlt_service mlt_playlist_service( mlt_playlist self )
145 return MLT_PRODUCER_SERVICE( &self->parent );
148 /** Get the properties associated to this playlist.
150 * \public \memberof mlt_playlist_s
151 * \param self a playlist
152 * \return the playlist's properties list
153 * \see MLT_PLAYLIST_PROPERTIES
156 mlt_properties mlt_playlist_properties( mlt_playlist self )
158 return MLT_PRODUCER_PROPERTIES( &self->parent );
161 /** Refresh the playlist after a clip has been changed.
163 * \private \memberof mlt_playlist_s
164 * \param self a playlist
168 static int mlt_playlist_virtual_refresh( mlt_playlist self )
170 // Obtain the properties
171 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
173 mlt_position frame_count = 0;
175 for ( i = 0; i < self->count; i ++ )
178 mlt_producer producer = self->list[ i ]->producer;
181 int current_length = mlt_producer_get_playtime( producer );
183 // Check if the length of the producer has changed
184 if ( self->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
185 self->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
187 // This clip should be removed...
188 if ( current_length < 1 )
190 self->list[ i ]->frame_in = 0;
191 self->list[ i ]->frame_out = -1;
192 self->list[ i ]->frame_count = 0;
196 self->list[ i ]->frame_in = mlt_producer_get_in( producer );
197 self->list[ i ]->frame_out = mlt_producer_get_out( producer );
198 self->list[ i ]->frame_count = current_length;
201 // Update the producer_length
202 self->list[ i ]->producer_length = current_length;
206 // Calculate the frame_count
207 self->list[ i ]->frame_count = ( self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1 ) * self->list[ i ]->repeat;
209 // Update the frame_count for self clip
210 frame_count += self->list[ i ]->frame_count;
213 // Refresh all properties
214 mlt_events_block( properties, properties );
215 mlt_properties_set_position( properties, "length", frame_count );
216 mlt_events_unblock( properties, properties );
217 mlt_properties_set_position( properties, "out", frame_count - 1 );
222 /** Listener for producers on the playlist.
224 * Refreshes the playlist whenever an entry receives producer-changed.
225 * \private \memberof mlt_playlist_s
226 * \param producer a producer
227 * \param self a playlist
230 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist self )
232 mlt_playlist_virtual_refresh( self );
235 /** Append to the virtual playlist.
237 * \private \memberof mlt_playlist_s
238 * \param self a playlist
239 * \param source a producer
240 * \param in the producer's starting time
241 * \param out the producer's ending time
242 * \return true if there was an error
245 static int mlt_playlist_virtual_append( mlt_playlist self, mlt_producer source, mlt_position in, mlt_position out )
247 mlt_producer producer = NULL;
248 mlt_properties properties = NULL;
249 mlt_properties parent = NULL;
251 // If we have a cut, then use the in/out points from the cut
252 if ( mlt_producer_is_blank( source ) )
254 mlt_position length = out - in + 1;
256 // Make sure the blank is long enough to accomodate the length specified
257 if ( length > mlt_producer_get_length( &self->blank ) )
259 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
260 mlt_events_block( blank_props, blank_props );
261 mlt_producer_set_in_and_out( &self->blank, in, out );
262 mlt_events_unblock( blank_props, blank_props );
265 // Now make sure the cut comes from this self->blank
266 if ( source == NULL )
268 producer = mlt_producer_cut( &self->blank, in, out );
270 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &self->blank )
272 producer = mlt_producer_cut( &self->blank, in, out );
277 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
280 properties = MLT_PRODUCER_PROPERTIES( producer );
282 // Make sure this cut of blank is long enough
283 if ( length > mlt_producer_get_length( producer ) )
284 mlt_properties_set_int( properties, "length", length );
286 else if ( mlt_producer_is_cut( source ) )
290 in = mlt_producer_get_in( producer );
291 if ( out < 0 || out > mlt_producer_get_out( producer ) )
292 out = mlt_producer_get_out( producer );
293 properties = MLT_PRODUCER_PROPERTIES( producer );
294 mlt_properties_inc_ref( properties );
298 producer = mlt_producer_cut( source, in, out );
299 if ( in < 0 || in < mlt_producer_get_in( producer ) )
300 in = mlt_producer_get_in( producer );
301 if ( out < 0 || out > mlt_producer_get_out( producer ) )
302 out = mlt_producer_get_out( producer );
303 properties = MLT_PRODUCER_PROPERTIES( producer );
306 // Fetch the cuts parent properties
307 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
309 // Remove loader normalisers for fx cuts
310 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
312 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
313 mlt_filter filter = mlt_service_filter( service, 0 );
314 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_loader" ) )
316 mlt_service_detach( service, filter );
317 filter = mlt_service_filter( service, 0 );
319 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
322 // Check that we have room
323 if ( self->count >= self->size )
326 self->list = realloc( self->list, ( self->size + 10 ) * sizeof( playlist_entry * ) );
327 for ( i = self->size; i < self->size + 10; i ++ ) self->list[ i ] = NULL;
332 self->list[ self->count ] = calloc( sizeof( playlist_entry ), 1 );
333 if ( self->list[ self->count ] != NULL )
335 self->list[ self->count ]->producer = producer;
336 self->list[ self->count ]->frame_in = in;
337 self->list[ self->count ]->frame_out = out;
338 self->list[ self->count ]->frame_count = out - in + 1;
339 self->list[ self->count ]->repeat = 1;
340 self->list[ self->count ]->producer_length = mlt_producer_get_playtime( producer );
341 self->list[ self->count ]->event = mlt_events_listen( parent, self, "producer-changed", ( mlt_listener )mlt_playlist_listener );
342 mlt_event_inc_ref( self->list[ self->count ]->event );
343 mlt_properties_set( properties, "eof", "pause" );
344 mlt_producer_set_speed( producer, 0 );
348 return mlt_playlist_virtual_refresh( self );
351 /** Locate a producer by index.
353 * \private \memberof mlt_playlist_s
354 * \param self a playlist
355 * \param[in, out] position the time at which to locate the producer, returns the time relative to the producer's starting point
356 * \param[out] clip the index of the playlist entry
357 * \param[out] total the duration of the playlist up to and including this producer
358 * \return a producer or NULL if not found
361 static mlt_producer mlt_playlist_locate( mlt_playlist self, mlt_position *position, int *clip, int *total )
363 // Default producer to NULL
364 mlt_producer producer = NULL;
366 // Loop for each producer until found
367 for ( *clip = 0; *clip < self->count; *clip += 1 )
369 // Increment the total
370 *total += self->list[ *clip ]->frame_count;
372 // Check if the position indicates that we have found the clip
373 // Note that 0 length clips get skipped automatically
374 if ( *position < self->list[ *clip ]->frame_count )
376 // Found it, now break
377 producer = self->list[ *clip ]->producer;
382 // Decrement position by length of self entry
383 *position -= self->list[ *clip ]->frame_count;
390 /** Seek in the virtual playlist.
392 * This gets the producer at the current position and seeks on the producer
393 * while doing repeat and end-of-file handling. This is also responsible for
394 * closing producers previous to the preceding playlist if the autoclose
396 * \private \memberof mlt_playlist_s
397 * \param self a playlist
398 * \param[out] progressive true if the producer should be displayed progressively
399 * \return the service interface of the producer at the play head
400 * \see producer_get_frame
403 static mlt_service mlt_playlist_virtual_seek( mlt_playlist self, int *progressive )
405 // Map playlist position to real producer in virtual playlist
406 mlt_position position = mlt_producer_frame( &self->parent );
408 // Keep the original position since we change it while iterating through the list
409 mlt_position original = position;
411 // Clip index and total
415 // Locate the producer for the position
416 mlt_producer producer = mlt_playlist_locate( self, &position, &i, &total );
418 // Get the properties
419 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
421 // Automatically close previous producers if requested
422 if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
423 && position < 2 // tolerate off-by-one error on going to next clip
424 && mlt_properties_get_int( properties, "autoclose" ) )
427 // They might have jumped ahead!
428 for ( j = 0; j < i - 1; j++ )
430 mlt_service_lock( MLT_PRODUCER_SERVICE( self->list[ j ]->producer ) );
431 mlt_producer p = self->list[ j ]->producer;
434 self->list[ j ]->producer = NULL;
435 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
436 mlt_producer_close( p );
438 // If p is null, the lock will not have been "taken"
442 // Get the eof handling
443 char *eof = mlt_properties_get( properties, "eof" );
445 // Seek in real producer to relative position
446 if ( producer != NULL )
448 int count = self->list[ i ]->frame_count / self->list[ i ]->repeat;
449 *progressive = count == 1;
450 mlt_producer_seek( producer, (int)position % count );
452 else if ( !strcmp( eof, "pause" ) && total > 0 )
454 playlist_entry *entry = self->list[ self->count - 1 ];
455 int count = entry->frame_count / entry->repeat;
456 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
457 mlt_producer_seek( self_producer, original - 1 );
458 producer = entry->producer;
459 mlt_producer_seek( producer, (int)entry->frame_out % count );
460 mlt_producer_set_speed( self_producer, 0 );
461 mlt_producer_set_speed( producer, 0 );
462 *progressive = count == 1;
464 else if ( !strcmp( eof, "loop" ) && total > 0 )
466 playlist_entry *entry = self->list[ 0 ];
467 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
468 mlt_producer_seek( self_producer, 0 );
469 producer = entry->producer;
470 mlt_producer_seek( producer, 0 );
474 producer = &self->blank;
477 return MLT_PRODUCER_SERVICE( producer );
480 /** Invoked when a producer indicates that it has prematurely reached its end.
482 * \private \memberof mlt_playlist_s
483 * \param self a playlist
485 * \see producer_get_frame
488 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist self )
490 // Default producer to blank
491 mlt_producer producer = &self->blank;
493 // Map playlist position to real producer in virtual playlist
494 mlt_position position = mlt_producer_frame( &self->parent );
496 // Loop through the virtual playlist
499 for ( i = 0; i < self->count; i ++ )
501 if ( position < self->list[ i ]->frame_count )
503 // Found it, now break
504 producer = self->list[ i ]->producer;
509 // Decrement position by length of this entry
510 position -= self->list[ i ]->frame_count;
514 // Seek in real producer to relative position
515 if ( i < self->count && self->list[ i ]->frame_out != position )
517 // Update the frame_count for the changed clip (hmmm)
518 self->list[ i ]->frame_out = position;
519 self->list[ i ]->frame_count = self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1;
521 // Refresh the playlist
522 mlt_playlist_virtual_refresh( self );
528 /** Obtain the current clips index.
530 * \public \memberof mlt_playlist_s
531 * \param self a playlist
532 * \return the index of the playlist entry at the current position
535 int mlt_playlist_current_clip( mlt_playlist self )
537 // Map playlist position to real producer in virtual playlist
538 mlt_position position = mlt_producer_frame( &self->parent );
540 // Loop through the virtual playlist
543 for ( i = 0; i < self->count; i ++ )
545 if ( position < self->list[ i ]->frame_count )
547 // Found it, now break
552 // Decrement position by length of this entry
553 position -= self->list[ i ]->frame_count;
560 /** Obtain the current clips producer.
562 * \public \memberof mlt_playlist_s
563 * \param self a playlist
564 * \return the producer at the current position
567 mlt_producer mlt_playlist_current( mlt_playlist self )
569 int i = mlt_playlist_current_clip( self );
570 if ( i < self->count )
571 return self->list[ i ]->producer;
576 /** Get the position which corresponds to the start of the next clip.
578 * \public \memberof mlt_playlist_s
579 * \param self a playlist
580 * \param whence the location from which to make the index relative:
581 * start of playlist, end of playlist, or current position
582 * \param index the playlist entry index relative to whence
583 * \return the time at which the referenced clip starts
586 mlt_position mlt_playlist_clip( mlt_playlist self, mlt_whence whence, int index )
588 mlt_position position = 0;
589 int absolute_clip = index;
592 // Determine the absolute clip
595 case mlt_whence_relative_start:
596 absolute_clip = index;
599 case mlt_whence_relative_current:
600 absolute_clip = mlt_playlist_current_clip( self ) + index;
603 case mlt_whence_relative_end:
604 absolute_clip = self->count - index;
608 // Check that we're in a valid range
609 if ( absolute_clip < 0 )
611 else if ( absolute_clip > self->count )
612 absolute_clip = self->count;
614 // Now determine the position
615 for ( i = 0; i < absolute_clip; i ++ )
616 position += self->list[ i ]->frame_count;
621 /** Get all the info about the clip specified.
623 * \public \memberof mlt_playlist_s
624 * \param self a playlist
625 * \param info a clip info struct
626 * \param index a playlist entry index
627 * \return true if there was an error
630 int mlt_playlist_get_clip_info( mlt_playlist self, mlt_playlist_clip_info *info, int index )
632 int error = index < 0 || index >= self->count || self->list[ index ]->producer == NULL;
633 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
636 mlt_producer producer = mlt_producer_cut_parent( self->list[ index ]->producer );
637 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
639 info->producer = producer;
640 info->cut = self->list[ index ]->producer;
641 info->start = mlt_playlist_clip( self, mlt_whence_relative_start, index );
642 info->resource = mlt_properties_get( properties, "resource" );
643 info->frame_in = self->list[ index ]->frame_in;
644 info->frame_out = self->list[ index ]->frame_out;
645 info->frame_count = self->list[ index ]->frame_count;
646 info->repeat = self->list[ index ]->repeat;
647 info->length = mlt_producer_get_length( producer );
648 info->fps = mlt_producer_get_fps( producer );
654 /** Get number of clips in the playlist.
656 * \public \memberof mlt_playlist_s
657 * \param self a playlist
658 * \return the number of playlist entries
661 int mlt_playlist_count( mlt_playlist self )
666 /** Clear the playlist.
668 * \public \memberof mlt_playlist_s
669 * \param self a playlist
670 * \return true if there was an error
673 int mlt_playlist_clear( mlt_playlist self )
676 for ( i = 0; i < self->count; i ++ )
678 mlt_event_close( self->list[ i ]->event );
679 mlt_producer_close( self->list[ i ]->producer );
682 return mlt_playlist_virtual_refresh( self );
685 /** Append a producer to the playlist.
687 * \public \memberof mlt_playlist_s
688 * \param self a playlist
689 * \param producer the producer to append
690 * \return true if there was an error
693 int mlt_playlist_append( mlt_playlist self, mlt_producer producer )
695 // Append to virtual list
696 return mlt_playlist_virtual_append( self, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
699 /** Append a producer to the playlist with in/out points.
701 * \public \memberof mlt_playlist_s
702 * \param self a playlist
703 * \param producer the producer to append
704 * \param in the starting point on the producer; a negative value is the same as 0
705 * \param out the ending point on the producer; a negative value is the same as producer length - 1
706 * \return true if there was an error
709 int mlt_playlist_append_io( mlt_playlist self, mlt_producer producer, mlt_position in, mlt_position out )
711 // Append to virtual list
712 if ( in < 0 && out < 0 )
713 return mlt_playlist_append( self, producer );
715 return mlt_playlist_virtual_append( self, producer, in, out );
718 /** Append a blank to the playlist of a given length.
720 * \public \memberof mlt_playlist_s
721 * \param self a playlist
722 * \param out the ending time of the blank entry, not its duration
723 * \return true if there was an error
726 int mlt_playlist_blank( mlt_playlist self, mlt_position out )
728 // Append to the virtual list
730 return mlt_playlist_virtual_append( self, &self->blank, 0, out );
735 /** Append a blank item to the playlist with duration as a time string.
737 * \public \memberof mlt_playlist_s
738 * \param self a playlist
739 * \param length the duration of the blank entry as a time string
740 * \return true if there was an error
743 int mlt_playlist_blank_time( mlt_playlist self, const char* length )
745 if ( self && length )
747 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
748 mlt_properties_set( properties , "_blank_time", length );
749 mlt_position duration = mlt_properties_get_position( properties, "_blank_time" );
750 return mlt_playlist_blank( self, duration - 1 );
756 /** Insert a producer into the playlist.
758 * \public \memberof mlt_playlist_s
759 * \param self a playlist
760 * \param producer the producer to insert
761 * \param where the producer's playlist entry index
762 * \param in the starting point on the producer
763 * \param out the ending point on the producer
764 * \return true if there was an error
767 int mlt_playlist_insert( mlt_playlist self, mlt_producer producer, int where, mlt_position in, mlt_position out )
770 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
771 mlt_playlist_append_io( self, producer, in, out );
773 // Move to the position specified
774 mlt_playlist_move( self, self->count - 1, where );
775 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
777 return mlt_playlist_virtual_refresh( self );
780 /** Remove an entry in the playlist.
782 * \public \memberof mlt_playlist_s
783 * \param self a playlist
784 * \param where the playlist entry index
785 * \return true if there was an error
788 int mlt_playlist_remove( mlt_playlist self, int where )
790 int error = where < 0 || where >= self->count;
791 if ( error == 0 && mlt_playlist_unmix( self, where ) != 0 )
793 // We need to know the current clip and the position within the playlist
794 int current = mlt_playlist_current_clip( self );
795 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
797 // We need all the details about the clip we're removing
798 mlt_playlist_clip_info where_info;
799 playlist_entry *entry = self->list[ where ];
800 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
806 mlt_playlist_get_clip_info( self, &where_info, where );
808 // Make sure the clip to be removed is valid and correct if necessary
811 if ( where >= self->count )
812 where = self->count - 1;
814 // Reorganise the list
815 for ( i = where + 1; i < self->count; i ++ )
816 self->list[ i - 1 ] = self->list[ i ];
819 if ( entry->preservation_hack == 0 )
821 // Decouple from mix_in/out if necessary
822 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
824 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
825 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
827 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
829 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
830 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
833 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
834 mlt_producer_clear( entry->producer );
837 // Close the producer associated to the clip info
838 mlt_event_close( entry->event );
839 mlt_producer_close( entry->producer );
842 if ( where == current )
843 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), where_info.start );
844 else if ( where < current && self->count > 0 )
845 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), position - where_info.frame_count );
846 else if ( self->count == 0 )
847 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), 0 );
852 // Refresh the playlist
853 mlt_playlist_virtual_refresh( self );
859 /** Move an entry in the playlist.
861 * \public \memberof mlt_playlist_s
862 * \param self a playlist
863 * \param src an entry index
864 * \param dest an entry index
868 int mlt_playlist_move( mlt_playlist self, int src, int dest )
872 /* We need to ensure that the requested indexes are valid and correct it as necessary */
875 if ( src >= self->count )
876 src = self->count - 1;
880 if ( dest >= self->count )
881 dest = self->count - 1;
883 if ( src != dest && self->count > 1 )
885 int current = mlt_playlist_current_clip( self );
886 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
887 playlist_entry *src_entry = NULL;
889 // We need all the details about the current clip
890 mlt_playlist_clip_info current_info;
892 mlt_playlist_get_clip_info( self, ¤t_info, current );
893 position -= current_info.start;
895 if ( current == src )
897 else if ( current > src && current < dest )
899 else if ( current == dest )
902 src_entry = self->list[ src ];
905 for ( i = src; i > dest; i -- )
906 self->list[ i ] = self->list[ i - 1 ];
910 for ( i = src; i < dest; i ++ )
911 self->list[ i ] = self->list[ i + 1 ];
913 self->list[ dest ] = src_entry;
915 mlt_playlist_get_clip_info( self, ¤t_info, current );
916 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), current_info.start + position );
917 mlt_playlist_virtual_refresh( self );
923 /** Repeat the specified clip n times.
925 * \public \memberof mlt_playlist_s
926 * \param self a playlist
927 * \param clip a playlist entry index
928 * \param repeat the number of times to repeat the clip
929 * \return true if there was an error
932 int mlt_playlist_repeat_clip( mlt_playlist self, int clip, int repeat )
934 int error = repeat < 1 || clip < 0 || clip >= self->count;
937 playlist_entry *entry = self->list[ clip ];
938 entry->repeat = repeat;
939 mlt_playlist_virtual_refresh( self );
944 /** Resize the specified clip.
946 * \public \memberof mlt_playlist_s
947 * \param self a playlist
948 * \param clip the index of the playlist entry
949 * \param in the new starting time on the clip's producer; a negative value is the same as 0
950 * \param out the new ending time on the clip's producer; a negative value is the same as length - 1
951 * \return true if there was an error
954 int mlt_playlist_resize_clip( mlt_playlist self, int clip, mlt_position in, mlt_position out )
956 int error = clip < 0 || clip >= self->count;
957 if ( error == 0 && mlt_playlist_resize_mix( self, clip, in, out ) != 0 )
959 playlist_entry *entry = self->list[ clip ];
960 mlt_producer producer = entry->producer;
961 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
963 mlt_events_block( properties, properties );
965 if ( mlt_producer_is_blank( producer ) )
967 mlt_position length = out - in + 1;
969 // Make sure the parent blank is long enough to accomodate the length specified
970 if ( length > mlt_producer_get_length( &self->blank ) )
972 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
973 mlt_properties_set_int( blank_props, "length", length );
974 mlt_producer_set_in_and_out( &self->blank, 0, out - in );
976 // Make sure this cut of blank is long enough
977 if ( length > mlt_producer_get_length( producer ) )
978 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", length );
983 if ( out < 0 || out >= mlt_producer_get_length( producer ) )
984 out = mlt_producer_get_length( producer ) - 1;
993 mlt_producer_set_in_and_out( producer, in, out );
994 mlt_events_unblock( properties, properties );
995 mlt_playlist_virtual_refresh( self );
1000 /** Split a clip on the playlist at the given position.
1002 * This splits after the specified frame.
1003 * \public \memberof mlt_playlist_s
1004 * \param self a playlist
1005 * \param clip the index of the playlist entry
1006 * \param position the time at which to split relative to the beginning of the clip or its end if negative
1007 * \return true if there was an error
1010 int mlt_playlist_split( mlt_playlist self, int clip, mlt_position position )
1012 int error = clip < 0 || clip >= self->count;
1015 playlist_entry *entry = self->list[ clip ];
1016 position = position < 0 ? entry->frame_count + position - 1 : position;
1017 if ( position >= 0 && position < entry->frame_count - 1 )
1019 int in = entry->frame_in;
1020 int out = entry->frame_out;
1021 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1022 mlt_playlist_resize_clip( self, clip, in, in + position );
1023 if ( !mlt_producer_is_blank( entry->producer ) )
1026 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
1027 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
1028 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
1029 mlt_playlist_insert( self, split, clip + 1, 0, -1 );
1030 mlt_properties_lock( entry_properties );
1031 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
1033 char *name = mlt_properties_get_name( entry_properties, i );
1034 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
1035 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
1037 mlt_properties_unlock( entry_properties );
1038 mlt_producer_close( split );
1042 mlt_playlist_insert( self, &self->blank, clip + 1, 0, out - position - 1 );
1044 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1045 mlt_playlist_virtual_refresh( self );
1055 /** Split the playlist at the absolute position.
1057 * \public \memberof mlt_playlist_s
1058 * \param self a playlist
1059 * \param position the time at which to split relative to the beginning of the clip
1060 * \param left true to split before the frame starting at position
1061 * \return true if there was an error
1064 int mlt_playlist_split_at( mlt_playlist self, mlt_position position, int left )
1066 int result = self == NULL ? -1 : 0;
1069 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) ) )
1071 int clip = mlt_playlist_get_clip_index_at( self, position );
1072 mlt_playlist_clip_info info;
1073 mlt_playlist_get_clip_info( self, &info, clip );
1074 if ( left && position != info.start )
1075 mlt_playlist_split( self, clip, position - info.start - 1 );
1077 mlt_playlist_split( self, clip, position - info.start );
1080 else if ( position <= 0 )
1086 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1092 /** Join 1 or more consecutive clips.
1094 * \public \memberof mlt_playlist_s
1095 * \param self a playlist
1096 * \param clip the starting playlist entry index
1097 * \param count the number of entries to merge
1098 * \param merge ignored
1099 * \return true if there was an error
1102 int mlt_playlist_join( mlt_playlist self, int clip, int count, int merge )
1104 int error = clip < 0 || clip >= self->count;
1108 mlt_playlist new_clip = mlt_playlist_new( mlt_service_profile( MLT_PLAYLIST_SERVICE(self) ) );
1109 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1110 if ( clip + count >= self->count )
1111 count = self->count - clip - 1;
1112 for ( i = 0; i <= count; i ++ )
1114 playlist_entry *entry = self->list[ clip ];
1115 mlt_playlist_append( new_clip, entry->producer );
1116 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
1117 entry->preservation_hack = 1;
1118 mlt_playlist_remove( self, clip );
1120 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1121 mlt_playlist_insert( self, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
1122 mlt_playlist_close( new_clip );
1127 /** Mix consecutive clips for a specified length and apply transition if specified.
1129 * \public \memberof mlt_playlist_s
1130 * \param self a playlist
1131 * \param clip the index of the playlist entry
1132 * \param length the number of frames over which to create the mix
1133 * \param transition the transition to use for the mix
1134 * \return true if there was an error
1137 int mlt_playlist_mix( mlt_playlist self, int clip, int length, mlt_transition transition )
1139 int error = ( clip < 0 || clip + 1 >= self->count );
1142 playlist_entry *clip_a = self->list[ clip ];
1143 playlist_entry *clip_b = self->list[ clip + 1 ];
1144 mlt_producer track_a = NULL;
1145 mlt_producer track_b = NULL;
1146 mlt_tractor tractor = mlt_tractor_new( );
1147 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1149 // Check length is valid for both clips and resize if necessary.
1150 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
1151 length = length > max_size ? max_size : length;
1153 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
1154 if ( length != clip_a->frame_count )
1155 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
1157 track_a = clip_a->producer;
1159 if ( length != clip_b->frame_count )
1160 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
1162 track_b = clip_b->producer;
1164 // Set the tracks on the tractor
1165 mlt_tractor_set_track( tractor, track_a, 0 );
1166 mlt_tractor_set_track( tractor, track_b, 1 );
1168 // Insert the mix object into the playlist
1169 mlt_playlist_insert( self, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
1170 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
1172 // Attach the transition
1173 if ( transition != NULL )
1175 mlt_field field = mlt_tractor_field( tractor );
1176 mlt_field_plant_transition( field, transition, 0, 1 );
1177 mlt_transition_set_in_and_out( transition, 0, length - 1 );
1180 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
1181 if ( track_a != clip_a->producer )
1182 mlt_producer_close( track_a );
1183 if ( track_b != clip_b->producer )
1184 mlt_producer_close( track_b );
1186 // Check if we have anything left on the right hand clip
1187 if ( track_b == clip_b->producer )
1189 clip_b->preservation_hack = 1;
1190 mlt_playlist_remove( self, clip + 2 );
1192 else if ( clip_b->frame_out - clip_b->frame_in > length )
1194 mlt_playlist_resize_clip( self, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
1195 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
1196 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
1200 mlt_producer_clear( clip_b->producer );
1201 mlt_playlist_remove( self, clip + 2 );
1204 // Check if we have anything left on the left hand clip
1205 if ( track_a == clip_a->producer )
1207 clip_a->preservation_hack = 1;
1208 mlt_playlist_remove( self, clip );
1210 else if ( clip_a->frame_out - clip_a->frame_in > length )
1212 mlt_playlist_resize_clip( self, clip, clip_a->frame_in, clip_a->frame_out - length );
1213 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
1214 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
1218 mlt_producer_clear( clip_a->producer );
1219 mlt_playlist_remove( self, clip );
1222 // Unblock and force a fire off of change events to listeners
1223 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1224 mlt_playlist_virtual_refresh( self );
1225 mlt_tractor_close( tractor );
1230 /** Add a transition to an existing mix.
1232 * \public \memberof mlt_playlist_s
1233 * \param self a playlist
1234 * \param clip the index of the playlist entry
1235 * \param transition a transition
1236 * \return true if there was an error
1239 int mlt_playlist_mix_add( mlt_playlist self, int clip, mlt_transition transition )
1241 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1242 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1243 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1244 int error = transition == NULL || tractor == NULL;
1247 mlt_field field = mlt_tractor_field( tractor );
1248 mlt_field_plant_transition( field, transition, 0, 1 );
1249 mlt_transition_set_in_and_out( transition, 0, self->list[ clip ]->frame_count - 1 );
1254 /** Return the clip at the clip index.
1256 * \public \memberof mlt_playlist_s
1257 * \param self a playlist
1258 * \param clip the index of a playlist entry
1259 * \return a producer or NULL if there was an error
1262 mlt_producer mlt_playlist_get_clip( mlt_playlist self, int clip )
1264 if ( clip >= 0 && clip < self->count )
1265 return self->list[ clip ]->producer;
1269 /** Return the clip at the specified position.
1271 * \public \memberof mlt_playlist_s
1272 * \param self a playlist
1273 * \param position a time relative to the beginning of the playlist
1274 * \return a producer or NULL if not found
1277 mlt_producer mlt_playlist_get_clip_at( mlt_playlist self, mlt_position position )
1279 int index = 0, total = 0;
1280 return mlt_playlist_locate( self, &position, &index, &total );
1283 /** Return the clip index of the specified position.
1285 * \public \memberof mlt_playlist_s
1286 * \param self a playlist
1287 * \param position a time relative to the beginning of the playlist
1288 * \return the index of the playlist entry
1291 int mlt_playlist_get_clip_index_at( mlt_playlist self, mlt_position position )
1293 int index = 0, total = 0;
1294 mlt_playlist_locate( self, &position, &index, &total );
1298 /** Determine if the clip is a mix.
1300 * \public \memberof mlt_playlist_s
1301 * \param self a playlist
1302 * \param clip the index of the playlist entry
1303 * \return true if the producer is a mix
1306 int mlt_playlist_clip_is_mix( mlt_playlist self, int clip )
1308 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1309 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1310 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1311 return tractor != NULL;
1314 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1315 * back correctly on to the playlist.
1317 * \private \memberof mlt_playlist_s
1318 * \param self a playlist
1319 * \param clip the index of the playlist entry
1320 * \return true if there was an error
1323 static int mlt_playlist_unmix( mlt_playlist self, int clip )
1325 int error = ( clip < 0 || clip >= self->count );
1327 // Ensure that the clip request is actually a mix
1330 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1331 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1332 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1333 self->list[ clip ]->preservation_hack;
1338 playlist_entry *mix = self->list[ clip ];
1339 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1340 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1341 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1342 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1343 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1344 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1346 if ( clip_a != NULL )
1348 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1352 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1353 mlt_playlist_insert( self, cut, clip, -1, -1 );
1357 if ( clip_b != NULL )
1359 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1363 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1364 mlt_playlist_insert( self, cut, clip + 1, -1, -1 );
1367 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1368 mlt_playlist_remove( self, clip );
1369 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1370 mlt_playlist_virtual_refresh( self );
1375 /** Resize a mix clip.
1377 * \private \memberof mlt_playlist_s
1378 * \param self a playlist
1379 * \param clip the index of the playlist entry
1380 * \param in the new starting point
1381 * \param out the new ending point
1382 * \return true if there was an error
1385 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out )
1387 int error = ( clip < 0 || clip >= self->count );
1389 // Ensure that the clip request is actually a mix
1392 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1393 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1394 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1399 playlist_entry *mix = self->list[ clip ];
1400 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1401 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1402 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1403 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1404 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1405 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1406 int length = out - in + 1;
1407 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1408 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1410 if ( clip_a != NULL )
1411 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1413 if ( clip_b != NULL )
1414 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1416 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1417 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1418 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1419 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1420 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1421 mlt_producer_set_in_and_out( mix->producer, in, out );
1423 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1424 mlt_playlist_virtual_refresh( self );
1429 /** Consolidate adjacent blank producers.
1431 * \public \memberof mlt_playlist_s
1432 * \param self a playlist
1433 * \param keep_length set false to remove the last entry if it is blank
1436 void mlt_playlist_consolidate_blanks( mlt_playlist self, int keep_length )
1441 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1443 mlt_events_block( properties, properties );
1444 for ( i = 1; i < self->count; i ++ )
1446 playlist_entry *left = self->list[ i - 1 ];
1447 playlist_entry *right = self->list[ i ];
1449 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1451 mlt_playlist_resize_clip( self, i - 1, 0, left->frame_count + right->frame_count - 1 );
1452 mlt_playlist_remove( self, i -- );
1456 if ( !keep_length && self->count > 0 )
1458 playlist_entry *last = self->list[ self->count - 1 ];
1459 if ( mlt_producer_is_blank( last->producer ) )
1460 mlt_playlist_remove( self, self->count - 1 );
1463 mlt_events_unblock( properties, properties );
1464 mlt_playlist_virtual_refresh( self );
1468 /** Determine if the specified clip index is a blank.
1470 * \public \memberof mlt_playlist_s
1471 * \param self a playlist
1472 * \param clip the index of the playlist entry
1473 * \return true if there was an error
1476 int mlt_playlist_is_blank( mlt_playlist self, int clip )
1478 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( self, clip ) );
1481 /** Determine if the specified position is a blank.
1483 * \public \memberof mlt_playlist_s
1484 * \param self a playlist
1485 * \param position a time relative to the start or end (negative) of the playlist
1486 * \return true if there was an error
1489 int mlt_playlist_is_blank_at( mlt_playlist self, mlt_position position )
1491 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( self, position ) );
1494 /** Replace the specified clip with a blank and return the clip.
1496 * \public \memberof mlt_playlist_s
1497 * \param self a playlist
1498 * \param clip the index of the playlist entry
1499 * \return a producer or NULL if there was an error
1502 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist self, int clip )
1504 mlt_producer producer = NULL;
1505 if ( !mlt_playlist_is_blank( self, clip ) )
1507 playlist_entry *entry = self->list[ clip ];
1508 int in = entry->frame_in;
1509 int out = entry->frame_out;
1510 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1511 producer = entry->producer;
1512 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1513 mlt_events_block( properties, properties );
1514 mlt_playlist_remove( self, clip );
1515 mlt_playlist_blank( self, out - in );
1516 mlt_playlist_move( self, self->count - 1, clip );
1517 mlt_events_unblock( properties, properties );
1518 mlt_playlist_virtual_refresh( self );
1519 mlt_producer_set_in_and_out( producer, in, out );
1524 /** Insert blank space.
1526 * \public \memberof mlt_playlist_s
1527 * \param self a playlist
1528 * \param clip the index of the new blank section
1529 * \param length the ending time of the new blank section (duration - 1)
1532 void mlt_playlist_insert_blank( mlt_playlist self, int clip, int length )
1534 if ( self != NULL && length >= 0 )
1536 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1537 mlt_events_block( properties, properties );
1538 mlt_playlist_blank( self, length );
1539 mlt_playlist_move( self, self->count - 1, clip );
1540 mlt_events_unblock( properties, properties );
1541 mlt_playlist_virtual_refresh( self );
1545 /** Resize a blank entry.
1547 * \public \memberof mlt_playlist_s
1548 * \param self a playlist
1549 * \param position the time at which the blank entry exists relative to the start or end (negative) of the playlist.
1550 * \param length the additional amount of blank frames to add
1551 * \param find true to fist locate the blank after the clip at position
1553 void mlt_playlist_pad_blanks( mlt_playlist self, mlt_position position, int length, int find )
1555 if ( self != NULL && length != 0 )
1557 int clip = mlt_playlist_get_clip_index_at( self, position );
1558 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1559 mlt_events_block( properties, properties );
1560 if ( find && clip < self->count && !mlt_playlist_is_blank( self, clip ) )
1562 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1564 mlt_playlist_clip_info info;
1565 mlt_playlist_get_clip_info( self, &info, clip );
1566 if ( info.frame_out + length > info.frame_in )
1567 mlt_playlist_resize_clip( self, clip, info.frame_in, info.frame_out + length );
1569 mlt_playlist_remove( self, clip );
1571 else if ( find && clip < self->count && length > 0 )
1573 mlt_playlist_insert_blank( self, clip, length );
1575 mlt_events_unblock( properties, properties );
1576 mlt_playlist_virtual_refresh( self );
1580 /** Insert a clip at a specific time.
1582 * \public \memberof mlt_playlist_s
1583 * \param self a playlist
1584 * \param position the time at which to insert
1585 * \param producer the producer to insert
1586 * \param mode true if you want to overwrite any blank section
1587 * \return true if there was an error
1590 int mlt_playlist_insert_at( mlt_playlist self, mlt_position position, mlt_producer producer, int mode )
1592 int ret = self == NULL || position < 0 || producer == NULL;
1595 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1596 int length = mlt_producer_get_playtime( producer );
1597 int clip = mlt_playlist_get_clip_index_at( self, position );
1598 mlt_playlist_clip_info info;
1599 mlt_playlist_get_clip_info( self, &info, clip );
1600 mlt_events_block( properties, self );
1601 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1603 // Split and move to new clip if need be
1604 if ( position != info.start && mlt_playlist_split( self, clip, position - info.start - 1 ) == 0 )
1605 mlt_playlist_get_clip_info( self, &info, ++ clip );
1607 // Split again if need be
1608 if ( length < info.frame_count )
1609 mlt_playlist_split( self, clip, length - 1 );
1612 mlt_playlist_remove( self, clip );
1615 mlt_playlist_insert( self, producer, clip, -1, -1 );
1618 else if ( clip < self->count )
1620 if ( position > info.start + info.frame_count / 2 )
1622 if ( mode == 1 && clip < self->count && mlt_playlist_is_blank( self, clip ) )
1624 mlt_playlist_get_clip_info( self, &info, clip );
1625 if ( length < info.frame_count )
1626 mlt_playlist_split( self, clip, length );
1627 mlt_playlist_remove( self, clip );
1629 mlt_playlist_insert( self, producer, clip, -1, -1 );
1635 if ( position == info.start )
1636 mlt_playlist_remove( self, clip );
1638 mlt_playlist_blank( self, position - mlt_properties_get_int( properties, "length" ) - 1 );
1640 mlt_playlist_append( self, producer );
1641 ret = self->count - 1;
1643 mlt_events_unblock( properties, self );
1644 mlt_playlist_virtual_refresh( self );
1653 /** Get the time at which the clip starts relative to the playlist.
1655 * \public \memberof mlt_playlist_s
1656 * \param self a playlist
1657 * \param clip the index of the playlist entry
1658 * \return the starting time
1661 int mlt_playlist_clip_start( mlt_playlist self, int clip )
1663 mlt_playlist_clip_info info;
1664 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1666 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1669 /** Get the playable duration of the clip.
1671 * \public \memberof mlt_playlist_s
1672 * \param self a playlist
1673 * \param clip the index of the playlist entry
1674 * \return the duration of the playlist entry
1677 int mlt_playlist_clip_length( mlt_playlist self, int clip )
1679 mlt_playlist_clip_info info;
1680 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1681 return info.frame_count;
1685 /** Get the duration of a blank space.
1687 * \public \memberof mlt_playlist_s
1688 * \param self a playlist
1689 * \param clip the index of the playlist entry
1690 * \param bounded the maximum number of blank entries or 0 for all
1691 * \return the duration of a blank section
1694 int mlt_playlist_blanks_from( mlt_playlist self, int clip, int bounded )
1697 mlt_playlist_clip_info info;
1698 if ( self != NULL && clip < self->count )
1700 mlt_playlist_get_clip_info( self, &info, clip );
1701 if ( mlt_playlist_is_blank( self, clip ) )
1702 count += info.frame_count;
1704 bounded = self->count;
1705 for ( clip ++; clip < self->count && bounded >= 0; clip ++ )
1707 mlt_playlist_get_clip_info( self, &info, clip );
1708 if ( mlt_playlist_is_blank( self, clip ) )
1709 count += info.frame_count;
1717 /** Remove a portion of the playlist by time.
1719 * \public \memberof mlt_playlist_s
1720 * \param self a playlist
1721 * \param position the starting time
1722 * \param length the duration of time to remove
1723 * \return the new entry index at the position
1726 int mlt_playlist_remove_region( mlt_playlist self, mlt_position position, int length )
1728 int index = mlt_playlist_get_clip_index_at( self, position );
1729 if ( index >= 0 && index < self->count )
1731 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1732 int clip_start = mlt_playlist_clip_start( self, index );
1733 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1734 mlt_events_block( properties, self );
1736 if ( position + length > list_length )
1737 length -= ( position + length - list_length );
1739 if ( clip_start < position )
1741 mlt_playlist_split( self, index ++, position - clip_start - 1 );
1746 if ( mlt_playlist_clip_length( self, index ) > length )
1747 mlt_playlist_split( self, index, length - 1 );
1748 length -= mlt_playlist_clip_length( self, index );
1749 mlt_playlist_remove( self, index );
1752 mlt_playlist_consolidate_blanks( self, 0 );
1753 mlt_events_unblock( properties, self );
1754 mlt_playlist_virtual_refresh( self );
1756 // Just to be sure, we'll get the clip index again...
1757 index = mlt_playlist_get_clip_index_at( self, position );
1764 * \deprecated not implemented
1765 * \public \memberof mlt_playlist_s
1769 * \param new_position
1773 int mlt_playlist_move_region( mlt_playlist self, mlt_position position, int length, int new_position )
1781 /** Get the current frame.
1783 * The implementation of the get_frame virtual function.
1784 * \private \memberof mlt_playlist_s
1785 * \param producer a producer
1786 * \param frame a frame by reference
1787 * \param index the time at which to get the frame
1791 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1793 // Check that we have a producer
1794 if ( producer == NULL )
1800 // Get this mlt_playlist
1801 mlt_playlist self = producer->child;
1803 // Need to ensure the frame is deinterlaced when repeating 1 frame
1804 int progressive = 0;
1806 // Get the real producer
1807 mlt_service real = mlt_playlist_virtual_seek( self, &progressive );
1809 // Check that we have a producer
1812 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1817 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1819 mlt_service_get_frame( real, frame, index );
1823 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1824 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1825 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1826 mlt_frame_push_service( *frame, NULL );
1827 mlt_frame_push_audio( *frame, NULL );
1828 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1829 mlt_service_apply_filters( real, *frame, 0 );
1830 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1831 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1834 // Check if we're at the end of the clip
1835 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1836 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1837 mlt_playlist_virtual_set_out( self );
1839 // Set the consumer progressive property
1842 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1843 mlt_properties_set_int( properties, "test_audio", 1 );
1846 // Check for notifier and call with appropriate argument
1847 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1848 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1849 if ( notifier != NULL )
1851 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1852 notifier( argument );
1855 // Update position on the frame we're creating
1856 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1858 // Position ourselves on the next frame
1859 mlt_producer_prepare_next( producer );
1864 /** Close the playlist.
1866 * \public \memberof mlt_playlist_s
1867 * \param self a playlist
1870 void mlt_playlist_close( mlt_playlist self )
1872 if ( self != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( self ) ) <= 0 )
1875 self->parent.close = NULL;
1876 for ( i = 0; i < self->count; i ++ )
1878 mlt_event_close( self->list[ i ]->event );
1879 mlt_producer_close( self->list[ i ]->producer );
1880 free( self->list[ i ] );
1882 mlt_producer_close( &self->blank );
1883 mlt_producer_close( &self->parent );