annotate src/libmad-0.15.1b/timer.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 545efbb81310
children
rev   line source
cannam@85 1 /*
cannam@85 2 * libmad - MPEG audio decoder library
cannam@85 3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
cannam@85 4 *
cannam@85 5 * This program is free software; you can redistribute it and/or modify
cannam@85 6 * it under the terms of the GNU General Public License as published by
cannam@85 7 * the Free Software Foundation; either version 2 of the License, or
cannam@85 8 * (at your option) any later version.
cannam@85 9 *
cannam@85 10 * This program is distributed in the hope that it will be useful,
cannam@85 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@85 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@85 13 * GNU General Public License for more details.
cannam@85 14 *
cannam@85 15 * You should have received a copy of the GNU General Public License
cannam@85 16 * along with this program; if not, write to the Free Software
cannam@85 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
cannam@85 18 *
cannam@85 19 * $Id: timer.c,v 1.18 2004/01/23 09:41:33 rob Exp $
cannam@85 20 */
cannam@85 21
cannam@85 22 # ifdef HAVE_CONFIG_H
cannam@85 23 # include "config.h"
cannam@85 24 # endif
cannam@85 25
cannam@85 26 # include "global.h"
cannam@85 27
cannam@85 28 # include <stdio.h>
cannam@85 29
cannam@85 30 # ifdef HAVE_ASSERT_H
cannam@85 31 # include <assert.h>
cannam@85 32 # endif
cannam@85 33
cannam@85 34 # include "timer.h"
cannam@85 35
cannam@85 36 mad_timer_t const mad_timer_zero = { 0, 0 };
cannam@85 37
cannam@85 38 /*
cannam@85 39 * NAME: timer->compare()
cannam@85 40 * DESCRIPTION: indicate relative order of two timers
cannam@85 41 */
cannam@85 42 int mad_timer_compare(mad_timer_t timer1, mad_timer_t timer2)
cannam@85 43 {
cannam@85 44 signed long diff;
cannam@85 45
cannam@85 46 diff = timer1.seconds - timer2.seconds;
cannam@85 47 if (diff < 0)
cannam@85 48 return -1;
cannam@85 49 else if (diff > 0)
cannam@85 50 return +1;
cannam@85 51
cannam@85 52 diff = timer1.fraction - timer2.fraction;
cannam@85 53 if (diff < 0)
cannam@85 54 return -1;
cannam@85 55 else if (diff > 0)
cannam@85 56 return +1;
cannam@85 57
cannam@85 58 return 0;
cannam@85 59 }
cannam@85 60
cannam@85 61 /*
cannam@85 62 * NAME: timer->negate()
cannam@85 63 * DESCRIPTION: invert the sign of a timer
cannam@85 64 */
cannam@85 65 void mad_timer_negate(mad_timer_t *timer)
cannam@85 66 {
cannam@85 67 timer->seconds = -timer->seconds;
cannam@85 68
cannam@85 69 if (timer->fraction) {
cannam@85 70 timer->seconds -= 1;
cannam@85 71 timer->fraction = MAD_TIMER_RESOLUTION - timer->fraction;
cannam@85 72 }
cannam@85 73 }
cannam@85 74
cannam@85 75 /*
cannam@85 76 * NAME: timer->abs()
cannam@85 77 * DESCRIPTION: return the absolute value of a timer
cannam@85 78 */
cannam@85 79 mad_timer_t mad_timer_abs(mad_timer_t timer)
cannam@85 80 {
cannam@85 81 if (timer.seconds < 0)
cannam@85 82 mad_timer_negate(&timer);
cannam@85 83
cannam@85 84 return timer;
cannam@85 85 }
cannam@85 86
cannam@85 87 /*
cannam@85 88 * NAME: reduce_timer()
cannam@85 89 * DESCRIPTION: carry timer fraction into seconds
cannam@85 90 */
cannam@85 91 static
cannam@85 92 void reduce_timer(mad_timer_t *timer)
cannam@85 93 {
cannam@85 94 timer->seconds += timer->fraction / MAD_TIMER_RESOLUTION;
cannam@85 95 timer->fraction %= MAD_TIMER_RESOLUTION;
cannam@85 96 }
cannam@85 97
cannam@85 98 /*
cannam@85 99 * NAME: gcd()
cannam@85 100 * DESCRIPTION: compute greatest common denominator
cannam@85 101 */
cannam@85 102 static
cannam@85 103 unsigned long gcd(unsigned long num1, unsigned long num2)
cannam@85 104 {
cannam@85 105 unsigned long tmp;
cannam@85 106
cannam@85 107 while (num2) {
cannam@85 108 tmp = num2;
cannam@85 109 num2 = num1 % num2;
cannam@85 110 num1 = tmp;
cannam@85 111 }
cannam@85 112
cannam@85 113 return num1;
cannam@85 114 }
cannam@85 115
cannam@85 116 /*
cannam@85 117 * NAME: reduce_rational()
cannam@85 118 * DESCRIPTION: convert rational expression to lowest terms
cannam@85 119 */
cannam@85 120 static
cannam@85 121 void reduce_rational(unsigned long *numer, unsigned long *denom)
cannam@85 122 {
cannam@85 123 unsigned long factor;
cannam@85 124
cannam@85 125 factor = gcd(*numer, *denom);
cannam@85 126
cannam@85 127 assert(factor != 0);
cannam@85 128
cannam@85 129 *numer /= factor;
cannam@85 130 *denom /= factor;
cannam@85 131 }
cannam@85 132
cannam@85 133 /*
cannam@85 134 * NAME: scale_rational()
cannam@85 135 * DESCRIPTION: solve numer/denom == ?/scale avoiding overflowing
cannam@85 136 */
cannam@85 137 static
cannam@85 138 unsigned long scale_rational(unsigned long numer, unsigned long denom,
cannam@85 139 unsigned long scale)
cannam@85 140 {
cannam@85 141 reduce_rational(&numer, &denom);
cannam@85 142 reduce_rational(&scale, &denom);
cannam@85 143
cannam@85 144 assert(denom != 0);
cannam@85 145
cannam@85 146 if (denom < scale)
cannam@85 147 return numer * (scale / denom) + numer * (scale % denom) / denom;
cannam@85 148 if (denom < numer)
cannam@85 149 return scale * (numer / denom) + scale * (numer % denom) / denom;
cannam@85 150
cannam@85 151 return numer * scale / denom;
cannam@85 152 }
cannam@85 153
cannam@85 154 /*
cannam@85 155 * NAME: timer->set()
cannam@85 156 * DESCRIPTION: set timer to specific (positive) value
cannam@85 157 */
cannam@85 158 void mad_timer_set(mad_timer_t *timer, unsigned long seconds,
cannam@85 159 unsigned long numer, unsigned long denom)
cannam@85 160 {
cannam@85 161 timer->seconds = seconds;
cannam@85 162 if (numer >= denom && denom > 0) {
cannam@85 163 timer->seconds += numer / denom;
cannam@85 164 numer %= denom;
cannam@85 165 }
cannam@85 166
cannam@85 167 switch (denom) {
cannam@85 168 case 0:
cannam@85 169 case 1:
cannam@85 170 timer->fraction = 0;
cannam@85 171 break;
cannam@85 172
cannam@85 173 case MAD_TIMER_RESOLUTION:
cannam@85 174 timer->fraction = numer;
cannam@85 175 break;
cannam@85 176
cannam@85 177 case 1000:
cannam@85 178 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 1000);
cannam@85 179 break;
cannam@85 180
cannam@85 181 case 8000:
cannam@85 182 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 8000);
cannam@85 183 break;
cannam@85 184
cannam@85 185 case 11025:
cannam@85 186 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 11025);
cannam@85 187 break;
cannam@85 188
cannam@85 189 case 12000:
cannam@85 190 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 12000);
cannam@85 191 break;
cannam@85 192
cannam@85 193 case 16000:
cannam@85 194 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 16000);
cannam@85 195 break;
cannam@85 196
cannam@85 197 case 22050:
cannam@85 198 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 22050);
cannam@85 199 break;
cannam@85 200
cannam@85 201 case 24000:
cannam@85 202 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 24000);
cannam@85 203 break;
cannam@85 204
cannam@85 205 case 32000:
cannam@85 206 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 32000);
cannam@85 207 break;
cannam@85 208
cannam@85 209 case 44100:
cannam@85 210 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 44100);
cannam@85 211 break;
cannam@85 212
cannam@85 213 case 48000:
cannam@85 214 timer->fraction = numer * (MAD_TIMER_RESOLUTION / 48000);
cannam@85 215 break;
cannam@85 216
cannam@85 217 default:
cannam@85 218 timer->fraction = scale_rational(numer, denom, MAD_TIMER_RESOLUTION);
cannam@85 219 break;
cannam@85 220 }
cannam@85 221
cannam@85 222 if (timer->fraction >= MAD_TIMER_RESOLUTION)
cannam@85 223 reduce_timer(timer);
cannam@85 224 }
cannam@85 225
cannam@85 226 /*
cannam@85 227 * NAME: timer->add()
cannam@85 228 * DESCRIPTION: add one timer to another
cannam@85 229 */
cannam@85 230 void mad_timer_add(mad_timer_t *timer, mad_timer_t incr)
cannam@85 231 {
cannam@85 232 timer->seconds += incr.seconds;
cannam@85 233 timer->fraction += incr.fraction;
cannam@85 234
cannam@85 235 if (timer->fraction >= MAD_TIMER_RESOLUTION)
cannam@85 236 reduce_timer(timer);
cannam@85 237 }
cannam@85 238
cannam@85 239 /*
cannam@85 240 * NAME: timer->multiply()
cannam@85 241 * DESCRIPTION: multiply a timer by a scalar value
cannam@85 242 */
cannam@85 243 void mad_timer_multiply(mad_timer_t *timer, signed long scalar)
cannam@85 244 {
cannam@85 245 mad_timer_t addend;
cannam@85 246 unsigned long factor;
cannam@85 247
cannam@85 248 factor = scalar;
cannam@85 249 if (scalar < 0) {
cannam@85 250 factor = -scalar;
cannam@85 251 mad_timer_negate(timer);
cannam@85 252 }
cannam@85 253
cannam@85 254 addend = *timer;
cannam@85 255 *timer = mad_timer_zero;
cannam@85 256
cannam@85 257 while (factor) {
cannam@85 258 if (factor & 1)
cannam@85 259 mad_timer_add(timer, addend);
cannam@85 260
cannam@85 261 mad_timer_add(&addend, addend);
cannam@85 262 factor >>= 1;
cannam@85 263 }
cannam@85 264 }
cannam@85 265
cannam@85 266 /*
cannam@85 267 * NAME: timer->count()
cannam@85 268 * DESCRIPTION: return timer value in selected units
cannam@85 269 */
cannam@85 270 signed long mad_timer_count(mad_timer_t timer, enum mad_units units)
cannam@85 271 {
cannam@85 272 switch (units) {
cannam@85 273 case MAD_UNITS_HOURS:
cannam@85 274 return timer.seconds / 60 / 60;
cannam@85 275
cannam@85 276 case MAD_UNITS_MINUTES:
cannam@85 277 return timer.seconds / 60;
cannam@85 278
cannam@85 279 case MAD_UNITS_SECONDS:
cannam@85 280 return timer.seconds;
cannam@85 281
cannam@85 282 case MAD_UNITS_DECISECONDS:
cannam@85 283 case MAD_UNITS_CENTISECONDS:
cannam@85 284 case MAD_UNITS_MILLISECONDS:
cannam@85 285
cannam@85 286 case MAD_UNITS_8000_HZ:
cannam@85 287 case MAD_UNITS_11025_HZ:
cannam@85 288 case MAD_UNITS_12000_HZ:
cannam@85 289 case MAD_UNITS_16000_HZ:
cannam@85 290 case MAD_UNITS_22050_HZ:
cannam@85 291 case MAD_UNITS_24000_HZ:
cannam@85 292 case MAD_UNITS_32000_HZ:
cannam@85 293 case MAD_UNITS_44100_HZ:
cannam@85 294 case MAD_UNITS_48000_HZ:
cannam@85 295
cannam@85 296 case MAD_UNITS_24_FPS:
cannam@85 297 case MAD_UNITS_25_FPS:
cannam@85 298 case MAD_UNITS_30_FPS:
cannam@85 299 case MAD_UNITS_48_FPS:
cannam@85 300 case MAD_UNITS_50_FPS:
cannam@85 301 case MAD_UNITS_60_FPS:
cannam@85 302 case MAD_UNITS_75_FPS:
cannam@85 303 return timer.seconds * (signed long) units +
cannam@85 304 (signed long) scale_rational(timer.fraction, MAD_TIMER_RESOLUTION,
cannam@85 305 units);
cannam@85 306
cannam@85 307 case MAD_UNITS_23_976_FPS:
cannam@85 308 case MAD_UNITS_24_975_FPS:
cannam@85 309 case MAD_UNITS_29_97_FPS:
cannam@85 310 case MAD_UNITS_47_952_FPS:
cannam@85 311 case MAD_UNITS_49_95_FPS:
cannam@85 312 case MAD_UNITS_59_94_FPS:
cannam@85 313 return (mad_timer_count(timer, -units) + 1) * 1000 / 1001;
cannam@85 314 }
cannam@85 315
cannam@85 316 /* unsupported units */
cannam@85 317 return 0;
cannam@85 318 }
cannam@85 319
cannam@85 320 /*
cannam@85 321 * NAME: timer->fraction()
cannam@85 322 * DESCRIPTION: return fractional part of timer in arbitrary terms
cannam@85 323 */
cannam@85 324 unsigned long mad_timer_fraction(mad_timer_t timer, unsigned long denom)
cannam@85 325 {
cannam@85 326 timer = mad_timer_abs(timer);
cannam@85 327
cannam@85 328 switch (denom) {
cannam@85 329 case 0:
cannam@85 330 return timer.fraction ?
cannam@85 331 MAD_TIMER_RESOLUTION / timer.fraction : MAD_TIMER_RESOLUTION + 1;
cannam@85 332
cannam@85 333 case MAD_TIMER_RESOLUTION:
cannam@85 334 return timer.fraction;
cannam@85 335
cannam@85 336 default:
cannam@85 337 return scale_rational(timer.fraction, MAD_TIMER_RESOLUTION, denom);
cannam@85 338 }
cannam@85 339 }
cannam@85 340
cannam@85 341 /*
cannam@85 342 * NAME: timer->string()
cannam@85 343 * DESCRIPTION: write a string representation of a timer using a template
cannam@85 344 */
cannam@85 345 void mad_timer_string(mad_timer_t timer,
cannam@85 346 char *dest, char const *format, enum mad_units units,
cannam@85 347 enum mad_units fracunits, unsigned long subparts)
cannam@85 348 {
cannam@85 349 unsigned long hours, minutes, seconds, sub;
cannam@85 350 unsigned int frac;
cannam@85 351
cannam@85 352 timer = mad_timer_abs(timer);
cannam@85 353
cannam@85 354 seconds = timer.seconds;
cannam@85 355 frac = sub = 0;
cannam@85 356
cannam@85 357 switch (fracunits) {
cannam@85 358 case MAD_UNITS_HOURS:
cannam@85 359 case MAD_UNITS_MINUTES:
cannam@85 360 case MAD_UNITS_SECONDS:
cannam@85 361 break;
cannam@85 362
cannam@85 363 case MAD_UNITS_DECISECONDS:
cannam@85 364 case MAD_UNITS_CENTISECONDS:
cannam@85 365 case MAD_UNITS_MILLISECONDS:
cannam@85 366
cannam@85 367 case MAD_UNITS_8000_HZ:
cannam@85 368 case MAD_UNITS_11025_HZ:
cannam@85 369 case MAD_UNITS_12000_HZ:
cannam@85 370 case MAD_UNITS_16000_HZ:
cannam@85 371 case MAD_UNITS_22050_HZ:
cannam@85 372 case MAD_UNITS_24000_HZ:
cannam@85 373 case MAD_UNITS_32000_HZ:
cannam@85 374 case MAD_UNITS_44100_HZ:
cannam@85 375 case MAD_UNITS_48000_HZ:
cannam@85 376
cannam@85 377 case MAD_UNITS_24_FPS:
cannam@85 378 case MAD_UNITS_25_FPS:
cannam@85 379 case MAD_UNITS_30_FPS:
cannam@85 380 case MAD_UNITS_48_FPS:
cannam@85 381 case MAD_UNITS_50_FPS:
cannam@85 382 case MAD_UNITS_60_FPS:
cannam@85 383 case MAD_UNITS_75_FPS:
cannam@85 384 {
cannam@85 385 unsigned long denom;
cannam@85 386
cannam@85 387 denom = MAD_TIMER_RESOLUTION / fracunits;
cannam@85 388
cannam@85 389 frac = timer.fraction / denom;
cannam@85 390 sub = scale_rational(timer.fraction % denom, denom, subparts);
cannam@85 391 }
cannam@85 392 break;
cannam@85 393
cannam@85 394 case MAD_UNITS_23_976_FPS:
cannam@85 395 case MAD_UNITS_24_975_FPS:
cannam@85 396 case MAD_UNITS_29_97_FPS:
cannam@85 397 case MAD_UNITS_47_952_FPS:
cannam@85 398 case MAD_UNITS_49_95_FPS:
cannam@85 399 case MAD_UNITS_59_94_FPS:
cannam@85 400 /* drop-frame encoding */
cannam@85 401 /* N.B. this is only well-defined for MAD_UNITS_29_97_FPS */
cannam@85 402 {
cannam@85 403 unsigned long frame, cycle, d, m;
cannam@85 404
cannam@85 405 frame = mad_timer_count(timer, fracunits);
cannam@85 406
cannam@85 407 cycle = -fracunits * 60 * 10 - (10 - 1) * 2;
cannam@85 408
cannam@85 409 d = frame / cycle;
cannam@85 410 m = frame % cycle;
cannam@85 411 frame += (10 - 1) * 2 * d;
cannam@85 412 if (m > 2)
cannam@85 413 frame += 2 * ((m - 2) / (cycle / 10));
cannam@85 414
cannam@85 415 frac = frame % -fracunits;
cannam@85 416 seconds = frame / -fracunits;
cannam@85 417 }
cannam@85 418 break;
cannam@85 419 }
cannam@85 420
cannam@85 421 switch (units) {
cannam@85 422 case MAD_UNITS_HOURS:
cannam@85 423 minutes = seconds / 60;
cannam@85 424 hours = minutes / 60;
cannam@85 425
cannam@85 426 sprintf(dest, format,
cannam@85 427 hours,
cannam@85 428 (unsigned int) (minutes % 60),
cannam@85 429 (unsigned int) (seconds % 60),
cannam@85 430 frac, sub);
cannam@85 431 break;
cannam@85 432
cannam@85 433 case MAD_UNITS_MINUTES:
cannam@85 434 minutes = seconds / 60;
cannam@85 435
cannam@85 436 sprintf(dest, format,
cannam@85 437 minutes,
cannam@85 438 (unsigned int) (seconds % 60),
cannam@85 439 frac, sub);
cannam@85 440 break;
cannam@85 441
cannam@85 442 case MAD_UNITS_SECONDS:
cannam@85 443 sprintf(dest, format,
cannam@85 444 seconds,
cannam@85 445 frac, sub);
cannam@85 446 break;
cannam@85 447
cannam@85 448 case MAD_UNITS_23_976_FPS:
cannam@85 449 case MAD_UNITS_24_975_FPS:
cannam@85 450 case MAD_UNITS_29_97_FPS:
cannam@85 451 case MAD_UNITS_47_952_FPS:
cannam@85 452 case MAD_UNITS_49_95_FPS:
cannam@85 453 case MAD_UNITS_59_94_FPS:
cannam@85 454 if (fracunits < 0) {
cannam@85 455 /* not yet implemented */
cannam@85 456 sub = 0;
cannam@85 457 }
cannam@85 458
cannam@85 459 /* fall through */
cannam@85 460
cannam@85 461 case MAD_UNITS_DECISECONDS:
cannam@85 462 case MAD_UNITS_CENTISECONDS:
cannam@85 463 case MAD_UNITS_MILLISECONDS:
cannam@85 464
cannam@85 465 case MAD_UNITS_8000_HZ:
cannam@85 466 case MAD_UNITS_11025_HZ:
cannam@85 467 case MAD_UNITS_12000_HZ:
cannam@85 468 case MAD_UNITS_16000_HZ:
cannam@85 469 case MAD_UNITS_22050_HZ:
cannam@85 470 case MAD_UNITS_24000_HZ:
cannam@85 471 case MAD_UNITS_32000_HZ:
cannam@85 472 case MAD_UNITS_44100_HZ:
cannam@85 473 case MAD_UNITS_48000_HZ:
cannam@85 474
cannam@85 475 case MAD_UNITS_24_FPS:
cannam@85 476 case MAD_UNITS_25_FPS:
cannam@85 477 case MAD_UNITS_30_FPS:
cannam@85 478 case MAD_UNITS_48_FPS:
cannam@85 479 case MAD_UNITS_50_FPS:
cannam@85 480 case MAD_UNITS_60_FPS:
cannam@85 481 case MAD_UNITS_75_FPS:
cannam@85 482 sprintf(dest, format, mad_timer_count(timer, units), sub);
cannam@85 483 break;
cannam@85 484 }
cannam@85 485 }