stb_vorbis.c
This file
Path
stb_vorbis.c
5,584 lines · 188.3 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
History of this fileTake it
1// Ogg Vorbis audio decoder - v1.22 - public domain2// http://nothings.org/stb_vorbis/3//4// Original version written by Sean Barrett in 2007.5//6// Originally sponsored by RAD Game Tools. Seeking implementation7// sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker,8// Elias Software, Aras Pranckevicius, and Sean Barrett.9//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 github:alxprd30// Tom Beaumont Ingo Leitgeb Nicolas Guillemot31// Phillip Bennefall Rohit Thiago Goulart32// github:manxorist Saga Musix github:infatum33// Timur Gagiev Maxwell Koo Peter Waller34// github:audinowho Dougall Johnson David Reid35// github:Clownacy Pedro J. Estebanez Remi Verschelde36// AnthoFoxo github:morlat Gabriel Ravier37//38// Partial history:39// 1.22 - 2021-07-11 - various small fixes40// 1.21 - 2021-07-02 - fix bug for files with no comments41// 1.20 - 2020-07-11 - several small fixes42// 1.19 - 2020-02-05 - warnings43// 1.18 - 2020-02-02 - fix seek bugs; parse header comments; misc warnings etc.44// 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure)45// 1.16 - 2019-03-04 - fix warnings46// 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found47// 1.14 - 2018-02-11 - delete bogus dealloca usage48// 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)49// 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files50// 1.11 - 2017-07-23 - fix MinGW compilation51// 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory52// 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version53// 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame54// 1.07 - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const55// 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)56// some crash fixes when out of memory or with corrupt files57// fix some inappropriately signed shifts58// 1.05 - 2015-04-19 - don't define __forceinline if it's redundant59// 1.04 - 2014-08-27 - fix missing const-correct case in API60// 1.03 - 2014-08-07 - warning fixes61// 1.02 - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows62// 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct)63// 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel;64// (API change) report sample rate for decode-full-file funcs65//66// See end of file for full version history.676869//////////////////////////////////////////////////////////////////////////////70//71// HEADER BEGINS HERE72//7374#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H75#define STB_VORBIS_INCLUDE_STB_VORBIS_H7677#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)78#define STB_VORBIS_NO_STDIO 179#endif8081#ifndef STB_VORBIS_NO_STDIO82#include <stdio.h>83#endif8485#ifdef __cplusplus86extern "C" {87#endif8889/////////// THREAD SAFETY9091// Individual stb_vorbis* handles are not thread-safe; you cannot decode from92// them from multiple threads at the same time. However, you can have multiple93// stb_vorbis* handles and decode from them independently in multiple thrads.949596/////////// MEMORY ALLOCATION9798// normally stb_vorbis uses malloc() to allocate memory at startup,99// and alloca() to allocate temporary memory during a frame on the100// stack. (Memory consumption will depend on the amount of setup101// data in the file and how you set the compile flags for speed102// vs. size. In my test files the maximal-size usage is ~150KB.)103//104// You can modify the wrapper functions in the source (setup_malloc,105// setup_temp_malloc, temp_malloc) to change this behavior, or you106// can use a simpler allocation model: you pass in a buffer from107// which stb_vorbis will allocate _all_ its memory (including the108// temp memory). "open" may fail with a VORBIS_outofmem if you109// do not pass in enough data; there is no way to determine how110// much you do need except to succeed (at which point you can111// query get_info to find the exact amount required. yes I know112// this is lame).113//114// If you pass in a non-NULL buffer of the type below, allocation115// will occur from it as described above. Otherwise just pass NULL116// to use malloc()/alloca()117118typedef struct119{120 char *alloc_buffer;121 int alloc_buffer_length_in_bytes;122} stb_vorbis_alloc;123124125/////////// FUNCTIONS USEABLE WITH ALL INPUT MODES126127typedef struct stb_vorbis stb_vorbis;128129typedef struct130{131 unsigned int sample_rate;132 int channels;133134 unsigned int setup_memory_required;135 unsigned int setup_temp_memory_required;136 unsigned int temp_memory_required;137138 int max_frame_size;139} stb_vorbis_info;140141typedef struct142{143 char *vendor;144145 int comment_list_length;146 char **comment_list;147} stb_vorbis_comment;148149// get general information about the file150extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);151152// get ogg comments153extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f);154155// get the last error detected (clears it, too)156extern int stb_vorbis_get_error(stb_vorbis *f);157158// close an ogg vorbis file and free all memory in use159extern void stb_vorbis_close(stb_vorbis *f);160161// this function returns the offset (in samples) from the beginning of the162// file that will be returned by the next decode, if it is known, or -1163// otherwise. after a flush_pushdata() call, this may take a while before164// it becomes valid again.165// NOT WORKING YET after a seek with PULLDATA API166extern int stb_vorbis_get_sample_offset(stb_vorbis *f);167168// returns the current seek point within the file, or offset from the beginning169// of the memory buffer. In pushdata mode it returns 0.170extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);171172/////////// PUSHDATA API173174#ifndef STB_VORBIS_NO_PUSHDATA_API175176// this API allows you to get blocks of data from any source and hand177// them to stb_vorbis. you have to buffer them; stb_vorbis will tell178// you how much it used, and you have to give it the rest next time;179// and stb_vorbis may not have enough data to work with and you will180// need to give it the same data again PLUS more. Note that the Vorbis181// specification does not bound the size of an individual frame.182183extern stb_vorbis *stb_vorbis_open_pushdata(184 const unsigned char * datablock, int datablock_length_in_bytes,185 int *datablock_memory_consumed_in_bytes,186 int *error,187 const stb_vorbis_alloc *alloc_buffer);188// create a vorbis decoder by passing in the initial data block containing189// the ogg&vorbis headers (you don't need to do parse them, just provide190// the first N bytes of the file--you're told if it's not enough, see below)191// on success, returns an stb_vorbis *, does not set error, returns the amount of192// data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;193// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed194// if returns NULL and *error is VORBIS_need_more_data, then the input block was195// incomplete and you need to pass in a larger block from the start of the file196197extern int stb_vorbis_decode_frame_pushdata(198 stb_vorbis *f,199 const unsigned char *datablock, int datablock_length_in_bytes,200 int *channels, // place to write number of float * buffers201 float ***output, // place to write float ** array of float * buffers202 int *samples // place to write number of output samples203 );204// decode a frame of audio sample data if possible from the passed-in data block205//206// return value: number of bytes we used from datablock207//208// possible cases:209// 0 bytes used, 0 samples output (need more data)210// N bytes used, 0 samples output (resynching the stream, keep going)211// N bytes used, M samples output (one frame of data)212// note that after opening a file, you will ALWAYS get one N-bytes,0-sample213// frame, because Vorbis always "discards" the first frame.214//215// Note that on resynch, stb_vorbis will rarely consume all of the buffer,216// instead only datablock_length_in_bytes-3 or less. This is because it wants217// to avoid missing parts of a page header if they cross a datablock boundary,218// without writing state-machiney code to record a partial detection.219//220// The number of channels returned are stored in *channels (which can be221// NULL--it is always the same as the number of channels reported by222// get_info). *output will contain an array of float* buffers, one per223// channel. In other words, (*output)[0][0] contains the first sample from224// the first channel, and (*output)[1][0] contains the first sample from225// the second channel.226//227// *output points into stb_vorbis's internal output buffer storage; these228// buffers are owned by stb_vorbis and application code should not free229// them or modify their contents. They are transient and will be overwritten230// once you ask for more data to get decoded, so be sure to grab any data231// you need before then.232233extern void stb_vorbis_flush_pushdata(stb_vorbis *f);234// inform stb_vorbis that your next datablock will not be contiguous with235// previous ones (e.g. you've seeked in the data); future attempts to decode236// frames will cause stb_vorbis to resynchronize (as noted above), and237// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it238// will begin decoding the _next_ frame.239//240// if you want to seek using pushdata, you need to seek in your file, then241// call stb_vorbis_flush_pushdata(), then start calling decoding, then once242// decoding is returning you data, call stb_vorbis_get_sample_offset, and243// if you don't like the result, seek your file again and repeat.244#endif245246247////////// PULLING INPUT API248249#ifndef STB_VORBIS_NO_PULLDATA_API250// This API assumes stb_vorbis is allowed to pull data from a source--251// either a block of memory containing the _entire_ vorbis stream, or a252// FILE * that you or it create, or possibly some other reading mechanism253// if you go modify the source to replace the FILE * case with some kind254// of callback to your code. (But if you don't support seeking, you may255// just want to go ahead and use pushdata.)256257#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)258extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);259#endif260#if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)261extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);262#endif263// decode an entire file and output the data interleaved into a malloc()ed264// buffer stored in *output. The return value is the number of samples265// decoded, or -1 if the file could not be opened or was not an ogg vorbis file.266// When you're done with it, just free() the pointer returned in *output.267268extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,269 int *error, const stb_vorbis_alloc *alloc_buffer);270// create an ogg vorbis decoder from an ogg vorbis stream in memory (note271// this must be the entire stream!). on failure, returns NULL and sets *error272273#ifndef STB_VORBIS_NO_STDIO274extern stb_vorbis * stb_vorbis_open_filename(const char *filename,275 int *error, const stb_vorbis_alloc *alloc_buffer);276// create an ogg vorbis decoder from a filename via fopen(). on failure,277// returns NULL and sets *error (possibly to VORBIS_file_open_failure).278279extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,280 int *error, const stb_vorbis_alloc *alloc_buffer);281// create an ogg vorbis decoder from an open FILE *, looking for a stream at282// the _current_ seek point (ftell). on failure, returns NULL and sets *error.283// note that stb_vorbis must "own" this stream; if you seek it in between284// calls to stb_vorbis, it will become confused. Moreover, if you attempt to285// perform stb_vorbis_seek_*() operations on this file, it will assume it286// owns the _entire_ rest of the file after the start point. Use the next287// function, stb_vorbis_open_file_section(), to limit it.288289extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close,290 int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);291// create an ogg vorbis decoder from an open FILE *, looking for a stream at292// the _current_ seek point (ftell); the stream will be of length 'len' bytes.293// on failure, returns NULL and sets *error. note that stb_vorbis must "own"294// this stream; if you seek it in between calls to stb_vorbis, it will become295// confused.296#endif297298extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);299extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);300// these functions seek in the Vorbis file to (approximately) 'sample_number'.301// after calling seek_frame(), the next call to get_frame_*() will include302// the specified sample. after calling stb_vorbis_seek(), the next call to303// stb_vorbis_get_samples_* will start with the specified sample. If you304// do not need to seek to EXACTLY the target sample when using get_samples_*,305// you can also use seek_frame().306307extern int stb_vorbis_seek_start(stb_vorbis *f);308// this function is equivalent to stb_vorbis_seek(f,0)309310extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);311extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f);312// these functions return the total length of the vorbis stream313314extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);315// decode the next frame and return the number of samples. the number of316// channels returned are stored in *channels (which can be NULL--it is always317// the same as the number of channels reported by get_info). *output will318// contain an array of float* buffers, one per channel. These outputs will319// be overwritten on the next call to stb_vorbis_get_frame_*.320//321// You generally should not intermix calls to stb_vorbis_get_frame_*()322// and stb_vorbis_get_samples_*(), since the latter calls the former.323324#ifndef STB_VORBIS_NO_INTEGER_CONVERSION325extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);326extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples);327#endif328// decode the next frame and return the number of *samples* per channel.329// Note that for interleaved data, you pass in the number of shorts (the330// size of your array), but the return value is the number of samples per331// channel, not the total number of samples.332//333// The data is coerced to the number of channels you request according to the334// channel coercion rules (see below). You must pass in the size of your335// buffer(s) so that stb_vorbis will not overwrite the end of the buffer.336// The maximum buffer size needed can be gotten from get_info(); however,337// the Vorbis I specification implies an absolute maximum of 4096 samples338// per channel.339340// Channel coercion rules:341// Let M be the number of channels requested, and N the number of channels present,342// and Cn be the nth channel; let stereo L be the sum of all L and center channels,343// and stereo R be the sum of all R and center channels (channel assignment from the344// vorbis spec).345// M N output346// 1 k sum(Ck) for all k347// 2 * stereo L, stereo R348// k l k > l, the first l channels, then 0s349// k l k <= l, the first k channels350// Note that this is not _good_ surround etc. mixing at all! It's just so351// you get something useful.352353extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);354extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);355// gets num_samples samples, not necessarily on a frame boundary--this requires356// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.357// Returns the number of samples stored per channel; it may be less than requested358// at the end of the file. If there are no more samples in the file, returns 0.359360#ifndef STB_VORBIS_NO_INTEGER_CONVERSION361extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);362extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);363#endif364// gets num_samples samples, not necessarily on a frame boundary--this requires365// buffering so you have to supply the buffers. Applies the coercion rules above366// to produce 'channels' channels. Returns the number of samples stored per channel;367// it may be less than requested at the end of the file. If there are no more368// samples in the file, returns 0.369370#endif371372//////// ERROR CODES373374enum STBVorbisError375{376 VORBIS__no_error,377378 VORBIS_need_more_data=1, // not a real error379380 VORBIS_invalid_api_mixing, // can't mix API modes381 VORBIS_outofmem, // not enough memory382 VORBIS_feature_not_supported, // uses floor 0383 VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small384 VORBIS_file_open_failure, // fopen() failed385 VORBIS_seek_without_length, // can't seek in unknown-length file386387 VORBIS_unexpected_eof=10, // file is truncated?388 VORBIS_seek_invalid, // seek past EOF389390 // decoding errors (corrupt/invalid stream) -- you probably391 // don't care about the exact details of these392393 // vorbis errors:394 VORBIS_invalid_setup=20,395 VORBIS_invalid_stream,396397 // ogg errors:398 VORBIS_missing_capture_pattern=30,399 VORBIS_invalid_stream_structure_version,400 VORBIS_continued_packet_flag_invalid,401 VORBIS_incorrect_stream_serial_number,402 VORBIS_invalid_first_page,403 VORBIS_bad_packet_type,404 VORBIS_cant_find_last_page,405 VORBIS_seek_failed,406 VORBIS_ogg_skeleton_not_supported407};408409410#ifdef __cplusplus411}412#endif413414#endif // STB_VORBIS_INCLUDE_STB_VORBIS_H415//416// HEADER ENDS HERE417//418//////////////////////////////////////////////////////////////////////////////419420#ifndef STB_VORBIS_HEADER_ONLY421422// global configuration settings (e.g. set these in the project/makefile),423// or just set them in this file at the top (although ideally the first few424// should be visible when the header file is compiled too, although it's not425// crucial)426427// STB_VORBIS_NO_PUSHDATA_API428// does not compile the code for the various stb_vorbis_*_pushdata()429// functions430// #define STB_VORBIS_NO_PUSHDATA_API431432// STB_VORBIS_NO_PULLDATA_API433// does not compile the code for the non-pushdata APIs434// #define STB_VORBIS_NO_PULLDATA_API435436// STB_VORBIS_NO_STDIO437// does not compile the code for the APIs that use FILE *s internally438// or externally (implied by STB_VORBIS_NO_PULLDATA_API)439// #define STB_VORBIS_NO_STDIO440441// STB_VORBIS_NO_INTEGER_CONVERSION442// does not compile the code for converting audio sample data from443// float to integer (implied by STB_VORBIS_NO_PULLDATA_API)444// #define STB_VORBIS_NO_INTEGER_CONVERSION445446// STB_VORBIS_NO_FAST_SCALED_FLOAT447// does not use a fast float-to-int trick to accelerate float-to-int on448// most platforms which requires endianness be defined correctly.449//#define STB_VORBIS_NO_FAST_SCALED_FLOAT450451452// STB_VORBIS_MAX_CHANNELS [number]453// globally define this to the maximum number of channels you need.454// The spec does not put a restriction on channels except that455// the count is stored in a byte, so 255 is the hard limit.456// Reducing this saves about 16 bytes per value, so using 16 saves457// (255-16)*16 or around 4KB. Plus anything other memory usage458// I forgot to account for. Can probably go as low as 8 (7.1 audio),459// 6 (5.1 audio), or 2 (stereo only).460#ifndef STB_VORBIS_MAX_CHANNELS461#define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone?462#endif463464// STB_VORBIS_PUSHDATA_CRC_COUNT [number]465// after a flush_pushdata(), stb_vorbis begins scanning for the466// next valid page, without backtracking. when it finds something467// that looks like a page, it streams through it and verifies its468// CRC32. Should that validation fail, it keeps scanning. But it's469// possible that _while_ streaming through to check the CRC32 of470// one candidate page, it sees another candidate page. This #define471// determines how many "overlapping" candidate pages it can search472// at once. Note that "real" pages are typically ~4KB to ~8KB, whereas473// garbage pages could be as big as 64KB, but probably average ~16KB.474// So don't hose ourselves by scanning an apparent 64KB page and475// missing a ton of real ones in the interim; so minimum of 2476#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT477#define STB_VORBIS_PUSHDATA_CRC_COUNT 4478#endif479480// STB_VORBIS_FAST_HUFFMAN_LENGTH [number]481// sets the log size of the huffman-acceleration table. Maximum482// supported value is 24. with larger numbers, more decodings are O(1),483// but the table size is larger so worse cache missing, so you'll have484// to probe (and try multiple ogg vorbis files) to find the sweet spot.485#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH486#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10487#endif488489// STB_VORBIS_FAST_BINARY_LENGTH [number]490// sets the log size of the binary-search acceleration table. this491// is used in similar fashion to the fast-huffman size to set initial492// parameters for the binary search493494// STB_VORBIS_FAST_HUFFMAN_INT495// The fast huffman tables are much more efficient if they can be496// stored as 16-bit results instead of 32-bit results. This restricts497// the codebooks to having only 65535 possible outcomes, though.498// (At least, accelerated by the huffman table.)499#ifndef STB_VORBIS_FAST_HUFFMAN_INT500#define STB_VORBIS_FAST_HUFFMAN_SHORT501#endif502503// STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH504// If the 'fast huffman' search doesn't succeed, then stb_vorbis falls505// back on binary searching for the correct one. This requires storing506// extra tables with the huffman codes in sorted order. Defining this507// symbol trades off space for speed by forcing a linear search in the508// non-fast case, except for "sparse" codebooks.509// #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH510511// STB_VORBIS_DIVIDES_IN_RESIDUE512// stb_vorbis precomputes the result of the scalar residue decoding513// that would otherwise require a divide per chunk. you can trade off514// space for time by defining this symbol.515// #define STB_VORBIS_DIVIDES_IN_RESIDUE516517// STB_VORBIS_DIVIDES_IN_CODEBOOK518// vorbis VQ codebooks can be encoded two ways: with every case explicitly519// stored, or with all elements being chosen from a small range of values,520// and all values possible in all elements. By default, stb_vorbis expands521// this latter kind out to look like the former kind for ease of decoding,522// because otherwise an integer divide-per-vector-element is required to523// unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can524// trade off storage for speed.525//#define STB_VORBIS_DIVIDES_IN_CODEBOOK526527#ifdef STB_VORBIS_CODEBOOK_SHORTS528#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"529#endif530531// STB_VORBIS_DIVIDE_TABLE532// this replaces small integer divides in the floor decode loop with533// table lookups. made less than 1% difference, so disabled by default.534535// STB_VORBIS_NO_INLINE_DECODE536// disables the inlining of the scalar codebook fast-huffman decode.537// might save a little codespace; useful for debugging538// #define STB_VORBIS_NO_INLINE_DECODE539540// STB_VORBIS_NO_DEFER_FLOOR541// Normally we only decode the floor without synthesizing the actual542// full curve. We can instead synthesize the curve immediately. This543// requires more memory and is very likely slower, so I don't think544// you'd ever want to do it except for debugging.545// #define STB_VORBIS_NO_DEFER_FLOOR546547548549550//////////////////////////////////////////////////////////////////////////////551552#ifdef STB_VORBIS_NO_PULLDATA_API553 #define STB_VORBIS_NO_INTEGER_CONVERSION554 #define STB_VORBIS_NO_STDIO555#endif556557#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)558 #define STB_VORBIS_NO_STDIO 1559#endif560561#ifndef STB_VORBIS_NO_INTEGER_CONVERSION562#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT563564 // only need endianness for fast-float-to-int, which we don't565 // use for pushdata566567 #ifndef STB_VORBIS_BIG_ENDIAN568 #define STB_VORBIS_ENDIAN 0569 #else570 #define STB_VORBIS_ENDIAN 1571 #endif572573#endif574#endif575576577#ifndef STB_VORBIS_NO_STDIO578#include <stdio.h>579#endif580581#ifndef STB_VORBIS_NO_CRT582 #include <stdlib.h>583 #include <string.h>584 #include <assert.h>585 #include <math.h>586587 // find definition of alloca if it's not in stdlib.h:588 #if defined(_MSC_VER) || defined(__MINGW32__)589 #include <malloc.h>590 #endif591 #if defined(__linux__) || defined(__linux) || defined(__sun__) || defined(__EMSCRIPTEN__) || defined(__NEWLIB__)592 #include <alloca.h>593 #endif594#else // STB_VORBIS_NO_CRT595 #define NULL 0596 #define malloc(s) 0597 #define free(s) ((void) 0)598 #define realloc(s) 0599#endif // STB_VORBIS_NO_CRT600601#include <limits.h>602603#ifdef __MINGW32__604 // eff you mingw:605 // "fixed":606 // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/607 // "no that broke the build, reverted, who cares about C":608 // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/609 #ifdef __forceinline610 #undef __forceinline611 #endif612 #define __forceinline613 #ifndef alloca614 #define alloca __builtin_alloca615 #endif616#elif !defined(_MSC_VER)617 #if __GNUC__618 #define __forceinline inline619 #else620 #define __forceinline621 #endif622#endif623624#if STB_VORBIS_MAX_CHANNELS > 256625#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"626#endif627628#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24629#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"630#endif631632633#if 0634#include <crtdbg.h>635#define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])636#else637#define CHECK(f) ((void) 0)638#endif639640#define MAX_BLOCKSIZE_LOG 13 // from specification641#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)642643644typedef unsigned char uint8;645typedef signed char int8;646typedef unsigned short uint16;647typedef signed short int16;648typedef unsigned int uint32;649typedef signed int int32;650651#ifndef TRUE652#define TRUE 1653#define FALSE 0654#endif655656typedef float codetype;657658#ifdef _MSC_VER659#define STBV_NOTUSED(v) (void)(v)660#else661#define STBV_NOTUSED(v) (void)sizeof(v)662#endif663664// @NOTE665//666// Some arrays below are tagged "//varies", which means it's actually667// a variable-sized piece of data, but rather than malloc I assume it's668// small enough it's better to just allocate it all together with the669// main thing670//671// Most of the variables are specified with the smallest size I could pack672// them into. It might give better performance to make them all full-sized673// integers. It should be safe to freely rearrange the structures or change674// the sizes larger--nothing relies on silently truncating etc., nor the675// order of variables.676677#define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)678#define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1)679680typedef struct681{682 int dimensions, entries;683 uint8 *codeword_lengths;684 float minimum_value;685 float delta_value;686 uint8 value_bits;687 uint8 lookup_type;688 uint8 sequence_p;689 uint8 sparse;690 uint32 lookup_values;691 codetype *multiplicands;692 uint32 *codewords;693 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT694 int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];695 #else696 int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];697 #endif698 uint32 *sorted_codewords;699 int *sorted_values;700 int sorted_entries;701} Codebook;702703typedef struct704{705 uint8 order;706 uint16 rate;707 uint16 bark_map_size;708 uint8 amplitude_bits;709 uint8 amplitude_offset;710 uint8 number_of_books;711 uint8 book_list[16]; // varies712} Floor0;713714typedef struct715{716 uint8 partitions;717 uint8 partition_class_list[32]; // varies718 uint8 class_dimensions[16]; // varies719 uint8 class_subclasses[16]; // varies720 uint8 class_masterbooks[16]; // varies721 int16 subclass_books[16][8]; // varies722 uint16 Xlist[31*8+2]; // varies723 uint8 sorted_order[31*8+2];724 uint8 neighbors[31*8+2][2];725 uint8 floor1_multiplier;726 uint8 rangebits;727 int values;728} Floor1;729730typedef union731{732 Floor0 floor0;733 Floor1 floor1;734} Floor;735736typedef struct737{738 uint32 begin, end;739 uint32 part_size;740 uint8 classifications;741 uint8 classbook;742 uint8 **classdata;743 int16 (*residue_books)[8];744} Residue;745746typedef struct747{748 uint8 magnitude;749 uint8 angle;750 uint8 mux;751} MappingChannel;752753typedef struct754{755 uint16 coupling_steps;756 MappingChannel *chan;757 uint8 submaps;758 uint8 submap_floor[15]; // varies759 uint8 submap_residue[15]; // varies760} Mapping;761762typedef struct763{764 uint8 blockflag;765 uint8 mapping;766 uint16 windowtype;767 uint16 transformtype;768} Mode;769770typedef struct771{772 uint32 goal_crc; // expected crc if match773 int bytes_left; // bytes left in packet774 uint32 crc_so_far; // running crc775 int bytes_done; // bytes processed in _current_ chunk776 uint32 sample_loc; // granule pos encoded in page777} CRCscan;778779typedef struct780{781 uint32 page_start, page_end;782 uint32 last_decoded_sample;783} ProbedPage;784785struct stb_vorbis786{787 // user-accessible info788 unsigned int sample_rate;789 int channels;790791 unsigned int setup_memory_required;792 unsigned int temp_memory_required;793 unsigned int setup_temp_memory_required;794795 char *vendor;796 int comment_list_length;797 char **comment_list;798799 // input config800#ifndef STB_VORBIS_NO_STDIO801 FILE *f;802 uint32 f_start;803 int close_on_free;804#endif805806 uint8 *stream;807 uint8 *stream_start;808 uint8 *stream_end;809810 uint32 stream_len;811812 uint8 push_mode;813814 // the page to seek to when seeking to start, may be zero815 uint32 first_audio_page_offset;816817 // p_first is the page on which the first audio packet ends818 // (but not necessarily the page on which it starts)819 ProbedPage p_first, p_last;820821 // memory management822 stb_vorbis_alloc alloc;823 int setup_offset;824 int temp_offset;825826 // run-time results827 int eof;828 enum STBVorbisError error;829830 // user-useful data831832 // header info833 int blocksize[2];834 int blocksize_0, blocksize_1;835 int codebook_count;836 Codebook *codebooks;837 int floor_count;838 uint16 floor_types[64]; // varies839 Floor *floor_config;840 int residue_count;841 uint16 residue_types[64]; // varies842 Residue *residue_config;843 int mapping_count;844 Mapping *mapping;845 int mode_count;846 Mode mode_config[64]; // varies847848 uint32 total_samples;849850 // decode buffer851 float *channel_buffers[STB_VORBIS_MAX_CHANNELS];852 float *outputs [STB_VORBIS_MAX_CHANNELS];853854 float *previous_window[STB_VORBIS_MAX_CHANNELS];855 int previous_length;856857 #ifndef STB_VORBIS_NO_DEFER_FLOOR858 int16 *finalY[STB_VORBIS_MAX_CHANNELS];859 #else860 float *floor_buffers[STB_VORBIS_MAX_CHANNELS];861 #endif862863 uint32 current_loc; // sample location of next frame to decode864 int current_loc_valid;865866 // per-blocksize precomputed data867868 // twiddle factors869 float *A[2],*B[2],*C[2];870 float *window[2];871 uint16 *bit_reverse[2];872873 // current page/packet/segment streaming info874 uint32 serial; // stream serial number for verification875 int last_page;876 int segment_count;877 uint8 segments[255];878 uint8 page_flag;879 uint8 bytes_in_seg;880 uint8 first_decode;881 int next_seg;882 int last_seg; // flag that we're on the last segment883 int last_seg_which; // what was the segment number of the last seg?884 uint32 acc;885 int valid_bits;886 int packet_bytes;887 int end_seg_with_known_loc;888 uint32 known_loc_for_packet;889 int discard_samples_deferred;890 uint32 samples_output;891892 // push mode scanning893 int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching894#ifndef STB_VORBIS_NO_PUSHDATA_API895 CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];896#endif897898 // sample-access899 int channel_buffer_start;900 int channel_buffer_end;901};902903#if defined(STB_VORBIS_NO_PUSHDATA_API)904 #define IS_PUSH_MODE(f) FALSE905#elif defined(STB_VORBIS_NO_PULLDATA_API)906 #define IS_PUSH_MODE(f) TRUE907#else908 #define IS_PUSH_MODE(f) ((f)->push_mode)909#endif910911typedef struct stb_vorbis vorb;912913static int error(vorb *f, enum STBVorbisError e)914{915 f->error = e;916 if (!f->eof && e != VORBIS_need_more_data) {917 f->error=e; // breakpoint for debugging918 }919 return 0;920}921922923// these functions are used for allocating temporary memory924// while decoding. if you can afford the stack space, use925// alloca(); otherwise, provide a temp buffer and it will926// allocate out of those.927928#define array_size_required(count,size) (count*(sizeof(void *)+(size)))929930#define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))931#define temp_free(f,p) (void)0932#define temp_alloc_save(f) ((f)->temp_offset)933#define temp_alloc_restore(f,p) ((f)->temp_offset = (p))934935#define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)936937// given a sufficiently large block of memory, make an array of pointers to subblocks of it938static void *make_block_array(void *mem, int count, int size)939{940 int i;941 void ** p = (void **) mem;942 char *q = (char *) (p + count);943 for (i=0; i < count; ++i) {944 p[i] = q;945 q += size;946 }947 return p;948}949950static void *setup_malloc(vorb *f, int sz)951{952 sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.953 f->setup_memory_required += sz;954 if (f->alloc.alloc_buffer) {955 void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;956 if (f->setup_offset + sz > f->temp_offset) return NULL;957 f->setup_offset += sz;958 return p;959 }960 return sz ? malloc(sz) : NULL;961}962963static void setup_free(vorb *f, void *p)964{965 if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack966 free(p);967}968969static void *setup_temp_malloc(vorb *f, int sz)970{971 sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs.972 if (f->alloc.alloc_buffer) {973 if (f->temp_offset - sz < f->setup_offset) return NULL;974 f->temp_offset -= sz;975 return (char *) f->alloc.alloc_buffer + f->temp_offset;976 }977 return malloc(sz);978}979980static void setup_temp_free(vorb *f, void *p, int sz)981{982 if (f->alloc.alloc_buffer) {983 f->temp_offset += (sz+7)&~7;984 return;985 }986 free(p);987}988989#define CRC32_POLY 0x04c11db7 // from spec990991static uint32 crc_table[256];992static void crc32_init(void)993{994 int i,j;995 uint32 s;996 for(i=0; i < 256; i++) {997 for (s=(uint32) i << 24, j=0; j < 8; ++j)998 s = (s << 1) ^ (s >= (1U<<31) ? CRC32_POLY : 0);999 crc_table[i] = s;1000 }1001}10021003static __forceinline uint32 crc32_update(uint32 crc, uint8 byte)1004{1005 return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];1006}100710081009// used in setup, and for huffman that doesn't go fast path1010static unsigned int bit_reverse(unsigned int n)1011{1012 n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);1013 n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);1014 n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);1015 n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);1016 return (n >> 16) | (n << 16);1017}10181019static float square(float x)1020{1021 return x*x;1022}10231024// this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 31025// as required by the specification. fast(?) implementation from stb.h1026// @OPTIMIZE: called multiple times per-packet with "constants"; move to setup1027static int ilog(int32 n)1028{1029 static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };10301031 if (n < 0) return 0; // signed n returns 010321033 // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)1034 if (n < (1 << 14))1035 if (n < (1 << 4)) return 0 + log2_4[n ];1036 else if (n < (1 << 9)) return 5 + log2_4[n >> 5];1037 else return 10 + log2_4[n >> 10];1038 else if (n < (1 << 24))1039 if (n < (1 << 19)) return 15 + log2_4[n >> 15];1040 else return 20 + log2_4[n >> 20];1041 else if (n < (1 << 29)) return 25 + log2_4[n >> 25];1042 else return 30 + log2_4[n >> 30];1043}10441045#ifndef M_PI1046 #define M_PI 3.14159265358979323846264f // from CRC1047#endif10481049// code length assigned to a value with no huffman encoding1050#define NO_CODE 25510511052/////////////////////// LEAF SETUP FUNCTIONS //////////////////////////1053//1054// these functions are only called at setup, and only a few times1055// per file10561057static float float32_unpack(uint32 x)1058{1059 // from the specification1060 uint32 mantissa = x & 0x1fffff;1061 uint32 sign = x & 0x80000000;1062 uint32 exp = (x & 0x7fe00000) >> 21;1063 double res = sign ? -(double)mantissa : (double)mantissa;1064 return (float) ldexp((float)res, (int)exp-788);1065}106610671068// zlib & jpeg huffman tables assume that the output symbols1069// can either be arbitrarily arranged, or have monotonically1070// increasing frequencies--they rely on the lengths being sorted;1071// this makes for a very simple generation algorithm.1072// vorbis allows a huffman table with non-sorted lengths. This1073// requires a more sophisticated construction, since symbols in1074// order do not map to huffman codes "in order".1075static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values)1076{1077 if (!c->sparse) {1078 c->codewords [symbol] = huff_code;1079 } else {1080 c->codewords [count] = huff_code;1081 c->codeword_lengths[count] = len;1082 values [count] = symbol;1083 }1084}10851086static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)1087{1088 int i,k,m=0;1089 uint32 available[32];10901091 memset(available, 0, sizeof(available));1092 // find the first entry1093 for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;1094 if (k == n) { assert(c->sorted_entries == 0); return TRUE; }1095 assert(len[k] < 32); // no error return required, code reading lens checks this1096 // add to the list1097 add_entry(c, 0, k, m++, len[k], values);1098 // add all available leaves1099 for (i=1; i <= len[k]; ++i)1100 available[i] = 1U << (32-i);1101 // note that the above code treats the first case specially,1102 // but it's really the same as the following code, so they1103 // could probably be combined (except the initial code is 0,1104 // and I use 0 in available[] to mean 'empty')1105 for (i=k+1; i < n; ++i) {1106 uint32 res;1107 int z = len[i], y;1108 if (z == NO_CODE) continue;1109 assert(z < 32); // no error return required, code reading lens checks this1110 // find lowest available leaf (should always be earliest,1111 // which is what the specification calls for)1112 // note that this property, and the fact we can never have1113 // more than one free leaf at a given level, isn't totally1114 // trivial to prove, but it seems true and the assert never1115 // fires, so!1116 while (z > 0 && !available[z]) --z;1117 if (z == 0) { return FALSE; }1118 res = available[z];1119 available[z] = 0;1120 add_entry(c, bit_reverse(res), i, m++, len[i], values);1121 // propagate availability up the tree1122 if (z != len[i]) {1123 for (y=len[i]; y > z; --y) {1124 assert(available[y] == 0);1125 available[y] = res + (1 << (32-y));1126 }1127 }1128 }1129 return TRUE;1130}11311132// accelerated huffman table allows fast O(1) match of all symbols1133// of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH1134static void compute_accelerated_huffman(Codebook *c)1135{1136 int i, len;1137 for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)1138 c->fast_huffman[i] = -1;11391140 len = c->sparse ? c->sorted_entries : c->entries;1141 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT1142 if (len > 32767) len = 32767; // largest possible value we can encode!1143 #endif1144 for (i=0; i < len; ++i) {1145 if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {1146 uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];1147 // set table entries for all bit combinations in the higher bits1148 while (z < FAST_HUFFMAN_TABLE_SIZE) {1149 c->fast_huffman[z] = i;1150 z += 1 << c->codeword_lengths[i];1151 }1152 }1153 }1154}11551156#ifdef _MSC_VER1157#define STBV_CDECL __cdecl1158#else1159#define STBV_CDECL1160#endif11611162static int STBV_CDECL uint32_compare(const void *p, const void *q)1163{1164 uint32 x = * (uint32 *) p;1165 uint32 y = * (uint32 *) q;1166 return x < y ? -1 : x > y;1167}11681169static int include_in_sort(Codebook *c, uint8 len)1170{1171 if (c->sparse) { assert(len != NO_CODE); return TRUE; }1172 if (len == NO_CODE) return FALSE;1173 if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;1174 return FALSE;1175}11761177// if the fast table above doesn't work, we want to binary1178// search them... need to reverse the bits1179static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)1180{1181 int i, len;1182 // build a list of all the entries1183 // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.1184 // this is kind of a frivolous optimization--I don't see any performance improvement,1185 // but it's like 4 extra lines of code, so.1186 if (!c->sparse) {1187 int k = 0;1188 for (i=0; i < c->entries; ++i)1189 if (include_in_sort(c, lengths[i]))1190 c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);1191 assert(k == c->sorted_entries);1192 } else {1193 for (i=0; i < c->sorted_entries; ++i)1194 c->sorted_codewords[i] = bit_reverse(c->codewords[i]);1195 }11961197 qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);1198 c->sorted_codewords[c->sorted_entries] = 0xffffffff;11991200 len = c->sparse ? c->sorted_entries : c->entries;1201 // now we need to indicate how they correspond; we could either1202 // #1: sort a different data structure that says who they correspond to1203 // #2: for each sorted entry, search the original list to find who corresponds1204 // #3: for each original entry, find the sorted entry1205 // #1 requires extra storage, #2 is slow, #3 can use binary search!1206 for (i=0; i < len; ++i) {1207 int huff_len = c->sparse ? lengths[values[i]] : lengths[i];1208 if (include_in_sort(c,huff_len)) {1209 uint32 code = bit_reverse(c->codewords[i]);1210 int x=0, n=c->sorted_entries;1211 while (n > 1) {1212 // invariant: sc[x] <= code < sc[x+n]1213 int m = x + (n >> 1);1214 if (c->sorted_codewords[m] <= code) {1215 x = m;1216 n -= (n>>1);1217 } else {1218 n >>= 1;1219 }1220 }1221 assert(c->sorted_codewords[x] == code);1222 if (c->sparse) {1223 c->sorted_values[x] = values[i];1224 c->codeword_lengths[x] = huff_len;1225 } else {1226 c->sorted_values[x] = i;1227 }1228 }1229 }1230}12311232// only run while parsing the header (3 times)1233static int vorbis_validate(uint8 *data)1234{1235 static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };1236 return memcmp(data, vorbis, 6) == 0;1237}12381239// called from setup only, once per code book1240// (formula implied by specification)1241static int lookup1_values(int entries, int dim)1242{1243 int r = (int) floor(exp((float) log((float) entries) / dim));1244 if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;1245 ++r; // floor() to avoid _ftol() when non-CRT1246 if (pow((float) r+1, dim) <= entries)1247 return -1;1248 if ((int) floor(pow((float) r, dim)) > entries)1249 return -1;1250 return r;1251}12521253// called twice per file1254static void compute_twiddle_factors(int n, float *A, float *B, float *C)1255{1256 int n4 = n >> 2, n8 = n >> 3;1257 int k,k2;12581259 for (k=k2=0; k < n4; ++k,k2+=2) {1260 A[k2 ] = (float) cos(4*k*M_PI/n);1261 A[k2+1] = (float) -sin(4*k*M_PI/n);1262 B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f;1263 B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f;1264 }1265 for (k=k2=0; k < n8; ++k,k2+=2) {1266 C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);1267 C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);1268 }1269}12701271static void compute_window(int n, float *window)1272{1273 int n2 = n >> 1, i;1274 for (i=0; i < n2; ++i)1275 window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));1276}12771278static void compute_bitreverse(int n, uint16 *rev)1279{1280 int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions1281 int i, n8 = n >> 3;1282 for (i=0; i < n8; ++i)1283 rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2;1284}12851286static int init_blocksize(vorb *f, int b, int n)1287{1288 int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;1289 f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);1290 f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);1291 f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);1292 if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);1293 compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);1294 f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);1295 if (!f->window[b]) return error(f, VORBIS_outofmem);1296 compute_window(n, f->window[b]);1297 f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);1298 if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);1299 compute_bitreverse(n, f->bit_reverse[b]);1300 return TRUE;1301}13021303static void neighbors(uint16 *x, int n, int *plow, int *phigh)1304{1305 int low = -1;1306 int high = 65536;1307 int i;1308 for (i=0; i < n; ++i) {1309 if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; }1310 if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }1311 }1312}13131314// this has been repurposed so y is now the original index instead of y1315typedef struct1316{1317 uint16 x,id;1318} stbv__floor_ordering;13191320static int STBV_CDECL point_compare(const void *p, const void *q)1321{1322 stbv__floor_ordering *a = (stbv__floor_ordering *) p;1323 stbv__floor_ordering *b = (stbv__floor_ordering *) q;1324 return a->x < b->x ? -1 : a->x > b->x;1325}13261327//1328/////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////132913301331#if defined(STB_VORBIS_NO_STDIO)1332 #define USE_MEMORY(z) TRUE1333#else1334 #define USE_MEMORY(z) ((z)->stream)1335#endif13361337static uint8 get8(vorb *z)1338{1339 if (USE_MEMORY(z)) {1340 if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }1341 return *z->stream++;1342 }13431344 #ifndef STB_VORBIS_NO_STDIO1345 {1346 int c = fgetc(z->f);1347 if (c == EOF) { z->eof = TRUE; return 0; }1348 return c;1349 }1350 #endif1351}13521353static uint32 get32(vorb *f)1354{1355 uint32 x;1356 x = get8(f);1357 x += get8(f) << 8;1358 x += get8(f) << 16;1359 x += (uint32) get8(f) << 24;1360 return x;1361}13621363static int getn(vorb *z, uint8 *data, int n)1364{1365 if (USE_MEMORY(z)) {1366 if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }1367 memcpy(data, z->stream, n);1368 z->stream += n;1369 return 1;1370 }13711372 #ifndef STB_VORBIS_NO_STDIO1373 if (fread(data, n, 1, z->f) == 1)1374 return 1;1375 else {1376 z->eof = 1;1377 return 0;1378 }1379 #endif1380}13811382static void skip(vorb *z, int n)1383{1384 if (USE_MEMORY(z)) {1385 z->stream += n;1386 if (z->stream >= z->stream_end) z->eof = 1;1387 return;1388 }1389 #ifndef STB_VORBIS_NO_STDIO1390 {1391 long x = ftell(z->f);1392 fseek(z->f, x+n, SEEK_SET);1393 }1394 #endif1395}13961397static int set_file_offset(stb_vorbis *f, unsigned int loc)1398{1399 #ifndef STB_VORBIS_NO_PUSHDATA_API1400 if (f->push_mode) return 0;1401 #endif1402 f->eof = 0;1403 if (USE_MEMORY(f)) {1404 if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {1405 f->stream = f->stream_end;1406 f->eof = 1;1407 return 0;1408 } else {1409 f->stream = f->stream_start + loc;1410 return 1;1411 }1412 }1413 #ifndef STB_VORBIS_NO_STDIO1414 if (loc + f->f_start < loc || loc >= 0x80000000) {1415 loc = 0x7fffffff;1416 f->eof = 1;1417 } else {1418 loc += f->f_start;1419 }1420 if (!fseek(f->f, loc, SEEK_SET))1421 return 1;1422 f->eof = 1;1423 fseek(f->f, f->f_start, SEEK_END);1424 return 0;1425 #endif1426}142714281429static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };14301431static int capture_pattern(vorb *f)1432{1433 if (0x4f != get8(f)) return FALSE;1434 if (0x67 != get8(f)) return FALSE;1435 if (0x67 != get8(f)) return FALSE;1436 if (0x53 != get8(f)) return FALSE;1437 return TRUE;1438}14391440#define PAGEFLAG_continued_packet 11441#define PAGEFLAG_first_page 21442#define PAGEFLAG_last_page 414431444static int start_page_no_capturepattern(vorb *f)1445{1446 uint32 loc0,loc1,n;1447 if (f->first_decode && !IS_PUSH_MODE(f)) {1448 f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4;1449 }1450 // stream structure version1451 if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);1452 // header flag1453 f->page_flag = get8(f);1454 // absolute granule position1455 loc0 = get32(f);1456 loc1 = get32(f);1457 // @TODO: validate loc0,loc1 as valid positions?1458 // stream serial number -- vorbis doesn't interleave, so discard1459 get32(f);1460 //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);1461 // page sequence number1462 n = get32(f);1463 f->last_page = n;1464 // CRC321465 get32(f);1466 // page_segments1467 f->segment_count = get8(f);1468 if (!getn(f, f->segments, f->segment_count))1469 return error(f, VORBIS_unexpected_eof);1470 // assume we _don't_ know any the sample position of any segments1471 f->end_seg_with_known_loc = -2;1472 if (loc0 != ~0U || loc1 != ~0U) {1473 int i;1474 // determine which packet is the last one that will complete1475 for (i=f->segment_count-1; i >= 0; --i)1476 if (f->segments[i] < 255)1477 break;1478 // 'i' is now the index of the _last_ segment of a packet that ends1479 if (i >= 0) {1480 f->end_seg_with_known_loc = i;1481 f->known_loc_for_packet = loc0;1482 }1483 }1484 if (f->first_decode) {1485 int i,len;1486 len = 0;1487 for (i=0; i < f->segment_count; ++i)1488 len += f->segments[i];1489 len += 27 + f->segment_count;1490 f->p_first.page_end = f->p_first.page_start + len;1491 f->p_first.last_decoded_sample = loc0;1492 }1493 f->next_seg = 0;1494 return TRUE;1495}14961497static int start_page(vorb *f)1498{1499 if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);1500 return start_page_no_capturepattern(f);1501}15021503static int start_packet(vorb *f)1504{1505 while (f->next_seg == -1) {1506 if (!start_page(f)) return FALSE;1507 if (f->page_flag & PAGEFLAG_continued_packet)1508 return error(f, VORBIS_continued_packet_flag_invalid);1509 }1510 f->last_seg = FALSE;1511 f->valid_bits = 0;1512 f->packet_bytes = 0;1513 f->bytes_in_seg = 0;1514 // f->next_seg is now valid1515 return TRUE;1516}15171518static int maybe_start_packet(vorb *f)1519{1520 if (f->next_seg == -1) {1521 int x = get8(f);1522 if (f->eof) return FALSE; // EOF at page boundary is not an error!1523 if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern);1524 if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);1525 if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);1526 if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);1527 if (!start_page_no_capturepattern(f)) return FALSE;1528 if (f->page_flag & PAGEFLAG_continued_packet) {1529 // set up enough state that we can read this packet if we want,1530 // e.g. during recovery1531 f->last_seg = FALSE;1532 f->bytes_in_seg = 0;1533 return error(f, VORBIS_continued_packet_flag_invalid);1534 }1535 }1536 return start_packet(f);1537}15381539static int next_segment(vorb *f)1540{1541 int len;1542 if (f->last_seg) return 0;1543 if (f->next_seg == -1) {1544 f->last_seg_which = f->segment_count-1; // in case start_page fails1545 if (!start_page(f)) { f->last_seg = 1; return 0; }1546 if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);1547 }1548 len = f->segments[f->next_seg++];1549 if (len < 255) {1550 f->last_seg = TRUE;1551 f->last_seg_which = f->next_seg-1;1552 }1553 if (f->next_seg >= f->segment_count)1554 f->next_seg = -1;1555 assert(f->bytes_in_seg == 0);1556 f->bytes_in_seg = len;1557 return len;1558}15591560#define EOP (-1)1561#define INVALID_BITS (-1)15621563static int get8_packet_raw(vorb *f)1564{1565 if (!f->bytes_in_seg) { // CLANG!1566 if (f->last_seg) return EOP;1567 else if (!next_segment(f)) return EOP;1568 }1569 assert(f->bytes_in_seg > 0);1570 --f->bytes_in_seg;1571 ++f->packet_bytes;1572 return get8(f);1573}15741575static int get8_packet(vorb *f)1576{1577 int x = get8_packet_raw(f);1578 f->valid_bits = 0;1579 return x;1580}15811582static int get32_packet(vorb *f)1583{1584 uint32 x;1585 x = get8_packet(f);1586 x += get8_packet(f) << 8;1587 x += get8_packet(f) << 16;1588 x += (uint32) get8_packet(f) << 24;1589 return x;1590}15911592static void flush_packet(vorb *f)1593{1594 while (get8_packet_raw(f) != EOP);1595}15961597// @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important1598// as the huffman decoder?1599static uint32 get_bits(vorb *f, int n)1600{1601 uint32 z;16021603 if (f->valid_bits < 0) return 0;1604 if (f->valid_bits < n) {1605 if (n > 24) {1606 // the accumulator technique below would not work correctly in this case1607 z = get_bits(f, 24);1608 z += get_bits(f, n-24) << 24;1609 return z;1610 }1611 if (f->valid_bits == 0) f->acc = 0;1612 while (f->valid_bits < n) {1613 int z = get8_packet_raw(f);1614 if (z == EOP) {1615 f->valid_bits = INVALID_BITS;1616 return 0;1617 }1618 f->acc += z << f->valid_bits;1619 f->valid_bits += 8;1620 }1621 }16221623 assert(f->valid_bits >= n);1624 z = f->acc & ((1 << n)-1);1625 f->acc >>= n;1626 f->valid_bits -= n;1627 return z;1628}16291630// @OPTIMIZE: primary accumulator for huffman1631// expand the buffer to as many bits as possible without reading off end of packet1632// it might be nice to allow f->valid_bits and f->acc to be stored in registers,1633// e.g. cache them locally and decode locally1634static __forceinline void prep_huffman(vorb *f)1635{1636 if (f->valid_bits <= 24) {1637 if (f->valid_bits == 0) f->acc = 0;1638 do {1639 int z;1640 if (f->last_seg && !f->bytes_in_seg) return;1641 z = get8_packet_raw(f);1642 if (z == EOP) return;1643 f->acc += (unsigned) z << f->valid_bits;1644 f->valid_bits += 8;1645 } while (f->valid_bits <= 24);1646 }1647}16481649enum1650{1651 VORBIS_packet_id = 1,1652 VORBIS_packet_comment = 3,1653 VORBIS_packet_setup = 51654};16551656static int codebook_decode_scalar_raw(vorb *f, Codebook *c)1657{1658 int i;1659 prep_huffman(f);16601661 if (c->codewords == NULL && c->sorted_codewords == NULL)1662 return -1;16631664 // cases to use binary search: sorted_codewords && !c->codewords1665 // sorted_codewords && c->entries > 81666 if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) {1667 // binary search1668 uint32 code = bit_reverse(f->acc);1669 int x=0, n=c->sorted_entries, len;16701671 while (n > 1) {1672 // invariant: sc[x] <= code < sc[x+n]1673 int m = x + (n >> 1);1674 if (c->sorted_codewords[m] <= code) {1675 x = m;1676 n -= (n>>1);1677 } else {1678 n >>= 1;1679 }1680 }1681 // x is now the sorted index1682 if (!c->sparse) x = c->sorted_values[x];1683 // x is now sorted index if sparse, or symbol otherwise1684 len = c->codeword_lengths[x];1685 if (f->valid_bits >= len) {1686 f->acc >>= len;1687 f->valid_bits -= len;1688 return x;1689 }16901691 f->valid_bits = 0;1692 return -1;1693 }16941695 // if small, linear search1696 assert(!c->sparse);1697 for (i=0; i < c->entries; ++i) {1698 if (c->codeword_lengths[i] == NO_CODE) continue;1699 if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) {1700 if (f->valid_bits >= c->codeword_lengths[i]) {1701 f->acc >>= c->codeword_lengths[i];1702 f->valid_bits -= c->codeword_lengths[i];1703 return i;1704 }1705 f->valid_bits = 0;1706 return -1;1707 }1708 }17091710 error(f, VORBIS_invalid_stream);1711 f->valid_bits = 0;1712 return -1;1713}17141715#ifndef STB_VORBIS_NO_INLINE_DECODE17161717#define DECODE_RAW(var, f,c) \1718 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \1719 prep_huffman(f); \1720 var = f->acc & FAST_HUFFMAN_TABLE_MASK; \1721 var = c->fast_huffman[var]; \1722 if (var >= 0) { \1723 int n = c->codeword_lengths[var]; \1724 f->acc >>= n; \1725 f->valid_bits -= n; \1726 if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \1727 } else { \1728 var = codebook_decode_scalar_raw(f,c); \1729 }17301731#else17321733static int codebook_decode_scalar(vorb *f, Codebook *c)1734{1735 int i;1736 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)1737 prep_huffman(f);1738 // fast huffman table lookup1739 i = f->acc & FAST_HUFFMAN_TABLE_MASK;1740 i = c->fast_huffman[i];1741 if (i >= 0) {1742 f->acc >>= c->codeword_lengths[i];1743 f->valid_bits -= c->codeword_lengths[i];1744 if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }1745 return i;1746 }1747 return codebook_decode_scalar_raw(f,c);1748}17491750#define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);17511752#endif17531754#define DECODE(var,f,c) \1755 DECODE_RAW(var,f,c) \1756 if (c->sparse) var = c->sorted_values[var];17571758#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK1759 #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)1760#else1761 #define DECODE_VQ(var,f,c) DECODE(var,f,c)1762#endif1763176417651766176717681769// CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case1770// where we avoid one addition1771#define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])1772#define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])1773#define CODEBOOK_ELEMENT_BASE(c) (0)17741775static int codebook_decode_start(vorb *f, Codebook *c)1776{1777 int z = -1;17781779 // type 0 is only legal in a scalar context1780 if (c->lookup_type == 0)1781 error(f, VORBIS_invalid_stream);1782 else {1783 DECODE_VQ(z,f,c);1784 if (c->sparse) assert(z < c->sorted_entries);1785 if (z < 0) { // check for EOP1786 if (!f->bytes_in_seg)1787 if (f->last_seg)1788 return z;1789 error(f, VORBIS_invalid_stream);1790 }1791 }1792 return z;1793}17941795static int codebook_decode(vorb *f, Codebook *c, float *output, int len)1796{1797 int i,z = codebook_decode_start(f,c);1798 if (z < 0) return FALSE;1799 if (len > c->dimensions) len = c->dimensions;18001801#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK1802 if (c->lookup_type == 1) {1803 float last = CODEBOOK_ELEMENT_BASE(c);1804 int div = 1;1805 for (i=0; i < len; ++i) {1806 int off = (z / div) % c->lookup_values;1807 float val = CODEBOOK_ELEMENT_FAST(c,off) + last;1808 output[i] += val;1809 if (c->sequence_p) last = val + c->minimum_value;1810 div *= c->lookup_values;1811 }1812 return TRUE;1813 }1814#endif18151816 z *= c->dimensions;1817 if (c->sequence_p) {1818 float last = CODEBOOK_ELEMENT_BASE(c);1819 for (i=0; i < len; ++i) {1820 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1821 output[i] += val;1822 last = val + c->minimum_value;1823 }1824 } else {1825 float last = CODEBOOK_ELEMENT_BASE(c);1826 for (i=0; i < len; ++i) {1827 output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;1828 }1829 }18301831 return TRUE;1832}18331834static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step)1835{1836 int i,z = codebook_decode_start(f,c);1837 float last = CODEBOOK_ELEMENT_BASE(c);1838 if (z < 0) return FALSE;1839 if (len > c->dimensions) len = c->dimensions;18401841#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK1842 if (c->lookup_type == 1) {1843 int div = 1;1844 for (i=0; i < len; ++i) {1845 int off = (z / div) % c->lookup_values;1846 float val = CODEBOOK_ELEMENT_FAST(c,off) + last;1847 output[i*step] += val;1848 if (c->sequence_p) last = val;1849 div *= c->lookup_values;1850 }1851 return TRUE;1852 }1853#endif18541855 z *= c->dimensions;1856 for (i=0; i < len; ++i) {1857 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1858 output[i*step] += val;1859 if (c->sequence_p) last = val;1860 }18611862 return TRUE;1863}18641865static 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)1866{1867 int c_inter = *c_inter_p;1868 int p_inter = *p_inter_p;1869 int i,z, effective = c->dimensions;18701871 // type 0 is only legal in a scalar context1872 if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream);18731874 while (total_decode > 0) {1875 float last = CODEBOOK_ELEMENT_BASE(c);1876 DECODE_VQ(z,f,c);1877 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK1878 assert(!c->sparse || z < c->sorted_entries);1879 #endif1880 if (z < 0) {1881 if (!f->bytes_in_seg)1882 if (f->last_seg) return FALSE;1883 return error(f, VORBIS_invalid_stream);1884 }18851886 // if this will take us off the end of the buffers, stop short!1887 // we check by computing the length of the virtual interleaved1888 // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),1889 // and the length we'll be using (effective)1890 if (c_inter + p_inter*ch + effective > len * ch) {1891 effective = len*ch - (p_inter*ch - c_inter);1892 }18931894 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK1895 if (c->lookup_type == 1) {1896 int div = 1;1897 for (i=0; i < effective; ++i) {1898 int off = (z / div) % c->lookup_values;1899 float val = CODEBOOK_ELEMENT_FAST(c,off) + last;1900 if (outputs[c_inter])1901 outputs[c_inter][p_inter] += val;1902 if (++c_inter == ch) { c_inter = 0; ++p_inter; }1903 if (c->sequence_p) last = val;1904 div *= c->lookup_values;1905 }1906 } else1907 #endif1908 {1909 z *= c->dimensions;1910 if (c->sequence_p) {1911 for (i=0; i < effective; ++i) {1912 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1913 if (outputs[c_inter])1914 outputs[c_inter][p_inter] += val;1915 if (++c_inter == ch) { c_inter = 0; ++p_inter; }1916 last = val;1917 }1918 } else {1919 for (i=0; i < effective; ++i) {1920 float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;1921 if (outputs[c_inter])1922 outputs[c_inter][p_inter] += val;1923 if (++c_inter == ch) { c_inter = 0; ++p_inter; }1924 }1925 }1926 }19271928 total_decode -= effective;1929 }1930 *c_inter_p = c_inter;1931 *p_inter_p = p_inter;1932 return TRUE;1933}19341935static int predict_point(int x, int x0, int x1, int y0, int y1)1936{1937 int dy = y1 - y0;1938 int adx = x1 - x0;1939 // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?1940 int err = abs(dy) * (x - x0);1941 int off = err / adx;1942 return dy < 0 ? y0 - off : y0 + off;1943}19441945// the following table is block-copied from the specification1946static float inverse_db_table[256] =1947{1948 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,1949 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,1950 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,1951 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,1952 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,1953 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,1954 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,1955 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,1956 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,1957 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,1958 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,1959 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,1960 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,1961 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,1962 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,1963 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,1964 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,1965 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,1966 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,1967 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,1968 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,1969 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,1970 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,1971 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,1972 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,1973 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,1974 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,1975 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,1976 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,1977 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,1978 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,1979 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,1980 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,1981 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,1982 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,1983 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,1984 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,1985 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,1986 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,1987 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,1988 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,1989 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,1990 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,1991 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,1992 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,1993 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,1994 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,1995 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,1996 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,1997 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,1998 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,1999 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,2000 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,The first 2,000 of 5,584 lines.