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