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.

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