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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 // Get the fps of the first producer
132 double fps = mlt_properties_get_double( properties, "first_fps" );
134 mlt_position frame_count = 0;
136 for ( i = 0; i < this->count; i ++ )
139 mlt_producer producer = this->list[ i ]->producer;
140 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
145 // Inherit it from the producer
146 fps = mlt_producer_get_fps( producer );
148 else if ( fps != mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( producer ), "fps" ) )
150 // Generate a warning for now - the following attempt to fix may fail
151 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
153 // It should be safe to impose fps on an image producer, but not necessarily safe for video
154 mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( producer ), "fps", fps );
157 // Check if the length of the producer has changed
158 if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
159 this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
161 // This clip should be removed...
162 if ( current_length < 1 )
164 this->list[ i ]->frame_in = 0;
165 this->list[ i ]->frame_out = -1;
166 this->list[ i ]->frame_count = 0;
170 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
171 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
172 this->list[ i ]->frame_count = current_length;
175 // Update the producer_length
176 this->list[ i ]->producer_length = current_length;
179 // Calculate the frame_count
180 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
182 // Update the frame_count for this clip
183 frame_count += this->list[ i ]->frame_count;
186 // Refresh all properties
187 mlt_properties_set_double( properties, "first_fps", fps );
188 mlt_properties_set_double( properties, "fps", fps == 0 ? 25 : fps );
189 mlt_events_block( properties, properties );
190 mlt_properties_set_position( properties, "length", frame_count );
191 mlt_events_unblock( properties, properties );
192 mlt_properties_set_position( properties, "out", frame_count - 1 );
197 /** Listener for producers on the playlist.
200 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
202 mlt_playlist_virtual_refresh( this );
205 /** Append to the virtual playlist.
208 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
210 mlt_producer producer = NULL;
211 mlt_properties properties = NULL;
212 mlt_properties parent = NULL;
214 // If we have a cut, then use the in/out points from the cut
215 if ( mlt_producer_is_blank( source ) )
217 // Make sure the blank is long enough to accomodate the length specified
218 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
220 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
221 mlt_events_block( blank_props, blank_props );
222 mlt_producer_set_in_and_out( &this->blank, in, out );
223 mlt_events_unblock( blank_props, blank_props );
226 // Now make sure the cut comes from this this->blank
227 if ( source == NULL )
229 producer = mlt_producer_cut( &this->blank, in, out );
231 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
233 producer = mlt_producer_cut( &this->blank, in, out );
238 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
241 properties = MLT_PRODUCER_PROPERTIES( producer );
243 else if ( mlt_producer_is_cut( source ) )
247 in = mlt_producer_get_in( producer );
248 if ( out == -1 || out > mlt_producer_get_out( producer ) )
249 out = mlt_producer_get_out( producer );
250 properties = MLT_PRODUCER_PROPERTIES( producer );
251 mlt_properties_inc_ref( properties );
255 producer = mlt_producer_cut( source, in, out );
256 if ( in == -1 || in < mlt_producer_get_in( producer ) )
257 in = mlt_producer_get_in( producer );
258 if ( out == -1 || out > mlt_producer_get_out( producer ) )
259 out = mlt_producer_get_out( producer );
260 properties = MLT_PRODUCER_PROPERTIES( producer );
263 // Fetch the cuts parent properties
264 parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
266 // Check that we have room
267 if ( this->count >= this->size )
270 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
271 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
276 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
277 if ( this->list[ this->count ] != NULL )
279 this->list[ this->count ]->producer = producer;
280 this->list[ this->count ]->frame_in = in;
281 this->list[ this->count ]->frame_out = out;
282 this->list[ this->count ]->frame_count = out - in + 1;
283 this->list[ this->count ]->repeat = 1;
284 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
285 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
286 mlt_event_inc_ref( this->list[ this->count ]->event );
287 mlt_properties_set( properties, "eof", "pause" );
288 mlt_producer_set_speed( producer, 0 );
292 return mlt_playlist_virtual_refresh( this );
295 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
297 // Default producer to NULL
298 mlt_producer producer = NULL;
300 // Loop for each producer until found
301 for ( *clip = 0; *clip < this->count; *clip += 1 )
303 // Increment the total
304 *total += this->list[ *clip ]->frame_count;
306 // Check if the position indicates that we have found the clip
307 // Note that 0 length clips get skipped automatically
308 if ( *position < this->list[ *clip ]->frame_count )
310 // Found it, now break
311 producer = this->list[ *clip ]->producer;
316 // Decrement position by length of this entry
317 *position -= this->list[ *clip ]->frame_count;
324 /** Seek in the virtual playlist.
327 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
329 // Map playlist position to real producer in virtual playlist
330 mlt_position position = mlt_producer_frame( &this->parent );
332 // Keep the original position since we change it while iterating through the list
333 mlt_position original = position;
335 // Clip index and total
339 // Locate the producer for the position
340 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
342 // Get the properties
343 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
345 // Get the eof handling
346 char *eof = mlt_properties_get( properties, "eof" );
348 // Seek in real producer to relative position
349 if ( producer != NULL )
351 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
352 *progressive = count == 1;
353 mlt_producer_seek( producer, position % count );
355 else if ( !strcmp( eof, "pause" ) && total > 0 )
357 playlist_entry *entry = this->list[ this->count - 1 ];
358 int count = entry->frame_count / entry->repeat;
359 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
360 mlt_producer_seek( this_producer, original - 1 );
361 producer = entry->producer;
362 mlt_producer_seek( producer, entry->frame_out % count );
363 mlt_producer_set_speed( this_producer, 0 );
364 mlt_producer_set_speed( producer, 0 );
365 *progressive = count == 1;
367 else if ( !strcmp( eof, "loop" ) && total > 0 )
369 playlist_entry *entry = this->list[ 0 ];
370 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
371 mlt_producer_seek( this_producer, 0 );
372 producer = entry->producer;
373 mlt_producer_seek( producer, 0 );
377 producer = &this->blank;
380 return MLT_PRODUCER_SERVICE( producer );
383 /** Invoked when a producer indicates that it has prematurely reached its end.
386 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
388 // Default producer to blank
389 mlt_producer producer = &this->blank;
391 // Map playlist position to real producer in virtual playlist
392 mlt_position position = mlt_producer_frame( &this->parent );
394 // Loop through the virtual playlist
397 for ( i = 0; i < this->count; i ++ )
399 if ( position < this->list[ i ]->frame_count )
401 // Found it, now break
402 producer = this->list[ i ]->producer;
407 // Decrement position by length of this entry
408 position -= this->list[ i ]->frame_count;
412 // Seek in real producer to relative position
413 if ( i < this->count && this->list[ i ]->frame_out != position )
415 // Update the frame_count for the changed clip (hmmm)
416 this->list[ i ]->frame_out = position;
417 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
419 // Refresh the playlist
420 mlt_playlist_virtual_refresh( this );
426 /** Obtain the current clips index.
429 int mlt_playlist_current_clip( mlt_playlist this )
431 // Map playlist position to real producer in virtual playlist
432 mlt_position position = mlt_producer_frame( &this->parent );
434 // Loop through the virtual playlist
437 for ( i = 0; i < this->count; i ++ )
439 if ( position < this->list[ i ]->frame_count )
441 // Found it, now break
446 // Decrement position by length of this entry
447 position -= this->list[ i ]->frame_count;
454 /** Obtain the current clips producer.
457 mlt_producer mlt_playlist_current( mlt_playlist this )
459 int i = mlt_playlist_current_clip( this );
460 if ( i < this->count )
461 return this->list[ i ]->producer;
466 /** Get the position which corresponds to the start of the next clip.
469 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
471 mlt_position position = 0;
472 int absolute_clip = index;
475 // Determine the absolute clip
478 case mlt_whence_relative_start:
479 absolute_clip = index;
482 case mlt_whence_relative_current:
483 absolute_clip = mlt_playlist_current_clip( this ) + index;
486 case mlt_whence_relative_end:
487 absolute_clip = this->count - index;
491 // Check that we're in a valid range
492 if ( absolute_clip < 0 )
494 else if ( absolute_clip > this->count )
495 absolute_clip = this->count;
497 // Now determine the position
498 for ( i = 0; i < absolute_clip; i ++ )
499 position += this->list[ i ]->frame_count;
504 /** Get all the info about the clip specified.
507 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
509 int error = index < 0 || index >= this->count;
510 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
513 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
514 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
516 info->producer = producer;
517 info->cut = this->list[ index ]->producer;
518 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
519 info->resource = mlt_properties_get( properties, "resource" );
520 info->frame_in = this->list[ index ]->frame_in;
521 info->frame_out = this->list[ index ]->frame_out;
522 info->frame_count = this->list[ index ]->frame_count;
523 info->repeat = this->list[ index ]->repeat;
524 info->length = mlt_producer_get_length( producer );
525 info->fps = mlt_producer_get_fps( producer );
531 /** Get number of clips in the playlist.
534 int mlt_playlist_count( mlt_playlist this )
539 /** Clear the playlist.
542 int mlt_playlist_clear( mlt_playlist this )
545 for ( i = 0; i < this->count; i ++ )
547 mlt_event_close( this->list[ i ]->event );
548 mlt_producer_close( this->list[ i ]->producer );
551 mlt_properties_set_double( MLT_PLAYLIST_PROPERTIES( this ), "first_fps", 0 );
552 return mlt_playlist_virtual_refresh( this );
555 /** Append a producer to the playlist.
558 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
560 // Append to virtual list
561 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
564 /** Append a producer to the playlist with in/out points.
567 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
569 // Append to virtual list
570 if ( in != -1 && out != -1 )
571 return mlt_playlist_virtual_append( this, producer, in, out );
573 return mlt_playlist_append( this, producer );
576 /** Append a blank to the playlist of a given length.
579 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
581 // Append to the virtual list
582 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
585 /** Insert a producer into the playlist.
588 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
591 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
592 mlt_playlist_append_io( this, producer, in, out );
594 // Move to the position specified
595 mlt_playlist_move( this, this->count - 1, where );
596 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
598 return mlt_playlist_virtual_refresh( this );
601 /** Remove an entry in the playlist.
604 int mlt_playlist_remove( mlt_playlist this, int where )
606 int error = where < 0 || where >= this->count;
607 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
609 // We need to know the current clip and the position within the playlist
610 int current = mlt_playlist_current_clip( this );
611 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
613 // We need all the details about the clip we're removing
614 mlt_playlist_clip_info where_info;
615 playlist_entry *entry = this->list[ where ];
616 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
622 mlt_playlist_get_clip_info( this, &where_info, where );
624 // Make sure the clip to be removed is valid and correct if necessary
627 if ( where >= this->count )
628 where = this->count - 1;
630 // Reorganise the list
631 for ( i = where + 1; i < this->count; i ++ )
632 this->list[ i - 1 ] = this->list[ i ];
635 if ( entry->preservation_hack == 0 )
637 // Decouple from mix_in/out if necessary
638 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
640 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
641 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
643 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
645 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
646 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
649 if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
650 mlt_producer_clear( entry->producer );
653 // Close the producer associated to the clip info
654 mlt_event_close( entry->event );
655 mlt_producer_close( entry->producer );
658 if ( where == current )
659 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
660 else if ( where < current && this->count > 0 )
661 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
662 else if ( this->count == 0 )
663 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
668 // Refresh the playlist
669 mlt_playlist_virtual_refresh( this );
675 /** Move an entry in the playlist.
678 int mlt_playlist_move( mlt_playlist this, int src, int dest )
682 /* We need to ensure that the requested indexes are valid and correct it as necessary */
685 if ( src >= this->count )
686 src = this->count - 1;
690 if ( dest >= this->count )
691 dest = this->count - 1;
693 if ( src != dest && this->count > 1 )
695 int current = mlt_playlist_current_clip( this );
696 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
697 playlist_entry *src_entry = NULL;
699 // We need all the details about the current clip
700 mlt_playlist_clip_info current_info;
702 mlt_playlist_get_clip_info( this, ¤t_info, current );
703 position -= current_info.start;
705 if ( current == src )
707 else if ( current > src && current < dest )
709 else if ( current == dest )
712 src_entry = this->list[ src ];
715 for ( i = src; i > dest; i -- )
716 this->list[ i ] = this->list[ i - 1 ];
720 for ( i = src; i < dest; i ++ )
721 this->list[ i ] = this->list[ i + 1 ];
723 this->list[ dest ] = src_entry;
725 mlt_playlist_get_clip_info( this, ¤t_info, current );
726 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
727 mlt_playlist_virtual_refresh( this );
733 /** Repeat the specified clip n times.
736 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
738 int error = repeat < 1 || clip < 0 || clip >= this->count;
741 playlist_entry *entry = this->list[ clip ];
742 entry->repeat = repeat;
743 mlt_playlist_virtual_refresh( this );
748 /** Resize the specified clip.
751 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
753 int error = clip < 0 || clip >= this->count;
754 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
756 playlist_entry *entry = this->list[ clip ];
757 mlt_producer producer = entry->producer;
759 if ( mlt_producer_is_blank( producer ) )
761 // Make sure the blank is long enough to accomodate the length specified
762 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
764 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
765 mlt_events_block( blank_props, blank_props );
766 mlt_producer_set_in_and_out( &this->blank, in, out );
767 mlt_events_unblock( blank_props, blank_props );
773 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
774 out = mlt_producer_get_length( producer ) - 1;
783 mlt_producer_set_in_and_out( producer, in, out );
788 /** Split a clip on the playlist at the given position.
791 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
793 int error = clip < 0 || clip >= this->count;
796 playlist_entry *entry = this->list[ clip ];
797 position = position < 0 ? entry->frame_count + position - 1 : position;
798 if ( position >= 0 && position <= entry->frame_count )
800 int in = entry->frame_in;
801 int out = entry->frame_out;
802 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
803 mlt_playlist_resize_clip( this, clip, in, in + position );
804 if ( !mlt_producer_is_blank( entry->producer ) )
806 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
807 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
808 mlt_producer_close( split );
812 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
814 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
815 mlt_playlist_virtual_refresh( this );
825 /** Join 1 or more consecutive clips.
828 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
830 int error = clip < 0 || ( clip ) >= this->count;
834 mlt_playlist new_clip = mlt_playlist_init( );
835 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
836 if ( clip + count >= this->count )
837 count = this->count - clip;
838 for ( i = 0; i <= count; i ++ )
840 playlist_entry *entry = this->list[ clip ];
841 mlt_playlist_append( new_clip, entry->producer );
842 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
843 entry->preservation_hack = 1;
844 mlt_playlist_remove( this, clip );
846 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
847 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
848 mlt_playlist_close( new_clip );
853 /** Mix consecutive clips for a specified length and apply transition if specified.
856 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
858 int error = ( clip < 0 || clip + 1 >= this->count );
861 playlist_entry *clip_a = this->list[ clip ];
862 playlist_entry *clip_b = this->list[ clip + 1 ];
863 mlt_producer track_a = NULL;
864 mlt_producer track_b = NULL;
865 mlt_tractor tractor = mlt_tractor_new( );
866 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
868 // Check length is valid for both clips and resize if necessary.
869 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
870 length = length > max_size ? max_size : length;
872 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
873 if ( length != clip_a->frame_count )
874 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
876 track_a = clip_a->producer;
878 if ( length != clip_b->frame_count )
879 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
881 track_b = clip_b->producer;
883 // Set the tracks on the tractor
884 mlt_tractor_set_track( tractor, track_a, 0 );
885 mlt_tractor_set_track( tractor, track_b, 1 );
887 // Insert the mix object into the playlist
888 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
889 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
891 // Attach the transition
892 if ( transition != NULL )
894 mlt_field field = mlt_tractor_field( tractor );
895 mlt_field_plant_transition( field, transition, 0, 1 );
896 mlt_transition_set_in_and_out( transition, 0, length - 1 );
899 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
900 if ( track_a != clip_a->producer )
901 mlt_producer_close( track_a );
902 if ( track_b != clip_b->producer )
903 mlt_producer_close( track_b );
905 // Check if we have anything left on the right hand clip
906 if ( track_b == clip_b->producer )
908 clip_b->preservation_hack = 1;
909 mlt_playlist_remove( this, clip + 2 );
911 else if ( clip_b->frame_out - clip_b->frame_in > length )
913 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
914 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
915 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
919 mlt_producer_clear( clip_b->producer );
920 mlt_playlist_remove( this, clip + 2 );
923 // Check if we have anything left on the left hand clip
924 if ( track_a == clip_a->producer )
926 clip_a->preservation_hack = 1;
927 mlt_playlist_remove( this, clip );
929 else if ( clip_a->frame_out - clip_a->frame_in > length )
931 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
932 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
933 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
937 mlt_producer_clear( clip_a->producer );
938 mlt_playlist_remove( this, clip );
941 // Unblock and force a fire off of change events to listeners
942 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
943 mlt_playlist_virtual_refresh( this );
944 mlt_tractor_close( tractor );
949 /** Add a transition to an existing mix.
952 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
954 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
955 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
956 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
957 int error = transition == NULL || tractor == NULL;
960 mlt_field field = mlt_tractor_field( tractor );
961 mlt_field_plant_transition( field, transition, 0, 1 );
962 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
967 /** Return the clip at the clip index.
970 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
972 if ( clip >= 0 && clip < this->count )
973 return this->list[ clip ]->producer;
977 /** Return the clip at the specified position.
980 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, int position )
982 int index = 0, total = 0;
983 return mlt_playlist_locate( this, &position, &index, &total );
986 /** Return the clip index of the specified position.
989 int mlt_playlist_get_clip_index_at( mlt_playlist this, int position )
991 int index = 0, total = 0;
992 mlt_playlist_locate( this, &position, &index, &total );
996 /** Determine if the clip is a mix.
999 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1001 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1002 mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1003 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1004 return tractor != NULL;
1007 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1008 back correctly on to the playlist.
1011 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1013 int error = ( clip < 0 || clip >= this->count );
1015 // Ensure that the clip request is actually a mix
1018 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1019 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1020 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1021 this->list[ clip ]->preservation_hack;
1026 playlist_entry *mix = this->list[ clip ];
1027 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1028 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1029 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1030 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1031 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1032 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1034 if ( clip_a != NULL )
1036 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1040 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1041 mlt_playlist_insert( this, cut, clip, -1, -1 );
1045 if ( clip_b != NULL )
1047 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1051 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1052 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1055 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1056 mlt_playlist_remove( this, clip );
1057 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1058 mlt_playlist_virtual_refresh( this );
1063 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1065 int error = ( clip < 0 || clip >= this->count );
1067 // Ensure that the clip request is actually a mix
1070 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1071 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1072 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1077 playlist_entry *mix = this->list[ clip ];
1078 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1079 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1080 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1081 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1082 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1083 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1084 int length = out - in + 1;
1085 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1086 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1088 if ( clip_a != NULL )
1089 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1091 if ( clip_b != NULL )
1092 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1094 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1095 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1096 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1097 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1098 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1099 mlt_producer_set_in_and_out( mix->producer, in, out );
1101 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1102 mlt_playlist_virtual_refresh( this );
1107 /** Consolodate adjacent blank producers.
1110 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1115 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1117 mlt_events_block( properties, properties );
1118 for ( i = 1; i < this->count; i ++ )
1120 playlist_entry *left = this->list[ i - 1 ];
1121 playlist_entry *right = this->list[ i ];
1123 if ( mlt_producer_cut_parent( left->producer ) == mlt_producer_cut_parent( right->producer ) &&
1124 mlt_producer_is_blank( left->producer ) )
1126 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1127 mlt_playlist_remove( this, i -- );
1133 playlist_entry *last = this->list[ this->count - 1 ];
1134 if ( mlt_producer_is_blank( last->producer ) )
1135 mlt_playlist_remove( this, this->count - 1 );
1138 mlt_events_unblock( properties, properties );
1139 mlt_playlist_virtual_refresh( this );
1143 /** Determine if the specified clip index is a blank.
1146 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1148 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1151 /** Replace the specified clip with a blank and return the clip.
1154 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1156 mlt_producer producer = NULL;
1157 if ( !mlt_playlist_is_blank( this, clip ) )
1159 playlist_entry *entry = this->list[ clip ];
1160 int in = entry->frame_in;
1161 int out = entry->frame_out;
1162 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1163 producer = entry->producer;
1164 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1165 mlt_events_block( properties, properties );
1166 mlt_playlist_remove( this, clip );
1167 mlt_playlist_blank( this, out - in );
1168 mlt_playlist_move( this, this->count - 1, clip );
1169 mlt_producer_set_in_and_out( producer, in, out );
1170 mlt_events_unblock( properties, properties );
1171 mlt_playlist_virtual_refresh( this );
1176 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1178 if ( this != NULL && length > 0 )
1180 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1181 mlt_events_block( properties, properties );
1182 mlt_playlist_blank( this, length );
1183 mlt_playlist_move( this, this->count - 1, clip );
1184 mlt_events_unblock( properties, properties );
1185 mlt_playlist_virtual_refresh( this );
1189 void mlt_playlist_pad_blanks( mlt_playlist this, int position, int length, int find )
1191 if ( this != NULL && length != 0 )
1193 int clip = mlt_playlist_get_clip_index_at( this, position );
1194 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1195 mlt_events_block( properties, properties );
1196 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1198 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1200 mlt_playlist_clip_info info;
1201 mlt_playlist_get_clip_info( this, &info, clip );
1202 if ( info.frame_out + length > info.frame_in )
1203 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1205 mlt_playlist_remove( this, clip );
1207 else if ( find && clip < this->count && length > 0 )
1209 mlt_playlist_insert_blank( this, clip, length );
1211 mlt_events_unblock( properties, properties );
1212 mlt_playlist_virtual_refresh( this );
1216 int mlt_playlist_insert_at( mlt_playlist this, int position, mlt_producer producer, int mode )
1218 int ret = this == NULL || position < 0 || producer == NULL;
1221 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1222 int length = mlt_producer_get_playtime( producer );
1223 int clip = mlt_playlist_get_clip_index_at( this, position );
1224 mlt_playlist_clip_info info;
1225 mlt_playlist_get_clip_info( this, &info, clip );
1226 mlt_events_block( properties, this );
1227 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1229 mlt_playlist_split( this, clip, position - info.start );
1230 mlt_playlist_get_clip_info( this, &info, ++ clip );
1231 if ( length < info.frame_count )
1232 mlt_playlist_split( this, clip, length );
1233 mlt_playlist_remove( this, clip );
1234 mlt_playlist_insert( this, producer, clip, -1, -1 );
1237 else if ( clip < this->count )
1239 if ( position > info.start + info.frame_count / 2 )
1241 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1243 mlt_playlist_get_clip_info( this, &info, clip );
1244 if ( length < info.frame_count )
1245 mlt_playlist_split( this, clip, length );
1246 mlt_playlist_remove( this, clip );
1248 mlt_playlist_insert( this, producer, clip, -1, -1 );
1254 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) );
1255 mlt_playlist_append( this, producer );
1256 ret = this->count - 1;
1258 mlt_events_unblock( properties, this );
1259 mlt_playlist_virtual_refresh( this );
1268 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1270 mlt_playlist_clip_info info;
1271 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1276 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1278 mlt_playlist_clip_info info;
1279 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1280 return info.frame_count;
1284 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1287 mlt_playlist_clip_info info;
1288 if ( this != NULL && clip < this->count )
1290 mlt_playlist_get_clip_info( this, &info, clip );
1291 if ( mlt_playlist_is_blank( this, clip ) )
1292 count += info.frame_count;
1294 bounded = this->count;
1295 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1297 mlt_playlist_get_clip_info( this, &info, clip );
1298 if ( mlt_playlist_is_blank( this, clip ) )
1299 count += info.frame_count;
1307 int mlt_playlist_remove_region( mlt_playlist this, int position, int length )
1309 int index = mlt_playlist_get_clip_index_at( this, position );
1310 if ( index >= 0 && index < this->count )
1312 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1313 int clip_start = mlt_playlist_clip_start( this, index );
1314 int clip_length = mlt_playlist_clip_length( this, index );
1315 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1316 mlt_events_block( properties, this );
1318 if ( position + length > list_length )
1319 length -= ( position + length - list_length );
1321 if ( clip_start < position )
1323 mlt_playlist_split( this, index ++, position - clip_start );
1324 clip_length -= position - clip_start;
1329 if ( mlt_playlist_clip_length( this, index ) > length )
1330 mlt_playlist_split( this, index, length );
1331 length -= mlt_playlist_clip_length( this, index );
1332 mlt_playlist_remove( this, index );
1335 mlt_playlist_consolidate_blanks( this, 0 );
1336 mlt_events_unblock( properties, this );
1337 mlt_playlist_virtual_refresh( this );
1339 // Just to be sure, we'll get the clip index again...
1340 index = mlt_playlist_get_clip_index_at( this, position );
1345 int mlt_playlist_move_region( mlt_playlist this, int position, int length, int new_position )
1353 /** Get the current frame.
1356 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1358 // Check that we have a producer
1359 if ( producer == NULL )
1361 *frame = mlt_frame_init( );
1365 // Get this mlt_playlist
1366 mlt_playlist this = producer->child;
1368 // Need to ensure the frame is deinterlaced when repeating 1 frame
1369 int progressive = 0;
1371 // Get the real producer
1372 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1374 // Check that we have a producer
1377 *frame = mlt_frame_init( );
1382 mlt_service_get_frame( real, frame, index );
1384 // Check if we're at the end of the clip
1385 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1386 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1387 mlt_playlist_virtual_set_out( this );
1389 // Set the consumer progressive property
1392 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1393 mlt_properties_set_int( properties, "test_audio", 1 );
1396 // Check for notifier and call with appropriate argument
1397 mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1398 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1399 if ( notifier != NULL )
1401 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1402 notifier( argument );
1405 // Update position on the frame we're creating
1406 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1408 // Position ourselves on the next frame
1409 mlt_producer_prepare_next( producer );
1414 /** Close the playlist.
1417 void mlt_playlist_close( mlt_playlist this )
1419 if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1422 this->parent.close = NULL;
1423 for ( i = 0; i < this->count; i ++ )
1425 mlt_event_close( this->list[ i ]->event );
1426 mlt_producer_close( this->list[ i ]->producer );
1427 free( this->list[ i ] );
1429 mlt_producer_close( &this->blank );
1430 mlt_producer_close( &this->parent );