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

📄 mode_decision.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 3 页
字号:

/*!
 ***************************************************************************
 * \file mode_decision.c
 *
 * \brief
 *    Main macroblock mode decision functions and helpers
 *
 **************************************************************************
 */

#include <math.h>
#include <limits.h>
#include <float.h>

#include "global.h"
#include "rdopt_coding_state.h"
#include "mb_access.h"
#include "intrarefresh.h"
#include "image.h"
#include "transform8x8.h"
#include "ratectl.h"
#include "mode_decision.h"
#include "fmo.h"
#include "me_umhex.h"
#include "me_umhexsmp.h"
#include "macroblock.h"
#include "rdoq.h"
#include "errdo.h"


//==== MODULE PARAMETERS ====
const int  b8_mode_table[6]  = {0, 4, 5, 6, 7};         // DO NOT CHANGE ORDER !!!
const int  mb_mode_table[9]  = {0, 1, 2, 3, P8x8, I16MB, I4MB, I8MB, IPCM}; // DO NOT CHANGE ORDER !!!

//double *mb16x16_cost_frame;

/*!
*************************************************************************************
* \brief
*    Reset Valid Modes
*************************************************************************************
*/
void reset_valid_modes(RD_PARAMS *enc_mb)
{
  memset(enc_mb->valid, 0, MAXMODE * sizeof(short));
}


/*!
*************************************************************************************
* \brief
*    Initialize Encoding parameters for Macroblock
*************************************************************************************
*/
void init_enc_mb_params(Macroblock* currMB, RD_PARAMS *enc_mb, int intra, int bslice)
{
  int l,k;

  //Setup list offset
  enc_mb->list_offset[LIST_0] = LIST_0 + currMB->list_offset;
  enc_mb->list_offset[LIST_1] = LIST_1 + currMB->list_offset;

  enc_mb->curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
 
  // Set valid modes  
  enc_mb->valid[I8MB]  = (!params->DisableIntraInInter || intra )?   params->Transform8x8Mode : 0;
  enc_mb->valid[I4MB]  = (!params->DisableIntraInInter || intra )? ((params->Transform8x8Mode == 2) ? 0 : 1) : 0;
  enc_mb->valid[I4MB]  = (!params->DisableIntra4x4  ) ? enc_mb->valid[I4MB] : 0;
  enc_mb->valid[I16MB] = (!params->DisableIntraInInter || intra )? 1 : 0;
  enc_mb->valid[I16MB] = (!params->DisableIntra16x16) ? enc_mb->valid[I16MB] : 0;
  enc_mb->valid[IPCM]  = (!params->DisableIntraInInter || intra )? params->EnableIPCM : 0;

  enc_mb->valid[0]     = (!intra && params->InterSearch[bslice][0]);
  enc_mb->valid[1]     = (!intra && params->InterSearch[bslice][1]);
  enc_mb->valid[2]     = (!intra && params->InterSearch[bslice][2]);
  enc_mb->valid[3]     = (!intra && params->InterSearch[bslice][3]);
  enc_mb->valid[4]     = (!intra && params->InterSearch[bslice][4]);
  enc_mb->valid[5]     = (!intra && params->InterSearch[bslice][5] && !(params->Transform8x8Mode==2));
  enc_mb->valid[6]     = (!intra && params->InterSearch[bslice][6] && !(params->Transform8x8Mode==2));
  enc_mb->valid[7]     = (!intra && params->InterSearch[bslice][7] && !(params->Transform8x8Mode==2));
  enc_mb->valid[P8x8]  = (enc_mb->valid[4] || enc_mb->valid[5] || enc_mb->valid[6] || enc_mb->valid[7]);
  enc_mb->valid[12]    = (img->type == SI_SLICE);
  
  if (params->UseRDOQuant && params->RDOQ_CP_Mode && (img->qp != img->masterQP) )  
    RDOQ_update_mode(enc_mb, bslice);

  if(img->type==SP_SLICE)
  {
    if(si_frame_indicator)
    {
      reset_valid_modes(enc_mb);
      if(check_for_SI16())
      {
        enc_mb->valid[I16MB] = 1;
      }
      else
      {
        enc_mb->valid[I4MB]  = 1;
      }
    }
    
    if(sp2_frame_indicator)
    {
      if(check_for_SI16())
      {
        reset_valid_modes(enc_mb);
        enc_mb->valid[I16MB] = 1;
      }
      else
      {
        enc_mb->valid[I8MB]  = 0;
        enc_mb->valid[IPCM]  = 0;
        enc_mb->valid[0]     = 0;
        enc_mb->valid[I16MB] = 0;
      }
    }
  }

  //===== SET LAGRANGE PARAMETERS =====
  // Note that these are now computed at the slice level to reduce
  // computations and cleanup code.
  if (bslice && img->nal_reference_idc)
  {
    enc_mb->lambda_md = img->lambda_md[5][img->masterQP];

    enc_mb->lambda_me[F_PEL] = img->lambda_me[5][img->masterQP][F_PEL];
    enc_mb->lambda_me[H_PEL] = img->lambda_me[5][img->masterQP][H_PEL];
    enc_mb->lambda_me[Q_PEL] = img->lambda_me[5][img->masterQP][Q_PEL];

    enc_mb->lambda_mf[F_PEL] = img->lambda_mf[5][img->masterQP][F_PEL];
    enc_mb->lambda_mf[H_PEL] = img->lambda_mf[5][img->masterQP][H_PEL];
    enc_mb->lambda_mf[Q_PEL] = img->lambda_mf[5][img->masterQP][Q_PEL];
  }
  else
  {
    enc_mb->lambda_md = img->lambda_md[img->type][img->masterQP];
    enc_mb->lambda_me[F_PEL] = img->lambda_me[img->type][img->masterQP][F_PEL];
    enc_mb->lambda_me[H_PEL] = img->lambda_me[img->type][img->masterQP][H_PEL];
    enc_mb->lambda_me[Q_PEL] = img->lambda_me[img->type][img->masterQP][Q_PEL];

    enc_mb->lambda_mf[F_PEL] = img->lambda_mf[img->type][img->masterQP][F_PEL];
    enc_mb->lambda_mf[H_PEL] = img->lambda_mf[img->type][img->masterQP][H_PEL];
    enc_mb->lambda_mf[Q_PEL] = img->lambda_mf[img->type][img->masterQP][Q_PEL];
  }
 
  if (!img->MbaffFrameFlag)
  {
    for (l = LIST_0; l < BI_PRED; l++)
    {
      for(k = 0; k < listXsize[l]; k++)
      {
        if(img->structure != listX[l][k]->structure)
        {
          if (img->structure == TOP_FIELD)
            listX[l][k]->chroma_vector_adjustment = -2;
          else if (img->structure == BOTTOM_FIELD)
            listX[l][k]->chroma_vector_adjustment = 2;
          else
            listX[l][k]->chroma_vector_adjustment= 0;
        }
        else
          listX[l][k]->chroma_vector_adjustment= 0;
      }
    }
  }
  else
  {
    if (enc_mb->curr_mb_field)
    {
      for (l = enc_mb->list_offset[LIST_0]; l <= enc_mb->list_offset[LIST_1]; l++)
      {
        for(k = 0; k < listXsize[l]; k++)
        {
          listX[l][k]->chroma_vector_adjustment= 0;
          if((img->current_mb_nr & 0x01) == 0 && listX[l][k]->structure == BOTTOM_FIELD)
            listX[l][k]->chroma_vector_adjustment = -2;
          if((img->current_mb_nr & 0x01) == 1 && listX[l][k]->structure == TOP_FIELD)
            listX[l][k]->chroma_vector_adjustment = 2;
        }
      }
    }
    else
    {
      for (l = enc_mb->list_offset[LIST_0]; l <= enc_mb->list_offset[LIST_1]; l++)
      {
        for(k = 0; k < listXsize[l]; k++)
          listX[l][k]->chroma_vector_adjustment = 0;
      }
    }
  }
}

/*!
*************************************************************************************
* \brief
*    computation of prediction list (including biprediction) cost
*************************************************************************************
*/
void list_prediction_cost(Macroblock *currMB, int list, int block, int mode, RD_PARAMS *enc_mb, int bmcost[5], char best_ref[2])
{
  short ref;
  int mcost;
  int cur_list = list < BI_PRED ? enc_mb->list_offset[list] : enc_mb->list_offset[LIST_0];

  //--- get cost and reference frame for forward prediction ---

  if (list < BI_PRED)
  {
    for (ref=0; ref < listXsize[cur_list]; ref++)
    {
      if (!img->checkref || list || ref==0 || (params->RestrictRef && CheckReliabilityOfRef (block, list, ref, mode)))
      {
        // limit the number of reference frames to 1 when switching SP frames are used
        if((!params->sp2_frame_indicator && !params->sp_output_indicator)||
          ((params->sp2_frame_indicator || params->sp_output_indicator) && (img->type != P_SLICE && img->type != SP_SLICE))||
          ((params->sp2_frame_indicator || params->sp_output_indicator) && ((img->type == P_SLICE || img->type == SP_SLICE) &&(ref==0))))
        {
          mcost  = (params->rdopt
            ? REF_COST (enc_mb->lambda_mf[Q_PEL], ref, cur_list)
            : (int) (2 * enc_mb->lambda_me[Q_PEL] * imin(ref, 1)));

          mcost += motion_cost[mode][list][ref][block];
          if (mcost < bmcost[list])
          {
            bmcost[list]   = mcost;
            best_ref[list] = (char)ref;
          }
        }
      }
    }
  }
  else if (list == BI_PRED)
  {
    if (active_pps->weighted_bipred_idc == 1)
    {
      int weight_sum = wbp_weight[0][(int) best_ref[LIST_0]][(int) best_ref[LIST_1]][0] + wbp_weight[1][(int) best_ref[LIST_0]][(int) best_ref[LIST_1]][0];
      
      if (weight_sum < -128 ||  weight_sum > 127)
      {
        bmcost[list] = INT_MAX;
      }
      else
      {
        bmcost[list]  = (params->rdopt
          ? (REF_COST  (enc_mb->lambda_mf[Q_PEL], (short)best_ref[LIST_0], cur_list)
          +  REF_COST  (enc_mb->lambda_mf[Q_PEL], (short)best_ref[LIST_1], cur_list + LIST_1))
          : (int) (2 * (enc_mb->lambda_me[Q_PEL] * (imin((short)best_ref[LIST_0], 1) + imin((short)best_ref[LIST_1], 1)))));
        bmcost[list] += BIDPartitionCost (currMB, mode, block, best_ref, enc_mb->lambda_mf[Q_PEL]);
      }
    }
    else
    {
      bmcost[list]  = (params->rdopt
        ? (REF_COST  (enc_mb->lambda_mf[Q_PEL], (short)best_ref[LIST_0], cur_list)
        +  REF_COST  (enc_mb->lambda_mf[Q_PEL], (short)best_ref[LIST_1], cur_list + LIST_1))
        : (int) (2 * (enc_mb->lambda_me[Q_PEL] * (imin((short)best_ref[LIST_0], 1) + imin((short)best_ref[LIST_1], 1)))));
      bmcost[list] += BIDPartitionCost (currMB, mode, block, best_ref, enc_mb->lambda_mf[Q_PEL]);
    }
  }
  else
  {
    bmcost[list]  = (params->rdopt
      ? (REF_COST (enc_mb->lambda_mf[Q_PEL], 0, cur_list)
      +  REF_COST (enc_mb->lambda_mf[Q_PEL], 0, cur_list + LIST_1))
      : (int) (4 * enc_mb->lambda_me[Q_PEL]));
    bmcost[list] += BPredPartitionCost(currMB, mode, block, 0, 0, enc_mb->lambda_mf[Q_PEL], !(list&1));
  }
}

int compute_ref_cost(RD_PARAMS *enc_mb, int ref, int list)
{
  return WEIGHTED_COST(enc_mb->lambda_mf[Q_PEL],((listXsize[enc_mb->list_offset[list]] <= 1)? 0:refbits[ref]));
}

/*!
*************************************************************************************
* \brief
*    Determination of prediction list based on simple distortion computation
*************************************************************************************
*/
void determine_prediction_list(int mode, int bmcost[5], char best_ref[2], char *best_pdir, int *cost, short *bipred_me)
{
  int bestlist;  
  
  *cost += iminarray ( bmcost, 5, &bestlist);
  
  if (bestlist <= BI_PRED)  //LIST_0, LIST_1 & BI_DIR
  {
    *best_pdir = bestlist; 
    *bipred_me = 0;
  }
  else                      //BI_PRED_L0 & BI_PRED_L1
  {
    *best_pdir = 2;    
    *bipred_me = bestlist - 2;
    best_ref[LIST_1] = 0;
    best_ref[LIST_0] = 0;
  }
}

/*!
*************************************************************************************
* \brief
*    RD decision process
*************************************************************************************
*/
void compute_mode_RD_cost(int mode,
                          Macroblock *currMB,
                          RD_PARAMS *enc_mb,
                          double *min_rdcost,
                          double *min_dcost,
                          double *min_rate,
                          int i16mode,
                          short bslice,
                          short *inter_skip,
                          int   is_cavlc)
{
  //--- transform size ---
  currMB->luma_transform_size_8x8_flag = params->Transform8x8Mode==2
    ?  (mode >= 1 && mode <= 3)
    || (mode == 0 && bslice && active_sps->direct_8x8_inference_flag)
    || ((mode == P8x8) && (enc_mb->valid[4]))
    :  0;
  //store_coding_state (currMB, cs_cm); // RD
  SetModesAndRefframeForBlocks (currMB, mode);

  // Encode with coefficients
  img->NoResidueDirect = 0;

  if ((params->FastCrIntraDecision )
    || (currMB->c_ipred_mode == DC_PRED_8 || (IS_INTRA(currMB) )))
  {
    while(1)
    {
      if (RDCost_for_macroblocks (currMB, enc_mb->lambda_md, mode, min_rdcost, min_dcost, min_rate, i16mode, is_cavlc))
      {
        //Rate control
        if (params->RCEnable)
        {
          if(mode == P8x8)
            rc_store_diff(img->opix_x, img->opix_y, currMB->luma_transform_size_8x8_flag == 1 ? tr8x8.mpr8x8 : tr4x4.mpr8x8);
          else
            rc_store_diff(img->opix_x, img->opix_y, pred);
        }

        store_macroblock_parameters (currMB, mode);

        if(params->rdopt == 2 && mode == 0 && params->EarlySkipEnable)
        {
          // check transform quantized coeff.
          if(currMB->cbp == 0)
            *inter_skip = 1;
        }
      }

      // Go through transform modes.

⌨️ 快捷键说明

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