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.

287 lines
8.3KB

  1. /*
  2. * AAC Spectral Band Replication decoding functions
  3. * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
  4. * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Note: Rounding-to-nearest used unless otherwise stated
  23. *
  24. */
  25. #define USE_FIXED 1
  26. #include "aac.h"
  27. #include "config.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/intfloat.h"
  30. #include "sbrdsp.h"
  31. static SoftFloat sbr_sum_square_c(int (*x)[2], int n)
  32. {
  33. SoftFloat ret;
  34. int64_t accu = 0;
  35. int i, nz, round;
  36. for (i = 0; i < n; i += 2) {
  37. accu += (int64_t)x[i + 0][0] * x[i + 0][0];
  38. accu += (int64_t)x[i + 0][1] * x[i + 0][1];
  39. accu += (int64_t)x[i + 1][0] * x[i + 1][0];
  40. accu += (int64_t)x[i + 1][1] * x[i + 1][1];
  41. }
  42. i = (int)(accu >> 32);
  43. if (i == 0) {
  44. nz = 1;
  45. } else {
  46. nz = 0;
  47. while (FFABS(i) < 0x40000000) {
  48. i <<= 1;
  49. nz++;
  50. }
  51. nz = 32 - nz;
  52. }
  53. round = 1 << (nz-1);
  54. i = (int)((accu + round) >> nz);
  55. i >>= 1;
  56. ret = av_int2sf(i, 15 - nz);
  57. return ret;
  58. }
  59. static void sbr_neg_odd_64_c(int *x)
  60. {
  61. int i;
  62. for (i = 1; i < 64; i += 2)
  63. x[i] = -x[i];
  64. }
  65. static void sbr_qmf_pre_shuffle_c(int *z)
  66. {
  67. int k;
  68. z[64] = z[0];
  69. z[65] = z[1];
  70. for (k = 1; k < 32; k++) {
  71. z[64+2*k ] = -z[64 - k];
  72. z[64+2*k+1] = z[ k + 1];
  73. }
  74. }
  75. static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
  76. {
  77. int k;
  78. for (k = 0; k < 32; k++) {
  79. W[k][0] = -z[63-k];
  80. W[k][1] = z[k];
  81. }
  82. }
  83. static void sbr_qmf_deint_neg_c(int *v, const int *src)
  84. {
  85. int i;
  86. for (i = 0; i < 32; i++) {
  87. v[ i] = ( src[63 - 2*i ] + 0x10) >> 5;
  88. v[63 - i] = (-src[63 - 2*i - 1] + 0x10) >> 5;
  89. }
  90. }
  91. static av_always_inline SoftFloat autocorr_calc(int64_t accu)
  92. {
  93. int nz, mant, expo, round;
  94. int i = (int)(accu >> 32);
  95. if (i == 0) {
  96. nz = 1;
  97. } else {
  98. nz = 0;
  99. while (FFABS(i) < 0x40000000) {
  100. i <<= 1;
  101. nz++;
  102. }
  103. nz = 32-nz;
  104. }
  105. round = 1 << (nz-1);
  106. mant = (int)((accu + round) >> nz);
  107. mant = (mant + 0x40)>>7;
  108. mant <<= 6;
  109. expo = nz + 15;
  110. return av_int2sf(mant, 30 - expo);
  111. }
  112. static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
  113. {
  114. int i;
  115. int64_t real_sum, imag_sum;
  116. int64_t accu_re = 0, accu_im = 0;
  117. if (lag) {
  118. for (i = 1; i < 38; i++) {
  119. accu_re += (int64_t)x[i][0] * x[i+lag][0];
  120. accu_re += (int64_t)x[i][1] * x[i+lag][1];
  121. accu_im += (int64_t)x[i][0] * x[i+lag][1];
  122. accu_im -= (int64_t)x[i][1] * x[i+lag][0];
  123. }
  124. real_sum = accu_re;
  125. imag_sum = accu_im;
  126. accu_re += (int64_t)x[ 0][0] * x[lag][0];
  127. accu_re += (int64_t)x[ 0][1] * x[lag][1];
  128. accu_im += (int64_t)x[ 0][0] * x[lag][1];
  129. accu_im -= (int64_t)x[ 0][1] * x[lag][0];
  130. phi[2-lag][1][0] = autocorr_calc(accu_re);
  131. phi[2-lag][1][1] = autocorr_calc(accu_im);
  132. if (lag == 1) {
  133. accu_re = real_sum;
  134. accu_im = imag_sum;
  135. accu_re += (int64_t)x[38][0] * x[39][0];
  136. accu_re += (int64_t)x[38][1] * x[39][1];
  137. accu_im += (int64_t)x[38][0] * x[39][1];
  138. accu_im -= (int64_t)x[38][1] * x[39][0];
  139. phi[0][0][0] = autocorr_calc(accu_re);
  140. phi[0][0][1] = autocorr_calc(accu_im);
  141. }
  142. } else {
  143. for (i = 1; i < 38; i++) {
  144. accu_re += (int64_t)x[i][0] * x[i][0];
  145. accu_re += (int64_t)x[i][1] * x[i][1];
  146. }
  147. real_sum = accu_re;
  148. accu_re += (int64_t)x[ 0][0] * x[ 0][0];
  149. accu_re += (int64_t)x[ 0][1] * x[ 0][1];
  150. phi[2][1][0] = autocorr_calc(accu_re);
  151. accu_re = real_sum;
  152. accu_re += (int64_t)x[38][0] * x[38][0];
  153. accu_re += (int64_t)x[38][1] * x[38][1];
  154. phi[1][0][0] = autocorr_calc(accu_re);
  155. }
  156. }
  157. static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
  158. {
  159. autocorrelate(x, phi, 0);
  160. autocorrelate(x, phi, 1);
  161. autocorrelate(x, phi, 2);
  162. }
  163. static void sbr_hf_gen_c(int (*X_high)[2], const int (*X_low)[2],
  164. const int alpha0[2], const int alpha1[2],
  165. int bw, int start, int end)
  166. {
  167. int alpha[4];
  168. int i;
  169. int64_t accu;
  170. accu = (int64_t)alpha0[0] * bw;
  171. alpha[2] = (int)((accu + 0x40000000) >> 31);
  172. accu = (int64_t)alpha0[1] * bw;
  173. alpha[3] = (int)((accu + 0x40000000) >> 31);
  174. accu = (int64_t)bw * bw;
  175. bw = (int)((accu + 0x40000000) >> 31);
  176. accu = (int64_t)alpha1[0] * bw;
  177. alpha[0] = (int)((accu + 0x40000000) >> 31);
  178. accu = (int64_t)alpha1[1] * bw;
  179. alpha[1] = (int)((accu + 0x40000000) >> 31);
  180. for (i = start; i < end; i++) {
  181. accu = (int64_t)X_low[i][0] * 0x20000000;
  182. accu += (int64_t)X_low[i - 2][0] * alpha[0];
  183. accu -= (int64_t)X_low[i - 2][1] * alpha[1];
  184. accu += (int64_t)X_low[i - 1][0] * alpha[2];
  185. accu -= (int64_t)X_low[i - 1][1] * alpha[3];
  186. X_high[i][0] = (int)((accu + 0x10000000) >> 29);
  187. accu = (int64_t)X_low[i][1] * 0x20000000;
  188. accu += (int64_t)X_low[i - 2][1] * alpha[0];
  189. accu += (int64_t)X_low[i - 2][0] * alpha[1];
  190. accu += (int64_t)X_low[i - 1][1] * alpha[2];
  191. accu += (int64_t)X_low[i - 1][0] * alpha[3];
  192. X_high[i][1] = (int)((accu + 0x10000000) >> 29);
  193. }
  194. }
  195. static void sbr_hf_g_filt_c(int (*Y)[2], const int (*X_high)[40][2],
  196. const SoftFloat *g_filt, int m_max, intptr_t ixh)
  197. {
  198. int m, r;
  199. int64_t accu;
  200. for (m = 0; m < m_max; m++) {
  201. r = 1 << (22-g_filt[m].exp);
  202. accu = (int64_t)X_high[m][ixh][0] * ((g_filt[m].mant + 0x40)>>7);
  203. Y[m][0] = (int)((accu + r) >> (23-g_filt[m].exp));
  204. accu = (int64_t)X_high[m][ixh][1] * ((g_filt[m].mant + 0x40)>>7);
  205. Y[m][1] = (int)((accu + r) >> (23-g_filt[m].exp));
  206. }
  207. }
  208. static av_always_inline void sbr_hf_apply_noise(int (*Y)[2],
  209. const SoftFloat *s_m,
  210. const SoftFloat *q_filt,
  211. int noise,
  212. int phi_sign0,
  213. int phi_sign1,
  214. int m_max)
  215. {
  216. int m;
  217. for (m = 0; m < m_max; m++) {
  218. int y0 = Y[m][0];
  219. int y1 = Y[m][1];
  220. noise = (noise + 1) & 0x1ff;
  221. if (s_m[m].mant) {
  222. int shift, round;
  223. shift = 22 - s_m[m].exp;
  224. if (shift < 30) {
  225. round = 1 << (shift-1);
  226. y0 += (s_m[m].mant * phi_sign0 + round) >> shift;
  227. y1 += (s_m[m].mant * phi_sign1 + round) >> shift;
  228. }
  229. } else {
  230. int shift, round, tmp;
  231. int64_t accu;
  232. shift = 22 - q_filt[m].exp;
  233. if (shift < 30) {
  234. round = 1 << (shift-1);
  235. accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][0];
  236. tmp = (int)((accu + 0x40000000) >> 31);
  237. y0 += (tmp + round) >> shift;
  238. accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][1];
  239. tmp = (int)((accu + 0x40000000) >> 31);
  240. y1 += (tmp + round) >> shift;
  241. }
  242. }
  243. Y[m][0] = y0;
  244. Y[m][1] = y1;
  245. phi_sign1 = -phi_sign1;
  246. }
  247. }
  248. #include "sbrdsp_template.c"