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.

233 lines
5.5KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file cabac.h
  22. * Context Adaptive Binary Arithmetic Coder.
  23. */
  24. #undef NDEBUG
  25. #include <assert.h>
  26. typedef struct CABACContext{
  27. int low;
  28. int range;
  29. int outstanding_count;
  30. #ifdef STRICT_LIMITS
  31. int symCount;
  32. #endif
  33. uint8_t lps_range[2*64][4]; ///< rangeTabLPS
  34. uint8_t lps_state[2*64]; ///< transIdxLPS
  35. uint8_t mps_state[2*64]; ///< transIdxMPS
  36. uint8_t *bytestream;
  37. int bits_left; ///<
  38. PutBitContext pb;
  39. }CABACContext;
  40. const uint8_t ff_h264_lps_range[64][4];
  41. const uint8_t ff_h264_mps_state[64];
  42. const uint8_t ff_h264_lps_state[64];
  43. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  44. void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
  45. void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
  46. uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
  47. static inline void put_cabac_bit(CABACContext *c, int b){
  48. put_bits(&c->pb, 1, b);
  49. for(;c->outstanding_count; c->outstanding_count--){
  50. put_bits(&c->pb, 1, 1-b);
  51. }
  52. }
  53. static inline void renorm_cabac_encoder(CABACContext *c){
  54. while(c->range < 0x100){
  55. //FIXME optimize
  56. if(c->low<0x100){
  57. put_cabac_bit(c, 0);
  58. }else if(c->low<0x200){
  59. c->outstanding_count++;
  60. c->low -= 0x100;
  61. }else{
  62. put_cabac_bit(c, 1);
  63. c->low -= 0x200;
  64. }
  65. c->range+= c->range;
  66. c->low += c->low;
  67. }
  68. }
  69. static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  70. int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
  71. if(bit == ((*state)&1)){
  72. c->range -= RangeLPS;
  73. *state= c->mps_state[*state];
  74. }else{
  75. c->low += c->range - RangeLPS;
  76. c->range = RangeLPS;
  77. *state= c->lps_state[*state];
  78. }
  79. renorm_cabac_encoder(c);
  80. #ifdef STRICT_LIMITS
  81. c->symCount++;
  82. #endif
  83. }
  84. static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  85. assert(c->range > RangeLPS);
  86. if(!bit){
  87. c->range -= RangeLPS;
  88. }else{
  89. c->low += c->range - RangeLPS;
  90. c->range = RangeLPS;
  91. }
  92. renorm_cabac_encoder(c);
  93. #ifdef STRICT_LIMITS
  94. c->symCount++;
  95. #endif
  96. }
  97. static inline void put_cabac_bypass(CABACContext *c, int bit){
  98. c->low += c->low;
  99. if(bit){
  100. c->low += c->range;
  101. }
  102. //FIXME optimize
  103. if(c->low<0x200){
  104. put_cabac_bit(c, 0);
  105. }else if(c->low<0x400){
  106. c->outstanding_count++;
  107. c->low -= 0x200;
  108. }else{
  109. put_cabac_bit(c, 1);
  110. c->low -= 0x400;
  111. }
  112. #ifdef STRICT_LIMITS
  113. c->symCount++;
  114. #endif
  115. }
  116. static inline void put_cabac_terminate(CABACContext *c, int bit){
  117. c->range -= 2;
  118. if(!bit){
  119. renorm_cabac_encoder(c);
  120. }else{
  121. c->low += c->range;
  122. c->range= 2;
  123. renorm_cabac_encoder(c);
  124. assert(c->low <= 0x1FF);
  125. put_cabac_bit(c, c->low>>9);
  126. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  127. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  128. }
  129. #ifdef STRICT_LIMITS
  130. c->symCount++;
  131. #endif
  132. }
  133. static inline void renorm_cabac_decoder(CABACContext *c){
  134. while(c->range < 0x10000){
  135. c->range+= c->range;
  136. c->low+= c->low;
  137. if(--c->bits_left == 0){
  138. c->low+= *c->bytestream++;
  139. c->bits_left= 8;
  140. }
  141. }
  142. }
  143. static inline int get_cabac(CABACContext *c, uint8_t * const state){
  144. int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
  145. int bit;
  146. c->range -= RangeLPS;
  147. if(c->low < c->range){
  148. bit= (*state)&1;
  149. *state= c->mps_state[*state];
  150. }else{
  151. bit= ((*state)&1)^1;
  152. c->low -= c->range;
  153. c->range = RangeLPS;
  154. *state= c->lps_state[*state];
  155. }
  156. renorm_cabac_decoder(c);
  157. return bit;
  158. }
  159. static inline int get_cabac_static(CABACContext *c, int RangeLPS){
  160. int bit;
  161. c->range -= RangeLPS;
  162. if(c->low < c->range){
  163. bit= 0;
  164. }else{
  165. bit= 1;
  166. c->low -= c->range;
  167. c->range = RangeLPS;
  168. }
  169. renorm_cabac_decoder(c);
  170. return bit;
  171. }
  172. static inline int get_cabac_bypass(CABACContext *c){
  173. c->low += c->low;
  174. if(--c->bits_left == 0){
  175. c->low+= *c->bytestream++;
  176. c->bits_left= 8;
  177. }
  178. if(c->low < c->range){
  179. return 0;
  180. }else{
  181. c->low -= c->range;
  182. return 1;
  183. }
  184. }
  185. static inline int get_cabac_terminate(CABACContext *c){
  186. c->range -= 2<<8;
  187. if(c->low < c->range){
  188. renorm_cabac_decoder(c);
  189. return 0;
  190. }else{
  191. return 1;
  192. }
  193. }