Path: blob/master/thirdparty/libwebp/src/dsp/cost_neon.c
9913 views
// Copyright 2018 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// ARM NEON version of cost functions1011#include "src/dsp/dsp.h"1213#if defined(WEBP_USE_NEON)1415#include "src/dsp/neon.h"16#include "src/enc/cost_enc.h"1718static const uint8_t position[16] = { 1, 2, 3, 4, 5, 6, 7, 8,199, 10, 11, 12, 13, 14, 15, 16 };2021static void SetResidualCoeffs_NEON(const int16_t* WEBP_RESTRICT const coeffs,22VP8Residual* WEBP_RESTRICT const res) {23const int16x8_t minus_one = vdupq_n_s16(-1);24const int16x8_t coeffs_0 = vld1q_s16(coeffs);25const int16x8_t coeffs_1 = vld1q_s16(coeffs + 8);26const uint16x8_t eob_0 = vtstq_s16(coeffs_0, minus_one);27const uint16x8_t eob_1 = vtstq_s16(coeffs_1, minus_one);28const uint8x16_t eob = vcombine_u8(vqmovn_u16(eob_0), vqmovn_u16(eob_1));29const uint8x16_t masked = vandq_u8(eob, vld1q_u8(position));3031#if WEBP_AARCH6432res->last = vmaxvq_u8(masked) - 1;33#else34const uint8x8_t eob_8x8 = vmax_u8(vget_low_u8(masked), vget_high_u8(masked));35const uint16x8_t eob_16x8 = vmovl_u8(eob_8x8);36const uint16x4_t eob_16x4 =37vmax_u16(vget_low_u16(eob_16x8), vget_high_u16(eob_16x8));38const uint32x4_t eob_32x4 = vmovl_u16(eob_16x4);39uint32x2_t eob_32x2 =40vmax_u32(vget_low_u32(eob_32x4), vget_high_u32(eob_32x4));41eob_32x2 = vpmax_u32(eob_32x2, eob_32x2);4243vst1_lane_s32(&res->last, vreinterpret_s32_u32(eob_32x2), 0);44--res->last;45#endif // WEBP_AARCH644647res->coeffs = coeffs;48}4950static int GetResidualCost_NEON(int ctx0, const VP8Residual* const res) {51uint8_t levels[16], ctxs[16];52uint16_t abs_levels[16];53int n = res->first;54// should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 155const int p0 = res->prob[n][ctx0][0];56CostArrayPtr const costs = res->costs;57const uint16_t* t = costs[n][ctx0];58// bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 059// (as required by the syntax). For ctx0 == 0, we need to add it here or it'll60// be missing during the loop.61int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0;6263if (res->last < 0) {64return VP8BitCost(0, p0);65}6667{ // precompute clamped levels and contexts, packed to 8b.68const uint8x16_t kCst2 = vdupq_n_u8(2);69const uint8x16_t kCst67 = vdupq_n_u8(MAX_VARIABLE_LEVEL);70const int16x8_t c0 = vld1q_s16(res->coeffs);71const int16x8_t c1 = vld1q_s16(res->coeffs + 8);72const uint16x8_t E0 = vreinterpretq_u16_s16(vabsq_s16(c0));73const uint16x8_t E1 = vreinterpretq_u16_s16(vabsq_s16(c1));74const uint8x16_t F = vcombine_u8(vqmovn_u16(E0), vqmovn_u16(E1));75const uint8x16_t G = vminq_u8(F, kCst2); // context = 0,1,276const uint8x16_t H = vminq_u8(F, kCst67); // clamp_level in [0..67]7778vst1q_u8(ctxs, G);79vst1q_u8(levels, H);8081vst1q_u16(abs_levels, E0);82vst1q_u16(abs_levels + 8, E1);83}84for (; n < res->last; ++n) {85const int ctx = ctxs[n];86const int level = levels[n];87const int flevel = abs_levels[n]; // full level88cost += VP8LevelFixedCosts[flevel] + t[level]; // simplified VP8LevelCost()89t = costs[n + 1][ctx];90}91// Last coefficient is always non-zero92{93const int level = levels[n];94const int flevel = abs_levels[n];95assert(flevel != 0);96cost += VP8LevelFixedCosts[flevel] + t[level];97if (n < 15) {98const int b = VP8EncBands[n + 1];99const int ctx = ctxs[n];100const int last_p0 = res->prob[b][ctx][0];101cost += VP8BitCost(0, last_p0);102}103}104return cost;105}106107//------------------------------------------------------------------------------108// Entry point109110extern void VP8EncDspCostInitNEON(void);111112WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInitNEON(void) {113VP8SetResidualCoeffs = SetResidualCoeffs_NEON;114VP8GetResidualCost = GetResidualCost_NEON;115}116117#else // !WEBP_USE_NEON118119WEBP_DSP_INIT_STUB(VP8EncDspCostInitNEON)120121#endif // WEBP_USE_NEON122123124