1 /*
2 * AAC encoder TNS
3 * Copyright (C) 2015 Rostislav Pehlivanov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * AAC encoder temporal noise shaping
25 * @author Rostislav Pehlivanov ( atomnuker gmail com )
26 */
27
33
34 /* Could be set to 3 to save an additional bit at the cost of little quality */
36
37 /* Coefficient resolution in short windows */
38 #define TNS_Q_BITS_IS8 4
39
40 /* We really need the bits we save here elsewhere */
41 #define TNS_ENABLE_COEF_COMPRESSION
42
43 /* TNS will only be used if the LPC gain is within these margins */
44 #define TNS_GAIN_THRESHOLD_LOW 1.4f
45 #define TNS_GAIN_THRESHOLD_HIGH 1.16f*TNS_GAIN_THRESHOLD_LOW
46
48 {
50 const int low_idx = c_bits ? 4 : 2;
51 const int shift_val = c_bits ? 8 : 4;
52 const int high_idx = c_bits ? 11 : 5;
53 #ifndef TNS_ENABLE_COEF_COMPRESSION
54 return 0;
55 #endif /* TNS_ENABLE_COEF_COMPRESSION */
56 for (
i = 0;
i < order;
i++)
57 if (coef[
i] >= low_idx && coef[
i] <= high_idx)
58 return 0;
59 for (
i = 0;
i < order;
i++)
60 coef[
i] -= (coef[
i] > high_idx) ? shift_val : 0;
61 return 1;
62 }
63
64 /**
65 * Encode TNS data.
66 * Coefficient compression is simply not lossless as it should be
67 * on any decoder tested and as such is not active.
68 */
70 {
72 int i,
w,
filt, coef_compress = 0, coef_len;
75
77 return;
78
82 continue;
88 continue;
93 coef_len = c_bits + 3 - coef_compress;
96 }
97 }
98 }
99
100 /* Apply TNS filter */
102 {
105 int w,
filt, m,
i, top, order, bottom, start, end,
size, inc;
108
112 top = bottom;
115 if (order == 0)
116 continue;
117
118 // tns_decode_coef
120
123 if ((
size = end - start) <= 0)
124 continue;
126 inc = -1;
127 start = end - 1;
128 } else {
129 inc = 1;
130 }
132
133 /* AR filter */
134 for (m = 0; m <
size; m++, start += inc) {
135 for (
i = 1;
i <=
FFMIN(m, order);
i++) {
137 }
138 }
139 }
140 }
141 }
142
143 /*
144 * c_bits - 1 if 4 bit coefficients, 0 if 3 bit coefficients
145 */
147 int c_bits)
148 {
151 for (
i = 0;
i < order;
i++) {
153 lpc[
i] = quant_arr[idx[
i]];
154 }
155 }
156
157 /*
158 * 3 bits per coefficient with 8 short windows
159 */
161 {
173 const int sfb_len = sfb_end - sfb_start;
175
176 if (coef_len <= 0 || sfb_len <= 0) {
178 return;
179 }
180
182 float en[2] = {0.0f, 0.0f};
183 int oc_start = 0, os_start = 0;
185
187 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[
w*16+
g];
188 if (
g > sfb_start + (sfb_len/2))
190 else
192 }
193
194 /* LPC */
196 coef_len, order, coefs);
197
199 continue;
200
210 }
211 count++;
212 }
214 }