aaccoder.c 43 KB

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