2
0

aacsbr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /**
  23. * @file
  24. * AAC Spectral Band Replication decoding functions
  25. * @author Robert Swain ( rob opendot cl )
  26. */
  27. #define USE_FIXED 0
  28. #include "aac.h"
  29. #include "sbr.h"
  30. #include "aacsbr.h"
  31. #include "aacsbrdata.h"
  32. #include "aacsbr_tablegen.h"
  33. #include "fft.h"
  34. #include "internal.h"
  35. #include "aacps.h"
  36. #include "sbrdsp.h"
  37. #include "libavutil/internal.h"
  38. #include "libavutil/libm.h"
  39. #include "libavutil/avassert.h"
  40. #include <stdint.h>
  41. #include <float.h>
  42. #include <math.h>
  43. #if ARCH_MIPS
  44. #include "mips/aacsbr_mips.h"
  45. #endif /* ARCH_MIPS */
  46. static VLC vlc_sbr[10];
  47. static void aacsbr_func_ptr_init(AACSBRContext *c);
  48. static void make_bands(int16_t* bands, int start, int stop, int num_bands)
  49. {
  50. int k, previous, present;
  51. float base, prod;
  52. base = powf((float)stop / start, 1.0f / num_bands);
  53. prod = start;
  54. previous = start;
  55. for (k = 0; k < num_bands-1; k++) {
  56. prod *= base;
  57. present = lrintf(prod);
  58. bands[k] = present - previous;
  59. previous = present;
  60. }
  61. bands[num_bands-1] = stop - previous;
  62. }
  63. /// Dequantization and stereo decoding (14496-3 sp04 p203)
  64. static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
  65. {
  66. int k, e;
  67. int ch;
  68. static const double exp2_tab[2] = {1, M_SQRT2};
  69. if (id_aac == TYPE_CPE && sbr->bs_coupling) {
  70. int pan_offset = sbr->data[0].bs_amp_res ? 12 : 24;
  71. for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
  72. for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
  73. float temp1, temp2, fac;
  74. if (sbr->data[0].bs_amp_res) {
  75. temp1 = ff_exp2fi(sbr->data[0].env_facs_q[e][k] + 7);
  76. temp2 = ff_exp2fi(pan_offset - sbr->data[1].env_facs_q[e][k]);
  77. }
  78. else {
  79. temp1 = ff_exp2fi((sbr->data[0].env_facs_q[e][k]>>1) + 7) *
  80. exp2_tab[sbr->data[0].env_facs_q[e][k] & 1];
  81. temp2 = ff_exp2fi((pan_offset - sbr->data[1].env_facs_q[e][k])>>1) *
  82. exp2_tab[(pan_offset - sbr->data[1].env_facs_q[e][k]) & 1];
  83. }
  84. if (temp1 > 1E20) {
  85. av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
  86. temp1 = 1;
  87. }
  88. fac = temp1 / (1.0f + temp2);
  89. sbr->data[0].env_facs[e][k] = fac;
  90. sbr->data[1].env_facs[e][k] = fac * temp2;
  91. }
  92. }
  93. for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
  94. for (k = 0; k < sbr->n_q; k++) {
  95. float temp1 = ff_exp2fi(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs_q[e][k] + 1);
  96. float temp2 = ff_exp2fi(12 - sbr->data[1].noise_facs_q[e][k]);
  97. float fac;
  98. av_assert0(temp1 <= 1E20);
  99. fac = temp1 / (1.0f + temp2);
  100. sbr->data[0].noise_facs[e][k] = fac;
  101. sbr->data[1].noise_facs[e][k] = fac * temp2;
  102. }
  103. }
  104. } else { // SCE or one non-coupled CPE
  105. for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
  106. for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
  107. for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++){
  108. if (sbr->data[ch].bs_amp_res)
  109. sbr->data[ch].env_facs[e][k] = ff_exp2fi(sbr->data[ch].env_facs_q[e][k] + 6);
  110. else
  111. sbr->data[ch].env_facs[e][k] = ff_exp2fi((sbr->data[ch].env_facs_q[e][k]>>1) + 6)
  112. * exp2_tab[sbr->data[ch].env_facs_q[e][k] & 1];
  113. if (sbr->data[ch].env_facs[e][k] > 1E20) {
  114. av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
  115. sbr->data[ch].env_facs[e][k] = 1;
  116. }
  117. }
  118. for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
  119. for (k = 0; k < sbr->n_q; k++)
  120. sbr->data[ch].noise_facs[e][k] =
  121. ff_exp2fi(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs_q[e][k]);
  122. }
  123. }
  124. }
  125. /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
  126. * (14496-3 sp04 p214)
  127. * Warning: This routine does not seem numerically stable.
  128. */
  129. static void sbr_hf_inverse_filter(SBRDSPContext *dsp,
  130. float (*alpha0)[2], float (*alpha1)[2],
  131. const float X_low[32][40][2], int k0)
  132. {
  133. int k;
  134. for (k = 0; k < k0; k++) {
  135. LOCAL_ALIGNED_16(float, phi, [3], [2][2]);
  136. float dk;
  137. dsp->autocorrelate(X_low[k], phi);
  138. dk = phi[2][1][0] * phi[1][0][0] -
  139. (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
  140. if (!dk) {
  141. alpha1[k][0] = 0;
  142. alpha1[k][1] = 0;
  143. } else {
  144. float temp_real, temp_im;
  145. temp_real = phi[0][0][0] * phi[1][1][0] -
  146. phi[0][0][1] * phi[1][1][1] -
  147. phi[0][1][0] * phi[1][0][0];
  148. temp_im = phi[0][0][0] * phi[1][1][1] +
  149. phi[0][0][1] * phi[1][1][0] -
  150. phi[0][1][1] * phi[1][0][0];
  151. alpha1[k][0] = temp_real / dk;
  152. alpha1[k][1] = temp_im / dk;
  153. }
  154. if (!phi[1][0][0]) {
  155. alpha0[k][0] = 0;
  156. alpha0[k][1] = 0;
  157. } else {
  158. float temp_real, temp_im;
  159. temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
  160. alpha1[k][1] * phi[1][1][1];
  161. temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
  162. alpha1[k][0] * phi[1][1][1];
  163. alpha0[k][0] = -temp_real / phi[1][0][0];
  164. alpha0[k][1] = -temp_im / phi[1][0][0];
  165. }
  166. if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
  167. alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
  168. alpha1[k][0] = 0;
  169. alpha1[k][1] = 0;
  170. alpha0[k][0] = 0;
  171. alpha0[k][1] = 0;
  172. }
  173. }
  174. }
  175. /// Chirp Factors (14496-3 sp04 p214)
  176. static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
  177. {
  178. int i;
  179. float new_bw;
  180. static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
  181. for (i = 0; i < sbr->n_q; i++) {
  182. if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
  183. new_bw = 0.6f;
  184. } else
  185. new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
  186. if (new_bw < ch_data->bw_array[i]) {
  187. new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
  188. } else
  189. new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
  190. ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
  191. }
  192. }
  193. /**
  194. * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
  195. * and Calculation of gain (14496-3 sp04 p219)
  196. */
  197. static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr,
  198. SBRData *ch_data, const int e_a[2])
  199. {
  200. int e, k, m;
  201. // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
  202. static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
  203. for (e = 0; e < ch_data->bs_num_env; e++) {
  204. int delta = !((e == e_a[1]) || (e == e_a[0]));
  205. for (k = 0; k < sbr->n_lim; k++) {
  206. float gain_boost, gain_max;
  207. float sum[2] = { 0.0f, 0.0f };
  208. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  209. const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
  210. sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
  211. sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
  212. if (!sbr->s_mapped[e][m]) {
  213. sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
  214. ((1.0f + sbr->e_curr[e][m]) *
  215. (1.0f + sbr->q_mapped[e][m] * delta)));
  216. } else {
  217. sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
  218. ((1.0f + sbr->e_curr[e][m]) *
  219. (1.0f + sbr->q_mapped[e][m])));
  220. }
  221. sbr->gain[e][m] += FLT_MIN;
  222. }
  223. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  224. sum[0] += sbr->e_origmapped[e][m];
  225. sum[1] += sbr->e_curr[e][m];
  226. }
  227. gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
  228. gain_max = FFMIN(100000.f, gain_max);
  229. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  230. float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
  231. sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max);
  232. sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
  233. }
  234. sum[0] = sum[1] = 0.0f;
  235. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  236. sum[0] += sbr->e_origmapped[e][m];
  237. sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
  238. + sbr->s_m[e][m] * sbr->s_m[e][m]
  239. + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
  240. }
  241. gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
  242. gain_boost = FFMIN(1.584893192f, gain_boost);
  243. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  244. sbr->gain[e][m] *= gain_boost;
  245. sbr->q_m[e][m] *= gain_boost;
  246. sbr->s_m[e][m] *= gain_boost;
  247. }
  248. }
  249. }
  250. }
  251. /// Assembling HF Signals (14496-3 sp04 p220)
  252. static void sbr_hf_assemble(float Y1[38][64][2],
  253. const float X_high[64][40][2],
  254. SpectralBandReplication *sbr, SBRData *ch_data,
  255. const int e_a[2])
  256. {
  257. int e, i, j, m;
  258. const int h_SL = 4 * !sbr->bs_smoothing_mode;
  259. const int kx = sbr->kx[1];
  260. const int m_max = sbr->m[1];
  261. static const float h_smooth[5] = {
  262. 0.33333333333333,
  263. 0.30150283239582,
  264. 0.21816949906249,
  265. 0.11516383427084,
  266. 0.03183050093751,
  267. };
  268. float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
  269. int indexnoise = ch_data->f_indexnoise;
  270. int indexsine = ch_data->f_indexsine;
  271. if (sbr->reset) {
  272. for (i = 0; i < h_SL; i++) {
  273. memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
  274. memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
  275. }
  276. } else if (h_SL) {
  277. for (i = 0; i < 4; i++) {
  278. memcpy(g_temp[i + 2 * ch_data->t_env[0]],
  279. g_temp[i + 2 * ch_data->t_env_num_env_old],
  280. sizeof(g_temp[0]));
  281. memcpy(q_temp[i + 2 * ch_data->t_env[0]],
  282. q_temp[i + 2 * ch_data->t_env_num_env_old],
  283. sizeof(q_temp[0]));
  284. }
  285. }
  286. for (e = 0; e < ch_data->bs_num_env; e++) {
  287. for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
  288. memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
  289. memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
  290. }
  291. }
  292. for (e = 0; e < ch_data->bs_num_env; e++) {
  293. for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
  294. LOCAL_ALIGNED_16(float, g_filt_tab, [48]);
  295. LOCAL_ALIGNED_16(float, q_filt_tab, [48]);
  296. float *g_filt, *q_filt;
  297. if (h_SL && e != e_a[0] && e != e_a[1]) {
  298. g_filt = g_filt_tab;
  299. q_filt = q_filt_tab;
  300. for (m = 0; m < m_max; m++) {
  301. const int idx1 = i + h_SL;
  302. g_filt[m] = 0.0f;
  303. q_filt[m] = 0.0f;
  304. for (j = 0; j <= h_SL; j++) {
  305. g_filt[m] += g_temp[idx1 - j][m] * h_smooth[j];
  306. q_filt[m] += q_temp[idx1 - j][m] * h_smooth[j];
  307. }
  308. }
  309. } else {
  310. g_filt = g_temp[i + h_SL];
  311. q_filt = q_temp[i];
  312. }
  313. sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
  314. i + ENVELOPE_ADJUSTMENT_OFFSET);
  315. if (e != e_a[0] && e != e_a[1]) {
  316. sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
  317. q_filt, indexnoise,
  318. kx, m_max);
  319. } else {
  320. int idx = indexsine&1;
  321. int A = (1-((indexsine+(kx & 1))&2));
  322. int B = (A^(-idx)) + idx;
  323. float *out = &Y1[i][kx][idx];
  324. float *in = sbr->s_m[e];
  325. for (m = 0; m+1 < m_max; m+=2) {
  326. out[2*m ] += in[m ] * A;
  327. out[2*m+2] += in[m+1] * B;
  328. }
  329. if(m_max&1)
  330. out[2*m ] += in[m ] * A;
  331. }
  332. indexnoise = (indexnoise + m_max) & 0x1ff;
  333. indexsine = (indexsine + 1) & 3;
  334. }
  335. }
  336. ch_data->f_indexnoise = indexnoise;
  337. ch_data->f_indexsine = indexsine;
  338. }
  339. #include "aacsbr_template.c"