This commit is contained in:
bnnm 2022-07-23 14:55:26 +02:00
parent 8d4940daea
commit 686f3ae2bb

View File

@ -1,48 +1,51 @@
#include "meta.h"
#include "../coding/coding.h"
#include "../util.h"
#if 0
#include "adx_keys.h"
#endif
/* AHX - CRI format mainly for voices, contains MPEG-2 Layer 2 audio with lying frame headers */
VGMSTREAM * init_vgmstream_ahx(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset;
int channel_count = 1, loop_flag = 0, type;
/* AHX - CRI voice format */
VGMSTREAM* init_vgmstream_ahx(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
uint32_t start_offset;
int channels = 1, loop_flag = 0, type;
/* check extension, case insensitive */
if ( !check_extensions(streamFile, "ahx") ) goto fail;
/* checks */
if (read_u16be(0x00,sf) != 0x8000)
goto fail;
/* check first 2 bytes */
if ((uint16_t)read_16bitBE(0,streamFile)!=0x8000) goto fail;
if (!check_extensions(sf, "ahx") )
goto fail;
/* get stream offset, check for CRI signature just before */
start_offset = (uint16_t)read_16bitBE(0x02,streamFile) + 0x04;
if ((uint16_t)read_16bitBE(start_offset-0x06,streamFile)!=0x2863 || /* "(c" */
(uint32_t)read_32bitBE(start_offset-0x04,streamFile)!=0x29435249) /* ")CRI" */
start_offset = read_u16be(0x02,sf) + 0x04;
if (read_u16be(start_offset - 0x06,sf) != 0x2863 || /* "(c" */
read_u32be(start_offset - 0x04,sf) != 0x29435249) /* ")CRI" */
goto fail;
/* check for encoding type (0x10 is AHX for DC with bigger frames, 0x11 is AHX, 0x0N are ADX) */
type = read_8bit(0x04,streamFile);
/* types: 0x10 = AHX for DC with bigger frames, 0x11 = AHX, 0x0N = ADX */
type = read_u8(0x04,sf);
if (type != 0x10 && type != 0x11) goto fail;
/* check for frame size (0 for AHX) */
if (read_8bit(0x05,streamFile) != 0) goto fail;
/* frame size (0 for AHX) */
if (read_u8(0x05,sf) != 0) goto fail;
/* check for bits per sample? (0 for AHX) */
if (read_8bit(0x06,streamFile) != 0) goto fail;
if (read_u8(0x06,sf) != 0) goto fail;
/* check channel count (only mono AHXs can be created by the encoder) */
if (read_8bit(0x07,streamFile) != 1) goto fail;
if (read_u8(0x07,sf) != 1) goto fail;
/* check version signature */
if (read_8bit(0x12,streamFile) != 0x06) goto fail;
if (read_u8(0x12,sf) != 0x06) goto fail;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = read_32bitBE(0x08,streamFile); /* real sample rate */
vgmstream->num_samples = read_32bitBE(0x0c,streamFile); /* doesn't include encoder_delay (handled in decoder) */
vgmstream->sample_rate = read_s32be(0x08,sf); /* real sample rate */
vgmstream->num_samples = read_s32be(0x0c,sf); /* doesn't include encoder_delay (handled in decoder) */
vgmstream->meta_type = meta_AHX;
@ -50,30 +53,51 @@ VGMSTREAM * init_vgmstream_ahx(STREAMFILE *streamFile) {
#ifdef VGM_USE_MPEG
mpeg_custom_config cfg = {0};
cfg.encryption = read_8bit(0x13,streamFile); /* 0x08 = keyword encryption */
cfg.encryption = read_u8(0x13,sf); /* 0x08 = keyword encryption */
cfg.cri_type = type;
if (cfg.encryption) {
uint8_t keybuf[6];
if (read_key_file(keybuf, 6, streamFile) == 6) {
cfg.cri_key1 = get_16bitBE(keybuf+0);
cfg.cri_key2 = get_16bitBE(keybuf+2);
cfg.cri_key3 = get_16bitBE(keybuf+4);
uint8_t keybuf[0x10+1] = {0}; /* approximate max for keystrings, +1 extra null for keystrings */
size_t key_size;
key_size = read_key_file(keybuf, sizeof(keybuf), sf);
if (key_size > 0) {
#if 0
int i, is_ascii;
is_ascii = 1;
for (i = 0; i < key_size; i++) {
if (keybuf[i] < 0x20 || keybuf[i] > 0x7f) {
is_ascii = 0;
break;
}
}
#endif
if (key_size == 0x06 /*&& !is_ascii*/) {
cfg.cri_key1 = get_u16be(keybuf + 0x00);
cfg.cri_key2 = get_u16be(keybuf + 0x02);
cfg.cri_key3 = get_u16be(keybuf + 0x04);
}
#if 0
else if (is_ascii) {
const char* keystring = (const char*)keybuf;
derive_adx_key8(keystring, &cfg.cri_key1, &cfg.cri_key2, &cfg.cri_key3);
VGM_LOG("ok: %x, %x, %x\n", cfg.cri_key1, cfg.cri_key2, cfg.cri_key3 );
}
#endif
}
}
vgmstream->layout_type = layout_none;
vgmstream->codec_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, channel_count, MPEG_AHX, &cfg);
vgmstream->codec_data = init_mpeg_custom(sf, start_offset, &vgmstream->coding_type, channels, MPEG_AHX, &cfg);
if (!vgmstream->codec_data) goto fail;
#else
goto fail;
#endif
}
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
goto fail;
return vgmstream;
fail: