1// Copyright 2016 Google Inc. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8// -----------------------------------------------------------------------------
9//
10// MSA version of dsp functions
11//
12// Author(s):  Prashant Patil   (prashant.patil@imgtec.com)
13
14
15#include "./dsp.h"
16
17#if defined(WEBP_USE_MSA)
18
19#include "./msa_macro.h"
20
21//------------------------------------------------------------------------------
22// Transforms
23
24#define IDCT_1D_W(in0, in1, in2, in3, out0, out1, out2, out3) {  \
25  v4i32 a1_m, b1_m, c1_m, d1_m;                                  \
26  v4i32 c_tmp1_m, c_tmp2_m, d_tmp1_m, d_tmp2_m;                  \
27  const v4i32 cospi8sqrt2minus1 = __msa_fill_w(20091);           \
28  const v4i32 sinpi8sqrt2 = __msa_fill_w(35468);                 \
29                                                                 \
30  a1_m = in0 + in2;                                              \
31  b1_m = in0 - in2;                                              \
32  c_tmp1_m = (in1 * sinpi8sqrt2) >> 16;                          \
33  c_tmp2_m = in3 + ((in3 * cospi8sqrt2minus1) >> 16);            \
34  c1_m = c_tmp1_m - c_tmp2_m;                                    \
35  d_tmp1_m = in1 + ((in1 * cospi8sqrt2minus1) >> 16);            \
36  d_tmp2_m = (in3 * sinpi8sqrt2) >> 16;                          \
37  d1_m = d_tmp1_m + d_tmp2_m;                                    \
38  BUTTERFLY_4(a1_m, b1_m, c1_m, d1_m, out0, out1, out2, out3);   \
39}
40#define MULT1(a) ((((a) * 20091) >> 16) + (a))
41#define MULT2(a) (((a) * 35468) >> 16)
42
43static void TransformOne(const int16_t* in, uint8_t* dst) {
44  v8i16 input0, input1;
45  v4i32 in0, in1, in2, in3, hz0, hz1, hz2, hz3, vt0, vt1, vt2, vt3;
46  v4i32 res0, res1, res2, res3;
47  const v16i8 zero = { 0 };
48  v16i8 dest0, dest1, dest2, dest3;
49
50  LD_SH2(in, 8, input0, input1);
51  UNPCK_SH_SW(input0, in0, in1);
52  UNPCK_SH_SW(input1, in2, in3);
53  IDCT_1D_W(in0, in1, in2, in3, hz0, hz1, hz2, hz3);
54  TRANSPOSE4x4_SW_SW(hz0, hz1, hz2, hz3, hz0, hz1, hz2, hz3);
55  IDCT_1D_W(hz0, hz1, hz2, hz3, vt0, vt1, vt2, vt3);
56  SRARI_W4_SW(vt0, vt1, vt2, vt3, 3);
57  TRANSPOSE4x4_SW_SW(vt0, vt1, vt2, vt3, vt0, vt1, vt2, vt3);
58  LD_SB4(dst, BPS, dest0, dest1, dest2, dest3);
59  ILVR_B4_SW(zero, dest0, zero, dest1, zero, dest2, zero, dest3,
60             res0, res1, res2, res3);
61  ILVR_H4_SW(zero, res0, zero, res1, zero, res2, zero, res3,
62             res0, res1, res2, res3);
63  ADD4(res0, vt0, res1, vt1, res2, vt2, res3, vt3, res0, res1, res2, res3);
64  CLIP_SW4_0_255(res0, res1, res2, res3);
65  PCKEV_B2_SW(res0, res1, res2, res3, vt0, vt1);
66  res0 = (v4i32)__msa_pckev_b((v16i8)vt0, (v16i8)vt1);
67  ST4x4_UB(res0, res0, 3, 2, 1, 0, dst, BPS);
68}
69
70static void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
71  TransformOne(in, dst);
72  if (do_two) {
73    TransformOne(in + 16, dst + 4);
74  }
75}
76
77static void TransformWHT(const int16_t* in, int16_t* out) {
78  v8i16 input0, input1;
79  const v8i16 mask0 = { 0, 1, 2, 3, 8, 9, 10, 11 };
80  const v8i16 mask1 = { 4, 5, 6, 7, 12, 13, 14, 15 };
81  const v8i16 mask2 = { 0, 4, 8, 12, 1, 5, 9, 13 };
82  const v8i16 mask3 = { 3, 7, 11, 15, 2, 6, 10, 14 };
83  v8i16 tmp0, tmp1, tmp2, tmp3;
84  v8i16 out0, out1;
85
86  LD_SH2(in, 8, input0, input1);
87  input1 = SLDI_SH(input1, input1, 8);
88  tmp0 = input0 + input1;
89  tmp1 = input0 - input1;
90  VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask0, mask1, tmp2, tmp3);
91  out0 = tmp2 + tmp3;
92  out1 = tmp2 - tmp3;
93  VSHF_H2_SH(out0, out1, out0, out1, mask2, mask3, input0, input1);
94  tmp0 = input0 + input1;
95  tmp1 = input0 - input1;
96  VSHF_H2_SH(tmp0, tmp1, tmp0, tmp1, mask0, mask1, tmp2, tmp3);
97  tmp0 = tmp2 + tmp3;
98  tmp1 = tmp2 - tmp3;
99  ADDVI_H2_SH(tmp0, 3, tmp1, 3, out0, out1);
100  SRAI_H2_SH(out0, out1, 3);
101  out[0] = __msa_copy_s_h(out0, 0);
102  out[16] = __msa_copy_s_h(out0, 4);
103  out[32] = __msa_copy_s_h(out1, 0);
104  out[48] = __msa_copy_s_h(out1, 4);
105  out[64] = __msa_copy_s_h(out0, 1);
106  out[80] = __msa_copy_s_h(out0, 5);
107  out[96] = __msa_copy_s_h(out1, 1);
108  out[112] = __msa_copy_s_h(out1, 5);
109  out[128] = __msa_copy_s_h(out0, 2);
110  out[144] = __msa_copy_s_h(out0, 6);
111  out[160] = __msa_copy_s_h(out1, 2);
112  out[176] = __msa_copy_s_h(out1, 6);
113  out[192] = __msa_copy_s_h(out0, 3);
114  out[208] = __msa_copy_s_h(out0, 7);
115  out[224] = __msa_copy_s_h(out1, 3);
116  out[240] = __msa_copy_s_h(out1, 7);
117}
118
119static void TransformDC(const int16_t* in, uint8_t* dst) {
120  const int DC = (in[0] + 4) >> 3;
121  const v8i16 tmp0 = __msa_fill_h(DC);
122  ADDBLK_ST4x4_UB(tmp0, tmp0, tmp0, tmp0, dst, BPS);
123}
124
125static void TransformAC3(const int16_t* in, uint8_t* dst) {
126  const int a = in[0] + 4;
127  const int c4 = MULT2(in[4]);
128  const int d4 = MULT1(in[4]);
129  const int in2 = MULT2(in[1]);
130  const int in3 = MULT1(in[1]);
131  v4i32 tmp0 = { 0 };
132  v4i32 out0 = __msa_fill_w(a + d4);
133  v4i32 out1 = __msa_fill_w(a + c4);
134  v4i32 out2 = __msa_fill_w(a - c4);
135  v4i32 out3 = __msa_fill_w(a - d4);
136  v4i32 res0, res1, res2, res3;
137  const v4i32 zero = { 0 };
138  v16u8 dest0, dest1, dest2, dest3;
139
140  INSERT_W4_SW(in3, in2, -in2, -in3, tmp0);
141  ADD4(out0, tmp0, out1, tmp0, out2, tmp0, out3, tmp0,
142       out0, out1, out2, out3);
143  SRAI_W4_SW(out0, out1, out2, out3, 3);
144  LD_UB4(dst, BPS, dest0, dest1, dest2, dest3);
145  ILVR_B4_SW(zero, dest0, zero, dest1, zero, dest2, zero, dest3,
146             res0, res1, res2, res3);
147  ILVR_H4_SW(zero, res0, zero, res1, zero, res2, zero, res3,
148             res0, res1, res2, res3);
149  ADD4(res0, out0, res1, out1, res2, out2, res3, out3, res0, res1, res2, res3);
150  CLIP_SW4_0_255(res0, res1, res2, res3);
151  PCKEV_B2_SW(res0, res1, res2, res3, out0, out1);
152  res0 = (v4i32)__msa_pckev_b((v16i8)out0, (v16i8)out1);
153  ST4x4_UB(res0, res0, 3, 2, 1, 0, dst, BPS);
154}
155
156//------------------------------------------------------------------------------
157// Edge filtering functions
158
159#define FLIP_SIGN2(in0, in1, out0, out1) {  \
160  out0 = (v16i8)__msa_xori_b(in0, 0x80);    \
161  out1 = (v16i8)__msa_xori_b(in1, 0x80);    \
162}
163
164#define FLIP_SIGN4(in0, in1, in2, in3, out0, out1, out2, out3) {  \
165  FLIP_SIGN2(in0, in1, out0, out1);                               \
166  FLIP_SIGN2(in2, in3, out2, out3);                               \
167}
168
169#define FILT_VAL(q0_m, p0_m, mask, filt) do {  \
170  v16i8 q0_sub_p0;                             \
171  q0_sub_p0 = __msa_subs_s_b(q0_m, p0_m);      \
172  filt = __msa_adds_s_b(filt, q0_sub_p0);      \
173  filt = __msa_adds_s_b(filt, q0_sub_p0);      \
174  filt = __msa_adds_s_b(filt, q0_sub_p0);      \
175  filt = filt & mask;                          \
176} while (0)
177
178#define FILT2(q_m, p_m, q, p) do {            \
179  u_r = SRAI_H(temp1, 7);                     \
180  u_r = __msa_sat_s_h(u_r, 7);                \
181  u_l = SRAI_H(temp3, 7);                     \
182  u_l = __msa_sat_s_h(u_l, 7);                \
183  u = __msa_pckev_b((v16i8)u_l, (v16i8)u_r);  \
184  q_m = __msa_subs_s_b(q_m, u);               \
185  p_m = __msa_adds_s_b(p_m, u);               \
186  q = __msa_xori_b((v16u8)q_m, 0x80);         \
187  p = __msa_xori_b((v16u8)p_m, 0x80);         \
188} while (0)
189
190#define LPF_FILTER4_4W(p1, p0, q0, q1, mask, hev) do {  \
191  v16i8 p1_m, p0_m, q0_m, q1_m;                         \
192  v16i8 filt, t1, t2;                                   \
193  const v16i8 cnst4b = __msa_ldi_b(4);                  \
194  const v16i8 cnst3b = __msa_ldi_b(3);                  \
195                                                        \
196  FLIP_SIGN4(p1, p0, q0, q1, p1_m, p0_m, q0_m, q1_m);   \
197  filt = __msa_subs_s_b(p1_m, q1_m);                    \
198  filt = filt & hev;                                    \
199  FILT_VAL(q0_m, p0_m, mask, filt);                     \
200  t1 = __msa_adds_s_b(filt, cnst4b);                    \
201  t1 = SRAI_B(t1, 3);                                   \
202  t2 = __msa_adds_s_b(filt, cnst3b);                    \
203  t2 = SRAI_B(t2, 3);                                   \
204  q0_m = __msa_subs_s_b(q0_m, t1);                      \
205  q0 = __msa_xori_b((v16u8)q0_m, 0x80);                 \
206  p0_m = __msa_adds_s_b(p0_m, t2);                      \
207  p0 = __msa_xori_b((v16u8)p0_m, 0x80);                 \
208  filt = __msa_srari_b(t1, 1);                          \
209  hev = __msa_xori_b(hev, 0xff);                        \
210  filt = filt & hev;                                    \
211  q1_m = __msa_subs_s_b(q1_m, filt);                    \
212  q1 = __msa_xori_b((v16u8)q1_m, 0x80);                 \
213  p1_m = __msa_adds_s_b(p1_m, filt);                    \
214  p1 = __msa_xori_b((v16u8)p1_m, 0x80);                 \
215} while (0)
216
217#define LPF_MBFILTER(p2, p1, p0, q0, q1, q2, mask, hev) do {  \
218  v16i8 p2_m, p1_m, p0_m, q2_m, q1_m, q0_m;                   \
219  v16i8 u, filt, t1, t2, filt_sign;                           \
220  v8i16 filt_r, filt_l, u_r, u_l;                             \
221  v8i16 temp0, temp1, temp2, temp3;                           \
222  const v16i8 cnst4b = __msa_ldi_b(4);                        \
223  const v16i8 cnst3b = __msa_ldi_b(3);                        \
224  const v8i16 cnst9h = __msa_ldi_h(9);                        \
225                                                              \
226  FLIP_SIGN4(p1, p0, q0, q1, p1_m, p0_m, q0_m, q1_m);         \
227  filt = __msa_subs_s_b(p1_m, q1_m);                          \
228  FILT_VAL(q0_m, p0_m, mask, filt);                           \
229  FLIP_SIGN2(p2, q2, p2_m, q2_m);                             \
230  t2 = filt & hev;                                            \
231  /* filt_val &= ~hev */                                      \
232  hev = __msa_xori_b(hev, 0xff);                              \
233  filt = filt & hev;                                          \
234  t1 = __msa_adds_s_b(t2, cnst4b);                            \
235  t1 = SRAI_B(t1, 3);                                         \
236  t2 = __msa_adds_s_b(t2, cnst3b);                            \
237  t2 = SRAI_B(t2, 3);                                         \
238  q0_m = __msa_subs_s_b(q0_m, t1);                            \
239  p0_m = __msa_adds_s_b(p0_m, t2);                            \
240  filt_sign = __msa_clti_s_b(filt, 0);                        \
241  ILVRL_B2_SH(filt_sign, filt, filt_r, filt_l);               \
242  /* update q2/p2 */                                          \
243  temp0 = filt_r * cnst9h;                                    \
244  temp1 = ADDVI_H(temp0, 63);                                 \
245  temp2 = filt_l * cnst9h;                                    \
246  temp3 = ADDVI_H(temp2, 63);                                 \
247  FILT2(q2_m, p2_m, q2, p2);                                  \
248  /* update q1/p1 */                                          \
249  temp1 = temp1 + temp0;                                      \
250  temp3 = temp3 + temp2;                                      \
251  FILT2(q1_m, p1_m, q1, p1);                                  \
252  /* update q0/p0 */                                          \
253  temp1 = temp1 + temp0;                                      \
254  temp3 = temp3 + temp2;                                      \
255  FILT2(q0_m, p0_m, q0, p0);                                  \
256} while (0)
257
258#define LPF_MASK_HEV(p3_in, p2_in, p1_in, p0_in,                 \
259                     q0_in, q1_in, q2_in, q3_in,                 \
260                     limit_in, b_limit_in, thresh_in,            \
261                     hev_out, mask_out) do {                     \
262  v16u8 p3_asub_p2_m, p2_asub_p1_m, p1_asub_p0_m, q1_asub_q0_m;  \
263  v16u8 p1_asub_q1_m, p0_asub_q0_m, q3_asub_q2_m, q2_asub_q1_m;  \
264  v16u8 flat_out;                                                \
265                                                                 \
266  /* absolute subtraction of pixel values */                     \
267  p3_asub_p2_m = __msa_asub_u_b(p3_in, p2_in);                   \
268  p2_asub_p1_m = __msa_asub_u_b(p2_in, p1_in);                   \
269  p1_asub_p0_m = __msa_asub_u_b(p1_in, p0_in);                   \
270  q1_asub_q0_m = __msa_asub_u_b(q1_in, q0_in);                   \
271  q2_asub_q1_m = __msa_asub_u_b(q2_in, q1_in);                   \
272  q3_asub_q2_m = __msa_asub_u_b(q3_in, q2_in);                   \
273  p0_asub_q0_m = __msa_asub_u_b(p0_in, q0_in);                   \
274  p1_asub_q1_m = __msa_asub_u_b(p1_in, q1_in);                   \
275  /* calculation of hev */                                       \
276  flat_out = __msa_max_u_b(p1_asub_p0_m, q1_asub_q0_m);          \
277  hev_out = (thresh_in < flat_out);                              \
278  /* calculation of mask */                                      \
279  p0_asub_q0_m = __msa_adds_u_b(p0_asub_q0_m, p0_asub_q0_m);     \
280  p1_asub_q1_m = SRAI_B(p1_asub_q1_m, 1);                        \
281  p0_asub_q0_m = __msa_adds_u_b(p0_asub_q0_m, p1_asub_q1_m);     \
282  mask_out = (b_limit_in < p0_asub_q0_m);                        \
283  mask_out = __msa_max_u_b(flat_out, mask_out);                  \
284  p3_asub_p2_m = __msa_max_u_b(p3_asub_p2_m, p2_asub_p1_m);      \
285  mask_out = __msa_max_u_b(p3_asub_p2_m, mask_out);              \
286  q2_asub_q1_m = __msa_max_u_b(q2_asub_q1_m, q3_asub_q2_m);      \
287  mask_out = __msa_max_u_b(q2_asub_q1_m, mask_out);              \
288  mask_out = (limit_in < mask_out);                              \
289  mask_out = __msa_xori_b(mask_out, 0xff);                       \
290} while (0)
291
292#define ST6x1_UB(in0, in0_idx, in1, in1_idx, pdst, stride) do { \
293  const uint16_t tmp0_h = __msa_copy_s_h((v8i16)in1, in1_idx);  \
294  const uint32_t tmp0_w = __msa_copy_s_w((v4i32)in0, in0_idx);  \
295  SW(tmp0_w, pdst);                                             \
296  SH(tmp0_h, pdst + stride);                                    \
297} while (0)
298
299#define ST6x4_UB(in0, start_in0_idx, in1, start_in1_idx, pdst, stride) do { \
300  uint8_t* ptmp1 = (uint8_t*)pdst;                                          \
301  ST6x1_UB(in0, start_in0_idx, in1, start_in1_idx, ptmp1, 4);               \
302  ptmp1 += stride;                                                          \
303  ST6x1_UB(in0, start_in0_idx + 1, in1, start_in1_idx + 1, ptmp1, 4);       \
304  ptmp1 += stride;                                                          \
305  ST6x1_UB(in0, start_in0_idx + 2, in1, start_in1_idx + 2, ptmp1, 4);       \
306  ptmp1 += stride;                                                          \
307  ST6x1_UB(in0, start_in0_idx + 3, in1, start_in1_idx + 3, ptmp1, 4);       \
308} while (0)
309
310#define LPF_SIMPLE_FILT(p1_in, p0_in, q0_in, q1_in, mask) do {       \
311    v16i8 p1_m, p0_m, q0_m, q1_m, filt, filt1, filt2;                \
312    const v16i8 cnst4b = __msa_ldi_b(4);                             \
313    const v16i8 cnst3b =  __msa_ldi_b(3);                            \
314                                                                     \
315    FLIP_SIGN4(p1_in, p0_in, q0_in, q1_in, p1_m, p0_m, q0_m, q1_m);  \
316    filt = __msa_subs_s_b(p1_m, q1_m);                               \
317    FILT_VAL(q0_m, p0_m, mask, filt);                                \
318    filt1 = __msa_adds_s_b(filt, cnst4b);                            \
319    filt1 = SRAI_B(filt1, 3);                                        \
320    filt2 = __msa_adds_s_b(filt, cnst3b);                            \
321    filt2 = SRAI_B(filt2, 3);                                        \
322    q0_m = __msa_subs_s_b(q0_m, filt1);                              \
323    p0_m = __msa_adds_s_b(p0_m, filt2);                              \
324    q0_in = __msa_xori_b((v16u8)q0_m, 0x80);                         \
325    p0_in = __msa_xori_b((v16u8)p0_m, 0x80);                         \
326} while (0)
327
328#define LPF_SIMPLE_MASK(p1, p0, q0, q1, b_limit, mask) do {    \
329    v16u8 p1_a_sub_q1, p0_a_sub_q0;                            \
330                                                               \
331    p0_a_sub_q0 = __msa_asub_u_b(p0, q0);                      \
332    p1_a_sub_q1 = __msa_asub_u_b(p1, q1);                      \
333    p1_a_sub_q1 = (v16u8)__msa_srli_b((v16i8)p1_a_sub_q1, 1);  \
334    p0_a_sub_q0 = __msa_adds_u_b(p0_a_sub_q0, p0_a_sub_q0);    \
335    mask = __msa_adds_u_b(p0_a_sub_q0, p1_a_sub_q1);           \
336    mask = (mask <= b_limit);                                  \
337} while (0)
338
339static void VFilter16(uint8_t* src, int stride,
340                      int b_limit_in, int limit_in, int thresh_in) {
341  uint8_t* ptemp = src - 4 * stride;
342  v16u8 p3, p2, p1, p0, q3, q2, q1, q0;
343  v16u8 mask, hev;
344  const v16u8 thresh = (v16u8)__msa_fill_b(thresh_in);
345  const v16u8 limit = (v16u8)__msa_fill_b(limit_in);
346  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
347
348  LD_UB8(ptemp, stride, p3, p2, p1, p0, q0, q1, q2, q3);
349  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit, b_limit, thresh,
350               hev, mask);
351  LPF_MBFILTER(p2, p1, p0, q0, q1, q2, mask, hev);
352  ptemp = src - 3 * stride;
353  ST_UB4(p2, p1, p0, q0, ptemp, stride);
354  ptemp += (4 * stride);
355  ST_UB2(q1, q2, ptemp, stride);
356}
357
358static void HFilter16(uint8_t* src, int stride,
359                      int b_limit_in, int limit_in, int thresh_in) {
360  uint8_t* ptmp  = src - 4;
361  v16u8 p3, p2, p1, p0, q3, q2, q1, q0;
362  v16u8 mask, hev;
363  v16u8 row0, row1, row2, row3, row4, row5, row6, row7, row8;
364  v16u8 row9, row10, row11, row12, row13, row14, row15;
365  v8i16 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
366  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
367  const v16u8 limit = (v16u8)__msa_fill_b(limit_in);
368  const v16u8 thresh = (v16u8)__msa_fill_b(thresh_in);
369
370  LD_UB8(ptmp, stride, row0, row1, row2, row3, row4, row5, row6, row7);
371  ptmp += (8 * stride);
372  LD_UB8(ptmp, stride, row8, row9, row10, row11, row12, row13, row14, row15);
373  TRANSPOSE16x8_UB_UB(row0, row1, row2, row3, row4, row5, row6, row7,
374                      row8, row9, row10, row11, row12, row13, row14, row15,
375                      p3, p2, p1, p0, q0, q1, q2, q3);
376  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit, b_limit, thresh,
377               hev, mask);
378  LPF_MBFILTER(p2, p1, p0, q0, q1, q2, mask, hev);
379  ILVR_B2_SH(p1, p2, q0, p0, tmp0, tmp1);
380  ILVRL_H2_SH(tmp1, tmp0, tmp3, tmp4);
381  ILVL_B2_SH(p1, p2, q0, p0, tmp0, tmp1);
382  ILVRL_H2_SH(tmp1, tmp0, tmp6, tmp7);
383  ILVRL_B2_SH(q2, q1, tmp2, tmp5);
384  ptmp = src - 3;
385  ST6x1_UB(tmp3, 0, tmp2, 0, ptmp, 4);
386  ptmp += stride;
387  ST6x1_UB(tmp3, 1, tmp2, 1, ptmp, 4);
388  ptmp += stride;
389  ST6x1_UB(tmp3, 2, tmp2, 2, ptmp, 4);
390  ptmp += stride;
391  ST6x1_UB(tmp3, 3, tmp2, 3, ptmp, 4);
392  ptmp += stride;
393  ST6x1_UB(tmp4, 0, tmp2, 4, ptmp, 4);
394  ptmp += stride;
395  ST6x1_UB(tmp4, 1, tmp2, 5, ptmp, 4);
396  ptmp += stride;
397  ST6x1_UB(tmp4, 2, tmp2, 6, ptmp, 4);
398  ptmp += stride;
399  ST6x1_UB(tmp4, 3, tmp2, 7, ptmp, 4);
400  ptmp += stride;
401  ST6x1_UB(tmp6, 0, tmp5, 0, ptmp, 4);
402  ptmp += stride;
403  ST6x1_UB(tmp6, 1, tmp5, 1, ptmp, 4);
404  ptmp += stride;
405  ST6x1_UB(tmp6, 2, tmp5, 2, ptmp, 4);
406  ptmp += stride;
407  ST6x1_UB(tmp6, 3, tmp5, 3, ptmp, 4);
408  ptmp += stride;
409  ST6x1_UB(tmp7, 0, tmp5, 4, ptmp, 4);
410  ptmp += stride;
411  ST6x1_UB(tmp7, 1, tmp5, 5, ptmp, 4);
412  ptmp += stride;
413  ST6x1_UB(tmp7, 2, tmp5, 6, ptmp, 4);
414  ptmp += stride;
415  ST6x1_UB(tmp7, 3, tmp5, 7, ptmp, 4);
416}
417
418// on three inner edges
419static void VFilterHorEdge16i(uint8_t* src, int stride,
420                              int b_limit, int limit, int thresh) {
421  v16u8 mask, hev;
422  v16u8 p3, p2, p1, p0, q3, q2, q1, q0;
423  const v16u8 thresh0 = (v16u8)__msa_fill_b(thresh);
424  const v16u8 b_limit0 = (v16u8)__msa_fill_b(b_limit);
425  const v16u8 limit0 = (v16u8)__msa_fill_b(limit);
426
427  LD_UB8((src - 4 * stride), stride, p3, p2, p1, p0, q0, q1, q2, q3);
428  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit0, b_limit0, thresh0,
429               hev, mask);
430  LPF_FILTER4_4W(p1, p0, q0, q1, mask, hev);
431  ST_UB4(p1, p0, q0, q1, (src - 2 * stride), stride);
432}
433
434static void VFilter16i(uint8_t* src_y, int stride,
435                       int b_limit, int limit, int thresh) {
436  VFilterHorEdge16i(src_y +  4 * stride, stride, b_limit, limit, thresh);
437  VFilterHorEdge16i(src_y +  8 * stride, stride, b_limit, limit, thresh);
438  VFilterHorEdge16i(src_y + 12 * stride, stride, b_limit, limit, thresh);
439}
440
441static void HFilterVertEdge16i(uint8_t* src, int stride,
442                               int b_limit, int limit, int thresh) {
443  v16u8 mask, hev;
444  v16u8 p3, p2, p1, p0, q3, q2, q1, q0;
445  v16u8 row0, row1, row2, row3, row4, row5, row6, row7;
446  v16u8 row8, row9, row10, row11, row12, row13, row14, row15;
447  v8i16 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
448  const v16u8 thresh0 = (v16u8)__msa_fill_b(thresh);
449  const v16u8 b_limit0 = (v16u8)__msa_fill_b(b_limit);
450  const v16u8 limit0 = (v16u8)__msa_fill_b(limit);
451
452  LD_UB8(src - 4, stride, row0, row1, row2, row3, row4, row5, row6, row7);
453  LD_UB8(src - 4 + (8 * stride), stride,
454         row8, row9, row10, row11, row12, row13, row14, row15);
455  TRANSPOSE16x8_UB_UB(row0, row1, row2, row3, row4, row5, row6, row7,
456                      row8, row9, row10, row11, row12, row13, row14, row15,
457                      p3, p2, p1, p0, q0, q1, q2, q3);
458  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit0, b_limit0, thresh0,
459               hev, mask);
460  LPF_FILTER4_4W(p1, p0, q0, q1, mask, hev);
461  ILVR_B2_SH(p0, p1, q1, q0, tmp0, tmp1);
462  ILVRL_H2_SH(tmp1, tmp0, tmp2, tmp3);
463  ILVL_B2_SH(p0, p1, q1, q0, tmp0, tmp1);
464  ILVRL_H2_SH(tmp1, tmp0, tmp4, tmp5);
465  src -= 2;
466  ST4x8_UB(tmp2, tmp3, src, stride);
467  src += (8 * stride);
468  ST4x8_UB(tmp4, tmp5, src, stride);
469}
470
471static void HFilter16i(uint8_t* src_y, int stride,
472                       int b_limit, int limit, int thresh) {
473  HFilterVertEdge16i(src_y +  4, stride, b_limit, limit, thresh);
474  HFilterVertEdge16i(src_y +  8, stride, b_limit, limit, thresh);
475  HFilterVertEdge16i(src_y + 12, stride, b_limit, limit, thresh);
476}
477
478// 8-pixels wide variants, for chroma filtering
479static void VFilter8(uint8_t* src_u, uint8_t* src_v, int stride,
480                     int b_limit_in, int limit_in, int thresh_in) {
481  uint8_t* ptmp_src_u = src_u - 4 * stride;
482  uint8_t* ptmp_src_v = src_v - 4 * stride;
483  uint64_t p2_d, p1_d, p0_d, q0_d, q1_d, q2_d;
484  v16u8 p3, p2, p1, p0, q3, q2, q1, q0, mask, hev;
485  v16u8 p3_u, p2_u, p1_u, p0_u, q3_u, q2_u, q1_u, q0_u;
486  v16u8 p3_v, p2_v, p1_v, p0_v, q3_v, q2_v, q1_v, q0_v;
487  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
488  const v16u8 limit = (v16u8)__msa_fill_b(limit_in);
489  const v16u8 thresh = (v16u8)__msa_fill_b(thresh_in);
490
491  LD_UB8(ptmp_src_u, stride, p3_u, p2_u, p1_u, p0_u, q0_u, q1_u, q2_u, q3_u);
492  LD_UB8(ptmp_src_v, stride, p3_v, p2_v, p1_v, p0_v, q0_v, q1_v, q2_v, q3_v);
493  ILVR_D4_UB(p3_v, p3_u, p2_v, p2_u, p1_v, p1_u, p0_v, p0_u, p3, p2, p1, p0);
494  ILVR_D4_UB(q0_v, q0_u, q1_v, q1_u, q2_v, q2_u, q3_v, q3_u, q0, q1, q2, q3);
495  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit, b_limit, thresh,
496               hev, mask);
497  LPF_MBFILTER(p2, p1, p0, q0, q1, q2, mask, hev);
498  p2_d = __msa_copy_s_d((v2i64)p2, 0);
499  p1_d = __msa_copy_s_d((v2i64)p1, 0);
500  p0_d = __msa_copy_s_d((v2i64)p0, 0);
501  q0_d = __msa_copy_s_d((v2i64)q0, 0);
502  q1_d = __msa_copy_s_d((v2i64)q1, 0);
503  q2_d = __msa_copy_s_d((v2i64)q2, 0);
504  ptmp_src_u += stride;
505  SD4(p2_d, p1_d, p0_d, q0_d, ptmp_src_u, stride);
506  ptmp_src_u += (4 * stride);
507  SD(q1_d, ptmp_src_u);
508  ptmp_src_u += stride;
509  SD(q2_d, ptmp_src_u);
510  p2_d = __msa_copy_s_d((v2i64)p2, 1);
511  p1_d = __msa_copy_s_d((v2i64)p1, 1);
512  p0_d = __msa_copy_s_d((v2i64)p0, 1);
513  q0_d = __msa_copy_s_d((v2i64)q0, 1);
514  q1_d = __msa_copy_s_d((v2i64)q1, 1);
515  q2_d = __msa_copy_s_d((v2i64)q2, 1);
516  ptmp_src_v += stride;
517  SD4(p2_d, p1_d, p0_d, q0_d, ptmp_src_v, stride);
518  ptmp_src_v += (4 * stride);
519  SD(q1_d, ptmp_src_v);
520  ptmp_src_v += stride;
521  SD(q2_d, ptmp_src_v);
522}
523
524static void HFilter8(uint8_t* src_u, uint8_t* src_v, int stride,
525                     int b_limit_in, int limit_in, int thresh_in) {
526  uint8_t* ptmp_src_u = src_u - 4;
527  uint8_t* ptmp_src_v = src_v - 4;
528  v16u8 p3, p2, p1, p0, q3, q2, q1, q0, mask, hev;
529  v16u8 row0, row1, row2, row3, row4, row5, row6, row7, row8;
530  v16u8 row9, row10, row11, row12, row13, row14, row15;
531  v8i16 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
532  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
533  const v16u8 limit = (v16u8)__msa_fill_b(limit_in);
534  const v16u8 thresh = (v16u8)__msa_fill_b(thresh_in);
535
536  LD_UB8(ptmp_src_u, stride, row0, row1, row2, row3, row4, row5, row6, row7);
537  LD_UB8(ptmp_src_v, stride,
538         row8, row9, row10, row11, row12, row13, row14, row15);
539  TRANSPOSE16x8_UB_UB(row0, row1, row2, row3, row4, row5, row6, row7,
540                      row8, row9, row10, row11, row12, row13, row14, row15,
541                      p3, p2, p1, p0, q0, q1, q2, q3);
542  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit, b_limit, thresh,
543               hev, mask);
544  LPF_MBFILTER(p2, p1, p0, q0, q1, q2, mask, hev);
545  ILVR_B2_SH(p1, p2, q0, p0, tmp0, tmp1);
546  ILVRL_H2_SH(tmp1, tmp0, tmp3, tmp4);
547  ILVL_B2_SH(p1, p2, q0, p0, tmp0, tmp1);
548  ILVRL_H2_SH(tmp1, tmp0, tmp6, tmp7);
549  ILVRL_B2_SH(q2, q1, tmp2, tmp5);
550  ptmp_src_u += 1;
551  ST6x4_UB(tmp3, 0, tmp2, 0, ptmp_src_u, stride);
552  ptmp_src_u += 4 * stride;
553  ST6x4_UB(tmp4, 0, tmp2, 4, ptmp_src_u, stride);
554  ptmp_src_v += 1;
555  ST6x4_UB(tmp6, 0, tmp5, 0, ptmp_src_v, stride);
556  ptmp_src_v += 4 * stride;
557  ST6x4_UB(tmp7, 0, tmp5, 4, ptmp_src_v, stride);
558}
559
560static void VFilter8i(uint8_t* src_u, uint8_t* src_v, int stride,
561                      int b_limit_in, int limit_in, int thresh_in) {
562  uint64_t p1_d, p0_d, q0_d, q1_d;
563  v16u8 p3, p2, p1, p0, q3, q2, q1, q0, mask, hev;
564  v16u8 p3_u, p2_u, p1_u, p0_u, q3_u, q2_u, q1_u, q0_u;
565  v16u8 p3_v, p2_v, p1_v, p0_v, q3_v, q2_v, q1_v, q0_v;
566  const v16u8 thresh = (v16u8)__msa_fill_b(thresh_in);
567  const v16u8 limit = (v16u8)__msa_fill_b(limit_in);
568  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
569
570  LD_UB8(src_u, stride, p3_u, p2_u, p1_u, p0_u, q0_u, q1_u, q2_u, q3_u);
571  src_u += (5 * stride);
572  LD_UB8(src_v, stride, p3_v, p2_v, p1_v, p0_v, q0_v, q1_v, q2_v, q3_v);
573  src_v += (5 * stride);
574  ILVR_D4_UB(p3_v, p3_u, p2_v, p2_u, p1_v, p1_u, p0_v, p0_u, p3, p2, p1, p0);
575  ILVR_D4_UB(q0_v, q0_u, q1_v, q1_u, q2_v, q2_u, q3_v, q3_u, q0, q1, q2, q3);
576  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit, b_limit, thresh,
577               hev, mask);
578  LPF_FILTER4_4W(p1, p0, q0, q1, mask, hev);
579  p1_d = __msa_copy_s_d((v2i64)p1, 0);
580  p0_d = __msa_copy_s_d((v2i64)p0, 0);
581  q0_d = __msa_copy_s_d((v2i64)q0, 0);
582  q1_d = __msa_copy_s_d((v2i64)q1, 0);
583  SD4(q1_d, q0_d, p0_d, p1_d, src_u, -stride);
584  p1_d = __msa_copy_s_d((v2i64)p1, 1);
585  p0_d = __msa_copy_s_d((v2i64)p0, 1);
586  q0_d = __msa_copy_s_d((v2i64)q0, 1);
587  q1_d = __msa_copy_s_d((v2i64)q1, 1);
588  SD4(q1_d, q0_d, p0_d, p1_d, src_v, -stride);
589}
590
591static void HFilter8i(uint8_t* src_u, uint8_t* src_v, int stride,
592                      int b_limit_in, int limit_in, int thresh_in) {
593  v16u8 p3, p2, p1, p0, q3, q2, q1, q0, mask, hev;
594  v16u8 row0, row1, row2, row3, row4, row5, row6, row7, row8;
595  v16u8 row9, row10, row11, row12, row13, row14, row15;
596  v4i32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
597  const v16u8 thresh = (v16u8)__msa_fill_b(thresh_in);
598  const v16u8 limit = (v16u8)__msa_fill_b(limit_in);
599  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
600
601  LD_UB8(src_u, stride, row0, row1, row2, row3, row4, row5, row6, row7);
602  LD_UB8(src_v, stride,
603         row8, row9, row10, row11, row12, row13, row14, row15);
604  TRANSPOSE16x8_UB_UB(row0, row1, row2, row3, row4, row5, row6, row7,
605                      row8, row9, row10, row11, row12, row13, row14, row15,
606                      p3, p2, p1, p0, q0, q1, q2, q3);
607  LPF_MASK_HEV(p3, p2, p1, p0, q0, q1, q2, q3, limit, b_limit, thresh,
608               hev, mask);
609  LPF_FILTER4_4W(p1, p0, q0, q1, mask, hev);
610  ILVR_B2_SW(p0, p1, q1, q0, tmp0, tmp1);
611  ILVRL_H2_SW(tmp1, tmp0, tmp2, tmp3);
612  ILVL_B2_SW(p0, p1, q1, q0, tmp0, tmp1);
613  ILVRL_H2_SW(tmp1, tmp0, tmp4, tmp5);
614  src_u += 2;
615  ST4x4_UB(tmp2, tmp2, 0, 1, 2, 3, src_u, stride);
616  src_u += 4 * stride;
617  ST4x4_UB(tmp3, tmp3, 0, 1, 2, 3, src_u, stride);
618  src_v += 2;
619  ST4x4_UB(tmp4, tmp4, 0, 1, 2, 3, src_v, stride);
620  src_v += 4 * stride;
621  ST4x4_UB(tmp5, tmp5, 0, 1, 2, 3, src_v, stride);
622}
623
624static void SimpleVFilter16(uint8_t* src, int stride, int b_limit_in) {
625  v16u8 p1, p0, q1, q0, mask;
626  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
627
628  LD_UB4(src - 2 * stride, stride, p1, p0, q0, q1);
629  LPF_SIMPLE_MASK(p1, p0, q0, q1, b_limit, mask);
630  LPF_SIMPLE_FILT(p1, p0, q0, q1, mask);
631  ST_UB2(p0, q0, src - stride, stride);
632}
633
634static void SimpleHFilter16(uint8_t* src, int stride, int b_limit_in) {
635  v16u8 p1, p0, q1, q0, mask, row0, row1, row2, row3, row4, row5, row6, row7;
636  v16u8 row8, row9, row10, row11, row12, row13, row14, row15;
637  v8i16 tmp0, tmp1;
638  const v16u8 b_limit = (v16u8)__msa_fill_b(b_limit_in);
639  uint8_t* ptemp_src = src - 2;
640
641  LD_UB8(ptemp_src, stride, row0, row1, row2, row3, row4, row5, row6, row7);
642  LD_UB8(ptemp_src + 8 * stride, stride,
643         row8, row9, row10, row11, row12, row13, row14, row15);
644  TRANSPOSE16x4_UB_UB(row0, row1, row2, row3, row4, row5, row6, row7,
645                      row8, row9, row10, row11, row12, row13, row14, row15,
646                      p1, p0, q0, q1);
647  LPF_SIMPLE_MASK(p1, p0, q0, q1, b_limit, mask);
648  LPF_SIMPLE_FILT(p1, p0, q0, q1, mask);
649  ILVRL_B2_SH(q0, p0, tmp1, tmp0);
650  ptemp_src += 1;
651  ST2x4_UB(tmp1, 0, ptemp_src, stride);
652  ptemp_src += 4 * stride;
653  ST2x4_UB(tmp1, 4, ptemp_src, stride);
654  ptemp_src += 4 * stride;
655  ST2x4_UB(tmp0, 0, ptemp_src, stride);
656  ptemp_src += 4 * stride;
657  ST2x4_UB(tmp0, 4, ptemp_src, stride);
658  ptemp_src += 4 * stride;
659}
660
661static void SimpleVFilter16i(uint8_t* src_y, int stride, int b_limit_in) {
662  SimpleVFilter16(src_y +  4 * stride, stride, b_limit_in);
663  SimpleVFilter16(src_y +  8 * stride, stride, b_limit_in);
664  SimpleVFilter16(src_y + 12 * stride, stride, b_limit_in);
665}
666
667static void SimpleHFilter16i(uint8_t* src_y, int stride, int b_limit_in) {
668  SimpleHFilter16(src_y +  4, stride, b_limit_in);
669  SimpleHFilter16(src_y +  8, stride, b_limit_in);
670  SimpleHFilter16(src_y + 12, stride, b_limit_in);
671}
672
673//------------------------------------------------------------------------------
674// Intra predictions
675//------------------------------------------------------------------------------
676
677// 4x4
678
679static void DC4(uint8_t* dst) {   // DC
680  uint32_t dc = 4;
681  int i;
682  for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
683  dc >>= 3;
684  dc = dc | (dc << 8) | (dc << 16) | (dc << 24);
685  SW4(dc, dc, dc, dc, dst, BPS);
686}
687
688static void TM4(uint8_t* dst) {
689  const uint8_t* const ptemp = dst - BPS - 1;
690  v8i16 T, d, r0, r1, r2, r3;
691  const v16i8 zero = { 0 };
692  const v8i16 TL = (v8i16)__msa_fill_h(ptemp[0 * BPS]);
693  const v8i16 L0 = (v8i16)__msa_fill_h(ptemp[1 * BPS]);
694  const v8i16 L1 = (v8i16)__msa_fill_h(ptemp[2 * BPS]);
695  const v8i16 L2 = (v8i16)__msa_fill_h(ptemp[3 * BPS]);
696  const v8i16 L3 = (v8i16)__msa_fill_h(ptemp[4 * BPS]);
697  const v16u8 T1 = LD_UB(ptemp + 1);
698
699  T  = (v8i16)__msa_ilvr_b(zero, (v16i8)T1);
700  d = T - TL;
701  ADD4(d, L0, d, L1, d, L2, d, L3, r0, r1, r2, r3);
702  CLIP_SH4_0_255(r0, r1, r2, r3);
703  PCKEV_ST4x4_UB(r0, r1, r2, r3, dst, BPS);
704}
705
706static void VE4(uint8_t* dst) {    // vertical
707  const uint8_t* const ptop = dst - BPS - 1;
708  const uint32_t val0 = LW(ptop + 0);
709  const uint32_t val1 = LW(ptop + 4);
710  uint32_t out;
711  v16u8 A, B, C, AC, B2, R;
712
713  INSERT_W2_UB(val0, val1, A);
714  B = SLDI_UB(A, A, 1);
715  C = SLDI_UB(A, A, 2);
716  AC = __msa_ave_u_b(A, C);
717  B2 = __msa_ave_u_b(B, B);
718  R = __msa_aver_u_b(AC, B2);
719  out = __msa_copy_s_w((v4i32)R, 0);
720  SW4(out, out, out, out, dst, BPS);
721}
722
723static void RD4(uint8_t* dst) {   // Down-right
724  const uint8_t* const ptop = dst - 1 - BPS;
725  uint32_t val0 = LW(ptop + 0);
726  uint32_t val1 = LW(ptop + 4);
727  uint32_t val2, val3;
728  v16u8 A, B, C, AC, B2, R, A1;
729
730  INSERT_W2_UB(val0, val1, A1);
731  A = SLDI_UB(A1, A1, 12);
732  A = (v16u8)__msa_insert_b((v16i8)A, 3, ptop[1 * BPS]);
733  A = (v16u8)__msa_insert_b((v16i8)A, 2, ptop[2 * BPS]);
734  A = (v16u8)__msa_insert_b((v16i8)A, 1, ptop[3 * BPS]);
735  A = (v16u8)__msa_insert_b((v16i8)A, 0, ptop[4 * BPS]);
736  B = SLDI_UB(A, A, 1);
737  C = SLDI_UB(A, A, 2);
738  AC = __msa_ave_u_b(A, C);
739  B2 = __msa_ave_u_b(B, B);
740  R = __msa_aver_u_b(AC, B2);
741  val3 = __msa_copy_s_w((v4i32)R, 0);
742  R = SLDI_UB(R, R, 1);
743  val2 = __msa_copy_s_w((v4i32)R, 0);
744  R = SLDI_UB(R, R, 1);
745  val1 = __msa_copy_s_w((v4i32)R, 0);
746  R = SLDI_UB(R, R, 1);
747  val0 = __msa_copy_s_w((v4i32)R, 0);
748  SW4(val0, val1, val2, val3, dst, BPS);
749}
750
751static void LD4(uint8_t* dst) {   // Down-Left
752  const uint8_t* const ptop = dst - BPS;
753  uint32_t val0 = LW(ptop + 0);
754  uint32_t val1 = LW(ptop + 4);
755  uint32_t val2, val3;
756  v16u8 A, B, C, AC, B2, R;
757
758  INSERT_W2_UB(val0, val1, A);
759  B = SLDI_UB(A, A, 1);
760  C = SLDI_UB(A, A, 2);
761  C = (v16u8)__msa_insert_b((v16i8)C, 6, ptop[7]);
762  AC = __msa_ave_u_b(A, C);
763  B2 = __msa_ave_u_b(B, B);
764  R = __msa_aver_u_b(AC, B2);
765  val0 = __msa_copy_s_w((v4i32)R, 0);
766  R = SLDI_UB(R, R, 1);
767  val1 = __msa_copy_s_w((v4i32)R, 0);
768  R = SLDI_UB(R, R, 1);
769  val2 = __msa_copy_s_w((v4i32)R, 0);
770  R = SLDI_UB(R, R, 1);
771  val3 = __msa_copy_s_w((v4i32)R, 0);
772  SW4(val0, val1, val2, val3, dst, BPS);
773}
774
775// 16x16
776
777static void DC16(uint8_t* dst) {   // DC
778  uint32_t dc = 16;
779  int i;
780  const v16u8 rtop = LD_UB(dst - BPS);
781  const v8u16 dctop = __msa_hadd_u_h(rtop, rtop);
782  v16u8 out;
783
784  for (i = 0; i < 16; ++i) {
785    dc += dst[-1 + i * BPS];
786  }
787  dc += HADD_UH_U32(dctop);
788  out = (v16u8)__msa_fill_b(dc >> 5);
789  ST_UB8(out, out, out, out, out, out, out, out, dst, BPS);
790  ST_UB8(out, out, out, out, out, out, out, out, dst + 8 * BPS, BPS);
791}
792
793static void TM16(uint8_t* dst) {
794  int j;
795  v8i16 d1, d2;
796  const v16i8 zero = { 0 };
797  const v8i16 TL = (v8i16)__msa_fill_h(dst[-1 - BPS]);
798  const v16i8 T = LD_SB(dst - BPS);
799
800  ILVRL_B2_SH(zero, T, d1, d2);
801  SUB2(d1, TL, d2, TL, d1, d2);
802  for (j = 0; j < 16; j += 4) {
803    v16i8 t0, t1, t2, t3;
804    v8i16 r0, r1, r2, r3, r4, r5, r6, r7;
805    const v8i16 L0 = (v8i16)__msa_fill_h(dst[-1 + 0 * BPS]);
806    const v8i16 L1 = (v8i16)__msa_fill_h(dst[-1 + 1 * BPS]);
807    const v8i16 L2 = (v8i16)__msa_fill_h(dst[-1 + 2 * BPS]);
808    const v8i16 L3 = (v8i16)__msa_fill_h(dst[-1 + 3 * BPS]);
809    ADD4(d1, L0, d1, L1, d1, L2, d1, L3, r0, r1, r2, r3);
810    ADD4(d2, L0, d2, L1, d2, L2, d2, L3, r4, r5, r6, r7);
811    CLIP_SH4_0_255(r0, r1, r2, r3);
812    CLIP_SH4_0_255(r4, r5, r6, r7);
813    PCKEV_B4_SB(r4, r0, r5, r1, r6, r2, r7, r3, t0, t1, t2, t3);
814    ST_SB4(t0, t1, t2, t3, dst, BPS);
815    dst += 4 * BPS;
816  }
817}
818
819static void VE16(uint8_t* dst) {   // vertical
820  const v16u8 rtop = LD_UB(dst - BPS);
821  ST_UB8(rtop, rtop, rtop, rtop, rtop, rtop, rtop, rtop, dst, BPS);
822  ST_UB8(rtop, rtop, rtop, rtop, rtop, rtop, rtop, rtop, dst + 8 * BPS, BPS);
823}
824
825static void HE16(uint8_t* dst) {   // horizontal
826  int j;
827  for (j = 16; j > 0; j -= 4) {
828    const v16u8 L0 = (v16u8)__msa_fill_b(dst[-1 + 0 * BPS]);
829    const v16u8 L1 = (v16u8)__msa_fill_b(dst[-1 + 1 * BPS]);
830    const v16u8 L2 = (v16u8)__msa_fill_b(dst[-1 + 2 * BPS]);
831    const v16u8 L3 = (v16u8)__msa_fill_b(dst[-1 + 3 * BPS]);
832    ST_UB4(L0, L1, L2, L3, dst, BPS);
833    dst += 4 * BPS;
834  }
835}
836
837static void DC16NoTop(uint8_t* dst) {   // DC with top samples not available
838  int j;
839  uint32_t dc = 8;
840  v16u8 out;
841
842  for (j = 0; j < 16; ++j) {
843    dc += dst[-1 + j * BPS];
844  }
845  out = (v16u8)__msa_fill_b(dc >> 4);
846  ST_UB8(out, out, out, out, out, out, out, out, dst, BPS);
847  ST_UB8(out, out, out, out, out, out, out, out, dst + 8 * BPS, BPS);
848}
849
850static void DC16NoLeft(uint8_t* dst) {   // DC with left samples not available
851  uint32_t dc = 8;
852  const v16u8 rtop = LD_UB(dst - BPS);
853  const v8u16 dctop = __msa_hadd_u_h(rtop, rtop);
854  v16u8 out;
855
856  dc += HADD_UH_U32(dctop);
857  out = (v16u8)__msa_fill_b(dc >> 4);
858  ST_UB8(out, out, out, out, out, out, out, out, dst, BPS);
859  ST_UB8(out, out, out, out, out, out, out, out, dst + 8 * BPS, BPS);
860}
861
862static void DC16NoTopLeft(uint8_t* dst) {   // DC with nothing
863  const v16u8 out = (v16u8)__msa_fill_b(0x80);
864  ST_UB8(out, out, out, out, out, out, out, out, dst, BPS);
865  ST_UB8(out, out, out, out, out, out, out, out, dst + 8 * BPS, BPS);
866}
867
868// Chroma
869
870#define STORE8x8(out, dst) do {                 \
871  SD4(out, out, out, out, dst + 0 * BPS, BPS);  \
872  SD4(out, out, out, out, dst + 4 * BPS, BPS);  \
873} while (0)
874
875static void DC8uv(uint8_t* dst) {   // DC
876  uint32_t dc = 8;
877  int i;
878  uint64_t out;
879  const v16u8 rtop = LD_UB(dst - BPS);
880  const v8u16 temp0 = __msa_hadd_u_h(rtop, rtop);
881  const v4u32 temp1 = __msa_hadd_u_w(temp0, temp0);
882  const v2u64 temp2 = __msa_hadd_u_d(temp1, temp1);
883  v16u8 dctemp;
884
885  for (i = 0; i < 8; ++i) {
886    dc += dst[-1 + i * BPS];
887  }
888  dc += __msa_copy_s_w((v4i32)temp2, 0);
889  dctemp = (v16u8)__msa_fill_b(dc >> 4);
890  out = __msa_copy_s_d((v2i64)dctemp, 0);
891  STORE8x8(out, dst);
892}
893
894static void TM8uv(uint8_t* dst) {
895  int j;
896  const v16i8 T1 = LD_SB(dst - BPS);
897  const v16i8 zero = { 0 };
898  const v8i16 T  = (v8i16)__msa_ilvr_b(zero, T1);
899  const v8i16 TL = (v8i16)__msa_fill_h(dst[-1 - BPS]);
900  const v8i16 d = T - TL;
901
902  for (j = 0; j < 8; j += 4) {
903    v16i8 t0, t1;
904    v8i16 r0 = (v8i16)__msa_fill_h(dst[-1 + 0 * BPS]);
905    v8i16 r1 = (v8i16)__msa_fill_h(dst[-1 + 1 * BPS]);
906    v8i16 r2 = (v8i16)__msa_fill_h(dst[-1 + 2 * BPS]);
907    v8i16 r3 = (v8i16)__msa_fill_h(dst[-1 + 3 * BPS]);
908    ADD4(d, r0, d, r1, d, r2, d, r3, r0, r1, r2, r3);
909    CLIP_SH4_0_255(r0, r1, r2, r3);
910    PCKEV_B2_SB(r1, r0, r3, r2, t0, t1);
911    ST4x4_UB(t0, t1, 0, 2, 0, 2, dst, BPS);
912    ST4x4_UB(t0, t1, 1, 3, 1, 3, dst + 4, BPS);
913    dst += 4 * BPS;
914  }
915}
916
917static void VE8uv(uint8_t* dst) {   // vertical
918  const v16u8 rtop = LD_UB(dst - BPS);
919  const uint64_t out = __msa_copy_s_d((v2i64)rtop, 0);
920  STORE8x8(out, dst);
921}
922
923static void HE8uv(uint8_t* dst) {   // horizontal
924  int j;
925  for (j = 0; j < 8; j += 4) {
926    const v16u8 L0 = (v16u8)__msa_fill_b(dst[-1 + 0 * BPS]);
927    const v16u8 L1 = (v16u8)__msa_fill_b(dst[-1 + 1 * BPS]);
928    const v16u8 L2 = (v16u8)__msa_fill_b(dst[-1 + 2 * BPS]);
929    const v16u8 L3 = (v16u8)__msa_fill_b(dst[-1 + 3 * BPS]);
930    const uint64_t out0 = __msa_copy_s_d((v2i64)L0, 0);
931    const uint64_t out1 = __msa_copy_s_d((v2i64)L1, 0);
932    const uint64_t out2 = __msa_copy_s_d((v2i64)L2, 0);
933    const uint64_t out3 = __msa_copy_s_d((v2i64)L3, 0);
934    SD4(out0, out1, out2, out3, dst, BPS);
935    dst += 4 * BPS;
936  }
937}
938
939static void DC8uvNoLeft(uint8_t* dst) {   // DC with no left samples
940  const uint32_t dc = 4;
941  const v16u8 rtop = LD_UB(dst - BPS);
942  const v8u16 temp0 = __msa_hadd_u_h(rtop, rtop);
943  const v4u32 temp1 = __msa_hadd_u_w(temp0, temp0);
944  const v2u64 temp2 = __msa_hadd_u_d(temp1, temp1);
945  const uint32_t sum_m = __msa_copy_s_w((v4i32)temp2, 0);
946  const v16u8 dcval = (v16u8)__msa_fill_b((dc + sum_m) >> 3);
947  const uint64_t out = __msa_copy_s_d((v2i64)dcval, 0);
948  STORE8x8(out, dst);
949}
950
951static void DC8uvNoTop(uint8_t* dst) {   // DC with no top samples
952  uint32_t dc = 4;
953  int i;
954  uint64_t out;
955  v16u8 dctemp;
956
957  for (i = 0; i < 8; ++i) {
958    dc += dst[-1 + i * BPS];
959  }
960  dctemp = (v16u8)__msa_fill_b(dc >> 3);
961  out = __msa_copy_s_d((v2i64)dctemp, 0);
962  STORE8x8(out, dst);
963}
964
965static void DC8uvNoTopLeft(uint8_t* dst) {   // DC with nothing
966  const uint64_t out = 0x8080808080808080ULL;
967  STORE8x8(out, dst);
968}
969
970//------------------------------------------------------------------------------
971// Entry point
972
973extern void VP8DspInitMSA(void);
974
975WEBP_TSAN_IGNORE_FUNCTION void VP8DspInitMSA(void) {
976  VP8TransformWHT = TransformWHT;
977  VP8Transform = TransformTwo;
978  VP8TransformDC = TransformDC;
979  VP8TransformAC3 = TransformAC3;
980
981  VP8VFilter16  = VFilter16;
982  VP8HFilter16  = HFilter16;
983  VP8VFilter16i = VFilter16i;
984  VP8HFilter16i = HFilter16i;
985  VP8VFilter8  = VFilter8;
986  VP8HFilter8  = HFilter8;
987  VP8VFilter8i = VFilter8i;
988  VP8HFilter8i = HFilter8i;
989  VP8SimpleVFilter16  = SimpleVFilter16;
990  VP8SimpleHFilter16  = SimpleHFilter16;
991  VP8SimpleVFilter16i = SimpleVFilter16i;
992  VP8SimpleHFilter16i = SimpleHFilter16i;
993
994  VP8PredLuma4[0] = DC4;
995  VP8PredLuma4[1] = TM4;
996  VP8PredLuma4[2] = VE4;
997  VP8PredLuma4[4] = RD4;
998  VP8PredLuma4[6] = LD4;
999  VP8PredLuma16[0] = DC16;
1000  VP8PredLuma16[1] = TM16;
1001  VP8PredLuma16[2] = VE16;
1002  VP8PredLuma16[3] = HE16;
1003  VP8PredLuma16[4] = DC16NoTop;
1004  VP8PredLuma16[5] = DC16NoLeft;
1005  VP8PredLuma16[6] = DC16NoTopLeft;
1006  VP8PredChroma8[0] = DC8uv;
1007  VP8PredChroma8[1] = TM8uv;
1008  VP8PredChroma8[2] = VE8uv;
1009  VP8PredChroma8[3] = HE8uv;
1010  VP8PredChroma8[4] = DC8uvNoTop;
1011  VP8PredChroma8[5] = DC8uvNoLeft;
1012  VP8PredChroma8[6] = DC8uvNoTopLeft;
1013}
1014
1015#else  // !WEBP_USE_MSA
1016
1017WEBP_DSP_INIT_STUB(VP8DspInitMSA)
1018
1019#endif  // WEBP_USE_MSA
1020