59 #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
60 #define STB_VORBIS_INCLUDE_STB_VORBIS_H
62 #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
63 #define STB_VORBIS_NO_STDIO 1
66 #ifndef STB_VORBIS_NO_STDIO
148 #ifndef STB_VORBIS_NO_PUSHDATA_API
158 const unsigned char * datablock,
int datablock_length_in_bytes,
159 int *datablock_memory_consumed_in_bytes,
173 const unsigned char *datablock,
int datablock_length_in_bytes,
217 #ifndef STB_VORBIS_NO_PULLDATA_API
225 #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
228 #if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
241 #ifndef STB_VORBIS_NO_STDIO
292 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
328 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
381 #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
387 #ifndef STB_VORBIS_HEADER_ONLY
427 #ifndef STB_VORBIS_MAX_CHANNELS
428 #define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone?
443 #ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
444 #define STB_VORBIS_PUSHDATA_CRC_COUNT 4
452 #ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
453 #define STB_VORBIS_FAST_HUFFMAN_LENGTH 10
466 #ifndef STB_VORBIS_FAST_HUFFMAN_INT
467 #define STB_VORBIS_FAST_HUFFMAN_SHORT
494 #ifdef STB_VORBIS_CODEBOOK_SHORTS
495 #error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
519 #ifdef STB_VORBIS_NO_PULLDATA_API
520 #define STB_VORBIS_NO_INTEGER_CONVERSION
521 #define STB_VORBIS_NO_STDIO
524 #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
525 #define STB_VORBIS_NO_STDIO 1
528 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
529 #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
534 #ifndef STB_VORBIS_BIG_ENDIAN
535 #define STB_VORBIS_ENDIAN 0
537 #define STB_VORBIS_ENDIAN 1
544 #ifndef STB_VORBIS_NO_STDIO
548 #ifndef STB_VORBIS_NO_CRT
555 #if defined(_MSC_VER) || defined(__MINGW32__)
558 #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)
561 #else // STB_VORBIS_NO_CRT
564 #define free(s) ((void) 0)
566 #endif // STB_VORBIS_NO_CRT
579 #define __forceinline
580 #define alloca __builtin_alloca
581 #elif !defined(_MSC_VER)
583 #define __forceinline inline
585 #define __forceinline
589 #if STB_VORBIS_MAX_CHANNELS > 256
590 #error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
593 #if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
594 #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
600 #define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])
602 #define CHECK(f) ((void) 0)
605 #define MAX_BLOCKSIZE_LOG 13 // from specification
606 #define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)
636 #define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
637 #define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1)
652 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
755 #ifndef STB_VORBIS_NO_STDIO
809 #ifndef STB_VORBIS_NO_DEFER_FLOOR
846 #ifndef STB_VORBIS_NO_PUSHDATA_API
855 #if defined(STB_VORBIS_NO_PUSHDATA_API)
856 #define IS_PUSH_MODE(f) FALSE
857 #elif defined(STB_VORBIS_NO_PULLDATA_API)
858 #define IS_PUSH_MODE(f) TRUE
860 #define IS_PUSH_MODE(f) ((f)->push_mode)
880 #define array_size_required(count,size) (count*(sizeof(void *)+(size)))
882 #define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
884 #define temp_free(f,p) (f->alloc.alloc_buffer ? 0 : dealloca(size))
886 #define temp_free(f,p) 0
888 #define temp_alloc_save(f) ((f)->temp_offset)
889 #define temp_alloc_restore(f,p) ((f)->temp_offset = (p))
891 #define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)
897 void ** p = (
void **) mem;
898 char *
q = (
char *) (p +
count);
909 f->setup_memory_required += sz;
910 if (
f->alloc.alloc_buffer) {
911 void *p = (
char *)
f->alloc.alloc_buffer +
f->setup_offset;
912 if (
f->setup_offset + sz >
f->temp_offset)
return NULL;
913 f->setup_offset += sz;
916 return sz ? malloc(sz) :
NULL;
921 if (
f->alloc.alloc_buffer)
return;
928 if (
f->alloc.alloc_buffer) {
930 f->temp_offset -= sz;
931 return (
char *)
f->alloc.alloc_buffer +
f->temp_offset;
938 if (
f->alloc.alloc_buffer) {
939 f->temp_offset += (sz+3)&~3;
945 #define CRC32_POLY 0x04c11db7 // from spec
952 for(
i=0;
i < 256;
i++) {
961 return (crc << 8) ^
crc_table[
byte ^ (crc >> 24)];
968 n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
969 n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
970 n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
971 n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
972 return (n >> 16) | (n << 16);
985 static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
991 if (n < (1 << 4))
return 0 + log2_4[n ];
992 else if (n < (1 << 9))
return 5 + log2_4[n >> 5];
993 else return 10 + log2_4[n >> 10];
994 else if (n < (1 << 24))
995 if (n < (1 << 19))
return 15 + log2_4[n >> 15];
996 else return 20 + log2_4[n >> 20];
997 else if (n < (1 << 29))
return 25 + log2_4[n >> 25];
998 else return 30 + log2_4[n >> 30];
1002 #define M_PI 3.14159265358979323846264f // from CRC
1016 uint32 mantissa =
x & 0x1fffff;
1018 uint32 exp = (
x & 0x7fe00000) >> 21;
1019 double res = sign ? -(double)mantissa : (
double)mantissa;
1020 return (
float) ldexp((
float)res, exp-788);
1047 memset(available, 0,
sizeof(available));
1049 for (k=0; k < n; ++k)
if (len[k] <
NO_CODE)
break;
1054 for (
i=1;
i <= len[k]; ++
i)
1055 available[
i] = 1
U << (32-
i);
1060 for (
i=k+1;
i < n; ++
i) {
1070 while (
z > 0 && !available[
z]) --
z;
1071 if (
z == 0) {
return FALSE; }
1073 assert(
z >= 0 &&
z < 32);
1078 assert(len[
i] >= 0 && len[
i] < 32);
1079 for (
y=len[
i];
y >
z; --
y) {
1080 assert(available[
y] == 0);
1081 available[
y] = res + (1 << (32-
y));
1098 if (len > 32767) len = 32767;
1100 for (
i=0;
i < len; ++
i) {
1113 #define STBV_CDECL __cdecl
1122 return x <
y ? -1 :
x >
y;
1162 for (
i=0;
i < len; ++
i) {
1169 int m =
x + (n >> 1);
1191 static uint8 vorbis[6] = {
'v',
'o',
'r',
'b',
'i',
's' };
1192 return memcmp(data, vorbis, 6) == 0;
1199 int r = (
int) floor(exp((
float) log((
float) entries) / dim));
1200 if ((
int) floor(pow((
float)
r+1, dim)) <= entries)
1202 assert(pow((
float)
r+1, dim) > entries);
1203 assert((
int) floor(pow((
float)
r, dim)) <= entries);
1210 int n4 = n >> 2, n8 = n >> 3;
1213 for (k=k2=0; k < n4; ++k,k2+=2) {
1214 A[k2 ] = (float) cos(4*k*
M_PI/n);
1215 A[k2+1] = (float) -sin(4*k*
M_PI/n);
1216 B[k2 ] = (float) cos((k2+1)*
M_PI/n/2) * 0.5
f;
1217 B[k2+1] = (float) sin((k2+1)*
M_PI/n/2) * 0.5
f;
1219 for (k=k2=0; k < n8; ++k,k2+=2) {
1220 C[k2 ] = (float) cos(2*(k2+1)*
M_PI/n);
1221 C[k2+1] = (float) -sin(2*(k2+1)*
M_PI/n);
1228 for (
i=0;
i < n2; ++
i)
1234 int ld =
ilog(n) - 1;
1236 for (
i=0;
i < n8; ++
i)
1242 int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
1262 for (
i=0;
i < n; ++
i) {
1263 if (
x[
i] > low &&
x[
i] <
x[n]) { *plow =
i; low =
x[
i]; }
1264 if (
x[
i] < high &&
x[
i] >
x[n]) { *phigh =
i; high =
x[
i]; }
1278 return a->
x < b->
x ? -1 : a->
x > b->
x;
1285 #if defined(STB_VORBIS_NO_STDIO)
1286 #define USE_MEMORY(z) TRUE
1288 #define USE_MEMORY(z) ((z)->stream)
1294 if (
z->stream >=
z->stream_end) {
z->eof =
TRUE;
return 0; }
1295 return *
z->stream++;
1298 #ifndef STB_VORBIS_NO_STDIO
1300 int c = fgetc(
z->f);
1301 if (c == EOF) {
z->eof =
TRUE;
return 0; }
1320 if (
z->stream+n >
z->stream_end) {
z->eof = 1;
return 0; }
1321 memcpy(data,
z->stream, n);
1326 #ifndef STB_VORBIS_NO_STDIO
1327 if (fread(data, n, 1,
z->f) == 1)
1340 if (
z->stream >=
z->stream_end)
z->eof = 1;
1343 #ifndef STB_VORBIS_NO_STDIO
1345 long x = ftell(
z->f);
1346 fseek(
z->f,
x+n, SEEK_SET);
1353 #ifndef STB_VORBIS_NO_PUSHDATA_API
1354 if (
f->push_mode)
return 0;
1358 if (
f->stream_start + loc >=
f->stream_end ||
f->stream_start + loc < f->
stream_start) {
1359 f->stream =
f->stream_end;
1363 f->stream =
f->stream_start + loc;
1367 #ifndef STB_VORBIS_NO_STDIO
1368 if (loc +
f->f_start < loc || loc >= 0x80000000) {
1374 if (!fseek(
f->f, loc, SEEK_SET))
1377 fseek(
f->f,
f->f_start, SEEK_END);
1394 #define PAGEFLAG_continued_packet 1
1395 #define PAGEFLAG_first_page 2
1396 #define PAGEFLAG_last_page 4
1418 f->segment_count =
get8(
f);
1419 if (!
getn(
f,
f->segments,
f->segment_count))
1422 f->end_seg_with_known_loc = -2;
1423 if (loc0 != ~0
U || loc1 != ~0
U) {
1426 for (
i=
f->segment_count-1;
i >= 0; --
i)
1427 if (
f->segments[
i] < 255)
1431 f->end_seg_with_known_loc =
i;
1432 f->known_loc_for_packet = loc0;
1435 if (
f->first_decode) {
1439 for (
i=0;
i <
f->segment_count; ++
i)
1440 len +=
f->segments[
i];
1441 len += 27 +
f->segment_count;
1459 while (
f->next_seg == -1) {
1466 f->packet_bytes = 0;
1467 f->bytes_in_seg = 0;
1474 if (
f->next_seg == -1) {
1476 if (
f->eof)
return FALSE;
1486 f->bytes_in_seg = 0;
1496 if (
f->last_seg)
return 0;
1497 if (
f->next_seg == -1) {
1498 f->last_seg_which =
f->segment_count-1;
1502 len =
f->segments[
f->next_seg++];
1505 f->last_seg_which =
f->next_seg-1;
1507 if (
f->next_seg >=
f->segment_count)
1509 assert(
f->bytes_in_seg == 0);
1510 f->bytes_in_seg = len;
1515 #define INVALID_BITS (-1)
1519 if (!
f->bytes_in_seg) {
1520 if (
f->last_seg)
return EOP;
1523 assert(
f->bytes_in_seg > 0);
1547 if (
f->valid_bits < 0)
return 0;
1548 if (
f->valid_bits < n) {
1555 if (
f->valid_bits == 0)
f->acc = 0;
1556 while (
f->valid_bits < n) {
1562 f->acc +=
z <<
f->valid_bits;
1566 if (
f->valid_bits < 0)
return 0;
1567 z =
f->acc & ((1 << n)-1);
1579 if (
f->valid_bits <= 24) {
1580 if (
f->valid_bits == 0)
f->acc = 0;
1583 if (
f->last_seg && !
f->bytes_in_seg)
return;
1585 if (
z ==
EOP)
return;
1586 f->acc += (unsigned)
z <<
f->valid_bits;
1588 }
while (
f->valid_bits <= 24);
1616 int m =
x + (n >> 1);
1628 if (
f->valid_bits >= len) {
1630 f->valid_bits -= len;
1658 #ifndef STB_VORBIS_NO_INLINE_DECODE
1660 #define DECODE_RAW(var, f,c) \
1661 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \
1663 var = f->acc & FAST_HUFFMAN_TABLE_MASK; \
1664 var = c->fast_huffman[var]; \
1666 int n = c->codeword_lengths[var]; \
1668 f->valid_bits -= n; \
1669 if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
1671 var = codebook_decode_scalar_raw(f,c); \
1687 if (
f->valid_bits < 0) {
f->valid_bits = 0;
return -1; }
1693 #define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);
1697 #define DECODE(var,f,c) \
1698 DECODE_RAW(var,f,c) \
1699 if (c->sparse) var = c->sorted_values[var];
1701 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1702 #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c)
1704 #define DECODE_VQ(var,f,c) DECODE(var,f,c)
1714 #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
1715 #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
1716 #define CODEBOOK_ELEMENT_BASE(c) (0)
1727 if (c->
sparse) assert(z < c->sorted_entries);
1729 if (!
f->bytes_in_seg)
1744 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1748 for (
i=0;
i < len; ++
i) {
1762 for (
i=0;
i < len; ++
i) {
1769 for (
i=0;
i < len; ++
i) {
1784 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1787 for (
i=0;
i < len; ++
i) {
1790 output[
i*step] += val;
1799 for (
i=0;
i < len; ++
i) {
1801 output[
i*step] += val;
1810 int c_inter = *c_inter_p;
1811 int p_inter = *p_inter_p;
1817 while (total_decode > 0) {
1820 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1821 assert(!c->
sparse || z < c->sorted_entries);
1824 if (!
f->bytes_in_seg)
1825 if (
f->last_seg)
return FALSE;
1833 if (c_inter + p_inter*ch + effective > len * ch) {
1834 effective = len*ch - (p_inter*ch - c_inter);
1837 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1840 for (
i=0;
i < effective; ++
i) {
1844 outputs[c_inter][p_inter] += val;
1845 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1854 for (
i=0;
i < effective; ++
i) {
1857 outputs[c_inter][p_inter] += val;
1858 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1862 for (
i=0;
i < effective; ++
i) {
1865 outputs[c_inter][p_inter] += val;
1866 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1871 total_decode -= effective;
1873 *c_inter_p = c_inter;
1874 *p_inter_p = p_inter;
1883 int err = abs(dy) * (
x - x0);
1884 int off = err / adx;
1885 return dy < 0 ? y0 - off : y0 + off;
1891 1.0649863e-07
f, 1.1341951e-07
f, 1.2079015e-07
f, 1.2863978e-07
f,
1892 1.3699951e-07
f, 1.4590251e-07
f, 1.5538408e-07
f, 1.6548181e-07
f,
1893 1.7623575e-07
f, 1.8768855e-07
f, 1.9988561e-07
f, 2.1287530e-07
f,
1894 2.2670913e-07
f, 2.4144197e-07
f, 2.5713223e-07
f, 2.7384213e-07
f,
1895 2.9163793e-07
f, 3.1059021e-07
f, 3.3077411e-07
f, 3.5226968e-07
f,
1896 3.7516214e-07
f, 3.9954229e-07
f, 4.2550680e-07
f, 4.5315863e-07
f,
1897 4.8260743e-07
f, 5.1396998e-07
f, 5.4737065e-07
f, 5.8294187e-07
f,
1898 6.2082472e-07
f, 6.6116941e-07
f, 7.0413592e-07
f, 7.4989464e-07
f,
1899 7.9862701e-07
f, 8.5052630e-07
f, 9.0579828e-07
f, 9.6466216e-07
f,
1900 1.0273513e-06
f, 1.0941144e-06
f, 1.1652161e-06
f, 1.2409384e-06
f,
1901 1.3215816e-06
f, 1.4074654e-06
f, 1.4989305e-06
f, 1.5963394e-06
f,
1902 1.7000785e-06
f, 1.8105592e-06
f, 1.9282195e-06
f, 2.0535261e-06
f,
1903 2.1869758e-06
f, 2.3290978e-06
f, 2.4804557e-06
f, 2.6416497e-06
f,
1904 2.8133190e-06
f, 2.9961443e-06
f, 3.1908506e-06
f, 3.3982101e-06
f,
1905 3.6190449e-06
f, 3.8542308e-06
f, 4.1047004e-06
f, 4.3714470e-06
f,
1906 4.6555282e-06
f, 4.9580707e-06
f, 5.2802740e-06
f, 5.6234160e-06
f,
1907 5.9888572e-06
f, 6.3780469e-06
f, 6.7925283e-06
f, 7.2339451e-06
f,
1908 7.7040476e-06
f, 8.2047000e-06
f, 8.7378876e-06
f, 9.3057248e-06
f,
1909 9.9104632e-06
f, 1.0554501e-05
f, 1.1240392e-05
f, 1.1970856e-05
f,
1910 1.2748789e-05
f, 1.3577278e-05
f, 1.4459606e-05
f, 1.5399272e-05
f,
1911 1.6400004e-05
f, 1.7465768e-05
f, 1.8600792e-05
f, 1.9809576e-05
f,
1912 2.1096914e-05
f, 2.2467911e-05
f, 2.3928002e-05
f, 2.5482978e-05
f,
1913 2.7139006e-05
f, 2.8902651e-05
f, 3.0780908e-05
f, 3.2781225e-05
f,
1914 3.4911534e-05
f, 3.7180282e-05
f, 3.9596466e-05
f, 4.2169667e-05
f,
1915 4.4910090e-05
f, 4.7828601e-05
f, 5.0936773e-05
f, 5.4246931e-05
f,
1916 5.7772202e-05
f, 6.1526565e-05
f, 6.5524908e-05
f, 6.9783085e-05
f,
1917 7.4317983e-05
f, 7.9147585e-05
f, 8.4291040e-05
f, 8.9768747e-05
f,
1918 9.5602426e-05
f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
1919 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
1920 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
1921 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
1922 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
1923 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
1924 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
1925 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
1926 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
1927 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
1928 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
1929 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
1930 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
1931 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
1932 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
1933 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
1934 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
1935 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
1936 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
1937 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
1938 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
1939 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
1940 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
1941 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
1942 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
1943 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
1944 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
1945 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
1946 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
1947 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
1948 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
1949 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
1950 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
1951 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
1952 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
1953 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
1954 0.82788260f, 0.88168307f, 0.9389798f, 1.0f
1965 #ifndef STB_VORBIS_NO_DEFER_FLOOR
1966 #define LINE_OP(a,b) a *= b
1968 #define LINE_OP(a,b) a = b
1971 #ifdef STB_VORBIS_DIVIDE_TABLE
1972 #define DIVTAB_NUMER 32
1973 #define DIVTAB_DENOM 64
1974 int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM];
1987 #ifdef STB_VORBIS_DIVIDE_TABLE
1988 if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {
1990 base = -integer_divide_table[ady][adx];
1993 base = integer_divide_table[ady][adx];
2010 ady -= abs(base) * adx;
2014 for (++
x;
x < x1; ++
x) {
2031 for (k=0; k < step; ++k)
2035 for (k=0; k < n; ) {
2049 int rtype =
f->residue_types[rn];
2050 int c =
r->classbook;
2051 int classwords =
f->codebooks[c].dimensions;
2052 int n_read =
r->end -
r->begin;
2053 int part_read = n_read /
r->part_size;
2055 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2058 int **classifications = (
int **)
temp_block_array(
f,
f->channels, part_read *
sizeof(**classifications));
2063 for (
i=0;
i < ch; ++
i)
2064 if (!do_not_decode[
i])
2065 memset(residue_buffers[
i], 0,
sizeof(
float) * n);
2067 if (rtype == 2 && ch != 1) {
2068 for (
j=0;
j < ch; ++
j)
2069 if (!do_not_decode[
j])
2074 for (pass=0; pass < 8; ++pass) {
2075 int pcount = 0, class_set = 0;
2077 while (pcount < part_read) {
2078 int z =
r->begin + pcount*
r->part_size;
2079 int c_inter = (
z & 1), p_inter =
z>>1;
2084 if (
q ==
EOP)
goto done;
2085 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2086 part_classdata[0][class_set] =
r->classdata[
q];
2088 for (
i=classwords-1;
i >= 0; --
i) {
2089 classifications[0][
i+pcount] =
q %
r->classifications;
2090 q /=
r->classifications;
2094 for (
i=0;
i < classwords && pcount < part_read; ++
i, ++pcount) {
2095 int z =
r->begin + pcount*
r->part_size;
2096 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2097 int c = part_classdata[0][class_set][
i];
2099 int c = classifications[0][pcount];
2101 int b =
r->residue_books[c][pass];
2104 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
2118 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2122 }
else if (ch == 1) {
2123 while (pcount < part_read) {
2124 int z =
r->begin + pcount*
r->part_size;
2125 int c_inter = 0, p_inter =
z;
2130 if (
q ==
EOP)
goto done;
2131 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2132 part_classdata[0][class_set] =
r->classdata[
q];
2134 for (
i=classwords-1;
i >= 0; --
i) {
2135 classifications[0][
i+pcount] =
q %
r->classifications;
2136 q /=
r->classifications;
2140 for (
i=0;
i < classwords && pcount < part_read; ++
i, ++pcount) {
2141 int z =
r->begin + pcount*
r->part_size;
2142 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2143 int c = part_classdata[0][class_set][
i];
2145 int c = classifications[0][pcount];
2147 int b =
r->residue_books[c][pass];
2158 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2163 while (pcount < part_read) {
2164 int z =
r->begin + pcount*
r->part_size;
2165 int c_inter =
z % ch, p_inter =
z/ch;
2170 if (
q ==
EOP)
goto done;
2171 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2172 part_classdata[0][class_set] =
r->classdata[
q];
2174 for (
i=classwords-1;
i >= 0; --
i) {
2175 classifications[0][
i+pcount] =
q %
r->classifications;
2176 q /=
r->classifications;
2180 for (
i=0;
i < classwords && pcount < part_read; ++
i, ++pcount) {
2181 int z =
r->begin + pcount*
r->part_size;
2182 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2183 int c = part_classdata[0][class_set][
i];
2185 int c = classifications[0][pcount];
2187 int b =
r->residue_books[c][pass];
2198 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2208 for (pass=0; pass < 8; ++pass) {
2209 int pcount = 0, class_set=0;
2210 while (pcount < part_read) {
2212 for (
j=0;
j < ch; ++
j) {
2213 if (!do_not_decode[
j]) {
2217 if (temp ==
EOP)
goto done;
2218 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2219 part_classdata[
j][class_set] =
r->classdata[temp];
2221 for (
i=classwords-1;
i >= 0; --
i) {
2222 classifications[
j][
i+pcount] = temp %
r->classifications;
2223 temp /=
r->classifications;
2229 for (
i=0;
i < classwords && pcount < part_read; ++
i, ++pcount) {
2230 for (
j=0;
j < ch; ++
j) {
2231 if (!do_not_decode[
j]) {
2232 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2233 int c = part_classdata[
j][class_set][
i];
2235 int c = classifications[
j][pcount];
2237 int b =
r->residue_books[c][pass];
2239 float *target = residue_buffers[
j];
2240 int offset =
r->begin + pcount *
r->part_size;
2241 int n =
r->part_size;
2249 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2256 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2267 void inverse_mdct_slow(
float *
buffer,
int n)
2271 float *
x = (
float *) malloc(
sizeof(*
x) * n2);
2272 memcpy(
x,
buffer,
sizeof(*
x) * n2);
2273 for (
i=0;
i < n; ++
i) {
2275 for (
j=0;
j < n2; ++
j)
2283 acc +=
x[
j] * (
float) cos(
M_PI / 2 / n * (2 *
i + 1 + n/2.0)*(2*
j+1));
2290 void inverse_mdct_slow(
float *
buffer,
int n,
vorb *
f,
int blocktype)
2294 int n2 = n >> 1, nmask = (n << 2) -1;
2295 float *
x = (
float *) malloc(
sizeof(*
x) * n2);
2296 memcpy(
x,
buffer,
sizeof(*
x) * n2);
2297 for (
i=0;
i < 4*n; ++
i)
2298 mcos[
i] = (
float) cos(
M_PI / 2 *
i / n);
2300 for (
i=0;
i < n; ++
i) {
2302 for (
j=0;
j < n2; ++
j)
2303 acc +=
x[
j] * mcos[(2 *
i + 1 + n2)*(2*
j+1) & nmask];
2311 void dct_iv_slow(
float *
buffer,
int n)
2316 int n2 = n >> 1, nmask = (n << 3) - 1;
2318 for (
i=0;
i < 8*n; ++
i)
2319 mcos[
i] = (
float) cos(
M_PI / 4 *
i / n);
2320 for (
i=0;
i < n; ++
i) {
2322 for (
j=0;
j < n; ++
j)
2323 acc +=
x[
j] * mcos[((2 *
i + 1)*(2*
j+1)) & nmask];
2328 void inverse_mdct_slow(
float *
buffer,
int n,
vorb *
f,
int blocktype)
2330 int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
2333 memcpy(temp,
buffer, n2 *
sizeof(
float));
2334 dct_iv_slow(temp, n2);
2337 for ( ;
i < n3_4; ++
i)
buffer[
i] = -temp[n3_4 -
i - 1];
2338 for ( ;
i < n ; ++
i)
buffer[
i] = -temp[
i - n3_4];
2342 #ifndef LIBVORBIS_MDCT
2343 #define LIBVORBIS_MDCT 0
2360 extern void mdct_init(mdct_lookup *lookup,
int n);
2361 extern void mdct_clear(mdct_lookup *l);
2362 extern void mdct_backward(mdct_lookup *init,
float *in,
float *out);
2369 if (M1.n == n) M = &M1;
2370 else if (M2.n == n) M = &M2;
2371 else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; }
2373 if (M2.n) __asm
int 3;
2388 float *ee0 = e + i_off;
2389 float *ee2 = ee0 + k_off;
2392 assert((n & 3) == 0);
2393 for (
i=(n>>2);
i > 0; --
i) {
2394 float k00_20, k01_21;
2395 k00_20 = ee0[ 0] - ee2[ 0];
2396 k01_21 = ee0[-1] - ee2[-1];
2399 ee2[ 0] = k00_20 *
A[0] - k01_21 *
A[1];
2400 ee2[-1] = k01_21 *
A[0] + k00_20 *
A[1];
2403 k00_20 = ee0[-2] - ee2[-2];
2404 k01_21 = ee0[-3] - ee2[-3];
2407 ee2[-2] = k00_20 *
A[0] - k01_21 *
A[1];
2408 ee2[-3] = k01_21 *
A[0] + k00_20 *
A[1];
2411 k00_20 = ee0[-4] - ee2[-4];
2412 k01_21 = ee0[-5] - ee2[-5];
2415 ee2[-4] = k00_20 *
A[0] - k01_21 *
A[1];
2416 ee2[-5] = k01_21 *
A[0] + k00_20 *
A[1];
2419 k00_20 = ee0[-6] - ee2[-6];
2420 k01_21 = ee0[-7] - ee2[-7];
2423 ee2[-6] = k00_20 *
A[0] - k01_21 *
A[1];
2424 ee2[-7] = k01_21 *
A[0] + k00_20 *
A[1];
2434 float k00_20, k01_21;
2437 float *e2 = e0 + k_off;
2439 for (
i=lim >> 2;
i > 0; --
i) {
2440 k00_20 = e0[-0] - e2[-0];
2441 k01_21 = e0[-1] - e2[-1];
2444 e2[-0] = (k00_20)*
A[0] - (k01_21) *
A[1];
2445 e2[-1] = (k01_21)*
A[0] + (k00_20) *
A[1];
2449 k00_20 = e0[-2] - e2[-2];
2450 k01_21 = e0[-3] - e2[-3];
2453 e2[-2] = (k00_20)*
A[0] - (k01_21) *
A[1];
2454 e2[-3] = (k01_21)*
A[0] + (k00_20) *
A[1];
2458 k00_20 = e0[-4] - e2[-4];
2459 k01_21 = e0[-5] - e2[-5];
2462 e2[-4] = (k00_20)*
A[0] - (k01_21) *
A[1];
2463 e2[-5] = (k01_21)*
A[0] + (k00_20) *
A[1];
2467 k00_20 = e0[-6] - e2[-6];
2468 k01_21 = e0[-7] - e2[-7];
2471 e2[-6] = (k00_20)*
A[0] - (k01_21) *
A[1];
2472 e2[-7] = (k01_21)*
A[0] + (k00_20) *
A[1];
2486 float A2 =
A[0+a_off];
2487 float A3 =
A[0+a_off+1];
2488 float A4 =
A[0+a_off*2+0];
2489 float A5 =
A[0+a_off*2+1];
2490 float A6 =
A[0+a_off*3+0];
2491 float A7 =
A[0+a_off*3+1];
2495 float *ee0 = e +i_off;
2496 float *ee2 = ee0+k_off;
2498 for (
i=n;
i > 0; --
i) {
2499 k00 = ee0[ 0] - ee2[ 0];
2500 k11 = ee0[-1] - ee2[-1];
2501 ee0[ 0] = ee0[ 0] + ee2[ 0];
2502 ee0[-1] = ee0[-1] + ee2[-1];
2503 ee2[ 0] = (k00) * A0 - (k11) * A1;
2504 ee2[-1] = (k11) * A0 + (k00) * A1;
2506 k00 = ee0[-2] - ee2[-2];
2507 k11 = ee0[-3] - ee2[-3];
2508 ee0[-2] = ee0[-2] + ee2[-2];
2509 ee0[-3] = ee0[-3] + ee2[-3];
2510 ee2[-2] = (k00) * A2 - (k11) * A3;
2511 ee2[-3] = (k11) * A2 + (k00) * A3;
2513 k00 = ee0[-4] - ee2[-4];
2514 k11 = ee0[-5] - ee2[-5];
2515 ee0[-4] = ee0[-4] + ee2[-4];
2516 ee0[-5] = ee0[-5] + ee2[-5];
2517 ee2[-4] = (k00) * A4 - (k11) * A5;
2518 ee2[-5] = (k11) * A4 + (k00) * A5;
2520 k00 = ee0[-6] - ee2[-6];
2521 k11 = ee0[-7] - ee2[-7];
2522 ee0[-6] = ee0[-6] + ee2[-6];
2523 ee0[-7] = ee0[-7] + ee2[-7];
2524 ee2[-6] = (k00) * A6 - (k11) * A7;
2525 ee2[-7] = (k11) * A6 + (k00) * A7;
2534 float k00,k11,k22,k33;
2537 k00 =
z[ 0] -
z[-4];
2540 k22 =
z[-2] -
z[-6];
2547 k33 =
z[-3] -
z[-7];
2554 k11 =
z[-1] -
z[-5];
2566 int a_off = base_n >> 3;
2567 float A2 =
A[0+a_off];
2568 float *
z = e + i_off;
2569 float *base =
z - 16 * n;
2574 k00 =
z[-0] -
z[-8];
2575 k11 =
z[-1] -
z[-9];
2576 z[-0] =
z[-0] +
z[-8];
2577 z[-1] =
z[-1] +
z[-9];
2581 k00 =
z[ -2] -
z[-10];
2582 k11 =
z[ -3] -
z[-11];
2583 z[ -2] =
z[ -2] +
z[-10];
2584 z[ -3] =
z[ -3] +
z[-11];
2585 z[-10] = (k00+k11) * A2;
2586 z[-11] = (k11-k00) * A2;
2588 k00 =
z[-12] -
z[ -4];
2589 k11 =
z[ -5] -
z[-13];
2590 z[ -4] =
z[ -4] +
z[-12];
2591 z[ -5] =
z[ -5] +
z[-13];
2595 k00 =
z[-14] -
z[ -6];
2596 k11 =
z[ -7] -
z[-15];
2597 z[ -6] =
z[ -6] +
z[-14];
2598 z[ -7] =
z[ -7] +
z[-15];
2599 z[-14] = (k00+k11) * A2;
2600 z[-15] = (k00-k11) * A2;
2610 int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
2614 float *buf2 = (
float *)
temp_alloc(f, n2 *
sizeof(*buf2));
2617 float *
A = f->
A[blocktype];
2639 float *d,*e, *
AA, *e_stop;
2644 while (e != e_stop) {
2645 d[1] = (e[0] *
AA[0] - e[2]*
AA[1]);
2646 d[0] = (e[0] *
AA[1] + e[2]*
AA[0]);
2654 d[1] = (-e[2] *
AA[0] - -e[0]*
AA[1]);
2655 d[0] = (-e[2] *
AA[1] + -e[0]*
AA[0]);
2673 float *
AA = &
A[n2-8];
2674 float *d0,*d1, *e0, *e1;
2683 float v40_20, v41_21;
2685 v41_21 = e0[1] - e1[1];
2686 v40_20 = e0[0] - e1[0];
2687 d0[1] = e0[1] + e1[1];
2688 d0[0] = e0[0] + e1[0];
2689 d1[1] = v41_21*
AA[4] - v40_20*
AA[5];
2690 d1[0] = v40_20*
AA[4] + v41_21*
AA[5];
2692 v41_21 = e0[3] - e1[3];
2693 v40_20 = e0[2] - e1[2];
2694 d0[3] = e0[3] + e1[3];
2695 d0[2] = e0[2] + e1[2];
2696 d1[3] = v41_21*
AA[0] - v40_20*
AA[1];
2697 d1[2] = v40_20*
AA[0] + v41_21*
AA[1];
2729 for (; l < (ld-3)>>1; ++l) {
2730 int k0 = n >> (l+2), k0_2 = k0>>1;
2731 int lim = 1 << (l+1);
2733 for (
i=0;
i < lim; ++
i)
2737 for (; l < ld-6; ++l) {
2738 int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1;
2739 int rlim = n >> (l+6),
r;
2740 int lim = 1 << (l+1);
2744 for (
r=rlim;
r > 0; --
r) {
2769 float *d0 = &
v[n4-4];
2770 float *d1 = &
v[n2-4];
2800 float *
C = f->
C[blocktype];
2807 float a02,a11,b0,b1,b2,b3;
2812 b0 =
C[1]*a02 +
C[0]*a11;
2813 b1 =
C[1]*a11 -
C[0]*a02;
2826 b0 =
C[3]*a02 +
C[2]*a11;
2827 b1 =
C[3]*a11 -
C[2]*a02;
2854 float *d0,*d1,*d2,*d3;
2856 float *
B = f->
B[blocktype] + n2 - 8;
2857 float *e = buf2 + n2 - 8;
2865 p3 = e[6]*
B[7] - e[7]*
B[6];
2866 p2 = -e[6]*
B[6] - e[7]*
B[7];
2873 p1 = e[4]*
B[5] - e[5]*
B[4];
2874 p0 = -e[4]*
B[4] - e[5]*
B[5];
2881 p3 = e[2]*
B[3] - e[3]*
B[2];
2882 p2 = -e[2]*
B[2] - e[3]*
B[3];
2889 p1 = e[0]*
B[1] - e[1]*
B[0];
2890 p0 = -e[0]*
B[0] - e[1]*
B[1];
2912 void inverse_mdct_naive(
float *
buffer,
int n)
2915 float A[1 << 12],
B[1 << 12],
C[1 << 11];
2916 int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
2917 int n3_4 = n - n4, ld;
2920 float u[1 << 13],
X[1 << 13],
v[1 << 13],
w[1 << 13];
2923 for (k=k2=0; k < n4; ++k,k2+=2) {
2924 A[k2 ] = (float) cos(4*k*
M_PI/n);
2925 A[k2+1] = (float) -sin(4*k*
M_PI/n);
2926 B[k2 ] = (float) cos((k2+1)*
M_PI/n/2);
2927 B[k2+1] = (float) sin((k2+1)*
M_PI/n/2);
2929 for (k=k2=0; k < n8; ++k,k2+=2) {
2930 C[k2 ] = (float) cos(2*(k2+1)*
M_PI/n);
2931 C[k2+1] = (float) -sin(2*(k2+1)*
M_PI/n);
2942 for (k=0; k < n2; ++k)
u[k] =
buffer[k];
2943 for ( ; k < n ; ++k)
u[k] = -
buffer[n - k - 1];
2946 for (k=k2=k4=0; k < n4; k+=1, k2+=2, k4+=4) {
2947 v[n-k4-1] = (
u[k4] -
u[n-k4-1]) *
A[k2] - (
u[k4+2] -
u[n-k4-3])*
A[k2+1];
2948 v[n-k4-3] = (
u[k4] -
u[n-k4-1]) *
A[k2+1] + (
u[k4+2] -
u[n-k4-3])*
A[k2];
2951 for (k=k4=0; k < n8; k+=1, k4+=4) {
2952 w[n2+3+k4] =
v[n2+3+k4] +
v[k4+3];
2953 w[n2+1+k4] =
v[n2+1+k4] +
v[k4+1];
2954 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];
2955 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];
2959 for (l=0; l < ld-3; ++l) {
2960 int k0 = n >> (l+2), k1 = 1 << (l+3);
2961 int rlim = n >> (l+4), r4,
r;
2962 int s2lim = 1 << (l+2), s2;
2963 for (
r=r4=0;
r < rlim; r4+=4,++
r) {
2964 for (s2=0; s2 < s2lim; s2+=2) {
2965 u[n-1-k0*s2-r4] =
w[n-1-k0*s2-r4] +
w[n-1-k0*(s2+1)-r4];
2966 u[n-3-k0*s2-r4] =
w[n-3-k0*s2-r4] +
w[n-3-k0*(s2+1)-r4];
2967 u[n-1-k0*(s2+1)-r4] = (
w[n-1-k0*s2-r4] -
w[n-1-k0*(s2+1)-r4]) *
A[
r*k1]
2968 - (
w[n-3-k0*s2-r4] -
w[n-3-k0*(s2+1)-r4]) *
A[
r*k1+1];
2969 u[n-3-k0*(s2+1)-r4] = (
w[n-3-k0*s2-r4] -
w[n-3-k0*(s2+1)-r4]) *
A[
r*k1]
2970 + (
w[n-1-k0*s2-r4] -
w[n-1-k0*(s2+1)-r4]) *
A[
r*k1+1];
2975 memcpy(
w,
u,
sizeof(
u));
2980 for (
i=0;
i < n8; ++
i) {
2992 int i8 =
i << 3, j8 =
j << 3;
2993 v[j8+1] =
u[i8+1],
v[i8+1] =
u[j8 + 1];
2994 v[j8+3] =
u[i8+3],
v[i8+3] =
u[j8 + 3];
2995 v[j8+5] =
u[i8+5],
v[i8+5] =
u[j8 + 5];
2996 v[j8+7] =
u[i8+7],
v[i8+7] =
u[j8 + 7];
3000 for (k=0; k < n2; ++k) {
3004 for (k=k2=k4=0; k < n8; ++k, k2 += 2, k4 += 4) {
3006 u[n-2-k2] =
w[k4+1];
3007 u[n3_4 - 1 - k2] =
w[k4+2];
3008 u[n3_4 - 2 - k2] =
w[k4+3];
3011 for (k=k2=0; k < n8; ++k, k2 += 2) {
3012 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;
3013 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;
3014 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;
3015 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;
3018 for (k=k2=0; k < n4; ++k,k2 += 2) {
3019 X[k] =
v[k2+n2]*
B[k2 ] +
v[k2+1+n2]*
B[k2+1];
3020 X[n2-1-k] =
v[k2+n2]*
B[k2+1] -
v[k2+1+n2]*
B[k2 ];
3033 for ( ;
i < n3_4; ++
i)
buffer[
i] = -
s *
X[n3_4 -
i - 1];
3047 #ifndef STB_VORBIS_NO_DEFER_FLOOR
3065 #ifndef STB_VORBIS_NO_DEFER_FLOOR
3081 for (
j=lx;
j < n2; ++
j)
3106 int i, n, prev, next, window_center;
3140 window_center = n >> 1;
3146 *p_left_end = window_center;
3152 *p_right_start = window_center;
3163 int zero_channel[256];
3164 int really_zero_channel[256];
3186 uint8 step2_flag[256];
3187 static int range_list[4] = { 256, 128, 86, 64 };
3197 int csub = (1 << cbits)-1;
3203 for (k=0; k < cdim; ++k) {
3205 cval = cval >> cbits;
3210 finalY[offset++] = temp;
3212 finalY[offset++] = 0;
3216 step2_flag[0] = step2_flag[1] = 1;
3218 int low, high, pred, highroom, lowroom, room, val;
3224 highroom =
range - pred;
3226 if (highroom < lowroom)
3227 room = highroom * 2;
3231 step2_flag[low] = step2_flag[high] = 1;
3234 if (highroom > lowroom)
3235 finalY[
j] = val - lowroom + pred;
3237 finalY[
j] = pred - val + highroom - 1;
3240 finalY[
j] = pred - ((val+1)>>1);
3242 finalY[
j] = pred + (val>>1);
3249 #ifdef STB_VORBIS_NO_DEFER_FLOOR
3250 do_floor(f, map,
i, n, f->floor_buffers[
i], finalY, step2_flag);
3260 zero_channel[
i] =
TRUE;
3274 memcpy(really_zero_channel, zero_channel,
sizeof(really_zero_channel[0]) * f->
channels);
3285 uint8 do_not_decode[256];
3289 if (zero_channel[
j]) {
3290 do_not_decode[ch] =
TRUE;
3291 residue_buffers[ch] =
NULL;
3293 do_not_decode[ch] =
FALSE;
3312 for (
j=0;
j < n2; ++
j) {
3316 m2 = m[
j], a2 = m[
j] - a[
j];
3318 a2 = m[
j], m2 = m[
j] + a[
j];
3321 m2 = m[
j], a2 = m[
j] + a[
j];
3323 a2 = m[
j], m2 = m[
j] - a[
j];
3331 #ifndef STB_VORBIS_NO_DEFER_FLOOR
3333 if (really_zero_channel[
i]) {
3341 if (really_zero_channel[
i]) {
3344 for (
j=0;
j < n2; ++
j)
3374 left_start = right_start;
3375 *p_left = left_start;
3378 *p_left = left_start;
3396 if (current_end < f->current_loc + (right_end-left_start)) {
3397 if (current_end < f->current_loc) {
3404 if (*len > right_end) *len = right_end;
3429 int mode, left_end, right_end;
3450 for (
j=0;
j < n; ++
j)
3483 return right - left;
3488 int len,
right, left, res;
3495 #ifndef STB_VORBIS_NO_PUSHDATA_API
3545 for (
s=0;
s < n; ++
s) {
3559 #endif // !STB_VORBIS_NO_PUSHDATA_API
3564 int len,
i,
j,k, max_submaps = 0;
3565 int longest_floorlist=0;
3619 #ifndef STB_VORBIS_NO_PUSHDATA_API
3644 int ordered, sorted_count;
3670 int current_entry = 0;
3671 int current_length =
get_bits(f,5) + 1;
3672 while (current_entry < c->entries) {
3673 int limit = c->
entries - current_entry;
3676 memset(lengths + current_entry, current_length, n);
3686 if (lengths[
j] == 32)
3709 sorted_count = total;
3712 #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
3791 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3793 int len, sparse = c->
sparse;
3803 for (
j=0;
j < len; ++
j) {
3808 float val = mults[off];
3838 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3851 for (
i=0;
i <
x; ++
i) {
3884 for (
j=0;
j <= max_class; ++
j) {
3924 if (g->
values > longest_floorlist)
3925 longest_floorlist = g->
values;
3935 uint8 residue_cascade[64];
3946 for (
j=0;
j <
r->classifications; ++
j) {
3951 residue_cascade[
j] = high_bits*8 + low_bits;
3953 r->residue_books = (short (*)[8])
setup_malloc(f,
sizeof(
r->residue_books[0]) *
r->classifications);
3955 for (
j=0;
j <
r->classifications; ++
j) {
3956 for (k=0; k < 8; ++k) {
3957 if (residue_cascade[
j] & (1 << k)) {
3961 r->residue_books[
j][k] = -1;
3975 for (k=classwords-1; k >= 0; --k) {
3976 r->classdata[
j][k] = temp %
r->classifications;
3977 temp /=
r->classifications;
4053 #ifdef STB_VORBIS_NO_DEFER_FLOOR
4064 #ifdef STB_VORBIS_DIVIDE_TABLE
4065 if (integer_divide_table[1][1]==0)
4066 for (
i=0;
i < DIVTAB_NUMER; ++
i)
4067 for (
j=1;
j < DIVTAB_DENOM; ++
j)
4068 integer_divide_table[
i][
j] =
i /
j;
4077 int i,max_part_read=0;
4080 int n_read =
r->end -
r->begin;
4081 int part_read = n_read /
r->part_size;
4082 if (part_read > max_part_read)
4083 max_part_read = part_read;
4085 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
4086 classify_mem = f->
channels * (
sizeof(
void*) + max_part_read *
sizeof(
uint8 *));
4088 classify_mem = f->
channels * (
sizeof(
void*) + max_part_read *
sizeof(
int *));
4149 #ifdef STB_VORBIS_NO_DEFER_FLOOR
4154 for (
i=0;
i < 2; ++
i) {
4161 #ifndef STB_VORBIS_NO_STDIO
4168 if (p ==
NULL)
return;
4175 memset(p, 0,
sizeof(*p));
4186 #ifndef STB_VORBIS_NO_STDIO
4225 #ifndef STB_VORBIS_NO_PUSHDATA_API
4248 if (data_len < 4)
return 0;
4251 for (
i=0;
i < data_len; ++
i) {
4252 if (data[
i] == 0x4f) {
4257 if (
i+26 >= data_len ||
i+27+data[
i+26] >= data_len) {
4264 len = 27 + data[
i+26];
4265 for (
j=0;
j < data[
i+26]; ++
j)
4266 len += data[
i+27+
j];
4269 for (
j=0;
j < 22; ++
j)
4272 for ( ;
j < 26; ++
j)
4278 f->
scan[n].
goal_crc = data[
i+22] + (data[
i+23] << 8) + (data[
i+24]<<16) + (data[
i+25]<<24);
4281 if (data[
i+27+data[
i+26]-1] == 255)
4284 f->
scan[n].
sample_loc = data[
i+6] + (data[
i+7] << 8) + (data[
i+ 8]<<16) + (data[
i+ 9]<<24);
4299 if (m > data_len - n) m = data_len - n;
4302 for (
j=0;
j < m; ++
j)
4332 const uint8 *data,
int data_len,
4367 return (
int) (f->
stream - data);
4377 return (
int) (f->
stream - data);
4397 return (
int) (f->
stream - data);
4401 const unsigned char *data,
int data_len,
4428 #endif // STB_VORBIS_NO_PUSHDATA_API
4432 #ifndef STB_VORBIS_NO_PUSHDATA_API
4436 #ifndef STB_VORBIS_NO_STDIO
4437 return (
unsigned int) (ftell(f->
f) - f->
f_start);
4441 #ifndef STB_VORBIS_NO_PULLDATA_API
4450 if (f->
eof)
return 0;
4459 for (
i=1;
i < 4; ++
i)
4462 if (f->
eof)
return 0;
4466 for (
i=0;
i < 4; ++
i)
4469 header[
i] =
get8(f);
4470 if (f->
eof)
return 0;
4471 if (header[4] != 0)
goto invalid;
4472 goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24);
4473 for (
i=22;
i < 26; ++
i)
4476 for (
i=0;
i < 27; ++
i)
4479 for (
i=0;
i < header[26]; ++
i) {
4484 if (len && f->
eof)
return 0;
4485 for (
i=0;
i < len; ++
i)
4500 if (header[5] & 0x04)
4517 #define SAMPLE_unknown 0xffffffff
4530 uint8 header[27], lacing[255];
4537 getn(f, header, 27);
4538 if (header[0] !=
'O' || header[1] !=
'g' || header[2] !=
'g' || header[3] !=
'S')
4540 getn(f, lacing, header[26]);
4544 for (
i=0;
i < header[26]; ++
i)
4548 z->page_end =
z->page_start + 27 + header[26] + len;
4551 z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
4562 unsigned int previous_safe, end;
4566 previous_safe = limit_offset - 65536;
4588 int i, start_seg_with_known_loc, end_pos, page_start;
4589 uint32 delta, stream_length, padding;
4590 double offset = 0, bytes_per_sample = 0;
4602 if (sample_number < padding)
4605 sample_number -= padding;
4615 assert(
right.last_decoded_sample != ~0
U);
4628 if (delta <= 65536) {
4636 bytes_per_sample = data_bytes /
right.last_decoded_sample;
4642 if (error < 0 && error > -8000)
error = -8000;
4643 offset +=
error * 2;
4649 if (offset >
right.page_start - 65536)
4650 offset =
right.page_start - 65536;
4688 assert(end_pos >= 0);
4691 for (
i = end_pos;
i > 0; --
i)
4695 start_seg_with_known_loc =
i;
4716 f->
next_seg = start_seg_with_known_loc;
4718 for (
i = 0;
i < start_seg_with_known_loc;
i++)
4737 int bits_read, bytes_read;
4746 bytes_read = (bits_read + 7) / 8;
4750 skip(f, -bytes_read);
4762 uint32 max_frame_samples;
4776 int left_start, left_end, right_start, right_end,
mode, frame_samples;
4780 frame_samples = right_start - left_start;
4781 if (f->
current_loc + frame_samples > sample_number) {
4783 }
else if (f->
current_loc + frame_samples + max_frame_samples > sample_number) {
4808 assert(sample_number > frame_start);
4828 unsigned int restore_offset, previous_safe;
4829 unsigned int end, last_page_loc;
4871 previous_safe = last_page_loc+1;
4878 getn(f, (
unsigned char *)header, 6);
4882 if (lo == 0xffffffff && hi == 0xffffffff) {
4926 if (output) *output = f->
outputs;
4930 #ifndef STB_VORBIS_NO_STDIO
4955 unsigned int len, start;
4956 start = (
unsigned int) ftell(file);
4957 fseek(file, 0, SEEK_END);
4958 len = (
unsigned int) (ftell(file) - start);
4959 fseek(file, start, SEEK_SET);
4965 FILE *f = fopen(filename,
"rb");
4971 #endif // STB_VORBIS_NO_STDIO
4997 #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
4998 #define PLAYBACK_MONO 1
4999 #define PLAYBACK_LEFT 2
5000 #define PLAYBACK_RIGHT 4
5002 #define L (PLAYBACK_LEFT | PLAYBACK_MONO)
5003 #define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO)
5004 #define R (PLAYBACK_RIGHT | PLAYBACK_MONO)
5018 #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
5024 #define FASTDEF(x) float_conv x
5026 #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
5027 #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
5028 #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
5029 #define check_endianness()
5031 #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
5032 #define check_endianness()
5040 for (
i=0;
i < len; ++
i) {
5043 if ((
unsigned int) (
v + 32768) > 65535)
5044 v =
v < 0 ? -32768 : 32767;
5051 #define BUFFER_SIZE 32
5057 if (o + n > len) n = len - o;
5058 for (
j=0;
j < num_c; ++
j) {
5060 for (
i=0;
i < n; ++
i)
5064 for (
i=0;
i < n; ++
i) {
5067 if ((
unsigned int) (
v + 32768) > 65535)
5068 v =
v < 0 ? -32768 : 32767;
5076 #define BUFFER_SIZE 32
5081 for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
5085 if (o + n > len) n = len - o;
5086 for (
j=0;
j < num_c; ++
j) {
5089 for (
i=0;
i < n; ++
i) {
5094 for (
i=0;
i < n; ++
i) {
5098 for (
i=0;
i < n; ++
i) {
5103 for (
i=0;
i < (n<<1); ++
i) {
5106 if ((
unsigned int) (
v + 32768) > 65535)
5107 v =
v < 0 ? -32768 : 32767;
5116 if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5118 for (
i=0;
i < buf_c; ++
i)
5121 int limit = buf_c < data_c ? buf_c : data_c;
5122 for (
i=0;
i < limit; ++
i)
5124 for ( ;
i < buf_c; ++
i)
5125 memset(
buffer[
i]+b_offset, 0,
sizeof(
short) * samples);
5133 if (len > num_samples) len = num_samples;
5143 if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5145 for (
i=0;
i < buf_c; ++
i)
5148 int limit = buf_c < data_c ? buf_c : data_c;
5150 for (
j=0;
j < len; ++
j) {
5151 for (
i=0;
i < limit; ++
i) {
5153 float f = data[
i][d_offset+
j];
5155 if ((
unsigned int) (
v + 32768) > 65535)
5156 v =
v < 0 ? -32768 : 32767;
5159 for ( ;
i < buf_c; ++
i)
5172 if (len*num_c > num_shorts) len = num_shorts / num_c;
5187 if (n+k >= len) k = len - n;
5193 if (n == len)
break;
5207 if (n+k >= len) k = len - n;
5212 if (n == len)
break;
5218 #ifndef STB_VORBIS_NO_STDIO
5221 int data_len, offset, total, limit,
error;
5224 if (
v ==
NULL)
return -1;
5225 limit =
v->channels * 4096;
5228 *sample_rate =
v->sample_rate;
5229 offset = data_len = 0;
5231 data = (
short *) malloc(total *
sizeof(*data));
5240 offset += n *
v->channels;
5241 if (offset + limit > total) {
5244 data2 = (
short *) realloc(data, total *
sizeof(*data));
5245 if (data2 ==
NULL) {
5261 int data_len, offset, total, limit,
error;
5264 if (
v ==
NULL)
return -1;
5265 limit =
v->channels * 4096;
5268 *sample_rate =
v->sample_rate;
5269 offset = data_len = 0;
5271 data = (
short *) malloc(total *
sizeof(*data));
5280 offset += n *
v->channels;
5281 if (offset + limit > total) {
5284 data2 = (
short *) realloc(data, total *
sizeof(*data));
5285 if (data2 ==
NULL) {
5297 #endif // STB_VORBIS_NO_INTEGER_CONVERSION
5309 if (n+k >= len) k = len - n;
5310 for (
j=0;
j < k; ++
j) {
5311 for (
i=0;
i <
z; ++
i)
5332 while (n < num_samples) {
5335 if (n+k >= num_samples) k = num_samples - n;
5337 for (
i=0;
i <
z; ++
i)
5340 memset(
buffer[
i]+n, 0,
sizeof(
float) * k);
5344 if (n == num_samples)
5351 #endif // STB_VORBIS_NO_PULLDATA_API
5406 #endif // STB_VORBIS_HEADER_ONLY