2008-11-07 00:30:33 +01:00
|
|
|
#include "meta.h"
|
2017-10-27 18:50:06 +02:00
|
|
|
#include "../coding/coding.h"
|
2017-01-14 00:59:54 +01:00
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
|
|
|
|
static STREAMFILE* setup_bgw_atrac3_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, size_t frame_size, int channels);
|
|
|
|
|
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
/* BGW - from Final Fantasy XI (PC) music files */
|
2008-11-07 00:30:33 +01:00
|
|
|
VGMSTREAM * init_vgmstream_bgw(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2018-01-27 12:50:25 +01:00
|
|
|
STREAMFILE *temp_streamFile = NULL;
|
2017-10-27 18:50:06 +02:00
|
|
|
uint32_t codec, file_size, block_size, sample_rate, block_align;
|
2008-11-07 02:26:25 +01:00
|
|
|
int32_t loop_start;
|
2016-12-29 23:34:21 +01:00
|
|
|
off_t start_offset;
|
2008-11-07 00:30:33 +01:00
|
|
|
|
2017-01-02 15:24:10 +01:00
|
|
|
int channel_count, loop_flag = 0;
|
2008-11-07 00:30:33 +01:00
|
|
|
|
2016-12-29 23:34:21 +01:00
|
|
|
/* check extensions */
|
2017-01-14 00:59:54 +01:00
|
|
|
if ( !check_extensions(streamFile, "bgw") )
|
2016-12-29 23:34:21 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x42474d53 || /* "BGMS" */
|
|
|
|
read_32bitBE(0x04,streamFile) != 0x74726561 || /* "trea" */
|
|
|
|
read_32bitBE(0x08,streamFile) != 0x6d000000 ) /* "m\0\0\0" */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
codec = read_32bitLE(0x0c,streamFile);
|
2017-10-27 18:50:06 +02:00
|
|
|
file_size = read_32bitLE(0x10,streamFile);
|
2019-08-26 22:58:43 +02:00
|
|
|
/* file_id = read_32bitLE(0x14,streamFile); */
|
2017-10-27 18:50:06 +02:00
|
|
|
block_size = read_32bitLE(0x18,streamFile);
|
2016-12-29 23:34:21 +01:00
|
|
|
loop_start = read_32bitLE(0x1c,streamFile);
|
2017-10-27 18:50:06 +02:00
|
|
|
sample_rate = (read_32bitLE(0x20,streamFile) + read_32bitLE(0x24,streamFile)) & 0x7FFFFFFF; /* bizarrely obfuscated sample rate */
|
2016-12-29 23:34:21 +01:00
|
|
|
start_offset = read_32bitLE(0x28,streamFile);
|
2019-08-26 22:58:43 +02:00
|
|
|
/* 0x2c: unk (vol?) */
|
|
|
|
/* 0x2d: unk (0x10?) */
|
2016-12-29 23:34:21 +01:00
|
|
|
channel_count = read_8bit(0x2e,streamFile);
|
2018-08-11 17:54:42 +02:00
|
|
|
block_align = (uint8_t)read_8bit(0x2f,streamFile);
|
2008-11-07 00:30:33 +01:00
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
if (file_size != get_streamfile_size(streamFile))
|
2008-11-07 00:30:33 +01:00
|
|
|
goto fail;
|
|
|
|
|
2008-11-07 02:26:25 +01:00
|
|
|
loop_flag = (loop_start > 0);
|
2016-12-29 23:34:21 +01:00
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
/* build the VGMSTREAM */
|
2008-11-07 00:30:33 +01:00
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2016-12-29 23:34:21 +01:00
|
|
|
vgmstream->meta_type = meta_FFXI_BGW;
|
|
|
|
vgmstream->sample_rate = sample_rate;
|
2008-11-07 00:30:33 +01:00
|
|
|
|
2016-12-29 23:34:21 +01:00
|
|
|
switch (codec) {
|
2017-01-02 15:24:10 +01:00
|
|
|
case 0: /* PS ADPCM */
|
2017-01-08 01:09:20 +01:00
|
|
|
vgmstream->coding_type = coding_PSX_cfg;
|
2016-12-29 23:34:21 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = (block_align / 2) + 1; /* half, even if channels = 1 */
|
2017-01-02 15:24:10 +01:00
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
vgmstream->num_samples = block_size * block_align;
|
2017-01-02 15:24:10 +01:00
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = (loop_start-1) * block_align;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
2016-12-29 23:34:21 +01:00
|
|
|
break;
|
2017-10-27 18:50:06 +02:00
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 3: { /* ATRAC3 (encrypted) */
|
2018-01-27 12:50:25 +01:00
|
|
|
size_t data_size = file_size - start_offset;
|
2019-08-26 22:58:43 +02:00
|
|
|
int encoder_delay, block_align;
|
2017-10-27 18:50:06 +02:00
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
encoder_delay = 1024*2 + 69*2; /* observed value, all files start at +2200 (PS-ADPCM also starts around 50-150 samples in) */
|
|
|
|
block_align = 0xC0 * vgmstream->channels; /* 0x00 in header */
|
|
|
|
vgmstream->num_samples = block_size - encoder_delay; /* atrac3_bytes_to_samples gives block_size */
|
2017-10-27 18:50:06 +02:00
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
temp_streamFile = setup_bgw_atrac3_streamfile(streamFile, start_offset,data_size, 0xC0,channel_count);
|
|
|
|
if (!temp_streamFile) goto fail;
|
2017-10-27 18:50:06 +02:00
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
vgmstream->codec_data = init_ffmpeg_atrac3_raw(temp_streamFile, 0x00,data_size, vgmstream->num_samples,vgmstream->channels,vgmstream->sample_rate, block_align, encoder_delay);
|
2017-10-27 18:50:06 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
2018-01-27 12:50:25 +01:00
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = loop_start - encoder_delay;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
close_streamfile(temp_streamFile);
|
2017-10-27 18:50:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-29 23:34:21 +01:00
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-11-07 00:30:33 +01:00
|
|
|
|
|
|
|
|
2017-01-14 00:59:54 +01:00
|
|
|
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
|
2016-12-29 23:34:21 +01:00
|
|
|
goto fail;
|
2008-11-07 00:30:33 +01:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2018-01-27 12:50:25 +01:00
|
|
|
close_streamfile(temp_streamFile);
|
2016-12-29 23:34:21 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2008-11-07 00:30:33 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-07 02:47:39 +01:00
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
|
2017-01-14 00:59:54 +01:00
|
|
|
/* SPW (SEWave) - from PlayOnline viewer for Final Fantasy XI (PC) */
|
2008-11-07 02:47:39 +01:00
|
|
|
VGMSTREAM * init_vgmstream_spw(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2019-04-21 01:17:03 +02:00
|
|
|
STREAMFILE *temp_streamFile = NULL;
|
2017-10-27 18:50:06 +02:00
|
|
|
uint32_t codec, file_size, block_size, sample_rate, block_align;
|
2017-01-02 15:24:10 +01:00
|
|
|
int32_t loop_start;
|
2008-11-07 02:47:39 +01:00
|
|
|
off_t start_offset;
|
|
|
|
|
2017-01-02 15:24:10 +01:00
|
|
|
int channel_count, loop_flag = 0;
|
2008-11-07 02:47:39 +01:00
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
/* check extensions */
|
|
|
|
if ( !check_extensions(streamFile, "spw") )
|
2016-12-29 23:34:21 +01:00
|
|
|
goto fail;
|
2008-11-07 02:47:39 +01:00
|
|
|
|
2017-01-02 15:24:10 +01:00
|
|
|
/* check header */
|
|
|
|
if (read_32bitBE(0,streamFile) != 0x53655761 || /* "SeWa" */
|
|
|
|
read_32bitBE(4,streamFile) != 0x76650000) /* "ve\0\0" */
|
|
|
|
goto fail;
|
2008-11-07 02:47:39 +01:00
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
file_size = read_32bitLE(0x08,streamFile);
|
2017-01-02 15:24:10 +01:00
|
|
|
codec = read_32bitLE(0x0c,streamFile);
|
2019-08-26 22:58:43 +02:00
|
|
|
/* file_id = read_32bitLE(0x10,streamFile);*/
|
2017-10-27 18:50:06 +02:00
|
|
|
block_size = read_32bitLE(0x14,streamFile);
|
2008-11-07 02:47:39 +01:00
|
|
|
loop_start = read_32bitLE(0x18,streamFile);
|
2017-10-27 18:50:06 +02:00
|
|
|
sample_rate = (read_32bitLE(0x1c,streamFile) + read_32bitLE(0x20,streamFile)) & 0x7FFFFFFF; /* bizarrely obfuscated sample rate */
|
2017-01-02 15:24:10 +01:00
|
|
|
start_offset = read_32bitLE(0x24,streamFile);
|
2019-08-26 22:58:43 +02:00
|
|
|
/* 0x2c: unk (0x00?) */
|
|
|
|
/* 0x2d: unk (0x00/01?) */
|
2017-01-02 15:24:10 +01:00
|
|
|
channel_count = read_8bit(0x2a,streamFile);
|
2019-04-21 01:17:03 +02:00
|
|
|
/*0x2b: unk (0x01 when PCM, 0x10 when VAG?) */
|
|
|
|
block_align = read_8bit(0x2c,streamFile);
|
2017-01-02 15:24:10 +01:00
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
if (file_size != get_streamfile_size(streamFile))
|
2017-01-02 15:24:10 +01:00
|
|
|
goto fail;
|
|
|
|
|
2008-11-07 02:47:39 +01:00
|
|
|
loop_flag = (loop_start > 0);
|
2017-01-02 15:24:10 +01:00
|
|
|
|
2018-01-27 12:50:25 +01:00
|
|
|
/* build the VGMSTREAM */
|
2008-11-07 02:47:39 +01:00
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->meta_type = meta_FFXI_SPW;
|
2017-01-02 15:24:10 +01:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
|
|
|
|
switch (codec) {
|
|
|
|
case 0: /* PS ADPCM */
|
2017-01-08 01:09:20 +01:00
|
|
|
vgmstream->coding_type = coding_PSX_cfg;
|
2017-01-02 15:24:10 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = (block_align / 2) + 1; /* half, even if channels = 1 */
|
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
vgmstream->num_samples = block_size * block_align;
|
2017-01-02 15:24:10 +01:00
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = (loop_start-1) * block_align;;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2018-01-27 12:50:25 +01:00
|
|
|
|
2017-01-02 15:24:10 +01:00
|
|
|
case 1: /* PCM */
|
|
|
|
vgmstream->coding_type = coding_PCM16LE;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x02;
|
|
|
|
|
2017-10-27 18:50:06 +02:00
|
|
|
vgmstream->num_samples = block_size;
|
2017-01-02 15:24:10 +01:00
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = (loop_start-1);
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
2018-01-27 12:50:25 +01:00
|
|
|
|
2017-01-02 15:24:10 +01:00
|
|
|
break;
|
2018-01-27 12:50:25 +01:00
|
|
|
|
2019-04-21 01:17:03 +02:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 3: { /* ATRAC3 (encrypted) */
|
|
|
|
size_t data_size = file_size - start_offset;
|
2019-08-26 22:58:43 +02:00
|
|
|
int encoder_delay, block_align;
|
2019-04-21 01:17:03 +02:00
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
encoder_delay = 1024*2 + 69*2; /* observed value, all files start at +2200 (PS-ADPCM also starts around 50-150 samples in) */
|
|
|
|
block_align = 0xC0 * vgmstream->channels; /* 0x00 in header */
|
|
|
|
vgmstream->num_samples = block_size - encoder_delay; /* atrac3_bytes_to_samples gives block_size */
|
2019-04-21 01:17:03 +02:00
|
|
|
|
|
|
|
temp_streamFile = setup_bgw_atrac3_streamfile(streamFile, start_offset,data_size, 0xC0,channel_count);
|
|
|
|
if (!temp_streamFile) goto fail;
|
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
vgmstream->codec_data = init_ffmpeg_atrac3_raw(temp_streamFile, 0x00,data_size, vgmstream->num_samples,vgmstream->channels,vgmstream->sample_rate, block_align, encoder_delay);
|
2019-04-21 01:17:03 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = loop_start - encoder_delay;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:17:03 +02:00
|
|
|
close_streamfile(temp_streamFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-02 15:24:10 +01:00
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-11-07 02:47:39 +01:00
|
|
|
|
|
|
|
|
2016-12-29 23:34:21 +01:00
|
|
|
/* open the file for reading */
|
2017-01-14 00:59:54 +01:00
|
|
|
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
|
2016-12-29 23:34:21 +01:00
|
|
|
goto fail;
|
2008-11-07 02:47:39 +01:00
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2019-04-21 01:17:03 +02:00
|
|
|
close_streamfile(temp_streamFile);
|
2017-01-02 15:24:10 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2008-11-07 02:47:39 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-27 12:50:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define BGW_KEY_MAX (0xC0*2)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t key[BGW_KEY_MAX];
|
|
|
|
size_t key_size;
|
|
|
|
} bgw_decryption_data;
|
|
|
|
|
|
|
|
/* Encrypted ATRAC3 info from Moogle Toolbox (https://sourceforge.net/projects/mogbox/) */
|
|
|
|
static size_t bgw_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, bgw_decryption_data* data) {
|
|
|
|
size_t bytes_read;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bytes_read = streamfile->read(streamfile, dest, offset, length);
|
|
|
|
|
|
|
|
/* decrypt data (xor) */
|
|
|
|
for (i = 0; i < bytes_read; i++) {
|
|
|
|
dest[i] ^= data->key[(offset + i) % data->key_size];
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
static STREAMFILE* setup_bgw_atrac3_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, size_t frame_size, int channels) {
|
|
|
|
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
|
|
|
|
bgw_decryption_data io_data = {0};
|
|
|
|
size_t io_data_size = sizeof(bgw_decryption_data);
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
/* setup decryption with key (first frame + modified channel header) */
|
|
|
|
if (frame_size*channels == 0 || frame_size*channels > BGW_KEY_MAX) goto fail;
|
|
|
|
|
|
|
|
io_data.key_size = read_streamfile(io_data.key, subfile_offset, frame_size*channels, streamFile);
|
|
|
|
for (ch = 0; ch < channels; ch++) {
|
|
|
|
uint32_t xor = get_32bitBE(io_data.key + frame_size*ch);
|
|
|
|
put_32bitBE(io_data.key + frame_size*ch, xor ^ 0xA0024E9F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup subfile */
|
|
|
|
new_streamFile = open_wrap_streamfile(streamFile);
|
|
|
|
if (!new_streamFile) goto fail;
|
|
|
|
temp_streamFile = new_streamFile;
|
|
|
|
|
|
|
|
new_streamFile = open_clamp_streamfile(temp_streamFile, subfile_offset,subfile_size);
|
|
|
|
if (!new_streamFile) goto fail;
|
|
|
|
temp_streamFile = new_streamFile;
|
|
|
|
|
2018-03-30 18:10:23 +02:00
|
|
|
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, bgw_decryption_read,NULL);
|
2018-01-27 12:50:25 +01:00
|
|
|
if (!new_streamFile) goto fail;
|
|
|
|
temp_streamFile = new_streamFile;
|
|
|
|
|
|
|
|
return temp_streamFile;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_streamfile(temp_streamFile);
|
|
|
|
return NULL;
|
|
|
|
}
|