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

📄 md_highfast.c

📁 the newest JM software by h.264 JVT official reference model.
💻 C
📖 第 1 页 / 共 2 页
字号:

/*!
 ***************************************************************************
 * \file md_highfast.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 "ratectl.h"
#include "mode_decision.h"
#include "fmo.h"
#include "me_umhex.h"
#include "me_umhexsmp.h"
#include "macroblock.h"
#include "conformance.h"
#include "vlc.h"
#include "rdopt.h"

/*!
*************************************************************************************
* \brief
*    Fast intra decision
*************************************************************************************
*/

static 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)

  getNeighbour(currMB,  0 ,  -1 , img->mb_size[IS_LUMA], &up);
  getNeighbour(currMB, -1 ,  -1 , img->mb_size[IS_LUMA], &left[0]);
  getNeighbour(currMB, -1 ,   0 , img->mb_size[IS_LUMA], &left[1]);
  
  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(pImgOrg[1][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(pImgOrg[1][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(pImgOrg[2][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(pImgOrg[2][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
*    Mode Decision for a macroblock
*************************************************************************************
*/
void encode_one_macroblock_highfast (Slice *currSlice, Macroblock *currMB)
{
  int         max_index;

  int         block, index, mode, i, j, ctr16x16;
  char        best_pdir;
  RD_PARAMS   enc_mb;
  double      min_rdcost = 1e30, max_rdcost = 1e30;
  double      min_dcost = 1e30;
  char        best_ref[2] = {0, -1};
  int         bmcost[5] = {INT_MAX};
  int         cost=0;
  int         min_cost = INT_MAX, cost_direct=0, have_direct=0, i16mode=0;
  int         intra1 = 0;
  int         lambda_mf[3];
  int         cost8x8_direct = 0;
  short       islice      = (img->type==I_SLICE);
  short       bslice      = (img->type==B_SLICE);
  short       pslice      = (img->type==P_SLICE) || (img->type==SP_SLICE);
  short       intra       = (islice || (pslice && img->mb_y==img->mb_y_upd && img->mb_y_upd!=img->mb_y_intra));
  int         is_cavlc = (img->currentSlice->symbol_mode == CAVLC);
  Macroblock     *prevMB     = currMB->PrevMB; 
  Block8x8Info *b8x8info   = img->b8x8info;

  short   *allmvs = img->all_mv[0][0][0][0][0];
  short   min_chroma_pred_mode, max_chroma_pred_mode;
  imgpel  **mb_pred = img->mb_pred[0];

  // Fast Mode Decision
  short inter_skip = 0, intra_skip = 0;
  short  bipred_me;
  int cost16 = 0, mode16 = 0;
  double min_rate = 0, RDCost16 = DBL_MAX;


  if(params->SearchMode == UM_HEX)
  {
    UMHEX_decide_intrabk_SAD();
  }
  else if (params->SearchMode == UM_HEX_SIMPLE)
  {
    smpUMHEX_decide_intrabk_SAD();
  }

  if (params->AdaptiveRounding)
  {
    reset_adaptive_rounding();
  }

  if (img->MbaffFrameFlag)
  {
    reset_mb_nz_coeff(img->current_mb_nr);
  }

  intra |= RandomIntra (img->current_mb_nr);    // Forced Pseudo-Random Intra

  //===== Setup Macroblock encoding parameters =====
  init_enc_mb_params(currMB, &enc_mb, intra, bslice);

  //=====   S T O R E   C O D I N G   S T A T E   =====
  //---------------------------------------------------
  store_coding_state (currSlice, currMB, cs_cm);

  if (!intra)
  {
    //===== set direct motion vectors =====
    best_mode = 1;
    if (bslice)
    {
      Get_Direct_Motion_Vectors (currMB);
      if (enc_mb.valid[0])
      {
        best_mode = 0;
        currMB->c_ipred_mode=DC_PRED_8;
        min_rdcost = max_rdcost;
        compute_mode_RD_cost(currSlice, currMB, &enc_mb, 0, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
      }
    }

    if (params->CtxAdptLagrangeMult == 1)
    {
      get_initial_mb16x16_cost(currMB);
    }

    //===== MOTION ESTIMATION FOR 16x16, 16x8, 8x16 BLOCKS =====
    for (min_cost=INT_MAX, mode=1; mode<4; mode++)
    {
      bipred_me = 0;
      b8x8info->bipred8x8me[mode][0] = 0;
      if (enc_mb.valid[mode] && !inter_skip)
      {
        for (cost=0, block=0; block<(mode==1?1:2); block++)
        {
          update_lambda_costs(&enc_mb, lambda_mf);
          PartitionMotionSearch (currMB, mode, block, lambda_mf);

          //--- set 4x4 block indizes (for getting MV) ---
          j = (block==1 && mode==2 ? 2 : 0);
          i = (block==1 && mode==3 ? 2 : 0);

          //--- get cost and reference frame for List 0 prediction ---
          bmcost[LIST_0] = INT_MAX;
          list_prediction_cost(currMB, LIST_0, block, mode, &enc_mb, bmcost, best_ref);

          if (bslice)
          {
            //--- get cost and reference frame for List 1 prediction ---
            bmcost[LIST_1] = INT_MAX;
            list_prediction_cost(currMB, LIST_1, block, mode, &enc_mb, bmcost, best_ref);

            // Compute bipredictive cost between best list 0 and best list 1 references
            list_prediction_cost(currMB, BI_PRED, block, mode, &enc_mb, bmcost, best_ref);

            // currently Bi prediction ME is only supported for modes 1, 2, 3 and ref 0
            if (is_bipred_enabled(mode))
            {
              list_prediction_cost(currMB, BI_PRED_L0, block, mode, &enc_mb, bmcost, 0);
              list_prediction_cost(currMB, BI_PRED_L1, block, mode, &enc_mb, bmcost, 0);
            }
            else
            {
              bmcost[BI_PRED_L0] = INT_MAX;
              bmcost[BI_PRED_L1] = INT_MAX;
            }

            // Determine prediction list based on mode cost
            determine_prediction_list(mode, bmcost, best_ref, &best_pdir, &cost, &bipred_me);
          }
          else // if (bslice)
          {
            best_pdir  = 0;
            cost      += bmcost[LIST_0];
          }

          assign_enc_picture_params(mode, best_pdir, 2 * block, enc_mb.list_offset[LIST_0], best_ref[LIST_0], best_ref[LIST_1], bslice, bipred_me);

          //----- set reference frame and direction parameters -----
          set_block8x8_info(b8x8info, mode, block, best_ref, best_pdir, bipred_me);
          
          //--- set reference frames and motion vectors ---
          if (mode>1 && block==0)
            SetRefAndMotionVectors (&enc_picture->motion, currMB, block, mode, best_pdir, best_ref[LIST_0], best_ref[LIST_1], bipred_me);
        } // for (block=0; block<(mode==1?1:2); block++)


        if(mode == 1)
        {
          if(pslice)
            min_rdcost = max_rdcost;

          //=====   S T O R E   C O D I N G   S T A T E   =====
          //---------------------------------------------------
          //store_coding_state (currSlice, currMB, cs_cm);

                    
          currMB->c_ipred_mode=DC_PRED_8;
          compute_mode_RD_cost(currSlice, currMB, &enc_mb, mode, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
         
          if(pslice)
          {
            // Get SKIP motion vector and compare SKIP_MV with best motion vector of 16x16
            FindSkipModeMotionVector (currMB);
            if(params->EarlySkipEnable)
            {
              //===== check for SKIP mode =====
              if ( currMB->cbp==0 && enc_picture->motion.ref_idx[LIST_0][img->block_y][img->block_x]==0 &&
                enc_picture->motion.mv[LIST_0][img->block_y][img->block_x][0]==allmvs[0] &&
                enc_picture->motion.mv[LIST_0][img->block_y][img->block_x][1]==allmvs[1]               )
              {
                inter_skip = 1;
                best_mode = 0;
              }
            } // if(params->EarlySkipEnable)
          }

          // store variables.
          RDCost16 = min_rdcost;
          mode16 = best_mode;
          cost16 = cost;
        } // if(mode == 1)

        if ((!inter_skip) && (cost < min_cost))
        {
          best_mode = mode;
          min_cost  = cost;
          if (params->CtxAdptLagrangeMult == 1)
          {
            adjust_mb16x16_cost(cost);
          }
        }
      } // if (enc_mb.valid[mode])

⌨️ 快捷键说明

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