cabac.h
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:10k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*
  2.  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3.  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  *
  19.  */
  20.  
  21. /**
  22.  * @file cabac.h
  23.  * Context Adaptive Binary Arithmetic Coder.
  24.  */
  25. //#undef NDEBUG //Picard
  26. //#include <assert.h> //Picard
  27. #define CABAC_BITS 8
  28. #define CABAC_MASK ((1<<CABAC_BITS)-1)
  29. typedef struct CABACContext{
  30.     int low;
  31.     int range;
  32.     int outstanding_count;
  33. #ifdef STRICT_LIMITS
  34.     int symCount;
  35. #endif
  36.     uint8_t lps_range[2*65][4];   ///< rangeTabLPS
  37.     uint8_t lps_state[2*64];      ///< transIdxLPS
  38.     uint8_t mps_state[2*64];      ///< transIdxMPS
  39.     const uint8_t *bytestream_start;
  40.     const uint8_t *bytestream;
  41.     const uint8_t *bytestream_end;
  42.     PutBitContext pb;
  43. }CABACContext;
  44. extern const uint8_t ff_h264_lps_range[64][4];
  45. extern const uint8_t ff_h264_mps_state[64];
  46. extern const uint8_t ff_h264_lps_state[64];
  47. extern const uint8_t ff_h264_norm_shift[256];
  48. void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
  49. void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
  50. void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], 
  51.                           uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
  52. static inline void put_cabac_bit(CABACContext *c, int b){
  53.     put_bits(&c->pb, 1, b); 
  54.     for(;c->outstanding_count; c->outstanding_count--){ 
  55.         put_bits(&c->pb, 1, 1-b);
  56.     }
  57. }
  58. static inline void renorm_cabac_encoder(CABACContext *c){
  59.     while(c->range < 0x100){
  60.         //FIXME optimize
  61.         if(c->low<0x100){
  62.             put_cabac_bit(c, 0);
  63.         }else if(c->low<0x200){
  64.             c->outstanding_count++;
  65.             c->low -= 0x100;
  66.         }else{
  67.             put_cabac_bit(c, 1);
  68.             c->low -= 0x200;
  69.         }
  70.         
  71.         c->range+= c->range;
  72.         c->low += c->low;
  73.     }
  74. }
  75. static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
  76.     int RangeLPS= c->lps_range[*state][c->range>>6];
  77.     
  78.     if(bit == ((*state)&1)){
  79.         c->range -= RangeLPS;
  80.         *state= c->mps_state[*state];
  81.     }else{
  82.         c->low += c->range - RangeLPS;
  83.         c->range = RangeLPS;
  84.         *state= c->lps_state[*state];
  85.     }
  86.     
  87.     renorm_cabac_encoder(c);
  88. #ifdef STRICT_LIMITS
  89.     c->symCount++;
  90. #endif
  91. }
  92. static inline 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 inline 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.         
  124. #ifdef STRICT_LIMITS
  125.     c->symCount++;
  126. #endif
  127. }
  128. /**
  129.  *
  130.  * @return the number of bytes written
  131.  */
  132. static inline int put_cabac_terminate(CABACContext *c, int bit){
  133.     c->range -= 2;
  134.     if(!bit){
  135.         renorm_cabac_encoder(c);
  136.     }else{
  137.         c->low += c->range;
  138.         c->range= 2;
  139.         
  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.         
  145.         flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
  146.     }
  147.         
  148. #ifdef STRICT_LIMITS
  149.     c->symCount++;
  150. #endif
  151.     return (put_bits_count(&c->pb)+7)>>3;
  152. }
  153. /**
  154.  * put (truncated) unary binarization.
  155.  */
  156. static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
  157.     int i;
  158.     
  159.     assert(v <= max);
  160.     
  161. #if 1
  162.     for(i=0; i<v; i++){
  163.         put_cabac(c, state, 1);
  164.         if(i < max_index) state++;
  165.     }
  166.     if(truncated==0 || v<max)
  167.         put_cabac(c, state, 0);
  168. #else
  169.     if(v <= max_index){
  170.         for(i=0; i<v; i++){
  171.             put_cabac(c, state+i, 1);
  172.         }
  173.         if(truncated==0 || v<max)
  174.             put_cabac(c, state+i, 0);
  175.     }else{
  176.         for(i=0; i<=max_index; i++){
  177.             put_cabac(c, state+i, 1);
  178.         }
  179.         for(; i<v; i++){
  180.             put_cabac(c, state+max_index, 1);
  181.         }
  182.         if(truncated==0 || v<max)
  183.             put_cabac(c, state+max_index, 0);
  184.     }
  185. #endif
  186. }
  187. /**
  188.  * put unary exp golomb k-th order binarization.
  189.  */
  190. static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
  191.     int i;
  192.     
  193.     if(v==0)
  194.         put_cabac(c, state, 0);
  195.     else{
  196.         const int sign= v < 0;
  197.         
  198.         if(is_signed) v= ABS(v);
  199.         
  200.         if(v<max){
  201.             for(i=0; i<v; i++){
  202.                 put_cabac(c, state, 1);
  203.                 if(i < max_index) state++;
  204.             }
  205.             put_cabac(c, state, 0);
  206.         }else{
  207.             int m= 1<<k;
  208.             for(i=0; i<max; i++){
  209.                 put_cabac(c, state, 1);
  210.                 if(i < max_index) state++;
  211.             }
  212.             v -= max;
  213.             while(v >= m){ //FIXME optimize
  214.                 put_cabac_bypass(c, 1);
  215.                 v-= m;
  216.                 m+= m;
  217.             }
  218.             put_cabac_bypass(c, 0);
  219.             while(m>>=1){
  220.                 put_cabac_bypass(c, v&m);
  221.             }
  222.         }
  223.         if(is_signed)
  224.             put_cabac_bypass(c, sign);
  225.     }
  226. }
  227. static void refill(CABACContext *c){
  228.     if(c->bytestream <= c->bytestream_end)
  229. #if CABAC_BITS == 16
  230.         c->low+= ((c->bytestream[0]<<9) + (c->bytestream[1])<<1);
  231. #else
  232.         c->low+= c->bytestream[0]<<1;
  233. #endif
  234.     c->low -= CABAC_MASK;
  235.     c->bytestream+= CABAC_BITS/8;
  236. }
  237. #if 0 /* all use commented */
  238. static void refill2(CABACContext *c){
  239.     int i, x;
  240.     x= c->low ^ (c->low-1);
  241.     i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
  242.     x= -CABAC_MASK;
  243.     
  244.     if(c->bytestream < c->bytestream_end)
  245. #if CABAC_BITS == 16
  246.         x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
  247. #else
  248.         x+= c->bytestream[0]<<1;
  249. #endif
  250.     
  251.     c->low += x<<i;
  252.     c->bytestream+= CABAC_BITS/8;
  253. }
  254. #endif
  255. static inline void renorm_cabac_decoder(CABACContext *c){
  256.     while(c->range < (0x200 << CABAC_BITS)){
  257.         c->range+= c->range;
  258.         c->low+= c->low;
  259.         if(!(c->low & CABAC_MASK))
  260.             refill(c);
  261.     }
  262. }
  263. static inline void renorm_cabac_decoder_once(CABACContext *c){
  264.     int mask= (c->range - (0x200 << CABAC_BITS))>>31;
  265.     c->range+= c->range&mask;
  266.     c->low  += c->low  &mask;
  267.     if(!(c->low & CABAC_MASK))
  268.         refill(c);
  269. }
  270. static inline int get_cabac(CABACContext *c, uint8_t * const state){
  271.     int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
  272.     int bit, lps_mask attribute_unused;
  273.     
  274.     c->range -= RangeLPS;
  275. #if 1
  276.     if(c->low < c->range){
  277.         bit= (*state)&1;
  278.         *state= c->mps_state[*state];
  279.         renorm_cabac_decoder_once(c);
  280.     }else{
  281. //        int shift= ff_h264_norm_shift[RangeLPS>>17];
  282.         bit= ((*state)&1)^1;
  283.         c->low -= c->range;
  284.         *state= c->lps_state[*state];
  285.         c->range = RangeLPS;
  286.         renorm_cabac_decoder(c);
  287. /*        c->range = RangeLPS<<shift;
  288.         c->low <<= shift;
  289.         if(!(c->low & 0xFFFF)){
  290.             refill2(c);
  291.         }*/
  292.     }
  293. #else
  294.     lps_mask= (c->range - c->low)>>31;
  295.     
  296.     c->low -= c->range & lps_mask;
  297.     c->range += (RangeLPS - c->range) & lps_mask;
  298.     
  299.     bit= ((*state)^lps_mask)&1;
  300.     *state= c->mps_state[(*state) - (128&lps_mask)];
  301.     
  302.     lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
  303.     c->range<<= lps_mask;
  304.     c->low  <<= lps_mask;
  305.     if(!(c->low & CABAC_MASK))
  306.         refill2(c);
  307. #endif
  308.     return bit;    
  309. }
  310. static inline int get_cabac_bypass(CABACContext *c){
  311.     c->low += c->low;
  312.     if(!(c->low & CABAC_MASK))
  313.         refill(c);
  314.     
  315.     if(c->low < c->range){
  316.         return 0;
  317.     }else{
  318.         c->low -= c->range;
  319.         return 1;
  320.     }
  321. }
  322. /**
  323.  *
  324.  * @return the number of bytes read or 0 if no end
  325.  */
  326. static inline int get_cabac_terminate(CABACContext *c){
  327.     c->range -= 4<<CABAC_BITS;
  328.     if(c->low < c->range){
  329.         renorm_cabac_decoder_once(c);
  330.         return 0;
  331.     }else{
  332.         return c->bytestream - c->bytestream_start;
  333.     }    
  334. }
  335. /**
  336.  * get (truncated) unnary binarization.
  337.  */
  338. static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
  339.     int i;
  340.     
  341.     for(i=0; i<max; i++){ 
  342.         if(get_cabac(c, state)==0)
  343.             return i;
  344.             
  345.         if(i< max_index) state++;
  346.     }
  347.     return truncated ? max : -1;
  348. }
  349. /**
  350.  * get unary exp golomb k-th order binarization.
  351.  */
  352. static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
  353.     int i, v;
  354.     int m= 1<<k;
  355.     
  356.     if(get_cabac(c, state)==0) 
  357.         return 0;
  358.         
  359.     if(0 < max_index) state++;
  360.     
  361.     for(i=1; i<max; i++){ 
  362.         if(get_cabac(c, state)==0){
  363.             if(is_signed && get_cabac_bypass(c)){
  364.                 return -i;
  365.             }else
  366.                 return i;
  367.         }
  368.         if(i < max_index) state++;
  369.     }
  370.     
  371.     while(get_cabac_bypass(c)){
  372.         i+= m;
  373.         m+= m;
  374.     }
  375.     
  376.     v=0;
  377.     while(m>>=1){
  378.         v+= v + get_cabac_bypass(c);
  379.     }
  380.     i += v;
  381.     if(is_signed && get_cabac_bypass(c)){
  382.         return -i;
  383.     }else
  384.         return i;
  385. }