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
;
76 // Define the destructor
77 producer
->close
= ( mlt_destructor
)mlt_playlist_close
;
78 producer
->close_object
= this;
81 mlt_producer_init( &this->blank
, NULL
);
82 mlt_properties_set( mlt_producer_properties( &this->blank
), "mlt_service", "blank" );
83 mlt_properties_set( mlt_producer_properties( &this->blank
), "resource", "blank" );
85 // Indicate that this producer is a playlist
86 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
88 // Specify the eof condition
89 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
90 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
91 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
94 this->list
= malloc( this->size
* sizeof( playlist_entry
* ) );
100 /** Get the producer associated to this playlist.
103 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
105 return this != NULL ?
&this->parent
: NULL
;
108 /** Get the service associated to this playlist.
111 mlt_service
mlt_playlist_service( mlt_playlist
this )
113 return mlt_producer_service( &this->parent
);
116 /** Get the propertues associated to this playlist.
119 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
121 return mlt_producer_properties( &this->parent
);
124 /** Refresh the playlist after a clip has been changed.
127 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
131 // Get the fps of the first producer
132 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
133 mlt_position frame_count
= 0;
135 for ( i
= 0; i
< this->count
; i
++ )
138 mlt_producer producer
= this->list
[ i
]->producer
;
143 // Inherit it from the producer
144 fps
= mlt_producer_get_fps( producer
);
146 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
148 // Generate a warning for now - the following attempt to fix may fail
149 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
151 // It should be safe to impose fps on an image producer, but not necessarily safe for video
152 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
155 // Update the frame_count for this clip
156 frame_count
+= this->list
[ i
]->frame_count
;
159 // Refresh all properties
160 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
161 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
162 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count
);
163 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count
- 1 );
168 /** Append to the virtual playlist.
171 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
173 // Check that we have room
174 if ( this->count
>= this->size
)
177 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
178 for ( i
= this->size
; i
< this->size
+ 10; i
++ ) this->list
[ i
] = NULL
;
182 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
183 this->list
[ this->count
]->producer
= producer
;
184 this->list
[ this->count
]->frame_in
= in
;
185 this->list
[ this->count
]->frame_out
= out
;
186 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
188 mlt_properties_set( mlt_producer_properties( producer
), "eof", "pause" );
190 mlt_producer_set_speed( producer
, 0 );
194 mlt_properties_inc_ref( mlt_producer_properties( producer
) );
196 return mlt_playlist_virtual_refresh( this );
199 /** Seek in the virtual playlist.
202 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
204 // Default producer to blank
205 mlt_producer producer
= NULL
;
206 mlt_service service
= NULL
;
208 // Map playlist position to real producer in virtual playlist
209 mlt_position position
= mlt_producer_frame( &this->parent
);
211 mlt_position original
= position
;
213 // Total number of frames
216 // Get the properties
217 mlt_properties properties
= mlt_playlist_properties( this );
219 // Get the eof handling
220 char *eof
= mlt_properties_get( properties
, "eof" );
222 // Index for the main loop
225 // Loop for each producer until found
226 for ( i
= 0; i
< this->count
; i
++ )
228 // Increment the total
229 total
+= this->list
[ i
]->frame_count
;
231 // Check if the position indicates that we have found the clip
232 if ( position
< this->list
[ i
]->frame_count
)
234 // Found it, now break
235 producer
= this->list
[ i
]->producer
;
240 // Decrement position by length of this entry
241 position
-= this->list
[ i
]->frame_count
;
245 // Seek in real producer to relative position
246 if ( producer
!= NULL
)
248 position
+= this->list
[ i
]->frame_in
;
249 mlt_producer_seek( producer
, position
);
251 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
253 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
254 mlt_producer this_producer
= mlt_playlist_producer( this );
255 mlt_producer_seek( this_producer
, original
- 1 );
256 producer
= entry
->producer
;
257 mlt_producer_seek( producer
, entry
->frame_out
);
258 mlt_producer_set_speed( this_producer
, 0 );
259 mlt_producer_set_speed( producer
, 0 );
261 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
263 playlist_entry
*entry
= this->list
[ 0 ];
264 mlt_producer this_producer
= mlt_playlist_producer( this );
265 mlt_producer_seek( this_producer
, 0 );
266 producer
= entry
->producer
;
267 mlt_producer_seek( producer
, entry
->frame_in
);
271 producer
= &this->blank
;
274 if ( producer
!= NULL
)
276 service
= mlt_producer_service( producer
);
277 while ( mlt_service_consumer( service
) != NULL
)
278 service
= mlt_service_consumer( service
);
284 /** Invoked when a producer indicates that it has prematurely reached its end.
287 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
289 // Default producer to blank
290 mlt_producer producer
= &this->blank
;
292 // Map playlist position to real producer in virtual playlist
293 mlt_position position
= mlt_producer_frame( &this->parent
);
295 // Loop through the virtual playlist
298 for ( i
= 0; i
< this->count
; i
++ )
300 if ( position
< this->list
[ i
]->frame_count
)
302 // Found it, now break
303 producer
= this->list
[ i
]->producer
;
308 // Decrement position by length of this entry
309 position
-= this->list
[ i
]->frame_count
;
313 // Seek in real producer to relative position
314 if ( i
< this->count
)
316 // Update the frame_count for the changed clip (hmmm)
317 this->list
[ i
]->frame_out
= position
;
318 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
320 // Refresh the playlist
321 mlt_playlist_virtual_refresh( this );
327 /** Obtain the current clips index.
330 int mlt_playlist_current_clip( mlt_playlist
this )
332 // Map playlist position to real producer in virtual playlist
333 mlt_position position
= mlt_producer_frame( &this->parent
);
335 // Loop through the virtual playlist
338 for ( i
= 0; i
< this->count
; i
++ )
340 if ( position
< this->list
[ i
]->frame_count
)
342 // Found it, now break
347 // Decrement position by length of this entry
348 position
-= this->list
[ i
]->frame_count
;
355 /** Obtain the current clips producer.
358 mlt_producer
mlt_playlist_current( mlt_playlist
this )
360 int i
= mlt_playlist_current_clip( this );
361 if ( i
< this->count
)
362 return this->list
[ i
]->producer
;
367 /** Get the position which corresponds to the start of the next clip.
370 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
372 mlt_position position
= 0;
373 int absolute_clip
= index
;
376 // Determine the absolute clip
379 case mlt_whence_relative_start
:
380 absolute_clip
= index
;
383 case mlt_whence_relative_current
:
384 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
387 case mlt_whence_relative_end
:
388 absolute_clip
= this->count
- index
;
392 // Check that we're in a valid range
393 if ( absolute_clip
< 0 )
395 else if ( absolute_clip
> this->count
)
396 absolute_clip
= this->count
;
398 // Now determine the position
399 for ( i
= 0; i
< absolute_clip
; i
++ )
400 position
+= this->list
[ i
]->frame_count
;
405 /** Get all the info about the clip specified.
408 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
410 int error
= index
< 0 || index
>= this->count
;
411 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
414 mlt_producer producer
= this->list
[ index
]->producer
;
415 mlt_properties properties
= mlt_producer_properties( producer
);
417 info
->producer
= producer
;
418 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
419 info
->resource
= mlt_properties_get( properties
, "resource" );
420 info
->frame_in
= this->list
[ index
]->frame_in
;
421 info
->frame_out
= this->list
[ index
]->frame_out
;
422 info
->frame_count
= this->list
[ index
]->frame_count
;
423 info
->length
= mlt_producer_get_length( producer
);
424 info
->fps
= mlt_producer_get_fps( producer
);
427 // Determine the consuming filter service
428 if ( info
->producer
!= NULL
)
430 info
->service
= mlt_producer_service( info
->producer
);
431 while ( mlt_service_consumer( info
->service
) != NULL
)
432 info
->service
= mlt_service_consumer( info
->service
);
438 /** Get number of clips in the playlist.
441 int mlt_playlist_count( mlt_playlist
this )
446 /** Clear the playlist.
449 int mlt_playlist_clear( mlt_playlist
this )
452 for ( i
= 0; i
< this->count
; i
++ )
453 mlt_producer_close( this->list
[ i
]->producer
);
455 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
456 return mlt_playlist_virtual_refresh( this );
459 /** Append a producer to the playlist.
462 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
464 // Append to virtual list
465 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
468 /** Append a producer to the playlist with in/out points.
471 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
473 // Append to virtual list
474 if ( in
!= -1 && out
!= -1 )
475 return mlt_playlist_virtual_append( this, producer
, in
, out
);
477 return mlt_playlist_append( this, producer
);
480 /** Append a blank to the playlist of a given length.
483 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
485 // Append to the virtual list
486 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
489 /** Insert a producer into the playlist.
492 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
495 mlt_playlist_append_io( this, producer
, in
, out
);
497 // Move to the position specified
498 return mlt_playlist_move( this, this->count
- 1, where
);
501 /** Remove an entry in the playlist.
504 int mlt_playlist_remove( mlt_playlist
this, int where
)
506 if ( this->count
> 0 )
508 // We need to know the current clip and the position within the playlist
509 int current
= mlt_playlist_current_clip( this );
510 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
512 // We need all the details about the clip we're removing
513 mlt_playlist_clip_info where_info
;
518 // Make sure the clip to be removed is valid and correct if necessary
521 if ( where
>= this->count
)
522 where
= this->count
- 1;
524 // Get the clip info of the clip to be removed
525 mlt_playlist_get_clip_info( this, &where_info
, where
);
527 // Close the producer associated to the clip info
528 mlt_producer_close( where_info
.producer
);
530 // Reorganise the list
531 for ( i
= where
+ 1; i
< this->count
; i
++ )
532 this->list
[ i
- 1 ] = this->list
[ i
];
536 if ( where
== current
)
537 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
538 else if ( where
< current
&& this->count
> 0 )
539 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
540 else if ( this->count
== 0 )
541 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
547 /** Move an entry in the playlist.
550 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
554 /* We need to ensure that the requested indexes are valid and correct it as necessary */
557 if ( src
>= this->count
)
558 src
= this->count
- 1;
562 if ( dest
>= this->count
)
563 dest
= this->count
- 1;
565 if ( src
!= dest
&& this->count
> 1 )
567 int current
= mlt_playlist_current_clip( this );
568 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
569 playlist_entry
*src_entry
= NULL
;
571 // We need all the details about the current clip
572 mlt_playlist_clip_info current_info
;
574 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
575 position
-= current_info
.start
;
577 if ( current
== src
)
579 else if ( current
> src
&& current
< dest
)
581 else if ( current
== dest
)
584 src_entry
= this->list
[ src
];
587 for ( i
= src
; i
> dest
; i
-- )
588 this->list
[ i
] = this->list
[ i
- 1 ];
592 for ( i
= src
; i
< dest
; i
++ )
593 this->list
[ i
] = this->list
[ i
+ 1 ];
595 this->list
[ dest
] = src_entry
;
597 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
598 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
604 /** Resize the current clip.
607 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
609 int error
= clip
< 0 || clip
>= this->count
;
612 playlist_entry
*entry
= this->list
[ clip
];
613 mlt_producer producer
= entry
->producer
;
617 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
618 out
= mlt_producer_get_playtime( producer
) - 1;
627 entry
->frame_in
= in
;
628 entry
->frame_out
= out
;
629 entry
->frame_count
= out
- in
+ 1;
630 mlt_playlist_virtual_refresh( this );
635 /** Split a clip on the playlist at the given position.
638 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
640 int error
= clip
< 0 || clip
>= this->count
;
643 playlist_entry
*entry
= this->list
[ clip
];
644 if ( position
> 0 && position
< entry
->frame_count
)
646 int in
= entry
->frame_in
;
647 int out
= entry
->frame_out
;
648 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
649 mlt_playlist_insert( this, entry
->producer
, clip
+ 1, in
+ position
+ 1, out
);
659 /** Join 1 or more consecutive clips.
662 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
664 int error
= clip
< 0 || ( clip
+ 1 ) >= this->count
;
668 mlt_playlist new_clip
= mlt_playlist_init( );
669 if ( clip
+ count
>= this->count
)
670 count
= this->count
- clip
;
671 for ( i
= 0; i
<= count
; i
++ )
673 playlist_entry
*entry
= this->list
[ clip
];
674 char *resource
= mlt_properties_get( mlt_producer_properties( entry
->producer
), "resource" );
675 if ( merge
&& resource
!= NULL
&& !strcmp( resource
, "<playlist>" ) )
677 mlt_playlist old_clip
= ( mlt_playlist
)entry
->producer
;
678 while( old_clip
->count
)
680 entry
= old_clip
->list
[ 0 ];
681 mlt_playlist_append_io( new_clip
, entry
->producer
, entry
->frame_in
, entry
->frame_out
);
682 mlt_playlist_remove( old_clip
, 0 );
687 mlt_playlist_append_io( new_clip
, entry
->producer
, entry
->frame_in
, entry
->frame_out
);
689 mlt_playlist_remove( this, clip
);
691 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
692 mlt_playlist_close( new_clip
);
697 /** Get the current frame.
700 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
702 // Get this mlt_playlist
703 mlt_playlist
this = producer
->child
;
705 // Get the real producer
706 mlt_service real
= mlt_playlist_virtual_seek( this );
709 mlt_service_get_frame( real
, frame
, index
);
711 // Check if we're at the end of the clip
712 mlt_properties properties
= mlt_frame_properties( *frame
);
713 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
714 mlt_playlist_virtual_set_out( this );
716 // Check for notifier and call with appropriate argument
717 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
718 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
719 if ( notifier
!= NULL
)
721 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
722 notifier( argument
);
725 // Update position on the frame we're creating
726 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
728 // Position ourselves on the next frame
729 mlt_producer_prepare_next( producer
);
734 /** Close the playlist.
737 void mlt_playlist_close( mlt_playlist
this )
739 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
742 this->parent
.close
= NULL
;
743 mlt_producer_close( &this->parent
);
744 mlt_producer_close( &this->blank
);
745 for ( i
= 0; i
< this->count
; i
++ )
747 mlt_producer_close( this->list
[ i
]->producer
);
748 free( this->list
[ i
] );