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
;
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
;
77 // Define the destructor
78 producer
->close
= ( mlt_destructor
)mlt_playlist_close
;
79 producer
->close_object
= this;
82 mlt_producer_init( &this->blank
, NULL
);
83 mlt_properties_set( mlt_producer_properties( &this->blank
), "mlt_service", "blank" );
84 mlt_properties_set( mlt_producer_properties( &this->blank
), "resource", "blank" );
86 // Indicate that this producer is a playlist
87 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
89 // Specify the eof condition
90 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
91 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
92 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
93 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
94 mlt_properties_set_position( mlt_playlist_properties( this ), "out", 0 );
95 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
98 this->list
= malloc( this->size
* sizeof( playlist_entry
* ) );
104 /** Get the producer associated to this playlist.
107 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
109 return this != NULL ?
&this->parent
: NULL
;
112 /** Get the service associated to this playlist.
115 mlt_service
mlt_playlist_service( mlt_playlist
this )
117 return mlt_producer_service( &this->parent
);
120 /** Get the propertues associated to this playlist.
123 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
125 return mlt_producer_properties( &this->parent
);
128 /** Refresh the playlist after a clip has been changed.
131 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
133 // Obtain the properties
134 mlt_properties properties
= mlt_playlist_properties( this );
136 // Get the fps of the first producer
137 double fps
= mlt_properties_get_double( properties
, "first_fps" );
139 mlt_position frame_count
= 0;
141 for ( i
= 0; i
< this->count
; i
++ )
144 mlt_producer producer
= this->list
[ i
]->producer
;
149 // Inherit it from the producer
150 fps
= mlt_producer_get_fps( producer
);
152 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
154 // Generate a warning for now - the following attempt to fix may fail
155 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
157 // It should be safe to impose fps on an image producer, but not necessarily safe for video
158 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
161 // Update the frame_count for this clip
162 frame_count
+= this->list
[ i
]->frame_count
;
165 // Refresh all properties
166 mlt_properties_set_double( properties
, "first_fps", fps
);
167 mlt_properties_set_double( properties
, "fps", fps
== 0 ?
25 : fps
);
168 mlt_events_block( properties
, properties
);
169 mlt_properties_set_position( properties
, "length", frame_count
);
170 mlt_events_unblock( properties
, properties
);
171 mlt_properties_set_position( properties
, "out", frame_count
- 1 );
176 /** Listener for producers on the playlist.
179 static void mlt_playlist_listener( mlt_producer producer
, mlt_playlist
this )
181 mlt_playlist_virtual_refresh( this );
184 /** Append to the virtual playlist.
187 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
189 mlt_properties properties
= mlt_producer_properties( producer
);
191 // Check that we have room
192 if ( this->count
>= this->size
)
195 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
196 for ( i
= this->size
; i
< this->size
+ 10; i
++ ) this->list
[ i
] = NULL
;
200 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
201 this->list
[ this->count
]->producer
= producer
;
202 this->list
[ this->count
]->frame_in
= in
;
203 this->list
[ this->count
]->frame_out
= out
;
204 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
205 this->list
[ this->count
]->event
= mlt_events_listen( properties
, this, "producer-changed", ( mlt_listener
)mlt_playlist_listener
);
206 mlt_event_inc_ref( this->list
[ this->count
]->event
);
208 mlt_properties_set( properties
, "eof", "pause" );
210 mlt_producer_set_speed( producer
, 0 );
214 mlt_properties_inc_ref( properties
);
216 return mlt_playlist_virtual_refresh( this );
219 /** Seek in the virtual playlist.
222 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
224 // Default producer to blank
225 mlt_producer producer
= NULL
;
227 // Map playlist position to real producer in virtual playlist
228 mlt_position position
= mlt_producer_frame( &this->parent
);
230 mlt_position original
= position
;
232 // Total number of frames
235 // Get the properties
236 mlt_properties properties
= mlt_playlist_properties( this );
238 // Get the eof handling
239 char *eof
= mlt_properties_get( properties
, "eof" );
241 // Index for the main loop
244 // Loop for each producer until found
245 for ( i
= 0; i
< this->count
; i
++ )
247 // Increment the total
248 total
+= this->list
[ i
]->frame_count
;
250 // Check if the position indicates that we have found the clip
251 if ( position
< this->list
[ i
]->frame_count
)
253 // Found it, now break
254 producer
= this->list
[ i
]->producer
;
259 // Decrement position by length of this entry
260 position
-= this->list
[ i
]->frame_count
;
264 // Seek in real producer to relative position
265 if ( producer
!= NULL
)
267 position
+= this->list
[ i
]->frame_in
;
268 mlt_producer_seek( producer
, position
);
270 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
272 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
273 mlt_producer this_producer
= mlt_playlist_producer( this );
274 mlt_producer_seek( this_producer
, original
- 1 );
275 producer
= entry
->producer
;
276 mlt_producer_seek( producer
, entry
->frame_out
);
277 mlt_producer_set_speed( this_producer
, 0 );
278 mlt_producer_set_speed( producer
, 0 );
280 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
282 playlist_entry
*entry
= this->list
[ 0 ];
283 mlt_producer this_producer
= mlt_playlist_producer( this );
284 mlt_producer_seek( this_producer
, 0 );
285 producer
= entry
->producer
;
286 mlt_producer_seek( producer
, entry
->frame_in
);
290 producer
= &this->blank
;
293 return mlt_producer_service( producer
);
296 /** Invoked when a producer indicates that it has prematurely reached its end.
299 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
301 // Default producer to blank
302 mlt_producer producer
= &this->blank
;
304 // Map playlist position to real producer in virtual playlist
305 mlt_position position
= mlt_producer_frame( &this->parent
);
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
315 producer
= this->list
[ i
]->producer
;
320 // Decrement position by length of this entry
321 position
-= this->list
[ i
]->frame_count
;
325 // Seek in real producer to relative position
326 if ( i
< this->count
&& this->list
[ i
]->frame_out
!= position
)
328 // Update the frame_count for the changed clip (hmmm)
329 this->list
[ i
]->frame_out
= position
;
330 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
332 // Refresh the playlist
333 mlt_playlist_virtual_refresh( this );
339 /** Obtain the current clips index.
342 int mlt_playlist_current_clip( mlt_playlist
this )
344 // Map playlist position to real producer in virtual playlist
345 mlt_position position
= mlt_producer_frame( &this->parent
);
347 // Loop through the virtual playlist
350 for ( i
= 0; i
< this->count
; i
++ )
352 if ( position
< this->list
[ i
]->frame_count
)
354 // Found it, now break
359 // Decrement position by length of this entry
360 position
-= this->list
[ i
]->frame_count
;
367 /** Obtain the current clips producer.
370 mlt_producer
mlt_playlist_current( mlt_playlist
this )
372 int i
= mlt_playlist_current_clip( this );
373 if ( i
< this->count
)
374 return this->list
[ i
]->producer
;
379 /** Get the position which corresponds to the start of the next clip.
382 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
384 mlt_position position
= 0;
385 int absolute_clip
= index
;
388 // Determine the absolute clip
391 case mlt_whence_relative_start
:
392 absolute_clip
= index
;
395 case mlt_whence_relative_current
:
396 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
399 case mlt_whence_relative_end
:
400 absolute_clip
= this->count
- index
;
404 // Check that we're in a valid range
405 if ( absolute_clip
< 0 )
407 else if ( absolute_clip
> this->count
)
408 absolute_clip
= this->count
;
410 // Now determine the position
411 for ( i
= 0; i
< absolute_clip
; i
++ )
412 position
+= this->list
[ i
]->frame_count
;
417 /** Get all the info about the clip specified.
420 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
422 int error
= index
< 0 || index
>= this->count
;
423 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
426 mlt_producer producer
= this->list
[ index
]->producer
;
427 mlt_properties properties
= mlt_producer_properties( producer
);
429 info
->producer
= producer
;
430 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
431 info
->resource
= mlt_properties_get( properties
, "resource" );
432 info
->frame_in
= this->list
[ index
]->frame_in
;
433 info
->frame_out
= this->list
[ index
]->frame_out
;
434 info
->frame_count
= this->list
[ index
]->frame_count
;
435 info
->length
= mlt_producer_get_length( producer
);
436 info
->fps
= mlt_producer_get_fps( producer
);
437 info
->event
= this->list
[ index
]->event
;
440 // Determine the consuming filter service
441 if ( info
->producer
!= NULL
)
443 info
->service
= mlt_producer_service( info
->producer
);
444 while ( mlt_service_consumer( info
->service
) != NULL
)
445 info
->service
= mlt_service_consumer( info
->service
);
451 /** Get number of clips in the playlist.
454 int mlt_playlist_count( mlt_playlist
this )
459 /** Clear the playlist.
462 int mlt_playlist_clear( mlt_playlist
this )
465 for ( i
= 0; i
< this->count
; i
++ )
467 mlt_event_close( this->list
[ i
]->event
);
468 mlt_producer_close( this->list
[ i
]->producer
);
471 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
472 return mlt_playlist_virtual_refresh( this );
475 /** Append a producer to the playlist.
478 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
480 // Append to virtual list
481 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
484 /** Append a producer to the playlist with in/out points.
487 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
489 // Append to virtual list
490 if ( in
!= -1 && out
!= -1 )
491 return mlt_playlist_virtual_append( this, producer
, in
, out
);
493 return mlt_playlist_append( this, producer
);
496 /** Append a blank to the playlist of a given length.
499 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
501 // Append to the virtual list
502 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
505 /** Insert a producer into the playlist.
508 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
511 mlt_events_block( mlt_playlist_properties( this ), this );
512 mlt_playlist_append_io( this, producer
, in
, out
);
514 // Move to the position specified
515 mlt_playlist_move( this, this->count
- 1, where
);
516 mlt_events_unblock( mlt_playlist_properties( this ), this );
518 return mlt_playlist_virtual_refresh( this );
521 /** Remove an entry in the playlist.
524 int mlt_playlist_remove( mlt_playlist
this, int where
)
526 if ( this->count
> 0 )
528 // We need to know the current clip and the position within the playlist
529 int current
= mlt_playlist_current_clip( this );
530 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
532 // We need all the details about the clip we're removing
533 mlt_playlist_clip_info where_info
;
538 // Make sure the clip to be removed is valid and correct if necessary
541 if ( where
>= this->count
)
542 where
= this->count
- 1;
544 // Get the clip info of the clip to be removed
545 mlt_playlist_get_clip_info( this, &where_info
, where
);
547 // Close the producer associated to the clip info
548 mlt_event_close( where_info
.event
);
549 mlt_producer_close( where_info
.producer
);
551 // Reorganise the list
552 for ( i
= where
+ 1; i
< this->count
; i
++ )
553 this->list
[ i
- 1 ] = this->list
[ i
];
557 if ( where
== current
)
558 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
559 else if ( where
< current
&& this->count
> 0 )
560 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
561 else if ( this->count
== 0 )
562 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
564 // Refresh the playlist
565 mlt_playlist_virtual_refresh( this );
571 /** Move an entry in the playlist.
574 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
578 /* We need to ensure that the requested indexes are valid and correct it as necessary */
581 if ( src
>= this->count
)
582 src
= this->count
- 1;
586 if ( dest
>= this->count
)
587 dest
= this->count
- 1;
589 if ( src
!= dest
&& this->count
> 1 )
591 int current
= mlt_playlist_current_clip( this );
592 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
593 playlist_entry
*src_entry
= NULL
;
595 // We need all the details about the current clip
596 mlt_playlist_clip_info current_info
;
598 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
599 position
-= current_info
.start
;
601 if ( current
== src
)
603 else if ( current
> src
&& current
< dest
)
605 else if ( current
== dest
)
608 src_entry
= this->list
[ src
];
611 for ( i
= src
; i
> dest
; i
-- )
612 this->list
[ i
] = this->list
[ i
- 1 ];
616 for ( i
= src
; i
< dest
; i
++ )
617 this->list
[ i
] = this->list
[ i
+ 1 ];
619 this->list
[ dest
] = src_entry
;
621 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
622 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
623 mlt_playlist_virtual_refresh( this );
629 /** Resize the current clip.
632 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
634 int error
= clip
< 0 || clip
>= this->count
;
637 playlist_entry
*entry
= this->list
[ clip
];
638 mlt_producer producer
= entry
->producer
;
642 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
643 out
= mlt_producer_get_playtime( producer
) - 1;
652 entry
->frame_in
= in
;
653 entry
->frame_out
= out
;
654 entry
->frame_count
= out
- in
+ 1;
655 mlt_playlist_virtual_refresh( this );
660 /** Split a clip on the playlist at the given position.
663 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
665 int error
= clip
< 0 || clip
>= this->count
;
668 playlist_entry
*entry
= this->list
[ clip
];
669 if ( position
> 0 && position
< entry
->frame_count
)
671 int in
= entry
->frame_in
;
672 int out
= entry
->frame_out
;
673 mlt_events_block( mlt_playlist_properties( this ), this );
674 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
675 mlt_events_unblock( mlt_playlist_properties( this ), this );
676 mlt_playlist_insert( this, entry
->producer
, clip
+ 1, in
+ position
+ 1, out
);
686 /** Join 1 or more consecutive clips.
689 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
691 int error
= clip
< 0 || ( clip
+ 1 ) >= this->count
;
695 mlt_playlist new_clip
= mlt_playlist_init( );
696 mlt_events_block( mlt_playlist_properties( this ), this );
697 if ( clip
+ count
>= this->count
)
698 count
= this->count
- clip
;
699 for ( i
= 0; i
<= count
; i
++ )
701 playlist_entry
*entry
= this->list
[ clip
];
702 mlt_playlist_append_io( new_clip
, entry
->producer
, entry
->frame_in
, entry
->frame_out
);
703 mlt_playlist_remove( this, clip
);
705 mlt_events_unblock( mlt_playlist_properties( this ), this );
706 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
707 mlt_playlist_close( new_clip
);
712 /** Get the current frame.
715 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
717 // Get this mlt_playlist
718 mlt_playlist
this = producer
->child
;
720 // Get the real producer
721 mlt_service real
= mlt_playlist_virtual_seek( this );
724 mlt_service_get_frame( real
, frame
, index
);
726 // Check if we're at the end of the clip
727 mlt_properties properties
= mlt_frame_properties( *frame
);
728 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
729 mlt_playlist_virtual_set_out( this );
731 // Check for notifier and call with appropriate argument
732 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
733 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
734 if ( notifier
!= NULL
)
736 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
737 notifier( argument
);
740 // Update position on the frame we're creating
741 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
743 // Position ourselves on the next frame
744 mlt_producer_prepare_next( producer
);
749 /** Close the playlist.
752 void mlt_playlist_close( mlt_playlist
this )
754 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
757 this->parent
.close
= NULL
;
758 for ( i
= 0; i
< this->count
; i
++ )
760 mlt_event_close( this->list
[ i
]->event
);
761 mlt_producer_close( this->list
[ i
]->producer
);
762 free( this->list
[ i
] );
764 mlt_producer_close( &this->blank
);
765 mlt_producer_close( &this->parent
);