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