diff --git a/Frameworks/vgmstream/vgmstream.xcodeproj/project.pbxproj b/Frameworks/vgmstream/vgmstream.xcodeproj/project.pbxproj index 26406498a..47671b387 100644 --- a/Frameworks/vgmstream/vgmstream.xcodeproj/project.pbxproj +++ b/Frameworks/vgmstream/vgmstream.xcodeproj/project.pbxproj @@ -343,6 +343,10 @@ 83709E0A1ECBC1A4005C03D3 /* waa_wac_wad_wam.c in Sources */ = {isa = PBXBuildFile; fileRef = 83709E041ECBC1A4005C03D3 /* waa_wac_wad_wam.c */; }; 83709E0D1ECBC1C3005C03D3 /* mc3_decoder.c in Sources */ = {isa = PBXBuildFile; fileRef = 83709E0B1ECBC1C3005C03D3 /* mc3_decoder.c */; }; 83709E0E1ECBC1C3005C03D3 /* psv_decoder.c in Sources */ = {isa = PBXBuildFile; fileRef = 83709E0C1ECBC1C3005C03D3 /* psv_decoder.c */; }; + 8374EE3E1F787AB600033E90 /* ffmpeg_decoder_utils_ea_xma.c in Sources */ = {isa = PBXBuildFile; fileRef = 8374EE361F787AB500033E90 /* ffmpeg_decoder_utils_ea_xma.c */; }; + 8374EE3F1F787AB600033E90 /* ffmpeg_decoder_utils_wwise_opus.c in Sources */ = {isa = PBXBuildFile; fileRef = 8374EE3B1F787AB600033E90 /* ffmpeg_decoder_utils_wwise_opus.c */; }; + 8374EE401F787AB600033E90 /* ffmpeg_decoder_utils.h in Headers */ = {isa = PBXBuildFile; fileRef = 8374EE3C1F787AB600033E90 /* ffmpeg_decoder_utils.h */; }; + 8374EE411F787AB600033E90 /* ffmpeg_decoder_utils.c in Sources */ = {isa = PBXBuildFile; fileRef = 8374EE3D1F787AB600033E90 /* ffmpeg_decoder_utils.c */; }; 838BDB641D3AF08C0022CA6F /* libavcodec.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 838BDB611D3AF08C0022CA6F /* libavcodec.a */; }; 838BDB651D3AF08C0022CA6F /* libavformat.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 838BDB621D3AF08C0022CA6F /* libavformat.a */; }; 838BDB661D3AF08C0022CA6F /* libavutil.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 838BDB631D3AF08C0022CA6F /* libavutil.a */; }; @@ -845,6 +849,10 @@ 83709E041ECBC1A4005C03D3 /* waa_wac_wad_wam.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = waa_wac_wad_wam.c; sourceTree = ""; }; 83709E0B1ECBC1C3005C03D3 /* mc3_decoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mc3_decoder.c; sourceTree = ""; }; 83709E0C1ECBC1C3005C03D3 /* psv_decoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = psv_decoder.c; sourceTree = ""; }; + 8374EE361F787AB500033E90 /* ffmpeg_decoder_utils_ea_xma.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ffmpeg_decoder_utils_ea_xma.c; sourceTree = ""; }; + 8374EE3B1F787AB600033E90 /* ffmpeg_decoder_utils_wwise_opus.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ffmpeg_decoder_utils_wwise_opus.c; sourceTree = ""; }; + 8374EE3C1F787AB600033E90 /* ffmpeg_decoder_utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ffmpeg_decoder_utils.h; sourceTree = ""; }; + 8374EE3D1F787AB600033E90 /* ffmpeg_decoder_utils.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ffmpeg_decoder_utils.c; sourceTree = ""; }; 838BDB611D3AF08C0022CA6F /* libavcodec.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libavcodec.a; path = ../../ThirdParty/ffmpeg/lib/libavcodec.a; sourceTree = ""; }; 838BDB621D3AF08C0022CA6F /* libavformat.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libavformat.a; path = ../../ThirdParty/ffmpeg/lib/libavformat.a; sourceTree = ""; }; 838BDB631D3AF08C0022CA6F /* libavutil.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libavutil.a; path = ../../ThirdParty/ffmpeg/lib/libavutil.a; sourceTree = ""; }; @@ -1042,6 +1050,10 @@ 836F6DDF18BDC2180095E648 /* coding */ = { isa = PBXGroup; children = ( + 8374EE361F787AB500033E90 /* ffmpeg_decoder_utils_ea_xma.c */, + 8374EE3B1F787AB600033E90 /* ffmpeg_decoder_utils_wwise_opus.c */, + 8374EE3D1F787AB600033E90 /* ffmpeg_decoder_utils.c */, + 8374EE3C1F787AB600033E90 /* ffmpeg_decoder_utils.h */, 83AA5D0E1F6E2F5F0020821C /* ea_xa_decoder.c */, 83AA5D151F6E2F600020821C /* ea_xas_decoder.c */, 83AA5D141F6E2F600020821C /* mpeg_custom_utils_awc.c */, @@ -1460,6 +1472,7 @@ 836F6F2718BDC2190095E648 /* g72x_state.h in Headers */, 839E21E01F2EDAF100EE54D7 /* vorbis_custom_data_fsb.h in Headers */, 836F705418BDC2190095E648 /* streamfile.h in Headers */, + 8374EE401F787AB600033E90 /* ffmpeg_decoder_utils.h in Headers */, 83A3F0761E3AD8B900D6A794 /* stack_alloc.h in Headers */, 83A3F0751E3AD8B900D6A794 /* formats.h in Headers */, 8323894B1D22419B00482226 /* clHCA.h in Headers */, @@ -1668,6 +1681,7 @@ 836F6F7018BDC2190095E648 /* apple_caff.c in Sources */, 836F700018BDC2190095E648 /* ps2_svag.c in Sources */, 836F6FB618BDC2190095E648 /* ngc_sck_dsp.c in Sources */, + 8374EE3F1F787AB600033E90 /* ffmpeg_decoder_utils_wwise_opus.c in Sources */, 836F6F2818BDC2190095E648 /* ima_decoder.c in Sources */, 836F702318BDC2190095E648 /* rsf.c in Sources */, 83299FD01E7660C7003A3242 /* bik.c in Sources */, @@ -1711,6 +1725,7 @@ 83709E0D1ECBC1C3005C03D3 /* mc3_decoder.c in Sources */, 836F6FA818BDC2190095E648 /* nds_swav.c in Sources */, 836F6F3F18BDC2190095E648 /* ast_blocked.c in Sources */, + 8374EE411F787AB600033E90 /* ffmpeg_decoder_utils.c in Sources */, 833A7A2E1ED11961003EC53E /* xau.c in Sources */, 836F6F5018BDC2190095E648 /* mxch_blocked.c in Sources */, 836F6FB518BDC2190095E648 /* ngc_pdt.c in Sources */, @@ -1868,6 +1883,7 @@ 831BA61C1EAC61A500CF89B0 /* sxd.c in Sources */, 836F6F8018BDC2190095E648 /* dsp_bdsp.c in Sources */, 836F6F9618BDC2190095E648 /* lsf.c in Sources */, + 8374EE3E1F787AB600033E90 /* ffmpeg_decoder_utils_ea_xma.c in Sources */, 836F6FC818BDC2190095E648 /* pos.c in Sources */, 8350C05A1E071990009E0A93 /* ps2_svag_snk.c in Sources */, 836F6F8918BDC2190095E648 /* gca.c in Sources */, diff --git a/Frameworks/vgmstream/vgmstream/src/coding/coding.h b/Frameworks/vgmstream/vgmstream/src/coding/coding.h index 3ec4efcaf..0c21a18d5 100644 --- a/Frameworks/vgmstream/vgmstream/src/coding/coding.h +++ b/Frameworks/vgmstream/vgmstream/src/coding/coding.h @@ -202,9 +202,8 @@ void free_at3plus(maiatrac3plus_codec_data *data); #ifdef VGM_USE_FFMPEG /* ffmpeg_decoder */ ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size); -ffmpeg_codec_data * init_ffmpeg_offset_index(STREAMFILE *streamFile, uint64_t start, uint64_t size, int stream_index); ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size); -ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size, int stream_index); +ffmpeg_codec_data * init_ffmpeg_config(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size, ffmpeg_custom_config * config); void decode_ffmpeg(VGMSTREAM *stream, sample * outbuf, int32_t samples_to_do, int channels); void reset_ffmpeg(VGMSTREAM *vgmstream); @@ -212,6 +211,10 @@ void seek_ffmpeg(VGMSTREAM *vgmstream, int32_t num_sample); void free_ffmpeg(ffmpeg_codec_data *data); void ffmpeg_set_skip_samples(ffmpeg_codec_data * data, int skip_samples); + + +size_t ffmpeg_make_opus_header(uint8_t * buf, int buf_size, int channels, int skip, int sample_rate); +size_t ffmpeg_get_eaxma_virtual_size(off_t real_offset, size_t real_size, STREAMFILE *streamFile); #endif /* coding_utils */ @@ -256,5 +259,4 @@ void xma2_parse_xma2_chunk(STREAMFILE *streamFile, off_t chunk_offset, int * cha size_t atrac3_bytes_to_samples(size_t bytes, int full_block_align); size_t atrac3plus_bytes_to_samples(size_t bytes, int full_block_align); - #endif /*_CODING_H*/ diff --git a/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder.c b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder.c index ae31a4bef..1dc445505 100644 --- a/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder.c +++ b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder.c @@ -1,4 +1,5 @@ #include "coding.h" +#include "ffmpeg_decoder_utils.h" #ifdef VGM_USE_FFMPEG @@ -181,51 +182,53 @@ fail: /* AVIO CALLBACKS */ /* ******************************************** */ -/* AVIO callback: read stream, skipping external headers if needed */ +/* AVIO callback: read stream, handling custom data */ static int ffmpeg_read(void *opaque, uint8_t *buf, int buf_size) { ffmpeg_codec_data *data = (ffmpeg_codec_data *) opaque; - uint64_t offset = data->offset; + int ret = 0; int max_to_copy = 0; - int ret; - if (data->header_insert_block) { - if (offset < data->header_size) { - max_to_copy = (int)(data->header_size - offset); + //;VGM_LOG("AVIO read: r_off=%"PRIx64", v_off=%"PRIx64", b_size=%x\n", data->real_offset, data->virtual_offset, buf_size);fflush(stdout); + + /* clamp reads */ + if (data->virtual_offset + buf_size > data->virtual_size) + buf_size = data->virtual_size - data->virtual_offset; + if (buf_size == 0) + return ret; + + /* handle reads on inserted header */ + if (data->header_size) { + if (data->virtual_offset < data->header_size) { + max_to_copy = (int)(data->header_size - data->virtual_offset); if (max_to_copy > buf_size) { max_to_copy = buf_size; } - memcpy(buf, data->header_insert_block + offset, max_to_copy); + + //;VGM_LOG("AVIO header: v_off=%lx, h_size=%lx, mtc=%x, b_size=%x\n", (off_t)data->virtual_offset, (off_t)data->header_size, max_to_copy, buf_size);fflush(stdout); + memcpy(buf, data->header_insert_block + data->virtual_offset, max_to_copy); buf += max_to_copy; buf_size -= max_to_copy; - offset += max_to_copy; - if (!buf_size) { - data->offset = offset; - return max_to_copy; + data->virtual_offset += max_to_copy; /* adjust for reads below */ + + if (buf_size == 0) { + return max_to_copy; /* offset still in header */ } } - offset -= data->header_size; } - /* when "fake" size is smaller than "real" size we need to make sure bytes_read (ret) is clamped; - * it confuses FFmpeg in rare cases (STREAMFILE may have valid data after size) */ - if (offset + buf_size > data->size + data->header_size) { - buf_size = data->size - offset; /* header "read" is manually inserted later */ + /* main read */ + switch(data->config.type) { + case FFMPEG_EA_XMA: ret = ffmpeg_custom_read_eaxma(data, buf, buf_size); break; + case FFMPEG_WWISE_OPUS: ret = ffmpeg_custom_read_wwise_opus(data, buf, buf_size); break; + //case FFMPEG_EA_SCHL: ret = ffmpeg_custom_read_ea_schl(data, buf, buf_size); break; + //case FFMPEG_SFH: ret = ffmpeg_custom_read_sfh(data, buf, buf_size); break; + default: ret = ffmpeg_custom_read_standard(data, buf, buf_size); break; } + data->virtual_offset += ret; + //data->real_offset = ; /* must be updated in function */ - ret = read_streamfile(buf, offset + data->start, buf_size, data->streamfile); - if (ret > 0) { - offset += ret; - if (data->header_insert_block) { - ret += max_to_copy; - } - } - - if (data->header_insert_block) { - offset += data->header_size; - } - - data->offset = offset; - return ret; + //;VGM_LOG("AVIO read done: ret=%x, r_off=%"PRIx64", v_off=%"PRIx64"\n", ret + max_to_copy, data->real_offset, data->virtual_offset);fflush(stdout); + return ret + max_to_copy; } /* AVIO callback: write stream not needed */ @@ -233,53 +236,92 @@ static int ffmpeg_write(void *opaque, uint8_t *buf, int buf_size) { return -1; } -/* AVIO callback: seek stream, skipping external headers if needed */ +/* AVIO callback: seek stream, handling custom data */ static int64_t ffmpeg_seek(void *opaque, int64_t offset, int whence) { ffmpeg_codec_data *data = (ffmpeg_codec_data *) opaque; int ret = 0; + /* get cache'd size */ if (whence & AVSEEK_SIZE) { - return data->size + data->header_size; + //;VGM_LOG("AVIO size: v_size=%"PRIx64", h_size=%"PRIx64", r_size=%"PRIx64", r_start=%"PRIx64"\n", data->virtual_size, data->header_size, data->real_size, data->real_start);fflush(stdout); + return data->virtual_size; } + + //;VGM_LOG("AVIO seek: off=%"PRIx64" r_off=%"PRIx64", v_off=%"PRIx64"\n", offset, data->real_offset, data->virtual_offset);fflush(stdout); + whence &= ~(AVSEEK_SIZE | AVSEEK_FORCE); - /* false offsets, on reads data->start will be added */ + + /* find the final offset FFmpeg sees (within fake header + virtual size) */ switch (whence) { - case SEEK_SET: + case SEEK_SET: /* absolute */ break; - case SEEK_CUR: - offset += data->offset; + case SEEK_CUR: /* relative to current */ + offset += data->virtual_offset; break; - case SEEK_END: - offset += data->size; - if (data->header_insert_block) - offset += data->header_size; + case SEEK_END: /* relative to file end (should be negative) */ + offset += data->virtual_size; break; } - /* clamp offset; fseek returns 0 when offset > size, too */ - if (offset > data->size + data->header_size) { - offset = data->size + data->header_size; + /* clamp offset; fseek does this too */ + if (offset > data->virtual_size) + offset = data->virtual_size; + else if (offset < 0) + offset = 0; + + /* no change */ + if (data->virtual_offset == offset) { + return ret; } - data->offset = offset; + /* seeks inside fake header */ + if (offset < data->header_size) { + data->virtual_offset = offset; + data->real_offset = data->real_start; + return ret; + } + + /* main seek */ + switch(data->config.type) { + case FFMPEG_EA_XMA: offset = ffmpeg_custom_seek_eaxma(data, offset); break; + case FFMPEG_WWISE_OPUS: offset = ffmpeg_custom_seek_wwise_opus(data, offset); break; + //case FFMPEG_EA_SCHL: offset = ffmpeg_custom_seek_ea_schl(data, offset); break; + //case FFMPEG_SFH: offset = ffmpeg_custom_seek_sfh(data, offset); break; + default: offset = ffmpeg_custom_seek_standard(data, offset); break; + } + data->virtual_offset = offset; + //data->real_offset = ; /* must be updated in function */ + + //;VGM_LOG("AVIO seek done: r_off=%"PRIx64", v_off=%"PRIx64"\n", data->real_offset, data->virtual_offset);fflush(stdout); return ret; } +/* called on init, not a callback */ //todo rename to ffmpeg_init +static int64_t ffmpeg_size(ffmpeg_codec_data * data) { + int64_t bytes; + switch(data->config.type) { + case FFMPEG_EA_XMA: bytes = ffmpeg_custom_size_eaxma(data); break; + case FFMPEG_WWISE_OPUS: bytes = ffmpeg_custom_size_wwise_opus(data); break; + //case FFMPEG_EA_SCHL: bytes = ffmpeg_custom_size_ea_schl(data); break; + //case FFMPEG_SFH: bytes = ffmpeg_custom_size_sfh(data); break; + default: bytes = ffmpeg_custom_size_standard(data); break; + } + + return bytes; +} + /* ******************************************** */ /* MAIN INIT/DECODER */ /* ******************************************** */ ffmpeg_codec_data * init_ffmpeg_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size) { - return init_ffmpeg_header_offset_index(streamFile, NULL,0, start,size, 0); -} -ffmpeg_codec_data * init_ffmpeg_offset_index(STREAMFILE *streamFile, uint64_t start, uint64_t size, int stream_index) { - return init_ffmpeg_header_offset_index(streamFile, NULL,0, start,size, stream_index); + return init_ffmpeg_config(streamFile, NULL,0, start,size, NULL); } ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size) { - return init_ffmpeg_header_offset_index(streamFile, header,header_size, start,size, 0); + return init_ffmpeg_config(streamFile, header,header_size, start,size, NULL); } @@ -292,7 +334,7 @@ ffmpeg_codec_data * init_ffmpeg_header_offset(STREAMFILE *streamFile, uint8_t * * NULL header can used given if the stream has internal data recognized by FFmpeg at offset. * Stream index can be passed to FFmpeg, if the format has multiple streams (1=first). */ -ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size, int stream_index) { +ffmpeg_codec_data * init_ffmpeg_config(STREAMFILE *streamFile, uint8_t * header, uint64_t header_size, uint64_t start, uint64_t size, ffmpeg_custom_config * config) { char filename[PATH_LIMIT]; ffmpeg_codec_data * data; int errcode, i; @@ -315,17 +357,25 @@ ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint data->streamfile = streamFile->open(streamFile, filename, STREAMFILE_DEFAULT_BUFFER_SIZE); if (!data->streamfile) goto fail; - data->start = start; - data->size = size; + if (config) { + memcpy(&data->config, config, sizeof(ffmpeg_custom_config)); + } - - /* insert fake header to trick FFmpeg into demuxing/decoding the stream */ + /* 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; } + data->real_start = start; + data->real_offset = data->real_start; + data->real_size = size; + data->virtual_offset = 0; + data->virtual_size = ffmpeg_size(data); + data->virtual_base = 0; + if (data->virtual_size == 0) goto fail; + /* setup IO, attempt to autodetect format and gather some info */ data->buffer = av_malloc(FFMPEG_DEFAULT_IO_BUFFER_SIZE); if (!data->buffer) goto fail; @@ -354,7 +404,7 @@ ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint streamCount++; /* select Nth audio stream if specified, or first one */ - if (streamIndex < 0 || (stream_index > 0 && streamCount == stream_index)) { + if (streamIndex < 0 || (data->config.stream_index > 0 && streamCount == data->config.stream_index)) { codecPar = stream->codecpar; streamIndex = i; } @@ -363,7 +413,7 @@ ffmpeg_codec_data * init_ffmpeg_header_offset_index(STREAMFILE *streamFile, uint if (i != streamIndex) stream->discard = AVDISCARD_ALL; /* disable demuxing for other streams */ } - if (streamCount < stream_index) goto fail; + if (streamCount < data->config.stream_index) goto fail; if (streamIndex < 0 || !codecPar) goto fail; data->streamIndex = streamIndex; diff --git a/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils.c b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils.c new file mode 100644 index 000000000..2d674d87b --- /dev/null +++ b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils.c @@ -0,0 +1,28 @@ +#include "coding.h" +#include "ffmpeg_decoder_utils.h" + +#ifdef VGM_USE_FFMPEG + +/** + * Standard read mode: virtual values are 1:1 but inside a portion of the streamfile (between real_start and real_size). + */ + + +int ffmpeg_custom_read_standard(ffmpeg_codec_data *data, uint8_t *buf, int buf_size) { + size_t bytes = read_streamfile(buf, data->real_offset, buf_size, data->streamfile); + data->real_offset += bytes; + + return bytes; +} + +int64_t ffmpeg_custom_seek_standard(ffmpeg_codec_data *data, int64_t virtual_offset) { + data->real_offset = data->real_start + (virtual_offset - data->header_size); + return virtual_offset; +} + +int64_t ffmpeg_custom_size_standard(ffmpeg_codec_data *data) { + return data->real_size + data->header_size; +} + + +#endif diff --git a/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils.h b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils.h new file mode 100644 index 000000000..18957e3c3 --- /dev/null +++ b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils.h @@ -0,0 +1,44 @@ +#ifndef _FFMPEG_DECODER_UTILS_ +#define _FFMPEG_DECODER_UTILS_ + +#ifdef VGM_USE_FFMPEG +/* used by ffmpeg_decoder.c, but scattered in other .c files */ + +/** + * Custom read/seek for data transformation. Must handle seeks+reads from virtual offsets, ie.- + * reads "real/file" data, not decodable by FFmpeg, and transforms to decodable "virtual/buffer" data, + * block by block (must seek to closest file offset and adjust on reads). + * + * To simplify, functions won't be called in common cases (seek over filesize, no change in offset, etc), + * and fake header seeks/reads are handled externally. Real offset must be updated internally though. + * + * example (a 0x100 block transforms to a 0x150 block): + * - seek 0: file-offset=0, virtual-offset=0 + * - read 0x150: file-read=0x100 transforms to buffer=0x150 + * - new file-offset=0x100, virtual-offset=0x150 + * - seek 0x310: file-offset=0x200, virtual-offset=0x310 (closest virtual block is 0x150+0x150, + 0x10 adjusted on reads) + */ + +int ffmpeg_custom_read_standard(ffmpeg_codec_data *data, uint8_t *buf, int buf_size); +int64_t ffmpeg_custom_seek_standard(ffmpeg_codec_data *data, int64_t virtual_offset); +int64_t ffmpeg_custom_size_standard(ffmpeg_codec_data *data); + +int ffmpeg_custom_read_eaxma(ffmpeg_codec_data *data, uint8_t *buf, int buf_size); +int64_t ffmpeg_custom_seek_eaxma(ffmpeg_codec_data *data, int64_t virtual_offset); +int64_t ffmpeg_custom_size_eaxma(ffmpeg_codec_data *data); + +int ffmpeg_custom_read_wwise_opus(ffmpeg_codec_data *data, uint8_t *buf, int buf_size); +int64_t ffmpeg_custom_seek_wwise_opus(ffmpeg_codec_data *data, int64_t virtual_offset); +int64_t ffmpeg_custom_size_wwise_opus(ffmpeg_codec_data *data); + +//int ffmpeg_custom_read_ea_schl(ffmpeg_codec_data *data, uint8_t *buf, int buf_size); +//int64_t ffmpeg_custom_seek_ea_schl(ffmpeg_codec_data *data, int64_t virtual_offset); +//int64_t ffmpeg_custom_size_ea_schl(ffmpeg_codec_data *data); + +//int ffmpeg_custom_read_sfh(ffmpeg_codec_data *data, uint8_t *buf, int buf_size); +//int64_t ffmpeg_custom_seek_sfh(ffmpeg_codec_data *data, int64_t virtual_offset); +//int64_t ffmpeg_custom_size_sfh(ffmpeg_codec_data *data); + +#endif + +#endif/*_FFMPEG_DECODER_UTILS_*/ diff --git a/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils_ea_xma.c b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils_ea_xma.c new file mode 100644 index 000000000..84e69fd73 --- /dev/null +++ b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils_ea_xma.c @@ -0,0 +1,153 @@ +#include "coding.h" +#include "ffmpeg_decoder_utils.h" + +#ifdef VGM_USE_FFMPEG + +#define EAXMA_XMA_BLOCK_SIZE 0x800 + +/** + * EA-XMA is XMA with padding removed (so a real 0x450 block would be padded to a virtual 0x800 block). + * //todo missing multichannel (packet multistream) support, unknown layout + */ + + +int ffmpeg_custom_read_eaxma(ffmpeg_codec_data *data, uint8_t *buf, int buf_size) { + uint8_t v_buf[0x8000]; /* intermediate buffer, could be simplified */ + int buf_done = 0; + uint64_t real_offset = data->real_offset; + uint64_t virtual_offset = data->virtual_offset - data->header_size; + uint64_t virtual_base = data->virtual_base; + + + /* read and transform SNS/EA-XMA block into XMA block by adding padding */ + while (buf_done < buf_size) { + int bytes_to_copy; + size_t data_size, extra_size = 0, gap_size = 0; + size_t block_size = read_32bitBE(real_offset, data->streamfile); + /* 0x04(4): some kind of size? 0x08(4): decoded samples */ + + /* setup */ + data_size = (block_size & 0x00FFFFFF) - 0x0c; //todo last block size may be slightly off? + if (data_size % EAXMA_XMA_BLOCK_SIZE) /* aligned padding */ + extra_size = EAXMA_XMA_BLOCK_SIZE - (data_size % EAXMA_XMA_BLOCK_SIZE); + if (buf_done == 0) /* first read */ + gap_size = virtual_offset - virtual_base; /* might start a few bytes into the block */ + + if (data_size + extra_size > 0x8000) { + VGM_LOG("EA-XMA: total size bigger than buffer at %lx\n", (off_t)real_offset); + return 0; + } + + bytes_to_copy = data_size + extra_size - gap_size; + if (bytes_to_copy > buf_size - buf_done) + bytes_to_copy = buf_size - buf_done; + + /* transform */ + read_streamfile(v_buf, real_offset + 0x0c, data_size, data->streamfile); + memset(v_buf + data_size, 0xFF, extra_size); /* padding can be any value, typically 0xFF */ + memcpy(buf + buf_done, v_buf + gap_size, bytes_to_copy); + + /* move when block is fully done */ + if (data_size + extra_size == bytes_to_copy + gap_size) { + real_offset += (block_size & 0x00FFFFFF); + virtual_base += data_size + extra_size; + } + + buf_done += bytes_to_copy; + + /* exit on last block just in case, though should reach file size */ + if (block_size & 0x80000000) + break; + } + + + data->real_offset = real_offset; + data->virtual_base = virtual_base; + return buf_size; +} + +int64_t ffmpeg_custom_seek_eaxma(ffmpeg_codec_data *data, int64_t virtual_offset) { + int64_t real_offset, virtual_base; + int64_t current_virtual_offset = data->virtual_offset; + + /* Find SNS block start closest to offset. ie. virtual_offset 0x1A10 could mean SNS blocks + * of 0x456+0x820 padded to 0x800+0x1000 (base) + 0x210 (extra for reads), thus real_offset = 0xC76 */ + + if (virtual_offset > current_virtual_offset) { /* seek after current: start from current block */ + real_offset = data->real_offset; + virtual_base = data->virtual_base; + } + else { /* seek before current: start from the beginning */ + real_offset = data->real_start; + virtual_base = 0; + } + + + /* find target block */ + while (virtual_base < virtual_offset) { + size_t data_size, extra_size = 0; + size_t block_size = read_32bitBE(real_offset, data->streamfile); + + data_size = (block_size & 0x00FFFFFF) - 0x0c; + if (data_size % EAXMA_XMA_BLOCK_SIZE) + extra_size = EAXMA_XMA_BLOCK_SIZE - (data_size % EAXMA_XMA_BLOCK_SIZE); + + /* stop if virtual_offset lands inside current block */ + if (data_size + extra_size > virtual_offset) + break; + + real_offset += (block_size & 0x00FFFFFF); + virtual_base += data_size + extra_size; + } + + /* closest we can use for reads */ + data->real_offset = real_offset; + data->virtual_base = virtual_base; + + return virtual_offset; +} + +int64_t ffmpeg_custom_size_eaxma(ffmpeg_codec_data *data) { + + uint64_t virtual_size = data->config.virtual_size; + if (!virtual_size) + return 0; + + return virtual_size + data->header_size; +} + +/* needed to know in meta for fake RIFF */ +size_t ffmpeg_get_eaxma_virtual_size(off_t real_offset, size_t real_size, STREAMFILE *streamFile) { + size_t virtual_size = 0; + + /* count all SNS/EAXMA blocks size + padding size */ + while (real_offset < real_size) { + size_t data_size; + size_t block_size = read_32bitBE(real_offset, streamFile); + + data_size = (block_size & 0x00FFFFFF) - 0x0c; + + if ((block_size & 0xFF000000) && !(block_size & 0x80000000)) { + VGM_LOG("EA-XMA: unknown flag found at %lx\n", (off_t)real_offset); + goto fail; + } + + real_offset += (block_size & 0x00FFFFFF); + + virtual_size += data_size; + if (data_size % EAXMA_XMA_BLOCK_SIZE) /* XMA block padding */ + virtual_size += EAXMA_XMA_BLOCK_SIZE - (data_size % EAXMA_XMA_BLOCK_SIZE); + + /* exit on last block just in case, though should reach real_size */ + if (block_size & 0x80000000) + break; + } + + return virtual_size; + +fail: + return 0; + +} + +#endif diff --git a/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils_wwise_opus.c b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils_wwise_opus.c new file mode 100644 index 000000000..377701328 --- /dev/null +++ b/Frameworks/vgmstream/vgmstream/src/coding/ffmpeg_decoder_utils_wwise_opus.c @@ -0,0 +1,346 @@ +#include "coding.h" +#include "ffmpeg_decoder_utils.h" +#include + +#ifdef VGM_USE_FFMPEG + + +/** + * Xiph Opus without Ogg layer and custom packet headers. This creates valid Ogg pages with single Opus packets. + * Wwise opus looks encoded roughly like "opusenc --hard-cbr --framesize 40". The packets are Opus-compliant. + * + * Info, CRC and stuff: + * https://www.opus-codec.org/docs/ + * https://tools.ietf.org/html/rfc7845.html + * https://github.com/hcs64/ww2ogg + */ + +static size_t make_oggs_page(uint8_t * buf, int buf_size, size_t data_size, int page_sequence, int granule); +static size_t make_opus_header(uint8_t * buf, int buf_size, int channels, int skip, int sample_rate); +static size_t make_opus_comment(uint8_t * buf, int buf_size); +static uint32_t get_opus_samples_per_frame(const uint8_t * data, int Fs); + + +size_t ffmpeg_make_opus_header(uint8_t * buf, int buf_size, int channels, int skip, int sample_rate) { + int buf_done = 0; + size_t bytes; + + if (buf_size < 0x100) /* approx */ + goto fail; + + /* make header */ + bytes = make_opus_header(buf+buf_done + 0x1c,buf_size, channels, skip, sample_rate); + make_oggs_page(buf+buf_done + 0x00,buf_size, bytes, 0, 0); + buf_done += 0x1c + bytes; + + /* make comment */ + bytes = make_opus_comment(buf+buf_done + 0x1c,buf_size); + make_oggs_page(buf+buf_done + 0x00,buf_size, bytes, 1, 0); + buf_done += 0x1c + bytes; + + return buf_done; +fail: + return 0; +} + + +int ffmpeg_custom_read_wwise_opus(ffmpeg_codec_data *data, uint8_t *buf, int buf_size) { + uint8_t v_buf[0x8000]; /* intermediate buffer, could be simplified */ + int buf_done = 0; + uint64_t real_offset = data->real_offset; + uint64_t virtual_offset = data->virtual_offset - data->header_size; + uint64_t virtual_base = data->virtual_base; + + + if (data->config.sequence == 0) + data->config.sequence = 2; + + /* read and transform Wwise Opus block into Ogg Opus block by making Ogg pages */ + while (buf_done < buf_size) { + int bytes_to_copy, samples_per_frame; + size_t extra_size = 0, gap_size = 0; + size_t data_size = read_32bitBE(real_offset, data->streamfile); + /* 0x00: data size, 0x04: ?, 0x08+: data */ + + /* setup */ + extra_size = 0x1b + (int)(data_size / 0xFF + 1); /* OggS page: base size + lacing values */ + if (buf_done == 0) /* first read */ + gap_size = virtual_offset - virtual_base; /* might start a few bytes into the block */ + + if (data_size + extra_size > 0x8000) { + VGM_LOG("WW OPUS: total size bigger than buffer at %lx\n", (off_t)real_offset); + return 0; + } + + bytes_to_copy = data_size + extra_size - gap_size; + if (bytes_to_copy > buf_size - buf_done) + bytes_to_copy = buf_size - buf_done; + + /* transform */ + read_streamfile(v_buf + extra_size, real_offset + 0x08, data_size, data->streamfile); + samples_per_frame = get_opus_samples_per_frame(v_buf + extra_size, 48000); /* fixed? */ + make_oggs_page(v_buf,0x8000, data_size, data->config.sequence, data->config.samples_done + samples_per_frame); + memcpy(buf + buf_done, v_buf + gap_size, bytes_to_copy); + + /* move when block is fully done */ + if (data_size + extra_size == bytes_to_copy + gap_size) { + real_offset += 0x04 + 0x04 + data_size; + virtual_base += data_size + extra_size; + data->config.sequence++; + data->config.samples_done += samples_per_frame; + } + + buf_done += bytes_to_copy; + } + + data->real_offset = real_offset; + data->virtual_base = virtual_base; + return buf_size; +} + +int64_t ffmpeg_custom_seek_wwise_opus(ffmpeg_codec_data *data, int64_t virtual_offset) { + int64_t real_offset, virtual_base; + int64_t current_virtual_offset = data->virtual_offset; + + /* find Wwise block start closest to offset; a 0x1E8 block expands to 0x1D + 0x1E0 (oggs + data) */ + + if (virtual_offset > current_virtual_offset) { /* seek after current: start from current block */ + real_offset = data->real_offset; + virtual_base = data->virtual_base; + } + else { /* seek before current: start from the beginning */ + real_offset = data->real_start; + virtual_base = 0; + data->config.sequence = 0; + data->config.samples_done = 0; + } + + + /* find target block */ + while (virtual_base < virtual_offset) { + size_t extra_size; + size_t data_size = read_32bitBE(real_offset, data->streamfile); + + extra_size = 0x1b + (int)(data_size / 0xFF + 1); /* OggS page: base size + lacing values */ + + /* stop if virtual_offset lands inside current block */ + if (data_size + extra_size > virtual_offset) + break; + + real_offset += 0x04 + 0x04 + data_size; + virtual_base += data_size + extra_size; + } + + /* closest we can use for reads */ + data->real_offset = real_offset; + data->virtual_base = virtual_base; + + return virtual_offset; +} + +int64_t ffmpeg_custom_size_wwise_opus(ffmpeg_codec_data *data) { + uint64_t real_offset = data->real_start; + uint64_t real_size = data->real_size; + uint64_t virtual_size = data->header_size; + + /* count all Wwise Opus blocks size + OggS page size */ + while (real_offset < real_size) { + size_t extra_size; + size_t data_size = read_32bitBE(real_offset, data->streamfile); + /* 0x00: data size, 0x04: ? (not a sequence or CRC), 0x08+: data */ + + extra_size = 0x1b + (int)(data_size / 0xFF + 1); /* OggS page: base size + lacing values */ + + real_offset += 0x04 + 0x04 + data_size; + virtual_size += extra_size + data_size; + } + + + return virtual_size; +} + + + +/* ************************************************** */ + +/* from ww2ogg - from Tremor (lowmem) */ +static uint32_t crc_lookup[256]={ + 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9, 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005, + 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61, 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd, + 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9, 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75, + 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011, 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd, + 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039, 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5, + 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81, 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d, + 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49, 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95, + 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1, 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d, + 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae, 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072, + 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16, 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca, + 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde, 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02, + 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066, 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba, + 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e, 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692, + 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6, 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a, + 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e, 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2, + 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686, 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a, + 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637, 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb, + 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f, 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53, + 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47, 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b, + 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff, 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623, + 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7, 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b, + 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f, 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3, + 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7, 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b, + 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f, 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3, + 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640, 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c, + 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8, 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24, + 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30, 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec, + 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088, 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654, + 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0, 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c, + 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18, 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4, + 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0, 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c, + 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668, 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4 +}; + +/* from ww2ogg */ +static uint32_t get_oggs_checksum(uint8_t * data, int bytes) { + uint32_t crc_reg=0; + int i; + + for(i=0;i> 24)&0xff)^data[i]]; + + return crc_reg; +} + +/* from opus_decoder.c */ +static uint32_t get_opus_samples_per_frame(const uint8_t * data, int Fs) { + int audiosize; + if (data[0]&0x80) + { + audiosize = ((data[0]>>3)&0x3); + audiosize = (Fs<>3)&0x3); + if (audiosize == 3) + audiosize = Fs*60/1000; + else + audiosize = (Fs< buf_size) { + VGM_LOG("WW OPUS: buffer can't hold OggS page\n"); + goto fail; + } + + segment_count = (int)(data_size / 0xFF + 1); + + put_32bitBE(buf+0x00, 0x4F676753); /* capture pattern ("OggS") */ + put_8bit (buf+0x04, 0); /* stream structure version, fixed */ + put_8bit (buf+0x05, header_type_flag); /* bitflags (0: normal, continued = 1, first = 2, last = 4) */ + put_32bitLE(buf+0x06, (uint32_t)(absolute_granule >> 0 & 0xFFFFFFFF)); /* lower */ + put_32bitLE(buf+0x0A, (uint32_t)(absolute_granule >> 32 & 0xFFFFFFFF)); /* upper */ + put_32bitLE(buf+0x0E, stream_serial_number); /* for interleaved multi-streams */ + put_32bitLE(buf+0x12, page_sequence); + put_32bitLE(buf+0x16, checksum); /* 0 for now, until all data is written */ + put_8bit (buf+0x1A, segment_count); /* count of all lacing values */ + + /* segment table: size N in "lacing values" (ex. 0x20E=0xFF+FF+10; 0xFF=0xFF+00) */ + page_done = 0x1B; + while (lacing_done < data_size) { + int bytes = data_size - lacing_done; + if (bytes > 0xFF) + bytes = 0xFF; + + put_8bit(buf+page_done, bytes); + page_done++; + lacing_done += bytes; + + if (lacing_done == data_size && bytes == 0xFF) { + put_8bit(buf+page_done, 0x00); + page_done++; + } + } + + /* data */ + //memcpy(buf+page_done, data_buf, data_size); /* data must be copied before this call */ + page_done += data_size; + + /* final checksum */ + checksum = get_oggs_checksum(buf, page_done); + put_32bitLE(buf+0x16, checksum); + + return page_done; + +fail: + return 0; +} + +static size_t make_opus_header(uint8_t * buf, int buf_size, int channels, int skip, int sample_rate) { + size_t header_size = 0x13; + int output_gain = 0; + int channel_papping_family = 0; + + if (header_size > buf_size) { + VGM_LOG("WW OPUS: buffer can't hold header\n"); + goto fail; + } + + put_32bitBE(buf+0x00, 0x4F707573); /* header magic ("Opus") */ + put_32bitBE(buf+0x04, 0x48656164); /* header magic ("Head") */ + put_8bit (buf+0x08, 1); /* version, fixed */ + put_8bit (buf+0x09, channels); + put_16bitLE(buf+0x0A, skip); + put_32bitLE(buf+0x0c, sample_rate); + put_16bitLE(buf+0x10, output_gain); + put_8bit (buf+0x12, channel_papping_family); + + + return header_size; +fail: + return 0; +} + +static size_t make_opus_comment(uint8_t * buf, int buf_size) { + size_t comment_size; + int vendor_string_length, user_comment_0_length; + char * vendor_string = "libopus 1.0.2"; + char * user_comment_0_string = "ENCODER=opusenc from opus-tools 0.1.6"; + vendor_string_length = strlen(vendor_string); + user_comment_0_length = strlen(user_comment_0_string); + + comment_size = 0x14 + vendor_string_length + user_comment_0_length; + + if (comment_size > buf_size) { + VGM_LOG("WW OPUS: buffer can't hold comment\n"); + goto fail; + } + + put_32bitBE(buf+0x00, 0x4F707573); /* header magic ("Opus") */ + put_32bitBE(buf+0x04, 0x54616773); /* header magic ("Tags") */ + put_32bitLE(buf+0x08, vendor_string_length); + memcpy(buf+0x0c, vendor_string, vendor_string_length); + put_32bitLE(buf+0x0c + vendor_string_length+0x00, 1); /* user_comment_list_length */ + put_32bitLE(buf+0x0c + vendor_string_length+0x04, user_comment_0_length); + memcpy(buf+0x0c + vendor_string_length+0x08, user_comment_0_string, user_comment_0_length); + + + return comment_size; +fail: + return 0; +} + + +#endif diff --git a/Frameworks/vgmstream/vgmstream/src/coding/pcm_decoder.c b/Frameworks/vgmstream/vgmstream/src/coding/pcm_decoder.c index ea350ae3c..47c69cd09 100644 --- a/Frameworks/vgmstream/vgmstream/src/coding/pcm_decoder.c +++ b/Frameworks/vgmstream/vgmstream/src/coding/pcm_decoder.c @@ -1,5 +1,6 @@ #include "coding.h" #include "../util.h" +#include void decode_pcm16LE(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) { int i; diff --git a/Frameworks/vgmstream/vgmstream/src/formats.c b/Frameworks/vgmstream/vgmstream/src/formats.c index 7764ab401..658bc7b53 100644 --- a/Frameworks/vgmstream/vgmstream/src/formats.c +++ b/Frameworks/vgmstream/vgmstream/src/formats.c @@ -33,6 +33,7 @@ static const char* extension_list[] = { "aix", "akb", "amts", //fake extension (to be removed) + "ao", //txth/reserved [Cloudphobia (PC)] "as4", "asd", "asf", @@ -85,6 +86,7 @@ static const char* extension_list[] = { "cps", "cxs", + "da", "dbm", "dcs", "ddsp", @@ -135,6 +137,7 @@ static const char* extension_list[] = { "int", "isd", "isws", + "itl", //txth/reserved [Charinko Hero (GC)] "ivaud", "ivag", "ivb", @@ -660,7 +663,7 @@ static const meta_info meta_info_list[] = { {meta_RIFF_WAVE_smpl, "RIFF WAVE header with sample looping info"}, {meta_RIFX_WAVE, "RIFX WAVE header"}, {meta_RIFX_WAVE_smpl, "RIFX WAVE header with sample looping info"}, - {meta_XNBm, "XNBm header"}, + {meta_XNB, "Microsoft XNA Game Studio 4.0 header"}, {meta_PCM_SCD, "PCM file with custom header (SCD)"}, {meta_PCM_PS2, "PCM file with custom header (PS2)"}, {meta_PS2_RKV, "Legacy of Kain - Blood Omen 2 RKV Header"}, diff --git a/Frameworks/vgmstream/vgmstream/src/meta/bik.c b/Frameworks/vgmstream/vgmstream/src/meta/bik.c index 20ca20ff9..f703b2a6a 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/bik.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/bik.c @@ -34,8 +34,11 @@ VGMSTREAM * init_vgmstream_bik(STREAMFILE *streamFile) { #ifdef VGM_USE_FFMPEG { - /* init_FFmpeg uses streamFile->stream_index internally, if specified */ - vgmstream->codec_data = init_ffmpeg_offset_index(streamFile, 0x0, get_streamfile_size(streamFile), stream_index); + ffmpeg_custom_config cfg; + memset(&cfg, 0, sizeof(ffmpeg_custom_config)); + cfg.stream_index = stream_index; + + vgmstream->codec_data = init_ffmpeg_config(streamFile, NULL,0, 0x0,get_streamfile_size(streamFile), &cfg); if (!vgmstream->codec_data) goto fail; vgmstream->coding_type = coding_FFmpeg; } diff --git a/Frameworks/vgmstream/vgmstream/src/meta/ea_snu.c b/Frameworks/vgmstream/vgmstream/src/meta/ea_snu.c index 722533333..c21a72523 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/ea_snu.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/ea_snu.c @@ -1,27 +1,38 @@ #include "meta.h" #include "../layout/layout.h" +#include "../coding/coding.h" -/* .SNU - EA new-ish header (Dead Space, The Godfather 2) */ + +/* .SNU - from EA Redwood Shores/Visceral games (Dead Space, Dante's Inferno, The Godfather 2) */ VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; int channel_count, loop_flag = 0, channel_config, codec, sample_rate, flags; uint32_t num_samples, loop_start = 0, loop_end = 0; off_t start_offset; + int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; /* check extension, case insensitive */ if (!check_extensions(streamFile,"snu")) goto fail; - /* check header */ - //if ((read_32bitBE(0x00,streamFile) & 0x00FFFF00 != 0x00000000) && (read_32bitBE(0x0c,streamFile) != 0x00000000)) - // goto fail; - /* 0x00: related to sample rate?, 0x02: always 0?, 0x03: related to channels? (usually match but may be 0) */ - /* 0x04: some size, maybe >>2 ~= number of 0x4c frames (BE/LE depending on platform) */ - /* 0x08: always 0x20? (also BE/LE), 0x0c: always 0? */ + /* check header (the first 0x10 are BE/LE depending on platform) */ + /* 0x00(1): related to sample rate? (03=48000) + * 0x01(1): flags? (when set seems to be a bank and has extra data before start_offset) //todo + * 0x02(1): always 0? + * 0x03(1): channels? (usually matches but rarely may be 0) + * 0x04(4): some size, maybe >>2 ~= number of frames + * 0x08(4): start offset + * 0x0c(4): some sub-offset? (0x20, found when 0x01 is set) */ + /* use start offset as endianness flag */ + if ((uint32_t)read_32bitLE(0x08,streamFile) > 0x00F00000) { + read_32bit = read_32bitBE; + } else { + read_32bit = read_32bitLE; + } - start_offset = 0x20; /* first block */ + start_offset = read_32bit(0x08,streamFile); codec = read_8bit(0x10,streamFile); channel_config = read_8bit(0x11,streamFile); @@ -36,7 +47,7 @@ VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE *streamFile) { } #if 0 - //todo not working ok with blocks + //todo not working ok with blocks in XAS if (flags & 0x60) { /* full loop, seen in ambient tracks */ loop_flag = 1; loop_start = 0; @@ -45,7 +56,7 @@ VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE *streamFile) { #endif //channel_count = (channel_config >> 2) + 1; //todo test - /* 01/02/03 = 1 ch?, 05/06/07 = 2/3 ch?, 0d/0e/0f = 4/5 ch?, 14/15/16/17 = 6/7 ch?, 1d/1e/1f = 8 ch? */ + /* 01/02/03 = 1 ch?, 05/06/07 = 2/3 ch?, 0d/0e/0f = 4/5 ch?, 15/16/17 = 6/7 ch?, 1d/1e/1f = 8 ch? */ switch(channel_config) { case 0x00: channel_count = 1; break; case 0x04: channel_count = 2; break; @@ -68,20 +79,64 @@ VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE *streamFile) { vgmstream->loop_end_sample = loop_end; vgmstream->meta_type = meta_EA_SNU; - vgmstream->layout_type = layout_ea_sns_blocked; switch(codec) { - case 0x04: /* "Xas1": EA-XAS (Dead Space) */ + case 0x04: /* "Xas1": EA-XAS (Dead Space) */ vgmstream->coding_type = coding_EA_XAS; + vgmstream->layout_type = layout_ea_sns_blocked; break; +#if 0 +#ifdef VGM_USE_MPEG + case 0x07: { /* "EL32S": EALayer3 v2 "S" (Dante's Inferno PS3) */ + mpeg_custom_config cfg; + off_t mpeg_start_offset = start_offset + 0x08; + + memset(&cfg, 0, sizeof(mpeg_custom_config)); + + /* layout is still blocks, but should work fine with the custom mpeg decoder */ + vgmstream->codec_data = init_mpeg_custom_codec_data(streamFile, mpeg_start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_EAL32S, &cfg); + if (!vgmstream->codec_data) goto fail; + + vgmstream->layout_type = layout_ea_sns_blocked; + break; + } +#endif +#endif + +#ifdef VGM_USE_FFMPEG + case 0x03: { /* "EXm0": EA-XMA (Dante's Inferno X360) */ + uint8_t buf[0x100]; + int bytes, block_size, block_count; + size_t stream_size, virtual_size; + ffmpeg_custom_config cfg; + + stream_size = get_streamfile_size(streamFile) - start_offset; + virtual_size = ffmpeg_get_eaxma_virtual_size(start_offset,stream_size, streamFile); + block_size = 0x8000; /* ? */ + block_count = stream_size / block_size + (stream_size % block_size ? 1 : 0); + + bytes = ffmpeg_make_riff_xma2(buf, 0x100, vgmstream->num_samples, virtual_size, vgmstream->channels, vgmstream->sample_rate, block_count, block_size); + if (bytes <= 0) goto fail; + + memset(&cfg, 0, sizeof(ffmpeg_custom_config)); + cfg.type = FFMPEG_EA_XMA; + cfg.virtual_size = virtual_size; + + vgmstream->codec_data = init_ffmpeg_config(streamFile, buf,bytes, start_offset,stream_size, &cfg); + if (!vgmstream->codec_data) goto fail; + + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + break; + } +#endif case 0x00: /* "NONE" */ case 0x01: /* not used? */ case 0x02: /* "P6B0": PCM16BE */ - case 0x03: /* "EXm0": EA-XMA */ + case 0x05: /* "EL31": EALayer3 v1 b (with PCM blocks in normal EA-frames?) */ case 0x06: /* "EL32P": EALayer3 v2 "P" */ - case 0x07: /* "EL32S": EALayer3 v2 "S" */ case 0x09: /* EASpeex? */ case 0x0c: /* EAOpus? */ case 0x0e: /* XAS variant? */ @@ -97,7 +152,8 @@ VGMSTREAM * init_vgmstream_ea_snu(STREAMFILE *streamFile) { if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; - ea_sns_block_update(start_offset, vgmstream); + if (vgmstream->layout_type == layout_ea_sns_blocked) + ea_sns_block_update(start_offset, vgmstream); return vgmstream; diff --git a/Frameworks/vgmstream/vgmstream/src/meta/hca_keys.h b/Frameworks/vgmstream/vgmstream/src/meta/hca_keys.h index e3012873e..3d1cba9b4 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/hca_keys.h +++ b/Frameworks/vgmstream/vgmstream/src/meta/hca_keys.h @@ -100,6 +100,15 @@ static const hcakey_info hcakey_list[] = { // Yuyuyui (iOS/Android) *unconfirmed {4867249871962584729}, // 438BF1F883653699 + // Tekken Mobile (iOS/Android) + {0xFFFFFFFFFFFFFFFF}, // + + // Tales of the Rays (iOS/Android) + {9516284}, // 00000000009134FC + + // Skylock - Kamigami to Unmei no Itsutsuko [iOS] + {49160768297}, // 0000000B7235CB29 + }; diff --git a/Frameworks/vgmstream/vgmstream/src/meta/meta.h b/Frameworks/vgmstream/vgmstream/src/meta/meta.h index f920bb111..fbbb0699c 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/meta.h +++ b/Frameworks/vgmstream/vgmstream/src/meta/meta.h @@ -144,9 +144,7 @@ VGMSTREAM * init_vgmstream_str_snds(STREAMFILE * streamFile); VGMSTREAM * init_vgmstream_ws_aud(STREAMFILE * streamFile); -#ifdef VGM_USE_MPEG VGMSTREAM * init_vgmstream_ahx(STREAMFILE * streamFile); -#endif VGMSTREAM * init_vgmstream_ivb(STREAMFILE * streamFile); @@ -630,9 +628,7 @@ VGMSTREAM * init_vgmstream_ps2_svag_snk(STREAMFILE* streamFile); VGMSTREAM * init_vgmstream_xma(STREAMFILE* streamFile); -#ifdef VGM_USE_FFMPEG VGMSTREAM * init_vgmstream_bik(STREAMFILE* streamFile); -#endif VGMSTREAM * init_vgmstream_ps2_vds_vdm(STREAMFILE* streamFile); diff --git a/Frameworks/vgmstream/vgmstream/src/meta/riff.c b/Frameworks/vgmstream/vgmstream/src/meta/riff.c index e0ac857cc..e3933e4f9 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/riff.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/riff.c @@ -2,6 +2,7 @@ #include "../coding/coding.h" #include "../layout/layout.h" #include "../util.h" +#include /* Resource Interchange File Format */ @@ -262,7 +263,8 @@ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { /* check extension, case insensitive */ streamFile->get_name(streamFile,filename,sizeof(filename)); if (strcasecmp("wav",filename_extension(filename)) && - strcasecmp("lwav",filename_extension(filename)) + strcasecmp("lwav",filename_extension(filename)) && + strcasecmp("da",filename_extension(filename)) /* SD Gundam - Over Galaxian, The Great Battle VI (PS) */ #ifndef VGM_USE_FFMPEG && strcasecmp("sgb",filename_extension(filename)) /* SGB has proper support with FFmpeg in sgxd */ #endif @@ -835,79 +837,66 @@ fail: return NULL; } -/* XNBm (Windows 7 Phone) */ +//todo move +/* XNB - Microsoft XNA Game Studio 4.0 format */ VGMSTREAM * init_vgmstream_xnbm(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; - char filename[PATH_LIMIT]; + off_t start_offset; + int loop_flag = 0, version, flags, num_samples = 0; + size_t xnb_size, data_size; struct riff_fmt_chunk fmt; - off_t file_size = -1; - int sample_count = 0; - off_t start_offset = -1; - - int loop_flag = 0; -#if 0 - long loop_start_ms = -1; - long loop_end_ms = -1; - off_t loop_start_offset = -1; - off_t loop_end_offset = -1; -#endif - - uint32_t xnbm_size; - uint32_t data_size = 0; /* check extension, case insensitive */ - streamFile->get_name(streamFile,filename,sizeof(filename)); - if (strcasecmp("xnb",filename_extension(filename))) - { + if ( !check_extensions(streamFile,"xnb")) goto fail; - } /* check header */ - if ((uint32_t)read_32bitBE(0,streamFile)!=0x584E426d) /* "XNBm" */ - goto fail; - /* check version? */ - if ((uint32_t)read_16bitLE(4,streamFile)!=5) + if ((read_32bitBE(0,streamFile) & 0xFFFFFF00) != 0x584E4200) /* "XNB" */ goto fail; + /* 0x04: platform: ‘w’ = Microsoft Windows, ‘m’ = Windows Phone 7, ‘x’ = Xbox 360, 'a' = Android */ - xnbm_size = read_32bitLE(6,streamFile); - file_size = get_streamfile_size(streamFile); + version = read_8bit(0x04,streamFile); + if (version != 5) goto fail; /* XNA 4.0 only */ - /* check for tructated XNBm */ - if (file_size < xnbm_size) goto fail; + flags = read_8bit(0x05,streamFile); + if (flags & 0x80) goto fail; /* compressed with XMemCompress, not public */ + //if (flags & 0x01) goto fail; /* XMA flag? */ - /* read through chunks to verify format and find metadata */ + /* "check for truncated XNB" (???) */ + xnb_size = read_32bitLE(0x06,streamFile); + if (get_streamfile_size(streamFile) < xnb_size) goto fail; + + /* XNB contains "type reader" class references to parse "shared resource" data (can be any implemented filetype) */ { - off_t current_chunk = 0xa; /* start with first chunk */ - int id_string_len; + char reader_name[255+1]; + off_t current_chunk = 0xa; + int reader_string_len; uint32_t fmt_chunk_size; + const char * type_sound = "Microsoft.Xna.Framework.Content.SoundEffectReader"; /* partial "fmt" chunk or XMA */ + //const char * type_song = "Microsoft.Xna.Framework.Content.SongReader"; /* just references a companion .wma */ - /* flag? count of strings? */ - if (read_8bit(current_chunk ++, streamFile) != 1) - goto fail; - - /* string length */ - id_string_len = read_8bit(current_chunk ++, streamFile); - - /* skip string */ - /* may want to check this ID "Microsoft.Xna.Framework.Content.SoundEffectReader" */ - current_chunk += id_string_len; - - /* ???? */ - if (read_32bitLE(current_chunk, streamFile) != 0) - goto fail; - current_chunk += 4; - - /* ???? */ - if (read_8bit(current_chunk ++, streamFile) != 0) - goto fail; - - /* flag? count of chunks? */ + /* type reader count, accept only one for now */ if (read_8bit(current_chunk++, streamFile) != 1) goto fail; - /* fmt size */ + reader_string_len = read_8bit(current_chunk++, streamFile); /* doesn't count null */ + if (reader_string_len > 255) goto fail; + + /* check SoundEffect type string */ + if (read_string(reader_name,reader_string_len+1,current_chunk,streamFile) != reader_string_len) + goto fail; + if ( strcmp(reader_name, type_sound) != 0 ) + goto fail; + current_chunk += reader_string_len + 1; + current_chunk += 4; /* reader version */ + + /* shared resource count */ + if (read_8bit(current_chunk++, streamFile) != 1) + goto fail; + + /* shared resource: partial "fmt" chunk */ fmt_chunk_size = read_32bitLE(current_chunk, streamFile); current_chunk += 4; @@ -918,10 +907,8 @@ VGMSTREAM * init_vgmstream_xnbm(STREAMFILE *streamFile) { 0, /* sns == false */ 0)) /* mwv == false */ goto fail; - current_chunk += fmt_chunk_size; - /* data size! */ data_size = read_32bitLE(current_chunk, streamFile); current_chunk += 4; @@ -930,40 +917,40 @@ VGMSTREAM * init_vgmstream_xnbm(STREAMFILE *streamFile) { switch (fmt.coding_type) { case coding_PCM16LE: - sample_count = data_size/2/fmt.channel_count; + num_samples = pcm_bytes_to_samples(data_size, fmt.channel_count, 16); break; case coding_PCM8_U_int: - sample_count = data_size/fmt.channel_count; + num_samples = pcm_bytes_to_samples(data_size, fmt.channel_count, 8); break; case coding_MSADPCM: - sample_count = msadpcm_bytes_to_samples(data_size, fmt.block_size, fmt.channel_count); + num_samples = msadpcm_bytes_to_samples(data_size, fmt.block_size, fmt.channel_count); break; case coding_MS_IMA: - sample_count = (data_size / fmt.block_size) * (fmt.block_size - 4 * fmt.channel_count) * 2 / fmt.channel_count + + num_samples = (data_size / fmt.block_size) * (fmt.block_size - 4 * fmt.channel_count) * 2 / fmt.channel_count + ((data_size % fmt.block_size) ? (data_size % fmt.block_size - 4 * fmt.channel_count) * 2 / fmt.channel_count : 0); break; default: + VGM_LOG("XNB: unknown codec 0x%x\n", fmt.coding_type); goto fail; } - /* build the VGMSTREAM */ + /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(fmt.channel_count,loop_flag); if (!vgmstream) goto fail; - /* fill in the vital statistics */ - vgmstream->num_samples = sample_count; + vgmstream->num_samples = num_samples; vgmstream->sample_rate = fmt.sample_rate; + vgmstream->meta_type = meta_XNB; vgmstream->coding_type = fmt.coding_type; - vgmstream->layout_type = layout_none; if (fmt.channel_count > 1) { switch (fmt.coding_type) { case coding_PCM8_U_int: case coding_MS_IMA: case coding_MSADPCM: - // use layout_none from above + vgmstream->layout_type = layout_none; break; default: vgmstream->layout_type = layout_interleave; @@ -971,40 +958,24 @@ VGMSTREAM * init_vgmstream_xnbm(STREAMFILE *streamFile) { } } - vgmstream->interleave_block_size = fmt.interleave; switch (fmt.coding_type) { case coding_MSADPCM: case coding_MS_IMA: - // override interleave_block_size with frame size vgmstream->interleave_block_size = fmt.block_size; break; default: - // use interleave from above + vgmstream->interleave_block_size = fmt.interleave; break; } - vgmstream->meta_type = meta_XNBm; - /* open the file, set up each channel */ - { - int i; - - vgmstream->ch[0].streamfile = streamFile->open(streamFile,filename, - STREAMFILE_DEFAULT_BUFFER_SIZE); - if (!vgmstream->ch[0].streamfile) goto fail; - - for (i=0;ich[i].streamfile = vgmstream->ch[0].streamfile; - vgmstream->ch[i].offset = vgmstream->ch[i].channel_start_offset = - start_offset+i*fmt.interleave; - } - } + if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) ) + goto fail; return vgmstream; - /* clean up anything we may have opened */ fail: - if (vgmstream) close_vgmstream(vgmstream); + close_vgmstream(vgmstream); return NULL; } diff --git a/Frameworks/vgmstream/vgmstream/src/meta/txth.c b/Frameworks/vgmstream/vgmstream/src/meta/txth.c index d75af34fd..1432ca9b4 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/txth.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/txth.c @@ -289,6 +289,9 @@ found: /* default FFmpeg */ ffmpeg_data = init_ffmpeg_offset(streamFile, txth.start_offset,txth.data_size); if ( !ffmpeg_data ) goto fail; + + if (vgmstream->num_samples == 0) + vgmstream->num_samples = ffmpeg_data->totalSamples; /* sometimes works */ } else { /* fake header FFmpeg */ @@ -674,7 +677,7 @@ static int get_bytes_to_samples(txth_header * txth, uint32_t bytes) { return (bytes / txth->interleave) * (txth->interleave - 2) * 2; case MPEG: /* a bit complex */ - case FFMPEG: /* too complex */ + case FFMPEG: /* too complex, try after init */ default: return 0; } diff --git a/Frameworks/vgmstream/vgmstream/src/meta/wwise.c b/Frameworks/vgmstream/vgmstream/src/meta/wwise.c index 173b8eb55..435377c9a 100644 --- a/Frameworks/vgmstream/vgmstream/src/meta/wwise.c +++ b/Frameworks/vgmstream/vgmstream/src/meta/wwise.c @@ -8,7 +8,7 @@ * * Some info: https://www.audiokinetic.com/en/library/edge/ */ -typedef enum { PCM, IMA, VORBIS, DSP, XMA2, XWMA, AAC, HEVAG, ATRAC9 } wwise_codec; +typedef enum { PCM, IMA, VORBIS, DSP, XMA2, XWMA, AAC, HEVAG, ATRAC9, OPUS } wwise_codec; typedef struct { int big_endian; size_t file_size; @@ -142,6 +142,7 @@ VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { case 0x0162: ww.codec = XWMA; break; case 0x0165: ww.codec = XMA2; break; /* always with the "XMA2" chunk, Wwise doesn't use XMA1 */ case 0x0166: ww.codec = XMA2; break; + case 0x3039: ww.codec = OPUS; break; case 0xAAC0: ww.codec = AAC; break; case 0xFFF0: ww.codec = DSP; break; case 0xFFFB: ww.codec = HEVAG; break; @@ -283,6 +284,8 @@ VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { } } + //ww.data_size -= audio_offset; //todo test + vgmstream->codec_data = init_vorbis_custom_codec_data(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); if (!vgmstream->codec_data) goto fail; } @@ -326,6 +329,7 @@ VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { cfg.packet_type = STANDARD; } + //ww.data_size -= audio_offset; //todo test /* try with the selected codebooks */ vgmstream->codec_data = init_vorbis_custom_codec_data(streamFile, start_offset + setup_offset, VORBIS_WWISE, &cfg); @@ -340,7 +344,7 @@ VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { vgmstream->coding_type = coding_VORBIS_custom; vgmstream->codec_endian = ww.big_endian; - start_offset = start_offset + audio_offset; + start_offset += audio_offset; /* Vorbis is VBR so this is very approximate, meh */ if (ww.truncated) @@ -467,11 +471,48 @@ VGMSTREAM * init_vgmstream_wwise(STREAMFILE *streamFile) { vgmstream->num_samples = ffmpeg_data->totalSamples; break; } + + case OPUS: { /* Switch */ + uint8_t buf[0x100]; + size_t bytes, skip; + ffmpeg_custom_config cfg; + + /* values up to 0x14 seem fixed and similar to HEVAG's (block_align 0x02/04, bits_per_sample 0x10) */ + if (ww.fmt_size == 0x28) { + size_t seek_size; + + vgmstream->num_samples += read_32bit(ww.fmt_offset + 0x18, streamFile); + //todo 0x1c and 0x20: related to samples/looping? + seek_size = read_32bit(ww.fmt_offset + 0x24, streamFile); + + start_offset += seek_size; + ww.data_size -= seek_size; + } + else { + goto fail; + } + + skip = 0; /* Wwise doesn't seem to use it? (0x138 /0x3E8 ~default) */ + + bytes = ffmpeg_make_opus_header(buf,0x100, ww.channels, skip, ww.sample_rate); + if (bytes <= 0) goto fail; + + memset(&cfg, 0, sizeof(ffmpeg_custom_config)); + cfg.type = FFMPEG_WWISE_OPUS; + //cfg.big_endian = ww.big_endian; /* internally BE */ + + vgmstream->codec_data = init_ffmpeg_config(streamFile, buf,bytes, start_offset,ww.data_size, &cfg); + if (!vgmstream->codec_data) goto fail; + + vgmstream->coding_type = coding_FFmpeg; + vgmstream->layout_type = layout_none; + break; + } #endif case HEVAG: /* PSV */ /* changed values, another bizarre Wwise quirk */ //ww.block_align /* unknown (1ch=2, 2ch=4) */ - //ww.bits_per_sample; /* probably interleave (0x10) */ + //ww.bits_per_sample; /* unknown (0x10) */ //if (ww.bits_per_sample != 4) goto fail; if (ww.big_endian) goto fail; diff --git a/Frameworks/vgmstream/vgmstream/src/vgmstream.h b/Frameworks/vgmstream/vgmstream/src/vgmstream.h index a7b2cda8f..d50a1e149 100644 --- a/Frameworks/vgmstream/vgmstream/src/vgmstream.h +++ b/Frameworks/vgmstream/vgmstream/src/vgmstream.h @@ -64,6 +64,7 @@ enum { STREAM_NAME_SIZE = 255 }; /* reasonable max */ #ifdef VGM_USE_FFMPEG #include #include +#include #endif #include "clHCA.h" @@ -487,7 +488,7 @@ typedef enum { meta_RIFF_WAVE_SNS, /* .sns RIFF */ meta_RIFX_WAVE, /* RIFX, for big-endian WAVs */ meta_RIFX_WAVE_smpl, /* RIFX w/ loop data in smpl chunk */ - meta_XNBm, /* XNBm, which has a RIFF fmt chunk */ + meta_XNB, /* XNA Game Studio 4.0 */ meta_PC_MXST, /* Lego Island MxSt */ meta_PC_SOB_SAB, /* Worms 4 Mayhem SOB+SAB file */ meta_NWA, /* Visual Art's NWA */ @@ -1033,22 +1034,45 @@ typedef struct { } hca_codec_data; #ifdef VGM_USE_FFMPEG +/* Custom FFMPEG modes */ +typedef enum { + FFMPEG_STANDARD, /* default FFmpeg */ + FFMPEG_WWISE_OPUS, /* Opus without Ogg layer */ + FFMPEG_EA_XMA, /* XMA with padding removed in SNS blocks */ + //FFMPEG_EA_SCHL, /* Normal header+data (ex. ATRAC3) in SCxx blocks */ + //FFMPEG_SFH, /* ATRAC3plus header+data in SFH blocks */ + //FFMPEG_AWC_XMA, /* XMA data in AWC blocks, 1 streams per channel */ +} ffmpeg_custom_t; + +/* config for the above modes */ typedef struct { - /*** init data ***/ + int stream_index; /* FFmpeg's sub-stream (as opposed to an internal stream in custom read/seeks) */ + int codec_endian; + + ffmpeg_custom_t type; /* ffmpeg subtype */ + size_t virtual_size; /* external value, if meta needs to know/supply it */ + + /* internal sequences, when needed */ + int sequence; + int samples_done; +} ffmpeg_custom_config; + +typedef struct { + /*** IO internals ***/ STREAMFILE *streamfile; + + uint64_t real_start; // absolute start within the streamfile + uint64_t real_offset; // absolute offset within the streamfile + uint64_t real_size; // max size within the streamfile + uint64_t virtual_offset; // computed offset FFmpeg sees (including fake header) + uint64_t virtual_size; // computed size FFmpeg sees (including fake header) + uint64_t virtual_base; // info/base virtual_offset equivalent to current real_offset (block aligned) - // offset and total size of raw stream data - uint64_t start; - uint64_t size; - - // offset into stream, includes header_size if header exists - uint64_t offset; - - // inserted header, ie. fake RIFF header - uint8_t *header_insert_block; - // header/fake RIFF over the real (parseable by FFmpeg) file start - uint64_t header_size; - + uint64_t header_size; // fake header (parseable by FFmpeg) prepended on reads + uint8_t *header_insert_block; // fake header data (ie. RIFF) + + ffmpeg_custom_config config; /* custom config/state */ + /*** "public" API (read-only) ***/ // stream info int channels; @@ -1088,6 +1112,8 @@ typedef struct { // Seeking is not ideal, so rollback is necessary int samplesToDiscard; + + } ffmpeg_codec_data; #endif