aacsbr_fixed.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright (c) 2013
  3. * MIPS Technologies, Inc., California.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * AAC Spectral Band Replication decoding functions (fixed-point)
  30. * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
  31. * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
  32. *
  33. * This file is part of FFmpeg.
  34. *
  35. * FFmpeg is free software; you can redistribute it and/or
  36. * modify it under the terms of the GNU Lesser General Public
  37. * License as published by the Free Software Foundation; either
  38. * version 2.1 of the License, or (at your option) any later version.
  39. *
  40. * FFmpeg is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  43. * Lesser General Public License for more details.
  44. *
  45. * You should have received a copy of the GNU Lesser General Public
  46. * License along with FFmpeg; if not, write to the Free Software
  47. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  48. */
  49. /**
  50. * @file
  51. * AAC Spectral Band Replication decoding functions (fixed-point)
  52. * Note: Rounding-to-nearest used unless otherwise stated
  53. * @author Robert Swain ( rob opendot cl )
  54. * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
  55. */
  56. #define USE_FIXED 1
  57. #include "aac.h"
  58. #include "sbr.h"
  59. #include "aacsbr.h"
  60. #include "aacsbrdata.h"
  61. #include "aacsbr_fixed_tablegen.h"
  62. #include "fft.h"
  63. #include "aacps.h"
  64. #include "sbrdsp.h"
  65. #include "libavutil/internal.h"
  66. #include "libavutil/libm.h"
  67. #include "libavutil/avassert.h"
  68. #include <stdint.h>
  69. #include <float.h>
  70. #include <math.h>
  71. static VLC vlc_sbr[10];
  72. static void aacsbr_func_ptr_init(AACSBRContext *c);
  73. static const int CONST_LN2 = Q31(0.6931471806/256); // ln(2)/256
  74. static const int CONST_RECIP_LN2 = Q31(0.7213475204); // 0.5/ln(2)
  75. static const int CONST_076923 = Q31(0.76923076923076923077f);
  76. static const int fixed_log_table[10] =
  77. {
  78. Q31(1.0/2), Q31(1.0/3), Q31(1.0/4), Q31(1.0/5), Q31(1.0/6),
  79. Q31(1.0/7), Q31(1.0/8), Q31(1.0/9), Q31(1.0/10), Q31(1.0/11)
  80. };
  81. static int fixed_log(int x)
  82. {
  83. int i, ret, xpow, tmp;
  84. ret = x;
  85. xpow = x;
  86. for (i=0; i<10; i+=2){
  87. xpow = (int)(((int64_t)xpow * x + 0x40000000) >> 31);
  88. tmp = (int)(((int64_t)xpow * fixed_log_table[i] + 0x40000000) >> 31);
  89. ret -= tmp;
  90. xpow = (int)(((int64_t)xpow * x + 0x40000000) >> 31);
  91. tmp = (int)(((int64_t)xpow * fixed_log_table[i+1] + 0x40000000) >> 31);
  92. ret += tmp;
  93. }
  94. return ret;
  95. }
  96. static const int fixed_exp_table[7] =
  97. {
  98. Q31(1.0/2), Q31(1.0/6), Q31(1.0/24), Q31(1.0/120),
  99. Q31(1.0/720), Q31(1.0/5040), Q31(1.0/40320)
  100. };
  101. static int fixed_exp(int x)
  102. {
  103. int i, ret, xpow, tmp;
  104. ret = 0x800000 + x;
  105. xpow = x;
  106. for (i=0; i<7; i++){
  107. xpow = (int)(((int64_t)xpow * x + 0x400000) >> 23);
  108. tmp = (int)(((int64_t)xpow * fixed_exp_table[i] + 0x40000000) >> 31);
  109. ret += tmp;
  110. }
  111. return ret;
  112. }
  113. static void make_bands(int16_t* bands, int start, int stop, int num_bands)
  114. {
  115. int k, previous, present;
  116. int base, prod, nz = 0;
  117. base = (stop << 23) / start;
  118. while (base < 0x40000000){
  119. base <<= 1;
  120. nz++;
  121. }
  122. base = fixed_log(base - 0x80000000);
  123. base = (((base + 0x80) >> 8) + (8-nz)*CONST_LN2) / num_bands;
  124. base = fixed_exp(base);
  125. previous = start;
  126. prod = start << 23;
  127. for (k = 0; k < num_bands-1; k++) {
  128. prod = (int)(((int64_t)prod * base + 0x400000) >> 23);
  129. present = (prod + 0x400000) >> 23;
  130. bands[k] = present - previous;
  131. previous = present;
  132. }
  133. bands[num_bands-1] = stop - previous;
  134. }
  135. /// Dequantization and stereo decoding (14496-3 sp04 p203)
  136. static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
  137. {
  138. int k, e;
  139. int ch;
  140. if (id_aac == TYPE_CPE && sbr->bs_coupling) {
  141. int alpha = sbr->data[0].bs_amp_res ? 2 : 1;
  142. int pan_offset = sbr->data[0].bs_amp_res ? 12 : 24;
  143. for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
  144. for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
  145. SoftFloat temp1, temp2, fac;
  146. temp1.exp = sbr->data[0].env_facs_q[e][k] * alpha + 14;
  147. if (temp1.exp & 1)
  148. temp1.mant = 759250125;
  149. else
  150. temp1.mant = 0x20000000;
  151. temp1.exp = (temp1.exp >> 1) + 1;
  152. if (temp1.exp > 66) { // temp1 > 1E20
  153. av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
  154. temp1 = FLOAT_1;
  155. }
  156. temp2.exp = (pan_offset - sbr->data[1].env_facs_q[e][k]) * alpha;
  157. if (temp2.exp & 1)
  158. temp2.mant = 759250125;
  159. else
  160. temp2.mant = 0x20000000;
  161. temp2.exp = (temp2.exp >> 1) + 1;
  162. fac = av_div_sf(temp1, av_add_sf(FLOAT_1, temp2));
  163. sbr->data[0].env_facs[e][k] = fac;
  164. sbr->data[1].env_facs[e][k] = av_mul_sf(fac, temp2);
  165. }
  166. }
  167. for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
  168. for (k = 0; k < sbr->n_q; k++) {
  169. SoftFloat temp1, temp2, fac;
  170. temp1.exp = NOISE_FLOOR_OFFSET - \
  171. sbr->data[0].noise_facs_q[e][k] + 2;
  172. temp1.mant = 0x20000000;
  173. av_assert0(temp1.exp <= 66);
  174. temp2.exp = 12 - sbr->data[1].noise_facs_q[e][k] + 1;
  175. temp2.mant = 0x20000000;
  176. fac = av_div_sf(temp1, av_add_sf(FLOAT_1, temp2));
  177. sbr->data[0].noise_facs[e][k] = fac;
  178. sbr->data[1].noise_facs[e][k] = av_mul_sf(fac, temp2);
  179. }
  180. }
  181. } else { // SCE or one non-coupled CPE
  182. for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
  183. int alpha = sbr->data[ch].bs_amp_res ? 2 : 1;
  184. for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
  185. for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++){
  186. SoftFloat temp1;
  187. temp1.exp = alpha * sbr->data[ch].env_facs_q[e][k] + 12;
  188. if (temp1.exp & 1)
  189. temp1.mant = 759250125;
  190. else
  191. temp1.mant = 0x20000000;
  192. temp1.exp = (temp1.exp >> 1) + 1;
  193. if (temp1.exp > 66) { // temp1 > 1E20
  194. av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
  195. temp1 = FLOAT_1;
  196. }
  197. sbr->data[ch].env_facs[e][k] = temp1;
  198. }
  199. for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
  200. for (k = 0; k < sbr->n_q; k++){
  201. sbr->data[ch].noise_facs[e][k].exp = NOISE_FLOOR_OFFSET - \
  202. sbr->data[ch].noise_facs_q[e][k] + 1;
  203. sbr->data[ch].noise_facs[e][k].mant = 0x20000000;
  204. }
  205. }
  206. }
  207. }
  208. /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
  209. * (14496-3 sp04 p214)
  210. * Warning: This routine does not seem numerically stable.
  211. */
  212. static void sbr_hf_inverse_filter(SBRDSPContext *dsp,
  213. int (*alpha0)[2], int (*alpha1)[2],
  214. const int X_low[32][40][2], int k0)
  215. {
  216. int k;
  217. int shift, round;
  218. for (k = 0; k < k0; k++) {
  219. SoftFloat phi[3][2][2];
  220. SoftFloat a00, a01, a10, a11;
  221. SoftFloat dk;
  222. dsp->autocorrelate(X_low[k], phi);
  223. dk = av_sub_sf(av_mul_sf(phi[2][1][0], phi[1][0][0]),
  224. av_mul_sf(av_add_sf(av_mul_sf(phi[1][1][0], phi[1][1][0]),
  225. av_mul_sf(phi[1][1][1], phi[1][1][1])), FLOAT_0999999));
  226. if (!dk.mant) {
  227. a10 = FLOAT_0;
  228. a11 = FLOAT_0;
  229. } else {
  230. SoftFloat temp_real, temp_im;
  231. temp_real = av_sub_sf(av_sub_sf(av_mul_sf(phi[0][0][0], phi[1][1][0]),
  232. av_mul_sf(phi[0][0][1], phi[1][1][1])),
  233. av_mul_sf(phi[0][1][0], phi[1][0][0]));
  234. temp_im = av_sub_sf(av_add_sf(av_mul_sf(phi[0][0][0], phi[1][1][1]),
  235. av_mul_sf(phi[0][0][1], phi[1][1][0])),
  236. av_mul_sf(phi[0][1][1], phi[1][0][0]));
  237. a10 = av_div_sf(temp_real, dk);
  238. a11 = av_div_sf(temp_im, dk);
  239. }
  240. if (!phi[1][0][0].mant) {
  241. a00 = FLOAT_0;
  242. a01 = FLOAT_0;
  243. } else {
  244. SoftFloat temp_real, temp_im;
  245. temp_real = av_add_sf(phi[0][0][0],
  246. av_add_sf(av_mul_sf(a10, phi[1][1][0]),
  247. av_mul_sf(a11, phi[1][1][1])));
  248. temp_im = av_add_sf(phi[0][0][1],
  249. av_sub_sf(av_mul_sf(a11, phi[1][1][0]),
  250. av_mul_sf(a10, phi[1][1][1])));
  251. temp_real.mant = -temp_real.mant;
  252. temp_im.mant = -temp_im.mant;
  253. a00 = av_div_sf(temp_real, phi[1][0][0]);
  254. a01 = av_div_sf(temp_im, phi[1][0][0]);
  255. }
  256. shift = a00.exp;
  257. if (shift >= 3)
  258. alpha0[k][0] = 0x7fffffff;
  259. else if (shift <= -30)
  260. alpha0[k][0] = 0;
  261. else {
  262. shift = 1-shift;
  263. if (shift <= 0)
  264. alpha0[k][0] = a00.mant * (1<<-shift);
  265. else {
  266. round = 1 << (shift-1);
  267. alpha0[k][0] = (a00.mant + round) >> shift;
  268. }
  269. }
  270. shift = a01.exp;
  271. if (shift >= 3)
  272. alpha0[k][1] = 0x7fffffff;
  273. else if (shift <= -30)
  274. alpha0[k][1] = 0;
  275. else {
  276. shift = 1-shift;
  277. if (shift <= 0)
  278. alpha0[k][1] = a01.mant * (1<<-shift);
  279. else {
  280. round = 1 << (shift-1);
  281. alpha0[k][1] = (a01.mant + round) >> shift;
  282. }
  283. }
  284. shift = a10.exp;
  285. if (shift >= 3)
  286. alpha1[k][0] = 0x7fffffff;
  287. else if (shift <= -30)
  288. alpha1[k][0] = 0;
  289. else {
  290. shift = 1-shift;
  291. if (shift <= 0)
  292. alpha1[k][0] = a10.mant * (1<<-shift);
  293. else {
  294. round = 1 << (shift-1);
  295. alpha1[k][0] = (a10.mant + round) >> shift;
  296. }
  297. }
  298. shift = a11.exp;
  299. if (shift >= 3)
  300. alpha1[k][1] = 0x7fffffff;
  301. else if (shift <= -30)
  302. alpha1[k][1] = 0;
  303. else {
  304. shift = 1-shift;
  305. if (shift <= 0)
  306. alpha1[k][1] = a11.mant * (1<<-shift);
  307. else {
  308. round = 1 << (shift-1);
  309. alpha1[k][1] = (a11.mant + round) >> shift;
  310. }
  311. }
  312. shift = (int)(((int64_t)(alpha1[k][0]>>1) * (alpha1[k][0]>>1) + \
  313. (int64_t)(alpha1[k][1]>>1) * (alpha1[k][1]>>1) + \
  314. 0x40000000) >> 31);
  315. if (shift >= 0x20000000){
  316. alpha1[k][0] = 0;
  317. alpha1[k][1] = 0;
  318. alpha0[k][0] = 0;
  319. alpha0[k][1] = 0;
  320. }
  321. shift = (int)(((int64_t)(alpha0[k][0]>>1) * (alpha0[k][0]>>1) + \
  322. (int64_t)(alpha0[k][1]>>1) * (alpha0[k][1]>>1) + \
  323. 0x40000000) >> 31);
  324. if (shift >= 0x20000000){
  325. alpha1[k][0] = 0;
  326. alpha1[k][1] = 0;
  327. alpha0[k][0] = 0;
  328. alpha0[k][1] = 0;
  329. }
  330. }
  331. }
  332. /// Chirp Factors (14496-3 sp04 p214)
  333. static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
  334. {
  335. int i;
  336. int new_bw;
  337. static const int bw_tab[] = { 0, 1610612736, 1932735283, 2104533975 };
  338. int64_t accu;
  339. for (i = 0; i < sbr->n_q; i++) {
  340. if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1)
  341. new_bw = 1288490189;
  342. else
  343. new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
  344. if (new_bw < ch_data->bw_array[i]){
  345. accu = (int64_t)new_bw * 1610612736;
  346. accu += (int64_t)ch_data->bw_array[i] * 0x20000000;
  347. new_bw = (int)((accu + 0x40000000) >> 31);
  348. } else {
  349. accu = (int64_t)new_bw * 1946157056;
  350. accu += (int64_t)ch_data->bw_array[i] * 201326592;
  351. new_bw = (int)((accu + 0x40000000) >> 31);
  352. }
  353. ch_data->bw_array[i] = new_bw < 0x2000000 ? 0 : new_bw;
  354. }
  355. }
  356. /**
  357. * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
  358. * and Calculation of gain (14496-3 sp04 p219)
  359. */
  360. static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr,
  361. SBRData *ch_data, const int e_a[2])
  362. {
  363. int e, k, m;
  364. // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
  365. static const SoftFloat limgain[4] = { { 760155524, 0 }, { 0x20000000, 1 },
  366. { 758351638, 1 }, { 625000000, 34 } };
  367. for (e = 0; e < ch_data->bs_num_env; e++) {
  368. int delta = !((e == e_a[1]) || (e == e_a[0]));
  369. for (k = 0; k < sbr->n_lim; k++) {
  370. SoftFloat gain_boost, gain_max;
  371. SoftFloat sum[2];
  372. sum[0] = sum[1] = FLOAT_0;
  373. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  374. const SoftFloat temp = av_div_sf(sbr->e_origmapped[e][m],
  375. av_add_sf(FLOAT_1, sbr->q_mapped[e][m]));
  376. sbr->q_m[e][m] = av_sqrt_sf(av_mul_sf(temp, sbr->q_mapped[e][m]));
  377. sbr->s_m[e][m] = av_sqrt_sf(av_mul_sf(temp, av_int2sf(ch_data->s_indexmapped[e + 1][m], 0)));
  378. if (!sbr->s_mapped[e][m]) {
  379. if (delta) {
  380. sbr->gain[e][m] = av_sqrt_sf(av_div_sf(sbr->e_origmapped[e][m],
  381. av_mul_sf(av_add_sf(FLOAT_1, sbr->e_curr[e][m]),
  382. av_add_sf(FLOAT_1, sbr->q_mapped[e][m]))));
  383. } else {
  384. sbr->gain[e][m] = av_sqrt_sf(av_div_sf(sbr->e_origmapped[e][m],
  385. av_add_sf(FLOAT_1, sbr->e_curr[e][m])));
  386. }
  387. } else {
  388. sbr->gain[e][m] = av_sqrt_sf(
  389. av_div_sf(
  390. av_mul_sf(sbr->e_origmapped[e][m], sbr->q_mapped[e][m]),
  391. av_mul_sf(
  392. av_add_sf(FLOAT_1, sbr->e_curr[e][m]),
  393. av_add_sf(FLOAT_1, sbr->q_mapped[e][m]))));
  394. }
  395. sbr->gain[e][m] = av_add_sf(sbr->gain[e][m], FLOAT_MIN);
  396. }
  397. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  398. sum[0] = av_add_sf(sum[0], sbr->e_origmapped[e][m]);
  399. sum[1] = av_add_sf(sum[1], sbr->e_curr[e][m]);
  400. }
  401. gain_max = av_mul_sf(limgain[sbr->bs_limiter_gains],
  402. av_sqrt_sf(
  403. av_div_sf(
  404. av_add_sf(FLOAT_EPSILON, sum[0]),
  405. av_add_sf(FLOAT_EPSILON, sum[1]))));
  406. if (av_gt_sf(gain_max, FLOAT_100000))
  407. gain_max = FLOAT_100000;
  408. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  409. SoftFloat q_m_max = av_div_sf(
  410. av_mul_sf(sbr->q_m[e][m], gain_max),
  411. sbr->gain[e][m]);
  412. if (av_gt_sf(sbr->q_m[e][m], q_m_max))
  413. sbr->q_m[e][m] = q_m_max;
  414. if (av_gt_sf(sbr->gain[e][m], gain_max))
  415. sbr->gain[e][m] = gain_max;
  416. }
  417. sum[0] = sum[1] = FLOAT_0;
  418. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  419. sum[0] = av_add_sf(sum[0], sbr->e_origmapped[e][m]);
  420. sum[1] = av_add_sf(sum[1],
  421. av_mul_sf(
  422. av_mul_sf(sbr->e_curr[e][m],
  423. sbr->gain[e][m]),
  424. sbr->gain[e][m]));
  425. sum[1] = av_add_sf(sum[1],
  426. av_mul_sf(sbr->s_m[e][m], sbr->s_m[e][m]));
  427. if (delta && !sbr->s_m[e][m].mant)
  428. sum[1] = av_add_sf(sum[1],
  429. av_mul_sf(sbr->q_m[e][m], sbr->q_m[e][m]));
  430. }
  431. gain_boost = av_sqrt_sf(
  432. av_div_sf(
  433. av_add_sf(FLOAT_EPSILON, sum[0]),
  434. av_add_sf(FLOAT_EPSILON, sum[1])));
  435. if (av_gt_sf(gain_boost, FLOAT_1584893192))
  436. gain_boost = FLOAT_1584893192;
  437. for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
  438. sbr->gain[e][m] = av_mul_sf(sbr->gain[e][m], gain_boost);
  439. sbr->q_m[e][m] = av_mul_sf(sbr->q_m[e][m], gain_boost);
  440. sbr->s_m[e][m] = av_mul_sf(sbr->s_m[e][m], gain_boost);
  441. }
  442. }
  443. }
  444. }
  445. /// Assembling HF Signals (14496-3 sp04 p220)
  446. static void sbr_hf_assemble(int Y1[38][64][2],
  447. const int X_high[64][40][2],
  448. SpectralBandReplication *sbr, SBRData *ch_data,
  449. const int e_a[2])
  450. {
  451. int e, i, j, m;
  452. const int h_SL = 4 * !sbr->bs_smoothing_mode;
  453. const int kx = sbr->kx[1];
  454. const int m_max = sbr->m[1];
  455. static const SoftFloat h_smooth[5] = {
  456. { 715827883, -1 },
  457. { 647472402, -1 },
  458. { 937030863, -2 },
  459. { 989249804, -3 },
  460. { 546843842, -4 },
  461. };
  462. SoftFloat (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
  463. int indexnoise = ch_data->f_indexnoise;
  464. int indexsine = ch_data->f_indexsine;
  465. if (sbr->reset) {
  466. for (i = 0; i < h_SL; i++) {
  467. memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
  468. memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
  469. }
  470. } else if (h_SL) {
  471. for (i = 0; i < 4; i++) {
  472. memcpy(g_temp[i + 2 * ch_data->t_env[0]],
  473. g_temp[i + 2 * ch_data->t_env_num_env_old],
  474. sizeof(g_temp[0]));
  475. memcpy(q_temp[i + 2 * ch_data->t_env[0]],
  476. q_temp[i + 2 * ch_data->t_env_num_env_old],
  477. sizeof(q_temp[0]));
  478. }
  479. }
  480. for (e = 0; e < ch_data->bs_num_env; e++) {
  481. for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
  482. memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
  483. memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
  484. }
  485. }
  486. for (e = 0; e < ch_data->bs_num_env; e++) {
  487. for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
  488. SoftFloat g_filt_tab[48];
  489. SoftFloat q_filt_tab[48];
  490. SoftFloat *g_filt, *q_filt;
  491. if (h_SL && e != e_a[0] && e != e_a[1]) {
  492. g_filt = g_filt_tab;
  493. q_filt = q_filt_tab;
  494. for (m = 0; m < m_max; m++) {
  495. const int idx1 = i + h_SL;
  496. g_filt[m].mant = g_filt[m].exp = 0;
  497. q_filt[m].mant = q_filt[m].exp = 0;
  498. for (j = 0; j <= h_SL; j++) {
  499. g_filt[m] = av_add_sf(g_filt[m],
  500. av_mul_sf(g_temp[idx1 - j][m],
  501. h_smooth[j]));
  502. q_filt[m] = av_add_sf(q_filt[m],
  503. av_mul_sf(q_temp[idx1 - j][m],
  504. h_smooth[j]));
  505. }
  506. }
  507. } else {
  508. g_filt = g_temp[i + h_SL];
  509. q_filt = q_temp[i];
  510. }
  511. sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
  512. i + ENVELOPE_ADJUSTMENT_OFFSET);
  513. if (e != e_a[0] && e != e_a[1]) {
  514. sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
  515. q_filt, indexnoise,
  516. kx, m_max);
  517. } else {
  518. int idx = indexsine&1;
  519. int A = (1-((indexsine+(kx & 1))&2));
  520. int B = (A^(-idx)) + idx;
  521. unsigned *out = &Y1[i][kx][idx];
  522. int shift;
  523. unsigned round;
  524. SoftFloat *in = sbr->s_m[e];
  525. for (m = 0; m+1 < m_max; m+=2) {
  526. int shift2;
  527. shift = 22 - in[m ].exp;
  528. shift2= 22 - in[m+1].exp;
  529. if (shift < 1 || shift2 < 1) {
  530. av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_assemble, shift=%d,%d\n", shift, shift2);
  531. return;
  532. }
  533. if (shift < 32) {
  534. round = 1 << (shift-1);
  535. out[2*m ] += (int)(in[m ].mant * A + round) >> shift;
  536. }
  537. if (shift2 < 32) {
  538. round = 1 << (shift2-1);
  539. out[2*m+2] += (int)(in[m+1].mant * B + round) >> shift2;
  540. }
  541. }
  542. if(m_max&1)
  543. {
  544. shift = 22 - in[m ].exp;
  545. if (shift < 1) {
  546. av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_assemble, shift=%d\n", shift);
  547. return;
  548. } else if (shift < 32) {
  549. round = 1 << (shift-1);
  550. out[2*m ] += (int)(in[m ].mant * A + round) >> shift;
  551. }
  552. }
  553. }
  554. indexnoise = (indexnoise + m_max) & 0x1ff;
  555. indexsine = (indexsine + 1) & 3;
  556. }
  557. }
  558. ch_data->f_indexnoise = indexnoise;
  559. ch_data->f_indexsine = indexsine;
  560. }
  561. #include "aacsbr_template.c"