xml based westley serialisation
[melted] / 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 <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_position frame_in;
37 mlt_position frame_out;
38 mlt_position frame_count;
39 }
40 playlist_entry;
41
42 /** Private definition.
43 */
44
45 struct mlt_playlist_s
46 {
47 struct mlt_producer_s parent;
48 struct mlt_producer_s blank;
49
50 int size;
51 int count;
52 playlist_entry **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 // Initialise blank
77 mlt_producer_init( &this->blank, NULL );
78 mlt_properties_set( mlt_producer_properties( &this->blank ), "mlt_service", "blank" );
79 mlt_properties_set( mlt_producer_properties( &this->blank ), "resource", "blank" );
80
81 // Indicate that this producer is a playlist
82 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
83
84 // Specify the eof condition
85 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
86 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
87 }
88
89 return this;
90 }
91
92 /** Get the producer associated to this playlist.
93 */
94
95 mlt_producer mlt_playlist_producer( mlt_playlist this )
96 {
97 return &this->parent;
98 }
99
100 /** Get the service associated to this playlist.
101 */
102
103 mlt_service mlt_playlist_service( mlt_playlist this )
104 {
105 return mlt_producer_service( &this->parent );
106 }
107
108 /** Get the propertues associated to this playlist.
109 */
110
111 mlt_properties mlt_playlist_properties( mlt_playlist this )
112 {
113 return mlt_producer_properties( &this->parent );
114 }
115
116 /** Refresh the playlist after a clip has been changed.
117 */
118
119 static int mlt_playlist_virtual_refresh( mlt_playlist this )
120 {
121 int i = 0;
122
123 // Get the fps of the first producer
124 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
125 mlt_position frame_count = 0;
126
127 for ( i = 0; i < this->count; i ++ )
128 {
129 // Get the producer
130 mlt_producer producer = this->list[ i ]->producer;
131
132 // If fps is 0
133 if ( fps == 0 )
134 {
135 // Inherit it from the producer
136 fps = mlt_producer_get_fps( producer );
137 }
138 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
139 {
140 // Generate a warning for now - the following attempt to fix may fail
141 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
142
143 // It should be safe to impose fps on an image producer, but not necessarily safe for video
144 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
145 }
146
147 // Update the frame_count for this clip
148 frame_count += this->list[ i ]->frame_count;
149 }
150
151 // Refresh all properties
152 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
153 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps == 0 ? 25 : fps );
154 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count );
155 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count - 1 );
156
157 return 0;
158 }
159
160 /** Append to the virtual playlist.
161 */
162
163 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
164 {
165 // Check that we have room
166 if ( this->count >= this->size )
167 {
168 int i;
169 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
170 for ( i = this->size; i < this->size + 10; i ++ )
171 this->list[ i ] = NULL;
172 this->size += 10;
173 }
174
175 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
176 this->list[ this->count ]->producer = producer;
177 this->list[ this->count ]->frame_in = in;
178 this->list[ this->count ]->frame_out = out;
179 this->list[ this->count ]->frame_count = out - in + 1;
180
181 mlt_properties_set( mlt_producer_properties( producer ), "eof", "continue" );
182
183 mlt_producer_set_speed( producer, 0 );
184
185 this->count ++;
186
187 return mlt_playlist_virtual_refresh( this );
188 }
189
190 /** Seek in the virtual playlist.
191 */
192
193 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
194 {
195 // Default producer to blank
196 mlt_producer producer = NULL;
197
198 // Map playlist position to real producer in virtual playlist
199 mlt_position position = mlt_producer_frame( &this->parent );
200
201 // Total number of frames
202 int64_t total = 0;
203
204 // Get the properties
205 mlt_properties properties = mlt_playlist_properties( this );
206
207 // Get the eof handling
208 char *eof = mlt_properties_get( properties, "eof" );
209
210 // Index for the main loop
211 int i = 0;
212
213 // Loop for each producer until found
214 for ( i = 0; i < this->count; i ++ )
215 {
216 // Increment the total
217 total += this->list[ i ]->frame_count;
218
219 // Check if the position indicates that we have found the clip
220 if ( position < this->list[ i ]->frame_count )
221 {
222 // Found it, now break
223 producer = this->list[ i ]->producer;
224 break;
225 }
226 else
227 {
228 // Decrement position by length of this entry
229 position -= this->list[ i ]->frame_count;
230 }
231 }
232
233 // Seek in real producer to relative position
234 if ( producer != NULL )
235 {
236 position += this->list[ i ]->frame_in;
237 mlt_producer_seek( producer, position );
238 }
239 else if ( !strcmp( eof, "pause" ) && total > 0 )
240 {
241 playlist_entry *entry = this->list[ this->count - 1 ];
242 mlt_producer this_producer = mlt_playlist_producer( this );
243 mlt_producer_seek( this_producer, total - 1 - mlt_producer_get_in( this_producer ) );
244 producer = entry->producer;
245 mlt_producer_seek( producer, entry->frame_out );
246 mlt_producer_set_speed( this_producer, 0 );
247 }
248 else if ( !strcmp( eof, "loop" ) && total > 0 )
249 {
250 playlist_entry *entry = this->list[ 0 ];
251 mlt_producer this_producer = mlt_playlist_producer( this );
252 mlt_producer_seek( this_producer, 0 );
253 producer = entry->producer;
254 mlt_producer_seek( producer, entry->frame_in );
255 }
256 else
257 {
258 producer = &this->blank;
259 }
260
261 return producer;
262 }
263
264 /** Invoked when a producer indicates that it has prematurely reached its end.
265 */
266
267 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
268 {
269 // Default producer to blank
270 mlt_producer producer = &this->blank;
271
272 // Map playlist position to real producer in virtual playlist
273 mlt_position position = mlt_producer_frame( &this->parent );
274
275 // Loop through the virtual playlist
276 int i = 0;
277
278 for ( i = 0; i < this->count; i ++ )
279 {
280 if ( position < this->list[ i ]->frame_count )
281 {
282 // Found it, now break
283 producer = this->list[ i ]->producer;
284 break;
285 }
286 else
287 {
288 // Decrement position by length of this entry
289 position -= this->list[ i ]->frame_count;
290 }
291 }
292
293 // Seek in real producer to relative position
294 if ( i < this->count )
295 {
296 // Update the frame_count for the changed clip (hmmm)
297 this->list[ i ]->frame_out = position;
298 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
299
300 // Refresh the playlist
301 mlt_playlist_virtual_refresh( this );
302 }
303
304 return producer;
305 }
306
307 /** Obtain the current clips index.
308 */
309
310 int mlt_playlist_current_clip( mlt_playlist this )
311 {
312 // Map playlist position to real producer in virtual playlist
313 mlt_position position = mlt_producer_frame( &this->parent );
314
315 // Loop through the virtual playlist
316 int i = 0;
317
318 for ( i = 0; i < this->count; i ++ )
319 {
320 if ( position < this->list[ i ]->frame_count )
321 {
322 // Found it, now break
323 break;
324 }
325 else
326 {
327 // Decrement position by length of this entry
328 position -= this->list[ i ]->frame_count;
329 }
330 }
331
332 return i;
333 }
334
335 /** Obtain the current clips producer.
336 */
337
338 mlt_producer mlt_playlist_current( mlt_playlist this )
339 {
340 int i = mlt_playlist_current_clip( this );
341 if ( i < this->count )
342 return this->list[ i ]->producer;
343 else
344 return &this->blank;
345 }
346
347 /** Get the position which corresponds to the start of the next clip.
348 */
349
350 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
351 {
352 mlt_position position = 0;
353 int absolute_clip = index;
354 int i = 0;
355
356 // Determine the absolute clip
357 switch ( whence )
358 {
359 case mlt_whence_relative_start:
360 absolute_clip = index;
361 break;
362
363 case mlt_whence_relative_current:
364 absolute_clip = mlt_playlist_current_clip( this ) + index;
365 break;
366
367 case mlt_whence_relative_end:
368 absolute_clip = this->count - index;
369 break;
370 }
371
372 // Check that we're in a valid range
373 if ( absolute_clip < 0 )
374 absolute_clip = 0;
375 else if ( absolute_clip > this->count )
376 absolute_clip = this->count;
377
378 // Now determine the position
379 for ( i = 0; i < absolute_clip; i ++ )
380 position += this->list[ i ]->frame_count;
381
382 return position;
383 }
384
385 /** Get all the info about the clip specified.
386 */
387
388 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
389 {
390 int error = index < 0 || index >= this->count;
391 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
392 if ( !error )
393 {
394 mlt_producer producer = this->list[ index ]->producer;
395 mlt_properties properties = mlt_producer_properties( producer );
396 info->clip = index;
397 info->producer = producer;
398 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
399 info->resource = mlt_properties_get( properties, "resource" );
400 info->frame_in = this->list[ index ]->frame_in;
401 info->frame_out = this->list[ index ]->frame_out;
402 info->frame_count = this->list[ index ]->frame_count;
403 info->length = mlt_producer_get_length( producer );
404 info->fps = mlt_producer_get_fps( producer );
405 }
406 return error;
407 }
408
409 /** Get number of clips in the playlist.
410 */
411
412 int mlt_playlist_count( mlt_playlist this )
413 {
414 return this->count;
415 }
416
417 /** Clear the playlist.
418 */
419
420 int mlt_playlist_clear( mlt_playlist this )
421 {
422 this->count = 0;
423 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
424 return mlt_playlist_virtual_refresh( this );
425 }
426
427 /** Append a producer to the playlist.
428 */
429
430 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
431 {
432 // Append to virtual list
433 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
434 }
435
436 /** Append a producer to the playlist with in/out points.
437 */
438
439 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
440 {
441 // Append to virtual list
442 if ( in != -1 && out != -1 )
443 return mlt_playlist_virtual_append( this, producer, in, out );
444 else
445 return mlt_playlist_append( this, producer );
446 }
447
448 /** Append a blank to the playlist of a given length.
449 */
450
451 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
452 {
453 // Append to the virtual list
454 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
455 }
456
457 /** Insert a producer into the playlist.
458 */
459
460 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
461 {
462 // Append to end
463 mlt_playlist_append_io( this, producer, in, out );
464
465 // Move to the position specified
466 return mlt_playlist_move( this, this->count - 1, where );
467 }
468
469 /** Remove an entry in the playlist.
470 */
471
472 int mlt_playlist_remove( mlt_playlist this, int where )
473 {
474 if ( this->count > 0 )
475 {
476 // We need to know the current clip and the position within the playlist
477 int current = mlt_playlist_current_clip( this );
478 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
479
480 // We need all the details about the clip we're removing
481 mlt_playlist_clip_info where_info;
482
483 // Loop variable
484 int i = 0;
485
486 // Make sure the clip to be removed is valid and correct if necessary
487 if ( where < 0 )
488 where = 0;
489 if ( where >= this->count )
490 where = this->count - 1;
491
492 // Get the clip info of the clip to be removed
493 mlt_playlist_get_clip_info( this, &where_info, where );
494
495 // Reorganise the list
496 for ( i = where + 1; i < this->count; i ++ )
497 this->list[ i - 1 ] = this->list[ i ];
498 this->count --;
499
500 // Correct position
501 if ( where == current )
502 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
503 else if ( where < current && this->count > 0 )
504 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
505 else if ( this->count == 0 )
506 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
507 }
508
509 return 0;
510 }
511
512 /** Move an entry in the playlist.
513 */
514
515 int mlt_playlist_move( mlt_playlist this, int src, int dest )
516 {
517 int i;
518
519 /* We need to ensure that the requested indexes are valid and correct it as necessary */
520 if ( src < 0 )
521 src = 0;
522 if ( src >= this->count )
523 src = this->count - 1;
524
525 if ( dest < 0 )
526 dest = 0;
527 if ( dest >= this->count )
528 dest = this->count - 1;
529
530 if ( src != dest && this->count > 1 )
531 {
532 int current = mlt_playlist_current_clip( this );
533 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
534 playlist_entry *src_entry = NULL;
535
536 // We need all the details about the current clip
537 mlt_playlist_clip_info current_info;
538
539 mlt_playlist_get_clip_info( this, &current_info, current );
540 position -= current_info.start;
541
542 if ( current == src )
543 current = dest;
544 else if ( current > src && current < dest )
545 current ++;
546 else if ( current == dest )
547 current = src;
548
549 if ( src > dest )
550 {
551 int t = dest;
552 dest = src;
553 src = t;
554 }
555
556 src_entry = this->list[ src ];
557
558 for ( i = src + 1; i <= dest; i ++ )
559 this->list[ i - 1 ] = this->list[ i ];
560
561 this->list[ dest ] = src_entry;
562
563 mlt_playlist_get_clip_info( this, &current_info, current );
564 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
565 }
566
567 return 0;
568 }
569
570 /** Resize the current clip.
571 */
572
573 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
574 {
575 int error = clip < 0 || clip >= this->count;
576 if ( error == 0 )
577 {
578 playlist_entry *entry = this->list[ clip ];
579 mlt_producer producer = entry->producer;
580
581 if ( in <= -1 )
582 in = 0;
583 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
584 out = mlt_producer_get_playtime( producer ) - 1;
585
586 if ( out < in )
587 {
588 mlt_position t = in;
589 in = out;
590 out = t;
591 }
592
593 entry->frame_in = in;
594 entry->frame_out = out;
595 entry->frame_count = out - in + 1;
596 mlt_playlist_virtual_refresh( this );
597 }
598 return error;
599 }
600
601 /** Get the current frame.
602 */
603
604 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
605 {
606 // Get this mlt_playlist
607 mlt_playlist this = producer->child;
608
609 // Get the real producer
610 mlt_producer real = mlt_playlist_virtual_seek( this );
611
612 // Get the frame
613 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
614
615 // Check if we're at the end of the clip
616 mlt_properties properties = mlt_frame_properties( *frame );
617 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
618 mlt_playlist_virtual_set_out( this );
619
620 // Check for notifier and call with appropriate argument
621 mlt_properties playlist_properties = mlt_producer_properties( producer );
622 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
623 if ( notifier != NULL )
624 {
625 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
626 notifier( argument );
627 }
628
629 // Update position on the frame we're creating
630 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
631
632 // Position ourselves on the next frame
633 mlt_producer_prepare_next( producer );
634
635 return 0;
636 }
637
638 /** Close the playlist.
639 */
640
641 void mlt_playlist_close( mlt_playlist this )
642 {
643 mlt_producer_close( &this->parent );
644 mlt_producer_close( &this->blank );
645 free( this );
646 }