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.

430 lines
10KB

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