1 /* (C) 2001-2008 Timothy B. Terriberry
2 (C) 2008 Jean-Marc Valin */
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.
42 /*A multiply-free range encoder.
43 See mfrngdec.c and the references for implementation details
44 \cite{Mar79,MNW98,SM98}.
47 author="Martin, G.N.N.",
48 title="Range encoding: an algorithm for removing redundancy from a digitised
50 booktitle="Video \& Data Recording Conference",
52 address="Southampton",
56 author="Alistair Moffat and Radford Neal and Ian H. Witten",
57 title="Arithmetic Coding Revisited",
58 journal="{ACM} Transactions on Information Systems",
64 URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf"
67 author="Lang Stuiver and Alistair Moffat",
68 title="Piecewise Integer Mapping for Arithmetic Coding",
69 booktitle="Proceedings of the {IEEE} Data Compression Conference",
71 address="Snowbird, UT",
78 /*Outputs a symbol, with a carry bit.
79 If there is a potential to propagate a carry over several symbols, they are
80 buffered until it can be determined whether or not an actual carry will
82 If the counter for the buffered symbols overflows, then the stream becomes
84 This gives a theoretical limit of a few billion symbols in a single packet on
86 The alternative is to truncate the range in order to force a carry, but
87 requires similar carry tracking in the decoder, needlessly slowing it down.*/
88 static void ec_enc_carry_out(ec_enc *_this,int _c){
90 /*No further carry propagation possible, flush buffer.*/
92 carry=_c>>EC_SYM_BITS;
93 /*Don't output a byte on the first write.
94 This compare should be taken care of by branch-prediction thereafter.*/
95 if(_this->rem>=0)ec_byte_write1(_this->buf,_this->rem+carry);
98 sym=EC_SYM_MAX+carry&EC_SYM_MAX;
99 do ec_byte_write1(_this->buf,sym);
100 while(--(_this->ext)>0);
102 _this->rem=_c&EC_SYM_MAX;
107 static inline void ec_enc_normalize(ec_enc *_this){
108 /*If the range is too small, output some bits and rescale it.*/
109 while(_this->rng<=EC_CODE_BOT){
110 ec_enc_carry_out(_this,(int)(_this->low>>EC_CODE_SHIFT));
111 /*Move the next-to-high-order symbol into the high-order position.*/
112 _this->low=_this->low<<EC_SYM_BITS&EC_CODE_TOP-1;
113 _this->rng<<=EC_SYM_BITS;
117 void ec_enc_init(ec_enc *_this,ec_byte_buffer *_buf){
122 _this->rng=EC_CODE_TOP;
125 void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
134 /*Step 1: we want ft in the range of [rng/2,rng).
135 The high-order bits of the rng and ft are computed via a logarithm.
136 This could also be done on some architectures with some custom assembly,
137 which would provide even more speed.*/
138 nrm=EC_ILOG(_this->rng)-EC_ILOG(_ft);
139 /*Having the same high order bit may be too much.
140 We may need to shift one less to ensure that ft is actually in the proper
142 ft=(ec_uint32)_ft<<nrm;
146 /*We then scale everything by the computed power of 2.*/
147 fl=(ec_uint32)_fl<<nrm;
148 fh=(ec_uint32)_fh<<nrm;
149 /*Step 2: compute the two values of the partition function.
150 d is the splitting point of the interval [0,ft).*/
154 /*Step 3: Update the end-point and range of the interval.*/
157 /*Step 4: Normalize the interval.*/
158 ec_enc_normalize(_this);
161 void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits){
162 ec_encode(_this, _fl, _fh, 1U<<bits);
165 long ec_enc_tell(ec_enc *_this,int _b){
169 nbits=(ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext)*EC_SYM_BITS;
170 /*To handle the non-integral number of bits still left in the encoder state,
171 we compute the number of bits of low that must be encoded to ensure that
172 the value is inside the range for any possible subsequent bits.
173 Note that this is subtly different than the actual value we would end the
174 stream with, which tries to make as many of the trailing bits zeros as
178 l=EC_ILOG(_this->rng);
190 void ec_enc_done(ec_enc *_this){
191 /*We compute the integer in the current interval that has the largest number
192 of trailing zeros, and write that to the stream.
193 This is guaranteed to yield the smallest possible encoding.*/
197 /*Ensure that the end value is in the range.*/
198 if(end-_this->low>=_this->rng){
203 end=_this->low+msk&~msk|msk+1;
205 while(end-_this->low>=_this->rng);
207 /*The remaining output is the next free end.*/
209 ec_enc_carry_out(_this,end>>EC_CODE_SHIFT);
210 end=end<<EC_SYM_BITS&EC_CODE_TOP-1;
213 /*If we have a buffered byte flush it into the output buffer.*/
214 if(_this->rem>0||_this->ext>0){
215 ec_enc_carry_out(_this,0);