29 #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
30 #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
34 x = (
MUL16(x, x) + 4096) >> 13;
46 return (ls << 11) - (lc << 11) +
59 for (i = 0; i < 6; i++) {
60 int center = (low + high + 1) >> 1;
61 if (cache[center] >= bits)
67 return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] -
bits) ? low : high;
73 return (pulses == 0) ? 0 : cache[
pulses] + 1;
80 for (i = 0; i <
N; i++)
91 for (i = 0; i < len -
stride; i++) {
94 Xptr[
stride] = c * x2 + s * x1;
95 *Xptr++ = c * x1 - s * x2;
98 Xptr = &X[len - 2 * stride - 1];
99 for (i = len - 2 * stride - 1; i >= 0; i--) {
102 Xptr[
stride] = c * x2 + s * x1;
103 *Xptr-- = c * x1 - s * x2;
108 uint32_t
stride, uint32_t K,
111 uint32_t stride2 = 0;
119 gain = (float)len / (len + (20 - 5*spread) * K);
120 theta =
M_PI * gain * gain / 4;
125 if (len >= stride << 3) {
129 while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)
134 for (i = 0; i <
stride; i++) {
149 int i, j,
N0 = N /
B;
150 uint32_t collapse_mask = 0;
155 for (i = 0; i <
B; i++)
156 for (j = 0; j <
N0; j++)
157 collapse_mask |= (!!iy[i*N0+j]) << i;
158 return collapse_mask;
164 float xp = 0, side = 0;
170 for (i = 0; i <
N; i++) {
178 E[0] = mid2 * mid2 + side - 2 * xp;
179 E[1] = mid2 * mid2 + side + 2 * xp;
180 if (E[0] < 6e-4f || E[1] < 6e-4f) {
181 for (i = 0; i <
N; i++)
186 gain[0] = 1.0f / sqrtf(E[0]);
187 gain[1] = 1.0f / sqrtf(E[1]);
189 for (i = 0; i <
N; i++) {
192 value[0] = mid * X[i];
194 X[i] = gain[0] * (value[0] - value[1]);
195 Y[i] = gain[1] * (value[0] + value[1]);
205 for (i = 0; i <
stride; i++)
206 for (j = 0; j <
N0; j++)
207 tmp[j*stride+i] = X[order[i]*N0+j];
209 memcpy(X, tmp, N*
sizeof(
float));
218 for (i = 0; i <
stride; i++)
219 for (j = 0; j <
N0; j++)
220 tmp[order[i]*N0+j] = X[j*stride+i];
222 memcpy(X, tmp, N*
sizeof(
float));
229 for (i = 0; i <
stride; i++) {
230 for (j = 0; j <
N0; j++) {
231 float x0 = X[stride * (2 * j + 0) + i];
232 float x1 = X[stride * (2 * j + 1) + i];
233 X[stride * (2 * j + 0) + i] = (x0 + x1) *
M_SQRT1_2;
234 X[stride * (2 * j + 1) + i] = (x0 - x1) *
M_SQRT1_2;
244 if (stereo && N == 2)
250 qb =
FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3);
251 qn = (qb < (1 << 3 >> 1)) ? 1 : ((
ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
256 static inline uint32_t
celt_icwrsi(uint32_t
N, uint32_t K,
const int *y)
258 int i, idx = 0, sum = 0;
259 for (i = N - 1; i >= 0; i--) {
261 idx +=
CELT_PVQ_U(N - i, sum) + (y[i] < 0)*i_s;
268 static inline uint64_t
celt_cwrsi(uint32_t
N, uint32_t K, uint32_t i,
int *y)
294 for (p = row[K]; p > i; p = row[K])
298 val = (k0 - K +
s) ^ s;
306 if (p <= i && i < q) {
320 val = (k0 - K +
s) ^ s;
338 val = (k0 - K +
s) ^ s;
369 float res = 0.0f, xy_norm = 0.0f;
371 for (i = 0; i <
N; i++)
374 res = K/(res + FLT_EPSILON);
376 for (i = 0; i <
N; i++) {
379 xy_norm += y[i]*X[i];
384 int max_idx = 0, phase =
FFSIGN(K);
385 float max_num = 0.0f;
386 float max_den = 1.0f;
389 for (i = 0; i <
N; i++) {
393 const int ca = 1 ^ ((y[i] == 0) & (phase < 0));
394 const int y_new = y_norm + 2*phase*
FFABS(y[i]);
395 float xy_new = xy_norm + 1*phase*
FFABS(X[i]);
396 xy_new = xy_new * xy_new;
397 if (ca && (max_den*xy_new) > (y_new*max_num)) {
406 phase *=
FFSIGN(X[max_idx]);
407 xy_norm += 1*phase*X[max_idx];
408 y_norm += 2*phase*y[max_idx];
412 return (
float)y_norm;
416 enum CeltSpread spread, uint32_t blocks,
float gain,
432 enum CeltSpread spread, uint32_t blocks,
float gain,
446 float e[2] = { 0.0f, 0.0f };
448 for (i = 0; i <
N; i++) {
449 e[0] += (X[i] + Y[i])*(X[i] + Y[i]);
450 e[1] += (X[i] - Y[i])*(X[i] - Y[i]);
453 for (i = 0; i <
N; i++) {
464 const float energy_n = 1.0f/(sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON);
467 for (i = 0; i <
N; i++)
468 X[i] = e_l*X[i] + e_r*Y[i];
474 for (i = 0; i <
N; i++) {
475 const float Xret = X[i];
483 const int band,
float *X,
484 float *
Y,
int N,
int b,
485 uint32_t blocks,
float *lowband,
487 int level,
float gain,
488 float *lowband_scratch,
494 int stereo = !!
Y,
split = stereo;
495 int imid = 0, iside = 0;
497 int N_B = N / blocks;
503 float mid = 0, side = 0;
504 int longblocks = (B0 == 1);
509 for (i = 0; i <= stereo; i++) {
520 x[0] = 1.0f - 2.0f*sign;
524 lowband_out[0] = X[0];
528 if (!stereo && level == 0) {
532 recombine = tf_change;
536 (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
537 for (i = 0; i <
N; i++)
538 lowband_scratch[i] = lowband[i];
539 lowband = lowband_scratch;
542 for (k = 0; k < recombine; k++) {
543 if (quant || lowband)
544 celt_haar1(quant ? X : lowband, N >> k, 1 << k);
547 blocks >>= recombine;
551 while ((N_B & 1) == 0 && tf_change < 0) {
552 if (quant || lowband)
554 fill |= fill << blocks;
564 if (B0 > 1 && (quant || lowband))
566 N_B >> recombine, B0 << recombine,
573 if (!stereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
579 fill = (fill & 1) | (fill << 1);
580 blocks = (blocks + 1) >> 1;
586 int mbits, sbits,
delta;
602 itheta = (itheta*qn + 8192) >> 14;
608 else if (stereo || B0 > 1)
612 itheta = itheta * 16384 / qn;
623 else if (stereo || B0 > 1)
627 itheta = itheta * 16384 / qn;
633 for (i = 0; i <
N; i++)
656 fill = av_mod_uintp2(fill, blocks);
658 }
else if (itheta == 16384) {
661 fill &= ((1 << blocks) - 1) << blocks;
671 mid = imid / 32768.0f;
672 side = iside / 32768.0f;
677 if (N == 2 && stereo) {
684 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
693 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
702 cm = rec(pvq, f, rc, band, x2,
NULL, N, mbits, blocks, lowband, duration,
703 lowband_out, level, gain, lowband_scratch, orig_fill);
706 y2[0] = -sign * x2[1];
707 y2[1] = sign * x2[0];
720 float *next_lowband2 =
NULL;
721 float *next_lowband_out1 =
NULL;
728 if (B0 > 1 && !stereo && (itheta & 0x3fff)) {
735 delta =
FFMIN(0, delta + (N << 3 >> (5 - duration)));
737 mbits = av_clip((b - delta) / 2, 0, b);
741 if (lowband && !stereo)
742 next_lowband2 = lowband +
N;
747 next_lowband_out1 = lowband_out;
749 next_level = level + 1;
752 if (mbits >= sbits) {
755 cm = rec(pvq, f, rc, band, X,
NULL, N, mbits, blocks, lowband,
756 duration, next_lowband_out1, next_level,
757 stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
758 rebalance = mbits - (rebalance - f->
remaining2);
759 if (rebalance > 3 << 3 && itheta != 0)
760 sbits += rebalance - (3 << 3);
764 cmt = rec(pvq, f, rc, band, Y,
NULL, N, sbits, blocks, next_lowband2,
765 duration,
NULL, next_level, gain * side,
NULL,
767 cm |= cmt << ((B0 >> 1) & (stereo - 1));
771 cm = rec(pvq, f, rc, band, Y,
NULL, N, sbits, blocks, next_lowband2,
772 duration,
NULL, next_level, gain * side,
NULL, fill >> blocks);
773 cm <<= ((B0 >> 1) & (stereo - 1));
774 rebalance = sbits - (rebalance - f->
remaining2);
775 if (rebalance > 3 << 3 && itheta != 16384)
776 mbits += rebalance - (3 << 3);
780 cm |= rec(pvq, f, rc, band, X,
NULL, N, mbits, blocks, lowband, duration,
781 next_lowband_out1, next_level, stereo ? 1.0f : (gain * mid),
782 lowband_scratch, fill);
801 cm =
celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
802 f->
spread, blocks, gain, pvq);
804 cm =
celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
805 f->
spread, blocks, gain, pvq);
809 uint32_t cm_mask = (1 << blocks) - 1;
814 for (i = 0; i <
N; i++)
819 for (i = 0; i <
N; i++) {
821 X[i] = lowband[i] + (((
celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
827 memset(X, 0, N*
sizeof(
float));
837 for (i = 0; i <
N; i++)
840 }
else if (level == 0) {
846 B0 << recombine, longblocks);
851 for (k = 0; k < time_divide; k++) {
858 for (k = 0; k < recombine; k++) {
862 blocks <<= recombine;
867 for (i = 0; i <
N0; i++)
868 lowband_out[i] = n * X[i];
870 cm = av_mod_uintp2(cm, blocks);
879 return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration,
880 lowband_out, level, gain, lowband_scratch, fill, 0,
886 return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration,
887 lowband_out, level, gain, lowband_scratch, fill, 1,
892 float *
bits,
float lambda)
897 float buf[176 * 2], lowband_scratch[176], norm1[176], norm2[176];
898 float dist, cost, err_x = 0.0f, err_y = 0.0f;
905 memcpy(X, X_orig, band_size*
sizeof(
float));
907 memcpy(Y, Y_orig, band_size*
sizeof(
float));
910 if (band <= f->coded_bands - 1) {
916 pvq->
encode_band(pvq, f, rc, band, X,
NULL, band_size, b / 2, f->
blocks,
NULL,
917 f->
size, norm1, 0, 1.0f, lowband_scratch, cm[0]);
919 pvq->
encode_band(pvq, f, rc, band, Y,
NULL, band_size, b / 2, f->
blocks,
NULL,
920 f->
size, norm2, 0, 1.0f, lowband_scratch, cm[1]);
922 pvq->
encode_band(pvq, f, rc, band, X, Y, band_size, b, f->
blocks,
NULL, f->
size,
923 norm1, 0, 1.0f, lowband_scratch, cm[0] | cm[1]);
926 for (i = 0; i < band_size; i++) {
927 err_x += (X[i] - X_orig[i])*(X[i] - X_orig[i]);
928 err_y += (Y[i] - Y_orig[i])*(Y[i] - Y_orig[i]);
931 dist = sqrtf(err_x) + sqrtf(err_y);
937 return lambda*dist*cost;
static void celt_stereo_merge(float *X, float *Y, float mid, int N)
const char const char void * val
const uint8_t ff_celt_cache_bits[392]
const uint32_t *const ff_celt_pvq_u_row[15]
#define OPUS_RC_CHECKPOINT_SPAWN(rc)
void ff_opus_dsp_init_x86(struct CeltPVQ *s)
static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
static int celt_compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
const uint8_t ff_celt_log_freq_range[]
float coeffs[CELT_MAX_FRAME_SIZE]
static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
static float ppp_pvq_search_c(float *X, int *y, int K, int N)
const uint8_t ff_celt_freq_bands[]
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
const uint8_t ff_celt_bit_deinterleave[]
static uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y)
const uint16_t ff_celt_qn_exp2[]
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
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...
static void celt_interleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
const uint8_t ff_celt_hadamard_order[]
uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
static void celt_stereo_ms_decouple(float *X, float *Y, int N)
int av_cold ff_celt_pvq_init(CeltPVQ **pvq)
static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride, float c, float s)
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, QUANT_FN(*rec))
static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
static float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
static void celt_haar1(float *X, int N0, int stride)
float lin_energy[CELT_MAX_BANDS]
void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn)
const int16_t ff_celt_cache_index[105]
static int celt_log2tan(int isin, int icos)
static void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
#define OPUS_RC_CHECKPOINT_ROLLBACK(rc)
int tf_change[CELT_MAX_BANDS]
int pulses[CELT_MAX_BANDS]
static const uint8_t offset[127][2]
static char * split(char *message, char delim)
static int16_t celt_cos(int16_t x)
#define OPUS_RC_CHECKPOINT_BITS(rc)
GLsizei GLboolean const GLfloat * value
static uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
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)
const uint8_t ff_celt_freq_range[]
float(* band_cost)(struct CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, float *bits, float lambda)
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
float(* pvq_search)(float *X, int *y, int K, int N)
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.
uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
static uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
static int celt_pulses2bits(const uint8_t *cache, int pulses)
#define CELT_QTHETA_OFFSET
static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N)
GLint GLenum GLboolean GLsizei stride
#define ROUND_MUL16(a, b)
void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count)
CELT: write 0 - 31 bits to the rawbits buffer.
static int celt_bits2pulses(const uint8_t *cache, int bits)
static av_always_inline uint32_t celt_rng(CeltFrame *f)
const uint8_t ff_celt_bit_interleave[]
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_normalize_residual(const int *av_restrict iy, float *av_restrict X, int N, float g)
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0)
static QUANT_FN(pvq_decode_band)
void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
#define CELT_QTHETA_OFFSET_TWOPHASE
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)