Bug Summary

File:c:\siege\siege/src/siege/internal/stb/stb_vorbis.c
Location:line 5219, column 22
Description:Value stored to 'z' is never read

Annotated Source Code

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