patch-libavcodec_aaccoder_c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. $OpenBSD: patch-libavcodec_aaccoder_c,v 1.12 2016/10/17 07:51:00 ajacoutot Exp $
  2. aaccoder: use roundf() instead of ceilf()
  3. aaccoder: rewrite PNS implementation
  4. aaccoder: add frequency scaling and quantization correction for PNS
  5. aaccoder: tweak PNS implementation further
  6. aaccoder: respect cutoff when marking bands as PNS
  7. AAC encoder: refactor to resynchronize MIPS port
  8. AAC encoder: tweak PNS usage to be more aggressive
  9. AAC encoder: fix OOB access in search_for_pns
  10. AAC encoder: Extensive improvements
  11. aacenc: shorten name of ff_aac_adjust_common_prediction
  12. aacenc: add support for encoding files using Long Term Prediction
  13. AAC encoder: Fix application of M/S with PNS
  14. avcodec/aac_tablegen: get rid of hardcoded tables entirely
  15. AAC encoder: improve SF range utilization
  16. AAC encoder: ANMR, avoid empty search ranges
  17. AAC encoder: fix OOB access in search_for_pns
  18. aacenc: switch to using the RNG from libavutil
  19. AAC encoder: fix possible assertion failure in PNS
  20. AAC encoder: avoid assertion failure on PNS
  21. AAC encoder: various fixes in M/S coding
  22. AAC encoder: enforce SF delta in PNS and IS SFs
  23. aacenc: remove FAAC-like coder
  24. aacenc: add a faster version of twoloop as the "fast" coder
  25. aacenc: fix various typos and an error message
  26. aacenc: use the decoder's lcg PRNG
  27. --- libavcodec/aaccoder.c.orig Sat Aug 27 22:51:19 2016
  28. +++ libavcodec/aaccoder.c Sat Oct 15 18:48:53 2016
  29. @@ -33,7 +33,9 @@
  30. #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
  31. #include <float.h>
  32. +
  33. #include "libavutil/mathematics.h"
  34. +#include "mathops.h"
  35. #include "avcodec.h"
  36. #include "put_bits.h"
  37. #include "aac.h"
  38. @@ -42,24 +44,24 @@
  39. #include "aacenctab.h"
  40. #include "aacenc_utils.h"
  41. #include "aacenc_quantization.h"
  42. -#include "aac_tablegen_decl.h"
  43. #include "aacenc_is.h"
  44. #include "aacenc_tns.h"
  45. +#include "aacenc_ltp.h"
  46. #include "aacenc_pred.h"
  47. -/** Frequency in Hz for lower limit of noise substitution **/
  48. -#define NOISE_LOW_LIMIT 4500
  49. +#include "libavcodec/aaccoder_twoloop.h"
  50. -/* Energy spread threshold value below which no PNS is used, this corresponds to
  51. - * typically around 17Khz, after which PNS usage decays ending at 19Khz */
  52. -#define NOISE_SPREAD_THRESHOLD 0.5f
  53. +/* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread
  54. + * beyond which no PNS is used (since the SFBs contain tone rather than noise) */
  55. +#define NOISE_SPREAD_THRESHOLD 0.9f
  56. -/* This constant gets divided by lambda to return ~1.65 which when multiplied
  57. - * by the band->threshold and compared to band->energy is the boundary between
  58. - * excessive PNS and little PNS usage. */
  59. -#define NOISE_LAMBDA_NUMERATOR 252.1f
  60. +/* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to
  61. + * replace low energy non zero bands */
  62. +#define NOISE_LAMBDA_REPLACE 1.948f
  63. +#include "libavcodec/aaccoder_trellis.h"
  64. +
  65. /**
  66. * structure used in optimal codebook search
  67. */
  68. @@ -121,7 +123,7 @@ static void encode_window_bands_info(AACEncContext *s,
  69. rd += quantize_band_cost(s, &sce->coeffs[start + w*128],
  70. &s->scoefs[start + w*128], size,
  71. sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
  72. - lambda / band->threshold, INFINITY, NULL, 0);
  73. + lambda / band->threshold, INFINITY, NULL, NULL, 0);
  74. }
  75. cost_stay_here = path[swb][cb].cost + rd;
  76. cost_get_here = minrd + rd + run_bits + 4;
  77. @@ -182,138 +184,7 @@ static void encode_window_bands_info(AACEncContext *s,
  78. }
  79. }
  80. -static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
  81. - int win, int group_len, const float lambda)
  82. -{
  83. - BandCodingPath path[120][CB_TOT_ALL];
  84. - int w, swb, cb, start, size;
  85. - int i, j;
  86. - const int max_sfb = sce->ics.max_sfb;
  87. - const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
  88. - const int run_esc = (1 << run_bits) - 1;
  89. - int idx, ppos, count;
  90. - int stackrun[120], stackcb[120], stack_len;
  91. - float next_minbits = INFINITY;
  92. - int next_mincb = 0;
  93. - abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  94. - start = win*128;
  95. - for (cb = 0; cb < CB_TOT_ALL; cb++) {
  96. - path[0][cb].cost = run_bits+4;
  97. - path[0][cb].prev_idx = -1;
  98. - path[0][cb].run = 0;
  99. - }
  100. - for (swb = 0; swb < max_sfb; swb++) {
  101. - size = sce->ics.swb_sizes[swb];
  102. - if (sce->zeroes[win*16 + swb]) {
  103. - float cost_stay_here = path[swb][0].cost;
  104. - float cost_get_here = next_minbits + run_bits + 4;
  105. - if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
  106. - != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
  107. - cost_stay_here += run_bits;
  108. - if (cost_get_here < cost_stay_here) {
  109. - path[swb+1][0].prev_idx = next_mincb;
  110. - path[swb+1][0].cost = cost_get_here;
  111. - path[swb+1][0].run = 1;
  112. - } else {
  113. - path[swb+1][0].prev_idx = 0;
  114. - path[swb+1][0].cost = cost_stay_here;
  115. - path[swb+1][0].run = path[swb][0].run + 1;
  116. - }
  117. - next_minbits = path[swb+1][0].cost;
  118. - next_mincb = 0;
  119. - for (cb = 1; cb < CB_TOT_ALL; cb++) {
  120. - path[swb+1][cb].cost = 61450;
  121. - path[swb+1][cb].prev_idx = -1;
  122. - path[swb+1][cb].run = 0;
  123. - }
  124. - } else {
  125. - float minbits = next_minbits;
  126. - int mincb = next_mincb;
  127. - int startcb = sce->band_type[win*16+swb];
  128. - startcb = aac_cb_in_map[startcb];
  129. - next_minbits = INFINITY;
  130. - next_mincb = 0;
  131. - for (cb = 0; cb < startcb; cb++) {
  132. - path[swb+1][cb].cost = 61450;
  133. - path[swb+1][cb].prev_idx = -1;
  134. - path[swb+1][cb].run = 0;
  135. - }
  136. - for (cb = startcb; cb < CB_TOT_ALL; cb++) {
  137. - float cost_stay_here, cost_get_here;
  138. - float bits = 0.0f;
  139. - if (cb >= 12 && sce->band_type[win*16+swb] != aac_cb_out_map[cb]) {
  140. - path[swb+1][cb].cost = 61450;
  141. - path[swb+1][cb].prev_idx = -1;
  142. - path[swb+1][cb].run = 0;
  143. - continue;
  144. - }
  145. - for (w = 0; w < group_len; w++) {
  146. - bits += quantize_band_cost(s, &sce->coeffs[start + w*128],
  147. - &s->scoefs[start + w*128], size,
  148. - sce->sf_idx[win*16+swb],
  149. - aac_cb_out_map[cb],
  150. - 0, INFINITY, NULL, 0);
  151. - }
  152. - cost_stay_here = path[swb][cb].cost + bits;
  153. - cost_get_here = minbits + bits + run_bits + 4;
  154. - if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
  155. - != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
  156. - cost_stay_here += run_bits;
  157. - if (cost_get_here < cost_stay_here) {
  158. - path[swb+1][cb].prev_idx = mincb;
  159. - path[swb+1][cb].cost = cost_get_here;
  160. - path[swb+1][cb].run = 1;
  161. - } else {
  162. - path[swb+1][cb].prev_idx = cb;
  163. - path[swb+1][cb].cost = cost_stay_here;
  164. - path[swb+1][cb].run = path[swb][cb].run + 1;
  165. - }
  166. - if (path[swb+1][cb].cost < next_minbits) {
  167. - next_minbits = path[swb+1][cb].cost;
  168. - next_mincb = cb;
  169. - }
  170. - }
  171. - }
  172. - start += sce->ics.swb_sizes[swb];
  173. - }
  174. -
  175. - //convert resulting path from backward-linked list
  176. - stack_len = 0;
  177. - idx = 0;
  178. - for (cb = 1; cb < CB_TOT_ALL; cb++)
  179. - if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
  180. - idx = cb;
  181. - ppos = max_sfb;
  182. - while (ppos > 0) {
  183. - av_assert1(idx >= 0);
  184. - cb = idx;
  185. - stackrun[stack_len] = path[ppos][cb].run;
  186. - stackcb [stack_len] = cb;
  187. - idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
  188. - ppos -= path[ppos][cb].run;
  189. - stack_len++;
  190. - }
  191. - //perform actual band info encoding
  192. - start = 0;
  193. - for (i = stack_len - 1; i >= 0; i--) {
  194. - cb = aac_cb_out_map[stackcb[i]];
  195. - put_bits(&s->pb, 4, cb);
  196. - count = stackrun[i];
  197. - memset(sce->zeroes + win*16 + start, !cb, count);
  198. - //XXX: memset when band_type is also uint8_t
  199. - for (j = 0; j < count; j++) {
  200. - sce->band_type[win*16 + start] = cb;
  201. - start++;
  202. - }
  203. - while (count >= run_esc) {
  204. - put_bits(&s->pb, run_bits, run_esc);
  205. - count -= run_esc;
  206. - }
  207. - put_bits(&s->pb, run_bits, count);
  208. - }
  209. -}
  210. -
  211. typedef struct TrellisPath {
  212. float cost;
  213. int prev;
  214. @@ -324,23 +195,23 @@ typedef struct TrellisPath {
  215. static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
  216. {
  217. - int w, g, start = 0;
  218. - int minscaler_n = sce->sf_idx[0], minscaler_i = sce->sf_idx[0];
  219. + int w, g;
  220. + int prevscaler_n = -255, prevscaler_i = 0;
  221. int bands = 0;
  222. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  223. - start = 0;
  224. - for (g = 0; g < sce->ics.num_swb; g++) {
  225. + for (g = 0; g < sce->ics.num_swb; g++) {
  226. + if (sce->zeroes[w*16+g])
  227. + continue;
  228. if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
  229. - sce->sf_idx[w*16+g] = av_clip(ceilf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
  230. - minscaler_i = FFMIN(minscaler_i, sce->sf_idx[w*16+g]);
  231. + sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
  232. bands++;
  233. } else if (sce->band_type[w*16+g] == NOISE_BT) {
  234. - sce->sf_idx[w*16+g] = av_clip(4+log2f(sce->pns_ener[w*16+g])*2, -100, 155);
  235. - minscaler_n = FFMIN(minscaler_n, sce->sf_idx[w*16+g]);
  236. + sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
  237. + if (prevscaler_n == -255)
  238. + prevscaler_n = sce->sf_idx[w*16+g];
  239. bands++;
  240. }
  241. - start += sce->ics.swb_sizes[g];
  242. }
  243. }
  244. @@ -349,11 +220,13 @@ static void set_special_band_scalefactors(AACEncContex
  245. /* Clip the scalefactor indices */
  246. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  247. - for (g = 0; g < sce->ics.num_swb; g++) {
  248. + for (g = 0; g < sce->ics.num_swb; g++) {
  249. + if (sce->zeroes[w*16+g])
  250. + continue;
  251. if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
  252. - sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_i, minscaler_i + SCALE_MAX_DIFF);
  253. + sce->sf_idx[w*16+g] = prevscaler_i = av_clip(sce->sf_idx[w*16+g], prevscaler_i - SCALE_MAX_DIFF, prevscaler_i + SCALE_MAX_DIFF);
  254. } else if (sce->band_type[w*16+g] == NOISE_BT) {
  255. - sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
  256. + sce->sf_idx[w*16+g] = prevscaler_n = av_clip(sce->sf_idx[w*16+g], prevscaler_n - SCALE_MAX_DIFF, prevscaler_n + SCALE_MAX_DIFF);
  257. }
  258. }
  259. }
  260. @@ -390,9 +263,9 @@ static void search_for_quantizers_anmr(AVCodecContext
  261. }
  262. //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
  263. - q0 = coef2minsf(q0f);
  264. + q0 = av_clip(coef2minsf(q0f), 0, SCALE_MAX_POS-1);
  265. //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
  266. - q1 = coef2maxsf(q1f);
  267. + q1 = av_clip(coef2maxsf(q1f), 1, SCALE_MAX_POS);
  268. if (q1 - q0 > 60) {
  269. int q0low = q0;
  270. int q1high = q1;
  271. @@ -408,6 +281,12 @@ static void search_for_quantizers_anmr(AVCodecContext
  272. q1 = q1high;
  273. }
  274. }
  275. + // q0 == q1 isn't really a legal situation
  276. + if (q0 == q1) {
  277. + // the following is indirect but guarantees q1 != q0 && q1 near q0
  278. + q1 = av_clip(q0+1, 1, SCALE_MAX_POS);
  279. + q0 = av_clip(q1-1, 0, SCALE_MAX_POS - 1);
  280. + }
  281. for (i = 0; i < TRELLIS_STATES; i++) {
  282. paths[0][i].cost = 0.0f;
  283. @@ -456,6 +335,10 @@ static void search_for_quantizers_anmr(AVCodecContext
  284. maxscale = coef2maxsf(qmax);
  285. minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
  286. maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
  287. + if (minscale == maxscale) {
  288. + maxscale = av_clip(minscale+1, 1, TRELLIS_STATES);
  289. + minscale = av_clip(maxscale-1, 0, TRELLIS_STATES - 1);
  290. + }
  291. maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
  292. for (q = minscale; q < maxscale; q++) {
  293. float dist = 0;
  294. @@ -463,7 +346,7 @@ static void search_for_quantizers_anmr(AVCodecContext
  295. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  296. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  297. dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
  298. - q + q0, cb, lambda / band->threshold, INFINITY, NULL, 0);
  299. + q + q0, cb, lambda / band->threshold, INFINITY, NULL, NULL, 0);
  300. }
  301. minrd = FFMIN(minrd, dist);
  302. @@ -504,18 +387,14 @@ static void search_for_quantizers_anmr(AVCodecContext
  303. }
  304. //set the same quantizers inside window groups
  305. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  306. - for (g = 0; g < sce->ics.num_swb; g++)
  307. + for (g = 0; g < sce->ics.num_swb; g++)
  308. for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  309. sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  310. }
  311. -/**
  312. - * two-loop quantizers search taken from ISO 13818-7 Appendix C
  313. - */
  314. -static void search_for_quantizers_twoloop(AVCodecContext *avctx,
  315. - AACEncContext *s,
  316. - SingleChannelElement *sce,
  317. - const float lambda)
  318. +static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
  319. + SingleChannelElement *sce,
  320. + const float lambda)
  321. {
  322. int start = 0, i, w, w2, g;
  323. int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
  324. @@ -529,15 +408,16 @@ static void search_for_quantizers_twoloop(AVCodecConte
  325. // for values above this the decoder might end up in an endless loop
  326. // due to always having more bits than what can be encoded.
  327. destbits = FFMIN(destbits, 5800);
  328. - //XXX: some heuristic to determine initial quantizers will reduce search time
  329. + //some heuristic to determine initial quantizers will reduce search time
  330. //determine zero bands and upper limits
  331. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  332. - for (g = 0; g < sce->ics.num_swb; g++) {
  333. + start = 0;
  334. + for (g = 0; g < sce->ics.num_swb; g++) {
  335. int nz = 0;
  336. float uplim = 0.0f, energy = 0.0f;
  337. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  338. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  339. - uplim += band->threshold;
  340. + uplim += band->threshold;
  341. energy += band->energy;
  342. if (band->energy <= band->threshold || band->threshold == 0.0f) {
  343. sce->zeroes[(w+w2)*16+g] = 1;
  344. @@ -546,14 +426,16 @@ static void search_for_quantizers_twoloop(AVCodecConte
  345. nz = 1;
  346. }
  347. uplims[w*16+g] = uplim *512;
  348. + sce->band_type[w*16+g] = 0;
  349. sce->zeroes[w*16+g] = !nz;
  350. if (nz)
  351. minthr = FFMIN(minthr, uplim);
  352. allz |= nz;
  353. + start += sce->ics.swb_sizes[g];
  354. }
  355. }
  356. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  357. - for (g = 0; g < sce->ics.num_swb; g++) {
  358. + for (g = 0; g < sce->ics.num_swb; g++) {
  359. if (sce->zeroes[w*16+g]) {
  360. sce->sf_idx[w*16+g] = SCALE_ONE_POS;
  361. continue;
  362. @@ -565,10 +447,11 @@ static void search_for_quantizers_twoloop(AVCodecConte
  363. if (!allz)
  364. return;
  365. abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  366. + ff_quantize_band_cost_cache_init(s);
  367. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  368. start = w*128;
  369. - for (g = 0; g < sce->ics.num_swb; g++) {
  370. + for (g = 0; g < sce->ics.num_swb; g++) {
  371. const float *scaled = s->scoefs + start;
  372. maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
  373. start += sce->ics.swb_sizes[g];
  374. @@ -587,9 +470,9 @@ static void search_for_quantizers_twoloop(AVCodecConte
  375. tbits = 0;
  376. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  377. start = w*128;
  378. - for (g = 0; g < sce->ics.num_swb; g++) {
  379. - const float *coefs = &sce->coeffs[start];
  380. - const float *scaled = &s->scoefs[start];
  381. + for (g = 0; g < sce->ics.num_swb; g++) {
  382. + const float *coefs = sce->coeffs + start;
  383. + const float *scaled = s->scoefs + start;
  384. int bits = 0;
  385. int cb;
  386. float dist = 0.0f;
  387. @@ -602,15 +485,13 @@ static void search_for_quantizers_twoloop(AVCodecConte
  388. cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
  389. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  390. int b;
  391. - dist += quantize_band_cost(s, coefs + w2*128,
  392. - scaled + w2*128,
  393. - sce->ics.swb_sizes[g],
  394. - sce->sf_idx[w*16+g],
  395. - cb,
  396. - 1.0f,
  397. - INFINITY,
  398. - &b,
  399. - 0);
  400. + dist += quantize_band_cost_cached(s, w + w2, g,
  401. + coefs + w2*128,
  402. + scaled + w2*128,
  403. + sce->ics.swb_sizes[g],
  404. + sce->sf_idx[w*16+g],
  405. + cb, 1.0f, INFINITY,
  406. + &b, NULL, 0);
  407. bits += b;
  408. }
  409. dists[w*16+g] = dist - bits;
  410. @@ -659,331 +540,378 @@ static void search_for_quantizers_twoloop(AVCodecConte
  411. } while (fflag && its < 10);
  412. }
  413. -static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
  414. - SingleChannelElement *sce,
  415. - const float lambda)
  416. +static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
  417. {
  418. - int start = 0, i, w, w2, g;
  419. - float uplim[128], maxq[128];
  420. - int minq, maxsf;
  421. - float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
  422. - int last = 0, lastband = 0, curband = 0;
  423. - float avg_energy = 0.0;
  424. - if (sce->ics.num_windows == 1) {
  425. - start = 0;
  426. - for (i = 0; i < 1024; i++) {
  427. - if (i - start >= sce->ics.swb_sizes[curband]) {
  428. - start += sce->ics.swb_sizes[curband];
  429. - curband++;
  430. - }
  431. - if (sce->coeffs[i]) {
  432. - avg_energy += sce->coeffs[i] * sce->coeffs[i];
  433. - last = i;
  434. - lastband = curband;
  435. - }
  436. - }
  437. + FFPsyBand *band;
  438. + int w, g, w2, i;
  439. + int wlen = 1024 / sce->ics.num_windows;
  440. + int bandwidth, cutoff;
  441. + float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
  442. + float *NOR34 = &s->scoefs[3*128];
  443. + uint8_t nextband[128];
  444. + const float lambda = s->lambda;
  445. + const float freq_mult = avctx->sample_rate*0.5f/wlen;
  446. + const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
  447. + const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
  448. + const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
  449. + const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
  450. +
  451. + int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
  452. + / ((avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
  453. + * (lambda / 120.f);
  454. +
  455. + /** Keep this in sync with twoloop's cutoff selection */
  456. + float rate_bandwidth_multiplier = 1.5f;
  457. + int prev = -1000, prev_sf = -1;
  458. + int frame_bit_rate = (avctx->flags & CODEC_FLAG_QSCALE)
  459. + ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
  460. + : (avctx->bit_rate / avctx->channels);
  461. +
  462. + frame_bit_rate *= 1.15f;
  463. +
  464. + if (avctx->cutoff > 0) {
  465. + bandwidth = avctx->cutoff;
  466. } else {
  467. - for (w = 0; w < 8; w++) {
  468. - const float *coeffs = &sce->coeffs[w*128];
  469. - curband = start = 0;
  470. - for (i = 0; i < 128; i++) {
  471. - if (i - start >= sce->ics.swb_sizes[curband]) {
  472. - start += sce->ics.swb_sizes[curband];
  473. - curband++;
  474. - }
  475. - if (coeffs[i]) {
  476. - avg_energy += coeffs[i] * coeffs[i];
  477. - last = FFMAX(last, i);
  478. - lastband = FFMAX(lastband, curband);
  479. - }
  480. - }
  481. - }
  482. + bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
  483. }
  484. - last++;
  485. - avg_energy /= last;
  486. - if (avg_energy == 0.0f) {
  487. - for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
  488. - sce->sf_idx[i] = SCALE_ONE_POS;
  489. - return;
  490. - }
  491. +
  492. + cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
  493. +
  494. + memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
  495. + ff_init_nextband_map(sce, nextband);
  496. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  497. - start = w*128;
  498. + int wstart = w*128;
  499. for (g = 0; g < sce->ics.num_swb; g++) {
  500. - float *coefs = &sce->coeffs[start];
  501. - const int size = sce->ics.swb_sizes[g];
  502. - int start2 = start, end2 = start + size, peakpos = start;
  503. - float maxval = -1, thr = 0.0f, t;
  504. - maxq[w*16+g] = 0.0f;
  505. - if (g > lastband) {
  506. - maxq[w*16+g] = 0.0f;
  507. - start += size;
  508. - for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
  509. - memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
  510. + int noise_sfi;
  511. + float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
  512. + float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
  513. + float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
  514. + float min_energy = -1.0f, max_energy = 0.0f;
  515. + const int start = wstart+sce->ics.swb_offset[g];
  516. + const float freq = (start-wstart)*freq_mult;
  517. + const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
  518. + if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) {
  519. + if (!sce->zeroes[w*16+g])
  520. + prev_sf = sce->sf_idx[w*16+g];
  521. continue;
  522. }
  523. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  524. - for (i = 0; i < size; i++) {
  525. - float t = coefs[w2*128+i]*coefs[w2*128+i];
  526. - maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
  527. - thr += t;
  528. - if (sce->ics.num_windows == 1 && maxval < t) {
  529. - maxval = t;
  530. - peakpos = start+i;
  531. - }
  532. + band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  533. + sfb_energy += band->energy;
  534. + spread = FFMIN(spread, band->spread);
  535. + threshold += band->threshold;
  536. + if (!w2) {
  537. + min_energy = max_energy = band->energy;
  538. + } else {
  539. + min_energy = FFMIN(min_energy, band->energy);
  540. + max_energy = FFMAX(max_energy, band->energy);
  541. }
  542. }
  543. - if (sce->ics.num_windows == 1) {
  544. - start2 = FFMAX(peakpos - 2, start2);
  545. - end2 = FFMIN(peakpos + 3, end2);
  546. - } else {
  547. - start2 -= start;
  548. - end2 -= start;
  549. - }
  550. - start += size;
  551. - thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
  552. - t = 1.0 - (1.0 * start2 / last);
  553. - uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
  554. - }
  555. - }
  556. - memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  557. - abs_pow34_v(s->scoefs, sce->coeffs, 1024);
  558. - for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  559. - start = w*128;
  560. - for (g = 0; g < sce->ics.num_swb; g++) {
  561. - const float *coefs = &sce->coeffs[start];
  562. - const float *scaled = &s->scoefs[start];
  563. - const int size = sce->ics.swb_sizes[g];
  564. - int scf, prev_scf, step;
  565. - int min_scf = -1, max_scf = 256;
  566. - float curdiff;
  567. - if (maxq[w*16+g] < 21.544) {
  568. - sce->zeroes[w*16+g] = 1;
  569. - start += size;
  570. +
  571. + /* Ramps down at ~8000Hz and loosens the dist threshold */
  572. + dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias;
  573. +
  574. + /* PNS is acceptable when all of these are true:
  575. + * 1. high spread energy (noise-like band)
  576. + * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
  577. + * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
  578. + *
  579. + * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important)
  580. + */
  581. + if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) ||
  582. + ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
  583. + (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) ||
  584. + min_energy < pns_transient_energy_r * max_energy ) {
  585. + sce->pns_ener[w*16+g] = sfb_energy;
  586. + if (!sce->zeroes[w*16+g])
  587. + prev_sf = sce->sf_idx[w*16+g];
  588. continue;
  589. }
  590. - sce->zeroes[w*16+g] = 0;
  591. - scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
  592. - for (;;) {
  593. - float dist = 0.0f;
  594. - int quant_max;
  595. - for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  596. - int b;
  597. - dist += quantize_band_cost(s, coefs + w2*128,
  598. - scaled + w2*128,
  599. - sce->ics.swb_sizes[g],
  600. - scf,
  601. - ESC_BT,
  602. - lambda,
  603. - INFINITY,
  604. - &b,
  605. - 0);
  606. - dist -= b;
  607. + pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread);
  608. + noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
  609. + noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */
  610. + if (prev != -1000) {
  611. + int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO;
  612. + if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) {
  613. + if (!sce->zeroes[w*16+g])
  614. + prev_sf = sce->sf_idx[w*16+g];
  615. + continue;
  616. }
  617. - dist *= 1.0f / 512.0f / lambda;
  618. - quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512], ROUND_STANDARD);
  619. - if (quant_max >= 8191) { // too much, return to the previous quantizer
  620. - sce->sf_idx[w*16+g] = prev_scf;
  621. - break;
  622. + }
  623. + for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  624. + float band_energy, scale, pns_senergy;
  625. + const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
  626. + band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  627. + for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
  628. + s->random_state = lcg_random(s->random_state);
  629. + PNS[i] = s->random_state;
  630. }
  631. - prev_scf = scf;
  632. - curdiff = fabsf(dist - uplim[w*16+g]);
  633. - if (curdiff <= 1.0f)
  634. - step = 0;
  635. - else
  636. - step = log2f(curdiff);
  637. - if (dist > uplim[w*16+g])
  638. - step = -step;
  639. - scf += step;
  640. - scf = av_clip_uint8(scf);
  641. - step = scf - prev_scf;
  642. - if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
  643. - sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
  644. - break;
  645. - }
  646. - if (step > 0)
  647. - min_scf = prev_scf;
  648. - else
  649. - max_scf = prev_scf;
  650. + band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
  651. + scale = noise_amp/sqrtf(band_energy);
  652. + s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
  653. + pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
  654. + pns_energy += pns_senergy;
  655. + abs_pow34_v(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
  656. + abs_pow34_v(PNS34, PNS, sce->ics.swb_sizes[g]);
  657. + dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
  658. + NOR34,
  659. + sce->ics.swb_sizes[g],
  660. + sce->sf_idx[(w+w2)*16+g],
  661. + sce->band_alt[(w+w2)*16+g],
  662. + lambda/band->threshold, INFINITY, NULL, NULL, 0);
  663. + /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */
  664. + dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold;
  665. }
  666. - start += size;
  667. + if (g && sce->band_type[w*16+g-1] == NOISE_BT) {
  668. + dist2 += 5;
  669. + } else {
  670. + dist2 += 9;
  671. + }
  672. + energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
  673. + sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
  674. + if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
  675. + sce->band_type[w*16+g] = NOISE_BT;
  676. + sce->zeroes[w*16+g] = 0;
  677. + prev = noise_sfi;
  678. + } else {
  679. + if (!sce->zeroes[w*16+g])
  680. + prev_sf = sce->sf_idx[w*16+g];
  681. + }
  682. }
  683. }
  684. - minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
  685. - for (i = 1; i < 128; i++) {
  686. - if (!sce->sf_idx[i])
  687. - sce->sf_idx[i] = sce->sf_idx[i-1];
  688. - else
  689. - minq = FFMIN(minq, sce->sf_idx[i]);
  690. - }
  691. - if (minq == INT_MAX)
  692. - minq = 0;
  693. - minq = FFMIN(minq, SCALE_MAX_POS);
  694. - maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
  695. - for (i = 126; i >= 0; i--) {
  696. - if (!sce->sf_idx[i])
  697. - sce->sf_idx[i] = sce->sf_idx[i+1];
  698. - sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
  699. - }
  700. }
  701. -static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
  702. - SingleChannelElement *sce,
  703. - const float lambda)
  704. +static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
  705. {
  706. - int i, w, w2, g;
  707. - int minq = 255;
  708. + FFPsyBand *band;
  709. + int w, g, w2;
  710. + int wlen = 1024 / sce->ics.num_windows;
  711. + int bandwidth, cutoff;
  712. + const float lambda = s->lambda;
  713. + const float freq_mult = avctx->sample_rate*0.5f/wlen;
  714. + const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
  715. + const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
  716. - memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
  717. + int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
  718. + / ((avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
  719. + * (lambda / 120.f);
  720. +
  721. + /** Keep this in sync with twoloop's cutoff selection */
  722. + float rate_bandwidth_multiplier = 1.5f;
  723. + int frame_bit_rate = (avctx->flags & CODEC_FLAG_QSCALE)
  724. + ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
  725. + : (avctx->bit_rate / avctx->channels);
  726. +
  727. + frame_bit_rate *= 1.15f;
  728. +
  729. + if (avctx->cutoff > 0) {
  730. + bandwidth = avctx->cutoff;
  731. + } else {
  732. + bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
  733. + }
  734. +
  735. + cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
  736. +
  737. + memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
  738. for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  739. for (g = 0; g < sce->ics.num_swb; g++) {
  740. + float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
  741. + float min_energy = -1.0f, max_energy = 0.0f;
  742. + const int start = sce->ics.swb_offset[g];
  743. + const float freq = start*freq_mult;
  744. + const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
  745. + if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
  746. + sce->can_pns[w*16+g] = 0;
  747. + continue;
  748. + }
  749. for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  750. - FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  751. - if (band->energy <= band->threshold) {
  752. - sce->sf_idx[(w+w2)*16+g] = 218;
  753. - sce->zeroes[(w+w2)*16+g] = 1;
  754. + band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  755. + sfb_energy += band->energy;
  756. + spread = FFMIN(spread, band->spread);
  757. + threshold += band->threshold;
  758. + if (!w2) {
  759. + min_energy = max_energy = band->energy;
  760. } else {
  761. - sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
  762. - sce->zeroes[(w+w2)*16+g] = 0;
  763. + min_energy = FFMIN(min_energy, band->energy);
  764. + max_energy = FFMAX(max_energy, band->energy);
  765. }
  766. - minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
  767. }
  768. - }
  769. - }
  770. - for (i = 0; i < 128; i++) {
  771. - sce->sf_idx[i] = 140;
  772. - //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
  773. - }
  774. - //set the same quantizers inside window groups
  775. - for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
  776. - for (g = 0; g < sce->ics.num_swb; g++)
  777. - for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
  778. - sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
  779. -}
  780. -static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
  781. -{
  782. - int start = 0, w, w2, g;
  783. - const float lambda = s->lambda;
  784. - const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
  785. - const float spread_threshold = NOISE_SPREAD_THRESHOLD*(lambda/120.f);
  786. - const float thr_mult = NOISE_LAMBDA_NUMERATOR/lambda;
  787. -
  788. - for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
  789. - start = 0;
  790. - for (g = 0; g < sce->ics.num_swb; g++) {
  791. - if (start*freq_mult > NOISE_LOW_LIMIT*(lambda/170.0f)) {
  792. - float energy = 0.0f, threshold = 0.0f, spread = 0.0f;
  793. - for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  794. - FFPsyBand *band = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  795. - energy += band->energy;
  796. - threshold += band->threshold;
  797. - spread += band->spread;
  798. - }
  799. - if (spread > spread_threshold*sce->ics.group_len[w] &&
  800. - ((sce->zeroes[w*16+g] && energy >= threshold) ||
  801. - energy < threshold*thr_mult*sce->ics.group_len[w])) {
  802. - sce->band_type[w*16+g] = NOISE_BT;
  803. - sce->pns_ener[w*16+g] = energy / sce->ics.group_len[w];
  804. - sce->zeroes[w*16+g] = 0;
  805. - }
  806. + /* PNS is acceptable when all of these are true:
  807. + * 1. high spread energy (noise-like band)
  808. + * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
  809. + * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
  810. + */
  811. + sce->pns_ener[w*16+g] = sfb_energy;
  812. + if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
  813. + sce->can_pns[w*16+g] = 0;
  814. + } else {
  815. + sce->can_pns[w*16+g] = 1;
  816. }
  817. - start += sce->ics.swb_sizes[g];
  818. }
  819. }
  820. }
  821. static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
  822. {
  823. - int start = 0, i, w, w2, g;
  824. + int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
  825. + uint8_t nextband0[128], nextband1[128];
  826. float M[128], S[128];
  827. float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
  828. const float lambda = s->lambda;
  829. + const float mslambda = FFMIN(1.0f, lambda / 120.f);
  830. SingleChannelElement *sce0 = &cpe->ch[0];
  831. SingleChannelElement *sce1 = &cpe->ch[1];
  832. if (!cpe->common_window)
  833. return;
  834. +
  835. + /** Scout out next nonzero bands */
  836. + ff_init_nextband_map(sce0, nextband0);
  837. + ff_init_nextband_map(sce1, nextband1);
  838. +
  839. + prev_mid = sce0->sf_idx[0];
  840. + prev_side = sce1->sf_idx[0];
  841. for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
  842. start = 0;
  843. - for (g = 0; g < sce0->ics.num_swb; g++) {
  844. - if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
  845. - float dist1 = 0.0f, dist2 = 0.0f;
  846. + for (g = 0; g < sce0->ics.num_swb; g++) {
  847. + float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
  848. + if (!cpe->is_mask[w*16+g])
  849. + cpe->ms_mask[w*16+g] = 0;
  850. + if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
  851. + float Mmax = 0.0f, Smax = 0.0f;
  852. +
  853. + /* Must compute mid/side SF and book for the whole window group */
  854. for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  855. - FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  856. - FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
  857. - float minthr = FFMIN(band0->threshold, band1->threshold);
  858. - float maxthr = FFMAX(band0->threshold, band1->threshold);
  859. for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  860. M[i] = (sce0->coeffs[start+(w+w2)*128+i]
  861. + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
  862. S[i] = M[i]
  863. - sce1->coeffs[start+(w+w2)*128+i];
  864. }
  865. - abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  866. - abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  867. - abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  868. - abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  869. - dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
  870. - L34,
  871. - sce0->ics.swb_sizes[g],
  872. - sce0->sf_idx[(w+w2)*16+g],
  873. - sce0->band_type[(w+w2)*16+g],
  874. - lambda / band0->threshold, INFINITY, NULL, 0);
  875. - dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
  876. - R34,
  877. - sce1->ics.swb_sizes[g],
  878. - sce1->sf_idx[(w+w2)*16+g],
  879. - sce1->band_type[(w+w2)*16+g],
  880. - lambda / band1->threshold, INFINITY, NULL, 0);
  881. - dist2 += quantize_band_cost(s, M,
  882. - M34,
  883. - sce0->ics.swb_sizes[g],
  884. - sce0->sf_idx[(w+w2)*16+g],
  885. - sce0->band_type[(w+w2)*16+g],
  886. - lambda / maxthr, INFINITY, NULL, 0);
  887. - dist2 += quantize_band_cost(s, S,
  888. - S34,
  889. - sce1->ics.swb_sizes[g],
  890. - sce1->sf_idx[(w+w2)*16+g],
  891. - sce1->band_type[(w+w2)*16+g],
  892. - lambda / minthr, INFINITY, NULL, 0);
  893. + abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  894. + abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  895. + for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
  896. + Mmax = FFMAX(Mmax, M34[i]);
  897. + Smax = FFMAX(Smax, S34[i]);
  898. + }
  899. }
  900. - cpe->ms_mask[w*16+g] = dist2 < dist1;
  901. +
  902. + for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
  903. + float dist1 = 0.0f, dist2 = 0.0f;
  904. + int B0 = 0, B1 = 0;
  905. + int minidx;
  906. + int mididx, sididx;
  907. + int midcb, sidcb;
  908. +
  909. + minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]);
  910. + mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512);
  911. + sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512);
  912. + if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
  913. + && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
  914. + || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) {
  915. + /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
  916. + continue;
  917. + }
  918. +
  919. + midcb = find_min_book(Mmax, mididx);
  920. + sidcb = find_min_book(Smax, sididx);
  921. +
  922. + /* No CB can be zero */
  923. + midcb = FFMAX(1,midcb);
  924. + sidcb = FFMAX(1,sidcb);
  925. +
  926. + for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
  927. + FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
  928. + FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
  929. + float minthr = FFMIN(band0->threshold, band1->threshold);
  930. + int b1,b2,b3,b4;
  931. + for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
  932. + M[i] = (sce0->coeffs[start+(w+w2)*128+i]
  933. + + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
  934. + S[i] = M[i]
  935. + - sce1->coeffs[start+(w+w2)*128+i];
  936. + }
  937. +
  938. + abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  939. + abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
  940. + abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
  941. + abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
  942. + dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
  943. + L34,
  944. + sce0->ics.swb_sizes[g],
  945. + sce0->sf_idx[w*16+g],
  946. + sce0->band_type[w*16+g],
  947. + lambda / band0->threshold, INFINITY, &b1, NULL, 0);
  948. + dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
  949. + R34,
  950. + sce1->ics.swb_sizes[g],
  951. + sce1->sf_idx[w*16+g],
  952. + sce1->band_type[w*16+g],
  953. + lambda / band1->threshold, INFINITY, &b2, NULL, 0);
  954. + dist2 += quantize_band_cost(s, M,
  955. + M34,
  956. + sce0->ics.swb_sizes[g],
  957. + mididx,
  958. + midcb,
  959. + lambda / minthr, INFINITY, &b3, NULL, 0);
  960. + dist2 += quantize_band_cost(s, S,
  961. + S34,
  962. + sce1->ics.swb_sizes[g],
  963. + sididx,
  964. + sidcb,
  965. + mslambda / (minthr * bmax), INFINITY, &b4, NULL, 0);
  966. + B0 += b1+b2;
  967. + B1 += b3+b4;
  968. + dist1 -= b1+b2;
  969. + dist2 -= b3+b4;
  970. + }
  971. + cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
  972. + if (cpe->ms_mask[w*16+g]) {
  973. + if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) {
  974. + sce0->sf_idx[w*16+g] = mididx;
  975. + sce1->sf_idx[w*16+g] = sididx;
  976. + sce0->band_type[w*16+g] = midcb;
  977. + sce1->band_type[w*16+g] = sidcb;
  978. + } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) {
  979. + /* ms_mask unneeded, and it confuses some decoders */
  980. + cpe->ms_mask[w*16+g] = 0;
  981. + }
  982. + break;
  983. + } else if (B1 > B0) {
  984. + /* More boost won't fix this */
  985. + break;
  986. + }
  987. + }
  988. }
  989. + if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
  990. + prev_mid = sce0->sf_idx[w*16+g];
  991. + if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT)
  992. + prev_side = sce1->sf_idx[w*16+g];
  993. start += sce0->ics.swb_sizes[g];
  994. }
  995. }
  996. }
  997. AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
  998. - [AAC_CODER_FAAC] = {
  999. - search_for_quantizers_faac,
  1000. - encode_window_bands_info,
  1001. - quantize_and_encode_band,
  1002. - ff_aac_encode_tns_info,
  1003. - ff_aac_encode_main_pred,
  1004. - ff_aac_adjust_common_prediction,
  1005. - ff_aac_apply_main_pred,
  1006. - ff_aac_apply_tns,
  1007. - set_special_band_scalefactors,
  1008. - search_for_pns,
  1009. - ff_aac_search_for_tns,
  1010. - search_for_ms,
  1011. - ff_aac_search_for_is,
  1012. - ff_aac_search_for_pred,
  1013. - },
  1014. [AAC_CODER_ANMR] = {
  1015. search_for_quantizers_anmr,
  1016. encode_window_bands_info,
  1017. quantize_and_encode_band,
  1018. ff_aac_encode_tns_info,
  1019. + ff_aac_encode_ltp_info,
  1020. ff_aac_encode_main_pred,
  1021. - ff_aac_adjust_common_prediction,
  1022. + ff_aac_adjust_common_pred,
  1023. + ff_aac_adjust_common_ltp,
  1024. ff_aac_apply_main_pred,
  1025. ff_aac_apply_tns,
  1026. + ff_aac_update_ltp,
  1027. + ff_aac_ltp_insert_new_frame,
  1028. set_special_band_scalefactors,
  1029. search_for_pns,
  1030. + mark_pns,
  1031. ff_aac_search_for_tns,
  1032. + ff_aac_search_for_ltp,
  1033. search_for_ms,
  1034. ff_aac_search_for_is,
  1035. ff_aac_search_for_pred,
  1036. @@ -993,29 +921,41 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
  1037. codebook_trellis_rate,
  1038. quantize_and_encode_band,
  1039. ff_aac_encode_tns_info,
  1040. + ff_aac_encode_ltp_info,
  1041. ff_aac_encode_main_pred,
  1042. - ff_aac_adjust_common_prediction,
  1043. + ff_aac_adjust_common_pred,
  1044. + ff_aac_adjust_common_ltp,
  1045. ff_aac_apply_main_pred,
  1046. ff_aac_apply_tns,
  1047. + ff_aac_update_ltp,
  1048. + ff_aac_ltp_insert_new_frame,
  1049. set_special_band_scalefactors,
  1050. search_for_pns,
  1051. + mark_pns,
  1052. ff_aac_search_for_tns,
  1053. + ff_aac_search_for_ltp,
  1054. search_for_ms,
  1055. ff_aac_search_for_is,
  1056. ff_aac_search_for_pred,
  1057. },
  1058. [AAC_CODER_FAST] = {
  1059. search_for_quantizers_fast,
  1060. - encode_window_bands_info,
  1061. + codebook_trellis_rate,
  1062. quantize_and_encode_band,
  1063. ff_aac_encode_tns_info,
  1064. + ff_aac_encode_ltp_info,
  1065. ff_aac_encode_main_pred,
  1066. - ff_aac_adjust_common_prediction,
  1067. + ff_aac_adjust_common_pred,
  1068. + ff_aac_adjust_common_ltp,
  1069. ff_aac_apply_main_pred,
  1070. ff_aac_apply_tns,
  1071. + ff_aac_update_ltp,
  1072. + ff_aac_ltp_insert_new_frame,
  1073. set_special_band_scalefactors,
  1074. search_for_pns,
  1075. + mark_pns,
  1076. ff_aac_search_for_tns,
  1077. + ff_aac_search_for_ltp,
  1078. search_for_ms,
  1079. ff_aac_search_for_is,
  1080. ff_aac_search_for_pred,