FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aaccoder.c
Date: 2026-10-28 09:10:55
Exec Total Coverage
Lines: 342 451 75.8%
Functions: 17 19 89.5%
Branches: 265 368 72.0%

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 27 av_cold void ff_aac_coder_init_tables(void)
68 {
69
2/2
✓ Branch 0 taken 221184 times.
✓ Branch 1 taken 27 times.
221211 for (int c = 0; c < FF_ARRAY_ELEMS(esc_pow43); c++)
70 221184 esc_pow43[c] = c * cbrtf(c);
71 27 }
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 11763085 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 11763085 const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
94 11763085 const float Q = ff_aac_pow2sf_tab [q_idx];
95 11763085 const float Q34 = ff_aac_pow34sf_tab[q_idx];
96 11763085 const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
97 11763085 const float CLIPPED_ESCAPE = 165140.0f*IQ;
98 11763085 float cost = 0;
99 11763085 float qenergy = 0;
100
2/2
✓ Branch 0 taken 6798084 times.
✓ Branch 1 taken 4965001 times.
11763085 const int dim = BT_PAIR ? 2 : 4;
101 11763085 int resbits = 0;
102 int off;
103
104
6/6
✓ Branch 0 taken 10585936 times.
✓ Branch 1 taken 1177149 times.
✓ Branch 2 taken 10578986 times.
✓ Branch 3 taken 6950 times.
✓ Branch 4 taken 47228 times.
✓ Branch 5 taken 10531758 times.
11763085 if (BT_ZERO || BT_NOISE || BT_STEREO) {
105
2/2
✓ Branch 0 taken 25646100 times.
✓ Branch 1 taken 1231327 times.
26877427 for (int i = 0; i < size; i++)
106 25646100 cost += in[i]*in[i];
107
2/2
✓ Branch 0 taken 1198901 times.
✓ Branch 1 taken 32426 times.
1231327 if (bits)
108 1198901 *bits = 0;
109
2/2
✓ Branch 0 taken 1159697 times.
✓ Branch 1 taken 71630 times.
1231327 if (energy)
110 1159697 *energy = qenergy;
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1231327 times.
1231327 if (out) {
112 ✗ for (int i = 0; i < size; i += dim)
113 ✗ for (int j = 0; j < dim; j++)
114 ✗ out[i+j] = 0.0f;
115 }
116 1231327 return cost * lambda;
117 }
118
2/2
✓ Branch 0 taken 519170 times.
✓ Branch 1 taken 10012588 times.
10531758 if (!scaled) {
119 519170 s->aacdsp.abs_pow34(s->scoefs, in, size);
120 519170 scaled = s->scoefs;
121 }
122 10531758 s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING);
123
2/2
✓ Branch 0 taken 6864499 times.
✓ Branch 1 taken 3667259 times.
10531758 if (BT_UNSIGNED) {
124 6864499 off = 0;
125 } else {
126 3667259 off = aac_cb_maxval[cb];
127 }
128
2/2
✓ Branch 0 taken 77476221 times.
✓ Branch 1 taken 10531758 times.
88007979 for (int i = 0; i < size; i += dim) {
129 const float *vec;
130 77476221 int *quants = s->qcoefs + i;
131 77476221 int curidx = 0;
132 int curbits;
133 77476221 float quantized, rd = 0.0f;
134
2/2
✓ Branch 0 taken 193891332 times.
✓ Branch 1 taken 77476221 times.
271367553 for (int j = 0; j < dim; j++) {
135 193891332 curidx *= aac_cb_range[cb];
136 193891332 curidx += quants[j] + off;
137 }
138 77476221 curbits = ff_aac_spectral_bits[cb-1][curidx];
139 77476221 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
140
2/2
✓ Branch 0 taken 51790259 times.
✓ Branch 1 taken 25685962 times.
77476221 if (BT_UNSIGNED) {
141
2/2
✓ Branch 0 taken 120077444 times.
✓ Branch 1 taken 51790259 times.
171867703 for (int j = 0; j < dim; j++) {
142 120077444 float t = fabsf(in[i+j]);
143 float di;
144
4/4
✓ Branch 0 taken 41286660 times.
✓ Branch 1 taken 78790784 times.
✓ Branch 2 taken 20113827 times.
✓ Branch 3 taken 21172833 times.
120077444 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
145
2/2
✓ Branch 0 taken 2092313 times.
✓ Branch 1 taken 18021514 times.
20113827 if (t >= CLIPPED_ESCAPE) {
146 2092313 quantized = CLIPPED_ESCAPE;
147 2092313 curbits += 21;
148 } else {
149 18021514 int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
150 18021514 quantized = esc_pow43[c]*IQ;
151 18021514 curbits += av_log2(c)*2 - 4 + 1;
152 }
153 } else {
154 99963617 quantized = vec[j]*IQ;
155 }
156 120077444 di = t - quantized;
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120077444 times.
120077444 if (out)
158 ✗ out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
159
2/2
✓ Branch 0 taken 81753851 times.
✓ Branch 1 taken 38323593 times.
120077444 if (vec[j] != 0.0f)
160 81753851 curbits++;
161 120077444 qenergy += quantized*quantized;
162 120077444 rd += di*di;
163 }
164 } else {
165
2/2
✓ Branch 0 taken 73813888 times.
✓ Branch 1 taken 25685962 times.
99499850 for (int j = 0; j < dim; j++) {
166 73813888 quantized = vec[j]*IQ;
167 73813888 qenergy += quantized*quantized;
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73813888 times.
73813888 if (out)
169 ✗ out[i+j] = quantized;
170 73813888 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
171 }
172 }
173 77476221 cost += rd * lambda + curbits;
174 77476221 resbits += curbits;
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77476221 times.
77476221 if (cost >= uplim)
176 ✗ return uplim;
177
2/2
✓ Branch 0 taken 3521406 times.
✓ Branch 1 taken 73954815 times.
77476221 if (pb) {
178 3521406 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
179
2/2
✓ Branch 0 taken 1781153 times.
✓ Branch 1 taken 1740253 times.
3521406 if (BT_UNSIGNED)
180
2/2
✓ Branch 0 taken 4875576 times.
✓ Branch 1 taken 1781153 times.
6656729 for (int j = 0; j < dim; j++)
181
2/2
✓ Branch 0 taken 3336259 times.
✓ Branch 1 taken 1539317 times.
4875576 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
182 3336259 put_bits(pb, 1, in[i+j] < 0.0f);
183
2/2
✓ Branch 0 taken 681990 times.
✓ Branch 1 taken 2839416 times.
3521406 if (BT_ESC) {
184
2/2
✓ Branch 0 taken 1363980 times.
✓ Branch 1 taken 681990 times.
2045970 for (int j = 0; j < 2; j++) {
185
2/2
✓ Branch 0 taken 196902 times.
✓ Branch 1 taken 1167078 times.
1363980 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
186 196902 int coef = av_clip(quant(fabsf(in[i+j]), Q, ROUNDING), 16, (1 << 13) - 1);
187 196902 int len = av_log2(coef);
188
189 196902 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
190 196902 put_sbits(pb, len, coef);
191 }
192 }
193 }
194 }
195 }
196
197
2/2
✓ Branch 0 taken 9944537 times.
✓ Branch 1 taken 587221 times.
10531758 if (bits)
198 9944537 *bits = resbits;
199
2/2
✓ Branch 0 taken 6003127 times.
✓ Branch 1 taken 4528631 times.
10531758 if (energy)
200 6003127 *energy = qenergy;
201 10531758 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 1177149 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD)
228 2146224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
229 1587450 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
230 1521035 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
231 2513870 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
232 2760691 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD)
233 2488 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
234 6950 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 11216826 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 11216826 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 546259 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 0 taken 5350 times.
✓ Branch 1 taken 540909 times.
546259 (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 546259 }
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 11374 static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
314 {
315 int w, g;
316 11374 int prevscaler_n = -255, prevscaler_i = 0;
317 11374 int bands = 0;
318
319
2/2
✓ Branch 0 taken 12089 times.
✓ Branch 1 taken 11374 times.
23463 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
320
2/2
✓ Branch 0 taken 541693 times.
✓ Branch 1 taken 12089 times.
553782 for (g = 0; g < sce->ics.num_swb; g++) {
321
2/2
✓ Branch 0 taken 23663 times.
✓ Branch 1 taken 518030 times.
541693 if (sce->zeroes[w*16+g])
322 23663 continue;
323
4/4
✓ Branch 0 taken 497634 times.
✓ Branch 1 taken 20396 times.
✓ Branch 2 taken 2390 times.
✓ Branch 3 taken 495244 times.
518030 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 0 taken 3416 times.
✓ Branch 1 taken 491828 times.
495244 } else if (sce->band_type[w*16+g] == NOISE_BT) {
327 3416 sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
328
2/2
✓ Branch 0 taken 842 times.
✓ Branch 1 taken 2574 times.
3416 if (prevscaler_n == -255)
329 842 prevscaler_n = sce->sf_idx[w*16+g];
330 3416 bands++;
331 }
332 }
333 }
334
335
2/2
✓ Branch 0 taken 9033 times.
✓ Branch 1 taken 2341 times.
11374 if (!bands)
336 9033 return;
337
338 /* Clip the scalefactor indices */
339
2/2
✓ Branch 0 taken 2414 times.
✓ Branch 1 taken 2341 times.
4755 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
340
2/2
✓ Branch 0 taken 114156 times.
✓ Branch 1 taken 2414 times.
116570 for (g = 0; g < sce->ics.num_swb; g++) {
341
2/2
✓ Branch 0 taken 6892 times.
✓ Branch 1 taken 107264 times.
114156 if (sce->zeroes[w*16+g])
342 6892 continue;
343
4/4
✓ Branch 0 taken 86868 times.
✓ Branch 1 taken 20396 times.
✓ Branch 2 taken 2390 times.
✓ Branch 3 taken 84478 times.
107264 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 0 taken 3416 times.
✓ Branch 1 taken 81062 times.
84478 } else if (sce->band_type[w*16+g] == NOISE_BT) {
346 3416 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 7966 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
353 SingleChannelElement *sce,
354 const float lambda)
355 {
356 7966 int start = 0, i, w, w2, g;
357 7966 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
358 7966 float dists[128] = { 0 }, uplims[128] = { 0 };
359 float maxvals[128];
360 int fflag, minscaler;
361 7966 int its = 0;
362 7966 int allz = 0;
363 7966 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 7966 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 0 taken 8418 times.
✓ Branch 1 taken 7966 times.
16384 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
371 8418 start = 0;
372
2/2
✓ Branch 0 taken 382684 times.
✓ Branch 1 taken 8418 times.
391102 for (g = 0; g < sce->ics.num_swb; g++) {
373 382684 int nz = 0;
374 382684 float uplim = 0.0f;
375
2/2
✓ Branch 0 taken 406736 times.
✓ Branch 1 taken 382684 times.
789420 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
376 406736 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
377 406736 uplim += band->threshold;
378
3/4
✓ Branch 0 taken 403343 times.
✓ Branch 1 taken 3393 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 403343 times.
406736 if (band->energy <= band->threshold || band->threshold == 0.0f) {
379 3393 sce->zeroes[(w+w2)*16+g] = 1;
380 3393 continue;
381 }
382 403343 nz = 1;
383 }
384 382684 uplims[w*16+g] = uplim *512;
385 382684 sce->band_type[w*16+g] = 0;
386 382684 sce->zeroes[w*16+g] = !nz;
387
2/2
✓ Branch 0 taken 379311 times.
✓ Branch 1 taken 3373 times.
382684 if (nz)
388
2/2
✓ Branch 0 taken 94427 times.
✓ Branch 1 taken 284884 times.
379311 minthr = FFMIN(minthr, uplim);
389 382684 allz |= nz;
390 382684 start += sce->ics.swb_sizes[g];
391 }
392 }
393
2/2
✓ Branch 0 taken 8418 times.
✓ Branch 1 taken 7966 times.
16384 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
394
2/2
✓ Branch 0 taken 382684 times.
✓ Branch 1 taken 8418 times.
391102 for (g = 0; g < sce->ics.num_swb; g++) {
395
2/2
✓ Branch 0 taken 3373 times.
✓ Branch 1 taken 379311 times.
382684 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 0 taken 188225 times.
✓ Branch 1 taken 191086 times.
379311 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 0 taken 12 times.
✓ Branch 1 taken 7954 times.
7966 if (!allz)
404 12 return;
405 7954 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
406 7954 ff_quantize_band_cost_cache_init(s);
407
408
2/2
✓ Branch 0 taken 8406 times.
✓ Branch 1 taken 7954 times.
16360 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
409 8406 start = w*128;
410
2/2
✓ Branch 0 taken 382096 times.
✓ Branch 1 taken 8406 times.
390502 for (g = 0; g < sce->ics.num_swb; g++) {
411 382096 const float *scaled = s->scoefs + start;
412 382096 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
413 382096 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 34641 minscaler = sce->sf_idx[0];
422 //inner loop - quantize spectrum to fit into given number of bits
423
2/2
✓ Branch 0 taken 26687 times.
✓ Branch 1 taken 7954 times.
34641 qstep = its ? 1 : 32;
424 do {
425 78062 int prev = -1;
426 78062 tbits = 0;
427
2/2
✓ Branch 0 taken 82834 times.
✓ Branch 1 taken 78062 times.
160896 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
428 82834 start = w*128;
429
2/2
✓ Branch 0 taken 3733941 times.
✓ Branch 1 taken 82834 times.
3816775 for (g = 0; g < sce->ics.num_swb; g++) {
430 3733941 const float *coefs = sce->coeffs + start;
431 3733941 const float *scaled = s->scoefs + start;
432 3733941 int bits = 0;
433 int cb;
434 3733941 float dist = 0.0f;
435
436
3/4
✓ Branch 0 taken 3693559 times.
✓ Branch 1 taken 40382 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3693559 times.
3733941 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 3693559 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
441 3693559 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
442
2/2
✓ Branch 0 taken 3937209 times.
✓ Branch 1 taken 3693559 times.
7630768 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
443 int b;
444 7874418 dist += quantize_band_cost_cached(s, w + w2, g,
445 3937209 coefs + w2*128,
446 3937209 scaled + w2*128,
447 3937209 sce->ics.swb_sizes[g],
448 3937209 sce->sf_idx[w*16+g],
449 cb, 1.0f, INFINITY,
450 &b, NULL, 0);
451 3937209 bits += b;
452 }
453 3693559 dists[w*16+g] = dist - bits;
454
2/2
✓ Branch 0 taken 3615497 times.
✓ Branch 1 taken 78062 times.
3693559 if (prev != -1) {
455 3615497 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
456 }
457 3693559 tbits += bits;
458 3693559 start += sce->ics.swb_sizes[g];
459 3693559 prev = sce->sf_idx[w*16+g];
460 }
461 }
462
2/2
✓ Branch 0 taken 14509 times.
✓ Branch 1 taken 63553 times.
78062 if (tbits > destbits) {
463
2/2
✓ Branch 0 taken 1857152 times.
✓ Branch 1 taken 14509 times.
1871661 for (i = 0; i < 128; i++)
464
1/2
✓ Branch 0 taken 1857152 times.
✗ Branch 1 not taken.
1857152 if (sce->sf_idx[i] < 218 - qstep)
465 1857152 sce->sf_idx[i] += qstep;
466 } else {
467
2/2
✓ Branch 0 taken 8134784 times.
✓ Branch 1 taken 63553 times.
8198337 for (i = 0; i < 128; i++)
468
2/2
✓ Branch 0 taken 3283891 times.
✓ Branch 1 taken 4850893 times.
8134784 if (sce->sf_idx[i] > 60 - qstep)
469 3283891 sce->sf_idx[i] -= qstep;
470 }
471 78062 qstep >>= 1;
472
5/6
✓ Branch 0 taken 38292 times.
✓ Branch 1 taken 39770 times.
✓ Branch 2 taken 3651 times.
✓ Branch 3 taken 34641 times.
✓ Branch 4 taken 3651 times.
✗ Branch 5 not taken.
78062 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
473 3651 qstep = 1;
474
2/2
✓ Branch 0 taken 43421 times.
✓ Branch 1 taken 34641 times.
78062 } while (qstep);
475
476 34641 fflag = 0;
477 34641 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
478
479
2/2
✓ Branch 0 taken 36113 times.
✓ Branch 1 taken 34641 times.
70754 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
480
2/2
✓ Branch 0 taken 1653032 times.
✓ Branch 1 taken 36113 times.
1689145 for (g = 0; g < sce->ics.num_swb; g++) {
481 1653032 int prevsc = sce->sf_idx[w*16+g];
482
3/4
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 1647615 times.
✓ Branch 2 taken 5417 times.
✗ Branch 3 not taken.
1653032 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
483
2/2
✓ Branch 1 taken 5239 times.
✓ Branch 2 taken 178 times.
5417 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
484 5239 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 1653032 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
489 1653032 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
490
2/2
✓ Branch 0 taken 388376 times.
✓ Branch 1 taken 1264656 times.
1653032 if (sce->sf_idx[w*16+g] != prevsc)
491 388376 fflag = 1;
492 1653032 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
493 }
494 }
495 34641 its++;
496
4/4
✓ Branch 0 taken 29596 times.
✓ Branch 1 taken 5045 times.
✓ Branch 2 taken 26687 times.
✓ Branch 3 taken 2909 times.
34641 } while (fflag && its < 10);
497 }
498
499 1230 static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
500 {
501 FFPsyBand *band;
502 int w, g, w2, i;
503 1230 int wlen = 1024 / sce->ics.num_windows;
504 int bandwidth, cutoff;
505 1230 float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
506 1230 float *NOR34 = &s->scoefs[3*128];
507 uint8_t nextband[128];
508 1230 const float lambda = s->lambda;
509 1230 const float freq_mult = avctx->sample_rate*0.5f/wlen;
510 1230 const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
511
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1230 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1230 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1230 const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
512 1230 const float dist_bias = av_clipf(4.f * 120 / lambda, 0.25f, 4.0f);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1230 times.
1230 const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
514
515 1230 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 1230 bandwidth = s->bandwidth;
521 1230 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
522
523 1230 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
524 1230 ff_init_nextband_map(sce, nextband);
525
2/2
✓ Branch 0 taken 1304 times.
✓ Branch 1 taken 1230 times.
2534 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
526 1304 int wstart = w*128;
527
2/2
✓ Branch 0 taken 56428 times.
✓ Branch 1 taken 1304 times.
57732 for (g = 0; g < sce->ics.num_swb; g++) {
528 int noise_sfi;
529 56428 float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
530 56428 float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
531 56428 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
532 56428 float min_energy = -1.0f, max_energy = 0.0f;
533 56428 const int start = wstart+sce->ics.swb_offset[g];
534 56428 const float freq = (start-wstart)*freq_mult;
535
2/2
✓ Branch 0 taken 28018 times.
✓ Branch 1 taken 28410 times.
56428 const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
536
4/4
✓ Branch 0 taken 29130 times.
✓ Branch 1 taken 27298 times.
✓ Branch 2 taken 2716 times.
✓ Branch 3 taken 26414 times.
56428 if (freq < NOISE_LOW_LIMIT || (start-wstart) >= cutoff) {
537
2/2
✓ Branch 0 taken 26973 times.
✓ Branch 1 taken 3041 times.
30014 if (!sce->zeroes[w*16+g])
538 26973 prev_sf = sce->sf_idx[w*16+g];
539 30014 continue;
540 }
541
2/2
✓ Branch 0 taken 28602 times.
✓ Branch 1 taken 26414 times.
55016 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
542 28602 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
543 28602 sfb_energy += band->energy;
544
2/2
✓ Branch 0 taken 27004 times.
✓ Branch 1 taken 1598 times.
28602 spread = FFMIN(spread, band->spread);
545 28602 threshold += band->threshold;
546
2/2
✓ Branch 0 taken 26414 times.
✓ Branch 1 taken 2188 times.
28602 if (!w2) {
547 26414 min_energy = max_energy = band->energy;
548 } else {
549
2/2
✓ Branch 0 taken 779 times.
✓ Branch 1 taken 1409 times.
2188 min_energy = FFMIN(min_energy, band->energy);
550
2/2
✓ Branch 0 taken 1278 times.
✓ Branch 1 taken 910 times.
2188 max_energy = FFMAX(max_energy, band->energy);
551 }
552 }
553
554 /* Ramps down at ~8000Hz and loosens the dist threshold */
555 26414 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 0 taken 26186 times.
✓ Branch 1 taken 228 times.
✓ Branch 3 taken 26186 times.
✗ Branch 4 not taken.
26414 if ((!sce->zeroes[w*16+g] && !ff_sfdelta_can_remove_band(sce, nextband, prev_sf, w*16+g)) ||
565
8/8
✓ Branch 0 taken 26186 times.
✓ Branch 1 taken 228 times.
✓ Branch 2 taken 3583 times.
✓ Branch 3 taken 22603 times.
✓ Branch 4 taken 3725 times.
✓ Branch 5 taken 86 times.
✓ Branch 6 taken 25978 times.
✓ Branch 7 taken 350 times.
26414 ((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.0f/freq_boost)) || spread < spread_threshold ||
566
5/6
✓ Branch 0 taken 25978 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22397 times.
✓ Branch 3 taken 3581 times.
✓ Branch 4 taken 12888 times.
✓ Branch 5 taken 9509 times.
25978 (!sce->zeroes[w*16+g] && sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost) ||
567
2/2
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 16262 times.
16469 min_energy < pns_transient_energy_r * max_energy ) {
568 10152 sce->pns_ener[w*16+g] = sfb_energy;
569
2/2
✓ Branch 0 taken 9924 times.
✓ Branch 1 taken 228 times.
10152 if (!sce->zeroes[w*16+g])
570 9924 prev_sf = sce->sf_idx[w*16+g];
571 10152 continue;
572 }
573
574
2/2
✓ Branch 0 taken 15627 times.
✓ Branch 1 taken 635 times.
16262 pns_tgt_energy = sfb_energy*FFMIN(1.0f, spread*spread);
575 16262 noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
576 16262 noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO]; /* Dequantize */
577
2/2
✓ Branch 0 taken 10793 times.
✓ Branch 1 taken 5469 times.
16262 if (prev != -1000) {
578 10793 int noise_sfdiff = noise_sfi - prev + SCALE_DIFF_ZERO;
579
2/4
✓ Branch 0 taken 10793 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10793 times.
10793 if (noise_sfdiff < 0 || noise_sfdiff > 2*SCALE_MAX_DIFF) {
580 ✗ if (!sce->zeroes[w*16+g])
581 ✗ prev_sf = sce->sf_idx[w*16+g];
582 ✗ continue;
583 }
584 }
585
2/2
✓ Branch 0 taken 16322 times.
✓ Branch 1 taken 16262 times.
32584 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
586 float band_energy, scale, pns_senergy;
587 16322 const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
588 16322 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
589
2/2
✓ Branch 0 taken 536788 times.
✓ Branch 1 taken 16322 times.
553110 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
590 536788 s->random_state = lcg_random(s->random_state);
591 536788 PNS[i] = s->random_state;
592 }
593 16322 band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
594 16322 scale = noise_amp/sqrtf(band_energy);
595 16322 s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
596 16322 pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
597 16322 pns_energy += pns_senergy;
598 16322 s->aacdsp.abs_pow34(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
599 16322 s->aacdsp.abs_pow34(PNS34, PNS, sce->ics.swb_sizes[g]);
600 32644 dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
601 NOR34,
602 16322 sce->ics.swb_sizes[g],
603 16322 sce->sf_idx[(w+w2)*16+g],
604 16322 sce->band_alt[(w+w2)*16+g],
605 16322 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 16322 dist2 += band->energy/(band->spread*band->spread)*lambda*dist_thresh/band->threshold;
608 }
609
3/4
✓ Branch 0 taken 16262 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2682 times.
✓ Branch 3 taken 13580 times.
16262 if (g && sce->band_type[w*16+g-1] == NOISE_BT) {
610 2682 dist2 += 5;
611 } else {
612 13580 dist2 += 9;
613 }
614 16262 energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
615 16262 sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
616
7/10
✓ Branch 0 taken 16262 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12874 times.
✓ Branch 3 taken 3388 times.
✓ Branch 4 taken 12502 times.
✓ Branch 5 taken 372 times.
✓ Branch 6 taken 12502 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 12502 times.
16262 if (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || (energy_ratio > 0.85f && energy_ratio < 1.25f && dist2 < dist1)) {
617 3388 sce->band_type[w*16+g] = NOISE_BT;
618 3388 sce->zeroes[w*16+g] = 0;
619 3388 prev = noise_sfi;
620 } else {
621
1/2
✓ Branch 0 taken 12874 times.
✗ Branch 1 not taken.
12874 if (!sce->zeroes[w*16+g])
622 12874 prev_sf = sce->sf_idx[w*16+g];
623 }
624 }
625 }
626 1230 }
627
628 4638 static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
629 {
630 FFPsyBand *band;
631 int w, g, w2;
632 4638 int wlen = 1024 / sce->ics.num_windows;
633 int bandwidth, cutoff;
634 4638 const float lambda = s->lambda;
635 4638 const float freq_mult = avctx->sample_rate*0.5f/wlen;
636
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4638 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4638 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4638 const float spread_threshold = FFMIN(0.75f, NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f));
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4638 times.
4638 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 4638 bandwidth = s->bandwidth;
643 4638 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
644
645 4638 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
646
2/2
✓ Branch 0 taken 4975 times.
✓ Branch 1 taken 4638 times.
9613 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
647
2/2
✓ Branch 0 taken 215437 times.
✓ Branch 1 taken 4975 times.
220412 for (g = 0; g < sce->ics.num_swb; g++) {
648 215437 float sfb_energy = 0.0f, threshold = 0.0f, spread = 2.0f;
649 215437 float min_energy = -1.0f, max_energy = 0.0f;
650 215437 const int start = sce->ics.swb_offset[g];
651 215437 const float freq = start*freq_mult;
652
2/2
✓ Branch 0 taken 106314 times.
✓ Branch 1 taken 109123 times.
215437 const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
653
4/4
✓ Branch 0 taken 110529 times.
✓ Branch 1 taken 104908 times.
✓ Branch 2 taken 19738 times.
✓ Branch 3 taken 90791 times.
215437 if (freq < NOISE_LOW_LIMIT || start >= cutoff) {
654 124646 sce->can_pns[w*16+g] = 0;
655 124646 continue;
656 }
657
2/2
✓ Branch 0 taken 101055 times.
✓ Branch 1 taken 90791 times.
191846 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
658 101055 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
659 101055 sfb_energy += band->energy;
660
2/2
✓ Branch 0 taken 93715 times.
✓ Branch 1 taken 7340 times.
101055 spread = FFMIN(spread, band->spread);
661 101055 threshold += band->threshold;
662
2/2
✓ Branch 0 taken 90791 times.
✓ Branch 1 taken 10264 times.
101055 if (!w2) {
663 90791 min_energy = max_energy = band->energy;
664 } else {
665
2/2
✓ Branch 0 taken 3630 times.
✓ Branch 1 taken 6634 times.
10264 min_energy = FFMIN(min_energy, band->energy);
666
2/2
✓ Branch 0 taken 5743 times.
✓ Branch 1 taken 4521 times.
10264 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 90791 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 0 taken 10050 times.
✓ Branch 1 taken 80741 times.
100841 int near = sfb_energy < 2.0f * threshold &&
680
2/2
✓ Branch 0 taken 9880 times.
✓ Branch 1 taken 170 times.
10050 sfb_energy > threshold * 0.25f;
681
2/2
✓ Branch 0 taken 9880 times.
✓ Branch 1 taken 80911 times.
90791 if (near) {
682 /* deletion candidate: noise beats the ~silent rendition */
683
2/2
✓ Branch 0 taken 9748 times.
✓ Branch 1 taken 132 times.
19628 sce->can_pns[w*16+g] = spread >= spread_threshold &&
684
1/2
✓ Branch 0 taken 9748 times.
✗ Branch 1 not taken.
9748 min_energy >= 0.2f * max_energy;
685
6/6
✓ Branch 0 taken 80741 times.
✓ Branch 1 taken 170 times.
✓ Branch 2 taken 80034 times.
✓ Branch 3 taken 707 times.
✓ Branch 4 taken 3489 times.
✓ Branch 5 taken 76545 times.
80911 } else if (sfb_energy < threshold*sqrtf(1.5f/freq_boost) || spread < spread_threshold || min_energy < pns_transient_energy_r * max_energy) {
686 4366 sce->can_pns[w*16+g] = 0;
687 } else {
688 76545 sce->can_pns[w*16+g] = 1;
689 }
690 }
691 }
692 }
693 4638 }
694
695 ✗ static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
696 {
697 ✗ int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
698 uint8_t nextband0[128], nextband1[128];
699 ✗ float *M = s->scoefs + 128*0, *S = s->scoefs + 128*1;
700 ✗ float *L34 = s->scoefs + 128*2, *R34 = s->scoefs + 128*3;
701 ✗ float *M34 = s->scoefs + 128*4, *S34 = s->scoefs + 128*5;
702 ✗ const float lambda = s->lambda;
703 ✗ const float mslambda = FFMIN(1.0f, lambda / 120.f);
704 ✗ SingleChannelElement *sce0 = &cpe->ch[0];
705 ✗ SingleChannelElement *sce1 = &cpe->ch[1];
706 ✗ if (!cpe->common_window)
707 ✗ 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 ✗ for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
716 ✗ start = 0;
717 ✗ for (g = 0; g < sce0->ics.num_swb; g++) {
718 ✗ float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
719 ✗ if (!cpe->is_mask[w*16+g])
720 ✗ cpe->ms_mask[w*16+g] = 0;
721 ✗ 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 ✗ for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
726 ✗ 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 ✗ for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
735 ✗ Mmax = FFMAX(Mmax, M34[i]);
736 ✗ Smax = FFMAX(Smax, S34[i]);
737 }
738 }
739
740 ✗ 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 ✗ if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
751 ✗ && ( !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
752 ✗ || !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 ✗ 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 ✗ float minthr = FFMIN(band0->threshold, band1->threshold);
768 int b1,b2,b3,b4;
769 ✗ 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 ✗ cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
810 ✗ if (cpe->ms_mask[w*16+g]) {
811 ✗ 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 ✗ } 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 ✗ } else if (B1 > B0) {
822 /* More boost won't fix this */
823 ✗ break;
824 }
825 }
826 }
827 ✗ if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
828 ✗ prev_mid = sce0->sf_idx[w*16+g];
829 ✗ 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