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 static av_always_inline void opus_rc_dec_normalize(OpusRangeCoder *rc)
27 while (rc->range <= 1<<23) {
28 rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1);
30 rc->total_read_bits += 8;
34 static av_always_inline void opus_rc_dec_update(OpusRangeCoder *rc, uint32_t scale,
35 uint32_t low, uint32_t high,
38 rc->value -= scale * (total - high);
39 rc->range = low ? scale * (high - low)
40 : rc->range - scale * (total - high);
41 opus_rc_dec_normalize(rc);
44 uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
46 unsigned int k, scale, total, symbol, low, high;
50 scale = rc->range / total;
51 symbol = rc->value / scale + 1;
52 symbol = total - FFMIN(symbol, total);
54 for (k = 0; cdf[k] <= symbol; k++);
56 low = k ? cdf[k-1] : 0;
58 opus_rc_dec_update(rc, scale, low, high, total);
63 uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
66 scale = rc->range >> bits; // in this case, scale = symbol
68 if (rc->value >= scale) {
76 opus_rc_dec_normalize(rc);
81 * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
83 uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count)
87 while (rc->rb.bytes && rc->rb.cachelen < count) {
88 rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen;
93 value = av_mod_uintp2(rc->rb.cacheval, count);
94 rc->rb.cacheval >>= count;
95 rc->rb.cachelen -= count;
96 rc->total_read_bits += count;
102 * CELT: read a uniform distribution
104 uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
106 uint32_t bits, k, scale, total;
108 bits = opus_ilog(size - 1);
109 total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size;
111 scale = rc->range / total;
112 k = rc->value / scale + 1;
113 k = total - FFMIN(k, total);
114 opus_rc_dec_update(rc, scale, k, k + 1, total);
117 k = k << (bits - 8) | ff_opus_rc_get_raw(rc, bits - 8);
118 return FFMIN(k, size - 1);
123 uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0)
125 /* Use a probability of 3 up to itheta=8192 and then use 1 after */
126 uint32_t k, scale, symbol, total = (k0+1)*3 + k0;
127 scale = rc->range / total;
128 symbol = rc->value / scale + 1;
129 symbol = total - FFMIN(symbol, total);
131 k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
133 opus_rc_dec_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1),
134 (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
138 uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn)
140 uint32_t k, scale, symbol, total, low, center;
142 total = ((qn>>1) + 1) * ((qn>>1) + 1);
143 scale = rc->range / total;
144 center = rc->value / scale + 1;
145 center = total - FFMIN(center, total);
147 if (center < total >> 1) {
148 k = (ff_sqrt(8 * center + 1) - 1) >> 1;
149 low = k * (k + 1) >> 1;
152 k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1;
153 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
157 opus_rc_dec_update(rc, scale, low, low + symbol, total);
162 int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay)
164 /* extends the range coder to model a Laplace distribution */
166 uint32_t scale, low = 0, center;
168 scale = rc->range >> 15;
169 center = rc->value / scale + 1;
170 center = (1 << 15) - FFMIN(center, 1 << 15);
172 if (center >= symbol) {
175 symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
177 while (symbol > 1 && center >= low + 2 * symbol) {
181 symbol = (((symbol - 2) * decay) >> 15) + 1;
185 int distance = (center - low) >> 1;
190 if (center < low + symbol)
196 opus_rc_dec_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768);
201 int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
203 int ret = init_get_bits8(&rc->gb, data, size);
208 rc->value = 127 - get_bits(&rc->gb, 7);
209 rc->total_read_bits = 9;
210 opus_rc_dec_normalize(rc);
215 void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
217 rc->rb.position = rightend;
218 rc->rb.bytes = bytes;