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.

1383 lines
56KB

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