1 /* Copyright (c) 2001-2008 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #if !defined(_entdec_H)
33 # define _entdec_H (1)
38 typedef struct ec_dec ec_dec;
42 /*The entropy decoder.*/
44 /*The buffer to decode.*/
46 /*The remainder of a buffered input symbol.*/
48 /*The number of values in the current range.*/
50 /*The difference between the top of the current range and the input value.*/
52 /*Normalization factor.*/
54 /*Byte that will be written at the end*/
55 unsigned char end_byte;
56 /*Number of valid bits in end_byte*/
62 /*Initializes the decoder.
63 _buf: The input buffer to use.
64 Return: 0 on success, or a negative value on error.*/
65 void ec_dec_init(ec_dec *_this,ec_byte_buffer *_buf);
66 /*Calculates the cumulative frequency for the next symbol.
67 This can then be fed into the probability model to determine what that
68 symbol is, and the additional frequency information required to advance to
70 This function cannot be called more than once without a corresponding call to
71 ec_dec_update(), or decoding will not proceed correctly.
72 _ft: The total frequency of the symbols in the alphabet the next symbol was
74 Return: A cumulative frequency representing the encoded symbol.
75 If the cumulative frequency of all the symbols before the one that
76 was encoded was fl, and the cumulative frequency of all the symbols
77 up to and including the one encoded is fh, then the returned value
78 will fall in the range [fl,fh).*/
79 unsigned ec_decode(ec_dec *_this,unsigned _ft);
80 unsigned ec_decode_bin(ec_dec *_this,unsigned _bits);
81 unsigned ec_decode_raw(ec_dec *_this,unsigned bits);
83 /*Advance the decoder past the next symbol using the frequency information the
84 symbol was encoded with.
85 Exactly one call to ec_decode() must have been made so that all necessary
86 intermediate calculations are performed.
87 _fl: The cumulative frequency of all symbols that come before the symbol
89 _fh: The cumulative frequency of all symbols up to and including the symbol
91 Together with _fl, this defines the range [_fl,_fh) in which the value
92 returned above must fall.
93 _ft: The total frequency of the symbols in the alphabet the symbol decoded
95 This must be the same as passed to the preceding call to ec_decode().*/
96 void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,
98 /*Extracts a sequence of raw bits from the stream.
99 The bits must have been encoded with ec_enc_bits().
100 No call to ec_dec_update() is necessary after this call.
101 _ftb: The number of bits to extract.
102 This must be at least one, and no more than 32.
103 Return: The decoded bits.*/
104 ec_uint32 ec_dec_bits(ec_dec *_this,int _ftb);
105 /*Extracts a raw unsigned integer with a non-power-of-2 range from the stream.
106 The bits must have been encoded with ec_enc_uint().
107 No call to ec_dec_update() is necessary after this call.
108 _ft: The number of integers that can be decoded (one more than the max).
109 This must be at least one, and no more than 2**32-1.
110 Return: The decoded bits.*/
111 ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft);
113 /* Decode a bit that has a _prob/65536 probability of being a one */
114 int ec_dec_bit_prob(ec_dec *_this,unsigned _prob);
116 /*Returns the number of bits "used" by the encoded symbols so far.
117 This same number can be computed by the encoder, and is suitable for making
119 _b: The number of extra bits of precision to include.
120 At most 16 will be accurate.
121 Return: The number of bits scaled by 2**_b.
122 This will always be slightly larger than the exact value (e.g., all
123 rounding error is in the positive direction).*/
124 long ec_dec_tell(ec_dec *_this,int _b);