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.

450 lines
9.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 file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Context Adaptive Binary Arithmetic Coder.
  24. */
  25. #ifndef AVCODEC_CABAC_H
  26. #define AVCODEC_CABAC_H
  27. #include <stddef.h>
  28. #include "put_bits.h"
  29. //#undef NDEBUG
  30. #include <assert.h>
  31. #define CABAC_BITS 16
  32. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  33. typedef struct CABACContext{
  34. int low;
  35. int range;
  36. int outstanding_count;
  37. #ifdef STRICT_LIMITS
  38. int symCount;
  39. #endif
  40. const uint8_t *bytestream_start;
  41. const uint8_t *bytestream;
  42. const uint8_t *bytestream_end;
  43. PutBitContext pb;
  44. }CABACContext;
  45. extern uint8_t ff_h264_mlps_state[4*64];
  46. extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
  47. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  48. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  49. extern const uint8_t ff_h264_norm_shift[512];
  50. #if ARCH_X86
  51. # include "x86/cabac.h"
  52. #endif
  53. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  54. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  55. void ff_init_cabac_states(CABACContext *c);
  56. static inline void put_cabac_bit(CABACContext *c, int b){
  57. put_bits(&c->pb, 1, b);
  58. for(;c->outstanding_count; c->outstanding_count--){
  59. put_bits(&c->pb, 1, 1-b);
  60. }
  61. }
  62. static inline void renorm_cabac_encoder(CABACContext *c){
  63. while(c->range < 0x100){
  64. //FIXME optimize
  65. if(c->low<0x100){
  66. put_cabac_bit(c, 0);
  67. }else if(c->low<0x200){
  68. c->outstanding_count++;
  69. c->low -= 0x100;
  70. }else{
  71. put_cabac_bit(c, 1);
  72. c->low -= 0x200;
  73. }
  74. c->range+= c->range;
  75. c->low += c->low;
  76. }
  77. }
  78. #ifdef TEST
  79. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  80. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  81. if(bit == ((*state)&1)){
  82. c->range -= RangeLPS;
  83. *state= ff_h264_mps_state[*state];
  84. }else{
  85. c->low += c->range - RangeLPS;
  86. c->range = RangeLPS;
  87. *state= ff_h264_lps_state[*state];
  88. }
  89. renorm_cabac_encoder(c);
  90. #ifdef STRICT_LIMITS
  91. c->symCount++;
  92. #endif
  93. }
  94. static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  95. assert(c->range > RangeLPS);
  96. if(!bit){
  97. c->range -= RangeLPS;
  98. }else{
  99. c->low += c->range - RangeLPS;
  100. c->range = RangeLPS;
  101. }
  102. renorm_cabac_encoder(c);
  103. #ifdef STRICT_LIMITS
  104. c->symCount++;
  105. #endif
  106. }
  107. /**
  108. * @param bit 0 -> write zero bit, !=0 write one bit
  109. */
  110. static void put_cabac_bypass(CABACContext *c, int bit){
  111. c->low += c->low;
  112. if(bit){
  113. c->low += c->range;
  114. }
  115. //FIXME optimize
  116. if(c->low<0x200){
  117. put_cabac_bit(c, 0);
  118. }else if(c->low<0x400){
  119. c->outstanding_count++;
  120. c->low -= 0x200;
  121. }else{
  122. put_cabac_bit(c, 1);
  123. c->low -= 0x400;
  124. }
  125. #ifdef STRICT_LIMITS
  126. c->symCount++;
  127. #endif
  128. }
  129. /**
  130. *
  131. * @return the number of bytes written
  132. */
  133. static int put_cabac_terminate(CABACContext *c, int bit){
  134. c->range -= 2;
  135. if(!bit){
  136. renorm_cabac_encoder(c);
  137. }else{
  138. c->low += c->range;
  139. c->range= 2;
  140. renorm_cabac_encoder(c);
  141. assert(c->low <= 0x1FF);
  142. put_cabac_bit(c, c->low>>9);
  143. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  144. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  145. }
  146. #ifdef STRICT_LIMITS
  147. c->symCount++;
  148. #endif
  149. return (put_bits_count(&c->pb)+7)>>3;
  150. }
  151. /**
  152. * put (truncated) unary binarization.
  153. */
  154. static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  155. int i;
  156. assert(v <= max);
  157. #if 1
  158. for(i=0; i<v; i++){
  159. put_cabac(c, state, 1);
  160. if(i < max_index) state++;
  161. }
  162. if(truncated==0 || v<max)
  163. put_cabac(c, state, 0);
  164. #else
  165. if(v <= max_index){
  166. for(i=0; i<v; i++){
  167. put_cabac(c, state+i, 1);
  168. }
  169. if(truncated==0 || v<max)
  170. put_cabac(c, state+i, 0);
  171. }else{
  172. for(i=0; i<=max_index; i++){
  173. put_cabac(c, state+i, 1);
  174. }
  175. for(; i<v; i++){
  176. put_cabac(c, state+max_index, 1);
  177. }
  178. if(truncated==0 || v<max)
  179. put_cabac(c, state+max_index, 0);
  180. }
  181. #endif
  182. }
  183. /**
  184. * put unary exp golomb k-th order binarization.
  185. */
  186. static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  187. int i;
  188. if(v==0)
  189. put_cabac(c, state, 0);
  190. else{
  191. const int sign= v < 0;
  192. if(is_signed) v= FFABS(v);
  193. if(v<max){
  194. for(i=0; i<v; i++){
  195. put_cabac(c, state, 1);
  196. if(i < max_index) state++;
  197. }
  198. put_cabac(c, state, 0);
  199. }else{
  200. int m= 1<<k;
  201. for(i=0; i<max; i++){
  202. put_cabac(c, state, 1);
  203. if(i < max_index) state++;
  204. }
  205. v -= max;
  206. while(v >= m){ //FIXME optimize
  207. put_cabac_bypass(c, 1);
  208. v-= m;
  209. m+= m;
  210. }
  211. put_cabac_bypass(c, 0);
  212. while(m>>=1){
  213. put_cabac_bypass(c, v&m);
  214. }
  215. }
  216. if(is_signed)
  217. put_cabac_bypass(c, sign);
  218. }
  219. }
  220. #endif /* TEST */
  221. static void refill(CABACContext *c){
  222. #if CABAC_BITS == 16
  223. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  224. #else
  225. c->low+= c->bytestream[0]<<1;
  226. #endif
  227. c->low -= CABAC_MASK;
  228. c->bytestream+= CABAC_BITS/8;
  229. }
  230. static inline void renorm_cabac_decoder(CABACContext *c){
  231. while(c->range < 0x100){
  232. c->range+= c->range;
  233. c->low+= c->low;
  234. if(!(c->low & CABAC_MASK))
  235. refill(c);
  236. }
  237. }
  238. static inline void renorm_cabac_decoder_once(CABACContext *c){
  239. int shift= (uint32_t)(c->range - 0x100)>>31;
  240. c->range<<= shift;
  241. c->low <<= shift;
  242. if(!(c->low & CABAC_MASK))
  243. refill(c);
  244. }
  245. #ifndef get_cabac_inline
  246. static void refill2(CABACContext *c){
  247. int i, x;
  248. x= c->low ^ (c->low-1);
  249. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  250. x= -CABAC_MASK;
  251. #if CABAC_BITS == 16
  252. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  253. #else
  254. x+= c->bytestream[0]<<1;
  255. #endif
  256. c->low += x<<i;
  257. c->bytestream+= CABAC_BITS/8;
  258. }
  259. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  260. int s = *state;
  261. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  262. int bit, lps_mask;
  263. c->range -= RangeLPS;
  264. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  265. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  266. c->range += (RangeLPS - c->range) & lps_mask;
  267. s^=lps_mask;
  268. *state= (ff_h264_mlps_state+128)[s];
  269. bit= s&1;
  270. lps_mask= ff_h264_norm_shift[c->range];
  271. c->range<<= lps_mask;
  272. c->low <<= lps_mask;
  273. if(!(c->low & CABAC_MASK))
  274. refill2(c);
  275. return bit;
  276. }
  277. #endif
  278. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  279. return get_cabac_inline(c,state);
  280. }
  281. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  282. return get_cabac_inline(c,state);
  283. }
  284. static int av_unused get_cabac_bypass(CABACContext *c){
  285. int range;
  286. c->low += c->low;
  287. if(!(c->low & CABAC_MASK))
  288. refill(c);
  289. range= c->range<<(CABAC_BITS+1);
  290. if(c->low < range){
  291. return 0;
  292. }else{
  293. c->low -= range;
  294. return 1;
  295. }
  296. }
  297. #ifndef get_cabac_bypass_sign
  298. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  299. int range, mask;
  300. c->low += c->low;
  301. if(!(c->low & CABAC_MASK))
  302. refill(c);
  303. range= c->range<<(CABAC_BITS+1);
  304. c->low -= range;
  305. mask= c->low >> 31;
  306. range &= mask;
  307. c->low += range;
  308. return (val^mask)-mask;
  309. }
  310. #endif
  311. /**
  312. *
  313. * @return the number of bytes read or 0 if no end
  314. */
  315. static int av_unused get_cabac_terminate(CABACContext *c){
  316. c->range -= 2;
  317. if(c->low < c->range<<(CABAC_BITS+1)){
  318. renorm_cabac_decoder_once(c);
  319. return 0;
  320. }else{
  321. return c->bytestream - c->bytestream_start;
  322. }
  323. }
  324. #if 0
  325. /**
  326. * Get (truncated) unary binarization.
  327. */
  328. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  329. int i;
  330. for(i=0; i<max; i++){
  331. if(get_cabac(c, state)==0)
  332. return i;
  333. if(i< max_index) state++;
  334. }
  335. return truncated ? max : -1;
  336. }
  337. /**
  338. * get unary exp golomb k-th order binarization.
  339. */
  340. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  341. int i, v;
  342. int m= 1<<k;
  343. if(get_cabac(c, state)==0)
  344. return 0;
  345. if(0 < max_index) state++;
  346. for(i=1; i<max; i++){
  347. if(get_cabac(c, state)==0){
  348. if(is_signed && get_cabac_bypass(c)){
  349. return -i;
  350. }else
  351. return i;
  352. }
  353. if(i < max_index) state++;
  354. }
  355. while(get_cabac_bypass(c)){
  356. i+= m;
  357. m+= m;
  358. }
  359. v=0;
  360. while(m>>=1){
  361. v+= v + get_cabac_bypass(c);
  362. }
  363. i += v;
  364. if(is_signed && get_cabac_bypass(c)){
  365. return -i;
  366. }else
  367. return i;
  368. }
  369. #endif /* 0 */
  370. #endif /* AVCODEC_CABAC_H */