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