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