Path: blob/master/thirdparty/libwebp/src/dsp/lossless_neon.c
9913 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// NEON variant of methods for lossless decoder10//11// Author: Skal ([email protected])1213#include "src/dsp/dsp.h"1415#if defined(WEBP_USE_NEON)1617#include <arm_neon.h>1819#include "src/dsp/lossless.h"20#include "src/dsp/neon.h"2122//------------------------------------------------------------------------------23// Colorspace conversion functions2425#if !defined(WORK_AROUND_GCC)26// gcc 4.6.0 had some trouble (NDK-r9) with this code. We only use it for27// gcc-4.8.x at least.28static void ConvertBGRAToRGBA_NEON(const uint32_t* WEBP_RESTRICT src,29int num_pixels, uint8_t* WEBP_RESTRICT dst) {30const uint32_t* const end = src + (num_pixels & ~15);31for (; src < end; src += 16) {32uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);33// swap B and R. (VSWP d0,d2 has no intrinsics equivalent!)34const uint8x16_t tmp = pixel.val[0];35pixel.val[0] = pixel.val[2];36pixel.val[2] = tmp;37vst4q_u8(dst, pixel);38dst += 64;39}40VP8LConvertBGRAToRGBA_C(src, num_pixels & 15, dst); // left-overs41}4243static void ConvertBGRAToBGR_NEON(const uint32_t* WEBP_RESTRICT src,44int num_pixels, uint8_t* WEBP_RESTRICT dst) {45const uint32_t* const end = src + (num_pixels & ~15);46for (; src < end; src += 16) {47const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);48const uint8x16x3_t tmp = { { pixel.val[0], pixel.val[1], pixel.val[2] } };49vst3q_u8(dst, tmp);50dst += 48;51}52VP8LConvertBGRAToBGR_C(src, num_pixels & 15, dst); // left-overs53}5455static void ConvertBGRAToRGB_NEON(const uint32_t* WEBP_RESTRICT src,56int num_pixels, uint8_t* WEBP_RESTRICT dst) {57const uint32_t* const end = src + (num_pixels & ~15);58for (; src < end; src += 16) {59const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);60const uint8x16x3_t tmp = { { pixel.val[2], pixel.val[1], pixel.val[0] } };61vst3q_u8(dst, tmp);62dst += 48;63}64VP8LConvertBGRAToRGB_C(src, num_pixels & 15, dst); // left-overs65}6667#else // WORK_AROUND_GCC6869// gcc-4.6.0 fallback7071static const uint8_t kRGBAShuffle[8] = { 2, 1, 0, 3, 6, 5, 4, 7 };7273static void ConvertBGRAToRGBA_NEON(const uint32_t* WEBP_RESTRICT src,74int num_pixels, uint8_t* WEBP_RESTRICT dst) {75const uint32_t* const end = src + (num_pixels & ~1);76const uint8x8_t shuffle = vld1_u8(kRGBAShuffle);77for (; src < end; src += 2) {78const uint8x8_t pixels = vld1_u8((uint8_t*)src);79vst1_u8(dst, vtbl1_u8(pixels, shuffle));80dst += 8;81}82VP8LConvertBGRAToRGBA_C(src, num_pixels & 1, dst); // left-overs83}8485static const uint8_t kBGRShuffle[3][8] = {86{ 0, 1, 2, 4, 5, 6, 8, 9 },87{ 10, 12, 13, 14, 16, 17, 18, 20 },88{ 21, 22, 24, 25, 26, 28, 29, 30 }89};9091static void ConvertBGRAToBGR_NEON(const uint32_t* WEBP_RESTRICT src,92int num_pixels, uint8_t* WEBP_RESTRICT dst) {93const uint32_t* const end = src + (num_pixels & ~7);94const uint8x8_t shuffle0 = vld1_u8(kBGRShuffle[0]);95const uint8x8_t shuffle1 = vld1_u8(kBGRShuffle[1]);96const uint8x8_t shuffle2 = vld1_u8(kBGRShuffle[2]);97for (; src < end; src += 8) {98uint8x8x4_t pixels;99INIT_VECTOR4(pixels,100vld1_u8((const uint8_t*)(src + 0)),101vld1_u8((const uint8_t*)(src + 2)),102vld1_u8((const uint8_t*)(src + 4)),103vld1_u8((const uint8_t*)(src + 6)));104vst1_u8(dst + 0, vtbl4_u8(pixels, shuffle0));105vst1_u8(dst + 8, vtbl4_u8(pixels, shuffle1));106vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));107dst += 8 * 3;108}109VP8LConvertBGRAToBGR_C(src, num_pixels & 7, dst); // left-overs110}111112static const uint8_t kRGBShuffle[3][8] = {113{ 2, 1, 0, 6, 5, 4, 10, 9 },114{ 8, 14, 13, 12, 18, 17, 16, 22 },115{ 21, 20, 26, 25, 24, 30, 29, 28 }116};117118static void ConvertBGRAToRGB_NEON(const uint32_t* WEBP_RESTRICT src,119int num_pixels, uint8_t* WEBP_RESTRICT dst) {120const uint32_t* const end = src + (num_pixels & ~7);121const uint8x8_t shuffle0 = vld1_u8(kRGBShuffle[0]);122const uint8x8_t shuffle1 = vld1_u8(kRGBShuffle[1]);123const uint8x8_t shuffle2 = vld1_u8(kRGBShuffle[2]);124for (; src < end; src += 8) {125uint8x8x4_t pixels;126INIT_VECTOR4(pixels,127vld1_u8((const uint8_t*)(src + 0)),128vld1_u8((const uint8_t*)(src + 2)),129vld1_u8((const uint8_t*)(src + 4)),130vld1_u8((const uint8_t*)(src + 6)));131vst1_u8(dst + 0, vtbl4_u8(pixels, shuffle0));132vst1_u8(dst + 8, vtbl4_u8(pixels, shuffle1));133vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));134dst += 8 * 3;135}136VP8LConvertBGRAToRGB_C(src, num_pixels & 7, dst); // left-overs137}138139#endif // !WORK_AROUND_GCC140141//------------------------------------------------------------------------------142// Predictor Transform143144#define LOAD_U32_AS_U8(IN) vreinterpret_u8_u32(vdup_n_u32((IN)))145#define LOAD_U32P_AS_U8(IN) vreinterpret_u8_u32(vld1_u32((IN)))146#define LOADQ_U32_AS_U8(IN) vreinterpretq_u8_u32(vdupq_n_u32((IN)))147#define LOADQ_U32P_AS_U8(IN) vreinterpretq_u8_u32(vld1q_u32((IN)))148#define GET_U8_AS_U32(IN) vget_lane_u32(vreinterpret_u32_u8((IN)), 0)149#define GETQ_U8_AS_U32(IN) vgetq_lane_u32(vreinterpretq_u32_u8((IN)), 0)150#define STOREQ_U8_AS_U32P(OUT, IN) vst1q_u32((OUT), vreinterpretq_u32_u8((IN)))151#define ROTATE32_LEFT(L) vextq_u8((L), (L), 12) // D|C|B|A -> C|B|A|D152153static WEBP_INLINE uint8x8_t Average2_u8_NEON(uint32_t a0, uint32_t a1) {154const uint8x8_t A0 = LOAD_U32_AS_U8(a0);155const uint8x8_t A1 = LOAD_U32_AS_U8(a1);156return vhadd_u8(A0, A1);157}158159static WEBP_INLINE uint32_t ClampedAddSubtractHalf_NEON(uint32_t c0,160uint32_t c1,161uint32_t c2) {162const uint8x8_t avg = Average2_u8_NEON(c0, c1);163// Remove one to c2 when bigger than avg.164const uint8x8_t C2 = LOAD_U32_AS_U8(c2);165const uint8x8_t cmp = vcgt_u8(C2, avg);166const uint8x8_t C2_1 = vadd_u8(C2, cmp);167// Compute half of the difference between avg and c2.168const int8x8_t diff_avg = vreinterpret_s8_u8(vhsub_u8(avg, C2_1));169// Compute the sum with avg and saturate.170const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(avg));171const uint8x8_t res = vqmovun_s16(vaddw_s8(avg_16, diff_avg));172const uint32_t output = GET_U8_AS_U32(res);173return output;174}175176static WEBP_INLINE uint32_t Average2_NEON(uint32_t a0, uint32_t a1) {177const uint8x8_t avg_u8x8 = Average2_u8_NEON(a0, a1);178const uint32_t avg = GET_U8_AS_U32(avg_u8x8);179return avg;180}181182static WEBP_INLINE uint32_t Average3_NEON(uint32_t a0, uint32_t a1,183uint32_t a2) {184const uint8x8_t avg0 = Average2_u8_NEON(a0, a2);185const uint8x8_t A1 = LOAD_U32_AS_U8(a1);186const uint32_t avg = GET_U8_AS_U32(vhadd_u8(avg0, A1));187return avg;188}189190static uint32_t Predictor5_NEON(const uint32_t* const left,191const uint32_t* const top) {192return Average3_NEON(*left, top[0], top[1]);193}194static uint32_t Predictor6_NEON(const uint32_t* const left,195const uint32_t* const top) {196return Average2_NEON(*left, top[-1]);197}198static uint32_t Predictor7_NEON(const uint32_t* const left,199const uint32_t* const top) {200return Average2_NEON(*left, top[0]);201}202static uint32_t Predictor13_NEON(const uint32_t* const left,203const uint32_t* const top) {204return ClampedAddSubtractHalf_NEON(*left, top[0], top[-1]);205}206207// Batch versions of those functions.208209// Predictor0: ARGB_BLACK.210static void PredictorAdd0_NEON(const uint32_t* in, const uint32_t* upper,211int num_pixels, uint32_t* WEBP_RESTRICT out) {212int i;213const uint8x16_t black = vreinterpretq_u8_u32(vdupq_n_u32(ARGB_BLACK));214for (i = 0; i + 4 <= num_pixels; i += 4) {215const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);216const uint8x16_t res = vaddq_u8(src, black);217STOREQ_U8_AS_U32P(&out[i], res);218}219VP8LPredictorsAdd_C[0](in + i, upper + i, num_pixels - i, out + i);220}221222// Predictor1: left.223static void PredictorAdd1_NEON(const uint32_t* in, const uint32_t* upper,224int num_pixels, uint32_t* WEBP_RESTRICT out) {225int i;226const uint8x16_t zero = LOADQ_U32_AS_U8(0);227for (i = 0; i + 4 <= num_pixels; i += 4) {228// a | b | c | d229const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);230// 0 | a | b | c231const uint8x16_t shift0 = vextq_u8(zero, src, 12);232// a | a + b | b + c | c + d233const uint8x16_t sum0 = vaddq_u8(src, shift0);234// 0 | 0 | a | a + b235const uint8x16_t shift1 = vextq_u8(zero, sum0, 8);236// a | a + b | a + b + c | a + b + c + d237const uint8x16_t sum1 = vaddq_u8(sum0, shift1);238const uint8x16_t prev = LOADQ_U32_AS_U8(out[i - 1]);239const uint8x16_t res = vaddq_u8(sum1, prev);240STOREQ_U8_AS_U32P(&out[i], res);241}242VP8LPredictorsAdd_C[1](in + i, upper + i, num_pixels - i, out + i);243}244245// Macro that adds 32-bit integers from IN using mod 256 arithmetic246// per 8 bit channel.247#define GENERATE_PREDICTOR_1(X, IN) \248static void PredictorAdd##X##_NEON(const uint32_t* in, \249const uint32_t* upper, int num_pixels, \250uint32_t* WEBP_RESTRICT out) { \251int i; \252for (i = 0; i + 4 <= num_pixels; i += 4) { \253const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \254const uint8x16_t other = LOADQ_U32P_AS_U8(&(IN)); \255const uint8x16_t res = vaddq_u8(src, other); \256STOREQ_U8_AS_U32P(&out[i], res); \257} \258VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \259}260// Predictor2: Top.261GENERATE_PREDICTOR_1(2, upper[i])262// Predictor3: Top-right.263GENERATE_PREDICTOR_1(3, upper[i + 1])264// Predictor4: Top-left.265GENERATE_PREDICTOR_1(4, upper[i - 1])266#undef GENERATE_PREDICTOR_1267268// Predictor5: average(average(left, TR), T)269#define DO_PRED5(LANE) do { \270const uint8x16_t avgLTR = vhaddq_u8(L, TR); \271const uint8x16_t avg = vhaddq_u8(avgLTR, T); \272const uint8x16_t res = vaddq_u8(avg, src); \273vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \274L = ROTATE32_LEFT(res); \275} while (0)276277static void PredictorAdd5_NEON(const uint32_t* in, const uint32_t* upper,278int num_pixels, uint32_t* WEBP_RESTRICT out) {279int i;280uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);281for (i = 0; i + 4 <= num_pixels; i += 4) {282const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);283const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i + 0]);284const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);285DO_PRED5(0);286DO_PRED5(1);287DO_PRED5(2);288DO_PRED5(3);289}290VP8LPredictorsAdd_C[5](in + i, upper + i, num_pixels - i, out + i);291}292#undef DO_PRED5293294#define DO_PRED67(LANE) do { \295const uint8x16_t avg = vhaddq_u8(L, top); \296const uint8x16_t res = vaddq_u8(avg, src); \297vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \298L = ROTATE32_LEFT(res); \299} while (0)300301// Predictor6: average(left, TL)302static void PredictorAdd6_NEON(const uint32_t* in, const uint32_t* upper,303int num_pixels, uint32_t* WEBP_RESTRICT out) {304int i;305uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);306for (i = 0; i + 4 <= num_pixels; i += 4) {307const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);308const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i - 1]);309DO_PRED67(0);310DO_PRED67(1);311DO_PRED67(2);312DO_PRED67(3);313}314VP8LPredictorsAdd_C[6](in + i, upper + i, num_pixels - i, out + i);315}316317// Predictor7: average(left, T)318static void PredictorAdd7_NEON(const uint32_t* in, const uint32_t* upper,319int num_pixels, uint32_t* WEBP_RESTRICT out) {320int i;321uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);322for (i = 0; i + 4 <= num_pixels; i += 4) {323const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);324const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i]);325DO_PRED67(0);326DO_PRED67(1);327DO_PRED67(2);328DO_PRED67(3);329}330VP8LPredictorsAdd_C[7](in + i, upper + i, num_pixels - i, out + i);331}332#undef DO_PRED67333334#define GENERATE_PREDICTOR_2(X, IN) \335static void PredictorAdd##X##_NEON(const uint32_t* in, \336const uint32_t* upper, int num_pixels, \337uint32_t* WEBP_RESTRICT out) { \338int i; \339for (i = 0; i + 4 <= num_pixels; i += 4) { \340const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \341const uint8x16_t Tother = LOADQ_U32P_AS_U8(&(IN)); \342const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]); \343const uint8x16_t avg = vhaddq_u8(T, Tother); \344const uint8x16_t res = vaddq_u8(avg, src); \345STOREQ_U8_AS_U32P(&out[i], res); \346} \347VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \348}349// Predictor8: average TL T.350GENERATE_PREDICTOR_2(8, upper[i - 1])351// Predictor9: average T TR.352GENERATE_PREDICTOR_2(9, upper[i + 1])353#undef GENERATE_PREDICTOR_2354355// Predictor10: average of (average of (L,TL), average of (T, TR)).356#define DO_PRED10(LANE) do { \357const uint8x16_t avgLTL = vhaddq_u8(L, TL); \358const uint8x16_t avg = vhaddq_u8(avgTTR, avgLTL); \359const uint8x16_t res = vaddq_u8(avg, src); \360vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \361L = ROTATE32_LEFT(res); \362} while (0)363364static void PredictorAdd10_NEON(const uint32_t* in, const uint32_t* upper,365int num_pixels, uint32_t* WEBP_RESTRICT out) {366int i;367uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);368for (i = 0; i + 4 <= num_pixels; i += 4) {369const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);370const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);371const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);372const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);373const uint8x16_t avgTTR = vhaddq_u8(T, TR);374DO_PRED10(0);375DO_PRED10(1);376DO_PRED10(2);377DO_PRED10(3);378}379VP8LPredictorsAdd_C[10](in + i, upper + i, num_pixels - i, out + i);380}381#undef DO_PRED10382383// Predictor11: select.384#define DO_PRED11(LANE) do { \385const uint8x16_t sumLin = vaddq_u8(L, src); /* in + L */ \386const uint8x16_t pLTL = vabdq_u8(L, TL); /* |L - TL| */ \387const uint16x8_t sum_LTL = vpaddlq_u8(pLTL); \388const uint32x4_t pa = vpaddlq_u16(sum_LTL); \389const uint32x4_t mask = vcleq_u32(pa, pb); \390const uint8x16_t res = vbslq_u8(vreinterpretq_u8_u32(mask), sumTin, sumLin); \391vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \392L = ROTATE32_LEFT(res); \393} while (0)394395static void PredictorAdd11_NEON(const uint32_t* in, const uint32_t* upper,396int num_pixels, uint32_t* WEBP_RESTRICT out) {397int i;398uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);399for (i = 0; i + 4 <= num_pixels; i += 4) {400const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);401const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);402const uint8x16_t pTTL = vabdq_u8(T, TL); // |T - TL|403const uint16x8_t sum_TTL = vpaddlq_u8(pTTL);404const uint32x4_t pb = vpaddlq_u16(sum_TTL);405const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);406const uint8x16_t sumTin = vaddq_u8(T, src); // in + T407DO_PRED11(0);408DO_PRED11(1);409DO_PRED11(2);410DO_PRED11(3);411}412VP8LPredictorsAdd_C[11](in + i, upper + i, num_pixels - i, out + i);413}414#undef DO_PRED11415416// Predictor12: ClampedAddSubtractFull.417#define DO_PRED12(DIFF, LANE) do { \418const uint8x8_t pred = \419vqmovun_s16(vaddq_s16(vreinterpretq_s16_u16(L), (DIFF))); \420const uint8x8_t res = \421vadd_u8(pred, (LANE <= 1) ? vget_low_u8(src) : vget_high_u8(src)); \422const uint16x8_t res16 = vmovl_u8(res); \423vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \424/* rotate in the left predictor for next iteration */ \425L = vextq_u16(res16, res16, 4); \426} while (0)427428static void PredictorAdd12_NEON(const uint32_t* in, const uint32_t* upper,429int num_pixels, uint32_t* WEBP_RESTRICT out) {430int i;431uint16x8_t L = vmovl_u8(LOAD_U32_AS_U8(out[-1]));432for (i = 0; i + 4 <= num_pixels; i += 4) {433// load four pixels of source434const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);435// precompute the difference T - TL once for all, stored as s16436const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);437const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);438const int16x8_t diff_lo =439vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(T), vget_low_u8(TL)));440const int16x8_t diff_hi =441vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(T), vget_high_u8(TL)));442// loop over the four reconstructed pixels443DO_PRED12(diff_lo, 0);444DO_PRED12(diff_lo, 1);445DO_PRED12(diff_hi, 2);446DO_PRED12(diff_hi, 3);447}448VP8LPredictorsAdd_C[12](in + i, upper + i, num_pixels - i, out + i);449}450#undef DO_PRED12451452// Predictor13: ClampedAddSubtractHalf453#define DO_PRED13(LANE, LOW_OR_HI) do { \454const uint8x16_t avg = vhaddq_u8(L, T); \455const uint8x16_t cmp = vcgtq_u8(TL, avg); \456const uint8x16_t TL_1 = vaddq_u8(TL, cmp); \457/* Compute half of the difference between avg and TL'. */ \458const int8x8_t diff_avg = \459vreinterpret_s8_u8(LOW_OR_HI(vhsubq_u8(avg, TL_1))); \460/* Compute the sum with avg and saturate. */ \461const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(LOW_OR_HI(avg))); \462const uint8x8_t delta = vqmovun_s16(vaddw_s8(avg_16, diff_avg)); \463const uint8x8_t res = vadd_u8(LOW_OR_HI(src), delta); \464const uint8x16_t res2 = vcombine_u8(res, res); \465vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \466L = ROTATE32_LEFT(res2); \467} while (0)468469static void PredictorAdd13_NEON(const uint32_t* in, const uint32_t* upper,470int num_pixels, uint32_t* WEBP_RESTRICT out) {471int i;472uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);473for (i = 0; i + 4 <= num_pixels; i += 4) {474const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);475const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);476const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);477DO_PRED13(0, vget_low_u8);478DO_PRED13(1, vget_low_u8);479DO_PRED13(2, vget_high_u8);480DO_PRED13(3, vget_high_u8);481}482VP8LPredictorsAdd_C[13](in + i, upper + i, num_pixels - i, out + i);483}484#undef DO_PRED13485486#undef LOAD_U32_AS_U8487#undef LOAD_U32P_AS_U8488#undef LOADQ_U32_AS_U8489#undef LOADQ_U32P_AS_U8490#undef GET_U8_AS_U32491#undef GETQ_U8_AS_U32492#undef STOREQ_U8_AS_U32P493#undef ROTATE32_LEFT494495//------------------------------------------------------------------------------496// Subtract-Green Transform497498// vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use499// non-standard versions there.500#if defined(__APPLE__) && WEBP_AARCH64 && \501defined(__apple_build_version__) && (__apple_build_version__< 6020037)502#define USE_VTBLQ503#endif504505#ifdef USE_VTBLQ506// 255 = byte will be zeroed507static const uint8_t kGreenShuffle[16] = {5081, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255509};510511static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,512const uint8x16_t shuffle) {513return vcombine_u8(vtbl1q_u8(argb, vget_low_u8(shuffle)),514vtbl1q_u8(argb, vget_high_u8(shuffle)));515}516#else // !USE_VTBLQ517// 255 = byte will be zeroed518static const uint8_t kGreenShuffle[8] = { 1, 255, 1, 255, 5, 255, 5, 255 };519520static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,521const uint8x8_t shuffle) {522return vcombine_u8(vtbl1_u8(vget_low_u8(argb), shuffle),523vtbl1_u8(vget_high_u8(argb), shuffle));524}525#endif // USE_VTBLQ526527static void AddGreenToBlueAndRed_NEON(const uint32_t* src, int num_pixels,528uint32_t* dst) {529const uint32_t* const end = src + (num_pixels & ~3);530#ifdef USE_VTBLQ531const uint8x16_t shuffle = vld1q_u8(kGreenShuffle);532#else533const uint8x8_t shuffle = vld1_u8(kGreenShuffle);534#endif535for (; src < end; src += 4, dst += 4) {536const uint8x16_t argb = vld1q_u8((const uint8_t*)src);537const uint8x16_t greens = DoGreenShuffle_NEON(argb, shuffle);538vst1q_u8((uint8_t*)dst, vaddq_u8(argb, greens));539}540// fallthrough and finish off with plain-C541VP8LAddGreenToBlueAndRed_C(src, num_pixels & 3, dst);542}543544//------------------------------------------------------------------------------545// Color Transform546547static void TransformColorInverse_NEON(const VP8LMultipliers* const m,548const uint32_t* const src,549int num_pixels, uint32_t* dst) {550// sign-extended multiplying constants, pre-shifted by 6.551#define CST(X) (((int16_t)(m->X << 8)) >> 6)552const int16_t rb[8] = {553CST(green_to_blue_), CST(green_to_red_),554CST(green_to_blue_), CST(green_to_red_),555CST(green_to_blue_), CST(green_to_red_),556CST(green_to_blue_), CST(green_to_red_)557};558const int16x8_t mults_rb = vld1q_s16(rb);559const int16_t b2[8] = {5600, CST(red_to_blue_), 0, CST(red_to_blue_),5610, CST(red_to_blue_), 0, CST(red_to_blue_),562};563const int16x8_t mults_b2 = vld1q_s16(b2);564#undef CST565#ifdef USE_VTBLQ566static const uint8_t kg0g0[16] = {567255, 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13568};569const uint8x16_t shuffle = vld1q_u8(kg0g0);570#else571static const uint8_t k0g0g[8] = { 255, 1, 255, 1, 255, 5, 255, 5 };572const uint8x8_t shuffle = vld1_u8(k0g0g);573#endif574const uint32x4_t mask_ag = vdupq_n_u32(0xff00ff00u);575int i;576for (i = 0; i + 4 <= num_pixels; i += 4) {577const uint8x16_t in = vld1q_u8((const uint8_t*)(src + i));578const uint32x4_t a0g0 = vandq_u32(vreinterpretq_u32_u8(in), mask_ag);579// 0 g 0 g580const uint8x16_t greens = DoGreenShuffle_NEON(in, shuffle);581// x dr x db1582const int16x8_t A = vqdmulhq_s16(vreinterpretq_s16_u8(greens), mults_rb);583// x r' x b'584const int8x16_t B = vaddq_s8(vreinterpretq_s8_u8(in),585vreinterpretq_s8_s16(A));586// r' 0 b' 0587const int16x8_t C = vshlq_n_s16(vreinterpretq_s16_s8(B), 8);588// x db2 0 0589const int16x8_t D = vqdmulhq_s16(C, mults_b2);590// 0 x db2 0591const uint32x4_t E = vshrq_n_u32(vreinterpretq_u32_s16(D), 8);592// r' x b'' 0593const int8x16_t F = vaddq_s8(vreinterpretq_s8_u32(E),594vreinterpretq_s8_s16(C));595// 0 r' 0 b''596const uint16x8_t G = vshrq_n_u16(vreinterpretq_u16_s8(F), 8);597const uint32x4_t out = vorrq_u32(vreinterpretq_u32_u16(G), a0g0);598vst1q_u32(dst + i, out);599}600// Fall-back to C-version for left-overs.601VP8LTransformColorInverse_C(m, src + i, num_pixels - i, dst + i);602}603604#undef USE_VTBLQ605606//------------------------------------------------------------------------------607// Entry point608609extern void VP8LDspInitNEON(void);610611WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitNEON(void) {612VP8LPredictors[5] = Predictor5_NEON;613VP8LPredictors[6] = Predictor6_NEON;614VP8LPredictors[7] = Predictor7_NEON;615VP8LPredictors[13] = Predictor13_NEON;616617VP8LPredictorsAdd[0] = PredictorAdd0_NEON;618VP8LPredictorsAdd[1] = PredictorAdd1_NEON;619VP8LPredictorsAdd[2] = PredictorAdd2_NEON;620VP8LPredictorsAdd[3] = PredictorAdd3_NEON;621VP8LPredictorsAdd[4] = PredictorAdd4_NEON;622VP8LPredictorsAdd[5] = PredictorAdd5_NEON;623VP8LPredictorsAdd[6] = PredictorAdd6_NEON;624VP8LPredictorsAdd[7] = PredictorAdd7_NEON;625VP8LPredictorsAdd[8] = PredictorAdd8_NEON;626VP8LPredictorsAdd[9] = PredictorAdd9_NEON;627VP8LPredictorsAdd[10] = PredictorAdd10_NEON;628VP8LPredictorsAdd[11] = PredictorAdd11_NEON;629VP8LPredictorsAdd[12] = PredictorAdd12_NEON;630VP8LPredictorsAdd[13] = PredictorAdd13_NEON;631632VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA_NEON;633VP8LConvertBGRAToBGR = ConvertBGRAToBGR_NEON;634VP8LConvertBGRAToRGB = ConvertBGRAToRGB_NEON;635636VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed_NEON;637VP8LTransformColorInverse = TransformColorInverse_NEON;638}639640#else // !WEBP_USE_NEON641642WEBP_DSP_INIT_STUB(VP8LDspInitNEON)643644#endif // WEBP_USE_NEON645646647