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.

1227 lines
48KB

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