Path: blob/master/thirdparty/libwebp/src/dsp/lossless_neon.c
21654 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"21#include "src/webp/format_constants.h"2223//------------------------------------------------------------------------------24// Colorspace conversion functions2526#if !defined(WORK_AROUND_GCC)27// gcc 4.6.0 had some trouble (NDK-r9) with this code. We only use it for28// gcc-4.8.x at least.29static void ConvertBGRAToRGBA_NEON(const uint32_t* WEBP_RESTRICT src,30int num_pixels, uint8_t* WEBP_RESTRICT dst) {31const uint32_t* const end = src + (num_pixels & ~15);32for (; src < end; src += 16) {33uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);34// swap B and R. (VSWP d0,d2 has no intrinsics equivalent!)35const uint8x16_t tmp = pixel.val[0];36pixel.val[0] = pixel.val[2];37pixel.val[2] = tmp;38vst4q_u8(dst, pixel);39dst += 64;40}41VP8LConvertBGRAToRGBA_C(src, num_pixels & 15, dst); // left-overs42}4344static void ConvertBGRAToBGR_NEON(const uint32_t* WEBP_RESTRICT src,45int num_pixels, uint8_t* WEBP_RESTRICT dst) {46const uint32_t* const end = src + (num_pixels & ~15);47for (; src < end; src += 16) {48const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);49const uint8x16x3_t tmp = { { pixel.val[0], pixel.val[1], pixel.val[2] } };50vst3q_u8(dst, tmp);51dst += 48;52}53VP8LConvertBGRAToBGR_C(src, num_pixels & 15, dst); // left-overs54}5556static void ConvertBGRAToRGB_NEON(const uint32_t* WEBP_RESTRICT src,57int num_pixels, uint8_t* WEBP_RESTRICT dst) {58const uint32_t* const end = src + (num_pixels & ~15);59for (; src < end; src += 16) {60const uint8x16x4_t pixel = vld4q_u8((uint8_t*)src);61const uint8x16x3_t tmp = { { pixel.val[2], pixel.val[1], pixel.val[0] } };62vst3q_u8(dst, tmp);63dst += 48;64}65VP8LConvertBGRAToRGB_C(src, num_pixels & 15, dst); // left-overs66}6768#else // WORK_AROUND_GCC6970// gcc-4.6.0 fallback7172static const uint8_t kRGBAShuffle[8] = { 2, 1, 0, 3, 6, 5, 4, 7 };7374static void ConvertBGRAToRGBA_NEON(const uint32_t* WEBP_RESTRICT src,75int num_pixels, uint8_t* WEBP_RESTRICT dst) {76const uint32_t* const end = src + (num_pixels & ~1);77const uint8x8_t shuffle = vld1_u8(kRGBAShuffle);78for (; src < end; src += 2) {79const uint8x8_t pixels = vld1_u8((uint8_t*)src);80vst1_u8(dst, vtbl1_u8(pixels, shuffle));81dst += 8;82}83VP8LConvertBGRAToRGBA_C(src, num_pixels & 1, dst); // left-overs84}8586static 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};9192static void ConvertBGRAToBGR_NEON(const uint32_t* WEBP_RESTRICT src,93int num_pixels, uint8_t* WEBP_RESTRICT dst) {94const uint32_t* const end = src + (num_pixels & ~7);95const uint8x8_t shuffle0 = vld1_u8(kBGRShuffle[0]);96const uint8x8_t shuffle1 = vld1_u8(kBGRShuffle[1]);97const uint8x8_t shuffle2 = vld1_u8(kBGRShuffle[2]);98for (; src < end; src += 8) {99uint8x8x4_t pixels;100INIT_VECTOR4(pixels,101vld1_u8((const uint8_t*)(src + 0)),102vld1_u8((const uint8_t*)(src + 2)),103vld1_u8((const uint8_t*)(src + 4)),104vld1_u8((const uint8_t*)(src + 6)));105vst1_u8(dst + 0, vtbl4_u8(pixels, shuffle0));106vst1_u8(dst + 8, vtbl4_u8(pixels, shuffle1));107vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));108dst += 8 * 3;109}110VP8LConvertBGRAToBGR_C(src, num_pixels & 7, dst); // left-overs111}112113static 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};118119static void ConvertBGRAToRGB_NEON(const uint32_t* WEBP_RESTRICT src,120int num_pixels, uint8_t* WEBP_RESTRICT dst) {121const uint32_t* const end = src + (num_pixels & ~7);122const uint8x8_t shuffle0 = vld1_u8(kRGBShuffle[0]);123const uint8x8_t shuffle1 = vld1_u8(kRGBShuffle[1]);124const uint8x8_t shuffle2 = vld1_u8(kRGBShuffle[2]);125for (; src < end; src += 8) {126uint8x8x4_t pixels;127INIT_VECTOR4(pixels,128vld1_u8((const uint8_t*)(src + 0)),129vld1_u8((const uint8_t*)(src + 2)),130vld1_u8((const uint8_t*)(src + 4)),131vld1_u8((const uint8_t*)(src + 6)));132vst1_u8(dst + 0, vtbl4_u8(pixels, shuffle0));133vst1_u8(dst + 8, vtbl4_u8(pixels, shuffle1));134vst1_u8(dst + 16, vtbl4_u8(pixels, shuffle2));135dst += 8 * 3;136}137VP8LConvertBGRAToRGB_C(src, num_pixels & 7, dst); // left-overs138}139140#endif // !WORK_AROUND_GCC141142//------------------------------------------------------------------------------143// Predictor Transform144145#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|D153154static WEBP_INLINE uint8x8_t Average2_u8_NEON(uint32_t a0, uint32_t a1) {155const uint8x8_t A0 = LOAD_U32_AS_U8(a0);156const uint8x8_t A1 = LOAD_U32_AS_U8(a1);157return vhadd_u8(A0, A1);158}159160static WEBP_INLINE uint32_t ClampedAddSubtractHalf_NEON(uint32_t c0,161uint32_t c1,162uint32_t c2) {163const uint8x8_t avg = Average2_u8_NEON(c0, c1);164// Remove one to c2 when bigger than avg.165const uint8x8_t C2 = LOAD_U32_AS_U8(c2);166const uint8x8_t cmp = vcgt_u8(C2, avg);167const uint8x8_t C2_1 = vadd_u8(C2, cmp);168// Compute half of the difference between avg and c2.169const int8x8_t diff_avg = vreinterpret_s8_u8(vhsub_u8(avg, C2_1));170// Compute the sum with avg and saturate.171const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(avg));172const uint8x8_t res = vqmovun_s16(vaddw_s8(avg_16, diff_avg));173const uint32_t output = GET_U8_AS_U32(res);174return output;175}176177static WEBP_INLINE uint32_t Average2_NEON(uint32_t a0, uint32_t a1) {178const uint8x8_t avg_u8x8 = Average2_u8_NEON(a0, a1);179const uint32_t avg = GET_U8_AS_U32(avg_u8x8);180return avg;181}182183static WEBP_INLINE uint32_t Average3_NEON(uint32_t a0, uint32_t a1,184uint32_t a2) {185const uint8x8_t avg0 = Average2_u8_NEON(a0, a2);186const uint8x8_t A1 = LOAD_U32_AS_U8(a1);187const uint32_t avg = GET_U8_AS_U32(vhadd_u8(avg0, A1));188return avg;189}190191static uint32_t Predictor5_NEON(const uint32_t* const left,192const uint32_t* const top) {193return Average3_NEON(*left, top[0], top[1]);194}195static uint32_t Predictor6_NEON(const uint32_t* const left,196const uint32_t* const top) {197return Average2_NEON(*left, top[-1]);198}199static uint32_t Predictor7_NEON(const uint32_t* const left,200const uint32_t* const top) {201return Average2_NEON(*left, top[0]);202}203static uint32_t Predictor13_NEON(const uint32_t* const left,204const uint32_t* const top) {205return ClampedAddSubtractHalf_NEON(*left, top[0], top[-1]);206}207208// Batch versions of those functions.209210// Predictor0: ARGB_BLACK.211static void PredictorAdd0_NEON(const uint32_t* in, const uint32_t* upper,212int num_pixels, uint32_t* WEBP_RESTRICT out) {213int i;214const uint8x16_t black = vreinterpretq_u8_u32(vdupq_n_u32(ARGB_BLACK));215for (i = 0; i + 4 <= num_pixels; i += 4) {216const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);217const uint8x16_t res = vaddq_u8(src, black);218STOREQ_U8_AS_U32P(&out[i], res);219}220VP8LPredictorsAdd_C[0](in + i, upper + i, num_pixels - i, out + i);221}222223// Predictor1: left.224static void PredictorAdd1_NEON(const uint32_t* in, const uint32_t* upper,225int num_pixels, uint32_t* WEBP_RESTRICT out) {226int i;227const uint8x16_t zero = LOADQ_U32_AS_U8(0);228for (i = 0; i + 4 <= num_pixels; i += 4) {229// a | b | c | d230const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);231// 0 | a | b | c232const uint8x16_t shift0 = vextq_u8(zero, src, 12);233// a | a + b | b + c | c + d234const uint8x16_t sum0 = vaddq_u8(src, shift0);235// 0 | 0 | a | a + b236const uint8x16_t shift1 = vextq_u8(zero, sum0, 8);237// a | a + b | a + b + c | a + b + c + d238const uint8x16_t sum1 = vaddq_u8(sum0, shift1);239const uint8x16_t prev = LOADQ_U32_AS_U8(out[i - 1]);240const uint8x16_t res = vaddq_u8(sum1, prev);241STOREQ_U8_AS_U32P(&out[i], res);242}243VP8LPredictorsAdd_C[1](in + i, upper + i, num_pixels - i, out + i);244}245246// Macro that adds 32-bit integers from IN using mod 256 arithmetic247// per 8 bit channel.248#define GENERATE_PREDICTOR_1(X, IN) \249static void PredictorAdd##X##_NEON(const uint32_t* in, \250const uint32_t* upper, int num_pixels, \251uint32_t* WEBP_RESTRICT out) { \252int i; \253for (i = 0; i + 4 <= num_pixels; i += 4) { \254const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \255const uint8x16_t other = LOADQ_U32P_AS_U8(&(IN)); \256const uint8x16_t res = vaddq_u8(src, other); \257STOREQ_U8_AS_U32P(&out[i], res); \258} \259VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \260}261// Predictor2: Top.262GENERATE_PREDICTOR_1(2, upper[i])263// Predictor3: Top-right.264GENERATE_PREDICTOR_1(3, upper[i + 1])265// Predictor4: Top-left.266GENERATE_PREDICTOR_1(4, upper[i - 1])267#undef GENERATE_PREDICTOR_1268269// Predictor5: average(average(left, TR), T)270#define DO_PRED5(LANE) do { \271const uint8x16_t avgLTR = vhaddq_u8(L, TR); \272const uint8x16_t avg = vhaddq_u8(avgLTR, T); \273const uint8x16_t res = vaddq_u8(avg, src); \274vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \275L = ROTATE32_LEFT(res); \276} while (0)277278static void PredictorAdd5_NEON(const uint32_t* in, const uint32_t* upper,279int num_pixels, uint32_t* WEBP_RESTRICT out) {280int i;281uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);282for (i = 0; i + 4 <= num_pixels; i += 4) {283const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);284const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i + 0]);285const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);286DO_PRED5(0);287DO_PRED5(1);288DO_PRED5(2);289DO_PRED5(3);290}291VP8LPredictorsAdd_C[5](in + i, upper + i, num_pixels - i, out + i);292}293#undef DO_PRED5294295#define DO_PRED67(LANE) do { \296const uint8x16_t avg = vhaddq_u8(L, top); \297const uint8x16_t res = vaddq_u8(avg, src); \298vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \299L = ROTATE32_LEFT(res); \300} while (0)301302// Predictor6: average(left, TL)303static void PredictorAdd6_NEON(const uint32_t* in, const uint32_t* upper,304int num_pixels, uint32_t* WEBP_RESTRICT out) {305int i;306uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);307for (i = 0; i + 4 <= num_pixels; i += 4) {308const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);309const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i - 1]);310DO_PRED67(0);311DO_PRED67(1);312DO_PRED67(2);313DO_PRED67(3);314}315VP8LPredictorsAdd_C[6](in + i, upper + i, num_pixels - i, out + i);316}317318// Predictor7: average(left, T)319static void PredictorAdd7_NEON(const uint32_t* in, const uint32_t* upper,320int num_pixels, uint32_t* WEBP_RESTRICT out) {321int i;322uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);323for (i = 0; i + 4 <= num_pixels; i += 4) {324const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);325const uint8x16_t top = LOADQ_U32P_AS_U8(&upper[i]);326DO_PRED67(0);327DO_PRED67(1);328DO_PRED67(2);329DO_PRED67(3);330}331VP8LPredictorsAdd_C[7](in + i, upper + i, num_pixels - i, out + i);332}333#undef DO_PRED67334335#define GENERATE_PREDICTOR_2(X, IN) \336static void PredictorAdd##X##_NEON(const uint32_t* in, \337const uint32_t* upper, int num_pixels, \338uint32_t* WEBP_RESTRICT out) { \339int i; \340for (i = 0; i + 4 <= num_pixels; i += 4) { \341const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]); \342const uint8x16_t Tother = LOADQ_U32P_AS_U8(&(IN)); \343const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]); \344const uint8x16_t avg = vhaddq_u8(T, Tother); \345const uint8x16_t res = vaddq_u8(avg, src); \346STOREQ_U8_AS_U32P(&out[i], res); \347} \348VP8LPredictorsAdd_C[(X)](in + i, upper + i, num_pixels - i, out + i); \349}350// Predictor8: average TL T.351GENERATE_PREDICTOR_2(8, upper[i - 1])352// Predictor9: average T TR.353GENERATE_PREDICTOR_2(9, upper[i + 1])354#undef GENERATE_PREDICTOR_2355356// Predictor10: average of (average of (L,TL), average of (T, TR)).357#define DO_PRED10(LANE) do { \358const uint8x16_t avgLTL = vhaddq_u8(L, TL); \359const uint8x16_t avg = vhaddq_u8(avgTTR, avgLTL); \360const uint8x16_t res = vaddq_u8(avg, src); \361vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \362L = ROTATE32_LEFT(res); \363} while (0)364365static void PredictorAdd10_NEON(const uint32_t* in, const uint32_t* upper,366int num_pixels, uint32_t* WEBP_RESTRICT out) {367int i;368uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);369for (i = 0; i + 4 <= num_pixels; i += 4) {370const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);371const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);372const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);373const uint8x16_t TR = LOADQ_U32P_AS_U8(&upper[i + 1]);374const uint8x16_t avgTTR = vhaddq_u8(T, TR);375DO_PRED10(0);376DO_PRED10(1);377DO_PRED10(2);378DO_PRED10(3);379}380VP8LPredictorsAdd_C[10](in + i, upper + i, num_pixels - i, out + i);381}382#undef DO_PRED10383384// Predictor11: select.385#define DO_PRED11(LANE) do { \386const uint8x16_t sumLin = vaddq_u8(L, src); /* in + L */ \387const uint8x16_t pLTL = vabdq_u8(L, TL); /* |L - TL| */ \388const uint16x8_t sum_LTL = vpaddlq_u8(pLTL); \389const uint32x4_t pa = vpaddlq_u16(sum_LTL); \390const uint32x4_t mask = vcleq_u32(pa, pb); \391const uint8x16_t res = vbslq_u8(vreinterpretq_u8_u32(mask), sumTin, sumLin); \392vst1q_lane_u32(&out[i + (LANE)], vreinterpretq_u32_u8(res), (LANE)); \393L = ROTATE32_LEFT(res); \394} while (0)395396static void PredictorAdd11_NEON(const uint32_t* in, const uint32_t* upper,397int num_pixels, uint32_t* WEBP_RESTRICT out) {398int i;399uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);400for (i = 0; i + 4 <= num_pixels; i += 4) {401const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);402const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);403const uint8x16_t pTTL = vabdq_u8(T, TL); // |T - TL|404const uint16x8_t sum_TTL = vpaddlq_u8(pTTL);405const uint32x4_t pb = vpaddlq_u16(sum_TTL);406const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);407const uint8x16_t sumTin = vaddq_u8(T, src); // in + T408DO_PRED11(0);409DO_PRED11(1);410DO_PRED11(2);411DO_PRED11(3);412}413VP8LPredictorsAdd_C[11](in + i, upper + i, num_pixels - i, out + i);414}415#undef DO_PRED11416417// Predictor12: ClampedAddSubtractFull.418#define DO_PRED12(DIFF, LANE) do { \419const uint8x8_t pred = \420vqmovun_s16(vaddq_s16(vreinterpretq_s16_u16(L), (DIFF))); \421const uint8x8_t res = \422vadd_u8(pred, (LANE <= 1) ? vget_low_u8(src) : vget_high_u8(src)); \423const uint16x8_t res16 = vmovl_u8(res); \424vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \425/* rotate in the left predictor for next iteration */ \426L = vextq_u16(res16, res16, 4); \427} while (0)428429static void PredictorAdd12_NEON(const uint32_t* in, const uint32_t* upper,430int num_pixels, uint32_t* WEBP_RESTRICT out) {431int i;432uint16x8_t L = vmovl_u8(LOAD_U32_AS_U8(out[-1]));433for (i = 0; i + 4 <= num_pixels; i += 4) {434// load four pixels of source435const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);436// precompute the difference T - TL once for all, stored as s16437const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);438const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);439const int16x8_t diff_lo =440vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(T), vget_low_u8(TL)));441const int16x8_t diff_hi =442vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(T), vget_high_u8(TL)));443// loop over the four reconstructed pixels444DO_PRED12(diff_lo, 0);445DO_PRED12(diff_lo, 1);446DO_PRED12(diff_hi, 2);447DO_PRED12(diff_hi, 3);448}449VP8LPredictorsAdd_C[12](in + i, upper + i, num_pixels - i, out + i);450}451#undef DO_PRED12452453// Predictor13: ClampedAddSubtractHalf454#define DO_PRED13(LANE, LOW_OR_HI) do { \455const uint8x16_t avg = vhaddq_u8(L, T); \456const uint8x16_t cmp = vcgtq_u8(TL, avg); \457const uint8x16_t TL_1 = vaddq_u8(TL, cmp); \458/* Compute half of the difference between avg and TL'. */ \459const int8x8_t diff_avg = \460vreinterpret_s8_u8(LOW_OR_HI(vhsubq_u8(avg, TL_1))); \461/* Compute the sum with avg and saturate. */ \462const int16x8_t avg_16 = vreinterpretq_s16_u16(vmovl_u8(LOW_OR_HI(avg))); \463const uint8x8_t delta = vqmovun_s16(vaddw_s8(avg_16, diff_avg)); \464const uint8x8_t res = vadd_u8(LOW_OR_HI(src), delta); \465const uint8x16_t res2 = vcombine_u8(res, res); \466vst1_lane_u32(&out[i + (LANE)], vreinterpret_u32_u8(res), (LANE) & 1); \467L = ROTATE32_LEFT(res2); \468} while (0)469470static void PredictorAdd13_NEON(const uint32_t* in, const uint32_t* upper,471int num_pixels, uint32_t* WEBP_RESTRICT out) {472int i;473uint8x16_t L = LOADQ_U32_AS_U8(out[-1]);474for (i = 0; i + 4 <= num_pixels; i += 4) {475const uint8x16_t src = LOADQ_U32P_AS_U8(&in[i]);476const uint8x16_t T = LOADQ_U32P_AS_U8(&upper[i]);477const uint8x16_t TL = LOADQ_U32P_AS_U8(&upper[i - 1]);478DO_PRED13(0, vget_low_u8);479DO_PRED13(1, vget_low_u8);480DO_PRED13(2, vget_high_u8);481DO_PRED13(3, vget_high_u8);482}483VP8LPredictorsAdd_C[13](in + i, upper + i, num_pixels - i, out + i);484}485#undef DO_PRED13486487#undef LOAD_U32_AS_U8488#undef LOAD_U32P_AS_U8489#undef LOADQ_U32_AS_U8490#undef LOADQ_U32P_AS_U8491#undef GET_U8_AS_U32492#undef GETQ_U8_AS_U32493#undef STOREQ_U8_AS_U32P494#undef ROTATE32_LEFT495496//------------------------------------------------------------------------------497// Subtract-Green Transform498499// vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use500// non-standard versions there.501#if defined(__APPLE__) && WEBP_AARCH64 && \502defined(__apple_build_version__) && (__apple_build_version__< 6020037)503#define USE_VTBLQ504#endif505506#ifdef USE_VTBLQ507// 255 = byte will be zeroed508static const uint8_t kGreenShuffle[16] = {5091, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255510};511512static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,513const uint8x16_t shuffle) {514return vcombine_u8(vtbl1q_u8(argb, vget_low_u8(shuffle)),515vtbl1q_u8(argb, vget_high_u8(shuffle)));516}517#else // !USE_VTBLQ518// 255 = byte will be zeroed519static const uint8_t kGreenShuffle[8] = { 1, 255, 1, 255, 5, 255, 5, 255 };520521static WEBP_INLINE uint8x16_t DoGreenShuffle_NEON(const uint8x16_t argb,522const uint8x8_t shuffle) {523return vcombine_u8(vtbl1_u8(vget_low_u8(argb), shuffle),524vtbl1_u8(vget_high_u8(argb), shuffle));525}526#endif // USE_VTBLQ527528static void AddGreenToBlueAndRed_NEON(const uint32_t* src, int num_pixels,529uint32_t* dst) {530const uint32_t* const end = src + (num_pixels & ~3);531#ifdef USE_VTBLQ532const uint8x16_t shuffle = vld1q_u8(kGreenShuffle);533#else534const uint8x8_t shuffle = vld1_u8(kGreenShuffle);535#endif536for (; src < end; src += 4, dst += 4) {537const uint8x16_t argb = vld1q_u8((const uint8_t*)src);538const uint8x16_t greens = DoGreenShuffle_NEON(argb, shuffle);539vst1q_u8((uint8_t*)dst, vaddq_u8(argb, greens));540}541// fallthrough and finish off with plain-C542VP8LAddGreenToBlueAndRed_C(src, num_pixels & 3, dst);543}544545//------------------------------------------------------------------------------546// Color Transform547548static void TransformColorInverse_NEON(const VP8LMultipliers* const m,549const uint32_t* const src,550int num_pixels, uint32_t* dst) {551// sign-extended multiplying constants, pre-shifted by 6.552#define CST(X) (((int16_t)(m->X << 8)) >> 6)553const int16_t rb[8] = {554CST(green_to_blue), CST(green_to_red),555CST(green_to_blue), CST(green_to_red),556CST(green_to_blue), CST(green_to_red),557CST(green_to_blue), CST(green_to_red)558};559const int16x8_t mults_rb = vld1q_s16(rb);560const int16_t b2[8] = {5610, CST(red_to_blue), 0, CST(red_to_blue),5620, CST(red_to_blue), 0, CST(red_to_blue),563};564const int16x8_t mults_b2 = vld1q_s16(b2);565#undef CST566#ifdef USE_VTBLQ567static const uint8_t kg0g0[16] = {568255, 1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13569};570const uint8x16_t shuffle = vld1q_u8(kg0g0);571#else572static const uint8_t k0g0g[8] = { 255, 1, 255, 1, 255, 5, 255, 5 };573const uint8x8_t shuffle = vld1_u8(k0g0g);574#endif575const uint32x4_t mask_ag = vdupq_n_u32(0xff00ff00u);576int i;577for (i = 0; i + 4 <= num_pixels; i += 4) {578const uint8x16_t in = vld1q_u8((const uint8_t*)(src + i));579const uint32x4_t a0g0 = vandq_u32(vreinterpretq_u32_u8(in), mask_ag);580// 0 g 0 g581const uint8x16_t greens = DoGreenShuffle_NEON(in, shuffle);582// x dr x db1583const int16x8_t A = vqdmulhq_s16(vreinterpretq_s16_u8(greens), mults_rb);584// x r' x b'585const int8x16_t B = vaddq_s8(vreinterpretq_s8_u8(in),586vreinterpretq_s8_s16(A));587// r' 0 b' 0588const int16x8_t C = vshlq_n_s16(vreinterpretq_s16_s8(B), 8);589// x db2 0 0590const int16x8_t D = vqdmulhq_s16(C, mults_b2);591// 0 x db2 0592const uint32x4_t E = vshrq_n_u32(vreinterpretq_u32_s16(D), 8);593// r' x b'' 0594const int8x16_t F = vaddq_s8(vreinterpretq_s8_u32(E),595vreinterpretq_s8_s16(C));596// 0 r' 0 b''597const uint16x8_t G = vshrq_n_u16(vreinterpretq_u16_s8(F), 8);598const uint32x4_t out = vorrq_u32(vreinterpretq_u32_u16(G), a0g0);599vst1q_u32(dst + i, out);600}601// Fall-back to C-version for left-overs.602VP8LTransformColorInverse_C(m, src + i, num_pixels - i, dst + i);603}604605#undef USE_VTBLQ606607//------------------------------------------------------------------------------608// Entry point609610extern void VP8LDspInitNEON(void);611612WEBP_TSAN_IGNORE_FUNCTION void VP8LDspInitNEON(void) {613VP8LPredictors[5] = Predictor5_NEON;614VP8LPredictors[6] = Predictor6_NEON;615VP8LPredictors[7] = Predictor7_NEON;616VP8LPredictors[13] = Predictor13_NEON;617618VP8LPredictorsAdd[0] = PredictorAdd0_NEON;619VP8LPredictorsAdd[1] = PredictorAdd1_NEON;620VP8LPredictorsAdd[2] = PredictorAdd2_NEON;621VP8LPredictorsAdd[3] = PredictorAdd3_NEON;622VP8LPredictorsAdd[4] = PredictorAdd4_NEON;623VP8LPredictorsAdd[5] = PredictorAdd5_NEON;624VP8LPredictorsAdd[6] = PredictorAdd6_NEON;625VP8LPredictorsAdd[7] = PredictorAdd7_NEON;626VP8LPredictorsAdd[8] = PredictorAdd8_NEON;627VP8LPredictorsAdd[9] = PredictorAdd9_NEON;628VP8LPredictorsAdd[10] = PredictorAdd10_NEON;629VP8LPredictorsAdd[11] = PredictorAdd11_NEON;630VP8LPredictorsAdd[12] = PredictorAdd12_NEON;631VP8LPredictorsAdd[13] = PredictorAdd13_NEON;632633VP8LConvertBGRAToRGBA = ConvertBGRAToRGBA_NEON;634VP8LConvertBGRAToBGR = ConvertBGRAToBGR_NEON;635VP8LConvertBGRAToRGB = ConvertBGRAToRGB_NEON;636637VP8LAddGreenToBlueAndRed = AddGreenToBlueAndRed_NEON;638VP8LTransformColorInverse = TransformColorInverse_NEON;639}640641#else // !WEBP_USE_NEON642643WEBP_DSP_INIT_STUB(VP8LDspInitNEON)644645#endif // WEBP_USE_NEON646647648