]> git.sesse.net Git - mlt/blob - src/framework/mlt_playlist.c
add support for time string to playlist blanks
[mlt] / src / framework / mlt_playlist.c
1 /**
2  * \file mlt_playlist.c
3  * \brief playlist service class
4  * \see mlt_playlist_s
5  *
6  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "mlt_playlist.h"
25 #include "mlt_tractor.h"
26 #include "mlt_multitrack.h"
27 #include "mlt_field.h"
28 #include "mlt_frame.h"
29 #include "mlt_transition.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 /** \brief Virtual playlist entry used by mlt_playlist_s
36 */
37
38 struct playlist_entry_s
39 {
40         mlt_producer producer;
41         mlt_position frame_in;
42         mlt_position frame_out;
43         mlt_position frame_count;
44         int repeat;
45         mlt_position producer_length;
46         mlt_event event;
47         int preservation_hack;
48 };
49
50 /* Forward declarations
51 */
52
53 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
54 static int mlt_playlist_unmix( mlt_playlist self, int clip );
55 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out );
56
57 /** Construct a playlist.
58  *
59  * Sets the resource property to "<playlist>".
60  * Set the mlt_type to property to "mlt_producer".
61  * \public \memberof mlt_playlist_s
62  * \return a new playlist
63  */
64
65 mlt_playlist mlt_playlist_init( )
66 {
67         mlt_playlist self = calloc( sizeof( struct mlt_playlist_s ), 1 );
68         if ( self != NULL )
69         {
70                 mlt_producer producer = &self->parent;
71
72                 // Construct the producer
73                 mlt_producer_init( producer, self );
74
75                 // Override the producer get_frame
76                 producer->get_frame = producer_get_frame;
77
78                 // Define the destructor
79                 producer->close = ( mlt_destructor )mlt_playlist_close;
80                 producer->close_object = self;
81
82                 // Initialise blank
83                 mlt_producer_init( &self->blank, NULL );
84                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &self->blank ), "mlt_service", "blank" );
85                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &self->blank ), "resource", "blank" );
86
87                 // Indicate that this producer is a playlist
88                 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "playlist", self, 0, NULL, NULL );
89
90                 // Specify the eof condition
91                 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "eof", "pause" );
92                 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "resource", "<playlist>" );
93                 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( self ), "mlt_type", "mlt_producer" );
94                 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "in", 0 );
95                 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "out", -1 );
96                 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( self ), "length", 0 );
97
98                 self->size = 10;
99                 self->list = malloc( self->size * sizeof( playlist_entry * ) );
100         }
101
102         return self;
103 }
104
105 /** Construct a playlist with a profile.
106  *
107  * Sets the resource property to "<playlist>".
108  * Set the mlt_type to property to "mlt_producer".
109  * \public \memberof mlt_playlist_s
110  * \param profile the profile to use with the profile
111  * \return a new playlist
112  */
113
114 mlt_playlist mlt_playlist_new( mlt_profile profile )
115 {
116     mlt_playlist self = mlt_playlist_init();
117     if ( self )
118         mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( self ), "_profile", profile, 0, NULL, NULL );
119     return self;
120 }
121
122 /** Get the producer associated to this playlist.
123  *
124  * \public \memberof mlt_playlist_s
125  * \param self a playlist
126  * \return the producer interface
127  * \see MLT_PLAYLIST_PRODUCER
128  */
129
130 mlt_producer mlt_playlist_producer( mlt_playlist self )
131 {
132         return self != NULL ? &self->parent : NULL;
133 }
134
135 /** Get the service associated to this playlist.
136  *
137  * \public \memberof mlt_playlist_s
138  * \param self a playlist
139  * \return the service interface
140  * \see MLT_PLAYLIST_SERVICE
141  */
142
143 mlt_service mlt_playlist_service( mlt_playlist self )
144 {
145         return MLT_PRODUCER_SERVICE( &self->parent );
146 }
147
148 /** Get the properties associated to this playlist.
149  *
150  * \public \memberof mlt_playlist_s
151  * \param self a playlist
152  * \return the playlist's properties list
153  * \see MLT_PLAYLIST_PROPERTIES
154  */
155
156 mlt_properties mlt_playlist_properties( mlt_playlist self )
157 {
158         return MLT_PRODUCER_PROPERTIES( &self->parent );
159 }
160
161 /** Refresh the playlist after a clip has been changed.
162  *
163  * \private \memberof mlt_playlist_s
164  * \param self a playlist
165  * \return false
166  */
167
168 static int mlt_playlist_virtual_refresh( mlt_playlist self )
169 {
170         // Obtain the properties
171         mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
172         int i = 0;
173         mlt_position frame_count = 0;
174
175         for ( i = 0; i < self->count; i ++ )
176         {
177                 // Get the producer
178                 mlt_producer producer = self->list[ i ]->producer;
179                 if ( producer )
180                 {
181                         int current_length = mlt_producer_get_playtime( producer );
182
183                         // Check if the length of the producer has changed
184                         if ( self->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
185                                 self->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
186                         {
187                                 // This clip should be removed...
188                                 if ( current_length < 1 )
189                                 {
190                                         self->list[ i ]->frame_in = 0;
191                                         self->list[ i ]->frame_out = -1;
192                                         self->list[ i ]->frame_count = 0;
193                                 }
194                                 else
195                                 {
196                                         self->list[ i ]->frame_in = mlt_producer_get_in( producer );
197                                         self->list[ i ]->frame_out = mlt_producer_get_out( producer );
198                                         self->list[ i ]->frame_count = current_length;
199                                 }
200
201                                 // Update the producer_length
202                                 self->list[ i ]->producer_length = current_length;
203                         }
204                 }
205
206                 // Calculate the frame_count
207                 self->list[ i ]->frame_count = ( self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1 ) * self->list[ i ]->repeat;
208
209                 // Update the frame_count for self clip
210                 frame_count += self->list[ i ]->frame_count;
211         }
212
213         // Refresh all properties
214         mlt_events_block( properties, properties );
215         mlt_properties_set_position( properties, "length", frame_count );
216         mlt_events_unblock( properties, properties );
217         mlt_properties_set_position( properties, "out", frame_count - 1 );
218
219         return 0;
220 }
221
222 /** Listener for producers on the playlist.
223  *
224  * Refreshes the playlist whenever an entry receives producer-changed.
225  * \private \memberof mlt_playlist_s
226  * \param producer a producer
227  * \param self a playlist
228  */
229
230 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist self )
231 {
232         mlt_playlist_virtual_refresh( self );
233 }
234
235 /** Append to the virtual playlist.
236  *
237  * \private \memberof mlt_playlist_s
238  * \param self a playlist
239  * \param source a producer
240  * \param in the producer's starting time
241  * \param out the producer's ending time
242  * \return true if there was an error
243  */
244
245 static int mlt_playlist_virtual_append( mlt_playlist self, mlt_producer source, mlt_position in, mlt_position out )
246 {
247         mlt_producer producer = NULL;
248         mlt_properties properties = NULL;
249         mlt_properties parent = NULL;
250
251         // If we have a cut, then use the in/out points from the cut
252         if ( mlt_producer_is_blank( source )  )
253         {
254                 mlt_position length = out - in + 1;
255
256                 // Make sure the blank is long enough to accomodate the length specified
257                 if ( length > mlt_producer_get_length( &self->blank ) )
258                 {
259                         mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
260                         mlt_events_block( blank_props, blank_props );
261                         mlt_producer_set_in_and_out( &self->blank, in, out );
262                         mlt_events_unblock( blank_props, blank_props );
263                 }
264
265                 // Now make sure the cut comes from this self->blank
266                 if ( source == NULL )
267                 {
268                         producer = mlt_producer_cut( &self->blank, in, out );
269                 }
270                 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &self->blank )
271                 {
272                         producer = mlt_producer_cut( &self->blank, in, out );
273                 }
274                 else
275                 {
276                         producer = source;
277                         mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
278                 }
279
280                 properties = MLT_PRODUCER_PROPERTIES( producer );
281
282                 // Make sure this cut of blank is long enough
283                 if ( length > mlt_producer_get_length( producer ) )
284                         mlt_properties_set_int( properties, "length", length );
285         }
286         else if ( mlt_producer_is_cut( source ) )
287         {
288                 producer = source;
289                 if ( in < 0 )
290                         in = mlt_producer_get_in( producer );
291                 if ( out < 0 || out > mlt_producer_get_out( producer ) )
292                         out = mlt_producer_get_out( producer );
293                 properties = MLT_PRODUCER_PROPERTIES( producer );
294                 mlt_properties_inc_ref( properties );
295         }
296         else
297         {
298                 producer = mlt_producer_cut( source, in, out );
299                 if ( in < 0 || in < mlt_producer_get_in( producer ) )
300                         in = mlt_producer_get_in( producer );
301                 if ( out < 0 || out > mlt_producer_get_out( producer ) )
302                         out = mlt_producer_get_out( producer );
303                 properties = MLT_PRODUCER_PROPERTIES( producer );
304         }
305
306         // Fetch the cuts parent properties
307         parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
308
309         // Remove loader normalisers for fx cuts
310         if ( mlt_properties_get_int( parent, "meta.fx_cut" ) )
311         {
312                 mlt_service service = MLT_PRODUCER_SERVICE( mlt_producer_cut_parent( producer ) );
313                 mlt_filter filter = mlt_service_filter( service, 0 );
314                 while ( filter != NULL && mlt_properties_get_int( MLT_FILTER_PROPERTIES( filter ), "_loader" ) )
315                 {
316                         mlt_service_detach( service, filter );
317                         filter = mlt_service_filter( service, 0 );
318                 }
319                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "meta.fx_cut", 1 );
320         }
321
322         // Check that we have room
323         if ( self->count >= self->size )
324         {
325                 int i;
326                 self->list = realloc( self->list, ( self->size + 10 ) * sizeof( playlist_entry * ) );
327                 for ( i = self->size; i < self->size + 10; i ++ ) self->list[ i ] = NULL;
328                 self->size += 10;
329         }
330
331         // Create the entry
332         self->list[ self->count ] = calloc( sizeof( playlist_entry ), 1 );
333         if ( self->list[ self->count ] != NULL )
334         {
335                 self->list[ self->count ]->producer = producer;
336                 self->list[ self->count ]->frame_in = in;
337                 self->list[ self->count ]->frame_out = out;
338                 self->list[ self->count ]->frame_count = out - in + 1;
339                 self->list[ self->count ]->repeat = 1;
340                 self->list[ self->count ]->producer_length = mlt_producer_get_playtime( producer );
341                 self->list[ self->count ]->event = mlt_events_listen( parent, self, "producer-changed", ( mlt_listener )mlt_playlist_listener );
342                 mlt_event_inc_ref( self->list[ self->count ]->event );
343                 mlt_properties_set( properties, "eof", "pause" );
344                 mlt_producer_set_speed( producer, 0 );
345                 self->count ++;
346         }
347
348         return mlt_playlist_virtual_refresh( self );
349 }
350
351 /** Locate a producer by index.
352  *
353  * \private \memberof mlt_playlist_s
354  * \param self a playlist
355  * \param[in, out] position the time at which to locate the producer, returns the time relative to the producer's starting point
356  * \param[out] clip the index of the playlist entry
357  * \param[out] total the duration of the playlist up to and including this producer
358  * \return a producer or NULL if not found
359  */
360
361 static mlt_producer mlt_playlist_locate( mlt_playlist self, mlt_position *position, int *clip, int *total )
362 {
363         // Default producer to NULL
364         mlt_producer producer = NULL;
365
366         // Loop for each producer until found
367         for ( *clip = 0; *clip < self->count; *clip += 1 )
368         {
369                 // Increment the total
370                 *total += self->list[ *clip ]->frame_count;
371
372                 // Check if the position indicates that we have found the clip
373                 // Note that 0 length clips get skipped automatically
374                 if ( *position < self->list[ *clip ]->frame_count )
375                 {
376                         // Found it, now break
377                         producer = self->list[ *clip ]->producer;
378                         break;
379                 }
380                 else
381                 {
382                         // Decrement position by length of self entry
383                         *position -= self->list[ *clip ]->frame_count;
384                 }
385         }
386
387         return producer;
388 }
389
390 /** Seek in the virtual playlist.
391  *
392  * This gets the producer at the current position and seeks on the producer
393  * while doing repeat and end-of-file handling. This is also responsible for
394  * closing producers previous to the preceding playlist if the autoclose
395  * property is set.
396  * \private \memberof mlt_playlist_s
397  * \param self a playlist
398  * \param[out] progressive true if the producer should be displayed progressively
399  * \return the service interface of the producer at the play head
400  * \see producer_get_frame
401  */
402
403 static mlt_service mlt_playlist_virtual_seek( mlt_playlist self, int *progressive )
404 {
405         // Map playlist position to real producer in virtual playlist
406         mlt_position position = mlt_producer_frame( &self->parent );
407
408         // Keep the original position since we change it while iterating through the list
409         mlt_position original = position;
410
411         // Clip index and total
412         int i = 0;
413         int total = 0;
414
415         // Locate the producer for the position
416         mlt_producer producer = mlt_playlist_locate( self, &position, &i, &total );
417
418         // Get the properties
419         mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
420
421         // Automatically close previous producers if requested
422         if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
423                 && position < 2 // tolerate off-by-one error on going to next clip
424                 && mlt_properties_get_int( properties, "autoclose" ) )
425         {
426                 int j;
427                 // They might have jumped ahead!
428                 for ( j = 0; j < i - 1; j++ )
429                 {
430                         mlt_service_lock( MLT_PRODUCER_SERVICE( self->list[ j ]->producer ) );
431                         mlt_producer p = self->list[ j ]->producer;
432                         if ( p )
433                         {
434                                 self->list[ j ]->producer = NULL;
435                                 mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
436                                 mlt_producer_close( p );
437                         }
438                         // If p is null, the lock will not have been "taken"
439                 }
440         }
441
442         // Get the eof handling
443         char *eof = mlt_properties_get( properties, "eof" );
444
445         // Seek in real producer to relative position
446         if ( producer != NULL )
447         {
448                 int count = self->list[ i ]->frame_count / self->list[ i ]->repeat;
449                 *progressive = count == 1;
450                 mlt_producer_seek( producer, (int)position % count );
451         }
452         else if ( !strcmp( eof, "pause" ) && total > 0 )
453         {
454                 playlist_entry *entry = self->list[ self->count - 1 ];
455                 int count = entry->frame_count / entry->repeat;
456                 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
457                 mlt_producer_seek( self_producer, original - 1 );
458                 producer = entry->producer;
459                 mlt_producer_seek( producer, (int)entry->frame_out % count );
460                 mlt_producer_set_speed( self_producer, 0 );
461                 mlt_producer_set_speed( producer, 0 );
462                 *progressive = count == 1;
463         }
464         else if ( !strcmp( eof, "loop" ) && total > 0 )
465         {
466                 playlist_entry *entry = self->list[ 0 ];
467                 mlt_producer self_producer = MLT_PLAYLIST_PRODUCER( self );
468                 mlt_producer_seek( self_producer, 0 );
469                 producer = entry->producer;
470                 mlt_producer_seek( producer, 0 );
471         }
472         else
473         {
474                 producer = &self->blank;
475         }
476
477         return MLT_PRODUCER_SERVICE( producer );
478 }
479
480 /** Invoked when a producer indicates that it has prematurely reached its end.
481  *
482  * \private \memberof mlt_playlist_s
483  * \param self a playlist
484  * \return a producer
485  * \see producer_get_frame
486  */
487
488 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist self )
489 {
490         // Default producer to blank
491         mlt_producer producer = &self->blank;
492
493         // Map playlist position to real producer in virtual playlist
494         mlt_position position = mlt_producer_frame( &self->parent );
495
496         // Loop through the virtual playlist
497         int i = 0;
498
499         for ( i = 0; i < self->count; i ++ )
500         {
501                 if ( position < self->list[ i ]->frame_count )
502                 {
503                         // Found it, now break
504                         producer = self->list[ i ]->producer;
505                         break;
506                 }
507                 else
508                 {
509                         // Decrement position by length of this entry
510                         position -= self->list[ i ]->frame_count;
511                 }
512         }
513
514         // Seek in real producer to relative position
515         if ( i < self->count && self->list[ i ]->frame_out != position )
516         {
517                 // Update the frame_count for the changed clip (hmmm)
518                 self->list[ i ]->frame_out = position;
519                 self->list[ i ]->frame_count = self->list[ i ]->frame_out - self->list[ i ]->frame_in + 1;
520
521                 // Refresh the playlist
522                 mlt_playlist_virtual_refresh( self );
523         }
524
525         return producer;
526 }
527
528 /** Obtain the current clips index.
529  *
530  * \public \memberof mlt_playlist_s
531  * \param self a playlist
532  * \return the index of the playlist entry at the current position
533  */
534
535 int mlt_playlist_current_clip( mlt_playlist self )
536 {
537         // Map playlist position to real producer in virtual playlist
538         mlt_position position = mlt_producer_frame( &self->parent );
539
540         // Loop through the virtual playlist
541         int i = 0;
542
543         for ( i = 0; i < self->count; i ++ )
544         {
545                 if ( position < self->list[ i ]->frame_count )
546                 {
547                         // Found it, now break
548                         break;
549                 }
550                 else
551                 {
552                         // Decrement position by length of this entry
553                         position -= self->list[ i ]->frame_count;
554                 }
555         }
556
557         return i;
558 }
559
560 /** Obtain the current clips producer.
561  *
562  * \public \memberof mlt_playlist_s
563  * \param self a playlist
564  * \return the producer at the current position
565  */
566
567 mlt_producer mlt_playlist_current( mlt_playlist self )
568 {
569         int i = mlt_playlist_current_clip( self );
570         if ( i < self->count )
571                 return self->list[ i ]->producer;
572         else
573                 return &self->blank;
574 }
575
576 /** Get the position which corresponds to the start of the next clip.
577  *
578  * \public \memberof mlt_playlist_s
579  * \param self a playlist
580  * \param whence the location from which to make the index relative:
581  * start of playlist, end of playlist, or current position
582  * \param index the playlist entry index relative to whence
583  * \return the time at which the referenced clip starts
584  */
585
586 mlt_position mlt_playlist_clip( mlt_playlist self, mlt_whence whence, int index )
587 {
588         mlt_position position = 0;
589         int absolute_clip = index;
590         int i = 0;
591
592         // Determine the absolute clip
593         switch ( whence )
594         {
595                 case mlt_whence_relative_start:
596                         absolute_clip = index;
597                         break;
598
599                 case mlt_whence_relative_current:
600                         absolute_clip = mlt_playlist_current_clip( self ) + index;
601                         break;
602
603                 case mlt_whence_relative_end:
604                         absolute_clip = self->count - index;
605                         break;
606         }
607
608         // Check that we're in a valid range
609         if ( absolute_clip < 0 )
610                 absolute_clip = 0;
611         else if ( absolute_clip > self->count )
612                 absolute_clip = self->count;
613
614         // Now determine the position
615         for ( i = 0; i < absolute_clip; i ++ )
616                 position += self->list[ i ]->frame_count;
617
618         return position;
619 }
620
621 /** Get all the info about the clip specified.
622  *
623  * \public \memberof mlt_playlist_s
624  * \param self a playlist
625  * \param info a clip info struct
626  * \param index a playlist entry index
627  * \return true if there was an error
628  */
629
630 int mlt_playlist_get_clip_info( mlt_playlist self, mlt_playlist_clip_info *info, int index )
631 {
632         int error = index < 0 || index >= self->count || self->list[ index ]->producer == NULL;
633         memset( info, 0, sizeof( mlt_playlist_clip_info ) );
634         if ( !error )
635         {
636                 mlt_producer producer = mlt_producer_cut_parent( self->list[ index ]->producer );
637                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
638                 info->clip = index;
639                 info->producer = producer;
640                 info->cut = self->list[ index ]->producer;
641                 info->start = mlt_playlist_clip( self, mlt_whence_relative_start, index );
642                 info->resource = mlt_properties_get( properties, "resource" );
643                 info->frame_in = self->list[ index ]->frame_in;
644                 info->frame_out = self->list[ index ]->frame_out;
645                 info->frame_count = self->list[ index ]->frame_count;
646                 info->repeat = self->list[ index ]->repeat;
647                 info->length = mlt_producer_get_length( producer );
648                 info->fps = mlt_producer_get_fps( producer );
649         }
650
651         return error;
652 }
653
654 /** Get number of clips in the playlist.
655  *
656  * \public \memberof mlt_playlist_s
657  * \param self a playlist
658  * \return the number of playlist entries
659  */
660
661 int mlt_playlist_count( mlt_playlist self )
662 {
663         return self->count;
664 }
665
666 /** Clear the playlist.
667  *
668  * \public \memberof mlt_playlist_s
669  * \param self a playlist
670  * \return true if there was an error
671  */
672
673 int mlt_playlist_clear( mlt_playlist self )
674 {
675         int i;
676         for ( i = 0; i < self->count; i ++ )
677         {
678                 mlt_event_close( self->list[ i ]->event );
679                 mlt_producer_close( self->list[ i ]->producer );
680         }
681         self->count = 0;
682         return mlt_playlist_virtual_refresh( self );
683 }
684
685 /** Append a producer to the playlist.
686  *
687  * \public \memberof mlt_playlist_s
688  * \param self a playlist
689  * \param producer the producer to append
690  * \return true if there was an error
691  */
692
693 int mlt_playlist_append( mlt_playlist self, mlt_producer producer )
694 {
695         // Append to virtual list
696         return mlt_playlist_virtual_append( self, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
697 }
698
699 /** Append a producer to the playlist with in/out points.
700  *
701  * \public \memberof mlt_playlist_s
702  * \param self a playlist
703  * \param producer the producer to append
704  * \param in the starting point on the producer; a negative value is the same as 0
705  * \param out the ending point on the producer; a negative value is the same as producer length - 1
706  * \return true if there was an error
707  */
708
709 int mlt_playlist_append_io( mlt_playlist self, mlt_producer producer, mlt_position in, mlt_position out )
710 {
711         // Append to virtual list
712         if ( in < 0 && out < 0 )
713                 return mlt_playlist_append( self, producer );
714         else
715                 return mlt_playlist_virtual_append( self, producer, in, out );
716 }
717
718 /** Append a blank to the playlist of a given length.
719  *
720  * \public \memberof mlt_playlist_s
721  * \param self a playlist
722  * \param out the ending time of the blank entry, not its duration
723  * \return true if there was an error
724  */
725
726 int mlt_playlist_blank( mlt_playlist self, mlt_position out )
727 {
728         // Append to the virtual list
729         if ( out >= 0 )
730                 return mlt_playlist_virtual_append( self, &self->blank, 0, out );
731         else
732                 return 1;
733 }
734
735 /** Append a blank item to the playlist with duration as a time string.
736  *
737  * \public \memberof mlt_playlist_s
738  * \param self a playlist
739  * \param length the duration of the blank entry as a time string
740  * \return true if there was an error
741  */
742
743 int mlt_playlist_blank_time( mlt_playlist self, const char* length )
744 {
745         if ( self && length )
746         {
747                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
748                 mlt_properties_set( properties , "_blank_time", length );
749                 mlt_position duration = mlt_properties_get_position( properties, "_blank_time" );
750                 return mlt_playlist_blank( self, duration - 1 );
751         }
752         else
753                 return 1;
754 }
755
756 /** Insert a producer into the playlist.
757  *
758  * \public \memberof mlt_playlist_s
759  * \param self a playlist
760  * \param producer the producer to insert
761  * \param where the producer's playlist entry index
762  * \param in the starting point on the producer
763  * \param out the ending point on the producer
764  * \return true if there was an error
765  */
766
767 int mlt_playlist_insert( mlt_playlist self, mlt_producer producer, int where, mlt_position in, mlt_position out )
768 {
769         // Append to end
770         mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
771         mlt_playlist_append_io( self, producer, in, out );
772
773         // Move to the position specified
774         mlt_playlist_move( self, self->count - 1, where );
775         mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
776
777         return mlt_playlist_virtual_refresh( self );
778 }
779
780 /** Remove an entry in the playlist.
781  *
782  * \public \memberof mlt_playlist_s
783  * \param self a playlist
784  * \param where the playlist entry index
785  * \return true if there was an error
786  */
787
788 int mlt_playlist_remove( mlt_playlist self, int where )
789 {
790         int error = where < 0 || where >= self->count;
791         if ( error == 0 && mlt_playlist_unmix( self, where ) != 0 )
792         {
793                 // We need to know the current clip and the position within the playlist
794                 int current = mlt_playlist_current_clip( self );
795                 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
796
797                 // We need all the details about the clip we're removing
798                 mlt_playlist_clip_info where_info;
799                 playlist_entry *entry = self->list[ where ];
800                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
801
802                 // Loop variable
803                 int i = 0;
804
805                 // Get the clip info
806                 mlt_playlist_get_clip_info( self, &where_info, where );
807
808                 // Make sure the clip to be removed is valid and correct if necessary
809                 if ( where < 0 )
810                         where = 0;
811                 if ( where >= self->count )
812                         where = self->count - 1;
813
814                 // Reorganise the list
815                 for ( i = where + 1; i < self->count; i ++ )
816                         self->list[ i - 1 ] = self->list[ i ];
817                 self->count --;
818
819                 if ( entry->preservation_hack == 0 )
820                 {
821                         // Decouple from mix_in/out if necessary
822                         if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
823                         {
824                                 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
825                                 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
826                         }
827                         if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
828                         {
829                                 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
830                                 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
831                         }
832
833                         if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
834                                 mlt_producer_clear( entry->producer );
835                 }
836
837                 // Close the producer associated to the clip info
838                 mlt_event_close( entry->event );
839                 mlt_producer_close( entry->producer );
840
841                 // Correct position
842                 if ( where == current )
843                         mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), where_info.start );
844                 else if ( where < current && self->count > 0 )
845                         mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), position - where_info.frame_count );
846                 else if ( self->count == 0 )
847                         mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), 0 );
848
849                 // Free the entry
850                 free( entry );
851
852                 // Refresh the playlist
853                 mlt_playlist_virtual_refresh( self );
854         }
855
856         return error;
857 }
858
859 /** Move an entry in the playlist.
860  *
861  * \public \memberof mlt_playlist_s
862  * \param self a playlist
863  * \param src an entry index
864  * \param dest an entry index
865  * \return false
866  */
867
868 int mlt_playlist_move( mlt_playlist self, int src, int dest )
869 {
870         int i;
871
872         /* We need to ensure that the requested indexes are valid and correct it as necessary */
873         if ( src < 0 )
874                 src = 0;
875         if ( src >= self->count )
876                 src = self->count - 1;
877
878         if ( dest < 0 )
879                 dest = 0;
880         if ( dest >= self->count )
881                 dest = self->count - 1;
882
883         if ( src != dest && self->count > 1 )
884         {
885                 int current = mlt_playlist_current_clip( self );
886                 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( self ) );
887                 playlist_entry *src_entry = NULL;
888
889                 // We need all the details about the current clip
890                 mlt_playlist_clip_info current_info;
891
892                 mlt_playlist_get_clip_info( self, &current_info, current );
893                 position -= current_info.start;
894
895                 if ( current == src )
896                         current = dest;
897                 else if ( current > src && current < dest )
898                         current ++;
899                 else if ( current == dest )
900                         current = src;
901
902                 src_entry = self->list[ src ];
903                 if ( src > dest )
904                 {
905                         for ( i = src; i > dest; i -- )
906                                 self->list[ i ] = self->list[ i - 1 ];
907                 }
908                 else
909                 {
910                         for ( i = src; i < dest; i ++ )
911                                 self->list[ i ] = self->list[ i + 1 ];
912                 }
913                 self->list[ dest ] = src_entry;
914
915                 mlt_playlist_get_clip_info( self, &current_info, current );
916                 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( self ), current_info.start + position );
917                 mlt_playlist_virtual_refresh( self );
918         }
919
920         return 0;
921 }
922
923 /** Repeat the specified clip n times.
924  *
925  * \public \memberof mlt_playlist_s
926  * \param self a playlist
927  * \param clip a playlist entry index
928  * \param repeat the number of times to repeat the clip
929  * \return true if there was an error
930  */
931
932 int mlt_playlist_repeat_clip( mlt_playlist self, int clip, int repeat )
933 {
934         int error = repeat < 1 || clip < 0 || clip >= self->count;
935         if ( error == 0 )
936         {
937                 playlist_entry *entry = self->list[ clip ];
938                 entry->repeat = repeat;
939                 mlt_playlist_virtual_refresh( self );
940         }
941         return error;
942 }
943
944 /** Resize the specified clip.
945  *
946  * \public \memberof mlt_playlist_s
947  * \param self a playlist
948  * \param clip the index of the playlist entry
949  * \param in the new starting time on the clip's producer;  a negative value is the same as 0
950  * \param out the new ending time on the clip's producer;  a negative value is the same as length - 1
951  * \return true if there was an error
952  */
953
954 int mlt_playlist_resize_clip( mlt_playlist self, int clip, mlt_position in, mlt_position out )
955 {
956         int error = clip < 0 || clip >= self->count;
957         if ( error == 0 && mlt_playlist_resize_mix( self, clip, in, out ) != 0 )
958         {
959                 playlist_entry *entry = self->list[ clip ];
960                 mlt_producer producer = entry->producer;
961                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
962
963                 mlt_events_block( properties, properties );
964
965                 if ( mlt_producer_is_blank( producer ) )
966                 {
967                         mlt_position length = out - in + 1;
968
969                         // Make sure the parent blank is long enough to accomodate the length specified
970                         if ( length > mlt_producer_get_length( &self->blank ) )
971                         {
972                                 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &self->blank );
973                                 mlt_properties_set_int( blank_props, "length", length );
974                                 mlt_producer_set_in_and_out( &self->blank, 0, out - in );
975                         }
976                         // Make sure this cut of blank is long enough
977                         if ( length > mlt_producer_get_length( producer ) )
978                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", length );
979                 }
980
981                 if ( in < 0 )
982                         in = 0;
983                 if ( out < 0 || out >= mlt_producer_get_length( producer ) )
984                         out = mlt_producer_get_length( producer ) - 1;
985
986                 if ( out < in )
987                 {
988                         mlt_position t = in;
989                         in = out;
990                         out = t;
991                 }
992
993                 mlt_producer_set_in_and_out( producer, in, out );
994                 mlt_events_unblock( properties, properties );
995                 mlt_playlist_virtual_refresh( self );
996         }
997         return error;
998 }
999
1000 /** Split a clip on the playlist at the given position.
1001  *
1002  * This splits after the specified frame.
1003  * \public \memberof mlt_playlist_s
1004  * \param self a playlist
1005  * \param clip the index of the playlist entry
1006  * \param position the time at which to split relative to the beginning of the clip or its end if negative
1007  * \return true if there was an error
1008  */
1009
1010 int mlt_playlist_split( mlt_playlist self, int clip, mlt_position position )
1011 {
1012         int error = clip < 0 || clip >= self->count;
1013         if ( error == 0 )
1014         {
1015                 playlist_entry *entry = self->list[ clip ];
1016                 position = position < 0 ? entry->frame_count + position - 1 : position;
1017                 if ( position >= 0 && position < entry->frame_count - 1 )
1018                 {
1019                         int in = entry->frame_in;
1020                         int out = entry->frame_out;
1021                         mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1022                         mlt_playlist_resize_clip( self, clip, in, in + position );
1023                         if ( !mlt_producer_is_blank( entry->producer ) )
1024                         {
1025                                 int i = 0;
1026                                 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
1027                                 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
1028                                 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
1029                                 mlt_playlist_insert( self, split, clip + 1, 0, -1 );
1030                                 mlt_properties_lock( entry_properties );
1031                                 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
1032                                 {
1033                                         char *name = mlt_properties_get_name( entry_properties, i );
1034                                         if ( name != NULL && !strncmp( name, "meta.", 5 ) )
1035                                                 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
1036                                 }
1037                                 mlt_properties_unlock( entry_properties );
1038                                 mlt_producer_close( split );
1039                         }
1040                         else
1041                         {
1042                                 mlt_playlist_insert( self, &self->blank, clip + 1, 0, out - position - 1 );
1043                         }
1044                         mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1045                         mlt_playlist_virtual_refresh( self );
1046                 }
1047                 else
1048                 {
1049                         error = 1;
1050                 }
1051         }
1052         return error;
1053 }
1054
1055 /** Split the playlist at the absolute position.
1056  *
1057  * \public \memberof mlt_playlist_s
1058  * \param self a playlist
1059  * \param position the time at which to split relative to the beginning of the clip
1060  * \param left true to split before the frame starting at position
1061  * \return true if there was an error
1062  */
1063
1064 int mlt_playlist_split_at( mlt_playlist self, mlt_position position, int left )
1065 {
1066         int result = self == NULL ? -1 : 0;
1067         if ( !result )
1068         {
1069                 if ( position >= 0 && position < mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) ) )
1070                 {
1071                         int clip = mlt_playlist_get_clip_index_at( self, position );
1072                         mlt_playlist_clip_info info;
1073                         mlt_playlist_get_clip_info( self, &info, clip );
1074                         if ( left && position != info.start )
1075                                 mlt_playlist_split( self, clip, position - info.start - 1 );
1076                         else if ( !left )
1077                                 mlt_playlist_split( self, clip, position - info.start );
1078                         result = position;
1079                 }
1080                 else if ( position <= 0 )
1081                 {
1082                         result = 0;
1083                 }
1084                 else
1085                 {
1086                         result = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1087                 }
1088         }
1089         return result;
1090 }
1091
1092 /** Join 1 or more consecutive clips.
1093  *
1094  * \public \memberof mlt_playlist_s
1095  * \param self a playlist
1096  * \param clip the starting playlist entry index
1097  * \param count the number of entries to merge
1098  * \param merge ignored
1099  * \return true if there was an error
1100  */
1101
1102 int mlt_playlist_join( mlt_playlist self, int clip, int count, int merge )
1103 {
1104         int error = clip < 0 || clip >= self->count;
1105         if ( error == 0 )
1106         {
1107                 int i = clip;
1108                 mlt_playlist new_clip = mlt_playlist_new( mlt_service_profile( MLT_PLAYLIST_SERVICE(self) ) );
1109                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1110                 if ( clip + count >= self->count )
1111                         count = self->count - clip - 1;
1112                 for ( i = 0; i <= count; i ++ )
1113                 {
1114                         playlist_entry *entry = self->list[ clip ];
1115                         mlt_playlist_append( new_clip, entry->producer );
1116                         mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
1117                         entry->preservation_hack = 1;
1118                         mlt_playlist_remove( self, clip );
1119                 }
1120                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1121                 mlt_playlist_insert( self, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
1122                 mlt_playlist_close( new_clip );
1123         }
1124         return error;
1125 }
1126
1127 /** Mix consecutive clips for a specified length and apply transition if specified.
1128  *
1129  * \public \memberof mlt_playlist_s
1130  * \param self a playlist
1131  * \param clip the index of the playlist entry
1132  * \param length the number of frames over which to create the mix
1133  * \param transition the transition to use for the mix
1134  * \return true if there was an error
1135  */
1136
1137 int mlt_playlist_mix( mlt_playlist self, int clip, int length, mlt_transition transition )
1138 {
1139         int error = ( clip < 0 || clip + 1 >= self->count );
1140         if ( error == 0 )
1141         {
1142                 playlist_entry *clip_a = self->list[ clip ];
1143                 playlist_entry *clip_b = self->list[ clip + 1 ];
1144                 mlt_producer track_a = NULL;
1145                 mlt_producer track_b = NULL;
1146                 mlt_tractor tractor = mlt_tractor_new( );
1147                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1148
1149                 // Check length is valid for both clips and resize if necessary.
1150                 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
1151                 length = length > max_size ? max_size : length;
1152
1153                 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
1154                 if ( length != clip_a->frame_count )
1155                         track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
1156                 else
1157                         track_a = clip_a->producer;
1158
1159                 if ( length != clip_b->frame_count )
1160                         track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
1161                 else
1162                         track_b = clip_b->producer;
1163
1164                 // Set the tracks on the tractor
1165                 mlt_tractor_set_track( tractor, track_a, 0 );
1166                 mlt_tractor_set_track( tractor, track_b, 1 );
1167
1168                 // Insert the mix object into the playlist
1169                 mlt_playlist_insert( self, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
1170                 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
1171
1172                 // Attach the transition
1173                 if ( transition != NULL )
1174                 {
1175                         mlt_field field = mlt_tractor_field( tractor );
1176                         mlt_field_plant_transition( field, transition, 0, 1 );
1177                         mlt_transition_set_in_and_out( transition, 0, length - 1 );
1178                 }
1179
1180                 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
1181                 if ( track_a != clip_a->producer )
1182                         mlt_producer_close( track_a );
1183                 if ( track_b != clip_b->producer )
1184                         mlt_producer_close( track_b );
1185
1186                 // Check if we have anything left on the right hand clip
1187                 if ( track_b == clip_b->producer )
1188                 {
1189                         clip_b->preservation_hack = 1;
1190                         mlt_playlist_remove( self, clip + 2 );
1191                 }
1192                 else if ( clip_b->frame_out - clip_b->frame_in > length )
1193                 {
1194                         mlt_playlist_resize_clip( self, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
1195                         mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
1196                         mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
1197                 }
1198                 else
1199                 {
1200                         mlt_producer_clear( clip_b->producer );
1201                         mlt_playlist_remove( self, clip + 2 );
1202                 }
1203
1204                 // Check if we have anything left on the left hand clip
1205                 if ( track_a == clip_a->producer )
1206                 {
1207                         clip_a->preservation_hack = 1;
1208                         mlt_playlist_remove( self, clip );
1209                 }
1210                 else if ( clip_a->frame_out - clip_a->frame_in > length )
1211                 {
1212                         mlt_playlist_resize_clip( self, clip, clip_a->frame_in, clip_a->frame_out - length );
1213                         mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
1214                         mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
1215                 }
1216                 else
1217                 {
1218                         mlt_producer_clear( clip_a->producer );
1219                         mlt_playlist_remove( self, clip );
1220                 }
1221
1222                 // Unblock and force a fire off of change events to listeners
1223                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1224                 mlt_playlist_virtual_refresh( self );
1225                 mlt_tractor_close( tractor );
1226         }
1227         return error;
1228 }
1229
1230 /** Add a transition to an existing mix.
1231  *
1232  * \public \memberof mlt_playlist_s
1233  * \param self a playlist
1234  * \param clip the index of the playlist entry
1235  * \param transition a transition
1236  * \return true if there was an error
1237  */
1238
1239 int mlt_playlist_mix_add( mlt_playlist self, int clip, mlt_transition transition )
1240 {
1241         mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1242         mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1243         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1244         int error = transition == NULL || tractor == NULL;
1245         if ( error == 0 )
1246         {
1247                 mlt_field field = mlt_tractor_field( tractor );
1248                 mlt_field_plant_transition( field, transition, 0, 1 );
1249                 mlt_transition_set_in_and_out( transition, 0, self->list[ clip ]->frame_count - 1 );
1250         }
1251         return error;
1252 }
1253
1254 /** Return the clip at the clip index.
1255  *
1256  * \public \memberof mlt_playlist_s
1257  * \param self a playlist
1258  * \param clip the index of a playlist entry
1259  * \return a producer or NULL if there was an error
1260  */
1261
1262 mlt_producer mlt_playlist_get_clip( mlt_playlist self, int clip )
1263 {
1264         if ( clip >= 0 && clip < self->count )
1265                 return self->list[ clip ]->producer;
1266         return NULL;
1267 }
1268
1269 /** Return the clip at the specified position.
1270  *
1271  * \public \memberof mlt_playlist_s
1272  * \param self a playlist
1273  * \param position a time relative to the beginning of the playlist
1274  * \return a producer or NULL if not found
1275  */
1276
1277 mlt_producer mlt_playlist_get_clip_at( mlt_playlist self, mlt_position position )
1278 {
1279         int index = 0, total = 0;
1280         return mlt_playlist_locate( self, &position, &index, &total );
1281 }
1282
1283 /** Return the clip index of the specified position.
1284  *
1285  * \public \memberof mlt_playlist_s
1286  * \param self a playlist
1287  * \param position a time relative to the beginning of the playlist
1288  * \return the index of the playlist entry
1289  */
1290
1291 int mlt_playlist_get_clip_index_at( mlt_playlist self, mlt_position position )
1292 {
1293         int index = 0, total = 0;
1294         mlt_playlist_locate( self, &position, &index, &total );
1295         return index;
1296 }
1297
1298 /** Determine if the clip is a mix.
1299  *
1300  * \public \memberof mlt_playlist_s
1301  * \param self a playlist
1302  * \param clip the index of the playlist entry
1303  * \return true if the producer is a mix
1304  */
1305
1306 int mlt_playlist_clip_is_mix( mlt_playlist self, int clip )
1307 {
1308         mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( self, clip ) );
1309         mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1310         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1311         return tractor != NULL;
1312 }
1313
1314 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1315  * back correctly on to the playlist.
1316  *
1317  * \private \memberof mlt_playlist_s
1318  * \param self a playlist
1319  * \param clip the index of the playlist entry
1320  * \return true if there was an error
1321  */
1322
1323 static int mlt_playlist_unmix( mlt_playlist self, int clip )
1324 {
1325         int error = ( clip < 0 || clip >= self->count );
1326
1327         // Ensure that the clip request is actually a mix
1328         if ( error == 0 )
1329         {
1330                 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1331                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1332                 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1333                             self->list[ clip ]->preservation_hack;
1334         }
1335
1336         if ( error == 0 )
1337         {
1338                 playlist_entry *mix = self->list[ clip ];
1339                 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1340                 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1341                 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1342                 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1343                 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1344                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1345
1346                 if ( clip_a != NULL )
1347                 {
1348                         mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1349                 }
1350                 else
1351                 {
1352                         mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1353                         mlt_playlist_insert( self, cut, clip, -1, -1 );
1354                         clip ++;
1355                 }
1356
1357                 if ( clip_b != NULL )
1358                 {
1359                         mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1360                 }
1361                 else
1362                 {
1363                         mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1364                         mlt_playlist_insert( self, cut, clip + 1, -1, -1 );
1365                 }
1366
1367                 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1368                 mlt_playlist_remove( self, clip );
1369                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1370                 mlt_playlist_virtual_refresh( self );
1371         }
1372         return error;
1373 }
1374
1375 /** Resize a mix clip.
1376  *
1377  * \private \memberof mlt_playlist_s
1378  * \param self a playlist
1379  * \param clip the index of the playlist entry
1380  * \param in the new starting point
1381  * \param out the new ending point
1382  * \return true if there was an error
1383  */
1384
1385 static int mlt_playlist_resize_mix( mlt_playlist self, int clip, int in, int out )
1386 {
1387         int error = ( clip < 0 || clip >= self->count );
1388
1389         // Ensure that the clip request is actually a mix
1390         if ( error == 0 )
1391         {
1392                 mlt_producer producer = mlt_producer_cut_parent( self->list[ clip ]->producer );
1393                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1394                 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1395         }
1396
1397         if ( error == 0 )
1398         {
1399                 playlist_entry *mix = self->list[ clip ];
1400                 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1401                 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1402                 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1403                 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1404                 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1405                 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1406                 int length = out - in + 1;
1407                 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1408                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( self ), self );
1409
1410                 if ( clip_a != NULL )
1411                         mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1412
1413                 if ( clip_b != NULL )
1414                         mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1415
1416                 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1417                 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1418                 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1419                 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1420                 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1421                 mlt_producer_set_in_and_out( mix->producer, in, out );
1422
1423                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( self ), self );
1424                 mlt_playlist_virtual_refresh( self );
1425         }
1426         return error;
1427 }
1428
1429 /** Consolidate adjacent blank producers.
1430  *
1431  * \public \memberof mlt_playlist_s
1432  * \param self a playlist
1433  * \param keep_length set false to remove the last entry if it is blank
1434  */
1435
1436 void mlt_playlist_consolidate_blanks( mlt_playlist self, int keep_length )
1437 {
1438         if ( self != NULL )
1439         {
1440                 int i = 0;
1441                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1442
1443                 mlt_events_block( properties, properties );
1444                 for ( i = 1; i < self->count; i ++ )
1445                 {
1446                         playlist_entry *left = self->list[ i - 1 ];
1447                         playlist_entry *right = self->list[ i ];
1448
1449                         if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1450                         {
1451                                 mlt_playlist_resize_clip( self, i - 1, 0, left->frame_count + right->frame_count - 1 );
1452                                 mlt_playlist_remove( self, i -- );
1453                         }
1454                 }
1455
1456                 if ( !keep_length && self->count > 0 )
1457                 {
1458                         playlist_entry *last = self->list[ self->count - 1 ];
1459                         if ( mlt_producer_is_blank( last->producer ) )
1460                                 mlt_playlist_remove( self, self->count - 1 );
1461                 }
1462
1463                 mlt_events_unblock( properties, properties );
1464                 mlt_playlist_virtual_refresh( self );
1465         }
1466 }
1467
1468 /** Determine if the specified clip index is a blank.
1469  *
1470  * \public \memberof mlt_playlist_s
1471  * \param self a playlist
1472  * \param clip the index of the playlist entry
1473  * \return true if there was an error
1474  */
1475
1476 int mlt_playlist_is_blank( mlt_playlist self, int clip )
1477 {
1478         return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( self, clip ) );
1479 }
1480
1481 /** Determine if the specified position is a blank.
1482  *
1483  * \public \memberof mlt_playlist_s
1484  * \param self a playlist
1485  * \param position a time relative to the start or end (negative) of the playlist
1486  * \return true if there was an error
1487  */
1488
1489 int mlt_playlist_is_blank_at( mlt_playlist self, mlt_position position )
1490 {
1491         return self == NULL || mlt_producer_is_blank( mlt_playlist_get_clip_at( self, position ) );
1492 }
1493
1494 /** Replace the specified clip with a blank and return the clip.
1495  *
1496  * \public \memberof mlt_playlist_s
1497  * \param self a playlist
1498  * \param clip the index of the playlist entry
1499  * \return a producer or NULL if there was an error
1500  */
1501
1502 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist self, int clip )
1503 {
1504         mlt_producer producer = NULL;
1505         if ( !mlt_playlist_is_blank( self, clip ) )
1506         {
1507                 playlist_entry *entry = self->list[ clip ];
1508                 int in = entry->frame_in;
1509                 int out = entry->frame_out;
1510                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1511                 producer = entry->producer;
1512                 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1513                 mlt_events_block( properties, properties );
1514                 mlt_playlist_remove( self, clip );
1515                 mlt_playlist_blank( self, out - in );
1516                 mlt_playlist_move( self, self->count - 1, clip );
1517                 mlt_events_unblock( properties, properties );
1518                 mlt_playlist_virtual_refresh( self );
1519                 mlt_producer_set_in_and_out( producer, in, out );
1520         }
1521         return producer;
1522 }
1523
1524 /** Insert blank space.
1525  *
1526  * \public \memberof mlt_playlist_s
1527  * \param self a playlist
1528  * \param clip the index of the new blank section
1529  * \param length the ending time of the new blank section (duration - 1)
1530  */
1531
1532 void mlt_playlist_insert_blank( mlt_playlist self, int clip, int length )
1533 {
1534         if ( self != NULL && length >= 0 )
1535         {
1536                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1537                 mlt_events_block( properties, properties );
1538                 mlt_playlist_blank( self, length );
1539                 mlt_playlist_move( self, self->count - 1, clip );
1540                 mlt_events_unblock( properties, properties );
1541                 mlt_playlist_virtual_refresh( self );
1542         }
1543 }
1544
1545 /** Resize a blank entry.
1546  *
1547  * \public \memberof mlt_playlist_s
1548  * \param self a playlist
1549  * \param position the time at which the blank entry exists relative to the start or end (negative) of the playlist.
1550  * \param length the additional amount of blank frames to add
1551  * \param find true to fist locate the blank after the clip at position
1552  */
1553 void mlt_playlist_pad_blanks( mlt_playlist self, mlt_position position, int length, int find )
1554 {
1555         if ( self != NULL && length != 0 )
1556         {
1557                 int clip = mlt_playlist_get_clip_index_at( self, position );
1558                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1559                 mlt_events_block( properties, properties );
1560                 if ( find && clip < self->count && !mlt_playlist_is_blank( self, clip ) )
1561                         clip ++;
1562                 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1563                 {
1564                         mlt_playlist_clip_info info;
1565                         mlt_playlist_get_clip_info( self, &info, clip );
1566                         if ( info.frame_out + length > info.frame_in )
1567                                 mlt_playlist_resize_clip( self, clip, info.frame_in, info.frame_out + length );
1568                         else
1569                                 mlt_playlist_remove( self, clip );
1570                 }
1571                 else if ( find && clip < self->count && length > 0  )
1572                 {
1573                         mlt_playlist_insert_blank( self, clip, length );
1574                 }
1575                 mlt_events_unblock( properties, properties );
1576                 mlt_playlist_virtual_refresh( self );
1577         }
1578 }
1579
1580 /** Insert a clip at a specific time.
1581  *
1582  * \public \memberof mlt_playlist_s
1583  * \param self a playlist
1584  * \param position the time at which to insert
1585  * \param producer the producer to insert
1586  * \param mode true if you want to overwrite any blank section
1587  * \return true if there was an error
1588  */
1589
1590 int mlt_playlist_insert_at( mlt_playlist self, mlt_position position, mlt_producer producer, int mode )
1591 {
1592         int ret = self == NULL || position < 0 || producer == NULL;
1593         if ( ret == 0 )
1594         {
1595                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1596                 int length = mlt_producer_get_playtime( producer );
1597                 int clip = mlt_playlist_get_clip_index_at( self, position );
1598                 mlt_playlist_clip_info info;
1599                 mlt_playlist_get_clip_info( self, &info, clip );
1600                 mlt_events_block( properties, self );
1601                 if ( clip < self->count && mlt_playlist_is_blank( self, clip ) )
1602                 {
1603                         // Split and move to new clip if need be
1604                         if ( position != info.start && mlt_playlist_split( self, clip, position - info.start - 1 ) == 0 )
1605                                 mlt_playlist_get_clip_info( self, &info, ++ clip );
1606
1607                         // Split again if need be
1608                         if ( length < info.frame_count )
1609                                 mlt_playlist_split( self, clip, length - 1 );
1610
1611                         // Remove
1612                         mlt_playlist_remove( self, clip );
1613
1614                         // Insert
1615                         mlt_playlist_insert( self, producer, clip, -1, -1 );
1616                         ret = clip;
1617                 }
1618                 else if ( clip < self->count )
1619                 {
1620                         if ( position > info.start + info.frame_count / 2 )
1621                                 clip ++;
1622                         if ( mode == 1 && clip < self->count && mlt_playlist_is_blank( self, clip ) )
1623                         {
1624                                 mlt_playlist_get_clip_info( self, &info, clip );
1625                                 if ( length < info.frame_count )
1626                                         mlt_playlist_split( self, clip, length );
1627                                 mlt_playlist_remove( self, clip );
1628                         }
1629                         mlt_playlist_insert( self, producer, clip, -1, -1 );
1630                         ret = clip;
1631                 }
1632                 else
1633                 {
1634                         if ( mode == 1 ) {
1635                                 if ( position == info.start )
1636                                         mlt_playlist_remove( self, clip );
1637                                 else
1638                                         mlt_playlist_blank( self, position - mlt_properties_get_int( properties, "length" ) - 1 );
1639                         }
1640                         mlt_playlist_append( self, producer );
1641                         ret = self->count - 1;
1642                 }
1643                 mlt_events_unblock( properties, self );
1644                 mlt_playlist_virtual_refresh( self );
1645         }
1646         else
1647         {
1648                 ret = -1;
1649         }
1650         return ret;
1651 }
1652
1653 /** Get the time at which the clip starts relative to the playlist.
1654  *
1655  * \public \memberof mlt_playlist_s
1656  * \param self a playlist
1657  * \param clip the index of the playlist entry
1658  * \return the starting time
1659  */
1660
1661 int mlt_playlist_clip_start( mlt_playlist self, int clip )
1662 {
1663         mlt_playlist_clip_info info;
1664         if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1665                 return info.start;
1666         return clip < 0 ? 0 : mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1667 }
1668
1669 /** Get the playable duration of the clip.
1670  *
1671  * \public \memberof mlt_playlist_s
1672  * \param self a playlist
1673  * \param clip the index of the playlist entry
1674  * \return the duration of the playlist entry
1675  */
1676
1677 int mlt_playlist_clip_length( mlt_playlist self, int clip )
1678 {
1679         mlt_playlist_clip_info info;
1680         if ( mlt_playlist_get_clip_info( self, &info, clip ) == 0 )
1681                 return info.frame_count;
1682         return 0;
1683 }
1684
1685 /** Get the duration of a blank space.
1686  *
1687  * \public \memberof mlt_playlist_s
1688  * \param self a playlist
1689  * \param clip the index of the playlist entry
1690  * \param bounded the maximum number of blank entries or 0 for all
1691  * \return the duration of a blank section
1692  */
1693
1694 int mlt_playlist_blanks_from( mlt_playlist self, int clip, int bounded )
1695 {
1696         int count = 0;
1697         mlt_playlist_clip_info info;
1698         if ( self != NULL && clip < self->count )
1699         {
1700                 mlt_playlist_get_clip_info( self, &info, clip );
1701                 if ( mlt_playlist_is_blank( self, clip ) )
1702                         count += info.frame_count;
1703                 if ( bounded == 0 )
1704                         bounded = self->count;
1705                 for ( clip ++; clip < self->count && bounded >= 0; clip ++ )
1706                 {
1707                         mlt_playlist_get_clip_info( self, &info, clip );
1708                         if ( mlt_playlist_is_blank( self, clip ) )
1709                                 count += info.frame_count;
1710                         else
1711                                 bounded --;
1712                 }
1713         }
1714         return count;
1715 }
1716
1717 /** Remove a portion of the playlist by time.
1718  *
1719  * \public \memberof mlt_playlist_s
1720  * \param self a playlist
1721  * \param position the starting time
1722  * \param length the duration of time to remove
1723  * \return the new entry index at the position
1724  */
1725
1726 int mlt_playlist_remove_region( mlt_playlist self, mlt_position position, int length )
1727 {
1728         int index = mlt_playlist_get_clip_index_at( self, position );
1729         if ( index >= 0 && index < self->count )
1730         {
1731                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( self );
1732                 int clip_start = mlt_playlist_clip_start( self, index );
1733                 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( self ) );
1734                 mlt_events_block( properties, self );
1735
1736                 if ( position + length > list_length )
1737                         length -= ( position + length - list_length );
1738
1739                 if ( clip_start < position )
1740                 {
1741                         mlt_playlist_split( self, index ++, position - clip_start - 1 );
1742                 }
1743
1744                 while( length > 0 )
1745                 {
1746                         if ( mlt_playlist_clip_length( self, index ) > length )
1747                                 mlt_playlist_split( self, index, length - 1 );
1748                         length -= mlt_playlist_clip_length( self, index );
1749                         mlt_playlist_remove( self, index );
1750                 }
1751
1752                 mlt_playlist_consolidate_blanks( self, 0 );
1753                 mlt_events_unblock( properties, self );
1754                 mlt_playlist_virtual_refresh( self );
1755
1756                 // Just to be sure, we'll get the clip index again...
1757                 index = mlt_playlist_get_clip_index_at( self, position );
1758         }
1759         return index;
1760 }
1761
1762 /** Not implemented
1763  *
1764  * \deprecated not implemented
1765  * \public \memberof mlt_playlist_s
1766  * \param self
1767  * \param position
1768  * \param length
1769  * \param new_position
1770  * \return
1771  */
1772
1773 int mlt_playlist_move_region( mlt_playlist self, mlt_position position, int length, int new_position )
1774 {
1775         if ( self != NULL )
1776         {
1777         }
1778         return 0;
1779 }
1780
1781 /** Get the current frame.
1782  *
1783  * The implementation of the get_frame virtual function.
1784  * \private \memberof mlt_playlist_s
1785  * \param producer a producer
1786  * \param frame a frame by reference
1787  * \param index the time at which to get the frame
1788  * \return false
1789  */
1790
1791 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1792 {
1793         // Check that we have a producer
1794         if ( producer == NULL )
1795         {
1796                 *frame = NULL;
1797                 return -1;
1798         }
1799
1800         // Get this mlt_playlist
1801         mlt_playlist self = producer->child;
1802
1803         // Need to ensure the frame is deinterlaced when repeating 1 frame
1804         int progressive = 0;
1805
1806         // Get the real producer
1807         mlt_service real = mlt_playlist_virtual_seek( self, &progressive );
1808
1809         // Check that we have a producer
1810         if ( real == NULL )
1811         {
1812                 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
1813                 return 0;
1814         }
1815
1816         // Get the frame
1817         if ( !mlt_properties_get_int( MLT_SERVICE_PROPERTIES( real ), "meta.fx_cut" ) )
1818         {
1819                 mlt_service_get_frame( real, frame, index );
1820         }
1821         else
1822         {
1823                 mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
1824                 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
1825                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
1826                 mlt_frame_push_service( *frame, NULL );
1827                 mlt_frame_push_audio( *frame, NULL );
1828                 mlt_service_apply_filters( MLT_PRODUCER_SERVICE( parent ), *frame, 0 );
1829                 mlt_service_apply_filters( real, *frame, 0 );
1830                 mlt_deque_pop_front( MLT_FRAME_IMAGE_STACK( *frame ) );
1831                 mlt_deque_pop_front( MLT_FRAME_AUDIO_STACK( *frame ) );
1832         }
1833
1834         // Check if we're at the end of the clip
1835         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1836         if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1837                 mlt_playlist_virtual_set_out( self );
1838
1839         // Set the consumer progressive property
1840         if ( progressive )
1841         {
1842                 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1843                 mlt_properties_set_int( properties, "test_audio", 1 );
1844         }
1845
1846         // Check for notifier and call with appropriate argument
1847         mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1848         void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1849         if ( notifier != NULL )
1850         {
1851                 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1852                 notifier( argument );
1853         }
1854
1855         // Update position on the frame we're creating
1856         mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1857
1858         // Position ourselves on the next frame
1859         mlt_producer_prepare_next( producer );
1860
1861         return 0;
1862 }
1863
1864 /** Close the playlist.
1865  *
1866  * \public \memberof mlt_playlist_s
1867  * \param self a playlist
1868  */
1869
1870 void mlt_playlist_close( mlt_playlist self )
1871 {
1872         if ( self != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( self ) ) <= 0 )
1873         {
1874                 int i = 0;
1875                 self->parent.close = NULL;
1876                 for ( i = 0; i < self->count; i ++ )
1877                 {
1878                         mlt_event_close( self->list[ i ]->event );
1879                         mlt_producer_close( self->list[ i ]->producer );
1880                         free( self->list[ i ] );
1881                 }
1882                 mlt_producer_close( &self->blank );
1883                 mlt_producer_close( &self->parent );
1884                 free( self->list );
1885                 free( self );
1886         }
1887 }