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.

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