vgmstream/src/meta/wii_bns.c

151 lines
5.0 KiB
C
Raw Normal View History

#include "meta.h"
#include "../coding/coding.h"
#include "../util.h"
/* BNS - Wii "Banner Sound" disc jingle */
2020-12-19 15:48:15 +01:00
VGMSTREAM* init_vgmstream_wii_bns(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
off_t bns_offset;
uint32_t info_offset = 0, data_offset = 0;
uint32_t channel_info_offset_list_offset;
2020-12-19 15:48:15 +01:00
int channels, loop_flag, sample_rate;
uint32_t sample_count, loop_start;
2020-12-19 15:48:15 +01:00
/* checks */
/* .bin: actual extension
* .bns: header id */
if (!check_extensions(sf, "bin,lbin,bns"))
goto fail;
2020-12-19 15:48:15 +01:00
bns_offset = 0;
if (is_id32be(bns_offset + 0x40, sf, "IMET")) {
/* regular .bnr, find sound.bin offset */
bns_offset = read_u32be(bns_offset + 0x44,sf);
2020-12-19 15:48:15 +01:00
/* tables, probably not ok for all cases */
bns_offset += read_u32be(bns_offset + 0x54,sf);
}
2020-12-19 15:48:15 +01:00
if (is_id32be(bns_offset + 0x00, sf, "IMD5")) {
/* skip IMD5 header if present */
bns_offset += 0x20;
}
2020-12-19 15:48:15 +01:00
if (!is_id32be(bns_offset + 0x00,sf, "BNS "))
goto fail;
if (read_u32be(bns_offset + 0x04,sf) != 0xFEFF0100u)
goto fail;
2020-12-19 15:48:15 +01:00
/* find chunks */
{
uint32_t file_size = read_u32be(bns_offset+0x08,sf);
uint32_t header_size = read_u16be(bns_offset+0x0c,sf);
uint16_t chunk_count = read_u16be(bns_offset+0x0e,sf);
int i;
2020-12-19 15:48:15 +01:00
/* assume BNS is the last thing in the file */
if (file_size + bns_offset != get_streamfile_size(sf))
goto fail;
for (i = 0; i < chunk_count; i++) {
2020-12-19 15:48:15 +01:00
uint32_t chunk_info_offset = bns_offset+0x10+i*0x08;
uint32_t chunk_offset, chunk_size;
// ensure chunk info is within header
2020-12-19 15:48:15 +01:00
if (chunk_info_offset+8 > bns_offset+header_size) goto fail;
2020-12-19 15:48:15 +01:00
chunk_offset = bns_offset + (uint32_t)read_32bitBE(chunk_info_offset,sf);
chunk_size = (uint32_t)read_32bitBE(chunk_info_offset+4,sf);
// ensure chunk is within file
2020-12-19 15:48:15 +01:00
if (chunk_offset < bns_offset+header_size ||
chunk_offset+chunk_size > bns_offset+file_size) goto fail;
// ensure chunk size in header matches that listed in chunk
// Note: disabled for now, as the Homebrew Channel BNS has a DATA
// chunk that doesn't include the header size
2020-12-19 15:48:15 +01:00
//if ((uint32_t)read_32bitBE(chunk_offset+4,sf) != chunk_size) goto fail;
// handle each chunk type
2020-12-19 15:48:15 +01:00
switch (read_32bitBE(chunk_offset,sf)) {
case 0x494E464F: // INFO
2020-12-19 15:48:15 +01:00
info_offset = chunk_offset+0x08;
break;
case 0x44415441: // DATA
2020-12-19 15:48:15 +01:00
data_offset = chunk_offset+0x08;
break;
default:
goto fail;
}
}
/* need both INFO and DATA */
if (!info_offset || !data_offset) goto fail;
}
/* parse out basic stuff in INFO */
{
/* only seen this zero, specifies DSP format? */
2020-12-19 15:48:15 +01:00
if (read_8bit(info_offset+0x00,sf) != 0) goto fail;
2020-12-19 15:48:15 +01:00
loop_flag = read_8bit(info_offset+0x01,sf);
channels = read_8bit(info_offset+0x02,sf);
/* only seen zero, padding? */
2020-12-19 15:48:15 +01:00
if (read_8bit(info_offset+0x03,sf) != 0) goto fail;
2020-12-19 15:48:15 +01:00
sample_rate = read_u16be(info_offset+0x04,sf);
/* only seen this zero, padding? */
2020-12-19 15:48:15 +01:00
if (read_u16be(info_offset+0x06,sf) != 0) goto fail;
2020-12-19 15:48:15 +01:00
loop_start = read_s32be(info_offset+0x08,sf);
2020-12-19 15:48:15 +01:00
sample_count = read_s32be(info_offset+0x0c,sf);
2020-12-19 15:48:15 +01:00
channel_info_offset_list_offset = info_offset + read_u32be(info_offset+0x10,sf);
}
/* build the VGMSTREAM */
2020-12-19 15:48:15 +01:00
vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail;
2020-12-19 15:48:15 +01:00
vgmstream->meta_type = meta_WII_BNS;
vgmstream->sample_rate = sample_rate;
vgmstream->num_samples = sample_count;
2020-12-19 15:48:15 +01:00
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = sample_count;
2020-12-19 15:48:15 +01:00
vgmstream->coding_type = coding_NGC_DSP;
vgmstream->layout_type = layout_none;
2020-12-19 15:48:15 +01:00
//todo cleanup
/* open the file for reading */
{
2020-12-19 15:48:15 +01:00
int i, j;
for (i = 0; i < channels; i++) {
uint32_t channel_info_offset = info_offset + read_u32be(channel_info_offset_list_offset + 0x04*i,sf);
uint32_t channel_data_offset = data_offset + read_u32be(channel_info_offset + 0x00,sf);
uint32_t channel_dsp_offset = info_offset + read_u32be(channel_info_offset + 0x04,sf);
/* always been 0... */
2020-12-19 15:48:15 +01:00
if (read_u32be(channel_info_offset + 0x8,sf) != 0) goto fail;
2020-12-19 15:48:15 +01:00
vgmstream->ch[i].streamfile = reopen_streamfile(sf, 0);
if (!vgmstream->ch[i].streamfile) goto fail;
2020-12-19 15:48:15 +01:00
vgmstream->ch[i].channel_start_offset = vgmstream->ch[i].offset = channel_data_offset;
2020-12-19 15:48:15 +01:00
for (j = 0; j < 16; j++) {
vgmstream->ch[i].adpcm_coef[j] = read_s16be(channel_dsp_offset + j*0x02,sf);
}
}
}
2020-12-19 15:48:15 +01:00
//if (!vgmstream_open_stream(vgmstream, sf, start_offset))
// goto fail;
return vgmstream;
fail:
2020-12-19 15:48:15 +01:00
close_vgmstream(vgmstream);
return NULL;
}