Path: blob/master/thirdparty/libwebp/sharpyuv/sharpyuv_neon.c
9898 views
// Copyright 2022 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// Speed-critical functions for Sharp YUV.10//11// Author: Skal ([email protected])1213#include "sharpyuv/sharpyuv_dsp.h"1415#if defined(WEBP_USE_NEON)16#include <assert.h>17#include <stdlib.h>18#include <arm_neon.h>1920static uint16_t clip_NEON(int v, int max) {21return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;22}2324static uint64_t SharpYuvUpdateY_NEON(const uint16_t* ref, const uint16_t* src,25uint16_t* dst, int len, int bit_depth) {26const int max_y = (1 << bit_depth) - 1;27int i;28const int16x8_t zero = vdupq_n_s16(0);29const int16x8_t max = vdupq_n_s16(max_y);30uint64x2_t sum = vdupq_n_u64(0);31uint64_t diff;3233for (i = 0; i + 8 <= len; i += 8) {34const int16x8_t A = vreinterpretq_s16_u16(vld1q_u16(ref + i));35const int16x8_t B = vreinterpretq_s16_u16(vld1q_u16(src + i));36const int16x8_t C = vreinterpretq_s16_u16(vld1q_u16(dst + i));37const int16x8_t D = vsubq_s16(A, B); // diff_y38const int16x8_t F = vaddq_s16(C, D); // new_y39const uint16x8_t H =40vreinterpretq_u16_s16(vmaxq_s16(vminq_s16(F, max), zero));41const int16x8_t I = vabsq_s16(D); // abs(diff_y)42vst1q_u16(dst + i, H);43sum = vpadalq_u32(sum, vpaddlq_u16(vreinterpretq_u16_s16(I)));44}45diff = vgetq_lane_u64(sum, 0) + vgetq_lane_u64(sum, 1);46for (; i < len; ++i) {47const int diff_y = ref[i] - src[i];48const int new_y = (int)(dst[i]) + diff_y;49dst[i] = clip_NEON(new_y, max_y);50diff += (uint64_t)(abs(diff_y));51}52return diff;53}5455static void SharpYuvUpdateRGB_NEON(const int16_t* ref, const int16_t* src,56int16_t* dst, int len) {57int i;58for (i = 0; i + 8 <= len; i += 8) {59const int16x8_t A = vld1q_s16(ref + i);60const int16x8_t B = vld1q_s16(src + i);61const int16x8_t C = vld1q_s16(dst + i);62const int16x8_t D = vsubq_s16(A, B); // diff_uv63const int16x8_t E = vaddq_s16(C, D); // new_uv64vst1q_s16(dst + i, E);65}66for (; i < len; ++i) {67const int diff_uv = ref[i] - src[i];68dst[i] += diff_uv;69}70}7172static void SharpYuvFilterRow16_NEON(const int16_t* A, const int16_t* B,73int len, const uint16_t* best_y,74uint16_t* out, int bit_depth) {75const int max_y = (1 << bit_depth) - 1;76int i;77const int16x8_t max = vdupq_n_s16(max_y);78const int16x8_t zero = vdupq_n_s16(0);79for (i = 0; i + 8 <= len; i += 8) {80const int16x8_t a0 = vld1q_s16(A + i + 0);81const int16x8_t a1 = vld1q_s16(A + i + 1);82const int16x8_t b0 = vld1q_s16(B + i + 0);83const int16x8_t b1 = vld1q_s16(B + i + 1);84const int16x8_t a0b1 = vaddq_s16(a0, b1);85const int16x8_t a1b0 = vaddq_s16(a1, b0);86const int16x8_t a0a1b0b1 = vaddq_s16(a0b1, a1b0); // A0+A1+B0+B187const int16x8_t a0b1_2 = vaddq_s16(a0b1, a0b1); // 2*(A0+B1)88const int16x8_t a1b0_2 = vaddq_s16(a1b0, a1b0); // 2*(A1+B0)89const int16x8_t c0 = vshrq_n_s16(vaddq_s16(a0b1_2, a0a1b0b1), 3);90const int16x8_t c1 = vshrq_n_s16(vaddq_s16(a1b0_2, a0a1b0b1), 3);91const int16x8_t e0 = vrhaddq_s16(c1, a0);92const int16x8_t e1 = vrhaddq_s16(c0, a1);93const int16x8x2_t f = vzipq_s16(e0, e1);94const int16x8_t g0 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 0));95const int16x8_t g1 = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i + 8));96const int16x8_t h0 = vaddq_s16(g0, f.val[0]);97const int16x8_t h1 = vaddq_s16(g1, f.val[1]);98const int16x8_t i0 = vmaxq_s16(vminq_s16(h0, max), zero);99const int16x8_t i1 = vmaxq_s16(vminq_s16(h1, max), zero);100vst1q_u16(out + 2 * i + 0, vreinterpretq_u16_s16(i0));101vst1q_u16(out + 2 * i + 8, vreinterpretq_u16_s16(i1));102}103for (; i < len; ++i) {104const int a0b1 = A[i + 0] + B[i + 1];105const int a1b0 = A[i + 1] + B[i + 0];106const int a0a1b0b1 = a0b1 + a1b0 + 8;107const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;108const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;109out[2 * i + 0] = clip_NEON(best_y[2 * i + 0] + v0, max_y);110out[2 * i + 1] = clip_NEON(best_y[2 * i + 1] + v1, max_y);111}112}113114static void SharpYuvFilterRow32_NEON(const int16_t* A, const int16_t* B,115int len, const uint16_t* best_y,116uint16_t* out, int bit_depth) {117const int max_y = (1 << bit_depth) - 1;118int i;119const uint16x8_t max = vdupq_n_u16(max_y);120for (i = 0; i + 4 <= len; i += 4) {121const int16x4_t a0 = vld1_s16(A + i + 0);122const int16x4_t a1 = vld1_s16(A + i + 1);123const int16x4_t b0 = vld1_s16(B + i + 0);124const int16x4_t b1 = vld1_s16(B + i + 1);125const int32x4_t a0b1 = vaddl_s16(a0, b1);126const int32x4_t a1b0 = vaddl_s16(a1, b0);127const int32x4_t a0a1b0b1 = vaddq_s32(a0b1, a1b0); // A0+A1+B0+B1128const int32x4_t a0b1_2 = vaddq_s32(a0b1, a0b1); // 2*(A0+B1)129const int32x4_t a1b0_2 = vaddq_s32(a1b0, a1b0); // 2*(A1+B0)130const int32x4_t c0 = vshrq_n_s32(vaddq_s32(a0b1_2, a0a1b0b1), 3);131const int32x4_t c1 = vshrq_n_s32(vaddq_s32(a1b0_2, a0a1b0b1), 3);132const int32x4_t e0 = vrhaddq_s32(c1, vmovl_s16(a0));133const int32x4_t e1 = vrhaddq_s32(c0, vmovl_s16(a1));134const int32x4x2_t f = vzipq_s32(e0, e1);135136const int16x8_t g = vreinterpretq_s16_u16(vld1q_u16(best_y + 2 * i));137const int32x4_t h0 = vaddw_s16(f.val[0], vget_low_s16(g));138const int32x4_t h1 = vaddw_s16(f.val[1], vget_high_s16(g));139const uint16x8_t i_16 = vcombine_u16(vqmovun_s32(h0), vqmovun_s32(h1));140const uint16x8_t i_clamped = vminq_u16(i_16, max);141vst1q_u16(out + 2 * i + 0, i_clamped);142}143for (; i < len; ++i) {144const int a0b1 = A[i + 0] + B[i + 1];145const int a1b0 = A[i + 1] + B[i + 0];146const int a0a1b0b1 = a0b1 + a1b0 + 8;147const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;148const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;149out[2 * i + 0] = clip_NEON(best_y[2 * i + 0] + v0, max_y);150out[2 * i + 1] = clip_NEON(best_y[2 * i + 1] + v1, max_y);151}152}153154static void SharpYuvFilterRow_NEON(const int16_t* A, const int16_t* B, int len,155const uint16_t* best_y, uint16_t* out,156int bit_depth) {157if (bit_depth <= 10) {158SharpYuvFilterRow16_NEON(A, B, len, best_y, out, bit_depth);159} else {160SharpYuvFilterRow32_NEON(A, B, len, best_y, out, bit_depth);161}162}163164//------------------------------------------------------------------------------165166extern void InitSharpYuvNEON(void);167168WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvNEON(void) {169SharpYuvUpdateY = SharpYuvUpdateY_NEON;170SharpYuvUpdateRGB = SharpYuvUpdateRGB_NEON;171SharpYuvFilterRow = SharpYuvFilterRow_NEON;172}173174#else // !WEBP_USE_NEON175176extern void InitSharpYuvNEON(void);177178void InitSharpYuvNEON(void) {}179180#endif // WEBP_USE_NEON181182183