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