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.

230 lines
11KB

  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/x86/asm.h"
  25. #include "libavutil/internal.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. "add"OPSIZE" $2 , "byte" \n\t"\
  71. "movzwl (%%"REG_c") , "tmp" \n\t"\
  72. "lea -1("low") , %%ecx \n\t"\
  73. "xor "low" , %%ecx \n\t"\
  74. "shr $15 , %%ecx \n\t"\
  75. "bswap "tmp" \n\t"\
  76. "shr $15 , "tmp" \n\t"\
  77. "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
  78. "sub $0xFFFF , "tmp" \n\t"\
  79. "neg %%ecx \n\t"\
  80. "add $7 , %%ecx \n\t"\
  81. "shl %%cl , "tmp" \n\t"\
  82. "add "tmp" , "low" \n\t"\
  83. "2: \n\t"
  84. #else /* BROKEN_RELOCATIONS */
  85. #define TABLES_ARG
  86. #define RIP_ARG
  87. #if HAVE_FAST_CMOV
  88. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  89. "mov "tmp" , %%ecx \n\t"\
  90. "shl $17 , "tmp" \n\t"\
  91. "cmp "low" , "tmp" \n\t"\
  92. "cmova %%ecx , "range" \n\t"\
  93. "sbb %%ecx , %%ecx \n\t"\
  94. "and %%ecx , "tmp" \n\t"\
  95. "xor %%ecx , "ret" \n\t"\
  96. "sub "tmp" , "low" \n\t"
  97. #else /* HAVE_FAST_CMOV */
  98. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  99. "mov "tmp" , %%ecx \n\t"\
  100. "shl $17 , "tmp" \n\t"\
  101. "sub "low" , "tmp" \n\t"\
  102. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  103. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  104. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  105. "add %%ecx , "range" \n\t" /*new range*/\
  106. "shl $17 , %%ecx \n\t"\
  107. "and "tmp" , %%ecx \n\t"\
  108. "sub %%ecx , "low" \n\t"\
  109. "xor "tmp" , "ret" \n\t"
  110. #endif /* HAVE_FAST_CMOV */
  111. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  112. "movzbl "statep" , "ret" \n\t"\
  113. "mov "range" , "tmp" \n\t"\
  114. "and $0xC0 , "range" \n\t"\
  115. "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
  116. "sub "range" , "tmp" \n\t"\
  117. BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
  118. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
  119. "shl %%cl , "range" \n\t"\
  120. "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
  121. "shl %%cl , "low" \n\t"\
  122. "mov "tmpbyte" , "statep" \n\t"\
  123. "test "lowword" , "lowword" \n\t"\
  124. " jnz 2f \n\t"\
  125. "mov "byte" , %%"REG_c" \n\t"\
  126. "add"OPSIZE" $2 , "byte" \n\t"\
  127. "movzwl (%%"REG_c") , "tmp" \n\t"\
  128. "lea -1("low") , %%ecx \n\t"\
  129. "xor "low" , %%ecx \n\t"\
  130. "shr $15 , %%ecx \n\t"\
  131. "bswap "tmp" \n\t"\
  132. "shr $15 , "tmp" \n\t"\
  133. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
  134. "sub $0xFFFF , "tmp" \n\t"\
  135. "neg %%ecx \n\t"\
  136. "add $7 , %%ecx \n\t"\
  137. "shl %%cl , "tmp" \n\t"\
  138. "add "tmp" , "low" \n\t"\
  139. "2: \n\t"
  140. #endif /* BROKEN_RELOCATIONS */
  141. #if HAVE_7REGS && !(defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
  142. && !( !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)
  143. #define get_cabac_inline get_cabac_inline_x86
  144. static av_always_inline int get_cabac_inline_x86(CABACContext *c,
  145. uint8_t *const state)
  146. {
  147. int bit, tmp;
  148. #ifdef BROKEN_RELOCATIONS
  149. void *tables;
  150. __asm__ volatile(
  151. "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
  152. : "=&r"(tables)
  153. );
  154. #endif
  155. __asm__ volatile(
  156. BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
  157. "%2", "%q2", "%3", "%b3",
  158. "%c6(%5)", "%c7(%5)",
  159. AV_STRINGIFY(H264_NORM_SHIFT_OFFSET),
  160. AV_STRINGIFY(H264_LPS_RANGE_OFFSET),
  161. AV_STRINGIFY(H264_MLPS_STATE_OFFSET),
  162. "%8")
  163. : "=&r"(bit), "=&r"(c->low), "=&r"(c->range), "=&q"(tmp)
  164. : "r"(state), "r"(c),
  165. "i"(offsetof(CABACContext, bytestream)),
  166. "i"(offsetof(CABACContext, bytestream_end))
  167. TABLES_ARG
  168. ,"1"(c->low), "2"(c->range)
  169. : "%"REG_c, "memory"
  170. );
  171. return bit & 1;
  172. }
  173. #endif /* HAVE_7REGS */
  174. #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
  175. static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
  176. {
  177. x86_reg tmp;
  178. __asm__ volatile(
  179. "movl %c6(%2), %k1 \n\t"
  180. "movl %c3(%2), %%eax \n\t"
  181. "shl $17, %k1 \n\t"
  182. "add %%eax, %%eax \n\t"
  183. "sub %k1, %%eax \n\t"
  184. "cltd \n\t"
  185. "and %%edx, %k1 \n\t"
  186. "add %k1, %%eax \n\t"
  187. "xor %%edx, %%ecx \n\t"
  188. "sub %%edx, %%ecx \n\t"
  189. "test %%ax, %%ax \n\t"
  190. "jnz 1f \n\t"
  191. "mov %c4(%2), %1 \n\t"
  192. "subl $0xFFFF, %%eax \n\t"
  193. "movzwl (%1), %%edx \n\t"
  194. "bswap %%edx \n\t"
  195. "shrl $15, %%edx \n\t"
  196. "add $2, %1 \n\t"
  197. "addl %%edx, %%eax \n\t"
  198. "mov %1, %c4(%2) \n\t"
  199. "1: \n\t"
  200. "movl %%eax, %c3(%2) \n\t"
  201. : "+c"(val), "=&r"(tmp)
  202. : "r"(c),
  203. "i"(offsetof(CABACContext, low)),
  204. "i"(offsetof(CABACContext, bytestream)),
  205. "i"(offsetof(CABACContext, bytestream_end)),
  206. "i"(offsetof(CABACContext, range))
  207. : "%eax", "%edx", "memory"
  208. );
  209. return val;
  210. }
  211. #endif /* HAVE_INLINE_ASM */
  212. #endif /* AVCODEC_X86_CABAC_H */