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