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.

217 lines
5.2KB

  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.
  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(CABACContext *c){
  85. while(c->range < 0x100){
  86. c->range+= c->range;
  87. c->low+= c->low;
  88. if(!(c->low & CABAC_MASK))
  89. refill(c);
  90. }
  91. }
  92. static inline void renorm_cabac_decoder_once(CABACContext *c){
  93. int shift= (uint32_t)(c->range - 0x100)>>31;
  94. c->range<<= shift;
  95. c->low <<= shift;
  96. if(!(c->low & CABAC_MASK))
  97. refill(c);
  98. }
  99. #ifndef get_cabac_inline
  100. static void refill2(CABACContext *c){
  101. int i, x;
  102. x= c->low ^ (c->low-1);
  103. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  104. x= -CABAC_MASK;
  105. #if CABAC_BITS == 16
  106. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  107. #else
  108. x+= c->bytestream[0]<<1;
  109. #endif
  110. c->low += x<<i;
  111. c->bytestream+= CABAC_BITS/8;
  112. }
  113. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  114. int s = *state;
  115. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  116. int bit, lps_mask;
  117. c->range -= RangeLPS;
  118. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  119. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  120. c->range += (RangeLPS - c->range) & lps_mask;
  121. s^=lps_mask;
  122. *state= (ff_h264_mlps_state+128)[s];
  123. bit= s&1;
  124. lps_mask= ff_h264_norm_shift[c->range];
  125. c->range<<= lps_mask;
  126. c->low <<= lps_mask;
  127. if(!(c->low & CABAC_MASK))
  128. refill2(c);
  129. return bit;
  130. }
  131. #endif
  132. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  133. return get_cabac_inline(c,state);
  134. }
  135. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  136. return get_cabac_inline(c,state);
  137. }
  138. static int av_unused get_cabac_bypass(CABACContext *c){
  139. int range;
  140. c->low += c->low;
  141. if(!(c->low & CABAC_MASK))
  142. refill(c);
  143. range= c->range<<(CABAC_BITS+1);
  144. if(c->low < range){
  145. return 0;
  146. }else{
  147. c->low -= range;
  148. return 1;
  149. }
  150. }
  151. #ifndef get_cabac_bypass_sign
  152. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  153. int range, mask;
  154. c->low += c->low;
  155. if(!(c->low & CABAC_MASK))
  156. refill(c);
  157. range= c->range<<(CABAC_BITS+1);
  158. c->low -= range;
  159. mask= c->low >> 31;
  160. range &= mask;
  161. c->low += range;
  162. return (val^mask)-mask;
  163. }
  164. #endif
  165. /**
  166. *
  167. * @return the number of bytes read or 0 if no end
  168. */
  169. static int av_unused get_cabac_terminate(CABACContext *c){
  170. c->range -= 2;
  171. if(c->low < c->range<<(CABAC_BITS+1)){
  172. renorm_cabac_decoder_once(c);
  173. return 0;
  174. }else{
  175. return c->bytestream - c->bytestream_start;
  176. }
  177. }
  178. #endif /* AVCODEC_CABAC_H */