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
23 #ifndef AVCODEC_OPUS_RC_H
24 #define AVCODEC_OPUS_RC_H
29 #define opus_ilog(i) (av_log2(i) + !!(i))
31 typedef struct RawBitsContext {
32 const uint8_t *position;
38 typedef struct OpusRangeCoder {
43 uint32_t total_read_bits;
47 * CELT: estimate bits of entropy that have thus far been consumed for the
48 * current CELT frame, to integer and fractional (1/8th bit) precision
50 static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
52 return rc->total_read_bits - av_log2(rc->range) - 1;
55 static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
57 uint32_t i, total_bits, rcbuffer, range;
59 total_bits = rc->total_read_bits << 3;
60 rcbuffer = av_log2(rc->range) + 1;
61 range = rc->range >> (rcbuffer-16);
63 for (i = 0; i < 3; i++) {
65 range = range * range >> 15;
67 rcbuffer = rcbuffer << 1 | bit;
71 return total_bits - rcbuffer;
74 uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf);
75 uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits);
76 uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size);
77 uint32_t ff_opus_rc_dec_uint_step(OpusRangeCoder *rc, int k0);
78 uint32_t ff_opus_rc_dec_uint_tri(OpusRangeCoder *rc, int qn);
79 uint32_t ff_opus_rc_get_raw(OpusRangeCoder *rc, uint32_t count);
80 int ff_opus_rc_dec_laplace(OpusRangeCoder *rc, uint32_t symbol, int decay);
82 int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size);
83 void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes);
85 #endif /* AVCODEC_OPUS_RC_H */