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.

649 lines
18KB

  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. #define BRANCHLESS_CABAC_DECODER 1
  31. #define CMOV_IS_FAST 1
  32. typedef struct CABACContext{
  33. int low;
  34. int range;
  35. int outstanding_count;
  36. #ifdef STRICT_LIMITS
  37. int symCount;
  38. #endif
  39. const uint8_t *bytestream_start;
  40. const uint8_t *bytestream;
  41. const uint8_t *bytestream_end;
  42. PutBitContext pb;
  43. }CABACContext;
  44. extern uint8_t ff_h264_lps_range[2*65][4]; ///< rangeTabLPS
  45. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  46. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  47. extern const uint8_t ff_h264_norm_shift[128];
  48. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  49. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  50. void ff_init_cabac_states(CABACContext *c);
  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 void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  74. int RangeLPS= ff_h264_lps_range[*state][c->range>>6];
  75. if(bit == ((*state)&1)){
  76. c->range -= RangeLPS;
  77. *state= ff_h264_mps_state[*state];
  78. }else{
  79. c->low += c->range - RangeLPS;
  80. c->range = RangeLPS;
  81. *state= ff_h264_lps_state[*state];
  82. }
  83. renorm_cabac_encoder(c);
  84. #ifdef STRICT_LIMITS
  85. c->symCount++;
  86. #endif
  87. }
  88. static 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 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 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 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 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 CABAC_BITS == 16
  216. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  217. #else
  218. c->low+= c->bytestream[0]<<1;
  219. #endif
  220. c->low -= CABAC_MASK;
  221. c->bytestream+= CABAC_BITS/8;
  222. }
  223. static void refill2(CABACContext *c){
  224. int i, x;
  225. x= c->low ^ (c->low-1);
  226. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
  227. x= -CABAC_MASK;
  228. #if CABAC_BITS == 16
  229. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  230. #else
  231. x+= c->bytestream[0]<<1;
  232. #endif
  233. c->low += x<<i;
  234. c->bytestream+= CABAC_BITS/8;
  235. }
  236. static inline void renorm_cabac_decoder(CABACContext *c){
  237. while(c->range < (0x200 << CABAC_BITS)){
  238. c->range+= c->range;
  239. c->low+= c->low;
  240. if(!(c->low & CABAC_MASK))
  241. refill(c);
  242. }
  243. }
  244. static inline void renorm_cabac_decoder_once(CABACContext *c){
  245. #ifdef ARCH_X86_DISABLED
  246. int temp;
  247. #if 0
  248. //P3:683 athlon:475
  249. asm(
  250. "lea -0x2000000(%0), %2 \n\t"
  251. "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
  252. "shl %%cl, %0 \n\t"
  253. "shl %%cl, %1 \n\t"
  254. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  255. );
  256. #elif 0
  257. //P3:680 athlon:474
  258. asm(
  259. "cmp $0x2000000, %0 \n\t"
  260. "setb %%cl \n\t" //FIXME 31->63 for x86-64
  261. "shl %%cl, %0 \n\t"
  262. "shl %%cl, %1 \n\t"
  263. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  264. );
  265. #elif 1
  266. int temp2;
  267. //P3:665 athlon:517
  268. asm(
  269. "lea -0x2000000(%0), %%eax \n\t"
  270. "cdq \n\t"
  271. "mov %0, %%eax \n\t"
  272. "and %%edx, %0 \n\t"
  273. "and %1, %%edx \n\t"
  274. "add %%eax, %0 \n\t"
  275. "add %%edx, %1 \n\t"
  276. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  277. );
  278. #elif 0
  279. int temp2;
  280. //P3:673 athlon:509
  281. asm(
  282. "cmp $0x2000000, %0 \n\t"
  283. "sbb %%edx, %%edx \n\t"
  284. "mov %0, %%eax \n\t"
  285. "and %%edx, %0 \n\t"
  286. "and %1, %%edx \n\t"
  287. "add %%eax, %0 \n\t"
  288. "add %%edx, %1 \n\t"
  289. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  290. );
  291. #else
  292. int temp2;
  293. //P3:677 athlon:511
  294. asm(
  295. "cmp $0x2000000, %0 \n\t"
  296. "lea (%0, %0), %%eax \n\t"
  297. "lea (%1, %1), %%edx \n\t"
  298. "cmovb %%eax, %0 \n\t"
  299. "cmovb %%edx, %1 \n\t"
  300. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  301. );
  302. #endif
  303. #else
  304. //P3:675 athlon:476
  305. int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
  306. c->range<<= shift;
  307. c->low <<= shift;
  308. #endif
  309. if(!(c->low & CABAC_MASK))
  310. refill(c);
  311. }
  312. static int get_cabac(CABACContext *c, uint8_t * const state){
  313. //FIXME gcc generates duplicate load/stores for c->low and c->range
  314. #ifdef ARCH_X86
  315. int bit;
  316. #define LOW "0"
  317. #define RANGE "4"
  318. #define BYTESTART "12"
  319. #define BYTE "16"
  320. #define BYTEEND "20"
  321. #ifndef BRANCHLESS_CABAC_DECODER
  322. asm volatile(
  323. "movzbl (%1), %%eax \n\t"
  324. "movl "RANGE "(%2), %%ebx \n\t"
  325. "movl "RANGE "(%2), %%edx \n\t"
  326. "shrl $23, %%ebx \n\t"
  327. "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
  328. "shll $17, %%esi \n\t"
  329. "movl "LOW "(%2), %%ebx \n\t"
  330. //eax:state ebx:low, edx:range, esi:RangeLPS
  331. "subl %%esi, %%edx \n\t"
  332. "cmpl %%edx, %%ebx \n\t"
  333. " ja 1f \n\t"
  334. #if 1
  335. //athlon:4067 P3:4110
  336. "lea -0x2000000(%%edx), %%ecx \n\t"
  337. "shr $31, %%ecx \n\t"
  338. "shl %%cl, %%edx \n\t"
  339. "shl %%cl, %%ebx \n\t"
  340. #else
  341. //athlon:4057 P3:4130
  342. "cmp $0x2000000, %%edx \n\t" //FIXME avoidable
  343. "setb %%cl \n\t"
  344. "shl %%cl, %%edx \n\t"
  345. "shl %%cl, %%ebx \n\t"
  346. #endif
  347. "movzbl "MANGLE(ff_h264_mps_state)"(%%eax), %%ecx \n\t"
  348. "movb %%cl, (%1) \n\t"
  349. //eax:state ebx:low, edx:range, esi:RangeLPS
  350. "test %%bx, %%bx \n\t"
  351. " jnz 2f \n\t"
  352. "movl "BYTE "(%2), %%esi \n\t"
  353. "subl $0xFFFF, %%ebx \n\t"
  354. "movzwl (%%esi), %%ecx \n\t"
  355. "bswap %%ecx \n\t"
  356. "shrl $15, %%ecx \n\t"
  357. "addl $2, %%esi \n\t"
  358. "addl %%ecx, %%ebx \n\t"
  359. "movl %%esi, "BYTE "(%2) \n\t"
  360. "jmp 2f \n\t"
  361. "1: \n\t"
  362. //eax:state ebx:low, edx:range, esi:RangeLPS
  363. "subl %%edx, %%ebx \n\t"
  364. "movl %%esi, %%edx \n\t"
  365. "shr $19, %%esi \n\t"
  366. "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx \n\t"
  367. "movb %%cl, (%1) \n\t"
  368. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  369. "shll %%cl, %%ebx \n\t"
  370. "shll %%cl, %%edx \n\t"
  371. "addl $1, %%eax \n\t"
  372. "test %%bx, %%bx \n\t"
  373. " jnz 2f \n\t"
  374. "movl "BYTE "(%2), %%ecx \n\t"
  375. "movzwl (%%ecx), %%esi \n\t"
  376. "bswap %%esi \n\t"
  377. "shrl $15, %%esi \n\t"
  378. "subl $0xFFFF, %%esi \n\t"
  379. "addl $2, %%ecx \n\t"
  380. "movl %%ecx, "BYTE "(%2) \n\t"
  381. "leal -1(%%ebx), %%ecx \n\t"
  382. "xorl %%ebx, %%ecx \n\t"
  383. "shrl $17, %%ecx \n\t"
  384. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  385. "neg %%ecx \n\t"
  386. "add $7, %%ecx \n\t"
  387. "shll %%cl , %%esi \n\t"
  388. "addl %%esi, %%ebx \n\t"
  389. "2: \n\t"
  390. "movl %%edx, "RANGE "(%2) \n\t"
  391. "movl %%ebx, "LOW "(%2) \n\t"
  392. :"=&a"(bit) //FIXME this is fragile gcc either runs out of registers or misscompiles it (for example if "+a"(bit) or "+m"(*state) is used
  393. :"r"(state), "r"(c)
  394. : "%ecx", "%ebx", "%edx", "%esi"
  395. );
  396. bit&=1;
  397. #else
  398. asm volatile(
  399. "movzbl (%1), %%eax \n\t"
  400. "movl "RANGE "(%2), %%ebx \n\t"
  401. "movl "RANGE "(%2), %%edx \n\t"
  402. "shrl $23, %%ebx \n\t"
  403. "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
  404. "shll $17, %%esi \n\t"
  405. "movl "LOW "(%2), %%ebx \n\t"
  406. //eax:state ebx:low, edx:range, esi:RangeLPS
  407. "subl %%esi, %%edx \n\t"
  408. #ifdef CMOV_IS_FAST //FIXME actually define this somewhere
  409. "cmpl %%ebx, %%edx \n\t"
  410. "cmova %%edx, %%esi \n\t"
  411. "sbbl %%ecx, %%ecx \n\t"
  412. "andl %%ecx, %%edx \n\t"
  413. "subl %%edx, %%ebx \n\t"
  414. "xorl %%ecx, %%eax \n\t"
  415. #else
  416. "movl %%edx, %%ecx \n\t"
  417. "subl %%ebx, %%edx \n\t"
  418. "sarl $31, %%edx \n\t" //lps_mask
  419. "subl %%ecx, %%esi \n\t" //RangeLPS - range
  420. "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
  421. "addl %%ecx, %%esi \n\t" //new range
  422. "andl %%edx, %%ecx \n\t"
  423. "subl %%ecx, %%ebx \n\t"
  424. "xorl %%edx, %%eax \n\t"
  425. #endif
  426. //eax:state ebx:low edx:mask esi:range
  427. "movzbl "MANGLE(ff_h264_mps_state)"(%%eax), %%ecx \n\t"
  428. "movb %%cl, (%1) \n\t"
  429. "movl %%esi, %%edx \n\t"
  430. //eax:bit ebx:low edx:range esi:range
  431. "shr $19, %%esi \n\t"
  432. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  433. "shll %%cl, %%edx \n\t"
  434. "movl %%edx, "RANGE "(%2) \n\t"
  435. "shll %%cl, %%ebx \n\t"
  436. "movl %%ebx, "LOW "(%2) \n\t"
  437. "test %%bx, %%bx \n\t"
  438. " jnz 1f \n\t"
  439. "movl "BYTE "(%2), %%ecx \n\t"
  440. "movzwl (%%ecx), %%esi \n\t"
  441. "bswap %%esi \n\t"
  442. "shrl $15, %%esi \n\t"
  443. "subl $0xFFFF, %%esi \n\t"
  444. "addl $2, %%ecx \n\t"
  445. "movl %%ecx, "BYTE "(%2) \n\t"
  446. "leal -1(%%ebx), %%ecx \n\t"
  447. "xorl %%ebx, %%ecx \n\t"
  448. "shrl $17, %%ecx \n\t"
  449. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  450. "neg %%ecx \n\t"
  451. "add $7, %%ecx \n\t"
  452. "shll %%cl , %%esi \n\t"
  453. "addl %%esi, %%ebx \n\t"
  454. "movl %%ebx, "LOW "(%2) \n\t"
  455. "1: \n\t"
  456. :"=&a"(bit)
  457. :"r"(state), "r"(c)
  458. : "%ecx", "%ebx", "%edx", "%esi"
  459. );
  460. bit&=1;
  461. #endif
  462. #else
  463. int s = *state;
  464. int RangeLPS= ff_h264_lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
  465. int bit, lps_mask attribute_unused;
  466. c->range -= RangeLPS;
  467. #ifndef BRANCHLESS_CABAC_DECODER
  468. if(c->low < c->range){
  469. bit= s&1;
  470. *state= ff_h264_mps_state[s];
  471. renorm_cabac_decoder_once(c);
  472. }else{
  473. bit= ff_h264_norm_shift[RangeLPS>>19];
  474. c->low -= c->range;
  475. *state= ff_h264_lps_state[s];
  476. c->range = RangeLPS<<bit;
  477. c->low <<= bit;
  478. bit= (s&1)^1;
  479. if(!(c->low & 0xFFFF)){
  480. refill2(c);
  481. }
  482. }
  483. #else
  484. lps_mask= (c->range - c->low)>>31;
  485. c->low -= c->range & lps_mask;
  486. c->range += (RangeLPS - c->range) & lps_mask;
  487. s^=lps_mask;
  488. *state= ff_h264_mps_state[s];
  489. bit= s&1;
  490. lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
  491. c->range<<= lps_mask;
  492. c->low <<= lps_mask;
  493. if(!(c->low & CABAC_MASK))
  494. refill2(c);
  495. #endif
  496. #endif
  497. return bit;
  498. }
  499. static int get_cabac_bypass(CABACContext *c){
  500. c->low += c->low;
  501. if(!(c->low & CABAC_MASK))
  502. refill(c);
  503. if(c->low < c->range){
  504. return 0;
  505. }else{
  506. c->low -= c->range;
  507. return 1;
  508. }
  509. }
  510. /**
  511. *
  512. * @return the number of bytes read or 0 if no end
  513. */
  514. static int get_cabac_terminate(CABACContext *c){
  515. c->range -= 4<<CABAC_BITS;
  516. if(c->low < c->range){
  517. renorm_cabac_decoder_once(c);
  518. return 0;
  519. }else{
  520. return c->bytestream - c->bytestream_start;
  521. }
  522. }
  523. /**
  524. * get (truncated) unnary binarization.
  525. */
  526. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  527. int i;
  528. for(i=0; i<max; i++){
  529. if(get_cabac(c, state)==0)
  530. return i;
  531. if(i< max_index) state++;
  532. }
  533. return truncated ? max : -1;
  534. }
  535. /**
  536. * get unary exp golomb k-th order binarization.
  537. */
  538. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  539. int i, v;
  540. int m= 1<<k;
  541. if(get_cabac(c, state)==0)
  542. return 0;
  543. if(0 < max_index) state++;
  544. for(i=1; i<max; i++){
  545. if(get_cabac(c, state)==0){
  546. if(is_signed && get_cabac_bypass(c)){
  547. return -i;
  548. }else
  549. return i;
  550. }
  551. if(i < max_index) state++;
  552. }
  553. while(get_cabac_bypass(c)){
  554. i+= m;
  555. m+= m;
  556. }
  557. v=0;
  558. while(m>>=1){
  559. v+= v + get_cabac_bypass(c);
  560. }
  561. i += v;
  562. if(is_signed && get_cabac_bypass(c)){
  563. return -i;
  564. }else
  565. return i;
  566. }