me_epzs.c

来自「the newest JM software by h.264 JVT offi」· C语言 代码 · 共 1,639 行 · 第 1/5 页

C
1,639 行
字号
        ? rshift_rnd_sf((mot_scale[refB] * tmp_mv[block_b->pos_y][block_b->pos_x][1]), sp_shift_mv - 1)
        : rshift_rnd_sf((mot_scale[refB] * tmp_mv[block_b->pos_y][block_b->pos_x][1]), sp_shift_mv) :  fixed_mv;

      // Up-Right predictor
      point[3].motion.mv_x = (block_c->available)
        ? rshift_rnd_sf((mot_scale[refC] * tmp_mv[block_c->pos_y][block_c->pos_x][0]), sp_shift_mv) : -fixed_mv;
      point[3].motion.mv_y = (block_c->available)
        ? img->mb_data[block_c->mb_addr].mb_field
        ? rshift_rnd_sf((mot_scale[refC] * tmp_mv[block_c->pos_y][block_c->pos_x][1]), sp_shift_mv - 1)
        : rshift_rnd_sf((mot_scale[refC] * tmp_mv[block_c->pos_y][block_c->pos_x][1]), sp_shift_mv) : 0;

      //Up-Left predictor
      point[4].motion.mv_x = (block_d->available)
        ? rshift_rnd_sf((mot_scale[refD] * tmp_mv[block_d->pos_y][block_d->pos_x][0]), sp_shift_mv) : 0;
      point[4].motion.mv_y = (block_d->available)
        ? img->mb_data[block_d->mb_addr].mb_field
        ? rshift_rnd_sf((mot_scale[refD] * tmp_mv[block_d->pos_y][block_d->pos_x][1]), sp_shift_mv - 1)
        : rshift_rnd_sf((mot_scale[refD] * tmp_mv[block_d->pos_y][block_d->pos_x][1]), sp_shift_mv) : -fixed_mv;
    }
  }

  return ((refA == -1) + (refB == -1) + (refC == -1 && refD == -1));
}

/*!
***********************************************************************
* \brief
*    Spatial Predictors
*    AMT/HYC
***********************************************************************
*/

static void EPZSSpatialMemPredictors (int list,
                                      short ref,
                                      int blocktype,
                                      int pic_x,
                                      int bs_x,
                                      int bs_y,
                                      int by,
                                      int *prednum,
                                      int img_width,
                                      SPoint *point)
{
#if EPZSREF
  MotionVector **prd_mv = EPZS_Motion[list][ref][blocktype];
  MotionVector *cur_mv  = &point[*prednum].motion;

  // Left Predictor
  if (pic_x > 0)
  {
    *cur_mv = prd_mv[by][pic_x - bs_x];
    *prednum += (*((int*) cur_mv) != 0);
    cur_mv = &point[*prednum].motion;
  }
  
  by = (by > 0) ? by - bs_y: 4 - bs_y;
  // Up predictor
  *cur_mv = prd_mv[by][pic_x];
  *prednum += (*((int*) cur_mv) != 0);

  // Up-Right predictor
  if (pic_x + bs_x < img_width)
  {
    cur_mv = &point[*prednum].motion;
    *cur_mv = prd_mv[by][pic_x + bs_x];
    *prednum += (*((int*) cur_mv) != 0);
  }

#else
  int mot_scale = mv_scale[list][ref][0];
  MotionVector **prd_mv = EPZS_Motion[list][blocktype];

  // Left Predictor
  point[*prednum].motion.mv_x = (pic_x > 0)
    ? rshift_rnd_sf((mot_scale * prd_mv[by][pic_x - bs_x].mv_x), 8)
    : 0;
  point[*prednum].motion.mv_y = (pic_x > 0)
    ? rshift_rnd_sf((mot_scale * prd_mv[by][pic_x - bs_x].mv_y), 8)
    : 0;
  *prednum += ((point[*prednum].motion.mv_x != 0) || (point[*prednum].motion.mv_y != 0));

  // Up predictor
  point[*prednum].motion.mv_x = (by > 0)
    ? rshift_rnd_sf((mot_scale * prd_mv[by - bs_y][pic_x].mv_x), 8)
    : rshift_rnd_sf((mot_scale * prd_mv[4  - bs_y][pic_x].mv_x), 8);
  point[*prednum].motion.mv_y = (by > 0)
    ? rshift_rnd_sf((mot_scale * prd_mv[by - bs_y][pic_x].mv_y), 8)
    : rshift_rnd_sf((mot_scale * prd_mv[4  - bs_y][pic_x].mv_y), 8);
  *prednum += ((point[*prednum].motion.mv_x != 0) || (point[*prednum].motion.mv_y != 0));

  // Up-Right predictor
  point[*prednum].motion.mv_x = (pic_x + bs_x < img_width)
    ? (by > 0)
    ? rshift_rnd_sf((mot_scale * prd_mv[by - bs_y][pic_x + bs_x].mv_x), 8)
    : rshift_rnd_sf((mot_scale * prd_mv[4  - bs_y][pic_x + bs_x].mv_x), 8)
    : 0;
  point[*prednum].motion.mv_y = (pic_x + bs_x < img_width)
    ? (by > 0)
    ? rshift_rnd_sf((mot_scale * prd_mv[by - bs_y][pic_x + bs_x].mv_y), 8)
    : rshift_rnd_sf((mot_scale * prd_mv[4  - bs_y][pic_x + bs_x].mv_y), 8)
    : 0;
  *prednum += ((point[*prednum].motion.mv_x != 0) || (point[*prednum].motion.mv_y != 0));
#endif
}


/*!
***********************************************************************
* \brief
*    Temporal Predictors
*    AMT/HYC
***********************************************************************
*/
static void
EPZSTemporalPredictors (int list,         // <--  current list
                        int list_offset,  // <--  list offset for MBAFF
                        short ref,        // <--  current reference frame
                        int o_block_x,    // <--  absolute x-coordinate of regarded AxB block
                        int o_block_y,    // <--  absolute y-coordinate of regarded AxB block
                        SPoint *point,
                        int *prednum,
                        int block_available_left,
                        int block_available_up,
                        int block_available_right,
                        int block_available_below,
                        int blockshape_x,
                        int blockshape_y,
                        int stopCriterion,
                        int min_mcost)
{
  int mvScale = mv_scale[list + list_offset][ref][0];
  MotionVector **col_mv = (list_offset == 0) ? EPZSCo_located->frame[list]  
    : (list_offset == 2) ? EPZSCo_located->top[list] : EPZSCo_located->bot[list];
  short temp_shift_mv = 8 + mv_rescale; // 16 - invmv_precision + mv_rescale  
  MotionVector *cur_mv = &point[*prednum].motion;

  *prednum += add_predictor(cur_mv, col_mv[o_block_y][o_block_x], mvScale, temp_shift_mv);

  if (min_mcost > stopCriterion && ref < 2)
  {
    if (block_available_left)
    {
      *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y][o_block_x - 1], mvScale, temp_shift_mv);

      //Up_Left
      if (block_available_up)
      {
        *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y - 1][o_block_x - 1], mvScale, temp_shift_mv);
      }
      //Down_Left
      if (block_available_below)
      {
        *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y + blockshape_y][o_block_x - 1], mvScale, temp_shift_mv);
      }
    }
    // Up
    if (block_available_up)
    {
      *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y - 1][o_block_x], mvScale, temp_shift_mv);
    }

    // Up - Right
    if (block_available_right)
    {
      *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y][o_block_x + blockshape_x], mvScale, temp_shift_mv);

      if (block_available_up)
      {
        *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y - 1][o_block_x + blockshape_x], mvScale, temp_shift_mv);
      }
      if (block_available_below)
      {
        *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y + blockshape_y][o_block_x + blockshape_x], mvScale, temp_shift_mv);
      }
    }

    if (block_available_below)
    {
      *prednum += add_predictor( &point[*prednum].motion, col_mv[o_block_y + blockshape_y][o_block_x], mvScale, temp_shift_mv);
    }
  }
}

/*!
************************************************************************
* \brief
*    EPZS Block Type Predictors
************************************************************************
*/
static void EPZSBlockTypePredictors (int block_x, int block_y, int blocktype, int ref, int list,
                                     SPoint *point, int *prednum)
{
  short *****all_mv = img->all_mv[list];
  short block_shift_mv = 8 + mv_rescale;
  MotionVector *cur_mv = &point[*prednum].motion;

  cur_mv->mv_x = rshift_rnd((all_mv[ref][blk_parent[blocktype]][block_y][block_x][0]), mv_rescale);
  cur_mv->mv_y = rshift_rnd((all_mv[ref][blk_parent[blocktype]][block_y][block_x][1]), mv_rescale);
  //*prednum += ((cur_mv->mv_x | cur_mv->mv_y) != 0);
  *prednum += (*((int*) cur_mv) != 0);

  if ((ref > 0) && (blocktype < 5 || img->structure != FRAME))
  {
    cur_mv = &point[*prednum].motion;
    cur_mv->mv_x = rshift_rnd_sf((mv_scale[list][ref][ref-1] * all_mv[ref-1][blocktype][block_y][block_x][0]), block_shift_mv );
    cur_mv->mv_y = rshift_rnd_sf((mv_scale[list][ref][ref-1] * all_mv[ref-1][blocktype][block_y][block_x][1]), block_shift_mv );
    //*prednum += ((cur_mv->mv_x | cur_mv->mv_y) != 0);
    *prednum += (*((int*) cur_mv) != 0);

    cur_mv = &point[*prednum].motion;
    cur_mv->mv_x = rshift_rnd_sf((mv_scale[list][ref][0] * all_mv[0][blocktype][block_y][block_x][0]), block_shift_mv );
    cur_mv->mv_y = rshift_rnd_sf((mv_scale[list][ref][0] * all_mv[0][blocktype][block_y][block_x][1]), block_shift_mv );
    //*prednum += ((cur_mv->mv_x | cur_mv->mv_y) != 0);
    *prednum += (*((int*) cur_mv) != 0);
  }

  if (blocktype != 1)
  {
    cur_mv = &point[*prednum].motion;
    cur_mv->mv_x = rshift_rnd((all_mv[ref][1][block_y][block_x][0]), mv_rescale);
    cur_mv->mv_y = rshift_rnd((all_mv[ref][1][block_y][block_x][1]), mv_rescale);
    //*prednum += ((cur_mv->mv_x | cur_mv->mv_y) != 0);
    *prednum += (*((int*) cur_mv) != 0);
  }

  if (blocktype != 4)
  {
    cur_mv = &point[*prednum].motion;
    cur_mv->mv_x = rshift_rnd((all_mv[ref][4][block_y][block_x][0]), mv_rescale);
    cur_mv->mv_y = rshift_rnd((all_mv[ref][4][block_y][block_x][1]), mv_rescale);
    //*prednum += ((cur_mv->mv_x | cur_mv->mv_y) != 0);
    *prednum += (*((int*) cur_mv) != 0);
  }
}

/*!
************************************************************************
* \brief
*    EPZS Window Based Predictors
************************************************************************
*/
static void EPZSWindowPredictors (MotionVector *mv, EPZSStructure *predictor, int *prednum, int extended)
{
  int pos;
  EPZSStructure *windowPred = (extended) ? window_predictor_extended : window_predictor;
  SPoint *wPoint = &windowPred->point[0];
  SPoint *pPoint = &predictor->point[(*prednum)];

  for (pos = 0; pos < windowPred->searchPoints; pos++)
  {
    (pPoint  )->motion.mv_x = mv->mv_x + (wPoint  )->motion.mv_x;
    (pPoint++)->motion.mv_y = mv->mv_y + (wPoint++)->motion.mv_y;    
  }
  *prednum += windowPred->searchPoints;
}

/*!
***********************************************************************
* \brief
*    FAST Motion Estimation using EPZS
*    AMT/HYC
***********************************************************************
*/
int                                           //  ==> minimum motion cost after search
EPZSPelBlockMotionSearch (Macroblock *currMB, // <--  current Macroblock
                          imgpel * cur_pic,   // <--  original pixel values for the AxB block
                          short ref,          // <--  reference picture
                          int list,           // <--  reference list
                          char ***refPic,     // <--  reference array
                          short ****tmp_mv,   // <--  mv array
                          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)
                          MotionVector *pred_mv,   // <--  motion vector predictor in sub-pel units
                          MotionVector *mv,        // <--> in: search center (x|y) / out: motion vector (x|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 apply_weights)  // <--  perform weight based ME
{
  int cur_list = list + currMB->list_offset;
  StorablePicture *ref_picture = listX[cur_list][ref];

  short blocksize_y  = params->blc_size[blocktype][1];  // vertical block size
  short blocksize_x  = params->blc_size[blocktype][0];  // horizontal block size
  short blockshape_x = (blocksize_x >> 2);  // horizontal block size in 4-pel units
  short blockshape_y = (blocksize_y >> 2);  // vertical block size in 4-pel units

  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 pic_pix_y2 = pic_pix_y >> 2;
  short block_x = (mb_x >> 2);
  short block_y = (mb_y >> 2);

  int   stopCriterion = medthres[blocktype];
  int   mapCenter_x = search_range - mv->mv_x;
  int   mapCenter_y = search_range - mv->mv_y;
  int   second_mcost = INT_MAX;
  int   *prevSad = EPZSDistortion[cur_list][blocktype - 1];
  MotionVector *p_motion = NULL;
  int   mv_range = (2<<(2-mv_rescale));

  short invalid_refs = 0;
  byte  checkMedian = FALSE;
  EPZSStructure *searchPatternF = searchPattern;

  MotionVector center, tmp = *mv, tmp2 = {0, 0}, cand, pred;
  pred.mv_x = (pic_pix_x << 2) + pred_mv->mv_x;  // predicted position x (in sub-pel units)
  pred.mv_y = (pic_pix_y << 2) + pred_mv->mv_y;  // predicted position y (in sub-pel units)
  center.mv_x = (pic_pix_x << (params->EPZSGrid))+ mv->mv_x;
  center.mv_y = (pic_pix_y << (params->EPZSGrid))+ mv->mv_y;
  cand.mv_x = center.mv_x << mv_rescale;
  cand.mv_y = center.mv_y << mv_rescale;
  
  computePred = computeUniPred[F_PEL + 3 * apply_weights];

  EPZSBlkCount ++;
  if (EPZSBlkCount == 0)
    EPZSBlkCount ++;

  ref_pic_sub.luma = ref_picture->p_curr_img_sub;

  img_width  = ref_picture->size_x;
  img_height = ref_picture->size_y;
  width_pad  = ref_picture->size_x_pad;
  height_pad = ref_picture->

⌨️ 快捷键说明

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