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 );
79 // Indicate that this producer is a playlist
80 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
82 // Specify the eof condition
83 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
84 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
90 /** Get the producer associated to this playlist.
93 mlt_producer mlt_playlist_producer( mlt_playlist this )
98 /** Get the service associated to this playlist.
101 mlt_service mlt_playlist_service( mlt_playlist this )
103 return mlt_producer_service( &this->parent );
106 /** Get the propertues associated to this playlist.
109 mlt_properties mlt_playlist_properties( mlt_playlist this )
111 return mlt_producer_properties( &this->parent );
114 /** Refresh the playlist after a clip has been changed.
117 static int mlt_playlist_virtual_refresh( mlt_playlist this )
121 // Get the fps of the first producer
122 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
123 mlt_position frame_count = 0;
125 for ( i = 0; i < this->count; i ++ )
128 mlt_producer producer = this->list[ i ]->producer;
133 // Inherit it from the producer
134 fps = mlt_producer_get_fps( producer );
136 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
138 // Generate a warning for now - the following attempt to fix may fail
139 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
141 // It should be safe to impose fps on an image producer, but not necessarily safe for video
142 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
145 // Update the frame_count for this clip
146 frame_count += this->list[ i ]->frame_count;
149 // Refresh all properties
150 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
151 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps == 0 ? 25 : fps );
152 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count );
153 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count - 1 );
158 /** Append to the virtual playlist.
161 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
163 // Check that we have room
164 if ( this->count >= this->size )
167 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
168 for ( i = this->size; i < this->size + 10; i ++ )
169 this->list[ i ] = NULL;
173 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
174 this->list[ this->count ]->producer = producer;
175 this->list[ this->count ]->frame_in = in;
176 this->list[ this->count ]->frame_out = out;
177 this->list[ this->count ]->frame_count = out - in + 1;
179 mlt_properties_set( mlt_producer_properties( producer ), "eof", "continue" );
181 mlt_producer_set_speed( producer, 0 );
185 return mlt_playlist_virtual_refresh( this );
188 /** Seek in the virtual playlist.
191 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
193 // Default producer to blank
194 mlt_producer producer = NULL;
196 // Map playlist position to real producer in virtual playlist
197 mlt_position position = mlt_producer_frame( &this->parent );
199 // Total number of frames
202 // Get the properties
203 mlt_properties properties = mlt_playlist_properties( this );
205 // Get the eof handling
206 char *eof = mlt_properties_get( properties, "eof" );
208 // Index for the main loop
211 // Loop for each producer until found
212 for ( i = 0; i < this->count; i ++ )
214 // Increment the total
215 total += this->list[ i ]->frame_count;
217 // Check if the position indicates that we have found the clip
218 if ( position < this->list[ i ]->frame_count )
220 // Found it, now break
221 producer = this->list[ i ]->producer;
226 // Decrement position by length of this entry
227 position -= this->list[ i ]->frame_count;
231 // Seek in real producer to relative position
232 if ( producer != NULL )
234 position += this->list[ i ]->frame_in;
235 mlt_producer_seek( producer, position );
237 else if ( !strcmp( eof, "pause" ) && total > 0 )
239 playlist_entry *entry = this->list[ this->count - 1 ];
240 mlt_producer this_producer = mlt_playlist_producer( this );
241 mlt_producer_seek( this_producer, total - 1 - mlt_producer_get_in( this_producer ) );
242 producer = entry->producer;
243 mlt_producer_seek( producer, entry->frame_out );
244 mlt_producer_set_speed( this_producer, 0 );
246 else if ( !strcmp( eof, "loop" ) && total > 0 )
248 playlist_entry *entry = this->list[ 0 ];
249 mlt_producer this_producer = mlt_playlist_producer( this );
250 mlt_producer_seek( this_producer, 0 );
251 producer = entry->producer;
252 mlt_producer_seek( producer, entry->frame_in );
256 producer = &this->blank;
262 /** Invoked when a producer indicates that it has prematurely reached its end.
265 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
267 // Default producer to blank
268 mlt_producer producer = &this->blank;
270 // Map playlist position to real producer in virtual playlist
271 mlt_position position = mlt_producer_frame( &this->parent );
273 // Loop through the virtual playlist
276 for ( i = 0; i < this->count; i ++ )
278 if ( position < this->list[ i ]->frame_count )
280 // Found it, now break
281 producer = this->list[ i ]->producer;
286 // Decrement position by length of this entry
287 position -= this->list[ i ]->frame_count;
291 // Seek in real producer to relative position
292 if ( i < this->count )
294 // Update the frame_count for the changed clip (hmmm)
295 this->list[ i ]->frame_out = position;
296 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
298 // Refresh the playlist
299 mlt_playlist_virtual_refresh( this );
305 /** Obtain the current clips index.
308 int mlt_playlist_current_clip( mlt_playlist this )
310 // Map playlist position to real producer in virtual playlist
311 mlt_position position = mlt_producer_frame( &this->parent );
313 // Loop through the virtual playlist
316 for ( i = 0; i < this->count; i ++ )
318 if ( position < this->list[ i ]->frame_count )
320 // Found it, now break
325 // Decrement position by length of this entry
326 position -= this->list[ i ]->frame_count;
333 /** Obtain the current clips producer.
336 mlt_producer mlt_playlist_current( mlt_playlist this )
338 int i = mlt_playlist_current_clip( this );
339 if ( i < this->count )
340 return this->list[ i ]->producer;
345 /** Get the position which corresponds to the start of the next clip.
348 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
350 mlt_position position = 0;
351 int absolute_clip = index;
354 // Determine the absolute clip
357 case mlt_whence_relative_start:
358 absolute_clip = index;
361 case mlt_whence_relative_current:
362 absolute_clip = mlt_playlist_current_clip( this ) + index;
365 case mlt_whence_relative_end:
366 absolute_clip = this->count - index;
370 // Check that we're in a valid range
371 if ( absolute_clip < 0 )
373 else if ( absolute_clip > this->count )
374 absolute_clip = this->count;
376 // Now determine the position
377 for ( i = 0; i < absolute_clip; i ++ )
378 position += this->list[ i ]->frame_count;
383 /** Get all the info about the clip specified.
386 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
388 int error = index < 0 || index >= this->count;
389 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
392 mlt_producer producer = this->list[ index ]->producer;
393 mlt_properties properties = mlt_producer_properties( producer );
395 info->producer = producer;
396 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
397 info->resource = mlt_properties_get( properties, "resource" );
398 info->frame_in = this->list[ index ]->frame_in;
399 info->frame_out = this->list[ index ]->frame_out;
400 info->frame_count = this->list[ index ]->frame_count;
401 info->length = mlt_producer_get_length( producer );
402 info->fps = mlt_producer_get_fps( producer );
407 /** Get number of clips in the playlist.
410 int mlt_playlist_count( mlt_playlist this )
415 /** Clear the playlist.
418 int mlt_playlist_clear( mlt_playlist this )
421 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
422 return mlt_playlist_virtual_refresh( this );
425 /** Append a producer to the playlist.
428 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
430 // Append to virtual list
431 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
434 /** Append a producer to the playlist with in/out points.
437 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
439 // Append to virtual list
440 if ( in != -1 && out != -1 )
441 return mlt_playlist_virtual_append( this, producer, in, out );
443 return mlt_playlist_append( this, producer );
446 /** Append a blank to the playlist of a given length.
449 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
451 // Append to the virtual list
452 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
455 /** Insert a producer into the playlist.
458 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
461 mlt_playlist_append_io( this, producer, in, out );
463 // Move to the position specified
464 return mlt_playlist_move( this, this->count - 1, where );
467 /** Remove an entry in the playlist.
470 int mlt_playlist_remove( mlt_playlist this, int where )
472 if ( this->count > 0 )
474 // We need to know the current clip and the position within the playlist
475 int current = mlt_playlist_current_clip( this );
476 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
478 // We need all the details about the clip we're removing
479 mlt_playlist_clip_info where_info;
484 // Make sure the clip to be removed is valid and correct if necessary
487 if ( where >= this->count )
488 where = this->count - 1;
490 // Get the clip info of the clip to be removed
491 mlt_playlist_get_clip_info( this, &where_info, where );
493 // Reorganise the list
494 for ( i = where + 1; i < this->count; i ++ )
495 this->list[ i - 1 ] = this->list[ i ];
499 if ( where == current )
500 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
501 else if ( where < current && this->count > 0 )
502 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
503 else if ( this->count == 0 )
504 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
510 /** Move an entry in the playlist.
513 int mlt_playlist_move( mlt_playlist this, int src, int dest )
517 /* We need to ensure that the requested indexes are valid and correct it as necessary */
520 if ( src >= this->count )
521 src = this->count - 1;
525 if ( dest >= this->count )
526 dest = this->count - 1;
528 if ( src != dest && this->count > 1 )
530 int current = mlt_playlist_current_clip( this );
531 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
532 playlist_entry *src_entry = NULL;
534 // We need all the details about the current clip
535 mlt_playlist_clip_info current_info;
537 mlt_playlist_get_clip_info( this, ¤t_info, current );
538 position -= current_info.start;
540 if ( current == src )
542 else if ( current > src && current < dest )
544 else if ( current == dest )
554 src_entry = this->list[ src ];
556 for ( i = src + 1; i <= dest; i ++ )
557 this->list[ i - 1 ] = this->list[ i ];
559 this->list[ dest ] = src_entry;
561 mlt_playlist_get_clip_info( this, ¤t_info, current );
562 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
568 /** Resize the current clip.
571 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
573 int error = clip < 0 || clip >= this->count;
576 playlist_entry *entry = this->list[ clip ];
577 mlt_producer producer = entry->producer;
581 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
582 out = mlt_producer_get_playtime( producer ) - 1;
591 entry->frame_in = in;
592 entry->frame_out = out;
593 entry->frame_count = out - in + 1;
594 mlt_playlist_virtual_refresh( this );
599 /** Get the current frame.
602 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
604 // Get this mlt_playlist
605 mlt_playlist this = producer->child;
607 // Get the real producer
608 mlt_producer real = mlt_playlist_virtual_seek( this );
611 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
613 // Check if we're at the end of the clip
614 mlt_properties properties = mlt_frame_properties( *frame );
615 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
616 mlt_playlist_virtual_set_out( this );
618 // Check for notifier and call with appropriate argument
619 mlt_properties playlist_properties = mlt_producer_properties( producer );
620 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
621 if ( notifier != NULL )
623 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
624 notifier( argument );
627 // Update position on the frame we're creating
628 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
630 // Position ourselves on the next frame
631 mlt_producer_prepare_next( producer );
636 /** Close the playlist.
639 void mlt_playlist_close( mlt_playlist this )
641 mlt_producer_close( &this->parent );
642 mlt_producer_close( &this->blank );