2010-09-05 00:06:39 +02:00
|
|
|
#include "meta.h"
|
2010-09-10 23:49:56 +02:00
|
|
|
#include "../coding/coding.h"
|
2018-01-13 11:36:35 +01:00
|
|
|
#include "../layout/layout.h"
|
|
|
|
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2017-12-28 23:37:18 +01:00
|
|
|
/* XVAG - Sony's Scream Tool/Stream Creator format (God of War III, Ratchet & Clank Future, The Last of Us, Uncharted) */
|
2017-09-29 23:37:20 +02:00
|
|
|
VGMSTREAM * init_vgmstream_xvag(STREAMFILE *streamFile) {
|
2010-09-05 00:06:39 +02:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2017-02-18 15:43:29 +01:00
|
|
|
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
|
|
|
|
int loop_flag = 0, channel_count, codec;
|
2017-12-28 23:37:18 +01:00
|
|
|
int big_endian;
|
2018-01-13 11:36:35 +01:00
|
|
|
int sample_rate, num_samples, interleave_factor, multistreams = 0;
|
2017-12-28 23:37:18 +01:00
|
|
|
int total_subsongs = 0, target_subsong = streamFile->stream_index;
|
2010-09-09 00:33:06 +02:00
|
|
|
|
2018-01-13 15:23:17 +01:00
|
|
|
off_t start_offset, loop_start = 0, loop_end = 0, chunk_offset;
|
2017-02-18 15:43:29 +01:00
|
|
|
off_t first_offset = 0x20;
|
2018-08-23 17:00:52 +02:00
|
|
|
size_t chunk_size, data_size;
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2018-08-15 17:36:20 +02:00
|
|
|
|
|
|
|
/* checks */
|
|
|
|
/* .xvag: standard
|
|
|
|
* (extensionless): The Last Of Us (PS3) speech files */
|
2018-06-28 22:30:33 +02:00
|
|
|
if (!check_extensions(streamFile,"xvag,"))
|
|
|
|
goto fail;
|
|
|
|
|
2010-09-05 00:06:39 +02:00
|
|
|
/* check header */
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x58564147) /* "XVAG" */
|
|
|
|
goto fail;
|
|
|
|
|
2018-01-04 17:35:14 +01:00
|
|
|
/* endian flag (XVAGs of the same game can use BE or LE, usually when reusing from other platforms) */
|
|
|
|
big_endian = read_8bit(0x08,streamFile) & 0x01;
|
2017-12-28 23:37:18 +01:00
|
|
|
if (big_endian) {
|
2017-02-18 15:43:29 +01:00
|
|
|
read_32bit = read_32bitBE;
|
2017-12-28 23:37:18 +01:00
|
|
|
} else {
|
|
|
|
read_32bit = read_32bitLE;
|
2017-02-18 15:43:29 +01:00
|
|
|
}
|
2010-09-10 23:49:56 +02:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
start_offset = read_32bit(0x04,streamFile);
|
2018-01-04 17:35:14 +01:00
|
|
|
/* 0x08: flags? (&0x01=big endian, 0x02=?, 0x06=full RIFF AT9?)
|
|
|
|
* 0x09: flags2? (0x00/0x01/0x04, speaker mode?)
|
|
|
|
* 0x0a: always 0?
|
2018-01-13 11:36:35 +01:00
|
|
|
* 0x0b: version-flag? (0x5f/0x60/0x61/0x62/etc) */
|
2017-02-18 15:43:29 +01:00
|
|
|
|
2018-08-15 17:36:20 +02:00
|
|
|
|
2018-01-04 17:35:14 +01:00
|
|
|
/* "fmat": base format (always first) */
|
2017-12-28 23:37:18 +01:00
|
|
|
if (!find_chunk(streamFile, 0x666D6174,first_offset,0, &chunk_offset,&chunk_size, big_endian, 1)) /*"fmat"*/
|
|
|
|
goto fail;
|
2017-02-18 15:43:29 +01:00
|
|
|
channel_count = read_32bit(chunk_offset+0x00,streamFile);
|
|
|
|
codec = read_32bit(chunk_offset+0x04,streamFile);
|
|
|
|
num_samples = read_32bit(chunk_offset+0x08,streamFile);
|
2018-01-04 17:35:14 +01:00
|
|
|
/* 0x0c: samples again? playable section? */
|
|
|
|
VGM_ASSERT(num_samples != read_32bit(chunk_offset+0x0c,streamFile), "XVAG: num_samples values don't match\n");
|
|
|
|
|
2018-01-13 11:36:35 +01:00
|
|
|
interleave_factor = read_32bit(chunk_offset+0x10,streamFile);
|
2017-02-18 15:43:29 +01:00
|
|
|
sample_rate = read_32bit(chunk_offset+0x14,streamFile);
|
2018-08-23 17:00:52 +02:00
|
|
|
data_size = read_32bit(chunk_offset+0x18,streamFile); /* not always accurate */
|
2017-12-28 23:37:18 +01:00
|
|
|
|
2018-01-13 11:36:35 +01:00
|
|
|
/* extra data, seen in versions 0x61+ */
|
2018-01-04 17:35:14 +01:00
|
|
|
if (chunk_size > 0x1c) {
|
2018-01-13 11:36:35 +01:00
|
|
|
/* number of interleaved subsong layers */
|
|
|
|
total_subsongs = read_32bit(chunk_offset+0x1c,streamFile);
|
2018-08-15 17:36:20 +02:00
|
|
|
/* number of interleaved streams per layer (multistreams * channels_per_stream = channels) */
|
2018-01-13 11:36:35 +01:00
|
|
|
multistreams = read_32bit(chunk_offset+0x20,streamFile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
total_subsongs = 1;
|
|
|
|
multistreams = 1;
|
2017-12-28 23:37:18 +01:00
|
|
|
}
|
2018-01-04 17:35:14 +01:00
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
|
2017-12-28 23:37:18 +01:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
|
|
|
|
/* other chunks: */
|
|
|
|
/* "cpan": pan/volume per channel */
|
2018-01-04 17:35:14 +01:00
|
|
|
/* "cues": cue/labels (rare) */
|
2017-02-19 20:21:57 +01:00
|
|
|
/* "0000": end chunk before start_offset */
|
2017-02-18 15:43:29 +01:00
|
|
|
|
2018-08-23 17:00:52 +02:00
|
|
|
/* XVAG has no looping, but some PS3 PS-ADPCM seems to do full loops (without data flags) */
|
2018-01-13 11:36:35 +01:00
|
|
|
if (codec == 0x06 && total_subsongs == 1) {
|
2018-08-23 17:00:52 +02:00
|
|
|
size_t file_size = get_streamfile_size(streamFile);
|
|
|
|
/* simply test if last frame is not empty = may loop */
|
|
|
|
loop_flag = (read_8bit(file_size - 0x01, streamFile) != 0);
|
|
|
|
loop_start = 0;
|
|
|
|
loop_end = file_size - start_offset;
|
2017-02-18 15:43:29 +01:00
|
|
|
}
|
2010-09-10 23:49:56 +02:00
|
|
|
|
2018-08-15 17:36:20 +02:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
/* build the VGMSTREAM */
|
2010-09-05 00:06:39 +02:00
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2010-09-10 23:49:56 +02:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
vgmstream->num_samples = num_samples;
|
2017-12-28 23:37:18 +01:00
|
|
|
vgmstream->num_streams = total_subsongs;
|
2018-08-23 17:00:52 +02:00
|
|
|
vgmstream->stream_size = (data_size / total_subsongs);
|
2017-09-29 23:37:20 +02:00
|
|
|
vgmstream->meta_type = meta_XVAG;
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
switch (codec) {
|
2018-08-15 17:36:20 +02:00
|
|
|
case 0x06: /* VAG (PS-ADPCM): God of War III (PS3), Uncharted 1/2 (PS3), Ratchet and Clank Future (PS3) */
|
|
|
|
case 0x07: /* SVAG? (PS-ADPCM with extended table?): inFamous 1 (PS3) */
|
2018-01-13 11:36:35 +01:00
|
|
|
if (multistreams > 1 && multistreams != vgmstream->channels) goto fail;
|
|
|
|
if (total_subsongs > 1 && multistreams > 1) goto fail;
|
|
|
|
if (total_subsongs > 1 && vgmstream->channels > 1) goto fail; /* unknown layout */
|
2018-01-04 17:35:14 +01:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
|
2018-01-13 11:36:35 +01:00
|
|
|
if (total_subsongs > 1) { /* God of War 3 (PS4) */
|
|
|
|
vgmstream->layout_type = layout_blocked_xvag_subsong;
|
|
|
|
vgmstream->interleave_block_size = 0x10;
|
|
|
|
vgmstream->full_block_size = 0x10 * interleave_factor * total_subsongs;
|
|
|
|
vgmstream->current_block_size = 0x10 * interleave_factor;
|
|
|
|
start_offset += 0x10 * interleave_factor * (target_subsong-1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x10 * interleave_factor; /* usually 1, bigger in GoW3 PS4 */
|
|
|
|
}
|
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
if (loop_flag) {
|
2017-12-28 23:42:04 +01:00
|
|
|
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start, vgmstream->channels);
|
|
|
|
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end, vgmstream->channels);
|
2017-02-18 15:43:29 +01:00
|
|
|
}
|
2018-01-13 11:36:35 +01:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
break;
|
|
|
|
|
2010-09-10 23:49:56 +02:00
|
|
|
#ifdef VGM_USE_MPEG
|
2018-01-04 17:35:14 +01:00
|
|
|
case 0x08: { /* MPEG: The Last of Us (PS3), Uncharted 3 (PS3), Medieval Moves (PS3) */
|
2017-12-17 19:25:10 +01:00
|
|
|
mpeg_custom_config cfg = {0};
|
2017-07-29 23:14:04 +02:00
|
|
|
|
2018-01-13 11:36:35 +01:00
|
|
|
/* often 2ch per MPEG and rarely 1ch (GoW3 PS4) */
|
|
|
|
if (multistreams > 1 && !(multistreams*1 == vgmstream->channels || multistreams*2 == vgmstream->channels)) goto fail;
|
|
|
|
if (total_subsongs > 1) goto fail;
|
|
|
|
//todo rare test file in The Last of Us PS4 uses 6ch with 1 2ch stream, surround MPEG/mp3pro?
|
2018-01-04 17:35:14 +01:00
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
/* "mpin": mpeg info */
|
2017-02-19 20:21:57 +01:00
|
|
|
/* 0x00/04: mpeg version/layer? other: unknown or repeats of "fmat" */
|
2017-12-28 23:37:18 +01:00
|
|
|
if (!find_chunk(streamFile, 0x6D70696E,first_offset,0, &chunk_offset,NULL, big_endian, 1)) /*"mpin"*/
|
|
|
|
goto fail;
|
2017-02-18 15:43:29 +01:00
|
|
|
|
2017-12-17 19:25:10 +01:00
|
|
|
cfg.chunk_size = read_32bit(chunk_offset+0x1c,streamFile); /* fixed frame size */
|
2018-01-13 11:36:35 +01:00
|
|
|
cfg.interleave = cfg.chunk_size * interleave_factor;
|
2010-09-10 23:49:56 +02:00
|
|
|
|
2018-01-04 23:22:03 +01:00
|
|
|
vgmstream->codec_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_XVAG, &cfg);
|
2017-07-29 23:14:04 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2017-12-17 19:25:10 +01:00
|
|
|
vgmstream->layout_type = layout_none;
|
2017-02-18 15:43:29 +01:00
|
|
|
break;
|
2010-09-10 23:49:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
2017-02-18 15:43:29 +01:00
|
|
|
|
2017-12-28 23:37:18 +01:00
|
|
|
#ifdef VGM_USE_ATRAC9
|
2018-01-04 17:35:14 +01:00
|
|
|
case 0x09: { /* ATRAC9: Sly Cooper and the Thievius Raccoonus (Vita), The Last of Us Remastered (PS4) */
|
2017-12-28 23:37:18 +01:00
|
|
|
atrac9_config cfg = {0};
|
2018-08-15 17:36:20 +02:00
|
|
|
size_t frame_size;
|
2017-12-28 23:37:18 +01:00
|
|
|
|
2017-02-19 20:21:57 +01:00
|
|
|
/* "a9in": ATRAC9 info */
|
2018-08-15 17:36:20 +02:00
|
|
|
/* 0x00: frame size, 0x04: samples per frame, 0x0c: fact num_samples (no change), 0x10: encoder delay1 */
|
2017-12-28 23:37:18 +01:00
|
|
|
if (!find_chunk(streamFile, 0x6139696E,first_offset,0, &chunk_offset,NULL, big_endian, 1)) /*"a9in"*/
|
|
|
|
goto fail;
|
|
|
|
|
2018-08-15 17:36:20 +02:00
|
|
|
frame_size = read_32bit(chunk_offset+0x00,streamFile);
|
|
|
|
|
2018-01-04 17:35:14 +01:00
|
|
|
cfg.type = ATRAC9_XVAG;
|
2017-12-28 23:37:18 +01:00
|
|
|
cfg.channels = vgmstream->channels;
|
|
|
|
cfg.config_data = read_32bitBE(chunk_offset+0x08,streamFile);
|
|
|
|
cfg.encoder_delay = read_32bit(chunk_offset+0x14,streamFile);
|
|
|
|
|
2018-01-04 17:35:14 +01:00
|
|
|
if (total_subsongs > 1 && multistreams > 1) {
|
2018-08-15 17:36:20 +02:00
|
|
|
VGM_LOG("XVAG: unknown %i subsongs and %i multistreams\n", total_subsongs, multistreams);
|
2018-01-04 17:35:14 +01:00
|
|
|
goto fail; /* not known */
|
|
|
|
}
|
|
|
|
else if (total_subsongs > 1) {
|
|
|
|
/* interleaves 'multiplier' superframes per subsong (all share config_data) */
|
2018-08-15 17:36:20 +02:00
|
|
|
cfg.interleave_skip = frame_size * interleave_factor;
|
2018-01-04 17:35:14 +01:00
|
|
|
cfg.subsong_skip = total_subsongs;
|
|
|
|
/* start in subsong's first superframe */
|
|
|
|
start_offset += (target_subsong-1) * cfg.interleave_skip * (cfg.subsong_skip-1);
|
|
|
|
}
|
|
|
|
else if (multistreams > 1) {
|
|
|
|
/* Vita multichannel (flower) interleaves streams like MPEG
|
|
|
|
* PS4 (The Last of Us) uses ATRAC9's multichannel directly instead (multistreams==1) */
|
2018-08-15 17:36:20 +02:00
|
|
|
VGM_LOG("XVAG: unknown %i multistreams of size %x\n", multistreams, frame_size * interleave_factor);
|
2018-01-04 17:35:14 +01:00
|
|
|
goto fail;//todo add
|
2017-12-28 23:37:18 +01:00
|
|
|
}
|
2018-01-04 17:35:14 +01:00
|
|
|
//if (multistreams == vgmstream->channels) goto fail;
|
2017-12-28 23:37:18 +01:00
|
|
|
|
|
|
|
vgmstream->codec_data = init_atrac9(&cfg);
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_ATRAC9;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
break;
|
2017-02-18 15:43:29 +01:00
|
|
|
}
|
2017-12-28 23:37:18 +01:00
|
|
|
#endif
|
2017-02-18 15:43:29 +01:00
|
|
|
|
2018-01-13 11:36:35 +01:00
|
|
|
//case 0x??: /* PCM? */
|
2017-02-18 15:43:29 +01:00
|
|
|
default:
|
|
|
|
goto fail;
|
2010-09-05 00:06:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 15:43:29 +01:00
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
goto fail;
|
|
|
|
|
2018-01-13 11:36:35 +01:00
|
|
|
if (vgmstream->layout_type == layout_blocked_xvag_subsong)
|
|
|
|
block_update_xvag_subsong(start_offset, vgmstream);
|
|
|
|
|
2010-09-05 00:06:39 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|