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", "continue" );
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 // Total number of frames
204 // Get the properties
205 mlt_properties properties
= mlt_playlist_properties( this );
207 // Get the eof handling
208 char *eof
= mlt_properties_get( properties
, "eof" );
210 // Index for the main loop
213 // Loop for each producer until found
214 for ( i
= 0; i
< this->count
; i
++ )
216 // Increment the total
217 total
+= this->list
[ i
]->frame_count
;
219 // Check if the position indicates that we have found the clip
220 if ( position
< this->list
[ i
]->frame_count
)
222 // Found it, now break
223 producer
= this->list
[ i
]->producer
;
228 // Decrement position by length of this entry
229 position
-= this->list
[ i
]->frame_count
;
233 // Seek in real producer to relative position
234 if ( producer
!= NULL
)
236 position
+= this->list
[ i
]->frame_in
;
237 mlt_producer_seek( producer
, position
);
239 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
241 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
242 mlt_producer this_producer
= mlt_playlist_producer( this );
243 mlt_producer_seek( this_producer
, total
- 1 - mlt_producer_get_in( this_producer
) );
244 producer
= entry
->producer
;
245 mlt_producer_seek( producer
, entry
->frame_out
);
246 mlt_producer_set_speed( this_producer
, 0 );
248 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
250 playlist_entry
*entry
= this->list
[ 0 ];
251 mlt_producer this_producer
= mlt_playlist_producer( this );
252 mlt_producer_seek( this_producer
, 0 );
253 producer
= entry
->producer
;
254 mlt_producer_seek( producer
, entry
->frame_in
);
258 producer
= &this->blank
;
264 /** Invoked when a producer indicates that it has prematurely reached its end.
267 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
269 // Default producer to blank
270 mlt_producer producer
= &this->blank
;
272 // Map playlist position to real producer in virtual playlist
273 mlt_position position
= mlt_producer_frame( &this->parent
);
275 // Loop through the virtual playlist
278 for ( i
= 0; i
< this->count
; i
++ )
280 if ( position
< this->list
[ i
]->frame_count
)
282 // Found it, now break
283 producer
= this->list
[ i
]->producer
;
288 // Decrement position by length of this entry
289 position
-= this->list
[ i
]->frame_count
;
293 // Seek in real producer to relative position
294 if ( i
< this->count
)
296 // Update the frame_count for the changed clip (hmmm)
297 this->list
[ i
]->frame_out
= position
;
298 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
300 // Refresh the playlist
301 mlt_playlist_virtual_refresh( this );
307 /** Obtain the current clips index.
310 int mlt_playlist_current_clip( mlt_playlist
this )
312 // Map playlist position to real producer in virtual playlist
313 mlt_position position
= mlt_producer_frame( &this->parent
);
315 // Loop through the virtual playlist
318 for ( i
= 0; i
< this->count
; i
++ )
320 if ( position
< this->list
[ i
]->frame_count
)
322 // Found it, now break
327 // Decrement position by length of this entry
328 position
-= this->list
[ i
]->frame_count
;
335 /** Obtain the current clips producer.
338 mlt_producer
mlt_playlist_current( mlt_playlist
this )
340 int i
= mlt_playlist_current_clip( this );
341 if ( i
< this->count
)
342 return this->list
[ i
]->producer
;
347 /** Get the position which corresponds to the start of the next clip.
350 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
352 mlt_position position
= 0;
353 int absolute_clip
= index
;
356 // Determine the absolute clip
359 case mlt_whence_relative_start
:
360 absolute_clip
= index
;
363 case mlt_whence_relative_current
:
364 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
367 case mlt_whence_relative_end
:
368 absolute_clip
= this->count
- index
;
372 // Check that we're in a valid range
373 if ( absolute_clip
< 0 )
375 else if ( absolute_clip
> this->count
)
376 absolute_clip
= this->count
;
378 // Now determine the position
379 for ( i
= 0; i
< absolute_clip
; i
++ )
380 position
+= this->list
[ i
]->frame_count
;
385 /** Get all the info about the clip specified.
388 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
390 int error
= index
< 0 || index
>= this->count
;
391 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
394 mlt_producer producer
= this->list
[ index
]->producer
;
395 mlt_properties properties
= mlt_producer_properties( producer
);
397 info
->producer
= producer
;
398 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
399 info
->resource
= mlt_properties_get( properties
, "resource" );
400 info
->frame_in
= this->list
[ index
]->frame_in
;
401 info
->frame_out
= this->list
[ index
]->frame_out
;
402 info
->frame_count
= this->list
[ index
]->frame_count
;
403 info
->length
= mlt_producer_get_length( producer
);
404 info
->fps
= mlt_producer_get_fps( producer
);
409 /** Get number of clips in the playlist.
412 int mlt_playlist_count( mlt_playlist
this )
417 /** Clear the playlist.
420 int mlt_playlist_clear( mlt_playlist
this )
423 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
424 return mlt_playlist_virtual_refresh( this );
427 /** Append a producer to the playlist.
430 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
432 // Append to virtual list
433 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
436 /** Append a producer to the playlist with in/out points.
439 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
441 // Append to virtual list
442 if ( in
!= -1 && out
!= -1 )
443 return mlt_playlist_virtual_append( this, producer
, in
, out
);
445 return mlt_playlist_append( this, producer
);
448 /** Append a blank to the playlist of a given length.
451 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
453 // Append to the virtual list
454 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
457 /** Insert a producer into the playlist.
460 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
463 mlt_playlist_append_io( this, producer
, in
, out
);
465 // Move to the position specified
466 return mlt_playlist_move( this, this->count
- 1, where
);
469 /** Remove an entry in the playlist.
472 int mlt_playlist_remove( mlt_playlist
this, int where
)
474 if ( this->count
> 0 )
476 // We need to know the current clip and the position within the playlist
477 int current
= mlt_playlist_current_clip( this );
478 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
480 // We need all the details about the clip we're removing
481 mlt_playlist_clip_info where_info
;
486 // Make sure the clip to be removed is valid and correct if necessary
489 if ( where
>= this->count
)
490 where
= this->count
- 1;
492 // Get the clip info of the clip to be removed
493 mlt_playlist_get_clip_info( this, &where_info
, where
);
495 // Reorganise the list
496 for ( i
= where
+ 1; i
< this->count
; i
++ )
497 this->list
[ i
- 1 ] = this->list
[ i
];
501 if ( where
== current
)
502 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
503 else if ( where
< current
&& this->count
> 0 )
504 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
505 else if ( this->count
== 0 )
506 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
512 /** Move an entry in the playlist.
515 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
519 /* We need to ensure that the requested indexes are valid and correct it as necessary */
522 if ( src
>= this->count
)
523 src
= this->count
- 1;
527 if ( dest
>= this->count
)
528 dest
= this->count
- 1;
530 if ( src
!= dest
&& this->count
> 1 )
532 int current
= mlt_playlist_current_clip( this );
533 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
534 playlist_entry
*src_entry
= NULL
;
536 // We need all the details about the current clip
537 mlt_playlist_clip_info current_info
;
539 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
540 position
-= current_info
.start
;
542 if ( current
== src
)
544 else if ( current
> src
&& current
< dest
)
546 else if ( current
== dest
)
556 src_entry
= this->list
[ src
];
558 for ( i
= src
+ 1; i
<= dest
; i
++ )
559 this->list
[ i
- 1 ] = this->list
[ i
];
561 this->list
[ dest
] = src_entry
;
563 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
564 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
570 /** Resize the current clip.
573 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
575 int error
= clip
< 0 || clip
>= this->count
;
578 playlist_entry
*entry
= this->list
[ clip
];
579 mlt_producer producer
= entry
->producer
;
583 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
584 out
= mlt_producer_get_playtime( producer
) - 1;
593 entry
->frame_in
= in
;
594 entry
->frame_out
= out
;
595 entry
->frame_count
= out
- in
+ 1;
596 mlt_playlist_virtual_refresh( this );
601 /** Get the current frame.
604 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
606 // Get this mlt_playlist
607 mlt_playlist
this = producer
->child
;
609 // Get the real producer
610 mlt_producer real
= mlt_playlist_virtual_seek( this );
613 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
615 // Check if we're at the end of the clip
616 mlt_properties properties
= mlt_frame_properties( *frame
);
617 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
618 mlt_playlist_virtual_set_out( this );
620 // Check for notifier and call with appropriate argument
621 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
622 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
623 if ( notifier
!= NULL
)
625 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
626 notifier( argument
);
629 // Update position on the frame we're creating
630 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
632 // Position ourselves on the next frame
633 mlt_producer_prepare_next( producer
);
638 /** Close the playlist.
641 void mlt_playlist_close( mlt_playlist
this )
643 mlt_producer_close( &this->parent
);
644 mlt_producer_close( &this->blank
);