sbrdsp_fixed.c 9.4 KB

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