]> git.sesse.net Git - mlt/blob - src/framework/mlt_playlist.c
f786d31d0c10e45296af3feb593fefe1bcef0708
[mlt] / src / framework / mlt_playlist.c
1 /*
2  * mlt_playlist.c -- playlist service class
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22
23 #include "mlt_playlist.h"
24 #include "mlt_frame.h"
25
26 #include <stdlib.h>
27
28 /** Virtual playlist entry.
29 */
30
31 typedef struct
32 {
33         mlt_producer producer;
34         mlt_timecode in;
35         mlt_timecode playtime;
36 }
37 playlist_entry;
38
39 /** Private definition.
40 */
41
42 struct mlt_playlist_s
43 {
44         struct mlt_producer_s parent;
45         int size;
46         int count;
47         mlt_producer *list;
48         mlt_producer blank;
49
50         int virtual_size;
51         int virtual_count;
52         playlist_entry **virtual_list;
53 };
54
55 /** Forward declarations
56 */
57
58 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
59
60 /** Constructor.
61 */
62
63 mlt_playlist mlt_playlist_init( )
64 {
65         mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
66         if ( this != NULL )
67         {
68                 mlt_producer producer = &this->parent;
69
70                 // Construct the producer
71                 mlt_producer_init( producer, this );
72
73                 // Override the producer get_frame
74                 producer->get_frame = producer_get_frame;
75
76                 // Create a producer
77                 this->blank = calloc( sizeof( struct mlt_producer_s ), 1 );
78
79                 // Initialise it
80                 mlt_producer_init( this->blank, NULL );
81         }
82         
83         return this;
84 }
85
86 /** Get the producer associated to this playlist.
87 */
88
89 mlt_producer mlt_playlist_producer( mlt_playlist this )
90 {
91         return &this->parent;
92 }
93
94 /** Get the service associated to this playlist.
95 */
96
97 mlt_service mlt_playlist_service( mlt_playlist this )
98 {
99         return mlt_producer_service( &this->parent );
100 }
101
102 /** Store a producer in the playlist.
103 */
104
105 static int mlt_playlist_store( mlt_playlist this, mlt_producer producer )
106 {
107         int i;
108
109         // If it's already added, return the index
110         for ( i = 0; i < this->count; i ++ )
111         {
112                 if ( producer == this->list[ i ] )
113                         return i;
114         }
115
116         // Check that we have room
117         if ( this->count >= this->size )
118         {
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;
122                 this->size += 10;
123         }
124
125         // Add this producer to the list
126         this->list[ this->count ] = producer;
127
128         return this->count ++;
129 }
130
131 /** Append to the virtual playlist.
132 */
133
134 static int mlt_playlist_virtual_append( mlt_playlist this, int position, mlt_timecode in, mlt_timecode out )
135 {
136         // Check that we have room
137         if ( this->virtual_count >= this->virtual_size )
138         {
139                 int i;
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;
144         }
145
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;
150
151         this->virtual_count ++;
152
153         return 0;
154 }
155
156 /** Seek in the virtual playlist.
157 */
158
159 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
160 {
161         // Default producer to blank
162         mlt_producer producer = this->blank;
163
164         // Map playlist position to real producer in virtual playlist
165         mlt_timecode position = mlt_producer_position( &this->parent );
166
167         // Loop through the virtual playlist
168         int i = 0;
169
170         for ( i = 0; i < this->virtual_count; i ++ )
171         {
172                 if ( position < this->virtual_list[ i ]->playtime )
173                 {
174                         // Found it, now break
175                         producer = this->virtual_list[ i ]->producer;
176                         position += this->virtual_list[ i ]->in;
177                         break;
178                 }
179                 else
180                 {
181                         // Decrement position by length of this entry
182                         position -= this->virtual_list[ i ]->playtime;
183                 }
184         }
185
186         // Seek in real producer to relative position
187         mlt_producer_seek( producer, position );
188
189         return producer;
190 }
191
192 /** Append a producer to the playlist.
193 */
194
195 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
196 {
197         // Get the position of the producer in the list
198         int position = mlt_playlist_store( this, producer );
199
200         // Append to virtual list
201         mlt_playlist_virtual_append( this, position, 0, mlt_producer_get_playtime( producer ) );
202
203         return 0;
204 }
205
206 /** Append a blank to the playlist of a given length.
207 */
208
209 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
210 {
211         // Get the position of the producer in the list
212         int position = mlt_playlist_store( this, this->blank );
213
214         // Append to the virtual list
215         mlt_playlist_virtual_append( this, position, 0, length );
216
217         return 0;
218 }
219
220 /** Get the current frame.
221 */
222
223 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
224 {
225         // Get this mlt_playlist
226         mlt_playlist this = producer->child;
227
228         // Get the real producer
229         mlt_producer real = mlt_playlist_virtual_seek( this );
230
231         // Get the frame
232         mlt_service_get_frame( mlt_producer_service( real ), frame, index );
233
234         // Update timecode on the frame we're creating
235         mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
236
237         // Position ourselves on the next frame
238         mlt_producer_prepare_next( producer );
239
240         return 0;
241 }
242
243 /** Close the playlist.
244 */
245
246 void mlt_playlist_close( mlt_playlist this )
247 {
248         mlt_producer_close( &this->parent );
249         mlt_producer_close( this->blank );
250         free( this->blank );
251         free( this );
252 }