1 /*
2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
4 * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
24
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)
31
33 {
37 return;
38 }
41 for (; rc->
ext > 0; rc->
ext--)
45 }
46
48 {
53 }
54 }
55
57 {
63 }
64 }
65
67 uint32_t low, uint32_t
high,
68 uint32_t total)
69 {
74 }
75
76 /* Main encoding function, this needs to go fast */
78 uint32_t p_tot, const int ptwo)
79 {
80 uint32_t rscaled, cnd = !!
b;
81 if (ptwo) /* Whole function is inlined so hopefully branch is optimized out */
83 else
84 rscaled = rc->
range/p_tot;
86 rc->
range = (!cnd)*(rc->
range - rscaled*(p_tot - p)) + cnd*rscaled*(p -
b);
88 }
89
91 {
92 unsigned int k,
scale, total, symbol, low,
high;
93
94 total = *cdf++;
95
98 symbol = total -
FFMIN(symbol, total);
99
100 for (k = 0; cdf[k] <= symbol; k++);
102 low = k ? cdf[k-1] : 0;
103
105
106 return k;
107 }
108
110 {
112 }
113
115 {
118
122 k = 0;
123 } else {
125 k = 1;
126 }
128 return k;
129 }
130
132 {
135 }
136
137 /**
138 * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise
139 */
141 {
143
148 }
149
154
156 }
157
158 /**
159 * CELT: write 0 - 31 bits to the rawbits buffer
160 */
162 {
164
168
176 }
177 }
178
179 /**
180 * CELT: read a uniform distribution
181 */
183 {
185
188
191 k = total -
FFMIN(k, total);
193
197 } else
198 return k;
199 }
200
201 /**
202 * CELT: write a uniformly distributed integer
203 */
205 {
209 }
210
212 {
213 /* Use a probability of 3 up to itheta=8192 and then use 1 after */
214 uint32_t k,
scale, symbol, total = (k0+1)*3 + k0;
217 symbol = total -
FFMIN(symbol, total);
218
219 k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2;
220
222 (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total);
223 return k;
224 }
225
227 {
228 const uint32_t
a =
val <= k0,
b = 2*
a + 1;
229 k0 = (k0 + 1) << 1;
232 }
233
235 {
236 uint32_t k,
scale, symbol, total, low, center;
237
238 total = ((qn>>1) + 1) * ((qn>>1) + 1);
241 center = total -
FFMIN(center, total);
242
243 if (center < total >> 1) {
244 k = (
ff_sqrt(8 * center + 1) - 1) >> 1;
245 low = k * (k + 1) >> 1;
246 symbol = k + 1;
247 } else {
248 k = (2*(qn + 1) -
ff_sqrt(8*(total - center - 1) + 1)) >> 1;
249 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
250 symbol = qn + 1 - k;
251 }
252
254
255 return k;
256 }
257
259 {
260 uint32_t symbol, low, total;
261
262 total = ((qn>>1) + 1) * ((qn>>1) + 1);
263
264 if (k <= qn >> 1) {
265 low = k * (k + 1) >> 1;
266 symbol = k + 1;
267 } else {
268 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1);
269 symbol = qn + 1 - k;
270 }
271
273 }
274
276 {
277 /* extends the range coder to model a Laplace distribution */
279 uint32_t
scale, low = 0, center;
280
283 center = (1 << 15) -
FFMIN(center, 1 << 15);
284
285 if (center >= symbol) {
287 low = symbol;
288 symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15);
289
290 while (symbol > 1 && center >= low + 2 * symbol) {
292 symbol *= 2;
293 low += symbol;
294 symbol = (((symbol - 2) * decay) >> 15) + 1;
295 }
296
297 if (symbol <= 1) {
301 }
302
303 if (center < low + symbol)
305 else
306 low += symbol;
307 }
308
310
312 }
313
315 {
316 uint32_t low = symbol;
320 return;
321 }
322 symbol = ((32768 - 32 - symbol)*(16384 - decay)) >> 15;
323 for (;
i <
val && symbol;
i++) {
324 low += (symbol << 1) + 2;
325 symbol = (symbol*decay) >> 14;
326 }
327 if (symbol) {
328 low += (++symbol)*
pos;
329 } else {
332 symbol =
FFMIN(1, 32768 - low);
334 }
336 }
337
339 {
343
348
349 return 0;
350 }
351
353 {
358 }
359
361 {
365
370 }
371
372 /* Finish what's left */
377 }
378
379 /* Flush out anything left or marked */
380 if (rc->
rem >= 0 || rc->
ext > 0)
382
384 memcpy(
dst, rc->
buf, rng_bytes);
385
387
388 /* Put the rawbits part, if any */
391 uint8_t *rb_src, *rb_dst;
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));
399 }
400 }
401
403 {
411 }