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.

1140 lines
44KB

  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 "libavutil/libm.h" // brought forward to work around cygwin header breakage
  31. #include <float.h>
  32. #include "avcodec.h"
  33. #include "put_bits.h"
  34. #include "aac.h"
  35. #include "aacenc.h"
  36. #include "aactab.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, 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. size = sce->ics.swb_sizes[swb];
  306. if (sce->zeroes[win*16 + swb]) {
  307. for (cb = 0; cb < 12; cb++) {
  308. path[swb+1][cb].prev_idx = cb;
  309. path[swb+1][cb].cost = path[swb][cb].cost;
  310. path[swb+1][cb].run = path[swb][cb].run + 1;
  311. }
  312. } else {
  313. float minrd = next_minrd;
  314. int mincb = next_mincb;
  315. next_minrd = INFINITY;
  316. next_mincb = 0;
  317. for (cb = 0; cb < 12; cb++) {
  318. float cost_stay_here, cost_get_here;
  319. float rd = 0.0f;
  320. for (w = 0; w < group_len; w++) {
  321. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
  322. rd += quantize_band_cost(s, sce->coeffs + start + w*128,
  323. s->scoefs + start + w*128, size,
  324. sce->sf_idx[(win+w)*16+swb], cb,
  325. lambda / band->threshold, INFINITY, NULL);
  326. }
  327. cost_stay_here = path[swb][cb].cost + rd;
  328. cost_get_here = minrd + rd + run_bits + 4;
  329. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  330. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  331. cost_stay_here += run_bits;
  332. if (cost_get_here < cost_stay_here) {
  333. path[swb+1][cb].prev_idx = mincb;
  334. path[swb+1][cb].cost = cost_get_here;
  335. path[swb+1][cb].run = 1;
  336. } else {
  337. path[swb+1][cb].prev_idx = cb;
  338. path[swb+1][cb].cost = cost_stay_here;
  339. path[swb+1][cb].run = path[swb][cb].run + 1;
  340. }
  341. if (path[swb+1][cb].cost < next_minrd) {
  342. next_minrd = path[swb+1][cb].cost;
  343. next_mincb = cb;
  344. }
  345. }
  346. }
  347. start += sce->ics.swb_sizes[swb];
  348. }
  349. //convert resulting path from backward-linked list
  350. stack_len = 0;
  351. idx = 0;
  352. for (cb = 1; cb < 12; cb++)
  353. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  354. idx = cb;
  355. ppos = max_sfb;
  356. while (ppos > 0) {
  357. cb = idx;
  358. stackrun[stack_len] = path[ppos][cb].run;
  359. stackcb [stack_len] = cb;
  360. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  361. ppos -= path[ppos][cb].run;
  362. stack_len++;
  363. }
  364. //perform actual band info encoding
  365. start = 0;
  366. for (i = stack_len - 1; i >= 0; i--) {
  367. put_bits(&s->pb, 4, stackcb[i]);
  368. count = stackrun[i];
  369. memset(sce->zeroes + win*16 + start, !stackcb[i], count);
  370. //XXX: memset when band_type is also uint8_t
  371. for (j = 0; j < count; j++) {
  372. sce->band_type[win*16 + start] = stackcb[i];
  373. start++;
  374. }
  375. while (count >= run_esc) {
  376. put_bits(&s->pb, run_bits, run_esc);
  377. count -= run_esc;
  378. }
  379. put_bits(&s->pb, run_bits, count);
  380. }
  381. }
  382. static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
  383. int win, int group_len, const float lambda)
  384. {
  385. BandCodingPath path[120][12];
  386. int w, swb, cb, start, size;
  387. int i, j;
  388. const int max_sfb = sce->ics.max_sfb;
  389. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  390. const int run_esc = (1 << run_bits) - 1;
  391. int idx, ppos, count;
  392. int stackrun[120], stackcb[120], stack_len;
  393. float next_minrd = INFINITY;
  394. int next_mincb = 0;
  395. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  396. start = win*128;
  397. for (cb = 0; cb < 12; cb++) {
  398. path[0][cb].cost = run_bits+4;
  399. path[0][cb].prev_idx = -1;
  400. path[0][cb].run = 0;
  401. }
  402. for (swb = 0; swb < max_sfb; swb++) {
  403. size = sce->ics.swb_sizes[swb];
  404. if (sce->zeroes[win*16 + swb]) {
  405. float cost_stay_here = path[swb][0].cost;
  406. float cost_get_here = next_minrd + run_bits + 4;
  407. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
  408. != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
  409. cost_stay_here += run_bits;
  410. if (cost_get_here < cost_stay_here) {
  411. path[swb+1][0].prev_idx = next_mincb;
  412. path[swb+1][0].cost = cost_get_here;
  413. path[swb+1][0].run = 1;
  414. } else {
  415. path[swb+1][0].prev_idx = 0;
  416. path[swb+1][0].cost = cost_stay_here;
  417. path[swb+1][0].run = path[swb][0].run + 1;
  418. }
  419. next_minrd = path[swb+1][0].cost;
  420. next_mincb = 0;
  421. for (cb = 1; cb < 12; cb++) {
  422. path[swb+1][cb].cost = 61450;
  423. path[swb+1][cb].prev_idx = -1;
  424. path[swb+1][cb].run = 0;
  425. }
  426. } else {
  427. float minrd = next_minrd;
  428. int mincb = next_mincb;
  429. int startcb = sce->band_type[win*16+swb];
  430. next_minrd = INFINITY;
  431. next_mincb = 0;
  432. for (cb = 0; cb < startcb; cb++) {
  433. path[swb+1][cb].cost = 61450;
  434. path[swb+1][cb].prev_idx = -1;
  435. path[swb+1][cb].run = 0;
  436. }
  437. for (cb = startcb; cb < 12; cb++) {
  438. float cost_stay_here, cost_get_here;
  439. float rd = 0.0f;
  440. for (w = 0; w < group_len; w++) {
  441. rd += quantize_band_cost(s, sce->coeffs + start + w*128,
  442. s->scoefs + start + w*128, size,
  443. sce->sf_idx[(win+w)*16+swb], cb,
  444. 0, INFINITY, NULL);
  445. }
  446. cost_stay_here = path[swb][cb].cost + rd;
  447. cost_get_here = minrd + rd + run_bits + 4;
  448. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  449. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  450. cost_stay_here += run_bits;
  451. if (cost_get_here < cost_stay_here) {
  452. path[swb+1][cb].prev_idx = mincb;
  453. path[swb+1][cb].cost = cost_get_here;
  454. path[swb+1][cb].run = 1;
  455. } else {
  456. path[swb+1][cb].prev_idx = cb;
  457. path[swb+1][cb].cost = cost_stay_here;
  458. path[swb+1][cb].run = path[swb][cb].run + 1;
  459. }
  460. if (path[swb+1][cb].cost < next_minrd) {
  461. next_minrd = path[swb+1][cb].cost;
  462. next_mincb = cb;
  463. }
  464. }
  465. }
  466. start += sce->ics.swb_sizes[swb];
  467. }
  468. //convert resulting path from backward-linked list
  469. stack_len = 0;
  470. idx = 0;
  471. for (cb = 1; cb < 12; cb++)
  472. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  473. idx = cb;
  474. ppos = max_sfb;
  475. while (ppos > 0) {
  476. assert(idx >= 0);
  477. cb = idx;
  478. stackrun[stack_len] = path[ppos][cb].run;
  479. stackcb [stack_len] = cb;
  480. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  481. ppos -= path[ppos][cb].run;
  482. stack_len++;
  483. }
  484. //perform actual band info encoding
  485. start = 0;
  486. for (i = stack_len - 1; i >= 0; i--) {
  487. put_bits(&s->pb, 4, stackcb[i]);
  488. count = stackrun[i];
  489. memset(sce->zeroes + win*16 + start, !stackcb[i], count);
  490. //XXX: memset when band_type is also uint8_t
  491. for (j = 0; j < count; j++) {
  492. sce->band_type[win*16 + start] = stackcb[i];
  493. start++;
  494. }
  495. while (count >= run_esc) {
  496. put_bits(&s->pb, run_bits, run_esc);
  497. count -= run_esc;
  498. }
  499. put_bits(&s->pb, run_bits, count);
  500. }
  501. }
  502. /** Return the minimum scalefactor where the quantized coef does not clip. */
  503. static av_always_inline uint8_t coef2minsf(float coef) {
  504. return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
  505. }
  506. /** Return the maximum scalefactor where the quantized coef is not zero. */
  507. static av_always_inline uint8_t coef2maxsf(float coef) {
  508. return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
  509. }
  510. typedef struct TrellisPath {
  511. float cost;
  512. int prev;
  513. } TrellisPath;
  514. #define TRELLIS_STAGES 121
  515. #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
  516. static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
  517. SingleChannelElement *sce,
  518. const float lambda)
  519. {
  520. int q, w, w2, g, start = 0;
  521. int i, j;
  522. int idx;
  523. TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
  524. int bandaddr[TRELLIS_STAGES];
  525. int minq;
  526. float mincost;
  527. float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
  528. int q0, q1, qcnt = 0;
  529. for (i = 0; i < 1024; i++) {
  530. float t = fabsf(sce->coeffs[i]);
  531. if (t > 0.0f) {
  532. q0f = FFMIN(q0f, t);
  533. q1f = FFMAX(q1f, t);
  534. qnrgf += t*t;
  535. qcnt++;
  536. }
  537. }
  538. if (!qcnt) {
  539. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  540. memset(sce->zeroes, 1, sizeof(sce->zeroes));
  541. return;
  542. }
  543. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  544. q0 = coef2minsf(q0f);
  545. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  546. q1 = coef2maxsf(q1f);
  547. //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
  548. if (q1 - q0 > 60) {
  549. int q0low = q0;
  550. int q1high = q1;
  551. //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
  552. int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
  553. q1 = qnrg + 30;
  554. q0 = qnrg - 30;
  555. //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
  556. if (q0 < q0low) {
  557. q1 += q0low - q0;
  558. q0 = q0low;
  559. } else if (q1 > q1high) {
  560. q0 -= q1 - q1high;
  561. q1 = q1high;
  562. }
  563. }
  564. //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
  565. for (i = 0; i < TRELLIS_STATES; i++) {
  566. paths[0][i].cost = 0.0f;
  567. paths[0][i].prev = -1;
  568. }
  569. for (j = 1; j < TRELLIS_STAGES; j++) {
  570. for (i = 0; i < TRELLIS_STATES; i++) {
  571. paths[j][i].cost = INFINITY;
  572. paths[j][i].prev = -2;
  573. }
  574. }
  575. idx = 1;
  576. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  577. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  578. start = w*128;
  579. for (g = 0; g < sce->ics.num_swb; g++) {
  580. const float *coefs = sce->coeffs + start;
  581. float qmin, qmax;
  582. int nz = 0;
  583. bandaddr[idx] = w * 16 + g;
  584. qmin = INT_MAX;
  585. qmax = 0.0f;
  586. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  587. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  588. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  589. sce->zeroes[(w+w2)*16+g] = 1;
  590. continue;
  591. }
  592. sce->zeroes[(w+w2)*16+g] = 0;
  593. nz = 1;
  594. for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  595. float t = fabsf(coefs[w2*128+i]);
  596. if (t > 0.0f)
  597. qmin = FFMIN(qmin, t);
  598. qmax = FFMAX(qmax, t);
  599. }
  600. }
  601. if (nz) {
  602. int minscale, maxscale;
  603. float minrd = INFINITY;
  604. float maxval;
  605. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  606. minscale = coef2minsf(qmin);
  607. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  608. maxscale = coef2maxsf(qmax);
  609. minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
  610. maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
  611. maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
  612. for (q = minscale; q < maxscale; q++) {
  613. float dist = 0;
  614. int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
  615. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  616. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  617. dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
  618. q + q0, cb, lambda / band->threshold, INFINITY, NULL);
  619. }
  620. minrd = FFMIN(minrd, dist);
  621. for (i = 0; i < q1 - q0; i++) {
  622. float cost;
  623. cost = paths[idx - 1][i].cost + dist
  624. + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
  625. if (cost < paths[idx][q].cost) {
  626. paths[idx][q].cost = cost;
  627. paths[idx][q].prev = i;
  628. }
  629. }
  630. }
  631. } else {
  632. for (q = 0; q < q1 - q0; q++) {
  633. paths[idx][q].cost = paths[idx - 1][q].cost + 1;
  634. paths[idx][q].prev = q;
  635. }
  636. }
  637. sce->zeroes[w*16+g] = !nz;
  638. start += sce->ics.swb_sizes[g];
  639. idx++;
  640. }
  641. }
  642. idx--;
  643. mincost = paths[idx][0].cost;
  644. minq = 0;
  645. for (i = 1; i < TRELLIS_STATES; i++) {
  646. if (paths[idx][i].cost < mincost) {
  647. mincost = paths[idx][i].cost;
  648. minq = i;
  649. }
  650. }
  651. while (idx) {
  652. sce->sf_idx[bandaddr[idx]] = minq + q0;
  653. minq = paths[idx][minq].prev;
  654. idx--;
  655. }
  656. //set the same quantizers inside window groups
  657. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  658. for (g = 0; g < sce->ics.num_swb; g++)
  659. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  660. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  661. }
  662. /**
  663. * two-loop quantizers search taken from ISO 13818-7 Appendix C
  664. */
  665. static void search_for_quantizers_twoloop(AVCodecContext *avctx,
  666. AACEncContext *s,
  667. SingleChannelElement *sce,
  668. const float lambda)
  669. {
  670. int start = 0, i, w, w2, g;
  671. int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
  672. float dists[128], uplims[128];
  673. float maxvals[128];
  674. int fflag, minscaler;
  675. int its = 0;
  676. int allz = 0;
  677. float minthr = INFINITY;
  678. //XXX: some heuristic to determine initial quantizers will reduce search time
  679. memset(dists, 0, sizeof(dists));
  680. //determine zero bands and upper limits
  681. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  682. for (g = 0; g < sce->ics.num_swb; g++) {
  683. int nz = 0;
  684. float uplim = 0.0f;
  685. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  686. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  687. uplim += band->threshold;
  688. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  689. sce->zeroes[(w+w2)*16+g] = 1;
  690. continue;
  691. }
  692. nz = 1;
  693. }
  694. uplims[w*16+g] = uplim *512;
  695. sce->zeroes[w*16+g] = !nz;
  696. if (nz)
  697. minthr = FFMIN(minthr, uplim);
  698. allz |= nz;
  699. }
  700. }
  701. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  702. for (g = 0; g < sce->ics.num_swb; g++) {
  703. if (sce->zeroes[w*16+g]) {
  704. sce->sf_idx[w*16+g] = SCALE_ONE_POS;
  705. continue;
  706. }
  707. sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
  708. }
  709. }
  710. if (!allz)
  711. return;
  712. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  713. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  714. start = w*128;
  715. for (g = 0; g < sce->ics.num_swb; g++) {
  716. const float *scaled = s->scoefs + start;
  717. maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
  718. start += sce->ics.swb_sizes[g];
  719. }
  720. }
  721. //perform two-loop search
  722. //outer loop - improve quality
  723. do {
  724. int tbits, qstep;
  725. minscaler = sce->sf_idx[0];
  726. //inner loop - quantize spectrum to fit into given number of bits
  727. qstep = its ? 1 : 32;
  728. do {
  729. int prev = -1;
  730. tbits = 0;
  731. fflag = 0;
  732. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  733. start = w*128;
  734. for (g = 0; g < sce->ics.num_swb; g++) {
  735. const float *coefs = sce->coeffs + start;
  736. const float *scaled = s->scoefs + start;
  737. int bits = 0;
  738. int cb;
  739. float dist = 0.0f;
  740. if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
  741. start += sce->ics.swb_sizes[g];
  742. continue;
  743. }
  744. minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
  745. cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  746. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  747. int b;
  748. dist += quantize_band_cost(s, coefs + w2*128,
  749. scaled + w2*128,
  750. sce->ics.swb_sizes[g],
  751. sce->sf_idx[w*16+g],
  752. cb,
  753. 1.0f,
  754. INFINITY,
  755. &b);
  756. bits += b;
  757. }
  758. dists[w*16+g] = dist - bits;
  759. if (prev != -1) {
  760. bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
  761. }
  762. tbits += bits;
  763. start += sce->ics.swb_sizes[g];
  764. prev = sce->sf_idx[w*16+g];
  765. }
  766. }
  767. if (tbits > destbits) {
  768. for (i = 0; i < 128; i++)
  769. if (sce->sf_idx[i] < 218 - qstep)
  770. sce->sf_idx[i] += qstep;
  771. } else {
  772. for (i = 0; i < 128; i++)
  773. if (sce->sf_idx[i] > 60 - qstep)
  774. sce->sf_idx[i] -= qstep;
  775. }
  776. qstep >>= 1;
  777. if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
  778. qstep = 1;
  779. } while (qstep);
  780. fflag = 0;
  781. minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
  782. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  783. for (g = 0; g < sce->ics.num_swb; g++) {
  784. int prevsc = sce->sf_idx[w*16+g];
  785. if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
  786. if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
  787. sce->sf_idx[w*16+g]--;
  788. else //Try to make sure there is some energy in every band
  789. sce->sf_idx[w*16+g]-=2;
  790. }
  791. sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
  792. sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
  793. if (sce->sf_idx[w*16+g] != prevsc)
  794. fflag = 1;
  795. sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  796. }
  797. }
  798. its++;
  799. } while (fflag && its < 10);
  800. }
  801. static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
  802. SingleChannelElement *sce,
  803. const float lambda)
  804. {
  805. int start = 0, i, w, w2, g;
  806. float uplim[128], maxq[128];
  807. int minq, maxsf;
  808. float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
  809. int last = 0, lastband = 0, curband = 0;
  810. float avg_energy = 0.0;
  811. if (sce->ics.num_windows == 1) {
  812. start = 0;
  813. for (i = 0; i < 1024; i++) {
  814. if (i - start >= sce->ics.swb_sizes[curband]) {
  815. start += sce->ics.swb_sizes[curband];
  816. curband++;
  817. }
  818. if (sce->coeffs[i]) {
  819. avg_energy += sce->coeffs[i] * sce->coeffs[i];
  820. last = i;
  821. lastband = curband;
  822. }
  823. }
  824. } else {
  825. for (w = 0; w < 8; w++) {
  826. const float *coeffs = sce->coeffs + w*128;
  827. start = 0;
  828. for (i = 0; i < 128; i++) {
  829. if (i - start >= sce->ics.swb_sizes[curband]) {
  830. start += sce->ics.swb_sizes[curband];
  831. curband++;
  832. }
  833. if (coeffs[i]) {
  834. avg_energy += coeffs[i] * coeffs[i];
  835. last = FFMAX(last, i);
  836. lastband = FFMAX(lastband, curband);
  837. }
  838. }
  839. }
  840. }
  841. last++;
  842. avg_energy /= last;
  843. if (avg_energy == 0.0f) {
  844. for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
  845. sce->sf_idx[i] = SCALE_ONE_POS;
  846. return;
  847. }
  848. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  849. start = w*128;
  850. for (g = 0; g < sce->ics.num_swb; g++) {
  851. float *coefs = sce->coeffs + start;
  852. const int size = sce->ics.swb_sizes[g];
  853. int start2 = start, end2 = start + size, peakpos = start;
  854. float maxval = -1, thr = 0.0f, t;
  855. maxq[w*16+g] = 0.0f;
  856. if (g > lastband) {
  857. maxq[w*16+g] = 0.0f;
  858. start += size;
  859. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  860. memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
  861. continue;
  862. }
  863. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  864. for (i = 0; i < size; i++) {
  865. float t = coefs[w2*128+i]*coefs[w2*128+i];
  866. maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
  867. thr += t;
  868. if (sce->ics.num_windows == 1 && maxval < t) {
  869. maxval = t;
  870. peakpos = start+i;
  871. }
  872. }
  873. }
  874. if (sce->ics.num_windows == 1) {
  875. start2 = FFMAX(peakpos - 2, start2);
  876. end2 = FFMIN(peakpos + 3, end2);
  877. } else {
  878. start2 -= start;
  879. end2 -= start;
  880. }
  881. start += size;
  882. thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
  883. t = 1.0 - (1.0 * start2 / last);
  884. uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
  885. }
  886. }
  887. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  888. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  889. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  890. start = w*128;
  891. for (g = 0; g < sce->ics.num_swb; g++) {
  892. const float *coefs = sce->coeffs + start;
  893. const float *scaled = s->scoefs + start;
  894. const int size = sce->ics.swb_sizes[g];
  895. int scf, prev_scf, step;
  896. int min_scf = -1, max_scf = 256;
  897. float curdiff;
  898. if (maxq[w*16+g] < 21.544) {
  899. sce->zeroes[w*16+g] = 1;
  900. start += size;
  901. continue;
  902. }
  903. sce->zeroes[w*16+g] = 0;
  904. scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
  905. step = 16;
  906. for (;;) {
  907. float dist = 0.0f;
  908. int quant_max;
  909. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  910. int b;
  911. dist += quantize_band_cost(s, coefs + w2*128,
  912. scaled + w2*128,
  913. sce->ics.swb_sizes[g],
  914. scf,
  915. ESC_BT,
  916. lambda,
  917. INFINITY,
  918. &b);
  919. dist -= b;
  920. }
  921. dist *= 1.0f / 512.0f / lambda;
  922. quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
  923. if (quant_max >= 8191) { // too much, return to the previous quantizer
  924. sce->sf_idx[w*16+g] = prev_scf;
  925. break;
  926. }
  927. prev_scf = scf;
  928. curdiff = fabsf(dist - uplim[w*16+g]);
  929. if (curdiff <= 1.0f)
  930. step = 0;
  931. else
  932. step = log2f(curdiff);
  933. if (dist > uplim[w*16+g])
  934. step = -step;
  935. scf += step;
  936. scf = av_clip_uint8(scf);
  937. step = scf - prev_scf;
  938. if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
  939. sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
  940. break;
  941. }
  942. if (step > 0)
  943. min_scf = prev_scf;
  944. else
  945. max_scf = prev_scf;
  946. }
  947. start += size;
  948. }
  949. }
  950. minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
  951. for (i = 1; i < 128; i++) {
  952. if (!sce->sf_idx[i])
  953. sce->sf_idx[i] = sce->sf_idx[i-1];
  954. else
  955. minq = FFMIN(minq, sce->sf_idx[i]);
  956. }
  957. if (minq == INT_MAX)
  958. minq = 0;
  959. minq = FFMIN(minq, SCALE_MAX_POS);
  960. maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
  961. for (i = 126; i >= 0; i--) {
  962. if (!sce->sf_idx[i])
  963. sce->sf_idx[i] = sce->sf_idx[i+1];
  964. sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
  965. }
  966. }
  967. static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
  968. SingleChannelElement *sce,
  969. const float lambda)
  970. {
  971. int i, w, w2, g;
  972. int minq = 255;
  973. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  974. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  975. for (g = 0; g < sce->ics.num_swb; g++) {
  976. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  977. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  978. if (band->energy <= band->threshold) {
  979. sce->sf_idx[(w+w2)*16+g] = 218;
  980. sce->zeroes[(w+w2)*16+g] = 1;
  981. } else {
  982. sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
  983. sce->zeroes[(w+w2)*16+g] = 0;
  984. }
  985. minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
  986. }
  987. }
  988. }
  989. for (i = 0; i < 128; i++) {
  990. sce->sf_idx[i] = 140;
  991. //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
  992. }
  993. //set the same quantizers inside window groups
  994. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  995. for (g = 0; g < sce->ics.num_swb; g++)
  996. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  997. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  998. }
  999. static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
  1000. const float lambda)
  1001. {
  1002. int start = 0, i, w, w2, g;
  1003. float M[128], S[128];
  1004. float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
  1005. SingleChannelElement *sce0 = &cpe->ch[0];
  1006. SingleChannelElement *sce1 = &cpe->ch[1];
  1007. if (!cpe->common_window)
  1008. return;
  1009. for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
  1010. for (g = 0; g < sce0->ics.num_swb; g++) {
  1011. if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
  1012. float dist1 = 0.0f, dist2 = 0.0f;
  1013. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  1014. FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  1015. FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
  1016. float minthr = FFMIN(band0->threshold, band1->threshold);
  1017. float maxthr = FFMAX(band0->threshold, band1->threshold);
  1018. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  1019. M[i] = (sce0->coeffs[start+w2*128+i]
  1020. + sce1->coeffs[start+w2*128+i]) * 0.5;
  1021. S[i] = M[i]
  1022. - sce1->coeffs[start+w2*128+i];
  1023. }
  1024. abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
  1025. abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
  1026. abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  1027. abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  1028. dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
  1029. L34,
  1030. sce0->ics.swb_sizes[g],
  1031. sce0->sf_idx[(w+w2)*16+g],
  1032. sce0->band_type[(w+w2)*16+g],
  1033. lambda / band0->threshold, INFINITY, NULL);
  1034. dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
  1035. R34,
  1036. sce1->ics.swb_sizes[g],
  1037. sce1->sf_idx[(w+w2)*16+g],
  1038. sce1->band_type[(w+w2)*16+g],
  1039. lambda / band1->threshold, INFINITY, NULL);
  1040. dist2 += quantize_band_cost(s, M,
  1041. M34,
  1042. sce0->ics.swb_sizes[g],
  1043. sce0->sf_idx[(w+w2)*16+g],
  1044. sce0->band_type[(w+w2)*16+g],
  1045. lambda / maxthr, INFINITY, NULL);
  1046. dist2 += quantize_band_cost(s, S,
  1047. S34,
  1048. sce1->ics.swb_sizes[g],
  1049. sce1->sf_idx[(w+w2)*16+g],
  1050. sce1->band_type[(w+w2)*16+g],
  1051. lambda / minthr, INFINITY, NULL);
  1052. }
  1053. cpe->ms_mask[w*16+g] = dist2 < dist1;
  1054. }
  1055. start += sce0->ics.swb_sizes[g];
  1056. }
  1057. }
  1058. }
  1059. AACCoefficientsEncoder ff_aac_coders[] = {
  1060. {
  1061. search_for_quantizers_faac,
  1062. encode_window_bands_info,
  1063. quantize_and_encode_band,
  1064. search_for_ms,
  1065. },
  1066. {
  1067. search_for_quantizers_anmr,
  1068. encode_window_bands_info,
  1069. quantize_and_encode_band,
  1070. search_for_ms,
  1071. },
  1072. {
  1073. search_for_quantizers_twoloop,
  1074. codebook_trellis_rate,
  1075. quantize_and_encode_band,
  1076. search_for_ms,
  1077. },
  1078. {
  1079. search_for_quantizers_fast,
  1080. encode_window_bands_info,
  1081. quantize_and_encode_band,
  1082. search_for_ms,
  1083. },
  1084. };