Code Coverage Report for src/decode/common.h


Hit Total Coverage
Lines: 3 3 100.0%
Branches: 0 0 -----

1 /*
2 * libnogg: a decoder library for Ogg Vorbis streams
3 * Copyright (c) 2014-2023 Andrew Church <achurch@achurch.org>
4 *
5 * This software may be copied and redistributed under certain conditions;
6 * see the file "COPYING" in the source code distribution for details.
7 * NO WARRANTY is provided with this software.
8 */
9
10 #ifndef NOGG_SRC_DECODE_COMMON_H
11 #define NOGG_SRC_DECODE_COMMON_H
12
13 #include <stdbool.h>
14 #include <stdint.h>
15
16 /*************************************************************************/
17 /*************************************************************************/
18
19 /* Pi as a single-precision floating-point value. */
20 #define M_PIf 3.14159265f
21
22 /* Code length value indicating that a symbol has no associated code. */
23 #define NO_CODE 127
24
25 /* Maximum number of floor-1 X list entries (defined by the Vorbis spec). */
26 #define FLOOR1_X_LIST_MAX 65
27
28 /* Internal helper which evaluates to "const" when precomputed lookup
29 * tables are in use, to ensure that code does not try to overwrite
30 * constant data. */
31 #ifdef USE_LOOKUP_TABLES
32 #define TABLE_CONST const
33 #else
34 #define TABLE_CONST /*nothing*/
35 #endif
36
37 /*-----------------------------------------------------------------------*/
38
39 /*
40 * Note: Arrays marked "// varies" are actually of variable length but are
41 * small enough that it makes more sense to store them as fixed-size arrays
42 * than to incur the extra overhead of dynamic allocation.
43 */
44
45 /* Data for a codebook. */
46 typedef struct Codebook {
47 /* Codebook configuration. */
48 int32_t dimensions;
49 int32_t entries;
50 bool sparse;
51 int8_t lookup_type;
52 int8_t value_bits;
53 bool sequence_p;
54 float minimum_value;
55 float delta_value;
56 int32_t lookup_values;
57 /* List of codewords for each symbol. Only used for non-sparse
58 * codebooks; indexed by symbol value. */
59 uint32_t *codewords;
60 /* List of codeword lengths for each symbol. For non-sparse codebooks,
61 * the array is indexed by symbol value; for sparse codebooks, the
62 * order matches the order of codewords in sorted_codewords[]. */
63 int8_t *codeword_lengths;
64 /* List of multiplicands (vector components). These are pre-expanded
65 * from the 16-bit integer multiplicands read from the stream into the
66 * corresponding "minimum + delta * multiplicand" values. */
67 float *multiplicands;
68 /* Lookup table for O(1) decoding of short codewords. */
69 int16_t *fast_huffman;
70 /* Sorted lookup table for binary search of longer codewords. */
71 uint32_t *sorted_codewords;
72 /* Symbol corresponding to each codeword in sorted_codewords[]. */
73 int32_t *sorted_values;
74 /* Number of entries in the sorted tables. */
75 int32_t sorted_entries;
76 } Codebook;
77
78 /* Data for a type 0 floor curve. */
79 typedef struct Floor0 {
80 /* Floor configuration. */
81 uint8_t order;
82 uint16_t rate;
83 uint16_t bark_map_size;
84 int8_t amplitude_bits;
85 uint8_t amplitude_offset;
86 int8_t number_of_books;
87 int8_t book_bits;
88 uint8_t book_list[16]; // varies
89 /* Lookup table for the map function, for short and long windows. */
90 int16_t *map[2];
91 } Floor0;
92
93 /* Structure holding neighbor indices for Floor1.X_list. */
94 typedef struct Floor1Neighbors {
95 uint8_t low; // Index of next smallest preceding value.
96 uint8_t high; // Index of next largest preceding value.
97 } Floor1Neighbors;
98
99 /* Data for a type 1 floor curve. */
100 typedef struct Floor1 {
101 /* Floor configuration. */
102 int8_t partitions;
103 int8_t partition_class_list[31]; // varies
104 int8_t class_dimensions[16]; // varies
105 int8_t class_subclasses[16]; // varies
106 uint8_t class_masterbooks[16]; // varies
107 int16_t subclass_books[16][8]; // varies
108 int8_t floor1_multiplier;
109 int8_t rangebits;
110 int8_t values;
111 uint16_t X_list[FLOOR1_X_LIST_MAX]; // varies
112 /* Indices of X_list[] values when sorted, such that
113 * X_list[sorted_order[0]] < X_list[sorted_order[1]] < ... */
114 int8_t sorted_order[FLOOR1_X_LIST_MAX]; // varies
115 /* Low and high neighbors (as defined by the Vorbis spec) for each
116 * element of X_list. */
117 Floor1Neighbors neighbors[FLOOR1_X_LIST_MAX]; // varies
118 } Floor1;
119
120 /* Union holding data for all possible floor types. */
121 typedef union Floor {
122 Floor0 floor0;
123 Floor1 floor1;
124 } Floor;
125
126 /* Data for a residue configuration. */
127 typedef struct Residue {
128 /* Residue configuration data. */
129 int32_t begin, end;
130 int32_t part_size;
131 int8_t classifications;
132 uint8_t classbook;
133 int16_t (*residue_books)[8];
134 /* Precomputed classifications[][] array, used to avoid div/mod in the
135 * decode loop. The Vorbis spec calls this "classifications", but we
136 * already have a field by that name (from "residue_classification"),
137 * so we use "classdata" instead. */
138 uint8_t **classdata;
139 } Residue;
140
141 /* Channel coupling data for a mapping. */
142 typedef struct CouplingStep {
143 uint8_t magnitude;
144 uint8_t angle;
145 } CouplingStep;
146
147 /* Data for a mapping configuration. */
148 typedef struct Mapping {
149 /* Data for channel coupling. */
150 int16_t coupling_steps;
151 CouplingStep *coupling;
152 /* Channel multiplex settings. */
153 int8_t *mux;
154 /* Submap data. */
155 int8_t submaps;
156 uint8_t submap_floor[15]; // varies
157 uint8_t submap_residue[15]; // varies
158 } Mapping;
159
160 /* Data for an encoding mode. */
161 typedef struct Mode {
162 bool blockflag;
163 uint8_t mapping;
164 uint16_t windowtype;
165 uint16_t transformtype;
166 } Mode;
167
168 /* Position information for an Ogg page, used in seeking. */
169 typedef struct ProbedPage {
170 /* The start and end file offsets of this page. */
171 int64_t page_start, page_end;
172 /* A file offset known to be within the previous page. */
173 int64_t after_previous_page_start;
174 /* The first and last sample offsets in this page. */
175 uint64_t first_decoded_sample;
176 uint64_t last_decoded_sample;
177 } ProbedPage;
178
179 /* The top-level decoder handle structure. */
180 struct stb_vorbis {
181 /* Basic stream information. */
182 uint32_t sample_rate;
183 int32_t nominal_bitrate;
184 int32_t min_bitrate;
185 int32_t max_bitrate;
186 int channels;
187 uint64_t total_samples;
188 int64_t stream_len; // From open().
189 uint32_t bitstream_id;
190 bool bitstream_id_set; // False until bitstream_id has been set.
191
192 /* Packet mode flag. If true, the caller is expected to send packets
193 * directly to vorbis_decode_packet_direct() rather than implicitly
194 * read them via vorbis_decode_packet(). */
195 bool packet_mode;
196
197 /* Current packet's data pointer and length, used if packet_mode is
198 * true. */
199 const uint8_t *packet_data;
200 int32_t packet_len;
201
202 /* Callbacks for stream reading, used if packet_mode is false. The
203 * seek and tell callbacks are only used if stream_len >= 0. */
204 int32_t (*read_callback)(void *opaque, void *buf, int32_t len);
205 void (*seek_callback)(void *opaque, int64_t offset);
206 int64_t (*tell_callback)(void *opaque);
207
208 /* Opaque pointer for all callbacks (including memory allocation).
209 * This is always a vorbis_t pointer from the libnogg API functions. */
210 void *opaque;
211
212 /* Decoder configuration. */
213 uint32_t fast_huffman_mask;
214 int8_t fast_huffman_length;
215 bool huffman_binary_search;
216 bool divides_in_residue;
217 bool divides_in_codebook;
218 bool scan_for_next_page;
219
220 /* Operation results. */
221 bool eof;
222 STBVorbisError error;
223
224 /* Have we started decoding yet? (This flag is set when the handle
225 * is created and cleared when the first frmae is decoded.) */
226 bool first_decode;
227
228 /* Stream configuration. */
229 int16_t blocksize[2];
230 int8_t blocksize_bits[2];
231 int16_t codebook_count;
232 Codebook *codebooks;
233 int8_t floor_count;
234 uint16_t floor_types[64]; // varies
235 Floor *floor_config;
236 int8_t residue_count;
237 uint16_t residue_types[64]; // varies
238 Residue *residue_config;
239 int8_t mapping_count;
240 Mapping *mapping;
241 int8_t mode_count;
242 int8_t mode_bits; // ilog(mode_count - 1)
243 Mode mode_config[64]; // varies
244
245 /* IMDCT twiddle factors for each blocksize. */
246 TABLE_CONST float *A[2],*B[2],*C[2];
247 TABLE_CONST uint16_t *bit_reverse[2];
248 /* Window sample weights for each blocksize. */
249 TABLE_CONST float *window_weights[2];
250
251 /* Buffers for decoded data, two per channel. */
252 float **channel_buffers[2];
253 /* Channel buffer index containing this frame's data (0 or 1). */
254 int8_t cur_channel_buffer;
255 /* Per-channel pointers within channel_buffers to the decode buffer for
256 * the current frame. */
257 float **outputs;
258 /* Per-channel pointers within channel_buffers for data from the
259 * previous frame's right-side overlap window. */
260 float **previous_window;
261 /* Length of the previous window. */
262 int previous_length;
263
264 /* Temporary buffers used in floor curve computation. */
265 float **coefficients;
266 int16_t **final_Y;
267
268 /* Temporary buffer used in residue decoding. This is either "int **"
269 * if the DIVIDES_IN_RESIDUE option is set, or "uint8_t ***" if not. */
270 void *classifications;
271
272 /* Temporary buffer for inverse MDCT computation. */
273 float *imdct_temp_buf;
274
275 /* Data for the current Ogg page. */
276 uint32_t page_number;
277 uint8_t segment_count;
278 uint8_t page_flag;
279 uint8_t segments[255];
280 uint8_t segment_data[255];
281 uint8_t segment_size; // Size of current segment's data.
282 uint8_t segment_pos; // Current read position in segment data.
283 /* Index of the segment corresponding to the page's sample position, or
284 * negative if unknown or unavailable. */
285 int end_seg_with_known_loc;
286 /* Sample position for that segment's packet. */
287 uint64_t known_loc_for_packet;
288
289 /* Index of the next segment to read, or -1 if the current segment is
290 * the last one on the page. */
291 int next_seg;
292 /* Flag indicating whether we've hit the last segment in the page. */
293 bool last_seg;
294 /* Segment index of the last segment. Only valid if last_seg is true. */
295 int last_seg_index;
296
297 /* Information about the first and last pages containing audio data.
298 * Used in seeking. */
299 ProbedPage p_first, p_last;
300
301 /* Accumulator for bits read from the stream. */
302 unsigned long acc;
303 /* Number of valid bits in the accumulator, or -1 if end-of-packet
304 * has been reached. */
305 int valid_bits;
306
307 /* Sample position of next decoded sample to return. */
308 uint64_t current_loc;
309 /* True if current_loc has ever been set for this stream. */
310 bool current_loc_valid;
311 };
312
313
314 static inline UNUSED COLD_FUNCTION int error(
315 stb_vorbis *f, enum STBVorbisError e)
316 {
317 f->error = e;
318 return 0;
319 }
320
321 /*************************************************************************/
322 /*************************************************************************/
323
324 #endif // NOGG_SRC_DECODE_COMMON_H