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