1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Copyright (c) 2021 Loongson Technology Corporation Limited
3cabdff1aSopenharmony_ci * Contributed by Xiwei Gu <guxiwei-hf@loongson.cn>
4cabdff1aSopenharmony_ci *
5cabdff1aSopenharmony_ci * This file is part of FFmpeg.
6cabdff1aSopenharmony_ci *
7cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
8cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
9cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
10cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
11cabdff1aSopenharmony_ci *
12cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
13cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
14cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15cabdff1aSopenharmony_ci * Lesser General Public License for more details.
16cabdff1aSopenharmony_ci *
17cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
18cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
19cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20cabdff1aSopenharmony_ci */
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#include "libavcodec/bit_depth_template.c"
23cabdff1aSopenharmony_ci#include "h264dsp_lasx.h"
24cabdff1aSopenharmony_ci#include "libavutil/loongarch/loongson_intrinsics.h"
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#define H264_LOOP_FILTER_STRENGTH_ITERATION_LASX(edges, step, mask_mv, dir, \
27cabdff1aSopenharmony_ci                                                 d_idx, mask_dir)           \
28cabdff1aSopenharmony_cido {                                                                        \
29cabdff1aSopenharmony_ci    int b_idx = 0; \
30cabdff1aSopenharmony_ci    int step_x4 = step << 2; \
31cabdff1aSopenharmony_ci    int d_idx_12 = d_idx + 12; \
32cabdff1aSopenharmony_ci    int d_idx_52 = d_idx + 52; \
33cabdff1aSopenharmony_ci    int d_idx_x4 = d_idx << 2; \
34cabdff1aSopenharmony_ci    int d_idx_x4_48 = d_idx_x4 + 48; \
35cabdff1aSopenharmony_ci    int dir_x32  = dir * 32; \
36cabdff1aSopenharmony_ci    uint8_t *ref_t = (uint8_t*)ref; \
37cabdff1aSopenharmony_ci    uint8_t *mv_t  = (uint8_t*)mv; \
38cabdff1aSopenharmony_ci    uint8_t *nnz_t = (uint8_t*)nnz; \
39cabdff1aSopenharmony_ci    uint8_t *bS_t  = (uint8_t*)bS; \
40cabdff1aSopenharmony_ci    mask_mv <<= 3; \
41cabdff1aSopenharmony_ci    for (; b_idx < edges; b_idx += step) { \
42cabdff1aSopenharmony_ci        out &= mask_dir; \
43cabdff1aSopenharmony_ci        if (!(mask_mv & b_idx)) { \
44cabdff1aSopenharmony_ci            if (bidir) { \
45cabdff1aSopenharmony_ci                ref2 = __lasx_xvldx(ref_t, d_idx_12); \
46cabdff1aSopenharmony_ci                ref3 = __lasx_xvldx(ref_t, d_idx_52); \
47cabdff1aSopenharmony_ci                ref0 = __lasx_xvld(ref_t, 12); \
48cabdff1aSopenharmony_ci                ref1 = __lasx_xvld(ref_t, 52); \
49cabdff1aSopenharmony_ci                ref2 = __lasx_xvilvl_w(ref3, ref2); \
50cabdff1aSopenharmony_ci                ref0 = __lasx_xvilvl_w(ref0, ref0); \
51cabdff1aSopenharmony_ci                ref1 = __lasx_xvilvl_w(ref1, ref1); \
52cabdff1aSopenharmony_ci                ref3 = __lasx_xvshuf4i_w(ref2, 0xB1); \
53cabdff1aSopenharmony_ci                ref0 = __lasx_xvsub_b(ref0, ref2); \
54cabdff1aSopenharmony_ci                ref1 = __lasx_xvsub_b(ref1, ref3); \
55cabdff1aSopenharmony_ci                ref0 = __lasx_xvor_v(ref0, ref1); \
56cabdff1aSopenharmony_ci\
57cabdff1aSopenharmony_ci                tmp2 = __lasx_xvldx(mv_t, d_idx_x4_48);   \
58cabdff1aSopenharmony_ci                tmp3 = __lasx_xvld(mv_t, 48); \
59cabdff1aSopenharmony_ci                tmp4 = __lasx_xvld(mv_t, 208); \
60cabdff1aSopenharmony_ci                tmp5 = __lasx_xvld(mv_t + d_idx_x4, 208); \
61cabdff1aSopenharmony_ci                DUP2_ARG3(__lasx_xvpermi_q, tmp2, tmp2, 0x20, tmp5, tmp5, \
62cabdff1aSopenharmony_ci                          0x20, tmp2, tmp5); \
63cabdff1aSopenharmony_ci                tmp3 =  __lasx_xvpermi_q(tmp4, tmp3, 0x20); \
64cabdff1aSopenharmony_ci                tmp2 = __lasx_xvsub_h(tmp2, tmp3); \
65cabdff1aSopenharmony_ci                tmp5 = __lasx_xvsub_h(tmp5, tmp3); \
66cabdff1aSopenharmony_ci                DUP2_ARG2(__lasx_xvsat_h, tmp2, 7, tmp5, 7, tmp2, tmp5); \
67cabdff1aSopenharmony_ci                tmp0 = __lasx_xvpickev_b(tmp5, tmp2); \
68cabdff1aSopenharmony_ci                tmp0 = __lasx_xvpermi_d(tmp0, 0xd8); \
69cabdff1aSopenharmony_ci                tmp0 = __lasx_xvadd_b(tmp0, cnst_1); \
70cabdff1aSopenharmony_ci                tmp0 = __lasx_xvssub_bu(tmp0, cnst_0); \
71cabdff1aSopenharmony_ci                tmp0 = __lasx_xvsat_h(tmp0, 7); \
72cabdff1aSopenharmony_ci                tmp0 = __lasx_xvpickev_b(tmp0, tmp0); \
73cabdff1aSopenharmony_ci                tmp0 = __lasx_xvpermi_d(tmp0, 0xd8); \
74cabdff1aSopenharmony_ci                tmp1 = __lasx_xvpickod_d(tmp0, tmp0); \
75cabdff1aSopenharmony_ci                out = __lasx_xvor_v(ref0, tmp0); \
76cabdff1aSopenharmony_ci                tmp1 = __lasx_xvshuf4i_w(tmp1, 0xB1); \
77cabdff1aSopenharmony_ci                out = __lasx_xvor_v(out, tmp1); \
78cabdff1aSopenharmony_ci                tmp0 = __lasx_xvshuf4i_w(out, 0xB1); \
79cabdff1aSopenharmony_ci                out = __lasx_xvmin_bu(out, tmp0); \
80cabdff1aSopenharmony_ci            } else { \
81cabdff1aSopenharmony_ci                ref0 = __lasx_xvldx(ref_t, d_idx_12); \
82cabdff1aSopenharmony_ci                ref3 = __lasx_xvld(ref_t, 12); \
83cabdff1aSopenharmony_ci                tmp2 = __lasx_xvldx(mv_t, d_idx_x4_48); \
84cabdff1aSopenharmony_ci                tmp3 = __lasx_xvld(mv_t, 48); \
85cabdff1aSopenharmony_ci                tmp4 = __lasx_xvsub_h(tmp3, tmp2); \
86cabdff1aSopenharmony_ci                tmp1 = __lasx_xvsat_h(tmp4, 7); \
87cabdff1aSopenharmony_ci                tmp1 = __lasx_xvpickev_b(tmp1, tmp1); \
88cabdff1aSopenharmony_ci                tmp1 = __lasx_xvadd_b(tmp1, cnst_1); \
89cabdff1aSopenharmony_ci                out = __lasx_xvssub_bu(tmp1, cnst_0); \
90cabdff1aSopenharmony_ci                out = __lasx_xvsat_h(out, 7); \
91cabdff1aSopenharmony_ci                out = __lasx_xvpickev_b(out, out); \
92cabdff1aSopenharmony_ci                ref0 = __lasx_xvsub_b(ref3, ref0); \
93cabdff1aSopenharmony_ci                out = __lasx_xvor_v(out, ref0); \
94cabdff1aSopenharmony_ci            } \
95cabdff1aSopenharmony_ci        } \
96cabdff1aSopenharmony_ci        tmp0 = __lasx_xvld(nnz_t, 12); \
97cabdff1aSopenharmony_ci        tmp1 = __lasx_xvldx(nnz_t, d_idx_12); \
98cabdff1aSopenharmony_ci        tmp0 = __lasx_xvor_v(tmp0, tmp1); \
99cabdff1aSopenharmony_ci        tmp0 = __lasx_xvmin_bu(tmp0, cnst_2); \
100cabdff1aSopenharmony_ci        out  = __lasx_xvmin_bu(out, cnst_2); \
101cabdff1aSopenharmony_ci        tmp0 = __lasx_xvslli_h(tmp0, 1); \
102cabdff1aSopenharmony_ci        tmp0 = __lasx_xvmax_bu(out, tmp0); \
103cabdff1aSopenharmony_ci        tmp0 = __lasx_vext2xv_hu_bu(tmp0); \
104cabdff1aSopenharmony_ci        __lasx_xvstelm_d(tmp0, bS_t + dir_x32, 0, 0); \
105cabdff1aSopenharmony_ci        ref_t += step; \
106cabdff1aSopenharmony_ci        mv_t  += step_x4; \
107cabdff1aSopenharmony_ci        nnz_t += step; \
108cabdff1aSopenharmony_ci        bS_t  += step; \
109cabdff1aSopenharmony_ci    } \
110cabdff1aSopenharmony_ci} while(0)
111cabdff1aSopenharmony_ci
112cabdff1aSopenharmony_civoid ff_h264_loop_filter_strength_lasx(int16_t bS[2][4][4], uint8_t nnz[40],
113cabdff1aSopenharmony_ci                                       int8_t ref[2][40], int16_t mv[2][40][2],
114cabdff1aSopenharmony_ci                                       int bidir, int edges, int step,
115cabdff1aSopenharmony_ci                                       int mask_mv0, int mask_mv1, int field)
116cabdff1aSopenharmony_ci{
117cabdff1aSopenharmony_ci    __m256i out;
118cabdff1aSopenharmony_ci    __m256i ref0, ref1, ref2, ref3;
119cabdff1aSopenharmony_ci    __m256i tmp0, tmp1;
120cabdff1aSopenharmony_ci    __m256i tmp2, tmp3, tmp4, tmp5;
121cabdff1aSopenharmony_ci    __m256i cnst_0, cnst_1, cnst_2;
122cabdff1aSopenharmony_ci    __m256i zero = __lasx_xvldi(0);
123cabdff1aSopenharmony_ci    __m256i one  = __lasx_xvnor_v(zero, zero);
124cabdff1aSopenharmony_ci    int64_t cnst3 = 0x0206020602060206, cnst4 = 0x0103010301030103;
125cabdff1aSopenharmony_ci    if (field) {
126cabdff1aSopenharmony_ci        cnst_0 = __lasx_xvreplgr2vr_d(cnst3);
127cabdff1aSopenharmony_ci        cnst_1 = __lasx_xvreplgr2vr_d(cnst4);
128cabdff1aSopenharmony_ci        cnst_2 = __lasx_xvldi(0x01);
129cabdff1aSopenharmony_ci    } else {
130cabdff1aSopenharmony_ci        DUP2_ARG1(__lasx_xvldi, 0x06, 0x03, cnst_0, cnst_1);
131cabdff1aSopenharmony_ci        cnst_2 = __lasx_xvldi(0x01);
132cabdff1aSopenharmony_ci    }
133cabdff1aSopenharmony_ci    step  <<= 3;
134cabdff1aSopenharmony_ci    edges <<= 3;
135cabdff1aSopenharmony_ci
136cabdff1aSopenharmony_ci    H264_LOOP_FILTER_STRENGTH_ITERATION_LASX(edges, step, mask_mv1,
137cabdff1aSopenharmony_ci                                             1, -8, zero);
138cabdff1aSopenharmony_ci    H264_LOOP_FILTER_STRENGTH_ITERATION_LASX(32, 8, mask_mv0, 0, -1, one);
139cabdff1aSopenharmony_ci
140cabdff1aSopenharmony_ci    DUP2_ARG2(__lasx_xvld, (int8_t*)bS, 0, (int8_t*)bS, 16, tmp0, tmp1);
141cabdff1aSopenharmony_ci    DUP2_ARG2(__lasx_xvilvh_d, tmp0, tmp0, tmp1, tmp1, tmp2, tmp3);
142cabdff1aSopenharmony_ci    LASX_TRANSPOSE4x4_H(tmp0, tmp2, tmp1, tmp3, tmp2, tmp3, tmp4, tmp5);
143cabdff1aSopenharmony_ci    __lasx_xvstelm_d(tmp2, (int8_t*)bS, 0, 0);
144cabdff1aSopenharmony_ci    __lasx_xvstelm_d(tmp3, (int8_t*)bS + 8, 0, 0);
145cabdff1aSopenharmony_ci    __lasx_xvstelm_d(tmp4, (int8_t*)bS + 16, 0, 0);
146cabdff1aSopenharmony_ci    __lasx_xvstelm_d(tmp5, (int8_t*)bS + 24, 0, 0);
147cabdff1aSopenharmony_ci}
148