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