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 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 28 | #include "opus_types.h" |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame^] | 29 | #include "opus_defines.h" |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame] | 30 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 31 | #if !defined(_entcode_H) |
| 32 | # define _entcode_H (1) |
| 33 | # include <limits.h> |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame] | 34 | # include <stddef.h> |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 35 | # include "ecintrin.h" |
| 36 | |
Timothy B. Terriberry | 9bac8c1 | 2011-03-02 16:24:32 -0800 | [diff] [blame] | 37 | /*OPT: ec_window must be at least 32 bits, but if you have fast arithmetic on a |
| 38 | larger type, you can speed up the decoder by using it here.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 39 | typedef opus_uint32 ec_window; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 40 | typedef struct ec_ctx ec_ctx; |
| 41 | typedef struct ec_ctx ec_enc; |
| 42 | typedef struct ec_ctx ec_dec; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 43 | |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame] | 44 | # define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT) |
| 45 | |
| 46 | /*The number of bits to use for the range-coded part of unsigned integers.*/ |
| 47 | # define EC_UINT_BITS (8) |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 48 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 49 | /*The resolution of fractional-precision bit usage measurements, i.e., |
| 50 | 3 => 1/8th bits.*/ |
| 51 | # define BITRES 3 |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 52 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 53 | /*The entropy encoder/decoder context. |
| 54 | We use the same structure for both, so that common functions like ec_tell() |
| 55 | can be used on either one.*/ |
| 56 | struct ec_ctx{ |
| 57 | /*Buffered input/output.*/ |
| 58 | unsigned char *buf; |
| 59 | /*The size of the buffer.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 60 | opus_uint32 storage; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 61 | /*The offset at which the last byte containing raw bits was read/written.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 62 | opus_uint32 end_offs; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 63 | /*Bits that will be read from/written at the end.*/ |
| 64 | ec_window end_window; |
| 65 | /*Number of valid bits in end_window.*/ |
| 66 | int nend_bits; |
| 67 | /*The total number of whole bits read/written. |
| 68 | This does not include partial bits currently in the range coder.*/ |
| 69 | int nbits_total; |
| 70 | /*The offset at which the next range coder byte will be read/written.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 71 | opus_uint32 offs; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 72 | /*The number of values in the current range.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 73 | opus_uint32 rng; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 74 | /*In the decoder: the difference between the top of the current range and |
| 75 | the input value, minus one. |
| 76 | In the encoder: the low end of the current range.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 77 | opus_uint32 val; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 78 | /*In the decoder: the saved normalization factor from ec_decode(). |
| 79 | In the encoder: the number of oustanding carry propagating symbols.*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 80 | opus_uint32 ext; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 81 | /*A buffered input/output symbol, awaiting carry propagation.*/ |
| 82 | int rem; |
| 83 | /*Nonzero if an error occurred.*/ |
| 84 | int error; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 85 | }; |
| 86 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame^] | 87 | static OPUS_INLINE opus_uint32 ec_range_bytes(ec_ctx *_this){ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 88 | return _this->offs; |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 89 | } |
| 90 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame^] | 91 | static OPUS_INLINE unsigned char *ec_get_buffer(ec_ctx *_this){ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 92 | return _this->buf; |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 93 | } |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 94 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame^] | 95 | static OPUS_INLINE int ec_get_error(ec_ctx *_this){ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 96 | return _this->error; |
| 97 | } |
| 98 | |
| 99 | /*Returns the number of bits "used" by the encoded or decoded symbols so far. |
| 100 | This same number can be computed in either the encoder or the decoder, and is |
| 101 | suitable for making coding decisions. |
| 102 | Return: The number of bits. |
| 103 | This will always be slightly larger than the exact value (e.g., all |
| 104 | rounding error is in the positive direction).*/ |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame^] | 105 | static OPUS_INLINE int ec_tell(ec_ctx *_this){ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 106 | return _this->nbits_total-EC_ILOG(_this->rng); |
| 107 | } |
| 108 | |
| 109 | /*Returns the number of bits "used" by the encoded or decoded symbols so far. |
| 110 | This same number can be computed in either the encoder or the decoder, and is |
| 111 | suitable for making coding decisions. |
| 112 | Return: The number of bits scaled by 2**BITRES. |
| 113 | This will always be slightly larger than the exact value (e.g., all |
| 114 | rounding error is in the positive direction).*/ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 115 | opus_uint32 ec_tell_frac(ec_ctx *_this); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 116 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 117 | #endif |