]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus_rc.h
Merge commit '37961044c6'
[ffmpeg] / libavcodec / opus_rc.h
1 /*
2  * Copyright (c) 2012 Andrew D'Addesio
3  * Copyright (c) 2013-2014 Mozilla Corporation
4  * Copyright (c) 2016 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
23 #ifndef AVCODEC_OPUS_RC_H
24 #define AVCODEC_OPUS_RC_H
25
26 #include <stdint.h>
27 #include "get_bits.h"
28
29 #define opus_ilog(i) (av_log2(i) + !!(i))
30
31 typedef struct RawBitsContext {
32     const uint8_t *position;
33     uint32_t bytes;
34     uint32_t cachelen;
35     uint32_t cacheval;
36 } RawBitsContext;
37
38 typedef struct OpusRangeCoder {
39     GetBitContext gb;
40     RawBitsContext rb;
41     uint32_t range;
42     uint32_t value;
43     uint32_t total_read_bits;
44 } OpusRangeCoder;
45
46 /**
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
49  */
50 static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
51 {
52     return rc->total_read_bits - av_log2(rc->range) - 1;
53 }
54
55 static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)
56 {
57     uint32_t i, total_bits, rcbuffer, range;
58
59     total_bits = rc->total_read_bits << 3;
60     rcbuffer   = av_log2(rc->range) + 1;
61     range      = rc->range >> (rcbuffer-16);
62
63     for (i = 0; i < 3; i++) {
64         int bit;
65         range = range * range >> 15;
66         bit = range >> 16;
67         rcbuffer = rcbuffer << 1 | bit;
68         range >>= bit;
69     }
70
71     return total_bits - rcbuffer;
72 }
73
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);
81
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);
84
85 #endif /* AVCODEC_OPUS_RC_H */