ddcc28f7e4be3c51072dd27062b2ab1873f1d383
[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", -1 );
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 static mlt_producer mlt_playlist_locate( mlt_playlist this, mlt_position *position, int *clip, int *total )
290 {
291 // Default producer to NULL
292 mlt_producer producer = NULL;
293
294 // Loop for each producer until found
295 for ( *clip = 0; *clip < this->count; *clip += 1 )
296 {
297 // Increment the total
298 *total += this->list[ *clip ]->frame_count;
299
300 // Check if the position indicates that we have found the clip
301 // Note that 0 length clips get skipped automatically
302 if ( *position < this->list[ *clip ]->frame_count )
303 {
304 // Found it, now break
305 producer = this->list[ *clip ]->producer;
306 break;
307 }
308 else
309 {
310 // Decrement position by length of this entry
311 *position -= this->list[ *clip ]->frame_count;
312 }
313 }
314
315 return producer;
316 }
317
318 /** Seek in the virtual playlist.
319 */
320
321 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressive )
322 {
323 // Map playlist position to real producer in virtual playlist
324 mlt_position position = mlt_producer_frame( &this->parent );
325
326 // Keep the original position since we change it while iterating through the list
327 mlt_position original = position;
328
329 // Clip index and total
330 int i = 0;
331 int total = 0;
332
333 // Locate the producer for the position
334 mlt_producer producer = mlt_playlist_locate( this, &position, &i, &total );
335
336 // Get the properties
337 mlt_properties properties = mlt_playlist_properties( this );
338
339 // Get the eof handling
340 char *eof = mlt_properties_get( properties, "eof" );
341
342 // Seek in real producer to relative position
343 if ( producer != NULL )
344 {
345 int count = this->list[ i ]->frame_count / this->list[ i ]->repeat;
346 *progressive = count == 1;
347 mlt_producer_seek( producer, position % count );
348 }
349 else if ( !strcmp( eof, "pause" ) && total > 0 )
350 {
351 playlist_entry *entry = this->list[ this->count - 1 ];
352 int count = entry->frame_count / entry->repeat;
353 mlt_producer this_producer = mlt_playlist_producer( this );
354 mlt_producer_seek( this_producer, original - 1 );
355 producer = entry->producer;
356 mlt_producer_seek( producer, entry->frame_out % count );
357 mlt_producer_set_speed( this_producer, 0 );
358 mlt_producer_set_speed( producer, 0 );
359 *progressive = count == 1;
360 }
361 else if ( !strcmp( eof, "loop" ) && total > 0 )
362 {
363 playlist_entry *entry = this->list[ 0 ];
364 mlt_producer this_producer = mlt_playlist_producer( this );
365 mlt_producer_seek( this_producer, 0 );
366 producer = entry->producer;
367 mlt_producer_seek( producer, 0 );
368 }
369 else
370 {
371 producer = &this->blank;
372 }
373
374 return mlt_producer_service( producer );
375 }
376
377 /** Invoked when a producer indicates that it has prematurely reached its end.
378 */
379
380 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
381 {
382 // Default producer to blank
383 mlt_producer producer = &this->blank;
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 producer = this->list[ i ]->producer;
397 break;
398 }
399 else
400 {
401 // Decrement position by length of this entry
402 position -= this->list[ i ]->frame_count;
403 }
404 }
405
406 // Seek in real producer to relative position
407 if ( i < this->count && this->list[ i ]->frame_out != position )
408 {
409 // Update the frame_count for the changed clip (hmmm)
410 this->list[ i ]->frame_out = position;
411 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
412
413 // Refresh the playlist
414 mlt_playlist_virtual_refresh( this );
415 }
416
417 return producer;
418 }
419
420 /** Obtain the current clips index.
421 */
422
423 int mlt_playlist_current_clip( mlt_playlist this )
424 {
425 // Map playlist position to real producer in virtual playlist
426 mlt_position position = mlt_producer_frame( &this->parent );
427
428 // Loop through the virtual playlist
429 int i = 0;
430
431 for ( i = 0; i < this->count; i ++ )
432 {
433 if ( position < this->list[ i ]->frame_count )
434 {
435 // Found it, now break
436 break;
437 }
438 else
439 {
440 // Decrement position by length of this entry
441 position -= this->list[ i ]->frame_count;
442 }
443 }
444
445 return i;
446 }
447
448 /** Obtain the current clips producer.
449 */
450
451 mlt_producer mlt_playlist_current( mlt_playlist this )
452 {
453 int i = mlt_playlist_current_clip( this );
454 if ( i < this->count )
455 return this->list[ i ]->producer;
456 else
457 return &this->blank;
458 }
459
460 /** Get the position which corresponds to the start of the next clip.
461 */
462
463 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
464 {
465 mlt_position position = 0;
466 int absolute_clip = index;
467 int i = 0;
468
469 // Determine the absolute clip
470 switch ( whence )
471 {
472 case mlt_whence_relative_start:
473 absolute_clip = index;
474 break;
475
476 case mlt_whence_relative_current:
477 absolute_clip = mlt_playlist_current_clip( this ) + index;
478 break;
479
480 case mlt_whence_relative_end:
481 absolute_clip = this->count - index;
482 break;
483 }
484
485 // Check that we're in a valid range
486 if ( absolute_clip < 0 )
487 absolute_clip = 0;
488 else if ( absolute_clip > this->count )
489 absolute_clip = this->count;
490
491 // Now determine the position
492 for ( i = 0; i < absolute_clip; i ++ )
493 position += this->list[ i ]->frame_count;
494
495 return position;
496 }
497
498 /** Get all the info about the clip specified.
499 */
500
501 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
502 {
503 int error = index < 0 || index >= this->count;
504 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
505 if ( !error )
506 {
507 mlt_producer producer = mlt_producer_cut_parent( this->list[ index ]->producer );
508 mlt_properties properties = mlt_producer_properties( producer );
509 info->clip = index;
510 info->producer = producer;
511 info->cut = this->list[ index ]->producer;
512 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
513 info->resource = mlt_properties_get( properties, "resource" );
514 info->frame_in = this->list[ index ]->frame_in;
515 info->frame_out = this->list[ index ]->frame_out;
516 info->frame_count = this->list[ index ]->frame_count;
517 info->repeat = this->list[ index ]->repeat;
518 info->length = mlt_producer_get_length( producer );
519 info->fps = mlt_producer_get_fps( producer );
520 }
521
522 return error;
523 }
524
525 /** Get number of clips in the playlist.
526 */
527
528 int mlt_playlist_count( mlt_playlist this )
529 {
530 return this->count;
531 }
532
533 /** Clear the playlist.
534 */
535
536 int mlt_playlist_clear( mlt_playlist this )
537 {
538 int i;
539 for ( i = 0; i < this->count; i ++ )
540 {
541 mlt_event_close( this->list[ i ]->event );
542 mlt_producer_close( this->list[ i ]->producer );
543 }
544 this->count = 0;
545 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
546 return mlt_playlist_virtual_refresh( this );
547 }
548
549 /** Append a producer to the playlist.
550 */
551
552 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
553 {
554 // Append to virtual list
555 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
556 }
557
558 /** Append a producer to the playlist with in/out points.
559 */
560
561 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
562 {
563 // Append to virtual list
564 if ( in != -1 && out != -1 )
565 return mlt_playlist_virtual_append( this, producer, in, out );
566 else
567 return mlt_playlist_append( this, producer );
568 }
569
570 /** Append a blank to the playlist of a given length.
571 */
572
573 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
574 {
575 // Append to the virtual list
576 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
577 }
578
579 /** Insert a producer into the playlist.
580 */
581
582 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
583 {
584 // Append to end
585 mlt_events_block( mlt_playlist_properties( this ), this );
586 mlt_playlist_append_io( this, producer, in, out );
587
588 // Move to the position specified
589 mlt_playlist_move( this, this->count - 1, where );
590 mlt_events_unblock( mlt_playlist_properties( this ), this );
591
592 return mlt_playlist_virtual_refresh( this );
593 }
594
595 /** Remove an entry in the playlist.
596 */
597
598 int mlt_playlist_remove( mlt_playlist this, int where )
599 {
600 int error = where < 0 || where >= this->count;
601 if ( error == 0 && mlt_playlist_unmix( this, where ) != 0 )
602 {
603 // We need to know the current clip and the position within the playlist
604 int current = mlt_playlist_current_clip( this );
605 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
606
607 // We need all the details about the clip we're removing
608 mlt_playlist_clip_info where_info;
609 playlist_entry *entry = this->list[ where ];
610 mlt_properties properties = mlt_producer_properties( entry->producer );
611
612 // Loop variable
613 int i = 0;
614
615 // Get the clip info
616 mlt_playlist_get_clip_info( this, &where_info, where );
617
618 // Make sure the clip to be removed is valid and correct if necessary
619 if ( where < 0 )
620 where = 0;
621 if ( where >= this->count )
622 where = this->count - 1;
623
624 // Reorganise the list
625 for ( i = where + 1; i < this->count; i ++ )
626 this->list[ i - 1 ] = this->list[ i ];
627 this->count --;
628
629 if ( entry->preservation_hack == 0 )
630 {
631 // Decouple from mix_in/out if necessary
632 if ( mlt_properties_get_data( properties, "mix_in", NULL ) != NULL )
633 {
634 mlt_properties mix = mlt_properties_get_data( properties, "mix_in", NULL );
635 mlt_properties_set_data( mix, "mix_out", NULL, 0, NULL, NULL );
636 }
637 if ( mlt_properties_get_data( properties, "mix_out", NULL ) != NULL )
638 {
639 mlt_properties mix = mlt_properties_get_data( properties, "mix_out", NULL );
640 mlt_properties_set_data( mix, "mix_in", NULL, 0, NULL, NULL );
641 }
642
643 mlt_producer_clear( entry->producer );
644 }
645
646 // Close the producer associated to the clip info
647 mlt_event_close( entry->event );
648 mlt_producer_close( entry->producer );
649
650 // Correct position
651 if ( where == current )
652 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
653 else if ( where < current && this->count > 0 )
654 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
655 else if ( this->count == 0 )
656 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
657
658 // Free the entry
659 free( entry );
660
661 // Refresh the playlist
662 mlt_playlist_virtual_refresh( this );
663 }
664
665 return error;
666 }
667
668 /** Move an entry in the playlist.
669 */
670
671 int mlt_playlist_move( mlt_playlist this, int src, int dest )
672 {
673 int i;
674
675 /* We need to ensure that the requested indexes are valid and correct it as necessary */
676 if ( src < 0 )
677 src = 0;
678 if ( src >= this->count )
679 src = this->count - 1;
680
681 if ( dest < 0 )
682 dest = 0;
683 if ( dest >= this->count )
684 dest = this->count - 1;
685
686 if ( src != dest && this->count > 1 )
687 {
688 int current = mlt_playlist_current_clip( this );
689 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
690 playlist_entry *src_entry = NULL;
691
692 // We need all the details about the current clip
693 mlt_playlist_clip_info current_info;
694
695 mlt_playlist_get_clip_info( this, &current_info, current );
696 position -= current_info.start;
697
698 if ( current == src )
699 current = dest;
700 else if ( current > src && current < dest )
701 current ++;
702 else if ( current == dest )
703 current = src;
704
705 src_entry = this->list[ src ];
706 if ( src > dest )
707 {
708 for ( i = src; i > dest; i -- )
709 this->list[ i ] = this->list[ i - 1 ];
710 }
711 else
712 {
713 for ( i = src; i < dest; i ++ )
714 this->list[ i ] = this->list[ i + 1 ];
715 }
716 this->list[ dest ] = src_entry;
717
718 mlt_playlist_get_clip_info( this, &current_info, current );
719 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
720 mlt_playlist_virtual_refresh( this );
721 }
722
723 return 0;
724 }
725
726 /** Repeat the specified clip n times.
727 */
728
729 int mlt_playlist_repeat_clip( mlt_playlist this, int clip, int repeat )
730 {
731 int error = repeat < 1 || clip < 0 || clip >= this->count;
732 if ( error == 0 )
733 {
734 playlist_entry *entry = this->list[ clip ];
735 entry->repeat = repeat;
736 mlt_playlist_virtual_refresh( this );
737 }
738 return error;
739 }
740
741 /** Resize the specified clip.
742 */
743
744 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
745 {
746 int error = clip < 0 || clip >= this->count;
747 if ( error == 0 && mlt_playlist_resize_mix( this, clip, in, out ) != 0 )
748 {
749 playlist_entry *entry = this->list[ clip ];
750 mlt_producer producer = entry->producer;
751
752 if ( in <= -1 )
753 in = 0;
754 if ( out <= -1 || out >= mlt_producer_get_length( producer ) )
755 out = mlt_producer_get_length( producer ) - 1;
756
757 if ( out < in )
758 {
759 mlt_position t = in;
760 in = out;
761 out = t;
762 }
763
764 mlt_producer_set_in_and_out( producer, in, out );
765 }
766 return error;
767 }
768
769 /** Split a clip on the playlist at the given position.
770 */
771
772 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
773 {
774 int error = clip < 0 || clip >= this->count;
775 if ( error == 0 )
776 {
777 playlist_entry *entry = this->list[ clip ];
778 position = position < 0 ? entry->frame_count + position - 1 : position;
779 if ( position >= 0 && position <= entry->frame_count )
780 {
781 mlt_producer split = NULL;
782 int in = entry->frame_in;
783 int out = entry->frame_out;
784 mlt_events_block( mlt_playlist_properties( this ), this );
785 mlt_playlist_resize_clip( this, clip, in, in + position );
786 split = mlt_producer_cut( entry->producer, in + position + 1, out );
787 mlt_playlist_insert( this, split, clip + 1, 0, -1 );
788 mlt_producer_close( split );
789 mlt_events_unblock( mlt_playlist_properties( this ), this );
790 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL );
791 }
792 else
793 {
794 error = 1;
795 }
796 }
797 return error;
798 }
799
800 /** Join 1 or more consecutive clips.
801 */
802
803 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
804 {
805 int error = clip < 0 || ( clip ) >= this->count;
806 if ( error == 0 )
807 {
808 int i = clip;
809 mlt_playlist new_clip = mlt_playlist_init( );
810 mlt_events_block( mlt_playlist_properties( this ), this );
811 if ( clip + count >= this->count )
812 count = this->count - clip;
813 for ( i = 0; i <= count; i ++ )
814 {
815 playlist_entry *entry = this->list[ clip ];
816 mlt_playlist_append( new_clip, entry->producer );
817 mlt_playlist_repeat_clip( new_clip, i, entry->repeat );
818 entry->preservation_hack = 1;
819 mlt_playlist_remove( this, clip );
820 }
821 mlt_events_unblock( mlt_playlist_properties( this ), this );
822 mlt_playlist_insert( this, mlt_playlist_producer( new_clip ), clip, 0, -1 );
823 mlt_playlist_close( new_clip );
824 }
825 return error;
826 }
827
828 /** Mix consecutive clips for a specified length and apply transition if specified.
829 */
830
831 int mlt_playlist_mix( mlt_playlist this, int clip, int length, mlt_transition transition )
832 {
833 int error = ( clip < 0 || clip + 1 >= this->count );
834 if ( error == 0 )
835 {
836 playlist_entry *clip_a = this->list[ clip ];
837 playlist_entry *clip_b = this->list[ clip + 1 ];
838 mlt_producer track_a = NULL;
839 mlt_producer track_b = NULL;
840 mlt_tractor tractor = mlt_tractor_new( );
841 mlt_events_block( mlt_playlist_properties( this ), this );
842
843 // Check length is valid for both clips and resize if necessary.
844 int max_size = clip_a->frame_count > clip_b->frame_count ? clip_a->frame_count : clip_b->frame_count;
845 length = length > max_size ? max_size : length;
846
847 // Create the a and b tracks/cuts if necessary - note that no cuts are required if the length matches
848 if ( length != clip_a->frame_count )
849 track_a = mlt_producer_cut( clip_a->producer, clip_a->frame_out - length + 1, clip_a->frame_out );
850 else
851 track_a = clip_a->producer;
852
853 if ( length != clip_b->frame_count )
854 track_b = mlt_producer_cut( clip_b->producer, clip_b->frame_in, clip_b->frame_in + length - 1 );
855 else
856 track_b = clip_b->producer;
857
858 // Set the tracks on the tractor
859 mlt_tractor_set_track( tractor, track_a, 0 );
860 mlt_tractor_set_track( tractor, track_b, 1 );
861
862 // Insert the mix object into the playlist
863 mlt_playlist_insert( this, mlt_tractor_producer( tractor ), clip + 1, -1, -1 );
864 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mlt_mix", tractor, 0, NULL, NULL );
865
866 // Attach the transition
867 if ( transition != NULL )
868 {
869 mlt_field field = mlt_tractor_field( tractor );
870 mlt_field_plant_transition( field, transition, 0, 1 );
871 mlt_transition_set_in_and_out( transition, 0, length - 1 );
872 }
873
874 // Close our references to the tracks if we created new cuts above (the tracks can still be used here)
875 if ( track_a != clip_a->producer )
876 mlt_producer_close( track_a );
877 if ( track_b != clip_b->producer )
878 mlt_producer_close( track_b );
879
880 // Check if we have anything left on the right hand clip
881 if ( track_b == clip_b->producer )
882 {
883 clip_b->preservation_hack = 1;
884 mlt_playlist_remove( this, clip + 2 );
885 }
886 else if ( clip_b->frame_out - clip_b->frame_in > length )
887 {
888 mlt_playlist_resize_clip( this, clip + 2, clip_b->frame_in + length, clip_b->frame_out );
889 mlt_properties_set_data( mlt_producer_properties( clip_b->producer ), "mix_in", tractor, 0, NULL, NULL );
890 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mix_out", clip_b->producer, 0, NULL, NULL );
891 }
892 else
893 {
894 mlt_producer_clear( clip_b->producer );
895 mlt_playlist_remove( this, clip + 2 );
896 }
897
898 // Check if we have anything left on the left hand clip
899 if ( track_a == clip_a->producer )
900 {
901 clip_a->preservation_hack = 1;
902 mlt_playlist_remove( this, clip );
903 }
904 else if ( clip_a->frame_out - clip_a->frame_in > length )
905 {
906 mlt_playlist_resize_clip( this, clip, clip_a->frame_in, clip_a->frame_out - length );
907 mlt_properties_set_data( mlt_producer_properties( clip_a->producer ), "mix_out", tractor, 0, NULL, NULL );
908 mlt_properties_set_data( mlt_tractor_properties( tractor ), "mix_in", clip_a->producer, 0, NULL, NULL );
909 }
910 else
911 {
912 mlt_producer_clear( clip_a->producer );
913 mlt_playlist_remove( this, clip );
914 }
915
916 // Unblock and force a fire off of change events to listeners
917 mlt_events_unblock( mlt_playlist_properties( this ), this );
918 mlt_playlist_virtual_refresh( this );
919 mlt_tractor_close( tractor );
920 }
921 return error;
922 }
923
924 /** Add a transition to an existing mix.
925 */
926
927 int mlt_playlist_mix_add( mlt_playlist this, int clip, mlt_transition transition )
928 {
929 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
930 mlt_properties properties = producer != NULL ? mlt_producer_properties( producer ) : NULL;
931 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
932 int error = transition == NULL || tractor == NULL;
933 if ( error == 0 )
934 {
935 mlt_field field = mlt_tractor_field( tractor );
936 mlt_field_plant_transition( field, transition, 0, 1 );
937 mlt_transition_set_in_and_out( transition, 0, this->list[ clip ]->frame_count - 1 );
938 }
939 return error;
940 }
941
942 /** Return the clip at the clip index.
943 */
944
945 mlt_producer mlt_playlist_get_clip( mlt_playlist this, int clip )
946 {
947 if ( clip >= 0 && clip < this->count )
948 return this->list[ clip ]->producer;
949 return NULL;
950 }
951
952 /** Return the clip at the specified position.
953 */
954
955 mlt_producer mlt_playlist_get_clip_at( mlt_playlist this, int position )
956 {
957 int index = 0, total = 0;
958 return mlt_playlist_locate( this, &position, &index, &total );
959 }
960
961 /** Return the clip index of the specified position.
962 */
963
964 int mlt_playlist_get_clip_index_at( mlt_playlist this, int position )
965 {
966 int index = 0, total = 0;
967 mlt_playlist_locate( this, &position, &index, &total );
968 return index;
969 }
970
971 /** Determine if the clip is a mix.
972 */
973
974 int mlt_playlist_clip_is_mix( mlt_playlist this, int clip )
975 {
976 mlt_producer producer = mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip ) );
977 mlt_properties properties = producer != NULL ? mlt_producer_properties( producer ) : NULL;
978 mlt_tractor tractor = properties != NULL ? mlt_properties_get_data( properties, "mlt_mix", NULL ) : NULL;
979 return tractor != NULL;
980 }
981
982 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
983 back correctly on to the playlist.
984 */
985
986 static int mlt_playlist_unmix( mlt_playlist this, int clip )
987 {
988 int error = ( clip < 0 || clip >= this->count );
989
990 // Ensure that the clip request is actually a mix
991 if ( error == 0 )
992 {
993 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
994 mlt_properties properties = mlt_producer_properties( producer );
995 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL ||
996 this->list[ clip ]->preservation_hack;
997 }
998
999 if ( error == 0 )
1000 {
1001 playlist_entry *mix = this->list[ clip ];
1002 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1003 mlt_properties properties = mlt_tractor_properties( tractor );
1004 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1005 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1006 int length = mlt_producer_get_playtime( mlt_tractor_producer( tractor ) );
1007 mlt_events_block( mlt_playlist_properties( this ), this );
1008
1009 if ( clip_a != NULL )
1010 {
1011 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) + length );
1012 }
1013 else
1014 {
1015 mlt_producer cut = mlt_tractor_get_track( tractor, 0 );
1016 mlt_playlist_insert( this, cut, clip, -1, -1 );
1017 clip ++;
1018 }
1019
1020 if ( clip_b != NULL )
1021 {
1022 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) - length, mlt_producer_get_out( clip_b ) );
1023 }
1024 else
1025 {
1026 mlt_producer cut = mlt_tractor_get_track( tractor, 1 );
1027 mlt_playlist_insert( this, cut, clip + 1, -1, -1 );
1028 }
1029
1030 mlt_properties_set_data( properties, "mlt_mix", NULL, 0, NULL, NULL );
1031 mlt_playlist_remove( this, clip );
1032 mlt_events_unblock( mlt_playlist_properties( this ), this );
1033 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL );
1034 }
1035 return error;
1036 }
1037
1038 static int mlt_playlist_resize_mix( mlt_playlist this, int clip, int in, int out )
1039 {
1040 int error = ( clip < 0 || clip >= this->count );
1041
1042 // Ensure that the clip request is actually a mix
1043 if ( error == 0 )
1044 {
1045 mlt_producer producer = mlt_producer_cut_parent( this->list[ clip ]->producer );
1046 mlt_properties properties = mlt_producer_properties( producer );
1047 error = mlt_properties_get_data( properties, "mlt_mix", NULL ) == NULL;
1048 }
1049
1050 if ( error == 0 )
1051 {
1052 playlist_entry *mix = this->list[ clip ];
1053 mlt_tractor tractor = ( mlt_tractor )mlt_producer_cut_parent( mix->producer );
1054 mlt_properties properties = mlt_tractor_properties( tractor );
1055 mlt_producer clip_a = mlt_properties_get_data( properties, "mix_in", NULL );
1056 mlt_producer clip_b = mlt_properties_get_data( properties, "mix_out", NULL );
1057 mlt_producer track_a = mlt_tractor_get_track( tractor, 0 );
1058 mlt_producer track_b = mlt_tractor_get_track( tractor, 1 );
1059 int length = out - in + 1;
1060 int length_diff = length - mlt_producer_get_playtime( mlt_tractor_producer( tractor ) );
1061 mlt_events_block( mlt_playlist_properties( this ), this );
1062
1063 if ( clip_a != NULL )
1064 mlt_producer_set_in_and_out( clip_a, mlt_producer_get_in( clip_a ), mlt_producer_get_out( clip_a ) - length_diff );
1065
1066 if ( clip_b != NULL )
1067 mlt_producer_set_in_and_out( clip_b, mlt_producer_get_in( clip_b ) + length_diff, mlt_producer_get_out( clip_b ) );
1068
1069 mlt_producer_set_in_and_out( track_a, mlt_producer_get_in( track_a ) - length_diff, mlt_producer_get_out( track_a ) );
1070 mlt_producer_set_in_and_out( track_b, mlt_producer_get_in( track_b ), mlt_producer_get_out( track_b ) + length_diff );
1071 mlt_producer_set_in_and_out( mlt_multitrack_producer( mlt_tractor_multitrack( tractor ) ), in, out );
1072 mlt_producer_set_in_and_out( mlt_tractor_producer( tractor ), in, out );
1073 mlt_properties_set_position( mlt_producer_properties( mix->producer ), "length", out - in + 1 );
1074 mlt_producer_set_in_and_out( mix->producer, in, out );
1075
1076 mlt_events_unblock( mlt_playlist_properties( this ), this );
1077 mlt_playlist_virtual_refresh( this );
1078 }
1079 return error;
1080 }
1081
1082 /** Get the current frame.
1083 */
1084
1085 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
1086 {
1087 // Get this mlt_playlist
1088 mlt_playlist this = producer->child;
1089
1090 // Need to ensure the frame is deinterlaced when repeating 1 frame
1091 int progressive = 0;
1092
1093 // Get the real producer
1094 mlt_service real = mlt_playlist_virtual_seek( this, &progressive );
1095
1096 // Get the frame
1097 mlt_service_get_frame( real, frame, index );
1098
1099 // Check if we're at the end of the clip
1100 mlt_properties properties = mlt_frame_properties( *frame );
1101 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
1102 mlt_playlist_virtual_set_out( this );
1103
1104 // Set the consumer progressive property
1105 if ( progressive )
1106 {
1107 mlt_properties_set_int( properties, "consumer_deinterlace", progressive );
1108 mlt_properties_set_int( properties, "test_audio", 1 );
1109 }
1110
1111 // Check for notifier and call with appropriate argument
1112 mlt_properties playlist_properties = mlt_producer_properties( producer );
1113 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
1114 if ( notifier != NULL )
1115 {
1116 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
1117 notifier( argument );
1118 }
1119
1120 // Update position on the frame we're creating
1121 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
1122
1123 // Position ourselves on the next frame
1124 mlt_producer_prepare_next( producer );
1125
1126 return 0;
1127 }
1128
1129 /** Close the playlist.
1130 */
1131
1132 void mlt_playlist_close( mlt_playlist this )
1133 {
1134 if ( this != NULL && mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
1135 {
1136 int i = 0;
1137 this->parent.close = NULL;
1138 for ( i = 0; i < this->count; i ++ )
1139 {
1140 mlt_event_close( this->list[ i ]->event );
1141 mlt_producer_close( this->list[ i ]->producer );
1142 free( this->list[ i ] );
1143 }
1144 mlt_producer_close( &this->blank );
1145 mlt_producer_close( &this->parent );
1146 free( this->list );
1147 free( this );
1148 }
1149 }