2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
4 * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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)
32 static av_always_inline void opus_rc_dec_normalize(OpusRangeCoder *rc)
34 while (rc->range <= OPUS_RC_BOT) {
35 rc->value = ((rc->value << OPUS_RC_SYM) | (get_bits(&rc->gb, OPUS_RC_SYM) ^ OPUS_RC_CEIL)) & (OPUS_RC_TOP - 1);
36 rc->range <<= OPUS_RC_SYM;
37 rc->total_bits += OPUS_RC_SYM;
41 static av_always_inline void opus_rc_dec_update(OpusRangeCoder *rc, uint32_t scale,
42 uint32_t low, uint32_t high,
45 rc->value -= scale * (total - high);
46 rc->range = low ? scale * (high - low)
47 : rc->range - scale * (total - high);
48 opus_rc_dec_normalize(rc);
51 uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
53 unsigned int k, scale, total, symbol, low, high;
57 scale = rc->range / total;
58 symbol = rc->value / scale + 1;
59 symbol = total - FFMIN(symbol, total);
61 for (k = 0; cdf[k] <= symbol; k++);
63 low = k ? cdf[k-1] : 0;
65 opus_rc_dec_update(rc, scale, low, high, total);
70 uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
73 scale = rc->range >> bits; // in this case, scale = symbol
75 if (rc->value >= scale) {
83 opus_rc_dec_normalize(rc);
88 * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
90 uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count)
94 while (rc->rb.bytes && rc->rb.cachelen < count) {
95 rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
100 value = av_mod_uintp2(rc->rb.cacheval, count);
101 rc->rb.cacheval >>= count;
102 rc->rb.cachelen -= count;
103 rc->total_bits += count;
109 * CELT: read a uniform distribution
111 uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
113 uint32_t bits, k, scale, total;
115 bits = opus_ilog(size - 1);
116 total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
118 scale = rc->range / total;
119 k = rc->value / scale + 1;
120 k = total - FFMIN(k, total);
121 opus_rc_dec_update(rc, scale, k, k + 1, total);
124 k = k << (bits - 8) | ff_opus_rc_get_raw(rc, bits - 8);
125 return FFMIN(k, size - 1);
130 uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
132 /* Use a probability of 3 up to itheta=8192 and then use 1 after */
133 uint32_t k, scale, symbol, total = (k0+1)*3 + k0;
134 scale = rc->range / total;
135 symbol = rc->value / scale + 1;
136 symbol = total - FFMIN(symbol, total);
138 k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
140 opus_rc_dec_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
141 (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
145 uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
147 uint32_t k, scale, symbol, total, low, center;
149 total = ((qn>>1) + 1) * ((qn>>1) + 1);
150 scale = rc->range / total;
151 center = rc->value / scale + 1;
152 center = total - FFMIN(center, total);
154 if (center < total >> 1) {
155 k = (ff_sqrt(8 * center + 1) - 1) >> 1;
156 low = k * (k + 1) >> 1;
159 k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
160 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
164 opus_rc_dec_update(rc, scale, low, low + symbol, total);
169 int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
171 /* extends the range coder to model a Laplace distribution */
173 uint32_t scale, low = 0, center;
175 scale = rc->range >> 15;
176 center = rc->value / scale + 1;
177 center = (1 << 15) - FFMIN(center, 1 << 15);
179 if (center >= symbol) {
182 symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
184 while (symbol > 1 && center >= low + 2 * symbol) {
188 symbol = (((symbol - 2) * decay) >> 15) + 1;
192 int distance = (center - low) >> 1;
197 if (center < low + symbol)
203 opus_rc_dec_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
208 int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
210 int ret = init_get_bits8(&rc->gb, data, size);
215 rc->value = 127 - get_bits(&rc->gb, 7);
217 opus_rc_dec_normalize(rc);
222 void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
224 rc->rb.position = rightend;
225 rc->rb.bytes = bytes;