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
21 #include "mlt_playlist.h"
22 #include "mlt_tractor.h"
23 #include "mlt_multitrack.h"
24 #include "mlt_field.h"
25 #include "mlt_frame.h"
26 #include "mlt_transition.h"
32 /** Virtual playlist entry.
35 struct playlist_entry_s
37 mlt_producer producer;
38 mlt_position frame_in;
39 mlt_position frame_out;
40 mlt_position frame_count;
42 mlt_position producer_length;
44 int preservation_hack;
47 /** Forward declarations
50 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
51 static int mlt_playlist_unmix( mlt_playlist this, int clip );
52 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out );
57 mlt_playlist mlt_playlist_init( )
59 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
62 mlt_producer producer = &this->parent;
64 // Construct the producer
65 mlt_producer_init( producer, this );
67 // Override the producer get_frame
68 producer->get_frame = producer_get_frame;
70 // Define the destructor
71 producer->close = ( mlt_destructor )mlt_playlist_close;
72 producer->close_object = this;
75 mlt_producer_init( &this->blank, NULL );
76 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "mlt_service", "blank" );
77 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "resource", "blank" );
79 // Indicate that this producer is a playlist
80 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( this ), "playlist", this, 0, NULL, NULL );
82 // Specify the eof condition
83 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "eof", "pause" );
84 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "resource", "<playlist>" );
85 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "mlt_type", "mlt_producer" );
86 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "in", 0 );
87 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "out", -1 );
88 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "length", 0 );
91 this->list = malloc( this->size * sizeof( playlist_entry * ) );
97 /** Get the producer associated to this playlist.
100 mlt_producer mlt_playlist_producer( mlt_playlist this )
102 return this != NULL ? &this->parent : NULL;
105 /** Get the service associated to this playlist.
108 mlt_service mlt_playlist_service( mlt_playlist this )
110 return MLT_PRODUCER_SERVICE( &this->parent );
113 /** Get the propertues associated to this playlist.
116 mlt_properties mlt_playlist_properties( mlt_playlist this )
118 return MLT_PRODUCER_PROPERTIES( &this->parent );
121 /** Refresh the playlist after a clip has been changed.
124 static int mlt_playlist_virtual_refresh( mlt_playlist this )
126 // Obtain the properties
127 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
129 mlt_position frame_count = 0;
131 for ( i = 0; i < this->count; i ++ )
134 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;
162 // Calculate the frame_count
163 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
165 // Update the frame_count for this clip
166 frame_count += this->list[ i ]->frame_count;
169 // Refresh all properties
170 mlt_events_block( properties, properties );
171 mlt_properties_set_position( properties, "length", frame_count );
172 mlt_events_unblock( properties, properties );
173 mlt_properties_set_position( properties, "out", frame_count - 1 );
178 /** Listener for producers on the playlist.
181 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
183 mlt_playlist_virtual_refresh( this );
186 /** Append to the virtual playlist.
189 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
191 mlt_producer producer = NULL;
192 mlt_properties properties = NULL;
193 mlt_properties parent = NULL;
195 // If we have a cut, then use the in/out points from the cut
196 if ( mlt_producer_is_blank( source ) )
198 // Make sure the blank is long enough to accomodate the length specified
199 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
201 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
202 mlt_events_block( blank_props, blank_props );
203 mlt_producer_set_in_and_out( &this->blank, in, out );
204 mlt_events_unblock( blank_props, blank_props );
207 // Now make sure the cut comes from this this->blank
208 if ( source == NULL )
210 producer = mlt_producer_cut( &this->blank, in, out );
212 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
214 producer = mlt_producer_cut( &this->blank, in, out );
219 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
222 properties = MLT_PRODUCER_PROPERTIES( producer );
224 else if ( mlt_producer_is_cut( source ) )
228 in = mlt_producer_get_in( producer );
229 if ( out == -1 || out > mlt_producer_get_out( producer ) )
230 out = mlt_producer_get_out( producer );
231 properties = MLT_PRODUCER_PROPERTIES( producer );
232 mlt_properties_inc_ref( properties );
236 producer = mlt_producer_cut( source, in, out );
237 if ( in == -1 || in < mlt_producer_get_in( producer ) )
238 in = mlt_producer_get_in( producer );
239 if ( out == -1 || out > mlt_producer_get_out( producer ) )
240 out = mlt_producer_get_out( producer );
241 properties = MLT_PRODUCER_PROPERTIES( producer );
244 // Fetch the cuts parent properties
245 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
247 // Remove fezzik normalisers for fx cuts
248 if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
250 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
251 mlt_filter filter = mlt_service_filter( service, 0 );
252 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik" ) )
254 mlt_service_detach( service, filter );
255 filter = mlt_service_filter( service, 0 );
257 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
260 // Check that we have room
261 if ( this->count >= this->size )
264 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
265 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
270 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
271 if ( this->list[ this->count ] != NULL )
273 this->list[ this->count ]->producer = producer;
274 this->list[ this->count ]->frame_in = in;
275 this->list[ this->count ]->frame_out = out;
276 this->list[ this->count ]->frame_count = out - in + 1;
277 this->list[ this->count ]->repeat = 1;
278 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
279 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
280 mlt_event_inc_ref( this->list[ this->count ]->event );
281 mlt_properties_set( properties, "eof", "pause" );
282 mlt_producer_set_speed( producer, 0 );
286 return mlt_playlist_virtual_refresh( this );
289 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
291 // Default producer to NULL
292 mlt_producer producer = NULL;
294 // Loop for each producer until found
295 for ( *clip = 0; *clip < this->count; *clip += 1 )
297 // Increment the total
298 *total += this->list[ *clip ]->frame_count;
300 // Check if the position indicates that we have found the clip
301 // Note that 0 length clips get skipped automatically
302 if ( *position < this->list[ *clip ]->frame_count )
304 // Found it, now break
305 producer = this->list[ *clip ]->producer;
310 // Decrement position by length of this entry
311 *position -= this->list[ *clip ]->frame_count;
318 /** Seek in the virtual playlist.
321 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
323 // Map playlist position to real producer in virtual playlist
324 mlt_position position = mlt_producer_frame( &this->parent );
326 // Keep the original position since we change it while iterating through the list
327 mlt_position original = position;
329 // Clip index and total
333 // Locate the producer for the position
334 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
336 // Get the properties
337 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
339 // Automatically close previous producers if requested
340 if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
341 && position < 2 // tolerate off-by-one error on going to next clip
342 && mlt_properties_get_int( properties, "autoclose" ) )
345 // They might have jumped ahead!
346 for ( j = 0; j < i - 1; j++ )
348 mlt_service_lock( MLT_PRODUCER_SERVICE( this->list[ j ]->producer ) );
349 mlt_producer p = this->list[ j ]->producer;
352 this->list[ j ]->producer = NULL;
353 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
354 mlt_producer_close( p );
356 // If p is null, the lock will not have been "taken"
360 // Get the eof handling
361 char *eof = mlt_properties_get( properties, "eof" );
363 // Seek in real producer to relative position
364 if ( producer != NULL )
366 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
367 *progressive = count == 1;
368 mlt_producer_seek( producer, (int)position % count );
370 else if ( !strcmp( eof, "pause" ) && total > 0 )
372 playlist_entry *entry = this->list[ this->count - 1 ];
373 int count = entry->frame_count / entry->repeat;
374 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
375 mlt_producer_seek( this_producer, original - 1 );
376 producer = entry->producer;
377 mlt_producer_seek( producer, (int)entry->frame_out % count );
378 mlt_producer_set_speed( this_producer, 0 );
379 mlt_producer_set_speed( producer, 0 );
380 *progressive = count == 1;
382 else if ( !strcmp( eof, "loop" ) && total > 0 )
384 playlist_entry *entry = this->list[ 0 ];
385 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
386 mlt_producer_seek( this_producer, 0 );
387 producer = entry->producer;
388 mlt_producer_seek( producer, 0 );
392 producer = &this->blank;
395 return MLT_PRODUCER_SERVICE( producer );
398 /** Invoked when a producer indicates that it has prematurely reached its end.
401 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
403 // Default producer to blank
404 mlt_producer producer = &this->blank;
406 // Map playlist position to real producer in virtual playlist
407 mlt_position position = mlt_producer_frame( &this->parent );
409 // Loop through the virtual playlist
412 for ( i = 0; i < this->count; i ++ )
414 if ( position < this->list[ i ]->frame_count )
416 // Found it, now break
417 producer = this->list[ i ]->producer;
422 // Decrement position by length of this entry
423 position -= this->list[ i ]->frame_count;
427 // Seek in real producer to relative position
428 if ( i < this->count && this->list[ i ]->frame_out != position )
430 // Update the frame_count for the changed clip (hmmm)
431 this->list[ i ]->frame_out = position;
432 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
434 // Refresh the playlist
435 mlt_playlist_virtual_refresh( this );
441 /** Obtain the current clips index.
444 int mlt_playlist_current_clip( mlt_playlist this )
446 // Map playlist position to real producer in virtual playlist
447 mlt_position position = mlt_producer_frame( &this->parent );
449 // Loop through the virtual playlist
452 for ( i = 0; i < this->count; i ++ )
454 if ( position < this->list[ i ]->frame_count )
456 // Found it, now break
461 // Decrement position by length of this entry
462 position -= this->list[ i ]->frame_count;
469 /** Obtain the current clips producer.
472 mlt_producer mlt_playlist_current( mlt_playlist this )
474 int i = mlt_playlist_current_clip( this );
475 if ( i < this->count )
476 return this->list[ i ]->producer;
481 /** Get the position which corresponds to the start of the next clip.
484 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
486 mlt_position position = 0;
487 int absolute_clip = index;
490 // Determine the absolute clip
493 case mlt_whence_relative_start:
494 absolute_clip = index;
497 case mlt_whence_relative_current:
498 absolute_clip = mlt_playlist_current_clip( this ) + index;
501 case mlt_whence_relative_end:
502 absolute_clip = this->count - index;
506 // Check that we're in a valid range
507 if ( absolute_clip < 0 )
509 else if ( absolute_clip > this->count )
510 absolute_clip = this->count;
512 // Now determine the position
513 for ( i = 0; i < absolute_clip; i ++ )
514 position += this->list[ i ]->frame_count;
519 /** Get all the info about the clip specified.
522 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
524 int error = index < 0 || index >= this->count;
525 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
528 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
529 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
531 info->producer = producer;
532 info->cut = this->list[ index ]->producer;
533 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
534 info->resource = mlt_properties_get( properties, "resource" );
535 info->frame_in = this->list[ index ]->frame_in;
536 info->frame_out = this->list[ index ]->frame_out;
537 info->frame_count = this->list[ index ]->frame_count;
538 info->repeat = this->list[ index ]->repeat;
539 info->length = mlt_producer_get_length( producer );
540 info->fps = mlt_producer_get_fps( producer );
546 /** Get number of clips in the playlist.
549 int mlt_playlist_count( mlt_playlist this )
554 /** Clear the playlist.
557 int mlt_playlist_clear( mlt_playlist this )
560 for ( i = 0; i < this->count; i ++ )
562 mlt_event_close( this->list[ i ]->event );
563 mlt_producer_close( this->list[ i ]->producer );
566 return mlt_playlist_virtual_refresh( this );
569 /** Append a producer to the playlist.
572 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
574 // Append to virtual list
575 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
578 /** Append a producer to the playlist with in/out points.
581 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
583 // Append to virtual list
584 if ( in != -1 && out != -1 )
585 return mlt_playlist_virtual_append( this, producer, in, out );
587 return mlt_playlist_append( this, producer );
590 /** Append a blank to the playlist of a given length.
593 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
595 // Append to the virtual list
596 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
599 /** Insert a producer into the playlist.
602 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
605 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
606 mlt_playlist_append_io( this, producer, in, out );
608 // Move to the position specified
609 mlt_playlist_move( this, this->count - 1, where );
610 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
612 return mlt_playlist_virtual_refresh( this );
615 /** Remove an entry in the playlist.
618 int mlt_playlist_remove( mlt_playlist this, int where )
620 int error = where < 0 || where >= this->count;
621 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
623 // We need to know the current clip and the position within the playlist
624 int current = mlt_playlist_current_clip( this );
625 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
627 // We need all the details about the clip we're removing
628 mlt_playlist_clip_info where_info;
629 playlist_entry *entry = this->list[ where ];
630 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
636 mlt_playlist_get_clip_info( this, &where_info, where );
638 // Make sure the clip to be removed is valid and correct if necessary
641 if ( where >= this->count )
642 where = this->count - 1;
644 // Reorganise the list
645 for ( i = where + 1; i < this->count; i ++ )
646 this->list[ i - 1 ] = this->list[ i ];
649 if ( entry->preservation_hack == 0 )
651 // Decouple from mix_in/out if necessary
652 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
654 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
655 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
657 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
659 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
660 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
663 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
664 mlt_producer_clear( entry->producer );
667 // Close the producer associated to the clip info
668 mlt_event_close( entry->event );
669 mlt_producer_close( entry->producer );
672 if ( where == current )
673 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
674 else if ( where < current && this->count > 0 )
675 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
676 else if ( this->count == 0 )
677 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
682 // Refresh the playlist
683 mlt_playlist_virtual_refresh( this );
689 /** Move an entry in the playlist.
692 int mlt_playlist_move( mlt_playlist this, int src, int dest )
696 /* We need to ensure that the requested indexes are valid and correct it as necessary */
699 if ( src >= this->count )
700 src = this->count - 1;
704 if ( dest >= this->count )
705 dest = this->count - 1;
707 if ( src != dest && this->count > 1 )
709 int current = mlt_playlist_current_clip( this );
710 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
711 playlist_entry *src_entry = NULL;
713 // We need all the details about the current clip
714 mlt_playlist_clip_info current_info;
716 mlt_playlist_get_clip_info( this, ¤t_info, current );
717 position -= current_info.start;
719 if ( current == src )
721 else if ( current > src && current < dest )
723 else if ( current == dest )
726 src_entry = this->list[ src ];
729 for ( i = src; i > dest; i -- )
730 this->list[ i ] = this->list[ i - 1 ];
734 for ( i = src; i < dest; i ++ )
735 this->list[ i ] = this->list[ i + 1 ];
737 this->list[ dest ] = src_entry;
739 mlt_playlist_get_clip_info( this, ¤t_info, current );
740 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
741 mlt_playlist_virtual_refresh( this );
747 /** Repeat the specified clip n times.
750 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
752 int error = repeat < 1 || clip < 0 || clip >= this->count;
755 playlist_entry *entry = this->list[ clip ];
756 entry->repeat = repeat;
757 mlt_playlist_virtual_refresh( this );
762 /** Resize the specified clip.
765 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
767 int error = clip < 0 || clip >= this->count;
768 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
770 playlist_entry *entry = this->list[ clip ];
771 mlt_producer producer = entry->producer;
772 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
774 mlt_events_block( properties, properties );
776 if ( mlt_producer_is_blank( producer ) )
778 // Make sure the blank is long enough to accomodate the length specified
779 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
781 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
782 mlt_properties_set_int( blank_props, "length", out - in + 1 );
783 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
784 mlt_producer_set_in_and_out( &this->blank, 0, out - in );
790 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
791 out = mlt_producer_get_length( producer ) - 1;
800 mlt_producer_set_in_and_out( producer, in, out );
801 mlt_events_unblock( properties, properties );
802 mlt_playlist_virtual_refresh( this );
807 /** Split a clip on the playlist at the given position.
810 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
812 int error = clip < 0 || clip >= this->count;
815 playlist_entry *entry = this->list[ clip ];
816 position = position < 0 ? entry->frame_count + position - 1 : position;
817 if ( position >= 0 && position < entry->frame_count - 1 )
819 int in = entry->frame_in;
820 int out = entry->frame_out;
821 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
822 mlt_playlist_resize_clip( this, clip, in, in + position );
823 if ( !mlt_producer_is_blank( entry->producer ) )
826 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
827 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
828 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
829 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
830 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
832 char *name = mlt_properties_get_name( entry_properties, i );
833 if ( name != NULL && !strncmp( name, "meta.", 5 ) )
834 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
836 mlt_producer_close( split );
840 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
842 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
843 mlt_playlist_virtual_refresh( this );
853 /** Split the playlist at the absolute position.
856 int mlt_playlist_split_at( mlt_playlist this, mlt_position position, int left )
858 int result = this == NULL ? -1 : 0;
861 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) ) )
863 int clip = mlt_playlist_get_clip_index_at( this, position );
864 mlt_playlist_clip_info info;
865 mlt_playlist_get_clip_info( this, &info, clip );
866 if ( left && position != info.start )
867 mlt_playlist_split( this, clip, position - info.start - 1 );
869 mlt_playlist_split( this, clip, position - info.start );
872 else if ( position <= 0 )
878 result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
884 /** Join 1 or more consecutive clips.
887 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
889 int error = clip < 0 || clip >= this->count;
893 mlt_playlist new_clip = mlt_playlist_init( );
894 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
895 if ( clip + count >= this->count )
896 count = this->count - clip - 1;
897 for ( i = 0; i <= count; i ++ )
899 playlist_entry *entry = this->list[ clip ];
900 mlt_playlist_append( new_clip, entry->producer );
901 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
902 entry->preservation_hack = 1;
903 mlt_playlist_remove( this, clip );
905 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
906 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
907 mlt_playlist_close( new_clip );
912 /** Mix consecutive clips for a specified length and apply transition if specified.
915 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
917 int error = ( clip < 0 || clip + 1 >= this->count );
920 playlist_entry *clip_a = this->list[ clip ];
921 playlist_entry *clip_b = this->list[ clip + 1 ];
922 mlt_producer track_a = NULL;
923 mlt_producer track_b = NULL;
924 mlt_tractor tractor = mlt_tractor_new( );
925 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
927 // Check length is valid for both clips and resize if necessary.
928 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
929 length = length > max_size ? max_size : length;
931 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
932 if ( length != clip_a->frame_count )
933 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
935 track_a = clip_a->producer;
937 if ( length != clip_b->frame_count )
938 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
940 track_b = clip_b->producer;
942 // Set the tracks on the tractor
943 mlt_tractor_set_track( tractor, track_a, 0 );
944 mlt_tractor_set_track( tractor, track_b, 1 );
946 // Insert the mix object into the playlist
947 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
948 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
950 // Attach the transition
951 if ( transition != NULL )
953 mlt_field field = mlt_tractor_field( tractor );
954 mlt_field_plant_transition( field, transition, 0, 1 );
955 mlt_transition_set_in_and_out( transition, 0, length - 1 );
958 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
959 if ( track_a != clip_a->producer )
960 mlt_producer_close( track_a );
961 if ( track_b != clip_b->producer )
962 mlt_producer_close( track_b );
964 // Check if we have anything left on the right hand clip
965 if ( track_b == clip_b->producer )
967 clip_b->preservation_hack = 1;
968 mlt_playlist_remove( this, clip + 2 );
970 else if ( clip_b->frame_out - clip_b->frame_in > length )
972 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
973 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
974 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
978 mlt_producer_clear( clip_b->producer );
979 mlt_playlist_remove( this, clip + 2 );
982 // Check if we have anything left on the left hand clip
983 if ( track_a == clip_a->producer )
985 clip_a->preservation_hack = 1;
986 mlt_playlist_remove( this, clip );
988 else if ( clip_a->frame_out - clip_a->frame_in > length )
990 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
991 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
992 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
996 mlt_producer_clear( clip_a->producer );
997 mlt_playlist_remove( this, clip );
1000 // Unblock and force a fire off of change events to listeners
1001 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1002 mlt_playlist_virtual_refresh( this );
1003 mlt_tractor_close( tractor );
1008 /** Add a transition to an existing mix.
1011 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
1013 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1014 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1015 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1016 int error = transition == NULL || tractor == NULL;
1019 mlt_field field = mlt_tractor_field( tractor );
1020 mlt_field_plant_transition( field, transition, 0, 1 );
1021 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
1026 /** Return the clip at the clip index.
1029 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
1031 if ( clip >= 0 && clip < this->count )
1032 return this->list[ clip ]->producer;
1036 /** Return the clip at the specified position.
1039 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, mlt_position position )
1041 int index = 0, total = 0;
1042 return mlt_playlist_locate( this, &position, &index, &total );
1045 /** Return the clip index of the specified position.
1048 int mlt_playlist_get_clip_index_at( mlt_playlist this, mlt_position position )
1050 int index = 0, total = 0;
1051 mlt_playlist_locate( this, &position, &index, &total );
1055 /** Determine if the clip is a mix.
1058 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1060 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1061 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1062 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1063 return tractor != NULL;
1066 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1067 back correctly on to the playlist.
1070 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1072 int error = ( clip < 0 || clip >= this->count );
1074 // Ensure that the clip request is actually a mix
1077 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1078 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1079 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1080 this->list[ clip ]->preservation_hack;
1085 playlist_entry *mix = this->list[ clip ];
1086 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1087 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1088 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1089 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1090 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1091 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1093 if ( clip_a != NULL )
1095 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1099 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1100 mlt_playlist_insert( this, cut, clip, -1, -1 );
1104 if ( clip_b != NULL )
1106 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1110 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1111 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1114 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1115 mlt_playlist_remove( this, clip );
1116 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1117 mlt_playlist_virtual_refresh( this );
1122 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1124 int error = ( clip < 0 || clip >= this->count );
1126 // Ensure that the clip request is actually a mix
1129 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1130 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1131 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1136 playlist_entry *mix = this->list[ clip ];
1137 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1138 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1139 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1140 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1141 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1142 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1143 int length = out - in + 1;
1144 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1145 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1147 if ( clip_a != NULL )
1148 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1150 if ( clip_b != NULL )
1151 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1153 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1154 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1155 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1156 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1157 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1158 mlt_producer_set_in_and_out( mix->producer, in, out );
1160 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1161 mlt_playlist_virtual_refresh( this );
1166 /** Consolodate adjacent blank producers.
1169 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1174 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1176 mlt_events_block( properties, properties );
1177 for ( i = 1; i < this->count; i ++ )
1179 playlist_entry *left = this->list[ i - 1 ];
1180 playlist_entry *right = this->list[ i ];
1182 if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1184 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1185 mlt_playlist_remove( this, i -- );
1189 if ( !keep_length && this->count > 0 )
1191 playlist_entry *last = this->list[ this->count - 1 ];
1192 if ( mlt_producer_is_blank( last->producer ) )
1193 mlt_playlist_remove( this, this->count - 1 );
1196 mlt_events_unblock( properties, properties );
1197 mlt_playlist_virtual_refresh( this );
1201 /** Determine if the specified clip index is a blank.
1204 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1206 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1209 /** Determine if the specified position is a blank.
1212 int mlt_playlist_is_blank_at( mlt_playlist this, mlt_position position )
1214 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( this, position ) );
1217 /** Replace the specified clip with a blank and return the clip.
1220 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1222 mlt_producer producer = NULL;
1223 if ( !mlt_playlist_is_blank( this, clip ) )
1225 playlist_entry *entry = this->list[ clip ];
1226 int in = entry->frame_in;
1227 int out = entry->frame_out;
1228 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1229 producer = entry->producer;
1230 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1231 mlt_events_block( properties, properties );
1232 mlt_playlist_remove( this, clip );
1233 mlt_playlist_blank( this, out - in );
1234 mlt_playlist_move( this, this->count - 1, clip );
1235 mlt_events_unblock( properties, properties );
1236 mlt_playlist_virtual_refresh( this );
1237 mlt_producer_set_in_and_out( producer, in, out );
1242 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1244 if ( this != NULL && length >= 0 )
1246 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1247 mlt_events_block( properties, properties );
1248 mlt_playlist_blank( this, length );
1249 mlt_playlist_move( this, this->count - 1, clip );
1250 mlt_events_unblock( properties, properties );
1251 mlt_playlist_virtual_refresh( this );
1255 void mlt_playlist_pad_blanks( mlt_playlist this, mlt_position position, int length, int find )
1257 if ( this != NULL && length != 0 )
1259 int clip = mlt_playlist_get_clip_index_at( this, position );
1260 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1261 mlt_events_block( properties, properties );
1262 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1264 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1266 mlt_playlist_clip_info info;
1267 mlt_playlist_get_clip_info( this, &info, clip );
1268 if ( info.frame_out + length > info.frame_in )
1269 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1271 mlt_playlist_remove( this, clip );
1273 else if ( find && clip < this->count && length > 0 )
1275 mlt_playlist_insert_blank( this, clip, length );
1277 mlt_events_unblock( properties, properties );
1278 mlt_playlist_virtual_refresh( this );
1282 int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_producer producer, int mode )
1284 int ret = this == NULL || position < 0 || producer == NULL;
1287 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1288 int length = mlt_producer_get_playtime( producer );
1289 int clip = mlt_playlist_get_clip_index_at( this, position );
1290 mlt_playlist_clip_info info;
1291 mlt_playlist_get_clip_info( this, &info, clip );
1292 mlt_events_block( properties, this );
1293 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1295 // Split and move to new clip if need be
1296 if ( position != info.start && mlt_playlist_split( this, clip, position - info.start - 1 ) == 0 )
1297 mlt_playlist_get_clip_info( this, &info, ++ clip );
1299 // Split again if need be
1300 if ( length < info.frame_count )
1301 mlt_playlist_split( this, clip, length - 1 );
1304 mlt_playlist_remove( this, clip );
1307 mlt_playlist_insert( this, producer, clip, -1, -1 );
1310 else if ( clip < this->count )
1312 if ( position > info.start + info.frame_count / 2 )
1314 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1316 mlt_playlist_get_clip_info( this, &info, clip );
1317 if ( length < info.frame_count )
1318 mlt_playlist_split( this, clip, length );
1319 mlt_playlist_remove( this, clip );
1321 mlt_playlist_insert( this, producer, clip, -1, -1 );
1327 if ( position == info.start )
1328 mlt_playlist_remove( this, clip );
1330 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) - 1 );
1332 mlt_playlist_append( this, producer );
1333 ret = this->count - 1;
1335 mlt_events_unblock( properties, this );
1336 mlt_playlist_virtual_refresh( this );
1345 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1347 mlt_playlist_clip_info info;
1348 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1350 return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1353 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1355 mlt_playlist_clip_info info;
1356 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1357 return info.frame_count;
1361 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1364 mlt_playlist_clip_info info;
1365 if ( this != NULL && clip < this->count )
1367 mlt_playlist_get_clip_info( this, &info, clip );
1368 if ( mlt_playlist_is_blank( this, clip ) )
1369 count += info.frame_count;
1371 bounded = this->count;
1372 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1374 mlt_playlist_get_clip_info( this, &info, clip );
1375 if ( mlt_playlist_is_blank( this, clip ) )
1376 count += info.frame_count;
1384 int mlt_playlist_remove_region( mlt_playlist this, mlt_position position, int length )
1386 int index = mlt_playlist_get_clip_index_at( this, position );
1387 if ( index >= 0 && index < this->count )
1389 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1390 int clip_start = mlt_playlist_clip_start( this, index );
1391 int clip_length = mlt_playlist_clip_length( this, index );
1392 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1393 mlt_events_block( properties, this );
1395 if ( position + length > list_length )
1396 length -= ( position + length - list_length );
1398 if ( clip_start < position )
1400 mlt_playlist_split( this, index ++, position - clip_start );
1401 clip_length -= position - clip_start;
1406 if ( mlt_playlist_clip_length( this, index ) > length )
1407 mlt_playlist_split( this, index, length );
1408 length -= mlt_playlist_clip_length( this, index );
1409 mlt_playlist_remove( this, index );
1412 mlt_playlist_consolidate_blanks( this, 0 );
1413 mlt_events_unblock( properties, this );
1414 mlt_playlist_virtual_refresh( this );
1416 // Just to be sure, we'll get the clip index again...
1417 index = mlt_playlist_get_clip_index_at( this, position );
1422 int mlt_playlist_move_region( mlt_playlist this, mlt_position position, int length, int new_position )
1430 /** Get the current frame.
1433 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1435 // Check that we have a producer
1436 if ( producer == NULL )
1442 // Get this mlt_playlist
1443 mlt_playlist this = producer->child;
1445 // Need to ensure the frame is deinterlaced when repeating 1 frame
1446 int progressive = 0;
1448 // Get the real producer
1449 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1451 // Check that we have a producer
1454 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1459 if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1461 mlt_service_get_frame( real, frame, index );
1465 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1466 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1467 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1468 mlt_frame_push_service( *frame, NULL );
1469 mlt_frame_push_audio( *frame, NULL );
1470 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1471 mlt_service_apply_filters( real, *frame, 0 );
1472 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1473 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1476 // Check if we're at the end of the clip
1477 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1478 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1479 mlt_playlist_virtual_set_out( this );
1481 // Set the consumer progressive property
1484 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1485 mlt_properties_set_int( properties, "test_audio", 1 );
1488 // Check for notifier and call with appropriate argument
1489 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1490 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1491 if ( notifier != NULL )
1493 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1494 notifier( argument );
1497 // Update position on the frame we're creating
1498 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1500 // Position ourselves on the next frame
1501 mlt_producer_prepare_next( producer );
1506 /** Close the playlist.
1509 void mlt_playlist_close( mlt_playlist this )
1511 if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1514 this->parent.close = NULL;
1515 for ( i = 0; i < this->count; i ++ )
1517 mlt_event_close( this->list[ i ]->event );
1518 mlt_producer_close( this->list[ i ]->producer );
1519 free( this->list[ i ] );
1521 mlt_producer_close( &this->blank );
1522 mlt_producer_close( &this->parent );