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.

200 lines
5.1KB

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