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

📄 cabac.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
      }
      else if (act_sym<=2)
      {
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][act_ctx]);
        biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][4]);
        csym=act_sym-1;
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
      }
      else if (act_sym<=10)
      {
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][act_ctx]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][4]);
        biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][5]);
        csym=(((act_sym-3)>>2)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        csym=(((act_sym-3)>>1)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        csym=((act_sym-3)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
      }
      else if (act_sym==11 || act_sym==22)
      {
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][act_ctx]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][4]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][5]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        if (act_sym==11)  biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        else              biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
      }
      else
      {
        if (act_sym > 22) act_sym--;
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][act_ctx]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][4]);
        biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][5]);
        csym=(((act_sym-12)>>3)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        csym=(((act_sym-12)>>2)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        csym=(((act_sym-12)>>1)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        csym=((act_sym-12)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][6]); 
        else      biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][6]);
        if (act_sym >=22) act_sym++;
      }
    }

    if(act_sym==mode16x16) // additional info for 16x16 Intra-mode
    {
      if( mode_sym==25 )
      {
        biari_encode_symbol_final(eep_dp, 1 );
        return;
      }
      biari_encode_symbol_final(eep_dp, 0 );

      act_ctx = 8;
      act_sym = mode_sym/12;
      biari_encode_symbol(eep_dp, (unsigned char) act_sym, ctx->mb_type_contexts[1] + act_ctx ); // coding of AC/no AC
      mode_sym = mode_sym % 12;

      act_sym = mode_sym / 4; // coding of cbp: 0,1,2
      act_ctx = 9;
      if (act_sym==0)
      {
        biari_encode_symbol(eep_dp, 0, ctx->mb_type_contexts[1] + act_ctx );
      }
      else
      {
        biari_encode_symbol(eep_dp, 1, ctx->mb_type_contexts[1] + act_ctx );
        if (act_sym==1)
        {
          biari_encode_symbol(eep_dp, 0, ctx->mb_type_contexts[1] + act_ctx );
        }
        else
        {
          biari_encode_symbol(eep_dp, 1, ctx->mb_type_contexts[1] + act_ctx );
        }
      }

      mode_sym = mode_sym % 4; // coding of I pred-mode: 0,1,2,3
      act_ctx  = 10;
      act_sym  = mode_sym/2;
      biari_encode_symbol(eep_dp, (unsigned char) act_sym, ctx->mb_type_contexts[1] + act_ctx );
      act_sym  = mode_sym%2;
      biari_encode_symbol(eep_dp, (unsigned char) act_sym, ctx->mb_type_contexts[1] + act_ctx );
    }
  }
}


/*!
 ***************************************************************************
 * \brief
 *    This function is used to arithmetically encode the 8x8 block
 *    type info
 ***************************************************************************
 */
void writeB8_typeInfo2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
  int act_ctx;
  int act_sym, csym;
  int bframe=(img->type==B_IMG || img->type==BS_IMG);

  MotionInfoContexts *ctx    = (img->currentSlice)->mot_ctx;

  act_sym = se->value1;
  act_ctx = 0;

  if (!bframe)
  {
    switch (act_sym)
    {
    case 0:
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[0][1]);
      break;
    case 1:
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[0][1]);
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[0][3]);
      break;
    case 2:
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[0][1]);
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[0][3]);
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[0][4]);
      break;
    case 3:
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[0][1]);
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[0][3]);
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[0][4]);
      break;
    }
  }
  else //===== B-FRAME =====
  {
    if (act_sym==0)
    {
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][0]);
      return;
    }
    else
    {
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][0]);
      act_sym--;
    }
    if (act_sym<2)
    {
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][1]);
      if (act_sym==0)   biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
      else              biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
    }
    else if (act_sym<6)
    {
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][1]);
      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][2]);
      csym=(((act_sym-2)>>1)&0x01);
      if (csym) biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
      else      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
      csym=((act_sym-2)&0x01);
      if (csym) biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
      else      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
    }
    else
    {
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][1]);
      biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][2]);
      csym=(((act_sym-6)>>2)&0x01);
      if (csym)
      {
        biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
        csym=((act_sym-6)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
      }
      else
      {
        biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
        csym=(((act_sym-6)>>1)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
        csym=((act_sym-6)&0x01);
        if (csym) biari_encode_symbol (eep_dp, 1, &ctx->b8_type_contexts[1][3]);
        else      biari_encode_symbol (eep_dp, 0, &ctx->b8_type_contexts[1][3]);
      }
    }
  }
}


/*!
 ****************************************************************************
 * \brief
 *    This function is used to arithmetically encode a pair of
 *    intra prediction modes of a given MB.
 ****************************************************************************
 */
void writeIntraPredMode2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
  TextureInfoContexts *ctx = img->currentSlice->tex_ctx;

        // use_most_probable_mode
        if (se->value1 == -1)
				biari_encode_symbol(eep_dp, 1, ctx->ipr_contexts);
        else
        {
				biari_encode_symbol(eep_dp, 0, ctx->ipr_contexts);

                // remaining_mode_selector
                biari_encode_symbol(eep_dp,(signed short)( se->value1 & 0x1    ), ctx->ipr_contexts+1);
                biari_encode_symbol(eep_dp,(signed short)((se->value1 & 0x2)>>1), ctx->ipr_contexts+1);
                biari_encode_symbol(eep_dp,(signed short)((se->value1 & 0x4)>>2), ctx->ipr_contexts+1);
        }
}
/*!
 ****************************************************************************
 * \brief
 *    This function is used to arithmetically encode the reference
 *    parameter of a given MB.
 ****************************************************************************
 */
void writeRefFrame2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
  MotionInfoContexts  *ctx    = img->currentSlice->mot_ctx;
  Macroblock          *currMB = &img->mb_data[img->current_mb_nr];
  int                 addctx  = 0;

  int   a, b;
  int   act_ctx;
  int   act_sym;
  int** refframe_array = ((img->type==B_IMG || img->type==BS_IMG)? fw_refFrArr : refFrArr);
  int   block_y        = img->block_y;

  if( input->InterlaceCodingOption >= MB_CODING && mb_adaptive )
  {
    if( !img->field_mode )
    {
      refframe_array = ((img->type==B_IMG || img->type==BS_IMG) ? fw_refFrArr_frm : refFrArr_frm);
    }
    else if ( !img->top_field )
    {
      refframe_array = ((img->type==B_IMG || img->type==BS_IMG) ? fw_refFrArr_bot : refFrArr_bot);
      block_y        = ( img->block_y - 4 ) / 2;
    }
    else
    {
      refframe_array = ((img->type==B_IMG || img->type==BS_IMG) ? fw_refFrArr_top : refFrArr_top);
      block_y        = img->block_y / 2;
    }
  }

	if ((img->subblock_y == 0) && ((currMB->mb_available[0][1] == NULL) || (IS_DIRECT(currMB->mb_available[0][1]))))
		b = 0;
  else
	{
    /*===== HS: the chosen refframe_array should already contain the scale versions of RefIdx =====
		if(( input->InterlaceCodingOption >= MB_CODING && mb_adaptive && (img->subblock_y == 0)
			 && (img->field_mode == 0) && (currMB->mb_available[0][1]->mb_field == 1))) //|| (img->field_mode == 1))
			b = (refframe_array[block_y+img->subblock_y-1][img->block_x+img->subblock_x] > 1 ? 1 : 0);
		else
    */
			b = (refframe_array[block_y+img->subblock_y-1][img->block_x+img->subblock_x] > 0 ? 1 : 0);
	}

	if ((img->subblock_x == 0) && ((currMB->mb_available[1][0] == NULL) || (IS_DIRECT(currMB->mb_available[1][0]))))
    a = 0;
  else 
	{
    /*===== HS: the chosen refframe_array should already contain the scale versions of RefIdx =====
		if(( input->InterlaceCodingOption >= MB_CODING && mb_adaptive && (img->subblock_x == 0)
			 && (img->field_mode == 0) && (currMB->mb_available[1][0]->mb_field == 1))) // || (img->field_mode == 1))
			a = (refframe_array[block_y+img->subblock_y][img->block_x+img->subblock_x-1] > 1 ? 1 : 0);
		else
    */
			a = (refframe_array[block_y+img->subblock_y][img->block_x+img->subblock_x-1] > 0 ? 1 : 0);
	}

  act_ctx     = a + 2*b; 
  se->context = act_ctx; // store context
  act_sym     = se->value1;

  if (act_sym==0)
  {
    biari_encode_symbol(eep_dp, 0, ctx->ref_no_contexts[addctx] + act_ctx );
  }
  else
  {
    biari_encode_symbol(eep_dp, 1, ctx->ref_no_contexts[addctx] + act_ctx);
    act_sym--;
    act_ctx=4;
    unary_bin_encode(eep_dp, act_sym,ctx->ref_no_contexts[addctx]+act_ctx,1);
  }
}


/*!
 ****************************************************************************
 * \brief
 *    This function is used to arithmetically encode the reference
 *    parameter of a given MB.
 ****************************************************************************
 */
void writeBwdRefFrame2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
  MotionInfoContexts  *ctx    = img->currentSlice->mot_ctx;
  Macroblock          *currMB = &img->mb_data[img->current_mb_nr];
  int                 addctx  = 0;

  int   a, b;
  int   act_ctx;
  int   act_sym;
  int** refframe_array = ((img->type==B_IMG || img->type==BS_IMG)? bw_refFrArr : refFrArr);
  int   block_y        = img->block_y;


  if( input->InterlaceCodingOption >= MB_CODING && mb_adaptive )
  {
    if( !img->field_mode )
    {
      refframe_array = ((img->type==B_IMG || img->type==BS_IMG) ? bw_refFrArr_frm : refFrArr_frm);
    }
    else if ( !img->top_field )
    {
      refframe_array = ((img->type==B_IMG || img->type==BS_IMG) ? bw_refFrArr_bot : refFrArr_bot);
      block_y        = ( img->block_y - 4 ) / 2;
    }
    else
    {
      refframe_array = ((img->type==B_IMG || img->type==BS_IMG) ? bw_refFrArr_top : refFrArr_top);
      block_y        = img->block_y / 2;
    }
  }
	if ((img->subblock_y == 0) && ((currMB->mb_available[0][1] == NULL) || (IS_DIRECT(currMB->mb_available[0][1]))))
		b = 0;
	else
	{
    /*===== HS: the chosen refframe_array should already contain the scale versions of RefIdx =====
		if( input->InterlaceCodingOption >= MB_CODING && mb_adaptive && (img->subblock_y == 0)
			 && (img->field_mode == 0) && (currMB->mb_available[0][1]->mb_field == 1))
			b = (BWD_IDX(refframe_array[block_y+img->subblock_y-1][img->block_x+img->subblock_x]) > 1 ? 1 : 0);
		else
    */
			b = (BWD_IDX(refframe_array[block_y+img->subblock_y-1][img->block_x+img->subblock_x]) > 0 ? 1 : 0);
	}

	if ((img->subblock_x == 0) && ((currMB->mb_available[1][0] == NULL) || (IS_DIRECT(currMB->mb_available[1][0]))))
		a = 0;
  else 
	{
    /*===== HS: the chosen refframe_array should already contain the scale versions of RefIdx =====
		if( input->InterlaceCodingOption >= MB_CODING && mb_adaptive && (img->subblock_x == 0)
			 && (img->field_mode == 0) && (currMB->mb_available[1][0]->mb_field == 1))
			a = (BWD_IDX(refframe_array[block_y+img->subblock_y][img->block_x+img->subblock_x-1]) > 1 ? 1 : 0);
		else
    */
			a = (BWD_IDX(refframe_array[block_y+img->subblock_y][img->block_x+img->subblock_x-1]) > 0 ? 1 : 0);
	}


  act_ctx     = a + 2*b;
  se->context = act_ctx; // store context
  act_sym     = se->value1;

  if (act_sym==0)
  {
    biari_encode_symbol(eep_dp, 0, ctx->/*bwd_*/ref_no_contexts[addctx] + act_ctx );
  }
  else
  {
    biari_encode_symbol(eep_dp, 1, ctx->/*bwd_*/ref_no_contexts[addctx] + act_ctx);
    act_sym--;
    act_ctx=4;
    unary_bin_encode(eep_dp, act_sym,ctx->/*bwd_*/ref_no_contexts[addctx]+act_ctx,1);
  }
}


/*!
 ****************************************************************************
 * \brief
 *    This function is used to arithmetically encode the motion
 *    vector data of a given MB.
 ****************************************************************************
 */
void writeMVD2Buffer_CABAC(SyntaxElement *se, EncodingEnvironmentPtr eep_dp)
{
  int i = img->subblock_x;

⌨️ 快捷键说明

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