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.

372 lines
11KB

  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 "libavutil/thread.h"
  24. #include "dct32.h"
  25. #include "mathops.h"
  26. #include "mpegaudiodsp.h"
  27. #include "mpegaudio.h"
  28. #if USE_FLOATS
  29. #define RENAME(n) n##_float
  30. static inline float round_sample(float *sum)
  31. {
  32. float sum1=*sum;
  33. *sum = 0;
  34. return sum1;
  35. }
  36. #define MACS(rt, ra, rb) rt+=(ra)*(rb)
  37. #define MULS(ra, rb) ((ra)*(rb))
  38. #define MULH3(x, y, s) ((s)*(y)*(x))
  39. #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
  40. #define MULLx(x, y, s) ((y)*(x))
  41. #define FIXHR(x) ((float)(x))
  42. #define FIXR(x) ((float)(x))
  43. #define SHR(a,b) ((a)*(1.0f/(1<<(b))))
  44. #else
  45. #define RENAME(n) n##_fixed
  46. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
  47. static inline int round_sample(int64_t *sum)
  48. {
  49. int sum1;
  50. sum1 = (int)((*sum) >> OUT_SHIFT);
  51. *sum &= (1<<OUT_SHIFT)-1;
  52. return av_clip_int16(sum1);
  53. }
  54. # define MULS(ra, rb) MUL64(ra, rb)
  55. # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
  56. # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
  57. # define MULH3(x, y, s) MULH((s)*(x), y)
  58. # define MULLx(x, y, s) MULL((int)(x),(y),s)
  59. # define SHR(a,b) (((int)(a))>>(b))
  60. # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
  61. # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
  62. #endif
  63. /** Window for MDCT. Actually only the elements in [0,17] and
  64. [MDCT_BUF_SIZE/2, MDCT_BUF_SIZE/2 + 17] are actually used. The rest
  65. is just to preserve alignment for SIMD implementations.
  66. */
  67. DECLARE_ALIGNED(16, INTFLOAT, RENAME(ff_mdct_win))[8][MDCT_BUF_SIZE];
  68. DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
  69. #define SUM8(op, sum, w, p) \
  70. { \
  71. op(sum, (w)[0 * 64], (p)[0 * 64]); \
  72. op(sum, (w)[1 * 64], (p)[1 * 64]); \
  73. op(sum, (w)[2 * 64], (p)[2 * 64]); \
  74. op(sum, (w)[3 * 64], (p)[3 * 64]); \
  75. op(sum, (w)[4 * 64], (p)[4 * 64]); \
  76. op(sum, (w)[5 * 64], (p)[5 * 64]); \
  77. op(sum, (w)[6 * 64], (p)[6 * 64]); \
  78. op(sum, (w)[7 * 64], (p)[7 * 64]); \
  79. }
  80. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  81. { \
  82. INTFLOAT tmp;\
  83. tmp = p[0 * 64];\
  84. op1(sum1, (w1)[0 * 64], tmp);\
  85. op2(sum2, (w2)[0 * 64], tmp);\
  86. tmp = p[1 * 64];\
  87. op1(sum1, (w1)[1 * 64], tmp);\
  88. op2(sum2, (w2)[1 * 64], tmp);\
  89. tmp = p[2 * 64];\
  90. op1(sum1, (w1)[2 * 64], tmp);\
  91. op2(sum2, (w2)[2 * 64], tmp);\
  92. tmp = p[3 * 64];\
  93. op1(sum1, (w1)[3 * 64], tmp);\
  94. op2(sum2, (w2)[3 * 64], tmp);\
  95. tmp = p[4 * 64];\
  96. op1(sum1, (w1)[4 * 64], tmp);\
  97. op2(sum2, (w2)[4 * 64], tmp);\
  98. tmp = p[5 * 64];\
  99. op1(sum1, (w1)[5 * 64], tmp);\
  100. op2(sum2, (w2)[5 * 64], tmp);\
  101. tmp = p[6 * 64];\
  102. op1(sum1, (w1)[6 * 64], tmp);\
  103. op2(sum2, (w2)[6 * 64], tmp);\
  104. tmp = p[7 * 64];\
  105. op1(sum1, (w1)[7 * 64], tmp);\
  106. op2(sum2, (w2)[7 * 64], tmp);\
  107. }
  108. void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
  109. int *dither_state, OUT_INT *samples,
  110. ptrdiff_t incr)
  111. {
  112. register const MPA_INT *w, *w2, *p;
  113. int j;
  114. OUT_INT *samples2;
  115. #if USE_FLOATS
  116. float sum, sum2;
  117. #else
  118. int64_t sum, sum2;
  119. #endif
  120. /* copy to avoid wrap */
  121. memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
  122. samples2 = samples + 31 * incr;
  123. w = window;
  124. w2 = window + 31;
  125. sum = *dither_state;
  126. p = synth_buf + 16;
  127. SUM8(MACS, sum, w, p);
  128. p = synth_buf + 48;
  129. SUM8(MLSS, sum, w + 32, p);
  130. *samples = round_sample(&sum);
  131. samples += incr;
  132. w++;
  133. /* we calculate two samples at the same time to avoid one memory
  134. access per two sample */
  135. for(j=1;j<16;j++) {
  136. sum2 = 0;
  137. p = synth_buf + 16 + j;
  138. SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
  139. p = synth_buf + 48 - j;
  140. SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
  141. *samples = round_sample(&sum);
  142. samples += incr;
  143. sum += sum2;
  144. *samples2 = round_sample(&sum);
  145. samples2 -= incr;
  146. w++;
  147. w2--;
  148. }
  149. p = synth_buf + 32;
  150. SUM8(MLSS, sum, w + 32, p);
  151. *samples = round_sample(&sum);
  152. *dither_state= sum;
  153. }
  154. /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
  155. 32 samples. */
  156. void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
  157. int *synth_buf_offset,
  158. MPA_INT *window, int *dither_state,
  159. OUT_INT *samples, ptrdiff_t incr,
  160. MPA_INT *sb_samples)
  161. {
  162. MPA_INT *synth_buf;
  163. int offset;
  164. offset = *synth_buf_offset;
  165. synth_buf = synth_buf_ptr + offset;
  166. s->RENAME(dct32)(synth_buf, sb_samples);
  167. s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
  168. offset = (offset - 32) & 511;
  169. *synth_buf_offset = offset;
  170. }
  171. static av_cold void mpa_synth_init(MPA_INT *window)
  172. {
  173. int i, j;
  174. /* max = 18760, max sum over all 16 coefs : 44736 */
  175. for(i=0;i<257;i++) {
  176. INTFLOAT v;
  177. v = ff_mpa_enwindow[i];
  178. #if USE_FLOATS
  179. v *= 1.0 / (1LL<<(16 + FRAC_BITS));
  180. #endif
  181. window[i] = v;
  182. if ((i & 63) != 0)
  183. v = -v;
  184. if (i != 0)
  185. window[512 - i] = v;
  186. }
  187. // Needed for avoiding shuffles in ASM implementations
  188. for(i=0; i < 8; i++)
  189. for(j=0; j < 16; j++)
  190. window[512+16*i+j] = window[64*i+32-j];
  191. for(i=0; i < 8; i++)
  192. for(j=0; j < 16; j++)
  193. window[512+128+16*i+j] = window[64*i+48-j];
  194. }
  195. static av_cold void mpa_synth_window_init(void)
  196. {
  197. mpa_synth_init(RENAME(ff_mpa_synth_window));
  198. }
  199. av_cold void RENAME(ff_mpa_synth_init)(void)
  200. {
  201. static AVOnce init_static_once = AV_ONCE_INIT;
  202. ff_thread_once(&init_static_once, mpa_synth_window_init);
  203. }
  204. /* cos(pi*i/18) */
  205. #define C1 FIXHR(0.98480775301220805936/2)
  206. #define C2 FIXHR(0.93969262078590838405/2)
  207. #define C3 FIXHR(0.86602540378443864676/2)
  208. #define C4 FIXHR(0.76604444311897803520/2)
  209. #define C5 FIXHR(0.64278760968653932632/2)
  210. #define C6 FIXHR(0.5/2)
  211. #define C7 FIXHR(0.34202014332566873304/2)
  212. #define C8 FIXHR(0.17364817766693034885/2)
  213. /* 0.5 / cos(pi*(2*i+1)/36) */
  214. static const INTFLOAT icos36[9] = {
  215. FIXR(0.50190991877167369479),
  216. FIXR(0.51763809020504152469), //0
  217. FIXR(0.55168895948124587824),
  218. FIXR(0.61038729438072803416),
  219. FIXR(0.70710678118654752439), //1
  220. FIXR(0.87172339781054900991),
  221. FIXR(1.18310079157624925896),
  222. FIXR(1.93185165257813657349), //2
  223. FIXR(5.73685662283492756461),
  224. };
  225. /* 0.5 / cos(pi*(2*i+1)/36) */
  226. static const INTFLOAT icos36h[9] = {
  227. FIXHR(0.50190991877167369479/2),
  228. FIXHR(0.51763809020504152469/2), //0
  229. FIXHR(0.55168895948124587824/2),
  230. FIXHR(0.61038729438072803416/2),
  231. FIXHR(0.70710678118654752439/2), //1
  232. FIXHR(0.87172339781054900991/2),
  233. FIXHR(1.18310079157624925896/4),
  234. FIXHR(1.93185165257813657349/4), //2
  235. // FIXHR(5.73685662283492756461),
  236. };
  237. /* using Lee like decomposition followed by hand coded 9 points DCT */
  238. static void imdct36(INTFLOAT *out, INTFLOAT *buf, SUINTFLOAT *in, INTFLOAT *win)
  239. {
  240. int i, j;
  241. SUINTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
  242. SUINTFLOAT tmp[18], *tmp1, *in1;
  243. for (i = 17; i >= 1; i--)
  244. in[i] += in[i-1];
  245. for (i = 17; i >= 3; i -= 2)
  246. in[i] += in[i-2];
  247. for (j = 0; j < 2; j++) {
  248. tmp1 = tmp + j;
  249. in1 = in + j;
  250. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  251. t3 = in1[2*0] + SHR(in1[2*6],1);
  252. t1 = in1[2*0] - in1[2*6];
  253. tmp1[ 6] = t1 - SHR(t2,1);
  254. tmp1[16] = t1 + t2;
  255. t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
  256. t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
  257. t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
  258. tmp1[10] = t3 - t0 - t2;
  259. tmp1[ 2] = t3 + t0 + t1;
  260. tmp1[14] = t3 + t2 - t1;
  261. tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
  262. t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
  263. t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
  264. t0 = MULH3(in1[2*3], C3, 2);
  265. t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
  266. tmp1[ 0] = t2 + t3 + t0;
  267. tmp1[12] = t2 + t1 - t0;
  268. tmp1[ 8] = t3 - t1 - t0;
  269. }
  270. i = 0;
  271. for (j = 0; j < 4; j++) {
  272. t0 = tmp[i];
  273. t1 = tmp[i + 2];
  274. s0 = t1 + t0;
  275. s2 = t1 - t0;
  276. t2 = tmp[i + 1];
  277. t3 = tmp[i + 3];
  278. s1 = MULH3(t3 + t2, icos36h[ j], 2);
  279. s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
  280. t0 = s0 + s1;
  281. t1 = s0 - s1;
  282. out[(9 + j) * SBLIMIT] = MULH3(t1, win[ 9 + j], 1) + buf[4*(9 + j)];
  283. out[(8 - j) * SBLIMIT] = MULH3(t1, win[ 8 - j], 1) + buf[4*(8 - j)];
  284. buf[4 * ( 9 + j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
  285. buf[4 * ( 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
  286. t0 = s2 + s3;
  287. t1 = s2 - s3;
  288. out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[ 9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
  289. out[ j * SBLIMIT] = MULH3(t1, win[ j], 1) + buf[4*( j)];
  290. buf[4 * ( 9 + 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
  291. buf[4 * ( j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + j], 1);
  292. i += 4;
  293. }
  294. s0 = tmp[16];
  295. s1 = MULH3(tmp[17], icos36h[4], 2);
  296. t0 = s0 + s1;
  297. t1 = s0 - s1;
  298. out[(9 + 4) * SBLIMIT] = MULH3(t1, win[ 9 + 4], 1) + buf[4*(9 + 4)];
  299. out[(8 - 4) * SBLIMIT] = MULH3(t1, win[ 8 - 4], 1) + buf[4*(8 - 4)];
  300. buf[4 * ( 9 + 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
  301. buf[4 * ( 8 - 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
  302. }
  303. void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
  304. int count, int switch_point, int block_type)
  305. {
  306. int j;
  307. for (j=0 ; j < count; j++) {
  308. /* apply window & overlap with previous buffer */
  309. /* select window */
  310. int win_idx = (switch_point && j < 2) ? 0 : block_type;
  311. INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
  312. imdct36(out, buf, in, win);
  313. in += 18;
  314. buf += ((j&3) != 3 ? 1 : (72-3));
  315. out++;
  316. }
  317. }