1 /*
2 * Loongson optimized cabac
3 *
4 * Copyright (c) 2021 Loongson Technology Corporation Limited
5 * Contributed by Hao Chen <chenhao@loongson.cn>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavcodec/cabac.h"
25 #include "cabac.h"
26
27 #define decode_significance decode_significance_loongarch
decode_significance_loongarch(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index, int64_t last_off)28 static int decode_significance_loongarch(CABACContext *c, int max_coeff,
29 uint8_t *significant_coeff_ctx_base, int *index, int64_t last_off)
30 {
31 void *end = significant_coeff_ctx_base + max_coeff - 1;
32 int64_t minusstart = -(int64_t)significant_coeff_ctx_base;
33 int64_t minusindex = 4 - (int64_t)index;
34 int64_t bit, tmp0, tmp1, tmp2, one = 1;
35 uint8_t *state = significant_coeff_ctx_base;
36
37 __asm__ volatile(
38 "3:"
39 #if UNCHECKED_BITSTREAM_READER
40 GET_CABAC_LOONGARCH_UNCBSR
41 #else
42 GET_CABAC_LOONGARCH
43 #endif
44 "blt %[bit], %[one], 4f \n\t"
45 "add.d %[state], %[state], %[last_off] \n\t"
46 #if UNCHECKED_BITSTREAM_READER
47 GET_CABAC_LOONGARCH_UNCBSR
48 #else
49 GET_CABAC_LOONGARCH
50 #endif
51 "sub.d %[state], %[state], %[last_off] \n\t"
52 "add.d %[tmp0], %[state], %[minusstart] \n\t"
53 "st.w %[tmp0], %[index], 0 \n\t"
54 "bge %[bit], %[one], 5f \n\t"
55 "addi.d %[index], %[index], 4 \n\t"
56 "4: \n\t"
57 "addi.d %[state], %[state], 1 \n\t"
58 "blt %[state], %[end], 3b \n\t"
59 "add.d %[tmp0], %[state], %[minusstart] \n\t"
60 "st.w %[tmp0], %[index], 0 \n\t"
61 "5: \n\t"
62 "add.d %[tmp0], %[index], %[minusindex] \n\t"
63 "srli.d %[tmp0], %[tmp0], 2 \n\t"
64 : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2),
65 [c_range]"+&r"(c->range), [c_low]"+&r"(c->low), [state]"+&r"(state),
66 [c_bytestream]"+&r"(c->bytestream), [index]"+&r"(index)
67 : [tables]"r"(ff_h264_cabac_tables), [end]"r"(end), [one]"r"(one),
68 [minusstart]"r"(minusstart), [minusindex]"r"(minusindex),
69 [last_off]"r"(last_off),
70 #if !UNCHECKED_BITSTREAM_READER
71 [c_bytestream_end]"r"(c->bytestream_end),
72 #endif
73 [lps_off]"i"(H264_LPS_RANGE_OFFSET),
74 [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
75 [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
76 [cabac_mask]"r"(CABAC_MASK)
77 : "memory"
78 );
79
80 return (int)tmp0;
81 }
82
83 #define decode_significance_8x8 decode_significance_8x8_loongarch
decode_significance_8x8_loongarch( CABACContext *c, uint8_t *significant_coeff_ctx_base, int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off)84 static int decode_significance_8x8_loongarch(
85 CABACContext *c, uint8_t *significant_coeff_ctx_base,
86 int *index, uint8_t *last_coeff_ctx_base, const uint8_t *sig_off)
87 {
88 int64_t minusindex = 4 - (int64_t)index;
89 int64_t bit, tmp0, tmp1, tmp2, one = 1, end = 63, last = 0;
90 uint8_t *state = 0;
91 int64_t flag_offset = H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
92
93 __asm__ volatile(
94 "3: \n\t"
95 "ldx.bu %[tmp0], %[sig_off], %[last] \n\t"
96 "add.d %[state], %[tmp0], %[significant_coeff_ctx_base] \n\t"
97 #if UNCHECKED_BITSTREAM_READER
98 GET_CABAC_LOONGARCH_UNCBSR
99 #else
100 GET_CABAC_LOONGARCH
101 #endif
102 "blt %[bit], %[one], 4f \n\t"
103 "add.d %[tmp0], %[tables], %[flag_offset] \n\t"
104 "ldx.bu %[tmp1], %[tmp0], %[last] \n\t"
105 "add.d %[state], %[tmp1], %[last_coeff_ctx_base] \n\t"
106 #if UNCHECKED_BITSTREAM_READER
107 GET_CABAC_LOONGARCH_UNCBSR
108 #else
109 GET_CABAC_LOONGARCH
110 #endif
111 "st.w %[last], %[index], 0 \n\t"
112 "bge %[bit], %[one], 5f \n\t"
113 "addi.d %[index], %[index], 4 \n\t"
114 "4: \n\t"
115 "addi.d %[last], %[last], 1 \n\t"
116 "blt %[last], %[end], 3b \n\t"
117 "st.w %[last], %[index], 0 \n\t"
118 "5: \n\t"
119 "add.d %[tmp0], %[index], %[minusindex] \n\t"
120 "srli.d %[tmp0], %[tmp0], 2 \n\t"
121 : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1),
122 [tmp2]"=&r"(tmp2), [c_range]"+&r"(c->range),
123 [c_low]"+&r"(c->low), [state]"+&r"(state), [last]"+&r"(last),
124 [c_bytestream]"+&r"(c->bytestream), [index]"+&r"(index)
125 : [tables]"r"(ff_h264_cabac_tables), [end]"r"(end),
126 [one]"r"(one), [minusindex]"r"(minusindex),
127 [last_coeff_ctx_base]"r"(last_coeff_ctx_base),
128 [flag_offset]"r"(flag_offset),
129 #if !UNCHECKED_BITSTREAM_READER
130 [c_bytestream_end]"r"(c->bytestream_end),
131 #endif
132 [lps_off]"i"(H264_LPS_RANGE_OFFSET), [sig_off]"r"(sig_off),
133 [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
134 [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
135 [cabac_mask]"r"(CABAC_MASK),
136 [significant_coeff_ctx_base]"r"(significant_coeff_ctx_base)
137 );
138
139 return (int)tmp0;
140 }
141