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