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