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