annotate src/opus-1.3/celt/entcode.h @ 169:223a55898ab9 tip default

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