Fix multilayer XVAG ATRAC9 [flower (Vita), Farpoint (PS4)]

This commit is contained in:
bnnm 2018-08-25 17:25:02 +02:00
parent 1ba2637256
commit 9c8dae38dc
5 changed files with 271 additions and 25 deletions

View File

@ -259,6 +259,10 @@
<File
RelativePath=".\meta\ubi_lyn_ogg_streamfile.h"
>
</File>
<File
RelativePath=".\meta\xvag_streamfile.h"
>
</File>
</Filter>
<Filter

View File

@ -106,7 +106,7 @@
<ClInclude Include="meta\ps2_psh_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\xvag_streamfile.h" />
<ClInclude Include="meta\meta.h" />
<ClInclude Include="meta\hca_keys.h" />
<ClInclude Include="meta\fsb_keys.h" />

View File

@ -101,6 +101,9 @@
<ClInclude Include="meta\ubi_lyn_ogg_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\xvag_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\meta.h">
<Filter>meta\Header Files</Filter>
</ClInclude>

View File

@ -1,6 +1,7 @@
#include "meta.h"
#include "../coding/coding.h"
#include "../layout/layout.h"
#include "xvag_streamfile.h"
typedef struct {
@ -23,9 +24,13 @@ typedef struct {
off_t stream_offset;
} xvag_header;
static int init_xvag_atrac9(STREAMFILE *streamFile, VGMSTREAM* vgmstream, xvag_header * xvag, off_t chunk_offset);
static layered_layout_data* build_layered_xvag(STREAMFILE *streamFile, xvag_header * xvag, off_t chunk_offset, off_t start_offset);
/* XVAG - Sony's Scream Tool/Stream Creator format (God of War III, Ratchet & Clank Future, The Last of Us, Uncharted) */
VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE* temp_streamFile = NULL;
xvag_header xvag = {0};
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
off_t start_offset, chunk_offset, first_offset = 0x20;
@ -165,9 +170,6 @@ VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) {
#ifdef VGM_USE_ATRAC9
case 0x09: { /* ATRAC9: Sly Cooper and the Thievius Raccoonus (Vita), The Last of Us Remastered (PS4) */
atrac9_config cfg = {0};
size_t frame_size;
if (xvag.subsongs > 1 && xvag.layers > 1) goto fail;
/* "a9in": ATRAC9 info */
@ -175,30 +177,27 @@ VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) {
if (!find_chunk(streamFile, 0x6139696E,first_offset,0, &chunk_offset,NULL, xvag.big_endian, 1)) /*"a9in"*/
goto fail;
frame_size = read_32bit(chunk_offset+0x00,streamFile);
if (xvag.layers > 1) {
/* some Vita/PS4 multichannel [flower (Vita), Uncharted Collection (PS4)]. PS4 ATRAC9 also
* does single-stream >2ch, but this can do configs ATRAC9 can't, like 5ch/14ch/etc */
vgmstream->layout_data = build_layered_xvag(streamFile, &xvag, chunk_offset, start_offset);
if (!vgmstream->layout_data) goto fail;
vgmstream->coding_type = coding_ATRAC9;
vgmstream->layout_type = layout_layered;
cfg.type = ATRAC9_XVAG;
cfg.channels = vgmstream->channels;
cfg.config_data = read_32bitBE(chunk_offset+0x08,streamFile);
cfg.encoder_delay = read_32bit(chunk_offset+0x14,streamFile);
if (xvag.subsongs > 1) {
/* interleaves 'multiplier' superframes per subsong (all share config_data) */
cfg.interleave_skip = frame_size * xvag.factor;
cfg.subsong_skip = xvag.subsongs;
/* start in subsong's first superframe */
start_offset += (target_subsong-1) * cfg.interleave_skip * (cfg.subsong_skip-1);
break;
}
else if (xvag.layers > 1) {
/* Vita multichannel, or multilanguage [flower (Vita), Uncharted Collection (PS4)] */
VGM_LOG("XVAG: unknown %i multistreams of size %x\n", xvag.layers, frame_size * xvag.factor);
goto fail;//todo add
else {
/* interleaved subsongs (section layers) */
size_t frame_size = read_32bit(chunk_offset+0x00,streamFile);
if (!init_xvag_atrac9(streamFile, vgmstream, &xvag, chunk_offset))
goto fail;
temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, frame_size*xvag.factor,frame_size, (target_subsong-1), total_subsongs);
if (!temp_streamFile) goto fail;
start_offset = 0;
}
vgmstream->codec_data = init_atrac9(&cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_ATRAC9;
vgmstream->layout_type = layout_none;
break;
}
#endif
@ -209,15 +208,92 @@ VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) {
/* open the file for reading */
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
if (!vgmstream_open_stream(vgmstream,temp_streamFile ? temp_streamFile : streamFile,start_offset))
goto fail;
if (vgmstream->layout_type == layout_blocked_xvag_subsong)
block_update_xvag_subsong(start_offset, vgmstream);
close_streamfile(temp_streamFile);
return vgmstream;
fail:
close_streamfile(temp_streamFile);
close_vgmstream(vgmstream);
return NULL;
}
#ifdef VGM_USE_ATRAC9
static int init_xvag_atrac9(STREAMFILE *streamFile, VGMSTREAM* vgmstream, xvag_header * xvag, off_t chunk_offset) {
int32_t (*read_32bit)(off_t,STREAMFILE*) = xvag->big_endian ? read_32bitBE : read_32bitLE;
atrac9_config cfg = {0};
cfg.channels = vgmstream->channels;
cfg.config_data = read_32bitBE(chunk_offset+0x08,streamFile);
cfg.encoder_delay = read_32bit(chunk_offset+0x14,streamFile);
vgmstream->codec_data = init_atrac9(&cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_ATRAC9;
vgmstream->layout_type = layout_none;
return 1;
fail:
return 0;
}
#endif
static layered_layout_data* build_layered_xvag(STREAMFILE *streamFile, xvag_header * xvag, off_t chunk_offset, off_t start_offset) {
layered_layout_data* data = NULL;
STREAMFILE* temp_streamFile = NULL;
int32_t (*read_32bit)(off_t,STREAMFILE*) = xvag->big_endian ? read_32bitBE : read_32bitLE;
int i, layers = xvag->layers;
/* init layout */
data = init_layout_layered(layers);
if (!data) goto fail;
/* interleaves frames per substreams */
for (i = 0; i < layers; i++) {
int layer_channels = xvag->channels / layers; /* all streams must be equal (XVAG limitation) */
/* build the layer VGMSTREAM */
data->layers[i] = allocate_vgmstream(layer_channels, xvag->loop_flag);
if (!data->layers[i]) goto fail;
data->layers[i]->sample_rate = xvag->sample_rate;
data->layers[i]->num_samples = xvag->num_samples;
switch(xvag->codec) {
#ifdef VGM_USE_ATRAC9
case 0x09: {
size_t frame_size = read_32bit(chunk_offset+0x00,streamFile);
if (!init_xvag_atrac9(streamFile, data->layers[i], xvag, chunk_offset))
goto fail;
temp_streamFile = setup_xvag_streamfile(streamFile, start_offset, frame_size*xvag->factor,frame_size, i, layers);
if (!temp_streamFile) goto fail;
break;
}
#endif
default:
goto fail;
}
if ( !vgmstream_open_stream(data->layers[i], temp_streamFile, 0x00) ) {
goto fail;
}
}
/* setup layered VGMSTREAMs */
if (!setup_layout_layered(data))
goto fail;
close_streamfile(temp_streamFile);
return data;
fail:
close_streamfile(temp_streamFile);
free_layout_layered(data);
return NULL;
}

163
src/meta/xvag_streamfile.h Normal file
View File

@ -0,0 +1,163 @@
#ifndef _XVAG_STREAMFILE_H_
#define _XVAG_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
/* config */
int stream_number;
int stream_count;
size_t interleave_size;
size_t frame_size;
off_t stream_offset;
//size_t stream_size;
/* state */
off_t logical_offset; /* offset that corresponds to physical_offset */
off_t physical_offset; /* actual file offset */
size_t skip_size; /* size to skip from a block start to reach data start */
size_t data_size; /* logical size of the block */
size_t logical_size;
} xvag_io_data;
static size_t xvag_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, xvag_io_data* data) {
size_t total_read = 0;
/* ignore bad reads */
if (offset < 0 || offset > data->logical_size) {
return 0;
}
/* previous offset: re-start as we can't map logical<>physical offsets
* (kinda slow as it trashes buffers, but shouldn't happen often) */
if (offset < data->logical_offset) {
data->logical_offset = 0x00;
data->physical_offset = data->stream_offset;
data->data_size = 0;
}
/* read blocks, one at a time */
while (length > 0) {
/* ignore EOF */
if (data->logical_offset >= data->logical_size) {
break;
}
/* process new block */
if (data->data_size == 0) {
data->skip_size = data->interleave_size * data->stream_number;
data->data_size = data->interleave_size;
/* some ATRAC9 XVAG have padding+RIFF at start [The Last of Us (PS4), Farpoint (PS4)] */
if (data->logical_offset == 0 && read_32bitBE(data->physical_offset+data->skip_size,streamfile) == 0) {
data->skip_size += data->frame_size;
data->data_size -= data->frame_size;
}
}
/* move to next block */
if (offset >= data->logical_offset + data->data_size) {
data->physical_offset += data->interleave_size*data->stream_count;
data->logical_offset += data->data_size;
data->data_size = 0;
continue;
}
/* read data */
{
size_t bytes_consumed, bytes_done, to_read;
bytes_consumed = offset - data->logical_offset;
to_read = data->data_size - bytes_consumed;
if (to_read > length)
to_read = length;
bytes_done = read_streamfile(dest, data->physical_offset + data->skip_size + bytes_consumed, to_read, streamfile);
offset += bytes_done;
total_read += bytes_done;
length -= bytes_done;
dest += bytes_done;
if (bytes_done != to_read || bytes_done == 0) {
break; /* error/EOF */
}
}
}
return total_read;
}
static size_t xvag_io_size(STREAMFILE *streamfile, xvag_io_data* data) {
off_t physical_offset = data->stream_offset;
off_t max_physical_offset = get_streamfile_size(streamfile);
size_t logical_size = 0;
if (data->logical_size)
return data->logical_size;
/* get size of the logical stream */
while (physical_offset < max_physical_offset) {
size_t skip_size = data->interleave_size * data->stream_number;
size_t data_size = data->interleave_size;
/* some ATRAC9 XVAG have padding+RIFF at start [The Last of Us (PS4), Farpoint (PS4)] */
if (logical_size == 0 && read_32bitBE(physical_offset+skip_size,streamfile) == 0) {
skip_size += data->frame_size;
data_size -= data->frame_size;
}
logical_size += data_size;
physical_offset += data->interleave_size*data->stream_count;
}
if (logical_size > max_physical_offset)
return 0;
data->logical_size = logical_size;
return data->logical_size;
}
/* Prepares custom IO for XVAG, which interleaves many superframes per subsong/layer.
* May have start padding, even with only one subsong. All layers share config_data too. */
static STREAMFILE* setup_xvag_streamfile(STREAMFILE *streamFile, off_t stream_offset, size_t interleave_size, size_t frame_size, int stream_number, int stream_count) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
xvag_io_data io_data = {0};
size_t io_data_size = sizeof(xvag_io_data);
io_data.stream_number = stream_number;
io_data.stream_count = stream_count;
io_data.stream_offset = stream_offset;
//io_data.stream_size = stream_size;
io_data.interleave_size = interleave_size;
io_data.frame_size = frame_size;
io_data.physical_offset = stream_offset;
io_data.logical_size = xvag_io_size(streamFile, &io_data); /* force init */
if (io_data.logical_size == 0) {
VGM_LOG("XVAG: wrong logical size\n");
goto fail;
}
/* 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, xvag_io_read,xvag_io_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _XVAG_STREAMFILE_H_ */