../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
353 / 451
78.3%
Branches:
280 / 368
76.1%
Functions:
18 / 19
94.7%

libavcodec/aaccoder.c
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 <math.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 #include "libavcodec/aaccoder_nmr.h"
63
64 /* c * cbrtf(c), the reconstruction of every escape magnitude c */
65 static float esc_pow43[1 << 13];
66
67 31 av_cold void ff_aac_coder_init_tables(void)
68 {
69
2/2
✓ Branch 4 → 3 taken 253952 times.
✓ Branch 4 → 5 taken 31 times.
253983 for (int c = 0; c < FF_ARRAY_ELEMS(esc_pow43); c++)
70 253952 esc_pow43[c] = c * cbrtf(c);
71 31 }
72
73 typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb,
74 const float *in, float *quant, const float *scaled,
75 int size, int scale_idx, int cb,
76 const float lambda, const float uplim,
77 int *bits, float *energy);
78
79 /**
80 * Calculate rate distortion cost for quantizing with given codebook
81 *
82 * @return quantization distortion
83 */
84 11820239 static av_always_inline float quantize_and_encode_band_cost_template(
85 struct AACEncContext *s,
86 PutBitContext *pb, const float *in, float *out,
87 const float *scaled, int size, int scale_idx,
88 int cb, const float lambda, const float uplim,
89 int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED,
90 int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
91 const float ROUNDING)
92 {
93 11820239 const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
94 11820239 const float Q = ff_aac_pow2sf_tab [q_idx];
95 11820239 const float Q34 = ff_aac_pow34sf_tab[q_idx];
96 11820239 const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
97 11820239 const float CLIPPED_ESCAPE = 165140.0f*IQ;
98 11820239 float cost = 0;
99 11820239 float qenergy = 0;
100
2/2
✓ Branch 2 → 3 taken 6818714 times.
✓ Branch 2 → 4 taken 5001525 times.
11820239 const int dim = BT_PAIR ? 2 : 4;
101 11820239 int resbits = 0;
102 int off;
103
104
6/6
✓ Branch 5 → 6 taken 10625439 times.
✓ Branch 5 → 8 taken 1194800 times.
✓ Branch 6 → 7 taken 10617965 times.
✓ Branch 6 → 8 taken 7474 times.
✓ Branch 7 → 8 taken 47228 times.
✓ Branch 7 → 23 taken 10570737 times.
11820239 if (BT_ZERO || BT_NOISE || BT_STEREO) {
105
2/2
✓ Branch 10 → 9 taken 25782464 times.
✓ Branch 10 → 11 taken 1249502 times.
27031966 for (int i = 0; i < size; i++)
106 25782464 cost += in[i]*in[i];
107
2/2
✓ Branch 11 → 12 taken 1216571 times.
✓ Branch 11 → 13 taken 32931 times.
1249502 if (bits)
108 1216571 *bits = 0;
109
2/2
✓ Branch 13 → 14 taken 1177153 times.
✓ Branch 13 → 15 taken 72349 times.
1249502 if (energy)
110 1177153 *energy = qenergy;
111
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 22 taken 1249502 times.
1249502 if (out) {
112
0/2
✗ Branch 21 → 17 not taken.
✗ Branch 21 → 22 not taken.
✗ for (int i = 0; i < size; i += dim)
113
0/2
✗ Branch 19 → 18 not taken.
✗ Branch 19 → 20 not taken.
✗ for (int j = 0; j < dim; j++)
114 ✗ out[i+j] = 0.0f;
115 }
116 1249502 return cost * lambda;
117 }
118
2/2
✓ Branch 23 → 24 taken 519135 times.
✓ Branch 23 → 26 taken 10051602 times.
10570737 if (!scaled) {
119 519135 s->aacdsp.abs_pow34(s->scoefs, in, size);
120 519135 scaled = s->scoefs;
121 }
122 10570737 s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
123
2/2
✓ Branch 27 → 28 taken 6889301 times.
✓ Branch 27 → 29 taken 3681436 times.
10570737 if (BT_UNSIGNED) {
124 6889301 off = 0;
125 } else {
126 3681436 off = aac_cb_maxval[cb];
127 }
128
2/2
✓ Branch 77 → 31 taken 77599401 times.
✓ Branch 77 → 78 taken 10570737 times.
88170138 for (int i = 0; i < size; i += dim) {
129 const float *vec;
130 77599401 int *quants = s->qcoefs + i;
131 77599401 int curidx = 0;
132 int curbits;
133 77599401 float quantized, rd = 0.0f;
134
2/2
✓ Branch 33 → 32 taken 194211308 times.
✓ Branch 33 → 34 taken 77599401 times.
271810709 for (int j = 0; j < dim; j++) {
135 194211308 curidx *= aac_cb_range[cb];
136 194211308 curidx += quants[j] + off;
137 }
138 77599401 curbits = ff_aac_spectral_bits[cb-1][curidx];
139 77599401 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
140
2/2
✓ Branch 34 → 35 taken 51876668 times.
✓ Branch 34 → 53 taken 25722733 times.
77599401 if (BT_UNSIGNED) {
141
2/2
✓ Branch 52 → 36 taken 120287940 times.
✓ Branch 52 → 58 taken 51876668 times.
172164608 for (int j = 0; j < dim; j++) {
142 120287940 float t = fabsf(in[i+j]);
143 float di;
144
4/4
✓ Branch 36 → 37 taken 41398368 times.
✓ Branch 36 → 43 taken 78889572 times.
✓ Branch 37 → 38 taken 20200634 times.
✓ Branch 37 → 43 taken 21197734 times.
120287940 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
145
2/2
✓ Branch 38 → 39 taken 2094538 times.
✓ Branch 38 → 40 taken 18106096 times.
20200634 if (t >= CLIPPED_ESCAPE) {
146 2094538 quantized = CLIPPED_ESCAPE;
147 2094538 curbits += 21;
148 } else {
149 18106096 int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
150 18106096 quantized = esc_pow43[c]*IQ;
151 18106096 curbits += av_log2(c)*2 - 4 + 1;
152 }
153 } else {
154 100087306 quantized = vec[j]*IQ;
155 }
156 120287940 di = t - quantized;
157
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 49 taken 120287940 times.
120287940 if (out)
158
0/2
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 47 not taken.
✗ out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
159
2/2
✓ Branch 49 → 50 taken 81937967 times.
✓ Branch 49 → 51 taken 38349973 times.
120287940 if (vec[j] != 0.0f)
160 81937967 curbits++;
161 120287940 qenergy += quantized*quantized;
162 120287940 rd += di*di;
163 }
164 } else {
165
2/2
✓ Branch 57 → 54 taken 73923368 times.
✓ Branch 57 → 58 taken 25722733 times.
99646101 for (int j = 0; j < dim; j++) {
166 73923368 quantized = vec[j]*IQ;
167 73923368 qenergy += quantized*quantized;
168
1/2
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 73923368 times.
73923368 if (out)
169 ✗ out[i+j] = quantized;
170 73923368 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
171 }
172 }
173 77599401 cost += rd * lambda + curbits;
174 77599401 resbits += curbits;
175
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 77599401 times.
77599401 if (cost >= uplim)
176 ✗ return uplim;
177
2/2
✓ Branch 60 → 61 taken 3520320 times.
✓ Branch 60 → 76 taken 74079081 times.
77599401 if (pb) {
178 3520320 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
179
2/2
✓ Branch 62 → 63 taken 1780858 times.
✓ Branch 62 → 68 taken 1739462 times.
3520320 if (BT_UNSIGNED)
180
2/2
✓ Branch 67 → 64 taken 4874532 times.
✓ Branch 67 → 68 taken 1780858 times.
6655390 for (int j = 0; j < dim; j++)
181
2/2
✓ Branch 64 → 65 taken 3335821 times.
✓ Branch 64 → 66 taken 1538711 times.
4874532 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
182 3335821 put_bits(pb, 1, in[i+j] < 0.0f);
183
2/2
✓ Branch 68 → 69 taken 682850 times.
✓ Branch 68 → 76 taken 2837470 times.
3520320 if (BT_ESC) {
184
2/2
✓ Branch 75 → 70 taken 1365700 times.
✓ Branch 75 → 76 taken 682850 times.
2048550 for (int j = 0; j < 2; j++) {
185
2/2
✓ Branch 70 → 71 taken 198891 times.
✓ Branch 70 → 74 taken 1166809 times.
1365700 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
186 198891 int coef = av_clip(quant(fabsf(in[i+j]), Q, ROUNDING), 16, (1 << 13) - 1);
187 198891 int len = av_log2(coef);
188
189 198891 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
190 198891 put_sbits(pb, len, coef);
191 }
192 }
193 }
194 }
195 }
196
197
2/2
✓ Branch 78 → 79 taken 9983532 times.
✓ Branch 78 → 80 taken 587205 times.
10570737 if (bits)
198 9983532 *bits = resbits;
199
2/2
✓ Branch 80 → 81 taken 6042555 times.
✓ Branch 80 → 82 taken 4528182 times.
10570737 if (energy)
200 6042555 *energy = qenergy;
201 10570737 return cost;
202 }
203
204 ✗ static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
205 const float *in, float *quant, const float *scaled,
206 int size, int scale_idx, int cb,
207 const float lambda, const float uplim,
208 int *bits, float *energy) {
209 ✗ av_assert0(0);
210 return 0.0f;
211 }
212
213 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
214 static float quantize_and_encode_band_cost_ ## NAME( \
215 struct AACEncContext *s, \
216 PutBitContext *pb, const float *in, float *quant, \
217 const float *scaled, int size, int scale_idx, \
218 int cb, const float lambda, const float uplim, \
219 int *bits, float *energy) { \
220 return quantize_and_encode_band_cost_template( \
221 s, pb, in, quant, scaled, size, scale_idx, \
222 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, energy, \
223 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
224 ROUNDING); \
225 }
226
227 1194800 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
228 2155756 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
229 1596267 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
230 1525680 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
231 2517286 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
232 2773097 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
233 2651 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
234 7474 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
235 47228 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
236
237 static const quantize_and_encode_band_func quantize_and_encode_band_cost_arr[] =
238 {
239 quantize_and_encode_band_cost_ZERO,
240 quantize_and_encode_band_cost_SQUAD,
241 quantize_and_encode_band_cost_SQUAD,
242 quantize_and_encode_band_cost_UQUAD,
243 quantize_and_encode_band_cost_UQUAD,
244 quantize_and_encode_band_cost_SPAIR,
245 quantize_and_encode_band_cost_SPAIR,
246 quantize_and_encode_band_cost_UPAIR,
247 quantize_and_encode_band_cost_UPAIR,
248 quantize_and_encode_band_cost_UPAIR,
249 quantize_and_encode_band_cost_UPAIR,
250 quantize_and_encode_band_cost_ESC,
251 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
252 quantize_and_encode_band_cost_NOISE,
253 quantize_and_encode_band_cost_STEREO,
254 quantize_and_encode_band_cost_STEREO,
255 };
256
257 static const quantize_and_encode_band_func quantize_and_encode_band_cost_rtz_arr[] =
258 {
259 quantize_and_encode_band_cost_ZERO,
260 quantize_and_encode_band_cost_SQUAD,
261 quantize_and_encode_band_cost_SQUAD,
262 quantize_and_encode_band_cost_UQUAD,
263 quantize_and_encode_band_cost_UQUAD,
264 quantize_and_encode_band_cost_SPAIR,
265 quantize_and_encode_band_cost_SPAIR,
266 quantize_and_encode_band_cost_UPAIR,
267 quantize_and_encode_band_cost_UPAIR,
268 quantize_and_encode_band_cost_UPAIR,
269 quantize_and_encode_band_cost_UPAIR,
270 quantize_and_encode_band_cost_ESC_RTZ,
271 quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */
272 quantize_and_encode_band_cost_NOISE,
273 quantize_and_encode_band_cost_STEREO,
274 quantize_and_encode_band_cost_STEREO,
275 };
276
277 11273753 float ff_quantize_and_encode_band_cost(struct AACEncContext *s, PutBitContext *pb,
278 const float *in, float *quant, const float *scaled,
279 int size, int scale_idx, int cb,
280 const float lambda, const float uplim,
281 int *bits, float *energy)
282 {
283 11273753 return quantize_and_encode_band_cost_arr[cb](s, pb, in, quant, scaled, size,
284 scale_idx, cb, lambda, uplim,
285 bits, energy);
286 }
287
288 546486 static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
289 const float *in, float *out, int size, int scale_idx,
290 int cb, const float lambda, int rtz)
291 {
292
2/2
✓ Branch 2 → 3 taken 5998 times.
✓ Branch 2 → 4 taken 540488 times.
546486 (rtz ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb](s, pb, in, out, NULL, size, scale_idx, cb,
293 lambda, INFINITY, NULL, NULL);
294 546486 }
295
296 /**
297 * structure used in optimal codebook search
298 */
299 typedef struct BandCodingPath {
300 int prev_idx; ///< pointer to the previous path point
301 float cost; ///< path cost
302 int run;
303 } BandCodingPath;
304
305 typedef struct TrellisPath {
306 float cost;
307 int prev;
308 } TrellisPath;
309
310 #define TRELLIS_STAGES 121
311 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
312
313 11849 static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
314 {
315 int w, g;
316 11849 int prevscaler_n = -255, prevscaler_i = 0;
317 11849 int bands = 0;
318
319
2/2
✓ Branch 16 → 3 taken 12612 times.
✓ Branch 16 → 17 taken 11849 times.
24461 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
320
2/2
✓ Branch 14 → 4 taken 562008 times.
✓ Branch 14 → 15 taken 12612 times.
574620 for (g = 0; g < sce->ics.num_swb; g++) {
321
2/2
✓ Branch 4 → 5 taken 43799 times.
✓ Branch 4 → 6 taken 518209 times.
562008 if (sce->zeroes[w*16+g])
322 43799 continue;
323
4/4
✓ Branch 6 → 7 taken 497813 times.
✓ Branch 6 → 8 taken 20396 times.
✓ Branch 7 → 8 taken 2390 times.
✓ Branch 7 → 9 taken 495423 times.
518209 if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
324 22786 sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
325 22786 bands++;
326
2/2
✓ Branch 9 → 10 taken 3513 times.
✓ Branch 9 → 13 taken 491910 times.
495423 } else if (sce->band_type[w*16+g] == NOISE_BT) {
327 3513 sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
328
2/2
✓ Branch 10 → 11 taken 853 times.
✓ Branch 10 → 12 taken 2660 times.
3513 if (prevscaler_n == -255)
329 853 prevscaler_n = sce->sf_idx[w*16+g];
330 3513 bands++;
331 }
332 }
333 }
334
335
2/2
✓ Branch 17 → 18 taken 9497 times.
✓ Branch 17 → 19 taken 2352 times.
11849 if (!bands)
336 9497 return;
337
338 /* Clip the scalefactor indices */
339
2/2
✓ Branch 31 → 20 taken 2453 times.
✓ Branch 31 → 32 taken 2352 times.
4805 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
340
2/2
✓ Branch 29 → 21 taken 114702 times.
✓ Branch 29 → 30 taken 2453 times.
117155 for (g = 0; g < sce->ics.num_swb; g++) {
341
2/2
✓ Branch 21 → 22 taken 7102 times.
✓ Branch 21 → 23 taken 107600 times.
114702 if (sce->zeroes[w*16+g])
342 7102 continue;
343
4/4
✓ Branch 23 → 24 taken 87204 times.
✓ Branch 23 → 25 taken 20396 times.
✓ Branch 24 → 25 taken 2390 times.
✓ Branch 24 → 26 taken 84814 times.
107600 if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
344 22786 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);
345
2/2
✓ Branch 26 → 27 taken 3513 times.
✓ Branch 26 → 28 taken 81301 times.
84814 } else if (sce->band_type[w*16+g] == NOISE_BT) {
346 3513 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);
347 }
348 }
349 }
350 }
351
352 7958 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
353 SingleChannelElement *sce,
354 const float lambda)
355 {
356 7958 int start = 0, i, w, w2, g;
357 7958 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
358 7958 float dists[128] = { 0 }, uplims[128] = { 0 };
359 float maxvals[128];
360 int fflag, minscaler;
361 7958 int its = 0;
362 7958 int allz = 0;
363 7958 float minthr = INFINITY;
364
365 // for values above this the decoder might end up in an endless loop
366 // due to always having more bits than what can be encoded.
367 7958 destbits = FFMIN(destbits, 5800);
368 //some heuristic to determine initial quantizers will reduce search time
369 //determine zero bands and upper limits
370
2/2
✓ Branch 18 → 3 taken 8402 times.
✓ Branch 18 → 19 taken 7958 times.
16360 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
371 8402 start = 0;
372
2/2
✓ Branch 16 → 4 taken 382460 times.
✓ Branch 16 → 17 taken 8402 times.
390862 for (g = 0; g < sce->ics.num_swb; g++) {
373 382460 int nz = 0;
374 382460 float uplim = 0.0f;
375
2/2
✓ Branch 10 → 5 taken 405840 times.
✓ Branch 10 → 11 taken 382460 times.
788300 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
376 405840 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
377 405840 uplim += band->threshold;
378
3/4
✓ Branch 5 → 6 taken 402442 times.
✓ Branch 5 → 7 taken 3398 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 402442 times.
405840 if (band->energy <= band->threshold || band->threshold == 0.0f) {
379 3398 sce->zeroes[(w+w2)*16+g] = 1;
380 3398 continue;
381 }
382 402442 nz = 1;
383 }
384 382460 uplims[w*16+g] = uplim *512;
385 382460 sce->band_type[w*16+g] = 0;
386 382460 sce->zeroes[w*16+g] = !nz;
387
2/2
✓ Branch 11 → 12 taken 379087 times.
✓ Branch 11 → 15 taken 3373 times.
382460 if (nz)
388
2/2
✓ Branch 12 → 13 taken 94301 times.
✓ Branch 12 → 14 taken 284786 times.
379087 minthr = FFMIN(minthr, uplim);
389 382460 allz |= nz;
390 382460 start += sce->ics.swb_sizes[g];
391 }
392 }
393
2/2
✓ Branch 30 → 20 taken 8402 times.
✓ Branch 30 → 31 taken 7958 times.
16360 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
394
2/2
✓ Branch 28 → 21 taken 382460 times.
✓ Branch 28 → 29 taken 8402 times.
390862 for (g = 0; g < sce->ics.num_swb; g++) {
395
2/2
✓ Branch 21 → 22 taken 3373 times.
✓ Branch 21 → 23 taken 379087 times.
382460 if (sce->zeroes[w*16+g]) {
396 3373 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
397 3373 continue;
398 }
399
2/2
✓ Branch 23 → 24 taken 188103 times.
✓ Branch 23 → 25 taken 190984 times.
379087 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
400 }
401 }
402
403
2/2
✓ Branch 31 → 32 taken 12 times.
✓ Branch 31 → 33 taken 7946 times.
7958 if (!allz)
404 12 return;
405 7946 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
406 7946 ff_quantize_band_cost_cache_init(s);
407
408
2/2
✓ Branch 41 → 36 taken 8390 times.
✓ Branch 41 → 42 taken 7946 times.
16336 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
409 8390 start = w*128;
410
2/2
✓ Branch 39 → 37 taken 381872 times.
✓ Branch 39 → 40 taken 8390 times.
390262 for (g = 0; g < sce->ics.num_swb; g++) {
411 381872 const float *scaled = s->scoefs + start;
412 381872 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
413 381872 start += sce->ics.swb_sizes[g];
414 }
415 }
416
417 //perform two-loop search
418 //outer loop - improve quality
419 do {
420 int tbits, qstep;
421 34561 minscaler = sce->sf_idx[0];
422 //inner loop - quantize spectrum to fit into given number of bits
423
2/2
✓ Branch 42 → 43 taken 26615 times.
✓ Branch 42 → 44 taken 7946 times.
34561 qstep = its ? 1 : 32;
424 do {
425 77942 int prev = -1;
426 77942 tbits = 0;
427
2/2
✓ Branch 62 → 47 taken 82594 times.
✓ Branch 62 → 63 taken 77942 times.
160536 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
428 82594 start = w*128;
429
2/2
✓ Branch 60 → 48 taken 3730581 times.
✓ Branch 60 → 61 taken 82594 times.
3813175 for (g = 0; g < sce->ics.num_swb; g++) {
430 3730581 const float *coefs = sce->coeffs + start;
431 3730581 const float *scaled = s->scoefs + start;
432 3730581 int bits = 0;
433 int cb;
434 3730581 float dist = 0.0f;
435
436
3/4
✓ Branch 48 → 49 taken 3690199 times.
✓ Branch 48 → 50 taken 40382 times.
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 3690199 times.
3730581 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
437 40382 start += sce->ics.swb_sizes[g];
438 40382 continue;
439 }
440 3690199 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
441 3690199 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
442
2/2
✓ Branch 55 → 53 taken 3923769 times.
✓ Branch 55 → 56 taken 3690199 times.
7613968 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
443 int b;
444 7847538 dist += quantize_band_cost_cached(s, w + w2, g,
445 3923769 coefs + w2*128,
446 3923769 scaled + w2*128,
447 3923769 sce->ics.swb_sizes[g],
448 3923769 sce->sf_idx[w*16+g],
449 cb, 1.0f, INFINITY,
450 &b, NULL, 0);
451 3923769 bits += b;
452 }
453 3690199 dists[w*16+g] = dist - bits;
454
2/2
✓ Branch 56 → 57 taken 3612257 times.
✓ Branch 56 → 58 taken 77942 times.
3690199 if (prev != -1) {
455 3612257 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
456 }
457 3690199 tbits += bits;
458 3690199 start += sce->ics.swb_sizes[g];
459 3690199 prev = sce->sf_idx[w*16+g];
460 }
461 }
462
2/2
✓ Branch 63 → 64 taken 14509 times.
✓ Branch 63 → 69 taken 63433 times.
77942 if (tbits > destbits) {
463
2/2
✓ Branch 68 → 65 taken 1857152 times.
✓ Branch 68 → 74 taken 14509 times.
1871661 for (i = 0; i < 128; i++)
464
1/2
✓ Branch 65 → 66 taken 1857152 times.
✗ Branch 65 → 67 not taken.
1857152 if (sce->sf_idx[i] < 218 - qstep)
465 1857152 sce->sf_idx[i] += qstep;
466 } else {
467
2/2
✓ Branch 73 → 70 taken 8119424 times.
✓ Branch 73 → 74 taken 63433 times.
8182857 for (i = 0; i < 128; i++)
468
2/2
✓ Branch 70 → 71 taken 3280454 times.
✓ Branch 70 → 72 taken 4838970 times.
8119424 if (sce->sf_idx[i] > 60 - qstep)
469 3280454 sce->sf_idx[i] -= qstep;
470 }
471 77942 qstep >>= 1;
472
5/6
✓ Branch 74 → 75 taken 38212 times.
✓ Branch 74 → 78 taken 39730 times.
✓ Branch 75 → 76 taken 3651 times.
✓ Branch 75 → 78 taken 34561 times.
✓ Branch 76 → 77 taken 3651 times.
✗ Branch 76 → 78 not taken.
77942 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
473 3651 qstep = 1;
474
2/2
✓ Branch 78 → 46 taken 43381 times.
✓ Branch 78 → 79 taken 34561 times.
77942 } while (qstep);
475
476 34561 fflag = 0;
477 34561 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
478
479
2/2
✓ Branch 93 → 80 taken 35953 times.
✓ Branch 93 → 94 taken 34561 times.
70514 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
480
2/2
✓ Branch 91 → 81 taken 1650792 times.
✓ Branch 91 → 92 taken 35953 times.
1686745 for (g = 0; g < sce->ics.num_swb; g++) {
481 1650792 int prevsc = sce->sf_idx[w*16+g];
482
3/4
✓ Branch 81 → 82 taken 5289 times.
✓ Branch 81 → 87 taken 1645503 times.
✓ Branch 82 → 83 taken 5289 times.
✗ Branch 82 → 87 not taken.
1650792 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
483
2/2
✓ Branch 84 → 85 taken 5111 times.
✓ Branch 84 → 86 taken 178 times.
5289 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
484 5111 sce->sf_idx[w*16+g]--;
485 else //Try to make sure there is some energy in every band
486 178 sce->sf_idx[w*16+g]-=2;
487 }
488 1650792 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
489 1650792 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
490
2/2
✓ Branch 87 → 88 taken 388051 times.
✓ Branch 87 → 89 taken 1262741 times.
1650792 if (sce->sf_idx[w*16+g] != prevsc)
491 388051 fflag = 1;
492 1650792 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
493 }
494 }
495 34561 its++;
496
4/4
✓ Branch 94 → 95 taken 29516 times.
✓ Branch 94 → 96 taken 5045 times.
✓ Branch 95 → 42 taken 26615 times.
✓ Branch 95 → 96 taken 2901 times.
34561 } while (fflag && its < 10);
497 }
498
499 1254 static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
500 {
501 FFPsyBand *band;
502 int w, g, w2, i;
503 1254 int wlen = 1024 / sce->ics.num_windows;
504 int bandwidth, cutoff;
505 1254 float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
506 1254 float *NOR34 = &s->scoefs[3*128];
507 uint8_t nextband[128];
508 1254 const float lambda = s->lambda;
509 1254 const float freq_mult = avctx->sample_rate*0.5f/wlen;
510 1254 const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
511
6/6
✓ Branch 2 → 3 taken 9 times.
✓ Branch 2 → 4 taken 1245 times.
✓ Branch 5 → 6 taken 12 times.
✓ Branch 5 → 10 taken 1242 times.
✓ Branch 6 → 7 taken 9 times.
✓ Branch 6 → 8 taken 3 times.
1254 const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
512 1254 const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
513
2/2
✓ Branch 11 → 12 taken 12 times.
✓ Branch 11 → 13 taken 1242 times.
1254 const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
514
515 1254 int prev = -1000, prev_sf = -1;
516
517 /* PNS candidacy must use the coder's actual coding bandwidth (s->bandwidth,
518 * fixed at init), not a separate heuristic, or it evaluates a different band
519 * range than the coder later codes. */
520 1254 bandwidth = s->bandwidth;
521 1254 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
522
523 1254 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
524 1254 ff_init_nextband_map(sce, nextband);
525
2/2
✓ Branch 90 → 16 taken 1376 times.
✓ Branch 90 → 91 taken 1254 times.
2630 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
526 1376 int wstart = w*128;
527
2/2
✓ Branch 88 → 17 taken 57646 times.
✓ Branch 88 → 89 taken 1376 times.
59022 for (g = 0; g < sce->ics.num_swb; g++) {
528 int noise_sfi;
529 57646 float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
530 57646 float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
531 57646 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
532 57646 float min_energy = -1.0f, max_energy = 0.0f;
533 57646 const int start = wstart+sce->ics.swb_offset[g];
534 57646 const float freq = (start-wstart)*freq_mult;
535
2/2
✓ Branch 17 → 18 taken 28690 times.
✓ Branch 17 → 19 taken 28956 times.
57646 const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
536
4/4
✓ Branch 20 → 21 taken 29808 times.
✓ Branch 20 → 22 taken 27838 times.
✓ Branch 21 → 22 taken 2878 times.
✓ Branch 21 → 25 taken 26930 times.
57646 if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) {
537
2/2
✓ Branch 22 → 23 taken 27153 times.
✓ Branch 22 → 24 taken 3563 times.
30716 if (!sce->zeroes[w*16+g])
538 27153 prev_sf = sce->sf_idx[w*16+g];
539 30716 continue;
540 }
541
2/2
✓ Branch 38 → 26 taken 29586 times.
✓ Branch 38 → 39 taken 26930 times.
56516 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
542 29586 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
543 29586 sfb_energy += band->energy;
544
2/2
✓ Branch 26 → 27 taken 27592 times.
✓ Branch 26 → 28 taken 1994 times.
29586 spread = FFMIN(spread, band->spread);
545 29586 threshold += band->threshold;
546
2/2
✓ Branch 29 → 30 taken 26930 times.
✓ Branch 29 → 31 taken 2656 times.
29586 if (!w2) {
547 26930 min_energy = max_energy = band->energy;
548 } else {
549
2/2
✓ Branch 31 → 32 taken 863 times.
✓ Branch 31 → 33 taken 1793 times.
2656 min_energy = FFMIN(min_energy, band->energy);
550
2/2
✓ Branch 34 → 35 taken 1428 times.
✓ Branch 34 → 36 taken 1228 times.
2656 max_energy = FFMAX(max_energy, band->energy);
551 }
552 }
553
554 /* Ramps down at ~8000Hz and loosens the dist threshold */
555 26930 dist_thresh = av_clipf(2.5f*NOISE_LOW_LIMIT/freq, 0.5f, 2.5f) * dist_bias;
556
557 /* PNS is acceptable when all of these are true:
558 * 1. high spread energy (noise-like band)
559 * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
560 * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
561 *
562 * At this stage, point 2 is relaxed for zeroed bands near the noise threshold (hole avoidance is more important)
563 */
564
3/4
✓ Branch 39 → 40 taken 26269 times.
✓ Branch 39 → 42 taken 661 times.
✓ Branch 41 → 42 taken 26269 times.
✗ Branch 41 → 50 not taken.
26930 if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) ||
565
8/8
✓ Branch 42 → 43 taken 26269 times.
✓ Branch 42 → 44 taken 661 times.
✓ Branch 43 → 44 taken 3583 times.
✓ Branch 43 → 45 taken 22686 times.
✓ Branch 44 → 45 taken 3680 times.
✓ Branch 44 → 50 taken 564 times.
✓ Branch 45 → 46 taken 26158 times.
✓ Branch 45 → 50 taken 208 times.
26930 ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
566
6/6
✓ Branch 46 → 47 taken 26061 times.
✓ Branch 46 → 49 taken 97 times.
✓ Branch 47 → 48 taken 22480 times.
✓ Branch 47 → 49 taken 3581 times.
✓ Branch 48 → 49 taken 12888 times.
✓ Branch 48 → 50 taken 9592 times.
26158 (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) ||
567
2/2
✓ Branch 49 → 50 taken 207 times.
✓ Branch 49 → 53 taken 16359 times.
16566 min_energy < pns_transient_energy_r * max_energy ) {
568 10571 sce->pns_ener[w*16+g] = sfb_energy;
569
2/2
✓ Branch 50 → 51 taken 10007 times.
✓ Branch 50 → 52 taken 564 times.
10571 if (!sce->zeroes[w*16+g])
570 10007 prev_sf = sce->sf_idx[w*16+g];
571 10571 continue;
572 }
573
574
2/2
✓ Branch 53 → 54 taken 15654 times.
✓ Branch 53 → 55 taken 705 times.
16359 pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread);
575 16359 noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
576 16359 noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */
577
2/2
✓ Branch 56 → 57 taken 10879 times.
✓ Branch 56 → 62 taken 5480 times.
16359 if (prev != -1000) {
578 10879 int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO;
579
2/4
✓ Branch 57 → 58 taken 10879 times.
✗ Branch 57 → 59 not taken.
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 62 taken 10879 times.
10879 if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) {
580
0/2
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ if (!sce->zeroes[w*16+g])
581 ✗ prev_sf = sce->sf_idx[w*16+g];
582 ✗ continue;
583 }
584 }
585
2/2
✓ Branch 74 → 63 taken 16584 times.
✓ Branch 74 → 75 taken 16359 times.
32943 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
586 float band_energy, scale, pns_senergy;
587 16584 const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
588 16584 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
589
2/2
✓ Branch 66 → 64 taken 539668 times.
✓ Branch 66 → 67 taken 16584 times.
556252 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
590 539668 s->random_state = lcg_random(s->random_state);
591 539668 PNS[i] = s->random_state;
592 }
593 16584 band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
594 16584 scale = noise_amp/sqrtf(band_energy);
595 16584 s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
596 16584 pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
597 16584 pns_energy += pns_senergy;
598 16584 s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
599 16584 s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]);
600 33168 dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
601 NOR34,
602 16584 sce->ics.swb_sizes[g],
603 16584 sce->sf_idx[(w+w2)*16+g],
604 16584 sce->band_alt[(w+w2)*16+g],
605 16584 lambda/band->threshold, INFINITY, NULL, NULL);
606 /* Estimate rd on average as 5 bits for SF, 4 for the CB, plus spread energy * lambda/thr */
607 16584 dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold;
608 }
609
3/4
✓ Branch 75 → 76 taken 16359 times.
✗ Branch 75 → 78 not taken.
✓ Branch 76 → 77 taken 2752 times.
✓ Branch 76 → 78 taken 13607 times.
16359 if (g && sce->band_type[w*16+g-1] == NOISE_BT) {
610 2752 dist2 += 5;
611 } else {
612 13607 dist2 += 9;
613 }
614 16359 energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
615 16359 sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
616
8/10
✓ Branch 79 → 80 taken 16262 times.
✓ Branch 79 → 84 taken 97 times.
✓ Branch 80 → 81 taken 12874 times.
✓ Branch 80 → 84 taken 3388 times.
✓ Branch 81 → 82 taken 12502 times.
✓ Branch 81 → 85 taken 372 times.
✓ Branch 82 → 83 taken 12502 times.
✗ Branch 82 → 85 not taken.
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 12502 times.
16359 if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
617 3485 sce->band_type[w*16+g] = NOISE_BT;
618 3485 sce->zeroes[w*16+g] = 0;
619 3485 prev = noise_sfi;
620 } else {
621
1/2
✓ Branch 85 → 86 taken 12874 times.
✗ Branch 85 → 87 not taken.
12874 if (!sce->zeroes[w*16+g])
622 12874 prev_sf = sce->sf_idx[w*16+g];
623 }
624 }
625 }
626 1254 }
627
628 5121 static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
629 {
630 FFPsyBand *band;
631 int w, g, w2;
632 5121 int wlen = 1024 / sce->ics.num_windows;
633 int bandwidth, cutoff;
634 5121 const float lambda = s->lambda;
635 5121 const float freq_mult = avctx->sample_rate*0.5f/wlen;
636
6/6
✓ Branch 2 → 3 taken 105 times.
✓ Branch 2 → 4 taken 5016 times.
✓ Branch 5 → 6 taken 108 times.
✓ Branch 5 → 10 taken 5013 times.
✓ Branch 6 → 7 taken 105 times.
✓ Branch 6 → 8 taken 3 times.
5121 const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
637
2/2
✓ Branch 11 → 12 taken 108 times.
✓ Branch 11 → 13 taken 5013 times.
5121 const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
638
639 /* PNS candidacy must use the coder's actual coding bandwidth (s->bandwidth,
640 * fixed at init), not a separate heuristic, or it evaluates a different band
641 * range than the coder later codes (NMR relies on this output directly). */
642 5121 bandwidth = s->bandwidth;
643 5121 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
644
645 5121 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
646
2/2
✓ Branch 54 → 15 taken 5514 times.
✓ Branch 54 → 55 taken 5121 times.
10635 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
647
2/2
✓ Branch 52 → 16 taken 235976 times.
✓ Branch 52 → 53 taken 5514 times.
241490 for (g = 0; g < sce->ics.num_swb; g++) {
648 235976 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
649 235976 float min_energy = -1.0f, max_energy = 0.0f;
650 235976 const int start = sce->ics.swb_offset[g];
651 235976 const float freq = start*freq_mult;
652
2/2
✓ Branch 16 → 17 taken 109648 times.
✓ Branch 16 → 18 taken 126328 times.
235976 const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
653
4/4
✓ Branch 19 → 20 taken 113972 times.
✓ Branch 19 → 21 taken 122004 times.
✓ Branch 20 → 21 taken 21866 times.
✓ Branch 20 → 22 taken 92106 times.
235976 if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
654 143870 sce->can_pns[w*16+g] = 0;
655 143870 continue;
656 }
657
2/2
✓ Branch 35 → 23 taken 102916 times.
✓ Branch 35 → 36 taken 92106 times.
195022 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
658 102916 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
659 102916 sfb_energy += band->energy;
660
2/2
✓ Branch 23 → 24 taken 95121 times.
✓ Branch 23 → 25 taken 7795 times.
102916 spread = FFMIN(spread, band->spread);
661 102916 threshold += band->threshold;
662
2/2
✓ Branch 26 → 27 taken 92106 times.
✓ Branch 26 → 28 taken 10810 times.
102916 if (!w2) {
663 92106 min_energy = max_energy = band->energy;
664 } else {
665
2/2
✓ Branch 28 → 29 taken 3728 times.
✓ Branch 28 → 30 taken 7082 times.
10810 min_energy = FFMIN(min_energy, band->energy);
666
2/2
✓ Branch 31 → 32 taken 5918 times.
✓ Branch 31 → 33 taken 4892 times.
10810 max_energy = FFMAX(max_energy, band->energy);
667 }
668 }
669
670 /* PNS is acceptable when all of these are true:
671 * 1. high spread energy (noise-like band)
672 * 2. near-threshold energy (high PE means the random nature of PNS content will be noticed)
673 * 3. on short window groups, all windows have similar energy (variations in energy would be destroyed by PNS)
674 */
675 92106 sce->pns_ener[w*16+g] = sfb_energy;
676 {
677 /* near-mask PNS class (E in [thr/4, 2*thr]): deletion
678 * candidates go to noise, not silence (AAC_PNSHOLE) */
679
2/2
✓ Branch 36 → 37 taken 11440 times.
✓ Branch 36 → 39 taken 80666 times.
103546 int near = sfb_energy < 2.0f * threshold &&
680
2/2
✓ Branch 37 → 38 taken 9879 times.
✓ Branch 37 → 39 taken 1561 times.
11440 sfb_energy > threshold * 0.25f;
681
2/2
✓ Branch 40 → 41 taken 9879 times.
✓ Branch 40 → 46 taken 82227 times.
92106 if (near) {
682 /* deletion candidate: noise beats the ~silent rendition */
683
2/2
✓ Branch 41 → 42 taken 9747 times.
✓ Branch 41 → 44 taken 132 times.
19626 sce->can_pns[w*16+g] = spread >= spread_threshold &&
684
1/2
✓ Branch 42 → 43 taken 9747 times.
✗ Branch 42 → 44 not taken.
9747 min_energy >= 0.2f * max_energy;
685
6/6
✓ Branch 46 → 47 taken 80666 times.
✓ Branch 46 → 49 taken 1561 times.
✓ Branch 47 → 48 taken 80245 times.
✓ Branch 47 → 49 taken 421 times.
✓ Branch 48 → 49 taken 3535 times.
✓ Branch 48 → 50 taken 76710 times.
82227 } else if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
686 5517 sce->can_pns[w*16+g] = 0;
687 } else {
688 76710 sce->can_pns[w*16+g] = 1;
689 }
690 }
691 }
692 }
693 5121 }
694
695 24 static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
696 {
697 24 int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
698 uint8_t nextband0[128], nextband1[128];
699 24 float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1;
700 24 float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3;
701 24 float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5;
702 24 const float lambda = s->lambda;
703
2/2
✓ Branch 2 → 3 taken 12 times.
✓ Branch 2 → 4 taken 12 times.
24 const float mslambda = FFMIN(1.0f, lambda / 120.f);
704 24 SingleChannelElement *sce0 = &cpe->ch[0];
705 24 SingleChannelElement *sce1 = &cpe->ch[1];
706
1/2
✓ Branch 5 → 6 taken 24 times.
✗ Branch 5 → 7 not taken.
24 if (!cpe->common_window)
707 24 return;
708
709 /** Scout out next nonzero bands */
710 ✗ ff_init_nextband_map(sce0, nextband0);
711 ✗ ff_init_nextband_map(sce1, nextband1);
712
713 ✗ prev_mid = sce0->sf_idx[0];
714 ✗ prev_side = sce1->sf_idx[0];
715
0/2
✗ Branch 86 → 10 not taken.
✗ Branch 86 → 87 not taken.
✗ for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
716 ✗ start = 0;
717
0/2
✗ Branch 84 → 11 not taken.
✗ Branch 84 → 85 not taken.
✗ for (g = 0; g < sce0->ics.num_swb; g++) {
718 ✗ float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
719
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if (!cpe->is_mask[w*16+g])
720 ✗ cpe->ms_mask[w*16+g] = 0;
721
0/6
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 76 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 76 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 76 not taken.
✗ if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
722 ✗ float Mmax = 0.0f, Smax = 0.0f;
723
724 /* Must compute mid/side SF and book for the whole window group */
725
0/2
✗ Branch 33 → 18 not taken.
✗ Branch 33 → 34 not taken.
✗ for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
726
0/2
✗ Branch 20 → 19 not taken.
✗ Branch 20 → 21 not taken.
✗ for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
727 ✗ M[i] = (sce0->coeffs[start+(w+w2)*128+i]
728 ✗ + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
729 ✗ S[i] = M[i]
730 ✗ - sce1->coeffs[start+(w+w2)*128+i];
731 }
732 ✗ s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
733 ✗ s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
734
0/2
✗ Branch 31 → 24 not taken.
✗ Branch 31 → 32 not taken.
✗ for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
735
0/2
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ Mmax = FFMAX(Mmax, M34[i]);
736
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ Smax = FFMAX(Smax, S34[i]);
737 }
738 }
739
740
0/2
✗ Branch 75 → 35 not taken.
✗ Branch 75 → 76 not taken.
✗ for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
741 ✗ float dist1 = 0.0f, dist2 = 0.0f;
742 ✗ int B0 = 0, B1 = 0;
743 int minidx;
744 int mididx, sididx;
745 int midcb, sidcb;
746
747 ✗ minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]);
748 ✗ mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512);
749 ✗ sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512);
750
0/4
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 42 not taken.
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 42 not taken.
✗ if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
751
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 41 not taken.
✗ && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
752
0/2
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 42 not taken.
✗ || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) {
753 /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
754 ✗ continue;
755 }
756
757 ✗ midcb = find_min_book(Mmax, mididx);
758 ✗ sidcb = find_min_book(Smax, sididx);
759
760 /* No CB can be zero */
761 ✗ midcb = FFMAX(1,midcb);
762 ✗ sidcb = FFMAX(1,sidcb);
763
764
0/2
✗ Branch 60 → 45 not taken.
✗ Branch 60 → 61 not taken.
✗ for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
765 ✗ FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
766 ✗ FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
767
0/2
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 47 not taken.
✗ float minthr = FFMIN(band0->threshold, band1->threshold);
768 int b1,b2,b3,b4;
769
0/2
✗ Branch 50 → 49 not taken.
✗ Branch 50 → 51 not taken.
✗ for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
770 ✗ M[i] = (sce0->coeffs[start+(w+w2)*128+i]
771 ✗ + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
772 ✗ S[i] = M[i]
773 ✗ - sce1->coeffs[start+(w+w2)*128+i];
774 }
775
776 ✗ s->aacdsp.abs_pow34(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
777 ✗ s->aacdsp.abs_pow34(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
778 ✗ s->aacdsp.abs_pow34(M34, M, sce0->ics.swb_sizes[g]);
779 ✗ s->aacdsp.abs_pow34(S34, S, sce0->ics.swb_sizes[g]);
780 ✗ dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
781 L34,
782 ✗ sce0->ics.swb_sizes[g],
783 ✗ sce0->sf_idx[w*16+g],
784 ✗ sce0->band_type[w*16+g],
785 ✗ lambda / (band0->threshold + FLT_MIN), INFINITY, &b1, NULL);
786 ✗ dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
787 R34,
788 ✗ sce1->ics.swb_sizes[g],
789 ✗ sce1->sf_idx[w*16+g],
790 ✗ sce1->band_type[w*16+g],
791 ✗ lambda / (band1->threshold + FLT_MIN), INFINITY, &b2, NULL);
792 ✗ dist2 += quantize_band_cost(s, M,
793 M34,
794 ✗ sce0->ics.swb_sizes[g],
795 mididx,
796 midcb,
797 ✗ lambda / (minthr + FLT_MIN), INFINITY, &b3, NULL);
798 ✗ dist2 += quantize_band_cost(s, S,
799 S34,
800 ✗ sce1->ics.swb_sizes[g],
801 sididx,
802 sidcb,
803 ✗ mslambda / (minthr * bmax + FLT_MIN), INFINITY, &b4, NULL);
804 ✗ B0 += b1+b2;
805 ✗ B1 += b3+b4;
806 ✗ dist1 -= b1+b2;
807 ✗ dist2 -= b3+b4;
808 }
809
0/4
✗ Branch 61 → 62 not taken.
✗ Branch 61 → 64 not taken.
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
810
0/2
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 72 not taken.
✗ if (cpe->ms_mask[w*16+g]) {
811
0/4
✗ Branch 66 → 67 not taken.
✗ Branch 66 → 69 not taken.
✗ Branch 67 → 68 not taken.
✗ Branch 67 → 69 not taken.
✗ if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) {
812 ✗ sce0->sf_idx[w*16+g] = mididx;
813 ✗ sce1->sf_idx[w*16+g] = sididx;
814 ✗ sce0->band_type[w*16+g] = midcb;
815 ✗ sce1->band_type[w*16+g] = sidcb;
816
0/2
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 71 not taken.
✗ } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) {
817 /* ms_mask unneeded, and it confuses some decoders */
818 ✗ cpe->ms_mask[w*16+g] = 0;
819 }
820 ✗ break;
821
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
✗ } else if (B1 > B0) {
822 /* More boost won't fix this */
823 ✗ break;
824 }
825 }
826 }
827
0/4
✗ Branch 76 → 77 not taken.
✗ Branch 76 → 79 not taken.
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 79 not taken.
✗ if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
828 ✗ prev_mid = sce0->sf_idx[w*16+g];
829
0/6
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 83 not taken.
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 83 not taken.
✗ Branch 81 → 82 not taken.
✗ Branch 81 → 83 not taken.
✗ if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT)
830 ✗ prev_side = sce1->sf_idx[w*16+g];
831 ✗ start += sce0->ics.swb_sizes[g];
832 }
833 }
834 }
835
836 const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
837 [AAC_CODER_TWOLOOP] = {
838 search_for_quantizers_twoloop,
839 codebook_trellis_rate,
840 quantize_and_encode_band,
841 ff_aac_encode_tns_info,
842 ff_aac_apply_tns,
843 set_special_band_scalefactors,
844 search_for_pns,
845 mark_pns,
846 ff_aac_search_for_tns,
847 search_for_ms,
848 ff_aac_search_for_is,
849 },
850 [AAC_CODER_FAST] = {
851 search_for_quantizers_fast,
852 codebook_trellis_rate,
853 quantize_and_encode_band,
854 ff_aac_encode_tns_info,
855 ff_aac_apply_tns,
856 set_special_band_scalefactors,
857 search_for_pns,
858 mark_pns,
859 ff_aac_search_for_tns,
860 search_for_ms,
861 ff_aac_search_for_is,
862 },
863 [AAC_CODER_NMR] = {
864 search_for_quantizers_nmr,
865 codebook_trellis_rate,
866 quantize_and_encode_band,
867 ff_aac_encode_tns_info,
868 ff_aac_apply_tns,
869 set_special_band_scalefactors,
870 NULL, /* PNS decided in the trellis (search_for_quantizers_nmr) */
871 mark_pns,
872 ff_aac_search_for_tns,
873 NULL,
874 NULL,
875 },
876 };
877