FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aaccoder_twoloop.h
Date: 2024-04-23 06:12:56
Exec Total Coverage
Lines: 375 388 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 #define sclip(x) av_clip(x,60,218)
57
58 /* Reflects the cost to change codebooks */
59 27475 static inline int ff_pns_bits(SingleChannelElement *sce, int w, int g)
60 {
61
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;
62 }
63
64 /**
65 * two-loop quantizers search taken from ISO 13818-7 Appendix C
66 */
67 739 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
68 AACEncContext *s,
69 SingleChannelElement *sce,
70 const float lambda)
71 {
72 739 int start = 0, i, w, w2, g, recomprd;
73 1478 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
74
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)
75 739 * (lambda / 120.f);
76 739 int refbits = destbits;
77 int toomanybits, toofewbits;
78 char nzs[128];
79 uint8_t nextband[128];
80 int maxsf[128], minsf[128];
81 739 float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
82 float maxvals[128], spread_thr_r[128];
83 float min_spread_thr_r, max_spread_thr_r;
84
85 /**
86 * rdlambda controls the maximum tolerated distortion. Twoloop
87 * will keep iterating until it fails to lower it or it reaches
88 * ulimit * rdlambda. Keeping it low increases quality on difficult
89 * signals, but lower it too much, and bits will be taken from weak
90 * signals, creating "holes". A balance is necessary.
91 * rdmax and rdmin specify the relative deviation from rdlambda
92 * allowed for tonality compensation
93 */
94 739 float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f);
95 739 const float nzslope = 1.5f;
96 739 float rdmin = 0.03125f;
97 739 float rdmax = 1.0f;
98
99 /**
100 * sfoffs controls an offset of optmium allocation that will be
101 * applied based on lambda. Keep it real and modest, the loop
102 * will take care of the rest, this just accelerates convergence
103 */
104 739 float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10);
105
106 int fflag, minscaler, maxscaler, nminscaler;
107 739 int its = 0;
108 739 int maxits = 30;
109 739 int allz = 0;
110 int tbits;
111 739 int cutoff = 1024;
112 int pns_start_pos;
113 int prev;
114
115 /**
116 * zeroscale controls a multiplier of the threshold, if band energy
117 * is below this, a zero is forced. Keep it lower than 1, unless
118 * low lambda is used, because energy < threshold doesn't mean there's
119 * no audible signal outright, it's just energy. Also make it rise
120 * slower than rdlambda, as rdscale has due compensation with
121 * noisy band depriorization below, whereas zeroing logic is rather dumb
122 */
123 float zeroscale;
124
2/2
✓ Branch 0 taken 641 times.
✓ Branch 1 taken 98 times.
739 if (lambda > 120.f) {
125 641 zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f);
126 } else {
127 98 zeroscale = 1.f;
128 }
129
130
1/2
✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
739 if (s->psy.bitres.alloc >= 0) {
131 /**
132 * Psy granted us extra bits to use, from the reservoire
133 * adjust for lambda except what psy already did
134 */
135 1478 destbits = s->psy.bitres.alloc
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 * (lambda / (avctx->global_quality ? avctx->global_quality : 120));
137 }
138
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
140 /**
141 * Constant Q-scale doesn't compensate MS coding on its own
142 * No need to be overly precise, this only controls RD
143 * adjustment CB limits when going overboard
144 */
145 if (s->options.mid_side && s->cur_type == TYPE_CPE)
146 destbits *= 2;
147
148 /**
149 * When using a constant Q-scale, don't adjust bits, just use RD
150 * Don't let it go overboard, though... 8x psy target is enough
151 */
152 toomanybits = 5800;
153 toofewbits = destbits / 16;
154
155 /** Don't offset scalers, just RD */
156 sfoffs = sce->ics.num_windows - 1;
157 rdlambda = sqrtf(rdlambda);
158
159 /** search further */
160 maxits *= 2;
161 } else {
162 /* When using ABR, be strict, but a reasonable leeway is
163 * critical to allow RC to smoothly track desired bitrate
164 * without sudden quality drops that cause audible artifacts.
165 * Symmetry is also desirable, to avoid systematic bias.
166 */
167 739 toomanybits = destbits + destbits/8;
168 739 toofewbits = destbits - destbits/8;
169
170 739 sfoffs = 0;
171 739 rdlambda = sqrtf(rdlambda);
172 }
173
174 /** and zero out above cutoff frequency */
175 {
176 739 int wlen = 1024 / sce->ics.num_windows;
177 int bandwidth;
178
179 /**
180 * Scale, psy gives us constant quality, this LP only scales
181 * bitrate by lambda, so we save bits on subjectively unimportant HF
182 * rather than increase quantization noise. Adjust nominal bitrate
183 * to effective bitrate according to encoding parameters,
184 * AAC_CUTOFF_FROM_BITRATE is calibrated for effective bitrate.
185 */
186 739 float rate_bandwidth_multiplier = 1.5f;
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
188 ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
189 739 : (avctx->bit_rate / avctx->ch_layout.nb_channels);
190
191 /** Compensate for extensions that increase efficiency */
192
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)
193 739 frame_bit_rate *= 1.15f;
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 if (avctx->cutoff > 0) {
196 bandwidth = avctx->cutoff;
197 } else {
198
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));
199 739 s->psy.cutoff = bandwidth;
200 }
201
202 739 cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
203 739 pns_start_pos = NOISE_LOW_LIMIT * 2 * wlen / avctx->sample_rate;
204 }
205
206 /**
207 * for values above this the decoder might end up in an endless loop
208 * due to always having more bits than what can be encoded.
209 */
210 739 destbits = FFMIN(destbits, 5800);
211 739 toomanybits = FFMIN(toomanybits, 5800);
212 739 toofewbits = FFMIN(toofewbits, 5800);
213 /**
214 * XXX: some heuristic to determine initial quantizers will reduce search time
215 * determine zero bands and upper distortion limits
216 */
217 739 min_spread_thr_r = -1;
218 739 max_spread_thr_r = -1;
219
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]) {
220
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++]) {
221 36267 int nz = 0;
222 36267 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
223
2/2
✓ Branch 0 taken 36967 times.
✓ Branch 1 taken 36267 times.
73234 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
224 36967 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
225
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) {
226 6218 sce->zeroes[(w+w2)*16+g] = 1;
227 6218 continue;
228 }
229 30749 nz = 1;
230 }
231
2/2
✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 30219 times.
36267 if (!nz) {
232 6048 uplim = 0.0f;
233 } else {
234 30219 nz = 0;
235
2/2
✓ Branch 0 taken 30803 times.
✓ Branch 1 taken 30219 times.
61022 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
236 30803 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
237
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)
238 54 continue;
239 30749 uplim += band->threshold;
240 30749 energy += band->energy;
241 30749 spread += band->spread;
242 30749 nz++;
243 }
244 }
245 36267 uplims[w*16+g] = uplim;
246 36267 energies[w*16+g] = energy;
247 36267 nzs[w*16+g] = nz;
248 36267 sce->zeroes[w*16+g] = !nz;
249 36267 allz |= nz;
250
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]) {
251 14475 spread_thr_r[w*16+g] = energy * nz / (uplim * spread);
252
2/2
✓ Branch 0 taken 737 times.
✓ Branch 1 taken 13738 times.
14475 if (min_spread_thr_r < 0) {
253 737 min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+g];
254 } else {
255
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]);
256
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]);
257 }
258 }
259 }
260 }
261
262 /** Compute initial scalers */
263 739 minscaler = 65535;
264
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]) {
265
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
266
2/2
✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 30219 times.
36267 if (sce->zeroes[w*16+g]) {
267 6048 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
268 6048 continue;
269 }
270 /**
271 * log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2).
272 * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion,
273 * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus
274 * more robust.
275 */
276 30219 sce->sf_idx[w*16+g] = av_clip(
277 SCALE_ONE_POS
278
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])
279 30219 + sfoffs,
280 60, SCALE_MAX_POS);
281 30219 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
282 }
283 }
284
285 /** Clip */
286 739 minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
287
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])
288
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++)
289
2/2
✓ Branch 0 taken 30219 times.
✓ Branch 1 taken 6048 times.
36267 if (!sce->zeroes[w*16+g])
290 30219 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1);
291
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739 times.
739 if (!allz)
293 return;
294 739 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
295 739 ff_quantize_band_cost_cache_init(s);
296
297
2/2
✓ Branch 0 taken 94592 times.
✓ Branch 1 taken 739 times.
95331 for (i = 0; i < sizeof(minsf) / sizeof(minsf[0]); ++i)
298 94592 minsf[i] = 0;
299
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]) {
300 773 start = w*128;
301
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
302 36267 const float *scaled = s->scoefs + start;
303 int minsfidx;
304 36267 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
305
2/2
✓ Branch 0 taken 36239 times.
✓ Branch 1 taken 28 times.
36267 if (maxvals[w*16+g] > 0) {
306 36239 minsfidx = coef2minsf(maxvals[w*16+g]);
307
2/2
✓ Branch 0 taken 36911 times.
✓ Branch 1 taken 36239 times.
73150 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
308 36911 minsf[(w+w2)*16+g] = minsfidx;
309 }
310 36267 start += sce->ics.swb_sizes[g];
311 }
312 }
313
314 /**
315 * Scale uplims to match rate distortion to quality
316 * bu applying noisy band depriorization and tonal band priorization.
317 * Maxval-energy ratio gives us an idea of how noisy/tonal the band is.
318 * If maxval^2 ~ energy, then that band is mostly noise, and we can relax
319 * rate distortion requirements.
320 */
321 739 memcpy(euplims, uplims, sizeof(euplims));
322
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]) {
323 /** psy already priorizes transients to some extent */
324
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;
325 773 start = w*128;
326
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
327
2/2
✓ Branch 0 taken 30243 times.
✓ Branch 1 taken 6024 times.
36267 if (nzs[g] > 0) {
328 30243 float cleanup_factor = ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
329 30243 float energy2uplim = find_form_factor(
330 30243 sce->ics.group_len[w], sce->ics.swb_sizes[g],
331 30243 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
332 30243 sce->coeffs + start,
333 nzslope * cleanup_factor);
334 30243 energy2uplim *= de_psy_factor;
335
1/2
✓ Branch 0 taken 30243 times.
✗ Branch 1 not taken.
30243 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
336 /** In ABR, we need to priorize less and let rate control do its thing */
337 30243 energy2uplim = sqrtf(energy2uplim);
338 }
339
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));
340 30243 uplims[w*16+g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
341 30243 * sce->ics.group_len[w];
342
343 30243 energy2uplim = find_form_factor(
344 30243 sce->ics.group_len[w], sce->ics.swb_sizes[g],
345 30243 uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
346 30243 sce->coeffs + start,
347 2.0f);
348 30243 energy2uplim *= de_psy_factor;
349
1/2
✓ Branch 0 taken 30243 times.
✗ Branch 1 not taken.
30243 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
350 /** In ABR, we need to priorize less and let rate control do its thing */
351 30243 energy2uplim = sqrtf(energy2uplim);
352 }
353
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));
354 30243 euplims[w*16+g] *= av_clipf(rdlambda * energy2uplim * sce->ics.group_len[w],
355 0.5f, 1.0f);
356 }
357 36267 start += sce->ics.swb_sizes[g];
358 }
359 }
360
361
2/2
✓ Branch 0 taken 94592 times.
✓ Branch 1 taken 739 times.
95331 for (i = 0; i < sizeof(maxsf) / sizeof(maxsf[0]); ++i)
362 94592 maxsf[i] = SCALE_MAX_POS;
363
364 //perform two-loop search
365 //outer loop - improve quality
366 do {
367 //inner loop - quantize spectrum to fit into given number of bits
368 int overdist;
369
2/2
✓ Branch 0 taken 18373 times.
✓ Branch 1 taken 739 times.
19112 int qstep = its ? 1 : 32;
370 do {
371 52697 int changed = 0;
372 52697 prev = -1;
373 52697 recomprd = 0;
374 52697 tbits = 0;
375
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]) {
376 61400 start = w*128;
377
2/2
✓ Branch 0 taken 2598575 times.
✓ Branch 1 taken 61400 times.
2659975 for (g = 0; g < sce->ics.num_swb; g++) {
378 2598575 const float *coefs = &sce->coeffs[start];
379 2598575 const float *scaled = &s->scoefs[start];
380 2598575 int bits = 0;
381 int cb;
382 2598575 float dist = 0.0f;
383 2598575 float qenergy = 0.0f;
384
385
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) {
386 426092 start += sce->ics.swb_sizes[g];
387
2/2
✓ Branch 0 taken 21104 times.
✓ Branch 1 taken 404988 times.
426092 if (sce->can_pns[w*16+g]) {
388 /** PNS isn't free */
389 21104 tbits += ff_pns_bits(sce, w, g);
390 }
391 426092 continue;
392 }
393 2172483 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
394
2/2
✓ Branch 0 taken 2318211 times.
✓ Branch 1 taken 2172483 times.
4490694 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
395 int b;
396 float sqenergy;
397 4636422 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
398 2318211 scaled + w2*128,
399 2318211 sce->ics.swb_sizes[g],
400 2318211 sce->sf_idx[w*16+g],
401 cb,
402 1.0f,
403 INFINITY,
404 &b, &sqenergy,
405 0);
406 2318211 bits += b;
407 2318211 qenergy += sqenergy;
408 }
409 2172483 dists[w*16+g] = dist - bits;
410 2172483 qenergies[w*16+g] = qenergy;
411
2/2
✓ Branch 0 taken 2119786 times.
✓ Branch 1 taken 52697 times.
2172483 if (prev != -1) {
412 2119786 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
413 2119786 bits += ff_aac_scalefactor_bits[sfdiff];
414 }
415 2172483 tbits += bits;
416 2172483 start += sce->ics.swb_sizes[g];
417 2172483 prev = sce->sf_idx[w*16+g];
418 }
419 }
420
2/2
✓ Branch 0 taken 30117 times.
✓ Branch 1 taken 22580 times.
52697 if (tbits > toomanybits) {
421 30117 recomprd = 1;
422
2/2
✓ Branch 0 taken 3854976 times.
✓ Branch 1 taken 30117 times.
3885093 for (i = 0; i < 128; i++) {
423
2/2
✓ Branch 0 taken 3252463 times.
✓ Branch 1 taken 602513 times.
3854976 if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) {
424
1/2
✓ Branch 0 taken 3252463 times.
✗ Branch 1 not taken.
3252463 int maxsf_i = (tbits > 5800) ? SCALE_MAX_POS : maxsf[i];
425 3252463 int new_sf = FFMIN(maxsf_i, sce->sf_idx[i] + qstep);
426
2/2
✓ Branch 0 taken 3193772 times.
✓ Branch 1 taken 58691 times.
3252463 if (new_sf != sce->sf_idx[i]) {
427 3193772 sce->sf_idx[i] = new_sf;
428 3193772 changed = 1;
429 }
430 }
431 }
432
2/2
✓ Branch 0 taken 8811 times.
✓ Branch 1 taken 13769 times.
22580 } else if (tbits < toofewbits) {
433 8811 recomprd = 1;
434
2/2
✓ Branch 0 taken 1127808 times.
✓ Branch 1 taken 8811 times.
1136619 for (i = 0; i < 128; i++) {
435
2/2
✓ Branch 0 taken 198474 times.
✓ Branch 1 taken 929334 times.
1127808 if (sce->sf_idx[i] > SCALE_ONE_POS) {
436 198474 int new_sf = FFMAX3(minsf[i], SCALE_ONE_POS, sce->sf_idx[i] - qstep);
437
1/2
✓ Branch 0 taken 198474 times.
✗ Branch 1 not taken.
198474 if (new_sf != sce->sf_idx[i]) {
438 198474 sce->sf_idx[i] = new_sf;
439 198474 changed = 1;
440 }
441 }
442 }
443 }
444 52697 qstep >>= 1;
445
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)
446 29890 qstep = 1;
447
2/2
✓ Branch 0 taken 33585 times.
✓ Branch 1 taken 19112 times.
52697 } while (qstep);
448
449 19112 overdist = 1;
450 19112 fflag = tbits < toofewbits;
451
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) {
452
2/2
✓ Branch 0 taken 11765 times.
✓ Branch 1 taken 21319 times.
33084 if (recomprd) {
453 /** Must recompute distortion */
454 11765 prev = -1;
455 11765 tbits = 0;
456
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]) {
457 11844 start = w*128;
458
2/2
✓ Branch 0 taken 576576 times.
✓ Branch 1 taken 11844 times.
588420 for (g = 0; g < sce->ics.num_swb; g++) {
459 576576 const float *coefs = sce->coeffs + start;
460 576576 const float *scaled = s->scoefs + start;
461 576576 int bits = 0;
462 int cb;
463 576576 float dist = 0.0f;
464 576576 float qenergy = 0.0f;
465
466
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) {
467 77681 start += sce->ics.swb_sizes[g];
468
2/2
✓ Branch 0 taken 6371 times.
✓ Branch 1 taken 71310 times.
77681 if (sce->can_pns[w*16+g]) {
469 /** PNS isn't free */
470 6371 tbits += ff_pns_bits(sce, w, g);
471 }
472 77681 continue;
473 }
474 498895 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
475
2/2
✓ Branch 0 taken 500301 times.
✓ Branch 1 taken 498895 times.
999196 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
476 int b;
477 float sqenergy;
478 1000602 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
479 500301 scaled + w2*128,
480 500301 sce->ics.swb_sizes[g],
481 500301 sce->sf_idx[w*16+g],
482 cb,
483 1.0f,
484 INFINITY,
485 &b, &sqenergy,
486 0);
487 500301 bits += b;
488 500301 qenergy += sqenergy;
489 }
490 498895 dists[w*16+g] = dist - bits;
491 498895 qenergies[w*16+g] = qenergy;
492
2/2
✓ Branch 0 taken 487130 times.
✓ Branch 1 taken 11765 times.
498895 if (prev != -1) {
493 487130 int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF);
494 487130 bits += ff_aac_scalefactor_bits[sfdiff];
495 }
496 498895 tbits += bits;
497 498895 start += sce->ics.swb_sizes[g];
498 498895 prev = sce->sf_idx[w*16+g];
499 }
500 }
501 }
502
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) {
503 6325 float maxoverdist = 0.0f;
504 6325 float ovrfactor = 1.f+(maxits-its)*16.f/maxits;
505 6325 overdist = recomprd = 0;
506
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]) {
507
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++]) {
508
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) {
509
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]);
510
2/2
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 1671 times.
3723 maxoverdist = FFMAX(maxoverdist, ovrdist);
511 3723 overdist++;
512 }
513 }
514 }
515
2/2
✓ Branch 0 taken 1185 times.
✓ Branch 1 taken 5140 times.
6325 if (overdist) {
516 /* We have overdistorted bands, trade for zeroes (that can be noise)
517 * Zero the bands in the lowest 1.25% spread-energy-threshold ranking
518 */
519 1185 float minspread = max_spread_thr_r;
520 1185 float maxspread = min_spread_thr_r;
521 float zspread;
522 1185 int zeroable = 0;
523 1185 int zeroed = 0;
524 int maxzeroed, zloop;
525
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]) {
526
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++]) {
527
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]) {
528
2/2
✓ Branch 0 taken 5931 times.
✓ Branch 1 taken 12305 times.
18236 minspread = FFMIN(minspread, spread_thr_r[w*16+g]);
529
2/2
✓ Branch 0 taken 15181 times.
✓ Branch 1 taken 3055 times.
18236 maxspread = FFMAX(maxspread, spread_thr_r[w*16+g]);
530 18236 zeroable++;
531 }
532 }
533 }
534 1185 zspread = (maxspread-minspread) * 0.0125f + minspread;
535 /* Don't PNS everything even if allowed. It suppresses bit starvation signals from RC,
536 * and forced the hand of the later search_for_pns step.
537 * Instead, PNS a fraction of the spread_thr_r range depending on how starved for bits we are,
538 * and leave further PNSing to search_for_pns if worthwhile.
539 */
540
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,
541 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
542 1185 maxzeroed = FFMIN(zeroable, FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
543
2/2
✓ Branch 0 taken 2370 times.
✓ Branch 1 taken 1185 times.
3555 for (zloop = 0; zloop < 2; zloop++) {
544 /* Two passes: first distorted stuff - two birds in one shot and all that,
545 * then anything viable. Viable means not zero, but either CB=zero-able
546 * (too high SF), not SF <= 1 (that means we'd be operating at very high
547 * quality, we don't want PNS when doing VHQ), PNS allowed, and within
548 * the lowest ranking percentile.
549 */
550
2/2
✓ Branch 0 taken 1185 times.
✓ Branch 1 taken 1185 times.
2370 float loopovrfactor = (zloop) ? 1.0f : ovrfactor;
551
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;
552 int mcb;
553
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--) {
554
2/2
✓ Branch 0 taken 47574 times.
✓ Branch 1 taken 53421 times.
100995 if (sce->ics.swb_offset[g] < pns_start_pos)
555 47574 continue;
556
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]) {
557
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
558
2/2
✓ Branch 0 taken 2294 times.
✓ Branch 1 taken 2012 times.
4306 && sce->sf_idx[w*16+g] > loopminsf
559
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]))
560
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]))) ) {
561 2169 sce->zeroes[w*16+g] = 1;
562 2169 sce->band_type[w*16+g] = 0;
563 2169 zeroed++;
564 }
565 }
566 }
567 }
568
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 165 times.
1185 if (zeroed)
569 1020 recomprd = fflag = 1;
570 } else {
571 5140 overdist = 0;
572 }
573 }
574 }
575
576 19112 minscaler = SCALE_MAX_POS;
577 19112 maxscaler = 0;
578
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]) {
579
2/2
✓ Branch 0 taken 938168 times.
✓ Branch 1 taken 20132 times.
958300 for (g = 0; g < sce->ics.num_swb; g++) {
580
2/2
✓ Branch 0 taken 775629 times.
✓ Branch 1 taken 162539 times.
938168 if (!sce->zeroes[w*16+g]) {
581 775629 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
582 775629 maxscaler = FFMAX(maxscaler, sce->sf_idx[w*16+g]);
583 }
584 }
585 }
586
587 19112 minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
588 19112 prev = -1;
589
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]) {
590 /** Start with big steps, end up fine-tunning */
591
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;
592 20132 int edepth = depth+2;
593 20132 float uplmax = its / (maxits*0.25f) + 1.0f;
594
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;
595 20132 start = w * 128;
596
2/2
✓ Branch 0 taken 938168 times.
✓ Branch 1 taken 20132 times.
958300 for (g = 0; g < sce->ics.num_swb; g++) {
597 938168 int prevsc = sce->sf_idx[w*16+g];
598
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])
599 19112 prev = sce->sf_idx[0];
600
2/2
✓ Branch 0 taken 775629 times.
✓ Branch 1 taken 162539 times.
938168 if (!sce->zeroes[w*16+g]) {
601 775629 const float *coefs = sce->coeffs + start;
602 775629 const float *scaled = s->scoefs + start;
603 775629 int cmb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
604 775629 int mindeltasf = FFMAX(0, prev - SCALE_MAX_DIFF);
605 775629 int maxdeltasf = FFMIN(SCALE_MAX_POS - SCALE_DIV_512, prev + SCALE_MAX_DIFF);
606
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])) {
607 /* Try to make sure there is some energy in every nonzero band
608 * NOTE: This algorithm must be forcibly imbalanced, pushing harder
609 * on holes or more distorted bands at first, otherwise there's
610 * no net gain (since the next iteration will offset all bands
611 * on the opposite direction to compensate for extra bits)
612 */
613
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) {
614 int cb, bits;
615 float dist, qenergy;
616 1817378 int mb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1);
617 1817378 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
618 1817378 dist = qenergy = 0.f;
619 1817378 bits = 0;
620
2/2
✓ Branch 0 taken 334688 times.
✓ Branch 1 taken 1482690 times.
1817378 if (!cb) {
621 334688 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g]-1, maxsf[w*16+g]);
622
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]) {
623 89331 break;
624 }
625 /* !g is the DC band, it's important, since quantization error here
626 * applies to less than a cycle, it creates horrible intermodulation
627 * distortion if it doesn't stick to what psy requests
628 */
629
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])
630 1376 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
631
2/2
✓ Branch 0 taken 1840799 times.
✓ Branch 1 taken 1728047 times.
3568846 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
632 int b;
633 float sqenergy;
634 3681598 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
635 1840799 scaled + w2*128,
636 1840799 sce->ics.swb_sizes[g],
637 1840799 sce->sf_idx[w*16+g]-1,
638 cb,
639 1.0f,
640 INFINITY,
641 &b, &sqenergy,
642 0);
643 1840799 bits += b;
644 1840799 qenergy += sqenergy;
645 }
646 1728047 sce->sf_idx[w*16+g]--;
647 1728047 dists[w*16+g] = dist - bits;
648 1728047 qenergies[w*16+g] = qenergy;
649
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 || (
650
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]))
651
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])
652 ) )) {
653 break;
654 }
655 }
656
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])
657
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]))
658
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])
659 ) {
660 /** Um... over target. Save bits for more important stuff. */
661
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) {
662 int cb, bits;
663 float dist, qenergy;
664 33781 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]+1);
665
2/2
✓ Branch 0 taken 33768 times.
✓ Branch 1 taken 13 times.
33781 if (cb > 0) {
666 33768 dist = qenergy = 0.f;
667 33768 bits = 0;
668
2/2
✓ Branch 0 taken 33812 times.
✓ Branch 1 taken 33768 times.
67580 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
669 int b;
670 float sqenergy;
671 67624 dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128,
672 33812 scaled + w2*128,
673 33812 sce->ics.swb_sizes[g],
674 33812 sce->sf_idx[w*16+g]+1,
675 cb,
676 1.0f,
677 INFINITY,
678 &b, &sqenergy,
679 0);
680 33812 bits += b;
681 33812 qenergy += sqenergy;
682 }
683 33768 dist -= bits;
684
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])) {
685 13638 sce->sf_idx[w*16+g]++;
686 13638 dists[w*16+g] = dist;
687 13638 qenergies[w*16+g] = qenergy;
688 } else {
689 20130 break;
690 }
691 } else {
692 13 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
693 13 break;
694 }
695 }
696 }
697 775629 prev = sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], mindeltasf, maxdeltasf);
698
2/2
✓ Branch 0 taken 482112 times.
✓ Branch 1 taken 293517 times.
775629 if (sce->sf_idx[w*16+g] != prevsc)
699 482112 fflag = 1;
700 775629 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]);
701 775629 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
702 }
703 938168 start += sce->ics.swb_sizes[g];
704 }
705 }
706
707 /** SF difference limit violation risk. Must re-clamp. */
708 19112 prev = -1;
709
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]) {
710
2/2
✓ Branch 0 taken 938168 times.
✓ Branch 1 taken 20132 times.
958300 for (g = 0; g < sce->ics.num_swb; g++) {
711
2/2
✓ Branch 0 taken 775629 times.
✓ Branch 1 taken 162539 times.
938168 if (!sce->zeroes[w*16+g]) {
712 775629 int prevsf = sce->sf_idx[w*16+g];
713
2/2
✓ Branch 0 taken 19112 times.
✓ Branch 1 taken 756517 times.
775629 if (prev < 0)
714 19112 prev = prevsf;
715 775629 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], prev - SCALE_MAX_DIFF, prev + SCALE_MAX_DIFF);
716 775629 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
717 775629 prev = sce->sf_idx[w*16+g];
718
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])
719 fflag = 1;
720 }
721 }
722 }
723
724 19112 its++;
725
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);
726
727 /** Scout out next nonzero bands */
728 739 ff_init_nextband_map(sce, nextband);
729
730 739 prev = -1;
731
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]) {
732 /** Make sure proper codebooks are set */
733
2/2
✓ Branch 0 taken 36267 times.
✓ Branch 1 taken 773 times.
37040 for (g = 0; g < sce->ics.num_swb; g++) {
734
2/2
✓ Branch 0 taken 28050 times.
✓ Branch 1 taken 8217 times.
36267 if (!sce->zeroes[w*16+g]) {
735 28050 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
736
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 27982 times.
28050 if (sce->band_type[w*16+g] <= 0) {
737
1/2
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
68 if (!ff_sfdelta_can_remove_band(sce, nextband, prev, w*16+g)) {
738 /** Cannot zero out, make sure it's not attempted */
739 68 sce->band_type[w*16+g] = 1;
740 } else {
741 sce->zeroes[w*16+g] = 1;
742 sce->band_type[w*16+g] = 0;
743 }
744 }
745 } else {
746 8217 sce->band_type[w*16+g] = 0;
747 }
748 /** Check that there's no SF delta range violations */
749
2/2
✓ Branch 0 taken 28050 times.
✓ Branch 1 taken 8217 times.
36267 if (!sce->zeroes[w*16+g]) {
750
2/2
✓ Branch 0 taken 27311 times.
✓ Branch 1 taken 739 times.
28050 if (prev != -1) {
751 27311 av_unused int sfdiff = sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO;
752 av_assert1(sfdiff >= 0 && sfdiff <= 2*SCALE_MAX_DIFF);
753
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 545 times.
739 } else if (sce->zeroes[0]) {
754 /** Set global gain to something useful */
755 194 sce->sf_idx[0] = sce->sf_idx[w*16+g];
756 }
757 28050 prev = sce->sf_idx[w*16+g];
758 }
759 }
760 }
761 }
762
763 #endif /* AVCODEC_AACCODER_TWOLOOP_H */
764