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