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.

1020 lines
40KB

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