cleanup: misc

This commit is contained in:
bnnm 2024-09-01 23:58:37 +02:00
parent f95f5441ca
commit 3ebe08e9ee
4 changed files with 39 additions and 73 deletions

View File

@ -12,7 +12,7 @@ typedef struct {
//TODO may be more useful with filled+consumed and not moving *samples? //TODO may be more useful with filled+consumed and not moving *samples?
} s16buf_t; } 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; int samples_silence;
samples_silence = *p_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; *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; int samples_discard;
samples_discard = *p_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 */ /* 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; int samples_consume;
samples_consume = *p_samples_consume; samples_consume = *p_samples_consume;

View File

@ -1,5 +1,5 @@
#include "coding.h" #include "coding.h"
#include "coding_utils_samples.h"
/* LucasArts' iMUSE decoder, mainly for VIMA (like IMA but with variable frame and code sizes). /* LucasArts' iMUSE decoder, mainly for VIMA (like IMA but with variable frame and code sizes).
* Reverse engineered from various .exe * 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) { static int clamp_s32(int val, int min, int max) {
if (val > max) if (val > max)
return max; return max;
@ -174,7 +149,7 @@ struct imuse_codec_data {
uint16_t adpcm_table[64 * 89]; uint16_t adpcm_table[64 * 89];
/* state */ /* state */
sbuf_t sbuf; s16buf_t sbuf;
int current_block; int current_block;
int16_t samples[MAX_BLOCK_SIZE / sizeof(int16_t) * MAX_CHANNELS]; 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 ch, i, j, s;
int bitpos; int bitpos;
int adpcm_history[MAX_CHANNELS] = {0}; 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; 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 i, j;
int channels = sbuf->channels; 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 ch, i, s;
int bitpos; int bitpos;
int adpcm_history[MAX_CHANNELS] = {0}; 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) { while (samples_to_do > 0) {
sbuf_t* sbuf = &data->sbuf; s16buf_t* sbuf = &data->sbuf;
if (sbuf->filled == 0) { if (sbuf->filled == 0) {
ok = decode_block(sf, data); ok = decode_block(sf, data);
if (!ok) goto fail; if (!ok) goto fail;
} }
sbuf_consume(&outbuf, &samples_to_do, sbuf); s16buf_consume(&outbuf, sbuf, &samples_to_do);
} }
return; return;

View File

@ -12,10 +12,10 @@ struct tac_codec_data {
int encoder_delay; int encoder_delay;
uint8_t buf[TAC_BLOCK_SIZE]; uint8_t buf[TAC_BLOCK_SIZE];
int feed_block; bool feed_block;
off_t offset; off_t offset;
int16_t* samples; int16_t samples[TAC_FRAME_SAMPLES * TAC_CHANNELS];
int frame_samples; int frame_samples;
/* frame state */ /* frame state */
@ -38,7 +38,7 @@ tac_codec_data* init_tac(STREAMFILE* sf) {
data->handle = tac_init(data->buf, bytes); data->handle = tac_init(data->buf, bytes);
if (!data->handle) goto fail; 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->offset = bytes;
data->channels = TAC_CHANNELS; data->channels = TAC_CHANNELS;
data->frame_samples = TAC_FRAME_SAMPLES; data->frame_samples = TAC_FRAME_SAMPLES;
@ -46,9 +46,6 @@ tac_codec_data* init_tac(STREAMFILE* sf) {
data->encoder_delay = 0; data->encoder_delay = 0;
data->samples_discard = data->encoder_delay; data->samples_discard = data->encoder_delay;
data->samples = malloc(data->channels * data->frame_samples * sizeof(int16_t));
if (!data->samples) goto fail;
return data; return data;
fail: fail:
free_tac(data); 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; int err;
data->sbuf.samples = data->samples; data->sbuf.samples = data->samples;
data->sbuf.channels = 2; data->sbuf.channels = data->channels;
data->sbuf.filled = 0; data->sbuf.filled = 0;
err = tac_decode_frame(data->handle, data->buf); err = tac_decode_frame(data->handle, data->buf);
if (err == TAC_PROCESS_NEXT_BLOCK) { if (err == TAC_PROCESS_NEXT_BLOCK) {
data->feed_block = 1; data->feed_block = true;
return 1; return true;
} }
if (err == TAC_PROCESS_DONE) { if (err == TAC_PROCESS_DONE) {
VGM_LOG("TAC: process done (EOF) %i\n", err); 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) { if (err != TAC_PROCESS_OK) {
VGM_LOG("TAC: process error %i\n", err); VGM_LOG("TAC: process error %i\n", err);
goto fail; return false;
} }
tac_get_samples_pcm16(data->handle, data->sbuf.samples); tac_get_samples_pcm16(data->handle, data->sbuf.samples);
data->sbuf.filled = data->frame_samples; data->sbuf.filled = data->frame_samples;
return 1; return true;
fail:
return 0;
} }
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 */ /* new block must be read only when signaled by lib */
if (data->feed_block) { if (!data->feed_block)
int bytes = read_streamfile(data->buf, data->offset, sizeof(data->buf), sf); return true;
data->offset += bytes;
data->feed_block = 0; int bytes = read_streamfile(data->buf, data->offset, sizeof(data->buf), sf);
if (bytes <= 0) goto fail; /* can read less that buf near EOF */ data->offset += bytes;
} data->feed_block = 0;
if (bytes <= 0) return false; /* can read less that buf near EOF */
return 1; return true;
fail:
return 0;
} }
void decode_tac(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do) { void decode_tac(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do) {
VGMSTREAMCHANNEL* stream = &vgmstream->ch[0]; VGMSTREAMCHANNEL* stream = &vgmstream->ch[0];
tac_codec_data* data = vgmstream->codec_data; tac_codec_data* data = vgmstream->codec_data;
int ok; bool ok;
while (samples_to_do > 0) { while (samples_to_do > 0) {
@ -132,7 +126,7 @@ void decode_tac(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do) {
fail: fail:
/* on error just put some 0 samples */ /* 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); 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); tac_reset(data->handle);
data->offset = 0; data->feed_block = true;
data->feed_block = 1; data->offset = 0x00;
data->sbuf.filled = 0; data->sbuf.filled = 0;
data->samples_discard = data->encoder_delay; data->samples_discard = data->encoder_delay;
return;
} }
void seek_tac(tac_codec_data* data, int32_t num_sample) { 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) { if (loop_sample == num_sample) {
tac_set_loop(data->handle); /* direct looping */ tac_set_loop(data->handle); /* direct looping */
data->samples_discard = hdr->loop_discard; data->feed_block = true;
data->offset = hdr->loop_offset; data->offset = hdr->loop_offset;
data->feed_block = 1;
data->sbuf.filled = 0; data->sbuf.filled = 0;
data->samples_discard = hdr->loop_discard;
} }
else { else {
tac_reset(data->handle); tac_reset(data->handle);
data->samples_discard = num_sample; data->feed_block = true;
data->offset = 0; data->offset = 0x00;
data->feed_block = 1;
data->sbuf.filled = 0; data->sbuf.filled = 0;
data->samples_discard = num_sample;
} }
} }
@ -182,6 +174,5 @@ void free_tac(tac_codec_data* data) {
return; return;
tac_free(data->handle); tac_free(data->handle);
free(data->samples);
free(data); free(data);
} }

View File

@ -1,7 +1,7 @@
#ifndef _LIBVGMSTREAM_H_ #ifndef _LIBVGMSTREAM_H_
#define _LIBVGMSTREAM_H_ #define _LIBVGMSTREAM_H_
//#define LIBVGMSTREAM_ENABLE 1 #define LIBVGMSTREAM_ENABLE 1
#if LIBVGMSTREAM_ENABLE #if LIBVGMSTREAM_ENABLE
/* libvgmstream: vgmstream's public API /* libvgmstream: vgmstream's public API