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.

425 lines
11KB

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