Allow blocks with 0 samples (ignored, then next block is called)

Happens in Kakuto Chojin .stx, can be used to simplify some blocks
parsers so they don't have to manually search for next valid block and
can just set 0 samples if empty block is found.

Also move block_update code since it was getting a bit unwieldy.
This commit is contained in:
bnnm 2018-04-12 22:51:24 +02:00
parent dacac7f9b4
commit 6f3522348e

View File

@ -1,6 +1,8 @@
#include "layout.h"
#include "../vgmstream.h"
static void block_update(VGMSTREAM * vgmstream);
void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstream) {
int samples_written=0;
@ -34,12 +36,13 @@ void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM *
}
/* probably block bug or EOF, next calcs would give wrong values and buffer segfaults */
if (samples_this_block <= 0) {
VGM_LOG("layout_blocked: empty/wrong block\n");
if (samples_this_block < 0) {
VGM_LOG("layout_blocked: wrong block at 0x%lx\n", vgmstream->current_block_offset);
memset(buffer + samples_written*vgmstream->channels, 0, (sample_count - samples_written) * vgmstream->channels * sizeof(sample));
break;
break; /* probable infinite loop otherwise */
}
/* samples_this_block = 0 is allowed (empty block), will do nothing then move to next block */
samples_to_do = vgmstream_samples_to_do(samples_this_block, samples_per_frame, vgmstream);
if (samples_written + samples_to_do > sample_count)
@ -49,7 +52,7 @@ void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM *
decode_vgmstream(vgmstream, samples_written, samples_to_do, buffer);
}
else {
/* block end signal (used below): partially 0-set buffer */
/* block end signal (used in halpst): partially 0-set buffer */
int i;
for (i = samples_written*vgmstream->channels; i < (samples_written+samples_to_do)*vgmstream->channels; i++) {
buffer[i]=0;
@ -64,6 +67,29 @@ void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM *
/* move to next block when all samples are consumed */
if (vgmstream->samples_into_block==samples_this_block
/*&& vgmstream->current_sample < vgmstream->num_samples*/) { /* don't go past last block */
block_update(vgmstream);
/* for VBR these may change */
frame_size = get_vgmstream_frame_size(vgmstream);
samples_per_frame = get_vgmstream_samples_per_frame(vgmstream);
/* get samples in the current block */
if (vgmstream->current_block_samples) {
samples_this_block = vgmstream->current_block_samples;
} else if (frame_size == 0) { /* assume 4 bit */ //TODO: get_vgmstream_frame_size() really should return bits... */
samples_this_block = vgmstream->current_block_size * 2 * samples_per_frame;
} else {
samples_this_block = vgmstream->current_block_size / frame_size * samples_per_frame;
}
vgmstream->samples_into_block = 0;
}
}
}
static void block_update(VGMSTREAM * vgmstream) {
switch (vgmstream->layout_type) {
case layout_blocked_ast:
block_update_ast(vgmstream->next_block_offset,vgmstream);
@ -179,22 +205,4 @@ void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM *
default:
break;
}
/* for VBR these may change */
frame_size = get_vgmstream_frame_size(vgmstream); /* for VBR these may change */
samples_per_frame = get_vgmstream_samples_per_frame(vgmstream);
/* get samples in the current block */
if (vgmstream->current_block_samples) {
samples_this_block = vgmstream->current_block_samples;
} else if (frame_size == 0) { /* assume 4 bit */ //TODO: get_vgmstream_frame_size() really should return bits... */
samples_this_block = vgmstream->current_block_size * 2 * samples_per_frame;
} else {
samples_this_block = vgmstream->current_block_size / frame_size * samples_per_frame;
}
vgmstream->samples_into_block = 0;
}
}
}