Improve Wwise Vorbis bitreader performance

This commit is contained in:
bnnm 2020-07-30 20:17:14 +02:00
parent 3bce06ea61
commit 2e9d35b207
5 changed files with 322 additions and 196 deletions

View File

@ -0,0 +1,128 @@
#ifndef _VORBIS_BITREADER_H
#define _VORBIS_BITREADER_H
/* Simple bitreader for Vorbis' bit format.
* Kept in .h since it's slightly faster (compiler can optimize statics better) */
typedef struct {
uint8_t* buf; /* buffer to read/write */
size_t bufsize; /* max size of the buffer */
uint32_t b_off; /* current offset in bits inside the buffer */
} bitstream_t;
/* same as (1 << bits) - 1, but that seems to trigger some nasty UB when bits = 32
* (though in theory (1 << 32) = 0, 0 - 1 = UINT_MAX, but gives 0 compiling in some cases, but not always) */
static const uint32_t MASK_TABLE[33] = {
0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 0x0001ffff,
0x0003ffff, 0x0007ffff, 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff, 0x01ffffff, 0x03ffffff,
0x07ffffff, 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff
};
/* Read bits (max 32) from buf and update the bit offset. Vorbis packs values in LSB order and byte by byte.
* (ex. from 2 bytes 00100111 00000001 we can could read 4b=0111 and 6b=010010, 6b=remainder (second value is split into the 2nd byte) */
static int rv_bits(bitstream_t* ib, uint32_t bits, uint32_t* value) {
uint32_t shift, mask, pos, val;
if (bits > 32 || ib->b_off + bits > ib->bufsize * 8)
goto fail;
pos = ib->b_off / 8; /* byte offset */
shift = ib->b_off % 8; /* bit sub-offset */
mask = MASK_TABLE[bits]; /* to remove upper in highest byte */
val = 0;
val |= ib->buf[pos+0] >> shift;
if (bits + shift > 8) {
val |= ib->buf[pos+1] << (8u - shift);
if (bits + shift > 16) {
val |= ib->buf[pos+2] << (16u - shift);
if (bits + shift > 24) {
val |= ib->buf[pos+3] << (24u - shift);
if (bits + shift > 32) {
/* upper bits are lost (shifting over 32) */
val |= ib->buf[pos+4] << (32u - shift);
}
}
}
}
*value = (val & mask);
ib->b_off += bits;
return 1;
fail:
VGM_LOG("BITREADER: read fail\n");
*value = 0;
return 0;
}
/* Write bits (max 32) to buf and update the bit offset. Vorbis packs values in LSB order and byte by byte.
* (ex. writing 1101011010 from b_off 2 we get 01101011 00001101 (value split, and 11 in the first byte skipped)*/
static int wv_bits(bitstream_t* ob, uint32_t bits, uint32_t value) {
uint32_t shift, mask, pos;
if (bits > 32 || ob->b_off + bits > ob->bufsize*8)
goto fail;
pos = ob->b_off / 8; /* byte offset */
shift = ob->b_off % 8; /* bit sub-offset */
mask = (1 << shift) - 1; /* to keep lower bits in lowest byte */
ob->buf[pos+0] = (value << shift) | (ob->buf[pos+0] & mask);
if (bits + shift > 8) {
ob->buf[pos+1] = value >> (8 - shift);
if (bits + shift > 16) {
ob->buf[pos+2] = value >> (16 - shift);
if (bits + shift > 24) {
ob->buf[pos+3] = value >> (24 - shift);
if (bits + shift > 32) {
/* upper bits are set to 0 (shifting unsigned) but shouldn't matter */
ob->buf[pos+4] = value >> (32 - shift);
}
}
}
}
ob->b_off += bits;
return 1;
fail:
VGM_LOG("BITREADER: write fail\n");
return 0;
}
static int copy_bytes(bitstream_t* ob, bitstream_t* ib, uint32_t bytes) {
int i;
/* copy packet bytes, where input/output bufs may not be byte-aligned (so non memcpy) */
for (i = 0; i < bytes / 4; i++) {
uint32_t c = 0;
rv_bits(ib, 32, &c);
wv_bits(ob, 32, c);
}
for (i = 0; i < bytes % 4; i++) {
uint32_t c = 0;
rv_bits(ib, 8, &c);
wv_bits(ob, 8, c);
}
#if 0
/* in theory the above is faster, but only seems minimally noticeable when converting lots of files */
for (i = 0; i < bytes; i++) {
uint32_t c = 0;
rv_bits(ib, 8, &c);
wv_bits(ob, 8, c);
}
#endif
return 1;
}
#endif

View File

@ -1,4 +1,5 @@
#include "vorbis_custom_decoder.h"
#include "vorbis_bitreader.h"
#ifdef VGM_USE_VORBIS
#include <vorbis/codec.h>
@ -19,8 +20,8 @@ static size_t get_packet_header(STREAMFILE* sf, off_t offset, wwise_header_t hea
static size_t rebuild_packet(uint8_t* obuf, size_t obufsize, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian);
static size_t rebuild_setup(uint8_t* obuf, size_t obufsize, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian, int channels);
static int ww2ogg_generate_vorbis_packet(vgm_bitstream* ow, vgm_bitstream* iw, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian);
static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vorbis_custom_codec_data* data, int channels, size_t packet_size, STREAMFILE* sf);
static int ww2ogg_generate_vorbis_packet(bitstream_t* ow, bitstream_t* iw, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian);
static int ww2ogg_generate_vorbis_setup(bitstream_t* ow, bitstream_t* iw, vorbis_custom_codec_data* data, int channels, size_t packet_size, STREAMFILE* sf);
static int load_wvc(uint8_t* ibuf, size_t ibufsize, uint32_t codebook_id, wwise_setup_t setup_type, STREAMFILE* sf);
static int load_wvc_file(uint8_t* buf, size_t bufsize, uint32_t codebook_id, STREAMFILE* sf);
@ -144,7 +145,7 @@ static size_t get_packet_header(STREAMFILE* sf, off_t offset, wwise_header_t hea
/* Transforms a Wwise data packet into a real Vorbis one (depending on config) */
static size_t rebuild_packet(uint8_t* obuf, size_t obufsize, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian) {
vgm_bitstream ow, iw;
bitstream_t ow, iw;
int rc, granulepos;
size_t header_size, packet_size;
@ -163,12 +164,10 @@ static size_t rebuild_packet(uint8_t* obuf, size_t obufsize, STREAMFILE* sf, off
ow.buf = obuf;
ow.bufsize = obufsize;
ow.b_off = 0;
ow.mode = BITSTREAM_VORBIS;
iw.buf = ibuf;
iw.bufsize = ibufsize;
iw.b_off = 0;
iw.mode = BITSTREAM_VORBIS;
rc = ww2ogg_generate_vorbis_packet(&ow,&iw, sf,offset, data, big_endian);
if (!rc) goto fail;
@ -187,7 +186,7 @@ fail:
/* Transforms a Wwise setup packet into a real Vorbis one (depending on config). */
static size_t rebuild_setup(uint8_t* obuf, size_t obufsize, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian, int channels) {
vgm_bitstream ow, iw;
bitstream_t ow, iw;
int rc, granulepos;
size_t header_size, packet_size;
@ -207,12 +206,10 @@ static size_t rebuild_setup(uint8_t* obuf, size_t obufsize, STREAMFILE* sf, off_
ow.buf = obuf;
ow.bufsize = obufsize;
ow.b_off = 0;
ow.mode = BITSTREAM_VORBIS;
iw.buf = ibuf;
iw.bufsize = ibufsize;
iw.b_off = 0;
iw.mode = BITSTREAM_VORBIS;
rc = ww2ogg_generate_vorbis_setup(&ow,&iw, data, channels, packet_size, sf);
if (!rc) goto fail;
@ -278,8 +275,8 @@ static size_t build_header_comment(uint8_t* buf, size_t bufsize) {
/* Copy packet as-is or rebuild first byte if mod_packets is used.
* (ref: https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-720004.3) */
static int ww2ogg_generate_vorbis_packet(vgm_bitstream* ow, vgm_bitstream* iw, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian) {
int i,granule;
static int ww2ogg_generate_vorbis_packet(bitstream_t* ow, bitstream_t* iw, STREAMFILE* sf, off_t offset, vorbis_custom_codec_data* data, int big_endian) {
int granule;
size_t header_size, packet_size, data_size;
header_size = get_packet_header(sf,offset, data->config.header_type, &granule, &packet_size, big_endian);
@ -307,12 +304,12 @@ static int ww2ogg_generate_vorbis_packet(vgm_bitstream* ow, vgm_bitstream* iw, S
/* audio packet type */
packet_type = 0;
w_bits(ow, 1, packet_type);
wv_bits(ow, 1, packet_type);
/* collect this packet mode from the first byte */
r_bits(iw, data->mode_bits,&mode_number); /* max 6b */
w_bits(ow, data->mode_bits, mode_number);
r_bits(iw, 8-data->mode_bits,&remainder);
rv_bits(iw, data->mode_bits,&mode_number); /* max 6b */
wv_bits(ow, data->mode_bits, mode_number);
rv_bits(iw, 8-data->mode_bits,&remainder);
/* adjust window info */
if (data->mode_blockflag[mode_number]) {
@ -333,57 +330,51 @@ static int ww2ogg_generate_vorbis_packet(vgm_bitstream* ow, vgm_bitstream* iw, S
/* get next first byte to read next_mode_number */
uint32_t next_mode_number;
uint8_t nbuf[1];
vgm_bitstream nw;
bitstream_t nw;
nw.buf = nbuf;
nw.bufsize = 1;
nw.b_off = 0;
nw.mode = BITSTREAM_VORBIS;
if (read_streamfile(nw.buf, next_offset + next_header_size, nw.bufsize, sf) != nw.bufsize)
goto fail;
r_bits(&nw, data->mode_bits,&next_mode_number); /* max 6b */
rv_bits(&nw, data->mode_bits,&next_mode_number); /* max 6b */
next_blockflag = data->mode_blockflag[next_mode_number];
}
}
prev_window_type = data->prev_blockflag;
w_bits(ow, 1, prev_window_type);
wv_bits(ow, 1, prev_window_type);
next_window_type = next_blockflag;
w_bits(ow, 1, next_window_type);
wv_bits(ow, 1, next_window_type);
}
data->prev_blockflag = data->mode_blockflag[mode_number]; /* save for next packet */
w_bits(ow, 8-data->mode_bits, remainder); /* this *isn't* byte aligned (ex. could be 10 bits written) */
wv_bits(ow, 8-data->mode_bits, remainder);
/* rest of the packet (input/output bytes aren't byte aligned here, so no memcpy) */
copy_bytes(ow, iw, packet_size - 1);
/* remove trailing garbage bits (probably unneeded) */
if (ow->b_off % 8 != 0) {
uint32_t padding = 0;
int padding_bits = 8 - (ow->b_off % 8);
wv_bits(ow, padding_bits, padding);
}
}
else {
/* normal packets: first byte unchanged */
uint32_t c = 0;
/* normal packets */
r_bits(iw, 8, &c);
w_bits(ow, 8, c);
}
/* remainder of packet (not byte-aligned when using mod_packets) */
for (i = 1; i < packet_size; i++) {
uint32_t c = 0;
r_bits(iw, 8, &c);
w_bits(ow, 8, c);
}
/* remove trailing garbage bits */
if (ow->b_off % 8 != 0) {
uint32_t padding = 0;
int padding_bits = 8 - (ow->b_off % 8);
w_bits(ow, padding_bits, padding);
/* can directly copy here (much, much faster), but least common case vs the above... */
memcpy(ow->buf + ow->b_off / 8, iw->buf + iw->b_off / 8, packet_size);
ow->b_off += packet_size * 8;
iw->b_off += packet_size * 8;
}
@ -432,17 +423,17 @@ static unsigned int ww2ogg_tremor_book_maptype1_quantvals(unsigned int entries,
/* copies Vorbis codebooks (untouched, but size uncertain) */
static int ww2ogg_codebook_library_copy(vgm_bitstream* ow, vgm_bitstream* iw) {
static int ww2ogg_codebook_library_copy(bitstream_t* ow, bitstream_t* iw) {
int i;
uint32_t id = 0, dimensions = 0, entries = 0;
uint32_t ordered = 0, lookup_type = 0;
r_bits(iw, 24,&id);
w_bits(ow, 24, id);
r_bits(iw, 16,&dimensions);
w_bits(ow, 16, dimensions);
r_bits(iw, 24,&entries);
w_bits(ow, 24, entries);
rv_bits(iw, 24,&id);
wv_bits(ow, 24, id);
rv_bits(iw, 16,&dimensions);
wv_bits(ow, 16, dimensions);
rv_bits(iw, 24,&entries);
wv_bits(ow, 24, entries);
if (0x564342 != id) { /* "VCB" */
VGM_LOG("Wwise Vorbis: invalid codebook identifier\n");
@ -450,21 +441,21 @@ static int ww2ogg_codebook_library_copy(vgm_bitstream* ow, vgm_bitstream* iw) {
}
/* codeword lengths */
r_bits(iw, 1,&ordered);
w_bits(ow, 1, ordered);
rv_bits(iw, 1,&ordered);
wv_bits(ow, 1, ordered);
if (ordered) {
uint32_t initial_length = 0, current_entry = 0;
r_bits(iw, 5,&initial_length);
w_bits(ow, 5, initial_length);
rv_bits(iw, 5,&initial_length);
wv_bits(ow, 5, initial_length);
current_entry = 0;
while (current_entry < entries) {
uint32_t number = 0;
int number_bits = ww2ogg_tremor_ilog(entries-current_entry);
int numberv_bits = ww2ogg_tremor_ilog(entries-current_entry);
r_bits(iw, number_bits,&number);
w_bits(ow, number_bits, number);
rv_bits(iw, numberv_bits,&number);
wv_bits(ow, numberv_bits, number);
current_entry += number;
}
if (current_entry > entries) {
@ -475,8 +466,8 @@ static int ww2ogg_codebook_library_copy(vgm_bitstream* ow, vgm_bitstream* iw) {
else {
uint32_t sparse = 0;
r_bits(iw, 1,&sparse);
w_bits(ow, 1, sparse);
rv_bits(iw, 1,&sparse);
wv_bits(ow, 1, sparse);
for (i = 0; i < entries; i++) {
uint32_t present_bool = 0;
@ -485,8 +476,8 @@ static int ww2ogg_codebook_library_copy(vgm_bitstream* ow, vgm_bitstream* iw) {
if (sparse) {
uint32_t present = 0;
r_bits(iw, 1,&present);
w_bits(ow, 1, present);
rv_bits(iw, 1,&present);
wv_bits(ow, 1, present);
present_bool = (0 != present);
}
@ -494,16 +485,16 @@ static int ww2ogg_codebook_library_copy(vgm_bitstream* ow, vgm_bitstream* iw) {
if (present_bool) {
uint32_t codeword_length = 0;
r_bits(iw, 5,&codeword_length);
w_bits(ow, 5, codeword_length);
rv_bits(iw, 5,&codeword_length);
wv_bits(ow, 5, codeword_length);
}
}
}
/* lookup table */
r_bits(iw, 4,&lookup_type);
w_bits(ow, 4, lookup_type);
rv_bits(iw, 4,&lookup_type);
wv_bits(ow, 4, lookup_type);
if (0 == lookup_type) {
//VGM_LOG("Wwise Vorbis: no lookup table\n");
@ -513,22 +504,22 @@ static int ww2ogg_codebook_library_copy(vgm_bitstream* ow, vgm_bitstream* iw) {
uint32_t quantvals = 0, min = 0, max = 0;
uint32_t value_length = 0, sequence_flag = 0;
r_bits(iw, 32,&min);
w_bits(ow, 32, min);
r_bits(iw, 32,&max);
w_bits(ow, 32, max);
r_bits(iw, 4,&value_length);
w_bits(ow, 4, value_length);
r_bits(iw, 1,&sequence_flag);
w_bits(ow, 1, sequence_flag);
rv_bits(iw, 32,&min);
wv_bits(ow, 32, min);
rv_bits(iw, 32,&max);
wv_bits(ow, 32, max);
rv_bits(iw, 4,&value_length);
wv_bits(ow, 4, value_length);
rv_bits(iw, 1,&sequence_flag);
wv_bits(ow, 1, sequence_flag);
quantvals = ww2ogg_tremor_book_maptype1_quantvals(entries, dimensions);
for (i = 0; i < quantvals; i++) {
uint32_t val = 0, val_bits = 0;
val_bits = value_length+1;
r_bits(iw, val_bits,&val);
w_bits(ow, val_bits, val);
rv_bits(iw, val_bits,&val);
wv_bits(ow, val_bits, val);
}
}
else if (2 == lookup_type) {
@ -546,35 +537,35 @@ fail:
}
/* rebuilds a Wwise codebook into a Vorbis codebook */
static int ww2ogg_codebook_library_rebuild(vgm_bitstream* ow, vgm_bitstream* iw, size_t cb_size, STREAMFILE* sf) {
static int ww2ogg_codebook_library_rebuild(bitstream_t* ow, bitstream_t* iw, size_t cb_size, STREAMFILE* sf) {
int i;
uint32_t id = 0, dimensions = 0, entries = 0;
uint32_t ordered = 0, lookup_type = 0;
id = 0x564342; /* "VCB" */
w_bits(ow, 24, id);
r_bits(iw, 4,&dimensions);
w_bits(ow, 16, dimensions); /* 4 to 16 */
r_bits(iw, 14,&entries);
w_bits(ow, 24, entries); /* 14 to 24*/
wv_bits(ow, 24, id);
rv_bits(iw, 4,&dimensions);
wv_bits(ow, 16, dimensions); /* 4 to 16 */
rv_bits(iw, 14,&entries);
wv_bits(ow, 24, entries); /* 14 to 24*/
/* codeword lengths */
r_bits(iw, 1,&ordered);
w_bits(ow, 1, ordered);
rv_bits(iw, 1,&ordered);
wv_bits(ow, 1, ordered);
if (ordered) {
uint32_t initial_length = 0, current_entry = 0;
r_bits(iw, 5,&initial_length);
w_bits(ow, 5, initial_length);
rv_bits(iw, 5,&initial_length);
wv_bits(ow, 5, initial_length);
current_entry = 0;
while (current_entry < entries) {
uint32_t number = 0;
int number_bits = ww2ogg_tremor_ilog(entries-current_entry);
int numberv_bits = ww2ogg_tremor_ilog(entries-current_entry);
r_bits(iw, number_bits,&number);
w_bits(ow, number_bits, number);
rv_bits(iw, numberv_bits,&number);
wv_bits(ow, numberv_bits, number);
current_entry += number;
}
if (current_entry > entries) {
@ -585,9 +576,9 @@ static int ww2ogg_codebook_library_rebuild(vgm_bitstream* ow, vgm_bitstream* iw,
else {
uint32_t codeword_length_length = 0, sparse = 0;
r_bits(iw, 3,&codeword_length_length);
r_bits(iw, 1,&sparse);
w_bits(ow, 1, sparse);
rv_bits(iw, 3,&codeword_length_length);
rv_bits(iw, 1,&sparse);
wv_bits(ow, 1, sparse);
if (0 == codeword_length_length || 5 < codeword_length_length) {
VGM_LOG("Wwise Vorbis: nonsense codeword length\n");
@ -601,8 +592,8 @@ static int ww2ogg_codebook_library_rebuild(vgm_bitstream* ow, vgm_bitstream* iw,
if (sparse) {
uint32_t present = 0;
r_bits(iw, 1,&present);
w_bits(ow, 1, present);
rv_bits(iw, 1,&present);
wv_bits(ow, 1, present);
present_bool = (0 != present);
}
@ -610,16 +601,16 @@ static int ww2ogg_codebook_library_rebuild(vgm_bitstream* ow, vgm_bitstream* iw,
if (present_bool) {
uint32_t codeword_length = 0;
r_bits(iw, codeword_length_length,&codeword_length);
w_bits(ow, 5, codeword_length); /* max 7 (3b) to 5 */
rv_bits(iw, codeword_length_length,&codeword_length);
wv_bits(ow, 5, codeword_length); /* max 7 (3b) to 5 */
}
}
}
/* lookup table */
r_bits(iw, 1,&lookup_type);
w_bits(ow, 4, lookup_type); /* 1 to 4 */
rv_bits(iw, 1,&lookup_type);
wv_bits(ow, 4, lookup_type); /* 1 to 4 */
if (0 == lookup_type) {
//VGM_LOG("Wwise Vorbis: no lookup table\n");
@ -629,22 +620,22 @@ static int ww2ogg_codebook_library_rebuild(vgm_bitstream* ow, vgm_bitstream* iw,
uint32_t quantvals = 0, min = 0, max = 0;
uint32_t value_length = 0, sequence_flag = 0;
r_bits(iw, 32,&min);
w_bits(ow, 32, min);
r_bits(iw, 32,&max);
w_bits(ow, 32, max);
r_bits(iw, 4,&value_length);
w_bits(ow, 4, value_length);
r_bits(iw, 1,&sequence_flag);
w_bits(ow, 1, sequence_flag);
rv_bits(iw, 32,&min);
wv_bits(ow, 32, min);
rv_bits(iw, 32,&max);
wv_bits(ow, 32, max);
rv_bits(iw, 4,&value_length);
wv_bits(ow, 4, value_length);
rv_bits(iw, 1,&sequence_flag);
wv_bits(ow, 1, sequence_flag);
quantvals = ww2ogg_tremor_book_maptype1_quantvals(entries, dimensions);
for (i = 0; i < quantvals; i++) {
uint32_t val = 0, val_bits = 0;
val_bits = value_length+1;
r_bits(iw, val_bits,&val);
w_bits(ow, val_bits, val);
rv_bits(iw, val_bits,&val);
wv_bits(ow, val_bits, val);
}
}
else if (2 == lookup_type) {
@ -670,11 +661,11 @@ fail:
}
/* rebuilds an external Wwise codebook referenced by id to a Vorbis codebook */
static int ww2ogg_codebook_library_rebuild_by_id(vgm_bitstream* ow, uint32_t codebook_id, wwise_setup_t setup_type, STREAMFILE* sf) {
static int ww2ogg_codebook_library_rebuild_by_id(bitstream_t* ow, uint32_t codebook_id, wwise_setup_t setup_type, STREAMFILE* sf) {
size_t ibufsize = 0x8000; /* arbitrary max size of a codebook */
uint8_t ibuf[0x8000]; /* Wwise codebook buffer */
size_t cb_size;
vgm_bitstream iw;
bitstream_t iw;
cb_size = load_wvc(ibuf,ibufsize, codebook_id, setup_type, sf);
if (cb_size == 0) goto fail;
@ -682,7 +673,6 @@ static int ww2ogg_codebook_library_rebuild_by_id(vgm_bitstream* ow, uint32_t cod
iw.buf = ibuf;
iw.bufsize = ibufsize;
iw.b_off = 0;
iw.mode = BITSTREAM_VORBIS;
return ww2ogg_codebook_library_rebuild(ow, &iw, cb_size, sf);
fail:
@ -691,7 +681,7 @@ fail:
/* Rebuild a Wwise setup (simplified with removed stuff), recreating all six setup parts.
* (ref: https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-650004.2.4) */
static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vorbis_custom_codec_data* data, int channels, size_t packet_size, STREAMFILE* sf) {
static int ww2ogg_generate_vorbis_setup(bitstream_t* ow, bitstream_t* iw, vorbis_custom_codec_data* data, int channels, size_t packet_size, STREAMFILE* sf) {
int i,j,k;
uint32_t codebook_count = 0, floor_count = 0, residue_count = 0;
uint32_t codebook_count_less1 = 0;
@ -705,8 +695,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
/* Codebooks */
r_bits(iw, 8,&codebook_count_less1);
w_bits(ow, 8, codebook_count_less1);
rv_bits(iw, 8,&codebook_count_less1);
wv_bits(ow, 8, codebook_count_less1);
codebook_count = codebook_count_less1 + 1;
if (data->config.setup_type == WWV_FULL_SETUP) {
@ -729,7 +719,7 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
int rc;
uint32_t codebook_id = 0;
r_bits(iw, 10,&codebook_id);
rv_bits(iw, 10,&codebook_id);
rc = ww2ogg_codebook_library_rebuild_by_id(ow, codebook_id, data->config.setup_type, sf);
if (!rc) goto fail;
@ -739,9 +729,9 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
/* Time domain transforms */
time_count_less1 = 0;
w_bits(ow, 6, time_count_less1);
wv_bits(ow, 6, time_count_less1);
dummy_time_value = 0;
w_bits(ow, 16, dummy_time_value);
wv_bits(ow, 16, dummy_time_value);
if (data->config.setup_type == WWV_FULL_SETUP) {
@ -751,8 +741,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
uint32_t setup_packet_size_bits = packet_size*8;
while (total_bits_read < setup_packet_size_bits) {
r_bits(iw, 1,&bitly);
w_bits(ow, 1, bitly);
rv_bits(iw, 1,&bitly);
wv_bits(ow, 1, bitly);
total_bits_read = iw->b_off;
}
}
@ -765,8 +755,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
/* Floors */
r_bits(iw, 6,&floor_count_less1);
w_bits(ow, 6, floor_count_less1);
rv_bits(iw, 6,&floor_count_less1);
wv_bits(ow, 6, floor_count_less1);
floor_count = floor_count_less1 + 1;
for (i = 0; i < floor_count; i++) {
@ -777,10 +767,10 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
// Always floor type 1
floor_type = 1;
w_bits(ow, 16, floor_type);
wv_bits(ow, 16, floor_type);
r_bits(iw, 5,&floor1_partitions);
w_bits(ow, 5, floor1_partitions);
rv_bits(iw, 5,&floor1_partitions);
wv_bits(ow, 5, floor1_partitions);
memset(floor1_partition_class_list, 0, sizeof(uint32_t)*32);
@ -788,8 +778,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
for (j = 0; j < floor1_partitions; j++) {
uint32_t floor1_partition_class = 0;
r_bits(iw, 4,&floor1_partition_class);
w_bits(ow, 4, floor1_partition_class);
rv_bits(iw, 4,&floor1_partition_class);
wv_bits(ow, 4, floor1_partition_class);
floor1_partition_class_list[j] = floor1_partition_class;
@ -802,19 +792,19 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
for (j = 0; j <= maximum_class; j++) {
uint32_t class_dimensions_less1 = 0, class_subclasses = 0;
r_bits(iw, 3,&class_dimensions_less1);
w_bits(ow, 3, class_dimensions_less1);
rv_bits(iw, 3,&class_dimensions_less1);
wv_bits(ow, 3, class_dimensions_less1);
floor1_class_dimensions_list[j] = class_dimensions_less1 + 1;
r_bits(iw, 2,&class_subclasses);
w_bits(ow, 2, class_subclasses);
rv_bits(iw, 2,&class_subclasses);
wv_bits(ow, 2, class_subclasses);
if (0 != class_subclasses) {
uint32_t masterbook = 0;
r_bits(iw, 8,&masterbook);
w_bits(ow, 8, masterbook);
rv_bits(iw, 8,&masterbook);
wv_bits(ow, 8, masterbook);
if (masterbook >= codebook_count) {
VGM_LOG("Wwise Vorbis: invalid floor1 masterbook\n");
@ -826,8 +816,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
uint32_t subclass_book_plus1 = 0;
int subclass_book = 0; /* this MUST be int */
r_bits(iw, 8,&subclass_book_plus1);
w_bits(ow, 8, subclass_book_plus1);
rv_bits(iw, 8,&subclass_book_plus1);
wv_bits(ow, 8, subclass_book_plus1);
subclass_book = subclass_book_plus1 - 1;
if (subclass_book >= 0 && subclass_book >= codebook_count) {
@ -837,11 +827,11 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
}
}
r_bits(iw, 2,&floor1_multiplier_less1);
w_bits(ow, 2, floor1_multiplier_less1);
rv_bits(iw, 2,&floor1_multiplier_less1);
wv_bits(ow, 2, floor1_multiplier_less1);
r_bits(iw, 4,&rangebits);
w_bits(ow, 4, rangebits);
rv_bits(iw, 4,&rangebits);
wv_bits(ow, 4, rangebits);
for (j = 0; j < floor1_partitions; j++) {
uint32_t current_class_number = 0;
@ -850,16 +840,16 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
for (k = 0; k < floor1_class_dimensions_list[current_class_number]; k++) {
uint32_t X = 0; /* max 4b (15) */
r_bits(iw, rangebits,&X);
w_bits(ow, rangebits, X);
rv_bits(iw, rangebits,&X);
wv_bits(ow, rangebits, X);
}
}
}
/* Residues */
r_bits(iw, 6,&residue_count_less1);
w_bits(ow, 6, residue_count_less1);
rv_bits(iw, 6,&residue_count_less1);
wv_bits(ow, 6, residue_count_less1);
residue_count = residue_count_less1 + 1;
for (i = 0; i < residue_count; i++) {
@ -867,24 +857,24 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
uint32_t residue_begin = 0, residue_end = 0, residue_partition_size_less1 = 0, residue_classifications_less1 = 0, residue_classbook = 0;
uint32_t residue_cascade[64+1]; /* 6b +1 */
r_bits(iw, 2,&residue_type);
w_bits(ow, 16, residue_type); /* 2b to 16b */
rv_bits(iw, 2,&residue_type);
wv_bits(ow, 16, residue_type); /* 2b to 16b */
if (residue_type > 2) {
VGM_LOG("Wwise Vorbis: invalid residue type\n");
goto fail;
}
r_bits(iw, 24,&residue_begin);
w_bits(ow, 24, residue_begin);
r_bits(iw, 24,&residue_end);
w_bits(ow, 24, residue_end);
r_bits(iw, 24,&residue_partition_size_less1);
w_bits(ow, 24, residue_partition_size_less1);
r_bits(iw, 6,&residue_classifications_less1);
w_bits(ow, 6, residue_classifications_less1);
r_bits(iw, 8,&residue_classbook);
w_bits(ow, 8, residue_classbook);
rv_bits(iw, 24,&residue_begin);
wv_bits(ow, 24, residue_begin);
rv_bits(iw, 24,&residue_end);
wv_bits(ow, 24, residue_end);
rv_bits(iw, 24,&residue_partition_size_less1);
wv_bits(ow, 24, residue_partition_size_less1);
rv_bits(iw, 6,&residue_classifications_less1);
wv_bits(ow, 6, residue_classifications_less1);
rv_bits(iw, 8,&residue_classbook);
wv_bits(ow, 8, residue_classbook);
residue_classifications = residue_classifications_less1 + 1;
if (residue_classbook >= codebook_count) {
@ -895,21 +885,21 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
memset(residue_cascade, 0, sizeof(uint32_t)*(64+1));
for (j = 0; j < residue_classifications; j++) {
uint32_t high_bits = 0, low_bits = 0, bitflag = 0;
uint32_t high_bits = 0, lowv_bits = 0, bitflag = 0;
high_bits = 0;
r_bits(iw, 3,&low_bits);
w_bits(ow, 3, low_bits);
rv_bits(iw, 3,&lowv_bits);
wv_bits(ow, 3, lowv_bits);
r_bits(iw, 1,&bitflag);
w_bits(ow, 1, bitflag);
rv_bits(iw, 1,&bitflag);
wv_bits(ow, 1, bitflag);
if (bitflag) {
r_bits(iw, 5,&high_bits);
w_bits(ow, 5, high_bits);
rv_bits(iw, 5,&high_bits);
wv_bits(ow, 5, high_bits);
}
residue_cascade[j] = high_bits * 8 + low_bits;
residue_cascade[j] = high_bits * 8 + lowv_bits;
}
for (j = 0; j < residue_classifications; j++) {
@ -917,8 +907,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
if (residue_cascade[j] & (1 << k)) {
uint32_t residue_book = 0;
r_bits(iw, 8,&residue_book);
w_bits(ow, 8, residue_book);
rv_bits(iw, 8,&residue_book);
wv_bits(ow, 8, residue_book);
if (residue_book >= codebook_count) {
VGM_LOG("Wwise Vorbis: invalid residue book\n");
@ -931,8 +921,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
/* Mappings */
r_bits(iw, 6,&mapping_count_less1);
w_bits(ow, 6, mapping_count_less1);
rv_bits(iw, 6,&mapping_count_less1);
wv_bits(ow, 6, mapping_count_less1);
mapping_count = mapping_count_less1 + 1;
for (i = 0; i < mapping_count; i++) {
@ -941,28 +931,28 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
// always mapping type 0, the only one
mapping_type = 0;
w_bits(ow, 16, mapping_type);
wv_bits(ow, 16, mapping_type);
r_bits(iw, 1,&submaps_flag);
w_bits(ow, 1, submaps_flag);
rv_bits(iw, 1,&submaps_flag);
wv_bits(ow, 1, submaps_flag);
submaps = 1;
if (submaps_flag) {
uint32_t submaps_less1 = 0;
r_bits(iw, 4,&submaps_less1);
w_bits(ow, 4, submaps_less1);
rv_bits(iw, 4,&submaps_less1);
wv_bits(ow, 4, submaps_less1);
submaps = submaps_less1 + 1;
}
r_bits(iw, 1,&square_polar_flag);
w_bits(ow, 1, square_polar_flag);
rv_bits(iw, 1,&square_polar_flag);
wv_bits(ow, 1, square_polar_flag);
if (square_polar_flag) {
uint32_t coupling_steps_less1 = 0, coupling_steps = 0;
r_bits(iw, 8,&coupling_steps_less1);
w_bits(ow, 8, coupling_steps_less1);
rv_bits(iw, 8,&coupling_steps_less1);
wv_bits(ow, 8, coupling_steps_less1);
coupling_steps = coupling_steps_less1 + 1;
for (j = 0; j < coupling_steps; j++) {
@ -970,10 +960,10 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
int magnitude_bits = ww2ogg_tremor_ilog(channels-1);
int angle_bits = ww2ogg_tremor_ilog(channels-1);
r_bits(iw, magnitude_bits,&magnitude);
w_bits(ow, magnitude_bits, magnitude);
r_bits(iw, angle_bits,&angle);
w_bits(ow, angle_bits, angle);
rv_bits(iw, magnitude_bits,&magnitude);
wv_bits(ow, magnitude_bits, magnitude);
rv_bits(iw, angle_bits,&angle);
wv_bits(ow, angle_bits, angle);
if (angle == magnitude || magnitude >= channels || angle >= channels) {
VGM_LOG("Wwise Vorbis: invalid coupling (angle=%i, mag=%i, ch=%i)\n", angle, magnitude,channels);
@ -983,8 +973,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
}
// a rare reserved field not removed by Ak!
r_bits(iw, 2,&mapping_reserved);
w_bits(ow, 2, mapping_reserved);
rv_bits(iw, 2,&mapping_reserved);
wv_bits(ow, 2, mapping_reserved);
if (0 != mapping_reserved) {
VGM_LOG("Wwise Vorbis: mapping reserved field nonzero\n");
goto fail;
@ -994,8 +984,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
for (j = 0; j < channels; j++) {
uint32_t mapping_mux = 0;
r_bits(iw, 4,&mapping_mux);
w_bits(ow, 4, mapping_mux);
rv_bits(iw, 4,&mapping_mux);
wv_bits(ow, 4, mapping_mux);
if (mapping_mux >= submaps) {
VGM_LOG("Wwise Vorbis: mapping_mux >= submaps\n");
goto fail;
@ -1007,18 +997,18 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
uint32_t time_config = 0, floor_number = 0, residue_number = 0;
// Another! Unused time domain transform configuration placeholder!
r_bits(iw, 8,&time_config);
w_bits(ow, 8, time_config);
rv_bits(iw, 8,&time_config);
wv_bits(ow, 8, time_config);
r_bits(iw, 8,&floor_number);
w_bits(ow, 8, floor_number);
rv_bits(iw, 8,&floor_number);
wv_bits(ow, 8, floor_number);
if (floor_number >= floor_count) {
VGM_LOG("Wwise Vorbis: invalid floor mapping\n");
goto fail;
}
r_bits(iw, 8,&residue_number);
w_bits(ow, 8, residue_number);
rv_bits(iw, 8,&residue_number);
wv_bits(ow, 8, residue_number);
if (residue_number >= residue_count) {
VGM_LOG("Wwise Vorbis: invalid residue mapping\n");
goto fail;
@ -1028,8 +1018,8 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
/* Modes */
r_bits(iw, 6,&mode_count_less1);
w_bits(ow, 6, mode_count_less1);
rv_bits(iw, 6,&mode_count_less1);
wv_bits(ow, 6, mode_count_less1);
mode_count = mode_count_less1 + 1;
memset(data->mode_blockflag, 0, sizeof(uint8_t)*(64+1)); /* up to max mode_count */
@ -1038,18 +1028,18 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
for (i = 0; i < mode_count; i++) {
uint32_t block_flag = 0, windowtype = 0, transformtype = 0, mapping = 0;
r_bits(iw, 1,&block_flag);
w_bits(ow, 1, block_flag);
rv_bits(iw, 1,&block_flag);
wv_bits(ow, 1, block_flag);
data->mode_blockflag[i] = (block_flag != 0); /* for mod_packets */
windowtype = 0;
transformtype = 0;
w_bits(ow, 16, windowtype);
w_bits(ow, 16, transformtype);
wv_bits(ow, 16, windowtype);
wv_bits(ow, 16, transformtype);
r_bits(iw, 8,&mapping);
w_bits(ow, 8, mapping);
rv_bits(iw, 8,&mapping);
wv_bits(ow, 8, mapping);
if (mapping >= mapping_count) {
VGM_LOG("Wwise Vorbis: invalid mode mapping\n");
goto fail;
@ -1063,7 +1053,7 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
uint32_t framing = 0;
framing = 1;
w_bits(ow, 1, framing);
wv_bits(ow, 1, framing);
}
/* remove trailing garbage bits */
@ -1071,7 +1061,7 @@ static int ww2ogg_generate_vorbis_setup(vgm_bitstream* ow, vgm_bitstream* iw, vo
uint32_t padding = 0;
int padding_bits = 8 - (ow->b_off % 8);
w_bits(ow, padding_bits, padding);
wv_bits(ow, padding_bits, padding);
}

View File

@ -2010,6 +2010,10 @@
RelativePath=".\coding\nwa_decoder.h"
>
</File>
<File
RelativePath=".\coding\vorbis_bitreader.h"
>
</File>
<File
RelativePath=".\coding\vorbis_custom_decoder.h"
>

View File

@ -85,6 +85,7 @@
<ItemGroup>
<ClInclude Include="..\ext_includes\clHCA.h" />
<ClInclude Include="coding\mpeg_decoder.h" />
<ClInclude Include="coding\vorbis_bitreader.h" />
<ClInclude Include="coding\vorbis_custom_data_fsb.h" />
<ClInclude Include="coding\vorbis_custom_data_wwise.h" />
<ClInclude Include="coding\vorbis_custom_decoder.h" />

View File

@ -245,6 +245,9 @@
<ClInclude Include="coding\mpeg_decoder.h">
<Filter>coding\Header Files</Filter>
</ClInclude>
<ClInclude Include="coding\vorbis_bitreader.h">
<Filter>coding\Header Files</Filter>
</ClInclude>
<ClInclude Include="coding\vorbis_custom_data_fsb.h">
<Filter>coding\Header Files</Filter>
</ClInclude>