>From 5d7f8f0ffbaf7035666377c29a6862c0ec19a5b4 Mon Sep 17 00:00:00 2001
From: Raimo33 <[email protected]>
Date: Sat, 9 May 2026 22:44:09 +0200
Subject: [PATCH] avcodec/vvc/filter_template: optimize alf gradient loop

Use local scalar accumulators to prevent potential pointer aliasing between the 
'sum' array and the 'grad' pointer. This allows the compiler to fully unroll 
and auto-vectorize the gradient accumulation loop.

Signed-off-by: Raimo33 <[email protected]>
---
 libavcodec/vvc/filter_template.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libavcodec/vvc/filter_template.c b/libavcodec/vvc/filter_template.c
index 6dd73100897..ee199ac1048 100644
--- a/libavcodec/vvc/filter_template.c
+++ b/libavcodec/vvc/filter_template.c
@@ -347,20 +347,20 @@ static void FUNC(alf_classify)(int *class_idx, int 
*transpose_idx,
         for (int x = 0; x < width; x += ALF_BLOCK_SIZE) {
             const int xg = x / ALF_GRADIENT_STEP;
             const int yg = y / ALF_GRADIENT_STEP;
-            int sum[ALF_NUM_DIR] = { 0 };
+            int sum_v = 0, sum_h = 0, sum_d0 = 0, sum_d1 = 0;
 
             grad = gradient_tmp + (yg + start) * gstride + xg * ALF_NUM_DIR;
-            //todo: optimize this loop
             for (int i = start; i < end; i++) {
                 for (int j = 0; j < size; j++) {
-                    sum[ALF_DIR_VERT]  += grad[ALF_DIR_VERT];
-                    sum[ALF_DIR_HORZ]  += grad[ALF_DIR_HORZ];
-                    sum[ALF_DIR_DIGA0] += grad[ALF_DIR_DIGA0];
-                    sum[ALF_DIR_DIGA1] += grad[ALF_DIR_DIGA1];
+                    sum_v  += grad[ALF_DIR_VERT];
+                    sum_h  += grad[ALF_DIR_HORZ];
+                    sum_d0 += grad[ALF_DIR_DIGA0];
+                    sum_d1 += grad[ALF_DIR_DIGA1];
                     grad += ALF_NUM_DIR;
                 }
                 grad += gstride - size * ALF_NUM_DIR;
             }
+            int sum[ALF_NUM_DIR] = { sum_v, sum_h, sum_d0, sum_d1 };
             FUNC(alf_get_idx)(class_idx, transpose_idx, sum, ac);
 
             class_idx++;
-- 
2.43.0


_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to