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