Add Capcom multistream Opus [RE: Revelations (Switch)]

This commit is contained in:
bnnm 2018-04-22 02:20:46 +02:00
parent 7a212eed9b
commit e8b2759dcf
5 changed files with 211 additions and 4 deletions

View File

@ -228,6 +228,10 @@
RelativePath=".\meta\ea_eaac_streamfile.h"
>
</File>
<File
RelativePath=".\meta\opus_interleave_streamfile.h"
>
</File>
<File
RelativePath=".\meta\ppst_streamfile.h"
>

View File

@ -100,6 +100,7 @@
<ClInclude Include="meta\bar_streamfile.h" />
<ClInclude Include="meta\ea_eaac_streamfile.h" />
<ClInclude Include="meta\ppst_streamfile.h" />
<ClInclude Include="meta\opus_interleave_streamfile.h" />
<ClInclude Include="meta\sqex_scd_streamfile.h" />
<ClInclude Include="meta\ubi_lyn_ogg_streamfile.h" />
<ClInclude Include="meta\meta.h" />

View File

@ -77,6 +77,9 @@
<ClInclude Include="meta\ea_eaac_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\opus_interleave_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\ppst_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>

View File

@ -1,6 +1,8 @@
#include "meta.h"
#include "../util.h"
#include "../coding/coding.h"
#include "../layout/layout.h"
#include "opus_interleave_streamfile.h"
/* Nintendo OPUS - from Switch games, including header variations (not the same as Ogg Opus) */
@ -128,6 +130,7 @@ fail:
/* Capcom variation [Ultra Street Fighter II (Switch), Resident Evil: Revelations (Switch)] */
VGMSTREAM * init_vgmstream_opus_capcom(STREAMFILE *streamFile) {
VGMSTREAM *vgmstream = NULL;
off_t offset = 0;
int num_samples = 0, loop_start = 0, loop_end = 0;
int channel_count;
@ -137,22 +140,72 @@ VGMSTREAM * init_vgmstream_opus_capcom(STREAMFILE *streamFile) {
goto fail;
channel_count = read_32bitLE(0x04,streamFile);
if (channel_count != 0x01 && channel_count != 0x02)
goto fail;
if (channel_count != 1 && channel_count != 2 && channel_count != 6)
goto fail; /* unknown stream layout */
num_samples = read_32bitLE(0x00,streamFile);
/* 0x04: channels, >2 uses interleaved streams (2ch+2ch+2ch) */
loop_start = read_32bitLE(0x08,streamFile);
loop_end = read_32bitLE(0x0c,streamFile);
/* 0x10: frame size (with extra data) */
/* 0x14: extra chunk count (each of 0x08, after 0x30) */
/* 0x14: extra chunk count */
/* 0x18: null */
offset = read_32bitLE(0x1c,streamFile);
/* 0x20-8: config? (0x0077C102 04000000 E107070C) */
/* 0x2c: some size? */
/* 0x30+: extra chunks (0x00: 0x7f, 0x04: num_sample), alt loop starts/regions? */
if (channel_count == 6) {
/* 2ch multistream hacky-hacks, don't try this at home. We'll end up with:
* main vgmstream > N vgmstream layers > substream IO deinterleaver > opus meta > Opus IO transmogrifier (phew) */
//todo deinterleave has some problems with reading after total_size
layered_layout_data* data = NULL;
int layers = channel_count / 2;
int i;
int loop_flag = (loop_end > 0);
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->layout_type = layout_layered;
/* init layout */
data = init_layout_layered(layers);
if (!data) goto fail;
vgmstream->layout_data = data;
/* open each layer subfile */
for (i = 0; i < layers; i++) {
STREAMFILE* temp_streamFile = setup_opus_interleave_streamfile(streamFile, offset+0x28*i, layers);
if (!temp_streamFile) goto fail;
data->layers[i] = init_vgmstream_opus(temp_streamFile, meta_OPUS, 0x00, num_samples,loop_start,loop_end);
close_streamfile(temp_streamFile);
if (!data->layers[i]) goto fail;
}
/* setup layered VGMSTREAMs */
if (!setup_layout_layered(data))
goto fail;
vgmstream->sample_rate = data->layers[0]->sample_rate;
vgmstream->num_samples = data->layers[0]->num_samples;
vgmstream->loop_start_sample = data->layers[0]->loop_start_sample;
vgmstream->loop_end_sample = data->layers[0]->loop_end_sample;
vgmstream->meta_type = meta_OPUS;
vgmstream->coding_type = data->layers[0]->coding_type;
return vgmstream;
}
else {
return init_vgmstream_opus(streamFile, meta_OPUS, offset, num_samples,loop_start,loop_end);
}
return init_vgmstream_opus(streamFile, meta_OPUS, offset, num_samples,loop_start,loop_end);
fail:
close_vgmstream(vgmstream);
return NULL;
}

View File

@ -0,0 +1,146 @@
#ifndef _OPUS_CAPCOM_STREAMFILE_H_
#define _OPUS_CAPCOM_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
/* state */
off_t logical_offset; /* offset that corresponds to physical_offset */
off_t physical_offset; /* actual file offset */
int skip_frames; /* frames to skip from other streams at points */
/* config */
int version;
int streams;
off_t start_offset; /* pointing to the stream's beginning */
size_t total_size; /* size of the resulting substream */
} opus_interleave_io_data;
/* Reads skipping other streams */
static size_t opus_interleave_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, opus_interleave_io_data* data) {
size_t total_read = 0;
/* ignore bad reads */
if (offset < 0 || offset > data->total_size) {
return total_read;
}
/* previous offset: re-start as we can't map logical<>physical offsets, since it may be VBR
* (kinda slow as it trashes buffers, but shouldn't happen often) */
if (offset < data->logical_offset) {
data->physical_offset = data->start_offset;
data->logical_offset = 0x00;
}
/* read doing one frame at a time */
while (length > 0) {
size_t to_read, bytes_read;
off_t intrablock_offset, intradata_offset;
uint32_t data_size;
data_size = read_32bitBE(data->physical_offset+0x00,streamfile);
//if (offset >= data->total_size) //todo fix
// return total_read;
/* Nintendo Opus header rather than a frame */
if ((uint32_t)data_size == 0x01000080) {
data_size = read_32bitLE(data->physical_offset+0x10,streamfile);
data_size += 0x08;
}
else {
data_size += 0x08;
}
/* skip frames from other streams */
if (data->skip_frames) {
data->physical_offset += data_size;
data->skip_frames--;
continue;
}
/* requested offset is outside current block, try next */
if (offset >= data->logical_offset + data_size) {
data->physical_offset += data_size;
data->logical_offset += data_size;
data->skip_frames = data->streams - 1;
continue;
}
/* reads could fall in the middle of the block */
intradata_offset = offset - data->logical_offset;
intrablock_offset = intradata_offset;
/* clamp reads up to this block's end */
to_read = (data_size - intradata_offset);
if (to_read > length)
to_read = length;
if (to_read == 0)
return total_read; /* should never happen... */
/* finally read and move buffer/offsets */
bytes_read = read_streamfile(dest, data->physical_offset + intrablock_offset, to_read, streamfile);
total_read += bytes_read;
if (bytes_read != to_read)
return total_read; /* couldn't read fully */
dest += bytes_read;
offset += bytes_read;
length -= bytes_read;
/* block fully read, go next */
if (intradata_offset + bytes_read == data_size) {
data->physical_offset += data_size;
data->logical_offset += data_size;
data->skip_frames = data->streams - 1;
}
}
return total_read;
}
static size_t opus_interleave_io_size(STREAMFILE *streamfile, opus_interleave_io_data* data) {
off_t info_offset;
if (data->total_size)
return data->total_size;
info_offset = read_32bitLE(data->start_offset+0x10,streamfile);
return read_32bitLE(data->start_offset + info_offset+0x04,streamfile);
}
/* Prepares custom IO for multistream Opus, interleaves 1 packet per stream */
static STREAMFILE* setup_opus_interleave_streamfile(STREAMFILE *streamFile, off_t start_offset, int streams) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
opus_interleave_io_data io_data = {0};
size_t io_data_size = sizeof(opus_interleave_io_data);
io_data.start_offset = start_offset;
io_data.streams = streams;
io_data.physical_offset = start_offset;
io_data.total_size = opus_interleave_io_size(streamFile, &io_data); /* force init */
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, opus_interleave_io_read,opus_interleave_io_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_buffer_streamfile(new_streamFile,0);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _OPUS_CAPCOM_STREAMFILE_H_ */