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