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