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