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 typedef 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;
50 /** Private definition.
55 struct mlt_producer_s parent;
56 struct mlt_producer_s blank;
60 playlist_entry **list;
63 /** Forward declarations
66 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
67 static int mlt_playlist_unmix( mlt_playlist this, int clip );
68 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out );
73 mlt_playlist mlt_playlist_init( )
75 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
78 mlt_producer producer = &this->parent;
80 // Construct the producer
81 mlt_producer_init( producer, this );
83 // Override the producer get_frame
84 producer->get_frame = producer_get_frame;
86 // Define the destructor
87 producer->close = ( mlt_destructor )mlt_playlist_close;
88 producer->close_object = this;
91 mlt_producer_init( &this->blank, NULL );
92 mlt_properties_set( mlt_producer_properties( &this->blank ), "mlt_service", "blank" );
93 mlt_properties_set( mlt_producer_properties( &this->blank ), "resource", "blank" );
95 // Indicate that this producer is a playlist
96 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
98 // Specify the eof condition
99 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
100 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
101 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
102 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
103 mlt_properties_set_position( mlt_playlist_properties( this ), "out", -1 );
104 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
107 this->list = malloc( this->size * sizeof( playlist_entry * ) );
113 /** Get the producer associated to this playlist.
116 mlt_producer mlt_playlist_producer( mlt_playlist this )
118 return this != NULL ? &this->parent : NULL;
121 /** Get the service associated to this playlist.
124 mlt_service mlt_playlist_service( mlt_playlist this )
126 return mlt_producer_service( &this->parent );
129 /** Get the propertues associated to this playlist.
132 mlt_properties mlt_playlist_properties( mlt_playlist this )
134 return mlt_producer_properties( &this->parent );
137 /** Refresh the playlist after a clip has been changed.
140 static int mlt_playlist_virtual_refresh( mlt_playlist this )
142 // Obtain the properties
143 mlt_properties properties = mlt_playlist_properties( this );
145 // Get the fps of the first producer
146 double fps = mlt_properties_get_double( properties, "first_fps" );
148 mlt_position frame_count = 0;
150 for ( i = 0; i < this->count; i ++ )
153 mlt_producer producer = this->list[ i ]->producer;
154 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
159 // Inherit it from the producer
160 fps = mlt_producer_get_fps( producer );
162 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
164 // Generate a warning for now - the following attempt to fix may fail
165 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
167 // It should be safe to impose fps on an image producer, but not necessarily safe for video
168 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
171 // Check if the length of the producer has changed
172 if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
173 this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
175 // This clip should be removed...
176 if ( current_length < 1 )
178 this->list[ i ]->frame_in = 0;
179 this->list[ i ]->frame_out = -1;
180 this->list[ i ]->frame_count = 0;
184 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
185 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
186 this->list[ i ]->frame_count = current_length;
189 // Update the producer_length
190 this->list[ i ]->producer_length = current_length;
193 // Calculate the frame_count
194 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
196 // Update the frame_count for this clip
197 frame_count += this->list[ i ]->frame_count;
200 // Refresh all properties
201 mlt_properties_set_double( properties, "first_fps", fps );
202 mlt_properties_set_double( properties, "fps", fps == 0 ? 25 : fps );
203 mlt_events_block( properties, properties );
204 mlt_properties_set_position( properties, "length", frame_count );
205 mlt_events_unblock( properties, properties );
206 mlt_properties_set_position( properties, "out", frame_count - 1 );
211 /** Listener for producers on the playlist.
214 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
216 mlt_playlist_virtual_refresh( this );
219 /** Append to the virtual playlist.
222 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
224 mlt_producer producer = NULL;
225 mlt_properties properties = NULL;
226 mlt_properties parent = NULL;
228 // If we have a cut, then use the in/out points from the cut
229 if ( mlt_producer_is_blank( source ) )
231 // Make sure the blank is long enough to accomodate the length specified
232 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
234 mlt_properties blank_props = mlt_producer_properties( &this->blank );
235 mlt_events_block( blank_props, blank_props );
236 mlt_producer_set_in_and_out( &this->blank, in, out );
237 mlt_events_unblock( blank_props, blank_props );
240 // Now make sure the cut comes from this this->blank
241 if ( source == NULL )
243 producer = mlt_producer_cut( &this->blank, in, out );
245 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
247 producer = mlt_producer_cut( &this->blank, in, out );
252 mlt_properties_inc_ref( mlt_producer_properties( producer ) );
255 properties = mlt_producer_properties( producer );
257 else if ( mlt_producer_is_cut( source ) )
261 in = mlt_producer_get_in( producer );
262 if ( out == -1 || out > mlt_producer_get_out( producer ) )
263 out = mlt_producer_get_out( producer );
264 properties = mlt_producer_properties( producer );
265 mlt_properties_inc_ref( properties );
269 producer = mlt_producer_cut( source, in, out );
270 if ( in == -1 || in < mlt_producer_get_in( producer ) )
271 in = mlt_producer_get_in( producer );
272 if ( out == -1 || out > mlt_producer_get_out( producer ) )
273 out = mlt_producer_get_out( producer );
274 properties = mlt_producer_properties( producer );
277 // Fetch the cuts parent properties
278 parent = mlt_producer_properties( mlt_producer_cut_parent( producer ) );
280 // Check that we have room
281 if ( this->count >= this->size )
284 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
285 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
290 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
291 if ( this->list[ this->count ] != NULL )
293 this->list[ this->count ]->producer = producer;
294 this->list[ this->count ]->frame_in = in;
295 this->list[ this->count ]->frame_out = out;
296 this->list[ this->count ]->frame_count = out - in + 1;
297 this->list[ this->count ]->repeat = 1;
298 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
299 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
300 mlt_event_inc_ref( this->list[ this->count ]->event );
301 mlt_properties_set( properties, "eof", "pause" );
302 mlt_producer_set_speed( producer, 0 );
306 return mlt_playlist_virtual_refresh( this );
309 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
311 // Default producer to NULL
312 mlt_producer producer = NULL;
314 // Loop for each producer until found
315 for ( *clip = 0; *clip < this->count; *clip += 1 )
317 // Increment the total
318 *total += this->list[ *clip ]->frame_count;
320 // Check if the position indicates that we have found the clip
321 // Note that 0 length clips get skipped automatically
322 if ( *position < this->list[ *clip ]->frame_count )
324 // Found it, now break
325 producer = this->list[ *clip ]->producer;
330 // Decrement position by length of this entry
331 *position -= this->list[ *clip ]->frame_count;
338 /** Seek in the virtual playlist.
341 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
343 // Map playlist position to real producer in virtual playlist
344 mlt_position position = mlt_producer_frame( &this->parent );
346 // Keep the original position since we change it while iterating through the list
347 mlt_position original = position;
349 // Clip index and total
353 // Locate the producer for the position
354 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
356 // Get the properties
357 mlt_properties properties = mlt_playlist_properties( this );
359 // Get the eof handling
360 char *eof = mlt_properties_get( properties, "eof" );
362 // Seek in real producer to relative position
363 if ( producer != NULL )
365 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
366 *progressive = count == 1;
367 mlt_producer_seek( producer, position % count );
369 else if ( !strcmp( eof, "pause" ) && total > 0 )
371 playlist_entry *entry = this->list[ this->count - 1 ];
372 int count = entry->frame_count / entry->repeat;
373 mlt_producer this_producer = mlt_playlist_producer( this );
374 mlt_producer_seek( this_producer, original - 1 );
375 producer = entry->producer;
376 mlt_producer_seek( producer, entry->frame_out % count );
377 mlt_producer_set_speed( this_producer, 0 );
378 mlt_producer_set_speed( producer, 0 );
379 *progressive = count == 1;
381 else if ( !strcmp( eof, "loop" ) && total > 0 )
383 playlist_entry *entry = this->list[ 0 ];
384 mlt_producer this_producer = mlt_playlist_producer( this );
385 mlt_producer_seek( this_producer, 0 );
386 producer = entry->producer;
387 mlt_producer_seek( producer, 0 );
391 producer = &this->blank;
394 return mlt_producer_service( producer );
397 /** Invoked when a producer indicates that it has prematurely reached its end.
400 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
402 // Default producer to blank
403 mlt_producer producer = &this->blank;
405 // Map playlist position to real producer in virtual playlist
406 mlt_position position = mlt_producer_frame( &this->parent );
408 // Loop through the virtual playlist
411 for ( i = 0; i < this->count; i ++ )
413 if ( position < this->list[ i ]->frame_count )
415 // Found it, now break
416 producer = this->list[ i ]->producer;
421 // Decrement position by length of this entry
422 position -= this->list[ i ]->frame_count;
426 // Seek in real producer to relative position
427 if ( i < this->count && this->list[ i ]->frame_out != position )
429 // Update the frame_count for the changed clip (hmmm)
430 this->list[ i ]->frame_out = position;
431 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
433 // Refresh the playlist
434 mlt_playlist_virtual_refresh( this );
440 /** Obtain the current clips index.
443 int mlt_playlist_current_clip( mlt_playlist this )
445 // Map playlist position to real producer in virtual playlist
446 mlt_position position = mlt_producer_frame( &this->parent );
448 // Loop through the virtual playlist
451 for ( i = 0; i < this->count; i ++ )
453 if ( position < this->list[ i ]->frame_count )
455 // Found it, now break
460 // Decrement position by length of this entry
461 position -= this->list[ i ]->frame_count;
468 /** Obtain the current clips producer.
471 mlt_producer mlt_playlist_current( mlt_playlist this )
473 int i = mlt_playlist_current_clip( this );
474 if ( i < this->count )
475 return this->list[ i ]->producer;
480 /** Get the position which corresponds to the start of the next clip.
483 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
485 mlt_position position = 0;
486 int absolute_clip = index;
489 // Determine the absolute clip
492 case mlt_whence_relative_start:
493 absolute_clip = index;
496 case mlt_whence_relative_current:
497 absolute_clip = mlt_playlist_current_clip( this ) + index;
500 case mlt_whence_relative_end:
501 absolute_clip = this->count - index;
505 // Check that we're in a valid range
506 if ( absolute_clip < 0 )
508 else if ( absolute_clip > this->count )
509 absolute_clip = this->count;
511 // Now determine the position
512 for ( i = 0; i < absolute_clip; i ++ )
513 position += this->list[ i ]->frame_count;
518 /** Get all the info about the clip specified.
521 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
523 int error = index < 0 || index >= this->count;
524 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
527 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
528 mlt_properties properties = mlt_producer_properties( producer );
530 info->producer = producer;
531 info->cut = this->list[ index ]->producer;
532 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
533 info->resource = mlt_properties_get( properties, "resource" );
534 info->frame_in = this->list[ index ]->frame_in;
535 info->frame_out = this->list[ index ]->frame_out;
536 info->frame_count = this->list[ index ]->frame_count;
537 info->repeat = this->list[ index ]->repeat;
538 info->length = mlt_producer_get_length( producer );
539 info->fps = mlt_producer_get_fps( producer );
545 /** Get number of clips in the playlist.
548 int mlt_playlist_count( mlt_playlist this )
553 /** Clear the playlist.
556 int mlt_playlist_clear( mlt_playlist this )
559 for ( i = 0; i < this->count; i ++ )
561 mlt_event_close( this->list[ i ]->event );
562 mlt_producer_close( this->list[ i ]->producer );
565 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
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 mlt_producer_clear( entry->producer );
666 // Close the producer associated to the clip info
667 mlt_event_close( entry->event );
668 mlt_producer_close( entry->producer );
671 if ( where == current )
672 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
673 else if ( where < current && this->count > 0 )
674 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
675 else if ( this->count == 0 )
676 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
681 // Refresh the playlist
682 mlt_playlist_virtual_refresh( this );
688 /** Move an entry in the playlist.
691 int mlt_playlist_move( mlt_playlist this, int src, int dest )
695 /* We need to ensure that the requested indexes are valid and correct it as necessary */
698 if ( src >= this->count )
699 src = this->count - 1;
703 if ( dest >= this->count )
704 dest = this->count - 1;
706 if ( src != dest && this->count > 1 )
708 int current = mlt_playlist_current_clip( this );
709 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
710 playlist_entry *src_entry = NULL;
712 // We need all the details about the current clip
713 mlt_playlist_clip_info current_info;
715 mlt_playlist_get_clip_info( this, ¤t_info, current );
716 position -= current_info.start;
718 if ( current == src )
720 else if ( current > src && current < dest )
722 else if ( current == dest )
725 src_entry = this->list[ src ];
728 for ( i = src; i > dest; i -- )
729 this->list[ i ] = this->list[ i - 1 ];
733 for ( i = src; i < dest; i ++ )
734 this->list[ i ] = this->list[ i + 1 ];
736 this->list[ dest ] = src_entry;
738 mlt_playlist_get_clip_info( this, ¤t_info, current );
739 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
740 mlt_playlist_virtual_refresh( this );
746 /** Repeat the specified clip n times.
749 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
751 int error = repeat < 1 || clip < 0 || clip >= this->count;
754 playlist_entry *entry = this->list[ clip ];
755 entry->repeat = repeat;
756 mlt_playlist_virtual_refresh( this );
761 /** Resize the specified clip.
764 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
766 int error = clip < 0 || clip >= this->count;
767 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
769 playlist_entry *entry = this->list[ clip ];
770 mlt_producer producer = entry->producer;
772 if ( mlt_producer_is_blank( producer ) )
774 // Make sure the blank is long enough to accomodate the length specified
775 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
777 mlt_properties blank_props = mlt_producer_properties( &this->blank );
778 mlt_events_block( blank_props, blank_props );
779 mlt_producer_set_in_and_out( &this->blank, in, out );
780 mlt_events_unblock( blank_props, blank_props );
786 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
787 out = mlt_producer_get_length( producer ) - 1;
796 mlt_producer_set_in_and_out( producer, in, out );
801 /** Split a clip on the playlist at the given position.
804 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
806 int error = clip < 0 || clip >= this->count;
809 playlist_entry *entry = this->list[ clip ];
810 position = position < 0 ? entry->frame_count + position - 1 : position;
811 if ( position >= 0 && position <= entry->frame_count )
813 int in = entry->frame_in;
814 int out = entry->frame_out;
815 mlt_events_block( mlt_playlist_properties( this ), this );
816 mlt_playlist_resize_clip( this, clip, in, in + position );
817 if ( !mlt_producer_is_blank( entry->producer ) )
819 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
820 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
821 mlt_producer_close( split );
825 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
827 mlt_events_unblock( mlt_playlist_properties( this ), this );
828 mlt_playlist_virtual_refresh( this );
838 /** Join 1 or more consecutive clips.
841 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
843 int error = clip < 0 || ( clip ) >= this->count;
847 mlt_playlist new_clip = mlt_playlist_init( );
848 mlt_events_block( mlt_playlist_properties( this ), this );
849 if ( clip + count >= this->count )
850 count = this->count - clip;
851 for ( i = 0; i <= count; i ++ )
853 playlist_entry *entry = this->list[ clip ];
854 mlt_playlist_append( new_clip, entry->producer );
855 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
856 entry->preservation_hack = 1;
857 mlt_playlist_remove( this, clip );
859 mlt_events_unblock( mlt_playlist_properties( this ), this );
860 mlt_playlist_insert( this, mlt_playlist_producer( new_clip ), clip, 0, -1 );
861 mlt_playlist_close( new_clip );
866 /** Mix consecutive clips for a specified length and apply transition if specified.
869 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
871 int error = ( clip < 0 || clip + 1 >= this->count );
874 playlist_entry *clip_a = this->list[ clip ];
875 playlist_entry *clip_b = this->list[ clip + 1 ];
876 mlt_producer track_a = NULL;
877 mlt_producer track_b = NULL;
878 mlt_tractor tractor = mlt_tractor_new( );
879 mlt_events_block( mlt_playlist_properties( this ), this );
881 // Check length is valid for both clips and resize if necessary.
882 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
883 length = length > max_size ? max_size : length;
885 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
886 if ( length != clip_a->frame_count )
887 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
889 track_a = clip_a->producer;
891 if ( length != clip_b->frame_count )
892 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
894 track_b = clip_b->producer;
896 // Set the tracks on the tractor
897 mlt_tractor_set_track( tractor, track_a, 0 );
898 mlt_tractor_set_track( tractor, track_b, 1 );
900 // Insert the mix object into the playlist
901 mlt_playlist_insert( this, mlt_tractor_producer( tractor ), clip + 1, -1, -1 );
902 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
904 // Attach the transition
905 if ( transition != NULL )
907 mlt_field field = mlt_tractor_field( tractor );
908 mlt_field_plant_transition( field, transition, 0, 1 );
909 mlt_transition_set_in_and_out( transition, 0, length - 1 );
912 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
913 if ( track_a != clip_a->producer )
914 mlt_producer_close( track_a );
915 if ( track_b != clip_b->producer )
916 mlt_producer_close( track_b );
918 // Check if we have anything left on the right hand clip
919 if ( track_b == clip_b->producer )
921 clip_b->preservation_hack = 1;
922 mlt_playlist_remove( this, clip + 2 );
924 else if ( clip_b->frame_out - clip_b->frame_in > length )
926 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
927 mlt_properties_set_data( mlt_producer_properties( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
928 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
932 mlt_producer_clear( clip_b->producer );
933 mlt_playlist_remove( this, clip + 2 );
936 // Check if we have anything left on the left hand clip
937 if ( track_a == clip_a->producer )
939 clip_a->preservation_hack = 1;
940 mlt_playlist_remove( this, clip );
942 else if ( clip_a->frame_out - clip_a->frame_in > length )
944 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
945 mlt_properties_set_data( mlt_producer_properties( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
946 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
950 mlt_producer_clear( clip_a->producer );
951 mlt_playlist_remove( this, clip );
954 // Unblock and force a fire off of change events to listeners
955 mlt_events_unblock( mlt_playlist_properties( this ), this );
956 mlt_playlist_virtual_refresh( this );
957 mlt_tractor_close( tractor );
962 /** Add a transition to an existing mix.
965 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
967 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
968 mlt_properties properties = producer != NULL ? mlt_producer_properties( producer ) : NULL;
969 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
970 int error = transition == NULL || tractor == NULL;
973 mlt_field field = mlt_tractor_field( tractor );
974 mlt_field_plant_transition( field, transition, 0, 1 );
975 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
980 /** Return the clip at the clip index.
983 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
985 if ( clip >= 0 && clip < this->count )
986 return this->list[ clip ]->producer;
990 /** Return the clip at the specified position.
993 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, int position )
995 int index = 0, total = 0;
996 return mlt_playlist_locate( this, &position, &index, &total );
999 /** Return the clip index of the specified position.
1002 int mlt_playlist_get_clip_index_at( mlt_playlist this, int position )
1004 int index = 0, total = 0;
1005 mlt_playlist_locate( this, &position, &index, &total );
1009 /** Determine if the clip is a mix.
1012 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1014 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1015 mlt_properties properties = producer != NULL ? mlt_producer_properties( producer ) : NULL;
1016 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1017 return tractor != NULL;
1020 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1021 back correctly on to the playlist.
1024 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1026 int error = ( clip < 0 || clip >= this->count );
1028 // Ensure that the clip request is actually a mix
1031 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1032 mlt_properties properties = mlt_producer_properties( producer );
1033 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1034 this->list[ clip ]->preservation_hack;
1039 playlist_entry *mix = this->list[ clip ];
1040 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1041 mlt_properties properties = mlt_tractor_properties( tractor );
1042 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1043 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1044 int length = mlt_producer_get_playtime( mlt_tractor_producer( tractor ) );
1045 mlt_events_block( mlt_playlist_properties( this ), this );
1047 if ( clip_a != NULL )
1049 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1053 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1054 mlt_playlist_insert( this, cut, clip, -1, -1 );
1058 if ( clip_b != NULL )
1060 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1064 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1065 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1068 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1069 mlt_playlist_remove( this, clip );
1070 mlt_events_unblock( mlt_playlist_properties( this ), this );
1071 mlt_playlist_virtual_refresh( this );
1076 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1078 int error = ( clip < 0 || clip >= this->count );
1080 // Ensure that the clip request is actually a mix
1083 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1084 mlt_properties properties = mlt_producer_properties( producer );
1085 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1090 playlist_entry *mix = this->list[ clip ];
1091 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1092 mlt_properties properties = mlt_tractor_properties( tractor );
1093 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1094 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1095 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1096 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1097 int length = out - in + 1;
1098 int length_diff = length - mlt_producer_get_playtime( mlt_tractor_producer( tractor ) );
1099 mlt_events_block( mlt_playlist_properties( this ), this );
1101 if ( clip_a != NULL )
1102 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1104 if ( clip_b != NULL )
1105 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1107 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1108 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1109 mlt_producer_set_in_and_out( mlt_multitrack_producer( mlt_tractor_multitrack( tractor ) ), in, out );
1110 mlt_producer_set_in_and_out( mlt_tractor_producer( tractor ), in, out );
1111 mlt_properties_set_position( mlt_producer_properties( mix->producer ), "length", out - in + 1 );
1112 mlt_producer_set_in_and_out( mix->producer, in, out );
1114 mlt_events_unblock( mlt_playlist_properties( this ), this );
1115 mlt_playlist_virtual_refresh( this );
1120 /** Consolodate adjacent blank producers.
1123 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1128 mlt_properties properties = mlt_playlist_properties( this );
1130 mlt_events_block( properties, properties );
1131 for ( i = 1; i < this->count; i ++ )
1133 playlist_entry *left = this->list[ i - 1 ];
1134 playlist_entry *right = this->list[ i ];
1136 if ( mlt_producer_cut_parent( left->producer ) == mlt_producer_cut_parent( right->producer ) &&
1137 mlt_producer_is_blank( left->producer ) )
1139 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1140 mlt_playlist_remove( this, i -- );
1146 playlist_entry *last = this->list[ this->count - 1 ];
1147 if ( mlt_producer_is_blank( last->producer ) )
1148 mlt_playlist_remove( this, this->count - 1 );
1151 mlt_events_unblock( properties, properties );
1152 mlt_playlist_virtual_refresh( this );
1156 /** Determine if the specified clip index is a blank.
1159 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1161 return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1164 /** Replace the specified clip with a blank and return the clip.
1167 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1169 mlt_producer producer = NULL;
1170 if ( !mlt_playlist_is_blank( this, clip ) )
1172 playlist_entry *entry = this->list[ clip ];
1173 int in = entry->frame_in;
1174 int out = entry->frame_out;
1175 mlt_properties properties = mlt_playlist_properties( this );
1176 producer = entry->producer;
1177 mlt_properties_inc_ref( mlt_producer_properties( producer ) );
1178 mlt_events_block( properties, properties );
1179 mlt_playlist_remove( this, clip );
1180 mlt_playlist_blank( this, out - in );
1181 mlt_playlist_move( this, this->count - 1, clip );
1182 mlt_producer_set_in_and_out( producer, in, out );
1183 mlt_events_unblock( properties, properties );
1184 mlt_playlist_virtual_refresh( this );
1189 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1191 if ( this != NULL && length > 0 )
1193 mlt_properties properties = mlt_playlist_properties( this );
1194 mlt_events_block( properties, properties );
1195 mlt_playlist_blank( this, length );
1196 mlt_playlist_move( this, this->count - 1, clip );
1197 mlt_events_unblock( properties, properties );
1198 mlt_playlist_virtual_refresh( this );
1202 void mlt_playlist_pad_blanks( mlt_playlist this, int position, int length, int find )
1204 if ( this != NULL && length != 0 )
1206 int clip = mlt_playlist_get_clip_index_at( this, position );
1207 mlt_properties properties = mlt_playlist_properties( this );
1208 mlt_events_block( properties, properties );
1209 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1211 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1213 mlt_playlist_clip_info info;
1214 mlt_playlist_get_clip_info( this, &info, clip );
1215 if ( info.frame_out + length > info.frame_in )
1216 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1218 mlt_playlist_remove( this, clip );
1220 else if ( find && clip < this->count && length > 0 )
1222 mlt_playlist_insert_blank( this, clip, length );
1224 mlt_events_unblock( properties, properties );
1225 mlt_playlist_virtual_refresh( this );
1229 int mlt_playlist_insert_at( mlt_playlist this, int position, mlt_producer producer, int mode )
1231 int ret = this == NULL || position < 0 || producer == NULL;
1234 mlt_properties properties = mlt_playlist_properties( this );
1235 int length = mlt_producer_get_playtime( producer );
1236 int clip = mlt_playlist_get_clip_index_at( this, position );
1237 mlt_playlist_clip_info info;
1238 mlt_playlist_get_clip_info( this, &info, clip );
1239 mlt_events_block( properties, this );
1240 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1242 mlt_playlist_split( this, clip, position - info.start );
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 );
1247 mlt_playlist_insert( this, producer, clip, -1, -1 );
1250 else if ( clip < this->count )
1252 if ( position > info.start + info.frame_count / 2 )
1254 if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1256 mlt_playlist_get_clip_info( this, &info, clip );
1257 if ( length < info.frame_count )
1258 mlt_playlist_split( this, clip, length );
1259 mlt_playlist_remove( this, clip );
1261 mlt_playlist_insert( this, producer, clip, -1, -1 );
1267 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) );
1268 mlt_playlist_append( this, producer );
1269 ret = this->count - 1;
1271 mlt_events_unblock( properties, this );
1272 mlt_playlist_virtual_refresh( this );
1281 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1283 mlt_playlist_clip_info info;
1284 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1289 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1291 mlt_playlist_clip_info info;
1292 if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1293 return info.frame_count;
1297 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1300 mlt_playlist_clip_info info;
1301 if ( this != NULL && clip < this->count )
1303 mlt_playlist_get_clip_info( this, &info, clip );
1304 if ( mlt_playlist_is_blank( this, clip ) )
1305 count += info.frame_count;
1307 bounded = this->count;
1308 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1310 mlt_playlist_get_clip_info( this, &info, clip );
1311 if ( mlt_playlist_is_blank( this, clip ) )
1312 count += info.frame_count;
1320 int mlt_playlist_remove_region( mlt_playlist this, int position, int length )
1322 int index = mlt_playlist_get_clip_index_at( this, position );
1323 if ( index >= 0 && index < this->count )
1325 mlt_properties properties = mlt_playlist_properties( this );
1326 int clip_start = mlt_playlist_clip_start( this, index );
1327 int clip_length = mlt_playlist_clip_length( this, index );
1328 int list_length = mlt_producer_get_playtime( mlt_playlist_producer( this ) );
1329 mlt_events_block( properties, this );
1331 if ( position + length > list_length )
1332 length -= ( position + length - list_length );
1334 if ( clip_start < position )
1336 mlt_playlist_split( this, index ++, position - clip_start );
1337 clip_length -= position - clip_start;
1342 if ( mlt_playlist_clip_length( this, index ) > length )
1343 mlt_playlist_split( this, index, length );
1344 length -= mlt_playlist_clip_length( this, index );
1345 mlt_playlist_remove( this, index );
1348 mlt_playlist_consolidate_blanks( this, 0 );
1349 mlt_events_unblock( properties, this );
1350 mlt_playlist_virtual_refresh( this );
1352 // Just to be sure, we'll get the clip index again...
1353 index = mlt_playlist_get_clip_index_at( this, position );
1358 int mlt_playlist_move_region( mlt_playlist this, int position, int length, int new_position )
1362 mlt_playlist temp = mlt_playlist_init( );
1363 int original_size = mlt_producer_get_playtime( mlt_playlist_producer( temp ) );
1364 mlt_playlist_close( temp );
1369 /** Get the current frame.
1372 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1374 // Check that we have a producer
1375 if ( producer == NULL )
1377 *frame = mlt_frame_init( );
1381 // Get this mlt_playlist
1382 mlt_playlist this = producer->child;
1384 // Need to ensure the frame is deinterlaced when repeating 1 frame
1385 int progressive = 0;
1387 // Get the real producer
1388 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1390 // Check that we have a producer
1393 *frame = mlt_frame_init( );
1398 mlt_service_get_frame( real, frame, index );
1400 // Check if we're at the end of the clip
1401 mlt_properties properties = mlt_frame_properties( *frame );
1402 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1403 mlt_playlist_virtual_set_out( this );
1405 // Set the consumer progressive property
1408 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1409 mlt_properties_set_int( properties, "test_audio", 1 );
1412 // Check for notifier and call with appropriate argument
1413 mlt_properties playlist_properties = mlt_producer_properties( producer );
1414 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1415 if ( notifier != NULL )
1417 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1418 notifier( argument );
1421 // Update position on the frame we're creating
1422 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1424 // Position ourselves on the next frame
1425 mlt_producer_prepare_next( producer );
1430 /** Close the playlist.
1433 void mlt_playlist_close( mlt_playlist this )
1435 if ( this != NULL && mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
1438 this->parent.close = NULL;
1439 for ( i = 0; i < this->count; i ++ )
1441 mlt_event_close( this->list[ i ]->event );
1442 mlt_producer_close( this->list[ i ]->producer );
1443 free( this->list[ i ] );
1445 mlt_producer_close( &this->blank );
1446 mlt_producer_close( &this->parent );