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