Fix some decoding edge cases

This commit is contained in:
bnnm 2020-08-15 16:13:01 +02:00
parent 295e187452
commit fbab0c0905
4 changed files with 28 additions and 14 deletions

View File

@ -5,7 +5,7 @@
/* Decodes samples for flat streams.
* Data forms a single stream, and the decoder may internally skip chunks and move offsets as needed. */
void render_vgmstream_flat(sample_t* buffer, int32_t sample_count, VGMSTREAM* vgmstream) {
void render_vgmstream_flat(sample_t* outbuf, int32_t sample_count, VGMSTREAM* vgmstream) {
int samples_written = 0;
int samples_per_frame, samples_this_block;
@ -25,17 +25,19 @@ void render_vgmstream_flat(sample_t* buffer, int32_t sample_count, VGMSTREAM* vg
if (samples_to_do > sample_count - samples_written)
samples_to_do = sample_count - samples_written;
if (samples_to_do == 0) {
VGM_LOG("layout_flat: wrong samples_to_do 0 found\n"); /* could happen when calling render at EOF? */
//VGM_LOG("layout_flat: tb=%i sib=%i, spf=%i\n", samples_this_block, vgmstream->samples_into_block, samples_per_frame);
memset(buffer + samples_written*vgmstream->channels, 0, (sample_count - samples_written) * vgmstream->channels * sizeof(sample_t));
break;
if (samples_to_do == 0) { /* when decoding more than num_samples */
VGM_LOG("FLAT: samples_to_do 0\n");
goto decode_fail;
}
decode_vgmstream(vgmstream, samples_written, samples_to_do, buffer);
decode_vgmstream(vgmstream, samples_written, samples_to_do, outbuf);
samples_written += samples_to_do;
vgmstream->current_sample += samples_to_do;
vgmstream->samples_into_block += samples_to_do;
}
return;
decode_fail:
memset(outbuf + samples_written * vgmstream->channels, 0, (sample_count - samples_written) * vgmstream->channels * sizeof(sample_t));
}

View File

@ -34,6 +34,10 @@ void render_vgmstream_layered(sample_t* outbuf, int32_t sample_count, VGMSTREAM*
if (samples_to_do > sample_count - samples_written)
samples_to_do = sample_count - samples_written;
if (samples_to_do <= 0) { /* when decoding more than num_samples */
VGM_LOG("LAYERED: samples_to_do 0\n");
goto decode_fail;
}
/* decode all layers */
ch = 0;
@ -65,6 +69,10 @@ void render_vgmstream_layered(sample_t* outbuf, int32_t sample_count, VGMSTREAM*
vgmstream->current_sample += samples_to_do;
vgmstream->samples_into_block += samples_to_do;
}
return;
decode_fail:
memset(outbuf + samples_written * data->output_channels, 0, (sample_count - samples_written) * data->output_channels * sizeof(sample_t));
}

View File

@ -23,7 +23,7 @@ void render_vgmstream_segmented(sample_t* outbuf, int32_t sample_count, VGMSTREA
if (data->current_segment >= data->segment_count) {
VGM_LOG("SEGMENT: wrong current segment\n");
return;
goto decode_fail;
}
samples_this_block = vgmstream_get_samples(data->segments[data->current_segment]);
@ -41,10 +41,9 @@ void render_vgmstream_segmented(sample_t* outbuf, int32_t sample_count, VGMSTREA
if (vgmstream->samples_into_block >= samples_this_block) {
data->current_segment++;
/* could happen on last segment trying to decode more samples */
if (data->current_segment >= data->segment_count) {
VGM_LOG("SEGMENTED: wrong next segment\n");
break;
if (data->current_segment >= data->segment_count) { /* when decoding more than num_samples */
VGM_LOG("SEGMENTED: reached last segment\n");
goto decode_fail;
}
/* in case of looping spanning multiple segments */
@ -62,9 +61,9 @@ void render_vgmstream_segmented(sample_t* outbuf, int32_t sample_count, VGMSTREA
if (samples_to_do > VGMSTREAM_SEGMENT_SAMPLE_BUFFER /*&& use_internal_buffer*/) /* always for fade/etc mixes */
samples_to_do = VGMSTREAM_SEGMENT_SAMPLE_BUFFER;
if (samples_to_do < 0) { /* ? */
if (samples_to_do < 0) { /* 0 is ok? */
VGM_LOG("SEGMENTED: wrong samples_to_do %i found\n", samples_to_do);
break;
goto decode_fail;
}
render_vgmstream(
@ -84,6 +83,10 @@ void render_vgmstream_segmented(sample_t* outbuf, int32_t sample_count, VGMSTREA
vgmstream->current_sample += samples_to_do;
vgmstream->samples_into_block += samples_to_do;
}
return;
decode_fail:
memset(outbuf + samples_written * data->output_channels, 0, (sample_count - samples_written) * data->output_channels * sizeof(sample_t));
}
void loop_layout_segmented(VGMSTREAM* vgmstream, int32_t loop_sample) {

View File

@ -248,6 +248,7 @@ static int render_layout(sample_t* buf, int32_t sample_count, VGMSTREAM* vgmstre
/* current_sample goes between loop points (if looped) or up to max samples,
* must detect beyond that decoders would encounter garbage data */
/* not ">=" to allow layouts to loop in some cases when == happens */
if (vgmstream->current_sample > vgmstream->num_samples) {
int channels = vgmstream->channels;