vgmstream/src/meta/adx.c

429 lines
16 KiB
C
Raw Normal View History

#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <limits.h>
#include "meta.h"
#include "adx_keys.h"
#include "../coding/coding.h"
#define MAX_TEST_FRAMES (INT_MAX/0x8000)
static int find_adx_key(STREAMFILE *streamFile, uint8_t type, uint16_t *xor_start, uint16_t *xor_mult, uint16_t *xor_add);
/* ADX - CRI Middleware format */
VGMSTREAM * init_vgmstream_adx(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset, hist_offset = 0;
int loop_flag = 0, channel_count;
int32_t loop_start_sample = 0, loop_end_sample = 0;
uint16_t version_signature;
2017-05-13 23:04:30 +02:00
uint8_t encoding_type;
uint8_t frame_size;
meta_t header_type;
2017-05-14 00:37:24 +02:00
coding_t coding_type;
int16_t coef1, coef2;
uint16_t xor_start=0,xor_mult=0,xor_add=0;
/* checks*/
/* .adx: standard, .adp: Headhunter (DC) */
if (!check_extensions(streamFile,"adx,adp"))
goto fail;
/* check first 2 bytes */
if ((uint16_t)read_16bitBE(0x00,streamFile)!=0x8000) goto fail;
/* get stream offset, check for CRI signature just before */
start_offset = (uint16_t)read_16bitBE(0x02,streamFile) + 4;
if ((uint16_t)read_16bitBE(start_offset-6,streamFile)!=0x2863 || /* "(c" */
(uint32_t)read_32bitBE(start_offset-4,streamFile)!=0x29435249 /* ")CRI" */
) goto fail;
/* check for encoding type */
/* 0x02 is for some unknown fixed filter, 0x03 is standard ADX, 0x04 is
* ADX with exponential scale, 0x10 is AHX for DC, 0x11 is AHX */
2017-05-13 23:04:30 +02:00
encoding_type = read_8bit(0x04, streamFile);
2017-05-14 00:37:24 +02:00
switch (encoding_type) {
case 2:
coding_type = coding_CRI_ADX_fixed;
break;
case 3:
coding_type = coding_CRI_ADX;
break;
case 4:
coding_type = coding_CRI_ADX_exp;
break;
default:
goto fail;
}
frame_size = read_8bit(0x05, streamFile);
/* check for bits per sample? (only 4 makes sense for ADX) */
if (read_8bit(0x06,streamFile) != 4) goto fail;
/* older ADX (adxencd) up to 2ch, newer ADX (criatomencd) up to 8 */
channel_count = read_8bit(0x07,streamFile);
/* check version signature, read loop info */
version_signature = read_16bitBE(0x12,streamFile);
/* encryption */
if (version_signature == 0x0408) {
if (find_adx_key(streamFile, 8, &xor_start, &xor_mult, &xor_add)) {
coding_type = coding_CRI_ADX_enc_8;
version_signature = 0x0400;
}
}
else if (version_signature == 0x0409) {
if (find_adx_key(streamFile, 9, &xor_start, &xor_mult, &xor_add)) {
coding_type = coding_CRI_ADX_enc_9;
version_signature = 0x0400;
}
}
/* version + extra data */
if (version_signature == 0x0300) { /* early ADX (~2004?) */
size_t base_size = 0x14, loops_size = 0x18;
header_type = meta_ADX_03;
/* no sample history */
if (start_offset - 6 >= base_size + loops_size) { /* enough space for loop info? */
off_t loops_offset = base_size;
/* off+0x00 (2): initial loop padding (the encoder adds a few blank samples so loop start is block-aligned; max 31)
* ex. loop_start=12: enc_start=32, padding=20 (32-20=12); loop_start=35: enc_start=64, padding=29 (64-29=35)
* off+0x02 (2): loop sample(?) flag (always 1) */
loop_flag = read_32bitBE(loops_offset+0x04,streamFile) != 0; /* loop offset(?) flag (always 1) */
loop_start_sample = read_32bitBE(loops_offset+0x08,streamFile);
//loop_start_offset = read_32bitBE(loops_offset+0x0c,streamFile);
loop_end_sample = read_32bitBE(loops_offset+0x10,streamFile);
//loop_end_offset = read_32bitBE(loops_offset+0x14,streamFile);
}
}
else if (version_signature == 0x0400) { /* common */
size_t base_size = 0x18, hist_size, ainf_size = 0, loops_size = 0x18;
off_t ainf_offset;
header_type = meta_ADX_04;
hist_offset = base_size; /* always present but often blank */
hist_size = (channel_count > 1 ? 4*channel_count : 4+4); /* min is 8, even in 1ch files */
ainf_offset = base_size + hist_size + 0x4; /* not seen with >2ch though */
if ((uint32_t)read_32bitBE(ainf_offset+0x00,streamFile) == 0x41494E46) /* "AINF" */
ainf_size = read_32bitBE(ainf_offset+0x04,streamFile);
if (start_offset - ainf_size - 6 >= hist_offset + hist_size + loops_size) { /* enough space for loop info? */
off_t loops_offset = base_size + hist_size;
/* off+0x00 (2): initial loop padding (the encoder adds a few blank samples so loop start is block-aligned; max 31)
* ex. loop_start=12: enc_start=32, padding=20 (32-20=12); loop_start=35: enc_start=64, padding=29 (64-29=35)
* off+0x02 (2): loop sample(?) flag (always 1) */
loop_flag = read_32bitBE(loops_offset+0x04,streamFile) != 0; /* loop offset(?) flag (always 1) */
loop_start_sample = read_32bitBE(loops_offset+0x08,streamFile);
//loop_start_offset = read_32bitBE(loops_offset+0x0c,streamFile);
loop_end_sample = read_32bitBE(loops_offset+0x10,streamFile);
//loop_end_offset = read_32bitBE(loops_offset+0x14,streamFile);
}
/* AINF header info (may be inserted by CRI's tools but is rarely used)
* Can also start right after the loop points (base_size + hist_size + loops_size)
* 0x00 (4): "AINF"; 0x04 (4): size; 0x08 (10): str_id
* 0x18 (2): volume (0=base/max?, negative=reduce)
* 0x1c (2): pan l; 0x1e (2): pan r (0=base, max +-128) */
2018-09-02 21:06:38 +02:00
/* CINF header info (very rare, found after loops) [Sakura Taisen 3 (PS2)]
* 0x00 (4): "CINF"
* 0x04 (4): size
* 0x08 (4): "ASO ", unknown
* 0x28 (4): "SND ", unknown
* 0x48 (-): file name, null terminated
*/
}
else if (version_signature == 0x0500) { /* found in some SFD: Buggy Heat, appears to have no loop */
header_type = meta_ADX_05;
}
else { /* not a known/supported version signature */
goto fail;
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->num_samples = read_32bitBE(0xc,streamFile);
vgmstream->sample_rate = read_32bitBE(0x8,streamFile);
vgmstream->loop_start_sample = loop_start_sample;
vgmstream->loop_end_sample = loop_end_sample;
vgmstream->coding_type = coding_type;
vgmstream->layout_type = channel_count==1 ? layout_none : layout_interleave;
vgmstream->interleave_block_size = frame_size;
vgmstream->meta_type = header_type;
/* calculate filter coefficients */
2017-05-13 23:04:30 +02:00
if (coding_type == coding_CRI_ADX_fixed) {
int i;
for (i = 0; i < channel_count; i++) {
vgmstream->ch[i].adpcm_coef[0] = 0x0000;
vgmstream->ch[i].adpcm_coef[1] = 0x0000;
vgmstream->ch[i].adpcm_coef[2] = 0x0F00;
vgmstream->ch[i].adpcm_coef[3] = 0x0000;
vgmstream->ch[i].adpcm_coef[4] = 0x1CC0;
vgmstream->ch[i].adpcm_coef[5] = 0xF300;
vgmstream->ch[i].adpcm_coef[6] = 0x1880;
vgmstream->ch[i].adpcm_coef[7] = 0xF240;
}
}
else {
double x,y,z,a,b,c;
2017-05-14 01:48:35 +02:00
int i;
/* high-pass cutoff frequency, always 500 that I've seen */
uint16_t cutoff = (uint16_t)read_16bitBE(0x10,streamFile);
x = cutoff;
y = vgmstream->sample_rate;
z = cos(2.0*M_PI*x/y);
a = M_SQRT2-z;
b = M_SQRT2-1.0;
c = (a-sqrt((a+b)*(a-b)))/b;
coef1 = (short)(c*8192);
coef2 = (short)(c*c*-4096);
2017-05-13 23:04:30 +02:00
for (i = 0; i < channel_count; i++) {
2017-05-14 00:37:24 +02:00
vgmstream->ch[i].adpcm_coef[0] = coef1;
vgmstream->ch[i].adpcm_coef[1] = coef2;
2017-05-13 23:04:30 +02:00
}
}
/* init decoder */
{
int i;
for (i=0;i<channel_count;i++) {
/* 2 hist shorts per ch, corresponding to the very first original sample repeated (verified with CRI's encoders).
* Not vital as their effect is small, after a few samples they don't matter, and most songs start in silence. */
if (hist_offset) {
vgmstream->ch[i].adpcm_history1_32 = read_16bitBE(hist_offset + i*4 + 0x00,streamFile);
vgmstream->ch[i].adpcm_history2_32 = read_16bitBE(hist_offset + i*4 + 0x02,streamFile);
}
if (coding_type == coding_CRI_ADX_enc_8 || coding_type == coding_CRI_ADX_enc_9) {
int j;
vgmstream->ch[i].adx_channels = channel_count;
vgmstream->ch[i].adx_xor = xor_start;
vgmstream->ch[i].adx_mult = xor_mult;
vgmstream->ch[i].adx_add = xor_add;
for (j=0;j<i;j++)
adx_next_key(&vgmstream->ch[i]);
}
}
}
if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) )
goto fail;
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
}
/* return 0 if not found, 1 if found and set parameters */
static int find_adx_key(STREAMFILE *streamFile, uint8_t type, uint16_t *xor_start, uint16_t *xor_mult, uint16_t *xor_add) {
uint16_t * scales = NULL;
uint16_t * prescales = NULL;
int bruteframe = 0, bruteframe_count = -1;
int startoff, endoff;
int i, rc = 0;
/* try to find key in external file first */
{
uint8_t keybuf[6];
if (read_key_file(keybuf, 6, streamFile) == 6) {
*xor_start = get_16bitBE(keybuf+0);
*xor_mult = get_16bitBE(keybuf+2);
*xor_add = get_16bitBE(keybuf+4);
return 1;
}
}
/* setup totals */
{
int frame_count;
startoff = read_16bitBE(2, streamFile) + 4;
endoff = (read_32bitBE(12, streamFile) + 31) / 32 * 18 * read_8bit(7, streamFile) + startoff;
frame_count = (endoff - startoff) / 18;
if (frame_count < bruteframe_count || bruteframe_count < 0)
bruteframe_count = frame_count;
}
/* find longest run of nonzero frames */
{
int longest = -1, longest_length = -1;
int length = 0;
for (i = 0; i < bruteframe_count; i++) {
static const unsigned char zeroes[18] = {0};
unsigned char buf[18];
read_streamfile(buf, startoff + i * 18, 18, streamFile);
if (memcmp(zeroes, buf, 18))
length++;
else
length = 0;
if (length > longest_length) {
longest_length = length;
longest = i - length + 1;
if (longest_length >= 0x8000)
break;
}
}
if (longest == -1) {
goto find_key_cleanup;
}
bruteframe_count = longest_length;
bruteframe = longest;
}
/* try to guess key */
{
const adxkey_info * keys = NULL;
int keycount = 0, keymask = 0;
int scales_to_do;
int key_id;
/* allocate storage for scales */
scales_to_do = (bruteframe_count > MAX_TEST_FRAMES ? MAX_TEST_FRAMES : bruteframe_count);
scales = malloc(scales_to_do*sizeof(uint16_t));
if (!scales) goto find_key_cleanup;
/* prescales are those scales before the first frame we test
* against, we use these to compute the actual start */
if (bruteframe > 0) {
/* allocate memory for the prescales */
prescales = malloc(bruteframe*sizeof(uint16_t));
if (!prescales) goto find_key_cleanup;
/* read the prescales */
for (i=0; i<bruteframe; i++) {
prescales[i] = read_16bitBE(startoff+i*18, streamFile);
}
}
/* read in the scales */
for (i=0; i < scales_to_do; i++) {
scales[i] = read_16bitBE(startoff+(bruteframe+i)*18, streamFile);
}
if (type == 8) {
keys = adxkey8_list;
keycount = adxkey8_list_count;
keymask = 0x6000;
}
else if (type == 9) {
/* smarter XOR as seen in PSO2. The scale is technically 13 bits,
* but the maximum value assigned by the encoder is 0x1000.
* This is written to the ADX file as 0xFFF, leaving the high bit
* empty, which is used to validate a key */
keys = adxkey9_list;
keycount = adxkey9_list_count;
keymask = 0x1000;
}
/* try all keys until one decrypts correctly vs expected values */
for (key_id = 0; key_id < keycount; key_id++) {
uint16_t key_xor, key_mul, key_add;
uint16_t xor, mul, add;
/* get pre-derived XOR values or derive if needed */
if (keys[key_id].start || keys[key_id].mult || keys[key_id].add) {
key_xor = keys[key_id].start;
key_mul = keys[key_id].mult;
key_add = keys[key_id].add;
}
else if (type == 8 && keys[key_id].key8) {
derive_adx_key8(keys[key_id].key8, &key_xor, &key_mul, &key_add);
}
else if (type == 9 && keys[key_id].key9) {
derive_adx_key9(keys[key_id].key9, &key_xor, &key_mul, &key_add);
}
else {
VGM_LOG("ADX: incorrectly defined key id=%i\n", key_id);
continue;
}
/* temp test values */
xor = key_xor;
mul = key_mul;
add = key_add;
#if 0
/* derive and print all keys in the list, quick validity test */
2017-10-28 18:04:15 +02:00
{
uint16_t test_xor, test_mul, test_add;
xor = keys[key_id].start;
mul = keys[key_id].mult;
add = keys[key_id].add;
2017-10-28 18:04:15 +02:00
if (type == 8 && keys[key_id].key8) {
derive_adx_key8(keys[key_id].key8, &test_xor, &test_mul, &test_add);
2017-10-28 18:04:15 +02:00
VGM_LOG("key8: pre=%04x %04x %04x vs calc=%04x %04x %04x = %s (\"%s\")\n",
xor,mul,add, test_xor,test_mul,test_add,
xor==test_xor && mul==test_mul && add==test_add ? "ok" : "ko", keys[key_id].key8);
2017-10-28 18:04:15 +02:00
}
else if (type == 9 && keys[key_id].key9) {
derive_adx_key9(keys[key_id].key9, &test_xor, &test_mul, &test_add);
2017-10-28 18:04:15 +02:00
VGM_LOG("key9: pre=%04x %04x %04x vs calc=%04x %04x %04x = %s (%"PRIu64")\n",
xor,mul,add, test_xor,test_mul,test_add,
xor==test_xor && mul==test_mul && add==test_add ? "ok" : "ko", keys[key_id].key9);
2017-10-28 18:04:15 +02:00
}
continue;
}
#endif
/* test vs prescales while xor looks valid */
for (i = 0; i < bruteframe && ((prescales[i] & keymask) == (xor & keymask) || prescales[i] == 0); i++) {
xor = xor * mul + add;
}
if (i == bruteframe) {
/* test vs scales while xor looks valid */
for (i = 0; i < scales_to_do && (scales[i] & keymask) == (xor & keymask); i++) {
xor = xor * mul + add;
}
/* key is good */
if (i == scales_to_do) {
*xor_start = key_xor;
*xor_mult = key_mul;
*xor_add = key_add;
rc = 1;
goto find_key_cleanup;
}
}
}
}
find_key_cleanup:
free(scales);
free(prescales);
return rc;
}