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.

860 lines
28KB

  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. #ifdef ARCH_X86
  29. #include "x86_cpu.h"
  30. #endif
  31. #define CABAC_BITS 16
  32. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  33. #define BRANCHLESS_CABAC_DECODER 1
  34. //#define ARCH_X86_DISABLED 1
  35. typedef struct CABACContext{
  36. int low;
  37. int range;
  38. int outstanding_count;
  39. #ifdef STRICT_LIMITS
  40. int symCount;
  41. #endif
  42. const uint8_t *bytestream_start;
  43. const uint8_t *bytestream;
  44. const uint8_t *bytestream_end;
  45. PutBitContext pb;
  46. }CABACContext;
  47. extern uint8_t ff_h264_mlps_state[4*64];
  48. extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
  49. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  50. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  51. extern const uint8_t ff_h264_norm_shift[512];
  52. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  53. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  54. void ff_init_cabac_states(CABACContext *c);
  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= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  79. if(bit == ((*state)&1)){
  80. c->range -= RangeLPS;
  81. *state= ff_h264_mps_state[*state];
  82. }else{
  83. c->low += c->range - RangeLPS;
  84. c->range = RangeLPS;
  85. *state= ff_h264_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= FFABS(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 < 0x100){
  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 -0x100(%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 $0x100, %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 -0x100(%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 $0x100, %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 $0x100, %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 - 0x100)>>31;
  310. c->range<<= shift;
  311. c->low <<= shift;
  312. #endif
  313. if(!(c->low & CABAC_MASK))
  314. refill(c);
  315. }
  316. static int av_always_inline get_cabac_inline(CABACContext *c, uint8_t * const state){
  317. //FIXME gcc generates duplicate load/stores for c->low and c->range
  318. #define LOW "0"
  319. #define RANGE "4"
  320. #ifdef ARCH_X86_64
  321. #define BYTESTART "16"
  322. #define BYTE "24"
  323. #define BYTEEND "32"
  324. #else
  325. #define BYTESTART "12"
  326. #define BYTE "16"
  327. #define BYTEEND "20"
  328. #endif
  329. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  330. int bit;
  331. #ifndef BRANCHLESS_CABAC_DECODER
  332. asm volatile(
  333. "movzbl (%1), %0 \n\t"
  334. "movl "RANGE "(%2), %%ebx \n\t"
  335. "movl "RANGE "(%2), %%edx \n\t"
  336. "andl $0xC0, %%ebx \n\t"
  337. "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
  338. "movl "LOW "(%2), %%ebx \n\t"
  339. //eax:state ebx:low, edx:range, esi:RangeLPS
  340. "subl %%esi, %%edx \n\t"
  341. "movl %%edx, %%ecx \n\t"
  342. "shll $17, %%ecx \n\t"
  343. "cmpl %%ecx, %%ebx \n\t"
  344. " ja 1f \n\t"
  345. #if 1
  346. //athlon:4067 P3:4110
  347. "lea -0x100(%%edx), %%ecx \n\t"
  348. "shr $31, %%ecx \n\t"
  349. "shl %%cl, %%edx \n\t"
  350. "shl %%cl, %%ebx \n\t"
  351. #else
  352. //athlon:4057 P3:4130
  353. "cmp $0x100, %%edx \n\t" //FIXME avoidable
  354. "setb %%cl \n\t"
  355. "shl %%cl, %%edx \n\t"
  356. "shl %%cl, %%ebx \n\t"
  357. #endif
  358. "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx \n\t"
  359. "movb %%cl, (%1) \n\t"
  360. //eax:state ebx:low, edx:range, esi:RangeLPS
  361. "test %%bx, %%bx \n\t"
  362. " jnz 2f \n\t"
  363. "mov "BYTE "(%2), %%"REG_S" \n\t"
  364. "subl $0xFFFF, %%ebx \n\t"
  365. "movzwl (%%"REG_S"), %%ecx \n\t"
  366. "bswap %%ecx \n\t"
  367. "shrl $15, %%ecx \n\t"
  368. "add $2, %%"REG_S" \n\t"
  369. "addl %%ecx, %%ebx \n\t"
  370. "mov %%"REG_S", "BYTE "(%2) \n\t"
  371. "jmp 2f \n\t"
  372. "1: \n\t"
  373. //eax:state ebx:low, edx:range, esi:RangeLPS
  374. "subl %%ecx, %%ebx \n\t"
  375. "movl %%esi, %%edx \n\t"
  376. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  377. "shll %%cl, %%ebx \n\t"
  378. "shll %%cl, %%edx \n\t"
  379. "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx \n\t"
  380. "movb %%cl, (%1) \n\t"
  381. "add $1, %0 \n\t"
  382. "test %%bx, %%bx \n\t"
  383. " jnz 2f \n\t"
  384. "mov "BYTE "(%2), %%"REG_c" \n\t"
  385. "movzwl (%%"REG_c"), %%esi \n\t"
  386. "bswap %%esi \n\t"
  387. "shrl $15, %%esi \n\t"
  388. "subl $0xFFFF, %%esi \n\t"
  389. "add $2, %%"REG_c" \n\t"
  390. "mov %%"REG_c", "BYTE "(%2) \n\t"
  391. "leal -1(%%ebx), %%ecx \n\t"
  392. "xorl %%ebx, %%ecx \n\t"
  393. "shrl $15, %%ecx \n\t"
  394. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  395. "neg %%ecx \n\t"
  396. "add $7, %%ecx \n\t"
  397. "shll %%cl , %%esi \n\t"
  398. "addl %%esi, %%ebx \n\t"
  399. "2: \n\t"
  400. "movl %%edx, "RANGE "(%2) \n\t"
  401. "movl %%ebx, "LOW "(%2) \n\t"
  402. :"=&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
  403. :"r"(state), "r"(c)
  404. : "%"REG_c, "%ebx", "%edx", "%"REG_S, "memory"
  405. );
  406. bit&=1;
  407. #else /* BRANCHLESS_CABAC_DECODER */
  408. #if defined HAVE_FAST_CMOV
  409. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  410. "mov "tmp" , %%ecx \n\t"\
  411. "shl $17 , "tmp" \n\t"\
  412. "cmp "low" , "tmp" \n\t"\
  413. "cmova %%ecx , "range" \n\t"\
  414. "sbb %%ecx , %%ecx \n\t"\
  415. "and %%ecx , "tmp" \n\t"\
  416. "sub "tmp" , "low" \n\t"\
  417. "xor %%ecx , "ret" \n\t"
  418. #else /* HAVE_FAST_CMOV */
  419. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  420. "mov "tmp" , %%ecx \n\t"\
  421. "shl $17 , "tmp" \n\t"\
  422. "sub "low" , "tmp" \n\t"\
  423. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  424. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  425. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  426. "add %%ecx , "range" \n\t" /*new range*/\
  427. "shl $17 , %%ecx \n\t"\
  428. "and "tmp" , %%ecx \n\t"\
  429. "sub %%ecx , "low" \n\t"\
  430. "xor "tmp" , "ret" \n\t"
  431. #endif /* HAVE_FAST_CMOV */
  432. #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  433. "movzbl "statep" , "ret" \n\t"\
  434. "mov "range" , "tmp" \n\t"\
  435. "and $0xC0 , "range" \n\t"\
  436. "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
  437. "sub "range" , "tmp" \n\t"\
  438. BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  439. "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
  440. "shl %%cl , "range" \n\t"\
  441. "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
  442. "mov "tmpbyte" , "statep" \n\t"\
  443. "shl %%cl , "low" \n\t"\
  444. "test "lowword" , "lowword" \n\t"\
  445. " jnz 1f \n\t"\
  446. "mov "BYTE"("cabac"), %%"REG_c" \n\t"\
  447. "movzwl (%%"REG_c") , "tmp" \n\t"\
  448. "bswap "tmp" \n\t"\
  449. "shr $15 , "tmp" \n\t"\
  450. "sub $0xFFFF , "tmp" \n\t"\
  451. "add $2 , %%"REG_c" \n\t"\
  452. "mov %%"REG_c" , "BYTE "("cabac") \n\t"\
  453. "lea -1("low") , %%ecx \n\t"\
  454. "xor "low" , %%ecx \n\t"\
  455. "shr $15 , %%ecx \n\t"\
  456. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
  457. "neg %%ecx \n\t"\
  458. "add $7 , %%ecx \n\t"\
  459. "shl %%cl , "tmp" \n\t"\
  460. "add "tmp" , "low" \n\t"\
  461. "1: \n\t"
  462. asm volatile(
  463. "movl "RANGE "(%2), %%esi \n\t"
  464. "movl "LOW "(%2), %%ebx \n\t"
  465. BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
  466. "movl %%esi, "RANGE "(%2) \n\t"
  467. "movl %%ebx, "LOW "(%2) \n\t"
  468. :"=&a"(bit)
  469. :"r"(state), "r"(c)
  470. : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
  471. );
  472. bit&=1;
  473. #endif /* BRANCHLESS_CABAC_DECODER */
  474. #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  475. int s = *state;
  476. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  477. int bit, lps_mask attribute_unused;
  478. c->range -= RangeLPS;
  479. #ifndef BRANCHLESS_CABAC_DECODER
  480. if(c->low < (c->range<<(CABAC_BITS+1))){
  481. bit= s&1;
  482. *state= ff_h264_mps_state[s];
  483. renorm_cabac_decoder_once(c);
  484. }else{
  485. bit= ff_h264_norm_shift[RangeLPS];
  486. c->low -= (c->range<<(CABAC_BITS+1));
  487. *state= ff_h264_lps_state[s];
  488. c->range = RangeLPS<<bit;
  489. c->low <<= bit;
  490. bit= (s&1)^1;
  491. if(!(c->low & CABAC_MASK)){
  492. refill2(c);
  493. }
  494. }
  495. #else /* BRANCHLESS_CABAC_DECODER */
  496. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  497. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  498. c->range += (RangeLPS - c->range) & lps_mask;
  499. s^=lps_mask;
  500. *state= (ff_h264_mlps_state+128)[s];
  501. bit= s&1;
  502. lps_mask= ff_h264_norm_shift[c->range];
  503. c->range<<= lps_mask;
  504. c->low <<= lps_mask;
  505. if(!(c->low & CABAC_MASK))
  506. refill2(c);
  507. #endif /* BRANCHLESS_CABAC_DECODER */
  508. #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  509. return bit;
  510. }
  511. static int av_noinline get_cabac_noinline(CABACContext *c, uint8_t * const state){
  512. return get_cabac_inline(c,state);
  513. }
  514. static int get_cabac(CABACContext *c, uint8_t * const state){
  515. return get_cabac_inline(c,state);
  516. }
  517. static int get_cabac_bypass(CABACContext *c){
  518. #if 0 //not faster
  519. int bit;
  520. asm volatile(
  521. "movl "RANGE "(%1), %%ebx \n\t"
  522. "movl "LOW "(%1), %%eax \n\t"
  523. "shl $17, %%ebx \n\t"
  524. "add %%eax, %%eax \n\t"
  525. "sub %%ebx, %%eax \n\t"
  526. "cdq \n\t"
  527. "and %%edx, %%ebx \n\t"
  528. "add %%ebx, %%eax \n\t"
  529. "test %%ax, %%ax \n\t"
  530. " jnz 1f \n\t"
  531. "movl "BYTE "(%1), %%"REG_b" \n\t"
  532. "subl $0xFFFF, %%eax \n\t"
  533. "movzwl (%%"REG_b"), %%ecx \n\t"
  534. "bswap %%ecx \n\t"
  535. "shrl $15, %%ecx \n\t"
  536. "addl $2, %%"REG_b" \n\t"
  537. "addl %%ecx, %%eax \n\t"
  538. "movl %%"REG_b", "BYTE "(%1) \n\t"
  539. "1: \n\t"
  540. "movl %%eax, "LOW "(%1) \n\t"
  541. :"=&d"(bit)
  542. :"r"(c)
  543. : "%eax", "%"REG_b, "%ecx", "memory"
  544. );
  545. return bit+1;
  546. #else
  547. int range;
  548. c->low += c->low;
  549. if(!(c->low & CABAC_MASK))
  550. refill(c);
  551. range= c->range<<(CABAC_BITS+1);
  552. if(c->low < range){
  553. return 0;
  554. }else{
  555. c->low -= range;
  556. return 1;
  557. }
  558. #endif
  559. }
  560. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  561. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  562. asm volatile(
  563. "movl "RANGE "(%1), %%ebx \n\t"
  564. "movl "LOW "(%1), %%eax \n\t"
  565. "shl $17, %%ebx \n\t"
  566. "add %%eax, %%eax \n\t"
  567. "sub %%ebx, %%eax \n\t"
  568. "cdq \n\t"
  569. "and %%edx, %%ebx \n\t"
  570. "add %%ebx, %%eax \n\t"
  571. "xor %%edx, %%ecx \n\t"
  572. "sub %%edx, %%ecx \n\t"
  573. "test %%ax, %%ax \n\t"
  574. " jnz 1f \n\t"
  575. "mov "BYTE "(%1), %%"REG_b" \n\t"
  576. "subl $0xFFFF, %%eax \n\t"
  577. "movzwl (%%"REG_b"), %%edx \n\t"
  578. "bswap %%edx \n\t"
  579. "shrl $15, %%edx \n\t"
  580. "add $2, %%"REG_b" \n\t"
  581. "addl %%edx, %%eax \n\t"
  582. "mov %%"REG_b", "BYTE "(%1) \n\t"
  583. "1: \n\t"
  584. "movl %%eax, "LOW "(%1) \n\t"
  585. :"+c"(val)
  586. :"r"(c)
  587. : "%eax", "%"REG_b, "%edx", "memory"
  588. );
  589. return val;
  590. #else
  591. int range, mask;
  592. c->low += c->low;
  593. if(!(c->low & CABAC_MASK))
  594. refill(c);
  595. range= c->range<<(CABAC_BITS+1);
  596. c->low -= range;
  597. mask= c->low >> 31;
  598. range &= mask;
  599. c->low += range;
  600. return (val^mask)-mask;
  601. #endif
  602. }
  603. //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!)
  604. //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
  605. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  606. static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
  607. void *end= significant_coeff_ctx_base + max_coeff - 1;
  608. int minusstart= -(int)significant_coeff_ctx_base;
  609. int minusindex= 4-(int)index;
  610. int coeff_count;
  611. asm volatile(
  612. "movl "RANGE "(%3), %%esi \n\t"
  613. "movl "LOW "(%3), %%ebx \n\t"
  614. "2: \n\t"
  615. BRANCHLESS_GET_CABAC("%%edx", "%3", "(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  616. "test $1, %%edx \n\t"
  617. " jz 3f \n\t"
  618. BRANCHLESS_GET_CABAC("%%edx", "%3", "61(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  619. "mov %2, %%"REG_a" \n\t"
  620. "movl %4, %%ecx \n\t"
  621. "add %1, %%"REG_c" \n\t"
  622. "movl %%ecx, (%%"REG_a") \n\t"
  623. "test $1, %%edx \n\t"
  624. " jnz 4f \n\t"
  625. "add $4, %%"REG_a" \n\t"
  626. "mov %%"REG_a", %2 \n\t"
  627. "3: \n\t"
  628. "add $1, %1 \n\t"
  629. "cmp %5, %1 \n\t"
  630. " jb 2b \n\t"
  631. "mov %2, %%"REG_a" \n\t"
  632. "movl %4, %%ecx \n\t"
  633. "add %1, %%"REG_c" \n\t"
  634. "movl %%ecx, (%%"REG_a") \n\t"
  635. "4: \n\t"
  636. "add %6, %%eax \n\t"
  637. "shr $2, %%eax \n\t"
  638. "movl %%esi, "RANGE "(%3) \n\t"
  639. "movl %%ebx, "LOW "(%3) \n\t"
  640. :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
  641. :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
  642. : "%"REG_c, "%ebx", "%edx", "%esi", "memory"\
  643. );
  644. return coeff_count;
  645. }
  646. static int decode_significance_8x8_x86(CABACContext *c, uint8_t *significant_coeff_ctx_base, int *index, uint8_t *sig_off){
  647. int minusindex= 4-(int)index;
  648. int coeff_count;
  649. long last=0;
  650. asm volatile(
  651. "movl "RANGE "(%3), %%esi \n\t"
  652. "movl "LOW "(%3), %%ebx \n\t"
  653. "mov %1, %%"REG_D" \n\t"
  654. "2: \n\t"
  655. "mov %6, %%"REG_a" \n\t"
  656. "movzbl (%%"REG_a", %%"REG_D"), %%edi \n\t"
  657. "add %5, %%"REG_D" \n\t"
  658. BRANCHLESS_GET_CABAC("%%edx", "%3", "(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  659. "mov %1, %%edi \n\t"
  660. "test $1, %%edx \n\t"
  661. " jz 3f \n\t"
  662. "movzbl "MANGLE(last_coeff_flag_offset_8x8)"(%%edi), %%edi\n\t"
  663. "add %5, %%"REG_D" \n\t"
  664. BRANCHLESS_GET_CABAC("%%edx", "%3", "15(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  665. "mov %2, %%"REG_a" \n\t"
  666. "mov %1, %%edi \n\t"
  667. "movl %%edi, (%%"REG_a") \n\t"
  668. "test $1, %%edx \n\t"
  669. " jnz 4f \n\t"
  670. "add $4, %%"REG_a" \n\t"
  671. "mov %%"REG_a", %2 \n\t"
  672. "3: \n\t"
  673. "addl $1, %%edi \n\t"
  674. "mov %%edi, %1 \n\t"
  675. "cmpl $63, %%edi \n\t"
  676. " jb 2b \n\t"
  677. "mov %2, %%"REG_a" \n\t"
  678. "movl %%edi, (%%"REG_a") \n\t"
  679. "4: \n\t"
  680. "addl %4, %%eax \n\t"
  681. "shr $2, %%eax \n\t"
  682. "movl %%esi, "RANGE "(%3) \n\t"
  683. "movl %%ebx, "LOW "(%3) \n\t"
  684. :"=&a"(coeff_count),"+m"(last), "+m"(index)\
  685. :"r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base), "m"(sig_off)\
  686. : "%"REG_c, "%ebx", "%edx", "%esi", "%"REG_D, "memory"\
  687. );
  688. return coeff_count;
  689. }
  690. #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  691. /**
  692. *
  693. * @return the number of bytes read or 0 if no end
  694. */
  695. static int get_cabac_terminate(CABACContext *c){
  696. c->range -= 2;
  697. if(c->low < c->range<<(CABAC_BITS+1)){
  698. renorm_cabac_decoder_once(c);
  699. return 0;
  700. }else{
  701. return c->bytestream - c->bytestream_start;
  702. }
  703. }
  704. /**
  705. * get (truncated) unnary binarization.
  706. */
  707. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  708. int i;
  709. for(i=0; i<max; i++){
  710. if(get_cabac(c, state)==0)
  711. return i;
  712. if(i< max_index) state++;
  713. }
  714. return truncated ? max : -1;
  715. }
  716. /**
  717. * get unary exp golomb k-th order binarization.
  718. */
  719. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  720. int i, v;
  721. int m= 1<<k;
  722. if(get_cabac(c, state)==0)
  723. return 0;
  724. if(0 < max_index) state++;
  725. for(i=1; i<max; i++){
  726. if(get_cabac(c, state)==0){
  727. if(is_signed && get_cabac_bypass(c)){
  728. return -i;
  729. }else
  730. return i;
  731. }
  732. if(i < max_index) state++;
  733. }
  734. while(get_cabac_bypass(c)){
  735. i+= m;
  736. m+= m;
  737. }
  738. v=0;
  739. while(m>>=1){
  740. v+= v + get_cabac_bypass(c);
  741. }
  742. i += v;
  743. if(is_signed && get_cabac_bypass(c)){
  744. return -i;
  745. }else
  746. return i;
  747. }