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.

335 lines
9.5KB

  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/mem.h"
  22. #include "dct32.h"
  23. #include "mathops.h"
  24. #include "mpegaudiodsp.h"
  25. #include "mpegaudio.h"
  26. #include "mpegaudiodata.h"
  27. #if CONFIG_FLOAT
  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(x,y,s)
  58. # define SHR(a,b) ((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. DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
  63. #define SUM8(op, sum, w, p) \
  64. { \
  65. op(sum, (w)[0 * 64], (p)[0 * 64]); \
  66. op(sum, (w)[1 * 64], (p)[1 * 64]); \
  67. op(sum, (w)[2 * 64], (p)[2 * 64]); \
  68. op(sum, (w)[3 * 64], (p)[3 * 64]); \
  69. op(sum, (w)[4 * 64], (p)[4 * 64]); \
  70. op(sum, (w)[5 * 64], (p)[5 * 64]); \
  71. op(sum, (w)[6 * 64], (p)[6 * 64]); \
  72. op(sum, (w)[7 * 64], (p)[7 * 64]); \
  73. }
  74. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  75. { \
  76. INTFLOAT tmp;\
  77. tmp = p[0 * 64];\
  78. op1(sum1, (w1)[0 * 64], tmp);\
  79. op2(sum2, (w2)[0 * 64], tmp);\
  80. tmp = p[1 * 64];\
  81. op1(sum1, (w1)[1 * 64], tmp);\
  82. op2(sum2, (w2)[1 * 64], tmp);\
  83. tmp = p[2 * 64];\
  84. op1(sum1, (w1)[2 * 64], tmp);\
  85. op2(sum2, (w2)[2 * 64], tmp);\
  86. tmp = p[3 * 64];\
  87. op1(sum1, (w1)[3 * 64], tmp);\
  88. op2(sum2, (w2)[3 * 64], tmp);\
  89. tmp = p[4 * 64];\
  90. op1(sum1, (w1)[4 * 64], tmp);\
  91. op2(sum2, (w2)[4 * 64], tmp);\
  92. tmp = p[5 * 64];\
  93. op1(sum1, (w1)[5 * 64], tmp);\
  94. op2(sum2, (w2)[5 * 64], tmp);\
  95. tmp = p[6 * 64];\
  96. op1(sum1, (w1)[6 * 64], tmp);\
  97. op2(sum2, (w2)[6 * 64], tmp);\
  98. tmp = p[7 * 64];\
  99. op1(sum1, (w1)[7 * 64], tmp);\
  100. op2(sum2, (w2)[7 * 64], tmp);\
  101. }
  102. void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
  103. int *dither_state, OUT_INT *samples,
  104. int incr)
  105. {
  106. register const MPA_INT *w, *w2, *p;
  107. int j;
  108. OUT_INT *samples2;
  109. #if CONFIG_FLOAT
  110. float sum, sum2;
  111. #else
  112. int64_t sum, sum2;
  113. #endif
  114. /* copy to avoid wrap */
  115. memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
  116. samples2 = samples + 31 * incr;
  117. w = window;
  118. w2 = window + 31;
  119. sum = *dither_state;
  120. p = synth_buf + 16;
  121. SUM8(MACS, sum, w, p);
  122. p = synth_buf + 48;
  123. SUM8(MLSS, sum, w + 32, p);
  124. *samples = round_sample(&sum);
  125. samples += incr;
  126. w++;
  127. /* we calculate two samples at the same time to avoid one memory
  128. access per two sample */
  129. for(j=1;j<16;j++) {
  130. sum2 = 0;
  131. p = synth_buf + 16 + j;
  132. SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
  133. p = synth_buf + 48 - j;
  134. SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
  135. *samples = round_sample(&sum);
  136. samples += incr;
  137. sum += sum2;
  138. *samples2 = round_sample(&sum);
  139. samples2 -= incr;
  140. w++;
  141. w2--;
  142. }
  143. p = synth_buf + 32;
  144. SUM8(MLSS, sum, w + 32, p);
  145. *samples = round_sample(&sum);
  146. *dither_state= sum;
  147. }
  148. /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
  149. 32 samples. */
  150. void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
  151. int *synth_buf_offset,
  152. MPA_INT *window, int *dither_state,
  153. OUT_INT *samples, int incr,
  154. MPA_INT *sb_samples)
  155. {
  156. MPA_INT *synth_buf;
  157. int offset;
  158. offset = *synth_buf_offset;
  159. synth_buf = synth_buf_ptr + offset;
  160. s->RENAME(dct32)(synth_buf, sb_samples);
  161. s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
  162. offset = (offset - 32) & 511;
  163. *synth_buf_offset = offset;
  164. }
  165. void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
  166. {
  167. int i, j;
  168. /* max = 18760, max sum over all 16 coefs : 44736 */
  169. for(i=0;i<257;i++) {
  170. INTFLOAT v;
  171. v = ff_mpa_enwindow[i];
  172. #if CONFIG_FLOAT
  173. v *= 1.0 / (1LL<<(16 + FRAC_BITS));
  174. #endif
  175. window[i] = v;
  176. if ((i & 63) != 0)
  177. v = -v;
  178. if (i != 0)
  179. window[512 - i] = v;
  180. }
  181. // Needed for avoiding shuffles in ASM implementations
  182. for(i=0; i < 8; i++)
  183. for(j=0; j < 16; j++)
  184. window[512+16*i+j] = window[64*i+32-j];
  185. for(i=0; i < 8; i++)
  186. for(j=0; j < 16; j++)
  187. window[512+128+16*i+j] = window[64*i+48-j];
  188. }
  189. /* cos(pi*i/18) */
  190. #define C1 FIXHR(0.98480775301220805936/2)
  191. #define C2 FIXHR(0.93969262078590838405/2)
  192. #define C3 FIXHR(0.86602540378443864676/2)
  193. #define C4 FIXHR(0.76604444311897803520/2)
  194. #define C5 FIXHR(0.64278760968653932632/2)
  195. #define C6 FIXHR(0.5/2)
  196. #define C7 FIXHR(0.34202014332566873304/2)
  197. #define C8 FIXHR(0.17364817766693034885/2)
  198. /* 0.5 / cos(pi*(2*i+1)/36) */
  199. static const INTFLOAT icos36[9] = {
  200. FIXR(0.50190991877167369479),
  201. FIXR(0.51763809020504152469),
  202. FIXR(0.55168895948124587824),
  203. FIXR(0.61038729438072803416),
  204. FIXR(0.70710678118654752439),
  205. FIXR(0.87172339781054900991),
  206. FIXR(1.18310079157624925896),
  207. FIXR(1.93185165257813657349),
  208. FIXR(5.73685662283492756461),
  209. };
  210. /* 0.5 / cos(pi*(2*i+1)/36) */
  211. static const INTFLOAT icos36h[9] = {
  212. FIXHR(0.50190991877167369479/2),
  213. FIXHR(0.51763809020504152469/2),
  214. FIXHR(0.55168895948124587824/2),
  215. FIXHR(0.61038729438072803416/2),
  216. FIXHR(0.70710678118654752439/2),
  217. FIXHR(0.87172339781054900991/2),
  218. FIXHR(1.18310079157624925896/4),
  219. FIXHR(1.93185165257813657349/4),
  220. };
  221. /* using Lee like decomposition followed by hand coded 9 points DCT */
  222. void RENAME(ff_imdct36)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
  223. INTFLOAT *win)
  224. {
  225. int i, j;
  226. INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
  227. INTFLOAT tmp[18], *tmp1, *in1;
  228. for(i=17;i>=1;i--)
  229. in[i] += in[i-1];
  230. for(i=17;i>=3;i-=2)
  231. in[i] += in[i-2];
  232. for(j=0;j<2;j++) {
  233. tmp1 = tmp + j;
  234. in1 = in + j;
  235. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  236. t3 = in1[2*0] + SHR(in1[2*6],1);
  237. t1 = in1[2*0] - in1[2*6];
  238. tmp1[ 6] = t1 - SHR(t2,1);
  239. tmp1[16] = t1 + t2;
  240. t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
  241. t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
  242. t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
  243. tmp1[10] = t3 - t0 - t2;
  244. tmp1[ 2] = t3 + t0 + t1;
  245. tmp1[14] = t3 + t2 - t1;
  246. tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
  247. t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
  248. t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
  249. t0 = MULH3(in1[2*3], C3, 2);
  250. t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
  251. tmp1[ 0] = t2 + t3 + t0;
  252. tmp1[12] = t2 + t1 - t0;
  253. tmp1[ 8] = t3 - t1 - t0;
  254. }
  255. i = 0;
  256. for(j=0;j<4;j++) {
  257. t0 = tmp[i];
  258. t1 = tmp[i + 2];
  259. s0 = t1 + t0;
  260. s2 = t1 - t0;
  261. t2 = tmp[i + 1];
  262. t3 = tmp[i + 3];
  263. s1 = MULH3(t3 + t2, icos36h[j], 2);
  264. s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
  265. t0 = s0 + s1;
  266. t1 = s0 - s1;
  267. out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
  268. out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
  269. buf[9 + j] = MULH3(t0, win[20 + 9 + j], 1);
  270. buf[8 - j] = MULH3(t0, win[20 + 8 - j], 1);
  271. t0 = s2 + s3;
  272. t1 = s2 - s3;
  273. out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
  274. out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
  275. buf[9 + 8 - j] = MULH3(t0, win[20 + 9 + 8 - j], 1);
  276. buf[ + j] = MULH3(t0, win[20 + j], 1);
  277. i += 4;
  278. }
  279. s0 = tmp[16];
  280. s1 = MULH3(tmp[17], icos36h[4], 2);
  281. t0 = s0 + s1;
  282. t1 = s0 - s1;
  283. out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
  284. out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
  285. buf[9 + 4] = MULH3(t0, win[20 + 9 + 4], 1);
  286. buf[8 - 4] = MULH3(t0, win[20 + 8 - 4], 1);
  287. }