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.

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