From 030249f713eef90cdd68a86a71f935ea4de8030b Mon Sep 17 00:00:00 2001 From: eukara Date: Wed, 26 Jul 2023 12:12:55 -0700 Subject: [PATCH] =?UTF-8?q?Workaround=20for=20the=20OGG=20decoding=20issue?= =?UTF-8?q?.=205159a80=20was=20bad=20for=20ogg=20vorbis=E2=80=A6=20(#206)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Workaround for the OGG decoding issue. 5159a80 was bad for ogg vorbis decoding. It was never ideal to begin with, but it's better than rendering the game unplayable. * Concentrated patch, courtesy of Spike so we can still mark sounds as nopurge --- engine/client/snd_ov.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/engine/client/snd_ov.c b/engine/client/snd_ov.c index e10686b1b..3c71c8ee3 100644 --- a/engine/client/snd_ov.c +++ b/engine/client/snd_ov.c @@ -86,11 +86,10 @@ static float QDECL OV_Query(struct sfx_s *sfx, struct sfxcache_s *buf, char *nam if (!dec) return -1; - if (dec->timetotal < 0) - { - dec->pcmtotal = p_ov_pcm_total(&dec->vf, -1); - dec->timetotal = p_ov_time_total(&dec->vf, -1); - } +// if (dec->pcmtotal == 0) +// dec->pcmtotal = p_ov_pcm_total(&dec->vf, -1); +// if (dec->timetotal < 0) +// dec->timetotal = p_ov_time_total(&dec->vf, -1); if (buf) { @@ -199,9 +198,9 @@ static sfxcache_t *QDECL OV_DecodeSome(struct sfx_s *sfx, struct sfxcache_s *buf if (dec->failed || start+length <= dec->decodedbytestart + dec->decodedbytecount) break; - if (dec->decodedbufferbytes < start+length - dec->decodedbytestart + 4096) //expand if needed. 4096 seems to be the recommended size. + if (dec->decodedbufferbytes < start+length - dec->decodedbytestart + 4096 && !dec->nopurge) //expand if needed. 4096 seems to be the recommended size. { - // Con_Printf("Expand buffer\n"); +// Con_Printf("Expand buffer for %s\n", sfx->name); dec->decodedbufferbytes = (start+length - dec->decodedbytestart) + max(outspeed, 4096); //over allocate, because we can. dec->decodedbuffer = BZ_Realloc(dec->decodedbuffer, dec->decodedbufferbytes); } @@ -489,8 +488,21 @@ static qboolean OV_StartDecode(unsigned char *start, unsigned long length, ovdec buffer->start = BZ_Malloc(length); memcpy(buffer->start, start, length); - buffer->timetotal = -1; + buffer->pcmtotal = p_ov_pcm_total(&buffer->vf, -1); + buffer->timetotal = buffer->pcmtotal / (float)buffer->srcspeed; + if (buffer->timetotal < 5) //short sounds might as well remain cached. + buffer->nopurge = true; + + if (buffer->nopurge) + { //waste more memory upfront to avoid reallocs later. + buffer->decodedbufferbytes = buffer->pcmtotal; + buffer->decodedbufferbytes += 4096; //ogg vorbis apparently lies/fails sometimes. + buffer->decodedbufferbytes *= (double)snd_speed / buffer->srcspeed; //from src rate to dst rate + buffer->decodedbufferbytes *= 2*buffer->srcchannels; //convert from frames to bytes. + buffer->decodedbufferbytes += 4096; //just general paranoia. + buffer->decodedbuffer = BZ_Realloc(buffer->decodedbuffer, buffer->decodedbufferbytes); + } return true; } @@ -533,9 +545,6 @@ qboolean QDECL S_LoadOVSound (sfx_t *s, qbyte *data, size_t datalen, int sndspee s->decoder.decodedata(s, NULL, 0, 100); - if (p_ov_time_total(&buffer->vf, -1) < 5) //short sounds might as well remain cached. - buffer->nopurge = true; - return true; }