cabac.c

来自「JM 11.0 KTA 2.1 Source Code」· C语言 代码 · 共 1,962 行 · 第 1/5 页

C
1,962
字号
      }
    }
    return;
  }
  else
  {
    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+pos2ctx_map_int [type][k]);
      if (sig)
      {
        last = (--coeff_ctr == 0);
        
        biari_encode_symbol(eep_dp, last, last_ctx+pos2ctx_last[type][k]);
        if (last) return;
      }
    }
  }
}

#ifdef RDO_Q
/*!
 ****************************************************************************
 * \brief
 *    estimate CABAC bit cost for significant coefficient map
 ****************************************************************************
 */
void est_significance_map(Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type)
{
  int   k;
  unsigned short sig, last;
  int   k1      = maxpos[type]-1;
  int               fld       = ( img->structure!=FRAME || currMB->mb_field );
  BiContextTypePtr  map_ctx   = ( fld ? img->currentSlice->tex_ctx->fld_map_contexts[type2ctx_map [type]]
    : img->currentSlice->tex_ctx->map_contexts[type2ctx_map [type]] );
  BiContextTypePtr  last_ctx  = ( fld ? img->currentSlice->tex_ctx->fld_last_contexts[type2ctx_last[type]]
    : img->currentSlice->tex_ctx->last_contexts[type2ctx_last[type]] );
  
  for (k=0; k<k1; k++) // if last coeff is reached, it has to be significant
  {
    sig   = 0;     
    estBitsCabac[type].significantBits[pos2ctx_map[type][k]][sig]=biari_no_bits  (eep_dp, sig,  map_ctx+pos2ctx_map     [type][k]);

    sig   = 1;     
    estBitsCabac[type].significantBits[pos2ctx_map[type][k]][sig]=biari_no_bits  (eep_dp, sig,  map_ctx+pos2ctx_map     [type][k]);

    last=0;
    estBitsCabac[type].lastBits[pos2ctx_last[type][k]][last]=biari_no_bits(eep_dp, last, last_ctx+pos2ctx_last[type][k]);

    last=1;
    estBitsCabac[type].lastBits[pos2ctx_last[type][k]][last]=biari_no_bits(eep_dp, last, last_ctx+pos2ctx_last[type][k]);
  }
  // if last coeff is reached, it has to be significant
  estBitsCabac[type].significantBits[pos2ctx_map[type][k1]][0]=0;
  estBitsCabac[type].significantBits[pos2ctx_map[type][k1]][1]=0;
  estBitsCabac[type].lastBits[pos2ctx_last[type][k1]][0]=0;
  estBitsCabac[type].lastBits[pos2ctx_last[type][k1]][1]=0;
}
#endif

/*!
****************************************************************************
* \brief
*    Write Significance MAP
****************************************************************************
*/
#ifdef ADAPTIVE_FD_SD_CODING
void write_significance_map_SD (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[], int coeff_ctr)
{
  int   k;
  unsigned short sig, last;
  int   k0      = 0;
  int   k1      = maxpos[type]-1;

  BiContextTypePtr  map_ctx       = img->currentSlice->tex_ctx->map_contexts_SD;
  BiContextTypePtr  last_ctx      = img->currentSlice->tex_ctx->last_contexts_SD;

  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+pos2ctx_map_int [type][k]);

    if (sig)
    {
      last = (--coeff_ctr == 0);

      biari_encode_symbol(eep_dp, last, last_ctx+pos2ctx_last[type][k]);

      if (last) return;
    }
  }
}




void write_significance_map_SD8 (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[], int coeff_ctr)
{
  int   k;
  unsigned short sig, last;
  int   k0      = 0;
  int   k1      = maxpos[type]-1;

  BiContextTypePtr  map_ctx       = img->currentSlice->tex_ctx->map8_contexts_SD;
  BiContextTypePtr  last_ctx      = img->currentSlice->tex_ctx->last8_contexts_SD;

  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+k);

    if (sig)
    {
      last = (--coeff_ctr == 0);

      biari_encode_symbol(eep_dp, last, last_ctx+k);

      if (last) return;
    }
  }
  return;
}
#endif





/*!
 ****************************************************************************
 * \brief
 *    Write Levels
 ****************************************************************************
 */
void write_significant_coefficients (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[])
{
  int   i;
  int   absLevel;
  int   ctx;
  short sign;
  short greater_one;
  int   c1 = 1;
  int   c2 = 0;
  
  for (i=maxpos[type]-1; 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 = min(c1,4);    
      biari_encode_symbol (eep_dp, greater_one, img->currentSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);

      if (greater_one)
      {
        ctx = min(c2, max_c2[type]);
        unary_exp_golomb_level_encode(eep_dp, absLevel-2, img->currentSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);
        c1 = 0;
        c2++;
      }
      else if (c1)
      {
        c1++;
      }
      biari_encode_symbol_eq_prob (eep_dp, sign);
    }
  }
}

#ifdef RDO_Q
/*!
 ****************************************************************************
 * \brief
 *    estimate bit cost of significant coefficient
 ****************************************************************************
 */
void est_significant_coefficients (Macroblock* currMB, EncodingEnvironmentPtr eep_dp,  int type)
{
  int   ctx;
  short greater_one;
  int maxCtx=min(4, max_c2[type]);

  for (ctx=0; ctx<=4; ctx++)
  {    
    greater_one=0;
    estBitsCabac[type].greaterOneBits[0][ctx][greater_one]=
      biari_no_bits (eep_dp, greater_one, img->currentSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);

    greater_one=1;
    estBitsCabac[type].greaterOneBits[0][ctx][greater_one]=
      biari_no_bits (eep_dp, greater_one, img->currentSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);
  }

  for (ctx=0; ctx<=maxCtx; ctx++)
  {
    estBitsCabac[type].greaterOneBits[1][ctx][0]=
      biari_no_bits(eep_dp, 0, img->currentSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);
  
    estBitsCabac[type].greaterOneState[ctx]=biari_state(eep_dp, 0, img->currentSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);

    estBitsCabac[type].greaterOneBits[1][ctx][1]=
      biari_no_bits(eep_dp, 1, img->currentSlice->tex_ctx->abs_contexts[type2ctx_abs[type]] + ctx);
  }
}
#endif

#ifdef ADAPTIVE_FD_SD_CODING
/*!
 ****************************************************************************
 * \brief
 *    Write Levels
 ****************************************************************************
 */
void write_significant_coefficients_SD (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[])
{
  int   i;
  int   absLevel;
  int   ctx;
  short sign;
  short greater_one;
  int   c1 = 1;
  int   c2 = 0;


  for (i=maxpos[type]-1; 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 = min(c1,4);

      biari_encode_symbol (eep_dp, greater_one, img->currentSlice->tex_ctx->one_contexts_SD + ctx);

      if (greater_one)
      {
        ctx = min(c2, max_c2[type]);

        unary_exp_golomb_level_encode(eep_dp, absLevel-2, img->currentSlice->tex_ctx->abs_contexts_SD + ctx);

        c1 = 0;
        c2++;
      }
      else if (c1)
      {
        c1++;
      }

      biari_encode_symbol_eq_prob (eep_dp, sign);
    }
  }
}

void write_significant_coefficients_SD8 (Macroblock* currMB, EncodingEnvironmentPtr eep_dp, int type, int coeff[])
{
  int   i;
  int   absLevel;
  int   ctx;
  short sign;
  short greater_one;
  int   c1 = 1;
  int   c2 = 0;

  for (i=maxpos[type]-1; 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 = min(c1,4);

      biari_encode_symbol (eep_dp, greater_one, img->currentSlice->tex_ctx->one8_contexts_SD+ ctx);

      if (greater_one)
      {
        ctx = min(c2, max_c2[type]);

        unary_exp_golomb_level_encode(eep_dp, absLevel-2, img->currentSlice->tex_ctx->abs8_contexts_SD + ctx);

        c1 = 0;
        c2++;
      }
      else if (c1)
      {
        c1++;
      }
      biari_encode_symbol_eq_prob (eep_dp, sign);
    }
  }
}
#endif




#ifdef ADAPTIVE_FD_SD_CODING
/*!
 ****************************************************************************
 * \brief
 *    Write Block-Transform Coefficients if coded in frequency domain or
 *    samples if coded in spatial domain
 ****************************************************************************
 */
void writeRunLevel_CABAC (SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
  int         writing_b4=se->writing_b4;
  int         writing_b8=se->writing_b8;
#ifdef USE_INTRA_MDDT
  static int  coeff    [256];
#else
  static int  coeff    [64];
#endif
  static int  coeff_ctr = 0;
  static int  pos       = 0;
  int         i;
  int         DF_or_SD=0;
  int         b8_y=((writing_b8/ 2)<<3)+((writing_b4/ 2)<<2);
  int         b8_x=((writing_b8% 2)<<3)+((writing_b4% 2)<<2);
  Macroblock* currMB    = &img->mb_data[img->current_mb_nr];
  int         need_SD_FD_bit     =(currMB->b8mode[writing_b8] != IBLOCK && currMB->luma_transform_size_8x8_flag==0 && se->context==LUMA_4x4 && !IS_INTRA (currMB));
  int         need_SD_FD_bit_t8x8=(currMB->b8mode[writing_b8] != IBLOCK && currMB->luma_transform_size_8x8_flag==1 && se->context==LUMA_8x8 && !IS_INTRA (currMB));
  int SD;

  if (img->APEC_in_FD_and_SD==0)
  {
    need_SD_FD_bit=0;
    need_SD_FD_bit_t8x8=0;
  }
  if (img->APEC_in_FD_and_SD==1 && currMB->SD_Coding_on_off==0)
  {
    need_SD_FD_bit=0;
    need_SD_FD_bit_t8x8=0;
  }

  if (cabac_encoding && (currMB->b8mode[writing_b8] == IBLOCK || currMB->luma_transform_size_8x8_flag==0 || se->context==LUMA_4x4 || IS_INTRA (currMB)))
    set_bit ( &(currMB->SD_or_FD_t8x8), writing_b8, 0);

  SD= (need_SD_FD_bit      && (currMB->SD_or_FD[writing_b8/ 2][writing_b8% 2] & (1<<writing_b4))!=0) ||
          (need_SD_FD_bit_t8x8 && ((currMB->SD_or_FD_t8x8) & (1<<writing_b8))!=0);

  /

⌨️ 快捷键说明

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