25 #define OPUS_RC_BITS 32
27 #define OPUS_RC_CEIL ((1 << OPUS_RC_SYM) - 1)
28 #define OPUS_RC_TOP (1u << 31)
29 #define OPUS_RC_BOT (OPUS_RC_TOP >> OPUS_RC_SYM)
30 #define OPUS_RC_SHIFT (OPUS_RC_BITS - OPUS_RC_SYM - 1)
41 for (; rc->
ext > 0; rc->
ext--)
67 uint32_t low, uint32_t high,
70 rc->
value -= scale * (total - high);
71 rc->
range = low ? scale * (high - low)
72 : rc->
range - scale * (total - high);
78 uint32_t p_tot,
const int ptwo)
80 uint32_t rscaled, cnd = !!
b;
84 rscaled = rc->
range/p_tot;
86 rc->
range = (!cnd)*(rc->
range - rscaled*(p_tot - p)) + cnd*rscaled*(p - b);
92 unsigned int k, scale, total, symbol, low, high;
96 scale = rc->
range / total;
97 symbol = rc->
value / scale + 1;
98 symbol = total -
FFMIN(symbol, total);
100 for (k = 0; cdf[k] <= symbol; k++);
102 low = k ? cdf[k-1] : 0;
117 scale = rc->
range >> bits;
119 if (rc->
value >= scale) {
133 bits = (1 << bits) - 1;
150 value = av_mod_uintp2(rc->
rb.
cacheval, count);
184 uint32_t bits, k, scale, total;
187 total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 :
size;
189 scale = rc->
range / total;
190 k = rc->
value / scale + 1;
191 k = total -
FFMIN(k, total);
196 return FFMIN(k, size - 1);
214 uint32_t k, scale, symbol, total = (k0+1)*3 + k0;
215 scale = rc->
range / total;
216 symbol = rc->
value / scale + 1;
217 symbol = total -
FFMIN(symbol, total);
219 k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
222 (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
228 const uint32_t
a = val <= k0,
b = 2*a + 1;
230 val = b*(val + k0) - 3*a*k0;
236 uint32_t k, scale, symbol, total, low, center;
238 total = ((qn>>1) + 1) * ((qn>>1) + 1);
239 scale = rc->
range / total;
240 center = rc->
value / scale + 1;
241 center = total -
FFMIN(center, total);
243 if (center < total >> 1) {
244 k = (
ff_sqrt(8 * center + 1) - 1) >> 1;
245 low = k * (k + 1) >> 1;
248 k = (2*(qn + 1) -
ff_sqrt(8*(total - center - 1) + 1)) >> 1;
249 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
260 uint32_t symbol, low, total;
262 total = ((qn>>1) + 1) * ((qn>>1) + 1);
265 low = k * (k + 1) >> 1;
268 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
279 uint32_t scale, low = 0, center;
281 scale = rc->
range >> 15;
282 center = rc->
value / scale + 1;
283 center = (1 << 15) -
FFMIN(center, 1 << 15);
285 if (center >= symbol) {
288 symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
290 while (symbol > 1 && center >= low + 2 * symbol) {
294 symbol = (((symbol - 2) * decay) >> 15) + 1;
303 if (center < low + symbol)
316 uint32_t low = symbol;
317 int i = 1,
val =
FFABS(*value), pos = *value > 0;
322 symbol = ((32768 - 32 - symbol)*(16384 - decay)) >> 15;
323 for (; i <
val && symbol; i++) {
324 low += (symbol << 1) + 2;
325 symbol = (symbol*decay) >> 14;
328 low += (++symbol)*pos;
331 low += pos + (distance << 1);
332 symbol =
FFMIN(1, 32768 - low);
333 *value =
FFSIGN(*value)*(distance + i);
380 if (rc->
rem >= 0 || rc->
ext > 0)
384 memcpy(dst, rc->
buf, rng_bytes);
395 lap = &dst[rng_bytes] - rb_dst;
396 for (i = 0; i < lap; i++)
397 rb_dst[i] |= rb_src[i];
398 memcpy(&rb_dst[lap], &rb_src[lap],
FFMAX(rc->
rb.
bytes - lap, 0));
const char const char void * val
ptrdiff_t const GLvoid * data
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
static av_always_inline void opus_rc_dec_update(OpusRangeCoder *rc, uint32_t scale, uint32_t low, uint32_t high, uint32_t total)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
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)
#define OPUS_MAX_PACKET_SIZE
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_tri(OpusRangeCoder *rc, int qn)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static double cb(void *priv, double x, double y)
static av_cold int end(AVCodecContext *avctx)
static av_always_inline void opus_rc_enc_carryout(OpusRangeCoder *rc, int cbuf)
void ff_opus_rc_enc_uint_tri(OpusRangeCoder *rc, uint32_t k, int qn)
static const uint16_t mask[17]
static av_always_inline void opus_rc_enc_update(OpusRangeCoder *rc, uint32_t b, uint32_t p, uint32_t p_tot, const int ptwo)
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
static float distance(float x, float y, int band)
GLsizei GLboolean const GLfloat * value
static av_always_inline void opus_rc_enc_normalize(OpusRangeCoder *rc)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
void ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay)
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_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
uint8_t buf[OPUS_MAX_PACKET_SIZE+12]
void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count)
CELT: write 0 - 31 bits to the rawbits buffer.
void ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size)
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
static av_always_inline void opus_rc_dec_normalize(OpusRangeCoder *rc)
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)