From ce9d1814449d6ff6323dd1030fb4c8d1093c6744 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Wed, 8 Oct 2025 03:12:19 +0200 Subject: [PATCH] avcodec/mjpegdec: Remove unnecessary reloads Hint: The parts of this patch in decode_block_progressive() and decode_block_refinement() rely on the fact that GET_VLC returns -1 on error, so that it enters the codepaths for actually coded block coefficients. Reviewed-by: Ramiro Polla Signed-off-by: Andreas Rheinhardt --- libavcodec/mjpegdec.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c index 69bc003490..5fd77073da 100644 --- a/libavcodec/mjpegdec.c +++ b/libavcodec/mjpegdec.c @@ -854,9 +854,9 @@ static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, i += ((unsigned)code) >> 4; code &= 0xf; if (code) { - if (code > MIN_CACHE_BITS - 16) - UPDATE_CACHE(re, &s->gb); - + // GET_VLC updates the cache if parsing reaches the second stage. + // So we have at least MIN_CACHE_BITS - 9 > 15 bits left here + // and don't need to refill the cache. { int cache = GET_CACHE(re, &s->gb); int sign = (~cache) >> 31; @@ -918,8 +918,6 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, code &= 0xF; if (code) { i += run; - if (code > MIN_CACHE_BITS - 16) - UPDATE_CACHE(re, &s->gb); { int cache = GET_CACHE(re, &s->gb); @@ -950,7 +948,8 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, } else { val = (1 << run); if (run) { - UPDATE_CACHE(re, &s->gb); + // Given that GET_VLC reloads internally, we always + // have at least 16 bits in the cache here. val += NEG_USR32(GET_CACHE(re, &s->gb), run); LAST_SKIP_BITS(re, &s->gb, run); } @@ -1012,7 +1011,6 @@ static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, if (code & 0xF) { run = ((unsigned) code) >> 4; - UPDATE_CACHE(re, &s->gb); val = SHOW_UBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); ZERO_RUN; @@ -1033,7 +1031,8 @@ static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, val = run; run = (1 << run); if (val) { - UPDATE_CACHE(re, &s->gb); + // Given that GET_VLC reloads internally, we always + // have at least 16 bits in the cache here. run += SHOW_UBITS(re, &s->gb, val); LAST_SKIP_BITS(re, &s->gb, val); }