mv-search.c

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

C
1,857
字号
int BIDPartitionCost (Macroblock *currMB, 
                      int   blocktype,
                      int   block8x8,
                      char  cur_ref[2],
                      int   lambda_factor)
{
  static int  bx0[5][4] = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,2,0,0}, {0,2,0,2}};
  static int  by0[5][4] = {{0,0,0,0}, {0,0,0,0}, {0,2,0,0}, {0,0,0,0}, {0,0,2,2}};

  int   curr_blk[MB_BLOCK_SIZE][MB_BLOCK_SIZE]; // ABT pred.error buffer
  int   bsx       = imin(params->blc_size[blocktype][0],8);
  int   bsy       = imin(params->blc_size[blocktype][1],8);

  int   pic_pix_x, pic_pix_y, block_x, block_y;
  int   v, h, mcost, i, j, k;
  int   mvd_bits  = 0;
  int   parttype  = (blocktype<4?blocktype:4);
  int   step_h0   = (params->part_size[ parttype][0]);
  int   step_v0   = (params->part_size[ parttype][1]);
  int   step_h    = (params->part_size[blocktype][0]);
  int   step_v    = (params->part_size[blocktype][1]);
  int   bxx, byy;                               // indexing curr_blk
  int   bx = bx0[parttype][block8x8];
  int   by = by0[parttype][block8x8];
  short   *** all_mv_l0 = img->all_mv [LIST_0][(int) cur_ref[LIST_0]][blocktype];
  short   *** all_mv_l1 = img->all_mv [LIST_1][(int) cur_ref[LIST_1]][blocktype];
  short bipred_me =  0; //no bipred for this case 
  imgpel  **mb_pred = img->mb_pred[0];

  //----- cost for motion vector bits -----
  // Should write a separate, small function to do this processing
  // List0 
  // mvd_bits = mv_bits_cost(all_mv_l0, p_mv_l0, by, bx, step_v0, step_v, step_h0, step_h, mvd_bits);
  mvd_bits = mv_bit_cost(currMB, all_mv_l0, LIST_0, cur_ref[LIST_0], by, bx, step_v0, step_v, step_h0, step_h, mvd_bits);
  // List1
  // mvd_bits = mv_bits_cost(all_mv_l1, p_mv_l1, by, bx, step_v0, step_v, step_h0, step_h, mvd_bits);
  mvd_bits = mv_bit_cost(currMB, all_mv_l1, LIST_1, cur_ref[LIST_1], by, bx, step_v0, step_v, step_h0, step_h, mvd_bits);

  mcost = WEIGHTED_COST (lambda_factor, mvd_bits);

  //----- cost of residual signal -----
  for (byy=0, v=by; v<by + step_v0; byy+=4, v++)
  {
    pic_pix_y = img->opix_y + (block_y = (v<<2));
    for (bxx=0, h=bx; h<bx + step_h0; bxx+=4, h++)
    {
      pic_pix_x = img->opix_x + (block_x = (h<<2));
      LumaPrediction (currMB, block_x, block_y, 4, 4, 2, blocktype, blocktype, (short) cur_ref[0], (short) cur_ref[1], bipred_me);

      for (k=j=0; j<4; j++)
      {
        for (  i=0; i<4; i++)
          diff64[k++] = curr_blk[byy+j][bxx+i] =
          pCurImg[pic_pix_y+j][pic_pix_x+i] - mb_pred[j+block_y][i+block_x];
      }
      if ((!params->Transform8x8Mode) || (blocktype>4))
        mcost += distortion4x4 (diff64);
    }
  }

  if (params->Transform8x8Mode && (blocktype<=4))  // tchen 4-29-04
  {
    for (byy=0; byy < params->blc_size[parttype][1]; byy+=bsy)
    {
      for (bxx=0; bxx<params->blc_size[parttype][0]; bxx+=bsx)
      {
        for (k=0, j=byy;j<byy + 8;j++, k += 8)
          memcpy(&diff64[k], &(curr_blk[j][bxx]), 8 * sizeof(int));

        mcost += distortion8x8(diff64);
      }
    }
  }
  return mcost;
}

/*!
 ************************************************************************
 * \brief
 *    Get cost for skip mode for an macroblock
 ************************************************************************
 */
int GetSkipCostMB (Macroblock *currMB)
{
  int block_y, block_x, pic_pix_y, pic_pix_x, i, j, k;
  int cost = 0;

  int curr_diff[8][8];
  int mb_x, mb_y;
  int block;
  imgpel  **mb_pred = img->mb_pred[0];

  for(block = 0;block < 4;block++)
  {
    mb_y    = (block >>   1)<<3;
    mb_x    = (block & 0x01)<<3;
    for (block_y = mb_y; block_y < mb_y+8; block_y += 4)
    {
      pic_pix_y = img->opix_y + block_y;
      for (block_x = mb_x; block_x < mb_x + 8; block_x += 4)
      {
        pic_pix_x = img->opix_x + block_x;

        //===== prediction of 4x4 block =====
        LumaPrediction (currMB, block_x, block_y, 4, 4, 0, 0, 0, 0, 0, 0);

        //===== get displaced frame difference ======
        for (k = j = 0; j < 4; j++)
        {
          for (i = 0; i < 4; i++, k++)
          {
            diff[k] = curr_diff[block_y-mb_y+j][block_x-mb_x+i] = pCurImg[pic_pix_y+j][pic_pix_x+i] - mb_pred[j+block_y][i+block_x];
          }
        }

        if(!((params->rdopt==0) && (params->Transform8x8Mode)))
          cost += distortion4x4 (diff);
      }
    }

    if((params->rdopt == 0) && (params->Transform8x8Mode))
    {
      for(k=j=0; j<8; j++, k+=8)
        memcpy(&diff64[k], &(curr_diff[j]), 8 * sizeof(int));
      cost += distortion8x8 (diff64);
    }
  }

  return cost;
}

/*!
 ************************************************************************
 * \brief
 *    Find motion vector for the Skip mode
 ************************************************************************
 */
void FindSkipModeMotionVector (Macroblock *currMB)
{
  int   bx, by;
  short ***all_mv = img->all_mv[0][0][0];

  short pmv[2];

  int zeroMotionAbove;
  int zeroMotionLeft;
  PixelPos mb_a, mb_b, mb_c, mb_d;
  int      a_mv_y = 0;
  int      a_ref_idx = 0;
  int      b_mv_y = 0;
  int      b_ref_idx = 0;
  short    ***mv = enc_picture->motion.mv[LIST_0];

  get_neighbors(currMB, &mb_a, &mb_b, &mb_c, &mb_d, 0, 0, 16);

  if (mb_a.available)
  {
    a_mv_y    = mv[mb_a.pos_y][mb_a.pos_x][1];
    a_ref_idx = enc_picture->motion.ref_idx[LIST_0][mb_a.pos_y][mb_a.pos_x];

    if (currMB->mb_field && !img->mb_data[mb_a.mb_addr].mb_field)
    {
      a_mv_y    /=2;
      a_ref_idx *=2;
    }
    if (!currMB->mb_field && img->mb_data[mb_a.mb_addr].mb_field)
    {
      a_mv_y    *= 2;
      a_ref_idx >>=1;
    }
  }

  if (mb_b.available)
  {
    b_mv_y    = mv[mb_b.pos_y][mb_b.pos_x][1];
    b_ref_idx = enc_picture->motion.ref_idx[LIST_0][mb_b.pos_y][mb_b.pos_x];

    if (currMB->mb_field && !img->mb_data[mb_b.mb_addr].mb_field)
    {
      b_mv_y    /=2;
      b_ref_idx *=2;
    }
    if (!currMB->mb_field && img->mb_data[mb_b.mb_addr].mb_field)
    {
      b_mv_y    *=2;
      b_ref_idx >>=1;
    }
  }

  zeroMotionLeft  = !mb_a.available ? 1 : a_ref_idx==0 && mv[mb_a.pos_y][mb_a.pos_x][0]==0 && a_mv_y==0 ? 1 : 0;
  zeroMotionAbove = !mb_b.available ? 1 : b_ref_idx==0 && mv[mb_b.pos_y][mb_b.pos_x][0]==0 && b_mv_y==0 ? 1 : 0;

  if (zeroMotionAbove || zeroMotionLeft)
  {
    memset(all_mv [0][0], 0, 4 * 4 * 2* sizeof(short));
  }
  else
  {
    GetMotionVectorPredictor (currMB, &mb_a, &mb_b, &mb_c, pmv, enc_picture->motion.ref_idx[LIST_0], mv, 0, 0, 0, 16, 16);

    for (bx = 0;bx < 4;bx++)
    {
      memcpy(all_mv [0][bx], pmv, 2* sizeof(short));
    }

    for (by = 1;by < 4;by++)
      memcpy(all_mv [by][0], all_mv [0][0], 4 * 2* sizeof(short));
  }
}

/*!
 ************************************************************************
 * \brief
 *    Get cost for direct mode for an 8x8 block
 ************************************************************************
 */
int GetDirectCost8x8 (Macroblock *currMB, int block, int *cost8x8)
{
  int block_y, block_x, pic_pix_y, pic_pix_x, i, j, k;
  int curr_diff[8][8];
  int cost  = 0;
  int mb_y  = (block/2)<<3;
  int mb_x  = (block%2)<<3;
  short bipred_me  = 0;
  imgpel  **mb_pred = img->mb_pred[0];

  for (block_y=mb_y; block_y<mb_y+8; block_y+=4)
  {
    pic_pix_y = img->opix_y + block_y;

    for (block_x=mb_x; block_x<mb_x+8; block_x+=4)
    {
      pic_pix_x = img->opix_x + block_x;

      if (direct_pdir[pic_pix_y>>2][pic_pix_x>>2]<0)
      {
        *cost8x8=INT_MAX;
        return INT_MAX; //mode not allowed
      }

      //===== prediction of 4x4 block =====

      LumaPrediction (currMB, block_x, block_y, 4, 4,
        direct_pdir[pic_pix_y>>2][pic_pix_x>>2], 0, 0,
        direct_ref_idx[LIST_0][pic_pix_y>>2][pic_pix_x>>2],
        direct_ref_idx[LIST_1][pic_pix_y>>2][pic_pix_x>>2], bipred_me);

      //===== get displaced frame difference ======
      for (k=j=0; j<4; j++)
        for (i=0; i<4; i++, k++)
        {
          diff[k] = curr_diff[block_y-mb_y+j][block_x-mb_x+i] =
            pCurImg[pic_pix_y+j][pic_pix_x+i] - mb_pred[j+block_y][i+block_x];
        }

        cost += distortion4x4 (diff);
    }
  }

  if((params->rdopt == 0) && (params->Transform8x8Mode))
  {
    k=0;
    for(j=0; j<8; j++, k+=8)
      memcpy(&diff64[k], &(curr_diff[j]), 8 * sizeof(int));          

    *cost8x8 += distortion8x8 (diff64);
  }

  return cost;
}



/*!
 ************************************************************************
 * \brief
 *    Get cost for direct mode for an macroblock
 ************************************************************************
 */
int GetDirectCostMB (Macroblock *currMB, int bslice)
{
  int i;
  int cost = 0;
  int cost8x8 = 0;

  for (i=0; i<4; i++)
  {
    cost += GetDirectCost8x8 (currMB, i, &cost8x8);
    if (cost8x8 == INT_MAX) return INT_MAX;
  }

  switch(params->Transform8x8Mode)
  {
  case 1: // Mixture of 8x8 & 4x4 transform
    if((cost8x8 < cost)||
      !(params->InterSearch[bslice][5] &&
      params->InterSearch[bslice][6] &&
      params->InterSearch[bslice][7])
      )
    {
      cost = cost8x8; //return 8x8 cost
    }
    break;
  case 2: // 8x8 Transform only
    cost = cost8x8;
    break;
  default: // 4x4 Transform only
    break;
  }

  return cost;
}


/*!
 ************************************************************************
 * \brief
 *    Motion search for a partition
 ************************************************************************
 */
void
PartitionMotionSearch (Macroblock *currMB,
                       int    blocktype,
                       int    block8x8,
                       int    *lambda_factor)
{
  static int  bx0[5][4] = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,2,0,0}, {0,2,0,2}};
  static int  by0[5][4] = {{0,0,0,0}, {0,0,0,0}, {0,2,0,0}, {0,0,0,0}, {0,0,2,2}};

  char  **ref_array;
  short ***mv_array;
  short *all_mv;
  short ref;
  int   v, h, mcost, search_range, i, j;
  int   pic_block_x, pic_block_y;
  int   bslice    = (img->type==B_SLICE);
  int   parttype  = (blocktype < 4 ? blocktype : 4);
  int   step_h0   = (params->part_size[ parttype][0]);
  int   step_v0   = (params->part_size[ parttype][1]);
  int   step_h    = (params->part_size[blocktype][0]);
  int   step_v    = (params->part_size[blocktype][1]);
  int   list;
  int   numlists  = bslice ? 2 : 1;
  int   list_offset = currMB->list_offset;
  int   *m_cost;
  int   by = by0[parttype][block8x8];
  int   bx = bx0[parttype][block8x8];

#if GET_METIME
  static TIME_T me_time_start;
  static TIME_T me_time_end;
  int64 me_tmp_time;
  gettime( &me_time_start );    // start time ms
#endif
  //===== LOOP OVER REFERENCE FRAMES =====
  for (list=0; list<numlists;list++)
  {
    //----- set arrays -----
    ref_array = enc_picture->motion.ref_idx[list];
    mv_array  = enc_picture->motion.mv[list];

    for (ref=0; ref < listXsize[list+list_offset]; ref++)
    {
      m_cost = &motion_cost[blocktype][list][ref][block8x8];
      //----- set search range ---
      if      (params->full_search == 2)
        search_range = params->search_range;
      else if (params->full_search == 1)
        search_range = params->search_range /  (imin(ref,1)+1);
      else
        search_range = params->search_range / ((imin(ref,1)+1) * imin(2,blocktype));

⌨️ 快捷键说明

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