Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/opus/pvq.c
Line
Count
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
1.87M
#define ROUND_MUL16(a,b)  ((MUL16(a, b) + 16384) >> 15)
36
37
2.04M
#define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
38
1.02M
#define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
39
40
static inline int16_t celt_cos(int16_t x)
41
751k
{
42
751k
    x = (MUL16(x, x) + 4096) >> 13;
43
751k
    x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x)))));
44
751k
    return x + 1;
45
751k
}
46
47
static inline int celt_log2tan(int isin, int icos)
48
375k
{
49
375k
    int lc, ls;
50
375k
    lc = opus_ilog(icos);
51
375k
    ls = opus_ilog(isin);
52
375k
    icos <<= 15 - lc;
53
375k
    isin <<= 15 - ls;
54
375k
    return (ls << 11) - (lc << 11) +
55
375k
           ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) -
56
375k
           ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932);
57
375k
}
58
59
static inline int celt_bits2pulses(const uint8_t *cache, int bits)
60
2.94M
{
61
    // TODO: Find the size of cache and make it into an array in the parameters list
62
2.94M
    int i, low = 0, high;
63
64
2.94M
    high = cache[0];
65
2.94M
    bits--;
66
67
20.6M
    for (i = 0; i < 6; i++) {
68
17.6M
        int center = (low + high + 1) >> 1;
69
17.6M
        if (cache[center] >= bits)
70
13.8M
            high = center;
71
3.82M
        else
72
3.82M
            low = center;
73
17.6M
    }
74
75
2.94M
    return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high;
76
2.94M
}
77
78
static inline int celt_pulses2bits(const uint8_t *cache, int pulses)
79
2.96M
{
80
    // TODO: Find the size of cache and make it into an array in the parameters list
81
2.96M
   return (pulses == 0) ? 0 : cache[pulses] + 1;
82
2.96M
}
83
84
static inline void celt_normalize_residual(const int * restrict iy, float * restrict X,
85
                                           int N, float g)
86
1.02M
{
87
1.02M
    int i;
88
6.89M
    for (i = 0; i < N; i++)
89
5.87M
        X[i] = g * iy[i];
90
1.02M
}
91
92
static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride,
93
                                   float c, float s)
94
450k
{
95
450k
    float *Xptr;
96
450k
    int i;
97
98
450k
    Xptr = X;
99
3.25M
    for (i = 0; i < len - stride; i++) {
100
2.80M
        float x1     = Xptr[0];
101
2.80M
        float x2     = Xptr[stride];
102
2.80M
        Xptr[stride] = c * x2 + s * x1;
103
2.80M
        *Xptr++      = c * x1 - s * x2;
104
2.80M
    }
105
106
450k
    Xptr = &X[len - 2 * stride - 1];
107
2.55M
    for (i = len - 2 * stride - 1; i >= 0; i--) {
108
2.10M
        float x1     = Xptr[0];
109
2.10M
        float x2     = Xptr[stride];
110
2.10M
        Xptr[stride] = c * x2 + s * x1;
111
2.10M
        *Xptr--      = c * x1 - s * x2;
112
2.10M
    }
113
450k
}
114
115
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
1.02M
{
119
1.02M
    uint32_t stride2 = 0;
120
1.02M
    float c, s;
121
1.02M
    float gain, theta;
122
1.02M
    int i;
123
124
1.02M
    if (2*K >= len || spread == CELT_SPREAD_NONE)
125
872k
        return;
126
127
151k
    gain = (float)len / (len + (20 - 5*spread) * K);
128
151k
    theta = M_PI * gain * gain / 4;
129
130
151k
    c = cosf(theta);
131
151k
    s = sinf(theta);
132
133
151k
    if (len >= stride << 3) {
134
111k
        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
383k
        while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)
138
272k
            stride2++;
139
111k
    }
140
141
151k
    len /= stride;
142
475k
    for (i = 0; i < stride; i++) {
143
323k
        if (encode) {
144
0
            celt_exp_rotation_impl(X + i * len, len, 1, c, -s);
145
0
            if (stride2)
146
0
                celt_exp_rotation_impl(X + i * len, len, stride2, s, -c);
147
323k
        } else {
148
323k
            if (stride2)
149
127k
                celt_exp_rotation_impl(X + i * len, len, stride2, s, c);
150
323k
            celt_exp_rotation_impl(X + i * len, len, 1, c, s);
151
323k
        }
152
323k
    }
153
151k
}
154
155
static inline uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
156
1.02M
{
157
1.02M
    int i, j, N0 = N / B;
158
1.02M
    uint32_t collapse_mask = 0;
159
160
1.02M
    if (B <= 1)
161
816k
        return 1;
162
163
881k
    for (i = 0; i < B; i++)
164
2.02M
        for (j = 0; j < N0; j++)
165
1.35M
            collapse_mask |= (!!iy[i*N0+j]) << i;
166
207k
    return collapse_mask;
167
1.02M
}
168
169
static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
170
853k
{
171
853k
    int i;
172
853k
    float xp = 0, side = 0;
173
853k
    float E[2];
174
853k
    float mid2;
175
853k
    float gain[2];
176
177
    /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
178
29.6M
    for (i = 0; i < N; i++) {
179
28.8M
        xp   += X[i] * Y[i];
180
28.8M
        side += Y[i] * Y[i];
181
28.8M
    }
182
183
    /* Compensating for the mid normalization */
184
853k
    xp *= mid;
185
853k
    mid2 = mid;
186
853k
    E[0] = mid2 * mid2 + side - 2 * xp;
187
853k
    E[1] = mid2 * mid2 + side + 2 * xp;
188
853k
    if (E[0] < 6e-4f || E[1] < 6e-4f) {
189
11.5k
        for (i = 0; i < N; i++)
190
10.2k
            Y[i] = X[i];
191
1.27k
        return;
192
1.27k
    }
193
194
852k
    gain[0] = 1.0f / sqrtf(E[0]);
195
852k
    gain[1] = 1.0f / sqrtf(E[1]);
196
197
29.6M
    for (i = 0; i < N; i++) {
198
28.8M
        float value[2];
199
        /* Apply mid scaling (side is already scaled) */
200
28.8M
        value[0] = mid * X[i];
201
28.8M
        value[1] = Y[i];
202
28.8M
        X[i] = gain[0] * (value[0] - value[1]);
203
28.8M
        Y[i] = gain[1] * (value[0] + value[1]);
204
28.8M
    }
205
852k
}
206
207
static void celt_interleave_hadamard(float *tmp, float *X, int N0,
208
                                     int stride, int hadamard)
209
720k
{
210
720k
    int i, j, N = N0*stride;
211
720k
    const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30];
212
213
6.31M
    for (i = 0; i < stride; i++)
214
30.7M
        for (j = 0; j < N0; j++)
215
25.1M
            tmp[j*stride+i] = X[order[i]*N0+j];
216
217
720k
    memcpy(X, tmp, N*sizeof(float));
218
720k
}
219
220
static void celt_deinterleave_hadamard(float *tmp, float *X, int N0,
221
                                       int stride, int hadamard)
222
438k
{
223
438k
    int i, j, N = N0*stride;
224
438k
    const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30];
225
226
3.52M
    for (i = 0; i < stride; i++)
227
16.4M
        for (j = 0; j < N0; j++)
228
13.3M
            tmp[order[i]*N0+j] = X[j*stride+i];
229
230
438k
    memcpy(X, tmp, N*sizeof(float));
231
438k
}
232
233
static void celt_haar1(float *X, int N0, int stride)
234
1.78M
{
235
1.78M
    int i, j;
236
1.78M
    N0 >>= 1;
237
6.25M
    for (i = 0; i < stride; i++) {
238
37.5M
        for (j = 0; j < N0; j++) {
239
33.0M
            float x0 = X[stride * (2 * j + 0) + i];
240
33.0M
            float x1 = X[stride * (2 * j + 1) + i];
241
33.0M
            X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2;
242
33.0M
            X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2;
243
33.0M
        }
244
4.47M
    }
245
1.78M
}
246
247
static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap,
248
                                  int stereo)
249
434k
{
250
434k
    int qn, qb;
251
434k
    int N2 = 2 * N - 1;
252
434k
    if (stereo && N == 2)
253
8.74k
        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
434k
    qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3);
259
434k
    qn = (qb < (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
260
434k
    return qn;
261
434k
}
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
0
{
266
0
    int i, idx = 0, sum = 0;
267
0
    for (i = N - 1; i >= 0; i--) {
268
0
        const uint32_t i_s = CELT_PVQ_U(N - i, sum + FFABS(y[i]) + 1);
269
0
        idx += CELT_PVQ_U(N - i, sum) + (y[i] < 0)*i_s;
270
0
        sum += FFABS(y[i]);
271
0
    }
272
0
    return idx;
273
0
}
274
275
// this code was adapted from libopus
276
static inline uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
277
1.02M
{
278
1.02M
    uint64_t norm = 0;
279
1.02M
    uint32_t q, p;
280
1.02M
    int s, val;
281
1.02M
    int k0;
282
283
4.84M
    while (N > 2) {
284
        /*Lots of pulses case:*/
285
3.82M
        if (K >= N) {
286
1.12M
            const uint32_t *row = ff_celt_pvq_u_row[N];
287
288
            /* Are the pulses in this dimension negative? */
289
1.12M
            p  = row[K + 1];
290
1.12M
            s  = -(i >= p);
291
1.12M
            i -= p & s;
292
293
            /*Count how many pulses were placed in this dimension.*/
294
1.12M
            k0 = K;
295
1.12M
            q = row[N];
296
1.12M
            if (q > i) {
297
143k
                K = N;
298
223k
                do {
299
223k
                    p = ff_celt_pvq_u_row[--K][N];
300
223k
                } while (p > i);
301
143k
            } else
302
7.65M
                for (p = row[K]; p > i; p = row[K])
303
6.67M
                    K--;
304
305
1.12M
            i    -= p;
306
1.12M
            val   = (k0 - K + s) ^ s;
307
1.12M
            norm += val * val;
308
1.12M
            *y++  = val;
309
2.70M
        } else { /*Lots of dimensions case:*/
310
            /*Are there any pulses in this dimension at all?*/
311
2.70M
            p = ff_celt_pvq_u_row[K    ][N];
312
2.70M
            q = ff_celt_pvq_u_row[K + 1][N];
313
314
2.70M
            if (p <= i && i < q) {
315
2.13M
                i -= p;
316
2.13M
                *y++ = 0;
317
2.13M
            } else {
318
                /*Are the pulses in this dimension negative?*/
319
566k
                s  = -(i >= q);
320
566k
                i -= q & s;
321
322
                /*Count how many pulses were placed in this dimension.*/
323
566k
                k0 = K;
324
670k
                do p = ff_celt_pvq_u_row[--K][N];
325
670k
                while (p > i);
326
327
566k
                i    -= p;
328
566k
                val   = (k0 - K + s) ^ s;
329
566k
                norm += val * val;
330
566k
                *y++  = val;
331
566k
            }
332
2.70M
        }
333
3.82M
        N--;
334
3.82M
    }
335
336
    /* N == 2 */
337
1.02M
    p  = 2 * K + 1;
338
1.02M
    s  = -(i >= p);
339
1.02M
    i -= p & s;
340
1.02M
    k0 = K;
341
1.02M
    K  = (i + 1) / 2;
342
343
1.02M
    if (K)
344
776k
        i -= 2 * K - 1;
345
346
1.02M
    val   = (k0 - K + s) ^ s;
347
1.02M
    norm += val * val;
348
1.02M
    *y++  = val;
349
350
    /* N==1 */
351
1.02M
    s     = -i;
352
1.02M
    val   = (K + s) ^ s;
353
1.02M
    norm += val * val;
354
1.02M
    *y    = val;
355
356
1.02M
    return norm;
357
1.02M
}
358
359
static inline void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
360
0
{
361
0
    ff_opus_rc_enc_uint(rc, celt_icwrsi(N, K, y), CELT_PVQ_V(N, K));
362
0
}
363
364
static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
365
1.02M
{
366
1.02M
    const uint32_t idx = ff_opus_rc_dec_uint(rc, CELT_PVQ_V(N, K));
367
1.02M
    return celt_cwrsi(N, K, idx, y);
368
1.02M
}
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
0
{
377
0
    int i, y_norm = 0;
378
0
    float res = 0.0f, xy_norm = 0.0f;
379
380
0
    for (i = 0; i < N; i++)
381
0
        res += FFABS(X[i]);
382
383
0
    res = K/(res + FLT_EPSILON);
384
385
0
    for (i = 0; i < N; i++) {
386
0
        y[i] = lrintf(res*X[i]);
387
0
        y_norm  += y[i]*y[i];
388
0
        xy_norm += y[i]*X[i];
389
0
        K -= FFABS(y[i]);
390
0
    }
391
392
0
    while (K) {
393
0
        int max_idx = 0, phase = FFSIGN(K);
394
0
        float max_num = 0.0f;
395
0
        float max_den = 1.0f;
396
0
        y_norm += 1.0f;
397
398
0
        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
0
            const int ca = 1 ^ ((y[i] == 0) & (phase < 0));
403
0
            const int y_new = y_norm  + 2*phase*FFABS(y[i]);
404
0
            float xy_new = xy_norm + 1*phase*FFABS(X[i]);
405
0
            xy_new = xy_new * xy_new;
406
0
            if (ca && (max_den*xy_new) > (y_new*max_num)) {
407
0
                max_den = y_new;
408
0
                max_num = xy_new;
409
0
                max_idx = i;
410
0
            }
411
0
        }
412
413
0
        K -= phase;
414
415
0
        phase *= FFSIGN(X[max_idx]);
416
0
        xy_norm += 1*phase*X[max_idx];
417
0
        y_norm  += 2*phase*y[max_idx];
418
0
        y[max_idx] += phase;
419
0
    }
420
421
0
    return (float)y_norm;
422
0
}
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
0
{
429
0
    int *y = pvq->qcoeff;
430
431
0
    celt_exp_rotation(X, N, blocks, K, spread, 1);
432
0
    gain /= sqrtf(pvq->pvq_search(X, y, K, N));
433
0
    celt_encode_pulses(rc, y,  N, K);
434
0
    celt_normalize_residual(y, X, N, gain);
435
0
    celt_exp_rotation(X, N, blocks, K, spread, 0);
436
0
    return celt_extract_collapse_mask(y, N, blocks);
437
0
}
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
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
1.02M
{
445
1.02M
    int *y = pvq->qcoeff;
446
447
1.02M
    gain /= sqrtf(celt_decode_pulses(rc, y, N, K));
448
1.02M
    celt_normalize_residual(y, X, N, gain);
449
1.02M
    celt_exp_rotation(X, N, blocks, K, spread, 0);
450
1.02M
    return celt_extract_collapse_mask(y, N, blocks);
451
1.02M
}
452
453
static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
454
0
{
455
0
    int i;
456
0
    float e[2] = { 0.0f, 0.0f };
457
0
    if (coupling) { /* Coupling case */
458
0
        for (i = 0; i < N; i++) {
459
0
            e[0] += (X[i] + Y[i])*(X[i] + Y[i]);
460
0
            e[1] += (X[i] - Y[i])*(X[i] - Y[i]);
461
0
        }
462
0
    } else {
463
0
        for (i = 0; i < N; i++) {
464
0
            e[0] += X[i]*X[i];
465
0
            e[1] += Y[i]*Y[i];
466
0
        }
467
0
    }
468
0
    return lrintf(32768.0f*atan2f(sqrtf(e[1]), sqrtf(e[0]))/M_PI);
469
0
}
470
471
static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N)
472
0
{
473
0
    int i;
474
0
    const float energy_n = 1.0f/(sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON);
475
0
    e_l *= energy_n;
476
0
    e_r *= energy_n;
477
0
    for (i = 0; i < N; i++)
478
0
        X[i] = e_l*X[i] + e_r*Y[i];
479
0
}
480
481
static void celt_stereo_ms_decouple(float *X, float *Y, int N)
482
0
{
483
0
    int i;
484
0
    for (i = 0; i < N; i++) {
485
0
        const float Xret = X[i];
486
0
        X[i] = (X[i] + Y[i])*M_SQRT1_2;
487
0
        Y[i] = (Y[i] - Xret)*M_SQRT1_2;
488
0
    }
489
0
}
490
491
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
4.35M
{
501
4.35M
    int i;
502
4.35M
    const uint8_t *cache;
503
4.35M
    int stereo = !!Y, split = stereo;
504
4.35M
    int imid = 0, iside = 0;
505
4.35M
    uint32_t N0 = N;
506
4.35M
    int N_B = N / blocks;
507
4.35M
    int N_B0 = N_B;
508
4.35M
    int B0 = blocks;
509
4.35M
    int time_divide = 0;
510
4.35M
    int recombine = 0;
511
4.35M
    int inv = 0;
512
4.35M
    float mid = 0, side = 0;
513
4.35M
    int longblocks = (B0 == 1);
514
4.35M
    uint32_t cm = 0;
515
516
4.35M
    if (N == 1) {
517
90.9k
        float *x = X;
518
220k
        for (i = 0; i <= stereo; i++) {
519
129k
            int sign = 0;
520
129k
            if (f->remaining2 >= 1 << 3) {
521
35.4k
                if (quant) {
522
0
                    sign = x[0] < 0;
523
0
                    ff_opus_rc_put_raw(rc, sign, 1);
524
35.4k
                } else {
525
35.4k
                    sign = ff_opus_rc_get_raw(rc, 1);
526
35.4k
                }
527
35.4k
                f->remaining2 -= 1 << 3;
528
35.4k
            }
529
129k
            x[0] = 1.0f - 2.0f*sign;
530
129k
            x = Y;
531
129k
        }
532
90.9k
        if (lowband_out)
533
90.9k
            lowband_out[0] = X[0];
534
90.9k
        return 1;
535
90.9k
    }
536
537
4.26M
    if (!stereo && level == 0) {
538
2.59M
        int tf_change = f->tf_change[band];
539
2.59M
        int k;
540
2.59M
        if (tf_change > 0)
541
238k
            recombine = tf_change;
542
        /* Band recombining to increase frequency resolution */
543
544
2.59M
        if (lowband &&
545
1.59M
            (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
546
15.1M
            for (i = 0; i < N; i++)
547
14.6M
                lowband_scratch[i] = lowband[i];
548
522k
            lowband = lowband_scratch;
549
522k
        }
550
551
2.95M
        for (k = 0; k < recombine; k++) {
552
355k
            if (quant || lowband)
553
193k
                celt_haar1(quant ? X : lowband, N >> k, 1 << k);
554
355k
            fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2;
555
355k
        }
556
2.59M
        blocks >>= recombine;
557
2.59M
        N_B <<= recombine;
558
559
        /* Increasing the time resolution */
560
3.37M
        while ((N_B & 1) == 0 && tf_change < 0) {
561
776k
            if (quant || lowband)
562
457k
                celt_haar1(quant ? X : lowband, N_B, blocks);
563
776k
            fill |= fill << blocks;
564
776k
            blocks <<= 1;
565
776k
            N_B >>= 1;
566
776k
            time_divide++;
567
776k
            tf_change++;
568
776k
        }
569
2.59M
        B0 = blocks;
570
2.59M
        N_B0 = N_B;
571
572
        /* Reorganize the samples in time order instead of frequency order */
573
2.59M
        if (B0 > 1 && (quant || lowband))
574
438k
            celt_deinterleave_hadamard(pvq->hadamard_tmp, quant ? X : lowband,
575
438k
                                       N_B >> recombine, B0 << recombine,
576
438k
                                       longblocks);
577
2.59M
    }
578
579
    /* If we need 1.5 more bit than we can produce, split the band in two. */
580
4.26M
    cache = ff_celt_cache_bits +
581
4.26M
            ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band];
582
4.26M
    if (!stereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
583
344k
        N >>= 1;
584
344k
        Y = X + N;
585
344k
        split = 1;
586
344k
        duration -= 1;
587
344k
        if (blocks == 1)
588
265k
            fill = (fill & 1) | (fill << 1);
589
344k
        blocks = (blocks + 1) >> 1;
590
344k
    }
591
592
4.26M
    if (split) {
593
1.32M
        int qn;
594
1.32M
        int itheta = quant ? celt_calc_theta(X, Y, stereo, N) : 0;
595
1.32M
        int mbits, sbits, delta;
596
1.32M
        int qalloc;
597
1.32M
        int pulse_cap;
598
1.32M
        int offset;
599
1.32M
        int orig_fill;
600
1.32M
        int tell;
601
602
        /* Decide on the resolution to give to the split parameter theta */
603
1.32M
        pulse_cap = ff_celt_log_freq_range[band] + duration * 8;
604
1.32M
        offset = (pulse_cap >> 1) - (stereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
605
1.32M
                                                          CELT_QTHETA_OFFSET);
606
1.32M
        qn = (stereo && band >= f->intensity_stereo) ? 1 :
607
1.32M
             celt_compute_qn(N, b, offset, pulse_cap, stereo);
608
1.32M
        tell = opus_rc_tell_frac(rc);
609
1.32M
        if (qn != 1) {
610
408k
            if (quant)
611
0
                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
408k
            if (quant) {
615
0
                if (stereo && N > 2)
616
0
                    ff_opus_rc_enc_uint_step(rc, itheta, qn / 2);
617
0
                else if (stereo || B0 > 1)
618
0
                    ff_opus_rc_enc_uint(rc, itheta, qn + 1);
619
0
                else
620
0
                    ff_opus_rc_enc_uint_tri(rc, itheta, qn);
621
0
                itheta = itheta * 16384 / qn;
622
0
                if (stereo) {
623
0
                    if (itheta == 0)
624
0
                        celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
625
0
                                                f->block[1].lin_energy[band], N);
626
0
                    else
627
0
                        celt_stereo_ms_decouple(X, Y, N);
628
0
                }
629
408k
            } else {
630
408k
                if (stereo && N > 2)
631
54.9k
                    itheta = ff_opus_rc_dec_uint_step(rc, qn / 2);
632
353k
                else if (stereo || B0 > 1)
633
88.0k
                    itheta = ff_opus_rc_dec_uint(rc, qn+1);
634
265k
                else
635
265k
                    itheta = ff_opus_rc_dec_uint_tri(rc, qn);
636
408k
                itheta = itheta * 16384 / qn;
637
408k
            }
638
914k
        } else if (stereo) {
639
914k
            if (quant) {
640
0
                inv = f->apply_phase_inv ? itheta > 8192 : 0;
641
0
                 if (inv) {
642
0
                    for (i = 0; i < N; i++)
643
0
                       Y[i] *= -1;
644
0
                 }
645
0
                 celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
646
0
                                         f->block[1].lin_energy[band], N);
647
648
0
                if (b > 2 << 3 && f->remaining2 > 2 << 3) {
649
0
                    ff_opus_rc_enc_log(rc, inv, 2);
650
0
                } else {
651
0
                    inv = 0;
652
0
                }
653
914k
            } else {
654
914k
                inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0;
655
914k
                inv = f->apply_phase_inv ? inv : 0;
656
914k
            }
657
914k
            itheta = 0;
658
914k
        }
659
1.32M
        qalloc = opus_rc_tell_frac(rc) - tell;
660
1.32M
        b -= qalloc;
661
662
1.32M
        orig_fill = fill;
663
1.32M
        if (itheta == 0) {
664
937k
            imid = 32767;
665
937k
            iside = 0;
666
937k
            fill = av_zero_extend(fill, blocks);
667
937k
            delta = -16384;
668
937k
        } else if (itheta == 16384) {
669
9.39k
            imid = 0;
670
9.39k
            iside = 32767;
671
9.39k
            fill &= ((1 << blocks) - 1) << blocks;
672
9.39k
            delta = 16384;
673
375k
        } else {
674
375k
            imid = celt_cos(itheta);
675
375k
            iside = celt_cos(16384-itheta);
676
            /* This is the mid vs side allocation that minimizes squared error
677
            in that band. */
678
375k
            delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid));
679
375k
        }
680
681
1.32M
        mid  = imid  / 32768.0f;
682
1.32M
        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
1.32M
        if (N == 2 && stereo) {
688
123k
            int c;
689
123k
            int sign = 0;
690
123k
            float tmp;
691
123k
            float *x2, *y2;
692
123k
            mbits = b;
693
            /* Only need one bit for the side */
694
123k
            sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
695
123k
            mbits -= sbits;
696
123k
            c = (itheta > 8192);
697
123k
            f->remaining2 -= qalloc+sbits;
698
699
123k
            x2 = c ? Y : X;
700
123k
            y2 = c ? X : Y;
701
123k
            if (sbits) {
702
7.35k
                if (quant) {
703
0
                    sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
704
0
                    ff_opus_rc_put_raw(rc, sign, 1);
705
7.35k
                } else {
706
7.35k
                    sign = ff_opus_rc_get_raw(rc, 1);
707
7.35k
                }
708
7.35k
            }
709
123k
            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
123k
            cm = pvq->quant_band(pvq, f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration,
713
123k
                                 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
123k
            y2[0] = -sign * x2[1];
717
123k
            y2[1] =  sign * x2[0];
718
123k
            X[0] *= mid;
719
123k
            X[1] *= mid;
720
123k
            Y[0] *= side;
721
123k
            Y[1] *= side;
722
123k
            tmp = X[0];
723
123k
            X[0] = tmp - Y[0];
724
123k
            Y[0] = tmp + Y[0];
725
123k
            tmp = X[1];
726
123k
            X[1] = tmp - Y[1];
727
123k
            Y[1] = tmp + Y[1];
728
1.19M
        } else {
729
            /* "Normal" split code */
730
1.19M
            float *next_lowband2     = NULL;
731
1.19M
            float *next_lowband_out1 = NULL;
732
1.19M
            int next_level = 0;
733
1.19M
            int rebalance;
734
1.19M
            uint32_t cmt;
735
736
            /* Give more bits to low-energy MDCTs than they would
737
             * otherwise deserve */
738
1.19M
            if (B0 > 1 && !stereo && (itheta & 0x3fff)) {
739
73.6k
                if (itheta > 8192)
740
                    /* Rough approximation for pre-echo masking */
741
38.5k
                    delta -= delta >> (4 - duration);
742
35.1k
                else
743
                    /* Corresponds to a forward-masking slope of
744
                     * 1.5 dB per 10 ms */
745
35.1k
                    delta = FFMIN(0, delta + (N << 3 >> (5 - duration)));
746
73.6k
            }
747
1.19M
            mbits = av_clip((b - delta) / 2, 0, b);
748
1.19M
            sbits = b - mbits;
749
1.19M
            f->remaining2 -= qalloc;
750
751
1.19M
            if (lowband && !stereo)
752
294k
                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
1.19M
            if (stereo)
757
853k
                next_lowband_out1 = lowband_out;
758
344k
            else
759
344k
                next_level = level + 1;
760
761
1.19M
            rebalance = f->remaining2;
762
1.19M
            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
1.00M
                cm = pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks,
766
1.00M
                                     lowband, duration, next_lowband_out1, next_level,
767
1.00M
                                     stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
768
1.00M
                rebalance = mbits - (rebalance - f->remaining2);
769
1.00M
                if (rebalance > 3 << 3 && itheta != 0)
770
84.1k
                    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
1.00M
                cmt = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks,
775
1.00M
                                      next_lowband2, duration, NULL, next_level,
776
1.00M
                                      gain * side, NULL, fill >> blocks);
777
1.00M
                cm |= cmt << ((B0 >> 1) & (stereo - 1));
778
1.00M
            } 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
195k
                cm = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks,
782
195k
                                     next_lowband2, duration, NULL, next_level,
783
195k
                                     gain * side, NULL, fill >> blocks);
784
195k
                cm <<= ((B0 >> 1) & (stereo - 1));
785
195k
                rebalance = sbits - (rebalance - f->remaining2);
786
195k
                if (rebalance > 3 << 3 && itheta != 16384)
787
98.7k
                    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
195k
                cm |= pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks,
792
195k
                                      lowband, duration, next_lowband_out1, next_level,
793
195k
                                      stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
794
195k
            }
795
1.19M
        }
796
2.94M
    } else {
797
        /* This is the basic no-split case */
798
2.94M
        uint32_t q         = celt_bits2pulses(cache, b);
799
2.94M
        uint32_t curr_bits = celt_pulses2bits(cache, q);
800
2.94M
        f->remaining2 -= curr_bits;
801
802
        /* Ensures we can never bust the budget */
803
2.96M
        while (f->remaining2 < 0 && q > 0) {
804
16.9k
            f->remaining2 += curr_bits;
805
16.9k
            curr_bits      = celt_pulses2bits(cache, --q);
806
16.9k
            f->remaining2 -= curr_bits;
807
16.9k
        }
808
809
2.94M
        if (q != 0) {
810
            /* Finally do the actual (de)quantization */
811
1.02M
            if (quant) {
812
0
                cm = celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
813
0
                                    f->spread, blocks, gain, pvq);
814
1.02M
            } else {
815
1.02M
                cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
816
1.02M
                                      f->spread, blocks, gain, pvq);
817
1.02M
            }
818
1.92M
        } else {
819
            /* If there's no pulse, fill the band anyway */
820
1.92M
            uint32_t cm_mask = (1 << blocks) - 1;
821
1.92M
            fill &= cm_mask;
822
1.92M
            if (fill) {
823
1.08M
                if (!lowband) {
824
                    /* Noise */
825
913k
                    for (i = 0; i < N; i++)
826
871k
                        X[i] = (((int32_t)celt_rng(f)) >> 20);
827
42.5k
                    cm = cm_mask;
828
1.04M
                } else {
829
                    /* Folded spectrum */
830
29.3M
                    for (i = 0; i < N; i++) {
831
                        /* About 48 dB below the "normal" folding level */
832
28.3M
                        X[i] = lowband[i] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
833
28.3M
                    }
834
1.04M
                    cm = fill;
835
1.04M
                }
836
1.08M
                celt_renormalize_vector(X, N, gain);
837
1.08M
            } else {
838
833k
                memset(X, 0, N*sizeof(float));
839
833k
            }
840
1.92M
        }
841
2.94M
    }
842
843
    /* This code is used by the decoder and by the resynthesis-enabled encoder */
844
4.26M
    if (stereo) {
845
977k
        if (N > 2)
846
853k
            celt_stereo_merge(X, Y, mid, N);
847
977k
        if (inv) {
848
853k
            for (i = 0; i < N; i++)
849
809k
                Y[i] *= -1;
850
43.7k
        }
851
3.28M
    } else if (level == 0) {
852
2.59M
        int k;
853
854
        /* Undo the sample reorganization going from time order to frequency order */
855
2.59M
        if (B0 > 1)
856
720k
            celt_interleave_hadamard(pvq->hadamard_tmp, X, N_B >> recombine,
857
720k
                                     B0 << recombine, longblocks);
858
859
        /* Undo time-freq changes that we did earlier */
860
2.59M
        N_B = N_B0;
861
2.59M
        blocks = B0;
862
3.37M
        for (k = 0; k < time_divide; k++) {
863
776k
            blocks >>= 1;
864
776k
            N_B <<= 1;
865
776k
            cm |= cm >> blocks;
866
776k
            celt_haar1(X, N_B, blocks);
867
776k
        }
868
869
2.95M
        for (k = 0; k < recombine; k++) {
870
355k
            cm = ff_celt_bit_deinterleave[cm];
871
355k
            celt_haar1(X, N0>>k, 1<<k);
872
355k
        }
873
2.59M
        blocks <<= recombine;
874
875
        /* Scale output for later folding */
876
2.59M
        if (lowband_out) {
877
1.74M
            float n = sqrtf(N0);
878
36.5M
            for (i = 0; i < N0; i++)
879
34.8M
                lowband_out[i] = n * X[i];
880
1.74M
        }
881
2.59M
        cm = av_zero_extend(cm, blocks);
882
2.59M
    }
883
884
4.26M
    return cm;
885
4.35M
}
886
887
static QUANT_FN(pvq_decode_band)
888
4.35M
{
889
4.35M
#if CONFIG_OPUS_DECODER
890
4.35M
    return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration,
891
4.35M
                               lowband_out, level, gain, lowband_scratch, fill, 0);
892
#else
893
    return 0;
894
#endif
895
4.35M
}
896
897
static QUANT_FN(pvq_encode_band)
898
0
{
899
0
#if CONFIG_OPUS_ENCODER
900
0
    return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration,
901
0
                               lowband_out, level, gain, lowband_scratch, fill, 1);
902
#else
903
    return 0;
904
#endif
905
0
}
906
907
int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode)
908
72.4k
{
909
72.4k
    CeltPVQ *s = av_malloc(sizeof(CeltPVQ));
910
72.4k
    if (!s)
911
0
        return AVERROR(ENOMEM);
912
913
72.4k
    s->quant_band = encode ? pvq_encode_band : pvq_decode_band;
914
915
72.4k
#if CONFIG_OPUS_ENCODER
916
72.4k
    s->pvq_search = ppp_pvq_search_c;
917
72.4k
#if ARCH_X86
918
72.4k
    ff_celt_pvq_init_x86(s);
919
72.4k
#endif
920
72.4k
#endif
921
922
72.4k
    *pvq = s;
923
924
72.4k
    return 0;
925
72.4k
}
926
927
void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
928
72.4k
{
929
72.4k
    av_freep(pvq);
930
72.4k
}