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