cannam@154: /* Copyright (c) 2001-2011 Timothy B. Terriberry cannam@154: Copyright (c) 2008-2009 Xiph.Org Foundation */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: #include "opus_types.h" cannam@154: #include "opus_defines.h" cannam@154: cannam@154: #if !defined(_entcode_H) cannam@154: # define _entcode_H (1) cannam@154: # include cannam@154: # include cannam@154: # include "ecintrin.h" cannam@154: cannam@154: extern const opus_uint32 SMALL_DIV_TABLE[129]; cannam@154: cannam@154: #ifdef OPUS_ARM_ASM cannam@154: #define USE_SMALL_DIV_TABLE cannam@154: #endif cannam@154: cannam@154: /*OPT: ec_window must be at least 32 bits, but if you have fast arithmetic on a cannam@154: larger type, you can speed up the decoder by using it here.*/ cannam@154: typedef opus_uint32 ec_window; cannam@154: typedef struct ec_ctx ec_ctx; cannam@154: typedef struct ec_ctx ec_enc; cannam@154: typedef struct ec_ctx ec_dec; cannam@154: cannam@154: # define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT) cannam@154: cannam@154: /*The number of bits to use for the range-coded part of unsigned integers.*/ cannam@154: # define EC_UINT_BITS (8) cannam@154: cannam@154: /*The resolution of fractional-precision bit usage measurements, i.e., cannam@154: 3 => 1/8th bits.*/ cannam@154: # define BITRES 3 cannam@154: cannam@154: /*The entropy encoder/decoder context. cannam@154: We use the same structure for both, so that common functions like ec_tell() cannam@154: can be used on either one.*/ cannam@154: struct ec_ctx{ cannam@154: /*Buffered input/output.*/ cannam@154: unsigned char *buf; cannam@154: /*The size of the buffer.*/ cannam@154: opus_uint32 storage; cannam@154: /*The offset at which the last byte containing raw bits was read/written.*/ cannam@154: opus_uint32 end_offs; cannam@154: /*Bits that will be read from/written at the end.*/ cannam@154: ec_window end_window; cannam@154: /*Number of valid bits in end_window.*/ cannam@154: int nend_bits; cannam@154: /*The total number of whole bits read/written. cannam@154: This does not include partial bits currently in the range coder.*/ cannam@154: int nbits_total; cannam@154: /*The offset at which the next range coder byte will be read/written.*/ cannam@154: opus_uint32 offs; cannam@154: /*The number of values in the current range.*/ cannam@154: opus_uint32 rng; cannam@154: /*In the decoder: the difference between the top of the current range and cannam@154: the input value, minus one. cannam@154: In the encoder: the low end of the current range.*/ cannam@154: opus_uint32 val; cannam@154: /*In the decoder: the saved normalization factor from ec_decode(). cannam@154: In the encoder: the number of oustanding carry propagating symbols.*/ cannam@154: opus_uint32 ext; cannam@154: /*A buffered input/output symbol, awaiting carry propagation.*/ cannam@154: int rem; cannam@154: /*Nonzero if an error occurred.*/ cannam@154: int error; cannam@154: }; cannam@154: cannam@154: static OPUS_INLINE opus_uint32 ec_range_bytes(ec_ctx *_this){ cannam@154: return _this->offs; cannam@154: } cannam@154: cannam@154: static OPUS_INLINE unsigned char *ec_get_buffer(ec_ctx *_this){ cannam@154: return _this->buf; cannam@154: } cannam@154: cannam@154: static OPUS_INLINE int ec_get_error(ec_ctx *_this){ cannam@154: return _this->error; cannam@154: } cannam@154: cannam@154: /*Returns the number of bits "used" by the encoded or decoded symbols so far. cannam@154: This same number can be computed in either the encoder or the decoder, and is cannam@154: suitable for making coding decisions. cannam@154: Return: The number of bits. cannam@154: This will always be slightly larger than the exact value (e.g., all cannam@154: rounding error is in the positive direction).*/ cannam@154: static OPUS_INLINE int ec_tell(ec_ctx *_this){ cannam@154: return _this->nbits_total-EC_ILOG(_this->rng); cannam@154: } cannam@154: cannam@154: /*Returns the number of bits "used" by the encoded or decoded symbols so far. cannam@154: This same number can be computed in either the encoder or the decoder, and is cannam@154: suitable for making coding decisions. cannam@154: Return: The number of bits scaled by 2**BITRES. cannam@154: This will always be slightly larger than the exact value (e.g., all cannam@154: rounding error is in the positive direction).*/ cannam@154: opus_uint32 ec_tell_frac(ec_ctx *_this); cannam@154: cannam@154: /* Tested exhaustively for all n and for 1<=d<=256 */ cannam@154: static OPUS_INLINE opus_uint32 celt_udiv(opus_uint32 n, opus_uint32 d) { cannam@154: celt_sig_assert(d>0); cannam@154: #ifdef USE_SMALL_DIV_TABLE cannam@154: if (d>256) cannam@154: return n/d; cannam@154: else { cannam@154: opus_uint32 t, q; cannam@154: t = EC_ILOG(d&-d); cannam@154: q = (opus_uint64)SMALL_DIV_TABLE[d>>t]*(n>>(t-1))>>32; cannam@154: return q+(n-q*d >= d); cannam@154: } cannam@154: #else cannam@154: return n/d; cannam@154: #endif cannam@154: } cannam@154: cannam@154: static OPUS_INLINE opus_int32 celt_sudiv(opus_int32 n, opus_int32 d) { cannam@154: celt_sig_assert(d>0); cannam@154: #ifdef USE_SMALL_DIV_TABLE cannam@154: if (n<0) cannam@154: return -(opus_int32)celt_udiv(-n, d); cannam@154: else cannam@154: return celt_udiv(n, d); cannam@154: #else cannam@154: return n/d; cannam@154: #endif cannam@154: } cannam@154: cannam@154: #endif