From 23474dc27c5818333caf8f4756f2716eea0361b6 Mon Sep 17 00:00:00 2001 From: bnnm Date: Thu, 14 Jul 2022 23:08:09 +0200 Subject: [PATCH] Fix missing end samples in Wwise Vorbis --- src/coding/coding.h | 1 + src/coding/vorbis_custom_decoder.c | 17 +++++++++-------- src/meta/fsb5.c | 1 + 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/coding/coding.h b/src/coding/coding.h index e55c6572..3c3fc216 100644 --- a/src/coding/coding.h +++ b/src/coding/coding.h @@ -409,6 +409,7 @@ typedef struct { uint32_t setup_id; /* external setup */ int big_endian; /* flag */ + uint32_t stream_end; /* optional, to avoid overreading into next subsong or chunk */ /* Wwise Vorbis config */ wwise_setup_t setup_type; diff --git a/src/coding/vorbis_custom_decoder.c b/src/coding/vorbis_custom_decoder.c index bf09b662..cfcf12f1 100644 --- a/src/coding/vorbis_custom_decoder.c +++ b/src/coding/vorbis_custom_decoder.c @@ -65,6 +65,9 @@ vorbis_custom_codec_data* init_vorbis_custom(STREAMFILE* sf, off_t start_offset, /* write output */ config->data_start_offset = data->config.data_start_offset; + if (!data->config.stream_end) { + data->config.stream_end = get_streamfile_size(sf); + } return data; @@ -78,19 +81,11 @@ fail: void decode_vorbis_custom(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do, int channels) { VGMSTREAMCHANNEL *stream = &vgmstream->ch[0]; vorbis_custom_codec_data* data = vgmstream->codec_data; - size_t stream_size = get_streamfile_size(stream->streamfile); //data->op.packet = data->buffer;/* implicit from init */ int samples_done = 0; while (samples_done < samples_to_do) { - /* extra EOF check for edge cases */ - if (stream->offset >= stream_size) { - memset(outbuf + samples_done * channels, 0, (samples_to_do - samples_done) * sizeof(sample) * channels); - break; - } - - if (data->samples_full) { /* read more samples */ int samples_to_get; float **pcm; @@ -123,6 +118,12 @@ void decode_vorbis_custom(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t sample else { /* read more data */ int ok, rc; + /* extra EOF check */ + if (stream->offset >= data->config.stream_end) { + /* may need to drain samples? (not a thing in vorbis due to packet types?) */ + goto decode_fail; + } + /* not actually needed, but feels nicer */ data->op.granulepos += samples_to_do; /* can be changed next if desired */ data->op.packetno++; diff --git a/src/meta/fsb5.c b/src/meta/fsb5.c index e9195013..11478bf4 100644 --- a/src/meta/fsb5.c +++ b/src/meta/fsb5.c @@ -477,6 +477,7 @@ VGMSTREAM* init_vgmstream_fsb5(STREAMFILE* sf) { cfg.channels = fsb5.channels; cfg.sample_rate = fsb5.sample_rate; cfg.setup_id = read_u32le(fsb5.extradata_offset,sf); + cfg.stream_end = fsb5.stream_offset + fsb5.stream_size; vgmstream->codec_data = init_vorbis_custom(sb, fsb5.stream_offset, VORBIS_FSB, &cfg); if (!vgmstream->codec_data) goto fail;