You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
12KB

  1. /*
  2. * imdct.c
  3. * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
  4. * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  5. *
  6. * The ifft algorithms in this file have been largely inspired by Dan
  7. * Bernstein's work, djbfft, available at http://cr.yp.to/djbfft.html
  8. *
  9. * This file is part of a52dec, a free ATSC A-52 stream decoder.
  10. * See http://liba52.sourceforge.net/ for updates.
  11. *
  12. * a52dec is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * a52dec is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include "config.h"
  27. #include <math.h>
  28. #include <stdio.h>
  29. #ifdef LIBA52_DJBFFT
  30. #include <fftc4.h>
  31. #endif
  32. #ifndef M_PI
  33. #define M_PI 3.1415926535897932384626433832795029
  34. #endif
  35. #include <inttypes.h>
  36. #include "a52.h"
  37. #include "a52_internal.h"
  38. #include "mm_accel.h"
  39. typedef struct complex_s {
  40. sample_t real;
  41. sample_t imag;
  42. } complex_t;
  43. static complex_t buf[128];
  44. static uint8_t fftorder[] = {
  45. 0,128, 64,192, 32,160,224, 96, 16,144, 80,208,240,112, 48,176,
  46. 8,136, 72,200, 40,168,232,104,248,120, 56,184, 24,152,216, 88,
  47. 4,132, 68,196, 36,164,228,100, 20,148, 84,212,244,116, 52,180,
  48. 252,124, 60,188, 28,156,220, 92, 12,140, 76,204,236,108, 44,172,
  49. 2,130, 66,194, 34,162,226, 98, 18,146, 82,210,242,114, 50,178,
  50. 10,138, 74,202, 42,170,234,106,250,122, 58,186, 26,154,218, 90,
  51. 254,126, 62,190, 30,158,222, 94, 14,142, 78,206,238,110, 46,174,
  52. 6,134, 70,198, 38,166,230,102,246,118, 54,182, 22,150,214, 86
  53. };
  54. /* Root values for IFFT */
  55. static sample_t roots16[3];
  56. static sample_t roots32[7];
  57. static sample_t roots64[15];
  58. static sample_t roots128[31];
  59. /* Twiddle factors for IMDCT */
  60. static complex_t pre1[128];
  61. static complex_t post1[64];
  62. static complex_t pre2[64];
  63. static complex_t post2[32];
  64. static sample_t a52_imdct_window[256];
  65. static void (* ifft128) (complex_t * buf);
  66. static void (* ifft64) (complex_t * buf);
  67. static inline void ifft2 (complex_t * buf)
  68. {
  69. double r, i;
  70. r = buf[0].real;
  71. i = buf[0].imag;
  72. buf[0].real += buf[1].real;
  73. buf[0].imag += buf[1].imag;
  74. buf[1].real = r - buf[1].real;
  75. buf[1].imag = i - buf[1].imag;
  76. }
  77. static inline void ifft4 (complex_t * buf)
  78. {
  79. double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  80. tmp1 = buf[0].real + buf[1].real;
  81. tmp2 = buf[3].real + buf[2].real;
  82. tmp3 = buf[0].imag + buf[1].imag;
  83. tmp4 = buf[2].imag + buf[3].imag;
  84. tmp5 = buf[0].real - buf[1].real;
  85. tmp6 = buf[0].imag - buf[1].imag;
  86. tmp7 = buf[2].imag - buf[3].imag;
  87. tmp8 = buf[3].real - buf[2].real;
  88. buf[0].real = tmp1 + tmp2;
  89. buf[0].imag = tmp3 + tmp4;
  90. buf[2].real = tmp1 - tmp2;
  91. buf[2].imag = tmp3 - tmp4;
  92. buf[1].real = tmp5 + tmp7;
  93. buf[1].imag = tmp6 + tmp8;
  94. buf[3].real = tmp5 - tmp7;
  95. buf[3].imag = tmp6 - tmp8;
  96. }
  97. /* the basic split-radix ifft butterfly */
  98. #define BUTTERFLY(a0,a1,a2,a3,wr,wi) do { \
  99. tmp5 = a2.real * wr + a2.imag * wi; \
  100. tmp6 = a2.imag * wr - a2.real * wi; \
  101. tmp7 = a3.real * wr - a3.imag * wi; \
  102. tmp8 = a3.imag * wr + a3.real * wi; \
  103. tmp1 = tmp5 + tmp7; \
  104. tmp2 = tmp6 + tmp8; \
  105. tmp3 = tmp6 - tmp8; \
  106. tmp4 = tmp7 - tmp5; \
  107. a2.real = a0.real - tmp1; \
  108. a2.imag = a0.imag - tmp2; \
  109. a3.real = a1.real - tmp3; \
  110. a3.imag = a1.imag - tmp4; \
  111. a0.real += tmp1; \
  112. a0.imag += tmp2; \
  113. a1.real += tmp3; \
  114. a1.imag += tmp4; \
  115. } while (0)
  116. /* split-radix ifft butterfly, specialized for wr=1 wi=0 */
  117. #define BUTTERFLY_ZERO(a0,a1,a2,a3) do { \
  118. tmp1 = a2.real + a3.real; \
  119. tmp2 = a2.imag + a3.imag; \
  120. tmp3 = a2.imag - a3.imag; \
  121. tmp4 = a3.real - a2.real; \
  122. a2.real = a0.real - tmp1; \
  123. a2.imag = a0.imag - tmp2; \
  124. a3.real = a1.real - tmp3; \
  125. a3.imag = a1.imag - tmp4; \
  126. a0.real += tmp1; \
  127. a0.imag += tmp2; \
  128. a1.real += tmp3; \
  129. a1.imag += tmp4; \
  130. } while (0)
  131. /* split-radix ifft butterfly, specialized for wr=wi */
  132. #define BUTTERFLY_HALF(a0,a1,a2,a3,w) do { \
  133. tmp5 = (a2.real + a2.imag) * w; \
  134. tmp6 = (a2.imag - a2.real) * w; \
  135. tmp7 = (a3.real - a3.imag) * w; \
  136. tmp8 = (a3.imag + a3.real) * w; \
  137. tmp1 = tmp5 + tmp7; \
  138. tmp2 = tmp6 + tmp8; \
  139. tmp3 = tmp6 - tmp8; \
  140. tmp4 = tmp7 - tmp5; \
  141. a2.real = a0.real - tmp1; \
  142. a2.imag = a0.imag - tmp2; \
  143. a3.real = a1.real - tmp3; \
  144. a3.imag = a1.imag - tmp4; \
  145. a0.real += tmp1; \
  146. a0.imag += tmp2; \
  147. a1.real += tmp3; \
  148. a1.imag += tmp4; \
  149. } while (0)
  150. static inline void ifft8 (complex_t * buf)
  151. {
  152. double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  153. ifft4 (buf);
  154. ifft2 (buf + 4);
  155. ifft2 (buf + 6);
  156. BUTTERFLY_ZERO (buf[0], buf[2], buf[4], buf[6]);
  157. BUTTERFLY_HALF (buf[1], buf[3], buf[5], buf[7], roots16[1]);
  158. }
  159. static void ifft_pass (complex_t * buf, sample_t * weight, int n)
  160. {
  161. complex_t * buf1;
  162. complex_t * buf2;
  163. complex_t * buf3;
  164. double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  165. int i;
  166. buf++;
  167. buf1 = buf + n;
  168. buf2 = buf + 2 * n;
  169. buf3 = buf + 3 * n;
  170. BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
  171. i = n - 1;
  172. do {
  173. BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0], weight[n], weight[2*i]);
  174. buf++;
  175. buf1++;
  176. buf2++;
  177. buf3++;
  178. weight++;
  179. } while (--i);
  180. }
  181. static void ifft16 (complex_t * buf)
  182. {
  183. ifft8 (buf);
  184. ifft4 (buf + 8);
  185. ifft4 (buf + 12);
  186. ifft_pass (buf, roots16 - 4, 4);
  187. }
  188. static void ifft32 (complex_t * buf)
  189. {
  190. ifft16 (buf);
  191. ifft8 (buf + 16);
  192. ifft8 (buf + 24);
  193. ifft_pass (buf, roots32 - 8, 8);
  194. }
  195. static void ifft64_c (complex_t * buf)
  196. {
  197. ifft32 (buf);
  198. ifft16 (buf + 32);
  199. ifft16 (buf + 48);
  200. ifft_pass (buf, roots64 - 16, 16);
  201. }
  202. static void ifft128_c (complex_t * buf)
  203. {
  204. ifft32 (buf);
  205. ifft16 (buf + 32);
  206. ifft16 (buf + 48);
  207. ifft_pass (buf, roots64 - 16, 16);
  208. ifft32 (buf + 64);
  209. ifft32 (buf + 96);
  210. ifft_pass (buf, roots128 - 32, 32);
  211. }
  212. void a52_imdct_512 (sample_t * data, sample_t * delay, sample_t bias)
  213. {
  214. int i, k;
  215. sample_t t_r, t_i, a_r, a_i, b_r, b_i, w_1, w_2;
  216. const sample_t * window = a52_imdct_window;
  217. for (i = 0; i < 128; i++) {
  218. k = fftorder[i];
  219. t_r = pre1[i].real;
  220. t_i = pre1[i].imag;
  221. buf[i].real = t_i * data[255-k] + t_r * data[k];
  222. buf[i].imag = t_r * data[255-k] - t_i * data[k];
  223. }
  224. ifft128 (buf);
  225. /* Post IFFT complex multiply plus IFFT complex conjugate*/
  226. /* Window and convert to real valued signal */
  227. for (i = 0; i < 64; i++) {
  228. /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
  229. t_r = post1[i].real;
  230. t_i = post1[i].imag;
  231. a_r = t_r * buf[i].real + t_i * buf[i].imag;
  232. a_i = t_i * buf[i].real - t_r * buf[i].imag;
  233. b_r = t_i * buf[127-i].real + t_r * buf[127-i].imag;
  234. b_i = t_r * buf[127-i].real - t_i * buf[127-i].imag;
  235. w_1 = window[2*i];
  236. w_2 = window[255-2*i];
  237. data[2*i] = delay[2*i] * w_2 - a_r * w_1 + bias;
  238. data[255-2*i] = delay[2*i] * w_1 + a_r * w_2 + bias;
  239. delay[2*i] = a_i;
  240. w_1 = window[2*i+1];
  241. w_2 = window[254-2*i];
  242. data[2*i+1] = delay[2*i+1] * w_2 + b_r * w_1 + bias;
  243. data[254-2*i] = delay[2*i+1] * w_1 - b_r * w_2 + bias;
  244. delay[2*i+1] = b_i;
  245. }
  246. }
  247. void a52_imdct_256(sample_t data[],sample_t delay[],sample_t bias)
  248. {
  249. int i, k;
  250. sample_t t_r, t_i, a_r, a_i, b_r, b_i, c_r, c_i, d_r, d_i, w_1, w_2;
  251. complex_t * buf1, * buf2;
  252. const sample_t * window = a52_imdct_window;
  253. buf1 = &buf[0];
  254. buf2 = &buf[64];
  255. /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
  256. for (i = 0; i < 64; i++) {
  257. k = fftorder[i];
  258. t_r = pre2[i].real;
  259. t_i = pre2[i].imag;
  260. buf1[i].real = t_i * data[254-k] + t_r * data[k];
  261. buf1[i].imag = t_r * data[254-k] - t_i * data[k];
  262. buf2[i].real = t_i * data[255-k] + t_r * data[k+1];
  263. buf2[i].imag = t_r * data[255-k] - t_i * data[k+1];
  264. }
  265. ifft64 (buf1);
  266. ifft64 (buf2);
  267. /* Post IFFT complex multiply */
  268. /* Window and convert to real valued signal */
  269. for (i = 0; i < 32; i++) {
  270. /* y1[n] = z1[n] * (xcos2[n] + j * xs in2[n]) ; */
  271. t_r = post2[i].real;
  272. t_i = post2[i].imag;
  273. a_r = t_r * buf1[i].real + t_i * buf1[i].imag;
  274. a_i = t_i * buf1[i].real - t_r * buf1[i].imag;
  275. b_r = t_i * buf1[63-i].real + t_r * buf1[63-i].imag;
  276. b_i = t_r * buf1[63-i].real - t_i * buf1[63-i].imag;
  277. c_r = t_r * buf2[i].real + t_i * buf2[i].imag;
  278. c_i = t_i * buf2[i].real - t_r * buf2[i].imag;
  279. d_r = t_i * buf2[63-i].real + t_r * buf2[63-i].imag;
  280. d_i = t_r * buf2[63-i].real - t_i * buf2[63-i].imag;
  281. w_1 = window[2*i];
  282. w_2 = window[255-2*i];
  283. data[2*i] = delay[2*i] * w_2 - a_r * w_1 + bias;
  284. data[255-2*i] = delay[2*i] * w_1 + a_r * w_2 + bias;
  285. delay[2*i] = c_i;
  286. w_1 = window[128+2*i];
  287. w_2 = window[127-2*i];
  288. data[128+2*i] = delay[127-2*i] * w_2 + a_i * w_1 + bias;
  289. data[127-2*i] = delay[127-2*i] * w_1 - a_i * w_2 + bias;
  290. delay[127-2*i] = c_r;
  291. w_1 = window[2*i+1];
  292. w_2 = window[254-2*i];
  293. data[2*i+1] = delay[2*i+1] * w_2 - b_i * w_1 + bias;
  294. data[254-2*i] = delay[2*i+1] * w_1 + b_i * w_2 + bias;
  295. delay[2*i+1] = d_r;
  296. w_1 = window[129+2*i];
  297. w_2 = window[126-2*i];
  298. data[129+2*i] = delay[126-2*i] * w_2 + b_r * w_1 + bias;
  299. data[126-2*i] = delay[126-2*i] * w_1 - b_r * w_2 + bias;
  300. delay[126-2*i] = d_i;
  301. }
  302. }
  303. static double besselI0 (double x)
  304. {
  305. double bessel = 1;
  306. int i = 100;
  307. do
  308. bessel = bessel * x / (i * i) + 1;
  309. while (--i);
  310. return bessel;
  311. }
  312. void a52_imdct_init (uint32_t mm_accel)
  313. {
  314. int i, k;
  315. double sum;
  316. /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
  317. sum = 0;
  318. for (i = 0; i < 256; i++) {
  319. sum += besselI0 (i * (256 - i) * (5 * M_PI / 256) * (5 * M_PI / 256));
  320. a52_imdct_window[i] = sum;
  321. }
  322. sum++;
  323. for (i = 0; i < 256; i++)
  324. a52_imdct_window[i] = sqrt (a52_imdct_window[i] / sum);
  325. for (i = 0; i < 3; i++)
  326. roots16[i] = cos ((M_PI / 8) * (i + 1));
  327. for (i = 0; i < 7; i++)
  328. roots32[i] = cos ((M_PI / 16) * (i + 1));
  329. for (i = 0; i < 15; i++)
  330. roots64[i] = cos ((M_PI / 32) * (i + 1));
  331. for (i = 0; i < 31; i++)
  332. roots128[i] = cos ((M_PI / 64) * (i + 1));
  333. for (i = 0; i < 64; i++) {
  334. k = fftorder[i] / 2 + 64;
  335. pre1[i].real = cos ((M_PI / 256) * (k - 0.25));
  336. pre1[i].imag = sin ((M_PI / 256) * (k - 0.25));
  337. }
  338. for (i = 64; i < 128; i++) {
  339. k = fftorder[i] / 2 + 64;
  340. pre1[i].real = -cos ((M_PI / 256) * (k - 0.25));
  341. pre1[i].imag = -sin ((M_PI / 256) * (k - 0.25));
  342. }
  343. for (i = 0; i < 64; i++) {
  344. post1[i].real = cos ((M_PI / 256) * (i + 0.5));
  345. post1[i].imag = sin ((M_PI / 256) * (i + 0.5));
  346. }
  347. for (i = 0; i < 64; i++) {
  348. k = fftorder[i] / 4;
  349. pre2[i].real = cos ((M_PI / 128) * (k - 0.25));
  350. pre2[i].imag = sin ((M_PI / 128) * (k - 0.25));
  351. }
  352. for (i = 0; i < 32; i++) {
  353. post2[i].real = cos ((M_PI / 128) * (i + 0.5));
  354. post2[i].imag = sin ((M_PI / 128) * (i + 0.5));
  355. }
  356. #ifdef LIBA52_DJBFFT
  357. if (mm_accel & MM_ACCEL_DJBFFT) {
  358. fprintf (stderr, "Using djbfft for IMDCT transform\n");
  359. ifft128 = (void (*) (complex_t *)) fftc4_un128;
  360. ifft64 = (void (*) (complex_t *)) fftc4_un64;
  361. } else
  362. #endif
  363. {
  364. fprintf (stderr, "No accelerated IMDCT transform found\n");
  365. ifft128 = ifft128_c;
  366. ifft64 = ifft64_c;
  367. }
  368. }