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.

188 lines
4.8KB

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