../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
342 / 375
91.2%
Branches:
314 / 364
86.3%
Functions:
2 / 2
100.0%

libavcodec/aaccoder_twoloop.h
Line Branch Exec Source
1 /*
2 * AAC encoder twoloop coder
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 encoder twoloop coder
25 * @author Konstantin Shishkov, Claudio Freire
26 */
27
28 /**
29 * This file contains a template for the twoloop coder function.
30 * It needs to be provided, externally, as an already included declaration,
31 * the following functions from aacenc_quantization/util.h. They're not included
32 * explicitly here to make it possible to provide alternative implementations:
33 * - quantize_band_cost
34 * - abs_pow34_v
35 * - find_max_val
36 * - find_min_book
37 * - find_form_factor
38 */
39
40 #ifndef AVCODEC_AACCODER_TWOLOOP_H
41 #define AVCODEC_AACCODER_TWOLOOP_H
42
43 #include <float.h>
44 #include "libavutil/mathematics.h"
45 #include "mathops.h"
46 #include "avcodec.h"
47 #include "put_bits.h"
48 #include "aac.h"
49 #include "aacenc.h"
50 #include "aactab.h"
51 #include "aacenctab.h"
52
53 /** Frequency in Hz for lower limit of noise substitution **/
54 #define NOISE_LOW_LIMIT 4000
55
56 /* Reflects the cost to change codebooks */
57 5825 static inline int ff_pns_bits(SingleChannelElement *sce, int w, int g)
58 {
59
4/6
✓ Branch 2 → 3 taken 5825 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 3757 times.
✓ Branch 3 → 5 taken 2068 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 3757 times.
5825 return (!g || !sce->zeroes[w*16+g-1] || !sce->can_pns[w*16+g-1]) ? 9 : 5;
60 }
61
62 /**
63 * two-loop quantizers search taken from ISO 13818-7 Appendix C
64 */
65 24 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
66 AACEncContext *s,
67 SingleChannelElement *sce,
68 const float lambda)
69 {
70 24 int start = 0, i, w, w2, g, recomprd;
71 48 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
72
1/2
✓ Branch 2 → 3 taken 24 times.
✗ Branch 2 → 4 not taken.
24 / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
73 24 * (lambda / 120.f);
74 int toomanybits, toofewbits;
75 char nzs[128];
76 uint8_t nextband[128];
77 int maxsf[128], minsf[128];
78 24 float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
79 float maxvals[128], spread_thr_r[128];
80 float min_spread_thr_r, max_spread_thr_r;
81
82 /**
83 * rdlambda controls the maximum tolerated distortion. Twoloop
84 * will keep iterating until it fails to lower it or it reaches
85 * ulimit * rdlambda. Keeping it low increases quality on difficult
86 * signals, but lower it too much, and bits will be taken from weak
87 * signals, creating "holes". A balance is necessary.
88 * rdmax and rdmin specify the relative deviation from rdlambda
89 * allowed for tonality compensation
90 */
91 24 float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f);
92 24 const float nzslope = 1.5f;
93 24 float rdmin = 0.03125f;
94 24 float rdmax = 1.0f;
95
96 /**
97 * sfoffs controls an offset of optmium allocation that will be
98 * applied based on lambda. Keep it real and modest, the loop
99 * will take care of the rest, this just accelerates convergence
100 */
101 24 float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10);
102
103 int fflag, minscaler, nminscaler;
104 24 int its = 0;
105 24 int maxits = 30;
106 24 int allz = 0;
107 int tbits;
108 24 int cutoff = 1024;
109 int pns_start_pos;
110 int prev;
111
112 /**
113 * zeroscale controls a multiplier of the threshold, if band energy
114 * is below this, a zero is forced. Keep it lower than 1, unless
115 * low lambda is used, because energy < threshold doesn't mean there's
116 * no audible signal outright, it's just energy. Also make it rise
117 * slower than rdlambda, as rdscale has due compensation with
118 * noisy band depriorization below, whereas zeroing logic is rather dumb
119 */
120 float zeroscale;
121
2/2
✓ Branch 5 → 6 taken 11 times.
✓ Branch 5 → 7 taken 13 times.
24 if (lambda > 120.f) {
122 11 zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f);
123 } else {
124 13 zeroscale = 1.f;
125 }
126
127
1/2
✓ Branch 8 → 9 taken 24 times.
✗ Branch 8 → 13 not taken.
24 if (s->psy.bitres.alloc >= 0) {
128 /**
129 * Psy granted us extra bits to use, from the reservoire
130 * adjust for lambda except what psy already did
131 */
132 48 destbits = s->psy.bitres.alloc
133
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 24 times.
24 * (lambda / (avctx->global_quality ? avctx->global_quality : 120));
134 }
135
136
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 18 taken 24 times.
24 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
137 /**
138 * Constant Q-scale doesn't compensate MS coding on its own
139 * No need to be overly precise, this only controls RD
140 * adjustment CB limits when going overboard
141 */
142
0/4
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 17 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (s->options.mid_side && s->cur_type == TYPE_CPE)
143 ✗ destbits *= 2;
144
145 /**
146 * When using a constant Q-scale, don't adjust bits, just use RD
147 * Don't let it go overboard, though... 8x psy target is enough
148 */
149 ✗ toomanybits = 5800;
150 ✗ toofewbits = destbits / 16;
151
152 /** Don't offset scalers, just RD */
153 ✗ sfoffs = sce->ics.num_windows - 1;
154 ✗ rdlambda = sqrtf(rdlambda);
155
156 /** search further */
157 ✗ maxits *= 2;
158 } else {
159 /* When using ABR, be strict, but a reasonable leeway is
160 * critical to allow RC to smoothly track desired bitrate
161 * without sudden quality drops that cause audible artifacts.
162 * Symmetry is also desirable, to avoid systematic bias.
163 */
164 24 toomanybits = destbits + destbits/8;
165 24 toofewbits = destbits - destbits/8;
166
167 24 sfoffs = 0;
168 24 rdlambda = sqrtf(rdlambda);
169 }
170
171 /** and zero out above cutoff frequency */
172 {
173 24 int wlen = 1024 / sce->ics.num_windows;
174 /* the bandwidth is fixed at init and shared with the psy model */
175 24 cutoff = s->bandwidth * 2 * wlen / avctx->sample_rate;
176 24 pns_start_pos = NOISE_LOW_LIMIT * 2 * wlen / avctx->sample_rate;
177 }
178
179 /**
180 * for values above this the decoder might end up in an endless loop
181 * due to always having more bits than what can be encoded.
182 */
183 24 destbits = FFMIN(destbits, 5800);
184 24 toomanybits = FFMIN(toomanybits, 5800);
185 24 toofewbits = FFMIN(toofewbits, 5800);
186 /**
187 * XXX: some heuristic to determine initial quantizers will reduce search time
188 * determine zero bands and upper distortion limits
189 */
190 24 min_spread_thr_r = -1;
191 24 max_spread_thr_r = -1;
192
2/2
✓ Branch 51 → 20 taken 72 times.
✓ Branch 51 → 52 taken 24 times.
96 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
193
2/2
✓ Branch 49 → 21 taken 1218 times.
✓ Branch 49 → 50 taken 72 times.
1290 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
194 1218 int nz = 0;
195 1218 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
196
2/2
✓ Branch 28 → 22 taken 2310 times.
✓ Branch 28 → 29 taken 1218 times.
3528 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
197 2310 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
198
5/6
✓ Branch 22 → 23 taken 1992 times.
✓ Branch 22 → 25 taken 318 times.
✓ Branch 23 → 24 taken 936 times.
✓ Branch 23 → 25 taken 1056 times.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 936 times.
2310 if (start >= cutoff || band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f) {
199 1374 sce->zeroes[(w+w2)*16+g] = 1;
200 1374 continue;
201 }
202 936 nz = 1;
203 }
204
2/2
✓ Branch 29 → 30 taken 858 times.
✓ Branch 29 → 31 taken 360 times.
1218 if (!nz) {
205 858 uplim = 0.0f;
206 } else {
207 360 nz = 0;
208
2/2
✓ Branch 37 → 32 taken 936 times.
✓ Branch 37 → 38 taken 360 times.
1296 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
209 936 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
210
2/4
✓ Branch 32 → 33 taken 936 times.
✗ Branch 32 → 34 not taken.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 936 times.
936 if (band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f)
211 ✗ continue;
212 936 uplim += band->threshold;
213 936 energy += band->energy;
214 936 spread += band->spread;
215 936 nz++;
216 }
217 }
218 1218 uplims[w*16+g] = uplim;
219 1218 energies[w*16+g] = energy;
220 1218 nzs[w*16+g] = nz;
221 1218 sce->zeroes[w*16+g] = !nz;
222 1218 allz |= nz;
223
4/4
✓ Branch 38 → 39 taken 360 times.
✓ Branch 38 → 48 taken 858 times.
✓ Branch 39 → 40 taken 151 times.
✓ Branch 39 → 48 taken 209 times.
1218 if (nz && sce->can_pns[w*16+g]) {
224 151 spread_thr_r[w*16+g] = energy * nz / (uplim * spread);
225
2/2
✓ Branch 40 → 41 taken 12 times.
✓ Branch 40 → 42 taken 139 times.
151 if (min_spread_thr_r < 0) {
226 12 min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+g];
227 } else {
228
2/2
✓ Branch 42 → 43 taken 6 times.
✓ Branch 42 → 44 taken 133 times.
139 min_spread_thr_r = FFMIN(min_spread_thr_r, spread_thr_r[w*16+g]);
229
2/2
✓ Branch 45 → 46 taken 101 times.
✓ Branch 45 → 47 taken 38 times.
139 max_spread_thr_r = FFMAX(max_spread_thr_r, spread_thr_r[w*16+g]);
230 }
231 }
232 }
233 }
234
235 /** Compute initial scalers */
236 24 minscaler = 65535;
237
2/2
✓ Branch 63 → 53 taken 72 times.
✓ Branch 63 → 64 taken 24 times.
96 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
238
2/2
✓ Branch 61 → 54 taken 1218 times.
✓ Branch 61 → 62 taken 72 times.
1290 for (g = 0; g < sce->ics.num_swb; g++) {
239
2/2
✓ Branch 54 → 55 taken 858 times.
✓ Branch 54 → 56 taken 360 times.
1218 if (sce->zeroes[w*16+g]) {
240 858 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
241 858 continue;
242 }
243 /**
244 * log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2).
245 * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion,
246 * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus
247 * more robust.
248 */
249 360 sce->sf_idx[w*16+g] = av_clip(
250 SCALE_ONE_POS
251
1/2
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 58 taken 360 times.
360 + 1.75*log2f(FFMAX(0.00125f,uplims[w*16+g]) / sce->ics.swb_sizes[g])
252 360 + sfoffs,
253 60, SCALE_MAX_POS);
254 360 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
255 }
256 }
257
258 /** Clip */
259 24 minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
260
2/2
✓ Branch 71 → 65 taken 72 times.
✓ Branch 71 → 72 taken 24 times.
96 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
261
2/2
✓ Branch 69 → 66 taken 1218 times.
✓ Branch 69 → 70 taken 72 times.
1290 for (g = 0; g < sce->ics.num_swb; g++)
262
2/2
✓ Branch 66 → 67 taken 360 times.
✓ Branch 66 → 68 taken 858 times.
1218 if (!sce->zeroes[w*16+g])
263 360 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1);
264
265
2/2
✓ Branch 72 → 73 taken 12 times.
✓ Branch 72 → 74 taken 12 times.
24 if (!allz)
266 12 return;
267 12 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
268 12 ff_quantize_band_cost_cache_init(s);
269
270
2/2
✓ Branch 78 → 77 taken 1536 times.
✓ Branch 78 → 79 taken 12 times.
1548 for (i = 0; i < sizeof(minsf) / sizeof(minsf[0]); ++i)
271 1536 minsf[i] = 0;
272
2/2
✓ Branch 90 → 80 taken 42 times.
✓ Branch 90 → 91 taken 12 times.
54 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
273 42 start = w*128;
274
2/2
✓ Branch 88 → 81 taken 588 times.
✓ Branch 88 → 89 taken 42 times.
630 for (g = 0; g < sce->ics.num_swb; g++) {
275 588 const float *scaled = s->scoefs + start;
276 int minsfidx;
277 588 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
278
2/2
✓ Branch 82 → 83 taken 504 times.
✓ Branch 82 → 87 taken 84 times.
588 if (maxvals[w*16+g] > 0) {
279 504 minsfidx = coef2minsf(maxvals[w*16+g]);
280
2/2
✓ Branch 86 → 85 taken 1176 times.
✓ Branch 86 → 87 taken 504 times.
1680 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
281 1176 minsf[(w+w2)*16+g] = minsfidx;
282 }
283 588 start += sce->ics.swb_sizes[g];
284 }
285 }
286
287 /**
288 * Scale uplims to match rate distortion to quality
289 * bu applying noisy band depriorization and tonal band prioritization.
290 * Maxval-energy ratio gives us an idea of how noisy/tonal the band is.
291 * If maxval^2 ~ energy, then that band is mostly noise, and we can relax
292 * rate distortion requirements.
293 */
294 12 memcpy(euplims, uplims, sizeof(euplims));
295
2/2
✓ Branch 121 → 92 taken 42 times.
✓ Branch 121 → 122 taken 12 times.
54 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
296 /** psy already prioritizes transients to some extent */
297
1/2
✓ Branch 92 → 93 taken 42 times.
✗ Branch 92 → 94 not taken.
42 float de_psy_factor = (sce->ics.num_windows > 1) ? 8.0f / sce->ics.group_len[w] : 1.0f;
298 42 start = w*128;
299
2/2
✓ Branch 119 → 96 taken 588 times.
✓ Branch 119 → 120 taken 42 times.
630 for (g = 0; g < sce->ics.num_swb; g++) {
300
2/2
✓ Branch 96 → 97 taken 288 times.
✓ Branch 96 → 118 taken 300 times.
588 if (nzs[g] > 0) {
301 288 float cleanup_factor = ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
302 288 float energy2uplim = find_form_factor(
303 288 sce->ics.group_len[w], sce->ics.swb_sizes[g],
304 288 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
305 288 sce->coeffs + start,
306 nzslope * cleanup_factor);
307 288 energy2uplim *= de_psy_factor;
308
1/2
✓ Branch 98 → 99 taken 288 times.
✗ Branch 98 → 100 not taken.
288 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
309 /** In ABR, we need to prioritize less and let rate control do its thing */
310 288 energy2uplim = sqrtf(energy2uplim);
311 }
312
5/6
✓ Branch 100 → 101 taken 156 times.
✓ Branch 100 → 102 taken 132 times.
✓ Branch 101 → 102 taken 156 times.
✗ Branch 101 → 106 not taken.
✓ Branch 102 → 103 taken 156 times.
✓ Branch 102 → 104 taken 132 times.
288 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
313 288 uplims[w*16+g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
314 288 * sce->ics.group_len[w];
315
316 288 energy2uplim = find_form_factor(
317 288 sce->ics.group_len[w], sce->ics.swb_sizes[g],
318 288 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
319 288 sce->coeffs + start,
320 2.0f);
321 288 energy2uplim *= de_psy_factor;
322
1/2
✓ Branch 108 → 109 taken 288 times.
✗ Branch 108 → 110 not taken.
288 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
323 /** In ABR, we need to prioritize less and let rate control do its thing */
324 288 energy2uplim = sqrtf(energy2uplim);
325 }
326
5/6
✓ Branch 110 → 111 taken 156 times.
✓ Branch 110 → 112 taken 132 times.
✓ Branch 111 → 112 taken 156 times.
✗ Branch 111 → 116 not taken.
✓ Branch 112 → 113 taken 156 times.
✓ Branch 112 → 114 taken 132 times.
288 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
327 288 euplims[w*16+g] *= av_clipf(rdlambda * energy2uplim * sce->ics.group_len[w],
328 0.5f, 1.0f);
329 }
330 588 start += sce->ics.swb_sizes[g];
331 }
332 }
333
334
2/2
✓ Branch 124 → 123 taken 1536 times.
✓ Branch 124 → 125 taken 12 times.
1548 for (i = 0; i < sizeof(maxsf) / sizeof(maxsf[0]); ++i)
335 1536 maxsf[i] = SCALE_MAX_POS;
336
337 //perform two-loop search
338 //outer loop - improve quality
339 do {
340 //inner loop - quantize spectrum to fit into given number of bits
341 int overdist;
342
2/2
✓ Branch 125 → 126 taken 328 times.
✓ Branch 125 → 127 taken 12 times.
340 int qstep = its ? 1 : 32;
343 do {
344 2243 int changed = 0;
345 2243 prev = -1;
346 2243 recomprd = 0;
347 2243 tbits = 0;
348
2/2
✓ Branch 148 → 130 taken 8364 times.
✓ Branch 148 → 149 taken 2243 times.
10607 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
349 8364 start = w*128;
350
2/2
✓ Branch 146 → 131 taken 117096 times.
✓ Branch 146 → 147 taken 8364 times.
125460 for (g = 0; g < sce->ics.num_swb; g++) {
351 117096 const float *coefs = &sce->coeffs[start];
352 117096 const float *scaled = &s->scoefs[start];
353 117096 int bits = 0;
354 int cb;
355 117096 float dist = 0.0f;
356 117096 float qenergy = 0.0f;
357
358
4/4
✓ Branch 131 → 132 taken 69598 times.
✓ Branch 131 → 133 taken 47498 times.
✓ Branch 132 → 133 taken 930 times.
✓ Branch 132 → 137 taken 68668 times.
117096 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
359 48428 start += sce->ics.swb_sizes[g];
360
2/2
✓ Branch 133 → 134 taken 4339 times.
✓ Branch 133 → 136 taken 44089 times.
48428 if (sce->can_pns[w*16+g]) {
361 /** PNS isn't free */
362 4339 tbits += ff_pns_bits(sce, w, g);
363 }
364 48428 continue;
365 }
366 68668 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
367
2/2
✓ Branch 141 → 139 taken 157591 times.
✓ Branch 141 → 142 taken 68668 times.
226259 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
368 int b;
369 float sqenergy;
370 315182 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
371 157591 scaled + w2*128,
372 157591 sce->ics.swb_sizes[g],
373 157591 sce->sf_idx[w*16+g],
374 cb,
375 1.0f,
376 INFINITY,
377 &b, &sqenergy,
378 0);
379 157591 bits += b;
380 157591 qenergy += sqenergy;
381 }
382 68668 dists[w*16+g] = dist - bits;
383 68668 qenergies[w*16+g] = qenergy;
384
2/2
✓ Branch 142 → 143 taken 66445 times.
✓ Branch 142 → 144 taken 2223 times.
68668 if (prev != -1) {
385 66445 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
386 66445 bits += ff_aac_scalefactor_bits[sfdiff];
387 }
388 68668 tbits += bits;
389 68668 start += sce->ics.swb_sizes[g];
390 68668 prev = sce->sf_idx[w*16+g];
391 }
392 }
393
2/2
✓ Branch 149 → 150 taken 2191 times.
✓ Branch 149 → 159 taken 52 times.
2243 if (tbits > toomanybits) {
394 2191 recomprd = 1;
395
2/2
✓ Branch 158 → 151 taken 280448 times.
✓ Branch 158 → 166 taken 2191 times.
282639 for (i = 0; i < 128; i++) {
396
2/2
✓ Branch 151 → 152 taken 104797 times.
✓ Branch 151 → 157 taken 175651 times.
280448 if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) {
397
2/2
✓ Branch 152 → 153 taken 89786 times.
✓ Branch 152 → 154 taken 15011 times.
104797 int maxsf_i = (tbits > 5800) ? SCALE_MAX_POS : maxsf[i];
398 104797 int new_sf = FFMIN(maxsf_i, sce->sf_idx[i] + qstep);
399
2/2
✓ Branch 155 → 156 taken 70619 times.
✓ Branch 155 → 157 taken 34178 times.
104797 if (new_sf != sce->sf_idx[i]) {
400 70619 sce->sf_idx[i] = new_sf;
401 70619 changed = 1;
402 }
403 }
404 }
405
2/2
✓ Branch 159 → 160 taken 29 times.
✓ Branch 159 → 166 taken 23 times.
52 } else if (tbits < toofewbits) {
406 29 recomprd = 1;
407
2/2
✓ Branch 165 → 161 taken 3712 times.
✓ Branch 165 → 166 taken 29 times.
3741 for (i = 0; i < 128; i++) {
408
2/2
✓ Branch 161 → 162 taken 2745 times.
✓ Branch 161 → 164 taken 967 times.
3712 if (sce->sf_idx[i] > SCALE_ONE_POS) {
409 2745 int new_sf = FFMAX3(minsf[i], SCALE_ONE_POS, sce->sf_idx[i] - qstep);
410
1/2
✓ Branch 162 → 163 taken 2745 times.
✗ Branch 162 → 164 not taken.
2745 if (new_sf != sce->sf_idx[i]) {
411 2745 sce->sf_idx[i] = new_sf;
412 2745 changed = 1;
413 }
414 }
415 }
416 }
417 2243 qstep >>= 1;
418
8/8
✓ Branch 166 → 167 taken 2183 times.
✓ Branch 166 → 171 taken 60 times.
✓ Branch 167 → 168 taken 2155 times.
✓ Branch 167 → 171 taken 28 times.
✓ Branch 168 → 169 taken 2012 times.
✓ Branch 168 → 171 taken 143 times.
✓ Branch 169 → 170 taken 1843 times.
✓ Branch 169 → 171 taken 169 times.
2243 if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217 && changed)
419 1843 qstep = 1;
420
2/2
✓ Branch 171 → 129 taken 1903 times.
✓ Branch 171 → 172 taken 340 times.
2243 } while (qstep);
421
422 340 overdist = 1;
423 340 fflag = tbits < toofewbits;
424
5/6
✓ Branch 274 → 275 taken 680 times.
✓ Branch 274 → 277 taken 332 times.
✓ Branch 275 → 173 taken 672 times.
✓ Branch 275 → 276 taken 8 times.
✗ Branch 276 → 173 not taken.
✓ Branch 276 → 277 taken 8 times.
1012 for (i = 0; i < 2 && (overdist || recomprd); ++i) {
425
2/2
✓ Branch 173 → 174 taken 545 times.
✓ Branch 173 → 194 taken 127 times.
672 if (recomprd) {
426 /** Must recompute distortion */
427 545 prev = -1;
428 545 tbits = 0;
429
2/2
✓ Branch 193 → 175 taken 1936 times.
✓ Branch 193 → 194 taken 545 times.
2481 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
430 1936 start = w*128;
431
2/2
✓ Branch 191 → 176 taken 27104 times.
✓ Branch 191 → 192 taken 1936 times.
29040 for (g = 0; g < sce->ics.num_swb; g++) {
432 27104 const float *coefs = sce->coeffs + start;
433 27104 const float *scaled = s->scoefs + start;
434 27104 int bits = 0;
435 int cb;
436 27104 float dist = 0.0f;
437 27104 float qenergy = 0.0f;
438
439
4/4
✓ Branch 176 → 177 taken 15392 times.
✓ Branch 176 → 178 taken 11712 times.
✓ Branch 177 → 178 taken 408 times.
✓ Branch 177 → 182 taken 14984 times.
27104 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
440 12120 start += sce->ics.swb_sizes[g];
441
2/2
✓ Branch 178 → 179 taken 1486 times.
✓ Branch 178 → 181 taken 10634 times.
12120 if (sce->can_pns[w*16+g]) {
442 /** PNS isn't free */
443 1486 tbits += ff_pns_bits(sce, w, g);
444 }
445 12120 continue;
446 }
447 14984 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
448
2/2
✓ Branch 186 → 184 taken 38021 times.
✓ Branch 186 → 187 taken 14984 times.
53005 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
449 int b;
450 float sqenergy;
451 76042 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
452 38021 scaled + w2*128,
453 38021 sce->ics.swb_sizes[g],
454 38021 sce->sf_idx[w*16+g],
455 cb,
456 1.0f,
457 INFINITY,
458 &b, &sqenergy,
459 0);
460 38021 bits += b;
461 38021 qenergy += sqenergy;
462 }
463 14984 dists[w*16+g] = dist - bits;
464 14984 qenergies[w*16+g] = qenergy;
465
2/2
✓ Branch 187 → 188 taken 14449 times.
✓ Branch 187 → 189 taken 535 times.
14984 if (prev != -1) {
466 14449 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
467 14449 bits += ff_aac_scalefactor_bits[sfdiff];
468 }
469 14984 tbits += bits;
470 14984 start += sce->ics.swb_sizes[g];
471 14984 prev = sce->sf_idx[w*16+g];
472 }
473 }
474 }
475
6/8
✓ Branch 194 → 195 taken 340 times.
✓ Branch 194 → 273 taken 332 times.
✓ Branch 195 → 196 taken 340 times.
✗ Branch 195 → 273 not taken.
✓ Branch 196 → 197 taken 154 times.
✓ Branch 196 → 273 taken 186 times.
✓ Branch 197 → 198 taken 154 times.
✗ Branch 197 → 273 not taken.
672 if (!i && s->options.pns && its > maxits/2 && tbits > toofewbits) {
476 154 float maxoverdist = 0.0f;
477 154 float ovrfactor = 1.f+(maxits-its)*16.f/maxits;
478 154 overdist = recomprd = 0;
479
2/2
✓ Branch 213 → 199 taken 546 times.
✓ Branch 213 → 214 taken 154 times.
700 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
480
2/2
✓ Branch 211 → 200 taken 7644 times.
✓ Branch 211 → 212 taken 546 times.
8190 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
481
6/6
✓ Branch 200 → 201 taken 3753 times.
✓ Branch 200 → 210 taken 3891 times.
✓ Branch 201 → 202 taken 2368 times.
✓ Branch 201 → 210 taken 1385 times.
✓ Branch 202 → 203 taken 1269 times.
✓ Branch 202 → 210 taken 1099 times.
7644 if (!sce->zeroes[w*16+g] && sce->sf_idx[w*16+g] > SCALE_ONE_POS && dists[w*16+g] > uplims[w*16+g]*ovrfactor) {
482
2/2
✓ Branch 203 → 204 taken 19 times.
✓ Branch 203 → 205 taken 1250 times.
1269 float ovrdist = dists[w*16+g] / FFMAX(uplims[w*16+g],euplims[w*16+g]);
483
2/2
✓ Branch 206 → 207 taken 916 times.
✓ Branch 206 → 208 taken 353 times.
1269 maxoverdist = FFMAX(maxoverdist, ovrdist);
484 1269 overdist++;
485 }
486 }
487 }
488
2/2
✓ Branch 214 → 215 taken 146 times.
✓ Branch 214 → 272 taken 8 times.
154 if (overdist) {
489 /* We have overdistorted bands, trade for zeroes (that can be noise)
490 * Zero the bands in the lowest 1.25% spread-energy-threshold ranking
491 */
492 146 float minspread = max_spread_thr_r;
493 146 float maxspread = min_spread_thr_r;
494 float zspread;
495 146 int zeroable = 0;
496 146 int zeroed = 0;
497 int maxzeroed, zloop;
498
2/2
✓ Branch 230 → 216 taken 522 times.
✓ Branch 230 → 231 taken 146 times.
668 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
499
2/2
✓ Branch 228 → 217 taken 7308 times.
✓ Branch 228 → 229 taken 522 times.
7830 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
500
6/6
✓ Branch 217 → 218 taken 4176 times.
✓ Branch 217 → 227 taken 3132 times.
✓ Branch 218 → 219 taken 1353 times.
✓ Branch 218 → 227 taken 2823 times.
✓ Branch 219 → 220 taken 1110 times.
✓ Branch 219 → 227 taken 243 times.
7308 if (start >= pns_start_pos && !sce->zeroes[w*16+g] && sce->can_pns[w*16+g]) {
501
2/2
✓ Branch 220 → 221 taken 287 times.
✓ Branch 220 → 222 taken 823 times.
1110 minspread = FFMIN(minspread, spread_thr_r[w*16+g]);
502
2/2
✓ Branch 223 → 224 taken 669 times.
✓ Branch 223 → 225 taken 441 times.
1110 maxspread = FFMAX(maxspread, spread_thr_r[w*16+g]);
503 1110 zeroable++;
504 }
505 }
506 }
507 146 zspread = (maxspread-minspread) * 0.0125f + minspread;
508 /* Don't PNS everything even if allowed. It suppresses bit starvation signals from RC,
509 * and forced the hand of the later search_for_pns step.
510 * Instead, PNS a fraction of the spread_thr_r range depending on how starved for bits we are,
511 * and leave further PNSing to search_for_pns if worthwhile.
512 */
513
5/6
✓ Branch 231 → 232 taken 89 times.
✓ Branch 231 → 233 taken 57 times.
✗ Branch 234 → 235 not taken.
✓ Branch 234 → 236 taken 146 times.
✓ Branch 236 → 237 taken 89 times.
✓ Branch 236 → 238 taken 57 times.
146 zspread = FFMIN3(min_spread_thr_r * 8.f, zspread,
514 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
515 146 maxzeroed = FFMIN(zeroable, FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
516
2/2
✓ Branch 269 → 240 taken 292 times.
✓ Branch 269 → 270 taken 146 times.
438 for (zloop = 0; zloop < 2; zloop++) {
517 /* Two passes: first distorted stuff - two birds in one shot and all that,
518 * then anything viable. Viable means not zero, but either CB=zero-able
519 * (too high SF), not SF <= 1 (that means we'd be operating at very high
520 * quality, we don't want PNS when doing VHQ), PNS allowed, and within
521 * the lowest ranking percentile.
522 */
523
2/2
✓ Branch 240 → 241 taken 146 times.
✓ Branch 240 → 242 taken 146 times.
292 float loopovrfactor = (zloop) ? 1.0f : ovrfactor;
524
2/2
✓ Branch 243 → 244 taken 146 times.
✓ Branch 243 → 245 taken 146 times.
292 int loopminsf = (zloop) ? (SCALE_ONE_POS - SCALE_DIV_512) : SCALE_ONE_POS;
525 int mcb;
526
4/4
✓ Branch 266 → 267 taken 3583 times.
✓ Branch 266 → 268 taken 269 times.
✓ Branch 267 → 247 taken 3560 times.
✓ Branch 267 → 268 taken 23 times.
3852 for (g = sce->ics.num_swb-1; g > 0 && zeroed < maxzeroed; g--) {
527
2/2
✓ Branch 247 → 248 taken 1345 times.
✓ Branch 247 → 249 taken 2215 times.
3560 if (sce->ics.swb_offset[g] < pns_start_pos)
528 1345 continue;
529
2/2
✓ Branch 264 → 250 taken 7921 times.
✓ Branch 264 → 265 taken 2215 times.
10136 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
530
6/6
✓ Branch 250 → 251 taken 2477 times.
✓ Branch 250 → 263 taken 5444 times.
✓ Branch 251 → 252 taken 2066 times.
✓ Branch 251 → 263 taken 411 times.
✓ Branch 252 → 253 taken 199 times.
✓ Branch 252 → 263 taken 1867 times.
7921 if (!sce->zeroes[w*16+g] && sce->can_pns[w*16+g] && spread_thr_r[w*16+g] <= zspread
531
2/2
✓ Branch 253 → 254 taken 133 times.
✓ Branch 253 → 263 taken 66 times.
199 && sce->sf_idx[w*16+g] > loopminsf
532
3/4
✓ Branch 254 → 255 taken 75 times.
✓ Branch 254 → 262 taken 58 times.
✓ Branch 256 → 257 taken 75 times.
✗ Branch 256 → 262 not taken.
133 && (dists[w*16+g] > loopovrfactor*uplims[w*16+g] || !(mcb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]))
533
5/6
✓ Branch 257 → 258 taken 39 times.
✓ Branch 257 → 263 taken 36 times.
✓ Branch 258 → 259 taken 31 times.
✓ Branch 258 → 260 taken 8 times.
✓ Branch 261 → 262 taken 39 times.
✗ Branch 261 → 263 not taken.
75 || (mcb <= 1 && dists[w*16+g] > FFMIN(uplims[w*16+g], euplims[w*16+g]))) ) {
534 97 sce->zeroes[w*16+g] = 1;
535 97 sce->band_type[w*16+g] = 0;
536 97 zeroed++;
537 }
538 }
539 }
540 }
541
2/2
✓ Branch 270 → 271 taken 55 times.
✓ Branch 270 → 273 taken 91 times.
146 if (zeroed)
542 55 recomprd = fflag = 1;
543 } else {
544 8 overdist = 0;
545 }
546 }
547 }
548
549 340 minscaler = SCALE_MAX_POS;
550
2/2
✓ Branch 284 → 278 taken 1200 times.
✓ Branch 284 → 285 taken 340 times.
1540 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
551
2/2
✓ Branch 282 → 279 taken 16800 times.
✓ Branch 282 → 283 taken 1200 times.
18000 for (g = 0; g < sce->ics.num_swb; g++) {
552
2/2
✓ Branch 279 → 280 taken 9272 times.
✓ Branch 279 → 281 taken 7528 times.
16800 if (!sce->zeroes[w*16+g]) {
553 9272 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
554 }
555 }
556 }
557
558 340 minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
559 340 prev = -1;
560
2/2
✓ Branch 368 → 286 taken 1200 times.
✓ Branch 368 → 369 taken 340 times.
1540 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
561 /** Start with big steps, end up fine-tunning */
562
4/4
✓ Branch 286 → 287 taken 546 times.
✓ Branch 286 → 290 taken 654 times.
✓ Branch 287 → 288 taken 351 times.
✓ Branch 287 → 289 taken 195 times.
1200 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
563 1200 int edepth = depth+2;
564 1200 float uplmax = its / (maxits*0.25f) + 1.0f;
565
6/8
✓ Branch 291 → 292 taken 1187 times.
✓ Branch 291 → 301 taken 13 times.
✓ Branch 292 → 293 taken 1187 times.
✗ Branch 292 → 294 not taken.
✓ Branch 295 → 296 taken 100 times.
✓ Branch 295 → 300 taken 1087 times.
✓ Branch 296 → 297 taken 100 times.
✗ Branch 296 → 298 not taken.
1200 uplmax *= (tbits > destbits) ? FFMIN(2.0f, tbits / (float)FFMAX(1,destbits)) : 1.0f;
566 1200 start = w * 128;
567
2/2
✓ Branch 366 → 303 taken 16800 times.
✓ Branch 366 → 367 taken 1200 times.
18000 for (g = 0; g < sce->ics.num_swb; g++) {
568 16800 int prevsc = sce->sf_idx[w*16+g];
569
4/4
✓ Branch 303 → 304 taken 2580 times.
✓ Branch 303 → 306 taken 14220 times.
✓ Branch 304 → 305 taken 340 times.
✓ Branch 304 → 306 taken 2240 times.
16800 if (prev < 0 && !sce->zeroes[w*16+g])
570 340 prev = sce->sf_idx[0];
571
2/2
✓ Branch 306 → 307 taken 9272 times.
✓ Branch 306 → 365 taken 7528 times.
16800 if (!sce->zeroes[w*16+g]) {
572 9272 const float *coefs = sce->coeffs + start;
573 9272 const float *scaled = s->scoefs + start;
574 9272 int cmb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
575 9272 int mindeltasf = FFMAX(0, prev - SCALE_MAX_DIFF);
576 9272 int maxdeltasf = FFMIN(SCALE_MAX_POS - SCALE_DIV_512, prev + SCALE_MAX_DIFF);
577
6/6
✓ Branch 308 → 309 taken 8296 times.
✓ Branch 308 → 310 taken 976 times.
✓ Branch 309 → 310 taken 6008 times.
✓ Branch 309 → 338 taken 2288 times.
✓ Branch 310 → 311 taken 6953 times.
✓ Branch 310 → 338 taken 31 times.
9272 if ((!cmb || dists[w*16+g] > uplims[w*16+g]) && sce->sf_idx[w*16+g] > FFMAX(mindeltasf, minsf[w*16+g])) {
578 /* Try to make sure there is some energy in every nonzero band
579 * NOTE: This algorithm must be forcibly imbalanced, pushing harder
580 * on holes or more distorted bands at first, otherwise there's
581 * no net gain (since the next iteration will offset all bands
582 * on the opposite direction to compensate for extra bits)
583 */
584
4/4
✓ Branch 335 → 336 taken 53154 times.
✓ Branch 335 → 337 taken 3919 times.
✓ Branch 336 → 312 taken 53048 times.
✓ Branch 336 → 337 taken 106 times.
57073 for (i = 0; i < edepth && sce->sf_idx[w*16+g] > mindeltasf; ++i) {
585 int cb, bits;
586 float dist, qenergy;
587 53048 int mb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1);
588 53048 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
589 53048 dist = qenergy = 0.f;
590 53048 bits = 0;
591
2/2
✓ Branch 314 → 315 taken 10035 times.
✓ Branch 314 → 316 taken 43013 times.
53048 if (!cb) {
592 10035 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g]-1, maxsf[w*16+g]);
593
4/4
✓ Branch 316 → 317 taken 8139 times.
✓ Branch 316 → 319 taken 34874 times.
✓ Branch 317 → 318 taken 1408 times.
✓ Branch 317 → 319 taken 6731 times.
43013 } else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) {
594 1408 break;
595 }
596 /* !g is the DC band, it's important, since quantization error here
597 * applies to less than a cycle, it creates horrible intermodulation
598 * distortion if it doesn't stick to what psy requests
599 */
600
5/6
✓ Branch 319 → 320 taken 2020 times.
✓ Branch 319 → 323 taken 49620 times.
✓ Branch 320 → 321 taken 2020 times.
✗ Branch 320 → 323 not taken.
✓ Branch 321 → 322 taken 1756 times.
✓ Branch 321 → 323 taken 264 times.
51640 if (!g && sce->ics.num_windows > 1 && dists[w*16+g] >= euplims[w*16+g])
601 1756 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
602
2/2
✓ Branch 326 → 324 taken 119553 times.
✓ Branch 326 → 327 taken 51640 times.
171193 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
603 int b;
604 float sqenergy;
605 239106 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
606 119553 scaled + w2*128,
607 119553 sce->ics.swb_sizes[g],
608 119553 sce->sf_idx[w*16+g]-1,
609 cb,
610 1.0f,
611 INFINITY,
612 &b, &sqenergy,
613 0);
614 119553 bits += b;
615 119553 qenergy += sqenergy;
616 }
617 51640 sce->sf_idx[w*16+g]--;
618 51640 dists[w*16+g] = dist - bits;
619 51640 qenergies[w*16+g] = qenergy;
620
5/6
✓ Branch 327 → 328 taken 41887 times.
✓ Branch 327 → 334 taken 9753 times.
✓ Branch 328 → 329 taken 41887 times.
✗ Branch 328 → 337 not taken.
✓ Branch 332 → 333 taken 5729 times.
✓ Branch 332 → 334 taken 36158 times.
93527 if (mb && (sce->sf_idx[w*16+g] < mindeltasf || (
621
2/2
✓ Branch 329 → 330 taken 41838 times.
✓ Branch 329 → 331 taken 49 times.
41887 (dists[w*16+g] < FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g]))
622
2/2
✓ Branch 333 → 334 taken 4209 times.
✓ Branch 333 → 337 taken 1520 times.
5729 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
623 ) )) {
624 break;
625 }
626 }
627
3/4
✓ Branch 338 → 339 taken 2319 times.
✗ Branch 338 → 361 not taken.
✓ Branch 339 → 340 taken 1510 times.
✓ Branch 339 → 361 taken 809 times.
2319 } else if (tbits > toofewbits && sce->sf_idx[w*16+g] < FFMIN(maxdeltasf, maxsf[w*16+g])
628
2/4
✗ Branch 340 → 341 not taken.
✓ Branch 340 → 342 taken 1510 times.
✓ Branch 343 → 344 taken 1510 times.
✗ Branch 343 → 361 not taken.
1510 && (dists[w*16+g] < FFMIN(euplims[w*16+g], uplims[w*16+g]))
629
1/2
✗ Branch 344 → 345 not taken.
✓ Branch 344 → 361 taken 1510 times.
1510 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
630 ) {
631 /** Um... over target. Save bits for more important stuff. */
632
0/4
✗ Branch 359 → 360 not taken.
✗ Branch 359 → 361 not taken.
✗ Branch 360 → 346 not taken.
✗ Branch 360 → 361 not taken.
✗ for (i = 0; i < depth && sce->sf_idx[w*16+g] < maxdeltasf; ++i) {
633 int cb, bits;
634 float dist, qenergy;
635 ✗ cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]+1);
636
0/2
✗ Branch 347 → 348 not taken.
✗ Branch 347 → 358 not taken.
✗ if (cb > 0) {
637 ✗ dist = qenergy = 0.f;
638 ✗ bits = 0;
639
0/2
✗ Branch 351 → 349 not taken.
✗ Branch 351 → 352 not taken.
✗ for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
640 int b;
641 float sqenergy;
642 ✗ dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
643 ✗ scaled + w2*128,
644 ✗ sce->ics.swb_sizes[g],
645 ✗ sce->sf_idx[w*16+g]+1,
646 cb,
647 1.0f,
648 INFINITY,
649 &b, &sqenergy,
650 0);
651 ✗ bits += b;
652 ✗ qenergy += sqenergy;
653 }
654 ✗ dist -= bits;
655
0/4
✗ Branch 352 → 353 not taken.
✗ Branch 352 → 354 not taken.
✗ Branch 355 → 356 not taken.
✗ Branch 355 → 357 not taken.
✗ if (dist < FFMIN(euplims[w*16+g], uplims[w*16+g])) {
656 ✗ sce->sf_idx[w*16+g]++;
657 ✗ dists[w*16+g] = dist;
658 ✗ qenergies[w*16+g] = qenergy;
659 } else {
660 ✗ break;
661 }
662 } else {
663 ✗ maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
664 ✗ break;
665 }
666 }
667 }
668 9272 prev = sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], mindeltasf, maxdeltasf);
669
2/2
✓ Branch 361 → 362 taken 6977 times.
✓ Branch 361 → 363 taken 2295 times.
9272 if (sce->sf_idx[w*16+g] != prevsc)
670 6977 fflag = 1;
671 9272 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]);
672 9272 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
673 }
674 16800 start += sce->ics.swb_sizes[g];
675 }
676 }
677
678 /** SF difference limit violation risk. Must re-clamp. */
679 340 prev = -1;
680
2/2
✓ Branch 381 → 370 taken 1200 times.
✓ Branch 381 → 382 taken 340 times.
1540 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
681
2/2
✓ Branch 379 → 371 taken 16800 times.
✓ Branch 379 → 380 taken 1200 times.
18000 for (g = 0; g < sce->ics.num_swb; g++) {
682
2/2
✓ Branch 371 → 372 taken 9272 times.
✓ Branch 371 → 378 taken 7528 times.
16800 if (!sce->zeroes[w*16+g]) {
683 9272 int prevsf = sce->sf_idx[w*16+g];
684
2/2
✓ Branch 372 → 373 taken 340 times.
✓ Branch 372 → 374 taken 8932 times.
9272 if (prev < 0)
685 340 prev = prevsf;
686 9272 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], prev - SCALE_MAX_DIFF, prev + SCALE_MAX_DIFF);
687 9272 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
688 9272 prev = sce->sf_idx[w*16+g];
689
3/4
✓ Branch 375 → 376 taken 24 times.
✓ Branch 375 → 378 taken 9248 times.
✗ Branch 376 → 377 not taken.
✓ Branch 376 → 378 taken 24 times.
9272 if (!fflag && prevsf != sce->sf_idx[w*16+g])
690 ✗ fflag = 1;
691 }
692 }
693 }
694
695 340 its++;
696
4/4
✓ Branch 382 → 383 taken 339 times.
✓ Branch 382 → 384 taken 1 time.
✓ Branch 383 → 125 taken 328 times.
✓ Branch 383 → 384 taken 11 times.
340 } while (fflag && its < maxits);
697
698 /** Scout out next nonzero bands */
699 12 ff_init_nextband_map(sce, nextband);
700
701 12 prev = -1;
702
2/2
✓ Branch 404 → 386 taken 42 times.
✓ Branch 404 → 405 taken 12 times.
54 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
703 /** Make sure proper codebooks are set */
704
2/2
✓ Branch 402 → 387 taken 588 times.
✓ Branch 402 → 403 taken 42 times.
630 for (g = 0; g < sce->ics.num_swb; g++) {
705
2/2
✓ Branch 387 → 388 taken 263 times.
✓ Branch 387 → 394 taken 325 times.
588 if (!sce->zeroes[w*16+g]) {
706 263 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
707
1/2
✗ Branch 389 → 390 not taken.
✓ Branch 389 → 395 taken 263 times.
263 if (sce->band_type[w*16+g] <= 0) {
708
0/2
✗ Branch 391 → 392 not taken.
✗ Branch 391 → 393 not taken.
✗ if (!ff_sfdelta_can_remove_band(sce, nextband, prev, w*16+g)) {
709 /** Cannot zero out, make sure it's not attempted */
710 ✗ sce->band_type[w*16+g] = 1;
711 } else {
712 ✗ sce->zeroes[w*16+g] = 1;
713 ✗ sce->band_type[w*16+g] = 0;
714 }
715 }
716 } else {
717 325 sce->band_type[w*16+g] = 0;
718 }
719 /** Check that there's no SF delta range violations */
720
2/2
✓ Branch 395 → 396 taken 263 times.
✓ Branch 395 → 401 taken 325 times.
588 if (!sce->zeroes[w*16+g]) {
721
2/2
✓ Branch 396 → 397 taken 251 times.
✓ Branch 396 → 398 taken 12 times.
263 if (prev != -1) {
722 251 av_unused int sfdiff = sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO;
723 av_assert1(sfdiff >= 0 && sfdiff <= 2*SCALE_MAX_DIFF);
724
2/2
✓ Branch 398 → 399 taken 6 times.
✓ Branch 398 → 400 taken 6 times.
12 } else if (sce->zeroes[0]) {
725 /** Set global gain to something useful */
726 6 sce->sf_idx[0] = sce->sf_idx[w*16+g];
727 }
728 263 prev = sce->sf_idx[w*16+g];
729 }
730 }
731 }
732 }
733
734 #endif /* AVCODEC_AACCODER_TWOLOOP_H */
735