From 3ebe08e9ee73cbd3552055c94b579d29b64d1570 Mon Sep 17 00:00:00 2001 From: bnnm Date: Sun, 1 Sep 2024 23:58:37 +0200 Subject: [PATCH] cleanup: misc --- src/coding/coding_utils_samples.h | 6 +-- src/coding/imuse_decoder.c | 39 ++++--------------- src/coding/tac_decoder.c | 65 +++++++++++++------------------ src/libvgmstream.h | 2 +- 4 files changed, 39 insertions(+), 73 deletions(-) diff --git a/src/coding/coding_utils_samples.h b/src/coding/coding_utils_samples.h index 05b55da3..659d3012 100644 --- a/src/coding/coding_utils_samples.h +++ b/src/coding/coding_utils_samples.h @@ -12,7 +12,7 @@ typedef struct { //TODO may be more useful with filled+consumed and not moving *samples? } s16buf_t; -static void s16buf_silence(sample_t** p_outbuf, int32_t* p_samples_silence, int channels) { +static inline void s16buf_silence(sample_t** p_outbuf, int32_t* p_samples_silence, int channels) { int samples_silence; samples_silence = *p_samples_silence; @@ -23,7 +23,7 @@ static void s16buf_silence(sample_t** p_outbuf, int32_t* p_samples_silence, int *p_samples_silence -= samples_silence; } -static void s16buf_discard(sample_t** p_outbuf, s16buf_t* sbuf, int32_t* p_samples_discard) { +static inline void s16buf_discard(sample_t** p_outbuf, s16buf_t* sbuf, int32_t* p_samples_discard) { int samples_discard; samples_discard = *p_samples_discard; @@ -39,7 +39,7 @@ static void s16buf_discard(sample_t** p_outbuf, s16buf_t* sbuf, int32_t* p_sampl } /* copy, move and mark consumed samples */ -static void s16buf_consume(sample_t** p_outbuf, s16buf_t* sbuf, int32_t* p_samples_consume) { +static inline void s16buf_consume(sample_t** p_outbuf, s16buf_t* sbuf, int32_t* p_samples_consume) { int samples_consume; samples_consume = *p_samples_consume; diff --git a/src/coding/imuse_decoder.c b/src/coding/imuse_decoder.c index 3b9d35f9..238c7ff8 100644 --- a/src/coding/imuse_decoder.c +++ b/src/coding/imuse_decoder.c @@ -1,5 +1,5 @@ #include "coding.h" - +#include "coding_utils_samples.h" /* LucasArts' iMUSE decoder, mainly for VIMA (like IMA but with variable frame and code sizes). * Reverse engineered from various .exe @@ -122,31 +122,6 @@ static const int8_t* index_tables_v2[8] = { /* ************************** */ -typedef struct { - /*const*/ int16_t* samples; - int filled; - int channels; - //todo may be more useful with filled/full? use 2 mark methods? -} sbuf_t; - -/* copy, move and mark consumed samples */ -static void sbuf_consume(sample_t** p_outbuf, int32_t* p_samples_to_do, sbuf_t* sbuf) { - int samples_consume; - - samples_consume = *p_samples_to_do; - if (samples_consume > sbuf->filled) - samples_consume = sbuf->filled; - - /* memcpy is safe when filled/samples_copy is 0 (but must pass non-NULL bufs) */ - memcpy(*p_outbuf, sbuf->samples, samples_consume * sbuf->channels * sizeof(int16_t)); - - sbuf->samples += samples_consume * sbuf->channels; - sbuf->filled -= samples_consume; - - *p_outbuf += samples_consume * sbuf->channels; - *p_samples_to_do -= samples_consume; -} - static int clamp_s32(int val, int min, int max) { if (val > max) return max; @@ -174,7 +149,7 @@ struct imuse_codec_data { uint16_t adpcm_table[64 * 89]; /* state */ - sbuf_t sbuf; + s16buf_t sbuf; int current_block; int16_t samples[MAX_BLOCK_SIZE / sizeof(int16_t) * MAX_CHANNELS]; }; @@ -309,7 +284,7 @@ fail: /* **************************************** */ -static void decode_vima1(sbuf_t* sbuf, uint8_t* buf, size_t data_left, int block_num, uint16_t* adpcm_table) { +static void decode_vima1(s16buf_t* sbuf, uint8_t* buf, size_t data_left, int block_num, uint16_t* adpcm_table) { int ch, i, j, s; int bitpos; int adpcm_history[MAX_CHANNELS] = {0}; @@ -434,7 +409,7 @@ static int decode_block1(imuse_codec_data* data, uint8_t* block, size_t data_lef return 1; } -static void decode_data2(sbuf_t* sbuf, uint8_t* buf, size_t data_left, int block_num) { +static void decode_data2(s16buf_t* sbuf, uint8_t* buf, size_t data_left, int block_num) { int i, j; int channels = sbuf->channels; @@ -453,7 +428,7 @@ static void decode_data2(sbuf_t* sbuf, uint8_t* buf, size_t data_left, int block } } -static void decode_vima2(sbuf_t* sbuf, uint8_t* buf, size_t data_left, uint16_t* adpcm_table) { +static void decode_vima2(s16buf_t* sbuf, uint8_t* buf, size_t data_left, uint16_t* adpcm_table) { int ch, i, s; int bitpos; int adpcm_history[MAX_CHANNELS] = {0}; @@ -622,14 +597,14 @@ void decode_imuse(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do) while (samples_to_do > 0) { - sbuf_t* sbuf = &data->sbuf; + s16buf_t* sbuf = &data->sbuf; if (sbuf->filled == 0) { ok = decode_block(sf, data); if (!ok) goto fail; } - sbuf_consume(&outbuf, &samples_to_do, sbuf); + s16buf_consume(&outbuf, sbuf, &samples_to_do); } return; diff --git a/src/coding/tac_decoder.c b/src/coding/tac_decoder.c index fc4ed696..5bf1f6fa 100644 --- a/src/coding/tac_decoder.c +++ b/src/coding/tac_decoder.c @@ -12,10 +12,10 @@ struct tac_codec_data { int encoder_delay; uint8_t buf[TAC_BLOCK_SIZE]; - int feed_block; + bool feed_block; off_t offset; - int16_t* samples; + int16_t samples[TAC_FRAME_SAMPLES * TAC_CHANNELS]; int frame_samples; /* frame state */ @@ -38,7 +38,7 @@ tac_codec_data* init_tac(STREAMFILE* sf) { data->handle = tac_init(data->buf, bytes); if (!data->handle) goto fail; - data->feed_block = 0; /* ok to use current block */ + data->feed_block = false; /* ok to use current block */ data->offset = bytes; data->channels = TAC_CHANNELS; data->frame_samples = TAC_FRAME_SAMPLES; @@ -46,9 +46,6 @@ tac_codec_data* init_tac(STREAMFILE* sf) { data->encoder_delay = 0; data->samples_discard = data->encoder_delay; - data->samples = malloc(data->channels * data->frame_samples * sizeof(int16_t)); - if (!data->samples) goto fail; - return data; fail: free_tac(data); @@ -56,58 +53,55 @@ fail: } -static int decode_frame(tac_codec_data* data) { +static bool decode_frame(tac_codec_data* data) { int err; data->sbuf.samples = data->samples; - data->sbuf.channels = 2; + data->sbuf.channels = data->channels; data->sbuf.filled = 0; err = tac_decode_frame(data->handle, data->buf); if (err == TAC_PROCESS_NEXT_BLOCK) { - data->feed_block = 1; - return 1; + data->feed_block = true; + return true; } if (err == TAC_PROCESS_DONE) { VGM_LOG("TAC: process done (EOF) %i\n", err); - goto fail; /* shouldn't reach this */ + return false; /* shouldn't reach this */ } if (err != TAC_PROCESS_OK) { VGM_LOG("TAC: process error %i\n", err); - goto fail; + return false; } tac_get_samples_pcm16(data->handle, data->sbuf.samples); data->sbuf.filled = data->frame_samples; - return 1; -fail: - return 0; + return true; } -static int read_frame(tac_codec_data* data, STREAMFILE* sf) { +static bool read_frame(tac_codec_data* data, STREAMFILE* sf) { /* new block must be read only when signaled by lib */ - if (data->feed_block) { - int bytes = read_streamfile(data->buf, data->offset, sizeof(data->buf), sf); - data->offset += bytes; - data->feed_block = 0; - if (bytes <= 0) goto fail; /* can read less that buf near EOF */ - } + if (!data->feed_block) + return true; + + int bytes = read_streamfile(data->buf, data->offset, sizeof(data->buf), sf); + data->offset += bytes; + data->feed_block = 0; + if (bytes <= 0) return false; /* can read less that buf near EOF */ - return 1; -fail: - return 0; + return true; } void decode_tac(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do) { VGMSTREAMCHANNEL* stream = &vgmstream->ch[0]; tac_codec_data* data = vgmstream->codec_data; - int ok; + bool ok; while (samples_to_do > 0) { @@ -132,7 +126,7 @@ void decode_tac(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do) { fail: /* on error just put some 0 samples */ VGM_LOG("TAC: decode fail at %x, missing %i samples\n", (uint32_t)data->offset, samples_to_do); - s16buf_silence(&outbuf, &samples_to_do, data->channels); + s16buf_silence(&outbuf, &samples_to_do, data->sbuf.channels); } @@ -141,12 +135,10 @@ void reset_tac(tac_codec_data* data) { tac_reset(data->handle); - data->offset = 0; - data->feed_block = 1; + data->feed_block = true; + data->offset = 0x00; data->sbuf.filled = 0; data->samples_discard = data->encoder_delay; - - return; } void seek_tac(tac_codec_data* data, int32_t num_sample) { @@ -162,18 +154,18 @@ void seek_tac(tac_codec_data* data, int32_t num_sample) { if (loop_sample == num_sample) { tac_set_loop(data->handle); /* direct looping */ - data->samples_discard = hdr->loop_discard; + data->feed_block = true; data->offset = hdr->loop_offset; - data->feed_block = 1; data->sbuf.filled = 0; + data->samples_discard = hdr->loop_discard; } else { tac_reset(data->handle); - data->samples_discard = num_sample; - data->offset = 0; - data->feed_block = 1; + data->feed_block = true; + data->offset = 0x00; data->sbuf.filled = 0; + data->samples_discard = num_sample; } } @@ -182,6 +174,5 @@ void free_tac(tac_codec_data* data) { return; tac_free(data->handle); - free(data->samples); free(data); } diff --git a/src/libvgmstream.h b/src/libvgmstream.h index 3be44d64..5afd4787 100644 --- a/src/libvgmstream.h +++ b/src/libvgmstream.h @@ -1,7 +1,7 @@ #ifndef _LIBVGMSTREAM_H_ #define _LIBVGMSTREAM_H_ -//#define LIBVGMSTREAM_ENABLE 1 +#define LIBVGMSTREAM_ENABLE 1 #if LIBVGMSTREAM_ENABLE /* libvgmstream: vgmstream's public API