Path: blob/master/thirdparty/libwebp/src/dsp/enc_msa.c
9913 views
// Copyright 2016 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// MSA version of encoder dsp functions.10//11// Author: Prashant Patil ([email protected])1213#include "src/dsp/dsp.h"1415#if defined(WEBP_USE_MSA)1617#include <stdlib.h>18#include "src/dsp/msa_macro.h"19#include "src/enc/vp8i_enc.h"2021//------------------------------------------------------------------------------22// Transforms2324#define IDCT_1D_W(in0, in1, in2, in3, out0, out1, out2, out3) do { \25v4i32 a1_m, b1_m, c1_m, d1_m; \26const v4i32 cospi8sqrt2minus1 = __msa_fill_w(20091); \27const v4i32 sinpi8sqrt2 = __msa_fill_w(35468); \28v4i32 c_tmp1_m = in1 * sinpi8sqrt2; \29v4i32 c_tmp2_m = in3 * cospi8sqrt2minus1; \30v4i32 d_tmp1_m = in1 * cospi8sqrt2minus1; \31v4i32 d_tmp2_m = in3 * sinpi8sqrt2; \32\33ADDSUB2(in0, in2, a1_m, b1_m); \34SRAI_W2_SW(c_tmp1_m, c_tmp2_m, 16); \35c_tmp2_m = c_tmp2_m + in3; \36c1_m = c_tmp1_m - c_tmp2_m; \37SRAI_W2_SW(d_tmp1_m, d_tmp2_m, 16); \38d_tmp1_m = d_tmp1_m + in1; \39d1_m = d_tmp1_m + d_tmp2_m; \40BUTTERFLY_4(a1_m, b1_m, c1_m, d1_m, out0, out1, out2, out3); \41} while (0)4243static WEBP_INLINE void ITransformOne(const uint8_t* WEBP_RESTRICT ref,44const int16_t* WEBP_RESTRICT in,45uint8_t* WEBP_RESTRICT dst) {46v8i16 input0, input1;47v4i32 in0, in1, in2, in3, hz0, hz1, hz2, hz3, vt0, vt1, vt2, vt3;48v4i32 res0, res1, res2, res3;49v16i8 dest0, dest1, dest2, dest3;50const v16i8 zero = { 0 };5152LD_SH2(in, 8, input0, input1);53UNPCK_SH_SW(input0, in0, in1);54UNPCK_SH_SW(input1, in2, in3);55IDCT_1D_W(in0, in1, in2, in3, hz0, hz1, hz2, hz3);56TRANSPOSE4x4_SW_SW(hz0, hz1, hz2, hz3, hz0, hz1, hz2, hz3);57IDCT_1D_W(hz0, hz1, hz2, hz3, vt0, vt1, vt2, vt3);58SRARI_W4_SW(vt0, vt1, vt2, vt3, 3);59TRANSPOSE4x4_SW_SW(vt0, vt1, vt2, vt3, vt0, vt1, vt2, vt3);60LD_SB4(ref, BPS, dest0, dest1, dest2, dest3);61ILVR_B4_SW(zero, dest0, zero, dest1, zero, dest2, zero, dest3,62res0, res1, res2, res3);63ILVR_H4_SW(zero, res0, zero, res1, zero, res2, zero, res3,64res0, res1, res2, res3);65ADD4(res0, vt0, res1, vt1, res2, vt2, res3, vt3, res0, res1, res2, res3);66CLIP_SW4_0_255(res0, res1, res2, res3);67PCKEV_B2_SW(res0, res1, res2, res3, vt0, vt1);68res0 = (v4i32)__msa_pckev_b((v16i8)vt0, (v16i8)vt1);69ST4x4_UB(res0, res0, 3, 2, 1, 0, dst, BPS);70}7172static void ITransform_MSA(const uint8_t* WEBP_RESTRICT ref,73const int16_t* WEBP_RESTRICT in,74uint8_t* WEBP_RESTRICT dst, int do_two) {75ITransformOne(ref, in, dst);76if (do_two) {77ITransformOne(ref + 4, in + 16, dst + 4);78}79}8081static void FTransform_MSA(const uint8_t* WEBP_RESTRICT src,82const uint8_t* WEBP_RESTRICT ref,83int16_t* WEBP_RESTRICT out) {84uint64_t out0, out1, out2, out3;85uint32_t in0, in1, in2, in3;86v4i32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;87v8i16 t0, t1, t2, t3;88v16u8 srcl0, srcl1, src0 = { 0 }, src1 = { 0 };89const v8i16 mask0 = { 0, 4, 8, 12, 1, 5, 9, 13 };90const v8i16 mask1 = { 3, 7, 11, 15, 2, 6, 10, 14 };91const v8i16 mask2 = { 4, 0, 5, 1, 6, 2, 7, 3 };92const v8i16 mask3 = { 0, 4, 1, 5, 2, 6, 3, 7 };93const v8i16 cnst0 = { 2217, -5352, 2217, -5352, 2217, -5352, 2217, -5352 };94const v8i16 cnst1 = { 5352, 2217, 5352, 2217, 5352, 2217, 5352, 2217 };9596LW4(src, BPS, in0, in1, in2, in3);97INSERT_W4_UB(in0, in1, in2, in3, src0);98LW4(ref, BPS, in0, in1, in2, in3);99INSERT_W4_UB(in0, in1, in2, in3, src1);100ILVRL_B2_UB(src0, src1, srcl0, srcl1);101HSUB_UB2_SH(srcl0, srcl1, t0, t1);102VSHF_H2_SH(t0, t1, t0, t1, mask0, mask1, t2, t3);103ADDSUB2(t2, t3, t0, t1);104t0 = SRLI_H(t0, 3);105VSHF_H2_SH(t0, t0, t1, t1, mask2, mask3, t3, t2);106tmp0 = __msa_hadd_s_w(t3, t3);107tmp2 = __msa_hsub_s_w(t3, t3);108FILL_W2_SW(1812, 937, tmp1, tmp3);109DPADD_SH2_SW(t2, t2, cnst0, cnst1, tmp3, tmp1);110SRAI_W2_SW(tmp1, tmp3, 9);111PCKEV_H2_SH(tmp1, tmp0, tmp3, tmp2, t0, t1);112VSHF_H2_SH(t0, t1, t0, t1, mask0, mask1, t2, t3);113ADDSUB2(t2, t3, t0, t1);114VSHF_H2_SH(t0, t0, t1, t1, mask2, mask3, t3, t2);115tmp0 = __msa_hadd_s_w(t3, t3);116tmp2 = __msa_hsub_s_w(t3, t3);117ADDVI_W2_SW(tmp0, 7, tmp2, 7, tmp0, tmp2);118SRAI_W2_SW(tmp0, tmp2, 4);119FILL_W2_SW(12000, 51000, tmp1, tmp3);120DPADD_SH2_SW(t2, t2, cnst0, cnst1, tmp3, tmp1);121SRAI_W2_SW(tmp1, tmp3, 16);122UNPCK_R_SH_SW(t1, tmp4);123tmp5 = __msa_ceqi_w(tmp4, 0);124tmp4 = (v4i32)__msa_nor_v((v16u8)tmp5, (v16u8)tmp5);125tmp5 = __msa_fill_w(1);126tmp5 = (v4i32)__msa_and_v((v16u8)tmp5, (v16u8)tmp4);127tmp1 += tmp5;128PCKEV_H2_SH(tmp1, tmp0, tmp3, tmp2, t0, t1);129out0 = __msa_copy_s_d((v2i64)t0, 0);130out1 = __msa_copy_s_d((v2i64)t0, 1);131out2 = __msa_copy_s_d((v2i64)t1, 0);132out3 = __msa_copy_s_d((v2i64)t1, 1);133SD4(out0, out1, out2, out3, out, 8);134}135136static void FTransformWHT_MSA(const int16_t* WEBP_RESTRICT in,137int16_t* WEBP_RESTRICT out) {138v8i16 in0 = { 0 };139v8i16 in1 = { 0 };140v8i16 tmp0, tmp1, tmp2, tmp3;141v8i16 out0, out1;142const v8i16 mask0 = { 0, 1, 2, 3, 8, 9, 10, 11 };143const v8i16 mask1 = { 4, 5, 6, 7, 12, 13, 14, 15 };144const v8i16 mask2 = { 0, 4, 8, 12, 1, 5, 9, 13 };145const v8i16 mask3 = { 3, 7, 11, 15, 2, 6, 10, 14 };146147in0 = __msa_insert_h(in0, 0, in[ 0]);148in0 = __msa_insert_h(in0, 1, in[ 64]);149in0 = __msa_insert_h(in0, 2, in[128]);150in0 = __msa_insert_h(in0, 3, in[192]);151in0 = __msa_insert_h(in0, 4, in[ 16]);152in0 = __msa_insert_h(in0, 5, in[ 80]);153in0 = __msa_insert_h(in0, 6, in[144]);154in0 = __msa_insert_h(in0, 7, in[208]);155in1 = __msa_insert_h(in1, 0, in[ 48]);156in1 = __msa_insert_h(in1, 1, in[112]);157in1 = __msa_insert_h(in1, 2, in[176]);158in1 = __msa_insert_h(in1, 3, in[240]);159in1 = __msa_insert_h(in1, 4, in[ 32]);160in1 = __msa_insert_h(in1, 5, in[ 96]);161in1 = __msa_insert_h(in1, 6, in[160]);162in1 = __msa_insert_h(in1, 7, in[224]);163ADDSUB2(in0, in1, tmp0, tmp1);164VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask0, mask1, tmp2, tmp3);165ADDSUB2(tmp2, tmp3, tmp0, tmp1);166VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask2, mask3, in0, in1);167ADDSUB2(in0, in1, tmp0, tmp1);168VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask0, mask1, tmp2, tmp3);169ADDSUB2(tmp2, tmp3, out0, out1);170SRAI_H2_SH(out0, out1, 1);171ST_SH2(out0, out1, out, 8);172}173174static int TTransform_MSA(const uint8_t* WEBP_RESTRICT in,175const uint16_t* WEBP_RESTRICT w) {176int sum;177uint32_t in0_m, in1_m, in2_m, in3_m;178v16i8 src0 = { 0 };179v8i16 in0, in1, tmp0, tmp1, tmp2, tmp3;180v4i32 dst0, dst1;181const v16i8 zero = { 0 };182const v8i16 mask0 = { 0, 1, 2, 3, 8, 9, 10, 11 };183const v8i16 mask1 = { 4, 5, 6, 7, 12, 13, 14, 15 };184const v8i16 mask2 = { 0, 4, 8, 12, 1, 5, 9, 13 };185const v8i16 mask3 = { 3, 7, 11, 15, 2, 6, 10, 14 };186187LW4(in, BPS, in0_m, in1_m, in2_m, in3_m);188INSERT_W4_SB(in0_m, in1_m, in2_m, in3_m, src0);189ILVRL_B2_SH(zero, src0, tmp0, tmp1);190VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask2, mask3, in0, in1);191ADDSUB2(in0, in1, tmp0, tmp1);192VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask0, mask1, tmp2, tmp3);193ADDSUB2(tmp2, tmp3, tmp0, tmp1);194VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask2, mask3, in0, in1);195ADDSUB2(in0, in1, tmp0, tmp1);196VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask0, mask1, tmp2, tmp3);197ADDSUB2(tmp2, tmp3, tmp0, tmp1);198tmp0 = __msa_add_a_h(tmp0, (v8i16)zero);199tmp1 = __msa_add_a_h(tmp1, (v8i16)zero);200LD_SH2(w, 8, tmp2, tmp3);201DOTP_SH2_SW(tmp0, tmp1, tmp2, tmp3, dst0, dst1);202dst0 = dst0 + dst1;203sum = HADD_SW_S32(dst0);204return sum;205}206207static int Disto4x4_MSA(const uint8_t* WEBP_RESTRICT const a,208const uint8_t* WEBP_RESTRICT const b,209const uint16_t* WEBP_RESTRICT const w) {210const int sum1 = TTransform_MSA(a, w);211const int sum2 = TTransform_MSA(b, w);212return abs(sum2 - sum1) >> 5;213}214215static int Disto16x16_MSA(const uint8_t* WEBP_RESTRICT const a,216const uint8_t* WEBP_RESTRICT const b,217const uint16_t* WEBP_RESTRICT const w) {218int D = 0;219int x, y;220for (y = 0; y < 16 * BPS; y += 4 * BPS) {221for (x = 0; x < 16; x += 4) {222D += Disto4x4_MSA(a + x + y, b + x + y, w);223}224}225return D;226}227228//------------------------------------------------------------------------------229// Histogram230231static void CollectHistogram_MSA(const uint8_t* ref, const uint8_t* pred,232int start_block, int end_block,233VP8Histogram* const histo) {234int j;235int distribution[MAX_COEFF_THRESH + 1] = { 0 };236for (j = start_block; j < end_block; ++j) {237int16_t out[16];238VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);239{240int k;241v8i16 coeff0, coeff1;242const v8i16 zero = { 0 };243const v8i16 max_coeff_thr = __msa_ldi_h(MAX_COEFF_THRESH);244LD_SH2(&out[0], 8, coeff0, coeff1);245coeff0 = __msa_add_a_h(coeff0, zero);246coeff1 = __msa_add_a_h(coeff1, zero);247SRAI_H2_SH(coeff0, coeff1, 3);248coeff0 = __msa_min_s_h(coeff0, max_coeff_thr);249coeff1 = __msa_min_s_h(coeff1, max_coeff_thr);250ST_SH2(coeff0, coeff1, &out[0], 8);251for (k = 0; k < 16; ++k) {252++distribution[out[k]];253}254}255}256VP8SetHistogramData(distribution, histo);257}258259//------------------------------------------------------------------------------260// Intra predictions261262// luma 4x4 prediction263264#define DST(x, y) dst[(x) + (y) * BPS]265#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)266#define AVG2(a, b) (((a) + (b) + 1) >> 1)267268// vertical269static WEBP_INLINE void VE4(uint8_t* WEBP_RESTRICT dst,270const uint8_t* WEBP_RESTRICT top) {271const v16u8 A1 = { 0 };272const uint64_t val_m = LD(top - 1);273const v16u8 A = (v16u8)__msa_insert_d((v2i64)A1, 0, val_m);274const v16u8 B = SLDI_UB(A, A, 1);275const v16u8 C = SLDI_UB(A, A, 2);276const v16u8 AC = __msa_ave_u_b(A, C);277const v16u8 B2 = __msa_ave_u_b(B, B);278const v16u8 R = __msa_aver_u_b(AC, B2);279const uint32_t out = __msa_copy_s_w((v4i32)R, 0);280SW4(out, out, out, out, dst, BPS);281}282283// horizontal284static WEBP_INLINE void HE4(uint8_t* WEBP_RESTRICT dst,285const uint8_t* WEBP_RESTRICT top) {286const int X = top[-1];287const int I = top[-2];288const int J = top[-3];289const int K = top[-4];290const int L = top[-5];291WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));292WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));293WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));294WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));295}296297static WEBP_INLINE void DC4(uint8_t* WEBP_RESTRICT dst,298const uint8_t* WEBP_RESTRICT top) {299uint32_t dc = 4;300int i;301for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];302dc >>= 3;303dc = dc | (dc << 8) | (dc << 16) | (dc << 24);304SW4(dc, dc, dc, dc, dst, BPS);305}306307static WEBP_INLINE void RD4(uint8_t* WEBP_RESTRICT dst,308const uint8_t* WEBP_RESTRICT top) {309const v16u8 A2 = { 0 };310const uint64_t val_m = LD(top - 5);311const v16u8 A1 = (v16u8)__msa_insert_d((v2i64)A2, 0, val_m);312const v16u8 A = (v16u8)__msa_insert_b((v16i8)A1, 8, top[3]);313const v16u8 B = SLDI_UB(A, A, 1);314const v16u8 C = SLDI_UB(A, A, 2);315const v16u8 AC = __msa_ave_u_b(A, C);316const v16u8 B2 = __msa_ave_u_b(B, B);317const v16u8 R0 = __msa_aver_u_b(AC, B2);318const v16u8 R1 = SLDI_UB(R0, R0, 1);319const v16u8 R2 = SLDI_UB(R1, R1, 1);320const v16u8 R3 = SLDI_UB(R2, R2, 1);321const uint32_t val0 = __msa_copy_s_w((v4i32)R0, 0);322const uint32_t val1 = __msa_copy_s_w((v4i32)R1, 0);323const uint32_t val2 = __msa_copy_s_w((v4i32)R2, 0);324const uint32_t val3 = __msa_copy_s_w((v4i32)R3, 0);325SW4(val3, val2, val1, val0, dst, BPS);326}327328static WEBP_INLINE void LD4(uint8_t* WEBP_RESTRICT dst,329const uint8_t* WEBP_RESTRICT top) {330const v16u8 A1 = { 0 };331const uint64_t val_m = LD(top);332const v16u8 A = (v16u8)__msa_insert_d((v2i64)A1, 0, val_m);333const v16u8 B = SLDI_UB(A, A, 1);334const v16u8 C1 = SLDI_UB(A, A, 2);335const v16u8 C = (v16u8)__msa_insert_b((v16i8)C1, 6, top[7]);336const v16u8 AC = __msa_ave_u_b(A, C);337const v16u8 B2 = __msa_ave_u_b(B, B);338const v16u8 R0 = __msa_aver_u_b(AC, B2);339const v16u8 R1 = SLDI_UB(R0, R0, 1);340const v16u8 R2 = SLDI_UB(R1, R1, 1);341const v16u8 R3 = SLDI_UB(R2, R2, 1);342const uint32_t val0 = __msa_copy_s_w((v4i32)R0, 0);343const uint32_t val1 = __msa_copy_s_w((v4i32)R1, 0);344const uint32_t val2 = __msa_copy_s_w((v4i32)R2, 0);345const uint32_t val3 = __msa_copy_s_w((v4i32)R3, 0);346SW4(val0, val1, val2, val3, dst, BPS);347}348349static WEBP_INLINE void VR4(uint8_t* WEBP_RESTRICT dst,350const uint8_t* WEBP_RESTRICT top) {351const int X = top[-1];352const int I = top[-2];353const int J = top[-3];354const int K = top[-4];355const int A = top[0];356const int B = top[1];357const int C = top[2];358const int D = top[3];359DST(0, 0) = DST(1, 2) = AVG2(X, A);360DST(1, 0) = DST(2, 2) = AVG2(A, B);361DST(2, 0) = DST(3, 2) = AVG2(B, C);362DST(3, 0) = AVG2(C, D);363DST(0, 3) = AVG3(K, J, I);364DST(0, 2) = AVG3(J, I, X);365DST(0, 1) = DST(1, 3) = AVG3(I, X, A);366DST(1, 1) = DST(2, 3) = AVG3(X, A, B);367DST(2, 1) = DST(3, 3) = AVG3(A, B, C);368DST(3, 1) = AVG3(B, C, D);369}370371static WEBP_INLINE void VL4(uint8_t* WEBP_RESTRICT dst,372const uint8_t* WEBP_RESTRICT top) {373const int A = top[0];374const int B = top[1];375const int C = top[2];376const int D = top[3];377const int E = top[4];378const int F = top[5];379const int G = top[6];380const int H = top[7];381DST(0, 0) = AVG2(A, B);382DST(1, 0) = DST(0, 2) = AVG2(B, C);383DST(2, 0) = DST(1, 2) = AVG2(C, D);384DST(3, 0) = DST(2, 2) = AVG2(D, E);385DST(0, 1) = AVG3(A, B, C);386DST(1, 1) = DST(0, 3) = AVG3(B, C, D);387DST(2, 1) = DST(1, 3) = AVG3(C, D, E);388DST(3, 1) = DST(2, 3) = AVG3(D, E, F);389DST(3, 2) = AVG3(E, F, G);390DST(3, 3) = AVG3(F, G, H);391}392393static WEBP_INLINE void HU4(uint8_t* WEBP_RESTRICT dst,394const uint8_t* WEBP_RESTRICT top) {395const int I = top[-2];396const int J = top[-3];397const int K = top[-4];398const int L = top[-5];399DST(0, 0) = AVG2(I, J);400DST(2, 0) = DST(0, 1) = AVG2(J, K);401DST(2, 1) = DST(0, 2) = AVG2(K, L);402DST(1, 0) = AVG3(I, J, K);403DST(3, 0) = DST(1, 1) = AVG3(J, K, L);404DST(3, 1) = DST(1, 2) = AVG3(K, L, L);405DST(3, 2) = DST(2, 2) =406DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;407}408409static WEBP_INLINE void HD4(uint8_t* WEBP_RESTRICT dst,410const uint8_t* WEBP_RESTRICT top) {411const int X = top[-1];412const int I = top[-2];413const int J = top[-3];414const int K = top[-4];415const int L = top[-5];416const int A = top[0];417const int B = top[1];418const int C = top[2];419DST(0, 0) = DST(2, 1) = AVG2(I, X);420DST(0, 1) = DST(2, 2) = AVG2(J, I);421DST(0, 2) = DST(2, 3) = AVG2(K, J);422DST(0, 3) = AVG2(L, K);423DST(3, 0) = AVG3(A, B, C);424DST(2, 0) = AVG3(X, A, B);425DST(1, 0) = DST(3, 1) = AVG3(I, X, A);426DST(1, 1) = DST(3, 2) = AVG3(J, I, X);427DST(1, 2) = DST(3, 3) = AVG3(K, J, I);428DST(1, 3) = AVG3(L, K, J);429}430431static WEBP_INLINE void TM4(uint8_t* WEBP_RESTRICT dst,432const uint8_t* WEBP_RESTRICT top) {433const v16i8 zero = { 0 };434const v8i16 TL = (v8i16)__msa_fill_h(top[-1]);435const v8i16 L0 = (v8i16)__msa_fill_h(top[-2]);436const v8i16 L1 = (v8i16)__msa_fill_h(top[-3]);437const v8i16 L2 = (v8i16)__msa_fill_h(top[-4]);438const v8i16 L3 = (v8i16)__msa_fill_h(top[-5]);439const v16u8 T1 = LD_UB(top);440const v8i16 T = (v8i16)__msa_ilvr_b(zero, (v16i8)T1);441const v8i16 d = T - TL;442v8i16 r0, r1, r2, r3;443ADD4(d, L0, d, L1, d, L2, d, L3, r0, r1, r2, r3);444CLIP_SH4_0_255(r0, r1, r2, r3);445PCKEV_ST4x4_UB(r0, r1, r2, r3, dst, BPS);446}447448#undef DST449#undef AVG3450#undef AVG2451452static void Intra4Preds_MSA(uint8_t* WEBP_RESTRICT dst,453const uint8_t* WEBP_RESTRICT top) {454DC4(I4DC4 + dst, top);455TM4(I4TM4 + dst, top);456VE4(I4VE4 + dst, top);457HE4(I4HE4 + dst, top);458RD4(I4RD4 + dst, top);459VR4(I4VR4 + dst, top);460LD4(I4LD4 + dst, top);461VL4(I4VL4 + dst, top);462HD4(I4HD4 + dst, top);463HU4(I4HU4 + dst, top);464}465466// luma 16x16 prediction467468#define STORE16x16(out, dst) do { \469ST_UB8(out, out, out, out, out, out, out, out, dst + 0 * BPS, BPS); \470ST_UB8(out, out, out, out, out, out, out, out, dst + 8 * BPS, BPS); \471} while (0)472473static WEBP_INLINE void VerticalPred16x16(uint8_t* WEBP_RESTRICT dst,474const uint8_t* WEBP_RESTRICT top) {475if (top != NULL) {476const v16u8 out = LD_UB(top);477STORE16x16(out, dst);478} else {479const v16u8 out = (v16u8)__msa_fill_b(0x7f);480STORE16x16(out, dst);481}482}483484static WEBP_INLINE void HorizontalPred16x16(uint8_t* WEBP_RESTRICT dst,485const uint8_t* WEBP_RESTRICT left) {486if (left != NULL) {487int j;488for (j = 0; j < 16; j += 4) {489const v16u8 L0 = (v16u8)__msa_fill_b(left[0]);490const v16u8 L1 = (v16u8)__msa_fill_b(left[1]);491const v16u8 L2 = (v16u8)__msa_fill_b(left[2]);492const v16u8 L3 = (v16u8)__msa_fill_b(left[3]);493ST_UB4(L0, L1, L2, L3, dst, BPS);494dst += 4 * BPS;495left += 4;496}497} else {498const v16u8 out = (v16u8)__msa_fill_b(0x81);499STORE16x16(out, dst);500}501}502503static WEBP_INLINE void TrueMotion16x16(uint8_t* WEBP_RESTRICT dst,504const uint8_t* WEBP_RESTRICT left,505const uint8_t* WEBP_RESTRICT top) {506if (left != NULL) {507if (top != NULL) {508int j;509v8i16 d1, d2;510const v16i8 zero = { 0 };511const v8i16 TL = (v8i16)__msa_fill_h(left[-1]);512const v16u8 T = LD_UB(top);513ILVRL_B2_SH(zero, T, d1, d2);514SUB2(d1, TL, d2, TL, d1, d2);515for (j = 0; j < 16; j += 4) {516v16i8 t0, t1, t2, t3;517v8i16 r0, r1, r2, r3, r4, r5, r6, r7;518const v8i16 L0 = (v8i16)__msa_fill_h(left[j + 0]);519const v8i16 L1 = (v8i16)__msa_fill_h(left[j + 1]);520const v8i16 L2 = (v8i16)__msa_fill_h(left[j + 2]);521const v8i16 L3 = (v8i16)__msa_fill_h(left[j + 3]);522ADD4(d1, L0, d1, L1, d1, L2, d1, L3, r0, r1, r2, r3);523ADD4(d2, L0, d2, L1, d2, L2, d2, L3, r4, r5, r6, r7);524CLIP_SH4_0_255(r0, r1, r2, r3);525CLIP_SH4_0_255(r4, r5, r6, r7);526PCKEV_B4_SB(r4, r0, r5, r1, r6, r2, r7, r3, t0, t1, t2, t3);527ST_SB4(t0, t1, t2, t3, dst, BPS);528dst += 4 * BPS;529}530} else {531HorizontalPred16x16(dst, left);532}533} else {534if (top != NULL) {535VerticalPred16x16(dst, top);536} else {537const v16u8 out = (v16u8)__msa_fill_b(0x81);538STORE16x16(out, dst);539}540}541}542543static WEBP_INLINE void DCMode16x16(uint8_t* WEBP_RESTRICT dst,544const uint8_t* WEBP_RESTRICT left,545const uint8_t* WEBP_RESTRICT top) {546int DC;547v16u8 out;548if (top != NULL && left != NULL) {549const v16u8 rtop = LD_UB(top);550const v8u16 dctop = __msa_hadd_u_h(rtop, rtop);551const v16u8 rleft = LD_UB(left);552const v8u16 dcleft = __msa_hadd_u_h(rleft, rleft);553const v8u16 dctemp = dctop + dcleft;554DC = HADD_UH_U32(dctemp);555DC = (DC + 16) >> 5;556} else if (left != NULL) { // left but no top557const v16u8 rleft = LD_UB(left);558const v8u16 dcleft = __msa_hadd_u_h(rleft, rleft);559DC = HADD_UH_U32(dcleft);560DC = (DC + DC + 16) >> 5;561} else if (top != NULL) { // top but no left562const v16u8 rtop = LD_UB(top);563const v8u16 dctop = __msa_hadd_u_h(rtop, rtop);564DC = HADD_UH_U32(dctop);565DC = (DC + DC + 16) >> 5;566} else { // no top, no left, nothing.567DC = 0x80;568}569out = (v16u8)__msa_fill_b(DC);570STORE16x16(out, dst);571}572573static void Intra16Preds_MSA(uint8_t* WEBP_RESTRICT dst,574const uint8_t* WEBP_RESTRICT left,575const uint8_t* WEBP_RESTRICT top) {576DCMode16x16(I16DC16 + dst, left, top);577VerticalPred16x16(I16VE16 + dst, top);578HorizontalPred16x16(I16HE16 + dst, left);579TrueMotion16x16(I16TM16 + dst, left, top);580}581582// Chroma 8x8 prediction583584#define CALC_DC8(in, out) do { \585const v8u16 temp0 = __msa_hadd_u_h(in, in); \586const v4u32 temp1 = __msa_hadd_u_w(temp0, temp0); \587const v2i64 temp2 = (v2i64)__msa_hadd_u_d(temp1, temp1); \588const v2i64 temp3 = __msa_splati_d(temp2, 1); \589const v2i64 temp4 = temp3 + temp2; \590const v16i8 temp5 = (v16i8)__msa_srari_d(temp4, 4); \591const v2i64 temp6 = (v2i64)__msa_splati_b(temp5, 0); \592out = __msa_copy_s_d(temp6, 0); \593} while (0)594595#define STORE8x8(out, dst) do { \596SD4(out, out, out, out, dst + 0 * BPS, BPS); \597SD4(out, out, out, out, dst + 4 * BPS, BPS); \598} while (0)599600static WEBP_INLINE void VerticalPred8x8(uint8_t* WEBP_RESTRICT dst,601const uint8_t* WEBP_RESTRICT top) {602if (top != NULL) {603const uint64_t out = LD(top);604STORE8x8(out, dst);605} else {606const uint64_t out = 0x7f7f7f7f7f7f7f7fULL;607STORE8x8(out, dst);608}609}610611static WEBP_INLINE void HorizontalPred8x8(uint8_t* WEBP_RESTRICT dst,612const uint8_t* WEBP_RESTRICT left) {613if (left != NULL) {614int j;615for (j = 0; j < 8; j += 4) {616const v16u8 L0 = (v16u8)__msa_fill_b(left[0]);617const v16u8 L1 = (v16u8)__msa_fill_b(left[1]);618const v16u8 L2 = (v16u8)__msa_fill_b(left[2]);619const v16u8 L3 = (v16u8)__msa_fill_b(left[3]);620const uint64_t out0 = __msa_copy_s_d((v2i64)L0, 0);621const uint64_t out1 = __msa_copy_s_d((v2i64)L1, 0);622const uint64_t out2 = __msa_copy_s_d((v2i64)L2, 0);623const uint64_t out3 = __msa_copy_s_d((v2i64)L3, 0);624SD4(out0, out1, out2, out3, dst, BPS);625dst += 4 * BPS;626left += 4;627}628} else {629const uint64_t out = 0x8181818181818181ULL;630STORE8x8(out, dst);631}632}633634static WEBP_INLINE void TrueMotion8x8(uint8_t* WEBP_RESTRICT dst,635const uint8_t* WEBP_RESTRICT left,636const uint8_t* WEBP_RESTRICT top) {637if (left != NULL) {638if (top != NULL) {639int j;640const v8i16 TL = (v8i16)__msa_fill_h(left[-1]);641const v16u8 T1 = LD_UB(top);642const v16i8 zero = { 0 };643const v8i16 T = (v8i16)__msa_ilvr_b(zero, (v16i8)T1);644const v8i16 d = T - TL;645for (j = 0; j < 8; j += 4) {646uint64_t out0, out1, out2, out3;647v16i8 t0, t1;648v8i16 r0 = (v8i16)__msa_fill_h(left[j + 0]);649v8i16 r1 = (v8i16)__msa_fill_h(left[j + 1]);650v8i16 r2 = (v8i16)__msa_fill_h(left[j + 2]);651v8i16 r3 = (v8i16)__msa_fill_h(left[j + 3]);652ADD4(d, r0, d, r1, d, r2, d, r3, r0, r1, r2, r3);653CLIP_SH4_0_255(r0, r1, r2, r3);654PCKEV_B2_SB(r1, r0, r3, r2, t0, t1);655out0 = __msa_copy_s_d((v2i64)t0, 0);656out1 = __msa_copy_s_d((v2i64)t0, 1);657out2 = __msa_copy_s_d((v2i64)t1, 0);658out3 = __msa_copy_s_d((v2i64)t1, 1);659SD4(out0, out1, out2, out3, dst, BPS);660dst += 4 * BPS;661}662} else {663HorizontalPred8x8(dst, left);664}665} else {666if (top != NULL) {667VerticalPred8x8(dst, top);668} else {669const uint64_t out = 0x8181818181818181ULL;670STORE8x8(out, dst);671}672}673}674675static WEBP_INLINE void DCMode8x8(uint8_t* WEBP_RESTRICT dst,676const uint8_t* WEBP_RESTRICT left,677const uint8_t* WEBP_RESTRICT top) {678uint64_t out;679v16u8 src = { 0 };680if (top != NULL && left != NULL) {681const uint64_t left_m = LD(left);682const uint64_t top_m = LD(top);683INSERT_D2_UB(left_m, top_m, src);684CALC_DC8(src, out);685} else if (left != NULL) { // left but no top686const uint64_t left_m = LD(left);687INSERT_D2_UB(left_m, left_m, src);688CALC_DC8(src, out);689} else if (top != NULL) { // top but no left690const uint64_t top_m = LD(top);691INSERT_D2_UB(top_m, top_m, src);692CALC_DC8(src, out);693} else { // no top, no left, nothing.694src = (v16u8)__msa_fill_b(0x80);695out = __msa_copy_s_d((v2i64)src, 0);696}697STORE8x8(out, dst);698}699700static void IntraChromaPreds_MSA(uint8_t* WEBP_RESTRICT dst,701const uint8_t* WEBP_RESTRICT left,702const uint8_t* WEBP_RESTRICT top) {703// U block704DCMode8x8(C8DC8 + dst, left, top);705VerticalPred8x8(C8VE8 + dst, top);706HorizontalPred8x8(C8HE8 + dst, left);707TrueMotion8x8(C8TM8 + dst, left, top);708// V block709dst += 8;710if (top != NULL) top += 8;711if (left != NULL) left += 16;712DCMode8x8(C8DC8 + dst, left, top);713VerticalPred8x8(C8VE8 + dst, top);714HorizontalPred8x8(C8HE8 + dst, left);715TrueMotion8x8(C8TM8 + dst, left, top);716}717718//------------------------------------------------------------------------------719// Metric720721#define PACK_DOTP_UB4_SW(in0, in1, in2, in3, out0, out1, out2, out3) do { \722v16u8 tmp0, tmp1; \723v8i16 tmp2, tmp3; \724ILVRL_B2_UB(in0, in1, tmp0, tmp1); \725HSUB_UB2_SH(tmp0, tmp1, tmp2, tmp3); \726DOTP_SH2_SW(tmp2, tmp3, tmp2, tmp3, out0, out1); \727ILVRL_B2_UB(in2, in3, tmp0, tmp1); \728HSUB_UB2_SH(tmp0, tmp1, tmp2, tmp3); \729DOTP_SH2_SW(tmp2, tmp3, tmp2, tmp3, out2, out3); \730} while (0)731732#define PACK_DPADD_UB4_SW(in0, in1, in2, in3, out0, out1, out2, out3) do { \733v16u8 tmp0, tmp1; \734v8i16 tmp2, tmp3; \735ILVRL_B2_UB(in0, in1, tmp0, tmp1); \736HSUB_UB2_SH(tmp0, tmp1, tmp2, tmp3); \737DPADD_SH2_SW(tmp2, tmp3, tmp2, tmp3, out0, out1); \738ILVRL_B2_UB(in2, in3, tmp0, tmp1); \739HSUB_UB2_SH(tmp0, tmp1, tmp2, tmp3); \740DPADD_SH2_SW(tmp2, tmp3, tmp2, tmp3, out2, out3); \741} while (0)742743static int SSE16x16_MSA(const uint8_t* WEBP_RESTRICT a,744const uint8_t* WEBP_RESTRICT b) {745uint32_t sum;746v16u8 src0, src1, src2, src3, src4, src5, src6, src7;747v16u8 ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7;748v4i32 out0, out1, out2, out3;749750LD_UB8(a, BPS, src0, src1, src2, src3, src4, src5, src6, src7);751LD_UB8(b, BPS, ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7);752PACK_DOTP_UB4_SW(src0, ref0, src1, ref1, out0, out1, out2, out3);753PACK_DPADD_UB4_SW(src2, ref2, src3, ref3, out0, out1, out2, out3);754PACK_DPADD_UB4_SW(src4, ref4, src5, ref5, out0, out1, out2, out3);755PACK_DPADD_UB4_SW(src6, ref6, src7, ref7, out0, out1, out2, out3);756a += 8 * BPS;757b += 8 * BPS;758LD_UB8(a, BPS, src0, src1, src2, src3, src4, src5, src6, src7);759LD_UB8(b, BPS, ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7);760PACK_DPADD_UB4_SW(src0, ref0, src1, ref1, out0, out1, out2, out3);761PACK_DPADD_UB4_SW(src2, ref2, src3, ref3, out0, out1, out2, out3);762PACK_DPADD_UB4_SW(src4, ref4, src5, ref5, out0, out1, out2, out3);763PACK_DPADD_UB4_SW(src6, ref6, src7, ref7, out0, out1, out2, out3);764out0 += out1;765out2 += out3;766out0 += out2;767sum = HADD_SW_S32(out0);768return sum;769}770771static int SSE16x8_MSA(const uint8_t* WEBP_RESTRICT a,772const uint8_t* WEBP_RESTRICT b) {773uint32_t sum;774v16u8 src0, src1, src2, src3, src4, src5, src6, src7;775v16u8 ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7;776v4i32 out0, out1, out2, out3;777778LD_UB8(a, BPS, src0, src1, src2, src3, src4, src5, src6, src7);779LD_UB8(b, BPS, ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7);780PACK_DOTP_UB4_SW(src0, ref0, src1, ref1, out0, out1, out2, out3);781PACK_DPADD_UB4_SW(src2, ref2, src3, ref3, out0, out1, out2, out3);782PACK_DPADD_UB4_SW(src4, ref4, src5, ref5, out0, out1, out2, out3);783PACK_DPADD_UB4_SW(src6, ref6, src7, ref7, out0, out1, out2, out3);784out0 += out1;785out2 += out3;786out0 += out2;787sum = HADD_SW_S32(out0);788return sum;789}790791static int SSE8x8_MSA(const uint8_t* WEBP_RESTRICT a,792const uint8_t* WEBP_RESTRICT b) {793uint32_t sum;794v16u8 src0, src1, src2, src3, src4, src5, src6, src7;795v16u8 ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7;796v16u8 t0, t1, t2, t3;797v4i32 out0, out1, out2, out3;798799LD_UB8(a, BPS, src0, src1, src2, src3, src4, src5, src6, src7);800LD_UB8(b, BPS, ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7);801ILVR_B4_UB(src0, src1, src2, src3, ref0, ref1, ref2, ref3, t0, t1, t2, t3);802PACK_DOTP_UB4_SW(t0, t2, t1, t3, out0, out1, out2, out3);803ILVR_B4_UB(src4, src5, src6, src7, ref4, ref5, ref6, ref7, t0, t1, t2, t3);804PACK_DPADD_UB4_SW(t0, t2, t1, t3, out0, out1, out2, out3);805out0 += out1;806out2 += out3;807out0 += out2;808sum = HADD_SW_S32(out0);809return sum;810}811812static int SSE4x4_MSA(const uint8_t* WEBP_RESTRICT a,813const uint8_t* WEBP_RESTRICT b) {814uint32_t sum = 0;815uint32_t src0, src1, src2, src3, ref0, ref1, ref2, ref3;816v16u8 src = { 0 }, ref = { 0 }, tmp0, tmp1;817v8i16 diff0, diff1;818v4i32 out0, out1;819820LW4(a, BPS, src0, src1, src2, src3);821LW4(b, BPS, ref0, ref1, ref2, ref3);822INSERT_W4_UB(src0, src1, src2, src3, src);823INSERT_W4_UB(ref0, ref1, ref2, ref3, ref);824ILVRL_B2_UB(src, ref, tmp0, tmp1);825HSUB_UB2_SH(tmp0, tmp1, diff0, diff1);826DOTP_SH2_SW(diff0, diff1, diff0, diff1, out0, out1);827out0 += out1;828sum = HADD_SW_S32(out0);829return sum;830}831832//------------------------------------------------------------------------------833// Quantization834835static int QuantizeBlock_MSA(int16_t in[16], int16_t out[16],836const VP8Matrix* WEBP_RESTRICT const mtx) {837int sum;838v8i16 in0, in1, sh0, sh1, out0, out1;839v8i16 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, sign0, sign1;840v4i32 s0, s1, s2, s3, b0, b1, b2, b3, t0, t1, t2, t3;841const v8i16 zero = { 0 };842const v8i16 zigzag0 = { 0, 1, 4, 8, 5, 2, 3, 6 };843const v8i16 zigzag1 = { 9, 12, 13, 10, 7, 11, 14, 15 };844const v8i16 maxlevel = __msa_fill_h(MAX_LEVEL);845846LD_SH2(&in[0], 8, in0, in1);847LD_SH2(&mtx->sharpen_[0], 8, sh0, sh1);848tmp4 = __msa_add_a_h(in0, zero);849tmp5 = __msa_add_a_h(in1, zero);850ILVRL_H2_SH(sh0, tmp4, tmp0, tmp1);851ILVRL_H2_SH(sh1, tmp5, tmp2, tmp3);852HADD_SH4_SW(tmp0, tmp1, tmp2, tmp3, s0, s1, s2, s3);853sign0 = (in0 < zero);854sign1 = (in1 < zero); // sign855LD_SH2(&mtx->iq_[0], 8, tmp0, tmp1); // iq856ILVRL_H2_SW(zero, tmp0, t0, t1);857ILVRL_H2_SW(zero, tmp1, t2, t3);858LD_SW4(&mtx->bias_[0], 4, b0, b1, b2, b3); // bias859MUL4(t0, s0, t1, s1, t2, s2, t3, s3, t0, t1, t2, t3);860ADD4(b0, t0, b1, t1, b2, t2, b3, t3, b0, b1, b2, b3);861SRAI_W4_SW(b0, b1, b2, b3, 17);862PCKEV_H2_SH(b1, b0, b3, b2, tmp2, tmp3);863tmp0 = (tmp2 > maxlevel);864tmp1 = (tmp3 > maxlevel);865tmp2 = (v8i16)__msa_bmnz_v((v16u8)tmp2, (v16u8)maxlevel, (v16u8)tmp0);866tmp3 = (v8i16)__msa_bmnz_v((v16u8)tmp3, (v16u8)maxlevel, (v16u8)tmp1);867SUB2(zero, tmp2, zero, tmp3, tmp0, tmp1);868tmp2 = (v8i16)__msa_bmnz_v((v16u8)tmp2, (v16u8)tmp0, (v16u8)sign0);869tmp3 = (v8i16)__msa_bmnz_v((v16u8)tmp3, (v16u8)tmp1, (v16u8)sign1);870LD_SW4(&mtx->zthresh_[0], 4, t0, t1, t2, t3); // zthresh871t0 = (s0 > t0);872t1 = (s1 > t1);873t2 = (s2 > t2);874t3 = (s3 > t3);875PCKEV_H2_SH(t1, t0, t3, t2, tmp0, tmp1);876tmp4 = (v8i16)__msa_bmnz_v((v16u8)zero, (v16u8)tmp2, (v16u8)tmp0);877tmp5 = (v8i16)__msa_bmnz_v((v16u8)zero, (v16u8)tmp3, (v16u8)tmp1);878LD_SH2(&mtx->q_[0], 8, tmp0, tmp1);879MUL2(tmp4, tmp0, tmp5, tmp1, in0, in1);880VSHF_H2_SH(tmp4, tmp5, tmp4, tmp5, zigzag0, zigzag1, out0, out1);881ST_SH2(in0, in1, &in[0], 8);882ST_SH2(out0, out1, &out[0], 8);883out0 = __msa_add_a_h(out0, out1);884sum = HADD_SH_S32(out0);885return (sum > 0);886}887888static int Quantize2Blocks_MSA(int16_t in[32], int16_t out[32],889const VP8Matrix* WEBP_RESTRICT const mtx) {890int nz;891nz = VP8EncQuantizeBlock(in + 0 * 16, out + 0 * 16, mtx) << 0;892nz |= VP8EncQuantizeBlock(in + 1 * 16, out + 1 * 16, mtx) << 1;893return nz;894}895896//------------------------------------------------------------------------------897// Entry point898899extern void VP8EncDspInitMSA(void);900901WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitMSA(void) {902VP8ITransform = ITransform_MSA;903VP8FTransform = FTransform_MSA;904VP8FTransformWHT = FTransformWHT_MSA;905906VP8TDisto4x4 = Disto4x4_MSA;907VP8TDisto16x16 = Disto16x16_MSA;908VP8CollectHistogram = CollectHistogram_MSA;909910VP8EncPredLuma4 = Intra4Preds_MSA;911VP8EncPredLuma16 = Intra16Preds_MSA;912VP8EncPredChroma8 = IntraChromaPreds_MSA;913914VP8SSE16x16 = SSE16x16_MSA;915VP8SSE16x8 = SSE16x8_MSA;916VP8SSE8x8 = SSE8x8_MSA;917VP8SSE4x4 = SSE4x4_MSA;918919VP8EncQuantizeBlock = QuantizeBlock_MSA;920VP8EncQuantize2Blocks = Quantize2Blocks_MSA;921VP8EncQuantizeBlockWHT = QuantizeBlock_MSA;922}923924#else // !WEBP_USE_MSA925926WEBP_DSP_INIT_STUB(VP8EncDspInitMSA)927928#endif // WEBP_USE_MSA929930931