Home | History | Annotate | Download | only in dsp
      1 // Copyright 2010 Google Inc. All Rights Reserved.
      2 //
      3 // This code is licensed under the same terms as WebM:
      4 //  Software License Agreement:  http://www.webmproject.org/license/software/
      5 //  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
      6 // -----------------------------------------------------------------------------
      7 //
      8 // Speed-critical decoding functions.
      9 //
     10 // Author: Skal (pascal.massimino (at) gmail.com)
     11 
     12 #include "./dsp.h"
     13 #include "../dec/vp8i.h"
     14 
     15 #if defined(__cplusplus) || defined(c_plusplus)
     16 extern "C" {
     17 #endif
     18 
     19 //------------------------------------------------------------------------------
     20 // run-time tables (~4k)
     21 
     22 static uint8_t abs0[255 + 255 + 1];     // abs(i)
     23 static uint8_t abs1[255 + 255 + 1];     // abs(i)>>1
     24 static int8_t sclip1[1020 + 1020 + 1];  // clips [-1020, 1020] to [-128, 127]
     25 static int8_t sclip2[112 + 112 + 1];    // clips [-112, 112] to [-16, 15]
     26 static uint8_t clip1[255 + 510 + 1];    // clips [-255,510] to [0,255]
     27 
     28 // We declare this variable 'volatile' to prevent instruction reordering
     29 // and make sure it's set to true _last_ (so as to be thread-safe)
     30 static volatile int tables_ok = 0;
     31 
     32 static void DspInitTables(void) {
     33   if (!tables_ok) {
     34     int i;
     35     for (i = -255; i <= 255; ++i) {
     36       abs0[255 + i] = (i < 0) ? -i : i;
     37       abs1[255 + i] = abs0[255 + i] >> 1;
     38     }
     39     for (i = -1020; i <= 1020; ++i) {
     40       sclip1[1020 + i] = (i < -128) ? -128 : (i > 127) ? 127 : i;
     41     }
     42     for (i = -112; i <= 112; ++i) {
     43       sclip2[112 + i] = (i < -16) ? -16 : (i > 15) ? 15 : i;
     44     }
     45     for (i = -255; i <= 255 + 255; ++i) {
     46       clip1[255 + i] = (i < 0) ? 0 : (i > 255) ? 255 : i;
     47     }
     48     tables_ok = 1;
     49   }
     50 }
     51 
     52 static WEBP_INLINE uint8_t clip_8b(int v) {
     53   return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
     54 }
     55 
     56 //------------------------------------------------------------------------------
     57 // Transforms (Paragraph 14.4)
     58 
     59 #define STORE(x, y, v) \
     60   dst[x + y * BPS] = clip_8b(dst[x + y * BPS] + ((v) >> 3))
     61 
     62 static const int kC1 = 20091 + (1 << 16);
     63 static const int kC2 = 35468;
     64 #define MUL(a, b) (((a) * (b)) >> 16)
     65 
     66 static void TransformOne(const int16_t* in, uint8_t* dst) {
     67   int C[4 * 4], *tmp;
     68   int i;
     69   tmp = C;
     70   for (i = 0; i < 4; ++i) {    // vertical pass
     71     const int a = in[0] + in[8];    // [-4096, 4094]
     72     const int b = in[0] - in[8];    // [-4095, 4095]
     73     const int c = MUL(in[4], kC2) - MUL(in[12], kC1);   // [-3783, 3783]
     74     const int d = MUL(in[4], kC1) + MUL(in[12], kC2);   // [-3785, 3781]
     75     tmp[0] = a + d;   // [-7881, 7875]
     76     tmp[1] = b + c;   // [-7878, 7878]
     77     tmp[2] = b - c;   // [-7878, 7878]
     78     tmp[3] = a - d;   // [-7877, 7879]
     79     tmp += 4;
     80     in++;
     81   }
     82   // Each pass is expanding the dynamic range by ~3.85 (upper bound).
     83   // The exact value is (2. + (kC1 + kC2) / 65536).
     84   // After the second pass, maximum interval is [-3794, 3794], assuming
     85   // an input in [-2048, 2047] interval. We then need to add a dst value
     86   // in the [0, 255] range.
     87   // In the worst case scenario, the input to clip_8b() can be as large as
     88   // [-60713, 60968].
     89   tmp = C;
     90   for (i = 0; i < 4; ++i) {    // horizontal pass
     91     const int dc = tmp[0] + 4;
     92     const int a =  dc +  tmp[8];
     93     const int b =  dc -  tmp[8];
     94     const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
     95     const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
     96     STORE(0, 0, a + d);
     97     STORE(1, 0, b + c);
     98     STORE(2, 0, b - c);
     99     STORE(3, 0, a - d);
    100     tmp++;
    101     dst += BPS;
    102   }
    103 }
    104 #undef MUL
    105 
    106 static void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
    107   TransformOne(in, dst);
    108   if (do_two) {
    109     TransformOne(in + 16, dst + 4);
    110   }
    111 }
    112 
    113 static void TransformUV(const int16_t* in, uint8_t* dst) {
    114   VP8Transform(in + 0 * 16, dst, 1);
    115   VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
    116 }
    117 
    118 static void TransformDC(const int16_t *in, uint8_t* dst) {
    119   const int DC = in[0] + 4;
    120   int i, j;
    121   for (j = 0; j < 4; ++j) {
    122     for (i = 0; i < 4; ++i) {
    123       STORE(i, j, DC);
    124     }
    125   }
    126 }
    127 
    128 static void TransformDCUV(const int16_t* in, uint8_t* dst) {
    129   if (in[0 * 16]) TransformDC(in + 0 * 16, dst);
    130   if (in[1 * 16]) TransformDC(in + 1 * 16, dst + 4);
    131   if (in[2 * 16]) TransformDC(in + 2 * 16, dst + 4 * BPS);
    132   if (in[3 * 16]) TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
    133 }
    134 
    135 #undef STORE
    136 
    137 //------------------------------------------------------------------------------
    138 // Paragraph 14.3
    139 
    140 static void TransformWHT(const int16_t* in, int16_t* out) {
    141   int tmp[16];
    142   int i;
    143   for (i = 0; i < 4; ++i) {
    144     const int a0 = in[0 + i] + in[12 + i];
    145     const int a1 = in[4 + i] + in[ 8 + i];
    146     const int a2 = in[4 + i] - in[ 8 + i];
    147     const int a3 = in[0 + i] - in[12 + i];
    148     tmp[0  + i] = a0 + a1;
    149     tmp[8  + i] = a0 - a1;
    150     tmp[4  + i] = a3 + a2;
    151     tmp[12 + i] = a3 - a2;
    152   }
    153   for (i = 0; i < 4; ++i) {
    154     const int dc = tmp[0 + i * 4] + 3;    // w/ rounder
    155     const int a0 = dc             + tmp[3 + i * 4];
    156     const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
    157     const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
    158     const int a3 = dc             - tmp[3 + i * 4];
    159     out[ 0] = (a0 + a1) >> 3;
    160     out[16] = (a3 + a2) >> 3;
    161     out[32] = (a0 - a1) >> 3;
    162     out[48] = (a3 - a2) >> 3;
    163     out += 64;
    164   }
    165 }
    166 
    167 void (*VP8TransformWHT)(const int16_t* in, int16_t* out) = TransformWHT;
    168 
    169 //------------------------------------------------------------------------------
    170 // Intra predictions
    171 
    172 #define DST(x, y) dst[(x) + (y) * BPS]
    173 
    174 static WEBP_INLINE void TrueMotion(uint8_t *dst, int size) {
    175   const uint8_t* top = dst - BPS;
    176   const uint8_t* const clip0 = clip1 + 255 - top[-1];
    177   int y;
    178   for (y = 0; y < size; ++y) {
    179     const uint8_t* const clip = clip0 + dst[-1];
    180     int x;
    181     for (x = 0; x < size; ++x) {
    182       dst[x] = clip[top[x]];
    183     }
    184     dst += BPS;
    185   }
    186 }
    187 static void TM4(uint8_t *dst)   { TrueMotion(dst, 4); }
    188 static void TM8uv(uint8_t *dst) { TrueMotion(dst, 8); }
    189 static void TM16(uint8_t *dst)  { TrueMotion(dst, 16); }
    190 
    191 //------------------------------------------------------------------------------
    192 // 16x16
    193 
    194 static void VE16(uint8_t *dst) {     // vertical
    195   int j;
    196   for (j = 0; j < 16; ++j) {
    197     memcpy(dst + j * BPS, dst - BPS, 16);
    198   }
    199 }
    200 
    201 static void HE16(uint8_t *dst) {     // horizontal
    202   int j;
    203   for (j = 16; j > 0; --j) {
    204     memset(dst, dst[-1], 16);
    205     dst += BPS;
    206   }
    207 }
    208 
    209 static WEBP_INLINE void Put16(int v, uint8_t* dst) {
    210   int j;
    211   for (j = 0; j < 16; ++j) {
    212     memset(dst + j * BPS, v, 16);
    213   }
    214 }
    215 
    216 static void DC16(uint8_t *dst) {    // DC
    217   int DC = 16;
    218   int j;
    219   for (j = 0; j < 16; ++j) {
    220     DC += dst[-1 + j * BPS] + dst[j - BPS];
    221   }
    222   Put16(DC >> 5, dst);
    223 }
    224 
    225 static void DC16NoTop(uint8_t *dst) {   // DC with top samples not available
    226   int DC = 8;
    227   int j;
    228   for (j = 0; j < 16; ++j) {
    229     DC += dst[-1 + j * BPS];
    230   }
    231   Put16(DC >> 4, dst);
    232 }
    233 
    234 static void DC16NoLeft(uint8_t *dst) {  // DC with left samples not available
    235   int DC = 8;
    236   int i;
    237   for (i = 0; i < 16; ++i) {
    238     DC += dst[i - BPS];
    239   }
    240   Put16(DC >> 4, dst);
    241 }
    242 
    243 static void DC16NoTopLeft(uint8_t *dst) {  // DC with no top and left samples
    244   Put16(0x80, dst);
    245 }
    246 
    247 //------------------------------------------------------------------------------
    248 // 4x4
    249 
    250 #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
    251 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
    252 
    253 static void VE4(uint8_t *dst) {    // vertical
    254   const uint8_t* top = dst - BPS;
    255   const uint8_t vals[4] = {
    256     AVG3(top[-1], top[0], top[1]),
    257     AVG3(top[ 0], top[1], top[2]),
    258     AVG3(top[ 1], top[2], top[3]),
    259     AVG3(top[ 2], top[3], top[4])
    260   };
    261   int i;
    262   for (i = 0; i < 4; ++i) {
    263     memcpy(dst + i * BPS, vals, sizeof(vals));
    264   }
    265 }
    266 
    267 static void HE4(uint8_t *dst) {    // horizontal
    268   const int A = dst[-1 - BPS];
    269   const int B = dst[-1];
    270   const int C = dst[-1 + BPS];
    271   const int D = dst[-1 + 2 * BPS];
    272   const int E = dst[-1 + 3 * BPS];
    273   *(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(A, B, C);
    274   *(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(B, C, D);
    275   *(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(C, D, E);
    276   *(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(D, E, E);
    277 }
    278 
    279 static void DC4(uint8_t *dst) {   // DC
    280   uint32_t dc = 4;
    281   int i;
    282   for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
    283   dc >>= 3;
    284   for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
    285 }
    286 
    287 static void RD4(uint8_t *dst) {   // Down-right
    288   const int I = dst[-1 + 0 * BPS];
    289   const int J = dst[-1 + 1 * BPS];
    290   const int K = dst[-1 + 2 * BPS];
    291   const int L = dst[-1 + 3 * BPS];
    292   const int X = dst[-1 - BPS];
    293   const int A = dst[0 - BPS];
    294   const int B = dst[1 - BPS];
    295   const int C = dst[2 - BPS];
    296   const int D = dst[3 - BPS];
    297   DST(0, 3)                                     = AVG3(J, K, L);
    298   DST(0, 2) = DST(1, 3)                         = AVG3(I, J, K);
    299   DST(0, 1) = DST(1, 2) = DST(2, 3)             = AVG3(X, I, J);
    300   DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
    301   DST(1, 0) = DST(2, 1) = DST(3, 2)             = AVG3(B, A, X);
    302   DST(2, 0) = DST(3, 1)                         = AVG3(C, B, A);
    303   DST(3, 0)                                     = AVG3(D, C, B);
    304 }
    305 
    306 static void LD4(uint8_t *dst) {   // Down-Left
    307   const int A = dst[0 - BPS];
    308   const int B = dst[1 - BPS];
    309   const int C = dst[2 - BPS];
    310   const int D = dst[3 - BPS];
    311   const int E = dst[4 - BPS];
    312   const int F = dst[5 - BPS];
    313   const int G = dst[6 - BPS];
    314   const int H = dst[7 - BPS];
    315   DST(0, 0)                                     = AVG3(A, B, C);
    316   DST(1, 0) = DST(0, 1)                         = AVG3(B, C, D);
    317   DST(2, 0) = DST(1, 1) = DST(0, 2)             = AVG3(C, D, E);
    318   DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
    319   DST(3, 1) = DST(2, 2) = DST(1, 3)             = AVG3(E, F, G);
    320   DST(3, 2) = DST(2, 3)                         = AVG3(F, G, H);
    321   DST(3, 3)                                     = AVG3(G, H, H);
    322 }
    323 
    324 static void VR4(uint8_t *dst) {   // Vertical-Right
    325   const int I = dst[-1 + 0 * BPS];
    326   const int J = dst[-1 + 1 * BPS];
    327   const int K = dst[-1 + 2 * BPS];
    328   const int X = dst[-1 - BPS];
    329   const int A = dst[0 - BPS];
    330   const int B = dst[1 - BPS];
    331   const int C = dst[2 - BPS];
    332   const int D = dst[3 - BPS];
    333   DST(0, 0) = DST(1, 2) = AVG2(X, A);
    334   DST(1, 0) = DST(2, 2) = AVG2(A, B);
    335   DST(2, 0) = DST(3, 2) = AVG2(B, C);
    336   DST(3, 0)             = AVG2(C, D);
    337 
    338   DST(0, 3) =             AVG3(K, J, I);
    339   DST(0, 2) =             AVG3(J, I, X);
    340   DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
    341   DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
    342   DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
    343   DST(3, 1) =             AVG3(B, C, D);
    344 }
    345 
    346 static void VL4(uint8_t *dst) {   // Vertical-Left
    347   const int A = dst[0 - BPS];
    348   const int B = dst[1 - BPS];
    349   const int C = dst[2 - BPS];
    350   const int D = dst[3 - BPS];
    351   const int E = dst[4 - BPS];
    352   const int F = dst[5 - BPS];
    353   const int G = dst[6 - BPS];
    354   const int H = dst[7 - BPS];
    355   DST(0, 0) =             AVG2(A, B);
    356   DST(1, 0) = DST(0, 2) = AVG2(B, C);
    357   DST(2, 0) = DST(1, 2) = AVG2(C, D);
    358   DST(3, 0) = DST(2, 2) = AVG2(D, E);
    359 
    360   DST(0, 1) =             AVG3(A, B, C);
    361   DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
    362   DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
    363   DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
    364               DST(3, 2) = AVG3(E, F, G);
    365               DST(3, 3) = AVG3(F, G, H);
    366 }
    367 
    368 static void HU4(uint8_t *dst) {   // Horizontal-Up
    369   const int I = dst[-1 + 0 * BPS];
    370   const int J = dst[-1 + 1 * BPS];
    371   const int K = dst[-1 + 2 * BPS];
    372   const int L = dst[-1 + 3 * BPS];
    373   DST(0, 0) =             AVG2(I, J);
    374   DST(2, 0) = DST(0, 1) = AVG2(J, K);
    375   DST(2, 1) = DST(0, 2) = AVG2(K, L);
    376   DST(1, 0) =             AVG3(I, J, K);
    377   DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
    378   DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
    379   DST(3, 2) = DST(2, 2) =
    380     DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
    381 }
    382 
    383 static void HD4(uint8_t *dst) {  // Horizontal-Down
    384   const int I = dst[-1 + 0 * BPS];
    385   const int J = dst[-1 + 1 * BPS];
    386   const int K = dst[-1 + 2 * BPS];
    387   const int L = dst[-1 + 3 * BPS];
    388   const int X = dst[-1 - BPS];
    389   const int A = dst[0 - BPS];
    390   const int B = dst[1 - BPS];
    391   const int C = dst[2 - BPS];
    392 
    393   DST(0, 0) = DST(2, 1) = AVG2(I, X);
    394   DST(0, 1) = DST(2, 2) = AVG2(J, I);
    395   DST(0, 2) = DST(2, 3) = AVG2(K, J);
    396   DST(0, 3)             = AVG2(L, K);
    397 
    398   DST(3, 0)             = AVG3(A, B, C);
    399   DST(2, 0)             = AVG3(X, A, B);
    400   DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
    401   DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
    402   DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
    403   DST(1, 3)             = AVG3(L, K, J);
    404 }
    405 
    406 #undef DST
    407 #undef AVG3
    408 #undef AVG2
    409 
    410 //------------------------------------------------------------------------------
    411 // Chroma
    412 
    413 static void VE8uv(uint8_t *dst) {    // vertical
    414   int j;
    415   for (j = 0; j < 8; ++j) {
    416     memcpy(dst + j * BPS, dst - BPS, 8);
    417   }
    418 }
    419 
    420 static void HE8uv(uint8_t *dst) {    // horizontal
    421   int j;
    422   for (j = 0; j < 8; ++j) {
    423     memset(dst, dst[-1], 8);
    424     dst += BPS;
    425   }
    426 }
    427 
    428 // helper for chroma-DC predictions
    429 static WEBP_INLINE void Put8x8uv(uint8_t value, uint8_t* dst) {
    430   int j;
    431 #ifndef WEBP_REFERENCE_IMPLEMENTATION
    432   const uint64_t v = (uint64_t)value * 0x0101010101010101ULL;
    433   for (j = 0; j < 8; ++j) {
    434     *(uint64_t*)(dst + j * BPS) = v;
    435   }
    436 #else
    437   for (j = 0; j < 8; ++j) memset(dst + j * BPS, value, 8);
    438 #endif
    439 }
    440 
    441 static void DC8uv(uint8_t *dst) {     // DC
    442   int dc0 = 8;
    443   int i;
    444   for (i = 0; i < 8; ++i) {
    445     dc0 += dst[i - BPS] + dst[-1 + i * BPS];
    446   }
    447   Put8x8uv(dc0 >> 4, dst);
    448 }
    449 
    450 static void DC8uvNoLeft(uint8_t *dst) {   // DC with no left samples
    451   int dc0 = 4;
    452   int i;
    453   for (i = 0; i < 8; ++i) {
    454     dc0 += dst[i - BPS];
    455   }
    456   Put8x8uv(dc0 >> 3, dst);
    457 }
    458 
    459 static void DC8uvNoTop(uint8_t *dst) {  // DC with no top samples
    460   int dc0 = 4;
    461   int i;
    462   for (i = 0; i < 8; ++i) {
    463     dc0 += dst[-1 + i * BPS];
    464   }
    465   Put8x8uv(dc0 >> 3, dst);
    466 }
    467 
    468 static void DC8uvNoTopLeft(uint8_t *dst) {    // DC with nothing
    469   Put8x8uv(0x80, dst);
    470 }
    471 
    472 //------------------------------------------------------------------------------
    473 // default C implementations
    474 
    475 const VP8PredFunc VP8PredLuma4[NUM_BMODES] = {
    476   DC4, TM4, VE4, HE4, RD4, VR4, LD4, VL4, HD4, HU4
    477 };
    478 
    479 const VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES] = {
    480   DC16, TM16, VE16, HE16,
    481   DC16NoTop, DC16NoLeft, DC16NoTopLeft
    482 };
    483 
    484 const VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES] = {
    485   DC8uv, TM8uv, VE8uv, HE8uv,
    486   DC8uvNoTop, DC8uvNoLeft, DC8uvNoTopLeft
    487 };
    488 
    489 //------------------------------------------------------------------------------
    490 // Edge filtering functions
    491 
    492 // 4 pixels in, 2 pixels out
    493 static WEBP_INLINE void do_filter2(uint8_t* p, int step) {
    494   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
    495   const int a = 3 * (q0 - p0) + sclip1[1020 + p1 - q1];
    496   const int a1 = sclip2[112 + ((a + 4) >> 3)];
    497   const int a2 = sclip2[112 + ((a + 3) >> 3)];
    498   p[-step] = clip1[255 + p0 + a2];
    499   p[    0] = clip1[255 + q0 - a1];
    500 }
    501 
    502 // 4 pixels in, 4 pixels out
    503 static WEBP_INLINE void do_filter4(uint8_t* p, int step) {
    504   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
    505   const int a = 3 * (q0 - p0);
    506   const int a1 = sclip2[112 + ((a + 4) >> 3)];
    507   const int a2 = sclip2[112 + ((a + 3) >> 3)];
    508   const int a3 = (a1 + 1) >> 1;
    509   p[-2*step] = clip1[255 + p1 + a3];
    510   p[-  step] = clip1[255 + p0 + a2];
    511   p[      0] = clip1[255 + q0 - a1];
    512   p[   step] = clip1[255 + q1 - a3];
    513 }
    514 
    515 // 6 pixels in, 6 pixels out
    516 static WEBP_INLINE void do_filter6(uint8_t* p, int step) {
    517   const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
    518   const int q0 = p[0], q1 = p[step], q2 = p[2*step];
    519   const int a = sclip1[1020 + 3 * (q0 - p0) + sclip1[1020 + p1 - q1]];
    520   const int a1 = (27 * a + 63) >> 7;  // eq. to ((3 * a + 7) * 9) >> 7
    521   const int a2 = (18 * a + 63) >> 7;  // eq. to ((2 * a + 7) * 9) >> 7
    522   const int a3 = (9  * a + 63) >> 7;  // eq. to ((1 * a + 7) * 9) >> 7
    523   p[-3*step] = clip1[255 + p2 + a3];
    524   p[-2*step] = clip1[255 + p1 + a2];
    525   p[-  step] = clip1[255 + p0 + a1];
    526   p[      0] = clip1[255 + q0 - a1];
    527   p[   step] = clip1[255 + q1 - a2];
    528   p[ 2*step] = clip1[255 + q2 - a3];
    529 }
    530 
    531 static WEBP_INLINE int hev(const uint8_t* p, int step, int thresh) {
    532   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
    533   return (abs0[255 + p1 - p0] > thresh) || (abs0[255 + q1 - q0] > thresh);
    534 }
    535 
    536 static WEBP_INLINE int needs_filter(const uint8_t* p, int step, int thresh) {
    537   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
    538   return (2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) <= thresh;
    539 }
    540 
    541 static WEBP_INLINE int needs_filter2(const uint8_t* p,
    542                                      int step, int t, int it) {
    543   const int p3 = p[-4*step], p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
    544   const int q0 = p[0], q1 = p[step], q2 = p[2*step], q3 = p[3*step];
    545   if ((2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) > t)
    546     return 0;
    547   return abs0[255 + p3 - p2] <= it && abs0[255 + p2 - p1] <= it &&
    548          abs0[255 + p1 - p0] <= it && abs0[255 + q3 - q2] <= it &&
    549          abs0[255 + q2 - q1] <= it && abs0[255 + q1 - q0] <= it;
    550 }
    551 
    552 //------------------------------------------------------------------------------
    553 // Simple In-loop filtering (Paragraph 15.2)
    554 
    555 static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
    556   int i;
    557   for (i = 0; i < 16; ++i) {
    558     if (needs_filter(p + i, stride, thresh)) {
    559       do_filter2(p + i, stride);
    560     }
    561   }
    562 }
    563 
    564 static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
    565   int i;
    566   for (i = 0; i < 16; ++i) {
    567     if (needs_filter(p + i * stride, 1, thresh)) {
    568       do_filter2(p + i * stride, 1);
    569     }
    570   }
    571 }
    572 
    573 static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
    574   int k;
    575   for (k = 3; k > 0; --k) {
    576     p += 4 * stride;
    577     SimpleVFilter16(p, stride, thresh);
    578   }
    579 }
    580 
    581 static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
    582   int k;
    583   for (k = 3; k > 0; --k) {
    584     p += 4;
    585     SimpleHFilter16(p, stride, thresh);
    586   }
    587 }
    588 
    589 //------------------------------------------------------------------------------
    590 // Complex In-loop filtering (Paragraph 15.3)
    591 
    592 static WEBP_INLINE void FilterLoop26(uint8_t* p,
    593                                      int hstride, int vstride, int size,
    594                                      int thresh, int ithresh, int hev_thresh) {
    595   while (size-- > 0) {
    596     if (needs_filter2(p, hstride, thresh, ithresh)) {
    597       if (hev(p, hstride, hev_thresh)) {
    598         do_filter2(p, hstride);
    599       } else {
    600         do_filter6(p, hstride);
    601       }
    602     }
    603     p += vstride;
    604   }
    605 }
    606 
    607 static WEBP_INLINE void FilterLoop24(uint8_t* p,
    608                                      int hstride, int vstride, int size,
    609                                      int thresh, int ithresh, int hev_thresh) {
    610   while (size-- > 0) {
    611     if (needs_filter2(p, hstride, thresh, ithresh)) {
    612       if (hev(p, hstride, hev_thresh)) {
    613         do_filter2(p, hstride);
    614       } else {
    615         do_filter4(p, hstride);
    616       }
    617     }
    618     p += vstride;
    619   }
    620 }
    621 
    622 // on macroblock edges
    623 static void VFilter16(uint8_t* p, int stride,
    624                       int thresh, int ithresh, int hev_thresh) {
    625   FilterLoop26(p, stride, 1, 16, thresh, ithresh, hev_thresh);
    626 }
    627 
    628 static void HFilter16(uint8_t* p, int stride,
    629                       int thresh, int ithresh, int hev_thresh) {
    630   FilterLoop26(p, 1, stride, 16, thresh, ithresh, hev_thresh);
    631 }
    632 
    633 // on three inner edges
    634 static void VFilter16i(uint8_t* p, int stride,
    635                        int thresh, int ithresh, int hev_thresh) {
    636   int k;
    637   for (k = 3; k > 0; --k) {
    638     p += 4 * stride;
    639     FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
    640   }
    641 }
    642 
    643 static void HFilter16i(uint8_t* p, int stride,
    644                        int thresh, int ithresh, int hev_thresh) {
    645   int k;
    646   for (k = 3; k > 0; --k) {
    647     p += 4;
    648     FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
    649   }
    650 }
    651 
    652 // 8-pixels wide variant, for chroma filtering
    653 static void VFilter8(uint8_t* u, uint8_t* v, int stride,
    654                      int thresh, int ithresh, int hev_thresh) {
    655   FilterLoop26(u, stride, 1, 8, thresh, ithresh, hev_thresh);
    656   FilterLoop26(v, stride, 1, 8, thresh, ithresh, hev_thresh);
    657 }
    658 
    659 static void HFilter8(uint8_t* u, uint8_t* v, int stride,
    660                      int thresh, int ithresh, int hev_thresh) {
    661   FilterLoop26(u, 1, stride, 8, thresh, ithresh, hev_thresh);
    662   FilterLoop26(v, 1, stride, 8, thresh, ithresh, hev_thresh);
    663 }
    664 
    665 static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
    666                       int thresh, int ithresh, int hev_thresh) {
    667   FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
    668   FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
    669 }
    670 
    671 static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
    672                       int thresh, int ithresh, int hev_thresh) {
    673   FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
    674   FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
    675 }
    676 
    677 //------------------------------------------------------------------------------
    678 
    679 VP8DecIdct2 VP8Transform;
    680 VP8DecIdct VP8TransformUV;
    681 VP8DecIdct VP8TransformDC;
    682 VP8DecIdct VP8TransformDCUV;
    683 
    684 VP8LumaFilterFunc VP8VFilter16;
    685 VP8LumaFilterFunc VP8HFilter16;
    686 VP8ChromaFilterFunc VP8VFilter8;
    687 VP8ChromaFilterFunc VP8HFilter8;
    688 VP8LumaFilterFunc VP8VFilter16i;
    689 VP8LumaFilterFunc VP8HFilter16i;
    690 VP8ChromaFilterFunc VP8VFilter8i;
    691 VP8ChromaFilterFunc VP8HFilter8i;
    692 VP8SimpleFilterFunc VP8SimpleVFilter16;
    693 VP8SimpleFilterFunc VP8SimpleHFilter16;
    694 VP8SimpleFilterFunc VP8SimpleVFilter16i;
    695 VP8SimpleFilterFunc VP8SimpleHFilter16i;
    696 
    697 extern void VP8DspInitSSE2(void);
    698 extern void VP8DspInitNEON(void);
    699 
    700 void VP8DspInit(void) {
    701   DspInitTables();
    702 
    703   VP8Transform = TransformTwo;
    704   VP8TransformUV = TransformUV;
    705   VP8TransformDC = TransformDC;
    706   VP8TransformDCUV = TransformDCUV;
    707 
    708   VP8VFilter16 = VFilter16;
    709   VP8HFilter16 = HFilter16;
    710   VP8VFilter8 = VFilter8;
    711   VP8HFilter8 = HFilter8;
    712   VP8VFilter16i = VFilter16i;
    713   VP8HFilter16i = HFilter16i;
    714   VP8VFilter8i = VFilter8i;
    715   VP8HFilter8i = HFilter8i;
    716   VP8SimpleVFilter16 = SimpleVFilter16;
    717   VP8SimpleHFilter16 = SimpleHFilter16;
    718   VP8SimpleVFilter16i = SimpleVFilter16i;
    719   VP8SimpleHFilter16i = SimpleHFilter16i;
    720 
    721   // If defined, use CPUInfo() to overwrite some pointers with faster versions.
    722   if (VP8GetCPUInfo) {
    723 #if defined(WEBP_USE_SSE2)
    724     if (VP8GetCPUInfo(kSSE2)) {
    725       VP8DspInitSSE2();
    726     }
    727 #elif defined(WEBP_USE_NEON)
    728     if (VP8GetCPUInfo(kNEON)) {
    729       VP8DspInitNEON();
    730     }
    731 #endif
    732   }
    733 }
    734 
    735 #if defined(__cplusplus) || defined(c_plusplus)
    736 }    // extern "C"
    737 #endif
    738