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.

161 lines
3.9KB

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