Path: blob/master/3rdparty/libwebp/src/dsp/enc_neon.c
16348 views
// Copyright 2012 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 speed-critical encoding functions.10//11// adapted from libvpx (http://www.webmproject.org/code/)1213#include "src/dsp/dsp.h"1415#if defined(WEBP_USE_NEON)1617#include <assert.h>1819#include "src/dsp/neon.h"20#include "src/enc/vp8i_enc.h"2122//------------------------------------------------------------------------------23// Transforms (Paragraph 14.4)2425// Inverse transform.26// This code is pretty much the same as TransformOne in the dec_neon.c, except27// for subtraction to *ref. See the comments there for algorithmic explanations.2829static const int16_t kC1 = 20091;30static const int16_t kC2 = 17734; // half of kC2, actually. See comment above.3132// This code works but is *slower* than the inlined-asm version below33// (with gcc-4.6). So we disable it for now. Later, it'll be conditional to34// WEBP_USE_INTRINSICS define.35// With gcc-4.8, it's a little faster speed than inlined-assembly.36#if defined(WEBP_USE_INTRINSICS)3738// Treats 'v' as an uint8x8_t and zero extends to an int16x8_t.39static WEBP_INLINE int16x8_t ConvertU8ToS16_NEON(uint32x2_t v) {40return vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(v)));41}4243// Performs unsigned 8b saturation on 'dst01' and 'dst23' storing the result44// to the corresponding rows of 'dst'.45static WEBP_INLINE void SaturateAndStore4x4_NEON(uint8_t* const dst,46const int16x8_t dst01,47const int16x8_t dst23) {48// Unsigned saturate to 8b.49const uint8x8_t dst01_u8 = vqmovun_s16(dst01);50const uint8x8_t dst23_u8 = vqmovun_s16(dst23);5152// Store the results.53vst1_lane_u32((uint32_t*)(dst + 0 * BPS), vreinterpret_u32_u8(dst01_u8), 0);54vst1_lane_u32((uint32_t*)(dst + 1 * BPS), vreinterpret_u32_u8(dst01_u8), 1);55vst1_lane_u32((uint32_t*)(dst + 2 * BPS), vreinterpret_u32_u8(dst23_u8), 0);56vst1_lane_u32((uint32_t*)(dst + 3 * BPS), vreinterpret_u32_u8(dst23_u8), 1);57}5859static WEBP_INLINE void Add4x4_NEON(const int16x8_t row01,60const int16x8_t row23,61const uint8_t* const ref,62uint8_t* const dst) {63uint32x2_t dst01 = vdup_n_u32(0);64uint32x2_t dst23 = vdup_n_u32(0);6566// Load the source pixels.67dst01 = vld1_lane_u32((uint32_t*)(ref + 0 * BPS), dst01, 0);68dst23 = vld1_lane_u32((uint32_t*)(ref + 2 * BPS), dst23, 0);69dst01 = vld1_lane_u32((uint32_t*)(ref + 1 * BPS), dst01, 1);70dst23 = vld1_lane_u32((uint32_t*)(ref + 3 * BPS), dst23, 1);7172{73// Convert to 16b.74const int16x8_t dst01_s16 = ConvertU8ToS16_NEON(dst01);75const int16x8_t dst23_s16 = ConvertU8ToS16_NEON(dst23);7677// Descale with rounding.78const int16x8_t out01 = vrsraq_n_s16(dst01_s16, row01, 3);79const int16x8_t out23 = vrsraq_n_s16(dst23_s16, row23, 3);80// Add the inverse transform.81SaturateAndStore4x4_NEON(dst, out01, out23);82}83}8485static WEBP_INLINE void Transpose8x2_NEON(const int16x8_t in0,86const int16x8_t in1,87int16x8x2_t* const out) {88// a0 a1 a2 a3 | b0 b1 b2 b3 => a0 b0 c0 d0 | a1 b1 c1 d189// c0 c1 c2 c3 | d0 d1 d2 d3 a2 b2 c2 d2 | a3 b3 c3 d390const int16x8x2_t tmp0 = vzipq_s16(in0, in1); // a0 c0 a1 c1 a2 c2 ...91// b0 d0 b1 d1 b2 d2 ...92*out = vzipq_s16(tmp0.val[0], tmp0.val[1]);93}9495static WEBP_INLINE void TransformPass_NEON(int16x8x2_t* const rows) {96// {rows} = in0 | in497// in8 | in1298// B1 = in4 | in1299const int16x8_t B1 =100vcombine_s16(vget_high_s16(rows->val[0]), vget_high_s16(rows->val[1]));101// C0 = kC1 * in4 | kC1 * in12102// C1 = kC2 * in4 | kC2 * in12103const int16x8_t C0 = vsraq_n_s16(B1, vqdmulhq_n_s16(B1, kC1), 1);104const int16x8_t C1 = vqdmulhq_n_s16(B1, kC2);105const int16x4_t a = vqadd_s16(vget_low_s16(rows->val[0]),106vget_low_s16(rows->val[1])); // in0 + in8107const int16x4_t b = vqsub_s16(vget_low_s16(rows->val[0]),108vget_low_s16(rows->val[1])); // in0 - in8109// c = kC2 * in4 - kC1 * in12110// d = kC1 * in4 + kC2 * in12111const int16x4_t c = vqsub_s16(vget_low_s16(C1), vget_high_s16(C0));112const int16x4_t d = vqadd_s16(vget_low_s16(C0), vget_high_s16(C1));113const int16x8_t D0 = vcombine_s16(a, b); // D0 = a | b114const int16x8_t D1 = vcombine_s16(d, c); // D1 = d | c115const int16x8_t E0 = vqaddq_s16(D0, D1); // a+d | b+c116const int16x8_t E_tmp = vqsubq_s16(D0, D1); // a-d | b-c117const int16x8_t E1 = vcombine_s16(vget_high_s16(E_tmp), vget_low_s16(E_tmp));118Transpose8x2_NEON(E0, E1, rows);119}120121static void ITransformOne_NEON(const uint8_t* ref,122const int16_t* in, uint8_t* dst) {123int16x8x2_t rows;124INIT_VECTOR2(rows, vld1q_s16(in + 0), vld1q_s16(in + 8));125TransformPass_NEON(&rows);126TransformPass_NEON(&rows);127Add4x4_NEON(rows.val[0], rows.val[1], ref, dst);128}129130#else131132static void ITransformOne_NEON(const uint8_t* ref,133const int16_t* in, uint8_t* dst) {134const int kBPS = BPS;135const int16_t kC1C2[] = { kC1, kC2, 0, 0 };136137__asm__ volatile (138"vld1.16 {q1, q2}, [%[in]] \n"139"vld1.16 {d0}, [%[kC1C2]] \n"140141// d2: in[0]142// d3: in[8]143// d4: in[4]144// d5: in[12]145"vswp d3, d4 \n"146147// q8 = {in[4], in[12]} * kC1 * 2 >> 16148// q9 = {in[4], in[12]} * kC2 >> 16149"vqdmulh.s16 q8, q2, d0[0] \n"150"vqdmulh.s16 q9, q2, d0[1] \n"151152// d22 = a = in[0] + in[8]153// d23 = b = in[0] - in[8]154"vqadd.s16 d22, d2, d3 \n"155"vqsub.s16 d23, d2, d3 \n"156157// q8 = in[4]/[12] * kC1 >> 16158"vshr.s16 q8, q8, #1 \n"159160// Add {in[4], in[12]} back after the multiplication.161"vqadd.s16 q8, q2, q8 \n"162163// d20 = c = in[4]*kC2 - in[12]*kC1164// d21 = d = in[4]*kC1 + in[12]*kC2165"vqsub.s16 d20, d18, d17 \n"166"vqadd.s16 d21, d19, d16 \n"167168// d2 = tmp[0] = a + d169// d3 = tmp[1] = b + c170// d4 = tmp[2] = b - c171// d5 = tmp[3] = a - d172"vqadd.s16 d2, d22, d21 \n"173"vqadd.s16 d3, d23, d20 \n"174"vqsub.s16 d4, d23, d20 \n"175"vqsub.s16 d5, d22, d21 \n"176177"vzip.16 q1, q2 \n"178"vzip.16 q1, q2 \n"179180"vswp d3, d4 \n"181182// q8 = {tmp[4], tmp[12]} * kC1 * 2 >> 16183// q9 = {tmp[4], tmp[12]} * kC2 >> 16184"vqdmulh.s16 q8, q2, d0[0] \n"185"vqdmulh.s16 q9, q2, d0[1] \n"186187// d22 = a = tmp[0] + tmp[8]188// d23 = b = tmp[0] - tmp[8]189"vqadd.s16 d22, d2, d3 \n"190"vqsub.s16 d23, d2, d3 \n"191192"vshr.s16 q8, q8, #1 \n"193"vqadd.s16 q8, q2, q8 \n"194195// d20 = c = in[4]*kC2 - in[12]*kC1196// d21 = d = in[4]*kC1 + in[12]*kC2197"vqsub.s16 d20, d18, d17 \n"198"vqadd.s16 d21, d19, d16 \n"199200// d2 = tmp[0] = a + d201// d3 = tmp[1] = b + c202// d4 = tmp[2] = b - c203// d5 = tmp[3] = a - d204"vqadd.s16 d2, d22, d21 \n"205"vqadd.s16 d3, d23, d20 \n"206"vqsub.s16 d4, d23, d20 \n"207"vqsub.s16 d5, d22, d21 \n"208209"vld1.32 d6[0], [%[ref]], %[kBPS] \n"210"vld1.32 d6[1], [%[ref]], %[kBPS] \n"211"vld1.32 d7[0], [%[ref]], %[kBPS] \n"212"vld1.32 d7[1], [%[ref]], %[kBPS] \n"213214"sub %[ref], %[ref], %[kBPS], lsl #2 \n"215216// (val) + 4 >> 3217"vrshr.s16 d2, d2, #3 \n"218"vrshr.s16 d3, d3, #3 \n"219"vrshr.s16 d4, d4, #3 \n"220"vrshr.s16 d5, d5, #3 \n"221222"vzip.16 q1, q2 \n"223"vzip.16 q1, q2 \n"224225// Must accumulate before saturating226"vmovl.u8 q8, d6 \n"227"vmovl.u8 q9, d7 \n"228229"vqadd.s16 q1, q1, q8 \n"230"vqadd.s16 q2, q2, q9 \n"231232"vqmovun.s16 d0, q1 \n"233"vqmovun.s16 d1, q2 \n"234235"vst1.32 d0[0], [%[dst]], %[kBPS] \n"236"vst1.32 d0[1], [%[dst]], %[kBPS] \n"237"vst1.32 d1[0], [%[dst]], %[kBPS] \n"238"vst1.32 d1[1], [%[dst]] \n"239240: [in] "+r"(in), [dst] "+r"(dst) // modified registers241: [kBPS] "r"(kBPS), [kC1C2] "r"(kC1C2), [ref] "r"(ref) // constants242: "memory", "q0", "q1", "q2", "q8", "q9", "q10", "q11" // clobbered243);244}245246#endif // WEBP_USE_INTRINSICS247248static void ITransform_NEON(const uint8_t* ref,249const int16_t* in, uint8_t* dst, int do_two) {250ITransformOne_NEON(ref, in, dst);251if (do_two) {252ITransformOne_NEON(ref + 4, in + 16, dst + 4);253}254}255256// Load all 4x4 pixels into a single uint8x16_t variable.257static uint8x16_t Load4x4_NEON(const uint8_t* src) {258uint32x4_t out = vdupq_n_u32(0);259out = vld1q_lane_u32((const uint32_t*)(src + 0 * BPS), out, 0);260out = vld1q_lane_u32((const uint32_t*)(src + 1 * BPS), out, 1);261out = vld1q_lane_u32((const uint32_t*)(src + 2 * BPS), out, 2);262out = vld1q_lane_u32((const uint32_t*)(src + 3 * BPS), out, 3);263return vreinterpretq_u8_u32(out);264}265266// Forward transform.267268#if defined(WEBP_USE_INTRINSICS)269270static WEBP_INLINE void Transpose4x4_S16_NEON(const int16x4_t A,271const int16x4_t B,272const int16x4_t C,273const int16x4_t D,274int16x8_t* const out01,275int16x8_t* const out32) {276const int16x4x2_t AB = vtrn_s16(A, B);277const int16x4x2_t CD = vtrn_s16(C, D);278const int32x2x2_t tmp02 = vtrn_s32(vreinterpret_s32_s16(AB.val[0]),279vreinterpret_s32_s16(CD.val[0]));280const int32x2x2_t tmp13 = vtrn_s32(vreinterpret_s32_s16(AB.val[1]),281vreinterpret_s32_s16(CD.val[1]));282*out01 = vreinterpretq_s16_s64(283vcombine_s64(vreinterpret_s64_s32(tmp02.val[0]),284vreinterpret_s64_s32(tmp13.val[0])));285*out32 = vreinterpretq_s16_s64(286vcombine_s64(vreinterpret_s64_s32(tmp13.val[1]),287vreinterpret_s64_s32(tmp02.val[1])));288}289290static WEBP_INLINE int16x8_t DiffU8ToS16_NEON(const uint8x8_t a,291const uint8x8_t b) {292return vreinterpretq_s16_u16(vsubl_u8(a, b));293}294295static void FTransform_NEON(const uint8_t* src, const uint8_t* ref,296int16_t* out) {297int16x8_t d0d1, d3d2; // working 4x4 int16 variables298{299const uint8x16_t S0 = Load4x4_NEON(src);300const uint8x16_t R0 = Load4x4_NEON(ref);301const int16x8_t D0D1 = DiffU8ToS16_NEON(vget_low_u8(S0), vget_low_u8(R0));302const int16x8_t D2D3 = DiffU8ToS16_NEON(vget_high_u8(S0), vget_high_u8(R0));303const int16x4_t D0 = vget_low_s16(D0D1);304const int16x4_t D1 = vget_high_s16(D0D1);305const int16x4_t D2 = vget_low_s16(D2D3);306const int16x4_t D3 = vget_high_s16(D2D3);307Transpose4x4_S16_NEON(D0, D1, D2, D3, &d0d1, &d3d2);308}309{ // 1rst pass310const int32x4_t kCst937 = vdupq_n_s32(937);311const int32x4_t kCst1812 = vdupq_n_s32(1812);312const int16x8_t a0a1 = vaddq_s16(d0d1, d3d2); // d0+d3 | d1+d2 (=a0|a1)313const int16x8_t a3a2 = vsubq_s16(d0d1, d3d2); // d0-d3 | d1-d2 (=a3|a2)314const int16x8_t a0a1_2 = vshlq_n_s16(a0a1, 3);315const int16x4_t tmp0 = vadd_s16(vget_low_s16(a0a1_2),316vget_high_s16(a0a1_2));317const int16x4_t tmp2 = vsub_s16(vget_low_s16(a0a1_2),318vget_high_s16(a0a1_2));319const int32x4_t a3_2217 = vmull_n_s16(vget_low_s16(a3a2), 2217);320const int32x4_t a2_2217 = vmull_n_s16(vget_high_s16(a3a2), 2217);321const int32x4_t a2_p_a3 = vmlal_n_s16(a2_2217, vget_low_s16(a3a2), 5352);322const int32x4_t a3_m_a2 = vmlsl_n_s16(a3_2217, vget_high_s16(a3a2), 5352);323const int16x4_t tmp1 = vshrn_n_s32(vaddq_s32(a2_p_a3, kCst1812), 9);324const int16x4_t tmp3 = vshrn_n_s32(vaddq_s32(a3_m_a2, kCst937), 9);325Transpose4x4_S16_NEON(tmp0, tmp1, tmp2, tmp3, &d0d1, &d3d2);326}327{ // 2nd pass328// the (1<<16) addition is for the replacement: a3!=0 <-> 1-(a3==0)329const int32x4_t kCst12000 = vdupq_n_s32(12000 + (1 << 16));330const int32x4_t kCst51000 = vdupq_n_s32(51000);331const int16x8_t a0a1 = vaddq_s16(d0d1, d3d2); // d0+d3 | d1+d2 (=a0|a1)332const int16x8_t a3a2 = vsubq_s16(d0d1, d3d2); // d0-d3 | d1-d2 (=a3|a2)333const int16x4_t a0_k7 = vadd_s16(vget_low_s16(a0a1), vdup_n_s16(7));334const int16x4_t out0 = vshr_n_s16(vadd_s16(a0_k7, vget_high_s16(a0a1)), 4);335const int16x4_t out2 = vshr_n_s16(vsub_s16(a0_k7, vget_high_s16(a0a1)), 4);336const int32x4_t a3_2217 = vmull_n_s16(vget_low_s16(a3a2), 2217);337const int32x4_t a2_2217 = vmull_n_s16(vget_high_s16(a3a2), 2217);338const int32x4_t a2_p_a3 = vmlal_n_s16(a2_2217, vget_low_s16(a3a2), 5352);339const int32x4_t a3_m_a2 = vmlsl_n_s16(a3_2217, vget_high_s16(a3a2), 5352);340const int16x4_t tmp1 = vaddhn_s32(a2_p_a3, kCst12000);341const int16x4_t out3 = vaddhn_s32(a3_m_a2, kCst51000);342const int16x4_t a3_eq_0 =343vreinterpret_s16_u16(vceq_s16(vget_low_s16(a3a2), vdup_n_s16(0)));344const int16x4_t out1 = vadd_s16(tmp1, a3_eq_0);345vst1_s16(out + 0, out0);346vst1_s16(out + 4, out1);347vst1_s16(out + 8, out2);348vst1_s16(out + 12, out3);349}350}351352#else353354// adapted from vp8/encoder/arm/neon/shortfdct_neon.asm355static const int16_t kCoeff16[] = {3565352, 5352, 5352, 5352, 2217, 2217, 2217, 2217357};358static const int32_t kCoeff32[] = {3591812, 1812, 1812, 1812,360937, 937, 937, 937,36112000, 12000, 12000, 12000,36251000, 51000, 51000, 51000363};364365static void FTransform_NEON(const uint8_t* src, const uint8_t* ref,366int16_t* out) {367const int kBPS = BPS;368const uint8_t* src_ptr = src;369const uint8_t* ref_ptr = ref;370const int16_t* coeff16 = kCoeff16;371const int32_t* coeff32 = kCoeff32;372373__asm__ volatile (374// load src into q4, q5 in high half375"vld1.8 {d8}, [%[src_ptr]], %[kBPS] \n"376"vld1.8 {d10}, [%[src_ptr]], %[kBPS] \n"377"vld1.8 {d9}, [%[src_ptr]], %[kBPS] \n"378"vld1.8 {d11}, [%[src_ptr]] \n"379380// load ref into q6, q7 in high half381"vld1.8 {d12}, [%[ref_ptr]], %[kBPS] \n"382"vld1.8 {d14}, [%[ref_ptr]], %[kBPS] \n"383"vld1.8 {d13}, [%[ref_ptr]], %[kBPS] \n"384"vld1.8 {d15}, [%[ref_ptr]] \n"385386// Pack the high values in to q4 and q6387"vtrn.32 q4, q5 \n"388"vtrn.32 q6, q7 \n"389390// d[0-3] = src - ref391"vsubl.u8 q0, d8, d12 \n"392"vsubl.u8 q1, d9, d13 \n"393394// load coeff16 into q8(d16=5352, d17=2217)395"vld1.16 {q8}, [%[coeff16]] \n"396397// load coeff32 high half into q9 = 1812, q10 = 937398"vld1.32 {q9, q10}, [%[coeff32]]! \n"399400// load coeff32 low half into q11=12000, q12=51000401"vld1.32 {q11,q12}, [%[coeff32]] \n"402403// part 1404// Transpose. Register dN is the same as dN in C405"vtrn.32 d0, d2 \n"406"vtrn.32 d1, d3 \n"407"vtrn.16 d0, d1 \n"408"vtrn.16 d2, d3 \n"409410"vadd.s16 d4, d0, d3 \n" // a0 = d0 + d3411"vadd.s16 d5, d1, d2 \n" // a1 = d1 + d2412"vsub.s16 d6, d1, d2 \n" // a2 = d1 - d2413"vsub.s16 d7, d0, d3 \n" // a3 = d0 - d3414415"vadd.s16 d0, d4, d5 \n" // a0 + a1416"vshl.s16 d0, d0, #3 \n" // temp[0+i*4] = (a0+a1) << 3417"vsub.s16 d2, d4, d5 \n" // a0 - a1418"vshl.s16 d2, d2, #3 \n" // (temp[2+i*4] = (a0-a1) << 3419420"vmlal.s16 q9, d7, d16 \n" // a3*5352 + 1812421"vmlal.s16 q10, d7, d17 \n" // a3*2217 + 937422"vmlal.s16 q9, d6, d17 \n" // a2*2217 + a3*5352 + 1812423"vmlsl.s16 q10, d6, d16 \n" // a3*2217 + 937 - a2*5352424425// temp[1+i*4] = (d2*2217 + d3*5352 + 1812) >> 9426// temp[3+i*4] = (d3*2217 + 937 - d2*5352) >> 9427"vshrn.s32 d1, q9, #9 \n"428"vshrn.s32 d3, q10, #9 \n"429430// part 2431// transpose d0=ip[0], d1=ip[4], d2=ip[8], d3=ip[12]432"vtrn.32 d0, d2 \n"433"vtrn.32 d1, d3 \n"434"vtrn.16 d0, d1 \n"435"vtrn.16 d2, d3 \n"436437"vmov.s16 d26, #7 \n"438439"vadd.s16 d4, d0, d3 \n" // a1 = ip[0] + ip[12]440"vadd.s16 d5, d1, d2 \n" // b1 = ip[4] + ip[8]441"vsub.s16 d6, d1, d2 \n" // c1 = ip[4] - ip[8]442"vadd.s16 d4, d4, d26 \n" // a1 + 7443"vsub.s16 d7, d0, d3 \n" // d1 = ip[0] - ip[12]444445"vadd.s16 d0, d4, d5 \n" // op[0] = a1 + b1 + 7446"vsub.s16 d2, d4, d5 \n" // op[8] = a1 - b1 + 7447448"vmlal.s16 q11, d7, d16 \n" // d1*5352 + 12000449"vmlal.s16 q12, d7, d17 \n" // d1*2217 + 51000450451"vceq.s16 d4, d7, #0 \n"452453"vshr.s16 d0, d0, #4 \n"454"vshr.s16 d2, d2, #4 \n"455456"vmlal.s16 q11, d6, d17 \n" // c1*2217 + d1*5352 + 12000457"vmlsl.s16 q12, d6, d16 \n" // d1*2217 - c1*5352 + 51000458459"vmvn d4, d4 \n" // !(d1 == 0)460// op[4] = (c1*2217 + d1*5352 + 12000)>>16461"vshrn.s32 d1, q11, #16 \n"462// op[4] += (d1!=0)463"vsub.s16 d1, d1, d4 \n"464// op[12]= (d1*2217 - c1*5352 + 51000)>>16465"vshrn.s32 d3, q12, #16 \n"466467// set result to out array468"vst1.16 {q0, q1}, [%[out]] \n"469: [src_ptr] "+r"(src_ptr), [ref_ptr] "+r"(ref_ptr),470[coeff32] "+r"(coeff32) // modified registers471: [kBPS] "r"(kBPS), [coeff16] "r"(coeff16),472[out] "r"(out) // constants473: "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",474"q10", "q11", "q12", "q13" // clobbered475);476}477478#endif479480#define LOAD_LANE_16b(VALUE, LANE) do { \481(VALUE) = vld1_lane_s16(src, (VALUE), (LANE)); \482src += stride; \483} while (0)484485static void FTransformWHT_NEON(const int16_t* src, int16_t* out) {486const int stride = 16;487const int16x4_t zero = vdup_n_s16(0);488int32x4x4_t tmp0;489int16x4x4_t in;490INIT_VECTOR4(in, zero, zero, zero, zero);491LOAD_LANE_16b(in.val[0], 0);492LOAD_LANE_16b(in.val[1], 0);493LOAD_LANE_16b(in.val[2], 0);494LOAD_LANE_16b(in.val[3], 0);495LOAD_LANE_16b(in.val[0], 1);496LOAD_LANE_16b(in.val[1], 1);497LOAD_LANE_16b(in.val[2], 1);498LOAD_LANE_16b(in.val[3], 1);499LOAD_LANE_16b(in.val[0], 2);500LOAD_LANE_16b(in.val[1], 2);501LOAD_LANE_16b(in.val[2], 2);502LOAD_LANE_16b(in.val[3], 2);503LOAD_LANE_16b(in.val[0], 3);504LOAD_LANE_16b(in.val[1], 3);505LOAD_LANE_16b(in.val[2], 3);506LOAD_LANE_16b(in.val[3], 3);507508{509// a0 = in[0 * 16] + in[2 * 16]510// a1 = in[1 * 16] + in[3 * 16]511// a2 = in[1 * 16] - in[3 * 16]512// a3 = in[0 * 16] - in[2 * 16]513const int32x4_t a0 = vaddl_s16(in.val[0], in.val[2]);514const int32x4_t a1 = vaddl_s16(in.val[1], in.val[3]);515const int32x4_t a2 = vsubl_s16(in.val[1], in.val[3]);516const int32x4_t a3 = vsubl_s16(in.val[0], in.val[2]);517tmp0.val[0] = vaddq_s32(a0, a1);518tmp0.val[1] = vaddq_s32(a3, a2);519tmp0.val[2] = vsubq_s32(a3, a2);520tmp0.val[3] = vsubq_s32(a0, a1);521}522{523const int32x4x4_t tmp1 = Transpose4x4_NEON(tmp0);524// a0 = tmp[0 + i] + tmp[ 8 + i]525// a1 = tmp[4 + i] + tmp[12 + i]526// a2 = tmp[4 + i] - tmp[12 + i]527// a3 = tmp[0 + i] - tmp[ 8 + i]528const int32x4_t a0 = vaddq_s32(tmp1.val[0], tmp1.val[2]);529const int32x4_t a1 = vaddq_s32(tmp1.val[1], tmp1.val[3]);530const int32x4_t a2 = vsubq_s32(tmp1.val[1], tmp1.val[3]);531const int32x4_t a3 = vsubq_s32(tmp1.val[0], tmp1.val[2]);532const int32x4_t b0 = vhaddq_s32(a0, a1); // (a0 + a1) >> 1533const int32x4_t b1 = vhaddq_s32(a3, a2); // (a3 + a2) >> 1534const int32x4_t b2 = vhsubq_s32(a3, a2); // (a3 - a2) >> 1535const int32x4_t b3 = vhsubq_s32(a0, a1); // (a0 - a1) >> 1536const int16x4_t out0 = vmovn_s32(b0);537const int16x4_t out1 = vmovn_s32(b1);538const int16x4_t out2 = vmovn_s32(b2);539const int16x4_t out3 = vmovn_s32(b3);540541vst1_s16(out + 0, out0);542vst1_s16(out + 4, out1);543vst1_s16(out + 8, out2);544vst1_s16(out + 12, out3);545}546}547#undef LOAD_LANE_16b548549//------------------------------------------------------------------------------550// Texture distortion551//552// We try to match the spectral content (weighted) between source and553// reconstructed samples.554555// a 0123, b 0123556// a 4567, b 4567557// a 89ab, b 89ab558// a cdef, b cdef559//560// transpose561//562// a 048c, b 048c563// a 159d, b 159d564// a 26ae, b 26ae565// a 37bf, b 37bf566//567static WEBP_INLINE int16x8x4_t DistoTranspose4x4S16_NEON(int16x8x4_t q4_in) {568const int16x8x2_t q2_tmp0 = vtrnq_s16(q4_in.val[0], q4_in.val[1]);569const int16x8x2_t q2_tmp1 = vtrnq_s16(q4_in.val[2], q4_in.val[3]);570const int32x4x2_t q2_tmp2 = vtrnq_s32(vreinterpretq_s32_s16(q2_tmp0.val[0]),571vreinterpretq_s32_s16(q2_tmp1.val[0]));572const int32x4x2_t q2_tmp3 = vtrnq_s32(vreinterpretq_s32_s16(q2_tmp0.val[1]),573vreinterpretq_s32_s16(q2_tmp1.val[1]));574q4_in.val[0] = vreinterpretq_s16_s32(q2_tmp2.val[0]);575q4_in.val[2] = vreinterpretq_s16_s32(q2_tmp2.val[1]);576q4_in.val[1] = vreinterpretq_s16_s32(q2_tmp3.val[0]);577q4_in.val[3] = vreinterpretq_s16_s32(q2_tmp3.val[1]);578return q4_in;579}580581static WEBP_INLINE int16x8x4_t DistoHorizontalPass_NEON(582const int16x8x4_t q4_in) {583// {a0, a1} = {in[0] + in[2], in[1] + in[3]}584// {a3, a2} = {in[0] - in[2], in[1] - in[3]}585const int16x8_t q_a0 = vaddq_s16(q4_in.val[0], q4_in.val[2]);586const int16x8_t q_a1 = vaddq_s16(q4_in.val[1], q4_in.val[3]);587const int16x8_t q_a3 = vsubq_s16(q4_in.val[0], q4_in.val[2]);588const int16x8_t q_a2 = vsubq_s16(q4_in.val[1], q4_in.val[3]);589int16x8x4_t q4_out;590// tmp[0] = a0 + a1591// tmp[1] = a3 + a2592// tmp[2] = a3 - a2593// tmp[3] = a0 - a1594INIT_VECTOR4(q4_out,595vabsq_s16(vaddq_s16(q_a0, q_a1)),596vabsq_s16(vaddq_s16(q_a3, q_a2)),597vabdq_s16(q_a3, q_a2), vabdq_s16(q_a0, q_a1));598return q4_out;599}600601static WEBP_INLINE int16x8x4_t DistoVerticalPass_NEON(const uint8x8x4_t q4_in) {602const int16x8_t q_a0 = vreinterpretq_s16_u16(vaddl_u8(q4_in.val[0],603q4_in.val[2]));604const int16x8_t q_a1 = vreinterpretq_s16_u16(vaddl_u8(q4_in.val[1],605q4_in.val[3]));606const int16x8_t q_a2 = vreinterpretq_s16_u16(vsubl_u8(q4_in.val[1],607q4_in.val[3]));608const int16x8_t q_a3 = vreinterpretq_s16_u16(vsubl_u8(q4_in.val[0],609q4_in.val[2]));610int16x8x4_t q4_out;611612INIT_VECTOR4(q4_out,613vaddq_s16(q_a0, q_a1), vaddq_s16(q_a3, q_a2),614vsubq_s16(q_a3, q_a2), vsubq_s16(q_a0, q_a1));615return q4_out;616}617618static WEBP_INLINE int16x4x4_t DistoLoadW_NEON(const uint16_t* w) {619const uint16x8_t q_w07 = vld1q_u16(&w[0]);620const uint16x8_t q_w8f = vld1q_u16(&w[8]);621int16x4x4_t d4_w;622INIT_VECTOR4(d4_w,623vget_low_s16(vreinterpretq_s16_u16(q_w07)),624vget_high_s16(vreinterpretq_s16_u16(q_w07)),625vget_low_s16(vreinterpretq_s16_u16(q_w8f)),626vget_high_s16(vreinterpretq_s16_u16(q_w8f)));627return d4_w;628}629630static WEBP_INLINE int32x2_t DistoSum_NEON(const int16x8x4_t q4_in,631const int16x4x4_t d4_w) {632int32x2_t d_sum;633// sum += w[ 0] * abs(b0);634// sum += w[ 4] * abs(b1);635// sum += w[ 8] * abs(b2);636// sum += w[12] * abs(b3);637int32x4_t q_sum0 = vmull_s16(d4_w.val[0], vget_low_s16(q4_in.val[0]));638int32x4_t q_sum1 = vmull_s16(d4_w.val[1], vget_low_s16(q4_in.val[1]));639int32x4_t q_sum2 = vmull_s16(d4_w.val[2], vget_low_s16(q4_in.val[2]));640int32x4_t q_sum3 = vmull_s16(d4_w.val[3], vget_low_s16(q4_in.val[3]));641q_sum0 = vmlsl_s16(q_sum0, d4_w.val[0], vget_high_s16(q4_in.val[0]));642q_sum1 = vmlsl_s16(q_sum1, d4_w.val[1], vget_high_s16(q4_in.val[1]));643q_sum2 = vmlsl_s16(q_sum2, d4_w.val[2], vget_high_s16(q4_in.val[2]));644q_sum3 = vmlsl_s16(q_sum3, d4_w.val[3], vget_high_s16(q4_in.val[3]));645646q_sum0 = vaddq_s32(q_sum0, q_sum1);647q_sum2 = vaddq_s32(q_sum2, q_sum3);648q_sum2 = vaddq_s32(q_sum0, q_sum2);649d_sum = vpadd_s32(vget_low_s32(q_sum2), vget_high_s32(q_sum2));650d_sum = vpadd_s32(d_sum, d_sum);651return d_sum;652}653654#define LOAD_LANE_32b(src, VALUE, LANE) \655(VALUE) = vld1_lane_u32((const uint32_t*)(src), (VALUE), (LANE))656657// Hadamard transform658// Returns the weighted sum of the absolute value of transformed coefficients.659// w[] contains a row-major 4 by 4 symmetric matrix.660static int Disto4x4_NEON(const uint8_t* const a, const uint8_t* const b,661const uint16_t* const w) {662uint32x2_t d_in_ab_0123 = vdup_n_u32(0);663uint32x2_t d_in_ab_4567 = vdup_n_u32(0);664uint32x2_t d_in_ab_89ab = vdup_n_u32(0);665uint32x2_t d_in_ab_cdef = vdup_n_u32(0);666uint8x8x4_t d4_in;667668// load data a, b669LOAD_LANE_32b(a + 0 * BPS, d_in_ab_0123, 0);670LOAD_LANE_32b(a + 1 * BPS, d_in_ab_4567, 0);671LOAD_LANE_32b(a + 2 * BPS, d_in_ab_89ab, 0);672LOAD_LANE_32b(a + 3 * BPS, d_in_ab_cdef, 0);673LOAD_LANE_32b(b + 0 * BPS, d_in_ab_0123, 1);674LOAD_LANE_32b(b + 1 * BPS, d_in_ab_4567, 1);675LOAD_LANE_32b(b + 2 * BPS, d_in_ab_89ab, 1);676LOAD_LANE_32b(b + 3 * BPS, d_in_ab_cdef, 1);677INIT_VECTOR4(d4_in,678vreinterpret_u8_u32(d_in_ab_0123),679vreinterpret_u8_u32(d_in_ab_4567),680vreinterpret_u8_u32(d_in_ab_89ab),681vreinterpret_u8_u32(d_in_ab_cdef));682683{684// Vertical pass first to avoid a transpose (vertical and horizontal passes685// are commutative because w/kWeightY is symmetric) and subsequent686// transpose.687const int16x8x4_t q4_v = DistoVerticalPass_NEON(d4_in);688const int16x4x4_t d4_w = DistoLoadW_NEON(w);689// horizontal pass690const int16x8x4_t q4_t = DistoTranspose4x4S16_NEON(q4_v);691const int16x8x4_t q4_h = DistoHorizontalPass_NEON(q4_t);692int32x2_t d_sum = DistoSum_NEON(q4_h, d4_w);693694// abs(sum2 - sum1) >> 5695d_sum = vabs_s32(d_sum);696d_sum = vshr_n_s32(d_sum, 5);697return vget_lane_s32(d_sum, 0);698}699}700#undef LOAD_LANE_32b701702static int Disto16x16_NEON(const uint8_t* const a, const uint8_t* const b,703const uint16_t* const w) {704int D = 0;705int x, y;706for (y = 0; y < 16 * BPS; y += 4 * BPS) {707for (x = 0; x < 16; x += 4) {708D += Disto4x4_NEON(a + x + y, b + x + y, w);709}710}711return D;712}713714//------------------------------------------------------------------------------715716static void CollectHistogram_NEON(const uint8_t* ref, const uint8_t* pred,717int start_block, int end_block,718VP8Histogram* const histo) {719const uint16x8_t max_coeff_thresh = vdupq_n_u16(MAX_COEFF_THRESH);720int j;721int distribution[MAX_COEFF_THRESH + 1] = { 0 };722for (j = start_block; j < end_block; ++j) {723int16_t out[16];724FTransform_NEON(ref + VP8DspScan[j], pred + VP8DspScan[j], out);725{726int k;727const int16x8_t a0 = vld1q_s16(out + 0);728const int16x8_t b0 = vld1q_s16(out + 8);729const uint16x8_t a1 = vreinterpretq_u16_s16(vabsq_s16(a0));730const uint16x8_t b1 = vreinterpretq_u16_s16(vabsq_s16(b0));731const uint16x8_t a2 = vshrq_n_u16(a1, 3);732const uint16x8_t b2 = vshrq_n_u16(b1, 3);733const uint16x8_t a3 = vminq_u16(a2, max_coeff_thresh);734const uint16x8_t b3 = vminq_u16(b2, max_coeff_thresh);735vst1q_s16(out + 0, vreinterpretq_s16_u16(a3));736vst1q_s16(out + 8, vreinterpretq_s16_u16(b3));737// Convert coefficients to bin.738for (k = 0; k < 16; ++k) {739++distribution[out[k]];740}741}742}743VP8SetHistogramData(distribution, histo);744}745746//------------------------------------------------------------------------------747748static WEBP_INLINE void AccumulateSSE16_NEON(const uint8_t* const a,749const uint8_t* const b,750uint32x4_t* const sum) {751const uint8x16_t a0 = vld1q_u8(a);752const uint8x16_t b0 = vld1q_u8(b);753const uint8x16_t abs_diff = vabdq_u8(a0, b0);754const uint16x8_t prod1 = vmull_u8(vget_low_u8(abs_diff),755vget_low_u8(abs_diff));756const uint16x8_t prod2 = vmull_u8(vget_high_u8(abs_diff),757vget_high_u8(abs_diff));758/* pair-wise adds and widen */759const uint32x4_t sum1 = vpaddlq_u16(prod1);760const uint32x4_t sum2 = vpaddlq_u16(prod2);761*sum = vaddq_u32(*sum, vaddq_u32(sum1, sum2));762}763764// Horizontal sum of all four uint32_t values in 'sum'.765static int SumToInt_NEON(uint32x4_t sum) {766const uint64x2_t sum2 = vpaddlq_u32(sum);767const uint64_t sum3 = vgetq_lane_u64(sum2, 0) + vgetq_lane_u64(sum2, 1);768return (int)sum3;769}770771static int SSE16x16_NEON(const uint8_t* a, const uint8_t* b) {772uint32x4_t sum = vdupq_n_u32(0);773int y;774for (y = 0; y < 16; ++y) {775AccumulateSSE16_NEON(a + y * BPS, b + y * BPS, &sum);776}777return SumToInt_NEON(sum);778}779780static int SSE16x8_NEON(const uint8_t* a, const uint8_t* b) {781uint32x4_t sum = vdupq_n_u32(0);782int y;783for (y = 0; y < 8; ++y) {784AccumulateSSE16_NEON(a + y * BPS, b + y * BPS, &sum);785}786return SumToInt_NEON(sum);787}788789static int SSE8x8_NEON(const uint8_t* a, const uint8_t* b) {790uint32x4_t sum = vdupq_n_u32(0);791int y;792for (y = 0; y < 8; ++y) {793const uint8x8_t a0 = vld1_u8(a + y * BPS);794const uint8x8_t b0 = vld1_u8(b + y * BPS);795const uint8x8_t abs_diff = vabd_u8(a0, b0);796const uint16x8_t prod = vmull_u8(abs_diff, abs_diff);797sum = vpadalq_u16(sum, prod);798}799return SumToInt_NEON(sum);800}801802static int SSE4x4_NEON(const uint8_t* a, const uint8_t* b) {803const uint8x16_t a0 = Load4x4_NEON(a);804const uint8x16_t b0 = Load4x4_NEON(b);805const uint8x16_t abs_diff = vabdq_u8(a0, b0);806const uint16x8_t prod1 = vmull_u8(vget_low_u8(abs_diff),807vget_low_u8(abs_diff));808const uint16x8_t prod2 = vmull_u8(vget_high_u8(abs_diff),809vget_high_u8(abs_diff));810/* pair-wise adds and widen */811const uint32x4_t sum1 = vpaddlq_u16(prod1);812const uint32x4_t sum2 = vpaddlq_u16(prod2);813return SumToInt_NEON(vaddq_u32(sum1, sum2));814}815816//------------------------------------------------------------------------------817818// Compilation with gcc-4.6.x is problematic for now.819#if !defined(WORK_AROUND_GCC)820821static int16x8_t Quantize_NEON(int16_t* const in,822const VP8Matrix* const mtx, int offset) {823const uint16x8_t sharp = vld1q_u16(&mtx->sharpen_[offset]);824const uint16x8_t q = vld1q_u16(&mtx->q_[offset]);825const uint16x8_t iq = vld1q_u16(&mtx->iq_[offset]);826const uint32x4_t bias0 = vld1q_u32(&mtx->bias_[offset + 0]);827const uint32x4_t bias1 = vld1q_u32(&mtx->bias_[offset + 4]);828829const int16x8_t a = vld1q_s16(in + offset); // in830const uint16x8_t b = vreinterpretq_u16_s16(vabsq_s16(a)); // coeff = abs(in)831const int16x8_t sign = vshrq_n_s16(a, 15); // sign832const uint16x8_t c = vaddq_u16(b, sharp); // + sharpen833const uint32x4_t m0 = vmull_u16(vget_low_u16(c), vget_low_u16(iq));834const uint32x4_t m1 = vmull_u16(vget_high_u16(c), vget_high_u16(iq));835const uint32x4_t m2 = vhaddq_u32(m0, bias0);836const uint32x4_t m3 = vhaddq_u32(m1, bias1); // (coeff * iQ + bias) >> 1837const uint16x8_t c0 = vcombine_u16(vshrn_n_u32(m2, 16),838vshrn_n_u32(m3, 16)); // QFIX=17 = 16+1839const uint16x8_t c1 = vminq_u16(c0, vdupq_n_u16(MAX_LEVEL));840const int16x8_t c2 = veorq_s16(vreinterpretq_s16_u16(c1), sign);841const int16x8_t c3 = vsubq_s16(c2, sign); // restore sign842const int16x8_t c4 = vmulq_s16(c3, vreinterpretq_s16_u16(q));843vst1q_s16(in + offset, c4);844assert(QFIX == 17); // this function can't work as is if QFIX != 16+1845return c3;846}847848static const uint8_t kShuffles[4][8] = {849{ 0, 1, 2, 3, 8, 9, 16, 17 },850{ 10, 11, 4, 5, 6, 7, 12, 13 },851{ 18, 19, 24, 25, 26, 27, 20, 21 },852{ 14, 15, 22, 23, 28, 29, 30, 31 }853};854855static int QuantizeBlock_NEON(int16_t in[16], int16_t out[16],856const VP8Matrix* const mtx) {857const int16x8_t out0 = Quantize_NEON(in, mtx, 0);858const int16x8_t out1 = Quantize_NEON(in, mtx, 8);859uint8x8x4_t shuffles;860// vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use861// non-standard versions there.862#if defined(__APPLE__) && defined(__aarch64__) && \863defined(__apple_build_version__) && (__apple_build_version__< 6020037)864uint8x16x2_t all_out;865INIT_VECTOR2(all_out, vreinterpretq_u8_s16(out0), vreinterpretq_u8_s16(out1));866INIT_VECTOR4(shuffles,867vtbl2q_u8(all_out, vld1_u8(kShuffles[0])),868vtbl2q_u8(all_out, vld1_u8(kShuffles[1])),869vtbl2q_u8(all_out, vld1_u8(kShuffles[2])),870vtbl2q_u8(all_out, vld1_u8(kShuffles[3])));871#else872uint8x8x4_t all_out;873INIT_VECTOR4(all_out,874vreinterpret_u8_s16(vget_low_s16(out0)),875vreinterpret_u8_s16(vget_high_s16(out0)),876vreinterpret_u8_s16(vget_low_s16(out1)),877vreinterpret_u8_s16(vget_high_s16(out1)));878INIT_VECTOR4(shuffles,879vtbl4_u8(all_out, vld1_u8(kShuffles[0])),880vtbl4_u8(all_out, vld1_u8(kShuffles[1])),881vtbl4_u8(all_out, vld1_u8(kShuffles[2])),882vtbl4_u8(all_out, vld1_u8(kShuffles[3])));883#endif884// Zigzag reordering885vst1_u8((uint8_t*)(out + 0), shuffles.val[0]);886vst1_u8((uint8_t*)(out + 4), shuffles.val[1]);887vst1_u8((uint8_t*)(out + 8), shuffles.val[2]);888vst1_u8((uint8_t*)(out + 12), shuffles.val[3]);889// test zeros890if (*(uint64_t*)(out + 0) != 0) return 1;891if (*(uint64_t*)(out + 4) != 0) return 1;892if (*(uint64_t*)(out + 8) != 0) return 1;893if (*(uint64_t*)(out + 12) != 0) return 1;894return 0;895}896897static int Quantize2Blocks_NEON(int16_t in[32], int16_t out[32],898const VP8Matrix* const mtx) {899int nz;900nz = QuantizeBlock_NEON(in + 0 * 16, out + 0 * 16, mtx) << 0;901nz |= QuantizeBlock_NEON(in + 1 * 16, out + 1 * 16, mtx) << 1;902return nz;903}904905#endif // !WORK_AROUND_GCC906907//------------------------------------------------------------------------------908// Entry point909910extern void VP8EncDspInitNEON(void);911912WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitNEON(void) {913VP8ITransform = ITransform_NEON;914VP8FTransform = FTransform_NEON;915916VP8FTransformWHT = FTransformWHT_NEON;917918VP8TDisto4x4 = Disto4x4_NEON;919VP8TDisto16x16 = Disto16x16_NEON;920VP8CollectHistogram = CollectHistogram_NEON;921922VP8SSE16x16 = SSE16x16_NEON;923VP8SSE16x8 = SSE16x8_NEON;924VP8SSE8x8 = SSE8x8_NEON;925VP8SSE4x4 = SSE4x4_NEON;926927#if !defined(WORK_AROUND_GCC)928VP8EncQuantizeBlock = QuantizeBlock_NEON;929VP8EncQuantize2Blocks = Quantize2Blocks_NEON;930#endif931}932933#else // !WEBP_USE_NEON934935WEBP_DSP_INIT_STUB(VP8EncDspInitNEON)936937#endif // WEBP_USE_NEON938939940