⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cabac.c

📁 the newest JM software by h.264 JVT official reference model.
💻 C
📖 第 1 页 / 共 4 页
字号:
    {
      if (block_b.available)
      {
        if(img->mb_data[block_b.mb_addr].mb_type==IPCM)
          upper_bit = 1;
        else
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
      }


      if (block_a.available)
      {
        if(img->mb_data[block_a.mb_addr].mb_type == IPCM)
          left_bit = 1;
        else
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
      }

      ctx = 2 * upper_bit + left_bit;

      //===== encode symbol =====
      biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
    }
  }
  else if( IS_INDEPENDENT(params) )
  {
    if (type!=LUMA_8x8)
    {
      if (block_b.available)
      {
        if(img->mb_data[block_b.mb_addr].mb_type==IPCM)
          upper_bit = 1;
        else
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits[0],bit+bit_pos_b);
      }


      if (block_a.available)
      {
        if(img->mb_data[block_a.mb_addr].mb_type == IPCM)
          left_bit = 1;
        else
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits[0], bit + bit_pos_a);
      }

      ctx = 2 * upper_bit + left_bit;

      //===== encode symbol =====
      biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
    }
  }
  else 
  {
    if (block_b.available)
    {
      if(img->mb_data[block_b.mb_addr].mb_type == IPCM)
        upper_bit=1;
      else
      {
        if(type==LUMA_8x8)
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits_8x8[0], bit + bit_pos_b);
        else if (type==CB_8x8)
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits_8x8[1], bit + bit_pos_b);
        else if (type==CR_8x8)
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits_8x8[2], bit + bit_pos_b);
        else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits[1], bit + bit_pos_b);
        else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits[2], bit + bit_pos_b);
        else
          upper_bit = get_bit(img->mb_data[block_b.mb_addr].cbp_bits[0], bit + bit_pos_b);
      }
    }

    if (block_a.available)
    {
      if(img->mb_data[block_a.mb_addr].mb_type==IPCM)
        left_bit = 1;
      else
      {
        if(type==LUMA_8x8)
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits_8x8[0],bit + bit_pos_a);
        else if (type==CB_8x8)
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits_8x8[1],bit + bit_pos_a);
        else if (type==CR_8x8)
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits_8x8[2],bit + bit_pos_a);
        else if ((type==CB_4x4)||(type==CB_4x8)||(type==CB_8x4)||(type==CB_16AC)||(type==CB_16DC))
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits[1],bit + bit_pos_a);
        else if ((type==CR_4x4)||(type==CR_4x8)||(type==CR_8x4)||(type==CR_16AC)||(type==CR_16DC))
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits[2],bit + bit_pos_a);
        else
          left_bit = get_bit(img->mb_data[block_a.mb_addr].cbp_bits[0],bit + bit_pos_a);
      }
    }

    ctx = 2*upper_bit+left_bit;

    //===== encode symbol =====
    biari_encode_symbol (eep_dp, (short)cbp_bit, tex_ctx->bcbp_contexts[type2ctx_bcbp[type]]+ctx);
  }
}


/*!
****************************************************************************
* \brief
*    Write Significance MAP
****************************************************************************
*/
void write_significance_map (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[], int coeff_ctr, TextureInfoContexts*  tex_ctx)
{
  static int   k;
  static unsigned short sig, last;
  int   k0 = 0;
  int   k1 = maxpos[type];

#if ENABLE_FIELD_CTX
  int               fld       = ( img->structure!=FRAME || currMB->mb_field );
#else
  int               fld       = 0;
#endif
  BiContextTypePtr  map_ctx   = tex_ctx->map_contexts [fld][type2ctx_map [type]];
  BiContextTypePtr  last_ctx  = tex_ctx->last_contexts[fld][type2ctx_last[type]];
  const byte *pos2ctxmap  = fld ? pos2ctx_map_int [type] : pos2ctx_map  [type];
  const byte *pos2ctxlast = pos2ctx_last[type];

  if (!c1isdc[type])
  {
    k0++; k1++; coeff--;
  }

  for (k=k0; k<k1; k++) // if last coeff is reached, it has to be significant
  {
    sig   = (coeff[k] != 0);
    biari_encode_symbol  (eep_dp, sig,  map_ctx + pos2ctxmap[k]);
    if (sig)
    {
      last = (--coeff_ctr == 0);

      biari_encode_symbol(eep_dp, last, last_ctx + pos2ctxlast[k]);
      if (last) 
        return;
    }
  }
}


/*!
 ****************************************************************************
 * \brief
 *    Write Levels
 ****************************************************************************
 */
void write_significant_coefficients (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[], TextureInfoContexts*  tex_ctx)
{
  BiContextType *one_contexts = tex_ctx->one_contexts[type2ctx_one[type]];
  BiContextType *abs_contexts = tex_ctx->abs_contexts[type2ctx_abs[type]];  
  int   absLevel;
  int   ctx;
  short sign;
  short greater_one;
  int   c1 = 1;
  int   c2 = 0;
  int   i;

  for (i=maxpos[type]; i>=0; i--)
  {
    if (coeff[i]!=0)
    {
      if (coeff[i] > 0)
      {
        absLevel =  coeff[i];  
        sign = 0;
      }
      else            
      {
        absLevel = -coeff[i];  
        sign = 1;
      }

      greater_one = (absLevel > 1);

      //--- if coefficient is one ---
      ctx = imin(c1, 4);
      biari_encode_symbol (eep_dp, greater_one, one_contexts + ctx);

      if (greater_one)
      {
        ctx = imin(c2++, max_c2[type]);
        unary_exp_golomb_level_encode(eep_dp, absLevel - 2, abs_contexts + ctx);
        c1 = 0;
      }
      else if (c1)
      {
        c1++;
      }
      biari_encode_symbol_eq_prob (eep_dp, sign);
    }
  }
}



/*!
 ****************************************************************************
 * \brief
 *    Write Block-Transform Coefficients
 ****************************************************************************
 */
void writeRunLevel_CABAC (Macroblock* currMB, SyntaxElement *se, DataPartition *dp)
{
  EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
  int curr_len = arienco_bits_written(eep_dp);
  static int  coeff[64];
  static int  coeff_ctr = 0;
  static int  pos       = 0;

  //--- accumulate run-level information ---
  if (se->value1 != 0)
  {
    pos += se->value2;
    coeff[pos++] = se->value1;
    coeff_ctr++;
  }
  else
  {
    TextureInfoContexts *tex_ctx = img->currentSlice->tex_ctx;
    //===== encode CBP-BIT =====
    if (coeff_ctr > 0)
    {
      write_and_store_CBP_block_bit  (currMB, eep_dp, se->context, 1, tex_ctx);
      //===== encode significance map =====
      write_significance_map         (currMB, eep_dp, se->context, coeff, coeff_ctr, tex_ctx);
      //===== encode significant coefficients =====
      write_significant_coefficients (currMB, eep_dp, se->context, coeff, tex_ctx);
    }
    else
      write_and_store_CBP_block_bit  (currMB, eep_dp, se->context, 0, tex_ctx);

    //--- reset counters ---
    pos = coeff_ctr = 0;
    memset(coeff, 0 , 64 * sizeof(int));
  }

  dp->bitstream->write_flag = 1;
  se->len = (arienco_bits_written(eep_dp) - curr_len);
  CABAC_TRACE;
}




/*!
 ************************************************************************
 * \brief
 *    Unary binarization and encoding of a symbol by using
 *    one or two distinct models for the first two and all
 *    remaining bins
 *
 ************************************************************************/
static void unary_bin_encode(EncodingEnvironmentPtr eep_dp,
                      unsigned int symbol,
                      BiContextTypePtr ctx,
                      int ctx_offset)
{  
  if (symbol==0)
  {
    biari_encode_symbol(eep_dp, 0, ctx );
    return;
  }
  else
  {
    biari_encode_symbol(eep_dp, 1, ctx );
    ctx += ctx_offset;
    while ((--symbol) > 0)
      biari_encode_symbol(eep_dp, 1, ctx);
    biari_encode_symbol(eep_dp, 0, ctx);
  }
}

/*!
 ************************************************************************
 * \brief
 *    Unary binarization and encoding of a symbol by using
 *    one or two distinct models for the first two and all
 *    remaining bins; no terminating "0" for max_symbol
 *    (finite symbol alphabet)
 ************************************************************************
 */
static void unary_bin_max_encode(EncodingEnvironmentPtr eep_dp,
                          unsigned int symbol,
                          BiContextTypePtr ctx,
                          int ctx_offset,
                          unsigned int max_symbol)
{
  if (symbol==0)
  {
    biari_encode_symbol(eep_dp, 0, ctx );
    return;
  }
  else
  {
    unsigned int l = symbol;
    biari_encode_symbol(eep_dp, 1, ctx );
    
    ctx += ctx_offset;
    while ((--l)>0)
      biari_encode_symbol(eep_dp, 1, ctx);
    if (symbol < max_symbol)
      biari_encode_symbol(eep_dp, 0, ctx);
  }
}


/*!
 ************************************************************************
 * \brief
 *    Exp Golomb binarization and encoding
 ************************************************************************
 */
void exp_golomb_encode_eq_prob( EncodingEnvironmentPtr eep_dp,
                                unsigned int symbol,
                                int k)
{
  while(1)
  {
    if (symbol >= (unsigned int)(1<<k))
    {
      biari_encode_symbol_eq_prob(eep_dp, 1);   //first unary part
      symbol = symbol - (1<<k);
      k++;
    }
    else
    {
      biari_encode_symbol_eq_prob(eep_dp, 0);   //now terminated zero of unary part
      while (k--)                               //next binary part
        biari_encode_symbol_eq_prob(eep_dp, (signed short)((symbol>>k)&1));
      break;
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Exp-Golomb for Level Encoding
*
************************************************************************/
static void unary_exp_golomb_level_encode( EncodingEnvironmentPtr eep_dp,
                                    unsigned int symbol,
                                    BiContextTypePtr ctx)
{
  if (symbol==0)
  {
    biari_encode_symbol(eep_dp, 0, ctx );
    return;
  }
  else
  {
    unsigned int l=symbol;
    unsigned int k = 1;

    biari_encode_symbol(eep_dp, 1, ctx );
    while (((--l)>0) && (++k <= 13))
      biari_encode_symbol(eep_dp, 1, ctx);
    if (symbol < 13) 
      biari_encode_symbol(eep_dp, 0, ctx);
    else 
      exp_golomb_encode_eq_prob(eep_dp,symbol - 13, 0);
  }
}


/*!
 ************************************************************************
 * \brief
 *    Exp-Golomb for MV Encoding
*
************************************************************************/
static void unary_exp_golomb_mv_encode(EncodingEnvironmentPtr eep_dp,
                                unsigned int symbol,
                                BiContextTypePtr ctx,
                                unsigned int max_bin)
{  
  if (symbol==0)
  {
    biari_encode_symbol(eep_dp, 0, ctx );
    return;
  }
  else
  {
    unsigned int bin = 1;
    unsigned int l = symbol, k = 1;
    biari_encode_symbol(eep_dp, 1, ctx++ );

    while (((--l)>0) && (++k <= 8))
    {
      biari_encode_symbol(eep_dp, 1, ctx  );
      if ((++bin) == 2) 
        ctx++;
      if (bin == max_bin) 
        ctx++;
    }
    if (symbol < 8) 
      biari_encode_symbol(eep_dp, 0, ctx);
    else 
      exp_golomb_encode_eq_prob(eep_dp, symbol - 8, 3);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -