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