avcodec/huffyuvenc: Calculate mask only once
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>
Wed, 25 Feb 2026 19:00:46 +0000 (20:00 +0100)
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>
Sun, 1 Mar 2026 10:56:57 +0000 (11:56 +0100)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
libavcodec/huffyuvenc.c

index 0f6b18f0f6ae62dcd3eca71b71751b62f8d4d93d..5941b53bd7cf5d72d632e74f9801ffe56d435425 100644 (file)
@@ -55,7 +55,7 @@ typedef struct HYuvEncContext {
     int bitstream_bpp;
     int version;
     int bps;
-    int n;                                  // 1<<bps
+    unsigned mask;                          // (1<<bps)-1
     int vlc_n;                              // number of vlc codes (FFMIN(1<<bps, MAX_VLC_N))
     int alpha;
     int chroma;
@@ -85,7 +85,7 @@ static inline void diff_bytes(HYuvEncContext *s, uint8_t *dst,
     if (s->bps <= 8) {
         s->llvidencdsp.diff_bytes(dst, src0, src1, w);
     } else {
-        s->hencdsp.diff_int16((uint16_t *)dst, (const uint16_t *)src0, (const uint16_t *)src1, s->n - 1, w);
+        s->hencdsp.diff_int16((uint16_t *)dst, (const uint16_t *)src0, (const uint16_t *)src1, s->mask, w);
     }
 }
 
@@ -115,7 +115,7 @@ static inline int sub_left_prediction(HYuvEncContext *s, uint8_t *dst,
         }
         if (w < 32)
             return left;
-        s->hencdsp.diff_int16(dst16 + 32, src16 + 32, src16 + 31, s->n - 1, w - 32);
+        s->hencdsp.diff_int16(dst16 + 32, src16 + 32, src16 + 31, s->mask, w - 32);
         return src16[w-1];
     }
 }
@@ -191,7 +191,8 @@ static void sub_median_prediction(HYuvEncContext *s, uint8_t *dst,
     if (s->bps <= 8) {
         s->llvidencdsp.sub_median_pred(dst, src1, src2, w , left, left_top);
     } else {
-        s->hencdsp.sub_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src1, (const uint16_t *)src2, s->n - 1, w , left, left_top);
+        s->hencdsp.sub_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src1,
+                                              (const uint16_t *)src2, s->mask, w, left, left_top);
     }
 }
 
@@ -275,6 +276,9 @@ static av_cold int encode_init(AVCodecContext *avctx)
     s->chroma_h_shift = desc->log2_chroma_w;
     s->chroma_v_shift = desc->log2_chroma_h;
 
+    s->mask  = (1 << s->bps) - 1;
+    s->vlc_n = FFMIN(1 << s->bps, MAX_VLC_N);
+
     switch (avctx->pix_fmt) {
     case AV_PIX_FMT_YUV420P:
     case AV_PIX_FMT_YUV422P:
@@ -336,8 +340,6 @@ static av_cold int encode_init(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_ERROR, "format not supported\n");
         return AVERROR(EINVAL);
     }
-    s->n = 1<<s->bps;
-    s->vlc_n = FFMIN(s->n, MAX_VLC_N);
 
     avctx->bits_per_coded_sample = s->bitstream_bpp;
     s->decorrelate = s->bitstream_bpp >= 24 && !s->yuv && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
@@ -588,7 +590,7 @@ do {                                                                  \
     if (s->bps <= 8) {
         ENCODE_PLANE(LOAD2, LOADEND, WRITE2, WRITEEND, STAT2, STATEND);
     } else if (s->bps <= 14) {
-        int mask = s->n - 1;
+        unsigned mask = s->mask;
 
         ENCODE_PLANE(LOAD2_14, LOADEND_14, WRITE2, WRITEEND, STAT2, STATEND);
     } else {