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
;
39 mlt_timecode playtime
;
43 /** Private definition.
48 struct mlt_producer_s parent
;
49 struct mlt_producer_s blank
;
53 playlist_entry
**list
;
56 /** Forward declarations
59 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
64 mlt_playlist
mlt_playlist_init( )
66 mlt_playlist
this = calloc( sizeof( struct mlt_playlist_s
), 1 );
69 mlt_producer producer
= &this->parent
;
71 // Construct the producer
72 mlt_producer_init( producer
, this );
74 // Override the producer get_frame
75 producer
->get_frame
= producer_get_frame
;
78 mlt_producer_init( &this->blank
, NULL
);
80 // Indicate that this producer is a playlist
81 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
83 // Specify the eof condition
84 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
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 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
118 // Get the fps of the first producer
119 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
120 mlt_timecode playtime
= 0;
122 for ( i
= 0; i
< this->count
; i
++ )
125 mlt_producer producer
= this->list
[ i
]->producer
;
130 // Inherit it from the producer
131 fps
= mlt_producer_get_fps( producer
);
133 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
135 // Generate a warning for now - the following attempt to fix may fail
136 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
138 // It should be safe to impose fps on an image producer, but not necessarily safe for video
139 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
142 // Update the playtime for this clip
143 playtime
+= this->list
[ i
]->playtime
;
146 // Refresh all properties
147 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
148 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
149 mlt_properties_set_timecode( mlt_playlist_properties( this ), "length", playtime
);
150 mlt_properties_set_timecode( mlt_playlist_properties( this ), "out", playtime
);
155 /** Append to the virtual playlist.
158 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, int64_t in
, int64_t out
)
160 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "fps" );
161 double playtime
= ( double )( out
- in
+ 1 ) / fps
;
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;
178 this->list
[ this->count
]->playtime
= playtime
;
180 mlt_producer_set_speed( producer
, 0 );
184 return mlt_playlist_virtual_refresh( this );
187 /** Seek in the virtual playlist.
190 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
192 // Default producer to blank
193 mlt_producer producer
= NULL
;
195 // Map playlist position to real producer in virtual playlist
196 mlt_timecode pos
= mlt_producer_position( &this->parent
);
197 int64_t position
= mlt_producer_frame_position( &this->parent
, pos
);
200 mlt_properties properties
= mlt_playlist_properties( this );
201 char *eof
= mlt_properties_get( properties
, "eof" );
203 // Loop through the virtual playlist
206 for ( i
= 0; i
< this->count
; i
++ )
208 // Increment the total
209 total
+= this->list
[ i
]->frame_count
;
211 if ( position
< this->list
[ i
]->frame_count
)
213 // Found it, now break
214 producer
= this->list
[ i
]->producer
;
219 // Decrement position by length of this entry
220 position
-= this->list
[ i
]->frame_count
;
224 // Seek in real producer to relative position
225 if ( producer
!= NULL
)
227 position
+= this->list
[ i
]->frame_in
;
228 position
+= mlt_producer_frame_position( producer
, mlt_producer_get_in( producer
) );
229 mlt_producer_seek_frame( producer
, position
);
231 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
233 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
234 mlt_producer this_producer
= mlt_playlist_producer( this );
235 mlt_producer_seek_frame( this_producer
, total
- 1 );
236 producer
= entry
->producer
;
237 position
= mlt_producer_frame_position( producer
, mlt_producer_get_in( producer
) );
238 mlt_producer_seek_frame( producer
, position
+ entry
->frame_out
);
239 mlt_producer_set_speed( producer
, 0 );
241 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
243 playlist_entry
*entry
= this->list
[ 0 ];
244 mlt_producer this_producer
= mlt_playlist_producer( this );
245 mlt_producer_seek_frame( this_producer
, 0 );
246 producer
= entry
->producer
;
247 position
= entry
->frame_in
;
248 position
+= mlt_producer_frame_position( producer
, mlt_producer_get_in( producer
) );
249 mlt_producer_seek_frame( producer
, position
);
253 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
254 producer
= &this->blank
;
260 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
262 // Default producer to blank
263 mlt_producer producer
= &this->blank
;
265 // Map playlist position to real producer in virtual playlist
266 mlt_timecode pos
= mlt_producer_position( &this->parent
);
267 int64_t position
= mlt_producer_frame_position( &this->parent
, pos
);
269 // Loop through the virtual playlist
272 for ( i
= 0; i
< this->count
; i
++ )
274 if ( position
< this->list
[ i
]->frame_count
)
276 // Found it, now break
277 producer
= this->list
[ i
]->producer
;
282 // Decrement position by length of this entry
283 position
-= this->list
[ i
]->frame_count
;
287 // Seek in real producer to relative position
288 if ( i
< this->count
)
290 // Update the playtime for the changed clip (hmmm)
291 this->list
[ i
]->frame_count
= position
+ 1;
292 this->list
[ i
]->frame_out
= position
- this->list
[ i
]->frame_in
;
294 // Refresh the playlist
295 mlt_playlist_virtual_refresh( this );
301 int mlt_playlist_current_clip( mlt_playlist
this )
303 // Map playlist position to real producer in virtual playlist
304 mlt_timecode pos
= mlt_producer_position( &this->parent
);
305 int64_t position
= mlt_producer_frame_position( &this->parent
, pos
);
307 // Loop through the virtual playlist
310 for ( i
= 0; i
< this->count
; i
++ )
312 if ( position
< this->list
[ i
]->frame_count
)
314 // Found it, now break
319 // Decrement position by length of this entry
320 position
-= this->list
[ i
]->frame_count
;
327 mlt_producer
mlt_playlist_current( mlt_playlist
this )
329 int i
= mlt_playlist_current_clip( this );
330 if ( i
< this->count
)
331 return this->list
[ i
]->producer
;
336 /** Get the timecode which corresponds to the start of the next clip.
339 mlt_timecode
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
341 int64_t position
= 0;
342 int absolute_clip
= index
;
345 // Determine the absolute clip
348 case mlt_whence_relative_start
:
349 absolute_clip
= index
;
352 case mlt_whence_relative_current
:
353 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
356 case mlt_whence_relative_end
:
357 absolute_clip
= this->count
- index
;
361 // Check that we're in a valid range
362 if ( absolute_clip
< 0 )
364 else if ( absolute_clip
> this->count
)
365 absolute_clip
= this->count
;
367 // Now determine the timecode
368 for ( i
= 0; i
< absolute_clip
; i
++ )
369 position
+= this->list
[ i
]->frame_count
;
371 return mlt_producer_time( &this->parent
, position
);
374 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
376 int error
= index
< 0 || index
>= this->count
;
377 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
380 mlt_producer producer
= this->list
[ index
]->producer
;
381 mlt_properties properties
= mlt_producer_properties( producer
);
382 info
->producer
= producer
;
383 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
384 info
->resource
= mlt_properties_get( properties
, "resource" );
385 info
->frame_in
= this->list
[ index
]->frame_in
;
386 info
->in
= mlt_producer_time( producer
, info
->frame_in
);
387 info
->frame_out
= this->list
[ index
]->frame_out
;
388 info
->out
= mlt_producer_time( producer
, info
->frame_out
);
389 info
->playtime
= this->list
[ index
]->playtime
;
390 info
->length
= mlt_producer_get_length( producer
);
391 info
->fps
= mlt_producer_get_fps( producer
);
396 /** Get number of clips in the playlist.
399 int mlt_playlist_count( mlt_playlist
this )
404 /** Clear the playlist.
407 int mlt_playlist_clear( mlt_playlist
this )
410 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
411 return mlt_playlist_virtual_refresh( this );
414 /** Append a producer to the playlist.
417 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
419 // Append to virtual list
420 int64_t in
= mlt_producer_frame_position( producer
, mlt_producer_get_in( producer
) );
421 int64_t out
= mlt_producer_frame_position( producer
, mlt_producer_get_out( producer
) );
422 return mlt_playlist_virtual_append( this, producer
, 0, out
- in
);
425 /** Append a producer to the playlist with in/out points.
428 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, double in
, double out
)
430 // Append to virtual list
431 if ( in
!= -1 && out
!= -1 )
433 int64_t fin
= mlt_producer_frame_position( producer
, in
);
434 int64_t fout
= mlt_producer_frame_position( producer
, out
);
435 return mlt_playlist_virtual_append( this, producer
, 0, fout
- fin
);
439 return mlt_playlist_append( this, producer
);
443 /** Append a blank to the playlist of a given length.
446 int mlt_playlist_blank( mlt_playlist
this, mlt_timecode length
)
448 // Append to the virtual list
449 int64_t fout
= mlt_producer_frame_position( &this->blank
, length
);
450 return mlt_playlist_virtual_append( this, &this->blank
, 0, fout
);
453 /** Insert a producer into the playlist.
456 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_timecode in
, mlt_timecode out
)
461 int mlt_playlist_remove( mlt_playlist
this, int where
)
466 int mlt_playlist_move( mlt_playlist
this, int from
, int to
)
471 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_timecode in
, mlt_timecode out
)
473 int error
= clip
< 0 || clip
>= this->count
;
476 playlist_entry
*entry
= this->list
[ clip
];
477 mlt_producer producer
= entry
->producer
;
481 if ( out
<= -1 || out
>= mlt_producer_get_out( producer
) )
482 out
= mlt_producer_get_out( producer
);
491 int64_t fin
= mlt_producer_frame_position( producer
, in
);
492 int64_t fout
= mlt_producer_frame_position( producer
, out
);
494 entry
->frame_in
= fin
;
495 entry
->frame_out
= fout
;
496 entry
->frame_count
= fout
- fin
+ 1;
497 entry
->playtime
= out
- in
;
498 mlt_playlist_virtual_refresh( this );
503 /** Get the current frame.
506 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
508 // Get this mlt_playlist
509 mlt_playlist
this = producer
->child
;
511 // Get the real producer
512 mlt_producer real
= mlt_playlist_virtual_seek( this );
515 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
517 // Check if we're at the end of the clip
518 mlt_properties properties
= mlt_frame_properties( *frame
);
519 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
520 mlt_playlist_virtual_set_out( this );
522 // Check for notifier and call with appropriate argument
523 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
524 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
525 if ( notifier
!= NULL
)
527 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
528 notifier( argument
);
531 // Update timecode on the frame we're creating
532 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
534 // Position ourselves on the next frame
535 mlt_producer_prepare_next( producer
);
540 /** Close the playlist.
543 void mlt_playlist_close( mlt_playlist
this )
545 mlt_producer_close( &this->parent
);
546 mlt_producer_close( &this->blank
);