FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aaccoder_twoloop.h
Date: 2024-07-20 16:02:01
Exec Total Coverage
Lines: 373 386 96.6%
Functions: 2 2 100.0%
Branches: 340 382 89.0%

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 27475 static inline int ff_pns_bits(SingleChannelElement *sce, int w, int g)
58 {
59
4/6
✓ Branch 0 taken 27475 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18270 times.
✓ Branch 3 taken 9205 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18270 times.
27475 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 739 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
66 AACEncContext *s,
67 SingleChannelElement *sce,
68 const float lambda)
69 {
70 739 int start = 0, i, w, w2, g, recomprd;
71 1478 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
72
1/2
✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
739 / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
73 739 * (lambda / 120.f);
74 739 int refbits = destbits;
75 int toomanybits, toofewbits;
76 char nzs[128];
77 uint8_t nextband[128];
78 int maxsf[128], minsf[128];
79 739 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 739 float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f);
93 739 const float nzslope = 1.5f;
94 739 float rdmin = 0.03125f;
95 739 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 739 float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10);
103
104 int fflag, minscaler, nminscaler;
105 739 int its = 0;
106 739 int maxits = 30;
107 739 int allz = 0;
108 int tbits;
109 739 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 641 times.
✓ Branch 1 taken 98 times.
739 if (lambda > 120.f) {
123 641 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 739 times.
✗ Branch 1 not taken.
739 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 1478 destbits = s->psy.bitres.alloc
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 * (lambda / (avctx->global_quality ? avctx->global_quality : 120));
135 }
136
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 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 739 toomanybits = destbits + destbits/8;
166 739 toofewbits = destbits - destbits/8;
167
168 739 sfoffs = 0;
169 739 rdlambda = sqrtf(rdlambda);
170 }
171
172 /** and zero out above cutoff frequency */
173 {
174 739 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 739 float rate_bandwidth_multiplier = 1.5f;
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
186 ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
187 739 : (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 739 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
739 if (s->options.pns || s->options.intensity_stereo)
191 739 frame_bit_rate *= 1.15f;
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 if (avctx->cutoff > 0) {
194 bandwidth = avctx->cutoff;
195 } else {
196
5/10
✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 739 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 739 times.
✓ Branch 6 taken 739 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 739 times.
739 bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
197 739 s->psy.cutoff = bandwidth;
198 }
199
200 739 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
201 739 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 739 destbits = FFMIN(destbits, 5800);
209 739 toomanybits = FFMIN(toomanybits, 5800);
210 739 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 739 min_spread_thr_r = -1;
216 739 max_spread_thr_r = -1;
217
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 739 times.
1512 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
218
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
219 36267 int nz = 0;
220 36267 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
221
2/2
✓ Branch 0 taken 36967 times.
✓ Branch 1 taken 36267 times.
73234 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
222 36967 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
223
5/6
✓ Branch 0 taken 33156 times.
✓ Branch 1 taken 3811 times.
✓ Branch 2 taken 30749 times.
✓ Branch 3 taken 2407 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 30749 times.
36967 if (start >= cutoff || band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f) {
224 6218 sce->zeroes[(w+w2)*16+g] = 1;
225 6218 continue;
226 }
227 30749 nz = 1;
228 }
229
2/2
✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 30219 times.
36267 if (!nz) {
230 6048 uplim = 0.0f;
231 } else {
232 30219 nz = 0;
233
2/2
✓ Branch 0 taken 30803 times.
✓ Branch 1 taken 30219 times.
61022 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
234 30803 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
235
3/4
✓ Branch 0 taken 30749 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30749 times.
30803 if (band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f)
236 54 continue;
237 30749 uplim += band->threshold;
238 30749 energy += band->energy;
239 30749 spread += band->spread;
240 30749 nz++;
241 }
242 }
243 36267 uplims[w*16+g] = uplim;
244 36267 energies[w*16+g] = energy;
245 36267 nzs[w*16+g] = nz;
246 36267 sce->zeroes[w*16+g] = !nz;
247 36267 allz |= nz;
248
4/4
✓ Branch 0 taken 30219 times.
✓ Branch 1 taken 6048 times.
✓ Branch 2 taken 14475 times.
✓ Branch 3 taken 15744 times.
36267 if (nz && sce->can_pns[w*16+g]) {
249 14475 spread_thr_r[w*16+g] = energy * nz / (uplim * spread);
250
2/2
✓ Branch 0 taken 737 times.
✓ Branch 1 taken 13738 times.
14475 if (min_spread_thr_r < 0) {
251 737 min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+g];
252 } else {
253
2/2
✓ Branch 0 taken 3335 times.
✓ Branch 1 taken 10403 times.
13738 min_spread_thr_r = FFMIN(min_spread_thr_r, spread_thr_r[w*16+g]);
254
2/2
✓ Branch 0 taken 11655 times.
✓ Branch 1 taken 2083 times.
13738 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 739 minscaler = 65535;
262
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 739 times.
1512 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
263
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
264
2/2
✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 30219 times.
36267 if (sce->zeroes[w*16+g]) {
265 6048 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
266 6048 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 30219 sce->sf_idx[w*16+g] = av_clip(
275 SCALE_ONE_POS
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30219 times.
30219 + 1.75*log2f(FFMAX(0.00125f,uplims[w*16+g]) / sce->ics.swb_sizes[g])
277 30219 + sfoffs,
278 60, SCALE_MAX_POS);
279 30219 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
280 }
281 }
282
283 /** Clip */
284 739 minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
285
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 739 times.
1512 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
286
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++)
287
2/2
✓ Branch 0 taken 30219 times.
✓ Branch 1 taken 6048 times.
36267 if (!sce->zeroes[w*16+g])
288 30219 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 739 times.
739 if (!allz)
291 return;
292 739 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
293 739 ff_quantize_band_cost_cache_init(s);
294
295
2/2
✓ Branch 0 taken 94592 times.
✓ Branch 1 taken 739 times.
95331 for (i = 0; i < sizeof(minsf) / sizeof(minsf[0]); ++i)
296 94592 minsf[i] = 0;
297
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 739 times.
1512 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
298 773 start = w*128;
299
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
300 36267 const float *scaled = s->scoefs + start;
301 int minsfidx;
302 36267 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
303
2/2
✓ Branch 0 taken 36239 times.
✓ Branch 1 taken 28 times.
36267 if (maxvals[w*16+g] > 0) {
304 36239 minsfidx = coef2minsf(maxvals[w*16+g]);
305
2/2
✓ Branch 0 taken 36911 times.
✓ Branch 1 taken 36239 times.
73150 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
306 36911 minsf[(w+w2)*16+g] = minsfidx;
307 }
308 36267 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 739 memcpy(euplims, uplims, sizeof(euplims));
320
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 739 times.
1512 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 727 times.
773 float de_psy_factor = (sce->ics.num_windows > 1) ? 8.0f / sce->ics.group_len[w] : 1.0f;
323 773 start = w*128;
324
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
325
2/2
✓ Branch 0 taken 30243 times.
✓ Branch 1 taken 6024 times.
36267 if (nzs[g] > 0) {
326 30243 float cleanup_factor = ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
327 30243 float energy2uplim = find_form_factor(
328 30243 sce->ics.group_len[w], sce->ics.swb_sizes[g],
329 30243 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
330 30243 sce->coeffs + start,
331 nzslope * cleanup_factor);
332 30243 energy2uplim *= de_psy_factor;
333
1/2
✓ Branch 0 taken 30243 times.
✗ Branch 1 not taken.
30243 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
334 /** In ABR, we need to priorize less and let rate control do its thing */
335 30243 energy2uplim = sqrtf(energy2uplim);
336 }
337
5/6
✓ Branch 0 taken 30046 times.
✓ Branch 1 taken 197 times.
✓ Branch 2 taken 30046 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30046 times.
✓ Branch 5 taken 197 times.
30243 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
338 30243 uplims[w*16+g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
339 30243 * sce->ics.group_len[w];
340
341 30243 energy2uplim = find_form_factor(
342 30243 sce->ics.group_len[w], sce->ics.swb_sizes[g],
343 30243 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
344 30243 sce->coeffs + start,
345 2.0f);
346 30243 energy2uplim *= de_psy_factor;
347
1/2
✓ Branch 0 taken 30243 times.
✗ Branch 1 not taken.
30243 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
348 /** In ABR, we need to priorize less and let rate control do its thing */
349 30243 energy2uplim = sqrtf(energy2uplim);
350 }
351
5/6
✓ Branch 0 taken 30085 times.
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 30085 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30085 times.
✓ Branch 5 taken 158 times.
30243 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
352 30243 euplims[w*16+g] *= av_clipf(rdlambda * energy2uplim * sce->ics.group_len[w],
353 0.5f, 1.0f);
354 }
355 36267 start += sce->ics.swb_sizes[g];
356 }
357 }
358
359
2/2
✓ Branch 0 taken 94592 times.
✓ Branch 1 taken 739 times.
95331 for (i = 0; i < sizeof(maxsf) / sizeof(maxsf[0]); ++i)
360 94592 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 18373 times.
✓ Branch 1 taken 739 times.
19112 int qstep = its ? 1 : 32;
368 do {
369 52697 int changed = 0;
370 52697 prev = -1;
371 52697 recomprd = 0;
372 52697 tbits = 0;
373
2/2
✓ Branch 0 taken 61400 times.
✓ Branch 1 taken 52697 times.
114097 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
374 61400 start = w*128;
375
2/2
✓ Branch 0 taken 2598575 times.
✓ Branch 1 taken 61400 times.
2659975 for (g = 0; g < sce->ics.num_swb; g++) {
376 2598575 const float *coefs = &sce->coeffs[start];
377 2598575 const float *scaled = &s->scoefs[start];
378 2598575 int bits = 0;
379 int cb;
380 2598575 float dist = 0.0f;
381 2598575 float qenergy = 0.0f;
382
383
3/4
✓ Branch 0 taken 2172483 times.
✓ Branch 1 taken 426092 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2172483 times.
2598575 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
384 426092 start += sce->ics.swb_sizes[g];
385
2/2
✓ Branch 0 taken 21104 times.
✓ Branch 1 taken 404988 times.
426092 if (sce->can_pns[w*16+g]) {
386 /** PNS isn't free */
387 21104 tbits += ff_pns_bits(sce, w, g);
388 }
389 426092 continue;
390 }
391 2172483 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
392
2/2
✓ Branch 0 taken 2318211 times.
✓ Branch 1 taken 2172483 times.
4490694 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
393 int b;
394 float sqenergy;
395 4636422 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
396 2318211 scaled + w2*128,
397 2318211 sce->ics.swb_sizes[g],
398 2318211 sce->sf_idx[w*16+g],
399 cb,
400 1.0f,
401 INFINITY,
402 &b, &sqenergy,
403 0);
404 2318211 bits += b;
405 2318211 qenergy += sqenergy;
406 }
407 2172483 dists[w*16+g] = dist - bits;
408 2172483 qenergies[w*16+g] = qenergy;
409
2/2
✓ Branch 0 taken 2119786 times.
✓ Branch 1 taken 52697 times.
2172483 if (prev != -1) {
410 2119786 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
411 2119786 bits += ff_aac_scalefactor_bits[sfdiff];
412 }
413 2172483 tbits += bits;
414 2172483 start += sce->ics.swb_sizes[g];
415 2172483 prev = sce->sf_idx[w*16+g];
416 }
417 }
418
2/2
✓ Branch 0 taken 30117 times.
✓ Branch 1 taken 22580 times.
52697 if (tbits > toomanybits) {
419 30117 recomprd = 1;
420
2/2
✓ Branch 0 taken 3854976 times.
✓ Branch 1 taken 30117 times.
3885093 for (i = 0; i < 128; i++) {
421
2/2
✓ Branch 0 taken 3252463 times.
✓ Branch 1 taken 602513 times.
3854976 if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) {
422
1/2
✓ Branch 0 taken 3252463 times.
✗ Branch 1 not taken.
3252463 int maxsf_i = (tbits > 5800) ? SCALE_MAX_POS : maxsf[i];
423 3252463 int new_sf = FFMIN(maxsf_i, sce->sf_idx[i] + qstep);
424
2/2
✓ Branch 0 taken 3193772 times.
✓ Branch 1 taken 58691 times.
3252463 if (new_sf != sce->sf_idx[i]) {
425 3193772 sce->sf_idx[i] = new_sf;
426 3193772 changed = 1;
427 }
428 }
429 }
430
2/2
✓ Branch 0 taken 8811 times.
✓ Branch 1 taken 13769 times.
22580 } else if (tbits < toofewbits) {
431 8811 recomprd = 1;
432
2/2
✓ Branch 0 taken 1127808 times.
✓ Branch 1 taken 8811 times.
1136619 for (i = 0; i < 128; i++) {
433
2/2
✓ Branch 0 taken 198474 times.
✓ Branch 1 taken 929334 times.
1127808 if (sce->sf_idx[i] > SCALE_ONE_POS) {
434 198474 int new_sf = FFMAX3(minsf[i], SCALE_ONE_POS, sce->sf_idx[i] - qstep);
435
1/2
✓ Branch 0 taken 198474 times.
✗ Branch 1 not taken.
198474 if (new_sf != sce->sf_idx[i]) {
436 198474 sce->sf_idx[i] = new_sf;
437 198474 changed = 1;
438 }
439 }
440 }
441 }
442 52697 qstep >>= 1;
443
7/8
✓ Branch 0 taken 49002 times.
✓ Branch 1 taken 3695 times.
✓ Branch 2 taken 29916 times.
✓ Branch 3 taken 19086 times.
✓ Branch 4 taken 29890 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 29890 times.
✗ Branch 7 not taken.
52697 if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217 && changed)
444 29890 qstep = 1;
445
2/2
✓ Branch 0 taken 33585 times.
✓ Branch 1 taken 19112 times.
52697 } while (qstep);
446
447 19112 overdist = 1;
448 19112 fflag = tbits < toofewbits;
449
5/6
✓ Branch 0 taken 38224 times.
✓ Branch 1 taken 13972 times.
✓ Branch 2 taken 33084 times.
✓ Branch 3 taken 5140 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5140 times.
52196 for (i = 0; i < 2 && (overdist || recomprd); ++i) {
450
2/2
✓ Branch 0 taken 11765 times.
✓ Branch 1 taken 21319 times.
33084 if (recomprd) {
451 /** Must recompute distortion */
452 11765 prev = -1;
453 11765 tbits = 0;
454
2/2
✓ Branch 0 taken 11844 times.
✓ Branch 1 taken 11765 times.
23609 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
455 11844 start = w*128;
456
2/2
✓ Branch 0 taken 576576 times.
✓ Branch 1 taken 11844 times.
588420 for (g = 0; g < sce->ics.num_swb; g++) {
457 576576 const float *coefs = sce->coeffs + start;
458 576576 const float *scaled = s->scoefs + start;
459 576576 int bits = 0;
460 int cb;
461 576576 float dist = 0.0f;
462 576576 float qenergy = 0.0f;
463
464
3/4
✓ Branch 0 taken 498895 times.
✓ Branch 1 taken 77681 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 498895 times.
576576 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
465 77681 start += sce->ics.swb_sizes[g];
466
2/2
✓ Branch 0 taken 6371 times.
✓ Branch 1 taken 71310 times.
77681 if (sce->can_pns[w*16+g]) {
467 /** PNS isn't free */
468 6371 tbits += ff_pns_bits(sce, w, g);
469 }
470 77681 continue;
471 }
472 498895 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
473
2/2
✓ Branch 0 taken 500301 times.
✓ Branch 1 taken 498895 times.
999196 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
474 int b;
475 float sqenergy;
476 1000602 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
477 500301 scaled + w2*128,
478 500301 sce->ics.swb_sizes[g],
479 500301 sce->sf_idx[w*16+g],
480 cb,
481 1.0f,
482 INFINITY,
483 &b, &sqenergy,
484 0);
485 500301 bits += b;
486 500301 qenergy += sqenergy;
487 }
488 498895 dists[w*16+g] = dist - bits;
489 498895 qenergies[w*16+g] = qenergy;
490
2/2
✓ Branch 0 taken 487130 times.
✓ Branch 1 taken 11765 times.
498895 if (prev != -1) {
491 487130 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
492 487130 bits += ff_aac_scalefactor_bits[sfdiff];
493 }
494 498895 tbits += bits;
495 498895 start += sce->ics.swb_sizes[g];
496 498895 prev = sce->sf_idx[w*16+g];
497 }
498 }
499 }
500
7/8
✓ Branch 0 taken 19112 times.
✓ Branch 1 taken 13972 times.
✓ Branch 2 taken 19112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8331 times.
✓ Branch 5 taken 10781 times.
✓ Branch 6 taken 6325 times.
✓ Branch 7 taken 2006 times.
33084 if (!i && s->options.pns && its > maxits/2 && tbits > toofewbits) {
501 6325 float maxoverdist = 0.0f;
502 6325 float ovrfactor = 1.f+(maxits-its)*16.f/maxits;
503 6325 overdist = recomprd = 0;
504
2/2
✓ Branch 0 taken 6801 times.
✓ Branch 1 taken 6325 times.
13126 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
505
2/2
✓ Branch 0 taken 310709 times.
✓ Branch 1 taken 6801 times.
317510 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
506
6/6
✓ Branch 0 taken 248713 times.
✓ Branch 1 taken 61996 times.
✓ Branch 2 taken 46346 times.
✓ Branch 3 taken 202367 times.
✓ Branch 4 taken 3723 times.
✓ Branch 5 taken 42623 times.
310709 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 1304 times.
✓ Branch 1 taken 2419 times.
3723 float ovrdist = dists[w*16+g] / FFMAX(uplims[w*16+g],euplims[w*16+g]);
508
2/2
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 1671 times.
3723 maxoverdist = FFMAX(maxoverdist, ovrdist);
509 3723 overdist++;
510 }
511 }
512 }
513
2/2
✓ Branch 0 taken 1185 times.
✓ Branch 1 taken 5140 times.
6325 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 1185 float minspread = max_spread_thr_r;
518 1185 float maxspread = min_spread_thr_r;
519 float zspread;
520 1185 int zeroable = 0;
521 1185 int zeroed = 0;
522 int maxzeroed, zloop;
523
2/2
✓ Branch 0 taken 1605 times.
✓ Branch 1 taken 1185 times.
2790 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
524
2/2
✓ Branch 0 taken 59045 times.
✓ Branch 1 taken 1605 times.
60650 for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
525
6/6
✓ Branch 0 taken 30605 times.
✓ Branch 1 taken 28440 times.
✓ Branch 2 taken 19393 times.
✓ Branch 3 taken 11212 times.
✓ Branch 4 taken 18236 times.
✓ Branch 5 taken 1157 times.
59045 if (start >= pns_start_pos && !sce->zeroes[w*16+g] && sce->can_pns[w*16+g]) {
526
2/2
✓ Branch 0 taken 5931 times.
✓ Branch 1 taken 12305 times.
18236 minspread = FFMIN(minspread, spread_thr_r[w*16+g]);
527
2/2
✓ Branch 0 taken 15181 times.
✓ Branch 1 taken 3055 times.
18236 maxspread = FFMAX(maxspread, spread_thr_r[w*16+g]);
528 18236 zeroable++;
529 }
530 }
531 }
532 1185 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 1005 times.
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1175 times.
✓ Branch 4 taken 995 times.
✓ Branch 5 taken 180 times.
1185 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 1185 maxzeroed = FFMIN(zeroable, FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
541
2/2
✓ Branch 0 taken 2370 times.
✓ Branch 1 taken 1185 times.
3555 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 1185 times.
✓ Branch 1 taken 1185 times.
2370 float loopovrfactor = (zloop) ? 1.0f : ovrfactor;
549
2/2
✓ Branch 0 taken 1185 times.
✓ Branch 1 taken 1185 times.
2370 int loopminsf = (zloop) ? (SCALE_ONE_POS - SCALE_DIV_512) : SCALE_ONE_POS;
550 int mcb;
551
4/4
✓ Branch 0 taken 101079 times.
✓ Branch 1 taken 2286 times.
✓ Branch 2 taken 100995 times.
✓ Branch 3 taken 84 times.
103365 for (g = sce->ics.num_swb-1; g > 0 && zeroed < maxzeroed; g--) {
552
2/2
✓ Branch 0 taken 47574 times.
✓ Branch 1 taken 53421 times.
100995 if (sce->ics.swb_offset[g] < pns_start_pos)
553 47574 continue;
554
2/2
✓ Branch 0 taken 60126 times.
✓ Branch 1 taken 53421 times.
113547 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
555
6/6
✓ Branch 0 taken 37785 times.
✓ Branch 1 taken 22341 times.
✓ Branch 2 taken 35485 times.
✓ Branch 3 taken 2300 times.
✓ Branch 4 taken 4306 times.
✓ Branch 5 taken 31179 times.
60126 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 2294 times.
✓ Branch 1 taken 2012 times.
4306 && sce->sf_idx[w*16+g] > loopminsf
557
3/4
✓ Branch 0 taken 438 times.
✓ Branch 1 taken 1856 times.
✓ Branch 3 taken 438 times.
✗ Branch 4 not taken.
2294 && (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 377 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 248 times.
✓ Branch 3 taken 129 times.
✓ Branch 4 taken 313 times.
✓ Branch 5 taken 64 times.
438 || (mcb <= 1 && dists[w*16+g] > FFMIN(uplims[w*16+g], euplims[w*16+g]))) ) {
559 2169 sce->zeroes[w*16+g] = 1;
560 2169 sce->band_type[w*16+g] = 0;
561 2169 zeroed++;
562 }
563 }
564 }
565 }
566
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 165 times.
1185 if (zeroed)
567 1020 recomprd = fflag = 1;
568 } else {
569 5140 overdist = 0;
570 }
571 }
572 }
573
574 19112 minscaler = SCALE_MAX_POS;
575
2/2
✓ Branch 0 taken 20132 times.
✓ Branch 1 taken 19112 times.
39244 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
576
2/2
✓ Branch 0 taken 938168 times.
✓ Branch 1 taken 20132 times.
958300 for (g = 0; g < sce->ics.num_swb; g++) {
577
2/2
✓ Branch 0 taken 775629 times.
✓ Branch 1 taken 162539 times.
938168 if (!sce->zeroes[w*16+g]) {
578 775629 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
579 }
580 }
581 }
582
583 19112 minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
584 19112 prev = -1;
585
2/2
✓ Branch 0 taken 20132 times.
✓ Branch 1 taken 19112 times.
39244 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 8807 times.
✓ Branch 1 taken 11325 times.
✓ Branch 2 taken 5560 times.
✓ Branch 3 taken 3247 times.
20132 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
588 20132 int edepth = depth+2;
589 20132 float uplmax = its / (maxits*0.25f) + 1.0f;
590
5/8
✓ Branch 0 taken 13231 times.
✓ Branch 1 taken 6901 times.
✓ Branch 2 taken 13231 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13231 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13231 times.
✗ Branch 7 not taken.
20132 uplmax *= (tbits > destbits) ? FFMIN(2.0f, tbits / (float)FFMAX(1,destbits)) : 1.0f;
591 20132 start = w * 128;
592
2/2
✓ Branch 0 taken 938168 times.
✓ Branch 1 taken 20132 times.
958300 for (g = 0; g < sce->ics.num_swb; g++) {
593 938168 int prevsc = sce->sf_idx[w*16+g];
594
4/4
✓ Branch 0 taken 68456 times.
✓ Branch 1 taken 869712 times.
✓ Branch 2 taken 19112 times.
✓ Branch 3 taken 49344 times.
938168 if (prev < 0 && !sce->zeroes[w*16+g])
595 19112 prev = sce->sf_idx[0];
596
2/2
✓ Branch 0 taken 775629 times.
✓ Branch 1 taken 162539 times.
938168 if (!sce->zeroes[w*16+g]) {
597 775629 const float *coefs = sce->coeffs + start;
598 775629 const float *scaled = s->scoefs + start;
599 775629 int cmb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
600 775629 int mindeltasf = FFMAX(0, prev - SCALE_MAX_DIFF);
601 775629 int maxdeltasf = FFMIN(SCALE_MAX_POS - SCALE_DIV_512, prev + SCALE_MAX_DIFF);
602
6/6
✓ Branch 0 taken 734861 times.
✓ Branch 1 taken 40768 times.
✓ Branch 2 taken 436764 times.
✓ Branch 3 taken 298097 times.
✓ Branch 4 taken 476577 times.
✓ Branch 5 taken 955 times.
775629 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 1817498 times.
✓ Branch 1 taken 56567 times.
✓ Branch 2 taken 1817378 times.
✓ Branch 3 taken 120 times.
1874065 for (i = 0; i < edepth && sce->sf_idx[w*16+g] > mindeltasf; ++i) {
610 int cb, bits;
611 float dist, qenergy;
612 1817378 int mb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1);
613 1817378 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
614 1817378 dist = qenergy = 0.f;
615 1817378 bits = 0;
616
2/2
✓ Branch 0 taken 334688 times.
✓ Branch 1 taken 1482690 times.
1817378 if (!cb) {
617 334688 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g]-1, maxsf[w*16+g]);
618
4/4
✓ Branch 0 taken 220546 times.
✓ Branch 1 taken 1262144 times.
✓ Branch 2 taken 89331 times.
✓ Branch 3 taken 131215 times.
1482690 } else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) {
619 89331 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 34477 times.
✓ Branch 1 taken 1693570 times.
✓ Branch 2 taken 3086 times.
✓ Branch 3 taken 31391 times.
✓ Branch 4 taken 1376 times.
✓ Branch 5 taken 1710 times.
1728047 if (!g && sce->ics.num_windows > 1 && dists[w*16+g] >= euplims[w*16+g])
626 1376 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
627
2/2
✓ Branch 0 taken 1840799 times.
✓ Branch 1 taken 1728047 times.
3568846 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
628 int b;
629 float sqenergy;
630 3681598 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
631 1840799 scaled + w2*128,
632 1840799 sce->ics.swb_sizes[g],
633 1840799 sce->sf_idx[w*16+g]-1,
634 cb,
635 1.0f,
636 INFINITY,
637 &b, &sqenergy,
638 0);
639 1840799 bits += b;
640 1840799 qenergy += sqenergy;
641 }
642 1728047 sce->sf_idx[w*16+g]--;
643 1728047 dists[w*16+g] = dist - bits;
644 1728047 qenergies[w*16+g] = qenergy;
645
5/6
✓ Branch 0 taken 1415495 times.
✓ Branch 1 taken 312552 times.
✓ Branch 2 taken 1415495 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 689228 times.
✓ Branch 5 taken 726267 times.
3143542 if (mb && (sce->sf_idx[w*16+g] < mindeltasf || (
646
2/2
✓ Branch 0 taken 1168962 times.
✓ Branch 1 taken 246533 times.
1415495 (dists[w*16+g] < FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g]))
647
2/2
✓ Branch 0 taken 358669 times.
✓ Branch 1 taken 330559 times.
689228 && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
648 ) )) {
649 break;
650 }
651 }
652
4/4
✓ Branch 0 taken 119307 times.
✓ Branch 1 taken 179745 times.
✓ Branch 2 taken 116533 times.
✓ Branch 3 taken 2774 times.
299052 } else if (tbits > toofewbits && sce->sf_idx[w*16+g] < FFMIN(maxdeltasf, maxsf[w*16+g])
653
4/4
✓ Branch 0 taken 70481 times.
✓ Branch 1 taken 46052 times.
✓ Branch 2 taken 105072 times.
✓ Branch 3 taken 11461 times.
116533 && (dists[w*16+g] < FFMIN(euplims[w*16+g], uplims[w*16+g]))
654
2/2
✓ Branch 0 taken 21029 times.
✓ Branch 1 taken 84043 times.
105072 && (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
4/4
✓ Branch 0 taken 33783 times.
✓ Branch 1 taken 884 times.
✓ Branch 2 taken 33781 times.
✓ Branch 3 taken 2 times.
34667 for (i = 0; i < depth && sce->sf_idx[w*16+g] < maxdeltasf; ++i) {
658 int cb, bits;
659 float dist, qenergy;
660 33781 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]+1);
661
2/2
✓ Branch 0 taken 33768 times.
✓ Branch 1 taken 13 times.
33781 if (cb > 0) {
662 33768 dist = qenergy = 0.f;
663 33768 bits = 0;
664
2/2
✓ Branch 0 taken 33812 times.
✓ Branch 1 taken 33768 times.
67580 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
665 int b;
666 float sqenergy;
667 67624 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
668 33812 scaled + w2*128,
669 33812 sce->ics.swb_sizes[g],
670 33812 sce->sf_idx[w*16+g]+1,
671 cb,
672 1.0f,
673 INFINITY,
674 &b, &sqenergy,
675 0);
676 33812 bits += b;
677 33812 qenergy += sqenergy;
678 }
679 33768 dist -= bits;
680
4/4
✓ Branch 0 taken 26291 times.
✓ Branch 1 taken 7477 times.
✓ Branch 2 taken 13638 times.
✓ Branch 3 taken 20130 times.
33768 if (dist < FFMIN(euplims[w*16+g], uplims[w*16+g])) {
681 13638 sce->sf_idx[w*16+g]++;
682 13638 dists[w*16+g] = dist;
683 13638 qenergies[w*16+g] = qenergy;
684 } else {
685 20130 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 775629 prev = sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], mindeltasf, maxdeltasf);
694
2/2
✓ Branch 0 taken 482112 times.
✓ Branch 1 taken 293517 times.
775629 if (sce->sf_idx[w*16+g] != prevsc)
695 482112 fflag = 1;
696 775629 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]);
697 775629 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
698 }
699 938168 start += sce->ics.swb_sizes[g];
700 }
701 }
702
703 /** SF difference limit violation risk. Must re-clamp. */
704 19112 prev = -1;
705
2/2
✓ Branch 0 taken 20132 times.
✓ Branch 1 taken 19112 times.
39244 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
706
2/2
✓ Branch 0 taken 938168 times.
✓ Branch 1 taken 20132 times.
958300 for (g = 0; g < sce->ics.num_swb; g++) {
707
2/2
✓ Branch 0 taken 775629 times.
✓ Branch 1 taken 162539 times.
938168 if (!sce->zeroes[w*16+g]) {
708 775629 int prevsf = sce->sf_idx[w*16+g];
709
2/2
✓ Branch 0 taken 19112 times.
✓ Branch 1 taken 756517 times.
775629 if (prev < 0)
710 19112 prev = prevsf;
711 775629 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], prev - SCALE_MAX_DIFF, prev + SCALE_MAX_DIFF);
712 775629 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
713 775629 prev = sce->sf_idx[w*16+g];
714
3/4
✓ Branch 0 taken 7508 times.
✓ Branch 1 taken 768121 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7508 times.
775629 if (!fflag && prevsf != sce->sf_idx[w*16+g])
715 fflag = 1;
716 }
717 }
718 }
719
720 19112 its++;
721
4/4
✓ Branch 0 taken 18914 times.
✓ Branch 1 taken 198 times.
✓ Branch 2 taken 18373 times.
✓ Branch 3 taken 541 times.
19112 } while (fflag && its < maxits);
722
723 /** Scout out next nonzero bands */
724 739 ff_init_nextband_map(sce, nextband);
725
726 739 prev = -1;
727
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 739 times.
1512 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 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
730
2/2
✓ Branch 0 taken 28050 times.
✓ Branch 1 taken 8217 times.
36267 if (!sce->zeroes[w*16+g]) {
731 28050 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 68 times.
✓ Branch 1 taken 27982 times.
28050 if (sce->band_type[w*16+g] <= 0) {
733
1/2
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
68 if (!ff_sfdelta_can_remove_band(sce, nextband, prev, w*16+g)) {
734 /** Cannot zero out, make sure it's not attempted */
735 68 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 8217 sce->band_type[w*16+g] = 0;
743 }
744 /** Check that there's no SF delta range violations */
745
2/2
✓ Branch 0 taken 28050 times.
✓ Branch 1 taken 8217 times.
36267 if (!sce->zeroes[w*16+g]) {
746
2/2
✓ Branch 0 taken 27311 times.
✓ Branch 1 taken 739 times.
28050 if (prev != -1) {
747 27311 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 194 times.
✓ Branch 1 taken 545 times.
739 } else if (sce->zeroes[0]) {
750 /** Set global gain to something useful */
751 194 sce->sf_idx[0] = sce->sf_idx[w*16+g];
752 }
753 28050 prev = sce->sf_idx[w*16+g];
754 }
755 }
756 }
757 }
758
759 #endif /* AVCODEC_AACCODER_TWOLOOP_H */
760