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