cabac.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #ifndef AVCODEC_X86_CABAC_H
  21. #define AVCODEC_X86_CABAC_H
  22. #include "libavcodec/cabac.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/macros.h"
  25. #include "libavutil/x86/asm.h"
  26. #include "config.h"
  27. #if (defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
  28. || ( !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)\
  29. || (defined(__INTEL_COMPILER) && defined(_MSC_VER))
  30. # define BROKEN_COMPILER 1
  31. #else
  32. # define BROKEN_COMPILER 0
  33. #endif
  34. #if HAVE_INLINE_ASM
  35. #ifndef UNCHECKED_BITSTREAM_READER
  36. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  37. #endif
  38. #if UNCHECKED_BITSTREAM_READER
  39. #define END_CHECK(end) ""
  40. #else
  41. #define END_CHECK(end) \
  42. "cmp "end" , %%"FF_REG_c" \n\t"\
  43. "jge 1f \n\t"
  44. #endif
  45. #ifdef BROKEN_RELOCATIONS
  46. #define TABLES_ARG , "r"(tables)
  47. #if HAVE_FAST_CMOV
  48. #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  49. "cmp "low" , "tmp" \n\t"\
  50. "cmova %%ecx , "range" \n\t"\
  51. "sbb %%rcx , %%rcx \n\t"\
  52. "and %%ecx , "tmp" \n\t"\
  53. "xor %%rcx , "retq" \n\t"\
  54. "sub "tmp" , "low" \n\t"
  55. #else /* HAVE_FAST_CMOV */
  56. #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  57. /* P4 Prescott has crappy cmov,sbb,64-bit shift so avoid them */ \
  58. "sub "low" , "tmp" \n\t"\
  59. "sar $31 , "tmp" \n\t"\
  60. "sub %%ecx , "range" \n\t"\
  61. "and "tmp" , "range" \n\t"\
  62. "add %%ecx , "range" \n\t"\
  63. "shl $17 , %%ecx \n\t"\
  64. "and "tmp" , %%ecx \n\t"\
  65. "sub %%ecx , "low" \n\t"\
  66. "xor "tmp" , "ret" \n\t"\
  67. "movslq "ret" , "retq" \n\t"
  68. #endif /* HAVE_FAST_CMOV */
  69. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  70. "movzbl "statep" , "ret" \n\t"\
  71. "mov "range" , "tmp" \n\t"\
  72. "and $0xC0 , "range" \n\t"\
  73. "lea ("ret", "range", 2), %%ecx \n\t"\
  74. "movzbl "lps_off"("tables", %%rcx), "range" \n\t"\
  75. "sub "range" , "tmp" \n\t"\
  76. "mov "tmp" , %%ecx \n\t"\
  77. "shl $17 , "tmp" \n\t"\
  78. BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  79. "movzbl "norm_off"("tables", "rangeq"), %%ecx \n\t"\
  80. "shl %%cl , "range" \n\t"\
  81. "movzbl "mlps_off"+128("tables", "retq"), "tmp" \n\t"\
  82. "shl %%cl , "low" \n\t"\
  83. "mov "tmpbyte" , "statep" \n\t"\
  84. "test "lowword" , "lowword" \n\t"\
  85. "jnz 2f \n\t"\
  86. "mov "byte" , %%"FF_REG_c" \n\t"\
  87. END_CHECK(end)\
  88. "add"FF_OPSIZE" $2 , "byte" \n\t"\
  89. "1: \n\t"\
  90. "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
  91. "lea -1("low") , %%ecx \n\t"\
  92. "xor "low" , %%ecx \n\t"\
  93. "shr $15 , %%ecx \n\t"\
  94. "bswap "tmp" \n\t"\
  95. "shr $15 , "tmp" \n\t"\
  96. "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
  97. "sub $0xFFFF , "tmp" \n\t"\
  98. "neg %%ecx \n\t"\
  99. "add $7 , %%ecx \n\t"\
  100. "shl %%cl , "tmp" \n\t"\
  101. "add "tmp" , "low" \n\t"\
  102. "2: \n\t"
  103. #else /* BROKEN_RELOCATIONS */
  104. #define TABLES_ARG NAMED_CONSTRAINTS_ARRAY_ADD(ff_h264_cabac_tables)
  105. #define RIP_ARG
  106. #if HAVE_FAST_CMOV
  107. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  108. "mov "tmp" , %%ecx \n\t"\
  109. "shl $17 , "tmp" \n\t"\
  110. "cmp "low" , "tmp" \n\t"\
  111. "cmova %%ecx , "range" \n\t"\
  112. "sbb %%ecx , %%ecx \n\t"\
  113. "and %%ecx , "tmp" \n\t"\
  114. "xor %%ecx , "ret" \n\t"\
  115. "sub "tmp" , "low" \n\t"
  116. #else /* HAVE_FAST_CMOV */
  117. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  118. "mov "tmp" , %%ecx \n\t"\
  119. "shl $17 , "tmp" \n\t"\
  120. "sub "low" , "tmp" \n\t"\
  121. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  122. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  123. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  124. "add %%ecx , "range" \n\t" /*new range*/\
  125. "shl $17 , %%ecx \n\t"\
  126. "and "tmp" , %%ecx \n\t"\
  127. "sub %%ecx , "low" \n\t"\
  128. "xor "tmp" , "ret" \n\t"
  129. #endif /* HAVE_FAST_CMOV */
  130. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  131. "movzbl "statep" , "ret" \n\t"\
  132. "mov "range" , "tmp" \n\t"\
  133. "and $0xC0 , "range" \n\t"\
  134. "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
  135. "sub "range" , "tmp" \n\t"\
  136. BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
  137. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
  138. "shl %%cl , "range" \n\t"\
  139. "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
  140. "shl %%cl , "low" \n\t"\
  141. "mov "tmpbyte" , "statep" \n\t"\
  142. "test "lowword" , "lowword" \n\t"\
  143. " jnz 2f \n\t"\
  144. "mov "byte" , %%"FF_REG_c" \n\t"\
  145. END_CHECK(end)\
  146. "add"FF_OPSIZE" $2 , "byte" \n\t"\
  147. "1: \n\t"\
  148. "movzwl (%%"FF_REG_c") , "tmp" \n\t"\
  149. "lea -1("low") , %%ecx \n\t"\
  150. "xor "low" , %%ecx \n\t"\
  151. "shr $15 , %%ecx \n\t"\
  152. "bswap "tmp" \n\t"\
  153. "shr $15 , "tmp" \n\t"\
  154. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
  155. "sub $0xFFFF , "tmp" \n\t"\
  156. "neg %%ecx \n\t"\
  157. "add $7 , %%ecx \n\t"\
  158. "shl %%cl , "tmp" \n\t"\
  159. "add "tmp" , "low" \n\t"\
  160. "2: \n\t"
  161. #endif /* BROKEN_RELOCATIONS */
  162. #if HAVE_7REGS && !BROKEN_COMPILER
  163. #define get_cabac_inline get_cabac_inline_x86
  164. static av_always_inline int get_cabac_inline_x86(CABACContext *c,
  165. uint8_t *const state)
  166. {
  167. int bit, tmp;
  168. #ifdef BROKEN_RELOCATIONS
  169. void *tables;
  170. __asm__ volatile(
  171. "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
  172. : "=&r"(tables)
  173. : NAMED_CONSTRAINTS_ARRAY(ff_h264_cabac_tables)
  174. );
  175. #endif
  176. __asm__ volatile(
  177. BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
  178. "%2", "%q2", "%3", "%b3",
  179. "%c6(%5)", "%c7(%5)",
  180. AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  181. AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  182. AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  183. "%8")
  184. : "=&r"(bit), "=&r"(c->low), "=&r"(c->range), "=&q"(tmp)
  185. : "r"(state), "r"(c),
  186. "i"(offsetof(CABACContext, bytestream)),
  187. "i"(offsetof(CABACContext, bytestream_end))
  188. TABLES_ARG
  189. ,"1"(c->low), "2"(c->range)
  190. : "%"FF_REG_c, "memory"
  191. );
  192. return bit & 1;
  193. }
  194. #endif /* HAVE_7REGS && !BROKEN_COMPILER */
  195. #if !BROKEN_COMPILER
  196. #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
  197. static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
  198. {
  199. x86_reg tmp;
  200. __asm__ volatile(
  201. "movl %c6(%2), %k1 \n\t"
  202. "movl %c3(%2), %%eax \n\t"
  203. "shl $17, %k1 \n\t"
  204. "add %%eax, %%eax \n\t"
  205. "sub %k1, %%eax \n\t"
  206. "cdq \n\t"
  207. "and %%edx, %k1 \n\t"
  208. "add %k1, %%eax \n\t"
  209. "xor %%edx, %%ecx \n\t"
  210. "sub %%edx, %%ecx \n\t"
  211. "test %%ax, %%ax \n\t"
  212. "jnz 1f \n\t"
  213. "mov %c4(%2), %1 \n\t"
  214. "subl $0xFFFF, %%eax \n\t"
  215. "movzwl (%1), %%edx \n\t"
  216. "bswap %%edx \n\t"
  217. "shrl $15, %%edx \n\t"
  218. #if UNCHECKED_BITSTREAM_READER
  219. "add $2, %1 \n\t"
  220. "addl %%edx, %%eax \n\t"
  221. "mov %1, %c4(%2) \n\t"
  222. #else
  223. "addl %%edx, %%eax \n\t"
  224. "cmp %c5(%2), %1 \n\t"
  225. "jge 1f \n\t"
  226. "add"FF_OPSIZE" $2, %c4(%2) \n\t"
  227. #endif
  228. "1: \n\t"
  229. "movl %%eax, %c3(%2) \n\t"
  230. : "+c"(val), "=&r"(tmp)
  231. : "r"(c),
  232. "i"(offsetof(CABACContext, low)),
  233. "i"(offsetof(CABACContext, bytestream)),
  234. "i"(offsetof(CABACContext, bytestream_end)),
  235. "i"(offsetof(CABACContext, range))
  236. : "%eax", "%edx", "memory"
  237. );
  238. return val;
  239. }
  240. #define get_cabac_bypass get_cabac_bypass_x86
  241. static av_always_inline int get_cabac_bypass_x86(CABACContext *c)
  242. {
  243. x86_reg tmp;
  244. int res;
  245. __asm__ volatile(
  246. "movl %c6(%2), %k1 \n\t"
  247. "movl %c3(%2), %%eax \n\t"
  248. "shl $17, %k1 \n\t"
  249. "add %%eax, %%eax \n\t"
  250. "sub %k1, %%eax \n\t"
  251. "cdq \n\t"
  252. "and %%edx, %k1 \n\t"
  253. "add %k1, %%eax \n\t"
  254. "inc %%edx \n\t"
  255. "test %%ax, %%ax \n\t"
  256. "jnz 1f \n\t"
  257. "mov %c4(%2), %1 \n\t"
  258. "subl $0xFFFF, %%eax \n\t"
  259. "movzwl (%1), %%ecx \n\t"
  260. "bswap %%ecx \n\t"
  261. "shrl $15, %%ecx \n\t"
  262. "addl %%ecx, %%eax \n\t"
  263. "cmp %c5(%2), %1 \n\t"
  264. "jge 1f \n\t"
  265. "add"FF_OPSIZE" $2, %c4(%2) \n\t"
  266. "1: \n\t"
  267. "movl %%eax, %c3(%2) \n\t"
  268. : "=&d"(res), "=&r"(tmp)
  269. : "r"(c),
  270. "i"(offsetof(CABACContext, low)),
  271. "i"(offsetof(CABACContext, bytestream)),
  272. "i"(offsetof(CABACContext, bytestream_end)),
  273. "i"(offsetof(CABACContext, range))
  274. : "%eax", "%ecx", "memory"
  275. );
  276. return res;
  277. }
  278. #endif /* !BROKEN_COMPILER */
  279. #endif /* HAVE_INLINE_ASM */
  280. #endif /* AVCODEC_X86_CABAC_H */