Go to the documentation of this file.
21 #ifndef AVCODEC_X86_CABAC_H
22 #define AVCODEC_X86_CABAC_H
32 #if (defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
33 || ( !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)\
34 || (defined(__INTEL_COMPILER) && defined(_MSC_VER))
35 # define BROKEN_COMPILER 1
37 # define BROKEN_COMPILER 0
42 #ifndef UNCHECKED_BITSTREAM_READER
43 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
46 #if UNCHECKED_BITSTREAM_READER
47 #define END_CHECK(end) ""
49 #define END_CHECK(end) \
50 "cmp "end" , %%"FF_REG_c" \n\t"\
54 #ifdef BROKEN_RELOCATIONS
55 #define TABLES_ARG , "r"(tables)
58 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
59 "cmp "low" , "tmp" \n\t"\
60 "cmova %%ecx , "range" \n\t"\
61 "sbb %%rcx , %%rcx \n\t"\
62 "and %%ecx , "tmp" \n\t"\
63 "xor %%rcx , "retq" \n\t"\
64 "sub "tmp" , "low" \n\t"
66 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
68 "sub "low" , "tmp" \n\t"\
69 "sar $31 , "tmp" \n\t"\
70 "sub %%ecx , "range" \n\t"\
71 "and "tmp" , "range" \n\t"\
72 "add %%ecx , "range" \n\t"\
73 "shl $17 , %%ecx \n\t"\
74 "and "tmp" , %%ecx \n\t"\
75 "sub %%ecx , "low" \n\t"\
76 "xor "tmp" , "ret" \n\t"\
77 "movslq "ret" , "retq" \n\t"
80 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
81 "movzbl "statep" , "ret" \n\t"\
82 "mov "range" , "tmp" \n\t"\
83 "and $0xC0 , "range" \n\t"\
84 "lea ("ret", "range", 2), %%ecx \n\t"\
85 "movzbl "lps_off"("tables", %%rcx), "range" \n\t"\
86 "sub "range" , "tmp" \n\t"\
87 "mov "tmp" , %%ecx \n\t"\
88 "shl $17 , "tmp" \n\t"\
89 BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
90 "movzbl "norm_off"("tables", "rangeq"), %%ecx \n\t"\
91 "shl %%cl , "range" \n\t"\
92 "movzbl "mlps_off"+128("tables", "retq"), "tmp" \n\t"\
93 "shl %%cl , "low" \n\t"\
94 "mov "tmpbyte" , "statep" \n\t"\
95 "test "lowword" , "lowword" \n\t"\
97 "mov "byte" , %%"FF_REG_c" \n\t"\
99 "add"FF_OPSIZE" $2 , "byte" \n\t"\
101 "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
102 "lea -1("low") , %%ecx \n\t"\
103 "xor "low" , %%ecx \n\t"\
104 "shr $15 , %%ecx \n\t"\
106 "shr $15 , "tmp" \n\t"\
107 "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
108 "sub $0xFFFF , "tmp" \n\t"\
110 "add $7 , %%ecx \n\t"\
111 "shl %%cl , "tmp" \n\t"\
112 "add "tmp" , "low" \n\t"\
116 #define TABLES_ARG NAMED_CONSTRAINTS_ARRAY_ADD(ff_h264_cabac_tables)
120 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
121 "mov "tmp" , %%ecx \n\t"\
122 "shl $17 , "tmp" \n\t"\
123 "cmp "low" , "tmp" \n\t"\
124 "cmova %%ecx , "range" \n\t"\
125 "sbb %%ecx , %%ecx \n\t"\
126 "and %%ecx , "tmp" \n\t"\
127 "xor %%ecx , "ret" \n\t"\
128 "sub "tmp" , "low" \n\t"
130 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
131 "mov "tmp" , %%ecx \n\t"\
132 "shl $17 , "tmp" \n\t"\
133 "sub "low" , "tmp" \n\t"\
134 "sar $31 , "tmp" \n\t" \
135 "sub %%ecx , "range" \n\t" \
136 "and "tmp" , "range" \n\t" \
137 "add %%ecx , "range" \n\t" \
138 "shl $17 , %%ecx \n\t"\
139 "and "tmp" , %%ecx \n\t"\
140 "sub %%ecx , "low" \n\t"\
141 "xor "tmp" , "ret" \n\t"
144 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
145 "movzbl "statep" , "ret" \n\t"\
146 "mov "range" , "tmp" \n\t"\
147 "and $0xC0 , "range" \n\t"\
148 "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
149 "sub "range" , "tmp" \n\t"\
150 BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
151 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
152 "shl %%cl , "range" \n\t"\
153 "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
154 "shl %%cl , "low" \n\t"\
155 "mov "tmpbyte" , "statep" \n\t"\
156 "test "lowword" , "lowword" \n\t"\
158 "mov "byte" , %%"FF_REG_c" \n\t"\
160 "add"FF_OPSIZE" $2 , "byte" \n\t"\
162 "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
163 "lea -1("low") , %%ecx \n\t"\
164 "xor "low" , %%ecx \n\t"\
165 "shr $15 , %%ecx \n\t"\
167 "shr $15 , "tmp" \n\t"\
168 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
169 "sub $0xFFFF , "tmp" \n\t"\
171 "add $7 , %%ecx \n\t"\
172 "shl %%cl , "tmp" \n\t"\
173 "add "tmp" , "low" \n\t"\
178 #if HAVE_7REGS && !BROKEN_COMPILER
179 #define get_cabac_inline get_cabac_inline_x86
189 #ifdef BROKEN_RELOCATIONS
200 BRANCHLESS_GET_CABAC(
"%0",
"%q0",
"(%4)",
"%1",
"%w1",
201 "%2",
"%q2",
"%3",
"%b3",
202 "%c6(%5)",
"%c7(%5)",
207 :
"=&r"(
bit),
"=&r"(
c->low),
"=&r"(
c->range),
"=&q"(
tmp)
212 ,
"1"(
c->low),
"2"(
c->range)
213 :
"%"FF_REG_c,
"memory"
220 #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
225 "movl %c6(%2), %k1 \n\t"
226 "movl %c3(%2), %%eax \n\t"
228 "add %%eax, %%eax \n\t"
229 "sub %k1, %%eax \n\t"
231 "and %%edx, %k1 \n\t"
232 "add %k1, %%eax \n\t"
233 "xor %%edx, %%ecx \n\t"
234 "sub %%edx, %%ecx \n\t"
235 "test %%ax, %%ax \n\t"
237 "mov %c4(%2), %1 \n\t"
238 "subl $0xFFFF, %%eax \n\t"
239 "movzwl (%1), %%edx \n\t"
241 "shrl $15, %%edx \n\t"
242 #if UNCHECKED_BITSTREAM_READER
244 "addl %%edx, %%eax \n\t"
245 "mov %1, %c4(%2) \n\t"
247 "addl %%edx, %%eax \n\t"
248 "cmp %c5(%2), %1 \n\t"
250 "add"FF_OPSIZE
" $2, %c4(%2) \n\t"
253 "movl %%eax, %c3(%2) \n\t"
261 :
"%eax",
"%edx",
"memory"
266 #define get_cabac_bypass get_cabac_bypass_x86
272 "movl %c6(%2), %k1 \n\t"
273 "movl %c3(%2), %%eax \n\t"
275 "add %%eax, %%eax \n\t"
276 "sub %k1, %%eax \n\t"
278 "and %%edx, %k1 \n\t"
279 "add %k1, %%eax \n\t"
281 "test %%ax, %%ax \n\t"
283 "mov %c4(%2), %1 \n\t"
284 "subl $0xFFFF, %%eax \n\t"
285 "movzwl (%1), %%ecx \n\t"
287 "shrl $15, %%ecx \n\t"
288 "addl %%ecx, %%eax \n\t"
289 "cmp %c5(%2), %1 \n\t"
291 "add"FF_OPSIZE
" $2, %c4(%2) \n\t"
293 "movl %%eax, %c3(%2) \n\t"
295 :
"=&d"(res),
"=&r"(
tmp)
301 :
"%eax",
"%ecx",
"memory"
#define bit(string, value)
#define NAMED_CONSTRAINTS_ARRAY(...)
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
static double val(void *priv, double ch)
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
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
#define H264_LPS_RANGE_OFFSET
#define H264_NORM_SHIFT_OFFSET
__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")
#define H264_MLPS_STATE_OFFSET