tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

wiener_convolve_avx2.c (9956B)


      1 /*
      2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
      3 *
      4 * This source code is subject to the terms of the BSD 2 Clause License and
      5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      6 * was not distributed with this source code in the LICENSE file, you can
      7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      8 * Media Patent License 1.0 was not distributed with this source code in the
      9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
     10 */
     11 
     12 #include <immintrin.h>
     13 #include <assert.h>
     14 
     15 #include "config/av1_rtcd.h"
     16 
     17 #include "av1/common/convolve.h"
     18 #include "aom_dsp/aom_dsp_common.h"
     19 #include "aom_dsp/aom_filter.h"
     20 #include "aom_dsp/x86/convolve_avx2.h"
     21 #include "aom_dsp/x86/synonyms.h"
     22 #include "aom_dsp/x86/synonyms_avx2.h"
     23 
     24 // 128-bit xmmwords are written as [ ... ] with the MSB on the left.
     25 // 256-bit ymmwords are written as two xmmwords, [ ... ][ ... ] with the MSB
     26 // on the left.
     27 // A row of, say, 8-bit pixels with values p0, p1, p2, ..., p30, p31 will be
     28 // loaded and stored as [ p31 ... p17 p16 ][ p15 ... p1 p0 ].
     29 
     30 // Exploiting the range of wiener filter coefficients,
     31 // horizontal filtering can be done in 16 bit intermediate precision.
     32 // The details are as follows :
     33 // Consider the horizontal wiener filter coefficients of the following form :
     34 //      [C0, C1, C2, 2^(FILTER_BITS) -2 * (C0 + C1 + C2), C2, C1, C0]
     35 // Subtracting  2^(FILTER_BITS) from the centre tap we get the following  :
     36 //      [C0, C1, C2,     -2 * (C0 + C1 + C2),             C2, C1, C0]
     37 // The sum of the product "C0 * p0 + C1 * p1 + C2 * p2 -2 * (C0 + C1 + C2) * p3
     38 // + C2 * p4 + C1 * p5 + C0 * p6" would be in the range of signed 16 bit
     39 // precision. Finally, after rounding the above result by round_0, we multiply
     40 // the centre pixel by 2^(FILTER_BITS - round_0) and add it to get the
     41 // horizontal filter output.
     42 
     43 void av1_wiener_convolve_add_src_avx2(const uint8_t *src, ptrdiff_t src_stride,
     44                                      uint8_t *dst, ptrdiff_t dst_stride,
     45                                      const int16_t *filter_x, int x_step_q4,
     46                                      const int16_t *filter_y, int y_step_q4,
     47                                      int w, int h,
     48                                      const WienerConvolveParams *conv_params) {
     49  const int bd = 8;
     50  assert(x_step_q4 == 16 && y_step_q4 == 16);
     51  assert(!(w & 7));
     52  (void)x_step_q4;
     53  (void)y_step_q4;
     54 
     55  DECLARE_ALIGNED(32, int16_t, im_block[(MAX_SB_SIZE + SUBPEL_TAPS) * 8]);
     56  int im_h = h + SUBPEL_TAPS - 2;
     57  int im_stride = 8;
     58  memset(im_block + (im_h * im_stride), 0, MAX_SB_SIZE);
     59  int i, j;
     60  const int center_tap = (SUBPEL_TAPS - 1) / 2;
     61  const uint8_t *const src_ptr = src - center_tap * src_stride - center_tap;
     62 
     63  __m256i filt[4], coeffs_h[4], coeffs_v[4], filt_center;
     64 
     65  assert(conv_params->round_0 > 0);
     66 
     67  filt[0] = _mm256_load_si256((__m256i const *)filt1_global_avx2);
     68  filt[1] = _mm256_load_si256((__m256i const *)filt2_global_avx2);
     69  filt[2] = _mm256_load_si256((__m256i const *)filt3_global_avx2);
     70  filt[3] = _mm256_load_si256((__m256i const *)filt4_global_avx2);
     71 
     72  filt_center = _mm256_load_si256((__m256i const *)filt_center_global_avx2);
     73 
     74  const __m128i coeffs_x = _mm_loadu_si128((__m128i *)filter_x);
     75  const __m256i filter_coeffs_x = _mm256_broadcastsi128_si256(coeffs_x);
     76 
     77  // coeffs 0 1 0 1 0 1 0 1
     78  coeffs_h[0] =
     79      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0200u));
     80  // coeffs 2 3 2 3 2 3 2 3
     81  coeffs_h[1] =
     82      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0604u));
     83  // coeffs 4 5 4 5 4 5 4 5
     84  coeffs_h[2] =
     85      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0a08u));
     86  // coeffs 6 7 6 7 6 7 6 7
     87  coeffs_h[3] =
     88      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0e0cu));
     89 
     90  const __m256i round_const_h =
     91      _mm256_set1_epi16((1 << (conv_params->round_0 - 1)));
     92  const __m256i round_const_horz =
     93      _mm256_set1_epi16((1 << (bd + FILTER_BITS - conv_params->round_0 - 1)));
     94  const __m256i clamp_low = _mm256_setzero_si256();
     95  const __m256i clamp_high =
     96      _mm256_set1_epi16(WIENER_CLAMP_LIMIT(conv_params->round_0, bd) - 1);
     97  const __m128i round_shift_h = _mm_cvtsi32_si128(conv_params->round_0);
     98 
     99  // Add an offset to account for the "add_src" part of the convolve function.
    100  const __m128i zero_128 = _mm_setzero_si128();
    101  const __m128i offset_0 = _mm_insert_epi16(zero_128, 1 << FILTER_BITS, 3);
    102  const __m128i coeffs_y = _mm_add_epi16(xx_loadu_128(filter_y), offset_0);
    103 
    104  const __m256i filter_coeffs_y = _mm256_broadcastsi128_si256(coeffs_y);
    105 
    106  // coeffs 0 1 0 1 0 1 0 1
    107  coeffs_v[0] = _mm256_shuffle_epi32(filter_coeffs_y, 0x00);
    108  // coeffs 2 3 2 3 2 3 2 3
    109  coeffs_v[1] = _mm256_shuffle_epi32(filter_coeffs_y, 0x55);
    110  // coeffs 4 5 4 5 4 5 4 5
    111  coeffs_v[2] = _mm256_shuffle_epi32(filter_coeffs_y, 0xaa);
    112  // coeffs 6 7 6 7 6 7 6 7
    113  coeffs_v[3] = _mm256_shuffle_epi32(filter_coeffs_y, 0xff);
    114 
    115  const __m256i round_const_v =
    116      _mm256_set1_epi32((1 << (conv_params->round_1 - 1)) -
    117                        (1 << (bd + conv_params->round_1 - 1)));
    118  const __m128i round_shift_v = _mm_cvtsi32_si128(conv_params->round_1);
    119 
    120  for (j = 0; j < w; j += 8) {
    121    for (i = 0; i < im_h; i += 2) {
    122      __m256i data = _mm256_castsi128_si256(
    123          _mm_loadu_si128((__m128i *)&src_ptr[(i * src_stride) + j]));
    124 
    125      // Load the next line
    126      if (i + 1 < im_h)
    127        data = _mm256_inserti128_si256(
    128            data,
    129            _mm_loadu_si128(
    130                (__m128i *)&src_ptr[(i * src_stride) + j + src_stride]),
    131            1);
    132 
    133      __m256i res = convolve_lowbd_x(data, coeffs_h, filt);
    134 
    135      res =
    136          _mm256_sra_epi16(_mm256_add_epi16(res, round_const_h), round_shift_h);
    137 
    138      __m256i data_0 = _mm256_shuffle_epi8(data, filt_center);
    139 
    140      // multiply the center pixel by 2^(FILTER_BITS - round_0) and add it to
    141      // the result
    142      data_0 = _mm256_slli_epi16(data_0, FILTER_BITS - conv_params->round_0);
    143      res = _mm256_add_epi16(res, data_0);
    144      res = _mm256_add_epi16(res, round_const_horz);
    145      const __m256i res_clamped =
    146          _mm256_min_epi16(_mm256_max_epi16(res, clamp_low), clamp_high);
    147      _mm256_store_si256((__m256i *)&im_block[i * im_stride], res_clamped);
    148    }
    149 
    150    /* Vertical filter */
    151    {
    152      __m256i src_0 = _mm256_loadu_si256((__m256i *)(im_block + 0 * im_stride));
    153      __m256i src_1 = _mm256_loadu_si256((__m256i *)(im_block + 1 * im_stride));
    154      __m256i src_2 = _mm256_loadu_si256((__m256i *)(im_block + 2 * im_stride));
    155      __m256i src_3 = _mm256_loadu_si256((__m256i *)(im_block + 3 * im_stride));
    156      __m256i src_4 = _mm256_loadu_si256((__m256i *)(im_block + 4 * im_stride));
    157      __m256i src_5 = _mm256_loadu_si256((__m256i *)(im_block + 5 * im_stride));
    158 
    159      __m256i s[8];
    160      s[0] = _mm256_unpacklo_epi16(src_0, src_1);
    161      s[1] = _mm256_unpacklo_epi16(src_2, src_3);
    162      s[2] = _mm256_unpacklo_epi16(src_4, src_5);
    163 
    164      s[4] = _mm256_unpackhi_epi16(src_0, src_1);
    165      s[5] = _mm256_unpackhi_epi16(src_2, src_3);
    166      s[6] = _mm256_unpackhi_epi16(src_4, src_5);
    167 
    168      for (i = 0; i < h - 1; i += 2) {
    169        const int16_t *data = &im_block[i * im_stride];
    170 
    171        const __m256i s6 =
    172            _mm256_loadu_si256((__m256i *)(data + 6 * im_stride));
    173        const __m256i s7 =
    174            _mm256_loadu_si256((__m256i *)(data + 7 * im_stride));
    175 
    176        s[3] = _mm256_unpacklo_epi16(s6, s7);
    177        s[7] = _mm256_unpackhi_epi16(s6, s7);
    178 
    179        __m256i res_a = convolve(s, coeffs_v);
    180        __m256i res_b = convolve(s + 4, coeffs_v);
    181 
    182        const __m256i res_a_round = _mm256_sra_epi32(
    183            _mm256_add_epi32(res_a, round_const_v), round_shift_v);
    184        const __m256i res_b_round = _mm256_sra_epi32(
    185            _mm256_add_epi32(res_b, round_const_v), round_shift_v);
    186 
    187        /* rounding code */
    188        // 16 bit conversion
    189        const __m256i res_16bit = _mm256_packs_epi32(res_a_round, res_b_round);
    190        // 8 bit conversion and saturation to uint8
    191        const __m256i res_8b = _mm256_packus_epi16(res_16bit, res_16bit);
    192 
    193        const __m128i res_0 = _mm256_castsi256_si128(res_8b);
    194        const __m128i res_1 = _mm256_extracti128_si256(res_8b, 1);
    195 
    196        // Store values into the destination buffer
    197        __m128i *const p_0 = (__m128i *)&dst[i * dst_stride + j];
    198        __m128i *const p_1 = (__m128i *)&dst[i * dst_stride + j + dst_stride];
    199 
    200        _mm_storel_epi64(p_0, res_0);
    201        _mm_storel_epi64(p_1, res_1);
    202 
    203        s[0] = s[1];
    204        s[1] = s[2];
    205        s[2] = s[3];
    206 
    207        s[4] = s[5];
    208        s[5] = s[6];
    209        s[6] = s[7];
    210      }
    211      if (h - i) {
    212        s[0] = _mm256_permute2x128_si256(s[0], s[4], 0x20);
    213        s[1] = _mm256_permute2x128_si256(s[1], s[5], 0x20);
    214        s[2] = _mm256_permute2x128_si256(s[2], s[6], 0x20);
    215 
    216        const int16_t *data = &im_block[i * im_stride];
    217        const __m128i s6_ = _mm_loadu_si128((__m128i *)(data + 6 * im_stride));
    218        const __m128i s7_ = _mm_loadu_si128((__m128i *)(data + 7 * im_stride));
    219 
    220        __m128i s3 = _mm_unpacklo_epi16(s6_, s7_);
    221        __m128i s7 = _mm_unpackhi_epi16(s6_, s7_);
    222 
    223        s[3] = _mm256_inserti128_si256(_mm256_castsi128_si256(s3), s7, 1);
    224        __m256i convolveres = convolve(s, coeffs_v);
    225 
    226        const __m256i res_round = _mm256_sra_epi32(
    227            _mm256_add_epi32(convolveres, round_const_v), round_shift_v);
    228 
    229        /* rounding code */
    230        // 16 bit conversion
    231        __m128i reslo = _mm256_castsi256_si128(res_round);
    232        __m128i reshi = _mm256_extracti128_si256(res_round, 1);
    233        const __m128i res_16bit = _mm_packus_epi32(reslo, reshi);
    234 
    235        // 8 bit conversion and saturation to uint8
    236        const __m128i res_8b = _mm_packus_epi16(res_16bit, res_16bit);
    237        __m128i *const p_0 = (__m128i *)&dst[i * dst_stride + j];
    238        _mm_storel_epi64(p_0, res_8b);
    239      }
    240    }
    241  }
    242 }