Many ffmpeg and sdl mods
[melted] / mlt / src / framework / mlt_playlist.c
1 /*
2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "config.h"
22
23 #include "mlt_playlist.h"
24 #include "mlt_frame.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 /** Virtual playlist entry.
30 */
31
32 typedef struct
33 {
34 mlt_producer producer;
35 mlt_timecode in;
36 mlt_timecode playtime;
37 }
38 playlist_entry;
39
40 /** Private definition.
41 */
42
43 struct mlt_playlist_s
44 {
45 struct mlt_producer_s parent;
46 struct mlt_producer_s blank;
47
48 int size;
49 int count;
50 playlist_entry **list;
51 };
52
53 /** Forward declarations
54 */
55
56 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
57
58 /** Constructor.
59 */
60
61 mlt_playlist mlt_playlist_init( )
62 {
63 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
64 if ( this != NULL )
65 {
66 mlt_producer producer = &this->parent;
67
68 // Construct the producer
69 mlt_producer_init( producer, this );
70
71 // Override the producer get_frame
72 producer->get_frame = producer_get_frame;
73
74 // Initialise blank
75 mlt_producer_init( &this->blank, NULL );
76 }
77
78 return this;
79 }
80
81 /** Get the producer associated to this playlist.
82 */
83
84 mlt_producer mlt_playlist_producer( mlt_playlist this )
85 {
86 return &this->parent;
87 }
88
89 /** Get the service associated to this playlist.
90 */
91
92 mlt_service mlt_playlist_service( mlt_playlist this )
93 {
94 return mlt_producer_service( &this->parent );
95 }
96
97 /** Get the propertues associated to this playlist.
98 */
99
100 mlt_properties mlt_playlist_properties( mlt_playlist this )
101 {
102 return mlt_producer_properties( &this->parent );
103 }
104
105 /** Append to the virtual playlist.
106 */
107
108 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_timecode in, mlt_timecode out )
109 {
110 // Get the fps of the first producer
111 double fps = mlt_properties_get_double( mlt_playlist_properties( this ), "first_fps" );
112
113 // If fps is 0
114 if ( fps == 0 )
115 {
116 // Inherit it from the producer
117 fps = mlt_producer_get_fps( producer );
118 }
119 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
120 {
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 );
123
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 );
126 }
127
128 // Check that we have room
129 if ( this->count >= this->size )
130 {
131 int i;
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;
135 this->size += 10;
136 }
137
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;
142
143 this->count ++;
144
145 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", fps );
146 mlt_properties_set_double( mlt_playlist_properties( this ), "fps", fps );
147
148 return 0;
149 }
150
151 /** Seek in the virtual playlist.
152 */
153
154 static mlt_producer mlt_playlist_virtual_seek( mlt_playlist this )
155 {
156 // Default producer to blank
157 mlt_producer producer = &this->blank;
158
159 // Map playlist position to real producer in virtual playlist
160 mlt_timecode position = mlt_producer_position( &this->parent );
161
162 // Loop through the virtual playlist
163 int i = 0;
164
165 for ( i = 0; i < this->count; i ++ )
166 {
167 if ( position < this->list[ i ]->playtime )
168 {
169 // Found it, now break
170 producer = this->list[ i ]->producer;
171 position += this->list[ i ]->in;
172 break;
173 }
174 else
175 {
176 // Decrement position by length of this entry
177 position -= this->list[ i ]->playtime;
178 }
179 }
180
181 // Seek in real producer to relative position
182 mlt_producer_seek( producer, position );
183
184 return producer;
185 }
186
187 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
188 {
189 // Default producer to blank
190 mlt_producer producer = &this->blank;
191
192 // Map playlist position to real producer in virtual playlist
193 mlt_timecode position = mlt_producer_position( &this->parent );
194
195 // Loop through the virtual playlist
196 int i = 0;
197
198 for ( i = 0; i < this->count; i ++ )
199 {
200 if ( position < this->list[ i ]->playtime )
201 {
202 // Found it, now break
203 producer = this->list[ i ]->producer;
204 position += this->list[ i ]->in;
205 break;
206 }
207 else
208 {
209 // Decrement position by length of this entry
210 position -= this->list[ i ]->playtime;
211 }
212 }
213
214 // Seek in real producer to relative position
215 if ( i < this->count )
216 {
217 fprintf( stderr, "END OF CLIP %d AT %e\n", i, position );
218 this->list[ i ]->playtime = position - this->list[ i ]->in;
219 }
220
221 return producer;
222 }
223
224 /** Append a producer to the playlist.
225 */
226
227 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
228 {
229 // Append to virtual list
230 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) );
231 }
232
233 /** Append a blank to the playlist of a given length.
234 */
235
236 int mlt_playlist_blank( mlt_playlist this, mlt_timecode length )
237 {
238 // Append to the virtual list
239 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
240 }
241
242 /** Get the current frame.
243 */
244
245 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
246 {
247 // Get this mlt_playlist
248 mlt_playlist this = producer->child;
249
250 // Get the real producer
251 mlt_producer real = mlt_playlist_virtual_seek( this );
252
253 // Get the frame
254 mlt_service_get_frame( mlt_producer_service( real ), frame, index );
255
256 // Check if we're at the end of the clip
257 mlt_properties properties = mlt_frame_properties( *frame );
258 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
259 mlt_playlist_virtual_set_out( this );
260
261 // Update timecode on the frame we're creating
262 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
263
264 // Position ourselves on the next frame
265 mlt_producer_prepare_next( producer );
266
267 return 0;
268 }
269
270 /** Close the playlist.
271 */
272
273 void mlt_playlist_close( mlt_playlist this )
274 {
275 mlt_producer_close( &this->parent );
276 mlt_producer_close( &this->blank );
277 free( this );
278 }