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 void conv_to_int32(int32_t *loc, float *samples, int num, float norm)
37 for (i = 0; i < num; i++)
38 loc[i] = ceilf((samples[i]/norm)*INT32_MAX);
41 static inline void conv_to_float(float *arr, int32_t *cof, int num)
44 for (i = 0; i < num; i++)
45 arr[i] = (float)cof[i]/INT32_MAX;
48 /* Input: quantized 4 bit coef, output: 1 if first (MSB) 2 bits are the same */
49 static inline int coef_test_compression(int coef)
52 int res = ff_ctz(tmp);
54 return 1; /* ...00 -> compressable */
56 return 0; /* ...10 -> uncompressable */
57 else if (ff_ctz(tmp >> 1) > 0)
58 return 0; /* ...0 1 -> uncompressable */
60 return 1; /* ...1 1 -> compressable */
63 static inline int compress_coef(int *coefs, int num)
66 for (i = 0; i < num; i++)
67 res += coef_test_compression(coefs[i]);
68 return res == num ? 1 : 0;
73 * Coefficient compression saves a single bit.
75 void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
77 int i, w, filt, coef_len, coef_compress;
78 const int coef_res = MAX_LPC_PRECISION == 4 ? 1 : 0;
79 const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
81 put_bits(&s->pb, 1, !!sce->tns.present);
83 if (!sce->tns.present)
86 for (i = 0; i < sce->ics.num_windows; i++) {
87 put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
88 if (sce->tns.n_filt[i]) {
89 put_bits(&s->pb, 1, !!coef_res);
90 for (filt = 0; filt < sce->tns.n_filt[i]; filt++) {
91 put_bits(&s->pb, 6 - 2 * is8, sce->tns.length[i][filt]);
92 put_bits(&s->pb, 5 - 2 * is8, sce->tns.order[i][filt]);
93 if (sce->tns.order[i][filt]) {
94 coef_compress = compress_coef(sce->tns.coef_idx[i][filt],
95 sce->tns.order[i][filt]);
96 put_bits(&s->pb, 1, !!sce->tns.direction[i][filt]);
97 put_bits(&s->pb, 1, !!coef_compress);
98 coef_len = coef_res + 3 - coef_compress;
99 for (w = 0; w < sce->tns.order[i][filt]; w++)
100 put_bits(&s->pb, coef_len, sce->tns.coef_idx[i][filt][w]);
107 static int process_tns_coeffs(TemporalNoiseShaping *tns, float *tns_coefs_raw,
108 int order, int w, int filt)
111 int *idx = tns->coef_idx[w][filt];
112 float *lpc = tns->coef[w][filt];
113 const int iqfac_p = ((1 << (MAX_LPC_PRECISION-1)) - 0.5)/(M_PI/2.0);
114 const int iqfac_m = ((1 << (MAX_LPC_PRECISION-1)) + 0.5)/(M_PI/2.0);
115 float temp[TNS_MAX_ORDER] = {0.0f}, out[TNS_MAX_ORDER] = {0.0f};
118 for (i = 0; i < order; i++) {
119 idx[i] = ceilf(asin(tns_coefs_raw[i])*((tns_coefs_raw[i] >= 0) ? iqfac_p : iqfac_m));
120 lpc[i] = 2*sin(idx[i]/((idx[i] >= 0) ? iqfac_p : iqfac_m));
123 /* Trim any coeff less than 0.1f from the end */
124 for (i = order; i > -1; i--) {
125 lpc[i] = (fabs(lpc[i]) > 0.1f) ? lpc[i] : 0.0f;
126 if (lpc[i] != 0.0 ) {
135 /* Step up procedure, convert to LPC coeffs */
137 for (i = 1; i <= order; i++) {
138 for (j = 1; j < i; j++) {
139 temp[j] = out[j] + lpc[i]*out[i-j];
141 for (j = 1; j <= i; j++) {
146 memcpy(lpc, out, TNS_MAX_ORDER*sizeof(float));
151 static void apply_tns_filter(float *out, float *in, int order, int direction,
152 float *tns_coefs, int ltp_used, int w, int filt,
153 int start_i, int len)
155 int i, j, inc, start = start_i;
156 float tmp[TNS_MAX_ORDER+1];
159 start = (start + len) - 1;
163 if (!ltp_used) { /* AR filter */
164 for (i = 0; i < len; i++, start += inc)
166 for (j = 1; j <= FFMIN(i, order); j++)
167 out[i] += tns_coefs[j]*in[start - j*inc];
168 } else { /* MA filter */
169 for (i = 0; i < len; i++, start += inc) {
170 tmp[0] = out[i] = in[start];
171 for (j = 1; j <= FFMIN(i, order); j++)
172 out[i] += tmp[j]*tns_coefs[j];
173 for (j = order; j > 0; j--)
179 void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
181 TemporalNoiseShaping *tns = &sce->tns;
182 int w, g, order, sfb_start, sfb_len, coef_start, shift[MAX_LPC_ORDER], count = 0;
183 const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
184 const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
185 const float freq_mult = mpeg4audio_sample_rates[s->samplerate_index]/(1024.0f/sce->ics.num_windows)/2.0f;
186 float max_coef = 0.0f;
188 sce->tns.present = 0;
191 for (coef_start = 0; coef_start < 1024; coef_start++)
192 max_coef = FFMAX(max_coef, sce->pcoeffs[coef_start]);
194 for (w = 0; w < sce->ics.num_windows; w++) {
195 int filters = 1, start = 0, coef_len = 0;
196 int32_t conv_coeff[1024] = {0};
197 int32_t coefs_t[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
199 /* Determine start sfb + coef - excludes anything below threshold */
200 for (g = 0; g < sce->ics.num_swb; g++) {
201 if (start*freq_mult > TNS_LOW_LIMIT) {
203 sfb_len = (w+1)*16 + g - sfb_start;
204 coef_start = sce->ics.swb_offset[sfb_start];
205 coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
208 start += sce->ics.swb_sizes[g];
214 conv_to_int32(conv_coeff, &sce->pcoeffs[coef_start], coef_len, max_coef);
217 order = ff_lpc_calc_coefs(&s->lpc, conv_coeff, coef_len,
218 TNS_MIN_PRED_ORDER, tns_max_order,
220 FF_LPC_TYPE_LEVINSON, 10,
221 ORDER_METHOD_EST, MAX_LPC_SHIFT, 0) - 1;
223 /* Works surprisingly well, remember to tweak MAX_LPC_SHIFT if you want to play around with this */
224 if (shift[order] > 3) {
226 float tns_coefs_raw[TNS_MAX_ORDER];
227 tns->n_filt[w] = filters++;
228 conv_to_float(tns_coefs_raw, coefs_t[order], order);
229 for (g = 0; g < tns->n_filt[w]; g++) {
230 process_tns_coeffs(tns, tns_coefs_raw, order, w, g);
231 apply_tns_filter(&sce->coeffs[coef_start], sce->pcoeffs, order, direction, tns->coef[w][g],
232 sce->ics.ltp.present, w, g, coef_start, coef_len);
233 tns->order[w][g] = order;
234 tns->length[w][g] = sfb_len;
235 tns->direction[w][g] = direction;
241 sce->tns.present = !!count;