FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/x86/cabac.h
Date: 2026-09-15 09:36:26
Exec Total Coverage
Lines: 11 11 100.0%
Functions: 3 3 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #ifndef AVCODEC_X86_CABAC_H
22 #define AVCODEC_X86_CABAC_H
23
24 #include <stddef.h>
25
26 #include "libavcodec/cabac.h"
27 #include "libavutil/attributes.h"
28 #include "libavutil/macros.h"
29 #include "libavutil/x86/asm.h"
30 #include "config.h"
31
32 #if defined(__INTEL_COMPILER) && defined(_MSC_VER)
33 # define BROKEN_COMPILER 1
34 #else
35 # define BROKEN_COMPILER 0
36 #endif
37
38 #if HAVE_INLINE_ASM
39
40 #ifndef UNCHECKED_BITSTREAM_READER
41 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
42 #endif
43
44 #if UNCHECKED_BITSTREAM_READER
45 #define END_CHECK(end) ""
46 #else
47 #define END_CHECK(end) \
48 "cmp "end" , %%"FF_REG_c" \n\t"\
49 "jge 1f \n\t"
50 #endif
51
52 #ifdef BROKEN_RELOCATIONS
53 #define TABLES_ARG , "r"(tables)
54
55 #if HAVE_FAST_CMOV
56 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
57 "cmp "low" , "tmp" \n\t"\
58 "cmova %%ecx , "range" \n\t"\
59 "sbb %%rcx , %%rcx \n\t"\
60 "and %%ecx , "tmp" \n\t"\
61 "xor %%rcx , "retq" \n\t"\
62 "sub "tmp" , "low" \n\t"
63 #else /* HAVE_FAST_CMOV */
64 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
65 /* P4 Prescott has crappy cmov,sbb,64-bit shift so avoid them */ \
66 "sub "low" , "tmp" \n\t"\
67 "sar $31 , "tmp" \n\t"\
68 "sub %%ecx , "range" \n\t"\
69 "and "tmp" , "range" \n\t"\
70 "add %%ecx , "range" \n\t"\
71 "shl $17 , %%ecx \n\t"\
72 "and "tmp" , %%ecx \n\t"\
73 "sub %%ecx , "low" \n\t"\
74 "xor "tmp" , "ret" \n\t"\
75 "movslq "ret" , "retq" \n\t"
76 #endif /* HAVE_FAST_CMOV */
77
78 #ifdef __LZCNT__
79 /* renormalisation shift = lzcnt(range) - 23, refill shift = tzcnt(low) - 16 */
80 #define BRANCHLESS_GET_CABAC_RENORM(range, rangeq, norm_off, tables) \
81 "lzcnt "range" , %%ecx \n\t"\
82 "sub $23 , %%ecx \n\t"
83 #define BRANCHLESS_GET_CABAC_REFILL(low, tmp, norm_off, tables) \
84 "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
85 "tzcnt "low" , %%ecx \n\t"\
86 "bswap "tmp" \n\t"\
87 "shr $15 , "tmp" \n\t"\
88 "sub $16 , %%ecx \n\t"\
89 "sub $0xFFFF , "tmp" \n\t"\
90 "shl %%cl , "tmp" \n\t"\
91 "add "tmp" , "low" \n\t"
92 #else /* __LZCNT__ */
93 #define BRANCHLESS_GET_CABAC_RENORM(range, rangeq, norm_off, tables) \
94 "movzbl "norm_off"("tables", "rangeq"), %%ecx \n\t"
95 #define BRANCHLESS_GET_CABAC_REFILL(low, tmp, norm_off, tables) \
96 "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
97 "lea -1("low") , %%ecx \n\t"\
98 "xor "low" , %%ecx \n\t"\
99 "shr $15 , %%ecx \n\t"\
100 "bswap "tmp" \n\t"\
101 "shr $15 , "tmp" \n\t"\
102 "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
103 "sub $0xFFFF , "tmp" \n\t"\
104 "neg %%ecx \n\t"\
105 "add $7 , %%ecx \n\t"\
106 "shl %%cl , "tmp" \n\t"\
107 "add "tmp" , "low" \n\t"
108 #endif /* __LZCNT__ */
109
110 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
111 "movzbl "statep" , "ret" \n\t"\
112 "mov "range" , "tmp" \n\t"\
113 "and $0xC0 , "range" \n\t"\
114 "lea ("ret", "range", 2), %%ecx \n\t"\
115 "movzbl "lps_off"("tables", %%rcx), "range" \n\t"\
116 "sub "range" , "tmp" \n\t"\
117 "mov "tmp" , %%ecx \n\t"\
118 "shl $17 , "tmp" \n\t"\
119 BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
120 BRANCHLESS_GET_CABAC_RENORM(range, rangeq, norm_off, tables) \
121 "shl %%cl , "range" \n\t"\
122 "movzbl "mlps_off"+128("tables", "retq"), "tmp" \n\t"\
123 "shl %%cl , "low" \n\t"\
124 "mov "tmpbyte" , "statep" \n\t"\
125 "test "lowword" , "lowword" \n\t"\
126 "jnz 2f \n\t"\
127 "mov "byte" , %%"FF_REG_c" \n\t"\
128 END_CHECK(end)\
129 "add"FF_OPSIZE" $2 , "byte" \n\t"\
130 "1: \n\t"\
131 BRANCHLESS_GET_CABAC_REFILL(low, tmp, norm_off, tables) \
132 "2: \n\t"
133
134 #else /* BROKEN_RELOCATIONS */
135 #define TABLES_ARG NAMED_CONSTRAINTS_ARRAY_ADD(ff_h264_cabac_tables)
136 #define RIP_ARG
137
138 #if HAVE_FAST_CMOV
139 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
140 "mov "tmp" , %%ecx \n\t"\
141 "shl $17 , "tmp" \n\t"\
142 "cmp "low" , "tmp" \n\t"\
143 "cmova %%ecx , "range" \n\t"\
144 "sbb %%ecx , %%ecx \n\t"\
145 "and %%ecx , "tmp" \n\t"\
146 "xor %%ecx , "ret" \n\t"\
147 "sub "tmp" , "low" \n\t"
148 #else /* HAVE_FAST_CMOV */
149 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
150 "mov "tmp" , %%ecx \n\t"\
151 "shl $17 , "tmp" \n\t"\
152 "sub "low" , "tmp" \n\t"\
153 "sar $31 , "tmp" \n\t" /*lps_mask*/\
154 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
155 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
156 "add %%ecx , "range" \n\t" /*new range*/\
157 "shl $17 , %%ecx \n\t"\
158 "and "tmp" , %%ecx \n\t"\
159 "sub %%ecx , "low" \n\t"\
160 "xor "tmp" , "ret" \n\t"
161 #endif /* HAVE_FAST_CMOV */
162
163 #ifdef __LZCNT__
164 #define BRANCHLESS_GET_CABAC_RENORM(range, rangeq, norm_off, tables) \
165 "lzcnt "range" , %%ecx \n\t"\
166 "sub $23 , %%ecx \n\t"
167 #define BRANCHLESS_GET_CABAC_REFILL(low, tmp, norm_off, tables) \
168 "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
169 "tzcnt "low" , %%ecx \n\t"\
170 "bswap "tmp" \n\t"\
171 "shr $15 , "tmp" \n\t"\
172 "sub $16 , %%ecx \n\t"\
173 "sub $0xFFFF , "tmp" \n\t"\
174 "shl %%cl , "tmp" \n\t"\
175 "add "tmp" , "low" \n\t"
176 #else /* __LZCNT__ */
177 #define BRANCHLESS_GET_CABAC_RENORM(range, rangeq, norm_off, tables) \
178 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"
179 #define BRANCHLESS_GET_CABAC_REFILL(low, tmp, norm_off, tables) \
180 "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
181 "lea -1("low") , %%ecx \n\t"\
182 "xor "low" , %%ecx \n\t"\
183 "shr $15 , %%ecx \n\t"\
184 "bswap "tmp" \n\t"\
185 "shr $15 , "tmp" \n\t"\
186 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
187 "sub $0xFFFF , "tmp" \n\t"\
188 "neg %%ecx \n\t"\
189 "add $7 , %%ecx \n\t"\
190 "shl %%cl , "tmp" \n\t"\
191 "add "tmp" , "low" \n\t"
192 #endif /* __LZCNT__ */
193
194 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
195 "movzbl "statep" , "ret" \n\t"\
196 "mov "range" , "tmp" \n\t"\
197 "and $0xC0 , "range" \n\t"\
198 "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
199 "sub "range" , "tmp" \n\t"\
200 BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
201 BRANCHLESS_GET_CABAC_RENORM(range, rangeq, norm_off, tables) \
202 "shl %%cl , "range" \n\t"\
203 "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
204 "shl %%cl , "low" \n\t"\
205 "mov "tmpbyte" , "statep" \n\t"\
206 "test "lowword" , "lowword" \n\t"\
207 " jnz 2f \n\t"\
208 "mov "byte" , %%"FF_REG_c" \n\t"\
209 END_CHECK(end)\
210 "add"FF_OPSIZE" $2 , "byte" \n\t"\
211 "1: \n\t"\
212 BRANCHLESS_GET_CABAC_REFILL(low, tmp, norm_off, tables) \
213 "2: \n\t"
214
215 #endif /* BROKEN_RELOCATIONS */
216
217 #if HAVE_X86_7REGS && !BROKEN_COMPILER
218 #define get_cabac_inline get_cabac_inline_x86
219 static
220 #if ARCH_X86_32
221 av_noinline
222 #else
223 av_always_inline
224 #endif
225 1190408538 int get_cabac_inline_x86(CABACContext *c, uint8_t *const state)
226 {
227 int bit, tmp;
228 #ifdef BROKEN_RELOCATIONS
229 void *tables;
230
231 1190408538 __asm__ volatile(
232 "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
233 : "=&r"(tables)
234 : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
235 );
236 #endif
237
238 1190408538 __asm__ volatile(
239 BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
240 "%2", "%q2", "%3", "%b3",
241 "%c6(%5)", "%c7(%5)",
242 AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
243 AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
244 AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
245 "%8")
246 : "=&r"(bit), "=&r"(c->low), "=&r"(c->range), "=&q"(tmp)
247 : "r"(state), "r"(c),
248 "i"(offsetof(CABACContext, bytestream)),
249 "i"(offsetof(CABACContext, bytestream_end))
250 TABLES_ARG
251 1190408538 ,"1"(c->low), "2"(c->range)
252 : "%"FF_REG_c, "memory"
253 );
254 1190408538 return bit & 1;
255 }
256 #endif /* HAVE_X86_7REGS && !BROKEN_COMPILER */
257
258 #if !BROKEN_COMPILER
259 #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
260 182211427 static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
261 {
262 x86_reg tmp;
263 182211427 __asm__ volatile(
264 "movl %c6(%2), %k1 \n\t"
265 "movl %c3(%2), %%eax \n\t"
266 "shl $17, %k1 \n\t"
267 "add %%eax, %%eax \n\t"
268 "sub %k1, %%eax \n\t"
269 "cdq \n\t"
270 "and %%edx, %k1 \n\t"
271 "add %k1, %%eax \n\t"
272 "xor %%edx, %%ecx \n\t"
273 "sub %%edx, %%ecx \n\t"
274 "test %%ax, %%ax \n\t"
275 "jnz 1f \n\t"
276 "mov %c4(%2), %1 \n\t"
277 "subl $0xFFFF, %%eax \n\t"
278 "movzwl (%1), %%edx \n\t"
279 "bswap %%edx \n\t"
280 "shrl $15, %%edx \n\t"
281 #if UNCHECKED_BITSTREAM_READER
282 "add $2, %1 \n\t"
283 "addl %%edx, %%eax \n\t"
284 "mov %1, %c4(%2) \n\t"
285 #else
286 "addl %%edx, %%eax \n\t"
287 "cmp %c5(%2), %1 \n\t"
288 "jge 1f \n\t"
289 "add"FF_OPSIZE" $2, %c4(%2) \n\t"
290 #endif
291 "1: \n\t"
292 "movl %%eax, %c3(%2) \n\t"
293
294 : "+c"(val), "=&r"(tmp)
295 : "r"(c),
296 "i"(offsetof(CABACContext, low)),
297 "i"(offsetof(CABACContext, bytestream)),
298 "i"(offsetof(CABACContext, bytestream_end)),
299 "i"(offsetof(CABACContext, range))
300 : "%eax", "%edx", "memory"
301 );
302 182211427 return val;
303 }
304
305 #define get_cabac_bypass get_cabac_bypass_x86
306 165119810 static av_always_inline int get_cabac_bypass_x86(CABACContext *c)
307 {
308 x86_reg tmp;
309 int res;
310 165119810 __asm__ volatile(
311 "movl %c6(%2), %k1 \n\t"
312 "movl %c3(%2), %%eax \n\t"
313 "shl $17, %k1 \n\t"
314 "add %%eax, %%eax \n\t"
315 "sub %k1, %%eax \n\t"
316 "cdq \n\t"
317 "and %%edx, %k1 \n\t"
318 "add %k1, %%eax \n\t"
319 "inc %%edx \n\t"
320 "test %%ax, %%ax \n\t"
321 "jnz 1f \n\t"
322 "mov %c4(%2), %1 \n\t"
323 "subl $0xFFFF, %%eax \n\t"
324 "movzwl (%1), %%ecx \n\t"
325 "bswap %%ecx \n\t"
326 "shrl $15, %%ecx \n\t"
327 "addl %%ecx, %%eax \n\t"
328 "cmp %c5(%2), %1 \n\t"
329 "jge 1f \n\t"
330 "add"FF_OPSIZE" $2, %c4(%2) \n\t"
331 "1: \n\t"
332 "movl %%eax, %c3(%2) \n\t"
333
334 : "=&d"(res), "=&r"(tmp)
335 : "r"(c),
336 "i"(offsetof(CABACContext, low)),
337 "i"(offsetof(CABACContext, bytestream)),
338 "i"(offsetof(CABACContext, bytestream_end)),
339 "i"(offsetof(CABACContext, range))
340 : "%eax", "%ecx", "memory"
341 );
342 165119810 return res;
343 }
344 #endif /* !BROKEN_COMPILER */
345
346 #endif /* HAVE_INLINE_ASM */
347 #endif /* AVCODEC_X86_CABAC_H */
348