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

📄 mv-search.c

📁 H.264标准代码(用于视频编码)
💻 C
📖 第 1 页 / 共 5 页
字号:
  //==================================
#ifndef _FAST_FULL_ME_

  //--- set search center ---
  mv_x = pred_mv_x / 4;
  mv_y = pred_mv_y / 4;
  if (!input->rdopt)
  {
    //--- adjust search center so that the (0,0)-vector is inside ---
    mv_x = max (-search_range, min (search_range, mv_x));
    mv_y = max (-search_range, min (search_range, mv_y));
  }

  //--- perform motion search ---
  min_mcost = FullPelBlockMotionSearch     (orig_pic, ref, pic_pix_x, pic_pix_y, blocktype,
                                            pred_mv_x, pred_mv_y, &mv_x, &mv_y, search_range,
                                            min_mcost, lambda);

#else

  // comments:   - orig_pic is not used  -> be careful
  //             - search center is automatically determined
  min_mcost = FastFullPelBlockMotionSearch (orig_pic, ref, list, pic_pix_x, pic_pix_y, blocktype,
                                            pred_mv_x, pred_mv_y, &mv_x, &mv_y, search_range,
                                            min_mcost, lambda);

#endif

  //==============================
  //=====   SUB-PEL SEARCH   =====
  //==============================
  if (input->hadamard)
  {
    min_mcost = max_value;
  }
  min_mcost =  SubPelBlockMotionSearch (orig_pic, ref, list, pic_pix_x, pic_pix_y, blocktype,
                                        pred_mv_x, pred_mv_y, &mv_x, &mv_y, 9, 9,
                                        min_mcost, lambda);


  if (!input->rdopt)
  {
    // Get the skip mode cost
    if (blocktype == 1 && (img->type == P_SLICE||img->type == SP_SLICE))
    {
      int cost;

      FindSkipModeMotionVector ();

      cost  = GetSkipCostMB (lambda);
      cost -= (int)floor(8*lambda+0.4999);

      if (cost < min_mcost)
      {
        min_mcost = cost;
        mv_x      = img->all_mv [0][0][0][0][0];
        mv_y      = img->all_mv [0][0][0][0][1];
      }
    }
  }

  //===============================================
  //=====   SET MV'S AND RETURN MOTION COST   =====
  //===============================================
  for (i=0; i < (bsx>>2); i++)
  {
    for (j=0; j < (bsy>>2); j++)
    {
      all_mv[block_x+i][block_y+j][refframe][blocktype][0] = mv_x;
      all_mv[block_x+i][block_y+j][refframe][blocktype][1] = mv_y;
    }
  }
  //if (img->type==BS_IMG)
	if ( img->type==B_SLICE && img->nal_reference_idc>0)
  {
    for (i=0; i < (bsx>>2); i++)
    for (j=0; j < (bsy>>2); j++)
    {
      //  Backward
      all_bmv[block_x+i][block_y+j][ref][blocktype][0] = mv_x;
      all_bmv[block_x+i][block_y+j][ref][blocktype][1] = mv_y;
    }
  }
  return min_mcost;
}


/*!
 ***********************************************************************
 * \brief
 *    Motion Cost for Bidirectional modes
 ***********************************************************************
 */
int
BIDPartitionCost (int   blocktype,
                  int   block8x8,
                  int   fw_ref,
                  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   diff[16];
  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   = (input->blc_size[ parttype][0]>>2);
  int   step_v0   = (input->blc_size[ parttype][1]>>2);
  int   step_h    = (input->blc_size[blocktype][0]>>2);
  int   step_v    = (input->blc_size[blocktype][1]>>2);
  int   bxx, byy;                               // indexing curr_blk
  byte** imgY_original  = imgY_org;
  int    pix_y    =   img->pix_y;
  int    *****all_mv = img->all_mv;
  int   *****all_bmv = img->all_bmv;
  int   *****p_fwMV  = img->p_fwMV;
  int   *****p_bwMV  = img->p_bwMV;

  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    pix_y     = img->field_pix_y;
    if(img->top_field)
    {
      imgY_original = imgY_org_top;
      all_mv = img->all_mv_top;
      all_bmv = img->all_bmv_top;
      p_fwMV  = img->p_fwMV_top;
      p_bwMV   = img->p_bwMV_top;
    }
    else
    {
      imgY_original = imgY_org_bot;
      all_mv = img->all_mv_bot;
      all_bmv = img->all_bmv_bot;
      p_fwMV  = img->p_fwMV_bot;
      p_bwMV   = img->p_bwMV_bot;

    }
  }

  //----- cost for motion vector bits -----
  for (v=by0[parttype][block8x8]; v<by0[parttype][block8x8]+step_v0; v+=step_v)
  for (h=bx0[parttype][block8x8]; h<bx0[parttype][block8x8]+step_h0; h+=step_h)
  {
    mvd_bits += mvbits[ all_mv [h][v][fw_ref][blocktype][0] - p_fwMV[h][v][fw_ref][blocktype][0] ];
    mvd_bits += mvbits[ all_mv [h][v][fw_ref][blocktype][1] - p_fwMV[h][v][fw_ref][blocktype][1] ];
    mvd_bits += mvbits[ all_bmv[h][v][     0][blocktype][0] - p_bwMV[h][v][     0][blocktype][0] ];
    mvd_bits += mvbits[ all_bmv[h][v][     0][blocktype][1] - p_bwMV[h][v][     0][blocktype][1] ];
  }
  mcost = WEIGHTED_COST (lambda_factor, mvd_bits);

  //----- cost of residual signal -----
  for (byy=0, v=by0[parttype][block8x8]; v<by0[parttype][block8x8]+step_v0; byy+=4, v++)
  {
    pic_pix_y = pix_y + (block_y = (v<<2));

    for (bxx=0, h=bx0[parttype][block8x8]; h<bx0[parttype][block8x8]+step_h0; bxx+=4, h++)
    {
      pic_pix_x = img->pix_x + (block_x = (h<<2));

      LumaPrediction4x4 (block_x, block_y, blocktype, blocktype, fw_ref, 0);

      for (k=j=0; j<4; j++)
      for (  i=0; i<4; i++, k++)
      {
        diff[k] = imgY_original[pic_pix_y+j][pic_pix_x+i] - img->mpr[i+block_x][j+block_y];
      }
      mcost += SATD (diff, input->hadamard);
    }
  }
  return mcost;
}





/*!
 ***********************************************************************
 * \brief
 *    Motion Cost for ABidirectional modes
 ***********************************************************************
 */
int
ABIDPartitionCost (int   blocktype,
                   int   block8x8,
                   int*  fw_ref,
                   int*  bw_ref,
                   int   lambda_factor,
                   int*  abp_type )
{
  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   diff[16];
  int   pic_pix_x, pic_pix_y, block_x, block_y;
  int   v, h, mcost = INT_MAX, mcost0, i, j, k;
  int   mvd_bits  = 0;
  int   parttype  = (blocktype<4?blocktype:4);
  int   step_h0   = (input->blc_size[ parttype][0]>>2);
  int   step_v0   = (input->blc_size[ parttype][1]>>2);
  int   step_h    = (input->blc_size[blocktype][0]>>2);
  int   step_v    = (input->blc_size[blocktype][1]>>2);
  int   bxx, byy;                               // indexing curr_blk
  byte** imgY_original  = imgY_org;
  int    pix_y    =   img->pix_y;
  int    *****all_mv = img->all_mv;
  int   *****all_bmv = img->all_bmv;
  int   *****abp_all_dmv = img->abp_all_dmv;
  int   *****p_fwMV  = img->p_fwMV;
  int   *****p_bwMV  = img->p_bwMV;
  int   mv_scale;

#if 0
  int mcost1;
#endif

  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    pix_y     = img->field_pix_y;
    if(img->top_field)
    {
      imgY_original = imgY_org_top;
      all_mv = img->all_mv_top;
      all_bmv = img->all_bmv_top;
      p_fwMV  = img->p_fwMV_top;
      p_bwMV   = img->p_bwMV_top;
      abp_all_dmv = img->abp_all_dmv_top;
    }
    else
    {
      imgY_original = imgY_org_bot;
      all_mv = img->all_mv_bot;
      all_bmv = img->all_bmv_bot;
      p_fwMV  = img->p_fwMV_bot;
      p_bwMV   = img->p_bwMV_bot;
      abp_all_dmv = img->abp_all_dmv_bot;

    }
  }


  //if ( img->type==BS_IMG)
	if ( img->type==B_SLICE && img->nal_reference_idc>0)
  {
    //
    //  Interpolation (1/2,1/2,0)
    //
    //----- cost for motion vector bits -----
    if ((input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode) || input->InterlaceCodingOption == FIELD_CODING) 
    {
      *fw_ref = 3;
      *bw_ref = 1;
    }
    else
    {
      *fw_ref = 1;
      *bw_ref = 0;
    }
    mvd_bits = 0;
    mv_scale = 256*((*bw_ref)+1)/((*fw_ref)+1);
    for (v=by0[parttype][block8x8]; v<by0[parttype][block8x8]+step_v0; v+=step_v)
    for (h=bx0[parttype][block8x8]; h<bx0[parttype][block8x8]+step_h0; h+=step_h)
    {
      abp_all_dmv[h][v][*bw_ref][blocktype][0] = all_bmv [h][v][*bw_ref][blocktype][0] - ((mv_scale*all_mv [h][v][*fw_ref][blocktype][0]+128)>>8);
      abp_all_dmv[h][v][*bw_ref][blocktype][1] = all_bmv [h][v][*bw_ref][blocktype][1] - ((mv_scale*all_mv [h][v][*fw_ref][blocktype][1]+128)>>8);

      mvd_bits += mvbits[ all_mv [h][v][*fw_ref][blocktype][0] - p_fwMV[h][v][*fw_ref][blocktype][0] ];
      mvd_bits += mvbits[ all_mv [h][v][*fw_ref][blocktype][1] - p_fwMV[h][v][*fw_ref][blocktype][1] ];
      mvd_bits += mvbits[ abp_all_dmv[h][v][*bw_ref][blocktype][0] ];
      mvd_bits += mvbits[ abp_all_dmv[h][v][*bw_ref][blocktype][1] ];
    }
    mcost0 = WEIGHTED_COST (lambda_factor, mvd_bits);

    //----- cost of residual signal -----
    for (byy=0, v=by0[parttype][block8x8]; v<by0[parttype][block8x8]+step_v0; byy+=4, v++)
    {
      pic_pix_y = pix_y + (block_y = (v<<2));

      for (bxx=0, h=bx0[parttype][block8x8]; h<bx0[parttype][block8x8]+step_h0; bxx+=4, h++)
      {
        pic_pix_x = img->pix_x + (block_x = (h<<2));

        LumaPrediction4x4 (block_x, block_y, blocktype, blocktype, *fw_ref, *bw_ref);

        for (k=j=0; j<4; j++)
        for (  i=0; i<4; i++, k++)
        {
          diff[k] = imgY_original[pic_pix_y+j][pic_pix_x+i] - img->mpr[i+block_x][j+block_y];
        }
        mcost0 += SATD (diff, input->hadamard);
      }

    }

//    if ( img->type==BS_IMG)
		if ( img->type==B_SLICE && img->nal_reference_idc>0)
    {
      *abp_type = 1;
      mcost = mcost0;
      if ((input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode) || input->InterlaceCodingOption == FIELD_CODING) 
      {
        *fw_ref = 3;
        *bw_ref = 1;
      }
      else
      {
        *fw_ref = 1;
        *bw_ref = 0;
      }
    }
  }
#if 0
//  if (input->explicit_B_prediction==1 && img->type==BS_IMG)
	if (input->explicit_B_prediction==1 && img->type==B_SLICE && img->nal_reference_idc>0)
  {
    //
    //  Extrapolation (2,-1,0)
    //  
    //----- cost for motion vector bits -----
    if ((input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode) || input->InterlaceCodingOption == FIELD_CODING) 
    {
      *fw_ref = 1;
      *bw_ref = 3;
    }
    else
    {
      *fw_ref = 0;
      *bw_ref = 1;
    }
    mvd_bits = 0;
    mv_scale = 256*((*bw_ref)+1)/((*fw_ref)+1);
    for (v=by0[parttype][block8x8]; v<by0[parttype][block8x8]+step_v0; v+=step_v)
    for (h=bx0[parttype][block8x8]; h<bx0[parttype][block8x8]+step_h0; h+=step_h)
    {
      abp_all_dmv[h][v][*bw_ref][blocktype][0] = all_bmv [h][v][*bw_ref][blocktype][0] - ((mv_scale*all_mv [h][v][*fw_ref][blocktype][0]+128)>>8);
      abp_all_dmv[h][v][*bw_ref][blocktype][1] = all_bmv [h][v][*bw_ref][blocktype][1] - ((mv_scale*all_mv [h][v][*fw_ref][blocktype][1]+128)>>8);

      mvd_bits += mvbits[ all_mv [h][v][*fw_ref][blocktype][0] - p_fwMV[h][v][*fw_ref][blocktype][0] ];
      mvd_bits += mvbits[ all_mv [h][v][*fw_ref][blocktype][1] - p_fwMV[h][v][*fw_ref][blocktype][1] ];
      mvd_bits += mvbits[ abp_all_dmv[h][v][*bw_ref][blocktype][0] ];
      mvd_bits += mvbits[ abp_all_dmv[h][v][*bw_ref][blocktype][1] ];

    }
    mcost1 = WEIGHTED_COST (lambda_factor, mvd_bits);

    //----- cost of residual signal -----
    for (byy=0, v=by0[parttype][block8x8]; v<by0[parttype][block8x8]+step_v0; byy+=4, v++)
    {
      pic_pix_y = pix_y + (block_y = (v<<2));

      for (bxx=0, h=bx0[parttype][block8x8]; h<bx0[parttype][block8x8]+step_h0; bxx+=4, h++)
      {
        pic_pix_x = img->pix_x + (block_x = (h<<2));

        LumaPrediction4x4 (block_x, block_y, blocktype, blocktype, *fw_ref, *bw_

⌨️ 快捷键说明

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