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.

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