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
);
79 // Indicate that this producer is a playlist
80 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
82 // Specify the eof condition
83 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
84 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
90 /** Get the producer associated to this playlist.
93 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
98 /** Get the service associated to this playlist.
101 mlt_service
mlt_playlist_service( mlt_playlist
this )
103 return mlt_producer_service( &this->parent
);
106 /** Get the propertues associated to this playlist.
109 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
111 return mlt_producer_properties( &this->parent
);
114 /** Refresh the playlist after a clip has been changed.
117 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
121 // Get the fps of the first producer
122 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
123 mlt_position frame_count
= 0;
125 for ( i
= 0; i
< this->count
; i
++ )
128 mlt_producer producer
= this->list
[ i
]->producer
;
133 // Inherit it from the producer
134 fps
= mlt_producer_get_fps( producer
);
136 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
138 // Generate a warning for now - the following attempt to fix may fail
139 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
141 // It should be safe to impose fps on an image producer, but not necessarily safe for video
142 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
145 // Update the frame_count for this clip
146 frame_count
+= this->list
[ i
]->frame_count
;
149 // Refresh all properties
150 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
151 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
152 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count
);
153 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count
- 1 );
158 /** Append to the virtual playlist.
161 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
163 // Check that we have room
164 if ( this->count
>= this->size
)
167 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
168 for ( i
= this->size
; i
< this->size
+ 10; i
++ )
169 this->list
[ i
] = NULL
;
173 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
174 this->list
[ this->count
]->producer
= producer
;
175 this->list
[ this->count
]->frame_in
= in
;
176 this->list
[ this->count
]->frame_out
= out
;
177 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
179 mlt_properties_set( mlt_producer_properties( producer
), "eof", "continue" );
181 mlt_producer_set_speed( producer
, 0 );
185 return mlt_playlist_virtual_refresh( this );
188 /** Seek in the virtual playlist.
191 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
193 // Default producer to blank
194 mlt_producer producer
= NULL
;
196 // Map playlist position to real producer in virtual playlist
197 mlt_position position
= mlt_producer_frame( &this->parent
);
199 // Total number of frames
202 // Get the properties
203 mlt_properties properties
= mlt_playlist_properties( this );
205 // Get the eof handling
206 char *eof
= mlt_properties_get( properties
, "eof" );
208 // Index for the main loop
211 // Loop for each producer until found
212 for ( i
= 0; i
< this->count
; i
++ )
214 // Increment the total
215 total
+= this->list
[ i
]->frame_count
;
217 // Check if the position indicates that we have found the clip
218 if ( position
< this->list
[ i
]->frame_count
)
220 // Found it, now break
221 producer
= this->list
[ i
]->producer
;
226 // Decrement position by length of this entry
227 position
-= this->list
[ i
]->frame_count
;
231 // Seek in real producer to relative position
232 if ( producer
!= NULL
)
234 position
+= this->list
[ i
]->frame_in
;
235 mlt_producer_seek( producer
, position
);
237 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
239 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
240 mlt_producer this_producer
= mlt_playlist_producer( this );
241 mlt_producer_seek( this_producer
, total
- 1 - mlt_producer_get_in( this_producer
) );
242 producer
= entry
->producer
;
243 mlt_producer_seek( producer
, entry
->frame_out
);
244 mlt_producer_set_speed( this_producer
, 0 );
246 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
248 playlist_entry
*entry
= this->list
[ 0 ];
249 mlt_producer this_producer
= mlt_playlist_producer( this );
250 mlt_producer_seek( this_producer
, 0 );
251 producer
= entry
->producer
;
252 mlt_producer_seek( producer
, entry
->frame_in
);
256 producer
= &this->blank
;
262 /** Invoked when a producer indicates that it has prematurely reached its end.
265 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
267 // Default producer to blank
268 mlt_producer producer
= &this->blank
;
270 // Map playlist position to real producer in virtual playlist
271 mlt_position position
= mlt_producer_frame( &this->parent
);
273 // Loop through the virtual playlist
276 for ( i
= 0; i
< this->count
; i
++ )
278 if ( position
< this->list
[ i
]->frame_count
)
280 // Found it, now break
281 producer
= this->list
[ i
]->producer
;
286 // Decrement position by length of this entry
287 position
-= this->list
[ i
]->frame_count
;
291 // Seek in real producer to relative position
292 if ( i
< this->count
)
294 // Update the frame_count for the changed clip (hmmm)
295 this->list
[ i
]->frame_out
= position
;
296 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
298 // Refresh the playlist
299 mlt_playlist_virtual_refresh( this );
305 /** Obtain the current clips index.
308 int mlt_playlist_current_clip( mlt_playlist
this )
310 // Map playlist position to real producer in virtual playlist
311 mlt_position position
= mlt_producer_frame( &this->parent
);
313 // Loop through the virtual playlist
316 for ( i
= 0; i
< this->count
; i
++ )
318 if ( position
< this->list
[ i
]->frame_count
)
320 // Found it, now break
325 // Decrement position by length of this entry
326 position
-= this->list
[ i
]->frame_count
;
333 /** Obtain the current clips producer.
336 mlt_producer
mlt_playlist_current( mlt_playlist
this )
338 int i
= mlt_playlist_current_clip( this );
339 if ( i
< this->count
)
340 return this->list
[ i
]->producer
;
345 /** Get the position which corresponds to the start of the next clip.
348 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
350 int64_t position
= 0;
351 int absolute_clip
= index
;
354 // Determine the absolute clip
357 case mlt_whence_relative_start
:
358 absolute_clip
= index
;
361 case mlt_whence_relative_current
:
362 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
365 case mlt_whence_relative_end
:
366 absolute_clip
= this->count
- index
;
370 // Check that we're in a valid range
371 if ( absolute_clip
< 0 )
373 else if ( absolute_clip
> this->count
)
374 absolute_clip
= this->count
;
376 // Now determine the position
377 for ( i
= 0; i
< absolute_clip
; i
++ )
378 position
+= this->list
[ i
]->frame_count
;
383 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
385 int error
= index
< 0 || index
>= this->count
;
386 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
389 mlt_producer producer
= this->list
[ index
]->producer
;
390 mlt_properties properties
= mlt_producer_properties( producer
);
391 info
->producer
= producer
;
392 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
393 info
->resource
= mlt_properties_get( properties
, "resource" );
394 info
->frame_in
= this->list
[ index
]->frame_in
;
395 info
->frame_out
= this->list
[ index
]->frame_out
;
396 info
->frame_count
= this->list
[ index
]->frame_count
;
397 info
->length
= mlt_producer_get_length( producer
);
398 info
->fps
= mlt_producer_get_fps( producer
);
403 /** Get number of clips in the playlist.
406 int mlt_playlist_count( mlt_playlist
this )
411 /** Clear the playlist.
414 int mlt_playlist_clear( mlt_playlist
this )
417 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
418 return mlt_playlist_virtual_refresh( this );
421 /** Append a producer to the playlist.
424 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
426 // Append to virtual list
427 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
430 /** Append a producer to the playlist with in/out points.
433 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
435 // Append to virtual list
436 if ( in
!= -1 && out
!= -1 )
437 return mlt_playlist_virtual_append( this, producer
, in
, out
);
439 return mlt_playlist_append( this, producer
);
442 /** Append a blank to the playlist of a given length.
445 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
447 // Append to the virtual list
448 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
451 /** Insert a producer into the playlist.
454 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
459 int mlt_playlist_remove( mlt_playlist
this, int where
)
464 int mlt_playlist_move( mlt_playlist
this, int from
, int to
)
469 /** Resize the current clip.
472 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
474 int error
= clip
< 0 || clip
>= this->count
;
477 playlist_entry
*entry
= this->list
[ clip
];
478 mlt_producer producer
= entry
->producer
;
482 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
483 out
= mlt_producer_get_playtime( producer
) - 1;
492 entry
->frame_in
= in
;
493 entry
->frame_out
= out
;
494 entry
->frame_count
= out
- in
+ 1;
495 mlt_playlist_virtual_refresh( this );
500 /** Get the current frame.
503 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
505 // Get this mlt_playlist
506 mlt_playlist
this = producer
->child
;
508 // Get the real producer
509 mlt_producer real
= mlt_playlist_virtual_seek( this );
512 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
514 // Check if we're at the end of the clip
515 mlt_properties properties
= mlt_frame_properties( *frame
);
516 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
517 mlt_playlist_virtual_set_out( this );
519 // Check for notifier and call with appropriate argument
520 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
521 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
522 if ( notifier
!= NULL
)
524 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
525 notifier( argument
);
528 // Update position on the frame we're creating
529 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
531 // Position ourselves on the next frame
532 mlt_producer_prepare_next( producer
);
537 /** Close the playlist.
540 void mlt_playlist_close( mlt_playlist
this )
542 mlt_producer_close( &this->parent
);
543 mlt_producer_close( &this->blank
);