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