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"
34 static inline int compress_coef(int *coefs, int num)
37 for (i = 0; i < num; i++)
38 c += coefs[i] < 4 || coefs[i] > 11;
44 * Coefficient compression saves a single bit per coefficient.
46 void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
48 int i, w, filt, coef_len, coef_compress;
49 const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
51 if (!sce->tns.present)
54 for (i = 0; i < sce->ics.num_windows; i++) {
55 put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
56 if (sce->tns.n_filt[i]) {
57 put_bits(&s->pb, 1, 1);
58 for (filt = 0; filt < sce->tns.n_filt[i]; filt++) {
59 put_bits(&s->pb, 6 - 2 * is8, sce->tns.length[i][filt]);
60 put_bits(&s->pb, 5 - 2 * is8, sce->tns.order[i][filt]);
61 if (sce->tns.order[i][filt]) {
62 coef_compress = compress_coef(sce->tns.coef_idx[i][filt],
63 sce->tns.order[i][filt]);
64 put_bits(&s->pb, 1, !!sce->tns.direction[i][filt]);
65 put_bits(&s->pb, 1, !!coef_compress);
66 coef_len = 4 - coef_compress;
67 for (w = 0; w < sce->tns.order[i][filt]; w++)
68 put_bits(&s->pb, coef_len, sce->tns.coef_idx[i][filt][w]);
75 static void process_tns_coeffs(TemporalNoiseShaping *tns, double *coef_raw,
76 int *order_p, int w, int filt)
78 int i, j, order = *order_p;
79 int *idx = tns->coef_idx[w][filt];
80 float *lpc = tns->coef[w][filt];
81 float temp[TNS_MAX_ORDER] = {0.0f}, out[TNS_MAX_ORDER] = {0.0f};
86 /* Not what the specs say, but it's better */
87 for (i = 0; i < order; i++) {
88 idx[i] = quant_array_idx(coef_raw[i], tns_tmp2_map_0_4, 16);
89 lpc[i] = tns_tmp2_map_0_4[idx[i]];
92 /* Trim any coeff less than 0.1f from the end */
93 for (i = order-1; i > -1; i--) {
94 lpc[i] = (fabs(lpc[i]) > 0.1f) ? lpc[i] : 0.0f;
100 order = av_clip(order, 0, TNS_MAX_ORDER - 1);
105 /* Step up procedure, convert to LPC coeffs */
107 for (i = 1; i <= order; i++) {
108 for (j = 1; j < i; j++) {
109 temp[j] = out[j] + lpc[i]*out[i-j];
111 for (j = 1; j <= i; j++) {
116 memcpy(lpc, out, TNS_MAX_ORDER*sizeof(float));
119 /* Apply TNS filter */
120 void ff_aac_apply_tns(SingleChannelElement *sce)
122 float *coef = sce->pcoeffs;
123 TemporalNoiseShaping *tns = &sce->tns;
125 int bottom, top, order, start, end, size, inc;
126 float *lpc, tmp[TNS_MAX_ORDER+1];
128 for (w = 0; w < sce->ics.num_windows; w++) {
129 bottom = sce->ics.num_swb;
130 for (filt = 0; filt < tns->n_filt[w]; filt++) {
132 bottom = FFMAX(0, top - tns->length[w][filt]);
133 order = tns->order[w][filt];
134 lpc = tns->coef[w][filt];
138 start = sce->ics.swb_offset[bottom];
139 end = sce->ics.swb_offset[top];
140 if ((size = end - start) <= 0)
142 if (tns->direction[w][filt]) {
150 if (!sce->ics.ltp.present) {
152 for (m = 0; m < size; m++, start += inc)
153 for (i = 1; i <= FFMIN(m, order); i++)
154 coef[start] += coef[start - i * inc]*lpc[i - 1];
157 for (m = 0; m < size; m++, start += inc) {
158 tmp[0] = coef[start];
159 for (i = 1; i <= FFMIN(m, order); i++)
160 coef[start] += tmp[i]*lpc[i - 1];
161 for (i = order; i > 0; i--)
169 void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
171 TemporalNoiseShaping *tns = &sce->tns;
172 int w, g, w2, prev_end_sfb = 0, count = 0;
173 const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
174 const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
176 for (w = 0; w < sce->ics.num_windows; w++) {
177 int order = 0, filters = 1;
178 int sfb_start = 0, sfb_len = 0;
179 int coef_start = 0, coef_len = 0;
180 float energy = 0.0f, threshold = 0.0f;
181 double coefs[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
182 for (g = 0; g < sce->ics.num_swb; g++) {
183 if (!sfb_start && w*16+g > TNS_LOW_LIMIT && w*16+g > prev_end_sfb) {
185 coef_start = sce->ics.swb_offset[sfb_start];
188 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
189 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
190 if (!sfb_len && band->energy < band->threshold*1.3f) {
191 sfb_len = (w+w2)*16+g - sfb_start;
192 prev_end_sfb = sfb_start + sfb_len;
193 coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
196 energy += band->energy;
197 threshold += band->threshold;
200 sfb_len = (w+1)*16+g - sfb_start - 1;
201 coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
206 if (sfb_len <= 0 || coef_len <= 0)
208 if (coef_start + coef_len >= 1024)
209 coef_len = 1024 - coef_start;
212 order = ff_lpc_calc_levinson(&s->lpc, &sce->coeffs[coef_start], coef_len,
213 coefs, 0, tns_max_order, ORDER_METHOD_LOG);
215 if (energy > threshold) {
217 tns->n_filt[w] = filters++;
218 for (g = 0; g < tns->n_filt[w]; g++) {
219 process_tns_coeffs(tns, coefs[order], &order, w, g);
220 tns->order[w][g] = order;
221 tns->length[w][g] = sfb_len;
222 tns->direction[w][g] = direction;
228 sce->tns.present = !!count;