diff --git a/src/meta/bfstm.c b/src/meta/bfstm.c index c7fb775f..229cb2e8 100644 --- a/src/meta/bfstm.c +++ b/src/meta/bfstm.c @@ -3,22 +3,21 @@ /* Regions seem mostly for in-game purposes and are not very listenable on its own. - * Also, sample start is slightly off since vgmstream can't start in the middle of block ATM. + * Also, sample start is slightly off since vgmstream can't start in the middle of a frame ATM. * Otherwise this kinda works, but for now it's just a test. */ -#define BFSTM_ENABLE_REGION_SUBSONGS 0 -#define BFSTM_ENABLE_REGION_FORCE_LOOPS 0 /* this makes sense in SM3D World, but not in Zelda BotW) */ +#define BFSTM_ENABLE_REGION_SUBSONGS 1 #if BFSTM_ENABLE_REGION_SUBSONGS -static off_t bfstm_set_regions(STREAMFILE *streamFile, VGMSTREAM *vgmstream, int region_count, off_t regn_offset, int codec, int big_endian); +static off_t bfstm_set_regions(STREAMFILE* sf, VGMSTREAM* vgmstream, int region_count, off_t regn_offset, int codec, int big_endian); #endif -/* BFSTM - Nintendo Wii U format */ -VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { - VGMSTREAM * vgmstream = NULL; +/* BFSTM - Nintendo Wii U/Switch format */ +VGMSTREAM* init_vgmstream_bfstm(STREAMFILE* sf) { + VGMSTREAM* vgmstream = NULL; off_t start_offset; off_t info_offset = 0, data_offset = 0; - int channel_count, loop_flag, codec; + int channels, loop_flag, codec; int big_endian; int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; @@ -29,20 +28,20 @@ VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { /* checks */ - if ( !check_extensions(streamFile,"bfstm") ) + if (!check_extensions(sf,"bfstm")) goto fail; - - /* FSTM header */ - if (read_32bitBE(0x00, streamFile) != 0x4653544D) /* "FSTM" */ + if (!is_id32be(0x00,sf, "FSTM")) goto fail; - /* 0x06(2): header size (0x40), 0x08: version (0x00000400), 0x0c: file size */ + /* 0x06(2): header size (0x40) + * 0x08: version (0x00000400) + * 0x0c: file size */ /* check BOM */ - if ((uint16_t)read_16bitBE(0x04, streamFile) == 0xFEFF) { /* Wii U games */ + if (read_u16be(0x04, sf) == 0xFEFF) { /* Wii U games */ read_32bit = read_32bitBE; read_16bit = read_16bitBE; big_endian = 1; - } else if ((uint16_t)read_16bitBE(0x04, streamFile) == 0xFFFE) { /* Switch games */ + } else if (read_u16be(0x04, sf) == 0xFFFE) { /* Switch games */ read_32bit = read_32bitLE; read_16bit = read_16bitLE; big_endian = 0; @@ -53,18 +52,18 @@ VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { /* get sections (should always appear in the same order) */ { int i; - int section_count = read_16bit(0x10, streamFile); + int section_count = read_16bit(0x10, sf); for (i = 0; i < section_count; i++) { /* 0x00: id, 0x02(2): padding, 0x04(4): offset, 0x08(4): size */ - uint16_t section_id = read_16bit(0x14 + i*0xc+0x00, streamFile); + uint16_t section_id = read_16bit(0x14 + i*0xc+0x00, sf); switch(section_id) { - case 0x4000: info_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); break; - case 0x4001: /* seek_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); */ break; - case 0x4002: data_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); break; + case 0x4000: info_offset = read_32bit(0x14+i*0x0c+0x04, sf); break; + case 0x4001: /* seek_offset = read_32bit(0x14+i*0x0c+0x04, sf); */ break; + case 0x4002: data_offset = read_32bit(0x14+i*0x0c+0x04, sf); break; #if BFSTM_ENABLE_REGION_SUBSONGS - case 0x4003: regn_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); break; + case 0x4003: regn_offset = read_32bit(0x14+i*0x0c+0x04, sf); break; #endif - case 0x4004: /* pdat_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); */ break; /* prefetch data */ + case 0x4004: /* pdat_offset = read_32bit(0x14+i*0x0c+0x04, sf); */ break; /* prefetch data */ default: break; } @@ -75,13 +74,13 @@ VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { } /* INFO section */ - if (read_32bitBE(info_offset, streamFile) != 0x494E464F) /* "INFO" */ + if (read_32bitBE(info_offset, sf) != 0x494E464F) /* "INFO" */ goto fail; - codec = read_8bit(info_offset + 0x20, streamFile); - loop_flag = read_8bit(info_offset + 0x21, streamFile); - channel_count = read_8bit(info_offset + 0x22, streamFile); + codec = read_8bit(info_offset + 0x20, sf); + loop_flag = read_8bit(info_offset + 0x21, sf); + channels = read_8bit(info_offset + 0x22, sf); #if BFSTM_ENABLE_REGION_SUBSONGS - region_count = read_8bit(info_offset + 0x23, streamFile); + region_count = read_8bit(info_offset + 0x23, sf); #endif @@ -89,18 +88,18 @@ VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count, loop_flag); + vgmstream = allocate_vgmstream(channels, loop_flag); if (!vgmstream) goto fail; - vgmstream->sample_rate = read_32bit(info_offset + 0x24, streamFile); - vgmstream->num_samples = read_32bit(info_offset + 0x2c, streamFile); - vgmstream->loop_start_sample = read_32bit(info_offset + 0x28, streamFile); + vgmstream->sample_rate = read_32bit(info_offset + 0x24, sf); + vgmstream->num_samples = read_32bit(info_offset + 0x2c, sf); + vgmstream->loop_start_sample = read_32bit(info_offset + 0x28, sf); vgmstream->loop_end_sample = vgmstream->num_samples; vgmstream->meta_type = meta_FSTM; - vgmstream->layout_type = (channel_count == 1) ? layout_none : layout_interleave; - vgmstream->interleave_block_size = read_32bit(info_offset + 0x34, streamFile); - vgmstream->interleave_last_block_size = read_32bit(info_offset + 0x44, streamFile); + vgmstream->layout_type = (channels == 1) ? layout_none : layout_interleave; + vgmstream->interleave_block_size = read_32bit(info_offset + 0x34, sf); + vgmstream->interleave_last_block_size = read_32bit(info_offset + 0x44, sf); switch(codec) { case 0x00: @@ -116,13 +115,13 @@ VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { int i, c; off_t channel_indexes, channel_info_offset, coefs_offset; - channel_indexes = info_offset+0x08 + read_32bit(info_offset + 0x1C, streamFile); + channel_indexes = info_offset+0x08 + read_32bit(info_offset + 0x1C, sf); for (i = 0; i < vgmstream->channels; i++) { - channel_info_offset = channel_indexes + read_32bit(channel_indexes+0x04+(i*0x08)+0x04, streamFile); - coefs_offset = channel_info_offset + read_32bit(channel_info_offset+0x04, streamFile); + channel_info_offset = channel_indexes + read_32bit(channel_indexes+0x04+(i*0x08)+0x04, sf); + coefs_offset = channel_info_offset + read_32bit(channel_info_offset+0x04, sf); for (c = 0; c < 16; c++) { - vgmstream->ch[i].adpcm_coef[c] = read_16bit(coefs_offset + c*2, streamFile); + vgmstream->ch[i].adpcm_coef[c] = read_16bit(coefs_offset + c*2, sf); } } } @@ -134,10 +133,10 @@ VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) { #if BFSTM_ENABLE_REGION_SUBSONGS - start_offset += bfstm_set_regions(streamFile, vgmstream, region_count, regn_offset, codec, big_endian); + start_offset += bfstm_set_regions(sf, vgmstream, region_count, regn_offset, codec, big_endian); #endif - if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) + if (!vgmstream_open_stream(vgmstream,sf,start_offset)) goto fail; return vgmstream; @@ -151,17 +150,16 @@ fail: /* Newer .bfstm may have multiple regions, that are sample sections of some meaning, * like loop parts (Super Mario 3D World), or dynamic subsongs (Zelda: BotW) * We'll hack them in as subsongs (though seem mostly activated by game events) */ -static off_t bfstm_set_regions(STREAMFILE *streamFile, VGMSTREAM *vgmstream, int region_count, off_t regn_offset, int codec, int big_endian) { +static off_t bfstm_set_regions(STREAMFILE* sf, VGMSTREAM* vgmstream, int region_count, off_t regn_offset, int codec, int big_endian) { off_t start_offset; size_t stream_size; - int total_subsongs, target_subsong = streamFile->stream_index; - int32_t (*read_32bit)(off_t,STREAMFILE*) = big_endian ? read_32bitBE : read_32bitLE; - int16_t (*read_16bit)(off_t,STREAMFILE*) = big_endian ? read_16bitLE : read_16bitLE; - + int total_subsongs, target_subsong = sf->stream_index; + int32_t (*read_s32)(off_t,STREAMFILE*) = big_endian ? read_s32be : read_s32le; + int16_t (*read_s16)(off_t,STREAMFILE*) = big_endian ? read_s16be : read_s16le; if (region_count <= 0 && regn_offset == 0 && codec != 0x02) goto fail; - if (read_32bitBE(regn_offset, streamFile) != 0x5245474E) /* "REGN" */ + if (!is_id32be(regn_offset, sf, "REGN")) goto fail; /* pretend each region is a subsong, but use first subsong as the whole file, @@ -171,61 +169,74 @@ static off_t bfstm_set_regions(STREAMFILE *streamFile, VGMSTREAM *vgmstream, int if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; if (target_subsong > 1) { - int sample_aligned = 0, sample_skip = 0; - int i; - off_t region_start, region_end; - size_t block_size; - size_t sample_start = read_32bit(regn_offset + 0x20 + (target_subsong-2)*0x100+0x00, streamFile); - size_t sample_end = read_32bit(regn_offset + 0x20 + (target_subsong-2)*0x100+0x04, streamFile) + 1; - off_t adpcm_offset = regn_offset + 0x20 + (target_subsong-2)*0x100+0x08; + int ch; + off_t region_start, region_end, block_size; + /* target region info */ + int32_t sample_start = read_s32(regn_offset + 0x20 + (target_subsong-2)*0x100 + 0x00, sf); + int32_t sample_end = read_s32(regn_offset + 0x20 + (target_subsong-2)*0x100 + 0x04, sf) + 1; + off_t adpcm_offset = regn_offset + 0x20 + (target_subsong-2)*0x100 + 0x08; /* rest is padding up to 0x100 */ - /* samples-to-bytes, approximate since samples could land in the middle of a 0x08 frame */ + + /* samples-to-bytes, approximate since regions' samples can land in the middle of a 0x08 frame */ + if (sample_start % 14) { + /* can't decode in the middle of a nibble ATM so align to frame */ + //VGM_LOG("BFSTM: sample align %i, %i\n", sample_start % 14, sample_end % 14); + sample_start -= sample_start % 14; + sample_end -= sample_start % 14; + } region_start = sample_start / 14 * vgmstream->channels * 0x08; region_end = sample_end / 14 * vgmstream->channels * 0x08; stream_size = region_end - region_start; //;VGM_LOG("BFSTM: region offset start=%lx, end=%lx\n", region_start, region_end); - /* align to block start or interleave causes funny sounds, but the bigger the interleave - * the less accurate this is (with 0x2000 align can be off by ~4600 samples per channel) */ - //todo could be fixed with interleave_first_block - block_size = (vgmstream->interleave_block_size*vgmstream->channels); + /* adjust region to closest block + use interleave first to correctly skip to first sample */ + block_size = (vgmstream->interleave_block_size * vgmstream->channels); if (region_start % block_size) { - region_start -= region_start % block_size; /* now aligned */ - //;VGM_LOG("BFSTM: new region start=%lx\n", region_start); + off_t region_skip = (region_start % block_size); + //;VGM_LOG("BFSTM: new region start=%lx - %lx\n", region_start, region_skip); - /* get position of our block (close but smaller than sample_start) */ - sample_aligned = dsp_bytes_to_samples(region_start, vgmstream->channels); - /* and how many samples to skip until actual sample_start */ - sample_skip = (sample_start - sample_aligned); + /* use interleave first to skip to correct offset */ + vgmstream->interleave_first_block_size = (block_size - region_skip) / vgmstream->channels; + vgmstream->interleave_first_skip = (region_skip) / vgmstream->channels; + region_start = region_start - region_skip + vgmstream->interleave_first_skip; /* now aligned */ } - //;VGM_LOG("BFSTM: region align=%i, skip=%i, start=%i, end=%i\n", sample_aligned, sample_skip, sample_start, sample_end); + //;VGM_LOG("BFSTM: region=%lx, adpcm=%lx, start=%i, end=%i\n", region_start, adpcm_offset, sample_start, sample_end); start_offset = region_start; - if (sample_end != vgmstream->num_samples) /* not exact but... */ - vgmstream->interleave_last_block_size = 0; + /* sample_end doesn't fall in last block, interleave last doesn't apply */ + { + int32_t block_samples = dsp_bytes_to_samples(block_size, vgmstream->channels); + int32_t samples_align = vgmstream->num_samples / block_samples * block_samples; + if (sample_end < samples_align) + vgmstream->interleave_last_block_size = 0; + } - vgmstream->num_samples = sample_skip + (sample_end - sample_start); - vgmstream->loop_start_sample = sample_skip; + vgmstream->num_samples = /*sample_skip +*/ (sample_end - sample_start); +#if 0 + /* this makes sense in SM3D World, but not in Zelda BotW */ + vgmstream->loop_start_sample = 0;; vgmstream->loop_end_sample = vgmstream->num_samples; -#if BFSTM_ENABLE_REGION_FORCE_LOOPS vgmstream_force_loop(vgmstream, 1, vgmstream->loop_start_sample, vgmstream->loop_end_sample); #endif /* maybe loops should be disabled with some regions? */ - /* this won't make sense after aligning, whatevs, doesn't sound too bad */ - for (i = 0; i < vgmstream->channels; i++) { - vgmstream->ch[i].adpcm_history1_16 = read_16bit(adpcm_offset+0x02+0x00, streamFile); - vgmstream->ch[i].adpcm_history2_16 = read_16bit(adpcm_offset+0x02+0x02, streamFile); + /* region_start points to correct frame (when compared to ADPCM predictor), but not sure if hist + * is for exact nibble rather than first (sounds ok though) */ + for (ch = 0; ch < vgmstream->channels; ch++) { + /* 0x00: ADPCM predictor */ + vgmstream->ch[ch].adpcm_history1_16 = read_s16(adpcm_offset + 0x02 + 0x06*ch, sf); + vgmstream->ch[ch].adpcm_history2_16 = read_s16(adpcm_offset + 0x04 + 0x06*ch, sf); } } else { start_offset = 0; - stream_size = get_streamfile_size(streamFile); + stream_size = get_streamfile_size(sf); } - vgmstream->num_streams = total_subsongs; + /* for now don't show subsongs since some regions are a bit strange */ + //vgmstream->num_streams = total_subsongs; vgmstream->stream_size = stream_size; return start_offset;