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> |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 37 | # include <stddef.h> |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 38 | # include "ecintrin.h" |
| 39 | |
| 40 | |
| 41 | |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 42 | typedef celt_int32 ec_int32; |
| 43 | typedef celt_uint32 ec_uint32; |
| 44 | typedef size_t ec_window; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 45 | typedef struct ec_byte_buffer ec_byte_buffer; |
| 46 | |
| 47 | |
| 48 | |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 49 | /*This must be at least 32 bits.*/ |
| 50 | # define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT) |
| 51 | |
| 52 | /*The number of bits to use for the range-coded part of unsigned integers.*/ |
| 53 | # define EC_UINT_BITS (8) |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 54 | |
| 55 | |
| 56 | |
| 57 | /*Simple libogg1-style buffer.*/ |
| 58 | struct ec_byte_buffer{ |
| 59 | unsigned char *buf; |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 60 | ec_uint32 offs; |
| 61 | ec_uint32 end_offs; |
Jean-Marc Valin | 531f2ae | 2010-08-02 09:01:28 -0400 | [diff] [blame] | 62 | ec_uint32 storage; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /*Encoding functions.*/ |
Jean-Marc Valin | 531f2ae | 2010-08-02 09:01:28 -0400 | [diff] [blame] | 66 | void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, ec_uint32 _size); |
| 67 | void ec_byte_shrink(ec_byte_buffer *_b, ec_uint32 _size); |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 68 | int ec_byte_write(ec_byte_buffer *_b,unsigned _value); |
Jean-Marc Valin | 9d785af | 2010-07-18 09:42:05 -0400 | [diff] [blame] | 69 | int ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value); |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 70 | int ec_byte_write_done(ec_byte_buffer *_b,int _start_bits_available, |
| 71 | unsigned _end_byte,int _end_bits_used); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 72 | /*Decoding functions.*/ |
Jean-Marc Valin | 531f2ae | 2010-08-02 09:01:28 -0400 | [diff] [blame] | 73 | void ec_byte_readinit(ec_byte_buffer *_b,unsigned char *_buf,ec_uint32 _bytes); |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 74 | int ec_byte_read(ec_byte_buffer *_b); |
| 75 | unsigned char ec_byte_read_from_end(ec_byte_buffer *_b); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 76 | /*Shared functions.*/ |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 77 | static inline void ec_byte_reset(ec_byte_buffer *_b){ |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 78 | _b->offs=_b->end_offs=0; |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 79 | } |
| 80 | |
Jean-Marc Valin | 531f2ae | 2010-08-02 09:01:28 -0400 | [diff] [blame] | 81 | static inline ec_uint32 ec_byte_bytes(ec_byte_buffer *_b){ |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 82 | return _b->offs; |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static inline unsigned char *ec_byte_get_buffer(ec_byte_buffer *_b){ |
Timothy B. Terriberry | 30df6cf | 2010-12-21 08:42:26 -0800 | [diff] [blame^] | 86 | return _b->buf; |
Jean-Marc Valin | fd8fda9 | 2008-03-27 09:00:14 +1100 | [diff] [blame] | 87 | } |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 88 | |
| 89 | int ec_ilog(ec_uint32 _v); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 90 | |
| 91 | #endif |