From 2a7b645d239cc43438e78e2e34427807b419ce3a Mon Sep 17 00:00:00 2001 From: bnnm Date: Fri, 16 Dec 2016 20:29:02 +0100 Subject: [PATCH 1/6] Fixed PS2 MSS sample count and description --- src/meta/ps2_mss.c | 10 ++++++++-- src/vgmstream.c | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/meta/ps2_mss.c b/src/meta/ps2_mss.c index c0a96c27..7cc2d74c 100644 --- a/src/meta/ps2_mss.c +++ b/src/meta/ps2_mss.c @@ -1,6 +1,11 @@ #include "meta.h" #include "../util.h" +/** + * Guerrilla's MSS + * + * Found in ShellShock Nam '67, Killzone (PS2) + */ VGMSTREAM * init_vgmstream_ps2_mss(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; char filename[PATH_LIMIT]; @@ -26,9 +31,10 @@ VGMSTREAM * init_vgmstream_ps2_mss(STREAMFILE *streamFile) { /* fill in the vital statistics */ start_offset = read_32bitLE(0x08,streamFile); vgmstream->channels = channel_count; + /*datasize = read_32bitLE(0x0c,streamFile) */ vgmstream->sample_rate = read_32bitLE(0x10,streamFile); - vgmstream->coding_type = coding_PSX; - vgmstream->num_samples = read_32bitLE(0x1C,streamFile); + vgmstream->num_samples = read_32bitLE(0x1C,streamFile);/* / 16 * 28 */ + vgmstream->coding_type = coding_PSX; if (channel_count == 1) diff --git a/src/vgmstream.c b/src/vgmstream.c index 33676028..ad2e2b19 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -3222,7 +3222,7 @@ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length) { snprintf(temp,TEMPSIZE,"Mini Ninjas 'STR' header"); break; case meta_PS2_MSS: - snprintf(temp,TEMPSIZE,"ShellShock Nam '67 'MSCC' header"); + snprintf(temp,TEMPSIZE,"Guerilla MSCC header"); break; case meta_PS2_HSF: snprintf(temp,TEMPSIZE,"Lowrider 'HSF' header"); From 27868be72367988d6af3b49f6179fcafd153aa42 Mon Sep 17 00:00:00 2001 From: bnnm Date: Fri, 16 Dec 2016 20:34:44 +0100 Subject: [PATCH 2/6] Fixed v5 header coef positions --- src/meta/mca.c | 62 +++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/meta/mca.c b/src/meta/mca.c index abdc5fed..518dd16b 100644 --- a/src/meta/mca.c +++ b/src/meta/mca.c @@ -8,11 +8,11 @@ Capcom MADP format found in Capcom 3DS games. VGMSTREAM * init_vgmstream_mca(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; char filename[PATH_LIMIT]; - coding_t coding_type; int channel_count; int loop_flag; - off_t start_offset; - off_t coef_offset; + int version; + size_t head_size, data_size; + off_t start_offset, coef_offset, coef_start, coef_shift; int i, j; int coef_spacing; @@ -26,44 +26,60 @@ VGMSTREAM * init_vgmstream_mca(STREAMFILE *streamFile) { if ((uint32_t)read_32bitBE(0, streamFile) != 0x4D414450) /* "MADP" */ goto fail; - start_offset = (get_streamfile_size(streamFile) - read_32bitLE(0x20, streamFile)); - channel_count = read_8bit(0x8, streamFile); - - if (read_32bitLE(0x18, streamFile) > 0) - loop_flag = 1; - else - loop_flag = 0; - coding_type = coding_NGC_DSP; - - if (channel_count < 1) goto fail; + if (channel_count < 1) goto fail; + loop_flag = read_32bitLE(0x18, streamFile) > 0; /* build the VGMSTREAM */ - vgmstream = allocate_vgmstream(channel_count, loop_flag); if (!vgmstream) goto fail; - /* fill in the vital statistics */ + vgmstream->interleave_block_size = read_16bitLE(0xa, streamFile); /* guessed, only seen 0x100 */ vgmstream->num_samples = read_32bitLE(0xc, streamFile); vgmstream->sample_rate = (uint16_t)read_16bitLE(0x10, streamFile); - /* channels and loop flag are set by allocate_vgmstream */ - vgmstream->loop_start_sample = read_32bitLE(0x14, streamFile); vgmstream->loop_end_sample = read_32bitLE(0x18, streamFile); - vgmstream->coding_type = coding_type; + vgmstream->coding_type = coding_NGC_DSP; if (channel_count == 1) vgmstream->layout_type = layout_none; else vgmstream->layout_type = layout_interleave; - vgmstream->interleave_block_size = 0x100; // Constant for this format vgmstream->meta_type = meta_MCA; + + /* find data/coef offsets (guessed, formula may change with version) */ + version = read_16bitLE(0x04, streamFile); + coef_spacing = 0x30; + data_size = read_32bitLE(0x20, streamFile); + + if (version <= 0x3) { /* v3: Resident Evil Mercenaries 3D, Super Street Fighter IV 3D */ + head_size = get_streamfile_size(streamFile) - data_size; /* probably 0x2c + 0x30*ch */ + coef_shift = 0x0; + coef_start = head_size - coef_spacing * channel_count; + + start_offset = head_size; + coef_offset = coef_start + coef_shift * 0x14; + + } else if (version == 0x4) { /* v4: EX Troopers, Ace Attourney 5 */ + head_size = read_16bitLE(0x1c, streamFile); + coef_shift = read_16bitLE(0x28, streamFile); + coef_start = head_size - coef_spacing * channel_count; + + start_offset = head_size; + coef_offset = coef_start + coef_shift * 0x14; + + } else { /* v5: Ace Attourney 6, Monster Hunter Generations, v6+? */ + head_size = read_16bitLE(0x1c, streamFile); /* partial size */ + coef_shift = read_16bitLE(0x28, streamFile); + coef_start = head_size - coef_spacing * channel_count; + + start_offset = read_32bitLE(coef_start - 0x4, streamFile); + coef_offset = coef_start + coef_shift * 0x14; + } + - - coef_offset = start_offset - (vgmstream->channels * 0x30); - coef_spacing = 0x30; - + /* set up ADPCM coefs */ for (j = 0; jchannels; j++) { for (i = 0; i<16; i++) { vgmstream->ch[j].adpcm_coef[i] = read_16bitLE(coef_offset + j*coef_spacing + i * 2, streamFile); From d1dc2b60823e9811c7f94df6ff1d8cfe040b419c Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 18 Dec 2016 10:24:14 +0100 Subject: [PATCH 3/6] Added .BCWAV dual stereo (3DS Lego games) --- src/vgmstream.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/vgmstream.c b/src/vgmstream.c index ad2e2b19..25bb989f 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -394,7 +394,8 @@ VGMSTREAM * init_vgmstream_internal(STREAMFILE *streamFile, int do_dfs) { (vgmstream->meta_type == meta_NGCA) || (vgmstream->meta_type == meta_NUB_VAG) || (vgmstream->meta_type == meta_SPT_SPD) || - (vgmstream->meta_type == meta_EB_SFX) + (vgmstream->meta_type == meta_EB_SFX) || + (vgmstream->meta_type == meta_CWAV) ) && vgmstream->channels == 1) { try_dual_file_stereo(vgmstream, streamFile); } From ce040bbb363811371d3f764ab9fd9bb64f6c3a12 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 18 Dec 2016 12:46:11 +0100 Subject: [PATCH 4/6] Added put_8bit for consistency/clarity --- src/util.c | 4 ++++ src/util.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/util.c b/src/util.c index e3dc5021..a413206e 100644 --- a/src/util.c +++ b/src/util.c @@ -57,6 +57,10 @@ void interleave_stereo(sample * buffer, int32_t sample_count) { } */ +void put_8bit(uint8_t * buf, int8_t i) { + buf[0] = i; +} + void put_16bitLE(uint8_t * buf, int16_t i) { buf[0] = (i & 0xFF); buf[1] = i >> 8; diff --git a/src/util.h b/src/util.h index 63bce33b..9fbf7f00 100644 --- a/src/util.h +++ b/src/util.h @@ -25,6 +25,8 @@ static inline int32_t get_32bitLE(uint8_t * p) { return (p[0]) | (p[1]<<8) | (p[2]<<16) | (p[3]<<24); } +void put_8bit(uint8_t * buf, int8_t i); + void put_16bitLE(uint8_t * buf, int16_t i); void put_32bitLE(uint8_t * buf, int32_t i); From c1c1cd1ba66f834f837858af563600bc069490d3 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 18 Dec 2016 13:10:08 +0100 Subject: [PATCH 5/6] init_ffmpeg accepts any generic header; init_seek edge cases (for XMA) --- src/meta/ffmpeg.c | 117 ++++++++++++++++++++++++++++++---------------- src/meta/meta.h | 1 + 2 files changed, 77 insertions(+), 41 deletions(-) diff --git a/src/meta/ffmpeg.c b/src/meta/ffmpeg.c index 9525bd97..f0776351 100644 --- a/src/meta/ffmpeg.c +++ b/src/meta/ffmpeg.c @@ -186,18 +186,73 @@ static int64_t ffmpeg_seek(void *opaque, int64_t offset, int whence) /** - * Manually init FFmpeg only, from an offset. + * Manually init FFmpeg, from an offset. * Can be used if the stream has an extra header over data recognized by FFmpeg. */ ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size) { - return init_ffmpeg_faux_riff(streamFile, -1, start, size, 0); + return init_ffmpeg_header_offset(streamFile, NULL, 0, start, size); +} + + +/** + * Manually init FFmpeg, from an offset and creating a fake RIFF from a streamfile. + */ +ffmpeg_codec_data * init_ffmpeg_faux_riff(STREAMFILE *streamFile, int64_t fmt_offset, uint64_t start, uint64_t size, int big_endian) { + if (fmt_offset > 0) { + size_t header_size = 0; + int max_header_size = (int)(start - fmt_offset); + uint8_t p[100]; + if (max_header_size < 18 || max_header_size > 100) + goto fail; + //p = av_malloc(max_header_size + 8 + 4 + 8 + 8); + //if (!p) goto fail; + if (read_streamfile(p + 8 + 4 + 8, fmt_offset, max_header_size, streamFile) != max_header_size) + goto fail; + + if (big_endian) { + int shift = 8 + 4 + 8; + put_16bitLE(p+shift, get_16bitBE(p)); + put_16bitLE(p+shift + 2, get_16bitBE(p + 2)); + put_32bitLE(p+shift + 4, get_32bitBE(p + 4)); + put_32bitLE(p+shift + 8, get_32bitBE(p + 8)); + put_16bitLE(p+shift + 12, get_16bitBE(p + 12)); + put_16bitLE(p+shift + 14, get_16bitBE(p + 14)); + put_16bitLE(p+shift + 16, get_16bitBE(p + 16)); + } + header_size = 8 + 4 + 8 + 8 + 18 + get_16bitLE(p + 8 + 4 + 8 + 16); + // Meh, dunno how to handle swapping the extra data + // FFmpeg doesn't need most of this data anyway + if ((unsigned)(get_16bitLE(p + 8 + 4 + 8) - 0x165) < 2) + memset(p + 8 + 4 + 8 + 18, 0, 34); + + // Fill out the RIFF structure + memcpy(p, "RIFF", 4); + put_32bitLE(p + 4, header_size + size - 8); + memcpy(p + 8, "WAVE", 4); + memcpy(p + 12, "fmt ", 4); + put_32bitLE(p + 16, 18 + get_16bitLE(p + 8 + 4 + 8 + 16)); + memcpy(p + header_size - 8, "data", 4); + put_32bitLE(p + header_size - 4, size); + + + return init_ffmpeg_header_offset(streamFile, p, header_size, start, size); + } + else { + return init_ffmpeg_header_offset(streamFile, NULL, 0, start, size); + } + +fail: + return NULL; } /** - * Manually init FFmpeg only, from an offset / fake RIFF. - * Can insert a fake RIFF header, to trick FFmpeg into demuxing/decoding the stream. + * Manually init FFmpeg, from a fake header / offset. + * + * Can take a fake header, to trick FFmpeg into demuxing/decoding the stream. + * This header will be seamlessly inserted before 'start' offset, and total filesize will be 'header_size' + 'size'. + * The header buffer will be copied and memory-managed internally. */ -ffmpeg_codec_data * init_ffmpeg_faux_riff(STREAMFILE *streamFile, int64_t fmt_offset, uint64_t start, uint64_t size, int big_endian) { +ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size) { char filename[PATH_LIMIT]; ffmpeg_codec_data * data; @@ -226,42 +281,13 @@ ffmpeg_codec_data * init_ffmpeg_faux_riff(STREAMFILE *streamFile, int64_t fmt_of data->size = size; - /* insert fake RIFF header to trick FFmpeg into demuxing/decoding the stream */ - if (fmt_offset > 0) { - int max_header_size = (int)(start - fmt_offset); - uint8_t *p; - if (max_header_size < 18) goto fail; - data->header_insert_block = p = av_malloc(max_header_size + 8 + 4 + 8 + 8); + /* insert fake header to trick FFmpeg into demuxing/decoding the stream */ + if (header_size > 0) { + data->header_size = header_size; + data->header_insert_block = av_memdup(header, header_size); if (!data->header_insert_block) goto fail; - if (read_streamfile(p + 8 + 4 + 8, fmt_offset, max_header_size, streamFile) != max_header_size) goto fail; - if (big_endian) { - p += 8 + 4 + 8; - put_16bitLE(p, get_16bitBE(p)); - put_16bitLE(p + 2, get_16bitBE(p + 2)); - put_32bitLE(p + 4, get_32bitBE(p + 4)); - put_32bitLE(p + 8, get_32bitBE(p + 8)); - put_16bitLE(p + 12, get_16bitBE(p + 12)); - put_16bitLE(p + 14, get_16bitBE(p + 14)); - put_16bitLE(p + 16, get_16bitBE(p + 16)); - p -= 8 + 4 + 8; - } - data->header_size = 8 + 4 + 8 + 8 + 18 + get_16bitLE(p + 8 + 4 + 8 + 16); - // Meh, dunno how to handle swapping the extra data - // FFmpeg doesn't need most of this data anyway - if ((unsigned)(get_16bitLE(p + 8 + 4 + 8) - 0x165) < 2) - memset(p + 8 + 4 + 8 + 18, 0, 34); - - // Fill out the RIFF structure - memcpy(p, "RIFF", 4); - put_32bitLE(p + 4, data->header_size + size - 8); - memcpy(p + 8, "WAVE", 4); - memcpy(p + 12, "fmt ", 4); - put_32bitLE(p + 16, 18 + get_16bitLE(p + 8 + 4 + 8 + 16)); - memcpy(p + data->header_size - 8, "data", 4); - put_32bitLE(p + data->header_size - 4, size); } - /* setup IO, attempt to autodetect format and gather some info */ data->buffer = av_malloc(FFMPEG_DEFAULT_IO_BUFFER_SIZE); if (!data->buffer) goto fail; @@ -411,8 +437,8 @@ fail: static int init_seek(ffmpeg_codec_data * data) { int ret, ts_index, found_first = 0; int64_t ts = 0; - int64_t pos; /* offset */ - int size; /* coded size */ + int64_t pos = 0; /* offset */ + int size = 0; /* coded size */ int distance = 0; /* always? */ AVStream * stream; @@ -436,7 +462,7 @@ static int init_seek(ffmpeg_codec_data * data) { av_packet_unref(pkt); ret = av_read_frame(data->formatCtx, pkt); if (ret < 0) - goto fail; + break; if (pkt->stream_index != data->streamIndex) continue; /* ignore non-selected streams */ @@ -450,6 +476,15 @@ static int init_seek(ffmpeg_codec_data * data) { break; } } + if (!found_first) + goto fail; + + /* in rare cases there is only one packet */ + /* if (size == 0) { size = data_end - pos; } */ /* no easy way to know, ignore (most formats don's need size) */ + + /* some formats (XMA1) don't seem to have packet.dts, pretend it's 0 */ + if (ts == INT64_MIN) + ts = 0; /* apparently some (non-audio?) streams start with a DTS before 0, but some read_seeks expect 0, which would disrupt the index * we may need to keep start_ts around, since avstream/codec/format isn't always set */ diff --git a/src/meta/meta.h b/src/meta/meta.h index 8a9c6767..5a69358f 100644 --- a/src/meta/meta.h +++ b/src/meta/meta.h @@ -119,6 +119,7 @@ VGMSTREAM * init_vgmstream_hca_offset(STREAMFILE *streamFile, uint64_t start, ui #ifdef VGM_USE_FFMPEG ffmpeg_codec_data * init_ffmpeg_faux_riff(STREAMFILE *streamFile, int64_t fmt_offset, uint64_t stream_offset, uint64_t stream_size, int fmt_big_endian); ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size); +ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size); void free_ffmpeg(ffmpeg_codec_data *); From 81408005878a57bb75692dbddafd590b12bf2c50 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 18 Dec 2016 18:12:27 +0100 Subject: [PATCH 6/6] Added proper/looped .XMA --- fb2k/in_vgmstream.cpp | 4 + src/Makefile | 3 +- src/libvgmstream.vcproj | 4 + src/libvgmstream.vcxproj | 1 + src/meta/Makefile.unix.am | 1 + src/meta/meta.h | 2 + src/meta/xma.c | 396 ++++++++++++++++++++++++++++++++++++++ src/vgmstream.c | 1 + winamp/in_vgmstream.c | 2 + xmp-vgmstream/DllMain.c | 2 +- 10 files changed, 414 insertions(+), 2 deletions(-) create mode 100644 src/meta/xma.c diff --git a/fb2k/in_vgmstream.cpp b/fb2k/in_vgmstream.cpp index 5c7b3cfd..28883081 100644 --- a/fb2k/in_vgmstream.cpp +++ b/fb2k/in_vgmstream.cpp @@ -608,6 +608,8 @@ bool input_vgmstream::g_is_our_path(const char * p_path,const char * p_extension if(!stricmp_utf8(p_extension,"xa2")) return 1; if(!stricmp_utf8(p_extension,"xa30")) return 1; if(!stricmp_utf8(p_extension,"xau")) return 1; + if(!stricmp_utf8(p_extension,"xma")) return 1; + if(!stricmp_utf8(p_extension,"xma2")) return 1; if(!stricmp_utf8(p_extension,"xmu")) return 1; if(!stricmp_utf8(p_extension,"xnb")) return 1; if(!stricmp_utf8(p_extension,"xsf")) return 1; @@ -945,6 +947,8 @@ DECLARE_MULTIPLE_FILE_TYPE("PSX CD-XA File (*.XA)", xa); DECLARE_MULTIPLE_FILE_TYPE("XA2 Audio File (*.XA2)", xa2); DECLARE_MULTIPLE_FILE_TYPE("XA30 Audio File (*.XA30)", xa30); DECLARE_MULTIPLE_FILE_TYPE("XAU Audio File (*.XAU)", xau); +DECLARE_MULTIPLE_FILE_TYPE("XMA Audio File (*.XMA)", xma); +DECLARE_MULTIPLE_FILE_TYPE("XMA2 Audio File (*.XMA2)", xma2); DECLARE_MULTIPLE_FILE_TYPE("XMU Audio File (*.XMU)", xmu); DECLARE_MULTIPLE_FILE_TYPE("XNB Audio File (*.XNB)", xnb); DECLARE_MULTIPLE_FILE_TYPE("XSF Audio File (*.XSF)", xsf); diff --git a/src/Makefile b/src/Makefile index fcdc24ba..d344912f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -306,7 +306,8 @@ META_OBJS=meta/adx_header.o \ meta/hca.o \ meta/ps2_svag_snk.o \ meta/ffmpeg.o \ - meta/mp4.o + meta/mp4.o \ + meta/xma.o EXT_LIBS = ../ext_libs/clHCA.o diff --git a/src/libvgmstream.vcproj b/src/libvgmstream.vcproj index bc991683..f70e207d 100644 --- a/src/libvgmstream.vcproj +++ b/src/libvgmstream.vcproj @@ -1166,6 +1166,10 @@ RelativePath=".\meta\xbox_xwav.c" > + + diff --git a/src/libvgmstream.vcxproj b/src/libvgmstream.vcxproj index 4115b231..21f3f199 100644 --- a/src/libvgmstream.vcxproj +++ b/src/libvgmstream.vcxproj @@ -351,6 +351,7 @@ + diff --git a/src/meta/Makefile.unix.am b/src/meta/Makefile.unix.am index b6fb0f52..3dbfca57 100644 --- a/src/meta/Makefile.unix.am +++ b/src/meta/Makefile.unix.am @@ -247,5 +247,6 @@ libmeta_la_SOURCES += btsnd.c libmeta_la_SOURCES += hca.c libmeta_la_SOURCES += ps2_svag_snk.c libmeta_la_SOURCES += mp4.c +libmeta_la_SOURCES += xma.c EXTRA_DIST = meta.h diff --git a/src/meta/meta.h b/src/meta/meta.h index 5a69358f..689aa56d 100644 --- a/src/meta/meta.h +++ b/src/meta/meta.h @@ -662,4 +662,6 @@ VGMSTREAM * init_vgmstream_btsnd(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_ps2_svag_snk(STREAMFILE* streamFile); +VGMSTREAM * init_vgmstream_xma(STREAMFILE* streamFile); + #endif diff --git a/src/meta/xma.c b/src/meta/xma.c new file mode 100644 index 00000000..ccddbef8 --- /dev/null +++ b/src/meta/xma.c @@ -0,0 +1,396 @@ +#include "meta.h" +#include "../util.h" + +#define ADJUST_SAMPLE_RATE 0 +#define XMA_BYTES_PER_PACKET 2048 +#define XMA_SAMPLES_PER_FRAME 512 +#define XMA_SAMPLES_PER_SUBFRAME 128 +#define FAKE_RIFF_BUFFER_SIZE 100 + + +/* parsing helper */ +typedef struct { + size_t file_size; + /* file traversing */ + int big_endian; + off_t chunk_offset; /* main header chunk offset, after "(id)" and size */ + size_t chunk_size; + off_t data_offset; + size_t data_size; + + int32_t fmt_codec; + uint8_t xma2_version; + int needs_header; + + /* info */ + int loop_flag; + int32_t num_samples; + int32_t loop_start_sample; + int32_t loop_end_sample; + + int32_t xma1_loop_start_offset_b; + int32_t xma1_loop_end_offset_b; + int32_t xma1_subframe_data; +} xma_header_data; + + +static int parse_header(xma_header_data * xma, STREAMFILE *streamFile); +static void parse_xma1_sample_data(xma_header_data * xma, STREAMFILE *streamFile); +static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * xma, STREAMFILE *streamFile); +#if ADJUST_SAMPLE_RATE +static int get_xma_sample_rate(int32_t general_rate); +#endif + +/** + * XMA 1/2 (Microsoft) + * + * Usually in RIFF headers and minor variations. + */ +VGMSTREAM * init_vgmstream_xma(STREAMFILE *streamFile) { + char filename[PATH_LIMIT]; + VGMSTREAM * vgmstream = NULL; + ffmpeg_codec_data *data = NULL; + + xma_header_data xma; + uint8_t fake_riff[FAKE_RIFF_BUFFER_SIZE]; + int fake_riff_size = 0; + + + /* check extension, case insensitive */ + streamFile->get_name(streamFile,filename,sizeof(filename)); + if (strcasecmp("xma",filename_extension(filename)) + && strcasecmp("xma2",filename_extension(filename)) ) /* Skullgirls */ + goto fail; + + /* check header */ + if ( !parse_header(&xma, streamFile) ) + goto fail; + + + /* init ffmpeg (create a fake RIFF that FFmpeg can read if needed) */ + if (xma.needs_header) { /* fake header + partial size */ + fake_riff_size = create_riff_header(fake_riff, FAKE_RIFF_BUFFER_SIZE, &xma, streamFile); + if (fake_riff_size <= 0) goto fail; + + data = init_ffmpeg_header_offset(streamFile, fake_riff, (uint64_t)fake_riff_size, xma.data_offset+4+4, xma.data_size); + if (!data) goto fail; + } + else { /* no change */ + data = init_ffmpeg_offset(streamFile, 0, xma.file_size); + if (!data) goto fail; + } + + + /* build VGMSTREAM */ + vgmstream = allocate_vgmstream(data->channels, xma.loop_flag); + if (!vgmstream) goto fail; + /*vgmstream->channels = data->channels;*/ + /*vgmstream->loop_flag = loop_flag;*/ + + vgmstream->codec_data = data; + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + vgmstream->meta_type = meta_FFmpeg; + + vgmstream->sample_rate = data->sampleRate; +#if ADJUST_SAMPLE_RATE + vgmstream->sample_rate = get_xma_sample_rate(vgmstream->sample_rate); +#endif + vgmstream->num_samples = xma.num_samples; /* data->totalSamples: XMA1 = not set; XMA2 = not reliable */ + + if (vgmstream->loop_flag) { + vgmstream->loop_start_sample = xma.loop_start_sample; + vgmstream->loop_end_sample = xma.loop_end_sample; + } + + + return vgmstream; + +fail: + /* clean up */ + if (data) { + free_ffmpeg(data); + } + if (vgmstream) { + vgmstream->codec_data = NULL; + close_vgmstream(vgmstream); + } + return NULL; +} + + +/** + * Finds stuff needed for XMA with FFmpeg + * + * returns 1 if ok, 0 on error + */ +static int parse_header(xma_header_data * xma, STREAMFILE *streamFile) { + int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; + int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; + uint32_t id; + enum { RIFF } header_type; + int big_endian = 0; + + + /* check header */ + id = read_32bitBE(0x00,streamFile); + if (id == 0x52494646 || id == 0x52494658) { /* "RIFF" / "RIFX" */ + big_endian = id == 0x52494658; + header_type = RIFF; + } + else { + goto fail; + } + + + memset(xma,0,sizeof(xma_header_data)); + xma->big_endian = big_endian; + + if (xma->big_endian) { + read_32bit = read_32bitBE; + read_16bit = read_16bitBE; + } else { + read_32bit = read_32bitLE; + read_16bit = read_16bitLE; + } + + xma->file_size = streamFile->get_size(streamFile); + + /* find offsets */ + if (header_type == RIFF) { /* regular RIFF header */ + off_t current_chunk = 0xc; + off_t fmt_offset = 0, xma2_offset = 0; + size_t riff_size = 0, fmt_size = 0, xma2_size = 0; + + riff_size = read_32bit(4,streamFile); + if (riff_size+8 > xma->file_size) goto fail; + + while (current_chunk < xma->file_size && current_chunk < riff_size+8) { + uint32_t chunk_type = read_32bitBE(current_chunk,streamFile); + off_t chunk_size = read_32bit(current_chunk+4,streamFile); + + if (current_chunk+4+4+chunk_size > xma->file_size) + goto fail; + + switch(chunk_type) { + case 0x666d7420: /* "fmt " */ + if (fmt_offset) goto fail; + + fmt_offset = current_chunk + 4 + 4; + fmt_size = chunk_size; + break; + case 0x64617461: /* "data" */ + if (xma->data_offset) goto fail; + + xma->data_offset = current_chunk; + xma->data_size = chunk_size; + break; + case 0x584D4132: /* "XMA2" */ + if (xma2_offset) goto fail; + + xma2_offset = current_chunk + 4 + 4; + xma2_size = chunk_size; + break; + default: + break; + } + + current_chunk += 8+chunk_size; + } + + /* give priority to "XMA2" since it can go together with "fmt " */ + if (xma2_offset) { + xma->chunk_offset = xma2_offset; + xma->chunk_size = xma2_size; + xma->xma2_version = read_8bit(xma->chunk_offset,streamFile); + xma->needs_header = 1; /* FFmpeg can only parse pure XMA1 or pure XMA2 */ + } else if (fmt_offset) { + xma->chunk_offset = fmt_offset; + xma->chunk_size = fmt_size; + xma->fmt_codec = read_16bit(xma->chunk_offset,streamFile); + } else { + goto fail; + } + } else { + goto fail; + } + + + /* find sample data */ + if (xma->xma2_version) { /* old XMA2 (internally always BE) */ + xma->loop_start_sample = read_32bitBE(xma->chunk_offset+0x4,streamFile); + xma->loop_end_sample = read_32bitBE(xma->chunk_offset+0x8,streamFile); + xma->loop_flag = (uint8_t)read_8bit(xma->chunk_offset+0x3,streamFile) > 0 /* rarely not set */ + || xma->loop_end_sample; + if (xma->xma2_version == 3) { + xma->num_samples = read_32bitBE(xma->chunk_offset+0x14,streamFile); + } else { + xma->num_samples = read_32bitBE(xma->chunk_offset+0x1C,streamFile); + } + } + else if (xma->fmt_codec == 0x166) { /* pure XMA2 */ + xma->num_samples = read_32bit(xma->chunk_offset+0x18,streamFile); + xma->loop_start_sample = read_32bit(xma->chunk_offset+0x28,streamFile); + xma->loop_end_sample = xma->loop_start_sample + read_32bit(xma->chunk_offset+0x2C,streamFile); + xma->loop_flag = (uint8_t)read_8bit(xma->chunk_offset+0x30,streamFile) > 0 /* never set in practice */ + || xma->loop_end_sample; + /* not needed but may affect looping? (sometimes these don't match loop/total samples) */ + /* int32_t play_begin_sample = read_32bit(xma->fmt_offset+0x28,streamFile); */ + /* int32_t play_end_sample = play_begin_sample + read_32bit(xma->fmt_offset+0x24,streamFile); */ + } + else if (xma->fmt_codec == 0x165) { /* pure XMA1 */ + xma->loop_flag = (uint8_t)read_8bit(xma->chunk_offset+0xA,streamFile) > 0; + xma->xma1_loop_start_offset_b = read_32bit(xma->chunk_offset+0x14,streamFile); + xma->xma1_loop_end_offset_b = read_32bit(xma->chunk_offset+0x18,streamFile); + xma->xma1_subframe_data = (uint8_t)read_8bit(xma->chunk_offset+0x1C,streamFile); + /* find samples count + loop samples since they are not in the header */ + parse_xma1_sample_data(xma, streamFile); + } + else { /* RIFF with no XMA data or unknown version */ + goto fail; + } + + return 1; + +fail: + return 0; +} + + +/** + * XMA1: manually find total and loop samples + * + * A XMA1 stream is made of packets, each containing N frames of X samples, and frame is divided into subframes for looping purposes. + * FFmpeg can't get total samples without decoding, so we'll count frames+samples by reading packet headers. + */ +static void parse_xma1_sample_data(xma_header_data * xma, STREAMFILE *streamFile) { + int frames = 0, loop_frame_start = 0, loop_frame_end = 0, loop_subframe_end, loop_subframe_skip; + uint32_t header, first_frame_b, packet_skip_b, frame_size_b, packet_size_b; + uint64_t packet_offset_b, frame_offset_b; + uint32_t size; + + uint32_t packet_size = XMA_BYTES_PER_PACKET; + uint32_t offset = xma->data_offset + 4 + 4; + uint32_t offset_b = 0; + uint32_t stream_offset_b = (xma->data_offset + 4 + 4) * 8; + + size = offset + xma->data_size; + packet_size_b = packet_size*8; + + while (offset < size) { /* stream global offset*/ + /* XMA1 packet header (32b) = packet_sequence:4, unk:2: frame_offset_in_bits:15, packet_stream_skip_count:11 */ + header = read_32bitBE(offset, streamFile); + first_frame_b = (header >> 11) & 0x7FFF; + packet_skip_b = (header) & 0x7FF; + + offset_b = offset * 8; + packet_offset_b = 4*8 + first_frame_b; + while (packet_offset_b < packet_size_b && packet_skip_b!=0x7FF) { /* internal packet offset + full packet skip (needed?) */ + frame_offset_b = offset_b + packet_offset_b; /* global offset to packet, in bits for aligment stuff */ + + /* XMA1 frame header (32b) = frame_length_in_bits:15, frame_data:17+ */ + header = read_32bitBE(frame_offset_b/8, streamFile); + frame_size_b = (header >> (17 - frame_offset_b % 8)) & 0x7FFF; + + if (frame_size_b == 0) /* observed in some files */ + break; + + packet_offset_b += frame_size_b;/* including header */ + + if (frame_size_b != 0x7FFF) /* end frame marker*/ + frames++; + + if (xma->loop_flag && frame_offset_b - stream_offset_b == xma->xma1_loop_start_offset_b) + loop_frame_start = frames; + if (xma->loop_flag && frame_offset_b - stream_offset_b == xma->xma1_loop_end_offset_b) + loop_frame_end = frames; + } + + offset += packet_size; + } + + loop_subframe_end = xma->xma1_subframe_data >> 4; /* upper 4b: subframe where the loop ends, 0..3 */ + loop_subframe_skip = xma->xma1_subframe_data & 0xF; /* lower 4b: subframe where the loop starts, 0..4 */ + + xma->num_samples = frames * XMA_SAMPLES_PER_FRAME; + if (xma->loop_flag) { + xma->loop_start_sample = loop_frame_start * XMA_SAMPLES_PER_FRAME + loop_subframe_skip * XMA_SAMPLES_PER_SUBFRAME; + xma->loop_end_sample = loop_frame_end * XMA_SAMPLES_PER_FRAME + loop_subframe_end * XMA_SAMPLES_PER_SUBFRAME; + } +} + + +/** + * Recreates a RIFF header that FFmpeg can read since it lacks support for some variations. + * + * returns bytes written (up until "data" chunk + size), -1 on failure + */ +static int create_riff_header(uint8_t * buf, size_t buf_size, xma_header_data * xma, STREAMFILE *streamFile) { + void (*put_32bit)(uint8_t *, int32_t) = NULL; + uint8_t chunk[FAKE_RIFF_BUFFER_SIZE]; + uint8_t internal[FAKE_RIFF_BUFFER_SIZE]; + size_t head_size, file_size, internal_size; + + if (xma->big_endian) { + put_32bit = put_32bitBE; + } else { + put_32bit = put_32bitLE; + } + + memset(buf,0, sizeof(uint8_t) * buf_size); + if (read_streamfile(chunk,xma->chunk_offset,xma->chunk_size, streamFile) != xma->chunk_size) + goto fail; + + /* create internal chunks */ + if (xma->xma2_version == 3) { /* old XMA2 v3: change to v4 (extra 8 bytes in the middle) */ + internal_size = 4+4+xma->chunk_size + 8; + + memcpy(internal + 0x0, "XMA2", 4); /* "XMA2" chunk (interal data is BE) */ + put_32bit(internal + 0x4, xma->chunk_size + 8); /* v3 > v4 size*/ + put_8bit(internal + 0x8, 4); /* v4 */ + memcpy(internal + 0x9, chunk+1, 15); /* first v3 part (fixed) */ + put_32bitBE(internal + 0x18, 0); /* extra v4 BE: "EncodeOptions" (not used by FFmpeg) */ + put_32bitBE(internal + 0x1c, 0); /* extra v4 BE: "PsuedoBytesPerSec" (not used by FFmpeg) */ + memcpy(internal + 0x20, chunk+16, xma->chunk_size - 16); /* second v3 part (variable) */ + } + else { /* direct copy (old XMA2 v4 ignoring "fmt", pure XMA1/2) */ + internal_size = 4+4+xma->chunk_size; + + memcpy(internal + 0x0, xma->xma2_version ? "XMA2" : "fmt ", 4); + put_32bit(internal + 0x4, xma->chunk_size); + memcpy(internal + 0x8, chunk, xma->chunk_size); + } + + /* create main RIFF */ + head_size = 4+4 + 4 + internal_size + 4+4; + file_size = head_size-4-4 + xma->data_size; + if (head_size > buf_size) goto fail; + + memcpy(buf + 0x0, xma->big_endian ? "RIFX" : "RIFF", 4); + put_32bit(buf + 0x4, file_size); + memcpy(buf + 0x8, "WAVE", 4); + memcpy(buf + 0xc, internal, internal_size); + memcpy(buf + head_size-4-4, "data", 4); + put_32bit(buf + head_size-4, xma->data_size); + + return head_size; + +fail: + return -1; +} + + +#if ADJUST_SAMPLE_RATE +/** + * Get real XMA sample rate (from Microsoft docs, apparently info only and not correct for playback). + */ +static int32_t get_xma_sample_rate(int32_t general_rate) { + int32_t xma_rate = 48000; /* default XMA */ + + if (general_rate <= 24000) xma_rate = 24000; + else if (general_rate <= 32000) xma_rate = 32000; + else if (general_rate <= 44100) xma_rate = 44100; + + return xma_rate; +} +#endif diff --git a/src/vgmstream.c b/src/vgmstream.c index 25bb989f..c52982bb 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -338,6 +338,7 @@ VGMSTREAM * (*init_vgmstream_fcns[])(STREAMFILE *streamFile) = { init_vgmstream_g1l, init_vgmstream_hca, init_vgmstream_ps2_svag_snk, + init_vgmstream_xma, #ifdef VGM_USE_FFMPEG init_vgmstream_mp4_aac_ffmpeg, init_vgmstream_ffmpeg, diff --git a/winamp/in_vgmstream.c b/winamp/in_vgmstream.c index e2da450a..e0c59015 100644 --- a/winamp/in_vgmstream.c +++ b/winamp/in_vgmstream.c @@ -352,6 +352,8 @@ char * extension_list[] = { "xa2\0XA2 Audio File (*.XA2)\0", "xa30\0XA30 Audio File (*.XA30)\0", "xau\0XAU Audio File (*.XAU)\0", + "xma\0XMA Audio File (*.XMA)\0", + "xma2\0XMA2 Audio File (*.XMA2)\0", "xmu\0XMU Audio File (*.XMU)\0", "xnb\0XNB Audio File (*.XNB)\0", "xsf\0XSF Audio File (*.XSF)\0", diff --git a/xmp-vgmstream/DllMain.c b/xmp-vgmstream/DllMain.c index c24c729c..c8a19dee 100644 --- a/xmp-vgmstream/DllMain.c +++ b/xmp-vgmstream/DllMain.c @@ -332,7 +332,7 @@ void __stdcall GetAdditionalFields(char* blerp) { XMPIN vgmstream_intf = { XMPIN_FLAG_CANSTREAM, "vgmstream for XMPlay", - "vgmstream files\0""2dx9/aaap/aax/acm/adp/adpcm/ads/adx/afc/agsc/ahx/aifc/aiff/aix/amts/as4/asd/asf/asr/ass/ast/aud/aus/baf/baka/bar/bcstm/bcwav/bfstm/bfwav/bfwavnsmbu/bg00/bgw/bh2pcm/bmdx/bns/bnsf/bo2/brstm/caf/capdsp/ccc/cfn/cnk/dcs/dcsw/ddsp/de2/dmsg/dsp/dvi/dxh/eam/emff/enth/fag/filp/fsb/fwav/gca/gcm/gcsw/gcw/genh/gms/gsp/hca/hgc1/his/hps/hwas/idsp/idvi/ikm/ild/int/isd/ish/ivaud/ivb/joe/kces/kcey/khv/kraw/leg/logg/lps/lsf/lwav/matx/mcg/mi4/mib/mic/mihb/mpdsp/msa/mss/msvp/mus/musc/musx/mwv/myspd/ndp/npsf/nus3bank/nwa/omu/otm/p3d/pcm/pdt/pnb/pos/psh/psw/raw/rkv/rnd/rrds/rsd/rsf/rstm/rwar/rwav/rws/rwsd/rwx/rxw/s14/sab/sad/sap/sc/scd/sd9/sdt/seg/sfl/sfs/sl3/sli/smp/smpl/snd/sng/sns/spd/sps/spsd/spt/spw/ss2/ss7/ssm/sss/ster/sth/stm/stma/str/strm/sts/stx/svag/svs/swav/swd/tec/thp/tk5/tydsp/um3/vag/vas/vgs/vig/vjdsp/voi/vpk/vs/vsf/waa/wac/wad/wam/was/wavm/wb/wii/wp2/wsd/wsi/wvs/xa/xa2/xa30/xmu/xss/xvas/xwav/xwb/ydsp/ymf/zsd/zwdsp/vgmstream/vgms", + "vgmstream files\0""2dx9/aaap/aax/acm/adp/adpcm/ads/adx/afc/agsc/ahx/aifc/aiff/aix/amts/as4/asd/asf/asr/ass/ast/aud/aus/baf/baka/bar/bcstm/bcwav/bfstm/bfwav/bfwavnsmbu/bg00/bgw/bh2pcm/bmdx/bns/bnsf/bo2/brstm/caf/capdsp/ccc/cfn/cnk/dcs/dcsw/ddsp/de2/dmsg/dsp/dvi/dxh/eam/emff/enth/fag/filp/fsb/fwav/gca/gcm/gcsw/gcw/genh/gms/gsp/hca/hgc1/his/hps/hwas/idsp/idvi/ikm/ild/int/isd/ish/ivaud/ivb/joe/kces/kcey/khv/kraw/leg/logg/lps/lsf/lwav/matx/mcg/mi4/mib/mic/mihb/mpdsp/msa/mss/msvp/mus/musc/musx/mwv/myspd/ndp/npsf/nus3bank/nwa/omu/otm/p3d/pcm/pdt/pnb/pos/psh/psw/raw/rkv/rnd/rrds/rsd/rsf/rstm/rwar/rwav/rws/rwsd/rwx/rxw/s14/sab/sad/sap/sc/scd/sd9/sdt/seg/sfl/sfs/sl3/sli/smp/smpl/snd/sng/sns/spd/sps/spsd/spt/spw/ss2/ss7/ssm/sss/ster/sth/stm/stma/str/strm/sts/stx/svag/svs/swav/swd/tec/thp/tk5/tydsp/um3/vag/vas/vgs/vig/vjdsp/voi/vpk/vs/vsf/waa/wac/wad/wam/was/wavm/wb/wii/wp2/wsd/wsi/wvs/xa/xa2/xa30/xma/xma2/xmu/xss/xvas/xwav/xwb/ydsp/ymf/zsd/zwdsp/vgmstream/vgms", XMPAbout, NULL, XMP_CheckFile,