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.

147 lines
6.9KB

  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 "config.h"
  26. #if HAVE_FAST_CMOV
  27. #define BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, range, tmp)\
  28. "mov "tmp" , %%ecx \n\t"\
  29. "shl $17 , "tmp" \n\t"\
  30. "cmp "low" , "tmp" \n\t"\
  31. "cmova %%ecx , "range" \n\t"\
  32. "sbb %%ecx , %%ecx \n\t"\
  33. "and %%ecx , "tmp" \n\t"\
  34. "xor %%ecx , "ret" \n\t"\
  35. "sub "tmp" , "low" \n\t"
  36. #else /* HAVE_FAST_CMOV */
  37. #define BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, range, tmp)\
  38. "mov "tmp" , %%ecx \n\t"\
  39. "shl $17 , "tmp" \n\t"\
  40. "sub "low" , "tmp" \n\t"\
  41. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  42. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  43. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  44. "add %%ecx , "range" \n\t" /*new range*/\
  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. #endif /* HAVE_FAST_CMOV */
  50. #define BRANCHLESS_GET_CABAC(ret, statep, low, lowword, range, tmp, tmpbyte, byte, end) \
  51. "movzbl "statep" , "ret" \n\t"\
  52. "mov "range" , "tmp" \n\t"\
  53. "and $0xC0 , "range" \n\t"\
  54. "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
  55. "sub "range" , "tmp" \n\t"\
  56. BRANCHLESS_GET_CABAC_UPDATE(ret, statep, low, range, tmp) \
  57. "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
  58. "shl %%cl , "range" \n\t"\
  59. "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
  60. "shl %%cl , "low" \n\t"\
  61. "mov "tmpbyte" , "statep" \n\t"\
  62. "test "lowword" , "lowword" \n\t"\
  63. " jnz 2f \n\t"\
  64. "mov "byte" , %%"REG_c" \n\t"\
  65. "add"OPSIZE" $2 , "byte" \n\t"\
  66. "movzwl (%%"REG_c") , "tmp" \n\t"\
  67. "lea -1("low") , %%ecx \n\t"\
  68. "xor "low" , %%ecx \n\t"\
  69. "shr $15 , %%ecx \n\t"\
  70. "bswap "tmp" \n\t"\
  71. "shr $15 , "tmp" \n\t"\
  72. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
  73. "sub $0xFFFF , "tmp" \n\t"\
  74. "neg %%ecx \n\t"\
  75. "add $7 , %%ecx \n\t"\
  76. "shl %%cl , "tmp" \n\t"\
  77. "add "tmp" , "low" \n\t"\
  78. "2: \n\t"
  79. #if HAVE_7REGS && !defined(BROKEN_RELOCATIONS) && !(defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\
  80. && !(defined(__i386) && !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)
  81. #define get_cabac_inline get_cabac_inline_x86
  82. static av_always_inline int get_cabac_inline_x86(CABACContext *c,
  83. uint8_t *const state)
  84. {
  85. int bit, tmp;
  86. __asm__ volatile(
  87. BRANCHLESS_GET_CABAC("%0", "(%4)", "%1", "%w1",
  88. "%2", "%3", "%b3",
  89. "%a6(%5)", "%a7(%5)")
  90. : "=&r"(bit), "+&r"(c->low), "+&r"(c->range), "=&q"(tmp)
  91. : "r"(state), "r"(c),
  92. "i"(offsetof(CABACContext, bytestream)),
  93. "i"(offsetof(CABACContext, bytestream_end))
  94. : "%"REG_c, "memory"
  95. );
  96. return bit & 1;
  97. }
  98. #endif /* HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
  99. #define get_cabac_bypass_sign get_cabac_bypass_sign_x86
  100. static av_always_inline int get_cabac_bypass_sign_x86(CABACContext *c, int val)
  101. {
  102. x86_reg tmp;
  103. __asm__ volatile(
  104. "movl %a6(%2), %k1 \n\t"
  105. "movl %a3(%2), %%eax \n\t"
  106. "shl $17, %k1 \n\t"
  107. "add %%eax, %%eax \n\t"
  108. "sub %k1, %%eax \n\t"
  109. "cltd \n\t"
  110. "and %%edx, %k1 \n\t"
  111. "add %k1, %%eax \n\t"
  112. "xor %%edx, %%ecx \n\t"
  113. "sub %%edx, %%ecx \n\t"
  114. "test %%ax, %%ax \n\t"
  115. "jnz 1f \n\t"
  116. "mov %a4(%2), %1 \n\t"
  117. "subl $0xFFFF, %%eax \n\t"
  118. "movzwl (%1), %%edx \n\t"
  119. "bswap %%edx \n\t"
  120. "shrl $15, %%edx \n\t"
  121. "add $2, %1 \n\t"
  122. "addl %%edx, %%eax \n\t"
  123. "mov %1, %a4(%2) \n\t"
  124. "1: \n\t"
  125. "movl %%eax, %a3(%2) \n\t"
  126. : "+c"(val), "=&r"(tmp)
  127. : "r"(c),
  128. "i"(offsetof(CABACContext, low)),
  129. "i"(offsetof(CABACContext, bytestream)),
  130. "i"(offsetof(CABACContext, bytestream_end)),
  131. "i"(offsetof(CABACContext, range))
  132. : "%eax", "%edx", "memory"
  133. );
  134. return val;
  135. }
  136. #endif /* AVCODEC_X86_CABAC_H */