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.

557 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 && HAVE_EBX_AVAILABLE && !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 && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
  261. int bit;
  262. #if HAVE_FAST_CMOV
  263. #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
  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, tmpbyte)\
  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, tmpbyte)\
  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 %a3(%2), %%esi \n\t"
  318. "movl %a4(%2), %%ebx \n\t"
  319. BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl", "%a5")
  320. "movl %%esi, %a3(%2) \n\t"
  321. "movl %%ebx, %a4(%2) \n\t"
  322. :"=&a"(bit)
  323. :"r"(state), "r"(c),
  324. "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
  325. "i"(offsetof(CABACContext, bytestream))
  326. : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
  327. );
  328. bit&=1;
  329. #else /* ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !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 && HAVE_EBX_AVAILABLE && !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 && HAVE_EBX_AVAILABLE
  369. __asm__ volatile(
  370. "movl %a2(%1), %%ebx \n\t"
  371. "movl %a3(%1), %%eax \n\t"
  372. "shl $17, %%ebx \n\t"
  373. "add %%eax, %%eax \n\t"
  374. "sub %%ebx, %%eax \n\t"
  375. "cltd \n\t"
  376. "and %%edx, %%ebx \n\t"
  377. "add %%ebx, %%eax \n\t"
  378. "xor %%edx, %%ecx \n\t"
  379. "sub %%edx, %%ecx \n\t"
  380. "test %%ax, %%ax \n\t"
  381. " jnz 1f \n\t"
  382. "mov %a4(%1), %%"REG_b" \n\t"
  383. "subl $0xFFFF, %%eax \n\t"
  384. "movzwl (%%"REG_b"), %%edx \n\t"
  385. "bswap %%edx \n\t"
  386. "shrl $15, %%edx \n\t"
  387. "add $2, %%"REG_b" \n\t"
  388. "addl %%edx, %%eax \n\t"
  389. "mov %%"REG_b", %a4(%1) \n\t"
  390. "1: \n\t"
  391. "movl %%eax, %a3(%1) \n\t"
  392. :"+c"(val)
  393. :"r"(c),
  394. "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
  395. "i"(offsetof(CABACContext, bytestream))
  396. : "%eax", "%"REG_b, "%edx", "memory"
  397. );
  398. return val;
  399. #else
  400. int range, mask;
  401. c->low += c->low;
  402. if(!(c->low & CABAC_MASK))
  403. refill(c);
  404. range= c->range<<(CABAC_BITS+1);
  405. c->low -= range;
  406. mask= c->low >> 31;
  407. range &= mask;
  408. c->low += range;
  409. return (val^mask)-mask;
  410. #endif
  411. }
  412. /**
  413. *
  414. * @return the number of bytes read or 0 if no end
  415. */
  416. static int av_unused get_cabac_terminate(CABACContext *c){
  417. c->range -= 2;
  418. if(c->low < c->range<<(CABAC_BITS+1)){
  419. renorm_cabac_decoder_once(c);
  420. return 0;
  421. }else{
  422. return c->bytestream - c->bytestream_start;
  423. }
  424. }
  425. #if 0
  426. /**
  427. * Get (truncated) unary binarization.
  428. */
  429. static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  430. int i;
  431. for(i=0; i<max; i++){
  432. if(get_cabac(c, state)==0)
  433. return i;
  434. if(i< max_index) state++;
  435. }
  436. return truncated ? max : -1;
  437. }
  438. /**
  439. * get unary exp golomb k-th order binarization.
  440. */
  441. static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  442. int i, v;
  443. int m= 1<<k;
  444. if(get_cabac(c, state)==0)
  445. return 0;
  446. if(0 < max_index) state++;
  447. for(i=1; i<max; i++){
  448. if(get_cabac(c, state)==0){
  449. if(is_signed && get_cabac_bypass(c)){
  450. return -i;
  451. }else
  452. return i;
  453. }
  454. if(i < max_index) state++;
  455. }
  456. while(get_cabac_bypass(c)){
  457. i+= m;
  458. m+= m;
  459. }
  460. v=0;
  461. while(m>>=1){
  462. v+= v + get_cabac_bypass(c);
  463. }
  464. i += v;
  465. if(is_signed && get_cabac_bypass(c)){
  466. return -i;
  467. }else
  468. return i;
  469. }
  470. #endif /* 0 */
  471. #endif /* AVCODEC_CABAC_H */