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.

371 lines
8.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. /**
  98. * @param bit 0 -> write zero bit, !=0 write one bit
  99. */
  100. static inline void put_cabac_bypass(CABACContext *c, int bit){
  101. c->low += c->low;
  102. if(bit){
  103. c->low += c->range;
  104. }
  105. //FIXME optimize
  106. if(c->low<0x200){
  107. put_cabac_bit(c, 0);
  108. }else if(c->low<0x400){
  109. c->outstanding_count++;
  110. c->low -= 0x200;
  111. }else{
  112. put_cabac_bit(c, 1);
  113. c->low -= 0x400;
  114. }
  115. #ifdef STRICT_LIMITS
  116. c->symCount++;
  117. #endif
  118. }
  119. static inline void put_cabac_terminate(CABACContext *c, int bit){
  120. c->range -= 2;
  121. if(!bit){
  122. renorm_cabac_encoder(c);
  123. }else{
  124. c->low += c->range;
  125. c->range= 2;
  126. renorm_cabac_encoder(c);
  127. assert(c->low <= 0x1FF);
  128. put_cabac_bit(c, c->low>>9);
  129. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  130. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  131. }
  132. #ifdef STRICT_LIMITS
  133. c->symCount++;
  134. #endif
  135. }
  136. /**
  137. * put (truncated) unary binarization.
  138. */
  139. static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  140. int i;
  141. assert(v <= max);
  142. #if 1
  143. for(i=0; i<v; i++){
  144. put_cabac(c, state, 1);
  145. if(i < max_index) state++;
  146. }
  147. if(truncated==0 || v<max)
  148. put_cabac(c, state, 0);
  149. #else
  150. if(v <= max_index){
  151. for(i=0; i<v; i++){
  152. put_cabac(c, state+i, 1);
  153. }
  154. if(truncated==0 || v<max)
  155. put_cabac(c, state+i, 0);
  156. }else{
  157. for(i=0; i<=max_index; i++){
  158. put_cabac(c, state+i, 1);
  159. }
  160. for(; i<v; i++){
  161. put_cabac(c, state+max_index, 1);
  162. }
  163. if(truncated==0 || v<max)
  164. put_cabac(c, state+max_index, 0);
  165. }
  166. #endif
  167. }
  168. /**
  169. * put unary exp golomb k-th order binarization.
  170. */
  171. static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  172. int i;
  173. if(v==0)
  174. put_cabac(c, state, 0);
  175. else{
  176. const int sign= v < 0;
  177. if(is_signed) v= ABS(v);
  178. if(v<max){
  179. for(i=0; i<v; i++){
  180. put_cabac(c, state, 1);
  181. if(i < max_index) state++;
  182. }
  183. put_cabac(c, state, 0);
  184. }else{
  185. int m= 1<<k;
  186. for(i=0; i<max; i++){
  187. put_cabac(c, state, 1);
  188. if(i < max_index) state++;
  189. }
  190. v -= max;
  191. while(v >= m){ //FIXME optimize
  192. put_cabac_bypass(c, 1);
  193. v-= m;
  194. m+= m;
  195. }
  196. put_cabac_bypass(c, 0);
  197. while(m>>=1){
  198. put_cabac_bypass(c, v&m);
  199. }
  200. }
  201. if(is_signed)
  202. put_cabac_bypass(c, sign);
  203. }
  204. }
  205. static inline void renorm_cabac_decoder(CABACContext *c){
  206. while(c->range < 0x10000){
  207. c->range+= c->range;
  208. c->low+= c->low;
  209. if(--c->bits_left == 0){
  210. c->low+= *c->bytestream++;
  211. c->bits_left= 8;
  212. }
  213. }
  214. }
  215. static inline int get_cabac(CABACContext *c, uint8_t * const state){
  216. int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
  217. int bit;
  218. c->range -= RangeLPS;
  219. if(c->low < c->range){
  220. bit= (*state)&1;
  221. *state= c->mps_state[*state];
  222. }else{
  223. bit= ((*state)&1)^1;
  224. c->low -= c->range;
  225. c->range = RangeLPS;
  226. *state= c->lps_state[*state];
  227. }
  228. renorm_cabac_decoder(c);
  229. return bit;
  230. }
  231. static inline int get_cabac_static(CABACContext *c, int RangeLPS){
  232. int bit;
  233. c->range -= RangeLPS;
  234. if(c->low < c->range){
  235. bit= 0;
  236. }else{
  237. bit= 1;
  238. c->low -= c->range;
  239. c->range = RangeLPS;
  240. }
  241. renorm_cabac_decoder(c);
  242. return bit;
  243. }
  244. static inline int get_cabac_bypass(CABACContext *c){
  245. c->low += c->low;
  246. if(--c->bits_left == 0){
  247. c->low+= *c->bytestream++;
  248. c->bits_left= 8;
  249. }
  250. if(c->low < c->range){
  251. return 0;
  252. }else{
  253. c->low -= c->range;
  254. return 1;
  255. }
  256. }
  257. static inline int get_cabac_terminate(CABACContext *c){
  258. c->range -= 2<<8;
  259. if(c->low < c->range){
  260. renorm_cabac_decoder(c);
  261. return 0;
  262. }else{
  263. return 1;
  264. }
  265. }
  266. /**
  267. * get (truncated) unnary binarization.
  268. */
  269. static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  270. int i;
  271. for(i=0; i<max; i++){
  272. if(get_cabac(c, state)==0)
  273. return i;
  274. if(i< max_index) state++;
  275. }
  276. return truncated ? max : -1;
  277. }
  278. /**
  279. * get unary exp golomb k-th order binarization.
  280. */
  281. static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  282. int i, v;
  283. int m= 1<<k;
  284. if(get_cabac(c, state)==0)
  285. return 0;
  286. if(0 < max_index) state++;
  287. for(i=1; i<max; i++){
  288. if(get_cabac(c, state)==0){
  289. if(is_signed && get_cabac_bypass(c)){
  290. return -i;
  291. }else
  292. return i;
  293. }
  294. if(i < max_index) state++;
  295. }
  296. while(get_cabac_bypass(c)){
  297. i+= m;
  298. m+= m;
  299. }
  300. v=0;
  301. while(m>>=1){
  302. v+= v + get_cabac_bypass(c);
  303. }
  304. i += v;
  305. if(is_signed && get_cabac_bypass(c)){
  306. return -i;
  307. }else
  308. return i;
  309. }