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.

206 lines
5.6KB

  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 MLSS(rt, ra, rb) rt-=(ra)*(rb)
  38. #else
  39. #define RENAME(n) n##_fixed
  40. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
  41. static inline int round_sample(int64_t *sum)
  42. {
  43. int sum1;
  44. sum1 = (int)((*sum) >> OUT_SHIFT);
  45. *sum &= (1<<OUT_SHIFT)-1;
  46. return av_clip_int16(sum1);
  47. }
  48. # define MULS(ra, rb) MUL64(ra, rb)
  49. # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
  50. # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
  51. #endif
  52. DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
  53. #define SUM8(op, sum, w, p) \
  54. { \
  55. op(sum, (w)[0 * 64], (p)[0 * 64]); \
  56. op(sum, (w)[1 * 64], (p)[1 * 64]); \
  57. op(sum, (w)[2 * 64], (p)[2 * 64]); \
  58. op(sum, (w)[3 * 64], (p)[3 * 64]); \
  59. op(sum, (w)[4 * 64], (p)[4 * 64]); \
  60. op(sum, (w)[5 * 64], (p)[5 * 64]); \
  61. op(sum, (w)[6 * 64], (p)[6 * 64]); \
  62. op(sum, (w)[7 * 64], (p)[7 * 64]); \
  63. }
  64. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  65. { \
  66. INTFLOAT tmp;\
  67. tmp = p[0 * 64];\
  68. op1(sum1, (w1)[0 * 64], tmp);\
  69. op2(sum2, (w2)[0 * 64], tmp);\
  70. tmp = p[1 * 64];\
  71. op1(sum1, (w1)[1 * 64], tmp);\
  72. op2(sum2, (w2)[1 * 64], tmp);\
  73. tmp = p[2 * 64];\
  74. op1(sum1, (w1)[2 * 64], tmp);\
  75. op2(sum2, (w2)[2 * 64], tmp);\
  76. tmp = p[3 * 64];\
  77. op1(sum1, (w1)[3 * 64], tmp);\
  78. op2(sum2, (w2)[3 * 64], tmp);\
  79. tmp = p[4 * 64];\
  80. op1(sum1, (w1)[4 * 64], tmp);\
  81. op2(sum2, (w2)[4 * 64], tmp);\
  82. tmp = p[5 * 64];\
  83. op1(sum1, (w1)[5 * 64], tmp);\
  84. op2(sum2, (w2)[5 * 64], tmp);\
  85. tmp = p[6 * 64];\
  86. op1(sum1, (w1)[6 * 64], tmp);\
  87. op2(sum2, (w2)[6 * 64], tmp);\
  88. tmp = p[7 * 64];\
  89. op1(sum1, (w1)[7 * 64], tmp);\
  90. op2(sum2, (w2)[7 * 64], tmp);\
  91. }
  92. void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
  93. int *dither_state, OUT_INT *samples,
  94. int incr)
  95. {
  96. register const MPA_INT *w, *w2, *p;
  97. int j;
  98. OUT_INT *samples2;
  99. #if CONFIG_FLOAT
  100. float sum, sum2;
  101. #else
  102. int64_t sum, sum2;
  103. #endif
  104. /* copy to avoid wrap */
  105. memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
  106. samples2 = samples + 31 * incr;
  107. w = window;
  108. w2 = window + 31;
  109. sum = *dither_state;
  110. p = synth_buf + 16;
  111. SUM8(MACS, sum, w, p);
  112. p = synth_buf + 48;
  113. SUM8(MLSS, sum, w + 32, p);
  114. *samples = round_sample(&sum);
  115. samples += incr;
  116. w++;
  117. /* we calculate two samples at the same time to avoid one memory
  118. access per two sample */
  119. for(j=1;j<16;j++) {
  120. sum2 = 0;
  121. p = synth_buf + 16 + j;
  122. SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
  123. p = synth_buf + 48 - j;
  124. SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
  125. *samples = round_sample(&sum);
  126. samples += incr;
  127. sum += sum2;
  128. *samples2 = round_sample(&sum);
  129. samples2 -= incr;
  130. w++;
  131. w2--;
  132. }
  133. p = synth_buf + 32;
  134. SUM8(MLSS, sum, w + 32, p);
  135. *samples = round_sample(&sum);
  136. *dither_state= sum;
  137. }
  138. /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
  139. 32 samples. */
  140. void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
  141. int *synth_buf_offset,
  142. MPA_INT *window, int *dither_state,
  143. OUT_INT *samples, int incr,
  144. MPA_INT *sb_samples)
  145. {
  146. MPA_INT *synth_buf;
  147. int offset;
  148. offset = *synth_buf_offset;
  149. synth_buf = synth_buf_ptr + offset;
  150. s->RENAME(dct32)(synth_buf, sb_samples);
  151. s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
  152. offset = (offset - 32) & 511;
  153. *synth_buf_offset = offset;
  154. }
  155. void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
  156. {
  157. int i, j;
  158. /* max = 18760, max sum over all 16 coefs : 44736 */
  159. for(i=0;i<257;i++) {
  160. INTFLOAT v;
  161. v = ff_mpa_enwindow[i];
  162. #if CONFIG_FLOAT
  163. v *= 1.0 / (1LL<<(16 + FRAC_BITS));
  164. #endif
  165. window[i] = v;
  166. if ((i & 63) != 0)
  167. v = -v;
  168. if (i != 0)
  169. window[512 - i] = v;
  170. }
  171. // Needed for avoiding shuffles in ASM implementations
  172. for(i=0; i < 8; i++)
  173. for(j=0; j < 16; j++)
  174. window[512+16*i+j] = window[64*i+32-j];
  175. for(i=0; i < 8; i++)
  176. for(j=0; j < 16; j++)
  177. window[512+128+16*i+j] = window[64*i+48-j];
  178. }