2 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVCODEC_X86_CABAC_H
22 #define AVCODEC_X86_CABAC_H
24 #include "libavcodec/cabac.h"
25 #include "libavutil/attributes.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavutil/internal.h"
30 #if (defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
31 || ( !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)
32 # define BROKEN_COMPILER 1
34 # define BROKEN_COMPILER 0
39 #ifndef UNCHECKED_BITSTREAM_READER
40 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
43 #if UNCHECKED_BITSTREAM_READER
44 #define END_CHECK(end) ""
46 #define END_CHECK(end) \
47 "cmp "end" , %%"REG_c" \n\t"\
51 #ifdef BROKEN_RELOCATIONS
52 #define TABLES_ARG , "r"(tables)
55 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
56 "cmp "low" , "tmp" \n\t"\
57 "cmova %%ecx , "range" \n\t"\
58 "sbb %%rcx , %%rcx \n\t"\
59 "and %%ecx , "tmp" \n\t"\
60 "xor %%rcx , "retq" \n\t"\
61 "sub "tmp" , "low" \n\t"
62 #else /* HAVE_FAST_CMOV */
63 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
64 /* P4 Prescott has crappy cmov,sbb,64bit shift so avoid them */ \
65 "sub "low" , "tmp" \n\t"\
66 "sar $31 , "tmp" \n\t"\
67 "sub %%ecx , "range" \n\t"\
68 "and "tmp" , "range" \n\t"\
69 "add %%ecx , "range" \n\t"\
70 "shl $17 , %%ecx \n\t"\
71 "and "tmp" , %%ecx \n\t"\
72 "sub %%ecx , "low" \n\t"\
73 "xor "tmp" , "ret" \n\t"\
74 "movslq "ret" , "retq" \n\t"
75 #endif /* HAVE_FAST_CMOV */
77 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
78 "movzbl "statep" , "ret" \n\t"\
79 "mov "range" , "tmp" \n\t"\
80 "and $0xC0 , "range" \n\t"\
81 "lea ("ret", "range", 2), %%ecx \n\t"\
82 "movzbl "lps_off"("tables", %%rcx), "range" \n\t"\
83 "sub "range" , "tmp" \n\t"\
84 "mov "tmp" , %%ecx \n\t"\
85 "shl $17 , "tmp" \n\t"\
86 BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
87 "movzbl "norm_off"("tables", "rangeq"), %%ecx \n\t"\
88 "shl %%cl , "range" \n\t"\
89 "movzbl "mlps_off"+128("tables", "retq"), "tmp" \n\t"\
90 "shl %%cl , "low" \n\t"\
91 "mov "tmpbyte" , "statep" \n\t"\
92 "test "lowword" , "lowword" \n\t"\
94 "mov "byte" , %%"REG_c" \n\t"\
96 "add"OPSIZE" $2 , "byte" \n\t"\
98 "movzwl (%%"REG_c") , "tmp" \n\t"\
99 "lea -1("low") , %%ecx \n\t"\
100 "xor "low" , %%ecx \n\t"\
101 "shr $15 , %%ecx \n\t"\
103 "shr $15 , "tmp" \n\t"\
104 "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
105 "sub $0xFFFF , "tmp" \n\t"\
107 "add $7 , %%ecx \n\t"\
108 "shl %%cl , "tmp" \n\t"\
109 "add "tmp" , "low" \n\t"\
112 #else /* BROKEN_RELOCATIONS */
117 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
118 "mov "tmp" , %%ecx \n\t"\
119 "shl $17 , "tmp" \n\t"\
120 "cmp "low" , "tmp" \n\t"\
121 "cmova %%ecx , "range" \n\t"\
122 "sbb %%ecx , %%ecx \n\t"\
123 "and %%ecx , "tmp" \n\t"\
124 "xor %%ecx , "ret" \n\t"\
125 "sub "tmp" , "low" \n\t"
126 #else /* HAVE_FAST_CMOV */
127 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
128 "mov "tmp" , %%ecx \n\t"\
129 "shl $17 , "tmp" \n\t"\
130 "sub "low" , "tmp" \n\t"\
131 "sar $31 , "tmp" \n\t" /*lps_mask*/\
132 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
133 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
134 "add %%ecx , "range" \n\t" /*new range*/\
135 "shl $17 , %%ecx \n\t"\
136 "and "tmp" , %%ecx \n\t"\
137 "sub %%ecx , "low" \n\t"\
138 "xor "tmp" , "ret" \n\t"
139 #endif /* HAVE_FAST_CMOV */
141 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
142 "movzbl "statep" , "ret" \n\t"\
143 "mov "range" , "tmp" \n\t"\
144 "and $0xC0 , "range" \n\t"\
145 "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
146 "sub "range" , "tmp" \n\t"\
147 BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
148 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
149 "shl %%cl , "range" \n\t"\
150 "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
151 "shl %%cl , "low" \n\t"\
152 "mov "tmpbyte" , "statep" \n\t"\
153 "test "lowword" , "lowword" \n\t"\
155 "mov "byte" , %%"REG_c" \n\t"\
157 "add"OPSIZE" $2 , "byte" \n\t"\
159 "movzwl (%%"REG_c") , "tmp" \n\t"\
160 "lea -1("low") , %%ecx \n\t"\
161 "xor "low" , %%ecx \n\t"\
162 "shr $15 , %%ecx \n\t"\
164 "shr $15 , "tmp" \n\t"\
165 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
166 "sub $0xFFFF , "tmp" \n\t"\
168 "add $7 , %%ecx \n\t"\
169 "shl %%cl , "tmp" \n\t"\
170 "add "tmp" , "low" \n\t"\
173 #endif /* BROKEN_RELOCATIONS */
175 #if HAVE_7REGS && !BROKEN_COMPILER
176 #define get_cabac_inline get_cabac_inline_x86
177 static av_always_inline int get_cabac_inline_x86(CABACContext *c,
178 uint8_t *const state)
181 #ifdef BROKEN_RELOCATIONS
185 "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
191 BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
192 "%2", "%q2", "%3", "%b3",
193 "%c6(%5)", "%c7(%5)",
194 AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
195 AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
196 AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
198 : "=&r"(bit), "=&r"(c->low), "=&r"(c->range), "=&q"(tmp)
199 : "r"(state), "r"(c),
200 "i"(offsetof(CABACContext, bytestream)),
201 "i"(offsetof(CABACContext, bytestream_end))
203 ,"1"(c->low), "2"(c->range)
208 #endif /* HAVE_7REGS */
211 #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
212 static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
216 "movl %c6(%2), %k1 \n\t"
217 "movl %c3(%2), %%eax \n\t"
219 "add %%eax, %%eax \n\t"
220 "sub %k1, %%eax \n\t"
222 "and %%edx, %k1 \n\t"
223 "add %k1, %%eax \n\t"
224 "xor %%edx, %%ecx \n\t"
225 "sub %%edx, %%ecx \n\t"
226 "test %%ax, %%ax \n\t"
228 "mov %c4(%2), %1 \n\t"
229 "subl $0xFFFF, %%eax \n\t"
230 "movzwl (%1), %%edx \n\t"
232 "shrl $15, %%edx \n\t"
233 #if UNCHECKED_BITSTREAM_READER
235 "addl %%edx, %%eax \n\t"
236 "mov %1, %c4(%2) \n\t"
238 "addl %%edx, %%eax \n\t"
239 "cmp %c5(%2), %1 \n\t"
241 "add"OPSIZE" $2, %c4(%2) \n\t"
244 "movl %%eax, %c3(%2) \n\t"
246 : "+c"(val), "=&r"(tmp)
248 "i"(offsetof(CABACContext, low)),
249 "i"(offsetof(CABACContext, bytestream)),
250 "i"(offsetof(CABACContext, bytestream_end)),
251 "i"(offsetof(CABACContext, range))
252 : "%eax", "%edx", "memory"
257 #define get_cabac_bypass get_cabac_bypass_x86
258 static av_always_inline int get_cabac_bypass_x86(CABACContext *c)
263 "movl %c6(%2), %k1 \n\t"
264 "movl %c3(%2), %%eax \n\t"
266 "add %%eax, %%eax \n\t"
267 "sub %k1, %%eax \n\t"
269 "and %%edx, %k1 \n\t"
270 "add %k1, %%eax \n\t"
272 "test %%ax, %%ax \n\t"
274 "mov %c4(%2), %1 \n\t"
275 "subl $0xFFFF, %%eax \n\t"
276 "movzwl (%1), %%ecx \n\t"
278 "shrl $15, %%ecx \n\t"
279 "addl %%ecx, %%eax \n\t"
280 "cmp %c5(%2), %1 \n\t"
282 "add"OPSIZE" $2, %c4(%2) \n\t"
284 "movl %%eax, %c3(%2) \n\t"
286 : "=&d"(res), "=&r"(tmp)
288 "i"(offsetof(CABACContext, low)),
289 "i"(offsetof(CABACContext, bytestream)),
290 "i"(offsetof(CABACContext, bytestream_end)),
291 "i"(offsetof(CABACContext, range))
292 : "%eax", "%ecx", "memory"
296 #endif /* !BROKEN_COMPILER */
298 #endif /* HAVE_INLINE_ASM */
299 #endif /* AVCODEC_X86_CABAC_H */