int64 based comms and more unit functionality
[melted] / 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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 /** Virtual playlist entry.
31 */
32
33 typedef struct
34 {
35 mlt_producer producer;
36 mlt_timecode in;
37 mlt_timecode playtime;
38 }
39 playlist_entry;
40
41 /** Private definition.
42 */
43
44 struct mlt_playlist_s
45 {
46 struct mlt_producer_s parent;
47 struct mlt_producer_s blank;
48
49 int size;
50 int count;
51 playlist_entry **list;
52 };
53
54 /** Forward declarations
55 */
56
57 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
58
59 /** Constructor.
60 */
61
62 mlt_playlist mlt_playlist_init( )
63 {
64 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
65 if ( this != NULL )
66 {
67 mlt_producer producer = &this->parent;
68
69 // Construct the producer
70 mlt_producer_init( producer, this );
71
72 // Override the producer get_frame
73 producer->get_frame = producer_get_frame;
74
75 // Initialise blank
76 mlt_producer_init( &this->blank, NULL );
77
78 // Indicate that this producer is a playlist
79 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
80 }
81
82 return this;
83 }
84
85 /** Get the producer associated to this playlist.
86 */
87
88 mlt_producer mlt_playlist_producer( mlt_playlist this )
89 {
90 return &this->parent;
91 }
92
93 /** Get the service associated to this playlist.
94 */
95
96 mlt_service mlt_playlist_service( mlt_playlist this )
97 {
98 return mlt_producer_service( &this->parent );
99 }
100
101 /** Get the propertues associated to this playlist.
102 */
103
104 mlt_properties mlt_playlist_properties( mlt_playlist this )
105 {
106 return mlt_producer_properties( &this->parent );
107 }
108
109 static int mlt_playlist_virtual_refresh( mlt_playlist this )
110 {
111 int i = 0;
112
113 // Get the fps of the first producer
114 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
115 mlt_timecode playtime = 0;
116
117 for ( i = 0; i < this->count; i ++ )
118 {
119 // Get the producer
120 mlt_producer producer = this->list[ i ]->producer;
121
122 // If fps is 0
123 if ( fps == 0 )
124 {
125 // Inherit it from the producer
126 fps = mlt_producer_get_fps( producer );
127 }
128 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
129 {
130 // Generate a warning for now - the following attempt to fix may fail
131 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
132
133 // It should be safe to impose fps on an image producer, but not necessarily safe for video
134 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
135 }
136
137 // Update the playtime for this clip
138 playtime += this->list[ i ]->playtime;
139 }
140
141 // Refresh all properties
142 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
143 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps == 0 ? 25 : fps );
144 mlt_properties_set_timecode( mlt_playlist_properties( this ), "length", playtime );
145 mlt_properties_set_timecode( mlt_playlist_properties( this ), "out", playtime );
146
147 return 0;
148 }
149
150 /** Append to the virtual playlist.
151 */
152
153 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_timecode in, mlt_timecode playtime )
154 {
155 // Check that we have room
156 if ( this->count >= this->size )
157 {
158 int i;
159 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
160 for ( i = this->size; i < this->size + 10; i ++ )
161 this->list[ i ] = NULL;
162 this->size += 10;
163 }
164
165 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
166 this->list[ this->count ]->producer = producer;
167 this->list[ this->count ]->in = in;
168 this->list[ this->count ]->playtime = playtime;
169
170 this->count ++;
171
172 return mlt_playlist_virtual_refresh( this );
173 }
174
175 /** Seek in the virtual playlist.
176 */
177
178 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
179 {
180 // Default producer to blank
181 mlt_producer producer = &this->blank;
182
183 // Map playlist position to real producer in virtual playlist
184 mlt_timecode position = mlt_producer_position( &this->parent );
185
186 // Loop through the virtual playlist
187 int i = 0;
188
189 for ( i = 0; i < this->count; i ++ )
190 {
191 if ( position < this->list[ i ]->playtime )
192 {
193 // Found it, now break
194 producer = this->list[ i ]->producer;
195 position += this->list[ i ]->in;
196 break;
197 }
198 else
199 {
200 // Decrement position by length of this entry
201 position -= this->list[ i ]->playtime;
202 }
203 }
204
205 // Seek in real producer to relative position
206 mlt_producer_seek( producer, position );
207
208 return producer;
209 }
210
211 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
212 {
213 // Default producer to blank
214 mlt_producer producer = &this->blank;
215
216 // Map playlist position to real producer in virtual playlist
217 mlt_timecode position = mlt_producer_position( &this->parent );
218
219 // Loop through the virtual playlist
220 int i = 0;
221
222 for ( i = 0; i < this->count; i ++ )
223 {
224 if ( position < this->list[ i ]->playtime )
225 {
226 // Found it, now break
227 producer = this->list[ i ]->producer;
228 position += this->list[ i ]->in;
229 break;
230 }
231 else
232 {
233 // Decrement position by length of this entry
234 position -= this->list[ i ]->playtime;
235 }
236 }
237
238 // Seek in real producer to relative position
239 if ( i < this->count )
240 {
241 // Update the playtime for the changed clip (hmmm)
242 this->list[ i ]->playtime = position - this->list[ i ]->in;
243
244 // Refresh the playlist
245 mlt_playlist_virtual_refresh( this );
246 }
247
248 return producer;
249 }
250
251 int mlt_playlist_current_clip( mlt_playlist this )
252 {
253 // Map playlist position to real producer in virtual playlist
254 mlt_timecode position = mlt_producer_position( &this->parent );
255
256 // Loop through the virtual playlist
257 int i = 0;
258
259 for ( i = 0; i < this->count; i ++ )
260 {
261 if ( position < this->list[ i ]->playtime )
262 {
263 // Found it, now break
264 break;
265 }
266 else
267 {
268 // Decrement position by length of this entry
269 position -= this->list[ i ]->playtime;
270 }
271 }
272
273 return i;
274 }
275
276 mlt_producer mlt_playlist_current( mlt_playlist this )
277 {
278 int i = mlt_playlist_current_clip( this );
279 if ( i < this->count )
280 return this->list[ i ]->producer;
281 else
282 return &this->blank;
283 }
284
285 /** Get the timecode which corresponds to the start of the next clip.
286 */
287
288 mlt_timecode mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
289 {
290 mlt_timecode position = 0;
291 int absolute_clip = index;
292 int i = 0;
293
294 // Determine the absolute clip
295 switch ( whence )
296 {
297 case mlt_whence_relative_start:
298 absolute_clip = index;
299 break;
300
301 case mlt_whence_relative_current:
302 absolute_clip = mlt_playlist_current_clip( this ) + index;
303 break;
304
305 case mlt_whence_relative_end:
306 absolute_clip = this->count - index;
307 break;
308 }
309
310 // Check that we're in a valid range
311 if ( absolute_clip < 0 )
312 absolute_clip = 0;
313 else if ( absolute_clip > this->count )
314 absolute_clip = this->count;
315
316 // Now determine the timecode
317 for ( i = 0; i < absolute_clip; i ++ )
318 position += this->list[ i ]->playtime;
319
320 return position;
321 }
322
323 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
324 {
325 int error = index < 0 || index >= this->count;
326 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
327 if ( !error )
328 {
329 mlt_producer producer = this->list[ index ]->producer;
330 mlt_properties properties = mlt_producer_properties( producer );
331 info->producer = producer;
332 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
333 info->resource = mlt_properties_get( properties, "resource" );
334 info->in = this->list[ index ]->in;
335 info->out = this->list[ index ]->in + this->list[ index ]->playtime;
336 info->playtime = this->list[ index ]->playtime;
337 info->length = mlt_producer_get_length( producer );
338 info->fps = mlt_producer_get_fps( producer );
339 }
340 return error;
341 }
342
343 /** Get number of clips in the playlist.
344 */
345
346 int mlt_playlist_count( mlt_playlist this )
347 {
348 return this->count;
349 }
350
351 /** Clear the playlist.
352 */
353
354 int mlt_playlist_clear( mlt_playlist this )
355 {
356 this->count = 0;
357 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
358 return mlt_playlist_virtual_refresh( this );
359 }
360
361 /** Append a producer to the playlist.
362 */
363
364 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
365 {
366 // Append to virtual list
367 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) );
368 }
369
370 /** Append a producer to the playlist with in/out points.
371 */
372
373 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, double in, double out )
374 {
375 // Append to virtual list
376 if ( in != -1 && out != -1 )
377 return mlt_playlist_virtual_append( this, producer, in, out - in );
378 else
379 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) );
380 }
381
382 /** Append a blank to the playlist of a given length.
383 */
384
385 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
386 {
387 // Append to the virtual list
388 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
389 }
390
391 /** Insert a producer into the playlist.
392 */
393
394 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_timecode in, mlt_timecode out )
395 {
396 return 0;
397 }
398
399 int mlt_playlist_remove( mlt_playlist this, int where )
400 {
401 return 0;
402 }
403
404 int mlt_playlist_move( mlt_playlist this, int from, int to )
405 {
406 return 0;
407 }
408
409 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_timecode in, mlt_timecode out )
410 {
411 int error = clip < 0 || clip >= this->count;
412 if ( error == 0 )
413 {
414 playlist_entry *entry = this->list[ clip ];
415 mlt_producer producer = entry->producer;
416
417 if ( in <= -1 )
418 in = 0;
419 if ( out <= -1 || out >= mlt_producer_get_out( producer ) )
420 out = mlt_producer_get_out( producer );
421
422 if ( out < in )
423 {
424 mlt_timecode t = in;
425 in = out;
426 out = t;
427 }
428
429 entry->in = in;
430 entry->playtime = out - in;
431 mlt_playlist_virtual_refresh( this );
432 }
433 return error;
434 }
435
436 /** Get the current frame.
437 */
438
439 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
440 {
441 // Get this mlt_playlist
442 mlt_playlist this = producer->child;
443
444 // Get the real producer
445 mlt_producer real = mlt_playlist_virtual_seek( this );
446
447 // Get the frame
448 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
449
450 // Check if we're at the end of the clip
451 mlt_properties properties = mlt_frame_properties( *frame );
452 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
453 mlt_playlist_virtual_set_out( this );
454
455 // Check for notifier and call with appropriate argument
456 mlt_properties playlist_properties = mlt_producer_properties( producer );
457 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
458 if ( notifier != NULL )
459 {
460 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
461 notifier( argument );
462 }
463
464 // Update timecode on the frame we're creating
465 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
466
467 // Position ourselves on the next frame
468 mlt_producer_prepare_next( producer );
469
470 return 0;
471 }
472
473 /** Close the playlist.
474 */
475
476 void mlt_playlist_close( mlt_playlist this )
477 {
478 mlt_producer_close( &this->parent );
479 mlt_producer_close( &this->blank );
480 free( this );
481 }