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.

278 lines
6.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 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. #ifdef STRICT_LIMITS
  38. int symCount;
  39. #endif
  40. const uint8_t *bytestream_start;
  41. const uint8_t *bytestream;
  42. const uint8_t *bytestream_end;
  43. PutBitContext pb;
  44. }CABACContext;
  45. extern uint8_t ff_h264_mlps_state[4*64];
  46. extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
  47. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  48. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  49. extern const uint8_t ff_h264_norm_shift[512];
  50. #if ARCH_X86
  51. # include "x86/cabac.h"
  52. #endif
  53. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  54. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  55. void ff_init_cabac_states(CABACContext *c);
  56. static inline void put_cabac_bit(CABACContext *c, int b){
  57. put_bits(&c->pb, 1, b);
  58. for(;c->outstanding_count; c->outstanding_count--){
  59. put_bits(&c->pb, 1, 1-b);
  60. }
  61. }
  62. static inline void renorm_cabac_encoder(CABACContext *c){
  63. while(c->range < 0x100){
  64. //FIXME optimize
  65. if(c->low<0x100){
  66. put_cabac_bit(c, 0);
  67. }else if(c->low<0x200){
  68. c->outstanding_count++;
  69. c->low -= 0x100;
  70. }else{
  71. put_cabac_bit(c, 1);
  72. c->low -= 0x200;
  73. }
  74. c->range+= c->range;
  75. c->low += c->low;
  76. }
  77. }
  78. static void refill(CABACContext *c){
  79. #if CABAC_BITS == 16
  80. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  81. #else
  82. c->low+= c->bytestream[0]<<1;
  83. #endif
  84. c->low -= CABAC_MASK;
  85. c->bytestream+= CABAC_BITS/8;
  86. }
  87. static inline void renorm_cabac_decoder(CABACContext *c){
  88. while(c->range < 0x100){
  89. c->range+= c->range;
  90. c->low+= c->low;
  91. if(!(c->low & CABAC_MASK))
  92. refill(c);
  93. }
  94. }
  95. static inline void renorm_cabac_decoder_once(CABACContext *c){
  96. int shift= (uint32_t)(c->range - 0x100)>>31;
  97. c->range<<= shift;
  98. c->low <<= shift;
  99. if(!(c->low & CABAC_MASK))
  100. refill(c);
  101. }
  102. #ifndef get_cabac_inline
  103. static void refill2(CABACContext *c){
  104. int i, x;
  105. x= c->low ^ (c->low-1);
  106. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  107. x= -CABAC_MASK;
  108. #if CABAC_BITS == 16
  109. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  110. #else
  111. x+= c->bytestream[0]<<1;
  112. #endif
  113. c->low += x<<i;
  114. c->bytestream+= CABAC_BITS/8;
  115. }
  116. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  117. int s = *state;
  118. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  119. int bit, lps_mask;
  120. c->range -= RangeLPS;
  121. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  122. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  123. c->range += (RangeLPS - c->range) & lps_mask;
  124. s^=lps_mask;
  125. *state= (ff_h264_mlps_state+128)[s];
  126. bit= s&1;
  127. lps_mask= ff_h264_norm_shift[c->range];
  128. c->range<<= lps_mask;
  129. c->low <<= lps_mask;
  130. if(!(c->low & CABAC_MASK))
  131. refill2(c);
  132. return bit;
  133. }
  134. #endif
  135. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  136. return get_cabac_inline(c,state);
  137. }
  138. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  139. return get_cabac_inline(c,state);
  140. }
  141. static int av_unused get_cabac_bypass(CABACContext *c){
  142. int range;
  143. c->low += c->low;
  144. if(!(c->low & CABAC_MASK))
  145. refill(c);
  146. range= c->range<<(CABAC_BITS+1);
  147. if(c->low < range){
  148. return 0;
  149. }else{
  150. c->low -= range;
  151. return 1;
  152. }
  153. }
  154. #ifndef get_cabac_bypass_sign
  155. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  156. int range, mask;
  157. c->low += c->low;
  158. if(!(c->low & CABAC_MASK))
  159. refill(c);
  160. range= c->range<<(CABAC_BITS+1);
  161. c->low -= range;
  162. mask= c->low >> 31;
  163. range &= mask;
  164. c->low += range;
  165. return (val^mask)-mask;
  166. }
  167. #endif
  168. /**
  169. *
  170. * @return the number of bytes read or 0 if no end
  171. */
  172. static int av_unused get_cabac_terminate(CABACContext *c){
  173. c->range -= 2;
  174. if(c->low < c->range<<(CABAC_BITS+1)){
  175. renorm_cabac_decoder_once(c);
  176. return 0;
  177. }else{
  178. return c->bytestream - c->bytestream_start;
  179. }
  180. }
  181. #if 0
  182. /**
  183. * Get (truncated) unary binarization.
  184. */
  185. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  186. int i;
  187. for(i=0; i<max; i++){
  188. if(get_cabac(c, state)==0)
  189. return i;
  190. if(i< max_index) state++;
  191. }
  192. return truncated ? max : -1;
  193. }
  194. /**
  195. * get unary exp golomb k-th order binarization.
  196. */
  197. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  198. int i, v;
  199. int m= 1<<k;
  200. if(get_cabac(c, state)==0)
  201. return 0;
  202. if(0 < max_index) state++;
  203. for(i=1; i<max; i++){
  204. if(get_cabac(c, state)==0){
  205. if(is_signed && get_cabac_bypass(c)){
  206. return -i;
  207. }else
  208. return i;
  209. }
  210. if(i < max_index) state++;
  211. }
  212. while(get_cabac_bypass(c)){
  213. i+= m;
  214. m+= m;
  215. }
  216. v=0;
  217. while(m>>=1){
  218. v+= v + get_cabac_bypass(c);
  219. }
  220. i += v;
  221. if(is_signed && get_cabac_bypass(c)){
  222. return -i;
  223. }else
  224. return i;
  225. }
  226. #endif /* 0 */
  227. #endif /* AVCODEC_CABAC_H */