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

📄 mode_decision.c

📁 H264视频编解码程序
💻 C
📖 第 1 页 / 共 5 页
字号:

/*!
 ***************************************************************************
 * \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 "global.h"
#include "rdopt_coding_state.h"
#include "mb_access.h"
#include "intrarefresh.h"
#include "image.h"
#include "transform8x8.h"
#include "fast_me.h"
#include "simplified_fast_me.h"
#include "ratectl.h"            
#include "mode_decision.h"
#include "fmo.h"

//==== MODULE PARAMETERS ====
imgpel temp_imgY[16][16]; // to temp store the Y data for 8x8 transform
imgpel temp_imgU[16][16];
imgpel temp_imgV[16][16];

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 !!!
// Residue Color Transform
const int  mb_mode_table_RCT[11]  = {0, 1, 2, 3, P8x8, I16MB, I16MB, I16MB, I16MB, I4MB, I8MB};

/*!
*************************************************************************************
* \brief
*    Update Rate Control Difference
*************************************************************************************
*/
void rc_store_diff(int cpix_x, int cpix_y, imgpel prediction[16][16])
{
  int i,j;
  int pix_x, pix_y;
  
  for (i=0; i<MB_BLOCK_SIZE; i++)
  {
    pix_x = cpix_x + i;
    for(j=0; j<MB_BLOCK_SIZE; j++)
    {
      pix_y = cpix_y + j;
      diffy[j][i] = imgY_org[pix_y][pix_x] - prediction[j][i];
    }
  } 
}

/*!
*************************************************************************************
* \brief
*    Update Rate Control Parameters
*************************************************************************************
*/
void update_rc(Macroblock *currMB, short best_mode)
{
  if(img->type==P_SLICE)
  {
    img->MADofMB[img->current_mb_nr] = calc_MAD();
    
    if(input->basicunit<img->Frame_Total_Number_MB)
    {
      img->TotalMADBasicUnit +=img->MADofMB[img->current_mb_nr];
      
      // delta_qp is present only for non-skipped macroblocks
      if ((currMB->cbp!=0 || best_mode==I16MB))
        currMB->prev_cbp = 1;
      else
      {
        currMB->delta_qp = 0;
        currMB->qp = currMB->prev_qp;
        img->qp = currMB->qp;
        currMB->prev_cbp = 0;
      }
      // When MBAFF is used, delta_qp is only present for 
      // the first non-skipped macroblock of each macroblock pair
      if (input->MbInterlace)
      {
        if(!currMB->mb_field)
        {
          DELTA_QP = currMB->delta_qp;
          QP       = currMB->qp;
        }
        else
        {
          DELTA_QP2 = currMB->delta_qp;
          QP2       = currMB->qp;
        }
      }       
    }
  }
}

/*!
*************************************************************************************
* \brief
*    Fast intra decision
*************************************************************************************
*/
void fast_mode_intra_decision(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(img->current_mb_nr, -1 ,  i-1 , 0, &left[i]);
  }
  getNeighbour(img->current_mb_nr, 0     ,  -1 , 0, &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 += abs(imgY_org[img->opix_y][img->opix_x+i] - enc_picture->imgY[img->pix_y-1][img->pix_x+i]);
      SBE += abs(imgY_org[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 += abs(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 += abs(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 += abs(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 += abs(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));  
  enc_mb->best_ref[LIST_0] = 0;
  enc_mb->best_ref[LIST_1] = -1;
  
  // Set valid modes
  enc_mb->valid[I8MB]  = input->Transform8x8Mode;
  enc_mb->valid[I4MB]  = (input->Transform8x8Mode==2) ? 0:1;
  enc_mb->valid[I16MB] = 1;
  enc_mb->valid[IPCM]  = (input->symbol_mode != CABAC && input->EnableIPCM);
  
  enc_mb->valid[0]     = (!intra );
  enc_mb->valid[1]     = (!intra && input->InterSearch16x16);
  enc_mb->valid[2]     = (!intra && input->InterSearch16x8);
  enc_mb->valid[3]     = (!intra && input->InterSearch8x16);
  enc_mb->valid[4]     = (!intra && input->InterSearch8x8);
  enc_mb->valid[5]     = (!intra && input->InterSearch8x4 && !(input->Transform8x8Mode==2));
  enc_mb->valid[6]     = (!intra && input->InterSearch4x8 && !(input->Transform8x8Mode==2));
  enc_mb->valid[7]     = (!intra && input->InterSearch4x4 && !(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)
    {
      enc_mb->valid[I8MB]  = 0;
      enc_mb->valid[IPCM]  = 0;
      enc_mb->valid[0]     = 0;
      enc_mb->valid[1]     = 0;
      enc_mb->valid[2]     = 0; 
      enc_mb->valid[3]     = 0;
      enc_mb->valid[4]     = 0;
      enc_mb->valid[5]     = 0;
      enc_mb->valid[6]     = 0;
      enc_mb->valid[7]     = 0;
      enc_mb->valid[P8x8]  = 0;
      enc_mb->valid[12]    = 0;
      if(check_for_SI16())
      {
        enc_mb->valid[I4MB]  = 0;
        enc_mb->valid[I16MB] = 1;
      }
      else
      {
        enc_mb->valid[I4MB]  = 1;
        enc_mb->valid[I16MB] = 0;
      }
    }
  }

  if(img->type==SP_SLICE)
  {
    if(sp2_frame_indicator)
    {
      if(check_for_SI16())
      {
        enc_mb->valid[I8MB]  = 0;
        enc_mb->valid[IPCM]  = 0;
        enc_mb->valid[0]     = 0;
        enc_mb->valid[1]     = 0;
        enc_mb->valid[2]     = 0; 
        enc_mb->valid[3]     = 0;
        enc_mb->valid[4]     = 0;
        enc_mb->valid[5]     = 0;
        enc_mb->valid[6]     = 0;
        enc_mb->valid[7]     = 0;
        enc_mb->valid[P8x8]  = 0;
        enc_mb->valid[12]    = 0;
        enc_mb->valid[I4MB]  = 0;
        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 = img->lambda_me[5][img->qp];
    enc_mb->lambda_mf = img->lambda_mf[5][img->qp];
  }
  else
  {
    enc_mb->lambda_md = img->lambda_md[img->type][img->qp];
    enc_mb->lambda_me = img->lambda_me[img->type][img->qp];
    enc_mb->lambda_mf = img->lambda_mf[img->type][img->qp];
  }
  
  // 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(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 || 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, ref, cur_list) 
            : (int) (2 * enc_mb.lambda_me * min(ref, 1)));     

          mcost += motion_cost[mode][list][ref][block];
          if (mcost < bmcost[list])
          {
            bmcost[list]   = mcost;
            best_ref[list] = (char)ref;
          }
        }

⌨️ 快捷键说明

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