]> git.sesse.net Git - mlt/blob - src/framework/mlt_playlist.c
Splits, joins and repeats
[mlt] / src / framework / mlt_playlist.c
1 /*
2  * mlt_playlist.c -- playlist service class
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "mlt_playlist.h"
24 #include "mlt_tractor.h"
25 #include "mlt_field.h"
26 #include "mlt_frame.h"
27 #include "mlt_transition.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 /** Virtual playlist entry.
34 */
35
36 typedef struct playlist_entry_s
37 {
38         mlt_producer producer;
39         mlt_position frame_in;
40         mlt_position frame_out;
41         mlt_position frame_count;
42         int repeat;
43         mlt_position producer_length;
44         mlt_event event;
45         int mix_in_length;
46         int mix_out_length;
47         struct playlist_entry_s *mix_in;
48         struct playlist_entry_s *mix_out;
49 }
50 playlist_entry;
51
52 /** Private definition.
53 */
54
55 struct mlt_playlist_s
56 {
57         struct mlt_producer_s parent;
58         struct mlt_producer_s blank;
59
60         int size;
61         int count;
62         playlist_entry **list;
63 };
64
65 /** Forward declarations
66 */
67
68 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
69 static int mlt_playlist_unmix( mlt_playlist this, int clip );
70 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out );
71
72 /** Constructor.
73 */
74
75 mlt_playlist mlt_playlist_init( )
76 {
77         mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
78         if ( this != NULL )
79         {
80                 mlt_producer producer = &this->parent;
81
82                 // Construct the producer
83                 mlt_producer_init( producer, this );
84
85                 // Override the producer get_frame
86                 producer->get_frame = producer_get_frame;
87
88                 // Define the destructor
89                 producer->close = ( mlt_destructor )mlt_playlist_close;
90                 producer->close_object = this;
91
92                 // Initialise blank
93                 mlt_producer_init( &this->blank, NULL );
94                 mlt_properties_set( mlt_producer_properties( &this->blank ), "mlt_service", "blank" );
95                 mlt_properties_set( mlt_producer_properties( &this->blank ), "resource", "blank" );
96
97                 // Indicate that this producer is a playlist
98                 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
99
100                 // Specify the eof condition
101                 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
102                 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
103                 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
104                 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
105                 mlt_properties_set_position( mlt_playlist_properties( this ), "out", 0 );
106                 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
107
108                 this->size = 10;
109                 this->list = malloc( this->size * sizeof( playlist_entry * ) );
110         }
111         
112         return this;
113 }
114
115 /** Get the producer associated to this playlist.
116 */
117
118 mlt_producer mlt_playlist_producer( mlt_playlist this )
119 {
120         return this != NULL ? &this->parent : NULL;
121 }
122
123 /** Get the service associated to this playlist.
124 */
125
126 mlt_service mlt_playlist_service( mlt_playlist this )
127 {
128         return mlt_producer_service( &this->parent );
129 }
130
131 /** Get the propertues associated to this playlist.
132 */
133
134 mlt_properties mlt_playlist_properties( mlt_playlist this )
135 {
136         return mlt_producer_properties( &this->parent );
137 }
138
139 /** Refresh the playlist after a clip has been changed.
140 */
141
142 static int mlt_playlist_virtual_refresh( mlt_playlist this )
143 {
144         // Obtain the properties
145         mlt_properties properties = mlt_playlist_properties( this );
146
147         // Get the fps of the first producer
148         double fps = mlt_properties_get_double( properties, "first_fps" );
149         int i = 0;
150         mlt_position frame_count = 0;
151
152         for ( i = 0; i < this->count; i ++ )
153         {
154                 // Get the producer
155                 mlt_producer producer = this->list[ i ]->producer;
156                 int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
157
158                 // If fps is 0
159                 if ( fps == 0 )
160                 {
161                         // Inherit it from the producer
162                         fps = mlt_producer_get_fps( producer );
163                 }
164                 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
165                 {
166                         // Generate a warning for now - the following attempt to fix may fail
167                         fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
168
169                         // It should be safe to impose fps on an image producer, but not necessarily safe for video
170                         mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
171                 }
172
173                 // Check if the length of the producer has changed
174                 if ( this->list[ i ]->producer != &this->blank &&
175                          ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
176                            this->list[ i ]->frame_out != mlt_producer_get_out( producer ) ) )
177                 {
178                         // This clip should be removed...
179                         if ( current_length < 1 )
180                         {
181                                 this->list[ i ]->frame_in = 0;
182                                 this->list[ i ]->frame_out = -1;
183                                 this->list[ i ]->frame_count = 0;
184                         }
185                         else 
186                         {
187                                 this->list[ i ]->frame_in = mlt_producer_get_in( producer );
188                                 this->list[ i ]->frame_out = mlt_producer_get_out( producer );
189                                 this->list[ i ]->frame_count = current_length;
190                         }
191
192                         // Update the producer_length
193                         this->list[ i ]->producer_length = current_length;
194                 }
195
196                 // Calculate the frame_count
197                 this->list[ i ]->frame_count = ( this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1 ) * this->list[ i ]->repeat;
198
199                 // Update the frame_count for this clip
200                 frame_count += this->list[ i ]->frame_count;
201         }
202
203         // Refresh all properties
204         mlt_properties_set_double( properties, "first_fps", fps );
205         mlt_properties_set_double( properties, "fps", fps == 0 ? 25 : fps );
206         mlt_events_block( properties, properties );
207         mlt_properties_set_position( properties, "length", frame_count );
208         mlt_events_unblock( properties, properties );
209         mlt_properties_set_position( properties, "out", frame_count - 1 );
210
211         return 0;
212 }
213
214 /** Listener for producers on the playlist.
215 */
216
217 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
218 {
219         mlt_playlist_virtual_refresh( this );
220 }
221
222 /** Append to the virtual playlist.
223 */
224
225 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer source, mlt_position in, mlt_position out )
226 {
227         char *resource = source != NULL ? mlt_properties_get( mlt_producer_properties( source ), "resource" ) : NULL;
228         mlt_producer producer = NULL;
229         mlt_properties properties = NULL;
230
231         // If we have a cut, then use the in/out points from the cut
232         if ( resource == NULL || !strcmp( resource, "blank" ) )
233         {
234                 producer = &this->blank;
235                 properties = mlt_producer_properties( producer );
236                 mlt_properties_inc_ref( properties );
237         }
238         else if ( mlt_producer_is_cut( source ) )
239         {
240                 producer = source;
241                 if ( in == -1 )
242                         in = mlt_producer_get_in( producer );
243                 if ( out == -1 || out > mlt_producer_get_out( producer ) )
244                         out = mlt_producer_get_out( producer );
245                 properties = mlt_producer_properties( producer );
246                 mlt_properties_inc_ref( properties );
247         }
248         else
249         {
250                 producer = mlt_producer_cut( source, in, out );
251                 if ( in == -1 || in < mlt_producer_get_in( producer ) )
252                         in = mlt_producer_get_in( producer );
253                 if ( out == -1 || out > mlt_producer_get_out( producer ) )
254                         out = mlt_producer_get_out( producer );
255                 properties = mlt_producer_properties( producer );
256         }
257
258         // Check that we have room
259         if ( this->count >= this->size )
260         {
261                 int i;
262                 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
263                 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
264                 this->size += 10;
265         }
266
267         this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
268         this->list[ this->count ]->producer = producer;
269         this->list[ this->count ]->frame_in = in;
270         this->list[ this->count ]->frame_out = out;
271         this->list[ this->count ]->frame_count = out - in + 1;
272         this->list[ this->count ]->repeat = 1;
273         this->list[ this->count ]->producer_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
274         this->list[ this->count ]->event = mlt_events_listen( properties, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
275         mlt_event_inc_ref( this->list[ this->count ]->event );
276
277         mlt_properties_set( properties, "eof", "pause" );
278         mlt_producer_set_speed( producer, 0 );
279
280         this->count ++;
281
282         return mlt_playlist_virtual_refresh( this );
283 }
284
285 /** Seek in the virtual playlist.
286 */
287
288 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this )
289 {
290         // Default producer to blank
291         mlt_producer producer = NULL;
292
293         // Map playlist position to real producer in virtual playlist
294         mlt_position position = mlt_producer_frame( &this->parent );
295
296         mlt_position original = position;
297
298         // Total number of frames
299         int64_t total = 0;
300
301         // Get the properties
302         mlt_properties properties = mlt_playlist_properties( this );
303
304         // Get the eof handling
305         char *eof = mlt_properties_get( properties, "eof" );
306
307         // Index for the main loop
308         int i = 0;
309
310         // Loop for each producer until found
311         for ( i = 0; i < this->count; i ++ )
312         {
313                 // Increment the total
314                 total += this->list[ i ]->frame_count;
315
316                 // Check if the position indicates that we have found the clip
317                 // Note that 0 length clips get skipped automatically
318                 if ( position < this->list[ i ]->frame_count )
319                 {
320                         // Found it, now break
321                         producer = this->list[ i ]->producer;
322                         break;
323                 }
324                 else
325                 {
326                         // Decrement position by length of this entry
327                         position -= this->list[ i ]->frame_count;
328                 }
329         }
330
331         // Seek in real producer to relative position
332         if ( producer != NULL )
333         {
334                 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
335                 mlt_producer_seek( producer, position % count );
336         }
337         else if ( !strcmp( eof, "pause" ) && total > 0 )
338         {
339                 playlist_entry *entry = this->list[ this->count - 1 ];
340                 mlt_producer this_producer = mlt_playlist_producer( this );
341                 mlt_producer_seek( this_producer, original - 1 );
342                 producer = entry->producer;
343                 mlt_producer_seek( producer, entry->frame_out );
344                 mlt_producer_set_speed( this_producer, 0 );
345                 mlt_producer_set_speed( producer, 0 );
346         }
347         else if ( !strcmp( eof, "loop" ) && total > 0 )
348         {
349                 playlist_entry *entry = this->list[ 0 ];
350                 mlt_producer this_producer = mlt_playlist_producer( this );
351                 mlt_producer_seek( this_producer, 0 );
352                 producer = entry->producer;
353                 mlt_producer_seek( producer, 0 );
354         }
355         else
356         {
357                 producer = &this->blank;
358         }
359
360         return mlt_producer_service( producer );
361 }
362
363 /** Invoked when a producer indicates that it has prematurely reached its end.
364 */
365
366 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
367 {
368         // Default producer to blank
369         mlt_producer producer = &this->blank;
370
371         // Map playlist position to real producer in virtual playlist
372         mlt_position position = mlt_producer_frame( &this->parent );
373
374         // Loop through the virtual playlist
375         int i = 0;
376
377         for ( i = 0; i < this->count; i ++ )
378         {
379                 if ( position < this->list[ i ]->frame_count )
380                 {
381                         // Found it, now break
382                         producer = this->list[ i ]->producer;
383                         break;
384                 }
385                 else
386                 {
387                         // Decrement position by length of this entry
388                         position -= this->list[ i ]->frame_count;
389                 }
390         }
391
392         // Seek in real producer to relative position
393         if ( i < this->count && this->list[ i ]->frame_out != position )
394         {
395                 // Update the frame_count for the changed clip (hmmm)
396                 this->list[ i ]->frame_out = position;
397                 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
398
399                 // Refresh the playlist
400                 mlt_playlist_virtual_refresh( this );
401         }
402
403         return producer;
404 }
405
406 /** Obtain the current clips index.
407 */
408
409 int mlt_playlist_current_clip( mlt_playlist this )
410 {
411         // Map playlist position to real producer in virtual playlist
412         mlt_position position = mlt_producer_frame( &this->parent );
413
414         // Loop through the virtual playlist
415         int i = 0;
416
417         for ( i = 0; i < this->count; i ++ )
418         {
419                 if ( position < this->list[ i ]->frame_count )
420                 {
421                         // Found it, now break
422                         break;
423                 }
424                 else
425                 {
426                         // Decrement position by length of this entry
427                         position -= this->list[ i ]->frame_count;
428                 }
429         }
430
431         return i;
432 }
433
434 /** Obtain the current clips producer.
435 */
436
437 mlt_producer mlt_playlist_current( mlt_playlist this )
438 {
439         int i = mlt_playlist_current_clip( this );
440         if ( i < this->count )
441                 return this->list[ i ]->producer;
442         else
443                 return &this->blank;
444 }
445
446 /** Get the position which corresponds to the start of the next clip.
447 */
448
449 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
450 {
451         mlt_position position = 0;
452         int absolute_clip = index;
453         int i = 0;
454
455         // Determine the absolute clip
456         switch ( whence )
457         {
458                 case mlt_whence_relative_start:
459                         absolute_clip = index;
460                         break;
461
462                 case mlt_whence_relative_current:
463                         absolute_clip = mlt_playlist_current_clip( this ) + index;
464                         break;
465
466                 case mlt_whence_relative_end:
467                         absolute_clip = this->count - index;
468                         break;
469         }
470
471         // Check that we're in a valid range
472         if ( absolute_clip < 0 )
473                 absolute_clip = 0;
474         else if ( absolute_clip > this->count )
475                 absolute_clip = this->count;
476
477         // Now determine the position
478         for ( i = 0; i < absolute_clip; i ++ )
479                 position += this->list[ i ]->frame_count;
480
481         return position;
482 }
483
484 /** Get all the info about the clip specified.
485 */
486
487 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
488 {
489         int error = index < 0 || index >= this->count;
490         memset( info, 0, sizeof( mlt_playlist_clip_info ) );
491         if ( !error )
492         {
493                 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
494                 mlt_properties properties = mlt_producer_properties( producer );
495                 info->clip = index;
496                 info->producer = producer;
497                 info->cut = this->list[ index ]->producer;
498                 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
499                 info->resource = mlt_properties_get( properties, "resource" );
500                 info->frame_in = this->list[ index ]->frame_in;
501                 info->frame_out = this->list[ index ]->frame_out;
502                 info->frame_count = this->list[ index ]->frame_count;
503                 info->repeat = this->list[ index ]->repeat;
504                 info->length = mlt_producer_get_length( producer );
505                 info->fps = mlt_producer_get_fps( producer );
506         }
507
508         return error;
509 }
510
511 /** Get number of clips in the playlist.
512 */
513
514 int mlt_playlist_count( mlt_playlist this )
515 {
516         return this->count;
517 }
518
519 /** Clear the playlist.
520 */
521
522 int mlt_playlist_clear( mlt_playlist this )
523 {
524         int i;
525         for ( i = 0; i < this->count; i ++ )
526         {
527                 mlt_event_close( this->list[ i ]->event );
528                 mlt_producer_close( this->list[ i ]->producer );
529         }
530         this->count = 0;
531         mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
532         return mlt_playlist_virtual_refresh( this );
533 }
534
535 /** Append a producer to the playlist.
536 */
537
538 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
539 {
540         // Append to virtual list
541         return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
542 }
543
544 /** Append a producer to the playlist with in/out points.
545 */
546
547 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
548 {
549         // Append to virtual list
550         if ( in != -1 && out != -1 )
551                 return mlt_playlist_virtual_append( this, producer, in, out );
552         else
553                 return mlt_playlist_append( this, producer );
554 }
555
556 /** Append a blank to the playlist of a given length.
557 */
558
559 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
560 {
561         // Append to the virtual list
562         return mlt_playlist_virtual_append( this, &this->blank, 0, length );
563 }
564
565 /** Insert a producer into the playlist.
566 */
567
568 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
569 {
570         // Append to end
571         mlt_events_block( mlt_playlist_properties( this ), this );
572         mlt_playlist_append_io( this, producer, in, out );
573
574         // Move to the position specified
575         mlt_playlist_move( this, this->count - 1, where );
576         mlt_events_unblock( mlt_playlist_properties( this ), this );
577
578         return mlt_playlist_virtual_refresh( this );
579 }
580
581 /** Remove an entry in the playlist.
582 */
583
584 int mlt_playlist_remove( mlt_playlist this, int where )
585 {
586         int error = where < 0 || where >= this->count;
587         if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
588         {
589                 // We need to know the current clip and the position within the playlist
590                 int current = mlt_playlist_current_clip( this );
591                 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
592
593                 // We need all the details about the clip we're removing
594                 mlt_playlist_clip_info where_info;
595                 playlist_entry *entry = this->list[ where ];
596
597                 // Loop variable
598                 int i = 0;
599
600                 // Get the clip info 
601                 mlt_playlist_get_clip_info( this, &where_info, where );
602
603                 // Make sure the clip to be removed is valid and correct if necessary
604                 if ( where < 0 ) 
605                         where = 0;
606                 if ( where >= this->count )
607                         where = this->count - 1;
608
609                 // Close the producer associated to the clip info
610                 mlt_event_close( entry->event );
611                 mlt_producer_close( entry->producer );
612                 if ( entry->mix_in != NULL )
613                         entry->mix_in->mix_out = NULL;
614                 if ( entry->mix_out != NULL )
615                         entry->mix_out->mix_in = NULL;
616
617                 // Reorganise the list
618                 for ( i = where + 1; i < this->count; i ++ )
619                         this->list[ i - 1 ] = this->list[ i ];
620                 this->count --;
621
622                 // Correct position
623                 if ( where == current )
624                         mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
625                 else if ( where < current && this->count > 0 )
626                         mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
627                 else if ( this->count == 0 )
628                         mlt_producer_seek( mlt_playlist_producer( this ), 0 );
629
630                 // Free the entry
631                 free( entry );
632
633                 // Refresh the playlist
634                 mlt_playlist_virtual_refresh( this );
635         }
636
637         return error;
638 }
639
640 /** Move an entry in the playlist.
641 */
642
643 int mlt_playlist_move( mlt_playlist this, int src, int dest )
644 {
645         int i;
646
647         /* We need to ensure that the requested indexes are valid and correct it as necessary */
648         if ( src < 0 ) 
649                 src = 0;
650         if ( src >= this->count )
651                 src = this->count - 1;
652
653         if ( dest < 0 ) 
654                 dest = 0;
655         if ( dest >= this->count )
656                 dest = this->count - 1;
657         
658         if ( src != dest && this->count > 1 )
659         {
660                 int current = mlt_playlist_current_clip( this );
661                 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
662                 playlist_entry *src_entry = NULL;
663
664                 // We need all the details about the current clip
665                 mlt_playlist_clip_info current_info;
666
667                 mlt_playlist_get_clip_info( this, &current_info, current );
668                 position -= current_info.start;
669
670                 if ( current == src )
671                         current = dest;
672                 else if ( current > src && current < dest )
673                         current ++;
674                 else if ( current == dest )
675                         current = src;
676
677                 src_entry = this->list[ src ];
678                 if ( src > dest )
679                 {
680                         for ( i = src; i > dest; i -- )
681                                 this->list[ i ] = this->list[ i - 1 ];
682                 }
683                 else
684                 {
685                         for ( i = src; i < dest; i ++ )
686                                 this->list[ i ] = this->list[ i + 1 ];
687                 }
688                 this->list[ dest ] = src_entry;
689
690                 mlt_playlist_get_clip_info( this, &current_info, current );
691                 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
692                 mlt_playlist_virtual_refresh( this );
693         }
694
695         return 0;
696 }
697
698 /** Repeat the specified clip n times.
699 */
700
701 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
702 {
703         int error = repeat < 1 || clip < 0 || clip >= this->count;
704         if ( error == 0 )
705         {
706                 playlist_entry *entry = this->list[ clip ];
707                 entry->repeat = repeat;
708                 mlt_playlist_virtual_refresh( this );
709         }
710         return error;
711 }
712
713 /** Resize the specified clip.
714 */
715
716 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
717 {
718         int error = clip < 0 || clip >= this->count;
719         if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
720         {
721                 playlist_entry *entry = this->list[ clip ];
722                 mlt_producer producer = entry->producer;
723
724                 if ( in <= -1 )
725                         in = 0;
726                 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
727                         out = mlt_producer_get_length( producer ) - 1;
728
729                 if ( out < in )
730                 {
731                         mlt_position t = in;
732                         in = out;
733                         out = t;
734                 }
735
736                 mlt_producer_set_in_and_out( producer, in, out );
737         }
738         return error;
739 }
740
741 /** Split a clip on the playlist at the given position.
742 */
743
744 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
745 {
746         int error = clip < 0 || clip >= this->count;
747         if ( error == 0 )
748         {
749                 playlist_entry *entry = this->list[ clip ];
750                 if ( position > 0 && position < entry->frame_count )
751                 {
752                         mlt_producer split = NULL;
753                         int in = entry->frame_in;
754                         int out = entry->frame_out;
755                         mlt_events_block( mlt_playlist_properties( this ), this );
756                         mlt_playlist_resize_clip( this, clip, in, in + position );
757                         split = mlt_producer_cut( entry->producer, in + position + 1, out );
758                         mlt_playlist_insert( this, split, clip + 1, 0, -1 );
759                         mlt_events_unblock( mlt_playlist_properties( this ), this );
760                         mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL );
761                 }
762                 else
763                 {
764                         error = 1;
765                 }
766         }
767         return error;
768 }
769
770 /** Join 1 or more consecutive clips.
771 */
772
773 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
774 {
775         int error = clip < 0 || ( clip ) >= this->count;
776         if ( error == 0 )
777         {
778                 int i = clip;
779                 mlt_playlist new_clip = mlt_playlist_init( );
780                 mlt_events_block( mlt_playlist_properties( this ), this );
781                 if ( clip + count >= this->count )
782                         count = this->count - clip;
783                 for ( i = 0; i <= count; i ++ )
784                 {
785                         playlist_entry *entry = this->list[ clip ];
786                         mlt_playlist_append( new_clip, entry->producer );
787                         mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
788                         mlt_playlist_remove( this, clip );
789                 }
790                 mlt_events_unblock( mlt_playlist_properties( this ), this );
791                 mlt_playlist_insert( this, mlt_playlist_producer( new_clip ), clip, 0, -1 );
792                 mlt_playlist_close( new_clip );
793         }
794         return error;
795 }
796
797 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
798 {
799         int error = ( clip < 0 || clip + 1 >= this->count ) && this->list[ clip ]->mix_out != NULL;
800         if ( error == 0 )
801         {
802                 playlist_entry *clip_a = this->list[ clip ];
803                 playlist_entry *clip_b = this->list[ clip + 1 ];
804                 mlt_producer track_a;
805                 mlt_producer track_b;
806                 mlt_tractor tractor = mlt_tractor_new( );
807                 mlt_events_block( mlt_playlist_properties( this ), this );
808
809                 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
810                 mlt_properties_set_int( mlt_producer_properties( track_a ), "cut", 1 );
811                 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
812                 mlt_properties_set_int( mlt_producer_properties( track_b ), "cut", 1 );
813
814                 mlt_tractor_set_track( tractor, track_a, 0 );
815                 mlt_tractor_set_track( tractor, track_b, 1 );
816                 mlt_playlist_insert( this, mlt_tractor_producer( tractor ), clip + 1, -1, -1 );
817                 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
818
819                 if ( transition != NULL )
820                 {
821                         mlt_field field = mlt_tractor_field( tractor );
822                         mlt_field_plant_transition( field, transition, 0, 1 );
823                         mlt_transition_set_in_and_out( transition, 0, length - 1 );
824                 }
825
826                 if ( clip_b->frame_out - clip_b->frame_in > length )
827                         mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
828                 else
829                         mlt_playlist_remove( this, clip + 2 );
830
831                 if ( clip_a->frame_out - clip_a->frame_in > length )
832                         mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
833                 else
834                         mlt_playlist_remove( this, clip );
835
836                 mlt_events_unblock( mlt_playlist_properties( this ), this );
837                 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL );
838                 mlt_producer_close( track_a );
839                 mlt_producer_close( track_b );
840                 mlt_tractor_close( tractor );
841         }
842         return error;
843 }
844
845 static int mlt_playlist_unmix( mlt_playlist this, int clip )
846 {
847         int error = ( clip < 0 || clip >= this->count ); 
848
849         // Ensure that the clip request is actually a mix
850         if ( error == 0 )
851         {
852                 mlt_producer producer = this->list[ clip ]->producer;
853                 mlt_properties properties = mlt_producer_properties( producer );
854                 error = !mlt_properties_get_int( properties, "mlt_mix" );
855         }
856
857         if ( error == 0 )
858         {
859                 playlist_entry *mix = this->list[ clip ];
860                 playlist_entry *clip_a = mix->mix_in;
861                 playlist_entry *clip_b = mix->mix_out;
862                 int length = mix->mix_in_length;
863                 mlt_events_block( mlt_playlist_properties( this ), this );
864
865                 if ( clip_a != NULL )
866                 {
867                         clip_a->frame_out += length;
868                         clip_a->frame_count += length;
869                         clip_a->mix_out = NULL;
870                         clip_a->mix_out_length = 0;
871                 }
872                 else
873                 {
874                         mlt_tractor tractor = ( mlt_tractor )mix->producer;
875                         mlt_playlist playlist = ( mlt_playlist )mlt_tractor_get_track( tractor, 0 );
876                         mlt_producer producer = playlist->list[ 0 ]->producer;
877                         mlt_playlist_insert( this, producer, clip, playlist->list[0]->frame_in, playlist->list[0]->frame_out );
878                         clip ++;
879                 }
880
881                 if ( clip_b != NULL )
882                 {
883                         clip_b->frame_in -= length;
884                         clip_b->frame_count += length;
885                         clip_b->mix_in = NULL;
886                         clip_b->mix_in_length = 0;
887                 }
888                 else
889                 {
890                         mlt_tractor tractor = ( mlt_tractor )mix->producer;
891                         mlt_playlist playlist = ( mlt_playlist )mlt_tractor_get_track( tractor, 1 );
892                         mlt_producer producer = playlist->list[ 0 ]->producer;
893                         mlt_playlist_insert( this, producer, clip + 1, playlist->list[0]->frame_in, playlist->list[0]->frame_out );
894                 }
895
896                 mlt_properties_set_int( mlt_producer_properties( mix->producer ), "mlt_mix", 0 );
897                 mlt_playlist_remove( this, clip );
898                 mlt_events_unblock( mlt_playlist_properties( this ), this );
899                 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL );
900         }
901         return error;
902 }
903
904 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
905 {
906         int error = ( clip < 0 || clip >= this->count ); 
907
908         // Ensure that the clip request is actually a mix
909         if ( error == 0 )
910         {
911                 mlt_producer producer = this->list[ clip ]->producer;
912                 mlt_properties properties = mlt_producer_properties( producer );
913                 error = !mlt_properties_get_int( properties, "mlt_mix" );
914         }
915
916         if ( error == 0 )
917         {
918                 playlist_entry *mix = this->list[ clip ];
919                 playlist_entry *clip_a = mix->mix_in;
920                 playlist_entry *clip_b = mix->mix_out;
921                 int length = out - in + 1;
922                 int length_diff = length - mix->mix_in_length;
923                 mlt_events_block( mlt_playlist_properties( this ), this );
924
925                 if ( clip_a != NULL )
926                 {
927                         clip_a->frame_out -= length_diff;
928                         clip_a->frame_count -= length_diff;
929                         clip_a->mix_out_length -= length_diff;
930                 }
931
932                 if ( clip_b != NULL )
933                 {
934                         clip_b->frame_in += length_diff;
935                         clip_b->frame_count -= length_diff;
936                         clip_b->mix_in_length -= length_diff;
937                 }
938
939                 mix->frame_in = 0;
940                 mix->frame_out = length - 1;
941                 mix->frame_count = length;
942                 mix->mix_in_length = length;
943                 mix->mix_out_length = length;
944
945                 mlt_events_unblock( mlt_playlist_properties( this ), this );
946                 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL );
947         }
948         return error;
949 }
950
951 /** Get the current frame.
952 */
953
954 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
955 {
956         // Get this mlt_playlist
957         mlt_playlist this = producer->child;
958
959         // Get the real producer
960         mlt_service real = mlt_playlist_virtual_seek( this );
961
962         // Get the frame
963         mlt_service_get_frame( real, frame, index );
964
965         // Check if we're at the end of the clip
966         mlt_properties properties = mlt_frame_properties( *frame );
967         if ( mlt_properties_get_int( properties, "end_of_clip" ) )
968                 mlt_playlist_virtual_set_out( this );
969
970         // Check for notifier and call with appropriate argument
971         mlt_properties playlist_properties = mlt_producer_properties( producer );
972         void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
973         if ( notifier != NULL )
974         {
975                 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
976                 notifier( argument );
977         }
978
979         // Update position on the frame we're creating
980         mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
981
982         // Position ourselves on the next frame
983         mlt_producer_prepare_next( producer );
984
985         return 0;
986 }
987
988 /** Close the playlist.
989 */
990
991 void mlt_playlist_close( mlt_playlist this )
992 {
993         if ( this != NULL && mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
994         {
995                 int i = 0;
996                 this->parent.close = NULL;
997                 for ( i = 0; i < this->count; i ++ )
998                 {
999                         mlt_event_close( this->list[ i ]->event );
1000                         mlt_producer_close( this->list[ i ]->producer );
1001                         free( this->list[ i ] );
1002                 }
1003                 mlt_producer_close( &this->blank );
1004                 mlt_producer_close( &this->parent );
1005                 free( this->list );
1006                 free( this );
1007         }
1008 }