cabac.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVCODEC_AARCH64_CABAC_H
  19. #define AVCODEC_AARCH64_CABAC_H
  20. #include "config.h"
  21. #if HAVE_INLINE_ASM
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/internal.h"
  24. #include "libavcodec/cabac.h"
  25. #define get_cabac_inline get_cabac_inline_aarch64
  26. static av_always_inline int get_cabac_inline_aarch64(CABACContext *c,
  27. uint8_t *const state)
  28. {
  29. int bit;
  30. void *reg_a, *reg_b, *reg_c, *tmp;
  31. __asm__ volatile(
  32. "ldrb %w[bit] , [%[state]] \n\t"
  33. "add %[r_b] , %[tables] , %[lps_off] \n\t"
  34. "mov %w[tmp] , %w[range] \n\t"
  35. "and %w[range] , %w[range] , #0xC0 \n\t"
  36. "lsl %w[r_c] , %w[range] , #1 \n\t"
  37. "add %[r_b] , %[r_b] , %w[bit], UXTW \n\t"
  38. "ldrb %w[range] , [%[r_b], %w[r_c], SXTW] \n\t"
  39. "sub %w[r_c] , %w[tmp] , %w[range] \n\t"
  40. "lsl %w[tmp] , %w[r_c] , #17 \n\t"
  41. "cmp %w[tmp] , %w[low] \n\t"
  42. "csel %w[tmp] , %w[tmp] , wzr , cc \n\t"
  43. "csel %w[range] , %w[r_c] , %w[range], gt \n\t"
  44. "cinv %w[bit] , %w[bit] , cc \n\t"
  45. "sub %w[low] , %w[low] , %w[tmp] \n\t"
  46. "add %[r_b] , %[tables] , %[norm_off] \n\t"
  47. "add %[r_a] , %[tables] , %[mlps_off] \n\t"
  48. "ldrb %w[tmp] , [%[r_b], %w[range], SXTW] \n\t"
  49. "ldrb %w[r_a] , [%[r_a], %w[bit], SXTW] \n\t"
  50. "lsl %w[low] , %w[low] , %w[tmp] \n\t"
  51. "lsl %w[range] , %w[range] , %w[tmp] \n\t"
  52. "uxth %w[r_c] , %w[low] \n\t"
  53. "strb %w[r_a] , [%[state]] \n\t"
  54. "cbnz %w[r_c] , 2f \n\t"
  55. "ldr %[r_c] , [%[c], %[byte]] \n\t"
  56. "ldr %[r_a] , [%[c], %[end]] \n\t"
  57. "ldrh %w[tmp] , [%[r_c]] \n\t"
  58. "cmp %[r_c] , %[r_a] \n\t"
  59. "b.ge 1f \n\t"
  60. "add %[r_a] , %[r_c] , #2 \n\t"
  61. "str %[r_a] , [%[c], %[byte]] \n\t"
  62. "1: \n\t"
  63. "sub %w[r_c] , %w[low] , #1 \n\t"
  64. "eor %w[r_c] , %w[r_c] , %w[low] \n\t"
  65. "rev %w[tmp] , %w[tmp] \n\t"
  66. "lsr %w[r_c] , %w[r_c] , #15 \n\t"
  67. "lsr %w[tmp] , %w[tmp] , #15 \n\t"
  68. "ldrb %w[r_c] , [%[r_b], %w[r_c], SXTW] \n\t"
  69. "mov %w[r_b] , #0xFFFF \n\t"
  70. "mov %w[r_a] , #7 \n\t"
  71. "sub %w[tmp] , %w[tmp] , %w[r_b] \n\t"
  72. "sub %w[r_c] , %w[r_a] , %w[r_c] \n\t"
  73. "lsl %w[tmp] , %w[tmp] , %w[r_c] \n\t"
  74. "add %w[low] , %w[low] , %w[tmp] \n\t"
  75. "2: \n\t"
  76. : [bit]"=&r"(bit),
  77. [low]"+&r"(c->low),
  78. [range]"+&r"(c->range),
  79. [r_a]"=&r"(reg_a),
  80. [r_b]"=&r"(reg_b),
  81. [r_c]"=&r"(reg_c),
  82. [tmp]"=&r"(tmp)
  83. : [c]"r"(c),
  84. [state]"r"(state),
  85. [tables]"r"(ff_h264_cabac_tables),
  86. [byte]"i"(offsetof(CABACContext, bytestream)),
  87. [end]"i"(offsetof(CABACContext, bytestream_end)),
  88. [norm_off]"I"(H264_NORM_SHIFT_OFFSET),
  89. [lps_off]"I"(H264_LPS_RANGE_OFFSET),
  90. [mlps_off]"I"(H264_MLPS_STATE_OFFSET + 128)
  91. : "memory", "cc"
  92. );
  93. return bit & 1;
  94. }
  95. #endif /* HAVE_INLINE_ASM */
  96. #endif /* AVCODEC_AARCH64_CABAC_H */