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_frame.h"
30 /** Virtual playlist entry.
35 mlt_producer producer;
36 mlt_position frame_in;
37 mlt_position frame_out;
38 mlt_position frame_count;
42 /** Private definition.
47 struct mlt_producer_s parent;
48 struct mlt_producer_s blank;
52 playlist_entry **list;
55 /** Forward declarations
58 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
63 mlt_playlist mlt_playlist_init( )
65 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
68 mlt_producer producer = &this->parent;
70 // Construct the producer
71 mlt_producer_init( producer, this );
73 // Override the producer get_frame
74 producer->get_frame = producer_get_frame;
77 mlt_producer_init( &this->blank, NULL );
78 mlt_properties_set( mlt_producer_properties( &this->blank ), "mlt_service", "blank" );
79 mlt_properties_set( mlt_producer_properties( &this->blank ), "resource", "blank" );
81 // Indicate that this producer is a playlist
82 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
84 // Specify the eof condition
85 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
86 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
87 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
90 this->list = malloc( this->size * sizeof( playlist_entry * ) );
96 /** Get the producer associated to this playlist.
99 mlt_producer mlt_playlist_producer( mlt_playlist this )
101 return &this->parent;
104 /** Get the service associated to this playlist.
107 mlt_service mlt_playlist_service( mlt_playlist this )
109 return mlt_producer_service( &this->parent );
112 /** Get the propertues associated to this playlist.
115 mlt_properties mlt_playlist_properties( mlt_playlist this )
117 return mlt_producer_properties( &this->parent );
120 /** Refresh the playlist after a clip has been changed.
123 static int mlt_playlist_virtual_refresh( mlt_playlist this )
127 // Get the fps of the first producer
128 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
129 mlt_position frame_count = 0;
131 for ( i = 0; i < this->count; i ++ )
134 mlt_producer producer = this->list[ i ]->producer;
139 // Inherit it from the producer
140 fps = mlt_producer_get_fps( producer );
142 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
144 // Generate a warning for now - the following attempt to fix may fail
145 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
147 // It should be safe to impose fps on an image producer, but not necessarily safe for video
148 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
151 // Update the frame_count for this clip
152 frame_count += this->list[ i ]->frame_count;
155 // Refresh all properties
156 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
157 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps == 0 ? 25 : fps );
158 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count );
159 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count - 1 );
164 /** Append to the virtual playlist.
167 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
169 // Check that we have room
170 if ( this->count >= this->size )
173 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
174 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
178 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
179 this->list[ this->count ]->producer = producer;
180 this->list[ this->count ]->frame_in = in;
181 this->list[ this->count ]->frame_out = out;
182 this->list[ this->count ]->frame_count = out - in + 1;
184 mlt_properties_set( mlt_producer_properties( producer ), "eof", "pause" );
186 mlt_producer_set_speed( producer, 0 );
190 return mlt_playlist_virtual_refresh( this );
193 /** Seek in the virtual playlist.
196 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
198 // Default producer to blank
199 mlt_producer producer = NULL;
201 // Map playlist position to real producer in virtual playlist
202 mlt_position position = mlt_producer_frame( &this->parent );
204 mlt_position original = position;
206 // Total number of frames
209 // Get the properties
210 mlt_properties properties = mlt_playlist_properties( this );
212 // Get the eof handling
213 char *eof = mlt_properties_get( properties, "eof" );
215 // Index for the main loop
218 // Loop for each producer until found
219 for ( i = 0; i < this->count; i ++ )
221 // Increment the total
222 total += this->list[ i ]->frame_count;
224 // Check if the position indicates that we have found the clip
225 if ( position < this->list[ i ]->frame_count )
227 // Found it, now break
228 producer = this->list[ i ]->producer;
233 // Decrement position by length of this entry
234 position -= this->list[ i ]->frame_count;
238 // Seek in real producer to relative position
239 if ( producer != NULL )
241 position += this->list[ i ]->frame_in;
242 mlt_producer_seek( producer, position );
244 else if ( !strcmp( eof, "pause" ) && total > 0 )
246 playlist_entry *entry = this->list[ this->count - 1 ];
247 mlt_producer this_producer = mlt_playlist_producer( this );
248 mlt_producer_seek( this_producer, original - 1 );
249 producer = entry->producer;
250 mlt_producer_seek( producer, entry->frame_out );
251 mlt_producer_set_speed( this_producer, 0 );
252 mlt_producer_set_speed( producer, 0 );
254 else if ( !strcmp( eof, "loop" ) && total > 0 )
256 playlist_entry *entry = this->list[ 0 ];
257 mlt_producer this_producer = mlt_playlist_producer( this );
258 mlt_producer_seek( this_producer, 0 );
259 producer = entry->producer;
260 mlt_producer_seek( producer, entry->frame_in );
264 producer = &this->blank;
270 /** Invoked when a producer indicates that it has prematurely reached its end.
273 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
275 // Default producer to blank
276 mlt_producer producer = &this->blank;
278 // Map playlist position to real producer in virtual playlist
279 mlt_position position = mlt_producer_frame( &this->parent );
281 // Loop through the virtual playlist
284 for ( i = 0; i < this->count; i ++ )
286 if ( position < this->list[ i ]->frame_count )
288 // Found it, now break
289 producer = this->list[ i ]->producer;
294 // Decrement position by length of this entry
295 position -= this->list[ i ]->frame_count;
299 // Seek in real producer to relative position
300 if ( i < this->count )
302 // Update the frame_count for the changed clip (hmmm)
303 this->list[ i ]->frame_out = position;
304 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
306 // Refresh the playlist
307 mlt_playlist_virtual_refresh( this );
313 /** Obtain the current clips index.
316 int mlt_playlist_current_clip( mlt_playlist this )
318 // Map playlist position to real producer in virtual playlist
319 mlt_position position = mlt_producer_frame( &this->parent );
321 // Loop through the virtual playlist
324 for ( i = 0; i < this->count; i ++ )
326 if ( position < this->list[ i ]->frame_count )
328 // Found it, now break
333 // Decrement position by length of this entry
334 position -= this->list[ i ]->frame_count;
341 /** Obtain the current clips producer.
344 mlt_producer mlt_playlist_current( mlt_playlist this )
346 int i = mlt_playlist_current_clip( this );
347 if ( i < this->count )
348 return this->list[ i ]->producer;
353 /** Get the position which corresponds to the start of the next clip.
356 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
358 mlt_position position = 0;
359 int absolute_clip = index;
362 // Determine the absolute clip
365 case mlt_whence_relative_start:
366 absolute_clip = index;
369 case mlt_whence_relative_current:
370 absolute_clip = mlt_playlist_current_clip( this ) + index;
373 case mlt_whence_relative_end:
374 absolute_clip = this->count - index;
378 // Check that we're in a valid range
379 if ( absolute_clip < 0 )
381 else if ( absolute_clip > this->count )
382 absolute_clip = this->count;
384 // Now determine the position
385 for ( i = 0; i < absolute_clip; i ++ )
386 position += this->list[ i ]->frame_count;
391 /** Get all the info about the clip specified.
394 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
396 int error = index < 0 || index >= this->count;
397 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
400 mlt_producer producer = this->list[ index ]->producer;
401 mlt_properties properties = mlt_producer_properties( producer );
403 info->producer = producer;
404 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
405 info->resource = mlt_properties_get( properties, "resource" );
406 info->frame_in = this->list[ index ]->frame_in;
407 info->frame_out = this->list[ index ]->frame_out;
408 info->frame_count = this->list[ index ]->frame_count;
409 info->length = mlt_producer_get_length( producer );
410 info->fps = mlt_producer_get_fps( producer );
415 /** Get number of clips in the playlist.
418 int mlt_playlist_count( mlt_playlist this )
423 /** Clear the playlist.
426 int mlt_playlist_clear( mlt_playlist this )
429 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
430 return mlt_playlist_virtual_refresh( this );
433 /** Append a producer to the playlist.
436 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
438 // Append to virtual list
439 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
442 /** Append a producer to the playlist with in/out points.
445 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
447 // Append to virtual list
448 if ( in != -1 && out != -1 )
449 return mlt_playlist_virtual_append( this, producer, in, out );
451 return mlt_playlist_append( this, producer );
454 /** Append a blank to the playlist of a given length.
457 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
459 // Append to the virtual list
460 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
463 /** Insert a producer into the playlist.
466 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
469 mlt_playlist_append_io( this, producer, in, out );
471 // Move to the position specified
472 return mlt_playlist_move( this, this->count - 1, where );
475 /** Remove an entry in the playlist.
478 int mlt_playlist_remove( mlt_playlist this, int where )
480 if ( this->count > 0 )
482 // We need to know the current clip and the position within the playlist
483 int current = mlt_playlist_current_clip( this );
484 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
486 // We need all the details about the clip we're removing
487 mlt_playlist_clip_info where_info;
492 // Make sure the clip to be removed is valid and correct if necessary
495 if ( where >= this->count )
496 where = this->count - 1;
498 // Get the clip info of the clip to be removed
499 mlt_playlist_get_clip_info( this, &where_info, where );
501 // Reorganise the list
502 for ( i = where + 1; i < this->count; i ++ )
503 this->list[ i - 1 ] = this->list[ i ];
507 if ( where == current )
508 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
509 else if ( where < current && this->count > 0 )
510 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
511 else if ( this->count == 0 )
512 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
518 /** Move an entry in the playlist.
521 int mlt_playlist_move( mlt_playlist this, int src, int dest )
525 /* We need to ensure that the requested indexes are valid and correct it as necessary */
528 if ( src >= this->count )
529 src = this->count - 1;
533 if ( dest >= this->count )
534 dest = this->count - 1;
536 if ( src != dest && this->count > 1 )
538 int current = mlt_playlist_current_clip( this );
539 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
540 playlist_entry *src_entry = NULL;
542 // We need all the details about the current clip
543 mlt_playlist_clip_info current_info;
545 mlt_playlist_get_clip_info( this, ¤t_info, current );
546 position -= current_info.start;
548 if ( current == src )
550 else if ( current > src && current < dest )
552 else if ( current == dest )
555 src_entry = this->list[ src ];
558 for ( i = src; i > dest; i -- )
559 this->list[ i ] = this->list[ i - 1 ];
563 for ( i = src; i < dest; i ++ )
564 this->list[ i ] = this->list[ i + 1 ];
566 this->list[ dest ] = src_entry;
568 mlt_playlist_get_clip_info( this, ¤t_info, current );
569 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
575 /** Resize the current clip.
578 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
580 int error = clip < 0 || clip >= this->count;
583 playlist_entry *entry = this->list[ clip ];
584 mlt_producer producer = entry->producer;
588 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
589 out = mlt_producer_get_playtime( producer ) - 1;
598 entry->frame_in = in;
599 entry->frame_out = out;
600 entry->frame_count = out - in + 1;
601 mlt_playlist_virtual_refresh( this );
606 /** Get the current frame.
609 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
611 // Get this mlt_playlist
612 mlt_playlist this = producer->child;
614 // Get the real producer
615 mlt_producer real = mlt_playlist_virtual_seek( this );
618 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
620 // Check if we're at the end of the clip
621 mlt_properties properties = mlt_frame_properties( *frame );
622 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
623 mlt_playlist_virtual_set_out( this );
625 // Check for notifier and call with appropriate argument
626 mlt_properties playlist_properties = mlt_producer_properties( producer );
627 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
628 if ( notifier != NULL )
630 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
631 notifier( argument );
634 // Update position on the frame we're creating
635 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
637 // Position ourselves on the next frame
638 mlt_producer_prepare_next( producer );
643 /** Close the playlist.
646 void mlt_playlist_close( mlt_playlist this )
649 mlt_producer_close( &this->parent );
650 mlt_producer_close( &this->blank );
651 for ( i = 0; i < this->count; i ++ )
652 free( this->list[ i ] );