[x265] [PATCH 2/7] Move duplicated signOf function to common header

Hari Limaye hari.limaye at arm.com
Mon May 20 16:16:38 UTC 2024


Function signOf is duplicated between a few source files, so remove
the duplication and define it in common.h header file.
---
 source/common/aarch64/loopfilter-prim.cpp | 19 +++---
 source/common/common.h                    |  6 ++
 source/common/loopfilter.cpp              | 16 ++---
 source/encoder/sao.cpp                    | 74 +++++++++++------------
 4 files changed, 52 insertions(+), 63 deletions(-)

diff --git a/source/common/aarch64/loopfilter-prim.cpp b/source/common/aarch64/loopfilter-prim.cpp
index cb6ad4cd9..37679c0b6 100644
--- a/source/common/aarch64/loopfilter-prim.cpp
+++ b/source/common/aarch64/loopfilter-prim.cpp
@@ -1,3 +1,4 @@
+#include "common.h"
 #include "loopfilter-prim.h"
 
 #define PIXEL_MIN 0
@@ -11,12 +12,6 @@ namespace
 {
 
 
-/* get the sign of input variable (TODO: this is a dup, make common) */
-static inline int8_t signOf(int x)
-{
-    return (x >> 31) | ((int)((((uint32_t) - x)) >> 31));
-}
-
 static inline int8x8_t sign_diff_neon(const uint8x8_t in0, const uint8x8_t in1)
 {
     int16x8_t in = vsubl_u8(in0, in1);
@@ -33,7 +28,7 @@ static void calSign_neon(int8_t *dst, const pixel *src1, const pixel *src2, cons
 
     for (; x < endX; x++)
     {
-        dst[x] = signOf(src1[x] - src2[x]);
+        dst[x] = x265_signOf(src1[x] - src2[x]);
     }
 }
 
@@ -108,7 +103,7 @@ static void processSaoCUE1_neon(pixel *rec, int8_t *upBuff1, int8_t *offsetEo, i
     }
     for (; x < width; x++)
     {
-        signDown = signOf(rec[x] - rec[x + stride]);
+        signDown = x265_signOf(rec[x] - rec[x + stride]);
         edgeType = signDown + upBuff1[x] + 2;
         upBuff1[x] = -signDown;
         rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);
@@ -144,7 +139,7 @@ static void processSaoCUE1_2Rows_neon(pixel *rec, int8_t *upBuff1, int8_t *offse
         }
         for (; x < width; x++)
         {
-            signDown = signOf(rec[x] - rec[x + stride]);
+            signDown = x265_signOf(rec[x] - rec[x + stride]);
             edgeType = signDown + upBuff1[x] + 2;
             upBuff1[x] = -signDown;
             rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);
@@ -161,7 +156,7 @@ static void processSaoCUE2_neon(pixel *rec, int8_t *bufft, int8_t *buff1, int8_t
     {
         for (x = 0; x < width; x++)
         {
-            int8_t signDown = signOf(rec[x] - rec[x + stride + 1]);
+            int8_t signDown = x265_signOf(rec[x] - rec[x + stride + 1]);
             int edgeType = signDown + buff1[x] + 2;
             bufft[x + 1] = -signDown;
             rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);;
@@ -186,7 +181,7 @@ static void processSaoCUE2_neon(pixel *rec, int8_t *bufft, int8_t *buff1, int8_t
         }
         for (; x < width; x++)
         {
-            int8_t signDown = signOf(rec[x] - rec[x + stride + 1]);
+            int8_t signDown = x265_signOf(rec[x] - rec[x + stride + 1]);
             int edgeType = signDown + buff1[x] + 2;
             bufft[x + 1] = -signDown;
             rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);;
@@ -219,7 +214,7 @@ static void processSaoCUE3_neon(pixel *rec, int8_t *upBuff1, int8_t *offsetEo, i
     }
     for (; x < endX; x++)
     {
-        signDown = signOf(rec[x] - rec[x + stride]);
+        signDown = x265_signOf(rec[x] - rec[x + stride]);
         edgeType = signDown + upBuff1[x] + 2;
         upBuff1[x - 1] = -signDown;
         rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);
diff --git a/source/common/common.h b/source/common/common.h
index 37c19ae72..43025df06 100644
--- a/source/common/common.h
+++ b/source/common/common.h
@@ -176,6 +176,12 @@ inline T x265_clip3(T minVal, T maxVal, T a) { return x265_min(x265_max(minVal,
 template<typename T> /* clip to pixel range, 0..255 or 0..1023 */
 inline pixel x265_clip(T x) { return (pixel)x265_min<T>(T((1 << X265_DEPTH) - 1), x265_max<T>(T(0), x)); }
 
+/* get the sign of input variable */
+static inline int8_t x265_signOf(int32_t x)
+{
+    return (x >> 31) | ((int32_t)((((uint32_t) - x)) >> 31));
+}
+
 typedef int16_t  coeff_t;      // transform coefficient
 
 #define X265_MIN(a, b) ((a) < (b) ? (a) : (b))
diff --git a/source/common/loopfilter.cpp b/source/common/loopfilter.cpp
index 8651390d2..f4cd65389 100644
--- a/source/common/loopfilter.cpp
+++ b/source/common/loopfilter.cpp
@@ -30,16 +30,10 @@
 
 namespace {
 
-/* get the sign of input variable (TODO: this is a dup, make common) */
-inline int8_t signOf(int x)
-{
-    return (x >> 31) | ((int)((((uint32_t)-x)) >> 31));
-}
-
 static void calSign(int8_t *dst, const pixel *src1, const pixel *src2, const int endX)
 {
     for (int x = 0; x < endX; x++)
-        dst[x] = signOf(src1[x] - src2[x]);
+        dst[x] = x265_signOf(src1[x] - src2[x]);
 }
 
 static void processSaoCUE0(pixel * rec, int8_t * offsetEo, int width, int8_t* signLeft, intptr_t stride)
@@ -70,7 +64,7 @@ static void processSaoCUE1(pixel* rec, int8_t* upBuff1, int8_t* offsetEo, intptr
 
     for (x = 0; x < width; x++)
     {
-        signDown = signOf(rec[x] - rec[x + stride]);
+        signDown = x265_signOf(rec[x] - rec[x + stride]);
         edgeType = signDown + upBuff1[x] + 2;
         upBuff1[x] = -signDown;
         rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);
@@ -87,7 +81,7 @@ static void processSaoCUE1_2Rows(pixel* rec, int8_t* upBuff1, int8_t* offsetEo,
     {
         for (x = 0; x < width; x++)
         {
-            signDown = signOf(rec[x] - rec[x + stride]);
+            signDown = x265_signOf(rec[x] - rec[x + stride]);
             edgeType = signDown + upBuff1[x] + 2;
             upBuff1[x] = -signDown;
             rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);
@@ -101,7 +95,7 @@ static void processSaoCUE2(pixel * rec, int8_t * bufft, int8_t * buff1, int8_t *
     int x;
     for (x = 0; x < width; x++)
     {
-        int8_t signDown = signOf(rec[x] - rec[x + stride + 1]);
+        int8_t signDown = x265_signOf(rec[x] - rec[x + stride + 1]);
         int edgeType = signDown + buff1[x] + 2;
         bufft[x + 1] = -signDown;
         rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);;
@@ -115,7 +109,7 @@ static void processSaoCUE3(pixel *rec, int8_t *upBuff1, int8_t *offsetEo, intptr
 
     for (int x = startX + 1; x < endX; x++)
     {
-        signDown = signOf(rec[x] - rec[x + stride]);
+        signDown = x265_signOf(rec[x] - rec[x + stride]);
         edgeType = signDown + upBuff1[x] + 2;
         upBuff1[x - 1] = -signDown;
         rec[x] = x265_clip(rec[x] + offsetEo[edgeType]);
diff --git a/source/encoder/sao.cpp b/source/encoder/sao.cpp
index 0c46ece53..105ec79de 100644
--- a/source/encoder/sao.cpp
+++ b/source/encoder/sao.cpp
@@ -36,12 +36,6 @@ inline int32_t roundIBDI(int32_t num, int32_t den)
     return num >= 0 ? ((num * 2 + den) / (den * 2)) : -((-num * 2 + den) / (den * 2));
 }
 
-/* get the sign of input variable (TODO: this is a dup, make common) */
-inline int8_t signOf(int x)
-{
-    return (x >> 31) | ((int)((((uint32_t)-x)) >> 31));
-}
-
 inline int signOf2(const int a, const int b)
 {
     // NOTE: don't reorder below compare, both ICL, VC, GCC optimize strong depends on order!
@@ -328,10 +322,10 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
         {
             for (int y = 0; y < ctuHeight; y++, rec += stride)
             {
-                int signLeft = signOf(rec[startX] - tmpL[y]);
+                int signLeft = x265_signOf(rec[startX] - tmpL[y]);
                 for (int x = startX; x < endX; x++)
                 {
-                    int signRight = signOf(rec[x] - rec[x + 1]);
+                    int signRight = x265_signOf(rec[x] - rec[x + 1]);
                     int edgeType = signRight + signLeft + 2;
                     signLeft = -signRight;
 
@@ -343,8 +337,8 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
         {
             for (int y = 0; y < ctuHeight; y += 2, rec += 2 * stride)
             {
-                signLeft1[0] = signOf(rec[startX] - tmpL[y]);
-                signLeft1[1] = signOf(rec[stride + startX] - tmpL[y + 1]);
+                signLeft1[0] = x265_signOf(rec[startX] - tmpL[y]);
+                signLeft1[1] = x265_signOf(rec[stride + startX] - tmpL[y + 1]);
 
                 if (!lpelx)
                 {
@@ -385,13 +379,13 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
         if (ctuWidth & 15)
         {
             for (int x = 0; x < ctuWidth; x++)
-                upBuff1[x] = signOf(rec[x] - tmpU[x]);
+                upBuff1[x] = x265_signOf(rec[x] - tmpU[x]);
 
             for (int y = startY; y < endY; y++, rec += stride)
             {
                 for (int x = 0; x < ctuWidth; x++)
                 {
-                    int8_t signDown = signOf(rec[x] - rec[x + stride]);
+                    int8_t signDown = x265_signOf(rec[x] - rec[x + stride]);
                     int edgeType = signDown + upBuff1[x] + 2;
                     upBuff1[x] = -signDown;
 
@@ -445,17 +439,17 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
         else
         {
             for (int x = startX; x < endX; x++)
-                upBuff1[x] = signOf(rec[x] - tmpU[x - 1]);
+                upBuff1[x] = x265_signOf(rec[x] - tmpU[x - 1]);
         }
 
         if (ctuWidth & 15)
         {
              for (int y = startY; y < endY; y++, rec += stride)
              {
-                 upBufft[startX] = signOf(rec[stride + startX] - tmpL[y]);
+                 upBufft[startX] = x265_signOf(rec[stride + startX] - tmpL[y]);
                  for (int x = startX; x < endX; x++)
                  {
-                     int8_t signDown = signOf(rec[x] - rec[x + stride + 1]);
+                     int8_t signDown = x265_signOf(rec[x] - rec[x + stride + 1]);
                      int edgeType = signDown + upBuff1[x] + 2;
                      upBufft[x + 1] = -signDown;
                      rec[x] = m_clipTable[rec[x] + offsetEo[edgeType]];
@@ -468,7 +462,7 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
         {
             for (int y = startY; y < endY; y++, rec += stride)
             {
-                int8_t iSignDown2 = signOf(rec[stride + startX] - tmpL[y]);
+                int8_t iSignDown2 = x265_signOf(rec[stride + startX] - tmpL[y]);
 
                 primitives.saoCuOrgE2[endX > 16](rec + startX, upBufft + startX, upBuff1 + startX, offsetEo, endX - startX, stride);
 
@@ -493,25 +487,25 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
         if (ctuWidth & 15)
         {
             for (int x = startX - 1; x < endX; x++)
-                upBuff1[x] = signOf(rec[x] - tmpU[x + 1]);
+                upBuff1[x] = x265_signOf(rec[x] - tmpU[x + 1]);
 
             for (int y = startY; y < endY; y++, rec += stride)
             {
                 int x = startX;
-                int8_t signDown = signOf(rec[x] - tmpL[y + 1]);
+                int8_t signDown = x265_signOf(rec[x] - tmpL[y + 1]);
                 int edgeType = signDown + upBuff1[x] + 2;
                 upBuff1[x - 1] = -signDown;
                 rec[x] = m_clipTable[rec[x] + offsetEo[edgeType]];
 
                 for (x = startX + 1; x < endX; x++)
                 {
-                    signDown = signOf(rec[x] - rec[x + stride - 1]);
+                    signDown = x265_signOf(rec[x] - rec[x + stride - 1]);
                     edgeType = signDown + upBuff1[x] + 2;
                     upBuff1[x - 1] = -signDown;
                     rec[x] = m_clipTable[rec[x] + offsetEo[edgeType]];
                 }
 
-                upBuff1[endX - 1] = signOf(rec[endX - 1 + stride] - rec[endX]);
+                upBuff1[endX - 1] = x265_signOf(rec[endX - 1 + stride] - rec[endX]);
             }
         }
         else
@@ -519,7 +513,7 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
             int8_t firstSign, lastSign;
 
             if (lpelx)
-                firstSign = signOf(rec[-1] - tmpU[0]);
+                firstSign = x265_signOf(rec[-1] - tmpU[0]);
             if (rpelx == picWidth)
                 lastSign = upBuff1[ctuWidth - 1];
 
@@ -533,14 +527,14 @@ void SAO::applyPixelOffsets(int addr, int typeIdx, int plane)
             for (int y = startY; y < endY; y++, rec += stride)
             {
                 int x = startX;
-                int8_t signDown = signOf(rec[x] - tmpL[y + 1]);
+                int8_t signDown = x265_signOf(rec[x] - tmpL[y + 1]);
                 int edgeType = signDown + upBuff1[x] + 2;
                 upBuff1[x - 1] = -signDown;
                 rec[x] = m_clipTable[rec[x] + offsetEo[edgeType]];
 
                 primitives.saoCuOrgE3[endX > 16](rec, upBuff1, offsetEo, stride - 1, startX, endX);
 
-                upBuff1[endX - 1] = signOf(rec[endX - 1 + stride] - rec[endX]);
+                upBuff1[endX - 1] = x265_signOf(rec[endX - 1 + stride] - rec[endX]);
             }
         }
 
@@ -1030,10 +1024,10 @@ void SAO::calcSaoStatsCu_BeforeDblk(Frame* frame, int idxX, int idxY)
             for (y = 0; y < ctuHeight; y++)
             {
                 x = (y < startY ? startX : firstX);
-                int signLeft = signOf(rec[x] - rec[x - 1]);
+                int signLeft = x265_signOf(rec[x] - rec[x - 1]);
                 for (; x < endX; x++)
                 {
-                    int signRight = signOf(rec[x] - rec[x + 1]);
+                    int signRight = x265_signOf(rec[x] - rec[x + 1]);
                     int edgeType = signRight + signLeft + 2;
                     signLeft = -signRight;
 
@@ -1069,13 +1063,13 @@ void SAO::calcSaoStatsCu_BeforeDblk(Frame* frame, int idxX, int idxY)
             }
 
             for (x = startX; x < ctuWidth; x++)
-                upBuff1[x] = signOf(rec[x] - rec[x - stride]);
+                upBuff1[x] = x265_signOf(rec[x] - rec[x - stride]);
 
             for (y = firstY; y < endY; y++)
             {
                 for (x = (y < startY - 1 ? startX : 0); x < ctuWidth; x++)
                 {
-                    int signDown = signOf(rec[x] - rec[x + stride]);
+                    int signDown = x265_signOf(rec[x] - rec[x + stride]);
                     int edgeType = signDown + upBuff1[x] + 2;
                     upBuff1[x] = -signDown;
 
@@ -1117,15 +1111,15 @@ void SAO::calcSaoStatsCu_BeforeDblk(Frame* frame, int idxX, int idxY)
             }
 
             for (x = startX; x < endX; x++)
-                upBuff1[x] = signOf(rec[x] - rec[x - stride - 1]);
+                upBuff1[x] = x265_signOf(rec[x] - rec[x - stride - 1]);
 
             for (y = firstY; y < endY; y++)
             {
                 x = (y < startY - 1 ? startX : firstX);
-                upBufft[x] = signOf(rec[x + stride] - rec[x - 1]);
+                upBufft[x] = x265_signOf(rec[x + stride] - rec[x - 1]);
                 for (; x < endX; x++)
                 {
-                    int signDown = signOf(rec[x] - rec[x + stride + 1]);
+                    int signDown = x265_signOf(rec[x] - rec[x + stride + 1]);
                     int edgeType = signDown + upBuff1[x] + 2;
                     upBufft[x + 1] = -signDown;
 
@@ -1169,13 +1163,13 @@ void SAO::calcSaoStatsCu_BeforeDblk(Frame* frame, int idxX, int idxY)
             }
 
             for (x = startX - 1; x < endX; x++)
-                upBuff1[x] = signOf(rec[x] - rec[x - stride + 1]);
+                upBuff1[x] = x265_signOf(rec[x] - rec[x - stride + 1]);
 
             for (y = firstY; y < endY; y++)
             {
                 for (x = (y < startY - 1 ? startX : firstX); x < endX; x++)
                 {
-                    int signDown = signOf(rec[x] - rec[x + stride - 1]);
+                    int signDown = x265_signOf(rec[x] - rec[x + stride - 1]);
                     int edgeType = signDown + upBuff1[x] + 2;
                     upBuff1[x - 1] = -signDown;
 
@@ -1186,7 +1180,7 @@ void SAO::calcSaoStatsCu_BeforeDblk(Frame* frame, int idxX, int idxY)
                     count[s_eoTable[edgeType]]++;
                 }
 
-                upBuff1[endX - 1] = signOf(rec[endX - 1 + stride] - rec[endX]);
+                upBuff1[endX - 1] = x265_signOf(rec[endX - 1 + stride] - rec[endX]);
 
                 rec += stride;
                 fenc += stride;
@@ -1789,11 +1783,11 @@ void saoCuStatsE0_c(const int16_t *diff, const pixel *rec, intptr_t stride, int
 
     for (int y = 0; y < endY; y++)
     {
-        int signLeft = signOf(rec[0] - rec[-1]);
+        int signLeft = x265_signOf(rec[0] - rec[-1]);
         for (int x = 0; x < endX; x++)
         {
             int signRight = signOf2(rec[x], rec[x + 1]);
-            X265_CHECK(signRight == signOf(rec[x] - rec[x + 1]), "signDown check failure\n");
+            X265_CHECK(signRight == x265_signOf(rec[x] - rec[x + 1]), "signDown check failure\n");
             uint32_t edgeType = signRight + signLeft + 2;
             signLeft = -signRight;
 
@@ -1830,7 +1824,7 @@ void saoCuStatsE1_c(const int16_t *diff, const pixel *rec, intptr_t stride, int8
         for (int x = 0; x < endX; x++)
         {
             int signDown = signOf2(rec[x], rec[x + stride]);
-            X265_CHECK(signDown == signOf(rec[x] - rec[x + stride]), "signDown check failure\n");
+            X265_CHECK(signDown == x265_signOf(rec[x] - rec[x + stride]), "signDown check failure\n");
             uint32_t edgeType = signDown + upBuff1[x] + 2;
             upBuff1[x] = (int8_t)(-signDown);
 
@@ -1862,11 +1856,11 @@ void saoCuStatsE2_c(const int16_t *diff, const pixel *rec, intptr_t stride, int8
 
     for (int y = 0; y < endY; y++)
     {
-        upBufft[0] = signOf(rec[stride] - rec[-1]);
+        upBufft[0] = x265_signOf(rec[stride] - rec[-1]);
         for (int x = 0; x < endX; x++)
         {
             int signDown = signOf2(rec[x], rec[x + stride + 1]);
-            X265_CHECK(signDown == signOf(rec[x] - rec[x + stride + 1]), "signDown check failure\n");
+            X265_CHECK(signDown == x265_signOf(rec[x] - rec[x + stride + 1]), "signDown check failure\n");
             uint32_t edgeType = signDown + upBuff1[x] + 2;
             upBufft[x + 1] = (int8_t)(-signDown);
             tmp_stats[edgeType] += diff[x];
@@ -1902,7 +1896,7 @@ void saoCuStatsE3_c(const int16_t *diff, const pixel *rec, intptr_t stride, int8
         for (int x = 0; x < endX; x++)
         {
             int signDown = signOf2(rec[x], rec[x + stride - 1]);
-            X265_CHECK(signDown == signOf(rec[x] - rec[x + stride - 1]), "signDown check failure\n");
+            X265_CHECK(signDown == x265_signOf(rec[x] - rec[x + stride - 1]), "signDown check failure\n");
             X265_CHECK(abs(upBuff1[x]) <= 1, "upBuffer1 check failure\n");
 
             uint32_t edgeType = signDown + upBuff1[x] + 2;
@@ -1911,7 +1905,7 @@ void saoCuStatsE3_c(const int16_t *diff, const pixel *rec, intptr_t stride, int8
             tmp_count[edgeType]++;
         }
 
-        upBuff1[endX - 1] = signOf(rec[endX - 1 + stride] - rec[endX]);
+        upBuff1[endX - 1] = x265_signOf(rec[endX - 1 + stride] - rec[endX]);
 
         rec += stride;
         diff += MAX_CU_SIZE;
-- 
2.42.1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-Move-duplicated-signOf-function-to-common-header.patch
Type: text/x-patch
Size: 19570 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20240520/ee27fa6a/attachment-0001.bin>


More information about the x265-devel mailing list