[x265] [PATCH] Cleanup dct.c. Move Clip3(-32768, 32767 into separate function with tighter input type.

den c dncodr at gmail.com
Thu May 22 22:41:20 CEST 2014


# HG changeset patch
# User Den Scherbin <dncodr at gmail.com>
# Date 1400788772 -10800
#      Thu May 22 22:59:32 2014 +0300
# Node ID e698bf97c072f8cb7651621b88aeb7a55138cc4b
# Parent  1ed48eced207da74a4956b7fab8a410ddd6f27c7
Cleanup dct Clip3

diff -r 1ed48eced207 -r e698bf97c072 source/common/dct.cpp
--- a/source/common/dct.cpp	Wed May 21 02:04:44 2014 +0300
+++ b/source/common/dct.cpp	Thu May 22 22:59:32 2014 +0300
@@ -38,6 +38,12 @@
 #endif

 namespace {
+
+inline int16_t ClipInt16(int val)
+{
+    return (int16_t)Clip3<int>(-32768, 32767,val);
+}
+
 // anonymous file-static namespace

 // Fast DST Algorithm. Full matrix multiplication for DST and Fast
DST algorithm
@@ -75,10 +81,10 @@
         c[2] = tmp[i] - tmp[12 + i];
         c[3] = 74 * tmp[4 + i];

-        block[4 * i + 0] = (int16_t)Clip3(-32768, 32767, (29 * c[0] +
55 * c[1]     + c[3]               + rnd_factor) >> shift);
-        block[4 * i + 1] = (int16_t)Clip3(-32768, 32767, (55 * c[2] -
29 * c[1]     + c[3]               + rnd_factor) >> shift);
-        block[4 * i + 2] = (int16_t)Clip3(-32768, 32767, (74 *
(tmp[i] - tmp[8 + i]  + tmp[12 + i])      + rnd_factor) >> shift);
-        block[4 * i + 3] = (int16_t)Clip3(-32768, 32767, (55 * c[0] +
29 * c[2]     - c[3]               + rnd_factor) >> shift);
+        block[4 * i + 0] = ClipInt16( (29 * c[0] + 55 * c[1]     +
c[3]               + rnd_factor) >> shift);
+        block[4 * i + 1] = ClipInt16( (55 * c[2] - 29 * c[1]     +
c[3]               + rnd_factor) >> shift);
+        block[4 * i + 2] = ClipInt16( (74 * (tmp[i] - tmp[8 + i] +
tmp[12 + i])      + rnd_factor) >> shift);
+        block[4 * i + 3] = ClipInt16( (55 * c[0] + 29 * c[2]     -
c[3]               + rnd_factor) >> shift);
     }
 }

@@ -256,10 +262,10 @@
         E[1] = g_t4[0][1] * src[0] + g_t4[2][1] * src[2 * line];

         /* Combining even and odd terms at each hierarchy levels to
calculate the final spatial domain vector */
-        dst[0] = (int16_t)(Clip3(-32768, 32767, (E[0] + O[0] + add) >> shift));
-        dst[1] = (int16_t)(Clip3(-32768, 32767, (E[1] + O[1] + add) >> shift));
-        dst[2] = (int16_t)(Clip3(-32768, 32767, (E[1] - O[1] + add) >> shift));
-        dst[3] = (int16_t)(Clip3(-32768, 32767, (E[0] - O[0] + add) >> shift));
+        dst[0] = ClipInt16((E[0] + O[0] + add) >> shift);
+        dst[1] = ClipInt16((E[1] + O[1] + add) >> shift);
+        dst[2] = ClipInt16((E[1] - O[1] + add) >> shift);
+        dst[3] = ClipInt16((E[0] - O[0] + add) >> shift);

         src++;
         dst += 4;
@@ -293,8 +299,8 @@
         E[2] = EE[1] - EO[1];
         for (k = 0; k < 4; k++)
         {
-            dst[k] = (int16_t)Clip3(-32768, 32767, (E[k] + O[k] +
add) >> shift);
-            dst[k + 4] = (int16_t)Clip3(-32768, 32767, (E[3 - k] -
O[3 - k] + add) >> shift);
+            dst[k] = ClipInt16( (E[k] + O[k] + add) >> shift);
+            dst[k + 4] = ClipInt16( (E[3 - k] - O[3 - k] + add) >> shift);
         }

         src++;
@@ -344,8 +350,8 @@

         for (k = 0; k < 8; k++)
         {
-            dst[k]   = (int16_t)Clip3(-32768, 32767, (E[k] + O[k] +
add) >> shift);
-            dst[k + 8] = (int16_t)Clip3(-32768, 32767, (E[7 - k] -
O[7 - k] + add) >> shift);
+            dst[k]     = ClipInt16( (E[k] + O[k] + add) >> shift);
+            dst[k + 8] = ClipInt16( (E[7 - k] - O[7 - k] + add) >> shift);
         }

         src++;
@@ -408,8 +414,8 @@

         for (k = 0; k < 16; k++)
         {
-            dst[k] = (int16_t)Clip3(-32768, 32767, (E[k] + O[k] +
add) >> shift);
-            dst[k + 16] = (int16_t)Clip3(-32768, 32767, (E[15 - k] -
O[15 - k] + add) >> shift);
+            dst[k] = ClipInt16( (E[k] + O[k] + add) >> shift);
+            dst[k + 16] = ClipInt16( (E[15 - k] - O[15 - k] + add) >> shift);
         }

         src++;
@@ -736,9 +742,9 @@

     for (int n = 0; n < num; n++)
     {
-        clipQCoef = Clip3(-32768, 32767, quantCoef[n]);
+        clipQCoef = ClipInt16(quantCoef[n]);
         coeffQ = (clipQCoef * scale + add) >> shift;
-        coef[n] = Clip3(-32768, 32767, coeffQ);
+        coef[n] = ClipInt16( coeffQ);
     }
 }

@@ -757,18 +763,18 @@

         for (int n = 0; n < num; n++)
         {
-            clipQCoef = Clip3(-32768, 32767, quantCoef[n]);
+            clipQCoef = ClipInt16( quantCoef[n]);
             coeffQ = ((clipQCoef * deQuantCoef[n]) + add) >> (shift - per);
-            coef[n] = Clip3(-32768, 32767, coeffQ);
+            coef[n] = ClipInt16(coeffQ);
         }
     }
     else
     {
         for (int n = 0; n < num; n++)
         {
-            clipQCoef = Clip3(-32768, 32767, quantCoef[n]);
-            coeffQ   = Clip3(-32768, 32767, clipQCoef * deQuantCoef[n]);
-            coef[n] = Clip3(-32768, 32767, coeffQ << (per - shift));
+            clipQCoef = ClipInt16(quantCoef[n]);
+            coeffQ   = ClipInt16(clipQCoef * deQuantCoef[n]);
+            coef[n] = ClipInt16(coeffQ << (per - shift));
         }
     }
 }
@@ -792,7 +798,7 @@
         deltaU[blockpos] = ((tmplevel - (level << qBits)) >> qBits8);
         acSum += level;
         level *= sign;
-        qCoef[blockpos] = Clip3(-32768, 32767, level);
+        qCoef[blockpos] = ClipInt16(level);
     }

     return acSum;


More information about the x265-devel mailing list