3 * Copyright (C) 2015 Rostislav Pehlivanov
5 * This file is part of FFmpeg.
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.
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.
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
24 * AAC encoder temporal noise shaping
25 * @author Rostislav Pehlivanov ( atomnuker gmail com )
29 #include "aacenc_tns.h"
31 #include "aacenc_utils.h"
32 #include "aacenc_quantization.h"
36 * Coefficient compression saves a single bit per coefficient.
38 void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
41 const uint8_t coef_res = TNS_Q_BITS == 4;
42 int i, w, filt, coef_len, coef_compress = 0;
43 const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
44 TemporalNoiseShaping *tns = &sce->tns;
46 if (!sce->tns.present)
49 for (i = 0; i < sce->ics.num_windows; i++) {
50 put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
52 put_bits(&s->pb, 1, coef_res);
53 for (filt = 0; filt < tns->n_filt[i]; filt++) {
54 put_bits(&s->pb, 6 - 2 * is8, tns->length[i][filt]);
55 put_bits(&s->pb, 5 - 2 * is8, tns->order[i][filt]);
56 if (tns->order[i][filt]) {
57 put_bits(&s->pb, 1, !!tns->direction[i][filt]);
58 put_bits(&s->pb, 1, !!coef_compress);
59 coef_len = coef_res + 3 - coef_compress;
60 for (w = 0; w < tns->order[i][filt]; w++) {
61 u_coef = (tns->coef_idx[i][filt][w])&(~(~0<<coef_len));
62 put_bits(&s->pb, coef_len, u_coef);
70 static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order)
74 const float *quant_arr = tns_tmp2_map[TNS_Q_BITS == 4];
75 const double iqfac_p = ((1 << (TNS_Q_BITS-1)) - 0.5)/(M_PI/2.0);
76 const double iqfac_m = ((1 << (TNS_Q_BITS-1)) + 0.5)/(M_PI/2.0);
77 for (i = 0; i < order; i++) {
78 idx[i] = ceilf(asin(coef[i])*((coef[i] >= 0) ? iqfac_p : iqfac_m));
79 u_coef = (idx[i])&(~(~0<<TNS_Q_BITS));
80 lpc[i] = quant_arr[u_coef];
84 /* Apply TNS filter */
85 void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
87 TemporalNoiseShaping *tns = &sce->tns;
88 IndividualChannelStream *ics = &sce->ics;
89 int w, filt, m, i, top, order, bottom, start, end, size, inc;
90 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
91 float lpc[TNS_MAX_ORDER];
93 for (w = 0; w < ics->num_windows; w++) {
94 bottom = ics->num_swb;
95 for (filt = 0; filt < tns->n_filt[w]; filt++) {
97 bottom = FFMAX(0, top - tns->length[w][filt]);
98 order = tns->order[w][filt];
103 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
105 start = ics->swb_offset[FFMIN(bottom, mmm)];
106 end = ics->swb_offset[FFMIN( top, mmm)];
107 if ((size = end - start) <= 0)
109 if (tns->direction[w][filt]) {
118 for (m = 0; m < size; m++, start += inc)
119 for (i = 1; i <= FFMIN(m, order); i++)
120 sce->coeffs[start] += lpc[i-1]*sce->pcoeffs[start - i*inc];
125 void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
127 TemporalNoiseShaping *tns = &sce->tns;
128 int w, w2, g, count = 0;
129 const int mmm = FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb);
130 const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
131 const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
133 int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm);
134 int sfb_end = av_clip(sce->ics.num_swb, 0, mmm);
136 for (w = 0; w < sce->ics.num_windows; w++) {
137 float e_ratio = 0.0f, threshold = 0.0f, spread = 0.0f, en[2] = {0.0, 0.0f};
138 double gain = 0.0f, coefs[MAX_LPC_ORDER] = {0};
139 int coef_start = w*sce->ics.num_swb + sce->ics.swb_offset[sfb_start];
140 int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
142 for (g = 0; g < sce->ics.num_swb; g++) {
143 if (w*16+g < sfb_start || w*16+g > sfb_end)
145 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
146 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
147 if ((w+w2)*16+g > sfb_start + ((sfb_end - sfb_start)/2))
148 en[1] += band->energy;
150 en[0] += band->energy;
151 threshold += band->threshold;
152 spread += band->spread;
156 if (coef_len <= 0 || (sfb_end - sfb_start) <= 0)
159 e_ratio = en[0]/en[1];
162 gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[coef_start],
163 coef_len, order, coefs);
165 if (gain > TNS_GAIN_THRESHOLD_LOW && gain < TNS_GAIN_THRESHOLD_HIGH &&
166 (en[0]+en[1]) > TNS_GAIN_THRESHOLD_LOW*threshold &&
167 spread < TNS_SPREAD_THRESHOLD && order) {
168 if (is8 || order < 2 || (e_ratio > TNS_E_RATIO_LOW && e_ratio < TNS_E_RATIO_HIGH)) {
170 for (g = 0; g < tns->n_filt[w]; g++) {
171 tns->length[w][g] = sfb_end - sfb_start;
172 tns->direction[w][g] = en[0] < en[1];
173 tns->order[w][g] = order;
174 quantize_coefs(coefs, tns->coef_idx[w][g], tns->coef[w][g],
177 } else { /* 2 filters due to energy disbalance */
179 for (g = 0; g < tns->n_filt[w]; g++) {
180 tns->direction[w][g] = en[g] < en[!g];
181 tns->order[w][g] = !g ? order/2 : order - tns->order[w][g-1];
182 tns->length[w][g] = !g ? (sfb_end - sfb_start)/2 : \
183 (sfb_end - sfb_start) - tns->length[w][g-1];
184 quantize_coefs(&coefs[!g ? 0 : order - tns->order[w][g-1]],
185 tns->coef_idx[w][g], tns->coef[w][g],
193 sce->tns.present = !!count;