/src/middleware/stb_vorbis/stb_vorbis.c

https://bitbucket.org/vivkin/gam3b00bs/ · C · 5143 lines · 3973 code · 572 blank · 598 comment · 1013 complexity · 2048a9cd8e2bddc7e14a8b87072fc020 MD5 · raw file

Large files are truncated click here to view the full file

  1. #include "stb_vorbis.h"
  2. #ifndef STB_VORBIS_HEADER_ONLY
  3. // global configuration settings (e.g. set these in the project/makefile),
  4. // or just set them in this file at the top (although ideally the first few
  5. // should be visible when the header file is compiled too, although it's not
  6. // crucial)
  7. // STB_VORBIS_NO_PUSHDATA_API
  8. // does not compile the code for the various stb_vorbis_*_pushdata()
  9. // functions
  10. // #define STB_VORBIS_NO_PUSHDATA_API
  11. // STB_VORBIS_NO_PULLDATA_API
  12. // does not compile the code for the non-pushdata APIs
  13. // #define STB_VORBIS_NO_PULLDATA_API
  14. // STB_VORBIS_NO_STDIO
  15. // does not compile the code for the APIs that use FILE *s internally
  16. // or externally (implied by STB_VORBIS_NO_PULLDATA_API)
  17. // #define STB_VORBIS_NO_STDIO
  18. // STB_VORBIS_NO_INTEGER_CONVERSION
  19. // does not compile the code for converting audio sample data from
  20. // float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
  21. // #define STB_VORBIS_NO_INTEGER_CONVERSION
  22. // STB_VORBIS_NO_FAST_SCALED_FLOAT
  23. // does not use a fast float-to-int trick to accelerate float-to-int on
  24. // most platforms which requires endianness be defined correctly.
  25. //#define STB_VORBIS_NO_FAST_SCALED_FLOAT
  26. // STB_VORBIS_MAX_CHANNELS [number]
  27. // globally define this to the maximum number of channels you need.
  28. // The spec does not put a restriction on channels except that
  29. // the count is stored in a byte, so 255 is the hard limit.
  30. // Reducing this saves about 16 bytes per value, so using 16 saves
  31. // (255-16)*16 or around 4KB. Plus anything other memory usage
  32. // I forgot to account for. Can probably go as low as 8 (7.1 audio),
  33. // 6 (5.1 audio), or 2 (stereo only).
  34. #ifndef STB_VORBIS_MAX_CHANNELS
  35. #define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone?
  36. #endif
  37. // STB_VORBIS_PUSHDATA_CRC_COUNT [number]
  38. // after a flush_pushdata(), stb_vorbis begins scanning for the
  39. // next valid page, without backtracking. when it finds something
  40. // that looks like a page, it streams through it and verifies its
  41. // CRC32. Should that validation fail, it keeps scanning. But it's
  42. // possible that _while_ streaming through to check the CRC32 of
  43. // one candidate page, it sees another candidate page. This #define
  44. // determines how many "overlapping" candidate pages it can search
  45. // at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
  46. // garbage pages could be as big as 64KB, but probably average ~16KB.
  47. // So don't hose ourselves by scanning an apparent 64KB page and
  48. // missing a ton of real ones in the interim; so minimum of 2
  49. #ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
  50. #define STB_VORBIS_PUSHDATA_CRC_COUNT 4
  51. #endif
  52. // STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
  53. // sets the log size of the huffman-acceleration table. Maximum
  54. // supported value is 24. with larger numbers, more decodings are O(1),
  55. // but the table size is larger so worse cache missing, so you'll have
  56. // to probe (and try multiple ogg vorbis files) to find the sweet spot.
  57. #ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
  58. #define STB_VORBIS_FAST_HUFFMAN_LENGTH 10
  59. #endif
  60. // STB_VORBIS_FAST_BINARY_LENGTH [number]
  61. // sets the log size of the binary-search acceleration table. this
  62. // is used in similar fashion to the fast-huffman size to set initial
  63. // parameters for the binary search
  64. // STB_VORBIS_FAST_HUFFMAN_INT
  65. // The fast huffman tables are much more efficient if they can be
  66. // stored as 16-bit results instead of 32-bit results. This restricts
  67. // the codebooks to having only 65535 possible outcomes, though.
  68. // (At least, accelerated by the huffman table.)
  69. #ifndef STB_VORBIS_FAST_HUFFMAN_INT
  70. #define STB_VORBIS_FAST_HUFFMAN_SHORT
  71. #endif
  72. // STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
  73. // If the 'fast huffman' search doesn't succeed, then stb_vorbis falls
  74. // back on binary searching for the correct one. This requires storing
  75. // extra tables with the huffman codes in sorted order. Defining this
  76. // symbol trades off space for speed by forcing a linear search in the
  77. // non-fast case, except for "sparse" codebooks.
  78. // #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
  79. // STB_VORBIS_DIVIDES_IN_RESIDUE
  80. // stb_vorbis precomputes the result of the scalar residue decoding
  81. // that would otherwise require a divide per chunk. you can trade off
  82. // space for time by defining this symbol.
  83. // #define STB_VORBIS_DIVIDES_IN_RESIDUE
  84. // STB_VORBIS_DIVIDES_IN_CODEBOOK
  85. // vorbis VQ codebooks can be encoded two ways: with every case explicitly
  86. // stored, or with all elements being chosen from a small range of values,
  87. // and all values possible in all elements. By default, stb_vorbis expands
  88. // this latter kind out to look like the former kind for ease of decoding,
  89. // because otherwise an integer divide-per-vector-element is required to
  90. // unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can
  91. // trade off storage for speed.
  92. //#define STB_VORBIS_DIVIDES_IN_CODEBOOK
  93. // STB_VORBIS_CODEBOOK_SHORTS
  94. // The vorbis file format encodes VQ codebook floats as ax+b where a and
  95. // b are floating point per-codebook constants, and x is a 16-bit int.
  96. // Normally, stb_vorbis decodes them to floats rather than leaving them
  97. // as 16-bit ints and computing ax+b while decoding. This is a speed/space
  98. // tradeoff; you can save space by defining this flag.
  99. #ifndef STB_VORBIS_CODEBOOK_SHORTS
  100. #define STB_VORBIS_CODEBOOK_FLOATS
  101. #endif
  102. // STB_VORBIS_DIVIDE_TABLE
  103. // this replaces small integer divides in the floor decode loop with
  104. // table lookups. made less than 1% difference, so disabled by default.
  105. // STB_VORBIS_NO_INLINE_DECODE
  106. // disables the inlining of the scalar codebook fast-huffman decode.
  107. // might save a little codespace; useful for debugging
  108. // #define STB_VORBIS_NO_INLINE_DECODE
  109. // STB_VORBIS_NO_DEFER_FLOOR
  110. // Normally we only decode the floor without synthesizing the actual
  111. // full curve. We can instead synthesize the curve immediately. This
  112. // requires more memory and is very likely slower, so I don't think
  113. // you'd ever want to do it except for debugging.
  114. // #define STB_VORBIS_NO_DEFER_FLOOR
  115. //////////////////////////////////////////////////////////////////////////////
  116. #ifdef STB_VORBIS_NO_PULLDATA_API
  117. #define STB_VORBIS_NO_INTEGER_CONVERSION
  118. #define STB_VORBIS_NO_STDIO
  119. #endif
  120. #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
  121. #define STB_VORBIS_NO_STDIO 1
  122. #endif
  123. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  124. #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
  125. // only need endianness for fast-float-to-int, which we don't
  126. // use for pushdata
  127. #ifndef STB_VORBIS_BIG_ENDIAN
  128. #define STB_VORBIS_ENDIAN 0
  129. #else
  130. #define STB_VORBIS_ENDIAN 1
  131. #endif
  132. #endif
  133. #endif
  134. #ifndef STB_VORBIS_NO_STDIO
  135. #include <stdio.h>
  136. #endif
  137. #ifndef STB_VORBIS_NO_CRT
  138. #include <stdlib.h>
  139. #include <string.h>
  140. #include <assert.h>
  141. #include <math.h>
  142. #include <malloc.h>
  143. #else
  144. #define NULL 0
  145. #endif
  146. #ifndef _MSC_VER
  147. #if __GNUC__
  148. #define __forceinline inline
  149. #else
  150. #define __forceinline
  151. #endif
  152. #endif
  153. #if STB_VORBIS_MAX_CHANNELS > 256
  154. #error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
  155. #endif
  156. #if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
  157. #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
  158. #endif
  159. #define MAX_BLOCKSIZE_LOG 13 // from specification
  160. #define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)
  161. typedef unsigned char uint8;
  162. typedef signed char int8;
  163. typedef unsigned short uint16;
  164. typedef signed short int16;
  165. typedef unsigned int uint32;
  166. typedef signed int int32;
  167. #ifndef TRUE
  168. #define TRUE 1
  169. #define FALSE 0
  170. #endif
  171. #ifdef STB_VORBIS_CODEBOOK_FLOATS
  172. typedef float codetype;
  173. #else
  174. typedef uint16 codetype;
  175. #endif
  176. // @NOTE
  177. //
  178. // Some arrays below are tagged "//varies", which means it's actually
  179. // a variable-sized piece of data, but rather than malloc I assume it's
  180. // small enough it's better to just allocate it all together with the
  181. // main thing
  182. //
  183. // Most of the variables are specified with the smallest size I could pack
  184. // them into. It might give better performance to make them all full-sized
  185. // integers. It should be safe to freely rearrange the structures or change
  186. // the sizes larger--nothing relies on silently truncating etc., nor the
  187. // order of variables.
  188. #define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
  189. #define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1)
  190. typedef struct
  191. {
  192. int dimensions, entries;
  193. uint8 *codeword_lengths;
  194. float minimum_value;
  195. float delta_value;
  196. uint8 value_bits;
  197. uint8 lookup_type;
  198. uint8 sequence_p;
  199. uint8 sparse;
  200. uint32 lookup_values;
  201. codetype *multiplicands;
  202. uint32 *codewords;
  203. #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
  204. int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
  205. #else
  206. int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
  207. #endif
  208. uint32 *sorted_codewords;
  209. int *sorted_values;
  210. int sorted_entries;
  211. } Codebook;
  212. typedef struct
  213. {
  214. uint8 order;
  215. uint16 rate;
  216. uint16 bark_map_size;
  217. uint8 amplitude_bits;
  218. uint8 amplitude_offset;
  219. uint8 number_of_books;
  220. uint8 book_list[16]; // varies
  221. } Floor0;
  222. typedef struct
  223. {
  224. uint8 partitions;
  225. uint8 partition_class_list[32]; // varies
  226. uint8 class_dimensions[16]; // varies
  227. uint8 class_subclasses[16]; // varies
  228. uint8 class_masterbooks[16]; // varies
  229. int16 subclass_books[16][8]; // varies
  230. uint16 Xlist[31*8+2]; // varies
  231. uint8 sorted_order[31*8+2];
  232. uint8 neighbors[31*8+2][2];
  233. uint8 floor1_multiplier;
  234. uint8 rangebits;
  235. int values;
  236. } Floor1;
  237. typedef union
  238. {
  239. Floor0 floor0;
  240. Floor1 floor1;
  241. } Floor;
  242. typedef struct
  243. {
  244. uint32 begin, end;
  245. uint32 part_size;
  246. uint8 classifications;
  247. uint8 classbook;
  248. uint8 **classdata;
  249. int16 (*residue_books)[8];
  250. } Residue;
  251. typedef struct
  252. {
  253. uint8 magnitude;
  254. uint8 angle;
  255. uint8 mux;
  256. } MappingChannel;
  257. typedef struct
  258. {
  259. uint16 coupling_steps;
  260. MappingChannel *chan;
  261. uint8 submaps;
  262. uint8 submap_floor[15]; // varies
  263. uint8 submap_residue[15]; // varies
  264. } Mapping;
  265. typedef struct
  266. {
  267. uint8 blockflag;
  268. uint8 mapping;
  269. uint16 windowtype;
  270. uint16 transformtype;
  271. } Mode;
  272. typedef struct
  273. {
  274. uint32 goal_crc; // expected crc if match
  275. int bytes_left; // bytes left in packet
  276. uint32 crc_so_far; // running crc
  277. int bytes_done; // bytes processed in _current_ chunk
  278. uint32 sample_loc; // granule pos encoded in page
  279. } CRCscan;
  280. typedef struct
  281. {
  282. uint32 page_start, page_end;
  283. uint32 after_previous_page_start;
  284. uint32 first_decoded_sample;
  285. uint32 last_decoded_sample;
  286. } ProbedPage;
  287. struct stb_vorbis
  288. {
  289. // user-accessible info
  290. unsigned int sample_rate;
  291. int channels;
  292. unsigned int setup_memory_required;
  293. unsigned int temp_memory_required;
  294. unsigned int setup_temp_memory_required;
  295. // input config
  296. #ifndef STB_VORBIS_NO_STDIO
  297. FILE *f;
  298. uint32 f_start;
  299. int close_on_free;
  300. #endif
  301. #ifdef STB_VORBIS_USE_CALLBACKS
  302. STREAM_DATA_CLLBACK data_callback;
  303. STREAM_RESET_CLLBACK reset_callback;
  304. void* user_data;
  305. uint32 cb_offset;
  306. #endif
  307. uint8 *stream;
  308. uint8 *stream_start;
  309. uint8 *stream_end;
  310. uint32 stream_len;
  311. uint8 push_mode;
  312. uint32 first_audio_page_offset;
  313. ProbedPage p_first, p_last;
  314. // memory management
  315. stb_vorbis_alloc alloc;
  316. int setup_offset;
  317. int temp_offset;
  318. // run-time results
  319. int eof;
  320. enum STBVorbisError error;
  321. // user-useful data
  322. // header info
  323. int blocksize[2];
  324. int blocksize_0, blocksize_1;
  325. int codebook_count;
  326. Codebook *codebooks;
  327. int floor_count;
  328. uint16 floor_types[64]; // varies
  329. Floor *floor_config;
  330. int residue_count;
  331. uint16 residue_types[64]; // varies
  332. Residue *residue_config;
  333. int mapping_count;
  334. Mapping *mapping;
  335. int mode_count;
  336. Mode mode_config[64]; // varies
  337. uint32 total_samples;
  338. // decode buffer
  339. float *channel_buffers[STB_VORBIS_MAX_CHANNELS];
  340. float *outputs [STB_VORBIS_MAX_CHANNELS];
  341. float *previous_window[STB_VORBIS_MAX_CHANNELS];
  342. int previous_length;
  343. #ifndef STB_VORBIS_NO_DEFER_FLOOR
  344. int16 *finalY[STB_VORBIS_MAX_CHANNELS];
  345. #else
  346. float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
  347. #endif
  348. uint32 current_loc; // sample location of next frame to decode
  349. int current_loc_valid;
  350. // per-blocksize precomputed data
  351. // twiddle factors
  352. float *A[2],*B[2],*C[2];
  353. float *window[2];
  354. uint16 *bit_reverse[2];
  355. // current page/packet/segment streaming info
  356. uint32 serial; // stream serial number for verification
  357. int last_page;
  358. int segment_count;
  359. uint8 segments[255];
  360. uint8 page_flag;
  361. uint8 bytes_in_seg;
  362. uint8 first_decode;
  363. int next_seg;
  364. int last_seg; // flag that we're on the last segment
  365. int last_seg_which; // what was the segment number of the last seg?
  366. uint32 acc;
  367. int valid_bits;
  368. int packet_bytes;
  369. int end_seg_with_known_loc;
  370. uint32 known_loc_for_packet;
  371. int discard_samples_deferred;
  372. uint32 samples_output;
  373. // push mode scanning
  374. int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching
  375. #ifndef STB_VORBIS_NO_PUSHDATA_API
  376. CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];
  377. #endif
  378. // sample-access
  379. int channel_buffer_start;
  380. int channel_buffer_end;
  381. };
  382. extern int my_prof(int slot);
  383. //#define stb_prof my_prof
  384. #ifndef stb_prof
  385. #define stb_prof(x) 0
  386. #endif
  387. #if defined(STB_VORBIS_NO_PUSHDATA_API)
  388. #define IS_PUSH_MODE(f) FALSE
  389. #elif defined(STB_VORBIS_NO_PULLDATA_API)
  390. #define IS_PUSH_MODE(f) TRUE
  391. #else
  392. #define IS_PUSH_MODE(f) ((f)->push_mode)
  393. #endif
  394. typedef struct stb_vorbis vorb;
  395. static int error(vorb *f, enum STBVorbisError e)
  396. {
  397. f->error = e;
  398. if (!f->eof && e != VORBIS_need_more_data) {
  399. f->error=e; // breakpoint for debugging
  400. }
  401. return 0;
  402. }
  403. // these functions are used for allocating temporary memory
  404. // while decoding. if you can afford the stack space, use
  405. // alloca(); otherwise, provide a temp buffer and it will
  406. // allocate out of those.
  407. #define array_size_required(count,size) (count*(sizeof(void *)+(size)))
  408. #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
  409. #ifdef dealloca
  410. #define temp_free(f,p) (f->alloc.alloc_buffer ? 0 : dealloca(size))
  411. #else
  412. #define temp_free(f,p) 0
  413. #endif
  414. #define temp_alloc_save(f) ((f)->temp_offset)
  415. #define temp_alloc_restore(f,p) ((f)->temp_offset = (p))
  416. #define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)
  417. // given a sufficiently large block of memory, make an array of pointers to subblocks of it
  418. static void *make_block_array(void *mem, int count, int size)
  419. {
  420. int i;
  421. void ** p = (void **) mem;
  422. char *q = (char *) (p + count);
  423. for (i=0; i < count; ++i) {
  424. p[i] = q;
  425. q += size;
  426. }
  427. return p;
  428. }
  429. static void *setup_malloc(vorb *f, int sz)
  430. {
  431. sz = (sz+3) & ~3;
  432. f->setup_memory_required += sz;
  433. if (f->alloc.alloc_buffer) {
  434. void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
  435. if (f->setup_offset + sz > f->temp_offset) return NULL;
  436. f->setup_offset += sz;
  437. return p;
  438. }
  439. return sz ? malloc(sz) : NULL;
  440. }
  441. static void setup_free(vorb *f, void *p)
  442. {
  443. if (f->alloc.alloc_buffer) return; // do nothing; setup mem is not a stack
  444. free(p);
  445. }
  446. static void *setup_temp_malloc(vorb *f, int sz)
  447. {
  448. sz = (sz+3) & ~3;
  449. if (f->alloc.alloc_buffer) {
  450. if (f->temp_offset - sz < f->setup_offset) return NULL;
  451. f->temp_offset -= sz;
  452. return (char *) f->alloc.alloc_buffer + f->temp_offset;
  453. }
  454. return malloc(sz);
  455. }
  456. static void setup_temp_free(vorb *f, void *p, size_t sz)
  457. {
  458. if (f->alloc.alloc_buffer) {
  459. f->temp_offset += (sz+3)&~3;
  460. return;
  461. }
  462. free(p);
  463. }
  464. #define CRC32_POLY 0x04c11db7 // from spec
  465. static uint32 crc_table[256];
  466. static void crc32_init(void)
  467. {
  468. int i,j;
  469. uint32 s;
  470. for(i=0; i < 256; i++) {
  471. for (s=i<<24, j=0; j < 8; ++j)
  472. s = (s << 1) ^ (s >= (1<<31) ? CRC32_POLY : 0);
  473. crc_table[i] = s;
  474. }
  475. }
  476. static __forceinline uint32 crc32_update(uint32 crc, uint8 byte)
  477. {
  478. return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];
  479. }
  480. // used in setup, and for huffman that doesn't go fast path
  481. static unsigned int bit_reverse(unsigned int n)
  482. {
  483. n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
  484. n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
  485. n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
  486. n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
  487. return (n >> 16) | (n << 16);
  488. }
  489. static float square(float x)
  490. {
  491. return x*x;
  492. }
  493. // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
  494. // as required by the specification. fast(?) implementation from stb.h
  495. // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
  496. static int ilog(int32 n)
  497. {
  498. static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
  499. // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
  500. if (n < (1U << 14))
  501. if (n < (1U << 4)) return 0 + log2_4[n ];
  502. else if (n < (1U << 9)) return 5 + log2_4[n >> 5];
  503. else return 10 + log2_4[n >> 10];
  504. else if (n < (1U << 24))
  505. if (n < (1U << 19)) return 15 + log2_4[n >> 15];
  506. else return 20 + log2_4[n >> 20];
  507. else if (n < (1U << 29)) return 25 + log2_4[n >> 25];
  508. else if (n < (1U << 31)) return 30 + log2_4[n >> 30];
  509. else return 0; // signed n returns 0
  510. }
  511. #ifndef M_PI
  512. #define M_PI 3.14159265358979323846264f // from CRC
  513. #endif
  514. // code length assigned to a value with no huffman encoding
  515. #define NO_CODE 255
  516. /////////////////////// LEAF SETUP FUNCTIONS //////////////////////////
  517. //
  518. // these functions are only called at setup, and only a few times
  519. // per file
  520. static float float32_unpack(uint32 x)
  521. {
  522. // from the specification
  523. uint32 mantissa = x & 0x1fffff;
  524. uint32 sign = x & 0x80000000;
  525. uint32 exp = (x & 0x7fe00000) >> 21;
  526. double res = sign ? -(double)mantissa : (double)mantissa;
  527. return (float) ldexp((float)res, exp-788);
  528. }
  529. // zlib & jpeg huffman tables assume that the output symbols
  530. // can either be arbitrarily arranged, or have monotonically
  531. // increasing frequencies--they rely on the lengths being sorted;
  532. // this makes for a very simple generation algorithm.
  533. // vorbis allows a huffman table with non-sorted lengths. This
  534. // requires a more sophisticated construction, since symbols in
  535. // order do not map to huffman codes "in order".
  536. static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values)
  537. {
  538. if (!c->sparse) {
  539. c->codewords [symbol] = huff_code;
  540. } else {
  541. c->codewords [count] = huff_code;
  542. c->codeword_lengths[count] = len;
  543. values [count] = symbol;
  544. }
  545. }
  546. static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values)
  547. {
  548. int i,k,m=0;
  549. uint32 available[32];
  550. memset(available, 0, sizeof(available));
  551. // find the first entry
  552. for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;
  553. if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
  554. // add to the list
  555. add_entry(c, 0, k, m++, len[k], values);
  556. // add all available leaves
  557. for (i=1; i <= len[k]; ++i)
  558. available[i] = 1 << (32-i);
  559. // note that the above code treats the first case specially,
  560. // but it's really the same as the following code, so they
  561. // could probably be combined (except the initial code is 0,
  562. // and I use 0 in available[] to mean 'empty')
  563. for (i=k+1; i < n; ++i) {
  564. uint32 res;
  565. int z = len[i], y;
  566. if (z == NO_CODE) continue;
  567. // find lowest available leaf (should always be earliest,
  568. // which is what the specification calls for)
  569. // note that this property, and the fact we can never have
  570. // more than one free leaf at a given level, isn't totally
  571. // trivial to prove, but it seems true and the assert never
  572. // fires, so!
  573. while (z > 0 && !available[z]) --z;
  574. if (z == 0) { assert(0); return FALSE; }
  575. res = available[z];
  576. available[z] = 0;
  577. add_entry(c, bit_reverse(res), i, m++, len[i], values);
  578. // propogate availability up the tree
  579. if (z != len[i]) {
  580. for (y=len[i]; y > z; --y) {
  581. assert(available[y] == 0);
  582. available[y] = res + (1 << (32-y));
  583. }
  584. }
  585. }
  586. return TRUE;
  587. }
  588. // accelerated huffman table allows fast O(1) match of all symbols
  589. // of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
  590. static void compute_accelerated_huffman(Codebook *c)
  591. {
  592. int i, len;
  593. for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)
  594. c->fast_huffman[i] = -1;
  595. len = c->sparse ? c->sorted_entries : c->entries;
  596. #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
  597. if (len > 32767) len = 32767; // largest possible value we can encode!
  598. #endif
  599. for (i=0; i < len; ++i) {
  600. if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
  601. uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
  602. // set table entries for all bit combinations in the higher bits
  603. while (z < FAST_HUFFMAN_TABLE_SIZE) {
  604. c->fast_huffman[z] = i;
  605. z += 1 << c->codeword_lengths[i];
  606. }
  607. }
  608. }
  609. }
  610. static int uint32_compare(const void *p, const void *q)
  611. {
  612. uint32 x = * (uint32 *) p;
  613. uint32 y = * (uint32 *) q;
  614. return x < y ? -1 : x > y;
  615. }
  616. static int include_in_sort(Codebook *c, uint8 len)
  617. {
  618. if (c->sparse) { assert(len != NO_CODE); return TRUE; }
  619. if (len == NO_CODE) return FALSE;
  620. if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;
  621. return FALSE;
  622. }
  623. // if the fast table above doesn't work, we want to binary
  624. // search them... need to reverse the bits
  625. static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values)
  626. {
  627. int i, len;
  628. // build a list of all the entries
  629. // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
  630. // this is kind of a frivolous optimization--I don't see any performance improvement,
  631. // but it's like 4 extra lines of code, so.
  632. if (!c->sparse) {
  633. int k = 0;
  634. for (i=0; i < c->entries; ++i)
  635. if (include_in_sort(c, lengths[i]))
  636. c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
  637. assert(k == c->sorted_entries);
  638. } else {
  639. for (i=0; i < c->sorted_entries; ++i)
  640. c->sorted_codewords[i] = bit_reverse(c->codewords[i]);
  641. }
  642. qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);
  643. c->sorted_codewords[c->sorted_entries] = 0xffffffff;
  644. len = c->sparse ? c->sorted_entries : c->entries;
  645. // now we need to indicate how they correspond; we could either
  646. // #1: sort a different data structure that says who they correspond to
  647. // #2: for each sorted entry, search the original list to find who corresponds
  648. // #3: for each original entry, find the sorted entry
  649. // #1 requires extra storage, #2 is slow, #3 can use binary search!
  650. for (i=0; i < len; ++i) {
  651. int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
  652. if (include_in_sort(c,huff_len)) {
  653. uint32 code = bit_reverse(c->codewords[i]);
  654. int x=0, n=c->sorted_entries;
  655. while (n > 1) {
  656. // invariant: sc[x] <= code < sc[x+n]
  657. int m = x + (n >> 1);
  658. if (c->sorted_codewords[m] <= code) {
  659. x = m;
  660. n -= (n>>1);
  661. } else {
  662. n >>= 1;
  663. }
  664. }
  665. assert(c->sorted_codewords[x] == code);
  666. if (c->sparse) {
  667. c->sorted_values[x] = values[i];
  668. c->codeword_lengths[x] = huff_len;
  669. } else {
  670. c->sorted_values[x] = i;
  671. }
  672. }
  673. }
  674. }
  675. // only run while parsing the header (3 times)
  676. static int vorbis_validate(uint8 *data)
  677. {
  678. static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };
  679. return memcmp(data, vorbis, 6) == 0;
  680. }
  681. // called from setup only, once per code book
  682. // (formula implied by specification)
  683. static int lookup1_values(int entries, int dim)
  684. {
  685. int r = (int) floor(exp((float) log((float) entries) / dim));
  686. if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning;
  687. ++r; // floor() to avoid _ftol() when non-CRT
  688. assert(pow((float) r+1, dim) > entries);
  689. assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above
  690. return r;
  691. }
  692. // called twice per file
  693. static void compute_twiddle_factors(int n, float *A, float *B, float *C)
  694. {
  695. int n4 = n >> 2, n8 = n >> 3;
  696. int k,k2;
  697. for (k=k2=0; k < n4; ++k,k2+=2) {
  698. A[k2 ] = (float) cos(4*k*M_PI/n);
  699. A[k2+1] = (float) -sin(4*k*M_PI/n);
  700. B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f;
  701. B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f;
  702. }
  703. for (k=k2=0; k < n8; ++k,k2+=2) {
  704. C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);
  705. C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
  706. }
  707. }
  708. static void compute_window(int n, float *window)
  709. {
  710. int n2 = n >> 1, i;
  711. for (i=0; i < n2; ++i)
  712. window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
  713. }
  714. static void compute_bitreverse(int n, uint16 *rev)
  715. {
  716. int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
  717. int i, n8 = n >> 3;
  718. for (i=0; i < n8; ++i)
  719. rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2;
  720. }
  721. static int init_blocksize(vorb *f, int b, int n)
  722. {
  723. int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
  724. f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);
  725. f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);
  726. f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);
  727. if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);
  728. compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
  729. f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);
  730. if (!f->window[b]) return error(f, VORBIS_outofmem);
  731. compute_window(n, f->window[b]);
  732. f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);
  733. if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);
  734. compute_bitreverse(n, f->bit_reverse[b]);
  735. return TRUE;
  736. }
  737. static void neighbors(uint16 *x, int n, int *plow, int *phigh)
  738. {
  739. int low = -1;
  740. int high = 65536;
  741. int i;
  742. for (i=0; i < n; ++i) {
  743. if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; }
  744. if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
  745. }
  746. }
  747. // this has been repurposed so y is now the original index instead of y
  748. typedef struct
  749. {
  750. uint16 x,y;
  751. } Point;
  752. int point_compare(const void *p, const void *q)
  753. {
  754. Point *a = (Point *) p;
  755. Point *b = (Point *) q;
  756. return a->x < b->x ? -1 : a->x > b->x;
  757. }
  758. //
  759. /////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
  760. #if defined(STB_VORBIS_NO_STDIO)
  761. #define USE_MEMORY(z) TRUE
  762. #else
  763. #define USE_MEMORY(z) ((z)->stream)
  764. #endif
  765. #ifdef STB_VORBIS_USE_CALLBACKS
  766. #define USE_CALLBACKS(z) ((z)->data_callback)
  767. int stb_read_from_callback(vorb* z, int size, uint8* ptr)
  768. {
  769. int read = z->data_callback(size,ptr,z->user_data);
  770. if(read < 1 && size > 0)
  771. z->eof = 1;
  772. else
  773. z->cb_offset+=read;
  774. return read;
  775. }
  776. int stb_reset_callback(vorb* z)
  777. {
  778. int result = z->reset_callback(z->user_data);
  779. if(result == -1)
  780. z->eof = 1;
  781. else
  782. {
  783. z->cb_offset = 0;
  784. z->eof = 0;
  785. }
  786. return result;
  787. }
  788. #endif
  789. static uint8 get8(vorb *z)
  790. {
  791. if (USE_MEMORY(z)) {
  792. if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
  793. return *z->stream++;
  794. }
  795. #ifdef STB_VORBIS_USE_CALLBACKS
  796. if(USE_CALLBACKS(z))
  797. {
  798. uint8 data;
  799. int read = stb_read_from_callback(z,1,&data);
  800. if(z->eof)
  801. return 0;
  802. else
  803. return data;
  804. }
  805. #endif
  806. #ifndef STB_VORBIS_NO_STDIO
  807. {
  808. int c = fgetc(z->f);
  809. if (c == EOF) { z->eof = TRUE; return 0; }
  810. return c;
  811. }
  812. #endif
  813. }
  814. static uint32 get32(vorb *f)
  815. {
  816. uint32 x;
  817. x = get8(f);
  818. x += get8(f) << 8;
  819. x += get8(f) << 16;
  820. x += get8(f) << 24;
  821. return x;
  822. }
  823. static int getn(vorb *z, uint8 *data, int n)
  824. {
  825. if (USE_MEMORY(z)) {
  826. if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }
  827. memcpy(data, z->stream, n);
  828. z->stream += n;
  829. return 1;
  830. }
  831. #ifdef STB_VORBIS_USE_CALLBACKS
  832. if(USE_CALLBACKS(z))
  833. {
  834. int read = stb_read_from_callback(z,n,data);
  835. if(read < n)
  836. {
  837. z->eof = 1;
  838. return 0;
  839. }
  840. else
  841. return 1;
  842. }
  843. #endif
  844. #ifndef STB_VORBIS_NO_STDIO
  845. if (fread(data, n, 1, z->f) == 1)
  846. return 1;
  847. else {
  848. z->eof = 1;
  849. return 0;
  850. }
  851. #endif
  852. }
  853. static void skip(vorb *z, int n)
  854. {
  855. if (USE_MEMORY(z)) {
  856. z->stream += n;
  857. if (z->stream >= z->stream_end) z->eof = 1;
  858. return;
  859. }
  860. #ifdef STB_VORBIS_USE_CALLBACKS
  861. if(USE_CALLBACKS(z))
  862. {
  863. int read = stb_read_from_callback(z,n,NULL);
  864. if(read < n)
  865. z->eof = 1;
  866. return;
  867. }
  868. #endif
  869. #ifndef STB_VORBIS_NO_STDIO
  870. {
  871. long x = ftell(z->f);
  872. fseek(z->f, x+n, SEEK_SET);
  873. }
  874. #endif
  875. }
  876. static int set_file_offset(stb_vorbis *f, unsigned int loc)
  877. {
  878. #ifndef STB_VORBIS_NO_PUSHDATA_API
  879. if (f->push_mode) return 0;
  880. #endif
  881. f->eof = 0;
  882. if (USE_MEMORY(f)) {
  883. if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
  884. f->stream = f->stream_end;
  885. f->eof = 1;
  886. return 0;
  887. } else {
  888. f->stream = f->stream_start + loc;
  889. return 1;
  890. }
  891. }
  892. #ifdef STB_VORBIS_USE_CALLBACKS
  893. if(USE_CALLBACKS(f))
  894. {
  895. int read = stb_reset_callback(f);
  896. if(read < 0)
  897. {
  898. f->eof = 1;
  899. return 0;
  900. }
  901. read = stb_read_from_callback(f,loc,NULL);
  902. if(read < loc)
  903. {
  904. f->eof = 1;
  905. return 0;
  906. }
  907. return 1;
  908. }
  909. #endif
  910. #ifndef STB_VORBIS_NO_STDIO
  911. if (loc + f->f_start < loc || loc >= 0x80000000) {
  912. loc = 0x7fffffff;
  913. f->eof = 1;
  914. } else {
  915. loc += f->f_start;
  916. }
  917. if (!fseek(f->f, loc, SEEK_SET))
  918. return 1;
  919. f->eof = 1;
  920. fseek(f->f, f->f_start, SEEK_END);
  921. return 0;
  922. #endif
  923. }
  924. static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };
  925. static int capture_pattern(vorb *f)
  926. {
  927. if (0x4f != get8(f)) return FALSE;
  928. if (0x67 != get8(f)) return FALSE;
  929. if (0x67 != get8(f)) return FALSE;
  930. if (0x53 != get8(f)) return FALSE;
  931. return TRUE;
  932. }
  933. #define PAGEFLAG_continued_packet 1
  934. #define PAGEFLAG_first_page 2
  935. #define PAGEFLAG_last_page 4
  936. static int start_page_no_capturepattern(vorb *f)
  937. {
  938. uint32 loc0,loc1,n,i;
  939. // stream structure version
  940. if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
  941. // header flag
  942. f->page_flag = get8(f);
  943. // absolute granule position
  944. loc0 = get32(f);
  945. loc1 = get32(f);
  946. // @TODO: validate loc0,loc1 as valid positions?
  947. // stream serial number -- vorbis doesn't interleave, so discard
  948. get32(f);
  949. //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);
  950. // page sequence number
  951. n = get32(f);
  952. f->last_page = n;
  953. // CRC32
  954. get32(f);
  955. // page_segments
  956. f->segment_count = get8(f);
  957. if (!getn(f, f->segments, f->segment_count))
  958. return error(f, VORBIS_unexpected_eof);
  959. // assume we _don't_ know any the sample position of any segments
  960. f->end_seg_with_known_loc = -2;
  961. if (loc0 != ~0 || loc1 != ~0) {
  962. // determine which packet is the last one that will complete
  963. for (i=f->segment_count-1; i >= 0; --i)
  964. if (f->segments[i] < 255)
  965. break;
  966. // 'i' is now the index of the _last_ segment of a packet that ends
  967. if (i >= 0) {
  968. f->end_seg_with_known_loc = i;
  969. f->known_loc_for_packet = loc0;
  970. }
  971. }
  972. if (f->first_decode) {
  973. int i,len;
  974. ProbedPage p;
  975. len = 0;
  976. for (i=0; i < f->segment_count; ++i)
  977. len += f->segments[i];
  978. len += 27 + f->segment_count;
  979. p.page_start = f->first_audio_page_offset;
  980. p.page_end = p.page_start + len;
  981. p.after_previous_page_start = p.page_start;
  982. p.first_decoded_sample = 0;
  983. p.last_decoded_sample = loc0;
  984. f->p_first = p;
  985. }
  986. f->next_seg = 0;
  987. return TRUE;
  988. }
  989. static int start_page(vorb *f)
  990. {
  991. if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);
  992. return start_page_no_capturepattern(f);
  993. }
  994. static int start_packet(vorb *f)
  995. {
  996. while (f->next_seg == -1) {
  997. if (!start_page(f)) return FALSE;
  998. if (f->page_flag & PAGEFLAG_continued_packet)
  999. return error(f, VORBIS_continued_packet_flag_invalid);
  1000. }
  1001. f->last_seg = FALSE;
  1002. f->valid_bits = 0;
  1003. f->packet_bytes = 0;
  1004. f->bytes_in_seg = 0;
  1005. // f->next_seg is now valid
  1006. return TRUE;
  1007. }
  1008. static int maybe_start_packet(vorb *f)
  1009. {
  1010. if (f->next_seg == -1) {
  1011. int x = get8(f);
  1012. if (f->eof) return FALSE; // EOF at page boundary is not an error!
  1013. if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern);
  1014. if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
  1015. if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
  1016. if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
  1017. if (!start_page_no_capturepattern(f)) return FALSE;
  1018. if (f->page_flag & PAGEFLAG_continued_packet) {
  1019. // set up enough state that we can read this packet if we want,
  1020. // e.g. during recovery
  1021. f->last_seg = FALSE;
  1022. f->bytes_in_seg = 0;
  1023. return error(f, VORBIS_continued_packet_flag_invalid);
  1024. }
  1025. }
  1026. return start_packet(f);
  1027. }
  1028. static int next_segment(vorb *f)
  1029. {
  1030. int len;
  1031. if (f->last_seg) return 0;
  1032. if (f->next_seg == -1) {
  1033. f->last_seg_which = f->segment_count-1; // in case start_page fails
  1034. if (!start_page(f)) { f->last_seg = 1; return 0; }
  1035. if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);
  1036. }
  1037. len = f->segments[f->next_seg++];
  1038. if (len < 255) {
  1039. f->last_seg = TRUE;
  1040. f->last_seg_which = f->next_seg-1;
  1041. }
  1042. if (f->next_seg >= f->segment_count)
  1043. f->next_seg = -1;
  1044. assert(f->bytes_in_seg == 0);
  1045. f->bytes_in_seg = len;
  1046. return len;
  1047. }
  1048. #define EOP (-1)
  1049. #define INVALID_BITS (-1)
  1050. static int get8_packet_raw(vorb *f)
  1051. {
  1052. if (!f->bytes_in_seg)
  1053. if (f->last_seg) return EOP;
  1054. else if (!next_segment(f)) return EOP;
  1055. assert(f->bytes_in_seg > 0);
  1056. --f->bytes_in_seg;
  1057. ++f->packet_bytes;
  1058. return get8(f);
  1059. }
  1060. static int get8_packet(vorb *f)
  1061. {
  1062. int x = get8_packet_raw(f);
  1063. f->valid_bits = 0;
  1064. return x;
  1065. }
  1066. static void flush_packet(vorb *f)
  1067. {
  1068. while (get8_packet_raw(f) != EOP);
  1069. }
  1070. // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
  1071. // as the huffman decoder?
  1072. static uint32 get_bits(vorb *f, int n)
  1073. {
  1074. uint32 z;
  1075. if (f->valid_bits < 0) return 0;
  1076. if (f->valid_bits < n) {
  1077. if (n > 24) {
  1078. // the accumulator technique below would not work correctly in this case
  1079. z = get_bits(f, 24);
  1080. z += get_bits(f, n-24) << 24;
  1081. return z;
  1082. }
  1083. if (f->valid_bits == 0) f->acc = 0;
  1084. while (f->valid_bits < n) {
  1085. int z = get8_packet_raw(f);
  1086. if (z == EOP) {
  1087. f->valid_bits = INVALID_BITS;
  1088. return 0;
  1089. }
  1090. f->acc += z << f->valid_bits;
  1091. f->valid_bits += 8;
  1092. }
  1093. }
  1094. if (f->valid_bits < 0) return 0;
  1095. z = f->acc & ((1 << n)-1);
  1096. f->acc >>= n;
  1097. f->valid_bits -= n;
  1098. return z;
  1099. }
  1100. static int32 get_bits_signed(vorb *f, int n)
  1101. {
  1102. uint32 z = get_bits(f, n);
  1103. if (z & (1 << (n-1)))
  1104. z += ~((1 << n) - 1);
  1105. return (int32) z;
  1106. }
  1107. // @OPTIMIZE: primary accumulator for huffman
  1108. // expand the buffer to as many bits as possible without reading off end of packet
  1109. // it might be nice to allow f->valid_bits and f->acc to be stored in registers,
  1110. // e.g. cache them locally and decode locally
  1111. static __forceinline void prep_huffman(vorb *f)
  1112. {
  1113. if (f->valid_bits <= 24) {
  1114. if (f->valid_bits == 0) f->acc = 0;
  1115. do {
  1116. int z;
  1117. if (f->last_seg && !f->bytes_in_seg) return;
  1118. z = get8_packet_raw(f);
  1119. if (z == EOP) return;
  1120. f->acc += z << f->valid_bits;
  1121. f->valid_bits += 8;
  1122. } while (f->valid_bits <= 24);
  1123. }
  1124. }
  1125. enum
  1126. {
  1127. VORBIS_packet_id = 1,
  1128. VORBIS_packet_comment = 3,
  1129. VORBIS_packet_setup = 5,
  1130. };
  1131. static int codebook_decode_scalar_raw(vorb *f, Codebook *c)
  1132. {
  1133. int i;
  1134. prep_huffman(f);
  1135. assert(c->sorted_codewords || c->codewords);
  1136. // cases to use binary search: sorted_codewords && !c->codewords
  1137. // sorted_codewords && c->entries > 8
  1138. if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) {
  1139. // binary search
  1140. uint32 code = bit_reverse(f->acc);
  1141. int x=0, n=c->sorted_entries, len;
  1142. while (n > 1) {
  1143. // invariant: sc[x] <= code < sc[x+n]
  1144. int m = x + (n >> 1);
  1145. if (c->sorted_codewords[m] <= code) {
  1146. x = m;
  1147. n -= (n>>1);
  1148. } else {
  1149. n >>= 1;
  1150. }
  1151. }
  1152. // x is now the sorted index
  1153. if (!c->sparse) x = c->sorted_values[x];
  1154. // x is now sorted index if sparse, or symbol otherwise
  1155. len = c->codeword_lengths[x];
  1156. if (f->valid_bits >= len) {
  1157. f->acc >>= len;
  1158. f->valid_bits -= len;
  1159. return x;
  1160. }
  1161. f->valid_bits = 0;
  1162. return -1;
  1163. }
  1164. // if small, linear search
  1165. assert(!c->sparse);
  1166. for (i=0; i < c->entries; ++i) {
  1167. if (c->codeword_lengths[i] == NO_CODE) continue;
  1168. if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) {
  1169. if (f->valid_bits >= c->codeword_lengths[i]) {
  1170. f->acc >>= c->codeword_lengths[i];
  1171. f->valid_bits -= c->codeword_lengths[i];
  1172. return i;
  1173. }
  1174. f->valid_bits = 0;
  1175. return -1;
  1176. }
  1177. }
  1178. error(f, VORBIS_invalid_stream);
  1179. f->valid_bits = 0;
  1180. return -1;
  1181. }
  1182. static int codebook_decode_scalar(vorb *f, Codebook *c)
  1183. {
  1184. int i;
  1185. if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
  1186. prep_huffman(f);
  1187. // fast huffman table lookup
  1188. i = f->acc & FAST_HUFFMAN_TABLE_MASK;
  1189. i = c->fast_huffman[i];
  1190. if (i >= 0) {
  1191. f->acc >>= c->codeword_lengths[i];
  1192. f->valid_bits -= c->codeword_lengths[i];
  1193. if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }
  1194. return i;
  1195. }
  1196. return codebook_decode_scalar_raw(f,c);
  1197. }
  1198. #ifndef STB_VORBIS_NO_INLINE_DECODE
  1199. #define DECODE_RAW(var, f,c) \
  1200. if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \
  1201. prep_huffman(f); \
  1202. var = f->acc & FAST_HUFFMAN_TABLE_MASK; \
  1203. var = c->fast_huffman[var]; \
  1204. if (var >= 0) { \
  1205. int n = c->codeword_lengths[var]; \
  1206. f->acc >>= n; \
  1207. f->valid_bits -= n; \
  1208. if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
  1209. } else { \
  1210. var = codebook_decode_scalar_raw(f,c); \
  1211. }
  1212. #else
  1213. #define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);
  1214. #endif
  1215. #define DECODE(var,f,c) \
  1216. DECODE_RAW(var,f,c) \
  1217. if (c->sparse) var = c->sorted_values[var];
  1218. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1219. #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)
  1220. #else
  1221. #define DECODE_VQ(var,f,c) DECODE(var,f,c)
  1222. #endif
  1223. // CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
  1224. // where we avoid one addition
  1225. #ifndef STB_VORBIS_CODEBOOK_FLOATS
  1226. #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off] * c->delta_value + c->minimum_value)
  1227. #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off] * c->delta_value)
  1228. #define CODEBOOK_ELEMENT_BASE(c) (c->minimum_value)
  1229. #else
  1230. #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
  1231. #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
  1232. #define CODEBOOK_ELEMENT_BASE(c) (0)
  1233. #endif
  1234. static int codebook_decode_start(vorb *f, Codebook *c, int len)
  1235. {
  1236. int z = -1;
  1237. // type 0 is only legal in a scalar context
  1238. if (c->lookup_type == 0)
  1239. error(f, VORBIS_invalid_stream);
  1240. else {
  1241. DECODE_VQ(z,f,c);
  1242. if (c->sparse) assert(z < c->sorted_entries);
  1243. if (z < 0) { // check for EOP
  1244. if (!f->bytes_in_seg)
  1245. if (f->last_seg)
  1246. return z;
  1247. error(f, VORBIS_invalid_stream);
  1248. }
  1249. }
  1250. return z;
  1251. }
  1252. static int codebook_decode(vorb *f, Codebook *c, float *output, int len)
  1253. {
  1254. int i,z = codebook_decode_start(f,c,len);
  1255. if (z < 0) return FALSE;
  1256. if (len > c->dimensions) len = c->dimensions;
  1257. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1258. if (c->lookup_type == 1) {
  1259. float last = CODEBOOK_ELEMENT_BASE(c);
  1260. int div = 1;
  1261. for (i=0; i < len; ++i) {
  1262. int off = (z / div) % c->lookup_values;
  1263. float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
  1264. output[i] += val;
  1265. if (c->sequence_p) last = val + c->minimum_value;
  1266. div *= c->lookup_values;
  1267. }
  1268. return TRUE;
  1269. }
  1270. #endif
  1271. z *= c->dimensions;
  1272. if (c->sequence_p) {
  1273. float last = CODEBOOK_ELEMENT_BASE(c);
  1274. for (i=0; i < len; ++i) {
  1275. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1276. output[i] += val;
  1277. last = val + c->minimum_value;
  1278. }
  1279. } else {
  1280. float last = CODEBOOK_ELEMENT_BASE(c);
  1281. for (i=0; i < len; ++i) {
  1282. output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1283. }
  1284. }
  1285. return TRUE;
  1286. }
  1287. static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step)
  1288. {
  1289. int i,z = codebook_decode_start(f,c,len);
  1290. float last = CODEBOOK_ELEMENT_BASE(c);
  1291. if (z < 0) return FALSE;
  1292. if (len > c->dimensions) len = c->dimensions;
  1293. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1294. if (c->lookup_type == 1) {
  1295. int div = 1;
  1296. for (i=0; i < len; ++i) {
  1297. int off = (z / div) % c->lookup_values;
  1298. float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
  1299. output[i*step] += val;
  1300. if (c->sequence_p) last = val;
  1301. div *= c->lookup_values;
  1302. }
  1303. return TRUE;
  1304. }
  1305. #endif
  1306. z *= c->dimensions;
  1307. for (i=0; i < len; ++i) {
  1308. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1309. output[i*step] += val;
  1310. if (c->sequence_p) last = val;
  1311. }
  1312. return TRUE;
  1313. }
  1314. 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)
  1315. {
  1316. int c_inter = *c_inter_p;
  1317. int p_inter = *p_inter_p;
  1318. int i,z, effective = c->dimensions;
  1319. // type 0 is only legal in a scalar context
  1320. if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream);
  1321. while (total_decode > 0) {
  1322. float last = CODEBOOK_ELEMENT_BASE(c);
  1323. DECODE_VQ(z,f,c);
  1324. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1325. assert(!c->sparse || z < c->sorted_entries);
  1326. #endif
  1327. if (z < 0) {
  1328. if (!f->bytes_in_seg)
  1329. if (f->last_seg) return FALSE;
  1330. return error(f, VORBIS_invalid_stream);
  1331. }
  1332. // if this will take us off the end of the buffers, stop short!
  1333. // we check by computing the length of the virtual interleaved
  1334. // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
  1335. // and the length we'll be using (effective)
  1336. if (c_inter + p_inter*ch + effective > len * ch) {
  1337. effective = len*ch - (p_inter*ch - c_inter);
  1338. }
  1339. #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1340. if (c->lookup_type == 1) {
  1341. int div = 1;
  1342. for (i=0; i < effective; ++i) {
  1343. int off = (z / div) % c->lookup_values;
  1344. float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
  1345. outputs[c_inter][p_inter] += val;
  1346. if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1347. if (c->sequence_p) last = val;
  1348. div *= c->lookup_values;
  1349. }
  1350. } else
  1351. #endif
  1352. {
  1353. z *= c->dimensions;
  1354. if (c->sequence_p) {
  1355. for (i=0; i < effective; ++i) {
  1356. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1357. outputs[c_inter][p_inter] += val;
  1358. if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1359. last = val;
  1360. }
  1361. } else {
  1362. for (i=0; i < effective; ++i) {
  1363. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1364. outputs[c_inter][p_inter] += val;
  1365. if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1366. }
  1367. }
  1368. }
  1369. total_decode -= effective;
  1370. }
  1371. *c_inter_p = c_inter;
  1372. *p_inter_p = p_inter;
  1373. return TRUE;
  1374. }
  1375. #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1376. static int codebook_decode_deinterleave_repeat_2(vorb *f, Codebook *c, float **outputs, int *c_inter_p, int *p_inter_p, int len, int total_decode)
  1377. {
  1378. int c_inter = *c_inter_p;
  1379. int p_inter = *p_inter_p;
  1380. int i,z, effective = c->dimensions;
  1381. // type 0 is only legal in a scalar context
  1382. if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream);
  1383. while (total_decode > 0) {
  1384. float last = CODEBOOK_ELEMENT_BASE(c);
  1385. DECODE_VQ(z,f,c);
  1386. if (z < 0) {
  1387. if (!f->bytes_in_seg)
  1388. if (f->last_seg) return FALSE;
  1389. return error(f, VORBIS_invalid_stream);
  1390. }
  1391. // if this will take us off the end of the buffers, stop short!
  1392. // we check by computing the length of the virtual interleaved
  1393. // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
  1394. // and the length we'll be using (effective)
  1395. if (c_inter + p_inter*2 + effective > len * 2) {
  1396. effective = len*2 - (p_inter*2 - c_inter);
  1397. }
  1398. {
  1399. z *= c->dimensions;
  1400. stb_prof(11);
  1401. if (c->sequence_p) {
  1402. // haven't optimized this case because I don't have any examples
  1403. for (i=0; i < effective; ++i) {
  1404. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1405. outputs[c_inter][p_inter] += val;
  1406. if (++c_inter == 2) { c_inter = 0; ++p_inter; }
  1407. last = val;
  1408. }
  1409. } else {
  1410. i=0;
  1411. if (c_inter == 1) {
  1412. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1413. outputs[c_inter][p_inter] += val;
  1414. c_inter = 0; ++p_inter;
  1415. ++i;
  1416. }
  1417. {
  1418. float *z0 = outputs[0];
  1419. float *z1 = outputs[1];
  1420. for (; i+1 < effective;) {
  1421. z0[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1422. z1[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i+1) + last;
  1423. ++p_inter;
  1424. i += 2;
  1425. }
  1426. }
  1427. if (i < effective) {
  1428. float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1429. outputs[c_inter][p_inter] += val;
  1430. if (++c_inter == 2) { c_inter = 0; ++p_inter; }
  1431. }
  1432. }
  1433. }
  1434. total_decode -= effective;
  1435. }
  1436. *c_inter_p = c_inter;
  1437. *p_inter_p = p_inter;
  1438. return TRUE;
  1439. }
  1440. #endif
  1441. static int predict_point(int x, int x0, int x1, int y0, int y1)
  1442. {
  1443. int dy = y1 - y0;
  1444. int adx = x1 - x0;
  1445. // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
  1446. int err = abs(dy) * (x - x0);
  1447. int off = err / adx;
  1448. return dy < 0 ? y0 - off : y0 + off;
  1449. }
  1450. // the following table is block-copied from the specification
  1451. static float inverse_db_table[256] =
  1452. {
  1453. 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
  1454. 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
  1455. 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
  1456. 2.2670913e-07f, 2.41