Home | History | Annotate | Download | only in vpx_dsp
      1 /*
      2  *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <stdlib.h>
     12 
     13 #include "./vpx_config.h"
     14 #include "vpx_dsp/vpx_dsp_common.h"
     15 #include "vpx_ports/mem.h"
     16 
     17 static INLINE int8_t signed_char_clamp(int t) {
     18   return (int8_t)clamp(t, -128, 127);
     19 }
     20 
     21 #if CONFIG_VP9_HIGHBITDEPTH
     22 static INLINE int16_t signed_char_clamp_high(int t, int bd) {
     23   switch (bd) {
     24     case 10:
     25       return (int16_t)clamp(t, -128*4, 128*4-1);
     26     case 12:
     27       return (int16_t)clamp(t, -128*16, 128*16-1);
     28     case 8:
     29     default:
     30       return (int16_t)clamp(t, -128, 128-1);
     31   }
     32 }
     33 #endif
     34 
     35 // should we apply any filter at all: 11111111 yes, 00000000 no
     36 static INLINE int8_t filter_mask(uint8_t limit, uint8_t blimit,
     37                                  uint8_t p3, uint8_t p2,
     38                                  uint8_t p1, uint8_t p0,
     39                                  uint8_t q0, uint8_t q1,
     40                                  uint8_t q2, uint8_t q3) {
     41   int8_t mask = 0;
     42   mask |= (abs(p3 - p2) > limit) * -1;
     43   mask |= (abs(p2 - p1) > limit) * -1;
     44   mask |= (abs(p1 - p0) > limit) * -1;
     45   mask |= (abs(q1 - q0) > limit) * -1;
     46   mask |= (abs(q2 - q1) > limit) * -1;
     47   mask |= (abs(q3 - q2) > limit) * -1;
     48   mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  > blimit) * -1;
     49   return ~mask;
     50 }
     51 
     52 static INLINE int8_t flat_mask4(uint8_t thresh,
     53                                 uint8_t p3, uint8_t p2,
     54                                 uint8_t p1, uint8_t p0,
     55                                 uint8_t q0, uint8_t q1,
     56                                 uint8_t q2, uint8_t q3) {
     57   int8_t mask = 0;
     58   mask |= (abs(p1 - p0) > thresh) * -1;
     59   mask |= (abs(q1 - q0) > thresh) * -1;
     60   mask |= (abs(p2 - p0) > thresh) * -1;
     61   mask |= (abs(q2 - q0) > thresh) * -1;
     62   mask |= (abs(p3 - p0) > thresh) * -1;
     63   mask |= (abs(q3 - q0) > thresh) * -1;
     64   return ~mask;
     65 }
     66 
     67 static INLINE int8_t flat_mask5(uint8_t thresh,
     68                                 uint8_t p4, uint8_t p3,
     69                                 uint8_t p2, uint8_t p1,
     70                                 uint8_t p0, uint8_t q0,
     71                                 uint8_t q1, uint8_t q2,
     72                                 uint8_t q3, uint8_t q4) {
     73   int8_t mask = ~flat_mask4(thresh, p3, p2, p1, p0, q0, q1, q2, q3);
     74   mask |= (abs(p4 - p0) > thresh) * -1;
     75   mask |= (abs(q4 - q0) > thresh) * -1;
     76   return ~mask;
     77 }
     78 
     79 // is there high edge variance internal edge: 11111111 yes, 00000000 no
     80 static INLINE int8_t hev_mask(uint8_t thresh, uint8_t p1, uint8_t p0,
     81                               uint8_t q0, uint8_t q1) {
     82   int8_t hev = 0;
     83   hev  |= (abs(p1 - p0) > thresh) * -1;
     84   hev  |= (abs(q1 - q0) > thresh) * -1;
     85   return hev;
     86 }
     87 
     88 static INLINE void filter4(int8_t mask, uint8_t thresh, uint8_t *op1,
     89                            uint8_t *op0, uint8_t *oq0, uint8_t *oq1) {
     90   int8_t filter1, filter2;
     91 
     92   const int8_t ps1 = (int8_t) *op1 ^ 0x80;
     93   const int8_t ps0 = (int8_t) *op0 ^ 0x80;
     94   const int8_t qs0 = (int8_t) *oq0 ^ 0x80;
     95   const int8_t qs1 = (int8_t) *oq1 ^ 0x80;
     96   const uint8_t hev = hev_mask(thresh, *op1, *op0, *oq0, *oq1);
     97 
     98   // add outer taps if we have high edge variance
     99   int8_t filter = signed_char_clamp(ps1 - qs1) & hev;
    100 
    101   // inner taps
    102   filter = signed_char_clamp(filter + 3 * (qs0 - ps0)) & mask;
    103 
    104   // save bottom 3 bits so that we round one side +4 and the other +3
    105   // if it equals 4 we'll set to adjust by -1 to account for the fact
    106   // we'd round 3 the other way
    107   filter1 = signed_char_clamp(filter + 4) >> 3;
    108   filter2 = signed_char_clamp(filter + 3) >> 3;
    109 
    110   *oq0 = signed_char_clamp(qs0 - filter1) ^ 0x80;
    111   *op0 = signed_char_clamp(ps0 + filter2) ^ 0x80;
    112 
    113   // outer tap adjustments
    114   filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
    115 
    116   *oq1 = signed_char_clamp(qs1 - filter) ^ 0x80;
    117   *op1 = signed_char_clamp(ps1 + filter) ^ 0x80;
    118 }
    119 
    120 void vpx_lpf_horizontal_4_c(uint8_t *s, int p /* pitch */,
    121                             const uint8_t *blimit, const uint8_t *limit,
    122                             const uint8_t *thresh, int count) {
    123   int i;
    124 
    125   // loop filter designed to work using chars so that we can make maximum use
    126   // of 8 bit simd instructions.
    127   for (i = 0; i < 8 * count; ++i) {
    128     const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
    129     const uint8_t q0 = s[0 * p],  q1 = s[1 * p],  q2 = s[2 * p],  q3 = s[3 * p];
    130     const int8_t mask = filter_mask(*limit, *blimit,
    131                                     p3, p2, p1, p0, q0, q1, q2, q3);
    132     filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p);
    133     ++s;
    134   }
    135 }
    136 
    137 void vpx_lpf_horizontal_4_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
    138                                  const uint8_t *limit0, const uint8_t *thresh0,
    139                                  const uint8_t *blimit1, const uint8_t *limit1,
    140                                  const uint8_t *thresh1) {
    141   vpx_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0, 1);
    142   vpx_lpf_horizontal_4_c(s + 8, p, blimit1, limit1, thresh1, 1);
    143 }
    144 
    145 void vpx_lpf_vertical_4_c(uint8_t *s, int pitch, const uint8_t *blimit,
    146                           const uint8_t *limit, const uint8_t *thresh,
    147                           int count) {
    148   int i;
    149 
    150   // loop filter designed to work using chars so that we can make maximum use
    151   // of 8 bit simd instructions.
    152   for (i = 0; i < 8 * count; ++i) {
    153     const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
    154     const uint8_t q0 = s[0],  q1 = s[1],  q2 = s[2],  q3 = s[3];
    155     const int8_t mask = filter_mask(*limit, *blimit,
    156                                     p3, p2, p1, p0, q0, q1, q2, q3);
    157     filter4(mask, *thresh, s - 2, s - 1, s, s + 1);
    158     s += pitch;
    159   }
    160 }
    161 
    162 void vpx_lpf_vertical_4_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
    163                                const uint8_t *limit0, const uint8_t *thresh0,
    164                                const uint8_t *blimit1, const uint8_t *limit1,
    165                                const uint8_t *thresh1) {
    166   vpx_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0, 1);
    167   vpx_lpf_vertical_4_c(s + 8 * pitch, pitch, blimit1, limit1,
    168                                   thresh1, 1);
    169 }
    170 
    171 static INLINE void filter8(int8_t mask, uint8_t thresh, uint8_t flat,
    172                            uint8_t *op3, uint8_t *op2,
    173                            uint8_t *op1, uint8_t *op0,
    174                            uint8_t *oq0, uint8_t *oq1,
    175                            uint8_t *oq2, uint8_t *oq3) {
    176   if (flat && mask) {
    177     const uint8_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
    178     const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
    179 
    180     // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
    181     *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
    182     *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
    183     *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
    184     *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
    185     *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
    186     *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
    187   } else {
    188     filter4(mask, thresh, op1,  op0, oq0, oq1);
    189   }
    190 }
    191 
    192 void vpx_lpf_horizontal_8_c(uint8_t *s, int p, const uint8_t *blimit,
    193                             const uint8_t *limit, const uint8_t *thresh,
    194                             int count) {
    195   int i;
    196 
    197   // loop filter designed to work using chars so that we can make maximum use
    198   // of 8 bit simd instructions.
    199   for (i = 0; i < 8 * count; ++i) {
    200     const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
    201     const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
    202 
    203     const int8_t mask = filter_mask(*limit, *blimit,
    204                                     p3, p2, p1, p0, q0, q1, q2, q3);
    205     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
    206     filter8(mask, *thresh, flat, s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
    207                                  s,         s + 1 * p, s + 2 * p, s + 3 * p);
    208     ++s;
    209   }
    210 }
    211 
    212 void vpx_lpf_horizontal_8_dual_c(uint8_t *s, int p, const uint8_t *blimit0,
    213                                  const uint8_t *limit0, const uint8_t *thresh0,
    214                                  const uint8_t *blimit1, const uint8_t *limit1,
    215                                  const uint8_t *thresh1) {
    216   vpx_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0, 1);
    217   vpx_lpf_horizontal_8_c(s + 8, p, blimit1, limit1, thresh1, 1);
    218 }
    219 
    220 void vpx_lpf_vertical_8_c(uint8_t *s, int pitch, const uint8_t *blimit,
    221                           const uint8_t *limit, const uint8_t *thresh,
    222                           int count) {
    223   int i;
    224 
    225   for (i = 0; i < 8 * count; ++i) {
    226     const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
    227     const uint8_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
    228     const int8_t mask = filter_mask(*limit, *blimit,
    229                                     p3, p2, p1, p0, q0, q1, q2, q3);
    230     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
    231     filter8(mask, *thresh, flat, s - 4, s - 3, s - 2, s - 1,
    232                                  s,     s + 1, s + 2, s + 3);
    233     s += pitch;
    234   }
    235 }
    236 
    237 void vpx_lpf_vertical_8_dual_c(uint8_t *s, int pitch, const uint8_t *blimit0,
    238                                const uint8_t *limit0, const uint8_t *thresh0,
    239                                const uint8_t *blimit1, const uint8_t *limit1,
    240                                const uint8_t *thresh1) {
    241   vpx_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0, 1);
    242   vpx_lpf_vertical_8_c(s + 8 * pitch, pitch, blimit1, limit1,
    243                                     thresh1, 1);
    244 }
    245 
    246 static INLINE void filter16(int8_t mask, uint8_t thresh,
    247                             uint8_t flat, uint8_t flat2,
    248                             uint8_t *op7, uint8_t *op6,
    249                             uint8_t *op5, uint8_t *op4,
    250                             uint8_t *op3, uint8_t *op2,
    251                             uint8_t *op1, uint8_t *op0,
    252                             uint8_t *oq0, uint8_t *oq1,
    253                             uint8_t *oq2, uint8_t *oq3,
    254                             uint8_t *oq4, uint8_t *oq5,
    255                             uint8_t *oq6, uint8_t *oq7) {
    256   if (flat2 && flat && mask) {
    257     const uint8_t p7 = *op7, p6 = *op6, p5 = *op5, p4 = *op4,
    258                   p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
    259 
    260     const uint8_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3,
    261                   q4 = *oq4, q5 = *oq5, q6 = *oq6, q7 = *oq7;
    262 
    263     // 15-tap filter [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1]
    264     *op6 = ROUND_POWER_OF_TWO(p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 +
    265                               q0, 4);
    266     *op5 = ROUND_POWER_OF_TWO(p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 +
    267                               q0 + q1, 4);
    268     *op4 = ROUND_POWER_OF_TWO(p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 +
    269                               q0 + q1 + q2, 4);
    270     *op3 = ROUND_POWER_OF_TWO(p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 +
    271                               q0 + q1 + q2 + q3, 4);
    272     *op2 = ROUND_POWER_OF_TWO(p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 +
    273                               q0 + q1 + q2 + q3 + q4, 4);
    274     *op1 = ROUND_POWER_OF_TWO(p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 +
    275                               q0 + q1 + q2 + q3 + q4 + q5, 4);
    276     *op0 = ROUND_POWER_OF_TWO(p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +
    277                               q0 + q1 + q2 + q3 + q4 + q5 + q6, 4);
    278     *oq0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 + p0 +
    279                               q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7, 4);
    280     *oq1 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 +
    281                               q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2, 4);
    282     *oq2 = ROUND_POWER_OF_TWO(p4 + p3 + p2 + p1 + p0 +
    283                               q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3, 4);
    284     *oq3 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + p0 +
    285                               q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4, 4);
    286     *oq4 = ROUND_POWER_OF_TWO(p2 + p1 + p0 +
    287                               q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5, 4);
    288     *oq5 = ROUND_POWER_OF_TWO(p1 + p0 +
    289                               q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6, 4);
    290     *oq6 = ROUND_POWER_OF_TWO(p0 +
    291                               q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7, 4);
    292   } else {
    293     filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3);
    294   }
    295 }
    296 
    297 void vpx_lpf_horizontal_16_c(uint8_t *s, int p, const uint8_t *blimit,
    298                              const uint8_t *limit, const uint8_t *thresh,
    299                              int count) {
    300   int i;
    301 
    302   // loop filter designed to work using chars so that we can make maximum use
    303   // of 8 bit simd instructions.
    304   for (i = 0; i < 8 * count; ++i) {
    305     const uint8_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
    306     const uint8_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
    307     const int8_t mask = filter_mask(*limit, *blimit,
    308                                     p3, p2, p1, p0, q0, q1, q2, q3);
    309     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
    310     const int8_t flat2 = flat_mask5(1,
    311                              s[-8 * p], s[-7 * p], s[-6 * p], s[-5 * p], p0,
    312                              q0, s[4 * p], s[5 * p], s[6 * p], s[7 * p]);
    313 
    314     filter16(mask, *thresh, flat, flat2,
    315              s - 8 * p, s - 7 * p, s - 6 * p, s - 5 * p,
    316              s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
    317              s,         s + 1 * p, s + 2 * p, s + 3 * p,
    318              s + 4 * p, s + 5 * p, s + 6 * p, s + 7 * p);
    319     ++s;
    320   }
    321 }
    322 
    323 static void mb_lpf_vertical_edge_w(uint8_t *s, int p,
    324                                    const uint8_t *blimit,
    325                                    const uint8_t *limit,
    326                                    const uint8_t *thresh,
    327                                    int count) {
    328   int i;
    329 
    330   for (i = 0; i < count; ++i) {
    331     const uint8_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
    332     const uint8_t q0 = s[0], q1 = s[1],  q2 = s[2], q3 = s[3];
    333     const int8_t mask = filter_mask(*limit, *blimit,
    334                                     p3, p2, p1, p0, q0, q1, q2, q3);
    335     const int8_t flat = flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3);
    336     const int8_t flat2 = flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0,
    337                                     q0, s[4], s[5], s[6], s[7]);
    338 
    339     filter16(mask, *thresh, flat, flat2,
    340              s - 8, s - 7, s - 6, s - 5, s - 4, s - 3, s - 2, s - 1,
    341              s,     s + 1, s + 2, s + 3, s + 4, s + 5, s + 6, s + 7);
    342     s += p;
    343   }
    344 }
    345 
    346 void vpx_lpf_vertical_16_c(uint8_t *s, int p, const uint8_t *blimit,
    347                            const uint8_t *limit, const uint8_t *thresh) {
    348   mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 8);
    349 }
    350 
    351 void vpx_lpf_vertical_16_dual_c(uint8_t *s, int p, const uint8_t *blimit,
    352                                 const uint8_t *limit, const uint8_t *thresh) {
    353   mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 16);
    354 }
    355 
    356 #if CONFIG_VP9_HIGHBITDEPTH
    357 // Should we apply any filter at all: 11111111 yes, 00000000 no ?
    358 static INLINE int8_t highbd_filter_mask(uint8_t limit, uint8_t blimit,
    359                                         uint16_t p3, uint16_t p2,
    360                                         uint16_t p1, uint16_t p0,
    361                                         uint16_t q0, uint16_t q1,
    362                                         uint16_t q2, uint16_t q3, int bd) {
    363   int8_t mask = 0;
    364   int16_t limit16 = (uint16_t)limit << (bd - 8);
    365   int16_t blimit16 = (uint16_t)blimit << (bd - 8);
    366   mask |= (abs(p3 - p2) > limit16) * -1;
    367   mask |= (abs(p2 - p1) > limit16) * -1;
    368   mask |= (abs(p1 - p0) > limit16) * -1;
    369   mask |= (abs(q1 - q0) > limit16) * -1;
    370   mask |= (abs(q2 - q1) > limit16) * -1;
    371   mask |= (abs(q3 - q2) > limit16) * -1;
    372   mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2  > blimit16) * -1;
    373   return ~mask;
    374 }
    375 
    376 static INLINE int8_t highbd_flat_mask4(uint8_t thresh,
    377                                        uint16_t p3, uint16_t p2,
    378                                        uint16_t p1, uint16_t p0,
    379                                        uint16_t q0, uint16_t q1,
    380                                        uint16_t q2, uint16_t q3, int bd) {
    381   int8_t mask = 0;
    382   int16_t thresh16 = (uint16_t)thresh << (bd - 8);
    383   mask |= (abs(p1 - p0) > thresh16) * -1;
    384   mask |= (abs(q1 - q0) > thresh16) * -1;
    385   mask |= (abs(p2 - p0) > thresh16) * -1;
    386   mask |= (abs(q2 - q0) > thresh16) * -1;
    387   mask |= (abs(p3 - p0) > thresh16) * -1;
    388   mask |= (abs(q3 - q0) > thresh16) * -1;
    389   return ~mask;
    390 }
    391 
    392 static INLINE int8_t highbd_flat_mask5(uint8_t thresh,
    393                                        uint16_t p4, uint16_t p3,
    394                                        uint16_t p2, uint16_t p1,
    395                                        uint16_t p0, uint16_t q0,
    396                                        uint16_t q1, uint16_t q2,
    397                                        uint16_t q3, uint16_t q4, int bd) {
    398   int8_t mask = ~highbd_flat_mask4(thresh, p3, p2, p1, p0, q0, q1, q2, q3, bd);
    399   int16_t thresh16 = (uint16_t)thresh << (bd - 8);
    400   mask |= (abs(p4 - p0) > thresh16) * -1;
    401   mask |= (abs(q4 - q0) > thresh16) * -1;
    402   return ~mask;
    403 }
    404 
    405 // Is there high edge variance internal edge:
    406 // 11111111_11111111 yes, 00000000_00000000 no ?
    407 static INLINE int16_t highbd_hev_mask(uint8_t thresh, uint16_t p1, uint16_t p0,
    408                                       uint16_t q0, uint16_t q1, int bd) {
    409   int16_t hev = 0;
    410   int16_t thresh16 = (uint16_t)thresh << (bd - 8);
    411   hev |= (abs(p1 - p0) > thresh16) * -1;
    412   hev |= (abs(q1 - q0) > thresh16) * -1;
    413   return hev;
    414 }
    415 
    416 static INLINE void highbd_filter4(int8_t mask, uint8_t thresh, uint16_t *op1,
    417                                   uint16_t *op0, uint16_t *oq0, uint16_t *oq1,
    418                                   int bd) {
    419   int16_t filter1, filter2;
    420   // ^0x80 equivalent to subtracting 0x80 from the values to turn them
    421   // into -128 to +127 instead of 0 to 255.
    422   int shift = bd - 8;
    423   const int16_t ps1 = (int16_t)*op1 - (0x80 << shift);
    424   const int16_t ps0 = (int16_t)*op0 - (0x80 << shift);
    425   const int16_t qs0 = (int16_t)*oq0 - (0x80 << shift);
    426   const int16_t qs1 = (int16_t)*oq1 - (0x80 << shift);
    427   const uint16_t hev = highbd_hev_mask(thresh, *op1, *op0, *oq0, *oq1, bd);
    428 
    429   // Add outer taps if we have high edge variance.
    430   int16_t filter = signed_char_clamp_high(ps1 - qs1, bd) & hev;
    431 
    432   // Inner taps.
    433   filter = signed_char_clamp_high(filter + 3 * (qs0 - ps0), bd) & mask;
    434 
    435   // Save bottom 3 bits so that we round one side +4 and the other +3
    436   // if it equals 4 we'll set to adjust by -1 to account for the fact
    437   // we'd round 3 the other way.
    438   filter1 = signed_char_clamp_high(filter + 4, bd) >> 3;
    439   filter2 = signed_char_clamp_high(filter + 3, bd) >> 3;
    440 
    441   *oq0 = signed_char_clamp_high(qs0 - filter1, bd) + (0x80 << shift);
    442   *op0 = signed_char_clamp_high(ps0 + filter2, bd) + (0x80 << shift);
    443 
    444   // Outer tap adjustments.
    445   filter = ROUND_POWER_OF_TWO(filter1, 1) & ~hev;
    446 
    447   *oq1 = signed_char_clamp_high(qs1 - filter, bd) + (0x80 << shift);
    448   *op1 = signed_char_clamp_high(ps1 + filter, bd) + (0x80 << shift);
    449 }
    450 
    451 void vpx_highbd_lpf_horizontal_4_c(uint16_t *s, int p /* pitch */,
    452                                    const uint8_t *blimit, const uint8_t *limit,
    453                                    const uint8_t *thresh, int count, int bd) {
    454   int i;
    455 
    456   // loop filter designed to work using chars so that we can make maximum use
    457   // of 8 bit simd instructions.
    458   for (i = 0; i < 8 * count; ++i) {
    459     const uint16_t p3 = s[-4 * p];
    460     const uint16_t p2 = s[-3 * p];
    461     const uint16_t p1 = s[-2 * p];
    462     const uint16_t p0 = s[-p];
    463     const uint16_t q0 = s[0 * p];
    464     const uint16_t q1 = s[1 * p];
    465     const uint16_t q2 = s[2 * p];
    466     const uint16_t q3 = s[3 * p];
    467     const int8_t mask = highbd_filter_mask(*limit, *blimit,
    468                                            p3, p2, p1, p0, q0, q1, q2, q3, bd);
    469     highbd_filter4(mask, *thresh, s - 2 * p, s - 1 * p, s, s + 1 * p, bd);
    470     ++s;
    471   }
    472 }
    473 
    474 void vpx_highbd_lpf_horizontal_4_dual_c(uint16_t *s, int p,
    475                                         const uint8_t *blimit0,
    476                                         const uint8_t *limit0,
    477                                         const uint8_t *thresh0,
    478                                         const uint8_t *blimit1,
    479                                         const uint8_t *limit1,
    480                                         const uint8_t *thresh1,
    481                                         int bd) {
    482   vpx_highbd_lpf_horizontal_4_c(s, p, blimit0, limit0, thresh0, 1, bd);
    483   vpx_highbd_lpf_horizontal_4_c(s + 8, p, blimit1, limit1, thresh1, 1, bd);
    484 }
    485 
    486 void vpx_highbd_lpf_vertical_4_c(uint16_t *s, int pitch, const uint8_t *blimit,
    487                                  const uint8_t *limit, const uint8_t *thresh,
    488                                  int count, int bd) {
    489   int i;
    490 
    491   // loop filter designed to work using chars so that we can make maximum use
    492   // of 8 bit simd instructions.
    493   for (i = 0; i < 8 * count; ++i) {
    494     const uint16_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
    495     const uint16_t q0 = s[0],  q1 = s[1],  q2 = s[2],  q3 = s[3];
    496     const int8_t mask = highbd_filter_mask(*limit, *blimit,
    497                                            p3, p2, p1, p0, q0, q1, q2, q3, bd);
    498     highbd_filter4(mask, *thresh, s - 2, s - 1, s, s + 1, bd);
    499     s += pitch;
    500   }
    501 }
    502 
    503 void vpx_highbd_lpf_vertical_4_dual_c(uint16_t *s, int pitch,
    504                                       const uint8_t *blimit0,
    505                                       const uint8_t *limit0,
    506                                       const uint8_t *thresh0,
    507                                       const uint8_t *blimit1,
    508                                       const uint8_t *limit1,
    509                                       const uint8_t *thresh1,
    510                                       int bd) {
    511   vpx_highbd_lpf_vertical_4_c(s, pitch, blimit0, limit0, thresh0, 1, bd);
    512   vpx_highbd_lpf_vertical_4_c(s + 8 * pitch, pitch, blimit1, limit1,
    513                               thresh1, 1, bd);
    514 }
    515 
    516 static INLINE void highbd_filter8(int8_t mask, uint8_t thresh, uint8_t flat,
    517                                   uint16_t *op3, uint16_t *op2,
    518                                   uint16_t *op1, uint16_t *op0,
    519                                   uint16_t *oq0, uint16_t *oq1,
    520                                   uint16_t *oq2, uint16_t *oq3, int bd) {
    521   if (flat && mask) {
    522     const uint16_t p3 = *op3, p2 = *op2, p1 = *op1, p0 = *op0;
    523     const uint16_t q0 = *oq0, q1 = *oq1, q2 = *oq2, q3 = *oq3;
    524 
    525     // 7-tap filter [1, 1, 1, 2, 1, 1, 1]
    526     *op2 = ROUND_POWER_OF_TWO(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3);
    527     *op1 = ROUND_POWER_OF_TWO(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3);
    528     *op0 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3);
    529     *oq0 = ROUND_POWER_OF_TWO(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3);
    530     *oq1 = ROUND_POWER_OF_TWO(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3);
    531     *oq2 = ROUND_POWER_OF_TWO(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3);
    532   } else {
    533     highbd_filter4(mask, thresh, op1,  op0, oq0, oq1, bd);
    534   }
    535 }
    536 
    537 void vpx_highbd_lpf_horizontal_8_c(uint16_t *s, int p, const uint8_t *blimit,
    538                                    const uint8_t *limit, const uint8_t *thresh,
    539                                    int count, int bd) {
    540   int i;
    541 
    542   // loop filter designed to work using chars so that we can make maximum use
    543   // of 8 bit simd instructions.
    544   for (i = 0; i < 8 * count; ++i) {
    545     const uint16_t p3 = s[-4 * p], p2 = s[-3 * p], p1 = s[-2 * p], p0 = s[-p];
    546     const uint16_t q0 = s[0 * p], q1 = s[1 * p], q2 = s[2 * p], q3 = s[3 * p];
    547 
    548     const int8_t mask = highbd_filter_mask(*limit, *blimit,
    549                                          p3, p2, p1, p0, q0, q1, q2, q3, bd);
    550     const int8_t flat = highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3,
    551                                           bd);
    552     highbd_filter8(mask, *thresh, flat,
    553                  s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
    554                  s, s + 1 * p, s + 2 * p, s + 3 * p, bd);
    555     ++s;
    556   }
    557 }
    558 
    559 void vpx_highbd_lpf_horizontal_8_dual_c(uint16_t *s, int p,
    560                                         const uint8_t *blimit0,
    561                                         const uint8_t *limit0,
    562                                         const uint8_t *thresh0,
    563                                         const uint8_t *blimit1,
    564                                         const uint8_t *limit1,
    565                                         const uint8_t *thresh1,
    566                                         int bd) {
    567   vpx_highbd_lpf_horizontal_8_c(s, p, blimit0, limit0, thresh0, 1, bd);
    568   vpx_highbd_lpf_horizontal_8_c(s + 8, p, blimit1, limit1, thresh1, 1, bd);
    569 }
    570 
    571 void vpx_highbd_lpf_vertical_8_c(uint16_t *s, int pitch, const uint8_t *blimit,
    572                                  const uint8_t *limit, const uint8_t *thresh,
    573                                  int count, int bd) {
    574   int i;
    575 
    576   for (i = 0; i < 8 * count; ++i) {
    577     const uint16_t p3 = s[-4], p2 = s[-3], p1 = s[-2], p0 = s[-1];
    578     const uint16_t q0 = s[0], q1 = s[1], q2 = s[2], q3 = s[3];
    579     const int8_t mask = highbd_filter_mask(*limit, *blimit,
    580                                            p3, p2, p1, p0, q0, q1, q2, q3, bd);
    581     const int8_t flat = highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3,
    582                                           bd);
    583     highbd_filter8(mask, *thresh, flat,
    584                  s - 4, s - 3, s - 2, s - 1,
    585                  s, s + 1, s + 2, s + 3,
    586                  bd);
    587     s += pitch;
    588   }
    589 }
    590 
    591 void vpx_highbd_lpf_vertical_8_dual_c(uint16_t *s, int pitch,
    592                                       const uint8_t *blimit0,
    593                                       const uint8_t *limit0,
    594                                       const uint8_t *thresh0,
    595                                       const uint8_t *blimit1,
    596                                       const uint8_t *limit1,
    597                                       const uint8_t *thresh1,
    598                                       int bd) {
    599   vpx_highbd_lpf_vertical_8_c(s, pitch, blimit0, limit0, thresh0, 1, bd);
    600   vpx_highbd_lpf_vertical_8_c(s + 8 * pitch, pitch, blimit1, limit1,
    601                               thresh1, 1, bd);
    602 }
    603 
    604 static INLINE void highbd_filter16(int8_t mask, uint8_t thresh,
    605                                    uint8_t flat, uint8_t flat2,
    606                                    uint16_t *op7, uint16_t *op6,
    607                                    uint16_t *op5, uint16_t *op4,
    608                                    uint16_t *op3, uint16_t *op2,
    609                                    uint16_t *op1, uint16_t *op0,
    610                                    uint16_t *oq0, uint16_t *oq1,
    611                                    uint16_t *oq2, uint16_t *oq3,
    612                                    uint16_t *oq4, uint16_t *oq5,
    613                                    uint16_t *oq6, uint16_t *oq7, int bd) {
    614   if (flat2 && flat && mask) {
    615     const uint16_t p7 = *op7;
    616     const uint16_t p6 = *op6;
    617     const uint16_t p5 = *op5;
    618     const uint16_t p4 = *op4;
    619     const uint16_t p3 = *op3;
    620     const uint16_t p2 = *op2;
    621     const uint16_t p1 = *op1;
    622     const uint16_t p0 = *op0;
    623     const uint16_t q0 = *oq0;
    624     const uint16_t q1 = *oq1;
    625     const uint16_t q2 = *oq2;
    626     const uint16_t q3 = *oq3;
    627     const uint16_t q4 = *oq4;
    628     const uint16_t q5 = *oq5;
    629     const uint16_t q6 = *oq6;
    630     const uint16_t q7 = *oq7;
    631 
    632     // 15-tap filter [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1]
    633     *op6 = ROUND_POWER_OF_TWO(p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 +
    634                               q0, 4);
    635     *op5 = ROUND_POWER_OF_TWO(p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 +
    636                               q0 + q1, 4);
    637     *op4 = ROUND_POWER_OF_TWO(p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 +
    638                               q0 + q1 + q2, 4);
    639     *op3 = ROUND_POWER_OF_TWO(p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 +
    640                               q0 + q1 + q2 + q3, 4);
    641     *op2 = ROUND_POWER_OF_TWO(p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 +
    642                               q0 + q1 + q2 + q3 + q4, 4);
    643     *op1 = ROUND_POWER_OF_TWO(p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 +
    644                               q0 + q1 + q2 + q3 + q4 + q5, 4);
    645     *op0 = ROUND_POWER_OF_TWO(p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +
    646                               q0 + q1 + q2 + q3 + q4 + q5 + q6, 4);
    647     *oq0 = ROUND_POWER_OF_TWO(p6 + p5 + p4 + p3 + p2 + p1 + p0 +
    648                               q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7, 4);
    649     *oq1 = ROUND_POWER_OF_TWO(p5 + p4 + p3 + p2 + p1 + p0 +
    650                               q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2, 4);
    651     *oq2 = ROUND_POWER_OF_TWO(p4 + p3 + p2 + p1 + p0 +
    652                               q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3, 4);
    653     *oq3 = ROUND_POWER_OF_TWO(p3 + p2 + p1 + p0 +
    654                               q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4, 4);
    655     *oq4 = ROUND_POWER_OF_TWO(p2 + p1 + p0 +
    656                               q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5, 4);
    657     *oq5 = ROUND_POWER_OF_TWO(p1 + p0 +
    658                               q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6, 4);
    659     *oq6 = ROUND_POWER_OF_TWO(p0 +
    660                               q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7, 4);
    661   } else {
    662     highbd_filter8(mask, thresh, flat, op3, op2, op1, op0, oq0, oq1, oq2, oq3,
    663                    bd);
    664   }
    665 }
    666 
    667 void vpx_highbd_lpf_horizontal_16_c(uint16_t *s, int p, const uint8_t *blimit,
    668                                     const uint8_t *limit, const uint8_t *thresh,
    669                                     int count, int bd) {
    670   int i;
    671 
    672   // loop filter designed to work using chars so that we can make maximum use
    673   // of 8 bit simd instructions.
    674   for (i = 0; i < 8 * count; ++i) {
    675     const uint16_t p3 = s[-4 * p];
    676     const uint16_t p2 = s[-3 * p];
    677     const uint16_t p1 = s[-2 * p];
    678     const uint16_t p0 = s[-p];
    679     const uint16_t q0 = s[0 * p];
    680     const uint16_t q1 = s[1 * p];
    681     const uint16_t q2 = s[2 * p];
    682     const uint16_t q3 = s[3 * p];
    683     const int8_t mask = highbd_filter_mask(*limit, *blimit,
    684                                            p3, p2, p1, p0, q0, q1, q2, q3, bd);
    685     const int8_t flat = highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3,
    686                                           bd);
    687     const int8_t flat2 = highbd_flat_mask5(
    688         1, s[-8 * p], s[-7 * p], s[-6 * p], s[-5 * p], p0,
    689         q0, s[4 * p], s[5 * p], s[6 * p], s[7 * p], bd);
    690 
    691     highbd_filter16(mask, *thresh, flat, flat2,
    692                     s - 8 * p, s - 7 * p, s - 6 * p, s - 5 * p,
    693                     s - 4 * p, s - 3 * p, s - 2 * p, s - 1 * p,
    694                     s, s + 1 * p, s + 2 * p, s + 3 * p,
    695                     s + 4 * p, s + 5 * p, s + 6 * p, s + 7 * p,
    696                     bd);
    697     ++s;
    698   }
    699 }
    700 
    701 static void highbd_mb_lpf_vertical_edge_w(uint16_t *s, int p,
    702                                           const uint8_t *blimit,
    703                                           const uint8_t *limit,
    704                                           const uint8_t *thresh,
    705                                           int count, int bd) {
    706   int i;
    707 
    708   for (i = 0; i < count; ++i) {
    709     const uint16_t p3 = s[-4];
    710     const uint16_t p2 = s[-3];
    711     const uint16_t p1 = s[-2];
    712     const uint16_t p0 = s[-1];
    713     const uint16_t q0 = s[0];
    714     const uint16_t q1 = s[1];
    715     const uint16_t q2 = s[2];
    716     const uint16_t q3 = s[3];
    717     const int8_t mask = highbd_filter_mask(*limit, *blimit,
    718                                            p3, p2, p1, p0, q0, q1, q2, q3, bd);
    719     const int8_t flat = highbd_flat_mask4(1, p3, p2, p1, p0, q0, q1, q2, q3,
    720                                           bd);
    721     const int8_t flat2 = highbd_flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0,
    722                                            q0, s[4], s[5], s[6], s[7], bd);
    723 
    724     highbd_filter16(mask, *thresh, flat, flat2,
    725                     s - 8, s - 7, s - 6, s - 5, s - 4, s - 3, s - 2, s - 1,
    726                     s, s + 1, s + 2, s + 3, s + 4, s + 5, s + 6, s + 7,
    727                     bd);
    728     s += p;
    729   }
    730 }
    731 
    732 void vpx_highbd_lpf_vertical_16_c(uint16_t *s, int p, const uint8_t *blimit,
    733                                   const uint8_t *limit, const uint8_t *thresh,
    734                                   int bd) {
    735   highbd_mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 8, bd);
    736 }
    737 
    738 void vpx_highbd_lpf_vertical_16_dual_c(uint16_t *s, int p,
    739                                        const uint8_t *blimit,
    740                                        const uint8_t *limit,
    741                                        const uint8_t *thresh,
    742                                        int bd) {
    743   highbd_mb_lpf_vertical_edge_w(s, p, blimit, limit, thresh, 16, bd);
    744 }
    745 #endif  // CONFIG_VP9_HIGHBITDEPTH
    746