FFmpeg
cabac.h
Go to the documentation of this file.
1 /*
2  * Loongson SIMD optimized h264chroma
3  *
4  * Copyright (c) 2018 Loongson Technology Corporation Limited
5  * Contributed by Shiyou Yin <yinshiyou-hf@loongson.cn>
6  * Gu Xiwei(guxiwei-hf@loongson.cn)
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #ifndef AVCODEC_MIPS_CABAC_H
26 #define AVCODEC_MIPS_CABAC_H
27 
28 #include "libavutil/attributes.h"
29 #include "libavcodec/cabac.h"
31 #include "config.h"
32 
33 #if !HAVE_MIPS32R6 && !HAVE_MIPS64R6
34 #define get_cabac_inline get_cabac_inline_mips
36  uint8_t * const state){
37  mips_reg tmp0, tmp1, tmp2, bit;
38 
39  __asm__ volatile (
40  "lbu %[bit], 0(%[state]) \n\t"
41  "and %[tmp0], %[c_range], 0xC0 \n\t"
42  PTR_SLL "%[tmp0], %[tmp0], 0x01 \n\t"
43  PTR_ADDU "%[tmp0], %[tmp0], %[tables] \n\t"
44  PTR_ADDU "%[tmp0], %[tmp0], %[bit] \n\t"
45  /* tmp1: RangeLPS */
46  "lbu %[tmp1], %[lps_off](%[tmp0]) \n\t"
47 
48  PTR_SUBU "%[c_range], %[c_range], %[tmp1] \n\t"
49  PTR_SLL "%[tmp0], %[c_range], 0x11 \n\t"
50  "slt %[tmp2], %[tmp0], %[c_low] \n\t"
51  "beqz %[tmp2], 1f \n\t"
52  "move %[c_range], %[tmp1] \n\t"
53  "not %[bit], %[bit] \n\t"
54  PTR_SUBU "%[c_low], %[c_low], %[tmp0] \n\t"
55 
56  "1: \n\t"
57  /* tmp1: *state */
58  PTR_ADDU "%[tmp0], %[tables], %[bit] \n\t"
59  "lbu %[tmp1], %[mlps_off](%[tmp0]) \n\t"
60  /* tmp2: lps_mask */
61  PTR_ADDU "%[tmp0], %[tables], %[c_range] \n\t"
62  "lbu %[tmp2], %[norm_off](%[tmp0]) \n\t"
63 
64  "sb %[tmp1], 0(%[state]) \n\t"
65  "and %[bit], %[bit], 0x01 \n\t"
66  PTR_SLL "%[c_range], %[c_range], %[tmp2] \n\t"
67  PTR_SLL "%[c_low], %[c_low], %[tmp2] \n\t"
68 
69  "and %[tmp1], %[c_low], %[cabac_mask] \n\t"
70  "bnez %[tmp1], 1f \n\t"
71  PTR_ADDIU "%[tmp0], %[c_low], -0X01 \n\t"
72  "xor %[tmp0], %[c_low], %[tmp0] \n\t"
73  PTR_SRA "%[tmp0], %[tmp0], 0x0f \n\t"
74  PTR_ADDU "%[tmp0], %[tmp0], %[tables] \n\t"
75  /* tmp2: ff_h264_norm_shift[x >> (CABAC_BITS - 1)] */
76  "lbu %[tmp2], %[norm_off](%[tmp0]) \n\t"
77 #if HAVE_BIGENDIAN
78  "lhu %[tmp0], 0(%[c_bytestream]) \n\t"
79 #else
80  "lhu %[tmp0], 0(%[c_bytestream]) \n\t"
81 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
82  "wsbh %[tmp0], %[tmp0] \n\t"
83 #else
84  "and %[tmp1], %[tmp0], 0xff00ff00 \n\t"
85  "srl %[tmp1], %[tmp1], 8 \n\t"
86  "and %[tmp0], %[tmp0], 0x00ff00ff \n\t"
87  "sll %[tmp0], %[tmp0], 8 \n\t"
88  "or %[tmp0], %[tmp0], %[tmp1] \n\t"
89 #endif
90 #endif
91  PTR_SLL "%[tmp0], %[tmp0], 0x01 \n\t"
92  PTR_SUBU "%[tmp0], %[tmp0], %[cabac_mask] \n\t"
93 
94  "li %[tmp1], 0x07 \n\t"
95  PTR_SUBU "%[tmp1], %[tmp1], %[tmp2] \n\t"
96  PTR_SLL "%[tmp0], %[tmp0], %[tmp1] \n\t"
97  PTR_ADDU "%[c_low], %[c_low], %[tmp0] \n\t"
98 
99 #if UNCHECKED_BITSTREAM_READER
100  PTR_ADDIU "%[c_bytestream], %[c_bytestream], 0x02 \n\t"
101 #else
102  "slt %[tmp0], %[c_bytestream], %[c_bytestream_end] \n\t"
103  PTR_ADDIU "%[tmp2], %[c_bytestream], 0x02 \n\t"
104 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
105  "movn %[c_bytestream], %[tmp2], %[tmp0] \n\t"
106 #else
107  "beqz %[tmp0], 2f \n\t"
108  " nop \n\t"
109  "move %[c_bytestream], %[tmp2] \n\t"
110  "2: \n\t"
111 #endif
112 #endif
113  "1: \n\t"
114  : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2),
115  [c_range]"+&r"(c->range), [c_low]"+&r"(c->low),
116  [c_bytestream]"+&r"(c->bytestream)
117  : [state]"r"(state), [tables]"r"(ff_h264_cabac_tables),
119  [c_bytestream_end]"r"(c->bytestream_end),
120 #endif
121  [lps_off]"i"(H264_LPS_RANGE_OFFSET),
122  [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128),
123  [norm_off]"i"(H264_NORM_SHIFT_OFFSET),
124  [cabac_mask]"r"(CABAC_MASK)
125  : "memory"
126  );
127 
128  return bit;
129 }
130 
131 #define get_cabac_bypass get_cabac_bypass_mips
133 {
134  mips_reg tmp0, tmp1;
135  int res = 0;
136  __asm__ volatile(
137  PTR_SLL "%[c_low], %[c_low], 0x01 \n\t"
138  "and %[tmp0], %[c_low], %[cabac_mask] \n\t"
139  "bnez %[tmp0], 1f \n\t"
140 #if HAVE_BIGENDIAN
141  "lhu %[tmp1], 0(%[c_bytestream]) \n\t"
142 #else
143  "lhu %[tmp1], 0(%[c_bytestream]) \n\t"
144 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
145  "wsbh %[tmp1], %[tmp1] \n\t"
146 #else
147  "and %[tmp0], %[tmp1], 0xff00ff00 \n\t"
148  "srl %[tmp0], %[tmp0], 8 \n\t"
149  "and %[tmp1], %[tmp1], 0x00ff00ff \n\t"
150  "sll %[tmp1], %[tmp1], 8 \n\t"
151  "or %[tmp1], %[tmp1], %[tmp0] \n\t"
152 #endif
153 #endif
154  PTR_SLL "%[tmp1], %[tmp1], 0x01 \n\t"
155  PTR_SUBU "%[tmp1], %[tmp1], %[cabac_mask] \n\t"
156  PTR_ADDU "%[c_low], %[c_low], %[tmp1] \n\t"
157 #if UNCHECKED_BITSTREAM_READER
158  PTR_ADDIU "%[c_bytestream], %[c_bytestream], 0x02 \n\t"
159 #else
160  "slt %[tmp0], %[c_bytestream], %[c_bytestream_end] \n\t"
161  PTR_ADDIU "%[tmp1], %[c_bytestream], 0x02 \n\t"
162 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
163  "movn %[c_bytestream], %[tmp1], %[tmp0] \n\t"
164 #else
165  "beqz %[tmp0], 2f \n\t"
166  " nop \n\t"
167  "move %[c_bytestream], %[tmp1] \n\t"
168  "2: \n\t"
169 #endif
170 #endif
171  "1: \n\t"
172  PTR_SLL "%[tmp1], %[c_range], 0x11 \n\t"
173  "slt %[tmp0], %[c_low], %[tmp1] \n\t"
174  PTR_SUBU "%[tmp1], %[c_low], %[tmp1] \n\t"
175 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
176  "movz %[res], %[one], %[tmp0] \n\t"
177  "movz %[c_low], %[tmp1], %[tmp0] \n\t"
178 #else
179  "bnez %[tmp0], 2f \n\t"
180  " nop \n\t"
181  "move %[res], %[one] \n\t"
182  "move %[c_low], %[tmp1] \n\t"
183  "2: \n\t"
184 #endif
185  : [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [res]"+&r"(res),
186  [c_range]"+&r"(c->range), [c_low]"+&r"(c->low),
187  [c_bytestream]"+&r"(c->bytestream)
188  : [cabac_mask]"r"(CABAC_MASK),
190  [c_bytestream_end]"r"(c->bytestream_end),
191 #endif
192  [one]"r"(0x01)
193  : "memory"
194  );
195  return res;
196 }
197 
198 #define get_cabac_bypass_sign get_cabac_bypass_sign_mips
200 {
201  mips_reg tmp0, tmp1;
202  int res = val;
203  __asm__ volatile(
204  PTR_SLL "%[c_low], %[c_low], 0x01 \n\t"
205  "and %[tmp0], %[c_low], %[cabac_mask] \n\t"
206  "bnez %[tmp0], 1f \n\t"
207 #if HAVE_BIGENDIAN
208  "lhu %[tmp1], 0(%[c_bytestream]) \n\t"
209 #else
210  "lhu %[tmp1], 0(%[c_bytestream]) \n\t"
211 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
212  "wsbh %[tmp1], %[tmp1] \n\t"
213 #else
214  "and %[tmp0], %[tmp1], 0xff00ff00 \n\t"
215  "srl %[tmp0], %[tmp0], 8 \n\t"
216  "and %[tmp1], %[tmp1], 0x00ff00ff \n\t"
217  "sll %[tmp1], %[tmp1], 8 \n\t"
218  "or %[tmp1], %[tmp1], %[tmp0] \n\t"
219 #endif
220 #endif
221  PTR_SLL "%[tmp1], %[tmp1], 0x01 \n\t"
222  PTR_SUBU "%[tmp1], %[tmp1], %[cabac_mask] \n\t"
223  PTR_ADDU "%[c_low], %[c_low], %[tmp1] \n\t"
224 #if UNCHECKED_BITSTREAM_READER
225  PTR_ADDIU "%[c_bytestream], %[c_bytestream], 0x02 \n\t"
226 #else
227  "slt %[tmp0], %[c_bytestream], %[c_bytestream_end] \n\t"
228  PTR_ADDIU "%[tmp1], %[c_bytestream], 0x02 \n\t"
229 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
230  "movn %[c_bytestream], %[tmp1], %[tmp0] \n\t"
231 #else
232  "beqz %[tmp0], 2f \n\t"
233  " nop \n\t"
234  "move %[c_bytestream], %[tmp1] \n\t"
235  "2: \n\t"
236 #endif
237 #endif
238  "1: \n\t"
239  PTR_SLL "%[tmp1], %[c_range], 0x11 \n\t"
240  "slt %[tmp0], %[c_low], %[tmp1] \n\t"
241  PTR_SUBU "%[tmp1], %[c_low], %[tmp1] \n\t"
242 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
243  "movz %[c_low], %[tmp1], %[tmp0] \n\t"
244 #else
245  "bnez %[tmp0], 2f \n\t"
246  " nop \n\t"
247  "move %[c_low], %[tmp1] \n\t"
248  "2: \n\t"
249 #endif
250  PTR_SUBU "%[tmp1], %[zero], %[res] \n\t"
251 #if HAVE_MIPS32R2 || HAVE_MIPS64R2
252  "movn %[res], %[tmp1], %[tmp0] \n\t"
253 #else
254  "beqz %[tmp0], 3f \n\t"
255  " nop \n\t"
256  "move %[res], %[tmp1] \n\t"
257  "3: \n\t"
258 #endif
259  : [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [res]"+&r"(res),
260  [c_range]"+&r"(c->range), [c_low]"+&r"(c->low),
261  [c_bytestream]"+&r"(c->bytestream)
262  : [cabac_mask]"r"(CABAC_MASK),
264  [c_bytestream_end]"r"(c->bytestream_end),
265 #endif
266  [zero]"r"(0x0)
267  : "memory"
268  );
269 
270  return res;
271 }
272 #endif /* !HAVE_MIPS32R6 && !HAVE_MIPS64R6 */
273 #endif /* AVCODEC_MIPS_CABAC_H */
cabac.h
PTR_SLL
#define PTR_SLL
Definition: asmdefs.h:57
get_cabac_bypass_sign_mips
static av_always_inline int get_cabac_bypass_sign_mips(CABACContext *c, int val)
Definition: cabac.h:199
mips_reg
#define mips_reg
Definition: asmdefs.h:46
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
av_always_inline
#define av_always_inline
Definition: attributes.h:76
tables
Writing a table generator This documentation is preliminary Parts of the API are not good and should be changed Basic concepts A table generator consists of two *_tablegen c and *_tablegen h The h file will provide the variable declarations and initialization code for the tables
Definition: tablegen.txt:10
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_h264_cabac_tables
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
mmiutils.h
CABAC_MASK
#define CABAC_MASK
Definition: cabac.h:39
get_cabac_bypass_mips
static av_always_inline int get_cabac_bypass_mips(CABACContext *c)
Definition: cabac.h:132
attributes.h
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
H264_LPS_RANGE_OFFSET
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:34
PTR_SUBU
#define PTR_SUBU
Definition: asmdefs.h:52
zero
static int zero(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:121
state
static struct @593 state
UNCHECKED_BITSTREAM_READER
#define UNCHECKED_BITSTREAM_READER
Definition: bitstream.h:57
H264_NORM_SHIFT_OFFSET
#define H264_NORM_SHIFT_OFFSET
Definition: cabac.h:33
__asm__
__asm__(".macro parse_r var r\n\t" "\\var = -1\n\t" _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) ".iflt \\var\n\t" ".error \"Unable to parse register name \\r\"\n\t" ".endif\n\t" ".endm")
PTR_ADDU
#define PTR_ADDU
Definition: asmdefs.h:49
get_cabac_inline_mips
static av_always_inline int get_cabac_inline_mips(CABACContext *c, uint8_t *const state)
Definition: cabac.h:35
PTR_ADDIU
#define PTR_ADDIU
Definition: asmdefs.h:50
H264_MLPS_STATE_OFFSET
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:35
CABACContext
Definition: cabac.h:41
PTR_SRA
#define PTR_SRA
Definition: asmdefs.h:55