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