[FFmpeg-devel] [PATCH 2/4] avcodec/vvc/vvc_ps: Use union for luts to avoid unaligned accesses

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sun Feb 18 21:31:04 EET 2024


These arrays are currently accessed via uint16_t* pointers
although nothing guarantees their alignment. Furthermore,
this is problematic wrt the effective-type rules.
Fix this by using a union of arrays of uint8_t and uint16_t.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/vvc/vvc_filter.c          | 2 +-
 libavcodec/vvc/vvc_filter_template.c | 4 ++--
 libavcodec/vvc/vvc_inter.c           | 4 ++--
 libavcodec/vvc/vvc_ps.c              | 8 ++++----
 libavcodec/vvc/vvc_ps.h              | 7 ++++---
 libavcodec/vvc/vvcdsp.h              | 2 +-
 6 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/libavcodec/vvc/vvc_filter.c b/libavcodec/vvc/vvc_filter.c
index df77e443f6..5fa711c9e0 100644
--- a/libavcodec/vvc/vvc_filter.c
+++ b/libavcodec/vvc/vvc_filter.c
@@ -1328,5 +1328,5 @@ void ff_vvc_lmcs_filter(const VVCLocalContext *lc, const int x, const int y)
     const int height   = FFMIN(fc->ps.pps->height - y, ctb_size);
     uint8_t *data      = fc->frame->data[LUMA] + y * fc->frame->linesize[LUMA] + (x << fc->ps.sps->pixel_shift);
     if (sc->sh.r->sh_lmcs_used_flag)
-        fc->vvcdsp.lmcs.filter(data, fc->frame->linesize[LUMA], width, height, fc->ps.lmcs.inv_lut);
+        fc->vvcdsp.lmcs.filter(data, fc->frame->linesize[LUMA], width, height, &fc->ps.lmcs.inv_lut);
 }
diff --git a/libavcodec/vvc/vvc_filter_template.c b/libavcodec/vvc/vvc_filter_template.c
index b7eaef5125..9b3a0e46f7 100644
--- a/libavcodec/vvc/vvc_filter_template.c
+++ b/libavcodec/vvc/vvc_filter_template.c
@@ -22,9 +22,9 @@
 
 #include "libavcodec/h26x/h2656_sao_template.c"
 
-static void FUNC(lmcs_filter_luma)(uint8_t *_dst, ptrdiff_t dst_stride, const int width, const int height, const uint8_t *_lut)
+static void FUNC(lmcs_filter_luma)(uint8_t *_dst, ptrdiff_t dst_stride, const int width, const int height, const void *_lut)
 {
-    const pixel *lut = (const pixel *)_lut;
+    const pixel *lut = _lut;
     pixel *dst = (pixel*)_dst;
     dst_stride /= sizeof(pixel);
 
diff --git a/libavcodec/vvc/vvc_inter.c b/libavcodec/vvc/vvc_inter.c
index e05f3db93e..6c9c8a7165 100644
--- a/libavcodec/vvc/vvc_inter.c
+++ b/libavcodec/vvc/vvc_inter.c
@@ -571,7 +571,7 @@ static void pred_regular_luma(VVCLocalContext *lc, const int hf_idx, const int v
         const int intra_weight = ciip_derive_intra_weight(lc, x0, y0, sbw, sbh);
         fc->vvcdsp.intra.intra_pred(lc, x0, y0, sbw, sbh, 0);
         if (sc->sh.r->sh_lmcs_used_flag)
-            fc->vvcdsp.lmcs.filter(inter, inter_stride, sbw, sbh, fc->ps.lmcs.fwd_lut);
+            fc->vvcdsp.lmcs.filter(inter, inter_stride, sbw, sbh, &fc->ps.lmcs.fwd_lut);
         fc->vvcdsp.inter.put_ciip(dst, dst_stride, sbw, sbh, inter, inter_stride, intra_weight);
 
     }
@@ -887,7 +887,7 @@ static void predict_inter(VVCLocalContext *lc)
 
     if (lc->sc->sh.r->sh_lmcs_used_flag && !cu->ciip_flag) {
         uint8_t* dst0 = POS(0, cu->x0, cu->y0);
-        fc->vvcdsp.lmcs.filter(dst0, fc->frame->linesize[LUMA], cu->cb_width, cu->cb_height, fc->ps.lmcs.fwd_lut);
+        fc->vvcdsp.lmcs.filter(dst0, fc->frame->linesize[LUMA], cu->cb_width, cu->cb_height, &fc->ps.lmcs.fwd_lut);
     }
 }
 
diff --git a/libavcodec/vvc/vvc_ps.c b/libavcodec/vvc/vvc_ps.c
index 376027ed81..e6e46d2039 100644
--- a/libavcodec/vvc/vvc_ps.c
+++ b/libavcodec/vvc/vvc_ps.c
@@ -642,9 +642,9 @@ static int lmcs_derive_lut(VVCLMCS *lmcs, const H266RawAPS *rlmcs, const H266Raw
         const uint16_t fwd_sample = lmcs_derive_lut_sample(sample, lmcs->pivot,
             input_pivot, scale_coeff, idx_y, max);
         if (bit_depth > 8)
-            ((uint16_t *)lmcs->fwd_lut)[sample] = fwd_sample;
+            lmcs->fwd_lut.u16[sample] = fwd_sample;
         else
-            lmcs->fwd_lut[sample] = fwd_sample;
+            lmcs->fwd_lut.u8 [sample] = fwd_sample;
 
     }
 
@@ -659,9 +659,9 @@ static int lmcs_derive_lut(VVCLMCS *lmcs, const H266RawAPS *rlmcs, const H266Raw
             inv_scale_coeff, i, max);
 
         if (bit_depth > 8)
-            ((uint16_t *)lmcs->inv_lut)[sample] = inv_sample;
+            lmcs->inv_lut.u16[sample] = inv_sample;
         else
-            lmcs->inv_lut[sample] = inv_sample;
+            lmcs->inv_lut.u8 [sample] = inv_sample;
     }
 
     return 0;
diff --git a/libavcodec/vvc/vvc_ps.h b/libavcodec/vvc/vvc_ps.h
index 3d3aa061f5..5adf3f3453 100644
--- a/libavcodec/vvc/vvc_ps.h
+++ b/libavcodec/vvc/vvc_ps.h
@@ -191,9 +191,10 @@ typedef struct VVCLMCS {
     uint8_t  min_bin_idx;
     uint8_t  max_bin_idx;
 
-    //*2 for high depth
-    uint8_t  fwd_lut[LMCS_MAX_LUT_SIZE * 2];
-    uint8_t  inv_lut[LMCS_MAX_LUT_SIZE * 2];
+    union {
+        uint8_t  u8[LMCS_MAX_LUT_SIZE];
+        uint16_t u16[LMCS_MAX_LUT_SIZE]; ///< for high bit-depth
+    } fwd_lut, inv_lut;
 
     uint16_t pivot[LMCS_MAX_BIN_SIZE + 1];
     uint16_t chroma_scale_coeff[LMCS_MAX_BIN_SIZE];
diff --git a/libavcodec/vvc/vvcdsp.h b/libavcodec/vvc/vvcdsp.h
index 6f59e73654..f4fb3cb7d7 100644
--- a/libavcodec/vvc/vvcdsp.h
+++ b/libavcodec/vvc/vvcdsp.h
@@ -119,7 +119,7 @@ typedef struct VVCItxDSPContext {
 } VVCItxDSPContext;
 
 typedef struct VVCLMCSDSPContext {
-    void (*filter)(uint8_t *dst, ptrdiff_t dst_stride, int width, int height, const uint8_t *lut);
+    void (*filter)(uint8_t *dst, ptrdiff_t dst_stride, int width, int height, const void *lut);
 } VVCLMCSDSPContext;
 
 typedef struct VVCLFDSPContext {
-- 
2.34.1



More information about the ffmpeg-devel mailing list