2017-05-13 13:01:16 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../util.h"
|
2017-05-13 23:54:09 +02:00
|
|
|
#include "../coding/coding.h"
|
2017-05-13 13:01:16 +02:00
|
|
|
|
2017-05-13 23:54:09 +02:00
|
|
|
/* RXWS - from Sony SCEI PS2 games (Okage: Shadow King, Genji, Bokura no Kazoku) */
|
2021-06-20 10:54:00 +02:00
|
|
|
VGMSTREAM* init_vgmstream_rxws(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
|
|
|
STREAMFILE* sh = NULL;
|
2017-08-12 11:46:28 +02:00
|
|
|
off_t start_offset, chunk_offset, name_offset = 0;
|
2018-01-28 00:41:25 +01:00
|
|
|
size_t stream_size, chunk_size;
|
2021-06-20 10:54:00 +02:00
|
|
|
int loop_flag = 0, channels, is_separate = 0, type, sample_rate;
|
2019-08-26 22:58:43 +02:00
|
|
|
int32_t num_samples, loop_start;
|
2021-06-20 10:54:00 +02:00
|
|
|
int total_subsongs, target_subsong = sf->stream_index;
|
2017-05-13 23:54:09 +02:00
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
/* checks */
|
|
|
|
/* .xws: header and data
|
|
|
|
* .xwh+xwb: header + data (.bin+dat are also found in Wild Arms 4/5) */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!check_extensions(sf,"xws,xwb"))
|
2019-08-26 22:58:43 +02:00
|
|
|
goto fail;
|
2021-06-20 10:54:00 +02:00
|
|
|
is_separate = check_extensions(sf,"xwb");
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* xwh+xwb: use xwh as header; otherwise use the current file */
|
|
|
|
if (is_separate) {
|
2019-08-26 22:58:43 +02:00
|
|
|
/* extra check to reject Microsoft's XWB faster */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (is_id32be(0x00,sf,"WBND") || /* (LE) */
|
|
|
|
is_id32be(0x00,sf,"DNBW")) /* (BE) */
|
2017-05-13 23:54:09 +02:00
|
|
|
goto fail;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
sh = open_streamfile_by_ext(sf, "xwh");
|
|
|
|
if (!sh) goto fail;
|
2017-05-13 23:54:09 +02:00
|
|
|
} else {
|
2021-06-20 10:54:00 +02:00
|
|
|
sh = sf;
|
2017-05-13 23:54:09 +02:00
|
|
|
}
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!is_id32be(0x00,sh,"RXWS"))
|
2017-05-13 23:54:09 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* file size (just the .xwh/xws) */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (read_u32le(0x04,sh) + 0x10 != get_streamfile_size(sh))
|
2017-05-13 23:54:09 +02:00
|
|
|
goto fail;
|
2021-06-20 10:54:00 +02:00
|
|
|
/* 0x08: version (0x100/0x200)
|
|
|
|
* 0x0C: null */
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* typical chunks: FORM, FTXT, MARK, BODY (for .xws) */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!is_id32be(0x10,sh,"FORM")) /* main header (always first) */
|
2017-05-13 23:54:09 +02:00
|
|
|
goto fail;
|
2021-06-20 10:54:00 +02:00
|
|
|
chunk_size = read_u32le(0x10+0x04,sh); /* size - 0x10 */
|
2017-08-12 11:46:28 +02:00
|
|
|
/* 0x08 version (0x100), 0x0c: null */
|
2017-05-13 23:54:09 +02:00
|
|
|
chunk_offset = 0x20;
|
|
|
|
|
2017-08-12 11:46:28 +02:00
|
|
|
|
2017-05-13 23:54:09 +02:00
|
|
|
/* check multi-streams */
|
2021-06-20 10:54:00 +02:00
|
|
|
total_subsongs = read_s32le(chunk_offset+0x00,sh);
|
2018-01-28 00:41:25 +01:00
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* read stream header */
|
|
|
|
{
|
2018-01-28 00:41:25 +01:00
|
|
|
off_t header_offset = chunk_offset + 0x4 + 0x1c * (target_subsong-1); /* position in FORM */
|
2017-05-13 23:54:09 +02:00
|
|
|
off_t stream_offset, next_stream_offset, data_offset = 0;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
type = read_u8(header_offset+0x00, sh);
|
|
|
|
/* 0x01: unknown (always 0x1c) */
|
|
|
|
/* 0x02: flags? (usually 8002/0002, & 0x01 if looped) */
|
|
|
|
/* 0x04: vol/pan stuff? (0x00007F7F) */
|
|
|
|
/* 0x08: null? */
|
|
|
|
channels = read_u8(header_offset+0x09, sh);
|
|
|
|
/* 0x0c: null? */
|
|
|
|
sample_rate = read_u16le(header_offset+0x0a,sh);
|
|
|
|
stream_offset = read_u32le(header_offset+0x10,sh);
|
|
|
|
num_samples = read_s32le(header_offset+0x14,sh);
|
|
|
|
loop_start = read_s32le(header_offset+0x18,sh);
|
|
|
|
loop_flag = (loop_start >= 0);
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* find data start and size */
|
|
|
|
if (is_separate) {
|
|
|
|
data_offset = 0x00;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
off_t current_chunk = 0x10;
|
|
|
|
/* note the extra 0x10 in chunk_size/offsets */
|
2021-06-20 10:54:00 +02:00
|
|
|
while (current_chunk < get_streamfile_size(sf)) {
|
|
|
|
if (is_id32be(current_chunk,sf, "BODY")) {
|
2017-05-13 23:54:09 +02:00
|
|
|
data_offset = 0x10 + current_chunk;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-20 10:54:00 +02:00
|
|
|
current_chunk += 0x10 + read_u32le(current_chunk+4,sf);
|
2017-05-13 23:54:09 +02:00
|
|
|
}
|
|
|
|
if (!data_offset) goto fail;
|
|
|
|
}
|
|
|
|
|
2018-01-28 00:41:25 +01:00
|
|
|
if (target_subsong == total_subsongs) {
|
2021-06-20 10:54:00 +02:00
|
|
|
next_stream_offset = get_streamfile_size(is_separate ? sf : sh) - data_offset;
|
2017-05-13 23:54:09 +02:00
|
|
|
} else {
|
2018-01-28 00:41:25 +01:00
|
|
|
off_t next_header_offset = chunk_offset + 0x4 + 0x1c * (target_subsong);
|
2021-06-20 10:54:00 +02:00
|
|
|
next_stream_offset = read_u32le(next_header_offset+0x10,sh);
|
2017-05-13 23:54:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 00:41:25 +01:00
|
|
|
stream_size = next_stream_offset - stream_offset;
|
2017-05-13 23:54:09 +02:00
|
|
|
start_offset = data_offset + stream_offset;
|
|
|
|
}
|
|
|
|
|
2017-08-12 11:46:28 +02:00
|
|
|
/* get stream name (always follows FORM) */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (is_id32be(0x10+0x10 + chunk_size,sh, "FTXT")) {
|
2017-08-12 11:46:28 +02:00
|
|
|
chunk_offset = 0x10+0x10 + chunk_size + 0x10;
|
2021-06-20 10:54:00 +02:00
|
|
|
if (read_s32le(chunk_offset+0x00,sh) == total_subsongs) {
|
|
|
|
name_offset = chunk_offset + read_u32le(chunk_offset+0x04 + (target_subsong-1)*0x04,sh);
|
2017-08-12 11:46:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream = allocate_vgmstream(channels,loop_flag);
|
2017-05-13 23:54:09 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->meta_type = meta_RXWS;
|
2017-05-13 23:54:09 +02:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
2018-01-28 00:41:25 +01:00
|
|
|
vgmstream->num_streams = total_subsongs;
|
|
|
|
vgmstream->stream_size = stream_size;
|
2017-08-12 11:46:28 +02:00
|
|
|
if (name_offset)
|
2021-06-20 10:54:00 +02:00
|
|
|
read_string(vgmstream->stream_name,STREAM_NAME_SIZE, name_offset,sh);
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 0x00: /* PS-ADPCM */
|
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x10;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->num_samples = ps_bytes_to_samples(num_samples, channels);
|
|
|
|
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start, channels);
|
2019-08-26 22:58:43 +02:00
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
2017-05-13 23:54:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x01: /* PCM */
|
|
|
|
vgmstream->coding_type = coding_PCM16LE;
|
2019-08-26 22:58:43 +02:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
2017-05-13 23:54:09 +02:00
|
|
|
vgmstream->interleave_block_size = 0x2;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->num_samples = pcm_bytes_to_samples(num_samples, channels, 16);
|
|
|
|
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channels, 16);
|
2019-08-26 22:58:43 +02:00
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
2017-05-13 23:54:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 0x02: { /* ATRAC3 */
|
2019-08-26 22:58:43 +02:00
|
|
|
int block_align, encoder_delay;
|
2017-05-13 23:54:09 +02:00
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
block_align = 0xc0 * channels;
|
2019-08-26 22:58:43 +02:00
|
|
|
encoder_delay = 1024 + 69*2; /* observed default */
|
|
|
|
vgmstream->num_samples = num_samples - encoder_delay;
|
2017-05-13 23:54:09 +02:00
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->codec_data = init_ffmpeg_atrac3_raw(sf, start_offset,stream_size, vgmstream->num_samples,vgmstream->channels,vgmstream->sample_rate, block_align, encoder_delay);
|
2017-05-13 23:54:09 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
2019-08-26 22:58:43 +02:00
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
2017-05-13 23:54:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
|
2017-05-13 23:54:09 +02:00
|
|
|
goto fail;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
if (is_separate && sh) close_streamfile(sh);
|
2017-05-13 23:54:09 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2021-06-20 10:54:00 +02:00
|
|
|
if (is_separate && sh) close_streamfile(sh);
|
2017-05-13 23:54:09 +02:00
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* .RXW - legacy fake ext/header for poorly split XWH+XWB files generated by old tools (incorrect header/chunk sizes) */
|
2021-06-20 10:54:00 +02:00
|
|
|
VGMSTREAM* init_vgmstream_rxws_badrip(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
|
|
|
int loop_flag=0, channels;
|
2017-05-13 13:01:16 +02:00
|
|
|
off_t start_offset;
|
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!check_extensions(sf,"rxw"))
|
|
|
|
goto fail;
|
2017-05-13 13:01:16 +02:00
|
|
|
|
|
|
|
/* check RXWS/FORM Header */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!((read_32bitBE(0x00,sf) == 0x52585753) &&
|
|
|
|
(read_32bitBE(0x10,sf) == 0x464F524D)))
|
2017-05-13 13:01:16 +02:00
|
|
|
goto fail;
|
|
|
|
|
2021-07-29 17:08:30 +02:00
|
|
|
loop_flag = (read_u32le(0x3C,sf)!=0xFFFFFFFF);
|
2021-06-20 10:54:00 +02:00
|
|
|
channels=2; /* Always stereo files */
|
2017-05-13 13:01:16 +02:00
|
|
|
|
2017-11-10 22:22:04 +01:00
|
|
|
/* build the VGMSTREAM */
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream = allocate_vgmstream(channels,loop_flag);
|
2017-05-13 13:01:16 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->sample_rate = read_32bitLE(0x2E,sf);
|
|
|
|
vgmstream->num_samples = (read_32bitLE(0x38,sf)*28/16)/2;
|
2017-05-13 13:01:16 +02:00
|
|
|
|
2017-11-10 22:22:04 +01:00
|
|
|
/* Get loop point values */
|
|
|
|
if(vgmstream->loop_flag) {
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->loop_start_sample = read_32bitLE(0x3C,sf)/16*14;
|
|
|
|
vgmstream->loop_end_sample = read_32bitLE(0x38,sf)/16*14;
|
2017-11-10 22:22:04 +01:00
|
|
|
}
|
2017-05-13 13:01:16 +02:00
|
|
|
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->interleave_block_size = read_32bitLE(0x1c,sf)+0x10;
|
2017-05-13 13:01:16 +02:00
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
2021-06-20 10:54:00 +02:00
|
|
|
vgmstream->meta_type = meta_RXWS;
|
2017-11-10 22:22:04 +01:00
|
|
|
start_offset = 0x40;
|
2017-05-13 13:01:16 +02:00
|
|
|
|
|
|
|
/* open the file for reading */
|
2021-06-20 10:54:00 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
|
2017-05-13 13:01:16 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|