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

📄 me_umhex.c

📁 This program can encode the YUV vdieo format to H.264 and decode it.
💻 C
📖 第 1 页 / 共 4 页
字号:

/*!
 ************************************************************************
 *
 * \file me_umhex.c
 *
 * \brief
 *   Fast integer pel motion estimation and fractional pel motion estimation
 *   algorithms are described in this file.
 *   1. UMHEX_get_mem() and UMHEX_free_mem() are functions for allocation and release
 *      of memories about motion estimation
 *   2. UMHEX_BlockMotionSearch() is the function for fast integer pel motion
 *      estimation and fractional pel motion estimation
 *   3. UMHEX_DefineThreshold() defined thresholds for early termination
 * \author
 *    Main contributors: (see contributors.h for copyright, address and affiliation details)
 *    - Zhibo Chen         <chenzhibo@tsinghua.org.cn>
 *    - JianFeng Xu        <fenax@video.mdc.tsinghua.edu.cn>
 *    - Wenfang Fu         <fwf@video.mdc.tsinghua.edu.cn>
 *    - Xiaozhong Xu       <xxz@video.mdc.tsinghua.edu.cn>
 * \date
 *    2006.1
 ************************************************************************
 */

#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "global.h"
#include "memalloc.h"
#include "me_umhex.h"
#include "refbuf.h"
#include "mb_access.h"
#include "image.h"
#include "me_distortion.h"

#define Q_BITS          15
#define MIN_IMG_WIDTH   176
extern  int*   byte_abs;
extern  int*   mvbits;
extern  short*   spiral_search_x;
extern  short*   spiral_search_y;


static const int Diamond_x[4] = {-1, 0, 1, 0};
static const int Diamond_y[4] = {0, 1, 0, -1};
static const int Hexagon_x[6] = {2, 1, -1, -2, -1, 1};
static const int Hexagon_y[6] = {0, -2, -2, 0,  2, 2};
static const int Big_Hexagon_x[16] = {0,-2, -4,-4,-4, -4, -4, -2,  0,  2,  4,  4, 4, 4, 4, 2};
static const int Big_Hexagon_y[16] = {4, 3, 2,  1, 0, -1, -2, -3, -4, -3, -2, -1, 0, 1, 2, 3};

// for bipred mode
static int pred_MV_ref_flag;
static int dist_method;
static StorablePicture *ref_pic_ptr;

static const int   Multi_Ref_Thd[8]   = {0,  300,  120,  120,  60,  30,   30,  15};
static const int   Big_Hexagon_Thd[8] = {0, 3000, 1500, 1500, 800, 400,  400, 200};
static const int   Median_Pred_Thd[8] = {0,  750,  350,  350, 170,  80,   80,  40};
static const int   Threshold_DSR[8]   = {0, 2200, 1000, 1000, 500, 250,  250, 120};

static int Median_Pred_Thd_MB[8];
static int Big_Hexagon_Thd_MB[8];
static int Multi_Ref_Thd_MB[8];


static const int quant_coef[6][4][4] = {
  {{13107, 8066,13107, 8066},{ 8066, 5243, 8066, 5243},{13107, 8066,13107, 8066},{ 8066, 5243, 8066, 5243}},
  {{11916, 7490,11916, 7490},{ 7490, 4660, 7490, 4660},{11916, 7490,11916, 7490},{ 7490, 4660, 7490, 4660}},
  {{10082, 6554,10082, 6554},{ 6554, 4194, 6554, 4194},{10082, 6554,10082, 6554},{ 6554, 4194, 6554, 4194}},
  {{ 9362, 5825, 9362, 5825},{ 5825, 3647, 5825, 3647},{ 9362, 5825, 9362, 5825},{ 5825, 3647, 5825, 3647}},
  {{ 8192, 5243, 8192, 5243},{ 5243, 3355, 5243, 3355},{ 8192, 5243, 8192, 5243},{ 5243, 3355, 5243, 3355}},
  {{ 7282, 4559, 7282, 4559},{ 4559, 2893, 4559, 2893},{ 7282, 4559, 7282, 4559},{ 4559, 2893, 4559, 2893}}
};


void UMHEX_DefineThreshold()
{
  AlphaFourth_1[1] = 0.01f;
  AlphaFourth_1[2] = 0.01f;
  AlphaFourth_1[3] = 0.01f;
  AlphaFourth_1[4] = 0.02f;
  AlphaFourth_1[5] = 0.03f;
  AlphaFourth_1[6] = 0.03f;
  AlphaFourth_1[7] = 0.04f;

  AlphaFourth_2[1] = 0.06f;
  AlphaFourth_2[2] = 0.07f;
  AlphaFourth_2[3] = 0.07f;
  AlphaFourth_2[4] = 0.08f;
  AlphaFourth_2[5] = 0.12f;
  AlphaFourth_2[6] = 0.11f;
  AlphaFourth_2[7] = 0.15f;

  UMHEX_DefineThresholdMB();
  return;
}
/*!
 ************************************************************************
 * \brief
 *    Set MB thresholds for fast motion estimation
 *    Those thresholds may be adjusted to trade off rate-distortion
 *    performance and UMHEX speed
 ************************************************************************
 */

void UMHEX_DefineThresholdMB()
{
  int gb_qp_per    = (input->qpN-MIN_QP)/6;
  int gb_qp_rem    = (input->qpN-MIN_QP)%6;

  int gb_q_bits    = Q_BITS+gb_qp_per;
  int gb_qp_const,Thresh4x4;

  float Quantize_step;
  int i;
// scale factor: defined for different image sizes
  float scale_factor = (float)((1-input->UMHexScale*0.1)+input->UMHexScale*0.1*(img->width/MIN_IMG_WIDTH));
// QP factor: defined for different quantization steps
  float QP_factor = (float)((1.0-0.90*(input->qpN/51.0f)));

  gb_qp_const=(1<<gb_q_bits)/6;
  Thresh4x4 =   ((1<<gb_q_bits) - gb_qp_const)/quant_coef[gb_qp_rem][0][0];
  Quantize_step = Thresh4x4/(4*5.61f)*2.0f*scale_factor;
  Bsize[7]=(16*16)*Quantize_step;

  Bsize[6]=Bsize[7]*4;
  Bsize[5]=Bsize[7]*4;
  Bsize[4]=Bsize[5]*4;
  Bsize[3]=Bsize[4]*4;
  Bsize[2]=Bsize[4]*4;
  Bsize[1]=Bsize[2]*4;

  for(i=1;i<8;i++)
  {
    //ET_Thd1: early termination after median prediction
    Median_Pred_Thd_MB[i]  = (int) (Median_Pred_Thd[i]* scale_factor*QP_factor);
    //ET_thd2: early termination after every circle of 16 points Big-Hex Search
    Big_Hexagon_Thd_MB[i]  = (int) (Big_Hexagon_Thd[i]* scale_factor*QP_factor);
    //threshold for multi ref case
    Multi_Ref_Thd_MB[i]    = (int) (Multi_Ref_Thd[i]  * scale_factor*QP_factor);
    //threshold for usage of DSR technique. DSR ref to JVT-R088
    Threshold_DSR_MB[i]    = (int) (Threshold_DSR[i]  * scale_factor*QP_factor);
  }
}

/*!
 ************************************************************************
 * \brief
 *    Allocation of space for fast motion estimation
 ************************************************************************
 */
int UMHEX_get_mem()
{
  int memory_size = 0;
  if (NULL==(flag_intra = calloc ((img->width>>4)+1,sizeof(byte)))) no_mem_exit("UMHEX_get_mem: flag_intra"); //fwf 20050330

  memory_size += get_mem2D(&McostState, 2*input->search_range+1, 2*input->search_range+1);
  memory_size += get_mem4Dint(&(fastme_ref_cost), img->max_num_references, 9, 4, 4);
  memory_size += get_mem3Dint(&(fastme_l0_cost), 9, img->height/4, img->width/4);
  memory_size += get_mem3Dint(&(fastme_l1_cost), 9, img->height/4, img->width/4);
  memory_size += get_mem2D(&SearchState,7,7);
  memory_size += get_mem2Dint(&(fastme_best_cost), 7, img->width/4);
  if(input->BiPredMotionEstimation == 1)//memory allocation for bipred mode
  {
    memory_size += get_mem3Dint(&(fastme_l0_cost_bipred), 9, img->height/4, img->width/4);//for bipred
    memory_size += get_mem3Dint(&(fastme_l1_cost_bipred), 9, img->height/4, img->width/4);//for bipred
  }

  return memory_size;
}

/*!
 ************************************************************************
 * \brief
 *    Free space for fast motion estimation
 ************************************************************************
 */
void UMHEX_free_mem()
{
  free_mem2D(McostState);
  free_mem4Dint(fastme_ref_cost, img->max_num_references, 9);
  free_mem3Dint(fastme_l0_cost, 9);
  free_mem3Dint(fastme_l1_cost, 9);
  free_mem2D(SearchState);
  free_mem2Dint(fastme_best_cost);
  free (flag_intra);
  if(input->BiPredMotionEstimation == 1)
  {
    free_mem3Dint(fastme_l0_cost_bipred, 9);//for bipred
    free_mem3Dint(fastme_l1_cost_bipred, 9);//for bipred
  }
}

/*!
 ************************************************************************
 * \brief
 *    UMHEXIntegerPelBlockMotionSearch: fast pixel block motion search
 *    this algorithm is called UMHexagonS(see JVT-D016),which includes
 *    four steps with different kinds of search patterns
 * \par Input:
 * imgpel*   orig_pic,     // <--  original picture
 * int       ref,          // <--  reference frame (0... or -1 (backward))
 * int       pic_pix_x,    // <--  absolute x-coordinate of regarded AxB block
 * int       pic_pix_y,    // <--  absolute y-coordinate of regarded AxB block
 * int       blocktype,    // <--  block type (1-16x16 ... 7-4x4)
 * int       pred_mv_x,    // <--  motion vector predictor (x) in sub-pel units
 * int       pred_mv_y,    // <--  motion vector predictor (y) in sub-pel units
 * int*      mv_x,         //  --> motion vector (x) - in pel units
 * int*      mv_y,         //  --> motion vector (y) - in pel units
 * int       search_range, // <--  1-d search range in pel units
 * int       min_mcost,    // <--  minimum motion cost (cost for center or huge value)
 * int       lambda_factor // <--  lagrangian parameter for determining motion cost
 * \par
 * Two macro definitions defined in this program:
 * 1. EARLY_TERMINATION: early termination algrithm, refer to JVT-D016.doc
 * 2. SEARCH_ONE_PIXEL: search one pixel in search range
 * \author
 *   Main contributors: (see contributors.h for copyright, address and affiliation details)
 *   - Zhibo Chen         <chenzhibo@tsinghua.org.cn>
 *   - JianFeng Xu        <fenax@video.mdc.tsinghua.edu.cn>
 *   - Xiaozhong Xu       <xxz@video.mdc.tsinghua.edu.cn>
 * \date   :
 *   2006.1
 ************************************************************************
 */
int                                     //  ==> minimum motion cost after search
UMHEXIntegerPelBlockMotionSearch  (
                                  imgpel   *orig_pic,     //!< <--  not used
                                  short     ref,          //!< <--  reference frame (0... or -1 (backward))
                                  int       list,         //!< <--  reference picture list
                                  int       pic_pix_x,    //!< <--  absolute x-coordinate of regarded AxB block
                                  int       pic_pix_y,    //!< <--  absolute y-coordinate of regarded AxB block
                                  int       blocktype,    //!< <--  block type (1-16x16 ... 7-4x4)
                                  short     pred_mv_x,    //!< <--  motion vector predictor (x) in sub-pel units
                                  short     pred_mv_y,    //!< <--  motion vector predictor (y) in sub-pel units
                                  short*    mv_x,         //!< --> motion vector (x) - in pel units
                                  short*    mv_y,         //!< --> motion vector (y) - in pel units
                                  int       search_range, //!< <--  1-d search range in pel units
                                  int       min_mcost,    //!< <--  minimum motion cost (cost for center or huge value)
                                  int       lambda_factor //!< <--  lagrangian parameter for determining motion cost
                                  )
{
  int   list_offset   = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))?
                         img->current_mb_nr%2 ? 4 : 2 : 0;
  int   mvshift       = 2;                                        //!< motion vector shift for getting sub-pel units
  int   blocksize_y   = input->blc_size[blocktype][1];            //!< vertical block size
  int   blocksize_x   = input->blc_size[blocktype][0];            //!< horizontal block size
  int   pred_x        = (pic_pix_x << mvshift) + pred_mv_x;       //!< predicted position x (in sub-pel units)
  int   pred_y        = (pic_pix_y << mvshift) + pred_mv_y;       //!< predicted position y (in sub-pel units)
  int   center_x      = pic_pix_x + *mv_x;                        //!< center position x (in pel units)
  int   center_y      = pic_pix_y + *mv_y;                        //!< center position y (in pel units)
  int   best_x        = 0, best_y = 0;
  int   search_step, iYMinNow, iXMinNow;
  int   pos, cand_x, cand_y,  mcost;
  int   i,m,j;
  float betaFourth_1,betaFourth_2;
  int  temp_Big_Hexagon_x[16];//  temp for Big_Hexagon_x;
  int  temp_Big_Hexagon_y[16];//  temp for Big_Hexagon_y;
  short mb_x = pic_pix_x - img->opix_x;
  short mb_y = pic_pix_y - img->opix_y;
  short pic_pix_x2 = pic_pix_x >> 2;
  short block_x = (mb_x >> 2);
  short block_y = (mb_y >> 2);
  int ET_Thred = Median_Pred_Thd_MB[blocktype];//ET threshold in use
  int   *SAD_prediction = fastme_best_cost[blocktype-1];//multi ref SAD prediction
  //===== Use weighted Reference for ME ====

  int  apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
    (active_pps->weighted_bipred_idc && (img->type == B_SLICE))) && input->UseWeightedReferenceME;

  dist_method = F_PEL + 3 * apply_weights;

  ref_pic_ptr = listX[list+list_offset][ref];

  // Note that following seem to be universal for all functions and could be moved to a separate, clean public function in me_distortion.c
  
  ref_pic_sub.luma = ref_pic_ptr->curr_imgY_sub;

  img_width  = ref_pic_ptr->size_x;
  img_height = ref_pic_ptr->size_y;
  width_pad  = ref_pic_ptr->size_x_pad;
  height_pad = ref_pic_ptr->size_y_pad;

  if (apply_weights)
  {
    weight_luma = wp_weight[list + list_offset][ref][0];
    offset_luma = wp_offset[list + list_offset][ref][0];
  }

  if (ChromaMEEnable)
  {
    ref_pic_sub.crcb[0] = ref_pic_ptr->imgUV_sub[0];
    ref_pic_sub.crcb[1] = ref_pic_ptr->imgUV_sub[1];
    width_pad_cr  = ref_pic_ptr->size_x_cr_pad;
    height_pad_cr = ref_pic_ptr->size_y_cr_pad;

    if (apply_weights)
    {
      weight_cr[0] = wp_weight[list + list_offset][ref][1];
      weight_cr[1] = wp_weight[list + list_offset][ref][2];
      offset_cr[0] = wp_offset[list + list_offset][ref][1];
      offset_cr[1] = wp_offset[list + list_offset][ref][2];
    }
  }

  //===== set function for getting reference picture lines =====
  if ((center_x > search_range) && (center_x < img_width - 1 - search_range - blocksize_x) &&
    (center_y > search_range) && (center_y < img_height - 1 - search_range - blocksize_y))
  {
    ref_access_method = FAST_ACCESS;
  }
  else
  {
    ref_access_method = UMV_ACCESS;
  }

  //////allocate memory for search state//////////////////////////
  memset(McostState[0],0,(2*input->search_range+1)*(2*input->search_range+1));


  //check the center median predictor
  cand_x = center_x ;
  cand_y = center_y ;
  mcost = MV_COST (lambda_factor, mvshift, cand_x, cand_y, pred_x, pred_y);

  mcost += computeUniPred[dist_method](orig_pic, blocksize_y,blocksize_x, min_mcost - mcost,
    (cand_x << 2) + IMG_PAD_SIZE_TIMES4, (cand_y << 2) + IMG_PAD_SIZE_TIMES4);

  McostState[search_range][search_range] = 1;
  if (mcost < min_mcost)
  {
    min_mcost = mcost;
    best_x    = cand_x;
    best_y    = cand_y;
  }

  iXMinNow = best_x;
  iYMinNow = best_y;
  for (m = 0; m < 4; m++)
  {
    cand_x = iXMinNow + Diamond_x[m];
    cand_y = iYMinNow + Diamond_y[m];
    SEARCH_ONE_PIXEL
  }

  if(center_x != pic_pix_x || center_y != pic_pix_y)
  {
    cand_x = pic_pix_x ;
    cand_y = pic_pix_y ;
    SEARCH_ONE_PIXEL

    iXMinNow = best_x;
    iYMinNow = best_y;
    for (m = 0; m < 4; m++)
    {
      cand_x = iXMinNow + Diamond_x[m];
      cand_y = iYMinNow + Diamond_y[m];
      SEARCH_ONE_PIXEL
    }
  }
  /***********************************init process*************************/
  //for multi ref
  if(ref>0 && img->structure == FRAME  && min_mcost > ET_Thred && SAD_prediction[pic_pix_x2]<Multi_Ref_Thd_MB[blocktype])
    goto terminate_step;

  //ET_Thd1: early termination for low motion case
  if( min_mcost < ET_Thred)
  {
    goto terminate_step;
  }
  else // hybrid search for main search loop
  {
    /****************************(MV and SAD prediction)********************************/
    UMHEX_setup(ref, list, block_y, block_x, blocktype, img->all_mv );
    ET_Thred = Big_Hexagon_Thd_MB[blocktype];  // ET_Thd2: early termination Threshold for strong motion



    // Threshold defined for EARLY_TERMINATION
    if (pred_SAD == 0)
    {
      betaFourth_1=0;
      betaFourth_2=0;
    }
    else
    {

⌨️ 快捷键说明

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