Go to the documentation of this file.
28 #include "config_components.h"
34 #define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15)
36 #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
37 #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
41 x = (
MUL16(x, x) + 4096) >> 13;
53 return (ls << 11) - (lc << 11) +
66 for (
i = 0;
i < 6;
i++) {
67 int center = (low + high + 1) >> 1;
68 if (cache[center] >=
bits)
74 return (
bits - (low == 0 ? -1 : cache[low]) <= cache[high] -
bits) ? low : high;
87 for (
i = 0;
i <
N;
i++)
102 *Xptr++ =
c * x1 -
s * x2;
110 *Xptr-- =
c * x1 -
s * x2;
118 uint32_t stride2 = 0;
127 theta =
M_PI * gain * gain / 4;
156 int i, j,
N0 =
N /
B;
157 uint32_t collapse_mask = 0;
162 for (
i = 0;
i <
B;
i++)
163 for (j = 0; j <
N0; j++)
164 collapse_mask |= (!!iy[
i*
N0+j]) <<
i;
165 return collapse_mask;
171 float xp = 0, side = 0;
177 for (
i = 0;
i <
N;
i++) {
185 E[0] = mid2 * mid2 + side - 2 * xp;
186 E[1] = mid2 * mid2 + side + 2 * xp;
187 if (
E[0] < 6e-4
f ||
E[1] < 6e-4
f) {
188 for (
i = 0;
i <
N;
i++)
193 gain[0] = 1.0f /
sqrtf(
E[0]);
194 gain[1] = 1.0f /
sqrtf(
E[1]);
196 for (
i = 0;
i <
N;
i++) {
213 for (j = 0; j <
N0; j++)
216 memcpy(
X,
tmp,
N*
sizeof(
float));
226 for (j = 0; j <
N0; j++)
229 memcpy(
X,
tmp,
N*
sizeof(
float));
237 for (j = 0; j <
N0; j++) {
238 float x0 =
X[
stride * (2 * j + 0) +
i];
239 float x1 =
X[
stride * (2 * j + 1) +
i];
251 if (stereo &&
N == 2)
258 qn = (qb < (1 << 3 >> 1)) ? 1 : ((
ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
265 int i, idx = 0, sum = 0;
266 for (
i =
N - 1;
i >= 0;
i--) {
275 static inline uint64_t
celt_cwrsi(uint32_t
N, uint32_t
K, uint32_t
i,
int *y)
301 for (p = row[
K]; p >
i; p = row[
K])
313 if (p <=
i &&
i < q) {
369 #if CONFIG_OPUS_ENCODER
374 static float ppp_pvq_search_c(
float *
X,
int *y,
int K,
int N)
377 float res = 0.0f, xy_norm = 0.0f;
379 for (
i = 0;
i <
N;
i++)
382 res =
K/(res + FLT_EPSILON);
384 for (
i = 0;
i <
N;
i++) {
387 xy_norm += y[
i]*
X[
i];
392 int max_idx = 0, phase =
FFSIGN(
K);
393 float max_num = 0.0f;
394 float max_den = 1.0f;
397 for (
i = 0;
i <
N;
i++) {
401 const int ca = 1 ^ ((y[
i] == 0) & (phase < 0));
402 const int y_new = y_norm + 2*phase*
FFABS(y[
i]);
403 float xy_new = xy_norm + 1*phase*
FFABS(
X[
i]);
404 xy_new = xy_new * xy_new;
405 if (ca && (max_den*xy_new) > (y_new*max_num)) {
415 xy_norm += 1*phase*
X[max_idx];
416 y_norm += 2*phase*y[max_idx];
420 return (
float)y_norm;
425 enum CeltSpread spread, uint32_t blocks,
float gain,
441 enum CeltSpread spread, uint32_t blocks,
float gain,
455 float e[2] = { 0.0f, 0.0f };
457 for (
i = 0;
i <
N;
i++) {
458 e[0] += (
X[
i] +
Y[
i])*(
X[
i] +
Y[
i]);
459 e[1] += (
X[
i] -
Y[
i])*(
X[
i] -
Y[
i]);
462 for (
i = 0;
i <
N;
i++) {
473 const float energy_n = 1.0f/(
sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON);
476 for (
i = 0;
i <
N;
i++)
477 X[
i] = e_l*
X[
i] + e_r*
Y[
i];
483 for (
i = 0;
i <
N;
i++) {
484 const float Xret =
X[
i];
492 const int band,
float *
X,
493 float *
Y,
int N,
int b,
494 uint32_t blocks,
float *lowband,
496 int level,
float gain,
497 float *lowband_scratch,
501 const uint8_t *cache;
502 int stereo = !!
Y,
split = stereo;
503 int imid = 0, iside = 0;
505 int N_B =
N / blocks;
511 float mid = 0, side = 0;
512 int longblocks = (
B0 == 1);
517 for (
i = 0;
i <= stereo;
i++) {
519 if (
f->remaining2 >= 1 << 3) {
526 f->remaining2 -= 1 << 3;
528 x[0] = 1.0f - 2.0f*sign;
532 lowband_out[0] =
X[0];
536 if (!stereo &&
level == 0) {
537 int tf_change =
f->tf_change[band];
540 recombine = tf_change;
544 (recombine || ((N_B & 1) == 0 && tf_change < 0) ||
B0 > 1)) {
545 for (
i = 0;
i <
N;
i++)
546 lowband_scratch[
i] = lowband[
i];
547 lowband = lowband_scratch;
550 for (k = 0; k < recombine; k++) {
551 if (
quant || lowband)
555 blocks >>= recombine;
559 while ((N_B & 1) == 0 && tf_change < 0) {
560 if (
quant || lowband)
562 fill |= fill << blocks;
572 if (
B0 > 1 && (
quant || lowband))
574 N_B >> recombine,
B0 << recombine,
581 if (!stereo &&
duration >= 0 &&
b > cache[cache[0]] + 12 &&
N > 2) {
587 fill = (fill & 1) | (fill << 1);
588 blocks = (blocks + 1) >> 1;
594 int mbits, sbits,
delta;
605 qn = (stereo && band >=
f->intensity_stereo) ? 1 :
610 itheta = (itheta*qn + 8192) >> 14;
616 else if (stereo ||
B0 > 1)
620 itheta = itheta * 16384 / qn;
624 f->block[1].lin_energy[band],
N);
631 else if (stereo ||
B0 > 1)
635 itheta = itheta * 16384 / qn;
639 inv =
f->apply_phase_inv ? itheta > 8192 : 0;
641 for (
i = 0;
i <
N;
i++)
645 f->block[1].lin_energy[band],
N);
647 if (
b > 2 << 3 &&
f->remaining2 > 2 << 3) {
654 inv =
f->apply_phase_inv ? inv : 0;
667 }
else if (itheta == 16384) {
670 fill &= ((1 << blocks) - 1) << blocks;
680 mid = imid / 32768.0f;
681 side = iside / 32768.0f;
686 if (
N == 2 && stereo) {
693 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
696 f->remaining2 -= qalloc+sbits;
702 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
711 cm = pvq->
quant_band(pvq,
f, rc, band, x2,
NULL,
N, mbits, blocks, lowband,
duration,
712 lowband_out,
level, gain, lowband_scratch, orig_fill);
715 y2[0] = -sign * x2[1];
716 y2[1] = sign * x2[0];
729 float *next_lowband2 =
NULL;
730 float *next_lowband_out1 =
NULL;
737 if (
B0 > 1 && !stereo && (itheta & 0x3fff)) {
748 f->remaining2 -= qalloc;
750 if (lowband && !stereo)
751 next_lowband2 = lowband +
N;
756 next_lowband_out1 = lowband_out;
758 next_level =
level + 1;
760 rebalance =
f->remaining2;
761 if (mbits >= sbits) {
765 lowband,
duration, next_lowband_out1, next_level,
766 stereo ? 1.0
f : (gain * mid), lowband_scratch, fill);
767 rebalance = mbits - (rebalance -
f->remaining2);
768 if (rebalance > 3 << 3 && itheta != 0)
769 sbits += rebalance - (3 << 3);
775 gain * side,
NULL, fill >> blocks);
776 cm |= cmt << ((
B0 >> 1) & (stereo - 1));
782 gain * side,
NULL, fill >> blocks);
783 cm <<= ((
B0 >> 1) & (stereo - 1));
784 rebalance = sbits - (rebalance -
f->remaining2);
785 if (rebalance > 3 << 3 && itheta != 16384)
786 mbits += rebalance - (3 << 3);
791 lowband,
duration, next_lowband_out1, next_level,
792 stereo ? 1.0
f : (gain * mid), lowband_scratch, fill);
799 f->remaining2 -= curr_bits;
802 while (
f->remaining2 < 0 && q > 0) {
803 f->remaining2 += curr_bits;
805 f->remaining2 -= curr_bits;
812 f->spread, blocks, gain, pvq);
815 f->spread, blocks, gain, pvq);
819 uint32_t cm_mask = (1 << blocks) - 1;
824 for (
i = 0;
i <
N;
i++)
829 for (
i = 0;
i <
N;
i++) {
831 X[
i] = lowband[
i] + (((
celt_rng(
f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
837 memset(
X, 0,
N*
sizeof(
float));
847 for (
i = 0;
i <
N;
i++)
850 }
else if (
level == 0) {
856 B0 << recombine, longblocks);
861 for (k = 0; k < time_divide; k++) {
868 for (k = 0; k < recombine; k++) {
872 blocks <<= recombine;
877 for (
i = 0;
i <
N0;
i++)
878 lowband_out[
i] = n *
X[
i];
888 #if CONFIG_OPUS_DECODER
889 return quant_band_template(pvq,
f, rc, band,
X,
Y,
N,
b, blocks, lowband,
duration,
890 lowband_out,
level, gain, lowband_scratch, fill, 0);
898 #if CONFIG_OPUS_ENCODER
899 return quant_band_template(pvq,
f, rc, band,
X,
Y,
N,
b, blocks, lowband,
duration,
900 lowband_out,
level, gain, lowband_scratch, fill, 1);
912 s->quant_band =
encode ? pvq_encode_band : pvq_decode_band;
914 #if CONFIG_OPUS_ENCODER
915 s->pvq_search = ppp_pvq_search_c;
const uint8_t ff_celt_cache_bits[392]
const uint8_t ff_celt_bit_deinterleave[]
static void celt_normalize_residual(const int *av_restrict iy, float *av_restrict X, int N, float g)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
const uint16_t ff_celt_qn_exp2[]
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...
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.
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 uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
static void celt_haar1(float *X, int N0, int stride)
const uint8_t ff_celt_log_freq_range[]
static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N)
static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride, float c, float s)
const uint8_t ff_celt_hadamard_order[]
static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
static float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
static QUANT_FN(pvq_decode_band)
int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode)
const uint32_t *const ff_celt_pvq_u_row[15]
static void celt_exp_rotation(float *X, uint32_t len, uint32_t stride, uint32_t K, enum CeltSpread spread, const int encode)
static int celt_log2tan(int isin, int icos)
static int celt_pulses2bits(const uint8_t *cache, int pulses)
static double val(void *priv, double ch)
static int celt_bits2pulses(const uint8_t *cache, int bits)
static const uint8_t quant[64]
static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
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)
#define CELT_QTHETA_OFFSET_TWOPHASE
void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn)
static uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
const int16_t ff_celt_cache_index[105]
void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
static int BS_FUNC() tell(const BSCTX *bc)
Return number of bits already read.
#define CELT_QTHETA_OFFSET
static __device__ float sqrtf(float a)
uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static int16_t celt_cos(int16_t x)
static char * split(char *message, char delim)
static av_always_inline uint32_t celt_rng(CeltFrame *f)
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define ROUND_MUL16(a, b)
void ff_opus_rc_enc_uint_step(OpusRangeCoder *rc, uint32_t val, int k0)
#define i(width, name, range_min, range_max)
void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count)
CELT: write 0 - 31 bits to the rawbits buffer.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
static void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
static void celt_stereo_merge(float *X, float *Y, float mid, int N)
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
static int celt_compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
void ff_celt_pvq_init_x86(struct CeltPVQ *s)
static void celt_interleave_hadamard(float *tmp, float *X, int N0, int stride, int hadamard)
const uint8_t ff_celt_bit_interleave[]
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
float(* pvq_search)(float *X, int *y, int K, int N)
static const int8_t pulses[4]
Number of non-zero pulses in the MP-MLQ excitation.
static uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
static void celt_stereo_ms_decouple(float *X, float *Y, int N)