1*a58d3d2aSXin Li /* Copyright (c) 2001-2011 Timothy B. Terriberry 2*a58d3d2aSXin Li Copyright (c) 2008-2009 Xiph.Org Foundation */ 3*a58d3d2aSXin Li /* 4*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without 5*a58d3d2aSXin Li modification, are permitted provided that the following conditions 6*a58d3d2aSXin Li are met: 7*a58d3d2aSXin Li 8*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright 9*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer. 10*a58d3d2aSXin Li 11*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright 12*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the 13*a58d3d2aSXin Li documentation and/or other materials provided with the distribution. 14*a58d3d2aSXin Li 15*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16*a58d3d2aSXin Li ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17*a58d3d2aSXin Li LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18*a58d3d2aSXin Li A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 19*a58d3d2aSXin Li OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20*a58d3d2aSXin Li EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21*a58d3d2aSXin Li PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22*a58d3d2aSXin Li PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23*a58d3d2aSXin Li LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24*a58d3d2aSXin Li NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25*a58d3d2aSXin Li SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26*a58d3d2aSXin Li */ 27*a58d3d2aSXin Li 28*a58d3d2aSXin Li #if !defined(_entenc_H) 29*a58d3d2aSXin Li # define _entenc_H (1) 30*a58d3d2aSXin Li # include <stddef.h> 31*a58d3d2aSXin Li # include "entcode.h" 32*a58d3d2aSXin Li 33*a58d3d2aSXin Li /*Initializes the encoder. 34*a58d3d2aSXin Li _buf: The buffer to store output bytes in. 35*a58d3d2aSXin Li _size: The size of the buffer, in chars.*/ 36*a58d3d2aSXin Li void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size); 37*a58d3d2aSXin Li /*Encodes a symbol given its frequency information. 38*a58d3d2aSXin Li The frequency information must be discernable by the decoder, assuming it 39*a58d3d2aSXin Li has read only the previous symbols from the stream. 40*a58d3d2aSXin Li It is allowable to change the frequency information, or even the entire 41*a58d3d2aSXin Li source alphabet, so long as the decoder can tell from the context of the 42*a58d3d2aSXin Li previously encoded information that it is supposed to do so as well. 43*a58d3d2aSXin Li _fl: The cumulative frequency of all symbols that come before the one to be 44*a58d3d2aSXin Li encoded. 45*a58d3d2aSXin Li _fh: The cumulative frequency of all symbols up to and including the one to 46*a58d3d2aSXin Li be encoded. 47*a58d3d2aSXin Li Together with _fl, this defines the range [_fl,_fh) in which the 48*a58d3d2aSXin Li decoded value will fall. 49*a58d3d2aSXin Li _ft: The sum of the frequencies of all the symbols*/ 50*a58d3d2aSXin Li void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft); 51*a58d3d2aSXin Li 52*a58d3d2aSXin Li /*Equivalent to ec_encode() with _ft==1<<_bits.*/ 53*a58d3d2aSXin Li void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits); 54*a58d3d2aSXin Li 55*a58d3d2aSXin Li /* Encode a bit that has a 1/(1<<_logp) probability of being a one */ 56*a58d3d2aSXin Li void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp); 57*a58d3d2aSXin Li 58*a58d3d2aSXin Li /*Encodes a symbol given an "inverse" CDF table. 59*a58d3d2aSXin Li _s: The index of the symbol to encode. 60*a58d3d2aSXin Li _icdf: The "inverse" CDF, such that symbol _s falls in the range 61*a58d3d2aSXin Li [_s>0?ft-_icdf[_s-1]:0,ft-_icdf[_s]), where ft=1<<_ftb. 62*a58d3d2aSXin Li The values must be monotonically non-increasing, and the last value 63*a58d3d2aSXin Li must be 0. 64*a58d3d2aSXin Li _ftb: The number of bits of precision in the cumulative distribution.*/ 65*a58d3d2aSXin Li void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb); 66*a58d3d2aSXin Li 67*a58d3d2aSXin Li /*Encodes a symbol given an "inverse" CDF table. 68*a58d3d2aSXin Li _s: The index of the symbol to encode. 69*a58d3d2aSXin Li _icdf: The "inverse" CDF, such that symbol _s falls in the range 70*a58d3d2aSXin Li [_s>0?ft-_icdf[_s-1]:0,ft-_icdf[_s]), where ft=1<<_ftb. 71*a58d3d2aSXin Li The values must be monotonically non-increasing, and the last value 72*a58d3d2aSXin Li must be 0. 73*a58d3d2aSXin Li _ftb: The number of bits of precision in the cumulative distribution.*/ 74*a58d3d2aSXin Li void ec_enc_icdf16(ec_enc *_this,int _s,const opus_uint16 *_icdf,unsigned _ftb); 75*a58d3d2aSXin Li 76*a58d3d2aSXin Li /*Encodes a raw unsigned integer in the stream. 77*a58d3d2aSXin Li _fl: The integer to encode. 78*a58d3d2aSXin Li _ft: The number of integers that can be encoded (one more than the max). 79*a58d3d2aSXin Li This must be at least 2, and no more than 2**32-1.*/ 80*a58d3d2aSXin Li void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft); 81*a58d3d2aSXin Li 82*a58d3d2aSXin Li /*Encodes a sequence of raw bits in the stream. 83*a58d3d2aSXin Li _fl: The bits to encode. 84*a58d3d2aSXin Li _ftb: The number of bits to encode. 85*a58d3d2aSXin Li This must be between 1 and 25, inclusive.*/ 86*a58d3d2aSXin Li void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _ftb); 87*a58d3d2aSXin Li 88*a58d3d2aSXin Li /*Overwrites a few bits at the very start of an existing stream, after they 89*a58d3d2aSXin Li have already been encoded. 90*a58d3d2aSXin Li This makes it possible to have a few flags up front, where it is easy for 91*a58d3d2aSXin Li decoders to access them without parsing the whole stream, even if their 92*a58d3d2aSXin Li values are not determined until late in the encoding process, without having 93*a58d3d2aSXin Li to buffer all the intermediate symbols in the encoder. 94*a58d3d2aSXin Li In order for this to work, at least _nbits bits must have already been 95*a58d3d2aSXin Li encoded using probabilities that are an exact power of two. 96*a58d3d2aSXin Li The encoder can verify the number of encoded bits is sufficient, but cannot 97*a58d3d2aSXin Li check this latter condition. 98*a58d3d2aSXin Li _val: The bits to encode (in the least _nbits significant bits). 99*a58d3d2aSXin Li They will be decoded in order from most-significant to least. 100*a58d3d2aSXin Li _nbits: The number of bits to overwrite. 101*a58d3d2aSXin Li This must be no more than 8.*/ 102*a58d3d2aSXin Li void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits); 103*a58d3d2aSXin Li 104*a58d3d2aSXin Li /*Compacts the data to fit in the target size. 105*a58d3d2aSXin Li This moves up the raw bits at the end of the current buffer so they are at 106*a58d3d2aSXin Li the end of the new buffer size. 107*a58d3d2aSXin Li The caller must ensure that the amount of data that's already been written 108*a58d3d2aSXin Li will fit in the new size. 109*a58d3d2aSXin Li _size: The number of bytes in the new buffer. 110*a58d3d2aSXin Li This must be large enough to contain the bits already written, and 111*a58d3d2aSXin Li must be no larger than the existing size.*/ 112*a58d3d2aSXin Li void ec_enc_shrink(ec_enc *_this,opus_uint32 _size); 113*a58d3d2aSXin Li 114*a58d3d2aSXin Li /*Indicates that there are no more symbols to encode. 115*a58d3d2aSXin Li All reamining output bytes are flushed to the output buffer. 116*a58d3d2aSXin Li ec_enc_init() must be called before the encoder can be used again.*/ 117*a58d3d2aSXin Li void ec_enc_done(ec_enc *_this); 118*a58d3d2aSXin Li 119*a58d3d2aSXin Li #endif 120