Fix HCA num samples/looping [Binary Domain (PC), Octopath Traveller]

Decoder has been modified a bit so it's easier to feed an arbitrary
number of discard samples (or at least, easier for me to understand),
since encoder delay can be >1024 (frame size) and would make some calcs
go all wobbly
This commit is contained in:
bnnm 2018-08-29 23:42:47 +02:00
parent 9a2a2e4c2e
commit 43764d6c8d
5 changed files with 106 additions and 110 deletions

View File

@ -172,8 +172,8 @@ void free_ea_mt(ea_mt_codec_data *data);
/* hca_decoder */
hca_codec_data *init_hca(STREAMFILE *streamFile);
void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do);
void reset_hca(VGMSTREAM *vgmstream);
void loop_hca(VGMSTREAM *vgmstream);
void reset_hca(hca_codec_data * data);
void loop_hca(hca_codec_data * data);
void free_hca(hca_codec_data * data);
#ifdef VGM_USE_VORBIS

View File

@ -13,8 +13,6 @@ hca_codec_data * init_hca(STREAMFILE *streamFile) {
/* init library handle */
data->handle = calloc(1, clHCA_sizeof());
data->sample_ptr = clHCA_samplesPerBlock;
/* load streamfile for reads */
get_streamfile_name(streamFile,filename, sizeof(filename));
data->streamfile = open_streamfile(streamFile,filename);
@ -29,7 +27,9 @@ fail:
void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
int samples_done = 0;
int32_t samples_remain = clHCA_samplesPerBlock - data->sample_ptr;
const unsigned int channels = data->info.channelCount;
const unsigned int blockSize = data->info.blockSize;
//todo improve (can't be done on init since data->info is only read after setting key)
if (!data->data_buffer) {
@ -41,37 +41,40 @@ void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
}
if ( data->samples_discard ) {
if ( samples_remain <= data->samples_discard ) {
data->samples_discard -= samples_remain;
samples_remain = 0;
while (samples_done < samples_to_do) {
if (data->samples_filled) {
int samples_to_get = data->samples_filled;
if (data->samples_to_discard) {
/* discard samples for looping */
if (samples_to_get > data->samples_to_discard)
samples_to_get = data->samples_to_discard;
data->samples_to_discard -= samples_to_get;
}
else {
samples_remain -= data->samples_discard;
data->sample_ptr += data->samples_discard;
data->samples_discard = 0;
}
/* get max samples and copy */
if (samples_to_get > samples_to_do - samples_done)
samples_to_get = samples_to_do - samples_done;
memcpy(outbuf + samples_done*channels,
data->sample_buffer + data->samples_consumed*channels,
samples_to_get*channels * sizeof(sample));
samples_done += samples_to_get;
}
if (samples_remain > samples_to_do)
samples_remain = samples_to_do;
memcpy(outbuf, data->sample_buffer + data->sample_ptr * data->info.channelCount, samples_remain * data->info.channelCount * sizeof(sample));
outbuf += samples_remain * data->info.channelCount;
data->sample_ptr += samples_remain;
samples_done += samples_remain;
/* feed */
while ( samples_done < samples_to_do ) {
const unsigned int blockSize = data->info.blockSize;
const unsigned int channelCount = data->info.channelCount;
const unsigned int address = data->info.dataOffset + data->curblock * blockSize;
/* mark consumed samples */
data->samples_consumed += samples_to_get;
data->samples_filled -= samples_to_get;
}
else {
const unsigned int address = data->info.dataOffset + data->current_block * blockSize;
int status;
size_t bytes;
/* EOF/error */
if (data->curblock >= data->info.blockCount) {
memset(outbuf, 0, (samples_to_do - samples_done) * channelCount * sizeof(sample));
if (data->current_block >= data->info.blockCount) {
memset(outbuf, 0, (samples_to_do - samples_done) * channels * sizeof(sample));
break;
}
@ -88,55 +91,39 @@ void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
VGM_LOG("HCA: decode fail at %x", address);
break;
}
data->curblock++;
/* extract samples */
clHCA_DecodeSamples16(data->handle, data->sample_buffer);
samples_remain = clHCA_samplesPerBlock;
data->sample_ptr = 0;
if ( data->samples_discard ) {
if ( samples_remain <= data->samples_discard ) {
data->samples_discard -= samples_remain;
samples_remain = 0;
data->current_block++;
data->samples_consumed = 0;
data->samples_filled += data->info.samplesPerBlock;
}
else {
samples_remain -= data->samples_discard;
data->sample_ptr = data->samples_discard;
data->samples_discard = 0;
}
}
if (samples_remain > samples_to_do - samples_done)
samples_remain = samples_to_do - samples_done;
memcpy(outbuf, data->sample_buffer, samples_remain * channelCount * sizeof(sample));
samples_done += samples_remain;
outbuf += samples_remain * channelCount;
data->sample_ptr = samples_remain;
}
}
void reset_hca(VGMSTREAM *vgmstream) {
hca_codec_data *data = vgmstream->codec_data;
void reset_hca(hca_codec_data * data) {
if (!data) return;
data->curblock = 0;
data->sample_ptr = clHCA_samplesPerBlock;
data->samples_discard = 0;
data->current_block = 0;
data->samples_filled = 0;
data->samples_consumed = 0;
data->samples_to_discard = data->info.encoderDelay;
}
void loop_hca(VGMSTREAM *vgmstream) {
hca_codec_data *data = (hca_codec_data *)(vgmstream->codec_data);
void loop_hca(hca_codec_data * data) {
if (!data) return;
data->curblock = data->info.loopStartBlock;
data->sample_ptr = clHCA_samplesPerBlock;
data->samples_discard = 0;
data->current_block = data->info.loopStartBlock;
data->samples_filled = 0;
data->samples_consumed = 0;
data->samples_to_discard = data->info.loopStartDelay;
}
void free_hca(hca_codec_data * data) {
if (data) {
if (!data) return;
close_streamfile(data->streamfile);
clHCA_done(data->handle);
free(data->handle);
@ -144,4 +131,3 @@ void free_hca(hca_codec_data * data) {
free(data->sample_buffer);
free(data);
}
}

View File

@ -26,7 +26,6 @@ VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile) {
if (clHCA_isOurFile1(header_buffer, header_size) < 0)
goto fail;
/* init vgmstream context */
hca_data = init_hca(streamFile);
@ -48,6 +47,7 @@ VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile) {
goto fail;
if (clHCA_getInfo(hca_data->handle, &hca_data->info) < 0) /* copy important header values to info struct */
goto fail;
reset_hca(hca_data);
/* build the VGMSTREAM */
@ -56,9 +56,18 @@ VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile) {
vgmstream->meta_type = meta_HCA;
vgmstream->sample_rate = hca_data->info.samplingRate;
vgmstream->num_samples = hca_data->info.blockCount * hca_data->info.samplesPerBlock;
vgmstream->loop_start_sample = hca_data->info.loopStartBlock * hca_data->info.samplesPerBlock;
vgmstream->loop_end_sample = hca_data->info.loopEndBlock * hca_data->info.samplesPerBlock;
vgmstream->num_samples = hca_data->info.blockCount * hca_data->info.samplesPerBlock -
hca_data->info.encoderDelay - hca_data->info.encoderPadding;
vgmstream->loop_start_sample = hca_data->info.loopStartBlock * hca_data->info.samplesPerBlock -
hca_data->info.encoderDelay + hca_data->info.loopStartDelay;
vgmstream->loop_end_sample = hca_data->info.loopEndBlock * hca_data->info.samplesPerBlock -
hca_data->info.encoderDelay + (hca_data->info.samplesPerBlock - hca_data->info.loopEndPadding);
/* After loop end CRI's encoder removes the rest of the original samples and puts some
* garbage in the last frame that should be ignored. Optionally it can encode fully preserving
* the file too, but it isn't detectable, so we'll allow the whole thing just in case */
//if (vgmstream->loop_end_sample && vgmstream->num_samples > vgmstream->loop_end_sample)
// vgmstream->num_samples = vgmstream->loop_end_sample;
vgmstream->coding_type = coding_CRI_HCA;
vgmstream->layout_type = layout_none;
@ -101,9 +110,8 @@ static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int
/* re-init HCA with the current key as buffer becomes invalid (probably can be simplified) */
hca_data->curblock = 0;
hca_data->sample_ptr = clHCA_samplesPerBlock;
if (read_streamfile(header_buffer, hca_data->start, header_size, hca_data->streamfile) != header_size)
reset_hca(hca_data);
if (read_streamfile(header_buffer, 0x00, header_size, hca_data->streamfile) != header_size)
continue;
clHCA_clear(hca_data->handle, keycode_lower, keycode_upper);
@ -156,10 +164,9 @@ static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int
// break;
}
/* reset HCA */
hca_data->curblock = 0;
hca_data->sample_ptr = clHCA_samplesPerBlock;
read_streamfile(header_buffer, hca_data->start, header_size, hca_data->streamfile);
/* reset HCA header */
hca_data->current_block = 0;
read_streamfile(header_buffer, 0x00, header_size, hca_data->streamfile);
end:
VGM_ASSERT(min_clip_count > 0, "HCA: best key=%08x%08x (clips=%i)\n", best_keycode_upper,best_keycode_lower, min_clip_count);

View File

@ -573,7 +573,7 @@ void reset_vgmstream(VGMSTREAM * vgmstream) {
#endif
if (vgmstream->coding_type==coding_CRI_HCA) {
reset_hca(vgmstream);
reset_hca(vgmstream->codec_data);
}
if (vgmstream->coding_type==coding_EA_MT) {
@ -1210,7 +1210,7 @@ int get_vgmstream_samples_per_frame(VGMSTREAM * vgmstream) {
case coding_EA_MT:
return 432;
case coding_CRI_HCA:
return clHCA_samplesPerBlock;
return 0; /* 1024 - delay/padding (which can be bigger than 1024) */
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
case coding_MP4_AAC:
return ((mp4_aac_codec_data*)vgmstream->codec_data)->samples_per_frame;
@ -2038,7 +2038,7 @@ int vgmstream_do_loop(VGMSTREAM * vgmstream) {
/* prepare certain codecs' internal state for looping */
if (vgmstream->coding_type==coding_CRI_HCA) {
loop_hca(vgmstream);
loop_hca(vgmstream->codec_data);
}
if (vgmstream->coding_type==coding_EA_MT) {

View File

@ -1091,15 +1091,18 @@ typedef struct {
typedef struct {
STREAMFILE *streamfile;
uint64_t start;
//uint64_t size;
clHCA_stInfo info;
unsigned int curblock;
unsigned int sample_ptr;
unsigned int samples_discard;
signed short *sample_buffer;
void* handle;
size_t samples_filled;
size_t samples_consumed;
size_t samples_to_discard;
void* data_buffer;
unsigned int current_block;
void* handle;
} hca_codec_data;
#ifdef VGM_USE_FFMPEG