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" );
92 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
93 mlt_properties_set_position( mlt_playlist_properties( this ), "out", 0 );
94 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
97 this->list
= malloc( this->size
* sizeof( playlist_entry
* ) );
103 /** Get the producer associated to this playlist.
106 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
108 return this != NULL ?
&this->parent
: NULL
;
111 /** Get the service associated to this playlist.
114 mlt_service
mlt_playlist_service( mlt_playlist
this )
116 return mlt_producer_service( &this->parent
);
119 /** Get the propertues associated to this playlist.
122 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
124 return mlt_producer_properties( &this->parent
);
127 /** Refresh the playlist after a clip has been changed.
130 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
134 // Get the fps of the first producer
135 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
136 mlt_position frame_count
= 0;
138 for ( i
= 0; i
< this->count
; i
++ )
141 mlt_producer producer
= this->list
[ i
]->producer
;
146 // Inherit it from the producer
147 fps
= mlt_producer_get_fps( producer
);
149 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
151 // Generate a warning for now - the following attempt to fix may fail
152 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
154 // It should be safe to impose fps on an image producer, but not necessarily safe for video
155 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
158 // Update the frame_count for this clip
159 frame_count
+= this->list
[ i
]->frame_count
;
162 // Refresh all properties
163 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
164 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
165 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count
);
166 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count
- 1 );
171 /** Append to the virtual playlist.
174 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
176 // Check that we have room
177 if ( this->count
>= this->size
)
180 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
181 for ( i
= this->size
; i
< this->size
+ 10; i
++ ) this->list
[ i
] = NULL
;
185 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
186 this->list
[ this->count
]->producer
= producer
;
187 this->list
[ this->count
]->frame_in
= in
;
188 this->list
[ this->count
]->frame_out
= out
;
189 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
191 mlt_properties_set( mlt_producer_properties( producer
), "eof", "pause" );
193 mlt_producer_set_speed( producer
, 0 );
197 mlt_properties_inc_ref( mlt_producer_properties( producer
) );
199 return mlt_playlist_virtual_refresh( this );
202 /** Seek in the virtual playlist.
205 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
207 // Default producer to blank
208 mlt_producer producer
= NULL
;
210 // Map playlist position to real producer in virtual playlist
211 mlt_position position
= mlt_producer_frame( &this->parent
);
213 mlt_position original
= position
;
215 // Total number of frames
218 // Get the properties
219 mlt_properties properties
= mlt_playlist_properties( this );
221 // Get the eof handling
222 char *eof
= mlt_properties_get( properties
, "eof" );
224 // Index for the main loop
227 // Loop for each producer until found
228 for ( i
= 0; i
< this->count
; i
++ )
230 // Increment the total
231 total
+= this->list
[ i
]->frame_count
;
233 // Check if the position indicates that we have found the clip
234 if ( position
< this->list
[ i
]->frame_count
)
236 // Found it, now break
237 producer
= this->list
[ i
]->producer
;
242 // Decrement position by length of this entry
243 position
-= this->list
[ i
]->frame_count
;
247 // Seek in real producer to relative position
248 if ( producer
!= NULL
)
250 position
+= this->list
[ i
]->frame_in
;
251 mlt_producer_seek( producer
, position
);
253 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
255 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
256 mlt_producer this_producer
= mlt_playlist_producer( this );
257 mlt_producer_seek( this_producer
, original
- 1 );
258 producer
= entry
->producer
;
259 mlt_producer_seek( producer
, entry
->frame_out
);
260 mlt_producer_set_speed( this_producer
, 0 );
261 mlt_producer_set_speed( producer
, 0 );
263 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
265 playlist_entry
*entry
= this->list
[ 0 ];
266 mlt_producer this_producer
= mlt_playlist_producer( this );
267 mlt_producer_seek( this_producer
, 0 );
268 producer
= entry
->producer
;
269 mlt_producer_seek( producer
, entry
->frame_in
);
273 producer
= &this->blank
;
276 return mlt_producer_service( producer
);
279 /** Invoked when a producer indicates that it has prematurely reached its end.
282 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
284 // Default producer to blank
285 mlt_producer producer
= &this->blank
;
287 // Map playlist position to real producer in virtual playlist
288 mlt_position position
= mlt_producer_frame( &this->parent
);
290 // Loop through the virtual playlist
293 for ( i
= 0; i
< this->count
; i
++ )
295 if ( position
< this->list
[ i
]->frame_count
)
297 // Found it, now break
298 producer
= this->list
[ i
]->producer
;
303 // Decrement position by length of this entry
304 position
-= this->list
[ i
]->frame_count
;
308 // Seek in real producer to relative position
309 if ( i
< this->count
)
311 // Update the frame_count for the changed clip (hmmm)
312 this->list
[ i
]->frame_out
= position
;
313 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
315 // Refresh the playlist
316 mlt_playlist_virtual_refresh( this );
322 /** Obtain the current clips index.
325 int mlt_playlist_current_clip( mlt_playlist
this )
327 // Map playlist position to real producer in virtual playlist
328 mlt_position position
= mlt_producer_frame( &this->parent
);
330 // Loop through the virtual playlist
333 for ( i
= 0; i
< this->count
; i
++ )
335 if ( position
< this->list
[ i
]->frame_count
)
337 // Found it, now break
342 // Decrement position by length of this entry
343 position
-= this->list
[ i
]->frame_count
;
350 /** Obtain the current clips producer.
353 mlt_producer
mlt_playlist_current( mlt_playlist
this )
355 int i
= mlt_playlist_current_clip( this );
356 if ( i
< this->count
)
357 return this->list
[ i
]->producer
;
362 /** Get the position which corresponds to the start of the next clip.
365 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
367 mlt_position position
= 0;
368 int absolute_clip
= index
;
371 // Determine the absolute clip
374 case mlt_whence_relative_start
:
375 absolute_clip
= index
;
378 case mlt_whence_relative_current
:
379 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
382 case mlt_whence_relative_end
:
383 absolute_clip
= this->count
- index
;
387 // Check that we're in a valid range
388 if ( absolute_clip
< 0 )
390 else if ( absolute_clip
> this->count
)
391 absolute_clip
= this->count
;
393 // Now determine the position
394 for ( i
= 0; i
< absolute_clip
; i
++ )
395 position
+= this->list
[ i
]->frame_count
;
400 /** Get all the info about the clip specified.
403 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
405 int error
= index
< 0 || index
>= this->count
;
406 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
409 mlt_producer producer
= this->list
[ index
]->producer
;
410 mlt_properties properties
= mlt_producer_properties( producer
);
412 info
->producer
= producer
;
413 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
414 info
->resource
= mlt_properties_get( properties
, "resource" );
415 info
->frame_in
= this->list
[ index
]->frame_in
;
416 info
->frame_out
= this->list
[ index
]->frame_out
;
417 info
->frame_count
= this->list
[ index
]->frame_count
;
418 info
->length
= mlt_producer_get_length( producer
);
419 info
->fps
= mlt_producer_get_fps( producer
);
422 // Determine the consuming filter service
423 if ( info
->producer
!= NULL
)
425 info
->service
= mlt_producer_service( info
->producer
);
426 while ( mlt_service_consumer( info
->service
) != NULL
)
427 info
->service
= mlt_service_consumer( info
->service
);
433 /** Get number of clips in the playlist.
436 int mlt_playlist_count( mlt_playlist
this )
441 /** Clear the playlist.
444 int mlt_playlist_clear( mlt_playlist
this )
447 for ( i
= 0; i
< this->count
; i
++ )
448 mlt_producer_close( this->list
[ i
]->producer
);
450 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
451 return mlt_playlist_virtual_refresh( this );
454 /** Append a producer to the playlist.
457 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
459 // Append to virtual list
460 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
463 /** Append a producer to the playlist with in/out points.
466 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
468 // Append to virtual list
469 if ( in
!= -1 && out
!= -1 )
470 return mlt_playlist_virtual_append( this, producer
, in
, out
);
472 return mlt_playlist_append( this, producer
);
475 /** Append a blank to the playlist of a given length.
478 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
480 // Append to the virtual list
481 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
484 /** Insert a producer into the playlist.
487 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
490 mlt_playlist_append_io( this, producer
, in
, out
);
492 // Move to the position specified
493 return mlt_playlist_move( this, this->count
- 1, where
);
496 /** Remove an entry in the playlist.
499 int mlt_playlist_remove( mlt_playlist
this, int where
)
501 if ( this->count
> 0 )
503 // We need to know the current clip and the position within the playlist
504 int current
= mlt_playlist_current_clip( this );
505 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
507 // We need all the details about the clip we're removing
508 mlt_playlist_clip_info where_info
;
513 // Make sure the clip to be removed is valid and correct if necessary
516 if ( where
>= this->count
)
517 where
= this->count
- 1;
519 // Get the clip info of the clip to be removed
520 mlt_playlist_get_clip_info( this, &where_info
, where
);
522 // Close the producer associated to the clip info
523 mlt_producer_close( where_info
.producer
);
525 // Reorganise the list
526 for ( i
= where
+ 1; i
< this->count
; i
++ )
527 this->list
[ i
- 1 ] = this->list
[ i
];
531 if ( where
== current
)
532 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
533 else if ( where
< current
&& this->count
> 0 )
534 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
535 else if ( this->count
== 0 )
536 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
542 /** Move an entry in the playlist.
545 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
549 /* We need to ensure that the requested indexes are valid and correct it as necessary */
552 if ( src
>= this->count
)
553 src
= this->count
- 1;
557 if ( dest
>= this->count
)
558 dest
= this->count
- 1;
560 if ( src
!= dest
&& this->count
> 1 )
562 int current
= mlt_playlist_current_clip( this );
563 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
564 playlist_entry
*src_entry
= NULL
;
566 // We need all the details about the current clip
567 mlt_playlist_clip_info current_info
;
569 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
570 position
-= current_info
.start
;
572 if ( current
== src
)
574 else if ( current
> src
&& current
< dest
)
576 else if ( current
== dest
)
579 src_entry
= this->list
[ src
];
582 for ( i
= src
; i
> dest
; i
-- )
583 this->list
[ i
] = this->list
[ i
- 1 ];
587 for ( i
= src
; i
< dest
; i
++ )
588 this->list
[ i
] = this->list
[ i
+ 1 ];
590 this->list
[ dest
] = src_entry
;
592 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
593 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
599 /** Resize the current clip.
602 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
604 int error
= clip
< 0 || clip
>= this->count
;
607 playlist_entry
*entry
= this->list
[ clip
];
608 mlt_producer producer
= entry
->producer
;
612 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
613 out
= mlt_producer_get_playtime( producer
) - 1;
622 entry
->frame_in
= in
;
623 entry
->frame_out
= out
;
624 entry
->frame_count
= out
- in
+ 1;
625 mlt_playlist_virtual_refresh( this );
630 /** Split a clip on the playlist at the given position.
633 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
635 int error
= clip
< 0 || clip
>= this->count
;
638 playlist_entry
*entry
= this->list
[ clip
];
639 if ( position
> 0 && position
< entry
->frame_count
)
641 int in
= entry
->frame_in
;
642 int out
= entry
->frame_out
;
643 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
644 mlt_playlist_insert( this, entry
->producer
, clip
+ 1, in
+ position
+ 1, out
);
654 /** Join 1 or more consecutive clips.
657 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
659 int error
= clip
< 0 || ( clip
+ 1 ) >= this->count
;
663 mlt_playlist new_clip
= mlt_playlist_init( );
664 if ( clip
+ count
>= this->count
)
665 count
= this->count
- clip
;
666 for ( i
= 0; i
<= count
; i
++ )
668 playlist_entry
*entry
= this->list
[ clip
];
669 mlt_playlist_append_io( new_clip
, entry
->producer
, entry
->frame_in
, entry
->frame_out
);
670 mlt_playlist_remove( this, clip
);
672 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
673 mlt_playlist_close( new_clip
);
678 /** Get the current frame.
681 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
683 // Get this mlt_playlist
684 mlt_playlist
this = producer
->child
;
686 // Get the real producer
687 mlt_service real
= mlt_playlist_virtual_seek( this );
690 mlt_service_get_frame( real
, frame
, index
);
692 // Check if we're at the end of the clip
693 mlt_properties properties
= mlt_frame_properties( *frame
);
694 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
695 mlt_playlist_virtual_set_out( this );
697 // Check for notifier and call with appropriate argument
698 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
699 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
700 if ( notifier
!= NULL
)
702 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
703 notifier( argument
);
706 // Update position on the frame we're creating
707 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
709 // Position ourselves on the next frame
710 mlt_producer_prepare_next( producer
);
715 /** Close the playlist.
718 void mlt_playlist_close( mlt_playlist
this )
720 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
723 this->parent
.close
= NULL
;
724 mlt_producer_close( &this->parent
);
725 mlt_producer_close( &this->blank
);
726 for ( i
= 0; i
< this->count
; i
++ )
728 mlt_producer_close( this->list
[ i
]->producer
);
729 free( this->list
[ i
] );