cannam@154: /* Copyright (c) 2001-2011 Timothy B. Terriberry cannam@154: Copyright (c) 2008-2009 Xiph.Org Foundation */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: #if !defined(_entenc_H) cannam@154: # define _entenc_H (1) cannam@154: # include cannam@154: # include "entcode.h" cannam@154: cannam@154: /*Initializes the encoder. cannam@154: _buf: The buffer to store output bytes in. cannam@154: _size: The size of the buffer, in chars.*/ cannam@154: void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size); cannam@154: /*Encodes a symbol given its frequency information. cannam@154: The frequency information must be discernable by the decoder, assuming it cannam@154: has read only the previous symbols from the stream. cannam@154: It is allowable to change the frequency information, or even the entire cannam@154: source alphabet, so long as the decoder can tell from the context of the cannam@154: previously encoded information that it is supposed to do so as well. cannam@154: _fl: The cumulative frequency of all symbols that come before the one to be cannam@154: encoded. cannam@154: _fh: The cumulative frequency of all symbols up to and including the one to cannam@154: be encoded. cannam@154: Together with _fl, this defines the range [_fl,_fh) in which the cannam@154: decoded value will fall. cannam@154: _ft: The sum of the frequencies of all the symbols*/ cannam@154: void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft); cannam@154: cannam@154: /*Equivalent to ec_encode() with _ft==1<<_bits.*/ cannam@154: void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits); cannam@154: cannam@154: /* Encode a bit that has a 1/(1<<_logp) probability of being a one */ cannam@154: void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp); cannam@154: cannam@154: /*Encodes a symbol given an "inverse" CDF table. cannam@154: _s: The index of the symbol to encode. cannam@154: _icdf: The "inverse" CDF, such that symbol _s falls in the range cannam@154: [_s>0?ft-_icdf[_s-1]:0,ft-_icdf[_s]), where ft=1<<_ftb. cannam@154: The values must be monotonically non-increasing, and the last value cannam@154: must be 0. cannam@154: _ftb: The number of bits of precision in the cumulative distribution.*/ cannam@154: void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb); cannam@154: cannam@154: /*Encodes a raw unsigned integer in the stream. cannam@154: _fl: The integer to encode. cannam@154: _ft: The number of integers that can be encoded (one more than the max). cannam@154: This must be at least 2, and no more than 2**32-1.*/ cannam@154: void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft); cannam@154: cannam@154: /*Encodes a sequence of raw bits in the stream. cannam@154: _fl: The bits to encode. cannam@154: _ftb: The number of bits to encode. cannam@154: This must be between 1 and 25, inclusive.*/ cannam@154: void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _ftb); cannam@154: cannam@154: /*Overwrites a few bits at the very start of an existing stream, after they cannam@154: have already been encoded. cannam@154: This makes it possible to have a few flags up front, where it is easy for cannam@154: decoders to access them without parsing the whole stream, even if their cannam@154: values are not determined until late in the encoding process, without having cannam@154: to buffer all the intermediate symbols in the encoder. cannam@154: In order for this to work, at least _nbits bits must have already been cannam@154: encoded using probabilities that are an exact power of two. cannam@154: The encoder can verify the number of encoded bits is sufficient, but cannot cannam@154: check this latter condition. cannam@154: _val: The bits to encode (in the least _nbits significant bits). cannam@154: They will be decoded in order from most-significant to least. cannam@154: _nbits: The number of bits to overwrite. cannam@154: This must be no more than 8.*/ cannam@154: void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits); cannam@154: cannam@154: /*Compacts the data to fit in the target size. cannam@154: This moves up the raw bits at the end of the current buffer so they are at cannam@154: the end of the new buffer size. cannam@154: The caller must ensure that the amount of data that's already been written cannam@154: will fit in the new size. cannam@154: _size: The number of bytes in the new buffer. cannam@154: This must be large enough to contain the bits already written, and cannam@154: must be no larger than the existing size.*/ cannam@154: void ec_enc_shrink(ec_enc *_this,opus_uint32 _size); cannam@154: cannam@154: /*Indicates that there are no more symbols to encode. cannam@154: All reamining output bytes are flushed to the output buffer. cannam@154: ec_enc_init() must be called before the encoder can be used again.*/ cannam@154: void ec_enc_done(ec_enc *_this); cannam@154: cannam@154: #endif