| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 3 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 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 | #include <stdint.h> | ||
| 23 | |||
| 24 | #include "celt.h" | ||
| 25 | #include "pvq.h" | ||
| 26 | #include "tab.h" | ||
| 27 | |||
| 28 | 29972 | void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc) | |
| 29 | { | ||
| 30 | float lowband_scratch[8 * 22]; | ||
| 31 | float norm1[2 * 8 * 100]; | ||
| 32 | 29972 | float *norm2 = norm1 + 8 * 100; | |
| 33 | |||
| 34 | 29972 | int totalbits = (f->framebits << 3) - f->anticollapse_needed; | |
| 35 | |||
| 36 | 29972 | int update_lowband = 1; | |
| 37 | 29972 | int lowband_offset = 0; | |
| 38 | |||
| 39 | int i, j; | ||
| 40 | |||
| 41 |
2/2✓ Branch 0 taken 509159 times.
✓ Branch 1 taken 29972 times.
|
539131 | for (i = f->start_band; i < f->end_band; i++) { |
| 42 | 509159 | uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 }; | |
| 43 | 509159 | int band_offset = ff_celt_freq_bands[i] << f->size; | |
| 44 | 509159 | int band_size = ff_celt_freq_range[i] << f->size; | |
| 45 | 509159 | float *X = f->block[0].coeffs + band_offset; | |
| 46 |
2/2✓ Branch 0 taken 342205 times.
✓ Branch 1 taken 166954 times.
|
509159 | float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL; |
| 47 | float *norm_loc1, *norm_loc2; | ||
| 48 | |||
| 49 | 509159 | int consumed = opus_rc_tell_frac(rc); | |
| 50 | 509159 | int effective_lowband = -1; | |
| 51 | 509159 | int b = 0; | |
| 52 | |||
| 53 | /* Compute how many bits we want to allocate to this band */ | ||
| 54 |
2/2✓ Branch 0 taken 479187 times.
✓ Branch 1 taken 29972 times.
|
509159 | if (i != f->start_band) |
| 55 | 479187 | f->remaining -= consumed; | |
| 56 | 509159 | f->remaining2 = totalbits - consumed - 1; | |
| 57 |
2/2✓ Branch 0 taken 458185 times.
✓ Branch 1 taken 50974 times.
|
509159 | if (i <= f->coded_bands - 1) { |
| 58 | 458185 | int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i); | |
| 59 |
2/2✓ Branch 0 taken 458177 times.
✓ Branch 1 taken 8 times.
|
458185 | b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14); |
| 60 | } | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 34961 times.
✓ Branch 1 taken 474198 times.
|
509159 | if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] || |
| 63 |
6/6✓ Branch 0 taken 4989 times.
✓ Branch 1 taken 29972 times.
✓ Branch 2 taken 97835 times.
✓ Branch 3 taken 381352 times.
✓ Branch 4 taken 6526 times.
✓ Branch 5 taken 91309 times.
|
509159 | i == f->start_band + 1) && (update_lowband || lowband_offset == 0)) |
| 64 | 387878 | lowband_offset = i; | |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 29972 times.
✓ Branch 1 taken 479187 times.
|
509159 | if (i == f->start_band + 1) { |
| 67 | /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into | ||
| 68 | the second to ensure the second band never has to use the LCG. */ | ||
| 69 | 29972 | int count = (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]) << f->size; | |
| 70 | |||
| 71 | 29972 | memcpy(&norm1[band_offset], &norm1[band_offset - count], count * sizeof(float)); | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 18804 times.
✓ Branch 1 taken 11168 times.
|
29972 | if (f->channels == 2) |
| 74 | 18804 | memcpy(&norm2[band_offset], &norm2[band_offset - count], count * sizeof(float)); | |
| 75 | } | ||
| 76 | |||
| 77 | /* Get a conservative estimate of the collapse_mask's for the bands we're | ||
| 78 | going to be folding from. */ | ||
| 79 |
4/4✓ Branch 0 taken 479187 times.
✓ Branch 1 taken 29972 times.
✓ Branch 2 taken 71137 times.
✓ Branch 3 taken 408050 times.
|
509159 | if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE || |
| 80 |
3/4✓ Branch 0 taken 71137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11835 times.
✓ Branch 3 taken 59302 times.
|
71137 | f->blocks > 1 || f->tf_change[i] < 0)) { |
| 81 | int foldstart, foldend; | ||
| 82 | |||
| 83 | /* This ensures we never repeat spectral content within one band */ | ||
| 84 | 419885 | effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band], | |
| 85 | ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]); | ||
| 86 | 419885 | foldstart = lowband_offset; | |
| 87 |
2/2✓ Branch 0 taken 198091 times.
✓ Branch 1 taken 419885 times.
|
617976 | while (ff_celt_freq_bands[--foldstart] > effective_lowband); |
| 88 | 419885 | foldend = lowband_offset - 1; | |
| 89 |
4/4✓ Branch 0 taken 104596 times.
✓ Branch 1 taken 345023 times.
✓ Branch 2 taken 29734 times.
✓ Branch 3 taken 74862 times.
|
449619 | while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]); |
| 90 | |||
| 91 | 419885 | cm[0] = cm[1] = 0; | |
| 92 |
2/2✓ Branch 0 taken 647710 times.
✓ Branch 1 taken 419885 times.
|
1067595 | for (j = foldstart; j < foldend; j++) { |
| 93 | 647710 | cm[0] |= f->block[0].collapse_masks[j]; | |
| 94 | 647710 | cm[1] |= f->block[f->channels - 1].collapse_masks[j]; | |
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 |
4/4✓ Branch 0 taken 36886 times.
✓ Branch 1 taken 472273 times.
✓ Branch 2 taken 1677 times.
✓ Branch 3 taken 35209 times.
|
509159 | if (f->dual_stereo && i == f->intensity_stereo) { |
| 99 | /* Switch off dual stereo to do intensity */ | ||
| 100 | 1677 | f->dual_stereo = 0; | |
| 101 |
2/2✓ Branch 0 taken 572238 times.
✓ Branch 1 taken 1677 times.
|
573915 | for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++) |
| 102 | 572238 | norm1[j] = (norm1[j] + norm2[j]) / 2; | |
| 103 | } | ||
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 419885 times.
✓ Branch 1 taken 89274 times.
|
509159 | norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL; |
| 106 |
2/2✓ Branch 0 taken 419885 times.
✓ Branch 1 taken 89274 times.
|
509159 | norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL; |
| 107 | |||
| 108 |
2/2✓ Branch 0 taken 35209 times.
✓ Branch 1 taken 473950 times.
|
509159 | if (f->dual_stereo) { |
| 109 | 70418 | cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1, | |
| 110 | 35209 | f->blocks, norm_loc1, f->size, | |
| 111 | 35209 | norm1 + band_offset, 0, 1.0f, | |
| 112 | 35209 | lowband_scratch, cm[0]); | |
| 113 | |||
| 114 | 35209 | cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1, | |
| 115 | 35209 | f->blocks, norm_loc2, f->size, | |
| 116 | 35209 | norm2 + band_offset, 0, 1.0f, | |
| 117 | 35209 | lowband_scratch, cm[1]); | |
| 118 | } else { | ||
| 119 | 947900 | cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0, | |
| 120 | 473950 | f->blocks, norm_loc1, f->size, | |
| 121 | 473950 | norm1 + band_offset, 0, 1.0f, | |
| 122 | 473950 | lowband_scratch, cm[0] | cm[1]); | |
| 123 | 473950 | cm[1] = cm[0]; | |
| 124 | } | ||
| 125 | |||
| 126 | 509159 | f->block[0].collapse_masks[i] = (uint8_t)cm[0]; | |
| 127 | 509159 | f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1]; | |
| 128 | 509159 | f->remaining += f->pulses[i] + consumed; | |
| 129 | |||
| 130 | /* Update the folding position only as long as we have 1 bit/sample depth */ | ||
| 131 | 509159 | update_lowband = (b > band_size << 3); | |
| 132 | } | ||
| 133 | 29972 | } | |
| 134 | |||
| 135 | #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2) | ||
| 136 | |||
| 137 | 29972 | void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode) | |
| 138 | { | ||
| 139 | int i, j, low, high, total, done, bandbits, remaining, tbits_8ths; | ||
| 140 | 29972 | int skip_startband = f->start_band; | |
| 141 | 29972 | int skip_bit = 0; | |
| 142 | 29972 | int intensitystereo_bit = 0; | |
| 143 | 29972 | int dualstereo_bit = 0; | |
| 144 | 29972 | int dynalloc = 6; | |
| 145 | 29972 | int extrabits = 0; | |
| 146 | |||
| 147 | 29972 | int boost[CELT_MAX_BANDS] = { 0 }; | |
| 148 | int trim_offset[CELT_MAX_BANDS]; | ||
| 149 | int threshold[CELT_MAX_BANDS]; | ||
| 150 | int bits1[CELT_MAX_BANDS]; | ||
| 151 | int bits2[CELT_MAX_BANDS]; | ||
| 152 | |||
| 153 | /* Spread */ | ||
| 154 |
2/2✓ Branch 1 taken 29825 times.
✓ Branch 2 taken 147 times.
|
29972 | if (opus_rc_tell(rc) + 4 <= f->framebits) { |
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29825 times.
|
29825 | if (encode) |
| 156 | ✗ | ff_opus_rc_enc_cdf(rc, f->spread, ff_celt_model_spread); | |
| 157 | else | ||
| 158 | 29825 | f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread); | |
| 159 | } else { | ||
| 160 | 147 | f->spread = CELT_SPREAD_NORMAL; | |
| 161 | } | ||
| 162 | |||
| 163 | /* Initialize static allocation caps */ | ||
| 164 |
2/2✓ Branch 0 taken 629412 times.
✓ Branch 1 taken 29972 times.
|
659384 | for (i = 0; i < CELT_MAX_BANDS; i++) |
| 165 | 629412 | f->caps[i] = NORMC((ff_celt_static_caps[f->size][f->channels - 1][i] + 64) * ff_celt_freq_range[i]); | |
| 166 | |||
| 167 | /* Band boosts */ | ||
| 168 | 29972 | tbits_8ths = f->framebits << 3; | |
| 169 |
2/2✓ Branch 0 taken 509159 times.
✓ Branch 1 taken 29972 times.
|
539131 | for (i = f->start_band; i < f->end_band; i++) { |
| 170 | 509159 | int quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size; | |
| 171 | 509159 | int b_dynalloc = dynalloc; | |
| 172 | 509159 | int boost_amount = f->alloc_boost[i]; | |
| 173 | 509159 | quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta)); | |
| 174 | |||
| 175 |
3/4✓ Branch 1 taken 523265 times.
✓ Branch 2 taken 2763 times.
✓ Branch 3 taken 523265 times.
✗ Branch 4 not taken.
|
526028 | while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < f->caps[i]) { |
| 176 | int is_boost; | ||
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 523265 times.
|
523265 | if (encode) { |
| 178 | ✗ | is_boost = boost_amount--; | |
| 179 | ✗ | ff_opus_rc_enc_log(rc, is_boost, b_dynalloc); | |
| 180 | } else { | ||
| 181 | 523265 | is_boost = ff_opus_rc_dec_log(rc, b_dynalloc); | |
| 182 | } | ||
| 183 | |||
| 184 |
2/2✓ Branch 0 taken 506396 times.
✓ Branch 1 taken 16869 times.
|
523265 | if (!is_boost) |
| 185 | 506396 | break; | |
| 186 | |||
| 187 | 16869 | boost[i] += quanta; | |
| 188 | 16869 | tbits_8ths -= quanta; | |
| 189 | |||
| 190 | 16869 | b_dynalloc = 1; | |
| 191 | } | ||
| 192 | |||
| 193 |
2/2✓ Branch 0 taken 12658 times.
✓ Branch 1 taken 496501 times.
|
509159 | if (boost[i]) |
| 194 | 12658 | dynalloc = FFMAX(dynalloc - 1, 2); | |
| 195 | } | ||
| 196 | |||
| 197 | /* Allocation trim */ | ||
| 198 |
1/2✓ Branch 0 taken 29972 times.
✗ Branch 1 not taken.
|
29972 | if (!encode) |
| 199 | 29972 | f->alloc_trim = 5; | |
| 200 |
2/2✓ Branch 1 taken 29821 times.
✓ Branch 2 taken 151 times.
|
29972 | if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths) |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29821 times.
|
29821 | if (encode) |
| 202 | ✗ | ff_opus_rc_enc_cdf(rc, f->alloc_trim, ff_celt_model_alloc_trim); | |
| 203 | else | ||
| 204 | 29821 | f->alloc_trim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim); | |
| 205 | |||
| 206 | /* Anti-collapse bit reservation */ | ||
| 207 | 29972 | tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1; | |
| 208 | 29972 | f->anticollapse_needed = 0; | |
| 209 |
6/6✓ Branch 0 taken 4049 times.
✓ Branch 1 taken 25923 times.
✓ Branch 2 taken 3166 times.
✓ Branch 3 taken 883 times.
✓ Branch 4 taken 3163 times.
✓ Branch 5 taken 3 times.
|
29972 | if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3)) |
| 210 | 3163 | f->anticollapse_needed = 1 << 3; | |
| 211 | 29972 | tbits_8ths -= f->anticollapse_needed; | |
| 212 | |||
| 213 | /* Band skip bit reservation */ | ||
| 214 |
2/2✓ Branch 0 taken 29827 times.
✓ Branch 1 taken 145 times.
|
29972 | if (tbits_8ths >= 1 << 3) |
| 215 | 29827 | skip_bit = 1 << 3; | |
| 216 | 29972 | tbits_8ths -= skip_bit; | |
| 217 | |||
| 218 | /* Intensity/dual stereo bit reservation */ | ||
| 219 |
2/2✓ Branch 0 taken 18804 times.
✓ Branch 1 taken 11168 times.
|
29972 | if (f->channels == 2) { |
| 220 | 18804 | intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band]; | |
| 221 |
2/2✓ Branch 0 taken 18726 times.
✓ Branch 1 taken 78 times.
|
18804 | if (intensitystereo_bit <= tbits_8ths) { |
| 222 | 18726 | tbits_8ths -= intensitystereo_bit; | |
| 223 |
2/2✓ Branch 0 taken 18722 times.
✓ Branch 1 taken 4 times.
|
18726 | if (tbits_8ths >= 1 << 3) { |
| 224 | 18722 | dualstereo_bit = 1 << 3; | |
| 225 | 18722 | tbits_8ths -= 1 << 3; | |
| 226 | } | ||
| 227 | } else { | ||
| 228 | 78 | intensitystereo_bit = 0; | |
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | /* Trim offsets */ | ||
| 233 |
2/2✓ Branch 0 taken 509159 times.
✓ Branch 1 taken 29972 times.
|
539131 | for (i = f->start_band; i < f->end_band; i++) { |
| 234 | 509159 | int trim = f->alloc_trim - 5 - f->size; | |
| 235 | 509159 | int band = ff_celt_freq_range[i] * (f->end_band - i - 1); | |
| 236 | 509159 | int duration = f->size + 3; | |
| 237 | 509159 | int scale = duration + f->channels - 1; | |
| 238 | |||
| 239 | /* PVQ minimum allocation threshold, below this value the band is | ||
| 240 | * skipped */ | ||
| 241 | 509159 | threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4, | |
| 242 | f->channels << 3); | ||
| 243 | |||
| 244 | 509159 | trim_offset[i] = trim * (band << scale) >> 6; | |
| 245 | |||
| 246 |
2/2✓ Branch 0 taken 109648 times.
✓ Branch 1 taken 399511 times.
|
509159 | if (ff_celt_freq_range[i] << f->size == 1) |
| 247 | 109648 | trim_offset[i] -= f->channels << 3; | |
| 248 | } | ||
| 249 | |||
| 250 | /* Bisection */ | ||
| 251 | 29972 | low = 1; | |
| 252 | 29972 | high = CELT_VECTORS - 1; | |
| 253 |
2/2✓ Branch 0 taken 111923 times.
✓ Branch 1 taken 29972 times.
|
141895 | while (low <= high) { |
| 254 | 111923 | int center = (low + high) >> 1; | |
| 255 | 111923 | done = total = 0; | |
| 256 | |||
| 257 |
2/2✓ Branch 0 taken 1905424 times.
✓ Branch 1 taken 111923 times.
|
2017347 | for (i = f->end_band - 1; i >= f->start_band; i--) { |
| 258 | 1905424 | bandbits = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]); | |
| 259 | |||
| 260 |
2/2✓ Branch 0 taken 1815260 times.
✓ Branch 1 taken 90164 times.
|
1905424 | if (bandbits) |
| 261 | 1815260 | bandbits = FFMAX(bandbits + trim_offset[i], 0); | |
| 262 | 1905424 | bandbits += boost[i]; | |
| 263 | |||
| 264 |
4/4✓ Branch 0 taken 171798 times.
✓ Branch 1 taken 1733626 times.
✓ Branch 2 taken 21091 times.
✓ Branch 3 taken 150707 times.
|
1905424 | if (bandbits >= threshold[i] || done) { |
| 265 | 1754717 | done = 1; | |
| 266 | 1754717 | total += FFMIN(bandbits, f->caps[i]); | |
| 267 |
2/2✓ Branch 0 taken 31616 times.
✓ Branch 1 taken 119091 times.
|
150707 | } else if (bandbits >= f->channels << 3) { |
| 268 | 31616 | total += f->channels << 3; | |
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 |
2/2✓ Branch 0 taken 40011 times.
✓ Branch 1 taken 71912 times.
|
111923 | if (total > tbits_8ths) |
| 273 | 40011 | high = center - 1; | |
| 274 | else | ||
| 275 | 71912 | low = center + 1; | |
| 276 | } | ||
| 277 | 29972 | high = low--; | |
| 278 | |||
| 279 | /* Bisection */ | ||
| 280 |
2/2✓ Branch 0 taken 509159 times.
✓ Branch 1 taken 29972 times.
|
539131 | for (i = f->start_band; i < f->end_band; i++) { |
| 281 | 509159 | bits1[i] = NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]); | |
| 282 |
2/2✓ Branch 0 taken 37784 times.
✓ Branch 1 taken 471375 times.
|
509159 | bits2[i] = high >= CELT_VECTORS ? f->caps[i] : |
| 283 | 471375 | NORMC(ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]); | |
| 284 | |||
| 285 |
2/2✓ Branch 0 taken 465823 times.
✓ Branch 1 taken 43336 times.
|
509159 | if (bits1[i]) |
| 286 | 465823 | bits1[i] = FFMAX(bits1[i] + trim_offset[i], 0); | |
| 287 |
2/2✓ Branch 0 taken 497171 times.
✓ Branch 1 taken 11988 times.
|
509159 | if (bits2[i]) |
| 288 | 497171 | bits2[i] = FFMAX(bits2[i] + trim_offset[i], 0); | |
| 289 | |||
| 290 |
2/2✓ Branch 0 taken 486569 times.
✓ Branch 1 taken 22590 times.
|
509159 | if (low) |
| 291 | 486569 | bits1[i] += boost[i]; | |
| 292 | 509159 | bits2[i] += boost[i]; | |
| 293 | |||
| 294 |
2/2✓ Branch 0 taken 12658 times.
✓ Branch 1 taken 496501 times.
|
509159 | if (boost[i]) |
| 295 | 12658 | skip_startband = i; | |
| 296 | 509159 | bits2[i] = FFMAX(bits2[i] - bits1[i], 0); | |
| 297 | } | ||
| 298 | |||
| 299 | /* Bisection */ | ||
| 300 | 29972 | low = 0; | |
| 301 | 29972 | high = 1 << CELT_ALLOC_STEPS; | |
| 302 |
2/2✓ Branch 0 taken 179832 times.
✓ Branch 1 taken 29972 times.
|
209804 | for (i = 0; i < CELT_ALLOC_STEPS; i++) { |
| 303 | 179832 | int center = (low + high) >> 1; | |
| 304 | 179832 | done = total = 0; | |
| 305 | |||
| 306 |
2/2✓ Branch 0 taken 3054954 times.
✓ Branch 1 taken 179832 times.
|
3234786 | for (j = f->end_band - 1; j >= f->start_band; j--) { |
| 307 | 3054954 | bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS); | |
| 308 | |||
| 309 |
4/4✓ Branch 0 taken 263391 times.
✓ Branch 1 taken 2791563 times.
✓ Branch 2 taken 51935 times.
✓ Branch 3 taken 211456 times.
|
3054954 | if (bandbits >= threshold[j] || done) { |
| 310 | 2843498 | done = 1; | |
| 311 | 2843498 | total += FFMIN(bandbits, f->caps[j]); | |
| 312 |
2/2✓ Branch 0 taken 52533 times.
✓ Branch 1 taken 158923 times.
|
211456 | } else if (bandbits >= f->channels << 3) |
| 313 | 52533 | total += f->channels << 3; | |
| 314 | } | ||
| 315 |
2/2✓ Branch 0 taken 85909 times.
✓ Branch 1 taken 93923 times.
|
179832 | if (total > tbits_8ths) |
| 316 | 85909 | high = center; | |
| 317 | else | ||
| 318 | 93923 | low = center; | |
| 319 | } | ||
| 320 | |||
| 321 | /* Bisection */ | ||
| 322 | 29972 | done = total = 0; | |
| 323 |
2/2✓ Branch 0 taken 509159 times.
✓ Branch 1 taken 29972 times.
|
539131 | for (i = f->end_band - 1; i >= f->start_band; i--) { |
| 324 | 509159 | bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS); | |
| 325 | |||
| 326 |
4/4✓ Branch 0 taken 45447 times.
✓ Branch 1 taken 463712 times.
✓ Branch 2 taken 9156 times.
✓ Branch 3 taken 36291 times.
|
509159 | if (bandbits >= threshold[i] || done) |
| 327 | 472868 | done = 1; | |
| 328 | else | ||
| 329 | 36291 | bandbits = (bandbits >= f->channels << 3) ? | |
| 330 |
2/2✓ Branch 0 taken 8853 times.
✓ Branch 1 taken 27438 times.
|
36291 | f->channels << 3 : 0; |
| 331 | |||
| 332 | 509159 | bandbits = FFMIN(bandbits, f->caps[i]); | |
| 333 | 509159 | f->pulses[i] = bandbits; | |
| 334 | 509159 | total += bandbits; | |
| 335 | } | ||
| 336 | |||
| 337 | /* Band skipping */ | ||
| 338 | 29972 | for (f->coded_bands = f->end_band; ; f->coded_bands--) { | |
| 339 | int allocation; | ||
| 340 | 80946 | j = f->coded_bands - 1; | |
| 341 | |||
| 342 |
2/2✓ Branch 0 taken 1661 times.
✓ Branch 1 taken 79285 times.
|
80946 | if (j == skip_startband) { |
| 343 | /* all remaining bands are not skipped */ | ||
| 344 | 1661 | tbits_8ths += skip_bit; | |
| 345 | 1661 | break; | |
| 346 | } | ||
| 347 | |||
| 348 | /* determine the number of bits available for coding "do not skip" markers */ | ||
| 349 | 79285 | remaining = tbits_8ths - total; | |
| 350 | 79285 | bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]); | |
| 351 | 79285 | remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]); | |
| 352 | 79285 | allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j]; | |
| 353 | 79285 | allocation += FFMAX(remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]), 0); | |
| 354 | |||
| 355 | /* a "do not skip" marker is only coded if the allocation is | ||
| 356 | * above the chosen threshold */ | ||
| 357 |
2/2✓ Branch 0 taken 38211 times.
✓ Branch 1 taken 41074 times.
|
79285 | if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) { |
| 358 | int do_not_skip; | ||
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38211 times.
|
38211 | if (encode) { |
| 360 | ✗ | do_not_skip = f->coded_bands <= f->skip_band_floor; | |
| 361 | ✗ | ff_opus_rc_enc_log(rc, do_not_skip, 1); | |
| 362 | } else { | ||
| 363 | 38211 | do_not_skip = ff_opus_rc_dec_log(rc, 1); | |
| 364 | } | ||
| 365 | |||
| 366 |
2/2✓ Branch 0 taken 28311 times.
✓ Branch 1 taken 9900 times.
|
38211 | if (do_not_skip) |
| 367 | 28311 | break; | |
| 368 | |||
| 369 | 9900 | total += 1 << 3; | |
| 370 | 9900 | allocation -= 1 << 3; | |
| 371 | } | ||
| 372 | |||
| 373 | /* the band is skipped, so reclaim its bits */ | ||
| 374 | 50974 | total -= f->pulses[j]; | |
| 375 |
2/2✓ Branch 0 taken 34531 times.
✓ Branch 1 taken 16443 times.
|
50974 | if (intensitystereo_bit) { |
| 376 | 34531 | total -= intensitystereo_bit; | |
| 377 | 34531 | intensitystereo_bit = ff_celt_log2_frac[j - f->start_band]; | |
| 378 | 34531 | total += intensitystereo_bit; | |
| 379 | } | ||
| 380 | |||
| 381 |
2/2✓ Branch 0 taken 21888 times.
✓ Branch 1 taken 29086 times.
|
50974 | total += f->pulses[j] = (allocation >= f->channels << 3) ? f->channels << 3 : 0; |
| 382 | } | ||
| 383 | |||
| 384 | /* IS start band */ | ||
| 385 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29972 times.
|
29972 | if (encode) { |
| 386 | ✗ | if (intensitystereo_bit) { | |
| 387 | ✗ | f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands); | |
| 388 | ✗ | ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band); | |
| 389 | } | ||
| 390 | } else { | ||
| 391 | 29972 | f->intensity_stereo = f->dual_stereo = 0; | |
| 392 |
2/2✓ Branch 0 taken 18726 times.
✓ Branch 1 taken 11246 times.
|
29972 | if (intensitystereo_bit) |
| 393 | 18726 | f->intensity_stereo = f->start_band + ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band); | |
| 394 | } | ||
| 395 | |||
| 396 | /* DS flag */ | ||
| 397 |
2/2✓ Branch 0 taken 13242 times.
✓ Branch 1 taken 16730 times.
|
29972 | if (f->intensity_stereo <= f->start_band) |
| 398 | 13242 | tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */ | |
| 399 |
1/2✓ Branch 0 taken 16730 times.
✗ Branch 1 not taken.
|
16730 | else if (dualstereo_bit) |
| 400 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16730 times.
|
16730 | if (encode) |
| 401 | ✗ | ff_opus_rc_enc_log(rc, f->dual_stereo, 1); | |
| 402 | else | ||
| 403 | 16730 | f->dual_stereo = ff_opus_rc_dec_log(rc, 1); | |
| 404 | |||
| 405 | /* Supply the remaining bits in this frame to lower bands */ | ||
| 406 | 29972 | remaining = tbits_8ths - total; | |
| 407 | 29972 | bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]); | |
| 408 | 29972 | remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]); | |
| 409 |
2/2✓ Branch 0 taken 458185 times.
✓ Branch 1 taken 29972 times.
|
488157 | for (i = f->start_band; i < f->coded_bands; i++) { |
| 410 | 458185 | const int bits = FFMIN(remaining, ff_celt_freq_range[i]); | |
| 411 | 458185 | f->pulses[i] += bits + bandbits * ff_celt_freq_range[i]; | |
| 412 | 458185 | remaining -= bits; | |
| 413 | } | ||
| 414 | |||
| 415 | /* Finally determine the allocation */ | ||
| 416 |
2/2✓ Branch 0 taken 458185 times.
✓ Branch 1 taken 29972 times.
|
488157 | for (i = f->start_band; i < f->coded_bands; i++) { |
| 417 | 458185 | int N = ff_celt_freq_range[i] << f->size; | |
| 418 | 458185 | int prev_extra = extrabits; | |
| 419 | 458185 | f->pulses[i] += extrabits; | |
| 420 | |||
| 421 |
2/2✓ Branch 0 taken 351634 times.
✓ Branch 1 taken 106551 times.
|
458185 | if (N > 1) { |
| 422 | int dof; /* degrees of freedom */ | ||
| 423 | int temp; /* dof * channels * log(dof) */ | ||
| 424 | int fine_bits; | ||
| 425 | int max_bits; | ||
| 426 | int offset; /* fine energy quantization offset, i.e. | ||
| 427 | * extra bits assigned over the standard | ||
| 428 | * totalbits/dof */ | ||
| 429 | |||
| 430 | 351634 | extrabits = FFMAX(f->pulses[i] - f->caps[i], 0); | |
| 431 | 351634 | f->pulses[i] -= extrabits; | |
| 432 | |||
| 433 | /* intensity stereo makes use of an extra degree of freedom */ | ||
| 434 |
8/8✓ Branch 0 taken 242758 times.
✓ Branch 1 taken 108876 times.
✓ Branch 2 taken 190421 times.
✓ Branch 3 taken 52337 times.
✓ Branch 4 taken 156074 times.
✓ Branch 5 taken 34347 times.
✓ Branch 6 taken 140074 times.
✓ Branch 7 taken 16000 times.
|
351634 | dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo); |
| 435 | 351634 | temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3)); | |
| 436 | 351634 | offset = (temp >> 1) - dof * CELT_FINE_OFFSET; | |
| 437 |
2/2✓ Branch 0 taken 83666 times.
✓ Branch 1 taken 267968 times.
|
351634 | if (N == 2) /* dof=2 is the only case that doesn't fit the model */ |
| 438 | 83666 | offset += dof << 1; | |
| 439 | |||
| 440 | /* grant an additional bias for the first and second pulses */ | ||
| 441 |
2/2✓ Branch 0 taken 247373 times.
✓ Branch 1 taken 104261 times.
|
351634 | if (f->pulses[i] + offset < 2 * (dof << 3)) |
| 442 | 247373 | offset += temp >> 2; | |
| 443 |
2/2✓ Branch 0 taken 50189 times.
✓ Branch 1 taken 54072 times.
|
104261 | else if (f->pulses[i] + offset < 3 * (dof << 3)) |
| 444 | 50189 | offset += temp >> 3; | |
| 445 | |||
| 446 | 351634 | fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3); | |
| 447 | 351634 | max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS); | |
| 448 | 351634 | max_bits = FFMAX(max_bits, 0); | |
| 449 | 351634 | f->fine_bits[i] = av_clip(fine_bits, 0, max_bits); | |
| 450 | |||
| 451 | /* If fine_bits was rounded down or capped, | ||
| 452 | * give priority for the final fine energy pass */ | ||
| 453 | 351634 | f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset); | |
| 454 | |||
| 455 | /* the remaining bits are assigned to PVQ */ | ||
| 456 | 351634 | f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3; | |
| 457 | } else { | ||
| 458 | /* all bits go to fine energy except for the sign bit */ | ||
| 459 | 106551 | extrabits = FFMAX(f->pulses[i] - (f->channels << 3), 0); | |
| 460 | 106551 | f->pulses[i] -= extrabits; | |
| 461 | 106551 | f->fine_bits[i] = 0; | |
| 462 | 106551 | f->fine_priority[i] = 1; | |
| 463 | } | ||
| 464 | |||
| 465 | /* hand back a limited number of extra fine energy bits to this band */ | ||
| 466 |
2/2✓ Branch 0 taken 123207 times.
✓ Branch 1 taken 334978 times.
|
458185 | if (extrabits > 0) { |
| 467 | 123207 | int fineextra = FFMIN(extrabits >> (f->channels + 2), | |
| 468 | CELT_MAX_FINE_BITS - f->fine_bits[i]); | ||
| 469 | 123207 | f->fine_bits[i] += fineextra; | |
| 470 | |||
| 471 | 123207 | fineextra <<= f->channels + 2; | |
| 472 | 123207 | f->fine_priority[i] = (fineextra >= extrabits - prev_extra); | |
| 473 | 123207 | extrabits -= fineextra; | |
| 474 | } | ||
| 475 | } | ||
| 476 | 29972 | f->remaining = extrabits; | |
| 477 | |||
| 478 | /* skipped bands dedicate all of their bits for fine energy */ | ||
| 479 |
2/2✓ Branch 0 taken 50974 times.
✓ Branch 1 taken 29972 times.
|
80946 | for (; i < f->end_band; i++) { |
| 480 | 50974 | f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3; | |
| 481 | 50974 | f->pulses[i] = 0; | |
| 482 | 50974 | f->fine_priority[i] = f->fine_bits[i] < 1; | |
| 483 | } | ||
| 484 | 29972 | } | |
| 485 |