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