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"
28 /** Virtual playlist entry.
33 mlt_producer producer;
35 mlt_timecode playtime;
39 /** Private definition.
44 struct mlt_producer_s parent;
52 playlist_entry **virtual_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 this->blank = calloc( sizeof( struct mlt_producer_s ), 1 );
80 mlt_producer_init( this->blank, NULL );
86 /** Get the producer associated to this playlist.
89 mlt_producer mlt_playlist_producer( mlt_playlist this )
94 /** Get the service associated to this playlist.
97 mlt_service mlt_playlist_service( mlt_playlist this )
99 return mlt_producer_service( &this->parent );
102 /** Store a producer in the playlist.
105 static int mlt_playlist_store( mlt_playlist this, mlt_producer producer )
109 // If it's already added, return the index
110 for ( i = 0; i < this->count; i ++ )
112 if ( producer == this->list[ i ] )
116 // Check that we have room
117 if ( this->count >= this->size )
119 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( mlt_producer ) );
120 for ( i = this->size; i < this->size + 10; i ++ )
121 this->list[ i ] = NULL;
125 // Add this producer to the list
126 this->list[ this->count ] = producer;
128 return this->count ++;
131 /** Append to the virtual playlist.
134 static int mlt_playlist_virtual_append( mlt_playlist this, int position, mlt_timecode in, mlt_timecode out )
136 // Check that we have room
137 if ( this->virtual_count >= this->virtual_size )
140 this->virtual_list = realloc( this->virtual_list, ( this->virtual_size + 10 ) * sizeof( playlist_entry * ) );
141 for ( i = this->virtual_size; i < this->virtual_size + 10; i ++ )
142 this->virtual_list[ i ] = NULL;
143 this->virtual_size += 10;
146 this->virtual_list[ this->virtual_count ] = malloc( sizeof( playlist_entry ) );
147 this->virtual_list[ this->virtual_count ]->producer = this->list[ position ];
148 this->virtual_list[ this->virtual_count ]->in = in;
149 this->virtual_list[ this->virtual_count ]->playtime = out - in;
151 this->virtual_count ++;
156 /** Seek in the virtual playlist.
159 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
161 // Default producer to blank
162 mlt_producer producer = this->blank;
164 // Map playlist position to real producer in virtual playlist
165 mlt_timecode position = mlt_producer_position( &this->parent );
167 // Loop through the virtual playlist
170 for ( i = 0; i < this->virtual_count; i ++ )
172 if ( position < this->virtual_list[ i ]->playtime )
174 // Found it, now break
175 producer = this->virtual_list[ i ]->producer;
176 position += this->virtual_list[ i ]->in;
181 // Decrement position by length of this entry
182 position -= this->virtual_list[ i ]->playtime;
186 // Seek in real producer to relative position
187 mlt_producer_seek( producer, position );
192 /** Append a producer to the playlist.
195 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
197 // Get the position of the producer in the list
198 int position = mlt_playlist_store( this, producer );
200 // Append to virtual list
201 mlt_playlist_virtual_append( this, position, 0, mlt_producer_get_playtime( producer ) );
206 /** Append a blank to the playlist of a given length.
209 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
211 // Get the position of the producer in the list
212 int position = mlt_playlist_store( this, this->blank );
214 // Append to the virtual list
215 mlt_playlist_virtual_append( this, position, 0, length );
220 /** Get the current frame.
223 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
225 // Get this mlt_playlist
226 mlt_playlist this = producer->child;
228 // Get the real producer
229 mlt_producer real = mlt_playlist_virtual_seek( this );
232 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
234 // Update timecode on the frame we're creating
235 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
237 // Position ourselves on the next frame
238 mlt_producer_prepare_next( producer );
243 /** Close the playlist.
246 void mlt_playlist_close( mlt_playlist this )
248 mlt_producer_close( &this->parent );
249 mlt_producer_close( this->blank );