[FFmpeg-cvslog] avcodec/hevc/hevcdec: Use bitfield instead of array of flags

Andreas Rheinhardt git at videolan.org
Wed Apr 2 10:37:36 EEST 2025


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Sun Mar 30 14:35:04 2025 +0200| [0ce8868878c90153e45648b180cef59a9bcb0109] | committer: Andreas Rheinhardt

avcodec/hevc/hevcdec: Use bitfield instead of array of flags

It is simpler, avoids several loops and also makes GCC no longer
emit bogus -Wstringop-overflow= warnings.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=0ce8868878c90153e45648b180cef59a9bcb0109
---

 libavcodec/hevc/hevcdec.c | 55 ++++++++++++++++-------------------------------
 1 file changed, 19 insertions(+), 36 deletions(-)

diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c
index e0ca1b9690..83acf3511f 100644
--- a/libavcodec/hevc/hevcdec.c
+++ b/libavcodec/hevc/hevcdec.c
@@ -174,11 +174,8 @@ static int pred_weight_table(SliceHeader *sh, void *logctx,
 {
     int i = 0;
     int j = 0;
-    uint8_t luma_weight_l0_flag[16];
-    uint8_t chroma_weight_l0_flag[16];
-    uint8_t luma_weight_l1_flag[16];
-    uint8_t chroma_weight_l1_flag[16];
     int luma_log2_weight_denom;
+    unsigned luma_weight_flags, chroma_weight_flags;
 
     luma_log2_weight_denom = get_ue_golomb_long(gb);
     if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
@@ -195,29 +192,22 @@ static int pred_weight_table(SliceHeader *sh, void *logctx,
         sh->chroma_log2_weight_denom = chroma_log2_weight_denom;
     }
 
+    luma_weight_flags   = get_bits(gb, sh->nb_refs[L0]);
+    chroma_weight_flags = sps->chroma_format_idc != 0 ? get_bits(gb, sh->nb_refs[L0]) : 0;
     for (i = 0; i < sh->nb_refs[L0]; i++) {
-        luma_weight_l0_flag[i] = get_bits1(gb);
-        if (!luma_weight_l0_flag[i]) {
-            sh->luma_weight_l0[i] = 1 << sh->luma_log2_weight_denom;
-            sh->luma_offset_l0[i] = 0;
-        }
-    }
-    if (sps->chroma_format_idc != 0) {
-        for (i = 0; i < sh->nb_refs[L0]; i++)
-            chroma_weight_l0_flag[i] = get_bits1(gb);
-    } else {
-        for (i = 0; i < sh->nb_refs[L0]; i++)
-            chroma_weight_l0_flag[i] = 0;
-    }
-    for (i = 0; i < sh->nb_refs[L0]; i++) {
-        if (luma_weight_l0_flag[i]) {
+        unsigned flag_bit = 1 << (sh->nb_refs[L0] - 1 - i);
+
+        if (luma_weight_flags & flag_bit) {
             int delta_luma_weight_l0 = get_se_golomb(gb);
             if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
                 return AVERROR_INVALIDDATA;
             sh->luma_weight_l0[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l0;
             sh->luma_offset_l0[i] = get_se_golomb(gb);
+        } else {
+            sh->luma_weight_l0[i] = 1 << sh->luma_log2_weight_denom;
+            sh->luma_offset_l0[i] = 0;
         }
-        if (chroma_weight_l0_flag[i]) {
+        if (chroma_weight_flags & flag_bit) {
             for (j = 0; j < 2; j++) {
                 int delta_chroma_weight_l0 = get_se_golomb(gb);
                 int delta_chroma_offset_l0 = get_se_golomb(gb);
@@ -239,29 +229,22 @@ static int pred_weight_table(SliceHeader *sh, void *logctx,
         }
     }
     if (sh->slice_type == HEVC_SLICE_B) {
+        luma_weight_flags   = get_bits(gb, sh->nb_refs[L1]);
+        chroma_weight_flags = sps->chroma_format_idc != 0 ? get_bits(gb, sh->nb_refs[L1]) : 0;
         for (i = 0; i < sh->nb_refs[L1]; i++) {
-            luma_weight_l1_flag[i] = get_bits1(gb);
-            if (!luma_weight_l1_flag[i]) {
-                sh->luma_weight_l1[i] = 1 << sh->luma_log2_weight_denom;
-                sh->luma_offset_l1[i] = 0;
-            }
-        }
-        if (sps->chroma_format_idc != 0) {
-            for (i = 0; i < sh->nb_refs[L1]; i++)
-                chroma_weight_l1_flag[i] = get_bits1(gb);
-        } else {
-            for (i = 0; i < sh->nb_refs[L1]; i++)
-                chroma_weight_l1_flag[i] = 0;
-        }
-        for (i = 0; i < sh->nb_refs[L1]; i++) {
-            if (luma_weight_l1_flag[i]) {
+            unsigned flag_bit = 1 << (sh->nb_refs[L1] - 1 - i);
+
+            if (luma_weight_flags & flag_bit) {
                 int delta_luma_weight_l1 = get_se_golomb(gb);
                 if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
                     return AVERROR_INVALIDDATA;
                 sh->luma_weight_l1[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l1;
                 sh->luma_offset_l1[i] = get_se_golomb(gb);
+            } else {
+                sh->luma_weight_l1[i] = 1 << sh->luma_log2_weight_denom;
+                sh->luma_offset_l1[i] = 0;
             }
-            if (chroma_weight_l1_flag[i]) {
+            if (chroma_weight_flags & flag_bit) {
                 for (j = 0; j < 2; j++) {
                     int delta_chroma_weight_l1 = get_se_golomb(gb);
                     int delta_chroma_offset_l1 = get_se_golomb(gb);



More information about the ffmpeg-cvslog mailing list