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.

639 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. "cmp $0x2000000, %%edx \n\t" //FIXME avoidable
  335. "setb %%cl \n\t"
  336. "shl %%cl, %%edx \n\t"
  337. "shl %%cl, %%ebx \n\t"
  338. "movzbl "MANGLE(ff_h264_mps_state)"(%%eax), %%ecx \n\t"
  339. "movb %%cl, (%1) \n\t"
  340. //eax:state ebx:low, edx:range, esi:RangeLPS
  341. "test %%bx, %%bx \n\t"
  342. " jnz 2f \n\t"
  343. "movl "BYTE "(%2), %%esi \n\t"
  344. "subl $0xFFFF, %%ebx \n\t"
  345. "movzwl (%%esi), %%ecx \n\t"
  346. "bswap %%ecx \n\t"
  347. "shrl $15, %%ecx \n\t"
  348. "addl $2, %%esi \n\t"
  349. "addl %%ecx, %%ebx \n\t"
  350. "movl %%esi, "BYTE "(%2) \n\t"
  351. "jmp 2f \n\t"
  352. "1: \n\t"
  353. //eax:state ebx:low, edx:range, esi:RangeLPS
  354. "subl %%edx, %%ebx \n\t"
  355. "movl %%esi, %%edx \n\t"
  356. "shr $19, %%esi \n\t"
  357. "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx \n\t"
  358. "movb %%cl, (%1) \n\t"
  359. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  360. "shll %%cl, %%ebx \n\t"
  361. "shll %%cl, %%edx \n\t"
  362. "addl $1, %%eax \n\t"
  363. "test %%bx, %%bx \n\t"
  364. " jnz 2f \n\t"
  365. "movl "BYTE "(%2), %%ecx \n\t"
  366. "movzwl (%%ecx), %%esi \n\t"
  367. "bswap %%esi \n\t"
  368. "shrl $15, %%esi \n\t"
  369. "subl $0xFFFF, %%esi \n\t"
  370. "addl $2, %%ecx \n\t"
  371. "movl %%ecx, "BYTE "(%2) \n\t"
  372. "leal -1(%%ebx), %%ecx \n\t"
  373. "xorl %%ebx, %%ecx \n\t"
  374. "shrl $17, %%ecx \n\t"
  375. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  376. "neg %%ecx \n\t"
  377. "add $7, %%ecx \n\t"
  378. "shll %%cl , %%esi \n\t"
  379. "addl %%esi, %%ebx \n\t"
  380. "2: \n\t"
  381. "movl %%edx, "RANGE "(%2) \n\t"
  382. "movl %%ebx, "LOW "(%2) \n\t"
  383. :"=&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
  384. :"r"(state), "r"(c)
  385. : "%ecx", "%ebx", "%edx", "%esi"
  386. );
  387. bit&=1;
  388. #else
  389. asm volatile(
  390. "movzbl (%1), %%eax \n\t"
  391. "movl "RANGE "(%2), %%ebx \n\t"
  392. "movl "RANGE "(%2), %%edx \n\t"
  393. "shrl $23, %%ebx \n\t"
  394. "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
  395. "shll $17, %%esi \n\t"
  396. "movl "LOW "(%2), %%ebx \n\t"
  397. //eax:state ebx:low, edx:range, esi:RangeLPS
  398. "subl %%esi, %%edx \n\t"
  399. #ifdef CMOV_IS_FAST //FIXME actually define this somewhere
  400. "cmpl %%ebx, %%edx \n\t"
  401. "cmova %%edx, %%esi \n\t"
  402. "sbbl %%ecx, %%ecx \n\t"
  403. "andl %%ecx, %%edx \n\t"
  404. "subl %%edx, %%ebx \n\t"
  405. "xorl %%ecx, %%eax \n\t"
  406. #else
  407. "movl %%edx, %%ecx \n\t"
  408. "subl %%ebx, %%edx \n\t"
  409. "sarl $31, %%edx \n\t" //lps_mask
  410. "subl %%ecx, %%esi \n\t" //RangeLPS - range
  411. "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
  412. "addl %%ecx, %%esi \n\t" //new range
  413. "andl %%edx, %%ecx \n\t"
  414. "subl %%ecx, %%ebx \n\t"
  415. "xorl %%edx, %%eax \n\t"
  416. #endif
  417. //eax:state ebx:low edx:mask esi:range
  418. "movzbl "MANGLE(ff_h264_mps_state)"(%%eax), %%ecx \n\t"
  419. "movb %%cl, (%1) \n\t"
  420. "movl %%esi, %%edx \n\t"
  421. //eax:bit ebx:low edx:range esi:range
  422. "shr $19, %%esi \n\t"
  423. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  424. "shll %%cl, %%edx \n\t"
  425. "movl %%edx, "RANGE "(%2) \n\t"
  426. "shll %%cl, %%ebx \n\t"
  427. "movl %%ebx, "LOW "(%2) \n\t"
  428. "test %%bx, %%bx \n\t"
  429. " jnz 1f \n\t"
  430. "movl "BYTE "(%2), %%ecx \n\t"
  431. "movzwl (%%ecx), %%esi \n\t"
  432. "bswap %%esi \n\t"
  433. "shrl $15, %%esi \n\t"
  434. "subl $0xFFFF, %%esi \n\t"
  435. "addl $2, %%ecx \n\t"
  436. "movl %%ecx, "BYTE "(%2) \n\t"
  437. "leal -1(%%ebx), %%ecx \n\t"
  438. "xorl %%ebx, %%ecx \n\t"
  439. "shrl $17, %%ecx \n\t"
  440. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  441. "neg %%ecx \n\t"
  442. "add $7, %%ecx \n\t"
  443. "shll %%cl , %%esi \n\t"
  444. "addl %%esi, %%ebx \n\t"
  445. "movl %%ebx, "LOW "(%2) \n\t"
  446. "1: \n\t"
  447. :"=&a"(bit)
  448. :"r"(state), "r"(c)
  449. : "%ecx", "%ebx", "%edx", "%esi"
  450. );
  451. bit&=1;
  452. #endif
  453. #else
  454. int s = *state;
  455. int RangeLPS= ff_h264_lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
  456. int bit, lps_mask attribute_unused;
  457. c->range -= RangeLPS;
  458. #ifndef BRANCHLESS_CABAC_DECODER
  459. if(c->low < c->range){
  460. bit= s&1;
  461. *state= ff_h264_mps_state[s];
  462. renorm_cabac_decoder_once(c);
  463. }else{
  464. bit= ff_h264_norm_shift[RangeLPS>>19];
  465. c->low -= c->range;
  466. *state= ff_h264_lps_state[s];
  467. c->range = RangeLPS<<bit;
  468. c->low <<= bit;
  469. bit= (s&1)^1;
  470. if(!(c->low & 0xFFFF)){
  471. refill2(c);
  472. }
  473. }
  474. #else
  475. lps_mask= (c->range - c->low)>>31;
  476. c->low -= c->range & lps_mask;
  477. c->range += (RangeLPS - c->range) & lps_mask;
  478. s^=lps_mask;
  479. *state= ff_h264_mps_state[s];
  480. bit= s&1;
  481. lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
  482. c->range<<= lps_mask;
  483. c->low <<= lps_mask;
  484. if(!(c->low & CABAC_MASK))
  485. refill2(c);
  486. #endif
  487. #endif
  488. return bit;
  489. }
  490. static int get_cabac_bypass(CABACContext *c){
  491. c->low += c->low;
  492. if(!(c->low & CABAC_MASK))
  493. refill(c);
  494. if(c->low < c->range){
  495. return 0;
  496. }else{
  497. c->low -= c->range;
  498. return 1;
  499. }
  500. }
  501. /**
  502. *
  503. * @return the number of bytes read or 0 if no end
  504. */
  505. static int get_cabac_terminate(CABACContext *c){
  506. c->range -= 4<<CABAC_BITS;
  507. if(c->low < c->range){
  508. renorm_cabac_decoder_once(c);
  509. return 0;
  510. }else{
  511. return c->bytestream - c->bytestream_start;
  512. }
  513. }
  514. /**
  515. * get (truncated) unnary binarization.
  516. */
  517. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  518. int i;
  519. for(i=0; i<max; i++){
  520. if(get_cabac(c, state)==0)
  521. return i;
  522. if(i< max_index) state++;
  523. }
  524. return truncated ? max : -1;
  525. }
  526. /**
  527. * get unary exp golomb k-th order binarization.
  528. */
  529. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  530. int i, v;
  531. int m= 1<<k;
  532. if(get_cabac(c, state)==0)
  533. return 0;
  534. if(0 < max_index) state++;
  535. for(i=1; i<max; i++){
  536. if(get_cabac(c, state)==0){
  537. if(is_signed && get_cabac_bypass(c)){
  538. return -i;
  539. }else
  540. return i;
  541. }
  542. if(i < max_index) state++;
  543. }
  544. while(get_cabac_bypass(c)){
  545. i+= m;
  546. m+= m;
  547. }
  548. v=0;
  549. while(m>>=1){
  550. v+= v + get_cabac_bypass(c);
  551. }
  552. i += v;
  553. if(is_signed && get_cabac_bypass(c)){
  554. return -i;
  555. }else
  556. return i;
  557. }