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