| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2007-2008 CSIRO | ||
| 3 | * Copyright (c) 2007-2009 Xiph.Org Foundation | ||
| 4 | * Copyright (c) 2008-2009 Gregory Maxwell | ||
| 5 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 6 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 7 | * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com> | ||
| 8 | * | ||
| 9 | * This file is part of FFmpeg. | ||
| 10 | * | ||
| 11 | * FFmpeg is free software; you can redistribute it and/or | ||
| 12 | * modify it under the terms of the GNU Lesser General Public | ||
| 13 | * License as published by the Free Software Foundation; either | ||
| 14 | * version 2.1 of the License, or (at your option) any later version. | ||
| 15 | * | ||
| 16 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 19 | * Lesser General Public License for more details. | ||
| 20 | * | ||
| 21 | * You should have received a copy of the GNU Lesser General Public | ||
| 22 | * License along with FFmpeg; if not, write to the Free Software | ||
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <float.h> | ||
| 27 | |||
| 28 | #include "config_components.h" | ||
| 29 | |||
| 30 | #include "libavutil/mem.h" | ||
| 31 | #include "mathops.h" | ||
| 32 | #include "tab.h" | ||
| 33 | #include "pvq.h" | ||
| 34 | |||
| 35 | #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15) | ||
| 36 | |||
| 37 | #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)]) | ||
| 38 | #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1)) | ||
| 39 | |||
| 40 | 1040324 | static inline int16_t celt_cos(int16_t x) | |
| 41 | { | ||
| 42 | 1040324 | x = (MUL16(x, x) + 4096) >> 13; | |
| 43 | 1040324 | x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x))))); | |
| 44 | 1040324 | return x + 1; | |
| 45 | } | ||
| 46 | |||
| 47 | 520162 | static inline int celt_log2tan(int isin, int icos) | |
| 48 | { | ||
| 49 | int lc, ls; | ||
| 50 | 520162 | lc = opus_ilog(icos); | |
| 51 | 520162 | ls = opus_ilog(isin); | |
| 52 | 520162 | icos <<= 15 - lc; | |
| 53 | 520162 | isin <<= 15 - ls; | |
| 54 | 520162 | return (ls << 11) - (lc << 11) + | |
| 55 | 1040324 | ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) - | |
| 56 | 520162 | ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932); | |
| 57 | } | ||
| 58 | |||
| 59 | 1009098 | static inline int celt_bits2pulses(const uint8_t *cache, int bits) | |
| 60 | { | ||
| 61 | // TODO: Find the size of cache and make it into an array in the parameters list | ||
| 62 | 1009098 | int i, low = 0, high; | |
| 63 | |||
| 64 | 1009098 | high = cache[0]; | |
| 65 | 1009098 | bits--; | |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 6054588 times.
✓ Branch 1 taken 1009098 times.
|
7063686 | for (i = 0; i < 6; i++) { |
| 68 | 6054588 | int center = (low + high + 1) >> 1; | |
| 69 |
2/2✓ Branch 0 taken 3429432 times.
✓ Branch 1 taken 2625156 times.
|
6054588 | if (cache[center] >= bits) |
| 70 | 3429432 | high = center; | |
| 71 | else | ||
| 72 | 2625156 | low = center; | |
| 73 | } | ||
| 74 | |||
| 75 |
4/4✓ Branch 0 taken 862256 times.
✓ Branch 1 taken 146842 times.
✓ Branch 2 taken 409372 times.
✓ Branch 3 taken 599726 times.
|
1009098 | return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high; |
| 76 | } | ||
| 77 | |||
| 78 | 1020863 | static inline int celt_pulses2bits(const uint8_t *cache, int pulses) | |
| 79 | { | ||
| 80 | // TODO: Find the size of cache and make it into an array in the parameters list | ||
| 81 |
2/2✓ Branch 0 taken 881533 times.
✓ Branch 1 taken 139330 times.
|
1020863 | return (pulses == 0) ? 0 : cache[pulses] + 1; |
| 82 | } | ||
| 83 | |||
| 84 | 869768 | static inline void celt_normalize_residual(const int * restrict iy, float * restrict X, | |
| 85 | int N, float g) | ||
| 86 | { | ||
| 87 | int i; | ||
| 88 |
2/2✓ Branch 0 taken 7358248 times.
✓ Branch 1 taken 869768 times.
|
8228016 | for (i = 0; i < N; i++) |
| 89 | 7358248 | X[i] = g * iy[i]; | |
| 90 | 869768 | } | |
| 91 | |||
| 92 | 376319 | static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride, | |
| 93 | float c, float s) | ||
| 94 | { | ||
| 95 | float *Xptr; | ||
| 96 | int i; | ||
| 97 | |||
| 98 | 376319 | Xptr = X; | |
| 99 |
2/2✓ Branch 0 taken 5071415 times.
✓ Branch 1 taken 376319 times.
|
5447734 | for (i = 0; i < len - stride; i++) { |
| 100 | 5071415 | float x1 = Xptr[0]; | |
| 101 | 5071415 | float x2 = Xptr[stride]; | |
| 102 | 5071415 | Xptr[stride] = c * x2 + s * x1; | |
| 103 | 5071415 | *Xptr++ = c * x1 - s * x2; | |
| 104 | } | ||
| 105 | |||
| 106 | 376319 | Xptr = &X[len - 2 * stride - 1]; | |
| 107 |
2/2✓ Branch 0 taken 4226210 times.
✓ Branch 1 taken 376319 times.
|
4602529 | for (i = len - 2 * stride - 1; i >= 0; i--) { |
| 108 | 4226210 | float x1 = Xptr[0]; | |
| 109 | 4226210 | float x2 = Xptr[stride]; | |
| 110 | 4226210 | Xptr[stride] = c * x2 + s * x1; | |
| 111 | 4226210 | *Xptr-- = c * x1 - s * x2; | |
| 112 | } | ||
| 113 | 376319 | } | |
| 114 | |||
| 115 | 869768 | static inline void celt_exp_rotation(float *X, uint32_t len, | |
| 116 | uint32_t stride, uint32_t K, | ||
| 117 | enum CeltSpread spread, const int encode) | ||
| 118 | { | ||
| 119 | 869768 | uint32_t stride2 = 0; | |
| 120 | float c, s; | ||
| 121 | float gain, theta; | ||
| 122 | int i; | ||
| 123 | |||
| 124 |
4/4✓ Branch 0 taken 169538 times.
✓ Branch 1 taken 700230 times.
✓ Branch 2 taken 5544 times.
✓ Branch 3 taken 163994 times.
|
869768 | if (2*K >= len || spread == CELT_SPREAD_NONE) |
| 125 | 705774 | return; | |
| 126 | |||
| 127 | 163994 | gain = (float)len / (len + (20 - 5*spread) * K); | |
| 128 | 163994 | theta = M_PI * gain * gain / 4; | |
| 129 | |||
| 130 | 163994 | c = cosf(theta); | |
| 131 | 163994 | s = sinf(theta); | |
| 132 | |||
| 133 |
2/2✓ Branch 0 taken 139484 times.
✓ Branch 1 taken 24510 times.
|
163994 | if (len >= stride << 3) { |
| 134 | 139484 | stride2 = 1; | |
| 135 | /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding. | ||
| 136 | It's basically incrementing long as (stride2+0.5)^2 < len/stride. */ | ||
| 137 |
2/2✓ Branch 0 taken 452895 times.
✓ Branch 1 taken 139484 times.
|
592379 | while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len) |
| 138 | 452895 | stride2++; | |
| 139 | } | ||
| 140 | |||
| 141 | 163994 | len /= stride; | |
| 142 |
2/2✓ Branch 0 taken 221835 times.
✓ Branch 1 taken 163994 times.
|
385829 | for (i = 0; i < stride; i++) { |
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221835 times.
|
221835 | if (encode) { |
| 144 | ✗ | celt_exp_rotation_impl(X + i * len, len, 1, c, -s); | |
| 145 | ✗ | if (stride2) | |
| 146 | ✗ | celt_exp_rotation_impl(X + i * len, len, stride2, s, -c); | |
| 147 | } else { | ||
| 148 |
2/2✓ Branch 0 taken 154484 times.
✓ Branch 1 taken 67351 times.
|
221835 | if (stride2) |
| 149 | 154484 | celt_exp_rotation_impl(X + i * len, len, stride2, s, c); | |
| 150 | 221835 | celt_exp_rotation_impl(X + i * len, len, 1, c, s); | |
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | 869768 | static inline uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B) | |
| 156 | { | ||
| 157 | 869768 | int i, j, N0 = N / B; | |
| 158 | 869768 | uint32_t collapse_mask = 0; | |
| 159 | |||
| 160 |
2/2✓ Branch 0 taken 742652 times.
✓ Branch 1 taken 127116 times.
|
869768 | if (B <= 1) |
| 161 | 742652 | return 1; | |
| 162 | |||
| 163 |
2/2✓ Branch 0 taken 409398 times.
✓ Branch 1 taken 127116 times.
|
536514 | for (i = 0; i < B; i++) |
| 164 |
2/2✓ Branch 0 taken 1120520 times.
✓ Branch 1 taken 409398 times.
|
1529918 | for (j = 0; j < N0; j++) |
| 165 | 1120520 | collapse_mask |= (!!iy[i*N0+j]) << i; | |
| 166 | 127116 | return collapse_mask; | |
| 167 | } | ||
| 168 | |||
| 169 | 186425 | static inline void celt_stereo_merge(float *X, float *Y, float mid, int N) | |
| 170 | { | ||
| 171 | int i; | ||
| 172 | 186425 | float xp = 0, side = 0; | |
| 173 | float E[2]; | ||
| 174 | float mid2; | ||
| 175 | float gain[2]; | ||
| 176 | |||
| 177 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ | ||
| 178 |
2/2✓ Branch 0 taken 4299676 times.
✓ Branch 1 taken 186425 times.
|
4486101 | for (i = 0; i < N; i++) { |
| 179 | 4299676 | xp += X[i] * Y[i]; | |
| 180 | 4299676 | side += Y[i] * Y[i]; | |
| 181 | } | ||
| 182 | |||
| 183 | /* Compensating for the mid normalization */ | ||
| 184 | 186425 | xp *= mid; | |
| 185 | 186425 | mid2 = mid; | |
| 186 | 186425 | E[0] = mid2 * mid2 + side - 2 * xp; | |
| 187 | 186425 | E[1] = mid2 * mid2 + side + 2 * xp; | |
| 188 |
4/4✓ Branch 0 taken 186353 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 186269 times.
|
186425 | if (E[0] < 6e-4f || E[1] < 6e-4f) { |
| 189 |
2/2✓ Branch 0 taken 998 times.
✓ Branch 1 taken 156 times.
|
1154 | for (i = 0; i < N; i++) |
| 190 | 998 | Y[i] = X[i]; | |
| 191 | 156 | return; | |
| 192 | } | ||
| 193 | |||
| 194 | 186269 | gain[0] = 1.0f / sqrtf(E[0]); | |
| 195 | 186269 | gain[1] = 1.0f / sqrtf(E[1]); | |
| 196 | |||
| 197 |
2/2✓ Branch 0 taken 4298678 times.
✓ Branch 1 taken 186269 times.
|
4484947 | for (i = 0; i < N; i++) { |
| 198 | float value[2]; | ||
| 199 | /* Apply mid scaling (side is already scaled) */ | ||
| 200 | 4298678 | value[0] = mid * X[i]; | |
| 201 | 4298678 | value[1] = Y[i]; | |
| 202 | 4298678 | X[i] = gain[0] * (value[0] - value[1]); | |
| 203 | 4298678 | Y[i] = gain[1] * (value[0] + value[1]); | |
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | 140557 | static void celt_interleave_hadamard(float *tmp, float *X, int N0, | |
| 208 | int stride, int hadamard) | ||
| 209 | { | ||
| 210 | 140557 | int i, j, N = N0*stride; | |
| 211 |
2/2✓ Branch 0 taken 76200 times.
✓ Branch 1 taken 64357 times.
|
140557 | const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30]; |
| 212 | |||
| 213 |
2/2✓ Branch 0 taken 748698 times.
✓ Branch 1 taken 140557 times.
|
889255 | for (i = 0; i < stride; i++) |
| 214 |
2/2✓ Branch 0 taken 3669104 times.
✓ Branch 1 taken 748698 times.
|
4417802 | for (j = 0; j < N0; j++) |
| 215 | 3669104 | tmp[j*stride+i] = X[order[i]*N0+j]; | |
| 216 | |||
| 217 | 140557 | memcpy(X, tmp, N*sizeof(float)); | |
| 218 | 140557 | } | |
| 219 | |||
| 220 | 91168 | static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, | |
| 221 | int stride, int hadamard) | ||
| 222 | { | ||
| 223 | 91168 | int i, j, N = N0*stride; | |
| 224 |
2/2✓ Branch 0 taken 50950 times.
✓ Branch 1 taken 40218 times.
|
91168 | const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30]; |
| 225 | |||
| 226 |
2/2✓ Branch 0 taken 468648 times.
✓ Branch 1 taken 91168 times.
|
559816 | for (i = 0; i < stride; i++) |
| 227 |
2/2✓ Branch 0 taken 2290306 times.
✓ Branch 1 taken 468648 times.
|
2758954 | for (j = 0; j < N0; j++) |
| 228 | 2290306 | tmp[order[i]*N0+j] = X[j*stride+i]; | |
| 229 | |||
| 230 | 91168 | memcpy(X, tmp, N*sizeof(float)); | |
| 231 | 91168 | } | |
| 232 | |||
| 233 | 357319 | static void celt_haar1(float *X, int N0, int stride) | |
| 234 | { | ||
| 235 | int i, j; | ||
| 236 | 357319 | N0 >>= 1; | |
| 237 |
2/2✓ Branch 0 taken 677571 times.
✓ Branch 1 taken 357319 times.
|
1034890 | for (i = 0; i < stride; i++) { |
| 238 |
2/2✓ Branch 0 taken 5137144 times.
✓ Branch 1 taken 677571 times.
|
5814715 | for (j = 0; j < N0; j++) { |
| 239 | 5137144 | float x0 = X[stride * (2 * j + 0) + i]; | |
| 240 | 5137144 | float x1 = X[stride * (2 * j + 1) + i]; | |
| 241 | 5137144 | X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2; | |
| 242 | 5137144 | X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2; | |
| 243 | } | ||
| 244 | } | ||
| 245 | 357319 | } | |
| 246 | |||
| 247 | 576688 | static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap, | |
| 248 | int stereo) | ||
| 249 | { | ||
| 250 | int qn, qb; | ||
| 251 | 576688 | int N2 = 2 * N - 1; | |
| 252 |
4/4✓ Branch 0 taken 188735 times.
✓ Branch 1 taken 387953 times.
✓ Branch 2 taken 48661 times.
✓ Branch 3 taken 140074 times.
|
576688 | if (stereo && N == 2) |
| 253 | 48661 | N2--; | |
| 254 | |||
| 255 | /* The upper limit ensures that in a stereo split with itheta==16384, we'll | ||
| 256 | * always have enough bits left over to code at least one pulse in the | ||
| 257 | * side; otherwise it would collapse, since it doesn't get folded. */ | ||
| 258 | 576688 | qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3); | |
| 259 |
2/2✓ Branch 0 taken 574973 times.
✓ Branch 1 taken 1715 times.
|
576688 | qn = (qb < (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1; |
| 260 | 576688 | return qn; | |
| 261 | } | ||
| 262 | |||
| 263 | /* Convert the quantized vector to an index */ | ||
| 264 | ✗ | static inline uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y) | |
| 265 | { | ||
| 266 | ✗ | int i, idx = 0, sum = 0; | |
| 267 | ✗ | for (i = N - 1; i >= 0; i--) { | |
| 268 | ✗ | const uint32_t i_s = CELT_PVQ_U(N - i, sum + FFABS(y[i]) + 1); | |
| 269 | ✗ | idx += CELT_PVQ_U(N - i, sum) + (y[i] < 0)*i_s; | |
| 270 | ✗ | sum += FFABS(y[i]); | |
| 271 | } | ||
| 272 | ✗ | return idx; | |
| 273 | } | ||
| 274 | |||
| 275 | // this code was adapted from libopus | ||
| 276 | 869768 | static inline uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y) | |
| 277 | { | ||
| 278 | 869768 | uint64_t norm = 0; | |
| 279 | uint32_t q, p; | ||
| 280 | int s, val; | ||
| 281 | int k0; | ||
| 282 | |||
| 283 |
2/2✓ Branch 0 taken 5618712 times.
✓ Branch 1 taken 869768 times.
|
6488480 | while (N > 2) { |
| 284 | /*Lots of pulses case:*/ | ||
| 285 |
2/2✓ Branch 0 taken 1670295 times.
✓ Branch 1 taken 3948417 times.
|
5618712 | if (K >= N) { |
| 286 | 1670295 | const uint32_t *row = ff_celt_pvq_u_row[N]; | |
| 287 | |||
| 288 | /* Are the pulses in this dimension negative? */ | ||
| 289 | 1670295 | p = row[K + 1]; | |
| 290 | 1670295 | s = -(i >= p); | |
| 291 | 1670295 | i -= p & s; | |
| 292 | |||
| 293 | /*Count how many pulses were placed in this dimension.*/ | ||
| 294 | 1670295 | k0 = K; | |
| 295 | 1670295 | q = row[N]; | |
| 296 |
2/2✓ Branch 0 taken 269437 times.
✓ Branch 1 taken 1400858 times.
|
1670295 | if (q > i) { |
| 297 | 269437 | K = N; | |
| 298 | do { | ||
| 299 | 388229 | p = ff_celt_pvq_u_row[--K][N]; | |
| 300 |
2/2✓ Branch 0 taken 118792 times.
✓ Branch 1 taken 269437 times.
|
388229 | } while (p > i); |
| 301 | } else | ||
| 302 |
2/2✓ Branch 0 taken 7364456 times.
✓ Branch 1 taken 1400858 times.
|
8765314 | for (p = row[K]; p > i; p = row[K]) |
| 303 | 7364456 | K--; | |
| 304 | |||
| 305 | 1670295 | i -= p; | |
| 306 | 1670295 | val = (k0 - K + s) ^ s; | |
| 307 | 1670295 | norm += val * val; | |
| 308 | 1670295 | *y++ = val; | |
| 309 | } else { /*Lots of dimensions case:*/ | ||
| 310 | /*Are there any pulses in this dimension at all?*/ | ||
| 311 | 3948417 | p = ff_celt_pvq_u_row[K ][N]; | |
| 312 | 3948417 | q = ff_celt_pvq_u_row[K + 1][N]; | |
| 313 | |||
| 314 |
4/4✓ Branch 0 taken 3395659 times.
✓ Branch 1 taken 552758 times.
✓ Branch 2 taken 2842454 times.
✓ Branch 3 taken 553205 times.
|
3948417 | if (p <= i && i < q) { |
| 315 | 2842454 | i -= p; | |
| 316 | 2842454 | *y++ = 0; | |
| 317 | } else { | ||
| 318 | /*Are the pulses in this dimension negative?*/ | ||
| 319 | 1105963 | s = -(i >= q); | |
| 320 | 1105963 | i -= q & s; | |
| 321 | |||
| 322 | /*Count how many pulses were placed in this dimension.*/ | ||
| 323 | 1105963 | k0 = K; | |
| 324 | 1287869 | do p = ff_celt_pvq_u_row[--K][N]; | |
| 325 |
2/2✓ Branch 0 taken 181906 times.
✓ Branch 1 taken 1105963 times.
|
1287869 | while (p > i); |
| 326 | |||
| 327 | 1105963 | i -= p; | |
| 328 | 1105963 | val = (k0 - K + s) ^ s; | |
| 329 | 1105963 | norm += val * val; | |
| 330 | 1105963 | *y++ = val; | |
| 331 | } | ||
| 332 | } | ||
| 333 | 5618712 | N--; | |
| 334 | } | ||
| 335 | |||
| 336 | /* N == 2 */ | ||
| 337 | 869768 | p = 2 * K + 1; | |
| 338 | 869768 | s = -(i >= p); | |
| 339 | 869768 | i -= p & s; | |
| 340 | 869768 | k0 = K; | |
| 341 | 869768 | K = (i + 1) / 2; | |
| 342 | |||
| 343 |
2/2✓ Branch 0 taken 613142 times.
✓ Branch 1 taken 256626 times.
|
869768 | if (K) |
| 344 | 613142 | i -= 2 * K - 1; | |
| 345 | |||
| 346 | 869768 | val = (k0 - K + s) ^ s; | |
| 347 | 869768 | norm += val * val; | |
| 348 | 869768 | *y++ = val; | |
| 349 | |||
| 350 | /* N==1 */ | ||
| 351 | 869768 | s = -i; | |
| 352 | 869768 | val = (K + s) ^ s; | |
| 353 | 869768 | norm += val * val; | |
| 354 | 869768 | *y = val; | |
| 355 | |||
| 356 | 869768 | return norm; | |
| 357 | } | ||
| 358 | |||
| 359 | ✗ | static inline void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K) | |
| 360 | { | ||
| 361 | ✗ | ff_opus_rc_enc_uint(rc, celt_icwrsi(N, K, y), CELT_PVQ_V(N, K)); | |
| 362 | ✗ | } | |
| 363 | |||
| 364 | 869768 | static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K) | |
| 365 | { | ||
| 366 |
4/4✓ Branch 0 taken 313899 times.
✓ Branch 1 taken 555869 times.
✓ Branch 2 taken 281720 times.
✓ Branch 3 taken 588048 times.
|
869768 | const uint32_t idx = ff_opus_rc_dec_uint(rc, CELT_PVQ_V(N, K)); |
| 367 | 869768 | return celt_cwrsi(N, K, idx, y); | |
| 368 | } | ||
| 369 | |||
| 370 | #if CONFIG_OPUS_ENCODER | ||
| 371 | /* | ||
| 372 | * Faster than libopus's search, operates entirely in the signed domain. | ||
| 373 | * Slightly worse/better depending on N, K and the input vector. | ||
| 374 | */ | ||
| 375 | ✗ | static float ppp_pvq_search_c(float *X, int *y, int K, int N) | |
| 376 | { | ||
| 377 | ✗ | int i, y_norm = 0; | |
| 378 | ✗ | float res = 0.0f, xy_norm = 0.0f; | |
| 379 | |||
| 380 | ✗ | for (i = 0; i < N; i++) | |
| 381 | ✗ | res += FFABS(X[i]); | |
| 382 | |||
| 383 | ✗ | res = K/(res + FLT_EPSILON); | |
| 384 | |||
| 385 | ✗ | for (i = 0; i < N; i++) { | |
| 386 | ✗ | y[i] = lrintf(res*X[i]); | |
| 387 | ✗ | y_norm += y[i]*y[i]; | |
| 388 | ✗ | xy_norm += y[i]*X[i]; | |
| 389 | ✗ | K -= FFABS(y[i]); | |
| 390 | } | ||
| 391 | |||
| 392 | ✗ | while (K) { | |
| 393 | ✗ | int max_idx = 0, phase = FFSIGN(K); | |
| 394 | ✗ | float max_num = 0.0f; | |
| 395 | ✗ | float max_den = 1.0f; | |
| 396 | ✗ | y_norm += 1.0f; | |
| 397 | |||
| 398 | ✗ | for (i = 0; i < N; i++) { | |
| 399 | /* If the sum has been overshot and the best place has 0 pulses allocated | ||
| 400 | * to it, attempting to decrease it further will actually increase the | ||
| 401 | * sum. Prevent this by disregarding any 0 positions when decrementing. */ | ||
| 402 | ✗ | const int ca = 1 ^ ((y[i] == 0) & (phase < 0)); | |
| 403 | ✗ | const int y_new = y_norm + 2*phase*FFABS(y[i]); | |
| 404 | ✗ | float xy_new = xy_norm + 1*phase*FFABS(X[i]); | |
| 405 | ✗ | xy_new = xy_new * xy_new; | |
| 406 | ✗ | if (ca && (max_den*xy_new) > (y_new*max_num)) { | |
| 407 | ✗ | max_den = y_new; | |
| 408 | ✗ | max_num = xy_new; | |
| 409 | ✗ | max_idx = i; | |
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | ✗ | K -= phase; | |
| 414 | |||
| 415 | ✗ | phase *= FFSIGN(X[max_idx]); | |
| 416 | ✗ | xy_norm += 1*phase*X[max_idx]; | |
| 417 | ✗ | y_norm += 2*phase*y[max_idx]; | |
| 418 | ✗ | y[max_idx] += phase; | |
| 419 | } | ||
| 420 | |||
| 421 | ✗ | return (float)y_norm; | |
| 422 | } | ||
| 423 | #endif | ||
| 424 | |||
| 425 | ✗ | static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, | |
| 426 | enum CeltSpread spread, uint32_t blocks, float gain, | ||
| 427 | CeltPVQ *pvq) | ||
| 428 | { | ||
| 429 | ✗ | int *y = pvq->qcoeff; | |
| 430 | |||
| 431 | ✗ | celt_exp_rotation(X, N, blocks, K, spread, 1); | |
| 432 | ✗ | gain /= sqrtf(pvq->pvq_search(X, y, K, N)); | |
| 433 | ✗ | celt_encode_pulses(rc, y, N, K); | |
| 434 | ✗ | celt_normalize_residual(y, X, N, gain); | |
| 435 | ✗ | celt_exp_rotation(X, N, blocks, K, spread, 0); | |
| 436 | ✗ | return celt_extract_collapse_mask(y, N, blocks); | |
| 437 | } | ||
| 438 | |||
| 439 | /** Decode pulse vector and combine the result with the pitch vector to produce | ||
| 440 | the final normalised signal in the current band. */ | ||
| 441 | 869768 | static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, | |
| 442 | enum CeltSpread spread, uint32_t blocks, float gain, | ||
| 443 | CeltPVQ *pvq) | ||
| 444 | { | ||
| 445 | 869768 | int *y = pvq->qcoeff; | |
| 446 | |||
| 447 | 869768 | gain /= sqrtf(celt_decode_pulses(rc, y, N, K)); | |
| 448 | 869768 | celt_normalize_residual(y, X, N, gain); | |
| 449 | 869768 | celt_exp_rotation(X, N, blocks, K, spread, 0); | |
| 450 | 869768 | return celt_extract_collapse_mask(y, N, blocks); | |
| 451 | } | ||
| 452 | |||
| 453 | ✗ | static int celt_calc_theta(const float *X, const float *Y, int coupling, int N) | |
| 454 | { | ||
| 455 | int i; | ||
| 456 | ✗ | float e[2] = { 0.0f, 0.0f }; | |
| 457 | ✗ | if (coupling) { /* Coupling case */ | |
| 458 | ✗ | for (i = 0; i < N; i++) { | |
| 459 | ✗ | e[0] += (X[i] + Y[i])*(X[i] + Y[i]); | |
| 460 | ✗ | e[1] += (X[i] - Y[i])*(X[i] - Y[i]); | |
| 461 | } | ||
| 462 | } else { | ||
| 463 | ✗ | for (i = 0; i < N; i++) { | |
| 464 | ✗ | e[0] += X[i]*X[i]; | |
| 465 | ✗ | e[1] += Y[i]*Y[i]; | |
| 466 | } | ||
| 467 | } | ||
| 468 | ✗ | return lrintf(32768.0f*atan2f(sqrtf(e[1]), sqrtf(e[0]))/M_PI); | |
| 469 | } | ||
| 470 | |||
| 471 | ✗ | static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N) | |
| 472 | { | ||
| 473 | int i; | ||
| 474 | ✗ | const float energy_n = 1.0f/(sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON); | |
| 475 | ✗ | e_l *= energy_n; | |
| 476 | ✗ | e_r *= energy_n; | |
| 477 | ✗ | for (i = 0; i < N; i++) | |
| 478 | ✗ | X[i] = e_l*X[i] + e_r*Y[i]; | |
| 479 | ✗ | } | |
| 480 | |||
| 481 | ✗ | static void celt_stereo_ms_decouple(float *X, float *Y, int N) | |
| 482 | { | ||
| 483 | int i; | ||
| 484 | ✗ | for (i = 0; i < N; i++) { | |
| 485 | ✗ | const float Xret = X[i]; | |
| 486 | ✗ | X[i] = (X[i] + Y[i])*M_SQRT1_2; | |
| 487 | ✗ | Y[i] = (Y[i] - Xret)*M_SQRT1_2; | |
| 488 | } | ||
| 489 | ✗ | } | |
| 490 | |||
| 491 | 1747319 | static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |
| 492 | OpusRangeCoder *rc, | ||
| 493 | const int band, float *X, | ||
| 494 | float *Y, int N, int b, | ||
| 495 | uint32_t blocks, float *lowband, | ||
| 496 | int duration, float *lowband_out, | ||
| 497 | int level, float gain, | ||
| 498 | float *lowband_scratch, | ||
| 499 | int fill, int quant) | ||
| 500 | { | ||
| 501 | int i; | ||
| 502 | const uint8_t *cache; | ||
| 503 | 1747319 | int stereo = !!Y, split = stereo; | |
| 504 | 1747319 | int imid = 0, iside = 0; | |
| 505 | 1747319 | uint32_t N0 = N; | |
| 506 | 1747319 | int N_B = N / blocks; | |
| 507 | 1747319 | int N_B0 = N_B; | |
| 508 | 1747319 | int B0 = blocks; | |
| 509 | 1747319 | int time_divide = 0; | |
| 510 | 1747319 | int recombine = 0; | |
| 511 | 1747319 | int inv = 0; | |
| 512 | 1747319 | float mid = 0, side = 0; | |
| 513 | 1747319 | int longblocks = (B0 == 1); | |
| 514 | 1747319 | uint32_t cm = 0; | |
| 515 | |||
| 516 |
2/2✓ Branch 0 taken 109648 times.
✓ Branch 1 taken 1637671 times.
|
1747319 | if (N == 1) { |
| 517 | 109648 | float *x = X; | |
| 518 |
2/2✓ Branch 0 taken 176024 times.
✓ Branch 1 taken 109648 times.
|
285672 | for (i = 0; i <= stereo; i++) { |
| 519 | 176024 | int sign = 0; | |
| 520 |
2/2✓ Branch 0 taken 169213 times.
✓ Branch 1 taken 6811 times.
|
176024 | if (f->remaining2 >= 1 << 3) { |
| 521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 169213 times.
|
169213 | if (quant) { |
| 522 | ✗ | sign = x[0] < 0; | |
| 523 | ✗ | ff_opus_rc_put_raw(rc, sign, 1); | |
| 524 | } else { | ||
| 525 | 169213 | sign = ff_opus_rc_get_raw(rc, 1); | |
| 526 | } | ||
| 527 | 169213 | f->remaining2 -= 1 << 3; | |
| 528 | } | ||
| 529 | 176024 | x[0] = 1.0f - 2.0f*sign; | |
| 530 | 176024 | x = Y; | |
| 531 | } | ||
| 532 |
1/2✓ Branch 0 taken 109648 times.
✗ Branch 1 not taken.
|
109648 | if (lowband_out) |
| 533 | 109648 | lowband_out[0] = X[0]; | |
| 534 | 109648 | return 1; | |
| 535 | } | ||
| 536 | |||
| 537 |
4/4✓ Branch 0 taken 1397051 times.
✓ Branch 1 taken 240620 times.
✓ Branch 2 taken 621145 times.
✓ Branch 3 taken 775906 times.
|
1637671 | if (!stereo && level == 0) { |
| 538 | 621145 | int tf_change = f->tf_change[band]; | |
| 539 | int k; | ||
| 540 |
2/2✓ Branch 0 taken 40217 times.
✓ Branch 1 taken 580928 times.
|
621145 | if (tf_change > 0) |
| 541 | 40217 | recombine = tf_change; | |
| 542 | /* Band recombining to increase frequency resolution */ | ||
| 543 | |||
| 544 |
4/4✓ Branch 0 taken 371925 times.
✓ Branch 1 taken 249220 times.
✓ Branch 2 taken 346083 times.
✓ Branch 3 taken 25842 times.
|
621145 | if (lowband && |
| 545 |
6/6✓ Branch 0 taken 335115 times.
✓ Branch 1 taken 10968 times.
✓ Branch 2 taken 275970 times.
✓ Branch 3 taken 59145 times.
✓ Branch 4 taken 23636 times.
✓ Branch 5 taken 263302 times.
|
346083 | (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) { |
| 546 |
2/2✓ Branch 0 taken 2686826 times.
✓ Branch 1 taken 108623 times.
|
2795449 | for (i = 0; i < N; i++) |
| 547 | 2686826 | lowband_scratch[i] = lowband[i]; | |
| 548 | 108623 | lowband = lowband_scratch; | |
| 549 | } | ||
| 550 | |||
| 551 |
2/2✓ Branch 0 taken 67800 times.
✓ Branch 1 taken 621145 times.
|
688945 | for (k = 0; k < recombine; k++) { |
| 552 |
3/4✓ Branch 0 taken 67800 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42647 times.
✓ Branch 3 taken 25153 times.
|
67800 | if (quant || lowband) |
| 553 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42647 times.
|
42647 | celt_haar1(quant ? X : lowband, N >> k, 1 << k); |
| 554 | 67800 | fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2; | |
| 555 | } | ||
| 556 | 621145 | blocks >>= recombine; | |
| 557 | 621145 | N_B <<= recombine; | |
| 558 | |||
| 559 | /* Increasing the time resolution */ | ||
| 560 |
4/4✓ Branch 0 taken 720384 times.
✓ Branch 1 taken 50599 times.
✓ Branch 2 taken 149838 times.
✓ Branch 3 taken 570546 times.
|
770983 | while ((N_B & 1) == 0 && tf_change < 0) { |
| 561 |
3/4✓ Branch 0 taken 149838 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97034 times.
✓ Branch 3 taken 52804 times.
|
149838 | if (quant || lowband) |
| 562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 97034 times.
|
97034 | celt_haar1(quant ? X : lowband, N_B, blocks); |
| 563 | 149838 | fill |= fill << blocks; | |
| 564 | 149838 | blocks <<= 1; | |
| 565 | 149838 | N_B >>= 1; | |
| 566 | 149838 | time_divide++; | |
| 567 | 149838 | tf_change++; | |
| 568 | } | ||
| 569 | 621145 | B0 = blocks; | |
| 570 | 621145 | N_B0 = N_B; | |
| 571 | |||
| 572 | /* Reorganize the samples in time order instead of frequency order */ | ||
| 573 |
5/6✓ Branch 0 taken 140557 times.
✓ Branch 1 taken 480588 times.
✓ Branch 2 taken 140557 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 91168 times.
✓ Branch 5 taken 49389 times.
|
621145 | if (B0 > 1 && (quant || lowband)) |
| 574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 91168 times.
|
91168 | celt_deinterleave_hadamard(pvq->hadamard_tmp, quant ? X : lowband, |
| 575 | N_B >> recombine, B0 << recombine, | ||
| 576 | longblocks); | ||
| 577 | } | ||
| 578 | |||
| 579 | /* If we need 1.5 more bit than we can produce, split the band in two. */ | ||
| 580 | 1637671 | cache = ff_celt_cache_bits + | |
| 581 | 1637671 | ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band]; | |
| 582 |
8/8✓ Branch 0 taken 1397051 times.
✓ Branch 1 taken 240620 times.
✓ Branch 2 taken 1197035 times.
✓ Branch 3 taken 200016 times.
✓ Branch 4 taken 442447 times.
✓ Branch 5 taken 754588 times.
✓ Branch 6 taken 387953 times.
✓ Branch 7 taken 54494 times.
|
1637671 | if (!stereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) { |
| 583 | 387953 | N >>= 1; | |
| 584 | 387953 | Y = X + N; | |
| 585 | 387953 | split = 1; | |
| 586 | 387953 | duration -= 1; | |
| 587 |
2/2✓ Branch 0 taken 246169 times.
✓ Branch 1 taken 141784 times.
|
387953 | if (blocks == 1) |
| 588 | 246169 | fill = (fill & 1) | (fill << 1); | |
| 589 | 387953 | blocks = (blocks + 1) >> 1; | |
| 590 | } | ||
| 591 | |||
| 592 |
2/2✓ Branch 0 taken 628573 times.
✓ Branch 1 taken 1009098 times.
|
1637671 | if (split) { |
| 593 | int qn; | ||
| 594 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 628573 times.
|
628573 | int itheta = quant ? celt_calc_theta(X, Y, stereo, N) : 0; |
| 595 | int mbits, sbits, delta; | ||
| 596 | int qalloc; | ||
| 597 | int pulse_cap; | ||
| 598 | int offset; | ||
| 599 | int orig_fill; | ||
| 600 | int tell; | ||
| 601 | |||
| 602 | /* Decide on the resolution to give to the split parameter theta */ | ||
| 603 | 628573 | pulse_cap = ff_celt_log_freq_range[band] + duration * 8; | |
| 604 |
4/4✓ Branch 0 taken 240620 times.
✓ Branch 1 taken 387953 times.
✓ Branch 2 taken 54195 times.
✓ Branch 3 taken 186425 times.
|
628573 | offset = (pulse_cap >> 1) - (stereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE : |
| 605 | CELT_QTHETA_OFFSET); | ||
| 606 |
4/4✓ Branch 0 taken 240620 times.
✓ Branch 1 taken 387953 times.
✓ Branch 2 taken 188735 times.
✓ Branch 3 taken 51885 times.
|
628573 | qn = (stereo && band >= f->intensity_stereo) ? 1 : |
| 607 | 576688 | celt_compute_qn(N, b, offset, pulse_cap, stereo); | |
| 608 | 628573 | tell = opus_rc_tell_frac(rc); | |
| 609 |
2/2✓ Branch 0 taken 574973 times.
✓ Branch 1 taken 53600 times.
|
628573 | if (qn != 1) { |
| 610 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 574973 times.
|
574973 | if (quant) |
| 611 | ✗ | itheta = (itheta*qn + 8192) >> 14; | |
| 612 | /* Entropy coding of the angle. We use a uniform pdf for the | ||
| 613 | * time split, a step for stereo, and a triangular one for the rest. */ | ||
| 614 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 574973 times.
|
574973 | if (quant) { |
| 615 | ✗ | if (stereo && N > 2) | |
| 616 | ✗ | ff_opus_rc_enc_uint_step(rc, itheta, qn / 2); | |
| 617 | ✗ | else if (stereo || B0 > 1) | |
| 618 | ✗ | ff_opus_rc_enc_uint(rc, itheta, qn + 1); | |
| 619 | else | ||
| 620 | ✗ | ff_opus_rc_enc_uint_tri(rc, itheta, qn); | |
| 621 | ✗ | itheta = itheta * 16384 / qn; | |
| 622 | ✗ | if (stereo) { | |
| 623 | ✗ | if (itheta == 0) | |
| 624 | ✗ | celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band], | |
| 625 | f->block[1].lin_energy[band], N); | ||
| 626 | else | ||
| 627 | ✗ | celt_stereo_ms_decouple(X, Y, N); | |
| 628 | } | ||
| 629 | } else { | ||
| 630 |
4/4✓ Branch 0 taken 187020 times.
✓ Branch 1 taken 387953 times.
✓ Branch 2 taken 138861 times.
✓ Branch 3 taken 48159 times.
|
574973 | if (stereo && N > 2) |
| 631 | 138861 | itheta = ff_opus_rc_dec_uint_step(rc, qn / 2); | |
| 632 |
4/4✓ Branch 0 taken 387953 times.
✓ Branch 1 taken 48159 times.
✓ Branch 2 taken 141784 times.
✓ Branch 3 taken 246169 times.
|
436112 | else if (stereo || B0 > 1) |
| 633 | 189943 | itheta = ff_opus_rc_dec_uint(rc, qn+1); | |
| 634 | else | ||
| 635 | 246169 | itheta = ff_opus_rc_dec_uint_tri(rc, qn); | |
| 636 | 574973 | itheta = itheta * 16384 / qn; | |
| 637 | } | ||
| 638 |
1/2✓ Branch 0 taken 53600 times.
✗ Branch 1 not taken.
|
53600 | } else if (stereo) { |
| 639 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53600 times.
|
53600 | if (quant) { |
| 640 | ✗ | inv = f->apply_phase_inv ? itheta > 8192 : 0; | |
| 641 | ✗ | if (inv) { | |
| 642 | ✗ | for (i = 0; i < N; i++) | |
| 643 | ✗ | Y[i] *= -1; | |
| 644 | } | ||
| 645 | ✗ | celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band], | |
| 646 | f->block[1].lin_energy[band], N); | ||
| 647 | |||
| 648 | ✗ | if (b > 2 << 3 && f->remaining2 > 2 << 3) { | |
| 649 | ✗ | ff_opus_rc_enc_log(rc, inv, 2); | |
| 650 | } else { | ||
| 651 | ✗ | inv = 0; | |
| 652 | } | ||
| 653 | } else { | ||
| 654 |
3/4✓ Branch 0 taken 19635 times.
✓ Branch 1 taken 33965 times.
✓ Branch 2 taken 19635 times.
✗ Branch 3 not taken.
|
53600 | inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0; |
| 655 |
1/2✓ Branch 0 taken 53600 times.
✗ Branch 1 not taken.
|
53600 | inv = f->apply_phase_inv ? inv : 0; |
| 656 | } | ||
| 657 | 53600 | itheta = 0; | |
| 658 | } | ||
| 659 | 628573 | qalloc = opus_rc_tell_frac(rc) - tell; | |
| 660 | 628573 | b -= qalloc; | |
| 661 | |||
| 662 | 628573 | orig_fill = fill; | |
| 663 |
2/2✓ Branch 0 taken 106003 times.
✓ Branch 1 taken 522570 times.
|
628573 | if (itheta == 0) { |
| 664 | 106003 | imid = 32767; | |
| 665 | 106003 | iside = 0; | |
| 666 | 106003 | fill = av_zero_extend(fill, blocks); | |
| 667 | 106003 | delta = -16384; | |
| 668 |
2/2✓ Branch 0 taken 2408 times.
✓ Branch 1 taken 520162 times.
|
522570 | } else if (itheta == 16384) { |
| 669 | 2408 | imid = 0; | |
| 670 | 2408 | iside = 32767; | |
| 671 | 2408 | fill &= ((1 << blocks) - 1) << blocks; | |
| 672 | 2408 | delta = 16384; | |
| 673 | } else { | ||
| 674 | 520162 | imid = celt_cos(itheta); | |
| 675 | 520162 | iside = celt_cos(16384-itheta); | |
| 676 | /* This is the mid vs side allocation that minimizes squared error | ||
| 677 | in that band. */ | ||
| 678 | 520162 | delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid)); | |
| 679 | } | ||
| 680 | |||
| 681 | 628573 | mid = imid / 32768.0f; | |
| 682 | 628573 | side = iside / 32768.0f; | |
| 683 | |||
| 684 | /* This is a special case for N=2 that only works for stereo and takes | ||
| 685 | advantage of the fact that mid and side are orthogonal to encode | ||
| 686 | the side with just one bit. */ | ||
| 687 |
4/4✓ Branch 0 taken 104358 times.
✓ Branch 1 taken 524215 times.
✓ Branch 2 taken 54195 times.
✓ Branch 3 taken 50163 times.
|
628573 | if (N == 2 && stereo) { |
| 688 | int c; | ||
| 689 | 54195 | int sign = 0; | |
| 690 | float tmp; | ||
| 691 | float *x2, *y2; | ||
| 692 | 54195 | mbits = b; | |
| 693 | /* Only need one bit for the side */ | ||
| 694 |
4/4✓ Branch 0 taken 30179 times.
✓ Branch 1 taken 24016 times.
✓ Branch 2 taken 28840 times.
✓ Branch 3 taken 1339 times.
|
54195 | sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0; |
| 695 | 54195 | mbits -= sbits; | |
| 696 | 54195 | c = (itheta > 8192); | |
| 697 | 54195 | f->remaining2 -= qalloc+sbits; | |
| 698 | |||
| 699 |
2/2✓ Branch 0 taken 11376 times.
✓ Branch 1 taken 42819 times.
|
54195 | x2 = c ? Y : X; |
| 700 |
2/2✓ Branch 0 taken 11376 times.
✓ Branch 1 taken 42819 times.
|
54195 | y2 = c ? X : Y; |
| 701 |
2/2✓ Branch 0 taken 28840 times.
✓ Branch 1 taken 25355 times.
|
54195 | if (sbits) { |
| 702 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28840 times.
|
28840 | if (quant) { |
| 703 | ✗ | sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; | |
| 704 | ✗ | ff_opus_rc_put_raw(rc, sign, 1); | |
| 705 | } else { | ||
| 706 | 28840 | sign = ff_opus_rc_get_raw(rc, 1); | |
| 707 | } | ||
| 708 | } | ||
| 709 | 54195 | sign = 1 - 2 * sign; | |
| 710 | /* We use orig_fill here because we want to fold the side, but if | ||
| 711 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
| 712 | 54195 | cm = pvq->quant_band(pvq, f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration, | |
| 713 | lowband_out, level, gain, lowband_scratch, orig_fill); | ||
| 714 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
| 715 | and there's no need to worry about mixing with the other channel. */ | ||
| 716 | 54195 | y2[0] = -sign * x2[1]; | |
| 717 | 54195 | y2[1] = sign * x2[0]; | |
| 718 | 54195 | X[0] *= mid; | |
| 719 | 54195 | X[1] *= mid; | |
| 720 | 54195 | Y[0] *= side; | |
| 721 | 54195 | Y[1] *= side; | |
| 722 | 54195 | tmp = X[0]; | |
| 723 | 54195 | X[0] = tmp - Y[0]; | |
| 724 | 54195 | Y[0] = tmp + Y[0]; | |
| 725 | 54195 | tmp = X[1]; | |
| 726 | 54195 | X[1] = tmp - Y[1]; | |
| 727 | 54195 | Y[1] = tmp + Y[1]; | |
| 728 | } else { | ||
| 729 | /* "Normal" split code */ | ||
| 730 | 574378 | float *next_lowband2 = NULL; | |
| 731 | 574378 | float *next_lowband_out1 = NULL; | |
| 732 | 574378 | int next_level = 0; | |
| 733 | int rebalance; | ||
| 734 | uint32_t cmt; | ||
| 735 | |||
| 736 | /* Give more bits to low-energy MDCTs than they would | ||
| 737 | * otherwise deserve */ | ||
| 738 |
6/6✓ Branch 0 taken 170882 times.
✓ Branch 1 taken 403496 times.
✓ Branch 2 taken 141784 times.
✓ Branch 3 taken 29098 times.
✓ Branch 4 taken 140658 times.
✓ Branch 5 taken 1126 times.
|
574378 | if (B0 > 1 && !stereo && (itheta & 0x3fff)) { |
| 739 |
2/2✓ Branch 0 taken 61444 times.
✓ Branch 1 taken 79214 times.
|
140658 | if (itheta > 8192) |
| 740 | /* Rough approximation for pre-echo masking */ | ||
| 741 | 61444 | delta -= delta >> (4 - duration); | |
| 742 | else | ||
| 743 | /* Corresponds to a forward-masking slope of | ||
| 744 | * 1.5 dB per 10 ms */ | ||
| 745 | 79214 | delta = FFMIN(0, delta + (N << 3 >> (5 - duration))); | |
| 746 | } | ||
| 747 | 574378 | mbits = av_clip((b - delta) / 2, 0, b); | |
| 748 | 574378 | sbits = b - mbits; | |
| 749 | 574378 | f->remaining2 -= qalloc; | |
| 750 | |||
| 751 |
4/4✓ Branch 0 taken 453722 times.
✓ Branch 1 taken 120656 times.
✓ Branch 2 taken 293450 times.
✓ Branch 3 taken 160272 times.
|
574378 | if (lowband && !stereo) |
| 752 | 293450 | next_lowband2 = lowband + N; /* >32-bit split case */ | |
| 753 | |||
| 754 | /* Only stereo needs to pass on lowband_out. | ||
| 755 | * Otherwise, it's handled at the end */ | ||
| 756 |
2/2✓ Branch 0 taken 186425 times.
✓ Branch 1 taken 387953 times.
|
574378 | if (stereo) |
| 757 | 186425 | next_lowband_out1 = lowband_out; | |
| 758 | else | ||
| 759 | 387953 | next_level = level + 1; | |
| 760 | |||
| 761 | 574378 | rebalance = f->remaining2; | |
| 762 |
2/2✓ Branch 0 taken 352348 times.
✓ Branch 1 taken 222030 times.
|
574378 | if (mbits >= sbits) { |
| 763 | /* In stereo mode, we do not apply a scaling to the mid | ||
| 764 | * because we need the normalized mid for folding later */ | ||
| 765 |
2/2✓ Branch 0 taken 203023 times.
✓ Branch 1 taken 149325 times.
|
352348 | cm = pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks, |
| 766 | lowband, duration, next_lowband_out1, next_level, | ||
| 767 | stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | ||
| 768 | 352348 | rebalance = mbits - (rebalance - f->remaining2); | |
| 769 |
4/4✓ Branch 0 taken 95950 times.
✓ Branch 1 taken 256398 times.
✓ Branch 2 taken 86671 times.
✓ Branch 3 taken 9279 times.
|
352348 | if (rebalance > 3 << 3 && itheta != 0) |
| 770 | 86671 | sbits += rebalance - (3 << 3); | |
| 771 | |||
| 772 | /* For a stereo split, the high bits of fill are always zero, | ||
| 773 | * so no folding will be done to the side. */ | ||
| 774 | 352348 | cmt = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks, | |
| 775 | next_lowband2, duration, NULL, next_level, | ||
| 776 | gain * side, NULL, fill >> blocks); | ||
| 777 | 352348 | cm |= cmt << ((B0 >> 1) & (stereo - 1)); | |
| 778 | } else { | ||
| 779 | /* For a stereo split, the high bits of fill are always zero, | ||
| 780 | * so no folding will be done to the side. */ | ||
| 781 | 222030 | cm = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks, | |
| 782 | next_lowband2, duration, NULL, next_level, | ||
| 783 | gain * side, NULL, fill >> blocks); | ||
| 784 | 222030 | cm <<= ((B0 >> 1) & (stereo - 1)); | |
| 785 | 222030 | rebalance = sbits - (rebalance - f->remaining2); | |
| 786 |
4/4✓ Branch 0 taken 81426 times.
✓ Branch 1 taken 140604 times.
✓ Branch 2 taken 81157 times.
✓ Branch 3 taken 269 times.
|
222030 | if (rebalance > 3 << 3 && itheta != 16384) |
| 787 | 81157 | mbits += rebalance - (3 << 3); | |
| 788 | |||
| 789 | /* In stereo mode, we do not apply a scaling to the mid because | ||
| 790 | * we need the normalized mid for folding later */ | ||
| 791 |
2/2✓ Branch 0 taken 184930 times.
✓ Branch 1 taken 37100 times.
|
222030 | cm |= pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks, |
| 792 | lowband, duration, next_lowband_out1, next_level, | ||
| 793 | stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | ||
| 794 | } | ||
| 795 | } | ||
| 796 | } else { | ||
| 797 | /* This is the basic no-split case */ | ||
| 798 | 1009098 | uint32_t q = celt_bits2pulses(cache, b); | |
| 799 | 1009098 | uint32_t curr_bits = celt_pulses2bits(cache, q); | |
| 800 | 1009098 | f->remaining2 -= curr_bits; | |
| 801 | |||
| 802 | /* Ensures we can never bust the budget */ | ||
| 803 |
4/4✓ Branch 0 taken 15378 times.
✓ Branch 1 taken 1005485 times.
✓ Branch 2 taken 11765 times.
✓ Branch 3 taken 3613 times.
|
1020863 | while (f->remaining2 < 0 && q > 0) { |
| 804 | 11765 | f->remaining2 += curr_bits; | |
| 805 | 11765 | curr_bits = celt_pulses2bits(cache, --q); | |
| 806 | 11765 | f->remaining2 -= curr_bits; | |
| 807 | } | ||
| 808 | |||
| 809 |
2/2✓ Branch 0 taken 869768 times.
✓ Branch 1 taken 139330 times.
|
1009098 | if (q != 0) { |
| 810 | /* Finally do the actual (de)quantization */ | ||
| 811 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 869768 times.
|
869768 | if (quant) { |
| 812 | ✗ | cm = celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), | |
| 813 | f->spread, blocks, gain, pvq); | ||
| 814 | } else { | ||
| 815 |
2/2✓ Branch 0 taken 559059 times.
✓ Branch 1 taken 310709 times.
|
869768 | cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), |
| 816 | f->spread, blocks, gain, pvq); | ||
| 817 | } | ||
| 818 | } else { | ||
| 819 | /* If there's no pulse, fill the band anyway */ | ||
| 820 | 139330 | uint32_t cm_mask = (1 << blocks) - 1; | |
| 821 | 139330 | fill &= cm_mask; | |
| 822 |
2/2✓ Branch 0 taken 54433 times.
✓ Branch 1 taken 84897 times.
|
139330 | if (fill) { |
| 823 |
2/2✓ Branch 0 taken 9207 times.
✓ Branch 1 taken 45226 times.
|
54433 | if (!lowband) { |
| 824 | /* Noise */ | ||
| 825 |
2/2✓ Branch 0 taken 212568 times.
✓ Branch 1 taken 9207 times.
|
221775 | for (i = 0; i < N; i++) |
| 826 | 212568 | X[i] = (((int32_t)celt_rng(f)) >> 20); | |
| 827 | 9207 | cm = cm_mask; | |
| 828 | } else { | ||
| 829 | /* Folded spectrum */ | ||
| 830 |
2/2✓ Branch 0 taken 1783098 times.
✓ Branch 1 taken 45226 times.
|
1828324 | for (i = 0; i < N; i++) { |
| 831 | /* About 48 dB below the "normal" folding level */ | ||
| 832 |
2/2✓ Branch 1 taken 892913 times.
✓ Branch 2 taken 890185 times.
|
1783098 | X[i] = lowband[i] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256); |
| 833 | } | ||
| 834 | 45226 | cm = fill; | |
| 835 | } | ||
| 836 | 54433 | celt_renormalize_vector(X, N, gain); | |
| 837 | } else { | ||
| 838 | 84897 | memset(X, 0, N*sizeof(float)); | |
| 839 | } | ||
| 840 | } | ||
| 841 | } | ||
| 842 | |||
| 843 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | ||
| 844 |
2/2✓ Branch 0 taken 240620 times.
✓ Branch 1 taken 1397051 times.
|
1637671 | if (stereo) { |
| 845 |
2/2✓ Branch 0 taken 186425 times.
✓ Branch 1 taken 54195 times.
|
240620 | if (N > 2) |
| 846 | 186425 | celt_stereo_merge(X, Y, mid, N); | |
| 847 |
2/2✓ Branch 0 taken 6978 times.
✓ Branch 1 taken 233642 times.
|
240620 | if (inv) { |
| 848 |
2/2✓ Branch 0 taken 391242 times.
✓ Branch 1 taken 6978 times.
|
398220 | for (i = 0; i < N; i++) |
| 849 | 391242 | Y[i] *= -1; | |
| 850 | } | ||
| 851 |
2/2✓ Branch 0 taken 621145 times.
✓ Branch 1 taken 775906 times.
|
1397051 | } else if (level == 0) { |
| 852 | int k; | ||
| 853 | |||
| 854 | /* Undo the sample reorganization going from time order to frequency order */ | ||
| 855 |
2/2✓ Branch 0 taken 140557 times.
✓ Branch 1 taken 480588 times.
|
621145 | if (B0 > 1) |
| 856 | 140557 | celt_interleave_hadamard(pvq->hadamard_tmp, X, N_B >> recombine, | |
| 857 | B0 << recombine, longblocks); | ||
| 858 | |||
| 859 | /* Undo time-freq changes that we did earlier */ | ||
| 860 | 621145 | N_B = N_B0; | |
| 861 | 621145 | blocks = B0; | |
| 862 |
2/2✓ Branch 0 taken 149838 times.
✓ Branch 1 taken 621145 times.
|
770983 | for (k = 0; k < time_divide; k++) { |
| 863 | 149838 | blocks >>= 1; | |
| 864 | 149838 | N_B <<= 1; | |
| 865 | 149838 | cm |= cm >> blocks; | |
| 866 | 149838 | celt_haar1(X, N_B, blocks); | |
| 867 | } | ||
| 868 | |||
| 869 |
2/2✓ Branch 0 taken 67800 times.
✓ Branch 1 taken 621145 times.
|
688945 | for (k = 0; k < recombine; k++) { |
| 870 | 67800 | cm = ff_celt_bit_deinterleave[cm]; | |
| 871 | 67800 | celt_haar1(X, N0>>k, 1<<k); | |
| 872 | } | ||
| 873 | 621145 | blocks <<= recombine; | |
| 874 | |||
| 875 | /* Scale output for later folding */ | ||
| 876 |
2/2✓ Branch 0 taken 434720 times.
✓ Branch 1 taken 186425 times.
|
621145 | if (lowband_out) { |
| 877 | 434720 | float n = sqrtf(N0); | |
| 878 |
2/2✓ Branch 0 taken 7789770 times.
✓ Branch 1 taken 434720 times.
|
8224490 | for (i = 0; i < N0; i++) |
| 879 | 7789770 | lowband_out[i] = n * X[i]; | |
| 880 | } | ||
| 881 | 621145 | cm = av_zero_extend(cm, blocks); | |
| 882 | } | ||
| 883 | |||
| 884 | 1637671 | return cm; | |
| 885 | } | ||
| 886 | |||
| 887 | 1747319 | static QUANT_FN(pvq_decode_band) | |
| 888 | { | ||
| 889 | #if CONFIG_OPUS_DECODER | ||
| 890 | 1747319 | return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration, | |
| 891 | lowband_out, level, gain, lowband_scratch, fill, 0); | ||
| 892 | #else | ||
| 893 | return 0; | ||
| 894 | #endif | ||
| 895 | } | ||
| 896 | |||
| 897 | ✗ | static QUANT_FN(pvq_encode_band) | |
| 898 | { | ||
| 899 | #if CONFIG_OPUS_ENCODER | ||
| 900 | ✗ | return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration, | |
| 901 | lowband_out, level, gain, lowband_scratch, fill, 1); | ||
| 902 | #else | ||
| 903 | return 0; | ||
| 904 | #endif | ||
| 905 | } | ||
| 906 | |||
| 907 | 104 | int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode) | |
| 908 | { | ||
| 909 | 104 | CeltPVQ *s = av_malloc(sizeof(CeltPVQ)); | |
| 910 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (!s) |
| 911 | ✗ | return AVERROR(ENOMEM); | |
| 912 | |||
| 913 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | s->quant_band = encode ? pvq_encode_band : pvq_decode_band; |
| 914 | |||
| 915 | #if CONFIG_OPUS_ENCODER | ||
| 916 | 104 | s->pvq_search = ppp_pvq_search_c; | |
| 917 | #if ARCH_X86 | ||
| 918 | 104 | ff_celt_pvq_init_x86(s); | |
| 919 | #endif | ||
| 920 | #endif | ||
| 921 | |||
| 922 | 104 | *pvq = s; | |
| 923 | |||
| 924 | 104 | return 0; | |
| 925 | } | ||
| 926 | |||
| 927 | 104 | void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq) | |
| 928 | { | ||
| 929 | 104 | av_freep(pvq); | |
| 930 | 104 | } | |
| 931 |