diff --git a/doc/TXTH.md b/doc/TXTH.md
index 3190f031..f78dcf00 100644
--- a/doc/TXTH.md
+++ b/doc/TXTH.md
@@ -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)
diff --git a/src/coding/coding.h b/src/coding/coding.h
index 9d22b024..676a313f 100644
--- a/src/coding/coding.h
+++ b/src/coding/coding.h
@@ -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);
diff --git a/src/coding/pcfx_decoder.c b/src/coding/pcfx_decoder.c
new file mode 100644
index 00000000..63cac856
--- /dev/null
+++ b/src/coding/pcfx_decoder.c
@@ -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;
+}
diff --git a/src/formats.c b/src/formats.c
index 4eb018c5..37390142 100644
--- a/src/formats.c
+++ b/src/formats.c
@@ -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"},
diff --git a/src/libvgmstream.vcproj b/src/libvgmstream.vcproj
index 7e6b87fb..51b4b2ea 100644
--- a/src/libvgmstream.vcproj
+++ b/src/libvgmstream.vcproj
@@ -1889,6 +1889,10 @@
+
+
+
diff --git a/src/libvgmstream.vcxproj.filters b/src/libvgmstream.vcxproj.filters
index bc857d48..4d45e4e1 100644
--- a/src/libvgmstream.vcxproj.filters
+++ b/src/libvgmstream.vcxproj.filters
@@ -1120,6 +1120,9 @@
coding\Source Files
+
+ coding\Source Files
+
coding\Source Files
diff --git a/src/meta/genh.c b/src/meta/genh.c
index 86b91fd4..acdf9d01 100644
--- a/src/meta/genh.c
+++ b/src/meta/genh.c
@@ -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 */
diff --git a/src/meta/txth.c b/src/meta/txth.c
index 04e5fb4c..40165d5b 100644
--- a/src/meta/txth.c
+++ b/src/meta/txth.c
@@ -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:
diff --git a/src/vgmstream.c b/src/vgmstream.c
index fc7cb3cb..e2b36462 100644
--- a/src/vgmstream.c
+++ b/src/vgmstream.c
@@ -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,
diff --git a/src/vgmstream.h b/src/vgmstream.h
index 0770669b..eef1c601 100644
--- a/src/vgmstream.h
+++ b/src/vgmstream.h
@@ -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 */