blob: ea88d7edf1058ad53176bf7c20ad1727690d48f1 [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* Copyright (c) 2001-2008 Timothy B. Terriberry
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
Gregory Maxwellf40bbf72009-02-03 20:36:57 -05003/*
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 Valinabe043f2008-01-31 14:26:29 +110032#include "celt_types.h"
33
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110034#if !defined(_entcode_H)
35# define _entcode_H (1)
36# include <limits.h>
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080037# include <stddef.h>
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110038# include "ecintrin.h"
39
40
41
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080042typedef celt_int32 ec_int32;
43typedef celt_uint32 ec_uint32;
44typedef size_t ec_window;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110045typedef struct ec_byte_buffer ec_byte_buffer;
46
47
48
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080049/*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. Terriberry2ec8d9e2007-12-06 15:09:53 +110054
55
56
57/*Simple libogg1-style buffer.*/
58struct ec_byte_buffer{
59 unsigned char *buf;
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080060 ec_uint32 offs;
61 ec_uint32 end_offs;
Jean-Marc Valin531f2ae2010-08-02 09:01:28 -040062 ec_uint32 storage;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110063};
64
65/*Encoding functions.*/
Jean-Marc Valin531f2ae2010-08-02 09:01:28 -040066void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, ec_uint32 _size);
67void ec_byte_shrink(ec_byte_buffer *_b, ec_uint32 _size);
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080068int ec_byte_write(ec_byte_buffer *_b,unsigned _value);
Jean-Marc Valin9d785af2010-07-18 09:42:05 -040069int ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value);
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080070int ec_byte_write_done(ec_byte_buffer *_b,int _start_bits_available,
71 unsigned _end_byte,int _end_bits_used);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110072/*Decoding functions.*/
Jean-Marc Valin531f2ae2010-08-02 09:01:28 -040073void ec_byte_readinit(ec_byte_buffer *_b,unsigned char *_buf,ec_uint32 _bytes);
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080074int ec_byte_read(ec_byte_buffer *_b);
75unsigned char ec_byte_read_from_end(ec_byte_buffer *_b);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110076/*Shared functions.*/
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110077static inline void ec_byte_reset(ec_byte_buffer *_b){
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080078 _b->offs=_b->end_offs=0;
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110079}
80
Jean-Marc Valin531f2ae2010-08-02 09:01:28 -040081static inline ec_uint32 ec_byte_bytes(ec_byte_buffer *_b){
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080082 return _b->offs;
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110083}
84
85static inline unsigned char *ec_byte_get_buffer(ec_byte_buffer *_b){
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -080086 return _b->buf;
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110087}
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110088
89int ec_ilog(ec_uint32 _v);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110090
91#endif