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) */
|
|
|
|
VGMSTREAM * init_vgmstream_ps2_rxws(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
STREAMFILE * streamHeader = NULL;
|
2017-08-12 11:46:28 +02:00
|
|
|
off_t start_offset, chunk_offset, name_offset = 0;
|
|
|
|
size_t data_size, chunk_size;
|
2017-09-30 01:29:54 +02:00
|
|
|
int loop_flag = 0, channel_count, is_separate = false, type, sample_rate;
|
2017-05-13 23:54:09 +02:00
|
|
|
int32_t loop_start, loop_end;
|
2017-08-12 11:46:28 +02:00
|
|
|
int total_streams, target_stream = streamFile->stream_index;
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* check extensions */
|
2017-08-12 11:46:28 +02:00
|
|
|
/* .xws: header and data, .xwh+xwb: header + data (.bin+dat are also found in Wild Arms 4/5) */
|
2017-05-13 23:54:09 +02:00
|
|
|
if (!check_extensions(streamFile,"xws,xwb")) goto fail;
|
|
|
|
is_separate = check_extensions(streamFile,"xwb");
|
|
|
|
|
|
|
|
/* xwh+xwb: use xwh as header; otherwise use the current file */
|
|
|
|
if (is_separate) {
|
|
|
|
/* extra check to avoid hijacking Microsoft's XWB */
|
|
|
|
if ((read_32bitBE(0x00,streamFile) == 0x57424E44) || /* "WBND" (LE) */
|
|
|
|
(read_32bitBE(0x00,streamFile) == 0x444E4257)) /* "DNBW" (BE) */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
streamHeader = open_stream_ext(streamFile, "xwh");
|
|
|
|
if (!streamHeader) goto fail;
|
|
|
|
} else {
|
|
|
|
streamHeader = streamFile;
|
|
|
|
}
|
|
|
|
if (read_32bitBE(0x00,streamHeader) != 0x52585753) /* "RXWS" */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* file size (just the .xwh/xws) */
|
|
|
|
if (read_32bitLE(0x04,streamHeader)+0x10 != get_streamfile_size(streamHeader))
|
|
|
|
goto fail;
|
2017-08-12 11:46:28 +02:00
|
|
|
/* 0x08(4): version (0x100/0x200), 0x0C: null */
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* typical chunks: FORM, FTXT, MARK, BODY (for .xws) */
|
|
|
|
if (read_32bitBE(0x10,streamHeader) != 0x464F524D) /* "FORM", main header (always first) */
|
|
|
|
goto fail;
|
2017-08-12 11:46:28 +02:00
|
|
|
chunk_size = read_32bitLE(0x10+0x04,streamHeader); /* size - 0x10 */
|
|
|
|
/* 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 */
|
|
|
|
total_streams = read_32bitLE(chunk_offset+0x00,streamHeader);
|
|
|
|
if (target_stream == 0) target_stream = 1;
|
|
|
|
if (target_stream < 0 || target_stream > total_streams || total_streams < 1) goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
/* read stream header */
|
|
|
|
{
|
|
|
|
off_t header_offset = chunk_offset + 0x4 + 0x1c * (target_stream-1); /* position in FORM */
|
|
|
|
off_t stream_offset, next_stream_offset, data_offset = 0;
|
|
|
|
|
|
|
|
type = read_8bit(header_offset+0x00, streamHeader);
|
|
|
|
/* 0x01(1): unknown (always 0x1c), 0x02(2): flags? (usually 8002/0002, & 0x01 if looped) */
|
|
|
|
/* 0x04(4): vol/pan stuff? (0x00007F7F), 0x08(1): null?, 0x0c(4): null? */
|
|
|
|
channel_count = read_8bit(header_offset+0x09, streamHeader);
|
|
|
|
sample_rate = (uint16_t)read_16bitLE(header_offset+0x0a,streamHeader);
|
|
|
|
stream_offset = read_32bitLE(header_offset+0x10,streamHeader);
|
|
|
|
loop_end = read_32bitLE(header_offset+0x14,streamHeader);
|
|
|
|
loop_start = read_32bitLE(header_offset+0x18,streamHeader);
|
|
|
|
loop_flag = (loop_start != 0xFFFFFFFF);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
while (current_chunk < get_streamfile_size(streamFile)) {
|
|
|
|
if (read_32bitBE(current_chunk,streamFile) == 0x424F4459) { /* "BODY" chunk_type */
|
|
|
|
data_offset = 0x10 + current_chunk;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_chunk += 0x10 + read_32bitLE(current_chunk+4,streamFile);
|
|
|
|
}
|
|
|
|
if (!data_offset) goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_stream == total_streams) {
|
|
|
|
next_stream_offset = data_offset + get_streamfile_size(is_separate ? streamFile : streamHeader);
|
|
|
|
} else {
|
|
|
|
off_t next_header_offset = chunk_offset + 0x4 + 0x1c * (target_stream);
|
|
|
|
next_stream_offset = read_32bitLE(next_header_offset+0x10,streamHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
data_size = next_stream_offset - stream_offset;
|
|
|
|
start_offset = data_offset + stream_offset;
|
|
|
|
}
|
|
|
|
|
2017-08-12 11:46:28 +02:00
|
|
|
/* get stream name (always follows FORM) */
|
|
|
|
if (read_32bitBE(0x10+0x10 + chunk_size,streamHeader) == 0x46545854) { /* "FTXT" */
|
|
|
|
chunk_offset = 0x10+0x10 + chunk_size + 0x10;
|
|
|
|
if (read_32bitLE(chunk_offset+0x00,streamHeader) == total_streams) {
|
|
|
|
name_offset = chunk_offset + read_32bitLE(chunk_offset+0x04 + (target_stream-1)*0x04,streamHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 23:54:09 +02:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
vgmstream->num_streams = total_streams;
|
|
|
|
vgmstream->meta_type = meta_PS2_RXWS;
|
2017-08-12 11:46:28 +02:00
|
|
|
if (name_offset)
|
|
|
|
read_string(vgmstream->stream_name,STREAM_NAME_SIZE, name_offset,streamHeader);
|
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;
|
|
|
|
|
2017-08-12 11:46:28 +02:00
|
|
|
vgmstream->num_samples = ps_bytes_to_samples(loop_end, channel_count);
|
|
|
|
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start, channel_count);
|
|
|
|
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end, channel_count);
|
2017-05-13 23:54:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x01: /* PCM */
|
|
|
|
vgmstream->coding_type = coding_PCM16LE;
|
|
|
|
vgmstream->layout_type = channel_count==1 ? layout_none : layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x2;
|
|
|
|
|
|
|
|
vgmstream->num_samples = pcm_bytes_to_samples(loop_end, channel_count, 16);
|
|
|
|
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channel_count, 16);
|
|
|
|
vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channel_count, 16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 0x02: { /* ATRAC3 */
|
|
|
|
uint8_t buf[0x100];
|
|
|
|
int32_t bytes, block_size, encoder_delay, joint_stereo;
|
|
|
|
|
|
|
|
block_size = 0xc0 * channel_count;
|
|
|
|
joint_stereo = 0;
|
|
|
|
encoder_delay = 0x0;
|
|
|
|
|
|
|
|
bytes = ffmpeg_make_riff_atrac3(buf, 0x100, vgmstream->num_samples, data_size, vgmstream->channels, vgmstream->sample_rate, block_size, joint_stereo, encoder_delay);
|
|
|
|
if (bytes <= 0) goto fail;
|
|
|
|
|
|
|
|
vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,data_size);
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
|
|
|
vgmstream->num_samples = loop_end;
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
|
|
|
vgmstream->loop_end_sample = loop_end;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (is_separate && streamHeader) close_streamfile(streamHeader);
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (is_separate && streamHeader) close_streamfile(streamHeader);
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* .RXW - legacy fake ext/header for poorly split XWH+XWB files generated by old tools (incorrect header/chunk sizes) */
|
2017-05-13 13:01:16 +02:00
|
|
|
VGMSTREAM * init_vgmstream_ps2_rxw(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
int loop_flag=0, channel_count;
|
|
|
|
off_t start_offset;
|
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
|
|
|
if (!check_extensions(streamFile,"rxw")) goto fail;
|
|
|
|
|
|
|
|
/* check RXWS/FORM Header */
|
|
|
|
if (!((read_32bitBE(0x00,streamFile) == 0x52585753) &&
|
|
|
|
(read_32bitBE(0x10,streamFile) == 0x464F524D)))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
loop_flag = (read_32bitLE(0x3C,streamFile)!=0xFFFFFFFF);
|
|
|
|
channel_count=2; /* Always stereo files */
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = read_32bitLE(0x2E,streamFile);
|
|
|
|
vgmstream->num_samples = (read_32bitLE(0x38,streamFile)*28/16)/2;
|
|
|
|
|
|
|
|
/* Get loop point values */
|
|
|
|
if(vgmstream->loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = read_32bitLE(0x3C,streamFile)/16*14;
|
|
|
|
vgmstream->loop_end_sample = read_32bitLE(0x38,streamFile)/16*14;
|
|
|
|
}
|
|
|
|
|
|
|
|
vgmstream->interleave_block_size = read_32bitLE(0x1c,streamFile)+0x10;
|
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->meta_type = meta_PS2_RXWS;
|
|
|
|
start_offset = 0x40;
|
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|