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 |
Jean-Marc Valin | cb05e7c | 2012-04-20 16:40:24 -0400 | [diff] [blame] | 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
Gregory Maxwell | f40bbf7 | 2009-02-03 20:36:57 -0500 | [diff] [blame] | 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(_entdec_H) |
| 29 | # define _entdec_H (1) |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame] | 30 | # include <limits.h> |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 31 | # include "entcode.h" |
| 32 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 33 | /*Initializes the decoder. |
| 34 | _buf: The input buffer to use. |
| 35 | Return: 0 on success, or a negative value on error.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 36 | void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 37 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 38 | /*Calculates the cumulative frequency for the next symbol. |
| 39 | This can then be fed into the probability model to determine what that |
| 40 | symbol is, and the additional frequency information required to advance to |
| 41 | the next symbol. |
| 42 | This function cannot be called more than once without a corresponding call to |
| 43 | ec_dec_update(), or decoding will not proceed correctly. |
| 44 | _ft: The total frequency of the symbols in the alphabet the next symbol was |
| 45 | encoded with. |
| 46 | Return: A cumulative frequency representing the encoded symbol. |
| 47 | If the cumulative frequency of all the symbols before the one that |
| 48 | was encoded was fl, and the cumulative frequency of all the symbols |
| 49 | up to and including the one encoded is fh, then the returned value |
| 50 | will fall in the range [fl,fh).*/ |
| 51 | unsigned ec_decode(ec_dec *_this,unsigned _ft); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 52 | |
| 53 | /*Equivalent to ec_decode() with _ft==1<<_bits.*/ |
Jean-Marc Valin | 949a29b | 2009-07-25 20:16:01 -0400 | [diff] [blame] | 54 | unsigned ec_decode_bin(ec_dec *_this,unsigned _bits); |
Jean-Marc Valin | 949a29b | 2009-07-25 20:16:01 -0400 | [diff] [blame] | 55 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 56 | /*Advance the decoder past the next symbol using the frequency information the |
| 57 | symbol was encoded with. |
| 58 | Exactly one call to ec_decode() must have been made so that all necessary |
| 59 | intermediate calculations are performed. |
| 60 | _fl: The cumulative frequency of all symbols that come before the symbol |
| 61 | decoded. |
| 62 | _fh: The cumulative frequency of all symbols up to and including the symbol |
Timothy B. Terriberry | d710177 | 2007-12-11 13:25:00 +1100 | [diff] [blame] | 63 | decoded. |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 64 | Together with _fl, this defines the range [_fl,_fh) in which the value |
| 65 | returned above must fall. |
| 66 | _ft: The total frequency of the symbols in the alphabet the symbol decoded |
| 67 | was encoded in. |
| 68 | This must be the same as passed to the preceding call to ec_decode().*/ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 69 | void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft); |
| 70 | |
| 71 | /* Decode a bit that has a 1/(1<<_logp) probability of being a one */ |
| 72 | int ec_dec_bit_logp(ec_dec *_this,unsigned _logp); |
| 73 | |
Timothy B. Terriberry | 845dfa1 | 2011-01-02 16:53:28 -0800 | [diff] [blame] | 74 | /*Decodes a symbol given an "inverse" CDF table. |
Timothy B. Terriberry | a0b664d | 2010-12-17 10:49:00 -0800 | [diff] [blame] | 75 | No call to ec_dec_update() is necessary after this call. |
Timothy B. Terriberry | 845dfa1 | 2011-01-02 16:53:28 -0800 | [diff] [blame] | 76 | _icdf: The "inverse" CDF, such that symbol s falls in the range |
| 77 | [s>0?ft-_icdf[s-1]:0,ft-_icdf[s]), where ft=1<<_ftb. |
| 78 | The values must be monotonically non-increasing, and the last value |
| 79 | must be 0. |
Timothy B. Terriberry | a0b664d | 2010-12-17 10:49:00 -0800 | [diff] [blame] | 80 | _ftb: The number of bits of precision in the cumulative distribution. |
Timothy B. Terriberry | 845dfa1 | 2011-01-02 16:53:28 -0800 | [diff] [blame] | 81 | Return: The decoded symbol s.*/ |
| 82 | int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 83 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 84 | /*Extracts a raw unsigned integer with a non-power-of-2 range from the stream. |
| 85 | The bits must have been encoded with ec_enc_uint(). |
| 86 | No call to ec_dec_update() is necessary after this call. |
| 87 | _ft: The number of integers that can be decoded (one more than the max). |
| 88 | This must be at least one, and no more than 2**32-1. |
| 89 | Return: The decoded bits.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 90 | opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft); |
Jean-Marc Valin | 8035b65 | 2010-05-27 16:23:52 -0400 | [diff] [blame] | 91 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 92 | /*Extracts a sequence of raw bits from the stream. |
| 93 | The bits must have been encoded with ec_enc_bits(). |
| 94 | No call to ec_dec_update() is necessary after this call. |
| 95 | _ftb: The number of bits to extract. |
| 96 | This must be between 0 and 25, inclusive. |
| 97 | Return: The decoded bits.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 98 | opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _ftb); |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 99 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 100 | #endif |