Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/opus_pvq.c |
Date: | 2022-07-06 18:02:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 398 | 497 | 80.1% |
Branches: | 262 | 346 | 75.7% |
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 "config_components.h" | ||
27 | |||
28 | #include "opustab.h" | ||
29 | #include "opus_pvq.h" | ||
30 | |||
31 | #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)]) | ||
32 | #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1)) | ||
33 | |||
34 | 1037770 | static inline int16_t celt_cos(int16_t x) | |
35 | { | ||
36 | 1037770 | x = (MUL16(x, x) + 4096) >> 13; | |
37 | 1037770 | x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x))))); | |
38 | 1037770 | return x + 1; | |
39 | } | ||
40 | |||
41 | 518885 | static inline int celt_log2tan(int isin, int icos) | |
42 | { | ||
43 | int lc, ls; | ||
44 | 518885 | lc = opus_ilog(icos); | |
45 | 518885 | ls = opus_ilog(isin); | |
46 | 518885 | icos <<= 15 - lc; | |
47 | 518885 | isin <<= 15 - ls; | |
48 | 518885 | return (ls << 11) - (lc << 11) + | |
49 | 1037770 | ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) - | |
50 | 518885 | ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932); | |
51 | } | ||
52 | |||
53 | 1006497 | static inline int celt_bits2pulses(const uint8_t *cache, int bits) | |
54 | { | ||
55 | // TODO: Find the size of cache and make it into an array in the parameters list | ||
56 | 1006497 | int i, low = 0, high; | |
57 | |||
58 | 1006497 | high = cache[0]; | |
59 | 1006497 | bits--; | |
60 | |||
61 |
2/2✓ Branch 0 taken 6038982 times.
✓ Branch 1 taken 1006497 times.
|
7045479 | for (i = 0; i < 6; i++) { |
62 | 6038982 | int center = (low + high + 1) >> 1; | |
63 |
2/2✓ Branch 0 taken 3419805 times.
✓ Branch 1 taken 2619177 times.
|
6038982 | if (cache[center] >= bits) |
64 | 3419805 | high = center; | |
65 | else | ||
66 | 2619177 | low = center; | |
67 | } | ||
68 | |||
69 |
4/4✓ Branch 0 taken 860429 times.
✓ Branch 1 taken 146068 times.
✓ Branch 2 taken 408004 times.
✓ Branch 3 taken 598493 times.
|
1006497 | return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high; |
70 | } | ||
71 | |||
72 | 1018229 | static inline int celt_pulses2bits(const uint8_t *cache, int pulses) | |
73 | { | ||
74 | // TODO: Find the size of cache and make it into an array in the parameters list | ||
75 |
2/2✓ Branch 0 taken 879674 times.
✓ Branch 1 taken 138555 times.
|
1018229 | return (pulses == 0) ? 0 : cache[pulses] + 1; |
76 | } | ||
77 | |||
78 | 867942 | static inline void celt_normalize_residual(const int * av_restrict iy, float * av_restrict X, | |
79 | int N, float g) | ||
80 | { | ||
81 | int i; | ||
82 |
2/2✓ Branch 0 taken 7338362 times.
✓ Branch 1 taken 867942 times.
|
8206304 | for (i = 0; i < N; i++) |
83 | 7338362 | X[i] = g * iy[i]; | |
84 | 867942 | } | |
85 | |||
86 | 374943 | static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride, | |
87 | float c, float s) | ||
88 | { | ||
89 | float *Xptr; | ||
90 | int i; | ||
91 | |||
92 | 374943 | Xptr = X; | |
93 |
2/2✓ Branch 0 taken 5053544 times.
✓ Branch 1 taken 374943 times.
|
5428487 | for (i = 0; i < len - stride; i++) { |
94 | 5053544 | float x1 = Xptr[0]; | |
95 | 5053544 | float x2 = Xptr[stride]; | |
96 | 5053544 | Xptr[stride] = c * x2 + s * x1; | |
97 | 5053544 | *Xptr++ = c * x1 - s * x2; | |
98 | } | ||
99 | |||
100 | 374943 | Xptr = &X[len - 2 * stride - 1]; | |
101 |
2/2✓ Branch 0 taken 4211554 times.
✓ Branch 1 taken 374943 times.
|
4586497 | for (i = len - 2 * stride - 1; i >= 0; i--) { |
102 | 4211554 | float x1 = Xptr[0]; | |
103 | 4211554 | float x2 = Xptr[stride]; | |
104 | 4211554 | Xptr[stride] = c * x2 + s * x1; | |
105 | 4211554 | *Xptr-- = c * x1 - s * x2; | |
106 | } | ||
107 | 374943 | } | |
108 | |||
109 | 867942 | static inline void celt_exp_rotation(float *X, uint32_t len, | |
110 | uint32_t stride, uint32_t K, | ||
111 | enum CeltSpread spread, const int encode) | ||
112 | { | ||
113 | 867942 | uint32_t stride2 = 0; | |
114 | float c, s; | ||
115 | float gain, theta; | ||
116 | int i; | ||
117 | |||
118 |
4/4✓ Branch 0 taken 169041 times.
✓ Branch 1 taken 698901 times.
✓ Branch 2 taken 5544 times.
✓ Branch 3 taken 163497 times.
|
867942 | if (2*K >= len || spread == CELT_SPREAD_NONE) |
119 | 704445 | return; | |
120 | |||
121 | 163497 | gain = (float)len / (len + (20 - 5*spread) * K); | |
122 | 163497 | theta = M_PI * gain * gain / 4; | |
123 | |||
124 | 163497 | c = cosf(theta); | |
125 | 163497 | s = sinf(theta); | |
126 | |||
127 |
2/2✓ Branch 0 taken 138987 times.
✓ Branch 1 taken 24510 times.
|
163497 | if (len >= stride << 3) { |
128 | 138987 | stride2 = 1; | |
129 | /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding. | ||
130 | It's basically incrementing long as (stride2+0.5)^2 < len/stride. */ | ||
131 |
2/2✓ Branch 0 taken 451438 times.
✓ Branch 1 taken 138987 times.
|
590425 | while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len) |
132 | 451438 | stride2++; | |
133 | } | ||
134 | |||
135 | 163497 | len /= stride; | |
136 |
2/2✓ Branch 0 taken 221147 times.
✓ Branch 1 taken 163497 times.
|
384644 | for (i = 0; i < stride; i++) { |
137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221147 times.
|
221147 | if (encode) { |
138 | ✗ | celt_exp_rotation_impl(X + i * len, len, 1, c, -s); | |
139 | ✗ | if (stride2) | |
140 | ✗ | celt_exp_rotation_impl(X + i * len, len, stride2, s, -c); | |
141 | } else { | ||
142 |
2/2✓ Branch 0 taken 153796 times.
✓ Branch 1 taken 67351 times.
|
221147 | if (stride2) |
143 | 153796 | celt_exp_rotation_impl(X + i * len, len, stride2, s, c); | |
144 | 221147 | celt_exp_rotation_impl(X + i * len, len, 1, c, s); | |
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | 867942 | static inline uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B) | |
150 | { | ||
151 | 867942 | int i, j, N0 = N / B; | |
152 | 867942 | uint32_t collapse_mask = 0; | |
153 | |||
154 |
2/2✓ Branch 0 taken 741536 times.
✓ Branch 1 taken 126406 times.
|
867942 | if (B <= 1) |
155 | 741536 | return 1; | |
156 | |||
157 |
2/2✓ Branch 0 taken 407024 times.
✓ Branch 1 taken 126406 times.
|
533430 | for (i = 0; i < B; i++) |
158 |
2/2✓ Branch 0 taken 1113128 times.
✓ Branch 1 taken 407024 times.
|
1520152 | for (j = 0; j < N0; j++) |
159 | 1113128 | collapse_mask |= (!!iy[i*N0+j]) << i; | |
160 | 126406 | return collapse_mask; | |
161 | } | ||
162 | |||
163 | 186191 | static inline void celt_stereo_merge(float *X, float *Y, float mid, int N) | |
164 | { | ||
165 | int i; | ||
166 | 186191 | float xp = 0, side = 0; | |
167 | float E[2]; | ||
168 | float mid2; | ||
169 | float gain[2]; | ||
170 | |||
171 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ | ||
172 |
2/2✓ Branch 0 taken 4285756 times.
✓ Branch 1 taken 186191 times.
|
4471947 | for (i = 0; i < N; i++) { |
173 | 4285756 | xp += X[i] * Y[i]; | |
174 | 4285756 | side += Y[i] * Y[i]; | |
175 | } | ||
176 | |||
177 | /* Compensating for the mid normalization */ | ||
178 | 186191 | xp *= mid; | |
179 | 186191 | mid2 = mid; | |
180 | 186191 | E[0] = mid2 * mid2 + side - 2 * xp; | |
181 | 186191 | E[1] = mid2 * mid2 + side + 2 * xp; | |
182 |
4/4✓ Branch 0 taken 186119 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 186035 times.
|
186191 | if (E[0] < 6e-4f || E[1] < 6e-4f) { |
183 |
2/2✓ Branch 0 taken 998 times.
✓ Branch 1 taken 156 times.
|
1154 | for (i = 0; i < N; i++) |
184 | 998 | Y[i] = X[i]; | |
185 | 156 | return; | |
186 | } | ||
187 | |||
188 | 186035 | gain[0] = 1.0f / sqrtf(E[0]); | |
189 | 186035 | gain[1] = 1.0f / sqrtf(E[1]); | |
190 | |||
191 |
2/2✓ Branch 0 taken 4284758 times.
✓ Branch 1 taken 186035 times.
|
4470793 | for (i = 0; i < N; i++) { |
192 | float value[2]; | ||
193 | /* Apply mid scaling (side is already scaled) */ | ||
194 | 4284758 | value[0] = mid * X[i]; | |
195 | 4284758 | value[1] = Y[i]; | |
196 | 4284758 | X[i] = gain[0] * (value[0] - value[1]); | |
197 | 4284758 | Y[i] = gain[1] * (value[0] + value[1]); | |
198 | } | ||
199 | } | ||
200 | |||
201 | 140127 | static void celt_interleave_hadamard(float *tmp, float *X, int N0, | |
202 | int stride, int hadamard) | ||
203 | { | ||
204 | 140127 | int i, j, N = N0*stride; | |
205 |
2/2✓ Branch 0 taken 76152 times.
✓ Branch 1 taken 63975 times.
|
140127 | const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30]; |
206 | |||
207 |
2/2✓ Branch 0 taken 744618 times.
✓ Branch 1 taken 140127 times.
|
884745 | for (i = 0; i < stride; i++) |
208 |
2/2✓ Branch 0 taken 3650288 times.
✓ Branch 1 taken 744618 times.
|
4394906 | for (j = 0; j < N0; j++) |
209 | 3650288 | tmp[j*stride+i] = X[order[i]*N0+j]; | |
210 | |||
211 | 140127 | memcpy(X, tmp, N*sizeof(float)); | |
212 | 140127 | } | |
213 | |||
214 | 90824 | static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, | |
215 | int stride, int hadamard) | ||
216 | { | ||
217 | 90824 | int i, j, N = N0*stride; | |
218 |
2/2✓ Branch 0 taken 50902 times.
✓ Branch 1 taken 39922 times.
|
90824 | const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30]; |
219 | |||
220 |
2/2✓ Branch 0 taken 465256 times.
✓ Branch 1 taken 90824 times.
|
556080 | for (i = 0; i < stride; i++) |
221 |
2/2✓ Branch 0 taken 2277490 times.
✓ Branch 1 taken 465256 times.
|
2742746 | for (j = 0; j < N0; j++) |
222 | 2277490 | tmp[order[i]*N0+j] = X[j*stride+i]; | |
223 | |||
224 | 90824 | memcpy(X, tmp, N*sizeof(float)); | |
225 | 90824 | } | |
226 | |||
227 | 356919 | static void celt_haar1(float *X, int N0, int stride) | |
228 | { | ||
229 | int i, j; | ||
230 | 356919 | N0 >>= 1; | |
231 |
2/2✓ Branch 0 taken 675619 times.
✓ Branch 1 taken 356919 times.
|
1032538 | for (i = 0; i < stride; i++) { |
232 |
2/2✓ Branch 0 taken 5130104 times.
✓ Branch 1 taken 675619 times.
|
5805723 | for (j = 0; j < N0; j++) { |
233 | 5130104 | float x0 = X[stride * (2 * j + 0) + i]; | |
234 | 5130104 | float x1 = X[stride * (2 * j + 1) + i]; | |
235 | 5130104 | X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2; | |
236 | 5130104 | X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2; | |
237 | } | ||
238 | } | ||
239 | 356919 | } | |
240 | |||
241 | 575131 | static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap, | |
242 | int stereo) | ||
243 | { | ||
244 | int qn, qb; | ||
245 | 575131 | int N2 = 2 * N - 1; | |
246 |
4/4✓ Branch 0 taken 188735 times.
✓ Branch 1 taken 386396 times.
✓ Branch 2 taken 48661 times.
✓ Branch 3 taken 140074 times.
|
575131 | if (stereo && N == 2) |
247 | 48661 | N2--; | |
248 | |||
249 | /* The upper limit ensures that in a stereo split with itheta==16384, we'll | ||
250 | * always have enough bits left over to code at least one pulse in the | ||
251 | * side; otherwise it would collapse, since it doesn't get folded. */ | ||
252 | 575131 | qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3); | |
253 |
2/2✓ Branch 0 taken 573416 times.
✓ Branch 1 taken 1715 times.
|
575131 | qn = (qb < (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1; |
254 | 575131 | return qn; | |
255 | } | ||
256 | |||
257 | /* Convert the quantized vector to an index */ | ||
258 | ✗ | static inline uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y) | |
259 | { | ||
260 | ✗ | int i, idx = 0, sum = 0; | |
261 | ✗ | for (i = N - 1; i >= 0; i--) { | |
262 | ✗ | const uint32_t i_s = CELT_PVQ_U(N - i, sum + FFABS(y[i]) + 1); | |
263 | ✗ | idx += CELT_PVQ_U(N - i, sum) + (y[i] < 0)*i_s; | |
264 | ✗ | sum += FFABS(y[i]); | |
265 | } | ||
266 | ✗ | return idx; | |
267 | } | ||
268 | |||
269 | // this code was adapted from libopus | ||
270 | 867942 | static inline uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y) | |
271 | { | ||
272 | 867942 | uint64_t norm = 0; | |
273 | uint32_t q, p; | ||
274 | int s, val; | ||
275 | int k0; | ||
276 | |||
277 |
2/2✓ Branch 0 taken 5602478 times.
✓ Branch 1 taken 867942 times.
|
6470420 | while (N > 2) { |
278 | /*Lots of pulses case:*/ | ||
279 |
2/2✓ Branch 0 taken 1666017 times.
✓ Branch 1 taken 3936461 times.
|
5602478 | if (K >= N) { |
280 | 1666017 | const uint32_t *row = ff_celt_pvq_u_row[N]; | |
281 | |||
282 | /* Are the pulses in this dimension negative? */ | ||
283 | 1666017 | p = row[K + 1]; | |
284 | 1666017 | s = -(i >= p); | |
285 | 1666017 | i -= p & s; | |
286 | |||
287 | /*Count how many pulses were placed in this dimension.*/ | ||
288 | 1666017 | k0 = K; | |
289 | 1666017 | q = row[N]; | |
290 |
2/2✓ Branch 0 taken 268919 times.
✓ Branch 1 taken 1397098 times.
|
1666017 | if (q > i) { |
291 | 268919 | K = N; | |
292 | do { | ||
293 | 387231 | p = ff_celt_pvq_u_row[--K][N]; | |
294 |
2/2✓ Branch 0 taken 118312 times.
✓ Branch 1 taken 268919 times.
|
387231 | } while (p > i); |
295 | } else | ||
296 |
2/2✓ Branch 0 taken 7345399 times.
✓ Branch 1 taken 1397098 times.
|
8742497 | for (p = row[K]; p > i; p = row[K]) |
297 | 7345399 | K--; | |
298 | |||
299 | 1666017 | i -= p; | |
300 | 1666017 | val = (k0 - K + s) ^ s; | |
301 | 1666017 | norm += val * val; | |
302 | 1666017 | *y++ = val; | |
303 | } else { /*Lots of dimensions case:*/ | ||
304 | /*Are there any pulses in this dimension at all?*/ | ||
305 | 3936461 | p = ff_celt_pvq_u_row[K ][N]; | |
306 | 3936461 | q = ff_celt_pvq_u_row[K + 1][N]; | |
307 | |||
308 |
4/4✓ Branch 0 taken 3385353 times.
✓ Branch 1 taken 551108 times.
✓ Branch 2 taken 2833646 times.
✓ Branch 3 taken 551707 times.
|
3936461 | if (p <= i && i < q) { |
309 | 2833646 | i -= p; | |
310 | 2833646 | *y++ = 0; | |
311 | } else { | ||
312 | /*Are the pulses in this dimension negative?*/ | ||
313 | 1102815 | s = -(i >= q); | |
314 | 1102815 | i -= q & s; | |
315 | |||
316 | /*Count how many pulses were placed in this dimension.*/ | ||
317 | 1102815 | k0 = K; | |
318 | 1284281 | do p = ff_celt_pvq_u_row[--K][N]; | |
319 |
2/2✓ Branch 0 taken 181466 times.
✓ Branch 1 taken 1102815 times.
|
1284281 | while (p > i); |
320 | |||
321 | 1102815 | i -= p; | |
322 | 1102815 | val = (k0 - K + s) ^ s; | |
323 | 1102815 | norm += val * val; | |
324 | 1102815 | *y++ = val; | |
325 | } | ||
326 | } | ||
327 | 5602478 | N--; | |
328 | } | ||
329 | |||
330 | /* N == 2 */ | ||
331 | 867942 | p = 2 * K + 1; | |
332 | 867942 | s = -(i >= p); | |
333 | 867942 | i -= p & s; | |
334 | 867942 | k0 = K; | |
335 | 867942 | K = (i + 1) / 2; | |
336 | |||
337 |
2/2✓ Branch 0 taken 611968 times.
✓ Branch 1 taken 255974 times.
|
867942 | if (K) |
338 | 611968 | i -= 2 * K - 1; | |
339 | |||
340 | 867942 | val = (k0 - K + s) ^ s; | |
341 | 867942 | norm += val * val; | |
342 | 867942 | *y++ = val; | |
343 | |||
344 | /* N==1 */ | ||
345 | 867942 | s = -i; | |
346 | 867942 | val = (K + s) ^ s; | |
347 | 867942 | norm += val * val; | |
348 | 867942 | *y = val; | |
349 | |||
350 | 867942 | return norm; | |
351 | } | ||
352 | |||
353 | ✗ | static inline void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K) | |
354 | { | ||
355 | ✗ | ff_opus_rc_enc_uint(rc, celt_icwrsi(N, K, y), CELT_PVQ_V(N, K)); | |
356 | } | ||
357 | |||
358 | 867942 | static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K) | |
359 | { | ||
360 |
4/4✓ Branch 0 taken 313175 times.
✓ Branch 1 taken 554767 times.
✓ Branch 2 taken 281043 times.
✓ Branch 3 taken 586899 times.
|
867942 | const uint32_t idx = ff_opus_rc_dec_uint(rc, CELT_PVQ_V(N, K)); |
361 | 867942 | return celt_cwrsi(N, K, idx, y); | |
362 | } | ||
363 | |||
364 | /* | ||
365 | * Faster than libopus's search, operates entirely in the signed domain. | ||
366 | * Slightly worse/better depending on N, K and the input vector. | ||
367 | */ | ||
368 | ✗ | static float ppp_pvq_search_c(float *X, int *y, int K, int N) | |
369 | { | ||
370 | ✗ | int i, y_norm = 0; | |
371 | ✗ | float res = 0.0f, xy_norm = 0.0f; | |
372 | |||
373 | ✗ | for (i = 0; i < N; i++) | |
374 | ✗ | res += FFABS(X[i]); | |
375 | |||
376 | ✗ | res = K/(res + FLT_EPSILON); | |
377 | |||
378 | ✗ | for (i = 0; i < N; i++) { | |
379 | ✗ | y[i] = lrintf(res*X[i]); | |
380 | ✗ | y_norm += y[i]*y[i]; | |
381 | ✗ | xy_norm += y[i]*X[i]; | |
382 | ✗ | K -= FFABS(y[i]); | |
383 | } | ||
384 | |||
385 | ✗ | while (K) { | |
386 | ✗ | int max_idx = 0, phase = FFSIGN(K); | |
387 | ✗ | float max_num = 0.0f; | |
388 | ✗ | float max_den = 1.0f; | |
389 | ✗ | y_norm += 1.0f; | |
390 | |||
391 | ✗ | for (i = 0; i < N; i++) { | |
392 | /* If the sum has been overshot and the best place has 0 pulses allocated | ||
393 | * to it, attempting to decrease it further will actually increase the | ||
394 | * sum. Prevent this by disregarding any 0 positions when decrementing. */ | ||
395 | ✗ | const int ca = 1 ^ ((y[i] == 0) & (phase < 0)); | |
396 | ✗ | const int y_new = y_norm + 2*phase*FFABS(y[i]); | |
397 | ✗ | float xy_new = xy_norm + 1*phase*FFABS(X[i]); | |
398 | ✗ | xy_new = xy_new * xy_new; | |
399 | ✗ | if (ca && (max_den*xy_new) > (y_new*max_num)) { | |
400 | ✗ | max_den = y_new; | |
401 | ✗ | max_num = xy_new; | |
402 | ✗ | max_idx = i; | |
403 | } | ||
404 | } | ||
405 | |||
406 | ✗ | K -= phase; | |
407 | |||
408 | ✗ | phase *= FFSIGN(X[max_idx]); | |
409 | ✗ | xy_norm += 1*phase*X[max_idx]; | |
410 | ✗ | y_norm += 2*phase*y[max_idx]; | |
411 | ✗ | y[max_idx] += phase; | |
412 | } | ||
413 | |||
414 | ✗ | return (float)y_norm; | |
415 | } | ||
416 | |||
417 | ✗ | static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, | |
418 | enum CeltSpread spread, uint32_t blocks, float gain, | ||
419 | CeltPVQ *pvq) | ||
420 | { | ||
421 | ✗ | int *y = pvq->qcoeff; | |
422 | |||
423 | ✗ | celt_exp_rotation(X, N, blocks, K, spread, 1); | |
424 | ✗ | gain /= sqrtf(pvq->pvq_search(X, y, K, N)); | |
425 | ✗ | celt_encode_pulses(rc, y, N, K); | |
426 | ✗ | celt_normalize_residual(y, X, N, gain); | |
427 | ✗ | celt_exp_rotation(X, N, blocks, K, spread, 0); | |
428 | ✗ | return celt_extract_collapse_mask(y, N, blocks); | |
429 | } | ||
430 | |||
431 | /** Decode pulse vector and combine the result with the pitch vector to produce | ||
432 | the final normalised signal in the current band. */ | ||
433 | 867942 | static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, | |
434 | enum CeltSpread spread, uint32_t blocks, float gain, | ||
435 | CeltPVQ *pvq) | ||
436 | { | ||
437 | 867942 | int *y = pvq->qcoeff; | |
438 | |||
439 | 867942 | gain /= sqrtf(celt_decode_pulses(rc, y, N, K)); | |
440 | 867942 | celt_normalize_residual(y, X, N, gain); | |
441 | 867942 | celt_exp_rotation(X, N, blocks, K, spread, 0); | |
442 | 867942 | return celt_extract_collapse_mask(y, N, blocks); | |
443 | } | ||
444 | |||
445 | ✗ | static int celt_calc_theta(const float *X, const float *Y, int coupling, int N) | |
446 | { | ||
447 | int i; | ||
448 | ✗ | float e[2] = { 0.0f, 0.0f }; | |
449 | ✗ | if (coupling) { /* Coupling case */ | |
450 | ✗ | for (i = 0; i < N; i++) { | |
451 | ✗ | e[0] += (X[i] + Y[i])*(X[i] + Y[i]); | |
452 | ✗ | e[1] += (X[i] - Y[i])*(X[i] - Y[i]); | |
453 | } | ||
454 | } else { | ||
455 | ✗ | for (i = 0; i < N; i++) { | |
456 | ✗ | e[0] += X[i]*X[i]; | |
457 | ✗ | e[1] += Y[i]*Y[i]; | |
458 | } | ||
459 | } | ||
460 | ✗ | return lrintf(32768.0f*atan2f(sqrtf(e[1]), sqrtf(e[0]))/M_PI); | |
461 | } | ||
462 | |||
463 | ✗ | static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N) | |
464 | { | ||
465 | int i; | ||
466 | ✗ | const float energy_n = 1.0f/(sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON); | |
467 | ✗ | e_l *= energy_n; | |
468 | ✗ | e_r *= energy_n; | |
469 | ✗ | for (i = 0; i < N; i++) | |
470 | ✗ | X[i] = e_l*X[i] + e_r*Y[i]; | |
471 | } | ||
472 | |||
473 | ✗ | static void celt_stereo_ms_decouple(float *X, float *Y, int N) | |
474 | { | ||
475 | int i; | ||
476 | ✗ | for (i = 0; i < N; i++) { | |
477 | ✗ | const float Xret = X[i]; | |
478 | ✗ | X[i] = (X[i] + Y[i])*M_SQRT1_2; | |
479 | ✗ | Y[i] = (Y[i] - Xret)*M_SQRT1_2; | |
480 | } | ||
481 | } | ||
482 | |||
483 | 1742927 | static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |
484 | OpusRangeCoder *rc, | ||
485 | const int band, float *X, | ||
486 | float *Y, int N, int b, | ||
487 | uint32_t blocks, float *lowband, | ||
488 | int duration, float *lowband_out, | ||
489 | int level, float gain, | ||
490 | float *lowband_scratch, | ||
491 | int fill, int quant) | ||
492 | { | ||
493 | int i; | ||
494 | const uint8_t *cache; | ||
495 | 1742927 | int stereo = !!Y, split = stereo; | |
496 | 1742927 | int imid = 0, iside = 0; | |
497 | 1742927 | uint32_t N0 = N; | |
498 | 1742927 | int N_B = N / blocks; | |
499 | 1742927 | int N_B0 = N_B; | |
500 | 1742927 | int B0 = blocks; | |
501 | 1742927 | int time_divide = 0; | |
502 | 1742927 | int recombine = 0; | |
503 | 1742927 | int inv = 0; | |
504 | 1742927 | float mid = 0, side = 0; | |
505 | 1742927 | int longblocks = (B0 == 1); | |
506 | 1742927 | uint32_t cm = 0; | |
507 | |||
508 |
2/2✓ Branch 0 taken 109648 times.
✓ Branch 1 taken 1633279 times.
|
1742927 | if (N == 1) { |
509 | 109648 | float *x = X; | |
510 |
2/2✓ Branch 0 taken 176024 times.
✓ Branch 1 taken 109648 times.
|
285672 | for (i = 0; i <= stereo; i++) { |
511 | 176024 | int sign = 0; | |
512 |
2/2✓ Branch 0 taken 169213 times.
✓ Branch 1 taken 6811 times.
|
176024 | if (f->remaining2 >= 1 << 3) { |
513 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 169213 times.
|
169213 | if (quant) { |
514 | ✗ | sign = x[0] < 0; | |
515 | ✗ | ff_opus_rc_put_raw(rc, sign, 1); | |
516 | } else { | ||
517 | 169213 | sign = ff_opus_rc_get_raw(rc, 1); | |
518 | } | ||
519 | 169213 | f->remaining2 -= 1 << 3; | |
520 | } | ||
521 | 176024 | x[0] = 1.0f - 2.0f*sign; | |
522 | 176024 | x = Y; | |
523 | } | ||
524 |
1/2✓ Branch 0 taken 109648 times.
✗ Branch 1 not taken.
|
109648 | if (lowband_out) |
525 | 109648 | lowband_out[0] = X[0]; | |
526 | 109648 | return 1; | |
527 | } | ||
528 | |||
529 |
4/4✓ Branch 0 taken 1392893 times.
✓ Branch 1 taken 240386 times.
✓ Branch 2 taken 620101 times.
✓ Branch 3 taken 772792 times.
|
1633279 | if (!stereo && level == 0) { |
530 | 620101 | int tf_change = f->tf_change[band]; | |
531 | int k; | ||
532 |
2/2✓ Branch 0 taken 40217 times.
✓ Branch 1 taken 579884 times.
|
620101 | if (tf_change > 0) |
533 | 40217 | recombine = tf_change; | |
534 | /* Band recombining to increase frequency resolution */ | ||
535 | |||
536 |
4/4✓ Branch 0 taken 371229 times.
✓ Branch 1 taken 248872 times.
✓ Branch 2 taken 345387 times.
✓ Branch 3 taken 25842 times.
|
620101 | if (lowband && |
537 |
6/6✓ Branch 0 taken 334517 times.
✓ Branch 1 taken 10870 times.
✓ Branch 2 taken 275524 times.
✓ Branch 3 taken 58993 times.
✓ Branch 4 taken 23444 times.
✓ Branch 5 taken 262950 times.
|
345387 | (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) { |
538 |
2/2✓ Branch 0 taken 2674010 times.
✓ Branch 1 taken 108279 times.
|
2782289 | for (i = 0; i < N; i++) |
539 | 2674010 | lowband_scratch[i] = lowband[i]; | |
540 | 108279 | lowband = lowband_scratch; | |
541 | } | ||
542 | |||
543 |
2/2✓ Branch 0 taken 67800 times.
✓ Branch 1 taken 620101 times.
|
687901 | for (k = 0; k < recombine; k++) { |
544 |
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) |
545 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42647 times.
|
42647 | celt_haar1(quant ? X : lowband, N >> k, 1 << k); |
546 | 67800 | fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2; | |
547 | } | ||
548 | 620101 | blocks >>= recombine; | |
549 | 620101 | N_B <<= recombine; | |
550 | |||
551 | /* Increasing the time resolution */ | ||
552 |
4/4✓ Branch 0 taken 719316 times.
✓ Branch 1 taken 50423 times.
✓ Branch 2 taken 149638 times.
✓ Branch 3 taken 569678 times.
|
769739 | while ((N_B & 1) == 0 && tf_change < 0) { |
553 |
3/4✓ Branch 0 taken 149638 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96834 times.
✓ Branch 3 taken 52804 times.
|
149638 | if (quant || lowband) |
554 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96834 times.
|
96834 | celt_haar1(quant ? X : lowband, N_B, blocks); |
555 | 149638 | fill |= fill << blocks; | |
556 | 149638 | blocks <<= 1; | |
557 | 149638 | N_B >>= 1; | |
558 | 149638 | time_divide++; | |
559 | 149638 | tf_change++; | |
560 | } | ||
561 | 620101 | B0 = blocks; | |
562 | 620101 | N_B0 = N_B; | |
563 | |||
564 | /* Reorganize the samples in time order instead of frequency order */ | ||
565 |
5/6✓ Branch 0 taken 140127 times.
✓ Branch 1 taken 479974 times.
✓ Branch 2 taken 140127 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 90824 times.
✓ Branch 5 taken 49303 times.
|
620101 | if (B0 > 1 && (quant || lowband)) |
566 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90824 times.
|
90824 | celt_deinterleave_hadamard(pvq->hadamard_tmp, quant ? X : lowband, |
567 | N_B >> recombine, B0 << recombine, | ||
568 | longblocks); | ||
569 | } | ||
570 | |||
571 | /* If we need 1.5 more bit than we can produce, split the band in two. */ | ||
572 | 1633279 | cache = ff_celt_cache_bits + | |
573 | 1633279 | ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band]; | |
574 |
8/8✓ Branch 0 taken 1392893 times.
✓ Branch 1 taken 240386 times.
✓ Branch 2 taken 1193327 times.
✓ Branch 3 taken 199566 times.
✓ Branch 4 taken 440823 times.
✓ Branch 5 taken 752504 times.
✓ Branch 6 taken 386396 times.
✓ Branch 7 taken 54427 times.
|
1633279 | if (!stereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) { |
575 | 386396 | N >>= 1; | |
576 | 386396 | Y = X + N; | |
577 | 386396 | split = 1; | |
578 | 386396 | duration -= 1; | |
579 |
2/2✓ Branch 0 taken 245705 times.
✓ Branch 1 taken 140691 times.
|
386396 | if (blocks == 1) |
580 | 245705 | fill = (fill & 1) | (fill << 1); | |
581 | 386396 | blocks = (blocks + 1) >> 1; | |
582 | } | ||
583 | |||
584 |
2/2✓ Branch 0 taken 626782 times.
✓ Branch 1 taken 1006497 times.
|
1633279 | if (split) { |
585 | int qn; | ||
586 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 626782 times.
|
626782 | int itheta = quant ? celt_calc_theta(X, Y, stereo, N) : 0; |
587 | int mbits, sbits, delta; | ||
588 | int qalloc; | ||
589 | int pulse_cap; | ||
590 | int offset; | ||
591 | int orig_fill; | ||
592 | int tell; | ||
593 | |||
594 | /* Decide on the resolution to give to the split parameter theta */ | ||
595 | 626782 | pulse_cap = ff_celt_log_freq_range[band] + duration * 8; | |
596 |
4/4✓ Branch 0 taken 240386 times.
✓ Branch 1 taken 386396 times.
✓ Branch 2 taken 54195 times.
✓ Branch 3 taken 186191 times.
|
626782 | offset = (pulse_cap >> 1) - (stereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE : |
597 | CELT_QTHETA_OFFSET); | ||
598 |
4/4✓ Branch 0 taken 240386 times.
✓ Branch 1 taken 386396 times.
✓ Branch 2 taken 188735 times.
✓ Branch 3 taken 51651 times.
|
626782 | qn = (stereo && band >= f->intensity_stereo) ? 1 : |
599 | 575131 | celt_compute_qn(N, b, offset, pulse_cap, stereo); | |
600 | 626782 | tell = opus_rc_tell_frac(rc); | |
601 |
2/2✓ Branch 0 taken 573416 times.
✓ Branch 1 taken 53366 times.
|
626782 | if (qn != 1) { |
602 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 573416 times.
|
573416 | if (quant) |
603 | ✗ | itheta = (itheta*qn + 8192) >> 14; | |
604 | /* Entropy coding of the angle. We use a uniform pdf for the | ||
605 | * time split, a step for stereo, and a triangular one for the rest. */ | ||
606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 573416 times.
|
573416 | if (quant) { |
607 | ✗ | if (stereo && N > 2) | |
608 | ✗ | ff_opus_rc_enc_uint_step(rc, itheta, qn / 2); | |
609 | ✗ | else if (stereo || B0 > 1) | |
610 | ✗ | ff_opus_rc_enc_uint(rc, itheta, qn + 1); | |
611 | else | ||
612 | ✗ | ff_opus_rc_enc_uint_tri(rc, itheta, qn); | |
613 | ✗ | itheta = itheta * 16384 / qn; | |
614 | ✗ | if (stereo) { | |
615 | ✗ | if (itheta == 0) | |
616 | ✗ | celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band], | |
617 | f->block[1].lin_energy[band], N); | ||
618 | else | ||
619 | ✗ | celt_stereo_ms_decouple(X, Y, N); | |
620 | } | ||
621 | } else { | ||
622 |
4/4✓ Branch 0 taken 187020 times.
✓ Branch 1 taken 386396 times.
✓ Branch 2 taken 138861 times.
✓ Branch 3 taken 48159 times.
|
573416 | if (stereo && N > 2) |
623 | 138861 | itheta = ff_opus_rc_dec_uint_step(rc, qn / 2); | |
624 |
4/4✓ Branch 0 taken 386396 times.
✓ Branch 1 taken 48159 times.
✓ Branch 2 taken 140691 times.
✓ Branch 3 taken 245705 times.
|
434555 | else if (stereo || B0 > 1) |
625 | 188850 | itheta = ff_opus_rc_dec_uint(rc, qn+1); | |
626 | else | ||
627 | 245705 | itheta = ff_opus_rc_dec_uint_tri(rc, qn); | |
628 | 573416 | itheta = itheta * 16384 / qn; | |
629 | } | ||
630 |
1/2✓ Branch 0 taken 53366 times.
✗ Branch 1 not taken.
|
53366 | } else if (stereo) { |
631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53366 times.
|
53366 | if (quant) { |
632 | ✗ | inv = f->apply_phase_inv ? itheta > 8192 : 0; | |
633 | ✗ | if (inv) { | |
634 | ✗ | for (i = 0; i < N; i++) | |
635 | ✗ | Y[i] *= -1; | |
636 | } | ||
637 | ✗ | celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band], | |
638 | f->block[1].lin_energy[band], N); | ||
639 | |||
640 | ✗ | if (b > 2 << 3 && f->remaining2 > 2 << 3) { | |
641 | ✗ | ff_opus_rc_enc_log(rc, inv, 2); | |
642 | } else { | ||
643 | ✗ | inv = 0; | |
644 | } | ||
645 | } else { | ||
646 |
3/4✓ Branch 0 taken 19527 times.
✓ Branch 1 taken 33839 times.
✓ Branch 2 taken 19527 times.
✗ Branch 3 not taken.
|
53366 | inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0; |
647 |
1/2✓ Branch 0 taken 53366 times.
✗ Branch 1 not taken.
|
53366 | inv = f->apply_phase_inv ? inv : 0; |
648 | } | ||
649 | 53366 | itheta = 0; | |
650 | } | ||
651 | 626782 | qalloc = opus_rc_tell_frac(rc) - tell; | |
652 | 626782 | b -= qalloc; | |
653 | |||
654 | 626782 | orig_fill = fill; | |
655 |
2/2✓ Branch 0 taken 105651 times.
✓ Branch 1 taken 521131 times.
|
626782 | if (itheta == 0) { |
656 | 105651 | imid = 32767; | |
657 | 105651 | iside = 0; | |
658 | 105651 | fill = av_mod_uintp2(fill, blocks); | |
659 | 105651 | delta = -16384; | |
660 |
2/2✓ Branch 0 taken 2246 times.
✓ Branch 1 taken 518885 times.
|
521131 | } else if (itheta == 16384) { |
661 | 2246 | imid = 0; | |
662 | 2246 | iside = 32767; | |
663 | 2246 | fill &= ((1 << blocks) - 1) << blocks; | |
664 | 2246 | delta = 16384; | |
665 | } else { | ||
666 | 518885 | imid = celt_cos(itheta); | |
667 | 518885 | iside = celt_cos(16384-itheta); | |
668 | /* This is the mid vs side allocation that minimizes squared error | ||
669 | in that band. */ | ||
670 | 518885 | delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid)); | |
671 | } | ||
672 | |||
673 | 626782 | mid = imid / 32768.0f; | |
674 | 626782 | side = iside / 32768.0f; | |
675 | |||
676 | /* This is a special case for N=2 that only works for stereo and takes | ||
677 | advantage of the fact that mid and side are orthogonal to encode | ||
678 | the side with just one bit. */ | ||
679 |
4/4✓ Branch 0 taken 104239 times.
✓ Branch 1 taken 522543 times.
✓ Branch 2 taken 54195 times.
✓ Branch 3 taken 50044 times.
|
626782 | if (N == 2 && stereo) { |
680 | int c; | ||
681 | 54195 | int sign = 0; | |
682 | float tmp; | ||
683 | float *x2, *y2; | ||
684 | 54195 | mbits = b; | |
685 | /* Only need one bit for the side */ | ||
686 |
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; |
687 | 54195 | mbits -= sbits; | |
688 | 54195 | c = (itheta > 8192); | |
689 | 54195 | f->remaining2 -= qalloc+sbits; | |
690 | |||
691 |
2/2✓ Branch 0 taken 11376 times.
✓ Branch 1 taken 42819 times.
|
54195 | x2 = c ? Y : X; |
692 |
2/2✓ Branch 0 taken 11376 times.
✓ Branch 1 taken 42819 times.
|
54195 | y2 = c ? X : Y; |
693 |
2/2✓ Branch 0 taken 28840 times.
✓ Branch 1 taken 25355 times.
|
54195 | if (sbits) { |
694 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28840 times.
|
28840 | if (quant) { |
695 | ✗ | sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; | |
696 | ✗ | ff_opus_rc_put_raw(rc, sign, 1); | |
697 | } else { | ||
698 | 28840 | sign = ff_opus_rc_get_raw(rc, 1); | |
699 | } | ||
700 | } | ||
701 | 54195 | sign = 1 - 2 * sign; | |
702 | /* We use orig_fill here because we want to fold the side, but if | ||
703 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
704 | 54195 | cm = pvq->quant_band(pvq, f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration, | |
705 | lowband_out, level, gain, lowband_scratch, orig_fill); | ||
706 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
707 | and there's no need to worry about mixing with the other channel. */ | ||
708 | 54195 | y2[0] = -sign * x2[1]; | |
709 | 54195 | y2[1] = sign * x2[0]; | |
710 | 54195 | X[0] *= mid; | |
711 | 54195 | X[1] *= mid; | |
712 | 54195 | Y[0] *= side; | |
713 | 54195 | Y[1] *= side; | |
714 | 54195 | tmp = X[0]; | |
715 | 54195 | X[0] = tmp - Y[0]; | |
716 | 54195 | Y[0] = tmp + Y[0]; | |
717 | 54195 | tmp = X[1]; | |
718 | 54195 | X[1] = tmp - Y[1]; | |
719 | 54195 | Y[1] = tmp + Y[1]; | |
720 | } else { | ||
721 | /* "Normal" split code */ | ||
722 | 572587 | float *next_lowband2 = NULL; | |
723 | 572587 | float *next_lowband_out1 = NULL; | |
724 | 572587 | int next_level = 0; | |
725 | int rebalance; | ||
726 | uint32_t cmt; | ||
727 | |||
728 | /* Give more bits to low-energy MDCTs than they would | ||
729 | * otherwise deserve */ | ||
730 |
6/6✓ Branch 0 taken 169739 times.
✓ Branch 1 taken 402848 times.
✓ Branch 2 taken 140691 times.
✓ Branch 3 taken 29048 times.
✓ Branch 4 taken 139845 times.
✓ Branch 5 taken 846 times.
|
572587 | if (B0 > 1 && !stereo && (itheta & 0x3fff)) { |
731 |
2/2✓ Branch 0 taken 61043 times.
✓ Branch 1 taken 78802 times.
|
139845 | if (itheta > 8192) |
732 | /* Rough approximation for pre-echo masking */ | ||
733 | 61043 | delta -= delta >> (4 - duration); | |
734 | else | ||
735 | /* Corresponds to a forward-masking slope of | ||
736 | * 1.5 dB per 10 ms */ | ||
737 | 78802 | delta = FFMIN(0, delta + (N << 3 >> (5 - duration))); | |
738 | } | ||
739 | 572587 | mbits = av_clip((b - delta) / 2, 0, b); | |
740 | 572587 | sbits = b - mbits; | |
741 | 572587 | f->remaining2 -= qalloc; | |
742 | |||
743 |
4/4✓ Branch 0 taken 452225 times.
✓ Branch 1 taken 120362 times.
✓ Branch 2 taken 292101 times.
✓ Branch 3 taken 160124 times.
|
572587 | if (lowband && !stereo) |
744 | 292101 | next_lowband2 = lowband + N; /* >32-bit split case */ | |
745 | |||
746 | /* Only stereo needs to pass on lowband_out. | ||
747 | * Otherwise, it's handled at the end */ | ||
748 |
2/2✓ Branch 0 taken 186191 times.
✓ Branch 1 taken 386396 times.
|
572587 | if (stereo) |
749 | 186191 | next_lowband_out1 = lowband_out; | |
750 | else | ||
751 | 386396 | next_level = level + 1; | |
752 | |||
753 | 572587 | rebalance = f->remaining2; | |
754 |
2/2✓ Branch 0 taken 351486 times.
✓ Branch 1 taken 221101 times.
|
572587 | if (mbits >= sbits) { |
755 | /* In stereo mode, we do not apply a scaling to the mid | ||
756 | * because we need the normalized mid for folding later */ | ||
757 |
2/2✓ Branch 0 taken 202395 times.
✓ Branch 1 taken 149091 times.
|
351486 | cm = pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks, |
758 | lowband, duration, next_lowband_out1, next_level, | ||
759 | stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | ||
760 | 351486 | rebalance = mbits - (rebalance - f->remaining2); | |
761 |
4/4✓ Branch 0 taken 95700 times.
✓ Branch 1 taken 255786 times.
✓ Branch 2 taken 86536 times.
✓ Branch 3 taken 9164 times.
|
351486 | if (rebalance > 3 << 3 && itheta != 0) |
762 | 86536 | sbits += rebalance - (3 << 3); | |
763 | |||
764 | /* For a stereo split, the high bits of fill are always zero, | ||
765 | * so no folding will be done to the side. */ | ||
766 | 351486 | cmt = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks, | |
767 | next_lowband2, duration, NULL, next_level, | ||
768 | gain * side, NULL, fill >> blocks); | ||
769 | 351486 | cm |= cmt << ((B0 >> 1) & (stereo - 1)); | |
770 | } else { | ||
771 | /* For a stereo split, the high bits of fill are always zero, | ||
772 | * so no folding will be done to the side. */ | ||
773 | 221101 | cm = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks, | |
774 | next_lowband2, duration, NULL, next_level, | ||
775 | gain * side, NULL, fill >> blocks); | ||
776 | 221101 | cm <<= ((B0 >> 1) & (stereo - 1)); | |
777 | 221101 | rebalance = sbits - (rebalance - f->remaining2); | |
778 |
4/4✓ Branch 0 taken 81130 times.
✓ Branch 1 taken 139971 times.
✓ Branch 2 taken 80951 times.
✓ Branch 3 taken 179 times.
|
221101 | if (rebalance > 3 << 3 && itheta != 16384) |
779 | 80951 | mbits += rebalance - (3 << 3); | |
780 | |||
781 | /* In stereo mode, we do not apply a scaling to the mid because | ||
782 | * we need the normalized mid for folding later */ | ||
783 |
2/2✓ Branch 0 taken 184001 times.
✓ Branch 1 taken 37100 times.
|
221101 | cm |= pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks, |
784 | lowband, duration, next_lowband_out1, next_level, | ||
785 | stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | ||
786 | } | ||
787 | } | ||
788 | } else { | ||
789 | /* This is the basic no-split case */ | ||
790 | 1006497 | uint32_t q = celt_bits2pulses(cache, b); | |
791 | 1006497 | uint32_t curr_bits = celt_pulses2bits(cache, q); | |
792 | 1006497 | f->remaining2 -= curr_bits; | |
793 | |||
794 | /* Ensures we can never bust the budget */ | ||
795 |
4/4✓ Branch 0 taken 14967 times.
✓ Branch 1 taken 1003262 times.
✓ Branch 2 taken 11732 times.
✓ Branch 3 taken 3235 times.
|
1018229 | while (f->remaining2 < 0 && q > 0) { |
796 | 11732 | f->remaining2 += curr_bits; | |
797 | 11732 | curr_bits = celt_pulses2bits(cache, --q); | |
798 | 11732 | f->remaining2 -= curr_bits; | |
799 | } | ||
800 | |||
801 |
2/2✓ Branch 0 taken 867942 times.
✓ Branch 1 taken 138555 times.
|
1006497 | if (q != 0) { |
802 | /* Finally do the actual (de)quantization */ | ||
803 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 867942 times.
|
867942 | if (quant) { |
804 | ✗ | cm = celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), | |
805 | f->spread, blocks, gain, pvq); | ||
806 | } else { | ||
807 |
2/2✓ Branch 0 taken 557776 times.
✓ Branch 1 taken 310166 times.
|
867942 | cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), |
808 | f->spread, blocks, gain, pvq); | ||
809 | } | ||
810 | } else { | ||
811 | /* If there's no pulse, fill the band anyway */ | ||
812 | 138555 | uint32_t cm_mask = (1 << blocks) - 1; | |
813 | 138555 | fill &= cm_mask; | |
814 |
2/2✓ Branch 0 taken 54172 times.
✓ Branch 1 taken 84383 times.
|
138555 | if (fill) { |
815 |
2/2✓ Branch 0 taken 9194 times.
✓ Branch 1 taken 44978 times.
|
54172 | if (!lowband) { |
816 | /* Noise */ | ||
817 |
2/2✓ Branch 0 taken 212448 times.
✓ Branch 1 taken 9194 times.
|
221642 | for (i = 0; i < N; i++) |
818 | 212448 | X[i] = (((int32_t)celt_rng(f)) >> 20); | |
819 | 9194 | cm = cm_mask; | |
820 | } else { | ||
821 | /* Folded spectrum */ | ||
822 |
2/2✓ Branch 0 taken 1772186 times.
✓ Branch 1 taken 44978 times.
|
1817164 | for (i = 0; i < N; i++) { |
823 | /* About 48 dB below the "normal" folding level */ | ||
824 |
2/2✓ Branch 1 taken 887413 times.
✓ Branch 2 taken 884773 times.
|
1772186 | X[i] = lowband[i] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256); |
825 | } | ||
826 | 44978 | cm = fill; | |
827 | } | ||
828 | 54172 | celt_renormalize_vector(X, N, gain); | |
829 | } else { | ||
830 | 84383 | memset(X, 0, N*sizeof(float)); | |
831 | } | ||
832 | } | ||
833 | } | ||
834 | |||
835 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | ||
836 |
2/2✓ Branch 0 taken 240386 times.
✓ Branch 1 taken 1392893 times.
|
1633279 | if (stereo) { |
837 |
2/2✓ Branch 0 taken 186191 times.
✓ Branch 1 taken 54195 times.
|
240386 | if (N > 2) |
838 | 186191 | celt_stereo_merge(X, Y, mid, N); | |
839 |
2/2✓ Branch 0 taken 6978 times.
✓ Branch 1 taken 233408 times.
|
240386 | if (inv) { |
840 |
2/2✓ Branch 0 taken 391242 times.
✓ Branch 1 taken 6978 times.
|
398220 | for (i = 0; i < N; i++) |
841 | 391242 | Y[i] *= -1; | |
842 | } | ||
843 |
2/2✓ Branch 0 taken 620101 times.
✓ Branch 1 taken 772792 times.
|
1392893 | } else if (level == 0) { |
844 | int k; | ||
845 | |||
846 | /* Undo the sample reorganization going from time order to frequency order */ | ||
847 |
2/2✓ Branch 0 taken 140127 times.
✓ Branch 1 taken 479974 times.
|
620101 | if (B0 > 1) |
848 | 140127 | celt_interleave_hadamard(pvq->hadamard_tmp, X, N_B >> recombine, | |
849 | B0 << recombine, longblocks); | ||
850 | |||
851 | /* Undo time-freq changes that we did earlier */ | ||
852 | 620101 | N_B = N_B0; | |
853 | 620101 | blocks = B0; | |
854 |
2/2✓ Branch 0 taken 149638 times.
✓ Branch 1 taken 620101 times.
|
769739 | for (k = 0; k < time_divide; k++) { |
855 | 149638 | blocks >>= 1; | |
856 | 149638 | N_B <<= 1; | |
857 | 149638 | cm |= cm >> blocks; | |
858 | 149638 | celt_haar1(X, N_B, blocks); | |
859 | } | ||
860 | |||
861 |
2/2✓ Branch 0 taken 67800 times.
✓ Branch 1 taken 620101 times.
|
687901 | for (k = 0; k < recombine; k++) { |
862 | 67800 | cm = ff_celt_bit_deinterleave[cm]; | |
863 | 67800 | celt_haar1(X, N0>>k, 1<<k); | |
864 | } | ||
865 | 620101 | blocks <<= recombine; | |
866 | |||
867 | /* Scale output for later folding */ | ||
868 |
2/2✓ Branch 0 taken 433910 times.
✓ Branch 1 taken 186191 times.
|
620101 | if (lowband_out) { |
869 | 433910 | float n = sqrtf(N0); | |
870 |
2/2✓ Branch 0 taken 7755370 times.
✓ Branch 1 taken 433910 times.
|
8189280 | for (i = 0; i < N0; i++) |
871 | 7755370 | lowband_out[i] = n * X[i]; | |
872 | } | ||
873 | 620101 | cm = av_mod_uintp2(cm, blocks); | |
874 | } | ||
875 | |||
876 | 1633279 | return cm; | |
877 | } | ||
878 | |||
879 | 1742927 | static QUANT_FN(pvq_decode_band) | |
880 | { | ||
881 | #if CONFIG_OPUS_DECODER | ||
882 | 1742927 | return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration, | |
883 | lowband_out, level, gain, lowband_scratch, fill, 0); | ||
884 | #else | ||
885 | return 0; | ||
886 | #endif | ||
887 | } | ||
888 | |||
889 | ✗ | static QUANT_FN(pvq_encode_band) | |
890 | { | ||
891 | #if CONFIG_OPUS_ENCODER | ||
892 | ✗ | return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration, | |
893 | lowband_out, level, gain, lowband_scratch, fill, 1); | ||
894 | #else | ||
895 | return 0; | ||
896 | #endif | ||
897 | } | ||
898 | |||
899 | 48 | int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode) | |
900 | { | ||
901 | 48 | CeltPVQ *s = av_malloc(sizeof(CeltPVQ)); | |
902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (!s) |
903 | ✗ | return AVERROR(ENOMEM); | |
904 | |||
905 | 48 | s->pvq_search = ppp_pvq_search_c; | |
906 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | s->quant_band = encode ? pvq_encode_band : pvq_decode_band; |
907 | |||
908 | #if CONFIG_OPUS_ENCODER && ARCH_X86 | ||
909 | 48 | ff_celt_pvq_init_x86(s); | |
910 | #endif | ||
911 | |||
912 | 48 | *pvq = s; | |
913 | |||
914 | 48 | return 0; | |
915 | } | ||
916 | |||
917 | 48 | void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq) | |
918 | { | ||
919 | 48 | av_freep(pvq); | |
920 | 48 | } | |
921 |