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" );
90 this->list
= malloc( this->size
* sizeof( playlist_entry
* ) );
96 /** Get the producer associated to this playlist.
99 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
101 return this != NULL ?
&this->parent
: NULL
;
104 /** Get the service associated to this playlist.
107 mlt_service
mlt_playlist_service( mlt_playlist
this )
109 return mlt_producer_service( &this->parent
);
112 /** Get the propertues associated to this playlist.
115 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
117 return mlt_producer_properties( &this->parent
);
120 /** Refresh the playlist after a clip has been changed.
123 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
127 // Get the fps of the first producer
128 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
129 mlt_position frame_count
= 0;
131 for ( i
= 0; i
< this->count
; i
++ )
134 mlt_producer producer
= this->list
[ i
]->producer
;
139 // Inherit it from the producer
140 fps
= mlt_producer_get_fps( producer
);
142 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
144 // Generate a warning for now - the following attempt to fix may fail
145 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
147 // It should be safe to impose fps on an image producer, but not necessarily safe for video
148 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
151 // Update the frame_count for this clip
152 frame_count
+= this->list
[ i
]->frame_count
;
155 // Refresh all properties
156 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
157 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
158 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count
);
159 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count
- 1 );
164 /** Append to the virtual playlist.
167 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
169 // Check that we have room
170 if ( this->count
>= this->size
)
173 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
174 for ( i
= this->size
; i
< this->size
+ 10; i
++ ) this->list
[ i
] = NULL
;
178 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
179 this->list
[ this->count
]->producer
= producer
;
180 this->list
[ this->count
]->frame_in
= in
;
181 this->list
[ this->count
]->frame_out
= out
;
182 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
184 mlt_properties_set( mlt_producer_properties( producer
), "eof", "pause" );
186 mlt_producer_set_speed( producer
, 0 );
190 return mlt_playlist_virtual_refresh( this );
193 /** Seek in the virtual playlist.
196 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
198 // Default producer to blank
199 mlt_producer producer
= NULL
;
200 mlt_service service
= NULL
;
202 // Map playlist position to real producer in virtual playlist
203 mlt_position position
= mlt_producer_frame( &this->parent
);
205 mlt_position original
= position
;
207 // Total number of frames
210 // Get the properties
211 mlt_properties properties
= mlt_playlist_properties( this );
213 // Get the eof handling
214 char *eof
= mlt_properties_get( properties
, "eof" );
216 // Index for the main loop
219 // Loop for each producer until found
220 for ( i
= 0; i
< this->count
; i
++ )
222 // Increment the total
223 total
+= this->list
[ i
]->frame_count
;
225 // Check if the position indicates that we have found the clip
226 if ( position
< this->list
[ i
]->frame_count
)
228 // Found it, now break
229 producer
= this->list
[ i
]->producer
;
234 // Decrement position by length of this entry
235 position
-= this->list
[ i
]->frame_count
;
239 // Seek in real producer to relative position
240 if ( producer
!= NULL
)
242 position
+= this->list
[ i
]->frame_in
;
243 mlt_producer_seek( producer
, position
);
245 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
247 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
248 mlt_producer this_producer
= mlt_playlist_producer( this );
249 mlt_producer_seek( this_producer
, original
- 1 );
250 producer
= entry
->producer
;
251 mlt_producer_seek( producer
, entry
->frame_out
);
252 mlt_producer_set_speed( this_producer
, 0 );
253 mlt_producer_set_speed( producer
, 0 );
255 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
257 playlist_entry
*entry
= this->list
[ 0 ];
258 mlt_producer this_producer
= mlt_playlist_producer( this );
259 mlt_producer_seek( this_producer
, 0 );
260 producer
= entry
->producer
;
261 mlt_producer_seek( producer
, entry
->frame_in
);
265 producer
= &this->blank
;
268 if ( producer
!= NULL
)
270 service
= mlt_producer_service( producer
);
271 while ( mlt_service_consumer( service
) != NULL
)
272 service
= mlt_service_consumer( service
);
278 /** Invoked when a producer indicates that it has prematurely reached its end.
281 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
283 // Default producer to blank
284 mlt_producer producer
= &this->blank
;
286 // Map playlist position to real producer in virtual playlist
287 mlt_position position
= mlt_producer_frame( &this->parent
);
289 // Loop through the virtual playlist
292 for ( i
= 0; i
< this->count
; i
++ )
294 if ( position
< this->list
[ i
]->frame_count
)
296 // Found it, now break
297 producer
= this->list
[ i
]->producer
;
302 // Decrement position by length of this entry
303 position
-= this->list
[ i
]->frame_count
;
307 // Seek in real producer to relative position
308 if ( i
< this->count
)
310 // Update the frame_count for the changed clip (hmmm)
311 this->list
[ i
]->frame_out
= position
;
312 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
314 // Refresh the playlist
315 mlt_playlist_virtual_refresh( this );
321 /** Obtain the current clips index.
324 int mlt_playlist_current_clip( mlt_playlist
this )
326 // Map playlist position to real producer in virtual playlist
327 mlt_position position
= mlt_producer_frame( &this->parent
);
329 // Loop through the virtual playlist
332 for ( i
= 0; i
< this->count
; i
++ )
334 if ( position
< this->list
[ i
]->frame_count
)
336 // Found it, now break
341 // Decrement position by length of this entry
342 position
-= this->list
[ i
]->frame_count
;
349 /** Obtain the current clips producer.
352 mlt_producer
mlt_playlist_current( mlt_playlist
this )
354 int i
= mlt_playlist_current_clip( this );
355 if ( i
< this->count
)
356 return this->list
[ i
]->producer
;
361 /** Get the position which corresponds to the start of the next clip.
364 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
366 mlt_position position
= 0;
367 int absolute_clip
= index
;
370 // Determine the absolute clip
373 case mlt_whence_relative_start
:
374 absolute_clip
= index
;
377 case mlt_whence_relative_current
:
378 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
381 case mlt_whence_relative_end
:
382 absolute_clip
= this->count
- index
;
386 // Check that we're in a valid range
387 if ( absolute_clip
< 0 )
389 else if ( absolute_clip
> this->count
)
390 absolute_clip
= this->count
;
392 // Now determine the position
393 for ( i
= 0; i
< absolute_clip
; i
++ )
394 position
+= this->list
[ i
]->frame_count
;
399 /** Get all the info about the clip specified.
402 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
404 int error
= index
< 0 || index
>= this->count
;
405 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
408 mlt_producer producer
= this->list
[ index
]->producer
;
409 mlt_properties properties
= mlt_producer_properties( producer
);
411 info
->producer
= producer
;
412 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
413 info
->resource
= mlt_properties_get( properties
, "resource" );
414 info
->frame_in
= this->list
[ index
]->frame_in
;
415 info
->frame_out
= this->list
[ index
]->frame_out
;
416 info
->frame_count
= this->list
[ index
]->frame_count
;
417 info
->length
= mlt_producer_get_length( producer
);
418 info
->fps
= mlt_producer_get_fps( producer
);
421 // Determine the consuming filter service
422 if ( info
->producer
!= NULL
)
424 info
->service
= mlt_producer_service( info
->producer
);
425 while ( mlt_service_consumer( info
->service
) != NULL
)
426 info
->service
= mlt_service_consumer( info
->service
);
432 /** Get number of clips in the playlist.
435 int mlt_playlist_count( mlt_playlist
this )
440 /** Clear the playlist.
443 int mlt_playlist_clear( mlt_playlist
this )
446 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
447 return mlt_playlist_virtual_refresh( this );
450 /** Append a producer to the playlist.
453 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
455 // Append to virtual list
456 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
459 /** Append a producer to the playlist with in/out points.
462 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
464 // Append to virtual list
465 if ( in
!= -1 && out
!= -1 )
466 return mlt_playlist_virtual_append( this, producer
, in
, out
);
468 return mlt_playlist_append( this, producer
);
471 /** Append a blank to the playlist of a given length.
474 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
476 // Append to the virtual list
477 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
480 /** Insert a producer into the playlist.
483 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
486 mlt_playlist_append_io( this, producer
, in
, out
);
488 // Move to the position specified
489 return mlt_playlist_move( this, this->count
- 1, where
);
492 /** Remove an entry in the playlist.
495 int mlt_playlist_remove( mlt_playlist
this, int where
)
497 if ( this->count
> 0 )
499 // We need to know the current clip and the position within the playlist
500 int current
= mlt_playlist_current_clip( this );
501 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
503 // We need all the details about the clip we're removing
504 mlt_playlist_clip_info where_info
;
509 // Make sure the clip to be removed is valid and correct if necessary
512 if ( where
>= this->count
)
513 where
= this->count
- 1;
515 // Get the clip info of the clip to be removed
516 mlt_playlist_get_clip_info( this, &where_info
, where
);
518 // Reorganise the list
519 for ( i
= where
+ 1; i
< this->count
; i
++ )
520 this->list
[ i
- 1 ] = this->list
[ i
];
524 if ( where
== current
)
525 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
526 else if ( where
< current
&& this->count
> 0 )
527 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
528 else if ( this->count
== 0 )
529 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
535 /** Move an entry in the playlist.
538 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
542 /* We need to ensure that the requested indexes are valid and correct it as necessary */
545 if ( src
>= this->count
)
546 src
= this->count
- 1;
550 if ( dest
>= this->count
)
551 dest
= this->count
- 1;
553 if ( src
!= dest
&& this->count
> 1 )
555 int current
= mlt_playlist_current_clip( this );
556 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
557 playlist_entry
*src_entry
= NULL
;
559 // We need all the details about the current clip
560 mlt_playlist_clip_info current_info
;
562 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
563 position
-= current_info
.start
;
565 if ( current
== src
)
567 else if ( current
> src
&& current
< dest
)
569 else if ( current
== dest
)
572 src_entry
= this->list
[ src
];
575 for ( i
= src
; i
> dest
; i
-- )
576 this->list
[ i
] = this->list
[ i
- 1 ];
580 for ( i
= src
; i
< dest
; i
++ )
581 this->list
[ i
] = this->list
[ i
+ 1 ];
583 this->list
[ dest
] = src_entry
;
585 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
586 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
592 /** Resize the current clip.
595 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
597 int error
= clip
< 0 || clip
>= this->count
;
600 playlist_entry
*entry
= this->list
[ clip
];
601 mlt_producer producer
= entry
->producer
;
605 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
606 out
= mlt_producer_get_playtime( producer
) - 1;
615 entry
->frame_in
= in
;
616 entry
->frame_out
= out
;
617 entry
->frame_count
= out
- in
+ 1;
618 mlt_playlist_virtual_refresh( this );
623 /** Get the current frame.
626 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
628 // Get this mlt_playlist
629 mlt_playlist
this = producer
->child
;
631 // Get the real producer
632 mlt_service real
= mlt_playlist_virtual_seek( this );
635 mlt_service_get_frame( real
, frame
, index
);
637 // Check if we're at the end of the clip
638 mlt_properties properties
= mlt_frame_properties( *frame
);
639 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
640 mlt_playlist_virtual_set_out( this );
642 // Check for notifier and call with appropriate argument
643 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
644 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
645 if ( notifier
!= NULL
)
647 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
648 notifier( argument
);
651 // Update position on the frame we're creating
652 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
654 // Position ourselves on the next frame
655 mlt_producer_prepare_next( producer
);
660 /** Close the playlist.
663 void mlt_playlist_close( mlt_playlist
this )
666 mlt_producer_close( &this->parent
);
667 mlt_producer_close( &this->blank
);
668 for ( i
= 0; i
< this->count
; i
++ )
669 free( this->list
[ i
] );