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