From 45f14a8a7b1e0f6ce0797aba5e1b01a2bec93458 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 18 Dec 2021 12:17:47 +0100 Subject: [PATCH 1/3] Fix some Ubi Jade .waa/wac/wam [King Kong GE (PC)] --- src/meta/ubi_jade.c | 295 +++++++++++++++++++++++--------------------- 1 file changed, 155 insertions(+), 140 deletions(-) diff --git a/src/meta/ubi_jade.c b/src/meta/ubi_jade.c index dec62867..d0936bfb 100644 --- a/src/meta/ubi_jade.c +++ b/src/meta/ubi_jade.c @@ -1,106 +1,116 @@ #include "meta.h" +#include "../util/chunks.h" #include "../coding/coding.h" -static int get_loop_points(STREAMFILE* sf, int* p_loop_start, int* p_loop_end); +static int get_loop_points(STREAMFILE* sf, uint32_t cue_offset, uint32_t cue_size, uint32_t list_offset, uint32_t list_size, int* p_loop_start, int* p_loop_end); /* Jade RIFF - from Ubisoft Jade engine games [Beyond Good & Evil (multi), Rayman Raving Rabbids 1/2 (multi)] */ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { VGMSTREAM* vgmstream = NULL; - off_t start_offset, first_offset = 0xc; - off_t fmt_offset, data_offset; - size_t fmt_size, data_size; - int loop_flag, channel_count, sample_rate, codec, block_size; + uint32_t fmt_offset = 0, fmt_size = 0, data_offset = 0, data_size = 0; + uint32_t cue_offset = 0, cue_size = 0, list_offset = 0, list_size = 0; + int loop_flag = 0, channels = 0, sample_rate = 0, codec = 0, block_size = 0; int loop_start = 0, loop_end = 0; int is_jade_v2 = 0; /* checks */ - /* .waa: ambiances, .wam: music, .wac: sfx, .wad: dialogs (usually) - * .wav: Beyond Good & Evil HD (PS3) - * .psw: fake/badly extracted names [ex. Rayman Raving Rabbids (PS2)] */ - if (!check_extensions(sf,"waa,wac,wad,wam,wav,lwav,psw")) + if (!is_id32be(0x00,sf, "RIFF")) + goto fail; + if (read_u32le(0x04,sf) + 0x04 + 0x04 != get_streamfile_size(sf)) + goto fail; + if (!is_id32be(0x08,sf, "WAVE")) + goto fail; + + /* .waa: ambiances / .wam: music / .wac: sfx / .wad: dialogs (usually) + * .wav: Beyond Good & Evil HD (PS3) */ + if (!check_extensions(sf,"waa,wac,wad,wam,wav,lwav")) goto fail; /* a slightly twisted RIFF with custom codecs */ - if (!is_id32be(0x00,sf, "RIFF") || - !is_id32be(0x08,sf, "WAVE")) - goto fail; - - if (check_extensions(sf,"psw")) { /* .psw are incorrectly extracted missing 0x04 at the end */ - if (read_32bitLE(0x04,sf)+0x04 != get_streamfile_size(sf)) - goto fail; - } - else { - if (read_32bitLE(0x04,sf)+0x04+0x04 != get_streamfile_size(sf)) - goto fail; - } - - if (!find_chunk(sf, 0x666d7420,first_offset,0, &fmt_offset,&fmt_size, 0, 0)) /* "fmt " */ - goto fail; - if (!find_chunk(sf, 0x64617461,first_offset,0, &data_offset,&data_size, 0, 0)) /* "data" */ - goto fail; - - /* ignore LyN RIFF (needed as codec 0xFFFE is reused) */ + /* parse chunks (reads once linearly) */ { - off_t fact_offset; - size_t fact_size; + chunk_t rc = {0}; - if (find_chunk(sf, 0x66616374,first_offset,0, &fact_offset,&fact_size, 0, 0)) { /* "fact" */ - if (fact_size == 0x10 && read_32bitBE(fact_offset+0x04, sf) == 0x4C794E20) /* "LyN " */ - goto fail; /* parsed elsewhere */ - /* Jade doesn't use "fact", though */ - } - } + rc.current = 0x0c; + while (next_chunk(&rc, sf)) { + switch(rc.type) { + case 0x666d7420: /* "fmt " */ + fmt_offset = rc.offset; + fmt_size = rc.size; - /* parse format */ - { - if (fmt_size < 0x10) - goto fail; - codec = (uint16_t)read_16bitLE(fmt_offset+0x00,sf); - channel_count = read_16bitLE(fmt_offset+0x02,sf); - sample_rate = read_32bitLE(fmt_offset+0x04,sf); - block_size = (uint16_t)read_16bitLE(fmt_offset+0x0c,sf); - /* 0x08: average bytes, 0x0e: bps, etc */ + if (fmt_size < 0x10) /* min 0x10: MSF, 0x12: common, 0x32: MSADPCM */ + goto fail; + codec = read_u16le(fmt_offset+0x00,sf); + channels = read_u16le(fmt_offset+0x02,sf); + sample_rate = read_s32le(fmt_offset+0x04,sf); + block_size = read_u16le(fmt_offset+0x0c,sf); + /* 0x08: average bytes, 0x0e: bps, etc */ + break; - /* autodetect Jade "v2", uses a different interleave [Rayman Raving Rabbids (PS2/Wii)] */ - switch(codec) { - case 0xFFFF: { /* PS2 */ - int i; + case 0x64617461: /* "data" */ + data_offset = rc.offset; + data_size = rc.size; + break; - /* half interleave check as there is no flag (ends with the PS-ADPCM stop frame) */ - for (i = 0; i < channel_count; i++) { - off_t end_frame = data_offset + (data_size / channel_count) * (i+1) - 0x10; - if (read_32bitBE(end_frame+0x00,sf) != 0x07007777 || - read_32bitBE(end_frame+0x04,sf) != 0x77777777 || - read_32bitBE(end_frame+0x08,sf) != 0x77777777 || - read_32bitBE(end_frame+0x0c,sf) != 0x77777777) { - is_jade_v2 = 1; - break; - } - } - break; + case 0x63756520: /* "cue ": catches PC Rabbids (hopefully) */ + is_jade_v2 = 1; + cue_offset = rc.offset; + cue_size = rc.size; + break; + + case 0x66616374: /* "fact" */ + /* ignore LyN RIFF (needed as codec 0xFFFE is reused, and Jade doesn't set "fact") */ + //if (rc.size == 0x10 && !is_id32be(rc.offset + 0x04, sf, "LyN ")) + // goto fail; /* parsed elsewhere */ + goto fail; + + case 0x4C495354: /* "LIST": labels (rare) */ + list_offset = rc.offset; + list_size = rc.size; + break; + + default: + /* unknown chunk: must be another RIFF */ + goto fail; } - - case 0xFFFE: /* GC/Wii */ - is_jade_v2 = (read_16bitLE(fmt_offset+0x10,sf) == 0); /* extra data size (0x2e*channels) */ - break; - - default: - break; - } - - /* hopefully catches PC Rabbids */ - if (find_chunk(sf, 0x63756520,first_offset,0, NULL,NULL, 0, 0)) { /* "cue " */ - is_jade_v2 = 1; } } + if (!fmt_offset || !fmt_size || !data_offset || !data_size) + goto fail; + + /* autodetect Jade "v2", uses a different interleave [Rayman Raving Rabbids (PS2/Wii)] */ + switch(codec) { + case 0xFFFF: { /* PS2 */ + int i; + + /* half interleave check as there is no flag (ends with the PS-ADPCM stop frame) */ + for (i = 0; i < channels; i++) { + uint32_t end_frame = data_offset + (data_size / channels) * (i+1) - 0x10; + if (read_u32be(end_frame+0x00,sf) != 0x07007777 || + read_u32be(end_frame+0x04,sf) != 0x77777777 || + read_u32be(end_frame+0x08,sf) != 0x77777777 || + read_u32be(end_frame+0x0c,sf) != 0x77777777) { + is_jade_v2 = 1; + break; + } + } + break; + } + + case 0xFFFE: /* GC/Wii */ + is_jade_v2 = (read_u16le(fmt_offset+0x10,sf) == 0); /* extra data size (0x2e*channels) */ + break; + + default: + break; + } - /* get loop points */ if (is_jade_v2) { - loop_flag = get_loop_points(sf, &loop_start, &loop_end); /* loops in "LIST" */ + loop_flag = get_loop_points(sf, cue_offset, cue_size, list_offset, list_size, &loop_start, &loop_end); /* loops in "LIST" */ } else { /* BG&E files don't contain looping information, so the looping is done by extension. @@ -109,15 +119,13 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { loop_flag = check_extensions(sf,"waa,wam"); } - start_offset = data_offset; - /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count,loop_flag); + vgmstream = allocate_vgmstream(channels, loop_flag); if (!vgmstream) goto fail; - vgmstream->sample_rate = sample_rate; vgmstream->meta_type = meta_UBI_JADE; + vgmstream->sample_rate = sample_rate; if (is_jade_v2) { vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end; @@ -128,12 +136,12 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { case 0x0069: /* Xbox */ /* Peter Jackson's King Kong uses 0x14 (other versions don't) */ if (fmt_size != 0x12 && fmt_size != 0x14) goto fail; - if (block_size != 0x24*channel_count) goto fail; + if (block_size != 0x24*channels) goto fail; vgmstream->coding_type = coding_XBOX_IMA; vgmstream->layout_type = layout_none; - vgmstream->num_samples = xbox_ima_bytes_to_samples(data_size, channel_count); + vgmstream->num_samples = xbox_ima_bytes_to_samples(data_size, channels); if (!is_jade_v2) { vgmstream->loop_start_sample = 0; vgmstream->loop_end_sample = vgmstream->num_samples; @@ -154,10 +162,10 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { vgmstream->interleave_last_block_size = (data_size % (vgmstream->interleave_block_size*vgmstream->channels)) / vgmstream->channels; } else { - vgmstream->interleave_block_size = data_size / channel_count; + vgmstream->interleave_block_size = data_size / channels; } - vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count); + vgmstream->num_samples = ps_bytes_to_samples(data_size, channels); if (!is_jade_v2) { vgmstream->loop_start_sample = 0; vgmstream->loop_end_sample = vgmstream->num_samples; @@ -172,7 +180,7 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { vgmstream->coding_type = coding_NGC_DSP; vgmstream->layout_type = layout_interleave; - vgmstream->num_samples = dsp_bytes_to_samples(data_size, channel_count); + vgmstream->num_samples = dsp_bytes_to_samples(data_size, channels); if (!is_jade_v2) { vgmstream->loop_start_sample = 0; vgmstream->loop_end_sample = vgmstream->num_samples; @@ -191,7 +199,7 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { }; int i, ch; - for (ch = 0; ch < channel_count; ch++) { + for (ch = 0; ch < channels; ch++) { for (i = 0; i < 16; i++) { vgmstream->ch[ch].adpcm_coef[i] = coef[i]; } @@ -200,23 +208,33 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { } else { /* has extra 0x2e coefs before each channel, not counted in data_size */ - vgmstream->interleave_block_size = (data_size + 0x2e*channel_count) / channel_count; + vgmstream->interleave_block_size = (data_size + 0x2e*channels) / channels; - dsp_read_coefs_be(vgmstream, sf, start_offset+0x00, vgmstream->interleave_block_size); - dsp_read_hist_be (vgmstream, sf, start_offset+0x20, vgmstream->interleave_block_size); - start_offset += 0x2e; + dsp_read_coefs_be(vgmstream, sf, data_offset+0x00, vgmstream->interleave_block_size); + dsp_read_hist_be (vgmstream, sf, data_offset+0x20, vgmstream->interleave_block_size); + data_offset += 0x2e; } break; case 0x0002: /* PC */ - if (fmt_size != 0x12) goto fail; - if (block_size != 0x24*channel_count) goto fail; + if (fmt_size != 0x12 && fmt_size != 0x32) goto fail; + if (block_size != 0x24*channels) goto fail; vgmstream->coding_type = coding_MSADPCM; vgmstream->layout_type = layout_none; vgmstream->frame_size = block_size; - vgmstream->num_samples = msadpcm_bytes_to_samples(data_size, vgmstream->frame_size, channel_count); + /* King Kong: Gamers Edition (PC) */ + if (fmt_size == 0x32) { + /* standard WAVEFORMATEX must write extra size here, Jade sets 0 */ + if (read_u16le(fmt_offset + 0x10, sf) != 0) + goto fail; + /* 0x12: block samples */ + if (!msadpcm_check_coefs(sf, fmt_offset + 0x14)) + goto fail; + } + + vgmstream->num_samples = msadpcm_bytes_to_samples(data_size, vgmstream->frame_size, channels); if (!is_jade_v2) { vgmstream->loop_start_sample = 0; vgmstream->loop_end_sample = vgmstream->num_samples; @@ -225,17 +243,17 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { break; case 0x0001: { /* PS3 */ - VGMSTREAM *temp_vgmstream = NULL; - STREAMFILE *temp_sf = NULL; + VGMSTREAM* temp_vgmstream = NULL; + STREAMFILE* temp_sf = NULL; if (fmt_size != 0x10) goto fail; - if (block_size != 0x02*channel_count) goto fail; + if (block_size != 0x02 * channels) goto fail; /* a MSF (usually ATRAC3) masquerading as PCM */ - if (read_32bitBE(start_offset, sf) != 0x4D534643) /* "MSF\43" */ + if (!is_id32be(data_offset, sf, "MSFC")) goto fail; - temp_sf = setup_subfile_streamfile(sf, start_offset, data_size, "msf"); + temp_sf = setup_subfile_streamfile(sf, data_offset, data_size, "msf"); if (!temp_sf) goto fail; temp_vgmstream = init_vgmstream_msf(temp_sf); @@ -260,7 +278,7 @@ VGMSTREAM* init_vgmstream_ubi_jade(STREAMFILE* sf) { } - if (!vgmstream_open_stream(vgmstream, sf,start_offset)) + if (!vgmstream_open_stream(vgmstream, sf, data_offset)) goto fail; return vgmstream; @@ -270,58 +288,54 @@ fail: } /* extract loops from "cue /LIST", returns if loops (info from Droolie) */ -static int get_loop_points(STREAMFILE *sf, int *out_loop_start, int *out_loop_end) { - off_t cue_offset, list_offset; - size_t cue_size, list_size; - off_t offset, first_offset = 0x0c; +static int get_loop_points(STREAMFILE* sf, uint32_t cue_offset, uint32_t cue_size, uint32_t list_offset, uint32_t list_size, int* p_loop_start, int* p_loop_end) { + //off_t offset; int i, cue_count, loop_id = 0, loop_start = 0, loop_end = 0; - + chunk_t rc = {0}; /* unlooped files may contain LIST, but also may not */ - if (!find_chunk(sf, 0x63756520,first_offset,0, &cue_offset,&cue_size, 0, 0)) /* "cue " */ - goto fail; - if (!find_chunk(sf, 0x4C495354,first_offset,0, &list_offset,&list_size, 0, 0)) /* "LIST" */ + if (!cue_offset || !cue_size || !list_offset || !list_size) goto fail; - offset = list_offset + 0x04; - while (offset < list_offset + list_size) { - uint32_t chunk_id = read_32bitBE(offset+0x00, sf); - uint32_t chunk_size = read_32bitLE(offset+0x04, sf); - offset += 0x08; - - switch(chunk_id) { + rc.current = list_offset + 0x04; /* skip "adtl" */ + rc.max = list_offset + list_size; + while (next_chunk(&rc, sf)) { + switch(rc.type) { case 0x6C61626C: /* "labl" */ - if (read_32bitBE(offset+0x04, sf) == 0x6C6F6F70) /* "loop", actually an string tho */ - loop_id = read_32bitLE(offset+0x00, sf); - chunk_size += (chunk_size % 2) ? 1 : 0; /* string is even-padded after size */ + if (is_id32be(rc.offset + 0x04, sf, "loop")) /* actually a C-string tho */ + loop_id = read_u32le(rc.offset + 0x00, sf); + + if (rc.size % 2) { /* string is even-padded after size */ + rc.size++; + rc.current++; + } break; + case 0x6C747874: /* "ltxt" */ - if (loop_id == read_32bitLE(offset+0x00, sf)) - loop_end = read_32bitLE(offset+0x04, sf); + if (loop_id == read_u32le(rc.offset + 0x00, sf)) + loop_end = read_u32le(rc.offset + 0x04, sf); break; default: - VGM_LOG("Jade: unknown LIST chunk\n"); + VGM_LOG("UBI JADE: unknown LIST chunk\n"); goto fail; } - - offset += chunk_size; } if (!loop_end) return 0; - cue_count = read_32bitLE(cue_offset+0x00, sf); + cue_count = read_u32le(cue_offset+0x00, sf); for (i = 0; i < cue_count; i++) { - if (loop_id == read_32bitLE(cue_offset+0x04 + i*0x18 + 0x00, sf)) { - loop_start = read_32bitLE(cue_offset+0x04 + i*0x18 + 0x04, sf); + if (loop_id == read_u32le(cue_offset+0x04 + i*0x18 + 0x00, sf)) { + loop_start = read_u32le(cue_offset+0x04 + i*0x18 + 0x04, sf); loop_end += loop_start; break; } } - *out_loop_start = loop_start; - *out_loop_end = loop_end; + *p_loop_start = loop_start; + *p_loop_end = loop_end; return 1; fail: @@ -340,23 +354,19 @@ VGMSTREAM* init_vgmstream_ubi_jade_container(STREAMFILE* sf) { * the RIFF + padding after. Most extractors don't remove the padding correctly, so here we add support. */ /* checks */ - /* standard Jade exts + .xma for padded XMA used in Beyond Good & Evil HD (X360) */ - if (!check_extensions(sf,"waa,wac,wad,wam,wav,lwav,xma")) - goto fail; - - if (read_32bitBE(0x04,sf) == 0x52494646 && - read_32bitLE(0x00,sf)+0x04 == get_streamfile_size(sf)) { + if (is_id32be(0x04,sf, "RIFF") && + read_u32le(0x00,sf)+0x04 == get_streamfile_size(sf)) { /* data size + RIFF + padding */ subfile_offset = 0x04; } - else if (read_32bitBE(0x00,sf) == 0x52494646 && - read_32bitLE(0x04,sf)+0x04+0x04 < get_streamfile_size(sf) && + else if (is_id32be(0x00,sf, "RIFF") && + read_u32le(0x04,sf) + 0x04 + 0x04 < get_streamfile_size(sf) && (get_streamfile_size(sf) + 0x04) % 0x800 == 0) { /* RIFF + padding with data size removed (bad extraction) */ subfile_offset = 0x00; } - else if (read_32bitBE(0x04,sf) == 0x52494646 && - read_32bitLE(0x00,sf) == get_streamfile_size(sf)) { + else if (is_id32be(0x04,sf, "RIFF") && + read_u32le(0x00,sf) == get_streamfile_size(sf)) { /* data_size + RIFF + padding - 0x04 (bad extraction) */ subfile_offset = 0x04; } @@ -364,14 +374,19 @@ VGMSTREAM* init_vgmstream_ubi_jade_container(STREAMFILE* sf) { goto fail; } - subfile_size = read_32bitLE(subfile_offset+0x04,sf) + 0x04+0x04; + /* standard Jade exts + .xma for padded XMA used in Beyond Good & Evil HD (X360) */ + if (!check_extensions(sf,"waa,wac,wad,wam,wav,lwav,xma")) + goto fail; - temp_sf = setup_subfile_streamfile(sf, subfile_offset,subfile_size, NULL); + subfile_size = read_u32le(subfile_offset + 0x04,sf) + 0x04 + 0x04; + + temp_sf = setup_subfile_streamfile(sf, subfile_offset, subfile_size, NULL); if (!temp_sf) goto fail; - if (check_extensions(sf,"xma")) { + if (read_u16le(0x14, sf) == 0x166) { vgmstream = init_vgmstream_xma(temp_sf); - } else { + } + else { vgmstream = init_vgmstream_ubi_jade(temp_sf); } From 599004276f85421115873ce1746c787d98217324 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 18 Dec 2021 12:18:22 +0100 Subject: [PATCH 2/3] Fix some Ubi Lyn with Ogg [Rabbids Go Home (PC)] --- src/meta/ubi_lyn.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/meta/ubi_lyn.c b/src/meta/ubi_lyn.c index eeb162ac..a57574dc 100644 --- a/src/meta/ubi_lyn.c +++ b/src/meta/ubi_lyn.c @@ -139,15 +139,19 @@ VGMSTREAM* init_vgmstream_ubi_lyn(STREAMFILE* sf) { break; #ifdef VGM_USE_VORBIS - case 0x3157: { /* Ogg (PC), interleaved 1ch */ + case 0x3156: /* Ogg (PC), interleaved 1ch (older version) [Rabbids Go Home (PC)] */ + case 0x3157: { /* Ogg (PC), interleaved 1ch (newer version) [Adventures of Tintin (PC)] */ size_t interleave_size; layered_layout_data* data = NULL; int i; - if (read_u32le(start_offset+0x00,sf) != 1) /* id? */ - goto fail; + if (codec == 0x3157) { + if (read_u32le(start_offset+0x00,sf) != 1) /* id? */ + goto fail; + start_offset += 0x04; + } - interleave_size = read_u32le(start_offset+0x04,sf); + interleave_size = read_u32le(start_offset+0x00,sf); /* interleave is adjusted so there is no smaller last block, it seems */ vgmstream->coding_type = coding_OGG_VORBIS; @@ -161,8 +165,8 @@ VGMSTREAM* init_vgmstream_ubi_lyn(STREAMFILE* sf) { /* open each layer subfile */ for (i = 0; i < channels; i++) { STREAMFILE* temp_sf = NULL; - size_t logical_size = read_u32le(start_offset+0x08 + 0x04*i,sf); - off_t layer_offset = start_offset + 0x08 + 0x04*channels; //+ interleave_size*i; + size_t logical_size = read_u32le(start_offset+0x04 + 0x04*i,sf); + off_t layer_offset = start_offset + 0x04 + 0x04*channels; temp_sf = setup_ubi_lyn_streamfile(sf, layer_offset, interleave_size, i, channels, logical_size); if (!temp_sf) goto fail; From b40e48128fe2c405ed9211ec11d7ab491e7ab6c1 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 18 Dec 2021 12:18:36 +0100 Subject: [PATCH 3/3] Remove .psw fake extension (use waa/wac/wam) --- src/formats.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/formats.c b/src/formats.c index 49b20582..e762af24 100644 --- a/src/formats.c +++ b/src/formats.c @@ -401,7 +401,6 @@ static const char* extension_list[] = { "psf", "psh", //fake extension for .vsv (to be removed) "psnd", - "psw", //fake extension for .wam (renamed, to be removed) "r", "rac", //txth/reserved [Manhunt (Xbox)]