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.

195 lines
5.0KB

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