]> git.sesse.net Git - mlt/blob - src/framework/mlt_playlist.c
Corrections to playlist manipulations and producer type determination
[mlt] / src / framework / mlt_playlist.c
1 /*
2  * mlt_playlist.c -- playlist service class
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "mlt_playlist.h"
24 #include "mlt_tractor.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
27 #include "mlt_frame.h"
28 #include "mlt_transition.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 /** Virtual playlist entry.
35 */
36
37 struct playlist_entry_s
38 {
39         mlt_producer producer;
40         mlt_position frame_in;
41         mlt_position frame_out;
42         mlt_position frame_count;
43         int repeat;
44         mlt_position producer_length;
45         mlt_event event;
46         int preservation_hack;
47 };
48
49 /** Forward declarations
50 */
51
52 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
53 static int mlt_playlist_unmix( mlt_playlist this, int clip );
54 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out );
55
56 /** Constructor.
57 */
58
59 mlt_playlist mlt_playlist_init( )
60 {
61         mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
62         if ( this != NULL )
63         {
64                 mlt_producer producer = &this->parent;
65
66                 // Construct the producer
67                 mlt_producer_init( producer, this );
68
69                 // Override the producer get_frame
70                 producer->get_frame = producer_get_frame;
71
72                 // Define the destructor
73                 producer->close = ( mlt_destructor )mlt_playlist_close;
74                 producer->close_object = this;
75
76                 // Initialise blank
77                 mlt_producer_init( &this->blank, NULL );
78                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "mlt_service", "blank" );
79                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( &this->blank ), "resource", "blank" );
80
81                 // Indicate that this producer is a playlist
82                 mlt_properties_set_data( MLT_PLAYLIST_PROPERTIES( this ), "playlist", this, 0, NULL, NULL );
83
84                 // Specify the eof condition
85                 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "eof", "pause" );
86                 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "resource", "<playlist>" );
87                 mlt_properties_set( MLT_PLAYLIST_PROPERTIES( this ), "mlt_type", "mlt_producer" );
88                 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "in", 0 );
89                 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "out", -1 );
90                 mlt_properties_set_position( MLT_PLAYLIST_PROPERTIES( this ), "length", 0 );
91
92                 this->size = 10;
93                 this->list = malloc( this->size * sizeof( playlist_entry * ) );
94         }
95         
96         return this;
97 }
98
99 /** Get the producer associated to this playlist.
100 */
101
102 mlt_producer mlt_playlist_producer( mlt_playlist this )
103 {
104         return this != NULL ? &this->parent : NULL;
105 }
106
107 /** Get the service associated to this playlist.
108 */
109
110 mlt_service mlt_playlist_service( mlt_playlist this )
111 {
112         return MLT_PRODUCER_SERVICE( &this->parent );
113 }
114
115 /** Get the propertues associated to this playlist.
116 */
117
118 mlt_properties mlt_playlist_properties( mlt_playlist this )
119 {
120         return MLT_PRODUCER_PROPERTIES( &this->parent );
121 }
122
123 /** Refresh the playlist after a clip has been changed.
124 */
125
126 static int mlt_playlist_virtual_refresh( mlt_playlist this )
127 {
128         // Obtain the properties
129         mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
130
131         // Get the fps of the first producer
132         double fps = mlt_properties_get_double( properties, "first_fps" );
133         int i = 0;
134         mlt_position frame_count = 0;
135
136         for ( i = 0; i < this->count; i ++ )
137         {
138                 // Get the producer
139                 mlt_producer producer = this->list[ i ]->producer;
140                 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
141
142                 // If fps is 0
143                 if ( fps == 0 )
144                 {
145                         // Inherit it from the producer
146                         fps = mlt_producer_get_fps( producer );
147                 }
148                 else if ( fps != mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( producer ), "fps" ) )
149                 {
150                         // Generate a warning for now - the following attempt to fix may fail
151                         fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
152
153                         // It should be safe to impose fps on an image producer, but not necessarily safe for video
154                         mlt_properties_set_double( MLT_PRODUCER_PROPERTIES( producer ), "fps", fps );
155                 }
156
157                 // Check if the length of the producer has changed
158                 if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
159                          this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
160                 {
161                         // This clip should be removed...
162                         if ( current_length < 1 )
163                         {
164                                 this->list[ i ]->frame_in = 0;
165                                 this->list[ i ]->frame_out = -1;
166                                 this->list[ i ]->frame_count = 0;
167                         }
168                         else 
169                         {
170                                 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
171                                 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
172                                 this->list[ i ]->frame_count = current_length;
173                         }
174
175                         // Update the producer_length
176                         this->list[ i ]->producer_length = current_length;
177                 }
178
179                 // Calculate the frame_count
180                 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
181
182                 // Update the frame_count for this clip
183                 frame_count += this->list[ i ]->frame_count;
184         }
185
186         // Refresh all properties
187         mlt_properties_set_double( properties, "first_fps", fps );
188         mlt_properties_set_double( properties, "fps", fps == 0 ? 25 : fps );
189         mlt_events_block( properties, properties );
190         mlt_properties_set_position( properties, "length", frame_count );
191         mlt_events_unblock( properties, properties );
192         mlt_properties_set_position( properties, "out", frame_count - 1 );
193
194         return 0;
195 }
196
197 /** Listener for producers on the playlist.
198 */
199
200 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
201 {
202         mlt_playlist_virtual_refresh( this );
203 }
204
205 /** Append to the virtual playlist.
206 */
207
208 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
209 {
210         mlt_producer producer = NULL;
211         mlt_properties properties = NULL;
212         mlt_properties parent = NULL;
213
214         // If we have a cut, then use the in/out points from the cut
215         if ( mlt_producer_is_blank( source )  )
216         {
217                 // Make sure the blank is long enough to accomodate the length specified
218                 if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
219                 {
220                         mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
221                         mlt_events_block( blank_props, blank_props );
222                         mlt_producer_set_in_and_out( &this->blank, in, out );
223                         mlt_events_unblock( blank_props, blank_props );
224                 }
225
226                 // Now make sure the cut comes from this this->blank
227                 if ( source == NULL )
228                 {
229                         producer = mlt_producer_cut( &this->blank, in, out );
230                 }
231                 else if ( !mlt_producer_is_cut( source ) || mlt_producer_cut_parent( source ) != &this->blank )
232                 {
233                         producer = mlt_producer_cut( &this->blank, in, out );
234                 }
235                 else
236                 {
237                         producer = source;
238                         mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
239                 }
240
241                 properties = MLT_PRODUCER_PROPERTIES( producer );
242         }
243         else if ( mlt_producer_is_cut( source ) )
244         {
245                 producer = source;
246                 if ( in == -1 )
247                         in = mlt_producer_get_in( producer );
248                 if ( out == -1 || out > mlt_producer_get_out( producer ) )
249                         out = mlt_producer_get_out( producer );
250                 properties = MLT_PRODUCER_PROPERTIES( producer );
251                 mlt_properties_inc_ref( properties );
252         }
253         else
254         {
255                 producer = mlt_producer_cut( source, in, out );
256                 if ( in == -1 || in < mlt_producer_get_in( producer ) )
257                         in = mlt_producer_get_in( producer );
258                 if ( out == -1 || out > mlt_producer_get_out( producer ) )
259                         out = mlt_producer_get_out( producer );
260                 properties = MLT_PRODUCER_PROPERTIES( producer );
261         }
262
263         // Fetch the cuts parent properties
264         parent = MLT_PRODUCER_PROPERTIES( mlt_producer_cut_parent( producer ) );
265
266         // Check that we have room
267         if ( this->count >= this->size )
268         {
269                 int i;
270                 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
271                 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
272                 this->size += 10;
273         }
274
275         // Create the entry
276         this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
277         if ( this->list[ this->count ] != NULL )
278         {
279                 this->list[ this->count ]->producer = producer;
280                 this->list[ this->count ]->frame_in = in;
281                 this->list[ this->count ]->frame_out = out;
282                 this->list[ this->count ]->frame_count = out - in + 1;
283                 this->list[ this->count ]->repeat = 1;
284                 this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
285                 this->list[ this->count ]->event = mlt_events_listen( parent, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
286                 mlt_event_inc_ref( this->list[ this->count ]->event );
287                 mlt_properties_set( properties, "eof", "pause" );
288                 mlt_producer_set_speed( producer, 0 );
289                 this->count ++;
290         }
291
292         return mlt_playlist_virtual_refresh( this );
293 }
294
295 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
296 {
297         // Default producer to NULL
298         mlt_producer producer = NULL;
299
300         // Loop for each producer until found
301         for ( *clip = 0; *clip < this->count; *clip += 1 )
302         {
303                 // Increment the total
304                 *total += this->list[ *clip ]->frame_count;
305
306                 // Check if the position indicates that we have found the clip
307                 // Note that 0 length clips get skipped automatically
308                 if ( *position < this->list[ *clip ]->frame_count )
309                 {
310                         // Found it, now break
311                         producer = this->list[ *clip ]->producer;
312                         break;
313                 }
314                 else
315                 {
316                         // Decrement position by length of this entry
317                         *position -= this->list[ *clip ]->frame_count;
318                 }
319         }
320
321         return producer;
322 }
323
324 /** Seek in the virtual playlist.
325 */
326
327 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
328 {
329         // Map playlist position to real producer in virtual playlist
330         mlt_position position = mlt_producer_frame( &this->parent );
331
332         // Keep the original position since we change it while iterating through the list
333         mlt_position original = position;
334
335         // Clip index and total
336         int i = 0;
337         int total = 0;
338
339         // Locate the producer for the position
340         mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
341
342         // Get the properties
343         mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
344
345         // Get the eof handling
346         char *eof = mlt_properties_get( properties, "eof" );
347
348         // Seek in real producer to relative position
349         if ( producer != NULL )
350         {
351                 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
352                 *progressive = count == 1;
353                 mlt_producer_seek( producer, position % count );
354         }
355         else if ( !strcmp( eof, "pause" ) && total > 0 )
356         {
357                 playlist_entry *entry = this->list[ this->count - 1 ];
358                 int count = entry->frame_count / entry->repeat;
359                 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
360                 mlt_producer_seek( this_producer, original - 1 );
361                 producer = entry->producer;
362                 mlt_producer_seek( producer, entry->frame_out % count );
363                 mlt_producer_set_speed( this_producer, 0 );
364                 mlt_producer_set_speed( producer, 0 );
365                 *progressive = count == 1;
366         }
367         else if ( !strcmp( eof, "loop" ) && total > 0 )
368         {
369                 playlist_entry *entry = this->list[ 0 ];
370                 mlt_producer this_producer = MLT_PLAYLIST_PRODUCER( this );
371                 mlt_producer_seek( this_producer, 0 );
372                 producer = entry->producer;
373                 mlt_producer_seek( producer, 0 );
374         }
375         else
376         {
377                 producer = &this->blank;
378         }
379
380         return MLT_PRODUCER_SERVICE( producer );
381 }
382
383 /** Invoked when a producer indicates that it has prematurely reached its end.
384 */
385
386 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
387 {
388         // Default producer to blank
389         mlt_producer producer = &this->blank;
390
391         // Map playlist position to real producer in virtual playlist
392         mlt_position position = mlt_producer_frame( &this->parent );
393
394         // Loop through the virtual playlist
395         int i = 0;
396
397         for ( i = 0; i < this->count; i ++ )
398         {
399                 if ( position < this->list[ i ]->frame_count )
400                 {
401                         // Found it, now break
402                         producer = this->list[ i ]->producer;
403                         break;
404                 }
405                 else
406                 {
407                         // Decrement position by length of this entry
408                         position -= this->list[ i ]->frame_count;
409                 }
410         }
411
412         // Seek in real producer to relative position
413         if ( i < this->count && this->list[ i ]->frame_out != position )
414         {
415                 // Update the frame_count for the changed clip (hmmm)
416                 this->list[ i ]->frame_out = position;
417                 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
418
419                 // Refresh the playlist
420                 mlt_playlist_virtual_refresh( this );
421         }
422
423         return producer;
424 }
425
426 /** Obtain the current clips index.
427 */
428
429 int mlt_playlist_current_clip( mlt_playlist this )
430 {
431         // Map playlist position to real producer in virtual playlist
432         mlt_position position = mlt_producer_frame( &this->parent );
433
434         // Loop through the virtual playlist
435         int i = 0;
436
437         for ( i = 0; i < this->count; i ++ )
438         {
439                 if ( position < this->list[ i ]->frame_count )
440                 {
441                         // Found it, now break
442                         break;
443                 }
444                 else
445                 {
446                         // Decrement position by length of this entry
447                         position -= this->list[ i ]->frame_count;
448                 }
449         }
450
451         return i;
452 }
453
454 /** Obtain the current clips producer.
455 */
456
457 mlt_producer mlt_playlist_current( mlt_playlist this )
458 {
459         int i = mlt_playlist_current_clip( this );
460         if ( i < this->count )
461                 return this->list[ i ]->producer;
462         else
463                 return &this->blank;
464 }
465
466 /** Get the position which corresponds to the start of the next clip.
467 */
468
469 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
470 {
471         mlt_position position = 0;
472         int absolute_clip = index;
473         int i = 0;
474
475         // Determine the absolute clip
476         switch ( whence )
477         {
478                 case mlt_whence_relative_start:
479                         absolute_clip = index;
480                         break;
481
482                 case mlt_whence_relative_current:
483                         absolute_clip = mlt_playlist_current_clip( this ) + index;
484                         break;
485
486                 case mlt_whence_relative_end:
487                         absolute_clip = this->count - index;
488                         break;
489         }
490
491         // Check that we're in a valid range
492         if ( absolute_clip < 0 )
493                 absolute_clip = 0;
494         else if ( absolute_clip > this->count )
495                 absolute_clip = this->count;
496
497         // Now determine the position
498         for ( i = 0; i < absolute_clip; i ++ )
499                 position += this->list[ i ]->frame_count;
500
501         return position;
502 }
503
504 /** Get all the info about the clip specified.
505 */
506
507 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
508 {
509         int error = index < 0 || index >= this->count;
510         memset( info, 0, sizeof( mlt_playlist_clip_info ) );
511         if ( !error )
512         {
513                 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
514                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
515                 info->clip = index;
516                 info->producer = producer;
517                 info->cut = this->list[ index ]->producer;
518                 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
519                 info->resource = mlt_properties_get( properties, "resource" );
520                 info->frame_in = this->list[ index ]->frame_in;
521                 info->frame_out = this->list[ index ]->frame_out;
522                 info->frame_count = this->list[ index ]->frame_count;
523                 info->repeat = this->list[ index ]->repeat;
524                 info->length = mlt_producer_get_length( producer );
525                 info->fps = mlt_producer_get_fps( producer );
526         }
527
528         return error;
529 }
530
531 /** Get number of clips in the playlist.
532 */
533
534 int mlt_playlist_count( mlt_playlist this )
535 {
536         return this->count;
537 }
538
539 /** Clear the playlist.
540 */
541
542 int mlt_playlist_clear( mlt_playlist this )
543 {
544         int i;
545         for ( i = 0; i < this->count; i ++ )
546         {
547                 mlt_event_close( this->list[ i ]->event );
548                 mlt_producer_close( this->list[ i ]->producer );
549         }
550         this->count = 0;
551         mlt_properties_set_double( MLT_PLAYLIST_PROPERTIES( this ), "first_fps", 0 );
552         return mlt_playlist_virtual_refresh( this );
553 }
554
555 /** Append a producer to the playlist.
556 */
557
558 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
559 {
560         // Append to virtual list
561         return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
562 }
563
564 /** Append a producer to the playlist with in/out points.
565 */
566
567 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
568 {
569         // Append to virtual list
570         if ( in != -1 && out != -1 )
571                 return mlt_playlist_virtual_append( this, producer, in, out );
572         else
573                 return mlt_playlist_append( this, producer );
574 }
575
576 /** Append a blank to the playlist of a given length.
577 */
578
579 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
580 {
581         // Append to the virtual list
582         return mlt_playlist_virtual_append( this, &this->blank, 0, length );
583 }
584
585 /** Insert a producer into the playlist.
586 */
587
588 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
589 {
590         // Append to end
591         mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
592         mlt_playlist_append_io( this, producer, in, out );
593
594         // Move to the position specified
595         mlt_playlist_move( this, this->count - 1, where );
596         mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
597
598         return mlt_playlist_virtual_refresh( this );
599 }
600
601 /** Remove an entry in the playlist.
602 */
603
604 int mlt_playlist_remove( mlt_playlist this, int where )
605 {
606         int error = where < 0 || where >= this->count;
607         if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
608         {
609                 // We need to know the current clip and the position within the playlist
610                 int current = mlt_playlist_current_clip( this );
611                 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
612
613                 // We need all the details about the clip we're removing
614                 mlt_playlist_clip_info where_info;
615                 playlist_entry *entry = this->list[ where ];
616                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( entry->producer );
617
618                 // Loop variable
619                 int i = 0;
620
621                 // Get the clip info 
622                 mlt_playlist_get_clip_info( this, &where_info, where );
623
624                 // Make sure the clip to be removed is valid and correct if necessary
625                 if ( where < 0 ) 
626                         where = 0;
627                 if ( where >= this->count )
628                         where = this->count - 1;
629
630                 // Reorganise the list
631                 for ( i = where + 1; i < this->count; i ++ )
632                         this->list[ i - 1 ] = this->list[ i ];
633                 this->count --;
634
635                 if ( entry->preservation_hack == 0 )
636                 {
637                         // Decouple from mix_in/out if necessary
638                         if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
639                         {
640                                 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
641                                 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
642                         }
643                         if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
644                         {
645                                 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
646                                 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
647                         }
648         
649                         if ( mlt_properties_ref_count( MLT_PRODUCER_PROPERTIES( entry->producer ) ) == 1 )
650                                 mlt_producer_clear( entry->producer );
651                 }
652
653                 // Close the producer associated to the clip info
654                 mlt_event_close( entry->event );
655                 mlt_producer_close( entry->producer );
656
657                 // Correct position
658                 if ( where == current )
659                         mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), where_info.start );
660                 else if ( where < current && this->count > 0 )
661                         mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), position - where_info.frame_count );
662                 else if ( this->count == 0 )
663                         mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), 0 );
664
665                 // Free the entry
666                 free( entry );
667
668                 // Refresh the playlist
669                 mlt_playlist_virtual_refresh( this );
670         }
671
672         return error;
673 }
674
675 /** Move an entry in the playlist.
676 */
677
678 int mlt_playlist_move( mlt_playlist this, int src, int dest )
679 {
680         int i;
681
682         /* We need to ensure that the requested indexes are valid and correct it as necessary */
683         if ( src < 0 ) 
684                 src = 0;
685         if ( src >= this->count )
686                 src = this->count - 1;
687
688         if ( dest < 0 ) 
689                 dest = 0;
690         if ( dest >= this->count )
691                 dest = this->count - 1;
692         
693         if ( src != dest && this->count > 1 )
694         {
695                 int current = mlt_playlist_current_clip( this );
696                 mlt_position position = mlt_producer_position( MLT_PLAYLIST_PRODUCER( this ) );
697                 playlist_entry *src_entry = NULL;
698
699                 // We need all the details about the current clip
700                 mlt_playlist_clip_info current_info;
701
702                 mlt_playlist_get_clip_info( this, &current_info, current );
703                 position -= current_info.start;
704
705                 if ( current == src )
706                         current = dest;
707                 else if ( current > src && current < dest )
708                         current ++;
709                 else if ( current == dest )
710                         current = src;
711
712                 src_entry = this->list[ src ];
713                 if ( src > dest )
714                 {
715                         for ( i = src; i > dest; i -- )
716                                 this->list[ i ] = this->list[ i - 1 ];
717                 }
718                 else
719                 {
720                         for ( i = src; i < dest; i ++ )
721                                 this->list[ i ] = this->list[ i + 1 ];
722                 }
723                 this->list[ dest ] = src_entry;
724
725                 mlt_playlist_get_clip_info( this, &current_info, current );
726                 mlt_producer_seek( MLT_PLAYLIST_PRODUCER( this ), current_info.start + position );
727                 mlt_playlist_virtual_refresh( this );
728         }
729
730         return 0;
731 }
732
733 /** Repeat the specified clip n times.
734 */
735
736 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
737 {
738         int error = repeat < 1 || clip < 0 || clip >= this->count;
739         if ( error == 0 )
740         {
741                 playlist_entry *entry = this->list[ clip ];
742                 entry->repeat = repeat;
743                 mlt_playlist_virtual_refresh( this );
744         }
745         return error;
746 }
747
748 /** Resize the specified clip.
749 */
750
751 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
752 {
753         int error = clip < 0 || clip >= this->count;
754         if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
755         {
756                 playlist_entry *entry = this->list[ clip ];
757                 mlt_producer producer = entry->producer;
758                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
759
760                 mlt_events_block( properties, properties );
761
762                 if ( mlt_producer_is_blank( producer ) )
763                 {
764                         // Make sure the blank is long enough to accomodate the length specified
765                         if ( out - in + 1 > mlt_producer_get_length( &this->blank ) )
766                         {
767                                 mlt_properties blank_props = MLT_PRODUCER_PROPERTIES( &this->blank );
768                                 mlt_properties_set_int( blank_props, "length", out - in + 1 );
769                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( producer ), "length", out - in + 1 );
770                                 mlt_producer_set_in_and_out( &this->blank, 0, out - in );
771                         }
772                 }
773
774                 if ( in <= -1 )
775                         in = 0;
776                 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
777                         out = mlt_producer_get_length( producer ) - 1;
778
779                 if ( out < in )
780                 {
781                         mlt_position t = in;
782                         in = out;
783                         out = t;
784                 }
785
786                 mlt_producer_set_in_and_out( producer, in, out );
787                 mlt_events_unblock( properties, properties );
788                 mlt_playlist_virtual_refresh( this );
789         }
790         return error;
791 }
792
793 /** Split a clip on the playlist at the given position.
794 */
795
796 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
797 {
798         int error = clip < 0 || clip >= this->count;
799         if ( error == 0 )
800         {
801                 playlist_entry *entry = this->list[ clip ];
802                 position = position < 0 ? entry->frame_count + position - 1 : position;
803                 if ( position >= 0 && position <= entry->frame_count )
804                 {
805                         int in = entry->frame_in;
806                         int out = entry->frame_out;
807                         mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
808                         mlt_playlist_resize_clip( this, clip, in, in + position );
809                         if ( !mlt_producer_is_blank( entry->producer ) )
810                         {
811                                 int i = 0;
812                                 mlt_properties entry_properties = MLT_PRODUCER_PROPERTIES( entry->producer );
813                                 mlt_producer split = mlt_producer_cut( entry->producer, in + position + 1, out );
814                                 mlt_properties split_properties = MLT_PRODUCER_PROPERTIES( split );
815                                 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
816                                 for ( i = 0; i < mlt_properties_count( entry_properties ); i ++ )
817                                 {
818                                         char *name = mlt_properties_get_name( entry_properties, i );
819                                         if ( name != NULL && !strncmp( name, "meta.", 5 ) )
820                                                 mlt_properties_set( split_properties, name, mlt_properties_get_value( entry_properties, i ) );
821                                 }
822                                 mlt_producer_close( split );
823                         }
824                         else
825                         {
826                                 mlt_playlist_insert( this, &this->blank, clip + 1, 0, out - position - 1 );
827                         }
828                         mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
829                         mlt_playlist_virtual_refresh( this );
830                 }
831                 else
832                 {
833                         error = 1;
834                 }
835         }
836         return error;
837 }
838
839 /** Join 1 or more consecutive clips.
840 */
841
842 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
843 {
844         int error = clip < 0 || ( clip ) >= this->count;
845         if ( error == 0 )
846         {
847                 int i = clip;
848                 mlt_playlist new_clip = mlt_playlist_init( );
849                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
850                 if ( clip + count >= this->count )
851                         count = this->count - clip;
852                 for ( i = 0; i <= count; i ++ )
853                 {
854                         playlist_entry *entry = this->list[ clip ];
855                         mlt_playlist_append( new_clip, entry->producer );
856                         mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
857                         entry->preservation_hack = 1;
858                         mlt_playlist_remove( this, clip );
859                 }
860                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
861                 mlt_playlist_insert( this, MLT_PLAYLIST_PRODUCER( new_clip ), clip, 0, -1 );
862                 mlt_playlist_close( new_clip );
863         }
864         return error;
865 }
866
867 /** Mix consecutive clips for a specified length and apply transition if specified.
868 */
869
870 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
871 {
872         int error = ( clip < 0 || clip + 1 >= this->count );
873         if ( error == 0 )
874         {
875                 playlist_entry *clip_a = this->list[ clip ];
876                 playlist_entry *clip_b = this->list[ clip + 1 ];
877                 mlt_producer track_a = NULL;
878                 mlt_producer track_b = NULL;
879                 mlt_tractor tractor = mlt_tractor_new( );
880                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
881
882                 // Check length is valid for both clips and resize if necessary.
883                 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
884                 length = length > max_size ? max_size : length;
885
886                 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
887                 if ( length != clip_a->frame_count )
888                         track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
889                 else
890                         track_a = clip_a->producer;
891
892                 if ( length != clip_b->frame_count )
893                         track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
894                 else
895                         track_b = clip_b->producer;
896
897                 // Set the tracks on the tractor
898                 mlt_tractor_set_track( tractor, track_a, 0 );
899                 mlt_tractor_set_track( tractor, track_b, 1 );
900
901                 // Insert the mix object into the playlist
902                 mlt_playlist_insert( this, MLT_TRACTOR_PRODUCER( tractor ), clip + 1, -1, -1 );
903                 mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
904
905                 // Attach the transition
906                 if ( transition != NULL )
907                 {
908                         mlt_field field = mlt_tractor_field( tractor );
909                         mlt_field_plant_transition( field, transition, 0, 1 );
910                         mlt_transition_set_in_and_out( transition, 0, length - 1 );
911                 }
912
913                 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
914                 if ( track_a != clip_a->producer )
915                         mlt_producer_close( track_a );
916                 if ( track_b != clip_b->producer )
917                         mlt_producer_close( track_b );
918
919                 // Check if we have anything left on the right hand clip
920                 if ( track_b == clip_b->producer )
921                 {
922                         clip_b->preservation_hack = 1;
923                         mlt_playlist_remove( this, clip + 2 );
924                 }
925                 else if ( clip_b->frame_out - clip_b->frame_in > length )
926                 {
927                         mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
928                         mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
929                         mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
930                 }
931                 else
932                 {
933                         mlt_producer_clear( clip_b->producer );
934                         mlt_playlist_remove( this, clip + 2 );
935                 }
936
937                 // Check if we have anything left on the left hand clip
938                 if ( track_a == clip_a->producer )
939                 {
940                         clip_a->preservation_hack = 1;
941                         mlt_playlist_remove( this, clip );
942                 }
943                 else if ( clip_a->frame_out - clip_a->frame_in > length )
944                 {
945                         mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
946                         mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
947                         mlt_properties_set_data( MLT_TRACTOR_PROPERTIES( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
948                 }
949                 else
950                 {
951                         mlt_producer_clear( clip_a->producer );
952                         mlt_playlist_remove( this, clip );
953                 }
954
955                 // Unblock and force a fire off of change events to listeners
956                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
957                 mlt_playlist_virtual_refresh( this );
958                 mlt_tractor_close( tractor );
959         }
960         return error;
961 }
962
963 /** Add a transition to an existing mix.
964 */
965
966 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
967 {
968         mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
969         mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
970         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
971         int error = transition == NULL || tractor == NULL;
972         if ( error == 0 )
973         {
974                 mlt_field field = mlt_tractor_field( tractor );
975                 mlt_field_plant_transition( field, transition, 0, 1 );
976                 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
977         }
978         return error;
979 }
980
981 /** Return the clip at the clip index.
982 */
983
984 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
985 {
986         if ( clip >= 0 && clip < this->count )
987                 return this->list[ clip ]->producer;
988         return NULL;
989 }
990
991 /** Return the clip at the specified position.
992 */
993
994 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, int position )
995 {
996         int index = 0, total = 0;
997         return mlt_playlist_locate( this, &position, &index, &total );
998 }
999
1000 /** Return the clip index of the specified position.
1001 */
1002
1003 int mlt_playlist_get_clip_index_at( mlt_playlist this, int position )
1004 {
1005         int index = 0, total = 0;
1006         mlt_playlist_locate( this, &position, &index, &total );
1007         return index;
1008 }
1009
1010 /** Determine if the clip is a mix.
1011 */
1012
1013 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
1014 {
1015         mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
1016         mlt_properties properties = producer != NULL ? MLT_PRODUCER_PROPERTIES( producer ) : NULL;
1017         mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
1018         return tractor != NULL;
1019 }
1020
1021 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
1022         back correctly on to the playlist.
1023 */
1024
1025 static int mlt_playlist_unmix( mlt_playlist this, int clip )
1026 {
1027         int error = ( clip < 0 || clip >= this->count ); 
1028
1029         // Ensure that the clip request is actually a mix
1030         if ( error == 0 )
1031         {
1032                 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1033                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1034                 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
1035                             this->list[ clip ]->preservation_hack;
1036         }
1037
1038         if ( error == 0 )
1039         {
1040                 playlist_entry *mix = this->list[ clip ];
1041                 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1042                 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1043                 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1044                 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1045                 int length = mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1046                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1047
1048                 if ( clip_a != NULL )
1049                 {
1050                         mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1051                 }
1052                 else
1053                 {
1054                         mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1055                         mlt_playlist_insert( this, cut, clip, -1, -1 );
1056                         clip ++;
1057                 }
1058
1059                 if ( clip_b != NULL )
1060                 {
1061                         mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1062                 }
1063                 else
1064                 {
1065                         mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1066                         mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1067                 }
1068
1069                 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1070                 mlt_playlist_remove( this, clip );
1071                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1072                 mlt_playlist_virtual_refresh( this );
1073         }
1074         return error;
1075 }
1076
1077 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1078 {
1079         int error = ( clip < 0 || clip >= this->count ); 
1080
1081         // Ensure that the clip request is actually a mix
1082         if ( error == 0 )
1083         {
1084                 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1085                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
1086                 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1087         }
1088
1089         if ( error == 0 )
1090         {
1091                 playlist_entry *mix = this->list[ clip ];
1092                 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1093                 mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
1094                 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1095                 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1096                 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1097                 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1098                 int length = out - in + 1;
1099                 int length_diff = length - mlt_producer_get_playtime( MLT_TRACTOR_PRODUCER( tractor ) );
1100                 mlt_events_block( MLT_PLAYLIST_PROPERTIES( this ), this );
1101
1102                 if ( clip_a != NULL )
1103                         mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1104
1105                 if ( clip_b != NULL )
1106                         mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1107
1108                 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1109                 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1110                 mlt_producer_set_in_and_out( MLT_MULTITRACK_PRODUCER( mlt_tractor_multitrack( tractor ) ), in, out );
1111                 mlt_producer_set_in_and_out( MLT_TRACTOR_PRODUCER( tractor ), in, out );
1112                 mlt_properties_set_position( MLT_PRODUCER_PROPERTIES( mix->producer ), "length", out - in + 1 );
1113                 mlt_producer_set_in_and_out( mix->producer, in, out );
1114
1115                 mlt_events_unblock( MLT_PLAYLIST_PROPERTIES( this ), this );
1116                 mlt_playlist_virtual_refresh( this );
1117         }
1118         return error;
1119 }
1120
1121 /** Consolodate adjacent blank producers.
1122 */
1123
1124 void mlt_playlist_consolidate_blanks( mlt_playlist this, int keep_length )
1125 {
1126         if ( this != NULL )
1127         {
1128                 int i = 0;
1129                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1130
1131                 mlt_events_block( properties, properties );
1132                 for ( i = 1; i < this->count; i ++ )
1133                 {
1134                         playlist_entry *left = this->list[ i - 1 ];
1135                         playlist_entry *right = this->list[ i ];
1136
1137                         if ( mlt_producer_is_blank( left->producer ) && mlt_producer_is_blank( right->producer ) )
1138                         {
1139                                 mlt_playlist_resize_clip( this, i - 1, 0, left->frame_count + right->frame_count - 1 );
1140                                 mlt_playlist_remove( this, i -- );
1141                         }
1142                 }
1143
1144                 if ( !keep_length )
1145                 {
1146                         playlist_entry *last = this->list[ this->count - 1 ];
1147                         if ( mlt_producer_is_blank( last->producer ) )
1148                                 mlt_playlist_remove( this, this->count - 1 );
1149                 }
1150
1151                 mlt_events_unblock( properties, properties );
1152                 mlt_playlist_virtual_refresh( this );
1153         }
1154 }
1155
1156 /** Determine if the specified clip index is a blank.
1157 */
1158
1159 int mlt_playlist_is_blank( mlt_playlist this, int clip )
1160 {
1161         return this == NULL || mlt_producer_is_blank( mlt_playlist_get_clip( this, clip ) );
1162 }
1163
1164 /** Replace the specified clip with a blank and return the clip.
1165 */
1166
1167 mlt_producer mlt_playlist_replace_with_blank( mlt_playlist this, int clip )
1168 {
1169         mlt_producer producer = NULL;
1170         if ( !mlt_playlist_is_blank( this, clip ) )
1171         {
1172                 playlist_entry *entry = this->list[ clip ];
1173                 int in = entry->frame_in;
1174                 int out = entry->frame_out;
1175                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1176                 producer = entry->producer;
1177                 mlt_properties_inc_ref( MLT_PRODUCER_PROPERTIES( producer ) );
1178                 mlt_events_block( properties, properties );
1179                 mlt_playlist_remove( this, clip );
1180                 mlt_playlist_blank( this, out - in );
1181                 mlt_playlist_move( this, this->count - 1, clip );
1182                 mlt_events_unblock( properties, properties );
1183                 mlt_playlist_virtual_refresh( this );
1184                 mlt_producer_set_in_and_out( producer, in, out );
1185         }
1186         return producer;
1187 }
1188
1189 void mlt_playlist_insert_blank( mlt_playlist this, int clip, int length )
1190 {
1191         if ( this != NULL && length > 0 )
1192         {
1193                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1194                 mlt_events_block( properties, properties );
1195                 mlt_playlist_blank( this, length );
1196                 mlt_playlist_move( this, this->count - 1, clip );
1197                 mlt_events_unblock( properties, properties );
1198                 mlt_playlist_virtual_refresh( this );
1199         }
1200 }
1201
1202 void mlt_playlist_pad_blanks( mlt_playlist this, int position, int length, int find )
1203 {
1204         if ( this != NULL && length != 0 )
1205         {
1206                 int clip = mlt_playlist_get_clip_index_at( this, position );
1207                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1208                 mlt_events_block( properties, properties );
1209                 if ( find && clip < this->count && !mlt_playlist_is_blank( this, clip ) )
1210                         clip ++;
1211                 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1212                 {
1213                         mlt_playlist_clip_info info;
1214                         mlt_playlist_get_clip_info( this, &info, clip );
1215                         if ( info.frame_out + length > info.frame_in )
1216                                 mlt_playlist_resize_clip( this, clip, info.frame_in, info.frame_out + length );
1217                         else
1218                                 mlt_playlist_remove( this, clip );
1219                 }
1220                 else if ( find && clip < this->count && length > 0  )
1221                 {
1222                         mlt_playlist_insert_blank( this, clip, length );
1223                 }
1224                 mlt_events_unblock( properties, properties );
1225                 mlt_playlist_virtual_refresh( this );
1226         }
1227 }
1228
1229 int mlt_playlist_insert_at( mlt_playlist this, int position, mlt_producer producer, int mode )
1230 {
1231         int ret = this == NULL || position < 0 || producer == NULL;
1232         if ( ret == 0 )
1233         {
1234                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1235                 int length = mlt_producer_get_playtime( producer );
1236                 int clip = mlt_playlist_get_clip_index_at( this, position );
1237                 mlt_playlist_clip_info info;
1238                 mlt_playlist_get_clip_info( this, &info, clip );
1239                 mlt_events_block( properties, this );
1240                 if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
1241                 {
1242                         // Split and move to new clip if need be
1243                         if ( mlt_playlist_split( this, clip, position - info.start ) == 0 )
1244                                 mlt_playlist_get_clip_info( this, &info, ++ clip );
1245
1246                         // Split again if need be
1247                         if ( length < info.frame_count )
1248                                 mlt_playlist_split( this, clip, length - 1 );
1249
1250                         // Remove
1251                         mlt_playlist_remove( this, clip );
1252
1253                         // Insert
1254                         mlt_playlist_insert( this, producer, clip, -1, -1 );
1255                         ret = clip;
1256                 }
1257                 else if ( clip < this->count )
1258                 {
1259                         if ( position > info.start + info.frame_count / 2 )
1260                                 clip ++;
1261                         if ( mode == 1 && clip < this->count && mlt_playlist_is_blank( this, clip ) )
1262                         {
1263                                 mlt_playlist_get_clip_info( this, &info, clip );
1264                                 if ( length < info.frame_count )
1265                                         mlt_playlist_split( this, clip, length );
1266                                 mlt_playlist_remove( this, clip );
1267                         }
1268                         mlt_playlist_insert( this, producer, clip, -1, -1 );
1269                         ret = clip;
1270                 }
1271                 else 
1272                 {
1273                         if ( mode == 1 )
1274                                 mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) );
1275                         mlt_playlist_append( this, producer );
1276                         ret = this->count - 1;
1277                 }
1278                 mlt_events_unblock( properties, this );
1279                 mlt_playlist_virtual_refresh( this );
1280         }
1281         else
1282         {
1283                 ret = -1;
1284         }
1285         return ret;
1286 }
1287
1288 int mlt_playlist_clip_start( mlt_playlist this, int clip )
1289 {
1290         mlt_playlist_clip_info info;
1291         if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1292                 return info.start;
1293         return 0;
1294 }
1295
1296 int mlt_playlist_clip_length( mlt_playlist this, int clip )
1297 {
1298         mlt_playlist_clip_info info;
1299         if ( mlt_playlist_get_clip_info( this, &info, clip ) == 0 )
1300                 return info.frame_count;
1301         return 0;
1302 }
1303
1304 int mlt_playlist_blanks_from( mlt_playlist this, int clip, int bounded )
1305 {
1306         int count = 0;
1307         mlt_playlist_clip_info info;
1308         if ( this != NULL && clip < this->count )
1309         {
1310                 mlt_playlist_get_clip_info( this, &info, clip );
1311                 if ( mlt_playlist_is_blank( this, clip ) )
1312                         count += info.frame_count;
1313                 if ( bounded == 0 )
1314                         bounded = this->count;
1315                 for ( clip ++; clip < this->count && bounded >= 0; clip ++ )
1316                 {
1317                         mlt_playlist_get_clip_info( this, &info, clip );
1318                         if ( mlt_playlist_is_blank( this, clip ) )
1319                                 count += info.frame_count;
1320                         else
1321                                 bounded --;
1322                 }
1323         }
1324         return count;
1325 }
1326
1327 int mlt_playlist_remove_region( mlt_playlist this, int position, int length )
1328 {
1329         int index = mlt_playlist_get_clip_index_at( this, position );
1330         if ( index >= 0 && index < this->count )
1331         {
1332                 mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
1333                 int clip_start = mlt_playlist_clip_start( this, index );
1334                 int clip_length = mlt_playlist_clip_length( this, index );
1335                 int list_length = mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( this ) );
1336                 mlt_events_block( properties, this );
1337
1338                 if ( position + length > list_length )
1339                         length -= ( position + length - list_length );
1340
1341                 if ( clip_start < position )
1342                 {
1343                         mlt_playlist_split( this, index ++, position - clip_start );
1344                         clip_length -= position - clip_start;
1345                 }
1346
1347                 while( length > 0 )
1348                 {
1349                         if ( mlt_playlist_clip_length( this, index ) > length )
1350                                 mlt_playlist_split( this, index, length );
1351                         length -= mlt_playlist_clip_length( this, index );
1352                         mlt_playlist_remove( this, index );
1353                 }
1354         
1355                 mlt_playlist_consolidate_blanks( this, 0 );
1356                 mlt_events_unblock( properties, this );
1357                 mlt_playlist_virtual_refresh( this );
1358
1359                 // Just to be sure, we'll get the clip index again...
1360                 index = mlt_playlist_get_clip_index_at( this, position );
1361         }
1362         return index;
1363 }
1364
1365 int mlt_playlist_move_region( mlt_playlist this, int position, int length, int new_position )
1366 {
1367         if ( this != NULL )
1368         {
1369         }
1370         return 0;
1371 }
1372
1373 /** Get the current frame.
1374 */
1375
1376 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1377 {
1378         // Check that we have a producer
1379         if ( producer == NULL )
1380         {
1381                 *frame = mlt_frame_init( );
1382                 return 0;
1383         }
1384
1385         // Get this mlt_playlist
1386         mlt_playlist this = producer->child;
1387
1388         // Need to ensure the frame is deinterlaced when repeating 1 frame
1389         int progressive = 0;
1390
1391         // Get the real producer
1392         mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1393
1394         // Check that we have a producer
1395         if ( real == NULL )
1396         {
1397                 *frame = mlt_frame_init( );
1398                 return 0;
1399         }
1400
1401         // Get the frame
1402         mlt_service_get_frame( real, frame, index );
1403
1404         // Check if we're at the end of the clip
1405         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
1406         if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1407                 mlt_playlist_virtual_set_out( this );
1408
1409         // Set the consumer progressive property
1410         if ( progressive )
1411         {
1412                 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1413                 mlt_properties_set_int( properties, "test_audio", 1 );
1414         }
1415
1416         // Check for notifier and call with appropriate argument
1417         mlt_properties playlist_properties = MLT_PRODUCER_PROPERTIES( producer );
1418         void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1419         if ( notifier != NULL )
1420         {
1421                 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1422                 notifier( argument );
1423         }
1424
1425         // Update position on the frame we're creating
1426         mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1427
1428         // Position ourselves on the next frame
1429         mlt_producer_prepare_next( producer );
1430
1431         return 0;
1432 }
1433
1434 /** Close the playlist.
1435 */
1436
1437 void mlt_playlist_close( mlt_playlist this )
1438 {
1439         if ( this != NULL && mlt_properties_dec_ref( MLT_PLAYLIST_PROPERTIES( this ) ) <= 0 )
1440         {
1441                 int i = 0;
1442                 this->parent.close = NULL;
1443                 for ( i = 0; i < this->count; i ++ )
1444                 {
1445                         mlt_event_close( this->list[ i ]->event );
1446                         mlt_producer_close( this->list[ i ]->producer );
1447                         free( this->list[ i ] );
1448                 }
1449                 mlt_producer_close( &this->blank );
1450                 mlt_producer_close( &this->parent );
1451                 free( this->list );
1452                 free( this );
1453         }
1454 }