• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 // NEON variant of methods for lossless decoder
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13 
14 #include "src/dsp/dsp.h"
15 
16 #if defined(WEBP_USE_NEON)
17 
18 #include <arm_neon.h>
19 
20 #include "src/dsp/lossless.h"
21 #include "src/dsp/neon.h"
22 
23 //------------------------------------------------------------------------------
24 // Colorspace conversion functions
25 
26 #if !defined(WORK_AROUND_GCC)
27 // gcc 4.6.0 had some trouble (NDK-r9) with this code. We only use it for
28 // gcc-4.8.x at least.
ConvertBGRAToRGBA_NEON(const uint32_t * src,int num_pixels,uint8_t * dst)29 static void ConvertBGRAToRGBA_NEON(const uint32_t* src,
30                                    int num_pixels, uint8_t* dst) {
31   const uint32_t* const end = src + (num_pixels & ~15);
32   for (; src < end; src += 16) {
33     uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);
34     // swap B and R. (VSWP d0,d2 has no intrinsics equivalent!)
35     const uint8x16_t tmp = pixel.val[0];
36     pixel.val[0] = pixel.val[2];
37     pixel.val[2] = tmp;
38     vst4q_u8(dst, pixel);
39     dst += 64;
40   }
41   VP8LConvertBGRAToRGBA_C(src, num_pixels & 15, dst);  // left-overs
42 }
43 
ConvertBGRAToBGR_NEON(const uint32_t * src,int num_pixels,uint8_t * dst)44 static void ConvertBGRAToBGR_NEON(const uint32_t* src,
45                                   int num_pixels, uint8_t* dst) {
46   const uint32_t* const end = src + (num_pixels & ~15);
47   for (; src < end; src += 16) {
48     const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);
49     const uint8x16x3_t tmp = { { pixel.val[0], pixel.val[1], pixel.val[2] } };
50     vst3q_u8(dst, tmp);
51     dst += 48;
52   }
53   VP8LConvertBGRAToBGR_C(src, num_pixels & 15, dst);  // left-overs
54 }
55 
ConvertBGRAToRGB_NEON(const uint32_t * src,int num_pixels,uint8_t * dst)56 static void ConvertBGRAToRGB_NEON(const uint32_t* src,
57                                   int num_pixels, uint8_t* dst) {
58   const uint32_t* const end = src + (num_pixels & ~15);
59   for (; src < end; src += 16) {
60     const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);
61     const uint8x16x3_t tmp = { { pixel.val[2], pixel.val[1], pixel.val[0] } };
62     vst3q_u8(dst, tmp);
63     dst += 48;
64   }
65   VP8LConvertBGRAToRGB_C(src, num_pixels & 15, dst);  // left-overs
66 }
67 
68 #else  // WORK_AROUND_GCC
69 
70 // gcc-4.6.0 fallback
71 
72 static const uint8_t kRGBAShuffle[8] = { 2, 1, 0, 3, 6, 5, 4, 7 };
73 
ConvertBGRAToRGBA_NEON(const uint32_t * src,int num_pixels,uint8_t * dst)74 static void ConvertBGRAToRGBA_NEON(const uint32_t* src,
75                                    int num_pixels, uint8_t* dst) {
76   const uint32_t* const end = src + (num_pixels & ~1);
77   const uint8x8_t shuffle = vld1_u8(kRGBAShuffle);
78   for (; src < end; src += 2) {
79     const uint8x8_t pixels = vld1_u8((uint8_t*)src);
80     vst1_u8(dst, vtbl1_u8(pixels, shuffle));
81     dst += 8;
82   }
83   VP8LConvertBGRAToRGBA_C(src, num_pixels & 1, dst);  // left-overs
84 }
85 
86 static const uint8_t kBGRShuffle[3][8] = {
87   {  0,  1,  2,  4,  5,  6,  8,  9 },
88   { 10, 12, 13, 14, 16, 17, 18, 20 },
89   { 21, 22, 24, 25, 26, 28, 29, 30 }
90 };
91 
ConvertBGRAToBGR_NEON(const uint32_t * src,int num_pixels,uint8_t * dst)92 static void ConvertBGRAToBGR_NEON(const uint32_t* src,
93                                   int num_pixels, uint8_t* dst) {
94   const uint32_t* const end = src + (num_pixels & ~7);
95   const uint8x8_t shuffle0 = vld1_u8(kBGRShuffle[0]);
96   const uint8x8_t shuffle1 = vld1_u8(kBGRShuffle[1]);
97   const uint8x8_t shuffle2 = vld1_u8(kBGRShuffle[2]);
98   for (; src < end; src += 8) {
99     uint8x8x4_t pixels;
100     INIT_VECTOR4(pixels,
101                  vld1_u8((const uint8_t*)(src + 0)),
102                  vld1_u8((const uint8_t*)(src + 2)),
103                  vld1_u8((const uint8_t*)(src + 4)),
104                  vld1_u8((const uint8_t*)(src + 6)));
105     vst1_u8(dst +  0, vtbl4_u8(pixels, shuffle0));
106     vst1_u8(dst +  8, vtbl4_u8(pixels, shuffle1));
107     vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));
108     dst += 8 * 3;
109   }
110   VP8LConvertBGRAToBGR_C(src, num_pixels & 7, dst);  // left-overs
111 }
112 
113 static const uint8_t kRGBShuffle[3][8] = {
114   {  2,  1,  0,  6,  5,  4, 10,  9 },
115   {  8, 14, 13, 12, 18, 17, 16, 22 },
116   { 21, 20, 26, 25, 24, 30, 29, 28 }
117 };
118 
ConvertBGRAToRGB_NEON(const uint32_t * src,int num_pixels,uint8_t * dst)119 static void ConvertBGRAToRGB_NEON(const uint32_t* src,
120                                   int num_pixels, uint8_t* dst) {
121   const uint32_t* const end = src + (num_pixels & ~7);
122   const uint8x8_t shuffle0 = vld1_u8(kRGBShuffle[0]);
123   const uint8x8_t shuffle1 = vld1_u8(kRGBShuffle[1]);
124   const uint8x8_t shuffle2 = vld1_u8(kRGBShuffle[2]);
125   for (; src < end; src += 8) {
126     uint8x8x4_t pixels;
127     INIT_VECTOR4(pixels,
128                  vld1_u8((const uint8_t*)(src + 0)),
129                  vld1_u8((const uint8_t*)(src + 2)),
130                  vld1_u8((const uint8_t*)(src + 4)),
131                  vld1_u8((const uint8_t*)(src + 6)));
132     vst1_u8(dst +  0, vtbl4_u8(pixels, shuffle0));
133     vst1_u8(dst +  8, vtbl4_u8(pixels, shuffle1));
134     vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));
135     dst += 8 * 3;
136   }
137   VP8LConvertBGRAToRGB_C(src, num_pixels & 7, dst);  // left-overs
138 }
139 
140 #endif   // !WORK_AROUND_GCC
141 
142 //------------------------------------------------------------------------------
143 // Predictor Transform
144 
145 #define LOAD_U32_AS_U8(IN) vreinterpret_u8_u32(vdup_n_u32((IN)))
146 #define LOAD_U32P_AS_U8(IN) vreinterpret_u8_u32(vld1_u32((IN)))
147 #define LOADQ_U32_AS_U8(IN) vreinterpretq_u8_u32(vdupq_n_u32((IN)))
148 #define LOADQ_U32P_AS_U8(IN) vreinterpretq_u8_u32(vld1q_u32((IN)))
149 #define GET_U8_AS_U32(IN) vget_lane_u32(vreinterpret_u32_u8((IN)), 0);
150 #define GETQ_U8_AS_U32(IN) vgetq_lane_u32(vreinterpretq_u32_u8((IN)), 0);
151 #define STOREQ_U8_AS_U32P(OUT, IN) vst1q_u32((OUT), vreinterpretq_u32_u8((IN)));
152 #define ROTATE32_LEFT(L) vextq_u8((L), (L), 12)    // D|C|B|A -> C|B|A|D
153 
Average2_u8_NEON(uint32_t a0,uint32_t a1)154 static WEBP_INLINE uint8x8_t Average2_u8_NEON(uint32_t a0, uint32_t a1) {
155   const uint8x8_t A0 = LOAD_U32_AS_U8(a0);
156   const uint8x8_t A1 = LOAD_U32_AS_U8(a1);
157   return vhadd_u8(A0, A1);
158 }
159 
ClampedAddSubtractHalf_NEON(uint32_t c0,uint32_t c1,uint32_t c2)160 static WEBP_INLINE uint32_t ClampedAddSubtractHalf_NEON(uint32_t c0,
161                                                         uint32_t c1,
162                                                         uint32_t c2) {
163   const uint8x8_t avg = Average2_u8_NEON(c0, c1);
164   // Remove one to c2 when bigger than avg.
165   const uint8x8_t C2 = LOAD_U32_AS_U8(c2);
166   const uint8x8_t cmp = vcgt_u8(C2, avg);
167   const uint8x8_t C2_1 = vadd_u8(C2, cmp);
168   // Compute half of the difference between avg and c2.
169   const int8x8_t diff_avg = vreinterpret_s8_u8(vhsub_u8(avg, C2_1));
170   // Compute the sum with avg and saturate.
171   const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(avg));
172   const uint8x8_t res = vqmovun_s16(vaddw_s8(avg_16, diff_avg));
173   const uint32_t output = GET_U8_AS_U32(res);
174   return output;
175 }
176 
Average2_NEON(uint32_t a0,uint32_t a1)177 static WEBP_INLINE uint32_t Average2_NEON(uint32_t a0, uint32_t a1) {
178   const uint8x8_t avg_u8x8 = Average2_u8_NEON(a0, a1);
179   const uint32_t avg = GET_U8_AS_U32(avg_u8x8);
180   return avg;
181 }
182 
Average3_NEON(uint32_t a0,uint32_t a1,uint32_t a2)183 static WEBP_INLINE uint32_t Average3_NEON(uint32_t a0, uint32_t a1,
184                                           uint32_t a2) {
185   const uint8x8_t avg0 = Average2_u8_NEON(a0, a2);
186   const uint8x8_t A1 = LOAD_U32_AS_U8(a1);
187   const uint32_t avg = GET_U8_AS_U32(vhadd_u8(avg0, A1));
188   return avg;
189 }
190 
Predictor5_NEON(const uint32_t * const left,const uint32_t * const top)191 static uint32_t Predictor5_NEON(const uint32_t* const left,
192                                 const uint32_t* const top) {
193   return Average3_NEON(*left, top[0], top[1]);
194 }
Predictor6_NEON(const uint32_t * const left,const uint32_t * const top)195 static uint32_t Predictor6_NEON(const uint32_t* const left,
196                                 const uint32_t* const top) {
197   return Average2_NEON(*left, top[-1]);
198 }
Predictor7_NEON(const uint32_t * const left,const uint32_t * const top)199 static uint32_t Predictor7_NEON(const uint32_t* const left,
200                                 const uint32_t* const top) {
201   return Average2_NEON(*left, top[0]);
202 }
Predictor13_NEON(const uint32_t * const left,const uint32_t * const top)203 static uint32_t Predictor13_NEON(const uint32_t* const left,
204                                  const uint32_t* const top) {
205   return ClampedAddSubtractHalf_NEON(*left, top[0], top[-1]);
206 }
207 
208 // Batch versions of those functions.
209 
210 // Predictor0: ARGB_BLACK.
PredictorAdd0_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)211 static void PredictorAdd0_NEON(const uint32_t* in, const uint32_t* upper,
212                                int num_pixels, uint32_t* out) {
213   int i;
214   const uint8x16_t black = vreinterpretq_u8_u32(vdupq_n_u32(ARGB_BLACK));
215   for (i = 0; i + 4 <= num_pixels; i += 4) {
216     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
217     const uint8x16_t res = vaddq_u8(src, black);
218     STOREQ_U8_AS_U32P(&out[i], res);
219   }
220   VP8LPredictorsAdd_C[0](in + i, upper + i, num_pixels - i, out + i);
221 }
222 
223 // Predictor1: left.
PredictorAdd1_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)224 static void PredictorAdd1_NEON(const uint32_t* in, const uint32_t* upper,
225                                int num_pixels, uint32_t* out) {
226   int i;
227   const uint8x16_t zero = LOADQ_U32_AS_U8(0);
228   for (i = 0; i + 4 <= num_pixels; i += 4) {
229     // a | b | c | d
230     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
231     // 0 | a | b | c
232     const uint8x16_t shift0 = vextq_u8(zero, src, 12);
233     // a | a + b | b + c | c + d
234     const uint8x16_t sum0 = vaddq_u8(src, shift0);
235     // 0 | 0 | a | a + b
236     const uint8x16_t shift1 = vextq_u8(zero, sum0, 8);
237     // a | a + b | a + b + c | a + b + c + d
238     const uint8x16_t sum1 = vaddq_u8(sum0, shift1);
239     const uint8x16_t prev = LOADQ_U32_AS_U8(out[i - 1]);
240     const uint8x16_t res = vaddq_u8(sum1, prev);
241     STOREQ_U8_AS_U32P(&out[i], res);
242   }
243   VP8LPredictorsAdd_C[1](in + i, upper + i, num_pixels - i, out + i);
244 }
245 
246 // Macro that adds 32-bit integers from IN using mod 256 arithmetic
247 // per 8 bit channel.
248 #define GENERATE_PREDICTOR_1(X, IN)                                       \
249 static void PredictorAdd##X##_NEON(const uint32_t* in,                    \
250                                    const uint32_t* upper, int num_pixels, \
251                                    uint32_t* out) {                       \
252   int i;                                                                  \
253   for (i = 0; i + 4 <= num_pixels; i += 4) {                              \
254     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);                      \
255     const uint8x16_t other = LOADQ_U32P_AS_U8(&(IN));                     \
256     const uint8x16_t res = vaddq_u8(src, other);                          \
257     STOREQ_U8_AS_U32P(&out[i], res);                                      \
258   }                                                                       \
259   VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i);   \
260 }
261 // Predictor2: Top.
262 GENERATE_PREDICTOR_1(2, upper[i])
263 // Predictor3: Top-right.
264 GENERATE_PREDICTOR_1(3, upper[i + 1])
265 // Predictor4: Top-left.
266 GENERATE_PREDICTOR_1(4, upper[i - 1])
267 #undef GENERATE_PREDICTOR_1
268 
269 // Predictor5: average(average(left, TR), T)
270 #define DO_PRED5(LANE) do {                                              \
271   const uint8x16_t avgLTR = vhaddq_u8(L, TR);                            \
272   const uint8x16_t avg = vhaddq_u8(avgLTR, T);                           \
273   const uint8x16_t res = vaddq_u8(avg, src);                             \
274   vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE));   \
275   L = ROTATE32_LEFT(res);                                                \
276 } while (0)
277 
PredictorAdd5_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)278 static void PredictorAdd5_NEON(const uint32_t* in, const uint32_t* upper,
279                                int num_pixels, uint32_t* out) {
280   int i;
281   uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
282   for (i = 0; i + 4 <= num_pixels; i += 4) {
283     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
284     const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i + 0]);
285     const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);
286     DO_PRED5(0);
287     DO_PRED5(1);
288     DO_PRED5(2);
289     DO_PRED5(3);
290   }
291   VP8LPredictorsAdd_C[5](in + i, upper + i, num_pixels - i, out + i);
292 }
293 #undef DO_PRED5
294 
295 #define DO_PRED67(LANE) do {                                             \
296   const uint8x16_t avg = vhaddq_u8(L, top);                              \
297   const uint8x16_t res = vaddq_u8(avg, src);                             \
298   vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE));   \
299   L = ROTATE32_LEFT(res);                                                \
300 } while (0)
301 
302 // Predictor6: average(left, TL)
PredictorAdd6_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)303 static void PredictorAdd6_NEON(const uint32_t* in, const uint32_t* upper,
304                                int num_pixels, uint32_t* out) {
305   int i;
306   uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
307   for (i = 0; i + 4 <= num_pixels; i += 4) {
308     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
309     const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i - 1]);
310     DO_PRED67(0);
311     DO_PRED67(1);
312     DO_PRED67(2);
313     DO_PRED67(3);
314   }
315   VP8LPredictorsAdd_C[6](in + i, upper + i, num_pixels - i, out + i);
316 }
317 
318 // Predictor7: average(left, T)
PredictorAdd7_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)319 static void PredictorAdd7_NEON(const uint32_t* in, const uint32_t* upper,
320                                int num_pixels, uint32_t* out) {
321   int i;
322   uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
323   for (i = 0; i + 4 <= num_pixels; i += 4) {
324     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
325     const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i]);
326     DO_PRED67(0);
327     DO_PRED67(1);
328     DO_PRED67(2);
329     DO_PRED67(3);
330   }
331   VP8LPredictorsAdd_C[7](in + i, upper + i, num_pixels - i, out + i);
332 }
333 #undef DO_PRED67
334 
335 #define GENERATE_PREDICTOR_2(X, IN)                                       \
336 static void PredictorAdd##X##_NEON(const uint32_t* in,                    \
337                                    const uint32_t* upper, int num_pixels, \
338                                    uint32_t* out) {                       \
339   int i;                                                                  \
340   for (i = 0; i + 4 <= num_pixels; i += 4) {                              \
341     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);                      \
342     const uint8x16_t Tother = LOADQ_U32P_AS_U8(&(IN));                    \
343     const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);                     \
344     const uint8x16_t avg = vhaddq_u8(T, Tother);                          \
345     const uint8x16_t res = vaddq_u8(avg, src);                            \
346     STOREQ_U8_AS_U32P(&out[i], res);                                      \
347   }                                                                       \
348   VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i);   \
349 }
350 // Predictor8: average TL T.
351 GENERATE_PREDICTOR_2(8, upper[i - 1])
352 // Predictor9: average T TR.
353 GENERATE_PREDICTOR_2(9, upper[i + 1])
354 #undef GENERATE_PREDICTOR_2
355 
356 // Predictor10: average of (average of (L,TL), average of (T, TR)).
357 #define DO_PRED10(LANE) do {                                             \
358   const uint8x16_t avgLTL = vhaddq_u8(L, TL);                            \
359   const uint8x16_t avg = vhaddq_u8(avgTTR, avgLTL);                      \
360   const uint8x16_t res = vaddq_u8(avg, src);                             \
361   vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE));   \
362   L = ROTATE32_LEFT(res);                                                \
363 } while (0)
364 
PredictorAdd10_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)365 static void PredictorAdd10_NEON(const uint32_t* in, const uint32_t* upper,
366                                 int num_pixels, uint32_t* out) {
367   int i;
368   uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
369   for (i = 0; i + 4 <= num_pixels; i += 4) {
370     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
371     const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
372     const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
373     const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);
374     const uint8x16_t avgTTR = vhaddq_u8(T, TR);
375     DO_PRED10(0);
376     DO_PRED10(1);
377     DO_PRED10(2);
378     DO_PRED10(3);
379   }
380   VP8LPredictorsAdd_C[10](in + i, upper + i, num_pixels - i, out + i);
381 }
382 #undef DO_PRED10
383 
384 // Predictor11: select.
385 #define DO_PRED11(LANE) do {                                                   \
386   const uint8x16_t sumLin = vaddq_u8(L, src);  /* in + L */                    \
387   const uint8x16_t pLTL = vabdq_u8(L, TL);  /* |L - TL| */                     \
388   const uint16x8_t sum_LTL = vpaddlq_u8(pLTL);                                 \
389   const uint32x4_t pa = vpaddlq_u16(sum_LTL);                                  \
390   const uint32x4_t mask = vcleq_u32(pa, pb);                                   \
391   const uint8x16_t res = vbslq_u8(vreinterpretq_u8_u32(mask), sumTin, sumLin); \
392   vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE));         \
393   L = ROTATE32_LEFT(res);                                                      \
394 } while (0)
395 
PredictorAdd11_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)396 static void PredictorAdd11_NEON(const uint32_t* in, const uint32_t* upper,
397                                 int num_pixels, uint32_t* out) {
398   int i;
399   uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
400   for (i = 0; i + 4 <= num_pixels; i += 4) {
401     const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
402     const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
403     const uint8x16_t pTTL = vabdq_u8(T, TL);   // |T - TL|
404     const uint16x8_t sum_TTL = vpaddlq_u8(pTTL);
405     const uint32x4_t pb = vpaddlq_u16(sum_TTL);
406     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
407     const uint8x16_t sumTin = vaddq_u8(T, src);   // in + T
408     DO_PRED11(0);
409     DO_PRED11(1);
410     DO_PRED11(2);
411     DO_PRED11(3);
412   }
413   VP8LPredictorsAdd_C[11](in + i, upper + i, num_pixels - i, out + i);
414 }
415 #undef DO_PRED11
416 
417 // Predictor12: ClampedAddSubtractFull.
418 #define DO_PRED12(DIFF, LANE) do {                                       \
419   const uint8x8_t pred =                                                 \
420       vqmovun_s16(vaddq_s16(vreinterpretq_s16_u16(L), (DIFF)));          \
421   const uint8x8_t res =                                                  \
422       vadd_u8(pred, (LANE <= 1) ? vget_low_u8(src) : vget_high_u8(src)); \
423   const uint16x8_t res16 = vmovl_u8(res);                                \
424   vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \
425   /* rotate in the left predictor for next iteration */                  \
426   L = vextq_u16(res16, res16, 4);                                        \
427 } while (0)
428 
PredictorAdd12_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)429 static void PredictorAdd12_NEON(const uint32_t* in, const uint32_t* upper,
430                                 int num_pixels, uint32_t* out) {
431   int i;
432   uint16x8_t L = vmovl_u8(LOAD_U32_AS_U8(out[-1]));
433   for (i = 0; i + 4 <= num_pixels; i += 4) {
434     // load four pixels of source
435     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
436     // precompute the difference T - TL once for all, stored as s16
437     const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
438     const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
439     const int16x8_t diff_lo =
440         vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(T), vget_low_u8(TL)));
441     const int16x8_t diff_hi =
442         vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(T), vget_high_u8(TL)));
443     // loop over the four reconstructed pixels
444     DO_PRED12(diff_lo, 0);
445     DO_PRED12(diff_lo, 1);
446     DO_PRED12(diff_hi, 2);
447     DO_PRED12(diff_hi, 3);
448   }
449   VP8LPredictorsAdd_C[12](in + i, upper + i, num_pixels - i, out + i);
450 }
451 #undef DO_PRED12
452 
453 // Predictor13: ClampedAddSubtractHalf
454 #define DO_PRED13(LANE, LOW_OR_HI) do {                                        \
455   const uint8x16_t avg = vhaddq_u8(L, T);                                      \
456   const uint8x16_t cmp = vcgtq_u8(TL, avg);                                    \
457   const uint8x16_t TL_1 = vaddq_u8(TL, cmp);                                   \
458   /* Compute half of the difference between avg and TL'. */                    \
459   const int8x8_t diff_avg =                                                    \
460       vreinterpret_s8_u8(LOW_OR_HI(vhsubq_u8(avg, TL_1)));                     \
461   /* Compute the sum with avg and saturate. */                                 \
462   const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(LOW_OR_HI(avg)));    \
463   const uint8x8_t delta = vqmovun_s16(vaddw_s8(avg_16, diff_avg));             \
464   const uint8x8_t res = vadd_u8(LOW_OR_HI(src), delta);                        \
465   const uint8x16_t res2 = vcombine_u8(res, res);                               \
466   vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1);       \
467   L = ROTATE32_LEFT(res2);                                                     \
468 } while (0)
469 
PredictorAdd13_NEON(const uint32_t * in,const uint32_t * upper,int num_pixels,uint32_t * out)470 static void PredictorAdd13_NEON(const uint32_t* in, const uint32_t* upper,
471                                 int num_pixels, uint32_t* out) {
472   int i;
473   uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);
474   for (i = 0; i + 4 <= num_pixels; i += 4) {
475     const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);
476     const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);
477     const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);
478     DO_PRED13(0, vget_low_u8);
479     DO_PRED13(1, vget_low_u8);
480     DO_PRED13(2, vget_high_u8);
481     DO_PRED13(3, vget_high_u8);
482   }
483   VP8LPredictorsAdd_C[13](in + i, upper + i, num_pixels - i, out + i);
484 }
485 #undef DO_PRED13
486 
487 #undef LOAD_U32_AS_U8
488 #undef LOAD_U32P_AS_U8
489 #undef LOADQ_U32_AS_U8
490 #undef LOADQ_U32P_AS_U8
491 #undef GET_U8_AS_U32
492 #undef GETQ_U8_AS_U32
493 #undef STOREQ_U8_AS_U32P
494 #undef ROTATE32_LEFT
495 
496 //------------------------------------------------------------------------------
497 // Subtract-Green Transform
498 
499 // vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use
500 // non-standard versions there.
501 #if defined(__APPLE__) && defined(__aarch64__) && \
502     defined(__apple_build_version__) && (__apple_build_version__< 6020037)
503 #define USE_VTBLQ
504 #endif
505 
506 #ifdef USE_VTBLQ
507 // 255 = byte will be zeroed
508 static const uint8_t kGreenShuffle[16] = {
509   1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255
510 };
511 
DoGreenShuffle_NEON(const uint8x16_t argb,const uint8x16_t shuffle)512 static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,
513                                                   const uint8x16_t shuffle) {
514   return vcombine_u8(vtbl1q_u8(argb, vget_low_u8(shuffle)),
515                      vtbl1q_u8(argb, vget_high_u8(shuffle)));
516 }
517 #else  // !USE_VTBLQ
518 // 255 = byte will be zeroed
519 static const uint8_t kGreenShuffle[8] = { 1, 255, 1, 255, 5, 255, 5, 255  };
520 
DoGreenShuffle_NEON(const uint8x16_t argb,const uint8x8_t shuffle)521 static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,
522                                                   const uint8x8_t shuffle) {
523   return vcombine_u8(vtbl1_u8(vget_low_u8(argb), shuffle),
524                      vtbl1_u8(vget_high_u8(argb), shuffle));
525 }
526 #endif  // USE_VTBLQ
527 
AddGreenToBlueAndRed_NEON(const uint32_t * src,int num_pixels,uint32_t * dst)528 static void AddGreenToBlueAndRed_NEON(const uint32_t* src, int num_pixels,
529                                       uint32_t* dst) {
530   const uint32_t* const end = src + (num_pixels & ~3);
531 #ifdef USE_VTBLQ
532   const uint8x16_t shuffle = vld1q_u8(kGreenShuffle);
533 #else
534   const uint8x8_t shuffle = vld1_u8(kGreenShuffle);
535 #endif
536   for (; src < end; src += 4, dst += 4) {
537     const uint8x16_t argb = vld1q_u8((const uint8_t*)src);
538     const uint8x16_t greens = DoGreenShuffle_NEON(argb, shuffle);
539     vst1q_u8((uint8_t*)dst, vaddq_u8(argb, greens));
540   }
541   // fallthrough and finish off with plain-C
542   VP8LAddGreenToBlueAndRed_C(src, num_pixels & 3, dst);
543 }
544 
545 //------------------------------------------------------------------------------
546 // Color Transform
547 
TransformColorInverse_NEON(const VP8LMultipliers * const m,const uint32_t * const src,int num_pixels,uint32_t * dst)548 static void TransformColorInverse_NEON(const VP8LMultipliers* const m,
549                                        const uint32_t* const src,
550                                        int num_pixels, uint32_t* dst) {
551 // sign-extended multiplying constants, pre-shifted by 6.
552 #define CST(X)  (((int16_t)(m->X << 8)) >> 6)
553   const int16_t rb[8] = {
554     CST(green_to_blue_), CST(green_to_red_),
555     CST(green_to_blue_), CST(green_to_red_),
556     CST(green_to_blue_), CST(green_to_red_),
557     CST(green_to_blue_), CST(green_to_red_)
558   };
559   const int16x8_t mults_rb = vld1q_s16(rb);
560   const int16_t b2[8] = {
561     0, CST(red_to_blue_), 0, CST(red_to_blue_),
562     0, CST(red_to_blue_), 0, CST(red_to_blue_),
563   };
564   const int16x8_t mults_b2 = vld1q_s16(b2);
565 #undef CST
566 #ifdef USE_VTBLQ
567   static const uint8_t kg0g0[16] = {
568     255, 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13
569   };
570   const uint8x16_t shuffle = vld1q_u8(kg0g0);
571 #else
572   static const uint8_t k0g0g[8] = { 255, 1, 255, 1, 255, 5, 255, 5 };
573   const uint8x8_t shuffle = vld1_u8(k0g0g);
574 #endif
575   const uint32x4_t mask_ag = vdupq_n_u32(0xff00ff00u);
576   int i;
577   for (i = 0; i + 4 <= num_pixels; i += 4) {
578     const uint8x16_t in = vld1q_u8((const uint8_t*)(src + i));
579     const uint32x4_t a0g0 = vandq_u32(vreinterpretq_u32_u8(in), mask_ag);
580     // 0 g 0 g
581     const uint8x16_t greens = DoGreenShuffle_NEON(in, shuffle);
582     // x dr  x db1
583     const int16x8_t A = vqdmulhq_s16(vreinterpretq_s16_u8(greens), mults_rb);
584     // x r'  x   b'
585     const int8x16_t B = vaddq_s8(vreinterpretq_s8_u8(in),
586                                  vreinterpretq_s8_s16(A));
587     // r' 0   b' 0
588     const int16x8_t C = vshlq_n_s16(vreinterpretq_s16_s8(B), 8);
589     // x db2  0  0
590     const int16x8_t D = vqdmulhq_s16(C, mults_b2);
591     // 0  x db2  0
592     const uint32x4_t E = vshrq_n_u32(vreinterpretq_u32_s16(D), 8);
593     // r' x  b'' 0
594     const int8x16_t F = vaddq_s8(vreinterpretq_s8_u32(E),
595                                  vreinterpretq_s8_s16(C));
596     // 0  r'  0  b''
597     const uint16x8_t G = vshrq_n_u16(vreinterpretq_u16_s8(F), 8);
598     const uint32x4_t out = vorrq_u32(vreinterpretq_u32_u16(G), a0g0);
599     vst1q_u32(dst + i, out);
600   }
601   // Fall-back to C-version for left-overs.
602   VP8LTransformColorInverse_C(m, src + i, num_pixels - i, dst + i);
603 }
604 
605 #undef USE_VTBLQ
606 
607 //------------------------------------------------------------------------------
608 // Entry point
609 
610 extern void VP8LDspInitNEON(void);
611 
VP8LDspInitNEON(void)612 WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitNEON(void) {
613   VP8LPredictors[5] = Predictor5_NEON;
614   VP8LPredictors[6] = Predictor6_NEON;
615   VP8LPredictors[7] = Predictor7_NEON;
616   VP8LPredictors[13] = Predictor13_NEON;
617 
618   VP8LPredictorsAdd[0] = PredictorAdd0_NEON;
619   VP8LPredictorsAdd[1] = PredictorAdd1_NEON;
620   VP8LPredictorsAdd[2] = PredictorAdd2_NEON;
621   VP8LPredictorsAdd[3] = PredictorAdd3_NEON;
622   VP8LPredictorsAdd[4] = PredictorAdd4_NEON;
623   VP8LPredictorsAdd[5] = PredictorAdd5_NEON;
624   VP8LPredictorsAdd[6] = PredictorAdd6_NEON;
625   VP8LPredictorsAdd[7] = PredictorAdd7_NEON;
626   VP8LPredictorsAdd[8] = PredictorAdd8_NEON;
627   VP8LPredictorsAdd[9] = PredictorAdd9_NEON;
628   VP8LPredictorsAdd[10] = PredictorAdd10_NEON;
629   VP8LPredictorsAdd[11] = PredictorAdd11_NEON;
630   VP8LPredictorsAdd[12] = PredictorAdd12_NEON;
631   VP8LPredictorsAdd[13] = PredictorAdd13_NEON;
632 
633   VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA_NEON;
634   VP8LConvertBGRAToBGR = ConvertBGRAToBGR_NEON;
635   VP8LConvertBGRAToRGB = ConvertBGRAToRGB_NEON;
636 
637   VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed_NEON;
638   VP8LTransformColorInverse = TransformColorInverse_NEON;
639 }
640 
641 #else  // !WEBP_USE_NEON
642 
643 WEBP_DSP_INIT_STUB(VP8LDspInitNEON)
644 
645 #endif  // WEBP_USE_NEON
646