cabac.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*****************************************************************************
  2. * cabac.h: arithmetic coder
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Loren Merritt <lorenm@u.washington.edu>
  7. * Laurent Aimar <fenrir@via.ecp.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  22. *
  23. * This program is also available under a commercial proprietary license.
  24. * For more information, contact us at licensing@x264.com.
  25. *****************************************************************************/
  26. #ifndef X264_CABAC_H
  27. #define X264_CABAC_H
  28. typedef struct
  29. {
  30. /* state */
  31. int i_low;
  32. int i_range;
  33. /* bit stream */
  34. int i_queue; //stored with an offset of -8 for faster asm
  35. int i_bytes_outstanding;
  36. uint8_t *p_start;
  37. uint8_t *p;
  38. uint8_t *p_end;
  39. /* aligned for memcpy_aligned starting here */
  40. ALIGNED_64( int f8_bits_encoded ); // only if using x264_cabac_size_decision()
  41. /* context */
  42. uint8_t state[1024];
  43. /* for 16-byte alignment */
  44. uint8_t padding[12];
  45. } x264_cabac_t;
  46. /* init the contexts given i_slice_type, the quantif and the model */
  47. #define x264_cabac_context_init x264_template(cabac_context_init)
  48. void x264_cabac_context_init( x264_t *h, x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model );
  49. #define x264_cabac_encode_init_core x264_template(cabac_encode_init_core)
  50. void x264_cabac_encode_init_core( x264_cabac_t *cb );
  51. #define x264_cabac_encode_init x264_template(cabac_encode_init)
  52. void x264_cabac_encode_init( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end );
  53. #define x264_cabac_encode_decision_c x264_template(cabac_encode_decision_c)
  54. void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b );
  55. #define x264_cabac_encode_decision_asm x264_template(cabac_encode_decision_asm)
  56. void x264_cabac_encode_decision_asm( x264_cabac_t *cb, int i_ctx, int b );
  57. #define x264_cabac_encode_bypass_c x264_template(cabac_encode_bypass_c)
  58. void x264_cabac_encode_bypass_c( x264_cabac_t *cb, int b );
  59. #define x264_cabac_encode_bypass_asm x264_template(cabac_encode_bypass_asm)
  60. void x264_cabac_encode_bypass_asm( x264_cabac_t *cb, int b );
  61. #define x264_cabac_encode_terminal_c x264_template(cabac_encode_terminal_c)
  62. void x264_cabac_encode_terminal_c( x264_cabac_t *cb );
  63. #define x264_cabac_encode_terminal_asm x264_template(cabac_encode_terminal_asm)
  64. void x264_cabac_encode_terminal_asm( x264_cabac_t *cb );
  65. #define x264_cabac_encode_ue_bypass x264_template(cabac_encode_ue_bypass)
  66. void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val );
  67. #define x264_cabac_encode_flush x264_template(cabac_encode_flush)
  68. void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb );
  69. #if HAVE_MMX
  70. #define x264_cabac_encode_decision x264_cabac_encode_decision_asm
  71. #define x264_cabac_encode_bypass x264_cabac_encode_bypass_asm
  72. #define x264_cabac_encode_terminal x264_cabac_encode_terminal_asm
  73. #elif defined(ARCH_AARCH64)
  74. #define x264_cabac_encode_decision x264_cabac_encode_decision_asm
  75. #define x264_cabac_encode_bypass x264_cabac_encode_bypass_asm
  76. #define x264_cabac_encode_terminal x264_cabac_encode_terminal_asm
  77. #else
  78. #define x264_cabac_encode_decision x264_cabac_encode_decision_c
  79. #define x264_cabac_encode_bypass x264_cabac_encode_bypass_c
  80. #define x264_cabac_encode_terminal x264_cabac_encode_terminal_c
  81. #endif
  82. #define x264_cabac_encode_decision_noup x264_cabac_encode_decision
  83. static ALWAYS_INLINE int x264_cabac_pos( x264_cabac_t *cb )
  84. {
  85. return (cb->p - cb->p_start + cb->i_bytes_outstanding) * 8 + cb->i_queue;
  86. }
  87. /* internal only. these don't write the bitstream, just calculate bit cost: */
  88. static ALWAYS_INLINE void x264_cabac_size_decision( x264_cabac_t *cb, long i_ctx, long b )
  89. {
  90. int i_state = cb->state[i_ctx];
  91. cb->state[i_ctx] = x264_cabac_transition[i_state][b];
  92. cb->f8_bits_encoded += x264_cabac_entropy[i_state^b];
  93. }
  94. static ALWAYS_INLINE int x264_cabac_size_decision2( uint8_t *state, long b )
  95. {
  96. int i_state = *state;
  97. *state = x264_cabac_transition[i_state][b];
  98. return x264_cabac_entropy[i_state^b];
  99. }
  100. static ALWAYS_INLINE void x264_cabac_size_decision_noup( x264_cabac_t *cb, long i_ctx, long b )
  101. {
  102. int i_state = cb->state[i_ctx];
  103. cb->f8_bits_encoded += x264_cabac_entropy[i_state^b];
  104. }
  105. static ALWAYS_INLINE int x264_cabac_size_decision_noup2( uint8_t *state, long b )
  106. {
  107. return x264_cabac_entropy[*state^b];
  108. }
  109. #endif