Improve 1SNh: add mu-Law, fix video blocks/SEAD audio, fix some IMA

This commit is contained in:
bnnm 2017-12-24 01:48:26 +01:00
parent 2f9c16ae9b
commit 12aa4ef7ef
2 changed files with 148 additions and 52 deletions

View File

@ -4,36 +4,41 @@
/* set up for the block at the given offset */
void block_update_ea_1snh(off_t block_offset, VGMSTREAM * vgmstream) {
int i;
STREAMFILE* streamFile = vgmstream->ch[0].streamfile;
uint32_t id;
size_t file_size, block_size = 0, block_header = 0;
int i;
size_t block_size = 0, block_header = 0;
int32_t (*read_32bit)(off_t,STREAMFILE*) = vgmstream->codec_endian ? read_32bitBE : read_32bitLE;
size_t file_size = get_streamfile_size(streamFile);
/* find target block ID and skip the rest */
file_size = get_streamfile_size(streamFile);
while (block_offset < file_size) {
id = read_32bitBE(block_offset+0x00,streamFile);
block_size = read_32bit(block_offset+0x04,streamFile); /* includes id/size */
block_header = 0x0;
uint32_t id = read_32bitBE(block_offset+0x00,streamFile);
if (id == 0x31534E68) { /* "1SNh" header block found */
block_header = read_32bitBE(block_offset+0x08, streamFile) == 0x45414353 ? 0x28 : 0x2c; /* "EACS" */
if (block_header < block_size) /* sometimes has data */
break;
block_size = read_32bitLE(block_offset+0x04,streamFile);
if (block_size > 0x00F00000) /* BE in SAT, but one file may have both BE and LE chunks */
block_size = read_32bitBE(block_offset+0x04,streamFile);
block_header = 0;
if (id == 0x31534E68 || id == 0x53454144) { /* "1SNh" "SEAD" audio header */
int is_sead = (id == 0x53454144);
int is_eacs = read_32bitBE(block_offset+0x08, streamFile) == 0x45414353;
block_header = is_eacs ? 0x28 : (is_sead ? 0x14 : 0x2c);
if (block_header >= block_size) /* sometimes has audio data after header */
block_header = 0;
}
if (id == 0x31534E64) { /* "1SNd" data block found */
else if (id == 0x31534E64 || id == 0x534E4443) { /* "1SNd" "SNDC" audio data */
block_header = 0x08;
}
else if (id == 0x00000000) {
break;
}
if (id == 0x00000000 || id == 0xFFFFFFFF) { /* EOF: possible? */
if (block_header) {
break;
}
/* any other blocks "1SNl" "1SNe" etc */ //todo parse movie blocks
block_offset += block_size;
}
@ -45,6 +50,7 @@ void block_update_ea_1snh(off_t block_offset, VGMSTREAM * vgmstream) {
/* set new channel offsets and block sizes */
switch(vgmstream->coding_type) {
case coding_PCM8_int:
case coding_ULAW_int:
vgmstream->current_block_size /= vgmstream->channels;
for (i=0;i<vgmstream->channels;i++) {
vgmstream->ch[i].offset = block_offset + block_header + i;
@ -66,13 +72,24 @@ void block_update_ea_1snh(off_t block_offset, VGMSTREAM * vgmstream) {
break;
case coding_DVI_IMA:
vgmstream->current_block_size -= 0x14;
for(i = 0; i < vgmstream->channels; i++) {
off_t adpcm_offset = block_offset + block_header + 0x04;
vgmstream->ch[i].adpcm_step_index = read_32bit(adpcm_offset + i*0x04, streamFile);
vgmstream->ch[i].adpcm_history1_32 = read_32bit(adpcm_offset + 0x04*vgmstream->channels + i*0x04, streamFile);
// todo some demuxed vids don't have ADPCM hist? not sure how to correctly detect
vgmstream->ch[i].offset = block_offset + block_header + 0x14;
if (vgmstream->codec_version == 1) { /* ADPCM hist */
vgmstream->current_block_samples = read_32bit(block_offset + block_header, streamFile);
vgmstream->current_block_size = 0; // - (0x04 + 0x08*vgmstream->channels); /* should be equivalent */
for(i = 0; i < vgmstream->channels; i++) {
off_t adpcm_offset = block_offset + block_header + 0x04;
vgmstream->ch[i].adpcm_step_index = read_32bit(adpcm_offset + i*0x04 + 0x00*vgmstream->channels, streamFile);
vgmstream->ch[i].adpcm_history1_32 = read_32bit(adpcm_offset + i*0x04 + 0x04*vgmstream->channels, streamFile);
vgmstream->ch[i].offset = adpcm_offset + 0x08*vgmstream->channels;
}
VGM_ASSERT(vgmstream->current_block_samples != (block_size - block_header - 0x04 - 0x08*vgmstream->channels) * 2 / vgmstream->channels,
"EA 1SHN blocked: different expected vs block num samples at %lx\n", block_offset);
}
else {
for(i = 0; i < vgmstream->channels; i++) {
vgmstream->ch[i].offset = block_offset + block_header;
}
}
break;

View File

@ -3,7 +3,7 @@
#include "../layout/layout.h"
#define EA_CODEC_PCM 0x00
//#define EA_CODEC_??? 0x01 //used in SAT videos
#define EA_CODEC_ULAW 0x01
#define EA_CODEC_IMA 0x02
#define EA_CODEC_PSX 0xFF //fake value
@ -20,10 +20,13 @@ typedef struct {
int big_endian;
int loop_flag;
int is_sead;
int codec_version;
} ea_header;
static int parse_header(STREAMFILE* streamFile, ea_header* ea, off_t begin_offset);
static void set_ea_1snh_psx_samples(STREAMFILE* streamFile, off_t start_offset, ea_header* ea);
static void set_ea_1snh_num_samples(STREAMFILE* streamFile, off_t start_offset, ea_header* ea);
static int get_ea_1snh_ima_version(STREAMFILE* streamFile, off_t start_offset, const ea_header* ea);
/* EA 1SNh - from early EA games (~1996, ex. Need for Speed) */
VGMSTREAM * init_vgmstream_ea_1snh(STREAMFILE *streamFile) {
@ -37,9 +40,15 @@ VGMSTREAM * init_vgmstream_ea_1snh(STREAMFILE *streamFile) {
goto fail;
/* check header (first block) */
if (read_32bitBE(0,streamFile)!=0x31534E68) /* "1SNh" */
if (read_32bitBE(0x00,streamFile) != 0x31534E68 && /* "1SNh" */
read_32bitBE(0x00,streamFile) != 0x53454144) /* "SEAD" */
goto fail;
/* stream is divided into blocks/chunks: 1SNh=audio header, 1SNd=data xN, 1SNl=loop end, 1SNe=end.
* Video uses various blocks (kVGT/fVGT/etc) and sometimes alt audio blocks (SEAD/SNDC/SEND). */
ea.is_sead = read_32bitBE(0x00,streamFile) == 0x53454144;
/* use block size as endian marker (Saturn = BE) */
ea.big_endian = !(read_32bitLE(0x04,streamFile) < 0x0000FFFF);
@ -63,16 +72,22 @@ VGMSTREAM * init_vgmstream_ea_1snh(STREAMFILE *streamFile) {
vgmstream->meta_type = meta_EA_1SNH;
switch (ea.codec) {
case EA_CODEC_PCM:
case EA_CODEC_PCM: /* Need for Speed (PC) */
vgmstream->coding_type = ea.bits==1 ? coding_PCM8_int : coding_PCM16_int;
break;
case EA_CODEC_IMA:
if (ea.bits!=2) goto fail;
vgmstream->coding_type = coding_DVI_IMA; /* stereo/mono, high nibble first */
case EA_CODEC_ULAW: /* Crusader: No Remorse movies (SAT), FIFA 96 movies (SAT) */
if (ea.bits && ea.bits!=2) goto fail; /* only set in EACS */
vgmstream->coding_type = coding_ULAW_int;
break;
case EA_CODEC_PSX:
case EA_CODEC_IMA: /* Need for Speed II (PC) */
if (ea.bits && ea.bits!=2) goto fail; /* only in EACS */
vgmstream->coding_type = coding_DVI_IMA; /* stereo/mono, high nibble first */
vgmstream->codec_version = ea.codec_version;
break;
case EA_CODEC_PSX: /* Need for Speed (PS) */
vgmstream->coding_type = coding_PSX;
break;
@ -98,7 +113,7 @@ static int parse_header(STREAMFILE* streamFile, ea_header* ea, off_t offset) {
int32_t (*read_32bit)(off_t,STREAMFILE*) = ea->big_endian ? read_32bitBE : read_32bitLE;
if (read_32bitBE(offset+0x00, streamFile) == 0x45414353) { /* "EACS" */
/* PC/SAT EACS subheader */
/* EACS subheader (PC, SAT) */
ea->sample_rate = read_32bit(offset+0x04, streamFile);
ea->bits = read_8bit(offset+0x08, streamFile);
ea->channels = read_8bit(offset+0x09, streamFile);
@ -109,15 +124,32 @@ static int parse_header(STREAMFILE* streamFile, ea_header* ea, off_t offset) {
ea->loop_end = read_32bit(offset+0x14, streamFile) + ea->loop_start; /* loop length */
/* 0x18: data start? (0x00), 0x1c: pan/volume/etc? (0x7F), rest can be padding/garbage */
VGM_ASSERT(ea->type != 0, "EA EACS: unknown type\n"); /* block type? */
if (ea->codec == EA_CODEC_IMA)
ea->codec_version = get_ea_1snh_ima_version(streamFile, 0x00, ea);
}
else if (ea->is_sead) {
/* alt subheader (found in some PC videos) */
ea->sample_rate = read_32bit(offset+0x00, streamFile);
ea->channels = read_32bit(offset+0x04, streamFile);
ea->codec = read_32bit(offset+0x08, streamFile);
if (ea->codec == EA_CODEC_IMA)
ea->codec_version = get_ea_1snh_ima_version(streamFile, 0x00, ea);
set_ea_1snh_num_samples(streamFile, 0x00, ea);
if (ea->loop_start_offset) /* offset found, now find actual start sample */
set_ea_1snh_num_samples(streamFile, 0x00, ea);
}
else {
/* PS subheader */
/* alt subheader (PS) */
ea->sample_rate = read_32bit(offset+0x00, streamFile);
ea->channels = read_8bit(offset+0x18, streamFile);
ea->codec = EA_CODEC_PSX;
set_ea_1snh_psx_samples(streamFile, 0x00, ea);
if (ea->loop_start_offset)/* found offset, now find sample start */
set_ea_1snh_psx_samples(streamFile, 0x00, ea);
set_ea_1snh_num_samples(streamFile, 0x00, ea);
if (ea->loop_start_offset) /* offset found, now find actual start sample */
set_ea_1snh_num_samples(streamFile, 0x00, ea);
}
ea->loop_flag = (ea->loop_end > 0);
@ -126,42 +158,61 @@ static int parse_header(STREAMFILE* streamFile, ea_header* ea, off_t offset) {
}
/* get total samples by parsing block headers, needed when EACS isn't present */
static void set_ea_1snh_psx_samples(STREAMFILE* streamFile, off_t start_offset, ea_header* ea) {
static void set_ea_1snh_num_samples(STREAMFILE* streamFile, off_t start_offset, ea_header* ea) {
int num_samples = 0, loop_start = 0, loop_end = 0, loop_start_offset = 0;
off_t block_offset = start_offset;
size_t file_size = get_streamfile_size(streamFile);
size_t block_size, block_header, block_samples;
int32_t (*read_32bit)(off_t,STREAMFILE*) = ea->big_endian ? read_32bitBE : read_32bitLE;
size_t file_size = get_streamfile_size(streamFile);
while (block_offset < file_size) {
uint32_t id = read_32bitBE(block_offset+0x00,streamFile);
size_t block_size = read_32bit(block_offset+0x04,streamFile); /* includes id/size */
block_size = read_32bit(block_offset+0x04,streamFile);
block_header = 0;
block_samples = 0;
if (id == 0x31534E68) { /* "1SNh" header block found */
size_t block_header = read_32bitBE(block_offset+0x08, streamFile) == 0x45414353 ? 0x28 : 0x2c; /* "EACS" */
if (block_header < block_size) /* sometimes has data */
num_samples += ps_bytes_to_samples(block_size - block_header, ea->channels);
if (id == 0x31534E68 || id == 0x53454144) { /* "1SNh" "SEAD" audio header */
int is_sead = (id == 0x53454144);
int is_eacs = read_32bitBE(block_offset+0x08, streamFile) == 0x45414353;
block_header = is_eacs ? 0x28 : (is_sead ? 0x14 : 0x2c);
if (block_header >= block_size) /* sometimes has audio data after header */
block_header = 0;
}
if (id == 0x31534E64) { /* "1SNd" data block found */
num_samples += ps_bytes_to_samples(block_size - 0x08, ea->channels);
else if (id == 0x31534E64 || id == 0x534E4443) { /* "1SNd" "SNDC" audio data */
block_header = 0x08;
}
if (id == 0x31534E6C) { /* "1SNl" loop point found */
else if (id == 0x00000000) {
break;
}
else if (id == 0x31534E6C) { /* "1SNl" loop point found */
loop_start_offset = read_32bit(block_offset+0x08,streamFile);
loop_end = num_samples;
}
if (id == 0x00000000 || id == 0xFFFFFFFF) { /* EOF: possible? */
break;
if (block_header) {
switch(ea->codec) {
case EA_CODEC_PSX:
block_samples = ps_bytes_to_samples(block_size - block_header, ea->channels);
break;
case EA_CODEC_IMA:
if (ea->codec_version == 1)
block_samples = read_32bit(block_offset + block_header, streamFile);
else
block_samples = ima_bytes_to_samples(block_size - block_header, ea->channels);
break;
}
}
/* if there is a loop start offset this was called again just to find it */
/* if there is a loop start offset set, this was called again just to find it */
if (ea->loop_start_offset && ea->loop_start_offset == block_offset) {
ea->loop_start = num_samples;
return;
}
/* any other blocks "1SNl" "1SNe" etc */ //todo parse movie blocks
num_samples += block_samples;
block_offset += block_size;
}
@ -171,3 +222,31 @@ static void set_ea_1snh_psx_samples(STREAMFILE* streamFile, off_t start_offset,
ea->loop_end = loop_end;
ea->loop_start_offset = loop_start_offset;
}
/* find codec version used, with or without ADPCM hist per block */
static int get_ea_1snh_ima_version(STREAMFILE* streamFile, off_t start_offset, const ea_header* ea) {
off_t block_offset = start_offset;
size_t file_size = get_streamfile_size(streamFile);
int32_t (*read_32bit)(off_t,STREAMFILE*) = ea->big_endian ? read_32bitBE : read_32bitLE;
while (block_offset < file_size) {
uint32_t id = read_32bitBE(block_offset+0x00,streamFile);
size_t block_size = read_32bitLE(block_offset+0x04,streamFile);
if (block_size > 0x00F00000) /* BE in SAT, but one file may have both BE and LE chunks */
block_size = read_32bitBE(block_offset+0x04,streamFile);
if (id == 0x31534E64 || id == 0x534E4443) { /* "1SNd" "SNDC" audio data */
size_t ima_samples = read_32bit(block_offset + 0x08, streamFile);
size_t expected_samples = (block_size - 0x08 - 0x04 - 0x08*ea->channels) * 2 / ea->channels;
if (ima_samples == expected_samples) {
return 1; /* has ADPCM hist (hopefully) */
}
}
block_offset += block_size;
}
return 0; /* no ADPCM hist */
}