2014-10-18 23:54:31 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../util.h"
|
2015-07-14 10:01:46 +02:00
|
|
|
#include "../stack_alloc.h"
|
2014-10-18 23:54:31 +02:00
|
|
|
|
2017-03-13 21:19:38 +01:00
|
|
|
/* BFSTM - Nintendo Wii U format */
|
2014-10-18 23:54:31 +02:00
|
|
|
VGMSTREAM * init_vgmstream_bfstm(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
coding_t coding_type;
|
2017-07-05 03:51:14 +02:00
|
|
|
coding_t coding_PCM16;
|
2017-03-13 21:19:38 +01:00
|
|
|
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
|
|
|
|
int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL;
|
2014-10-18 23:54:31 +02:00
|
|
|
|
2016-12-27 16:33:10 +01:00
|
|
|
off_t info_offset = 0, seek_offset = 0, data_offset = 0;
|
2015-07-08 00:46:08 +02:00
|
|
|
uint16_t temp_id;
|
2014-10-18 23:54:31 +02:00
|
|
|
int codec_number;
|
2017-03-13 21:19:38 +01:00
|
|
|
int channel_count, loop_flag;
|
2015-07-08 00:46:08 +02:00
|
|
|
int i, j;
|
2014-10-18 23:54:31 +02:00
|
|
|
int ima = 0;
|
|
|
|
off_t start_offset;
|
2014-10-20 09:58:33 +02:00
|
|
|
off_t tempoffset1;
|
2016-10-29 01:50:39 +02:00
|
|
|
int section_count;
|
2014-10-18 23:54:31 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2017-03-13 21:19:38 +01:00
|
|
|
if ( !check_extensions(streamFile,"bfstm") )
|
2014-10-18 23:54:31 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
|
|
|
if ((uint32_t)read_32bitBE(0, streamFile) != 0x4653544D) /* "FSTM" */
|
|
|
|
goto fail;
|
|
|
|
|
2017-03-13 21:19:38 +01:00
|
|
|
if ((uint16_t)read_16bitBE(4, streamFile) == 0xFEFF) { /* endian marker (BE most common) */
|
|
|
|
read_32bit = read_32bitBE;
|
|
|
|
read_16bit = read_16bitBE;
|
2017-07-05 03:51:14 +02:00
|
|
|
coding_PCM16 = coding_PCM16BE;
|
2017-03-13 21:19:38 +01:00
|
|
|
} else if ((uint16_t)read_16bitBE(4, streamFile) == 0xFFFE) { /* Blaster Master Zero 3DS */
|
|
|
|
read_32bit = read_32bitLE;
|
|
|
|
read_16bit = read_16bitLE;
|
2017-07-05 03:51:14 +02:00
|
|
|
coding_PCM16 = coding_PCM16LE;
|
2017-03-13 21:19:38 +01:00
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
section_count = read_16bit(0x10, streamFile);
|
2015-07-08 00:46:08 +02:00
|
|
|
for (i = 0; i < section_count; i++) {
|
2017-03-13 21:19:38 +01:00
|
|
|
temp_id = read_16bit(0x14 + i * 0xc, streamFile);
|
2015-07-08 00:46:08 +02:00
|
|
|
switch(temp_id) {
|
|
|
|
case 0x4000:
|
2017-03-13 21:19:38 +01:00
|
|
|
info_offset = read_32bit(0x18 + i * 0xc, streamFile);
|
|
|
|
/* size_t info_size = read_32bit(0x1c + i * 0xc, streamFile); */
|
2015-07-08 00:46:08 +02:00
|
|
|
break;
|
|
|
|
case 0x4001:
|
2017-03-13 21:19:38 +01:00
|
|
|
seek_offset = read_32bit(0x18 + i * 0xc, streamFile);
|
|
|
|
/* size_t seek_size = read_32bit(0x1c + i * 0xc, streamFile); */
|
2015-07-08 00:46:08 +02:00
|
|
|
break;
|
|
|
|
case 0x4002:
|
2017-03-13 21:19:38 +01:00
|
|
|
data_offset = read_32bit(0x18 + i * 0xc, streamFile);
|
|
|
|
/* size_t data_size = read_32bit(0x1c + i * 0xc, streamFile); */
|
2015-07-08 00:46:08 +02:00
|
|
|
break;
|
|
|
|
case 0x4003:
|
2017-03-13 21:19:38 +01:00
|
|
|
/* off_t regn_offset = read_32bit(0x18 + i * 0xc, streamFile); */
|
|
|
|
/* size_t regn_size = read_32bit(0x1c + i * 0xc, streamFile); */
|
2015-07-08 00:46:08 +02:00
|
|
|
break;
|
|
|
|
case 0x4004:
|
2017-03-13 21:19:38 +01:00
|
|
|
/* off_t pdat_offset = read_32bit(0x18 + i * 0xc, streamFile); */
|
|
|
|
/* size_t pdat_size = read_32bit(0x1c + i * 0xc, streamFile); */
|
2015-07-08 00:46:08 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-10-18 23:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 16:33:10 +01:00
|
|
|
if (info_offset == 0) goto fail;
|
2015-07-08 00:46:08 +02:00
|
|
|
if ((uint32_t)read_32bitBE(info_offset, streamFile) != 0x494E464F) /* "INFO" */
|
2014-10-18 23:54:31 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
/* check type details */
|
2015-07-08 00:46:08 +02:00
|
|
|
codec_number = read_8bit(info_offset + 0x20, streamFile);
|
|
|
|
loop_flag = read_8bit(info_offset + 0x21, streamFile);
|
|
|
|
channel_count = read_8bit(info_offset + 0x22, streamFile);
|
2014-10-18 23:54:31 +02:00
|
|
|
|
|
|
|
switch (codec_number) {
|
|
|
|
case 0:
|
|
|
|
coding_type = coding_PCM8;
|
|
|
|
break;
|
|
|
|
case 1:
|
2017-07-05 03:51:14 +02:00
|
|
|
coding_type = coding_PCM16;
|
2014-10-18 23:54:31 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
2017-03-13 21:19:38 +01:00
|
|
|
vgmstream->num_samples = read_32bit(info_offset + 0x2c, streamFile);
|
|
|
|
vgmstream->sample_rate = read_32bit(info_offset + 0x24, streamFile);
|
2014-10-18 23:54:31 +02:00
|
|
|
/* 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
|
|
|
|
{
|
2017-03-13 21:19:38 +01:00
|
|
|
vgmstream->loop_start_sample = read_32bit(info_offset + 0x28, streamFile);
|
2014-10-18 23:54:31 +02:00
|
|
|
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
|
|
|
|
{
|
2017-03-13 21:19:38 +01:00
|
|
|
vgmstream->loop_start_sample = read_32bit(info_offset + 0x28, streamFile);
|
2014-10-18 23:54:31 +02:00
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_type;
|
|
|
|
if (channel_count == 1)
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ima)
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
else
|
|
|
|
vgmstream->layout_type = layout_interleave_shortblock;
|
|
|
|
}
|
|
|
|
vgmstream->meta_type = meta_FSTM;
|
|
|
|
|
|
|
|
if (ima)
|
|
|
|
vgmstream->interleave_block_size = 0x200;
|
|
|
|
else {
|
2017-03-13 21:19:38 +01:00
|
|
|
vgmstream->interleave_block_size = read_32bit(info_offset + 0x34, streamFile);
|
|
|
|
vgmstream->interleave_smallblock_size = read_32bit(info_offset + 0x44, streamFile);
|
2014-10-18 23:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vgmstream->coding_type == coding_NGC_DSP) {
|
2016-10-29 01:50:39 +02:00
|
|
|
off_t coeff_ptr_table;
|
2015-07-14 10:01:46 +02:00
|
|
|
VARDECL(off_t, coef_offset);
|
|
|
|
ALLOC(coef_offset, channel_count, off_t);
|
2017-03-13 21:19:38 +01:00
|
|
|
coeff_ptr_table = read_32bit(info_offset + 0x1c, streamFile) + info_offset + 8; // Getting pointer for coefficient pointer table
|
2015-07-08 00:46:08 +02:00
|
|
|
|
|
|
|
for (i = 0; i < channel_count; i++) {
|
2017-03-13 21:19:38 +01:00
|
|
|
tempoffset1 = read_32bit(coeff_ptr_table + 8 + i * 8, streamFile);
|
2015-07-08 00:46:08 +02:00
|
|
|
coef_offset[i] = tempoffset1 + coeff_ptr_table;
|
2017-03-13 21:19:38 +01:00
|
|
|
coef_offset[i] += read_32bit(coef_offset[i] + 4, streamFile);
|
2015-07-08 00:46:08 +02:00
|
|
|
}
|
|
|
|
|
2014-10-18 23:54:31 +02:00
|
|
|
for (j = 0; j<vgmstream->channels; j++) {
|
|
|
|
for (i = 0; i<16; i++) {
|
2017-03-13 21:19:38 +01:00
|
|
|
vgmstream->ch[j].adpcm_coef[i] = read_16bit(coef_offset[j] + i * 2, streamFile);
|
2014-10-18 23:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 16:33:10 +01:00
|
|
|
if (ima) { // No SEEK (ADPC) header, so just start where the SEEK header is supposed to be.
|
|
|
|
if (seek_offset == 0) goto fail;
|
2014-10-18 23:54:31 +02:00
|
|
|
start_offset = seek_offset;
|
2016-12-27 16:33:10 +01:00
|
|
|
} else {
|
|
|
|
if (data_offset == 0) goto fail;
|
2014-10-18 23:54:31 +02:00
|
|
|
start_offset = data_offset + 0x20;
|
2016-12-27 16:33:10 +01:00
|
|
|
}
|
2014-10-18 23:54:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* open the file for reading by each channel */
|
2017-03-13 21:19:38 +01:00
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
goto fail;
|
2014-10-18 23:54:31 +02:00
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-03-13 21:19:38 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2014-10-18 23:54:31 +02:00
|
|
|
return NULL;
|
|
|
|
}
|