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.

301 lines
9.0KB

  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. // Larger values are inavlid and could cause overflows of accu.
  38. av_assert2(FFABS(x[i + 0][0]) >> 29 == 0);
  39. accu += (int64_t)x[i + 0][0] * x[i + 0][0];
  40. av_assert2(FFABS(x[i + 0][1]) >> 29 == 0);
  41. accu += (int64_t)x[i + 0][1] * x[i + 0][1];
  42. av_assert2(FFABS(x[i + 1][0]) >> 29 == 0);
  43. accu += (int64_t)x[i + 1][0] * x[i + 1][0];
  44. av_assert2(FFABS(x[i + 1][1]) >> 29 == 0);
  45. accu += (int64_t)x[i + 1][1] * x[i + 1][1];
  46. }
  47. i = (int)(accu >> 32);
  48. if (i == 0) {
  49. nz = 1;
  50. } else {
  51. nz = 0;
  52. while (FFABS(i) < 0x40000000) {
  53. i <<= 1;
  54. nz++;
  55. }
  56. nz = 32 - nz;
  57. }
  58. round = 1 << (nz-1);
  59. i = (int)((accu + round) >> nz);
  60. i >>= 1;
  61. ret = av_int2sf(i, 15 - nz);
  62. return ret;
  63. }
  64. static void sbr_neg_odd_64_c(int *x)
  65. {
  66. int i;
  67. for (i = 1; i < 64; i += 2)
  68. x[i] = -x[i];
  69. }
  70. static void sbr_qmf_pre_shuffle_c(int *z)
  71. {
  72. int k;
  73. z[64] = z[0];
  74. z[65] = z[1];
  75. for (k = 1; k < 32; k++) {
  76. z[64+2*k ] = -z[64 - k];
  77. z[64+2*k+1] = z[ k + 1];
  78. }
  79. }
  80. static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
  81. {
  82. int k;
  83. for (k = 0; k < 32; k++) {
  84. W[k][0] = -z[63-k];
  85. W[k][1] = z[k];
  86. }
  87. }
  88. static void sbr_qmf_deint_neg_c(int *v, const int *src)
  89. {
  90. int i;
  91. for (i = 0; i < 32; i++) {
  92. v[ i] = ( src[63 - 2*i ] + 0x10) >> 5;
  93. v[63 - i] = (-src[63 - 2*i - 1] + 0x10) >> 5;
  94. }
  95. }
  96. static av_always_inline SoftFloat autocorr_calc(int64_t accu)
  97. {
  98. int nz, mant, expo, round;
  99. int i = (int)(accu >> 32);
  100. if (i == 0) {
  101. nz = 1;
  102. } else {
  103. nz = 0;
  104. while (FFABS(i) < 0x40000000) {
  105. i <<= 1;
  106. nz++;
  107. }
  108. nz = 32-nz;
  109. }
  110. round = 1 << (nz-1);
  111. mant = (int)((accu + round) >> nz);
  112. mant = (mant + 0x40)>>7;
  113. mant <<= 6;
  114. expo = nz + 15;
  115. return av_int2sf(mant, 30 - expo);
  116. }
  117. static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
  118. {
  119. int i;
  120. int64_t real_sum, imag_sum;
  121. int64_t accu_re = 0, accu_im = 0;
  122. if (lag) {
  123. for (i = 1; i < 38; i++) {
  124. accu_re += (uint64_t)x[i][0] * x[i+lag][0];
  125. accu_re += (uint64_t)x[i][1] * x[i+lag][1];
  126. accu_im += (uint64_t)x[i][0] * x[i+lag][1];
  127. accu_im -= (uint64_t)x[i][1] * x[i+lag][0];
  128. }
  129. real_sum = accu_re;
  130. imag_sum = accu_im;
  131. accu_re += (uint64_t)x[ 0][0] * x[lag][0];
  132. accu_re += (uint64_t)x[ 0][1] * x[lag][1];
  133. accu_im += (uint64_t)x[ 0][0] * x[lag][1];
  134. accu_im -= (uint64_t)x[ 0][1] * x[lag][0];
  135. phi[2-lag][1][0] = autocorr_calc(accu_re);
  136. phi[2-lag][1][1] = autocorr_calc(accu_im);
  137. if (lag == 1) {
  138. accu_re = real_sum;
  139. accu_im = imag_sum;
  140. accu_re += (uint64_t)x[38][0] * x[39][0];
  141. accu_re += (uint64_t)x[38][1] * x[39][1];
  142. accu_im += (uint64_t)x[38][0] * x[39][1];
  143. accu_im -= (uint64_t)x[38][1] * x[39][0];
  144. phi[0][0][0] = autocorr_calc(accu_re);
  145. phi[0][0][1] = autocorr_calc(accu_im);
  146. }
  147. } else {
  148. for (i = 1; i < 38; i++) {
  149. accu_re += (uint64_t)x[i][0] * x[i][0];
  150. accu_re += (uint64_t)x[i][1] * x[i][1];
  151. }
  152. real_sum = accu_re;
  153. accu_re += (uint64_t)x[ 0][0] * x[ 0][0];
  154. accu_re += (uint64_t)x[ 0][1] * x[ 0][1];
  155. phi[2][1][0] = autocorr_calc(accu_re);
  156. accu_re = real_sum;
  157. accu_re += (uint64_t)x[38][0] * x[38][0];
  158. accu_re += (uint64_t)x[38][1] * x[38][1];
  159. phi[1][0][0] = autocorr_calc(accu_re);
  160. }
  161. }
  162. static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
  163. {
  164. autocorrelate(x, phi, 0);
  165. autocorrelate(x, phi, 1);
  166. autocorrelate(x, phi, 2);
  167. }
  168. static void sbr_hf_gen_c(int (*X_high)[2], const int (*X_low)[2],
  169. const int alpha0[2], const int alpha1[2],
  170. int bw, int start, int end)
  171. {
  172. int alpha[4];
  173. int i;
  174. int64_t accu;
  175. accu = (int64_t)alpha0[0] * bw;
  176. alpha[2] = (int)((accu + 0x40000000) >> 31);
  177. accu = (int64_t)alpha0[1] * bw;
  178. alpha[3] = (int)((accu + 0x40000000) >> 31);
  179. accu = (int64_t)bw * bw;
  180. bw = (int)((accu + 0x40000000) >> 31);
  181. accu = (int64_t)alpha1[0] * bw;
  182. alpha[0] = (int)((accu + 0x40000000) >> 31);
  183. accu = (int64_t)alpha1[1] * bw;
  184. alpha[1] = (int)((accu + 0x40000000) >> 31);
  185. for (i = start; i < end; i++) {
  186. accu = (int64_t)X_low[i][0] * 0x20000000;
  187. accu += (int64_t)X_low[i - 2][0] * alpha[0];
  188. accu -= (int64_t)X_low[i - 2][1] * alpha[1];
  189. accu += (int64_t)X_low[i - 1][0] * alpha[2];
  190. accu -= (int64_t)X_low[i - 1][1] * alpha[3];
  191. X_high[i][0] = (int)((accu + 0x10000000) >> 29);
  192. accu = (int64_t)X_low[i][1] * 0x20000000;
  193. accu += (int64_t)X_low[i - 2][1] * alpha[0];
  194. accu += (int64_t)X_low[i - 2][0] * alpha[1];
  195. accu += (int64_t)X_low[i - 1][1] * alpha[2];
  196. accu += (int64_t)X_low[i - 1][0] * alpha[3];
  197. X_high[i][1] = (int)((accu + 0x10000000) >> 29);
  198. }
  199. }
  200. static void sbr_hf_g_filt_c(int (*Y)[2], const int (*X_high)[40][2],
  201. const SoftFloat *g_filt, int m_max, intptr_t ixh)
  202. {
  203. int m;
  204. int64_t accu;
  205. for (m = 0; m < m_max; m++) {
  206. if (22 - g_filt[m].exp < 61) {
  207. int64_t r = 1LL << (22-g_filt[m].exp);
  208. accu = (int64_t)X_high[m][ixh][0] * ((g_filt[m].mant + 0x40)>>7);
  209. Y[m][0] = (int)((accu + r) >> (23-g_filt[m].exp));
  210. accu = (int64_t)X_high[m][ixh][1] * ((g_filt[m].mant + 0x40)>>7);
  211. Y[m][1] = (int)((accu + r) >> (23-g_filt[m].exp));
  212. }
  213. }
  214. }
  215. static av_always_inline int sbr_hf_apply_noise(int (*Y)[2],
  216. const SoftFloat *s_m,
  217. const SoftFloat *q_filt,
  218. int noise,
  219. int phi_sign0,
  220. int phi_sign1,
  221. int m_max)
  222. {
  223. int m;
  224. for (m = 0; m < m_max; m++) {
  225. unsigned y0 = Y[m][0];
  226. unsigned y1 = Y[m][1];
  227. noise = (noise + 1) & 0x1ff;
  228. if (s_m[m].mant) {
  229. int shift, round;
  230. shift = 22 - s_m[m].exp;
  231. if (shift < 1) {
  232. av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
  233. return AVERROR(ERANGE);
  234. } else if (shift < 30) {
  235. round = 1 << (shift-1);
  236. y0 += (s_m[m].mant * phi_sign0 + round) >> shift;
  237. y1 += (s_m[m].mant * phi_sign1 + round) >> shift;
  238. }
  239. } else {
  240. int shift, round, tmp;
  241. int64_t accu;
  242. shift = 22 - q_filt[m].exp;
  243. if (shift < 1) {
  244. av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
  245. return AVERROR(ERANGE);
  246. } else if (shift < 30) {
  247. round = 1 << (shift-1);
  248. accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][0];
  249. tmp = (int)((accu + 0x40000000) >> 31);
  250. y0 += (tmp + round) >> shift;
  251. accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][1];
  252. tmp = (int)((accu + 0x40000000) >> 31);
  253. y1 += (tmp + round) >> shift;
  254. }
  255. }
  256. Y[m][0] = y0;
  257. Y[m][1] = y1;
  258. phi_sign1 = -phi_sign1;
  259. }
  260. return 0;
  261. }
  262. #include "sbrdsp_template.c"