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.

871 lines
29KB

  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. * @file cabac.h
  23. * Context Adaptive Binary Arithmetic Coder.
  24. */
  25. #ifndef FFMPEG_CABAC_H
  26. #define FFMPEG_CABAC_H
  27. #include "bitstream.h"
  28. //#undef NDEBUG
  29. #include <assert.h>
  30. #ifdef ARCH_X86
  31. #include "x86_cpu.h"
  32. #endif
  33. #define CABAC_BITS 16
  34. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  35. #define BRANCHLESS_CABAC_DECODER 1
  36. //#define ARCH_X86_DISABLED 1
  37. typedef struct CABACContext{
  38. int low;
  39. int range;
  40. int outstanding_count;
  41. #ifdef STRICT_LIMITS
  42. int symCount;
  43. #endif
  44. const uint8_t *bytestream_start;
  45. const uint8_t *bytestream;
  46. const uint8_t *bytestream_end;
  47. PutBitContext pb;
  48. }CABACContext;
  49. extern uint8_t ff_h264_mlps_state[4*64];
  50. extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
  51. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  52. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  53. extern const uint8_t ff_h264_norm_shift[512];
  54. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  55. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  56. void ff_init_cabac_states(CABACContext *c);
  57. static inline void put_cabac_bit(CABACContext *c, int b){
  58. put_bits(&c->pb, 1, b);
  59. for(;c->outstanding_count; c->outstanding_count--){
  60. put_bits(&c->pb, 1, 1-b);
  61. }
  62. }
  63. static inline void renorm_cabac_encoder(CABACContext *c){
  64. while(c->range < 0x100){
  65. //FIXME optimize
  66. if(c->low<0x100){
  67. put_cabac_bit(c, 0);
  68. }else if(c->low<0x200){
  69. c->outstanding_count++;
  70. c->low -= 0x100;
  71. }else{
  72. put_cabac_bit(c, 1);
  73. c->low -= 0x200;
  74. }
  75. c->range+= c->range;
  76. c->low += c->low;
  77. }
  78. }
  79. #ifdef TEST
  80. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  81. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  82. if(bit == ((*state)&1)){
  83. c->range -= RangeLPS;
  84. *state= ff_h264_mps_state[*state];
  85. }else{
  86. c->low += c->range - RangeLPS;
  87. c->range = RangeLPS;
  88. *state= ff_h264_lps_state[*state];
  89. }
  90. renorm_cabac_encoder(c);
  91. #ifdef STRICT_LIMITS
  92. c->symCount++;
  93. #endif
  94. }
  95. static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  96. assert(c->range > RangeLPS);
  97. if(!bit){
  98. c->range -= RangeLPS;
  99. }else{
  100. c->low += c->range - RangeLPS;
  101. c->range = RangeLPS;
  102. }
  103. renorm_cabac_encoder(c);
  104. #ifdef STRICT_LIMITS
  105. c->symCount++;
  106. #endif
  107. }
  108. /**
  109. * @param bit 0 -> write zero bit, !=0 write one bit
  110. */
  111. static void put_cabac_bypass(CABACContext *c, int bit){
  112. c->low += c->low;
  113. if(bit){
  114. c->low += c->range;
  115. }
  116. //FIXME optimize
  117. if(c->low<0x200){
  118. put_cabac_bit(c, 0);
  119. }else if(c->low<0x400){
  120. c->outstanding_count++;
  121. c->low -= 0x200;
  122. }else{
  123. put_cabac_bit(c, 1);
  124. c->low -= 0x400;
  125. }
  126. #ifdef STRICT_LIMITS
  127. c->symCount++;
  128. #endif
  129. }
  130. /**
  131. *
  132. * @return the number of bytes written
  133. */
  134. static int put_cabac_terminate(CABACContext *c, int bit){
  135. c->range -= 2;
  136. if(!bit){
  137. renorm_cabac_encoder(c);
  138. }else{
  139. c->low += c->range;
  140. c->range= 2;
  141. renorm_cabac_encoder(c);
  142. assert(c->low <= 0x1FF);
  143. put_cabac_bit(c, c->low>>9);
  144. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  145. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  146. }
  147. #ifdef STRICT_LIMITS
  148. c->symCount++;
  149. #endif
  150. return (put_bits_count(&c->pb)+7)>>3;
  151. }
  152. /**
  153. * put (truncated) unary binarization.
  154. */
  155. static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  156. int i;
  157. assert(v <= max);
  158. #if 1
  159. for(i=0; i<v; i++){
  160. put_cabac(c, state, 1);
  161. if(i < max_index) state++;
  162. }
  163. if(truncated==0 || v<max)
  164. put_cabac(c, state, 0);
  165. #else
  166. if(v <= max_index){
  167. for(i=0; i<v; i++){
  168. put_cabac(c, state+i, 1);
  169. }
  170. if(truncated==0 || v<max)
  171. put_cabac(c, state+i, 0);
  172. }else{
  173. for(i=0; i<=max_index; i++){
  174. put_cabac(c, state+i, 1);
  175. }
  176. for(; i<v; i++){
  177. put_cabac(c, state+max_index, 1);
  178. }
  179. if(truncated==0 || v<max)
  180. put_cabac(c, state+max_index, 0);
  181. }
  182. #endif
  183. }
  184. /**
  185. * put unary exp golomb k-th order binarization.
  186. */
  187. static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  188. int i;
  189. if(v==0)
  190. put_cabac(c, state, 0);
  191. else{
  192. const int sign= v < 0;
  193. if(is_signed) v= FFABS(v);
  194. if(v<max){
  195. for(i=0; i<v; i++){
  196. put_cabac(c, state, 1);
  197. if(i < max_index) state++;
  198. }
  199. put_cabac(c, state, 0);
  200. }else{
  201. int m= 1<<k;
  202. for(i=0; i<max; i++){
  203. put_cabac(c, state, 1);
  204. if(i < max_index) state++;
  205. }
  206. v -= max;
  207. while(v >= m){ //FIXME optimize
  208. put_cabac_bypass(c, 1);
  209. v-= m;
  210. m+= m;
  211. }
  212. put_cabac_bypass(c, 0);
  213. while(m>>=1){
  214. put_cabac_bypass(c, v&m);
  215. }
  216. }
  217. if(is_signed)
  218. put_cabac_bypass(c, sign);
  219. }
  220. }
  221. #endif /* TEST */
  222. static void refill(CABACContext *c){
  223. #if CABAC_BITS == 16
  224. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  225. #else
  226. c->low+= c->bytestream[0]<<1;
  227. #endif
  228. c->low -= CABAC_MASK;
  229. c->bytestream+= CABAC_BITS/8;
  230. }
  231. #if ! ( defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) )
  232. static void refill2(CABACContext *c){
  233. int i, x;
  234. x= c->low ^ (c->low-1);
  235. i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
  236. x= -CABAC_MASK;
  237. #if CABAC_BITS == 16
  238. x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  239. #else
  240. x+= c->bytestream[0]<<1;
  241. #endif
  242. c->low += x<<i;
  243. c->bytestream+= CABAC_BITS/8;
  244. }
  245. #endif
  246. static inline void renorm_cabac_decoder(CABACContext *c){
  247. while(c->range < 0x100){
  248. c->range+= c->range;
  249. c->low+= c->low;
  250. if(!(c->low & CABAC_MASK))
  251. refill(c);
  252. }
  253. }
  254. static inline void renorm_cabac_decoder_once(CABACContext *c){
  255. #ifdef ARCH_X86_DISABLED
  256. int temp;
  257. #if 0
  258. //P3:683 athlon:475
  259. asm(
  260. "lea -0x100(%0), %2 \n\t"
  261. "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
  262. "shl %%cl, %0 \n\t"
  263. "shl %%cl, %1 \n\t"
  264. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  265. );
  266. #elif 0
  267. //P3:680 athlon:474
  268. asm(
  269. "cmp $0x100, %0 \n\t"
  270. "setb %%cl \n\t" //FIXME 31->63 for x86-64
  271. "shl %%cl, %0 \n\t"
  272. "shl %%cl, %1 \n\t"
  273. : "+r"(c->range), "+r"(c->low), "+c"(temp)
  274. );
  275. #elif 1
  276. int temp2;
  277. //P3:665 athlon:517
  278. asm(
  279. "lea -0x100(%0), %%eax \n\t"
  280. "cltd \n\t"
  281. "mov %0, %%eax \n\t"
  282. "and %%edx, %0 \n\t"
  283. "and %1, %%edx \n\t"
  284. "add %%eax, %0 \n\t"
  285. "add %%edx, %1 \n\t"
  286. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  287. );
  288. #elif 0
  289. int temp2;
  290. //P3:673 athlon:509
  291. asm(
  292. "cmp $0x100, %0 \n\t"
  293. "sbb %%edx, %%edx \n\t"
  294. "mov %0, %%eax \n\t"
  295. "and %%edx, %0 \n\t"
  296. "and %1, %%edx \n\t"
  297. "add %%eax, %0 \n\t"
  298. "add %%edx, %1 \n\t"
  299. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  300. );
  301. #else
  302. int temp2;
  303. //P3:677 athlon:511
  304. asm(
  305. "cmp $0x100, %0 \n\t"
  306. "lea (%0, %0), %%eax \n\t"
  307. "lea (%1, %1), %%edx \n\t"
  308. "cmovb %%eax, %0 \n\t"
  309. "cmovb %%edx, %1 \n\t"
  310. : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
  311. );
  312. #endif
  313. #else
  314. //P3:675 athlon:476
  315. int shift= (uint32_t)(c->range - 0x100)>>31;
  316. c->range<<= shift;
  317. c->low <<= shift;
  318. #endif
  319. if(!(c->low & CABAC_MASK))
  320. refill(c);
  321. }
  322. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  323. //FIXME gcc generates duplicate load/stores for c->low and c->range
  324. #define LOW "0"
  325. #define RANGE "4"
  326. #ifdef ARCH_X86_64
  327. #define BYTESTART "16"
  328. #define BYTE "24"
  329. #define BYTEEND "32"
  330. #else
  331. #define BYTESTART "12"
  332. #define BYTE "16"
  333. #define BYTEEND "20"
  334. #endif
  335. #if defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
  336. int bit;
  337. #ifndef BRANCHLESS_CABAC_DECODER
  338. asm volatile(
  339. "movzbl (%1), %0 \n\t"
  340. "movl "RANGE "(%2), %%ebx \n\t"
  341. "movl "RANGE "(%2), %%edx \n\t"
  342. "andl $0xC0, %%ebx \n\t"
  343. "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
  344. "movl "LOW "(%2), %%ebx \n\t"
  345. //eax:state ebx:low, edx:range, esi:RangeLPS
  346. "subl %%esi, %%edx \n\t"
  347. "movl %%edx, %%ecx \n\t"
  348. "shll $17, %%ecx \n\t"
  349. "cmpl %%ecx, %%ebx \n\t"
  350. " ja 1f \n\t"
  351. #if 1
  352. //athlon:4067 P3:4110
  353. "lea -0x100(%%edx), %%ecx \n\t"
  354. "shr $31, %%ecx \n\t"
  355. "shl %%cl, %%edx \n\t"
  356. "shl %%cl, %%ebx \n\t"
  357. #else
  358. //athlon:4057 P3:4130
  359. "cmp $0x100, %%edx \n\t" //FIXME avoidable
  360. "setb %%cl \n\t"
  361. "shl %%cl, %%edx \n\t"
  362. "shl %%cl, %%ebx \n\t"
  363. #endif
  364. "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx \n\t"
  365. "movb %%cl, (%1) \n\t"
  366. //eax:state ebx:low, edx:range, esi:RangeLPS
  367. "test %%bx, %%bx \n\t"
  368. " jnz 2f \n\t"
  369. "mov "BYTE "(%2), %%"REG_S" \n\t"
  370. "subl $0xFFFF, %%ebx \n\t"
  371. "movzwl (%%"REG_S"), %%ecx \n\t"
  372. "bswap %%ecx \n\t"
  373. "shrl $15, %%ecx \n\t"
  374. "add $2, %%"REG_S" \n\t"
  375. "addl %%ecx, %%ebx \n\t"
  376. "mov %%"REG_S", "BYTE "(%2) \n\t"
  377. "jmp 2f \n\t"
  378. "1: \n\t"
  379. //eax:state ebx:low, edx:range, esi:RangeLPS
  380. "subl %%ecx, %%ebx \n\t"
  381. "movl %%esi, %%edx \n\t"
  382. "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
  383. "shll %%cl, %%ebx \n\t"
  384. "shll %%cl, %%edx \n\t"
  385. "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx \n\t"
  386. "movb %%cl, (%1) \n\t"
  387. "add $1, %0 \n\t"
  388. "test %%bx, %%bx \n\t"
  389. " jnz 2f \n\t"
  390. "mov "BYTE "(%2), %%"REG_c" \n\t"
  391. "movzwl (%%"REG_c"), %%esi \n\t"
  392. "bswap %%esi \n\t"
  393. "shrl $15, %%esi \n\t"
  394. "subl $0xFFFF, %%esi \n\t"
  395. "add $2, %%"REG_c" \n\t"
  396. "mov %%"REG_c", "BYTE "(%2) \n\t"
  397. "leal -1(%%ebx), %%ecx \n\t"
  398. "xorl %%ebx, %%ecx \n\t"
  399. "shrl $15, %%ecx \n\t"
  400. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
  401. "neg %%ecx \n\t"
  402. "add $7, %%ecx \n\t"
  403. "shll %%cl , %%esi \n\t"
  404. "addl %%esi, %%ebx \n\t"
  405. "2: \n\t"
  406. "movl %%edx, "RANGE "(%2) \n\t"
  407. "movl %%ebx, "LOW "(%2) \n\t"
  408. :"=&a"(bit) //FIXME this is fragile gcc either runs out of registers or miscompiles it (for example if "+a"(bit) or "+m"(*state) is used
  409. :"r"(state), "r"(c)
  410. : "%"REG_c, "%ebx", "%edx", "%"REG_S, "memory"
  411. );
  412. bit&=1;
  413. #else /* BRANCHLESS_CABAC_DECODER */
  414. #if defined HAVE_FAST_CMOV
  415. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  416. "mov "tmp" , %%ecx \n\t"\
  417. "shl $17 , "tmp" \n\t"\
  418. "cmp "low" , "tmp" \n\t"\
  419. "cmova %%ecx , "range" \n\t"\
  420. "sbb %%ecx , %%ecx \n\t"\
  421. "and %%ecx , "tmp" \n\t"\
  422. "sub "tmp" , "low" \n\t"\
  423. "xor %%ecx , "ret" \n\t"
  424. #else /* HAVE_FAST_CMOV */
  425. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  426. "mov "tmp" , %%ecx \n\t"\
  427. "shl $17 , "tmp" \n\t"\
  428. "sub "low" , "tmp" \n\t"\
  429. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  430. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  431. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  432. "add %%ecx , "range" \n\t" /*new range*/\
  433. "shl $17 , %%ecx \n\t"\
  434. "and "tmp" , %%ecx \n\t"\
  435. "sub %%ecx , "low" \n\t"\
  436. "xor "tmp" , "ret" \n\t"
  437. #endif /* HAVE_FAST_CMOV */
  438. #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  439. "movzbl "statep" , "ret" \n\t"\
  440. "mov "range" , "tmp" \n\t"\
  441. "and $0xC0 , "range" \n\t"\
  442. "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
  443. "sub "range" , "tmp" \n\t"\
  444. BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  445. "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
  446. "shl %%cl , "range" \n\t"\
  447. "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
  448. "mov "tmpbyte" , "statep" \n\t"\
  449. "shl %%cl , "low" \n\t"\
  450. "test "lowword" , "lowword" \n\t"\
  451. " jnz 1f \n\t"\
  452. "mov "BYTE"("cabac"), %%"REG_c" \n\t"\
  453. "movzwl (%%"REG_c") , "tmp" \n\t"\
  454. "bswap "tmp" \n\t"\
  455. "shr $15 , "tmp" \n\t"\
  456. "sub $0xFFFF , "tmp" \n\t"\
  457. "add $2 , %%"REG_c" \n\t"\
  458. "mov %%"REG_c" , "BYTE "("cabac") \n\t"\
  459. "lea -1("low") , %%ecx \n\t"\
  460. "xor "low" , %%ecx \n\t"\
  461. "shr $15 , %%ecx \n\t"\
  462. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
  463. "neg %%ecx \n\t"\
  464. "add $7 , %%ecx \n\t"\
  465. "shl %%cl , "tmp" \n\t"\
  466. "add "tmp" , "low" \n\t"\
  467. "1: \n\t"
  468. asm volatile(
  469. "movl "RANGE "(%2), %%esi \n\t"
  470. "movl "LOW "(%2), %%ebx \n\t"
  471. BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
  472. "movl %%esi, "RANGE "(%2) \n\t"
  473. "movl %%ebx, "LOW "(%2) \n\t"
  474. :"=&a"(bit)
  475. :"r"(state), "r"(c)
  476. : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
  477. );
  478. bit&=1;
  479. #endif /* BRANCHLESS_CABAC_DECODER */
  480. #else /* defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
  481. int s = *state;
  482. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  483. int bit, lps_mask av_unused;
  484. c->range -= RangeLPS;
  485. #ifndef BRANCHLESS_CABAC_DECODER
  486. if(c->low < (c->range<<(CABAC_BITS+1))){
  487. bit= s&1;
  488. *state= ff_h264_mps_state[s];
  489. renorm_cabac_decoder_once(c);
  490. }else{
  491. bit= ff_h264_norm_shift[RangeLPS];
  492. c->low -= (c->range<<(CABAC_BITS+1));
  493. *state= ff_h264_lps_state[s];
  494. c->range = RangeLPS<<bit;
  495. c->low <<= bit;
  496. bit= (s&1)^1;
  497. if(!(c->low & CABAC_MASK)){
  498. refill2(c);
  499. }
  500. }
  501. #else /* BRANCHLESS_CABAC_DECODER */
  502. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  503. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  504. c->range += (RangeLPS - c->range) & lps_mask;
  505. s^=lps_mask;
  506. *state= (ff_h264_mlps_state+128)[s];
  507. bit= s&1;
  508. lps_mask= ff_h264_norm_shift[c->range];
  509. c->range<<= lps_mask;
  510. c->low <<= lps_mask;
  511. if(!(c->low & CABAC_MASK))
  512. refill2(c);
  513. #endif /* BRANCHLESS_CABAC_DECODER */
  514. #endif /* defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
  515. return bit;
  516. }
  517. static int av_noinline get_cabac_noinline(CABACContext *c, uint8_t * const state){
  518. return get_cabac_inline(c,state);
  519. }
  520. static int get_cabac(CABACContext *c, uint8_t * const state){
  521. return get_cabac_inline(c,state);
  522. }
  523. static int get_cabac_bypass(CABACContext *c){
  524. #if 0 //not faster
  525. int bit;
  526. asm volatile(
  527. "movl "RANGE "(%1), %%ebx \n\t"
  528. "movl "LOW "(%1), %%eax \n\t"
  529. "shl $17, %%ebx \n\t"
  530. "add %%eax, %%eax \n\t"
  531. "sub %%ebx, %%eax \n\t"
  532. "cltd \n\t"
  533. "and %%edx, %%ebx \n\t"
  534. "add %%ebx, %%eax \n\t"
  535. "test %%ax, %%ax \n\t"
  536. " jnz 1f \n\t"
  537. "movl "BYTE "(%1), %%"REG_b" \n\t"
  538. "subl $0xFFFF, %%eax \n\t"
  539. "movzwl (%%"REG_b"), %%ecx \n\t"
  540. "bswap %%ecx \n\t"
  541. "shrl $15, %%ecx \n\t"
  542. "addl $2, %%"REG_b" \n\t"
  543. "addl %%ecx, %%eax \n\t"
  544. "movl %%"REG_b", "BYTE "(%1) \n\t"
  545. "1: \n\t"
  546. "movl %%eax, "LOW "(%1) \n\t"
  547. :"=&d"(bit)
  548. :"r"(c)
  549. : "%eax", "%"REG_b, "%ecx", "memory"
  550. );
  551. return bit+1;
  552. #else
  553. int range;
  554. c->low += c->low;
  555. if(!(c->low & CABAC_MASK))
  556. refill(c);
  557. range= c->range<<(CABAC_BITS+1);
  558. if(c->low < range){
  559. return 0;
  560. }else{
  561. c->low -= range;
  562. return 1;
  563. }
  564. #endif
  565. }
  566. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  567. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  568. asm volatile(
  569. "movl "RANGE "(%1), %%ebx \n\t"
  570. "movl "LOW "(%1), %%eax \n\t"
  571. "shl $17, %%ebx \n\t"
  572. "add %%eax, %%eax \n\t"
  573. "sub %%ebx, %%eax \n\t"
  574. "cltd \n\t"
  575. "and %%edx, %%ebx \n\t"
  576. "add %%ebx, %%eax \n\t"
  577. "xor %%edx, %%ecx \n\t"
  578. "sub %%edx, %%ecx \n\t"
  579. "test %%ax, %%ax \n\t"
  580. " jnz 1f \n\t"
  581. "mov "BYTE "(%1), %%"REG_b" \n\t"
  582. "subl $0xFFFF, %%eax \n\t"
  583. "movzwl (%%"REG_b"), %%edx \n\t"
  584. "bswap %%edx \n\t"
  585. "shrl $15, %%edx \n\t"
  586. "add $2, %%"REG_b" \n\t"
  587. "addl %%edx, %%eax \n\t"
  588. "mov %%"REG_b", "BYTE "(%1) \n\t"
  589. "1: \n\t"
  590. "movl %%eax, "LOW "(%1) \n\t"
  591. :"+c"(val)
  592. :"r"(c)
  593. : "%eax", "%"REG_b, "%edx", "memory"
  594. );
  595. return val;
  596. #else
  597. int range, mask;
  598. c->low += c->low;
  599. if(!(c->low & CABAC_MASK))
  600. refill(c);
  601. range= c->range<<(CABAC_BITS+1);
  602. c->low -= range;
  603. mask= c->low >> 31;
  604. range &= mask;
  605. c->low += range;
  606. return (val^mask)-mask;
  607. #endif
  608. }
  609. //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!)
  610. //FIXME use some macros to avoid duplicatin get_cabac (cannot be done yet as that would make optimization work hard)
  611. #if defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
  612. static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
  613. void *end= significant_coeff_ctx_base + max_coeff - 1;
  614. int minusstart= -(int)significant_coeff_ctx_base;
  615. int minusindex= 4-(int)index;
  616. int coeff_count;
  617. asm volatile(
  618. "movl "RANGE "(%3), %%esi \n\t"
  619. "movl "LOW "(%3), %%ebx \n\t"
  620. "2: \n\t"
  621. BRANCHLESS_GET_CABAC("%%edx", "%3", "(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  622. "test $1, %%edx \n\t"
  623. " jz 3f \n\t"
  624. BRANCHLESS_GET_CABAC("%%edx", "%3", "61(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  625. "mov %2, %%"REG_a" \n\t"
  626. "movl %4, %%ecx \n\t"
  627. "add %1, %%"REG_c" \n\t"
  628. "movl %%ecx, (%%"REG_a") \n\t"
  629. "test $1, %%edx \n\t"
  630. " jnz 4f \n\t"
  631. "add $4, %%"REG_a" \n\t"
  632. "mov %%"REG_a", %2 \n\t"
  633. "3: \n\t"
  634. "add $1, %1 \n\t"
  635. "cmp %5, %1 \n\t"
  636. " jb 2b \n\t"
  637. "mov %2, %%"REG_a" \n\t"
  638. "movl %4, %%ecx \n\t"
  639. "add %1, %%"REG_c" \n\t"
  640. "movl %%ecx, (%%"REG_a") \n\t"
  641. "4: \n\t"
  642. "add %6, %%eax \n\t"
  643. "shr $2, %%eax \n\t"
  644. "movl %%esi, "RANGE "(%3) \n\t"
  645. "movl %%ebx, "LOW "(%3) \n\t"
  646. :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)
  647. :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)
  648. : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
  649. );
  650. return coeff_count;
  651. }
  652. static int decode_significance_8x8_x86(CABACContext *c, uint8_t *significant_coeff_ctx_base, int *index, const uint8_t *sig_off){
  653. int minusindex= 4-(int)index;
  654. int coeff_count;
  655. long last=0;
  656. asm volatile(
  657. "movl "RANGE "(%3), %%esi \n\t"
  658. "movl "LOW "(%3), %%ebx \n\t"
  659. "mov %1, %%"REG_D" \n\t"
  660. "2: \n\t"
  661. "mov %6, %%"REG_a" \n\t"
  662. "movzbl (%%"REG_a", %%"REG_D"), %%edi \n\t"
  663. "add %5, %%"REG_D" \n\t"
  664. BRANCHLESS_GET_CABAC("%%edx", "%3", "(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  665. "mov %1, %%edi \n\t"
  666. "test $1, %%edx \n\t"
  667. " jz 3f \n\t"
  668. "movzbl "MANGLE(last_coeff_flag_offset_8x8)"(%%edi), %%edi\n\t"
  669. "add %5, %%"REG_D" \n\t"
  670. BRANCHLESS_GET_CABAC("%%edx", "%3", "15(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
  671. "mov %2, %%"REG_a" \n\t"
  672. "mov %1, %%edi \n\t"
  673. "movl %%edi, (%%"REG_a") \n\t"
  674. "test $1, %%edx \n\t"
  675. " jnz 4f \n\t"
  676. "add $4, %%"REG_a" \n\t"
  677. "mov %%"REG_a", %2 \n\t"
  678. "3: \n\t"
  679. "addl $1, %%edi \n\t"
  680. "mov %%edi, %1 \n\t"
  681. "cmpl $63, %%edi \n\t"
  682. " jb 2b \n\t"
  683. "mov %2, %%"REG_a" \n\t"
  684. "movl %%edi, (%%"REG_a") \n\t"
  685. "4: \n\t"
  686. "addl %4, %%eax \n\t"
  687. "shr $2, %%eax \n\t"
  688. "movl %%esi, "RANGE "(%3) \n\t"
  689. "movl %%ebx, "LOW "(%3) \n\t"
  690. :"=&a"(coeff_count),"+m"(last), "+m"(index)
  691. :"r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base), "m"(sig_off)
  692. : "%"REG_c, "%ebx", "%edx", "%esi", "%"REG_D, "memory"
  693. );
  694. return coeff_count;
  695. }
  696. #endif /* defined(ARCH_X86) && && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
  697. /**
  698. *
  699. * @return the number of bytes read or 0 if no end
  700. */
  701. static int get_cabac_terminate(CABACContext *c){
  702. c->range -= 2;
  703. if(c->low < c->range<<(CABAC_BITS+1)){
  704. renorm_cabac_decoder_once(c);
  705. return 0;
  706. }else{
  707. return c->bytestream - c->bytestream_start;
  708. }
  709. }
  710. #if 0
  711. /**
  712. * Get (truncated) unary binarization.
  713. */
  714. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  715. int i;
  716. for(i=0; i<max; i++){
  717. if(get_cabac(c, state)==0)
  718. return i;
  719. if(i< max_index) state++;
  720. }
  721. return truncated ? max : -1;
  722. }
  723. /**
  724. * get unary exp golomb k-th order binarization.
  725. */
  726. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  727. int i, v;
  728. int m= 1<<k;
  729. if(get_cabac(c, state)==0)
  730. return 0;
  731. if(0 < max_index) state++;
  732. for(i=1; i<max; i++){
  733. if(get_cabac(c, state)==0){
  734. if(is_signed && get_cabac_bypass(c)){
  735. return -i;
  736. }else
  737. return i;
  738. }
  739. if(i < max_index) state++;
  740. }
  741. while(get_cabac_bypass(c)){
  742. i+= m;
  743. m+= m;
  744. }
  745. v=0;
  746. while(m>>=1){
  747. v+= v + get_cabac_bypass(c);
  748. }
  749. i += v;
  750. if(is_signed && get_cabac_bypass(c)){
  751. return -i;
  752. }else
  753. return i;
  754. }
  755. #endif /* 0 */
  756. #endif /* FFMPEG_CABAC_H */