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>
9 * This file is part of FFmpeg.
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.
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.
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
26 #include "config_components.h"
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))
34 static inline int16_t celt_cos(int16_t x
)
36 x
= (MUL16(x
, x
) + 4096) >> 13;
37 x
= (32767-x
) + ROUND_MUL16(x
, (-7651 + ROUND_MUL16(x
, (8277 + ROUND_MUL16(-626, x
)))));
41 static inline int celt_log2tan(int isin
, int icos
)
48 return (ls
<< 11) - (lc
<< 11) +
49 ROUND_MUL16(isin
, ROUND_MUL16(isin
, -2597) + 7932) -
50 ROUND_MUL16(icos
, ROUND_MUL16(icos
, -2597) + 7932);
53 static inline int celt_bits2pulses(const uint8_t *cache
, int bits
)
55 // TODO: Find the size of cache and make it into an array in the parameters list
61 for (i
= 0; i
< 6; i
++) {
62 int center
= (low
+ high
+ 1) >> 1;
63 if (cache
[center
] >= bits
)
69 return (bits
- (low
== 0 ? -1 : cache
[low
]) <= cache
[high
] - bits
) ? low
: high
;
72 static inline int celt_pulses2bits(const uint8_t *cache
, int pulses
)
74 // TODO: Find the size of cache and make it into an array in the parameters list
75 return (pulses
== 0) ? 0 : cache
[pulses
] + 1;
78 static inline void celt_normalize_residual(const int * av_restrict iy
, float * av_restrict X
,
82 for (i
= 0; i
< N
; i
++)
86 static void celt_exp_rotation_impl(float *X
, uint32_t len
, uint32_t stride
,
93 for (i
= 0; i
< len
- stride
; i
++) {
95 float x2
= Xptr
[stride
];
96 Xptr
[stride
] = c
* x2
+ s
* x1
;
97 *Xptr
++ = c
* x1
- s
* x2
;
100 Xptr
= &X
[len
- 2 * stride
- 1];
101 for (i
= len
- 2 * stride
- 1; i
>= 0; i
--) {
103 float x2
= Xptr
[stride
];
104 Xptr
[stride
] = c
* x2
+ s
* x1
;
105 *Xptr
-- = c
* x1
- s
* x2
;
109 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
)
113 uint32_t stride2
= 0;
118 if (2*K
>= len
|| spread
== CELT_SPREAD_NONE
)
121 gain
= (float)len
/ (len
+ (20 - 5*spread
) * K
);
122 theta
= M_PI
* gain
* gain
/ 4;
127 if (len
>= stride
<< 3) {
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 while ((stride2
* stride2
+ stride2
) * stride
+ (stride
>> 2) < len
)
136 for (i
= 0; i
< stride
; i
++) {
138 celt_exp_rotation_impl(X
+ i
* len
, len
, 1, c
, -s
);
140 celt_exp_rotation_impl(X
+ i
* len
, len
, stride2
, s
, -c
);
143 celt_exp_rotation_impl(X
+ i
* len
, len
, stride2
, s
, c
);
144 celt_exp_rotation_impl(X
+ i
* len
, len
, 1, c
, s
);
149 static inline uint32_t celt_extract_collapse_mask(const int *iy
, uint32_t N
, uint32_t B
)
151 int i
, j
, N0
= N
/ B
;
152 uint32_t collapse_mask
= 0;
157 for (i
= 0; i
< B
; i
++)
158 for (j
= 0; j
< N0
; j
++)
159 collapse_mask
|= (!!iy
[i
*N0
+j
]) << i
;
160 return collapse_mask
;
163 static inline void celt_stereo_merge(float *X
, float *Y
, float mid
, int N
)
166 float xp
= 0, side
= 0;
171 /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
172 for (i
= 0; i
< N
; i
++) {
177 /* Compensating for the mid normalization */
180 E
[0] = mid2
* mid2
+ side
- 2 * xp
;
181 E
[1] = mid2
* mid2
+ side
+ 2 * xp
;
182 if (E
[0] < 6e-4f
|| E
[1] < 6e-4f
) {
183 for (i
= 0; i
< N
; i
++)
188 gain
[0] = 1.0f
/ sqrtf(E
[0]);
189 gain
[1] = 1.0f
/ sqrtf(E
[1]);
191 for (i
= 0; i
< N
; i
++) {
193 /* Apply mid scaling (side is already scaled) */
194 value
[0] = mid
* X
[i
];
196 X
[i
] = gain
[0] * (value
[0] - value
[1]);
197 Y
[i
] = gain
[1] * (value
[0] + value
[1]);
201 static void celt_interleave_hadamard(float *tmp
, float *X
, int N0
,
202 int stride
, int hadamard
)
204 int i
, j
, N
= N0
*stride
;
205 const uint8_t *order
= &ff_celt_hadamard_order
[hadamard
? stride
- 2 : 30];
207 for (i
= 0; i
< stride
; i
++)
208 for (j
= 0; j
< N0
; j
++)
209 tmp
[j
*stride
+i
] = X
[order
[i
]*N0
+j
];
211 memcpy(X
, tmp
, N
*sizeof(float));
214 static void celt_deinterleave_hadamard(float *tmp
, float *X
, int N0
,
215 int stride
, int hadamard
)
217 int i
, j
, N
= N0
*stride
;
218 const uint8_t *order
= &ff_celt_hadamard_order
[hadamard
? stride
- 2 : 30];
220 for (i
= 0; i
< stride
; i
++)
221 for (j
= 0; j
< N0
; j
++)
222 tmp
[order
[i
]*N0
+j
] = X
[j
*stride
+i
];
224 memcpy(X
, tmp
, N
*sizeof(float));
227 static void celt_haar1(float *X
, int N0
, int stride
)
231 for (i
= 0; i
< stride
; i
++) {
232 for (j
= 0; j
< N0
; j
++) {
233 float x0
= X
[stride
* (2 * j
+ 0) + i
];
234 float x1
= X
[stride
* (2 * j
+ 1) + i
];
235 X
[stride
* (2 * j
+ 0) + i
] = (x0
+ x1
) * M_SQRT1_2
;
236 X
[stride
* (2 * j
+ 1) + i
] = (x0
- x1
) * M_SQRT1_2
;
241 static inline int celt_compute_qn(int N
, int b
, int offset
, int pulse_cap
,
246 if (stereo
&& N
== 2)
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 qb
= FFMIN3(b
- pulse_cap
- (4 << 3), (b
+ N2
* offset
) / N2
, 8 << 3);
253 qn
= (qb
< (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2
[qb
& 0x7] >> (14 - (qb
>> 3))) + 1) >> 1 << 1;
257 /* Convert the quantized vector to an index */
258 static inline uint32_t celt_icwrsi(uint32_t N
, uint32_t K
, const int *y
)
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
;
269 // this code was adapted from libopus
270 static inline uint64_t celt_cwrsi(uint32_t N
, uint32_t K
, uint32_t i
, int *y
)
278 /*Lots of pulses case:*/
280 const uint32_t *row
= ff_celt_pvq_u_row
[N
];
282 /* Are the pulses in this dimension negative? */
287 /*Count how many pulses were placed in this dimension.*/
293 p
= ff_celt_pvq_u_row
[--K
][N
];
296 for (p
= row
[K
]; p
> i
; p
= row
[K
])
300 val
= (k0
- K
+ s
) ^ s
;
303 } else { /*Lots of dimensions case:*/
304 /*Are there any pulses in this dimension at all?*/
305 p
= ff_celt_pvq_u_row
[K
][N
];
306 q
= ff_celt_pvq_u_row
[K
+ 1][N
];
308 if (p
<= i
&& i
< q
) {
312 /*Are the pulses in this dimension negative?*/
316 /*Count how many pulses were placed in this dimension.*/
318 do p
= ff_celt_pvq_u_row
[--K
][N
];
322 val
= (k0
- K
+ s
) ^ s
;
340 val
= (k0
- K
+ s
) ^ s
;
353 static inline void celt_encode_pulses(OpusRangeCoder
*rc
, int *y
, uint32_t N
, uint32_t K
)
355 ff_opus_rc_enc_uint(rc
, celt_icwrsi(N
, K
, y
), CELT_PVQ_V(N
, K
));
358 static inline float celt_decode_pulses(OpusRangeCoder
*rc
, int *y
, uint32_t N
, uint32_t K
)
360 const uint32_t idx
= ff_opus_rc_dec_uint(rc
, CELT_PVQ_V(N
, K
));
361 return celt_cwrsi(N
, K
, idx
, y
);
365 * Faster than libopus's search, operates entirely in the signed domain.
366 * Slightly worse/better depending on N, K and the input vector.
368 static float ppp_pvq_search_c(float *X
, int *y
, int K
, int N
)
371 float res
= 0.0f
, xy_norm
= 0.0f
;
373 for (i
= 0; i
< N
; i
++)
376 res
= K
/(res
+ FLT_EPSILON
);
378 for (i
= 0; i
< N
; i
++) {
379 y
[i
] = lrintf(res
*X
[i
]);
381 xy_norm
+= y
[i
]*X
[i
];
386 int max_idx
= 0, phase
= FFSIGN(K
);
387 float max_num
= 0.0f
;
388 float max_den
= 1.0f
;
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
)) {
408 phase
*= FFSIGN(X
[max_idx
]);
409 xy_norm
+= 1*phase
*X
[max_idx
];
410 y_norm
+= 2*phase
*y
[max_idx
];
414 return (float)y_norm
;
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
,
421 int *y
= pvq
->qcoeff
;
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
);
431 /** Decode pulse vector and combine the result with the pitch vector to produce
432 the final normalised signal in the current band. */
433 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
,
437 int *y
= pvq
->qcoeff
;
439 gain
/= sqrtf(celt_decode_pulses(rc
, y
, N
, K
));
440 celt_normalize_residual(y
, X
, N
, gain
);
441 celt_exp_rotation(X
, N
, blocks
, K
, spread
, 0);
442 return celt_extract_collapse_mask(y
, N
, blocks
);
445 static int celt_calc_theta(const float *X
, const float *Y
, int coupling
, int N
)
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
]);
455 for (i
= 0; i
< N
; i
++) {
460 return lrintf(32768.0f
*atan2f(sqrtf(e
[1]), sqrtf(e
[0]))/M_PI
);
463 static void celt_stereo_is_decouple(float *X
, float *Y
, float e_l
, float e_r
, int N
)
466 const float energy_n
= 1.0f
/(sqrtf(e_l
*e_l
+ e_r
*e_r
) + FLT_EPSILON
);
469 for (i
= 0; i
< N
; i
++)
470 X
[i
] = e_l
*X
[i
] + e_r
*Y
[i
];
473 static void celt_stereo_ms_decouple(float *X
, float *Y
, int N
)
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
;
483 static av_always_inline
uint32_t quant_band_template(CeltPVQ
*pvq
, CeltFrame
*f
,
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
,
494 const uint8_t *cache
;
495 int stereo
= !!Y
, split
= stereo
;
496 int imid
= 0, iside
= 0;
498 int N_B
= N
/ blocks
;
504 float mid
= 0, side
= 0;
505 int longblocks
= (B0
== 1);
510 for (i
= 0; i
<= stereo
; i
++) {
512 if (f
->remaining2
>= 1 << 3) {
515 ff_opus_rc_put_raw(rc
, sign
, 1);
517 sign
= ff_opus_rc_get_raw(rc
, 1);
519 f
->remaining2
-= 1 << 3;
521 x
[0] = 1.0f
- 2.0f
*sign
;
525 lowband_out
[0] = X
[0];
529 if (!stereo
&& level
== 0) {
530 int tf_change
= f
->tf_change
[band
];
533 recombine
= tf_change
;
534 /* Band recombining to increase frequency resolution */
537 (recombine
|| ((N_B
& 1) == 0 && tf_change
< 0) || B0
> 1)) {
538 for (i
= 0; i
< N
; i
++)
539 lowband_scratch
[i
] = lowband
[i
];
540 lowband
= lowband_scratch
;
543 for (k
= 0; k
< recombine
; k
++) {
544 if (quant
|| lowband
)
545 celt_haar1(quant
? X
: lowband
, N
>> k
, 1 << k
);
546 fill
= ff_celt_bit_interleave
[fill
& 0xF] | ff_celt_bit_interleave
[fill
>> 4] << 2;
548 blocks
>>= recombine
;
551 /* Increasing the time resolution */
552 while ((N_B
& 1) == 0 && tf_change
< 0) {
553 if (quant
|| lowband
)
554 celt_haar1(quant
? X
: lowband
, N_B
, blocks
);
555 fill
|= fill
<< blocks
;
564 /* Reorganize the samples in time order instead of frequency order */
565 if (B0
> 1 && (quant
|| lowband
))
566 celt_deinterleave_hadamard(pvq
->hadamard_tmp
, quant
? X
: lowband
,
567 N_B
>> recombine
, B0
<< recombine
,
571 /* If we need 1.5 more bit than we can produce, split the band in two. */
572 cache
= ff_celt_cache_bits
+
573 ff_celt_cache_index
[(duration
+ 1) * CELT_MAX_BANDS
+ band
];
574 if (!stereo
&& duration
>= 0 && b
> cache
[cache
[0]] + 12 && N
> 2) {
580 fill
= (fill
& 1) | (fill
<< 1);
581 blocks
= (blocks
+ 1) >> 1;
586 int itheta
= quant
? celt_calc_theta(X
, Y
, stereo
, N
) : 0;
587 int mbits
, sbits
, delta
;
594 /* Decide on the resolution to give to the split parameter theta */
595 pulse_cap
= ff_celt_log_freq_range
[band
] + duration
* 8;
596 offset
= (pulse_cap
>> 1) - (stereo
&& N
== 2 ? CELT_QTHETA_OFFSET_TWOPHASE
:
598 qn
= (stereo
&& band
>= f
->intensity_stereo
) ? 1 :
599 celt_compute_qn(N
, b
, offset
, pulse_cap
, stereo
);
600 tell
= opus_rc_tell_frac(rc
);
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. */
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);
612 ff_opus_rc_enc_uint_tri(rc
, itheta
, qn
);
613 itheta
= itheta
* 16384 / qn
;
616 celt_stereo_is_decouple(X
, Y
, f
->block
[0].lin_energy
[band
],
617 f
->block
[1].lin_energy
[band
], N
);
619 celt_stereo_ms_decouple(X
, Y
, N
);
623 itheta
= ff_opus_rc_dec_uint_step(rc
, qn
/ 2);
624 else if (stereo
|| B0
> 1)
625 itheta
= ff_opus_rc_dec_uint(rc
, qn
+1);
627 itheta
= ff_opus_rc_dec_uint_tri(rc
, qn
);
628 itheta
= itheta
* 16384 / qn
;
632 inv
= f
->apply_phase_inv
? itheta
> 8192 : 0;
634 for (i
= 0; i
< N
; i
++)
637 celt_stereo_is_decouple(X
, Y
, f
->block
[0].lin_energy
[band
],
638 f
->block
[1].lin_energy
[band
], N
);
640 if (b
> 2 << 3 && f
->remaining2
> 2 << 3) {
641 ff_opus_rc_enc_log(rc
, inv
, 2);
646 inv
= (b
> 2 << 3 && f
->remaining2
> 2 << 3) ? ff_opus_rc_dec_log(rc
, 2) : 0;
647 inv
= f
->apply_phase_inv
? inv
: 0;
651 qalloc
= opus_rc_tell_frac(rc
) - tell
;
658 fill
= av_mod_uintp2(fill
, blocks
);
660 } else if (itheta
== 16384) {
663 fill
&= ((1 << blocks
) - 1) << blocks
;
666 imid
= celt_cos(itheta
);
667 iside
= celt_cos(16384-itheta
);
668 /* This is the mid vs side allocation that minimizes squared error
670 delta
= ROUND_MUL16((N
- 1) << 7, celt_log2tan(iside
, imid
));
673 mid
= imid
/ 32768.0f
;
674 side
= iside
/ 32768.0f
;
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 if (N
== 2 && stereo
) {
685 /* Only need one bit for the side */
686 sbits
= (itheta
!= 0 && itheta
!= 16384) ? 1 << 3 : 0;
689 f
->remaining2
-= qalloc
+sbits
;
695 sign
= x2
[0]*y2
[1] - x2
[1]*y2
[0] < 0;
696 ff_opus_rc_put_raw(rc
, sign
, 1);
698 sign
= ff_opus_rc_get_raw(rc
, 1);
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 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 y2
[0] = -sign
* x2
[1];
709 y2
[1] = sign
* x2
[0];
721 /* "Normal" split code */
722 float *next_lowband2
= NULL
;
723 float *next_lowband_out1
= NULL
;
728 /* Give more bits to low-energy MDCTs than they would
729 * otherwise deserve */
730 if (B0
> 1 && !stereo
&& (itheta
& 0x3fff)) {
732 /* Rough approximation for pre-echo masking */
733 delta
-= delta
>> (4 - duration
);
735 /* Corresponds to a forward-masking slope of
736 * 1.5 dB per 10 ms */
737 delta
= FFMIN(0, delta
+ (N
<< 3 >> (5 - duration
)));
739 mbits
= av_clip((b
- delta
) / 2, 0, b
);
741 f
->remaining2
-= qalloc
;
743 if (lowband
&& !stereo
)
744 next_lowband2
= lowband
+ N
; /* >32-bit split case */
746 /* Only stereo needs to pass on lowband_out.
747 * Otherwise, it's handled at the end */
749 next_lowband_out1
= lowband_out
;
751 next_level
= level
+ 1;
753 rebalance
= f
->remaining2
;
754 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 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 rebalance
= mbits
- (rebalance
- f
->remaining2
);
761 if (rebalance
> 3 << 3 && itheta
!= 0)
762 sbits
+= rebalance
- (3 << 3);
764 /* For a stereo split, the high bits of fill are always zero,
765 * so no folding will be done to the side. */
766 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 cm
|= cmt
<< ((B0
>> 1) & (stereo
- 1));
771 /* For a stereo split, the high bits of fill are always zero,
772 * so no folding will be done to the side. */
773 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 cm
<<= ((B0
>> 1) & (stereo
- 1));
777 rebalance
= sbits
- (rebalance
- f
->remaining2
);
778 if (rebalance
> 3 << 3 && itheta
!= 16384)
779 mbits
+= rebalance
- (3 << 3);
781 /* In stereo mode, we do not apply a scaling to the mid because
782 * we need the normalized mid for folding later */
783 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
);
789 /* This is the basic no-split case */
790 uint32_t q
= celt_bits2pulses(cache
, b
);
791 uint32_t curr_bits
= celt_pulses2bits(cache
, q
);
792 f
->remaining2
-= curr_bits
;
794 /* Ensures we can never bust the budget */
795 while (f
->remaining2
< 0 && q
> 0) {
796 f
->remaining2
+= curr_bits
;
797 curr_bits
= celt_pulses2bits(cache
, --q
);
798 f
->remaining2
-= curr_bits
;
802 /* Finally do the actual (de)quantization */
804 cm
= celt_alg_quant(rc
, X
, N
, (q
< 8) ? q
: (8 + (q
& 7)) << ((q
>> 3) - 1),
805 f
->spread
, blocks
, gain
, pvq
);
807 cm
= celt_alg_unquant(rc
, X
, N
, (q
< 8) ? q
: (8 + (q
& 7)) << ((q
>> 3) - 1),
808 f
->spread
, blocks
, gain
, pvq
);
811 /* If there's no pulse, fill the band anyway */
812 uint32_t cm_mask
= (1 << blocks
) - 1;
817 for (i
= 0; i
< N
; i
++)
818 X
[i
] = (((int32_t)celt_rng(f
)) >> 20);
821 /* Folded spectrum */
822 for (i
= 0; i
< N
; i
++) {
823 /* About 48 dB below the "normal" folding level */
824 X
[i
] = lowband
[i
] + (((celt_rng(f
)) & 0x8000) ? 1.0f
/ 256 : -1.0f
/ 256);
828 celt_renormalize_vector(X
, N
, gain
);
830 memset(X
, 0, N
*sizeof(float));
835 /* This code is used by the decoder and by the resynthesis-enabled encoder */
838 celt_stereo_merge(X
, Y
, mid
, N
);
840 for (i
= 0; i
< N
; i
++)
843 } else if (level
== 0) {
846 /* Undo the sample reorganization going from time order to frequency order */
848 celt_interleave_hadamard(pvq
->hadamard_tmp
, X
, N_B
>> recombine
,
849 B0
<< recombine
, longblocks
);
851 /* Undo time-freq changes that we did earlier */
854 for (k
= 0; k
< time_divide
; k
++) {
858 celt_haar1(X
, N_B
, blocks
);
861 for (k
= 0; k
< recombine
; k
++) {
862 cm
= ff_celt_bit_deinterleave
[cm
];
863 celt_haar1(X
, N0
>>k
, 1<<k
);
865 blocks
<<= recombine
;
867 /* Scale output for later folding */
870 for (i
= 0; i
< N0
; i
++)
871 lowband_out
[i
] = n
* X
[i
];
873 cm
= av_mod_uintp2(cm
, blocks
);
879 static QUANT_FN(pvq_decode_band
)
881 #if CONFIG_OPUS_DECODER
882 return quant_band_template(pvq
, f
, rc
, band
, X
, Y
, N
, b
, blocks
, lowband
, duration
,
883 lowband_out
, level
, gain
, lowband_scratch
, fill
, 0);
889 static QUANT_FN(pvq_encode_band
)
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);
899 int av_cold
ff_celt_pvq_init(CeltPVQ
**pvq
, int encode
)
901 CeltPVQ
*s
= av_malloc(sizeof(CeltPVQ
));
903 return AVERROR(ENOMEM
);
905 s
->pvq_search
= ppp_pvq_search_c
;
906 s
->quant_band
= encode
? pvq_encode_band
: pvq_decode_band
;
908 #if CONFIG_OPUS_ENCODER && ARCH_X86
909 ff_celt_pvq_init_x86(s
);
917 void av_cold
ff_celt_pvq_uninit(CeltPVQ
**pvq
)