Add PCFX for GENH/TXTH [Der Langrisser FX (PC-FX)]

This commit is contained in:
bnnm 2018-12-27 16:14:59 +01:00
parent 0bc8e084cf
commit e53740f27f
11 changed files with 140 additions and 2 deletions

View File

@ -79,6 +79,7 @@ A text file with the above commands must be saved as ".vag.txth" or ".txth", not
# - XMA2 Microsoft XMA2
# - FFMPEG Any headered FFmpeg format
# - AC3 AC3/SPDIF
# - PCFX PC-FX ADPCM
codec = (codec string)
# Codec variations [OPTIONAL, depends on codec]
@ -86,6 +87,7 @@ codec = (codec string)
# - ATRAC3: 0=autodetect joint stereo, 1=force joint stereo, 2=force normal stereo
# - XMA1|XMA2: 0=dual multichannel (2ch xN), 1=single multichannel (1ch xN)
# - XBOX: 0=standard (mono or stereo interleave), 1=force mono interleave mode
# - PCFX: 0=standard, 1='buggy encoder' mode, 2/3=same as 0/1 but with double volume
# - others: ignored
codec_mode = (number)

View File

@ -168,6 +168,10 @@ void decode_derf(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing,
/* circus_decoder */
void decode_circus_adpcm(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
/* pcfx_decoder */
void decode_pcfx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int mode);
size_t pcfx_bytes_to_samples(size_t bytes, int channels);
/* ea_mt_decoder*/
ea_mt_codec_data *init_ea_mt(int channels, int type);
ea_mt_codec_data *init_ea_mt_loops(int channels, int pcm_blocks, int loop_sample, off_t *loop_offsets);

92
src/coding/pcfx_decoder.c Normal file
View File

@ -0,0 +1,92 @@
#include "coding.h"
static const int step_sizes[49] = { /* OKI table */
16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50,
55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
};
static const int stex_indexes[16] = { /* IMA table */
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
static void pcfx_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset, int nibble_shift, int32_t * hist1, int32_t * step_index, int16_t *out_sample, int mode) {
int code, step, delta;
code = (read_8bit(byte_offset,stream->streamfile) >> nibble_shift) & 0xf;
step = step_sizes[*step_index];
delta = (code & 0x7);
if (mode & 1) {
if (step == 1552) /* bad last step_sizes value from OKI table */
step = 1522;
delta = step * (delta + 1) * 2;
}
else {
delta = step * (delta + 1);
}
if (code & 0x8)
delta = -delta;
*step_index += stex_indexes[code];
if (*step_index < 0) *step_index = 0;
if (*step_index > 48) *step_index = 48;
*hist1 += delta;
if (*hist1 > 16383) *hist1 = 16383;
if (*hist1 < -16384) *hist1 = -16384;
if (mode & 1) {
*out_sample = *hist1;
} else {
*out_sample = *hist1 << 1;
}
/* seems real HW does filtering here too */
/* double volume since it clips at half */
if (mode & 2) {
*out_sample = *hist1 << 1;
}
}
/* PC-FX ADPCM decoding, variation of OKI/Dialogic/VOX ADPCM. Based on mednafen/pcfx-music-dump.
* Apparently most ADPCM was made with a buggy encoder, resulting in incorrect sound in real hardware
* and sound clipped at half. Decoding can be controlled with modes:
* - 0: hardware decoding (waveforms in many games will look wrong, ex. Der Langrisser track 032)
* - 1: 'buggy encoder' decoding (waveforms will look fine)
* - 2: hardware decoding with double volume (may clip?)
* - 3: 'buggy encoder' decoding with double volume
*
* PC-FX ISOs don't have a standard filesystem nor file formats (raw data must be custom-ripped),
* so it's needs GENH/TXTH. Sample rate can only be base_value divided by 1/2/3/4, where
* base_value is approximately ~31468.5 (follows hardware clocks), mono or stereo-interleaved.
*/
void decode_pcfx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int mode) {
int i, sample_count = 0;
int32_t hist1 = stream->adpcm_history1_32;
int step_index = stream->adpcm_step_index;
int16_t out_sample;
for (i = first_sample; i < first_sample + samples_to_do; i++) {
off_t byte_offset = stream->offset + i/2;
int nibble_shift = (i&1?4:0); /* low nibble first */
pcfx_expand_nibble(stream, byte_offset,nibble_shift, &hist1, &step_index, &out_sample, mode);
outbuf[sample_count] = out_sample;
sample_count += channelspacing;
}
stream->adpcm_history1_32 = hist1;
stream->adpcm_step_index = step_index;
}
size_t pcfx_bytes_to_samples(size_t bytes, int channels) {
/* 2 samples per byte (2 nibbles) in stereo or mono config */
return bytes * 2 / channels;
}

View File

@ -616,6 +616,7 @@ static const coding_info coding_info_list[] = {
{coding_FADPCM, "FMOD FADPCM 4-bit ADPCM"},
{coding_ASF, "Argonaut ASF 4-bit ADPCM"},
{coding_XMD, "Konami XMD 4-bit ADPCM"},
{coding_PCFX, "PC-FX 4-bit ADPCM"},
{coding_SDX2, "Squareroot-delta-exact (SDX2) 8-bit DPCM"},
{coding_SDX2_int, "Squareroot-delta-exact (SDX2) 8-bit DPCM with 1 byte interleave"},

View File

@ -1889,6 +1889,10 @@
<File
RelativePath=".\coding\ogg_vorbis_decoder.c"
>
</File>
<File
RelativePath=".\coding\pcfx_decoder.c"
>
</File>
<File
RelativePath=".\coding\pcm_decoder.c"

View File

@ -525,6 +525,7 @@
<ClCompile Include="coding\ngc_dtk_decoder.c" />
<ClCompile Include="coding\nwa_decoder.c" />
<ClCompile Include="coding\ogg_vorbis_decoder.c" />
<ClCompile Include="coding\pcfx_decoder.c" />
<ClCompile Include="coding\pcm_decoder.c" />
<ClCompile Include="coding\psv_decoder.c" />
<ClCompile Include="coding\psx_decoder.c" />

View File

@ -1120,6 +1120,9 @@
<ClCompile Include="coding\ogg_vorbis_decoder.c">
<Filter>coding\Source Files</Filter>
</ClCompile>
<ClCompile Include="coding\pcfx_decoder.c">
<Filter>coding\Source Files</Filter>
</ClCompile>
<ClCompile Include="coding\pcm_decoder.c">
<Filter>coding\Source Files</Filter>
</ClCompile>

View File

@ -4,7 +4,6 @@
#include "../util.h"
/* known GENH types */
typedef enum {
PSX = 0, /* PSX ADPCM */
@ -31,6 +30,7 @@ typedef enum {
XMA2 = 21, /* raw XMA2 */
FFMPEG = 22, /* any headered FFmpeg format */
AC3 = 23, /* AC3/SPDIF */
PCFX = 24, /* PC-FX ADPCM */
} genh_type;
typedef struct {
@ -113,6 +113,7 @@ VGMSTREAM * init_vgmstream_genh(STREAMFILE *streamFile) {
case AC3:
case FFMPEG: coding = coding_FFmpeg; break;
#endif
case PCFX: coding = coding_PCFX; break;
default:
goto fail;
}
@ -185,6 +186,14 @@ VGMSTREAM * init_vgmstream_genh(STREAMFILE *streamFile) {
}
break;
case coding_PCFX:
vgmstream->interleave_block_size = genh.interleave;
vgmstream->layout_type = layout_interleave;
if (genh.codec_mode >= 0 && genh.codec_mode <= 3)
vgmstream->codec_config = genh.codec_mode;
break;
case coding_MS_IMA:
if (!genh.interleave) goto fail; /* creates garbage */

View File

@ -30,6 +30,7 @@ typedef enum {
XMA2 = 21, /* raw XMA2 */
FFMPEG = 22, /* any headered FFmpeg format */
AC3 = 23, /* AC3/SPDIF */
PCFX = 24, /* PC-FX ADPCM */
} txth_type;
typedef struct {
@ -170,6 +171,7 @@ VGMSTREAM * init_vgmstream_txth(STREAMFILE *streamFile) {
case AC3:
case FFMPEG: coding = coding_FFmpeg; break;
#endif
case PCFX: coding = coding_PCFX; break;
default:
goto fail;
}
@ -250,8 +252,15 @@ VGMSTREAM * init_vgmstream_txth(STREAMFILE *streamFile) {
vgmstream->ch[i].adpcm_step_index = 0x7f;
}
}
break;
case coding_PCFX:
vgmstream->interleave_block_size = txth.interleave;
vgmstream->layout_type = layout_interleave;
if (txth.codec_mode >= 0 && txth.codec_mode <= 3)
vgmstream->codec_config = txth.codec_mode;
break;
case coding_MS_IMA:
if (!txth.interleave) goto fail; /* creates garbage */
@ -565,6 +574,7 @@ static int parse_keyval(STREAMFILE * streamFile_, txth_header * txth, const char
else if (0==strcmp(val,"XMA2")) txth->codec = XMA2;
else if (0==strcmp(val,"FFMPEG")) txth->codec = FFMPEG;
else if (0==strcmp(val,"AC3")) txth->codec = AC3;
else if (0==strcmp(val,"PCFX")) txth->codec = PCFX;
else goto fail;
}
else if (0==strcmp(key,"codec_mode")) {
@ -909,6 +919,8 @@ static int get_bytes_to_samples(txth_header * txth, uint32_t bytes) {
return ima_bytes_to_samples(bytes, txth->channels);
case AICA:
return aica_bytes_to_samples(bytes, txth->channels);
case PCFX:
return pcfx_bytes_to_samples(bytes, txth->channels);
/* untested */
case SDX2:

View File

@ -1148,6 +1148,7 @@ int get_vgmstream_samples_per_frame(VGMSTREAM * vgmstream) {
case coding_WV6_IMA:
case coding_ALP_IMA:
case coding_FFTA2_IMA:
case coding_PCFX:
return 2;
case coding_XBOX_IMA:
case coding_XBOX_IMA_mch:
@ -1320,6 +1321,7 @@ int get_vgmstream_frame_size(VGMSTREAM * vgmstream) {
case coding_WV6_IMA:
case coding_ALP_IMA:
case coding_FFTA2_IMA:
case coding_PCFX:
return 0x01;
case coding_MS_IMA:
case coding_RAD_IMA:
@ -2019,6 +2021,13 @@ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to
vgmstream->interleave_block_size);
}
break;
case coding_PCFX:
for (ch = 0; ch < vgmstream->channels; ch++) {
decode_pcfx(&vgmstream->ch[ch],buffer+samples_written*vgmstream->channels+ch,
vgmstream->channels,vgmstream->samples_into_block,samples_to_do, vgmstream->codec_config);
}
break;
case coding_EA_MT:
for (ch = 0; ch < vgmstream->channels; ch++) {
decode_ea_mt(vgmstream, buffer+samples_written*vgmstream->channels+ch,

View File

@ -149,6 +149,7 @@ typedef enum {
coding_FADPCM, /* FMOD FADPCM 4-bit ADPCM */
coding_ASF, /* Argonaut ASF 4-bit ADPCM */
coding_XMD, /* Konami XMD 4-bit ADPCM */
coding_PCFX, /* PC-FX 4-bit ADPCM */
/* others */
coding_SDX2, /* SDX2 2:1 Squareroot-Delta-Exact compression DPCM */