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"
29 /** Virtual playlist entry.
34 mlt_producer producer;
36 mlt_timecode playtime;
40 /** Private definition.
45 struct mlt_producer_s parent;
46 struct mlt_producer_s blank;
50 playlist_entry **list;
53 /** Forward declarations
56 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
61 mlt_playlist mlt_playlist_init( )
63 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
66 mlt_producer producer = &this->parent;
68 // Construct the producer
69 mlt_producer_init( producer, this );
71 // Override the producer get_frame
72 producer->get_frame = producer_get_frame;
75 mlt_producer_init( &this->blank, NULL );
77 // Indicate that this producer is a playlist
78 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
84 /** Get the producer associated to this playlist.
87 mlt_producer mlt_playlist_producer( mlt_playlist this )
92 /** Get the service associated to this playlist.
95 mlt_service mlt_playlist_service( mlt_playlist this )
97 return mlt_producer_service( &this->parent );
100 /** Get the propertues associated to this playlist.
103 mlt_properties mlt_playlist_properties( mlt_playlist this )
105 return mlt_producer_properties( &this->parent );
108 /** Append to the virtual playlist.
111 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_timecode in, mlt_timecode out )
113 // Get the fps of the first producer
114 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
116 mlt_timecode playtime = mlt_producer_get_playtime( mlt_playlist_producer( this ) ) + out - in;
121 // Inherit it from the producer
122 fps = mlt_producer_get_fps( producer );
124 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
126 // Generate a warning for now - the following attempt to fix may fail
127 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
129 // It should be safe to impose fps on an image producer, but not necessarily safe for video
130 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
133 // Check that we have room
134 if ( this->count >= this->size )
137 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
138 for ( i = this->size; i < this->size + 10; i ++ )
139 this->list[ i ] = NULL;
143 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
144 this->list[ this->count ]->producer = producer;
145 this->list[ this->count ]->in = in;
146 this->list[ this->count ]->playtime = out - in;
150 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
151 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps );
152 mlt_properties_set_timecode( mlt_playlist_properties( this ), "length", playtime );
153 mlt_properties_set_timecode( mlt_playlist_properties( this ), "out", playtime );
158 /** Seek in the virtual playlist.
161 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
163 // Default producer to blank
164 mlt_producer producer = &this->blank;
166 // Map playlist position to real producer in virtual playlist
167 mlt_timecode position = mlt_producer_position( &this->parent );
169 // Loop through the virtual playlist
172 for ( i = 0; i < this->count; i ++ )
174 if ( position < this->list[ i ]->playtime )
176 // Found it, now break
177 producer = this->list[ i ]->producer;
178 position += this->list[ i ]->in;
183 // Decrement position by length of this entry
184 position -= this->list[ i ]->playtime;
188 // Seek in real producer to relative position
189 mlt_producer_seek( producer, position );
194 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
196 // Default producer to blank
197 mlt_producer producer = &this->blank;
199 // Map playlist position to real producer in virtual playlist
200 mlt_timecode position = mlt_producer_position( &this->parent );
202 // Loop through the virtual playlist
205 for ( i = 0; i < this->count; i ++ )
207 if ( position < this->list[ i ]->playtime )
209 // Found it, now break
210 producer = this->list[ i ]->producer;
211 position += this->list[ i ]->in;
216 // Decrement position by length of this entry
217 position -= this->list[ i ]->playtime;
221 // Seek in real producer to relative position
222 if ( i < this->count )
224 fprintf( stderr, "END OF CLIP %d AT %e\n", i, position );
225 this->list[ i ]->playtime = position - this->list[ i ]->in;
231 static int mlt_playlist_current_clip( mlt_playlist this )
233 // Map playlist position to real producer in virtual playlist
234 mlt_timecode position = mlt_producer_position( &this->parent );
236 // Loop through the virtual playlist
239 for ( i = 0; i < this->count; i ++ )
241 if ( position < this->list[ i ]->playtime )
243 // Found it, now break
248 // Decrement position by length of this entry
249 position -= this->list[ i ]->playtime;
256 /** Get the timecode which corresponds to the start of the next clip.
259 mlt_timecode mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
261 mlt_timecode position = 0;
262 int absolute_clip = index;
265 // Determine the absolute clip
268 case mlt_whence_relative_start:
269 absolute_clip = index;
272 case mlt_whence_relative_current:
273 absolute_clip = mlt_playlist_current_clip( this ) + index;
276 case mlt_whence_relative_end:
277 absolute_clip = this->count - index;
281 // Check that we're in a valid range
282 if ( absolute_clip < 0 )
284 else if ( absolute_clip > this->count )
285 absolute_clip = this->count;
287 // Now determine the timecode
288 for ( i = 0; i < absolute_clip; i ++ )
289 position += this->list[ i ]->playtime;
294 /** Append a producer to the playlist.
297 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
299 // Append to virtual list
300 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) );
303 /** Append a blank to the playlist of a given length.
306 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
308 // Append to the virtual list
309 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
312 /** Get the current frame.
315 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
317 // Get this mlt_playlist
318 mlt_playlist this = producer->child;
320 // Get the real producer
321 mlt_producer real = mlt_playlist_virtual_seek( this );
324 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
326 // Check if we're at the end of the clip
327 mlt_properties properties = mlt_frame_properties( *frame );
328 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
329 mlt_playlist_virtual_set_out( this );
331 // Update timecode on the frame we're creating
332 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
334 // Position ourselves on the next frame
335 mlt_producer_prepare_next( producer );
340 /** Close the playlist.
343 void mlt_playlist_close( mlt_playlist this )
345 mlt_producer_close( &this->parent );
346 mlt_producer_close( &this->blank );