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.

382 lines
8.7KB

  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_start;
  37. uint8_t *bytestream;
  38. int bits_left; ///<
  39. PutBitContext pb;
  40. }CABACContext;
  41. extern const uint8_t ff_h264_lps_range[64][4];
  42. extern const uint8_t ff_h264_mps_state[64];
  43. extern const uint8_t ff_h264_lps_state[64];
  44. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  45. void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
  46. void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
  47. uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
  48. static inline void put_cabac_bit(CABACContext *c, int b){
  49. put_bits(&c->pb, 1, b);
  50. for(;c->outstanding_count; c->outstanding_count--){
  51. put_bits(&c->pb, 1, 1-b);
  52. }
  53. }
  54. static inline void renorm_cabac_encoder(CABACContext *c){
  55. while(c->range < 0x100){
  56. //FIXME optimize
  57. if(c->low<0x100){
  58. put_cabac_bit(c, 0);
  59. }else if(c->low<0x200){
  60. c->outstanding_count++;
  61. c->low -= 0x100;
  62. }else{
  63. put_cabac_bit(c, 1);
  64. c->low -= 0x200;
  65. }
  66. c->range+= c->range;
  67. c->low += c->low;
  68. }
  69. }
  70. static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  71. int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
  72. if(bit == ((*state)&1)){
  73. c->range -= RangeLPS;
  74. *state= c->mps_state[*state];
  75. }else{
  76. c->low += c->range - RangeLPS;
  77. c->range = RangeLPS;
  78. *state= c->lps_state[*state];
  79. }
  80. renorm_cabac_encoder(c);
  81. #ifdef STRICT_LIMITS
  82. c->symCount++;
  83. #endif
  84. }
  85. static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  86. assert(c->range > RangeLPS);
  87. if(!bit){
  88. c->range -= RangeLPS;
  89. }else{
  90. c->low += c->range - RangeLPS;
  91. c->range = RangeLPS;
  92. }
  93. renorm_cabac_encoder(c);
  94. #ifdef STRICT_LIMITS
  95. c->symCount++;
  96. #endif
  97. }
  98. /**
  99. * @param bit 0 -> write zero bit, !=0 write one bit
  100. */
  101. static inline void put_cabac_bypass(CABACContext *c, int bit){
  102. c->low += c->low;
  103. if(bit){
  104. c->low += c->range;
  105. }
  106. //FIXME optimize
  107. if(c->low<0x200){
  108. put_cabac_bit(c, 0);
  109. }else if(c->low<0x400){
  110. c->outstanding_count++;
  111. c->low -= 0x200;
  112. }else{
  113. put_cabac_bit(c, 1);
  114. c->low -= 0x400;
  115. }
  116. #ifdef STRICT_LIMITS
  117. c->symCount++;
  118. #endif
  119. }
  120. /**
  121. *
  122. * @return the number of bytes written
  123. */
  124. static inline int put_cabac_terminate(CABACContext *c, int bit){
  125. c->range -= 2;
  126. if(!bit){
  127. renorm_cabac_encoder(c);
  128. }else{
  129. c->low += c->range;
  130. c->range= 2;
  131. renorm_cabac_encoder(c);
  132. assert(c->low <= 0x1FF);
  133. put_cabac_bit(c, c->low>>9);
  134. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  135. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  136. }
  137. #ifdef STRICT_LIMITS
  138. c->symCount++;
  139. #endif
  140. return (put_bits_count(&c->pb)+7)>>3;
  141. }
  142. /**
  143. * put (truncated) unary binarization.
  144. */
  145. static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  146. int i;
  147. assert(v <= max);
  148. #if 1
  149. for(i=0; i<v; i++){
  150. put_cabac(c, state, 1);
  151. if(i < max_index) state++;
  152. }
  153. if(truncated==0 || v<max)
  154. put_cabac(c, state, 0);
  155. #else
  156. if(v <= max_index){
  157. for(i=0; i<v; i++){
  158. put_cabac(c, state+i, 1);
  159. }
  160. if(truncated==0 || v<max)
  161. put_cabac(c, state+i, 0);
  162. }else{
  163. for(i=0; i<=max_index; i++){
  164. put_cabac(c, state+i, 1);
  165. }
  166. for(; i<v; i++){
  167. put_cabac(c, state+max_index, 1);
  168. }
  169. if(truncated==0 || v<max)
  170. put_cabac(c, state+max_index, 0);
  171. }
  172. #endif
  173. }
  174. /**
  175. * put unary exp golomb k-th order binarization.
  176. */
  177. static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  178. int i;
  179. if(v==0)
  180. put_cabac(c, state, 0);
  181. else{
  182. const int sign= v < 0;
  183. if(is_signed) v= ABS(v);
  184. if(v<max){
  185. for(i=0; i<v; i++){
  186. put_cabac(c, state, 1);
  187. if(i < max_index) state++;
  188. }
  189. put_cabac(c, state, 0);
  190. }else{
  191. int m= 1<<k;
  192. for(i=0; i<max; i++){
  193. put_cabac(c, state, 1);
  194. if(i < max_index) state++;
  195. }
  196. v -= max;
  197. while(v >= m){ //FIXME optimize
  198. put_cabac_bypass(c, 1);
  199. v-= m;
  200. m+= m;
  201. }
  202. put_cabac_bypass(c, 0);
  203. while(m>>=1){
  204. put_cabac_bypass(c, v&m);
  205. }
  206. }
  207. if(is_signed)
  208. put_cabac_bypass(c, sign);
  209. }
  210. }
  211. static inline void renorm_cabac_decoder(CABACContext *c){
  212. while(c->range < 0x10000){
  213. c->range+= c->range;
  214. c->low+= c->low;
  215. if(--c->bits_left == 0){
  216. c->low+= *c->bytestream++;
  217. c->bits_left= 8;
  218. }
  219. }
  220. }
  221. static inline int get_cabac(CABACContext *c, uint8_t * const state){
  222. int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
  223. int bit;
  224. c->range -= RangeLPS;
  225. if(c->low < c->range){
  226. bit= (*state)&1;
  227. *state= c->mps_state[*state];
  228. }else{
  229. bit= ((*state)&1)^1;
  230. c->low -= c->range;
  231. c->range = RangeLPS;
  232. *state= c->lps_state[*state];
  233. }
  234. renorm_cabac_decoder(c);
  235. return bit;
  236. }
  237. static inline int get_cabac_static(CABACContext *c, int RangeLPS){
  238. int bit;
  239. c->range -= RangeLPS;
  240. if(c->low < c->range){
  241. bit= 0;
  242. }else{
  243. bit= 1;
  244. c->low -= c->range;
  245. c->range = RangeLPS;
  246. }
  247. renorm_cabac_decoder(c);
  248. return bit;
  249. }
  250. static inline int get_cabac_bypass(CABACContext *c){
  251. c->low += c->low;
  252. if(--c->bits_left == 0){
  253. c->low+= *c->bytestream++;
  254. c->bits_left= 8;
  255. }
  256. if(c->low < c->range){
  257. return 0;
  258. }else{
  259. c->low -= c->range;
  260. return 1;
  261. }
  262. }
  263. /**
  264. *
  265. * @return the number of bytes read or 0 if no end
  266. */
  267. static inline int get_cabac_terminate(CABACContext *c){
  268. c->range -= 2<<8;
  269. if(c->low < c->range){
  270. renorm_cabac_decoder(c);
  271. return 0;
  272. }else{
  273. return c->bytestream - c->bytestream_start;
  274. }
  275. }
  276. /**
  277. * get (truncated) unnary binarization.
  278. */
  279. static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  280. int i;
  281. for(i=0; i<max; i++){
  282. if(get_cabac(c, state)==0)
  283. return i;
  284. if(i< max_index) state++;
  285. }
  286. return truncated ? max : -1;
  287. }
  288. /**
  289. * get unary exp golomb k-th order binarization.
  290. */
  291. static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  292. int i, v;
  293. int m= 1<<k;
  294. if(get_cabac(c, state)==0)
  295. return 0;
  296. if(0 < max_index) state++;
  297. for(i=1; i<max; i++){
  298. if(get_cabac(c, state)==0){
  299. if(is_signed && get_cabac_bypass(c)){
  300. return -i;
  301. }else
  302. return i;
  303. }
  304. if(i < max_index) state++;
  305. }
  306. while(get_cabac_bypass(c)){
  307. i+= m;
  308. m+= m;
  309. }
  310. v=0;
  311. while(m>>=1){
  312. v+= v + get_cabac_bypass(c);
  313. }
  314. i += v;
  315. if(is_signed && get_cabac_bypass(c)){
  316. return -i;
  317. }else
  318. return i;
  319. }