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-2024 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 stream reading callbacks. */
209 void *io_opaque;
210
211 /* Opaque pointer for memory allocation calls. This is always a
212 * vorbis_t pointer from the libnogg API functions. */
213 void *mem_opaque;
214
215 /* Decoder configuration. */
216 uint32_t fast_huffman_mask;
217 int8_t fast_huffman_length;
218 bool huffman_binary_search;
219 bool divides_in_residue;
220 bool divides_in_codebook;
221 bool scan_for_next_page;
222
223 /* Operation results. */
224 bool eof;
225 STBVorbisError error;
226
227 /* Have we started decoding yet? (This flag is set when the handle
228 * is created and cleared when the first frmae is decoded.) */
229 bool first_decode;
230
231 /* Stream configuration. */
232 int16_t blocksize[2];
233 int8_t blocksize_bits[2];
234 int16_t codebook_count;
235 Codebook *codebooks;
236 int8_t floor_count;
237 uint16_t floor_types[64]; // varies
238 Floor *floor_config;
239 int8_t residue_count;
240 uint16_t residue_types[64]; // varies
241 Residue *residue_config;
242 int8_t mapping_count;
243 Mapping *mapping;
244 int8_t mode_count;
245 int8_t mode_bits; // ilog(mode_count - 1)
246 Mode mode_config[64]; // varies
247
248 /* IMDCT twiddle factors for each blocksize. */
249 TABLE_CONST float *A[2],*B[2],*C[2];
250 TABLE_CONST uint16_t *bit_reverse[2];
251 /* Window sample weights for each blocksize. */
252 TABLE_CONST float *window_weights[2];
253
254 /* Buffers for decoded data, two per channel. */
255 float **channel_buffers[2];
256 /* Channel buffer index containing this frame's data (0 or 1). */
257 int8_t cur_channel_buffer;
258 /* Per-channel pointers within channel_buffers to the decode buffer for
259 * the current frame. */
260 float **outputs;
261 /* Per-channel pointers within channel_buffers for data from the
262 * previous frame's right-side overlap window. */
263 float **previous_window;
264 /* Length of the previous window. */
265 int previous_length;
266
267 /* Temporary buffers used in floor curve computation. */
268 float **coefficients;
269 int16_t **final_Y;
270
271 /* Temporary buffer used in residue decoding. This is either "int **"
272 * if the DIVIDES_IN_RESIDUE option is set, or "uint8_t ***" if not. */
273 void *classifications;
274
275 /* Temporary buffer for inverse MDCT computation. */
276 float *imdct_temp_buf;
277
278 /* Data for the current Ogg page. */
279 uint32_t page_number;
280 uint8_t segment_count;
281 uint8_t page_flag;
282 uint8_t segments[255];
283 uint8_t segment_data[255];
284 uint8_t segment_size; // Size of current segment's data.
285 uint8_t segment_pos; // Current read position in segment data.
286 /* Index of the segment corresponding to the page's sample position, or
287 * negative if unknown or unavailable. */
288 int end_seg_with_known_loc;
289 /* Sample position for that segment's packet. */
290 uint64_t known_loc_for_packet;
291
292 /* Index of the next segment to read, or -1 if the current segment is
293 * the last one on the page. */
294 int next_seg;
295 /* Flag indicating whether we've hit the last segment in the page. */
296 bool last_seg;
297 /* Segment index of the last segment. Only valid if last_seg is true. */
298 int last_seg_index;
299
300 /* Information about the first and last pages containing audio data.
301 * Used in seeking. */
302 ProbedPage p_first, p_last;
303
304 /* Accumulator for bits read from the stream. */
305 unsigned long acc;
306 /* Number of valid bits in the accumulator, or -1 if end-of-packet
307 * has been reached. */
308 int valid_bits;
309
310 /* Sample position of next decoded sample to return. */
311 uint64_t current_loc;
312 /* True if current_loc has ever been set for this stream. */
313 bool current_loc_valid;
314 };
315
316
317 static inline UNUSED COLD_FUNCTION int error(
318 stb_vorbis *f, enum STBVorbisError e)
319 {
320 f->error = e;
321 return 0;
322 }
323
324 /*************************************************************************/
325 /*************************************************************************/
326
327 #endif // NOGG_SRC_DECODE_COMMON_H