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