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.

208 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 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 inline void put_cabac_bit(CABACContext *c, int b){
  54. put_bits(&c->pb, 1, b);
  55. for(;c->outstanding_count; c->outstanding_count--){
  56. put_bits(&c->pb, 1, 1-b);
  57. }
  58. }
  59. static inline void renorm_cabac_encoder(CABACContext *c){
  60. while(c->range < 0x100){
  61. //FIXME optimize
  62. if(c->low<0x100){
  63. put_cabac_bit(c, 0);
  64. }else if(c->low<0x200){
  65. c->outstanding_count++;
  66. c->low -= 0x100;
  67. }else{
  68. put_cabac_bit(c, 1);
  69. c->low -= 0x200;
  70. }
  71. c->range+= c->range;
  72. c->low += c->low;
  73. }
  74. }
  75. static void refill(CABACContext *c){
  76. #if CABAC_BITS == 16
  77. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  78. #else
  79. c->low+= c->bytestream[0]<<1;
  80. #endif
  81. c->low -= CABAC_MASK;
  82. c->bytestream+= CABAC_BITS/8;
  83. }
  84. static inline void renorm_cabac_decoder_once(CABACContext *c){
  85. int shift= (uint32_t)(c->range - 0x100)>>31;
  86. c->range<<= shift;
  87. c->low <<= shift;
  88. if(!(c->low & CABAC_MASK))
  89. refill(c);
  90. }
  91. #ifndef get_cabac_inline
  92. static void refill2(CABACContext *c){
  93. int i, x;
  94. x= c->low ^ (c->low-1);
  95. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  96. x= -CABAC_MASK;
  97. #if CABAC_BITS == 16
  98. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  99. #else
  100. x+= c->bytestream[0]<<1;
  101. #endif
  102. c->low += x<<i;
  103. c->bytestream+= CABAC_BITS/8;
  104. }
  105. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  106. int s = *state;
  107. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  108. int bit, lps_mask;
  109. c->range -= RangeLPS;
  110. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  111. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  112. c->range += (RangeLPS - c->range) & lps_mask;
  113. s^=lps_mask;
  114. *state= (ff_h264_mlps_state+128)[s];
  115. bit= s&1;
  116. lps_mask= ff_h264_norm_shift[c->range];
  117. c->range<<= lps_mask;
  118. c->low <<= lps_mask;
  119. if(!(c->low & CABAC_MASK))
  120. refill2(c);
  121. return bit;
  122. }
  123. #endif
  124. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  125. return get_cabac_inline(c,state);
  126. }
  127. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  128. return get_cabac_inline(c,state);
  129. }
  130. static int av_unused get_cabac_bypass(CABACContext *c){
  131. int range;
  132. c->low += c->low;
  133. if(!(c->low & CABAC_MASK))
  134. refill(c);
  135. range= c->range<<(CABAC_BITS+1);
  136. if(c->low < range){
  137. return 0;
  138. }else{
  139. c->low -= range;
  140. return 1;
  141. }
  142. }
  143. #ifndef get_cabac_bypass_sign
  144. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  145. int range, mask;
  146. c->low += c->low;
  147. if(!(c->low & CABAC_MASK))
  148. refill(c);
  149. range= c->range<<(CABAC_BITS+1);
  150. c->low -= range;
  151. mask= c->low >> 31;
  152. range &= mask;
  153. c->low += range;
  154. return (val^mask)-mask;
  155. }
  156. #endif
  157. /**
  158. *
  159. * @return the number of bytes read or 0 if no end
  160. */
  161. static int av_unused get_cabac_terminate(CABACContext *c){
  162. c->range -= 2;
  163. if(c->low < c->range<<(CABAC_BITS+1)){
  164. renorm_cabac_decoder_once(c);
  165. return 0;
  166. }else{
  167. return c->bytestream - c->bytestream_start;
  168. }
  169. }
  170. #endif /* AVCODEC_CABAC_H */