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"
29 /** Virtual playlist entry.
34 mlt_producer producer
;
36 mlt_timecode playtime
;
40 /** Private definition.
45 struct mlt_producer_s parent
;
46 struct mlt_producer_s blank
;
50 playlist_entry
**list
;
53 /** Forward declarations
56 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
61 mlt_playlist
mlt_playlist_init( )
63 mlt_playlist
this = calloc( sizeof( struct mlt_playlist_s
), 1 );
66 mlt_producer producer
= &this->parent
;
68 // Construct the producer
69 mlt_producer_init( producer
, this );
71 // Override the producer get_frame
72 producer
->get_frame
= producer_get_frame
;
75 mlt_producer_init( &this->blank
, NULL
);
81 /** Get the producer associated to this playlist.
84 mlt_producer
mlt_playlist_producer( mlt_playlist
this )
89 /** Get the service associated to this playlist.
92 mlt_service
mlt_playlist_service( mlt_playlist
this )
94 return mlt_producer_service( &this->parent
);
97 /** Get the propertues associated to this playlist.
100 mlt_properties
mlt_playlist_properties( mlt_playlist
this )
102 return mlt_producer_properties( &this->parent
);
105 /** Append to the virtual playlist.
108 static int mlt_playlist_virtual_append( mlt_playlist
this, mlt_producer producer
, mlt_timecode in
, mlt_timecode out
)
110 // Get the fps of the first producer
111 double fps
= mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
116 // Inherit it from the producer
117 fps
= mlt_producer_get_fps( producer
);
119 else if ( fps
!= mlt_properties_get_double( mlt_producer_properties( producer
), "fps" ) )
121 // Generate a warning for now - the following attempt to fix may fail
122 fprintf( stderr
, "Warning: fps mismatch on playlist producer %d\n", this->count
);
124 // It should be safe to impose fps on an image producer, but not necessarily safe for video
125 mlt_properties_set_double( mlt_producer_properties( producer
), "fps", fps
);
128 // Check that we have room
129 if ( this->count
>= this->size
)
132 this->list
= realloc( this->list
, ( this->size
+ 10 ) * sizeof( playlist_entry
* ) );
133 for ( i
= this->size
; i
< this->size
+ 10; i
++ )
134 this->list
[ i
] = NULL
;
138 this->list
[ this->count
] = calloc( sizeof( playlist_entry
), 1 );
139 this->list
[ this->count
]->producer
= producer
;
140 this->list
[ this->count
]->in
= in
;
141 this->list
[ this->count
]->playtime
= out
- in
;
145 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps
);
146 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps
);
151 /** Seek in the virtual playlist.
154 static mlt_producer
mlt_playlist_virtual_seek( mlt_playlist
this )
156 // Default producer to blank
157 mlt_producer producer
= &this->blank
;
159 // Map playlist position to real producer in virtual playlist
160 mlt_timecode position
= mlt_producer_position( &this->parent
);
162 // Loop through the virtual playlist
165 for ( i
= 0; i
< this->count
; i
++ )
167 if ( position
< this->list
[ i
]->playtime
)
169 // Found it, now break
170 producer
= this->list
[ i
]->producer
;
171 position
+= this->list
[ i
]->in
;
176 // Decrement position by length of this entry
177 position
-= this->list
[ i
]->playtime
;
181 // Seek in real producer to relative position
182 mlt_producer_seek( producer
, position
);
187 /** Append a producer to the playlist.
190 int mlt_playlist_append( mlt_playlist
this, mlt_producer producer
)
192 // Append to virtual list
193 return mlt_playlist_virtual_append( this, producer
, 0, mlt_producer_get_playtime( producer
) );
196 /** Append a blank to the playlist of a given length.
199 int mlt_playlist_blank( mlt_playlist
this, mlt_timecode length
)
201 // Append to the virtual list
202 return mlt_playlist_virtual_append( this, &this->blank
, 0, length
);
205 /** Get the current frame.
208 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
210 // Get this mlt_playlist
211 mlt_playlist
this = producer
->child
;
213 // Get the real producer
214 mlt_producer real
= mlt_playlist_virtual_seek( this );
217 mlt_service_get_frame( mlt_producer_service( real
), frame
, index
);
219 // Update timecode on the frame we're creating
220 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
222 // Position ourselves on the next frame
223 mlt_producer_prepare_next( producer
);
228 /** Close the playlist.
231 void mlt_playlist_close( mlt_playlist
this )
233 mlt_producer_close( &this->parent
);
234 mlt_producer_close( &this->blank
);