Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * AAC coefficients encoder | ||
3 | * Copyright (C) 2008-2009 Konstantin Shishkov | ||
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 coefficients encoder | ||
25 | */ | ||
26 | |||
27 | /*********************************** | ||
28 | * TODOs: | ||
29 | * speedup quantizer selection | ||
30 | * add sane pulse detection | ||
31 | ***********************************/ | ||
32 | |||
33 | #include "libavutil/libm.h" // brought forward to work around cygwin header breakage | ||
34 | |||
35 | #include <float.h> | ||
36 | |||
37 | #include "libavutil/mathematics.h" | ||
38 | #include "mathops.h" | ||
39 | #include "avcodec.h" | ||
40 | #include "put_bits.h" | ||
41 | #include "aac.h" | ||
42 | #include "aacenc.h" | ||
43 | #include "aactab.h" | ||
44 | #include "aacenctab.h" | ||
45 | #include "aacenc_utils.h" | ||
46 | #include "aacenc_quantization.h" | ||
47 | |||
48 | #include "aacenc_is.h" | ||
49 | #include "aacenc_tns.h" | ||
50 | |||
51 | #include "libavcodec/aaccoder_twoloop.h" | ||
52 | |||
53 | /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread | ||
54 | * beyond which no PNS is used (since the SFBs contain tone rather than noise) */ | ||
55 | #define NOISE_SPREAD_THRESHOLD 0.9f | ||
56 | |||
57 | /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to | ||
58 | * replace low energy non zero bands */ | ||
59 | #define NOISE_LAMBDA_REPLACE 1.948f | ||
60 | |||
61 | #include "libavcodec/aaccoder_trellis.h" | ||
62 | |||
63 | typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb, | ||
64 | const float *in, float *quant, const float *scaled, | ||
65 | int size, int scale_idx, int cb, | ||
66 | const float lambda, const float uplim, | ||
67 | int *bits, float *energy); | ||
68 | |||
69 | /** | ||
70 | * Calculate rate distortion cost for quantizing with given codebook | ||
71 | * | ||
72 | * @return quantization distortion | ||
73 | */ | ||
74 | 8425788 | static av_always_inline float quantize_and_encode_band_cost_template( | |
75 | struct AACEncContext *s, | ||
76 | PutBitContext *pb, const float *in, float *out, | ||
77 | const float *scaled, int size, int scale_idx, | ||
78 | int cb, const float lambda, const float uplim, | ||
79 | int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, | ||
80 | int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, | ||
81 | const float ROUNDING) | ||
82 | { | ||
83 | 8425788 | const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; | |
84 | 8425788 | const float Q = ff_aac_pow2sf_tab [q_idx]; | |
85 | 8425788 | const float Q34 = ff_aac_pow34sf_tab[q_idx]; | |
86 | 8425788 | const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | |
87 | 8425788 | const float CLIPPED_ESCAPE = 165140.0f*IQ; | |
88 | 8425788 | float cost = 0; | |
89 | 8425788 | float qenergy = 0; | |
90 |
2/2✓ Branch 0 taken 4179380 times.
✓ Branch 1 taken 4246408 times.
|
8425788 | const int dim = BT_PAIR ? 2 : 4; |
91 | 8425788 | int resbits = 0; | |
92 | int off; | ||
93 | |||
94 |
6/6✓ Branch 0 taken 6892720 times.
✓ Branch 1 taken 1533068 times.
✓ Branch 2 taken 6879420 times.
✓ Branch 3 taken 13300 times.
✓ Branch 4 taken 17350 times.
✓ Branch 5 taken 6862070 times.
|
8425788 | if (BT_ZERO || BT_NOISE || BT_STEREO) { |
95 |
2/2✓ Branch 0 taken 35183532 times.
✓ Branch 1 taken 1563718 times.
|
36747250 | for (int i = 0; i < size; i++) |
96 | 35183532 | cost += in[i]*in[i]; | |
97 |
2/2✓ Branch 0 taken 1540776 times.
✓ Branch 1 taken 22942 times.
|
1563718 | if (bits) |
98 | 1540776 | *bits = 0; | |
99 |
2/2✓ Branch 0 taken 1513409 times.
✓ Branch 1 taken 50309 times.
|
1563718 | if (energy) |
100 | 1513409 | *energy = qenergy; | |
101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1563718 times.
|
1563718 | if (out) { |
102 | ✗ | for (int i = 0; i < size; i += dim) | |
103 | ✗ | for (int j = 0; j < dim; j++) | |
104 | ✗ | out[i+j] = 0.0f; | |
105 | } | ||
106 | 1563718 | return cost * lambda; | |
107 | } | ||
108 |
2/2✓ Branch 0 taken 429687 times.
✓ Branch 1 taken 6432383 times.
|
6862070 | if (!scaled) { |
109 | 429687 | s->aacdsp.abs_pow34(s->scoefs, in, size); | |
110 | 429687 | scaled = s->scoefs; | |
111 | } | ||
112 | 6862070 | s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING); | |
113 |
2/2✓ Branch 0 taken 4249191 times.
✓ Branch 1 taken 2612879 times.
|
6862070 | if (BT_UNSIGNED) { |
114 | 4249191 | off = 0; | |
115 | } else { | ||
116 | 2612879 | off = aac_cb_maxval[cb]; | |
117 | } | ||
118 |
2/2✓ Branch 0 taken 54442178 times.
✓ Branch 1 taken 6862070 times.
|
61304248 | for (int i = 0; i < size; i += dim) { |
119 | const float *vec; | ||
120 | 54442178 | int *quants = s->qcoefs + i; | |
121 | 54442178 | int curidx = 0; | |
122 | int curbits; | ||
123 | 54442178 | float quantized, rd = 0.0f; | |
124 |
2/2✓ Branch 0 taken 140009768 times.
✓ Branch 1 taken 54442178 times.
|
194451946 | for (int j = 0; j < dim; j++) { |
125 | 140009768 | curidx *= aac_cb_range[cb]; | |
126 | 140009768 | curidx += quants[j] + off; | |
127 | } | ||
128 | 54442178 | curbits = ff_aac_spectral_bits[cb-1][curidx]; | |
129 | 54442178 | vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | |
130 |
2/2✓ Branch 0 taken 34484091 times.
✓ Branch 1 taken 19958087 times.
|
54442178 | if (BT_UNSIGNED) { |
131 |
2/2✓ Branch 0 taken 82250064 times.
✓ Branch 1 taken 34484091 times.
|
116734155 | for (int j = 0; j < dim; j++) { |
132 | 82250064 | float t = fabsf(in[i+j]); | |
133 | float di; | ||
134 |
4/4✓ Branch 0 taken 14740292 times.
✓ Branch 1 taken 67509772 times.
✓ Branch 2 taken 1598091 times.
✓ Branch 3 taken 13142201 times.
|
82250064 | if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow |
135 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 1597978 times.
|
1598091 | if (t >= CLIPPED_ESCAPE) { |
136 | 113 | quantized = CLIPPED_ESCAPE; | |
137 | 113 | curbits += 21; | |
138 | } else { | ||
139 | 1597978 | int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13); | |
140 | 1597978 | quantized = c*cbrtf(c)*IQ; | |
141 | 1597978 | curbits += av_log2(c)*2 - 4 + 1; | |
142 | } | ||
143 | } else { | ||
144 | 80651973 | quantized = vec[j]*IQ; | |
145 | } | ||
146 | 82250064 | di = t - quantized; | |
147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82250064 times.
|
82250064 | if (out) |
148 | ✗ | out[i+j] = in[i+j] >= 0 ? quantized : -quantized; | |
149 |
2/2✓ Branch 0 taken 48894607 times.
✓ Branch 1 taken 33355457 times.
|
82250064 | if (vec[j] != 0.0f) |
150 | 48894607 | curbits++; | |
151 | 82250064 | qenergy += quantized*quantized; | |
152 | 82250064 | rd += di*di; | |
153 | } | ||
154 | } else { | ||
155 |
2/2✓ Branch 0 taken 57759704 times.
✓ Branch 1 taken 19958087 times.
|
77717791 | for (int j = 0; j < dim; j++) { |
156 | 57759704 | quantized = vec[j]*IQ; | |
157 | 57759704 | qenergy += quantized*quantized; | |
158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57759704 times.
|
57759704 | if (out) |
159 | ✗ | out[i+j] = quantized; | |
160 | 57759704 | rd += (in[i+j] - quantized)*(in[i+j] - quantized); | |
161 | } | ||
162 | } | ||
163 | 54442178 | cost += rd * lambda + curbits; | |
164 | 54442178 | resbits += curbits; | |
165 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54442178 times.
|
54442178 | if (cost >= uplim) |
166 | ✗ | return uplim; | |
167 |
2/2✓ Branch 0 taken 3138085 times.
✓ Branch 1 taken 51304093 times.
|
54442178 | if (pb) { |
168 | 3138085 | put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); | |
169 |
2/2✓ Branch 0 taken 1676348 times.
✓ Branch 1 taken 1461737 times.
|
3138085 | if (BT_UNSIGNED) |
170 |
2/2✓ Branch 0 taken 4388088 times.
✓ Branch 1 taken 1676348 times.
|
6064436 | for (int j = 0; j < dim; j++) |
171 |
2/2✓ Branch 0 taken 3089271 times.
✓ Branch 1 taken 1298817 times.
|
4388088 | if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) |
172 | 3089271 | put_bits(pb, 1, in[i+j] < 0.0f); | |
173 |
2/2✓ Branch 0 taken 444450 times.
✓ Branch 1 taken 2693635 times.
|
3138085 | if (BT_ESC) { |
174 |
2/2✓ Branch 0 taken 888900 times.
✓ Branch 1 taken 444450 times.
|
1333350 | for (int j = 0; j < 2; j++) { |
175 |
2/2✓ Branch 0 taken 131744 times.
✓ Branch 1 taken 757156 times.
|
888900 | if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { |
176 | 131744 | int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13); | |
177 | 131744 | int len = av_log2(coef); | |
178 | |||
179 | 131744 | put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); | |
180 | 131744 | put_sbits(pb, len, coef); | |
181 | } | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | |||
187 |
2/2✓ Branch 0 taken 6361490 times.
✓ Branch 1 taken 500580 times.
|
6862070 | if (bits) |
188 | 6361490 | *bits = resbits; | |
189 |
2/2✓ Branch 0 taken 3150348 times.
✓ Branch 1 taken 3711722 times.
|
6862070 | if (energy) |
190 | 3150348 | *energy = qenergy; | |
191 | 6862070 | return cost; | |
192 | } | ||
193 | |||
194 | ✗ | static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, | |
195 | const float *in, float *quant, const float *scaled, | ||
196 | int size, int scale_idx, int cb, | ||
197 | const float lambda, const float uplim, | ||
198 | int *bits, float *energy) { | ||
199 | ✗ | av_assert0(0); | |
200 | return 0.0f; | ||
201 | } | ||
202 | |||
203 | #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \ | ||
204 | static float quantize_and_encode_band_cost_ ## NAME( \ | ||
205 | struct AACEncContext *s, \ | ||
206 | PutBitContext *pb, const float *in, float *quant, \ | ||
207 | const float *scaled, int size, int scale_idx, \ | ||
208 | int cb, const float lambda, const float uplim, \ | ||
209 | int *bits, float *energy) { \ | ||
210 | return quantize_and_encode_band_cost_template( \ | ||
211 | s, pb, in, quant, scaled, size, scale_idx, \ | ||
212 | BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \ | ||
213 | BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \ | ||
214 | ROUNDING); \ | ||
215 | } | ||
216 | |||
217 | 1533068 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
218 | 1490390 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
219 | 1192300 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD) | |
220 | 1122489 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD) | |
221 | 2101402 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD) | |
222 | 950393 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD) | |
223 | 5096 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO) | |
224 | 13300 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD) | |
225 | 17350 | QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD) | |
226 | |||
227 | static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[] = | ||
228 | { | ||
229 | quantize_and_encode_band_cost_ZERO, | ||
230 | quantize_and_encode_band_cost_SQUAD, | ||
231 | quantize_and_encode_band_cost_SQUAD, | ||
232 | quantize_and_encode_band_cost_UQUAD, | ||
233 | quantize_and_encode_band_cost_UQUAD, | ||
234 | quantize_and_encode_band_cost_SPAIR, | ||
235 | quantize_and_encode_band_cost_SPAIR, | ||
236 | quantize_and_encode_band_cost_UPAIR, | ||
237 | quantize_and_encode_band_cost_UPAIR, | ||
238 | quantize_and_encode_band_cost_UPAIR, | ||
239 | quantize_and_encode_band_cost_UPAIR, | ||
240 | quantize_and_encode_band_cost_ESC, | ||
241 | quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | ||
242 | quantize_and_encode_band_cost_NOISE, | ||
243 | quantize_and_encode_band_cost_STEREO, | ||
244 | quantize_and_encode_band_cost_STEREO, | ||
245 | }; | ||
246 | |||
247 | static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[] = | ||
248 | { | ||
249 | quantize_and_encode_band_cost_ZERO, | ||
250 | quantize_and_encode_band_cost_SQUAD, | ||
251 | quantize_and_encode_band_cost_SQUAD, | ||
252 | quantize_and_encode_band_cost_UQUAD, | ||
253 | quantize_and_encode_band_cost_UQUAD, | ||
254 | quantize_and_encode_band_cost_SPAIR, | ||
255 | quantize_and_encode_band_cost_SPAIR, | ||
256 | quantize_and_encode_band_cost_UPAIR, | ||
257 | quantize_and_encode_band_cost_UPAIR, | ||
258 | quantize_and_encode_band_cost_UPAIR, | ||
259 | quantize_and_encode_band_cost_UPAIR, | ||
260 | quantize_and_encode_band_cost_ESC_RTZ, | ||
261 | quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | ||
262 | quantize_and_encode_band_cost_NOISE, | ||
263 | quantize_and_encode_band_cost_STEREO, | ||
264 | quantize_and_encode_band_cost_STEREO, | ||
265 | }; | ||
266 | |||
267 | 7981016 | float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb, | |
268 | const float *in, float *quant, const float *scaled, | ||
269 | int size, int scale_idx, int cb, | ||
270 | const float lambda, const float uplim, | ||
271 | int *bits, float *energy) | ||
272 | { | ||
273 | 7981016 | return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size, | |
274 | scale_idx, cb, lambda, uplim, | ||
275 | bits, energy); | ||
276 | } | ||
277 | |||
278 | 444772 | static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, | |
279 | const float *in, float *out, int size, int scale_idx, | ||
280 | int cb, const float lambda, int rtz) | ||
281 | { | ||
282 |
2/2✓ Branch 0 taken 16087 times.
✓ Branch 1 taken 428685 times.
|
444772 | (rtz ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb](s, pb, in, out, NULL, size, scale_idx, cb, |
283 | lambda, INFINITY, NULL, NULL); | ||
284 | 444772 | } | |
285 | |||
286 | /** | ||
287 | * structure used in optimal codebook search | ||
288 | */ | ||
289 | typedef struct BandCodingPath { | ||
290 | int prev_idx; ///< pointer to the previous path point | ||
291 | float cost; ///< path cost | ||
292 | int run; | ||
293 | } BandCodingPath; | ||
294 | |||
295 | typedef struct TrellisPath { | ||
296 | float cost; | ||
297 | int prev; | ||
298 | } TrellisPath; | ||
299 | |||
300 | #define TRELLIS_STAGES 121 | ||
301 | #define TRELLIS_STATES (SCALE_MAX_DIFF+1) | ||
302 | |||
303 | 9399 | static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce) | |
304 | { | ||
305 | int w, g; | ||
306 | 9399 | int prevscaler_n = -255, prevscaler_i = 0; | |
307 | 9399 | int bands = 0; | |
308 | |||
309 |
2/2✓ Branch 0 taken 9978 times.
✓ Branch 1 taken 9399 times.
|
19377 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
310 |
2/2✓ Branch 0 taken 457505 times.
✓ Branch 1 taken 9978 times.
|
467483 | for (g = 0; g < sce->ics.num_swb; g++) { |
311 |
2/2✓ Branch 0 taken 13402 times.
✓ Branch 1 taken 444103 times.
|
457505 | if (sce->zeroes[w*16+g]) |
312 | 13402 | continue; | |
313 |
4/4✓ Branch 0 taken 437777 times.
✓ Branch 1 taken 6326 times.
✓ Branch 2 taken 2238 times.
✓ Branch 3 taken 435539 times.
|
444103 | if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) { |
314 | 8564 | sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100); | |
315 | 8564 | bands++; | |
316 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 429164 times.
|
435539 | } else if (sce->band_type[w*16+g] == NOISE_BT) { |
317 | 6375 | sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155); | |
318 |
2/2✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 5253 times.
|
6375 | if (prevscaler_n == -255) |
319 | 1122 | prevscaler_n = sce->sf_idx[w*16+g]; | |
320 | 6375 | bands++; | |
321 | } | ||
322 | } | ||
323 | } | ||
324 | |||
325 |
2/2✓ Branch 0 taken 7814 times.
✓ Branch 1 taken 1585 times.
|
9399 | if (!bands) |
326 | 7814 | return; | |
327 | |||
328 | /* Clip the scalefactor indices */ | ||
329 |
2/2✓ Branch 0 taken 1652 times.
✓ Branch 1 taken 1585 times.
|
3237 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
330 |
2/2✓ Branch 0 taken 77798 times.
✓ Branch 1 taken 1652 times.
|
79450 | for (g = 0; g < sce->ics.num_swb; g++) { |
331 |
2/2✓ Branch 0 taken 5080 times.
✓ Branch 1 taken 72718 times.
|
77798 | if (sce->zeroes[w*16+g]) |
332 | 5080 | continue; | |
333 |
4/4✓ Branch 0 taken 66392 times.
✓ Branch 1 taken 6326 times.
✓ Branch 2 taken 2238 times.
✓ Branch 3 taken 64154 times.
|
72718 | if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) { |
334 | 8564 | sce->sf_idx[w*16+g] = prevscaler_i = av_clip(sce->sf_idx[w*16+g], prevscaler_i - SCALE_MAX_DIFF, prevscaler_i + SCALE_MAX_DIFF); | |
335 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 57779 times.
|
64154 | } else if (sce->band_type[w*16+g] == NOISE_BT) { |
336 | 6375 | sce->sf_idx[w*16+g] = prevscaler_n = av_clip(sce->sf_idx[w*16+g], prevscaler_n - SCALE_MAX_DIFF, prevscaler_n + SCALE_MAX_DIFF); | |
337 | } | ||
338 | } | ||
339 | } | ||
340 | } | ||
341 | |||
342 | 8660 | static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s, | |
343 | SingleChannelElement *sce, | ||
344 | const float lambda) | ||
345 | { | ||
346 | 8660 | int start = 0, i, w, w2, g; | |
347 | 8660 | int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f); | |
348 | 8660 | float dists[128] = { 0 }, uplims[128] = { 0 }; | |
349 | float maxvals[128]; | ||
350 | int fflag, minscaler; | ||
351 | 8660 | int its = 0; | |
352 | 8660 | int allz = 0; | |
353 | 8660 | float minthr = INFINITY; | |
354 | |||
355 | // for values above this the decoder might end up in an endless loop | ||
356 | // due to always having more bits than what can be encoded. | ||
357 | 8660 | destbits = FFMIN(destbits, 5800); | |
358 | //some heuristic to determine initial quantizers will reduce search time | ||
359 | //determine zero bands and upper limits | ||
360 |
2/2✓ Branch 0 taken 9205 times.
✓ Branch 1 taken 8660 times.
|
17865 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
361 | 9205 | start = 0; | |
362 |
2/2✓ Branch 0 taken 421238 times.
✓ Branch 1 taken 9205 times.
|
430443 | for (g = 0; g < sce->ics.num_swb; g++) { |
363 | 421238 | int nz = 0; | |
364 | 421238 | float uplim = 0.0f; | |
365 |
2/2✓ Branch 0 taken 434384 times.
✓ Branch 1 taken 421238 times.
|
855622 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
366 | 434384 | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
367 | 434384 | uplim += band->threshold; | |
368 |
3/4✓ Branch 0 taken 421345 times.
✓ Branch 1 taken 13039 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 421345 times.
|
434384 | if (band->energy <= band->threshold || band->threshold == 0.0f) { |
369 | 13039 | sce->zeroes[(w+w2)*16+g] = 1; | |
370 | 13039 | continue; | |
371 | } | ||
372 | 421345 | nz = 1; | |
373 | } | ||
374 | 421238 | uplims[w*16+g] = uplim *512; | |
375 | 421238 | sce->band_type[w*16+g] = 0; | |
376 | 421238 | sce->zeroes[w*16+g] = !nz; | |
377 |
2/2✓ Branch 0 taken 413884 times.
✓ Branch 1 taken 7354 times.
|
421238 | if (nz) |
378 |
2/2✓ Branch 0 taken 92607 times.
✓ Branch 1 taken 321277 times.
|
413884 | minthr = FFMIN(minthr, uplim); |
379 | 421238 | allz |= nz; | |
380 | 421238 | start += sce->ics.swb_sizes[g]; | |
381 | } | ||
382 | } | ||
383 |
2/2✓ Branch 0 taken 9205 times.
✓ Branch 1 taken 8660 times.
|
17865 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
384 |
2/2✓ Branch 0 taken 421238 times.
✓ Branch 1 taken 9205 times.
|
430443 | for (g = 0; g < sce->ics.num_swb; g++) { |
385 |
2/2✓ Branch 0 taken 7354 times.
✓ Branch 1 taken 413884 times.
|
421238 | if (sce->zeroes[w*16+g]) { |
386 | 7354 | sce->sf_idx[w*16+g] = SCALE_ONE_POS; | |
387 | 7354 | continue; | |
388 | } | ||
389 |
2/2✓ Branch 0 taken 180134 times.
✓ Branch 1 taken 233750 times.
|
413884 | sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59); |
390 | } | ||
391 | } | ||
392 | |||
393 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 8576 times.
|
8660 | if (!allz) |
394 | 84 | return; | |
395 | 8576 | s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024); | |
396 | 8576 | ff_quantize_band_cost_cache_init(s); | |
397 | |||
398 |
2/2✓ Branch 0 taken 8965 times.
✓ Branch 1 taken 8576 times.
|
17541 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
399 | 8965 | start = w*128; | |
400 |
2/2✓ Branch 0 taken 417668 times.
✓ Branch 1 taken 8965 times.
|
426633 | for (g = 0; g < sce->ics.num_swb; g++) { |
401 | 417668 | const float *scaled = s->scoefs + start; | |
402 | 417668 | maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled); | |
403 | 417668 | start += sce->ics.swb_sizes[g]; | |
404 | } | ||
405 | } | ||
406 | |||
407 | //perform two-loop search | ||
408 | //outer loop - improve quality | ||
409 | do { | ||
410 | int tbits, qstep; | ||
411 | 36546 | minscaler = sce->sf_idx[0]; | |
412 | //inner loop - quantize spectrum to fit into given number of bits | ||
413 |
2/2✓ Branch 0 taken 27970 times.
✓ Branch 1 taken 8576 times.
|
36546 | qstep = its ? 1 : 32; |
414 | do { | ||
415 | 83440 | int prev = -1; | |
416 | 83440 | tbits = 0; | |
417 |
2/2✓ Branch 0 taken 89790 times.
✓ Branch 1 taken 83440 times.
|
173230 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
418 | 89790 | start = w*128; | |
419 |
2/2✓ Branch 0 taken 4050465 times.
✓ Branch 1 taken 89790 times.
|
4140255 | for (g = 0; g < sce->ics.num_swb; g++) { |
420 | 4050465 | const float *coefs = sce->coeffs + start; | |
421 | 4050465 | const float *scaled = s->scoefs + start; | |
422 | 4050465 | int bits = 0; | |
423 | int cb; | ||
424 | 4050465 | float dist = 0.0f; | |
425 | |||
426 |
3/4✓ Branch 0 taken 3994565 times.
✓ Branch 1 taken 55900 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3994565 times.
|
4050465 | if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) { |
427 | 55900 | start += sce->ics.swb_sizes[g]; | |
428 | 55900 | continue; | |
429 | } | ||
430 | 3994565 | minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); | |
431 | 3994565 | cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
432 |
2/2✓ Branch 0 taken 4119445 times.
✓ Branch 1 taken 3994565 times.
|
8114010 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
433 | int b; | ||
434 | 8238890 | dist += quantize_band_cost_cached(s, w + w2, g, | |
435 | 4119445 | coefs + w2*128, | |
436 | 4119445 | scaled + w2*128, | |
437 | 4119445 | sce->ics.swb_sizes[g], | |
438 | 4119445 | sce->sf_idx[w*16+g], | |
439 | cb, 1.0f, INFINITY, | ||
440 | &b, NULL, 0); | ||
441 | 4119445 | bits += b; | |
442 | } | ||
443 | 3994565 | dists[w*16+g] = dist - bits; | |
444 |
2/2✓ Branch 0 taken 3911125 times.
✓ Branch 1 taken 83440 times.
|
3994565 | if (prev != -1) { |
445 | 3911125 | bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO]; | |
446 | } | ||
447 | 3994565 | tbits += bits; | |
448 | 3994565 | start += sce->ics.swb_sizes[g]; | |
449 | 3994565 | prev = sce->sf_idx[w*16+g]; | |
450 | } | ||
451 | } | ||
452 |
2/2✓ Branch 0 taken 16102 times.
✓ Branch 1 taken 67338 times.
|
83440 | if (tbits > destbits) { |
453 |
2/2✓ Branch 0 taken 2061056 times.
✓ Branch 1 taken 16102 times.
|
2077158 | for (i = 0; i < 128; i++) |
454 |
1/2✓ Branch 0 taken 2061056 times.
✗ Branch 1 not taken.
|
2061056 | if (sce->sf_idx[i] < 218 - qstep) |
455 | 2061056 | sce->sf_idx[i] += qstep; | |
456 | } else { | ||
457 |
2/2✓ Branch 0 taken 8619264 times.
✓ Branch 1 taken 67338 times.
|
8686602 | for (i = 0; i < 128; i++) |
458 |
2/2✓ Branch 0 taken 3490494 times.
✓ Branch 1 taken 5128770 times.
|
8619264 | if (sce->sf_idx[i] > 60 - qstep) |
459 | 3490494 | sce->sf_idx[i] -= qstep; | |
460 | } | ||
461 | 83440 | qstep >>= 1; | |
462 |
5/6✓ Branch 0 taken 40560 times.
✓ Branch 1 taken 42880 times.
✓ Branch 2 taken 4014 times.
✓ Branch 3 taken 36546 times.
✓ Branch 4 taken 4014 times.
✗ Branch 5 not taken.
|
83440 | if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217) |
463 | 4014 | qstep = 1; | |
464 |
2/2✓ Branch 0 taken 46894 times.
✓ Branch 1 taken 36546 times.
|
83440 | } while (qstep); |
465 | |||
466 | 36546 | fflag = 0; | |
467 | 36546 | minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF); | |
468 | |||
469 |
2/2✓ Branch 0 taken 39014 times.
✓ Branch 1 taken 36546 times.
|
75560 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
470 |
2/2✓ Branch 0 taken 1762191 times.
✓ Branch 1 taken 39014 times.
|
1801205 | for (g = 0; g < sce->ics.num_swb; g++) { |
471 | 1762191 | int prevsc = sce->sf_idx[w*16+g]; | |
472 |
3/4✓ Branch 0 taken 12632 times.
✓ Branch 1 taken 1749559 times.
✓ Branch 2 taken 12632 times.
✗ Branch 3 not taken.
|
1762191 | if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) { |
473 |
2/2✓ Branch 1 taken 12027 times.
✓ Branch 2 taken 605 times.
|
12632 | if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1)) |
474 | 12027 | sce->sf_idx[w*16+g]--; | |
475 | else //Try to make sure there is some energy in every band | ||
476 | 605 | sce->sf_idx[w*16+g]-=2; | |
477 | } | ||
478 | 1762191 | sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF); | |
479 | 1762191 | sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219); | |
480 |
2/2✓ Branch 0 taken 510306 times.
✓ Branch 1 taken 1251885 times.
|
1762191 | if (sce->sf_idx[w*16+g] != prevsc) |
481 | 510306 | fflag = 1; | |
482 | 1762191 | sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
483 | } | ||
484 | } | ||
485 | 36546 | its++; | |
486 |
4/4✓ Branch 0 taken 30964 times.
✓ Branch 1 taken 5582 times.
✓ Branch 2 taken 27970 times.
✓ Branch 3 taken 2994 times.
|
36546 | } while (fflag && its < 10); |
487 | } | ||
488 | |||
489 | 2003 | static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce) | |
490 | { | ||
491 | FFPsyBand *band; | ||
492 | int w, g, w2, i; | ||
493 | 2003 | int wlen = 1024 / sce->ics.num_windows; | |
494 | int bandwidth, cutoff; | ||
495 | 2003 | float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128]; | |
496 | 2003 | float *NOR34 = &s->scoefs[3*128]; | |
497 | uint8_t nextband[128]; | ||
498 | 2003 | const float lambda = s->lambda; | |
499 | 2003 | const float freq_mult = avctx->sample_rate*0.5f/wlen; | |
500 | 2003 | const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda); | |
501 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2003 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2003 | const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f)); |
502 | 2003 | const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f); | |
503 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f); |
504 | |||
505 | 4006 | int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate | |
506 |
1/2✓ Branch 0 taken 2003 times.
✗ Branch 1 not taken.
|
2003 | / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels) |
507 | 2003 | * (lambda / 120.f); | |
508 | |||
509 | /** Keep this in sync with twoloop's cutoff selection */ | ||
510 | 2003 | float rate_bandwidth_multiplier = 1.5f; | |
511 | 2003 | int prev = -1000, prev_sf = -1; | |
512 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE) |
513 | ✗ | ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024) | |
514 | 2003 | : (avctx->bit_rate / avctx->ch_layout.nb_channels); | |
515 | |||
516 | 2003 | frame_bit_rate *= 1.15f; | |
517 | |||
518 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 1171 times.
|
2003 | if (avctx->cutoff > 0) { |
519 | 832 | bandwidth = avctx->cutoff; | |
520 | } else { | ||
521 |
5/10✓ Branch 0 taken 1171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1171 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1171 times.
✓ Branch 6 taken 1171 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1171 times.
|
1171 | bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate)); |
522 | } | ||
523 | |||
524 | 2003 | cutoff = bandwidth * 2 * wlen / avctx->sample_rate; | |
525 | |||
526 | 2003 | memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type)); | |
527 | 2003 | ff_init_nextband_map(sce, nextband); | |
528 |
2/2✓ Branch 0 taken 2131 times.
✓ Branch 1 taken 2003 times.
|
4134 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
529 | 2131 | int wstart = w*128; | |
530 |
2/2✓ Branch 0 taken 94947 times.
✓ Branch 1 taken 2131 times.
|
97078 | for (g = 0; g < sce->ics.num_swb; g++) { |
531 | int noise_sfi; | ||
532 | 94947 | float dist1 = 0.0f, dist2 = 0.0f, noise_amp; | |
533 | 94947 | float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh; | |
534 | 94947 | float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f; | |
535 | 94947 | float min_energy = -1.0f, max_energy = 0.0f; | |
536 | 94947 | const int start = wstart+sce->ics.swb_offset[g]; | |
537 | 94947 | const float freq = (start-wstart)*freq_mult; | |
538 |
2/2✓ Branch 0 taken 46953 times.
✓ Branch 1 taken 47994 times.
|
94947 | const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f); |
539 |
4/4✓ Branch 0 taken 48836 times.
✓ Branch 1 taken 46111 times.
✓ Branch 2 taken 6599 times.
✓ Branch 3 taken 42237 times.
|
94947 | if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) { |
540 |
2/2✓ Branch 0 taken 43241 times.
✓ Branch 1 taken 9469 times.
|
52710 | if (!sce->zeroes[w*16+g]) |
541 | 43241 | prev_sf = sce->sf_idx[w*16+g]; | |
542 | 52710 | continue; | |
543 | } | ||
544 |
2/2✓ Branch 0 taken 43753 times.
✓ Branch 1 taken 42237 times.
|
85990 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
545 | 43753 | band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
546 | 43753 | sfb_energy += band->energy; | |
547 |
2/2✓ Branch 0 taken 42567 times.
✓ Branch 1 taken 1186 times.
|
43753 | spread = FFMIN(spread, band->spread); |
548 | 43753 | threshold += band->threshold; | |
549 |
2/2✓ Branch 0 taken 42237 times.
✓ Branch 1 taken 1516 times.
|
43753 | if (!w2) { |
550 | 42237 | min_energy = max_energy = band->energy; | |
551 | } else { | ||
552 |
2/2✓ Branch 0 taken 526 times.
✓ Branch 1 taken 990 times.
|
1516 | min_energy = FFMIN(min_energy, band->energy); |
553 |
2/2✓ Branch 0 taken 676 times.
✓ Branch 1 taken 840 times.
|
1516 | max_energy = FFMAX(max_energy, band->energy); |
554 | } | ||
555 | } | ||
556 | |||
557 | /* Ramps down at ~8000Hz and loosens the dist threshold */ | ||
558 | 42237 | dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias; | |
559 | |||
560 | /* PNS is acceptable when all of these are true: | ||
561 | * 1. high spread energy (noise-like band) | ||
562 | * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed) | ||
563 | * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS) | ||
564 | * | ||
565 | * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important) | ||
566 | */ | ||
567 |
4/4✓ Branch 0 taken 39218 times.
✓ Branch 1 taken 3019 times.
✓ Branch 3 taken 39192 times.
✓ Branch 4 taken 26 times.
|
42237 | if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) || |
568 |
8/8✓ Branch 0 taken 39192 times.
✓ Branch 1 taken 3019 times.
✓ Branch 2 taken 4276 times.
✓ Branch 3 taken 34916 times.
✓ Branch 4 taken 6471 times.
✓ Branch 5 taken 824 times.
✓ Branch 6 taken 40906 times.
✓ Branch 7 taken 481 times.
|
42211 | ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold || |
569 |
6/6✓ Branch 0 taken 38737 times.
✓ Branch 1 taken 2169 times.
✓ Branch 2 taken 34462 times.
✓ Branch 3 taken 4275 times.
✓ Branch 4 taken 21257 times.
✓ Branch 5 taken 13205 times.
|
40906 | (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) || |
570 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 27631 times.
|
27701 | min_energy < pns_transient_energy_r * max_energy ) { |
571 | 14606 | sce->pns_ener[w*16+g] = sfb_energy; | |
572 |
2/2✓ Branch 0 taken 13756 times.
✓ Branch 1 taken 850 times.
|
14606 | if (!sce->zeroes[w*16+g]) |
573 | 13756 | prev_sf = sce->sf_idx[w*16+g]; | |
574 | 14606 | continue; | |
575 | } | ||
576 | |||
577 |
2/2✓ Branch 0 taken 26697 times.
✓ Branch 1 taken 934 times.
|
27631 | pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread); |
578 | 27631 | noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */ | |
579 | 27631 | noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */ | |
580 |
2/2✓ Branch 0 taken 14932 times.
✓ Branch 1 taken 12699 times.
|
27631 | if (prev != -1000) { |
581 | 14932 | int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO; | |
582 |
2/4✓ Branch 0 taken 14932 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14932 times.
|
14932 | if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) { |
583 | ✗ | if (!sce->zeroes[w*16+g]) | |
584 | ✗ | prev_sf = sce->sf_idx[w*16+g]; | |
585 | ✗ | continue; | |
586 | } | ||
587 | } | ||
588 |
2/2✓ Branch 0 taken 27666 times.
✓ Branch 1 taken 27631 times.
|
55297 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
589 | float band_energy, scale, pns_senergy; | ||
590 | 27666 | const int start_c = (w+w2)*128+sce->ics.swb_offset[g]; | |
591 | 27666 | band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
592 |
2/2✓ Branch 0 taken 904188 times.
✓ Branch 1 taken 27666 times.
|
931854 | for (i = 0; i < sce->ics.swb_sizes[g]; i++) { |
593 | 904188 | s->random_state = lcg_random(s->random_state); | |
594 | 904188 | PNS[i] = s->random_state; | |
595 | } | ||
596 | 27666 | band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]); | |
597 | 27666 | scale = noise_amp/sqrtf(band_energy); | |
598 | 27666 | s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]); | |
599 | 27666 | pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]); | |
600 | 27666 | pns_energy += pns_senergy; | |
601 | 27666 | s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]); | |
602 | 27666 | s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]); | |
603 | 55332 | dist1 += quantize_band_cost(s, &sce->coeffs[start_c], | |
604 | NOR34, | ||
605 | 27666 | sce->ics.swb_sizes[g], | |
606 | 27666 | sce->sf_idx[(w+w2)*16+g], | |
607 | 27666 | sce->band_alt[(w+w2)*16+g], | |
608 | 27666 | lambda/band->threshold, INFINITY, NULL, NULL); | |
609 | /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */ | ||
610 | 27666 | dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold; | |
611 | } | ||
612 |
3/4✓ Branch 0 taken 27631 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5590 times.
✓ Branch 3 taken 22041 times.
|
27631 | if (g && sce->band_type[w*16+g-1] == NOISE_BT) { |
613 | 5590 | dist2 += 5; | |
614 | } else { | ||
615 | 22041 | dist2 += 9; | |
616 | } | ||
617 | 27631 | energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */ | |
618 | 27631 | sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy; | |
619 |
8/10✓ Branch 0 taken 25462 times.
✓ Branch 1 taken 2169 times.
✓ Branch 2 taken 21256 times.
✓ Branch 3 taken 4206 times.
✓ Branch 4 taken 20609 times.
✓ Branch 5 taken 647 times.
✓ Branch 6 taken 20609 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 20609 times.
|
27631 | if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) { |
620 | 6375 | sce->band_type[w*16+g] = NOISE_BT; | |
621 | 6375 | sce->zeroes[w*16+g] = 0; | |
622 | 6375 | prev = noise_sfi; | |
623 | } else { | ||
624 |
1/2✓ Branch 0 taken 21256 times.
✗ Branch 1 not taken.
|
21256 | if (!sce->zeroes[w*16+g]) |
625 | 21256 | prev_sf = sce->sf_idx[w*16+g]; | |
626 | } | ||
627 | } | ||
628 | } | ||
629 | 2003 | } | |
630 | |||
631 | 2003 | static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce) | |
632 | { | ||
633 | FFPsyBand *band; | ||
634 | int w, g, w2; | ||
635 | 2003 | int wlen = 1024 / sce->ics.num_windows; | |
636 | int bandwidth, cutoff; | ||
637 | 2003 | const float lambda = s->lambda; | |
638 | 2003 | const float freq_mult = avctx->sample_rate*0.5f/wlen; | |
639 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2003 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2003 | const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f)); |
640 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f); |
641 | |||
642 | 4006 | int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate | |
643 |
1/2✓ Branch 0 taken 2003 times.
✗ Branch 1 not taken.
|
2003 | / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels) |
644 | 2003 | * (lambda / 120.f); | |
645 | |||
646 | /** Keep this in sync with twoloop's cutoff selection */ | ||
647 | 2003 | float rate_bandwidth_multiplier = 1.5f; | |
648 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2003 times.
|
2003 | int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE) |
649 | ✗ | ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024) | |
650 | 2003 | : (avctx->bit_rate / avctx->ch_layout.nb_channels); | |
651 | |||
652 | 2003 | frame_bit_rate *= 1.15f; | |
653 | |||
654 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 1171 times.
|
2003 | if (avctx->cutoff > 0) { |
655 | 832 | bandwidth = avctx->cutoff; | |
656 | } else { | ||
657 |
5/10✓ Branch 0 taken 1171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1171 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1171 times.
✓ Branch 6 taken 1171 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1171 times.
|
1171 | bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate)); |
658 | } | ||
659 | |||
660 | 2003 | cutoff = bandwidth * 2 * wlen / avctx->sample_rate; | |
661 | |||
662 | 2003 | memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type)); | |
663 |
2/2✓ Branch 0 taken 2131 times.
✓ Branch 1 taken 2003 times.
|
4134 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
664 |
2/2✓ Branch 0 taken 94947 times.
✓ Branch 1 taken 2131 times.
|
97078 | for (g = 0; g < sce->ics.num_swb; g++) { |
665 | 94947 | float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f; | |
666 | 94947 | float min_energy = -1.0f, max_energy = 0.0f; | |
667 | 94947 | const int start = sce->ics.swb_offset[g]; | |
668 | 94947 | const float freq = start*freq_mult; | |
669 |
2/2✓ Branch 0 taken 46953 times.
✓ Branch 1 taken 47994 times.
|
94947 | const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f); |
670 |
4/4✓ Branch 0 taken 48836 times.
✓ Branch 1 taken 46111 times.
✓ Branch 2 taken 6599 times.
✓ Branch 3 taken 42237 times.
|
94947 | if (freq < NOISE_LOW_LIMIT || start >= cutoff) { |
671 | 52710 | sce->can_pns[w*16+g] = 0; | |
672 | 52710 | continue; | |
673 | } | ||
674 |
2/2✓ Branch 0 taken 43753 times.
✓ Branch 1 taken 42237 times.
|
85990 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
675 | 43753 | band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
676 | 43753 | sfb_energy += band->energy; | |
677 |
2/2✓ Branch 0 taken 42567 times.
✓ Branch 1 taken 1186 times.
|
43753 | spread = FFMIN(spread, band->spread); |
678 | 43753 | threshold += band->threshold; | |
679 |
2/2✓ Branch 0 taken 42237 times.
✓ Branch 1 taken 1516 times.
|
43753 | if (!w2) { |
680 | 42237 | min_energy = max_energy = band->energy; | |
681 | } else { | ||
682 |
2/2✓ Branch 0 taken 526 times.
✓ Branch 1 taken 990 times.
|
1516 | min_energy = FFMIN(min_energy, band->energy); |
683 |
2/2✓ Branch 0 taken 676 times.
✓ Branch 1 taken 840 times.
|
1516 | max_energy = FFMAX(max_energy, band->energy); |
684 | } | ||
685 | } | ||
686 | |||
687 | /* PNS is acceptable when all of these are true: | ||
688 | * 1. high spread energy (noise-like band) | ||
689 | * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed) | ||
690 | * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS) | ||
691 | */ | ||
692 | 42237 | sce->pns_ener[w*16+g] = sfb_energy; | |
693 |
6/6✓ Branch 0 taken 41403 times.
✓ Branch 1 taken 834 times.
✓ Branch 2 taken 40932 times.
✓ Branch 3 taken 471 times.
✓ Branch 4 taken 411 times.
✓ Branch 5 taken 40521 times.
|
42237 | if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) { |
694 | 1716 | sce->can_pns[w*16+g] = 0; | |
695 | } else { | ||
696 | 40521 | sce->can_pns[w*16+g] = 1; | |
697 | } | ||
698 | } | ||
699 | } | ||
700 | 2003 | } | |
701 | |||
702 | 630 | static void search_for_ms(AACEncContext *s, ChannelElement *cpe) | |
703 | { | ||
704 | 630 | int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side; | |
705 | uint8_t nextband0[128], nextband1[128]; | ||
706 | 630 | float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1; | |
707 | 630 | float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3; | |
708 | 630 | float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5; | |
709 | 630 | const float lambda = s->lambda; | |
710 |
2/2✓ Branch 0 taken 95 times.
✓ Branch 1 taken 535 times.
|
630 | const float mslambda = FFMIN(1.0f, lambda / 120.f); |
711 | 630 | SingleChannelElement *sce0 = &cpe->ch[0]; | |
712 | 630 | SingleChannelElement *sce1 = &cpe->ch[1]; | |
713 |
2/2✓ Branch 0 taken 521 times.
✓ Branch 1 taken 109 times.
|
630 | if (!cpe->common_window) |
714 | 521 | return; | |
715 | |||
716 | /** Scout out next nonzero bands */ | ||
717 | 109 | ff_init_nextband_map(sce0, nextband0); | |
718 | 109 | ff_init_nextband_map(sce1, nextband1); | |
719 | |||
720 | 109 | prev_mid = sce0->sf_idx[0]; | |
721 | 109 | prev_side = sce1->sf_idx[0]; | |
722 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 109 times.
|
232 | for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) { |
723 | 123 | start = 0; | |
724 |
2/2✓ Branch 0 taken 5362 times.
✓ Branch 1 taken 123 times.
|
5485 | for (g = 0; g < sce0->ics.num_swb; g++) { |
725 | 5362 | float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f; | |
726 |
2/2✓ Branch 0 taken 4904 times.
✓ Branch 1 taken 458 times.
|
5362 | if (!cpe->is_mask[w*16+g]) |
727 | 4904 | cpe->ms_mask[w*16+g] = 0; | |
728 |
5/6✓ Branch 0 taken 3641 times.
✓ Branch 1 taken 1721 times.
✓ Branch 2 taken 3641 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3183 times.
✓ Branch 5 taken 458 times.
|
5362 | if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) { |
729 | 3183 | float Mmax = 0.0f, Smax = 0.0f; | |
730 | |||
731 | /* Must compute mid/side SF and book for the whole window group */ | ||
732 |
2/2✓ Branch 0 taken 3395 times.
✓ Branch 1 taken 3183 times.
|
6578 | for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { |
733 |
2/2✓ Branch 0 taken 66844 times.
✓ Branch 1 taken 3395 times.
|
70239 | for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { |
734 | 66844 | M[i] = (sce0->coeffs[start+(w+w2)*128+i] | |
735 | 66844 | + sce1->coeffs[start+(w+w2)*128+i]) * 0.5; | |
736 | 66844 | S[i] = M[i] | |
737 | 66844 | - sce1->coeffs[start+(w+w2)*128+i]; | |
738 | } | ||
739 | 3395 | s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]); | |
740 | 3395 | s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]); | |
741 |
2/2✓ Branch 0 taken 66844 times.
✓ Branch 1 taken 3395 times.
|
70239 | for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) { |
742 |
2/2✓ Branch 0 taken 50326 times.
✓ Branch 1 taken 16518 times.
|
66844 | Mmax = FFMAX(Mmax, M34[i]); |
743 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66844 times.
|
66844 | Smax = FFMAX(Smax, S34[i]); |
744 | } | ||
745 | } | ||
746 | |||
747 |
2/2✓ Branch 0 taken 3294 times.
✓ Branch 1 taken 37 times.
|
3331 | for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) { |
748 | 3294 | float dist1 = 0.0f, dist2 = 0.0f; | |
749 | 3294 | int B0 = 0, B1 = 0; | |
750 | int minidx; | ||
751 | int mididx, sididx; | ||
752 | int midcb, sidcb; | ||
753 | |||
754 | 3294 | minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]); | |
755 | 3294 | mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512); | |
756 | 3294 | sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512); | |
757 |
4/4✓ Branch 0 taken 3063 times.
✓ Branch 1 taken 231 times.
✓ Branch 2 taken 3035 times.
✓ Branch 3 taken 28 times.
|
3294 | if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT |
758 |
1/2✓ Branch 1 taken 3035 times.
✗ Branch 2 not taken.
|
3035 | && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g) |
759 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3035 times.
|
3035 | || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) { |
760 | /* scalefactor range violation, bad stuff, will decrease quality unacceptably */ | ||
761 | ✗ | continue; | |
762 | } | ||
763 | |||
764 | 3294 | midcb = find_min_book(Mmax, mididx); | |
765 | 3294 | sidcb = find_min_book(Smax, sididx); | |
766 | |||
767 | /* No CB can be zero */ | ||
768 | 3294 | midcb = FFMAX(1,midcb); | |
769 | 3294 | sidcb = FFMAX(1,sidcb); | |
770 | |||
771 |
2/2✓ Branch 0 taken 3506 times.
✓ Branch 1 taken 3294 times.
|
6800 | for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) { |
772 | 3506 | FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g]; | |
773 | 3506 | FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g]; | |
774 |
2/2✓ Branch 0 taken 1433 times.
✓ Branch 1 taken 2073 times.
|
3506 | float minthr = FFMIN(band0->threshold, band1->threshold); |
775 | int b1,b2,b3,b4; | ||
776 |
2/2✓ Branch 0 taken 67732 times.
✓ Branch 1 taken 3506 times.
|
71238 | for (i = 0; i < sce0->ics.swb_sizes[g]; i++) { |
777 | 67732 | M[i] = (sce0->coeffs[start+(w+w2)*128+i] | |
778 | 67732 | + sce1->coeffs[start+(w+w2)*128+i]) * 0.5; | |
779 | 67732 | S[i] = M[i] | |
780 | 67732 | - sce1->coeffs[start+(w+w2)*128+i]; | |
781 | } | ||
782 | |||
783 | 3506 | s->aacdsp.abs_pow34(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]); | |
784 | 3506 | s->aacdsp.abs_pow34(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]); | |
785 | 3506 | s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]); | |
786 | 3506 | s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]); | |
787 | 7012 | dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128], | |
788 | L34, | ||
789 | 3506 | sce0->ics.swb_sizes[g], | |
790 | 3506 | sce0->sf_idx[w*16+g], | |
791 | 3506 | sce0->band_type[w*16+g], | |
792 | 3506 | lambda / (band0->threshold + FLT_MIN), INFINITY, &b1, NULL); | |
793 | 7012 | dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128], | |
794 | R34, | ||
795 | 3506 | sce1->ics.swb_sizes[g], | |
796 | 3506 | sce1->sf_idx[w*16+g], | |
797 | 3506 | sce1->band_type[w*16+g], | |
798 | 3506 | lambda / (band1->threshold + FLT_MIN), INFINITY, &b2, NULL); | |
799 | 7012 | dist2 += quantize_band_cost(s, M, | |
800 | M34, | ||
801 | 3506 | sce0->ics.swb_sizes[g], | |
802 | mididx, | ||
803 | midcb, | ||
804 | 3506 | lambda / (minthr + FLT_MIN), INFINITY, &b3, NULL); | |
805 | 7012 | dist2 += quantize_band_cost(s, S, | |
806 | S34, | ||
807 | 3506 | sce1->ics.swb_sizes[g], | |
808 | sididx, | ||
809 | sidcb, | ||
810 | 3506 | mslambda / (minthr * bmax + FLT_MIN), INFINITY, &b4, NULL); | |
811 | 3506 | B0 += b1+b2; | |
812 | 3506 | B1 += b3+b4; | |
813 | 3506 | dist1 -= b1+b2; | |
814 | 3506 | dist2 -= b3+b4; | |
815 | } | ||
816 |
3/4✓ Branch 0 taken 3294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2887 times.
✓ Branch 3 taken 407 times.
|
3294 | cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0; |
817 |
2/2✓ Branch 0 taken 2887 times.
✓ Branch 1 taken 407 times.
|
3294 | if (cpe->ms_mask[w*16+g]) { |
818 |
2/4✓ Branch 0 taken 2887 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2887 times.
✗ Branch 3 not taken.
|
2887 | if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) { |
819 | 2887 | sce0->sf_idx[w*16+g] = mididx; | |
820 | 2887 | sce1->sf_idx[w*16+g] = sididx; | |
821 | 2887 | sce0->band_type[w*16+g] = midcb; | |
822 | 2887 | sce1->band_type[w*16+g] = sidcb; | |
823 | ✗ | } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) { | |
824 | /* ms_mask unneeded, and it confuses some decoders */ | ||
825 | ✗ | cpe->ms_mask[w*16+g] = 0; | |
826 | } | ||
827 | 2887 | break; | |
828 |
2/2✓ Branch 0 taken 259 times.
✓ Branch 1 taken 148 times.
|
407 | } else if (B1 > B0) { |
829 | /* More boost won't fix this */ | ||
830 | 259 | break; | |
831 | } | ||
832 | } | ||
833 | } | ||
834 |
4/4✓ Branch 0 taken 3641 times.
✓ Branch 1 taken 1721 times.
✓ Branch 2 taken 3410 times.
✓ Branch 3 taken 231 times.
|
5362 | if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT) |
835 | 3410 | prev_mid = sce0->sf_idx[w*16+g]; | |
836 |
6/6✓ Branch 0 taken 3641 times.
✓ Branch 1 taken 1721 times.
✓ Branch 2 taken 3183 times.
✓ Branch 3 taken 458 times.
✓ Branch 4 taken 2950 times.
✓ Branch 5 taken 233 times.
|
5362 | if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT) |
837 | 2950 | prev_side = sce1->sf_idx[w*16+g]; | |
838 | 5362 | start += sce0->ics.swb_sizes[g]; | |
839 | } | ||
840 | } | ||
841 | } | ||
842 | |||
843 | const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = { | ||
844 | [AAC_CODER_TWOLOOP] = { | ||
845 | search_for_quantizers_twoloop, | ||
846 | codebook_trellis_rate, | ||
847 | quantize_and_encode_band, | ||
848 | ff_aac_encode_tns_info, | ||
849 | ff_aac_apply_tns, | ||
850 | set_special_band_scalefactors, | ||
851 | search_for_pns, | ||
852 | mark_pns, | ||
853 | ff_aac_search_for_tns, | ||
854 | search_for_ms, | ||
855 | ff_aac_search_for_is, | ||
856 | }, | ||
857 | [AAC_CODER_FAST] = { | ||
858 | search_for_quantizers_fast, | ||
859 | codebook_trellis_rate, | ||
860 | quantize_and_encode_band, | ||
861 | ff_aac_encode_tns_info, | ||
862 | ff_aac_apply_tns, | ||
863 | set_special_band_scalefactors, | ||
864 | search_for_pns, | ||
865 | mark_pns, | ||
866 | ff_aac_search_for_tns, | ||
867 | search_for_ms, | ||
868 | ff_aac_search_for_is, | ||
869 | }, | ||
870 | }; | ||
871 |