Add encrypted ATRAC3 BGW [Final Fantasy XI PC]

This commit is contained in:
bnnm 2017-10-27 18:50:06 +02:00
parent 56586b84dc
commit d09dc9739b
5 changed files with 122 additions and 20 deletions

View File

@ -98,6 +98,8 @@ static void convert_audio(sample *outbuf, const uint8_t *inbuf, int sampleCount,
* Fortunately seek_frame_generic can use an index to find the correct position. This function reads the
* first frame/packet and sets up index to timestamp 0. This ensures faulty demuxers will seek to 0 correctly.
* Some formats may not seek to 0 even with this, though.
*
* todo: some formats don't work with the current index values
*/
static int init_seek(ffmpeg_codec_data * data) {
int ret, ts_index, found_first = 0;
@ -220,6 +222,7 @@ static int ffmpeg_read(void *opaque, uint8_t *buf, int buf_size) {
switch(data->config.type) {
case FFMPEG_EA_XMA: ret = ffmpeg_custom_read_eaxma(data, buf, buf_size); break;
case FFMPEG_SWITCH_OPUS: ret = ffmpeg_custom_read_switch_opus(data, buf, buf_size); break;
case FFMPEG_BGW_ATRAC3: ret = ffmpeg_custom_read_bgw_atrac3(data, buf, buf_size); break;
//case FFMPEG_EA_SCHL: ret = ffmpeg_custom_read_ea_schl(data, buf, buf_size); break;
//case FFMPEG_SFH: ret = ffmpeg_custom_read_sfh(data, buf, buf_size); break;
default: ret = ffmpeg_custom_read_standard(data, buf, buf_size); break;
@ -228,6 +231,7 @@ static int ffmpeg_read(void *opaque, uint8_t *buf, int buf_size) {
//data->real_offset = ; /* must be updated in function */
//;VGM_LOG("AVIO read done: ret=%x, r_off=%"PRIx64", v_off=%"PRIx64"\n", ret + max_to_copy, data->real_offset, data->virtual_offset);fflush(stdout);
//;VGM_LOGB((buf - max_to_copy),ret + max_to_copy,0);
return ret + max_to_copy;
}
@ -287,6 +291,7 @@ static int64_t ffmpeg_seek(void *opaque, int64_t offset, int whence) {
switch(data->config.type) {
case FFMPEG_EA_XMA: offset = ffmpeg_custom_seek_eaxma(data, offset); break;
case FFMPEG_SWITCH_OPUS: offset = ffmpeg_custom_seek_switch_opus(data, offset); break;
case FFMPEG_BGW_ATRAC3: offset = ffmpeg_custom_seek_bgw_atrac3(data, offset); break;
//case FFMPEG_EA_SCHL: offset = ffmpeg_custom_seek_ea_schl(data, offset); break;
//case FFMPEG_SFH: offset = ffmpeg_custom_seek_sfh(data, offset); break;
default: offset = ffmpeg_custom_seek_standard(data, offset); break;
@ -304,6 +309,7 @@ static int64_t ffmpeg_size(ffmpeg_codec_data * data) {
switch(data->config.type) {
case FFMPEG_EA_XMA: bytes = ffmpeg_custom_size_eaxma(data); break;
case FFMPEG_SWITCH_OPUS: bytes = ffmpeg_custom_size_switch_opus(data); break;
case FFMPEG_BGW_ATRAC3: bytes = ffmpeg_custom_size_bgw_atrac3(data); break;
//case FFMPEG_EA_SCHL: bytes = ffmpeg_custom_size_ea_schl(data); break;
//case FFMPEG_SFH: bytes = ffmpeg_custom_size_sfh(data); break;
default: bytes = ffmpeg_custom_size_standard(data); break;
@ -800,6 +806,9 @@ void free_ffmpeg(ffmpeg_codec_data *data) {
close_streamfile(data->streamfile);
data->streamfile = NULL;
}
if (data->config.key) {
free(data->config.key);
}
free(data);
}

View File

@ -31,6 +31,10 @@ int ffmpeg_custom_read_switch_opus(ffmpeg_codec_data *data, uint8_t *buf, int bu
int64_t ffmpeg_custom_seek_switch_opus(ffmpeg_codec_data *data, int64_t virtual_offset);
int64_t ffmpeg_custom_size_switch_opus(ffmpeg_codec_data *data);
int ffmpeg_custom_read_bgw_atrac3(ffmpeg_codec_data *data, uint8_t *buf, int buf_size);
int64_t ffmpeg_custom_seek_bgw_atrac3(ffmpeg_codec_data *data, int64_t virtual_offset);
int64_t ffmpeg_custom_size_bgw_atrac3(ffmpeg_codec_data *data);
//int ffmpeg_custom_read_ea_schl(ffmpeg_codec_data *data, uint8_t *buf, int buf_size);
//int64_t ffmpeg_custom_seek_ea_schl(ffmpeg_codec_data *data, int64_t virtual_offset);
//int64_t ffmpeg_custom_size_ea_schl(ffmpeg_codec_data *data);

View File

@ -0,0 +1,58 @@
#if 1
#include "coding.h"
#include "ffmpeg_decoder_utils.h"
#ifdef VGM_USE_FFMPEG
#define BGM_ATRAC3_FRAME_SIZE 0xC0
/**
* Encrypted ATRAC3 used in BGW (Final Fantasy XI PC).
* Info from Moogle Toolbox: https://sourceforge.net/projects/mogbox/
*/
int ffmpeg_custom_read_bgw_atrac3(ffmpeg_codec_data *data, uint8_t *buf, int buf_size) {
int i, ch;
size_t bytes;
size_t block_align = BGM_ATRAC3_FRAME_SIZE * data->config.channels;
/* init key: first frame + modified channel header */
if (data->config.key == NULL) {
data->config.key = malloc(block_align);
if (!data->config.key) return 0;
read_streamfile(data->config.key, data->real_start, block_align, data->streamfile);
for (ch = 0; ch < data->config.channels; ch++) {
uint32_t xor = get_32bitBE(data->config.key + ch*BGM_ATRAC3_FRAME_SIZE);
put_32bitBE(data->config.key + ch*BGM_ATRAC3_FRAME_SIZE, xor ^ 0xA0024E9F);
}
}
/* read normally and unXOR the data */
bytes = read_streamfile(buf, data->real_offset, buf_size, data->streamfile);
for (i = 0; i < bytes; i++) {
int key_pos = (data->real_offset - data->real_start + i) % block_align;
buf[i] = buf[i] ^ data->config.key[key_pos];
}
data->real_offset += bytes;
return bytes;
}
int64_t ffmpeg_custom_seek_bgw_atrac3(ffmpeg_codec_data *data, int64_t virtual_offset) {
int64_t seek_virtual_offset = virtual_offset - data->header_size;
data->real_offset = data->real_start + seek_virtual_offset;
return virtual_offset;
}
int64_t ffmpeg_custom_size_bgw_atrac3(ffmpeg_codec_data *data) {
return data->real_size + data->header_size;
}
#endif
#endif

View File

@ -1,12 +1,11 @@
#include "meta.h"
#include "../coding/coding.h"
/* BGW - from Final Fantasy XI (PC) music files
* Some info from POLUtils */
/* BGW - from Final Fantasy XI (PC) music files */
VGMSTREAM * init_vgmstream_bgw(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
uint32_t codec, filesize, blocksize, sample_rate;
uint32_t codec, file_size, block_size, sample_rate, block_align;
int32_t loop_start;
uint8_t block_align;
off_t start_offset;
int channel_count, loop_flag = 0;
@ -22,11 +21,11 @@ VGMSTREAM * init_vgmstream_bgw(STREAMFILE *streamFile) {
goto fail;
codec = read_32bitLE(0x0c,streamFile);
filesize = read_32bitLE(0x10,streamFile);
file_size = read_32bitLE(0x10,streamFile);
/*file_id = read_32bitLE(0x14,streamFile);*/
blocksize = read_32bitLE(0x18,streamFile);
block_size = read_32bitLE(0x18,streamFile);
loop_start = read_32bitLE(0x1c,streamFile);
sample_rate = (read_32bitLE(0x20,streamFile) + read_32bitLE(0x24,streamFile)) & 0xFFFFFFFF; /* bizarrely obfuscated sample rate */
sample_rate = (read_32bitLE(0x20,streamFile) + read_32bitLE(0x24,streamFile)) & 0x7FFFFFFF; /* bizarrely obfuscated sample rate */
start_offset = read_32bitLE(0x28,streamFile);
/*0x2c: unk (vol?) */
/*0x2d: unk (0x10?) */
@ -35,7 +34,7 @@ VGMSTREAM * init_vgmstream_bgw(STREAMFILE *streamFile) {
/* check file size with header value */
if (filesize != get_streamfile_size(streamFile))
if (file_size != get_streamfile_size(streamFile))
goto fail;
loop_flag = (loop_start > 0);
@ -54,15 +53,46 @@ VGMSTREAM * init_vgmstream_bgw(STREAMFILE *streamFile) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = (block_align / 2) + 1; /* half, even if channels = 1 */
vgmstream->num_samples = blocksize * block_align;
vgmstream->num_samples = block_size * block_align;
if (loop_flag) {
vgmstream->loop_start_sample = (loop_start-1) * block_align;
vgmstream->loop_end_sample = vgmstream->num_samples;
}
break;
case 3: /* ATRAC3 (encrypted) */
#ifdef VGM_USE_FFMPEG
case 3: { /* ATRAC3 (encrypted) */
uint8_t buf[0x100];
int bytes, joint_stereo, skip_samples;
ffmpeg_custom_config cfg;
vgmstream->num_samples = block_size; /* atrac3_bytes_to_samples gives the same value */
if (loop_flag) {
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = vgmstream->num_samples;
}
block_align = 0xC0 * vgmstream->channels; /* 0x00 in header */
joint_stereo = 0;
skip_samples = 0;
bytes = ffmpeg_make_riff_atrac3(buf, 0x100, vgmstream->num_samples, file_size - start_offset, vgmstream->channels, vgmstream->sample_rate, block_align, joint_stereo, skip_samples);
if (bytes <= 0) goto fail;
memset(&cfg, 0, sizeof(ffmpeg_custom_config));
cfg.type = FFMPEG_BGW_ATRAC3;
cfg.channels = vgmstream->channels;
vgmstream->codec_data = init_ffmpeg_config(streamFile, buf,bytes, start_offset,file_size - start_offset, &cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
break;
}
#endif
default:
goto fail;
}
@ -82,9 +112,8 @@ fail:
/* SPW (SEWave) - from PlayOnline viewer for Final Fantasy XI (PC) */
VGMSTREAM * init_vgmstream_spw(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
uint32_t codec, filesize, blocksize, sample_rate;
uint32_t codec, file_size, block_size, sample_rate, block_align;
int32_t loop_start;
uint8_t block_align;
off_t start_offset;
int channel_count, loop_flag = 0;
@ -102,12 +131,12 @@ VGMSTREAM * init_vgmstream_spw(STREAMFILE *streamFile) {
if (read_32bitLE(0x8,streamFile) != get_streamfile_size(streamFile))
goto fail;
filesize = read_32bitLE(0x08,streamFile);
file_size = read_32bitLE(0x08,streamFile);
codec = read_32bitLE(0x0c,streamFile);
/*file_id = read_32bitLE(0x10,streamFile);*/
blocksize = read_32bitLE(0x14,streamFile);
block_size = read_32bitLE(0x14,streamFile);
loop_start = read_32bitLE(0x18,streamFile);
sample_rate = (read_32bitLE(0x1c,streamFile) + read_32bitLE(0x20,streamFile)) & 0xFFFFFFFF; /* bizarrely obfuscated sample rate */
sample_rate = (read_32bitLE(0x1c,streamFile) + read_32bitLE(0x20,streamFile)) & 0x7FFFFFFF; /* bizarrely obfuscated sample rate */
start_offset = read_32bitLE(0x24,streamFile);
/*0x2c: unk (0x00?) */
/*0x2d: unk (0x00/01?) */
@ -116,7 +145,7 @@ VGMSTREAM * init_vgmstream_spw(STREAMFILE *streamFile) {
/*0x2c: unk (0x01 when PCM, 0x10 when VAG?) */
/* check file size with header value */
if (filesize != get_streamfile_size(streamFile))
if (file_size != get_streamfile_size(streamFile))
goto fail;
loop_flag = (loop_start > 0);
@ -135,7 +164,7 @@ VGMSTREAM * init_vgmstream_spw(STREAMFILE *streamFile) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = (block_align / 2) + 1; /* half, even if channels = 1 */
vgmstream->num_samples = blocksize * block_align;
vgmstream->num_samples = block_size * block_align;
if (loop_flag) {
vgmstream->loop_start_sample = (loop_start-1) * block_align;;
vgmstream->loop_end_sample = vgmstream->num_samples;
@ -148,7 +177,7 @@ VGMSTREAM * init_vgmstream_spw(STREAMFILE *streamFile) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x02;
vgmstream->num_samples = blocksize;
vgmstream->num_samples = block_size;
if (loop_flag) {
vgmstream->loop_start_sample = (loop_start-1);
vgmstream->loop_end_sample = vgmstream->num_samples;

View File

@ -1033,6 +1033,7 @@ typedef enum {
FFMPEG_STANDARD, /* default FFmpeg */
FFMPEG_SWITCH_OPUS, /* Opus without Ogg layer */
FFMPEG_EA_XMA, /* XMA with padding removed and custom streams in SNS blocks */
FFMPEG_BGW_ATRAC3, /* Encrypted raw ATRAC3 */
//FFMPEG_EA_SCHL, /* Normal header+data (ex. ATRAC3) in SCxx blocks */
//FFMPEG_SFH, /* ATRAC3plus header+data in SFH blocks */
//FFMPEG_AWC_XMA, /* XMA data in AWC blocks, 1 streams per channel */
@ -1050,6 +1051,7 @@ typedef struct {
/* internal sequences, when needed */
int sequence;
int samples_done;
uint8_t * key;
} ffmpeg_custom_config;
typedef struct {
@ -1061,7 +1063,7 @@ typedef struct {
uint64_t real_size; // max size within the streamfile
uint64_t virtual_offset; // computed offset FFmpeg sees (including fake header)
uint64_t virtual_size; // computed size FFmpeg sees (including fake header)
uint64_t virtual_base; // info/base virtual_offset equivalent to current real_offset (block aligned)
uint64_t virtual_base; // info/base virtual_offset equivalent to current real_offset, block aligned (*not* including fake header)
uint64_t header_size; // fake header (parseable by FFmpeg) prepended on reads
uint8_t *header_insert_block; // fake header data (ie. RIFF)