2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "mlt_playlist.h"
24 #include "mlt_tractor.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
27 #include "mlt_frame.h"
28 #include "mlt_transition.h"
34 /** Virtual playlist entry.
37 struct playlist_entry_s
39 mlt_producer producer;
40 mlt_position frame_in;
41 mlt_position frame_out;
42 mlt_position frame_count;
44 mlt_position producer_length;
46 int preservation_hack;
49 /** Forward declarations
52 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
53 static int mlt_playlist_unmix( mlt_playlist this, int clip );
54 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out );
59 mlt_playlist mlt_playlist_init( )
61 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
64 mlt_producer producer = &this->parent;
66 // Construct the producer
67 mlt_producer_init( producer, this );
69 // Override the producer get_frame
70 producer->get_frame = producer_get_frame;
72 // Define the destructor
73 producer->close = ( mlt_destructor )mlt_playlist_close;
74 producer->close_object = this;
77 mlt_producer_init( &this->blank, NULL );
78 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "mlt_service", "blank" );
79 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "resource", "blank" );
81 // Indicate that this producer is a playlist
82 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( this ), "playlist", this, 0, NULL, NULL );
84 // Specify the eof condition
85 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "eof", "pause" );
86 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "resource", "<playlist>" );
87 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "mlt_type", "mlt_producer" );
88 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "in", 0 );
89 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "out", -1 );
90 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "length", 0 );
93 this->list = malloc( this->size * sizeof( playlist_entry * ) );
99 /** Get the producer associated to this playlist.
102 mlt_producer mlt_playlist_producer( mlt_playlist this )
104 return this != NULL ? &this->parent : NULL;
107 /** Get the service associated to this playlist.
110 mlt_service mlt_playlist_service( mlt_playlist this )
112 return MLT_PRODUCER_SERVICE( &this->parent );
115 /** Get the propertues associated to this playlist.
118 mlt_properties mlt_playlist_properties( mlt_playlist this )
120 return MLT_PRODUCER_PROPERTIES( &this->parent );
123 /** Refresh the playlist after a clip has been changed.
126 static int mlt_playlist_virtual_refresh( mlt_playlist this )
128 // Obtain the properties
129 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
131 mlt_position frame_count = 0;
133 for ( i = 0; i < this->count; i ++ )
136 mlt_producer producer = this->list[ i ]->producer;
137 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
139 // Check if the length of the producer has changed
140 if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
141 this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
143 // This clip should be removed...
144 if ( current_length < 1 )
146 this->list[ i ]->frame_in = 0;
147 this->list[ i ]->frame_out = -1;
148 this->list[ i ]->frame_count = 0;
152 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
153 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
154 this->list[ i ]->frame_count = current_length;
157 // Update the producer_length
158 this->list[ i ]->producer_length = current_length;
161 // Calculate the frame_count
162 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
164 // Update the frame_count for this clip
165 frame_count += this->list[ i ]->frame_count;
168 // Refresh all properties
169 mlt_events_block( properties, properties );
170 mlt_properties_set_position( properties, "length", frame_count );
171 mlt_events_unblock( properties, properties );
172 mlt_properties_set_position( properties, "out", frame_count - 1 );
177 /** Listener for producers on the playlist.
180 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
182 mlt_playlist_virtual_refresh( this );
185 /** Append to the virtual playlist.
188 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
190 mlt_producer producer = NULL;
191 mlt_properties properties = NULL;
192 mlt_properties parent = NULL;
194 // If we have a cut, then use the in/out points from the cut
195 if ( mlt_producer_is_blank( source ) )
197 // Make sure the blank is long enough to accomodate the length specified
198 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
200 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
201 mlt_events_block( blank_props, blank_props );
202 mlt_producer_set_in_and_out( &this->blank, in, out );
203 mlt_events_unblock( blank_props, blank_props );
206 // Now make sure the cut comes from this this->blank
207 if ( source == NULL )
209 producer = mlt_producer_cut( &this->blank, in, out );
211 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
213 producer = mlt_producer_cut( &this->blank, in, out );
218 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
221 properties = MLT_PRODUCER_PROPERTIES( producer );
223 else if ( mlt_producer_is_cut( source ) )
227 in = mlt_producer_get_in( producer );
228 if ( out == -1 || out > mlt_producer_get_out( producer ) )
229 out = mlt_producer_get_out( producer );
230 properties = MLT_PRODUCER_PROPERTIES( producer );
231 mlt_properties_inc_ref( properties );
235 producer = mlt_producer_cut( source, in, out );
236 if ( in == -1 || in < mlt_producer_get_in( producer ) )
237 in = mlt_producer_get_in( producer );
238 if ( out == -1 || out > mlt_producer_get_out( producer ) )
239 out = mlt_producer_get_out( producer );
240 properties = MLT_PRODUCER_PROPERTIES( producer );
243 // Fetch the cuts parent properties
244 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
246 // Remove fezzik normalisers for fx cuts
247 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
249 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
250 mlt_filter filter = mlt_service_filter( service, 0 );
251 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik" ) )
253 mlt_service_detach( service, filter );
254 filter = mlt_service_filter( service, 0 );
256 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
259 // Check that we have room
260 if ( this->count >= this->size )
263 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
264 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
269 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
270 if ( this->list[ this->count ] != NULL )
272 this->list[ this->count ]->producer = producer;
273 this->list[ this->count ]->frame_in = in;
274 this->list[ this->count ]->frame_out = out;
275 this->list[ this->count ]->frame_count = out - in + 1;
276 this->list[ this->count ]->repeat = 1;
277 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
278 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
279 mlt_event_inc_ref( this->list[ this->count ]->event );
280 mlt_properties_set( properties, "eof", "pause" );
281 mlt_producer_set_speed( producer, 0 );
285 return mlt_playlist_virtual_refresh( this );
288 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
290 // Default producer to NULL
291 mlt_producer producer = NULL;
293 // Loop for each producer until found
294 for ( *clip = 0; *clip < this->count; *clip += 1 )
296 // Increment the total
297 *total += this->list[ *clip ]->frame_count;
299 // Check if the position indicates that we have found the clip
300 // Note that 0 length clips get skipped automatically
301 if ( *position < this->list[ *clip ]->frame_count )
303 // Found it, now break
304 producer = this->list[ *clip ]->producer;
309 // Decrement position by length of this entry
310 *position -= this->list[ *clip ]->frame_count;
317 /** Seek in the virtual playlist.
320 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
322 // Map playlist position to real producer in virtual playlist
323 mlt_position position = mlt_producer_frame( &this->parent );
325 // Keep the original position since we change it while iterating through the list
326 mlt_position original = position;
328 // Clip index and total
332 // Locate the producer for the position
333 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
335 // Get the properties
336 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
338 // Get the eof handling
339 char *eof = mlt_properties_get( properties, "eof" );
341 // Seek in real producer to relative position
342 if ( producer != NULL )
344 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
345 *progressive = count == 1;
346 mlt_producer_seek( producer, (int)position % count );
348 else if ( !strcmp( eof, "pause" ) && total > 0 )
350 playlist_entry *entry = this->list[ this->count - 1 ];
351 int count = entry->frame_count / entry->repeat;
352 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
353 mlt_producer_seek( this_producer, original - 1 );
354 producer = entry->producer;
355 mlt_producer_seek( producer, (int)entry->frame_out % count );
356 mlt_producer_set_speed( this_producer, 0 );
357 mlt_producer_set_speed( producer, 0 );
358 *progressive = count == 1;
360 else if ( !strcmp( eof, "loop" ) && total > 0 )
362 playlist_entry *entry = this->list[ 0 ];
363 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
364 mlt_producer_seek( this_producer, 0 );
365 producer = entry->producer;
366 mlt_producer_seek( producer, 0 );
370 producer = &this->blank;
373 return MLT_PRODUCER_SERVICE( producer );
376 /** Invoked when a producer indicates that it has prematurely reached its end.
379 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
381 // Default producer to blank
382 mlt_producer producer = &this->blank;
384 // Map playlist position to real producer in virtual playlist
385 mlt_position position = mlt_producer_frame( &this->parent );
387 // Loop through the virtual playlist
390 for ( i = 0; i < this->count; i ++ )
392 if ( position < this->list[ i ]->frame_count )
394 // Found it, now break
395 producer = this->list[ i ]->producer;
400 // Decrement position by length of this entry
401 position -= this->list[ i ]->frame_count;
405 // Seek in real producer to relative position
406 if ( i < this->count && this->list[ i ]->frame_out != position )
408 // Update the frame_count for the changed clip (hmmm)
409 this->list[ i ]->frame_out = position;
410 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
412 // Refresh the playlist
413 mlt_playlist_virtual_refresh( this );
419 /** Obtain the current clips index.
422 int mlt_playlist_current_clip( mlt_playlist this )
424 // Map playlist position to real producer in virtual playlist
425 mlt_position position = mlt_producer_frame( &this->parent );
427 // Loop through the virtual playlist
430 for ( i = 0; i < this->count; i ++ )
432 if ( position < this->list[ i ]->frame_count )
434 // Found it, now break
439 // Decrement position by length of this entry
440 position -= this->list[ i ]->frame_count;
447 /** Obtain the current clips producer.
450 mlt_producer mlt_playlist_current( mlt_playlist this )
452 int i = mlt_playlist_current_clip( this );
453 if ( i < this->count )
454 return this->list[ i ]->producer;
459 /** Get the position which corresponds to the start of the next clip.
462 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
464 mlt_position position = 0;
465 int absolute_clip = index;
468 // Determine the absolute clip
471 case mlt_whence_relative_start:
472 absolute_clip = index;
475 case mlt_whence_relative_current:
476 absolute_clip = mlt_playlist_current_clip( this ) + index;
479 case mlt_whence_relative_end:
480 absolute_clip = this->count - index;
484 // Check that we're in a valid range
485 if ( absolute_clip < 0 )
487 else if ( absolute_clip > this->count )
488 absolute_clip = this->count;
490 // Now determine the position
491 for ( i = 0; i < absolute_clip; i ++ )
492 position += this->list[ i ]->frame_count;
497 /** Get all the info about the clip specified.
500 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
502 int error = index < 0 || index >= this->count;
503 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
506 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
507 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
509 info->producer = producer;
510 info->cut = this->list[ index ]->producer;
511 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
512 info->resource = mlt_properties_get( properties, "resource" );
513 info->frame_in = this->list[ index ]->frame_in;
514 info->frame_out = this->list[ index ]->frame_out;
515 info->frame_count = this->list[ index ]->frame_count;
516 info->repeat = this->list[ index ]->repeat;
517 info->length = mlt_producer_get_length( producer );
518 info->fps = mlt_producer_get_fps( producer );
524 /** Get number of clips in the playlist.
527 int mlt_playlist_count( mlt_playlist this )
532 /** Clear the playlist.
535 int mlt_playlist_clear( mlt_playlist this )
538 for ( i = 0; i < this->count; i ++ )
540 mlt_event_close( this->list[ i ]->event );
541 mlt_producer_close( this->list[ i ]->producer );
544 return mlt_playlist_virtual_refresh( this );
547 /** Append a producer to the playlist.
550 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
552 // Append to virtual list
553 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
556 /** Append a producer to the playlist with in/out points.
559 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
561 // Append to virtual list
562 if ( in != -1 && out != -1 )
563 return mlt_playlist_virtual_append( this, producer, in, out );
565 return mlt_playlist_append( this, producer );
568 /** Append a blank to the playlist of a given length.
571 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
573 // Append to the virtual list
574 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
577 /** Insert a producer into the playlist.
580 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
583 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
584 mlt_playlist_append_io( this, producer, in, out );
586 // Move to the position specified
587 mlt_playlist_move( this, this->count - 1, where );
588 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
590 return mlt_playlist_virtual_refresh( this );
593 /** Remove an entry in the playlist.
596 int mlt_playlist_remove( mlt_playlist this, int where )
598 int error = where < 0 || where >= this->count;
599 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
601 // We need to know the current clip and the position within the playlist
602 int current = mlt_playlist_current_clip( this );
603 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
605 // We need all the details about the clip we're removing
606 mlt_playlist_clip_info where_info;
607 playlist_entry *entry = this->list[ where ];
608 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
614 mlt_playlist_get_clip_info( this, &where_info, where );
616 // Make sure the clip to be removed is valid and correct if necessary
619 if ( where >= this->count )
620 where = this->count - 1;
622 // Reorganise the list
623 for ( i = where + 1; i < this->count; i ++ )
624 this->list[ i - 1 ] = this->list[ i ];
627 if ( entry->preservation_hack == 0 )
629 // Decouple from mix_in/out if necessary
630 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
632 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
633 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
635 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
637 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
638 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
641 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
642 mlt_producer_clear( entry->producer );
645 // Close the producer associated to the clip info
646 mlt_event_close( entry->event );
647 mlt_producer_close( entry->producer );
650 if ( where == current )
651 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
652 else if ( where < current && this->count > 0 )
653 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
654 else if ( this->count == 0 )
655 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
660 // Refresh the playlist
661 mlt_playlist_virtual_refresh( this );
667 /** Move an entry in the playlist.
670 int mlt_playlist_move( mlt_playlist this, int src, int dest )
674 /* We need to ensure that the requested indexes are valid and correct it as necessary */
677 if ( src >= this->count )
678 src = this->count - 1;
682 if ( dest >= this->count )
683 dest = this->count - 1;
685 if ( src != dest && this->count > 1 )
687 int current = mlt_playlist_current_clip( this );
688 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
689 playlist_entry *src_entry = NULL;
691 // We need all the details about the current clip
692 mlt_playlist_clip_info current_info;
694 mlt_playlist_get_clip_info( this, ¤t_info, current );
695 position -= current_info.start;
697 if ( current == src )
699 else if ( current > src && current < dest )
701 else if ( current == dest )
704 src_entry = this->list[ src ];
707 for ( i = src; i > dest; i -- )
708 this->list[ i ] = this->list[ i - 1 ];
712 for ( i = src; i < dest; i ++ )
713 this->list[ i ] = this->list[ i + 1 ];
715 this->list[ dest ] = src_entry;
717 mlt_playlist_get_clip_info( this, ¤t_info, current );
718 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
719 mlt_playlist_virtual_refresh( this );
725 /** Repeat the specified clip n times.
728 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
730 int error = repeat < 1 || clip < 0 || clip >= this->count;
733 playlist_entry *entry = this->list[ clip ];
734 entry->repeat = repeat;
735 mlt_playlist_virtual_refresh( this );
740 /** Resize the specified clip.
743 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
745 int error = clip < 0 || clip >= this->count;
746 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
748 playlist_entry *entry = this->list[ clip ];
749 mlt_producer producer = entry->producer;
750 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
752 mlt_events_block( properties, properties );
754 if ( mlt_producer_is_blank( producer ) )
756 // Make sure the blank is long enough to accomodate the length specified
757 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
759 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
760 mlt_properties_set_int( blank_props, "length", out - in + 1 );
761 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
762 mlt_producer_set_in_and_out( &this->blank, 0, out - in );
768 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
769 out = mlt_producer_get_length( producer ) - 1;
778 mlt_producer_set_in_and_out( producer, in, out );
779 mlt_events_unblock( properties, properties );
780 mlt_playlist_virtual_refresh( this );
785 /** Split a clip on the playlist at the given position.
788 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
790 int error = clip < 0 || clip >= this->count;
793 playlist_entry *entry = this->list[ clip ];
794 position = position < 0 ? entry->frame_count + position - 1 : position;
795 if ( position >= 0 && position < entry->frame_count - 1 )
797 int in = entry->frame_in;
798 int out = entry->frame_out;
799 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
800 mlt_playlist_resize_clip( this, clip, in, in + position );
801 if ( !mlt_producer_is_blank( entry->producer ) )
804 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
805 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
806 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
807 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
808 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
810 char *name = mlt_properties_get_name( entry_properties, i );
811 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
812 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
814 mlt_producer_close( split );
818 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
820 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
821 mlt_playlist_virtual_refresh( this );
831 /** Split the playlist at the absolute position.
834 int mlt_playlist_split_at( mlt_playlist this, mlt_position position, int left )
836 int result = this == NULL ? -1 : 0;
839 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) ) )
841 int clip = mlt_playlist_get_clip_index_at( this, position );
842 mlt_playlist_clip_info info;
843 mlt_playlist_get_clip_info( this, &info, clip );
844 if ( left && position != info.start )
845 mlt_playlist_split( this, clip, position - info.start - 1 );
847 mlt_playlist_split( this, clip, position - info.start );
850 else if ( position <= 0 )
856 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
862 /** Join 1 or more consecutive clips.
865 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
867 int error = clip < 0 || clip >= this->count;
871 mlt_playlist new_clip = mlt_playlist_init( );
872 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
873 if ( clip + count >= this->count )
874 count = this->count - clip - 1;
875 for ( i = 0; i <= count; i ++ )
877 playlist_entry *entry = this->list[ clip ];
878 mlt_playlist_append( new_clip, entry->producer );
879 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
880 entry->preservation_hack = 1;
881 mlt_playlist_remove( this, clip );
883 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
884 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
885 mlt_playlist_close( new_clip );
890 /** Mix consecutive clips for a specified length and apply transition if specified.
893 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
895 int error = ( clip < 0 || clip + 1 >= this->count );
898 playlist_entry *clip_a = this->list[ clip ];
899 playlist_entry *clip_b = this->list[ clip + 1 ];
900 mlt_producer track_a = NULL;
901 mlt_producer track_b = NULL;
902 mlt_tractor tractor = mlt_tractor_new( );
903 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
905 // Check length is valid for both clips and resize if necessary.
906 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
907 length = length > max_size ? max_size : length;
909 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
910 if ( length != clip_a->frame_count )
911 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
913 track_a = clip_a->producer;
915 if ( length != clip_b->frame_count )
916 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
918 track_b = clip_b->producer;
920 // Set the tracks on the tractor
921 mlt_tractor_set_track( tractor, track_a, 0 );
922 mlt_tractor_set_track( tractor, track_b, 1 );
924 // Insert the mix object into the playlist
925 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
926 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
928 // Attach the transition
929 if ( transition != NULL )
931 mlt_field field = mlt_tractor_field( tractor );
932 mlt_field_plant_transition( field, transition, 0, 1 );
933 mlt_transition_set_in_and_out( transition, 0, length - 1 );
936 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
937 if ( track_a != clip_a->producer )
938 mlt_producer_close( track_a );
939 if ( track_b != clip_b->producer )
940 mlt_producer_close( track_b );
942 // Check if we have anything left on the right hand clip
943 if ( track_b == clip_b->producer )
945 clip_b->preservation_hack = 1;
946 mlt_playlist_remove( this, clip + 2 );
948 else if ( clip_b->frame_out - clip_b->frame_in > length )
950 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
951 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
952 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
956 mlt_producer_clear( clip_b->producer );
957 mlt_playlist_remove( this, clip + 2 );
960 // Check if we have anything left on the left hand clip
961 if ( track_a == clip_a->producer )
963 clip_a->preservation_hack = 1;
964 mlt_playlist_remove( this, clip );
966 else if ( clip_a->frame_out - clip_a->frame_in > length )
968 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
969 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
970 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
974 mlt_producer_clear( clip_a->producer );
975 mlt_playlist_remove( this, clip );
978 // Unblock and force a fire off of change events to listeners
979 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
980 mlt_playlist_virtual_refresh( this );
981 mlt_tractor_close( tractor );
986 /** Add a transition to an existing mix.
989 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
991 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
992 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
993 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
994 int error = transition == NULL || tractor == NULL;
997 mlt_field field = mlt_tractor_field( tractor );
998 mlt_field_plant_transition( field, transition, 0, 1 );
999 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
1004 /** Return the clip at the clip index.
1007 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
1009 if ( clip >= 0 && clip < this->count )
1010 return this->list[ clip ]->producer;
1014 /** Return the clip at the specified position.
1017 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, mlt_position position )
1019 int index = 0, total = 0;
1020 return mlt_playlist_locate( this, &position, &index, &total );
1023 /** Return the clip index of the specified position.
1026 int mlt_playlist_get_clip_index_at( mlt_playlist this, mlt_position position )
1028 int index = 0, total = 0;
1029 mlt_playlist_locate( this, &position, &index, &total );
1033 /** Determine if the clip is a mix.
1036 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1038 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1039 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1040 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1041 return tractor != NULL;
1044 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1045 back correctly on to the playlist.
1048 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1050 int error = ( clip < 0 || clip >= this->count );
1052 // Ensure that the clip request is actually a mix
1055 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1056 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1057 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1058 this->list[ clip ]->preservation_hack;
1063 playlist_entry *mix = this->list[ clip ];
1064 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1065 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1066 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1067 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1068 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1069 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1071 if ( clip_a != NULL )
1073 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1077 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1078 mlt_playlist_insert( this, cut, clip, -1, -1 );
1082 if ( clip_b != NULL )
1084 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1088 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1089 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1092 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1093 mlt_playlist_remove( this, clip );
1094 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1095 mlt_playlist_virtual_refresh( this );
1100 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1102 int error = ( clip < 0 || clip >= this->count );
1104 // Ensure that the clip request is actually a mix
1107 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1108 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1109 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1114 playlist_entry *mix = this->list[ clip ];
1115 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1116 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1117 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1118 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1119 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1120 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1121 int length = out - in + 1;
1122 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1123 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1125 if ( clip_a != NULL )
1126 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1128 if ( clip_b != NULL )
1129 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1131 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1132 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1133 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1134 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1135 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1136 mlt_producer_set_in_and_out( mix->producer, in, out );
1138 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1139 mlt_playlist_virtual_refresh( this );
1144 /** Consolodate adjacent blank producers.
1147 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1152 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1154 mlt_events_block( properties, properties );
1155 for ( i = 1; i < this->count; i ++ )
1157 playlist_entry *left = this->list[ i - 1 ];
1158 playlist_entry *right = this->list[ i ];
1160 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1162 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1163 mlt_playlist_remove( this, i -- );
1167 if ( !keep_length && this->count > 0 )
1169 playlist_entry *last = this->list[ this->count - 1 ];
1170 if ( mlt_producer_is_blank( last->producer ) )
1171 mlt_playlist_remove( this, this->count - 1 );
1174 mlt_events_unblock( properties, properties );
1175 mlt_playlist_virtual_refresh( this );
1179 /** Determine if the specified clip index is a blank.
1182 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1184 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1187 /** Determine if the specified position is a blank.
1190 int mlt_playlist_is_blank_at( mlt_playlist this, mlt_position position )
1192 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( this, position ) );
1195 /** Replace the specified clip with a blank and return the clip.
1198 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1200 mlt_producer producer = NULL;
1201 if ( !mlt_playlist_is_blank( this, clip ) )
1203 playlist_entry *entry = this->list[ clip ];
1204 int in = entry->frame_in;
1205 int out = entry->frame_out;
1206 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1207 producer = entry->producer;
1208 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1209 mlt_events_block( properties, properties );
1210 mlt_playlist_remove( this, clip );
1211 mlt_playlist_blank( this, out - in );
1212 mlt_playlist_move( this, this->count - 1, clip );
1213 mlt_events_unblock( properties, properties );
1214 mlt_playlist_virtual_refresh( this );
1215 mlt_producer_set_in_and_out( producer, in, out );
1220 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1222 if ( this != NULL && length >= 0 )
1224 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1225 mlt_events_block( properties, properties );
1226 mlt_playlist_blank( this, length );
1227 mlt_playlist_move( this, this->count - 1, clip );
1228 mlt_events_unblock( properties, properties );
1229 mlt_playlist_virtual_refresh( this );
1233 void mlt_playlist_pad_blanks( mlt_playlist this, mlt_position position, int length, int find )
1235 if ( this != NULL && length != 0 )
1237 int clip = mlt_playlist_get_clip_index_at( this, position );
1238 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1239 mlt_events_block( properties, properties );
1240 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1242 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1244 mlt_playlist_clip_info info;
1245 mlt_playlist_get_clip_info( this, &info, clip );
1246 if ( info.frame_out + length > info.frame_in )
1247 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1249 mlt_playlist_remove( this, clip );
1251 else if ( find && clip < this->count && length > 0 )
1253 mlt_playlist_insert_blank( this, clip, length );
1255 mlt_events_unblock( properties, properties );
1256 mlt_playlist_virtual_refresh( this );
1260 int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_producer producer, int mode )
1262 int ret = this == NULL || position < 0 || producer == NULL;
1265 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1266 int length = mlt_producer_get_playtime( producer );
1267 int clip = mlt_playlist_get_clip_index_at( this, position );
1268 mlt_playlist_clip_info info;
1269 mlt_playlist_get_clip_info( this, &info, clip );
1270 mlt_events_block( properties, this );
1271 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1273 // Split and move to new clip if need be
1274 if ( position != info.start && mlt_playlist_split( this, clip, position - info.start - 1 ) == 0 )
1275 mlt_playlist_get_clip_info( this, &info, ++ clip );
1277 // Split again if need be
1278 if ( length < info.frame_count )
1279 mlt_playlist_split( this, clip, length - 1 );
1282 mlt_playlist_remove( this, clip );
1285 mlt_playlist_insert( this, producer, clip, -1, -1 );
1288 else if ( clip < this->count )
1290 if ( position > info.start + info.frame_count / 2 )
1292 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1294 mlt_playlist_get_clip_info( this, &info, clip );
1295 if ( length < info.frame_count )
1296 mlt_playlist_split( this, clip, length );
1297 mlt_playlist_remove( this, clip );
1299 mlt_playlist_insert( this, producer, clip, -1, -1 );
1305 if ( position == info.start )
1306 mlt_playlist_remove( this, clip );
1308 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) - 1 );
1310 mlt_playlist_append( this, producer );
1311 ret = this->count - 1;
1313 mlt_events_unblock( properties, this );
1314 mlt_playlist_virtual_refresh( this );
1323 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1325 mlt_playlist_clip_info info;
1326 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1328 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1331 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1333 mlt_playlist_clip_info info;
1334 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1335 return info.frame_count;
1339 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1342 mlt_playlist_clip_info info;
1343 if ( this != NULL && clip < this->count )
1345 mlt_playlist_get_clip_info( this, &info, clip );
1346 if ( mlt_playlist_is_blank( this, clip ) )
1347 count += info.frame_count;
1349 bounded = this->count;
1350 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1352 mlt_playlist_get_clip_info( this, &info, clip );
1353 if ( mlt_playlist_is_blank( this, clip ) )
1354 count += info.frame_count;
1362 int mlt_playlist_remove_region( mlt_playlist this, mlt_position position, int length )
1364 int index = mlt_playlist_get_clip_index_at( this, position );
1365 if ( index >= 0 && index < this->count )
1367 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1368 int clip_start = mlt_playlist_clip_start( this, index );
1369 int clip_length = mlt_playlist_clip_length( this, index );
1370 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1371 mlt_events_block( properties, this );
1373 if ( position + length > list_length )
1374 length -= ( position + length - list_length );
1376 if ( clip_start < position )
1378 mlt_playlist_split( this, index ++, position - clip_start );
1379 clip_length -= position - clip_start;
1384 if ( mlt_playlist_clip_length( this, index ) > length )
1385 mlt_playlist_split( this, index, length );
1386 length -= mlt_playlist_clip_length( this, index );
1387 mlt_playlist_remove( this, index );
1390 mlt_playlist_consolidate_blanks( this, 0 );
1391 mlt_events_unblock( properties, this );
1392 mlt_playlist_virtual_refresh( this );
1394 // Just to be sure, we'll get the clip index again...
1395 index = mlt_playlist_get_clip_index_at( this, position );
1400 int mlt_playlist_move_region( mlt_playlist this, mlt_position position, int length, int new_position )
1408 /** Get the current frame.
1411 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1413 // Check that we have a producer
1414 if ( producer == NULL )
1420 // Get this mlt_playlist
1421 mlt_playlist this = producer->child;
1423 // Need to ensure the frame is deinterlaced when repeating 1 frame
1424 int progressive = 0;
1426 // Get the real producer
1427 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1429 // Check that we have a producer
1432 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1437 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1439 mlt_service_get_frame( real, frame, index );
1443 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1444 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1445 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1446 mlt_frame_push_service( *frame, NULL );
1447 mlt_frame_push_audio( *frame, NULL );
1448 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1449 mlt_service_apply_filters( real, *frame, 0 );
1450 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1451 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1454 // Check if we're at the end of the clip
1455 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1456 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1457 mlt_playlist_virtual_set_out( this );
1459 // Set the consumer progressive property
1462 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1463 mlt_properties_set_int( properties, "test_audio", 1 );
1466 // Check for notifier and call with appropriate argument
1467 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1468 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1469 if ( notifier != NULL )
1471 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1472 notifier( argument );
1475 // Update position on the frame we're creating
1476 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1478 // Position ourselves on the next frame
1479 mlt_producer_prepare_next( producer );
1484 /** Close the playlist.
1487 void mlt_playlist_close( mlt_playlist this )
1489 if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1492 this->parent.close = NULL;
1493 for ( i = 0; i < this->count; i ++ )
1495 mlt_event_close( this->list[ i ]->event );
1496 mlt_producer_close( this->list[ i ]->producer );
1497 free( this->list[ i ] );
1499 mlt_producer_close( &this->blank );
1500 mlt_producer_close( &this->parent );