2
0

mpegaudiodsp_template.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2001, 2002 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/mem.h"
  23. #include "dct32.h"
  24. #include "mathops.h"
  25. #include "mpegaudiodsp.h"
  26. #include "mpegaudio.h"
  27. #if USE_FLOATS
  28. #define RENAME(n) n##_float
  29. static inline float round_sample(float *sum)
  30. {
  31. float sum1=*sum;
  32. *sum = 0;
  33. return sum1;
  34. }
  35. #define MACS(rt, ra, rb) rt+=(ra)*(rb)
  36. #define MULS(ra, rb) ((ra)*(rb))
  37. #define MULH3(x, y, s) ((s)*(y)*(x))
  38. #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
  39. #define MULLx(x, y, s) ((y)*(x))
  40. #define FIXHR(x) ((float)(x))
  41. #define FIXR(x) ((float)(x))
  42. #define SHR(a,b) ((a)*(1.0f/(1<<(b))))
  43. #else
  44. #define RENAME(n) n##_fixed
  45. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
  46. static inline int round_sample(int64_t *sum)
  47. {
  48. int sum1;
  49. sum1 = (int)((*sum) >> OUT_SHIFT);
  50. *sum &= (1<<OUT_SHIFT)-1;
  51. return av_clip_int16(sum1);
  52. }
  53. # define MULS(ra, rb) MUL64(ra, rb)
  54. # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
  55. # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
  56. # define MULH3(x, y, s) MULH((s)*(x), y)
  57. # define MULLx(x, y, s) MULL((int)(x),(y),s)
  58. # define SHR(a,b) (((int)(a))>>(b))
  59. # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
  60. # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
  61. #endif
  62. /** Window for MDCT. Actually only the elements in [0,17] and
  63. [MDCT_BUF_SIZE/2, MDCT_BUF_SIZE/2 + 17] are actually used. The rest
  64. is just to preserve alignment for SIMD implementations.
  65. */
  66. DECLARE_ALIGNED(16, INTFLOAT, RENAME(ff_mdct_win))[8][MDCT_BUF_SIZE];
  67. DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
  68. #define SUM8(op, sum, w, p) \
  69. { \
  70. op(sum, (w)[0 * 64], (p)[0 * 64]); \
  71. op(sum, (w)[1 * 64], (p)[1 * 64]); \
  72. op(sum, (w)[2 * 64], (p)[2 * 64]); \
  73. op(sum, (w)[3 * 64], (p)[3 * 64]); \
  74. op(sum, (w)[4 * 64], (p)[4 * 64]); \
  75. op(sum, (w)[5 * 64], (p)[5 * 64]); \
  76. op(sum, (w)[6 * 64], (p)[6 * 64]); \
  77. op(sum, (w)[7 * 64], (p)[7 * 64]); \
  78. }
  79. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  80. { \
  81. INTFLOAT tmp;\
  82. tmp = p[0 * 64];\
  83. op1(sum1, (w1)[0 * 64], tmp);\
  84. op2(sum2, (w2)[0 * 64], tmp);\
  85. tmp = p[1 * 64];\
  86. op1(sum1, (w1)[1 * 64], tmp);\
  87. op2(sum2, (w2)[1 * 64], tmp);\
  88. tmp = p[2 * 64];\
  89. op1(sum1, (w1)[2 * 64], tmp);\
  90. op2(sum2, (w2)[2 * 64], tmp);\
  91. tmp = p[3 * 64];\
  92. op1(sum1, (w1)[3 * 64], tmp);\
  93. op2(sum2, (w2)[3 * 64], tmp);\
  94. tmp = p[4 * 64];\
  95. op1(sum1, (w1)[4 * 64], tmp);\
  96. op2(sum2, (w2)[4 * 64], tmp);\
  97. tmp = p[5 * 64];\
  98. op1(sum1, (w1)[5 * 64], tmp);\
  99. op2(sum2, (w2)[5 * 64], tmp);\
  100. tmp = p[6 * 64];\
  101. op1(sum1, (w1)[6 * 64], tmp);\
  102. op2(sum2, (w2)[6 * 64], tmp);\
  103. tmp = p[7 * 64];\
  104. op1(sum1, (w1)[7 * 64], tmp);\
  105. op2(sum2, (w2)[7 * 64], tmp);\
  106. }
  107. void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
  108. int *dither_state, OUT_INT *samples,
  109. ptrdiff_t incr)
  110. {
  111. register const MPA_INT *w, *w2, *p;
  112. int j;
  113. OUT_INT *samples2;
  114. #if USE_FLOATS
  115. float sum, sum2;
  116. #else
  117. int64_t sum, sum2;
  118. #endif
  119. /* copy to avoid wrap */
  120. memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
  121. samples2 = samples + 31 * incr;
  122. w = window;
  123. w2 = window + 31;
  124. sum = *dither_state;
  125. p = synth_buf + 16;
  126. SUM8(MACS, sum, w, p);
  127. p = synth_buf + 48;
  128. SUM8(MLSS, sum, w + 32, p);
  129. *samples = round_sample(&sum);
  130. samples += incr;
  131. w++;
  132. /* we calculate two samples at the same time to avoid one memory
  133. access per two sample */
  134. for(j=1;j<16;j++) {
  135. sum2 = 0;
  136. p = synth_buf + 16 + j;
  137. SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
  138. p = synth_buf + 48 - j;
  139. SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
  140. *samples = round_sample(&sum);
  141. samples += incr;
  142. sum += sum2;
  143. *samples2 = round_sample(&sum);
  144. samples2 -= incr;
  145. w++;
  146. w2--;
  147. }
  148. p = synth_buf + 32;
  149. SUM8(MLSS, sum, w + 32, p);
  150. *samples = round_sample(&sum);
  151. *dither_state= sum;
  152. }
  153. /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
  154. 32 samples. */
  155. void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
  156. int *synth_buf_offset,
  157. MPA_INT *window, int *dither_state,
  158. OUT_INT *samples, ptrdiff_t incr,
  159. MPA_INT *sb_samples)
  160. {
  161. MPA_INT *synth_buf;
  162. int offset;
  163. offset = *synth_buf_offset;
  164. synth_buf = synth_buf_ptr + offset;
  165. s->RENAME(dct32)(synth_buf, sb_samples);
  166. s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
  167. offset = (offset - 32) & 511;
  168. *synth_buf_offset = offset;
  169. }
  170. av_cold void RENAME(ff_mpa_synth_init)(MPA_INT *window)
  171. {
  172. int i, j;
  173. /* max = 18760, max sum over all 16 coefs : 44736 */
  174. for(i=0;i<257;i++) {
  175. INTFLOAT v;
  176. v = ff_mpa_enwindow[i];
  177. #if USE_FLOATS
  178. v *= 1.0 / (1LL<<(16 + FRAC_BITS));
  179. #endif
  180. window[i] = v;
  181. if ((i & 63) != 0)
  182. v = -v;
  183. if (i != 0)
  184. window[512 - i] = v;
  185. }
  186. // Needed for avoiding shuffles in ASM implementations
  187. for(i=0; i < 8; i++)
  188. for(j=0; j < 16; j++)
  189. window[512+16*i+j] = window[64*i+32-j];
  190. for(i=0; i < 8; i++)
  191. for(j=0; j < 16; j++)
  192. window[512+128+16*i+j] = window[64*i+48-j];
  193. }
  194. av_cold void RENAME(ff_init_mpadsp_tabs)(void)
  195. {
  196. int i, j;
  197. /* compute mdct windows */
  198. for (i = 0; i < 36; i++) {
  199. for (j = 0; j < 4; j++) {
  200. double d;
  201. if (j == 2 && i % 3 != 1)
  202. continue;
  203. d = sin(M_PI * (i + 0.5) / 36.0);
  204. if (j == 1) {
  205. if (i >= 30) d = 0;
  206. else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
  207. else if (i >= 18) d = 1;
  208. } else if (j == 3) {
  209. if (i < 6) d = 0;
  210. else if (i < 12) d = sin(M_PI * (i - 6 + 0.5) / 12.0);
  211. else if (i < 18) d = 1;
  212. }
  213. //merge last stage of imdct into the window coefficients
  214. d *= 0.5 * IMDCT_SCALAR / cos(M_PI * (2 * i + 19) / 72);
  215. if (j == 2)
  216. RENAME(ff_mdct_win)[j][i/3] = FIXHR((d / (1<<5)));
  217. else {
  218. int idx = i < 18 ? i : i + (MDCT_BUF_SIZE/2 - 18);
  219. RENAME(ff_mdct_win)[j][idx] = FIXHR((d / (1<<5)));
  220. }
  221. }
  222. }
  223. /* NOTE: we do frequency inversion adter the MDCT by changing
  224. the sign of the right window coefs */
  225. for (j = 0; j < 4; j++) {
  226. for (i = 0; i < MDCT_BUF_SIZE; i += 2) {
  227. RENAME(ff_mdct_win)[j + 4][i ] = RENAME(ff_mdct_win)[j][i ];
  228. RENAME(ff_mdct_win)[j + 4][i + 1] = -RENAME(ff_mdct_win)[j][i + 1];
  229. }
  230. }
  231. }
  232. /* cos(pi*i/18) */
  233. #define C1 FIXHR(0.98480775301220805936/2)
  234. #define C2 FIXHR(0.93969262078590838405/2)
  235. #define C3 FIXHR(0.86602540378443864676/2)
  236. #define C4 FIXHR(0.76604444311897803520/2)
  237. #define C5 FIXHR(0.64278760968653932632/2)
  238. #define C6 FIXHR(0.5/2)
  239. #define C7 FIXHR(0.34202014332566873304/2)
  240. #define C8 FIXHR(0.17364817766693034885/2)
  241. /* 0.5 / cos(pi*(2*i+1)/36) */
  242. static const INTFLOAT icos36[9] = {
  243. FIXR(0.50190991877167369479),
  244. FIXR(0.51763809020504152469), //0
  245. FIXR(0.55168895948124587824),
  246. FIXR(0.61038729438072803416),
  247. FIXR(0.70710678118654752439), //1
  248. FIXR(0.87172339781054900991),
  249. FIXR(1.18310079157624925896),
  250. FIXR(1.93185165257813657349), //2
  251. FIXR(5.73685662283492756461),
  252. };
  253. /* 0.5 / cos(pi*(2*i+1)/36) */
  254. static const INTFLOAT icos36h[9] = {
  255. FIXHR(0.50190991877167369479/2),
  256. FIXHR(0.51763809020504152469/2), //0
  257. FIXHR(0.55168895948124587824/2),
  258. FIXHR(0.61038729438072803416/2),
  259. FIXHR(0.70710678118654752439/2), //1
  260. FIXHR(0.87172339781054900991/2),
  261. FIXHR(1.18310079157624925896/4),
  262. FIXHR(1.93185165257813657349/4), //2
  263. // FIXHR(5.73685662283492756461),
  264. };
  265. /* using Lee like decomposition followed by hand coded 9 points DCT */
  266. static void imdct36(INTFLOAT *out, INTFLOAT *buf, SUINTFLOAT *in, INTFLOAT *win)
  267. {
  268. int i, j;
  269. SUINTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
  270. SUINTFLOAT tmp[18], *tmp1, *in1;
  271. for (i = 17; i >= 1; i--)
  272. in[i] += in[i-1];
  273. for (i = 17; i >= 3; i -= 2)
  274. in[i] += in[i-2];
  275. for (j = 0; j < 2; j++) {
  276. tmp1 = tmp + j;
  277. in1 = in + j;
  278. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  279. t3 = in1[2*0] + SHR(in1[2*6],1);
  280. t1 = in1[2*0] - in1[2*6];
  281. tmp1[ 6] = t1 - SHR(t2,1);
  282. tmp1[16] = t1 + t2;
  283. t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
  284. t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
  285. t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
  286. tmp1[10] = t3 - t0 - t2;
  287. tmp1[ 2] = t3 + t0 + t1;
  288. tmp1[14] = t3 + t2 - t1;
  289. tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
  290. t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
  291. t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
  292. t0 = MULH3(in1[2*3], C3, 2);
  293. t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
  294. tmp1[ 0] = t2 + t3 + t0;
  295. tmp1[12] = t2 + t1 - t0;
  296. tmp1[ 8] = t3 - t1 - t0;
  297. }
  298. i = 0;
  299. for (j = 0; j < 4; j++) {
  300. t0 = tmp[i];
  301. t1 = tmp[i + 2];
  302. s0 = t1 + t0;
  303. s2 = t1 - t0;
  304. t2 = tmp[i + 1];
  305. t3 = tmp[i + 3];
  306. s1 = MULH3(t3 + t2, icos36h[ j], 2);
  307. s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
  308. t0 = s0 + s1;
  309. t1 = s0 - s1;
  310. out[(9 + j) * SBLIMIT] = MULH3(t1, win[ 9 + j], 1) + buf[4*(9 + j)];
  311. out[(8 - j) * SBLIMIT] = MULH3(t1, win[ 8 - j], 1) + buf[4*(8 - j)];
  312. buf[4 * ( 9 + j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
  313. buf[4 * ( 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
  314. t0 = s2 + s3;
  315. t1 = s2 - s3;
  316. out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[ 9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
  317. out[ j * SBLIMIT] = MULH3(t1, win[ j], 1) + buf[4*( j)];
  318. buf[4 * ( 9 + 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
  319. buf[4 * ( j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + j], 1);
  320. i += 4;
  321. }
  322. s0 = tmp[16];
  323. s1 = MULH3(tmp[17], icos36h[4], 2);
  324. t0 = s0 + s1;
  325. t1 = s0 - s1;
  326. out[(9 + 4) * SBLIMIT] = MULH3(t1, win[ 9 + 4], 1) + buf[4*(9 + 4)];
  327. out[(8 - 4) * SBLIMIT] = MULH3(t1, win[ 8 - 4], 1) + buf[4*(8 - 4)];
  328. buf[4 * ( 9 + 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
  329. buf[4 * ( 8 - 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
  330. }
  331. void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
  332. int count, int switch_point, int block_type)
  333. {
  334. int j;
  335. for (j=0 ; j < count; j++) {
  336. /* apply window & overlap with previous buffer */
  337. /* select window */
  338. int win_idx = (switch_point && j < 2) ? 0 : block_type;
  339. INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
  340. imdct36(out, buf, in, win);
  341. in += 18;
  342. buf += ((j&3) != 3 ? 1 : (72-3));
  343. out++;
  344. }
  345. }