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.

424 lines
11KB

  1. /*
  2. * imdct.c
  3. * Copyright (C) 2000-2003 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 uint8_t fftorder[] = {
  44. 0,128, 64,192, 32,160,224, 96, 16,144, 80,208,240,112, 48,176,
  45. 8,136, 72,200, 40,168,232,104,248,120, 56,184, 24,152,216, 88,
  46. 4,132, 68,196, 36,164,228,100, 20,148, 84,212,244,116, 52,180,
  47. 252,124, 60,188, 28,156,220, 92, 12,140, 76,204,236,108, 44,172,
  48. 2,130, 66,194, 34,162,226, 98, 18,146, 82,210,242,114, 50,178,
  49. 10,138, 74,202, 42,170,234,106,250,122, 58,186, 26,154,218, 90,
  50. 254,126, 62,190, 30,158,222, 94, 14,142, 78,206,238,110, 46,174,
  51. 6,134, 70,198, 38,166,230,102,246,118, 54,182, 22,150,214, 86
  52. };
  53. /* Root values for IFFT */
  54. static sample_t roots16[3];
  55. static sample_t roots32[7];
  56. static sample_t roots64[15];
  57. static sample_t roots128[31];
  58. /* Twiddle factors for IMDCT */
  59. static complex_t pre1[128];
  60. static complex_t post1[64];
  61. static complex_t pre2[64];
  62. static complex_t post2[32];
  63. static sample_t a52_imdct_window[256];
  64. static void (* ifft128) (complex_t * buf);
  65. static void (* ifft64) (complex_t * buf);
  66. static inline void ifft2 (complex_t * buf)
  67. {
  68. sample_t r, i;
  69. r = buf[0].real;
  70. i = buf[0].imag;
  71. buf[0].real += buf[1].real;
  72. buf[0].imag += buf[1].imag;
  73. buf[1].real = r - buf[1].real;
  74. buf[1].imag = i - buf[1].imag;
  75. }
  76. static inline void ifft4 (complex_t * buf)
  77. {
  78. sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  79. tmp1 = buf[0].real + buf[1].real;
  80. tmp2 = buf[3].real + buf[2].real;
  81. tmp3 = buf[0].imag + buf[1].imag;
  82. tmp4 = buf[2].imag + buf[3].imag;
  83. tmp5 = buf[0].real - buf[1].real;
  84. tmp6 = buf[0].imag - buf[1].imag;
  85. tmp7 = buf[2].imag - buf[3].imag;
  86. tmp8 = buf[3].real - buf[2].real;
  87. buf[0].real = tmp1 + tmp2;
  88. buf[0].imag = tmp3 + tmp4;
  89. buf[2].real = tmp1 - tmp2;
  90. buf[2].imag = tmp3 - tmp4;
  91. buf[1].real = tmp5 + tmp7;
  92. buf[1].imag = tmp6 + tmp8;
  93. buf[3].real = tmp5 - tmp7;
  94. buf[3].imag = tmp6 - tmp8;
  95. }
  96. /* basic radix-2 ifft butterfly */
  97. #define BUTTERFLY_0(t0,t1,W0,W1,d0,d1) do { \
  98. t0 = MUL (W1, d1) + MUL (W0, d0); \
  99. t1 = MUL (W0, d1) - MUL (W1, d0); \
  100. } while (0)
  101. /* radix-2 ifft butterfly with bias */
  102. #define BUTTERFLY_B(t0,t1,W0,W1,d0,d1) do { \
  103. t0 = BIAS (MUL (d1, W1) + MUL (d0, W0)); \
  104. t1 = BIAS (MUL (d1, W0) - MUL (d0, W1)); \
  105. } while (0)
  106. /* the basic split-radix ifft butterfly */
  107. #define BUTTERFLY(a0,a1,a2,a3,wr,wi) do { \
  108. BUTTERFLY_0 (tmp5, tmp6, wr, wi, a2.real, a2.imag); \
  109. BUTTERFLY_0 (tmp8, tmp7, wr, wi, a3.imag, a3.real); \
  110. tmp1 = tmp5 + tmp7; \
  111. tmp2 = tmp6 + tmp8; \
  112. tmp3 = tmp6 - tmp8; \
  113. tmp4 = tmp7 - tmp5; \
  114. a2.real = a0.real - tmp1; \
  115. a2.imag = a0.imag - tmp2; \
  116. a3.real = a1.real - tmp3; \
  117. a3.imag = a1.imag - tmp4; \
  118. a0.real += tmp1; \
  119. a0.imag += tmp2; \
  120. a1.real += tmp3; \
  121. a1.imag += tmp4; \
  122. } while (0)
  123. /* split-radix ifft butterfly, specialized for wr=1 wi=0 */
  124. #define BUTTERFLY_ZERO(a0,a1,a2,a3) do { \
  125. tmp1 = a2.real + a3.real; \
  126. tmp2 = a2.imag + a3.imag; \
  127. tmp3 = a2.imag - a3.imag; \
  128. tmp4 = a3.real - a2.real; \
  129. a2.real = a0.real - tmp1; \
  130. a2.imag = a0.imag - tmp2; \
  131. a3.real = a1.real - tmp3; \
  132. a3.imag = a1.imag - tmp4; \
  133. a0.real += tmp1; \
  134. a0.imag += tmp2; \
  135. a1.real += tmp3; \
  136. a1.imag += tmp4; \
  137. } while (0)
  138. /* split-radix ifft butterfly, specialized for wr=wi */
  139. #define BUTTERFLY_HALF(a0,a1,a2,a3,w) do { \
  140. tmp5 = MUL (a2.real + a2.imag, w); \
  141. tmp6 = MUL (a2.imag - a2.real, w); \
  142. tmp7 = MUL (a3.real - a3.imag, w); \
  143. tmp8 = MUL (a3.imag + a3.real, w); \
  144. tmp1 = tmp5 + tmp7; \
  145. tmp2 = tmp6 + tmp8; \
  146. tmp3 = tmp6 - tmp8; \
  147. tmp4 = tmp7 - tmp5; \
  148. a2.real = a0.real - tmp1; \
  149. a2.imag = a0.imag - tmp2; \
  150. a3.real = a1.real - tmp3; \
  151. a3.imag = a1.imag - tmp4; \
  152. a0.real += tmp1; \
  153. a0.imag += tmp2; \
  154. a1.real += tmp3; \
  155. a1.imag += tmp4; \
  156. } while (0)
  157. static inline void ifft8 (complex_t * buf)
  158. {
  159. sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  160. ifft4 (buf);
  161. ifft2 (buf + 4);
  162. ifft2 (buf + 6);
  163. BUTTERFLY_ZERO (buf[0], buf[2], buf[4], buf[6]);
  164. BUTTERFLY_HALF (buf[1], buf[3], buf[5], buf[7], roots16[1]);
  165. }
  166. static void ifft_pass (complex_t * buf, sample_t * weight, int n)
  167. {
  168. complex_t * buf1;
  169. complex_t * buf2;
  170. complex_t * buf3;
  171. sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  172. int i;
  173. buf++;
  174. buf1 = buf + n;
  175. buf2 = buf + 2 * n;
  176. buf3 = buf + 3 * n;
  177. BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
  178. i = n - 1;
  179. do {
  180. BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0],
  181. weight[0], weight[2*i-n]);
  182. buf++;
  183. buf1++;
  184. buf2++;
  185. buf3++;
  186. weight++;
  187. } while (--i);
  188. }
  189. static void ifft16 (complex_t * buf)
  190. {
  191. ifft8 (buf);
  192. ifft4 (buf + 8);
  193. ifft4 (buf + 12);
  194. ifft_pass (buf, roots16, 4);
  195. }
  196. static void ifft32 (complex_t * buf)
  197. {
  198. ifft16 (buf);
  199. ifft8 (buf + 16);
  200. ifft8 (buf + 24);
  201. ifft_pass (buf, roots32, 8);
  202. }
  203. static void ifft64_c (complex_t * buf)
  204. {
  205. ifft32 (buf);
  206. ifft16 (buf + 32);
  207. ifft16 (buf + 48);
  208. ifft_pass (buf, roots64, 16);
  209. }
  210. static void ifft128_c (complex_t * buf)
  211. {
  212. ifft32 (buf);
  213. ifft16 (buf + 32);
  214. ifft16 (buf + 48);
  215. ifft_pass (buf, roots64, 16);
  216. ifft32 (buf + 64);
  217. ifft32 (buf + 96);
  218. ifft_pass (buf, roots128, 32);
  219. }
  220. void a52_imdct_512 (sample_t * data, sample_t * delay, sample_t bias)
  221. {
  222. int i, k;
  223. sample_t t_r, t_i, a_r, a_i, b_r, b_i, w_1, w_2;
  224. const sample_t * window = a52_imdct_window;
  225. complex_t buf[128];
  226. for (i = 0; i < 128; i++) {
  227. k = fftorder[i];
  228. t_r = pre1[i].real;
  229. t_i = pre1[i].imag;
  230. BUTTERFLY_0 (buf[i].real, buf[i].imag, t_r, t_i, data[k], data[255-k]);
  231. }
  232. ifft128 (buf);
  233. /* Post IFFT complex multiply plus IFFT complex conjugate*/
  234. /* Window and convert to real valued signal */
  235. for (i = 0; i < 64; i++) {
  236. /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
  237. t_r = post1[i].real;
  238. t_i = post1[i].imag;
  239. BUTTERFLY_0 (a_r, a_i, t_i, t_r, buf[i].imag, buf[i].real);
  240. BUTTERFLY_0 (b_r, b_i, t_r, t_i, buf[127-i].imag, buf[127-i].real);
  241. w_1 = window[2*i];
  242. w_2 = window[255-2*i];
  243. BUTTERFLY_B (data[255-2*i], data[2*i], w_2, w_1, a_r, delay[2*i]);
  244. delay[2*i] = a_i;
  245. w_1 = window[2*i+1];
  246. w_2 = window[254-2*i];
  247. BUTTERFLY_B (data[2*i+1], data[254-2*i], w_1, w_2, b_r, delay[2*i+1]);
  248. delay[2*i+1] = b_i;
  249. }
  250. }
  251. void a52_imdct_256 (sample_t * data, sample_t * delay, sample_t bias)
  252. {
  253. int i, k;
  254. 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;
  255. const sample_t * window = a52_imdct_window;
  256. complex_t buf1[64], buf2[64];
  257. /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
  258. for (i = 0; i < 64; i++) {
  259. k = fftorder[i];
  260. t_r = pre2[i].real;
  261. t_i = pre2[i].imag;
  262. BUTTERFLY_0 (buf1[i].real, buf1[i].imag, t_r, t_i, data[k], data[254-k]);
  263. BUTTERFLY_0 (buf2[i].real, buf2[i].imag, t_r, t_i, data[k+1], data[255-k]);
  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. BUTTERFLY_0 (a_r, a_i, t_i, t_r, buf1[i].imag, buf1[i].real);
  274. BUTTERFLY_0 (b_r, b_i, t_r, t_i, buf1[63-i].imag, buf1[63-i].real);
  275. BUTTERFLY_0 (c_r, c_i, t_i, t_r, buf2[i].imag, buf2[i].real);
  276. BUTTERFLY_0 (d_r, d_i, t_r, t_i, buf2[63-i].imag, buf2[63-i].real);
  277. w_1 = window[2*i];
  278. w_2 = window[255-2*i];
  279. BUTTERFLY_B (data[255-2*i], data[2*i], w_2, w_1, a_r, delay[2*i]);
  280. delay[2*i] = c_i;
  281. w_1 = window[128+2*i];
  282. w_2 = window[127-2*i];
  283. BUTTERFLY_B (data[128+2*i], data[127-2*i], w_1, w_2, a_i, delay[127-2*i]);
  284. delay[127-2*i] = c_r;
  285. w_1 = window[2*i+1];
  286. w_2 = window[254-2*i];
  287. BUTTERFLY_B (data[254-2*i], data[2*i+1], w_2, w_1, b_i, delay[2*i+1]);
  288. delay[2*i+1] = d_r;
  289. w_1 = window[129+2*i];
  290. w_2 = window[126-2*i];
  291. BUTTERFLY_B (data[129+2*i], data[126-2*i], w_1, w_2, b_r, delay[126-2*i]);
  292. delay[126-2*i] = d_i;
  293. }
  294. }
  295. static double besselI0 (double x)
  296. {
  297. double bessel = 1;
  298. int i = 100;
  299. do
  300. bessel = bessel * x / (i * i) + 1;
  301. while (--i);
  302. return bessel;
  303. }
  304. void a52_imdct_init (uint32_t mm_accel)
  305. {
  306. int i, k;
  307. double sum;
  308. double local_imdct_window[256];
  309. /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
  310. sum = 0;
  311. for (i = 0; i < 256; i++) {
  312. sum += besselI0 (i * (256 - i) * (5 * M_PI / 256) * (5 * M_PI / 256));
  313. local_imdct_window[i] = sum;
  314. }
  315. sum++;
  316. for (i = 0; i < 256; i++)
  317. a52_imdct_window[i] = SAMPLE (sqrt (local_imdct_window[i] / sum));
  318. for (i = 0; i < 3; i++)
  319. roots16[i] = SAMPLE (cos ((M_PI / 8) * (i + 1)));
  320. for (i = 0; i < 7; i++)
  321. roots32[i] = SAMPLE (cos ((M_PI / 16) * (i + 1)));
  322. for (i = 0; i < 15; i++)
  323. roots64[i] = SAMPLE (cos ((M_PI / 32) * (i + 1)));
  324. for (i = 0; i < 31; i++)
  325. roots128[i] = SAMPLE (cos ((M_PI / 64) * (i + 1)));
  326. for (i = 0; i < 64; i++) {
  327. k = fftorder[i] / 2 + 64;
  328. pre1[i].real = SAMPLE (cos ((M_PI / 256) * (k - 0.25)));
  329. pre1[i].imag = SAMPLE (sin ((M_PI / 256) * (k - 0.25)));
  330. }
  331. for (i = 64; i < 128; i++) {
  332. k = fftorder[i] / 2 + 64;
  333. pre1[i].real = SAMPLE (-cos ((M_PI / 256) * (k - 0.25)));
  334. pre1[i].imag = SAMPLE (-sin ((M_PI / 256) * (k - 0.25)));
  335. }
  336. for (i = 0; i < 64; i++) {
  337. post1[i].real = SAMPLE (cos ((M_PI / 256) * (i + 0.5)));
  338. post1[i].imag = SAMPLE (sin ((M_PI / 256) * (i + 0.5)));
  339. }
  340. for (i = 0; i < 64; i++) {
  341. k = fftorder[i] / 4;
  342. pre2[i].real = SAMPLE (cos ((M_PI / 128) * (k - 0.25)));
  343. pre2[i].imag = SAMPLE (sin ((M_PI / 128) * (k - 0.25)));
  344. }
  345. for (i = 0; i < 32; i++) {
  346. post2[i].real = SAMPLE (cos ((M_PI / 128) * (i + 0.5)));
  347. post2[i].imag = SAMPLE (sin ((M_PI / 128) * (i + 0.5)));
  348. }
  349. #ifdef LIBA52_DJBFFT
  350. if (mm_accel & MM_ACCEL_DJBFFT) {
  351. ifft128 = (void (*) (complex_t *)) fftc4_un128;
  352. ifft64 = (void (*) (complex_t *)) fftc4_un64;
  353. } else
  354. #endif
  355. {
  356. ifft128 = ifft128_c;
  357. ifft64 = ifft64_c;
  358. }
  359. }