aaccoder.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 libavcodec/aaccoder.c
  23. * AAC coefficients encoder
  24. */
  25. /***********************************
  26. * TODOs:
  27. * speedup quantizer selection
  28. * add sane pulse detection
  29. ***********************************/
  30. #include "avcodec.h"
  31. #include "put_bits.h"
  32. #include "aac.h"
  33. #include "aacenc.h"
  34. #include "aactab.h"
  35. /** bits needed to code codebook run value for long windows */
  36. static const uint8_t run_value_bits_long[64] = {
  37. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  38. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
  39. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  40. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
  41. };
  42. /** bits needed to code codebook run value for short windows */
  43. static const uint8_t run_value_bits_short[16] = {
  44. 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
  45. };
  46. static const uint8_t *run_value_bits[2] = {
  47. run_value_bits_long, run_value_bits_short
  48. };
  49. /**
  50. * Quantize one coefficient.
  51. * @return absolute value of the quantized coefficient
  52. * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
  53. */
  54. static av_always_inline int quant(float coef, const float Q)
  55. {
  56. float a = coef * Q;
  57. return sqrtf(a * sqrtf(a)) + 0.4054;
  58. }
  59. static void quantize_bands(int (*out)[2], const float *in, const float *scaled,
  60. int size, float Q34, int is_signed, int maxval)
  61. {
  62. int i;
  63. double qc;
  64. for (i = 0; i < size; i++) {
  65. qc = scaled[i] * Q34;
  66. out[i][0] = (int)FFMIN(qc, (double)maxval);
  67. out[i][1] = (int)FFMIN(qc + 0.4054, (double)maxval);
  68. if (is_signed && in[i] < 0.0f) {
  69. out[i][0] = -out[i][0];
  70. out[i][1] = -out[i][1];
  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 float quantize_and_encode_band_cost(struct AACEncContext *s,
  92. PutBitContext *pb, const float *in,
  93. const float *scaled, int size, int scale_idx,
  94. int cb, const float lambda, const float uplim,
  95. int *bits)
  96. {
  97. const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
  98. const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
  99. const float CLIPPED_ESCAPE = 165140.0f*IQ;
  100. int i, j, k;
  101. float cost = 0;
  102. const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
  103. int resbits = 0;
  104. #ifndef USE_REALLY_FULL_SEARCH
  105. const float Q34 = sqrtf(Q * sqrtf(Q));
  106. const int range = aac_cb_range[cb];
  107. const int maxval = aac_cb_maxval[cb];
  108. int offs[4];
  109. #endif /* USE_REALLY_FULL_SEARCH */
  110. if (!cb) {
  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. #ifndef USE_REALLY_FULL_SEARCH
  118. offs[0] = 1;
  119. for (i = 1; i < dim; i++)
  120. offs[i] = offs[i-1]*range;
  121. if (!scaled) {
  122. abs_pow34_v(s->scoefs, in, size);
  123. scaled = s->scoefs;
  124. }
  125. quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
  126. #endif /* USE_REALLY_FULL_SEARCH */
  127. for (i = 0; i < size; i += dim) {
  128. float mincost;
  129. int minidx = 0;
  130. int minbits = 0;
  131. const float *vec;
  132. #ifndef USE_REALLY_FULL_SEARCH
  133. int (*quants)[2] = &s->qcoefs[i];
  134. mincost = 0.0f;
  135. for (j = 0; j < dim; j++)
  136. mincost += in[i+j]*in[i+j];
  137. minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
  138. minbits = ff_aac_spectral_bits[cb-1][minidx];
  139. mincost = mincost * lambda + minbits;
  140. for (j = 0; j < (1<<dim); j++) {
  141. float rd = 0.0f;
  142. int curbits;
  143. int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
  144. int same = 0;
  145. for (k = 0; k < dim; k++) {
  146. if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
  147. same = 1;
  148. break;
  149. }
  150. }
  151. if (same)
  152. continue;
  153. for (k = 0; k < dim; k++)
  154. curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
  155. curbits = ff_aac_spectral_bits[cb-1][curidx];
  156. vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
  157. #else
  158. mincost = INFINITY;
  159. vec = ff_aac_codebook_vectors[cb-1];
  160. for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
  161. float rd = 0.0f;
  162. int curbits = ff_aac_spectral_bits[cb-1][j];
  163. int curidx = j;
  164. #endif /* USE_REALLY_FULL_SEARCH */
  165. if (IS_CODEBOOK_UNSIGNED(cb)) {
  166. for (k = 0; k < dim; k++) {
  167. float t = fabsf(in[i+k]);
  168. float di;
  169. if (vec[k] == 64.0f) { //FIXME: slow
  170. //do not code with escape sequence small values
  171. if (t < 39.0f*IQ) {
  172. rd = INFINITY;
  173. break;
  174. }
  175. if (t >= CLIPPED_ESCAPE) {
  176. di = t - CLIPPED_ESCAPE;
  177. curbits += 21;
  178. } else {
  179. int c = av_clip(quant(t, Q), 0, 8191);
  180. di = t - c*cbrtf(c)*IQ;
  181. curbits += av_log2(c)*2 - 4 + 1;
  182. }
  183. } else {
  184. di = t - vec[k]*IQ;
  185. }
  186. if (vec[k] != 0.0f)
  187. curbits++;
  188. rd += di*di;
  189. }
  190. } else {
  191. for (k = 0; k < dim; k++) {
  192. float di = in[i+k] - vec[k]*IQ;
  193. rd += di*di;
  194. }
  195. }
  196. rd = rd * lambda + curbits;
  197. if (rd < mincost) {
  198. mincost = rd;
  199. minidx = curidx;
  200. minbits = curbits;
  201. }
  202. }
  203. cost += mincost;
  204. resbits += minbits;
  205. if (cost >= uplim)
  206. return uplim;
  207. if (pb) {
  208. put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
  209. if (IS_CODEBOOK_UNSIGNED(cb))
  210. for (j = 0; j < dim; j++)
  211. if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
  212. put_bits(pb, 1, in[i+j] < 0.0f);
  213. if (cb == ESC_BT) {
  214. for (j = 0; j < 2; j++) {
  215. if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
  216. int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
  217. int len = av_log2(coef);
  218. put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
  219. put_bits(pb, len, coef & ((1 << len) - 1));
  220. }
  221. }
  222. }
  223. }
  224. }
  225. if (bits)
  226. *bits = resbits;
  227. return cost;
  228. }
  229. static float quantize_band_cost(struct AACEncContext *s, const float *in,
  230. const float *scaled, int size, int scale_idx,
  231. int cb, const float lambda, const float uplim,
  232. int *bits)
  233. {
  234. return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
  235. cb, lambda, uplim, bits);
  236. }
  237. static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
  238. const float *in, int size, int scale_idx,
  239. int cb, const float lambda)
  240. {
  241. quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
  242. INFINITY, NULL);
  243. }
  244. /**
  245. * structure used in optimal codebook search
  246. */
  247. typedef struct BandCodingPath {
  248. int prev_idx; ///< pointer to the previous path point
  249. float cost; ///< path cost
  250. int run;
  251. } BandCodingPath;
  252. /**
  253. * Encode band info for single window group bands.
  254. */
  255. static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
  256. int win, int group_len, const float lambda)
  257. {
  258. BandCodingPath path[120][12];
  259. int w, swb, cb, start, start2, size;
  260. int i, j;
  261. const int max_sfb = sce->ics.max_sfb;
  262. const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  263. const int run_esc = (1 << run_bits) - 1;
  264. int idx, ppos, count;
  265. int stackrun[120], stackcb[120], stack_len;
  266. float next_minrd = INFINITY;
  267. int next_mincb = 0;
  268. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  269. start = win*128;
  270. for (cb = 0; cb < 12; cb++) {
  271. path[0][cb].cost = 0.0f;
  272. path[0][cb].prev_idx = -1;
  273. path[0][cb].run = 0;
  274. }
  275. for (swb = 0; swb < max_sfb; swb++) {
  276. start2 = start;
  277. size = sce->ics.swb_sizes[swb];
  278. if (sce->zeroes[win*16 + swb]) {
  279. for (cb = 0; cb < 12; cb++) {
  280. path[swb+1][cb].prev_idx = cb;
  281. path[swb+1][cb].cost = path[swb][cb].cost;
  282. path[swb+1][cb].run = path[swb][cb].run + 1;
  283. }
  284. } else {
  285. float minrd = next_minrd;
  286. int mincb = next_mincb;
  287. next_minrd = INFINITY;
  288. next_mincb = 0;
  289. for (cb = 0; cb < 12; cb++) {
  290. float cost_stay_here, cost_get_here;
  291. float rd = 0.0f;
  292. for (w = 0; w < group_len; w++) {
  293. FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
  294. rd += quantize_band_cost(s, sce->coeffs + start + w*128,
  295. s->scoefs + start + w*128, size,
  296. sce->sf_idx[(win+w)*16+swb], cb,
  297. lambda / band->threshold, INFINITY, NULL);
  298. }
  299. cost_stay_here = path[swb][cb].cost + rd;
  300. cost_get_here = minrd + rd + run_bits + 4;
  301. if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  302. != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  303. cost_stay_here += run_bits;
  304. if (cost_get_here < cost_stay_here) {
  305. path[swb+1][cb].prev_idx = mincb;
  306. path[swb+1][cb].cost = cost_get_here;
  307. path[swb+1][cb].run = 1;
  308. } else {
  309. path[swb+1][cb].prev_idx = cb;
  310. path[swb+1][cb].cost = cost_stay_here;
  311. path[swb+1][cb].run = path[swb][cb].run + 1;
  312. }
  313. if (path[swb+1][cb].cost < next_minrd) {
  314. next_minrd = path[swb+1][cb].cost;
  315. next_mincb = cb;
  316. }
  317. }
  318. }
  319. start += sce->ics.swb_sizes[swb];
  320. }
  321. //convert resulting path from backward-linked list
  322. stack_len = 0;
  323. idx = 0;
  324. for (cb = 1; cb < 12; cb++)
  325. if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  326. idx = cb;
  327. ppos = max_sfb;
  328. while (ppos > 0) {
  329. cb = idx;
  330. stackrun[stack_len] = path[ppos][cb].run;
  331. stackcb [stack_len] = cb;
  332. idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  333. ppos -= path[ppos][cb].run;
  334. stack_len++;
  335. }
  336. //perform actual band info encoding
  337. start = 0;
  338. for (i = stack_len - 1; i >= 0; i--) {
  339. put_bits(&s->pb, 4, stackcb[i]);
  340. count = stackrun[i];
  341. memset(sce->zeroes + win*16 + start, !stackcb[i], count);
  342. //XXX: memset when band_type is also uint8_t
  343. for (j = 0; j < count; j++) {
  344. sce->band_type[win*16 + start] = stackcb[i];
  345. start++;
  346. }
  347. while (count >= run_esc) {
  348. put_bits(&s->pb, run_bits, run_esc);
  349. count -= run_esc;
  350. }
  351. put_bits(&s->pb, run_bits, count);
  352. }
  353. }
  354. typedef struct TrellisPath {
  355. float cost;
  356. int prev;
  357. int min_val;
  358. int max_val;
  359. } TrellisPath;
  360. #define TRELLIS_STAGES 121
  361. #define TRELLIS_STATES 256
  362. static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
  363. SingleChannelElement *sce,
  364. const float lambda)
  365. {
  366. int q, w, w2, g, start = 0;
  367. int i, j;
  368. int idx;
  369. TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
  370. int bandaddr[TRELLIS_STAGES];
  371. int minq;
  372. float mincost;
  373. for (i = 0; i < TRELLIS_STATES; i++) {
  374. paths[0][i].cost = 0.0f;
  375. paths[0][i].prev = -1;
  376. paths[0][i].min_val = i;
  377. paths[0][i].max_val = i;
  378. }
  379. for (j = 1; j < TRELLIS_STAGES; j++) {
  380. for (i = 0; i < TRELLIS_STATES; i++) {
  381. paths[j][i].cost = INFINITY;
  382. paths[j][i].prev = -2;
  383. paths[j][i].min_val = INT_MAX;
  384. paths[j][i].max_val = 0;
  385. }
  386. }
  387. idx = 1;
  388. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  389. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  390. start = w*128;
  391. for (g = 0; g < sce->ics.num_swb; g++) {
  392. const float *coefs = sce->coeffs + start;
  393. float qmin, qmax;
  394. int nz = 0;
  395. bandaddr[idx] = w * 16 + g;
  396. qmin = INT_MAX;
  397. qmax = 0.0f;
  398. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  399. FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
  400. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  401. sce->zeroes[(w+w2)*16+g] = 1;
  402. continue;
  403. }
  404. sce->zeroes[(w+w2)*16+g] = 0;
  405. nz = 1;
  406. for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  407. float t = fabsf(coefs[w2*128+i]);
  408. if (t > 0.0f)
  409. qmin = FFMIN(qmin, t);
  410. qmax = FFMAX(qmax, t);
  411. }
  412. }
  413. if (nz) {
  414. int minscale, maxscale;
  415. float minrd = INFINITY;
  416. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  417. minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
  418. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  419. maxscale = av_clip_uint8(log2(qmax)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
  420. for (q = minscale; q < maxscale; q++) {
  421. float dists[12], dist;
  422. memset(dists, 0, sizeof(dists));
  423. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  424. FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
  425. int cb;
  426. for (cb = 0; cb <= ESC_BT; cb++)
  427. dists[cb] += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
  428. q, cb, lambda / band->threshold, INFINITY, NULL);
  429. }
  430. dist = dists[0];
  431. for (i = 1; i <= ESC_BT; i++)
  432. dist = FFMIN(dist, dists[i]);
  433. minrd = FFMIN(minrd, dist);
  434. for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
  435. float cost;
  436. int minv, maxv;
  437. if (isinf(paths[idx - 1][i].cost))
  438. continue;
  439. cost = paths[idx - 1][i].cost + dist
  440. + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
  441. minv = FFMIN(paths[idx - 1][i].min_val, q);
  442. maxv = FFMAX(paths[idx - 1][i].max_val, q);
  443. if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
  444. paths[idx][q].cost = cost;
  445. paths[idx][q].prev = i;
  446. paths[idx][q].min_val = minv;
  447. paths[idx][q].max_val = maxv;
  448. }
  449. }
  450. }
  451. } else {
  452. for (q = 0; q < TRELLIS_STATES; q++) {
  453. if (!isinf(paths[idx - 1][q].cost)) {
  454. paths[idx][q].cost = paths[idx - 1][q].cost + 1;
  455. paths[idx][q].prev = q;
  456. paths[idx][q].min_val = FFMIN(paths[idx - 1][q].min_val, q);
  457. paths[idx][q].max_val = FFMAX(paths[idx - 1][q].max_val, q);
  458. continue;
  459. }
  460. for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
  461. float cost;
  462. int minv, maxv;
  463. if (isinf(paths[idx - 1][i].cost))
  464. continue;
  465. cost = paths[idx - 1][i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
  466. minv = FFMIN(paths[idx - 1][i].min_val, q);
  467. maxv = FFMAX(paths[idx - 1][i].max_val, q);
  468. if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
  469. paths[idx][q].cost = cost;
  470. paths[idx][q].prev = i;
  471. paths[idx][q].min_val = minv;
  472. paths[idx][q].max_val = maxv;
  473. }
  474. }
  475. }
  476. }
  477. sce->zeroes[w*16+g] = !nz;
  478. start += sce->ics.swb_sizes[g];
  479. idx++;
  480. }
  481. }
  482. idx--;
  483. mincost = paths[idx][0].cost;
  484. minq = 0;
  485. for (i = 1; i < TRELLIS_STATES; i++) {
  486. if (paths[idx][i].cost < mincost) {
  487. mincost = paths[idx][i].cost;
  488. minq = i;
  489. }
  490. }
  491. while (idx) {
  492. sce->sf_idx[bandaddr[idx]] = minq;
  493. minq = paths[idx][minq].prev;
  494. idx--;
  495. }
  496. //set the same quantizers inside window groups
  497. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  498. for (g = 0; g < sce->ics.num_swb; g++)
  499. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  500. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  501. }
  502. /**
  503. * two-loop quantizers search taken from ISO 13818-7 Appendix C
  504. */
  505. static void search_for_quantizers_twoloop(AVCodecContext *avctx,
  506. AACEncContext *s,
  507. SingleChannelElement *sce,
  508. const float lambda)
  509. {
  510. int start = 0, i, w, w2, g;
  511. int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
  512. float dists[128], uplims[128];
  513. int fflag, minscaler;
  514. int its = 0;
  515. int allz = 0;
  516. float minthr = INFINITY;
  517. //XXX: some heuristic to determine initial quantizers will reduce search time
  518. memset(dists, 0, sizeof(dists));
  519. //determine zero bands and upper limits
  520. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  521. for (g = 0; g < sce->ics.num_swb; g++) {
  522. int nz = 0;
  523. float uplim = 0.0f;
  524. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  525. FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
  526. uplim += band->threshold;
  527. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  528. sce->zeroes[(w+w2)*16+g] = 1;
  529. continue;
  530. }
  531. nz = 1;
  532. }
  533. uplims[w*16+g] = uplim *512;
  534. sce->zeroes[w*16+g] = !nz;
  535. if (nz)
  536. minthr = FFMIN(minthr, uplim);
  537. allz = FFMAX(allz, nz);
  538. }
  539. }
  540. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  541. for (g = 0; g < sce->ics.num_swb; g++) {
  542. if (sce->zeroes[w*16+g]) {
  543. sce->sf_idx[w*16+g] = SCALE_ONE_POS;
  544. continue;
  545. }
  546. sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
  547. }
  548. }
  549. if (!allz)
  550. return;
  551. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  552. //perform two-loop search
  553. //outer loop - improve quality
  554. do {
  555. int tbits, qstep;
  556. minscaler = sce->sf_idx[0];
  557. //inner loop - quantize spectrum to fit into given number of bits
  558. qstep = its ? 1 : 32;
  559. do {
  560. int prev = -1;
  561. tbits = 0;
  562. fflag = 0;
  563. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  564. start = w*128;
  565. for (g = 0; g < sce->ics.num_swb; g++) {
  566. const float *coefs = sce->coeffs + start;
  567. const float *scaled = s->scoefs + start;
  568. int bits = 0;
  569. int cb;
  570. float mindist = INFINITY;
  571. int minbits = 0;
  572. if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
  573. start += sce->ics.swb_sizes[g];
  574. continue;
  575. }
  576. minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
  577. for (cb = 0; cb <= ESC_BT; cb++) {
  578. float dist = 0.0f;
  579. int bb = 0;
  580. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  581. int b;
  582. dist += quantize_band_cost(s, coefs + w2*128,
  583. scaled + w2*128,
  584. sce->ics.swb_sizes[g],
  585. sce->sf_idx[w*16+g],
  586. cb,
  587. lambda,
  588. INFINITY,
  589. &b);
  590. bb += b;
  591. }
  592. if (dist < mindist) {
  593. mindist = dist;
  594. minbits = bb;
  595. }
  596. }
  597. dists[w*16+g] = (mindist - minbits) / lambda;
  598. bits = minbits;
  599. if (prev != -1) {
  600. bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
  601. }
  602. tbits += bits;
  603. start += sce->ics.swb_sizes[g];
  604. prev = sce->sf_idx[w*16+g];
  605. }
  606. }
  607. if (tbits > destbits) {
  608. for (i = 0; i < 128; i++)
  609. if (sce->sf_idx[i] < 218 - qstep)
  610. sce->sf_idx[i] += qstep;
  611. } else {
  612. for (i = 0; i < 128; i++)
  613. if (sce->sf_idx[i] > 60 - qstep)
  614. sce->sf_idx[i] -= qstep;
  615. }
  616. qstep >>= 1;
  617. if (!qstep && tbits > destbits*1.02)
  618. qstep = 1;
  619. if (sce->sf_idx[0] >= 217)
  620. break;
  621. } while (qstep);
  622. fflag = 0;
  623. minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
  624. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  625. start = w*128;
  626. for (g = 0; g < sce->ics.num_swb; g++) {
  627. int prevsc = sce->sf_idx[w*16+g];
  628. if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
  629. sce->sf_idx[w*16+g]--;
  630. sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
  631. sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
  632. if (sce->sf_idx[w*16+g] != prevsc)
  633. fflag = 1;
  634. }
  635. }
  636. its++;
  637. } while (fflag && its < 10);
  638. }
  639. static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
  640. SingleChannelElement *sce,
  641. const float lambda)
  642. {
  643. int start = 0, i, w, w2, g;
  644. float uplim[128], maxq[128];
  645. int minq, maxsf;
  646. float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
  647. int last = 0, lastband = 0, curband = 0;
  648. float avg_energy = 0.0;
  649. if (sce->ics.num_windows == 1) {
  650. start = 0;
  651. for (i = 0; i < 1024; i++) {
  652. if (i - start >= sce->ics.swb_sizes[curband]) {
  653. start += sce->ics.swb_sizes[curband];
  654. curband++;
  655. }
  656. if (sce->coeffs[i]) {
  657. avg_energy += sce->coeffs[i] * sce->coeffs[i];
  658. last = i;
  659. lastband = curband;
  660. }
  661. }
  662. } else {
  663. for (w = 0; w < 8; w++) {
  664. const float *coeffs = sce->coeffs + w*128;
  665. start = 0;
  666. for (i = 0; i < 128; i++) {
  667. if (i - start >= sce->ics.swb_sizes[curband]) {
  668. start += sce->ics.swb_sizes[curband];
  669. curband++;
  670. }
  671. if (coeffs[i]) {
  672. avg_energy += coeffs[i] * coeffs[i];
  673. last = FFMAX(last, i);
  674. lastband = FFMAX(lastband, curband);
  675. }
  676. }
  677. }
  678. }
  679. last++;
  680. avg_energy /= last;
  681. if (avg_energy == 0.0f) {
  682. for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
  683. sce->sf_idx[i] = SCALE_ONE_POS;
  684. return;
  685. }
  686. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  687. start = w*128;
  688. for (g = 0; g < sce->ics.num_swb; g++) {
  689. float *coefs = sce->coeffs + start;
  690. const int size = sce->ics.swb_sizes[g];
  691. int start2 = start, end2 = start + size, peakpos = start;
  692. float maxval = -1, thr = 0.0f, t;
  693. maxq[w*16+g] = 0.0f;
  694. if (g > lastband) {
  695. maxq[w*16+g] = 0.0f;
  696. start += size;
  697. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  698. memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
  699. continue;
  700. }
  701. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  702. for (i = 0; i < size; i++) {
  703. float t = coefs[w2*128+i]*coefs[w2*128+i];
  704. maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
  705. thr += t;
  706. if (sce->ics.num_windows == 1 && maxval < t) {
  707. maxval = t;
  708. peakpos = start+i;
  709. }
  710. }
  711. }
  712. if (sce->ics.num_windows == 1) {
  713. start2 = FFMAX(peakpos - 2, start2);
  714. end2 = FFMIN(peakpos + 3, end2);
  715. } else {
  716. start2 -= start;
  717. end2 -= start;
  718. }
  719. start += size;
  720. thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
  721. t = 1.0 - (1.0 * start2 / last);
  722. uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
  723. }
  724. }
  725. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  726. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  727. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  728. start = w*128;
  729. for (g = 0; g < sce->ics.num_swb; g++) {
  730. const float *coefs = sce->coeffs + start;
  731. const float *scaled = s->scoefs + start;
  732. const int size = sce->ics.swb_sizes[g];
  733. int scf, prev_scf, step;
  734. int min_scf = 0, max_scf = 255;
  735. float curdiff;
  736. if (maxq[w*16+g] < 21.544) {
  737. sce->zeroes[w*16+g] = 1;
  738. start += size;
  739. continue;
  740. }
  741. sce->zeroes[w*16+g] = 0;
  742. scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
  743. step = 16;
  744. for (;;) {
  745. float dist = 0.0f;
  746. int quant_max;
  747. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  748. int b;
  749. dist += quantize_band_cost(s, coefs + w2*128,
  750. scaled + w2*128,
  751. sce->ics.swb_sizes[g],
  752. scf,
  753. ESC_BT,
  754. lambda,
  755. INFINITY,
  756. &b);
  757. dist -= b;
  758. }
  759. dist *= 1.0f / 512.0f / lambda;
  760. quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
  761. if (quant_max >= 8191) { // too much, return to the previous quantizer
  762. sce->sf_idx[w*16+g] = prev_scf;
  763. break;
  764. }
  765. prev_scf = scf;
  766. curdiff = fabsf(dist - uplim[w*16+g]);
  767. if (curdiff == 0.0f)
  768. step = 0;
  769. else
  770. step = fabsf(log2(curdiff));
  771. if (dist > uplim[w*16+g])
  772. step = -step;
  773. if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
  774. sce->sf_idx[w*16+g] = scf;
  775. break;
  776. }
  777. scf += step;
  778. if (step > 0)
  779. min_scf = scf;
  780. else
  781. max_scf = scf;
  782. }
  783. start += size;
  784. }
  785. }
  786. minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
  787. for (i = 1; i < 128; i++) {
  788. if (!sce->sf_idx[i])
  789. sce->sf_idx[i] = sce->sf_idx[i-1];
  790. else
  791. minq = FFMIN(minq, sce->sf_idx[i]);
  792. }
  793. if (minq == INT_MAX)
  794. minq = 0;
  795. minq = FFMIN(minq, SCALE_MAX_POS);
  796. maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
  797. for (i = 126; i >= 0; i--) {
  798. if (!sce->sf_idx[i])
  799. sce->sf_idx[i] = sce->sf_idx[i+1];
  800. sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
  801. }
  802. }
  803. static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
  804. SingleChannelElement *sce,
  805. const float lambda)
  806. {
  807. int start = 0, i, w, w2, g;
  808. int minq = 255;
  809. memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  810. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  811. start = w*128;
  812. for (g = 0; g < sce->ics.num_swb; g++) {
  813. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  814. FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
  815. if (band->energy <= band->threshold) {
  816. sce->sf_idx[(w+w2)*16+g] = 218;
  817. sce->zeroes[(w+w2)*16+g] = 1;
  818. } else {
  819. sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
  820. sce->zeroes[(w+w2)*16+g] = 0;
  821. }
  822. minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
  823. }
  824. }
  825. }
  826. for (i = 0; i < 128; i++) {
  827. sce->sf_idx[i] = 140;
  828. //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
  829. }
  830. //set the same quantizers inside window groups
  831. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  832. for (g = 0; g < sce->ics.num_swb; g++)
  833. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  834. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  835. }
  836. static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
  837. const float lambda)
  838. {
  839. int start = 0, i, w, w2, g;
  840. float M[128], S[128];
  841. float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
  842. SingleChannelElement *sce0 = &cpe->ch[0];
  843. SingleChannelElement *sce1 = &cpe->ch[1];
  844. if (!cpe->common_window)
  845. return;
  846. for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
  847. for (g = 0; g < sce0->ics.num_swb; g++) {
  848. if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
  849. float dist1 = 0.0f, dist2 = 0.0f;
  850. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  851. FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
  852. FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
  853. float minthr = FFMIN(band0->threshold, band1->threshold);
  854. float maxthr = FFMAX(band0->threshold, band1->threshold);
  855. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  856. M[i] = (sce0->coeffs[start+w2*128+i]
  857. + sce1->coeffs[start+w2*128+i]) * 0.5;
  858. S[i] = sce0->coeffs[start+w2*128+i]
  859. - sce1->coeffs[start+w2*128+i];
  860. }
  861. abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
  862. abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
  863. abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  864. abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  865. dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
  866. L34,
  867. sce0->ics.swb_sizes[g],
  868. sce0->sf_idx[(w+w2)*16+g],
  869. sce0->band_type[(w+w2)*16+g],
  870. lambda / band0->threshold, INFINITY, NULL);
  871. dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
  872. R34,
  873. sce1->ics.swb_sizes[g],
  874. sce1->sf_idx[(w+w2)*16+g],
  875. sce1->band_type[(w+w2)*16+g],
  876. lambda / band1->threshold, INFINITY, NULL);
  877. dist2 += quantize_band_cost(s, M,
  878. M34,
  879. sce0->ics.swb_sizes[g],
  880. sce0->sf_idx[(w+w2)*16+g],
  881. sce0->band_type[(w+w2)*16+g],
  882. lambda / maxthr, INFINITY, NULL);
  883. dist2 += quantize_band_cost(s, S,
  884. S34,
  885. sce1->ics.swb_sizes[g],
  886. sce1->sf_idx[(w+w2)*16+g],
  887. sce1->band_type[(w+w2)*16+g],
  888. lambda / minthr, INFINITY, NULL);
  889. }
  890. cpe->ms_mask[w*16+g] = dist2 < dist1;
  891. }
  892. start += sce0->ics.swb_sizes[g];
  893. }
  894. }
  895. }
  896. AACCoefficientsEncoder ff_aac_coders[] = {
  897. {
  898. search_for_quantizers_faac,
  899. encode_window_bands_info,
  900. quantize_and_encode_band,
  901. search_for_ms,
  902. },
  903. {
  904. search_for_quantizers_anmr,
  905. encode_window_bands_info,
  906. quantize_and_encode_band,
  907. search_for_ms,
  908. },
  909. {
  910. search_for_quantizers_twoloop,
  911. encode_window_bands_info,
  912. quantize_and_encode_band,
  913. search_for_ms,
  914. },
  915. {
  916. search_for_quantizers_fast,
  917. encode_window_bands_info,
  918. quantize_and_encode_band,
  919. search_for_ms,
  920. },
  921. };