vgmstream/src/meta/wwise.c

1041 lines
45 KiB
C
Raw Normal View History

2020-03-01 12:21:15 +01:00
#include "meta.h"
#include "../util.h"
#include "../coding/coding.h"
2021-09-19 23:54:38 +02:00
#include "../util/chunks.h"
2021-10-01 00:33:49 +02:00
#include "../util/endianness.h"
2020-03-01 12:21:15 +01:00
/* Wwise uses a custom RIFF/RIFX header, non-standard enough that it's parsed it here.
* There is some repetition from other metas, but not enough to bother me.
*
* Some info: https://www.audiokinetic.com/en/library/edge/
* .bnk (dynamic music/loop) info: https://github.com/bnnm/wwiser
2020-03-01 12:21:15 +01:00
*/
typedef enum { PCM, IMA, VORBIS, DSP, XMA2, XWMA, AAC, HEVAG, ATRAC9, OPUSNX, OPUS, OPUSCPR, OPUSWW, PTADPCM } wwise_codec;
2020-03-01 12:21:15 +01:00
typedef struct {
int big_endian;
size_t file_size;
2021-11-06 14:57:41 +01:00
int prefetch;
2021-09-19 23:54:38 +02:00
int is_wem;
2021-11-06 14:57:41 +01:00
int is_bnk;
2020-03-01 12:21:15 +01:00
/* chunks references */
2020-11-09 17:04:42 +01:00
off_t fmt_offset;
2020-03-01 12:21:15 +01:00
size_t fmt_size;
2020-11-09 17:04:42 +01:00
off_t data_offset;
2020-03-01 12:21:15 +01:00
size_t data_size;
2020-11-09 17:04:42 +01:00
off_t xma2_offset;
size_t xma2_size;
off_t vorb_offset;
size_t vorb_size;
off_t wiih_offset;
size_t wiih_size;
off_t smpl_offset;
size_t smpl_size;
off_t seek_offset;
size_t seek_size;
off_t meta_offset;
size_t meta_size;
2020-03-01 12:21:15 +01:00
/* standard fmt stuff */
wwise_codec codec;
int format;
int channels;
int sample_rate;
2021-09-11 13:11:29 +02:00
int block_size;
int avg_bitrate;
2020-03-01 12:21:15 +01:00
int bits_per_sample;
2020-11-22 19:00:55 +01:00
uint8_t channel_type;
2020-03-01 12:21:15 +01:00
uint32_t channel_layout;
size_t extra_size;
int32_t num_samples;
int loop_flag;
int32_t loop_start_sample;
int32_t loop_end_sample;
} wwise_header;
2020-11-09 17:04:42 +01:00
static int parse_wwise(STREAMFILE* sf, wwise_header* ww);
static int is_dsp_full_interleave(STREAMFILE* sf, wwise_header* ww, off_t coef_offset, int full_detection);
2021-09-19 23:54:38 +02:00
/* Wwise - Audiokinetic Wwise (WaveWorks Interactive Sound Engine) middleware */
2020-11-09 17:04:42 +01:00
VGMSTREAM* init_vgmstream_wwise(STREAMFILE* sf) {
2021-11-06 14:57:41 +01:00
return init_vgmstream_wwise_bnk(sf, NULL);
}
/* used in .bnk */
VGMSTREAM* init_vgmstream_wwise_bnk(STREAMFILE* sf, int* p_prefetch) {
2020-11-09 17:04:42 +01:00
VGMSTREAM* vgmstream = NULL;
2020-03-01 12:21:15 +01:00
wwise_header ww = {0};
2020-11-09 17:04:42 +01:00
off_t start_offset;
2021-09-26 19:34:12 +02:00
read_u32_t read_u32 = NULL;
read_s32_t read_s32 = NULL;
read_u16_t read_u16 = NULL;
2020-11-09 17:04:42 +01:00
2020-03-01 12:21:15 +01:00
/* checks */
2021-09-19 23:54:38 +02:00
if (!is_id32be(0x00,sf, "RIFF") && /* LE */
!is_id32be(0x00,sf, "RIFX")) /* BE */
goto fail;
/* note that Wwise allows those extensions only, so custom engine exts shouldn't be added
* .wem: newer "Wwise Encoded Media" used after the 2011.2 SDK (~july 2011)
2020-11-29 20:10:49 +01:00
* .wav: older PCM/ADPCM files [Spider-Man: Web of Shadows (PC), Punch Out!! (Wii)]
* .xma: older XMA files [Too Human (X360), Tron Evolution (X360)]
* .ogg: older Vorbis files [The King of Fighters XII (X360)]
2021-09-19 23:54:38 +02:00
* .bnk: Wwise banks for memory .wem detection (hack) */
if (!check_extensions(sf,"wem,wav,lwav,ogg,logg,xma,bnk"))
goto fail;
2020-03-01 12:21:15 +01:00
2021-11-06 14:57:41 +01:00
ww.is_bnk = (p_prefetch != NULL);
2020-11-09 17:04:42 +01:00
if (!parse_wwise(sf, &ww))
2020-03-01 12:21:15 +01:00
goto fail;
2021-11-06 14:57:41 +01:00
if (p_prefetch)
*p_prefetch = ww.prefetch;
2020-11-09 17:04:42 +01:00
read_u32 = ww.big_endian ? read_u32be : read_u32le;
read_s32 = ww.big_endian ? read_s32be : read_s32le;
read_u16 = ww.big_endian ? read_u16be : read_u16le;
2020-03-01 12:21:15 +01:00
start_offset = ww.data_offset;
/* build the VGMSTREAM */
2020-11-09 17:04:42 +01:00
vgmstream = allocate_vgmstream(ww.channels, ww.loop_flag);
2020-03-01 12:21:15 +01:00
if (!vgmstream) goto fail;
2020-11-09 17:04:42 +01:00
vgmstream->meta_type = meta_WWISE_RIFF;
2020-03-01 12:21:15 +01:00
vgmstream->sample_rate = ww.sample_rate;
vgmstream->loop_start_sample = ww.loop_start_sample;
vgmstream->loop_end_sample = ww.loop_end_sample;
vgmstream->channel_layout = ww.channel_layout;
2021-09-26 19:34:12 +02:00
vgmstream->stream_size = ww.data_size;
2020-03-01 12:21:15 +01:00
switch(ww.codec) {
case PCM: /* common */
/* normally riff.c has priority but it's needed when .wem is used */
/* old=0x10, 0x12=Army of Two: the 40th Day (PS3), new/Limbo (PC) */
if (ww.fmt_size != 0x10 && ww.fmt_size != 0x12 && ww.fmt_size != 0x18 && ww.fmt_size != 0x28) goto fail;
2020-03-01 12:21:15 +01:00
if (ww.bits_per_sample != 16) goto fail;
vgmstream->coding_type = (ww.big_endian ? coding_PCM16BE : coding_PCM16LE);
vgmstream->layout_type = ww.channels > 1 ? layout_interleave : layout_none;
vgmstream->interleave_block_size = 0x02;
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-03-01 12:21:59 +01:00
ww.data_size = ww.file_size - ww.data_offset;
}
2020-03-01 12:21:15 +01:00
vgmstream->num_samples = pcm_bytes_to_samples(ww.data_size, ww.channels, ww.bits_per_sample);
2021-11-06 14:57:41 +01:00
/* prefetch .bnk RIFFs that only have header and no data is possible [Metal Gear Solid V (PC)] */
if (ww.prefetch && !vgmstream->num_samples)
vgmstream->num_samples = 1; /* force something to avoid broken subsongs */
2020-03-01 12:21:15 +01:00
break;
case IMA: /* common */
/* slightly modified and mono-interleaved XBOX-IMA */
2020-03-01 12:21:15 +01:00
/* Wwise reuses common codec ids (ex. 0x0002 MSADPCM) for IMA so this parser should go AFTER riff.c avoid misdetection */
if (ww.fmt_size != 0x14 && ww.fmt_size != 0x28 && ww.fmt_size != 0x18) goto fail; /* oldest, old, new */
2020-03-01 12:21:15 +01:00
if (ww.bits_per_sample != 4) goto fail;
2021-09-11 13:11:29 +02:00
if (ww.block_size != 0x24 * ww.channels) goto fail;
2020-03-01 12:21:15 +01:00
vgmstream->coding_type = coding_WWISE_IMA;
vgmstream->layout_type = layout_interleave;
2021-09-11 13:11:29 +02:00
vgmstream->interleave_block_size = ww.block_size / ww.channels;
2020-03-01 12:21:15 +01:00
vgmstream->codec_endian = ww.big_endian;
/* oldest version uses regular XBOX IMA with stereo mode [Shadowrun (PC)] */
if (ww.fmt_size == 0x14 && ww.format == 0x0069) {
if (ww.channels > 2) goto fail; /* unlikely but just in case */
if (ww.big_endian) goto fail; /* unsure */
vgmstream->coding_type = coding_XBOX_IMA;
vgmstream->layout_type = layout_none;
vgmstream->interleave_block_size = 0;
}
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-03-01 12:21:15 +01:00
ww.data_size = ww.file_size - ww.data_offset;
}
vgmstream->num_samples = xbox_ima_bytes_to_samples(ww.data_size, ww.channels);
break;
#ifdef VGM_USE_VORBIS
2020-11-09 17:04:42 +01:00
case VORBIS: { /* common */
2020-03-01 12:21:15 +01:00
/* Wwise uses custom Vorbis, which changed over time (config must be detected to pass to the decoder). */
2020-11-09 17:04:42 +01:00
off_t data_offsets, block_offsets;
size_t setup_offset, audio_offset;
2020-03-01 12:21:15 +01:00
vorbis_custom_config cfg = {0};
cfg.channels = ww.channels;
cfg.sample_rate = ww.sample_rate;
cfg.big_endian = ww.big_endian;
2022-07-14 23:09:08 +02:00
cfg.stream_end = ww.data_offset + ww.data_size;
2020-03-01 12:21:15 +01:00
2021-09-11 13:11:29 +02:00
if (ww.block_size != 0 || ww.bits_per_sample != 0) goto fail; /* always 0 for Worbis */
2020-03-01 12:21:15 +01:00
/* autodetect format (fields are mostly common, see the end of the file) */
2020-11-09 17:04:42 +01:00
if (ww.vorb_offset) {
2020-03-01 12:21:15 +01:00
/* older Wwise (~<2012) */
2020-11-09 17:04:42 +01:00
switch(ww.vorb_size) {
case 0x2C: /* earliest (~2009) [The Lord of the Rings: Conquest (PC)] */
case 0x28: /* early (~2009) [UFC Undisputed 2009 (PS3), some EVE Online Apocrypha (PC)] */
2020-03-01 12:21:15 +01:00
data_offsets = 0x18;
block_offsets = 0; /* no need, full headers are present */
cfg.header_type = WWV_TYPE_8;
cfg.packet_type = WWV_STANDARD;
cfg.setup_type = WWV_HEADER_TRIAD;
break;
case 0x34: /* common (2010~2011) [The King of Fighters XII (PS3), Assassin's Creed II (X360)] */
case 0x32: /* rare (mid 2011) [Saints Row the 3rd (PC)] */
2020-03-01 12:21:15 +01:00
data_offsets = 0x18;
block_offsets = 0x30;
cfg.header_type = WWV_TYPE_6;
cfg.packet_type = WWV_STANDARD;
cfg.setup_type = WWV_EXTERNAL_CODEBOOKS; /* setup_type will be corrected later */
break;
case 0x2a: /* uncommon (mid 2011) [inFamous 2 (PS3), Captain America: Super Soldier (X360)] */
data_offsets = 0x10;
block_offsets = 0x28;
cfg.header_type = WWV_TYPE_2;
cfg.packet_type = WWV_MODIFIED;
cfg.setup_type = WWV_EXTERNAL_CODEBOOKS;
break;
default:
2020-11-09 17:04:42 +01:00
VGM_LOG("WWISE: unknown vorb size 0x%x\n", ww.vorb_size);
2020-03-01 12:21:15 +01:00
goto fail;
}
2020-11-09 17:04:42 +01:00
vgmstream->num_samples = read_s32(ww.vorb_offset + 0x00, sf);
setup_offset = read_u32(ww.vorb_offset + data_offsets + 0x00, sf); /* within data (0 = no seek table) */
audio_offset = read_u32(ww.vorb_offset + data_offsets + 0x04, sf); /* within data */
2020-03-01 12:21:15 +01:00
if (block_offsets) {
2020-11-09 17:04:42 +01:00
cfg.blocksize_1_exp = read_u8(ww.vorb_offset + block_offsets + 0x00, sf); /* small */
cfg.blocksize_0_exp = read_u8(ww.vorb_offset + block_offsets + 0x01, sf); /* big */
2020-03-01 12:21:15 +01:00
}
ww.data_size -= audio_offset;
/* detect normal packets */
2020-11-09 17:04:42 +01:00
if (ww.vorb_size == 0x2a) {
2020-03-01 12:21:15 +01:00
/* almost all blocksizes are 0x08+0x0B except a few with 0x0a+0x0a [Captain America: Super Soldier (X360) voices/sfx] */
if (cfg.blocksize_0_exp == cfg.blocksize_1_exp)
cfg.packet_type = WWV_STANDARD;
}
/* detect setup type:
* - full inline: ~2009, ex. The King of Fighters XII (X360), The Saboteur (PC)
* - trimmed inline: ~2010, ex. Army of Two: 40 days (X360) some multiplayer files
* - external: ~2010, ex. Assassin's Creed Brotherhood (X360), Dead Nation (X360) */
2020-11-09 17:04:42 +01:00
if (ww.vorb_size == 0x34) {
size_t setup_size = read_u16 (start_offset + setup_offset + 0x00, sf);
uint32_t setup_id = read_u32be(start_offset + setup_offset + 0x06, sf);
2020-03-01 12:21:15 +01:00
/* if the setup after header starts with "(data)BCV" it's an inline codebook) */
2022-07-14 23:09:08 +02:00
if ((setup_id & 0x00FFFFFF) == get_id32be("\0BCV")) {
2020-03-01 12:21:15 +01:00
cfg.setup_type = WWV_FULL_SETUP;
}
/* if the setup is suspiciously big it's probably trimmed inline codebooks */
else if (setup_size > 0x200) { /* an external setup it's ~0x100 max + some threshold */
cfg.setup_type = WWV_INLINE_CODEBOOKS;
}
}
vgmstream->codec_data = init_vorbis_custom(sf, start_offset + setup_offset, VORBIS_WWISE, &cfg);
2020-03-01 12:21:15 +01:00
if (!vgmstream->codec_data) goto fail;
}
else {
/* newer Wwise (>2012) */
off_t extra_offset = ww.fmt_offset + 0x18; /* after flag + channels */
switch(ww.extra_size) {
case 0x30:
data_offsets = 0x10;
block_offsets = 0x28;
cfg.header_type = WWV_TYPE_2;
cfg.packet_type = WWV_MODIFIED;
/* setup not detectable by header, so we'll try both; libvorbis should reject wrong codebooks
2020-03-01 12:21:15 +01:00
* - standard: early (<2012), ex. The King of Fighters XIII (X360)-2011/11, .ogg (cbs are from aoTuV, too)
* - aoTuV603: later (>2012), ex. Sonic & All-Stars Racing Transformed (PC)-2012/11, .wem */
2021-09-19 23:54:38 +02:00
cfg.setup_type = ww.is_wem ? WWV_AOTUV603_CODEBOOKS : WWV_EXTERNAL_CODEBOOKS; /* aoTuV came along .wem */
2020-03-01 12:21:15 +01:00
break;
default:
2020-11-09 17:04:42 +01:00
VGM_LOG("WWISE: unknown extra size 0x%x\n", ww.vorb_size);
2020-03-01 12:21:15 +01:00
goto fail;
}
2020-11-09 17:04:42 +01:00
vgmstream->num_samples = read_s32(extra_offset + 0x00, sf);
setup_offset = read_u32(extra_offset + data_offsets + 0x00, sf); /* within data */
audio_offset = read_u32(extra_offset + data_offsets + 0x04, sf); /* within data */
cfg.blocksize_1_exp = read_u8(extra_offset + block_offsets + 0x00, sf); /* small */
cfg.blocksize_0_exp = read_u8(extra_offset + block_offsets + 0x01, sf); /* big */
2020-03-01 12:21:15 +01:00
ww.data_size -= audio_offset;
/* mutant .wem with metadata (voice strings/etc) between seek table and vorbis setup [Gears of War 4 (PC)] */
if (ww.meta_offset) {
/* 0x00: original setup_offset */
setup_offset += read_u32(ww.meta_offset + 0x04, sf); /* metadata size */
}
2020-03-01 12:21:15 +01:00
/* detect normal packets */
if (ww.extra_size == 0x30) {
/* almost all blocksizes are 0x08+0x0B except some with 0x09+0x09 [Oddworld New 'n' Tasty! (PSV)] */
if (cfg.blocksize_0_exp == cfg.blocksize_1_exp)
cfg.packet_type = WWV_STANDARD;
}
/* try with the selected codebooks */
vgmstream->codec_data = init_vorbis_custom(sf, start_offset + setup_offset, VORBIS_WWISE, &cfg);
2020-03-01 12:21:15 +01:00
if (!vgmstream->codec_data) {
/* codebooks failed: try again with the other type */
2021-09-19 23:54:38 +02:00
cfg.setup_type = ww.is_wem ? WWV_EXTERNAL_CODEBOOKS : WWV_AOTUV603_CODEBOOKS;
vgmstream->codec_data = init_vorbis_custom(sf, start_offset + setup_offset, VORBIS_WWISE, &cfg);
2020-03-01 12:21:15 +01:00
if (!vgmstream->codec_data) goto fail;
}
}
vgmstream->layout_type = layout_none;
vgmstream->coding_type = coding_VORBIS_custom;
vgmstream->codec_endian = ww.big_endian;
start_offset += audio_offset;
/* Vorbis is VBR so this is very approximate percent, meh */
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-03-01 12:21:15 +01:00
vgmstream->num_samples = (int32_t)(vgmstream->num_samples *
(double)(ww.file_size - start_offset) / (double)ww.data_size);
}
break;
}
#endif
2020-11-09 17:04:42 +01:00
case DSP: { /* Wii/3DS/WiiU */
2020-03-01 12:21:15 +01:00
//if (ww.fmt_size != 0x28 && ww.fmt_size != ?) goto fail; /* old, new */
if (ww.bits_per_sample != 4) goto fail;
vgmstream->coding_type = coding_NGC_DSP;
vgmstream->layout_type = layout_interleave;
2021-09-11 13:11:29 +02:00
vgmstream->interleave_block_size = 0x08; /* ww.block_size = 0x8 in older Wwise, samples per block in newer Wwise */
2020-03-01 12:21:15 +01:00
/* find coef position */
2020-11-09 17:04:42 +01:00
if (ww.wiih_offset) { /* older */
2020-03-01 12:21:15 +01:00
vgmstream->num_samples = dsp_bytes_to_samples(ww.data_size, ww.channels);
2020-11-09 17:04:42 +01:00
if (ww.wiih_size != 0x2e * ww.channels) goto fail;
if (is_dsp_full_interleave(sf, &ww, ww.wiih_offset, 1))
vgmstream->interleave_block_size = ww.data_size / 2;
2020-03-01 12:21:15 +01:00
}
2020-11-09 17:04:42 +01:00
else if (ww.extra_size == 0x0c + ww.channels * 0x2e) { /* newer */
vgmstream->num_samples = read_s32(ww.fmt_offset + 0x18, sf);
ww.wiih_offset = ww.fmt_offset + 0x1c;
ww.wiih_size = 0x2e * ww.channels;
if (is_dsp_full_interleave(sf, &ww, ww.wiih_offset, 0)) /* less common */
vgmstream->interleave_block_size = ww.data_size / 2;
2020-03-01 12:21:15 +01:00
}
else {
goto fail;
}
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
ww.data_size = ww.file_size - ww.data_offset;
vgmstream->num_samples = dsp_bytes_to_samples(ww.data_size, ww.channels);
}
2020-03-01 12:21:15 +01:00
/* for some reason all(?) DSP .wem do full loops (even mono/jingles/etc) but
* several tracks do loop like this, so disable it for short-ish tracks */
if (ww.loop_flag && vgmstream->loop_start_sample == 0 &&
vgmstream->loop_end_sample < 20*ww.sample_rate) { /* in seconds */
vgmstream->loop_flag = 0;
}
2020-11-09 17:04:42 +01:00
dsp_read_coefs(vgmstream, sf, ww.wiih_offset + 0x00, 0x2e, ww.big_endian);
dsp_read_hist (vgmstream, sf, ww.wiih_offset + 0x24, 0x2e, ww.big_endian);
2020-03-01 12:21:15 +01:00
break;
}
#ifdef VGM_USE_FFMPEG
2020-11-09 17:04:42 +01:00
case XMA2: { /* X360/XBone */
2020-03-01 12:21:15 +01:00
/* endian check should be enough */
//if (ww.fmt_size != ...) goto fail; /* XMA1 0x20, XMA2old: 0x34, XMA2new: 0x40, XMA2 Guitar Hero Live/padded: 0x64, etc */
/* only Wwise XMA: X360=BE, or XBone=LE+wem (real X360 XMA are LE and parsed elsewhere) */
if (!(ww.big_endian || (!ww.big_endian && check_extensions(sf,"wem,bnk"))))
goto fail;
2020-03-01 12:21:15 +01:00
vgmstream->codec_data = init_ffmpeg_xma_chunk(sf, ww.data_offset, ww.data_size, ww.xma2_offset ? ww.xma2_offset : ww.fmt_offset, ww.xma2_size ? ww.xma2_size : ww.fmt_size);
2020-03-01 12:21:15 +01:00
if ( !vgmstream->codec_data ) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
vgmstream->num_samples = ww.num_samples; /* set while parsing XMAWAVEFORMATs */
/* Wwise loops are always pre-adjusted (old or new) and only num_samples is off */
2020-11-09 17:04:42 +01:00
xma_fix_raw_samples(vgmstream, sf, ww.data_offset, ww.data_size, ww.xma2_offset ? ww.xma2_offset : ww.fmt_offset, 1,0);
2020-03-01 12:21:15 +01:00
/* XMA is VBR so this is very approximate percent, meh */
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-03-01 12:21:15 +01:00
vgmstream->num_samples = (int32_t)(vgmstream->num_samples *
(double)(ww.file_size - start_offset) / (double)ww.data_size);
2020-11-09 14:51:46 +01:00
//todo data size, call function
2020-03-01 12:21:15 +01:00
}
break;
}
2020-11-09 17:04:42 +01:00
case XWMA: { /* X360 */
2020-03-01 12:21:15 +01:00
if (ww.fmt_size != 0x18) goto fail;
if (!ww.big_endian) goto fail; /* must be from Wwise X360 (PC LE XWMA is parsed elsewhere) */
2021-09-11 13:11:29 +02:00
vgmstream->codec_data = init_ffmpeg_xwma(sf, ww.data_offset, ww.data_size, ww.format, ww.channels, ww.sample_rate, ww.avg_bitrate, ww.block_size);
if (!vgmstream->codec_data) goto fail;
2020-03-01 12:21:15 +01:00
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
2022-01-30 13:31:06 +01:00
//if (ww.prefetch) {
// ww.data_size = ww.file_size - ww.data_offset;
//}
2021-09-11 13:11:29 +02:00
/* seek table seems BE dpds */
vgmstream->num_samples = xwma_dpds_get_samples(sf, ww.seek_offset, ww.seek_size, ww.channels, ww.big_endian);
if (!vgmstream->num_samples)
vgmstream->num_samples = xwma_get_samples(sf, ww.data_offset, ww.data_size, ww.format, ww.channels, ww.sample_rate, ww.block_size);
2022-01-30 13:31:06 +01:00
/* XWMA is VBR so this is very approximate percent, meh */
if (ww.prefetch) { /* Guardians of Middle Earth (X360) */
vgmstream->num_samples = (int32_t)(vgmstream->num_samples *
(double)(ww.file_size - start_offset) / (double)ww.data_size);
}
2020-03-01 12:21:15 +01:00
break;
}
case AAC: { /* iOS/Mac */
ffmpeg_codec_data * ffmpeg_data = NULL;
if (ww.fmt_size != 0x24) goto fail;
2021-09-11 13:11:29 +02:00
if (ww.block_size != 0 || ww.bits_per_sample != 0) goto fail;
2020-03-01 12:21:15 +01:00
/* extra: size 0x12, unknown values */
ffmpeg_data = init_ffmpeg_offset(sf, ww.data_offset,ww.data_size);
2020-03-01 12:21:15 +01:00
if (!ffmpeg_data) goto fail;
vgmstream->codec_data = ffmpeg_data;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
2021-07-23 18:55:03 +02:00
vgmstream->num_samples = ffmpeg_get_samples(ffmpeg_data); //todo correct?
2020-03-01 12:21:15 +01:00
break;
}
case OPUSNX: { /* Switch */
size_t skip;
2020-11-09 17:04:42 +01:00
size_t seek_size;
2020-03-01 12:21:15 +01:00
2020-11-09 17:04:42 +01:00
if (ww.fmt_size != 0x28) goto fail;
2021-09-11 13:11:29 +02:00
/* values up to 0x14 seem fixed and similar to HEVAG's (block_size 0x02/04, bits_per_sample 0x10) */
2020-03-01 12:21:15 +01:00
2020-11-09 17:04:42 +01:00
vgmstream->num_samples = read_s32(ww.fmt_offset + 0x18, sf);
/* 0x1c: null?
* 0x20: data_size without seek_size */
seek_size = read_u32(ww.fmt_offset + 0x24, sf);
2020-03-01 12:21:15 +01:00
2020-11-09 17:04:42 +01:00
start_offset += seek_size;
ww.data_size -= seek_size;
2020-03-01 12:21:15 +01:00
skip = switch_opus_get_encoder_delay(start_offset, sf); /* should be 120 */
2020-03-01 12:21:15 +01:00
2020-11-09 14:51:46 +01:00
/* some voices have original sample rate but OPUS can only do 48000 (ex. Mario Kart Home Circuit 24khz) */
if (vgmstream->sample_rate != 48000) {
vgmstream->sample_rate = 48000;
vgmstream->num_samples = switch_opus_get_samples(start_offset,ww.data_size, sf); /* also original's */
vgmstream->num_samples -= skip;
}
2020-03-01 12:21:15 +01:00
/* OPUS is VBR so this is very approximate percent, meh */
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-03-01 12:21:15 +01:00
vgmstream->num_samples = (int32_t)(vgmstream->num_samples *
(double)(ww.file_size - start_offset) / (double)ww.data_size);
ww.data_size = ww.file_size - start_offset;
}
2020-11-09 17:04:42 +01:00
vgmstream->codec_data = init_ffmpeg_switch_opus(sf, start_offset,ww.data_size, ww.channels, skip, vgmstream->sample_rate);
2020-03-01 12:21:15 +01:00
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
break;
}
case OPUS: { /* fully standard Ogg Opus [Girl Cafe Gun (Mobile), Gears 5 (PC)] */
2021-09-11 13:11:29 +02:00
if (ww.block_size != 0 || ww.bits_per_sample != 0) goto fail;
2020-03-01 12:21:15 +01:00
/* extra: size 0x12 */
2020-11-09 17:04:42 +01:00
vgmstream->num_samples = read_s32(ww.fmt_offset + 0x18, sf);
2020-03-01 12:21:15 +01:00
/* 0x1c: stream size without OggS? */
/* 0x20: full samples (without encoder delay) */
2020-03-08 19:35:50 +01:00
/* OPUS is VBR so this is very approximate percent, meh */
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-03-08 19:35:50 +01:00
vgmstream->num_samples = (int32_t)(vgmstream->num_samples *
(double)(ww.file_size - start_offset) / (double)ww.data_size);
ww.data_size = ww.file_size - start_offset;
}
/* mutant .wem with metadata (voice strings/etc) at data start [Gears 5 (PC)] */
if (ww.meta_offset) {
/* 0x00: original setup_offset? (0x00 for Opus) */
uint32_t meta_skip = read_u32(ww.meta_offset + 0x04, sf);
ww.data_offset += meta_skip;
ww.data_size -= meta_skip;
}
2020-11-09 14:51:46 +01:00
vgmstream->codec_data = init_ffmpeg_offset(sf, ww.data_offset, ww.data_size);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
break;
}
2020-11-09 17:04:42 +01:00
case OPUSCPR: { /* CD Project RED's Ogg Opus masquerading as PCMEX [Cyberpunk 2077 (PC)] */
if (ww.bits_per_sample != 16) goto fail;
/* original data_size doesn't look related to samples or anything */
2021-09-04 22:19:36 +02:00
ww.data_size = ww.file_size - ww.data_offset;
vgmstream->codec_data = init_ffmpeg_offset(sf, ww.data_offset, ww.data_size);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
/* FFmpeg's samples seem correct, otherwise see ogg_opus.c for getting samples. */
2021-07-23 18:55:03 +02:00
vgmstream->num_samples = ffmpeg_get_samples(vgmstream->codec_data);
break;
}
2020-11-22 19:00:55 +01:00
case OPUSWW: { /* updated Opus [Assassin's Creed Valhalla (PC)] */
2022-10-29 17:51:32 +02:00
int i, mapping;
2020-11-22 19:00:55 +01:00
opus_config cfg = {0};
2021-09-11 13:11:29 +02:00
if (ww.block_size != 0 || ww.bits_per_sample != 0) goto fail;
if (!ww.seek_offset) goto fail;
if (ww.channels > 255) goto fail; /* opus limit */
2020-11-09 14:51:46 +01:00
2020-11-22 19:00:55 +01:00
cfg.channels = ww.channels;
cfg.table_offset = ww.seek_offset;
2022-10-29 17:51:32 +02:00
vgmstream->sample_rate = 48000; /* fixed in AK's code */
2020-11-22 19:00:55 +01:00
/* extra: size 0x10 (though last 2 fields are beyond, AK plz) */
2020-11-09 14:51:46 +01:00
/* 0x12: samples per frame */
2020-11-09 17:04:42 +01:00
vgmstream->num_samples = read_s32(ww.fmt_offset + 0x18, sf);
2020-11-22 19:00:55 +01:00
cfg.table_count = read_u32(ww.fmt_offset + 0x1c, sf); /* same as seek size / 2 */
cfg.skip = read_u16(ww.fmt_offset + 0x20, sf);
/* 0x22: codec version */
mapping = read_u8(ww.fmt_offset + 0x23, sf);
if (mapping == 1 && ww.channels > 8) goto fail; /* mapping not defined */
2020-11-22 19:00:55 +01:00
if (read_u8(ww.fmt_offset + 0x22, sf) != 1)
goto fail;
2020-11-09 14:51:46 +01:00
/* OPUS is VBR so this is very approximate percent, meh */
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-11-09 14:51:46 +01:00
vgmstream->num_samples = (int32_t)(vgmstream->num_samples *
(double)(ww.file_size - start_offset) / (double)ww.data_size);
ww.data_size = ww.file_size - start_offset;
}
2022-10-29 17:51:32 +02:00
/* AK does some wonky implicit config for multichannel (only accepted channel type is 1) */
if (ww.channel_type == 1 && mapping == 1) {
static const int8_t mapping_matrix[8][8] = { /* (DeinterleaveAndRemap)*/
2020-11-22 19:00:55 +01:00
{ 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 1, 0, 0, 0, 0, 0, 0, },
{ 0, 2, 1, 0, 0, 0, 0, 0, },
{ 0, 1, 2, 3, 0, 0, 0, 0, },
{ 0, 4, 1, 2, 3, 0, 0, 0, },
{ 0, 4, 1, 2, 3, 5, 0, 0, },
{ 0, 6, 1, 2, 3, 4, 5, 0, },
{ 0, 6, 1, 2, 3, 4, 5, 7, },
};
2022-10-29 17:51:32 +02:00
if (ww.channels > 8)
goto fail; /* matrix limit */
/* find coupled (stereo) OPUS streams (simplification of ChannelConfigToMapping) */
2020-11-22 19:00:55 +01:00
switch(ww.channel_layout) {
case mapping_7POINT1_surround: cfg.coupled_count = 3; break; /* 2ch+2ch+2ch+1ch+1ch, 5 streams */
case mapping_5POINT1_surround: /* 2ch+2ch+1ch+1ch, 4 streams */
case mapping_QUAD_side: cfg.coupled_count = 2; break; /* 2ch+2ch, 2 streams */
case mapping_2POINT1_xiph: /* 2ch+1ch, 2 streams */
case mapping_STEREO: cfg.coupled_count = 1; break; /* 2ch, 1 stream */
default: cfg.coupled_count = 0; break; /* 1ch, 1 stream */
2022-07-14 23:09:08 +02:00
//TODO: AK OPUS doesn't seem to handle others mappings, though AK's .h imply they exist (uses 0 coupleds?)
2020-11-22 19:00:55 +01:00
}
/* total number internal OPUS streams (should be >0) */
cfg.stream_count = ww.channels - cfg.coupled_count;
2022-10-29 17:51:32 +02:00
/* channel order */
for (i = 0; i < ww.channels; i++) {
cfg.channel_mapping[i] = mapping_matrix[ww.channels - 1][i];
}
}
else if (ww.channel_type == 1 && mapping == 255) { /* Overwatch 2 (PC) */
/* only seen 12ch, but seems to be what ChannelConfigToMapping would output with > 8 */
cfg.coupled_count = 0;
cfg.stream_count = ww.channels - cfg.coupled_count;
//TODO: mapping seems to be 0x2d63f / FL FR FC LFE BL BR SL SR TFL TFR TBL TBR
// while output order seems to swap FC and LFE? (not set in passed channel mapping but reordered later)
for (i = 0; i < ww.channels; i++) {
cfg.channel_mapping[i] = i;
2020-11-22 19:00:55 +01:00
}
}
2022-10-29 17:51:32 +02:00
else {
/* mapping 0: standard opus (implicit mono/stereo) */
if (ww.channels > 2)
goto fail;
}
2020-11-22 19:00:55 +01:00
2020-11-09 14:51:46 +01:00
/* Wwise Opus saves all frame sizes in the seek table */
2020-11-22 19:00:55 +01:00
vgmstream->codec_data = init_ffmpeg_wwise_opus(sf, ww.data_offset, ww.data_size, &cfg);
2020-03-01 12:21:15 +01:00
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
break;
}
#endif
2021-08-26 19:39:58 +02:00
2020-11-09 17:04:42 +01:00
case HEVAG: /* PSV */
2020-03-01 12:21:15 +01:00
/* changed values, another bizarre Wwise quirk */
2021-09-11 13:11:29 +02:00
//ww.block_size /* unknown (1ch=2, 2ch=4) */
2020-03-01 12:21:15 +01:00
//ww.bits_per_sample; /* unknown (0x10) */
//if (ww.bits_per_sample != 4) goto fail;
if (ww.fmt_size != 0x18) goto fail;
if (ww.big_endian) goto fail;
/* extra data (size 0x06)
2020-11-09 17:04:42 +01:00
* 0x00: samples per block (0x1c)
* 0x02: channel config (again?) */
2020-03-01 12:21:15 +01:00
vgmstream->coding_type = coding_HEVAG;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x10;
vgmstream->num_samples = ps_bytes_to_samples(ww.data_size, ww.channels);
break;
#ifdef VGM_USE_ATRAC9
case ATRAC9: { /* PSV/PS4 */
atrac9_config cfg = {0};
if (ww.fmt_size != 0x24) goto fail;
if (ww.extra_size != 0x12) goto fail;
/* extra data
* 0x00: samples per subframe?
* 0x02: channel config (again?)
* 0x06: config
* 0x0a: samples
* 0x0e: encoder delay? (same as samples per subframe?)
* 0x10: decoder delay? (PS4 only, 0 on Vita?) */
2020-11-09 17:04:42 +01:00
cfg.channels = ww.channels;
cfg.config_data = read_u32be(ww.fmt_offset + 0x18,sf);
cfg.encoder_delay = read_u16(ww.fmt_offset + 0x20,sf);
/* PS4 value at 0x22 looks like encoder delay, but using it removes too many
* samples [DmC: Definitive Edition (PS4)] */
2020-03-01 12:21:15 +01:00
vgmstream->codec_data = init_atrac9(&cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_ATRAC9;
vgmstream->layout_type = layout_none;
2020-11-09 17:04:42 +01:00
vgmstream->num_samples = read_s32(ww.fmt_offset + 0x1c, sf);
2022-07-14 23:09:08 +02:00
if (ww.prefetch) {
vgmstream->num_samples = atrac9_bytes_to_samples_cfg(ww.file_size - start_offset, cfg.config_data);
}
2020-03-01 12:21:15 +01:00
break;
}
#endif
2021-08-26 19:39:58 +02:00
2020-11-09 17:04:42 +01:00
case PTADPCM: /* newer ADPCM [Bayonetta 2 (Switch), Genshin Impact (PC)] */
2020-03-01 12:21:15 +01:00
if (ww.bits_per_sample != 4) goto fail;
2021-09-11 13:11:29 +02:00
if (ww.block_size != 0x24 * ww.channels && ww.block_size != 0x104 * ww.channels) goto fail;
2020-03-01 12:21:15 +01:00
vgmstream->coding_type = coding_PTADPCM;
vgmstream->layout_type = layout_interleave;
2021-09-11 13:11:29 +02:00
vgmstream->interleave_block_size = ww.block_size / ww.channels;
2020-03-01 12:21:15 +01:00
//vgmstream->codec_endian = ww.big_endian; //?
2021-11-06 14:57:41 +01:00
if (ww.prefetch) {
2020-08-15 20:04:59 +02:00
ww.data_size = ww.file_size - ww.data_offset;
}
2020-03-01 12:21:15 +01:00
vgmstream->num_samples = ptadpcm_bytes_to_samples(ww.data_size, ww.channels, vgmstream->interleave_block_size);
break;
default:
goto fail;
}
2020-11-09 17:04:42 +01:00
if (!vgmstream_open_stream(vgmstream, sf, start_offset) )
2020-03-01 12:21:15 +01:00
goto fail;
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
}
static int is_dsp_full_interleave(STREAMFILE* sf, wwise_header* ww, off_t coef_offset, int full_detection) {
/* older (bank ~v48) Wwise use full interleave for memory (in .bnk) files, but
* detection from the .wem side is problematic [Punch Out!! (Wii)-old, Luigi's Mansion 2 (3DS)-new]
2021-11-06 14:57:41 +01:00
* - prefetch point to streams = normal
* - .bnk would be memory banks = full
* - otherwise small-ish sizes, stereo, with initial predictors for the
* second channel matching half size = full
2021-09-04 22:19:36 +02:00
* some files aren't detectable like this though, when predictors are 0
* (but since memory wem aren't that used this shouldn't be too common) */
2021-11-06 14:57:41 +01:00
if (ww->prefetch)
return 0;
2020-04-23 17:42:57 +02:00
if (ww->channels == 1)
return 0;
2021-11-06 14:57:41 +01:00
if (ww->is_bnk)
return 1;
2020-04-23 17:42:57 +02:00
if (ww->data_size > 0x30000)
return 0;
/* skip reading data if possible */
if (full_detection) {
uint16_t head_ps2 = read_u16be(coef_offset + 1 * 0x2e + 0x22, sf); /* ch2's initial predictor */
uint16_t init_ps2 = read_u8(ww->data_offset + 0x08, sf); /* at normal interleave */
uint16_t half_ps2 = read_u8(ww->data_offset + ww->data_size / 2, sf); /* at full interleave */
//;VGM_LOG("WWISE: DSP head2=%x, init2=%x, half2=%x\n", head_ps2, init_ps2, half_ps2);
if (head_ps2 != init_ps2 && head_ps2 == half_ps2) {
return 1;
}
}
return 0;
}
2020-03-01 12:21:15 +01:00
2020-11-09 17:04:42 +01:00
static int parse_wwise(STREAMFILE* sf, wwise_header* ww) {
2021-09-26 19:34:12 +02:00
read_u32_t read_u32;
read_u16_t read_u16;
2020-11-09 17:04:42 +01:00
2021-09-26 19:34:12 +02:00
/* Wwise honors machine's endianness (PC=RIFF, X360=RIFX --unlike XMA) */
ww->big_endian = is_id32be(0x00,sf, "RIFX"); /* RIFF size not useful to detect, see below */
if (ww->big_endian) {
2020-11-09 17:04:42 +01:00
read_u32 = read_u32be;
read_u16 = read_u16be;
} else {
read_u32 = read_u32le;
read_u16 = read_u16le;
}
ww->file_size = get_streamfile_size(sf);
#if 0
/* Wwise's RIFF size is often wonky, seemingly depending on codec:
* - PCM, IMA/PTADPCM, VORBIS, AAC, OPUSNX/OPUS: correct
* - DSP, XWMA, ATRAC9: almost always slightly smaller (around 0x50)
* - HEVAG: very off
* - XMA2: exact file size
* - some RIFX have LE size
2020-11-22 19:00:55 +01:00
* Value is ignored by AK's parser (set to -1).
2020-11-09 17:04:42 +01:00
* (later we'll validate "data" which fortunately is correct)
*/
if (read_u32(0x04,sf) + 0x04 + 0x04 != ww->file_size) {
VGM_LOG("WWISE: bad riff size\n");
goto fail;
}
#endif
2021-09-19 23:54:38 +02:00
if (!is_id32be(0x08,sf, "WAVE") &&
!is_id32be(0x08,sf, "XWMA"))
goto fail;
2020-11-09 17:04:42 +01:00
/* parse chunks (reads once linearly) */
{
2021-09-19 23:54:38 +02:00
chunk_t rc = {0};
2022-07-31 15:16:15 +02:00
uint32_t file_size = get_streamfile_size(sf);;
2020-11-09 17:04:42 +01:00
2021-09-19 23:54:38 +02:00
/* chunks are even-aligned and don't need to add padding byte, unlike real RIFFs */
rc.be_size = ww->big_endian;
rc.current = 0x0c;
while (next_chunk(&rc, sf)) {
switch(rc.type) {
2020-11-09 17:04:42 +01:00
case 0x666d7420: /* "fmt " */
2021-09-19 23:54:38 +02:00
ww->fmt_offset = rc.offset;
ww->fmt_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x584D4132: /* "XMA2" */
2021-09-19 23:54:38 +02:00
ww->xma2_offset = rc.offset;
ww->xma2_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x64617461: /* "data" */
2021-09-19 23:54:38 +02:00
ww->data_offset = rc.offset;
ww->data_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x766F7262: /* "vorb" */
2021-09-19 23:54:38 +02:00
ww->vorb_offset = rc.offset;
ww->vorb_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x57696948: /* "WiiH" */
2021-09-19 23:54:38 +02:00
ww->wiih_offset = rc.offset;
ww->wiih_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x7365656B: /* "seek" */
2021-09-19 23:54:38 +02:00
ww->seek_offset = rc.offset;
ww->seek_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x736D706C: /* "smpl" */
2021-09-19 23:54:38 +02:00
ww->smpl_offset = rc.offset;
ww->smpl_size = rc.size;
2020-11-09 17:04:42 +01:00
break;
case 0x6D657461: /* "meta" */
2021-09-19 23:54:38 +02:00
ww->meta_offset = rc.offset;
ww->meta_size = rc.size;
break;
2020-11-09 17:04:42 +01:00
case 0x66616374: /* "fact" */
2021-09-19 23:54:38 +02:00
/* Wwise never uses fact, but if somehow some file does uncomment the following: */
2020-11-09 17:04:42 +01:00
//if (size == 0x10 && read_u32be(offset + 0x04, sf) == 0x4C794E20) /* "LyN " */
// goto fail; /* ignore LyN RIFF */
goto fail;
/* "XMAc": rare XMA2 physical loop regions (loop_start_b, loop_end_b, loop_subframe_data)
* Can appear even in the file doesn't loop, maybe it's meant to be the playable physical region */
/* "LIST": leftover 'cue' info from OG .wavs (ex. loop starts in Platinum Games) */
/* "JUNK": optional padding for aligment (0-size JUNK exists too) */
/* "akd ": extra info for Wwise? (wave peaks/loudness/HDR envelope?) */
default:
2022-07-31 15:16:15 +02:00
/* mainly for incorrectly ripped wems, but should allow truncated wems
* (could also check that fourcc is ASCII) */
if (rc.offset + rc.size > file_size) {
vgm_logi("WWISE: broken .wem (bad extract?)\n");
goto fail;
}
2020-11-09 17:04:42 +01:00
break;
}
}
}
2021-09-19 23:54:38 +02:00
/* use extension as a guide for certain cases */
ww->is_wem = check_extensions(sf,"wem,bnk");
2020-11-09 17:04:42 +01:00
/* parse format (roughly spec-compliant but some massaging is needed) */
if (ww->xma2_offset) {
/* pseudo-XMA2WAVEFORMAT, "fmt"+"XMA2" (common) or only "XMA2" [Too Human (X360)] */
ww->format = 0x0165; /* signal for below */
xma2_parse_xma2_chunk(sf,
ww->xma2_offset, &ww->channels, &ww->sample_rate, &ww->loop_flag,
&ww->num_samples, &ww->loop_start_sample, &ww->loop_end_sample);
}
else {
/* pseudo-WAVEFORMATEX */
if (ww->fmt_size < 0x10)
goto fail;
ww->format = read_u16(ww->fmt_offset + 0x00,sf);
ww->channels = read_u16(ww->fmt_offset + 0x02,sf);
ww->sample_rate = read_u32(ww->fmt_offset + 0x04,sf);
2021-09-11 13:11:29 +02:00
ww->avg_bitrate = read_u32(ww->fmt_offset + 0x08,sf);
2021-09-19 23:54:38 +02:00
ww->block_size = read_u16(ww->fmt_offset + 0x0c,sf);
2020-11-09 17:04:42 +01:00
ww->bits_per_sample = read_u16(ww->fmt_offset + 0x0e,sf);
if (ww->fmt_size > 0x10 && ww->format != 0x0165 && ww->format != 0x0166) /* ignore XMAWAVEFORMAT */
ww->extra_size = read_u16(ww->fmt_offset + 0x10,sf);
if (ww->extra_size >= 0x06) { /* always present (actual RIFFs only have it in WAVEFORMATEXTENSIBLE) */
/* mostly WAVEFORMATEXTENSIBLE's bitmask (see AkSpeakerConfig.h) */
ww->channel_layout = read_u32(ww->fmt_offset + 0x14,sf);
/* later games (+2018?) have a pseudo-format instead to handle more cases:
* - 8b: uNumChannels
* - 4b: eConfigType (0=none, 1=standard, 2=ambisonic)
* - 19b: uChannelMask */
if ((ww->channel_layout & 0xFF) == ww->channels) {
2020-11-22 19:00:55 +01:00
ww->channel_type = (ww->channel_layout >> 8) & 0x0F;
2020-11-09 17:04:42 +01:00
ww->channel_layout = (ww->channel_layout >> 12);
}
}
if (ww->format == 0x0166) { /* XMA2WAVEFORMATEX in fmt */
xma2_parse_fmt_chunk_extra(sf, ww->fmt_offset, &ww->loop_flag,
&ww->num_samples, &ww->loop_start_sample, &ww->loop_end_sample, ww->big_endian);
}
}
/* common loops ("XMA2" chunks already read them) */
if (ww->smpl_offset) {
if (ww->smpl_size >= 0x34
&& read_u32(ww->smpl_offset + 0x1c, sf) == 1 /* loop count */
&& read_u32(ww->smpl_offset + 0x24 + 0x04, sf) == 0) { /* loop type */
ww->loop_flag = 1;
ww->loop_start_sample = read_u32(ww->smpl_offset + 0x24 + 0x8, sf);
ww->loop_end_sample = read_u32(ww->smpl_offset + 0x24 + 0xc, sf) + 1; /* +1 like standard RIFF */
}
}
if (!ww->data_offset)
goto fail;
/* format to codec */
switch(ww->format) {
case 0x0001: ww->codec = PCM; break; /* older Wwise */
2020-11-29 20:10:49 +01:00
case 0x0002: ww->codec = IMA; break; /* newer Wwise (variable, probably means "platform's ADPCM") */
2020-11-09 17:04:42 +01:00
case 0x0069: ww->codec = IMA; break; /* older Wwise [Spiderman Web of Shadows (X360), LotR Conquest (PC)] */
case 0x0161: ww->codec = XWMA; break; /* WMAv2 */
case 0x0162: ww->codec = XWMA; break; /* WMAPro */
case 0x0165: ww->codec = XMA2; break; /* XMA2-chunk XMA (Wwise doesn't use XMA1) */
case 0x0166: ww->codec = XMA2; break; /* fmt-chunk XMA */
case 0xAAC0: ww->codec = AAC; break;
case 0xFFF0: ww->codec = DSP; break;
case 0xFFFB: ww->codec = HEVAG; break; /* "VAG" */
2020-11-09 17:04:42 +01:00
case 0xFFFC: ww->codec = ATRAC9; break;
case 0xFFFE: ww->codec = PCM; break; /* "PCM for Wwise Authoring" */
case 0xFFFF: ww->codec = VORBIS; break;
case 0x3039: ww->codec = OPUSNX; break; /* renamed from "OPUS" on Wwise 2018.1 */
case 0x3040: ww->codec = OPUS; break;
case 0x3041: ww->codec = OPUSWW; break; /* "OPUS_WEM", added on Wwise 2019.2.3, replaces OPUS */
2020-11-09 17:04:42 +01:00
case 0x8311: ww->codec = PTADPCM; break; /* added on Wwise 2019.1, replaces IMA */
default:
2021-09-19 23:54:38 +02:00
/* some .wav may end up here, only report in .wem cases (newer codecs) */
if (ww->is_wem)
vgm_logi("WWISE: unknown codec 0x%04x (report)\n", ww->format);
2020-11-09 17:04:42 +01:00
goto fail;
}
/* identify system's ADPCM */
if (ww->format == 0x0002) {
if (ww->extra_size == 0x0c + ww->channels * 0x2e) {
/* newer Wwise DSP with coefs [Epic Mickey 2 (Wii), Batman Arkham Origins Blackgate (3DS)] */
ww->codec = DSP;
2021-08-26 19:39:58 +02:00
}
else if (ww->extra_size == 0x0a && ww->wiih_offset) { /* WiiH */
2020-11-09 17:04:42 +01:00
/* few older Wwise DSP with num_samples in extra_size [Tony Hawk: Shred (Wii)] */
ww->codec = DSP;
2021-08-26 19:39:58 +02:00
}
2021-09-11 13:11:29 +02:00
else if (ww->block_size == 0x104 * ww->channels) {
2021-08-26 19:39:58 +02:00
/* Bayonetta 2 (Switch) */
ww->codec = PTADPCM;
2020-11-09 17:04:42 +01:00
}
}
/* Some Wwise .bnk (RAM) files have truncated, prefetch mirrors of another file, that
* play while the rest of the real stream loads. We'll add basic support to avoid
* complaints of this or that .wem not playing */
if (ww->data_offset + ww->data_size > ww->file_size) {
//;VGM_LOG("WWISE: truncated data size (prefetch): (real=0x%x > riff=0x%x)\n", ww->data_size, ww->file_size);
/* catch wrong rips as truncated tracks' file_size should be much smaller than data_size,
* but it's possible to pre-fetch small files too [Punch Out!! (Wii)] */
if (ww->data_offset + ww->data_size - ww->file_size < 0x5000 && ww->file_size > 0x10000) {
2021-08-26 19:39:58 +02:00
vgm_logi("WWISE: wrong expected size (re-rip?)\n");
2020-11-09 17:04:42 +01:00
goto fail;
}
if (ww->codec == PCM || ww->codec == IMA || ww->codec == VORBIS || ww->codec == DSP || ww->codec == XMA2 ||
2022-07-14 23:09:08 +02:00
ww->codec == OPUSNX || ww->codec == OPUS || ww->codec == OPUSWW || ww->codec == PTADPCM || ww->codec == XWMA || ww->codec == ATRAC9) {
ww->prefetch = 1; /* only seen those, probably all exist (missing XWMA, AAC, HEVAG) */
2020-11-09 17:04:42 +01:00
} else {
2021-11-06 14:57:41 +01:00
vgm_logi("WWISE: wrong expected size, maybe prefetch (report)\n");
2020-11-09 17:04:42 +01:00
goto fail;
}
}
/* Cyberpunk 2077 has some mutant .wem, with proper Wwise header and PCMEX but data is standard OPUS.
* From init bank and CAkSound's sources, those may be piped through their plugins. They come in
* .opuspak (no names), have wrong riff/data sizes and only seem used for sfx (other audio is Vorbis). */
2021-11-06 14:57:41 +01:00
if (ww->format == 0xFFFE && ww->prefetch) {
2022-07-14 23:09:08 +02:00
if (is_id32be(ww->data_offset + 0x00, sf, "OggS")) {
ww->codec = OPUSCPR;
}
}
2020-11-09 17:04:42 +01:00
return 1;
fail:
return 0;
}
2020-03-01 12:21:15 +01:00
/* VORBIS FORMAT RESEARCH */
/*
- old format
"fmt" size 0x28, extra size 0x16 / size 0x18, extra size 0x06
0x18-24 (16): ? (fixed: 0x01000000 00001000 800000AA 00389B71) [removed when extra size is 0x06]
"vorb" size 0x34
2020-11-29 20:10:49 +01:00
0x00 (4): dwTotalPCMFrames
2020-03-01 12:21:15 +01:00
0x04 (4): skip samples?
2020-11-29 20:10:49 +01:00
0x08 (4): LoopInfo.uLoopBeginExtra? (present if loop)
0x0c (4): LoopInfo.dwLoopStartPacketOffset (data start, or loop start when "smpl" is present)
0x10 (4): LoopInfo.uLoopEndExtra? (0..~0x400)
0x14 (4): LoopInfo.dwLoopEndPacketOffset?
0x18 (4): dwSeekTableSize (0 = no seek table)
0x1c (4): dwVorbisDataOffset (offset within data)
2021-09-04 22:19:36 +02:00
0x20 (2): uMaxPacketSize (not including header)
2020-11-29 20:10:49 +01:00
0x22 (2): uLastGranuleExtra (0..~0x100)
0x24 (4): dwDecodeAllocSize (0~0x5000)
0x28 (4): dwDecodeX64AllocSize (mid, 0~0x5000)
0x2c (4): uHashCodebook? (shared by several .wem a game, but not all need to share it)
0x30 (1): uBlockSizes[0] (blocksize_1_exp, small)
0x31 (1): uBlockSizes[1] (blocksize_0_exp, large)
2020-03-01 12:21:15 +01:00
0x32 (2): empty
"vorb" size 0x28 / 0x2c / 0x2a
2020-11-29 20:10:49 +01:00
0x00 (4): dwTotalPCMFrames
0x04 (4): LoopInfo.dwLoopStartPacketOffset (data start, or loop start when "smpl" is present)
0x08 (4): LoopInfo.dwLoopEndPacketOffset (data end, or loop end when "smpl" is present)
2020-03-01 12:21:15 +01:00
0x0c (2): ? (small, 0..~0x400) [(4) when size is 0x2C]
2020-11-29 20:10:49 +01:00
0x10 (4): dwSeekTableSize (0 = no seek table)
0x14 (4): dwVorbisDataOffset (offset within data)
2021-09-04 22:19:36 +02:00
0x18 (2): uMaxPacketSize (not including header)
2020-11-29 20:10:49 +01:00
0x1a (2): uLastGranuleExtra (0..~0x100) [(4) when size is 0x2C]
0x1c (4): dwDecodeAllocSize (0~0x5000)
0x20 (4): dwDecodeX64AllocSize (0~0x5000)
0x24 (4): uHashCodebook? (shared by several .wem a game, but not all need to share it)
0x28 (1): uBlockSizes[0] (blocksize_1_exp, small) [removed when size is 0x28]
0x29 (1): uBlockSizes[1] (blocksize_0_exp, large) [removed when size is 0x28]
2020-03-01 12:21:15 +01:00
- new format:
"fmt" size 0x42, extra size 0x30
2020-11-29 20:10:49 +01:00
0x18 (4): dwTotalPCMFrames
0x1c (4): LoopInfo.dwLoopStartPacketOffset (data start, or loop start when "smpl" is present)
0x20 (4): LoopInfo.dwLoopEndPacketOffset (data end, or loop end when "smpl" is present)
0x24 (2): LoopInfo.uLoopBeginExtra (small, 0..~0x400)
0x26 (2): LoopInfo.uLoopEndExtra (extra samples after seek?)
0x28 (4): dwSeekTableSize (0 = no seek table)
0x2c (4): dwVorbisDataOffset (offset within data)
2021-09-04 22:19:36 +02:00
0x30 (2): uMaxPacketSize (not including header)
2020-11-29 20:10:49 +01:00
0x32 (2): uLastGranuleExtra (small, 0..~0x100)
0x34 (4): dwDecodeAllocSize (mid, 0~0x5000)
0x38 (4): dwDecodeX64AllocSize (mid, 0~0x5000)
0x40 (1): uBlockSizes[0] (blocksize_1_exp, small)
0x41 (1): uBlockSizes[1] (blocksize_0_exp, large)
2020-03-01 12:21:15 +01:00
*/