2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
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"
34 /** Virtual playlist entry.
37 typedef struct playlist_entry_s
39 mlt_producer producer
;
40 mlt_position frame_in
;
41 mlt_position frame_out
;
42 mlt_position frame_count
;
44 mlt_position producer_length
;
49 /** Private definition.
54 struct mlt_producer_s parent
;
55 struct mlt_producer_s blank
;
59 playlist_entry
**list
;
62 /** Forward declarations
65 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
66 static int mlt_playlist_unmix( mlt_playlist
this, int clip
);
67 static int mlt_playlist_resize_mix( mlt_playlist
this, int clip
, int in
, int out
);
72 mlt_playlist
mlt_playlist_init( )
74 mlt_playlist
this = calloc( sizeof( struct mlt_playlist_s
), 1 );
77 mlt_producer producer
= &this->parent
;
79 // Construct the producer
80 mlt_producer_init( producer
, this );
82 // Override the producer get_frame
83 producer
->get_frame
= producer_get_frame
;
85 // Define the destructor
86 producer
->close
= ( mlt_destructor
)mlt_playlist_close
;
87 producer
->close_object
= this;
90 mlt_producer_init( &this->blank
, NULL
);
91 mlt_properties_set( mlt_producer_properties( &this->blank
), "mlt_service", "blank" );
92 mlt_properties_set( mlt_producer_properties( &this->blank
), "resource", "blank" );
94 // Indicate that this producer is a playlist
95 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
97 // Specify the eof condition
98 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
99 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
100 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
101 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
102 mlt_properties_set_position( mlt_playlist_properties( this ), "out", 0 );
103 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
106 this->list
= malloc( this->size
* sizeof( playlist_entry
* ) );
112 /** Get the producer associated to this playlist.
115 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
117 return this != NULL ?
&this->parent
: NULL
;
120 /** Get the service associated to this playlist.
123 mlt_service
mlt_playlist_service( mlt_playlist
this )
125 return mlt_producer_service( &this->parent
);
128 /** Get the propertues associated to this playlist.
131 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
133 return mlt_producer_properties( &this->parent
);
136 /** Refresh the playlist after a clip has been changed.
139 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
141 // Obtain the properties
142 mlt_properties properties
= mlt_playlist_properties( this );
144 // Get the fps of the first producer
145 double fps
= mlt_properties_get_double( properties
, "first_fps" );
147 mlt_position frame_count
= 0;
149 for ( i
= 0; i
< this->count
; i
++ )
152 mlt_producer producer
= this->list
[ i
]->producer
;
153 int current_length
= mlt_producer_get_out( producer
) - mlt_producer_get_in( producer
) + 1;
158 // Inherit it from the producer
159 fps
= mlt_producer_get_fps( producer
);
161 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
163 // Generate a warning for now - the following attempt to fix may fail
164 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
166 // It should be safe to impose fps on an image producer, but not necessarily safe for video
167 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
170 // Check if the length of the producer has changed
171 if ( this->list
[ i
]->producer
!= &this->blank
&&
172 ( this->list
[ i
]->frame_in
!= mlt_producer_get_in( producer
) ||
173 this->list
[ i
]->frame_out
!= mlt_producer_get_out( producer
) ) )
175 // This clip should be removed...
176 if ( current_length
< 1 )
178 this->list
[ i
]->frame_in
= 0;
179 this->list
[ i
]->frame_out
= -1;
180 this->list
[ i
]->frame_count
= 0;
184 this->list
[ i
]->frame_in
= mlt_producer_get_in( producer
);
185 this->list
[ i
]->frame_out
= mlt_producer_get_out( producer
);
186 this->list
[ i
]->frame_count
= current_length
;
189 // Update the producer_length
190 this->list
[ i
]->producer_length
= current_length
;
193 // Calculate the frame_count
194 this->list
[ i
]->frame_count
= ( this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1 ) * this->list
[ i
]->repeat
;
196 // Update the frame_count for this clip
197 frame_count
+= this->list
[ i
]->frame_count
;
200 // Refresh all properties
201 mlt_properties_set_double( properties
, "first_fps", fps
);
202 mlt_properties_set_double( properties
, "fps", fps
== 0 ?
25 : fps
);
203 mlt_events_block( properties
, properties
);
204 mlt_properties_set_position( properties
, "length", frame_count
);
205 mlt_events_unblock( properties
, properties
);
206 mlt_properties_set_position( properties
, "out", frame_count
- 1 );
211 /** Listener for producers on the playlist.
214 static void mlt_playlist_listener( mlt_producer producer
, mlt_playlist
this )
216 mlt_playlist_virtual_refresh( this );
219 /** Append to the virtual playlist.
222 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer source
, mlt_position in
, mlt_position out
)
224 char *resource
= source
!= NULL ?
mlt_properties_get( mlt_producer_properties( source
), "resource" ) : NULL
;
225 mlt_producer producer
= NULL
;
226 mlt_properties properties
= NULL
;
227 mlt_properties parent
= NULL
;
229 // If we have a cut, then use the in/out points from the cut
230 if ( resource
== NULL
|| !strcmp( resource
, "blank" ) )
232 producer
= &this->blank
;
233 properties
= mlt_producer_properties( producer
);
234 mlt_properties_inc_ref( properties
);
236 else if ( mlt_producer_is_cut( source
) )
240 in
= mlt_producer_get_in( producer
);
241 if ( out
== -1 || out
> mlt_producer_get_out( producer
) )
242 out
= mlt_producer_get_out( producer
);
243 properties
= mlt_producer_properties( producer
);
244 mlt_properties_inc_ref( properties
);
248 producer
= mlt_producer_cut( source
, in
, out
);
249 if ( in
== -1 || in
< mlt_producer_get_in( producer
) )
250 in
= mlt_producer_get_in( producer
);
251 if ( out
== -1 || out
> mlt_producer_get_out( producer
) )
252 out
= mlt_producer_get_out( producer
);
253 properties
= mlt_producer_properties( producer
);
256 // Fetch the cuts parent properties
257 parent
= mlt_producer_properties( mlt_producer_cut_parent( producer
) );
259 // Check that we have room
260 if ( this->count
>= this->size
)
263 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
264 for ( i
= this->size
; i
< this->size
+ 10; i
++ ) this->list
[ i
] = NULL
;
269 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
270 if ( this->list
[ this->count
] != NULL
)
272 this->list
[ this->count
]->producer
= producer
;
273 this->list
[ this->count
]->frame_in
= in
;
274 this->list
[ this->count
]->frame_out
= out
;
275 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
276 this->list
[ this->count
]->repeat
= 1;
277 this->list
[ this->count
]->producer_length
= mlt_producer_get_out( producer
) - mlt_producer_get_in( producer
) + 1;
278 this->list
[ this->count
]->event
= mlt_events_listen( parent
, this, "producer-changed", ( mlt_listener
)mlt_playlist_listener
);
279 mlt_event_inc_ref( this->list
[ this->count
]->event
);
280 mlt_properties_set( properties
, "eof", "pause" );
281 mlt_producer_set_speed( producer
, 0 );
285 return mlt_playlist_virtual_refresh( this );
288 /** Seek in the virtual playlist.
291 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
293 // Default producer to blank
294 mlt_producer producer
= NULL
;
296 // Map playlist position to real producer in virtual playlist
297 mlt_position position
= mlt_producer_frame( &this->parent
);
299 mlt_position original
= position
;
301 // Total number of frames
304 // Get the properties
305 mlt_properties properties
= mlt_playlist_properties( this );
307 // Get the eof handling
308 char *eof
= mlt_properties_get( properties
, "eof" );
310 // Index for the main loop
313 // Loop for each producer until found
314 for ( i
= 0; i
< this->count
; i
++ )
316 // Increment the total
317 total
+= this->list
[ i
]->frame_count
;
319 // Check if the position indicates that we have found the clip
320 // Note that 0 length clips get skipped automatically
321 if ( position
< this->list
[ i
]->frame_count
)
323 // Found it, now break
324 producer
= this->list
[ i
]->producer
;
329 // Decrement position by length of this entry
330 position
-= this->list
[ i
]->frame_count
;
334 // Seek in real producer to relative position
335 if ( producer
!= NULL
)
337 int count
= this->list
[ i
]->frame_count
/ this->list
[ i
]->repeat
;
338 mlt_producer_seek( producer
, position
% count
);
340 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
342 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
343 mlt_producer this_producer
= mlt_playlist_producer( this );
344 mlt_producer_seek( this_producer
, original
- 1 );
345 producer
= entry
->producer
;
346 mlt_producer_seek( producer
, entry
->frame_out
);
347 mlt_producer_set_speed( this_producer
, 0 );
348 mlt_producer_set_speed( producer
, 0 );
350 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
352 playlist_entry
*entry
= this->list
[ 0 ];
353 mlt_producer this_producer
= mlt_playlist_producer( this );
354 mlt_producer_seek( this_producer
, 0 );
355 producer
= entry
->producer
;
356 mlt_producer_seek( producer
, 0 );
360 producer
= &this->blank
;
363 return mlt_producer_service( producer
);
366 /** Invoked when a producer indicates that it has prematurely reached its end.
369 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
371 // Default producer to blank
372 mlt_producer producer
= &this->blank
;
374 // Map playlist position to real producer in virtual playlist
375 mlt_position position
= mlt_producer_frame( &this->parent
);
377 // Loop through the virtual playlist
380 for ( i
= 0; i
< this->count
; i
++ )
382 if ( position
< this->list
[ i
]->frame_count
)
384 // Found it, now break
385 producer
= this->list
[ i
]->producer
;
390 // Decrement position by length of this entry
391 position
-= this->list
[ i
]->frame_count
;
395 // Seek in real producer to relative position
396 if ( i
< this->count
&& this->list
[ i
]->frame_out
!= position
)
398 // Update the frame_count for the changed clip (hmmm)
399 this->list
[ i
]->frame_out
= position
;
400 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
402 // Refresh the playlist
403 mlt_playlist_virtual_refresh( this );
409 /** Obtain the current clips index.
412 int mlt_playlist_current_clip( mlt_playlist
this )
414 // Map playlist position to real producer in virtual playlist
415 mlt_position position
= mlt_producer_frame( &this->parent
);
417 // Loop through the virtual playlist
420 for ( i
= 0; i
< this->count
; i
++ )
422 if ( position
< this->list
[ i
]->frame_count
)
424 // Found it, now break
429 // Decrement position by length of this entry
430 position
-= this->list
[ i
]->frame_count
;
437 /** Obtain the current clips producer.
440 mlt_producer
mlt_playlist_current( mlt_playlist
this )
442 int i
= mlt_playlist_current_clip( this );
443 if ( i
< this->count
)
444 return this->list
[ i
]->producer
;
449 /** Get the position which corresponds to the start of the next clip.
452 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
454 mlt_position position
= 0;
455 int absolute_clip
= index
;
458 // Determine the absolute clip
461 case mlt_whence_relative_start
:
462 absolute_clip
= index
;
465 case mlt_whence_relative_current
:
466 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
469 case mlt_whence_relative_end
:
470 absolute_clip
= this->count
- index
;
474 // Check that we're in a valid range
475 if ( absolute_clip
< 0 )
477 else if ( absolute_clip
> this->count
)
478 absolute_clip
= this->count
;
480 // Now determine the position
481 for ( i
= 0; i
< absolute_clip
; i
++ )
482 position
+= this->list
[ i
]->frame_count
;
487 /** Get all the info about the clip specified.
490 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
492 int error
= index
< 0 || index
>= this->count
;
493 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
496 mlt_producer producer
= mlt_producer_cut_parent( this->list
[ index
]->producer
);
497 mlt_properties properties
= mlt_producer_properties( producer
);
499 info
->producer
= producer
;
500 info
->cut
= this->list
[ index
]->producer
;
501 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
502 info
->resource
= mlt_properties_get( properties
, "resource" );
503 info
->frame_in
= this->list
[ index
]->frame_in
;
504 info
->frame_out
= this->list
[ index
]->frame_out
;
505 info
->frame_count
= this->list
[ index
]->frame_count
;
506 info
->repeat
= this->list
[ index
]->repeat
;
507 info
->length
= mlt_producer_get_length( producer
);
508 info
->fps
= mlt_producer_get_fps( producer
);
514 /** Get number of clips in the playlist.
517 int mlt_playlist_count( mlt_playlist
this )
522 /** Clear the playlist.
525 int mlt_playlist_clear( mlt_playlist
this )
528 for ( i
= 0; i
< this->count
; i
++ )
530 mlt_event_close( this->list
[ i
]->event
);
531 mlt_producer_close( this->list
[ i
]->producer
);
534 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
535 return mlt_playlist_virtual_refresh( this );
538 /** Append a producer to the playlist.
541 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
543 // Append to virtual list
544 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
547 /** Append a producer to the playlist with in/out points.
550 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
552 // Append to virtual list
553 if ( in
!= -1 && out
!= -1 )
554 return mlt_playlist_virtual_append( this, producer
, in
, out
);
556 return mlt_playlist_append( this, producer
);
559 /** Append a blank to the playlist of a given length.
562 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
564 // Append to the virtual list
565 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
568 /** Insert a producer into the playlist.
571 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
574 mlt_events_block( mlt_playlist_properties( this ), this );
575 mlt_playlist_append_io( this, producer
, in
, out
);
577 // Move to the position specified
578 mlt_playlist_move( this, this->count
- 1, where
);
579 mlt_events_unblock( mlt_playlist_properties( this ), this );
581 return mlt_playlist_virtual_refresh( this );
584 /** Remove an entry in the playlist.
587 int mlt_playlist_remove( mlt_playlist
this, int where
)
589 int error
= where
< 0 || where
>= this->count
;
590 if ( error
== 0 && mlt_playlist_unmix( this, where
) != 0 )
592 // We need to know the current clip and the position within the playlist
593 int current
= mlt_playlist_current_clip( this );
594 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
596 // We need all the details about the clip we're removing
597 mlt_playlist_clip_info where_info
;
598 playlist_entry
*entry
= this->list
[ where
];
599 mlt_properties properties
= mlt_producer_properties( entry
->producer
);
605 mlt_playlist_get_clip_info( this, &where_info
, where
);
607 // Make sure the clip to be removed is valid and correct if necessary
610 if ( where
>= this->count
)
611 where
= this->count
- 1;
613 // Reorganise the list
614 for ( i
= where
+ 1; i
< this->count
; i
++ )
615 this->list
[ i
- 1 ] = this->list
[ i
];
618 // Decouple from mix_in/out if necessary
619 if ( mlt_properties_get_data( properties
, "mix_in", NULL
) != NULL
)
621 mlt_properties mix
= mlt_properties_get_data( properties
, "mix_in", NULL
);
622 mlt_properties_set_data( mix
, "mix_out", NULL
, 0, NULL
, NULL
);
624 if ( mlt_properties_get_data( properties
, "mix_out", NULL
) != NULL
)
626 mlt_properties mix
= mlt_properties_get_data( properties
, "mix_out", NULL
);
627 mlt_properties_set_data( mix
, "mix_in", NULL
, 0, NULL
, NULL
);
630 // Close the producer associated to the clip info
631 mlt_event_close( entry
->event
);
632 mlt_producer_clear( entry
->producer
);
633 mlt_producer_close( entry
->producer
);
636 if ( where
== current
)
637 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
638 else if ( where
< current
&& this->count
> 0 )
639 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
640 else if ( this->count
== 0 )
641 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
646 // Refresh the playlist
647 mlt_playlist_virtual_refresh( this );
653 /** Move an entry in the playlist.
656 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
660 /* We need to ensure that the requested indexes are valid and correct it as necessary */
663 if ( src
>= this->count
)
664 src
= this->count
- 1;
668 if ( dest
>= this->count
)
669 dest
= this->count
- 1;
671 if ( src
!= dest
&& this->count
> 1 )
673 int current
= mlt_playlist_current_clip( this );
674 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
675 playlist_entry
*src_entry
= NULL
;
677 // We need all the details about the current clip
678 mlt_playlist_clip_info current_info
;
680 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
681 position
-= current_info
.start
;
683 if ( current
== src
)
685 else if ( current
> src
&& current
< dest
)
687 else if ( current
== dest
)
690 src_entry
= this->list
[ src
];
693 for ( i
= src
; i
> dest
; i
-- )
694 this->list
[ i
] = this->list
[ i
- 1 ];
698 for ( i
= src
; i
< dest
; i
++ )
699 this->list
[ i
] = this->list
[ i
+ 1 ];
701 this->list
[ dest
] = src_entry
;
703 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
704 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
705 mlt_playlist_virtual_refresh( this );
711 /** Repeat the specified clip n times.
714 int mlt_playlist_repeat_clip( mlt_playlist
this, int clip
, int repeat
)
716 int error
= repeat
< 1 || clip
< 0 || clip
>= this->count
;
719 playlist_entry
*entry
= this->list
[ clip
];
720 entry
->repeat
= repeat
;
721 mlt_playlist_virtual_refresh( this );
726 /** Resize the specified clip.
729 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
731 int error
= clip
< 0 || clip
>= this->count
;
732 if ( error
== 0 && mlt_playlist_resize_mix( this, clip
, in
, out
) != 0 )
734 playlist_entry
*entry
= this->list
[ clip
];
735 mlt_producer producer
= entry
->producer
;
739 if ( out
<= -1 || out
>= mlt_producer_get_length( producer
) )
740 out
= mlt_producer_get_length( producer
) - 1;
749 mlt_producer_set_in_and_out( producer
, in
, out
);
754 /** Split a clip on the playlist at the given position.
757 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
759 int error
= clip
< 0 || clip
>= this->count
;
762 playlist_entry
*entry
= this->list
[ clip
];
763 if ( position
>= 0 && position
< entry
->frame_count
)
765 mlt_producer split
= NULL
;
766 int in
= entry
->frame_in
;
767 int out
= entry
->frame_out
;
768 mlt_events_block( mlt_playlist_properties( this ), this );
769 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
770 split
= mlt_producer_cut( entry
->producer
, in
+ position
+ 1, out
);
771 mlt_playlist_insert( this, split
, clip
+ 1, 0, -1 );
772 mlt_producer_close( split
);
773 mlt_events_unblock( mlt_playlist_properties( this ), this );
774 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL
);
784 /** Join 1 or more consecutive clips.
787 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
789 int error
= clip
< 0 || ( clip
) >= this->count
;
793 mlt_playlist new_clip
= mlt_playlist_init( );
794 mlt_events_block( mlt_playlist_properties( this ), this );
795 if ( clip
+ count
>= this->count
)
796 count
= this->count
- clip
;
797 for ( i
= 0; i
<= count
; i
++ )
799 playlist_entry
*entry
= this->list
[ clip
];
800 mlt_playlist_append( new_clip
, entry
->producer
);
801 mlt_playlist_repeat_clip( new_clip
, i
, entry
->repeat
);
802 mlt_playlist_remove( this, clip
);
804 mlt_events_unblock( mlt_playlist_properties( this ), this );
805 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
806 mlt_playlist_close( new_clip
);
811 /** Mix consecutive clips for a specified length and apply transition if specified.
814 int mlt_playlist_mix( mlt_playlist
this, int clip
, int length
, mlt_transition transition
)
816 int error
= ( clip
< 0 || clip
+ 1 >= this->count
);
819 playlist_entry
*clip_a
= this->list
[ clip
];
820 playlist_entry
*clip_b
= this->list
[ clip
+ 1 ];
821 mlt_producer track_a
;
822 mlt_producer track_b
;
823 mlt_tractor tractor
= mlt_tractor_new( );
824 mlt_events_block( mlt_playlist_properties( this ), this );
826 // TODO: Check length is valid for both clips and resize if necessary.
828 // Create the a and b tracks/cuts
829 track_a
= mlt_producer_cut( clip_a
->producer
, clip_a
->frame_out
- length
+ 1, clip_a
->frame_out
);
830 track_b
= mlt_producer_cut( clip_b
->producer
, clip_b
->frame_in
, clip_b
->frame_in
+ length
- 1 );
832 // Temporary - for the benefit of westley serialisation
833 mlt_properties_set_int( mlt_producer_properties( track_a
), "cut", 1 );
834 mlt_properties_set_int( mlt_producer_properties( track_b
), "cut", 1 );
836 // Set the tracks on the tractor
837 mlt_tractor_set_track( tractor
, track_a
, 0 );
838 mlt_tractor_set_track( tractor
, track_b
, 1 );
840 // Insert the mix object into the playlist
841 mlt_playlist_insert( this, mlt_tractor_producer( tractor
), clip
+ 1, -1, -1 );
842 mlt_properties_set_data( mlt_tractor_properties( tractor
), "mlt_mix", tractor
, 0, NULL
, NULL
);
844 // Attach the transition
845 if ( transition
!= NULL
)
847 mlt_field field
= mlt_tractor_field( tractor
);
848 mlt_field_plant_transition( field
, transition
, 0, 1 );
849 mlt_transition_set_in_and_out( transition
, 0, length
- 1 );
852 // Check if we have anything left on the right hand clip
853 if ( clip_b
->frame_out
- clip_b
->frame_in
> length
)
855 mlt_playlist_resize_clip( this, clip
+ 2, clip_b
->frame_in
+ length
, clip_b
->frame_out
);
856 mlt_properties_set_data( mlt_producer_properties( clip_b
->producer
), "mix_in", tractor
, 0, NULL
, NULL
);
857 mlt_properties_set_data( mlt_tractor_properties( tractor
), "mix_out", clip_b
->producer
, 0, NULL
, NULL
);
861 mlt_producer_clear( clip_b
->producer
);
862 mlt_playlist_remove( this, clip
+ 2 );
865 // Check if we have anything left on the left hand clip
866 if ( clip_a
->frame_out
- clip_a
->frame_in
> length
)
868 mlt_playlist_resize_clip( this, clip
, clip_a
->frame_in
, clip_a
->frame_out
- length
);
869 mlt_properties_set_data( mlt_producer_properties( clip_a
->producer
), "mix_out", tractor
, 0, NULL
, NULL
);
870 mlt_properties_set_data( mlt_tractor_properties( tractor
), "mix_in", clip_a
->producer
, 0, NULL
, NULL
);
874 mlt_producer_clear( clip_a
->producer
);
875 mlt_playlist_remove( this, clip
);
878 mlt_events_unblock( mlt_playlist_properties( this ), this );
879 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL
);
880 mlt_producer_close( track_a
);
881 mlt_producer_close( track_b
);
882 mlt_tractor_close( tractor
);
887 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
888 back correctly on to the playlist.
891 static int mlt_playlist_unmix( mlt_playlist
this, int clip
)
893 int error
= ( clip
< 0 || clip
>= this->count
);
895 // Ensure that the clip request is actually a mix
898 mlt_producer producer
= mlt_producer_cut_parent( this->list
[ clip
]->producer
);
899 mlt_properties properties
= mlt_producer_properties( producer
);
900 error
= mlt_properties_get_data( properties
, "mlt_mix", NULL
) == NULL
;
905 playlist_entry
*mix
= this->list
[ clip
];
906 mlt_tractor tractor
= ( mlt_tractor
)mlt_producer_cut_parent( mix
->producer
);
907 mlt_properties properties
= mlt_tractor_properties( tractor
);
908 mlt_producer clip_a
= mlt_properties_get_data( properties
, "mix_in", NULL
);
909 mlt_producer clip_b
= mlt_properties_get_data( properties
, "mix_out", NULL
);
910 int length
= mlt_producer_get_playtime( mlt_tractor_producer( tractor
) );
911 mlt_events_block( mlt_playlist_properties( this ), this );
913 if ( clip_a
!= NULL
)
915 mlt_producer_set_in_and_out( clip_a
, mlt_producer_get_in( clip_a
), mlt_producer_get_out( clip_a
) + length
);
919 mlt_producer cut
= mlt_tractor_get_track( tractor
, 0 );
920 mlt_playlist_insert( this, cut
, clip
, -1, -1 );
924 if ( clip_b
!= NULL
)
926 mlt_producer_set_in_and_out( clip_b
, mlt_producer_get_in( clip_b
) - length
, mlt_producer_get_out( clip_b
) );
930 mlt_producer cut
= mlt_tractor_get_track( tractor
, 1 );
931 mlt_playlist_insert( this, cut
, clip
+ 1, -1, -1 );
934 mlt_properties_set_data( properties
, "mlt_mix", NULL
, 0, NULL
, NULL
);
935 mlt_playlist_remove( this, clip
);
936 mlt_events_unblock( mlt_playlist_properties( this ), this );
937 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL
);
942 static int mlt_playlist_resize_mix( mlt_playlist
this, int clip
, int in
, int out
)
944 int error
= ( clip
< 0 || clip
>= this->count
);
946 // Ensure that the clip request is actually a mix
949 mlt_producer producer
= mlt_producer_cut_parent( this->list
[ clip
]->producer
);
950 mlt_properties properties
= mlt_producer_properties( producer
);
951 error
= mlt_properties_get_data( properties
, "mlt_mix", NULL
) == NULL
;
956 playlist_entry
*mix
= this->list
[ clip
];
957 mlt_tractor tractor
= ( mlt_tractor
)mlt_producer_cut_parent( mix
->producer
);
958 mlt_properties properties
= mlt_tractor_properties( tractor
);
959 mlt_producer clip_a
= mlt_properties_get_data( properties
, "mix_in", NULL
);
960 mlt_producer clip_b
= mlt_properties_get_data( properties
, "mix_out", NULL
);
961 mlt_producer track_a
= mlt_tractor_get_track( tractor
, 0 );
962 mlt_producer track_b
= mlt_tractor_get_track( tractor
, 1 );
963 int length
= out
- in
+ 1;
964 int length_diff
= length
- mlt_producer_get_playtime( mlt_tractor_producer( tractor
) );
965 mlt_events_block( mlt_playlist_properties( this ), this );
967 if ( clip_a
!= NULL
)
968 mlt_producer_set_in_and_out( clip_a
, mlt_producer_get_in( clip_a
), mlt_producer_get_out( clip_a
) - length_diff
);
970 if ( clip_b
!= NULL
)
971 mlt_producer_set_in_and_out( clip_b
, mlt_producer_get_in( clip_b
) + length_diff
, mlt_producer_get_out( clip_b
) );
973 mlt_producer_set_in_and_out( track_a
, mlt_producer_get_in( track_a
) - length_diff
, mlt_producer_get_out( track_a
) );
974 mlt_producer_set_in_and_out( track_b
, mlt_producer_get_in( track_b
), mlt_producer_get_out( track_b
) + length_diff
);
975 mlt_producer_set_in_and_out( mlt_multitrack_producer( mlt_tractor_multitrack( tractor
) ), in
, out
);
976 mlt_producer_set_in_and_out( mlt_tractor_producer( tractor
), in
, out
);
977 mlt_properties_set_position( mlt_producer_properties( mix
->producer
), "length", out
- in
+ 1 );
978 mlt_producer_set_in_and_out( mix
->producer
, in
, out
);
980 mlt_events_unblock( mlt_playlist_properties( this ), this );
981 mlt_playlist_virtual_refresh( this );
986 /** Get the current frame.
989 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
991 // Get this mlt_playlist
992 mlt_playlist
this = producer
->child
;
994 // Get the real producer
995 mlt_service real
= mlt_playlist_virtual_seek( this );
998 mlt_service_get_frame( real
, frame
, index
);
1000 // Check if we're at the end of the clip
1001 mlt_properties properties
= mlt_frame_properties( *frame
);
1002 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
1003 mlt_playlist_virtual_set_out( this );
1005 // Check for notifier and call with appropriate argument
1006 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
1007 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
1008 if ( notifier
!= NULL
)
1010 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
1011 notifier( argument
);
1014 // Update position on the frame we're creating
1015 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
1017 // Position ourselves on the next frame
1018 mlt_producer_prepare_next( producer
);
1023 /** Close the playlist.
1026 void mlt_playlist_close( mlt_playlist
this )
1028 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
1031 this->parent
.close
= NULL
;
1032 for ( i
= 0; i
< this->count
; i
++ )
1034 mlt_event_close( this->list
[ i
]->event
);
1035 mlt_producer_close( this->list
[ i
]->producer
);
1036 free( this->list
[ i
] );
1038 mlt_producer_close( &this->blank
);
1039 mlt_producer_close( &this->parent
);