1/* Copyright (C) 2002 Jean-Marc Valin */
2/**
3   @file speex_bits.h
4   @brief Handles bit packing/unpacking
5*/
6/*
7   Redistribution and use in source and binary forms, with or without
8   modification, are permitted provided that the following conditions
9   are met:
10
11   - Redistributions of source code must retain the above copyright
12   notice, this list of conditions and the following disclaimer.
13
14   - Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17
18   - Neither the name of the Xiph.org Foundation nor the names of its
19   contributors may be used to endorse or promote products derived from
20   this software without specific prior written permission.
21
22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
26   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#ifndef BITS_H
37#define BITS_H
38/** @defgroup SpeexBits SpeexBits: Bit-stream manipulations
39 *  This is the structure that holds the bit-stream when encoding or decoding
40 * with Speex. It allows some manipulations as well.
41 *  @{
42 */
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/** Bit-packing data structure representing (part of) a bit-stream. */
49typedef struct SpeexBits {
50   char *chars;   /**< "raw" data */
51   int   nbBits;  /**< Total number of bits stored in the stream*/
52   int   charPtr; /**< Position of the byte "cursor" */
53   int   bitPtr;  /**< Position of the bit "cursor" within the current char */
54   int   owner;   /**< Does the struct "own" the "raw" buffer (member "chars") */
55   int   overflow;/**< Set to one if we try to read past the valid data */
56   int   buf_size;/**< Allocated size for buffer */
57   int   reserved1; /**< Reserved for future use */
58   void *reserved2; /**< Reserved for future use */
59} SpeexBits;
60
61/** Initializes and allocates resources for a SpeexBits struct */
62void speex_bits_init(SpeexBits *bits);
63
64/** Initializes SpeexBits struct using a pre-allocated buffer*/
65void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size);
66
67/** Sets the bits in a SpeexBits struct to use data from an existing buffer (for decoding without copying data) */
68void speex_bits_set_bit_buffer(SpeexBits *bits, void *buff, int buf_size);
69
70/** Frees all resources associated to a SpeexBits struct. Right now this does nothing since no resources are allocated, but this could change in the future.*/
71void speex_bits_destroy(SpeexBits *bits);
72
73/** Resets bits to initial value (just after initialization, erasing content)*/
74void speex_bits_reset(SpeexBits *bits);
75
76/** Rewind the bit-stream to the beginning (ready for read) without erasing the content */
77void speex_bits_rewind(SpeexBits *bits);
78
79/** Initializes the bit-stream from the data in an area of memory */
80void speex_bits_read_from(SpeexBits *bits, char *bytes, int len);
81
82/** Append bytes to the bit-stream
83 *
84 * @param bits Bit-stream to operate on
85 * @param bytes pointer to the bytes what will be appended
86 * @param len Number of bytes of append
87 */
88void speex_bits_read_whole_bytes(SpeexBits *bits, char *bytes, int len);
89
90/** Write the content of a bit-stream to an area of memory
91 *
92 * @param bits Bit-stream to operate on
93 * @param bytes Memory location where to write the bits
94 * @param max_len Maximum number of bytes to write (i.e. size of the "bytes" buffer)
95 * @return Number of bytes written to the "bytes" buffer
96*/
97int speex_bits_write(SpeexBits *bits, char *bytes, int max_len);
98
99/** Like speex_bits_write, but writes only the complete bytes in the stream. Also removes the written bytes from the stream */
100int speex_bits_write_whole_bytes(SpeexBits *bits, char *bytes, int max_len);
101
102/** Append bits to the bit-stream
103 * @param bits Bit-stream to operate on
104 * @param data Value to append as integer
105 * @param nbBits number of bits to consider in "data"
106 */
107void speex_bits_pack(SpeexBits *bits, int data, int nbBits);
108
109/** Interpret the next bits in the bit-stream as a signed integer
110 *
111 * @param bits Bit-stream to operate on
112 * @param nbBits Number of bits to interpret
113 * @return A signed integer represented by the bits read
114 */
115int speex_bits_unpack_signed(SpeexBits *bits, int nbBits);
116
117/** Interpret the next bits in the bit-stream as an unsigned integer
118 *
119 * @param bits Bit-stream to operate on
120 * @param nbBits Number of bits to interpret
121 * @return An unsigned integer represented by the bits read
122 */
123unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits);
124
125/** Returns the number of bytes in the bit-stream, including the last one even if it is not "full"
126 *
127 * @param bits Bit-stream to operate on
128 * @return Number of bytes in the stream
129 */
130int speex_bits_nbytes(SpeexBits *bits);
131
132/** Same as speex_bits_unpack_unsigned, but without modifying the cursor position
133 *
134 * @param bits Bit-stream to operate on
135 * @param nbBits Number of bits to look for
136 * @return Value of the bits peeked, interpreted as unsigned
137 */
138unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits);
139
140/** Get the value of the next bit in the stream, without modifying the
141 * "cursor" position
142 *
143 * @param bits Bit-stream to operate on
144 * @return Value of the bit peeked (one bit only)
145 */
146int speex_bits_peek(SpeexBits *bits);
147
148/** Advances the position of the "bit cursor" in the stream
149 *
150 * @param bits Bit-stream to operate on
151 * @param n Number of bits to advance
152 */
153void speex_bits_advance(SpeexBits *bits, int n);
154
155/** Returns the number of bits remaining to be read in a stream
156 *
157 * @param bits Bit-stream to operate on
158 * @return Number of bits that can still be read from the stream
159 */
160int speex_bits_remaining(SpeexBits *bits);
161
162/** Insert a terminator so that the data can be sent as a packet while auto-detecting
163 * the number of frames in each packet
164 *
165 * @param bits Bit-stream to operate on
166 */
167void speex_bits_insert_terminator(SpeexBits *bits);
168
169#ifdef __cplusplus
170}
171#endif
172
173/* @} */
174#endif
175