Rudimentary arithmetic property assignment
[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_producer mlt_playlist_virtual_seek( mlt_playlist this )
197 {
198 // Default producer to blank
199 mlt_producer producer = NULL;
200
201 // Map playlist position to real producer in virtual playlist
202 mlt_position position = mlt_producer_frame( &this->parent );
203
204 mlt_position original = position;
205
206 // Total number of frames
207 int64_t total = 0;
208
209 // Get the properties
210 mlt_properties properties = mlt_playlist_properties( this );
211
212 // Get the eof handling
213 char *eof = mlt_properties_get( properties, "eof" );
214
215 // Index for the main loop
216 int i = 0;
217
218 // Loop for each producer until found
219 for ( i = 0; i < this->count; i ++ )
220 {
221 // Increment the total
222 total += this->list[ i ]->frame_count;
223
224 // Check if the position indicates that we have found the clip
225 if ( position < this->list[ i ]->frame_count )
226 {
227 // Found it, now break
228 producer = this->list[ i ]->producer;
229 break;
230 }
231 else
232 {
233 // Decrement position by length of this entry
234 position -= this->list[ i ]->frame_count;
235 }
236 }
237
238 // Seek in real producer to relative position
239 if ( producer != NULL )
240 {
241 position += this->list[ i ]->frame_in;
242 mlt_producer_seek( producer, position );
243 }
244 else if ( !strcmp( eof, "pause" ) && total > 0 )
245 {
246 playlist_entry *entry = this->list[ this->count - 1 ];
247 mlt_producer this_producer = mlt_playlist_producer( this );
248 mlt_producer_seek( this_producer, original - 1 );
249 producer = entry->producer;
250 mlt_producer_seek( producer, entry->frame_out );
251 mlt_producer_set_speed( this_producer, 0 );
252 mlt_producer_set_speed( producer, 0 );
253 }
254 else if ( !strcmp( eof, "loop" ) && total > 0 )
255 {
256 playlist_entry *entry = this->list[ 0 ];
257 mlt_producer this_producer = mlt_playlist_producer( this );
258 mlt_producer_seek( this_producer, 0 );
259 producer = entry->producer;
260 mlt_producer_seek( producer, entry->frame_in );
261 }
262 else
263 {
264 producer = &this->blank;
265 }
266
267 return producer;
268 }
269
270 /** Invoked when a producer indicates that it has prematurely reached its end.
271 */
272
273 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
274 {
275 // Default producer to blank
276 mlt_producer producer = &this->blank;
277
278 // Map playlist position to real producer in virtual playlist
279 mlt_position position = mlt_producer_frame( &this->parent );
280
281 // Loop through the virtual playlist
282 int i = 0;
283
284 for ( i = 0; i < this->count; i ++ )
285 {
286 if ( position < this->list[ i ]->frame_count )
287 {
288 // Found it, now break
289 producer = this->list[ i ]->producer;
290 break;
291 }
292 else
293 {
294 // Decrement position by length of this entry
295 position -= this->list[ i ]->frame_count;
296 }
297 }
298
299 // Seek in real producer to relative position
300 if ( i < this->count )
301 {
302 // Update the frame_count for the changed clip (hmmm)
303 this->list[ i ]->frame_out = position;
304 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
305
306 // Refresh the playlist
307 mlt_playlist_virtual_refresh( this );
308 }
309
310 return producer;
311 }
312
313 /** Obtain the current clips index.
314 */
315
316 int mlt_playlist_current_clip( mlt_playlist this )
317 {
318 // Map playlist position to real producer in virtual playlist
319 mlt_position position = mlt_producer_frame( &this->parent );
320
321 // Loop through the virtual playlist
322 int i = 0;
323
324 for ( i = 0; i < this->count; i ++ )
325 {
326 if ( position < this->list[ i ]->frame_count )
327 {
328 // Found it, now break
329 break;
330 }
331 else
332 {
333 // Decrement position by length of this entry
334 position -= this->list[ i ]->frame_count;
335 }
336 }
337
338 return i;
339 }
340
341 /** Obtain the current clips producer.
342 */
343
344 mlt_producer mlt_playlist_current( mlt_playlist this )
345 {
346 int i = mlt_playlist_current_clip( this );
347 if ( i < this->count )
348 return this->list[ i ]->producer;
349 else
350 return &this->blank;
351 }
352
353 /** Get the position which corresponds to the start of the next clip.
354 */
355
356 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
357 {
358 mlt_position position = 0;
359 int absolute_clip = index;
360 int i = 0;
361
362 // Determine the absolute clip
363 switch ( whence )
364 {
365 case mlt_whence_relative_start:
366 absolute_clip = index;
367 break;
368
369 case mlt_whence_relative_current:
370 absolute_clip = mlt_playlist_current_clip( this ) + index;
371 break;
372
373 case mlt_whence_relative_end:
374 absolute_clip = this->count - index;
375 break;
376 }
377
378 // Check that we're in a valid range
379 if ( absolute_clip < 0 )
380 absolute_clip = 0;
381 else if ( absolute_clip > this->count )
382 absolute_clip = this->count;
383
384 // Now determine the position
385 for ( i = 0; i < absolute_clip; i ++ )
386 position += this->list[ i ]->frame_count;
387
388 return position;
389 }
390
391 /** Get all the info about the clip specified.
392 */
393
394 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
395 {
396 int error = index < 0 || index >= this->count;
397 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
398 if ( !error )
399 {
400 mlt_producer producer = this->list[ index ]->producer;
401 mlt_properties properties = mlt_producer_properties( producer );
402 info->clip = index;
403 info->producer = producer;
404 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
405 info->resource = mlt_properties_get( properties, "resource" );
406 info->frame_in = this->list[ index ]->frame_in;
407 info->frame_out = this->list[ index ]->frame_out;
408 info->frame_count = this->list[ index ]->frame_count;
409 info->length = mlt_producer_get_length( producer );
410 info->fps = mlt_producer_get_fps( producer );
411 }
412 return error;
413 }
414
415 /** Get number of clips in the playlist.
416 */
417
418 int mlt_playlist_count( mlt_playlist this )
419 {
420 return this->count;
421 }
422
423 /** Clear the playlist.
424 */
425
426 int mlt_playlist_clear( mlt_playlist this )
427 {
428 this->count = 0;
429 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
430 return mlt_playlist_virtual_refresh( this );
431 }
432
433 /** Append a producer to the playlist.
434 */
435
436 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
437 {
438 // Append to virtual list
439 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
440 }
441
442 /** Append a producer to the playlist with in/out points.
443 */
444
445 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
446 {
447 // Append to virtual list
448 if ( in != -1 && out != -1 )
449 return mlt_playlist_virtual_append( this, producer, in, out );
450 else
451 return mlt_playlist_append( this, producer );
452 }
453
454 /** Append a blank to the playlist of a given length.
455 */
456
457 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
458 {
459 // Append to the virtual list
460 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
461 }
462
463 /** Insert a producer into the playlist.
464 */
465
466 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
467 {
468 // Append to end
469 mlt_playlist_append_io( this, producer, in, out );
470
471 // Move to the position specified
472 return mlt_playlist_move( this, this->count - 1, where );
473 }
474
475 /** Remove an entry in the playlist.
476 */
477
478 int mlt_playlist_remove( mlt_playlist this, int where )
479 {
480 if ( this->count > 0 )
481 {
482 // We need to know the current clip and the position within the playlist
483 int current = mlt_playlist_current_clip( this );
484 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
485
486 // We need all the details about the clip we're removing
487 mlt_playlist_clip_info where_info;
488
489 // Loop variable
490 int i = 0;
491
492 // Make sure the clip to be removed is valid and correct if necessary
493 if ( where < 0 )
494 where = 0;
495 if ( where >= this->count )
496 where = this->count - 1;
497
498 // Get the clip info of the clip to be removed
499 mlt_playlist_get_clip_info( this, &where_info, where );
500
501 // Reorganise the list
502 for ( i = where + 1; i < this->count; i ++ )
503 this->list[ i - 1 ] = this->list[ i ];
504 this->count --;
505
506 // Correct position
507 if ( where == current )
508 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
509 else if ( where < current && this->count > 0 )
510 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
511 else if ( this->count == 0 )
512 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
513 }
514
515 return 0;
516 }
517
518 /** Move an entry in the playlist.
519 */
520
521 int mlt_playlist_move( mlt_playlist this, int src, int dest )
522 {
523 int i;
524
525 /* We need to ensure that the requested indexes are valid and correct it as necessary */
526 if ( src < 0 )
527 src = 0;
528 if ( src >= this->count )
529 src = this->count - 1;
530
531 if ( dest < 0 )
532 dest = 0;
533 if ( dest >= this->count )
534 dest = this->count - 1;
535
536 if ( src != dest && this->count > 1 )
537 {
538 int current = mlt_playlist_current_clip( this );
539 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
540 playlist_entry *src_entry = NULL;
541
542 // We need all the details about the current clip
543 mlt_playlist_clip_info current_info;
544
545 mlt_playlist_get_clip_info( this, &current_info, current );
546 position -= current_info.start;
547
548 if ( current == src )
549 current = dest;
550 else if ( current > src && current < dest )
551 current ++;
552 else if ( current == dest )
553 current = src;
554
555 src_entry = this->list[ src ];
556 if ( src > dest )
557 {
558 for ( i = src; i > dest; i -- )
559 this->list[ i ] = this->list[ i - 1 ];
560 }
561 else
562 {
563 for ( i = src; i < dest; i ++ )
564 this->list[ i ] = this->list[ i + 1 ];
565 }
566 this->list[ dest ] = src_entry;
567
568 mlt_playlist_get_clip_info( this, &current_info, current );
569 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
570 }
571
572 return 0;
573 }
574
575 /** Resize the current clip.
576 */
577
578 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
579 {
580 int error = clip < 0 || clip >= this->count;
581 if ( error == 0 )
582 {
583 playlist_entry *entry = this->list[ clip ];
584 mlt_producer producer = entry->producer;
585
586 if ( in <= -1 )
587 in = 0;
588 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
589 out = mlt_producer_get_playtime( producer ) - 1;
590
591 if ( out < in )
592 {
593 mlt_position t = in;
594 in = out;
595 out = t;
596 }
597
598 entry->frame_in = in;
599 entry->frame_out = out;
600 entry->frame_count = out - in + 1;
601 mlt_playlist_virtual_refresh( this );
602 }
603 return error;
604 }
605
606 /** Get the current frame.
607 */
608
609 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
610 {
611 // Get this mlt_playlist
612 mlt_playlist this = producer->child;
613
614 // Get the real producer
615 mlt_producer real = mlt_playlist_virtual_seek( this );
616
617 // Get the frame
618 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
619
620 // Check if we're at the end of the clip
621 mlt_properties properties = mlt_frame_properties( *frame );
622 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
623 mlt_playlist_virtual_set_out( this );
624
625 // Check for notifier and call with appropriate argument
626 mlt_properties playlist_properties = mlt_producer_properties( producer );
627 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
628 if ( notifier != NULL )
629 {
630 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
631 notifier( argument );
632 }
633
634 // Update position on the frame we're creating
635 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
636
637 // Position ourselves on the next frame
638 mlt_producer_prepare_next( producer );
639
640 return 0;
641 }
642
643 /** Close the playlist.
644 */
645
646 void mlt_playlist_close( mlt_playlist this )
647 {
648 int i = 0;
649 mlt_producer_close( &this->parent );
650 mlt_producer_close( &this->blank );
651 for ( i = 0; i < this->count; i ++ )
652 free( this->list[ i ] );
653 free( this->list );
654 free( this );
655 }