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.

183 lines
4.4KB

  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.
  24. */
  25. #ifndef AVCODEC_CABAC_H
  26. #define AVCODEC_CABAC_H
  27. #include <stddef.h>
  28. #include "put_bits.h"
  29. //#undef NDEBUG
  30. #include <assert.h>
  31. #define CABAC_BITS 16
  32. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  33. typedef struct CABACContext{
  34. int low;
  35. int range;
  36. int outstanding_count;
  37. const uint8_t *bytestream_start;
  38. const uint8_t *bytestream;
  39. const uint8_t *bytestream_end;
  40. PutBitContext pb;
  41. }CABACContext;
  42. extern uint8_t ff_h264_mlps_state[4*64];
  43. extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
  44. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  45. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  46. extern const uint8_t ff_h264_norm_shift[512];
  47. #if ARCH_X86
  48. # include "x86/cabac.h"
  49. #endif
  50. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  51. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  52. void ff_init_cabac_states(CABACContext *c);
  53. static void refill(CABACContext *c){
  54. #if CABAC_BITS == 16
  55. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  56. #else
  57. c->low+= c->bytestream[0]<<1;
  58. #endif
  59. c->low -= CABAC_MASK;
  60. c->bytestream+= CABAC_BITS/8;
  61. }
  62. static inline void renorm_cabac_decoder_once(CABACContext *c){
  63. int shift= (uint32_t)(c->range - 0x100)>>31;
  64. c->range<<= shift;
  65. c->low <<= shift;
  66. if(!(c->low & CABAC_MASK))
  67. refill(c);
  68. }
  69. #ifndef get_cabac_inline
  70. static void refill2(CABACContext *c){
  71. int i, x;
  72. x= c->low ^ (c->low-1);
  73. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  74. x= -CABAC_MASK;
  75. #if CABAC_BITS == 16
  76. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  77. #else
  78. x+= c->bytestream[0]<<1;
  79. #endif
  80. c->low += x<<i;
  81. c->bytestream+= CABAC_BITS/8;
  82. }
  83. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  84. int s = *state;
  85. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  86. int bit, lps_mask;
  87. c->range -= RangeLPS;
  88. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  89. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  90. c->range += (RangeLPS - c->range) & lps_mask;
  91. s^=lps_mask;
  92. *state= (ff_h264_mlps_state+128)[s];
  93. bit= s&1;
  94. lps_mask= ff_h264_norm_shift[c->range];
  95. c->range<<= lps_mask;
  96. c->low <<= lps_mask;
  97. if(!(c->low & CABAC_MASK))
  98. refill2(c);
  99. return bit;
  100. }
  101. #endif
  102. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  103. return get_cabac_inline(c,state);
  104. }
  105. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  106. return get_cabac_inline(c,state);
  107. }
  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. #ifndef get_cabac_bypass_sign
  122. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  123. int range, mask;
  124. c->low += c->low;
  125. if(!(c->low & CABAC_MASK))
  126. refill(c);
  127. range= c->range<<(CABAC_BITS+1);
  128. c->low -= range;
  129. mask= c->low >> 31;
  130. range &= mask;
  131. c->low += range;
  132. return (val^mask)-mask;
  133. }
  134. #endif
  135. /**
  136. *
  137. * @return the number of bytes read or 0 if no end
  138. */
  139. static int av_unused get_cabac_terminate(CABACContext *c){
  140. c->range -= 2;
  141. if(c->low < c->range<<(CABAC_BITS+1)){
  142. renorm_cabac_decoder_once(c);
  143. return 0;
  144. }else{
  145. return c->bytestream - c->bytestream_start;
  146. }
  147. }
  148. #endif /* AVCODEC_CABAC_H */