From 40e2099fea6fcd6c84487c99ba97c69df91487b6 Mon Sep 17 00:00:00 2001 From: bnnm Date: Thu, 23 May 2019 23:47:53 +0200 Subject: [PATCH] Fix installed TXTP loops in HCA files --- src/coding/coding.h | 2 +- src/coding/hca_decoder.c | 12 +++++++++++- src/vgmstream.c | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/coding/coding.h b/src/coding/coding.h index 7d0ff2ef..532b6d85 100644 --- a/src/coding/coding.h +++ b/src/coding/coding.h @@ -193,7 +193,7 @@ void free_ea_mt(ea_mt_codec_data *data, int channels); hca_codec_data *init_hca(STREAMFILE *streamFile); void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do); void reset_hca(hca_codec_data * data); -void loop_hca(hca_codec_data * data); +void loop_hca(hca_codec_data * data, int32_t num_sample); void free_hca(hca_codec_data * data); int test_hca_key(hca_codec_data * data, unsigned long long keycode); diff --git a/src/coding/hca_decoder.c b/src/coding/hca_decoder.c index 75e93440..64b6cd47 100644 --- a/src/coding/hca_decoder.c +++ b/src/coding/hca_decoder.c @@ -130,9 +130,19 @@ void reset_hca(hca_codec_data * data) { data->samples_to_discard = data->info.encoderDelay; } -void loop_hca(hca_codec_data * data) { +void loop_hca(hca_codec_data * data, int32_t num_sample) { if (!data) return; + /* manually calc loop values if not set (should only happen with installed/forced looping, + * as actual files usually pad encoder delay so earliest loopStartBlock becomes 1-2, + * probably for decoding cleanup so this may not be as exact) */ + if (data->info.loopStartBlock == 0 && data->info.loopStartDelay == 0) { + int target_sample = num_sample + data->info.encoderDelay; + + data->info.loopStartBlock = target_sample / data->info.samplesPerBlock; + data->info.loopStartDelay = target_sample - (data->info.loopStartBlock * data->info.samplesPerBlock); + } + data->current_block = data->info.loopStartBlock; data->samples_filled = 0; data->samples_consumed = 0; diff --git a/src/vgmstream.c b/src/vgmstream.c index 527b85b8..4e6c327d 100644 --- a/src/vgmstream.c +++ b/src/vgmstream.c @@ -2162,7 +2162,7 @@ int vgmstream_do_loop(VGMSTREAM * vgmstream) { /* prepare certain codecs' internal state for looping */ if (vgmstream->coding_type == coding_CRI_HCA) { - loop_hca(vgmstream->codec_data); + loop_hca(vgmstream->codec_data, vgmstream->loop_sample); } if (vgmstream->coding_type == coding_EA_MT) {