00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef AVCODEC_X86_CABAC_H
00022 #define AVCODEC_X86_CABAC_H
00023
00024 #include "libavcodec/cabac.h"
00025 #include "libavutil/attributes.h"
00026 #include "libavutil/x86/asm.h"
00027 #include "libavutil/internal.h"
00028 #include "config.h"
00029
00030 #if HAVE_INLINE_ASM
00031
00032 #ifdef BROKEN_RELOCATIONS
00033 #define TABLES_ARG , "r"(tables)
00034
00035 #if HAVE_FAST_CMOV
00036 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
00037 "cmp "low" , "tmp" \n\t"\
00038 "cmova %%ecx , "range" \n\t"\
00039 "sbb %%rcx , %%rcx \n\t"\
00040 "and %%ecx , "tmp" \n\t"\
00041 "xor %%rcx , "retq" \n\t"\
00042 "sub "tmp" , "low" \n\t"
00043 #else
00044 #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
00045 \
00046 "sub "low" , "tmp" \n\t"\
00047 "sar $31 , "tmp" \n\t"\
00048 "sub %%ecx , "range" \n\t"\
00049 "and "tmp" , "range" \n\t"\
00050 "add %%ecx , "range" \n\t"\
00051 "shl $17 , %%ecx \n\t"\
00052 "and "tmp" , %%ecx \n\t"\
00053 "sub %%ecx , "low" \n\t"\
00054 "xor "tmp" , "ret" \n\t"\
00055 "movslq "ret" , "retq" \n\t"
00056 #endif
00057
00058 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
00059 "movzbl "statep" , "ret" \n\t"\
00060 "mov "range" , "tmp" \n\t"\
00061 "and $0xC0 , "range" \n\t"\
00062 "lea ("ret", "range", 2), %%ecx \n\t"\
00063 "movzbl "lps_off"("tables", %%rcx), "range" \n\t"\
00064 "sub "range" , "tmp" \n\t"\
00065 "mov "tmp" , %%ecx \n\t"\
00066 "shl $17 , "tmp" \n\t"\
00067 BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
00068 "movzbl "norm_off"("tables", "rangeq"), %%ecx \n\t"\
00069 "shl %%cl , "range" \n\t"\
00070 "movzbl "mlps_off"+128("tables", "retq"), "tmp" \n\t"\
00071 "shl %%cl , "low" \n\t"\
00072 "mov "tmpbyte" , "statep" \n\t"\
00073 "test "lowword" , "lowword" \n\t"\
00074 "jnz 2f \n\t"\
00075 "mov "byte" , %%"REG_c" \n\t"\
00076 "add"OPSIZE" $2 , "byte" \n\t"\
00077 "movzwl (%%"REG_c") , "tmp" \n\t"\
00078 "lea -1("low") , %%ecx \n\t"\
00079 "xor "low" , %%ecx \n\t"\
00080 "shr $15 , %%ecx \n\t"\
00081 "bswap "tmp" \n\t"\
00082 "shr $15 , "tmp" \n\t"\
00083 "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
00084 "sub $0xFFFF , "tmp" \n\t"\
00085 "neg %%ecx \n\t"\
00086 "add $7 , %%ecx \n\t"\
00087 "shl %%cl , "tmp" \n\t"\
00088 "add "tmp" , "low" \n\t"\
00089 "2: \n\t"
00090
00091 #else
00092 #define TABLES_ARG
00093 #define RIP_ARG
00094
00095 #if HAVE_FAST_CMOV
00096 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
00097 "mov "tmp" , %%ecx \n\t"\
00098 "shl $17 , "tmp" \n\t"\
00099 "cmp "low" , "tmp" \n\t"\
00100 "cmova %%ecx , "range" \n\t"\
00101 "sbb %%ecx , %%ecx \n\t"\
00102 "and %%ecx , "tmp" \n\t"\
00103 "xor %%ecx , "ret" \n\t"\
00104 "sub "tmp" , "low" \n\t"
00105 #else
00106 #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
00107 "mov "tmp" , %%ecx \n\t"\
00108 "shl $17 , "tmp" \n\t"\
00109 "sub "low" , "tmp" \n\t"\
00110 "sar $31 , "tmp" \n\t" \
00111 "sub %%ecx , "range" \n\t" \
00112 "and "tmp" , "range" \n\t" \
00113 "add %%ecx , "range" \n\t" \
00114 "shl $17 , %%ecx \n\t"\
00115 "and "tmp" , %%ecx \n\t"\
00116 "sub %%ecx , "low" \n\t"\
00117 "xor "tmp" , "ret" \n\t"
00118 #endif
00119
00120 #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
00121 "movzbl "statep" , "ret" \n\t"\
00122 "mov "range" , "tmp" \n\t"\
00123 "and $0xC0 , "range" \n\t"\
00124 "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
00125 "sub "range" , "tmp" \n\t"\
00126 BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
00127 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
00128 "shl %%cl , "range" \n\t"\
00129 "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
00130 "shl %%cl , "low" \n\t"\
00131 "mov "tmpbyte" , "statep" \n\t"\
00132 "test "lowword" , "lowword" \n\t"\
00133 " jnz 2f \n\t"\
00134 "mov "byte" , %%"REG_c" \n\t"\
00135 "add"OPSIZE" $2 , "byte" \n\t"\
00136 "movzwl (%%"REG_c") , "tmp" \n\t"\
00137 "lea -1("low") , %%ecx \n\t"\
00138 "xor "low" , %%ecx \n\t"\
00139 "shr $15 , %%ecx \n\t"\
00140 "bswap "tmp" \n\t"\
00141 "shr $15 , "tmp" \n\t"\
00142 "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
00143 "sub $0xFFFF , "tmp" \n\t"\
00144 "neg %%ecx \n\t"\
00145 "add $7 , %%ecx \n\t"\
00146 "shl %%cl , "tmp" \n\t"\
00147 "add "tmp" , "low" \n\t"\
00148 "2: \n\t"
00149
00150 #endif
00151
00152
00153 #if HAVE_7REGS && !(defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
00154 && !(defined(__i386) && !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)
00155 #define get_cabac_inline get_cabac_inline_x86
00156 static av_always_inline int get_cabac_inline_x86(CABACContext *c,
00157 uint8_t *const state)
00158 {
00159 int bit, tmp;
00160 #ifdef BROKEN_RELOCATIONS
00161 void *tables;
00162
00163 __asm__ volatile(
00164 "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
00165 : "=&r"(tables)
00166 );
00167 #endif
00168
00169 __asm__ volatile(
00170 BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
00171 "%2", "%q2", "%3", "%b3",
00172 "%c6(%5)", "%c7(%5)",
00173 AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
00174 AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
00175 AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
00176 "%8")
00177 : "=&r"(bit), "=&r"(c->low), "=&r"(c->range), "=&q"(tmp)
00178 : "r"(state), "r"(c),
00179 "i"(offsetof(CABACContext, bytestream)),
00180 "i"(offsetof(CABACContext, bytestream_end))
00181 TABLES_ARG
00182 ,"1"(c->low), "2"(c->range)
00183 : "%"REG_c, "memory"
00184 );
00185 return bit & 1;
00186 }
00187 #endif
00188
00189 #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
00190 static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
00191 {
00192 x86_reg tmp;
00193 __asm__ volatile(
00194 "movl %c6(%2), %k1 \n\t"
00195 "movl %c3(%2), %%eax \n\t"
00196 "shl $17, %k1 \n\t"
00197 "add %%eax, %%eax \n\t"
00198 "sub %k1, %%eax \n\t"
00199 "cltd \n\t"
00200 "and %%edx, %k1 \n\t"
00201 "add %k1, %%eax \n\t"
00202 "xor %%edx, %%ecx \n\t"
00203 "sub %%edx, %%ecx \n\t"
00204 "test %%ax, %%ax \n\t"
00205 "jnz 1f \n\t"
00206 "mov %c4(%2), %1 \n\t"
00207 "subl $0xFFFF, %%eax \n\t"
00208 "movzwl (%1), %%edx \n\t"
00209 "bswap %%edx \n\t"
00210 "shrl $15, %%edx \n\t"
00211 "add $2, %1 \n\t"
00212 "addl %%edx, %%eax \n\t"
00213 "mov %1, %c4(%2) \n\t"
00214 "1: \n\t"
00215 "movl %%eax, %c3(%2) \n\t"
00216
00217 : "+c"(val), "=&r"(tmp)
00218 : "r"(c),
00219 "i"(offsetof(CABACContext, low)),
00220 "i"(offsetof(CABACContext, bytestream)),
00221 "i"(offsetof(CABACContext, bytestream_end)),
00222 "i"(offsetof(CABACContext, range))
00223 : "%eax", "%edx", "memory"
00224 );
00225 return val;
00226 }
00227
00228 #endif
00229 #endif