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.

796 lines
26KB

  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. //#define ARCH_X86_DISABLED 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. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  51. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  52. void ff_init_cabac_states(CABACContext *c);
  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= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  77. if(bit == ((*state)&1)){
  78. c->range -= RangeLPS;
  79. *state= ff_h264_mps_state[*state];
  80. }else{
  81. c->low += c->range - RangeLPS;
  82. c->range = RangeLPS;
  83. *state= ff_h264_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= FFABS(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 CABAC_BITS == 16
  218. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  219. #else
  220. c->low+= c->bytestream[0]<<1;
  221. #endif
  222. c->low -= CABAC_MASK;
  223. c->bytestream+= CABAC_BITS/8;
  224. }
  225. static void refill2(CABACContext *c){
  226. int i, x;
  227. x= c->low ^ (c->low-1);
  228. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  229. x= -CABAC_MASK;
  230. #if CABAC_BITS == 16
  231. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  232. #else
  233. x+= c->bytestream[0]<<1;
  234. #endif
  235. c->low += x<<i;
  236. c->bytestream+= CABAC_BITS/8;
  237. }
  238. static inline void renorm_cabac_decoder(CABACContext *c){
  239. while(c->range < 0x100){
  240. c->range+= c->range;
  241. c->low+= c->low;
  242. if(!(c->low & CABAC_MASK))
  243. refill(c);
  244. }
  245. }
  246. static inline void renorm_cabac_decoder_once(CABACContext *c){
  247. #ifdef ARCH_X86_DISABLED
  248. int temp;
  249. #if 0
  250. //P3:683 athlon:475
  251. asm(
  252. "lea -0x100(%0), %2 \n\t"
  253. "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
  254. "shl %%cl, %0 \n\t"
  255. "shl %%cl, %1 \n\t"
  256. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  257. );
  258. #elif 0
  259. //P3:680 athlon:474
  260. asm(
  261. "cmp $0x100, %0 \n\t"
  262. "setb %%cl \n\t" //FIXME 31->63 for x86-64
  263. "shl %%cl, %0 \n\t"
  264. "shl %%cl, %1 \n\t"
  265. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  266. );
  267. #elif 1
  268. int temp2;
  269. //P3:665 athlon:517
  270. asm(
  271. "lea -0x100(%0), %%eax \n\t"
  272. "cdq \n\t"
  273. "mov %0, %%eax \n\t"
  274. "and %%edx, %0 \n\t"
  275. "and %1, %%edx \n\t"
  276. "add %%eax, %0 \n\t"
  277. "add %%edx, %1 \n\t"
  278. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  279. );
  280. #elif 0
  281. int temp2;
  282. //P3:673 athlon:509
  283. asm(
  284. "cmp $0x100, %0 \n\t"
  285. "sbb %%edx, %%edx \n\t"
  286. "mov %0, %%eax \n\t"
  287. "and %%edx, %0 \n\t"
  288. "and %1, %%edx \n\t"
  289. "add %%eax, %0 \n\t"
  290. "add %%edx, %1 \n\t"
  291. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  292. );
  293. #else
  294. int temp2;
  295. //P3:677 athlon:511
  296. asm(
  297. "cmp $0x100, %0 \n\t"
  298. "lea (%0, %0), %%eax \n\t"
  299. "lea (%1, %1), %%edx \n\t"
  300. "cmovb %%eax, %0 \n\t"
  301. "cmovb %%edx, %1 \n\t"
  302. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  303. );
  304. #endif
  305. #else
  306. //P3:675 athlon:476
  307. int shift= (uint32_t)(c->range - 0x100)>>31;
  308. c->range<<= shift;
  309. c->low <<= shift;
  310. #endif
  311. if(!(c->low & CABAC_MASK))
  312. refill(c);
  313. }
  314. static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state){
  315. //FIXME gcc generates duplicate load/stores for c->low and c->range
  316. #define LOW "0"
  317. #define RANGE "4"
  318. #define BYTESTART "12"
  319. #define BYTE "16"
  320. #define BYTEEND "20"
  321. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  322. int bit;
  323. #ifndef BRANCHLESS_CABAC_DECODER
  324. asm volatile(
  325. "movzbl (%1), %0 \n\t"
  326. "movl "RANGE "(%2), %%ebx \n\t"
  327. "movl "RANGE "(%2), %%edx \n\t"
  328. "andl $0xC0, %%ebx \n\t"
  329. "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
  330. "movl "LOW "(%2), %%ebx \n\t"
  331. //eax:state ebx:low, edx:range, esi:RangeLPS
  332. "subl %%esi, %%edx \n\t"
  333. "movl %%edx, %%ecx \n\t"
  334. "shll $17, %%ecx \n\t"
  335. "cmpl %%ecx, %%ebx \n\t"
  336. " ja 1f \n\t"
  337. #if 1
  338. //athlon:4067 P3:4110
  339. "lea -0x100(%%edx), %%ecx \n\t"
  340. "shr $31, %%ecx \n\t"
  341. "shl %%cl, %%edx \n\t"
  342. "shl %%cl, %%ebx \n\t"
  343. #else
  344. //athlon:4057 P3:4130
  345. "cmp $0x100, %%edx \n\t" //FIXME avoidable
  346. "setb %%cl \n\t"
  347. "shl %%cl, %%edx \n\t"
  348. "shl %%cl, %%ebx \n\t"
  349. #endif
  350. "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx \n\t"
  351. "movb %%cl, (%1) \n\t"
  352. //eax:state ebx:low, edx:range, esi:RangeLPS
  353. "test %%bx, %%bx \n\t"
  354. " jnz 2f \n\t"
  355. "movl "BYTE "(%2), %%esi \n\t"
  356. "subl $0xFFFF, %%ebx \n\t"
  357. "movzwl (%%esi), %%ecx \n\t"
  358. "bswap %%ecx \n\t"
  359. "shrl $15, %%ecx \n\t"
  360. "addl $2, %%esi \n\t"
  361. "addl %%ecx, %%ebx \n\t"
  362. "movl %%esi, "BYTE "(%2) \n\t"
  363. "jmp 2f \n\t"
  364. "1: \n\t"
  365. //eax:state ebx:low, edx:range, esi:RangeLPS
  366. "subl %%ecx, %%ebx \n\t"
  367. "movl %%esi, %%edx \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. "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx \n\t"
  372. "movb %%cl, (%1) \n\t"
  373. "addl $1, %0 \n\t"
  374. "test %%bx, %%bx \n\t"
  375. " jnz 2f \n\t"
  376. "movl "BYTE "(%2), %%ecx \n\t"
  377. "movzwl (%%ecx), %%esi \n\t"
  378. "bswap %%esi \n\t"
  379. "shrl $15, %%esi \n\t"
  380. "subl $0xFFFF, %%esi \n\t"
  381. "addl $2, %%ecx \n\t"
  382. "movl %%ecx, "BYTE "(%2) \n\t"
  383. "leal -1(%%ebx), %%ecx \n\t"
  384. "xorl %%ebx, %%ecx \n\t"
  385. "shrl $15, %%ecx \n\t"
  386. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  387. "neg %%ecx \n\t"
  388. "add $7, %%ecx \n\t"
  389. "shll %%cl , %%esi \n\t"
  390. "addl %%esi, %%ebx \n\t"
  391. "2: \n\t"
  392. "movl %%edx, "RANGE "(%2) \n\t"
  393. "movl %%ebx, "LOW "(%2) \n\t"
  394. :"=&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
  395. :"r"(state), "r"(c)
  396. : "%ecx", "%ebx", "%edx", "%esi", "memory"
  397. );
  398. bit&=1;
  399. #else /* BRANCHLESS_CABAC_DECODER */
  400. #if (defined CMOV_IS_FAST && __CPU__ >= 686)
  401. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  402. "mov "tmp" , %%ecx \n\t"\
  403. "shl $17 , "tmp" \n\t"\
  404. "cmp "low" , "tmp" \n\t"\
  405. "cmova %%ecx , "range" \n\t"\
  406. "sbb %%ecx , %%ecx \n\t"\
  407. "and %%ecx , "tmp" \n\t"\
  408. "sub "tmp" , "low" \n\t"\
  409. "xor %%ecx , "ret" \n\t"
  410. #else /* CMOV_IS_FAST */
  411. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  412. "mov "tmp" , %%ecx \n\t"\
  413. "shl $17 , "tmp" \n\t"\
  414. "sub "low" , "tmp" \n\t"\
  415. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  416. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  417. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  418. "add %%ecx , "range" \n\t" /*new range*/\
  419. "shl $17 , %%ecx \n\t"\
  420. "and "tmp" , %%ecx \n\t"\
  421. "sub %%ecx , "low" \n\t"\
  422. "xor "tmp" , "ret" \n\t"
  423. #endif /* CMOV_IS_FAST */
  424. #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  425. "movzbl "statep" , "ret" \n\t"\
  426. "mov "range" , "tmp" \n\t"\
  427. "and $0xC0 , "range" \n\t"\
  428. "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
  429. "sub "range" , "tmp" \n\t"\
  430. BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  431. "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
  432. "shl %%cl , "range" \n\t"\
  433. "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
  434. "mov "tmpbyte" , "statep" \n\t"\
  435. "shl %%cl , "low" \n\t"\
  436. "test "lowword" , "lowword" \n\t"\
  437. " jnz 1f \n\t"\
  438. "mov "BYTE"("cabac"), %%ecx \n\t"\
  439. "movzwl (%%ecx) , "tmp" \n\t"\
  440. "bswap "tmp" \n\t"\
  441. "shr $15 , "tmp" \n\t"\
  442. "sub $0xFFFF , "tmp" \n\t"\
  443. "add $2 , %%ecx \n\t"\
  444. "mov %%ecx , "BYTE "("cabac") \n\t"\
  445. "lea -1("low") , %%ecx \n\t"\
  446. "xor "low" , %%ecx \n\t"\
  447. "shr $15 , %%ecx \n\t"\
  448. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
  449. "neg %%ecx \n\t"\
  450. "add $7 , %%ecx \n\t"\
  451. "shl %%cl , "tmp" \n\t"\
  452. "add "tmp" , "low" \n\t"\
  453. "1: \n\t"
  454. asm volatile(
  455. "movl "RANGE "(%2), %%esi \n\t"
  456. "movl "LOW "(%2), %%ebx \n\t"
  457. BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
  458. "movl %%esi, "RANGE "(%2) \n\t"
  459. "movl %%ebx, "LOW "(%2) \n\t"
  460. :"=&a"(bit)
  461. :"r"(state), "r"(c)
  462. : "%ecx", "%ebx", "%edx", "%esi", "memory"
  463. );
  464. bit&=1;
  465. #endif /* BRANCHLESS_CABAC_DECODER */
  466. #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  467. int s = *state;
  468. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  469. int bit, lps_mask attribute_unused;
  470. c->range -= RangeLPS;
  471. #ifndef BRANCHLESS_CABAC_DECODER
  472. if(c->low < (c->range<<17)){
  473. bit= s&1;
  474. *state= ff_h264_mps_state[s];
  475. renorm_cabac_decoder_once(c);
  476. }else{
  477. bit= ff_h264_norm_shift[RangeLPS];
  478. c->low -= (c->range<<17);
  479. *state= ff_h264_lps_state[s];
  480. c->range = RangeLPS<<bit;
  481. c->low <<= bit;
  482. bit= (s&1)^1;
  483. if(!(c->low & 0xFFFF)){
  484. refill2(c);
  485. }
  486. }
  487. #else /* BRANCHLESS_CABAC_DECODER */
  488. lps_mask= ((c->range<<17) - c->low)>>31;
  489. c->low -= (c->range<<17) & lps_mask;
  490. c->range += (RangeLPS - c->range) & lps_mask;
  491. s^=lps_mask;
  492. *state= (ff_h264_mlps_state+128)[s];
  493. bit= s&1;
  494. lps_mask= ff_h264_norm_shift[c->range];
  495. c->range<<= lps_mask;
  496. c->low <<= lps_mask;
  497. if(!(c->low & CABAC_MASK))
  498. refill2(c);
  499. #endif /* BRANCHLESS_CABAC_DECODER */
  500. #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  501. return bit;
  502. }
  503. static int __attribute((noinline)) get_cabac_noinline(CABACContext *c, uint8_t * const state){
  504. return get_cabac_inline(c,state);
  505. }
  506. static int get_cabac(CABACContext *c, uint8_t * const state){
  507. return get_cabac_inline(c,state);
  508. }
  509. static int get_cabac_bypass(CABACContext *c){
  510. #if 0 //not faster
  511. int bit;
  512. asm volatile(
  513. "movl "RANGE "(%1), %%ebx \n\t"
  514. "movl "LOW "(%1), %%eax \n\t"
  515. "shl $17, %%ebx \n\t"
  516. "add %%eax, %%eax \n\t"
  517. "sub %%ebx, %%eax \n\t"
  518. "cdq \n\t"
  519. "and %%edx, %%ebx \n\t"
  520. "add %%ebx, %%eax \n\t"
  521. "test %%ax, %%ax \n\t"
  522. " jnz 1f \n\t"
  523. "movl "BYTE "(%1), %%ebx \n\t"
  524. "subl $0xFFFF, %%eax \n\t"
  525. "movzwl (%%ebx), %%ecx \n\t"
  526. "bswap %%ecx \n\t"
  527. "shrl $15, %%ecx \n\t"
  528. "addl $2, %%ebx \n\t"
  529. "addl %%ecx, %%eax \n\t"
  530. "movl %%ebx, "BYTE "(%1) \n\t"
  531. "1: \n\t"
  532. "movl %%eax, "LOW "(%1) \n\t"
  533. :"=&d"(bit)
  534. :"r"(c)
  535. : "%eax", "%ebx", "%ecx", "memory"
  536. );
  537. return bit+1;
  538. #else
  539. int range;
  540. c->low += c->low;
  541. if(!(c->low & CABAC_MASK))
  542. refill(c);
  543. range= c->range<<17;
  544. if(c->low < range){
  545. return 0;
  546. }else{
  547. c->low -= range;
  548. return 1;
  549. }
  550. #endif
  551. }
  552. static always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  553. #ifdef ARCH_X86
  554. asm volatile(
  555. "movl "RANGE "(%1), %%ebx \n\t"
  556. "movl "LOW "(%1), %%eax \n\t"
  557. "shl $17, %%ebx \n\t"
  558. "add %%eax, %%eax \n\t"
  559. "sub %%ebx, %%eax \n\t"
  560. "cdq \n\t"
  561. "and %%edx, %%ebx \n\t"
  562. "add %%ebx, %%eax \n\t"
  563. "xor %%edx, %%ecx \n\t"
  564. "sub %%edx, %%ecx \n\t"
  565. "test %%ax, %%ax \n\t"
  566. " jnz 1f \n\t"
  567. "movl "BYTE "(%1), %%ebx \n\t"
  568. "subl $0xFFFF, %%eax \n\t"
  569. "movzwl (%%ebx), %%edx \n\t"
  570. "bswap %%edx \n\t"
  571. "shrl $15, %%edx \n\t"
  572. "addl $2, %%ebx \n\t"
  573. "addl %%edx, %%eax \n\t"
  574. "movl %%ebx, "BYTE "(%1) \n\t"
  575. "1: \n\t"
  576. "movl %%eax, "LOW "(%1) \n\t"
  577. :"+c"(val)
  578. :"r"(c)
  579. : "%eax", "%ebx", "%edx", "memory"
  580. );
  581. return val;
  582. #else
  583. int range, mask;
  584. c->low += c->low;
  585. if(!(c->low & CABAC_MASK))
  586. refill(c);
  587. range= c->range<<17;
  588. c->low -= range;
  589. mask= c->low >> 31;
  590. range &= mask;
  591. c->low += range;
  592. return (val^mask)-mask;
  593. #endif
  594. }
  595. //FIXME the x86 code from this file should be moved into i386/h264 or cabac something.c/h (note ill kill you if you move my code away from under my fingers before iam finished with it!)
  596. //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
  597. #ifdef ARCH_X86
  598. static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
  599. void *end= significant_coeff_ctx_base + max_coeff - 1;
  600. int minusstart= -(int)significant_coeff_ctx_base;
  601. int minusindex= 4-(int)index;
  602. int coeff_count;
  603. asm volatile(
  604. "movl "RANGE "(%3), %%esi \n\t"
  605. "movl "LOW "(%3), %%ebx \n\t"
  606. "2: \n\t"
  607. BRANCHLESS_GET_CABAC("%%edx", "%3", "(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  608. "test $1, %%edx \n\t"
  609. " jz 3f \n\t"
  610. BRANCHLESS_GET_CABAC("%%edx", "%3", "61(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  611. "movl %2, %%eax \n\t"
  612. "movl %4, %%ecx \n\t"
  613. "addl %1, %%ecx \n\t"
  614. "movl %%ecx, (%%eax) \n\t"
  615. "test $1, %%edx \n\t"
  616. " jnz 4f \n\t"
  617. "addl $4, %%eax \n\t"
  618. "movl %%eax, %2 \n\t"
  619. "3: \n\t"
  620. "addl $1, %1 \n\t"
  621. "cmpl %5, %1 \n\t"
  622. " jb 2b \n\t"
  623. "movl %2, %%eax \n\t"
  624. "movl %4, %%ecx \n\t"
  625. "addl %1, %%ecx \n\t"
  626. "movl %%ecx, (%%eax) \n\t"
  627. "4: \n\t"
  628. "addl %6, %%eax \n\t"
  629. "shr $2, %%eax \n\t"
  630. "movl %%esi, "RANGE "(%3) \n\t"
  631. "movl %%ebx, "LOW "(%3) \n\t"
  632. :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
  633. :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
  634. : "%ecx", "%ebx", "%edx", "%esi", "memory"\
  635. );
  636. return coeff_count;
  637. }
  638. #endif
  639. /**
  640. *
  641. * @return the number of bytes read or 0 if no end
  642. */
  643. static int get_cabac_terminate(CABACContext *c){
  644. c->range -= 2;
  645. if(c->low < c->range<<17){
  646. renorm_cabac_decoder_once(c);
  647. return 0;
  648. }else{
  649. return c->bytestream - c->bytestream_start;
  650. }
  651. }
  652. /**
  653. * get (truncated) unnary binarization.
  654. */
  655. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  656. int i;
  657. for(i=0; i<max; i++){
  658. if(get_cabac(c, state)==0)
  659. return i;
  660. if(i< max_index) state++;
  661. }
  662. return truncated ? max : -1;
  663. }
  664. /**
  665. * get unary exp golomb k-th order binarization.
  666. */
  667. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  668. int i, v;
  669. int m= 1<<k;
  670. if(get_cabac(c, state)==0)
  671. return 0;
  672. if(0 < max_index) state++;
  673. for(i=1; i<max; i++){
  674. if(get_cabac(c, state)==0){
  675. if(is_signed && get_cabac_bypass(c)){
  676. return -i;
  677. }else
  678. return i;
  679. }
  680. if(i < max_index) state++;
  681. }
  682. while(get_cabac_bypass(c)){
  683. i+= m;
  684. m+= m;
  685. }
  686. v=0;
  687. while(m>>=1){
  688. v+= v + get_cabac_bypass(c);
  689. }
  690. i += v;
  691. if(is_signed && get_cabac_bypass(c)){
  692. return -i;
  693. }else
  694. return i;
  695. }