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