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.

387 lines
8.9KB

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