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.

513 lines
12KB

  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. /**
  23. * @file cabac.h
  24. * Context Adaptive Binary Arithmetic Coder.
  25. */
  26. //#undef NDEBUG
  27. #include <assert.h>
  28. #define CABAC_BITS 16
  29. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  30. typedef struct CABACContext{
  31. int low;
  32. int range;
  33. int outstanding_count;
  34. #ifdef STRICT_LIMITS
  35. int symCount;
  36. #endif
  37. uint8_t lps_range[2*66][4]; ///< rangeTabLPS
  38. uint8_t lps_state[2*65]; ///< transIdxLPS
  39. uint8_t mps_state[2*65]; ///< transIdxMPS
  40. const uint8_t *bytestream_start;
  41. const uint8_t *bytestream;
  42. const uint8_t *bytestream_end;
  43. PutBitContext pb;
  44. }CABACContext;
  45. extern const uint8_t ff_h264_lps_range[64][4];
  46. extern const uint8_t ff_h264_mps_state[64];
  47. extern const uint8_t ff_h264_lps_state[64];
  48. extern const uint8_t ff_h264_norm_shift[256];
  49. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  50. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  51. void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
  52. uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
  53. static inline void put_cabac_bit(CABACContext *c, int b){
  54. put_bits(&c->pb, 1, b);
  55. for(;c->outstanding_count; c->outstanding_count--){
  56. put_bits(&c->pb, 1, 1-b);
  57. }
  58. }
  59. static inline void renorm_cabac_encoder(CABACContext *c){
  60. while(c->range < 0x100){
  61. //FIXME optimize
  62. if(c->low<0x100){
  63. put_cabac_bit(c, 0);
  64. }else if(c->low<0x200){
  65. c->outstanding_count++;
  66. c->low -= 0x100;
  67. }else{
  68. put_cabac_bit(c, 1);
  69. c->low -= 0x200;
  70. }
  71. c->range+= c->range;
  72. c->low += c->low;
  73. }
  74. }
  75. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  76. int RangeLPS= c->lps_range[*state][c->range>>6];
  77. if(bit == ((*state)&1)){
  78. c->range -= RangeLPS;
  79. *state= c->mps_state[*state];
  80. }else{
  81. c->low += c->range - RangeLPS;
  82. c->range = RangeLPS;
  83. *state= c->lps_state[*state];
  84. }
  85. renorm_cabac_encoder(c);
  86. #ifdef STRICT_LIMITS
  87. c->symCount++;
  88. #endif
  89. }
  90. static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  91. assert(c->range > RangeLPS);
  92. if(!bit){
  93. c->range -= RangeLPS;
  94. }else{
  95. c->low += c->range - RangeLPS;
  96. c->range = RangeLPS;
  97. }
  98. renorm_cabac_encoder(c);
  99. #ifdef STRICT_LIMITS
  100. c->symCount++;
  101. #endif
  102. }
  103. /**
  104. * @param bit 0 -> write zero bit, !=0 write one bit
  105. */
  106. static void put_cabac_bypass(CABACContext *c, int bit){
  107. c->low += c->low;
  108. if(bit){
  109. c->low += c->range;
  110. }
  111. //FIXME optimize
  112. if(c->low<0x200){
  113. put_cabac_bit(c, 0);
  114. }else if(c->low<0x400){
  115. c->outstanding_count++;
  116. c->low -= 0x200;
  117. }else{
  118. put_cabac_bit(c, 1);
  119. c->low -= 0x400;
  120. }
  121. #ifdef STRICT_LIMITS
  122. c->symCount++;
  123. #endif
  124. }
  125. /**
  126. *
  127. * @return the number of bytes written
  128. */
  129. static int put_cabac_terminate(CABACContext *c, int bit){
  130. c->range -= 2;
  131. if(!bit){
  132. renorm_cabac_encoder(c);
  133. }else{
  134. c->low += c->range;
  135. c->range= 2;
  136. renorm_cabac_encoder(c);
  137. assert(c->low <= 0x1FF);
  138. put_cabac_bit(c, c->low>>9);
  139. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  140. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  141. }
  142. #ifdef STRICT_LIMITS
  143. c->symCount++;
  144. #endif
  145. return (put_bits_count(&c->pb)+7)>>3;
  146. }
  147. /**
  148. * put (truncated) unary binarization.
  149. */
  150. static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  151. int i;
  152. assert(v <= max);
  153. #if 1
  154. for(i=0; i<v; i++){
  155. put_cabac(c, state, 1);
  156. if(i < max_index) state++;
  157. }
  158. if(truncated==0 || v<max)
  159. put_cabac(c, state, 0);
  160. #else
  161. if(v <= max_index){
  162. for(i=0; i<v; i++){
  163. put_cabac(c, state+i, 1);
  164. }
  165. if(truncated==0 || v<max)
  166. put_cabac(c, state+i, 0);
  167. }else{
  168. for(i=0; i<=max_index; i++){
  169. put_cabac(c, state+i, 1);
  170. }
  171. for(; i<v; i++){
  172. put_cabac(c, state+max_index, 1);
  173. }
  174. if(truncated==0 || v<max)
  175. put_cabac(c, state+max_index, 0);
  176. }
  177. #endif
  178. }
  179. /**
  180. * put unary exp golomb k-th order binarization.
  181. */
  182. static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  183. int i;
  184. if(v==0)
  185. put_cabac(c, state, 0);
  186. else{
  187. const int sign= v < 0;
  188. if(is_signed) v= ABS(v);
  189. if(v<max){
  190. for(i=0; i<v; i++){
  191. put_cabac(c, state, 1);
  192. if(i < max_index) state++;
  193. }
  194. put_cabac(c, state, 0);
  195. }else{
  196. int m= 1<<k;
  197. for(i=0; i<max; i++){
  198. put_cabac(c, state, 1);
  199. if(i < max_index) state++;
  200. }
  201. v -= max;
  202. while(v >= m){ //FIXME optimize
  203. put_cabac_bypass(c, 1);
  204. v-= m;
  205. m+= m;
  206. }
  207. put_cabac_bypass(c, 0);
  208. while(m>>=1){
  209. put_cabac_bypass(c, v&m);
  210. }
  211. }
  212. if(is_signed)
  213. put_cabac_bypass(c, sign);
  214. }
  215. }
  216. static void refill(CABACContext *c){
  217. if(c->bytestream <= c->bytestream_end)
  218. #if CABAC_BITS == 16
  219. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  220. #else
  221. c->low+= c->bytestream[0]<<1;
  222. #endif
  223. c->low -= CABAC_MASK;
  224. c->bytestream+= CABAC_BITS/8;
  225. }
  226. #if 1 /* all use commented */
  227. static void refill2(CABACContext *c){
  228. int i, x;
  229. x= c->low ^ (c->low-1);
  230. i= 9 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
  231. x= -CABAC_MASK;
  232. if(c->bytestream <= c->bytestream_end)
  233. #if CABAC_BITS == 16
  234. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  235. #else
  236. x+= c->bytestream[0]<<1;
  237. #endif
  238. c->low += x<<i;
  239. c->bytestream+= CABAC_BITS/8;
  240. }
  241. #endif
  242. static inline void renorm_cabac_decoder(CABACContext *c){
  243. while(c->range < (0x200 << CABAC_BITS)){
  244. c->range+= c->range;
  245. c->low+= c->low;
  246. if(!(c->low & CABAC_MASK))
  247. refill(c);
  248. }
  249. }
  250. static inline void renorm_cabac_decoder_once(CABACContext *c){
  251. #ifdef ARCH_X86_DISABLED
  252. int temp;
  253. #if 0
  254. //P3:683 athlon:475
  255. asm(
  256. "lea -0x2000000(%0), %2 \n\t"
  257. "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
  258. "shl %%cl, %0 \n\t"
  259. "shl %%cl, %1 \n\t"
  260. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  261. );
  262. #elif 0
  263. //P3:680 athlon:474
  264. asm(
  265. "cmp $0x2000000, %0 \n\t"
  266. "setb %%cl \n\t" //FIXME 31->63 for x86-64
  267. "shl %%cl, %0 \n\t"
  268. "shl %%cl, %1 \n\t"
  269. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  270. );
  271. #elif 1
  272. int temp2;
  273. //P3:665 athlon:517
  274. asm(
  275. "lea -0x2000000(%0), %%eax \n\t"
  276. "cdq \n\t"
  277. "mov %0, %%eax \n\t"
  278. "and %%edx, %0 \n\t"
  279. "and %1, %%edx \n\t"
  280. "add %%eax, %0 \n\t"
  281. "add %%edx, %1 \n\t"
  282. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  283. );
  284. #elif 0
  285. int temp2;
  286. //P3:673 athlon:509
  287. asm(
  288. "cmp $0x2000000, %0 \n\t"
  289. "sbb %%edx, %%edx \n\t"
  290. "mov %0, %%eax \n\t"
  291. "and %%edx, %0 \n\t"
  292. "and %1, %%edx \n\t"
  293. "add %%eax, %0 \n\t"
  294. "add %%edx, %1 \n\t"
  295. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  296. );
  297. #else
  298. int temp2;
  299. //P3:677 athlon:511
  300. asm(
  301. "cmp $0x2000000, %0 \n\t"
  302. "lea (%0, %0), %%eax \n\t"
  303. "lea (%1, %1), %%edx \n\t"
  304. "cmovb %%eax, %0 \n\t"
  305. "cmovb %%edx, %1 \n\t"
  306. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  307. );
  308. #endif
  309. #else
  310. //P3:675 athlon:476
  311. int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
  312. c->range<<= shift;
  313. c->low <<= shift;
  314. #endif
  315. if(!(c->low & CABAC_MASK))
  316. refill(c);
  317. }
  318. static int get_cabac(CABACContext *c, uint8_t * const state){
  319. //FIXME gcc generates duplicate load/stores for c->low and c->range
  320. //START_TIMER
  321. int s = *state;
  322. int RangeLPS= c->lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
  323. int bit, lps_mask attribute_unused;
  324. c->range -= RangeLPS;
  325. #if 1
  326. if(c->low < c->range){
  327. bit= s&1;
  328. #ifdef ARCH_X86
  329. //P3:627
  330. asm(
  331. "addb $2, %b0 \n\t"
  332. " js 1f \n\t"
  333. "movb %b0, %1 \n\t"
  334. "1: \n\t"
  335. : "+q"(s), "=m"(*state)
  336. );
  337. #else
  338. *state= c->mps_state[s]; //P3:655
  339. /* if(s<126) //P3:657
  340. *state= s+2;*/
  341. s+=2; //P3:631
  342. if(s<128)
  343. *state= s;
  344. #endif
  345. renorm_cabac_decoder_once(c);
  346. }else{
  347. bit= ff_h264_norm_shift[RangeLPS>>17];
  348. c->low -= c->range;
  349. *state= c->lps_state[s];
  350. // c->range = RangeLPS;
  351. // renorm_cabac_decoder(c);
  352. c->range = RangeLPS<<bit;
  353. c->low <<= bit;
  354. bit= (s&1)^1;
  355. if(!(c->low & 0xFFFF)){
  356. refill2(c);
  357. }
  358. }
  359. #else
  360. lps_mask= (c->range - c->low)>>31;
  361. c->low -= c->range & lps_mask;
  362. c->range += (RangeLPS - c->range) & lps_mask;
  363. bit= (s^lps_mask)&1;
  364. *state= c->mps_state[s - (128&lps_mask)];
  365. lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
  366. c->range<<= lps_mask;
  367. c->low <<= lps_mask;
  368. if(!(c->low & CABAC_MASK))
  369. refill2(c);
  370. #endif
  371. //STOP_TIMER("get_cabac")
  372. return bit;
  373. }
  374. static int get_cabac_bypass(CABACContext *c){
  375. c->low += c->low;
  376. if(!(c->low & CABAC_MASK))
  377. refill(c);
  378. if(c->low < c->range){
  379. return 0;
  380. }else{
  381. c->low -= c->range;
  382. return 1;
  383. }
  384. }
  385. /**
  386. *
  387. * @return the number of bytes read or 0 if no end
  388. */
  389. static int get_cabac_terminate(CABACContext *c){
  390. c->range -= 4<<CABAC_BITS;
  391. if(c->low < c->range){
  392. renorm_cabac_decoder_once(c);
  393. return 0;
  394. }else{
  395. return c->bytestream - c->bytestream_start;
  396. }
  397. }
  398. /**
  399. * get (truncated) unnary binarization.
  400. */
  401. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  402. int i;
  403. for(i=0; i<max; i++){
  404. if(get_cabac(c, state)==0)
  405. return i;
  406. if(i< max_index) state++;
  407. }
  408. return truncated ? max : -1;
  409. }
  410. /**
  411. * get unary exp golomb k-th order binarization.
  412. */
  413. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  414. int i, v;
  415. int m= 1<<k;
  416. if(get_cabac(c, state)==0)
  417. return 0;
  418. if(0 < max_index) state++;
  419. for(i=1; i<max; i++){
  420. if(get_cabac(c, state)==0){
  421. if(is_signed && get_cabac_bypass(c)){
  422. return -i;
  423. }else
  424. return i;
  425. }
  426. if(i < max_index) state++;
  427. }
  428. while(get_cabac_bypass(c)){
  429. i+= m;
  430. m+= m;
  431. }
  432. v=0;
  433. while(m>>=1){
  434. v+= v + get_cabac_bypass(c);
  435. }
  436. i += v;
  437. if(is_signed && get_cabac_bypass(c)){
  438. return -i;
  439. }else
  440. return i;
  441. }