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( 1, sizeof( struct mlt_playlist_s ) );
70 mlt_producer producer = &self->parent;
72 // Construct the producer
73 if ( mlt_producer_init( producer, self ) != 0 ) goto error1;
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 if ( mlt_producer_init( &self->blank, NULL ) != 0 ) goto error1;
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 = calloc( self->size, sizeof( playlist_entry * ) );
100 if ( self->list == NULL ) goto error2;
112 /** Construct a playlist with a profile.
114 * Sets the resource property to "<playlist>".
115 * Set the mlt_type to property to "mlt_producer".
116 * \public \memberof mlt_playlist_s
117 * \param profile the profile to use with the profile
118 * \return a new playlist
121 mlt_playlist mlt_playlist_new( mlt_profile profile )
123 mlt_playlist self = mlt_playlist_init();
125 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "_profile", profile, 0, NULL, NULL );
129 /** Get the producer associated to this playlist.
131 * \public \memberof mlt_playlist_s
132 * \param self a playlist
133 * \return the producer interface
134 * \see MLT_PLAYLIST_PRODUCER
137 mlt_producer mlt_playlist_producer( mlt_playlist self )
139 return self != NULL ? &self->parent : NULL;
142 /** Get the service associated to this playlist.
144 * \public \memberof mlt_playlist_s
145 * \param self a playlist
146 * \return the service interface
147 * \see MLT_PLAYLIST_SERVICE
150 mlt_service mlt_playlist_service( mlt_playlist self )
152 return MLT_PRODUCER_SERVICE( &self->parent );
155 /** Get the properties associated to this playlist.
157 * \public \memberof mlt_playlist_s
158 * \param self a playlist
159 * \return the playlist's properties list
160 * \see MLT_PLAYLIST_PROPERTIES
163 mlt_properties mlt_playlist_properties( mlt_playlist self )
165 return MLT_PRODUCER_PROPERTIES( &self->parent );
168 /** Refresh the playlist after a clip has been changed.
170 * \private \memberof mlt_playlist_s
171 * \param self a playlist
175 static int mlt_playlist_virtual_refresh( mlt_playlist self )
177 // Obtain the properties
178 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
180 mlt_position frame_count = 0;
182 for ( i = 0; i < self->count; i ++ )
185 mlt_producer producer = self->list[ i ]->producer;
188 int current_length = mlt_producer_get_playtime( producer );
190 // Check if the length of the producer has changed
191 if ( self->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
192 self->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
194 // This clip should be removed...
195 if ( current_length < 1 )
197 self->list[ i ]->frame_in = 0;
198 self->list[ i ]->frame_out = -1;
199 self->list[ i ]->frame_count = 0;
203 self->list[ i ]->frame_in = mlt_producer_get_in( producer );
204 self->list[ i ]->frame_out = mlt_producer_get_out( producer );
205 self->list[ i ]->frame_count = current_length;
208 // Update the producer_length
209 self->list[ i ]->producer_length = current_length;
213 // Calculate the frame_count
214 self->list[ i ]->frame_count = ( self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1 ) * self->list[ i ]->repeat;
216 // Update the frame_count for self clip
217 frame_count += self->list[ i ]->frame_count;
220 // Refresh all properties
221 mlt_events_block( properties, properties );
222 mlt_properties_set_position( properties, "length", frame_count );
223 mlt_events_unblock( properties, properties );
224 mlt_properties_set_position( properties, "out", frame_count - 1 );
229 /** Listener for producers on the playlist.
231 * Refreshes the playlist whenever an entry receives producer-changed.
232 * \private \memberof mlt_playlist_s
233 * \param producer a producer
234 * \param self a playlist
237 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist self )
239 mlt_playlist_virtual_refresh( self );
242 /** Append to the virtual playlist.
244 * \private \memberof mlt_playlist_s
245 * \param self a playlist
246 * \param source a producer
247 * \param in the producer's starting time
248 * \param out the producer's ending time
249 * \return true if there was an error
252 static int mlt_playlist_virtual_append( mlt_playlist self, mlt_producer source, mlt_position in, mlt_position out )
254 mlt_producer producer = NULL;
255 mlt_properties properties = NULL;
256 mlt_properties parent = NULL;
258 // If we have a cut, then use the in/out points from the cut
259 if ( mlt_producer_is_blank( source ) )
261 mlt_position length = out - in + 1;
263 // Make sure the blank is long enough to accomodate the length specified
264 if ( length > mlt_producer_get_length( &self->blank ) )
266 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
267 mlt_events_block( blank_props, blank_props );
268 mlt_producer_set_in_and_out( &self->blank, in, out );
269 mlt_events_unblock( blank_props, blank_props );
272 // Now make sure the cut comes from this self->blank
273 if ( source == NULL )
275 producer = mlt_producer_cut( &self->blank, in, out );
277 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &self->blank )
279 producer = mlt_producer_cut( &self->blank, in, out );
284 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
287 properties = MLT_PRODUCER_PROPERTIES( producer );
289 // Make sure this cut of blank is long enough
290 if ( length > mlt_producer_get_length( producer ) )
291 mlt_properties_set_int( properties, "length", length );
293 else if ( mlt_producer_is_cut( source ) )
297 in = mlt_producer_get_in( producer );
298 if ( out < 0 || out > mlt_producer_get_out( producer ) )
299 out = mlt_producer_get_out( producer );
300 properties = MLT_PRODUCER_PROPERTIES( producer );
301 mlt_properties_inc_ref( properties );
305 producer = mlt_producer_cut( source, in, out );
306 if ( in < 0 || in < mlt_producer_get_in( producer ) )
307 in = mlt_producer_get_in( producer );
308 if ( out < 0 || out > mlt_producer_get_out( producer ) )
309 out = mlt_producer_get_out( producer );
310 properties = MLT_PRODUCER_PROPERTIES( producer );
313 // Fetch the cuts parent properties
314 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
316 // Remove loader normalisers for fx cuts
317 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
319 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
320 mlt_filter filter = mlt_service_filter( service, 0 );
321 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_loader" ) )
323 mlt_service_detach( service, filter );
324 filter = mlt_service_filter( service, 0 );
326 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
329 // Check that we have room
330 if ( self->count >= self->size )
333 self->list = realloc( self->list, ( self->size + 10 ) * sizeof( playlist_entry * ) );
334 for ( i = self->size; i < self->size + 10; i ++ ) self->list[ i ] = NULL;
339 self->list[ self->count ] = calloc( 1, sizeof( playlist_entry ) );
340 if ( self->list[ self->count ] != NULL )
342 self->list[ self->count ]->producer = producer;
343 self->list[ self->count ]->frame_in = in;
344 self->list[ self->count ]->frame_out = out;
345 self->list[ self->count ]->frame_count = out - in + 1;
346 self->list[ self->count ]->repeat = 1;
347 self->list[ self->count ]->producer_length = mlt_producer_get_playtime( producer );
348 self->list[ self->count ]->event = mlt_events_listen( parent, self, "producer-changed", ( mlt_listener )mlt_playlist_listener );
349 mlt_event_inc_ref( self->list[ self->count ]->event );
350 mlt_properties_set( properties, "eof", "pause" );
351 mlt_producer_set_speed( producer, 0 );
355 return mlt_playlist_virtual_refresh( self );
358 /** Locate a producer by index.
360 * \private \memberof mlt_playlist_s
361 * \param self a playlist
362 * \param[in, out] position the time at which to locate the producer, returns the time relative to the producer's starting point
363 * \param[out] clip the index of the playlist entry
364 * \param[out] total the duration of the playlist up to and including this producer
365 * \return a producer or NULL if not found
368 static mlt_producer mlt_playlist_locate( mlt_playlist self, mlt_position *position, int *clip, int *total )
370 // Default producer to NULL
371 mlt_producer producer = NULL;
373 // Loop for each producer until found
374 for ( *clip = 0; *clip < self->count; *clip += 1 )
376 // Increment the total
377 *total += self->list[ *clip ]->frame_count;
379 // Check if the position indicates that we have found the clip
380 // Note that 0 length clips get skipped automatically
381 if ( *position < self->list[ *clip ]->frame_count )
383 // Found it, now break
384 producer = self->list[ *clip ]->producer;
389 // Decrement position by length of self entry
390 *position -= self->list[ *clip ]->frame_count;
397 /** Seek in the virtual playlist.
399 * This gets the producer at the current position and seeks on the producer
400 * while doing repeat and end-of-file handling. This is also responsible for
401 * closing producers previous to the preceding playlist if the autoclose
403 * \private \memberof mlt_playlist_s
404 * \param self a playlist
405 * \param[out] progressive true if the producer should be displayed progressively
406 * \return the service interface of the producer at the play head
407 * \see producer_get_frame
410 static mlt_service mlt_playlist_virtual_seek( mlt_playlist self, int *progressive )
412 // Map playlist position to real producer in virtual playlist
413 mlt_position position = mlt_producer_frame( &self->parent );
415 // Keep the original position since we change it while iterating through the list
416 mlt_position original = position;
418 // Clip index and total
422 // Locate the producer for the position
423 mlt_producer producer = mlt_playlist_locate( self, &position, &i, &total );
425 // Get the properties
426 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
428 // Automatically close previous producers if requested
429 if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
430 && position < 2 // tolerate off-by-one error on going to next clip
431 && mlt_properties_get_int( properties, "autoclose" ) )
434 // They might have jumped ahead!
435 for ( j = 0; j < i - 1; j++ )
437 mlt_service_lock( MLT_PRODUCER_SERVICE( self->list[ j ]->producer ) );
438 mlt_producer p = self->list[ j ]->producer;
441 self->list[ j ]->producer = NULL;
442 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
443 mlt_producer_close( p );
445 // If p is null, the lock will not have been "taken"
449 // Get the eof handling
450 char *eof = mlt_properties_get( properties, "eof" );
452 // Seek in real producer to relative position
453 if ( producer != NULL )
455 int count = self->list[ i ]->frame_count / self->list[ i ]->repeat;
456 *progressive = count == 1;
457 mlt_producer_seek( producer, (int)position % count );
459 else if ( !strcmp( eof, "pause" ) && total > 0 )
461 playlist_entry *entry = self->list[ self->count - 1 ];
462 int count = entry->frame_count / entry->repeat;
463 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
464 mlt_producer_seek( self_producer, original - 1 );
465 producer = entry->producer;
466 mlt_producer_seek( producer, (int)entry->frame_out % count );
467 mlt_producer_set_speed( self_producer, 0 );
468 mlt_producer_set_speed( producer, 0 );
469 *progressive = count == 1;
471 else if ( !strcmp( eof, "loop" ) && total > 0 )
473 playlist_entry *entry = self->list[ 0 ];
474 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
475 mlt_producer_seek( self_producer, 0 );
476 producer = entry->producer;
477 mlt_producer_seek( producer, 0 );
481 producer = &self->blank;
484 return MLT_PRODUCER_SERVICE( producer );
487 /** Invoked when a producer indicates that it has prematurely reached its end.
489 * \private \memberof mlt_playlist_s
490 * \param self a playlist
492 * \see producer_get_frame
495 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist self )
497 // Default producer to blank
498 mlt_producer producer = &self->blank;
500 // Map playlist position to real producer in virtual playlist
501 mlt_position position = mlt_producer_frame( &self->parent );
503 // Loop through the virtual playlist
506 for ( i = 0; i < self->count; i ++ )
508 if ( position < self->list[ i ]->frame_count )
510 // Found it, now break
511 producer = self->list[ i ]->producer;
516 // Decrement position by length of this entry
517 position -= self->list[ i ]->frame_count;
521 // Seek in real producer to relative position
522 if ( i < self->count && self->list[ i ]->frame_out != position )
524 // Update the frame_count for the changed clip (hmmm)
525 self->list[ i ]->frame_out = position;
526 self->list[ i ]->frame_count = self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1;
528 // Refresh the playlist
529 mlt_playlist_virtual_refresh( self );
535 /** Obtain the current clips index.
537 * \public \memberof mlt_playlist_s
538 * \param self a playlist
539 * \return the index of the playlist entry at the current position
542 int mlt_playlist_current_clip( mlt_playlist self )
544 // Map playlist position to real producer in virtual playlist
545 mlt_position position = mlt_producer_frame( &self->parent );
547 // Loop through the virtual playlist
550 for ( i = 0; i < self->count; i ++ )
552 if ( position < self->list[ i ]->frame_count )
554 // Found it, now break
559 // Decrement position by length of this entry
560 position -= self->list[ i ]->frame_count;
567 /** Obtain the current clips producer.
569 * \public \memberof mlt_playlist_s
570 * \param self a playlist
571 * \return the producer at the current position
574 mlt_producer mlt_playlist_current( mlt_playlist self )
576 int i = mlt_playlist_current_clip( self );
577 if ( i < self->count )
578 return self->list[ i ]->producer;
583 /** Get the position which corresponds to the start of the next clip.
585 * \public \memberof mlt_playlist_s
586 * \param self a playlist
587 * \param whence the location from which to make the index relative:
588 * start of playlist, end of playlist, or current position
589 * \param index the playlist entry index relative to whence
590 * \return the time at which the referenced clip starts
593 mlt_position mlt_playlist_clip( mlt_playlist self, mlt_whence whence, int index )
595 mlt_position position = 0;
596 int absolute_clip = index;
599 // Determine the absolute clip
602 case mlt_whence_relative_start:
603 absolute_clip = index;
606 case mlt_whence_relative_current:
607 absolute_clip = mlt_playlist_current_clip( self ) + index;
610 case mlt_whence_relative_end:
611 absolute_clip = self->count - index;
615 // Check that we're in a valid range
616 if ( absolute_clip < 0 )
618 else if ( absolute_clip > self->count )
619 absolute_clip = self->count;
621 // Now determine the position
622 for ( i = 0; i < absolute_clip; i ++ )
623 position += self->list[ i ]->frame_count;
628 /** Get all the info about the clip specified.
630 * \public \memberof mlt_playlist_s
631 * \param self a playlist
632 * \param info a clip info struct
633 * \param index a playlist entry index
634 * \return true if there was an error
637 int mlt_playlist_get_clip_info( mlt_playlist self, mlt_playlist_clip_info *info, int index )
639 int error = index < 0 || index >= self->count || self->list[ index ]->producer == NULL;
640 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
643 mlt_producer producer = mlt_producer_cut_parent( self->list[ index ]->producer );
644 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
646 info->producer = producer;
647 info->cut = self->list[ index ]->producer;
648 info->start = mlt_playlist_clip( self, mlt_whence_relative_start, index );
649 info->resource = mlt_properties_get( properties, "resource" );
650 info->frame_in = self->list[ index ]->frame_in;
651 info->frame_out = self->list[ index ]->frame_out;
652 info->frame_count = self->list[ index ]->frame_count;
653 info->repeat = self->list[ index ]->repeat;
654 info->length = mlt_producer_get_length( producer );
655 info->fps = mlt_producer_get_fps( producer );
661 /** Get number of clips in the playlist.
663 * \public \memberof mlt_playlist_s
664 * \param self a playlist
665 * \return the number of playlist entries
668 int mlt_playlist_count( mlt_playlist self )
673 /** Clear the playlist.
675 * \public \memberof mlt_playlist_s
676 * \param self a playlist
677 * \return true if there was an error
680 int mlt_playlist_clear( mlt_playlist self )
683 for ( i = 0; i < self->count; i ++ )
685 mlt_event_close( self->list[ i ]->event );
686 mlt_producer_close( self->list[ i ]->producer );
689 return mlt_playlist_virtual_refresh( self );
692 /** Append a producer to the playlist.
694 * \public \memberof mlt_playlist_s
695 * \param self a playlist
696 * \param producer the producer to append
697 * \return true if there was an error
700 int mlt_playlist_append( mlt_playlist self, mlt_producer producer )
702 // Append to virtual list
703 return mlt_playlist_virtual_append( self, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
706 /** Append a producer to the playlist with in/out points.
708 * \public \memberof mlt_playlist_s
709 * \param self a playlist
710 * \param producer the producer to append
711 * \param in the starting point on the producer; a negative value is the same as 0
712 * \param out the ending point on the producer; a negative value is the same as producer length - 1
713 * \return true if there was an error
716 int mlt_playlist_append_io( mlt_playlist self, mlt_producer producer, mlt_position in, mlt_position out )
718 // Append to virtual list
719 if ( in < 0 && out < 0 )
720 return mlt_playlist_append( self, producer );
722 return mlt_playlist_virtual_append( self, producer, in, out );
725 /** Append a blank to the playlist of a given length.
727 * \public \memberof mlt_playlist_s
728 * \param self a playlist
729 * \param out the ending time of the blank entry, not its duration
730 * \return true if there was an error
733 int mlt_playlist_blank( mlt_playlist self, mlt_position out )
735 // Append to the virtual list
737 return mlt_playlist_virtual_append( self, &self->blank, 0, out );
742 /** Append a blank item to the playlist with duration as a time string.
744 * \public \memberof mlt_playlist_s
745 * \param self a playlist
746 * \param length the duration of the blank entry as a time string
747 * \return true if there was an error
750 int mlt_playlist_blank_time( mlt_playlist self, const char* length )
752 if ( self && length )
754 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
755 mlt_properties_set( properties , "_blank_time", length );
756 mlt_position duration = mlt_properties_get_position( properties, "_blank_time" );
757 return mlt_playlist_blank( self, duration - 1 );
763 /** Insert a producer into the playlist.
765 * \public \memberof mlt_playlist_s
766 * \param self a playlist
767 * \param producer the producer to insert
768 * \param where the producer's playlist entry index
769 * \param in the starting point on the producer
770 * \param out the ending point on the producer
771 * \return true if there was an error
774 int mlt_playlist_insert( mlt_playlist self, mlt_producer producer, int where, mlt_position in, mlt_position out )
777 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
778 mlt_playlist_append_io( self, producer, in, out );
780 // Move to the position specified
781 mlt_playlist_move( self, self->count - 1, where );
782 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
784 return mlt_playlist_virtual_refresh( self );
787 /** Remove an entry in the playlist.
789 * \public \memberof mlt_playlist_s
790 * \param self a playlist
791 * \param where the playlist entry index
792 * \return true if there was an error
795 int mlt_playlist_remove( mlt_playlist self, int where )
797 int error = where < 0 || where >= self->count;
798 if ( error == 0 && mlt_playlist_unmix( self, where ) != 0 )
800 // We need to know the current clip and the position within the playlist
801 int current = mlt_playlist_current_clip( self );
802 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
804 // We need all the details about the clip we're removing
805 mlt_playlist_clip_info where_info;
806 playlist_entry *entry = self->list[ where ];
807 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
813 mlt_playlist_get_clip_info( self, &where_info, where );
815 // Reorganise the list
816 for ( i = where + 1; i < self->count; i ++ )
817 self->list[ i - 1 ] = self->list[ i ];
820 if ( entry->preservation_hack == 0 )
822 // Decouple from mix_in/out if necessary
823 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
825 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
826 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
828 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
830 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
831 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
834 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
835 mlt_producer_clear( entry->producer );
838 // Close the producer associated to the clip info
839 mlt_event_close( entry->event );
840 mlt_producer_close( entry->producer );
843 if ( where == current )
844 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), where_info.start );
845 else if ( where < current && self->count > 0 )
846 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), position - where_info.frame_count );
847 else if ( self->count == 0 )
848 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), 0 );
853 // Refresh the playlist
854 mlt_playlist_virtual_refresh( self );
860 /** Move an entry in the playlist.
862 * \public \memberof mlt_playlist_s
863 * \param self a playlist
864 * \param src an entry index
865 * \param dest an entry index
869 int mlt_playlist_move( mlt_playlist self, int src, int dest )
873 /* We need to ensure that the requested indexes are valid and correct it as necessary */
876 if ( src >= self->count )
877 src = self->count - 1;
881 if ( dest >= self->count )
882 dest = self->count - 1;
884 if ( src != dest && self->count > 1 )
886 int current = mlt_playlist_current_clip( self );
887 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
888 playlist_entry *src_entry = NULL;
890 // We need all the details about the current clip
891 mlt_playlist_clip_info current_info;
893 mlt_playlist_get_clip_info( self, ¤t_info, current );
894 position -= current_info.start;
896 if ( current == src )
898 else if ( current > src && current < dest )
900 else if ( current == dest )
903 src_entry = self->list[ src ];
906 for ( i = src; i > dest; i -- )
907 self->list[ i ] = self->list[ i - 1 ];
911 for ( i = src; i < dest; i ++ )
912 self->list[ i ] = self->list[ i + 1 ];
914 self->list[ dest ] = src_entry;
916 mlt_playlist_get_clip_info( self, ¤t_info, current );
917 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), current_info.start + position );
918 mlt_playlist_virtual_refresh( self );
924 /** Repeat the specified clip n times.
926 * \public \memberof mlt_playlist_s
927 * \param self a playlist
928 * \param clip a playlist entry index
929 * \param repeat the number of times to repeat the clip
930 * \return true if there was an error
933 int mlt_playlist_repeat_clip( mlt_playlist self, int clip, int repeat )
935 int error = repeat < 1 || clip < 0 || clip >= self->count;
938 playlist_entry *entry = self->list[ clip ];
939 entry->repeat = repeat;
940 mlt_playlist_virtual_refresh( self );
945 /** Resize the specified clip.
947 * \public \memberof mlt_playlist_s
948 * \param self a playlist
949 * \param clip the index of the playlist entry
950 * \param in the new starting time on the clip's producer; a negative value is the same as 0
951 * \param out the new ending time on the clip's producer; a negative value is the same as length - 1
952 * \return true if there was an error
955 int mlt_playlist_resize_clip( mlt_playlist self, int clip, mlt_position in, mlt_position out )
957 int error = clip < 0 || clip >= self->count;
958 if ( error == 0 && mlt_playlist_resize_mix( self, clip, in, out ) != 0 )
960 playlist_entry *entry = self->list[ clip ];
961 mlt_producer producer = entry->producer;
962 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
964 mlt_events_block( properties, properties );
966 if ( mlt_producer_is_blank( producer ) )
968 mlt_position length = out - in + 1;
970 // Make sure the parent blank is long enough to accomodate the length specified
971 if ( length > mlt_producer_get_length( &self->blank ) )
973 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
974 mlt_properties_set_int( blank_props, "length", length );
975 mlt_producer_set_in_and_out( &self->blank, 0, out - in );
977 // Make sure this cut of blank is long enough
978 if ( length > mlt_producer_get_length( producer ) )
979 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", length );
984 if ( out < 0 || out >= mlt_producer_get_length( producer ) )
985 out = mlt_producer_get_length( producer ) - 1;
994 mlt_producer_set_in_and_out( producer, in, out );
995 mlt_events_unblock( properties, properties );
996 mlt_playlist_virtual_refresh( self );
1001 /** Split a clip on the playlist at the given position.
1003 * This splits after the specified frame.
1004 * \public \memberof mlt_playlist_s
1005 * \param self a playlist
1006 * \param clip the index of the playlist entry
1007 * \param position the time at which to split relative to the beginning of the clip or its end if negative
1008 * \return true if there was an error
1011 int mlt_playlist_split( mlt_playlist self, int clip, mlt_position position )
1013 int error = clip < 0 || clip >= self->count;
1016 playlist_entry *entry = self->list[ clip ];
1017 position = position < 0 ? entry->frame_count + position - 1 : position;
1018 if ( position >= 0 && position < entry->frame_count - 1 )
1020 int in = entry->frame_in;
1021 int out = entry->frame_out;
1022 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1023 mlt_playlist_resize_clip( self, clip, in, in + position );
1024 if ( !mlt_producer_is_blank( entry->producer ) )
1027 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
1028 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
1029 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
1030 mlt_playlist_insert( self, split, clip + 1, 0, -1 );
1031 mlt_properties_lock( entry_properties );
1032 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
1034 char *name = mlt_properties_get_name( entry_properties, i );
1035 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
1036 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
1038 mlt_properties_unlock( entry_properties );
1039 mlt_producer_close( split );
1043 mlt_playlist_insert( self, &self->blank, clip + 1, 0, out - position - 1 );
1045 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1046 mlt_playlist_virtual_refresh( self );
1056 /** Split the playlist at the absolute position.
1058 * \public \memberof mlt_playlist_s
1059 * \param self a playlist
1060 * \param position the time at which to split relative to the beginning of the clip
1061 * \param left true to split before the frame starting at position
1062 * \return true if there was an error
1065 int mlt_playlist_split_at( mlt_playlist self, mlt_position position, int left )
1067 int result = self == NULL ? -1 : 0;
1070 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) ) )
1072 int clip = mlt_playlist_get_clip_index_at( self, position );
1073 mlt_playlist_clip_info info;
1074 mlt_playlist_get_clip_info( self, &info, clip );
1075 if ( left && position != info.start )
1076 mlt_playlist_split( self, clip, position - info.start - 1 );
1078 mlt_playlist_split( self, clip, position - info.start );
1081 else if ( position <= 0 )
1087 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1093 /** Join 1 or more consecutive clips.
1095 * \public \memberof mlt_playlist_s
1096 * \param self a playlist
1097 * \param clip the starting playlist entry index
1098 * \param count the number of entries to merge
1099 * \param merge ignored
1100 * \return true if there was an error
1103 int mlt_playlist_join( mlt_playlist self, int clip, int count, int merge )
1105 int error = clip < 0 || clip >= self->count;
1109 mlt_playlist new_clip = mlt_playlist_new( mlt_service_profile( MLT_PLAYLIST_SERVICE(self) ) );
1110 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1111 if ( clip + count >= self->count )
1112 count = self->count - clip - 1;
1113 for ( i = 0; i <= count; i ++ )
1115 playlist_entry *entry = self->list[ clip ];
1116 mlt_playlist_append( new_clip, entry->producer );
1117 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
1118 entry->preservation_hack = 1;
1119 mlt_playlist_remove( self, clip );
1121 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1122 mlt_playlist_insert( self, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
1123 mlt_playlist_close( new_clip );
1128 /** Mix consecutive clips for a specified length and apply transition if specified.
1130 * \public \memberof mlt_playlist_s
1131 * \param self a playlist
1132 * \param clip the index of the playlist entry
1133 * \param length the number of frames over which to create the mix
1134 * \param transition the transition to use for the mix
1135 * \return true if there was an error
1138 int mlt_playlist_mix( mlt_playlist self, int clip, int length, mlt_transition transition )
1140 int error = ( clip < 0 || clip + 1 >= self->count );
1143 playlist_entry *clip_a = self->list[ clip ];
1144 playlist_entry *clip_b = self->list[ clip + 1 ];
1145 mlt_producer track_a = NULL;
1146 mlt_producer track_b = NULL;
1147 mlt_tractor tractor = mlt_tractor_new( );
1148 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1150 // Check length is valid for both clips and resize if necessary.
1151 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
1152 length = length > max_size ? max_size : length;
1154 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
1155 if ( length != clip_a->frame_count )
1156 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
1158 track_a = clip_a->producer;
1160 if ( length != clip_b->frame_count )
1161 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
1163 track_b = clip_b->producer;
1165 // Set the tracks on the tractor
1166 mlt_tractor_set_track( tractor, track_a, 0 );
1167 mlt_tractor_set_track( tractor, track_b, 1 );
1169 // Insert the mix object into the playlist
1170 mlt_playlist_insert( self, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
1171 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
1173 // Attach the transition
1174 if ( transition != NULL )
1176 mlt_field field = mlt_tractor_field( tractor );
1177 mlt_field_plant_transition( field, transition, 0, 1 );
1178 mlt_transition_set_in_and_out( transition, 0, length - 1 );
1181 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
1182 if ( track_a != clip_a->producer )
1183 mlt_producer_close( track_a );
1184 if ( track_b != clip_b->producer )
1185 mlt_producer_close( track_b );
1187 // Check if we have anything left on the right hand clip
1188 if ( track_b == clip_b->producer )
1190 clip_b->preservation_hack = 1;
1191 mlt_playlist_remove( self, clip + 2 );
1193 else if ( clip_b->frame_out - clip_b->frame_in > length )
1195 mlt_playlist_resize_clip( self, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
1196 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
1197 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
1201 mlt_producer_clear( clip_b->producer );
1202 mlt_playlist_remove( self, clip + 2 );
1205 // Check if we have anything left on the left hand clip
1206 if ( track_a == clip_a->producer )
1208 clip_a->preservation_hack = 1;
1209 mlt_playlist_remove( self, clip );
1211 else if ( clip_a->frame_out - clip_a->frame_in > length )
1213 mlt_playlist_resize_clip( self, clip, clip_a->frame_in, clip_a->frame_out - length );
1214 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
1215 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
1219 mlt_producer_clear( clip_a->producer );
1220 mlt_playlist_remove( self, clip );
1223 // Unblock and force a fire off of change events to listeners
1224 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1225 mlt_playlist_virtual_refresh( self );
1226 mlt_tractor_close( tractor );
1231 /** Add a transition to an existing mix.
1233 * \public \memberof mlt_playlist_s
1234 * \param self a playlist
1235 * \param clip the index of the playlist entry
1236 * \param transition a transition
1237 * \return true if there was an error
1240 int mlt_playlist_mix_add( mlt_playlist self, int clip, mlt_transition transition )
1242 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1243 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1244 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1245 int error = transition == NULL || tractor == NULL;
1248 mlt_field field = mlt_tractor_field( tractor );
1249 mlt_field_plant_transition( field, transition, 0, 1 );
1250 mlt_transition_set_in_and_out( transition, 0, self->list[ clip ]->frame_count - 1 );
1255 /** Return the clip at the clip index.
1257 * \public \memberof mlt_playlist_s
1258 * \param self a playlist
1259 * \param clip the index of a playlist entry
1260 * \return a producer or NULL if there was an error
1263 mlt_producer mlt_playlist_get_clip( mlt_playlist self, int clip )
1265 if ( clip >= 0 && clip < self->count )
1266 return self->list[ clip ]->producer;
1270 /** Return the clip at the specified position.
1272 * \public \memberof mlt_playlist_s
1273 * \param self a playlist
1274 * \param position a time relative to the beginning of the playlist
1275 * \return a producer or NULL if not found
1278 mlt_producer mlt_playlist_get_clip_at( mlt_playlist self, mlt_position position )
1280 int index = 0, total = 0;
1281 return mlt_playlist_locate( self, &position, &index, &total );
1284 /** Return the clip index of the specified position.
1286 * \public \memberof mlt_playlist_s
1287 * \param self a playlist
1288 * \param position a time relative to the beginning of the playlist
1289 * \return the index of the playlist entry
1292 int mlt_playlist_get_clip_index_at( mlt_playlist self, mlt_position position )
1294 int index = 0, total = 0;
1295 mlt_playlist_locate( self, &position, &index, &total );
1299 /** Determine if the clip is a mix.
1301 * \public \memberof mlt_playlist_s
1302 * \param self a playlist
1303 * \param clip the index of the playlist entry
1304 * \return true if the producer is a mix
1307 int mlt_playlist_clip_is_mix( mlt_playlist self, int clip )
1309 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1310 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1311 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1312 return tractor != NULL;
1315 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1316 * back correctly on to the playlist.
1318 * \private \memberof mlt_playlist_s
1319 * \param self a playlist
1320 * \param clip the index of the playlist entry
1321 * \return true if there was an error
1324 static int mlt_playlist_unmix( mlt_playlist self, int clip )
1326 int error = ( clip < 0 || clip >= self->count );
1328 // Ensure that the clip request is actually a mix
1331 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1332 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1333 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1334 self->list[ clip ]->preservation_hack;
1339 playlist_entry *mix = self->list[ clip ];
1340 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1341 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1342 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1343 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1344 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1345 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1347 if ( clip_a != NULL )
1349 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1353 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1354 mlt_playlist_insert( self, cut, clip, -1, -1 );
1358 if ( clip_b != NULL )
1360 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1364 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1365 mlt_playlist_insert( self, cut, clip + 1, -1, -1 );
1368 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1369 mlt_playlist_remove( self, clip );
1370 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1371 mlt_playlist_virtual_refresh( self );
1376 /** Resize a mix clip.
1378 * \private \memberof mlt_playlist_s
1379 * \param self a playlist
1380 * \param clip the index of the playlist entry
1381 * \param in the new starting point
1382 * \param out the new ending point
1383 * \return true if there was an error
1386 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out )
1388 int error = ( clip < 0 || clip >= self->count );
1390 // Ensure that the clip request is actually a mix
1393 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1394 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1395 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1400 playlist_entry *mix = self->list[ clip ];
1401 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1402 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1403 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1404 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1405 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1406 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1407 int length = out - in + 1;
1408 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1409 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1411 if ( clip_a != NULL )
1412 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1414 if ( clip_b != NULL )
1415 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1417 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1418 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1419 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1420 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1421 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1422 mlt_producer_set_in_and_out( mix->producer, in, out );
1424 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1425 mlt_playlist_virtual_refresh( self );
1430 /** Consolidate adjacent blank producers.
1432 * \public \memberof mlt_playlist_s
1433 * \param self a playlist
1434 * \param keep_length set false to remove the last entry if it is blank
1437 void mlt_playlist_consolidate_blanks( mlt_playlist self, int keep_length )
1442 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1444 mlt_events_block( properties, properties );
1445 for ( i = 1; i < self->count; i ++ )
1447 playlist_entry *left = self->list[ i - 1 ];
1448 playlist_entry *right = self->list[ i ];
1450 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1452 mlt_playlist_resize_clip( self, i - 1, 0, left->frame_count + right->frame_count - 1 );
1453 mlt_playlist_remove( self, i -- );
1457 if ( !keep_length && self->count > 0 )
1459 playlist_entry *last = self->list[ self->count - 1 ];
1460 if ( mlt_producer_is_blank( last->producer ) )
1461 mlt_playlist_remove( self, self->count - 1 );
1464 mlt_events_unblock( properties, properties );
1465 mlt_playlist_virtual_refresh( self );
1469 /** Determine if the specified clip index is a blank.
1471 * \public \memberof mlt_playlist_s
1472 * \param self a playlist
1473 * \param clip the index of the playlist entry
1474 * \return true if there was an error
1477 int mlt_playlist_is_blank( mlt_playlist self, int clip )
1479 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( self, clip ) );
1482 /** Determine if the specified position is a blank.
1484 * \public \memberof mlt_playlist_s
1485 * \param self a playlist
1486 * \param position a time relative to the start or end (negative) of the playlist
1487 * \return true if there was an error
1490 int mlt_playlist_is_blank_at( mlt_playlist self, mlt_position position )
1492 return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( self, position ) );
1495 /** Replace the specified clip with a blank and return the clip.
1497 * \public \memberof mlt_playlist_s
1498 * \param self a playlist
1499 * \param clip the index of the playlist entry
1500 * \return a producer or NULL if there was an error
1503 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist self, int clip )
1505 mlt_producer producer = NULL;
1506 if ( !mlt_playlist_is_blank( self, clip ) )
1508 playlist_entry *entry = self->list[ clip ];
1509 int in = entry->frame_in;
1510 int out = entry->frame_out;
1511 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1512 producer = entry->producer;
1513 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1514 mlt_events_block( properties, properties );
1515 mlt_playlist_remove( self, clip );
1516 mlt_playlist_blank( self, out - in );
1517 mlt_playlist_move( self, self->count - 1, clip );
1518 mlt_events_unblock( properties, properties );
1519 mlt_playlist_virtual_refresh( self );
1520 mlt_producer_set_in_and_out( producer, in, out );
1525 /** Insert blank space.
1527 * \public \memberof mlt_playlist_s
1528 * \param self a playlist
1529 * \param clip the index of the new blank section
1530 * \param length the ending time of the new blank section (duration - 1)
1533 void mlt_playlist_insert_blank( mlt_playlist self, int clip, int length )
1535 if ( self != NULL && length >= 0 )
1537 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1538 mlt_events_block( properties, properties );
1539 mlt_playlist_blank( self, length );
1540 mlt_playlist_move( self, self->count - 1, clip );
1541 mlt_events_unblock( properties, properties );
1542 mlt_playlist_virtual_refresh( self );
1546 /** Resize a blank entry.
1548 * \public \memberof mlt_playlist_s
1549 * \param self a playlist
1550 * \param position the time at which the blank entry exists relative to the start or end (negative) of the playlist.
1551 * \param length the additional amount of blank frames to add
1552 * \param find true to fist locate the blank after the clip at position
1554 void mlt_playlist_pad_blanks( mlt_playlist self, mlt_position position, int length, int find )
1556 if ( self != NULL && length != 0 )
1558 int clip = mlt_playlist_get_clip_index_at( self, position );
1559 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1560 mlt_events_block( properties, properties );
1561 if ( find && clip < self->count && !mlt_playlist_is_blank( self, clip ) )
1563 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1565 mlt_playlist_clip_info info;
1566 mlt_playlist_get_clip_info( self, &info, clip );
1567 if ( info.frame_out + length > info.frame_in )
1568 mlt_playlist_resize_clip( self, clip, info.frame_in, info.frame_out + length );
1570 mlt_playlist_remove( self, clip );
1572 else if ( find && clip < self->count && length > 0 )
1574 mlt_playlist_insert_blank( self, clip, length );
1576 mlt_events_unblock( properties, properties );
1577 mlt_playlist_virtual_refresh( self );
1581 /** Insert a clip at a specific time.
1583 * \public \memberof mlt_playlist_s
1584 * \param self a playlist
1585 * \param position the time at which to insert
1586 * \param producer the producer to insert
1587 * \param mode true if you want to overwrite any blank section
1588 * \return true if there was an error
1591 int mlt_playlist_insert_at( mlt_playlist self, mlt_position position, mlt_producer producer, int mode )
1593 int ret = self == NULL || position < 0 || producer == NULL;
1596 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1597 int length = mlt_producer_get_playtime( producer );
1598 int clip = mlt_playlist_get_clip_index_at( self, position );
1599 mlt_playlist_clip_info info;
1600 mlt_playlist_get_clip_info( self, &info, clip );
1601 mlt_events_block( properties, self );
1602 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1604 // Split and move to new clip if need be
1605 if ( position != info.start && mlt_playlist_split( self, clip, position - info.start - 1 ) == 0 )
1606 mlt_playlist_get_clip_info( self, &info, ++ clip );
1608 // Split again if need be
1609 if ( length < info.frame_count )
1610 mlt_playlist_split( self, clip, length - 1 );
1613 mlt_playlist_remove( self, clip );
1616 mlt_playlist_insert( self, producer, clip, -1, -1 );
1619 else if ( clip < self->count )
1621 if ( position > info.start + info.frame_count / 2 )
1623 if ( mode == 1 && clip < self->count && mlt_playlist_is_blank( self, clip ) )
1625 mlt_playlist_get_clip_info( self, &info, clip );
1626 if ( length < info.frame_count )
1627 mlt_playlist_split( self, clip, length );
1628 mlt_playlist_remove( self, clip );
1630 mlt_playlist_insert( self, producer, clip, -1, -1 );
1636 if ( position == info.start )
1637 mlt_playlist_remove( self, clip );
1639 mlt_playlist_blank( self, position - mlt_properties_get_int( properties, "length" ) - 1 );
1641 mlt_playlist_append( self, producer );
1642 ret = self->count - 1;
1644 mlt_events_unblock( properties, self );
1645 mlt_playlist_virtual_refresh( self );
1654 /** Get the time at which the clip starts relative to the playlist.
1656 * \public \memberof mlt_playlist_s
1657 * \param self a playlist
1658 * \param clip the index of the playlist entry
1659 * \return the starting time
1662 int mlt_playlist_clip_start( mlt_playlist self, int clip )
1664 mlt_playlist_clip_info info;
1665 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1667 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1670 /** Get the playable duration of the clip.
1672 * \public \memberof mlt_playlist_s
1673 * \param self a playlist
1674 * \param clip the index of the playlist entry
1675 * \return the duration of the playlist entry
1678 int mlt_playlist_clip_length( mlt_playlist self, int clip )
1680 mlt_playlist_clip_info info;
1681 if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1682 return info.frame_count;
1686 /** Get the duration of a blank space.
1688 * \public \memberof mlt_playlist_s
1689 * \param self a playlist
1690 * \param clip the index of the playlist entry
1691 * \param bounded the maximum number of blank entries or 0 for all
1692 * \return the duration of a blank section
1695 int mlt_playlist_blanks_from( mlt_playlist self, int clip, int bounded )
1698 mlt_playlist_clip_info info;
1699 if ( self != NULL && clip < self->count )
1701 mlt_playlist_get_clip_info( self, &info, clip );
1702 if ( mlt_playlist_is_blank( self, clip ) )
1703 count += info.frame_count;
1705 bounded = self->count;
1706 for ( clip ++; clip < self->count && bounded >= 0; clip ++ )
1708 mlt_playlist_get_clip_info( self, &info, clip );
1709 if ( mlt_playlist_is_blank( self, clip ) )
1710 count += info.frame_count;
1718 /** Remove a portion of the playlist by time.
1720 * \public \memberof mlt_playlist_s
1721 * \param self a playlist
1722 * \param position the starting time
1723 * \param length the duration of time to remove
1724 * \return the new entry index at the position
1727 int mlt_playlist_remove_region( mlt_playlist self, mlt_position position, int length )
1729 int index = mlt_playlist_get_clip_index_at( self, position );
1730 if ( index >= 0 && index < self->count )
1732 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1733 int clip_start = mlt_playlist_clip_start( self, index );
1734 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1735 mlt_events_block( properties, self );
1737 if ( position + length > list_length )
1738 length -= ( position + length - list_length );
1740 if ( clip_start < position )
1742 mlt_playlist_split( self, index ++, position - clip_start - 1 );
1747 if ( mlt_playlist_clip_length( self, index ) > length )
1748 mlt_playlist_split( self, index, length - 1 );
1749 length -= mlt_playlist_clip_length( self, index );
1750 mlt_playlist_remove( self, index );
1753 mlt_playlist_consolidate_blanks( self, 0 );
1754 mlt_events_unblock( properties, self );
1755 mlt_playlist_virtual_refresh( self );
1757 // Just to be sure, we'll get the clip index again...
1758 index = mlt_playlist_get_clip_index_at( self, position );
1765 * \deprecated not implemented
1766 * \public \memberof mlt_playlist_s
1770 * \param new_position
1774 int mlt_playlist_move_region( mlt_playlist self, mlt_position position, int length, int new_position )
1782 /** Get the current frame.
1784 * The implementation of the get_frame virtual function.
1785 * \private \memberof mlt_playlist_s
1786 * \param producer a producer
1787 * \param frame a frame by reference
1788 * \param index the time at which to get the frame
1792 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1794 // Check that we have a producer
1795 if ( producer == NULL )
1801 // Get this mlt_playlist
1802 mlt_playlist self = producer->child;
1804 // Need to ensure the frame is deinterlaced when repeating 1 frame
1805 int progressive = 0;
1807 // Get the real producer
1808 mlt_service real = mlt_playlist_virtual_seek( self, &progressive );
1810 // Check that we have a producer
1813 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1818 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1820 mlt_service_get_frame( real, frame, index );
1824 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1825 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1826 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1827 mlt_frame_push_service( *frame, NULL );
1828 mlt_frame_push_audio( *frame, NULL );
1829 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1830 mlt_service_apply_filters( real, *frame, 0 );
1831 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1832 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1835 // Check if we're at the end of the clip
1836 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1837 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1838 mlt_playlist_virtual_set_out( self );
1840 // Set the consumer progressive property
1843 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1844 mlt_properties_set_int( properties, "test_audio", 1 );
1847 // Check for notifier and call with appropriate argument
1848 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1849 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1850 if ( notifier != NULL )
1852 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1853 notifier( argument );
1856 // Update position on the frame we're creating
1857 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1859 // Position ourselves on the next frame
1860 mlt_producer_prepare_next( producer );
1865 /** Close the playlist.
1867 * \public \memberof mlt_playlist_s
1868 * \param self a playlist
1871 void mlt_playlist_close( mlt_playlist self )
1873 if ( self != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( self ) ) <= 0 )
1876 self->parent.close = NULL;
1877 for ( i = 0; i < self->count; i ++ )
1879 mlt_event_close( self->list[ i ]->event );
1880 mlt_producer_close( self->list[ i ]->producer );
1881 free( self->list[ i ] );
1883 mlt_producer_close( &self->blank );
1884 mlt_producer_close( &self->parent );