blob: 10647eb3659a9f1836cd13fc25ebd4d64355f5d2 [file] [log] [blame]
Gregory Maxwellf40bbf72009-02-03 20:36:57 -05001/* (C) 2001-2008 Timothy B. Terriberry
2 (C) 2008 Jean-Marc Valin */
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
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110032#if !defined(_entenc_H)
33# define _entenc_H (1)
34# include <stddef.h>
35# include "entcode.h"
36
37
38
39typedef struct ec_enc ec_enc;
40
41
42
43/*The entropy encoder.*/
44struct ec_enc{
45 /*Buffered output.*/
46 ec_byte_buffer *buf;
47 /*A buffered output symbol, awaiting carry propagation.*/
48 int rem;
Timothy B. Terriberryd7101772007-12-11 13:25:00 +110049 /*Number of extra carry propagating symbols.*/
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110050 size_t ext;
51 /*The number of values in the current range.*/
52 ec_uint32 rng;
53 /*The low end of the current range (inclusive).*/
54 ec_uint32 low;
Jean-Marc Valinc08be442009-06-17 23:23:46 -040055 /*Byte that will be written at the end*/
56 unsigned char end_byte;
57 /*Number of valid bits in end_byte*/
58 int end_bits_left;
59 int nb_end_bits;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110060};
61
62
63/*Initializes the encoder.
64 _buf: The buffer to store output bytes in.
65 This must have already been initialized for writing and reset.*/
66void ec_enc_init(ec_enc *_this,ec_byte_buffer *_buf);
67/*Encodes a symbol given its frequency information.
68 The frequency information must be discernable by the decoder, assuming it
69 has read only the previous symbols from the stream.
70 It is allowable to change the frequency information, or even the entire
71 source alphabet, so long as the decoder can tell from the context of the
72 previously encoded information that it is supposed to do so as well.
Timothy B. Terriberryd7101772007-12-11 13:25:00 +110073 _fl: The cumulative frequency of all symbols that come before the one to be
74 encoded.
75 _fh: The cumulative frequency of all symbols up to and including the one to
76 be encoded.
77 Together with _fl, this defines the range [_fl,_fh) in which the
78 decoded value will fall.
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110079 _ft: The sum of the frequencies of all the symbols*/
Timothy B. Terriberryd7101772007-12-11 13:25:00 +110080void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft);
Jean-Marc Valin949a29b2009-07-25 20:16:01 -040081void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits);
82void ec_encode_raw(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110083/*Encodes a sequence of raw bits in the stream.
84 _fl: The bits to encode.
85 _ftb: The number of bits to encode.
86 This must be at least one, and no more than 32.*/
87void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb);
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +110088/*Encodes a sequence of raw bits in the stream.
89 _fl: The bits to encode.
90 _ftb: The number of bits to encode.
91 This must be at least one, and no more than 64.*/
92void ec_enc_bits64(ec_enc *_this,ec_uint64 _fl,int _ftb);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110093/*Encodes a raw unsigned integer in the stream.
94 _fl: The integer to encode.
95 _ft: The number of integers that can be encoded (one more than the max).
96 This must be at least one, and no more than 2**32-1.*/
97void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft);
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +110098/*Encodes a raw unsigned integer in the stream.
99 _fl: The integer to encode.
100 _ft: The number of integers that can be encoded (one more than the max).
101 This must be at least one, and no more than 2**64-1.*/
102void ec_enc_uint64(ec_enc *_this,ec_uint64 _fl,ec_uint64 _ft);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100103
tterribe06390d02008-01-11 03:13:50 +0000104/*Returns the number of bits "used" by the encoded symbols so far.
105 The actual number of bits may be larger, due to rounding to whole bytes, or
tterribe3eff11d2008-01-11 05:51:49 +0000106 smaller, due to trailing zeros that can be stripped, so this is not an
107 estimate of the true packet size.
108 This same number can be computed by the decoder, and is suitable for making
109 coding decisions.
tterribefad779c2008-01-11 05:12:17 +0000110 _b: The number of extra bits of precision to include.
111 At most 16 will be accurate.
112 Return: The number of bits scaled by 2**_b.
tterribe3eff11d2008-01-11 05:51:49 +0000113 This will always be slightly larger than the exact value (e.g., all
114 rounding error is in the positive direction).*/
115long ec_enc_tell(ec_enc *_this,int _b);
tterribefad779c2008-01-11 05:12:17 +0000116
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100117/*Indicates that there are no more symbols to encode.
118 All reamining output bytes are flushed to the output buffer.
119 ec_enc_init() must be called before the encoder can be used again.*/
120void ec_enc_done(ec_enc *_this);
121
122#endif