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