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.

229 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. "cmp "end" , %%"REG_c" \n\t"\
  70. "jge 1f \n\t"\
  71. "add"OPSIZE" $2 , "byte" \n\t"\
  72. "1: \n\t"\
  73. "movzwl (%%"REG_c") , "tmp" \n\t"\
  74. "lea -1("low") , %%ecx \n\t"\
  75. "xor "low" , %%ecx \n\t"\
  76. "shr $15 , %%ecx \n\t"\
  77. "bswap "tmp" \n\t"\
  78. "shr $15 , "tmp" \n\t"\
  79. "movzbl "norm_off"("tables", %%rcx), %%ecx \n\t"\
  80. "sub $0xFFFF , "tmp" \n\t"\
  81. "neg %%ecx \n\t"\
  82. "add $7 , %%ecx \n\t"\
  83. "shl %%cl , "tmp" \n\t"\
  84. "add "tmp" , "low" \n\t"\
  85. "2: \n\t"
  86. #else /* BROKEN_RELOCATIONS */
  87. #define TABLES_ARG
  88. #if HAVE_FAST_CMOV
  89. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  90. "mov "tmp" , %%ecx \n\t"\
  91. "shl $17 , "tmp" \n\t"\
  92. "cmp "low" , "tmp" \n\t"\
  93. "cmova %%ecx , "range" \n\t"\
  94. "sbb %%ecx , %%ecx \n\t"\
  95. "and %%ecx , "tmp" \n\t"\
  96. "xor %%ecx , "ret" \n\t"\
  97. "sub "tmp" , "low" \n\t"
  98. #else /* HAVE_FAST_CMOV */
  99. #define BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp)\
  100. "mov "tmp" , %%ecx \n\t"\
  101. "shl $17 , "tmp" \n\t"\
  102. "sub "low" , "tmp" \n\t"\
  103. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  104. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  105. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  106. "add %%ecx , "range" \n\t" /*new range*/\
  107. "shl $17 , %%ecx \n\t"\
  108. "and "tmp" , %%ecx \n\t"\
  109. "sub %%ecx , "low" \n\t"\
  110. "xor "tmp" , "ret" \n\t"
  111. #endif /* HAVE_FAST_CMOV */
  112. #define BRANCHLESS_GET_CABAC(ret, retq, statep, low, lowword, range, rangeq, tmp, tmpbyte, byte, end, norm_off, lps_off, mlps_off, tables) \
  113. "movzbl "statep" , "ret" \n\t"\
  114. "mov "range" , "tmp" \n\t"\
  115. "and $0xC0 , "range" \n\t"\
  116. "movzbl "MANGLE(ff_h264_cabac_tables)"+"lps_off"("ret", "range", 2), "range" \n\t"\
  117. "sub "range" , "tmp" \n\t"\
  118. BRANCHLESS_GET_CABAC_UPDATE(ret, low, range, tmp) \
  119. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"("range"), %%ecx \n\t"\
  120. "shl %%cl , "range" \n\t"\
  121. "movzbl "MANGLE(ff_h264_cabac_tables)"+"mlps_off"+128("ret"), "tmp" \n\t"\
  122. "shl %%cl , "low" \n\t"\
  123. "mov "tmpbyte" , "statep" \n\t"\
  124. "test "lowword" , "lowword" \n\t"\
  125. " jnz 2f \n\t"\
  126. "mov "byte" , %%"REG_c" \n\t"\
  127. "cmp "end" , %%"REG_c" \n\t"\
  128. "jge 1f \n\t"\
  129. "add"OPSIZE" $2 , "byte" \n\t"\
  130. "1: \n\t"\
  131. "movzwl (%%"REG_c") , "tmp" \n\t"\
  132. "lea -1("low") , %%ecx \n\t"\
  133. "xor "low" , %%ecx \n\t"\
  134. "shr $15 , %%ecx \n\t"\
  135. "bswap "tmp" \n\t"\
  136. "shr $15 , "tmp" \n\t"\
  137. "movzbl "MANGLE(ff_h264_cabac_tables)"+"norm_off"(%%ecx), %%ecx \n\t"\
  138. "sub $0xFFFF , "tmp" \n\t"\
  139. "neg %%ecx \n\t"\
  140. "add $7 , %%ecx \n\t"\
  141. "shl %%cl , "tmp" \n\t"\
  142. "add "tmp" , "low" \n\t"\
  143. "2: \n\t"
  144. #endif /* BROKEN_RELOCATIONS */
  145. #if HAVE_7REGS
  146. #define get_cabac_inline get_cabac_inline_x86
  147. static av_always_inline int get_cabac_inline_x86(CABACContext *c,
  148. uint8_t *const state)
  149. {
  150. int bit, tmp;
  151. #ifdef BROKEN_RELOCATIONS
  152. void *tables;
  153. __asm__ volatile(
  154. "lea "MANGLE(ff_h264_cabac_tables)", %0 \n\t"
  155. : "=&r"(tables)
  156. );
  157. #endif
  158. __asm__ volatile(
  159. BRANCHLESS_GET_CABAC("%0", "%q0", "(%4)", "%1", "%w1",
  160. "%2", "%q2", "%3", "%b3",
  161. "%a6(%5)", "%a7(%5)", "%a8", "%a9", "%a10", "%11")
  162. : "=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp)
  163. : "r"(state), "r"(c),
  164. "i"(offsetof(CABACContext, bytestream)),
  165. "i"(offsetof(CABACContext, bytestream_end)),
  166. "i"(H264_NORM_SHIFT_OFFSET),
  167. "i"(H264_LPS_RANGE_OFFSET),
  168. "i"(H264_MLPS_STATE_OFFSET) TABLES_ARG
  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 %a6(%2), %k1 \n\t"
  180. "movl %a3(%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 %a4(%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. "addl %%edx, %%eax \n\t"
  197. "cmp %a5(%2), %1 \n\t"
  198. "jge 1f \n\t"
  199. "add"OPSIZE" $2, %a4(%2) \n\t"
  200. "1: \n\t"
  201. "movl %%eax, %a3(%2) \n\t"
  202. : "+c"(val), "=&r"(tmp)
  203. : "r"(c),
  204. "i"(offsetof(CABACContext, low)),
  205. "i"(offsetof(CABACContext, bytestream)),
  206. "i"(offsetof(CABACContext, bytestream_end)),
  207. "i"(offsetof(CABACContext, range))
  208. : "%eax", "%edx", "memory"
  209. );
  210. return val;
  211. }
  212. #endif /* AVCODEC_X86_CABAC_H */