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