Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame^] | 1 | /* Copyright (c) 2001-2008 Timothy B. Terriberry |
| 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 | |
| 15 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | contributors may be used to endorse or promote products derived from |
| 17 | this software without specific prior written permission. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
Jean-Marc Valin | abe043f | 2008-01-31 14:26:29 +1100 | [diff] [blame] | 32 | #include "celt_types.h" |
| 33 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 34 | #if !defined(_entcode_H) |
| 35 | # define _entcode_H (1) |
| 36 | # include <limits.h> |
| 37 | # include "ecintrin.h" |
| 38 | |
| 39 | |
| 40 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 41 | typedef celt_int32 ec_int32; |
| 42 | typedef celt_uint32 ec_uint32; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 43 | typedef struct ec_byte_buffer ec_byte_buffer; |
| 44 | |
| 45 | |
| 46 | |
| 47 | /*The number of bits to code at a time when coding bits directly.*/ |
| 48 | # define EC_UNIT_BITS (8) |
| 49 | /*The mask for the given bits.*/ |
| 50 | # define EC_UNIT_MASK ((1U<<EC_UNIT_BITS)-1) |
| 51 | |
| 52 | |
| 53 | |
| 54 | /*Simple libogg1-style buffer.*/ |
| 55 | struct ec_byte_buffer{ |
| 56 | unsigned char *buf; |
| 57 | unsigned char *ptr; |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 58 | unsigned char *end_ptr; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 59 | long storage; |
Jean-Marc Valin | 761811d | 2008-10-18 09:11:05 -0400 | [diff] [blame] | 60 | int resizable; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /*Encoding functions.*/ |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 64 | void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size); |
Jean-Marc Valin | e610864 | 2009-08-01 23:05:47 +0200 | [diff] [blame] | 65 | void ec_byte_shrink(ec_byte_buffer *_b, long _size); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 66 | void ec_byte_writeinit(ec_byte_buffer *_b); |
| 67 | void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes); |
| 68 | void ec_byte_write1(ec_byte_buffer *_b,unsigned _value); |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 69 | void ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 70 | void ec_byte_write4(ec_byte_buffer *_b,ec_uint32 _value); |
| 71 | void ec_byte_writecopy(ec_byte_buffer *_b,void *_source,long _bytes); |
| 72 | void ec_byte_writeclear(ec_byte_buffer *_b); |
| 73 | /*Decoding functions.*/ |
| 74 | void ec_byte_readinit(ec_byte_buffer *_b,unsigned char *_buf,long _bytes); |
| 75 | int ec_byte_look1(ec_byte_buffer *_b); |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 76 | unsigned char ec_byte_look_at_end(ec_byte_buffer *_b); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 77 | int ec_byte_look4(ec_byte_buffer *_b,ec_uint32 *_val); |
| 78 | void ec_byte_adv1(ec_byte_buffer *_b); |
| 79 | void ec_byte_adv4(ec_byte_buffer *_b); |
| 80 | int ec_byte_read1(ec_byte_buffer *_b); |
| 81 | int ec_byte_read4(ec_byte_buffer *_b,ec_uint32 *_val); |
| 82 | /*Shared functions.*/ |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 83 | static inline void ec_byte_reset(ec_byte_buffer *_b){ |
| 84 | _b->ptr=_b->buf; |
| 85 | } |
| 86 | |
| 87 | static inline long ec_byte_bytes(ec_byte_buffer *_b){ |
| 88 | return _b->ptr-_b->buf; |
| 89 | } |
| 90 | |
| 91 | static inline unsigned char *ec_byte_get_buffer(ec_byte_buffer *_b){ |
| 92 | return _b->buf; |
| 93 | } |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 94 | |
| 95 | int ec_ilog(ec_uint32 _v); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 96 | |
| 97 | #endif |