]> git.sesse.net Git - mlt/blob - mlt/src/framework/mlt_playlist.c
partial corrections to serialisation
[mlt] / 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         int64_t 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 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
384 {
385         int error = index < 0 || index >= this->count;
386         memset( info, 0, sizeof( mlt_playlist_clip_info ) );
387         if ( !error )
388         {
389                 mlt_producer producer = this->list[ index ]->producer;
390                 mlt_properties properties = mlt_producer_properties( producer );
391                 info->producer = producer;
392                 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
393                 info->resource = mlt_properties_get( properties, "resource" );
394                 info->frame_in = this->list[ index ]->frame_in;
395                 info->frame_out = this->list[ index ]->frame_out;
396                 info->frame_count = this->list[ index ]->frame_count;
397                 info->length = mlt_producer_get_length( producer );
398                 info->fps = mlt_producer_get_fps( producer );
399         }
400         return error;
401 }
402
403 /** Get number of clips in the playlist.
404 */
405
406 int mlt_playlist_count( mlt_playlist this )
407 {
408         return this->count;
409 }
410
411 /** Clear the playlist.
412 */
413
414 int mlt_playlist_clear( mlt_playlist this )
415 {
416         this->count = 0;
417         mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
418         return mlt_playlist_virtual_refresh( this );
419 }
420
421 /** Append a producer to the playlist.
422 */
423
424 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
425 {
426         // Append to virtual list
427         return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
428 }
429
430 /** Append a producer to the playlist with in/out points.
431 */
432
433 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
434 {
435         // Append to virtual list
436         if ( in != -1 && out != -1 )
437                 return mlt_playlist_virtual_append( this, producer, in, out );
438         else
439                 return mlt_playlist_append( this, producer );
440 }
441
442 /** Append a blank to the playlist of a given length.
443 */
444
445 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
446 {
447         // Append to the virtual list
448         return mlt_playlist_virtual_append( this, &this->blank, 0, length );
449 }
450
451 /** Insert a producer into the playlist.
452 */
453
454 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
455 {
456         return 0;
457 }
458
459 int mlt_playlist_remove( mlt_playlist this, int where )
460 {
461         return 0;
462 }
463
464 int mlt_playlist_move( mlt_playlist this, int from, int to )
465 {
466         return 0;
467 }
468
469 /** Resize the current clip.
470 */
471
472 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
473 {
474         int error = clip < 0 || clip >= this->count;
475         if ( error == 0 )
476         {
477                 playlist_entry *entry = this->list[ clip ];
478                 mlt_producer producer = entry->producer;
479
480                 if ( in <= -1 )
481                         in = 0;
482                 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
483                         out = mlt_producer_get_playtime( producer ) - 1;
484
485                 if ( out < in )
486                 {
487                         mlt_position t = in;
488                         in = out;
489                         out = t;
490                 }
491
492                 entry->frame_in = in;
493                 entry->frame_out = out;
494                 entry->frame_count = out - in + 1;
495                 mlt_playlist_virtual_refresh( this );
496         }
497         return error;
498 }
499
500 /** Get the current frame.
501 */
502
503 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
504 {
505         // Get this mlt_playlist
506         mlt_playlist this = producer->child;
507
508         // Get the real producer
509         mlt_producer real = mlt_playlist_virtual_seek( this );
510
511         // Get the frame
512         mlt_service_get_frame( mlt_producer_service( real ), frame, index );
513
514         // Check if we're at the end of the clip
515         mlt_properties properties = mlt_frame_properties( *frame );
516         if ( mlt_properties_get_int( properties, "end_of_clip" ) )
517                 mlt_playlist_virtual_set_out( this );
518
519         // Check for notifier and call with appropriate argument
520         mlt_properties playlist_properties = mlt_producer_properties( producer );
521         void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
522         if ( notifier != NULL )
523         {
524                 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
525                 notifier( argument );
526         }
527
528         // Update position on the frame we're creating
529         mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
530
531         // Position ourselves on the next frame
532         mlt_producer_prepare_next( producer );
533
534         return 0;
535 }
536
537 /** Close the playlist.
538 */
539
540 void mlt_playlist_close( mlt_playlist this )
541 {
542         mlt_producer_close( &this->parent );
543         mlt_producer_close( &this->blank );
544         free( this );
545 }