blob: 13d6c84ef0fa39205166d3e776134b9bc23275c5 [file] [log] [blame]
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -08001/* Copyright (c) 2001-2011 Timothy B. Terriberry
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04002 Copyright (c) 2008-2009 Xiph.Org Foundation */
Gregory Maxwellf40bbf72009-02-03 20:36:57 -05003/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
Gregory Maxwellf40bbf72009-02-03 20:36:57 -050015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040018 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Gregory Maxwellf40bbf72009-02-03 20:36:57 -050020 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040028#include "opus_types.h"
Gregory Maxwell7830cf12013-10-17 15:56:52 -070029#include "opus_defines.h"
Jean-Marc Valinabe043f2008-01-31 14:26:29 +110030
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110031#if !defined(_entcode_H)
32# define _entcode_H (1)
33# include <limits.h>
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080034# include <stddef.h>
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110035# include "ecintrin.h"
36
Jean-Marc Valinec5d01c2014-01-20 16:32:16 -050037extern const opus_uint32 SMALL_DIV_TABLE[129];
38
39#ifdef OPUS_ARM_ASM
40#define USE_SMALL_DIV_TABLE
41#endif
42
Timothy B. Terriberry9bac8c12011-03-02 16:24:32 -080043/*OPT: ec_window must be at least 32 bits, but if you have fast arithmetic on a
44 larger type, you can speed up the decoder by using it here.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040045typedef opus_uint32 ec_window;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080046typedef struct ec_ctx ec_ctx;
47typedef struct ec_ctx ec_enc;
48typedef struct ec_ctx ec_dec;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110049
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080050# define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT)
51
52/*The number of bits to use for the range-coded part of unsigned integers.*/
53# define EC_UINT_BITS (8)
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110054
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080055/*The resolution of fractional-precision bit usage measurements, i.e.,
56 3 => 1/8th bits.*/
57# define BITRES 3
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110058
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080059/*The entropy encoder/decoder context.
60 We use the same structure for both, so that common functions like ec_tell()
61 can be used on either one.*/
62struct ec_ctx{
63 /*Buffered input/output.*/
64 unsigned char *buf;
65 /*The size of the buffer.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040066 opus_uint32 storage;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080067 /*The offset at which the last byte containing raw bits was read/written.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040068 opus_uint32 end_offs;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080069 /*Bits that will be read from/written at the end.*/
70 ec_window end_window;
71 /*Number of valid bits in end_window.*/
72 int nend_bits;
73 /*The total number of whole bits read/written.
74 This does not include partial bits currently in the range coder.*/
75 int nbits_total;
76 /*The offset at which the next range coder byte will be read/written.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040077 opus_uint32 offs;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080078 /*The number of values in the current range.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040079 opus_uint32 rng;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080080 /*In the decoder: the difference between the top of the current range and
81 the input value, minus one.
82 In the encoder: the low end of the current range.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040083 opus_uint32 val;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080084 /*In the decoder: the saved normalization factor from ec_decode().
85 In the encoder: the number of oustanding carry propagating symbols.*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040086 opus_uint32 ext;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080087 /*A buffered input/output symbol, awaiting carry propagation.*/
88 int rem;
89 /*Nonzero if an error occurred.*/
90 int error;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110091};
92
Gregory Maxwell7830cf12013-10-17 15:56:52 -070093static OPUS_INLINE opus_uint32 ec_range_bytes(ec_ctx *_this){
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080094 return _this->offs;
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110095}
96
Gregory Maxwell7830cf12013-10-17 15:56:52 -070097static OPUS_INLINE unsigned char *ec_get_buffer(ec_ctx *_this){
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080098 return _this->buf;
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110099}
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100100
Gregory Maxwell7830cf12013-10-17 15:56:52 -0700101static OPUS_INLINE int ec_get_error(ec_ctx *_this){
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800102 return _this->error;
103}
104
105/*Returns the number of bits "used" by the encoded or decoded symbols so far.
106 This same number can be computed in either the encoder or the decoder, and is
107 suitable for making coding decisions.
108 Return: The number of bits.
109 This will always be slightly larger than the exact value (e.g., all
110 rounding error is in the positive direction).*/
Gregory Maxwell7830cf12013-10-17 15:56:52 -0700111static OPUS_INLINE int ec_tell(ec_ctx *_this){
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800112 return _this->nbits_total-EC_ILOG(_this->rng);
113}
114
115/*Returns the number of bits "used" by the encoded or decoded symbols so far.
116 This same number can be computed in either the encoder or the decoder, and is
117 suitable for making coding decisions.
118 Return: The number of bits scaled by 2**BITRES.
119 This will always be slightly larger than the exact value (e.g., all
120 rounding error is in the positive direction).*/
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400121opus_uint32 ec_tell_frac(ec_ctx *_this);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800122
Jean-Marc Valinec5d01c2014-01-20 16:32:16 -0500123/* Tested exhaustively for all n and for 1<=d<=256 */
124static OPUS_INLINE opus_uint32 celt_udiv(opus_uint32 n, opus_uint32 d) {
Jean-Marc Valin29354ff2014-01-21 10:39:33 -0500125 celt_assert(d>0);
Jean-Marc Valinec5d01c2014-01-20 16:32:16 -0500126#ifdef USE_SMALL_DIV_TABLE
127 if (d>256)
128 return n/d;
129 else {
130 opus_uint32 t, q;
131 t = EC_ILOG(d&-d);
132 q = (opus_uint64)SMALL_DIV_TABLE[d>>t]*(n>>(t-1))>>32;
133 return q+(n-q*d >= d);
134 }
135#else
136 return n/d;
137#endif
138}
139
Jean-Marc Valind9fb8a62014-01-30 11:16:24 -0500140static OPUS_INLINE opus_int32 celt_sudiv(opus_int32 n, opus_int32 d) {
141 celt_assert(d>0);
142#ifdef USE_SMALL_DIV_TABLE
143 if (n<0)
144 return -(opus_int32)celt_udiv(-n, d);
145 else
146 return celt_udiv(n, d);
147#else
148 return n/d;
149#endif
150}
151
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100152#endif