cabac_functions.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Context Adaptive Binary Arithmetic Coder inline functions
  24. */
  25. #ifndef AVCODEC_CABAC_FUNCTIONS_H
  26. #define AVCODEC_CABAC_FUNCTIONS_H
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/intmath.h"
  31. #include "cabac.h"
  32. #include "config.h"
  33. #ifndef UNCHECKED_BITSTREAM_READER
  34. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  35. #endif
  36. #if ARCH_AARCH64
  37. # include "aarch64/cabac.h"
  38. #endif
  39. #if ARCH_ARM
  40. # include "arm/cabac.h"
  41. #endif
  42. #if ARCH_X86
  43. # include "x86/cabac.h"
  44. #endif
  45. #if ARCH_MIPS
  46. # include "mips/cabac.h"
  47. #endif
  48. #if ARCH_LOONGARCH64
  49. # include "loongarch/cabac.h"
  50. #endif
  51. static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET;
  52. static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET;
  53. static const uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET;
  54. static const uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
  55. #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
  56. static void refill(CABACContext *c){
  57. #if CABAC_BITS == 16
  58. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  59. #else
  60. c->low+= c->bytestream[0]<<1;
  61. #endif
  62. c->low -= CABAC_MASK;
  63. #if !UNCHECKED_BITSTREAM_READER
  64. if (c->bytestream < c->bytestream_end)
  65. #endif
  66. c->bytestream += CABAC_BITS / 8;
  67. }
  68. #endif
  69. #ifndef get_cabac_terminate
  70. static inline void renorm_cabac_decoder_once(CABACContext *c){
  71. int shift= (uint32_t)(c->range - 0x100)>>31;
  72. c->range<<= shift;
  73. c->low <<= shift;
  74. if(!(c->low & CABAC_MASK))
  75. refill(c);
  76. }
  77. #endif
  78. #ifndef get_cabac_inline
  79. static void refill2(CABACContext *c){
  80. int i;
  81. unsigned x;
  82. #if !HAVE_FAST_CLZ
  83. x= c->low ^ (c->low-1);
  84. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  85. #else
  86. i = ff_ctz(c->low) - CABAC_BITS;
  87. #endif
  88. x= -CABAC_MASK;
  89. #if CABAC_BITS == 16
  90. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  91. #else
  92. x+= c->bytestream[0]<<1;
  93. #endif
  94. c->low += x<<i;
  95. #if !UNCHECKED_BITSTREAM_READER
  96. if (c->bytestream < c->bytestream_end)
  97. #endif
  98. c->bytestream += CABAC_BITS/8;
  99. }
  100. #endif
  101. #ifndef get_cabac_inline
  102. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  103. int s = *state;
  104. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  105. int bit, lps_mask;
  106. c->range -= RangeLPS;
  107. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  108. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  109. c->range += (RangeLPS - c->range) & lps_mask;
  110. s^=lps_mask;
  111. *state= (ff_h264_mlps_state+128)[s];
  112. bit= s&1;
  113. lps_mask= ff_h264_norm_shift[c->range];
  114. c->range<<= lps_mask;
  115. c->low <<= lps_mask;
  116. if(!(c->low & CABAC_MASK))
  117. refill2(c);
  118. return bit;
  119. }
  120. #endif
  121. av_unused av_noinline static int get_cabac_noinline(CABACContext *c, uint8_t * const state){
  122. return get_cabac_inline(c,state);
  123. }
  124. av_unused static int get_cabac(CABACContext *c, uint8_t * const state){
  125. return get_cabac_inline(c,state);
  126. }
  127. #ifndef get_cabac_bypass
  128. av_unused static int get_cabac_bypass(CABACContext *c){
  129. int range;
  130. c->low += c->low;
  131. if(!(c->low & CABAC_MASK))
  132. refill(c);
  133. range= c->range<<(CABAC_BITS+1);
  134. if(c->low < range){
  135. return 0;
  136. }else{
  137. c->low -= range;
  138. return 1;
  139. }
  140. }
  141. #endif
  142. #ifndef get_cabac_bypass_sign
  143. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  144. int range, mask;
  145. c->low += c->low;
  146. if(!(c->low & CABAC_MASK))
  147. refill(c);
  148. range= c->range<<(CABAC_BITS+1);
  149. c->low -= range;
  150. mask= c->low >> 31;
  151. range &= mask;
  152. c->low += range;
  153. return (val^mask)-mask;
  154. }
  155. #endif
  156. /**
  157. * @return the number of bytes read or 0 if no end
  158. */
  159. #ifndef get_cabac_terminate
  160. av_unused static int get_cabac_terminate(CABACContext *c){
  161. c->range -= 2;
  162. if(c->low < c->range<<(CABAC_BITS+1)){
  163. renorm_cabac_decoder_once(c);
  164. return 0;
  165. }else{
  166. return c->bytestream - c->bytestream_start;
  167. }
  168. }
  169. #endif
  170. /**
  171. * Skip @p n bytes and reset the decoder.
  172. * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
  173. */
  174. #ifndef skip_bytes
  175. av_unused static const uint8_t* skip_bytes(CABACContext *c, int n) {
  176. const uint8_t *ptr = c->bytestream;
  177. if (c->low & 0x1)
  178. ptr--;
  179. #if CABAC_BITS == 16
  180. if (c->low & 0x1FF)
  181. ptr--;
  182. #endif
  183. if ((int) (c->bytestream_end - ptr) < n)
  184. return NULL;
  185. if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
  186. return NULL;
  187. return ptr;
  188. }
  189. #endif
  190. #endif /* AVCODEC_CABAC_FUNCTIONS_H */