summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/libmad/timer.c
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/libmad/timer.c') (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/libmad/timer.c66
1 files changed, 35 insertions, 31 deletions
diff --git a/core/multimedia/opieplayer/libmad/timer.c b/core/multimedia/opieplayer/libmad/timer.c
index b30680c..299fe0b 100644
--- a/core/multimedia/opieplayer/libmad/timer.c
+++ b/core/multimedia/opieplayer/libmad/timer.c
@@ -1,14 +1,14 @@
1/* 1/*
2 * mad - MPEG audio decoder 2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2001 Robert Leslie 3 * Copyright (C) 2000-2001 Robert Leslie
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
@@ -17,25 +17,28 @@
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * 18 *
19 * $Id$ 19 * $Id$
20 */ 20 */
21 21
22# ifdef HAVE_CONFIG_H 22# ifdef HAVE_CONFIG_H
23# include "libmad_config.h" 23# include "libmad_config.h"
24# endif 24# endif
25 25
26# include "libmad_global.h" 26# include "libmad_global.h"
27 27
28# include <stdio.h> 28# include <stdio.h>
29# include <assert.h> 29
30# ifdef HAVE_ASSERT_H
31# include <assert.h>
32# endif
30 33
31# include "timer.h" 34# include "timer.h"
32 35
33mad_timer_t const mad_timer_zero = { 0, 0 }; 36mad_timer_t const mad_timer_zero = { 0, 0 };
34 37
35/* 38/*
36 * NAME:timer->compare() 39 * NAME:timer->compare()
37 * DESCRIPTION:indicate relative order of two timers 40 * DESCRIPTION:indicate relative order of two timers
38 */ 41 */
39int mad_timer_compare(mad_timer_t timer1, mad_timer_t timer2) 42int mad_timer_compare(mad_timer_t timer1, mad_timer_t timer2)
40{ 43{
41 signed long diff; 44 signed long diff;
@@ -141,86 +144,87 @@ unsigned long scale_rational(unsigned long numer, unsigned long denom,
141 assert(denom != 0); 144 assert(denom != 0);
142 145
143 if (denom < scale) 146 if (denom < scale)
144 return numer * (scale / denom) + numer * (scale % denom) / denom; 147 return numer * (scale / denom) + numer * (scale % denom) / denom;
145 if (denom < numer) 148 if (denom < numer)
146 return scale * (numer / denom) + scale * (numer % denom) / denom; 149 return scale * (numer / denom) + scale * (numer % denom) / denom;
147 150
148 return numer * scale / denom; 151 return numer * scale / denom;
149} 152}
150 153
151/* 154/*
152 * NAME:timer->set() 155 * NAME:timer->set()
153 * DESCRIPTION:set timer to specific value 156 * DESCRIPTION:set timer to specific (positive) value
154 */ 157 */
155void mad_timer_set(mad_timer_t *timer, unsigned long seconds, 158void mad_timer_set(mad_timer_t *timer, unsigned long seconds,
156 unsigned long fraction, unsigned long fracparts) 159 unsigned long numer, unsigned long denom)
157{ 160{
158 timer->seconds = seconds; 161 timer->seconds = seconds;
159 162 if (numer >= denom && denom > 0) {
160 if (fraction == 0) 163 timer->seconds += numer / denom;
161 fracparts = 0; 164 numer %= denom;
162 else if (fracparts == 0) {
163 fracparts = fraction;
164 fraction = 1;
165 } 165 }
166 166
167 switch (fracparts) { 167 switch (denom) {
168 case 0: 168 case 0:
169 case 1:
169 timer->fraction = 0; 170 timer->fraction = 0;
170 break; 171 break;
171 172
172 case MAD_TIMER_RESOLUTION: 173 case MAD_TIMER_RESOLUTION:
173 timer->fraction = fraction; 174 timer->fraction = numer;
175 break;
176
177 case 1000:
178 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 1000);
174 break; 179 break;
175 180
176 case 8000: 181 case 8000:
177 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 8000); 182 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 8000);
178 break; 183 break;
179 184
180 case 11025: 185 case 11025:
181 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 11025); 186 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 11025);
182 break; 187 break;
183 188
184 case 12000: 189 case 12000:
185 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 12000); 190 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 12000);
186 break; 191 break;
187 192
188 case 16000: 193 case 16000:
189 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 16000); 194 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 16000);
190 break; 195 break;
191 196
192 case 22050: 197 case 22050:
193 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 22050); 198 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 22050);
194 break; 199 break;
195 200
196 case 24000: 201 case 24000:
197 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 24000); 202 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 24000);
198 break; 203 break;
199 204
200 case 32000: 205 case 32000:
201 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 32000); 206 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 32000);
202 break; 207 break;
203 208
204 case 44100: 209 case 44100:
205 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 44100); 210 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 44100);
206 break; 211 break;
207 212
208 case 48000: 213 case 48000:
209 timer->fraction = fraction * (MAD_TIMER_RESOLUTION / 48000); 214 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 48000);
210 break; 215 break;
211 216
212 default: 217 default:
213 timer->fraction = 218 timer->fraction = scale_rational(numer, denom, MAD_TIMER_RESOLUTION);
214 scale_rational(fraction, fracparts, MAD_TIMER_RESOLUTION);
215 break; 219 break;
216 } 220 }
217 221
218 if (timer->fraction >= MAD_TIMER_RESOLUTION) 222 if (timer->fraction >= MAD_TIMER_RESOLUTION)
219 reduce_timer(timer); 223 reduce_timer(timer);
220} 224}
221 225
222/* 226/*
223 * NAME:timer->add() 227 * NAME:timer->add()
224 * DESCRIPTION:add one timer to another 228 * DESCRIPTION:add one timer to another
225 */ 229 */
226void mad_timer_add(mad_timer_t *timer, mad_timer_t incr) 230void mad_timer_add(mad_timer_t *timer, mad_timer_t incr)
@@ -234,26 +238,26 @@ void mad_timer_add(mad_timer_t *timer, mad_timer_t incr)
234 238
235/* 239/*
236 * NAME:timer->multiply() 240 * NAME:timer->multiply()
237 * DESCRIPTION:multiply a timer by a scalar value 241 * DESCRIPTION:multiply a timer by a scalar value
238 */ 242 */
239void mad_timer_multiply(mad_timer_t *timer, signed long scalar) 243void mad_timer_multiply(mad_timer_t *timer, signed long scalar)
240{ 244{
241 mad_timer_t addend; 245 mad_timer_t addend;
242 unsigned long factor; 246 unsigned long factor;
243 247
244 factor = scalar; 248 factor = scalar;
245 if (scalar < 0) { 249 if (scalar < 0) {
246 mad_timer_negate(timer);
247 factor = -scalar; 250 factor = -scalar;
251 mad_timer_negate(timer);
248 } 252 }
249 253
250 addend = *timer; 254 addend = *timer;
251 *timer = mad_timer_zero; 255 *timer = mad_timer_zero;
252 256
253 while (factor) { 257 while (factor) {
254 if (factor & 1) 258 if (factor & 1)
255 mad_timer_add(timer, addend); 259 mad_timer_add(timer, addend);
256 260
257 mad_timer_add(&addend, addend); 261 mad_timer_add(&addend, addend);
258 factor >>= 1; 262 factor >>= 1;
259 } 263 }
@@ -308,37 +312,37 @@ signed long mad_timer_count(mad_timer_t timer, enum mad_units units)
308 case MAD_UNITS_59_94_FPS: 312 case MAD_UNITS_59_94_FPS:
309 return (mad_timer_count(timer, -units) + 1) * 1000 / 1001; 313 return (mad_timer_count(timer, -units) + 1) * 1000 / 1001;
310 } 314 }
311 315
312 /* unsupported units */ 316 /* unsupported units */
313 return 0; 317 return 0;
314} 318}
315 319
316/* 320/*
317 * NAME:timer->fraction() 321 * NAME:timer->fraction()
318 * DESCRIPTION:return fractional part of timer in arbitrary terms 322 * DESCRIPTION:return fractional part of timer in arbitrary terms
319 */ 323 */
320unsigned long mad_timer_fraction(mad_timer_t timer, unsigned long fracparts) 324unsigned long mad_timer_fraction(mad_timer_t timer, unsigned long denom)
321{ 325{
322 timer = mad_timer_abs(timer); 326 timer = mad_timer_abs(timer);
323 327
324 switch (fracparts) { 328 switch (denom) {
325 case 0: 329 case 0:
326 return MAD_TIMER_RESOLUTION / timer.fraction; 330 return MAD_TIMER_RESOLUTION / timer.fraction;
327 331
328 case MAD_TIMER_RESOLUTION: 332 case MAD_TIMER_RESOLUTION:
329 return timer.fraction; 333 return timer.fraction;
330 334
331 default: 335 default:
332 return scale_rational(timer.fraction, MAD_TIMER_RESOLUTION, fracparts); 336 return scale_rational(timer.fraction, MAD_TIMER_RESOLUTION, denom);
333 } 337 }
334} 338}
335 339
336/* 340/*
337 * NAME:timer->string() 341 * NAME:timer->string()
338 * DESCRIPTION:write a string representation of a timer using a template 342 * DESCRIPTION:write a string representation of a timer using a template
339 */ 343 */
340void mad_timer_string(mad_timer_t timer, 344void mad_timer_string(mad_timer_t timer,
341 char *dest, char const *format, enum mad_units units, 345 char *dest, char const *format, enum mad_units units,
342 enum mad_units fracunits, unsigned long subparts) 346 enum mad_units fracunits, unsigned long subparts)
343{ 347{
344 unsigned long hours, minutes, seconds, sub; 348 unsigned long hours, minutes, seconds, sub;
@@ -368,30 +372,30 @@ void mad_timer_string(mad_timer_t timer,
368 case MAD_UNITS_32000_HZ: 372 case MAD_UNITS_32000_HZ:
369 case MAD_UNITS_44100_HZ: 373 case MAD_UNITS_44100_HZ:
370 case MAD_UNITS_48000_HZ: 374 case MAD_UNITS_48000_HZ:
371 375
372 case MAD_UNITS_24_FPS: 376 case MAD_UNITS_24_FPS:
373 case MAD_UNITS_25_FPS: 377 case MAD_UNITS_25_FPS:
374 case MAD_UNITS_30_FPS: 378 case MAD_UNITS_30_FPS:
375 case MAD_UNITS_48_FPS: 379 case MAD_UNITS_48_FPS:
376 case MAD_UNITS_50_FPS: 380 case MAD_UNITS_50_FPS:
377 case MAD_UNITS_60_FPS: 381 case MAD_UNITS_60_FPS:
378 case MAD_UNITS_75_FPS: 382 case MAD_UNITS_75_FPS:
379 { 383 {
380 unsigned long fracparts; 384 unsigned long denom;
381 385
382 fracparts = MAD_TIMER_RESOLUTION / fracunits; 386 denom = MAD_TIMER_RESOLUTION / fracunits;
383 387
384 frac = timer.fraction / fracparts; 388 frac = timer.fraction / denom;
385 sub = scale_rational(timer.fraction % fracparts, fracparts, subparts); 389 sub = scale_rational(timer.fraction % denom, denom, subparts);
386 } 390 }
387 break; 391 break;
388 392
389 case MAD_UNITS_23_976_FPS: 393 case MAD_UNITS_23_976_FPS:
390 case MAD_UNITS_24_975_FPS: 394 case MAD_UNITS_24_975_FPS:
391 case MAD_UNITS_29_97_FPS: 395 case MAD_UNITS_29_97_FPS:
392 case MAD_UNITS_47_952_FPS: 396 case MAD_UNITS_47_952_FPS:
393 case MAD_UNITS_49_95_FPS: 397 case MAD_UNITS_49_95_FPS:
394 case MAD_UNITS_59_94_FPS: 398 case MAD_UNITS_59_94_FPS:
395 /* drop-frame encoding */ 399 /* drop-frame encoding */
396 /* N.B. this is only well-defined for MAD_UNITS_29_97_FPS */ 400 /* N.B. this is only well-defined for MAD_UNITS_29_97_FPS */
397 { 401 {