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.

907 lines
30KB

  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. asm volatile(
  401. "movzbl (%1), %0 \n\t"
  402. "movl "RANGE "(%2), %%ebx \n\t"
  403. "movl "RANGE "(%2), %%edx \n\t"
  404. "andl $0xC0, %%ebx \n\t"
  405. "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
  406. "movl "LOW "(%2), %%ebx \n\t"
  407. //eax:state ebx:low, edx:range, esi:RangeLPS
  408. "subl %%esi, %%edx \n\t"
  409. #if (defined CMOV_IS_FAST && __CPU__ >= 686)
  410. "movl %%edx, %%ecx \n\t"
  411. "shl $17, %%edx \n\t"
  412. "cmpl %%ebx, %%edx \n\t"
  413. "cmova %%ecx, %%esi \n\t"
  414. "sbbl %%ecx, %%ecx \n\t"
  415. "andl %%ecx, %%edx \n\t"
  416. "subl %%edx, %%ebx \n\t"
  417. "xorl %%ecx, %0 \n\t"
  418. #else /* CMOV_IS_FAST */
  419. "movl %%edx, %%ecx \n\t"
  420. "shl $17, %%edx \n\t"
  421. "subl %%ebx, %%edx \n\t"
  422. "sarl $31, %%edx \n\t" //lps_mask
  423. "subl %%ecx, %%esi \n\t" //RangeLPS - range
  424. "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
  425. "addl %%ecx, %%esi \n\t" //new range
  426. "shl $17, %%ecx \n\t"
  427. "andl %%edx, %%ecx \n\t"
  428. "subl %%ecx, %%ebx \n\t"
  429. "xorl %%edx, %0 \n\t"
  430. #endif /* CMOV_IS_FAST */
  431. //eax:state ebx:low edx:mask esi:range
  432. //eax:bit ebx:low esi:range
  433. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  434. "shll %%cl, %%esi \n\t"
  435. "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx \n\t"
  436. "movb %%dl, (%1) \n\t"
  437. "movl %%esi, "RANGE "(%2) \n\t"
  438. "shll %%cl, %%ebx \n\t"
  439. "movl %%ebx, "LOW "(%2) \n\t"
  440. "test %%bx, %%bx \n\t"
  441. " jnz 1f \n\t"
  442. "movl "BYTE "(%2), %%ecx \n\t"
  443. "movzwl (%%ecx), %%esi \n\t"
  444. "bswap %%esi \n\t"
  445. "shrl $15, %%esi \n\t"
  446. "subl $0xFFFF, %%esi \n\t"
  447. "addl $2, %%ecx \n\t"
  448. "movl %%ecx, "BYTE "(%2) \n\t"
  449. "leal -1(%%ebx), %%ecx \n\t"
  450. "xorl %%ebx, %%ecx \n\t"
  451. "shrl $15, %%ecx \n\t"
  452. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  453. "neg %%ecx \n\t"
  454. "add $7, %%ecx \n\t"
  455. "shll %%cl , %%esi \n\t"
  456. "addl %%esi, %%ebx \n\t"
  457. "movl %%ebx, "LOW "(%2) \n\t"
  458. "1: \n\t"
  459. :"=&a"(bit)
  460. :"r"(state), "r"(c)
  461. : "%ecx", "%ebx", "%edx", "%esi", "memory"
  462. );
  463. bit&=1;
  464. #endif /* BRANCHLESS_CABAC_DECODER */
  465. #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  466. int s = *state;
  467. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  468. int bit, lps_mask attribute_unused;
  469. c->range -= RangeLPS;
  470. #ifndef BRANCHLESS_CABAC_DECODER
  471. if(c->low < (c->range<<17)){
  472. bit= s&1;
  473. *state= ff_h264_mps_state[s];
  474. renorm_cabac_decoder_once(c);
  475. }else{
  476. bit= ff_h264_norm_shift[RangeLPS];
  477. c->low -= (c->range<<17);
  478. *state= ff_h264_lps_state[s];
  479. c->range = RangeLPS<<bit;
  480. c->low <<= bit;
  481. bit= (s&1)^1;
  482. if(!(c->low & 0xFFFF)){
  483. refill2(c);
  484. }
  485. }
  486. #else /* BRANCHLESS_CABAC_DECODER */
  487. lps_mask= ((c->range<<17) - c->low)>>31;
  488. c->low -= (c->range<<17) & lps_mask;
  489. c->range += (RangeLPS - c->range) & lps_mask;
  490. s^=lps_mask;
  491. *state= (ff_h264_mlps_state+128)[s];
  492. bit= s&1;
  493. lps_mask= ff_h264_norm_shift[c->range];
  494. c->range<<= lps_mask;
  495. c->low <<= lps_mask;
  496. if(!(c->low & CABAC_MASK))
  497. refill2(c);
  498. #endif /* BRANCHLESS_CABAC_DECODER */
  499. #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
  500. return bit;
  501. }
  502. static int __attribute((noinline)) get_cabac_noinline(CABACContext *c, uint8_t * const state){
  503. return get_cabac_inline(c,state);
  504. }
  505. static int get_cabac(CABACContext *c, uint8_t * const state){
  506. return get_cabac_inline(c,state);
  507. }
  508. static int get_cabac_bypass(CABACContext *c){
  509. #if 0 //not faster
  510. int bit;
  511. asm volatile(
  512. "movl "RANGE "(%1), %%ebx \n\t"
  513. "movl "LOW "(%1), %%eax \n\t"
  514. "shl $17, %%ebx \n\t"
  515. "add %%eax, %%eax \n\t"
  516. "sub %%ebx, %%eax \n\t"
  517. "cdq \n\t"
  518. "and %%edx, %%ebx \n\t"
  519. "add %%ebx, %%eax \n\t"
  520. "test %%ax, %%ax \n\t"
  521. " jnz 1f \n\t"
  522. "movl "BYTE "(%1), %%ebx \n\t"
  523. "subl $0xFFFF, %%eax \n\t"
  524. "movzwl (%%ebx), %%ecx \n\t"
  525. "bswap %%ecx \n\t"
  526. "shrl $15, %%ecx \n\t"
  527. "addl $2, %%ebx \n\t"
  528. "addl %%ecx, %%eax \n\t"
  529. "movl %%ebx, "BYTE "(%1) \n\t"
  530. "1: \n\t"
  531. "movl %%eax, "LOW "(%1) \n\t"
  532. :"=&d"(bit)
  533. :"r"(c)
  534. : "%eax", "%ebx", "%ecx", "memory"
  535. );
  536. return bit+1;
  537. #else
  538. int range;
  539. c->low += c->low;
  540. if(!(c->low & CABAC_MASK))
  541. refill(c);
  542. range= c->range<<17;
  543. if(c->low < range){
  544. return 0;
  545. }else{
  546. c->low -= range;
  547. return 1;
  548. }
  549. #endif
  550. }
  551. static always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  552. #ifdef ARCH_X86
  553. asm volatile(
  554. "movl "RANGE "(%1), %%ebx \n\t"
  555. "movl "LOW "(%1), %%eax \n\t"
  556. "shl $17, %%ebx \n\t"
  557. "add %%eax, %%eax \n\t"
  558. "sub %%ebx, %%eax \n\t"
  559. "cdq \n\t"
  560. "and %%edx, %%ebx \n\t"
  561. "add %%ebx, %%eax \n\t"
  562. "xor %%edx, %%ecx \n\t"
  563. "sub %%edx, %%ecx \n\t"
  564. "test %%ax, %%ax \n\t"
  565. " jnz 1f \n\t"
  566. "movl "BYTE "(%1), %%ebx \n\t"
  567. "subl $0xFFFF, %%eax \n\t"
  568. "movzwl (%%ebx), %%edx \n\t"
  569. "bswap %%edx \n\t"
  570. "shrl $15, %%edx \n\t"
  571. "addl $2, %%ebx \n\t"
  572. "addl %%edx, %%eax \n\t"
  573. "movl %%ebx, "BYTE "(%1) \n\t"
  574. "1: \n\t"
  575. "movl %%eax, "LOW "(%1) \n\t"
  576. :"+c"(val)
  577. :"r"(c)
  578. : "%eax", "%ebx", "%edx", "memory"
  579. );
  580. return val;
  581. #else
  582. int range, mask;
  583. c->low += c->low;
  584. if(!(c->low & CABAC_MASK))
  585. refill(c);
  586. range= c->range<<17;
  587. c->low -= range;
  588. mask= c->low >> 31;
  589. range &= mask;
  590. c->low += range;
  591. return (val^mask)-mask;
  592. #endif
  593. }
  594. //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!)
  595. //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
  596. #ifdef ARCH_X86
  597. static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
  598. void *end= significant_coeff_ctx_base + max_coeff - 1;
  599. int minusstart= -(int)significant_coeff_ctx_base;
  600. int minusindex= -(int)index;
  601. int coeff_count;
  602. asm volatile(
  603. "movl "RANGE "(%3), %%esi \n\t"
  604. "movl "LOW "(%3), %%ebx \n\t"
  605. "2: \n\t"
  606. "movzbl (%1), %0 \n\t"
  607. "movl %%esi, %%edx \n\t"
  608. "andl $0xC0, %%esi \n\t"
  609. "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%esi, 2), %%esi\n\t"
  610. /*eax:state ebx:low, edx:range, esi:RangeLPS*/
  611. "subl %%esi, %%edx \n\t"
  612. #if (defined CMOV_IS_FAST && __CPU__ >= 686)
  613. "movl %%edx, %%ecx \n\t"
  614. "shl $17, %%edx \n\t"
  615. "cmpl %%ebx, %%edx \n\t"
  616. "cmova %%ecx, %%esi \n\t"
  617. "sbbl %%ecx, %%ecx \n\t"
  618. "andl %%ecx, %%edx \n\t"
  619. "subl %%edx, %%ebx \n\t"
  620. "xorl %%ecx, %0 \n\t"
  621. #else /* CMOV_IS_FAST */
  622. "movl %%edx, %%ecx \n\t"
  623. "shl $17, %%edx \n\t"
  624. "subl %%ebx, %%edx \n\t"
  625. "sarl $31, %%edx \n\t" //lps_mask
  626. "subl %%ecx, %%esi \n\t" //RangeLPS - range
  627. "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
  628. "addl %%ecx, %%esi \n\t" //new range
  629. "shl $17, %%ecx \n\t"
  630. "andl %%edx, %%ecx \n\t"
  631. "subl %%ecx, %%ebx \n\t"
  632. "xorl %%edx, %0 \n\t"
  633. #endif /* CMOV_IS_FAST */
  634. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  635. "shll %%cl, %%esi \n\t"
  636. "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx \n\t"
  637. "movb %%dl, (%1) \n\t"
  638. "shll %%cl, %%ebx \n\t"
  639. "test %%bx, %%bx \n\t"
  640. " jnz 1f \n\t"
  641. "movl "BYTE "(%3), %%ecx \n\t"
  642. "movzwl (%%ecx), %%edx \n\t"
  643. "bswap %%edx \n\t"
  644. "shrl $15, %%edx \n\t"
  645. "subl $0xFFFF, %%edx \n\t"
  646. "addl $2, %%ecx \n\t"
  647. "movl %%ecx, "BYTE "(%3) \n\t"
  648. "leal -1(%%ebx), %%ecx \n\t"
  649. "xorl %%ebx, %%ecx \n\t"
  650. "shrl $15, %%ecx \n\t"
  651. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  652. "neg %%ecx \n\t"
  653. "add $7, %%ecx \n\t"
  654. "shll %%cl , %%edx \n\t"
  655. "addl %%edx, %%ebx \n\t"
  656. "1: \n\t"
  657. "test $1, %0 \n\t"
  658. " jz 3f \n\t"
  659. "movl %2, %%eax \n\t"
  660. "movl %4, %%ecx \n\t"
  661. "addl %1, %%ecx \n\t"
  662. "movl %%ecx, (%%eax) \n\t"
  663. "addl $4, %%eax \n\t"
  664. "movl %%eax, %2 \n\t"
  665. "movzbl 61(%1), %0 \n\t"
  666. "movl %%esi, %%edx \n\t"
  667. "andl $0xC0, %%esi \n\t"
  668. "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%esi, 2), %%esi\n\t"
  669. /*eax:state ebx:low, edx:range, esi:RangeLPS*/
  670. "subl %%esi, %%edx \n\t"
  671. #if (defined CMOV_IS_FAST && __CPU__ >= 686)
  672. "movl %%edx, %%ecx \n\t"
  673. "shl $17, %%edx \n\t"
  674. "cmpl %%ebx, %%edx \n\t"
  675. "cmova %%ecx, %%esi \n\t"
  676. "sbbl %%ecx, %%ecx \n\t"
  677. "andl %%ecx, %%edx \n\t"
  678. "subl %%edx, %%ebx \n\t"
  679. "xorl %%ecx, %0 \n\t"
  680. #else /* CMOV_IS_FAST */
  681. "movl %%edx, %%ecx \n\t"
  682. "shl $17, %%edx \n\t"
  683. "subl %%ebx, %%edx \n\t"
  684. "sarl $31, %%edx \n\t" //lps_mask
  685. "subl %%ecx, %%esi \n\t" //RangeLPS - range
  686. "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
  687. "addl %%ecx, %%esi \n\t" //new range
  688. "shl $17, %%ecx \n\t"
  689. "andl %%edx, %%ecx \n\t"
  690. "subl %%ecx, %%ebx \n\t"
  691. "xorl %%edx, %0 \n\t"
  692. #endif /* CMOV_IS_FAST */
  693. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  694. "shll %%cl, %%esi \n\t"
  695. "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx \n\t"
  696. "movb %%dl, 61(%1) \n\t"
  697. "shll %%cl, %%ebx \n\t"
  698. "test %%bx, %%bx \n\t"
  699. " jnz 1f \n\t"
  700. "movl "BYTE "(%3), %%ecx \n\t"
  701. "movzwl (%%ecx), %%edx \n\t"
  702. "bswap %%edx \n\t"
  703. "shrl $15, %%edx \n\t"
  704. "subl $0xFFFF, %%edx \n\t"
  705. "addl $2, %%ecx \n\t"
  706. "movl %%ecx, "BYTE "(%3) \n\t"
  707. "leal -1(%%ebx), %%ecx \n\t"
  708. "xorl %%ebx, %%ecx \n\t"
  709. "shrl $15, %%ecx \n\t"
  710. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  711. "neg %%ecx \n\t"
  712. "add $7, %%ecx \n\t"
  713. "shll %%cl , %%edx \n\t"
  714. "addl %%edx, %%ebx \n\t"
  715. "1: \n\t"
  716. "test $1, %%eax \n\t"
  717. " jnz 4f \n\t"
  718. "3: \n\t"
  719. "addl $1, %1 \n\t"
  720. "cmpl %5, %1 \n\t"
  721. " jb 2b \n\t"
  722. "movl %2, %%eax \n\t"
  723. "movl %4, %%ecx \n\t"
  724. "addl %1, %%ecx \n\t"
  725. "movl %%ecx, (%%eax) \n\t"
  726. "addl $4, %%eax \n\t"
  727. "movl %%eax, %2 \n\t"
  728. "4: \n\t"
  729. "movl %2, %%eax \n\t"
  730. "addl %6, %%eax \n\t"
  731. "shr $2, %%eax \n\t"
  732. "movl %%esi, "RANGE "(%3) \n\t"
  733. "movl %%ebx, "LOW "(%3) \n\t"
  734. :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
  735. :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
  736. : "%ecx", "%ebx", "%edx", "%esi", "memory"\
  737. );
  738. return coeff_count;
  739. }
  740. #endif
  741. /**
  742. *
  743. * @return the number of bytes read or 0 if no end
  744. */
  745. static int get_cabac_terminate(CABACContext *c){
  746. c->range -= 2;
  747. if(c->low < c->range<<17){
  748. renorm_cabac_decoder_once(c);
  749. return 0;
  750. }else{
  751. return c->bytestream - c->bytestream_start;
  752. }
  753. }
  754. /**
  755. * get (truncated) unnary binarization.
  756. */
  757. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  758. int i;
  759. for(i=0; i<max; i++){
  760. if(get_cabac(c, state)==0)
  761. return i;
  762. if(i< max_index) state++;
  763. }
  764. return truncated ? max : -1;
  765. }
  766. /**
  767. * get unary exp golomb k-th order binarization.
  768. */
  769. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  770. int i, v;
  771. int m= 1<<k;
  772. if(get_cabac(c, state)==0)
  773. return 0;
  774. if(0 < max_index) state++;
  775. for(i=1; i<max; i++){
  776. if(get_cabac(c, state)==0){
  777. if(is_signed && get_cabac_bypass(c)){
  778. return -i;
  779. }else
  780. return i;
  781. }
  782. if(i < max_index) state++;
  783. }
  784. while(get_cabac_bypass(c)){
  785. i+= m;
  786. m+= m;
  787. }
  788. v=0;
  789. while(m>>=1){
  790. v+= v + get_cabac_bypass(c);
  791. }
  792. i += v;
  793. if(is_signed && get_cabac_bypass(c)){
  794. return -i;
  795. }else
  796. return i;
  797. }