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
;
77 mlt_producer_init( &this->blank
, NULL
);
79 // Indicate that this producer is a playlist
80 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL
, NULL
);
82 // Specify the eof condition
83 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
89 /** Get the producer associated to this playlist.
92 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
97 /** Get the service associated to this playlist.
100 mlt_service
mlt_playlist_service( mlt_playlist
this )
102 return mlt_producer_service( &this->parent
);
105 /** Get the propertues associated to this playlist.
108 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
110 return mlt_producer_properties( &this->parent
);
113 /** Refresh the playlist after a clip has been changed.
116 static int mlt_playlist_virtual_refresh( mlt_playlist
this )
120 // Get the fps of the first producer
121 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
122 mlt_position frame_count
= 0;
124 for ( i
= 0; i
< this->count
; i
++ )
127 mlt_producer producer
= this->list
[ i
]->producer
;
132 // Inherit it from the producer
133 fps
= mlt_producer_get_fps( producer
);
135 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
137 // Generate a warning for now - the following attempt to fix may fail
138 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
140 // It should be safe to impose fps on an image producer, but not necessarily safe for video
141 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
144 // Update the frame_count for this clip
145 frame_count
+= this->list
[ i
]->frame_count
;
148 // Refresh all properties
149 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
150 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
== 0 ?
25 : fps
);
151 mlt_properties_set_position( mlt_playlist_properties( this ), "length", frame_count
);
152 mlt_properties_set_position( mlt_playlist_properties( this ), "out", frame_count
);
157 /** Append to the virtual playlist.
160 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
162 // Check that we have room
163 if ( this->count
>= this->size
)
166 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
167 for ( i
= this->size
; i
< this->size
+ 10; i
++ )
168 this->list
[ i
] = NULL
;
172 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
173 this->list
[ this->count
]->producer
= producer
;
174 this->list
[ this->count
]->frame_in
= in
;
175 this->list
[ this->count
]->frame_out
= out
;
176 this->list
[ this->count
]->frame_count
= out
- in
+ 1;
178 mlt_producer_set_speed( producer
, 0 );
182 return mlt_playlist_virtual_refresh( this );
185 /** Seek in the virtual playlist.
188 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
190 // Default producer to blank
191 mlt_producer producer
= NULL
;
193 // Map playlist position to real producer in virtual playlist
194 mlt_position position
= mlt_producer_frame( &this->parent
);
196 // Total number of frames
199 // Get the properties
200 mlt_properties properties
= mlt_playlist_properties( this );
202 // Get the eof handling
203 char *eof
= mlt_properties_get( properties
, "eof" );
205 // Index for the main loop
208 // Loop for each producer until found
209 for ( i
= 0; i
< this->count
; i
++ )
211 // Increment the total
212 total
+= this->list
[ i
]->frame_count
;
214 // Check if the position indicates that we have found the clip
215 if ( position
< this->list
[ i
]->frame_count
)
217 // Found it, now break
218 producer
= this->list
[ i
]->producer
;
223 // Decrement position by length of this entry
224 position
-= this->list
[ i
]->frame_count
;
228 // Seek in real producer to relative position
229 if ( producer
!= NULL
)
231 position
+= this->list
[ i
]->frame_in
;
232 mlt_producer_seek( producer
, position
);
234 else if ( !strcmp( eof
, "pause" ) && total
> 0 )
236 playlist_entry
*entry
= this->list
[ this->count
- 1 ];
237 mlt_producer this_producer
= mlt_playlist_producer( this );
238 mlt_producer_seek( this_producer
, total
- 1 - mlt_producer_get_in( this_producer
) );
239 producer
= entry
->producer
;
240 mlt_producer_seek( producer
, entry
->frame_out
);
241 mlt_producer_set_speed( this_producer
, 0 );
243 else if ( !strcmp( eof
, "loop" ) && total
> 0 )
245 playlist_entry
*entry
= this->list
[ 0 ];
246 mlt_producer this_producer
= mlt_playlist_producer( this );
247 mlt_producer_seek( this_producer
, 0 );
248 producer
= entry
->producer
;
249 mlt_producer_seek( producer
, entry
->frame_in
);
253 producer
= &this->blank
;
259 /** Invoked when a producer indicates that it has prematurely reached its end.
262 static mlt_producer
mlt_playlist_virtual_set_out( mlt_playlist
this )
264 // Default producer to blank
265 mlt_producer producer
= &this->blank
;
267 // Map playlist position to real producer in virtual playlist
268 mlt_position position
= mlt_producer_frame( &this->parent
);
270 // Loop through the virtual playlist
273 for ( i
= 0; i
< this->count
; i
++ )
275 if ( position
< this->list
[ i
]->frame_count
)
277 // Found it, now break
278 producer
= this->list
[ i
]->producer
;
283 // Decrement position by length of this entry
284 position
-= this->list
[ i
]->frame_count
;
288 // Seek in real producer to relative position
289 if ( i
< this->count
)
291 // Update the frame_count for the changed clip (hmmm)
292 this->list
[ i
]->frame_out
= position
;
293 this->list
[ i
]->frame_count
= this->list
[ i
]->frame_out
- this->list
[ i
]->frame_in
+ 1;
295 // Refresh the playlist
296 mlt_playlist_virtual_refresh( this );
302 /** Obtain the current clips index.
305 int mlt_playlist_current_clip( mlt_playlist
this )
307 // Map playlist position to real producer in virtual playlist
308 mlt_position position
= mlt_producer_frame( &this->parent
);
310 // Loop through the virtual playlist
313 for ( i
= 0; i
< this->count
; i
++ )
315 if ( position
< this->list
[ i
]->frame_count
)
317 // Found it, now break
322 // Decrement position by length of this entry
323 position
-= this->list
[ i
]->frame_count
;
330 /** Obtain the current clips producer.
333 mlt_producer
mlt_playlist_current( mlt_playlist
this )
335 int i
= mlt_playlist_current_clip( this );
336 if ( i
< this->count
)
337 return this->list
[ i
]->producer
;
342 /** Get the position which corresponds to the start of the next clip.
345 mlt_position
mlt_playlist_clip( mlt_playlist
this, mlt_whence whence
, int index
)
347 int64_t position
= 0;
348 int absolute_clip
= index
;
351 // Determine the absolute clip
354 case mlt_whence_relative_start
:
355 absolute_clip
= index
;
358 case mlt_whence_relative_current
:
359 absolute_clip
= mlt_playlist_current_clip( this ) + index
;
362 case mlt_whence_relative_end
:
363 absolute_clip
= this->count
- index
;
367 // Check that we're in a valid range
368 if ( absolute_clip
< 0 )
370 else if ( absolute_clip
> this->count
)
371 absolute_clip
= this->count
;
373 // Now determine the position
374 for ( i
= 0; i
< absolute_clip
; i
++ )
375 position
+= this->list
[ i
]->frame_count
;
380 int mlt_playlist_get_clip_info( mlt_playlist
this, mlt_playlist_clip_info
*info
, int index
)
382 int error
= index
< 0 || index
>= this->count
;
383 memset( info
, 0, sizeof( mlt_playlist_clip_info
) );
386 mlt_producer producer
= this->list
[ index
]->producer
;
387 mlt_properties properties
= mlt_producer_properties( producer
);
388 info
->producer
= producer
;
389 info
->start
= mlt_playlist_clip( this, mlt_whence_relative_start
, index
);
390 info
->resource
= mlt_properties_get( properties
, "resource" );
391 info
->frame_in
= this->list
[ index
]->frame_in
;
392 info
->frame_out
= this->list
[ index
]->frame_out
;
393 info
->frame_count
= this->list
[ index
]->frame_count
;
394 info
->length
= mlt_producer_get_length( producer
);
395 info
->fps
= mlt_producer_get_fps( producer
);
400 /** Get number of clips in the playlist.
403 int mlt_playlist_count( mlt_playlist
this )
408 /** Clear the playlist.
411 int mlt_playlist_clear( mlt_playlist
this )
414 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
415 return mlt_playlist_virtual_refresh( this );
418 /** Append a producer to the playlist.
421 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
423 // Append to virtual list
424 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) - 1 );
427 /** Append a producer to the playlist with in/out points.
430 int mlt_playlist_append_io( mlt_playlist
this, mlt_producer producer
, mlt_position in
, mlt_position out
)
432 // Append to virtual list
433 if ( in
!= -1 && out
!= -1 )
434 return mlt_playlist_virtual_append( this, producer
, in
, out
);
436 return mlt_playlist_append( this, producer
);
439 /** Append a blank to the playlist of a given length.
442 int mlt_playlist_blank( mlt_playlist
this, mlt_position length
)
444 // Append to the virtual list
445 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
448 /** Insert a producer into the playlist.
451 int mlt_playlist_insert( mlt_playlist
this, mlt_producer producer
, int where
, mlt_position in
, mlt_position out
)
456 int mlt_playlist_remove( mlt_playlist
this, int where
)
461 int mlt_playlist_move( mlt_playlist
this, int from
, int to
)
466 /** Resize the current clip.
469 int mlt_playlist_resize_clip( mlt_playlist
this, int clip
, mlt_position in
, mlt_position out
)
471 int error
= clip
< 0 || clip
>= this->count
;
474 playlist_entry
*entry
= this->list
[ clip
];
475 mlt_producer producer
= entry
->producer
;
479 if ( out
<= -1 || out
>= mlt_producer_get_playtime( producer
) )
480 out
= mlt_producer_get_playtime( producer
) - 1;
489 entry
->frame_in
= in
;
490 entry
->frame_out
= out
;
491 entry
->frame_count
= out
- in
+ 1;
492 mlt_playlist_virtual_refresh( this );
497 /** Get the current frame.
500 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
502 // Get this mlt_playlist
503 mlt_playlist
this = producer
->child
;
505 // Get the real producer
506 mlt_producer real
= mlt_playlist_virtual_seek( this );
509 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
511 // Check if we're at the end of the clip
512 mlt_properties properties
= mlt_frame_properties( *frame
);
513 if ( mlt_properties_get_int( properties
, "end_of_clip" ) )
514 mlt_playlist_virtual_set_out( this );
516 // Check for notifier and call with appropriate argument
517 mlt_properties playlist_properties
= mlt_producer_properties( producer
);
518 void ( *notifier
)( void * ) = mlt_properties_get_data( playlist_properties
, "notifier", NULL
);
519 if ( notifier
!= NULL
)
521 void *argument
= mlt_properties_get_data( playlist_properties
, "notifier_arg", NULL
);
522 notifier( argument
);
525 // Update position on the frame we're creating
526 mlt_frame_set_position( *frame
, mlt_producer_frame( producer
) );
528 // Position ourselves on the next frame
529 mlt_producer_prepare_next( producer
);
534 /** Close the playlist.
537 void mlt_playlist_close( mlt_playlist
this )
539 mlt_producer_close( &this->parent
);
540 mlt_producer_close( &this->blank
);