41#define TNS_Q_BITS_IS8 4
44#define TNS_ENABLE_COEF_COMPRESSION
48#define TNS_PREDGAIN_GATE 1.4f
49#define TNS_PG_C1_LONG 1.4f
50#define TNS_PG_C1_SHORT 3.2f
51#define TNS_PG_CLAMP 6.0f
52#define TNS_WEIGHT_FLOOR 0.01f
57 const int low_idx = c_bits ? 4 : 2;
58 const int shift_val = c_bits ? 8 : 4;
59 const int high_idx = c_bits ? 11 : 5;
60#ifndef TNS_ENABLE_COEF_COMPRESSION
63 for (
i = 0;
i < order;
i++)
64 if (coef[
i] >= low_idx && coef[
i] <= high_idx)
66 for (
i = 0;
i < order;
i++)
67 coef[
i] -= (coef[
i] > high_idx) ? shift_val : 0;
75 int i,
w,
filt, coef_compress = 0, coef_len;
96 coef_len = c_bits + 3 - coef_compress;
107 for (
int g = 0;
g < mmm;
g++)
117 int w,
filt, m,
i, top, order, bottom, start, end,
size,
inc;
123 memcpy(hist, sce->
coeffs,
sizeof(hist));
139 e0 =
FFMIN( top, mmm);
142 if ((
size = end - start) <= 0)
153 for (m = 0; m <
size; m++, start +=
inc) {
154 for (
i = 1;
i <=
FFMIN(m, order);
i++) {
155 sce->
coeffs[start] += lpc[
i-1]*hist[start -
i*
inc];
171 for (
i = 0;
i < order;
i++) {
173 lpc[
i] = quant_arr[idx[
i]];
191 FFPsyBand *
const psy_bands = &
s->psy.ch[
s->cur_channel].psy_bands[0];
193 memset(tns, 0,
sizeof(*tns));
194 if (sfb_end - sfb_start <= 0)
198 const int clen = c_hi - c_lo;
211 for (
int w2 = 0; w2 < gl; w2++) {
213 float maxrms = 0.0f, floorrms;
214 for (
int g = sfb_start;
g < sfb_end;
g++) {
216 float rms =
sqrtf(
FFMAX(psy_bands[
w*16 +
g].threshold, 0.0f) /
FFMAX(s1 - s0, 1));
217 maxrms =
FFMAX(maxrms, rms);
220 for (
int g = sfb_start;
g < sfb_end;
g++) {
222 float rms =
sqrtf(
FFMAX(psy_bands[
w*16 +
g].threshold, 0.0f) /
FFMAX(s1 - s0, 1));
223 float wgt = 1.0f /
FFMAX(rms, floorrms);
224 for (
int k = s0; k < s1; k++)
225 pooled[w2*clen + (k - c_lo)] = sce->
coeffs[
w*128 + k] * wgt;
230 if (!isfinite(gain) || gain < TNS_PREDGAIN_GATE || gain >
TNS_PG_CLAMP)
232 for (
int i = 0;
i < ord_g;
i++)
233 coefs[
i] = -coefs[
i];
240 for (
int w2 = 0; w2 < gl; w2++) {
241 const float *msrc = pooled + w2*clen;
242 float orig_e = 0.0f, filt_e = 0.0f;
243 for (
int m = 0; m < clen; m++) {
245 for (
int i = 1;
i <=
FFMIN(m, ord_g);
i++)
246 acc += lpc_q[
i-1] * msrc[m -
i];
247 orig_e += msrc[m]*msrc[m];
250 gmin =
FFMIN(gmin, orig_e /
FFMAX(filt_e, 1e-9f));
255 int in_run =
s->nmr ?
s->nmr->prev_was_short : 0;
256 int prev_on =
s->nmr ?
s->nmr->tns8_prev[
s->cur_channel & 15] : 0;
257 float bar =
TNS_PG_C1_SHORT * (!in_run ? 1.0f : prev_on ? 0.5f : 1.8f);
263 for (
int w2 = 0; w2 < gl; w2++) {
266 tns->
length[
w][0] = sfb_end - sfb_start;
271 memcpy(tns->
coef[
w][0], tns->
coef[wh][0],
sizeof(tns->
coef[
w][0]));
279 s->nmr->tns8_prev[
s->cur_channel & 15] = !!count;
291 const int order = is8 ? 7 : 12;
294 const int sfb_len = sfb_end - sfb_start;
297 const int ord_g = order / n_filt;
302 FFPsyBand *
const psy_bands = &
s->psy.ch[
s->cur_channel].psy_bands[0];
304 if (coef_len <= 0 || sfb_len <= 0) {
315 const int tlen = is8 ? 256 : 2048;
317 float mgain[8] = {0};
325 const float *tw = sce->
ret_buf +
w*tlen;
326 float e_early = 0.0f, e_late = 0.0f;
328 for (ti = 0; ti < tlen/2; ti++)
329 e_early += tw[ti]*tw[ti];
330 for (; ti < tlen; ti++)
331 e_late += tw[ti]*tw[ti];
332 const int tdir = e_early > e_late;
341 int len_sfb = (
filt == n_filt - 1) ? sfb_len -
filt*(sfb_len/n_filt)
343 int bot_sfb =
FFMAX(0, top_sfb - len_sfb);
344 int g_lo =
FFMIN(bot_sfb, mmm), g_hi =
FFMIN(top_sfb, mmm);
347 int clen = c_hi - c_lo;
348 const int dir = slant != 2 ? slant : tdir;
349 float gain, orig_e = 0.0f, filt_e = 0.0f;
350 int m,
i,
g,
inc, st;
362 float maxrms = 0.0f, floorrms;
364 for (
g = g_lo;
g < g_hi;
g++) {
366 float rms =
sqrtf(
FFMAX(psy_bands[
w*16 +
g].threshold, 0.0f) /
368 maxrms =
FFMAX(maxrms, rms);
371 for (
g = g_lo;
g < g_hi;
g++) {
373 float rms =
sqrtf(
FFMAX(psy_bands[
w*16 +
g].threshold, 0.0f) /
375 float wgt = 1.0f /
FFMAX(rms, floorrms);
376 for (k = s0; k < s1; k++)
377 wspec[k - c_lo] = sce->
coeffs[
w*128 + k] * wgt;
384 if (!isfinite(gain) || gain < TNS_PREDGAIN_GATE || gain >
TNS_PG_CLAMP)
388 for (
i = 0;
i < ord_g;
i++)
389 coefs[
i] = -coefs[
i];
397 const float *msrc = wspec;
399 st = dir ? clen - 1 : 0;
400 for (m = 0; m < clen; m++) {
401 int idx = st + m*
inc;
402 float acc = msrc[idx];
403 for (
i = 1;
i <=
FFMIN(m, ord_g);
i++)
404 acc += lpc_q[
i-1] * msrc[idx -
i*
inc];
407 for (m = 0; m < clen; m++) {
408 orig_e += msrc[m]*msrc[m];
411 filt_e =
FFMAX(filt_e, 1e-9f);
414 if (orig_e <
c1*filt_e)
419 mgain[
w] = orig_e / filt_e;
422 tns->
n_filt[
w] = any ? n_filt : 0;
430 const float gspread = 2.0f;
434 float gmin = FLT_MAX, gmax = 0.0f;
435 for (
int w2 =
w; w2 <
w + gl; w2++) {
436 if (!tns->
n_filt[w2] || mgain[w2] <= 0.0f) { drop = 1;
break; }
437 gmin =
FFMIN(gmin, mgain[w2]);
438 gmax =
FFMAX(gmax, mgain[w2]);
440 if (!drop && gmax > gspread * gmin)
442 for (
int w2 =
w; w2 <
w + gl; w2++) {
445 for (
int f2 = 0; f2 < n_filt; f2++)
446 tns->
order[w2][f2] = 0;
447 }
else if (tns->
n_filt[w2]) {
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
static int tns_max_nonpns(const SingleChannelElement *sce, int mmm)
void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
Encode TNS data.
static void quantize_coefs(double *coef, int *idx, float *lpc, int order, int c_bits)
static void search_for_tns_short_pooled(AACEncContext *s, SingleChannelElement *sce)
static int compress_coeffs(int *coef, int order, int c_bits)
void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
AAC encoder temporal noise shaping.
static int quant_array_idx(const float val, const float *arr, const int num)
static const uint8_t *const tns_min_sfb[2]
const float *const ff_tns_tmp2_map[4]
static const int8_t filt[NUMTAPS *2]
#define i(width, name, range_min, range_max)
static __device__ float sqrtf(float a)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
double ff_lpc_calc_ref_coefs_f(LPCContext *s, const float *samples, int len, int order, double *ref, int apply_window)
static int compute_lpc_coefs(const LPC_TYPE *autoc, int i, int max_order, LPC_TYPE *lpc, int lpc_stride, int fail, int normalize, LPC_TYPE *err_ptr)
Levinson-Durbin recursion.
static int inc(int num, int period)
single band psychoacoustic information
Individual Channel Stream.
uint8_t max_sfb
number of scalefactor bands per group
int num_swb
number of scalefactor window bands
enum WindowSequence window_sequence[2]
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Single Channel Element - used for both SCE and LFE elements.
float coeffs[1024]
coefficients for IMDCT, maybe processed
float ret_buf[2048]
PCM output buffer.
enum BandType band_type[128]
band types
IndividualChannelStream ics
float coef[8][4][TNS_MAX_ORDER]
int coef_idx[8][4][TNS_MAX_ORDER]
static double b0(void *priv, double x, double y)