blob: 4369dfe8e2bad9c97efda8a20c64ccbc42f1ecde [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>
37# include "ecintrin.h"
38
39
40
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040041typedef celt_int32 ec_int32;
42typedef celt_uint32 ec_uint32;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110043typedef 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.*/
55struct ec_byte_buffer{
56 unsigned char *buf;
57 unsigned char *ptr;
Jean-Marc Valinc08be442009-06-17 23:23:46 -040058 unsigned char *end_ptr;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110059 long storage;
Jean-Marc Valin761811d2008-10-18 09:11:05 -040060 int resizable;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110061};
62
63/*Encoding functions.*/
Jean-Marc Valin8679a802008-10-18 07:44:35 -040064void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size);
Jean-Marc Valine6108642009-08-01 23:05:47 +020065void ec_byte_shrink(ec_byte_buffer *_b, long _size);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110066void ec_byte_writeinit(ec_byte_buffer *_b);
67void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes);
68void ec_byte_write1(ec_byte_buffer *_b,unsigned _value);
Jean-Marc Valinc08be442009-06-17 23:23:46 -040069void ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110070void ec_byte_write4(ec_byte_buffer *_b,ec_uint32 _value);
71void ec_byte_writecopy(ec_byte_buffer *_b,void *_source,long _bytes);
72void ec_byte_writeclear(ec_byte_buffer *_b);
73/*Decoding functions.*/
74void ec_byte_readinit(ec_byte_buffer *_b,unsigned char *_buf,long _bytes);
75int ec_byte_look1(ec_byte_buffer *_b);
Jean-Marc Valinc08be442009-06-17 23:23:46 -040076unsigned char ec_byte_look_at_end(ec_byte_buffer *_b);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110077int ec_byte_look4(ec_byte_buffer *_b,ec_uint32 *_val);
78void ec_byte_adv1(ec_byte_buffer *_b);
79void ec_byte_adv4(ec_byte_buffer *_b);
80int ec_byte_read1(ec_byte_buffer *_b);
81int ec_byte_read4(ec_byte_buffer *_b,ec_uint32 *_val);
82/*Shared functions.*/
Jean-Marc Valinfd8fda92008-03-27 09:00:14 +110083static inline void ec_byte_reset(ec_byte_buffer *_b){
84 _b->ptr=_b->buf;
85}
86
87static inline long ec_byte_bytes(ec_byte_buffer *_b){
88 return _b->ptr-_b->buf;
89}
90
91static inline unsigned char *ec_byte_get_buffer(ec_byte_buffer *_b){
92 return _b->buf;
93}
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110094
95int ec_ilog(ec_uint32 _v);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110096
97#endif