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.

558 lines
16KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Context Adaptive Binary Arithmetic Coder.
  24. */
  25. #ifndef AVCODEC_CABAC_H
  26. #define AVCODEC_CABAC_H
  27. #include <stddef.h>
  28. #include "put_bits.h"
  29. //#undef NDEBUG
  30. #include <assert.h>
  31. #include "libavutil/x86_cpu.h"
  32. #define CABAC_BITS 16
  33. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  34. typedef struct CABACContext{
  35. int low;
  36. int range;
  37. int outstanding_count;
  38. #ifdef STRICT_LIMITS
  39. int symCount;
  40. #endif
  41. const uint8_t *bytestream_start;
  42. const uint8_t *bytestream;
  43. const uint8_t *bytestream_end;
  44. PutBitContext pb;
  45. }CABACContext;
  46. extern uint8_t ff_h264_mlps_state[4*64];
  47. extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
  48. extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
  49. extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
  50. extern const uint8_t ff_h264_norm_shift[512];
  51. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  52. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  53. void ff_init_cabac_states(CABACContext *c);
  54. static inline void put_cabac_bit(CABACContext *c, int b){
  55. put_bits(&c->pb, 1, b);
  56. for(;c->outstanding_count; c->outstanding_count--){
  57. put_bits(&c->pb, 1, 1-b);
  58. }
  59. }
  60. static inline void renorm_cabac_encoder(CABACContext *c){
  61. while(c->range < 0x100){
  62. //FIXME optimize
  63. if(c->low<0x100){
  64. put_cabac_bit(c, 0);
  65. }else if(c->low<0x200){
  66. c->outstanding_count++;
  67. c->low -= 0x100;
  68. }else{
  69. put_cabac_bit(c, 1);
  70. c->low -= 0x200;
  71. }
  72. c->range+= c->range;
  73. c->low += c->low;
  74. }
  75. }
  76. #ifdef TEST
  77. static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  78. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
  79. if(bit == ((*state)&1)){
  80. c->range -= RangeLPS;
  81. *state= ff_h264_mps_state[*state];
  82. }else{
  83. c->low += c->range - RangeLPS;
  84. c->range = RangeLPS;
  85. *state= ff_h264_lps_state[*state];
  86. }
  87. renorm_cabac_encoder(c);
  88. #ifdef STRICT_LIMITS
  89. c->symCount++;
  90. #endif
  91. }
  92. static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
  93. assert(c->range > RangeLPS);
  94. if(!bit){
  95. c->range -= RangeLPS;
  96. }else{
  97. c->low += c->range - RangeLPS;
  98. c->range = RangeLPS;
  99. }
  100. renorm_cabac_encoder(c);
  101. #ifdef STRICT_LIMITS
  102. c->symCount++;
  103. #endif
  104. }
  105. /**
  106. * @param bit 0 -> write zero bit, !=0 write one bit
  107. */
  108. static void put_cabac_bypass(CABACContext *c, int bit){
  109. c->low += c->low;
  110. if(bit){
  111. c->low += c->range;
  112. }
  113. //FIXME optimize
  114. if(c->low<0x200){
  115. put_cabac_bit(c, 0);
  116. }else if(c->low<0x400){
  117. c->outstanding_count++;
  118. c->low -= 0x200;
  119. }else{
  120. put_cabac_bit(c, 1);
  121. c->low -= 0x400;
  122. }
  123. #ifdef STRICT_LIMITS
  124. c->symCount++;
  125. #endif
  126. }
  127. /**
  128. *
  129. * @return the number of bytes written
  130. */
  131. static int put_cabac_terminate(CABACContext *c, int bit){
  132. c->range -= 2;
  133. if(!bit){
  134. renorm_cabac_encoder(c);
  135. }else{
  136. c->low += c->range;
  137. c->range= 2;
  138. renorm_cabac_encoder(c);
  139. assert(c->low <= 0x1FF);
  140. put_cabac_bit(c, c->low>>9);
  141. put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
  142. flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  143. }
  144. #ifdef STRICT_LIMITS
  145. c->symCount++;
  146. #endif
  147. return (put_bits_count(&c->pb)+7)>>3;
  148. }
  149. /**
  150. * put (truncated) unary binarization.
  151. */
  152. static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  153. int i;
  154. assert(v <= max);
  155. #if 1
  156. for(i=0; i<v; i++){
  157. put_cabac(c, state, 1);
  158. if(i < max_index) state++;
  159. }
  160. if(truncated==0 || v<max)
  161. put_cabac(c, state, 0);
  162. #else
  163. if(v <= max_index){
  164. for(i=0; i<v; i++){
  165. put_cabac(c, state+i, 1);
  166. }
  167. if(truncated==0 || v<max)
  168. put_cabac(c, state+i, 0);
  169. }else{
  170. for(i=0; i<=max_index; i++){
  171. put_cabac(c, state+i, 1);
  172. }
  173. for(; i<v; i++){
  174. put_cabac(c, state+max_index, 1);
  175. }
  176. if(truncated==0 || v<max)
  177. put_cabac(c, state+max_index, 0);
  178. }
  179. #endif
  180. }
  181. /**
  182. * put unary exp golomb k-th order binarization.
  183. */
  184. static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  185. int i;
  186. if(v==0)
  187. put_cabac(c, state, 0);
  188. else{
  189. const int sign= v < 0;
  190. if(is_signed) v= FFABS(v);
  191. if(v<max){
  192. for(i=0; i<v; i++){
  193. put_cabac(c, state, 1);
  194. if(i < max_index) state++;
  195. }
  196. put_cabac(c, state, 0);
  197. }else{
  198. int m= 1<<k;
  199. for(i=0; i<max; i++){
  200. put_cabac(c, state, 1);
  201. if(i < max_index) state++;
  202. }
  203. v -= max;
  204. while(v >= m){ //FIXME optimize
  205. put_cabac_bypass(c, 1);
  206. v-= m;
  207. m+= m;
  208. }
  209. put_cabac_bypass(c, 0);
  210. while(m>>=1){
  211. put_cabac_bypass(c, v&m);
  212. }
  213. }
  214. if(is_signed)
  215. put_cabac_bypass(c, sign);
  216. }
  217. }
  218. #endif /* TEST */
  219. static void refill(CABACContext *c){
  220. #if CABAC_BITS == 16
  221. c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  222. #else
  223. c->low+= c->bytestream[0]<<1;
  224. #endif
  225. c->low -= CABAC_MASK;
  226. c->bytestream+= CABAC_BITS/8;
  227. }
  228. #if ! ( ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS) )
  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. #endif
  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. int shift= (uint32_t)(c->range - 0x100)>>31;
  253. c->range<<= shift;
  254. c->low <<= shift;
  255. if(!(c->low & CABAC_MASK))
  256. refill(c);
  257. }
  258. static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
  259. //FIXME gcc generates duplicate load/stores for c->low and c->range
  260. #if ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS)
  261. int bit, low, range, tmp;
  262. #if HAVE_FAST_CMOV
  263. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp)\
  264. "mov "tmp" , %%ecx \n\t"\
  265. "shl $17 , "tmp" \n\t"\
  266. "cmp "low" , "tmp" \n\t"\
  267. "cmova %%ecx , "range" \n\t"\
  268. "sbb %%ecx , %%ecx \n\t"\
  269. "and %%ecx , "tmp" \n\t"\
  270. "sub "tmp" , "low" \n\t"\
  271. "xor %%ecx , "ret" \n\t"
  272. #else /* HAVE_FAST_CMOV */
  273. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp)\
  274. "mov "tmp" , %%ecx \n\t"\
  275. "shl $17 , "tmp" \n\t"\
  276. "sub "low" , "tmp" \n\t"\
  277. "sar $31 , "tmp" \n\t" /*lps_mask*/\
  278. "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
  279. "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
  280. "add %%ecx , "range" \n\t" /*new range*/\
  281. "shl $17 , %%ecx \n\t"\
  282. "and "tmp" , %%ecx \n\t"\
  283. "sub %%ecx , "low" \n\t"\
  284. "xor "tmp" , "ret" \n\t"
  285. #endif /* HAVE_FAST_CMOV */
  286. #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte, byte) \
  287. "movzbl "statep" , "ret" \n\t"\
  288. "mov "range" , "tmp" \n\t"\
  289. "and $0xC0 , "range" \n\t"\
  290. "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
  291. "sub "range" , "tmp" \n\t"\
  292. BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp)\
  293. "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
  294. "shl %%cl , "range" \n\t"\
  295. "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
  296. "mov "tmpbyte" , "statep" \n\t"\
  297. "shl %%cl , "low" \n\t"\
  298. "test "lowword" , "lowword" \n\t"\
  299. " jnz 1f \n\t"\
  300. "mov "byte"("cabac"), %%"REG_c" \n\t"\
  301. "movzwl (%%"REG_c") , "tmp" \n\t"\
  302. "bswap "tmp" \n\t"\
  303. "shr $15 , "tmp" \n\t"\
  304. "sub $0xFFFF , "tmp" \n\t"\
  305. "add $2 , %%"REG_c" \n\t"\
  306. "mov %%"REG_c" , "byte "("cabac") \n\t"\
  307. "lea -1("low") , %%ecx \n\t"\
  308. "xor "low" , %%ecx \n\t"\
  309. "shr $15 , %%ecx \n\t"\
  310. "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
  311. "neg %%ecx \n\t"\
  312. "add $7 , %%ecx \n\t"\
  313. "shl %%cl , "tmp" \n\t"\
  314. "add "tmp" , "low" \n\t"\
  315. "1: \n\t"
  316. __asm__ volatile(
  317. "movl %a6(%5), %2 \n\t"
  318. "movl %a7(%5), %1 \n\t"
  319. BRANCHLESS_GET_CABAC("%0", "%5", "(%4)", "%1", "%w1", "%2", "%3", "%b3", "%a8")
  320. "movl %2, %a6(%5) \n\t"
  321. "movl %1, %a7(%5) \n\t"
  322. :"=&r"(bit), "=&r"(low), "=&r"(range), "=&r"(tmp)
  323. :"r"(state), "r"(c),
  324. "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
  325. "i"(offsetof(CABACContext, bytestream))
  326. : "%"REG_c, "memory"
  327. );
  328. bit&=1;
  329. #else /* ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
  330. int s = *state;
  331. int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
  332. int bit, lps_mask;
  333. c->range -= RangeLPS;
  334. lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
  335. c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
  336. c->range += (RangeLPS - c->range) & lps_mask;
  337. s^=lps_mask;
  338. *state= (ff_h264_mlps_state+128)[s];
  339. bit= s&1;
  340. lps_mask= ff_h264_norm_shift[c->range];
  341. c->range<<= lps_mask;
  342. c->low <<= lps_mask;
  343. if(!(c->low & CABAC_MASK))
  344. refill2(c);
  345. #endif /* ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
  346. return bit;
  347. }
  348. static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
  349. return get_cabac_inline(c,state);
  350. }
  351. static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
  352. return get_cabac_inline(c,state);
  353. }
  354. static int av_unused get_cabac_bypass(CABACContext *c){
  355. int range;
  356. c->low += c->low;
  357. if(!(c->low & CABAC_MASK))
  358. refill(c);
  359. range= c->range<<(CABAC_BITS+1);
  360. if(c->low < range){
  361. return 0;
  362. }else{
  363. c->low -= range;
  364. return 1;
  365. }
  366. }
  367. static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
  368. #if ARCH_X86
  369. x86_reg tmp;
  370. __asm__ volatile(
  371. "movl %a3(%2), %k1 \n\t"
  372. "movl %a4(%2), %%eax \n\t"
  373. "shl $17, %k1 \n\t"
  374. "add %%eax, %%eax \n\t"
  375. "sub %k1, %%eax \n\t"
  376. "cltd \n\t"
  377. "and %%edx, %k1 \n\t"
  378. "add %k1, %%eax \n\t"
  379. "xor %%edx, %%ecx \n\t"
  380. "sub %%edx, %%ecx \n\t"
  381. "test %%ax, %%ax \n\t"
  382. " jnz 1f \n\t"
  383. "mov %a5(%2), %1 \n\t"
  384. "subl $0xFFFF, %%eax \n\t"
  385. "movzwl (%1), %%edx \n\t"
  386. "bswap %%edx \n\t"
  387. "shrl $15, %%edx \n\t"
  388. "add $2, %1 \n\t"
  389. "addl %%edx, %%eax \n\t"
  390. "mov %1, %a5(%2) \n\t"
  391. "1: \n\t"
  392. "movl %%eax, %a4(%2) \n\t"
  393. :"+c"(val), "=&r"(tmp)
  394. :"r"(c),
  395. "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
  396. "i"(offsetof(CABACContext, bytestream))
  397. : "%eax", "%edx", "memory"
  398. );
  399. return val;
  400. #else
  401. int range, mask;
  402. c->low += c->low;
  403. if(!(c->low & CABAC_MASK))
  404. refill(c);
  405. range= c->range<<(CABAC_BITS+1);
  406. c->low -= range;
  407. mask= c->low >> 31;
  408. range &= mask;
  409. c->low += range;
  410. return (val^mask)-mask;
  411. #endif
  412. }
  413. /**
  414. *
  415. * @return the number of bytes read or 0 if no end
  416. */
  417. static int av_unused get_cabac_terminate(CABACContext *c){
  418. c->range -= 2;
  419. if(c->low < c->range<<(CABAC_BITS+1)){
  420. renorm_cabac_decoder_once(c);
  421. return 0;
  422. }else{
  423. return c->bytestream - c->bytestream_start;
  424. }
  425. }
  426. #if 0
  427. /**
  428. * Get (truncated) unary binarization.
  429. */
  430. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  431. int i;
  432. for(i=0; i<max; i++){
  433. if(get_cabac(c, state)==0)
  434. return i;
  435. if(i< max_index) state++;
  436. }
  437. return truncated ? max : -1;
  438. }
  439. /**
  440. * get unary exp golomb k-th order binarization.
  441. */
  442. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  443. int i, v;
  444. int m= 1<<k;
  445. if(get_cabac(c, state)==0)
  446. return 0;
  447. if(0 < max_index) state++;
  448. for(i=1; i<max; i++){
  449. if(get_cabac(c, state)==0){
  450. if(is_signed && get_cabac_bypass(c)){
  451. return -i;
  452. }else
  453. return i;
  454. }
  455. if(i < max_index) state++;
  456. }
  457. while(get_cabac_bypass(c)){
  458. i+= m;
  459. m+= m;
  460. }
  461. v=0;
  462. while(m>>=1){
  463. v+= v + get_cabac_bypass(c);
  464. }
  465. i += v;
  466. if(is_signed && get_cabac_bypass(c)){
  467. return -i;
  468. }else
  469. return i;
  470. }
  471. #endif /* 0 */
  472. #endif /* AVCODEC_CABAC_H */