Logo

index : raylib-jai

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/raylib-jai.git/html/Raylib/raylib/src/external/stb_vorbis.c blob: 3e5c2504c08f76b9d04e4adfbce80818631e835e [raw] [clear marker]

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