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.

1311 lines
54KB

  1. /*
  2. * AAC coefficients encoder
  3. * Copyright (C) 2008-2009 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * AAC coefficients encoder
  24. */
  25. /***********************************
  26. * TODOs:
  27. * speedup quantizer selection
  28. * add sane pulse detection
  29. ***********************************/
  30. #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
  31. #include <float.h>
  32. #include "libavutil/mathematics.h"
  33. #include "avcodec.h"
  34. #include "put_bits.h"
  35. #include "aac.h"
  36. #include "aacenc.h"
  37. #include "aactab.h"
  38. #include "aacenctab.h"
  39. #include "aacenc_utils.h"
  40. #include "aac_tablegen_decl.h"
  41. /** Frequency in Hz for lower limit of noise substitution **/
  42. #define NOISE_LOW_LIMIT 4500
  43. /* Energy spread threshold value below which no PNS is used, this corresponds to
  44. * typically around 17Khz, after which PNS usage decays ending at 19Khz */
  45. #define NOISE_SPREAD_THRESHOLD 0.5f
  46. /* This constant gets divided by lambda to return ~1.65 which when multiplied
  47. * by the band->threshold and compared to band->energy is the boundary between
  48. * excessive PNS and little PNS usage. */
  49. #define NOISE_LAMBDA_NUMERATOR 252.1f
  50. /** Frequency in Hz for lower limit of intensity stereo **/
  51. #define INT_STEREO_LOW_LIMIT 6100
  52. /**
  53. * Calculate rate distortion cost for quantizing with given codebook
  54. *
  55. * @return quantization distortion
  56. */
  57. static av_always_inline float quantize_and_encode_band_cost_template(
  58. struct AACEncContext *s,
  59. PutBitContext *pb, const float *in,
  60. const float *scaled, int size, int scale_idx,
  61. int cb, const float lambda, const float uplim,
  62. int *bits, int BT_ZERO, int BT_UNSIGNED,
  63. int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
  64. const float ROUNDING)
  65. {
  66. const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
  67. const float Q = ff_aac_pow2sf_tab [q_idx];
  68. const float Q34 = ff_aac_pow34sf_tab[q_idx];
  69. const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
  70. const float CLIPPED_ESCAPE = 165140.0f*IQ;
  71. int i, j;
  72. float cost = 0;
  73. const int dim = BT_PAIR ? 2 : 4;
  74. int resbits = 0;
  75. int off;
  76. if (BT_ZERO || BT_NOISE || BT_STEREO) {
  77. for (i = 0; i < size; i++)
  78. cost += in[i]*in[i];
  79. if (bits)
  80. *bits = 0;
  81. return cost * lambda;
  82. }
  83. if (!scaled) {
  84. abs_pow34_v(s->scoefs, in, size);
  85. scaled = s->scoefs;
  86. }
  87. quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb], ROUNDING);
  88. if (BT_UNSIGNED) {
  89. off = 0;
  90. } else {
  91. off = aac_cb_maxval[cb];
  92. }
  93. for (i = 0; i < size; i += dim) {
  94. const float *vec;
  95. int *quants = s->qcoefs + i;
  96. int curidx = 0;
  97. int curbits;
  98. float rd = 0.0f;
  99. for (j = 0; j < dim; j++) {
  100. curidx *= aac_cb_range[cb];
  101. curidx += quants[j] + off;
  102. }
  103. curbits = ff_aac_spectral_bits[cb-1][curidx];
  104. vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
  105. if (BT_UNSIGNED) {
  106. for (j = 0; j < dim; j++) {
  107. float t = fabsf(in[i+j]);
  108. float di;
  109. if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
  110. if (t >= CLIPPED_ESCAPE) {
  111. di = t - CLIPPED_ESCAPE;
  112. curbits += 21;
  113. } else {
  114. int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
  115. di = t - c*cbrtf(c)*IQ;
  116. curbits += av_log2(c)*2 - 4 + 1;
  117. }
  118. } else {
  119. di = t - vec[j]*IQ;
  120. }
  121. if (vec[j] != 0.0f)
  122. curbits++;
  123. rd += di*di;
  124. }
  125. } else {
  126. for (j = 0; j < dim; j++) {
  127. float di = in[i+j] - vec[j]*IQ;
  128. rd += di*di;
  129. }
  130. }
  131. cost += rd * lambda + curbits;
  132. resbits += curbits;
  133. if (cost >= uplim)
  134. return uplim;
  135. if (pb) {
  136. put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
  137. if (BT_UNSIGNED)
  138. for (j = 0; j < dim; j++)
  139. if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
  140. put_bits(pb, 1, in[i+j] < 0.0f);
  141. if (BT_ESC) {
  142. for (j = 0; j < 2; j++) {
  143. if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
  144. int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
  145. int len = av_log2(coef);
  146. put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
  147. put_sbits(pb, len, coef);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. if (bits)
  154. *bits = resbits;
  155. return cost;
  156. }
  157. static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
  158. const float *in, const float *scaled,
  159. int size, int scale_idx, int cb,
  160. const float lambda, const float uplim,
  161. int *bits) {
  162. av_assert0(0);
  163. return 0.0f;
  164. }
  165. #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
  166. static float quantize_and_encode_band_cost_ ## NAME( \
  167. struct AACEncContext *s, \
  168. PutBitContext *pb, const float *in, \
  169. const float *scaled, int size, int scale_idx, \
  170. int cb, const float lambda, const float uplim, \
  171. int *bits) { \
  172. return quantize_and_encode_band_cost_template( \
  173. s, pb, in, scaled, size, scale_idx, \
  174. BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
  175. BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
  176. ROUNDING); \
  177. }
  178. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
  179. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
  180. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
  181. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
  182. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
  183. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
  184. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
  185. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
  186. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
  187. static float (*const quantize_and_encode_band_cost_arr[])(
  188. struct AACEncContext *s,
  189. PutBitContext *pb, const float *in,
  190. const float *scaled, int size, int scale_idx,
  191. int cb, const float lambda, const float uplim,
  192. int *bits) = {
  193. quantize_and_encode_band_cost_ZERO,
  194. quantize_and_encode_band_cost_SQUAD,
  195. quantize_and_encode_band_cost_SQUAD,
  196. quantize_and_encode_band_cost_UQUAD,
  197. quantize_and_encode_band_cost_UQUAD,
  198. quantize_and_encode_band_cost_SPAIR,
  199. quantize_and_encode_band_cost_SPAIR,
  200. quantize_and_encode_band_cost_UPAIR,
  201. quantize_and_encode_band_cost_UPAIR,
  202. quantize_and_encode_band_cost_UPAIR,
  203. quantize_and_encode_band_cost_UPAIR,
  204. quantize_and_encode_band_cost_ESC,
  205. quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
  206. quantize_and_encode_band_cost_NOISE,
  207. quantize_and_encode_band_cost_STEREO,
  208. quantize_and_encode_band_cost_STEREO,
  209. };
  210. static float (*const quantize_and_encode_band_cost_rtz_arr[])(
  211. struct AACEncContext *s,
  212. PutBitContext *pb, const float *in,
  213. const float *scaled, int size, int scale_idx,
  214. int cb, const float lambda, const float uplim,
  215. int *bits) = {
  216. quantize_and_encode_band_cost_ZERO,
  217. quantize_and_encode_band_cost_SQUAD,
  218. quantize_and_encode_band_cost_SQUAD,
  219. quantize_and_encode_band_cost_UQUAD,
  220. quantize_and_encode_band_cost_UQUAD,
  221. quantize_and_encode_band_cost_SPAIR,
  222. quantize_and_encode_band_cost_SPAIR,
  223. quantize_and_encode_band_cost_UPAIR,
  224. quantize_and_encode_band_cost_UPAIR,
  225. quantize_and_encode_band_cost_UPAIR,
  226. quantize_and_encode_band_cost_UPAIR,
  227. quantize_and_encode_band_cost_ESC_RTZ,
  228. quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
  229. quantize_and_encode_band_cost_NOISE,
  230. quantize_and_encode_band_cost_STEREO,
  231. quantize_and_encode_band_cost_STEREO,
  232. };
  233. #define quantize_and_encode_band_cost( \
  234. s, pb, in, scaled, size, scale_idx, cb, \
  235. lambda, uplim, bits, rtz) \
  236. ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
  237. s, pb, in, scaled, size, scale_idx, cb, \
  238. lambda, uplim, bits)
  239. static float quantize_band_cost(struct AACEncContext *s, const float *in,
  240. const float *scaled, int size, int scale_idx,
  241. int cb, const float lambda, const float uplim,
  242. int *bits, int rtz)
  243. {
  244. return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
  245. cb, lambda, uplim, bits, rtz);
  246. }
  247. static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
  248. const float *in, int size, int scale_idx,
  249. int cb, const float lambda, int rtz)
  250. {
  251. quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
  252. INFINITY, NULL, rtz);
  253. }
  254. /**
  255. * structure used in optimal codebook search
  256. */
  257. typedef struct BandCodingPath {
  258. int prev_idx; ///< pointer to the previous path point
  259. float cost; ///< path cost
  260. int run;
  261. } BandCodingPath;
  262. /**
  263. * Encode band info for single window group bands.
  264. */
  265. static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
  266. int win, int group_len, const float lambda)
  267. {
  268. BandCodingPath path[120][CB_TOT_ALL];
  269. int w, swb, cb, start, size;
  270. int i, j;
  271. const int max_sfb = sce->ics.max_sfb;
  272. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  273. const int run_esc = (1 << run_bits) - 1;
  274. int idx, ppos, count;
  275. int stackrun[120], stackcb[120], stack_len;
  276. float next_minrd = INFINITY;
  277. int next_mincb = 0;
  278. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  279. start = win*128;
  280. for (cb = 0; cb < CB_TOT_ALL; cb++) {
  281. path[0][cb].cost = 0.0f;
  282. path[0][cb].prev_idx = -1;
  283. path[0][cb].run = 0;
  284. }
  285. for (swb = 0; swb < max_sfb; swb++) {
  286. size = sce->ics.swb_sizes[swb];
  287. if (sce->zeroes[win*16 + swb]) {
  288. for (cb = 0; cb < CB_TOT_ALL; cb++) {
  289. path[swb+1][cb].prev_idx = cb;
  290. path[swb+1][cb].cost = path[swb][cb].cost;
  291. path[swb+1][cb].run = path[swb][cb].run + 1;
  292. }
  293. } else {
  294. float minrd = next_minrd;
  295. int mincb = next_mincb;
  296. next_minrd = INFINITY;
  297. next_mincb = 0;
  298. for (cb = 0; cb < CB_TOT_ALL; cb++) {
  299. float cost_stay_here, cost_get_here;
  300. float rd = 0.0f;
  301. if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] ||
  302. cb < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) {
  303. path[swb+1][cb].prev_idx = -1;
  304. path[swb+1][cb].cost = INFINITY;
  305. path[swb+1][cb].run = path[swb][cb].run + 1;
  306. continue;
  307. }
  308. for (w = 0; w < group_len; w++) {
  309. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
  310. rd += quantize_band_cost(s, sce->coeffs + start + w*128,
  311. s->scoefs + start + w*128, size,
  312. sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
  313. lambda / band->threshold, INFINITY, NULL, 0);
  314. }
  315. cost_stay_here = path[swb][cb].cost + rd;
  316. cost_get_here = minrd + rd + run_bits + 4;
  317. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  318. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  319. cost_stay_here += run_bits;
  320. if (cost_get_here < cost_stay_here) {
  321. path[swb+1][cb].prev_idx = mincb;
  322. path[swb+1][cb].cost = cost_get_here;
  323. path[swb+1][cb].run = 1;
  324. } else {
  325. path[swb+1][cb].prev_idx = cb;
  326. path[swb+1][cb].cost = cost_stay_here;
  327. path[swb+1][cb].run = path[swb][cb].run + 1;
  328. }
  329. if (path[swb+1][cb].cost < next_minrd) {
  330. next_minrd = path[swb+1][cb].cost;
  331. next_mincb = cb;
  332. }
  333. }
  334. }
  335. start += sce->ics.swb_sizes[swb];
  336. }
  337. //convert resulting path from backward-linked list
  338. stack_len = 0;
  339. idx = 0;
  340. for (cb = 1; cb < CB_TOT_ALL; cb++)
  341. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  342. idx = cb;
  343. ppos = max_sfb;
  344. while (ppos > 0) {
  345. av_assert1(idx >= 0);
  346. cb = idx;
  347. stackrun[stack_len] = path[ppos][cb].run;
  348. stackcb [stack_len] = cb;
  349. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  350. ppos -= path[ppos][cb].run;
  351. stack_len++;
  352. }
  353. //perform actual band info encoding
  354. start = 0;
  355. for (i = stack_len - 1; i >= 0; i--) {
  356. cb = aac_cb_out_map[stackcb[i]];
  357. put_bits(&s->pb, 4, cb);
  358. count = stackrun[i];
  359. memset(sce->zeroes + win*16 + start, !cb, count);
  360. //XXX: memset when band_type is also uint8_t
  361. for (j = 0; j < count; j++) {
  362. sce->band_type[win*16 + start] = cb;
  363. start++;
  364. }
  365. while (count >= run_esc) {
  366. put_bits(&s->pb, run_bits, run_esc);
  367. count -= run_esc;
  368. }
  369. put_bits(&s->pb, run_bits, count);
  370. }
  371. }
  372. static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
  373. int win, int group_len, const float lambda)
  374. {
  375. BandCodingPath path[120][CB_TOT_ALL];
  376. int w, swb, cb, start, size;
  377. int i, j;
  378. const int max_sfb = sce->ics.max_sfb;
  379. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  380. const int run_esc = (1 << run_bits) - 1;
  381. int idx, ppos, count;
  382. int stackrun[120], stackcb[120], stack_len;
  383. float next_minbits = INFINITY;
  384. int next_mincb = 0;
  385. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  386. start = win*128;
  387. for (cb = 0; cb < CB_TOT_ALL; cb++) {
  388. path[0][cb].cost = run_bits+4;
  389. path[0][cb].prev_idx = -1;
  390. path[0][cb].run = 0;
  391. }
  392. for (swb = 0; swb < max_sfb; swb++) {
  393. size = sce->ics.swb_sizes[swb];
  394. if (sce->zeroes[win*16 + swb]) {
  395. float cost_stay_here = path[swb][0].cost;
  396. float cost_get_here = next_minbits + run_bits + 4;
  397. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
  398. != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
  399. cost_stay_here += run_bits;
  400. if (cost_get_here < cost_stay_here) {
  401. path[swb+1][0].prev_idx = next_mincb;
  402. path[swb+1][0].cost = cost_get_here;
  403. path[swb+1][0].run = 1;
  404. } else {
  405. path[swb+1][0].prev_idx = 0;
  406. path[swb+1][0].cost = cost_stay_here;
  407. path[swb+1][0].run = path[swb][0].run + 1;
  408. }
  409. next_minbits = path[swb+1][0].cost;
  410. next_mincb = 0;
  411. for (cb = 1; cb < CB_TOT_ALL; cb++) {
  412. path[swb+1][cb].cost = 61450;
  413. path[swb+1][cb].prev_idx = -1;
  414. path[swb+1][cb].run = 0;
  415. }
  416. } else {
  417. float minbits = next_minbits;
  418. int mincb = next_mincb;
  419. int startcb = sce->band_type[win*16+swb];
  420. startcb = aac_cb_in_map[startcb];
  421. next_minbits = INFINITY;
  422. next_mincb = 0;
  423. for (cb = 0; cb < startcb; cb++) {
  424. path[swb+1][cb].cost = 61450;
  425. path[swb+1][cb].prev_idx = -1;
  426. path[swb+1][cb].run = 0;
  427. }
  428. for (cb = startcb; cb < CB_TOT_ALL; cb++) {
  429. float cost_stay_here, cost_get_here;
  430. float bits = 0.0f;
  431. if (cb >= 12 && sce->band_type[win*16+swb] != aac_cb_out_map[cb]) {
  432. path[swb+1][cb].cost = 61450;
  433. path[swb+1][cb].prev_idx = -1;
  434. path[swb+1][cb].run = 0;
  435. continue;
  436. }
  437. for (w = 0; w < group_len; w++) {
  438. bits += quantize_band_cost(s, sce->coeffs + start + w*128,
  439. s->scoefs + start + w*128, size,
  440. sce->sf_idx[win*16+swb],
  441. aac_cb_out_map[cb],
  442. 0, INFINITY, NULL, 0);
  443. }
  444. cost_stay_here = path[swb][cb].cost + bits;
  445. cost_get_here = minbits + bits + run_bits + 4;
  446. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  447. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  448. cost_stay_here += run_bits;
  449. if (cost_get_here < cost_stay_here) {
  450. path[swb+1][cb].prev_idx = mincb;
  451. path[swb+1][cb].cost = cost_get_here;
  452. path[swb+1][cb].run = 1;
  453. } else {
  454. path[swb+1][cb].prev_idx = cb;
  455. path[swb+1][cb].cost = cost_stay_here;
  456. path[swb+1][cb].run = path[swb][cb].run + 1;
  457. }
  458. if (path[swb+1][cb].cost < next_minbits) {
  459. next_minbits = path[swb+1][cb].cost;
  460. next_mincb = cb;
  461. }
  462. }
  463. }
  464. start += sce->ics.swb_sizes[swb];
  465. }
  466. //convert resulting path from backward-linked list
  467. stack_len = 0;
  468. idx = 0;
  469. for (cb = 1; cb < CB_TOT_ALL; cb++)
  470. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  471. idx = cb;
  472. ppos = max_sfb;
  473. while (ppos > 0) {
  474. av_assert1(idx >= 0);
  475. cb = idx;
  476. stackrun[stack_len] = path[ppos][cb].run;
  477. stackcb [stack_len] = cb;
  478. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  479. ppos -= path[ppos][cb].run;
  480. stack_len++;
  481. }
  482. //perform actual band info encoding
  483. start = 0;
  484. for (i = stack_len - 1; i >= 0; i--) {
  485. cb = aac_cb_out_map[stackcb[i]];
  486. put_bits(&s->pb, 4, cb);
  487. count = stackrun[i];
  488. memset(sce->zeroes + win*16 + start, !cb, count);
  489. //XXX: memset when band_type is also uint8_t
  490. for (j = 0; j < count; j++) {
  491. sce->band_type[win*16 + start] = cb;
  492. start++;
  493. }
  494. while (count >= run_esc) {
  495. put_bits(&s->pb, run_bits, run_esc);
  496. count -= run_esc;
  497. }
  498. put_bits(&s->pb, run_bits, count);
  499. }
  500. }
  501. typedef struct TrellisPath {
  502. float cost;
  503. int prev;
  504. } TrellisPath;
  505. #define TRELLIS_STAGES 121
  506. #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
  507. static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
  508. {
  509. int w, g, start = 0;
  510. int minscaler_n = sce->sf_idx[0], minscaler_i = sce->sf_idx[0];
  511. int bands = 0;
  512. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  513. start = 0;
  514. for (g = 0; g < sce->ics.num_swb; g++) {
  515. if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
  516. sce->sf_idx[w*16+g] = av_clip(ceilf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
  517. minscaler_i = FFMIN(minscaler_i, sce->sf_idx[w*16+g]);
  518. bands++;
  519. } else if (sce->band_type[w*16+g] == NOISE_BT) {
  520. sce->sf_idx[w*16+g] = av_clip(4+log2f(sce->pns_ener[w*16+g])*2, -100, 155);
  521. minscaler_n = FFMIN(minscaler_n, sce->sf_idx[w*16+g]);
  522. bands++;
  523. }
  524. start += sce->ics.swb_sizes[g];
  525. }
  526. }
  527. if (!bands)
  528. return;
  529. /* Clip the scalefactor indices */
  530. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  531. for (g = 0; g < sce->ics.num_swb; g++) {
  532. if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
  533. sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_i, minscaler_i + SCALE_MAX_DIFF);
  534. } else if (sce->band_type[w*16+g] == NOISE_BT) {
  535. sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
  536. }
  537. }
  538. }
  539. }
  540. static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
  541. SingleChannelElement *sce,
  542. const float lambda)
  543. {
  544. int q, w, w2, g, start = 0;
  545. int i, j;
  546. int idx;
  547. TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
  548. int bandaddr[TRELLIS_STAGES];
  549. int minq;
  550. float mincost;
  551. float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
  552. int q0, q1, qcnt = 0;
  553. for (i = 0; i < 1024; i++) {
  554. float t = fabsf(sce->coeffs[i]);
  555. if (t > 0.0f) {
  556. q0f = FFMIN(q0f, t);
  557. q1f = FFMAX(q1f, t);
  558. qnrgf += t*t;
  559. qcnt++;
  560. }
  561. }
  562. if (!qcnt) {
  563. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  564. memset(sce->zeroes, 1, sizeof(sce->zeroes));
  565. return;
  566. }
  567. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  568. q0 = coef2minsf(q0f);
  569. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  570. q1 = coef2maxsf(q1f);
  571. if (q1 - q0 > 60) {
  572. int q0low = q0;
  573. int q1high = q1;
  574. //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
  575. int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
  576. q1 = qnrg + 30;
  577. q0 = qnrg - 30;
  578. if (q0 < q0low) {
  579. q1 += q0low - q0;
  580. q0 = q0low;
  581. } else if (q1 > q1high) {
  582. q0 -= q1 - q1high;
  583. q1 = q1high;
  584. }
  585. }
  586. for (i = 0; i < TRELLIS_STATES; i++) {
  587. paths[0][i].cost = 0.0f;
  588. paths[0][i].prev = -1;
  589. }
  590. for (j = 1; j < TRELLIS_STAGES; j++) {
  591. for (i = 0; i < TRELLIS_STATES; i++) {
  592. paths[j][i].cost = INFINITY;
  593. paths[j][i].prev = -2;
  594. }
  595. }
  596. idx = 1;
  597. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  598. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  599. start = w*128;
  600. for (g = 0; g < sce->ics.num_swb; g++) {
  601. const float *coefs = sce->coeffs + start;
  602. float qmin, qmax;
  603. int nz = 0;
  604. bandaddr[idx] = w * 16 + g;
  605. qmin = INT_MAX;
  606. qmax = 0.0f;
  607. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  608. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  609. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  610. sce->zeroes[(w+w2)*16+g] = 1;
  611. continue;
  612. }
  613. sce->zeroes[(w+w2)*16+g] = 0;
  614. nz = 1;
  615. for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  616. float t = fabsf(coefs[w2*128+i]);
  617. if (t > 0.0f)
  618. qmin = FFMIN(qmin, t);
  619. qmax = FFMAX(qmax, t);
  620. }
  621. }
  622. if (nz) {
  623. int minscale, maxscale;
  624. float minrd = INFINITY;
  625. float maxval;
  626. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  627. minscale = coef2minsf(qmin);
  628. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  629. maxscale = coef2maxsf(qmax);
  630. minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
  631. maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
  632. maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
  633. for (q = minscale; q < maxscale; q++) {
  634. float dist = 0;
  635. int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
  636. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  637. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  638. dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
  639. q + q0, cb, lambda / band->threshold, INFINITY, NULL, 0);
  640. }
  641. minrd = FFMIN(minrd, dist);
  642. for (i = 0; i < q1 - q0; i++) {
  643. float cost;
  644. cost = paths[idx - 1][i].cost + dist
  645. + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
  646. if (cost < paths[idx][q].cost) {
  647. paths[idx][q].cost = cost;
  648. paths[idx][q].prev = i;
  649. }
  650. }
  651. }
  652. } else {
  653. for (q = 0; q < q1 - q0; q++) {
  654. paths[idx][q].cost = paths[idx - 1][q].cost + 1;
  655. paths[idx][q].prev = q;
  656. }
  657. }
  658. sce->zeroes[w*16+g] = !nz;
  659. start += sce->ics.swb_sizes[g];
  660. idx++;
  661. }
  662. }
  663. idx--;
  664. mincost = paths[idx][0].cost;
  665. minq = 0;
  666. for (i = 1; i < TRELLIS_STATES; i++) {
  667. if (paths[idx][i].cost < mincost) {
  668. mincost = paths[idx][i].cost;
  669. minq = i;
  670. }
  671. }
  672. while (idx) {
  673. sce->sf_idx[bandaddr[idx]] = minq + q0;
  674. minq = paths[idx][minq].prev;
  675. idx--;
  676. }
  677. //set the same quantizers inside window groups
  678. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  679. for (g = 0; g < sce->ics.num_swb; g++)
  680. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  681. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  682. }
  683. /**
  684. * two-loop quantizers search taken from ISO 13818-7 Appendix C
  685. */
  686. static void search_for_quantizers_twoloop(AVCodecContext *avctx,
  687. AACEncContext *s,
  688. SingleChannelElement *sce,
  689. const float lambda)
  690. {
  691. int start = 0, i, w, w2, g;
  692. int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
  693. float dists[128] = { 0 }, uplims[128] = { 0 };
  694. float maxvals[128];
  695. int fflag, minscaler;
  696. int its = 0;
  697. int allz = 0;
  698. float minthr = INFINITY;
  699. // for values above this the decoder might end up in an endless loop
  700. // due to always having more bits than what can be encoded.
  701. destbits = FFMIN(destbits, 5800);
  702. //XXX: some heuristic to determine initial quantizers will reduce search time
  703. //determine zero bands and upper limits
  704. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  705. for (g = 0; g < sce->ics.num_swb; g++) {
  706. int nz = 0;
  707. float uplim = 0.0f, energy = 0.0f;
  708. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  709. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  710. uplim += band->threshold;
  711. energy += band->energy;
  712. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  713. sce->zeroes[(w+w2)*16+g] = 1;
  714. continue;
  715. }
  716. nz = 1;
  717. }
  718. uplims[w*16+g] = uplim *512;
  719. sce->zeroes[w*16+g] = !nz;
  720. if (nz)
  721. minthr = FFMIN(minthr, uplim);
  722. allz |= nz;
  723. }
  724. }
  725. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  726. for (g = 0; g < sce->ics.num_swb; g++) {
  727. if (sce->zeroes[w*16+g]) {
  728. sce->sf_idx[w*16+g] = SCALE_ONE_POS;
  729. continue;
  730. }
  731. sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
  732. }
  733. }
  734. if (!allz)
  735. return;
  736. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  737. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  738. start = w*128;
  739. for (g = 0; g < sce->ics.num_swb; g++) {
  740. const float *scaled = s->scoefs + start;
  741. maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
  742. start += sce->ics.swb_sizes[g];
  743. }
  744. }
  745. //perform two-loop search
  746. //outer loop - improve quality
  747. do {
  748. int tbits, qstep;
  749. minscaler = sce->sf_idx[0];
  750. //inner loop - quantize spectrum to fit into given number of bits
  751. qstep = its ? 1 : 32;
  752. do {
  753. int prev = -1;
  754. tbits = 0;
  755. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  756. start = w*128;
  757. for (g = 0; g < sce->ics.num_swb; g++) {
  758. const float *coefs = sce->coeffs + start;
  759. const float *scaled = s->scoefs + start;
  760. int bits = 0;
  761. int cb;
  762. float dist = 0.0f;
  763. if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
  764. start += sce->ics.swb_sizes[g];
  765. continue;
  766. }
  767. minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
  768. cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  769. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  770. int b;
  771. dist += quantize_band_cost(s, coefs + w2*128,
  772. scaled + w2*128,
  773. sce->ics.swb_sizes[g],
  774. sce->sf_idx[w*16+g],
  775. cb,
  776. 1.0f,
  777. INFINITY,
  778. &b,
  779. 0);
  780. bits += b;
  781. }
  782. dists[w*16+g] = dist - bits;
  783. if (prev != -1) {
  784. bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
  785. }
  786. tbits += bits;
  787. start += sce->ics.swb_sizes[g];
  788. prev = sce->sf_idx[w*16+g];
  789. }
  790. }
  791. if (tbits > destbits) {
  792. for (i = 0; i < 128; i++)
  793. if (sce->sf_idx[i] < 218 - qstep)
  794. sce->sf_idx[i] += qstep;
  795. } else {
  796. for (i = 0; i < 128; i++)
  797. if (sce->sf_idx[i] > 60 - qstep)
  798. sce->sf_idx[i] -= qstep;
  799. }
  800. qstep >>= 1;
  801. if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
  802. qstep = 1;
  803. } while (qstep);
  804. fflag = 0;
  805. minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
  806. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  807. for (g = 0; g < sce->ics.num_swb; g++) {
  808. int prevsc = sce->sf_idx[w*16+g];
  809. if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
  810. if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
  811. sce->sf_idx[w*16+g]--;
  812. else //Try to make sure there is some energy in every band
  813. sce->sf_idx[w*16+g]-=2;
  814. }
  815. sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
  816. sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
  817. if (sce->sf_idx[w*16+g] != prevsc)
  818. fflag = 1;
  819. sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  820. }
  821. }
  822. its++;
  823. } while (fflag && its < 10);
  824. }
  825. static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
  826. SingleChannelElement *sce,
  827. const float lambda)
  828. {
  829. int start = 0, i, w, w2, g;
  830. float uplim[128], maxq[128];
  831. int minq, maxsf;
  832. float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
  833. int last = 0, lastband = 0, curband = 0;
  834. float avg_energy = 0.0;
  835. if (sce->ics.num_windows == 1) {
  836. start = 0;
  837. for (i = 0; i < 1024; i++) {
  838. if (i - start >= sce->ics.swb_sizes[curband]) {
  839. start += sce->ics.swb_sizes[curband];
  840. curband++;
  841. }
  842. if (sce->coeffs[i]) {
  843. avg_energy += sce->coeffs[i] * sce->coeffs[i];
  844. last = i;
  845. lastband = curband;
  846. }
  847. }
  848. } else {
  849. for (w = 0; w < 8; w++) {
  850. const float *coeffs = sce->coeffs + w*128;
  851. curband = start = 0;
  852. for (i = 0; i < 128; i++) {
  853. if (i - start >= sce->ics.swb_sizes[curband]) {
  854. start += sce->ics.swb_sizes[curband];
  855. curband++;
  856. }
  857. if (coeffs[i]) {
  858. avg_energy += coeffs[i] * coeffs[i];
  859. last = FFMAX(last, i);
  860. lastband = FFMAX(lastband, curband);
  861. }
  862. }
  863. }
  864. }
  865. last++;
  866. avg_energy /= last;
  867. if (avg_energy == 0.0f) {
  868. for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
  869. sce->sf_idx[i] = SCALE_ONE_POS;
  870. return;
  871. }
  872. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  873. start = w*128;
  874. for (g = 0; g < sce->ics.num_swb; g++) {
  875. float *coefs = sce->coeffs + start;
  876. const int size = sce->ics.swb_sizes[g];
  877. int start2 = start, end2 = start + size, peakpos = start;
  878. float maxval = -1, thr = 0.0f, t;
  879. maxq[w*16+g] = 0.0f;
  880. if (g > lastband) {
  881. maxq[w*16+g] = 0.0f;
  882. start += size;
  883. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  884. memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
  885. continue;
  886. }
  887. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  888. for (i = 0; i < size; i++) {
  889. float t = coefs[w2*128+i]*coefs[w2*128+i];
  890. maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
  891. thr += t;
  892. if (sce->ics.num_windows == 1 && maxval < t) {
  893. maxval = t;
  894. peakpos = start+i;
  895. }
  896. }
  897. }
  898. if (sce->ics.num_windows == 1) {
  899. start2 = FFMAX(peakpos - 2, start2);
  900. end2 = FFMIN(peakpos + 3, end2);
  901. } else {
  902. start2 -= start;
  903. end2 -= start;
  904. }
  905. start += size;
  906. thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
  907. t = 1.0 - (1.0 * start2 / last);
  908. uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
  909. }
  910. }
  911. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  912. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  913. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  914. start = w*128;
  915. for (g = 0; g < sce->ics.num_swb; g++) {
  916. const float *coefs = sce->coeffs + start;
  917. const float *scaled = s->scoefs + start;
  918. const int size = sce->ics.swb_sizes[g];
  919. int scf, prev_scf, step;
  920. int min_scf = -1, max_scf = 256;
  921. float curdiff;
  922. if (maxq[w*16+g] < 21.544) {
  923. sce->zeroes[w*16+g] = 1;
  924. start += size;
  925. continue;
  926. }
  927. sce->zeroes[w*16+g] = 0;
  928. scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
  929. for (;;) {
  930. float dist = 0.0f;
  931. int quant_max;
  932. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  933. int b;
  934. dist += quantize_band_cost(s, coefs + w2*128,
  935. scaled + w2*128,
  936. sce->ics.swb_sizes[g],
  937. scf,
  938. ESC_BT,
  939. lambda,
  940. INFINITY,
  941. &b,
  942. 0);
  943. dist -= b;
  944. }
  945. dist *= 1.0f / 512.0f / lambda;
  946. quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512], ROUND_STANDARD);
  947. if (quant_max >= 8191) { // too much, return to the previous quantizer
  948. sce->sf_idx[w*16+g] = prev_scf;
  949. break;
  950. }
  951. prev_scf = scf;
  952. curdiff = fabsf(dist - uplim[w*16+g]);
  953. if (curdiff <= 1.0f)
  954. step = 0;
  955. else
  956. step = log2f(curdiff);
  957. if (dist > uplim[w*16+g])
  958. step = -step;
  959. scf += step;
  960. scf = av_clip_uint8(scf);
  961. step = scf - prev_scf;
  962. if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
  963. sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
  964. break;
  965. }
  966. if (step > 0)
  967. min_scf = prev_scf;
  968. else
  969. max_scf = prev_scf;
  970. }
  971. start += size;
  972. }
  973. }
  974. minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
  975. for (i = 1; i < 128; i++) {
  976. if (!sce->sf_idx[i])
  977. sce->sf_idx[i] = sce->sf_idx[i-1];
  978. else
  979. minq = FFMIN(minq, sce->sf_idx[i]);
  980. }
  981. if (minq == INT_MAX)
  982. minq = 0;
  983. minq = FFMIN(minq, SCALE_MAX_POS);
  984. maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
  985. for (i = 126; i >= 0; i--) {
  986. if (!sce->sf_idx[i])
  987. sce->sf_idx[i] = sce->sf_idx[i+1];
  988. sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
  989. }
  990. }
  991. static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
  992. SingleChannelElement *sce,
  993. const float lambda)
  994. {
  995. int i, w, w2, g;
  996. int minq = 255;
  997. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  998. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  999. for (g = 0; g < sce->ics.num_swb; g++) {
  1000. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  1001. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  1002. if (band->energy <= band->threshold) {
  1003. sce->sf_idx[(w+w2)*16+g] = 218;
  1004. sce->zeroes[(w+w2)*16+g] = 1;
  1005. } else {
  1006. sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
  1007. sce->zeroes[(w+w2)*16+g] = 0;
  1008. }
  1009. minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
  1010. }
  1011. }
  1012. }
  1013. for (i = 0; i < 128; i++) {
  1014. sce->sf_idx[i] = 140;
  1015. //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
  1016. }
  1017. //set the same quantizers inside window groups
  1018. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  1019. for (g = 0; g < sce->ics.num_swb; g++)
  1020. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  1021. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  1022. }
  1023. static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
  1024. {
  1025. int start = 0, w, w2, g;
  1026. const float lambda = s->lambda;
  1027. const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
  1028. const float spread_threshold = NOISE_SPREAD_THRESHOLD*(lambda/120.f);
  1029. const float thr_mult = NOISE_LAMBDA_NUMERATOR/lambda;
  1030. /* Coders !twoloop don't reset the band_types */
  1031. for (w = 0; w < 128; w++)
  1032. if (sce->band_type[w] == NOISE_BT)
  1033. sce->band_type[w] = 0;
  1034. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  1035. start = 0;
  1036. for (g = 0; g < sce->ics.num_swb; g++) {
  1037. if (start*freq_mult > NOISE_LOW_LIMIT*(lambda/170.0f)) {
  1038. float energy = 0.0f, threshold = 0.0f, spread = 0.0f;
  1039. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  1040. FFPsyBand *band = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  1041. energy += band->energy;
  1042. threshold += band->threshold;
  1043. spread += band->spread;
  1044. }
  1045. if (spread > spread_threshold*sce->ics.group_len[w] &&
  1046. ((sce->zeroes[w*16+g] && energy >= threshold) ||
  1047. energy < threshold*thr_mult*sce->ics.group_len[w])) {
  1048. sce->band_type[w*16+g] = NOISE_BT;
  1049. sce->pns_ener[w*16+g] = energy / sce->ics.group_len[w];
  1050. sce->zeroes[w*16+g] = 0;
  1051. }
  1052. }
  1053. start += sce->ics.swb_sizes[g];
  1054. }
  1055. }
  1056. }
  1057. static void search_for_is(AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe)
  1058. {
  1059. float IS[128];
  1060. float *L34 = s->scoefs + 128*0, *R34 = s->scoefs + 128*1;
  1061. float *I34 = s->scoefs + 128*2;
  1062. SingleChannelElement *sce0 = &cpe->ch[0];
  1063. SingleChannelElement *sce1 = &cpe->ch[1];
  1064. int start = 0, count = 0, i, w, w2, g;
  1065. const float freq_mult = avctx->sample_rate/(1024.0f/sce0->ics.num_windows)/2.0f;
  1066. const float lambda = s->lambda;
  1067. for (w = 0; w < 128; w++)
  1068. if (sce1->band_type[w] >= INTENSITY_BT2)
  1069. sce1->band_type[w] = 0;
  1070. if (!cpe->common_window)
  1071. return;
  1072. for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
  1073. start = 0;
  1074. for (g = 0; g < sce0->ics.num_swb; g++) {
  1075. if (start*freq_mult > INT_STEREO_LOW_LIMIT*(lambda/170.0f) &&
  1076. cpe->ch[0].band_type[w*16+g] != NOISE_BT && !cpe->ch[0].zeroes[w*16+g] &&
  1077. cpe->ch[1].band_type[w*16+g] != NOISE_BT && !cpe->ch[1].zeroes[w*16+g]) {
  1078. int phase = 0;
  1079. float ener0 = 0.0f, ener1 = 0.0f, ener01 = 0.0f;
  1080. float dist1 = 0.0f, dist2 = 0.0f;
  1081. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  1082. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  1083. float coef0 = sce0->pcoeffs[start+(w+w2)*128+i];
  1084. float coef1 = sce1->pcoeffs[start+(w+w2)*128+i];
  1085. phase += coef0*coef1 >= 0.0f ? 1 : -1;
  1086. ener0 += coef0*coef0;
  1087. ener1 += coef1*coef1;
  1088. ener01 += (coef0 + coef1)*(coef0 + coef1);
  1089. }
  1090. }
  1091. if (!phase) { /* Too much phase difference between channels */
  1092. start += sce0->ics.swb_sizes[g];
  1093. continue;
  1094. }
  1095. phase = av_clip(phase, -1, 1);
  1096. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  1097. FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  1098. FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
  1099. int is_band_type, is_sf_idx = FFMAX(1, sce0->sf_idx[(w+w2)*16+g]-4);
  1100. float e01_34 = phase*pow(sqrt(ener1/ener0), 3.0/4.0);
  1101. float maxval, dist_spec_err = 0.0f;
  1102. float minthr = FFMIN(band0->threshold, band1->threshold);
  1103. for (i = 0; i < sce0->ics.swb_sizes[g]; i++)
  1104. IS[i] = (sce0->pcoeffs[start+(w+w2)*128+i] + phase*sce1->pcoeffs[start+(w+w2)*128+i]) * sqrt(ener0/ener01);
  1105. abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  1106. abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  1107. abs_pow34_v(I34, IS, sce0->ics.swb_sizes[g]);
  1108. maxval = find_max_val(1, sce0->ics.swb_sizes[g], I34);
  1109. is_band_type = find_min_book(maxval, is_sf_idx);
  1110. dist1 += quantize_band_cost(s, sce0->coeffs + start + (w+w2)*128,
  1111. L34,
  1112. sce0->ics.swb_sizes[g],
  1113. sce0->sf_idx[(w+w2)*16+g],
  1114. sce0->band_type[(w+w2)*16+g],
  1115. lambda / band0->threshold, INFINITY, NULL, 0);
  1116. dist1 += quantize_band_cost(s, sce1->coeffs + start + (w+w2)*128,
  1117. R34,
  1118. sce1->ics.swb_sizes[g],
  1119. sce1->sf_idx[(w+w2)*16+g],
  1120. sce1->band_type[(w+w2)*16+g],
  1121. lambda / band1->threshold, INFINITY, NULL, 0);
  1122. dist2 += quantize_band_cost(s, IS,
  1123. I34,
  1124. sce0->ics.swb_sizes[g],
  1125. is_sf_idx,
  1126. is_band_type,
  1127. lambda / minthr, INFINITY, NULL, 0);
  1128. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  1129. dist_spec_err += (L34[i] - I34[i])*(L34[i] - I34[i]);
  1130. dist_spec_err += (R34[i] - I34[i]*e01_34)*(R34[i] - I34[i]*e01_34);
  1131. }
  1132. dist_spec_err *= lambda / minthr;
  1133. dist2 += dist_spec_err;
  1134. }
  1135. if (dist2 <= dist1) {
  1136. cpe->is_mask[w*16+g] = 1;
  1137. cpe->ms_mask[w*16+g] = 0;
  1138. cpe->ch[0].is_ener[w*16+g] = sqrt(ener0/ener01);
  1139. cpe->ch[1].is_ener[w*16+g] = ener0/ener1;
  1140. if (phase)
  1141. cpe->ch[1].band_type[w*16+g] = INTENSITY_BT;
  1142. else
  1143. cpe->ch[1].band_type[w*16+g] = INTENSITY_BT2;
  1144. count++;
  1145. }
  1146. }
  1147. start += sce0->ics.swb_sizes[g];
  1148. }
  1149. }
  1150. cpe->is_mode = !!count;
  1151. }
  1152. static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
  1153. {
  1154. int start = 0, i, w, w2, g;
  1155. float M[128], S[128];
  1156. float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
  1157. const float lambda = s->lambda;
  1158. SingleChannelElement *sce0 = &cpe->ch[0];
  1159. SingleChannelElement *sce1 = &cpe->ch[1];
  1160. if (!cpe->common_window)
  1161. return;
  1162. for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
  1163. start = 0;
  1164. for (g = 0; g < sce0->ics.num_swb; g++) {
  1165. if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
  1166. float dist1 = 0.0f, dist2 = 0.0f;
  1167. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  1168. FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  1169. FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
  1170. float minthr = FFMIN(band0->threshold, band1->threshold);
  1171. float maxthr = FFMAX(band0->threshold, band1->threshold);
  1172. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  1173. M[i] = (sce0->pcoeffs[start+(w+w2)*128+i]
  1174. + sce1->pcoeffs[start+(w+w2)*128+i]) * 0.5;
  1175. S[i] = M[i]
  1176. - sce1->pcoeffs[start+(w+w2)*128+i];
  1177. }
  1178. abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  1179. abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  1180. abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  1181. abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  1182. dist1 += quantize_band_cost(s, sce0->coeffs + start + (w+w2)*128,
  1183. L34,
  1184. sce0->ics.swb_sizes[g],
  1185. sce0->sf_idx[(w+w2)*16+g],
  1186. sce0->band_type[(w+w2)*16+g],
  1187. lambda / band0->threshold, INFINITY, NULL, 0);
  1188. dist1 += quantize_band_cost(s, sce1->coeffs + start + (w+w2)*128,
  1189. R34,
  1190. sce1->ics.swb_sizes[g],
  1191. sce1->sf_idx[(w+w2)*16+g],
  1192. sce1->band_type[(w+w2)*16+g],
  1193. lambda / band1->threshold, INFINITY, NULL, 0);
  1194. dist2 += quantize_band_cost(s, M,
  1195. M34,
  1196. sce0->ics.swb_sizes[g],
  1197. sce0->sf_idx[(w+w2)*16+g],
  1198. sce0->band_type[(w+w2)*16+g],
  1199. lambda / maxthr, INFINITY, NULL, 0);
  1200. dist2 += quantize_band_cost(s, S,
  1201. S34,
  1202. sce1->ics.swb_sizes[g],
  1203. sce1->sf_idx[(w+w2)*16+g],
  1204. sce1->band_type[(w+w2)*16+g],
  1205. lambda / minthr, INFINITY, NULL, 0);
  1206. }
  1207. cpe->ms_mask[w*16+g] = dist2 < dist1;
  1208. }
  1209. start += sce0->ics.swb_sizes[g];
  1210. }
  1211. }
  1212. }
  1213. AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
  1214. [AAC_CODER_FAAC] = {
  1215. search_for_quantizers_faac,
  1216. encode_window_bands_info,
  1217. quantize_and_encode_band,
  1218. set_special_band_scalefactors,
  1219. search_for_pns,
  1220. search_for_ms,
  1221. search_for_is,
  1222. },
  1223. [AAC_CODER_ANMR] = {
  1224. search_for_quantizers_anmr,
  1225. encode_window_bands_info,
  1226. quantize_and_encode_band,
  1227. set_special_band_scalefactors,
  1228. search_for_pns,
  1229. search_for_ms,
  1230. search_for_is,
  1231. },
  1232. [AAC_CODER_TWOLOOP] = {
  1233. search_for_quantizers_twoloop,
  1234. codebook_trellis_rate,
  1235. quantize_and_encode_band,
  1236. set_special_band_scalefactors,
  1237. search_for_pns,
  1238. search_for_ms,
  1239. search_for_is,
  1240. },
  1241. [AAC_CODER_FAST] = {
  1242. search_for_quantizers_fast,
  1243. encode_window_bands_info,
  1244. quantize_and_encode_band,
  1245. set_special_band_scalefactors,
  1246. search_for_pns,
  1247. search_for_ms,
  1248. search_for_is,
  1249. },
  1250. };