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.

1126 lines
43KB

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