1
/* Copyright (C) 2002 Jean-Marc Valin */
4
@brief Handles bit packing/unpacking
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
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.
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.
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.
43
/** Bit-packing data structure representing (part of) a bit-stream. */
44
typedef struct SpeexBits {
45
char *bytes; /**< "raw" data */
46
int nbBits; /**< Total number of bits stored in the stream*/
47
int bytePtr; /**< Position of the byte "cursor" */
48
int bitPtr; /**< Position of the bit "cursor" within the current byte */
49
int owner; /**< Does the struct "own" the "raw" buffer (member "bytes") */
50
int overflow;/**< Set to one if we try to read past the valid data */
51
int buf_size;/**< Allocated size for buffer */
52
int reserved1; /**< Reserved for future use */
53
void *reserved2; /**< Reserved for future use */
56
/** Initializes and allocates resources for a SpeexBits struct */
57
void speex_bits_init(SpeexBits *bits);
59
/** Initializes SpeexBits struct using a pre-allocated buffer*/
60
void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size);
62
/** 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.*/
63
void speex_bits_destroy(SpeexBits *bits);
65
/** Resets bits to initial value (just after initialization, erasing content)*/
66
void speex_bits_reset(SpeexBits *bits);
68
/** Rewind the bit-stream to the beginning (ready for read) without erasing the content */
69
void speex_bits_rewind(SpeexBits *bits);
71
/** Initializes the bit-stream from the data in an area of memory */
72
void speex_bits_read_from(SpeexBits *bits, char *bytes, int len);
74
/** Append bytes to the bit-stream
75
* @param bits Bit-stream to operate on
76
* @param bytes pointer to the bytes what will be appended
77
* @param len Number of bytes of append
79
void speex_bits_read_whole_bytes(SpeexBits *bits, char *bytes, int len);
81
/** Write the content of a bit-stream to an area of memory */
82
int speex_bits_write(SpeexBits *bits, char *bytes, int max_len);
84
/** Like speex_bits_write, but writes only the complete bytes in the stream. Also removes the written bytes from the stream */
85
int speex_bits_write_whole_bytes(SpeexBits *bits, char *bytes, int max_len);
87
/** Append bits to the bit-stream
88
* @param bits Bit-stream to operate on
89
* @param data Value to append as integer
90
* @param nbBits number of bits to consider in "data"
92
void speex_bits_pack(SpeexBits *bits, int data, int nbBits);
94
/** Interpret the next bits in the bit-stream as a signed integer
96
* @param bits Bit-stream to operate on
97
* @param nbBits Number of bits to interpret
98
* @return A signed integer represented by the bits read
100
int speex_bits_unpack_signed(SpeexBits *bits, int nbBits);
102
/** Interpret the next bits in the bit-stream as an unsigned integer
104
* @param bits Bit-stream to operate on
105
* @param nbBits Number of bits to interpret
106
* @return An unsigned integer represented by the bits read
108
unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits);
110
/** Returns the number of bytes in the bit-stream, including the last one even if it is not "full"
112
* @param bits Bit-stream to operate on
113
* @return Number of bytes in the stream
115
int speex_bits_nbytes(SpeexBits *bits);
117
/** Same as speex_bits_unpack_unsigned, but without modifying the cursor position */
118
unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits);
120
/** Get the value of the next bit in the stream, without modifying the
123
* @param bits Bit-stream to operate on
125
int speex_bits_peek(SpeexBits *bits);
127
/** Advances the position of the "bit cursor" in the stream
129
* @param bits Bit-stream to operate on
130
* @param n Number of bits to advance
132
void speex_bits_advance(SpeexBits *bits, int n);
134
/** Returns the number of bits remaining to be read in a stream
136
* @param bits Bit-stream to operate on
138
int speex_bits_remaining(SpeexBits *bits);
140
/** Insert a terminator so that the data can be sent as a packet while auto-detecting
141
* the number of frames in each packet
143
* @param bits Bit-stream to operate on
145
void speex_bits_insert_terminator(SpeexBits *bits);