cabac_functions.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <stdint.h>
  28. #include "cabac.h"
  29. #include "config.h"
  30. #ifndef UNCHECKED_BITSTREAM_READER
  31. #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
  32. #endif
  33. #if ARCH_AARCH64
  34. # include "aarch64/cabac.h"
  35. #endif
  36. #if ARCH_ARM
  37. # include "arm/cabac.h"
  38. #endif
  39. #if ARCH_X86
  40. # include "x86/cabac.h"
  41. #endif
  42. #if ARCH_MIPS
  43. # include "mips/cabac.h"
  44. #endif
  45. static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET;
  46. static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET;
  47. static const uint8_t * const ff_h264_mlps_state = ff_h264_cabac_tables + H264_MLPS_STATE_OFFSET;
  48. static const uint8_t * const ff_h264_last_coeff_flag_offset_8x8 = ff_h264_cabac_tables + H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET;
  49. #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
  50. static void refill(CABACContext *c){
  51. #if CABAC_BITS == 16
  52. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  53. #else
  54. c->low+= c->bytestream[0]<<1;
  55. #endif
  56. c->low -= CABAC_MASK;
  57. #if !UNCHECKED_BITSTREAM_READER
  58. if (c->bytestream < c->bytestream_end)
  59. #endif
  60. c->bytestream += CABAC_BITS / 8;
  61. }
  62. #endif
  63. #ifndef get_cabac_terminate
  64. static inline void renorm_cabac_decoder_once(CABACContext *c){
  65. int shift= (uint32_t)(c->range - 0x100)>>31;
  66. c->range<<= shift;
  67. c->low <<= shift;
  68. if(!(c->low & CABAC_MASK))
  69. refill(c);
  70. }
  71. #endif
  72. #ifndef get_cabac_inline
  73. static void refill2(CABACContext *c){
  74. int i;
  75. unsigned x;
  76. #if !HAVE_FAST_CLZ
  77. x= c->low ^ (c->low-1);
  78. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  79. #else
  80. i = ff_ctz(c->low) - CABAC_BITS;
  81. #endif
  82. x= -CABAC_MASK;
  83. #if CABAC_BITS == 16
  84. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  85. #else
  86. x+= c->bytestream[0]<<1;
  87. #endif
  88. c->low += x<<i;
  89. #if !UNCHECKED_BITSTREAM_READER
  90. if (c->bytestream < c->bytestream_end)
  91. #endif
  92. c->bytestream += CABAC_BITS/8;
  93. }
  94. #endif
  95. #ifndef get_cabac_inline
  96. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  97. int s = *state;
  98. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  99. int bit, lps_mask;
  100. c->range -= RangeLPS;
  101. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  102. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  103. c->range += (RangeLPS - c->range) & lps_mask;
  104. s^=lps_mask;
  105. *state= (ff_h264_mlps_state+128)[s];
  106. bit= s&1;
  107. lps_mask= ff_h264_norm_shift[c->range];
  108. c->range<<= lps_mask;
  109. c->low <<= lps_mask;
  110. if(!(c->low & CABAC_MASK))
  111. refill2(c);
  112. return bit;
  113. }
  114. #endif
  115. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  116. return get_cabac_inline(c,state);
  117. }
  118. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  119. return get_cabac_inline(c,state);
  120. }
  121. #ifndef get_cabac_bypass
  122. static int av_unused get_cabac_bypass(CABACContext *c){
  123. int range;
  124. c->low += c->low;
  125. if(!(c->low & CABAC_MASK))
  126. refill(c);
  127. range= c->range<<(CABAC_BITS+1);
  128. if(c->low < range){
  129. return 0;
  130. }else{
  131. c->low -= range;
  132. return 1;
  133. }
  134. }
  135. #endif
  136. #ifndef get_cabac_bypass_sign
  137. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  138. int range, mask;
  139. c->low += c->low;
  140. if(!(c->low & CABAC_MASK))
  141. refill(c);
  142. range= c->range<<(CABAC_BITS+1);
  143. c->low -= range;
  144. mask= c->low >> 31;
  145. range &= mask;
  146. c->low += range;
  147. return (val^mask)-mask;
  148. }
  149. #endif
  150. /**
  151. * @return the number of bytes read or 0 if no end
  152. */
  153. #ifndef get_cabac_terminate
  154. static int av_unused get_cabac_terminate(CABACContext *c){
  155. c->range -= 2;
  156. if(c->low < c->range<<(CABAC_BITS+1)){
  157. renorm_cabac_decoder_once(c);
  158. return 0;
  159. }else{
  160. return c->bytestream - c->bytestream_start;
  161. }
  162. }
  163. #endif
  164. /**
  165. * Skip @p n bytes and reset the decoder.
  166. * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
  167. */
  168. #ifndef skip_bytes
  169. static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
  170. const uint8_t *ptr = c->bytestream;
  171. if (c->low & 0x1)
  172. ptr--;
  173. #if CABAC_BITS == 16
  174. if (c->low & 0x1FF)
  175. ptr--;
  176. #endif
  177. if ((int) (c->bytestream_end - ptr) < n)
  178. return NULL;
  179. if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
  180. return NULL;
  181. return ptr;
  182. }
  183. #endif
  184. #endif /* AVCODEC_CABAC_FUNCTIONS_H */