26 #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
27 #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
31 x = (
MUL16(x, x) + 4096) >> 13;
43 return (ls << 11) - (lc << 11) +
56 for (i = 0; i < 6; i++) {
57 int center = (low + high + 1) >> 1;
58 if (cache[center] >= bits)
64 return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] -
bits) ? low : high;
70 return (pulses == 0) ? 0 : cache[
pulses] + 1;
77 for (i = 0; i <
N; i++)
88 for (i = 0; i < len -
stride; i++) {
92 Xptr[
stride] = c * x2 + s * x1;
93 *Xptr++ = c * x1 - s * x2;
96 Xptr = &X[len - 2 * stride - 1];
97 for (i = len - 2 * stride - 1; i >= 0; i--) {
101 Xptr[
stride] = c * x2 + s * x1;
102 *Xptr-- = c * x1 - s * x2;
107 uint32_t
stride, uint32_t K,
110 uint32_t stride2 = 0;
118 gain = (float)len / (len + (20 - 5*spread) * K);
119 theta =
M_PI * gain * gain / 4;
124 if (len >= stride << 3) {
128 while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)
135 for (i = 0; i <
stride; i++) {
150 uint32_t collapse_mask;
161 for (i = 0; i <
B; i++)
162 for (j = 0; j <
N0; j++)
163 collapse_mask |= (iy[i*N0+j]!=0)<<i;
164 return collapse_mask;
170 float xp = 0, side = 0;
176 for (i = 0; i <
N; i++) {
184 E[0] = mid2 * mid2 + side - 2 * xp;
185 E[1] = mid2 * mid2 + side + 2 * xp;
186 if (E[0] < 6e-4f || E[1] < 6e-4f) {
187 for (i = 0; i <
N; i++)
193 gain[0] = 1.0f / sqrtf(t);
195 gain[1] = 1.0f / sqrtf(t);
197 for (i = 0; i <
N; i++) {
200 value[0] = mid * X[i];
202 X[i] = gain[0] * (value[0] - value[1]);
203 Y[i] = gain[1] * (value[0] + value[1]);
215 for (i = 0; i <
stride; i++)
216 for (j = 0; j <
N0; j++)
217 tmp[j*stride+i] = X[ordery[i]*N0+j];
219 for (i = 0; i <
stride; i++)
220 for (j = 0; j <
N0; j++)
221 tmp[j*stride+i] = X[i*N0+j];
224 for (i = 0; i <
N; i++)
236 for (i = 0; i <
stride; i++)
237 for (j = 0; j <
N0; j++)
238 tmp[ordery[i]*N0+j] = X[j*stride+i];
240 for (i = 0; i <
stride; i++)
241 for (j = 0; j <
N0; j++)
242 tmp[i*N0+j] = X[j*stride+i];
245 for (i = 0; i <
N; i++)
253 for (i = 0; i <
stride; i++) {
254 for (j = 0; j <
N0; j++) {
255 float x0 = X[stride * (2 * j + 0) + i];
256 float x1 = X[stride * (2 * j + 1) + i];
257 X[stride * (2 * j + 0) + i] = (x0 + x1) *
M_SQRT1_2;
258 X[stride * (2 * j + 1) + i] = (x0 - x1) *
M_SQRT1_2;
268 if (dualstereo && N == 2)
274 qb =
FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3);
275 qn = (qb < (1 << 3 >> 1)) ? 1 : ((
ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
280 static inline uint32_t
celt_icwrsi(uint32_t
N, uint32_t K,
const int *y)
282 int i, idx = 0, sum = 0;
283 for (i = N - 1; i >= 0; i--) {
285 idx +=
CELT_PVQ_U(N - i, sum) + (y[i] < 0)*i_s;
292 static inline uint64_t
celt_cwrsi(uint32_t
N, uint32_t K, uint32_t i,
int *y)
320 for (p = row[K]; p > i; p = row[K])
324 val = (k0 - K +
s) ^ s;
332 if (p <= i && i < q) {
346 val = (k0 - K +
s) ^ s;
364 val = (k0 - K +
s) ^ s;
395 float res = 0.0f, y_norm = 0.0f, xy_norm = 0.0f;
397 for (i = 0; i <
N; i++)
400 res = K/(res + FLT_EPSILON);
402 for (i = 0; i <
N; i++) {
405 xy_norm += y[i]*X[i];
410 int max_idx = 0, phase =
FFSIGN(K);
411 float max_den = 1.0f, max_num = 0.0f;
414 for (i = 0; i <
N; i++) {
418 const int ca = 1 ^ ((y[i] == 0) & (phase < 0));
419 float xy_new = xy_norm + 1*phase*
FFABS(X[i]);
420 float y_new = y_norm + 2*phase*
FFABS(y[i]);
421 xy_new = xy_new * xy_new;
422 if (ca && (max_den*xy_new) > (y_new*max_num)) {
431 phase *=
FFSIGN(X[max_idx]);
432 xy_norm += 1*phase*X[max_idx];
433 y_norm += 2*phase*y[max_idx];
439 enum CeltSpread spread, uint32_t blocks,
float gain)
452 enum CeltSpread spread, uint32_t blocks,
float gain)
463 float *X,
float *
Y,
int N,
int b, uint32_t blocks,
465 float gain,
float *lowband_scratch,
int fill)
468 int dualstereo,
split;
469 int imid = 0, iside = 0;
477 float mid = 0, side = 0;
478 int longblocks = (B0 == 1);
481 N_B0 = N_B = N / blocks;
482 split = dualstereo = (Y !=
NULL);
488 for (i = 0; i <= dualstereo; i++) {
495 x[0] = sign ? -1.0f : 1.0f;
499 lowband_out[0] = X[0];
503 if (!dualstereo && level == 0) {
507 recombine = tf_change;
511 (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
513 for (j = 0; j <
N; j++)
514 lowband_scratch[j] = lowband[j];
515 lowband = lowband_scratch;
518 for (k = 0; k < recombine; k++) {
523 blocks >>= recombine;
527 while ((N_B & 1) == 0 && tf_change < 0) {
530 fill |= fill << blocks;
540 if (B0 > 1 && lowband)
542 B0 << recombine, longblocks);
548 if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
554 fill = (fill & 1) | (fill << 1);
555 blocks = (blocks + 1) >> 1;
561 int mbits, sbits,
delta;
578 if (dualstereo && N > 2)
580 else if (dualstereo || B0 > 1)
584 itheta = itheta * 16384 / qn;
587 }
else if (dualstereo) {
598 fill = av_mod_uintp2(fill, blocks);
600 }
else if (itheta == 16384) {
603 fill &= ((1 << blocks) - 1) << blocks;
613 mid = imid / 32768.0f;
614 side = iside / 32768.0f;
619 if (N == 2 && dualstereo) {
626 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
639 lowband, duration, lowband_out, level, gain,
640 lowband_scratch, orig_fill);
643 y2[0] = -sign * x2[1];
644 y2[1] = sign * x2[0];
657 float *next_lowband2 =
NULL;
658 float *next_lowband_out1 =
NULL;
664 if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) {
671 delta =
FFMIN(0, delta + (N << 3 >> (5 - duration)));
673 mbits = av_clip((b - delta) / 2, 0, b);
677 if (lowband && !dualstereo)
678 next_lowband2 = lowband +
N;
683 next_lowband_out1 = lowband_out;
685 next_level = level + 1;
688 if (mbits >= sbits) {
692 lowband, duration, next_lowband_out1,
693 next_level, dualstereo ? 1.0f : (gain * mid),
694 lowband_scratch, fill);
696 rebalance = mbits - (rebalance - f->
remaining2);
697 if (rebalance > 3 << 3 && itheta != 0)
698 sbits += rebalance - (3 << 3);
703 next_lowband2, duration,
NULL,
704 next_level, gain * side,
NULL,
705 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
710 next_lowband2, duration,
NULL,
711 next_level, gain * side,
NULL,
712 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
714 rebalance = sbits - (rebalance - f->
remaining2);
715 if (rebalance > 3 << 3 && itheta != 16384)
716 mbits += rebalance - (3 << 3);
721 lowband, duration, next_lowband_out1,
722 next_level, dualstereo ? 1.0f : (gain * mid),
723 lowband_scratch, fill);
741 cm =
celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
746 uint32_t cm_mask = (1 << blocks) - 1;
749 for (j = 0; j <
N; j++)
754 for (j = 0; j <
N; j++)
759 for (j = 0; j <
N; j++) {
761 X[j] = lowband[j] + (((
celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
776 for (j = 0; j <
N; j++)
779 }
else if (level == 0) {
785 B0<<recombine, longblocks);
790 for (k = 0; k < time_divide; k++) {
797 for (k = 0; k < recombine; k++) {
801 blocks <<= recombine;
807 for (j = 0; j <
N0; j++)
808 lowband_out[j] = n * X[j];
810 cm = av_mod_uintp2(cm, blocks);
821 float e[2] = { 0.0f, 0.0f };
822 for (j = 0; j <
N; j++) {
824 e[0] += (X[j] + Y[j])*(X[j] + Y[j]);
825 e[1] += (X[j] - Y[j])*(X[j] - Y[j]);
837 const float energy_n = 1.0f/(sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON);
840 for (i = 0; i <
N; i++)
841 X[i] = e_l*X[i] + e_r*Y[i];
847 const float decouple_norm = 1.0f/sqrtf(2.0f);
848 for (i = 0; i <
N; i++) {
849 const float Xret = X[i];
850 X[i] = (X[i] + Y[i])*decouple_norm;
851 Y[i] = (Y[i] - Xret)*decouple_norm;
856 float *X,
float *
Y,
int N,
int b, uint32_t blocks,
858 float gain,
float *lowband_scratch,
int fill)
861 int dualstereo,
split;
862 int imid = 0, iside = 0;
864 int N_B = N / blocks;
870 float mid = 0, side = 0;
871 int longblocks = (B0 == 1);
874 split = dualstereo = (Y !=
NULL);
880 for (i = 0; i <= dualstereo; i++) {
889 lowband_out[0] = X[0];
893 if (!dualstereo && level == 0) {
897 recombine = tf_change;
901 (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
903 for (j = 0; j <
N; j++)
904 lowband_scratch[j] = lowband[j];
905 lowband = lowband_scratch;
908 for (k = 0; k < recombine; k++) {
912 blocks >>= recombine;
916 while ((N_B & 1) == 0 && tf_change < 0) {
918 fill |= fill << blocks;
930 B0 << recombine, longblocks);
936 if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
942 fill = (fill & 1) | (fill << 1);
943 blocks = (blocks + 1) >> 1;
949 int mbits, sbits,
delta;
966 itheta = (itheta*qn + 8192) >> 14;
970 if (dualstereo && N > 2)
972 else if (dualstereo || B0 > 1)
976 itheta = itheta * 16384 / qn;
984 }
else if (dualstereo) {
1009 fill = av_mod_uintp2(fill, blocks);
1011 }
else if (itheta == 16384) {
1014 fill &= ((1 << blocks) - 1) << blocks;
1024 mid = imid / 32768.0f;
1025 side = iside / 32768.0f;
1030 if (N == 2 && dualstereo) {
1037 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
1039 c = (itheta > 8192);
1045 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
1048 sign = 1 - 2 * sign;
1052 lowband, duration, lowband_out, level, gain,
1053 lowband_scratch, orig_fill);
1056 y2[0] = -sign * x2[1];
1057 y2[1] = sign * x2[0];
1070 float *next_lowband2 =
NULL;
1071 float *next_lowband_out1 =
NULL;
1077 if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) {
1084 delta =
FFMIN(0, delta + (N << 3 >> (5 - duration)));
1086 mbits = av_clip((b - delta) / 2, 0, b);
1090 if (lowband && !dualstereo)
1091 next_lowband2 = lowband +
N;
1096 next_lowband_out1 = lowband_out;
1098 next_level = level + 1;
1101 if (mbits >= sbits) {
1105 lowband, duration, next_lowband_out1,
1106 next_level, dualstereo ? 1.0f : (gain * mid),
1107 lowband_scratch, fill);
1109 rebalance = mbits - (rebalance - f->
remaining2);
1110 if (rebalance > 3 << 3 && itheta != 0)
1111 sbits += rebalance - (3 << 3);
1116 next_lowband2, duration,
NULL,
1117 next_level, gain * side,
NULL,
1118 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
1123 next_lowband2, duration,
NULL,
1124 next_level, gain * side,
NULL,
1125 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
1127 rebalance = sbits - (rebalance - f->
remaining2);
1128 if (rebalance > 3 << 3 && itheta != 16384)
1129 mbits += rebalance - (3 << 3);
1134 lowband, duration, next_lowband_out1,
1135 next_level, dualstereo ? 1.0f : (gain * mid),
1136 lowband_scratch, fill);
1154 cm =
celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
1155 f->
spread, blocks, gain);
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]
const uint8_t ff_celt_hadamard_ordery[]
const uint8_t ff_celt_log_freq_range[]
static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
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 void celt_interleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
static void celt_stereo_ms_decouple(float *X, float *Y, int N)
static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, enum CeltSpread spread, uint32_t blocks, float gain)
static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride, float c, float s)
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)
int tf_change[CELT_MAX_BANDS]
static const uint8_t offset[127][2]
static char * split(char *message, char delim)
static int celt_compute_qn(int N, int b, int offset, int pulse_cap, int dualstereo)
static int16_t celt_cos(int16_t x)
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_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K, enum CeltSpread spread, uint32_t blocks, float gain)
Decode pulse vector and combine the result with the pitch vector to produce the final normalised sign...
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)
uint32_t ff_celt_encode_band(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)
#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)
uint32_t ff_celt_decode_band(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)
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 void celt_pvq_search(float *X, int *y, int K, int N)
#define CELT_QTHETA_OFFSET_TWOPHASE
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)