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