stb_vorbis.c
This file
Path
stb_vorbis.c
5,476 lines · 184.8 KiB · C
Last changed by
Merge branch 'kc/fix-coverity-issues-1' of https://github.com/krcroft/stb into work2Sean Barrettbefore adoption4067b6d
History of this fileTake it
The walk stopped after 200 commits: lines older than 5511239 point at it.
stb_vorbis: bump version numberSean Barrettbefore adoption1b2fa11
line 1why, one step back1// Ogg Vorbis audio decoder - v1.17 - public domainMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 2–5why, one step back2// http://nothings.org/stb_vorbis/3//4// Original version written by Sean Barrett in 2007.5//Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 6–8why, one step back6// Originally sponsored by RAD Game Tools. Seeking implementation7// sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker,8// Elias Software, Aras Pranckevicius, and Sean Barrett.Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 9–31why, one step back9//10// LICENSE11//12// See end of file for license information.13//14// Limitations:15//16// - floor 0 not supported (used in old ogg vorbis files pre-2004)17// - lossless sample-truncation at beginning ignored18// - cannot concatenate multiple vorbis streams19// - sample positions are 32-bit, limiting seekable 192Khz20// files to around 6 hours (Ogg supports 64-bit)21//22// Feature contributors:23// Dougall Johnson (sample-exact seeking)24//25// Bugfix/warning contributors:26// Terje Mathisen Niklas Frykholm Andy Hill27// Casey Muratori John Bolton Gargaj28// Laurent Gomila Marc LeBlanc Ronny Chevalier29// Bernhard Wodo Evan Balster alxprd@github30// Tom Beaumont Ingo Leitgeb Nicolas Guillemot31// Phillip Bennefall Rohit Thiago GoulartMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 32why, one step back32// manxorist@github saga musix github:infatumstb_vorbis: bump version numberSean Barrettbefore adoption1b2fa11
line 33why, one step back33// Timur Gagiev Maxwell KooMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 34–35why, one step back34//35// Partial history:Fix seven bugs discovered and fixed by ForAllSecure:Sean Barrettbefore adoption98fdfc6
line 36why, one step back36// 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure)update version numbersSean Barrettbefore adoption2c2908f
line 37why, one step back37// 1.16 - 2019-03-04 - fix warningsupdate version numbersSean Barrettbefore adoption63b59b4
line 38why, one step back38// 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is foundMerge branch 'working'Sean Barrettbefore adoption19850aa
lines 39–47why, one step back39// 1.14 - 2018-02-11 - delete bogus dealloca usage40// 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)41// 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files42// 1.11 - 2017-07-23 - fix MinGW compilation 43// 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory44// 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version45// 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame46// 1.07 - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const47// 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 48–49why, one step back48// some crash fixes when out of memory or with corrupt files49// fix some inappropriately signed shiftsMerge branch 'working'Sean Barrettbefore adoption19850aa
lines 50–55why, one step back50// 1.05 - 2015-04-19 - don't define __forceinline if it's redundant51// 1.04 - 2014-08-27 - fix missing const-correct case in API52// 1.03 - 2014-08-07 - warning fixes53// 1.02 - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows54// 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct)55// 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel;Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 56–258why, one step back56// (API change) report sample rate for decode-full-file funcs57//58// See end of file for full version history.596061//////////////////////////////////////////////////////////////////////////////62//63// HEADER BEGINS HERE64//6566#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H67#define STB_VORBIS_INCLUDE_STB_VORBIS_H6869#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)70#define STB_VORBIS_NO_STDIO 171#endif7273#ifndef STB_VORBIS_NO_STDIO74#include <stdio.h>75#endif7677#ifdef __cplusplus78extern "C" {79#endif8081/////////// THREAD SAFETY8283// Individual stb_vorbis* handles are not thread-safe; you cannot decode from84// them from multiple threads at the same time. However, you can have multiple85// stb_vorbis* handles and decode from them independently in multiple thrads.868788/////////// MEMORY ALLOCATION8990// normally stb_vorbis uses malloc() to allocate memory at startup,91// and alloca() to allocate temporary memory during a frame on the92// stack. (Memory consumption will depend on the amount of setup93// data in the file and how you set the compile flags for speed94// vs. size. In my test files the maximal-size usage is ~150KB.)95//96// You can modify the wrapper functions in the source (setup_malloc,97// setup_temp_malloc, temp_malloc) to change this behavior, or you98// can use a simpler allocation model: you pass in a buffer from99// which stb_vorbis will allocate _all_ its memory (including the100// temp memory). "open" may fail with a VORBIS_outofmem if you101// do not pass in enough data; there is no way to determine how102// much you do need except to succeed (at which point you can103// query get_info to find the exact amount required. yes I know104// this is lame).105//106// If you pass in a non-NULL buffer of the type below, allocation107// will occur from it as described above. Otherwise just pass NULL108// to use malloc()/alloca()109110typedef struct111{112 char *alloc_buffer;113 int alloc_buffer_length_in_bytes;114} stb_vorbis_alloc;115116117/////////// FUNCTIONS USEABLE WITH ALL INPUT MODES118119typedef struct stb_vorbis stb_vorbis;120121typedef struct122{123 unsigned int sample_rate;124 int channels;125126 unsigned int setup_memory_required;127 unsigned int setup_temp_memory_required;128 unsigned int temp_memory_required;129130 int max_frame_size;131} stb_vorbis_info;132133// get general information about the file134extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);135136// get the last error detected (clears it, too)137extern int stb_vorbis_get_error(stb_vorbis *f);138139// close an ogg vorbis file and free all memory in use140extern void stb_vorbis_close(stb_vorbis *f);141142// this function returns the offset (in samples) from the beginning of the143// file that will be returned by the next decode, if it is known, or -1144// otherwise. after a flush_pushdata() call, this may take a while before145// it becomes valid again.146// NOT WORKING YET after a seek with PULLDATA API147extern int stb_vorbis_get_sample_offset(stb_vorbis *f);148149// returns the current seek point within the file, or offset from the beginning150// of the memory buffer. In pushdata mode it returns 0.151extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);152153/////////// PUSHDATA API154155#ifndef STB_VORBIS_NO_PUSHDATA_API156157// this API allows you to get blocks of data from any source and hand158// them to stb_vorbis. you have to buffer them; stb_vorbis will tell159// you how much it used, and you have to give it the rest next time;160// and stb_vorbis may not have enough data to work with and you will161// need to give it the same data again PLUS more. Note that the Vorbis162// specification does not bound the size of an individual frame.163164extern stb_vorbis *stb_vorbis_open_pushdata(165 const unsigned char * datablock, int datablock_length_in_bytes,166 int *datablock_memory_consumed_in_bytes,167 int *error,168 const stb_vorbis_alloc *alloc_buffer);169// create a vorbis decoder by passing in the initial data block containing170// the ogg&vorbis headers (you don't need to do parse them, just provide171// the first N bytes of the file--you're told if it's not enough, see below)172// on success, returns an stb_vorbis *, does not set error, returns the amount of173// data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;174// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed175// if returns NULL and *error is VORBIS_need_more_data, then the input block was176// incomplete and you need to pass in a larger block from the start of the file177178extern int stb_vorbis_decode_frame_pushdata(179 stb_vorbis *f,180 const unsigned char *datablock, int datablock_length_in_bytes,181 int *channels, // place to write number of float * buffers182 float ***output, // place to write float ** array of float * buffers183 int *samples // place to write number of output samples184 );185// decode a frame of audio sample data if possible from the passed-in data block186//187// return value: number of bytes we used from datablock188//189// possible cases:190// 0 bytes used, 0 samples output (need more data)191// N bytes used, 0 samples output (resynching the stream, keep going)192// N bytes used, M samples output (one frame of data)193// note that after opening a file, you will ALWAYS get one N-bytes,0-sample194// frame, because Vorbis always "discards" the first frame.195//196// Note that on resynch, stb_vorbis will rarely consume all of the buffer,197// instead only datablock_length_in_bytes-3 or less. This is because it wants198// to avoid missing parts of a page header if they cross a datablock boundary,199// without writing state-machiney code to record a partial detection.200//201// The number of channels returned are stored in *channels (which can be202// NULL--it is always the same as the number of channels reported by203// get_info). *output will contain an array of float* buffers, one per204// channel. In other words, (*output)[0][0] contains the first sample from205// the first channel, and (*output)[1][0] contains the first sample from206// the second channel.207208extern void stb_vorbis_flush_pushdata(stb_vorbis *f);209// inform stb_vorbis that your next datablock will not be contiguous with210// previous ones (e.g. you've seeked in the data); future attempts to decode211// frames will cause stb_vorbis to resynchronize (as noted above), and212// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it213// will begin decoding the _next_ frame.214//215// if you want to seek using pushdata, you need to seek in your file, then216// call stb_vorbis_flush_pushdata(), then start calling decoding, then once217// decoding is returning you data, call stb_vorbis_get_sample_offset, and218// if you don't like the result, seek your file again and repeat.219#endif220221222////////// PULLING INPUT API223224#ifndef STB_VORBIS_NO_PULLDATA_API225// This API assumes stb_vorbis is allowed to pull data from a source--226// either a block of memory containing the _entire_ vorbis stream, or a227// FILE * that you or it create, or possibly some other reading mechanism228// if you go modify the source to replace the FILE * case with some kind229// of callback to your code. (But if you don't support seeking, you may230// just want to go ahead and use pushdata.)231232#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)233extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);234#endif235#if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)236extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);237#endif238// decode an entire file and output the data interleaved into a malloc()ed239// buffer stored in *output. The return value is the number of samples240// decoded, or -1 if the file could not be opened or was not an ogg vorbis file.241// When you're done with it, just free() the pointer returned in *output.242243extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,244 int *error, const stb_vorbis_alloc *alloc_buffer);245// create an ogg vorbis decoder from an ogg vorbis stream in memory (note246// this must be the entire stream!). on failure, returns NULL and sets *error247248#ifndef STB_VORBIS_NO_STDIO249extern stb_vorbis * stb_vorbis_open_filename(const char *filename,250 int *error, const stb_vorbis_alloc *alloc_buffer);251// create an ogg vorbis decoder from a filename via fopen(). on failure,252// returns NULL and sets *error (possibly to VORBIS_file_open_failure).253254extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,255 int *error, const stb_vorbis_alloc *alloc_buffer);256// create an ogg vorbis decoder from an open FILE *, looking for a stream at257// the _current_ seek point (ftell). on failure, returns NULL and sets *error.258// note that stb_vorbis must "own" this stream; if you seek it in betweenMerge branch 'misc-typos' of https://github.com/luzpaz/stb into workingSean Barrettbefore adoption3005bcf
line 259why, one step back259// calls to stb_vorbis, it will become confused. Moreover, if you attempt toMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 260–379why, one step back260// perform stb_vorbis_seek_*() operations on this file, it will assume it261// owns the _entire_ rest of the file after the start point. Use the next262// function, stb_vorbis_open_file_section(), to limit it.263264extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close,265 int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);266// create an ogg vorbis decoder from an open FILE *, looking for a stream at267// the _current_ seek point (ftell); the stream will be of length 'len' bytes.268// on failure, returns NULL and sets *error. note that stb_vorbis must "own"269// this stream; if you seek it in between calls to stb_vorbis, it will become270// confused.271#endif272273extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);274extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);275// these functions seek in the Vorbis file to (approximately) 'sample_number'.276// after calling seek_frame(), the next call to get_frame_*() will include277// the specified sample. after calling stb_vorbis_seek(), the next call to278// stb_vorbis_get_samples_* will start with the specified sample. If you279// do not need to seek to EXACTLY the target sample when using get_samples_*,280// you can also use seek_frame().281282extern int stb_vorbis_seek_start(stb_vorbis *f);283// this function is equivalent to stb_vorbis_seek(f,0)284285extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);286extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f);287// these functions return the total length of the vorbis stream288289extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);290// decode the next frame and return the number of samples. the number of291// channels returned are stored in *channels (which can be NULL--it is always292// the same as the number of channels reported by get_info). *output will293// contain an array of float* buffers, one per channel. These outputs will294// be overwritten on the next call to stb_vorbis_get_frame_*.295//296// You generally should not intermix calls to stb_vorbis_get_frame_*()297// and stb_vorbis_get_samples_*(), since the latter calls the former.298299#ifndef STB_VORBIS_NO_INTEGER_CONVERSION300extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);301extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples);302#endif303// decode the next frame and return the number of *samples* per channel.304// Note that for interleaved data, you pass in the number of shorts (the305// size of your array), but the return value is the number of samples per306// channel, not the total number of samples.307//308// The data is coerced to the number of channels you request according to the309// channel coercion rules (see below). You must pass in the size of your310// buffer(s) so that stb_vorbis will not overwrite the end of the buffer.311// The maximum buffer size needed can be gotten from get_info(); however,312// the Vorbis I specification implies an absolute maximum of 4096 samples313// per channel.314315// Channel coercion rules:316// Let M be the number of channels requested, and N the number of channels present,317// and Cn be the nth channel; let stereo L be the sum of all L and center channels,318// and stereo R be the sum of all R and center channels (channel assignment from the319// vorbis spec).320// M N output321// 1 k sum(Ck) for all k322// 2 * stereo L, stereo R323// k l k > l, the first l channels, then 0s324// k l k <= l, the first k channels325// Note that this is not _good_ surround etc. mixing at all! It's just so326// you get something useful.327328extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);329extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);330// gets num_samples samples, not necessarily on a frame boundary--this requires331// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.332// Returns the number of samples stored per channel; it may be less than requested333// at the end of the file. If there are no more samples in the file, returns 0.334335#ifndef STB_VORBIS_NO_INTEGER_CONVERSION336extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);337extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);338#endif339// gets num_samples samples, not necessarily on a frame boundary--this requires340// buffering so you have to supply the buffers. Applies the coercion rules above341// to produce 'channels' channels. Returns the number of samples stored per channel;342// it may be less than requested at the end of the file. If there are no more343// samples in the file, returns 0.344345#endif346347//////// ERROR CODES348349enum STBVorbisError350{351 VORBIS__no_error,352353 VORBIS_need_more_data=1, // not a real error354355 VORBIS_invalid_api_mixing, // can't mix API modes356 VORBIS_outofmem, // not enough memory357 VORBIS_feature_not_supported, // uses floor 0358 VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small359 VORBIS_file_open_failure, // fopen() failed360 VORBIS_seek_without_length, // can't seek in unknown-length file361362 VORBIS_unexpected_eof=10, // file is truncated?363 VORBIS_seek_invalid, // seek past EOF364365 // decoding errors (corrupt/invalid stream) -- you probably366 // don't care about the exact details of these367368 // vorbis errors:369 VORBIS_invalid_setup=20,370 VORBIS_invalid_stream,371372 // ogg errors:373 VORBIS_missing_capture_pattern=30,374 VORBIS_invalid_stream_structure_version,375 VORBIS_continued_packet_flag_invalid,376 VORBIS_incorrect_stream_serial_number,377 VORBIS_invalid_first_page,378 VORBIS_bad_packet_type,379 VORBIS_cant_find_last_page,Merge branch 'working'Sean Barrettbefore adoption9092c04
lines 380–381why, one step back380 VORBIS_seek_failed,381 VORBIS_ogg_skeleton_not_supportedMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 382–562why, one step back382};383384385#ifdef __cplusplus386}387#endif388389#endif // STB_VORBIS_INCLUDE_STB_VORBIS_H390//391// HEADER ENDS HERE392//393//////////////////////////////////////////////////////////////////////////////394395#ifndef STB_VORBIS_HEADER_ONLY396397// global configuration settings (e.g. set these in the project/makefile),398// or just set them in this file at the top (although ideally the first few399// should be visible when the header file is compiled too, although it's not400// crucial)401402// STB_VORBIS_NO_PUSHDATA_API403// does not compile the code for the various stb_vorbis_*_pushdata()404// functions405// #define STB_VORBIS_NO_PUSHDATA_API406407// STB_VORBIS_NO_PULLDATA_API408// does not compile the code for the non-pushdata APIs409// #define STB_VORBIS_NO_PULLDATA_API410411// STB_VORBIS_NO_STDIO412// does not compile the code for the APIs that use FILE *s internally413// or externally (implied by STB_VORBIS_NO_PULLDATA_API)414// #define STB_VORBIS_NO_STDIO415416// STB_VORBIS_NO_INTEGER_CONVERSION417// does not compile the code for converting audio sample data from418// float to integer (implied by STB_VORBIS_NO_PULLDATA_API)419// #define STB_VORBIS_NO_INTEGER_CONVERSION420421// STB_VORBIS_NO_FAST_SCALED_FLOAT422// does not use a fast float-to-int trick to accelerate float-to-int on423// most platforms which requires endianness be defined correctly.424//#define STB_VORBIS_NO_FAST_SCALED_FLOAT425426427// STB_VORBIS_MAX_CHANNELS [number]428// globally define this to the maximum number of channels you need.429// The spec does not put a restriction on channels except that430// the count is stored in a byte, so 255 is the hard limit.431// Reducing this saves about 16 bytes per value, so using 16 saves432// (255-16)*16 or around 4KB. Plus anything other memory usage433// I forgot to account for. Can probably go as low as 8 (7.1 audio),434// 6 (5.1 audio), or 2 (stereo only).435#ifndef STB_VORBIS_MAX_CHANNELS436#define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone?437#endif438439// STB_VORBIS_PUSHDATA_CRC_COUNT [number]440// after a flush_pushdata(), stb_vorbis begins scanning for the441// next valid page, without backtracking. when it finds something442// that looks like a page, it streams through it and verifies its443// CRC32. Should that validation fail, it keeps scanning. But it's444// possible that _while_ streaming through to check the CRC32 of445// one candidate page, it sees another candidate page. This #define446// determines how many "overlapping" candidate pages it can search447// at once. Note that "real" pages are typically ~4KB to ~8KB, whereas448// garbage pages could be as big as 64KB, but probably average ~16KB.449// So don't hose ourselves by scanning an apparent 64KB page and450// missing a ton of real ones in the interim; so minimum of 2451#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT452#define STB_VORBIS_PUSHDATA_CRC_COUNT 4453#endif454455// STB_VORBIS_FAST_HUFFMAN_LENGTH [number]456// sets the log size of the huffman-acceleration table. Maximum457// supported value is 24. with larger numbers, more decodings are O(1),458// but the table size is larger so worse cache missing, so you'll have459// to probe (and try multiple ogg vorbis files) to find the sweet spot.460#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH461#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10462#endif463464// STB_VORBIS_FAST_BINARY_LENGTH [number]465// sets the log size of the binary-search acceleration table. this466// is used in similar fashion to the fast-huffman size to set initial467// parameters for the binary search468469// STB_VORBIS_FAST_HUFFMAN_INT470// The fast huffman tables are much more efficient if they can be471// stored as 16-bit results instead of 32-bit results. This restricts472// the codebooks to having only 65535 possible outcomes, though.473// (At least, accelerated by the huffman table.)474#ifndef STB_VORBIS_FAST_HUFFMAN_INT475#define STB_VORBIS_FAST_HUFFMAN_SHORT476#endif477478// STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH479// If the 'fast huffman' search doesn't succeed, then stb_vorbis falls480// back on binary searching for the correct one. This requires storing481// extra tables with the huffman codes in sorted order. Defining this482// symbol trades off space for speed by forcing a linear search in the483// non-fast case, except for "sparse" codebooks.484// #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH485486// STB_VORBIS_DIVIDES_IN_RESIDUE487// stb_vorbis precomputes the result of the scalar residue decoding488// that would otherwise require a divide per chunk. you can trade off489// space for time by defining this symbol.490// #define STB_VORBIS_DIVIDES_IN_RESIDUE491492// STB_VORBIS_DIVIDES_IN_CODEBOOK493// vorbis VQ codebooks can be encoded two ways: with every case explicitly494// stored, or with all elements being chosen from a small range of values,495// and all values possible in all elements. By default, stb_vorbis expands496// this latter kind out to look like the former kind for ease of decoding,497// because otherwise an integer divide-per-vector-element is required to498// unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can499// trade off storage for speed.500//#define STB_VORBIS_DIVIDES_IN_CODEBOOK501502#ifdef STB_VORBIS_CODEBOOK_SHORTS503#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"504#endif505506// STB_VORBIS_DIVIDE_TABLE507// this replaces small integer divides in the floor decode loop with508// table lookups. made less than 1% difference, so disabled by default.509510// STB_VORBIS_NO_INLINE_DECODE511// disables the inlining of the scalar codebook fast-huffman decode.512// might save a little codespace; useful for debugging513// #define STB_VORBIS_NO_INLINE_DECODE514515// STB_VORBIS_NO_DEFER_FLOOR516// Normally we only decode the floor without synthesizing the actual517// full curve. We can instead synthesize the curve immediately. This518// requires more memory and is very likely slower, so I don't think519// you'd ever want to do it except for debugging.520// #define STB_VORBIS_NO_DEFER_FLOOR521522523524525//////////////////////////////////////////////////////////////////////////////526527#ifdef STB_VORBIS_NO_PULLDATA_API528 #define STB_VORBIS_NO_INTEGER_CONVERSION529 #define STB_VORBIS_NO_STDIO530#endif531532#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)533 #define STB_VORBIS_NO_STDIO 1534#endif535536#ifndef STB_VORBIS_NO_INTEGER_CONVERSION537#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT538539 // only need endianness for fast-float-to-int, which we don't540 // use for pushdata541542 #ifndef STB_VORBIS_BIG_ENDIAN543 #define STB_VORBIS_ENDIAN 0544 #else545 #define STB_VORBIS_ENDIAN 1546 #endif547548#endif549#endif550551552#ifndef STB_VORBIS_NO_STDIO553#include <stdio.h>554#endif555556#ifndef STB_VORBIS_NO_CRT557 #include <stdlib.h>558 #include <string.h>559 #include <assert.h>560 #include <math.h>561562 // find definition of alloca if it's not in stdlib.h:Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 563why, one step back563 #if defined(_MSC_VER) || defined(__MINGW32__)Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 564–587why, one step back564 #include <malloc.h>565 #endif566 #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)567 #include <alloca.h>568 #endif569#else // STB_VORBIS_NO_CRT570 #define NULL 0571 #define malloc(s) 0572 #define free(s) ((void) 0)573 #define realloc(s) 0574#endif // STB_VORBIS_NO_CRT575576#include <limits.h>577578#ifdef __MINGW32__579 // eff you mingw:580 // "fixed":581 // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/582 // "no that broke the build, reverted, who cares about C":583 // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/584 #ifdef __forceinline585 #undef __forceinline586 #endif587 #define __forceinlineMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 588why, one step back588 #define alloca __builtin_allocaMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 589–776why, one step back589#elif !defined(_MSC_VER)590 #if __GNUC__591 #define __forceinline inline592 #else593 #define __forceinline594 #endif595#endif596597#if STB_VORBIS_MAX_CHANNELS > 256598#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"599#endif600601#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24602#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"603#endif604605606#if 0607#include <crtdbg.h>608#define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])609#else610#define CHECK(f) ((void) 0)611#endif612613#define MAX_BLOCKSIZE_LOG 13 // from specification614#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)615616617typedef unsigned char uint8;618typedef signed char int8;619typedef unsigned short uint16;620typedef signed short int16;621typedef unsigned int uint32;622typedef signed int int32;623624#ifndef TRUE625#define TRUE 1626#define FALSE 0627#endif628629typedef float codetype;630631// @NOTE632//633// Some arrays below are tagged "//varies", which means it's actually634// a variable-sized piece of data, but rather than malloc I assume it's635// small enough it's better to just allocate it all together with the636// main thing637//638// Most of the variables are specified with the smallest size I could pack639// them into. It might give better performance to make them all full-sized640// integers. It should be safe to freely rearrange the structures or change641// the sizes larger--nothing relies on silently truncating etc., nor the642// order of variables.643644#define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)645#define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1)646647typedef struct648{649 int dimensions, entries;650 uint8 *codeword_lengths;651 float minimum_value;652 float delta_value;653 uint8 value_bits;654 uint8 lookup_type;655 uint8 sequence_p;656 uint8 sparse;657 uint32 lookup_values;658 codetype *multiplicands;659 uint32 *codewords;660 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT661 int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];662 #else663 int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];664 #endif665 uint32 *sorted_codewords;666 int *sorted_values;667 int sorted_entries;668} Codebook;669670typedef struct671{672 uint8 order;673 uint16 rate;674 uint16 bark_map_size;675 uint8 amplitude_bits;676 uint8 amplitude_offset;677 uint8 number_of_books;678 uint8 book_list[16]; // varies679} Floor0;680681typedef struct682{683 uint8 partitions;684 uint8 partition_class_list[32]; // varies685 uint8 class_dimensions[16]; // varies686 uint8 class_subclasses[16]; // varies687 uint8 class_masterbooks[16]; // varies688 int16 subclass_books[16][8]; // varies689 uint16 Xlist[31*8+2]; // varies690 uint8 sorted_order[31*8+2];691 uint8 neighbors[31*8+2][2];692 uint8 floor1_multiplier;693 uint8 rangebits;694 int values;695} Floor1;696697typedef union698{699 Floor0 floor0;700 Floor1 floor1;701} Floor;702703typedef struct704{705 uint32 begin, end;706 uint32 part_size;707 uint8 classifications;708 uint8 classbook;709 uint8 **classdata;710 int16 (*residue_books)[8];711} Residue;712713typedef struct714{715 uint8 magnitude;716 uint8 angle;717 uint8 mux;718} MappingChannel;719720typedef struct721{722 uint16 coupling_steps;723 MappingChannel *chan;724 uint8 submaps;725 uint8 submap_floor[15]; // varies726 uint8 submap_residue[15]; // varies727} Mapping;728729typedef struct730{731 uint8 blockflag;732 uint8 mapping;733 uint16 windowtype;734 uint16 transformtype;735} Mode;736737typedef struct738{739 uint32 goal_crc; // expected crc if match740 int bytes_left; // bytes left in packet741 uint32 crc_so_far; // running crc742 int bytes_done; // bytes processed in _current_ chunk743 uint32 sample_loc; // granule pos encoded in page744} CRCscan;745746typedef struct747{748 uint32 page_start, page_end;749 uint32 last_decoded_sample;750} ProbedPage;751752struct stb_vorbis753{754 // user-accessible info755 unsigned int sample_rate;756 int channels;757758 unsigned int setup_memory_required;759 unsigned int temp_memory_required;760 unsigned int setup_temp_memory_required;761762 // input config763#ifndef STB_VORBIS_NO_STDIO764 FILE *f;765 uint32 f_start;766 int close_on_free;767#endif768769 uint8 *stream;770 uint8 *stream_start;771 uint8 *stream_end;772773 uint32 stream_len;774775 uint8 push_mode;776Merge branch 'vorbis_seek_fixes' of https://github.com/dougallj/stb into work2Sean Barrettbefore adoptiond882f54
line 777why, one step back777 // the page to seek to when seeking to start, may be zeroMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 778–779why, one step back778 uint32 first_audio_page_offset;779Merge branch 'vorbis_seek_fixes' of https://github.com/dougallj/stb into work2Sean Barrettbefore adoptiond882f54
lines 780–781why, one step back780 // p_first is the page on which the first audio packet ends781 // (but not necessarily the page on which it starts)Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 782–1082why, one step back782 ProbedPage p_first, p_last;783784 // memory management785 stb_vorbis_alloc alloc;786 int setup_offset;787 int temp_offset;788789 // run-time results790 int eof;791 enum STBVorbisError error;792793 // user-useful data794795 // header info796 int blocksize[2];797 int blocksize_0, blocksize_1;798 int codebook_count;799 Codebook *codebooks;800 int floor_count;801 uint16 floor_types[64]; // varies802 Floor *floor_config;803 int residue_count;804 uint16 residue_types[64]; // varies805 Residue *residue_config;806 int mapping_count;807 Mapping *mapping;808 int mode_count;809 Mode mode_config[64]; // varies810811 uint32 total_samples;812813 // decode buffer814 float *channel_buffers[STB_VORBIS_MAX_CHANNELS];815 float *outputs [STB_VORBIS_MAX_CHANNELS];816817 float *previous_window[STB_VORBIS_MAX_CHANNELS];818 int previous_length;819820 #ifndef STB_VORBIS_NO_DEFER_FLOOR821 int16 *finalY[STB_VORBIS_MAX_CHANNELS];822 #else823 float *floor_buffers[STB_VORBIS_MAX_CHANNELS];824 #endif825826 uint32 current_loc; // sample location of next frame to decode827 int current_loc_valid;828829 // per-blocksize precomputed data830 831 // twiddle factors832 float *A[2],*B[2],*C[2];833 float *window[2];834 uint16 *bit_reverse[2];835836 // current page/packet/segment streaming info837 uint32 serial; // stream serial number for verification838 int last_page;839 int segment_count;840 uint8 segments[255];841 uint8 page_flag;842 uint8 bytes_in_seg;843 uint8 first_decode;844 int next_seg;845 int last_seg; // flag that we're on the last segment846 int last_seg_which; // what was the segment number of the last seg?847 uint32 acc;848 int valid_bits;849 int packet_bytes;850 int end_seg_with_known_loc;851 uint32 known_loc_for_packet;852 int discard_samples_deferred;853 uint32 samples_output;854855 // push mode scanning856 int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching857#ifndef STB_VORBIS_NO_PUSHDATA_API858 CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];859#endif860861 // sample-access862 int channel_buffer_start;863 int channel_buffer_end;864};865866#if defined(STB_VORBIS_NO_PUSHDATA_API)867 #define IS_PUSH_MODE(f) FALSE868#elif defined(STB_VORBIS_NO_PULLDATA_API)869 #define IS_PUSH_MODE(f) TRUE870#else871 #define IS_PUSH_MODE(f) ((f)->push_mode)872#endif873874typedef struct stb_vorbis vorb;875876static int error(vorb *f, enum STBVorbisError e)877{878 f->error = e;879 if (!f->eof && e != VORBIS_need_more_data) {880 f->error=e; // breakpoint for debugging881 }882 return 0;883}884885886// these functions are used for allocating temporary memory887// while decoding. if you can afford the stack space, use888// alloca(); otherwise, provide a temp buffer and it will889// allocate out of those.890891#define array_size_required(count,size) (count*(sizeof(void *)+(size)))892893#define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))894#define temp_free(f,p) 0895#define temp_alloc_save(f) ((f)->temp_offset)896#define temp_alloc_restore(f,p) ((f)->temp_offset = (p))897898#define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)899900// given a sufficiently large block of memory, make an array of pointers to subblocks of it901static void *make_block_array(void *mem, int count, int size)902{903 int i;904 void ** p = (void **) mem;905 char *q = (char *) (p + count);906 for (i=0; i < count; ++i) {907 p[i] = q;908 q += size;909 }910 return p;911}912913static void *setup_malloc(vorb *f, int sz)914{915 sz = (sz+3) & ~3;916 f->setup_memory_required += sz;917 if (f->alloc.alloc_buffer) {918 void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;919 if (f->setup_offset + sz > f->temp_offset) return NULL;920 f->setup_offset += sz;921 return p;922 }923 return sz ? malloc(sz) : NULL;924}925926static void setup_free(vorb *f, void *p)927{928 if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack929 free(p);930}931932static void *setup_temp_malloc(vorb *f, int sz)933{934 sz = (sz+3) & ~3;935 if (f->alloc.alloc_buffer) {936 if (f->temp_offset - sz < f->setup_offset) return NULL;937 f->temp_offset -= sz;938 return (char *) f->alloc.alloc_buffer + f->temp_offset;939 }940 return malloc(sz);941}942943static void setup_temp_free(vorb *f, void *p, int sz)944{945 if (f->alloc.alloc_buffer) {946 f->temp_offset += (sz+3)&~3;947 return;948 }949 free(p);950}951952#define CRC32_POLY 0x04c11db7 // from spec953954static uint32 crc_table[256];955static void crc32_init(void)956{957 int i,j;958 uint32 s;959 for(i=0; i < 256; i++) {960 for (s=(uint32) i << 24, j=0; j < 8; ++j)961 s = (s << 1) ^ (s >= (1U<<31) ? CRC32_POLY : 0);962 crc_table[i] = s;963 }964}965966static __forceinline uint32 crc32_update(uint32 crc, uint8 byte)967{968 return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];969}970971972// used in setup, and for huffman that doesn't go fast path973static unsigned int bit_reverse(unsigned int n)974{975 n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);976 n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);977 n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);978 n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);979 return (n >> 16) | (n << 16);980}981982static float square(float x)983{984 return x*x;985}986987// this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3988// as required by the specification. fast(?) implementation from stb.h989// @OPTIMIZE: called multiple times per-packet with "constants"; move to setup990static int ilog(int32 n)991{992 static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };993994 if (n < 0) return 0; // signed n returns 0995996 // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)997 if (n < (1 << 14))998 if (n < (1 << 4)) return 0 + log2_4[n ];999 else if (n < (1 << 9)) return 5 + log2_4[n >> 5];1000 else return 10 + log2_4[n >> 10];1001 else if (n < (1 << 24))1002 if (n < (1 << 19)) return 15 + log2_4[n >> 15];1003 else return 20 + log2_4[n >> 20];1004 else if (n < (1 << 29)) return 25 + log2_4[n >> 25];1005 else return 30 + log2_4[n >> 30];1006}10071008#ifndef M_PI1009 #define M_PI 3.14159265358979323846264f // from CRC1010#endif10111012// code length assigned to a value with no huffman encoding1013#define NO_CODE 25510141015/////////////////////// LEAF SETUP FUNCTIONS //////////////////////////1016//1017// these functions are only called at setup, and only a few times1018// per file10191020static float float32_unpack(uint32 x)1021{1022 // from the specification1023 uint32 mantissa = x & 0x1fffff;1024 uint32 sign = x & 0x80000000;1025 uint32 exp = (x & 0x7fe00000) >> 21;1026 double res = sign ? -(double)mantissa : (double)mantissa;1027 return (float) ldexp((float)res, exp-788);1028}102910301031// zlib & jpeg huffman tables assume that the output symbols1032// can either be arbitrarily arranged, or have monotonically1033// increasing frequencies--they rely on the lengths being sorted;1034// this makes for a very simple generation algorithm.1035// vorbis allows a huffman table with non-sorted lengths. This1036// requires a more sophisticated construction, since symbols in1037// order do not map to huffman codes "in order".1038static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values)1039{1040 if (!c->sparse) {1041 c->codewords [symbol] = huff_code;1042 } else {1043 c->codewords [count] = huff_code;1044 c->codeword_lengths[count] = len;1045 values [count] = symbol;1046 }1047}10481049static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)1050{1051 int i,k,m=0;1052 uint32 available[32];10531054 memset(available, 0, sizeof(available));1055 // find the first entry1056 for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;1057 if (k == n) { assert(c->sorted_entries == 0); return TRUE; }1058 // add to the list1059 add_entry(c, 0, k, m++, len[k], values);1060 // add all available leaves1061 for (i=1; i <= len[k]; ++i)1062 available[i] = 1U << (32-i);1063 // note that the above code treats the first case specially,1064 // but it's really the same as the following code, so they1065 // could probably be combined (except the initial code is 0,1066 // and I use 0 in available[] to mean 'empty')1067 for (i=k+1; i < n; ++i) {1068 uint32 res;1069 int z = len[i], y;1070 if (z == NO_CODE) continue;1071 // find lowest available leaf (should always be earliest,1072 // which is what the specification calls for)1073 // note that this property, and the fact we can never have1074 // more than one free leaf at a given level, isn't totally1075 // trivial to prove, but it seems true and the assert never1076 // fires, so!1077 while (z > 0 && !available[z]) --z;1078 if (z == 0) { return FALSE; }1079 res = available[z];1080 assert(z >= 0 && z < 32);1081 available[z] = 0;1082 add_entry(c, bit_reverse(res), i, m++, len[i], values);Merge branch 'misc-typos' of https://github.com/luzpaz/stb into workingSean Barrettbefore adoption3005bcf
line 1083why, one step back1083 // propagate availability up the treeMerge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 1084–1208why, one step back1084 if (z != len[i]) {1085 assert(len[i] >= 0 && len[i] < 32);1086 for (y=len[i]; y > z; --y) {1087 assert(available[y] == 0);1088 available[y] = res + (1 << (32-y));1089 }1090 }1091 }1092 return TRUE;1093}10941095// accelerated huffman table allows fast O(1) match of all symbols1096// of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH1097static void compute_accelerated_huffman(Codebook *c)1098{1099 int i, len;1100 for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)1101 c->fast_huffman[i] = -1;11021103 len = c->sparse ? c->sorted_entries : c->entries;1104 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT1105 if (len > 32767) len = 32767; // largest possible value we can encode!1106 #endif1107 for (i=0; i < len; ++i) {1108 if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {1109 uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];1110 // set table entries for all bit combinations in the higher bits1111 while (z < FAST_HUFFMAN_TABLE_SIZE) {1112 c->fast_huffman[z] = i;1113 z += 1 << c->codeword_lengths[i];1114 }1115 }1116 }1117}11181119#ifdef _MSC_VER1120#define STBV_CDECL __cdecl1121#else1122#define STBV_CDECL1123#endif11241125static int STBV_CDECL uint32_compare(const void *p, const void *q)1126{1127 uint32 x = * (uint32 *) p;1128 uint32 y = * (uint32 *) q;1129 return x < y ? -1 : x > y;1130}11311132static int include_in_sort(Codebook *c, uint8 len)1133{1134 if (c->sparse) { assert(len != NO_CODE); return TRUE; }1135 if (len == NO_CODE) return FALSE;1136 if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;1137 return FALSE;1138}11391140// if the fast table above doesn't work, we want to binary1141// search them... need to reverse the bits1142static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)1143{1144 int i, len;1145 // build a list of all the entries1146 // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.1147 // this is kind of a frivolous optimization--I don't see any performance improvement,1148 // but it's like 4 extra lines of code, so.1149 if (!c->sparse) {1150 int k = 0;1151 for (i=0; i < c->entries; ++i)1152 if (include_in_sort(c, lengths[i])) 1153 c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);1154 assert(k == c->sorted_entries);1155 } else {1156 for (i=0; i < c->sorted_entries; ++i)1157 c->sorted_codewords[i] = bit_reverse(c->codewords[i]);1158 }11591160 qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);1161 c->sorted_codewords[c->sorted_entries] = 0xffffffff;11621163 len = c->sparse ? c->sorted_entries : c->entries;1164 // now we need to indicate how they correspond; we could either1165 // #1: sort a different data structure that says who they correspond to1166 // #2: for each sorted entry, search the original list to find who corresponds1167 // #3: for each original entry, find the sorted entry1168 // #1 requires extra storage, #2 is slow, #3 can use binary search!1169 for (i=0; i < len; ++i) {1170 int huff_len = c->sparse ? lengths[values[i]] : lengths[i];1171 if (include_in_sort(c,huff_len)) {1172 uint32 code = bit_reverse(c->codewords[i]);1173 int x=0, n=c->sorted_entries;1174 while (n > 1) {1175 // invariant: sc[x] <= code < sc[x+n]1176 int m = x + (n >> 1);1177 if (c->sorted_codewords[m] <= code) {1178 x = m;1179 n -= (n>>1);1180 } else {1181 n >>= 1;1182 }1183 }1184 assert(c->sorted_codewords[x] == code);1185 if (c->sparse) {1186 c->sorted_values[x] = values[i];1187 c->codeword_lengths[x] = huff_len;1188 } else {1189 c->sorted_values[x] = i;1190 }1191 }1192 }1193}11941195// only run while parsing the header (3 times)1196static int vorbis_validate(uint8 *data)1197{1198 static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };1199 return memcmp(data, vorbis, 6) == 0;1200}12011202// called from setup only, once per code book1203// (formula implied by specification)1204static int lookup1_values(int entries, int dim)1205{1206 int r = (int) floor(exp((float) log((float) entries) / dim));1207 if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;1208 ++r; // floor() to avoid _ftol() when non-CRTFix seven bugs discovered and fixed by ForAllSecure:Sean Barrettbefore adoption98fdfc6
lines 1209–1212why, one step back1209 if (pow((float) r+1, dim) <= entries)1210 return -1;1211 if ((int) floor(pow((float) r, dim)) > entries)1212 return -1;Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 1213–1409why, one step back1213 return r;1214}12151216// called twice per file1217static void compute_twiddle_factors(int n, float *A, float *B, float *C)1218{1219 int n4 = n >> 2, n8 = n >> 3;1220 int k,k2;12211222 for (k=k2=0; k < n4; ++k,k2+=2) {1223 A[k2 ] = (float) cos(4*k*M_PI/n);1224 A[k2+1] = (float) -sin(4*k*M_PI/n);1225 B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f;1226 B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f;1227 }1228 for (k=k2=0; k < n8; ++k,k2+=2) {1229 C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);1230 C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);1231 }1232}12331234static void compute_window(int n, float *window)1235{1236 int n2 = n >> 1, i;1237 for (i=0; i < n2; ++i)1238 window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));1239}12401241static void compute_bitreverse(int n, uint16 *rev)1242{1243 int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions1244 int i, n8 = n >> 3;1245 for (i=0; i < n8; ++i)1246 rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2;1247}12481249static int init_blocksize(vorb *f, int b, int n)1250{1251 int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;1252 f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);1253 f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);1254 f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);1255 if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);1256 compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);1257 f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);1258 if (!f->window[b]) return error(f, VORBIS_outofmem);1259 compute_window(n, f->window[b]);1260 f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);1261 if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);1262 compute_bitreverse(n, f->bit_reverse[b]);1263 return TRUE;1264}12651266static void neighbors(uint16 *x, int n, int *plow, int *phigh)1267{1268 int low = -1;1269 int high = 65536;1270 int i;1271 for (i=0; i < n; ++i) {1272 if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; }1273 if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }1274 }1275}12761277// this has been repurposed so y is now the original index instead of y1278typedef struct1279{1280 uint16 x,id;1281} stbv__floor_ordering;12821283static int STBV_CDECL point_compare(const void *p, const void *q)1284{1285 stbv__floor_ordering *a = (stbv__floor_ordering *) p;1286 stbv__floor_ordering *b = (stbv__floor_ordering *) q;1287 return a->x < b->x ? -1 : a->x > b->x;1288}12891290//1291/////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////129212931294#if defined(STB_VORBIS_NO_STDIO)1295 #define USE_MEMORY(z) TRUE1296#else1297 #define USE_MEMORY(z) ((z)->stream)1298#endif12991300static uint8 get8(vorb *z)1301{1302 if (USE_MEMORY(z)) {1303 if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }1304 return *z->stream++;1305 }13061307 #ifndef STB_VORBIS_NO_STDIO1308 {1309 int c = fgetc(z->f);1310 if (c == EOF) { z->eof = TRUE; return 0; }1311 return c;1312 }1313 #endif1314}13151316static uint32 get32(vorb *f)1317{1318 uint32 x;1319 x = get8(f);1320 x += get8(f) << 8;1321 x += get8(f) << 16;1322 x += (uint32) get8(f) << 24;1323 return x;1324}13251326static int getn(vorb *z, uint8 *data, int n)1327{1328 if (USE_MEMORY(z)) {1329 if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }1330 memcpy(data, z->stream, n);1331 z->stream += n;1332 return 1;1333 }13341335 #ifndef STB_VORBIS_NO_STDIO 1336 if (fread(data, n, 1, z->f) == 1)1337 return 1;1338 else {1339 z->eof = 1;1340 return 0;1341 }1342 #endif1343}13441345static void skip(vorb *z, int n)1346{1347 if (USE_MEMORY(z)) {1348 z->stream += n;1349 if (z->stream >= z->stream_end) z->eof = 1;1350 return;1351 }1352 #ifndef STB_VORBIS_NO_STDIO1353 {1354 long x = ftell(z->f);1355 fseek(z->f, x+n, SEEK_SET);1356 }1357 #endif1358}13591360static int set_file_offset(stb_vorbis *f, unsigned int loc)1361{1362 #ifndef STB_VORBIS_NO_PUSHDATA_API1363 if (f->push_mode) return 0;1364 #endif1365 f->eof = 0;1366 if (USE_MEMORY(f)) {1367 if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {1368 f->stream = f->stream_end;1369 f->eof = 1;1370 return 0;1371 } else {1372 f->stream = f->stream_start + loc;1373 return 1;1374 }1375 }1376 #ifndef STB_VORBIS_NO_STDIO1377 if (loc + f->f_start < loc || loc >= 0x80000000) {1378 loc = 0x7fffffff;1379 f->eof = 1;1380 } else {1381 loc += f->f_start;1382 }1383 if (!fseek(f->f, loc, SEEK_SET))1384 return 1;1385 f->eof = 1;1386 fseek(f->f, f->f_start, SEEK_END);1387 return 0;1388 #endif1389}139013911392static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };13931394static int capture_pattern(vorb *f)1395{1396 if (0x4f != get8(f)) return FALSE;1397 if (0x67 != get8(f)) return FALSE;1398 if (0x67 != get8(f)) return FALSE;1399 if (0x53 != get8(f)) return FALSE;1400 return TRUE;1401}14021403#define PAGEFLAG_continued_packet 11404#define PAGEFLAG_first_page 21405#define PAGEFLAG_last_page 414061407static int start_page_no_capturepattern(vorb *f)1408{1409 uint32 loc0,loc1,n;Merge branch 'vorbis_seek_fixes' of https://github.com/dougallj/stb into work2Sean Barrettbefore adoptiond882f54
lines 1410–1412why, one step back1410 if (f->first_decode && !IS_PUSH_MODE(f)) {1411 f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4;1412 }Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 1413–1452why, one step back1413 // stream structure version1414 if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);1415 // header flag1416 f->page_flag = get8(f);1417 // absolute granule position1418 loc0 = get32(f); 1419 loc1 = get32(f);1420 // @TODO: validate loc0,loc1 as valid positions?1421 // stream serial number -- vorbis doesn't interleave, so discard1422 get32(f);1423 //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);1424 // page sequence number1425 n = get32(f);1426 f->last_page = n;1427 // CRC321428 get32(f);1429 // page_segments1430 f->segment_count = get8(f);1431 if (!getn(f, f->segments, f->segment_count))1432 return error(f, VORBIS_unexpected_eof);1433 // assume we _don't_ know any the sample position of any segments1434 f->end_seg_with_known_loc = -2;1435 if (loc0 != ~0U || loc1 != ~0U) {1436 int i;1437 // determine which packet is the last one that will complete1438 for (i=f->segment_count-1; i >= 0; --i)1439 if (f->segments[i] < 255)1440 break;1441 // 'i' is now the index of the _last_ segment of a packet that ends1442 if (i >= 0) {1443 f->end_seg_with_known_loc = i;1444 f->known_loc_for_packet = loc0;1445 }1446 }1447 if (f->first_decode) {1448 int i,len;1449 len = 0;1450 for (i=0; i < f->segment_count; ++i)1451 len += f->segments[i];1452 len += 27 + f->segment_count;Merge branch 'vorbis_seek_fixes' of https://github.com/dougallj/stb into work2Sean Barrettbefore adoptiond882f54
lines 1453–1454why, one step back1453 f->p_first.page_end = f->p_first.page_start + len;1454 f->p_first.last_decoded_sample = loc0;Merge branch 'master' of https://github.com/uTox/stb into workingSean Barrettbefore adoption5511239
lines 1455–2021why, one step back1455 }1456 f->next_seg = 0;1457 return TRUE;1458}14591460static int start_page(vorb *f)1461{1462 if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);1463 return start_page_no_capturepattern(f);1464}14651466static int start_packet(vorb *f)1467{1468 while (f->next_seg == -1) {1469 if (!start_page(f)) return FALSE;1470 if (f->page_flag & PAGEFLAG_continued_packet)1471 return error(f, VORBIS_continued_packet_flag_invalid);1472 }1473 f->last_seg = FALSE;1474 f->valid_bits = 0;1475 f->packet_bytes = 0;1476 f->bytes_in_seg = 0;1477 // f->next_seg is now valid1478 return TRUE;1479}14801481static int maybe_start_packet(vorb *f)1482{1483 if (f->next_seg == -1) {1484 int x = get8(f);1485 if (f->eof) return FALSE; // EOF at page boundary is not an error!1486 if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern);1487 if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);1488 if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);1489 if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);1490 if (!start_page_no_capturepattern(f)) return FALSE;1491 if (f->page_flag & PAGEFLAG_continued_packet) {1492 // set up enough state that we can read this packet if we want,1493 // e.g. during recovery1494 f->last_seg = FALSE;1495 f->bytes_in_seg = 0;1496 return error(f, VORBIS_continued_packet_flag_invalid);1497 }1498 }1499 return start_packet(f);1500}15011502static int next_segment(vorb *f)1503{1504 int len;1505 if (f->last_seg) return 0;1506 if (f->next_seg == -1) {1507 f->last_seg_which = f->segment_count-1; // in case start_page fails1508 if (!start_page(f)) { f->last_seg = 1; return 0; }1509 if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);1510 }1511 len = f->segments[f->next_seg++];1512 if (len < 255) {1513 f->last_seg = TRUE;1514 f->last_seg_which = f->next_seg-1;1515 }1516 if (f->next_seg >= f->segment_count)1517 f->next_seg = -1;1518 assert(f->bytes_in_seg == 0);1519 f->bytes_in_seg = len;1520 return len;1521}15221523#define EOP (-1)1524#define INVALID_BITS (-1)15251526static int get8_packet_raw(vorb *f)1527{1528 if (!f->bytes_in_seg) { // CLANG!1529 if (f->last_seg) return EOP;1530 else if (!next_segment(f)) return EOP;1531 }1532 assert(f->bytes_in_seg > 0);1533 --f->bytes_in_seg;1534 ++f->packet_bytes;1535 return get8(f);1536}15371538static int get8_packet(vorb *f)1539{1540 int x = get8_packet_raw(f);1541 f->valid_bits = 0;1542 return x;1543}15441545static void flush_packet(vorb *f)1546{1547 while (get8_packet_raw(f) != EOP);1548}15491550// @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important1551// as the huffman decoder?1552static uint32 get_bits(vorb *f, int n)1553{1554 uint32 z;15551556 if (f->valid_bits < 0) return 0;1557 if (f->valid_bits < n) {1558 if (n > 24) {1559 // the accumulator technique below would not work correctly in this case1560 z = get_bits(f, 24);1561 z += get_bits(f, n-24) << 24;1562 return z;1563 }1564 if (f->valid_bits == 0) f->acc = 0;1565 while (f->valid_bits < n) {1566 int z = get8_packet_raw(f);1567 if (z == EOP) {1568 f->valid_bits = INVALID_BITS;1569 return 0;1570 }1571 f->acc += z << f->valid_bits;1572 f->valid_bits += 8;1573 }1574 }1575 if (f->valid_bits < 0) return 0;1576 z = f->acc & ((1 << n)-1);1577 f->acc >>= n;1578 f->valid_bits -= n;1579 return z;1580}15811582// @OPTIMIZE: primary accumulator for huffman1583// expand the buffer to as many bits as possible without reading off end of packet1584// it might be nice to allow f->valid_bits and f->acc to be stored in registers,1585// e.g. cache them locally and decode locally1586static __forceinline void prep_huffman(vorb *f)1587{1588 if (f->valid_bits <= 24) {1589 if (f->valid_bits == 0) f->acc = 0;1590 do {1591 int z;1592 if (f->last_seg && !f->bytes_in_seg) return;1593 z = get8_packet_raw(f);1594 if (z == EOP) return;1595 f->acc += (unsigned) z << f->valid_bits;1596 f->valid_bits += 8;1597 } while (f->valid_bits <= 24);1598 }1599}16001601enum1602{1603 VORBIS_packet_id = 1,1604 VORBIS_packet_comment = 3,1605 VORBIS_packet_setup = 51606};16071608static int codebook_decode_scalar_raw(vorb *f, Codebook *c)1609{1610 int i;1611 prep_huffman(f);16121613 if (c->codewords == NULL && c->sorted_codewords == NULL)1614 return -1;16151616 // cases to use binary search: sorted_codewords && !c->codewords1617 // sorted_codewords && c->entries > 81618 if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) {1619 // binary search1620 uint32 code = bit_reverse(f->acc);1621 int x=0, n=c->sorted_entries, len;16221623 while (n > 1) {1624 // invariant: sc[x] <= code < sc[x+n]1625 int m = x + (n >> 1);1626 if (c->sorted_codewords[m] <= code) {1627 x = m;1628 n -= (n>>1);1629 } else {1630 n >>= 1;1631 }1632 }1633 // x is now the sorted index1634 if (!c->sparse) x = c->sorted_values[x];1635 // x is now sorted index if sparse, or symbol otherwise1636 len = c->codeword_lengths[x];1637 if (f->valid_bits >= len) {1638 f->acc >>= len;1639 f->valid_bits -= len;1640 return x;1641 }16421643 f->valid_bits = 0;1644 return -1;1645 }16461647 // if small, linear search1648 assert(!c->sparse);1649 for (i=0; i < c->entries; ++i) {1650 if (c->codeword_lengths[i] == NO_CODE) continue;1651 if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) {1652 if (f->valid_bits >= c->codeword_lengths[i]) {1653 f->acc >>= c->codeword_lengths[i];1654 f->valid_bits -= c->codeword_lengths[i];1655 return i;1656 }1657 f->valid_bits = 0;1658 return -1;1659 }1660 }16611662 error(f, VORBIS_invalid_stream);1663 f->valid_bits = 0;1664 return -1;1665}16661667#ifndef STB_VORBIS_NO_INLINE_DECODE16681669#define DECODE_RAW(var, f,c) \1670 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \1671 prep_huffman(f); \1672 var = f->acc & FAST_HUFFMAN_TABLE_MASK; \1673 var = c->fast_huffman[var]; \1674 if (var >= 0) { \1675 int n = c->codeword_lengths[var]; \1676 f->acc >>= n; \1677 f->valid_bits -= n; \1678 if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \1679 } else { \1680 var = codebook_decode_scalar_raw(f,c); \1681 }16821683#else16841685static int codebook_decode_scalar(vorb *f, Codebook *c)1686{1687 int i;1688 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)1689 prep_huffman(f);1690 // fast huffman table lookup1691 i = f->acc & FAST_HUFFMAN_TABLE_MASK;1692 i = c->fast_huffman[i];1693 if (i >= 0) {1694 f->acc >>= c->codeword_lengths[i];1695 f->valid_bits -= c->codeword_lengths[i];1696 if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }1697 return i;1698 }1699 return codebook_decode_scalar_raw(f,c);1700}17011702#define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);17031704#endif17051706#define DECODE(var,f,c) \1707 DECODE_RAW(var,f,c) \1708 if (c->sparse) var = c->sorted_values[var];17091710#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK1711 #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)1712#else1713 #define DECODE_VQ(var,f,c) DECODE(var,f,c)1714#endif1715171617171718171917201721// CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case1722// where we avoid one addition1723#define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])1724#define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])1725#define CODEBOOK_ELEMENT_BASE(c) (0)17261727static int codebook_decode_start(vorb *f, Codebook *c)1728{1729 int z = -1;17301731 // type 0 is only legal in a scalar context1732 if (c->lookup_type == 0)1733 error(f, VORBIS_invalid_stream);1734 else {1735 DECODE_VQ(z,f,c);1736 if (c->sparse) assert(z < c->sorted_entries);1737 if (z < 0) { // check for EOP1738 if (!f->bytes_in_seg)1739 if (f->last_seg)1740 return z;1741 error(f, VORBIS_invalid_stream);1742 }1743 }1744 return z;1745}17461747static int codebook_decode(vorb *f, Codebook *c, float *output, int len)1748{1749 int i,z = codebook_decode_start(f,c);1750 if (z < 0) return FALSE;1751 if (len > c->dimensions) len = c->dimensions;17521753#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK1754 if (c->lookup_type == 1) {1755 float last = CODEBOOK_ELEMENT_BASE(c);1756 int div = 1;1757 for (i=0; i < len; ++i) {1758 int off = (z / div) % c->lookup_values;1759 float val = CODEBOOK_ELEMENT_FAST(c,off) + last;1760 output[i] += val;1761 if (c->sequence_p) last = val + c->minimum_value;1762 div *= c->lookup_values;1763 }1764 return TRUE;1765 }1766#endif17671768 z *= c->dimensions;1769 if (c->sequence_p) {1770 float last = CODEBOOK_ELEMENT_BASE(c);1771 for (i=0; i < len; ++i) {1772 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1773 output[i] += val;1774 last = val + c->minimum_value;1775 }1776 } else {1777 float last = CODEBOOK_ELEMENT_BASE(c);1778 for (i=0; i < len; ++i) {1779 output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;1780 }1781 }17821783 return TRUE;1784}17851786static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step)1787{1788 int i,z = codebook_decode_start(f,c);1789 float last = CODEBOOK_ELEMENT_BASE(c);1790 if (z < 0) return FALSE;1791 if (len > c->dimensions) len = c->dimensions;17921793#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK1794 if (c->lookup_type == 1) {1795 int div = 1;1796 for (i=0; i < len; ++i) {1797 int off = (z / div) % c->lookup_values;1798 float val = CODEBOOK_ELEMENT_FAST(c,off) + last;1799 output[i*step] += val;1800 if (c->sequence_p) last = val;1801 div *= c->lookup_values;1802 }1803 return TRUE;1804 }1805#endif18061807 z *= c->dimensions;1808 for (i=0; i < len; ++i) {1809 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1810 output[i*step] += val;1811 if (c->sequence_p) last = val;1812 }18131814 return TRUE;1815}18161817static 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)1818{1819 int c_inter = *c_inter_p;1820 int p_inter = *p_inter_p;1821 int i,z, effective = c->dimensions;18221823 // type 0 is only legal in a scalar context1824 if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream);18251826 while (total_decode > 0) {1827 float last = CODEBOOK_ELEMENT_BASE(c);1828 DECODE_VQ(z,f,c);1829 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK1830 assert(!c->sparse || z < c->sorted_entries);1831 #endif1832 if (z < 0) {1833 if (!f->bytes_in_seg)1834 if (f->last_seg) return FALSE;1835 return error(f, VORBIS_invalid_stream);1836 }18371838 // if this will take us off the end of the buffers, stop short!1839 // we check by computing the length of the virtual interleaved1840 // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),1841 // and the length we'll be using (effective)1842 if (c_inter + p_inter*ch + effective > len * ch) {1843 effective = len*ch - (p_inter*ch - c_inter);1844 }18451846 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK1847 if (c->lookup_type == 1) {1848 int div = 1;1849 for (i=0; i < effective; ++i) {1850 int off = (z / div) % c->lookup_values;1851 float val = CODEBOOK_ELEMENT_FAST(c,off) + last;1852 if (outputs[c_inter])1853 outputs[c_inter][p_inter] += val;1854 if (++c_inter == ch) { c_inter = 0; ++p_inter; }1855 if (c->sequence_p) last = val;1856 div *= c->lookup_values;1857 }1858 } else1859 #endif1860 {1861 z *= c->dimensions;1862 if (c->sequence_p) {1863 for (i=0; i < effective; ++i) {1864 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1865 if (outputs[c_inter])1866 outputs[c_inter][p_inter] += val;1867 if (++c_inter == ch) { c_inter = 0; ++p_inter; }1868 last = val;1869 }1870 } else {1871 for (i=0; i < effective; ++i) {1872 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1873 if (outputs[c_inter])1874 outputs[c_inter][p_inter] += val;1875 if (++c_inter == ch) { c_inter = 0; ++p_inter; }1876 }1877 }1878 }18791880 total_decode -= effective;1881 }1882 *c_inter_p = c_inter;1883 *p_inter_p = p_inter;1884 return TRUE;1885}18861887static int predict_point(int x, int x0, int x1, int y0, int y1)1888{1889 int dy = y1 - y0;1890 int adx = x1 - x0;1891 // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?1892 int err = abs(dy) * (x - x0);1893 int off = err / adx;1894 return dy < 0 ? y0 - off : y0 + off;1895}18961897// the following table is block-copied from the specification1898static float inverse_db_table[256] =1899{1900 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, 1901 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, 1902 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, 1903 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, 1904 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, 1905 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, 1906 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, 1907 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, 1908 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, 1909 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, 1910 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, 1911 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, 1912 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, 1913 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, 1914 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, 1915 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, 1916 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, 1917 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, 1918 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, 1919 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, 1920 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, 1921 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, 1922 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, 1923 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, 1924 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, 1925 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, 1926 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, 1927 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, 1928 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, 1929 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, 1930 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, 1931 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, 1932 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, 1933 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, 1934 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, 1935 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, 1936 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f, 1937 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f, 1938 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f, 1939 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f, 1940 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f, 1941 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f, 1942 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f, 1943 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f, 1944 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f, 1945 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f, 1946 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f, 1947 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f, 1948 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f, 1949 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f, 1950 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f, 1951 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f, 1952 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f, 1953 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f, 1954 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f, 1955 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f, 1956 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f, 1957 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f, 1958 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f, 1959 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f, 1960 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f, 1961 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f, 1962 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f, 1963 0.82788260f, 0.88168307f, 0.9389798f, 1.0f1964};196519661967// @OPTIMIZE: if you want to replace this bresenham line-drawing routine,1968// note that you must produce bit-identical output to decode correctly;1969// this specific sequence of operations is specified in the spec (it's1970// drawing integer-quantized frequency-space lines that the encoder1971// expects to be exactly the same)1972// ... also, isn't the whole point of Bresenham's algorithm to NOT1973// have to divide in the setup? sigh.1974#ifndef STB_VORBIS_NO_DEFER_FLOOR1975#define LINE_OP(a,b) a *= b1976#else1977#define LINE_OP(a,b) a = b1978#endif19791980#ifdef STB_VORBIS_DIVIDE_TABLE1981#define DIVTAB_NUMER 321982#define DIVTAB_DENOM 641983int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB1984#endif19851986static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n)1987{1988 int dy = y1 - y0;1989 int adx = x1 - x0;1990 int ady = abs(dy);1991 int base;1992 int x=x0,y=y0;1993 int err = 0;1994 int sy;19951996#ifdef STB_VORBIS_DIVIDE_TABLE1997 if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {1998 if (dy < 0) {1999 base = -integer_divide_table[ady][adx];2000 sy = base-1;The first 2,000 of 5,476 lines.