1*a58d3d2aSXin Li /* Copyright (c) 2001-2011 Timothy B. Terriberry
2*a58d3d2aSXin Li Copyright (c) 2008-2009 Xiph.Org Foundation */
3*a58d3d2aSXin Li /*
4*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without
5*a58d3d2aSXin Li modification, are permitted provided that the following conditions
6*a58d3d2aSXin Li are met:
7*a58d3d2aSXin Li
8*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright
9*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer.
10*a58d3d2aSXin Li
11*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright
12*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the
13*a58d3d2aSXin Li documentation and/or other materials provided with the distribution.
14*a58d3d2aSXin Li
15*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*a58d3d2aSXin Li ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*a58d3d2aSXin Li LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18*a58d3d2aSXin Li A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19*a58d3d2aSXin Li OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20*a58d3d2aSXin Li EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21*a58d3d2aSXin Li PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22*a58d3d2aSXin Li PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23*a58d3d2aSXin Li LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24*a58d3d2aSXin Li NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25*a58d3d2aSXin Li SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*a58d3d2aSXin Li */
27*a58d3d2aSXin Li
28*a58d3d2aSXin Li #include "opus_types.h"
29*a58d3d2aSXin Li #include "opus_defines.h"
30*a58d3d2aSXin Li
31*a58d3d2aSXin Li #if !defined(_entcode_H)
32*a58d3d2aSXin Li # define _entcode_H (1)
33*a58d3d2aSXin Li # include <limits.h>
34*a58d3d2aSXin Li # include <stddef.h>
35*a58d3d2aSXin Li # include "ecintrin.h"
36*a58d3d2aSXin Li
37*a58d3d2aSXin Li extern const opus_uint32 SMALL_DIV_TABLE[129];
38*a58d3d2aSXin Li
39*a58d3d2aSXin Li #ifdef OPUS_ARM_ASM
40*a58d3d2aSXin Li #define USE_SMALL_DIV_TABLE
41*a58d3d2aSXin Li #endif
42*a58d3d2aSXin Li
43*a58d3d2aSXin Li /*OPT: ec_window must be at least 32 bits, but if you have fast arithmetic on a
44*a58d3d2aSXin Li larger type, you can speed up the decoder by using it here.*/
45*a58d3d2aSXin Li typedef opus_uint32 ec_window;
46*a58d3d2aSXin Li typedef struct ec_ctx ec_ctx;
47*a58d3d2aSXin Li typedef struct ec_ctx ec_enc;
48*a58d3d2aSXin Li typedef struct ec_ctx ec_dec;
49*a58d3d2aSXin Li
50*a58d3d2aSXin Li # define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT)
51*a58d3d2aSXin Li
52*a58d3d2aSXin Li /*The number of bits to use for the range-coded part of unsigned integers.*/
53*a58d3d2aSXin Li # define EC_UINT_BITS (8)
54*a58d3d2aSXin Li
55*a58d3d2aSXin Li /*The resolution of fractional-precision bit usage measurements, i.e.,
56*a58d3d2aSXin Li 3 => 1/8th bits.*/
57*a58d3d2aSXin Li # define BITRES 3
58*a58d3d2aSXin Li
59*a58d3d2aSXin Li /*The entropy encoder/decoder context.
60*a58d3d2aSXin Li We use the same structure for both, so that common functions like ec_tell()
61*a58d3d2aSXin Li can be used on either one.*/
62*a58d3d2aSXin Li struct ec_ctx{
63*a58d3d2aSXin Li /*Buffered input/output.*/
64*a58d3d2aSXin Li unsigned char *buf;
65*a58d3d2aSXin Li /*The size of the buffer.*/
66*a58d3d2aSXin Li opus_uint32 storage;
67*a58d3d2aSXin Li /*The offset at which the last byte containing raw bits was read/written.*/
68*a58d3d2aSXin Li opus_uint32 end_offs;
69*a58d3d2aSXin Li /*Bits that will be read from/written at the end.*/
70*a58d3d2aSXin Li ec_window end_window;
71*a58d3d2aSXin Li /*Number of valid bits in end_window.*/
72*a58d3d2aSXin Li int nend_bits;
73*a58d3d2aSXin Li /*The total number of whole bits read/written.
74*a58d3d2aSXin Li This does not include partial bits currently in the range coder.*/
75*a58d3d2aSXin Li int nbits_total;
76*a58d3d2aSXin Li /*The offset at which the next range coder byte will be read/written.*/
77*a58d3d2aSXin Li opus_uint32 offs;
78*a58d3d2aSXin Li /*The number of values in the current range.*/
79*a58d3d2aSXin Li opus_uint32 rng;
80*a58d3d2aSXin Li /*In the decoder: the difference between the top of the current range and
81*a58d3d2aSXin Li the input value, minus one.
82*a58d3d2aSXin Li In the encoder: the low end of the current range.*/
83*a58d3d2aSXin Li opus_uint32 val;
84*a58d3d2aSXin Li /*In the decoder: the saved normalization factor from ec_decode().
85*a58d3d2aSXin Li In the encoder: the number of oustanding carry propagating symbols.*/
86*a58d3d2aSXin Li opus_uint32 ext;
87*a58d3d2aSXin Li /*A buffered input/output symbol, awaiting carry propagation.*/
88*a58d3d2aSXin Li int rem;
89*a58d3d2aSXin Li /*Nonzero if an error occurred.*/
90*a58d3d2aSXin Li int error;
91*a58d3d2aSXin Li };
92*a58d3d2aSXin Li
ec_range_bytes(ec_ctx * _this)93*a58d3d2aSXin Li static OPUS_INLINE opus_uint32 ec_range_bytes(ec_ctx *_this){
94*a58d3d2aSXin Li return _this->offs;
95*a58d3d2aSXin Li }
96*a58d3d2aSXin Li
ec_get_buffer(ec_ctx * _this)97*a58d3d2aSXin Li static OPUS_INLINE unsigned char *ec_get_buffer(ec_ctx *_this){
98*a58d3d2aSXin Li return _this->buf;
99*a58d3d2aSXin Li }
100*a58d3d2aSXin Li
ec_get_error(ec_ctx * _this)101*a58d3d2aSXin Li static OPUS_INLINE int ec_get_error(ec_ctx *_this){
102*a58d3d2aSXin Li return _this->error;
103*a58d3d2aSXin Li }
104*a58d3d2aSXin Li
105*a58d3d2aSXin Li /*Returns the number of bits "used" by the encoded or decoded symbols so far.
106*a58d3d2aSXin Li This same number can be computed in either the encoder or the decoder, and is
107*a58d3d2aSXin Li suitable for making coding decisions.
108*a58d3d2aSXin Li Return: The number of bits.
109*a58d3d2aSXin Li This will always be slightly larger than the exact value (e.g., all
110*a58d3d2aSXin Li rounding error is in the positive direction).*/
ec_tell(ec_ctx * _this)111*a58d3d2aSXin Li static OPUS_INLINE int ec_tell(ec_ctx *_this){
112*a58d3d2aSXin Li return _this->nbits_total-EC_ILOG(_this->rng);
113*a58d3d2aSXin Li }
114*a58d3d2aSXin Li
115*a58d3d2aSXin Li /*Returns the number of bits "used" by the encoded or decoded symbols so far.
116*a58d3d2aSXin Li This same number can be computed in either the encoder or the decoder, and is
117*a58d3d2aSXin Li suitable for making coding decisions.
118*a58d3d2aSXin Li Return: The number of bits scaled by 2**BITRES.
119*a58d3d2aSXin Li This will always be slightly larger than the exact value (e.g., all
120*a58d3d2aSXin Li rounding error is in the positive direction).*/
121*a58d3d2aSXin Li opus_uint32 ec_tell_frac(ec_ctx *_this);
122*a58d3d2aSXin Li
123*a58d3d2aSXin Li /* Tested exhaustively for all n and for 1<=d<=256 */
celt_udiv(opus_uint32 n,opus_uint32 d)124*a58d3d2aSXin Li static OPUS_INLINE opus_uint32 celt_udiv(opus_uint32 n, opus_uint32 d) {
125*a58d3d2aSXin Li celt_sig_assert(d>0);
126*a58d3d2aSXin Li #ifdef USE_SMALL_DIV_TABLE
127*a58d3d2aSXin Li if (d>256)
128*a58d3d2aSXin Li return n/d;
129*a58d3d2aSXin Li else {
130*a58d3d2aSXin Li opus_uint32 t, q;
131*a58d3d2aSXin Li t = EC_ILOG(d&-d);
132*a58d3d2aSXin Li q = (opus_uint64)SMALL_DIV_TABLE[d>>t]*(n>>(t-1))>>32;
133*a58d3d2aSXin Li return q+(n-q*d >= d);
134*a58d3d2aSXin Li }
135*a58d3d2aSXin Li #else
136*a58d3d2aSXin Li return n/d;
137*a58d3d2aSXin Li #endif
138*a58d3d2aSXin Li }
139*a58d3d2aSXin Li
celt_sudiv(opus_int32 n,opus_int32 d)140*a58d3d2aSXin Li static OPUS_INLINE opus_int32 celt_sudiv(opus_int32 n, opus_int32 d) {
141*a58d3d2aSXin Li celt_sig_assert(d>0);
142*a58d3d2aSXin Li #ifdef USE_SMALL_DIV_TABLE
143*a58d3d2aSXin Li if (n<0)
144*a58d3d2aSXin Li return -(opus_int32)celt_udiv(-n, d);
145*a58d3d2aSXin Li else
146*a58d3d2aSXin Li return celt_udiv(n, d);
147*a58d3d2aSXin Li #else
148*a58d3d2aSXin Li return n/d;
149*a58d3d2aSXin Li #endif
150*a58d3d2aSXin Li }
151*a58d3d2aSXin Li
152*a58d3d2aSXin Li #endif
153