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.

994 lines
39KB

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