| 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 | 34048 | static inline int ff_pns_bits(SingleChannelElement *sce, int w, int g) | |
| 58 | { | ||
| 59 |
4/6✓ Branch 0 taken 34048 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20305 times.
✓ Branch 3 taken 13743 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 20305 times.
|
34048 | 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 | 788 | static void search_for_quantizers_twoloop(AVCodecContext *avctx, | |
| 66 | AACEncContext *s, | ||
| 67 | SingleChannelElement *sce, | ||
| 68 | const float lambda) | ||
| 69 | { | ||
| 70 | 788 | int start = 0, i, w, w2, g, recomprd; | |
| 71 | 1576 | int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate | |
| 72 |
1/2✓ Branch 0 taken 788 times.
✗ Branch 1 not taken.
|
788 | / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels) |
| 73 | 788 | * (lambda / 120.f); | |
| 74 | 788 | int refbits = destbits; | |
| 75 | int toomanybits, toofewbits; | ||
| 76 | char nzs[128]; | ||
| 77 | uint8_t nextband[128]; | ||
| 78 | int maxsf[128], minsf[128]; | ||
| 79 | 788 | 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 | 788 | float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f); | |
| 93 | 788 | const float nzslope = 1.5f; | |
| 94 | 788 | float rdmin = 0.03125f; | |
| 95 | 788 | 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 | 788 | float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10); | |
| 103 | |||
| 104 | int fflag, minscaler, nminscaler; | ||
| 105 | 788 | int its = 0; | |
| 106 | 788 | int maxits = 30; | |
| 107 | 788 | int allz = 0; | |
| 108 | int tbits; | ||
| 109 | 788 | 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 644 times.
✓ Branch 1 taken 144 times.
|
788 | if (lambda > 120.f) { |
| 123 | 644 | zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f); | |
| 124 | } else { | ||
| 125 | 144 | zeroscale = 1.f; | |
| 126 | } | ||
| 127 | |||
| 128 |
1/2✓ Branch 0 taken 788 times.
✗ Branch 1 not taken.
|
788 | 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 | 1576 | destbits = s->psy.bitres.alloc | |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 788 times.
|
788 | * (lambda / (avctx->global_quality ? avctx->global_quality : 120)); |
| 135 | } | ||
| 136 | |||
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 788 times.
|
788 | 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 | 788 | toomanybits = destbits + destbits/8; | |
| 166 | 788 | toofewbits = destbits - destbits/8; | |
| 167 | |||
| 168 | 788 | sfoffs = 0; | |
| 169 | 788 | rdlambda = sqrtf(rdlambda); | |
| 170 | } | ||
| 171 | |||
| 172 | /** and zero out above cutoff frequency */ | ||
| 173 | { | ||
| 174 | 788 | 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 | 788 | float rate_bandwidth_multiplier = 1.5f; | |
| 185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 788 times.
|
788 | int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE) |
| 186 | ✗ | ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024) | |
| 187 | 788 | : (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 788 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
788 | if (s->options.pns || s->options.intensity_stereo) |
| 191 | 788 | frame_bit_rate *= 1.15f; | |
| 192 | |||
| 193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 788 times.
|
788 | if (avctx->cutoff > 0) { |
| 194 | ✗ | bandwidth = avctx->cutoff; | |
| 195 | } else { | ||
| 196 |
5/10✓ Branch 0 taken 788 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 788 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 788 times.
✓ Branch 6 taken 788 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 788 times.
|
788 | bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate)); |
| 197 | 788 | s->psy.cutoff = bandwidth; | |
| 198 | } | ||
| 199 | |||
| 200 | 788 | cutoff = bandwidth * 2 * wlen / avctx->sample_rate; | |
| 201 | 788 | 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 | 788 | destbits = FFMIN(destbits, 5800); | |
| 209 | 788 | toomanybits = FFMIN(toomanybits, 5800); | |
| 210 | 788 | 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 | 788 | min_spread_thr_r = -1; | |
| 216 | 788 | max_spread_thr_r = -1; | |
| 217 |
2/2✓ Branch 0 taken 843 times.
✓ Branch 1 taken 788 times.
|
1631 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 218 |
2/2✓ Branch 0 taken 38647 times.
✓ Branch 1 taken 843 times.
|
39490 | for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) { |
| 219 | 38647 | int nz = 0; | |
| 220 | 38647 | float uplim = 0.0f, energy = 0.0f, spread = 0.0f; | |
| 221 |
2/2✓ Branch 0 taken 39935 times.
✓ Branch 1 taken 38647 times.
|
78582 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
| 222 | 39935 | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
| 223 |
5/6✓ Branch 0 taken 35852 times.
✓ Branch 1 taken 4083 times.
✓ Branch 2 taken 32816 times.
✓ Branch 3 taken 3036 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 32816 times.
|
39935 | if (start >= cutoff || band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f) { |
| 224 | 7119 | sce->zeroes[(w+w2)*16+g] = 1; | |
| 225 | 7119 | continue; | |
| 226 | } | ||
| 227 | 32816 | nz = 1; | |
| 228 | } | ||
| 229 |
2/2✓ Branch 0 taken 6491 times.
✓ Branch 1 taken 32156 times.
|
38647 | if (!nz) { |
| 230 | 6491 | uplim = 0.0f; | |
| 231 | } else { | ||
| 232 | 32156 | nz = 0; | |
| 233 |
2/2✓ Branch 0 taken 32896 times.
✓ Branch 1 taken 32156 times.
|
65052 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
| 234 | 32896 | FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g]; | |
| 235 |
3/4✓ Branch 0 taken 32816 times.
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32816 times.
|
32896 | if (band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f) |
| 236 | 80 | continue; | |
| 237 | 32816 | uplim += band->threshold; | |
| 238 | 32816 | energy += band->energy; | |
| 239 | 32816 | spread += band->spread; | |
| 240 | 32816 | nz++; | |
| 241 | } | ||
| 242 | } | ||
| 243 | 38647 | uplims[w*16+g] = uplim; | |
| 244 | 38647 | energies[w*16+g] = energy; | |
| 245 | 38647 | nzs[w*16+g] = nz; | |
| 246 | 38647 | sce->zeroes[w*16+g] = !nz; | |
| 247 | 38647 | allz |= nz; | |
| 248 |
4/4✓ Branch 0 taken 32156 times.
✓ Branch 1 taken 6491 times.
✓ Branch 2 taken 15247 times.
✓ Branch 3 taken 16909 times.
|
38647 | if (nz && sce->can_pns[w*16+g]) { |
| 249 | 15247 | spread_thr_r[w*16+g] = energy * nz / (uplim * spread); | |
| 250 |
2/2✓ Branch 0 taken 780 times.
✓ Branch 1 taken 14467 times.
|
15247 | if (min_spread_thr_r < 0) { |
| 251 | 780 | min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+g]; | |
| 252 | } else { | ||
| 253 |
2/2✓ Branch 0 taken 3443 times.
✓ Branch 1 taken 11024 times.
|
14467 | min_spread_thr_r = FFMIN(min_spread_thr_r, spread_thr_r[w*16+g]); |
| 254 |
2/2✓ Branch 0 taken 12334 times.
✓ Branch 1 taken 2133 times.
|
14467 | 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 | 788 | minscaler = 65535; | |
| 262 |
2/2✓ Branch 0 taken 843 times.
✓ Branch 1 taken 788 times.
|
1631 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 263 |
2/2✓ Branch 0 taken 38647 times.
✓ Branch 1 taken 843 times.
|
39490 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 264 |
2/2✓ Branch 0 taken 6491 times.
✓ Branch 1 taken 32156 times.
|
38647 | if (sce->zeroes[w*16+g]) { |
| 265 | 6491 | sce->sf_idx[w*16+g] = SCALE_ONE_POS; | |
| 266 | 6491 | 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 | 32156 | sce->sf_idx[w*16+g] = av_clip( | |
| 275 | SCALE_ONE_POS | ||
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32156 times.
|
32156 | + 1.75*log2f(FFMAX(0.00125f,uplims[w*16+g]) / sce->ics.swb_sizes[g]) |
| 277 | 32156 | + sfoffs, | |
| 278 | 60, SCALE_MAX_POS); | ||
| 279 | 32156 | minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); | |
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | /** Clip */ | ||
| 284 | 788 | minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512); | |
| 285 |
2/2✓ Branch 0 taken 843 times.
✓ Branch 1 taken 788 times.
|
1631 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) |
| 286 |
2/2✓ Branch 0 taken 38647 times.
✓ Branch 1 taken 843 times.
|
39490 | for (g = 0; g < sce->ics.num_swb; g++) |
| 287 |
2/2✓ Branch 0 taken 32156 times.
✓ Branch 1 taken 6491 times.
|
38647 | if (!sce->zeroes[w*16+g]) |
| 288 | 32156 | sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1); | |
| 289 | |||
| 290 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 782 times.
|
788 | if (!allz) |
| 291 | 6 | return; | |
| 292 | 782 | s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024); | |
| 293 | 782 | ff_quantize_band_cost_cache_init(s); | |
| 294 | |||
| 295 |
2/2✓ Branch 0 taken 100096 times.
✓ Branch 1 taken 782 times.
|
100878 | for (i = 0; i < sizeof(minsf) / sizeof(minsf[0]); ++i) |
| 296 | 100096 | minsf[i] = 0; | |
| 297 |
2/2✓ Branch 0 taken 825 times.
✓ Branch 1 taken 782 times.
|
1607 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 298 | 825 | start = w*128; | |
| 299 |
2/2✓ Branch 0 taken 38395 times.
✓ Branch 1 taken 825 times.
|
39220 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 300 | 38395 | const float *scaled = s->scoefs + start; | |
| 301 | int minsfidx; | ||
| 302 | 38395 | maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled); | |
| 303 |
2/2✓ Branch 0 taken 38367 times.
✓ Branch 1 taken 28 times.
|
38395 | if (maxvals[w*16+g] > 0) { |
| 304 | 38367 | minsfidx = coef2minsf(maxvals[w*16+g]); | |
| 305 |
2/2✓ Branch 0 taken 39207 times.
✓ Branch 1 taken 38367 times.
|
77574 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) |
| 306 | 39207 | minsf[(w+w2)*16+g] = minsfidx; | |
| 307 | } | ||
| 308 | 38395 | 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 prioritization. | ||
| 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 | 782 | memcpy(euplims, uplims, sizeof(euplims)); | |
| 320 |
2/2✓ Branch 0 taken 825 times.
✓ Branch 1 taken 782 times.
|
1607 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 321 | /** psy already prioritizes transients to some extent */ | ||
| 322 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 767 times.
|
825 | float de_psy_factor = (sce->ics.num_windows > 1) ? 8.0f / sce->ics.group_len[w] : 1.0f; |
| 323 | 825 | start = w*128; | |
| 324 |
2/2✓ Branch 0 taken 38395 times.
✓ Branch 1 taken 825 times.
|
39220 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 325 |
2/2✓ Branch 0 taken 32180 times.
✓ Branch 1 taken 6215 times.
|
38395 | if (nzs[g] > 0) { |
| 326 | 32180 | float cleanup_factor = ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f)); | |
| 327 | 32180 | float energy2uplim = find_form_factor( | |
| 328 | 32180 | sce->ics.group_len[w], sce->ics.swb_sizes[g], | |
| 329 | 32180 | uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]), | |
| 330 | 32180 | sce->coeffs + start, | |
| 331 | nzslope * cleanup_factor); | ||
| 332 | 32180 | energy2uplim *= de_psy_factor; | |
| 333 |
1/2✓ Branch 0 taken 32180 times.
✗ Branch 1 not taken.
|
32180 | if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) { |
| 334 | /** In ABR, we need to prioritize less and let rate control do its thing */ | ||
| 335 | 32180 | energy2uplim = sqrtf(energy2uplim); | |
| 336 | } | ||
| 337 |
5/6✓ Branch 0 taken 31936 times.
✓ Branch 1 taken 244 times.
✓ Branch 2 taken 31936 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31936 times.
✓ Branch 5 taken 244 times.
|
32180 | energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim)); |
| 338 | 32180 | uplims[w*16+g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax) | |
| 339 | 32180 | * sce->ics.group_len[w]; | |
| 340 | |||
| 341 | 32180 | energy2uplim = find_form_factor( | |
| 342 | 32180 | sce->ics.group_len[w], sce->ics.swb_sizes[g], | |
| 343 | 32180 | uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]), | |
| 344 | 32180 | sce->coeffs + start, | |
| 345 | 2.0f); | ||
| 346 | 32180 | energy2uplim *= de_psy_factor; | |
| 347 |
1/2✓ Branch 0 taken 32180 times.
✗ Branch 1 not taken.
|
32180 | if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) { |
| 348 | /** In ABR, we need to prioritize less and let rate control do its thing */ | ||
| 349 | 32180 | energy2uplim = sqrtf(energy2uplim); | |
| 350 | } | ||
| 351 |
5/6✓ Branch 0 taken 31977 times.
✓ Branch 1 taken 203 times.
✓ Branch 2 taken 31977 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31977 times.
✓ Branch 5 taken 203 times.
|
32180 | energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim)); |
| 352 | 32180 | euplims[w*16+g] *= av_clipf(rdlambda * energy2uplim * sce->ics.group_len[w], | |
| 353 | 0.5f, 1.0f); | ||
| 354 | } | ||
| 355 | 38395 | start += sce->ics.swb_sizes[g]; | |
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 |
2/2✓ Branch 0 taken 100096 times.
✓ Branch 1 taken 782 times.
|
100878 | for (i = 0; i < sizeof(maxsf) / sizeof(maxsf[0]); ++i) |
| 360 | 100096 | 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 20778 times.
✓ Branch 1 taken 782 times.
|
21560 | int qstep = its ? 1 : 32; |
| 368 | do { | ||
| 369 | 64531 | int changed = 0; | |
| 370 | 64531 | prev = -1; | |
| 371 | 64531 | recomprd = 0; | |
| 372 | 64531 | tbits = 0; | |
| 373 |
2/2✓ Branch 0 taken 75755 times.
✓ Branch 1 taken 64531 times.
|
140286 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 374 | 75755 | start = w*128; | |
| 375 |
2/2✓ Branch 0 taken 3184195 times.
✓ Branch 1 taken 75755 times.
|
3259950 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 376 | 3184195 | const float *coefs = &sce->coeffs[start]; | |
| 377 | 3184195 | const float *scaled = &s->scoefs[start]; | |
| 378 | 3184195 | int bits = 0; | |
| 379 | int cb; | ||
| 380 | 3184195 | float dist = 0.0f; | |
| 381 | 3184195 | float qenergy = 0.0f; | |
| 382 | |||
| 383 |
3/4✓ Branch 0 taken 2633332 times.
✓ Branch 1 taken 550863 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2633332 times.
|
3184195 | if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) { |
| 384 | 550863 | start += sce->ics.swb_sizes[g]; | |
| 385 |
2/2✓ Branch 0 taken 26272 times.
✓ Branch 1 taken 524591 times.
|
550863 | if (sce->can_pns[w*16+g]) { |
| 386 | /** PNS isn't free */ | ||
| 387 | 26272 | tbits += ff_pns_bits(sce, w, g); | |
| 388 | } | ||
| 389 | 550863 | continue; | |
| 390 | } | ||
| 391 | 2633332 | cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
| 392 |
2/2✓ Branch 0 taken 2822668 times.
✓ Branch 1 taken 2633332 times.
|
5456000 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
| 393 | int b; | ||
| 394 | float sqenergy; | ||
| 395 | 5645336 | dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128, | |
| 396 | 2822668 | scaled + w2*128, | |
| 397 | 2822668 | sce->ics.swb_sizes[g], | |
| 398 | 2822668 | sce->sf_idx[w*16+g], | |
| 399 | cb, | ||
| 400 | 1.0f, | ||
| 401 | INFINITY, | ||
| 402 | &b, &sqenergy, | ||
| 403 | 0); | ||
| 404 | 2822668 | bits += b; | |
| 405 | 2822668 | qenergy += sqenergy; | |
| 406 | } | ||
| 407 | 2633332 | dists[w*16+g] = dist - bits; | |
| 408 | 2633332 | qenergies[w*16+g] = qenergy; | |
| 409 |
2/2✓ Branch 0 taken 2568801 times.
✓ Branch 1 taken 64531 times.
|
2633332 | if (prev != -1) { |
| 410 | 2568801 | int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF); | |
| 411 | 2568801 | bits += ff_aac_scalefactor_bits[sfdiff]; | |
| 412 | } | ||
| 413 | 2633332 | tbits += bits; | |
| 414 | 2633332 | start += sce->ics.swb_sizes[g]; | |
| 415 | 2633332 | prev = sce->sf_idx[w*16+g]; | |
| 416 | } | ||
| 417 | } | ||
| 418 |
2/2✓ Branch 0 taken 39378 times.
✓ Branch 1 taken 25153 times.
|
64531 | if (tbits > toomanybits) { |
| 419 | 39378 | recomprd = 1; | |
| 420 |
2/2✓ Branch 0 taken 5040384 times.
✓ Branch 1 taken 39378 times.
|
5079762 | for (i = 0; i < 128; i++) { |
| 421 |
2/2✓ Branch 0 taken 4126020 times.
✓ Branch 1 taken 914364 times.
|
5040384 | if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) { |
| 422 |
1/2✓ Branch 0 taken 4126020 times.
✗ Branch 1 not taken.
|
4126020 | int maxsf_i = (tbits > 5800) ? SCALE_MAX_POS : maxsf[i]; |
| 423 | 4126020 | int new_sf = FFMIN(maxsf_i, sce->sf_idx[i] + qstep); | |
| 424 |
2/2✓ Branch 0 taken 4051474 times.
✓ Branch 1 taken 74546 times.
|
4126020 | if (new_sf != sce->sf_idx[i]) { |
| 425 | 4051474 | sce->sf_idx[i] = new_sf; | |
| 426 | 4051474 | changed = 1; | |
| 427 | } | ||
| 428 | } | ||
| 429 | } | ||
| 430 |
2/2✓ Branch 0 taken 8883 times.
✓ Branch 1 taken 16270 times.
|
25153 | } else if (tbits < toofewbits) { |
| 431 | 8883 | recomprd = 1; | |
| 432 |
2/2✓ Branch 0 taken 1137024 times.
✓ Branch 1 taken 8883 times.
|
1145907 | for (i = 0; i < 128; i++) { |
| 433 |
2/2✓ Branch 0 taken 234245 times.
✓ Branch 1 taken 902779 times.
|
1137024 | if (sce->sf_idx[i] > SCALE_ONE_POS) { |
| 434 | 234245 | int new_sf = FFMAX3(minsf[i], SCALE_ONE_POS, sce->sf_idx[i] - qstep); | |
| 435 |
1/2✓ Branch 0 taken 234245 times.
✗ Branch 1 not taken.
|
234245 | if (new_sf != sce->sf_idx[i]) { |
| 436 | 234245 | sce->sf_idx[i] = new_sf; | |
| 437 | 234245 | changed = 1; | |
| 438 | } | ||
| 439 | } | ||
| 440 | } | ||
| 441 | } | ||
| 442 | 64531 | qstep >>= 1; | |
| 443 |
7/8✓ Branch 0 taken 60621 times.
✓ Branch 1 taken 3910 times.
✓ Branch 2 taken 39087 times.
✓ Branch 3 taken 21534 times.
✓ Branch 4 taken 39061 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 39061 times.
✗ Branch 7 not taken.
|
64531 | if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217 && changed) |
| 444 | 39061 | qstep = 1; | |
| 445 |
2/2✓ Branch 0 taken 42971 times.
✓ Branch 1 taken 21560 times.
|
64531 | } while (qstep); |
| 446 | |||
| 447 | 21560 | overdist = 1; | |
| 448 | 21560 | fflag = tbits < toofewbits; | |
| 449 |
5/6✓ Branch 0 taken 43120 times.
✓ Branch 1 taken 15280 times.
✓ Branch 2 taken 36840 times.
✓ Branch 3 taken 6280 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6280 times.
|
58400 | for (i = 0; i < 2 && (overdist || recomprd); ++i) { |
| 450 |
2/2✓ Branch 0 taken 11967 times.
✓ Branch 1 taken 24873 times.
|
36840 | if (recomprd) { |
| 451 | /** Must recompute distortion */ | ||
| 452 | 11967 | prev = -1; | |
| 453 | 11967 | tbits = 0; | |
| 454 |
2/2✓ Branch 0 taken 12070 times.
✓ Branch 1 taken 11967 times.
|
24037 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 455 | 12070 | start = w*128; | |
| 456 |
2/2✓ Branch 0 taken 586530 times.
✓ Branch 1 taken 12070 times.
|
598600 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 457 | 586530 | const float *coefs = sce->coeffs + start; | |
| 458 | 586530 | const float *scaled = s->scoefs + start; | |
| 459 | 586530 | int bits = 0; | |
| 460 | int cb; | ||
| 461 | 586530 | float dist = 0.0f; | |
| 462 | 586530 | float qenergy = 0.0f; | |
| 463 | |||
| 464 |
3/4✓ Branch 0 taken 506959 times.
✓ Branch 1 taken 79571 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 506959 times.
|
586530 | if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) { |
| 465 | 79571 | start += sce->ics.swb_sizes[g]; | |
| 466 |
2/2✓ Branch 0 taken 7776 times.
✓ Branch 1 taken 71795 times.
|
79571 | if (sce->can_pns[w*16+g]) { |
| 467 | /** PNS isn't free */ | ||
| 468 | 7776 | tbits += ff_pns_bits(sce, w, g); | |
| 469 | } | ||
| 470 | 79571 | continue; | |
| 471 | } | ||
| 472 | 506959 | cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
| 473 |
2/2✓ Branch 0 taken 508761 times.
✓ Branch 1 taken 506959 times.
|
1015720 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
| 474 | int b; | ||
| 475 | float sqenergy; | ||
| 476 | 1017522 | dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128, | |
| 477 | 508761 | scaled + w2*128, | |
| 478 | 508761 | sce->ics.swb_sizes[g], | |
| 479 | 508761 | sce->sf_idx[w*16+g], | |
| 480 | cb, | ||
| 481 | 1.0f, | ||
| 482 | INFINITY, | ||
| 483 | &b, &sqenergy, | ||
| 484 | 0); | ||
| 485 | 508761 | bits += b; | |
| 486 | 508761 | qenergy += sqenergy; | |
| 487 | } | ||
| 488 | 506959 | dists[w*16+g] = dist - bits; | |
| 489 | 506959 | qenergies[w*16+g] = qenergy; | |
| 490 |
2/2✓ Branch 0 taken 494992 times.
✓ Branch 1 taken 11967 times.
|
506959 | if (prev != -1) { |
| 491 | 494992 | int sfdiff = av_clip(sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF); | |
| 492 | 494992 | bits += ff_aac_scalefactor_bits[sfdiff]; | |
| 493 | } | ||
| 494 | 506959 | tbits += bits; | |
| 495 | 506959 | start += sce->ics.swb_sizes[g]; | |
| 496 | 506959 | prev = sce->sf_idx[w*16+g]; | |
| 497 | } | ||
| 498 | } | ||
| 499 | } | ||
| 500 |
7/8✓ Branch 0 taken 21560 times.
✓ Branch 1 taken 15280 times.
✓ Branch 2 taken 21560 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9805 times.
✓ Branch 5 taken 11755 times.
✓ Branch 6 taken 7799 times.
✓ Branch 7 taken 2006 times.
|
36840 | if (!i && s->options.pns && its > maxits/2 && tbits > toofewbits) { |
| 501 | 7799 | float maxoverdist = 0.0f; | |
| 502 | 7799 | float ovrfactor = 1.f+(maxits-its)*16.f/maxits; | |
| 503 | 7799 | overdist = recomprd = 0; | |
| 504 |
2/2✓ Branch 0 taken 8401 times.
✓ Branch 1 taken 7799 times.
|
16200 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 505 |
2/2✓ Branch 0 taken 383229 times.
✓ Branch 1 taken 8401 times.
|
391630 | for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) { |
| 506 |
6/6✓ Branch 0 taken 301444 times.
✓ Branch 1 taken 81785 times.
✓ Branch 2 taken 72343 times.
✓ Branch 3 taken 229101 times.
✓ Branch 4 taken 5978 times.
✓ Branch 5 taken 66365 times.
|
383229 | 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 2493 times.
✓ Branch 1 taken 3485 times.
|
5978 | float ovrdist = dists[w*16+g] / FFMAX(uplims[w*16+g],euplims[w*16+g]); |
| 508 |
2/2✓ Branch 0 taken 3373 times.
✓ Branch 1 taken 2605 times.
|
5978 | maxoverdist = FFMAX(maxoverdist, ovrdist); |
| 509 | 5978 | overdist++; | |
| 510 | } | ||
| 511 | } | ||
| 512 | } | ||
| 513 |
2/2✓ Branch 0 taken 1519 times.
✓ Branch 1 taken 6280 times.
|
7799 | 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 | 1519 | float minspread = max_spread_thr_r; | |
| 518 | 1519 | float maxspread = min_spread_thr_r; | |
| 519 | float zspread; | ||
| 520 | 1519 | int zeroable = 0; | |
| 521 | 1519 | int zeroed = 0; | |
| 522 | int maxzeroed, zloop; | ||
| 523 |
2/2✓ Branch 0 taken 2065 times.
✓ Branch 1 taken 1519 times.
|
3584 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 524 |
2/2✓ Branch 0 taken 75705 times.
✓ Branch 1 taken 2065 times.
|
77770 | for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) { |
| 525 |
6/6✓ Branch 0 taken 39249 times.
✓ Branch 1 taken 36456 times.
✓ Branch 2 taken 25375 times.
✓ Branch 3 taken 13874 times.
✓ Branch 4 taken 23305 times.
✓ Branch 5 taken 2070 times.
|
75705 | if (start >= pns_start_pos && !sce->zeroes[w*16+g] && sce->can_pns[w*16+g]) { |
| 526 |
2/2✓ Branch 0 taken 7652 times.
✓ Branch 1 taken 15653 times.
|
23305 | minspread = FFMIN(minspread, spread_thr_r[w*16+g]); |
| 527 |
2/2✓ Branch 0 taken 19329 times.
✓ Branch 1 taken 3976 times.
|
23305 | maxspread = FFMAX(maxspread, spread_thr_r[w*16+g]); |
| 528 | 23305 | zeroable++; | |
| 529 | } | ||
| 530 | } | ||
| 531 | } | ||
| 532 | 1519 | 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 1304 times.
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1509 times.
✓ Branch 4 taken 1294 times.
✓ Branch 5 taken 215 times.
|
1519 | 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 | 1519 | maxzeroed = FFMIN(zeroable, FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits))); | |
| 541 |
2/2✓ Branch 0 taken 3038 times.
✓ Branch 1 taken 1519 times.
|
4557 | 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 1519 times.
✓ Branch 1 taken 1519 times.
|
3038 | float loopovrfactor = (zloop) ? 1.0f : ovrfactor; |
| 549 |
2/2✓ Branch 0 taken 1519 times.
✓ Branch 1 taken 1519 times.
|
3038 | int loopminsf = (zloop) ? (SCALE_ONE_POS - SCALE_DIV_512) : SCALE_ONE_POS; |
| 550 | int mcb; | ||
| 551 |
4/4✓ Branch 0 taken 130185 times.
✓ Branch 1 taken 2952 times.
✓ Branch 2 taken 130099 times.
✓ Branch 3 taken 86 times.
|
133137 | for (g = sce->ics.num_swb-1; g > 0 && zeroed < maxzeroed; g--) { |
| 552 |
2/2✓ Branch 0 taken 61416 times.
✓ Branch 1 taken 68683 times.
|
130099 | if (sce->ics.swb_offset[g] < pns_start_pos) |
| 553 | 61416 | continue; | |
| 554 |
2/2✓ Branch 0 taken 77377 times.
✓ Branch 1 taken 68683 times.
|
146060 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 555 |
6/6✓ Branch 0 taken 49620 times.
✓ Branch 1 taken 27757 times.
✓ Branch 2 taken 45501 times.
✓ Branch 3 taken 4119 times.
✓ Branch 4 taken 5548 times.
✓ Branch 5 taken 39953 times.
|
77377 | 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 3293 times.
✓ Branch 1 taken 2255 times.
|
5548 | && sce->sf_idx[w*16+g] > loopminsf |
| 557 |
3/4✓ Branch 0 taken 943 times.
✓ Branch 1 taken 2350 times.
✓ Branch 3 taken 943 times.
✗ Branch 4 not taken.
|
3293 | && (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 593 times.
✓ Branch 1 taken 350 times.
✓ Branch 2 taken 278 times.
✓ Branch 3 taken 315 times.
✓ Branch 4 taken 417 times.
✓ Branch 5 taken 176 times.
|
943 | || (mcb <= 1 && dists[w*16+g] > FFMIN(uplims[w*16+g], euplims[w*16+g]))) ) { |
| 559 | 2767 | sce->zeroes[w*16+g] = 1; | |
| 560 | 2767 | sce->band_type[w*16+g] = 0; | |
| 561 | 2767 | zeroed++; | |
| 562 | } | ||
| 563 | } | ||
| 564 | } | ||
| 565 | } | ||
| 566 |
2/2✓ Branch 0 taken 1314 times.
✓ Branch 1 taken 205 times.
|
1519 | if (zeroed) |
| 567 | 1314 | recomprd = fflag = 1; | |
| 568 | } else { | ||
| 569 | 6280 | overdist = 0; | |
| 570 | } | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | 21560 | minscaler = SCALE_MAX_POS; | |
| 575 |
2/2✓ Branch 0 taken 22850 times.
✓ Branch 1 taken 21560 times.
|
44410 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 576 |
2/2✓ Branch 0 taken 1058750 times.
✓ Branch 1 taken 22850 times.
|
1081600 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 577 |
2/2✓ Branch 0 taken 868073 times.
✓ Branch 1 taken 190677 times.
|
1058750 | if (!sce->zeroes[w*16+g]) { |
| 578 | 868073 | minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]); | |
| 579 | } | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | 21560 | minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512); | |
| 584 | 21560 | prev = -1; | |
| 585 |
2/2✓ Branch 0 taken 22850 times.
✓ Branch 1 taken 21560 times.
|
44410 | 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 10407 times.
✓ Branch 1 taken 12443 times.
✓ Branch 2 taken 6638 times.
✓ Branch 3 taken 3769 times.
|
22850 | int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10; |
| 588 | 22850 | int edepth = depth+2; | |
| 589 | 22850 | float uplmax = its / (maxits*0.25f) + 1.0f; | |
| 590 |
5/8✓ Branch 0 taken 15826 times.
✓ Branch 1 taken 7024 times.
✓ Branch 2 taken 15826 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15826 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15826 times.
✗ Branch 7 not taken.
|
22850 | uplmax *= (tbits > destbits) ? FFMIN(2.0f, tbits / (float)FFMAX(1,destbits)) : 1.0f; |
| 591 | 22850 | start = w * 128; | |
| 592 |
2/2✓ Branch 0 taken 1058750 times.
✓ Branch 1 taken 22850 times.
|
1081600 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 593 | 1058750 | int prevsc = sce->sf_idx[w*16+g]; | |
| 594 |
4/4✓ Branch 0 taken 82563 times.
✓ Branch 1 taken 976187 times.
✓ Branch 2 taken 21560 times.
✓ Branch 3 taken 61003 times.
|
1058750 | if (prev < 0 && !sce->zeroes[w*16+g]) |
| 595 | 21560 | prev = sce->sf_idx[0]; | |
| 596 |
2/2✓ Branch 0 taken 868073 times.
✓ Branch 1 taken 190677 times.
|
1058750 | if (!sce->zeroes[w*16+g]) { |
| 597 | 868073 | const float *coefs = sce->coeffs + start; | |
| 598 | 868073 | const float *scaled = s->scoefs + start; | |
| 599 | 868073 | int cmb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
| 600 | 868073 | int mindeltasf = FFMAX(0, prev - SCALE_MAX_DIFF); | |
| 601 | 868073 | int maxdeltasf = FFMIN(SCALE_MAX_POS - SCALE_DIV_512, prev + SCALE_MAX_DIFF); | |
| 602 |
6/6✓ Branch 0 taken 827090 times.
✓ Branch 1 taken 40983 times.
✓ Branch 2 taken 532365 times.
✓ Branch 3 taken 294725 times.
✓ Branch 4 taken 570695 times.
✓ Branch 5 taken 2653 times.
|
868073 | 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 2191486 times.
✓ Branch 1 taken 61918 times.
✓ Branch 2 taken 2191290 times.
✓ Branch 3 taken 196 times.
|
2253404 | for (i = 0; i < edepth && sce->sf_idx[w*16+g] > mindeltasf; ++i) { |
| 610 | int cb, bits; | ||
| 611 | float dist, qenergy; | ||
| 612 | 2191290 | int mb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1); | |
| 613 | 2191290 | cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
| 614 | 2191290 | dist = qenergy = 0.f; | |
| 615 | 2191290 | bits = 0; | |
| 616 |
2/2✓ Branch 0 taken 334478 times.
✓ Branch 1 taken 1856812 times.
|
2191290 | if (!cb) { |
| 617 | 334478 | maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g]-1, maxsf[w*16+g]); | |
| 618 |
4/4✓ Branch 0 taken 272298 times.
✓ Branch 1 taken 1584514 times.
✓ Branch 2 taken 120633 times.
✓ Branch 3 taken 151665 times.
|
1856812 | } else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) { |
| 619 | 120633 | 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 39785 times.
✓ Branch 1 taken 2030872 times.
✓ Branch 2 taken 3689 times.
✓ Branch 3 taken 36096 times.
✓ Branch 4 taken 1659 times.
✓ Branch 5 taken 2030 times.
|
2070657 | if (!g && sce->ics.num_windows > 1 && dists[w*16+g] >= euplims[w*16+g]) |
| 626 | 1659 | maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]); | |
| 627 |
2/2✓ Branch 0 taken 2218397 times.
✓ Branch 1 taken 2070657 times.
|
4289054 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
| 628 | int b; | ||
| 629 | float sqenergy; | ||
| 630 | 4436794 | dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128, | |
| 631 | 2218397 | scaled + w2*128, | |
| 632 | 2218397 | sce->ics.swb_sizes[g], | |
| 633 | 2218397 | sce->sf_idx[w*16+g]-1, | |
| 634 | cb, | ||
| 635 | 1.0f, | ||
| 636 | INFINITY, | ||
| 637 | &b, &sqenergy, | ||
| 638 | 0); | ||
| 639 | 2218397 | bits += b; | |
| 640 | 2218397 | qenergy += sqenergy; | |
| 641 | } | ||
| 642 | 2070657 | sce->sf_idx[w*16+g]--; | |
| 643 | 2070657 | dists[w*16+g] = dist - bits; | |
| 644 | 2070657 | qenergies[w*16+g] = qenergy; | |
| 645 |
5/6✓ Branch 0 taken 1758554 times.
✓ Branch 1 taken 312103 times.
✓ Branch 2 taken 1758554 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 872860 times.
✓ Branch 5 taken 885694 times.
|
3829211 | if (mb && (sce->sf_idx[w*16+g] < mindeltasf || ( |
| 646 |
2/2✓ Branch 0 taken 1457965 times.
✓ Branch 1 taken 300589 times.
|
1758554 | (dists[w*16+g] < FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g])) |
| 647 |
2/2✓ Branch 0 taken 484912 times.
✓ Branch 1 taken 387948 times.
|
872860 | && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g]) |
| 648 | ) )) { | ||
| 649 | break; | ||
| 650 | } | ||
| 651 | } | ||
| 652 |
4/4✓ Branch 0 taken 117949 times.
✓ Branch 1 taken 179429 times.
✓ Branch 2 taken 112342 times.
✓ Branch 3 taken 5607 times.
|
297378 | } else if (tbits > toofewbits && sce->sf_idx[w*16+g] < FFMIN(maxdeltasf, maxsf[w*16+g]) |
| 653 |
4/4✓ Branch 0 taken 64883 times.
✓ Branch 1 taken 47459 times.
✓ Branch 2 taken 100269 times.
✓ Branch 3 taken 12073 times.
|
112342 | && (dists[w*16+g] < FFMIN(euplims[w*16+g], uplims[w*16+g])) |
| 654 |
2/2✓ Branch 0 taken 16168 times.
✓ Branch 1 taken 84101 times.
|
100269 | && (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 27707 times.
✓ Branch 1 taken 890 times.
✓ Branch 2 taken 27707 times.
✗ Branch 3 not taken.
|
28597 | for (i = 0; i < depth && sce->sf_idx[w*16+g] < maxdeltasf; ++i) { |
| 658 | int cb, bits; | ||
| 659 | float dist, qenergy; | ||
| 660 | 27707 | cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]+1); | |
| 661 |
2/2✓ Branch 0 taken 27689 times.
✓ Branch 1 taken 18 times.
|
27707 | if (cb > 0) { |
| 662 | 27689 | dist = qenergy = 0.f; | |
| 663 | 27689 | bits = 0; | |
| 664 |
2/2✓ Branch 0 taken 27733 times.
✓ Branch 1 taken 27689 times.
|
55422 | for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) { |
| 665 | int b; | ||
| 666 | float sqenergy; | ||
| 667 | 55466 | dist += quantize_band_cost_cached(s, w + w2, g, coefs + w2*128, | |
| 668 | 27733 | scaled + w2*128, | |
| 669 | 27733 | sce->ics.swb_sizes[g], | |
| 670 | 27733 | sce->sf_idx[w*16+g]+1, | |
| 671 | cb, | ||
| 672 | 1.0f, | ||
| 673 | INFINITY, | ||
| 674 | &b, &sqenergy, | ||
| 675 | 0); | ||
| 676 | 27733 | bits += b; | |
| 677 | 27733 | qenergy += sqenergy; | |
| 678 | } | ||
| 679 | 27689 | dist -= bits; | |
| 680 |
4/4✓ Branch 0 taken 20236 times.
✓ Branch 1 taken 7453 times.
✓ Branch 2 taken 12429 times.
✓ Branch 3 taken 15260 times.
|
27689 | if (dist < FFMIN(euplims[w*16+g], uplims[w*16+g])) { |
| 681 | 12429 | sce->sf_idx[w*16+g]++; | |
| 682 | 12429 | dists[w*16+g] = dist; | |
| 683 | 12429 | qenergies[w*16+g] = qenergy; | |
| 684 | } else { | ||
| 685 | 15260 | break; | |
| 686 | } | ||
| 687 | } else { | ||
| 688 | 18 | maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]); | |
| 689 | 18 | break; | |
| 690 | } | ||
| 691 | } | ||
| 692 | } | ||
| 693 | 868073 | prev = sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], mindeltasf, maxdeltasf); | |
| 694 |
2/2✓ Branch 0 taken 577073 times.
✓ Branch 1 taken 291000 times.
|
868073 | if (sce->sf_idx[w*16+g] != prevsc) |
| 695 | 577073 | fflag = 1; | |
| 696 | 868073 | nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]); | |
| 697 | 868073 | sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
| 698 | } | ||
| 699 | 1058750 | start += sce->ics.swb_sizes[g]; | |
| 700 | } | ||
| 701 | } | ||
| 702 | |||
| 703 | /** SF difference limit violation risk. Must re-clamp. */ | ||
| 704 | 21560 | prev = -1; | |
| 705 |
2/2✓ Branch 0 taken 22850 times.
✓ Branch 1 taken 21560 times.
|
44410 | for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) { |
| 706 |
2/2✓ Branch 0 taken 1058750 times.
✓ Branch 1 taken 22850 times.
|
1081600 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 707 |
2/2✓ Branch 0 taken 868073 times.
✓ Branch 1 taken 190677 times.
|
1058750 | if (!sce->zeroes[w*16+g]) { |
| 708 | 868073 | int prevsf = sce->sf_idx[w*16+g]; | |
| 709 |
2/2✓ Branch 0 taken 21560 times.
✓ Branch 1 taken 846513 times.
|
868073 | if (prev < 0) |
| 710 | 21560 | prev = prevsf; | |
| 711 | 868073 | sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], prev - SCALE_MAX_DIFF, prev + SCALE_MAX_DIFF); | |
| 712 | 868073 | sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]); | |
| 713 | 868073 | prev = sce->sf_idx[w*16+g]; | |
| 714 |
3/4✓ Branch 0 taken 5471 times.
✓ Branch 1 taken 862602 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5471 times.
|
868073 | if (!fflag && prevsf != sce->sf_idx[w*16+g]) |
| 715 | ✗ | fflag = 1; | |
| 716 | } | ||
| 717 | } | ||
| 718 | } | ||
| 719 | |||
| 720 | 21560 | its++; | |
| 721 |
4/4✓ Branch 0 taken 21427 times.
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 20778 times.
✓ Branch 3 taken 649 times.
|
21560 | } while (fflag && its < maxits); |
| 722 | |||
| 723 | /** Scout out next nonzero bands */ | ||
| 724 | 782 | ff_init_nextband_map(sce, nextband); | |
| 725 | |||
| 726 | 782 | prev = -1; | |
| 727 |
2/2✓ Branch 0 taken 825 times.
✓ Branch 1 taken 782 times.
|
1607 | 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 38395 times.
✓ Branch 1 taken 825 times.
|
39220 | for (g = 0; g < sce->ics.num_swb; g++) { |
| 730 |
2/2✓ Branch 0 taken 29389 times.
✓ Branch 1 taken 9006 times.
|
38395 | if (!sce->zeroes[w*16+g]) { |
| 731 | 29389 | 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 29215 times.
|
29389 | 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 | 9006 | sce->band_type[w*16+g] = 0; | |
| 743 | } | ||
| 744 | /** Check that there's no SF delta range violations */ | ||
| 745 |
2/2✓ Branch 0 taken 29389 times.
✓ Branch 1 taken 9006 times.
|
38395 | if (!sce->zeroes[w*16+g]) { |
| 746 |
2/2✓ Branch 0 taken 28607 times.
✓ Branch 1 taken 782 times.
|
29389 | if (prev != -1) { |
| 747 | 28607 | 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 590 times.
|
782 | } 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 | 29389 | prev = sce->sf_idx[w*16+g]; | |
| 754 | } | ||
| 755 | } | ||
| 756 | } | ||
| 757 | } | ||
| 758 | |||
| 759 | #endif /* AVCODEC_AACCODER_TWOLOOP_H */ | ||
| 760 |