porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c
Go to the documentation of this file.
1 // Ogg Vorbis audio decoder - v1.19 - public domain
2 // http://nothings.org/stb_vorbis/
3 //
4 // Original version written by Sean Barrett in 2007.
5 //
6 // Originally sponsored by RAD Game Tools. Seeking implementation
7 // sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker,
8 // Elias Software, Aras Pranckevicius, and Sean Barrett.
9 //
10 // LICENSE
11 //
12 // See end of file for license information.
13 //
14 // Limitations:
15 //
16 // - floor 0 not supported (used in old ogg vorbis files pre-2004)
17 // - lossless sample-truncation at beginning ignored
18 // - cannot concatenate multiple vorbis streams
19 // - sample positions are 32-bit, limiting seekable 192Khz
20 // files to around 6 hours (Ogg supports 64-bit)
21 //
22 // Feature contributors:
23 // Dougall Johnson (sample-exact seeking)
24 //
25 // Bugfix/warning contributors:
26 // Terje Mathisen Niklas Frykholm Andy Hill
27 // Casey Muratori John Bolton Gargaj
28 // Laurent Gomila Marc LeBlanc Ronny Chevalier
29 // Bernhard Wodo Evan Balster github:alxprd
30 // Tom Beaumont Ingo Leitgeb Nicolas Guillemot
31 // Phillip Bennefall Rohit Thiago Goulart
32 // github:manxorist saga musix github:infatum
33 // Timur Gagiev Maxwell Koo Peter Waller
34 // github:audinowho Dougall Johnson
35 //
36 // Partial history:
37 // 1.19 - 2020-02-05 - warnings
38 // 1.18 - 2020-02-02 - fix seek bugs; parse header comments; misc warnings etc.
39 // 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure)
40 // 1.16 - 2019-03-04 - fix warnings
41 // 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found
42 // 1.14 - 2018-02-11 - delete bogus dealloca usage
43 // 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)
44 // 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
45 // 1.11 - 2017-07-23 - fix MinGW compilation
46 // 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory
47 // 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version
48 // 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame
49 // 1.07 - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const
50 // 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
51 // some crash fixes when out of memory or with corrupt files
52 // fix some inappropriately signed shifts
53 // 1.05 - 2015-04-19 - don't define __forceinline if it's redundant
54 // 1.04 - 2014-08-27 - fix missing const-correct case in API
55 // 1.03 - 2014-08-07 - warning fixes
56 // 1.02 - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows
57 // 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct)
58 // 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
59 // (API change) report sample rate for decode-full-file funcs
60 //
61 // See end of file for full version history.
62 
63 
65 //
66 // HEADER BEGINS HERE
67 //
68 
69 #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
70 #define STB_VORBIS_INCLUDE_STB_VORBIS_H
71 
72 #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
73 #define STB_VORBIS_NO_STDIO 1
74 #endif
75 
76 #ifndef STB_VORBIS_NO_STDIO
77 #include <stdio.h>
78 #endif
79 
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83 
85 
86 // Individual stb_vorbis* handles are not thread-safe; you cannot decode from
87 // them from multiple threads at the same time. However, you can have multiple
88 // stb_vorbis* handles and decode from them independently in multiple thrads.
89 
90 
92 
93 // normally stb_vorbis uses malloc() to allocate memory at startup,
94 // and alloca() to allocate temporary memory during a frame on the
95 // stack. (Memory consumption will depend on the amount of setup
96 // data in the file and how you set the compile flags for speed
97 // vs. size. In my test files the maximal-size usage is ~150KB.)
98 //
99 // You can modify the wrapper functions in the source (setup_malloc,
100 // setup_temp_malloc, temp_malloc) to change this behavior, or you
101 // can use a simpler allocation model: you pass in a buffer from
102 // which stb_vorbis will allocate _all_ its memory (including the
103 // temp memory). "open" may fail with a VORBIS_outofmem if you
104 // do not pass in enough data; there is no way to determine how
105 // much you do need except to succeed (at which point you can
106 // query get_info to find the exact amount required. yes I know
107 // this is lame).
108 //
109 // If you pass in a non-NULL buffer of the type below, allocation
110 // will occur from it as described above. Otherwise just pass NULL
111 // to use malloc()/alloca()
112 
113 typedef struct
114 {
118 
119 
121 
122 typedef struct stb_vorbis stb_vorbis;
123 
124 typedef struct
125 {
126  unsigned int sample_rate;
127  int channels;
128 
129  unsigned int setup_memory_required;
131  unsigned int temp_memory_required;
132 
135 
136 typedef struct
137 {
138  char *vendor;
139 
141  char **comment_list;
143 
144 // get general information about the file
146 
147 // get ogg comments
149 
150 // get the last error detected (clears it, too)
151 extern int stb_vorbis_get_error(stb_vorbis *f);
152 
153 // close an ogg vorbis file and free all memory in use
154 extern void stb_vorbis_close(stb_vorbis *f);
155 
156 // this function returns the offset (in samples) from the beginning of the
157 // file that will be returned by the next decode, if it is known, or -1
158 // otherwise. after a flush_pushdata() call, this may take a while before
159 // it becomes valid again.
160 // NOT WORKING YET after a seek with PULLDATA API
162 
163 // returns the current seek point within the file, or offset from the beginning
164 // of the memory buffer. In pushdata mode it returns 0.
165 extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
166 
168 
169 #ifndef STB_VORBIS_NO_PUSHDATA_API
170 
171 // this API allows you to get blocks of data from any source and hand
172 // them to stb_vorbis. you have to buffer them; stb_vorbis will tell
173 // you how much it used, and you have to give it the rest next time;
174 // and stb_vorbis may not have enough data to work with and you will
175 // need to give it the same data again PLUS more. Note that the Vorbis
176 // specification does not bound the size of an individual frame.
177 
179  const unsigned char * datablock, int datablock_length_in_bytes,
180  int *datablock_memory_consumed_in_bytes,
181  int *error,
182  const stb_vorbis_alloc *alloc_buffer);
183 // create a vorbis decoder by passing in the initial data block containing
184 // the ogg&vorbis headers (you don't need to do parse them, just provide
185 // the first N bytes of the file--you're told if it's not enough, see below)
186 // on success, returns an stb_vorbis *, does not set error, returns the amount of
187 // data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
188 // on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
189 // if returns NULL and *error is VORBIS_need_more_data, then the input block was
190 // incomplete and you need to pass in a larger block from the start of the file
191 
193  stb_vorbis *f,
194  const unsigned char *datablock, int datablock_length_in_bytes,
195  int *channels, // place to write number of float * buffers
196  float ***output, // place to write float ** array of float * buffers
197  int *samples // place to write number of output samples
198  );
199 // decode a frame of audio sample data if possible from the passed-in data block
200 //
201 // return value: number of bytes we used from datablock
202 //
203 // possible cases:
204 // 0 bytes used, 0 samples output (need more data)
205 // N bytes used, 0 samples output (resynching the stream, keep going)
206 // N bytes used, M samples output (one frame of data)
207 // note that after opening a file, you will ALWAYS get one N-bytes,0-sample
208 // frame, because Vorbis always "discards" the first frame.
209 //
210 // Note that on resynch, stb_vorbis will rarely consume all of the buffer,
211 // instead only datablock_length_in_bytes-3 or less. This is because it wants
212 // to avoid missing parts of a page header if they cross a datablock boundary,
213 // without writing state-machiney code to record a partial detection.
214 //
215 // The number of channels returned are stored in *channels (which can be
216 // NULL--it is always the same as the number of channels reported by
217 // get_info). *output will contain an array of float* buffers, one per
218 // channel. In other words, (*output)[0][0] contains the first sample from
219 // the first channel, and (*output)[1][0] contains the first sample from
220 // the second channel.
221 
223 // inform stb_vorbis that your next datablock will not be contiguous with
224 // previous ones (e.g. you've seeked in the data); future attempts to decode
225 // frames will cause stb_vorbis to resynchronize (as noted above), and
226 // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
227 // will begin decoding the _next_ frame.
228 //
229 // if you want to seek using pushdata, you need to seek in your file, then
230 // call stb_vorbis_flush_pushdata(), then start calling decoding, then once
231 // decoding is returning you data, call stb_vorbis_get_sample_offset, and
232 // if you don't like the result, seek your file again and repeat.
233 #endif
234 
235 
237 
238 #ifndef STB_VORBIS_NO_PULLDATA_API
239 // This API assumes stb_vorbis is allowed to pull data from a source--
240 // either a block of memory containing the _entire_ vorbis stream, or a
241 // FILE * that you or it create, or possibly some other reading mechanism
242 // if you go modify the source to replace the FILE * case with some kind
243 // of callback to your code. (But if you don't support seeking, you may
244 // just want to go ahead and use pushdata.)
245 
246 #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
247 extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
248 #endif
249 #if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
250 extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
251 #endif
252 // decode an entire file and output the data interleaved into a malloc()ed
253 // buffer stored in *output. The return value is the number of samples
254 // decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
255 // When you're done with it, just free() the pointer returned in *output.
256 
257 extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,
258  int *error, const stb_vorbis_alloc *alloc_buffer);
259 // create an ogg vorbis decoder from an ogg vorbis stream in memory (note
260 // this must be the entire stream!). on failure, returns NULL and sets *error
261 
262 #ifndef STB_VORBIS_NO_STDIO
263 extern stb_vorbis * stb_vorbis_open_filename(const char *filename,
264  int *error, const stb_vorbis_alloc *alloc_buffer);
265 // create an ogg vorbis decoder from a filename via fopen(). on failure,
266 // returns NULL and sets *error (possibly to VORBIS_file_open_failure).
267 
268 extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,
269  int *error, const stb_vorbis_alloc *alloc_buffer);
270 // create an ogg vorbis decoder from an open FILE *, looking for a stream at
271 // the _current_ seek point (ftell). on failure, returns NULL and sets *error.
272 // note that stb_vorbis must "own" this stream; if you seek it in between
273 // calls to stb_vorbis, it will become confused. Moreover, if you attempt to
274 // perform stb_vorbis_seek_*() operations on this file, it will assume it
275 // owns the _entire_ rest of the file after the start point. Use the next
276 // function, stb_vorbis_open_file_section(), to limit it.
277 
278 extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close,
279  int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);
280 // create an ogg vorbis decoder from an open FILE *, looking for a stream at
281 // the _current_ seek point (ftell); the stream will be of length 'len' bytes.
282 // on failure, returns NULL and sets *error. note that stb_vorbis must "own"
283 // this stream; if you seek it in between calls to stb_vorbis, it will become
284 // confused.
285 #endif
286 
287 extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
288 extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);
289 // these functions seek in the Vorbis file to (approximately) 'sample_number'.
290 // after calling seek_frame(), the next call to get_frame_*() will include
291 // the specified sample. after calling stb_vorbis_seek(), the next call to
292 // stb_vorbis_get_samples_* will start with the specified sample. If you
293 // do not need to seek to EXACTLY the target sample when using get_samples_*,
294 // you can also use seek_frame().
295 
296 extern int stb_vorbis_seek_start(stb_vorbis *f);
297 // this function is equivalent to stb_vorbis_seek(f,0)
298 
299 extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);
301 // these functions return the total length of the vorbis stream
302 
303 extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);
304 // decode the next frame and return the number of samples. the number of
305 // channels returned are stored in *channels (which can be NULL--it is always
306 // the same as the number of channels reported by get_info). *output will
307 // contain an array of float* buffers, one per channel. These outputs will
308 // be overwritten on the next call to stb_vorbis_get_frame_*.
309 //
310 // You generally should not intermix calls to stb_vorbis_get_frame_*()
311 // and stb_vorbis_get_samples_*(), since the latter calls the former.
312 
313 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
314 extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);
315 extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples);
316 #endif
317 // decode the next frame and return the number of *samples* per channel.
318 // Note that for interleaved data, you pass in the number of shorts (the
319 // size of your array), but the return value is the number of samples per
320 // channel, not the total number of samples.
321 //
322 // The data is coerced to the number of channels you request according to the
323 // channel coercion rules (see below). You must pass in the size of your
324 // buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
325 // The maximum buffer size needed can be gotten from get_info(); however,
326 // the Vorbis I specification implies an absolute maximum of 4096 samples
327 // per channel.
328 
329 // Channel coercion rules:
330 // Let M be the number of channels requested, and N the number of channels present,
331 // and Cn be the nth channel; let stereo L be the sum of all L and center channels,
332 // and stereo R be the sum of all R and center channels (channel assignment from the
333 // vorbis spec).
334 // M N output
335 // 1 k sum(Ck) for all k
336 // 2 * stereo L, stereo R
337 // k l k > l, the first l channels, then 0s
338 // k l k <= l, the first k channels
339 // Note that this is not _good_ surround etc. mixing at all! It's just so
340 // you get something useful.
341 
342 extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);
343 extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);
344 // gets num_samples samples, not necessarily on a frame boundary--this requires
345 // buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
346 // Returns the number of samples stored per channel; it may be less than requested
347 // at the end of the file. If there are no more samples in the file, returns 0.
348 
349 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
350 extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);
351 extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);
352 #endif
353 // gets num_samples samples, not necessarily on a frame boundary--this requires
354 // buffering so you have to supply the buffers. Applies the coercion rules above
355 // to produce 'channels' channels. Returns the number of samples stored per channel;
356 // it may be less than requested at the end of the file. If there are no more
357 // samples in the file, returns 0.
358 
359 #endif
360 
362 
364 {
366 
367  VORBIS_need_more_data=1, // not a real error
368 
369  VORBIS_invalid_api_mixing, // can't mix API modes
370  VORBIS_outofmem, // not enough memory
372  VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small
373  VORBIS_file_open_failure, // fopen() failed
374  VORBIS_seek_without_length, // can't seek in unknown-length file
375 
376  VORBIS_unexpected_eof=10, // file is truncated?
377  VORBIS_seek_invalid, // seek past EOF
378 
379  // decoding errors (corrupt/invalid stream) -- you probably
380  // don't care about the exact details of these
381 
382  // vorbis errors:
385 
386  // ogg errors:
396 };
397 
398 
399 #ifdef __cplusplus
400 }
401 #endif
402 
403 #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
404 //
405 // HEADER ENDS HERE
406 //
408 
409 #ifndef STB_VORBIS_HEADER_ONLY
410 
411 // global configuration settings (e.g. set these in the project/makefile),
412 // or just set them in this file at the top (although ideally the first few
413 // should be visible when the header file is compiled too, although it's not
414 // crucial)
415 
416 // STB_VORBIS_NO_PUSHDATA_API
417 // does not compile the code for the various stb_vorbis_*_pushdata()
418 // functions
419 // #define STB_VORBIS_NO_PUSHDATA_API
420 
421 // STB_VORBIS_NO_PULLDATA_API
422 // does not compile the code for the non-pushdata APIs
423 // #define STB_VORBIS_NO_PULLDATA_API
424 
425 // STB_VORBIS_NO_STDIO
426 // does not compile the code for the APIs that use FILE *s internally
427 // or externally (implied by STB_VORBIS_NO_PULLDATA_API)
428 // #define STB_VORBIS_NO_STDIO
429 
430 // STB_VORBIS_NO_INTEGER_CONVERSION
431 // does not compile the code for converting audio sample data from
432 // float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
433 // #define STB_VORBIS_NO_INTEGER_CONVERSION
434 
435 // STB_VORBIS_NO_FAST_SCALED_FLOAT
436 // does not use a fast float-to-int trick to accelerate float-to-int on
437 // most platforms which requires endianness be defined correctly.
438 //#define STB_VORBIS_NO_FAST_SCALED_FLOAT
439 
440 
441 // STB_VORBIS_MAX_CHANNELS [number]
442 // globally define this to the maximum number of channels you need.
443 // The spec does not put a restriction on channels except that
444 // the count is stored in a byte, so 255 is the hard limit.
445 // Reducing this saves about 16 bytes per value, so using 16 saves
446 // (255-16)*16 or around 4KB. Plus anything other memory usage
447 // I forgot to account for. Can probably go as low as 8 (7.1 audio),
448 // 6 (5.1 audio), or 2 (stereo only).
449 #ifndef STB_VORBIS_MAX_CHANNELS
450 #define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone?
451 #endif
452 
453 // STB_VORBIS_PUSHDATA_CRC_COUNT [number]
454 // after a flush_pushdata(), stb_vorbis begins scanning for the
455 // next valid page, without backtracking. when it finds something
456 // that looks like a page, it streams through it and verifies its
457 // CRC32. Should that validation fail, it keeps scanning. But it's
458 // possible that _while_ streaming through to check the CRC32 of
459 // one candidate page, it sees another candidate page. This #define
460 // determines how many "overlapping" candidate pages it can search
461 // at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
462 // garbage pages could be as big as 64KB, but probably average ~16KB.
463 // So don't hose ourselves by scanning an apparent 64KB page and
464 // missing a ton of real ones in the interim; so minimum of 2
465 #ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
466 #define STB_VORBIS_PUSHDATA_CRC_COUNT 4
467 #endif
468 
469 // STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
470 // sets the log size of the huffman-acceleration table. Maximum
471 // supported value is 24. with larger numbers, more decodings are O(1),
472 // but the table size is larger so worse cache missing, so you'll have
473 // to probe (and try multiple ogg vorbis files) to find the sweet spot.
474 #ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
475 #define STB_VORBIS_FAST_HUFFMAN_LENGTH 10
476 #endif
477 
478 // STB_VORBIS_FAST_BINARY_LENGTH [number]
479 // sets the log size of the binary-search acceleration table. this
480 // is used in similar fashion to the fast-huffman size to set initial
481 // parameters for the binary search
482 
483 // STB_VORBIS_FAST_HUFFMAN_INT
484 // The fast huffman tables are much more efficient if they can be
485 // stored as 16-bit results instead of 32-bit results. This restricts
486 // the codebooks to having only 65535 possible outcomes, though.
487 // (At least, accelerated by the huffman table.)
488 #ifndef STB_VORBIS_FAST_HUFFMAN_INT
489 #define STB_VORBIS_FAST_HUFFMAN_SHORT
490 #endif
491 
492 // STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
493 // If the 'fast huffman' search doesn't succeed, then stb_vorbis falls
494 // back on binary searching for the correct one. This requires storing
495 // extra tables with the huffman codes in sorted order. Defining this
496 // symbol trades off space for speed by forcing a linear search in the
497 // non-fast case, except for "sparse" codebooks.
498 // #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
499 
500 // STB_VORBIS_DIVIDES_IN_RESIDUE
501 // stb_vorbis precomputes the result of the scalar residue decoding
502 // that would otherwise require a divide per chunk. you can trade off
503 // space for time by defining this symbol.
504 // #define STB_VORBIS_DIVIDES_IN_RESIDUE
505 
506 // STB_VORBIS_DIVIDES_IN_CODEBOOK
507 // vorbis VQ codebooks can be encoded two ways: with every case explicitly
508 // stored, or with all elements being chosen from a small range of values,
509 // and all values possible in all elements. By default, stb_vorbis expands
510 // this latter kind out to look like the former kind for ease of decoding,
511 // because otherwise an integer divide-per-vector-element is required to
512 // unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can
513 // trade off storage for speed.
514 //#define STB_VORBIS_DIVIDES_IN_CODEBOOK
515 
516 #ifdef STB_VORBIS_CODEBOOK_SHORTS
517 #error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
518 #endif
519 
520 // STB_VORBIS_DIVIDE_TABLE
521 // this replaces small integer divides in the floor decode loop with
522 // table lookups. made less than 1% difference, so disabled by default.
523 
524 // STB_VORBIS_NO_INLINE_DECODE
525 // disables the inlining of the scalar codebook fast-huffman decode.
526 // might save a little codespace; useful for debugging
527 // #define STB_VORBIS_NO_INLINE_DECODE
528 
529 // STB_VORBIS_NO_DEFER_FLOOR
530 // Normally we only decode the floor without synthesizing the actual
531 // full curve. We can instead synthesize the curve immediately. This
532 // requires more memory and is very likely slower, so I don't think
533 // you'd ever want to do it except for debugging.
534 // #define STB_VORBIS_NO_DEFER_FLOOR
535 
536 
537 
538 
540 
541 #ifdef STB_VORBIS_NO_PULLDATA_API
542  #define STB_VORBIS_NO_INTEGER_CONVERSION
543  #define STB_VORBIS_NO_STDIO
544 #endif
545 
546 #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
547  #define STB_VORBIS_NO_STDIO 1
548 #endif
549 
550 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
551 #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
552 
553  // only need endianness for fast-float-to-int, which we don't
554  // use for pushdata
555 
556  #ifndef STB_VORBIS_BIG_ENDIAN
557  #define STB_VORBIS_ENDIAN 0
558  #else
559  #define STB_VORBIS_ENDIAN 1
560  #endif
561 
562 #endif
563 #endif
564 
565 
566 #ifndef STB_VORBIS_NO_STDIO
567 #include <stdio.h>
568 #endif
569 
570 #ifndef STB_VORBIS_NO_CRT
571  #include <stdlib.h>
572  #include <string.h>
573  #include <assert.h>
574  #include <math.h>
575 
576  // find definition of alloca if it's not in stdlib.h:
577  #if defined(_MSC_VER) || defined(__MINGW32__)
578  #include <malloc.h>
579  #endif
580  #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)
581  #include <alloca.h>
582  #endif
583 #else // STB_VORBIS_NO_CRT
584  #define NULL 0
585  #define malloc(s) 0
586  #define free(s) ((void) 0)
587  #define realloc(s) 0
588 #endif // STB_VORBIS_NO_CRT
589 
590 #include <limits.h>
591 
592 #ifdef __MINGW32__
593  // eff you mingw:
594  // "fixed":
595  // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/
596  // "no that broke the build, reverted, who cares about C":
597  // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/
598  #ifdef __forceinline
599  #undef __forceinline
600  #endif
601  #define __forceinline
602  #ifndef alloca
603  #define alloca __builtin_alloca
604  #endif
605 #elif !defined(_MSC_VER)
606  #if __GNUC__
607  #define __forceinline inline
608  #else
609  #define __forceinline
610  #endif
611 #endif
612 
613 #if STB_VORBIS_MAX_CHANNELS > 256
614 #error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
615 #endif
616 
617 #if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
618 #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
619 #endif
620 
621 
622 #if 0
623 #include <crtdbg.h>
624 #define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])
625 #else
626 #define CHECK(f) ((void) 0)
627 #endif
628 
629 #define MAX_BLOCKSIZE_LOG 13 // from specification
630 #define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)
631 
632 
633 typedef unsigned char uint8;
634 typedef signed char int8;
635 typedef unsigned short uint16;
636 typedef signed short int16;
637 typedef unsigned int uint32;
638 typedef signed int int32;
639 
640 #ifndef TRUE
641 #define TRUE 1
642 #define FALSE 0
643 #endif
644 
645 typedef float codetype;
646 
647 // @NOTE
648 //
649 // Some arrays below are tagged "//varies", which means it's actually
650 // a variable-sized piece of data, but rather than malloc I assume it's
651 // small enough it's better to just allocate it all together with the
652 // main thing
653 //
654 // Most of the variables are specified with the smallest size I could pack
655 // them into. It might give better performance to make them all full-sized
656 // integers. It should be safe to freely rearrange the structures or change
657 // the sizes larger--nothing relies on silently truncating etc., nor the
658 // order of variables.
659 
660 #define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
661 #define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1)
662 
663 typedef struct
664 {
665  int dimensions, entries;
668  float delta_value;
676  #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
678  #else
679  int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
680  #endif
684 } Codebook;
685 
686 typedef struct
687 {
694  uint8 book_list[16]; // varies
695 } Floor0;
696 
697 typedef struct
698 {
700  uint8 partition_class_list[32]; // varies
701  uint8 class_dimensions[16]; // varies
702  uint8 class_subclasses[16]; // varies
703  uint8 class_masterbooks[16]; // varies
704  int16 subclass_books[16][8]; // varies
705  uint16 Xlist[31*8+2]; // varies
706  uint8 sorted_order[31*8+2];
707  uint8 neighbors[31*8+2][2];
710  int values;
711 } Floor1;
712 
713 typedef union
714 {
717 } Floor;
718 
719 typedef struct
720 {
721  uint32 begin, end;
726  int16 (*residue_books)[8];
727 } Residue;
728 
729 typedef struct
730 {
735 
736 typedef struct
737 {
741  uint8 submap_floor[15]; // varies
742  uint8 submap_residue[15]; // varies
743 } Mapping;
744 
745 typedef struct
746 {
751 } Mode;
752 
753 typedef struct
754 {
755  uint32 goal_crc; // expected crc if match
756  int bytes_left; // bytes left in packet
757  uint32 crc_so_far; // running crc
758  int bytes_done; // bytes processed in _current_ chunk
759  uint32 sample_loc; // granule pos encoded in page
760 } CRCscan;
761 
762 typedef struct
763 {
764  uint32 page_start, page_end;
766 } ProbedPage;
767 
769 {
770  // user-accessible info
771  unsigned int sample_rate;
772  int channels;
773 
774  unsigned int setup_memory_required;
775  unsigned int temp_memory_required;
777 
778  char *vendor;
780  char **comment_list;
781 
782  // input config
783 #ifndef STB_VORBIS_NO_STDIO
784  FILE *f;
787 #endif
788 
792 
794 
796 
797  // the page to seek to when seeking to start, may be zero
799 
800  // p_first is the page on which the first audio packet ends
801  // (but not necessarily the page on which it starts)
803 
804  // memory management
808 
809  // run-time results
810  int eof;
812 
813  // user-useful data
814 
815  // header info
816  int blocksize[2];
821  uint16 floor_types[64]; // varies
824  uint16 residue_types[64]; // varies
829  Mode mode_config[64]; // varies
830 
832 
833  // decode buffer
836 
839 
840  #ifndef STB_VORBIS_NO_DEFER_FLOOR
842  #else
843  float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
844  #endif
845 
846  uint32 current_loc; // sample location of next frame to decode
848 
849  // per-blocksize precomputed data
850 
851  // twiddle factors
852  float *A[2],*B[2],*C[2];
853  float *window[2];
855 
856  // current page/packet/segment streaming info
857  uint32 serial; // stream serial number for verification
864  int next_seg;
865  int last_seg; // flag that we're on the last segment
866  int last_seg_which; // what was the segment number of the last seg?
874 
875  // push mode scanning
876  int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching
877 #ifndef STB_VORBIS_NO_PUSHDATA_API
879 #endif
880 
881  // sample-access
884 };
885 
886 #if defined(STB_VORBIS_NO_PUSHDATA_API)
887  #define IS_PUSH_MODE(f) FALSE
888 #elif defined(STB_VORBIS_NO_PULLDATA_API)
889  #define IS_PUSH_MODE(f) TRUE
890 #else
891  #define IS_PUSH_MODE(f) ((f)->push_mode)
892 #endif
893 
894 typedef struct stb_vorbis vorb;
895 
896 static int error(vorb *f, enum STBVorbisError e)
897 {
898  f->error = e;
899  if (!f->eof && e != VORBIS_need_more_data) {
900  f->error=e; // breakpoint for debugging
901  }
902  return 0;
903 }
904 
905 
906 // these functions are used for allocating temporary memory
907 // while decoding. if you can afford the stack space, use
908 // alloca(); otherwise, provide a temp buffer and it will
909 // allocate out of those.
910 
911 #define array_size_required(count,size) (count*(sizeof(void *)+(size)))
912 
913 #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
914 #define temp_free(f,p) (void)0
915 #define temp_alloc_save(f) ((f)->temp_offset)
916 #define temp_alloc_restore(f,p) ((f)->temp_offset = (p))
917 
918 #define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)
919 
920 // given a sufficiently large block of memory, make an array of pointers to subblocks of it
921 static void *make_block_array(void *mem, int count, int size)
922 {
923  int i;
924  void ** p = (void **) mem;
925  char *q = (char *) (p + count);
926  for (i=0; i < count; ++i) {
927  p[i] = q;
928  q += size;
929  }
930  return p;
931 }
932 
933 static void *setup_malloc(vorb *f, int sz)
934 {
935  sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
936  f->setup_memory_required += sz;
937  if (f->alloc.alloc_buffer) {
938  void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
939  if (f->setup_offset + sz > f->temp_offset) return NULL;
940  f->setup_offset += sz;
941  return p;
942  }
943  return sz ? malloc(sz) : NULL;
944 }
945 
946 static void setup_free(vorb *f, void *p)
947 {
948  if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack
949  free(p);
950 }
951 
952 static void *setup_temp_malloc(vorb *f, int sz)
953 {
954  sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.
955  if (f->alloc.alloc_buffer) {
956  if (f->temp_offset - sz < f->setup_offset) return NULL;
957  f->temp_offset -= sz;
958  return (char *) f->alloc.alloc_buffer + f->temp_offset;
959  }
960  return malloc(sz);
961 }
962 
963 static void setup_temp_free(vorb *f, void *p, int sz)
964 {
965  if (f->alloc.alloc_buffer) {
966  f->temp_offset += (sz+3)&~3;
967  return;
968  }
969  free(p);
970 }
971 
972 #define CRC32_POLY 0x04c11db7 // from spec
973 
974 static uint32 crc_table[256];
975 static void crc32_init(void)
976 {
977  int i,j;
978  uint32 s;
979  for(i=0; i < 256; i++) {
980  for (s=(uint32) i << 24, j=0; j < 8; ++j)
981  s = (s << 1) ^ (s >= (1U<<31) ? CRC32_POLY : 0);
982  crc_table[i] = s;
983  }
984 }
985 
987 {
988  return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];
989 }
990 
991 
992 // used in setup, and for huffman that doesn't go fast path
993 static unsigned int bit_reverse(unsigned int n)
994 {
995  n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
996  n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
997  n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
998  n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
999  return (n >> 16) | (n << 16);
1000 }
1001 
1002 static float square(float x)
1003 {
1004  return x*x;
1005 }
1006 
1007 // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
1008 // as required by the specification. fast(?) implementation from stb.h
1009 // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
1010 static int ilog(int32 n)
1011 {
1012  static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
1013 
1014  if (n < 0) return 0; // signed n returns 0
1015 
1016  // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
1017  if (n < (1 << 14))
1018  if (n < (1 << 4)) return 0 + log2_4[n ];
1019  else if (n < (1 << 9)) return 5 + log2_4[n >> 5];
1020  else return 10 + log2_4[n >> 10];
1021  else if (n < (1 << 24))
1022  if (n < (1 << 19)) return 15 + log2_4[n >> 15];
1023  else return 20 + log2_4[n >> 20];
1024  else if (n < (1 << 29)) return 25 + log2_4[n >> 25];
1025  else return 30 + log2_4[n >> 30];
1026 }
1027 
1028 #ifndef M_PI
1029  #define M_PI 3.14159265358979323846264f // from CRC
1030 #endif
1031 
1032 // code length assigned to a value with no huffman encoding
1033 #define NO_CODE 255
1034 
1036 //
1037 // these functions are only called at setup, and only a few times
1038 // per file
1039 
1040 static float float32_unpack(uint32 x)
1041 {
1042  // from the specification
1043  uint32 mantissa = x & 0x1fffff;
1044  uint32 sign = x & 0x80000000;
1045  uint32 exp = (x & 0x7fe00000) >> 21;
1046  double res = sign ? -(double)mantissa : (double)mantissa;
1047  return (float) ldexp((float)res, exp-788);
1048 }
1049 
1050 
1051 // zlib & jpeg huffman tables assume that the output symbols
1052 // can either be arbitrarily arranged, or have monotonically
1053 // increasing frequencies--they rely on the lengths being sorted;
1054 // this makes for a very simple generation algorithm.
1055 // vorbis allows a huffman table with non-sorted lengths. This
1056 // requires a more sophisticated construction, since symbols in
1057 // order do not map to huffman codes "in order".
1058 static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values)
1059 {
1060  if (!c->sparse) {
1061  c->codewords [symbol] = huff_code;
1062  } else {
1063  c->codewords [count] = huff_code;
1064  c->codeword_lengths[count] = len;
1065  values [count] = symbol;
1066  }
1067 }
1068 
1069 static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
1070 {
1071  int i,k,m=0;
1072  uint32 available[32];
1073 
1074  memset(available, 0, sizeof(available));
1075  // find the first entry
1076  for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;
1077  if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
1078  // add to the list
1079  add_entry(c, 0, k, m++, len[k], values);
1080  // add all available leaves
1081  for (i=1; i <= len[k]; ++i)
1082  available[i] = 1U << (32-i);
1083  // note that the above code treats the first case specially,
1084  // but it's really the same as the following code, so they
1085  // could probably be combined (except the initial code is 0,
1086  // and I use 0 in available[] to mean 'empty')
1087  for (i=k+1; i < n; ++i) {
1088  uint32 res;
1089  int z = len[i], y;
1090  if (z == NO_CODE) continue;
1091  // find lowest available leaf (should always be earliest,
1092  // which is what the specification calls for)
1093  // note that this property, and the fact we can never have
1094  // more than one free leaf at a given level, isn't totally
1095  // trivial to prove, but it seems true and the assert never
1096  // fires, so!
1097  while (z > 0 && !available[z]) --z;
1098  if (z == 0) { return FALSE; }
1099  res = available[z];
1100  assert(z >= 0 && z < 32);
1101  available[z] = 0;
1102  add_entry(c, bit_reverse(res), i, m++, len[i], values);
1103  // propagate availability up the tree
1104  if (z != len[i]) {
1105  assert(len[i] >= 0 && len[i] < 32);
1106  for (y=len[i]; y > z; --y) {
1107  assert(available[y] == 0);
1108  available[y] = res + (1 << (32-y));
1109  }
1110  }
1111  }
1112  return TRUE;
1113 }
1114 
1115 // accelerated huffman table allows fast O(1) match of all symbols
1116 // of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
1118 {
1119  int i, len;
1120  for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)
1121  c->fast_huffman[i] = -1;
1122 
1123  len = c->sparse ? c->sorted_entries : c->entries;
1125  if (len > 32767) len = 32767; // largest possible value we can encode!
1126  #endif
1127  for (i=0; i < len; ++i) {
1129  uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
1130  // set table entries for all bit combinations in the higher bits
1131  while (z < FAST_HUFFMAN_TABLE_SIZE) {
1132  c->fast_huffman[z] = i;
1133  z += 1 << c->codeword_lengths[i];
1134  }
1135  }
1136  }
1137 }
1138 
1139 #ifdef _MSC_VER
1140 #define STBV_CDECL __cdecl
1141 #else
1142 #define STBV_CDECL
1143 #endif
1144 
1145 static int STBV_CDECL uint32_compare(const void *p, const void *q)
1146 {
1147  uint32 x = * (uint32 *) p;
1148  uint32 y = * (uint32 *) q;
1149  return x < y ? -1 : x > y;
1150 }
1151 
1152 static int include_in_sort(Codebook *c, uint8 len)
1153 {
1154  if (c->sparse) { assert(len != NO_CODE); return TRUE; }
1155  if (len == NO_CODE) return FALSE;
1156  if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;
1157  return FALSE;
1158 }
1159 
1160 // if the fast table above doesn't work, we want to binary
1161 // search them... need to reverse the bits
1162 static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
1163 {
1164  int i, len;
1165  // build a list of all the entries
1166  // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
1167  // this is kind of a frivolous optimization--I don't see any performance improvement,
1168  // but it's like 4 extra lines of code, so.
1169  if (!c->sparse) {
1170  int k = 0;
1171  for (i=0; i < c->entries; ++i)
1172  if (include_in_sort(c, lengths[i]))
1173  c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
1174  assert(k == c->sorted_entries);
1175  } else {
1176  for (i=0; i < c->sorted_entries; ++i)
1177  c->sorted_codewords[i] = bit_reverse(c->codewords[i]);
1178  }
1179 
1180  qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);
1181  c->sorted_codewords[c->sorted_entries] = 0xffffffff;
1182 
1183  len = c->sparse ? c->sorted_entries : c->entries;
1184  // now we need to indicate how they correspond; we could either
1185  // #1: sort a different data structure that says who they correspond to
1186  // #2: for each sorted entry, search the original list to find who corresponds
1187  // #3: for each original entry, find the sorted entry
1188  // #1 requires extra storage, #2 is slow, #3 can use binary search!
1189  for (i=0; i < len; ++i) {
1190  int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
1191  if (include_in_sort(c,huff_len)) {
1192  uint32 code = bit_reverse(c->codewords[i]);
1193  int x=0, n=c->sorted_entries;
1194  while (n > 1) {
1195  // invariant: sc[x] <= code < sc[x+n]
1196  int m = x + (n >> 1);
1197  if (c->sorted_codewords[m] <= code) {
1198  x = m;
1199  n -= (n>>1);
1200  } else {
1201  n >>= 1;
1202  }
1203  }
1204  assert(c->sorted_codewords[x] == code);
1205  if (c->sparse) {
1206  c->sorted_values[x] = values[i];
1207  c->codeword_lengths[x] = huff_len;
1208  } else {
1209  c->sorted_values[x] = i;
1210  }
1211  }
1212  }
1213 }
1214 
1215 // only run while parsing the header (3 times)
1216 static int vorbis_validate(uint8 *data)
1217 {
1218  static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };
1219  return memcmp(data, vorbis, 6) == 0;
1220 }
1221 
1222 // called from setup only, once per code book
1223 // (formula implied by specification)
1224 static int lookup1_values(int entries, int dim)
1225 {
1226  int r = (int) floor(exp((float) log((float) entries) / dim));
1227  if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;
1228  ++r; // floor() to avoid _ftol() when non-CRT
1229  if (pow((float) r+1, dim) <= entries)
1230  return -1;
1231  if ((int) floor(pow((float) r, dim)) > entries)
1232  return -1;
1233  return r;
1234 }
1235 
1236 // called twice per file
1237 static void compute_twiddle_factors(int n, float *A, float *B, float *C)
1238 {
1239  int n4 = n >> 2, n8 = n >> 3;
1240  int k,k2;
1241 
1242  for (k=k2=0; k < n4; ++k,k2+=2) {
1243  A[k2 ] = (float) cos(4*k*M_PI/n);
1244  A[k2+1] = (float) -sin(4*k*M_PI/n);
1245  B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f;
1246  B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f;
1247  }
1248  for (k=k2=0; k < n8; ++k,k2+=2) {
1249  C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);
1250  C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
1251  }
1252 }
1253 
1254 static void compute_window(int n, float *window)
1255 {
1256  int n2 = n >> 1, i;
1257  for (i=0; i < n2; ++i)
1258  window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
1259 }
1260 
1261 static void compute_bitreverse(int n, uint16 *rev)
1262 {
1263  int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
1264  int i, n8 = n >> 3;
1265  for (i=0; i < n8; ++i)
1266  rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2;
1267 }
1268 
1269 static int init_blocksize(vorb *f, int b, int n)
1270 {
1271  int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
1272  f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);
1273  f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);
1274  f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);
1275  if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);
1276  compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
1277  f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);
1278  if (!f->window[b]) return error(f, VORBIS_outofmem);
1279  compute_window(n, f->window[b]);
1280  f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);
1281  if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);
1282  compute_bitreverse(n, f->bit_reverse[b]);
1283  return TRUE;
1284 }
1285 
1286 static void neighbors(uint16 *x, int n, int *plow, int *phigh)
1287 {
1288  int low = -1;
1289  int high = 65536;
1290  int i;
1291  for (i=0; i < n; ++i) {
1292  if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; }
1293  if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
1294  }
1295 }
1296 
1297 // this has been repurposed so y is now the original index instead of y
1298 typedef struct
1299 {
1300  uint16 x,id;
1302 
1303 static int STBV_CDECL point_compare(const void *p, const void *q)
1304 {
1307  return a->x < b->x ? -1 : a->x > b->x;
1308 }
1309 
1310 //
1312 
1313 
1314 #if defined(STB_VORBIS_NO_STDIO)
1315  #define USE_MEMORY(z) TRUE
1316 #else
1317  #define USE_MEMORY(z) ((z)->stream)
1318 #endif
1319 
1320 static uint8 get8(vorb *z)
1321 {
1322  if (USE_MEMORY(z)) {
1323  if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
1324  return *z->stream++;
1325  }
1326 
1327  #ifndef STB_VORBIS_NO_STDIO
1328  {
1329  int c = fgetc(z->f);
1330  if (c == EOF) { z->eof = TRUE; return 0; }
1331  return c;
1332  }
1333  #endif
1334 }
1335 
1336 static uint32 get32(vorb *f)
1337 {
1338  uint32 x;
1339  x = get8(f);
1340  x += get8(f) << 8;
1341  x += get8(f) << 16;
1342  x += (uint32) get8(f) << 24;
1343  return x;
1344 }
1345 
1346 static int getn(vorb *z, uint8 *data, int n)
1347 {
1348  if (USE_MEMORY(z)) {
1349  if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }
1350  memcpy(data, z->stream, n);
1351  z->stream += n;
1352  return 1;
1353  }
1354 
1355  #ifndef STB_VORBIS_NO_STDIO
1356  if (fread(data, n, 1, z->f) == 1)
1357  return 1;
1358  else {
1359  z->eof = 1;
1360  return 0;
1361  }
1362  #endif
1363 }
1364 
1365 static void skip(vorb *z, int n)
1366 {
1367  if (USE_MEMORY(z)) {
1368  z->stream += n;
1369  if (z->stream >= z->stream_end) z->eof = 1;
1370  return;
1371  }
1372  #ifndef STB_VORBIS_NO_STDIO
1373  {
1374  long x = ftell(z->f);
1375  fseek(z->f, x+n, SEEK_SET);
1376  }
1377  #endif
1378 }
1379 
1380 static int set_file_offset(stb_vorbis *f, unsigned int loc)
1381 {
1382  #ifndef STB_VORBIS_NO_PUSHDATA_API
1383  if (f->push_mode) return 0;
1384  #endif
1385  f->eof = 0;
1386  if (USE_MEMORY(f)) {
1387  if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
1388  f->stream = f->stream_end;
1389  f->eof = 1;
1390  return 0;
1391  } else {
1392  f->stream = f->stream_start + loc;
1393  return 1;
1394  }
1395  }
1396  #ifndef STB_VORBIS_NO_STDIO
1397  if (loc + f->f_start < loc || loc >= 0x80000000) {
1398  loc = 0x7fffffff;
1399  f->eof = 1;
1400  } else {
1401  loc += f->f_start;
1402  }
1403  if (!fseek(f->f, loc, SEEK_SET))
1404  return 1;
1405  f->eof = 1;
1406  fseek(f->f, f->f_start, SEEK_END);
1407  return 0;
1408  #endif
1409 }
1410 
1411 
1412 static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };
1413 
1414 static int capture_pattern(vorb *f)
1415 {
1416  if (0x4f != get8(f)) return FALSE;
1417  if (0x67 != get8(f)) return FALSE;
1418  if (0x67 != get8(f)) return FALSE;
1419  if (0x53 != get8(f)) return FALSE;
1420  return TRUE;
1421 }
1422 
1423 #define PAGEFLAG_continued_packet 1
1424 #define PAGEFLAG_first_page 2
1425 #define PAGEFLAG_last_page 4
1426 
1428 {
1429  uint32 loc0,loc1,n;
1430  if (f->first_decode && !IS_PUSH_MODE(f)) {
1431  f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4;
1432  }
1433  // stream structure version
1435  // header flag
1436  f->page_flag = get8(f);
1437  // absolute granule position
1438  loc0 = get32(f);
1439  loc1 = get32(f);
1440  // @TODO: validate loc0,loc1 as valid positions?
1441  // stream serial number -- vorbis doesn't interleave, so discard
1442  get32(f);
1443  //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);
1444  // page sequence number
1445  n = get32(f);
1446  f->last_page = n;
1447  // CRC32
1448  get32(f);
1449  // page_segments
1450  f->segment_count = get8(f);
1451  if (!getn(f, f->segments, f->segment_count))
1452  return error(f, VORBIS_unexpected_eof);
1453  // assume we _don't_ know any the sample position of any segments
1454  f->end_seg_with_known_loc = -2;
1455  if (loc0 != ~0U || loc1 != ~0U) {
1456  int i;
1457  // determine which packet is the last one that will complete
1458  for (i=f->segment_count-1; i >= 0; --i)
1459  if (f->segments[i] < 255)
1460  break;
1461  // 'i' is now the index of the _last_ segment of a packet that ends
1462  if (i >= 0) {
1463  f->end_seg_with_known_loc = i;
1464  f->known_loc_for_packet = loc0;
1465  }
1466  }
1467  if (f->first_decode) {
1468  int i,len;
1469  len = 0;
1470  for (i=0; i < f->segment_count; ++i)
1471  len += f->segments[i];
1472  len += 27 + f->segment_count;
1473  f->p_first.page_end = f->p_first.page_start + len;
1474  f->p_first.last_decoded_sample = loc0;
1475  }
1476  f->next_seg = 0;
1477  return TRUE;
1478 }
1479 
1480 static int start_page(vorb *f)
1481 {
1484 }
1485 
1486 static int start_packet(vorb *f)
1487 {
1488  while (f->next_seg == -1) {
1489  if (!start_page(f)) return FALSE;
1490  if (f->page_flag & PAGEFLAG_continued_packet)
1492  }
1493  f->last_seg = FALSE;
1494  f->valid_bits = 0;
1495  f->packet_bytes = 0;
1496  f->bytes_in_seg = 0;
1497  // f->next_seg is now valid
1498  return TRUE;
1499 }
1500 
1502 {
1503  if (f->next_seg == -1) {
1504  int x = get8(f);
1505  if (f->eof) return FALSE; // EOF at page boundary is not an error!
1506  if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern);
1507  if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
1508  if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
1509  if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
1510  if (!start_page_no_capturepattern(f)) return FALSE;
1511  if (f->page_flag & PAGEFLAG_continued_packet) {
1512  // set up enough state that we can read this packet if we want,
1513  // e.g. during recovery
1514  f->last_seg = FALSE;
1515  f->bytes_in_seg = 0;
1517  }
1518  }
1519  return start_packet(f);
1520 }
1521 
1522 static int next_segment(vorb *f)
1523 {
1524  int len;
1525  if (f->last_seg) return 0;
1526  if (f->next_seg == -1) {
1527  f->last_seg_which = f->segment_count-1; // in case start_page fails
1528  if (!start_page(f)) { f->last_seg = 1; return 0; }
1530  }
1531  len = f->segments[f->next_seg++];
1532  if (len < 255) {
1533  f->last_seg = TRUE;
1534  f->last_seg_which = f->next_seg-1;
1535  }
1536  if (f->next_seg >= f->segment_count)
1537  f->next_seg = -1;
1538  assert(f->bytes_in_seg == 0);
1539  f->bytes_in_seg = len;
1540  return len;
1541 }
1542 
1543 #define EOP (-1)
1544 #define INVALID_BITS (-1)
1545 
1546 static int get8_packet_raw(vorb *f)
1547 {
1548  if (!f->bytes_in_seg) { // CLANG!
1549  if (f->last_seg) return EOP;
1550  else if (!next_segment(f)) return EOP;
1551  }
1552  assert(f->bytes_in_seg > 0);
1553  --f->bytes_in_seg;
1554  ++f->packet_bytes;
1555  return get8(f);
1556 }
1557 
1558 static int get8_packet(vorb *f)
1559 {
1560  int x = get8_packet_raw(f);
1561  f->valid_bits = 0;
1562  return x;
1563 }
1564 
1565 static int get32_packet(vorb *f)
1566 {
1567  uint32 x;
1568  x = get8_packet(f);
1569  x += get8_packet(f) << 8;
1570  x += get8_packet(f) << 16;
1571  x += (uint32) get8_packet(f) << 24;
1572  return x;
1573 }
1574 
1575 static void flush_packet(vorb *f)
1576 {
1577  while (get8_packet_raw(f) != EOP);
1578 }
1579 
1580 // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
1581 // as the huffman decoder?
1582 static uint32 get_bits(vorb *f, int n)
1583 {
1584  uint32 z;
1585 
1586  if (f->valid_bits < 0) return 0;
1587  if (f->valid_bits < n) {
1588  if (n > 24) {
1589  // the accumulator technique below would not work correctly in this case
1590  z = get_bits(f, 24);
1591  z += get_bits(f, n-24) << 24;
1592  return z;
1593  }
1594  if (f->valid_bits == 0) f->acc = 0;
1595  while (f->valid_bits < n) {
1596  int z = get8_packet_raw(f);
1597  if (z == EOP) {
1598  f->valid_bits = INVALID_BITS;
1599  return 0;
1600  }
1601  f->acc += z << f->valid_bits;
1602  f->valid_bits += 8;
1603  }
1604  }
1605  if (f->valid_bits < 0) return 0;
1606  z = f->acc & ((1 << n)-1);
1607  f->acc >>= n;
1608  f->valid_bits -= n;
1609  return z;
1610 }
1611 
1612 // @OPTIMIZE: primary accumulator for huffman
1613 // expand the buffer to as many bits as possible without reading off end of packet
1614 // it might be nice to allow f->valid_bits and f->acc to be stored in registers,
1615 // e.g. cache them locally and decode locally
1617 {
1618  if (f->valid_bits <= 24) {
1619  if (f->valid_bits == 0) f->acc = 0;
1620  do {
1621  int z;
1622  if (f->last_seg && !f->bytes_in_seg) return;
1623  z = get8_packet_raw(f);
1624  if (z == EOP) return;
1625  f->acc += (unsigned) z << f->valid_bits;
1626  f->valid_bits += 8;
1627  } while (f->valid_bits <= 24);
1628  }
1629 }
1630 
1631 enum
1632 {
1636 };
1637 
1639 {
1640  int i;
1641  prep_huffman(f);
1642 
1643  if (c->codewords == NULL && c->sorted_codewords == NULL)
1644  return -1;
1645 
1646  // cases to use binary search: sorted_codewords && !c->codewords
1647  // sorted_codewords && c->entries > 8
1648  if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) {
1649  // binary search
1650  uint32 code = bit_reverse(f->acc);
1651  int x=0, n=c->sorted_entries, len;
1652 
1653  while (n > 1) {
1654  // invariant: sc[x] <= code < sc[x+n]
1655  int m = x + (n >> 1);
1656  if (c->sorted_codewords[m] <= code) {
1657  x = m;
1658  n -= (n>>1);
1659  } else {
1660  n >>= 1;
1661  }
1662  }
1663  // x is now the sorted index
1664  if (!c->sparse) x = c->sorted_values[x];
1665  // x is now sorted index if sparse, or symbol otherwise
1666  len = c->codeword_lengths[x];
1667  if (f->valid_bits >= len) {
1668  f->acc >>= len;
1669  f->valid_bits -= len;
1670  return x;
1671  }
1672 
1673  f->valid_bits = 0;
1674  return -1;
1675  }
1676 
1677  // if small, linear search
1678  assert(!c->sparse);
1679  for (i=0; i < c->entries; ++i) {
1680  if (c->codeword_lengths[i] == NO_CODE) continue;
1681  if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) {
1682  if (f->valid_bits >= c->codeword_lengths[i]) {
1683  f->acc >>= c->codeword_lengths[i];
1684  f->valid_bits -= c->codeword_lengths[i];
1685  return i;
1686  }
1687  f->valid_bits = 0;
1688  return -1;
1689  }
1690  }
1691 
1693  f->valid_bits = 0;
1694  return -1;
1695 }
1696 
1697 #ifndef STB_VORBIS_NO_INLINE_DECODE
1698 
1699 #define DECODE_RAW(var, f,c) \
1700  if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \
1701  prep_huffman(f); \
1702  var = f->acc & FAST_HUFFMAN_TABLE_MASK; \
1703  var = c->fast_huffman[var]; \
1704  if (var >= 0) { \
1705  int n = c->codeword_lengths[var]; \
1706  f->acc >>= n; \
1707  f->valid_bits -= n; \
1708  if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
1709  } else { \
1710  var = codebook_decode_scalar_raw(f,c); \
1711  }
1712 
1713 #else
1714 
1715 static int codebook_decode_scalar(vorb *f, Codebook *c)
1716 {
1717  int i;
1718  if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
1719  prep_huffman(f);
1720  // fast huffman table lookup
1721  i = f->acc & FAST_HUFFMAN_TABLE_MASK;
1722  i = c->fast_huffman[i];
1723  if (i >= 0) {
1724  f->acc >>= c->codeword_lengths[i];
1725  f->valid_bits -= c->codeword_lengths[i];
1726  if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }
1727  return i;
1728  }
1729  return codebook_decode_scalar_raw(f,c);
1730 }
1731 
1732 #define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);
1733 
1734 #endif
1735 
1736 #define DECODE(var,f,c) \
1737  DECODE_RAW(var,f,c) \
1738  if (c->sparse) var = c->sorted_values[var];
1739 
1740 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1741  #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)
1742 #else
1743  #define DECODE_VQ(var,f,c) DECODE(var,f,c)
1744 #endif
1745 
1746 
1747 
1748 
1749 
1750 
1751 // CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
1752 // where we avoid one addition
1753 #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
1754 #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
1755 #define CODEBOOK_ELEMENT_BASE(c) (0)
1756 
1758 {
1759  int z = -1;
1760 
1761  // type 0 is only legal in a scalar context
1762  if (c->lookup_type == 0)
1764  else {
1765  DECODE_VQ(z,f,c);
1766  if (c->sparse) assert(z < c->sorted_entries);
1767  if (z < 0) { // check for EOP
1768  if (!f->bytes_in_seg)
1769  if (f->last_seg)
1770  return z;
1772  }
1773  }
1774  return z;
1775 }
1776 
1777 static int codebook_decode(vorb *f, Codebook *c, float *output, int len)
1778 {
1779  int i,z = codebook_decode_start(f,c);
1780  if (z < 0) return FALSE;
1781  if (len > c->dimensions) len = c->dimensions;
1782 
1783 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1784  if (c->lookup_type == 1) {
1785  float last = CODEBOOK_ELEMENT_BASE(c);
1786  int div = 1;
1787  for (i=0; i < len; ++i) {
1788  int off = (z / div) % c->lookup_values;
1789  float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
1790  output[i] += val;
1791  if (c->sequence_p) last = val + c->minimum_value;
1792  div *= c->lookup_values;
1793  }
1794  return TRUE;
1795  }
1796 #endif
1797 
1798  z *= c->dimensions;
1799  if (c->sequence_p) {
1800  float last = CODEBOOK_ELEMENT_BASE(c);
1801  for (i=0; i < len; ++i) {
1802  float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1803  output[i] += val;
1804  last = val + c->minimum_value;
1805  }
1806  } else {
1807  float last = CODEBOOK_ELEMENT_BASE(c);
1808  for (i=0; i < len; ++i) {
1809  output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1810  }
1811  }
1812 
1813  return TRUE;
1814 }
1815 
1816 static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step)
1817 {
1818  int i,z = codebook_decode_start(f,c);
1819  float last = CODEBOOK_ELEMENT_BASE(c);
1820  if (z < 0) return FALSE;
1821  if (len > c->dimensions) len = c->dimensions;
1822 
1823 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1824  if (c->lookup_type == 1) {
1825  int div = 1;
1826  for (i=0; i < len; ++i) {
1827  int off = (z / div) % c->lookup_values;
1828  float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
1829  output[i*step] += val;
1830  if (c->sequence_p) last = val;
1831  div *= c->lookup_values;
1832  }
1833  return TRUE;
1834  }
1835 #endif
1836 
1837  z *= c->dimensions;
1838  for (i=0; i < len; ++i) {
1839  float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1840  output[i*step] += val;
1841  if (c->sequence_p) last = val;
1842  }
1843 
1844  return TRUE;
1845 }
1846 
1847 static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode)
1848 {
1849  int c_inter = *c_inter_p;
1850  int p_inter = *p_inter_p;
1851  int i,z, effective = c->dimensions;
1852 
1853  // type 0 is only legal in a scalar context
1854  if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream);
1855 
1856  while (total_decode > 0) {
1857  float last = CODEBOOK_ELEMENT_BASE(c);
1858  DECODE_VQ(z,f,c);
1859  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1860  assert(!c->sparse || z < c->sorted_entries);
1861  #endif
1862  if (z < 0) {
1863  if (!f->bytes_in_seg)
1864  if (f->last_seg) return FALSE;
1865  return error(f, VORBIS_invalid_stream);
1866  }
1867 
1868  // if this will take us off the end of the buffers, stop short!
1869  // we check by computing the length of the virtual interleaved
1870  // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
1871  // and the length we'll be using (effective)
1872  if (c_inter + p_inter*ch + effective > len * ch) {
1873  effective = len*ch - (p_inter*ch - c_inter);
1874  }
1875 
1876  #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1877  if (c->lookup_type == 1) {
1878  int div = 1;
1879  for (i=0; i < effective; ++i) {
1880  int off = (z / div) % c->lookup_values;
1881  float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
1882  if (outputs[c_inter])
1883  outputs[c_inter][p_inter] += val;
1884  if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1885  if (c->sequence_p) last = val;
1886  div *= c->lookup_values;
1887  }
1888  } else
1889  #endif
1890  {
1891  z *= c->dimensions;
1892  if (c->sequence_p) {
1893  for (i=0; i < effective; ++i) {
1894  float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1895  if (outputs[c_inter])
1896  outputs[c_inter][p_inter] += val;
1897  if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1898  last = val;
1899  }
1900  } else {
1901  for (i=0; i < effective; ++i) {
1902  float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1903  if (outputs[c_inter])
1904  outputs[c_inter][p_inter] += val;
1905  if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1906  }
1907  }
1908  }
1909 
1910  total_decode -= effective;
1911  }
1912  *c_inter_p = c_inter;
1913  *p_inter_p = p_inter;
1914  return TRUE;
1915 }
1916 
1917 static int predict_point(int x, int x0, int x1, int y0, int y1)
1918 {
1919  int dy = y1 - y0;
1920  int adx = x1 - x0;
1921  // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
1922  int err = abs(dy) * (x - x0);
1923  int off = err / adx;
1924  return dy < 0 ? y0 - off : y0 + off;
1925 }
1926 
1927 // the following table is block-copied from the specification
1928 static float inverse_db_table[256] =
1929 {
1930  1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
1931  1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
1932  1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
1933  2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
1934  2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
1935  3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
1936  4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
1937  6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
1938  7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
1939  1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
1940  1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
1941  1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
1942  2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
1943  2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
1944  3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
1945  4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
1946  5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
1947  7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
1948  9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
1949  1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
1950  1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
1951  2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
1952  2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
1953  3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
1954  4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
1955  5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
1956  7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
1957  9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
1958  0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
1959  0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
1960  0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
1961  0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
1962  0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
1963  0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
1964  0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
1965  0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
1966  0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
1967  0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
1968  0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
1969  0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
1970  0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
1971  0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
1972  0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
1973  0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
1974  0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
1975  0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
1976  0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
1977  0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
1978  0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
1979  0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
1980  0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
1981  0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
1982  0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
1983  0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
1984  0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
1985  0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
1986  0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
1987  0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
1988  0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
1989  0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
1990  0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
1991  0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
1992  0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
1993  0.82788260f, 0.88168307f, 0.9389798f, 1.0f
1994 };
1995 
1996 
1997 // @OPTIMIZE: if you want to replace this bresenham line-drawing routine,
1998 // note that you must produce bit-identical output to decode correctly;
1999 // this specific sequence of operations is specified in the spec (it's
2000 // drawing integer-quantized frequency-space lines that the encoder
2001 // expects to be exactly the same)
2002 // ... also, isn't the whole point of Bresenham's algorithm to NOT
2003 // have to divide in the setup? sigh.
2004 #ifndef STB_VORBIS_NO_DEFER_FLOOR
2005 #define LINE_OP(a,b) a *= b
2006 #else
2007 #define LINE_OP(a,b) a = b
2008 #endif
2009 
2010 #ifdef STB_VORBIS_DIVIDE_TABLE
2011 #define DIVTAB_NUMER 32
2012 #define DIVTAB_DENOM 64
2013 int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB
2014 #endif
2015 
2016 static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n)
2017 {
2018  int dy = y1 - y0;
2019  int adx = x1 - x0;
2020  int ady = abs(dy);
2021  int base;
2022  int x=x0,y=y0;
2023  int err = 0;
2024  int sy;
2025 
2026 #ifdef STB_VORBIS_DIVIDE_TABLE
2027  if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {
2028  if (dy < 0) {
2029  base = -integer_divide_table[ady][adx];
2030  sy = base-1;
2031  } else {
2032  base = integer_divide_table[ady][adx];
2033  sy = base+1;
2034  }
2035  } else {
2036  base = dy / adx;
2037  if (dy < 0)
2038  sy = base - 1;
2039  else
2040  sy = base+1;
2041  }
2042 #else
2043  base = dy / adx;
2044  if (dy < 0)
2045  sy = base - 1;
2046  else
2047  sy = base+1;
2048 #endif
2049  ady -= abs(base) * adx;
2050  if (x1 > n) x1 = n;
2051  if (x < x1) {
2052  LINE_OP(output[x], inverse_db_table[y&255]);
2053  for (++x; x < x1; ++x) {
2054  err += ady;
2055  if (err >= adx) {
2056  err -= adx;
2057  y += sy;
2058  } else
2059  y += base;
2060  LINE_OP(output[x], inverse_db_table[y&255]);
2061  }
2062  }
2063 }
2064 
2065 static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype)
2066 {
2067  int k;
2068  if (rtype == 0) {
2069  int step = n / book->dimensions;
2070  for (k=0; k < step; ++k)
2071  if (!codebook_decode_step(f, book, target+offset+k, n-offset-k, step))
2072  return FALSE;
2073  } else {
2074  for (k=0; k < n; ) {
2075  if (!codebook_decode(f, book, target+offset, n-k))
2076  return FALSE;
2077  k += book->dimensions;
2078  offset += book->dimensions;
2079  }
2080  }
2081  return TRUE;
2082 }
2083 
2084 // n is 1/2 of the blocksize --
2085 // specification: "Correct per-vector decode length is [n]/2"
2086 static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode)
2087 {
2088  int i,j,pass;
2089  Residue *r = f->residue_config + rn;
2090  int rtype = f->residue_types[rn];
2091  int c = r->classbook;
2092  int classwords = f->codebooks[c].dimensions;
2093  unsigned int actual_size = rtype == 2 ? n*2 : n;
2094  unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size);
2095  unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size);
2096  int n_read = limit_r_end - limit_r_begin;
2097  int part_read = n_read / r->part_size;
2098  int temp_alloc_point = temp_alloc_save(f);
2099  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2100  uint8 ***part_classdata = (uint8 ***) temp_block_array(f,f->channels, part_read * sizeof(**part_classdata));
2101  #else
2102  int **classifications = (int **) temp_block_array(f,f->channels, part_read * sizeof(**classifications));
2103  #endif
2104 
2105  CHECK(f);
2106 
2107  for (i=0; i < ch; ++i)
2108  if (!do_not_decode[i])
2109  memset(residue_buffers[i], 0, sizeof(float) * n);
2110 
2111  if (rtype == 2 && ch != 1) {
2112  for (j=0; j < ch; ++j)
2113  if (!do_not_decode[j])
2114  break;
2115  if (j == ch)
2116  goto done;
2117 
2118  for (pass=0; pass < 8; ++pass) {
2119  int pcount = 0, class_set = 0;
2120  if (ch == 2) {
2121  while (pcount < part_read) {
2122  int z = r->begin + pcount*r->part_size;
2123  int c_inter = (z & 1), p_inter = z>>1;
2124  if (pass == 0) {
2125  Codebook *c = f->codebooks+r->classbook;
2126  int q;
2127  DECODE(q,f,c);
2128  if (q == EOP) goto done;
2129  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2130  part_classdata[0][class_set] = r->classdata[q];
2131  #else
2132  for (i=classwords-1; i >= 0; --i) {
2133  classifications[0][i+pcount] = q % r->classifications;
2134  q /= r->classifications;
2135  }
2136  #endif
2137  }
2138  for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2139  int z = r->begin + pcount*r->part_size;
2140  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2141  int c = part_classdata[0][class_set][i];
2142  #else
2143  int c = classifications[0][pcount];
2144  #endif
2145  int b = r->residue_books[c][pass];
2146  if (b >= 0) {
2147  Codebook *book = f->codebooks + b;
2148  #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
2149  if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2150  goto done;
2151  #else
2152  // saves 1%
2153  if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2154  goto done;
2155  #endif
2156  } else {
2157  z += r->part_size;
2158  c_inter = z & 1;
2159  p_inter = z >> 1;
2160  }
2161  }
2162  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2163  ++class_set;
2164  #endif
2165  }
2166  } else if (ch > 2) {
2167  while (pcount < part_read) {
2168  int z = r->begin + pcount*r->part_size;
2169  int c_inter = z % ch, p_inter = z/ch;
2170  if (pass == 0) {
2171  Codebook *c = f->codebooks+r->classbook;
2172  int q;
2173  DECODE(q,f,c);
2174  if (q == EOP) goto done;
2175  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2176  part_classdata[0][class_set] = r->classdata[q];
2177  #else
2178  for (i=classwords-1; i >= 0; --i) {
2179  classifications[0][i+pcount] = q % r->classifications;
2180  q /= r->classifications;
2181  }
2182  #endif
2183  }
2184  for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2185  int z = r->begin + pcount*r->part_size;
2186  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2187  int c = part_classdata[0][class_set][i];
2188  #else
2189  int c = classifications[0][pcount];
2190  #endif
2191  int b = r->residue_books[c][pass];
2192  if (b >= 0) {
2193  Codebook *book = f->codebooks + b;
2194  if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2195  goto done;
2196  } else {
2197  z += r->part_size;
2198  c_inter = z % ch;
2199  p_inter = z / ch;
2200  }
2201  }
2202  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2203  ++class_set;
2204  #endif
2205  }
2206  }
2207  }
2208  goto done;
2209  }
2210  CHECK(f);
2211 
2212  for (pass=0; pass < 8; ++pass) {
2213  int pcount = 0, class_set=0;
2214  while (pcount < part_read) {
2215  if (pass == 0) {
2216  for (j=0; j < ch; ++j) {
2217  if (!do_not_decode[j]) {
2218  Codebook *c = f->codebooks+r->classbook;
2219  int temp;
2220  DECODE(temp,f,c);
2221  if (temp == EOP) goto done;
2222  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2223  part_classdata[j][class_set] = r->classdata[temp];
2224  #else
2225  for (i=classwords-1; i >= 0; --i) {
2226  classifications[j][i+pcount] = temp % r->classifications;
2227  temp /= r->classifications;
2228  }
2229  #endif
2230  }
2231  }
2232  }
2233  for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2234  for (j=0; j < ch; ++j) {
2235  if (!do_not_decode[j]) {
2236  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2237  int c = part_classdata[j][class_set][i];
2238  #else
2239  int c = classifications[j][pcount];
2240  #endif
2241  int b = r->residue_books[c][pass];
2242  if (b >= 0) {
2243  float *target = residue_buffers[j];
2244  int offset = r->begin + pcount * r->part_size;
2245  int n = r->part_size;
2246  Codebook *book = f->codebooks + b;
2247  if (!residue_decode(f, book, target, offset, n, rtype))
2248  goto done;
2249  }
2250  }
2251  }
2252  }
2253  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2254  ++class_set;
2255  #endif
2256  }
2257  }
2258  done:
2259  CHECK(f);
2260  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2261  temp_free(f,part_classdata);
2262  #else
2263  temp_free(f,classifications);
2264  #endif
2265  temp_alloc_restore(f,temp_alloc_point);
2266 }
2267 
2268 
2269 #if 0
2270 // slow way for debugging
2271 void inverse_mdct_slow(float *buffer, int n)
2272 {
2273  int i,j;
2274  int n2 = n >> 1;
2275  float *x = (float *) malloc(sizeof(*x) * n2);
2276  memcpy(x, buffer, sizeof(*x) * n2);
2277  for (i=0; i < n; ++i) {
2278  float acc = 0;
2279  for (j=0; j < n2; ++j)
2280  // formula from paper:
2281  //acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
2282  // formula from wikipedia
2283  //acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
2284  // these are equivalent, except the formula from the paper inverts the multiplier!
2285  // however, what actually works is NO MULTIPLIER!?!
2286  //acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
2287  acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
2288  buffer[i] = acc;
2289  }
2290  free(x);
2291 }
2292 #elif 0
2293 // same as above, but just barely able to run in real time on modern machines
2294 void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype)
2295 {
2296  float mcos[16384];
2297  int i,j;
2298  int n2 = n >> 1, nmask = (n << 2) -1;
2299  float *x = (float *) malloc(sizeof(*x) * n2);
2300  memcpy(x, buffer, sizeof(*x) * n2);
2301  for (i=0; i < 4*n; ++i)
2302  mcos[i] = (float) cos(M_PI / 2 * i / n);
2303 
2304  for (i=0; i < n; ++i) {
2305  float acc = 0;
2306  for (j=0; j < n2; ++j)
2307  acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask];
2308  buffer[i] = acc;
2309  }
2310  free(x);
2311 }
2312 #elif 0
2313 // transform to use a slow dct-iv; this is STILL basically trivial,
2314 // but only requires half as many ops
2315 void dct_iv_slow(float *buffer, int n)
2316 {
2317  float mcos[16384];
2318  float x[2048];
2319  int i,j;
2320  int n2 = n >> 1, nmask = (n << 3) - 1;
2321  memcpy(x, buffer, sizeof(*x) * n);
2322  for (i=0; i < 8*n; ++i)
2323  mcos[i] = (float) cos(M_PI / 4 * i / n);
2324  for (i=0; i < n; ++i) {
2325  float acc = 0;
2326  for (j=0; j < n; ++j)
2327  acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask];
2328  buffer[i] = acc;
2329  }
2330 }
2331 
2332 void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype)
2333 {
2334  int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
2335  float temp[4096];
2336 
2337  memcpy(temp, buffer, n2 * sizeof(float));
2338  dct_iv_slow(temp, n2); // returns -c'-d, a-b'
2339 
2340  for (i=0; i < n4 ; ++i) buffer[i] = temp[i+n4]; // a-b'
2341  for ( ; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1]; // b-a', c+d'
2342  for ( ; i < n ; ++i) buffer[i] = -temp[i - n3_4]; // c'+d
2343 }
2344 #endif
2345 
2346 #ifndef LIBVORBIS_MDCT
2347 #define LIBVORBIS_MDCT 0
2348 #endif
2349 
2350 #if LIBVORBIS_MDCT
2351 // directly call the vorbis MDCT using an interface documented
2352 // by Jeff Roberts... useful for performance comparison
2353 typedef struct
2354 {
2355  int n;
2356  int log2n;
2357 
2358  float *trig;
2359  int *bitrev;
2360 
2361  float scale;
2362 } mdct_lookup;
2363 
2364 extern void mdct_init(mdct_lookup *lookup, int n);
2365 extern void mdct_clear(mdct_lookup *l);
2366 extern void mdct_backward(mdct_lookup *init, float *in, float *out);
2367 
2368 mdct_lookup M1,M2;
2369 
2370 void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2371 {
2372  mdct_lookup *M;
2373  if (M1.n == n) M = &M1;
2374  else if (M2.n == n) M = &M2;
2375  else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; }
2376  else {
2377  if (M2.n) __asm int 3;
2378  mdct_init(&M2, n);
2379  M = &M2;
2380  }
2381 
2382  mdct_backward(M, buffer, buffer);
2383 }
2384 #endif
2385 
2386 
2387 // the following were split out into separate functions while optimizing;
2388 // they could be pushed back up but eh. __forceinline showed no change;
2389 // they're probably already being inlined.
2390 static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A)
2391 {
2392  float *ee0 = e + i_off;
2393  float *ee2 = ee0 + k_off;
2394  int i;
2395 
2396  assert((n & 3) == 0);
2397  for (i=(n>>2); i > 0; --i) {
2398  float k00_20, k01_21;
2399  k00_20 = ee0[ 0] - ee2[ 0];
2400  k01_21 = ee0[-1] - ee2[-1];
2401  ee0[ 0] += ee2[ 0];//ee0[ 0] = ee0[ 0] + ee2[ 0];
2402  ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1];
2403  ee2[ 0] = k00_20 * A[0] - k01_21 * A[1];
2404  ee2[-1] = k01_21 * A[0] + k00_20 * A[1];
2405  A += 8;
2406 
2407  k00_20 = ee0[-2] - ee2[-2];
2408  k01_21 = ee0[-3] - ee2[-3];
2409  ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2];
2410  ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3];
2411  ee2[-2] = k00_20 * A[0] - k01_21 * A[1];
2412  ee2[-3] = k01_21 * A[0] + k00_20 * A[1];
2413  A += 8;
2414 
2415  k00_20 = ee0[-4] - ee2[-4];
2416  k01_21 = ee0[-5] - ee2[-5];
2417  ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4];
2418  ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5];
2419  ee2[-4] = k00_20 * A[0] - k01_21 * A[1];
2420  ee2[-5] = k01_21 * A[0] + k00_20 * A[1];
2421  A += 8;
2422 
2423  k00_20 = ee0[-6] - ee2[-6];
2424  k01_21 = ee0[-7] - ee2[-7];
2425  ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6];
2426  ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7];
2427  ee2[-6] = k00_20 * A[0] - k01_21 * A[1];
2428  ee2[-7] = k01_21 * A[0] + k00_20 * A[1];
2429  A += 8;
2430  ee0 -= 8;
2431  ee2 -= 8;
2432  }
2433 }
2434 
2435 static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1)
2436 {
2437  int i;
2438  float k00_20, k01_21;
2439 
2440  float *e0 = e + d0;
2441  float *e2 = e0 + k_off;
2442 
2443  for (i=lim >> 2; i > 0; --i) {
2444  k00_20 = e0[-0] - e2[-0];
2445  k01_21 = e0[-1] - e2[-1];
2446  e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0];
2447  e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1];
2448  e2[-0] = (k00_20)*A[0] - (k01_21) * A[1];
2449  e2[-1] = (k01_21)*A[0] + (k00_20) * A[1];
2450 
2451  A += k1;
2452 
2453  k00_20 = e0[-2] - e2[-2];
2454  k01_21 = e0[-3] - e2[-3];
2455  e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2];
2456  e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3];
2457  e2[-2] = (k00_20)*A[0] - (k01_21) * A[1];
2458  e2[-3] = (k01_21)*A[0] + (k00_20) * A[1];
2459 
2460  A += k1;
2461 
2462  k00_20 = e0[-4] - e2[-4];
2463  k01_21 = e0[-5] - e2[-5];
2464  e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4];
2465  e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5];
2466  e2[-4] = (k00_20)*A[0] - (k01_21) * A[1];
2467  e2[-5] = (k01_21)*A[0] + (k00_20) * A[1];
2468 
2469  A += k1;
2470 
2471  k00_20 = e0[-6] - e2[-6];
2472  k01_21 = e0[-7] - e2[-7];
2473  e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6];
2474  e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7];
2475  e2[-6] = (k00_20)*A[0] - (k01_21) * A[1];
2476  e2[-7] = (k01_21)*A[0] + (k00_20) * A[1];
2477 
2478  e0 -= 8;
2479  e2 -= 8;
2480 
2481  A += k1;
2482  }
2483 }
2484 
2485 static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0)
2486 {
2487  int i;
2488  float A0 = A[0];
2489  float A1 = A[0+1];
2490  float A2 = A[0+a_off];
2491  float A3 = A[0+a_off+1];
2492  float A4 = A[0+a_off*2+0];
2493  float A5 = A[0+a_off*2+1];
2494  float A6 = A[0+a_off*3+0];
2495  float A7 = A[0+a_off*3+1];
2496 
2497  float k00,k11;
2498 
2499  float *ee0 = e +i_off;
2500  float *ee2 = ee0+k_off;
2501 
2502  for (i=n; i > 0; --i) {
2503  k00 = ee0[ 0] - ee2[ 0];
2504  k11 = ee0[-1] - ee2[-1];
2505  ee0[ 0] = ee0[ 0] + ee2[ 0];
2506  ee0[-1] = ee0[-1] + ee2[-1];
2507  ee2[ 0] = (k00) * A0 - (k11) * A1;
2508  ee2[-1] = (k11) * A0 + (k00) * A1;
2509 
2510  k00 = ee0[-2] - ee2[-2];
2511  k11 = ee0[-3] - ee2[-3];
2512  ee0[-2] = ee0[-2] + ee2[-2];
2513  ee0[-3] = ee0[-3] + ee2[-3];
2514  ee2[-2] = (k00) * A2 - (k11) * A3;
2515  ee2[-3] = (k11) * A2 + (k00) * A3;
2516 
2517  k00 = ee0[-4] - ee2[-4];
2518  k11 = ee0[-5] - ee2[-5];
2519  ee0[-4] = ee0[-4] + ee2[-4];
2520  ee0[-5] = ee0[-5] + ee2[-5];
2521  ee2[-4] = (k00) * A4 - (k11) * A5;
2522  ee2[-5] = (k11) * A4 + (k00) * A5;
2523 
2524  k00 = ee0[-6] - ee2[-6];
2525  k11 = ee0[-7] - ee2[-7];
2526  ee0[-6] = ee0[-6] + ee2[-6];
2527  ee0[-7] = ee0[-7] + ee2[-7];
2528  ee2[-6] = (k00) * A6 - (k11) * A7;
2529  ee2[-7] = (k11) * A6 + (k00) * A7;
2530 
2531  ee0 -= k0;
2532  ee2 -= k0;
2533  }
2534 }
2535 
2536 static __forceinline void iter_54(float *z)
2537 {
2538  float k00,k11,k22,k33;
2539  float y0,y1,y2,y3;
2540 
2541  k00 = z[ 0] - z[-4];
2542  y0 = z[ 0] + z[-4];
2543  y2 = z[-2] + z[-6];
2544  k22 = z[-2] - z[-6];
2545 
2546  z[-0] = y0 + y2; // z0 + z4 + z2 + z6
2547  z[-2] = y0 - y2; // z0 + z4 - z2 - z6
2548 
2549  // done with y0,y2
2550 
2551  k33 = z[-3] - z[-7];
2552 
2553  z[-4] = k00 + k33; // z0 - z4 + z3 - z7
2554  z[-6] = k00 - k33; // z0 - z4 - z3 + z7
2555 
2556  // done with k33
2557 
2558  k11 = z[-1] - z[-5];
2559  y1 = z[-1] + z[-5];
2560  y3 = z[-3] + z[-7];
2561 
2562  z[-1] = y1 + y3; // z1 + z5 + z3 + z7
2563  z[-3] = y1 - y3; // z1 + z5 - z3 - z7
2564  z[-5] = k11 - k22; // z1 - z5 + z2 - z6
2565  z[-7] = k11 + k22; // z1 - z5 - z2 + z6
2566 }
2567 
2568 static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n)
2569 {
2570  int a_off = base_n >> 3;
2571  float A2 = A[0+a_off];
2572  float *z = e + i_off;
2573  float *base = z - 16 * n;
2574 
2575  while (z > base) {
2576  float k00,k11;
2577 
2578  k00 = z[-0] - z[-8];
2579  k11 = z[-1] - z[-9];
2580  z[-0] = z[-0] + z[-8];
2581  z[-1] = z[-1] + z[-9];
2582  z[-8] = k00;
2583  z[-9] = k11 ;
2584 
2585  k00 = z[ -2] - z[-10];
2586  k11 = z[ -3] - z[-11];
2587  z[ -2] = z[ -2] + z[-10];
2588  z[ -3] = z[ -3] + z[-11];
2589  z[-10] = (k00+k11) * A2;
2590  z[-11] = (k11-k00) * A2;
2591 
2592  k00 = z[-12] - z[ -4]; // reverse to avoid a unary negation
2593  k11 = z[ -5] - z[-13];
2594  z[ -4] = z[ -4] + z[-12];
2595  z[ -5] = z[ -5] + z[-13];
2596  z[-12] = k11;
2597  z[-13] = k00;
2598 
2599  k00 = z[-14] - z[ -6]; // reverse to avoid a unary negation
2600  k11 = z[ -7] - z[-15];
2601  z[ -6] = z[ -6] + z[-14];
2602  z[ -7] = z[ -7] + z[-15];
2603  z[-14] = (k00+k11) * A2;
2604  z[-15] = (k00-k11) * A2;
2605 
2606  iter_54(z);
2607  iter_54(z-8);
2608  z -= 16;
2609  }
2610 }
2611 
2612 static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
2613 {
2614  int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
2615  int ld;
2616  // @OPTIMIZE: reduce register pressure by using fewer variables?
2617  int save_point = temp_alloc_save(f);
2618  float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2));
2619  float *u=NULL,*v=NULL;
2620  // twiddle factors
2621  float *A = f->A[blocktype];
2622 
2623  // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
2624  // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function.
2625 
2626  // kernel from paper
2627 
2628 
2629  // merged:
2630  // copy and reflect spectral data
2631  // step 0
2632 
2633  // note that it turns out that the items added together during
2634  // this step are, in fact, being added to themselves (as reflected
2635  // by step 0). inexplicable inefficiency! this became obvious
2636  // once I combined the passes.
2637 
2638  // so there's a missing 'times 2' here (for adding X to itself).
2639  // this propagates through linearly to the end, where the numbers
2640  // are 1/2 too small, and need to be compensated for.
2641 
2642  {
2643  float *d,*e, *AA, *e_stop;
2644  d = &buf2[n2-2];
2645  AA = A;
2646  e = &buffer[0];
2647  e_stop = &buffer[n2];
2648  while (e != e_stop) {
2649  d[1] = (e[0] * AA[0] - e[2]*AA[1]);
2650  d[0] = (e[0] * AA[1] + e[2]*AA[0]);
2651  d -= 2;
2652  AA += 2;
2653  e += 4;
2654  }
2655 
2656  e = &buffer[n2-3];
2657  while (d >= buf2) {
2658  d[1] = (-e[2] * AA[0] - -e[0]*AA[1]);
2659  d[0] = (-e[2] * AA[1] + -e[0]*AA[0]);
2660  d -= 2;
2661  AA += 2;
2662  e -= 4;
2663  }
2664  }
2665 
2666  // now we use symbolic names for these, so that we can
2667  // possibly swap their meaning as we change which operations
2668  // are in place
2669 
2670  u = buffer;
2671  v = buf2;
2672 
2673  // step 2 (paper output is w, now u)
2674  // this could be in place, but the data ends up in the wrong
2675  // place... _somebody_'s got to swap it, so this is nominated
2676  {
2677  float *AA = &A[n2-8];
2678  float *d0,*d1, *e0, *e1;
2679 
2680  e0 = &v[n4];
2681  e1 = &v[0];
2682 
2683  d0 = &u[n4];
2684  d1 = &u[0];
2685 
2686  while (AA >= A) {
2687  float v40_20, v41_21;
2688 
2689  v41_21 = e0[1] - e1[1];
2690  v40_20 = e0[0] - e1[0];
2691  d0[1] = e0[1] + e1[1];
2692  d0[0] = e0[0] + e1[0];
2693  d1[1] = v41_21*AA[4] - v40_20*AA[5];
2694  d1[0] = v40_20*AA[4] + v41_21*AA[5];
2695 
2696  v41_21 = e0[3] - e1[3];
2697  v40_20 = e0[2] - e1[2];
2698  d0[3] = e0[3] + e1[3];
2699  d0[2] = e0[2] + e1[2];
2700  d1[3] = v41_21*AA[0] - v40_20*AA[1];
2701  d1[2] = v40_20*AA[0] + v41_21*AA[1];
2702 
2703  AA -= 8;
2704 
2705  d0 += 4;
2706  d1 += 4;
2707  e0 += 4;
2708  e1 += 4;
2709  }
2710  }
2711 
2712  // step 3
2713  ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
2714 
2715  // optimized step 3:
2716 
2717  // the original step3 loop can be nested r inside s or s inside r;
2718  // it's written originally as s inside r, but this is dumb when r
2719  // iterates many times, and s few. So I have two copies of it and
2720  // switch between them halfway.
2721 
2722  // this is iteration 0 of step 3
2723  imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*0, -(n >> 3), A);
2724  imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*1, -(n >> 3), A);
2725 
2726  // this is iteration 1 of step 3
2727  imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*0, -(n >> 4), A, 16);
2728  imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*1, -(n >> 4), A, 16);
2729  imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*2, -(n >> 4), A, 16);
2730  imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*3, -(n >> 4), A, 16);
2731 
2732  l=2;
2733  for (; l < (ld-3)>>1; ++l) {
2734  int k0 = n >> (l+2), k0_2 = k0>>1;
2735  int lim = 1 << (l+1);
2736  int i;
2737  for (i=0; i < lim; ++i)
2738  imdct_step3_inner_r_loop(n >> (l+4), u, n2-1 - k0*i, -k0_2, A, 1 << (l+3));
2739  }
2740 
2741  for (; l < ld-6; ++l) {
2742  int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1;
2743  int rlim = n >> (l+6), r;
2744  int lim = 1 << (l+1);
2745  int i_off;
2746  float *A0 = A;
2747  i_off = n2-1;
2748  for (r=rlim; r > 0; --r) {
2749  imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0);
2750  A0 += k1*4;
2751  i_off -= 8;
2752  }
2753  }
2754 
2755  // iterations with count:
2756  // ld-6,-5,-4 all interleaved together
2757  // the big win comes from getting rid of needless flops
2758  // due to the constants on pass 5 & 4 being all 1 and 0;
2759  // combining them to be simultaneous to improve cache made little difference
2760  imdct_step3_inner_s_loop_ld654(n >> 5, u, n2-1, A, n);
2761 
2762  // output is u
2763 
2764  // step 4, 5, and 6
2765  // cannot be in-place because of step 5
2766  {
2767  uint16 *bitrev = f->bit_reverse[blocktype];
2768  // weirdly, I'd have thought reading sequentially and writing
2769  // erratically would have been better than vice-versa, but in
2770  // fact that's not what my testing showed. (That is, with
2771  // j = bitreverse(i), do you read i and write j, or read j and write i.)
2772 
2773  float *d0 = &v[n4-4];
2774  float *d1 = &v[n2-4];
2775  while (d0 >= v) {
2776  int k4;
2777 
2778  k4 = bitrev[0];
2779  d1[3] = u[k4+0];
2780  d1[2] = u[k4+1];
2781  d0[3] = u[k4+2];
2782  d0[2] = u[k4+3];
2783 
2784  k4 = bitrev[1];
2785  d1[1] = u[k4+0];
2786  d1[0] = u[k4+1];
2787  d0[1] = u[k4+2];
2788  d0[0] = u[k4+3];
2789 
2790  d0 -= 4;
2791  d1 -= 4;
2792  bitrev += 2;
2793  }
2794  }
2795  // (paper output is u, now v)
2796 
2797 
2798  // data must be in buf2
2799  assert(v == buf2);
2800 
2801  // step 7 (paper output is v, now v)
2802  // this is now in place
2803  {
2804  float *C = f->C[blocktype];
2805  float *d, *e;
2806 
2807  d = v;
2808  e = v + n2 - 4;
2809 
2810  while (d < e) {
2811  float a02,a11,b0,b1,b2,b3;
2812 
2813  a02 = d[0] - e[2];
2814  a11 = d[1] + e[3];
2815 
2816  b0 = C[1]*a02 + C[0]*a11;
2817  b1 = C[1]*a11 - C[0]*a02;
2818 
2819  b2 = d[0] + e[ 2];
2820  b3 = d[1] - e[ 3];
2821 
2822  d[0] = b2 + b0;
2823  d[1] = b3 + b1;
2824  e[2] = b2 - b0;
2825  e[3] = b1 - b3;
2826 
2827  a02 = d[2] - e[0];
2828  a11 = d[3] + e[1];
2829 
2830  b0 = C[3]*a02 + C[2]*a11;
2831  b1 = C[3]*a11 - C[2]*a02;
2832 
2833  b2 = d[2] + e[ 0];
2834  b3 = d[3] - e[ 1];
2835 
2836  d[2] = b2 + b0;
2837  d[3] = b3 + b1;
2838  e[0] = b2 - b0;
2839  e[1] = b1 - b3;
2840 
2841  C += 4;
2842  d += 4;
2843  e -= 4;
2844  }
2845  }
2846 
2847  // data must be in buf2
2848 
2849 
2850  // step 8+decode (paper output is X, now buffer)
2851  // this generates pairs of data a la 8 and pushes them directly through
2852  // the decode kernel (pushing rather than pulling) to avoid having
2853  // to make another pass later
2854 
2855  // this cannot POSSIBLY be in place, so we refer to the buffers directly
2856 
2857  {
2858  float *d0,*d1,*d2,*d3;
2859 
2860  float *B = f->B[blocktype] + n2 - 8;
2861  float *e = buf2 + n2 - 8;
2862  d0 = &buffer[0];
2863  d1 = &buffer[n2-4];
2864  d2 = &buffer[n2];
2865  d3 = &buffer[n-4];
2866  while (e >= v) {
2867  float p0,p1,p2,p3;
2868 
2869  p3 = e[6]*B[7] - e[7]*B[6];
2870  p2 = -e[6]*B[6] - e[7]*B[7];
2871 
2872  d0[0] = p3;
2873  d1[3] = - p3;
2874  d2[0] = p2;
2875  d3[3] = p2;
2876 
2877  p1 = e[4]*B[5] - e[5]*B[4];
2878  p0 = -e[4]*B[4] - e[5]*B[5];
2879 
2880  d0[1] = p1;
2881  d1[2] = - p1;
2882  d2[1] = p0;
2883  d3[2] = p0;
2884 
2885  p3 = e[2]*B[3] - e[3]*B[2];
2886  p2 = -e[2]*B[2] - e[3]*B[3];
2887 
2888  d0[2] = p3;
2889  d1[1] = - p3;
2890  d2[2] = p2;
2891  d3[1] = p2;
2892 
2893  p1 = e[0]*B[1] - e[1]*B[0];
2894  p0 = -e[0]*B[0] - e[1]*B[1];
2895 
2896  d0[3] = p1;
2897  d1[0] = - p1;
2898  d2[3] = p0;
2899  d3[0] = p0;
2900 
2901  B -= 8;
2902  e -= 8;
2903  d0 += 4;
2904  d2 += 4;
2905  d1 -= 4;
2906  d3 -= 4;
2907  }
2908  }
2909 
2910  temp_free(f,buf2);
2911  temp_alloc_restore(f,save_point);
2912 }
2913 
2914 #if 0
2915 // this is the original version of the above code, if you want to optimize it from scratch
2916 void inverse_mdct_naive(float *buffer, int n)
2917 {
2918  float s;
2919  float A[1 << 12], B[1 << 12], C[1 << 11];
2920  int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
2921  int n3_4 = n - n4, ld;
2922  // how can they claim this only uses N words?!
2923  // oh, because they're only used sparsely, whoops
2924  float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13];
2925  // set up twiddle factors
2926 
2927  for (k=k2=0; k < n4; ++k,k2+=2) {
2928  A[k2 ] = (float) cos(4*k*M_PI/n);
2929  A[k2+1] = (float) -sin(4*k*M_PI/n);
2930  B[k2 ] = (float) cos((k2+1)*M_PI/n/2);
2931  B[k2+1] = (float) sin((k2+1)*M_PI/n/2);
2932  }
2933  for (k=k2=0; k < n8; ++k,k2+=2) {
2934  C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);
2935  C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
2936  }
2937 
2938  // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
2939  // Note there are bugs in that pseudocode, presumably due to them attempting
2940  // to rename the arrays nicely rather than representing the way their actual
2941  // implementation bounces buffers back and forth. As a result, even in the
2942  // "some formulars corrected" version, a direct implementation fails. These
2943  // are noted below as "paper bug".
2944 
2945  // copy and reflect spectral data
2946  for (k=0; k < n2; ++k) u[k] = buffer[k];
2947  for ( ; k < n ; ++k) u[k] = -buffer[n - k - 1];
2948  // kernel from paper
2949  // step 1
2950  for (k=k2=k4=0; k < n4; k+=1, k2+=2, k4+=4) {
2951  v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2] - (u[k4+2] - u[n-k4-3])*A[k2+1];
2952  v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3])*A[k2];
2953  }
2954  // step 2
2955  for (k=k4=0; k < n8; k+=1, k4+=4) {
2956  w[n2+3+k4] = v[n2+3+k4] + v[k4+3];
2957  w[n2+1+k4] = v[n2+1+k4] + v[k4+1];
2958  w[k4+3] = (v[n2+3+k4] - v[k4+3])*A[n2-4-k4] - (v[n2+1+k4]-v[k4+1])*A[n2-3-k4];
2959  w[k4+1] = (v[n2+1+k4] - v[k4+1])*A[n2-4-k4] + (v[n2+3+k4]-v[k4+3])*A[n2-3-k4];
2960  }
2961  // step 3
2962  ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
2963  for (l=0; l < ld-3; ++l) {
2964  int k0 = n >> (l+2), k1 = 1 << (l+3);
2965  int rlim = n >> (l+4), r4, r;
2966  int s2lim = 1 << (l+2), s2;
2967  for (r=r4=0; r < rlim; r4+=4,++r) {
2968  for (s2=0; s2 < s2lim; s2+=2) {
2969  u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4];
2970  u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4];
2971  u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1]
2972  - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1];
2973  u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1]
2974  + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1];
2975  }
2976  }
2977  if (l+1 < ld-3) {
2978  // paper bug: ping-ponging of u&w here is omitted
2979  memcpy(w, u, sizeof(u));
2980  }
2981  }
2982 
2983  // step 4
2984  for (i=0; i < n8; ++i) {
2985  int j = bit_reverse(i) >> (32-ld+3);
2986  assert(j < n8);
2987  if (i == j) {
2988  // paper bug: original code probably swapped in place; if copying,
2989  // need to directly copy in this case
2990  int i8 = i << 3;
2991  v[i8+1] = u[i8+1];
2992  v[i8+3] = u[i8+3];
2993  v[i8+5] = u[i8+5];
2994  v[i8+7] = u[i8+7];
2995  } else if (i < j) {
2996  int i8 = i << 3, j8 = j << 3;
2997  v[j8+1] = u[i8+1], v[i8+1] = u[j8 + 1];
2998  v[j8+3] = u[i8+3], v[i8+3] = u[j8 + 3];
2999  v[j8+5] = u[i8+5], v[i8+5] = u[j8 + 5];
3000  v[j8+7] = u[i8+7], v[i8+7] = u[j8 + 7];
3001  }
3002  }
3003  // step 5
3004  for (k=0; k < n2; ++k) {
3005  w[k] = v[k*2+1];
3006  }
3007  // step 6
3008  for (k=k2=k4=0; k < n8; ++k, k2 += 2, k4 += 4) {
3009  u[n-1-k2] = w[k4];
3010  u[n-2-k2] = w[k4+1];
3011  u[n3_4 - 1 - k2] = w[k4+2];
3012  u[n3_4 - 2 - k2] = w[k4+3];
3013  }
3014  // step 7
3015  for (k=k2=0; k < n8; ++k, k2 += 2) {
3016  v[n2 + k2 ] = ( u[n2 + k2] + u[n-2-k2] + C[k2+1]*(u[n2+k2]-u[n-2-k2]) + C[k2]*(u[n2+k2+1]+u[n-2-k2+1]))/2;
3017  v[n-2 - k2] = ( u[n2 + k2] + u[n-2-k2] - C[k2+1]*(u[n2+k2]-u[n-2-k2]) - C[k2]*(u[n2+k2+1]+u[n-2-k2+1]))/2;
3018  v[n2+1+ k2] = ( u[n2+1+k2] - u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2;
3019  v[n-1 - k2] = (-u[n2+1+k2] + u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2;
3020  }
3021  // step 8
3022  for (k=k2=0; k < n4; ++k,k2 += 2) {
3023  X[k] = v[k2+n2]*B[k2 ] + v[k2+1+n2]*B[k2+1];
3024  X[n2-1-k] = v[k2+n2]*B[k2+1] - v[k2+1+n2]*B[k2 ];
3025  }
3026 
3027  // decode kernel to output
3028  // determined the following value experimentally
3029  // (by first figuring out what made inverse_mdct_slow work); then matching that here
3030  // (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?)
3031  s = 0.5; // theoretically would be n4
3032 
3033  // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code,
3034  // so it needs to use the "old" B values to behave correctly, or else
3035  // set s to 1.0 ]]]
3036  for (i=0; i < n4 ; ++i) buffer[i] = s * X[i+n4];
3037  for ( ; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1];
3038  for ( ; i < n ; ++i) buffer[i] = -s * X[i - n3_4];
3039 }
3040 #endif
3041 
3042 static float *get_window(vorb *f, int len)
3043 {
3044  len <<= 1;
3045  if (len == f->blocksize_0) return f->window[0];
3046  if (len == f->blocksize_1) return f->window[1];
3047  return NULL;
3048 }
3049 
3050 #ifndef STB_VORBIS_NO_DEFER_FLOOR
3051 typedef int16 YTYPE;
3052 #else
3053 typedef int YTYPE;
3054 #endif
3055 static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag)
3056 {
3057  int n2 = n >> 1;
3058  int s = map->chan[i].mux, floor;
3059  floor = map->submap_floor[s];
3060  if (f->floor_types[floor] == 0) {
3061  return error(f, VORBIS_invalid_stream);
3062  } else {
3063  Floor1 *g = &f->floor_config[floor].floor1;
3064  int j,q;
3065  int lx = 0, ly = finalY[0] * g->floor1_multiplier;
3066  for (q=1; q < g->values; ++q) {
3067  j = g->sorted_order[q];
3068  #ifndef STB_VORBIS_NO_DEFER_FLOOR
3069  if (finalY[j] >= 0)
3070  #else
3071  if (step2_flag[j])
3072  #endif
3073  {
3074  int hy = finalY[j] * g->floor1_multiplier;
3075  int hx = g->Xlist[j];
3076  if (lx != hx)
3077  draw_line(target, lx,ly, hx,hy, n2);
3078  CHECK(f);
3079  lx = hx, ly = hy;
3080  }
3081  }
3082  if (lx < n2) {
3083  // optimization of: draw_line(target, lx,ly, n,ly, n2);
3084  for (j=lx; j < n2; ++j)
3085  LINE_OP(target[j], inverse_db_table[ly]);
3086  CHECK(f);
3087  }
3088  }
3089  return TRUE;
3090 }
3091 
3092 // The meaning of "left" and "right"
3093 //
3094 // For a given frame:
3095 // we compute samples from 0..n
3096 // window_center is n/2
3097 // we'll window and mix the samples from left_start to left_end with data from the previous frame
3098 // all of the samples from left_end to right_start can be output without mixing; however,
3099 // this interval is 0-length except when transitioning between short and long frames
3100 // all of the samples from right_start to right_end need to be mixed with the next frame,
3101 // which we don't have, so those get saved in a buffer
3102 // frame N's right_end-right_start, the number of samples to mix with the next frame,
3103 // has to be the same as frame N+1's left_end-left_start (which they are by
3104 // construction)
3105 
3106 static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode)
3107 {
3108  Mode *m;
3109  int i, n, prev, next, window_center;
3110  f->channel_buffer_start = f->channel_buffer_end = 0;
3111 
3112  retry:
3113  if (f->eof) return FALSE;
3114  if (!maybe_start_packet(f))
3115  return FALSE;
3116  // check packet type
3117  if (get_bits(f,1) != 0) {
3118  if (IS_PUSH_MODE(f))
3119  return error(f,VORBIS_bad_packet_type);
3120  while (EOP != get8_packet(f));
3121  goto retry;
3122  }
3123 
3124  if (f->alloc.alloc_buffer)
3125  assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3126 
3127  i = get_bits(f, ilog(f->mode_count-1));
3128  if (i == EOP) return FALSE;
3129  if (i >= f->mode_count) return FALSE;
3130  *mode = i;
3131  m = f->mode_config + i;
3132  if (m->blockflag) {
3133  n = f->blocksize_1;
3134  prev = get_bits(f,1);
3135  next = get_bits(f,1);
3136  } else {
3137  prev = next = 0;
3138  n = f->blocksize_0;
3139  }
3140 
3141 // WINDOWING
3142 
3143  window_center = n >> 1;
3144  if (m->blockflag && !prev) {
3145  *p_left_start = (n - f->blocksize_0) >> 2;
3146  *p_left_end = (n + f->blocksize_0) >> 2;
3147  } else {
3148  *p_left_start = 0;
3149  *p_left_end = window_center;
3150  }
3151  if (m->blockflag && !next) {
3152  *p_right_start = (n*3 - f->blocksize_0) >> 2;
3153  *p_right_end = (n*3 + f->blocksize_0) >> 2;
3154  } else {
3155  *p_right_start = window_center;
3156  *p_right_end = n;
3157  }
3158 
3159  return TRUE;
3160 }
3161 
3162 static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left)
3163 {
3164  Mapping *map;
3165  int i,j,k,n,n2;
3166  int zero_channel[256];
3167  int really_zero_channel[256];
3168 
3169 // WINDOWING
3170 
3171  n = f->blocksize[m->blockflag];
3172  map = &f->mapping[m->mapping];
3173 
3174 // FLOORS
3175  n2 = n >> 1;
3176 
3177  CHECK(f);
3178 
3179  for (i=0; i < f->channels; ++i) {
3180  int s = map->chan[i].mux, floor;
3181  zero_channel[i] = FALSE;
3182  floor = map->submap_floor[s];
3183  if (f->floor_types[floor] == 0) {
3184  return error(f, VORBIS_invalid_stream);
3185  } else {
3186  Floor1 *g = &f->floor_config[floor].floor1;
3187  if (get_bits(f, 1)) {
3188  short *finalY;
3189  uint8 step2_flag[256];
3190  static int range_list[4] = { 256, 128, 86, 64 };
3191  int range = range_list[g->floor1_multiplier-1];
3192  int offset = 2;
3193  finalY = f->finalY[i];
3194  finalY[0] = get_bits(f, ilog(range)-1);
3195  finalY[1] = get_bits(f, ilog(range)-1);
3196  for (j=0; j < g->partitions; ++j) {
3197  int pclass = g->partition_class_list[j];
3198  int cdim = g->class_dimensions[pclass];
3199  int cbits = g->class_subclasses[pclass];
3200  int csub = (1 << cbits)-1;
3201  int cval = 0;
3202  if (cbits) {
3203  Codebook *c = f->codebooks + g->class_masterbooks[pclass];
3204  DECODE(cval,f,c);
3205  }
3206  for (k=0; k < cdim; ++k) {
3207  int book = g->subclass_books[pclass][cval & csub];
3208  cval = cval >> cbits;
3209  if (book >= 0) {
3210  int temp;
3211  Codebook *c = f->codebooks + book;
3212  DECODE(temp,f,c);
3213  finalY[offset++] = temp;
3214  } else
3215  finalY[offset++] = 0;
3216  }
3217  }
3218  if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec
3219  step2_flag[0] = step2_flag[1] = 1;
3220  for (j=2; j < g->values; ++j) {
3221  int low, high, pred, highroom, lowroom, room, val;
3222  low = g->neighbors[j][0];
3223  high = g->neighbors[j][1];
3224  //neighbors(g->Xlist, j, &low, &high);
3225  pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]);
3226  val = finalY[j];
3227  highroom = range - pred;
3228  lowroom = pred;
3229  if (highroom < lowroom)
3230  room = highroom * 2;
3231  else
3232  room = lowroom * 2;
3233  if (val) {
3234  step2_flag[low] = step2_flag[high] = 1;
3235  step2_flag[j] = 1;
3236  if (val >= room)
3237  if (highroom > lowroom)
3238  finalY[j] = val - lowroom + pred;
3239  else
3240  finalY[j] = pred - val + highroom - 1;
3241  else
3242  if (val & 1)
3243  finalY[j] = pred - ((val+1)>>1);
3244  else
3245  finalY[j] = pred + (val>>1);
3246  } else {
3247  step2_flag[j] = 0;
3248  finalY[j] = pred;
3249  }
3250  }
3251 
3252 #ifdef STB_VORBIS_NO_DEFER_FLOOR
3253  do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
3254 #else
3255  // defer final floor computation until _after_ residue
3256  for (j=0; j < g->values; ++j) {
3257  if (!step2_flag[j])
3258  finalY[j] = -1;
3259  }
3260 #endif
3261  } else {
3262  error:
3263  zero_channel[i] = TRUE;
3264  }
3265  // So we just defer everything else to later
3266 
3267  // at this point we've decoded the floor into buffer
3268  }
3269  }
3270  CHECK(f);
3271  // at this point we've decoded all floors
3272 
3273  if (f->alloc.alloc_buffer)
3274  assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3275 
3276  // re-enable coupled channels if necessary
3277  memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels);
3278  for (i=0; i < map->coupling_steps; ++i)
3279  if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) {
3280  zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE;
3281  }
3282 
3283  CHECK(f);
3284 // RESIDUE DECODE
3285  for (i=0; i < map->submaps; ++i) {
3286  float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
3287  int r;
3288  uint8 do_not_decode[256];
3289  int ch = 0;
3290  for (j=0; j < f->channels; ++j) {
3291  if (map->chan[j].mux == i) {
3292  if (zero_channel[j]) {
3293  do_not_decode[ch] = TRUE;
3294  residue_buffers[ch] = NULL;
3295  } else {
3296  do_not_decode[ch] = FALSE;
3297  residue_buffers[ch] = f->channel_buffers[j];
3298  }
3299  ++ch;
3300  }
3301  }
3302  r = map->submap_residue[i];
3303  decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
3304  }
3305 
3306  if (f->alloc.alloc_buffer)
3307  assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3308  CHECK(f);
3309 
3310 // INVERSE COUPLING
3311  for (i = map->coupling_steps-1; i >= 0; --i) {
3312  int n2 = n >> 1;
3313  float *m = f->channel_buffers[map->chan[i].magnitude];
3314  float *a = f->channel_buffers[map->chan[i].angle ];
3315  for (j=0; j < n2; ++j) {
3316  float a2,m2;
3317  if (m[j] > 0)
3318  if (a[j] > 0)
3319  m2 = m[j], a2 = m[j] - a[j];
3320  else
3321  a2 = m[j], m2 = m[j] + a[j];
3322  else
3323  if (a[j] > 0)
3324  m2 = m[j], a2 = m[j] + a[j];
3325  else
3326  a2 = m[j], m2 = m[j] - a[j];
3327  m[j] = m2;
3328  a[j] = a2;
3329  }
3330  }
3331  CHECK(f);
3332 
3333  // finish decoding the floors
3334 #ifndef STB_VORBIS_NO_DEFER_FLOOR
3335  for (i=0; i < f->channels; ++i) {
3336  if (really_zero_channel[i]) {
3337  memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
3338  } else {
3339  do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL);
3340  }
3341  }
3342 #else
3343  for (i=0; i < f->channels; ++i) {
3344  if (really_zero_channel[i]) {
3345  memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
3346  } else {
3347  for (j=0; j < n2; ++j)
3348  f->channel_buffers[i][j] *= f->floor_buffers[i][j];
3349  }
3350  }
3351 #endif
3352 
3353 // INVERSE MDCT
3354  CHECK(f);
3355  for (i=0; i < f->channels; ++i)
3356  inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
3357  CHECK(f);
3358 
3359  // this shouldn't be necessary, unless we exited on an error
3360  // and want to flush to get to the next packet
3361  flush_packet(f);
3362 
3363  if (f->first_decode) {
3364  // assume we start so first non-discarded sample is sample 0
3365  // this isn't to spec, but spec would require us to read ahead
3366  // and decode the size of all current frames--could be done,
3367  // but presumably it's not a commonly used feature
3368  f->current_loc = -n2; // start of first frame is positioned for discard
3369  // we might have to discard samples "from" the next frame too,
3370  // if we're lapping a large block then a small at the start?
3371  f->discard_samples_deferred = n - right_end;
3372  f->current_loc_valid = TRUE;
3373  f->first_decode = FALSE;
3374  } else if (f->discard_samples_deferred) {
3375  if (f->discard_samples_deferred >= right_start - left_start) {
3376  f->discard_samples_deferred -= (right_start - left_start);
3377  left_start = right_start;
3378  *p_left = left_start;
3379  } else {
3380  left_start += f->discard_samples_deferred;
3381  *p_left = left_start;
3382  f->discard_samples_deferred = 0;
3383  }
3384  } else if (f->previous_length == 0 && f->current_loc_valid) {
3385  // we're recovering from a seek... that means we're going to discard
3386  // the samples from this packet even though we know our position from
3387  // the last page header, so we need to update the position based on
3388  // the discarded samples here
3389  // but wait, the code below is going to add this in itself even
3390  // on a discard, so we don't need to do it here...
3391  }
3392 
3393  // check if we have ogg information about the sample # for this packet
3394  if (f->last_seg_which == f->end_seg_with_known_loc) {
3395  // if we have a valid current loc, and this is final:
3396  if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) {
3397  uint32 current_end = f->known_loc_for_packet;
3398  // then let's infer the size of the (probably) short final frame
3399  if (current_end < f->current_loc + (right_end-left_start)) {
3400  if (current_end < f->current_loc) {
3401  // negative truncation, that's impossible!
3402  *len = 0;
3403  } else {
3404  *len = current_end - f->current_loc;
3405  }
3406  *len += left_start; // this doesn't seem right, but has no ill effect on my test files
3407  if (*len > right_end) *len = right_end; // this should never happen
3408  f->current_loc += *len;
3409  return TRUE;
3410  }
3411  }
3412  // otherwise, just set our sample loc
3413  // guess that the ogg granule pos refers to the _middle_ of the
3414  // last frame?
3415  // set f->current_loc to the position of left_start
3416  f->current_loc = f->known_loc_for_packet - (n2-left_start);
3417  f->current_loc_valid = TRUE;
3418  }
3419  if (f->current_loc_valid)
3420  f->current_loc += (right_start - left_start);
3421 
3422  if (f->alloc.alloc_buffer)
3423  assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3424  *len = right_end; // ignore samples after the window goes to 0
3425  CHECK(f);
3426 
3427  return TRUE;
3428 }
3429 
3430 static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right)
3431 {
3432  int mode, left_end, right_end;
3433  if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0;
3434  return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left);
3435 }
3436 
3437 static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
3438 {
3439  int prev,i,j;
3440  // we use right&left (the start of the right- and left-window sin()-regions)
3441  // to determine how much to return, rather than inferring from the rules
3442  // (same result, clearer code); 'left' indicates where our sin() window
3443  // starts, therefore where the previous window's right edge starts, and
3444  // therefore where to start mixing from the previous buffer. 'right'
3445  // indicates where our sin() ending-window starts, therefore that's where
3446  // we start saving, and where our returned-data ends.
3447 
3448  // mixin from previous window
3449  if (f->previous_length) {
3450  int i,j, n = f->previous_length;
3451  float *w = get_window(f, n);
3452  if (w == NULL) return 0;
3453  for (i=0; i < f->channels; ++i) {
3454  for (j=0; j < n; ++j)
3455  f->channel_buffers[i][left+j] =
3456  f->channel_buffers[i][left+j]*w[ j] +
3457  f->previous_window[i][ j]*w[n-1-j];
3458  }
3459  }
3460 
3461  prev = f->previous_length;
3462 
3463  // last half of this data becomes previous window
3464  f->previous_length = len - right;
3465 
3466  // @OPTIMIZE: could avoid this copy by double-buffering the
3467  // output (flipping previous_window with channel_buffers), but
3468  // then previous_window would have to be 2x as large, and
3469  // channel_buffers couldn't be temp mem (although they're NOT
3470  // currently temp mem, they could be (unless we want to level
3471  // performance by spreading out the computation))
3472  for (i=0; i < f->channels; ++i)
3473  for (j=0; right+j < len; ++j)
3474  f->previous_window[i][j] = f->channel_buffers[i][right+j];
3475 
3476  if (!prev)
3477  // there was no previous packet, so this data isn't valid...
3478  // this isn't entirely true, only the would-have-overlapped data
3479  // isn't valid, but this seems to be what the spec requires
3480  return 0;
3481 
3482  // truncate a short frame
3483  if (len < right) right = len;
3484 
3485  f->samples_output += right-left;
3486 
3487  return right - left;
3488 }
3489 
3491 {
3492  int len, right, left, res;
3493  res = vorbis_decode_packet(f, &len, &left, &right);
3494  if (res)
3495  vorbis_finish_frame(f, len, left, right);
3496  return res;
3497 }
3498 
3499 #ifndef STB_VORBIS_NO_PUSHDATA_API
3501 {
3502  // make sure that we have the packet available before continuing...
3503  // this requires a full ogg parse, but we know we can fetch from f->stream
3504 
3505  // instead of coding this out explicitly, we could save the current read state,
3506  // read the next packet with get8() until end-of-packet, check f->eof, then
3507  // reset the state? but that would be slower, esp. since we'd have over 256 bytes
3508  // of state to restore (primarily the page segment table)
3509 
3510  int s = f->next_seg, first = TRUE;
3511  uint8 *p = f->stream;
3512 
3513  if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag
3514  for (; s < f->segment_count; ++s) {
3515  p += f->segments[s];
3516  if (f->segments[s] < 255) // stop at first short segment
3517  break;
3518  }
3519  // either this continues, or it ends it...
3520  if (s == f->segment_count)
3521  s = -1; // set 'crosses page' flag
3522  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
3523  first = FALSE;
3524  }
3525  for (; s == -1;) {
3526  uint8 *q;
3527  int n;
3528 
3529  // check that we have the page header ready
3530  if (p + 26 >= f->stream_end) return error(f, VORBIS_need_more_data);
3531  // validate the page
3532  if (memcmp(p, ogg_page_header, 4)) return error(f, VORBIS_invalid_stream);
3533  if (p[4] != 0) return error(f, VORBIS_invalid_stream);
3534  if (first) { // the first segment must NOT have 'continued_packet', later ones MUST
3535  if (f->previous_length)
3536  if ((p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream);
3537  // if no previous length, we're resynching, so we can come in on a continued-packet,
3538  // which we'll just drop
3539  } else {
3540  if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream);
3541  }
3542  n = p[26]; // segment counts
3543  q = p+27; // q points to segment table
3544  p = q + n; // advance past header
3545  // make sure we've read the segment table
3546  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
3547  for (s=0; s < n; ++s) {
3548  p += q[s];
3549  if (q[s] < 255)
3550  break;
3551  }
3552  if (s == n)
3553  s = -1; // set 'crosses page' flag
3554  if (p > f->stream_end) return error(f, VORBIS_need_more_data);
3555  first = FALSE;
3556  }
3557  return TRUE;
3558 }
3559 #endif // !STB_VORBIS_NO_PUSHDATA_API
3560 
3561 static int start_decoder(vorb *f)
3562 {
3563  uint8 header[6], x,y;
3564  int len,i,j,k, max_submaps = 0;
3565  int longest_floorlist=0;
3566 
3567  // first page, first packet
3568  f->first_decode = TRUE;
3569 
3570  if (!start_page(f)) return FALSE;
3571  // validate page flag
3572  if (!(f->page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page);
3573  if (f->page_flag & PAGEFLAG_last_page) return error(f, VORBIS_invalid_first_page);
3574  if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page);
3575  // check for expected packet length
3576  if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page);
3577  if (f->segments[0] != 30) {
3578  // check for the Ogg skeleton fishead identifying header to refine our error
3579  if (f->segments[0] == 64 &&
3580  getn(f, header, 6) &&
3581  header[0] == 'f' &&
3582  header[1] == 'i' &&
3583  header[2] == 's' &&
3584  header[3] == 'h' &&
3585  header[4] == 'e' &&
3586  header[5] == 'a' &&
3587  get8(f) == 'd' &&
3588  get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported);
3589  else
3591  }
3592 
3593  // read packet
3594  // check packet header
3596  if (!getn(f, header, 6)) return error(f, VORBIS_unexpected_eof);
3598  // vorbis_version
3599  if (get32(f) != 0) return error(f, VORBIS_invalid_first_page);
3600  f->channels = get8(f); if (!f->channels) return error(f, VORBIS_invalid_first_page);
3601  if (f->channels > STB_VORBIS_MAX_CHANNELS) return error(f, VORBIS_too_many_channels);
3602  f->sample_rate = get32(f); if (!f->sample_rate) return error(f, VORBIS_invalid_first_page);
3603  get32(f); // bitrate_maximum
3604  get32(f); // bitrate_nominal
3605  get32(f); // bitrate_minimum
3606  x = get8(f);
3607  {
3608  int log0,log1;
3609  log0 = x & 15;
3610  log1 = x >> 4;
3611  f->blocksize_0 = 1 << log0;
3612  f->blocksize_1 = 1 << log1;
3613  if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup);
3614  if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup);
3615  if (log0 > log1) return error(f, VORBIS_invalid_setup);
3616  }
3617 
3618  // framing_flag
3619  x = get8(f);
3620  if (!(x & 1)) return error(f, VORBIS_invalid_first_page);
3621 
3622  // second packet!
3623  if (!start_page(f)) return FALSE;
3624 
3625  if (!start_packet(f)) return FALSE;
3626 
3627  if (!next_segment(f)) return FALSE;
3628 
3630  for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3632  //file vendor
3633  len = get32_packet(f);
3634  f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1));
3635  for(i=0; i < len; ++i) {
3636  f->vendor[i] = get8_packet(f);
3637  }
3638  f->vendor[len] = (char)'\0';
3639  //user comments
3640  f->comment_list_length = get32_packet(f);
3641  f->comment_list = (char**)setup_malloc(f, sizeof(char*) * (f->comment_list_length));
3642 
3643  for(i=0; i < f->comment_list_length; ++i) {
3644  len = get32_packet(f);
3645  f->comment_list[i] = (char*)setup_malloc(f, sizeof(char) * (len+1));
3646 
3647  for(j=0; j < len; ++j) {
3648  f->comment_list[i][j] = get8_packet(f);
3649  }
3650  f->comment_list[i][len] = (char)'\0';
3651  }
3652 
3653  // framing_flag
3654  x = get8_packet(f);
3655  if (!(x & 1)) return error(f, VORBIS_invalid_setup);
3656 
3657 
3658  skip(f, f->bytes_in_seg);
3659  f->bytes_in_seg = 0;
3660 
3661  do {
3662  len = next_segment(f);
3663  skip(f, len);
3664  f->bytes_in_seg = 0;
3665  } while (len);
3666 
3667  // third packet!
3668  if (!start_packet(f)) return FALSE;
3669 
3670  #ifndef STB_VORBIS_NO_PUSHDATA_API
3671  if (IS_PUSH_MODE(f)) {
3672  if (!is_whole_packet_present(f)) {
3673  // convert error in ogg header to write type
3674  if (f->error == VORBIS_invalid_stream)
3675  f->error = VORBIS_invalid_setup;
3676  return FALSE;
3677  }
3678  }
3679  #endif
3680 
3681  crc32_init(); // always init it, to avoid multithread race conditions
3682 
3684  for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3686 
3687  // codebooks
3688 
3689  f->codebook_count = get_bits(f,8) + 1;
3690  f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
3691  if (f->codebooks == NULL) return error(f, VORBIS_outofmem);
3692  memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
3693  for (i=0; i < f->codebook_count; ++i) {
3694  uint32 *values;
3695  int ordered, sorted_count;
3696  int total=0;
3697  uint8 *lengths;
3698  Codebook *c = f->codebooks+i;
3699  CHECK(f);
3700  x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup);
3701  x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup);
3702  x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup);
3703  x = get_bits(f, 8);
3704  c->dimensions = (get_bits(f, 8)<<8) + x;
3705  x = get_bits(f, 8);
3706  y = get_bits(f, 8);
3707  c->entries = (get_bits(f, 8)<<16) + (y<<8) + x;
3708  ordered = get_bits(f,1);
3709  c->sparse = ordered ? 0 : get_bits(f,1);
3710 
3711  if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup);
3712 
3713  if (c->sparse)
3714  lengths = (uint8 *) setup_temp_malloc(f, c->entries);
3715  else
3716  lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
3717 
3718  if (!lengths) return error(f, VORBIS_outofmem);
3719 
3720  if (ordered) {
3721  int current_entry = 0;
3722  int current_length = get_bits(f,5) + 1;
3723  while (current_entry < c->entries) {
3724  int limit = c->entries - current_entry;
3725  int n = get_bits(f, ilog(limit));
3726  if (current_length >= 32) return error(f, VORBIS_invalid_setup);
3727  if (current_entry + n > (int) c->entries) { return error(f, VORBIS_invalid_setup); }
3728  memset(lengths + current_entry, current_length, n);
3729  current_entry += n;
3730  ++current_length;
3731  }
3732  } else {
3733  for (j=0; j < c->entries; ++j) {
3734  int present = c->sparse ? get_bits(f,1) : 1;
3735  if (present) {
3736  lengths[j] = get_bits(f, 5) + 1;
3737  ++total;
3738  if (lengths[j] == 32)
3739  return error(f, VORBIS_invalid_setup);
3740  } else {
3741  lengths[j] = NO_CODE;
3742  }
3743  }
3744  }
3745 
3746  if (c->sparse && total >= c->entries >> 2) {
3747  // convert sparse items to non-sparse!
3748  if (c->entries > (int) f->setup_temp_memory_required)
3749  f->setup_temp_memory_required = c->entries;
3750 
3752  if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem);
3753  memcpy(c->codeword_lengths, lengths, c->entries);
3754  setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
3755  lengths = c->codeword_lengths;
3756  c->sparse = 0;
3757  }
3758 
3759  // compute the size of the sorted tables
3760  if (c->sparse) {
3761  sorted_count = total;
3762  } else {
3763  sorted_count = 0;
3764  #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
3765  for (j=0; j < c->entries; ++j)
3766  if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
3767  ++sorted_count;
3768  #endif
3769  }
3770 
3771  c->sorted_entries = sorted_count;
3772  values = NULL;
3773 
3774  CHECK(f);
3775  if (!c->sparse) {
3776  c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries);
3777  if (!c->codewords) return error(f, VORBIS_outofmem);
3778  } else {
3779  unsigned int size;
3780  if (c->sorted_entries) {
3782  if (!c->codeword_lengths) return error(f, VORBIS_outofmem);
3783  c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
3784  if (!c->codewords) return error(f, VORBIS_outofmem);
3785  values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
3786  if (!values) return error(f, VORBIS_outofmem);
3787  }
3788  size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
3789  if (size > f->setup_temp_memory_required)
3790  f->setup_temp_memory_required = size;
3791  }
3792 
3793  if (!compute_codewords(c, lengths, c->entries, values)) {
3794  if (c->sparse) setup_temp_free(f, values, 0);
3795  return error(f, VORBIS_invalid_setup);
3796  }
3797 
3798  if (c->sorted_entries) {
3799  // allocate an extra slot for sentinels
3800  c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries+1));
3801  if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem);
3802  // allocate an extra slot at the front so that c->sorted_values[-1] is defined
3803  // so that we can catch that case without an extra if
3804  c->sorted_values = ( int *) setup_malloc(f, sizeof(*c->sorted_values ) * (c->sorted_entries+1));
3805  if (c->sorted_values == NULL) return error(f, VORBIS_outofmem);
3806  ++c->sorted_values;
3807  c->sorted_values[-1] = -1;
3808  compute_sorted_huffman(c, lengths, values);
3809  }
3810 
3811  if (c->sparse) {
3812  setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
3813  setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
3814  setup_temp_free(f, lengths, c->entries);
3815  c->codewords = NULL;
3816  }
3817 
3819 
3820  CHECK(f);
3821  c->lookup_type = get_bits(f, 4);
3822  if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup);
3823  if (c->lookup_type > 0) {
3824  uint16 *mults;
3826  c->delta_value = float32_unpack(get_bits(f, 32));
3827  c->value_bits = get_bits(f, 4)+1;
3828  c->sequence_p = get_bits(f,1);
3829  if (c->lookup_type == 1) {
3830  int values = lookup1_values(c->entries, c->dimensions);
3831  if (values < 0) return error(f, VORBIS_invalid_setup);
3832  c->lookup_values = (uint32) values;
3833  } else {
3834  c->lookup_values = c->entries * c->dimensions;
3835  }
3836  if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup);
3837  mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
3838  if (mults == NULL) return error(f, VORBIS_outofmem);
3839  for (j=0; j < (int) c->lookup_values; ++j) {
3840  int q = get_bits(f, c->value_bits);
3841  if (q == EOP) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); }
3842  mults[j] = q;
3843  }
3844 
3845 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3846  if (c->lookup_type == 1) {
3847  int len, sparse = c->sparse;
3848  float last=0;
3849  // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
3850  if (sparse) {
3851  if (c->sorted_entries == 0) goto skip;
3852  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
3853  } else
3854  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions);
3855  if (c->multiplicands == NULL) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
3856  len = sparse ? c->sorted_entries : c->entries;
3857  for (j=0; j < len; ++j) {
3858  unsigned int z = sparse ? c->sorted_values[j] : j;
3859  unsigned int div=1;
3860  for (k=0; k < c->dimensions; ++k) {
3861  int off = (z / div) % c->lookup_values;
3862  float val = mults[off];
3863  val = mults[off]*c->delta_value + c->minimum_value + last;
3864  c->multiplicands[j*c->dimensions + k] = val;
3865  if (c->sequence_p)
3866  last = val;
3867  if (k+1 < c->dimensions) {
3868  if (div > UINT_MAX / (unsigned int) c->lookup_values) {
3869  setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
3870  return error(f, VORBIS_invalid_setup);
3871  }
3872  div *= c->lookup_values;
3873  }
3874  }
3875  }
3876  c->lookup_type = 2;
3877  }
3878  else
3879 #endif
3880  {
3881  float last=0;
3882  CHECK(f);
3883  c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
3884  if (c->multiplicands == NULL) { setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
3885  for (j=0; j < (int) c->lookup_values; ++j) {
3886  float val = mults[j] * c->delta_value + c->minimum_value + last;
3887  c->multiplicands[j] = val;
3888  if (c->sequence_p)
3889  last = val;
3890  }
3891  }
3892 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3893  skip:;
3894 #endif
3895  setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
3896 
3897  CHECK(f);
3898  }
3899  CHECK(f);
3900  }
3901 
3902  // time domain transfers (notused)
3903 
3904  x = get_bits(f, 6) + 1;
3905  for (i=0; i < x; ++i) {
3906  uint32 z = get_bits(f, 16);
3907  if (z != 0) return error(f, VORBIS_invalid_setup);
3908  }
3909 
3910  // Floors
3911  f->floor_count = get_bits(f, 6)+1;
3912  f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
3913  if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
3914  for (i=0; i < f->floor_count; ++i) {
3915  f->floor_types[i] = get_bits(f, 16);
3916  if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup);
3917  if (f->floor_types[i] == 0) {
3918  Floor0 *g = &f->floor_config[i].floor0;
3919  g->order = get_bits(f,8);
3920  g->rate = get_bits(f,16);
3921  g->bark_map_size = get_bits(f,16);
3922  g->amplitude_bits = get_bits(f,6);
3923  g->amplitude_offset = get_bits(f,8);
3924  g->number_of_books = get_bits(f,4) + 1;
3925  for (j=0; j < g->number_of_books; ++j)
3926  g->book_list[j] = get_bits(f,8);
3928  } else {
3929  stbv__floor_ordering p[31*8+2];
3930  Floor1 *g = &f->floor_config[i].floor1;
3931  int max_class = -1;
3932  g->partitions = get_bits(f, 5);
3933  for (j=0; j < g->partitions; ++j) {
3934  g->partition_class_list[j] = get_bits(f, 4);
3935  if (g->partition_class_list[j] > max_class)
3936  max_class = g->partition_class_list[j];
3937  }
3938  for (j=0; j <= max_class; ++j) {
3939  g->class_dimensions[j] = get_bits(f, 3)+1;
3940  g->class_subclasses[j] = get_bits(f, 2);
3941  if (g->class_subclasses[j]) {
3942  g->class_masterbooks[j] = get_bits(f, 8);
3943  if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
3944  }
3945  for (k=0; k < 1 << g->class_subclasses[j]; ++k) {
3946  g->subclass_books[j][k] = get_bits(f,8)-1;
3947  if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
3948  }
3949  }
3950  g->floor1_multiplier = get_bits(f,2)+1;
3951  g->rangebits = get_bits(f,4);
3952  g->Xlist[0] = 0;
3953  g->Xlist[1] = 1 << g->rangebits;
3954  g->values = 2;
3955  for (j=0; j < g->partitions; ++j) {
3956  int c = g->partition_class_list[j];
3957  for (k=0; k < g->class_dimensions[c]; ++k) {
3958  g->Xlist[g->values] = get_bits(f, g->rangebits);
3959  ++g->values;
3960  }
3961  }
3962  // precompute the sorting
3963  for (j=0; j < g->values; ++j) {
3964  p[j].x = g->Xlist[j];
3965  p[j].id = j;
3966  }
3967  qsort(p, g->values, sizeof(p[0]), point_compare);
3968  for (j=0; j < g->values-1; ++j)
3969  if (p[j].x == p[j+1].x)
3970  return error(f, VORBIS_invalid_setup);
3971  for (j=0; j < g->values; ++j)
3972  g->sorted_order[j] = (uint8) p[j].id;
3973  // precompute the neighbors
3974  for (j=2; j < g->values; ++j) {
3975  int low = 0,hi = 0;
3976  neighbors(g->Xlist, j, &low,&hi);
3977  g->neighbors[j][0] = low;
3978  g->neighbors[j][1] = hi;
3979  }
3980 
3981  if (g->values > longest_floorlist)
3982  longest_floorlist = g->values;
3983  }
3984  }
3985 
3986  // Residue
3987  f->residue_count = get_bits(f, 6)+1;
3988  f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
3989  if (f->residue_config == NULL) return error(f, VORBIS_outofmem);
3990  memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
3991  for (i=0; i < f->residue_count; ++i) {
3992  uint8 residue_cascade[64];
3993  Residue *r = f->residue_config+i;
3994  f->residue_types[i] = get_bits(f, 16);
3995  if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup);
3996  r->begin = get_bits(f, 24);
3997  r->end = get_bits(f, 24);
3998  if (r->end < r->begin) return error(f, VORBIS_invalid_setup);
3999  r->part_size = get_bits(f,24)+1;
4000  r->classifications = get_bits(f,6)+1;
4001  r->classbook = get_bits(f,8);
4002  if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4003  for (j=0; j < r->classifications; ++j) {
4004  uint8 high_bits=0;
4005  uint8 low_bits=get_bits(f,3);
4006  if (get_bits(f,1))
4007  high_bits = get_bits(f,5);
4008  residue_cascade[j] = high_bits*8 + low_bits;
4009  }
4010  r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
4011  if (r->residue_books == NULL) return error(f, VORBIS_outofmem);
4012  for (j=0; j < r->classifications; ++j) {
4013  for (k=0; k < 8; ++k) {
4014  if (residue_cascade[j] & (1 << k)) {
4015  r->residue_books[j][k] = get_bits(f, 8);
4016  if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
4017  } else {
4018  r->residue_books[j][k] = -1;
4019  }
4020  }
4021  }
4022  // precompute the classifications[] array to avoid inner-loop mod/divide
4023  // call it 'classdata' since we already have r->classifications
4024  r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
4025  if (!r->classdata) return error(f, VORBIS_outofmem);
4026  memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
4027  for (j=0; j < f->codebooks[r->classbook].entries; ++j) {
4028  int classwords = f->codebooks[r->classbook].dimensions;
4029  int temp = j;
4030  r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords);
4031  if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem);
4032  for (k=classwords-1; k >= 0; --k) {
4033  r->classdata[j][k] = temp % r->classifications;
4034  temp /= r->classifications;
4035  }
4036  }
4037  }
4038 
4039  f->mapping_count = get_bits(f,6)+1;
4040  f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
4041  if (f->mapping == NULL) return error(f, VORBIS_outofmem);
4042  memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
4043  for (i=0; i < f->mapping_count; ++i) {
4044  Mapping *m = f->mapping + i;
4045  int mapping_type = get_bits(f,16);
4046  if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
4047  m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
4048  if (m->chan == NULL) return error(f, VORBIS_outofmem);
4049  if (get_bits(f,1))
4050  m->submaps = get_bits(f,4)+1;
4051  else
4052  m->submaps = 1;
4053  if (m->submaps > max_submaps)
4054  max_submaps = m->submaps;
4055  if (get_bits(f,1)) {
4056  m->coupling_steps = get_bits(f,8)+1;
4057  if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup);
4058  for (k=0; k < m->coupling_steps; ++k) {
4059  m->chan[k].magnitude = get_bits(f, ilog(f->channels-1));
4060  m->chan[k].angle = get_bits(f, ilog(f->channels-1));
4061  if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup);
4062  if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup);
4063  if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup);
4064  }
4065  } else
4066  m->coupling_steps = 0;
4067 
4068  // reserved field
4069  if (get_bits(f,2)) return error(f, VORBIS_invalid_setup);
4070  if (m->submaps > 1) {
4071  for (j=0; j < f->channels; ++j) {
4072  m->chan[j].mux = get_bits(f, 4);
4073  if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup);
4074  }
4075  } else
4076  // @SPECIFICATION: this case is missing from the spec
4077  for (j=0; j < f->channels; ++j)
4078  m->chan[j].mux = 0;
4079 
4080  for (j=0; j < m->submaps; ++j) {
4081  get_bits(f,8); // discard
4082  m->submap_floor[j] = get_bits(f,8);
4083  m->submap_residue[j] = get_bits(f,8);
4084  if (m->submap_floor[j] >= f->floor_count) return error(f, VORBIS_invalid_setup);
4085  if (m->submap_residue[j] >= f->residue_count) return error(f, VORBIS_invalid_setup);
4086  }
4087  }
4088 
4089  // Modes
4090  f->mode_count = get_bits(f, 6)+1;
4091  for (i=0; i < f->mode_count; ++i) {
4092  Mode *m = f->mode_config+i;
4093  m->blockflag = get_bits(f,1);
4094  m->windowtype = get_bits(f,16);
4095  m->transformtype = get_bits(f,16);
4096  m->mapping = get_bits(f,8);
4097  if (m->windowtype != 0) return error(f, VORBIS_invalid_setup);
4098  if (m->transformtype != 0) return error(f, VORBIS_invalid_setup);
4099  if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup);
4100  }
4101 
4102  flush_packet(f);
4103 
4104  f->previous_length = 0;
4105 
4106  for (i=0; i < f->channels; ++i) {
4107  f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1);
4108  f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2);
4109  f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist);
4110  if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem);
4111  memset(f->channel_buffers[i], 0, sizeof(float) * f->blocksize_1);
4112  #ifdef STB_VORBIS_NO_DEFER_FLOOR
4113  f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2);
4114  if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem);
4115  #endif
4116  }
4117 
4118  if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE;
4119  if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE;
4120  f->blocksize[0] = f->blocksize_0;
4121  f->blocksize[1] = f->blocksize_1;
4122 
4123 #ifdef STB_VORBIS_DIVIDE_TABLE
4124  if (integer_divide_table[1][1]==0)
4125  for (i=0; i < DIVTAB_NUMER; ++i)
4126  for (j=1; j < DIVTAB_DENOM; ++j)
4127  integer_divide_table[i][j] = i / j;
4128 #endif
4129 
4130  // compute how much temporary memory is needed
4131 
4132  // 1.
4133  {
4134  uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1);
4135  uint32 classify_mem;
4136  int i,max_part_read=0;
4137  for (i=0; i < f->residue_count; ++i) {
4138  Residue *r = f->residue_config + i;
4139  unsigned int actual_size = f->blocksize_1 / 2;
4140  unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size;
4141  unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size;
4142  int n_read = limit_r_end - limit_r_begin;
4143  int part_read = n_read / r->part_size;
4144  if (part_read > max_part_read)
4145  max_part_read = part_read;
4146  }
4147  #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
4148  classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *));
4149  #else
4150  classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *));
4151  #endif
4152 
4153  // maximum reasonable partition size is f->blocksize_1
4154 
4155  f->temp_memory_required = classify_mem;
4156  if (imdct_mem > f->temp_memory_required)
4157  f->temp_memory_required = imdct_mem;
4158  }
4159 
4160 
4161  if (f->alloc.alloc_buffer) {
4162  assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
4163  // check if there's enough temp memory so we don't error later
4164  if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)
4165  return error(f, VORBIS_outofmem);
4166  }
4167 
4168  // @TODO: stb_vorbis_seek_start expects first_audio_page_offset to point to a page
4169  // without PAGEFLAG_continued_packet, so this either points to the first page, or
4170  // the page after the end of the headers. It might be cleaner to point to a page
4171  // in the middle of the headers, when that's the page where the first audio packet
4172  // starts, but we'd have to also correctly skip the end of any continued packet in
4173  // stb_vorbis_seek_start.
4174  if (f->next_seg == -1) {
4175  f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
4176  } else {
4177  f->first_audio_page_offset = 0;
4178  }
4179 
4180  return TRUE;
4181 }
4182 
4183 static void vorbis_deinit(stb_vorbis *p)
4184 {
4185  int i,j;
4186 
4187  setup_free(p, p->vendor);
4188  for (i=0; i < p->comment_list_length; ++i) {
4189  setup_free(p, p->comment_list[i]);
4190  }
4191  setup_free(p, p->comment_list);
4192 
4193  if (p->residue_config) {
4194  for (i=0; i < p->residue_count; ++i) {
4195  Residue *r = p->residue_config+i;
4196  if (r->classdata) {
4197  for (j=0; j < p->codebooks[r->classbook].entries; ++j)
4198  setup_free(p, r->classdata[j]);
4199  setup_free(p, r->classdata);
4200  }
4201  setup_free(p, r->residue_books);
4202  }
4203  }
4204 
4205  if (p->codebooks) {
4206  CHECK(p);
4207  for (i=0; i < p->codebook_count; ++i) {
4208  Codebook *c = p->codebooks + i;
4210  setup_free(p, c->multiplicands);
4211  setup_free(p, c->codewords);
4213  // c->sorted_values[-1] is the first entry in the array
4214  setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL);
4215  }
4216  setup_free(p, p->codebooks);
4217  }
4218  setup_free(p, p->floor_config);
4219  setup_free(p, p->residue_config);
4220  if (p->mapping) {
4221  for (i=0; i < p->mapping_count; ++i)
4222  setup_free(p, p->mapping[i].chan);
4223  setup_free(p, p->mapping);
4224  }
4225  CHECK(p);
4226  for (i=0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) {
4227  setup_free(p, p->channel_buffers[i]);
4228  setup_free(p, p->previous_window[i]);
4229  #ifdef STB_VORBIS_NO_DEFER_FLOOR
4230  setup_free(p, p->floor_buffers[i]);
4231  #endif
4232  setup_free(p, p->finalY[i]);
4233  }
4234  for (i=0; i < 2; ++i) {
4235  setup_free(p, p->A[i]);
4236  setup_free(p, p->B[i]);
4237  setup_free(p, p->C[i]);
4238  setup_free(p, p->window[i]);
4239  setup_free(p, p->bit_reverse[i]);
4240  }
4241  #ifndef STB_VORBIS_NO_STDIO
4242  if (p->close_on_free) fclose(p->f);
4243  #endif
4244 }
4245 
4247 {
4248  if (p == NULL) return;
4249  vorbis_deinit(p);
4250  setup_free(p,p);
4251 }
4252 
4253 static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
4254 {
4255  memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
4256  if (z) {
4257  p->alloc = *z;
4260  }
4261  p->eof = 0;
4262  p->error = VORBIS__no_error;
4263  p->stream = NULL;
4264  p->codebooks = NULL;
4265  p->page_crc_tests = -1;
4266  #ifndef STB_VORBIS_NO_STDIO
4267  p->close_on_free = FALSE;
4268  p->f = NULL;
4269  #endif
4270 }
4271 
4273 {
4274  if (f->current_loc_valid)
4275  return f->current_loc;
4276  else
4277  return -1;
4278 }
4279 
4281 {
4283  d.channels = f->channels;
4284  d.sample_rate = f->sample_rate;
4285  d.setup_memory_required = f->setup_memory_required;
4286  d.setup_temp_memory_required = f->setup_temp_memory_required;
4287  d.temp_memory_required = f->temp_memory_required;
4288  d.max_frame_size = f->blocksize_1 >> 1;
4289  return d;
4290 }
4291 
4293 {
4295  d.vendor = f->vendor;
4296  d.comment_list_length = f->comment_list_length;
4297  d.comment_list = f->comment_list;
4298  return d;
4299 }
4300 
4302 {
4303  int e = f->error;
4304  f->error = VORBIS__no_error;
4305  return e;
4306 }
4307 
4309 {
4310  stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p));
4311  return p;
4312 }
4313 
4314 #ifndef STB_VORBIS_NO_PUSHDATA_API
4315 
4317 {
4318  f->previous_length = 0;
4319  f->page_crc_tests = 0;
4320  f->discard_samples_deferred = 0;
4321  f->current_loc_valid = FALSE;
4322  f->first_decode = FALSE;
4323  f->samples_output = 0;
4324  f->channel_buffer_start = 0;
4325  f->channel_buffer_end = 0;
4326 }
4327 
4328 static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len)
4329 {
4330  int i,n;
4331  for (i=0; i < f->page_crc_tests; ++i)
4332  f->scan[i].bytes_done = 0;
4333 
4334  // if we have room for more scans, search for them first, because
4335  // they may cause us to stop early if their header is incomplete
4336  if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) {
4337  if (data_len < 4) return 0;
4338  data_len -= 3; // need to look for 4-byte sequence, so don't miss
4339  // one that straddles a boundary
4340  for (i=0; i < data_len; ++i) {
4341  if (data[i] == 0x4f) {
4342  if (0==memcmp(data+i, ogg_page_header, 4)) {
4343  int j,len;
4344  uint32 crc;
4345  // make sure we have the whole page header
4346  if (i+26 >= data_len || i+27+data[i+26] >= data_len) {
4347  // only read up to this page start, so hopefully we'll
4348  // have the whole page header start next time
4349  data_len = i;
4350  break;
4351  }
4352  // ok, we have it all; compute the length of the page
4353  len = 27 + data[i+26];
4354  for (j=0; j < data[i+26]; ++j)
4355  len += data[i+27+j];
4356  // scan everything up to the embedded crc (which we must 0)
4357  crc = 0;
4358  for (j=0; j < 22; ++j)
4359  crc = crc32_update(crc, data[i+j]);
4360  // now process 4 0-bytes
4361  for ( ; j < 26; ++j)
4362  crc = crc32_update(crc, 0);
4363  // len is the total number of bytes we need to scan
4364  n = f->page_crc_tests++;
4365  f->scan[n].bytes_left = len-j;
4366  f->scan[n].crc_so_far = crc;
4367  f->scan[n].goal_crc = data[i+22] + (data[i+23] << 8) + (data[i+24]<<16) + (data[i+25]<<24);
4368  // if the last frame on a page is continued to the next, then
4369  // we can't recover the sample_loc immediately
4370  if (data[i+27+data[i+26]-1] == 255)
4371  f->scan[n].sample_loc = ~0;
4372  else
4373  f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24);
4374  f->scan[n].bytes_done = i+j;
4375  if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT)
4376  break;
4377  // keep going if we still have room for more
4378  }
4379  }
4380  }
4381  }
4382 
4383  for (i=0; i < f->page_crc_tests;) {
4384  uint32 crc;
4385  int j;
4386  int n = f->scan[i].bytes_done;
4387  int m = f->scan[i].bytes_left;
4388  if (m > data_len - n) m = data_len - n;
4389  // m is the bytes to scan in the current chunk
4390  crc = f->scan[i].crc_so_far;
4391  for (j=0; j < m; ++j)
4392  crc = crc32_update(crc, data[n+j]);
4393  f->scan[i].bytes_left -= m;
4394  f->scan[i].crc_so_far = crc;
4395  if (f->scan[i].bytes_left == 0) {
4396  // does it match?
4397  if (f->scan[i].crc_so_far == f->scan[i].goal_crc) {
4398  // Houston, we have page
4399  data_len = n+m; // consumption amount is wherever that scan ended
4400  f->page_crc_tests = -1; // drop out of page scan mode
4401  f->previous_length = 0; // decode-but-don't-output one frame
4402  f->next_seg = -1; // start a new page
4403  f->current_loc = f->scan[i].sample_loc; // set the current sample location
4404  // to the amount we'd have decoded had we decoded this page
4405  f->current_loc_valid = f->current_loc != ~0U;
4406  return data_len;
4407  }
4408  // delete entry
4409  f->scan[i] = f->scan[--f->page_crc_tests];
4410  } else {
4411  ++i;
4412  }
4413  }
4414 
4415  return data_len;
4416 }
4417 
4418 // return value: number of bytes we used
4420  stb_vorbis *f, // the file we're decoding
4421  const uint8 *data, int data_len, // the memory available for decoding
4422  int *channels, // place to write number of float * buffers
4423  float ***output, // place to write float ** array of float * buffers
4424  int *samples // place to write number of output samples
4425  )
4426 {
4427  int i;
4428  int len,right,left;
4429 
4431 
4432  if (f->page_crc_tests >= 0) {
4433  *samples = 0;
4434  return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len);
4435  }
4436 
4437  f->stream = (uint8 *) data;
4438  f->stream_end = (uint8 *) data + data_len;
4439  f->error = VORBIS__no_error;
4440 
4441  // check that we have the entire packet in memory
4442  if (!is_whole_packet_present(f)) {
4443  *samples = 0;
4444  return 0;
4445  }
4446 
4447  if (!vorbis_decode_packet(f, &len, &left, &right)) {
4448  // save the actual error we encountered
4449  enum STBVorbisError error = f->error;
4450  if (error == VORBIS_bad_packet_type) {
4451  // flush and resynch
4452  f->error = VORBIS__no_error;
4453  while (get8_packet(f) != EOP)
4454  if (f->eof) break;
4455  *samples = 0;
4456  return (int) (f->stream - data);
4457  }
4459  if (f->previous_length == 0) {
4460  // we may be resynching, in which case it's ok to hit one
4461  // of these; just discard the packet
4462  f->error = VORBIS__no_error;
4463  while (get8_packet(f) != EOP)
4464  if (f->eof) break;
4465  *samples = 0;
4466  return (int) (f->stream - data);
4467  }
4468  }
4469  // if we get an error while parsing, what to do?
4470  // well, it DEFINITELY won't work to continue from where we are!
4472  // restore the error that actually made us bail
4473  f->error = error;
4474  *samples = 0;
4475  return 1;
4476  }
4477 
4478  // success!
4479  len = vorbis_finish_frame(f, len, left, right);
4480  for (i=0; i < f->channels; ++i)
4481  f->outputs[i] = f->channel_buffers[i] + left;
4482 
4483  if (channels) *channels = f->channels;
4484  *samples = len;
4485  *output = f->outputs;
4486  return (int) (f->stream - data);
4487 }
4488 
4490  const unsigned char *data, int data_len, // the memory available for decoding
4491  int *data_used, // only defined if result is not NULL
4492  int *error, const stb_vorbis_alloc *alloc)
4493 {
4494  stb_vorbis *f, p;
4495  vorbis_init(&p, alloc);
4496  p.stream = (uint8 *) data;
4497  p.stream_end = (uint8 *) data + data_len;
4498  p.push_mode = TRUE;
4499  if (!start_decoder(&p)) {
4500  if (p.eof)
4502  else
4503  *error = p.error;
4504  return NULL;
4505  }
4506  f = vorbis_alloc(&p);
4507  if (f) {
4508  *f = p;
4509  *data_used = (int) (f->stream - data);
4510  *error = 0;
4511  return f;
4512  } else {
4513  vorbis_deinit(&p);
4514  return NULL;
4515  }
4516 }
4517 #endif // STB_VORBIS_NO_PUSHDATA_API
4518 
4520 {
4521  #ifndef STB_VORBIS_NO_PUSHDATA_API
4522  if (f->push_mode) return 0;
4523  #endif
4524  if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
4525  #ifndef STB_VORBIS_NO_STDIO
4526  return (unsigned int) (ftell(f->f) - f->f_start);
4527  #endif
4528 }
4529 
4530 #ifndef STB_VORBIS_NO_PULLDATA_API
4531 //
4532 // DATA-PULLING API
4533 //
4534 
4536 {
4537  for(;;) {
4538  int n;
4539  if (f->eof) return 0;
4540  n = get8(f);
4541  if (n == 0x4f) { // page header candidate
4542  unsigned int retry_loc = stb_vorbis_get_file_offset(f);
4543  int i;
4544  // check if we're off the end of a file_section stream
4545  if (retry_loc - 25 > f->stream_len)
4546  return 0;
4547  // check the rest of the header
4548  for (i=1; i < 4; ++i)
4549  if (get8(f) != ogg_page_header[i])
4550  break;
4551  if (f->eof) return 0;
4552  if (i == 4) {
4553  uint8 header[27];
4554  uint32 i, crc, goal, len;
4555  for (i=0; i < 4; ++i)
4556  header[i] = ogg_page_header[i];
4557  for (; i < 27; ++i)
4558  header[i] = get8(f);
4559  if (f->eof) return 0;
4560  if (header[4] != 0) goto invalid;
4561  goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24);
4562  for (i=22; i < 26; ++i)
4563  header[i] = 0;
4564  crc = 0;
4565  for (i=0; i < 27; ++i)
4566  crc = crc32_update(crc, header[i]);
4567  len = 0;
4568  for (i=0; i < header[26]; ++i) {
4569  int s = get8(f);
4570  crc = crc32_update(crc, s);
4571  len += s;
4572  }
4573  if (len && f->eof) return 0;
4574  for (i=0; i < len; ++i)
4575  crc = crc32_update(crc, get8(f));
4576  // finished parsing probable page
4577  if (crc == goal) {
4578  // we could now check that it's either got the last
4579  // page flag set, OR it's followed by the capture
4580  // pattern, but I guess TECHNICALLY you could have
4581  // a file with garbage between each ogg page and recover
4582  // from it automatically? So even though that paranoia
4583  // might decrease the chance of an invalid decode by
4584  // another 2^32, not worth it since it would hose those
4585  // invalid-but-useful files?
4586  if (end)
4587  *end = stb_vorbis_get_file_offset(f);
4588  if (last) {
4589  if (header[5] & 0x04)
4590  *last = 1;
4591  else
4592  *last = 0;
4593  }
4594  set_file_offset(f, retry_loc-1);
4595  return 1;
4596  }
4597  }
4598  invalid:
4599  // not a valid page, so rewind and look for next one
4600  set_file_offset(f, retry_loc);
4601  }
4602  }
4603 }
4604 
4605 
4606 #define SAMPLE_unknown 0xffffffff
4607 
4608 // seeking is implemented with a binary search, which narrows down the range to
4609 // 64K, before using a linear search (because finding the synchronization
4610 // pattern can be expensive, and the chance we'd find the end page again is
4611 // relatively high for small ranges)
4612 //
4613 // two initial interpolation-style probes are used at the start of the search
4614 // to try to bound either side of the binary search sensibly, while still
4615 // working in O(log n) time if they fail.
4616 
4618 {
4619  uint8 header[27], lacing[255];
4620  int i,len;
4621 
4622  // record where the page starts
4624 
4625  // parse the header
4626  getn(f, header, 27);
4627  if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
4628  return 0;
4629  getn(f, lacing, header[26]);
4630 
4631  // determine the length of the payload
4632  len = 0;
4633  for (i=0; i < header[26]; ++i)
4634  len += lacing[i];
4635 
4636  // this implies where the page ends
4637  z->page_end = z->page_start + 27 + header[26] + len;
4638 
4639  // read the last-decoded sample out of the data
4640  z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
4641 
4642  // restore file state to where we were
4644  return 1;
4645 }
4646 
4647 // rarely used function to seek back to the preceding page while finding the
4648 // start of a packet
4649 static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset)
4650 {
4651  unsigned int previous_safe, end;
4652 
4653  // now we want to seek back 64K from the limit
4654  if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset)
4655  previous_safe = limit_offset - 65536;
4656  else
4657  previous_safe = f->first_audio_page_offset;
4658 
4659  set_file_offset(f, previous_safe);
4660 
4661  while (vorbis_find_page(f, &end, NULL)) {
4662  if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset)
4663  return 1;
4664  set_file_offset(f, end);
4665  }
4666 
4667  return 0;
4668 }
4669 
4670 // implements the search logic for finding a page and starting decoding. if
4671 // the function succeeds, current_loc_valid will be true and current_loc will
4672 // be less than or equal to the provided sample number (the closer the
4673 // better).
4674 static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
4675 {
4676  ProbedPage left, right, mid;
4677  int i, start_seg_with_known_loc, end_pos, page_start;
4678  uint32 delta, stream_length, padding, last_sample_limit;
4679  double offset = 0.0, bytes_per_sample = 0.0;
4680  int probe = 0;
4681 
4682  // find the last page and validate the target sample
4683  stream_length = stb_vorbis_stream_length_in_samples(f);
4684  if (stream_length == 0) return error(f, VORBIS_seek_without_length);
4685  if (sample_number > stream_length) return error(f, VORBIS_seek_invalid);
4686 
4687  // this is the maximum difference between the window-center (which is the
4688  // actual granule position value), and the right-start (which the spec
4689  // indicates should be the granule position (give or take one)).
4690  padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
4691  if (sample_number < padding)
4692  last_sample_limit = 0;
4693  else
4694  last_sample_limit = sample_number - padding;
4695 
4696  left = f->p_first;
4697  while (left.last_decoded_sample == ~0U) {
4698  // (untested) the first page does not have a 'last_decoded_sample'
4699  set_file_offset(f, left.page_end);
4700  if (!get_seek_page_info(f, &left)) goto error;
4701  }
4702 
4703  right = f->p_last;
4704  assert(right.last_decoded_sample != ~0U);
4705 
4706  // starting from the start is handled differently
4707  if (last_sample_limit <= left.last_decoded_sample) {
4708  if (stb_vorbis_seek_start(f)) {
4709  if (f->current_loc > sample_number)
4710  return error(f, VORBIS_seek_failed);
4711  return 1;
4712  }
4713  return 0;
4714  }
4715 
4716  while (left.page_end != right.page_start) {
4717  assert(left.page_end < right.page_start);
4718  // search range in bytes
4719  delta = right.page_start - left.page_end;
4720  if (delta <= 65536) {
4721  // there's only 64K left to search - handle it linearly
4722  set_file_offset(f, left.page_end);
4723  } else {
4724  if (probe < 2) {
4725  if (probe == 0) {
4726  // first probe (interpolate)
4727  double data_bytes = right.page_end - left.page_start;
4728  bytes_per_sample = data_bytes / right.last_decoded_sample;
4729  offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample);
4730  } else {
4731  // second probe (try to bound the other side)
4732  double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample;
4733  if (error >= 0 && error < 8000) error = 8000;
4734  if (error < 0 && error > -8000) error = -8000;
4735  offset += error * 2;
4736  }
4737 
4738  // ensure the offset is valid
4739  if (offset < left.page_end)
4740  offset = left.page_end;
4741  if (offset > right.page_start - 65536)
4742  offset = right.page_start - 65536;
4743 
4744  set_file_offset(f, (unsigned int) offset);
4745  } else {
4746  // binary search for large ranges (offset by 32K to ensure
4747  // we don't hit the right page)
4748  set_file_offset(f, left.page_end + (delta / 2) - 32768);
4749  }
4750 
4751  if (!vorbis_find_page(f, NULL, NULL)) goto error;
4752  }
4753 
4754  for (;;) {
4755  if (!get_seek_page_info(f, &mid)) goto error;
4756  if (mid.last_decoded_sample != ~0U) break;
4757  // (untested) no frames end on this page
4758  set_file_offset(f, mid.page_end);
4759  assert(mid.page_start < right.page_start);
4760  }
4761 
4762  // if we've just found the last page again then we're in a tricky file,
4763  // and we're close enough (if it wasn't an interpolation probe).
4764  if (mid.page_start == right.page_start) {
4765  if (probe >= 2 || delta <= 65536)
4766  break;
4767  } else {
4768  if (last_sample_limit < mid.last_decoded_sample)
4769  right = mid;
4770  else
4771  left = mid;
4772  }
4773 
4774  ++probe;
4775  }
4776 
4777  // seek back to start of the last packet
4778  page_start = left.page_start;
4779  set_file_offset(f, page_start);
4780  if (!start_page(f)) return error(f, VORBIS_seek_failed);
4781  end_pos = f->end_seg_with_known_loc;
4782  assert(end_pos >= 0);
4783 
4784  for (;;) {
4785  for (i = end_pos; i > 0; --i)
4786  if (f->segments[i-1] != 255)
4787  break;
4788 
4789  start_seg_with_known_loc = i;
4790 
4791  if (start_seg_with_known_loc > 0 || !(f->page_flag & PAGEFLAG_continued_packet))
4792  break;
4793 
4794  // (untested) the final packet begins on an earlier page
4795  if (!go_to_page_before(f, page_start))
4796  goto error;
4797 
4798  page_start = stb_vorbis_get_file_offset(f);
4799  if (!start_page(f)) goto error;
4800  end_pos = f->segment_count - 1;
4801  }
4802 
4803  // prepare to start decoding
4804  f->current_loc_valid = FALSE;
4805  f->last_seg = FALSE;
4806  f->valid_bits = 0;
4807  f->packet_bytes = 0;
4808  f->bytes_in_seg = 0;
4809  f->previous_length = 0;
4810  f->next_seg = start_seg_with_known_loc;
4811 
4812  for (i = 0; i < start_seg_with_known_loc; i++)
4813  skip(f, f->segments[i]);
4814 
4815  // start decoding (optimizable - this frame is generally discarded)
4816  if (!vorbis_pump_first_frame(f))
4817  return 0;
4818  if (f->current_loc > sample_number)
4819  return error(f, VORBIS_seek_failed);
4820  return 1;
4821 
4822 error:
4823  // try to restore the file to a valid state
4825  return error(f, VORBIS_seek_failed);
4826 }
4827 
4828 // the same as vorbis_decode_initial, but without advancing
4829 static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode)
4830 {
4831  int bits_read, bytes_read;
4832 
4833  if (!vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode))
4834  return 0;
4835 
4836  // either 1 or 2 bytes were read, figure out which so we can rewind
4837  bits_read = 1 + ilog(f->mode_count-1);
4838  if (f->mode_config[*mode].blockflag)
4839  bits_read += 2;
4840  bytes_read = (bits_read + 7) / 8;
4841 
4842  f->bytes_in_seg += bytes_read;
4843  f->packet_bytes -= bytes_read;
4844  skip(f, -bytes_read);
4845  if (f->next_seg == -1)
4846  f->next_seg = f->segment_count - 1;
4847  else
4848  f->next_seg--;
4849  f->valid_bits = 0;
4850 
4851  return 1;
4852 }
4853 
4854 int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
4855 {
4856  uint32 max_frame_samples;
4857 
4859 
4860  // fast page-level search
4861  if (!seek_to_sample_coarse(f, sample_number))
4862  return 0;
4863 
4864  assert(f->current_loc_valid);
4865  assert(f->current_loc <= sample_number);
4866 
4867  // linear search for the relevant packet
4868  max_frame_samples = (f->blocksize_1*3 - f->blocksize_0) >> 2;
4869  while (f->current_loc < sample_number) {
4870  int left_start, left_end, right_start, right_end, mode, frame_samples;
4871  if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
4872  return error(f, VORBIS_seek_failed);
4873  // calculate the number of samples returned by the next frame
4874  frame_samples = right_start - left_start;
4875  if (f->current_loc + frame_samples > sample_number) {
4876  return 1; // the next frame will contain the sample
4877  } else if (f->current_loc + frame_samples + max_frame_samples > sample_number) {
4878  // there's a chance the frame after this could contain the sample
4880  } else {
4881  // this frame is too early to be relevant
4882  f->current_loc += frame_samples;
4883  f->previous_length = 0;
4885  flush_packet(f);
4886  }
4887  }
4888  // the next frame should start with the sample
4889  if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);
4890  return 1;
4891 }
4892 
4893 int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
4894 {
4895  if (!stb_vorbis_seek_frame(f, sample_number))
4896  return 0;
4897 
4898  if (sample_number != f->current_loc) {
4899  int n;
4900  uint32 frame_start = f->current_loc;
4902  assert(sample_number > frame_start);
4903  assert(f->channel_buffer_start + (int) (sample_number-frame_start) <= f->channel_buffer_end);
4904  f->channel_buffer_start += (sample_number - frame_start);
4905  }
4906 
4907  return 1;
4908 }
4909 
4911 {
4912  if (IS_PUSH_MODE(f)) { return error(f, VORBIS_invalid_api_mixing); }
4913  set_file_offset(f, f->first_audio_page_offset);
4914  f->previous_length = 0;
4915  f->first_decode = TRUE;
4916  f->next_seg = -1;
4917  return vorbis_pump_first_frame(f);
4918 }
4919 
4921 {
4922  unsigned int restore_offset, previous_safe;
4923  unsigned int end, last_page_loc;
4924 
4926  if (!f->total_samples) {
4927  unsigned int last;
4928  uint32 lo,hi;
4929  char header[6];
4930 
4931  // first, store the current decode position so we can restore it
4932  restore_offset = stb_vorbis_get_file_offset(f);
4933 
4934  // now we want to seek back 64K from the end (the last page must
4935  // be at most a little less than 64K, but let's allow a little slop)
4936  if (f->stream_len >= 65536 && f->stream_len-65536 >= f->first_audio_page_offset)
4937  previous_safe = f->stream_len - 65536;
4938  else
4939  previous_safe = f->first_audio_page_offset;
4940 
4941  set_file_offset(f, previous_safe);
4942  // previous_safe is now our candidate 'earliest known place that seeking
4943  // to will lead to the final page'
4944 
4945  if (!vorbis_find_page(f, &end, &last)) {
4946  // if we can't find a page, we're hosed!
4947  f->error = VORBIS_cant_find_last_page;
4948  f->total_samples = 0xffffffff;
4949  goto done;
4950  }
4951 
4952  // check if there are more pages
4953  last_page_loc = stb_vorbis_get_file_offset(f);
4954 
4955  // stop when the last_page flag is set, not when we reach eof;
4956  // this allows us to stop short of a 'file_section' end without
4957  // explicitly checking the length of the section
4958  while (!last) {
4959  set_file_offset(f, end);
4960  if (!vorbis_find_page(f, &end, &last)) {
4961  // the last page we found didn't have the 'last page' flag
4962  // set. whoops!
4963  break;
4964  }
4965  previous_safe = last_page_loc+1;
4966  last_page_loc = stb_vorbis_get_file_offset(f);
4967  }
4968 
4969  set_file_offset(f, last_page_loc);
4970 
4971  // parse the header
4972  getn(f, (unsigned char *)header, 6);
4973  // extract the absolute granule position
4974  lo = get32(f);
4975  hi = get32(f);
4976  if (lo == 0xffffffff && hi == 0xffffffff) {
4977  f->error = VORBIS_cant_find_last_page;
4978  f->total_samples = SAMPLE_unknown;
4979  goto done;
4980  }
4981  if (hi)
4982  lo = 0xfffffffe; // saturate
4983  f->total_samples = lo;
4984 
4985  f->p_last.page_start = last_page_loc;
4986  f->p_last.page_end = end;
4987  f->p_last.last_decoded_sample = lo;
4988 
4989  done:
4990  set_file_offset(f, restore_offset);
4991  }
4992  return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples;
4993 }
4994 
4996 {
4997  return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate;
4998 }
4999 
5000 
5001 
5002 int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output)
5003 {
5004  int len, right,left,i;
5006 
5007  if (!vorbis_decode_packet(f, &len, &left, &right)) {
5008  f->channel_buffer_start = f->channel_buffer_end = 0;
5009  return 0;
5010  }
5011 
5012  len = vorbis_finish_frame(f, len, left, right);
5013  for (i=0; i < f->channels; ++i)
5014  f->outputs[i] = f->channel_buffers[i] + left;
5015 
5016  f->channel_buffer_start = left;
5017  f->channel_buffer_end = left+len;
5018 
5019  if (channels) *channels = f->channels;
5020  if (output) *output = f->outputs;
5021  return len;
5022 }
5023 
5024 #ifndef STB_VORBIS_NO_STDIO
5025 
5026 stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length)
5027 {
5028  stb_vorbis *f, p;
5029  vorbis_init(&p, alloc);
5030  p.f = file;
5031  p.f_start = (uint32) ftell(file);
5032  p.stream_len = length;
5034  if (start_decoder(&p)) {
5035  f = vorbis_alloc(&p);
5036  if (f) {
5037  *f = p;
5039  return f;
5040  }
5041  }
5042  if (error) *error = p.error;
5043  vorbis_deinit(&p);
5044  return NULL;
5045 }
5046 
5048 {
5049  unsigned int len, start;
5050  start = (unsigned int) ftell(file);
5051  fseek(file, 0, SEEK_END);
5052  len = (unsigned int) (ftell(file) - start);
5053  fseek(file, start, SEEK_SET);
5055 }
5056 
5057 stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc)
5058 {
5059  FILE *f;
5060 #if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)
5061  if (0 != fopen_s(&f, filename, "rb"))
5062  f = NULL;
5063 #else
5064  f = fopen(filename, "rb");
5065 #endif
5066  if (f)
5067  return stb_vorbis_open_file(f, TRUE, error, alloc);
5069  return NULL;
5070 }
5071 #endif // STB_VORBIS_NO_STDIO
5072 
5073 stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc)
5074 {
5075  stb_vorbis *f, p;
5076  if (data == NULL) return NULL;
5077  vorbis_init(&p, alloc);
5078  p.stream = (uint8 *) data;
5079  p.stream_end = (uint8 *) data + len;
5080  p.stream_start = (uint8 *) p.stream;
5081  p.stream_len = len;
5082  p.push_mode = FALSE;
5083  if (start_decoder(&p)) {
5084  f = vorbis_alloc(&p);
5085  if (f) {
5086  *f = p;
5088  if (error) *error = VORBIS__no_error;
5089  return f;
5090  }
5091  }
5092  if (error) *error = p.error;
5093  vorbis_deinit(&p);
5094  return NULL;
5095 }
5096 
5097 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
5098 #define PLAYBACK_MONO 1
5099 #define PLAYBACK_LEFT 2
5100 #define PLAYBACK_RIGHT 4
5101 
5102 #define L (PLAYBACK_LEFT | PLAYBACK_MONO)
5103 #define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO)
5104 #define R (PLAYBACK_RIGHT | PLAYBACK_MONO)
5105 
5106 static int8 channel_position[7][6] =
5107 {
5108  { 0 },
5109  { C },
5110  { L, R },
5111  { L, C, R },
5112  { L, R, L, R },
5113  { L, C, R, L, R },
5114  { L, C, R, L, R, C },
5115 };
5116 
5117 
5118 #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
5119  typedef union {
5120  float f;
5121  int i;
5122  } float_conv;
5123  typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
5124  #define FASTDEF(x) float_conv x
5125  // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
5126  #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
5127  #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
5128  #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
5129  #define check_endianness()
5130 #else
5131  #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
5132  #define check_endianness()
5133  #define FASTDEF(x)
5134 #endif
5135 
5136 static void copy_samples(short *dest, float *src, int len)
5137 {
5138  int i;
5139  check_endianness();
5140  for (i=0; i < len; ++i) {
5141  FASTDEF(temp);
5142  int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
5143  if ((unsigned int) (v + 32768) > 65535)
5144  v = v < 0 ? -32768 : 32767;
5145  dest[i] = v;
5146  }
5147 }
5148 
5149 static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
5150 {
5151  #define BUFFER_SIZE 32
5152  float buffer[BUFFER_SIZE];
5153  int i,j,o,n = BUFFER_SIZE;
5154  check_endianness();
5155  for (o = 0; o < len; o += BUFFER_SIZE) {
5156  memset(buffer, 0, sizeof(buffer));
5157  if (o + n > len) n = len - o;
5158  for (j=0; j < num_c; ++j) {
5159  if (channel_position[num_c][j] & mask) {
5160  for (i=0; i < n; ++i)
5161  buffer[i] += data[j][d_offset+o+i];
5162  }
5163  }
5164  for (i=0; i < n; ++i) {
5165  FASTDEF(temp);
5166  int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5167  if ((unsigned int) (v + 32768) > 65535)
5168  v = v < 0 ? -32768 : 32767;
5169  output[o+i] = v;
5170  }
5171  }
5172 }
5173 
5174 static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len)
5175 {
5176  #define BUFFER_SIZE 32
5177  float buffer[BUFFER_SIZE];
5178  int i,j,o,n = BUFFER_SIZE >> 1;
5179  // o is the offset in the source data
5180  check_endianness();
5181  for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
5182  // o2 is the offset in the output data
5183  int o2 = o << 1;
5184  memset(buffer, 0, sizeof(buffer));
5185  if (o + n > len) n = len - o;
5186  for (j=0; j < num_c; ++j) {
5187  int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT);
5188  if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) {
5189  for (i=0; i < n; ++i) {
5190  buffer[i*2+0] += data[j][d_offset+o+i];
5191  buffer[i*2+1] += data[j][d_offset+o+i];
5192  }
5193  } else if (m == PLAYBACK_LEFT) {
5194  for (i=0; i < n; ++i) {
5195  buffer[i*2+0] += data[j][d_offset+o+i];
5196  }
5197  } else if (m == PLAYBACK_RIGHT) {
5198  for (i=0; i < n; ++i) {
5199  buffer[i*2+1] += data[j][d_offset+o+i];
5200  }
5201  }
5202  }
5203  for (i=0; i < (n<<1); ++i) {
5204  FASTDEF(temp);
5205  int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5206  if ((unsigned int) (v + 32768) > 65535)
5207  v = v < 0 ? -32768 : 32767;
5208  output[o2+i] = v;
5209  }
5210  }
5211 }
5212 
5213 static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples)
5214 {
5215  int i;
5216  if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5217  static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
5218  for (i=0; i < buf_c; ++i)
5219  compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples);
5220  } else {
5221  int limit = buf_c < data_c ? buf_c : data_c;
5222  for (i=0; i < limit; ++i)
5223  copy_samples(buffer[i]+b_offset, data[i]+d_offset, samples);
5224  for ( ; i < buf_c; ++i)
5225  memset(buffer[i]+b_offset, 0, sizeof(short) * samples);
5226  }
5227 }
5228 
5229 int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples)
5230 {
5231  float **output = NULL;
5232  int len = stb_vorbis_get_frame_float(f, NULL, &output);
5233  if (len > num_samples) len = num_samples;
5234  if (len)
5235  convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len);
5236  return len;
5237 }
5238 
5239 static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len)
5240 {
5241  int i;
5242  check_endianness();
5243  if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5244  assert(buf_c == 2);
5245  for (i=0; i < buf_c; ++i)
5246  compute_stereo_samples(buffer, data_c, data, d_offset, len);
5247  } else {
5248  int limit = buf_c < data_c ? buf_c : data_c;
5249  int j;
5250  for (j=0; j < len; ++j) {
5251  for (i=0; i < limit; ++i) {
5252  FASTDEF(temp);
5253  float f = data[i][d_offset+j];
5254  int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15);
5255  if ((unsigned int) (v + 32768) > 65535)
5256  v = v < 0 ? -32768 : 32767;
5257  *buffer++ = v;
5258  }
5259  for ( ; i < buf_c; ++i)
5260  *buffer++ = 0;
5261  }
5262  }
5263 }
5264 
5265 int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts)
5266 {
5267  float **output;
5268  int len;
5269  if (num_c == 1) return stb_vorbis_get_frame_short(f,num_c,&buffer, num_shorts);
5270  len = stb_vorbis_get_frame_float(f, NULL, &output);
5271  if (len) {
5272  if (len*num_c > num_shorts) len = num_shorts / num_c;
5273  convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len);
5274  }
5275  return len;
5276 }
5277 
5278 int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts)
5279 {
5280  float **outputs;
5281  int len = num_shorts / channels;
5282  int n=0;
5283  int z = f->channels;
5284  if (z > channels) z = channels;
5285  while (n < len) {
5286  int k = f->channel_buffer_end - f->channel_buffer_start;
5287  if (n+k >= len) k = len - n;
5288  if (k)
5289  convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k);
5290  buffer += k*channels;
5291  n += k;
5292  f->channel_buffer_start += k;
5293  if (n == len) break;
5294  if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
5295  }
5296  return n;
5297 }
5298 
5299 int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len)
5300 {
5301  float **outputs;
5302  int n=0;
5303  int z = f->channels;
5304  if (z > channels) z = channels;
5305  while (n < len) {
5306  int k = f->channel_buffer_end - f->channel_buffer_start;
5307  if (n+k >= len) k = len - n;
5308  if (k)
5309  convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k);
5310  n += k;
5311  f->channel_buffer_start += k;
5312  if (n == len) break;
5313  if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
5314  }
5315  return n;
5316 }
5317 
5318 #ifndef STB_VORBIS_NO_STDIO
5319 int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output)
5320 {
5321  int data_len, offset, total, limit, error;
5322  short *data;
5323  stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL);
5324  if (v == NULL) return -1;
5325  limit = v->channels * 4096;
5326  *channels = v->channels;
5327  if (sample_rate)
5328  *sample_rate = v->sample_rate;
5329  offset = data_len = 0;
5330  total = limit;
5331  data = (short *) malloc(total * sizeof(*data));
5332  if (data == NULL) {
5333  stb_vorbis_close(v);
5334  return -2;
5335  }
5336  for (;;) {
5337  int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset);
5338  if (n == 0) break;
5339  data_len += n;
5340  offset += n * v->channels;
5341  if (offset + limit > total) {
5342  short *data2;
5343  total *= 2;
5344  data2 = (short *) realloc(data, total * sizeof(*data));
5345  if (data2 == NULL) {
5346  free(data);
5347  stb_vorbis_close(v);
5348  return -2;
5349  }
5350  data = data2;
5351  }
5352  }
5353  *output = data;
5354  stb_vorbis_close(v);
5355  return data_len;
5356 }
5357 #endif // NO_STDIO
5358 
5359 int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *sample_rate, short **output)
5360 {
5361  int data_len, offset, total, limit, error;
5362  short *data;
5363  stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL);
5364  if (v == NULL) return -1;
5365  limit = v->channels * 4096;
5366  *channels = v->channels;
5367  if (sample_rate)
5368  *sample_rate = v->sample_rate;
5369  offset = data_len = 0;
5370  total = limit;
5371  data = (short *) malloc(total * sizeof(*data));
5372  if (data == NULL) {
5373  stb_vorbis_close(v);
5374  return -2;
5375  }
5376  for (;;) {
5377  int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset);
5378  if (n == 0) break;
5379  data_len += n;
5380  offset += n * v->channels;
5381  if (offset + limit > total) {
5382  short *data2;
5383  total *= 2;
5384  data2 = (short *) realloc(data, total * sizeof(*data));
5385  if (data2 == NULL) {
5386  free(data);
5387  stb_vorbis_close(v);
5388  return -2;
5389  }
5390  data = data2;
5391  }
5392  }
5393  *output = data;
5394  stb_vorbis_close(v);
5395  return data_len;
5396 }
5397 #endif // STB_VORBIS_NO_INTEGER_CONVERSION
5398 
5399 int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats)
5400 {
5401  float **outputs;
5402  int len = num_floats / channels;
5403  int n=0;
5404  int z = f->channels;
5405  if (z > channels) z = channels;
5406  while (n < len) {
5407  int i,j;
5408  int k = f->channel_buffer_end - f->channel_buffer_start;
5409  if (n+k >= len) k = len - n;
5410  for (j=0; j < k; ++j) {
5411  for (i=0; i < z; ++i)
5412  *buffer++ = f->channel_buffers[i][f->channel_buffer_start+j];
5413  for ( ; i < channels; ++i)
5414  *buffer++ = 0;
5415  }
5416  n += k;
5417  f->channel_buffer_start += k;
5418  if (n == len)
5419  break;
5420  if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
5421  break;
5422  }
5423  return n;
5424 }
5425 
5426 int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples)
5427 {
5428  float **outputs;
5429  int n=0;
5430  int z = f->channels;
5431  if (z > channels) z = channels;
5432  while (n < num_samples) {
5433  int i;
5434  int k = f->channel_buffer_end - f->channel_buffer_start;
5435  if (n+k >= num_samples) k = num_samples - n;
5436  if (k) {
5437  for (i=0; i < z; ++i)
5438  memcpy(buffer[i]+n, f->channel_buffers[i]+f->channel_buffer_start, sizeof(float)*k);
5439  for ( ; i < channels; ++i)
5440  memset(buffer[i]+n, 0, sizeof(float) * k);
5441  }
5442  n += k;
5443  f->channel_buffer_start += k;
5444  if (n == num_samples)
5445  break;
5446  if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
5447  break;
5448  }
5449  return n;
5450 }
5451 #endif // STB_VORBIS_NO_PULLDATA_API
5452 
5453 /* Version history
5454  1.17 - 2019-07-08 - fix CVE-2019-13217, -13218, -13219, -13220, -13221, -13222, -13223
5455  found with Mayhem by ForAllSecure
5456  1.16 - 2019-03-04 - fix warnings
5457  1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found
5458  1.14 - 2018-02-11 - delete bogus dealloca usage
5459  1.13 - 2018-01-29 - fix truncation of last frame (hopefully)
5460  1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
5461  1.11 - 2017-07-23 - fix MinGW compilation
5462  1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory
5463  1.09 - 2016-04-04 - back out 'avoid discarding last frame' fix from previous version
5464  1.08 - 2016-04-02 - fixed multiple warnings; fix setup memory leaks;
5465  avoid discarding last frame of audio data
5466  1.07 - 2015-01-16 - fixed some warnings, fix mingw, const-correct API
5467  some more crash fixes when out of memory or with corrupt files
5468  1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
5469  some crash fixes when out of memory or with corrupt files
5470  1.05 - 2015-04-19 - don't define __forceinline if it's redundant
5471  1.04 - 2014-08-27 - fix missing const-correct case in API
5472  1.03 - 2014-08-07 - Warning fixes
5473  1.02 - 2014-07-09 - Declare qsort compare function _cdecl on windows
5474  1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float
5475  1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in multichannel
5476  (API change) report sample rate for decode-full-file funcs
5477  0.99996 - bracket #include <malloc.h> for macintosh compilation by Laurent Gomila
5478  0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem
5479  0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence
5480  0.99993 - remove assert that fired on legal files with empty tables
5481  0.99992 - rewind-to-start
5482  0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo
5483  0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++
5484  0.9998 - add a full-decode function with a memory source
5485  0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition
5486  0.9996 - query length of vorbis stream in samples/seconds
5487  0.9995 - bugfix to another optimization that only happened in certain files
5488  0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors
5489  0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation
5490  0.9992 - performance improvement of IMDCT; now performs close to reference implementation
5491  0.9991 - performance improvement of IMDCT
5492  0.999 - (should have been 0.9990) performance improvement of IMDCT
5493  0.998 - no-CRT support from Casey Muratori
5494  0.997 - bugfixes for bugs found by Terje Mathisen
5495  0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen
5496  0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen
5497  0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen
5498  0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen
5499  0.992 - fixes for MinGW warning
5500  0.991 - turn fast-float-conversion on by default
5501  0.990 - fix push-mode seek recovery if you seek into the headers
5502  0.98b - fix to bad release of 0.98
5503  0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode
5504  0.97 - builds under c++ (typecasting, don't use 'class' keyword)
5505  0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code
5506  0.95 - clamping code for 16-bit functions
5507  0.94 - not publically released
5508  0.93 - fixed all-zero-floor case (was decoding garbage)
5509  0.92 - fixed a memory leak
5510  0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION
5511  0.90 - first public release
5512 */
5513 
5514 #endif // STB_VORBIS_HEADER_ONLY
5515 
5516 
5517 /*
5518 ------------------------------------------------------------------------------
5519 This software is available under 2 licenses -- choose whichever you prefer.
5520 ------------------------------------------------------------------------------
5521 ALTERNATIVE A - MIT License
5522 Copyright (c) 2017 Sean Barrett
5523 Permission is hereby granted, free of charge, to any person obtaining a copy of
5524 this software and associated documentation files (the "Software"), to deal in
5525 the Software without restriction, including without limitation the rights to
5526 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
5527 of the Software, and to permit persons to whom the Software is furnished to do
5528 so, subject to the following conditions:
5529 The above copyright notice and this permission notice shall be included in all
5530 copies or substantial portions of the Software.
5531 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5532 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5533 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5534 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5535 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5536 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5537 SOFTWARE.
5538 ------------------------------------------------------------------------------
5539 ALTERNATIVE B - Public Domain (www.unlicense.org)
5540 This is free and unencumbered software released into the public domain.
5541 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
5542 software, either in source code form or as a compiled binary, for any purpose,
5543 commercial or non-commercial, and by any means.
5544 In jurisdictions that recognize copyright laws, the author or authors of this
5545 software dedicate any and all copyright interest in the software to the public
5546 domain. We make this dedication for the benefit of the public at large and to
5547 the detriment of our heirs and successors. We intend this dedication to be an
5548 overt act of relinquishment in perpetuity of all present and future rights to
5549 this software under copyright law.
5550 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5551 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5552 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5553 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
5554 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
5555 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5556 ------------------------------------------------------------------------------
5557 */
stb_vorbis::vendor
char * vendor
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:778
vorbis_search_for_page_pushdata
static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4328
R
#define R
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5104
stb_vorbis::known_loc_for_packet
uint32 known_loc_for_packet
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:871
Floor0
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:686
compute_samples
static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5149
USE_MEMORY
#define USE_MEMORY(z)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1317
stb_vorbis::residue_types
uint16 residue_types[64]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:824
point_compare
static int STBV_CDECL point_compare(const void *p, const void *q)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1303
uint8
unsigned char uint8
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:633
stb_vorbis_get_sample_offset
int stb_vorbis_get_sample_offset(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4272
stb_vorbis::residue_count
int residue_count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:823
compute_accelerated_huffman
static void compute_accelerated_huffman(Codebook *c)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1117
Codebook::codewords
uint32 * codewords
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:675
stb_vorbis_decode_filename
int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5319
codebook_decode_deinterleave_repeat
static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1847
stb_vorbis_get_file_offset
unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4519
stb_vorbis::f
FILE * f
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:784
stb_vorbis_get_info
stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4280
Floor0::rate
uint16 rate
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:689
stb_vorbis::setup_offset
int setup_offset
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:806
compute_bitreverse
static void compute_bitreverse(int n, uint16 *rev)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1261
predict_point
static int predict_point(int x, int x0, int x1, int y0, int y1)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1917
float_conv::i
int i
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5121
VORBIS_packet_setup
@ VORBIS_packet_setup
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1635
compute_sorted_huffman
static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1162
stb_vorbis::next_seg
int next_seg
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:864
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
codebook_decode
static int codebook_decode(vorb *f, Codebook *c, float *output, int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1777
stb_vorbis::serial
uint32 serial
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:857
stb_vorbis_seek
int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4893
stb_vorbis_seek_frame
int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4854
STB_VORBIS_MAX_CHANNELS
#define STB_VORBIS_MAX_CHANNELS
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:450
stb_vorbis::p_first
ProbedPage p_first
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:802
stb_vorbis::samples_output
uint32 samples_output
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:873
vorbis_decode_initial
static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3106
SAMPLE_unknown
#define SAMPLE_unknown
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4606
temp_alloc_restore
#define temp_alloc_restore(f, p)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:916
CRCscan::sample_loc
uint32 sample_loc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:759
Residue::classbook
uint8 classbook
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:724
vorbis_validate
static int vorbis_validate(uint8 *data)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1216
Mapping::submaps
uint8 submaps
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:740
VORBIS_cant_find_last_page
@ VORBIS_cant_find_last_page
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:393
Codebook::fast_huffman
int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:677
ilog
static int ilog(int32 n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1010
check_endianness
#define check_endianness()
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5129
s
XmlRpcServer s
stb_vorbis::mapping
Mapping * mapping
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:827
Residue
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:719
stb_vorbis::close_on_free
int close_on_free
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:786
stb_vorbis_get_error
int stb_vorbis_get_error(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4301
stb_vorbis_info
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:124
Mode::mapping
uint8 mapping
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:748
start_page_no_capturepattern
static int start_page_no_capturepattern(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1427
Mapping::chan
MappingChannel * chan
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:739
FALSE
#define FALSE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:642
Residue::end
uint32 end
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:721
PAGEFLAG_last_page
#define PAGEFLAG_last_page
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1425
vorbis_find_page
static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4535
vorbis_finish_frame
static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3437
Mode::blockflag
uint8 blockflag
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:747
stb_vorbis_comment::comment_list_length
int comment_list_length
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:140
stb_vorbis::current_loc
uint32 current_loc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:846
CODEBOOK_ELEMENT_BASE
#define CODEBOOK_ELEMENT_BASE(c)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1755
go_to_page_before
static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4649
stb_vorbis_get_frame_float
int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5002
stb_vorbis::bytes_in_seg
uint8 bytes_in_seg
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:862
Floor0::amplitude_bits
uint8 amplitude_bits
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:691
stb_vorbis::stream_start
uint8 * stream_start
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:790
stb_vorbis::residue_config
Residue * residue_config
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:825
init_blocksize
static int init_blocksize(vorb *f, int b, int n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1269
get8
static uint8 get8(vorb *z)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1320
stb_vorbis::end_seg_with_known_loc
int end_seg_with_known_loc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:870
VORBIS_bad_packet_type
@ VORBIS_bad_packet_type
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:392
Floor1::sorted_order
uint8 sorted_order[31 *8+2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:706
lookup1_values
static int lookup1_values(int entries, int dim)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1224
maybe_start_packet
static int maybe_start_packet(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1501
stb_vorbis_open_filename
stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc_buffer)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5057
residue_decode
static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2065
uint16
unsigned short uint16
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:635
float_conv::f
float f
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5120
LINE_OP
#define LINE_OP(a, b)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2005
stb_vorbis::outputs
float * outputs[STB_VORBIS_MAX_CHANNELS]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:835
ogg_page_header
static uint8 ogg_page_header[4]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1412
include_in_sort
static int include_in_sort(Codebook *c, uint8 len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1152
setup_temp_malloc
static void * setup_temp_malloc(vorb *f, int sz)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:952
is_whole_packet_present
static int is_whole_packet_present(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3500
stb_vorbis_open_file_section
stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5026
Floor1::partition_class_list
uint8 partition_class_list[32]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:700
DECODE_VQ
#define DECODE_VQ(var, f, c)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1741
CHECK
#define CHECK(f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:626
PLAYBACK_RIGHT
#define PLAYBACK_RIGHT
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5100
stb_vorbis_stream_length_in_samples
unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4920
stb_vorbis::channel_buffer_end
int channel_buffer_end
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:883
capture_pattern
static int capture_pattern(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1414
getn
static int getn(vorb *z, uint8 *data, int n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1346
int16
signed short int16
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:636
YTYPE
int16 YTYPE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3051
stb_vorbis::segments
uint8 segments[255]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:860
channel_position
static int8 channel_position[7][6]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5106
Codebook::minimum_value
float minimum_value
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:667
bit_reverse
static unsigned int bit_reverse(unsigned int n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:993
iter_54
static __forceinline void iter_54(float *z)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2536
seek_to_sample_coarse
static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4674
C
#define C
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5103
setup_temp_free
static void setup_temp_free(vorb *f, void *p, int sz)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:963
VORBIS_invalid_api_mixing
@ VORBIS_invalid_api_mixing
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:369
VORBIS_unexpected_eof
@ VORBIS_unexpected_eof
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:376
stb_vorbis::first_audio_page_offset
uint32 first_audio_page_offset
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:798
get8_packet
static int get8_packet(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1558
CRC32_POLY
#define CRC32_POLY
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:972
stb_vorbis::previous_window
float * previous_window[STB_VORBIS_MAX_CHANNELS]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:837
f
f
PAGEFLAG_first_page
#define PAGEFLAG_first_page
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1424
stb_vorbis::last_page
int last_page
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:858
stb_vorbis::push_mode
uint8 push_mode
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:795
codebook_decode_scalar_raw
static int codebook_decode_scalar_raw(vorb *f, Codebook *c)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1638
VORBIS_too_many_channels
@ VORBIS_too_many_channels
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:372
stb_vorbis::last_seg_which
int last_seg_which
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:866
stb_vorbis::bit_reverse
uint16 * bit_reverse[2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:854
stb_vorbis_alloc::alloc_buffer_length_in_bytes
int alloc_buffer_length_in_bytes
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:116
stb_vorbis::last_seg
int last_seg
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:865
Mapping
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:736
start_decoder
static int start_decoder(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3561
stb_vorbis_open_memory
stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc_buffer)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5073
stb_vorbis::valid_bits
int valid_bits
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:868
vorbis_alloc
static stb_vorbis * vorbis_alloc(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4308
VORBIS_seek_without_length
@ VORBIS_seek_without_length
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:374
stb_vorbis_get_samples_float_interleaved
int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5399
stb_vorbis_seek_start
int stb_vorbis_seek_start(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4910
get_bits
static uint32 get_bits(vorb *f, int n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1582
stb_vorbis_get_samples_float
int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5426
stb_vorbis::discard_samples_deferred
int discard_samples_deferred
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:872
STB_VORBIS_PUSHDATA_CRC_COUNT
#define STB_VORBIS_PUSHDATA_CRC_COUNT
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:466
Codebook::lookup_type
uint8 lookup_type
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:670
Codebook::dimensions
int dimensions
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:665
ProbedPage
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:762
stb_vorbis_alloc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:113
Mapping::coupling_steps
uint16 coupling_steps
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:738
VORBIS_invalid_stream
@ VORBIS_invalid_stream
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:384
stb_vorbis::B
float * B[2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:852
stb_vorbis::A
float * A[2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:852
int8
signed char int8
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:634
setup_malloc
static void * setup_malloc(vorb *f, int sz)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:933
Residue::classdata
uint8 ** classdata
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:725
imdct_step3_inner_s_loop
static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2485
stb_vorbis::sample_rate
unsigned int sample_rate
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:771
stb_vorbis::p_last
ProbedPage p_last
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:802
int32
signed int int32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:638
FAST_HUFFMAN_TABLE_SIZE
#define FAST_HUFFMAN_TABLE_SIZE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:660
Codebook::lookup_values
uint32 lookup_values
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:673
stb_vorbis::mode_config
Mode mode_config[64]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:829
set_file_offset
static int set_file_offset(stb_vorbis *f, unsigned int loc)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1380
TRUE
#define TRUE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:641
VORBIS_missing_capture_pattern
@ VORBIS_missing_capture_pattern
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:387
stb_vorbis::finalY
int16 * finalY[STB_VORBIS_MAX_CHANNELS]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:841
Codebook::sorted_entries
int sorted_entries
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:683
codebook_decode_step
static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1816
stb_vorbis_comment::vendor
char * vendor
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:138
STB_VORBIS_FAST_HUFFMAN_SHORT
#define STB_VORBIS_FAST_HUFFMAN_SHORT
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:489
PAGEFLAG_continued_packet
#define PAGEFLAG_continued_packet
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1423
stb_vorbis::stream_end
uint8 * stream_end
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:791
compute_codewords
static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1069
python.util.log
log
Definition: porcupine/binding/python/util.py:18
stb_vorbis::stream
uint8 * stream
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:789
IS_PUSH_MODE
#define IS_PUSH_MODE(f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:891
Floor1::neighbors
uint8 neighbors[31 *8+2][2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:707
stb_vorbis::channel_buffer_start
int channel_buffer_start
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:882
Codebook::value_bits
uint8 value_bits
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:669
VORBIS_invalid_setup
@ VORBIS_invalid_setup
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:383
stbv__floor_ordering::x
uint16 x
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1300
stb_vorbis_stream_length_in_seconds
float stb_vorbis_stream_length_in_seconds(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4995
Residue::residue_books
int16(* residue_books)[8]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:726
CRCscan
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:753
stb_vorbis::page_flag
uint8 page_flag
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:861
float32_unpack
static float float32_unpack(uint32 x)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1040
VORBIS_packet_id
@ VORBIS_packet_id
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1633
uint32_compare
static int STBV_CDECL uint32_compare(const void *p, const void *q)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1145
stb_vorbis_comment::comment_list
char ** comment_list
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:141
uint32
unsigned int uint32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:637
STBVorbisError
STBVorbisError
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:363
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
FAST_SCALED_FLOAT_TO_INT
#define FAST_SCALED_FLOAT_TO_INT(temp, x, s)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5128
stb_vorbis_float_size_test
char stb_vorbis_float_size_test[sizeof(float)==4 &&sizeof(int)==4]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5123
imdct_step3_inner_s_loop_ld654
static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2568
Floor1::Xlist
uint16 Xlist[31 *8+2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:705
CRCscan::goal_crc
uint32 goal_crc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:755
temp_free
#define temp_free(f, p)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:914
VORBIS_ogg_skeleton_not_supported
@ VORBIS_ogg_skeleton_not_supported
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:395
stb_vorbis::setup_memory_required
unsigned int setup_memory_required
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:774
stb_vorbis_comment
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:136
convert_channels_short_interleaved
static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5239
square
static float square(float x)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1002
VORBIS_invalid_first_page
@ VORBIS_invalid_first_page
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:391
draw_line
static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2016
Codebook::sparse
uint8 sparse
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:672
d
d
VORBIS_invalid_stream_structure_version
@ VORBIS_invalid_stream_structure_version
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:388
stb_vorbis::acc
uint32 acc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:867
CRCscan::bytes_done
int bytes_done
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:758
Floor1::rangebits
uint8 rangebits
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:709
decode_residue
static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2086
stb_vorbis::channel_buffers
float * channel_buffers[STB_VORBIS_MAX_CHANNELS]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:834
MappingChannel
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:729
stb_vorbis_open_pushdata
stb_vorbis * stb_vorbis_open_pushdata(const unsigned char *datablock, int datablock_length_in_bytes, int *datablock_memory_consumed_in_bytes, int *error, const stb_vorbis_alloc *alloc_buffer)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4489
stb_vorbis::scan
CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:878
Residue::classifications
uint8 classifications
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:723
float_conv
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5119
L
#define L
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5102
VORBIS_file_open_failure
@ VORBIS_file_open_failure
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:373
stb_vorbis
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:768
convert_samples_short
static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5213
__forceinline
#define __forceinline
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:609
start_packet
static int start_packet(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1486
stb_vorbis_open_file
stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close, int *error, const stb_vorbis_alloc *alloc_buffer)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5047
do_floor
static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3055
FASTDEF
#define FASTDEF(x)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5124
stb_vorbis_get_comment
stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4292
compute_window
static void compute_window(int n, float *window)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1254
M_PI
#define M_PI
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1029
imdct_step3_inner_r_loop
static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2435
Floor::floor0
Floor0 floor0
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:715
stb_vorbis::comment_list
char ** comment_list
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:780
stb_vorbis::eof
int eof
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:810
NO_CODE
#define NO_CODE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1033
Residue::part_size
uint32 part_size
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:722
CODEBOOK_ELEMENT_FAST
#define CODEBOOK_ELEMENT_FAST(c, off)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1754
stb_vorbis::floor_types
uint16 floor_types[64]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:821
EOP
#define EOP
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1543
stb_vorbis_get_frame_short
int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5229
Floor
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:713
temp_block_array
#define temp_block_array(f, count, size)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:918
make_block_array
static void * make_block_array(void *mem, int count, int size)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:921
Codebook::sequence_p
uint8 sequence_p
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:671
Floor1::class_subclasses
uint8 class_subclasses[16]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:702
stb_vorbis_info::setup_memory_required
unsigned int setup_memory_required
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:129
peek_decode_initial
static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4829
compute_stereo_samples
static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5174
stb_vorbis_info::sample_rate
unsigned int sample_rate
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:126
Mode::windowtype
uint16 windowtype
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:749
Floor0::order
uint8 order
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:688
start
ROSCPP_DECL void start()
stb_vorbis::channels
int channels
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:772
STBV_CDECL
#define STBV_CDECL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1142
inverse_mdct
static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2612
stb_vorbis::f_start
uint32 f_start
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:785
MappingChannel::angle
uint8 angle
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:732
crc32_update
static __forceinline uint32 crc32_update(uint32 crc, uint8 byte)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:986
BUFFER_SIZE
#define BUFFER_SIZE
VORBIS_feature_not_supported
@ VORBIS_feature_not_supported
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:371
stb_vorbis_flush_pushdata
void stb_vorbis_flush_pushdata(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4316
stb_vorbis::window
float * window[2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:853
stb_vorbis::C
float * C[2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:852
stb_vorbis::blocksize
int blocksize[2]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:816
Codebook
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:663
get32
static uint32 get32(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1336
Codebook::sorted_values
int * sorted_values
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:682
Codebook::codeword_lengths
uint8 * codeword_lengths
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:666
stb_vorbis::segment_count
int segment_count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:859
stb_vorbis::floor_config
Floor * floor_config
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:822
stb_vorbis::temp_offset
int temp_offset
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:807
VORBIS_seek_invalid
@ VORBIS_seek_invalid
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:377
stb_vorbis_get_samples_short_interleaved
int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5278
Codebook::sorted_codewords
uint32 * sorted_codewords
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:681
VORBIS__no_error
@ VORBIS__no_error
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:365
VORBIS_outofmem
@ VORBIS_outofmem
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:370
stb_vorbis_info::setup_temp_memory_required
unsigned int setup_temp_memory_required
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:130
codetype
float codetype
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:645
inverse_db_table
static float inverse_db_table[256]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1928
stb_vorbis_info::channels
int channels
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:127
temp_alloc_save
#define temp_alloc_save(f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:915
imdct_step3_iter0_loop
static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:2390
stb_vorbis::stream_len
uint32 stream_len
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:793
CRCscan::crc_so_far
uint32 crc_so_far
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:757
flush_packet
static void flush_packet(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1575
stb_vorbis_decode_frame_pushdata
int stb_vorbis_decode_frame_pushdata(stb_vorbis *f, const unsigned char *datablock, int datablock_length_in_bytes, int *channels, float ***output, int *samples)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4419
Codebook::delta_value
float delta_value
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:668
stb_vorbis::first_decode
uint8 first_decode
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:863
setup_free
static void setup_free(vorb *f, void *p)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:946
stb_vorbis_get_samples_short
int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5299
ProbedPage::last_decoded_sample
uint32 last_decoded_sample
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:765
stb_vorbis::codebooks
Codebook * codebooks
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:819
vorbis_init
static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4253
MappingChannel::mux
uint8 mux
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:733
Floor1::floor1_multiplier
uint8 floor1_multiplier
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:708
stb_vorbis::error
enum STBVorbisError error
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:811
CRCscan::bytes_left
int bytes_left
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:756
stb_vorbis::codebook_count
int codebook_count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:818
stbv__floor_ordering
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1298
vorbis_deinit
static void vorbis_deinit(stb_vorbis *p)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4183
Floor1
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:697
stb_vorbis_close
void stb_vorbis_close(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4246
neighbors
static void neighbors(uint16 *x, int n, int *plow, int *phigh)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1286
stb_vorbis::previous_length
int previous_length
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:838
stb_vorbis::page_crc_tests
int page_crc_tests
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:876
stb_vorbis::current_loc_valid
int current_loc_valid
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:847
PLAYBACK_MONO
#define PLAYBACK_MONO
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5098
Floor0::amplitude_offset
uint8 amplitude_offset
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:692
stb_vorbis_decode_memory
int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5359
stb_vorbis::blocksize_1
int blocksize_1
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:817
error
static int error(vorb *f, enum STBVorbisError e)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:896
crc_table
static uint32 crc_table[256]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:974
stb_vorbis::setup_temp_memory_required
unsigned int setup_temp_memory_required
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:776
crc32_init
static void crc32_init(void)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:975
prep_huffman
static __forceinline void prep_huffman(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1616
get8_packet_raw
static int get8_packet_raw(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1546
Mapping::submap_residue
uint8 submap_residue[15]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:742
Residue::begin
uint32 begin
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:721
Floor::floor1
Floor1 floor1
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:716
Mode::transformtype
uint16 transformtype
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:750
Mapping::submap_floor
uint8 submap_floor[15]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:741
VORBIS_continued_packet_flag_invalid
@ VORBIS_continued_packet_flag_invalid
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:389
Codebook::entries
int entries
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:665
ProbedPage::page_start
uint32 page_start
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:764
skip
static void skip(vorb *z, int n)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1365
stb_vorbis::packet_bytes
int packet_bytes
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:869
Floor1::class_masterbooks
uint8 class_masterbooks[16]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:703
stb_vorbis::temp_memory_required
unsigned int temp_memory_required
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:775
Floor1::class_dimensions
uint8 class_dimensions[16]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:701
codebook_decode_start
static int codebook_decode_start(vorb *f, Codebook *c)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1757
MappingChannel::magnitude
uint8 magnitude
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:731
header
const std::string header
Floor0::number_of_books
uint8 number_of_books
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:693
assert.h
get_window
static float * get_window(vorb *f, int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3042
next_segment
static int next_segment(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1522
get32_packet
static int get32_packet(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1565
Floor1::subclass_books
int16 subclass_books[16][8]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:704
STB_VORBIS_FAST_HUFFMAN_LENGTH
#define STB_VORBIS_FAST_HUFFMAN_LENGTH
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:475
stb_vorbis_alloc::alloc_buffer
char * alloc_buffer
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:115
add_entry
static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1058
Floor0::bark_map_size
uint16 bark_map_size
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:690
stb_vorbis::total_samples
uint32 total_samples
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:831
stb_vorbis::mapping_count
int mapping_count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:826
Mode
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:745
stb_vorbis::comment_list_length
int comment_list_length
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:779
FAST_HUFFMAN_TABLE_MASK
#define FAST_HUFFMAN_TABLE_MASK
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:661
compute_twiddle_factors
static void compute_twiddle_factors(int n, float *A, float *B, float *C)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1237
stb_vorbis_get_frame_short_interleaved
int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5265
stb_vorbis::mode_count
int mode_count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:828
DECODE
#define DECODE(var, f, c)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1736
VORBIS_need_more_data
@ VORBIS_need_more_data
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:367
VORBIS_seek_failed
@ VORBIS_seek_failed
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:394
PLAYBACK_LEFT
#define PLAYBACK_LEFT
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5099
stbv__floor_ordering::id
uint16 id
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1300
ProbedPage::page_end
uint32 page_end
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:764
start_page
static int start_page(vorb *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1480
temp_alloc
#define temp_alloc(f, size)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:913
stb_vorbis::floor_count
int floor_count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:820
VORBIS_incorrect_stream_serial_number
@ VORBIS_incorrect_stream_serial_number
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:390
Floor1::partitions
uint8 partitions
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:699
stb_vorbis::alloc
stb_vorbis_alloc alloc
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:805
copy_samples
static void copy_samples(short *dest, float *src, int len)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5136
get_seek_page_info
static int get_seek_page_info(stb_vorbis *f, ProbedPage *z)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:4617
vorbis_decode_packet_rest
static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3162
VORBIS_packet_comment
@ VORBIS_packet_comment
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1634
INVALID_BITS
#define INVALID_BITS
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:1544
stb_vorbis_info::temp_memory_required
unsigned int temp_memory_required
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:131
stb_vorbis_info::max_frame_size
int max_frame_size
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:133
Floor1::values
int values
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:710
vorbis_decode_packet
static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3430
vorbis_pump_first_frame
static int vorbis_pump_first_frame(stb_vorbis *f)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:3490
stb_vorbis::blocksize_0
int blocksize_0
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:817
Floor0::book_list
uint8 book_list[16]
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:694
Codebook::multiplicands
codetype * multiplicands
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:674


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:51