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