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.

224 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_cpu.h"
  25. #include "libavutil/internal.h"
  26. #include "config.h"
  27. #ifdef BROKEN_RELOCATIONS
  28. #define TABLES_ARG , "r"(tables)
  29. #if HAVE_FAST_CMOV
  30. #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  31. "cmp "low" , "tmp" \n\t"\
  32. "cmova %%ecx , "range" \n\t"\
  33. "sbb %%rcx , %%rcx \n\t"\
  34. "and %%ecx , "tmp" \n\t"\
  35. "xor %%rcx , "retq" \n\t"\
  36. "sub "tmp" , "low" \n\t"
  37. #else /* HAVE_FAST_CMOV */
  38. #define BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  39. /* P4 Prescott has crappy cmov,sbb,64bit shift so avoid them */ \
  40. "sub "low" , "tmp" \n\t"\
  41. "sar $31 , "tmp" \n\t"\
  42. "sub %%ecx , "range" \n\t"\
  43. "and "tmp" , "range" \n\t"\
  44. "add %%ecx , "range" \n\t"\
  45. "shl $17 , %%ecx \n\t"\
  46. "and "tmp" , %%ecx \n\t"\
  47. "sub %%ecx , "low" \n\t"\
  48. "xor "tmp" , "ret" \n\t"\
  49. "movslq "ret" , "retq" \n\t"
  50. #endif /* HAVE_FAST_CMOV */
  51. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  52. "movzbl "statep" , "ret" \n\t"\
  53. "mov "range" , "tmp" \n\t"\
  54. "and $0xC0 , "range" \n\t"\
  55. "lea ("ret", "range", 2), %%ecx \n\t"\
  56. "movzbl "lps_off"("tables", %%rcx), "range" \n\t"\
  57. "sub "range" , "tmp" \n\t"\
  58. "mov "tmp" , %%ecx \n\t"\
  59. "shl $17 , "tmp" \n\t"\
  60. BRANCHLESS_GET_CABAC_UPDATE(ret, retq, low, range, tmp) \
  61. "movzbl "norm_off"("tables", "rangeq"), %%ecx \n\t"\
  62. "shl %%cl , "range" \n\t"\
  63. "movzbl "mlps_off"+128("tables", "retq"), "tmp" \n\t"\
  64. "shl %%cl , "low" \n\t"\
  65. "mov "tmpbyte" , "statep" \n\t"\
  66. "test "lowword" , "lowword" \n\t"\
  67. "jnz 2f \n\t"\
  68. "mov "byte" , %%"REG_c" \n\t"\
  69. "add"OPSIZE" $2 , "byte" \n\t"\
  70. "movzwl (%%"REG_c") , "tmp" \n\t"\
  71. "lea -1("low") , %%ecx \n\t"\
  72. "xor "low" , %%ecx \n\t"\
  73. "shr $15 , %%ecx \n\t"\
  74. "bswap "tmp" \n\t"\
  75. "shr $15 , "tmp" \n\t"\
  76. "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
  77. "sub $0xFFFF , "tmp" \n\t"\
  78. "neg %%ecx \n\t"\
  79. "add $7 , %%ecx \n\t"\
  80. "shl %%cl , "tmp" \n\t"\
  81. "add "tmp" , "low" \n\t"\
  82. "2: \n\t"
  83. #else /* BROKEN_RELOCATIONS */
  84. #define TABLES_ARG
  85. #define RIP_ARG
  86. #if HAVE_FAST_CMOV
  87. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  88. "mov "tmp" , %%ecx \n\t"\
  89. "shl $17 , "tmp" \n\t"\
  90. "cmp "low" , "tmp" \n\t"\
  91. "cmova %%ecx , "range" \n\t"\
  92. "sbb %%ecx , %%ecx \n\t"\
  93. "and %%ecx , "tmp" \n\t"\
  94. "xor %%ecx , "ret" \n\t"\
  95. "sub "tmp" , "low" \n\t"
  96. #else /* HAVE_FAST_CMOV */
  97. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  98. "mov "tmp" , %%ecx \n\t"\
  99. "shl $17 , "tmp" \n\t"\
  100. "sub "low" , "tmp" \n\t"\
  101. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  102. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  103. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  104. "add %%ecx , "range" \n\t" /*new range*/\
  105. "shl $17 , %%ecx \n\t"\
  106. "and "tmp" , %%ecx \n\t"\
  107. "sub %%ecx , "low" \n\t"\
  108. "xor "tmp" , "ret" \n\t"
  109. #endif /* HAVE_FAST_CMOV */
  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. "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
  115. "sub "range" , "tmp" \n\t"\
  116. BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
  117. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
  118. "shl %%cl , "range" \n\t"\
  119. "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
  120. "shl %%cl , "low" \n\t"\
  121. "mov "tmpbyte" , "statep" \n\t"\
  122. "test "lowword" , "lowword" \n\t"\
  123. " jnz 2f \n\t"\
  124. "mov "byte" , %%"REG_c" \n\t"\
  125. "add"OPSIZE" $2 , "byte" \n\t"\
  126. "movzwl (%%"REG_c") , "tmp" \n\t"\
  127. "lea -1("low") , %%ecx \n\t"\
  128. "xor "low" , %%ecx \n\t"\
  129. "shr $15 , %%ecx \n\t"\
  130. "bswap "tmp" \n\t"\
  131. "shr $15 , "tmp" \n\t"\
  132. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
  133. "sub $0xFFFF , "tmp" \n\t"\
  134. "neg %%ecx \n\t"\
  135. "add $7 , %%ecx \n\t"\
  136. "shl %%cl , "tmp" \n\t"\
  137. "add "tmp" , "low" \n\t"\
  138. "2: \n\t"
  139. #endif /* BROKEN_RELOCATIONS */
  140. #if HAVE_7REGS && !(defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
  141. && !(defined(__i386) && !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)
  142. #define get_cabac_inline get_cabac_inline_x86
  143. static av_always_inline int get_cabac_inline_x86(CABACContext *c,
  144. uint8_t *const state)
  145. {
  146. int bit, tmp;
  147. #ifdef BROKEN_RELOCATIONS
  148. void *tables;
  149. __asm__ volatile(
  150. "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
  151. : "=&r"(tables)
  152. );
  153. #endif
  154. __asm__ volatile(
  155. BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
  156. "%2", "%q2", "%3", "%b3",
  157. "%a6(%5)", "%a7(%5)", "%a8", "%a9", "%a10", "%11")
  158. : "=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp)
  159. : "r"(state), "r"(c),
  160. "i"(offsetof(CABACContext, bytestream)),
  161. "i"(offsetof(CABACContext, bytestream_end)),
  162. "i"(H264_NORM_SHIFT_OFFSET),
  163. "i"(H264_LPS_RANGE_OFFSET),
  164. "i"(H264_MLPS_STATE_OFFSET) TABLES_ARG
  165. : "%"REG_c, "memory"
  166. );
  167. return bit & 1;
  168. }
  169. #endif /* HAVE_7REGS */
  170. #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
  171. static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
  172. {
  173. x86_reg tmp;
  174. __asm__ volatile(
  175. "movl %a6(%2), %k1 \n\t"
  176. "movl %a3(%2), %%eax \n\t"
  177. "shl $17, %k1 \n\t"
  178. "add %%eax, %%eax \n\t"
  179. "sub %k1, %%eax \n\t"
  180. "cltd \n\t"
  181. "and %%edx, %k1 \n\t"
  182. "add %k1, %%eax \n\t"
  183. "xor %%edx, %%ecx \n\t"
  184. "sub %%edx, %%ecx \n\t"
  185. "test %%ax, %%ax \n\t"
  186. "jnz 1f \n\t"
  187. "mov %a4(%2), %1 \n\t"
  188. "subl $0xFFFF, %%eax \n\t"
  189. "movzwl (%1), %%edx \n\t"
  190. "bswap %%edx \n\t"
  191. "shrl $15, %%edx \n\t"
  192. "add $2, %1 \n\t"
  193. "addl %%edx, %%eax \n\t"
  194. "mov %1, %a4(%2) \n\t"
  195. "1: \n\t"
  196. "movl %%eax, %a3(%2) \n\t"
  197. : "+c"(val), "=&r"(tmp)
  198. : "r"(c),
  199. "i"(offsetof(CABACContext, low)),
  200. "i"(offsetof(CABACContext, bytestream)),
  201. "i"(offsetof(CABACContext, bytestream_end)),
  202. "i"(offsetof(CABACContext, range))
  203. : "%eax", "%edx", "memory"
  204. );
  205. return val;
  206. }
  207. #endif /* AVCODEC_X86_CABAC_H */