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