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
);
207 mlt_properties_set( properties
, "eof", "pause" );
209 mlt_producer_set_speed( producer
, 0 );
213 mlt_properties_inc_ref( properties
);
215 return mlt_playlist_virtual_refresh( this );
218 /** Seek in the virtual playlist.
221 static mlt_service
mlt_playlist_virtual_seek( mlt_playlist
this )
223 // Default producer to blank
224 mlt_producer producer
= NULL
;
226 // Map playlist position to real producer in virtual playlist
227 mlt_position position
= mlt_producer_frame( &this->parent
);
229 mlt_position original
= position
;
231 // Total number of frames
234 // Get the properties
235 mlt_properties properties
= mlt_playlist_properties( this );
237 // Get the eof handling
238 char *eof
= mlt_properties_get( properties
, "eof" );
240 // Index for the main loop
243 // Loop for each producer until found
244 for ( i
= 0; i
< this->count
; i
++ )
246 // Increment the total
247 total
+= this->list
[ i
]->frame_count
;
249 // Check if the position indicates that we have found the clip
250 if ( position
< this->list
[ i
]->frame_count
)
252 // Found it, now break
253 producer
= this->list
[ i
]->producer
;
258 // Decrement position by length of this entry
259 position
-= this->list
[ i
]->frame_count
;
263 // Seek in real producer to relative position
264 if ( producer
!= NULL
)
266 position
+= this->list
[ i
]->frame_in
;
267 mlt_producer_seek( producer
, position
);
269 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
271 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
272 mlt_producer this_producer
= mlt_playlist_producer( this );
273 mlt_producer_seek( this_producer
, original
- 1 );
274 producer
= entry
->producer
;
275 mlt_producer_seek( producer
, entry
->frame_out
);
276 mlt_producer_set_speed( this_producer
, 0 );
277 mlt_producer_set_speed( producer
, 0 );
279 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
281 playlist_entry
*entry
= this->list
[ 0 ];
282 mlt_producer this_producer
= mlt_playlist_producer( this );
283 mlt_producer_seek( this_producer
, 0 );
284 producer
= entry
->producer
;
285 mlt_producer_seek( producer
, entry
->frame_in
);
289 producer
= &this->blank
;
292 return mlt_producer_service( producer
);
295 /** Invoked when a producer indicates that it has prematurely reached its end.
298 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
300 // Default producer to blank
301 mlt_producer producer
= &this->blank
;
303 // Map playlist position to real producer in virtual playlist
304 mlt_position position
= mlt_producer_frame( &this->parent
);
306 // Loop through the virtual playlist
309 for ( i
= 0; i
< this->count
; i
++ )
311 if ( position
< this->list
[ i
]->frame_count
)
313 // Found it, now break
314 producer
= this->list
[ i
]->producer
;
319 // Decrement position by length of this entry
320 position
-= this->list
[ i
]->frame_count
;
324 // Seek in real producer to relative position
325 if ( i
< this->count
&& this->list
[ i
]->frame_out
!= position
)
327 // Update the frame_count for the changed clip (hmmm)
328 this->list
[ i
]->frame_out
= position
;
329 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
331 // Refresh the playlist
332 mlt_playlist_virtual_refresh( this );
338 /** Obtain the current clips index.
341 int mlt_playlist_current_clip( mlt_playlist
this )
343 // Map playlist position to real producer in virtual playlist
344 mlt_position position
= mlt_producer_frame( &this->parent
);
346 // Loop through the virtual playlist
349 for ( i
= 0; i
< this->count
; i
++ )
351 if ( position
< this->list
[ i
]->frame_count
)
353 // Found it, now break
358 // Decrement position by length of this entry
359 position
-= this->list
[ i
]->frame_count
;
366 /** Obtain the current clips producer.
369 mlt_producer
mlt_playlist_current( mlt_playlist
this )
371 int i
= mlt_playlist_current_clip( this );
372 if ( i
< this->count
)
373 return this->list
[ i
]->producer
;
378 /** Get the position which corresponds to the start of the next clip.
381 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
383 mlt_position position
= 0;
384 int absolute_clip
= index
;
387 // Determine the absolute clip
390 case mlt_whence_relative_start
:
391 absolute_clip
= index
;
394 case mlt_whence_relative_current
:
395 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
398 case mlt_whence_relative_end
:
399 absolute_clip
= this->count
- index
;
403 // Check that we're in a valid range
404 if ( absolute_clip
< 0 )
406 else if ( absolute_clip
> this->count
)
407 absolute_clip
= this->count
;
409 // Now determine the position
410 for ( i
= 0; i
< absolute_clip
; i
++ )
411 position
+= this->list
[ i
]->frame_count
;
416 /** Get all the info about the clip specified.
419 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
421 int error
= index
< 0 || index
>= this->count
;
422 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
425 mlt_producer producer
= this->list
[ index
]->producer
;
426 mlt_properties properties
= mlt_producer_properties( producer
);
428 info
->producer
= producer
;
429 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
430 info
->resource
= mlt_properties_get( properties
, "resource" );
431 info
->frame_in
= this->list
[ index
]->frame_in
;
432 info
->frame_out
= this->list
[ index
]->frame_out
;
433 info
->frame_count
= this->list
[ index
]->frame_count
;
434 info
->length
= mlt_producer_get_length( producer
);
435 info
->fps
= mlt_producer_get_fps( producer
);
436 info
->event
= this->list
[ index
]->event
;
439 // Determine the consuming filter service
440 if ( info
->producer
!= NULL
)
442 info
->service
= mlt_producer_service( info
->producer
);
443 while ( mlt_service_consumer( info
->service
) != NULL
)
444 info
->service
= mlt_service_consumer( info
->service
);
450 /** Get number of clips in the playlist.
453 int mlt_playlist_count( mlt_playlist
this )
458 /** Clear the playlist.
461 int mlt_playlist_clear( mlt_playlist
this )
464 for ( i
= 0; i
< this->count
; i
++ )
466 mlt_event_close( this->list
[ i
]->event
);
467 mlt_producer_close( this->list
[ i
]->producer
);
470 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
471 return mlt_playlist_virtual_refresh( this );
474 /** Append a producer to the playlist.
477 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
479 // Append to virtual list
480 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
483 /** Append a producer to the playlist with in/out points.
486 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
488 // Append to virtual list
489 if ( in
!= -1 && out
!= -1 )
490 return mlt_playlist_virtual_append( this, producer
, in
, out
);
492 return mlt_playlist_append( this, producer
);
495 /** Append a blank to the playlist of a given length.
498 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
500 // Append to the virtual list
501 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
504 /** Insert a producer into the playlist.
507 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
510 mlt_events_block( mlt_playlist_properties( this ), this );
511 mlt_playlist_append_io( this, producer
, in
, out
);
513 // Move to the position specified
514 mlt_playlist_move( this, this->count
- 1, where
);
515 mlt_events_unblock( mlt_playlist_properties( this ), this );
517 return mlt_playlist_virtual_refresh( this );
520 /** Remove an entry in the playlist.
523 int mlt_playlist_remove( mlt_playlist
this, int where
)
525 if ( this->count
> 0 )
527 // We need to know the current clip and the position within the playlist
528 int current
= mlt_playlist_current_clip( this );
529 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
531 // We need all the details about the clip we're removing
532 mlt_playlist_clip_info where_info
;
537 // Make sure the clip to be removed is valid and correct if necessary
540 if ( where
>= this->count
)
541 where
= this->count
- 1;
543 // Get the clip info of the clip to be removed
544 mlt_playlist_get_clip_info( this, &where_info
, where
);
546 // Close the producer associated to the clip info
547 mlt_event_close( where_info
.event
);
548 mlt_producer_close( where_info
.producer
);
550 // Reorganise the list
551 for ( i
= where
+ 1; i
< this->count
; i
++ )
552 this->list
[ i
- 1 ] = this->list
[ i
];
556 if ( where
== current
)
557 mlt_producer_seek( mlt_playlist_producer( this ), where_info
.start
);
558 else if ( where
< current
&& this->count
> 0 )
559 mlt_producer_seek( mlt_playlist_producer( this ), position
- where_info
.frame_count
);
560 else if ( this->count
== 0 )
561 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
563 // Refresh the playlist
564 mlt_playlist_virtual_refresh( this );
570 /** Move an entry in the playlist.
573 int mlt_playlist_move( mlt_playlist
this, int src
, int dest
)
577 /* We need to ensure that the requested indexes are valid and correct it as necessary */
580 if ( src
>= this->count
)
581 src
= this->count
- 1;
585 if ( dest
>= this->count
)
586 dest
= this->count
- 1;
588 if ( src
!= dest
&& this->count
> 1 )
590 int current
= mlt_playlist_current_clip( this );
591 mlt_position position
= mlt_producer_position( mlt_playlist_producer( this ) );
592 playlist_entry
*src_entry
= NULL
;
594 // We need all the details about the current clip
595 mlt_playlist_clip_info current_info
;
597 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
598 position
-= current_info
.start
;
600 if ( current
== src
)
602 else if ( current
> src
&& current
< dest
)
604 else if ( current
== dest
)
607 src_entry
= this->list
[ src
];
610 for ( i
= src
; i
> dest
; i
-- )
611 this->list
[ i
] = this->list
[ i
- 1 ];
615 for ( i
= src
; i
< dest
; i
++ )
616 this->list
[ i
] = this->list
[ i
+ 1 ];
618 this->list
[ dest
] = src_entry
;
620 mlt_playlist_get_clip_info( this, ¤t_info
, current
);
621 mlt_producer_seek( mlt_playlist_producer( this ), current_info
.start
+ position
);
622 mlt_playlist_virtual_refresh( this );
628 /** Resize the current clip.
631 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
633 int error
= clip
< 0 || clip
>= this->count
;
636 playlist_entry
*entry
= this->list
[ clip
];
637 mlt_producer producer
= entry
->producer
;
641 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
642 out
= mlt_producer_get_playtime( producer
) - 1;
651 entry
->frame_in
= in
;
652 entry
->frame_out
= out
;
653 entry
->frame_count
= out
- in
+ 1;
654 mlt_playlist_virtual_refresh( this );
659 /** Split a clip on the playlist at the given position.
662 int mlt_playlist_split( mlt_playlist
this, int clip
, mlt_position position
)
664 int error
= clip
< 0 || clip
>= this->count
;
667 playlist_entry
*entry
= this->list
[ clip
];
668 if ( position
> 0 && position
< entry
->frame_count
)
670 int in
= entry
->frame_in
;
671 int out
= entry
->frame_out
;
672 mlt_events_block( mlt_playlist_properties( this ), this );
673 mlt_playlist_resize_clip( this, clip
, in
, in
+ position
);
674 mlt_events_unblock( mlt_playlist_properties( this ), this );
675 mlt_playlist_insert( this, entry
->producer
, clip
+ 1, in
+ position
+ 1, out
);
685 /** Join 1 or more consecutive clips.
688 int mlt_playlist_join( mlt_playlist
this, int clip
, int count
, int merge
)
690 int error
= clip
< 0 || ( clip
+ 1 ) >= this->count
;
694 mlt_playlist new_clip
= mlt_playlist_init( );
695 mlt_events_block( mlt_playlist_properties( this ), this );
696 if ( clip
+ count
>= this->count
)
697 count
= this->count
- clip
;
698 for ( i
= 0; i
<= count
; i
++ )
700 playlist_entry
*entry
= this->list
[ clip
];
701 mlt_playlist_append_io( new_clip
, entry
->producer
, entry
->frame_in
, entry
->frame_out
);
702 mlt_playlist_remove( this, clip
);
704 mlt_events_unblock( mlt_playlist_properties( this ), this );
705 mlt_playlist_insert( this, mlt_playlist_producer( new_clip
), clip
, 0, -1 );
706 mlt_playlist_close( new_clip
);
711 /** Get the current frame.
714 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
716 // Get this mlt_playlist
717 mlt_playlist
this = producer
->child
;
719 // Get the real producer
720 mlt_service real
= mlt_playlist_virtual_seek( this );
723 mlt_service_get_frame( real
, frame
, index
);
725 // Check if we're at the end of the clip
726 mlt_properties properties
= mlt_frame_properties( *frame
);
727 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
728 mlt_playlist_virtual_set_out( this );
730 // Check for notifier and call with appropriate argument
731 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
732 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
733 if ( notifier
!= NULL
)
735 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
736 notifier( argument
);
739 // Update position on the frame we're creating
740 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
742 // Position ourselves on the next frame
743 mlt_producer_prepare_next( producer
);
748 /** Close the playlist.
751 void mlt_playlist_close( mlt_playlist
this )
753 if ( this != NULL
&& mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
756 this->parent
.close
= NULL
;
757 for ( i
= 0; i
< this->count
; i
++ )
759 mlt_event_close( this->list
[ i
]->event
);
760 mlt_producer_close( this->list
[ i
]->producer
);
761 free( this->list
[ i
] );
763 mlt_producer_close( &this->blank
);
764 mlt_producer_close( &this->parent
);