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.

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