2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
23 #include "mlt_playlist.h"
24 #include "mlt_frame.h"
30 /** Virtual playlist entry.
35 mlt_producer producer
;
36 mlt_position frame_in
;
37 mlt_position frame_out
;
38 mlt_position frame_count
;
42 /** Private definition.
47 struct mlt_producer_s parent
;
48 struct mlt_producer_s blank
;
52 playlist_entry
**list
;
55 /** Forward declarations
58 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
63 mlt_playlist
mlt_playlist_init( )
65 mlt_playlist
this = calloc( sizeof( struct mlt_playlist_s
), 1 );
68 mlt_producer producer
= &this->parent
;
70 // Construct the producer
71 mlt_producer_init( producer
, this );
73 // Override the producer get_frame
74 producer
->get_frame
= producer_get_frame
;
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" );
81 // Indicate that this producer is a playlist
82 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
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 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
93 /** Get the producer associated to this playlist.
96 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
101 /** Get the service associated to this playlist.
104 mlt_service
mlt_playlist_service( mlt_playlist
this )
106 return mlt_producer_service( &this->parent
);
109 /** Get the propertues associated to this playlist.
112 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
114 return mlt_producer_properties( &this->parent
);
117 /** Refresh the playlist after a clip has been changed.
120 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
124 // Get the fps of the first producer
125 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
126 mlt_position frame_count
= 0;
128 for ( i
= 0; i
< this->count
; i
++ )
131 mlt_producer producer
= this->list
[ i
]->producer
;
136 // Inherit it from the producer
137 fps
= mlt_producer_get_fps( producer
);
139 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
141 // Generate a warning for now - the following attempt to fix may fail
142 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
144 // It should be safe to impose fps on an image producer, but not necessarily safe for video
145 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
148 // Update the frame_count for this clip
149 frame_count
+= this->list
[ i
]->frame_count
;
152 // Refresh all properties
153 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
154 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
155 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count
);
156 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count
- 1 );
161 /** Append to the virtual playlist.
164 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
166 // Check that we have room
167 if ( this->count
>= this->size
)
170 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
171 for ( i
= this->size
; i
< this->size
+ 10; i
++ )
172 this->list
[ i
] = NULL
;
176 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
177 this->list
[ this->count
]->producer
= producer
;
178 this->list
[ this->count
]->frame_in
= in
;
179 this->list
[ this->count
]->frame_out
= out
;
180 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
182 mlt_properties_set( mlt_producer_properties( producer
), "eof", "pause" );
184 mlt_producer_set_speed( producer
, 0 );
188 return mlt_playlist_virtual_refresh( this );
191 /** Seek in the virtual playlist.
194 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
196 // Default producer to blank
197 mlt_producer producer
= NULL
;
199 // Map playlist position to real producer in virtual playlist
200 mlt_position position
= mlt_producer_frame( &this->parent
);
202 mlt_position original
= position
;
204 // Total number of frames
207 // Get the properties
208 mlt_properties properties
= mlt_playlist_properties( this );
210 // Get the eof handling
211 char *eof
= mlt_properties_get( properties
, "eof" );
213 // Index for the main loop
216 // Loop for each producer until found
217 for ( i
= 0; i
< this->count
; i
++ )
219 // Increment the total
220 total
+= this->list
[ i
]->frame_count
;
222 // Check if the position indicates that we have found the clip
223 if ( position
< this->list
[ i
]->frame_count
)
225 // Found it, now break
226 producer
= this->list
[ i
]->producer
;
231 // Decrement position by length of this entry
232 position
-= this->list
[ i
]->frame_count
;
236 // Seek in real producer to relative position
237 if ( producer
!= NULL
)
239 position
+= this->list
[ i
]->frame_in
;
240 mlt_producer_seek( producer
, position
);
242 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
244 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
245 mlt_producer this_producer
= mlt_playlist_producer( this );
246 mlt_producer_seek( this_producer
, original
- 1 );
247 producer
= entry
->producer
;
248 mlt_producer_seek( producer
, entry
->frame_out
);
249 mlt_producer_set_speed( this_producer
, 0 );
250 mlt_producer_set_speed( producer
, 0 );
252 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
254 playlist_entry
*entry
= this->list
[ 0 ];
255 mlt_producer this_producer
= mlt_playlist_producer( this );
256 mlt_producer_seek( this_producer
, 0 );
257 producer
= entry
->producer
;
258 mlt_producer_seek( producer
, entry
->frame_in
);
262 producer
= &this->blank
;
268 /** Invoked when a producer indicates that it has prematurely reached its end.
271 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
273 // Default producer to blank
274 mlt_producer producer
= &this->blank
;
276 // Map playlist position to real producer in virtual playlist
277 mlt_position position
= mlt_producer_frame( &this->parent
);
279 // Loop through the virtual playlist
282 for ( i
= 0; i
< this->count
; i
++ )
284 if ( position
< this->list
[ i
]->frame_count
)
286 // Found it, now break
287 producer
= this->list
[ i
]->producer
;
292 // Decrement position by length of this entry
293 position
-= this->list
[ i
]->frame_count
;
297 // Seek in real producer to relative position
298 if ( i
< this->count
)
300 // Update the frame_count for the changed clip (hmmm)
301 this->list
[ i
]->frame_out
= position
;
302 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
304 // Refresh the playlist
305 mlt_playlist_virtual_refresh( this );
311 /** Obtain the current clips index.
314 int mlt_playlist_current_clip( mlt_playlist
this )
316 // Map playlist position to real producer in virtual playlist
317 mlt_position position
= mlt_producer_frame( &this->parent
);
319 // Loop through the virtual playlist
322 for ( i
= 0; i
< this->count
; i
++ )
324 if ( position
< this->list
[ i
]->frame_count
)
326 // Found it, now break
331 // Decrement position by length of this entry
332 position
-= this->list
[ i
]->frame_count
;
339 /** Obtain the current clips producer.
342 mlt_producer
mlt_playlist_current( mlt_playlist
this )
344 int i
= mlt_playlist_current_clip( this );
345 if ( i
< this->count
)
346 return this->list
[ i
]->producer
;
351 /** Get the position which corresponds to the start of the next clip.
354 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
356 mlt_position position
= 0;
357 int absolute_clip
= index
;
360 // Determine the absolute clip
363 case mlt_whence_relative_start
:
364 absolute_clip
= index
;
367 case mlt_whence_relative_current
:
368 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
371 case mlt_whence_relative_end
:
372 absolute_clip
= this->count
- index
;
376 // Check that we're in a valid range
377 if ( absolute_clip
< 0 )
379 else if ( absolute_clip
> this->count
)
380 absolute_clip
= this->count
;
382 // Now determine the position
383 for ( i
= 0; i
< absolute_clip
; i
++ )
384 position
+= this->list
[ i
]->frame_count
;
389 /** Get all the info about the clip specified.
392 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
394 int error
= index
< 0 || index
>= this->count
;
395 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
398 mlt_producer producer
= this->list
[ index
]->producer
;
399 mlt_properties properties
= mlt_producer_properties( producer
);
401 info
->producer
= producer
;
402 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
403 info
->resource
= mlt_properties_get( properties
, "resource" );
404 info
->frame_in
= this->list
[ index
]->frame_in
;
405 info
->frame_out
= this->list
[ index
]->frame_out
;
406 info
->frame_count
= this->list
[ index
]->frame_count
;
407 info
->length
= mlt_producer_get_length( producer
);
408 info
->fps
= mlt_producer_get_fps( producer
);
413 /** Get number of clips in the playlist.
416 int mlt_playlist_count( mlt_playlist
this )
421 /** Clear the playlist.
424 int mlt_playlist_clear( mlt_playlist
this )
427 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
428 return mlt_playlist_virtual_refresh( this );
431 /** Append a producer to the playlist.
434 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
436 // Append to virtual list
437 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
440 /** Append a producer to the playlist with in/out points.
443 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
445 // Append to virtual list
446 if ( in
!= -1 && out
!= -1 )
447 return mlt_playlist_virtual_append( this, producer
, in
, out
);
449 return mlt_playlist_append( this, producer
);
452 /** Append a blank to the playlist of a given length.
455 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
457 // Append to the virtual list
458 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
461 /** Insert a producer into the playlist.
464 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
467 mlt_playlist_append_io( this, producer
, in
, out
);
469 // Move to the position specified
470 return mlt_playlist_move( this, this->count
- 1, where
);
473 /** Remove an entry in the playlist.
476 int mlt_playlist_remove( mlt_playlist
this, int where
)
478 if ( this->count
> 0 )
480 // We need to know the current clip and the position within the playlist
481 int current
= mlt_playlist_current_clip( this );
482 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
484 // We need all the details about the clip we're removing
485 mlt_playlist_clip_info where_info
;
490 // Make sure the clip to be removed is valid and correct if necessary
493 if ( where
>= this->count
)
494 where
= this->count
- 1;
496 // Get the clip info of the clip to be removed
497 mlt_playlist_get_clip_info( this, &where_info
, where
);
499 // Reorganise the list
500 for ( i
= where
+ 1; i
< this->count
; i
++ )
501 this->list
[ i
- 1 ] = this->list
[ i
];
505 if ( where
== current
)
506 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
507 else if ( where
< current
&& this->count
> 0 )
508 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
509 else if ( this->count
== 0 )
510 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
516 /** Move an entry in the playlist.
519 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
523 /* We need to ensure that the requested indexes are valid and correct it as necessary */
526 if ( src
>= this->count
)
527 src
= this->count
- 1;
531 if ( dest
>= this->count
)
532 dest
= this->count
- 1;
534 if ( src
!= dest
&& this->count
> 1 )
536 int current
= mlt_playlist_current_clip( this );
537 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
538 playlist_entry
*src_entry
= NULL
;
540 // We need all the details about the current clip
541 mlt_playlist_clip_info current_info
;
543 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
544 position
-= current_info
.start
;
546 if ( current
== src
)
548 else if ( current
> src
&& current
< dest
)
550 else if ( current
== dest
)
553 src_entry
= this->list
[ src
];
556 for ( i
= src
; i
> dest
; i
-- )
557 this->list
[ i
] = this->list
[ i
- 1 ];
561 for ( i
= src
; i
< dest
; i
++ )
562 this->list
[ i
] = this->list
[ i
+ 1 ];
564 this->list
[ dest
] = src_entry
;
566 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
567 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
573 /** Resize the current clip.
576 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
578 int error
= clip
< 0 || clip
>= this->count
;
581 playlist_entry
*entry
= this->list
[ clip
];
582 mlt_producer producer
= entry
->producer
;
586 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
587 out
= mlt_producer_get_playtime( producer
) - 1;
596 entry
->frame_in
= in
;
597 entry
->frame_out
= out
;
598 entry
->frame_count
= out
- in
+ 1;
599 mlt_playlist_virtual_refresh( this );
604 /** Get the current frame.
607 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
609 // Get this mlt_playlist
610 mlt_playlist
this = producer
->child
;
612 // Get the real producer
613 mlt_producer real
= mlt_playlist_virtual_seek( this );
616 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
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 );
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
)
628 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
629 notifier( argument
);
632 // Update position on the frame we're creating
633 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
635 // Position ourselves on the next frame
636 mlt_producer_prepare_next( producer
);
641 /** Close the playlist.
644 void mlt_playlist_close( mlt_playlist
this )
646 mlt_producer_close( &this->parent
);
647 mlt_producer_close( &this->blank
);