00c98cd98afde8a124328b3c0c6baaa07c551fcb
[melted] / 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 int64_t frame_in;
37 int64_t frame_out;
38 int64_t frame_count;
39 mlt_timecode playtime;
40 }
41 playlist_entry;
42
43 /** Private definition.
44 */
45
46 struct mlt_playlist_s
47 {
48 struct mlt_producer_s parent;
49 struct mlt_producer_s blank;
50
51 int size;
52 int count;
53 playlist_entry **list;
54 };
55
56 /** Forward declarations
57 */
58
59 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
60
61 /** Constructor.
62 */
63
64 mlt_playlist mlt_playlist_init( )
65 {
66 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
67 if ( this != NULL )
68 {
69 mlt_producer producer = &this->parent;
70
71 // Construct the producer
72 mlt_producer_init( producer, this );
73
74 // Override the producer get_frame
75 producer->get_frame = producer_get_frame;
76
77 // Initialise blank
78 mlt_producer_init( &this->blank, NULL );
79
80 // Indicate that this producer is a playlist
81 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
82 }
83
84 return this;
85 }
86
87 /** Get the producer associated to this playlist.
88 */
89
90 mlt_producer mlt_playlist_producer( mlt_playlist this )
91 {
92 return &this->parent;
93 }
94
95 /** Get the service associated to this playlist.
96 */
97
98 mlt_service mlt_playlist_service( mlt_playlist this )
99 {
100 return mlt_producer_service( &this->parent );
101 }
102
103 /** Get the propertues associated to this playlist.
104 */
105
106 mlt_properties mlt_playlist_properties( mlt_playlist this )
107 {
108 return mlt_producer_properties( &this->parent );
109 }
110
111 static int mlt_playlist_virtual_refresh( mlt_playlist this )
112 {
113 int i = 0;
114
115 // Get the fps of the first producer
116 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
117 mlt_timecode playtime = 0;
118
119 for ( i = 0; i < this->count; i ++ )
120 {
121 // Get the producer
122 mlt_producer producer = this->list[ i ]->producer;
123
124 // If fps is 0
125 if ( fps == 0 )
126 {
127 // Inherit it from the producer
128 fps = mlt_producer_get_fps( producer );
129 }
130 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
131 {
132 // Generate a warning for now - the following attempt to fix may fail
133 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
134
135 // It should be safe to impose fps on an image producer, but not necessarily safe for video
136 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
137 }
138
139 // Update the playtime for this clip
140 playtime += this->list[ i ]->playtime;
141 }
142
143 // Refresh all properties
144 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
145 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps == 0 ? 25 : fps );
146 mlt_properties_set_timecode( mlt_playlist_properties( this ), "length", playtime );
147 mlt_properties_set_timecode( mlt_playlist_properties( this ), "out", playtime );
148
149 return 0;
150 }
151
152 /** Append to the virtual playlist.
153 */
154
155 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, int64_t in, int64_t out )
156 {
157 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "fps" );
158 double playtime = ( double )( out - in + 1 ) / fps;
159
160 // Check that we have room
161 if ( this->count >= this->size )
162 {
163 int i;
164 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
165 for ( i = this->size; i < this->size + 10; i ++ )
166 this->list[ i ] = NULL;
167 this->size += 10;
168 }
169
170 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
171 this->list[ this->count ]->producer = producer;
172 this->list[ this->count ]->frame_in = in;
173 this->list[ this->count ]->frame_out = out;
174 this->list[ this->count ]->frame_count = out - in + 1;
175 this->list[ this->count ]->playtime = playtime;
176
177 mlt_producer_set_speed( producer, 0 );
178
179 this->count ++;
180
181 return mlt_playlist_virtual_refresh( this );
182 }
183
184 /** Seek in the virtual playlist.
185 */
186
187 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
188 {
189 // Default producer to blank
190 mlt_producer producer = NULL;
191
192 // Map playlist position to real producer in virtual playlist
193 mlt_timecode pos = mlt_producer_position( &this->parent );
194 int64_t position = mlt_producer_frame_position( &this->parent, pos );
195 int64_t total = 0;
196
197 // Loop through the virtual playlist
198 int i = 0;
199
200 for ( i = 0; i < this->count; i ++ )
201 {
202 // Increment the total
203 total += this->list[ i ]->frame_count;
204
205 if ( position < this->list[ i ]->frame_count )
206 {
207 // Found it, now break
208 producer = this->list[ i ]->producer;
209 break;
210 }
211 else
212 {
213 // Decrement position by length of this entry
214 position -= this->list[ i ]->frame_count;
215 }
216 }
217
218 // Seek in real producer to relative position
219 if ( producer != NULL )
220 {
221 position += this->list[ i ]->frame_in;
222 position += mlt_producer_frame_position( producer, mlt_producer_get_in( producer ) );
223 mlt_producer_seek_frame( producer, position );
224 }
225 else if ( total > 0 )
226 {
227 playlist_entry *entry = this->list[ this->count - 1 ];
228 mlt_producer this_producer = mlt_playlist_producer( this );
229 mlt_producer_seek_frame( this_producer, total );
230 producer = entry->producer;
231 position = mlt_producer_frame_position( producer, mlt_producer_get_in( producer ) );
232 mlt_producer_seek_frame( producer, position + entry->frame_out );
233 }
234 else
235 {
236 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
237 producer = &this->blank;
238 }
239
240 return producer;
241 }
242
243 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
244 {
245 // Default producer to blank
246 mlt_producer producer = &this->blank;
247
248 // Map playlist position to real producer in virtual playlist
249 mlt_timecode pos = mlt_producer_position( &this->parent );
250 int64_t position = mlt_producer_frame_position( &this->parent, pos );
251
252 // Loop through the virtual playlist
253 int i = 0;
254
255 for ( i = 0; i < this->count; i ++ )
256 {
257 if ( position < this->list[ i ]->frame_count )
258 {
259 // Found it, now break
260 producer = this->list[ i ]->producer;
261 break;
262 }
263 else
264 {
265 // Decrement position by length of this entry
266 position -= this->list[ i ]->frame_count;
267 }
268 }
269
270 // Seek in real producer to relative position
271 if ( i < this->count )
272 {
273 // Update the playtime for the changed clip (hmmm)
274 this->list[ i ]->frame_count = position + 1;
275 this->list[ i ]->frame_out = position - this->list[ i ]->frame_in;
276
277 // Refresh the playlist
278 mlt_playlist_virtual_refresh( this );
279 }
280
281 return producer;
282 }
283
284 int mlt_playlist_current_clip( mlt_playlist this )
285 {
286 // Map playlist position to real producer in virtual playlist
287 mlt_timecode pos = mlt_producer_position( &this->parent );
288 int64_t position = mlt_producer_frame_position( &this->parent, pos );
289
290 // Loop through the virtual playlist
291 int i = 0;
292
293 for ( i = 0; i < this->count; i ++ )
294 {
295 if ( position < this->list[ i ]->frame_count )
296 {
297 // Found it, now break
298 break;
299 }
300 else
301 {
302 // Decrement position by length of this entry
303 position -= this->list[ i ]->frame_count;
304 }
305 }
306
307 return i;
308 }
309
310 mlt_producer mlt_playlist_current( mlt_playlist this )
311 {
312 int i = mlt_playlist_current_clip( this );
313 if ( i < this->count )
314 return this->list[ i ]->producer;
315 else
316 return &this->blank;
317 }
318
319 /** Get the timecode which corresponds to the start of the next clip.
320 */
321
322 mlt_timecode mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
323 {
324 int64_t position = 0;
325 int absolute_clip = index;
326 int i = 0;
327
328 // Determine the absolute clip
329 switch ( whence )
330 {
331 case mlt_whence_relative_start:
332 absolute_clip = index;
333 break;
334
335 case mlt_whence_relative_current:
336 absolute_clip = mlt_playlist_current_clip( this ) + index;
337 break;
338
339 case mlt_whence_relative_end:
340 absolute_clip = this->count - index;
341 break;
342 }
343
344 // Check that we're in a valid range
345 if ( absolute_clip < 0 )
346 absolute_clip = 0;
347 else if ( absolute_clip > this->count )
348 absolute_clip = this->count;
349
350 // Now determine the timecode
351 for ( i = 0; i < absolute_clip; i ++ )
352 position += this->list[ i ]->frame_count;
353
354 return mlt_producer_time( &this->parent, position );
355 }
356
357 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
358 {
359 int error = index < 0 || index >= this->count;
360 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
361 if ( !error )
362 {
363 mlt_producer producer = this->list[ index ]->producer;
364 mlt_properties properties = mlt_producer_properties( producer );
365 info->producer = producer;
366 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
367 info->resource = mlt_properties_get( properties, "resource" );
368 info->frame_in = this->list[ index ]->frame_in;
369 info->in = mlt_producer_time( producer, info->frame_in );
370 info->frame_out = this->list[ index ]->frame_out;
371 info->out = mlt_producer_time( producer, info->frame_out );
372 info->playtime = this->list[ index ]->playtime;
373 info->length = mlt_producer_get_length( producer );
374 info->fps = mlt_producer_get_fps( producer );
375 }
376 return error;
377 }
378
379 /** Get number of clips in the playlist.
380 */
381
382 int mlt_playlist_count( mlt_playlist this )
383 {
384 return this->count;
385 }
386
387 /** Clear the playlist.
388 */
389
390 int mlt_playlist_clear( mlt_playlist this )
391 {
392 this->count = 0;
393 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
394 return mlt_playlist_virtual_refresh( this );
395 }
396
397 /** Append a producer to the playlist.
398 */
399
400 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
401 {
402 // Append to virtual list
403 int64_t in = mlt_producer_frame_position( producer, mlt_producer_get_in( producer ) );
404 int64_t out = mlt_producer_frame_position( producer, mlt_producer_get_out( producer ) );
405 return mlt_playlist_virtual_append( this, producer, 0, out - in );
406 }
407
408 /** Append a producer to the playlist with in/out points.
409 */
410
411 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, double in, double out )
412 {
413 // Append to virtual list
414 if ( in != -1 && out != -1 )
415 {
416 int64_t fin = mlt_producer_frame_position( producer, in );
417 int64_t fout = mlt_producer_frame_position( producer, out );
418 return mlt_playlist_virtual_append( this, producer, 0, fout - fin );
419 }
420 else
421 {
422 return mlt_playlist_append( this, producer );
423 }
424 }
425
426 /** Append a blank to the playlist of a given length.
427 */
428
429 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
430 {
431 // Append to the virtual list
432 int64_t fout = mlt_producer_frame_position( &this->blank, length );
433 return mlt_playlist_virtual_append( this, &this->blank, 0, fout );
434 }
435
436 /** Insert a producer into the playlist.
437 */
438
439 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_timecode in, mlt_timecode out )
440 {
441 return 0;
442 }
443
444 int mlt_playlist_remove( mlt_playlist this, int where )
445 {
446 return 0;
447 }
448
449 int mlt_playlist_move( mlt_playlist this, int from, int to )
450 {
451 return 0;
452 }
453
454 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_timecode in, mlt_timecode out )
455 {
456 int error = clip < 0 || clip >= this->count;
457 if ( error == 0 )
458 {
459 playlist_entry *entry = this->list[ clip ];
460 mlt_producer producer = entry->producer;
461
462 if ( in <= -1 )
463 in = 0;
464 if ( out <= -1 || out >= mlt_producer_get_out( producer ) )
465 out = mlt_producer_get_out( producer );
466
467 if ( out < in )
468 {
469 mlt_timecode t = in;
470 in = out;
471 out = t;
472 }
473
474 int64_t fin = mlt_producer_frame_position( producer, in );
475 int64_t fout = mlt_producer_frame_position( producer, out );
476
477 entry->frame_in = fin;
478 entry->frame_out = fout;
479 entry->frame_count = fout - fin + 1;
480 entry->playtime = out - in;
481 mlt_playlist_virtual_refresh( this );
482 }
483 return error;
484 }
485
486 /** Get the current frame.
487 */
488
489 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
490 {
491 // Get this mlt_playlist
492 mlt_playlist this = producer->child;
493
494 // Get the real producer
495 mlt_producer real = mlt_playlist_virtual_seek( this );
496
497 // Get the frame
498 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
499
500 // Check if we're at the end of the clip
501 mlt_properties properties = mlt_frame_properties( *frame );
502 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
503 mlt_playlist_virtual_set_out( this );
504
505 // Check for notifier and call with appropriate argument
506 mlt_properties playlist_properties = mlt_producer_properties( producer );
507 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
508 if ( notifier != NULL )
509 {
510 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
511 notifier( argument );
512 }
513
514 // Update timecode on the frame we're creating
515 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
516
517 // Position ourselves on the next frame
518 mlt_producer_prepare_next( producer );
519
520 return 0;
521 }
522
523 /** Close the playlist.
524 */
525
526 void mlt_playlist_close( mlt_playlist this )
527 {
528 mlt_producer_close( &this->parent );
529 mlt_producer_close( &this->blank );
530 free( this );
531 }