You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
13KB

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