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

📄 mode_decision.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 3 页
字号:

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

#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#include <float.h>
#include <memory.h>
#include <string.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"


//==== 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
*    Fast intra decision
*************************************************************************************
*/
void fast_mode_intra_decision(Macroblock *currMB, short *intra_skip, double min_rate)
{
  int i;
  int mb_available_up, mb_available_left, mb_available_up_left;
  long SBE;
  double AR = 0, ABE = 0;
  PixelPos up;       //!< pixel position p(0,-1)
  PixelPos left[2];  //!< pixel positions p(-1, -1..0)

  for (i=0;i<2;i++)
  {
    getNeighbour(currMB, -1 ,  i-1 , IS_LUMA, &left[i]);
  }
  getNeighbour(currMB, 0     ,  -1 , IS_LUMA, &up);

  mb_available_up       = up.available;
  mb_available_up_left  = left[0].available;
  mb_available_left     = left[1].available;

  AR=(1.0/384)*min_rate;

  SBE = 0;

  if( (img->mb_y != (int)img->FrameHeightInMbs-1) && (img->mb_x != (int)img->PicWidthInMbs-1) && mb_available_left && mb_available_up)
  {
    for(i = 0; i < MB_BLOCK_SIZE; i++)
    {
      SBE += iabs(pCurImg[img->opix_y][img->opix_x+i] - enc_picture->imgY[img->pix_y-1][img->pix_x+i]);
      SBE += iabs(pCurImg[img->opix_y+i][img->opix_x] - enc_picture->imgY[img->pix_y+i][img->pix_x-1]);
    }
    for(i = 0; i < 8; i++)
    {
      SBE += iabs(imgUV_org[0][img->opix_c_y][img->opix_c_x+i] - enc_picture->imgUV[0][img->pix_c_y-1][img->pix_c_x+i]);
      SBE += iabs(imgUV_org[0][img->opix_c_y+i][img->opix_c_x] - enc_picture->imgUV[0][img->pix_c_y+i][img->pix_c_x-1]);
      SBE += iabs(imgUV_org[1][img->opix_c_y][img->opix_c_x+i] - enc_picture->imgUV[1][img->pix_c_y-1][img->pix_c_x+i]);
      SBE += iabs(imgUV_org[1][img->opix_c_y+i][img->opix_c_x] - enc_picture->imgUV[1][img->pix_c_y+i][img->pix_c_x-1]);
    }
    ABE = 1.0/64 * SBE;
  }
  else  // Image boundary
  {
    ABE = 0;
  }

  if(AR <= ABE)
  {
    *intra_skip = 1;
  }
}

/*!
*************************************************************************************
* \brief
*    Initialize Encoding parameters for Macroblock
*************************************************************************************
*/
void init_enc_mb_params(Macroblock* currMB, RD_PARAMS *enc_mb, int intra, int bslice)
{
  int mode;
  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]  = (!input->DisableIntraInInter || intra )? input->Transform8x8Mode : 0;
  enc_mb->valid[I4MB]  = (!input->DisableIntraInInter || intra )? ((input->Transform8x8Mode == 2) ? 0 : 1) : 0;
  enc_mb->valid[I16MB] = (!input->DisableIntraInInter || intra )? 1 : 0;
  enc_mb->valid[IPCM]  = (!input->DisableIntraInInter || intra )? input->EnableIPCM : 0;

  enc_mb->valid[0]     = (!intra && input->InterSearch[bslice][0]);
  enc_mb->valid[1]     = (!intra && input->InterSearch[bslice][1]);
  enc_mb->valid[2]     = (!intra && input->InterSearch[bslice][2]);
  enc_mb->valid[3]     = (!intra && input->InterSearch[bslice][3]);
  enc_mb->valid[4]     = (!intra && input->InterSearch[bslice][4]);
  enc_mb->valid[5]     = (!intra && input->InterSearch[bslice][5] && !(input->Transform8x8Mode==2));
  enc_mb->valid[6]     = (!intra && input->InterSearch[bslice][6] && !(input->Transform8x8Mode==2));
  enc_mb->valid[7]     = (!intra && input->InterSearch[bslice][7] && !(input->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(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->qp];

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

    enc_mb->lambda_mf[F_PEL] = img->lambda_mf[5][img->qp][F_PEL];
    enc_mb->lambda_mf[H_PEL] = img->lambda_mf[5][img->qp][H_PEL];
    enc_mb->lambda_mf[Q_PEL] = img->lambda_mf[5][img->qp][Q_PEL];

  }
  else
  {
    enc_mb->lambda_md = img->lambda_md[img->type][img->qp];

    enc_mb->lambda_me[F_PEL] = img->lambda_me[img->type][img->qp][F_PEL];
    enc_mb->lambda_me[H_PEL] = img->lambda_me[img->type][img->qp][H_PEL];
    enc_mb->lambda_me[Q_PEL] = img->lambda_me[img->type][img->qp][Q_PEL];

    enc_mb->lambda_mf[F_PEL] = img->lambda_mf[img->type][img->qp][F_PEL];
    enc_mb->lambda_mf[H_PEL] = img->lambda_mf[img->type][img->qp][H_PEL];
    enc_mb->lambda_mf[Q_PEL] = img->lambda_mf[img->type][img->qp][Q_PEL];
  }

  // Initialize bipredME decisions
  for (mode=0; mode<MAXMODE; mode++)
  {
    img->bi_pred_me[mode]=0;
  }

  if (!img->MbaffFrameFlag)
  {
    for (l = LIST_0; l < BI_PRED; l++)
    {
      for(k = 0; k < listXsize[l]; k++)
      {
        listX[l][k]->chroma_vector_adjustment= 0;
        if(img->structure == TOP_FIELD && img->structure != listX[l][k]->structure)
          listX[l][k]->chroma_vector_adjustment = -2;
        if(img->structure == BOTTOM_FIELD && img->structure != listX[l][k]->structure)
          listX[l][k]->chroma_vector_adjustment = 2;
      }
    }
  }
  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 % 2 == 0 && listX[l][k]->structure == BOTTOM_FIELD)
            listX[l][k]->chroma_vector_adjustment = -2;
          if(img->current_mb_nr % 2 == 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 || (input->RestrictRef && CheckReliabilityOfRef (block, list, ref, mode)))
      {
        // limit the number of reference frames to 1 when switching SP frames are used
        if((!input->sp2_frame_indicator && !input->sp_output_indicator)||
          ((input->sp2_frame_indicator || input->sp_output_indicator) && (img->type != P_SLICE && img->type != SP_SLICE))||
          ((input->sp2_frame_indicator || input->sp_output_indicator) && ((img->type == P_SLICE || img->type == SP_SLICE) &&(ref==0))))
        {
          mcost  = (input->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]  = (input->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, (short)best_ref[LIST_0], (short)best_ref[LIST_1], enc_mb.lambda_mf[Q_PEL]);
      }
    }
    else
    {
      bmcost[list]  = (input->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, (short)best_ref[LIST_0], (short)best_ref[LIST_1], enc_mb.lambda_mf[Q_PEL]);
    }
  }
  else
  {
    bmcost[list]  = (input->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 *bi_pred_me)
{
  if ((!input->BiPredMotionEstimation) || (mode != 1))
  {
    //--- get prediction direction ----
    if  (bmcost[LIST_0] <= bmcost[LIST_1]
      && bmcost[LIST_0] <= bmcost[BI_PRED])
    {
      *best_pdir = 0;
      *cost += bmcost[LIST_0];
    }
    else if (bmcost[LIST_1] <= bmcost[LIST_0]
      &&     bmcost[LIST_1] <= bmcost[BI_PRED])
    {
      *best_pdir = 1;
      *cost += bmcost[LIST_1];
    }
    else

⌨️ 快捷键说明

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