FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aaccoder_twoloop.h
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 373 386 96.6%
Functions: 2 2 100.0%
Branches: 339 382 88.7%

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 27928 static inline int ff_pns_bits(SingleChannelElement *sce, int w, int g)
58 {
59
4/6
✓ Branch 0 taken 27928 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18593 times.
✓ Branch 3 taken 9335 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18593 times.
27928 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 737 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
66 AACEncContext *s,
67 SingleChannelElement *sce,
68 const float lambda)
69 {
70 737 int start = 0, i, w, w2, g, recomprd;
71 1474 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
72
1/2
✓ Branch 0 taken 737 times.
✗ Branch 1 not taken.
737 / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
73 737 * (lambda / 120.f);
74 737 int refbits = destbits;
75 int toomanybits, toofewbits;
76 char nzs[128];
77 uint8_t nextband[128];
78 int maxsf[128], minsf[128];
79 737 float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
80 float maxvals[128], spread_thr_r[128];
81 float min_spread_thr_r, max_spread_thr_r;
82
83 /**
84 * rdlambda controls the maximum tolerated distortion. Twoloop
85 * will keep iterating until it fails to lower it or it reaches
86 * ulimit * rdlambda. Keeping it low increases quality on difficult
87 * signals, but lower it too much, and bits will be taken from weak
88 * signals, creating "holes". A balance is necessary.
89 * rdmax and rdmin specify the relative deviation from rdlambda
90 * allowed for tonality compensation
91 */
92 737 float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f);
93 737 const float nzslope = 1.5f;
94 737 float rdmin = 0.03125f;
95 737 float rdmax = 1.0f;
96
97 /**
98 * sfoffs controls an offset of optmium allocation that will be
99 * applied based on lambda. Keep it real and modest, the loop
100 * will take care of the rest, this just accelerates convergence
101 */
102 737 float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10);
103
104 int fflag, minscaler, nminscaler;
105 737 int its = 0;
106 737 int maxits = 30;
107 737 int allz = 0;
108 int tbits;
109 737 int cutoff = 1024;
110 int pns_start_pos;
111 int prev;
112
113 /**
114 * zeroscale controls a multiplier of the threshold, if band energy
115 * is below this, a zero is forced. Keep it lower than 1, unless
116 * low lambda is used, because energy < threshold doesn't mean there's
117 * no audible signal outright, it's just energy. Also make it rise
118 * slower than rdlambda, as rdscale has due compensation with
119 * noisy band depriorization below, whereas zeroing logic is rather dumb
120 */
121 float zeroscale;
122
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 98 times.
737 if (lambda > 120.f) {
123 639 zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f);
124 } else {
125 98 zeroscale = 1.f;
126 }
127
128
1/2
✓ Branch 0 taken 737 times.
✗ Branch 1 not taken.
737 if (s->psy.bitres.alloc >= 0) {
129 /**
130 * Psy granted us extra bits to use, from the reservoire
131 * adjust for lambda except what psy already did
132 */
133 1474 destbits = s->psy.bitres.alloc
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
737 * (lambda / (avctx->global_quality ? avctx->global_quality : 120));
135 }
136
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
737 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
138 /**
139 * Constant Q-scale doesn't compensate MS coding on its own
140 * No need to be overly precise, this only controls RD
141 * adjustment CB limits when going overboard
142 */
143 if (s->options.mid_side && s->cur_type == TYPE_CPE)
144 destbits *= 2;
145
146 /**
147 * When using a constant Q-scale, don't adjust bits, just use RD
148 * Don't let it go overboard, though... 8x psy target is enough
149 */
150 toomanybits = 5800;
151 toofewbits = destbits / 16;
152
153 /** Don't offset scalers, just RD */
154 sfoffs = sce->ics.num_windows - 1;
155 rdlambda = sqrtf(rdlambda);
156
157 /** search further */
158 maxits *= 2;
159 } else {
160 /* When using ABR, be strict, but a reasonable leeway is
161 * critical to allow RC to smoothly track desired bitrate
162 * without sudden quality drops that cause audible artifacts.
163 * Symmetry is also desirable, to avoid systematic bias.
164 */
165 737 toomanybits = destbits + destbits/8;
166 737 toofewbits = destbits - destbits/8;
167
168 737 sfoffs = 0;
169 737 rdlambda = sqrtf(rdlambda);
170 }
171
172 /** and zero out above cutoff frequency */
173 {
174 737 int wlen = 1024 / sce->ics.num_windows;
175 int bandwidth;
176
177 /**
178 * Scale, psy gives us constant quality, this LP only scales
179 * bitrate by lambda, so we save bits on subjectively unimportant HF
180 * rather than increase quantization noise. Adjust nominal bitrate
181 * to effective bitrate according to encoding parameters,
182 * AAC_CUTOFF_FROM_BITRATE is calibrated for effective bitrate.
183 */
184 737 float rate_bandwidth_multiplier = 1.5f;
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
737 int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
186 ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
187 737 : (avctx->bit_rate / avctx->ch_layout.nb_channels);
188
189 /** Compensate for extensions that increase efficiency */
190
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
737 if (s->options.pns || s->options.intensity_stereo)
191 737 frame_bit_rate *= 1.15f;
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
737 if (avctx->cutoff > 0) {
194 bandwidth = avctx->cutoff;
195 } else {
196
5/10
✓ Branch 0 taken 737 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 737 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 737 times.
✓ Branch 6 taken 737 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 737 times.
737 bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
197 737 s->psy.cutoff = bandwidth;
198 }
199
200 737 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
201 737 pns_start_pos = NOISE_LOW_LIMIT * 2 * wlen / avctx->sample_rate;
202 }
203
204 /**
205 * for values above this the decoder might end up in an endless loop
206 * due to always having more bits than what can be encoded.
207 */
208 737 destbits = FFMIN(destbits, 5800);
209 737 toomanybits = FFMIN(toomanybits, 5800);
210 737 toofewbits = FFMIN(toofewbits, 5800);
211 /**
212 * XXX: some heuristic to determine initial quantizers will reduce search time
213 * determine zero bands and upper distortion limits
214 */
215 737 min_spread_thr_r = -1;
216 737 max_spread_thr_r = -1;
217
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 737 times.
1508 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
218
2/2
✓ Branch 0 taken 36169 times.
✓ Branch 1 taken 771 times.
36940 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
219 36169 int nz = 0;
220 36169 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
221
2/2
✓ Branch 0 taken 36869 times.
✓ Branch 1 taken 36169 times.
73038 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
222 36869 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
223
5/6
✓ Branch 0 taken 33068 times.
✓ Branch 1 taken 3801 times.
✓ Branch 2 taken 30683 times.
✓ Branch 3 taken 2385 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 30683 times.
36869 if (start >= cutoff || band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f) {
224 6186 sce->zeroes[(w+w2)*16+g] = 1;
225 6186 continue;
226 }
227 30683 nz = 1;
228 }
229
2/2
✓ Branch 0 taken 6016 times.
✓ Branch 1 taken 30153 times.
36169 if (!nz) {
230 6016 uplim = 0.0f;
231 } else {
232 30153 nz = 0;
233
2/2
✓ Branch 0 taken 30737 times.
✓ Branch 1 taken 30153 times.
60890 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
234 30737 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
235
3/4
✓ Branch 0 taken 30683 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30683 times.
30737 if (band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f)
236 54 continue;
237 30683 uplim += band->threshold;
238 30683 energy += band->energy;
239 30683 spread += band->spread;
240 30683 nz++;
241 }
242 }
243 36169 uplims[w*16+g] = uplim;
244 36169 energies[w*16+g] = energy;
245 36169 nzs[w*16+g] = nz;
246 36169 sce->zeroes[w*16+g] = !nz;
247 36169 allz |= nz;
248
4/4
✓ Branch 0 taken 30153 times.
✓ Branch 1 taken 6016 times.
✓ Branch 2 taken 14437 times.
✓ Branch 3 taken 15716 times.
36169 if (nz && sce->can_pns[w*16+g]) {
249 14437 spread_thr_r[w*16+g] = energy * nz / (uplim * spread);
250
2/2
✓ Branch 0 taken 735 times.
✓ Branch 1 taken 13702 times.
14437 if (min_spread_thr_r < 0) {
251 735 min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+g];
252 } else {
253
2/2
✓ Branch 0 taken 3381 times.
✓ Branch 1 taken 10321 times.
13702 min_spread_thr_r = FFMIN(min_spread_thr_r, spread_thr_r[w*16+g]);
254
2/2
✓ Branch 0 taken 11606 times.
✓ Branch 1 taken 2096 times.
13702 max_spread_thr_r = FFMAX(max_spread_thr_r, spread_thr_r[w*16+g]);
255 }
256 }
257 }
258 }
259
260 /** Compute initial scalers */
261 737 minscaler = 65535;
262
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 737 times.
1508 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
263
2/2
✓ Branch 0 taken 36169 times.
✓ Branch 1 taken 771 times.
36940 for (g = 0; g < sce->ics.num_swb; g++) {
264
2/2
✓ Branch 0 taken 6016 times.
✓ Branch 1 taken 30153 times.
36169 if (sce->zeroes[w*16+g]) {
265 6016 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
266 6016 continue;
267 }
268 /**
269 * log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2).
270 * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion,
271 * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus
272 * more robust.
273 */
274 30153 sce->sf_idx[w*16+g] = av_clip(
275 SCALE_ONE_POS
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30153 times.
30153 + 1.75*log2f(FFMAX(0.00125f,uplims[w*16+g]) / sce->ics.swb_sizes[g])
277 30153 + sfoffs,
278 60, SCALE_MAX_POS);
279 30153 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
280 }
281 }
282
283 /** Clip */
284 737 minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
285
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 737 times.
1508 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
286
2/2
✓ Branch 0 taken 36169 times.
✓ Branch 1 taken 771 times.
36940 for (g = 0; g < sce->ics.num_swb; g++)
287
2/2
✓ Branch 0 taken 30153 times.
✓ Branch 1 taken 6016 times.
36169 if (!sce->zeroes[w*16+g])
288 30153 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1);
289
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
737 if (!allz)
291 return;
292 737 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
293 737 ff_quantize_band_cost_cache_init(s);
294
295
2/2
✓ Branch 0 taken 94336 times.
✓ Branch 1 taken 737 times.
95073 for (i = 0; i < sizeof(minsf) / sizeof(minsf[0]); ++i)
296 94336 minsf[i] = 0;
297
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 737 times.
1508 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
298 771 start = w*128;
299
2/2
✓ Branch 0 taken 36169 times.
✓ Branch 1 taken 771 times.
36940 for (g = 0; g < sce->ics.num_swb; g++) {
300 36169 const float *scaled = s->scoefs + start;
301 int minsfidx;
302 36169 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
303
2/2
✓ Branch 0 taken 36141 times.
✓ Branch 1 taken 28 times.
36169 if (maxvals[w*16+g] > 0) {
304 36141 minsfidx = coef2minsf(maxvals[w*16+g]);
305
2/2
✓ Branch 0 taken 36813 times.
✓ Branch 1 taken 36141 times.
72954 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
306 36813 minsf[(w+w2)*16+g] = minsfidx;
307 }
308 36169 start += sce->ics.swb_sizes[g];
309 }
310 }
311
312 /**
313 * Scale uplims to match rate distortion to quality
314 * bu applying noisy band depriorization and tonal band priorization.
315 * Maxval-energy ratio gives us an idea of how noisy/tonal the band is.
316 * If maxval^2 ~ energy, then that band is mostly noise, and we can relax
317 * rate distortion requirements.
318 */
319 737 memcpy(euplims, uplims, sizeof(euplims));
320
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 737 times.
1508 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
321 /** psy already priorizes transients to some extent */
322
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 725 times.
771 float de_psy_factor = (sce->ics.num_windows > 1) ? 8.0f / sce->ics.group_len[w] : 1.0f;
323 771 start = w*128;
324
2/2
✓ Branch 0 taken 36169 times.
✓ Branch 1 taken 771 times.
36940 for (g = 0; g < sce->ics.num_swb; g++) {
325
2/2
✓ Branch 0 taken 30177 times.
✓ Branch 1 taken 5992 times.
36169 if (nzs[g] > 0) {
326 30177 float cleanup_factor = ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
327 30177 float energy2uplim = find_form_factor(
328 30177 sce->ics.group_len[w], sce->ics.swb_sizes[g],
329 30177 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
330 30177 sce->coeffs + start,
331 nzslope * cleanup_factor);
332 30177 energy2uplim *= de_psy_factor;
333
1/2
✓ Branch 0 taken 30177 times.
✗ Branch 1 not taken.
30177 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
334 /** In ABR, we need to priorize less and let rate control do its thing */
335 30177 energy2uplim = sqrtf(energy2uplim);
336 }
337
5/6
✓ Branch 0 taken 29977 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 29977 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29977 times.
✓ Branch 5 taken 200 times.
30177 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
338 30177 uplims[w*16+g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
339 30177 * sce->ics.group_len[w];
340
341 30177 energy2uplim = find_form_factor(
342 30177 sce->ics.group_len[w], sce->ics.swb_sizes[g],
343 30177 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
344 30177 sce->coeffs + start,
345 2.0f);
346 30177 energy2uplim *= de_psy_factor;
347
1/2
✓ Branch 0 taken 30177 times.
✗ Branch 1 not taken.
30177 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
348 /** In ABR, we need to priorize less and let rate control do its thing */
349 30177 energy2uplim = sqrtf(energy2uplim);
350 }
351
5/6
✓ Branch 0 taken 30019 times.
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 30019 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30019 times.
✓ Branch 5 taken 158 times.
30177 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
352 30177 euplims[w*16+g] *= av_clipf(rdlambda * energy2uplim * sce->ics.group_len[w],
353 0.5f, 1.0f);
354 }
355 36169 start += sce->ics.swb_sizes[g];
356 }
357 }
358
359
2/2
✓ Branch 0 taken 94336 times.
✓ Branch 1 taken 737 times.
95073 for (i = 0; i < sizeof(maxsf) / sizeof(maxsf[0]); ++i)
360 94336 maxsf[i] = SCALE_MAX_POS;
361
362 //perform two-loop search
363 //outer loop - improve quality
364 do {
365 //inner loop - quantize spectrum to fit into given number of bits
366 int overdist;
367
2/2
✓ Branch 0 taken 19489 times.
✓ Branch 1 taken 737 times.
20226 int qstep = its ? 1 : 32;
368 do {
369 57936 int changed = 0;
370 57936 prev = -1;
371 57936 recomprd = 0;
372 57936 tbits = 0;
373
2/2
✓ Branch 0 taken 66673 times.
✓ Branch 1 taken 57936 times.
124609 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
374 66673 start = w*128;
375
2/2
✓ Branch 0 taken 2855237 times.
✓ Branch 1 taken 66673 times.
2921910 for (g = 0; g < sce->ics.num_swb; g++) {
376 2855237 const float *coefs = &sce->coeffs[start];
377 2855237 const float *scaled = &s->scoefs[start];
378 2855237 int bits = 0;
379 int cb;
380 2855237 float dist = 0.0f;
381 2855237 float qenergy = 0.0f;
382
383
3/4
✓ Branch 0 taken 2341657 times.
✓ Branch 1 taken 513580 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2341657 times.
2855237 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
384 513580 start += sce->ics.swb_sizes[g];
385
2/2
✓ Branch 0 taken 21285 times.
✓ Branch 1 taken 492295 times.
513580 if (sce->can_pns[w*16+g]) {
386 /** PNS isn't free */
387 21285 tbits += ff_pns_bits(sce, w, g);
388 }
389 513580 continue;
390 }
391 2341657 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
392
2/2
✓ Branch 0 taken 2488117 times.
✓ Branch 1 taken 2341657 times.
4829774 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
393 int b;
394 float sqenergy;
395 4976234 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
396 2488117 scaled + w2*128,
397 2488117 sce->ics.swb_sizes[g],
398 2488117 sce->sf_idx[w*16+g],
399 cb,
400 1.0f,
401 INFINITY,
402 &b, &sqenergy,
403 0);
404 2488117 bits += b;
405 2488117 qenergy += sqenergy;
406 }
407 2341657 dists[w*16+g] = dist - bits;
408 2341657 qenergies[w*16+g] = qenergy;
409
2/2
✓ Branch 0 taken 2283721 times.
✓ Branch 1 taken 57936 times.
2341657 if (prev != -1) {
410 2283721 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
411 2283721 bits += ff_aac_scalefactor_bits[sfdiff];
412 }
413 2341657 tbits += bits;
414 2341657 start += sce->ics.swb_sizes[g];
415 2341657 prev = sce->sf_idx[w*16+g];
416 }
417 }
418
2/2
✓ Branch 0 taken 34252 times.
✓ Branch 1 taken 23684 times.
57936 if (tbits > toomanybits) {
419 34252 recomprd = 1;
420
2/2
✓ Branch 0 taken 4384256 times.
✓ Branch 1 taken 34252 times.
4418508 for (i = 0; i < 128; i++) {
421
2/2
✓ Branch 0 taken 3782254 times.
✓ Branch 1 taken 602002 times.
4384256 if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) {
422
1/2
✓ Branch 0 taken 3782254 times.
✗ Branch 1 not taken.
3782254 int maxsf_i = (tbits > 5800) ? SCALE_MAX_POS : maxsf[i];
423 3782254 int new_sf = FFMIN(maxsf_i, sce->sf_idx[i] + qstep);
424
2/2
✓ Branch 0 taken 3722018 times.
✓ Branch 1 taken 60236 times.
3782254 if (new_sf != sce->sf_idx[i]) {
425 3722018 sce->sf_idx[i] = new_sf;
426 3722018 changed = 1;
427 }
428 }
429 }
430
2/2
✓ Branch 0 taken 8757 times.
✓ Branch 1 taken 14927 times.
23684 } else if (tbits < toofewbits) {
431 8757 recomprd = 1;
432
2/2
✓ Branch 0 taken 1120896 times.
✓ Branch 1 taken 8757 times.
1129653 for (i = 0; i < 128; i++) {
433
2/2
✓ Branch 0 taken 218581 times.
✓ Branch 1 taken 902315 times.
1120896 if (sce->sf_idx[i] > SCALE_ONE_POS) {
434 218581 int new_sf = FFMAX3(minsf[i], SCALE_ONE_POS, sce->sf_idx[i] - qstep);
435
1/2
✓ Branch 0 taken 218581 times.
✗ Branch 1 not taken.
218581 if (new_sf != sce->sf_idx[i]) {
436 218581 sce->sf_idx[i] = new_sf;
437 218581 changed = 1;
438 }
439 }
440 }
441 }
442 57936 qstep >>= 1;
443
7/8
✓ Branch 0 taken 54251 times.
✓ Branch 1 taken 3685 times.
✓ Branch 2 taken 34051 times.
✓ Branch 3 taken 20200 times.
✓ Branch 4 taken 34025 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 34025 times.
✗ Branch 7 not taken.
57936 if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217 && changed)
444 34025 qstep = 1;
445
2/2
✓ Branch 0 taken 37710 times.
✓ Branch 1 taken 20226 times.
57936 } while (qstep);
446
447 20226 overdist = 1;
448 20226 fflag = tbits < toofewbits;
449
5/6
✓ Branch 0 taken 40452 times.
✓ Branch 1 taken 14272 times.
✓ Branch 2 taken 34498 times.
✓ Branch 3 taken 5954 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5954 times.
54724 for (i = 0; i < 2 && (overdist || recomprd); ++i) {
450
2/2
✓ Branch 0 taken 11719 times.
✓ Branch 1 taken 22779 times.
34498 if (recomprd) {
451 /** Must recompute distortion */
452 11719 prev = -1;
453 11719 tbits = 0;
454
2/2
✓ Branch 0 taken 11801 times.
✓ Branch 1 taken 11719 times.
23520 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
455 11801 start = w*128;
456
2/2
✓ Branch 0 taken 574329 times.
✓ Branch 1 taken 11801 times.
586130 for (g = 0; g < sce->ics.num_swb; g++) {
457 574329 const float *coefs = sce->coeffs + start;
458 574329 const float *scaled = s->scoefs + start;
459 574329 int bits = 0;
460 int cb;
461 574329 float dist = 0.0f;
462 574329 float qenergy = 0.0f;
463
464
3/4
✓ Branch 0 taken 497128 times.
✓ Branch 1 taken 77201 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 497128 times.
574329 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
465 77201 start += sce->ics.swb_sizes[g];
466
2/2
✓ Branch 0 taken 6643 times.
✓ Branch 1 taken 70558 times.
77201 if (sce->can_pns[w*16+g]) {
467 /** PNS isn't free */
468 6643 tbits += ff_pns_bits(sce, w, g);
469 }
470 77201 continue;
471 }
472 497128 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
473
2/2
✓ Branch 0 taken 498580 times.
✓ Branch 1 taken 497128 times.
995708 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
474 int b;
475 float sqenergy;
476 997160 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
477 498580 scaled + w2*128,
478 498580 sce->ics.swb_sizes[g],
479 498580 sce->sf_idx[w*16+g],
480 cb,
481 1.0f,
482 INFINITY,
483 &b, &sqenergy,
484 0);
485 498580 bits += b;
486 498580 qenergy += sqenergy;
487 }
488 497128 dists[w*16+g] = dist - bits;
489 497128 qenergies[w*16+g] = qenergy;
490
2/2
✓ Branch 0 taken 485409 times.
✓ Branch 1 taken 11719 times.
497128 if (prev != -1) {
491 485409 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
492 485409 bits += ff_aac_scalefactor_bits[sfdiff];
493 }
494 497128 tbits += bits;
495 497128 start += sce->ics.swb_sizes[g];
496 497128 prev = sce->sf_idx[w*16+g];
497 }
498 }
499 }
500
7/8
✓ Branch 0 taken 20226 times.
✓ Branch 1 taken 14272 times.
✓ Branch 2 taken 20226 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9191 times.
✓ Branch 5 taken 11035 times.
✓ Branch 6 taken 7185 times.
✓ Branch 7 taken 2006 times.
34498 if (!i && s->options.pns && its > maxits/2 && tbits > toofewbits) {
501 7185 float maxoverdist = 0.0f;
502 7185 float ovrfactor = 1.f+(maxits-its)*16.f/maxits;
503 7185 overdist = recomprd = 0;
504
2/2
✓ Branch 0 taken 7661 times.
✓ Branch 1 taken 7185 times.
14846 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
505
2/2
✓ Branch 0 taken 352849 times.
✓ Branch 1 taken 7661 times.
360510 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
506
6/6
✓ Branch 0 taken 276256 times.
✓ Branch 1 taken 76593 times.
✓ Branch 2 taken 47203 times.
✓ Branch 3 taken 229053 times.
✓ Branch 4 taken 3775 times.
✓ Branch 5 taken 43428 times.
352849 if (!sce->zeroes[w*16+g] && sce->sf_idx[w*16+g] > SCALE_ONE_POS && dists[w*16+g] > uplims[w*16+g]*ovrfactor) {
507
2/2
✓ Branch 0 taken 1311 times.
✓ Branch 1 taken 2464 times.
3775 float ovrdist = dists[w*16+g] / FFMAX(uplims[w*16+g],euplims[w*16+g]);
508
2/2
✓ Branch 0 taken 2051 times.
✓ Branch 1 taken 1724 times.
3775 maxoverdist = FFMAX(maxoverdist, ovrdist);
509 3775 overdist++;
510 }
511 }
512 }
513
2/2
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 5954 times.
7185 if (overdist) {
514 /* We have overdistorted bands, trade for zeroes (that can be noise)
515 * Zero the bands in the lowest 1.25% spread-energy-threshold ranking
516 */
517 1231 float minspread = max_spread_thr_r;
518 1231 float maxspread = min_spread_thr_r;
519 float zspread;
520 1231 int zeroable = 0;
521 1231 int zeroed = 0;
522 int maxzeroed, zloop;
523
2/2
✓ Branch 0 taken 1651 times.
✓ Branch 1 taken 1231 times.
2882 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
524
2/2
✓ Branch 0 taken 61299 times.
✓ Branch 1 taken 1651 times.
62950 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
525
6/6
✓ Branch 0 taken 31755 times.
✓ Branch 1 taken 29544 times.
✓ Branch 2 taken 20276 times.
✓ Branch 3 taken 11479 times.
✓ Branch 4 taken 19073 times.
✓ Branch 5 taken 1203 times.
61299 if (start >= pns_start_pos && !sce->zeroes[w*16+g] && sce->can_pns[w*16+g]) {
526
2/2
✓ Branch 0 taken 6156 times.
✓ Branch 1 taken 12917 times.
19073 minspread = FFMIN(minspread, spread_thr_r[w*16+g]);
527
2/2
✓ Branch 0 taken 15695 times.
✓ Branch 1 taken 3378 times.
19073 maxspread = FFMAX(maxspread, spread_thr_r[w*16+g]);
528 19073 zeroable++;
529 }
530 }
531 }
532 1231 zspread = (maxspread-minspread) * 0.0125f + minspread;
533 /* Don't PNS everything even if allowed. It suppresses bit starvation signals from RC,
534 * and forced the hand of the later search_for_pns step.
535 * Instead, PNS a fraction of the spread_thr_r range depending on how starved for bits we are,
536 * and leave further PNSing to search_for_pns if worthwhile.
537 */
538
6/6
✓ Branch 0 taken 1053 times.
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1221 times.
✓ Branch 4 taken 1043 times.
✓ Branch 5 taken 178 times.
1231 zspread = FFMIN3(min_spread_thr_r * 8.f, zspread,
539 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
540 1231 maxzeroed = FFMIN(zeroable, FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
541
2/2
✓ Branch 0 taken 2462 times.
✓ Branch 1 taken 1231 times.
3693 for (zloop = 0; zloop < 2; zloop++) {
542 /* Two passes: first distorted stuff - two birds in one shot and all that,
543 * then anything viable. Viable means not zero, but either CB=zero-able
544 * (too high SF), not SF <= 1 (that means we'd be operating at very high
545 * quality, we don't want PNS when doing VHQ), PNS allowed, and within
546 * the lowest ranking percentile.
547 */
548
2/2
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 1231 times.
2462 float loopovrfactor = (zloop) ? 1.0f : ovrfactor;
549
2/2
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 1231 times.
2462 int loopminsf = (zloop) ? (SCALE_ONE_POS - SCALE_DIV_512) : SCALE_ONE_POS;
550 int mcb;
551
4/4
✓ Branch 0 taken 105494 times.
✓ Branch 1 taken 2378 times.
✓ Branch 2 taken 105410 times.
✓ Branch 3 taken 84 times.
107872 for (g = sce->ics.num_swb-1; g > 0 && zeroed < maxzeroed; g--) {
552
2/2
✓ Branch 0 taken 49690 times.
✓ Branch 1 taken 55720 times.
105410 if (sce->ics.swb_offset[g] < pns_start_pos)
553 49690 continue;
554
2/2
✓ Branch 0 taken 62425 times.
✓ Branch 1 taken 55720 times.
118145 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
555
6/6
✓ Branch 0 taken 39551 times.
✓ Branch 1 taken 22874 times.
✓ Branch 2 taken 37158 times.
✓ Branch 3 taken 2393 times.
✓ Branch 4 taken 4795 times.
✓ Branch 5 taken 32363 times.
62425 if (!sce->zeroes[w*16+g] && sce->can_pns[w*16+g] && spread_thr_r[w*16+g] <= zspread
556
2/2
✓ Branch 0 taken 2540 times.
✓ Branch 1 taken 2255 times.
4795 && sce->sf_idx[w*16+g] > loopminsf
557
3/4
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 2075 times.
✓ Branch 3 taken 465 times.
✗ Branch 4 not taken.
2540 && (dists[w*16+g] > loopovrfactor*uplims[w*16+g] || !(mcb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]))
558
6/6
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 268 times.
✓ Branch 3 taken 129 times.
✓ Branch 4 taken 333 times.
✓ Branch 5 taken 64 times.
465 || (mcb <= 1 && dists[w*16+g] > FFMIN(uplims[w*16+g], euplims[w*16+g]))) ) {
559 2408 sce->zeroes[w*16+g] = 1;
560 2408 sce->band_type[w*16+g] = 0;
561 2408 zeroed++;
562 }
563 }
564 }
565 }
566
2/2
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 165 times.
1231 if (zeroed)
567 1066 recomprd = fflag = 1;
568 } else {
569 5954 overdist = 0;
570 }
571 }
572 }
573
574 20226 minscaler = SCALE_MAX_POS;
575
2/2
✓ Branch 0 taken 21246 times.
✓ Branch 1 taken 20226 times.
41472 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
576
2/2
✓ Branch 0 taken 992754 times.
✓ Branch 1 taken 21246 times.
1014000 for (g = 0; g < sce->ics.num_swb; g++) {
577
2/2
✓ Branch 0 taken 811196 times.
✓ Branch 1 taken 181558 times.
992754 if (!sce->zeroes[w*16+g]) {
578 811196 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
579 }
580 }
581 }
582
583 20226 minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
584 20226 prev = -1;
585
2/2
✓ Branch 0 taken 21246 times.
✓ Branch 1 taken 20226 times.
41472 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
586 /** Start with big steps, end up fine-tunning */
587
4/4
✓ Branch 0 taken 9667 times.
✓ Branch 1 taken 11579 times.
✓ Branch 2 taken 6168 times.
✓ Branch 3 taken 3499 times.
21246 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
588 21246 int edepth = depth+2;
589 21246 float uplmax = its / (maxits*0.25f) + 1.0f;
590
5/8
✓ Branch 0 taken 14358 times.
✓ Branch 1 taken 6888 times.
✓ Branch 2 taken 14358 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14358 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14358 times.
✗ Branch 7 not taken.
21246 uplmax *= (tbits > destbits) ? FFMIN(2.0f, tbits / (float)FFMAX(1,destbits)) : 1.0f;
591 21246 start = w * 128;
592
2/2
✓ Branch 0 taken 992754 times.
✓ Branch 1 taken 21246 times.
1014000 for (g = 0; g < sce->ics.num_swb; g++) {
593 992754 int prevsc = sce->sf_idx[w*16+g];
594
4/4
✓ Branch 0 taken 81229 times.
✓ Branch 1 taken 911525 times.
✓ Branch 2 taken 20226 times.
✓ Branch 3 taken 61003 times.
992754 if (prev < 0 && !sce->zeroes[w*16+g])
595 20226 prev = sce->sf_idx[0];
596
2/2
✓ Branch 0 taken 811196 times.
✓ Branch 1 taken 181558 times.
992754 if (!sce->zeroes[w*16+g]) {
597 811196 const float *coefs = sce->coeffs + start;
598 811196 const float *scaled = s->scoefs + start;
599 811196 int cmb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
600 811196 int mindeltasf = FFMAX(0, prev - SCALE_MAX_DIFF);
601 811196 int maxdeltasf = FFMIN(SCALE_MAX_POS - SCALE_DIV_512, prev + SCALE_MAX_DIFF);
602
6/6
✓ Branch 0 taken 770512 times.
✓ Branch 1 taken 40684 times.
✓ Branch 2 taken 484693 times.
✓ Branch 3 taken 285819 times.
✓ Branch 4 taken 522724 times.
✓ Branch 5 taken 2653 times.
811196 if ((!cmb || dists[w*16+g] > uplims[w*16+g]) && sce->sf_idx[w*16+g] > FFMAX(mindeltasf, minsf[w*16+g])) {
603 /* Try to make sure there is some energy in every nonzero band
604 * NOTE: This algorithm must be forcibly imbalanced, pushing harder
605 * on holes or more distorted bands at first, otherwise there's
606 * no net gain (since the next iteration will offset all bands
607 * on the opposite direction to compensate for extra bits)
608 */
609
4/4
✓ Branch 0 taken 1961955 times.
✓ Branch 1 taken 56941 times.
✓ Branch 2 taken 1961759 times.
✓ Branch 3 taken 196 times.
2018896 for (i = 0; i < edepth && sce->sf_idx[w*16+g] > mindeltasf; ++i) {
610 int cb, bits;
611 float dist, qenergy;
612 1961759 int mb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1);
613 1961759 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
614 1961759 dist = qenergy = 0.f;
615 1961759 bits = 0;
616
2/2
✓ Branch 0 taken 333657 times.
✓ Branch 1 taken 1628102 times.
1961759 if (!cb) {
617 333657 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g]-1, maxsf[w*16+g]);
618
4/4
✓ Branch 0 taken 240559 times.
✓ Branch 1 taken 1387543 times.
✓ Branch 2 taken 106869 times.
✓ Branch 3 taken 133690 times.
1628102 } else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) {
619 106869 break;
620 }
621 /* !g is the DC band, it's important, since quantization error here
622 * applies to less than a cycle, it creates horrible intermodulation
623 * distortion if it doesn't stick to what psy requests
624 */
625
6/6
✓ Branch 0 taken 34512 times.
✓ Branch 1 taken 1820378 times.
✓ Branch 2 taken 3078 times.
✓ Branch 3 taken 31434 times.
✓ Branch 4 taken 1368 times.
✓ Branch 5 taken 1710 times.
1854890 if (!g && sce->ics.num_windows > 1 && dists[w*16+g] >= euplims[w*16+g])
626 1368 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
627
2/2
✓ Branch 0 taken 1967456 times.
✓ Branch 1 taken 1854890 times.
3822346 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
628 int b;
629 float sqenergy;
630 3934912 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
631 1967456 scaled + w2*128,
632 1967456 sce->ics.swb_sizes[g],
633 1967456 sce->sf_idx[w*16+g]-1,
634 cb,
635 1.0f,
636 INFINITY,
637 &b, &sqenergy,
638 0);
639 1967456 bits += b;
640 1967456 qenergy += sqenergy;
641 }
642 1854890 sce->sf_idx[w*16+g]--;
643 1854890 dists[w*16+g] = dist - bits;
644 1854890 qenergies[w*16+g] = qenergy;
645
5/6
✓ Branch 0 taken 1543310 times.
✓ Branch 1 taken 311580 times.
✓ Branch 2 taken 1543310 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 781161 times.
✓ Branch 5 taken 762149 times.
3398200 if (mb && (sce->sf_idx[w*16+g] < mindeltasf || (
646
2/2
✓ Branch 0 taken 1257392 times.
✓ Branch 1 taken 285918 times.
1543310 (dists[w*16+g] < FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g]))
647
2/2
✓ Branch 0 taken 422443 times.
✓ Branch 1 taken 358718 times.
781161 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
648 ) )) {
649 break;
650 }
651 }
652
4/4
✓ Branch 0 taken 109046 times.
✓ Branch 1 taken 179426 times.
✓ Branch 2 taken 104395 times.
✓ Branch 3 taken 4651 times.
288472 } else if (tbits > toofewbits && sce->sf_idx[w*16+g] < FFMIN(maxdeltasf, maxsf[w*16+g])
653
4/4
✓ Branch 0 taken 60287 times.
✓ Branch 1 taken 44108 times.
✓ Branch 2 taken 92536 times.
✓ Branch 3 taken 11859 times.
104395 && (dists[w*16+g] < FFMIN(euplims[w*16+g], uplims[w*16+g]))
654
2/2
✓ Branch 0 taken 15185 times.
✓ Branch 1 taken 77351 times.
92536 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
655 ) {
656 /** Um... over target. Save bits for more important stuff. */
657
3/4
✓ Branch 0 taken 26524 times.
✓ Branch 1 taken 823 times.
✓ Branch 2 taken 26524 times.
✗ Branch 3 not taken.
27347 for (i = 0; i < depth && sce->sf_idx[w*16+g] < maxdeltasf; ++i) {
658 int cb, bits;
659 float dist, qenergy;
660 26524 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]+1);
661
2/2
✓ Branch 0 taken 26511 times.
✓ Branch 1 taken 13 times.
26524 if (cb > 0) {
662 26511 dist = qenergy = 0.f;
663 26511 bits = 0;
664
2/2
✓ Branch 0 taken 26555 times.
✓ Branch 1 taken 26511 times.
53066 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
665 int b;
666 float sqenergy;
667 53110 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
668 26555 scaled + w2*128,
669 26555 sce->ics.swb_sizes[g],
670 26555 sce->sf_idx[w*16+g]+1,
671 cb,
672 1.0f,
673 INFINITY,
674 &b, &sqenergy,
675 0);
676 26555 bits += b;
677 26555 qenergy += sqenergy;
678 }
679 26511 dist -= bits;
680
4/4
✓ Branch 0 taken 19481 times.
✓ Branch 1 taken 7030 times.
✓ Branch 2 taken 12162 times.
✓ Branch 3 taken 14349 times.
26511 if (dist < FFMIN(euplims[w*16+g], uplims[w*16+g])) {
681 12162 sce->sf_idx[w*16+g]++;
682 12162 dists[w*16+g] = dist;
683 12162 qenergies[w*16+g] = qenergy;
684 } else {
685 14349 break;
686 }
687 } else {
688 13 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
689 13 break;
690 }
691 }
692 }
693 811196 prev = sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], mindeltasf, maxdeltasf);
694
2/2
✓ Branch 0 taken 528909 times.
✓ Branch 1 taken 282287 times.
811196 if (sce->sf_idx[w*16+g] != prevsc)
695 528909 fflag = 1;
696 811196 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]);
697 811196 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
698 }
699 992754 start += sce->ics.swb_sizes[g];
700 }
701 }
702
703 /** SF difference limit violation risk. Must re-clamp. */
704 20226 prev = -1;
705
2/2
✓ Branch 0 taken 21246 times.
✓ Branch 1 taken 20226 times.
41472 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
706
2/2
✓ Branch 0 taken 992754 times.
✓ Branch 1 taken 21246 times.
1014000 for (g = 0; g < sce->ics.num_swb; g++) {
707
2/2
✓ Branch 0 taken 811196 times.
✓ Branch 1 taken 181558 times.
992754 if (!sce->zeroes[w*16+g]) {
708 811196 int prevsf = sce->sf_idx[w*16+g];
709
2/2
✓ Branch 0 taken 20226 times.
✓ Branch 1 taken 790970 times.
811196 if (prev < 0)
710 20226 prev = prevsf;
711 811196 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], prev - SCALE_MAX_DIFF, prev + SCALE_MAX_DIFF);
712 811196 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
713 811196 prev = sce->sf_idx[w*16+g];
714
3/4
✓ Branch 0 taken 5050 times.
✓ Branch 1 taken 806146 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5050 times.
811196 if (!fflag && prevsf != sce->sf_idx[w*16+g])
715 fflag = 1;
716 }
717 }
718 }
719
720 20226 its++;
721
4/4
✓ Branch 0 taken 20105 times.
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 19489 times.
✓ Branch 3 taken 616 times.
20226 } while (fflag && its < maxits);
722
723 /** Scout out next nonzero bands */
724 737 ff_init_nextband_map(sce, nextband);
725
726 737 prev = -1;
727
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 737 times.
1508 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
728 /** Make sure proper codebooks are set */
729
2/2
✓ Branch 0 taken 36169 times.
✓ Branch 1 taken 771 times.
36940 for (g = 0; g < sce->ics.num_swb; g++) {
730
2/2
✓ Branch 0 taken 27745 times.
✓ Branch 1 taken 8424 times.
36169 if (!sce->zeroes[w*16+g]) {
731 27745 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
732
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 27571 times.
27745 if (sce->band_type[w*16+g] <= 0) {
733
1/2
✓ Branch 1 taken 174 times.
✗ Branch 2 not taken.
174 if (!ff_sfdelta_can_remove_band(sce, nextband, prev, w*16+g)) {
734 /** Cannot zero out, make sure it's not attempted */
735 174 sce->band_type[w*16+g] = 1;
736 } else {
737 sce->zeroes[w*16+g] = 1;
738 sce->band_type[w*16+g] = 0;
739 }
740 }
741 } else {
742 8424 sce->band_type[w*16+g] = 0;
743 }
744 /** Check that there's no SF delta range violations */
745
2/2
✓ Branch 0 taken 27745 times.
✓ Branch 1 taken 8424 times.
36169 if (!sce->zeroes[w*16+g]) {
746
2/2
✓ Branch 0 taken 27008 times.
✓ Branch 1 taken 737 times.
27745 if (prev != -1) {
747 27008 av_unused int sfdiff = sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO;
748 av_assert1(sfdiff >= 0 && sfdiff <= 2*SCALE_MAX_DIFF);
749
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 545 times.
737 } else if (sce->zeroes[0]) {
750 /** Set global gain to something useful */
751 192 sce->sf_idx[0] = sce->sf_idx[w*16+g];
752 }
753 27745 prev = sce->sf_idx[w*16+g];
754 }
755 }
756 }
757 }
758
759 #endif /* AVCODEC_AACCODER_TWOLOOP_H */
760