go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

yuv_neon.c (7396B)


      1 // Copyright 2017 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // YUV->RGB conversion functions
     11 //
     12 // Author: Skal (pascal.massimino@gmail.com)
     13 
     14 #include "src/dsp/yuv.h"
     15 
     16 #if defined(WEBP_USE_NEON)
     17 
     18 #include <assert.h>
     19 #include <stdlib.h>
     20 
     21 #include "src/dsp/dsp.h"
     22 #include "src/dsp/neon.h"
     23 
     24 //-----------------------------------------------------------------------------
     25 
     26 static uint8x8_t ConvertRGBToY_NEON(const uint8x8_t R,
     27                                     const uint8x8_t G,
     28                                     const uint8x8_t B) {
     29   const uint16x8_t r = vmovl_u8(R);
     30   const uint16x8_t g = vmovl_u8(G);
     31   const uint16x8_t b = vmovl_u8(B);
     32   const uint16x4_t r_lo = vget_low_u16(r);
     33   const uint16x4_t r_hi = vget_high_u16(r);
     34   const uint16x4_t g_lo = vget_low_u16(g);
     35   const uint16x4_t g_hi = vget_high_u16(g);
     36   const uint16x4_t b_lo = vget_low_u16(b);
     37   const uint16x4_t b_hi = vget_high_u16(b);
     38   const uint32x4_t tmp0_lo = vmull_n_u16(         r_lo, 16839u);
     39   const uint32x4_t tmp0_hi = vmull_n_u16(         r_hi, 16839u);
     40   const uint32x4_t tmp1_lo = vmlal_n_u16(tmp0_lo, g_lo, 33059u);
     41   const uint32x4_t tmp1_hi = vmlal_n_u16(tmp0_hi, g_hi, 33059u);
     42   const uint32x4_t tmp2_lo = vmlal_n_u16(tmp1_lo, b_lo, 6420u);
     43   const uint32x4_t tmp2_hi = vmlal_n_u16(tmp1_hi, b_hi, 6420u);
     44   const uint16x8_t Y1 = vcombine_u16(vrshrn_n_u32(tmp2_lo, 16),
     45                                      vrshrn_n_u32(tmp2_hi, 16));
     46   const uint16x8_t Y2 = vaddq_u16(Y1, vdupq_n_u16(16));
     47   return vqmovn_u16(Y2);
     48 }
     49 
     50 static void ConvertRGB24ToY_NEON(const uint8_t* WEBP_RESTRICT rgb,
     51                                  uint8_t* WEBP_RESTRICT y, int width) {
     52   int i;
     53   for (i = 0; i + 8 <= width; i += 8, rgb += 3 * 8) {
     54     const uint8x8x3_t RGB = vld3_u8(rgb);
     55     const uint8x8_t Y = ConvertRGBToY_NEON(RGB.val[0], RGB.val[1], RGB.val[2]);
     56     vst1_u8(y + i, Y);
     57   }
     58   for (; i < width; ++i, rgb += 3) {   // left-over
     59     y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
     60   }
     61 }
     62 
     63 static void ConvertBGR24ToY_NEON(const uint8_t* WEBP_RESTRICT bgr,
     64                                  uint8_t* WEBP_RESTRICT y, int width) {
     65   int i;
     66   for (i = 0; i + 8 <= width; i += 8, bgr += 3 * 8) {
     67     const uint8x8x3_t BGR = vld3_u8(bgr);
     68     const uint8x8_t Y = ConvertRGBToY_NEON(BGR.val[2], BGR.val[1], BGR.val[0]);
     69     vst1_u8(y + i, Y);
     70   }
     71   for (; i < width; ++i, bgr += 3) {  // left-over
     72     y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
     73   }
     74 }
     75 
     76 static void ConvertARGBToY_NEON(const uint32_t* WEBP_RESTRICT argb,
     77                                 uint8_t* WEBP_RESTRICT y, int width) {
     78   int i;
     79   for (i = 0; i + 8 <= width; i += 8) {
     80     const uint8x8x4_t RGB = vld4_u8((const uint8_t*)&argb[i]);
     81     const uint8x8_t Y = ConvertRGBToY_NEON(RGB.val[2], RGB.val[1], RGB.val[0]);
     82     vst1_u8(y + i, Y);
     83   }
     84   for (; i < width; ++i) {   // left-over
     85     const uint32_t p = argb[i];
     86     y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >>  0) & 0xff,
     87                      YUV_HALF);
     88   }
     89 }
     90 
     91 //-----------------------------------------------------------------------------
     92 
     93 // computes: DST_s16 = [(C0 * r + C1 * g + C2 * b) >> 16] + CST
     94 #define MULTIPLY_16b_PREAMBLE(r, g, b)                           \
     95   const int16x4_t r_lo = vreinterpret_s16_u16(vget_low_u16(r));  \
     96   const int16x4_t r_hi = vreinterpret_s16_u16(vget_high_u16(r)); \
     97   const int16x4_t g_lo = vreinterpret_s16_u16(vget_low_u16(g));  \
     98   const int16x4_t g_hi = vreinterpret_s16_u16(vget_high_u16(g)); \
     99   const int16x4_t b_lo = vreinterpret_s16_u16(vget_low_u16(b));  \
    100   const int16x4_t b_hi = vreinterpret_s16_u16(vget_high_u16(b))
    101 
    102 #define MULTIPLY_16b(C0, C1, C2, CST, DST_s16) do {              \
    103   const int32x4_t tmp0_lo = vmull_n_s16(         r_lo, C0);      \
    104   const int32x4_t tmp0_hi = vmull_n_s16(         r_hi, C0);      \
    105   const int32x4_t tmp1_lo = vmlal_n_s16(tmp0_lo, g_lo, C1);      \
    106   const int32x4_t tmp1_hi = vmlal_n_s16(tmp0_hi, g_hi, C1);      \
    107   const int32x4_t tmp2_lo = vmlal_n_s16(tmp1_lo, b_lo, C2);      \
    108   const int32x4_t tmp2_hi = vmlal_n_s16(tmp1_hi, b_hi, C2);      \
    109   const int16x8_t tmp3 = vcombine_s16(vshrn_n_s32(tmp2_lo, 16),  \
    110                                       vshrn_n_s32(tmp2_hi, 16)); \
    111   DST_s16 = vaddq_s16(tmp3, vdupq_n_s16(CST));                   \
    112 } while (0)
    113 
    114 // This needs to be a macro, since (128 << SHIFT) needs to be an immediate.
    115 #define CONVERT_RGB_TO_UV(r, g, b, SHIFT, U_DST, V_DST) do {     \
    116   MULTIPLY_16b_PREAMBLE(r, g, b);                                \
    117   MULTIPLY_16b(-9719, -19081, 28800, 128 << SHIFT, U_DST);       \
    118   MULTIPLY_16b(28800, -24116, -4684, 128 << SHIFT, V_DST);       \
    119 } while (0)
    120 
    121 static void ConvertRGBA32ToUV_NEON(const uint16_t* WEBP_RESTRICT rgb,
    122                                    uint8_t* WEBP_RESTRICT u,
    123                                    uint8_t* WEBP_RESTRICT v, int width) {
    124   int i;
    125   for (i = 0; i + 8 <= width; i += 8, rgb += 4 * 8) {
    126     const uint16x8x4_t RGB = vld4q_u16((const uint16_t*)rgb);
    127     int16x8_t U, V;
    128     CONVERT_RGB_TO_UV(RGB.val[0], RGB.val[1], RGB.val[2], 2, U, V);
    129     vst1_u8(u + i, vqrshrun_n_s16(U, 2));
    130     vst1_u8(v + i, vqrshrun_n_s16(V, 2));
    131   }
    132   for (; i < width; i += 1, rgb += 4) {
    133     const int r = rgb[0], g = rgb[1], b = rgb[2];
    134     u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
    135     v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
    136   }
    137 }
    138 
    139 static void ConvertARGBToUV_NEON(const uint32_t* WEBP_RESTRICT argb,
    140                                  uint8_t* WEBP_RESTRICT u,
    141                                  uint8_t* WEBP_RESTRICT v,
    142                                  int src_width, int do_store) {
    143   int i;
    144   for (i = 0; i + 16 <= src_width; i += 16, u += 8, v += 8) {
    145     const uint8x16x4_t RGB = vld4q_u8((const uint8_t*)&argb[i]);
    146     const uint16x8_t R = vpaddlq_u8(RGB.val[2]);  // pair-wise adds
    147     const uint16x8_t G = vpaddlq_u8(RGB.val[1]);
    148     const uint16x8_t B = vpaddlq_u8(RGB.val[0]);
    149     int16x8_t U_tmp, V_tmp;
    150     CONVERT_RGB_TO_UV(R, G, B, 1, U_tmp, V_tmp);
    151     {
    152       const uint8x8_t U = vqrshrun_n_s16(U_tmp, 1);
    153       const uint8x8_t V = vqrshrun_n_s16(V_tmp, 1);
    154       if (do_store) {
    155         vst1_u8(u, U);
    156         vst1_u8(v, V);
    157       } else {
    158         const uint8x8_t prev_u = vld1_u8(u);
    159         const uint8x8_t prev_v = vld1_u8(v);
    160         vst1_u8(u, vrhadd_u8(U, prev_u));
    161         vst1_u8(v, vrhadd_u8(V, prev_v));
    162       }
    163     }
    164   }
    165   if (i < src_width) {  // left-over
    166     WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
    167   }
    168 }
    169 
    170 
    171 //------------------------------------------------------------------------------
    172 
    173 extern void WebPInitConvertARGBToYUVNEON(void);
    174 
    175 WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVNEON(void) {
    176   WebPConvertRGB24ToY = ConvertRGB24ToY_NEON;
    177   WebPConvertBGR24ToY = ConvertBGR24ToY_NEON;
    178   WebPConvertARGBToY = ConvertARGBToY_NEON;
    179   WebPConvertARGBToUV = ConvertARGBToUV_NEON;
    180   WebPConvertRGBA32ToUV = ConvertRGBA32ToUV_NEON;
    181 }
    182 
    183 #else  // !WEBP_USE_NEON
    184 
    185 WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVNEON)
    186 
    187 #endif  // WEBP_USE_NEON