sbrdsp.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #define USE_FIXED 0
  23. #include "aac.h"
  24. #include "config.h"
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/intfloat.h"
  27. #include "sbrdsp.h"
  28. static float sbr_sum_square_c(float (*x)[2], int n)
  29. {
  30. float sum0 = 0.0f, sum1 = 0.0f;
  31. int i;
  32. for (i = 0; i < n; i += 2)
  33. {
  34. sum0 += x[i + 0][0] * x[i + 0][0];
  35. sum1 += x[i + 0][1] * x[i + 0][1];
  36. sum0 += x[i + 1][0] * x[i + 1][0];
  37. sum1 += x[i + 1][1] * x[i + 1][1];
  38. }
  39. return sum0 + sum1;
  40. }
  41. static void sbr_neg_odd_64_c(float *x)
  42. {
  43. union av_intfloat32 *xi = (union av_intfloat32*) x;
  44. int i;
  45. for (i = 1; i < 64; i += 4) {
  46. xi[i + 0].i ^= 1U << 31;
  47. xi[i + 2].i ^= 1U << 31;
  48. }
  49. }
  50. static void sbr_qmf_pre_shuffle_c(float *z)
  51. {
  52. union av_intfloat32 *zi = (union av_intfloat32*) z;
  53. int k;
  54. zi[64].i = zi[0].i;
  55. zi[65].i = zi[1].i;
  56. for (k = 1; k < 31; k += 2) {
  57. zi[64 + 2 * k + 0].i = zi[64 - k].i ^ (1U << 31);
  58. zi[64 + 2 * k + 1].i = zi[ k + 1].i;
  59. zi[64 + 2 * k + 2].i = zi[63 - k].i ^ (1U << 31);
  60. zi[64 + 2 * k + 3].i = zi[ k + 2].i;
  61. }
  62. zi[64 + 2 * 31 + 0].i = zi[64 - 31].i ^ (1U << 31);
  63. zi[64 + 2 * 31 + 1].i = zi[31 + 1].i;
  64. }
  65. static void sbr_qmf_post_shuffle_c(float W[32][2], const float *z)
  66. {
  67. const union av_intfloat32 *zi = (const union av_intfloat32*) z;
  68. union av_intfloat32 *Wi = (union av_intfloat32*) W;
  69. int k;
  70. for (k = 0; k < 32; k += 2) {
  71. Wi[2 * k + 0].i = zi[63 - k].i ^ (1U << 31);
  72. Wi[2 * k + 1].i = zi[ k + 0].i;
  73. Wi[2 * k + 2].i = zi[62 - k].i ^ (1U << 31);
  74. Wi[2 * k + 3].i = zi[ k + 1].i;
  75. }
  76. }
  77. static void sbr_qmf_deint_neg_c(float *v, const float *src)
  78. {
  79. const union av_intfloat32 *si = (const union av_intfloat32*)src;
  80. union av_intfloat32 *vi = (union av_intfloat32*)v;
  81. int i;
  82. for (i = 0; i < 32; i++) {
  83. vi[ i].i = si[63 - 2 * i ].i;
  84. vi[63 - i].i = si[63 - 2 * i - 1].i ^ (1U << 31);
  85. }
  86. }
  87. #if 0
  88. /* This code is slower because it multiplies memory accesses.
  89. * It is left for educational purposes and because it may offer
  90. * a better reference for writing arch-specific DSP functions. */
  91. static av_always_inline void autocorrelate(const float x[40][2],
  92. float phi[3][2][2], int lag)
  93. {
  94. int i;
  95. float real_sum = 0.0f;
  96. float imag_sum = 0.0f;
  97. if (lag) {
  98. for (i = 1; i < 38; i++) {
  99. real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1];
  100. imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0];
  101. }
  102. phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1];
  103. phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0];
  104. if (lag == 1) {
  105. phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1];
  106. phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0];
  107. }
  108. } else {
  109. for (i = 1; i < 38; i++) {
  110. real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1];
  111. }
  112. phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
  113. phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1];
  114. }
  115. }
  116. static void sbr_autocorrelate_c(const float x[40][2], float phi[3][2][2])
  117. {
  118. autocorrelate(x, phi, 0);
  119. autocorrelate(x, phi, 1);
  120. autocorrelate(x, phi, 2);
  121. }
  122. #else
  123. static void sbr_autocorrelate_c(const float x[40][2], float phi[3][2][2])
  124. {
  125. float real_sum2 = x[0][0] * x[2][0] + x[0][1] * x[2][1];
  126. float imag_sum2 = x[0][0] * x[2][1] - x[0][1] * x[2][0];
  127. float real_sum1 = 0.0f, imag_sum1 = 0.0f, real_sum0 = 0.0f;
  128. int i;
  129. for (i = 1; i < 38; i++) {
  130. real_sum0 += x[i][0] * x[i ][0] + x[i][1] * x[i ][1];
  131. real_sum1 += x[i][0] * x[i + 1][0] + x[i][1] * x[i + 1][1];
  132. imag_sum1 += x[i][0] * x[i + 1][1] - x[i][1] * x[i + 1][0];
  133. real_sum2 += x[i][0] * x[i + 2][0] + x[i][1] * x[i + 2][1];
  134. imag_sum2 += x[i][0] * x[i + 2][1] - x[i][1] * x[i + 2][0];
  135. }
  136. phi[2 - 2][1][0] = real_sum2;
  137. phi[2 - 2][1][1] = imag_sum2;
  138. phi[2 ][1][0] = real_sum0 + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
  139. phi[1 ][0][0] = real_sum0 + x[38][0] * x[38][0] + x[38][1] * x[38][1];
  140. phi[2 - 1][1][0] = real_sum1 + x[ 0][0] * x[ 1][0] + x[ 0][1] * x[ 1][1];
  141. phi[2 - 1][1][1] = imag_sum1 + x[ 0][0] * x[ 1][1] - x[ 0][1] * x[ 1][0];
  142. phi[0 ][0][0] = real_sum1 + x[38][0] * x[39][0] + x[38][1] * x[39][1];
  143. phi[0 ][0][1] = imag_sum1 + x[38][0] * x[39][1] - x[38][1] * x[39][0];
  144. }
  145. #endif
  146. static void sbr_hf_gen_c(float (*X_high)[2], const float (*X_low)[2],
  147. const float alpha0[2], const float alpha1[2],
  148. float bw, int start, int end)
  149. {
  150. float alpha[4];
  151. int i;
  152. alpha[0] = alpha1[0] * bw * bw;
  153. alpha[1] = alpha1[1] * bw * bw;
  154. alpha[2] = alpha0[0] * bw;
  155. alpha[3] = alpha0[1] * bw;
  156. for (i = start; i < end; i++) {
  157. X_high[i][0] =
  158. X_low[i - 2][0] * alpha[0] -
  159. X_low[i - 2][1] * alpha[1] +
  160. X_low[i - 1][0] * alpha[2] -
  161. X_low[i - 1][1] * alpha[3] +
  162. X_low[i][0];
  163. X_high[i][1] =
  164. X_low[i - 2][1] * alpha[0] +
  165. X_low[i - 2][0] * alpha[1] +
  166. X_low[i - 1][1] * alpha[2] +
  167. X_low[i - 1][0] * alpha[3] +
  168. X_low[i][1];
  169. }
  170. }
  171. static void sbr_hf_g_filt_c(float (*Y)[2], const float (*X_high)[40][2],
  172. const float *g_filt, int m_max, intptr_t ixh)
  173. {
  174. int m;
  175. for (m = 0; m < m_max; m++) {
  176. Y[m][0] = X_high[m][ixh][0] * g_filt[m];
  177. Y[m][1] = X_high[m][ixh][1] * g_filt[m];
  178. }
  179. }
  180. static av_always_inline void sbr_hf_apply_noise(float (*Y)[2],
  181. const float *s_m,
  182. const float *q_filt,
  183. int noise,
  184. float phi_sign0,
  185. float phi_sign1,
  186. int m_max)
  187. {
  188. int m;
  189. for (m = 0; m < m_max; m++) {
  190. float y0 = Y[m][0];
  191. float y1 = Y[m][1];
  192. noise = (noise + 1) & 0x1ff;
  193. if (s_m[m]) {
  194. y0 += s_m[m] * phi_sign0;
  195. y1 += s_m[m] * phi_sign1;
  196. } else {
  197. y0 += q_filt[m] * ff_sbr_noise_table[noise][0];
  198. y1 += q_filt[m] * ff_sbr_noise_table[noise][1];
  199. }
  200. Y[m][0] = y0;
  201. Y[m][1] = y1;
  202. phi_sign1 = -phi_sign1;
  203. }
  204. }
  205. #include "sbrdsp_template.c"