Fix some .sgt DMSG with streams

This commit is contained in:
bnnm 2021-08-22 12:20:55 +02:00
parent 8819cbf0b3
commit f9ef1f2d89
2 changed files with 145 additions and 95 deletions

View File

@ -155,7 +155,7 @@ static const char* extension_list[] = {
"de2",
"dec",
"diva",
"dmsg",
"dmsg", //fake extension/header id for .sgt (to be removed)
"ds2", //txth/reserved [Star Wars Bounty Hunter (GC)]
"dsb",
"dsf",
@ -402,7 +402,7 @@ static const char* extension_list[] = {
"rad",
"rak",
"ras",
"raw",
"raw", //txth/reserved [Madden NHL 97 (PC)-pcm8u]
"rda", //FFmpeg/reserved [Rhythm Destruction (PC)]
"rkv",
"rnd",
@ -464,6 +464,7 @@ static const char* extension_list[] = {
"sfx",
"sgb",
"sgd",
"sgt",
"sgx",
"sl3",
"slb", //txth/reserved [THE Nekomura no Hitobito (PS2)]
@ -1114,7 +1115,7 @@ static const meta_info meta_info_list[] = {
{meta_HIS, "Her Interactive HIS header"},
{meta_PS2_AST, "KOEI AST header"},
{meta_CAPDSP, "Capcom DSP header"},
{meta_DMSG, "RIFF/DMSGsegh header"},
{meta_DMSG, "Microsoft RIFF DMSG header"},
{meta_PONA_3DO, "Policenauts BGM header"},
{meta_PONA_PSX, "Policenauts BGM header"},
{meta_NGC_DSP_AAAP, "Acclaim Austin AAAp DSP header"},

View File

@ -1,110 +1,159 @@
#include "meta.h"
#include "../util.h"
#include "../coding/coding.h"
/* DMSG
found in: Nightcaster II - Equinox
2010-01-05 (manakoAT): Seems it's a corrupted "SGT" file, but I'm not sure...
*/
VGMSTREAM * init_vgmstream_dmsg(STREAMFILE *streamFile) {
typedef struct {
uint32_t type;
uint32_t size;
uint32_t offset;
off_t current;
off_t max;
int le_type;
int be_size;
int full_size;
} chunk_t;
static int next_chunk(chunk_t* chunk, STREAMFILE* sf) {
uint32_t (*read_u32type)(off_t,STREAMFILE*) = !chunk->le_type ? read_u32be : read_u32le;
uint32_t (*read_u32size)(off_t,STREAMFILE*) = chunk->be_size ? read_u32be : read_u32le;
if (chunk->max == 0)
chunk->max = get_streamfile_size(sf);
if (chunk->current >= chunk->max)
return 0;
/* can be used to signal "stop" */
if (chunk->current < 0)
return 0;
chunk->type = read_u32type(chunk->current + 0x00,sf);
chunk->size = read_u32size(chunk->current + 0x04,sf);
chunk->offset = chunk->current + 0x04 + 0x04;
chunk->current += chunk->full_size ? chunk->size : 0x08 + chunk->size;
//;VGM_LOG("CHUNK: %x, %x, %x\n", dc.offset, chunk->type, chunk->size);
/* read past data */
if (chunk->type == 0xFFFFFFFF || chunk->size == 0xFFFFFFFF)
return 0;
/* empty chunk with 0 size, seen in some formats (XVAG uses it as end marker, Wwise doesn't) */
if (chunk->type == 0 || chunk->size == 0)
return 0;
/* more chunks remain */
return 1;
}
enum {
CHUNK_RIFF = 0x52494646, /* "RIFF" */
CHUNK_LIST = 0x4C495354, /* "LIST" */
CHUNK_segh = 0x73656768, /* "segh" */
};
/* DMSG - DirectMusic Segment with streams [Nightcaster II: Equinox (Xbox), Wildfire (PC)] */
VGMSTREAM* init_vgmstream_dmsg(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
char filename[PATH_LIMIT];
int loop_flag = 0;
int frequency;
int channel_count;
int dataBuffer = 0;
int Founddata = 0;
size_t file_size;
off_t current_chunk;
off_t start_offset;
//int loop_flag, channels, sample_rate;
//int found_data = 0;
//int32_t num_samples, loop_start, loop_end;
//off_t start_offset;
off_t offset = 0, name_offset = 0, name_size = 0;
/* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename));
if (strcasecmp("dmsg",filename_extension(filename))) goto fail;
/* check header */
if (read_32bitBE(0x00,streamFile) != 0x52494646) /* "RIFF" */
goto fail;
if (read_32bitBE(0x08,streamFile) != 0x444D5347) /* "DMSG" */
goto fail;
if (read_32bitBE(0x0C,streamFile) != 0x73656768) /* "segh" */
goto fail;
if (read_32bitBE(0x10,streamFile) != 0x38000000) /* "0x38" */
/* checks */
/* .sgt: common
* .dmsg: header id */
if (!check_extensions(sf, "sgt,dmsg"))
goto fail;
/* scan file until we find a "data" string */
file_size = get_streamfile_size(streamFile);
if (!is_id32be(0x00,sf, "RIFF"))
goto fail;
if (!is_id32be(0x08,sf, "DMSG"))
goto fail;
/* A DirectMusic segment usually has lots of chunks then data pointing to .dls soundbank.
* This accepts .sgt with a RIFF WAVE inside (less common). */
{
current_chunk = 0;
/* Start at 0 and loop until we reached the
file size, or until we found a "data string */
while (!Founddata && current_chunk < file_size) {
dataBuffer = (read_32bitBE(current_chunk,streamFile));
if (dataBuffer == 0x64617461) { /* "data" */
/* if "data" string found, retrieve the needed infos */
Founddata = 1;
/* We will cancel the search here if we have a match */
chunk_t rc = {0};
chunk_t dc = {0};
rc.current = 0x0c;
while (next_chunk(&rc, sf)) {
switch(rc.type) {
/* "segh" has loopnum/samplesloop */
case CHUNK_segh:
//todo: missing TMusicTime format
/* 0x00: dwRepeats (>0 or -1=inf)
* 0x04: mtLength
* 0x08: mtPlayStart
* 0x0c: mtLoopStart
* 0x10: mtLoopEnd
* 0x14: dwResolution
* 0x18: rtLength (optional)
* .. */
break;
case CHUNK_LIST:
if (is_id32be(rc.offset + 0x00, sf, "UNFO") && is_id32be(rc.offset + 0x04, sf, "UNAM")) {
name_offset = rc.offset + 0x0c;
name_size = read_u32le(rc.offset + 0x08, sf);
}
break;
case CHUNK_RIFF:
if (!is_id32be(rc.offset, sf, "DMCN"))
goto fail;
dc.current = rc.offset + 0x04;
while (next_chunk(&dc, sf)) {
switch(dc.type) {
case CHUNK_LIST:
/* abridged, there are some sublists */
if (is_id32be(dc.offset + 0x00, sf, "cosl") && is_id32be(dc.offset + 0x30, sf, "WAVE")) {
offset = dc.offset + 0x34;
dc.current = -1;
rc.current = -1;
}
break;
default:
break;
}
}
break;
default:
break;
}
/* else we will increase the search offset by 1 */
current_chunk = current_chunk + 1;
}
}
if (Founddata == 0) {
if (!offset)
goto fail;
} else if (Founddata == 1) {
channel_count = (uint16_t)read_16bitLE(current_chunk-0x10,streamFile);
frequency = read_32bitLE(current_chunk-0xE,streamFile);
}
loop_flag = 1;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
/* subfile has a few extra chunks (guid, wavh) but otherwise standard (seen PCM and MS-ADPCM, with fact chunks) */
{
STREAMFILE* temp_sf = NULL;
off_t subfile_offset = offset;
size_t subfile_size = read_u32le(offset + 0x04, sf) + 0x08;
temp_sf = setup_subfile_streamfile(sf, subfile_offset, subfile_size, "wav");
if (!temp_sf) goto fail;
vgmstream = init_vgmstream_riff(temp_sf);
close_streamfile(temp_sf);
if (!vgmstream) goto fail;
/* fill in the vital statistics */
if (Founddata == 0) {
goto fail;
} else if (Founddata == 1) {
start_offset = current_chunk+0x8;
vgmstream->channels = channel_count;
vgmstream->sample_rate = frequency;
vgmstream->coding_type = coding_PCM16LE;
vgmstream->num_samples = (read_32bitLE(current_chunk+0x4,streamFile)/2/channel_count);
if (loop_flag) {
vgmstream->loop_start_sample = 0;
vgmstream->loop_end_sample = (read_32bitLE(current_chunk+0x4,streamFile)/2/channel_count);
}
}
if (channel_count == 1) {
vgmstream->layout_type = layout_none;
} else if (channel_count > 1) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x2;
}
vgmstream->meta_type = meta_DMSG;
/* open the file for reading */
{
int i;
STREAMFILE * file;
file = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!file) goto fail;
for (i=0;i<channel_count;i++) {
vgmstream->ch[i].streamfile = file;
vgmstream->ch[i].channel_start_offset=
vgmstream->ch[i].offset=start_offset+
vgmstream->interleave_block_size*i;
}
if (name_offset) {
if (name_size >= STREAM_NAME_SIZE)
name_size = STREAM_NAME_SIZE;
read_string_utf16le(vgmstream->stream_name,name_size, name_offset, sf);
}
return vgmstream;
}
/* clean up anything we may have opened */
fail:
if (vgmstream) close_vgmstream(vgmstream);
close_vgmstream(vgmstream);
return NULL;
}