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.

647 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. uint8_t lps_range[2*65][4]; ///< rangeTabLPS
  40. uint8_t lps_state[2*64]; ///< transIdxLPS
  41. uint8_t mps_state[2*64]; ///< transIdxMPS
  42. const uint8_t *bytestream_start;
  43. const uint8_t *bytestream;
  44. const uint8_t *bytestream_end;
  45. PutBitContext pb;
  46. }CABACContext;
  47. extern const uint8_t ff_h264_lps_range[64][4];
  48. extern const uint8_t ff_h264_mps_state[64];
  49. extern const uint8_t ff_h264_lps_state[64];
  50. extern const uint8_t ff_h264_norm_shift[128];
  51. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  52. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  53. void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
  54. uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
  55. static inline void put_cabac_bit(CABACContext *c, int b){
  56. put_bits(&c->pb, 1, b);
  57. for(;c->outstanding_count; c->outstanding_count--){
  58. put_bits(&c->pb, 1, 1-b);
  59. }
  60. }
  61. static inline void renorm_cabac_encoder(CABACContext *c){
  62. while(c->range < 0x100){
  63. //FIXME optimize
  64. if(c->low<0x100){
  65. put_cabac_bit(c, 0);
  66. }else if(c->low<0x200){
  67. c->outstanding_count++;
  68. c->low -= 0x100;
  69. }else{
  70. put_cabac_bit(c, 1);
  71. c->low -= 0x200;
  72. }
  73. c->range+= c->range;
  74. c->low += c->low;
  75. }
  76. }
  77. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  78. int RangeLPS= c->lps_range[*state][c->range>>6];
  79. if(bit == ((*state)&1)){
  80. c->range -= RangeLPS;
  81. *state= c->mps_state[*state];
  82. }else{
  83. c->low += c->range - RangeLPS;
  84. c->range = RangeLPS;
  85. *state= c->lps_state[*state];
  86. }
  87. renorm_cabac_encoder(c);
  88. #ifdef STRICT_LIMITS
  89. c->symCount++;
  90. #endif
  91. }
  92. static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  93. assert(c->range > RangeLPS);
  94. if(!bit){
  95. c->range -= RangeLPS;
  96. }else{
  97. c->low += c->range - RangeLPS;
  98. c->range = RangeLPS;
  99. }
  100. renorm_cabac_encoder(c);
  101. #ifdef STRICT_LIMITS
  102. c->symCount++;
  103. #endif
  104. }
  105. /**
  106. * @param bit 0 -> write zero bit, !=0 write one bit
  107. */
  108. static void put_cabac_bypass(CABACContext *c, int bit){
  109. c->low += c->low;
  110. if(bit){
  111. c->low += c->range;
  112. }
  113. //FIXME optimize
  114. if(c->low<0x200){
  115. put_cabac_bit(c, 0);
  116. }else if(c->low<0x400){
  117. c->outstanding_count++;
  118. c->low -= 0x200;
  119. }else{
  120. put_cabac_bit(c, 1);
  121. c->low -= 0x400;
  122. }
  123. #ifdef STRICT_LIMITS
  124. c->symCount++;
  125. #endif
  126. }
  127. /**
  128. *
  129. * @return the number of bytes written
  130. */
  131. static int put_cabac_terminate(CABACContext *c, int bit){
  132. c->range -= 2;
  133. if(!bit){
  134. renorm_cabac_encoder(c);
  135. }else{
  136. c->low += c->range;
  137. c->range= 2;
  138. renorm_cabac_encoder(c);
  139. assert(c->low <= 0x1FF);
  140. put_cabac_bit(c, c->low>>9);
  141. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  142. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  143. }
  144. #ifdef STRICT_LIMITS
  145. c->symCount++;
  146. #endif
  147. return (put_bits_count(&c->pb)+7)>>3;
  148. }
  149. /**
  150. * put (truncated) unary binarization.
  151. */
  152. static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  153. int i;
  154. assert(v <= max);
  155. #if 1
  156. for(i=0; i<v; i++){
  157. put_cabac(c, state, 1);
  158. if(i < max_index) state++;
  159. }
  160. if(truncated==0 || v<max)
  161. put_cabac(c, state, 0);
  162. #else
  163. if(v <= max_index){
  164. for(i=0; i<v; i++){
  165. put_cabac(c, state+i, 1);
  166. }
  167. if(truncated==0 || v<max)
  168. put_cabac(c, state+i, 0);
  169. }else{
  170. for(i=0; i<=max_index; i++){
  171. put_cabac(c, state+i, 1);
  172. }
  173. for(; i<v; i++){
  174. put_cabac(c, state+max_index, 1);
  175. }
  176. if(truncated==0 || v<max)
  177. put_cabac(c, state+max_index, 0);
  178. }
  179. #endif
  180. }
  181. /**
  182. * put unary exp golomb k-th order binarization.
  183. */
  184. static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  185. int i;
  186. if(v==0)
  187. put_cabac(c, state, 0);
  188. else{
  189. const int sign= v < 0;
  190. if(is_signed) v= ABS(v);
  191. if(v<max){
  192. for(i=0; i<v; i++){
  193. put_cabac(c, state, 1);
  194. if(i < max_index) state++;
  195. }
  196. put_cabac(c, state, 0);
  197. }else{
  198. int m= 1<<k;
  199. for(i=0; i<max; i++){
  200. put_cabac(c, state, 1);
  201. if(i < max_index) state++;
  202. }
  203. v -= max;
  204. while(v >= m){ //FIXME optimize
  205. put_cabac_bypass(c, 1);
  206. v-= m;
  207. m+= m;
  208. }
  209. put_cabac_bypass(c, 0);
  210. while(m>>=1){
  211. put_cabac_bypass(c, v&m);
  212. }
  213. }
  214. if(is_signed)
  215. put_cabac_bypass(c, sign);
  216. }
  217. }
  218. static void refill(CABACContext *c){
  219. #if CABAC_BITS == 16
  220. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  221. #else
  222. c->low+= c->bytestream[0]<<1;
  223. #endif
  224. c->low -= CABAC_MASK;
  225. c->bytestream+= CABAC_BITS/8;
  226. }
  227. static void refill2(CABACContext *c){
  228. int i, x;
  229. x= c->low ^ (c->low-1);
  230. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
  231. x= -CABAC_MASK;
  232. #if CABAC_BITS == 16
  233. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  234. #else
  235. x+= c->bytestream[0]<<1;
  236. #endif
  237. c->low += x<<i;
  238. c->bytestream+= CABAC_BITS/8;
  239. }
  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. #ifdef ARCH_X86_DISABLED
  250. int temp;
  251. #if 0
  252. //P3:683 athlon:475
  253. asm(
  254. "lea -0x2000000(%0), %2 \n\t"
  255. "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
  256. "shl %%cl, %0 \n\t"
  257. "shl %%cl, %1 \n\t"
  258. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  259. );
  260. #elif 0
  261. //P3:680 athlon:474
  262. asm(
  263. "cmp $0x2000000, %0 \n\t"
  264. "setb %%cl \n\t" //FIXME 31->63 for x86-64
  265. "shl %%cl, %0 \n\t"
  266. "shl %%cl, %1 \n\t"
  267. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  268. );
  269. #elif 1
  270. int temp2;
  271. //P3:665 athlon:517
  272. asm(
  273. "lea -0x2000000(%0), %%eax \n\t"
  274. "cdq \n\t"
  275. "mov %0, %%eax \n\t"
  276. "and %%edx, %0 \n\t"
  277. "and %1, %%edx \n\t"
  278. "add %%eax, %0 \n\t"
  279. "add %%edx, %1 \n\t"
  280. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  281. );
  282. #elif 0
  283. int temp2;
  284. //P3:673 athlon:509
  285. asm(
  286. "cmp $0x2000000, %0 \n\t"
  287. "sbb %%edx, %%edx \n\t"
  288. "mov %0, %%eax \n\t"
  289. "and %%edx, %0 \n\t"
  290. "and %1, %%edx \n\t"
  291. "add %%eax, %0 \n\t"
  292. "add %%edx, %1 \n\t"
  293. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  294. );
  295. #else
  296. int temp2;
  297. //P3:677 athlon:511
  298. asm(
  299. "cmp $0x2000000, %0 \n\t"
  300. "lea (%0, %0), %%eax \n\t"
  301. "lea (%1, %1), %%edx \n\t"
  302. "cmovb %%eax, %0 \n\t"
  303. "cmovb %%edx, %1 \n\t"
  304. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  305. );
  306. #endif
  307. #else
  308. //P3:675 athlon:476
  309. int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
  310. c->range<<= shift;
  311. c->low <<= shift;
  312. #endif
  313. if(!(c->low & CABAC_MASK))
  314. refill(c);
  315. }
  316. static int get_cabac(CABACContext *c, uint8_t * const state){
  317. //FIXME gcc generates duplicate load/stores for c->low and c->range
  318. #ifdef ARCH_X86
  319. int bit;
  320. #define LOW "0"
  321. #define RANGE "4"
  322. #define LPS_RANGE "12"
  323. #define LPS_STATE "12+2*65*4"
  324. #define MPS_STATE "12+2*65*4+2*64"
  325. #define BYTESTART "12+2*65*4+4*64"
  326. #define BYTE "16+2*65*4+4*64"
  327. #define BYTEEND "20+2*65*4+4*64"
  328. #ifndef BRANCHLESS_CABAC_DECODER
  329. asm volatile(
  330. "movzbl (%1), %%eax \n\t"
  331. "movl "RANGE "(%2), %%ebx \n\t"
  332. "movl "RANGE "(%2), %%edx \n\t"
  333. "shrl $23, %%ebx \n\t"
  334. "leal "LPS_RANGE"(%2, %%eax, 4), %%esi \n\t"
  335. "movzbl (%%ebx, %%esi), %%esi \n\t"
  336. "shll $17, %%esi \n\t"
  337. "movl "LOW "(%2), %%ebx \n\t"
  338. //eax:state ebx:low, edx:range, esi:RangeLPS
  339. "subl %%esi, %%edx \n\t"
  340. "cmpl %%edx, %%ebx \n\t"
  341. " ja 1f \n\t"
  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. "movzbl "MPS_STATE"(%2, %%eax), %%ecx \n\t"
  347. "movb %%cl, (%1) \n\t"
  348. //eax:state ebx:low, edx:range, esi:RangeLPS
  349. "test %%bx, %%bx \n\t"
  350. " jnz 2f \n\t"
  351. "movl "BYTE "(%2), %%esi \n\t"
  352. "subl $0xFFFF, %%ebx \n\t"
  353. "movzwl (%%esi), %%ecx \n\t"
  354. "bswap %%ecx \n\t"
  355. "shrl $15, %%ecx \n\t"
  356. "addl $2, %%esi \n\t"
  357. "addl %%ecx, %%ebx \n\t"
  358. "movl %%esi, "BYTE "(%2) \n\t"
  359. "jmp 2f \n\t"
  360. "1: \n\t"
  361. //eax:state ebx:low, edx:range, esi:RangeLPS
  362. "subl %%edx, %%ebx \n\t"
  363. "movl %%esi, %%edx \n\t"
  364. "shr $19, %%esi \n\t"
  365. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  366. "shll %%cl, %%ebx \n\t"
  367. "shll %%cl, %%edx \n\t"
  368. "movzbl "LPS_STATE"(%2, %%eax), %%ecx \n\t"
  369. "movb %%cl, (%1) \n\t"
  370. "addl $1, %%eax \n\t"
  371. "test %%bx, %%bx \n\t"
  372. " jnz 2f \n\t"
  373. "movl "BYTE "(%2), %%ecx \n\t"
  374. "movzwl (%%ecx), %%esi \n\t"
  375. "bswap %%esi \n\t"
  376. "shrl $15, %%esi \n\t"
  377. "subl $0xFFFF, %%esi \n\t"
  378. "addl $2, %%ecx \n\t"
  379. "movl %%ecx, "BYTE "(%2) \n\t"
  380. "leal -1(%%ebx), %%ecx \n\t"
  381. "xorl %%ebx, %%ecx \n\t"
  382. "shrl $17, %%ecx \n\t"
  383. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  384. "neg %%cl \n\t"
  385. "add $7, %%cl \n\t"
  386. "shll %%cl , %%esi \n\t"
  387. "addl %%esi, %%ebx \n\t"
  388. "2: \n\t"
  389. "movl %%edx, "RANGE "(%2) \n\t"
  390. "movl %%ebx, "LOW "(%2) \n\t"
  391. :"=&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
  392. :"r"(state), "r"(c)
  393. : "%ecx", "%ebx", "%edx", "%esi"
  394. );
  395. bit&=1;
  396. #else
  397. asm volatile(
  398. "movzbl (%1), %%eax \n\t"
  399. "movl "RANGE "(%2), %%ebx \n\t"
  400. "movl "RANGE "(%2), %%edx \n\t"
  401. "shrl $23, %%ebx \n\t"
  402. "leal "LPS_RANGE"(%2, %%eax, 4), %%esi \n\t"
  403. "movzbl (%%ebx, %%esi), %%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 "MPS_STATE"(%2, %%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, %%ebx \n\t"
  434. "shll %%cl, %%edx \n\t"
  435. "test %%bx, %%bx \n\t"
  436. " jnz 1f \n\t"
  437. "movl "BYTE "(%2), %%ecx \n\t"
  438. "movzwl (%%ecx), %%esi \n\t"
  439. "bswap %%esi \n\t"
  440. "shrl $15, %%esi \n\t"
  441. "subl $0xFFFF, %%esi \n\t"
  442. "addl $2, %%ecx \n\t"
  443. "movl %%ecx, "BYTE "(%2) \n\t"
  444. "leal -1(%%ebx), %%ecx \n\t"
  445. "xorl %%ebx, %%ecx \n\t"
  446. "shrl $17, %%ecx \n\t"
  447. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  448. "neg %%cl \n\t"
  449. "add $7, %%cl \n\t"
  450. "shll %%cl , %%esi \n\t"
  451. "addl %%esi, %%ebx \n\t"
  452. "1: \n\t"
  453. "movl %%edx, "RANGE "(%2) \n\t"
  454. "movl %%ebx, "LOW "(%2) \n\t"
  455. :"=&a"(bit)
  456. :"r"(state), "r"(c)
  457. : "%ecx", "%ebx", "%edx", "%esi"
  458. );
  459. bit&=1;
  460. #endif
  461. #else
  462. int s = *state;
  463. int RangeLPS= c->lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
  464. int bit, lps_mask attribute_unused;
  465. c->range -= RangeLPS;
  466. #ifndef BRANCHLESS_CABAC_DECODER
  467. if(c->low < c->range){
  468. bit= s&1;
  469. *state= c->mps_state[s];
  470. renorm_cabac_decoder_once(c);
  471. }else{
  472. bit= ff_h264_norm_shift[RangeLPS>>19];
  473. c->low -= c->range;
  474. *state= c->lps_state[s];
  475. c->range = RangeLPS<<bit;
  476. c->low <<= bit;
  477. bit= (s&1)^1;
  478. if(!(c->low & 0xFFFF)){
  479. refill2(c);
  480. }
  481. }
  482. #else
  483. lps_mask= (c->range - c->low)>>31;
  484. c->low -= c->range & lps_mask;
  485. c->range += (RangeLPS - c->range) & lps_mask;
  486. s^=lps_mask;
  487. *state= c->mps_state[s];
  488. bit= s&1;
  489. lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
  490. c->range<<= lps_mask;
  491. c->low <<= lps_mask;
  492. if(!(c->low & CABAC_MASK))
  493. refill2(c);
  494. #endif
  495. #endif
  496. return bit;
  497. }
  498. static int get_cabac_bypass(CABACContext *c){
  499. c->low += c->low;
  500. if(!(c->low & CABAC_MASK))
  501. refill(c);
  502. if(c->low < c->range){
  503. return 0;
  504. }else{
  505. c->low -= c->range;
  506. return 1;
  507. }
  508. }
  509. /**
  510. *
  511. * @return the number of bytes read or 0 if no end
  512. */
  513. static int get_cabac_terminate(CABACContext *c){
  514. c->range -= 4<<CABAC_BITS;
  515. if(c->low < c->range){
  516. renorm_cabac_decoder_once(c);
  517. return 0;
  518. }else{
  519. return c->bytestream - c->bytestream_start;
  520. }
  521. }
  522. /**
  523. * get (truncated) unnary binarization.
  524. */
  525. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  526. int i;
  527. for(i=0; i<max; i++){
  528. if(get_cabac(c, state)==0)
  529. return i;
  530. if(i< max_index) state++;
  531. }
  532. return truncated ? max : -1;
  533. }
  534. /**
  535. * get unary exp golomb k-th order binarization.
  536. */
  537. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  538. int i, v;
  539. int m= 1<<k;
  540. if(get_cabac(c, state)==0)
  541. return 0;
  542. if(0 < max_index) state++;
  543. for(i=1; i<max; i++){
  544. if(get_cabac(c, state)==0){
  545. if(is_signed && get_cabac_bypass(c)){
  546. return -i;
  547. }else
  548. return i;
  549. }
  550. if(i < max_index) state++;
  551. }
  552. while(get_cabac_bypass(c)){
  553. i+= m;
  554. m+= m;
  555. }
  556. v=0;
  557. while(m>>=1){
  558. v+= v + get_cabac_bypass(c);
  559. }
  560. i += v;
  561. if(is_signed && get_cabac_bypass(c)){
  562. return -i;
  563. }else
  564. return i;
  565. }