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>" );
92 /** Get the producer associated to this playlist.
95 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
100 /** Get the service associated to this playlist.
103 mlt_service
mlt_playlist_service( mlt_playlist
this )
105 return mlt_producer_service( &this->parent
);
108 /** Get the propertues associated to this playlist.
111 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
113 return mlt_producer_properties( &this->parent
);
116 /** Refresh the playlist after a clip has been changed.
119 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
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;
127 for ( i
= 0; i
< this->count
; i
++ )
130 mlt_producer producer
= this->list
[ i
]->producer
;
135 // Inherit it from the producer
136 fps
= mlt_producer_get_fps( producer
);
138 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
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
);
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
);
147 // Update the frame_count for this clip
148 frame_count
+= this->list
[ i
]->frame_count
;
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 );
160 /** Append to the virtual playlist.
163 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
165 // Check that we have room
166 if ( this->count
>= this->size
)
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
;
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;
181 mlt_properties_set( mlt_producer_properties( producer
), "eof", "pause" );
183 mlt_producer_set_speed( producer
, 0 );
187 return mlt_playlist_virtual_refresh( this );
190 /** Seek in the virtual playlist.
193 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
195 // Default producer to blank
196 mlt_producer producer
= NULL
;
198 // Map playlist position to real producer in virtual playlist
199 mlt_position position
= mlt_producer_frame( &this->parent
);
201 mlt_position original
= position
;
203 // Total number of frames
206 // Get the properties
207 mlt_properties properties
= mlt_playlist_properties( this );
209 // Get the eof handling
210 char *eof
= mlt_properties_get( properties
, "eof" );
212 // Index for the main loop
215 // Loop for each producer until found
216 for ( i
= 0; i
< this->count
; i
++ )
218 // Increment the total
219 total
+= this->list
[ i
]->frame_count
;
221 // Check if the position indicates that we have found the clip
222 if ( position
< this->list
[ i
]->frame_count
)
224 // Found it, now break
225 producer
= this->list
[ i
]->producer
;
230 // Decrement position by length of this entry
231 position
-= this->list
[ i
]->frame_count
;
235 // Seek in real producer to relative position
236 if ( producer
!= NULL
)
238 position
+= this->list
[ i
]->frame_in
;
239 mlt_producer_seek( producer
, position
);
241 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
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 );
251 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
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
);
261 producer
= &this->blank
;
267 /** Invoked when a producer indicates that it has prematurely reached its end.
270 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
272 // Default producer to blank
273 mlt_producer producer
= &this->blank
;
275 // Map playlist position to real producer in virtual playlist
276 mlt_position position
= mlt_producer_frame( &this->parent
);
278 // Loop through the virtual playlist
281 for ( i
= 0; i
< this->count
; i
++ )
283 if ( position
< this->list
[ i
]->frame_count
)
285 // Found it, now break
286 producer
= this->list
[ i
]->producer
;
291 // Decrement position by length of this entry
292 position
-= this->list
[ i
]->frame_count
;
296 // Seek in real producer to relative position
297 if ( i
< this->count
)
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;
303 // Refresh the playlist
304 mlt_playlist_virtual_refresh( this );
310 /** Obtain the current clips index.
313 int mlt_playlist_current_clip( mlt_playlist
this )
315 // Map playlist position to real producer in virtual playlist
316 mlt_position position
= mlt_producer_frame( &this->parent
);
318 // Loop through the virtual playlist
321 for ( i
= 0; i
< this->count
; i
++ )
323 if ( position
< this->list
[ i
]->frame_count
)
325 // Found it, now break
330 // Decrement position by length of this entry
331 position
-= this->list
[ i
]->frame_count
;
338 /** Obtain the current clips producer.
341 mlt_producer
mlt_playlist_current( mlt_playlist
this )
343 int i
= mlt_playlist_current_clip( this );
344 if ( i
< this->count
)
345 return this->list
[ i
]->producer
;
350 /** Get the position which corresponds to the start of the next clip.
353 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
355 mlt_position position
= 0;
356 int absolute_clip
= index
;
359 // Determine the absolute clip
362 case mlt_whence_relative_start
:
363 absolute_clip
= index
;
366 case mlt_whence_relative_current
:
367 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
370 case mlt_whence_relative_end
:
371 absolute_clip
= this->count
- index
;
375 // Check that we're in a valid range
376 if ( absolute_clip
< 0 )
378 else if ( absolute_clip
> this->count
)
379 absolute_clip
= this->count
;
381 // Now determine the position
382 for ( i
= 0; i
< absolute_clip
; i
++ )
383 position
+= this->list
[ i
]->frame_count
;
388 /** Get all the info about the clip specified.
391 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
393 int error
= index
< 0 || index
>= this->count
;
394 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
397 mlt_producer producer
= this->list
[ index
]->producer
;
398 mlt_properties properties
= mlt_producer_properties( producer
);
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
);
412 /** Get number of clips in the playlist.
415 int mlt_playlist_count( mlt_playlist
this )
420 /** Clear the playlist.
423 int mlt_playlist_clear( mlt_playlist
this )
426 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
427 return mlt_playlist_virtual_refresh( this );
430 /** Append a producer to the playlist.
433 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
435 // Append to virtual list
436 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
439 /** Append a producer to the playlist with in/out points.
442 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
444 // Append to virtual list
445 if ( in
!= -1 && out
!= -1 )
446 return mlt_playlist_virtual_append( this, producer
, in
, out
);
448 return mlt_playlist_append( this, producer
);
451 /** Append a blank to the playlist of a given length.
454 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
456 // Append to the virtual list
457 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
460 /** Insert a producer into the playlist.
463 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
466 mlt_playlist_append_io( this, producer
, in
, out
);
468 // Move to the position specified
469 return mlt_playlist_move( this, this->count
- 1, where
);
472 /** Remove an entry in the playlist.
475 int mlt_playlist_remove( mlt_playlist
this, int where
)
477 if ( this->count
> 0 )
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 ) );
483 // We need all the details about the clip we're removing
484 mlt_playlist_clip_info where_info
;
489 // Make sure the clip to be removed is valid and correct if necessary
492 if ( where
>= this->count
)
493 where
= this->count
- 1;
495 // Get the clip info of the clip to be removed
496 mlt_playlist_get_clip_info( this, &where_info
, where
);
498 // Reorganise the list
499 for ( i
= where
+ 1; i
< this->count
; i
++ )
500 this->list
[ i
- 1 ] = this->list
[ i
];
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 );
515 /** Move an entry in the playlist.
518 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
522 /* We need to ensure that the requested indexes are valid and correct it as necessary */
525 if ( src
>= this->count
)
526 src
= this->count
- 1;
530 if ( dest
>= this->count
)
531 dest
= this->count
- 1;
533 if ( src
!= dest
&& this->count
> 1 )
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
;
539 // We need all the details about the current clip
540 mlt_playlist_clip_info current_info
;
542 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
543 position
-= current_info
.start
;
545 if ( current
== src
)
547 else if ( current
> src
&& current
< dest
)
549 else if ( current
== dest
)
559 src_entry
= this->list
[ src
];
561 for ( i
= src
+ 1; i
<= dest
; i
++ )
562 this->list
[ i
- 1 ] = this->list
[ i
];
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
);