diff --git a/libavcodec/aacenc_tns.c b/libavcodec/aacenc_tns.c index 3c442e81d6..637b81385e 100644 --- a/libavcodec/aacenc_tns.c +++ b/libavcodec/aacenc_tns.c @@ -67,7 +67,7 @@ void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce) } } -static int quantize_coefs(double *coef, int *idx, float *lpc, int order) +static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order) { int i; uint8_t u_coef; @@ -79,7 +79,6 @@ static int quantize_coefs(double *coef, int *idx, float *lpc, int order) u_coef = (idx[i])&(~(~0<ics.tns_max_bands, sce->ics.max_sfb); const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE; - int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER; + const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER; int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm); int sfb_end = av_clip(sce->ics.num_swb, 0, mmm); for (w = 0; w < sce->ics.num_windows; w++) { - float en_low = 0.0f, en_high = 0.0f, threshold = 0.0f, spread = 0.0f; + float e_ratio = 0.0f, threshold = 0.0f, spread = 0.0f, en[2] = {0.0, 0.0f}; double gain = 0.0f, coefs[MAX_LPC_ORDER] = {0}; - int coef_start = w*sce->ics.num_swb + sce->ics.swb_offset[sfb_start]; int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start]; @@ -147,9 +145,9 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce) for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; if ((w+w2)*16+g > sfb_start + ((sfb_end - sfb_start)/2)) - en_high += band->energy; + en[1] += band->energy; else - en_low += band->energy; + en[0] += band->energy; threshold += band->threshold; spread += band->spread; } @@ -157,22 +155,36 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce) if (coef_len <= 0 || (sfb_end - sfb_start) <= 0) continue; + else + e_ratio = en[0]/en[1]; /* LPC */ gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[coef_start], coef_len, order, coefs); - gain *= s->lambda/110.0f; - - if (gain > TNS_GAIN_THRESHOLD_LOW && gain*0 < TNS_GAIN_THRESHOLD_HIGH && - (en_low+en_high) > TNS_GAIN_THRESHOLD_LOW*threshold && - spread > TNS_SPREAD_THRESHOLD) { - tns->n_filt[w] = 1; - for (g = 0; g < tns->n_filt[w]; g++) { - tns->length[w][g] = sfb_end - sfb_start; - tns->direction[w][g] = en_low < en_high && TNS_DIRECTION_VARY; - tns->order[w][g] = quantize_coefs(coefs, tns->coef_idx[w][g], - tns->coef[w][g], order); + if (gain > TNS_GAIN_THRESHOLD_LOW && gain < TNS_GAIN_THRESHOLD_HIGH && + (en[0]+en[1]) > TNS_GAIN_THRESHOLD_LOW*threshold && + spread < TNS_SPREAD_THRESHOLD && order) { + if (is8 || order < 2 || (e_ratio > TNS_E_RATIO_LOW && e_ratio < TNS_E_RATIO_HIGH)) { + tns->n_filt[w] = 1; + for (g = 0; g < tns->n_filt[w]; g++) { + tns->length[w][g] = sfb_end - sfb_start; + tns->direction[w][g] = en[0] < en[1]; + tns->order[w][g] = order; + quantize_coefs(coefs, tns->coef_idx[w][g], tns->coef[w][g], + order); + } + } else { /* 2 filters due to energy disbalance */ + tns->n_filt[w] = 2; + for (g = 0; g < tns->n_filt[w]; g++) { + tns->direction[w][g] = en[g] < en[!g]; + tns->order[w][g] = !g ? order/2 : order - tns->order[w][g-1]; + tns->length[w][g] = !g ? (sfb_end - sfb_start)/2 : \ + (sfb_end - sfb_start) - tns->length[w][g-1]; + quantize_coefs(&coefs[!g ? 0 : order - tns->order[w][g-1]], + tns->coef_idx[w][g], tns->coef[w][g], + tns->order[w][g]); + } } count++; } diff --git a/libavcodec/aacenc_tns.h b/libavcodec/aacenc_tns.h index 812deeac8c..f0e75569ae 100644 --- a/libavcodec/aacenc_tns.h +++ b/libavcodec/aacenc_tns.h @@ -37,11 +37,13 @@ #define TNS_GAIN_THRESHOLD_LOW 1.395f #define TNS_GAIN_THRESHOLD_HIGH 11.19f -/* Do not use TNS if the psy band spread is below this value */ -#define TNS_SPREAD_THRESHOLD 20.081512f +/* If the energy ratio between the low SFBs vs the high SFBs is not between + * those two values, use 2 filters instead */ +#define TNS_E_RATIO_LOW 0.77 +#define TNS_E_RATIO_HIGH 1.23 -/* Allows to reverse the filter direction if the band energy is uneven */ -#define TNS_DIRECTION_VARY 1 +/* Do not use TNS if the psy band spread is below this value */ +#define TNS_SPREAD_THRESHOLD 37.081512f void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce); void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce);