2010-08-28 03:43:40 +02:00
|
|
|
#include "meta.h"
|
2018-03-27 23:24:24 +02:00
|
|
|
#include "../coding/coding.h"
|
2010-08-28 03:43:40 +02:00
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
static STREAMFILE* setup_jstm_streamfile(STREAMFILE *streamFile, off_t start_offset);
|
|
|
|
|
|
|
|
/* JSTM - from Tantei Jinguji Saburo - Kind of Blue (PS2) */
|
2010-08-28 03:43:40 +02:00
|
|
|
VGMSTREAM * init_vgmstream_ps2_jstm(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2018-03-27 23:24:24 +02:00
|
|
|
STREAMFILE *temp_streamFile = NULL;
|
|
|
|
off_t start_offset;
|
|
|
|
int loop_flag, channel_count;
|
2010-08-28 03:43:40 +02:00
|
|
|
|
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
/* checks */
|
|
|
|
/* .stm: original, .jstm: header id */
|
|
|
|
if (!check_extensions(streamFile, "stm,jstm"))
|
|
|
|
goto fail;
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x4A53544D) /* "JSTM" */
|
|
|
|
goto fail;
|
2010-08-28 03:43:40 +02:00
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
start_offset = 0x20;
|
|
|
|
channel_count = read_16bitLE(0x04,streamFile);
|
|
|
|
if (channel_count != read_16bitLE(0x06,streamFile)) /* 0x02, interleave? */
|
|
|
|
goto fail;
|
|
|
|
loop_flag = (read_32bitLE(0x14,streamFile) != -1);
|
2010-08-28 03:43:40 +02:00
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
/* build the VGMSTREAM */
|
2010-08-28 03:43:40 +02:00
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
vgmstream->sample_rate = read_32bitLE(0x08,streamFile);
|
|
|
|
vgmstream->num_samples = pcm_bytes_to_samples(read_32bitLE(0x0C,streamFile),channel_count,16);
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = pcm_bytes_to_samples(read_32bitLE(0x14,streamFile),channel_count,16);
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
2010-08-28 03:43:40 +02:00
|
|
|
|
|
|
|
vgmstream->meta_type = meta_PS2_JSTM;
|
2018-03-27 23:24:24 +02:00
|
|
|
vgmstream->coding_type = coding_PCM16LE;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x02;
|
2010-08-28 03:43:40 +02:00
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
temp_streamFile = setup_jstm_streamfile(streamFile, start_offset);
|
|
|
|
if (!temp_streamFile) goto fail;
|
|
|
|
|
|
|
|
if (!vgmstream_open_stream(vgmstream,temp_streamFile,start_offset))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
close_streamfile(temp_streamFile);
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_streamfile(temp_streamFile);
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
off_t start_offset;
|
|
|
|
} jstm_decryption_data;
|
|
|
|
|
|
|
|
static size_t jstm_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, jstm_decryption_data* data) {
|
|
|
|
size_t bytes_read;
|
|
|
|
int i;
|
2010-08-28 03:43:40 +02:00
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
bytes_read = streamfile->read(streamfile, dest, offset, length);
|
|
|
|
|
|
|
|
/* decrypt data (xor) */
|
|
|
|
for (i = 0; i < bytes_read; i++) {
|
|
|
|
if (offset+i >= data->start_offset) {
|
|
|
|
//VGM_LOG("xor %x to %x\n", dest[i], dest[i] ^ 0x5A);getchar();
|
|
|
|
dest[i] = dest[i] ^ 0x5A;
|
2010-08-28 03:43:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 23:24:24 +02:00
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
static STREAMFILE* setup_jstm_streamfile(STREAMFILE *streamFile, off_t start_offset) {
|
|
|
|
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
|
|
|
|
jstm_decryption_data io_data = {0};
|
|
|
|
size_t io_data_size = sizeof(jstm_decryption_data);
|
|
|
|
|
|
|
|
/* setup decryption */
|
|
|
|
io_data.start_offset = start_offset;
|
|
|
|
|
|
|
|
|
|
|
|
/* setup custom streamfile */
|
|
|
|
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, jstm_decryption_read);
|
|
|
|
if (!new_streamFile) goto fail;
|
|
|
|
temp_streamFile = new_streamFile;
|
|
|
|
|
|
|
|
return temp_streamFile;
|
2010-08-28 03:43:40 +02:00
|
|
|
|
|
|
|
fail:
|
2018-03-27 23:24:24 +02:00
|
|
|
close_streamfile(temp_streamFile);
|
2010-08-28 03:43:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|