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.

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