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.

367 lines
8.4KB

  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 sign, 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. if(v<max){
  177. for(i=0; i<v; i++){
  178. put_cabac(c, state, 1);
  179. if(i < max_index) state++;
  180. }
  181. put_cabac(c, state, 0);
  182. }else{
  183. int m= 1<<k;
  184. for(i=0; i<max; i++){
  185. put_cabac(c, state, 1);
  186. if(i < max_index) state++;
  187. }
  188. v -= max;
  189. while(v >= m){ //FIXME optimize
  190. put_cabac_bypass(c, 1);
  191. v-= m;
  192. m+= m;
  193. }
  194. put_cabac_bypass(c, 0);
  195. while(m>>=1){
  196. put_cabac_bypass(c, v&m);
  197. }
  198. }
  199. if(is_signed)
  200. put_cabac_bypass(c, sign);
  201. }
  202. }
  203. static inline void renorm_cabac_decoder(CABACContext *c){
  204. while(c->range < 0x10000){
  205. c->range+= c->range;
  206. c->low+= c->low;
  207. if(--c->bits_left == 0){
  208. c->low+= *c->bytestream++;
  209. c->bits_left= 8;
  210. }
  211. }
  212. }
  213. static inline int get_cabac(CABACContext *c, uint8_t * const state){
  214. int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
  215. int bit;
  216. c->range -= RangeLPS;
  217. if(c->low < c->range){
  218. bit= (*state)&1;
  219. *state= c->mps_state[*state];
  220. }else{
  221. bit= ((*state)&1)^1;
  222. c->low -= c->range;
  223. c->range = RangeLPS;
  224. *state= c->lps_state[*state];
  225. }
  226. renorm_cabac_decoder(c);
  227. return bit;
  228. }
  229. static inline int get_cabac_static(CABACContext *c, int RangeLPS){
  230. int bit;
  231. c->range -= RangeLPS;
  232. if(c->low < c->range){
  233. bit= 0;
  234. }else{
  235. bit= 1;
  236. c->low -= c->range;
  237. c->range = RangeLPS;
  238. }
  239. renorm_cabac_decoder(c);
  240. return bit;
  241. }
  242. static inline int get_cabac_bypass(CABACContext *c){
  243. c->low += c->low;
  244. if(--c->bits_left == 0){
  245. c->low+= *c->bytestream++;
  246. c->bits_left= 8;
  247. }
  248. if(c->low < c->range){
  249. return 0;
  250. }else{
  251. c->low -= c->range;
  252. return 1;
  253. }
  254. }
  255. static inline int get_cabac_terminate(CABACContext *c){
  256. c->range -= 2<<8;
  257. if(c->low < c->range){
  258. renorm_cabac_decoder(c);
  259. return 0;
  260. }else{
  261. return 1;
  262. }
  263. }
  264. /**
  265. * get (truncated) unnary binarization.
  266. */
  267. static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  268. int i;
  269. for(i=0; i<max; i++){
  270. if(get_cabac(c, state)==0)
  271. return i;
  272. if(i< max_index) state++;
  273. }
  274. return truncated ? max : -1;
  275. }
  276. /**
  277. * get unary exp golomb k-th order binarization.
  278. */
  279. static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  280. int i, v;
  281. int m= 1<<k;
  282. if(get_cabac(c, state)==0)
  283. return 0;
  284. if(0 < max_index) state++;
  285. for(i=1; i<max; i++){
  286. if(get_cabac(c, state)==0){
  287. if(is_signed && get_cabac_bypass(c)){
  288. return -i;
  289. }else
  290. return i;
  291. }
  292. if(i < max_index) state++;
  293. }
  294. while(get_cabac_bypass(c)){
  295. i+= m;
  296. m+= m;
  297. }
  298. v=0;
  299. while(m>>=1){
  300. v+= v + get_cabac_bypass(c);
  301. }
  302. i += v;
  303. if(is_signed && get_cabac_bypass(c)){
  304. return -i;
  305. }else
  306. return i;
  307. }