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.

293 lines
9.6KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/intfloat.h"
  25. #include "sbrdsp.h"
  26. static void sbr_sum64x5_c(float *z)
  27. {
  28. int k;
  29. for (k = 0; k < 64; k++) {
  30. float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
  31. z[k] = f;
  32. }
  33. }
  34. static float sbr_sum_square_c(float (*x)[2], int n)
  35. {
  36. float sum0 = 0.0f, sum1 = 0.0f;
  37. int i;
  38. for (i = 0; i < n; i += 2)
  39. {
  40. sum0 += x[i + 0][0] * x[i + 0][0];
  41. sum1 += x[i + 0][1] * x[i + 0][1];
  42. sum0 += x[i + 1][0] * x[i + 1][0];
  43. sum1 += x[i + 1][1] * x[i + 1][1];
  44. }
  45. return sum0 + sum1;
  46. }
  47. static void sbr_neg_odd_64_c(float *x)
  48. {
  49. union av_intfloat32 *xi = (union av_intfloat32*) x;
  50. int i;
  51. for (i = 1; i < 64; i += 4) {
  52. xi[i + 0].i ^= 1U << 31;
  53. xi[i + 2].i ^= 1U << 31;
  54. }
  55. }
  56. static void sbr_qmf_pre_shuffle_c(float *z)
  57. {
  58. union av_intfloat32 *zi = (union av_intfloat32*) z;
  59. int k;
  60. zi[64].i = zi[0].i;
  61. zi[65].i = zi[1].i;
  62. for (k = 1; k < 31; k += 2) {
  63. zi[64 + 2 * k + 0].i = zi[64 - k].i ^ (1U << 31);
  64. zi[64 + 2 * k + 1].i = zi[ k + 1].i;
  65. zi[64 + 2 * k + 2].i = zi[63 - k].i ^ (1U << 31);
  66. zi[64 + 2 * k + 3].i = zi[ k + 2].i;
  67. }
  68. zi[64 + 2 * 31 + 0].i = zi[64 - 31].i ^ (1U << 31);
  69. zi[64 + 2 * 31 + 1].i = zi[31 + 1].i;
  70. }
  71. static void sbr_qmf_post_shuffle_c(float W[32][2], const float *z)
  72. {
  73. const union av_intfloat32 *zi = (const union av_intfloat32*) z;
  74. union av_intfloat32 *Wi = (union av_intfloat32*) W;
  75. int k;
  76. for (k = 0; k < 32; k += 2) {
  77. Wi[2 * k + 0].i = zi[63 - k].i ^ (1U << 31);
  78. Wi[2 * k + 1].i = zi[ k + 0].i;
  79. Wi[2 * k + 2].i = zi[62 - k].i ^ (1U << 31);
  80. Wi[2 * k + 3].i = zi[ k + 1].i;
  81. }
  82. }
  83. static void sbr_qmf_deint_neg_c(float *v, const float *src)
  84. {
  85. const union av_intfloat32 *si = (const union av_intfloat32*)src;
  86. union av_intfloat32 *vi = (union av_intfloat32*)v;
  87. int i;
  88. for (i = 0; i < 32; i++) {
  89. vi[ i].i = si[63 - 2 * i ].i;
  90. vi[63 - i].i = si[63 - 2 * i - 1].i ^ (1U << 31);
  91. }
  92. }
  93. static void sbr_qmf_deint_bfly_c(float *v, const float *src0, const float *src1)
  94. {
  95. int i;
  96. for (i = 0; i < 64; i++) {
  97. v[ i] = src0[i] - src1[63 - i];
  98. v[127 - i] = src0[i] + src1[63 - i];
  99. }
  100. }
  101. #if 0
  102. /* This code is slower because it multiplies memory accesses.
  103. * It is left for educational purposes and because it may offer
  104. * a better reference for writing arch-specific DSP functions. */
  105. static av_always_inline void autocorrelate(const float x[40][2],
  106. float phi[3][2][2], int lag)
  107. {
  108. int i;
  109. float real_sum = 0.0f;
  110. float imag_sum = 0.0f;
  111. if (lag) {
  112. for (i = 1; i < 38; i++) {
  113. real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1];
  114. imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0];
  115. }
  116. phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1];
  117. phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0];
  118. if (lag == 1) {
  119. phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1];
  120. phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0];
  121. }
  122. } else {
  123. for (i = 1; i < 38; i++) {
  124. real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1];
  125. }
  126. phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
  127. phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1];
  128. }
  129. }
  130. static void sbr_autocorrelate_c(const float x[40][2], float phi[3][2][2])
  131. {
  132. autocorrelate(x, phi, 0);
  133. autocorrelate(x, phi, 1);
  134. autocorrelate(x, phi, 2);
  135. }
  136. #else
  137. static void sbr_autocorrelate_c(const float x[40][2], float phi[3][2][2])
  138. {
  139. float real_sum2 = x[0][0] * x[2][0] + x[0][1] * x[2][1];
  140. float imag_sum2 = x[0][0] * x[2][1] - x[0][1] * x[2][0];
  141. float real_sum1 = 0.0f, imag_sum1 = 0.0f, real_sum0 = 0.0f;
  142. int i;
  143. for (i = 1; i < 38; i++) {
  144. real_sum0 += x[i][0] * x[i ][0] + x[i][1] * x[i ][1];
  145. real_sum1 += x[i][0] * x[i + 1][0] + x[i][1] * x[i + 1][1];
  146. imag_sum1 += x[i][0] * x[i + 1][1] - x[i][1] * x[i + 1][0];
  147. real_sum2 += x[i][0] * x[i + 2][0] + x[i][1] * x[i + 2][1];
  148. imag_sum2 += x[i][0] * x[i + 2][1] - x[i][1] * x[i + 2][0];
  149. }
  150. phi[2 - 2][1][0] = real_sum2;
  151. phi[2 - 2][1][1] = imag_sum2;
  152. phi[2 ][1][0] = real_sum0 + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
  153. phi[1 ][0][0] = real_sum0 + x[38][0] * x[38][0] + x[38][1] * x[38][1];
  154. phi[2 - 1][1][0] = real_sum1 + x[ 0][0] * x[ 1][0] + x[ 0][1] * x[ 1][1];
  155. phi[2 - 1][1][1] = imag_sum1 + x[ 0][0] * x[ 1][1] - x[ 0][1] * x[ 1][0];
  156. phi[0 ][0][0] = real_sum1 + x[38][0] * x[39][0] + x[38][1] * x[39][1];
  157. phi[0 ][0][1] = imag_sum1 + x[38][0] * x[39][1] - x[38][1] * x[39][0];
  158. }
  159. #endif
  160. static void sbr_hf_gen_c(float (*X_high)[2], const float (*X_low)[2],
  161. const float alpha0[2], const float alpha1[2],
  162. float bw, int start, int end)
  163. {
  164. float alpha[4];
  165. int i;
  166. alpha[0] = alpha1[0] * bw * bw;
  167. alpha[1] = alpha1[1] * bw * bw;
  168. alpha[2] = alpha0[0] * bw;
  169. alpha[3] = alpha0[1] * bw;
  170. for (i = start; i < end; i++) {
  171. X_high[i][0] =
  172. X_low[i - 2][0] * alpha[0] -
  173. X_low[i - 2][1] * alpha[1] +
  174. X_low[i - 1][0] * alpha[2] -
  175. X_low[i - 1][1] * alpha[3] +
  176. X_low[i][0];
  177. X_high[i][1] =
  178. X_low[i - 2][1] * alpha[0] +
  179. X_low[i - 2][0] * alpha[1] +
  180. X_low[i - 1][1] * alpha[2] +
  181. X_low[i - 1][0] * alpha[3] +
  182. X_low[i][1];
  183. }
  184. }
  185. static void sbr_hf_g_filt_c(float (*Y)[2], const float (*X_high)[40][2],
  186. const float *g_filt, int m_max, intptr_t ixh)
  187. {
  188. int m;
  189. for (m = 0; m < m_max; m++) {
  190. Y[m][0] = X_high[m][ixh][0] * g_filt[m];
  191. Y[m][1] = X_high[m][ixh][1] * g_filt[m];
  192. }
  193. }
  194. static av_always_inline void sbr_hf_apply_noise(float (*Y)[2],
  195. const float *s_m,
  196. const float *q_filt,
  197. int noise,
  198. float phi_sign0,
  199. float phi_sign1,
  200. int m_max)
  201. {
  202. int m;
  203. for (m = 0; m < m_max; m++) {
  204. float y0 = Y[m][0];
  205. float y1 = Y[m][1];
  206. noise = (noise + 1) & 0x1ff;
  207. if (s_m[m]) {
  208. y0 += s_m[m] * phi_sign0;
  209. y1 += s_m[m] * phi_sign1;
  210. } else {
  211. y0 += q_filt[m] * ff_sbr_noise_table[noise][0];
  212. y1 += q_filt[m] * ff_sbr_noise_table[noise][1];
  213. }
  214. Y[m][0] = y0;
  215. Y[m][1] = y1;
  216. phi_sign1 = -phi_sign1;
  217. }
  218. }
  219. static void sbr_hf_apply_noise_0(float (*Y)[2], const float *s_m,
  220. const float *q_filt, int noise,
  221. int kx, int m_max)
  222. {
  223. sbr_hf_apply_noise(Y, s_m, q_filt, noise, 1.0, 0.0, m_max);
  224. }
  225. static void sbr_hf_apply_noise_1(float (*Y)[2], const float *s_m,
  226. const float *q_filt, int noise,
  227. int kx, int m_max)
  228. {
  229. float phi_sign = 1 - 2 * (kx & 1);
  230. sbr_hf_apply_noise(Y, s_m, q_filt, noise, 0.0, phi_sign, m_max);
  231. }
  232. static void sbr_hf_apply_noise_2(float (*Y)[2], const float *s_m,
  233. const float *q_filt, int noise,
  234. int kx, int m_max)
  235. {
  236. sbr_hf_apply_noise(Y, s_m, q_filt, noise, -1.0, 0.0, m_max);
  237. }
  238. static void sbr_hf_apply_noise_3(float (*Y)[2], const float *s_m,
  239. const float *q_filt, int noise,
  240. int kx, int m_max)
  241. {
  242. float phi_sign = 1 - 2 * (kx & 1);
  243. sbr_hf_apply_noise(Y, s_m, q_filt, noise, 0.0, -phi_sign, m_max);
  244. }
  245. av_cold void ff_sbrdsp_init(SBRDSPContext *s)
  246. {
  247. s->sum64x5 = sbr_sum64x5_c;
  248. s->sum_square = sbr_sum_square_c;
  249. s->neg_odd_64 = sbr_neg_odd_64_c;
  250. s->qmf_pre_shuffle = sbr_qmf_pre_shuffle_c;
  251. s->qmf_post_shuffle = sbr_qmf_post_shuffle_c;
  252. s->qmf_deint_neg = sbr_qmf_deint_neg_c;
  253. s->qmf_deint_bfly = sbr_qmf_deint_bfly_c;
  254. s->autocorrelate = sbr_autocorrelate_c;
  255. s->hf_gen = sbr_hf_gen_c;
  256. s->hf_g_filt = sbr_hf_g_filt_c;
  257. s->hf_apply_noise[0] = sbr_hf_apply_noise_0;
  258. s->hf_apply_noise[1] = sbr_hf_apply_noise_1;
  259. s->hf_apply_noise[2] = sbr_hf_apply_noise_2;
  260. s->hf_apply_noise[3] = sbr_hf_apply_noise_3;
  261. if (ARCH_ARM)
  262. ff_sbrdsp_init_arm(s);
  263. if (ARCH_X86)
  264. ff_sbrdsp_init_x86(s);
  265. }