Merge pull request #665 from bnnm/ktsc-cdima

ktsc cdima
This commit is contained in:
Christopher Snowhill 2020-07-12 14:05:14 -07:00 committed by GitHub
commit 255f5dbd5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 24 deletions

View File

@ -322,11 +322,11 @@ static const int16_t mul_delta_table[16] = {
/* Crystal Dynamics IMA, reverse engineered from the exe, also info: https://github.com/sephiroth99/MulDeMu */
static void cd_ima_expand_nibble(VGMSTREAMCHANNEL* stream, off_t byte_offset, int shift, int32_t* hist1, int32_t* index) {
static void cd_ima_expand_nibble(uint8_t byte, int shift, int32_t* hist1, int32_t* index) {
int code, sample, step, delta;
/* could do the above table calcs during decode too */
code = (read_8bit(byte_offset,stream->streamfile) >> shift) & 0xf;
code = (byte >> shift) & 0xf;
sample = *hist1;
step = mul_adpcm_table[*index];
@ -1210,50 +1210,55 @@ void decode_h4m_ima(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspa
stream->adpcm_step_index = step_index;
}
/* test... */
static inline int _clamp_s32(int value, int min, int max) {
if (value < min)
return min;
else if (value > max)
return max;
else
return value;
}
/* Crystal Dynamics IMA. Original code uses mind-bending intrinsics, so this may not be fully accurate.
* Has another table with delta_table MMX combos, and using header sample and clamps seems necessary. */
* Has another table with delta_table MMX combos, and uses header sample (first nibble is always 0). */
void decode_cd_ima(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int channel) {
uint8_t frame[0x24] = {0};
int i, frames_in, sample_pos = 0, block_samples, frame_size;
int32_t hist1 = stream->adpcm_history1_32;
int step_index = stream->adpcm_step_index;
off_t frame_offset;
/* external interleave (fixed size), mono */
block_samples = (0x24 - 0x4) * 2;
frame_size = 0x24;
block_samples = (frame_size - 0x4) * 2;
frames_in = first_sample / block_samples;
first_sample = first_sample % block_samples;
frame_size = 0x24;
frame_offset = stream->offset + frame_size*frames_in;
frame_offset = stream->offset + frame_size * frames_in;
read_streamfile(frame, frame_offset, frame_size, stream->streamfile); /* ignore EOF errors */
/* normal header (hist+step+reserved), mono */
if (first_sample == 0) {
off_t header_offset = frame_offset + 0x00;
hist1 = get_s16le(frame + 0x00);
step_index = get_u8(frame + 0x02);
step_index = _clamp_s32(step_index, 0, 88);
hist1 = read_16bitLE(header_offset+0x00,stream->streamfile);
step_index = read_8bit(header_offset+0x02,stream->streamfile);
if (step_index < 0) step_index=0;
if (step_index > 88) step_index=88;
/* write header sample (even samples per block, skips last nibble) */
/* write header sample (even samples per block, skips first nibble) */
outbuf[sample_pos] = (short)(hist1);
sample_pos += channelspacing;
first_sample += 1;
samples_to_do -= 1;
}
/* decode nibbles (layout: straight in mono ) */
/* decode nibbles (layout: straight in mono) */
for (i = first_sample; i < first_sample + samples_to_do; i++) {
off_t byte_offset = frame_offset + 0x04 + (i-1)/2;
int nibble_shift = (!((i-1)&1) ? 0:4); /* low first */
int pos = 0x04 + (i/2);
int shift = (i&1 ? 4:0); /* low first, but first low nibble is skipped */
/* must skip last nibble per spec, rarely needed though (ex. Gauntlet Dark Legacy) */
if (i < block_samples) {
cd_ima_expand_nibble(stream, byte_offset,nibble_shift, &hist1, &step_index);
outbuf[sample_pos] = (short)(hist1);
sample_pos += channelspacing;
}
cd_ima_expand_nibble(frame[pos], shift, &hist1, &step_index);
outbuf[sample_pos] = (short)(hist1);
sample_pos += channelspacing;
}
stream->adpcm_history1_32 = hist1;

View File

@ -170,14 +170,18 @@ void reset_layout_layered(layered_layout_data *data) {
/* helper for easier creation of layers */
VGMSTREAM *allocate_layered_vgmstream(layered_layout_data* data) {
VGMSTREAM *vgmstream = NULL;
int i, channels, loop_flag;
int i, channels, loop_flag, num_samples;
/* get data */
channels = data->output_channels;
loop_flag = 1;
num_samples = 0;
for (i = 0; i < data->layer_count; i++) {
if (loop_flag && !data->layers[i]->loop_flag)
loop_flag = 0;
if (num_samples < data->layers[i]->num_samples)
num_samples = data->layers[i]->num_samples;
}
@ -187,7 +191,7 @@ VGMSTREAM *allocate_layered_vgmstream(layered_layout_data* data) {
vgmstream->meta_type = data->layers[0]->meta_type;
vgmstream->sample_rate = data->layers[0]->sample_rate;
vgmstream->num_samples = data->layers[0]->num_samples;
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = data->layers[0]->loop_start_sample;
vgmstream->loop_end_sample = data->layers[0]->loop_end_sample;
vgmstream->coding_type = data->layers[0]->coding_type;

View File

@ -832,6 +832,10 @@
RelativePath=".\meta\kraw.c"
>
</File>
<File
RelativePath=".\meta\ktsc.c"
>
</File>
<File
RelativePath=".\meta\ktsr.c"
>

View File

@ -193,6 +193,7 @@
<ClCompile Include="meta\imuse.c" />
<ClCompile Include="meta\ios_psnd.c" />
<ClCompile Include="meta\kat.c" />
<ClCompile Include="meta\ktsc.c" />
<ClCompile Include="meta\ktsr.c" />
<ClCompile Include="meta\ktss.c" />
<ClCompile Include="meta\kwb.c" />

View File

@ -1747,6 +1747,9 @@
<ClCompile Include="meta\mogg.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\ktsc.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\ktsr.c">
<Filter>meta\Source Files</Filter>
</ClCompile>

60
src/meta/ktsc.c Normal file
View File

@ -0,0 +1,60 @@
#include "meta.h"
#include "../coding/coding.h"
#include "../layout/layout.h"
/* KTSC - Koei Tecmo KTSR container */
VGMSTREAM* init_vgmstream_ktsc(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
STREAMFILE *temp_sf = NULL;
int target_subsong = sf->stream_index, total_subsongs;
off_t offset, subfile_offset;
size_t subfile_size;
/* checks */
/* .ktsl2asbin: common [Atelier Ryza (PC)] */
if (!check_extensions(sf, "ktsl2asbin"))
goto fail;
/* KTSC is a container of KTSRs, but can't be extracted easily as they use absolute pointers to the
* same stream companion file. KTSRs may have subsongs, but only seem to have 1, so use KTSC's subsongs. */
if (read_u32be(0x00, sf) != 0x4B545343) /* "KTSC" */
goto fail;
if (read_u32be(0x04, sf) != 0x01000001) /* version? */
goto fail;
if (target_subsong == 0) target_subsong = 1;
total_subsongs = read_u32le(0x08, sf);
if (target_subsong > total_subsongs)
goto fail;
/* 0x0c: CRC(?) table start */
offset = read_u32le(0x10, sf);
/* 0x14: file size */
/* 0x18: header end */
/* 0x1c: null */
/* 0x20+: CRC(?) table, 1 entry per file */
subfile_offset = read_u32le(offset + 0x04 * (target_subsong - 1), sf);
subfile_size = read_u32le(subfile_offset + 0x1c, sf); /* from header, meh */
temp_sf = setup_subfile_streamfile(sf, subfile_offset, subfile_size, NULL);
if (!temp_sf) goto fail;
temp_sf->stream_index = 1;
vgmstream = init_vgmstream_ktsr(temp_sf);
if (!vgmstream) goto fail;
if (vgmstream->num_streams > 1)
goto fail;
vgmstream->num_streams = total_subsongs;
close_streamfile(temp_sf);
return vgmstream;
fail:
close_streamfile(temp_sf);
close_vgmstream(vgmstream);
return NULL;
}

View File

@ -312,6 +312,7 @@ static int parse_ktsr_subfile(ktsr_header* ktsr, STREAMFILE* sf, off_t offset) {
switch(type) { /* hash-id? */
case 0x38D0437D: /* external [Nioh (PC), Atelier Ryza (PC)] */
case 0xDF92529F: /* external [Atelier Ryza (PC)] */
/* 08 subtype? (ex. 0x522B86B9)
* 0c channels
* 10 ? (always 0x002706B8)

View File

@ -907,4 +907,6 @@ VGMSTREAM* init_vgmstream_kat(STREAMFILE* sf);
VGMSTREAM* init_vgmstream_pcm_success(STREAMFILE* sf);
VGMSTREAM* init_vgmstream_ktsc(STREAMFILE* sf);
#endif /*_META_H*/

View File

@ -501,6 +501,7 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = {
init_vgmstream_mups,
init_vgmstream_kat,
init_vgmstream_pcm_success,
init_vgmstream_ktsc,
/* lowest priority metas (should go after all metas, and TXTH should go before raw formats) */
init_vgmstream_txth, /* proper parsers should supersede TXTH, once added */