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.

165 lines
4.3KB

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