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 this, int clip );
55 static int mlt_playlist_resize_mix( mlt_playlist this, 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 this = calloc( sizeof( struct mlt_playlist_s ), 1 );
70 mlt_producer producer = &this->parent;
72 // Construct the producer
73 mlt_producer_init( producer, this );
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 = this;
83 mlt_producer_init( &this->blank, NULL );
84 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "mlt_service", "blank" );
85 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "resource", "blank" );
87 // Indicate that this producer is a playlist
88 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( this ), "playlist", this, 0, NULL, NULL );
90 // Specify the eof condition
91 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "eof", "pause" );
92 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "resource", "<playlist>" );
93 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "mlt_type", "mlt_producer" );
94 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "in", 0 );
95 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "out", -1 );
96 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "length", 0 );
99 this->list = malloc( this->size * sizeof( playlist_entry * ) );
105 /** Get the producer associated to this playlist.
107 * \public \memberof mlt_playlist_s
108 * \param this a playlist
109 * \return the producer interface
110 * \see MLT_PLAYLIST_PRODUCER
113 mlt_producer mlt_playlist_producer( mlt_playlist this )
115 return this != NULL ? &this->parent : NULL;
118 /** Get the service associated to this playlist.
120 * \public \memberof mlt_playlist_s
121 * \param this a playlist
122 * \return the service interface
123 * \see MLT_PLAYLIST_SERVICE
126 mlt_service mlt_playlist_service( mlt_playlist this )
128 return MLT_PRODUCER_SERVICE( &this->parent );
131 /** Get the properties associated to this playlist.
133 * \public \memberof mlt_playlist_s
134 * \param this a playlist
135 * \return the playlist's properties list
136 * \see MLT_PLAYLIST_PROPERTIES
139 mlt_properties mlt_playlist_properties( mlt_playlist this )
141 return MLT_PRODUCER_PROPERTIES( &this->parent );
144 /** Refresh the playlist after a clip has been changed.
146 * \private \memberof mlt_playlist_s
147 * \param this a playlist
151 static int mlt_playlist_virtual_refresh( mlt_playlist this )
153 // Obtain the properties
154 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
156 mlt_position frame_count = 0;
158 for ( i = 0; i < this->count; i ++ )
161 mlt_producer producer = this->list[ i ]->producer;
164 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
166 // Check if the length of the producer has changed
167 if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
168 this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
170 // This clip should be removed...
171 if ( current_length < 1 )
173 this->list[ i ]->frame_in = 0;
174 this->list[ i ]->frame_out = -1;
175 this->list[ i ]->frame_count = 0;
179 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
180 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
181 this->list[ i ]->frame_count = current_length;
184 // Update the producer_length
185 this->list[ i ]->producer_length = current_length;
189 // Calculate the frame_count
190 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
192 // Update the frame_count for this clip
193 frame_count += this->list[ i ]->frame_count;
196 // Refresh all properties
197 mlt_events_block( properties, properties );
198 mlt_properties_set_position( properties, "length", frame_count );
199 mlt_events_unblock( properties, properties );
200 mlt_properties_set_position( properties, "out", frame_count - 1 );
205 /** Listener for producers on the playlist.
207 * Refreshes the playlist whenever an entry receives producer-changed.
208 * \private \memberof mlt_playlist_s
209 * \param producer a producer
210 * \param this a playlist
213 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
215 mlt_playlist_virtual_refresh( this );
218 /** Append to the virtual playlist.
220 * \private \memberof mlt_playlist_s
221 * \param this a playlist
222 * \param source a producer
223 * \param in the producer's starting time
224 * \param out the producer's ending time
225 * \return true if there was an error
228 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
230 mlt_producer producer = NULL;
231 mlt_properties properties = NULL;
232 mlt_properties parent = NULL;
234 // If we have a cut, then use the in/out points from the cut
235 if ( mlt_producer_is_blank( source ) )
237 // Make sure the blank is long enough to accomodate the length specified
238 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
240 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
241 mlt_events_block( blank_props, blank_props );
242 mlt_producer_set_in_and_out( &this->blank, in, out );
243 mlt_events_unblock( blank_props, blank_props );
246 // Now make sure the cut comes from this this->blank
247 if ( source == NULL )
249 producer = mlt_producer_cut( &this->blank, in, out );
251 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
253 producer = mlt_producer_cut( &this->blank, in, out );
258 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
261 properties = MLT_PRODUCER_PROPERTIES( producer );
263 else if ( mlt_producer_is_cut( source ) )
267 in = mlt_producer_get_in( producer );
268 if ( out == -1 || out > mlt_producer_get_out( producer ) )
269 out = mlt_producer_get_out( producer );
270 properties = MLT_PRODUCER_PROPERTIES( producer );
271 mlt_properties_inc_ref( properties );
275 producer = mlt_producer_cut( source, in, out );
276 if ( in == -1 || in < mlt_producer_get_in( producer ) )
277 in = mlt_producer_get_in( producer );
278 if ( out == -1 || out > mlt_producer_get_out( producer ) )
279 out = mlt_producer_get_out( producer );
280 properties = MLT_PRODUCER_PROPERTIES( producer );
283 // Fetch the cuts parent properties
284 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
286 // Remove loader normalisers for fx cuts
287 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
289 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
290 mlt_filter filter = mlt_service_filter( service, 0 );
291 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_loader" ) )
293 mlt_service_detach( service, filter );
294 filter = mlt_service_filter( service, 0 );
296 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
299 // Check that we have room
300 if ( this->count >= this->size )
303 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
304 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
309 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
310 if ( this->list[ this->count ] != NULL )
312 this->list[ this->count ]->producer = producer;
313 this->list[ this->count ]->frame_in = in;
314 this->list[ this->count ]->frame_out = out;
315 this->list[ this->count ]->frame_count = out - in + 1;
316 this->list[ this->count ]->repeat = 1;
317 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
318 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
319 mlt_event_inc_ref( this->list[ this->count ]->event );
320 mlt_properties_set( properties, "eof", "pause" );
321 mlt_producer_set_speed( producer, 0 );
325 return mlt_playlist_virtual_refresh( this );
328 /** Locate a producer by index.
330 * \private \memberof mlt_playlist_s
331 * \param this a playlist
332 * \param[in, out] position the time at which to locate the producer, returns the time relative to the producer's starting point
333 * \param[out] clip the index of the playlist entry
334 * \param[out] total the duration of the playlist up to and including this producer
335 * \return a producer or NULL if not found
338 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
340 // Default producer to NULL
341 mlt_producer producer = NULL;
343 // Loop for each producer until found
344 for ( *clip = 0; *clip < this->count; *clip += 1 )
346 // Increment the total
347 *total += this->list[ *clip ]->frame_count;
349 // Check if the position indicates that we have found the clip
350 // Note that 0 length clips get skipped automatically
351 if ( *position < this->list[ *clip ]->frame_count )
353 // Found it, now break
354 producer = this->list[ *clip ]->producer;
359 // Decrement position by length of this entry
360 *position -= this->list[ *clip ]->frame_count;
367 /** Seek in the virtual playlist.
369 * This gets the producer at the current position and seeks on the producer
370 * while doing repeat and end-of-file handling. This is also responsible for
371 * closing producers previous to the preceding playlist if the autoclose
373 * \private \memberof mlt_playlist_s
374 * \param this a playlist
375 * \param[out] progressive true if the producer should be displayed progressively
376 * \return the service interface of the producer at the play head
377 * \see producer_get_frame
380 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
382 // Map playlist position to real producer in virtual playlist
383 mlt_position position = mlt_producer_frame( &this->parent );
385 // Keep the original position since we change it while iterating through the list
386 mlt_position original = position;
388 // Clip index and total
392 // Locate the producer for the position
393 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
395 // Get the properties
396 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
398 // Automatically close previous producers if requested
399 if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
400 && position < 2 // tolerate off-by-one error on going to next clip
401 && mlt_properties_get_int( properties, "autoclose" ) )
404 // They might have jumped ahead!
405 for ( j = 0; j < i - 1; j++ )
407 mlt_service_lock( MLT_PRODUCER_SERVICE( this->list[ j ]->producer ) );
408 mlt_producer p = this->list[ j ]->producer;
411 this->list[ j ]->producer = NULL;
412 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
413 mlt_producer_close( p );
415 // If p is null, the lock will not have been "taken"
419 // Get the eof handling
420 char *eof = mlt_properties_get( properties, "eof" );
422 // Seek in real producer to relative position
423 if ( producer != NULL )
425 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
426 *progressive = count == 1;
427 mlt_producer_seek( producer, (int)position % count );
429 else if ( !strcmp( eof, "pause" ) && total > 0 )
431 playlist_entry *entry = this->list[ this->count - 1 ];
432 int count = entry->frame_count / entry->repeat;
433 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
434 mlt_producer_seek( this_producer, original - 1 );
435 producer = entry->producer;
436 mlt_producer_seek( producer, (int)entry->frame_out % count );
437 mlt_producer_set_speed( this_producer, 0 );
438 mlt_producer_set_speed( producer, 0 );
439 *progressive = count == 1;
441 else if ( !strcmp( eof, "loop" ) && total > 0 )
443 playlist_entry *entry = this->list[ 0 ];
444 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
445 mlt_producer_seek( this_producer, 0 );
446 producer = entry->producer;
447 mlt_producer_seek( producer, 0 );
451 producer = &this->blank;
454 return MLT_PRODUCER_SERVICE( producer );
457 /** Invoked when a producer indicates that it has prematurely reached its end.
459 * \private \memberof mlt_playlist_s
460 * \param this a playlist
462 * \see producer_get_frame
465 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
467 // Default producer to blank
468 mlt_producer producer = &this->blank;
470 // Map playlist position to real producer in virtual playlist
471 mlt_position position = mlt_producer_frame( &this->parent );
473 // Loop through the virtual playlist
476 for ( i = 0; i < this->count; i ++ )
478 if ( position < this->list[ i ]->frame_count )
480 // Found it, now break
481 producer = this->list[ i ]->producer;
486 // Decrement position by length of this entry
487 position -= this->list[ i ]->frame_count;
491 // Seek in real producer to relative position
492 if ( i < this->count && this->list[ i ]->frame_out != position )
494 // Update the frame_count for the changed clip (hmmm)
495 this->list[ i ]->frame_out = position;
496 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
498 // Refresh the playlist
499 mlt_playlist_virtual_refresh( this );
505 /** Obtain the current clips index.
507 * \public \memberof mlt_playlist_s
508 * \param this a playlist
509 * \return the index of the playlist entry at the current position
512 int mlt_playlist_current_clip( mlt_playlist this )
514 // Map playlist position to real producer in virtual playlist
515 mlt_position position = mlt_producer_frame( &this->parent );
517 // Loop through the virtual playlist
520 for ( i = 0; i < this->count; i ++ )
522 if ( position < this->list[ i ]->frame_count )
524 // Found it, now break
529 // Decrement position by length of this entry
530 position -= this->list[ i ]->frame_count;
537 /** Obtain the current clips producer.
539 * \public \memberof mlt_playlist_s
540 * \param this a playlist
541 * \return the producer at the current position
544 mlt_producer mlt_playlist_current( mlt_playlist this )
546 int i = mlt_playlist_current_clip( this );
547 if ( i < this->count )
548 return this->list[ i ]->producer;
553 /** Get the position which corresponds to the start of the next clip.
555 * \public \memberof mlt_playlist_s
556 * \param this a playlist
557 * \param whence the location from which to make the index relative:
558 * start of playlist, end of playlist, or current position
559 * \param index the playlist entry index relative to whence
560 * \return the time at which the referenced clip starts
563 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
565 mlt_position position = 0;
566 int absolute_clip = index;
569 // Determine the absolute clip
572 case mlt_whence_relative_start:
573 absolute_clip = index;
576 case mlt_whence_relative_current:
577 absolute_clip = mlt_playlist_current_clip( this ) + index;
580 case mlt_whence_relative_end:
581 absolute_clip = this->count - index;
585 // Check that we're in a valid range
586 if ( absolute_clip < 0 )
588 else if ( absolute_clip > this->count )
589 absolute_clip = this->count;
591 // Now determine the position
592 for ( i = 0; i < absolute_clip; i ++ )
593 position += this->list[ i ]->frame_count;
598 /** Get all the info about the clip specified.
600 * \public \memberof mlt_playlist_s
601 * \param this a playlist
602 * \param info a clip info struct
603 * \param index a playlist entry index
604 * \return true if there was an error
607 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
609 int error = index < 0 || index >= this->count || this->list[ index ]->producer == NULL;
610 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
613 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
614 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
616 info->producer = producer;
617 info->cut = this->list[ index ]->producer;
618 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
619 info->resource = mlt_properties_get( properties, "resource" );
620 info->frame_in = this->list[ index ]->frame_in;
621 info->frame_out = this->list[ index ]->frame_out;
622 info->frame_count = this->list[ index ]->frame_count;
623 info->repeat = this->list[ index ]->repeat;
624 info->length = mlt_producer_get_length( producer );
625 info->fps = mlt_producer_get_fps( producer );
631 /** Get number of clips in the playlist.
633 * \public \memberof mlt_playlist_s
634 * \param this a playlist
635 * \return the number of playlist entries
638 int mlt_playlist_count( mlt_playlist this )
643 /** Clear the playlist.
645 * \public \memberof mlt_playlist_s
646 * \param this a playlist
647 * \return true if there was an error
650 int mlt_playlist_clear( mlt_playlist this )
653 for ( i = 0; i < this->count; i ++ )
655 mlt_event_close( this->list[ i ]->event );
656 mlt_producer_close( this->list[ i ]->producer );
659 return mlt_playlist_virtual_refresh( this );
662 /** Append a producer to the playlist.
664 * \public \memberof mlt_playlist_s
665 * \param this a playlist
666 * \param producer the producer to append
667 * \return true if there was an error
670 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
672 // Append to virtual list
673 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
676 /** Append a producer to the playlist with in/out points.
678 * \public \memberof mlt_playlist_s
679 * \param this a playlist
680 * \param producer the producer to append
681 * \param in the starting point on the producer
682 * \param out the ending point on the producer
683 * \return true if there was an error
686 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
688 // Append to virtual list
689 if ( in != -1 && out != -1 )
690 return mlt_playlist_virtual_append( this, producer, in, out );
692 return mlt_playlist_append( this, producer );
695 /** Append a blank to the playlist of a given length.
697 * \public \memberof mlt_playlist_s
698 * \param this a playlist
699 * \param length the ending time of the blank entry, not its duration
700 * \return true if there was an error
703 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
705 // Append to the virtual list
707 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
712 /** Insert a producer into the playlist.
714 * \public \memberof mlt_playlist_s
715 * \param this a playlist
716 * \param producer the producer to insert
717 * \param where the producer's playlist entry index
718 * \param in the starting point on the producer
719 * \param out the ending point on the producer
720 * \return true if there was an error
723 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
726 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
727 mlt_playlist_append_io( this, producer, in, out );
729 // Move to the position specified
730 mlt_playlist_move( this, this->count - 1, where );
731 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
733 return mlt_playlist_virtual_refresh( this );
736 /** Remove an entry in the playlist.
738 * \public \memberof mlt_playlist_s
739 * \param this a playlist
740 * \param where the playlist entry index
741 * \return true if there was an error
744 int mlt_playlist_remove( mlt_playlist this, int where )
746 int error = where < 0 || where >= this->count;
747 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
749 // We need to know the current clip and the position within the playlist
750 int current = mlt_playlist_current_clip( this );
751 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
753 // We need all the details about the clip we're removing
754 mlt_playlist_clip_info where_info;
755 playlist_entry *entry = this->list[ where ];
756 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
762 mlt_playlist_get_clip_info( this, &where_info, where );
764 // Make sure the clip to be removed is valid and correct if necessary
767 if ( where >= this->count )
768 where = this->count - 1;
770 // Reorganise the list
771 for ( i = where + 1; i < this->count; i ++ )
772 this->list[ i - 1 ] = this->list[ i ];
775 if ( entry->preservation_hack == 0 )
777 // Decouple from mix_in/out if necessary
778 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
780 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
781 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
783 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
785 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
786 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
789 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
790 mlt_producer_clear( entry->producer );
793 // Close the producer associated to the clip info
794 mlt_event_close( entry->event );
795 mlt_producer_close( entry->producer );
798 if ( where == current )
799 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
800 else if ( where < current && this->count > 0 )
801 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
802 else if ( this->count == 0 )
803 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
808 // Refresh the playlist
809 mlt_playlist_virtual_refresh( this );
815 /** Move an entry in the playlist.
817 * \public \memberof mlt_playlist_s
818 * \param this a playlist
819 * \param src an entry index
820 * \param dest an entry index
824 int mlt_playlist_move( mlt_playlist this, int src, int dest )
828 /* We need to ensure that the requested indexes are valid and correct it as necessary */
831 if ( src >= this->count )
832 src = this->count - 1;
836 if ( dest >= this->count )
837 dest = this->count - 1;
839 if ( src != dest && this->count > 1 )
841 int current = mlt_playlist_current_clip( this );
842 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
843 playlist_entry *src_entry = NULL;
845 // We need all the details about the current clip
846 mlt_playlist_clip_info current_info;
848 mlt_playlist_get_clip_info( this, ¤t_info, current );
849 position -= current_info.start;
851 if ( current == src )
853 else if ( current > src && current < dest )
855 else if ( current == dest )
858 src_entry = this->list[ src ];
861 for ( i = src; i > dest; i -- )
862 this->list[ i ] = this->list[ i - 1 ];
866 for ( i = src; i < dest; i ++ )
867 this->list[ i ] = this->list[ i + 1 ];
869 this->list[ dest ] = src_entry;
871 mlt_playlist_get_clip_info( this, ¤t_info, current );
872 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
873 mlt_playlist_virtual_refresh( this );
879 /** Repeat the specified clip n times.
881 * \public \memberof mlt_playlist_s
882 * \param this a playlist
883 * \param clip a playlist entry index
884 * \param repeat the number of times to repeat the clip
885 * \return true if there was an error
888 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
890 int error = repeat < 1 || clip < 0 || clip >= this->count;
893 playlist_entry *entry = this->list[ clip ];
894 entry->repeat = repeat;
895 mlt_playlist_virtual_refresh( this );
900 /** Resize the specified clip.
902 * \public \memberof mlt_playlist_s
903 * \param this a playlist
904 * \param clip the index of the playlist entry
905 * \param in the new starting time on the clip's producer
906 * \param out the new ending time on the clip's producer
907 * \return true if there was an error
910 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
912 int error = clip < 0 || clip >= this->count;
913 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
915 playlist_entry *entry = this->list[ clip ];
916 mlt_producer producer = entry->producer;
917 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
919 mlt_events_block( properties, properties );
921 if ( mlt_producer_is_blank( producer ) )
923 // Make sure the blank is long enough to accomodate the length specified
924 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
926 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
927 mlt_properties_set_int( blank_props, "length", out - in + 1 );
928 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
929 mlt_producer_set_in_and_out( &this->blank, 0, out - in );
935 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
936 out = mlt_producer_get_length( producer ) - 1;
945 mlt_producer_set_in_and_out( producer, in, out );
946 mlt_events_unblock( properties, properties );
947 mlt_playlist_virtual_refresh( this );
952 /** Split a clip on the playlist at the given position.
954 * This splits after the specified frame.
955 * \public \memberof mlt_playlist_s
956 * \param this a playlist
957 * \param clip the index of the playlist entry
958 * \param position the time at which to split relative to the beginning of the clip or its end if negative
959 * \return true if there was an error
962 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
964 int error = clip < 0 || clip >= this->count;
967 playlist_entry *entry = this->list[ clip ];
968 position = position < 0 ? entry->frame_count + position - 1 : position;
969 if ( position >= 0 && position < entry->frame_count - 1 )
971 int in = entry->frame_in;
972 int out = entry->frame_out;
973 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
974 mlt_playlist_resize_clip( this, clip, in, in + position );
975 if ( !mlt_producer_is_blank( entry->producer ) )
978 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
979 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
980 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
981 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
982 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
984 char *name = mlt_properties_get_name( entry_properties, i );
985 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
986 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
988 mlt_producer_close( split );
992 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
994 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
995 mlt_playlist_virtual_refresh( this );
1005 /** Split the playlist at the absolute position.
1007 * \public \memberof mlt_playlist_s
1008 * \param this a playlist
1009 * \param position the time at which to split relative to the beginning of the clip
1010 * \param left true to split before the frame starting at position
1011 * \return true if there was an error
1014 int mlt_playlist_split_at( mlt_playlist this, mlt_position position, int left )
1016 int result = this == NULL ? -1 : 0;
1019 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) ) )
1021 int clip = mlt_playlist_get_clip_index_at( this, position );
1022 mlt_playlist_clip_info info;
1023 mlt_playlist_get_clip_info( this, &info, clip );
1024 if ( left && position != info.start )
1025 mlt_playlist_split( this, clip, position - info.start - 1 );
1027 mlt_playlist_split( this, clip, position - info.start );
1030 else if ( position <= 0 )
1036 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1042 /** Join 1 or more consecutive clips.
1044 * \public \memberof mlt_playlist_s
1045 * \param this a playlist
1046 * \param clip the starting playlist entry index
1047 * \param count the number of entries to merge
1048 * \param merge ignored
1049 * \return true if there was an error
1052 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
1054 int error = clip < 0 || clip >= this->count;
1058 mlt_playlist new_clip = mlt_playlist_init( );
1059 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1060 if ( clip + count >= this->count )
1061 count = this->count - clip - 1;
1062 for ( i = 0; i <= count; i ++ )
1064 playlist_entry *entry = this->list[ clip ];
1065 mlt_playlist_append( new_clip, entry->producer );
1066 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
1067 entry->preservation_hack = 1;
1068 mlt_playlist_remove( this, clip );
1070 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1071 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
1072 mlt_playlist_close( new_clip );
1077 /** Mix consecutive clips for a specified length and apply transition if specified.
1079 * \public \memberof mlt_playlist_s
1080 * \param this a playlist
1081 * \param clip the index of the playlist entry
1082 * \param length the number of frames over which to create the mix
1083 * \param transition the transition to use for the mix
1084 * \return true if there was an error
1087 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
1089 int error = ( clip < 0 || clip + 1 >= this->count );
1092 playlist_entry *clip_a = this->list[ clip ];
1093 playlist_entry *clip_b = this->list[ clip + 1 ];
1094 mlt_producer track_a = NULL;
1095 mlt_producer track_b = NULL;
1096 mlt_tractor tractor = mlt_tractor_new( );
1097 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1099 // Check length is valid for both clips and resize if necessary.
1100 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
1101 length = length > max_size ? max_size : length;
1103 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
1104 if ( length != clip_a->frame_count )
1105 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
1107 track_a = clip_a->producer;
1109 if ( length != clip_b->frame_count )
1110 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
1112 track_b = clip_b->producer;
1114 // Set the tracks on the tractor
1115 mlt_tractor_set_track( tractor, track_a, 0 );
1116 mlt_tractor_set_track( tractor, track_b, 1 );
1118 // Insert the mix object into the playlist
1119 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
1120 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
1122 // Attach the transition
1123 if ( transition != NULL )
1125 mlt_field field = mlt_tractor_field( tractor );
1126 mlt_field_plant_transition( field, transition, 0, 1 );
1127 mlt_transition_set_in_and_out( transition, 0, length - 1 );
1130 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
1131 if ( track_a != clip_a->producer )
1132 mlt_producer_close( track_a );
1133 if ( track_b != clip_b->producer )
1134 mlt_producer_close( track_b );
1136 // Check if we have anything left on the right hand clip
1137 if ( track_b == clip_b->producer )
1139 clip_b->preservation_hack = 1;
1140 mlt_playlist_remove( this, clip + 2 );
1142 else if ( clip_b->frame_out - clip_b->frame_in > length )
1144 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
1145 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
1146 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
1150 mlt_producer_clear( clip_b->producer );
1151 mlt_playlist_remove( this, clip + 2 );
1154 // Check if we have anything left on the left hand clip
1155 if ( track_a == clip_a->producer )
1157 clip_a->preservation_hack = 1;
1158 mlt_playlist_remove( this, clip );
1160 else if ( clip_a->frame_out - clip_a->frame_in > length )
1162 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
1163 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
1164 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
1168 mlt_producer_clear( clip_a->producer );
1169 mlt_playlist_remove( this, clip );
1172 // Unblock and force a fire off of change events to listeners
1173 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1174 mlt_playlist_virtual_refresh( this );
1175 mlt_tractor_close( tractor );
1180 /** Add a transition to an existing mix.
1182 * \public \memberof mlt_playlist_s
1183 * \param this a playlist
1184 * \param clip the index of the playlist entry
1185 * \param transition a transition
1186 * \return true if there was an error
1189 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
1191 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1192 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1193 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1194 int error = transition == NULL || tractor == NULL;
1197 mlt_field field = mlt_tractor_field( tractor );
1198 mlt_field_plant_transition( field, transition, 0, 1 );
1199 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
1204 /** Return the clip at the clip index.
1206 * \public \memberof mlt_playlist_s
1207 * \param this a playlist
1208 * \param clip the index of a playlist entry
1209 * \return a producer or NULL if there was an error
1212 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
1214 if ( clip >= 0 && clip < this->count )
1215 return this->list[ clip ]->producer;
1219 /** Return the clip at the specified position.
1221 * \public \memberof mlt_playlist_s
1222 * \param this a playlist
1223 * \param position a time relative to the beginning of the playlist
1224 * \return a producer or NULL if not found
1227 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, mlt_position position )
1229 int index = 0, total = 0;
1230 return mlt_playlist_locate( this, &position, &index, &total );
1233 /** Return the clip index of the specified position.
1235 * \public \memberof mlt_playlist_s
1236 * \param this a playlist
1237 * \param position a time relative to the beginning of the playlist
1238 * \return the index of the playlist entry
1241 int mlt_playlist_get_clip_index_at( mlt_playlist this, mlt_position position )
1243 int index = 0, total = 0;
1244 mlt_playlist_locate( this, &position, &index, &total );
1248 /** Determine if the clip is a mix.
1250 * \public \memberof mlt_playlist_s
1251 * \param this a playlist
1252 * \param clip the index of the playlist entry
1253 * \return true if the producer is a mix
1256 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1258 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1259 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1260 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1261 return tractor != NULL;
1264 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1265 * back correctly on to the playlist.
1267 * \private \memberof mlt_playlist_s
1268 * \param this a playlist
1269 * \param clip the index of the playlist entry
1270 * \return true if there was an error
1273 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1275 int error = ( clip < 0 || clip >= this->count );
1277 // Ensure that the clip request is actually a mix
1280 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1281 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1282 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1283 this->list[ clip ]->preservation_hack;
1288 playlist_entry *mix = this->list[ clip ];
1289 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1290 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1291 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1292 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1293 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1294 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1296 if ( clip_a != NULL )
1298 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1302 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1303 mlt_playlist_insert( this, cut, clip, -1, -1 );
1307 if ( clip_b != NULL )
1309 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1313 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1314 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1317 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1318 mlt_playlist_remove( this, clip );
1319 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1320 mlt_playlist_virtual_refresh( this );
1325 /** Resize a mix clip.
1327 * \private \memberof mlt_playlist_s
1328 * \param this a playlist
1329 * \param clip the index of the playlist entry
1330 * \param in the new starting point
1331 * \param out the new ending point
1332 * \return true if there was an error
1335 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1337 int error = ( clip < 0 || clip >= this->count );
1339 // Ensure that the clip request is actually a mix
1342 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1343 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1344 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1349 playlist_entry *mix = this->list[ clip ];
1350 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1351 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1352 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1353 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1354 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1355 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1356 int length = out - in + 1;
1357 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1358 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1360 if ( clip_a != NULL )
1361 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1363 if ( clip_b != NULL )
1364 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1366 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1367 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1368 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1369 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1370 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1371 mlt_producer_set_in_and_out( mix->producer, in, out );
1373 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1374 mlt_playlist_virtual_refresh( this );
1379 /** Consolidate adjacent blank producers.
1381 * \public \memberof mlt_playlist_s
1382 * \param this a playlist
1383 * \param keep_length set false to remove the last entry if it is blank
1386 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1391 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1393 mlt_events_block( properties, properties );
1394 for ( i = 1; i < this->count; i ++ )
1396 playlist_entry *left = this->list[ i - 1 ];
1397 playlist_entry *right = this->list[ i ];
1399 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1401 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1402 mlt_playlist_remove( this, i -- );
1406 if ( !keep_length && this->count > 0 )
1408 playlist_entry *last = this->list[ this->count - 1 ];
1409 if ( mlt_producer_is_blank( last->producer ) )
1410 mlt_playlist_remove( this, this->count - 1 );
1413 mlt_events_unblock( properties, properties );
1414 mlt_playlist_virtual_refresh( this );
1418 /** Determine if the specified clip index is a blank.
1420 * \public \memberof mlt_playlist_s
1421 * \param this a playlist
1422 * \param clip the index of the playlist entry
1423 * \return true if there was an error
1426 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1428 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1431 /** Determine if the specified position is a blank.
1433 * \public \memberof mlt_playlist_s
1434 * \param this a playlist
1435 * \param position a time relative to the start or end (negative) of the playlist
1436 * \return true if there was an error
1439 int mlt_playlist_is_blank_at( mlt_playlist this, mlt_position position )
1441 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( this, position ) );
1444 /** Replace the specified clip with a blank and return the clip.
1446 * \public \memberof mlt_playlist_s
1447 * \param this a playlist
1448 * \param clip the index of the playlist entry
1449 * \return a producer or NULL if there was an error
1452 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1454 mlt_producer producer = NULL;
1455 if ( !mlt_playlist_is_blank( this, clip ) )
1457 playlist_entry *entry = this->list[ clip ];
1458 int in = entry->frame_in;
1459 int out = entry->frame_out;
1460 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1461 producer = entry->producer;
1462 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1463 mlt_events_block( properties, properties );
1464 mlt_playlist_remove( this, clip );
1465 mlt_playlist_blank( this, out - in );
1466 mlt_playlist_move( this, this->count - 1, clip );
1467 mlt_events_unblock( properties, properties );
1468 mlt_playlist_virtual_refresh( this );
1469 mlt_producer_set_in_and_out( producer, in, out );
1474 /** Insert blank space.
1476 * \public \memberof mlt_playlist_s
1477 * \param this a playlist
1478 * \param clip the index of the new blank section
1479 * \param length the ending time of the new blank section (duration - 1)
1482 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1484 if ( this != NULL && length >= 0 )
1486 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1487 mlt_events_block( properties, properties );
1488 mlt_playlist_blank( this, length );
1489 mlt_playlist_move( this, this->count - 1, clip );
1490 mlt_events_unblock( properties, properties );
1491 mlt_playlist_virtual_refresh( this );
1495 /** Resize a blank entry.
1497 * \public \memberof mlt_playlist_s
1498 * \param this a playlist
1499 * \param position the time at which the blank entry exists relative to the start or end (negative) of the playlist.
1500 * \param length the additional amount of blank frames to add
1501 * \param find true to fist locate the blank after the clip at position
1503 void mlt_playlist_pad_blanks( mlt_playlist this, mlt_position position, int length, int find )
1505 if ( this != NULL && length != 0 )
1507 int clip = mlt_playlist_get_clip_index_at( this, position );
1508 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1509 mlt_events_block( properties, properties );
1510 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1512 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1514 mlt_playlist_clip_info info;
1515 mlt_playlist_get_clip_info( this, &info, clip );
1516 if ( info.frame_out + length > info.frame_in )
1517 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1519 mlt_playlist_remove( this, clip );
1521 else if ( find && clip < this->count && length > 0 )
1523 mlt_playlist_insert_blank( this, clip, length );
1525 mlt_events_unblock( properties, properties );
1526 mlt_playlist_virtual_refresh( this );
1530 /** Insert a clip at a specific time.
1532 * \public \memberof mlt_playlist_s
1533 * \param this a playlist
1534 * \param position the time at which to insert
1535 * \param producer the producer to insert
1536 * \param mode true if you want to overwrite any blank section
1537 * \return true if there was an error
1540 int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_producer producer, int mode )
1542 int ret = this == NULL || position < 0 || producer == NULL;
1545 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1546 int length = mlt_producer_get_playtime( producer );
1547 int clip = mlt_playlist_get_clip_index_at( this, position );
1548 mlt_playlist_clip_info info;
1549 mlt_playlist_get_clip_info( this, &info, clip );
1550 mlt_events_block( properties, this );
1551 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1553 // Split and move to new clip if need be
1554 if ( position != info.start && mlt_playlist_split( this, clip, position - info.start - 1 ) == 0 )
1555 mlt_playlist_get_clip_info( this, &info, ++ clip );
1557 // Split again if need be
1558 if ( length < info.frame_count )
1559 mlt_playlist_split( this, clip, length - 1 );
1562 mlt_playlist_remove( this, clip );
1565 mlt_playlist_insert( this, producer, clip, -1, -1 );
1568 else if ( clip < this->count )
1570 if ( position > info.start + info.frame_count / 2 )
1572 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1574 mlt_playlist_get_clip_info( this, &info, clip );
1575 if ( length < info.frame_count )
1576 mlt_playlist_split( this, clip, length );
1577 mlt_playlist_remove( this, clip );
1579 mlt_playlist_insert( this, producer, clip, -1, -1 );
1585 if ( position == info.start )
1586 mlt_playlist_remove( this, clip );
1588 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) - 1 );
1590 mlt_playlist_append( this, producer );
1591 ret = this->count - 1;
1593 mlt_events_unblock( properties, this );
1594 mlt_playlist_virtual_refresh( this );
1603 /** Get the time at which the clip starts relative to the playlist.
1605 * \public \memberof mlt_playlist_s
1606 * \param this a playlist
1607 * \param clip the index of the playlist entry
1608 * \return the starting time
1611 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1613 mlt_playlist_clip_info info;
1614 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1616 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1619 /** Get the playable duration of the clip.
1621 * \public \memberof mlt_playlist_s
1622 * \param this a playlist
1623 * \param clip the index of the playlist entry
1624 * \return the duration of the playlist entry
1627 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1629 mlt_playlist_clip_info info;
1630 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1631 return info.frame_count;
1635 /** Get the duration of a blank space.
1637 * \public \memberof mlt_playlist_s
1638 * \param this a playlist
1639 * \param clip the index of the playlist entry
1640 * \param bounded the maximum number of blank entries or 0 for all
1641 * \return the duration of a blank section
1644 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1647 mlt_playlist_clip_info info;
1648 if ( this != NULL && clip < this->count )
1650 mlt_playlist_get_clip_info( this, &info, clip );
1651 if ( mlt_playlist_is_blank( this, clip ) )
1652 count += info.frame_count;
1654 bounded = this->count;
1655 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1657 mlt_playlist_get_clip_info( this, &info, clip );
1658 if ( mlt_playlist_is_blank( this, clip ) )
1659 count += info.frame_count;
1667 /** Remove a portion of the playlist by time.
1669 * \public \memberof mlt_playlist_s
1670 * \param this a playlist
1671 * \param position the starting time
1672 * \param length the duration of time to remove
1673 * \return the new entry index at the position
1676 int mlt_playlist_remove_region( mlt_playlist this, mlt_position position, int length )
1678 int index = mlt_playlist_get_clip_index_at( this, position );
1679 if ( index >= 0 && index < this->count )
1681 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1682 int clip_start = mlt_playlist_clip_start( this, index );
1683 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1684 mlt_events_block( properties, this );
1686 if ( position + length > list_length )
1687 length -= ( position + length - list_length );
1689 if ( clip_start < position )
1691 mlt_playlist_split( this, index ++, position - clip_start - 1 );
1696 if ( mlt_playlist_clip_length( this, index ) > length )
1697 mlt_playlist_split( this, index, length - 1 );
1698 length -= mlt_playlist_clip_length( this, index );
1699 mlt_playlist_remove( this, index );
1702 mlt_playlist_consolidate_blanks( this, 0 );
1703 mlt_events_unblock( properties, this );
1704 mlt_playlist_virtual_refresh( this );
1706 // Just to be sure, we'll get the clip index again...
1707 index = mlt_playlist_get_clip_index_at( this, position );
1714 * \deprecated not implemented
1715 * \public \memberof mlt_playlist_s
1719 * \param new_position
1723 int mlt_playlist_move_region( mlt_playlist this, mlt_position position, int length, int new_position )
1731 /** Get the current frame.
1733 * The implementation of the get_frame virtual function.
1734 * \private \memberof mlt_playlist_s
1735 * \param producer a producer
1736 * \param frame a frame by reference
1737 * \param index the time at which to get the frame
1741 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1743 // Check that we have a producer
1744 if ( producer == NULL )
1750 // Get this mlt_playlist
1751 mlt_playlist this = producer->child;
1753 // Need to ensure the frame is deinterlaced when repeating 1 frame
1754 int progressive = 0;
1756 // Get the real producer
1757 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1759 // Check that we have a producer
1762 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1767 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1769 mlt_service_get_frame( real, frame, index );
1773 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1774 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1775 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1776 mlt_frame_push_service( *frame, NULL );
1777 mlt_frame_push_audio( *frame, NULL );
1778 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1779 mlt_service_apply_filters( real, *frame, 0 );
1780 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1781 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1784 // Check if we're at the end of the clip
1785 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1786 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1787 mlt_playlist_virtual_set_out( this );
1789 // Set the consumer progressive property
1792 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1793 mlt_properties_set_int( properties, "test_audio", 1 );
1796 // Check for notifier and call with appropriate argument
1797 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1798 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1799 if ( notifier != NULL )
1801 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1802 notifier( argument );
1805 // Update position on the frame we're creating
1806 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1808 // Position ourselves on the next frame
1809 mlt_producer_prepare_next( producer );
1814 /** Close the playlist.
1816 * \public \memberof mlt_playlist_s
1817 * \param this a playlist
1820 void mlt_playlist_close( mlt_playlist this )
1822 if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1825 this->parent.close = NULL;
1826 for ( i = 0; i < this->count; i ++ )
1828 mlt_event_close( this->list[ i ]->event );
1829 mlt_producer_close( this->list[ i ]->producer );
1830 free( this->list[ i ] );
1832 mlt_producer_close( &this->blank );
1833 mlt_producer_close( &this->parent );