Clean .bfstm and add region debug code

This commit is contained in:
bnnm 2018-08-14 20:25:17 +02:00
parent 12167c7318
commit 985a3aead6
2 changed files with 168 additions and 144 deletions

View File

@ -677,7 +677,7 @@ static const meta_info meta_info_list[] = {
{meta_AST, "Nintendo AST header"},
{meta_HALPST, "HAL Laboratory HALPST header"},
{meta_DSP_RS03, "Retro Studios RS03 header"},
{meta_DSP_STD, "Standard Nintendo DSP header"},
{meta_DSP_STD, "Nintendo DSP header"},
{meta_DSP_CSTR, "Namco Cstr header"},
{meta_GCSW, "GCSW header"},
{meta_PS2_SShd, "Sony ADS header"},
@ -702,7 +702,7 @@ static const meta_info meta_info_list[] = {
{meta_DSP_MSS, "Double DSP header stereo by .mss extension"},
{meta_DSP_GCM, "Double DSP header stereo by .gcm extension"},
{meta_IDSP_TT, "Traveller's Tales IDSP header"},
{meta_RSTM_SPM, "Nintendo RSTM header and .brstmspm extension"},
{meta_RSTM_SPM, "Nintendo RSTM header (brstmspm)"},
{meta_RAW, "assumed RAW PCM file by .raw extension"},
{meta_PS2_VAGi, "Sony VAG Interleaved header (VAGi)"},
{meta_PS2_VAGp, "Sony VAG Mono header (VAGp)"},
@ -965,8 +965,8 @@ static const meta_info meta_info_list[] = {
{meta_UBI_CKD, "Ubisoft CKD RIFF header"},
{meta_PS2_VBK, "PS2 VBK Header"},
{meta_OTM, "Otomedius OTM Header"},
{meta_CSTM, "Nintendo 3DS CSTM Header"},
{meta_FSTM, "Nintendo Wii U FSTM Header"},
{meta_CSTM, "Nintendo CSTM Header"},
{meta_FSTM, "Nintendo FSTM Header"},
{meta_KT_WIIBGM, "Koei Tecmo WiiBGM Header"},
{meta_KTSS, "Koei Tecmo Nintendo Stream KTSS Header"},
{meta_3DS_IDSP, "Nintendo IDSP Header"},

View File

@ -1,171 +1,195 @@
#include "meta.h"
#include "../util.h"
#include "../stack_alloc.h"
/* BFSTM - Nintendo Wii U format */
VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
coding_t coding_type;
coding_t coding_PCM16;
VGMSTREAM * vgmstream = NULL;
off_t start_offset;
off_t info_offset = 0, data_offset = 0; //, regn_offset = 0;
int channel_count, loop_flag, codec; //, region_count;
int big_endian;
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL;
off_t info_offset = 0, seek_offset = 0, data_offset = 0;
uint16_t temp_id;
int codec_number;
int channel_count, loop_flag;
int i, j;
int ima = 0;
off_t start_offset;
off_t tempoffset1;
int section_count;
/* check extension, case insensitive */
if ( !check_extensions(streamFile,"bfstm") )
goto fail;
/* checks */
if ( !check_extensions(streamFile,"bfstm") )
goto fail;
/* check header */
if ((uint32_t)read_32bitBE(0, streamFile) != 0x4653544D) /* "FSTM" */
goto fail;
/* FSTM header */
if (read_32bitBE(0x00, streamFile) != 0x4653544D) /* "FSTM" */
goto fail;
/* 0x06(2): header size (0x40), 0x08: version (0x00000400), 0x0c: file size */
if ((uint16_t)read_16bitBE(4, streamFile) == 0xFEFF) { /* endian marker (BE most common) */
/* check BOM */
if ((uint16_t)read_16bitBE(0x04, streamFile) == 0xFEFF) { /* Wii U games */
read_32bit = read_32bitBE;
read_16bit = read_16bitBE;
coding_PCM16 = coding_PCM16BE;
} else if ((uint16_t)read_16bitBE(4, streamFile) == 0xFFFE) { /* Blaster Master Zero 3DS */
big_endian = 1;
} else if ((uint16_t)read_16bitBE(0x04, streamFile) == 0xFFFE) { /* Switch games */
read_32bit = read_32bitLE;
read_16bit = read_16bitLE;
coding_PCM16 = coding_PCM16LE;
} else {
goto fail;
}
big_endian = 0;
} else {
goto fail;
}
section_count = read_16bit(0x10, streamFile);
for (i = 0; i < section_count; i++) {
temp_id = read_16bit(0x14 + i * 0xc, streamFile);
switch(temp_id) {
case 0x4000:
info_offset = read_32bit(0x18 + i * 0xc, streamFile);
/* size_t info_size = read_32bit(0x1c + i * 0xc, streamFile); */
break;
case 0x4001:
seek_offset = read_32bit(0x18 + i * 0xc, streamFile);
/* size_t seek_size = read_32bit(0x1c + i * 0xc, streamFile); */
break;
case 0x4002:
data_offset = read_32bit(0x18 + i * 0xc, streamFile);
/* size_t data_size = read_32bit(0x1c + i * 0xc, streamFile); */
break;
case 0x4003:
/* off_t regn_offset = read_32bit(0x18 + i * 0xc, streamFile); */
/* size_t regn_size = read_32bit(0x1c + i * 0xc, streamFile); */
break;
case 0x4004:
/* off_t pdat_offset = read_32bit(0x18 + i * 0xc, streamFile); */
/* size_t pdat_size = read_32bit(0x1c + i * 0xc, streamFile); */
break;
default:
break;
}
}
if (info_offset == 0) goto fail;
if ((uint32_t)read_32bitBE(info_offset, streamFile) != 0x494E464F) /* "INFO" */
goto fail;
/* get sections (should always appear in the same order) */
{
int i;
int section_count = read_16bit(0x10, streamFile);
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);
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 0x4003: /* regn_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); */ break;
case 0x4004: /* pdat_offset = read_32bit(0x14+i*0x0c+0x04, streamFile); */ break; /* prefetch data */
default:
break;
}
}
if (info_offset == 0 || data_offset == 0)
goto fail;
}
/* INFO section */
if (read_32bitBE(info_offset, streamFile) != 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);
//region_count = read_8bit(info_offset + 0x23, streamFile);
/* check type details */
codec_number = read_8bit(info_offset + 0x20, streamFile);
loop_flag = read_8bit(info_offset + 0x21, streamFile);
channel_count = read_8bit(info_offset + 0x22, streamFile);
start_offset = data_offset + 0x20;
switch (codec_number) {
case 0:
coding_type = coding_PCM8;
break;
case 1:
coding_type = coding_PCM16;
break;
case 2:
coding_type = coding_NGC_DSP;
break;
default:
goto fail;
}
if (channel_count < 1) goto fail;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, loop_flag);
if (!vgmstream) goto fail;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, 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->loop_end_sample = vgmstream->num_samples;
/* fill in the vital statistics */
vgmstream->num_samples = read_32bit(info_offset + 0x2c, streamFile);
vgmstream->sample_rate = read_32bit(info_offset + 0x24, streamFile);
/* channels and loop flag are set by allocate_vgmstream */
if (ima) //Shift the loop points back slightly to avoid stupid pops in some IMA streams due to DC offsetting
{
vgmstream->loop_start_sample = read_32bit(info_offset + 0x28, streamFile);
if (vgmstream->loop_start_sample > 10000)
{
vgmstream->loop_start_sample -= 5000;
vgmstream->loop_end_sample = vgmstream->num_samples - 5000;
}
else
vgmstream->loop_end_sample = vgmstream->num_samples;
}
else
{
vgmstream->loop_start_sample = read_32bit(info_offset + 0x28, streamFile);
vgmstream->loop_end_sample = vgmstream->num_samples;
}
vgmstream->coding_type = coding_type;
vgmstream->meta_type = meta_FSTM;
vgmstream->layout_type = (channel_count == 1) ? layout_none : layout_interleave;
vgmstream->meta_type = meta_FSTM;
vgmstream->interleave_block_size = read_32bit(info_offset + 0x34, streamFile);
vgmstream->interleave_last_block_size = read_32bit(info_offset + 0x44, streamFile);
if (ima)
vgmstream->interleave_block_size = 0x200;
else {
vgmstream->interleave_block_size = read_32bit(info_offset + 0x34, streamFile);
vgmstream->interleave_last_block_size = read_32bit(info_offset + 0x44, streamFile);
}
switch(codec) {
case 0x00:
vgmstream->coding_type = coding_PCM8;
break;
case 0x01:
vgmstream->coding_type = big_endian ? coding_PCM16BE : coding_PCM16LE;
break;
case 0x02:
vgmstream->coding_type = coding_NGC_DSP;
if (vgmstream->coding_type == coding_NGC_DSP) {
off_t coeff_ptr_table;
VARDECL(off_t, coef_offset);
ALLOC(coef_offset, channel_count, off_t);
coeff_ptr_table = read_32bit(info_offset + 0x1c, streamFile) + info_offset + 8; // Getting pointer for coefficient pointer table
for (i = 0; i < channel_count; i++) {
tempoffset1 = read_32bit(coeff_ptr_table + 8 + i * 8, streamFile);
coef_offset[i] = tempoffset1 + coeff_ptr_table;
coef_offset[i] += read_32bit(coef_offset[i] + 4, streamFile);
}
for (j = 0; j<vgmstream->channels; j++) {
for (i = 0; i<16; i++) {
vgmstream->ch[j].adpcm_coef[i] = read_16bit(coef_offset[j] + i * 2, streamFile);
}
}
}
{
int i, c;
off_t channel_indexes, channel_info_offset, coefs_offset;
if (ima) { // No SEEK (ADPC) header, so just start where the SEEK header is supposed to be.
if (seek_offset == 0) goto fail;
start_offset = seek_offset;
} else {
if (data_offset == 0) goto fail;
start_offset = data_offset + 0x20;
}
channel_indexes = info_offset+0x08 + read_32bit(info_offset + 0x1C, streamFile);
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);
for (c = 0; c < 16; c++) {
vgmstream->ch[i].adpcm_coef[c] = read_16bit(coefs_offset + c*2, streamFile);
}
}
}
break;
default: /* 0x03: IMA? */
goto fail;
}
/* open the file for reading by each channel */
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
goto fail;
//regions seem mostly for in-game purposes and are not very listenable, otherwise this kinda works
//start_offset += bfstm_set_regions(streamFile, vgmstream, region_count, regn_offset, codec);
return vgmstream;
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
goto fail;
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
close_vgmstream(vgmstream);
return NULL;
}
#if 0
/* 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) {
off_t start_offset = 0;
/* REGN section subsong hack */
if (region_count > 0 && regn_offset != 0 && codec == 0x02) {
size_t stream_size;
int total_subsongs, target_subsong = streamFile->stream_index;
if (read_32bitBE(regn_offset, streamFile) != 0x5245474E) /* "REGN" */
goto fail;
/* pretend each region is a subsong, but use first subsong as the whole file,
* since regions may not map all samples */
total_subsongs = region_count + 1;
if (target_subsong == 0) target_subsong = 1;
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
if (target_subsong > 1) {
int i;
off_t region_start, region_end;
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;
/* rest is padding up to 0x100 */
/* samples-to-bytes, approximate since samples could land in the middle of a frame, meh */
region_start = sample_start / 14 * vgmstream->channels * 0x08;
region_end = sample_end / 14 * vgmstream->channels * 0x08;
stream_size = region_end - region_start;
/* align to blocks or causes funny sounds, but the bigger the interleave the less
* accurate this is (with 0x2000 can be off by ~4600 samples) */
if (region_start % (vgmstream->interleave_block_size*vgmstream->channels))
region_start -= region_start % (vgmstream->interleave_block_size*vgmstream->channels);
start_offset += region_start;
if (sample_end != vgmstream->num_samples) /* not exact but... */
vgmstream->interleave_last_block_size = 0;
vgmstream->num_samples = sample_end - sample_start;
vgmstream->loop_start_sample = 0;
vgmstream->loop_end_sample = vgmstream->num_samples;
/* maybe loops should be enabled/disabled with 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);
}
}
else {
stream_size = get_streamfile_size(streamFile);
}
vgmstream->num_streams = total_subsongs;
vgmstream->stream_size = stream_size;
}
return start_offset;
fail:
return 0;
}
#endif