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.

1141 lines
44KB

  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 <math.h>
  33. #include "avcodec.h"
  34. #include "put_bits.h"
  35. #include "aac.h"
  36. #include "aacenc.h"
  37. #include "aactab.h"
  38. /** bits needed to code codebook run value for long windows */
  39. static const uint8_t run_value_bits_long[64] = {
  40. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  41. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
  42. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  43. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
  44. };
  45. /** bits needed to code codebook run value for short windows */
  46. static const uint8_t run_value_bits_short[16] = {
  47. 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
  48. };
  49. static const uint8_t *run_value_bits[2] = {
  50. run_value_bits_long, run_value_bits_short
  51. };
  52. /**
  53. * Quantize one coefficient.
  54. * @return absolute value of the quantized coefficient
  55. * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
  56. */
  57. static av_always_inline int quant(float coef, const float Q)
  58. {
  59. float a = coef * Q;
  60. return sqrtf(a * sqrtf(a)) + 0.4054;
  61. }
  62. static void quantize_bands(int *out, const float *in, const float *scaled,
  63. int size, float Q34, int is_signed, int maxval)
  64. {
  65. int i;
  66. double qc;
  67. for (i = 0; i < size; i++) {
  68. qc = scaled[i] * Q34;
  69. out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
  70. if (is_signed && in[i] < 0.0f) {
  71. out[i] = -out[i];
  72. }
  73. }
  74. }
  75. static void abs_pow34_v(float *out, const float *in, const int size)
  76. {
  77. #ifndef USE_REALLY_FULL_SEARCH
  78. int i;
  79. for (i = 0; i < size; i++) {
  80. float a = fabsf(in[i]);
  81. out[i] = sqrtf(a * sqrtf(a));
  82. }
  83. #endif /* USE_REALLY_FULL_SEARCH */
  84. }
  85. static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
  86. static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
  87. /**
  88. * Calculate rate distortion cost for quantizing with given codebook
  89. *
  90. * @return quantization distortion
  91. */
  92. static av_always_inline float quantize_and_encode_band_cost_template(
  93. struct AACEncContext *s,
  94. PutBitContext *pb, const float *in,
  95. const float *scaled, int size, int scale_idx,
  96. int cb, const float lambda, const float uplim,
  97. int *bits, int BT_ZERO, int BT_UNSIGNED,
  98. int BT_PAIR, int BT_ESC)
  99. {
  100. const float IQ = ff_aac_pow2sf_tab[POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
  101. const float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
  102. const float CLIPPED_ESCAPE = 165140.0f*IQ;
  103. int i, j;
  104. float cost = 0;
  105. const int dim = BT_PAIR ? 2 : 4;
  106. int resbits = 0;
  107. const float Q34 = sqrtf(Q * sqrtf(Q));
  108. const int range = aac_cb_range[cb];
  109. const int maxval = aac_cb_maxval[cb];
  110. int off;
  111. if (BT_ZERO) {
  112. for (i = 0; i < size; i++)
  113. cost += in[i]*in[i];
  114. if (bits)
  115. *bits = 0;
  116. return cost * lambda;
  117. }
  118. if (!scaled) {
  119. abs_pow34_v(s->scoefs, in, size);
  120. scaled = s->scoefs;
  121. }
  122. quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
  123. if (BT_UNSIGNED) {
  124. off = 0;
  125. } else {
  126. off = maxval;
  127. }
  128. for (i = 0; i < size; i += dim) {
  129. const float *vec;
  130. int *quants = s->qcoefs + i;
  131. int curidx = 0;
  132. int curbits;
  133. float rd = 0.0f;
  134. for (j = 0; j < dim; j++) {
  135. curidx *= range;
  136. curidx += quants[j] + off;
  137. }
  138. curbits = ff_aac_spectral_bits[cb-1][curidx];
  139. vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
  140. if (BT_UNSIGNED) {
  141. for (j = 0; j < dim; j++) {
  142. float t = fabsf(in[i+j]);
  143. float di;
  144. if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
  145. if (t >= CLIPPED_ESCAPE) {
  146. di = t - CLIPPED_ESCAPE;
  147. curbits += 21;
  148. } else {
  149. int c = av_clip(quant(t, Q), 0, 8191);
  150. di = t - c*cbrtf(c)*IQ;
  151. curbits += av_log2(c)*2 - 4 + 1;
  152. }
  153. } else {
  154. di = t - vec[j]*IQ;
  155. }
  156. if (vec[j] != 0.0f)
  157. curbits++;
  158. rd += di*di;
  159. }
  160. } else {
  161. for (j = 0; j < dim; j++) {
  162. float di = in[i+j] - vec[j]*IQ;
  163. rd += di*di;
  164. }
  165. }
  166. cost += rd * lambda + curbits;
  167. resbits += curbits;
  168. if (cost >= uplim)
  169. return uplim;
  170. if (pb) {
  171. put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
  172. if (BT_UNSIGNED)
  173. for (j = 0; j < dim; j++)
  174. if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
  175. put_bits(pb, 1, in[i+j] < 0.0f);
  176. if (BT_ESC) {
  177. for (j = 0; j < 2; j++) {
  178. if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
  179. int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
  180. int len = av_log2(coef);
  181. put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
  182. put_bits(pb, len, coef & ((1 << len) - 1));
  183. }
  184. }
  185. }
  186. }
  187. }
  188. if (bits)
  189. *bits = resbits;
  190. return cost;
  191. }
  192. #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
  193. static float quantize_and_encode_band_cost_ ## NAME( \
  194. struct AACEncContext *s, \
  195. PutBitContext *pb, const float *in, \
  196. const float *scaled, int size, int scale_idx, \
  197. int cb, const float lambda, const float uplim, \
  198. int *bits) { \
  199. return quantize_and_encode_band_cost_template( \
  200. s, pb, in, scaled, size, scale_idx, \
  201. BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
  202. BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \
  203. }
  204. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0)
  205. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0)
  206. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0)
  207. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0)
  208. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0)
  209. QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1)
  210. static float (*const quantize_and_encode_band_cost_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,
  228. };
  229. #define quantize_and_encode_band_cost( \
  230. s, pb, in, scaled, size, scale_idx, cb, \
  231. lambda, uplim, bits) \
  232. quantize_and_encode_band_cost_arr[cb]( \
  233. s, pb, in, scaled, size, scale_idx, cb, \
  234. lambda, uplim, bits)
  235. static float quantize_band_cost(struct AACEncContext *s, const float *in,
  236. const float *scaled, int size, int scale_idx,
  237. int cb, const float lambda, const float uplim,
  238. int *bits)
  239. {
  240. return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
  241. cb, lambda, uplim, bits);
  242. }
  243. static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
  244. const float *in, int size, int scale_idx,
  245. int cb, const float lambda)
  246. {
  247. quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
  248. INFINITY, NULL);
  249. }
  250. static float find_max_val(int group_len, int swb_size, const float *scaled) {
  251. float maxval = 0.0f;
  252. int w2, i;
  253. for (w2 = 0; w2 < group_len; w2++) {
  254. for (i = 0; i < swb_size; i++) {
  255. maxval = FFMAX(maxval, scaled[w2*128+i]);
  256. }
  257. }
  258. return maxval;
  259. }
  260. static int find_min_book(float maxval, int sf) {
  261. float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
  262. float Q34 = sqrtf(Q * sqrtf(Q));
  263. int qmaxval, cb;
  264. qmaxval = maxval * Q34 + 0.4054f;
  265. if (qmaxval == 0) cb = 0;
  266. else if (qmaxval == 1) cb = 1;
  267. else if (qmaxval == 2) cb = 3;
  268. else if (qmaxval <= 4) cb = 5;
  269. else if (qmaxval <= 7) cb = 7;
  270. else if (qmaxval <= 12) cb = 9;
  271. else cb = 11;
  272. return cb;
  273. }
  274. /**
  275. * structure used in optimal codebook search
  276. */
  277. typedef struct BandCodingPath {
  278. int prev_idx; ///< pointer to the previous path point
  279. float cost; ///< path cost
  280. int run;
  281. } BandCodingPath;
  282. /**
  283. * Encode band info for single window group bands.
  284. */
  285. static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
  286. int win, int group_len, const float lambda)
  287. {
  288. BandCodingPath path[120][12];
  289. int w, swb, cb, start, size;
  290. int i, j;
  291. const int max_sfb = sce->ics.max_sfb;
  292. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  293. const int run_esc = (1 << run_bits) - 1;
  294. int idx, ppos, count;
  295. int stackrun[120], stackcb[120], stack_len;
  296. float next_minrd = INFINITY;
  297. int next_mincb = 0;
  298. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  299. start = win*128;
  300. for (cb = 0; cb < 12; cb++) {
  301. path[0][cb].cost = 0.0f;
  302. path[0][cb].prev_idx = -1;
  303. path[0][cb].run = 0;
  304. }
  305. for (swb = 0; swb < max_sfb; swb++) {
  306. size = sce->ics.swb_sizes[swb];
  307. if (sce->zeroes[win*16 + swb]) {
  308. for (cb = 0; cb < 12; cb++) {
  309. path[swb+1][cb].prev_idx = cb;
  310. path[swb+1][cb].cost = path[swb][cb].cost;
  311. path[swb+1][cb].run = path[swb][cb].run + 1;
  312. }
  313. } else {
  314. float minrd = next_minrd;
  315. int mincb = next_mincb;
  316. next_minrd = INFINITY;
  317. next_mincb = 0;
  318. for (cb = 0; cb < 12; cb++) {
  319. float cost_stay_here, cost_get_here;
  320. float rd = 0.0f;
  321. for (w = 0; w < group_len; w++) {
  322. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
  323. rd += quantize_band_cost(s, sce->coeffs + start + w*128,
  324. s->scoefs + start + w*128, size,
  325. sce->sf_idx[(win+w)*16+swb], cb,
  326. lambda / band->threshold, INFINITY, NULL);
  327. }
  328. cost_stay_here = path[swb][cb].cost + rd;
  329. cost_get_here = minrd + rd + run_bits + 4;
  330. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  331. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  332. cost_stay_here += run_bits;
  333. if (cost_get_here < cost_stay_here) {
  334. path[swb+1][cb].prev_idx = mincb;
  335. path[swb+1][cb].cost = cost_get_here;
  336. path[swb+1][cb].run = 1;
  337. } else {
  338. path[swb+1][cb].prev_idx = cb;
  339. path[swb+1][cb].cost = cost_stay_here;
  340. path[swb+1][cb].run = path[swb][cb].run + 1;
  341. }
  342. if (path[swb+1][cb].cost < next_minrd) {
  343. next_minrd = path[swb+1][cb].cost;
  344. next_mincb = cb;
  345. }
  346. }
  347. }
  348. start += sce->ics.swb_sizes[swb];
  349. }
  350. //convert resulting path from backward-linked list
  351. stack_len = 0;
  352. idx = 0;
  353. for (cb = 1; cb < 12; cb++)
  354. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  355. idx = cb;
  356. ppos = max_sfb;
  357. while (ppos > 0) {
  358. cb = idx;
  359. stackrun[stack_len] = path[ppos][cb].run;
  360. stackcb [stack_len] = cb;
  361. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  362. ppos -= path[ppos][cb].run;
  363. stack_len++;
  364. }
  365. //perform actual band info encoding
  366. start = 0;
  367. for (i = stack_len - 1; i >= 0; i--) {
  368. put_bits(&s->pb, 4, stackcb[i]);
  369. count = stackrun[i];
  370. memset(sce->zeroes + win*16 + start, !stackcb[i], count);
  371. //XXX: memset when band_type is also uint8_t
  372. for (j = 0; j < count; j++) {
  373. sce->band_type[win*16 + start] = stackcb[i];
  374. start++;
  375. }
  376. while (count >= run_esc) {
  377. put_bits(&s->pb, run_bits, run_esc);
  378. count -= run_esc;
  379. }
  380. put_bits(&s->pb, run_bits, count);
  381. }
  382. }
  383. static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
  384. int win, int group_len, const float lambda)
  385. {
  386. BandCodingPath path[120][12];
  387. int w, swb, cb, start, size;
  388. int i, j;
  389. const int max_sfb = sce->ics.max_sfb;
  390. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  391. const int run_esc = (1 << run_bits) - 1;
  392. int idx, ppos, count;
  393. int stackrun[120], stackcb[120], stack_len;
  394. float next_minrd = INFINITY;
  395. int next_mincb = 0;
  396. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  397. start = win*128;
  398. for (cb = 0; cb < 12; cb++) {
  399. path[0][cb].cost = run_bits+4;
  400. path[0][cb].prev_idx = -1;
  401. path[0][cb].run = 0;
  402. }
  403. for (swb = 0; swb < max_sfb; swb++) {
  404. size = sce->ics.swb_sizes[swb];
  405. if (sce->zeroes[win*16 + swb]) {
  406. float cost_stay_here = path[swb][0].cost;
  407. float cost_get_here = next_minrd + run_bits + 4;
  408. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
  409. != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
  410. cost_stay_here += run_bits;
  411. if (cost_get_here < cost_stay_here) {
  412. path[swb+1][0].prev_idx = next_mincb;
  413. path[swb+1][0].cost = cost_get_here;
  414. path[swb+1][0].run = 1;
  415. } else {
  416. path[swb+1][0].prev_idx = 0;
  417. path[swb+1][0].cost = cost_stay_here;
  418. path[swb+1][0].run = path[swb][0].run + 1;
  419. }
  420. next_minrd = path[swb+1][0].cost;
  421. next_mincb = 0;
  422. for (cb = 1; cb < 12; cb++) {
  423. path[swb+1][cb].cost = 61450;
  424. path[swb+1][cb].prev_idx = -1;
  425. path[swb+1][cb].run = 0;
  426. }
  427. } else {
  428. float minrd = next_minrd;
  429. int mincb = next_mincb;
  430. int startcb = sce->band_type[win*16+swb];
  431. next_minrd = INFINITY;
  432. next_mincb = 0;
  433. for (cb = 0; cb < startcb; cb++) {
  434. path[swb+1][cb].cost = 61450;
  435. path[swb+1][cb].prev_idx = -1;
  436. path[swb+1][cb].run = 0;
  437. }
  438. for (cb = startcb; cb < 12; cb++) {
  439. float cost_stay_here, cost_get_here;
  440. float rd = 0.0f;
  441. for (w = 0; w < group_len; w++) {
  442. rd += quantize_band_cost(s, sce->coeffs + start + w*128,
  443. s->scoefs + start + w*128, size,
  444. sce->sf_idx[(win+w)*16+swb], cb,
  445. 0, INFINITY, NULL);
  446. }
  447. cost_stay_here = path[swb][cb].cost + rd;
  448. cost_get_here = minrd + rd + run_bits + 4;
  449. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  450. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  451. cost_stay_here += run_bits;
  452. if (cost_get_here < cost_stay_here) {
  453. path[swb+1][cb].prev_idx = mincb;
  454. path[swb+1][cb].cost = cost_get_here;
  455. path[swb+1][cb].run = 1;
  456. } else {
  457. path[swb+1][cb].prev_idx = cb;
  458. path[swb+1][cb].cost = cost_stay_here;
  459. path[swb+1][cb].run = path[swb][cb].run + 1;
  460. }
  461. if (path[swb+1][cb].cost < next_minrd) {
  462. next_minrd = path[swb+1][cb].cost;
  463. next_mincb = cb;
  464. }
  465. }
  466. }
  467. start += sce->ics.swb_sizes[swb];
  468. }
  469. //convert resulting path from backward-linked list
  470. stack_len = 0;
  471. idx = 0;
  472. for (cb = 1; cb < 12; cb++)
  473. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  474. idx = cb;
  475. ppos = max_sfb;
  476. while (ppos > 0) {
  477. assert(idx >= 0);
  478. cb = idx;
  479. stackrun[stack_len] = path[ppos][cb].run;
  480. stackcb [stack_len] = cb;
  481. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  482. ppos -= path[ppos][cb].run;
  483. stack_len++;
  484. }
  485. //perform actual band info encoding
  486. start = 0;
  487. for (i = stack_len - 1; i >= 0; i--) {
  488. put_bits(&s->pb, 4, stackcb[i]);
  489. count = stackrun[i];
  490. memset(sce->zeroes + win*16 + start, !stackcb[i], count);
  491. //XXX: memset when band_type is also uint8_t
  492. for (j = 0; j < count; j++) {
  493. sce->band_type[win*16 + start] = stackcb[i];
  494. start++;
  495. }
  496. while (count >= run_esc) {
  497. put_bits(&s->pb, run_bits, run_esc);
  498. count -= run_esc;
  499. }
  500. put_bits(&s->pb, run_bits, count);
  501. }
  502. }
  503. /** Return the minimum scalefactor where the quantized coef does not clip. */
  504. static av_always_inline uint8_t coef2minsf(float coef) {
  505. return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
  506. }
  507. /** Return the maximum scalefactor where the quantized coef is not zero. */
  508. static av_always_inline uint8_t coef2maxsf(float coef) {
  509. return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
  510. }
  511. typedef struct TrellisPath {
  512. float cost;
  513. int prev;
  514. } TrellisPath;
  515. #define TRELLIS_STAGES 121
  516. #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
  517. static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
  518. SingleChannelElement *sce,
  519. const float lambda)
  520. {
  521. int q, w, w2, g, start = 0;
  522. int i, j;
  523. int idx;
  524. TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
  525. int bandaddr[TRELLIS_STAGES];
  526. int minq;
  527. float mincost;
  528. float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
  529. int q0, q1, qcnt = 0;
  530. for (i = 0; i < 1024; i++) {
  531. float t = fabsf(sce->coeffs[i]);
  532. if (t > 0.0f) {
  533. q0f = FFMIN(q0f, t);
  534. q1f = FFMAX(q1f, t);
  535. qnrgf += t*t;
  536. qcnt++;
  537. }
  538. }
  539. if (!qcnt) {
  540. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  541. memset(sce->zeroes, 1, sizeof(sce->zeroes));
  542. return;
  543. }
  544. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  545. q0 = coef2minsf(q0f);
  546. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  547. q1 = coef2maxsf(q1f);
  548. //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
  549. if (q1 - q0 > 60) {
  550. int q0low = q0;
  551. int q1high = q1;
  552. //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
  553. int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
  554. q1 = qnrg + 30;
  555. q0 = qnrg - 30;
  556. //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
  557. if (q0 < q0low) {
  558. q1 += q0low - q0;
  559. q0 = q0low;
  560. } else if (q1 > q1high) {
  561. q0 -= q1 - q1high;
  562. q1 = q1high;
  563. }
  564. }
  565. //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
  566. for (i = 0; i < TRELLIS_STATES; i++) {
  567. paths[0][i].cost = 0.0f;
  568. paths[0][i].prev = -1;
  569. }
  570. for (j = 1; j < TRELLIS_STAGES; j++) {
  571. for (i = 0; i < TRELLIS_STATES; i++) {
  572. paths[j][i].cost = INFINITY;
  573. paths[j][i].prev = -2;
  574. }
  575. }
  576. idx = 1;
  577. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  578. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  579. start = w*128;
  580. for (g = 0; g < sce->ics.num_swb; g++) {
  581. const float *coefs = sce->coeffs + start;
  582. float qmin, qmax;
  583. int nz = 0;
  584. bandaddr[idx] = w * 16 + g;
  585. qmin = INT_MAX;
  586. qmax = 0.0f;
  587. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  588. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  589. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  590. sce->zeroes[(w+w2)*16+g] = 1;
  591. continue;
  592. }
  593. sce->zeroes[(w+w2)*16+g] = 0;
  594. nz = 1;
  595. for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  596. float t = fabsf(coefs[w2*128+i]);
  597. if (t > 0.0f)
  598. qmin = FFMIN(qmin, t);
  599. qmax = FFMAX(qmax, t);
  600. }
  601. }
  602. if (nz) {
  603. int minscale, maxscale;
  604. float minrd = INFINITY;
  605. float maxval;
  606. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  607. minscale = coef2minsf(qmin);
  608. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  609. maxscale = coef2maxsf(qmax);
  610. minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
  611. maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
  612. maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
  613. for (q = minscale; q < maxscale; q++) {
  614. float dist = 0;
  615. int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
  616. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  617. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  618. dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
  619. q + q0, cb, lambda / band->threshold, INFINITY, NULL);
  620. }
  621. minrd = FFMIN(minrd, dist);
  622. for (i = 0; i < q1 - q0; i++) {
  623. float cost;
  624. cost = paths[idx - 1][i].cost + dist
  625. + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
  626. if (cost < paths[idx][q].cost) {
  627. paths[idx][q].cost = cost;
  628. paths[idx][q].prev = i;
  629. }
  630. }
  631. }
  632. } else {
  633. for (q = 0; q < q1 - q0; q++) {
  634. paths[idx][q].cost = paths[idx - 1][q].cost + 1;
  635. paths[idx][q].prev = q;
  636. }
  637. }
  638. sce->zeroes[w*16+g] = !nz;
  639. start += sce->ics.swb_sizes[g];
  640. idx++;
  641. }
  642. }
  643. idx--;
  644. mincost = paths[idx][0].cost;
  645. minq = 0;
  646. for (i = 1; i < TRELLIS_STATES; i++) {
  647. if (paths[idx][i].cost < mincost) {
  648. mincost = paths[idx][i].cost;
  649. minq = i;
  650. }
  651. }
  652. while (idx) {
  653. sce->sf_idx[bandaddr[idx]] = minq + q0;
  654. minq = paths[idx][minq].prev;
  655. idx--;
  656. }
  657. //set the same quantizers inside window groups
  658. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  659. for (g = 0; g < sce->ics.num_swb; g++)
  660. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  661. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  662. }
  663. /**
  664. * two-loop quantizers search taken from ISO 13818-7 Appendix C
  665. */
  666. static void search_for_quantizers_twoloop(AVCodecContext *avctx,
  667. AACEncContext *s,
  668. SingleChannelElement *sce,
  669. const float lambda)
  670. {
  671. int start = 0, i, w, w2, g;
  672. int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
  673. float dists[128], uplims[128];
  674. float maxvals[128];
  675. int fflag, minscaler;
  676. int its = 0;
  677. int allz = 0;
  678. float minthr = INFINITY;
  679. //XXX: some heuristic to determine initial quantizers will reduce search time
  680. memset(dists, 0, sizeof(dists));
  681. //determine zero bands and upper limits
  682. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  683. for (g = 0; g < sce->ics.num_swb; g++) {
  684. int nz = 0;
  685. float uplim = 0.0f;
  686. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  687. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  688. uplim += band->threshold;
  689. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  690. sce->zeroes[(w+w2)*16+g] = 1;
  691. continue;
  692. }
  693. nz = 1;
  694. }
  695. uplims[w*16+g] = uplim *512;
  696. sce->zeroes[w*16+g] = !nz;
  697. if (nz)
  698. minthr = FFMIN(minthr, uplim);
  699. allz |= nz;
  700. }
  701. }
  702. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  703. for (g = 0; g < sce->ics.num_swb; g++) {
  704. if (sce->zeroes[w*16+g]) {
  705. sce->sf_idx[w*16+g] = SCALE_ONE_POS;
  706. continue;
  707. }
  708. sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
  709. }
  710. }
  711. if (!allz)
  712. return;
  713. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  714. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  715. start = w*128;
  716. for (g = 0; g < sce->ics.num_swb; g++) {
  717. const float *scaled = s->scoefs + start;
  718. maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
  719. start += sce->ics.swb_sizes[g];
  720. }
  721. }
  722. //perform two-loop search
  723. //outer loop - improve quality
  724. do {
  725. int tbits, qstep;
  726. minscaler = sce->sf_idx[0];
  727. //inner loop - quantize spectrum to fit into given number of bits
  728. qstep = its ? 1 : 32;
  729. do {
  730. int prev = -1;
  731. tbits = 0;
  732. fflag = 0;
  733. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  734. start = w*128;
  735. for (g = 0; g < sce->ics.num_swb; g++) {
  736. const float *coefs = sce->coeffs + start;
  737. const float *scaled = s->scoefs + start;
  738. int bits = 0;
  739. int cb;
  740. float dist = 0.0f;
  741. if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
  742. start += sce->ics.swb_sizes[g];
  743. continue;
  744. }
  745. minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
  746. cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  747. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  748. int b;
  749. dist += quantize_band_cost(s, coefs + w2*128,
  750. scaled + w2*128,
  751. sce->ics.swb_sizes[g],
  752. sce->sf_idx[w*16+g],
  753. cb,
  754. 1.0f,
  755. INFINITY,
  756. &b);
  757. bits += b;
  758. }
  759. dists[w*16+g] = dist - bits;
  760. if (prev != -1) {
  761. bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
  762. }
  763. tbits += bits;
  764. start += sce->ics.swb_sizes[g];
  765. prev = sce->sf_idx[w*16+g];
  766. }
  767. }
  768. if (tbits > destbits) {
  769. for (i = 0; i < 128; i++)
  770. if (sce->sf_idx[i] < 218 - qstep)
  771. sce->sf_idx[i] += qstep;
  772. } else {
  773. for (i = 0; i < 128; i++)
  774. if (sce->sf_idx[i] > 60 - qstep)
  775. sce->sf_idx[i] -= qstep;
  776. }
  777. qstep >>= 1;
  778. if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
  779. qstep = 1;
  780. } while (qstep);
  781. fflag = 0;
  782. minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
  783. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  784. for (g = 0; g < sce->ics.num_swb; g++) {
  785. int prevsc = sce->sf_idx[w*16+g];
  786. if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
  787. if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
  788. sce->sf_idx[w*16+g]--;
  789. else //Try to make sure there is some energy in every band
  790. sce->sf_idx[w*16+g]-=2;
  791. }
  792. sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
  793. sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
  794. if (sce->sf_idx[w*16+g] != prevsc)
  795. fflag = 1;
  796. sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  797. }
  798. }
  799. its++;
  800. } while (fflag && its < 10);
  801. }
  802. static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
  803. SingleChannelElement *sce,
  804. const float lambda)
  805. {
  806. int start = 0, i, w, w2, g;
  807. float uplim[128], maxq[128];
  808. int minq, maxsf;
  809. float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
  810. int last = 0, lastband = 0, curband = 0;
  811. float avg_energy = 0.0;
  812. if (sce->ics.num_windows == 1) {
  813. start = 0;
  814. for (i = 0; i < 1024; i++) {
  815. if (i - start >= sce->ics.swb_sizes[curband]) {
  816. start += sce->ics.swb_sizes[curband];
  817. curband++;
  818. }
  819. if (sce->coeffs[i]) {
  820. avg_energy += sce->coeffs[i] * sce->coeffs[i];
  821. last = i;
  822. lastband = curband;
  823. }
  824. }
  825. } else {
  826. for (w = 0; w < 8; w++) {
  827. const float *coeffs = sce->coeffs + w*128;
  828. start = 0;
  829. for (i = 0; i < 128; i++) {
  830. if (i - start >= sce->ics.swb_sizes[curband]) {
  831. start += sce->ics.swb_sizes[curband];
  832. curband++;
  833. }
  834. if (coeffs[i]) {
  835. avg_energy += coeffs[i] * coeffs[i];
  836. last = FFMAX(last, i);
  837. lastband = FFMAX(lastband, curband);
  838. }
  839. }
  840. }
  841. }
  842. last++;
  843. avg_energy /= last;
  844. if (avg_energy == 0.0f) {
  845. for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
  846. sce->sf_idx[i] = SCALE_ONE_POS;
  847. return;
  848. }
  849. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  850. start = w*128;
  851. for (g = 0; g < sce->ics.num_swb; g++) {
  852. float *coefs = sce->coeffs + start;
  853. const int size = sce->ics.swb_sizes[g];
  854. int start2 = start, end2 = start + size, peakpos = start;
  855. float maxval = -1, thr = 0.0f, t;
  856. maxq[w*16+g] = 0.0f;
  857. if (g > lastband) {
  858. maxq[w*16+g] = 0.0f;
  859. start += size;
  860. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  861. memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
  862. continue;
  863. }
  864. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  865. for (i = 0; i < size; i++) {
  866. float t = coefs[w2*128+i]*coefs[w2*128+i];
  867. maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
  868. thr += t;
  869. if (sce->ics.num_windows == 1 && maxval < t) {
  870. maxval = t;
  871. peakpos = start+i;
  872. }
  873. }
  874. }
  875. if (sce->ics.num_windows == 1) {
  876. start2 = FFMAX(peakpos - 2, start2);
  877. end2 = FFMIN(peakpos + 3, end2);
  878. } else {
  879. start2 -= start;
  880. end2 -= start;
  881. }
  882. start += size;
  883. thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
  884. t = 1.0 - (1.0 * start2 / last);
  885. uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
  886. }
  887. }
  888. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  889. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  890. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  891. start = w*128;
  892. for (g = 0; g < sce->ics.num_swb; g++) {
  893. const float *coefs = sce->coeffs + start;
  894. const float *scaled = s->scoefs + start;
  895. const int size = sce->ics.swb_sizes[g];
  896. int scf, prev_scf, step;
  897. int min_scf = -1, max_scf = 256;
  898. float curdiff;
  899. if (maxq[w*16+g] < 21.544) {
  900. sce->zeroes[w*16+g] = 1;
  901. start += size;
  902. continue;
  903. }
  904. sce->zeroes[w*16+g] = 0;
  905. scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
  906. step = 16;
  907. for (;;) {
  908. float dist = 0.0f;
  909. int quant_max;
  910. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  911. int b;
  912. dist += quantize_band_cost(s, coefs + w2*128,
  913. scaled + w2*128,
  914. sce->ics.swb_sizes[g],
  915. scf,
  916. ESC_BT,
  917. lambda,
  918. INFINITY,
  919. &b);
  920. dist -= b;
  921. }
  922. dist *= 1.0f / 512.0f / lambda;
  923. quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
  924. if (quant_max >= 8191) { // too much, return to the previous quantizer
  925. sce->sf_idx[w*16+g] = prev_scf;
  926. break;
  927. }
  928. prev_scf = scf;
  929. curdiff = fabsf(dist - uplim[w*16+g]);
  930. if (curdiff <= 1.0f)
  931. step = 0;
  932. else
  933. step = log2f(curdiff);
  934. if (dist > uplim[w*16+g])
  935. step = -step;
  936. scf += step;
  937. scf = av_clip_uint8(scf);
  938. step = scf - prev_scf;
  939. if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
  940. sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
  941. break;
  942. }
  943. if (step > 0)
  944. min_scf = prev_scf;
  945. else
  946. max_scf = prev_scf;
  947. }
  948. start += size;
  949. }
  950. }
  951. minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
  952. for (i = 1; i < 128; i++) {
  953. if (!sce->sf_idx[i])
  954. sce->sf_idx[i] = sce->sf_idx[i-1];
  955. else
  956. minq = FFMIN(minq, sce->sf_idx[i]);
  957. }
  958. if (minq == INT_MAX)
  959. minq = 0;
  960. minq = FFMIN(minq, SCALE_MAX_POS);
  961. maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
  962. for (i = 126; i >= 0; i--) {
  963. if (!sce->sf_idx[i])
  964. sce->sf_idx[i] = sce->sf_idx[i+1];
  965. sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
  966. }
  967. }
  968. static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
  969. SingleChannelElement *sce,
  970. const float lambda)
  971. {
  972. int i, w, w2, g;
  973. int minq = 255;
  974. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  975. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  976. for (g = 0; g < sce->ics.num_swb; g++) {
  977. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  978. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  979. if (band->energy <= band->threshold) {
  980. sce->sf_idx[(w+w2)*16+g] = 218;
  981. sce->zeroes[(w+w2)*16+g] = 1;
  982. } else {
  983. sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
  984. sce->zeroes[(w+w2)*16+g] = 0;
  985. }
  986. minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
  987. }
  988. }
  989. }
  990. for (i = 0; i < 128; i++) {
  991. sce->sf_idx[i] = 140;
  992. //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
  993. }
  994. //set the same quantizers inside window groups
  995. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  996. for (g = 0; g < sce->ics.num_swb; g++)
  997. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  998. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  999. }
  1000. static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
  1001. const float lambda)
  1002. {
  1003. int start = 0, i, w, w2, g;
  1004. float M[128], S[128];
  1005. float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
  1006. SingleChannelElement *sce0 = &cpe->ch[0];
  1007. SingleChannelElement *sce1 = &cpe->ch[1];
  1008. if (!cpe->common_window)
  1009. return;
  1010. for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
  1011. for (g = 0; g < sce0->ics.num_swb; g++) {
  1012. if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
  1013. float dist1 = 0.0f, dist2 = 0.0f;
  1014. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  1015. FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  1016. FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
  1017. float minthr = FFMIN(band0->threshold, band1->threshold);
  1018. float maxthr = FFMAX(band0->threshold, band1->threshold);
  1019. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  1020. M[i] = (sce0->coeffs[start+w2*128+i]
  1021. + sce1->coeffs[start+w2*128+i]) * 0.5;
  1022. S[i] = M[i]
  1023. - sce1->coeffs[start+w2*128+i];
  1024. }
  1025. abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
  1026. abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
  1027. abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  1028. abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  1029. dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
  1030. L34,
  1031. sce0->ics.swb_sizes[g],
  1032. sce0->sf_idx[(w+w2)*16+g],
  1033. sce0->band_type[(w+w2)*16+g],
  1034. lambda / band0->threshold, INFINITY, NULL);
  1035. dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
  1036. R34,
  1037. sce1->ics.swb_sizes[g],
  1038. sce1->sf_idx[(w+w2)*16+g],
  1039. sce1->band_type[(w+w2)*16+g],
  1040. lambda / band1->threshold, INFINITY, NULL);
  1041. dist2 += quantize_band_cost(s, M,
  1042. M34,
  1043. sce0->ics.swb_sizes[g],
  1044. sce0->sf_idx[(w+w2)*16+g],
  1045. sce0->band_type[(w+w2)*16+g],
  1046. lambda / maxthr, INFINITY, NULL);
  1047. dist2 += quantize_band_cost(s, S,
  1048. S34,
  1049. sce1->ics.swb_sizes[g],
  1050. sce1->sf_idx[(w+w2)*16+g],
  1051. sce1->band_type[(w+w2)*16+g],
  1052. lambda / minthr, INFINITY, NULL);
  1053. }
  1054. cpe->ms_mask[w*16+g] = dist2 < dist1;
  1055. }
  1056. start += sce0->ics.swb_sizes[g];
  1057. }
  1058. }
  1059. }
  1060. AACCoefficientsEncoder ff_aac_coders[] = {
  1061. {
  1062. search_for_quantizers_faac,
  1063. encode_window_bands_info,
  1064. quantize_and_encode_band,
  1065. search_for_ms,
  1066. },
  1067. {
  1068. search_for_quantizers_anmr,
  1069. encode_window_bands_info,
  1070. quantize_and_encode_band,
  1071. search_for_ms,
  1072. },
  1073. {
  1074. search_for_quantizers_twoloop,
  1075. codebook_trellis_rate,
  1076. quantize_and_encode_band,
  1077. search_for_ms,
  1078. },
  1079. {
  1080. search_for_quantizers_fast,
  1081. encode_window_bands_info,
  1082. quantize_and_encode_band,
  1083. search_for_ms,
  1084. },
  1085. };