28#include "config_components.h"
35#define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15)
37#define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
38#define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
42 x = (
MUL16(x, x) + 4096) >> 13;
54 return (ls << 11) - (lc << 11) +
67 for (
i = 0;
i < 6;
i++) {
68 int center = (low +
high + 1) >> 1;
69 if (cache[center] >=
bits)
75 return (
bits - (low == 0 ? -1 : cache[low]) <= cache[
high] -
bits) ? low :
high;
88 for (
i = 0;
i <
N;
i++)
103 *Xptr++ =
c * x1 -
s * x2;
111 *Xptr-- =
c * x1 -
s * x2;
119 uint32_t stride2 = 0;
128 theta =
M_PI * gain * gain / 4;
157 int i, j,
N0 =
N /
B;
158 uint32_t collapse_mask = 0;
163 for (
i = 0;
i <
B;
i++)
164 for (j = 0; j <
N0; j++)
165 collapse_mask |= (!!iy[
i*
N0+j]) <<
i;
166 return collapse_mask;
172 float xp = 0, side = 0;
178 for (
i = 0;
i <
N;
i++) {
186 E[0] = mid2 * mid2 + side - 2 * xp;
187 E[1] = mid2 * mid2 + side + 2 * xp;
188 if (
E[0] < 6e-4f ||
E[1] < 6e-4f) {
189 for (
i = 0;
i <
N;
i++)
194 gain[0] = 1.0f /
sqrtf(
E[0]);
195 gain[1] = 1.0f /
sqrtf(
E[1]);
197 for (
i = 0;
i <
N;
i++) {
214 for (j = 0; j <
N0; j++)
217 memcpy(
X,
tmp,
N*
sizeof(
float));
227 for (j = 0; j <
N0; j++)
230 memcpy(
X,
tmp,
N*
sizeof(
float));
238 for (j = 0; j <
N0; j++) {
239 float x0 =
X[
stride * (2 * j + 0) +
i];
240 float x1 =
X[
stride * (2 * j + 1) +
i];
252 if (stereo &&
N == 2)
259 qn = (qb < (1 << 3 >> 1)) ? 1 : ((
ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
266 int i, idx = 0, sum = 0;
267 for (
i =
N - 1;
i >= 0;
i--) {
276static inline uint64_t
celt_cwrsi(uint32_t
N, uint32_t
K, uint32_t
i,
int *y)
302 for (p = row[
K]; p >
i; p = row[
K])
314 if (p <=
i &&
i < q) {
370#if CONFIG_OPUS_ENCODER
375static float ppp_pvq_search_c(
float *
X,
int *y,
int K,
int N)
378 float res = 0.0f, xy_norm = 0.0f;
380 for (
i = 0;
i <
N;
i++)
383 res =
K/(res + FLT_EPSILON);
385 for (
i = 0;
i <
N;
i++) {
388 xy_norm += y[
i]*
X[
i];
393 int max_idx = 0, phase =
FFSIGN(
K);
394 float max_num = 0.0f;
395 float max_den = 1.0f;
398 for (
i = 0;
i <
N;
i++) {
402 const int ca = 1 ^ ((y[
i] == 0) & (phase < 0));
403 const int y_new = y_norm + 2*phase*
FFABS(y[
i]);
404 float xy_new = xy_norm + 1*phase*
FFABS(
X[
i]);
405 xy_new = xy_new * xy_new;
406 if (ca && (max_den*xy_new) > (y_new*max_num)) {
416 xy_norm += 1*phase*
X[max_idx];
417 y_norm += 2*phase*y[max_idx];
421 return (
float)y_norm;
426 enum CeltSpread spread, uint32_t blocks,
float gain,
442 enum CeltSpread spread, uint32_t blocks,
float gain,
456 float e[2] = { 0.0f, 0.0f };
458 for (
i = 0;
i <
N;
i++) {
459 e[0] += (
X[
i] +
Y[
i])*(
X[
i] +
Y[
i]);
460 e[1] += (
X[
i] -
Y[
i])*(
X[
i] -
Y[
i]);
463 for (
i = 0;
i <
N;
i++) {
474 const float energy_n = 1.0f/(
sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON);
477 for (
i = 0;
i <
N;
i++)
478 X[
i] = e_l*
X[
i] + e_r*
Y[
i];
484 for (
i = 0;
i <
N;
i++) {
485 const float Xret =
X[
i];
493 const int band,
float *
X,
494 float *
Y,
int N,
int b,
495 uint32_t blocks,
float *lowband,
497 int level,
float gain,
498 float *lowband_scratch,
502 const uint8_t *cache;
503 int stereo = !!
Y,
split = stereo;
504 int imid = 0, iside = 0;
506 int N_B =
N / blocks;
512 float mid = 0, side = 0;
513 int longblocks = (
B0 == 1);
518 for (
i = 0;
i <= stereo;
i++) {
520 if (
f->remaining2 >= 1 << 3) {
527 f->remaining2 -= 1 << 3;
529 x[0] = 1.0f - 2.0f*sign;
533 lowband_out[0] =
X[0];
537 if (!stereo &&
level == 0) {
538 int tf_change =
f->tf_change[band];
541 recombine = tf_change;
545 (recombine || ((N_B & 1) == 0 && tf_change < 0) ||
B0 > 1)) {
546 for (
i = 0;
i <
N;
i++)
547 lowband_scratch[
i] = lowband[
i];
548 lowband = lowband_scratch;
551 for (k = 0; k < recombine; k++) {
552 if (
quant || lowband)
556 blocks >>= recombine;
560 while ((N_B & 1) == 0 && tf_change < 0) {
561 if (
quant || lowband)
563 fill |= fill << blocks;
573 if (
B0 > 1 && (
quant || lowband))
575 N_B >> recombine,
B0 << recombine,
582 if (!stereo &&
duration >= 0 &&
b > cache[cache[0]] + 12 &&
N > 2) {
588 fill = (fill & 1) | (fill << 1);
589 blocks = (blocks + 1) >> 1;
595 int mbits, sbits,
delta;
606 qn = (stereo && band >=
f->intensity_stereo) ? 1 :
611 itheta = (itheta*qn + 8192) >> 14;
617 else if (stereo ||
B0 > 1)
621 itheta = itheta * 16384 / qn;
625 f->block[1].lin_energy[band],
N);
632 else if (stereo ||
B0 > 1)
636 itheta = itheta * 16384 / qn;
640 inv =
f->apply_phase_inv ? itheta > 8192 : 0;
642 for (
i = 0;
i <
N;
i++)
646 f->block[1].lin_energy[band],
N);
648 if (
b > 2 << 3 &&
f->remaining2 > 2 << 3) {
655 inv =
f->apply_phase_inv ? inv : 0;
668 }
else if (itheta == 16384) {
671 fill &= ((1 << blocks) - 1) << blocks;
681 mid = imid / 32768.0f;
682 side = iside / 32768.0f;
687 if (
N == 2 && stereo) {
694 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
697 f->remaining2 -= qalloc+sbits;
703 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
712 cm = pvq->
quant_band(pvq,
f, rc, band, x2,
NULL,
N, mbits, blocks, lowband,
duration,
713 lowband_out,
level, gain, lowband_scratch, orig_fill);
716 y2[0] = -sign * x2[1];
717 y2[1] = sign * x2[0];
730 float *next_lowband2 =
NULL;
731 float *next_lowband_out1 =
NULL;
738 if (
B0 > 1 && !stereo && (itheta & 0x3fff)) {
749 f->remaining2 -= qalloc;
751 if (lowband && !stereo)
752 next_lowband2 = lowband +
N;
757 next_lowband_out1 = lowband_out;
759 next_level =
level + 1;
761 rebalance =
f->remaining2;
762 if (mbits >= sbits) {
766 lowband,
duration, next_lowband_out1, next_level,
767 stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
768 rebalance = mbits - (rebalance -
f->remaining2);
769 if (rebalance > 3 << 3 && itheta != 0)
770 sbits += rebalance - (3 << 3);
776 gain * side,
NULL, fill >> blocks);
777 cm |= cmt << ((
B0 >> 1) & (stereo - 1));
783 gain * side,
NULL, fill >> blocks);
784 cm <<= ((
B0 >> 1) & (stereo - 1));
785 rebalance = sbits - (rebalance -
f->remaining2);
786 if (rebalance > 3 << 3 && itheta != 16384)
787 mbits += rebalance - (3 << 3);
792 lowband,
duration, next_lowband_out1, next_level,
793 stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
800 f->remaining2 -= curr_bits;
803 while (
f->remaining2 < 0 && q > 0) {
804 f->remaining2 += curr_bits;
806 f->remaining2 -= curr_bits;
813 f->spread, blocks, gain, pvq);
816 f->spread, blocks, gain, pvq);
820 uint32_t cm_mask = (1 << blocks) - 1;
825 for (
i = 0;
i <
N;
i++)
830 for (
i = 0;
i <
N;
i++) {
832 X[
i] = lowband[
i] + (((
celt_rng(
f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
838 memset(
X, 0,
N*
sizeof(
float));
848 for (
i = 0;
i <
N;
i++)
851 }
else if (
level == 0) {
857 B0 << recombine, longblocks);
862 for (k = 0; k < time_divide; k++) {
869 for (k = 0; k < recombine; k++) {
873 blocks <<= recombine;
878 for (
i = 0;
i <
N0;
i++)
879 lowband_out[
i] = n *
X[
i];
889#if CONFIG_OPUS_DECODER
890 return quant_band_template(pvq,
f, rc, band,
X,
Y,
N,
b, blocks, lowband,
duration,
891 lowband_out,
level, gain, lowband_scratch, fill, 0);
899#if CONFIG_OPUS_ENCODER
900 return quant_band_template(pvq,
f, rc, band,
X,
Y,
N,
b, blocks, lowband,
duration,
901 lowband_out,
level, gain, lowband_scratch, fill, 1);
913 s->quant_band =
encode ? pvq_encode_band : pvq_decode_band;
915#if CONFIG_OPUS_ENCODER
916 s->pvq_search = ppp_pvq_search_c;
917#if ARCH_X86 && HAVE_X86ASM
static double val(void *priv, double ch)
static char * split(char *message, char delim)
static int BS_FUNC tell(const BSCTX *bc)
Return number of bits already read.
#define i(width, name, range_min, range_max)
static av_always_inline uint32_t celt_rng(CeltFrame *f)
static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
#define CELT_QTHETA_OFFSET_TWOPHASE
#define CELT_QTHETA_OFFSET
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float sqrtf(float a)
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
static const uint8_t bits[8]
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
Memory handling functions.
static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, enum CeltSpread spread, uint32_t blocks, float gain, CeltPVQ *pvq)
static uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y)
static uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
static void celt_stereo_ms_decouple(float *X, float *Y, int N)
static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
static void celt_exp_rotation(float *X, uint32_t len, uint32_t stride, uint32_t K, enum CeltSpread spread, const int encode)
static void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
static float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
static void celt_stereo_merge(float *X, float *Y, float mid, int N)
static void celt_interleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode)
void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N)
static void celt_normalize_residual(const int *restrict iy, float *restrict X, int N, float g)
static int celt_pulses2bits(const uint8_t *cache, int pulses)
static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, const int band, float *X, float *Y, int N, int b, uint32_t blocks, float *lowband, int duration, float *lowband_out, int level, float gain, float *lowband_scratch, int fill, int quant)
static uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride, float c, float s)
static int celt_log2tan(int isin, int icos)
static int celt_bits2pulses(const uint8_t *cache, int bits)
static void celt_haar1(float *X, int N0, int stride)
static int16_t celt_cos(int16_t x)
static int celt_compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
#define ROUND_MUL16(a, b)
static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, enum CeltSpread spread, uint32_t blocks, float gain, CeltPVQ *pvq)
Decode pulse vector and combine the result with the pitch vector to produce the final normalised sign...
void ff_celt_pvq_init_x86(struct CeltPVQ *s)
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0)
void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn)
void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count)
CELT: write 0 - 31 bits to the rawbits buffer.
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count)
CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise.
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
float(* pvq_search)(float *X, int *y, int K, int N)
const uint8_t ff_celt_log_freq_range[]
const uint8_t ff_celt_bit_interleave[]
const uint32_t *const ff_celt_pvq_u_row[15]
const uint8_t ff_celt_hadamard_order[]
const uint8_t ff_celt_cache_bits[392]
const uint16_t ff_celt_qn_exp2[]
const int16_t ff_celt_cache_index[105]
const uint8_t ff_celt_bit_deinterleave[]
static const uint8_t quant[64]