hermite.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: hermite.c 4115 2012-04-12 21:06:13Z olereinhardt $
  3. *
  4. * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * Alternatively, the contents of this file may be used under the
  21. * terms of the GNU General Public License Version 2 or later (the
  22. * "GPL") in which case the provisions of the GPL are applicable
  23. * instead of those above. If you wish to allow use of your version of
  24. * this file only under the terms of the GPL, and not to allow others
  25. * to use your version of this file under the terms of either the RPSL
  26. * or RCSL, indicate your decision by deleting the provisions above
  27. * and replace them with the notice and other provisions required by
  28. * the GPL. If you do not delete the provisions above, a recipient may
  29. * use your version of this file under the terms of any one of the
  30. * RPSL, the RCSL or the GPL.
  31. *
  32. * This file is part of the Helix DNA Technology. RealNetworks is the
  33. * developer of the Original Code and owns the copyrights in the
  34. * portions it created.
  35. *
  36. * This file, and the files included with this file, is distributed
  37. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41. * ENJOYMENT OR NON-INFRINGEMENT.
  42. *
  43. * Technology Compatibility Kit Test Suite(s) Location:
  44. * http://www.helixcommunity.org/content/tck
  45. *
  46. * Contributor(s):
  47. *
  48. * ***** END LICENSE BLOCK ***** */
  49. /*
  50. * Sampling rate conversion, by polynomial interpolation.
  51. * Ken Cooke (kenc@real.com)
  52. */
  53. #include <stdlib.h>
  54. #include <memdebug.h>
  55. #include <contrib/hxmp3/hermite.h>
  56. #define MAXRATE ((1<<23) - 1) /* sampling rates cannot exceed MAXRATE */
  57. #define MAXSAMPS ((1<<23) - 1) /* max outsamps for GetMinInput() */
  58. #define MAXCHANS 2
  59. typedef long long SymInt64;
  60. static __inline__ int MulShift31(int x, int y)
  61. {
  62. SymInt64 a = x;
  63. SymInt64 b = y;
  64. a *= b;
  65. a >>= 31;
  66. return (int)a;
  67. }
  68. /* interpolator state */
  69. typedef struct {
  70. int inrate;
  71. int outrate;
  72. int nchans;
  73. int time_i;
  74. unsigned int time_f;
  75. unsigned int step_i;
  76. unsigned int step_f;
  77. short hist[3*MAXCHANS]; /* input history */
  78. } STATE;
  79. /* Initialize Hermite resampler
  80. *
  81. * Parameters
  82. * ----------
  83. * int inrate sample rate of input (Hz)
  84. * int outrate desired sample rate of output (Hz)
  85. * int nchans number of channels
  86. *
  87. * return value instance pointer which will be passed in all future RAXXXHermite() function calls, 0 if error
  88. *
  89. * Notes
  90. * -----
  91. * - inrate, outrate, nchans must be within valid ranges (see below)
  92. * - inrate < outrate (i.e. upsampling only!)
  93. */
  94. void *
  95. RAInitResamplerHermite(int inrate, int outrate, int nchans)
  96. {
  97. STATE *s;
  98. unsigned int step_i, step_f, ratio, rem;
  99. int i;
  100. /* validate params */
  101. if ((inrate <= 0) || (inrate > MAXRATE) ||
  102. (outrate <= 0) || (outrate > MAXRATE))
  103. return 0;
  104. /* only allow downsampling on certain platforms
  105. * until we have a fixed point resampler that can
  106. * downsample properly. */
  107. #ifndef _SYMBIAN
  108. /* XXXgfw remove this when the new resampler is available. */
  109. if( inrate > outrate )
  110. {
  111. return 0;
  112. }
  113. #endif
  114. if ((nchans < 1) || (nchans > MAXCHANS))
  115. return 0;
  116. /* create interpolator state */
  117. s = (STATE *) malloc(sizeof(STATE));
  118. if (!s)
  119. return 0;
  120. /* Compute 64-bit timestep, as a signed integer and 32-bit fraction */
  121. step_i = inrate / outrate; /* integer part */
  122. rem = inrate;
  123. step_f = 0 ;
  124. for (i = 0; i < 4; i++) {
  125. rem <<= 8;
  126. ratio = rem / outrate;
  127. rem -= ratio * outrate;
  128. step_f = (step_f << 8) | (ratio & 0xff); /* 8 more fraction bits */
  129. }
  130. s->inrate = inrate;
  131. s->outrate = outrate;
  132. s->nchans = nchans;
  133. s->time_i = 0;
  134. s->time_f = 0;
  135. s->step_i = step_i;
  136. s->step_f = step_f;
  137. for (i = 0; i < (3*MAXCHANS); i++)
  138. s->hist[i] = 0;
  139. return (void *)s;
  140. }
  141. /* Free memory associated with Hermite resampler
  142. *
  143. * Parameters
  144. * ----------
  145. * void *inst instance pointer
  146. *
  147. * return value none
  148. *
  149. * Notes
  150. * -----
  151. */
  152. void
  153. RAFreeResamplerHermite(void *inst)
  154. {
  155. STATE *s = (STATE *)inst;
  156. free(s);
  157. }
  158. /* Get max possible outsamps given insamps input
  159. *
  160. * Parameters
  161. * ----------
  162. * int insamps number of input samples
  163. * void *inst instance pointer
  164. *
  165. * return value maximum number of output samples generated by resampling insamps samples, -1 if error
  166. *
  167. * Notes
  168. * -----
  169. * - some alternate implementations are included as comments
  170. * these might be useful, depending on the target platform
  171. * - insamps must be even for stereo, function will exit with error if not
  172. */
  173. int RAGetMaxOutputHermite(int insamps, void *inst)
  174. {
  175. /* do an empty (null) resample of insamps samples */
  176. int inframes, outframes;
  177. unsigned int i, f;
  178. STATE *s = (STATE *)inst;
  179. if (s->nchans == 2 && insamps & 0x01)
  180. return -1;
  181. inframes = (s->nchans == 2 ? insamps >> 1 : insamps);
  182. for (i = f = outframes = 0; i < (unsigned int)inframes; outframes++) {
  183. f += s->step_f;
  184. i += s->step_i + (f < s->step_f); /* add with carry */
  185. }
  186. return (int)(outframes * s->nchans);
  187. /* equivalent method using __int64
  188. *
  189. * inframes = (s->nchans == 2 ? insamps >> 1 : insamps);
  190. * step64 = ((__int64)s->step_i << 32) + (__int64)s->step_f;
  191. * outframes = ( ((__int64)inframes << 32) + step64 - 1) / step64; COMMENT: ceiling
  192. * return (int)(outframes * s->nchans);
  193. */
  194. /* equivalent method using double-precision floats
  195. *
  196. * double step;
  197. * inframes = (s->nchans == 2 ? insamps >> 1 : insamps);
  198. * step = s->step_i + (s->step_f / 4294967296.0);
  199. * outframes = (int) ceil((double)inframes / step);
  200. * return (outframes * s->nchans);
  201. */
  202. }
  203. /* Get minimum number of input samples required to generate outsamps output samples
  204. *
  205. * Parameters
  206. * ----------
  207. * int outsamps number of desired output samples
  208. * void *inst instance pointer
  209. *
  210. * return value minimum number of input samples required to generate outsamps output samples, -1 if error
  211. *
  212. * Notes
  213. * -----
  214. * - some alternate implementations are included as comments
  215. * these might be useful, depending on the target platform
  216. * - outsamps must be even for stereo, function will exit with error if not
  217. */
  218. int
  219. RAGetMinInputHermite(int outsamps, void *inst)
  220. {
  221. unsigned int outframes;
  222. STATE *s = (STATE *)inst;
  223. unsigned int inframes, f, i;
  224. /* to ensure no overflow in multiply */
  225. if (outsamps > MAXSAMPS)
  226. return -1;
  227. if (s->nchans == 2 && outsamps & 0x01)
  228. return -1;
  229. outframes = (unsigned int)(s->nchans == 2 ? outsamps >> 1 : outsamps);
  230. inframes = 0;
  231. /* fractional part */
  232. f = s->step_f;
  233. for (i = 0; i < 4; i++) {
  234. inframes += outframes * (f & 0xff); /* add 24x8 partial product */
  235. inframes = (inframes + 0xff) >> 8; /* shift, rounding up */
  236. f >>= 8;
  237. }
  238. /* integer part */
  239. inframes += outframes * s->step_i;
  240. return (int)(inframes * s->nchans);
  241. /* equivalent method using __int64
  242. *
  243. * step64 = ((__int64)s->step_i << 32) + (__int64)s->step_f;
  244. * inframes = ( (__int64)outframes * step64);
  245. * inframes += (__int64)(0x00000000ffffffff); COMMENT: (add 1.0 - 2^-32 to 32.32 number)
  246. * return (int)((inframes >> 32) * s->nchans);
  247. */
  248. /* equivalent method using double-precision floats
  249. *
  250. * double step;
  251. * outframes = (s->nchans == 2 ? outsamps >> 1 : outsamps);
  252. * step = s->step_i + (s->step_f / 4294967296.0);
  253. * inframes = (int) ceil((double)outframes * step);
  254. * return (inframes * s->nchans);
  255. */
  256. /* equivalent method using an empty (null) resample
  257. *
  258. * outframes = (s->nchans == 2 ? outsamps >> 1 : outsamps);
  259. * for (i = f = currOut = 0; currOut < outframes; currOut++) {
  260. * f += s->step_f;
  261. * i += s->step_i + (f < s->step_f); COMMENT: add with carry
  262. * }
  263. * if (f) COMMENT: ceiling (if any fractional part, round up)
  264. * i++;
  265. * return (int)(i * s->nchans);
  266. */
  267. }
  268. /* Get number of frames of delay in the Hermite resampler
  269. *
  270. * Parameters
  271. * ----------
  272. * void *inst instance pointer
  273. *
  274. * return value frames of delay
  275. *
  276. * Notes
  277. * -----
  278. * - always two frames of delay (2 samples per channel)
  279. */
  280. int
  281. RAGetDelayHermite(void *inst)
  282. {
  283. return 2;
  284. }
  285. /* Cubic Hermite interpolation - one channel
  286. *
  287. * Parameters
  288. * ----------
  289. * void *inbuf pointer to buffer of input data (16-bit PCM)
  290. * int insamps number of samples in inbuf
  291. * cvtFunctionType cvt conversion function pointer, ignored
  292. * short *outbuf output buffer, must be large enough to hold RAGetMaxOutputHermite(insamps) samples
  293. * void *inst instance pointer
  294. *
  295. * return value number of output samples generated and placed in outbuf, -1 if error
  296. *
  297. * Notes
  298. * -----
  299. * - no restrictions on number of insamps
  300. * - inbuf MUST contain 16-bit PCM data, the cvt function is ignored
  301. */
  302. int
  303. RAResampleMonoHermite(void *inbuf, int insamps, short *outbuf, void *inst)
  304. {
  305. STATE *s = (STATE *)inst;
  306. unsigned int f, step_i, step_f;
  307. int outsamps, i, acc0;
  308. int x0, x1, x2, x3, frac;
  309. short *inptr;
  310. /* restore state */
  311. i = s->time_i;
  312. f = s->time_f;
  313. step_i = s->step_i;
  314. step_f = s->step_f;
  315. outsamps = 0;
  316. inptr = (short *)inbuf;
  317. if (s->nchans != 1)
  318. return -1;
  319. /* mono */
  320. while (i < insamps) {
  321. if (i < 3) {
  322. x3 = (i < 3 ? s->hist[i+0] : inptr[i-3]) << 12;
  323. x2 = (i < 2 ? s->hist[i+1] : inptr[i-2]) << 12;
  324. x1 = (i < 1 ? s->hist[i+2] : inptr[i-1]) << 12;
  325. } else {
  326. x3 = inptr[i-3] << 12;
  327. x2 = inptr[i-2] << 12;
  328. x1 = inptr[i-1] << 12;
  329. }
  330. x0 = inptr[i] << 12;
  331. frac = f >> 1;
  332. /* 4-tap Hermite, using Farrow structure */
  333. acc0 = (3 * (x2 - x1) + x0 - x3) >> 1;
  334. acc0 = MulShift31(acc0, frac);
  335. acc0 += 2 * x1 + x3 - ((5 * x2 + x0) >> 1);
  336. acc0 = MulShift31(acc0, frac);
  337. acc0 += (x1 - x3) >> 1;
  338. acc0 = MulShift31(acc0, frac);
  339. acc0 += x2;
  340. f += step_f;
  341. i += step_i + (f < step_f); /* add with carry */
  342. acc0 = (acc0 + (1<<11)) >> 12;
  343. if (acc0 > +32767) acc0 = +32767;
  344. if (acc0 < -32768) acc0 = -32768;
  345. outbuf[outsamps++] = (short)acc0;
  346. }
  347. /* save delay samples for next time (hist[0] = oldest, hist[2] = newest) */
  348. s->hist[0] = (insamps < 3 ? s->hist[insamps+0] : inptr[insamps-3]);
  349. s->hist[1] = (insamps < 2 ? s->hist[insamps+1] : inptr[insamps-2]);
  350. s->hist[2] = (insamps < 1 ? s->hist[insamps+2] : inptr[insamps-1]);
  351. /* save state */
  352. s->time_f = f;
  353. s->time_i = i - insamps;
  354. return outsamps;
  355. }
  356. /* Cubic Hermite interpolation - two channels
  357. *
  358. * Parameters
  359. * ----------
  360. * void *inbuf pointer to buffer of input data (16-bit PCM, interleaved LRLRLR...)
  361. * int insamps number of samples in inbuf
  362. * cvtFunctionType cvt conversion function pointer, ignored
  363. * short *outbuf output buffer, must be large enough to hold RAGetMaxOutputHermite(insamps) samples
  364. * void *inst instance pointer
  365. *
  366. * return value number of output samples generated and placed in outbuf, -1 if error
  367. *
  368. * Notes
  369. * -----
  370. * - no restrictions on number of insamps
  371. * - inbuf MUST contain 16-bit PCM data, the cvt function is ignored
  372. * - insamps must be even, function will exit with error if not
  373. */
  374. int
  375. RAResampleStereoHermite(void *inbuf, int insamps, short *outbuf, void *inst)
  376. {
  377. STATE *s = (STATE *)inst;
  378. unsigned int f, step_i, step_f;
  379. int outsamps, i, acc0, acc1, j;
  380. int x0, x1, x2, x3, frac;
  381. short *inptr;
  382. /* restore state */
  383. i = s->time_i;
  384. f = s->time_f;
  385. step_i = s->step_i;
  386. step_f = s->step_f;
  387. outsamps = 0;
  388. inptr = (short *)inbuf;
  389. /* fail if odd number of input samples */
  390. if (s->nchans != 2 || insamps & 0x01)
  391. return -1;
  392. /* stereo - assume insamps is even */
  393. insamps /= 2; /* number of stereo frames - consume samples two at a time */
  394. while (i < insamps) {
  395. frac = f >> 1;
  396. j = 2*i;
  397. /* left */
  398. if (i < 3) {
  399. x3 = (i < 3 ? s->hist[j+0] : inptr[j-6]) << 12;
  400. x2 = (i < 2 ? s->hist[j+2] : inptr[j-4]) << 12;
  401. x1 = (i < 1 ? s->hist[j+4] : inptr[j-2]) << 12;
  402. } else {
  403. x3 = inptr[j-6] << 12;
  404. x2 = inptr[j-4] << 12;
  405. x1 = inptr[j-2] << 12;
  406. }
  407. x0 = inptr[j] << 12;
  408. /* 4-tap Hermite, using Farrow structure */
  409. acc0 = (3 * (x2 - x1) + x0 - x3) >> 1;
  410. acc0 = MulShift31(acc0, frac);
  411. acc0 += 2 * x1 + x3 - ((5 * x2 + x0) >> 1);
  412. acc0 = MulShift31(acc0, frac);
  413. acc0 += (x1 - x3) >> 1;
  414. acc0 = MulShift31(acc0, frac);
  415. acc0 += x2;
  416. /* right */
  417. if (i < 3) {
  418. x3 = (i < 3 ? s->hist[j+1] : inptr[j-5]) << 12;
  419. x2 = (i < 2 ? s->hist[j+3] : inptr[j-3]) << 12;
  420. x1 = (i < 1 ? s->hist[j+5] : inptr[j-1]) << 12;
  421. } else {
  422. x3 = inptr[j-5] << 12;
  423. x2 = inptr[j-3] << 12;
  424. x1 = inptr[j-1] << 12;
  425. }
  426. x0 = inptr[j+1] << 12;
  427. /* 4-tap Hermite, using Farrow structure */
  428. acc1 = (3 * (x2 - x1) + x0 - x3) >> 1;
  429. acc1 = MulShift31(acc1, frac);
  430. acc1 += 2 * x1 + x3 - ((5 * x2 + x0) >> 1);
  431. acc1 = MulShift31(acc1, frac);
  432. acc1 += (x1 - x3) >> 1;
  433. acc1 = MulShift31(acc1, frac);
  434. acc1 += x2;
  435. f += step_f;
  436. i += step_i + (f < step_f); /* add with carry */
  437. acc0 = (acc0 + (1<<11)) >> 12;
  438. if (acc0 > +32767) acc0 = +32767;
  439. if (acc0 < -32768) acc0 = -32768;
  440. outbuf[outsamps++] = (short)acc0;
  441. acc1 = (acc1 + (1<<11)) >> 12;
  442. if (acc1 > +32767) acc1 = +32767;
  443. if (acc1 < -32768) acc1 = -32768;
  444. outbuf[outsamps++] = (short)acc1;
  445. }
  446. /* save delay samples for next time (hist[0] = oldest left, hist[1] = oldest right, ...) */
  447. s->hist[0] = (insamps < 3 ? s->hist[2*(insamps+0) + 0] : inptr[2*(insamps-3) + 0]);
  448. s->hist[2] = (insamps < 2 ? s->hist[2*(insamps+1) + 0] : inptr[2*(insamps-2) + 0]);
  449. s->hist[4] = (insamps < 1 ? s->hist[2*(insamps+2) + 0] : inptr[2*(insamps-1) + 0]);
  450. s->hist[1] = (insamps < 3 ? s->hist[2*(insamps+0) + 1] : inptr[2*(insamps-3) + 1]);
  451. s->hist[3] = (insamps < 2 ? s->hist[2*(insamps+1) + 1] : inptr[2*(insamps-2) + 1]);
  452. s->hist[5] = (insamps < 1 ? s->hist[2*(insamps+2) + 1] : inptr[2*(insamps-1) + 1]);
  453. /* save state */
  454. s->time_f = f;
  455. s->time_i = i - insamps;
  456. return outsamps;
  457. }