63#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
64#define STB_VORBIS_INCLUDE_STB_VORBIS_H
66#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
67#define STB_VORBIS_NO_STDIO
70#ifndef STB_VORBIS_NO_STDIO
75#if defined(PLATFORM_ANDROID)
83#ifdef STB_VORBIS_STATIC
163#ifndef STB_VORBIS_NO_PUSHDATA_API
173 const unsigned char * datablock,
int datablock_length_in_bytes,
174 int *datablock_memory_consumed_in_bytes,
188 const unsigned char *datablock,
int datablock_length_in_bytes,
232#ifndef STB_VORBIS_NO_PULLDATA_API
240#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
243#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
256#ifndef STB_VORBIS_NO_STDIO
307#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
343#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
402#ifdef STB_VORBIS_IMPLEMENTATION
442#ifndef STB_VORBIS_MAX_CHANNELS
443#define STB_VORBIS_MAX_CHANNELS 16
458#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
459#define STB_VORBIS_PUSHDATA_CRC_COUNT 4
467#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
468#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10
481#ifndef STB_VORBIS_FAST_HUFFMAN_INT
482#define STB_VORBIS_FAST_HUFFMAN_SHORT
509#ifdef STB_VORBIS_CODEBOOK_SHORTS
510#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
534#ifdef STB_VORBIS_NO_PULLDATA_API
535 #define STB_VORBIS_NO_INTEGER_CONVERSION
536 #define STB_VORBIS_NO_STDIO
539#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
540 #define STB_VORBIS_NO_STDIO 1
543#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
544#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
549 #ifndef STB_VORBIS_BIG_ENDIAN
550 #define STB_VORBIS_ENDIAN 0
552 #define STB_VORBIS_ENDIAN 1
559#ifndef STB_VORBIS_NO_STDIO
563#ifndef STB_VORBIS_NO_CRT
570 #if defined(_MSC_VER) || defined(__MINGW32__)
573 #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__) || defined(__APPLE__)
579 #define free(s) ((void) 0)
594 #define __forceinline
596 #define alloca(s) __builtin_alloca(s)
598#elif !defined(_MSC_VER)
600 #define __forceinline inline
602 #define __forceinline
606#if STB_VORBIS_MAX_CHANNELS > 256
607#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
610#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
611#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
617#define STBV_CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1])
619#define STBV_CHECK(f) ((void) 0)
622#define STBV_MAX_BLOCKSIZE_LOG 13
623#define STBV_MAX_BLOCKSIZE (1 << STBV_MAX_BLOCKSIZE_LOG)
626typedef unsigned char stbv_uint8;
627typedef signed char stbv_int8;
628typedef unsigned short stbv_uint16;
629typedef signed short stbv_int16;
630typedef unsigned int stbv_uint32;
631typedef signed int stbv_int32;
638typedef float stbv_codetype;
653#define STBV_FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
654#define STBV_FAST_HUFFMAN_TABLE_MASK (STBV_FAST_HUFFMAN_TABLE_SIZE - 1)
658 int dimensions, entries;
659 stbv_uint8 *codeword_lengths;
662 stbv_uint8 value_bits;
663 stbv_uint8 lookup_type;
664 stbv_uint8 sequence_p;
666 stbv_uint32 lookup_values;
667 stbv_codetype *multiplicands;
668 stbv_uint32 *codewords;
669 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
670 stbv_int16 fast_huffman[STBV_FAST_HUFFMAN_TABLE_SIZE];
672 stbv_int32 fast_huffman[STBV_FAST_HUFFMAN_TABLE_SIZE];
674 stbv_uint32 *sorted_codewords;
683 stbv_uint16 bark_map_size;
684 stbv_uint8 amplitude_bits;
685 stbv_uint8 amplitude_offset;
686 stbv_uint8 number_of_books;
687 stbv_uint8 book_list[16];
692 stbv_uint8 partitions;
693 stbv_uint8 partition_class_list[32];
694 stbv_uint8 class_dimensions[16];
695 stbv_uint8 class_subclasses[16];
696 stbv_uint8 class_masterbooks[16];
697 stbv_int16 subclass_books[16][8];
698 stbv_uint16 Xlist[31*8+2];
699 stbv_uint8 sorted_order[31*8+2];
700 stbv_uint8 stbv_neighbors[31*8+2][2];
701 stbv_uint8 floor1_multiplier;
702 stbv_uint8 rangebits;
714 stbv_uint32 begin, end;
715 stbv_uint32 part_size;
716 stbv_uint8 classifications;
717 stbv_uint8 classbook;
718 stbv_uint8 **classdata;
719 stbv_int16 (*residue_books)[8];
724 stbv_uint8 magnitude;
731 stbv_uint16 coupling_steps;
732 StbvMappingChannel *chan;
734 stbv_uint8 submap_floor[15];
735 stbv_uint8 submap_residue[15];
740 stbv_uint8 blockflag;
742 stbv_uint16 windowtype;
743 stbv_uint16 transformtype;
748 stbv_uint32 goal_crc;
750 stbv_uint32 crc_so_far;
752 stbv_uint32 sample_loc;
757 stbv_uint32 page_start, page_end;
758 stbv_uint32 last_decoded_sample;
764 unsigned int sample_rate;
767 unsigned int setup_memory_required;
768 unsigned int temp_memory_required;
769 unsigned int setup_temp_memory_required;
772#ifndef STB_VORBIS_NO_STDIO
779 stbv_uint8 *stream_start;
780 stbv_uint8 *stream_end;
782 stbv_uint32 stream_len;
784 stbv_uint8 push_mode;
786 stbv_uint32 first_audio_page_offset;
788 StbvProbedPage p_first, p_last;
803 int blocksize_0, blocksize_1;
805 StbvCodebook *codebooks;
807 stbv_uint16 floor_types[64];
808 StbvFloor *floor_config;
810 stbv_uint16 residue_types[64];
811 StbvResidue *residue_config;
813 StbvMapping *mapping;
815 StbvMode mode_config[64];
817 stbv_uint32 total_samples;
820 float *channel_buffers[STB_VORBIS_MAX_CHANNELS];
821 float *outputs [STB_VORBIS_MAX_CHANNELS];
823 float *previous_window[STB_VORBIS_MAX_CHANNELS];
826 #ifndef STB_VORBIS_NO_DEFER_FLOOR
827 stbv_int16 *finalY[STB_VORBIS_MAX_CHANNELS];
829 float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
832 stbv_uint32 current_loc;
833 int current_loc_valid;
838 float *A[2],*B[2],*C[2];
840 stbv_uint16 *stbv_bit_reverse[2];
846 stbv_uint8 segments[255];
847 stbv_uint8 page_flag;
848 stbv_uint8 bytes_in_seg;
849 stbv_uint8 first_decode;
856 int end_seg_with_known_loc;
857 stbv_uint32 known_loc_for_packet;
858 int discard_samples_deferred;
859 stbv_uint32 samples_output;
863#ifndef STB_VORBIS_NO_PUSHDATA_API
864 StbvCRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];
868 int channel_buffer_start;
869 int channel_buffer_end;
872#if defined(STB_VORBIS_NO_PUSHDATA_API)
873 #define STBV_IS_PUSH_MODE(f) FALSE
874#elif defined(STB_VORBIS_NO_PULLDATA_API)
875 #define STBV_IS_PUSH_MODE(f) TRUE
877 #define STBV_IS_PUSH_MODE(f) ((f)->push_mode)
897#define stbv_array_size_required(count,size) (count*(sizeof(void *)+(size)))
899#define stbv_temp_alloc(f,size) (f->alloc.alloc_buffer ? stbv_setup_temp_malloc(f,size) : alloca(size))
900#define stbv_temp_free(f,p) 0
901#define stbv_temp_alloc_save(f) ((f)->temp_offset)
902#define stbv_temp_alloc_restore(f,p) ((f)->temp_offset = (p))
904#define stbv_temp_block_array(f,count,size) stbv_make_block_array(stbv_temp_alloc(f,stbv_array_size_required(count,size)), count, size)
907static void *stbv_make_block_array(
void *mem,
int count,
int size)
910 void ** p = (
void **) mem;
911 char *q = (
char *) (p + count);
912 for (i=0; i < count; ++i) {
919static void *stbv_setup_malloc(stbv_vorb *f,
int sz)
922 f->setup_memory_required += sz;
923 if (f->alloc.alloc_buffer) {
924 void *p = (
char *) f->alloc.
alloc_buffer + f->setup_offset;
925 if (f->setup_offset + sz > f->temp_offset)
return NULL;
926 f->setup_offset += sz;
929 return sz ? malloc(sz) :
NULL;
932static void stbv_setup_free(stbv_vorb *f,
void *p)
934 if (f->alloc.alloc_buffer)
return;
938static void *stbv_setup_temp_malloc(stbv_vorb *f,
int sz)
941 if (f->alloc.alloc_buffer) {
942 if (f->temp_offset - sz < f->setup_offset)
return NULL;
943 f->temp_offset -= sz;
944 return (
char *) f->alloc.alloc_buffer + f->temp_offset;
949static void stbv_setup_temp_free(stbv_vorb *f,
void *p,
int sz)
951 if (f->alloc.alloc_buffer) {
952 f->temp_offset += (sz+3)&~3;
958#define STBV_CRC32_POLY 0x04c11db7
960static stbv_uint32 stbv_crc_table[256];
961static void stbv_crc32_init(
void)
965 for(i=0; i < 256; i++) {
966 for (s=(stbv_uint32) i << 24, j=0; j < 8; ++j)
967 s = (s << 1) ^ (s >= (1U<<31) ? STBV_CRC32_POLY : 0);
968 stbv_crc_table[i] = s;
972static __forceinline stbv_uint32 stbv_crc32_update(stbv_uint32 crc, stbv_uint8
byte)
974 return (crc << 8) ^ stbv_crc_table[
byte ^ (crc >> 24)];
979static unsigned int stbv_bit_reverse(
unsigned int n)
981 n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
982 n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
983 n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
984 n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
985 return (n >> 16) | (n << 16);
988static float stbv_square(
float x)
996static int stbv_ilog(stbv_int32 n)
998 static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
1000 if (n < 0)
return 0;
1004 if (n < (1 << 4))
return 0 + log2_4[n ];
1005 else if (n < (1 << 9))
return 5 + log2_4[n >> 5];
1006 else return 10 + log2_4[n >> 10];
1007 else if (n < (1 << 24))
1008 if (n < (1 << 19))
return 15 + log2_4[n >> 15];
1009 else return 20 + log2_4[n >> 20];
1010 else if (n < (1 << 29))
return 25 + log2_4[n >> 25];
1011 else return 30 + log2_4[n >> 30];
1015 #define M_PI 3.14159265358979323846264f
1026static float stbv_float32_unpack(stbv_uint32 x)
1029 stbv_uint32 mantissa = x & 0x1fffff;
1030 stbv_uint32 sign = x & 0x80000000;
1031 stbv_uint32 exp = (x & 0x7fe00000) >> 21;
1032 double res = sign ? -(double)mantissa : (
double)mantissa;
1033 return (
float) ldexp((
float)res, exp-788);
1044static void stbv_add_entry(StbvCodebook *c, stbv_uint32 huff_code,
int symbol,
int count,
int len, stbv_uint32 *values)
1047 c->codewords [symbol] = huff_code;
1049 c->codewords [count] = huff_code;
1050 c->codeword_lengths[count] = len;
1051 values [count] = symbol;
1055static int stbv_compute_codewords(StbvCodebook *c, stbv_uint8 *len,
int n, stbv_uint32 *values)
1058 stbv_uint32 available[32];
1060 memset(available, 0,
sizeof(available));
1062 for (k=0; k < n; ++k)
if (len[k] < NO_CODE)
break;
1063 if (k == n) { assert(c->sorted_entries == 0);
return TRUE; }
1065 stbv_add_entry(c, 0, k, m++, len[k], values);
1067 for (i=1; i <= len[k]; ++i)
1068 available[i] = 1U << (32-i);
1073 for (i=k+1; i < n; ++i) {
1076 if (z == NO_CODE)
continue;
1083 while (z > 0 && !available[z]) --z;
1084 if (z == 0) {
return FALSE; }
1086 assert(z >= 0 && z < 32);
1088 stbv_add_entry(c, stbv_bit_reverse(res), i, m++, len[i], values);
1091 assert(len[i] >= 0 && len[i] < 32);
1092 for (y=len[i]; y > z; --y) {
1093 assert(available[y] == 0);
1094 available[y] = res + (1 << (32-y));
1103static void stbv_compute_accelerated_huffman(StbvCodebook *c)
1106 for (i=0; i < STBV_FAST_HUFFMAN_TABLE_SIZE; ++i)
1107 c->fast_huffman[i] = -1;
1109 len = c->sparse ? c->sorted_entries : c->entries;
1110 #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
1111 if (len > 32767) len = 32767;
1113 for (i=0; i < len; ++i) {
1114 if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
1115 stbv_uint32 z = c->sparse ? stbv_bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
1117 while (z < STBV_FAST_HUFFMAN_TABLE_SIZE) {
1118 c->fast_huffman[z] = i;
1119 z += 1 << c->codeword_lengths[i];
1126#define STBV_CDECL __cdecl
1131static int STBV_CDECL stbv_uint32_compare(
const void *p,
const void *q)
1133 stbv_uint32 x = * (stbv_uint32 *) p;
1134 stbv_uint32 y = * (stbv_uint32 *) q;
1135 return x < y ? -1 : x > y;
1138static int stbv_include_in_sort(StbvCodebook *c, stbv_uint8 len)
1140 if (c->sparse) { assert(len != NO_CODE);
return TRUE; }
1141 if (len == NO_CODE)
return FALSE;
1142 if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH)
return TRUE;
1148static void stbv_compute_sorted_huffman(StbvCodebook *c, stbv_uint8 *lengths, stbv_uint32 *values)
1157 for (i=0; i < c->entries; ++i)
1158 if (stbv_include_in_sort(c, lengths[i]))
1159 c->sorted_codewords[k++] = stbv_bit_reverse(c->codewords[i]);
1160 assert(k == c->sorted_entries);
1162 for (i=0; i < c->sorted_entries; ++i)
1163 c->sorted_codewords[i] = stbv_bit_reverse(c->codewords[i]);
1166 qsort(c->sorted_codewords, c->sorted_entries,
sizeof(c->sorted_codewords[0]), stbv_uint32_compare);
1167 c->sorted_codewords[c->sorted_entries] = 0xffffffff;
1169 len = c->sparse ? c->sorted_entries : c->entries;
1175 for (i=0; i < len; ++i) {
1176 int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
1177 if (stbv_include_in_sort(c,huff_len)) {
1178 stbv_uint32 code = stbv_bit_reverse(c->codewords[i]);
1179 int x=0, n=c->sorted_entries;
1182 int m = x + (n >> 1);
1183 if (c->sorted_codewords[m] <= code) {
1190 assert(c->sorted_codewords[x] == code);
1192 c->sorted_values[x] = values[i];
1193 c->codeword_lengths[x] = huff_len;
1195 c->sorted_values[x] = i;
1202static int stbv_vorbis_validate(stbv_uint8 *data)
1204 static stbv_uint8 vorbis[6] = {
'v',
'o',
'r',
'b',
'i',
's' };
1205 return memcmp(data, vorbis, 6) == 0;
1210static int stbv_lookup1_values(
int entries,
int dim)
1212 int r = (int) floor(exp((
float) log((
float) entries) / dim));
1213 if ((
int) floor(pow((
float) r+1, dim)) <= entries)
1215 assert(pow((
float) r+1, dim) > entries);
1216 assert((
int) floor(pow((
float) r, dim)) <= entries);
1221static void stbv_compute_twiddle_factors(
int n,
float *A,
float *B,
float *C)
1223 int n4 = n >> 2, n8 = n >> 3;
1226 for (k=k2=0; k < n4; ++k,k2+=2) {
1227 A[k2 ] = (float) cos(4*k*M_PI/n);
1228 A[k2+1] = (float) -sin(4*k*M_PI/n);
1229 B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f;
1230 B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f;
1232 for (k=k2=0; k < n8; ++k,k2+=2) {
1233 C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);
1234 C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
1238static void stbv_compute_window(
int n,
float *window)
1241 for (i=0; i < n2; ++i)
1242 window[i] = (
float) sin(0.5 * M_PI * stbv_square((
float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
1245static void stbv_compute_bitreverse(
int n, stbv_uint16 *rev)
1247 int ld = stbv_ilog(n) - 1;
1249 for (i=0; i < n8; ++i)
1250 rev[i] = (stbv_bit_reverse(i) >> (32-ld+3)) << 2;
1253static int stbv_init_blocksize(stbv_vorb *f,
int b,
int n)
1255 int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
1256 f->A[b] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * n2);
1257 f->B[b] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * n2);
1258 f->C[b] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * n4);
1259 if (!f->A[b] || !f->B[b] || !f->C[b])
return stbv_error(f,
VORBIS_outofmem);
1260 stbv_compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
1261 f->window[b] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * n2);
1263 stbv_compute_window(n, f->window[b]);
1264 f->stbv_bit_reverse[b] = (stbv_uint16 *) stbv_setup_malloc(f,
sizeof(stbv_uint16) * n8);
1266 stbv_compute_bitreverse(n, f->stbv_bit_reverse[b]);
1270static void stbv_neighbors(stbv_uint16 *x,
int n,
int *plow,
int *phigh)
1275 for (i=0; i < n; ++i) {
1276 if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; }
1277 if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
1285} stbv_floor_ordering;
1287static int STBV_CDECL stbv_point_compare(
const void *p,
const void *q)
1289 stbv_floor_ordering *a = (stbv_floor_ordering *) p;
1290 stbv_floor_ordering *b = (stbv_floor_ordering *) q;
1291 return a->x < b->x ? -1 : a->x > b->x;
1298#if defined(STB_VORBIS_NO_STDIO)
1299 #define STBV_USE_MEMORY(z) TRUE
1301 #define STBV_USE_MEMORY(z) ((z)->stream)
1304static stbv_uint8 stbv_get8(stbv_vorb *z)
1306 if (STBV_USE_MEMORY(z)) {
1307 if (z->stream >= z->stream_end) { z->eof = TRUE;
return 0; }
1308 return *z->stream++;
1311 #ifndef STB_VORBIS_NO_STDIO
1313 int c = fgetc(z->f);
1314 if (c == EOF) { z->eof = TRUE;
return 0; }
1320static stbv_uint32 stbv_get32(stbv_vorb *f)
1324 x += stbv_get8(f) << 8;
1325 x += stbv_get8(f) << 16;
1326 x += (stbv_uint32) stbv_get8(f) << 24;
1330static int stbv_getn(stbv_vorb *z, stbv_uint8 *data,
int n)
1332 if (STBV_USE_MEMORY(z)) {
1333 if (z->stream+n > z->stream_end) { z->eof = 1;
return 0; }
1334 memcpy(data, z->stream, n);
1339 #ifndef STB_VORBIS_NO_STDIO
1340 if (fread(data, n, 1, z->f) == 1)
1349static void stbv_skip(stbv_vorb *z,
int n)
1351 if (STBV_USE_MEMORY(z)) {
1353 if (z->stream >= z->stream_end) z->eof = 1;
1356 #ifndef STB_VORBIS_NO_STDIO
1358 long x = ftell(z->f);
1359 fseek(z->f, x+n, SEEK_SET);
1364static int stbv_set_file_offset(
stb_vorbis *f,
unsigned int loc)
1366 #ifndef STB_VORBIS_NO_PUSHDATA_API
1367 if (f->push_mode)
return 0;
1370 if (STBV_USE_MEMORY(f)) {
1371 if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
1372 f->stream = f->stream_end;
1376 f->stream = f->stream_start + loc;
1380 #ifndef STB_VORBIS_NO_STDIO
1381 if (loc + f->f_start < loc || loc >= 0x80000000) {
1387 if (!fseek(f->f, loc, SEEK_SET))
1390 fseek(f->f, f->f_start, SEEK_END);
1396static stbv_uint8 stbv_ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };
1398static int stbv_capture_pattern(stbv_vorb *f)
1400 if (0x4f != stbv_get8(f))
return FALSE;
1401 if (0x67 != stbv_get8(f))
return FALSE;
1402 if (0x67 != stbv_get8(f))
return FALSE;
1403 if (0x53 != stbv_get8(f))
return FALSE;
1407#define STBV_PAGEFLAG_continued_packet 1
1408#define STBV_PAGEFLAG_first_page 2
1409#define STBV_PAGEFLAG_last_page 4
1411static int stbv_start_page_no_capturepattern(stbv_vorb *f)
1413 stbv_uint32 loc0,loc1,n;
1417 f->page_flag = stbv_get8(f);
1419 loc0 = stbv_get32(f);
1420 loc1 = stbv_get32(f);
1431 f->segment_count = stbv_get8(f);
1432 if (!stbv_getn(f, f->segments, f->segment_count))
1435 f->end_seg_with_known_loc = -2;
1436 if (loc0 != ~0U || loc1 != ~0U) {
1439 for (i=f->segment_count-1; i >= 0; --i)
1440 if (f->segments[i] < 255)
1444 f->end_seg_with_known_loc = i;
1445 f->known_loc_for_packet = loc0;
1448 if (f->first_decode) {
1452 for (i=0; i < f->segment_count; ++i)
1453 len += f->segments[i];
1454 len += 27 + f->segment_count;
1455 p.page_start = f->first_audio_page_offset;
1456 p.page_end = p.page_start + len;
1457 p.last_decoded_sample = loc0;
1464static int stbv_start_page(stbv_vorb *f)
1467 return stbv_start_page_no_capturepattern(f);
1470static int stbv_start_packet(stbv_vorb *f)
1472 while (f->next_seg == -1) {
1473 if (!stbv_start_page(f))
return FALSE;
1474 if (f->page_flag & STBV_PAGEFLAG_continued_packet)
1477 f->last_seg = FALSE;
1479 f->packet_bytes = 0;
1480 f->bytes_in_seg = 0;
1485static int stbv_maybe_start_packet(stbv_vorb *f)
1487 if (f->next_seg == -1) {
1488 int x = stbv_get8(f);
1489 if (f->eof)
return FALSE;
1494 if (!stbv_start_page_no_capturepattern(f))
return FALSE;
1495 if (f->page_flag & STBV_PAGEFLAG_continued_packet) {
1498 f->last_seg = FALSE;
1499 f->bytes_in_seg = 0;
1503 return stbv_start_packet(f);
1506static int stbv_next_segment(stbv_vorb *f)
1509 if (f->last_seg)
return 0;
1510 if (f->next_seg == -1) {
1511 f->last_seg_which = f->segment_count-1;
1512 if (!stbv_start_page(f)) { f->last_seg = 1;
return 0; }
1515 len = f->segments[f->next_seg++];
1518 f->last_seg_which = f->next_seg-1;
1520 if (f->next_seg >= f->segment_count)
1522 assert(f->bytes_in_seg == 0);
1523 f->bytes_in_seg = len;
1527#define STBV_EOP (-1)
1528#define STBV_INVALID_BITS (-1)
1530static int stbv_get8_packet_raw(stbv_vorb *f)
1532 if (!f->bytes_in_seg) {
1533 if (f->last_seg)
return STBV_EOP;
1534 else if (!stbv_next_segment(f))
return STBV_EOP;
1536 assert(f->bytes_in_seg > 0);
1539 return stbv_get8(f);
1542static int stbv_get8_packet(stbv_vorb *f)
1544 int x = stbv_get8_packet_raw(f);
1549static void stbv_flush_packet(stbv_vorb *f)
1551 while (stbv_get8_packet_raw(f) != STBV_EOP);
1556static stbv_uint32 stbv_get_bits(stbv_vorb *f,
int n)
1560 if (f->valid_bits < 0)
return 0;
1561 if (f->valid_bits < n) {
1564 z = stbv_get_bits(f, 24);
1565 z += stbv_get_bits(f, n-24) << 24;
1568 if (f->valid_bits == 0) f->acc = 0;
1569 while (f->valid_bits < n) {
1570 int z = stbv_get8_packet_raw(f);
1571 if (z == STBV_EOP) {
1572 f->valid_bits = STBV_INVALID_BITS;
1575 f->acc += z << f->valid_bits;
1579 if (f->valid_bits < 0)
return 0;
1580 z = f->acc & ((1 << n)-1);
1590static __forceinline
void stbv_prep_huffman(stbv_vorb *f)
1592 if (f->valid_bits <= 24) {
1593 if (f->valid_bits == 0) f->acc = 0;
1596 if (f->last_seg && !f->bytes_in_seg)
return;
1597 z = stbv_get8_packet_raw(f);
1598 if (z == STBV_EOP)
return;
1599 f->acc += (unsigned) z << f->valid_bits;
1601 }
while (f->valid_bits <= 24);
1607 STBV_VORBIS_packet_id = 1,
1608 STBV_VORBIS_packet_comment = 3,
1609 STBV_VORBIS_packet_setup = 5
1612static int stbv_codebook_decode_scalar_raw(stbv_vorb *f, StbvCodebook *c)
1615 stbv_prep_huffman(f);
1617 if (c->codewords ==
NULL && c->sorted_codewords ==
NULL)
1622 if (c->entries > 8 ? c->sorted_codewords!=
NULL : !c->codewords) {
1624 stbv_uint32 code = stbv_bit_reverse(f->acc);
1625 int x=0, n=c->sorted_entries, len;
1629 int m = x + (n >> 1);
1630 if (c->sorted_codewords[m] <= code) {
1638 if (!c->sparse) x = c->sorted_values[x];
1640 len = c->codeword_lengths[x];
1641 if (f->valid_bits >= len) {
1643 f->valid_bits -= len;
1653 for (i=0; i < c->entries; ++i) {
1654 if (c->codeword_lengths[i] == NO_CODE)
continue;
1655 if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) {
1656 if (f->valid_bits >= c->codeword_lengths[i]) {
1657 f->acc >>= c->codeword_lengths[i];
1658 f->valid_bits -= c->codeword_lengths[i];
1671#ifndef STB_VORBIS_NO_INLINE_DECODE
1673#define STBV_DECODE_RAW(var, f,c) \
1674 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \
1675 stbv_prep_huffman(f); \
1676 var = f->acc & STBV_FAST_HUFFMAN_TABLE_MASK; \
1677 var = c->fast_huffman[var]; \
1679 int n = c->codeword_lengths[var]; \
1681 f->valid_bits -= n; \
1682 if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
1684 var = stbv_codebook_decode_scalar_raw(f,c); \
1689static int stbv_codebook_decode_scalar(stbv_vorb *f, StbvCodebook *c)
1692 if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
1693 stbv_prep_huffman(f);
1695 i = f->acc & STBV_FAST_HUFFMAN_TABLE_MASK;
1696 i = c->fast_huffman[i];
1698 f->acc >>= c->codeword_lengths[i];
1699 f->valid_bits -= c->codeword_lengths[i];
1700 if (f->valid_bits < 0) { f->valid_bits = 0;
return -1; }
1703 return stbv_codebook_decode_scalar_raw(f,c);
1706#define STBV_DECODE_RAW(var,f,c) var = stbv_codebook_decode_scalar(f,c);
1710#define STBV_DECODE(var,f,c) \
1711 STBV_DECODE_RAW(var,f,c) \
1712 if (c->sparse) var = c->sorted_values[var];
1714#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1715 #define DECODE_VQ(var,f,c) STBV_DECODE_RAW(var,f,c)
1717 #define DECODE_VQ(var,f,c) STBV_DECODE(var,f,c)
1727#define STBV_CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
1728#define STBV_CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
1729#define STBV_CODEBOOK_ELEMENT_BASE(c) (0)
1731static int stbv_codebook_decode_start(stbv_vorb *f, StbvCodebook *c)
1736 if (c->lookup_type == 0)
1740 if (c->sparse) assert(z < c->sorted_entries);
1742 if (!f->bytes_in_seg)
1751static int stbv_codebook_decode(stbv_vorb *f, StbvCodebook *c,
float *output,
int len)
1753 int i,z = stbv_codebook_decode_start(f,c);
1754 if (z < 0)
return FALSE;
1755 if (len > c->dimensions) len = c->dimensions;
1757#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1758 if (c->lookup_type == 1) {
1759 float last = STBV_CODEBOOK_ELEMENT_BASE(c);
1761 for (i=0; i < len; ++i) {
1762 int off = (z / div) % c->lookup_values;
1763 float val = STBV_CODEBOOK_ELEMENT_FAST(c,off) + last;
1765 if (c->sequence_p) last = val + c->minimum_value;
1766 div *= c->lookup_values;
1773 if (c->sequence_p) {
1774 float last = STBV_CODEBOOK_ELEMENT_BASE(c);
1775 for (i=0; i < len; ++i) {
1776 float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1778 last = val + c->minimum_value;
1781 float last = STBV_CODEBOOK_ELEMENT_BASE(c);
1782 for (i=0; i < len; ++i) {
1783 output[i] += STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1790static int stbv_codebook_decode_step(stbv_vorb *f, StbvCodebook *c,
float *output,
int len,
int step)
1792 int i,z = stbv_codebook_decode_start(f,c);
1793 float last = STBV_CODEBOOK_ELEMENT_BASE(c);
1794 if (z < 0)
return FALSE;
1795 if (len > c->dimensions) len = c->dimensions;
1797#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1798 if (c->lookup_type == 1) {
1800 for (i=0; i < len; ++i) {
1801 int off = (z / div) % c->lookup_values;
1802 float val = STBV_CODEBOOK_ELEMENT_FAST(c,off) + last;
1803 output[i*step] += val;
1804 if (c->sequence_p) last = val;
1805 div *= c->lookup_values;
1812 for (i=0; i < len; ++i) {
1813 float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1814 output[i*step] += val;
1815 if (c->sequence_p) last = val;
1821static int stbv_codebook_decode_deinterleave_repeat(stbv_vorb *f, StbvCodebook *c,
float **outputs,
int ch,
int *c_inter_p,
int *p_inter_p,
int len,
int total_decode)
1823 int c_inter = *c_inter_p;
1824 int p_inter = *p_inter_p;
1825 int i,z, effective = c->dimensions;
1830 while (total_decode > 0) {
1831 float last = STBV_CODEBOOK_ELEMENT_BASE(c);
1833 #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1834 assert(!c->sparse || z < c->sorted_entries);
1837 if (!f->bytes_in_seg)
1838 if (f->last_seg)
return FALSE;
1846 if (c_inter + p_inter*ch + effective > len * ch) {
1847 effective = len*ch - (p_inter*ch - c_inter);
1850 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1851 if (c->lookup_type == 1) {
1853 for (i=0; i < effective; ++i) {
1854 int off = (z / div) % c->lookup_values;
1855 float val = STBV_CODEBOOK_ELEMENT_FAST(c,off) + last;
1856 if (outputs[c_inter])
1857 outputs[c_inter][p_inter] += val;
1858 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1859 if (c->sequence_p) last = val;
1860 div *= c->lookup_values;
1866 if (c->sequence_p) {
1867 for (i=0; i < effective; ++i) {
1868 float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1869 if (outputs[c_inter])
1870 outputs[c_inter][p_inter] += val;
1871 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1875 for (i=0; i < effective; ++i) {
1876 float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1877 if (outputs[c_inter])
1878 outputs[c_inter][p_inter] += val;
1879 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1884 total_decode -= effective;
1886 *c_inter_p = c_inter;
1887 *p_inter_p = p_inter;
1891static int stbv_predict_point(
int x,
int x0,
int x1,
int y0,
int y1)
1896 int err = abs(dy) * (x - x0);
1897 int off = err / adx;
1898 return dy < 0 ? y0 - off : y0 + off;
1902static float stbv_inverse_db_table[256] =
1904 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
1905 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
1906 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
1907 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
1908 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
1909 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
1910 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
1911 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
1912 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
1913 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
1914 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
1915 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
1916 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
1917 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
1918 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
1919 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
1920 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
1921 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
1922 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
1923 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
1924 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
1925 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
1926 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
1927 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
1928 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
1929 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
1930 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
1931 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
1932 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
1933 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
1934 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
1935 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
1936 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
1937 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
1938 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
1939 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
1940 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f,
1941 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f,
1942 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f,
1943 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f,
1944 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f,
1945 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f,
1946 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f,
1947 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f,
1948 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f,
1949 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f,
1950 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f,
1951 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f,
1952 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f,
1953 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f,
1954 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f,
1955 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f,
1956 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f,
1957 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f,
1958 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f,
1959 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f,
1960 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f,
1961 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f,
1962 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f,
1963 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f,
1964 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f,
1965 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f,
1966 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f,
1967 0.82788260f, 0.88168307f, 0.9389798f, 1.0f
1978#ifndef STB_VORBIS_NO_DEFER_FLOOR
1979#define STBV_LINE_OP(a,b) a *= b
1981#define STBV_LINE_OP(a,b) a = b
1984#ifdef STB_VORBIS_DIVIDE_TABLE
1985#define STBV_DIVTAB_NUMER 32
1986#define STBV_DIVTAB_DENOM 64
1987stbv_int8 stbv_integer_divide_table[STBV_DIVTAB_NUMER][STBV_DIVTAB_DENOM];
1990static __forceinline
void stbv_draw_line(
float *output,
int x0,
int y0,
int x1,
int y1,
int n)
2000#ifdef STB_VORBIS_DIVIDE_TABLE
2001 if (adx < STBV_DIVTAB_DENOM && ady < STBV_DIVTAB_NUMER) {
2003 base = -stbv_integer_divide_table[ady][adx];
2006 base = stbv_integer_divide_table[ady][adx];
2023 ady -= abs(base) * adx;
2026 STBV_LINE_OP(output[x], stbv_inverse_db_table[y]);
2027 for (++x; x < x1; ++x) {
2034 STBV_LINE_OP(output[x], stbv_inverse_db_table[y]);
2039static int stbv_residue_decode(stbv_vorb *f, StbvCodebook *book,
float *target,
int offset,
int n,
int rtype)
2043 int step = n / book->dimensions;
2044 for (k=0; k < step; ++k)
2045 if (!stbv_codebook_decode_step(f, book, target+offset+k, n-offset-k, step))
2048 for (k=0; k < n; ) {
2049 if (!stbv_codebook_decode(f, book, target+offset, n-k))
2051 k += book->dimensions;
2052 offset += book->dimensions;
2060static void stbv_decode_residue(stbv_vorb *f,
float *residue_buffers[],
int ch,
int n,
int rn, stbv_uint8 *do_not_decode)
2063 StbvResidue *r = f->residue_config + rn;
2064 int rtype = f->residue_types[rn];
2065 int c = r->classbook;
2066 int classwords = f->codebooks[c].dimensions;
2067 unsigned int actual_size = rtype == 2 ? n*2 : n;
2068 unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size);
2069 unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size);
2070 int n_read = limit_r_end - limit_r_begin;
2071 int part_read = n_read / r->part_size;
2072 int temp_alloc_point = stbv_temp_alloc_save(f);
2073 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2074 stbv_uint8 ***part_classdata = (stbv_uint8 ***) stbv_temp_block_array(f,f->channels, part_read *
sizeof(**part_classdata));
2076 int **classifications = (
int **) stbv_temp_block_array(f,f->channels, part_read *
sizeof(**classifications));
2081 for (i=0; i < ch; ++i)
2082 if (!do_not_decode[i])
2083 memset(residue_buffers[i], 0,
sizeof(
float) * n);
2085 if (rtype == 2 && ch != 1) {
2086 for (j=0; j < ch; ++j)
2087 if (!do_not_decode[j])
2092 for (pass=0; pass < 8; ++pass) {
2093 int pcount = 0, class_set = 0;
2095 while (pcount < part_read) {
2096 int z = r->begin + pcount*r->part_size;
2097 int c_inter = (z & 1), p_inter = z>>1;
2099 StbvCodebook *c = f->codebooks+r->classbook;
2102 if (q == STBV_EOP)
goto done;
2103 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2104 part_classdata[0][class_set] = r->classdata[q];
2106 for (i=classwords-1; i >= 0; --i) {
2107 classifications[0][i+pcount] = q % r->classifications;
2108 q /= r->classifications;
2112 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2113 int z = r->begin + pcount*r->part_size;
2114 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2115 int c = part_classdata[0][class_set][i];
2117 int c = classifications[0][pcount];
2119 int b = r->residue_books[c][pass];
2121 StbvCodebook *book = f->codebooks + b;
2122 #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
2123 if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2127 if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2136 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2140 }
else if (ch == 1) {
2141 while (pcount < part_read) {
2142 int z = r->begin + pcount*r->part_size;
2143 int c_inter = 0, p_inter = z;
2145 StbvCodebook *c = f->codebooks+r->classbook;
2148 if (q == STBV_EOP)
goto done;
2149 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2150 part_classdata[0][class_set] = r->classdata[q];
2152 for (i=classwords-1; i >= 0; --i) {
2153 classifications[0][i+pcount] = q % r->classifications;
2154 q /= r->classifications;
2158 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2159 int z = r->begin + pcount*r->part_size;
2160 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2161 int c = part_classdata[0][class_set][i];
2163 int c = classifications[0][pcount];
2165 int b = r->residue_books[c][pass];
2167 StbvCodebook *book = f->codebooks + b;
2168 if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2176 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2181 while (pcount < part_read) {
2182 int z = r->begin + pcount*r->part_size;
2183 int c_inter = z % ch, p_inter = z/ch;
2185 StbvCodebook *c = f->codebooks+r->classbook;
2188 if (q == STBV_EOP)
goto done;
2189 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2190 part_classdata[0][class_set] = r->classdata[q];
2192 for (i=classwords-1; i >= 0; --i) {
2193 classifications[0][i+pcount] = q % r->classifications;
2194 q /= r->classifications;
2198 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2199 int z = r->begin + pcount*r->part_size;
2200 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2201 int c = part_classdata[0][class_set][i];
2203 int c = classifications[0][pcount];
2205 int b = r->residue_books[c][pass];
2207 StbvCodebook *book = f->codebooks + b;
2208 if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
2216 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2226 for (pass=0; pass < 8; ++pass) {
2227 int pcount = 0, class_set=0;
2228 while (pcount < part_read) {
2230 for (j=0; j < ch; ++j) {
2231 if (!do_not_decode[j]) {
2232 StbvCodebook *c = f->codebooks+r->classbook;
2234 STBV_DECODE(temp,f,c);
2235 if (temp == STBV_EOP)
goto done;
2236 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2237 part_classdata[j][class_set] = r->classdata[temp];
2239 for (i=classwords-1; i >= 0; --i) {
2240 classifications[j][i+pcount] = temp % r->classifications;
2241 temp /= r->classifications;
2247 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
2248 for (j=0; j < ch; ++j) {
2249 if (!do_not_decode[j]) {
2250 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2251 int c = part_classdata[j][class_set][i];
2253 int c = classifications[j][pcount];
2255 int b = r->residue_books[c][pass];
2257 float *target = residue_buffers[j];
2258 int offset = r->begin + pcount * r->part_size;
2259 int n = r->part_size;
2260 StbvCodebook *book = f->codebooks + b;
2261 if (!stbv_residue_decode(f, book, target, offset, n, rtype))
2267 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2274 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
2275 stbv_temp_free(f,part_classdata);
2277 stbv_temp_free(f,classifications);
2279 stbv_temp_alloc_restore(f,temp_alloc_point);
2285void inverse_mdct_slow(
float *buffer,
int n)
2289 float *x = (
float *) malloc(
sizeof(*x) * n2);
2290 memcpy(x, buffer,
sizeof(*x) * n2);
2291 for (i=0; i < n; ++i) {
2293 for (j=0; j < n2; ++j)
2301 acc += x[j] * (
float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
2308void inverse_mdct_slow(
float *buffer,
int n, stbv_vorb *f,
int blocktype)
2312 int n2 = n >> 1, nmask = (n << 2) -1;
2313 float *x = (
float *) malloc(
sizeof(*x) * n2);
2314 memcpy(x, buffer,
sizeof(*x) * n2);
2315 for (i=0; i < 4*n; ++i)
2316 mcos[i] = (
float) cos(M_PI / 2 * i / n);
2318 for (i=0; i < n; ++i) {
2320 for (j=0; j < n2; ++j)
2321 acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask];
2329void dct_iv_slow(
float *buffer,
int n)
2334 int n2 = n >> 1, nmask = (n << 3) - 1;
2335 memcpy(x, buffer,
sizeof(*x) * n);
2336 for (i=0; i < 8*n; ++i)
2337 mcos[i] = (
float) cos(M_PI / 4 * i / n);
2338 for (i=0; i < n; ++i) {
2340 for (j=0; j < n; ++j)
2341 acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask];
2346void inverse_mdct_slow(
float *buffer,
int n, stbv_vorb *f,
int blocktype)
2348 int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
2351 memcpy(temp, buffer, n2 *
sizeof(
float));
2352 dct_iv_slow(temp, n2);
2354 for (i=0; i < n4 ; ++i) buffer[i] = temp[i+n4];
2355 for ( ; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1];
2356 for ( ; i < n ; ++i) buffer[i] = -temp[i - n3_4];
2360#ifndef LIBVORBIS_MDCT
2361#define LIBVORBIS_MDCT 0
2378extern void mdct_init(mdct_lookup *lookup,
int n);
2379extern void mdct_clear(mdct_lookup *l);
2380extern void mdct_backward(mdct_lookup *init,
float *in,
float *out);
2384void stbv_inverse_mdct(
float *buffer,
int n, stbv_vorb *f,
int blocktype)
2387 if (M1.n == n) M = &M1;
2388 else if (M2.n == n) M = &M2;
2389 else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; }
2391 if (M2.n) __asm
int 3;
2396 mdct_backward(M, buffer, buffer);
2404static void stbv_imdct_step3_iter0_loop(
int n,
float *e,
int i_off,
int k_off,
float *A)
2406 float *ee0 = e + i_off;
2407 float *ee2 = ee0 + k_off;
2410 assert((n & 3) == 0);
2411 for (i=(n>>2); i > 0; --i) {
2412 float k00_20, k01_21;
2413 k00_20 = ee0[ 0] - ee2[ 0];
2414 k01_21 = ee0[-1] - ee2[-1];
2417 ee2[ 0] = k00_20 * A[0] - k01_21 * A[1];
2418 ee2[-1] = k01_21 * A[0] + k00_20 * A[1];
2421 k00_20 = ee0[-2] - ee2[-2];
2422 k01_21 = ee0[-3] - ee2[-3];
2425 ee2[-2] = k00_20 * A[0] - k01_21 * A[1];
2426 ee2[-3] = k01_21 * A[0] + k00_20 * A[1];
2429 k00_20 = ee0[-4] - ee2[-4];
2430 k01_21 = ee0[-5] - ee2[-5];
2433 ee2[-4] = k00_20 * A[0] - k01_21 * A[1];
2434 ee2[-5] = k01_21 * A[0] + k00_20 * A[1];
2437 k00_20 = ee0[-6] - ee2[-6];
2438 k01_21 = ee0[-7] - ee2[-7];
2441 ee2[-6] = k00_20 * A[0] - k01_21 * A[1];
2442 ee2[-7] = k01_21 * A[0] + k00_20 * A[1];
2449static void stbv_imdct_step3_inner_r_loop(
int lim,
float *e,
int d0,
int k_off,
float *A,
int k1)
2452 float k00_20, k01_21;
2455 float *e2 = e0 + k_off;
2457 for (i=lim >> 2; i > 0; --i) {
2458 k00_20 = e0[-0] - e2[-0];
2459 k01_21 = e0[-1] - e2[-1];
2462 e2[-0] = (k00_20)*A[0] - (k01_21) * A[1];
2463 e2[-1] = (k01_21)*A[0] + (k00_20) * A[1];
2467 k00_20 = e0[-2] - e2[-2];
2468 k01_21 = e0[-3] - e2[-3];
2471 e2[-2] = (k00_20)*A[0] - (k01_21) * A[1];
2472 e2[-3] = (k01_21)*A[0] + (k00_20) * A[1];
2476 k00_20 = e0[-4] - e2[-4];
2477 k01_21 = e0[-5] - e2[-5];
2480 e2[-4] = (k00_20)*A[0] - (k01_21) * A[1];
2481 e2[-5] = (k01_21)*A[0] + (k00_20) * A[1];
2485 k00_20 = e0[-6] - e2[-6];
2486 k01_21 = e0[-7] - e2[-7];
2489 e2[-6] = (k00_20)*A[0] - (k01_21) * A[1];
2490 e2[-7] = (k01_21)*A[0] + (k00_20) * A[1];
2499static void stbv_imdct_step3_inner_s_loop(
int n,
float *e,
int i_off,
int k_off,
float *A,
int a_off,
int k0)
2504 float A2 = A[0+a_off];
2505 float A3 = A[0+a_off+1];
2506 float A4 = A[0+a_off*2+0];
2507 float A5 = A[0+a_off*2+1];
2508 float A6 = A[0+a_off*3+0];
2509 float A7 = A[0+a_off*3+1];
2513 float *ee0 = e +i_off;
2514 float *ee2 = ee0+k_off;
2516 for (i=n; i > 0; --i) {
2517 k00 = ee0[ 0] - ee2[ 0];
2518 k11 = ee0[-1] - ee2[-1];
2519 ee0[ 0] = ee0[ 0] + ee2[ 0];
2520 ee0[-1] = ee0[-1] + ee2[-1];
2521 ee2[ 0] = (k00) * A0 - (k11) * A1;
2522 ee2[-1] = (k11) * A0 + (k00) * A1;
2524 k00 = ee0[-2] - ee2[-2];
2525 k11 = ee0[-3] - ee2[-3];
2526 ee0[-2] = ee0[-2] + ee2[-2];
2527 ee0[-3] = ee0[-3] + ee2[-3];
2528 ee2[-2] = (k00) * A2 - (k11) * A3;
2529 ee2[-3] = (k11) * A2 + (k00) * A3;
2531 k00 = ee0[-4] - ee2[-4];
2532 k11 = ee0[-5] - ee2[-5];
2533 ee0[-4] = ee0[-4] + ee2[-4];
2534 ee0[-5] = ee0[-5] + ee2[-5];
2535 ee2[-4] = (k00) * A4 - (k11) * A5;
2536 ee2[-5] = (k11) * A4 + (k00) * A5;
2538 k00 = ee0[-6] - ee2[-6];
2539 k11 = ee0[-7] - ee2[-7];
2540 ee0[-6] = ee0[-6] + ee2[-6];
2541 ee0[-7] = ee0[-7] + ee2[-7];
2542 ee2[-6] = (k00) * A6 - (k11) * A7;
2543 ee2[-7] = (k11) * A6 + (k00) * A7;
2550static __forceinline
void stbv_iter_54(
float *z)
2552 float k00,k11,k22,k33;
2555 k00 = z[ 0] - z[-4];
2558 k22 = z[-2] - z[-6];
2565 k33 = z[-3] - z[-7];
2572 k11 = z[-1] - z[-5];
2582static void stbv_imdct_step3_inner_s_loop_ld654(
int n,
float *e,
int i_off,
float *A,
int base_n)
2584 int a_off = base_n >> 3;
2585 float A2 = A[0+a_off];
2586 float *z = e + i_off;
2587 float *base = z - 16 * n;
2592 k00 = z[-0] - z[-8];
2593 k11 = z[-1] - z[-9];
2594 z[-0] = z[-0] + z[-8];
2595 z[-1] = z[-1] + z[-9];
2599 k00 = z[ -2] - z[-10];
2600 k11 = z[ -3] - z[-11];
2601 z[ -2] = z[ -2] + z[-10];
2602 z[ -3] = z[ -3] + z[-11];
2603 z[-10] = (k00+k11) * A2;
2604 z[-11] = (k11-k00) * A2;
2606 k00 = z[-12] - z[ -4];
2607 k11 = z[ -5] - z[-13];
2608 z[ -4] = z[ -4] + z[-12];
2609 z[ -5] = z[ -5] + z[-13];
2613 k00 = z[-14] - z[ -6];
2614 k11 = z[ -7] - z[-15];
2615 z[ -6] = z[ -6] + z[-14];
2616 z[ -7] = z[ -7] + z[-15];
2617 z[-14] = (k00+k11) * A2;
2618 z[-15] = (k00-k11) * A2;
2626static void stbv_inverse_mdct(
float *buffer,
int n, stbv_vorb *f,
int blocktype)
2628 int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
2631 int save_point = stbv_temp_alloc_save(f);
2632 float *buf2 = (
float *) stbv_temp_alloc(f, n2 *
sizeof(*buf2));
2635 float *A = f->A[blocktype];
2657 float *d,*e, *AA, *e_stop;
2661 e_stop = &buffer[n2];
2662 while (e != e_stop) {
2663 d[1] = (e[0] * AA[0] - e[2]*AA[1]);
2664 d[0] = (e[0] * AA[1] + e[2]*AA[0]);
2672 d[1] = (-e[2] * AA[0] - -e[0]*AA[1]);
2673 d[0] = (-e[2] * AA[1] + -e[0]*AA[0]);
2691 float *AA = &A[n2-8];
2692 float *d0,*d1, *e0, *e1;
2701 float v40_20, v41_21;
2703 v41_21 = e0[1] - e1[1];
2704 v40_20 = e0[0] - e1[0];
2705 d0[1] = e0[1] + e1[1];
2706 d0[0] = e0[0] + e1[0];
2707 d1[1] = v41_21*AA[4] - v40_20*AA[5];
2708 d1[0] = v40_20*AA[4] + v41_21*AA[5];
2710 v41_21 = e0[3] - e1[3];
2711 v40_20 = e0[2] - e1[2];
2712 d0[3] = e0[3] + e1[3];
2713 d0[2] = e0[2] + e1[2];
2714 d1[3] = v41_21*AA[0] - v40_20*AA[1];
2715 d1[2] = v40_20*AA[0] + v41_21*AA[1];
2727 ld = stbv_ilog(n) - 1;
2737 stbv_imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*0, -(n >> 3), A);
2738 stbv_imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*1, -(n >> 3), A);
2741 stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*0, -(n >> 4), A, 16);
2742 stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*1, -(n >> 4), A, 16);
2743 stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*2, -(n >> 4), A, 16);
2744 stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*3, -(n >> 4), A, 16);
2747 for (; l < (ld-3)>>1; ++l) {
2748 int k0 = n >> (l+2), k0_2 = k0>>1;
2749 int lim = 1 << (l+1);
2751 for (i=0; i < lim; ++i)
2752 stbv_imdct_step3_inner_r_loop(n >> (l+4), u, n2-1 - k0*i, -k0_2, A, 1 << (l+3));
2755 for (; l < ld-6; ++l) {
2756 int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1;
2757 int rlim = n >> (l+6), r;
2758 int lim = 1 << (l+1);
2762 for (r=rlim; r > 0; --r) {
2763 stbv_imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0);
2774 stbv_imdct_step3_inner_s_loop_ld654(n >> 5, u, n2-1, A, n);
2781 stbv_uint16 *bitrev = f->stbv_bit_reverse[blocktype];
2787 float *d0 = &v[n4-4];
2788 float *d1 = &v[n2-4];
2818 float *C = f->C[blocktype];
2825 float a02,a11,b0,b1,b2,b3;
2830 b0 = C[1]*a02 + C[0]*a11;
2831 b1 = C[1]*a11 - C[0]*a02;
2844 b0 = C[3]*a02 + C[2]*a11;
2845 b1 = C[3]*a11 - C[2]*a02;
2872 float *d0,*d1,*d2,*d3;
2874 float *B = f->B[blocktype] + n2 - 8;
2875 float *e = buf2 + n2 - 8;
2883 p3 = e[6]*B[7] - e[7]*B[6];
2884 p2 = -e[6]*B[6] - e[7]*B[7];
2891 p1 = e[4]*B[5] - e[5]*B[4];
2892 p0 = -e[4]*B[4] - e[5]*B[5];
2899 p3 = e[2]*B[3] - e[3]*B[2];
2900 p2 = -e[2]*B[2] - e[3]*B[3];
2907 p1 = e[0]*B[1] - e[1]*B[0];
2908 p0 = -e[0]*B[0] - e[1]*B[1];
2924 stbv_temp_free(f,buf2);
2925 stbv_temp_alloc_restore(f,save_point);
2930void inverse_mdct_naive(
float *buffer,
int n)
2933 float A[1 << 12], B[1 << 12], C[1 << 11];
2934 int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
2935 int n3_4 = n - n4, ld;
2938 float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13];
2941 for (k=k2=0; k < n4; ++k,k2+=2) {
2942 A[k2 ] = (float) cos(4*k*M_PI/n);
2943 A[k2+1] = (float) -sin(4*k*M_PI/n);
2944 B[k2 ] = (float) cos((k2+1)*M_PI/n/2);
2945 B[k2+1] = (float) sin((k2+1)*M_PI/n/2);
2947 for (k=k2=0; k < n8; ++k,k2+=2) {
2948 C[k2 ] = (float) cos(2*(k2+1)*M_PI/n);
2949 C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
2960 for (k=0; k < n2; ++k) u[k] = buffer[k];
2961 for ( ; k < n ; ++k) u[k] = -buffer[n - k - 1];
2964 for (k=k2=k4=0; k < n4; k+=1, k2+=2, k4+=4) {
2965 v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2] - (u[k4+2] - u[n-k4-3])*A[k2+1];
2966 v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3])*A[k2];
2969 for (k=k4=0; k < n8; k+=1, k4+=4) {
2970 w[n2+3+k4] = v[n2+3+k4] + v[k4+3];
2971 w[n2+1+k4] = v[n2+1+k4] + v[k4+1];
2972 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];
2973 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];
2976 ld = stbv_ilog(n) - 1;
2977 for (l=0; l < ld-3; ++l) {
2978 int k0 = n >> (l+2), k1 = 1 << (l+3);
2979 int rlim = n >> (l+4), r4, r;
2980 int s2lim = 1 << (l+2), s2;
2981 for (r=r4=0; r < rlim; r4+=4,++r) {
2982 for (s2=0; s2 < s2lim; s2+=2) {
2983 u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4];
2984 u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4];
2985 u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1]
2986 - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1];
2987 u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1]
2988 + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1];
2993 memcpy(w, u,
sizeof(u));
2998 for (i=0; i < n8; ++i) {
2999 int j = stbv_bit_reverse(i) >> (32-ld+3);
3010 int i8 = i << 3, j8 = j << 3;
3011 v[j8+1] = u[i8+1], v[i8+1] = u[j8 + 1];
3012 v[j8+3] = u[i8+3], v[i8+3] = u[j8 + 3];
3013 v[j8+5] = u[i8+5], v[i8+5] = u[j8 + 5];
3014 v[j8+7] = u[i8+7], v[i8+7] = u[j8 + 7];
3018 for (k=0; k < n2; ++k) {
3022 for (k=k2=k4=0; k < n8; ++k, k2 += 2, k4 += 4) {
3024 u[n-2-k2] = w[k4+1];
3025 u[n3_4 - 1 - k2] = w[k4+2];
3026 u[n3_4 - 2 - k2] = w[k4+3];
3029 for (k=k2=0; k < n8; ++k, k2 += 2) {
3030 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;
3031 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;
3032 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;
3033 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;
3036 for (k=k2=0; k < n4; ++k,k2 += 2) {
3037 X[k] = v[k2+n2]*B[k2 ] + v[k2+1+n2]*B[k2+1];
3038 X[n2-1-k] = v[k2+n2]*B[k2+1] - v[k2+1+n2]*B[k2 ];
3050 for (i=0; i < n4 ; ++i) buffer[i] = s * X[i+n4];
3051 for ( ; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1];
3052 for ( ; i < n ; ++i) buffer[i] = -s * X[i - n3_4];
3056static float *stbv_get_window(stbv_vorb *f,
int len)
3059 if (len == f->blocksize_0)
return f->window[0];
3060 if (len == f->blocksize_1)
return f->window[1];
3065#ifndef STB_VORBIS_NO_DEFER_FLOOR
3066typedef stbv_int16 STBV_YTYPE;
3068typedef int STBV_YTYPE;
3070static int stbv_do_floor(stbv_vorb *f, StbvMapping *map,
int i,
int n,
float *target, STBV_YTYPE *finalY, stbv_uint8 *step2_flag)
3073 int s = map->chan[i].mux, floor;
3074 floor = map->submap_floor[s];
3075 if (f->floor_types[floor] == 0) {
3078 StbvFloor1 *g = &f->floor_config[floor].floor1;
3080 int lx = 0, ly = finalY[0] * g->floor1_multiplier;
3081 for (q=1; q < g->values; ++q) {
3082 j = g->sorted_order[q];
3083 #ifndef STB_VORBIS_NO_DEFER_FLOOR
3089 int hy = finalY[j] * g->floor1_multiplier;
3090 int hx = g->Xlist[j];
3092 stbv_draw_line(target, lx,ly, hx,hy, n2);
3099 for (j=lx; j < n2; ++j)
3100 STBV_LINE_OP(target[j], stbv_inverse_db_table[ly]);
3121static int stbv_vorbis_decode_initial(stbv_vorb *f,
int *p_left_start,
int *p_left_end,
int *p_right_start,
int *p_right_end,
int *mode)
3124 int i, n, prev, next, window_center;
3125 f->channel_buffer_start = f->channel_buffer_end = 0;
3128 if (f->eof)
return FALSE;
3129 if (!stbv_maybe_start_packet(f))
3132 if (stbv_get_bits(f,1) != 0) {
3133 if (STBV_IS_PUSH_MODE(f))
3135 while (STBV_EOP != stbv_get8_packet(f));
3139 if (f->alloc.alloc_buffer)
3140 assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3142 i = stbv_get_bits(f, stbv_ilog(f->mode_count-1));
3143 if (i == STBV_EOP)
return FALSE;
3144 if (i >= f->mode_count)
return FALSE;
3146 m = f->mode_config + i;
3149 prev = stbv_get_bits(f,1);
3150 next = stbv_get_bits(f,1);
3158 window_center = n >> 1;
3159 if (m->blockflag && !prev) {
3160 *p_left_start = (n - f->blocksize_0) >> 2;
3161 *p_left_end = (n + f->blocksize_0) >> 2;
3164 *p_left_end = window_center;
3166 if (m->blockflag && !next) {
3167 *p_right_start = (n*3 - f->blocksize_0) >> 2;
3168 *p_right_end = (n*3 + f->blocksize_0) >> 2;
3170 *p_right_start = window_center;
3177static int stbv_vorbis_decode_packet_rest(stbv_vorb *f,
int *len, StbvMode *m,
int left_start,
int left_end,
int right_start,
int right_end,
int *p_left)
3181 int zero_channel[256];
3182 int really_zero_channel[256];
3186 n = f->blocksize[m->blockflag];
3187 map = &f->mapping[m->mapping];
3194 for (i=0; i < f->channels; ++i) {
3195 int s = map->chan[i].mux, floor;
3196 zero_channel[i] = FALSE;
3197 floor = map->submap_floor[s];
3198 if (f->floor_types[floor] == 0) {
3201 StbvFloor1 *g = &f->floor_config[floor].floor1;
3202 if (stbv_get_bits(f, 1)) {
3204 stbv_uint8 step2_flag[256];
3205 static int range_list[4] = { 256, 128, 86, 64 };
3206 int range = range_list[g->floor1_multiplier-1];
3208 finalY = f->finalY[i];
3209 finalY[0] = stbv_get_bits(f, stbv_ilog(range)-1);
3210 finalY[1] = stbv_get_bits(f, stbv_ilog(range)-1);
3211 for (j=0; j < g->partitions; ++j) {
3212 int pclass = g->partition_class_list[j];
3213 int cdim = g->class_dimensions[pclass];
3214 int cbits = g->class_subclasses[pclass];
3215 int csub = (1 << cbits)-1;
3218 StbvCodebook *c = f->codebooks + g->class_masterbooks[pclass];
3219 STBV_DECODE(cval,f,c);
3221 for (k=0; k < cdim; ++k) {
3222 int book = g->subclass_books[pclass][cval & csub];
3223 cval = cval >> cbits;
3226 StbvCodebook *c = f->codebooks + book;
3227 STBV_DECODE(temp,f,c);
3228 finalY[offset++] = temp;
3230 finalY[offset++] = 0;
3233 if (f->valid_bits == STBV_INVALID_BITS)
goto error;
3234 step2_flag[0] = step2_flag[1] = 1;
3235 for (j=2; j < g->values; ++j) {
3236 int low, high, pred, highroom, lowroom, room, val;
3237 low = g->stbv_neighbors[j][0];
3238 high = g->stbv_neighbors[j][1];
3240 pred = stbv_predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]);
3242 highroom = range - pred;
3244 if (highroom < lowroom)
3245 room = highroom * 2;
3249 step2_flag[low] = step2_flag[high] = 1;
3252 if (highroom > lowroom)
3253 finalY[j] = val - lowroom + pred;
3255 finalY[j] = pred - val + highroom - 1;
3258 finalY[j] = pred - ((val+1)>>1);
3260 finalY[j] = pred + (val>>1);
3267#ifdef STB_VORBIS_NO_DEFER_FLOOR
3268 stbv_do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
3271 for (j=0; j < g->values; ++j) {
3278 zero_channel[i] = TRUE;
3288 if (f->alloc.alloc_buffer)
3289 assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3292 memcpy(really_zero_channel, zero_channel,
sizeof(really_zero_channel[0]) * f->channels);
3293 for (i=0; i < map->coupling_steps; ++i)
3294 if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) {
3295 zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE;
3300 for (i=0; i < map->submaps; ++i) {
3301 float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
3303 stbv_uint8 do_not_decode[256];
3305 for (j=0; j < f->channels; ++j) {
3306 if (map->chan[j].mux == i) {
3307 if (zero_channel[j]) {
3308 do_not_decode[ch] = TRUE;
3309 residue_buffers[ch] =
NULL;
3311 do_not_decode[ch] = FALSE;
3312 residue_buffers[ch] = f->channel_buffers[j];
3317 r = map->submap_residue[i];
3318 stbv_decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
3321 if (f->alloc.alloc_buffer)
3322 assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3326 for (i = map->coupling_steps-1; i >= 0; --i) {
3328 float *m = f->channel_buffers[map->chan[i].magnitude];
3329 float *a = f->channel_buffers[map->chan[i].angle ];
3330 for (j=0; j < n2; ++j) {
3334 m2 = m[j], a2 = m[j] - a[j];
3336 a2 = m[j], m2 = m[j] + a[j];
3339 m2 = m[j], a2 = m[j] + a[j];
3341 a2 = m[j], m2 = m[j] - a[j];
3349#ifndef STB_VORBIS_NO_DEFER_FLOOR
3350 for (i=0; i < f->channels; ++i) {
3351 if (really_zero_channel[i]) {
3352 memset(f->channel_buffers[i], 0,
sizeof(*f->channel_buffers[i]) * n2);
3354 stbv_do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i],
NULL);
3358 for (i=0; i < f->channels; ++i) {
3359 if (really_zero_channel[i]) {
3360 memset(f->channel_buffers[i], 0,
sizeof(*f->channel_buffers[i]) * n2);
3362 for (j=0; j < n2; ++j)
3363 f->channel_buffers[i][j] *= f->floor_buffers[i][j];
3370 for (i=0; i < f->channels; ++i)
3371 stbv_inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
3376 stbv_flush_packet(f);
3378 if (f->first_decode) {
3383 f->current_loc = -n2;
3386 f->discard_samples_deferred = n - right_end;
3387 f->current_loc_valid = TRUE;
3388 f->first_decode = FALSE;
3389 }
else if (f->discard_samples_deferred) {
3390 if (f->discard_samples_deferred >= right_start - left_start) {
3391 f->discard_samples_deferred -= (right_start - left_start);
3392 left_start = right_start;
3393 *p_left = left_start;
3395 left_start += f->discard_samples_deferred;
3396 *p_left = left_start;
3397 f->discard_samples_deferred = 0;
3399 }
else if (f->previous_length == 0 && f->current_loc_valid) {
3409 if (f->last_seg_which == f->end_seg_with_known_loc) {
3411 if (f->current_loc_valid && (f->page_flag & STBV_PAGEFLAG_last_page)) {
3412 stbv_uint32 current_end = f->known_loc_for_packet;
3414 if (current_end < f->current_loc + (right_end-left_start)) {
3415 if (current_end < f->current_loc) {
3419 *len = current_end - f->current_loc;
3422 if (*len > right_end) *len = right_end;
3423 f->current_loc += *len;
3431 f->current_loc = f->known_loc_for_packet - (n2-left_start);
3432 f->current_loc_valid = TRUE;
3434 if (f->current_loc_valid)
3435 f->current_loc += (right_start - left_start);
3437 if (f->alloc.alloc_buffer)
3438 assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
3445static int stbv_vorbis_decode_packet(stbv_vorb *f,
int *len,
int *p_left,
int *p_right)
3447 int mode, left_end, right_end;
3448 if (!stbv_vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode))
return 0;
3449 return stbv_vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left);
3452static int stbv_vorbis_finish_frame(
stb_vorbis *f,
int len,
int left,
int right)
3464 if (f->previous_length) {
3465 int i,j, n = f->previous_length;
3466 float *w = stbv_get_window(f, n);
3467 for (i=0; i < f->channels; ++i) {
3468 for (j=0; j < n; ++j)
3469 f->channel_buffers[i][left+j] =
3470 f->channel_buffers[i][left+j]*w[ j] +
3471 f->previous_window[i][ j]*w[n-1-j];
3475 prev = f->previous_length;
3478 f->previous_length = len - right;
3486 for (i=0; i < f->channels; ++i)
3487 for (j=0; right+j < len; ++j)
3488 f->previous_window[i][j] = f->channel_buffers[i][right+j];
3497 if (len < right) right = len;
3499 f->samples_output += right-left;
3501 return right - left;
3504static int stbv_vorbis_pump_first_frame(
stb_vorbis *f)
3506 int len, right, left, res;
3507 res = stbv_vorbis_decode_packet(f, &len, &left, &right);
3509 stbv_vorbis_finish_frame(f, len, left, right);
3513#ifndef STB_VORBIS_NO_PUSHDATA_API
3514static int stbv_is_whole_packet_present(
stb_vorbis *f,
int end_page)
3524 int s = f->next_seg, first = TRUE;
3525 stbv_uint8 *p = f->stream;
3528 for (; s < f->segment_count; ++s) {
3529 p += f->segments[s];
3530 if (f->segments[s] < 255)
3536 if (s == f->segment_count)
3551 if (f->previous_length)
3563 for (s=0; s < n; ++s) {
3579static int stbv_start_decoder(stbv_vorb *f)
3581 stbv_uint8 header[6], x,y;
3582 int len,i,j,k, max_submaps = 0;
3583 int longest_floorlist=0;
3587 if (!stbv_start_page(f))
return FALSE;
3613 f->blocksize_0 = 1 << log0;
3614 f->blocksize_1 = 1 << log1;
3625 if (!stbv_start_page(f))
return FALSE;
3627 if (!stbv_start_packet(f))
return FALSE;
3629 len = stbv_next_segment(f);
3631 f->bytes_in_seg = 0;
3635 if (!stbv_start_packet(f))
return FALSE;
3637 #ifndef STB_VORBIS_NO_PUSHDATA_API
3638 if (STBV_IS_PUSH_MODE(f)) {
3639 if (!stbv_is_whole_packet_present(f, TRUE)) {
3650 if (stbv_get8_packet(f) != STBV_VORBIS_packet_setup)
return stbv_error(f,
VORBIS_invalid_setup);
3651 for (i=0; i < 6; ++i) header[i] = stbv_get8_packet(f);
3656 f->codebook_count = stbv_get_bits(f,8) + 1;
3657 f->codebooks = (StbvCodebook *) stbv_setup_malloc(f,
sizeof(*f->codebooks) * f->codebook_count);
3659 memset(f->codebooks, 0,
sizeof(*f->codebooks) * f->codebook_count);
3660 for (i=0; i < f->codebook_count; ++i) {
3661 stbv_uint32 *values;
3662 int ordered, sorted_count;
3664 stbv_uint8 *lengths;
3665 StbvCodebook *c = f->codebooks+i;
3670 x = stbv_get_bits(f, 8);
3671 c->dimensions = (stbv_get_bits(f, 8)<<8) + x;
3672 x = stbv_get_bits(f, 8);
3673 y = stbv_get_bits(f, 8);
3674 c->entries = (stbv_get_bits(f, 8)<<16) + (y<<8) + x;
3675 ordered = stbv_get_bits(f,1);
3676 c->sparse = ordered ? 0 : stbv_get_bits(f,1);
3681 lengths = (stbv_uint8 *) stbv_setup_temp_malloc(f, c->entries);
3683 lengths = c->codeword_lengths = (stbv_uint8 *) stbv_setup_malloc(f, c->entries);
3688 int current_entry = 0;
3689 int current_length = stbv_get_bits(f,5) + 1;
3690 while (current_entry < c->entries) {
3691 int limit = c->entries - current_entry;
3692 int n = stbv_get_bits(f, stbv_ilog(limit));
3694 memset(lengths + current_entry, current_length, n);
3699 for (j=0; j < c->entries; ++j) {
3700 int present = c->sparse ? stbv_get_bits(f,1) : 1;
3702 lengths[j] = stbv_get_bits(f, 5) + 1;
3704 if (lengths[j] == 32)
3707 lengths[j] = NO_CODE;
3712 if (c->sparse && total >= c->entries >> 2) {
3714 if (c->entries > (
int) f->setup_temp_memory_required)
3715 f->setup_temp_memory_required = c->entries;
3717 c->codeword_lengths = (stbv_uint8 *) stbv_setup_malloc(f, c->entries);
3719 memcpy(c->codeword_lengths, lengths, c->entries);
3720 stbv_setup_temp_free(f, lengths, c->entries);
3721 lengths = c->codeword_lengths;
3727 sorted_count = total;
3730 #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
3731 for (j=0; j < c->entries; ++j)
3732 if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
3737 c->sorted_entries = sorted_count;
3742 c->codewords = (stbv_uint32 *) stbv_setup_malloc(f,
sizeof(c->codewords[0]) * c->entries);
3746 if (c->sorted_entries) {
3747 c->codeword_lengths = (stbv_uint8 *) stbv_setup_malloc(f, c->sorted_entries);
3749 c->codewords = (stbv_uint32 *) stbv_setup_temp_malloc(f,
sizeof(*c->codewords) * c->sorted_entries);
3751 values = (stbv_uint32 *) stbv_setup_temp_malloc(f,
sizeof(*values) * c->sorted_entries);
3754 size = c->entries + (
sizeof(*c->codewords) +
sizeof(*values)) * c->sorted_entries;
3755 if (size > f->setup_temp_memory_required)
3756 f->setup_temp_memory_required = size;
3759 if (!stbv_compute_codewords(c, lengths, c->entries, values)) {
3760 if (c->sparse) stbv_setup_temp_free(f, values, 0);
3764 if (c->sorted_entries) {
3766 c->sorted_codewords = (stbv_uint32 *) stbv_setup_malloc(f,
sizeof(*c->sorted_codewords) * (c->sorted_entries+1));
3770 c->sorted_values = (
int *) stbv_setup_malloc(f,
sizeof(*c->sorted_values ) * (c->sorted_entries+1));
3773 c->sorted_values[-1] = -1;
3774 stbv_compute_sorted_huffman(c, lengths, values);
3778 stbv_setup_temp_free(f, values,
sizeof(*values)*c->sorted_entries);
3779 stbv_setup_temp_free(f, c->codewords,
sizeof(*c->codewords)*c->sorted_entries);
3780 stbv_setup_temp_free(f, lengths, c->entries);
3781 c->codewords =
NULL;
3784 stbv_compute_accelerated_huffman(c);
3787 c->lookup_type = stbv_get_bits(f, 4);
3789 if (c->lookup_type > 0) {
3791 c->minimum_value = stbv_float32_unpack(stbv_get_bits(f, 32));
3792 c->delta_value = stbv_float32_unpack(stbv_get_bits(f, 32));
3793 c->value_bits = stbv_get_bits(f, 4)+1;
3794 c->sequence_p = stbv_get_bits(f,1);
3795 if (c->lookup_type == 1) {
3796 c->lookup_values = stbv_lookup1_values(c->entries, c->dimensions);
3798 c->lookup_values = c->entries * c->dimensions;
3801 mults = (stbv_uint16 *) stbv_setup_temp_malloc(f,
sizeof(mults[0]) * c->lookup_values);
3803 for (j=0; j < (int) c->lookup_values; ++j) {
3804 int q = stbv_get_bits(f, c->value_bits);
3805 if (q == STBV_EOP) { stbv_setup_temp_free(f,mults,
sizeof(mults[0])*c->lookup_values);
return stbv_error(f,
VORBIS_invalid_setup); }
3809#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3810 if (c->lookup_type == 1) {
3811 int len, sparse = c->sparse;
3815 if (c->sorted_entries == 0)
goto stbv_skip;
3816 c->multiplicands = (stbv_codetype *) stbv_setup_malloc(f,
sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
3818 c->multiplicands = (stbv_codetype *) stbv_setup_malloc(f,
sizeof(c->multiplicands[0]) * c->entries * c->dimensions);
3819 if (c->multiplicands ==
NULL) { stbv_setup_temp_free(f,mults,
sizeof(mults[0])*c->lookup_values);
return stbv_error(f,
VORBIS_outofmem); }
3820 len = sparse ? c->sorted_entries : c->entries;
3821 for (j=0; j < len; ++j) {
3822 unsigned int z = sparse ? c->sorted_values[j] : j;
3824 for (k=0; k < c->dimensions; ++k) {
3825 int off = (z / div) % c->lookup_values;
3826 float val = mults[off];
3827 val = mults[off]*c->delta_value + c->minimum_value + last;
3828 c->multiplicands[j*c->dimensions + k] = val;
3831 if (k+1 < c->dimensions) {
3832 if (div > UINT_MAX / (
unsigned int) c->lookup_values) {
3833 stbv_setup_temp_free(f, mults,
sizeof(mults[0])*c->lookup_values);
3836 div *= c->lookup_values;
3847 c->multiplicands = (stbv_codetype *) stbv_setup_malloc(f,
sizeof(c->multiplicands[0]) * c->lookup_values);
3848 if (c->multiplicands ==
NULL) { stbv_setup_temp_free(f, mults,
sizeof(mults[0])*c->lookup_values);
return stbv_error(f,
VORBIS_outofmem); }
3849 for (j=0; j < (int) c->lookup_values; ++j) {
3850 float val = mults[j] * c->delta_value + c->minimum_value + last;
3851 c->multiplicands[j] = val;
3856#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3859 stbv_setup_temp_free(f, mults,
sizeof(mults[0])*c->lookup_values);
3868 x = stbv_get_bits(f, 6) + 1;
3869 for (i=0; i < x; ++i) {
3870 stbv_uint32 z = stbv_get_bits(f, 16);
3875 f->floor_count = stbv_get_bits(f, 6)+1;
3876 f->floor_config = (StbvFloor *) stbv_setup_malloc(f, f->floor_count *
sizeof(*f->floor_config));
3878 for (i=0; i < f->floor_count; ++i) {
3879 f->floor_types[i] = stbv_get_bits(f, 16);
3881 if (f->floor_types[i] == 0) {
3882 StbvFloor0 *g = &f->floor_config[i].floor0;
3883 g->order = stbv_get_bits(f,8);
3884 g->rate = stbv_get_bits(f,16);
3885 g->bark_map_size = stbv_get_bits(f,16);
3886 g->amplitude_bits = stbv_get_bits(f,6);
3887 g->amplitude_offset = stbv_get_bits(f,8);
3888 g->number_of_books = stbv_get_bits(f,4) + 1;
3889 for (j=0; j < g->number_of_books; ++j)
3890 g->book_list[j] = stbv_get_bits(f,8);
3893 stbv_floor_ordering p[31*8+2];
3894 StbvFloor1 *g = &f->floor_config[i].floor1;
3896 g->partitions = stbv_get_bits(f, 5);
3897 for (j=0; j < g->partitions; ++j) {
3898 g->partition_class_list[j] = stbv_get_bits(f, 4);
3899 if (g->partition_class_list[j] > max_class)
3900 max_class = g->partition_class_list[j];
3902 for (j=0; j <= max_class; ++j) {
3903 g->class_dimensions[j] = stbv_get_bits(f, 3)+1;
3904 g->class_subclasses[j] = stbv_get_bits(f, 2);
3905 if (g->class_subclasses[j]) {
3906 g->class_masterbooks[j] = stbv_get_bits(f, 8);
3909 for (k=0; k < 1 << g->class_subclasses[j]; ++k) {
3910 g->subclass_books[j][k] = stbv_get_bits(f,8)-1;
3914 g->floor1_multiplier = stbv_get_bits(f,2)+1;
3915 g->rangebits = stbv_get_bits(f,4);
3917 g->Xlist[1] = 1 << g->rangebits;
3919 for (j=0; j < g->partitions; ++j) {
3920 int c = g->partition_class_list[j];
3921 for (k=0; k < g->class_dimensions[c]; ++k) {
3922 g->Xlist[g->values] = stbv_get_bits(f, g->rangebits);
3927 for (j=0; j < g->values; ++j) {
3928 p[j].x = g->Xlist[j];
3931 qsort(p, g->values,
sizeof(p[0]), stbv_point_compare);
3932 for (j=0; j < g->values; ++j)
3933 g->sorted_order[j] = (stbv_uint8) p[j].id;
3935 for (j=2; j < g->values; ++j) {
3937 stbv_neighbors(g->Xlist, j, &low,&hi);
3938 g->stbv_neighbors[j][0] = low;
3939 g->stbv_neighbors[j][1] = hi;
3942 if (g->values > longest_floorlist)
3943 longest_floorlist = g->values;
3948 f->residue_count = stbv_get_bits(f, 6)+1;
3949 f->residue_config = (StbvResidue *) stbv_setup_malloc(f, f->residue_count *
sizeof(f->residue_config[0]));
3951 memset(f->residue_config, 0, f->residue_count *
sizeof(f->residue_config[0]));
3952 for (i=0; i < f->residue_count; ++i) {
3953 stbv_uint8 residue_cascade[64];
3954 StbvResidue *r = f->residue_config+i;
3955 f->residue_types[i] = stbv_get_bits(f, 16);
3957 r->begin = stbv_get_bits(f, 24);
3958 r->end = stbv_get_bits(f, 24);
3960 r->part_size = stbv_get_bits(f,24)+1;
3961 r->classifications = stbv_get_bits(f,6)+1;
3962 r->classbook = stbv_get_bits(f,8);
3964 for (j=0; j < r->classifications; ++j) {
3965 stbv_uint8 high_bits=0;
3966 stbv_uint8 low_bits=stbv_get_bits(f,3);
3967 if (stbv_get_bits(f,1))
3968 high_bits = stbv_get_bits(f,5);
3969 residue_cascade[j] = high_bits*8 + low_bits;
3971 r->residue_books = (short (*)[8]) stbv_setup_malloc(f,
sizeof(r->residue_books[0]) * r->classifications);
3973 for (j=0; j < r->classifications; ++j) {
3974 for (k=0; k < 8; ++k) {
3975 if (residue_cascade[j] & (1 << k)) {
3976 r->residue_books[j][k] = stbv_get_bits(f, 8);
3979 r->residue_books[j][k] = -1;
3985 r->classdata = (stbv_uint8 **) stbv_setup_malloc(f,
sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
3987 memset(r->classdata, 0,
sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
3988 for (j=0; j < f->codebooks[r->classbook].entries; ++j) {
3989 int classwords = f->codebooks[r->classbook].dimensions;
3991 r->classdata[j] = (stbv_uint8 *) stbv_setup_malloc(f,
sizeof(r->classdata[j][0]) * classwords);
3993 for (k=classwords-1; k >= 0; --k) {
3994 r->classdata[j][k] = temp % r->classifications;
3995 temp /= r->classifications;
4000 f->mapping_count = stbv_get_bits(f,6)+1;
4001 f->mapping = (StbvMapping *) stbv_setup_malloc(f, f->mapping_count *
sizeof(*f->mapping));
4003 memset(f->mapping, 0, f->mapping_count *
sizeof(*f->mapping));
4004 for (i=0; i < f->mapping_count; ++i) {
4005 StbvMapping *m = f->mapping + i;
4006 int mapping_type = stbv_get_bits(f,16);
4008 m->chan = (StbvMappingChannel *) stbv_setup_malloc(f, f->channels *
sizeof(*m->chan));
4010 if (stbv_get_bits(f,1))
4011 m->submaps = stbv_get_bits(f,4)+1;
4014 if (m->submaps > max_submaps)
4015 max_submaps = m->submaps;
4016 if (stbv_get_bits(f,1)) {
4017 m->coupling_steps = stbv_get_bits(f,8)+1;
4018 for (k=0; k < m->coupling_steps; ++k) {
4019 m->chan[k].magnitude = stbv_get_bits(f, stbv_ilog(f->channels-1));
4020 m->chan[k].angle = stbv_get_bits(f, stbv_ilog(f->channels-1));
4026 m->coupling_steps = 0;
4030 if (m->submaps > 1) {
4031 for (j=0; j < f->channels; ++j) {
4032 m->chan[j].mux = stbv_get_bits(f, 4);
4037 for (j=0; j < f->channels; ++j)
4040 for (j=0; j < m->submaps; ++j) {
4042 m->submap_floor[j] = stbv_get_bits(f,8);
4043 m->submap_residue[j] = stbv_get_bits(f,8);
4050 f->mode_count = stbv_get_bits(f, 6)+1;
4051 for (i=0; i < f->mode_count; ++i) {
4052 StbvMode *m = f->mode_config+i;
4053 m->blockflag = stbv_get_bits(f,1);
4054 m->windowtype = stbv_get_bits(f,16);
4055 m->transformtype = stbv_get_bits(f,16);
4056 m->mapping = stbv_get_bits(f,8);
4062 stbv_flush_packet(f);
4064 f->previous_length = 0;
4066 for (i=0; i < f->channels; ++i) {
4067 f->channel_buffers[i] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * f->blocksize_1);
4068 f->previous_window[i] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * f->blocksize_1/2);
4069 f->finalY[i] = (stbv_int16 *) stbv_setup_malloc(f,
sizeof(stbv_int16) * longest_floorlist);
4070 if (f->channel_buffers[i] ==
NULL || f->previous_window[i] ==
NULL || f->finalY[i] ==
NULL)
return stbv_error(f,
VORBIS_outofmem);
4071 memset(f->channel_buffers[i], 0,
sizeof(
float) * f->blocksize_1);
4072 #ifdef STB_VORBIS_NO_DEFER_FLOOR
4073 f->floor_buffers[i] = (
float *) stbv_setup_malloc(f,
sizeof(
float) * f->blocksize_1/2);
4078 if (!stbv_init_blocksize(f, 0, f->blocksize_0))
return FALSE;
4079 if (!stbv_init_blocksize(f, 1, f->blocksize_1))
return FALSE;
4080 f->blocksize[0] = f->blocksize_0;
4081 f->blocksize[1] = f->blocksize_1;
4083#ifdef STB_VORBIS_DIVIDE_TABLE
4084 if (stbv_integer_divide_table[1][1]==0)
4085 for (i=0; i < STBV_DIVTAB_NUMER; ++i)
4086 for (j=1; j < STBV_DIVTAB_DENOM; ++j)
4087 stbv_integer_divide_table[i][j] = i / j;
4094 stbv_uint32 imdct_mem = (f->blocksize_1 *
sizeof(float) >> 1);
4095 stbv_uint32 classify_mem;
4096 int i,max_part_read=0;
4097 for (i=0; i < f->residue_count; ++i) {
4098 StbvResidue *r = f->residue_config + i;
4099 unsigned int actual_size = f->blocksize_1 / 2;
4100 unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size;
4101 unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size;
4102 int n_read = limit_r_end - limit_r_begin;
4103 int part_read = n_read / r->part_size;
4104 if (part_read > max_part_read)
4105 max_part_read = part_read;
4107 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
4108 classify_mem = f->channels * (
sizeof(
void*) + max_part_read *
sizeof(stbv_uint8 *));
4110 classify_mem = f->channels * (
sizeof(
void*) + max_part_read *
sizeof(
int *));
4115 f->temp_memory_required = classify_mem;
4116 if (imdct_mem > f->temp_memory_required)
4117 f->temp_memory_required = imdct_mem;
4120 f->first_decode = TRUE;
4122 if (f->alloc.alloc_buffer) {
4123 assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
4125 if (f->setup_offset +
sizeof(*f) + f->temp_memory_required > (
unsigned) f->temp_offset)
4137 if (p->residue_config) {
4138 for (i=0; i < p->residue_count; ++i) {
4139 StbvResidue *r = p->residue_config+i;
4141 for (j=0; j < p->codebooks[r->classbook].entries; ++j)
4142 stbv_setup_free(p, r->classdata[j]);
4143 stbv_setup_free(p, r->classdata);
4145 stbv_setup_free(p, r->residue_books);
4151 for (i=0; i < p->codebook_count; ++i) {
4152 StbvCodebook *c = p->codebooks + i;
4153 stbv_setup_free(p, c->codeword_lengths);
4154 stbv_setup_free(p, c->multiplicands);
4155 stbv_setup_free(p, c->codewords);
4156 stbv_setup_free(p, c->sorted_codewords);
4158 stbv_setup_free(p, c->sorted_values ? c->sorted_values-1 :
NULL);
4160 stbv_setup_free(p, p->codebooks);
4162 stbv_setup_free(p, p->floor_config);
4163 stbv_setup_free(p, p->residue_config);
4165 for (i=0; i < p->mapping_count; ++i)
4166 stbv_setup_free(p, p->mapping[i].chan);
4167 stbv_setup_free(p, p->mapping);
4170 for (i=0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) {
4171 stbv_setup_free(p, p->channel_buffers[i]);
4172 stbv_setup_free(p, p->previous_window[i]);
4173 #ifdef STB_VORBIS_NO_DEFER_FLOOR
4174 stbv_setup_free(p, p->floor_buffers[i]);
4176 stbv_setup_free(p, p->finalY[i]);
4178 for (i=0; i < 2; ++i) {
4179 stbv_setup_free(p, p->A[i]);
4180 stbv_setup_free(p, p->B[i]);
4181 stbv_setup_free(p, p->C[i]);
4182 stbv_setup_free(p, p->window[i]);
4183 stbv_setup_free(p, p->stbv_bit_reverse[i]);
4185 #ifndef STB_VORBIS_NO_STDIO
4186 if (p->close_on_free) fclose(p->f);
4192 if (p ==
NULL)
return;
4193 stbv_vorbis_deinit(p);
4194 stbv_setup_free(p,p);
4199 memset(p, 0,
sizeof(*p));
4203 p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
4208 p->codebooks =
NULL;
4209 p->page_crc_tests = -1;
4210 #ifndef STB_VORBIS_NO_STDIO
4211 p->close_on_free = FALSE;
4218 if (f->current_loc_valid)
4219 return f->current_loc;
4249#ifndef STB_VORBIS_NO_PUSHDATA_API
4253 f->previous_length = 0;
4254 f->page_crc_tests = 0;
4255 f->discard_samples_deferred = 0;
4256 f->current_loc_valid = FALSE;
4257 f->first_decode = FALSE;
4258 f->samples_output = 0;
4259 f->channel_buffer_start = 0;
4260 f->channel_buffer_end = 0;
4263static int stbv_vorbis_search_for_page_pushdata(stbv_vorb *f, stbv_uint8 *data,
int data_len)
4266 for (i=0; i < f->page_crc_tests; ++i)
4267 f->scan[i].bytes_done = 0;
4271 if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) {
4272 if (data_len < 4)
return 0;
4275 for (i=0; i < data_len; ++i) {
4276 if (data[i] == 0x4f) {
4277 if (0==memcmp(data+i, stbv_ogg_page_header, 4)) {
4281 if (i+26 >= data_len || i+27+data[i+26] >= data_len) {
4288 len = 27 + data[i+26];
4289 for (j=0; j < data[i+26]; ++j)
4290 len += data[i+27+j];
4293 for (j=0; j < 22; ++j)
4294 crc = stbv_crc32_update(crc, data[i+j]);
4296 for ( ; j < 26; ++j)
4297 crc = stbv_crc32_update(crc, 0);
4299 n = f->page_crc_tests++;
4300 f->scan[n].bytes_left = len-j;
4301 f->scan[n].crc_so_far = crc;
4302 f->scan[n].goal_crc = data[i+22] + (data[i+23] << 8) + (data[i+24]<<16) + (data[i+25]<<24);
4305 if (data[i+27+data[i+26]-1] == 255)
4306 f->scan[n].sample_loc = ~0;
4308 f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24);
4309 f->scan[n].bytes_done = i+j;
4310 if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT)
4318 for (i=0; i < f->page_crc_tests;) {
4321 int n = f->scan[i].bytes_done;
4322 int m = f->scan[i].bytes_left;
4323 if (m > data_len - n) m = data_len - n;
4325 crc = f->scan[i].crc_so_far;
4326 for (j=0; j < m; ++j)
4327 crc = stbv_crc32_update(crc, data[n+j]);
4328 f->scan[i].bytes_left -= m;
4329 f->scan[i].crc_so_far = crc;
4330 if (f->scan[i].bytes_left == 0) {
4332 if (f->scan[i].crc_so_far == f->scan[i].goal_crc) {
4335 f->page_crc_tests = -1;
4336 f->previous_length = 0;
4338 f->current_loc = f->scan[i].sample_loc;
4340 f->current_loc_valid = f->current_loc != ~0U;
4344 f->scan[i] = f->scan[--f->page_crc_tests];
4356 const stbv_uint8 *data,
int data_len,
4367 if (f->page_crc_tests >= 0) {
4369 return stbv_vorbis_search_for_page_pushdata(f, (stbv_uint8 *) data, data_len);
4372 f->stream = (stbv_uint8 *) data;
4373 f->stream_end = (stbv_uint8 *) data + data_len;
4377 if (!stbv_is_whole_packet_present(f, FALSE)) {
4382 if (!stbv_vorbis_decode_packet(f, &len, &left, &right)) {
4388 while (stbv_get8_packet(f) != STBV_EOP)
4391 return (
int) (f->stream - data);
4394 if (f->previous_length == 0) {
4398 while (stbv_get8_packet(f) != STBV_EOP)
4401 return (
int) (f->stream - data);
4414 len = stbv_vorbis_finish_frame(f, len, left, right);
4415 for (i=0; i < f->channels; ++i)
4416 f->outputs[i] = f->channel_buffers[i] + left;
4418 if (channels) *channels = f->channels;
4420 *output = f->outputs;
4421 return (
int) (f->stream - data);
4425 const unsigned char *data,
int data_len,
4430 stbv_vorbis_init(&p, alloc);
4431 p.stream = (stbv_uint8 *) data;
4432 p.stream_end = (stbv_uint8 *) data + data_len;
4434 if (!stbv_start_decoder(&p)) {
4441 f = stbv_vorbis_alloc(&p);
4444 *data_used = (int) (f->stream - data);
4448 stbv_vorbis_deinit(&p);
4456 #ifndef STB_VORBIS_NO_PUSHDATA_API
4457 if (f->push_mode)
return 0;
4459 if (STBV_USE_MEMORY(f))
return (
unsigned int) (f->stream - f->stream_start);
4460 #ifndef STB_VORBIS_NO_STDIO
4461 return (
unsigned int) (ftell(f->f) - f->f_start);
4465#ifndef STB_VORBIS_NO_PULLDATA_API
4470static stbv_uint32 stbv_vorbis_find_page(
stb_vorbis *f, stbv_uint32 *end, stbv_uint32 *last)
4474 if (f->eof)
return 0;
4480 if (retry_loc - 25 > f->stream_len)
4483 for (i=1; i < 4; ++i)
4484 if (stbv_get8(f) != stbv_ogg_page_header[i])
4486 if (f->eof)
return 0;
4488 stbv_uint8 header[27];
4489 stbv_uint32 i, crc, goal, len;
4490 for (i=0; i < 4; ++i)
4491 header[i] = stbv_ogg_page_header[i];
4493 header[i] = stbv_get8(f);
4494 if (f->eof)
return 0;
4495 if (header[4] != 0)
goto invalid;
4496 goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24);
4497 for (i=22; i < 26; ++i)
4500 for (i=0; i < 27; ++i)
4501 crc = stbv_crc32_update(crc, header[i]);
4503 for (i=0; i < header[26]; ++i) {
4504 int s = stbv_get8(f);
4505 crc = stbv_crc32_update(crc, s);
4508 if (len && f->eof)
return 0;
4509 for (i=0; i < len; ++i)
4510 crc = stbv_crc32_update(crc, stbv_get8(f));
4524 if (header[5] & 0x04)
4529 stbv_set_file_offset(f, retry_loc-1);
4535 stbv_set_file_offset(f, retry_loc);
4541#define STBV_SAMPLE_unknown 0xffffffff
4552static int stbv_get_seek_page_info(
stb_vorbis *f, StbvProbedPage *z)
4554 stbv_uint8 header[27], lacing[255];
4561 stbv_getn(f, header, 27);
4562 if (header[0] !=
'O' || header[1] !=
'g' || header[2] !=
'g' || header[3] !=
'S')
4564 stbv_getn(f, lacing, header[26]);
4568 for (i=0; i < header[26]; ++i)
4572 z->page_end = z->page_start + 27 + header[26] + len;
4575 z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
4578 stbv_set_file_offset(f, z->page_start);
4584static int stbv_go_to_page_before(
stb_vorbis *f,
unsigned int limit_offset)
4586 unsigned int previous_safe, end;
4589 if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset)
4590 previous_safe = limit_offset - 65536;
4592 previous_safe = f->first_audio_page_offset;
4594 stbv_set_file_offset(f, previous_safe);
4596 while (stbv_vorbis_find_page(f, &end,
NULL)) {
4599 stbv_set_file_offset(f, end);
4609static int stbv_seek_to_sample_coarse(
stb_vorbis *f, stbv_uint32 sample_number)
4611 StbvProbedPage left, right, mid;
4612 int i, start_seg_with_known_loc, end_pos, page_start;
4613 stbv_uint32 delta, stream_length, padding;
4614 double offset, bytes_per_sample;
4625 padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
4626 if (sample_number < padding)
4629 sample_number -= padding;
4632 while (left.last_decoded_sample == ~0U) {
4634 stbv_set_file_offset(f, left.page_end);
4635 if (!stbv_get_seek_page_info(f, &left))
goto error;
4639 assert(right.last_decoded_sample != ~0U);
4642 if (sample_number <= left.last_decoded_sample) {
4648 while (left.page_end != right.page_start) {
4649 assert(left.page_end < right.page_start);
4651 delta = right.page_start - left.page_end;
4652 if (delta <= 65536) {
4654 stbv_set_file_offset(f, left.page_end);
4659 double data_bytes = right.page_end - left.page_start;
4660 bytes_per_sample = data_bytes / right.last_decoded_sample;
4661 offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
4664 double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
4665 if (error >= 0 && error < 8000) error = 8000;
4666 if (error < 0 && error > -8000) error = -8000;
4667 offset += error * 2;
4671 if (offset < left.page_end)
4672 offset = left.page_end;
4673 if (offset > right.page_start - 65536)
4674 offset = right.page_start - 65536;
4676 stbv_set_file_offset(f, (
unsigned int) offset);
4680 stbv_set_file_offset(f, left.page_end + (delta / 2) - 32768);
4683 if (!stbv_vorbis_find_page(f,
NULL,
NULL))
goto error;
4687 if (!stbv_get_seek_page_info(f, &mid))
goto error;
4688 if (mid.last_decoded_sample != ~0U)
break;
4690 stbv_set_file_offset(f, mid.page_end);
4691 assert(mid.page_start < right.page_start);
4696 if (mid.page_start == right.page_start)
4699 if (sample_number < mid.last_decoded_sample)
4708 page_start = left.page_start;
4709 stbv_set_file_offset(f, page_start);
4711 end_pos = f->end_seg_with_known_loc;
4712 assert(end_pos >= 0);
4715 for (i = end_pos; i > 0; --i)
4716 if (f->segments[i-1] != 255)
4719 start_seg_with_known_loc = i;
4721 if (start_seg_with_known_loc > 0 || !(f->page_flag & STBV_PAGEFLAG_continued_packet))
4725 if (!stbv_go_to_page_before(f, page_start))
4729 if (!stbv_start_page(f))
goto error;
4730 end_pos = f->segment_count - 1;
4734 f->current_loc_valid = FALSE;
4735 f->last_seg = FALSE;
4737 f->packet_bytes = 0;
4738 f->bytes_in_seg = 0;
4739 f->previous_length = 0;
4740 f->next_seg = start_seg_with_known_loc;
4742 for (i = 0; i < start_seg_with_known_loc; i++)
4743 stbv_skip(f, f->segments[i]);
4746 if (!stbv_vorbis_pump_first_frame(f))
4748 if (f->current_loc > sample_number)
4759static int stbv_peek_decode_initial(stbv_vorb *f,
int *p_left_start,
int *p_left_end,
int *p_right_start,
int *p_right_end,
int *mode)
4761 int bits_read, bytes_read;
4763 if (!stbv_vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode))
4767 bits_read = 1 + stbv_ilog(f->mode_count-1);
4768 if (f->mode_config[*mode].blockflag)
4770 bytes_read = (bits_read + 7) / 8;
4772 f->bytes_in_seg += bytes_read;
4773 f->packet_bytes -= bytes_read;
4774 stbv_skip(f, -bytes_read);
4775 if (f->next_seg == -1)
4776 f->next_seg = f->segment_count - 1;
4786 stbv_uint32 max_frame_samples;
4791 if (!stbv_seek_to_sample_coarse(f, sample_number))
4794 assert(f->current_loc_valid);
4795 assert(f->current_loc <= sample_number);
4798 max_frame_samples = (f->blocksize_1*3 - f->blocksize_0) >> 2;
4799 while (f->current_loc < sample_number) {
4800 int left_start, left_end, right_start, right_end, mode, frame_samples;
4801 if (!stbv_peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
4804 frame_samples = right_start - left_start;
4805 if (f->current_loc + frame_samples > sample_number) {
4807 }
else if (f->current_loc + frame_samples + max_frame_samples > sample_number) {
4809 stbv_vorbis_pump_first_frame(f);
4812 f->current_loc += frame_samples;
4813 f->previous_length = 0;
4814 stbv_maybe_start_packet(f);
4815 stbv_flush_packet(f);
4819 assert(f->current_loc == sample_number);
4828 if (sample_number != f->current_loc) {
4830 stbv_uint32 frame_start = f->current_loc;
4832 assert(sample_number > frame_start);
4833 assert(f->channel_buffer_start + (
int) (sample_number-frame_start) <= f->channel_buffer_end);
4834 f->channel_buffer_start += (sample_number - frame_start);
4843 stbv_set_file_offset(f, f->first_audio_page_offset);
4844 f->previous_length = 0;
4845 f->first_decode = TRUE;
4847 return stbv_vorbis_pump_first_frame(f);
4852 unsigned int restore_offset, previous_safe;
4853 unsigned int end, last_page_loc;
4856 if (!f->total_samples) {
4866 if (f->stream_len >= 65536 && f->stream_len-65536 >= f->first_audio_page_offset)
4867 previous_safe = f->stream_len - 65536;
4869 previous_safe = f->first_audio_page_offset;
4871 stbv_set_file_offset(f, previous_safe);
4875 if (!stbv_vorbis_find_page(f, &end, &last)) {
4878 f->total_samples = 0xffffffff;
4889 stbv_set_file_offset(f, end);
4890 if (!stbv_vorbis_find_page(f, &end, &last)) {
4895 previous_safe = last_page_loc+1;
4899 stbv_set_file_offset(f, last_page_loc);
4902 stbv_getn(f, (
unsigned char *)header, 6);
4906 if (lo == 0xffffffff && hi == 0xffffffff) {
4908 f->total_samples = STBV_SAMPLE_unknown;
4913 f->total_samples = lo;
4915 f->p_last.page_start = last_page_loc;
4916 f->p_last.page_end = end;
4917 f->p_last.last_decoded_sample = lo;
4920 stbv_set_file_offset(f, restore_offset);
4922 return f->total_samples == STBV_SAMPLE_unknown ? 0 : f->total_samples;
4934 int len, right,left,i;
4937 if (!stbv_vorbis_decode_packet(f, &len, &left, &right)) {
4938 f->channel_buffer_start = f->channel_buffer_end = 0;
4942 len = stbv_vorbis_finish_frame(f, len, left, right);
4943 for (i=0; i < f->channels; ++i)
4944 f->outputs[i] = f->channel_buffers[i] + left;
4946 f->channel_buffer_start = left;
4947 f->channel_buffer_end = left+len;
4949 if (channels) *channels = f->channels;
4950 if (output) *output = f->outputs;
4954#ifndef STB_VORBIS_NO_STDIO
4959 stbv_vorbis_init(&p, alloc);
4961 p.f_start = (stbv_uint32) ftell(file);
4962 p.stream_len = length;
4963 p.close_on_free = close_on_free;
4964 if (stbv_start_decoder(&p)) {
4965 f = stbv_vorbis_alloc(&p);
4968 stbv_vorbis_pump_first_frame(f);
4972 if (error) *error = p.error;
4973 stbv_vorbis_deinit(&p);
4979 unsigned int len, start;
4980 start = (
unsigned int) ftell(file);
4981 fseek(file, 0, SEEK_END);
4982 len = (
unsigned int) (ftell(file) - start);
4983 fseek(file, start, SEEK_SET);
4989 FILE *f = fopen(filename,
"rb");
5001 stbv_vorbis_init(&p, alloc);
5002 p.stream = (stbv_uint8 *) data;
5003 p.stream_end = (stbv_uint8 *) data + len;
5004 p.stream_start = (stbv_uint8 *) p.stream;
5006 p.push_mode = FALSE;
5007 if (stbv_start_decoder(&p)) {
5008 f = stbv_vorbis_alloc(&p);
5011 stbv_vorbis_pump_first_frame(f);
5016 if (error) *error = p.error;
5017 stbv_vorbis_deinit(&p);
5021#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
5022#define STBV_PLAYBACK_MONO 1
5023#define STBV_PLAYBACK_LEFT 2
5024#define STBV_PLAYBACK_RIGHT 4
5026#define STBV_L (STBV_PLAYBACK_LEFT | STBV_PLAYBACK_MONO)
5027#define STBV_C (STBV_PLAYBACK_LEFT | STBV_PLAYBACK_RIGHT | STBV_PLAYBACK_MONO)
5028#define STBV_R (STBV_PLAYBACK_RIGHT | STBV_PLAYBACK_MONO)
5030static stbv_int8 stbv_channel_position[7][6] =
5035 { STBV_L, STBV_C, STBV_R },
5036 { STBV_L, STBV_R, STBV_L, STBV_R },
5037 { STBV_L, STBV_C, STBV_R, STBV_L, STBV_R },
5038 { STBV_L, STBV_C, STBV_R, STBV_L, STBV_R, STBV_C },
5042#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
5047 typedef char stb_vorbis_float_size_test[
sizeof(float)==4 &&
sizeof(
int) == 4];
5048 #define STBV_FASTDEF(x) stbv_float_conv x
5050 #define STBV_MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
5051 #define STBV_ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
5052 #define STBV_FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + STBV_MAGIC(s), temp.i - STBV_ADDEND(s))
5053 #define stbv_check_endianness()
5055 #define STBV_FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
5056 #define stbv_check_endianness()
5057 #define STBV_FASTDEF(x)
5060static void stbv_copy_samples(
short *dest,
float *src,
int len)
5063 stbv_check_endianness();
5064 for (i=0; i < len; ++i) {
5066 int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
5067 if ((
unsigned int) (v + 32768) > 65535)
5068 v = v < 0 ? -32768 : 32767;
5073static void stbv_compute_samples(
int mask,
short *output,
int num_c,
float **data,
int d_offset,
int len)
5075 #define BUFFER_SIZE 32
5076 float buffer[BUFFER_SIZE];
5077 int i,j,o,n = BUFFER_SIZE;
5078 stbv_check_endianness();
5079 for (o = 0; o < len; o += BUFFER_SIZE) {
5080 memset(buffer, 0,
sizeof(buffer));
5081 if (o + n > len) n = len - o;
5082 for (j=0; j < num_c; ++j) {
5083 if (stbv_channel_position[num_c][j] & mask) {
5084 for (i=0; i < n; ++i)
5085 buffer[i] += data[j][d_offset+o+i];
5088 for (i=0; i < n; ++i) {
5090 int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5091 if ((
unsigned int) (v + 32768) > 65535)
5092 v = v < 0 ? -32768 : 32767;
5098static void stbv_compute_stereo_samples(
short *output,
int num_c,
float **data,
int d_offset,
int len)
5100 #define BUFFER_SIZE 32
5101 float buffer[BUFFER_SIZE];
5102 int i,j,o,n = BUFFER_SIZE >> 1;
5104 stbv_check_endianness();
5105 for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
5108 memset(buffer, 0,
sizeof(buffer));
5109 if (o + n > len) n = len - o;
5110 for (j=0; j < num_c; ++j) {
5111 int m = stbv_channel_position[num_c][j] & (STBV_PLAYBACK_LEFT | STBV_PLAYBACK_RIGHT);
5112 if (m == (STBV_PLAYBACK_LEFT | STBV_PLAYBACK_RIGHT)) {
5113 for (i=0; i < n; ++i) {
5114 buffer[i*2+0] += data[j][d_offset+o+i];
5115 buffer[i*2+1] += data[j][d_offset+o+i];
5117 }
else if (m == STBV_PLAYBACK_LEFT) {
5118 for (i=0; i < n; ++i) {
5119 buffer[i*2+0] += data[j][d_offset+o+i];
5121 }
else if (m == STBV_PLAYBACK_RIGHT) {
5122 for (i=0; i < n; ++i) {
5123 buffer[i*2+1] += data[j][d_offset+o+i];
5127 for (i=0; i < (n<<1); ++i) {
5129 int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
5130 if ((
unsigned int) (v + 32768) > 65535)
5131 v = v < 0 ? -32768 : 32767;
5137static void stbv_convert_samples_short(
int buf_c,
short **buffer,
int b_offset,
int data_c,
float **data,
int d_offset,
int samples)
5140 if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5141 static int channel_selector[3][2] = { {0}, {STBV_PLAYBACK_MONO}, {STBV_PLAYBACK_LEFT, STBV_PLAYBACK_RIGHT} };
5142 for (i=0; i < buf_c; ++i)
5143 stbv_compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples);
5145 int limit = buf_c < data_c ? buf_c : data_c;
5146 for (i=0; i < limit; ++i)
5147 stbv_copy_samples(buffer[i]+b_offset, data[i]+d_offset, samples);
5148 for ( ; i < buf_c; ++i)
5149 memset(buffer[i]+b_offset, 0,
sizeof(
short) * samples);
5157 if (len > num_samples) len = num_samples;
5159 stbv_convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len);
5163static void stbv_convert_channels_short_interleaved(
int buf_c,
short *buffer,
int data_c,
float **data,
int d_offset,
int len)
5166 stbv_check_endianness();
5167 if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
5169 for (i=0; i < buf_c; ++i)
5170 stbv_compute_stereo_samples(buffer, data_c, data, d_offset, len);
5172 int limit = buf_c < data_c ? buf_c : data_c;
5174 for (j=0; j < len; ++j) {
5175 for (i=0; i < limit; ++i) {
5177 float f = data[i][d_offset+j];
5178 int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp, f,15);
5179 if ((
unsigned int) (v + 32768) > 65535)
5180 v = v < 0 ? -32768 : 32767;
5183 for ( ; i < buf_c; ++i)
5196 if (len*num_c > num_shorts) len = num_shorts / num_c;
5197 stbv_convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len);
5205 int len = num_shorts / channels;
5207 int z = f->channels;
5208 if (z > channels) z = channels;
5210 int k = f->channel_buffer_end - f->channel_buffer_start;
5211 if (n+k >= len) k = len - n;
5213 stbv_convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k);
5214 buffer += k*channels;
5216 f->channel_buffer_start += k;
5217 if (n == len)
break;
5227 int z = f->channels;
5228 if (z > channels) z = channels;
5230 int k = f->channel_buffer_end - f->channel_buffer_start;
5231 if (n+k >= len) k = len - n;
5233 stbv_convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k);
5235 f->channel_buffer_start += k;
5236 if (n == len)
break;
5242#ifndef STB_VORBIS_NO_STDIO
5245 int data_len, offset, total, limit, error;
5248 if (v ==
NULL)
return -1;
5249 limit = v->channels * 4096;
5250 *channels = v->channels;
5252 *sample_rate = v->sample_rate;
5253 offset = data_len = 0;
5255 data = (
short *) malloc(total *
sizeof(*data));
5264 offset += n * v->channels;
5265 if (offset + limit > total) {
5268 data2 = (
short *) realloc(data, total *
sizeof(*data));
5269 if (data2 ==
NULL) {
5285 int data_len, offset, total, limit, error;
5288 if (v ==
NULL)
return -1;
5289 limit = v->channels * 4096;
5290 *channels = v->channels;
5292 *sample_rate = v->sample_rate;
5293 offset = data_len = 0;
5295 data = (
short *) malloc(total *
sizeof(*data));
5304 offset += n * v->channels;
5305 if (offset + limit > total) {
5308 data2 = (
short *) realloc(data, total *
sizeof(*data));
5309 if (data2 ==
NULL) {
5326 int len = num_floats / channels;
5328 int z = f->channels;
5329 if (z > channels) z = channels;
5332 int k = f->channel_buffer_end - f->channel_buffer_start;
5333 if (n+k >= len) k = len - n;
5334 for (j=0; j < k; ++j) {
5335 for (i=0; i < z; ++i)
5336 *buffer++ = f->channel_buffers[i][f->channel_buffer_start+j];
5337 for ( ; i < channels; ++i)
5341 f->channel_buffer_start += k;
5354 int z = f->channels;
5355 if (z > channels) z = channels;
5356 while (n < num_samples) {
5358 int k = f->channel_buffer_end - f->channel_buffer_start;
5359 if (n+k >= num_samples) k = num_samples - n;
5361 for (i=0; i < z; ++i)
5362 memcpy(buffer[i]+n, f->channel_buffers[i]+f->channel_buffer_start,
sizeof(
float)*k);
5363 for ( ; i < channels; ++i)
5364 memset(buffer[i]+n, 0,
sizeof(
float) * k);
5367 f->channel_buffer_start += k;
5368 if (n == num_samples)
STBVDEF stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc_buffer)
STBVDEF int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts)
@ VORBIS_seek_without_length
@ VORBIS_invalid_first_page
@ VORBIS_incorrect_stream_serial_number
@ VORBIS_missing_capture_pattern
@ VORBIS_continued_packet_flag_invalid
@ VORBIS_cant_find_last_page
@ VORBIS_too_many_channels
@ VORBIS_feature_not_supported
@ VORBIS_invalid_api_mixing
@ VORBIS_file_open_failure
@ VORBIS_invalid_stream_structure_version
STBVDEF void stb_vorbis_close(stb_vorbis *f)
STBVDEF int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples)
STBVDEF int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats)
STBVDEF int stb_vorbis_decode_frame_pushdata(stb_vorbis *f, const unsigned char *datablock, int datablock_length_in_bytes, int *channels, float ***output, int *samples)
STBVDEF int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output)
STBVDEF int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples)
STBVDEF unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
STBVDEF void stb_vorbis_flush_pushdata(stb_vorbis *f)
STBVDEF stb_vorbis * stb_vorbis_open_pushdata(const unsigned char *datablock, int datablock_length_in_bytes, int *datablock_memory_consumed_in_bytes, int *error, const stb_vorbis_alloc *alloc_buffer)
STBVDEF int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
STBVDEF int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts)
STBVDEF int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output)
STBVDEF float stb_vorbis_stream_length_in_seconds(stb_vorbis *f)
STBVDEF int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples)
struct stb_vorbis stb_vorbis
STBVDEF stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len)
STBVDEF unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
STBVDEF int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
STBVDEF stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close, int *error, const stb_vorbis_alloc *alloc_buffer)
STBVDEF int stb_vorbis_get_error(stb_vorbis *f)
STBVDEF int stb_vorbis_get_sample_offset(stb_vorbis *f)
STBVDEF int stb_vorbis_seek_start(stb_vorbis *f)
STBVDEF stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
STBVDEF int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output)
STBVDEF stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc_buffer)
int alloc_buffer_length_in_bytes
unsigned int temp_memory_required
unsigned int setup_memory_required
unsigned int setup_temp_memory_required