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
;
46 int preservation_hack
;
50 /** Private definition.
55 struct mlt_producer_s parent
;
56 struct mlt_producer_s blank
;
60 playlist_entry
**list
;
63 /** Forward declarations
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
);
73 mlt_playlist
mlt_playlist_init( )
75 mlt_playlist
this = calloc( sizeof( struct mlt_playlist_s
), 1 );
78 mlt_producer producer
= &this->parent
;
80 // Construct the producer
81 mlt_producer_init( producer
, this );
83 // Override the producer get_frame
84 producer
->get_frame
= producer_get_frame
;
86 // Define the destructor
87 producer
->close
= ( mlt_destructor
)mlt_playlist_close
;
88 producer
->close_object
= this;
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" );
95 // Indicate that this producer is a playlist
96 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
98 // Specify the eof condition
99 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
100 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
101 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
102 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
103 mlt_properties_set_position( mlt_playlist_properties( this ), "out", 0 );
104 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
107 this->list
= malloc( this->size
* sizeof( playlist_entry
* ) );
113 /** Get the producer associated to this playlist.
116 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
118 return this != NULL ?
&this->parent
: NULL
;
121 /** Get the service associated to this playlist.
124 mlt_service
mlt_playlist_service( mlt_playlist
this )
126 return mlt_producer_service( &this->parent
);
129 /** Get the propertues associated to this playlist.
132 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
134 return mlt_producer_properties( &this->parent
);
137 /** Refresh the playlist after a clip has been changed.
140 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
142 // Obtain the properties
143 mlt_properties properties
= mlt_playlist_properties( this );
145 // Get the fps of the first producer
146 double fps
= mlt_properties_get_double( properties
, "first_fps" );
148 mlt_position frame_count
= 0;
150 for ( i
= 0; i
< this->count
; i
++ )
153 mlt_producer producer
= this->list
[ i
]->producer
;
154 int current_length
= mlt_producer_get_out( producer
) - mlt_producer_get_in( producer
) + 1;
159 // Inherit it from the producer
160 fps
= mlt_producer_get_fps( producer
);
162 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
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
);
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
);
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
) ) )
176 // This clip should be removed...
177 if ( current_length
< 1 )
179 this->list
[ i
]->frame_in
= 0;
180 this->list
[ i
]->frame_out
= -1;
181 this->list
[ i
]->frame_count
= 0;
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
;
190 // Update the producer_length
191 this->list
[ i
]->producer_length
= current_length
;
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
;
197 // Update the frame_count for this clip
198 frame_count
+= this->list
[ i
]->frame_count
;
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 );
212 /** Listener for producers on the playlist.
215 static void mlt_playlist_listener( mlt_producer producer
, mlt_playlist
this )
217 mlt_playlist_virtual_refresh( this );
220 /** Append to the virtual playlist.
223 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer source
, mlt_position in
, mlt_position out
)
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
;
230 // If we have a cut, then use the in/out points from the cut
231 if ( resource
== NULL
|| !strcmp( resource
, "blank" ) )
233 producer
= &this->blank
;
234 properties
= mlt_producer_properties( producer
);
235 mlt_properties_inc_ref( properties
);
237 else if ( mlt_producer_is_cut( source
) )
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
);
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
);
257 // Fetch the cuts parent properties
258 parent
= mlt_producer_properties( mlt_producer_cut_parent( producer
) );
260 // Check that we have room
261 if ( this->count
>= this->size
)
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
;
270 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
271 if ( this->list
[ this->count
] != NULL
)
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 );
286 return mlt_playlist_virtual_refresh( this );
289 /** Seek in the virtual playlist.
292 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
294 // Default producer to blank
295 mlt_producer producer
= NULL
;
297 // Map playlist position to real producer in virtual playlist
298 mlt_position position
= mlt_producer_frame( &this->parent
);
300 mlt_position original
= position
;
302 // Total number of frames
305 // Get the properties
306 mlt_properties properties
= mlt_playlist_properties( this );
308 // Get the eof handling
309 char *eof
= mlt_properties_get( properties
, "eof" );
311 // Index for the main loop
314 // Loop for each producer until found
315 for ( i
= 0; i
< this->count
; i
++ )
317 // Increment the total
318 total
+= this->list
[ i
]->frame_count
;
320 // Check if the position indicates that we have found the clip
321 // Note that 0 length clips get skipped automatically
322 if ( position
< this->list
[ i
]->frame_count
)
324 // Found it, now break
325 producer
= this->list
[ i
]->producer
;
330 // Decrement position by length of this entry
331 position
-= this->list
[ i
]->frame_count
;
335 // Seek in real producer to relative position
336 if ( producer
!= NULL
)
338 int count
= this->list
[ i
]->frame_count
/ this->list
[ i
]->repeat
;
339 mlt_producer_seek( producer
, position
% count
);
341 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
343 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
344 mlt_producer this_producer
= mlt_playlist_producer( this );
345 mlt_producer_seek( this_producer
, original
- 1 );
346 producer
= entry
->producer
;
347 mlt_producer_seek( producer
, entry
->frame_out
);
348 mlt_producer_set_speed( this_producer
, 0 );
349 mlt_producer_set_speed( producer
, 0 );
351 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
353 playlist_entry
*entry
= this->list
[ 0 ];
354 mlt_producer this_producer
= mlt_playlist_producer( this );
355 mlt_producer_seek( this_producer
, 0 );
356 producer
= entry
->producer
;
357 mlt_producer_seek( producer
, 0 );
361 producer
= &this->blank
;
364 return mlt_producer_service( producer
);
367 /** Invoked when a producer indicates that it has prematurely reached its end.
370 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
372 // Default producer to blank
373 mlt_producer producer
= &this->blank
;
375 // Map playlist position to real producer in virtual playlist
376 mlt_position position
= mlt_producer_frame( &this->parent
);
378 // Loop through the virtual playlist
381 for ( i
= 0; i
< this->count
; i
++ )
383 if ( position
< this->list
[ i
]->frame_count
)
385 // Found it, now break
386 producer
= this->list
[ i
]->producer
;
391 // Decrement position by length of this entry
392 position
-= this->list
[ i
]->frame_count
;
396 // Seek in real producer to relative position
397 if ( i
< this->count
&& this->list
[ i
]->frame_out
!= position
)
399 // Update the frame_count for the changed clip (hmmm)
400 this->list
[ i
]->frame_out
= position
;
401 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
403 // Refresh the playlist
404 mlt_playlist_virtual_refresh( this );
410 /** Obtain the current clips index.
413 int mlt_playlist_current_clip( mlt_playlist
this )
415 // Map playlist position to real producer in virtual playlist
416 mlt_position position
= mlt_producer_frame( &this->parent
);
418 // Loop through the virtual playlist
421 for ( i
= 0; i
< this->count
; i
++ )
423 if ( position
< this->list
[ i
]->frame_count
)
425 // Found it, now break
430 // Decrement position by length of this entry
431 position
-= this->list
[ i
]->frame_count
;
438 /** Obtain the current clips producer.
441 mlt_producer
mlt_playlist_current( mlt_playlist
this )
443 int i
= mlt_playlist_current_clip( this );
444 if ( i
< this->count
)
445 return this->list
[ i
]->producer
;
450 /** Get the position which corresponds to the start of the next clip.
453 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
455 mlt_position position
= 0;
456 int absolute_clip
= index
;
459 // Determine the absolute clip
462 case mlt_whence_relative_start
:
463 absolute_clip
= index
;
466 case mlt_whence_relative_current
:
467 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
470 case mlt_whence_relative_end
:
471 absolute_clip
= this->count
- index
;
475 // Check that we're in a valid range
476 if ( absolute_clip
< 0 )
478 else if ( absolute_clip
> this->count
)
479 absolute_clip
= this->count
;
481 // Now determine the position
482 for ( i
= 0; i
< absolute_clip
; i
++ )
483 position
+= this->list
[ i
]->frame_count
;
488 /** Get all the info about the clip specified.
491 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
493 int error
= index
< 0 || index
>= this->count
;
494 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
497 mlt_producer producer
= mlt_producer_cut_parent( this->list
[ index
]->producer
);
498 mlt_properties properties
= mlt_producer_properties( producer
);
500 info
->producer
= producer
;
501 info
->cut
= this->list
[ index
]->producer
;
502 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
503 info
->resource
= mlt_properties_get( properties
, "resource" );
504 info
->frame_in
= this->list
[ index
]->frame_in
;
505 info
->frame_out
= this->list
[ index
]->frame_out
;
506 info
->frame_count
= this->list
[ index
]->frame_count
;
507 info
->repeat
= this->list
[ index
]->repeat
;
508 info
->length
= mlt_producer_get_length( producer
);
509 info
->fps
= mlt_producer_get_fps( producer
);
515 /** Get number of clips in the playlist.
518 int mlt_playlist_count( mlt_playlist
this )
523 /** Clear the playlist.
526 int mlt_playlist_clear( mlt_playlist
this )
529 for ( i
= 0; i
< this->count
; i
++ )
531 mlt_event_close( this->list
[ i
]->event
);
532 mlt_producer_close( this->list
[ i
]->producer
);
535 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
536 return mlt_playlist_virtual_refresh( this );
539 /** Append a producer to the playlist.
542 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
544 // Append to virtual list
545 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
548 /** Append a producer to the playlist with in/out points.
551 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
553 // Append to virtual list
554 if ( in
!= -1 && out
!= -1 )
555 return mlt_playlist_virtual_append( this, producer
, in
, out
);
557 return mlt_playlist_append( this, producer
);
560 /** Append a blank to the playlist of a given length.
563 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
565 // Append to the virtual list
566 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
569 /** Insert a producer into the playlist.
572 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
575 mlt_events_block( mlt_playlist_properties( this ), this );
576 mlt_playlist_append_io( this, producer
, in
, out
);
578 // Move to the position specified
579 mlt_playlist_move( this, this->count
- 1, where
);
580 mlt_events_unblock( mlt_playlist_properties( this ), this );
582 return mlt_playlist_virtual_refresh( this );
585 /** Remove an entry in the playlist.
588 int mlt_playlist_remove( mlt_playlist
this, int where
)
590 int error
= where
< 0 || where
>= this->count
;
591 if ( error
== 0 && mlt_playlist_unmix( this, where
) != 0 )
593 // We need to know the current clip and the position within the playlist
594 int current
= mlt_playlist_current_clip( this );
595 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
597 // We need all the details about the clip we're removing
598 mlt_playlist_clip_info where_info
;
599 playlist_entry
*entry
= this->list
[ where
];
600 mlt_properties properties
= mlt_producer_properties( entry
->producer
);
606 mlt_playlist_get_clip_info( this, &where_info
, where
);
608 // Make sure the clip to be removed is valid and correct if necessary
611 if ( where
>= this->count
)
612 where
= this->count
- 1;
614 // Reorganise the list
615 for ( i
= where
+ 1; i
< this->count
; i
++ )
616 this->list
[ i
- 1 ] = this->list
[ i
];
619 if ( entry
->preservation_hack
== 0 )
621 // Decouple from mix_in/out if necessary
622 if ( mlt_properties_get_data( properties
, "mix_in", NULL
) != NULL
)
624 mlt_properties mix
= mlt_properties_get_data( properties
, "mix_in", NULL
);
625 mlt_properties_set_data( mix
, "mix_out", NULL
, 0, NULL
, NULL
);
627 if ( mlt_properties_get_data( properties
, "mix_out", NULL
) != NULL
)
629 mlt_properties mix
= mlt_properties_get_data( properties
, "mix_out", NULL
);
630 mlt_properties_set_data( mix
, "mix_in", NULL
, 0, NULL
, NULL
);
633 mlt_producer_clear( entry
->producer
);
636 // Close the producer associated to the clip info
637 mlt_event_close( entry
->event
);
638 mlt_producer_close( entry
->producer
);
641 if ( where
== current
)
642 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
643 else if ( where
< current
&& this->count
> 0 )
644 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
645 else if ( this->count
== 0 )
646 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
651 // Refresh the playlist
652 mlt_playlist_virtual_refresh( this );
658 /** Move an entry in the playlist.
661 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
665 /* We need to ensure that the requested indexes are valid and correct it as necessary */
668 if ( src
>= this->count
)
669 src
= this->count
- 1;
673 if ( dest
>= this->count
)
674 dest
= this->count
- 1;
676 if ( src
!= dest
&& this->count
> 1 )
678 int current
= mlt_playlist_current_clip( this );
679 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
680 playlist_entry
*src_entry
= NULL
;
682 // We need all the details about the current clip
683 mlt_playlist_clip_info current_info
;
685 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
686 position
-= current_info
.start
;
688 if ( current
== src
)
690 else if ( current
> src
&& current
< dest
)
692 else if ( current
== dest
)
695 src_entry
= this->list
[ src
];
698 for ( i
= src
; i
> dest
; i
-- )
699 this->list
[ i
] = this->list
[ i
- 1 ];
703 for ( i
= src
; i
< dest
; i
++ )
704 this->list
[ i
] = this->list
[ i
+ 1 ];
706 this->list
[ dest
] = src_entry
;
708 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
709 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
710 mlt_playlist_virtual_refresh( this );
716 /** Repeat the specified clip n times.
719 int mlt_playlist_repeat_clip( mlt_playlist
this, int clip
, int repeat
)
721 int error
= repeat
< 1 || clip
< 0 || clip
>= this->count
;
724 playlist_entry
*entry
= this->list
[ clip
];
725 entry
->repeat
= repeat
;
726 mlt_playlist_virtual_refresh( this );
731 /** Resize the specified clip.
734 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
736 int error
= clip
< 0 || clip
>= this->count
;
737 if ( error
== 0 && mlt_playlist_resize_mix( this, clip
, in
, out
) != 0 )
739 playlist_entry
*entry
= this->list
[ clip
];
740 mlt_producer producer
= entry
->producer
;
744 if ( out
<= -1 || out
>= mlt_producer_get_length( producer
) )
745 out
= mlt_producer_get_length( producer
) - 1;
754 mlt_producer_set_in_and_out( producer
, in
, out
);
759 /** Split a clip on the playlist at the given position.
762 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
764 int error
= clip
< 0 || clip
>= this->count
;
767 playlist_entry
*entry
= this->list
[ clip
];
768 if ( position
>= 0 && position
< entry
->frame_count
)
770 mlt_producer split
= NULL
;
771 int in
= entry
->frame_in
;
772 int out
= entry
->frame_out
;
773 mlt_events_block( mlt_playlist_properties( this ), this );
774 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
775 split
= mlt_producer_cut( entry
->producer
, in
+ position
+ 1, out
);
776 mlt_playlist_insert( this, split
, clip
+ 1, 0, -1 );
777 mlt_producer_close( split
);
778 mlt_events_unblock( mlt_playlist_properties( this ), this );
779 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL
);
789 /** Join 1 or more consecutive clips.
792 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
794 int error
= clip
< 0 || ( clip
) >= this->count
;
798 mlt_playlist new_clip
= mlt_playlist_init( );
799 mlt_events_block( mlt_playlist_properties( this ), this );
800 if ( clip
+ count
>= this->count
)
801 count
= this->count
- clip
;
802 for ( i
= 0; i
<= count
; i
++ )
804 playlist_entry
*entry
= this->list
[ clip
];
805 mlt_playlist_append( new_clip
, entry
->producer
);
806 mlt_playlist_repeat_clip( new_clip
, i
, entry
->repeat
);
807 entry
->preservation_hack
= 1;
808 mlt_playlist_remove( this, clip
);
810 mlt_events_unblock( mlt_playlist_properties( this ), this );
811 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
812 mlt_playlist_close( new_clip
);
817 /** Mix consecutive clips for a specified length and apply transition if specified.
820 int mlt_playlist_mix( mlt_playlist
this, int clip
, int length
, mlt_transition transition
)
822 int error
= ( clip
< 0 || clip
+ 1 >= this->count
);
825 playlist_entry
*clip_a
= this->list
[ clip
];
826 playlist_entry
*clip_b
= this->list
[ clip
+ 1 ];
827 mlt_producer track_a
;
828 mlt_producer track_b
;
829 mlt_tractor tractor
= mlt_tractor_new( );
830 mlt_events_block( mlt_playlist_properties( this ), this );
832 // TODO: Check length is valid for both clips and resize if necessary.
834 // Create the a and b tracks/cuts
835 track_a
= mlt_producer_cut( clip_a
->producer
, clip_a
->frame_out
- length
+ 1, clip_a
->frame_out
);
836 track_b
= mlt_producer_cut( clip_b
->producer
, clip_b
->frame_in
, clip_b
->frame_in
+ length
- 1 );
838 // Temporary - for the benefit of westley serialisation
839 mlt_properties_set_int( mlt_producer_properties( track_a
), "cut", 1 );
840 mlt_properties_set_int( mlt_producer_properties( track_b
), "cut", 1 );
842 // Set the tracks on the tractor
843 mlt_tractor_set_track( tractor
, track_a
, 0 );
844 mlt_tractor_set_track( tractor
, track_b
, 1 );
846 // Insert the mix object into the playlist
847 mlt_playlist_insert( this, mlt_tractor_producer( tractor
), clip
+ 1, -1, -1 );
848 mlt_properties_set_data( mlt_tractor_properties( tractor
), "mlt_mix", tractor
, 0, NULL
, NULL
);
850 // Attach the transition
851 if ( transition
!= NULL
)
853 mlt_field field
= mlt_tractor_field( tractor
);
854 mlt_field_plant_transition( field
, transition
, 0, 1 );
855 mlt_transition_set_in_and_out( transition
, 0, length
- 1 );
858 // Check if we have anything left on the right hand clip
859 if ( clip_b
->frame_out
- clip_b
->frame_in
> length
)
861 mlt_playlist_resize_clip( this, clip
+ 2, clip_b
->frame_in
+ length
, clip_b
->frame_out
);
862 mlt_properties_set_data( mlt_producer_properties( clip_b
->producer
), "mix_in", tractor
, 0, NULL
, NULL
);
863 mlt_properties_set_data( mlt_tractor_properties( tractor
), "mix_out", clip_b
->producer
, 0, NULL
, NULL
);
867 mlt_producer_clear( clip_b
->producer
);
868 mlt_playlist_remove( this, clip
+ 2 );
871 // Check if we have anything left on the left hand clip
872 if ( clip_a
->frame_out
- clip_a
->frame_in
> length
)
874 mlt_playlist_resize_clip( this, clip
, clip_a
->frame_in
, clip_a
->frame_out
- length
);
875 mlt_properties_set_data( mlt_producer_properties( clip_a
->producer
), "mix_out", tractor
, 0, NULL
, NULL
);
876 mlt_properties_set_data( mlt_tractor_properties( tractor
), "mix_in", clip_a
->producer
, 0, NULL
, NULL
);
880 mlt_producer_clear( clip_a
->producer
);
881 mlt_playlist_remove( this, clip
);
884 mlt_events_unblock( mlt_playlist_properties( this ), this );
885 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL
);
886 mlt_producer_close( track_a
);
887 mlt_producer_close( track_b
);
888 mlt_tractor_close( tractor
);
893 /** Add a transition to an existing mix.
896 int mlt_playlist_mix_add( mlt_playlist
this, int clip
, mlt_transition transition
)
898 mlt_producer producer
= mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip
) );
899 mlt_properties properties
= producer
!= NULL ?
mlt_producer_properties( producer
) : NULL
;
900 mlt_tractor tractor
= properties
!= NULL ?
mlt_properties_get_data( properties
, "mlt_mix", NULL
) : NULL
;
901 int error
= transition
== NULL
|| tractor
== NULL
;
904 mlt_field field
= mlt_tractor_field( tractor
);
905 mlt_field_plant_transition( field
, transition
, 0, 1 );
906 mlt_transition_set_in_and_out( transition
, 0, this->list
[ clip
]->frame_count
- 1 );
911 /** Return the clip at the position.
914 mlt_producer
mlt_playlist_get_clip( mlt_playlist
this, int clip
)
916 if ( clip
> 0 && clip
< this->count
)
917 return this->list
[ clip
]->producer
;
921 /** Determine if the clip is a mix.
924 int mlt_playlist_clip_is_mix( mlt_playlist
this, int clip
)
926 mlt_producer producer
= mlt_producer_cut_parent( mlt_playlist_get_clip( this, clip
) );
927 mlt_properties properties
= producer
!= NULL ?
mlt_producer_properties( producer
) : NULL
;
928 mlt_tractor tractor
= properties
!= NULL ?
mlt_properties_get_data( properties
, "mlt_mix", NULL
) : NULL
;
929 return tractor
!= NULL
;
932 /** Remove a mixed clip - ensure that the cuts included in the mix find their way
933 back correctly on to the playlist.
936 static int mlt_playlist_unmix( mlt_playlist
this, int clip
)
938 int error
= ( clip
< 0 || clip
>= this->count
);
940 // Ensure that the clip request is actually a mix
943 mlt_producer producer
= mlt_producer_cut_parent( this->list
[ clip
]->producer
);
944 mlt_properties properties
= mlt_producer_properties( producer
);
945 error
= mlt_properties_get_data( properties
, "mlt_mix", NULL
) == NULL
;
950 playlist_entry
*mix
= this->list
[ clip
];
951 mlt_tractor tractor
= ( mlt_tractor
)mlt_producer_cut_parent( mix
->producer
);
952 mlt_properties properties
= mlt_tractor_properties( tractor
);
953 mlt_producer clip_a
= mlt_properties_get_data( properties
, "mix_in", NULL
);
954 mlt_producer clip_b
= mlt_properties_get_data( properties
, "mix_out", NULL
);
955 int length
= mlt_producer_get_playtime( mlt_tractor_producer( tractor
) );
956 mlt_events_block( mlt_playlist_properties( this ), this );
958 if ( clip_a
!= NULL
)
960 mlt_producer_set_in_and_out( clip_a
, mlt_producer_get_in( clip_a
), mlt_producer_get_out( clip_a
) + length
);
964 mlt_producer cut
= mlt_tractor_get_track( tractor
, 0 );
965 mlt_playlist_insert( this, cut
, clip
, -1, -1 );
969 if ( clip_b
!= NULL
)
971 mlt_producer_set_in_and_out( clip_b
, mlt_producer_get_in( clip_b
) - length
, mlt_producer_get_out( clip_b
) );
975 mlt_producer cut
= mlt_tractor_get_track( tractor
, 1 );
976 mlt_playlist_insert( this, cut
, clip
+ 1, -1, -1 );
979 mlt_properties_set_data( properties
, "mlt_mix", NULL
, 0, NULL
, NULL
);
980 mlt_playlist_remove( this, clip
);
981 mlt_events_unblock( mlt_playlist_properties( this ), this );
982 mlt_events_fire( mlt_playlist_properties( this ), "producer-changed", NULL
);
987 static int mlt_playlist_resize_mix( mlt_playlist
this, int clip
, int in
, int out
)
989 int error
= ( clip
< 0 || clip
>= this->count
);
991 // Ensure that the clip request is actually a mix
994 mlt_producer producer
= mlt_producer_cut_parent( this->list
[ clip
]->producer
);
995 mlt_properties properties
= mlt_producer_properties( producer
);
996 error
= mlt_properties_get_data( properties
, "mlt_mix", NULL
) == NULL
;
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 mlt_producer track_a
= mlt_tractor_get_track( tractor
, 0 );
1007 mlt_producer track_b
= mlt_tractor_get_track( tractor
, 1 );
1008 int length
= out
- in
+ 1;
1009 int length_diff
= length
- mlt_producer_get_playtime( mlt_tractor_producer( tractor
) );
1010 mlt_events_block( mlt_playlist_properties( this ), this );
1012 if ( clip_a
!= NULL
)
1013 mlt_producer_set_in_and_out( clip_a
, mlt_producer_get_in( clip_a
), mlt_producer_get_out( clip_a
) - length_diff
);
1015 if ( clip_b
!= NULL
)
1016 mlt_producer_set_in_and_out( clip_b
, mlt_producer_get_in( clip_b
) + length_diff
, mlt_producer_get_out( clip_b
) );
1018 mlt_producer_set_in_and_out( track_a
, mlt_producer_get_in( track_a
) - length_diff
, mlt_producer_get_out( track_a
) );
1019 mlt_producer_set_in_and_out( track_b
, mlt_producer_get_in( track_b
), mlt_producer_get_out( track_b
) + length_diff
);
1020 mlt_producer_set_in_and_out( mlt_multitrack_producer( mlt_tractor_multitrack( tractor
) ), in
, out
);
1021 mlt_producer_set_in_and_out( mlt_tractor_producer( tractor
), in
, out
);
1022 mlt_properties_set_position( mlt_producer_properties( mix
->producer
), "length", out
- in
+ 1 );
1023 mlt_producer_set_in_and_out( mix
->producer
, in
, out
);
1025 mlt_events_unblock( mlt_playlist_properties( this ), this );
1026 mlt_playlist_virtual_refresh( this );
1031 /** Get the current frame.
1034 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
1036 // Get this mlt_playlist
1037 mlt_playlist
this = producer
->child
;
1039 // Get the real producer
1040 mlt_service real
= mlt_playlist_virtual_seek( this );
1043 mlt_service_get_frame( real
, frame
, index
);
1045 // Check if we're at the end of the clip
1046 mlt_properties properties
= mlt_frame_properties( *frame
);
1047 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
1048 mlt_playlist_virtual_set_out( this );
1050 // Check for notifier and call with appropriate argument
1051 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
1052 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
1053 if ( notifier
!= NULL
)
1055 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
1056 notifier( argument
);
1059 // Update position on the frame we're creating
1060 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
1062 // Position ourselves on the next frame
1063 mlt_producer_prepare_next( producer
);
1068 /** Close the playlist.
1071 void mlt_playlist_close( mlt_playlist
this )
1073 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
1076 this->parent
.close
= NULL
;
1077 for ( i
= 0; i
< this->count
; i
++ )
1079 mlt_event_close( this->list
[ i
]->event
);
1080 mlt_producer_close( this->list
[ i
]->producer
);
1081 free( this->list
[ i
] );
1083 mlt_producer_close( &this->blank
);
1084 mlt_producer_close( &this->parent
);