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
;
207 // Map playlist position to real producer in virtual playlist
208 mlt_position position
= mlt_producer_frame( &this->parent
);
210 mlt_position original
= position
;
212 // Total number of frames
215 // Get the properties
216 mlt_properties properties
= mlt_playlist_properties( this );
218 // Get the eof handling
219 char *eof
= mlt_properties_get( properties
, "eof" );
221 // Index for the main loop
224 // Loop for each producer until found
225 for ( i
= 0; i
< this->count
; i
++ )
227 // Increment the total
228 total
+= this->list
[ i
]->frame_count
;
230 // Check if the position indicates that we have found the clip
231 if ( position
< this->list
[ i
]->frame_count
)
233 // Found it, now break
234 producer
= this->list
[ i
]->producer
;
239 // Decrement position by length of this entry
240 position
-= this->list
[ i
]->frame_count
;
244 // Seek in real producer to relative position
245 if ( producer
!= NULL
)
247 position
+= this->list
[ i
]->frame_in
;
248 mlt_producer_seek( producer
, position
);
250 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
252 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
253 mlt_producer this_producer
= mlt_playlist_producer( this );
254 mlt_producer_seek( this_producer
, original
- 1 );
255 producer
= entry
->producer
;
256 mlt_producer_seek( producer
, entry
->frame_out
);
257 mlt_producer_set_speed( this_producer
, 0 );
258 mlt_producer_set_speed( producer
, 0 );
260 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
262 playlist_entry
*entry
= this->list
[ 0 ];
263 mlt_producer this_producer
= mlt_playlist_producer( this );
264 mlt_producer_seek( this_producer
, 0 );
265 producer
= entry
->producer
;
266 mlt_producer_seek( producer
, entry
->frame_in
);
270 producer
= &this->blank
;
273 return mlt_producer_service( producer
);
276 /** Invoked when a producer indicates that it has prematurely reached its end.
279 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
281 // Default producer to blank
282 mlt_producer producer
= &this->blank
;
284 // Map playlist position to real producer in virtual playlist
285 mlt_position position
= mlt_producer_frame( &this->parent
);
287 // Loop through the virtual playlist
290 for ( i
= 0; i
< this->count
; i
++ )
292 if ( position
< this->list
[ i
]->frame_count
)
294 // Found it, now break
295 producer
= this->list
[ i
]->producer
;
300 // Decrement position by length of this entry
301 position
-= this->list
[ i
]->frame_count
;
305 // Seek in real producer to relative position
306 if ( i
< this->count
)
308 // Update the frame_count for the changed clip (hmmm)
309 this->list
[ i
]->frame_out
= position
;
310 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
312 // Refresh the playlist
313 mlt_playlist_virtual_refresh( this );
319 /** Obtain the current clips index.
322 int mlt_playlist_current_clip( mlt_playlist
this )
324 // Map playlist position to real producer in virtual playlist
325 mlt_position position
= mlt_producer_frame( &this->parent
);
327 // Loop through the virtual playlist
330 for ( i
= 0; i
< this->count
; i
++ )
332 if ( position
< this->list
[ i
]->frame_count
)
334 // Found it, now break
339 // Decrement position by length of this entry
340 position
-= this->list
[ i
]->frame_count
;
347 /** Obtain the current clips producer.
350 mlt_producer
mlt_playlist_current( mlt_playlist
this )
352 int i
= mlt_playlist_current_clip( this );
353 if ( i
< this->count
)
354 return this->list
[ i
]->producer
;
359 /** Get the position which corresponds to the start of the next clip.
362 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
364 mlt_position position
= 0;
365 int absolute_clip
= index
;
368 // Determine the absolute clip
371 case mlt_whence_relative_start
:
372 absolute_clip
= index
;
375 case mlt_whence_relative_current
:
376 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
379 case mlt_whence_relative_end
:
380 absolute_clip
= this->count
- index
;
384 // Check that we're in a valid range
385 if ( absolute_clip
< 0 )
387 else if ( absolute_clip
> this->count
)
388 absolute_clip
= this->count
;
390 // Now determine the position
391 for ( i
= 0; i
< absolute_clip
; i
++ )
392 position
+= this->list
[ i
]->frame_count
;
397 /** Get all the info about the clip specified.
400 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
402 int error
= index
< 0 || index
>= this->count
;
403 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
406 mlt_producer producer
= this->list
[ index
]->producer
;
407 mlt_properties properties
= mlt_producer_properties( producer
);
409 info
->producer
= producer
;
410 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
411 info
->resource
= mlt_properties_get( properties
, "resource" );
412 info
->frame_in
= this->list
[ index
]->frame_in
;
413 info
->frame_out
= this->list
[ index
]->frame_out
;
414 info
->frame_count
= this->list
[ index
]->frame_count
;
415 info
->length
= mlt_producer_get_length( producer
);
416 info
->fps
= mlt_producer_get_fps( producer
);
419 // Determine the consuming filter service
420 if ( info
->producer
!= NULL
)
422 info
->service
= mlt_producer_service( info
->producer
);
423 while ( mlt_service_consumer( info
->service
) != NULL
)
424 info
->service
= mlt_service_consumer( info
->service
);
430 /** Get number of clips in the playlist.
433 int mlt_playlist_count( mlt_playlist
this )
438 /** Clear the playlist.
441 int mlt_playlist_clear( mlt_playlist
this )
444 for ( i
= 0; i
< this->count
; i
++ )
445 mlt_producer_close( this->list
[ i
]->producer
);
447 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
448 return mlt_playlist_virtual_refresh( this );
451 /** Append a producer to the playlist.
454 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
456 // Append to virtual list
457 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
460 /** Append a producer to the playlist with in/out points.
463 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
465 // Append to virtual list
466 if ( in
!= -1 && out
!= -1 )
467 return mlt_playlist_virtual_append( this, producer
, in
, out
);
469 return mlt_playlist_append( this, producer
);
472 /** Append a blank to the playlist of a given length.
475 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
477 // Append to the virtual list
478 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
481 /** Insert a producer into the playlist.
484 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
487 mlt_playlist_append_io( this, producer
, in
, out
);
489 // Move to the position specified
490 return mlt_playlist_move( this, this->count
- 1, where
);
493 /** Remove an entry in the playlist.
496 int mlt_playlist_remove( mlt_playlist
this, int where
)
498 if ( this->count
> 0 )
500 // We need to know the current clip and the position within the playlist
501 int current
= mlt_playlist_current_clip( this );
502 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
504 // We need all the details about the clip we're removing
505 mlt_playlist_clip_info where_info
;
510 // Make sure the clip to be removed is valid and correct if necessary
513 if ( where
>= this->count
)
514 where
= this->count
- 1;
516 // Get the clip info of the clip to be removed
517 mlt_playlist_get_clip_info( this, &where_info
, where
);
519 // Close the producer associated to the clip info
520 mlt_producer_close( where_info
.producer
);
522 // Reorganise the list
523 for ( i
= where
+ 1; i
< this->count
; i
++ )
524 this->list
[ i
- 1 ] = this->list
[ i
];
528 if ( where
== current
)
529 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
530 else if ( where
< current
&& this->count
> 0 )
531 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
532 else if ( this->count
== 0 )
533 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
539 /** Move an entry in the playlist.
542 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
546 /* We need to ensure that the requested indexes are valid and correct it as necessary */
549 if ( src
>= this->count
)
550 src
= this->count
- 1;
554 if ( dest
>= this->count
)
555 dest
= this->count
- 1;
557 if ( src
!= dest
&& this->count
> 1 )
559 int current
= mlt_playlist_current_clip( this );
560 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
561 playlist_entry
*src_entry
= NULL
;
563 // We need all the details about the current clip
564 mlt_playlist_clip_info current_info
;
566 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
567 position
-= current_info
.start
;
569 if ( current
== src
)
571 else if ( current
> src
&& current
< dest
)
573 else if ( current
== dest
)
576 src_entry
= this->list
[ src
];
579 for ( i
= src
; i
> dest
; i
-- )
580 this->list
[ i
] = this->list
[ i
- 1 ];
584 for ( i
= src
; i
< dest
; i
++ )
585 this->list
[ i
] = this->list
[ i
+ 1 ];
587 this->list
[ dest
] = src_entry
;
589 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
590 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
596 /** Resize the current clip.
599 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
601 int error
= clip
< 0 || clip
>= this->count
;
604 playlist_entry
*entry
= this->list
[ clip
];
605 mlt_producer producer
= entry
->producer
;
609 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
610 out
= mlt_producer_get_playtime( producer
) - 1;
619 entry
->frame_in
= in
;
620 entry
->frame_out
= out
;
621 entry
->frame_count
= out
- in
+ 1;
622 mlt_playlist_virtual_refresh( this );
627 /** Split a clip on the playlist at the given position.
630 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
632 int error
= clip
< 0 || clip
>= this->count
;
635 playlist_entry
*entry
= this->list
[ clip
];
636 if ( position
> 0 && position
< entry
->frame_count
)
638 int in
= entry
->frame_in
;
639 int out
= entry
->frame_out
;
640 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
641 mlt_playlist_insert( this, entry
->producer
, clip
+ 1, in
+ position
+ 1, out
);
651 /** Join 1 or more consecutive clips.
654 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
656 int error
= clip
< 0 || ( clip
+ 1 ) >= this->count
;
660 mlt_playlist new_clip
= mlt_playlist_init( );
661 if ( clip
+ count
>= this->count
)
662 count
= this->count
- clip
;
663 for ( i
= 0; i
<= count
; i
++ )
665 playlist_entry
*entry
= this->list
[ clip
];
666 mlt_playlist_append_io( new_clip
, entry
->producer
, entry
->frame_in
, entry
->frame_out
);
667 mlt_playlist_remove( this, clip
);
669 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
670 mlt_playlist_close( new_clip
);
675 /** Get the current frame.
678 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
680 // Get this mlt_playlist
681 mlt_playlist
this = producer
->child
;
683 // Get the real producer
684 mlt_service real
= mlt_playlist_virtual_seek( this );
687 mlt_service_get_frame( real
, frame
, index
);
689 // Check if we're at the end of the clip
690 mlt_properties properties
= mlt_frame_properties( *frame
);
691 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
692 mlt_playlist_virtual_set_out( this );
694 // Check for notifier and call with appropriate argument
695 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
696 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
697 if ( notifier
!= NULL
)
699 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
700 notifier( argument
);
703 // Update position on the frame we're creating
704 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
706 // Position ourselves on the next frame
707 mlt_producer_prepare_next( producer
);
712 /** Close the playlist.
715 void mlt_playlist_close( mlt_playlist
this )
717 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
720 this->parent
.close
= NULL
;
721 mlt_producer_close( &this->parent
);
722 mlt_producer_close( &this->blank
);
723 for ( i
= 0; i
< this->count
; i
++ )
725 mlt_producer_close( this->list
[ i
]->producer
);
726 free( this->list
[ i
] );