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