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

📄 mc_prediction.c

📁 the newest JM software by h.264 JVT official reference model.
💻 C
📖 第 1 页 / 共 3 页
字号:
/*!
 *************************************************************************************
 * \file mc_prediction.c
 *
 * \brief
 *    Motion Compensation
 *
 * \author
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
 *    - Alexis Michael Tourapis         <alexismt@ieee.org>
 *
 *************************************************************************************
 */

#include "contributors.h"

#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <math.h>

#include "global.h"

#include "macroblock.h"
#include "mc_prediction.h"
#include "refbuf.h"
#include "image.h"
#include "mb_access.h"

static int diff  [16];
static imgpel l0_pred[MB_PIXELS];
static imgpel l1_pred[MB_PIXELS];


/*!
 ************************************************************************
 * \brief
 *    Weighted BiPrediction
 ************************************************************************
 */
static inline void MCWeightedBiPrediction(imgpel** mb_pred, imgpel* l0pred, imgpel *l1pred, 
                                          int block_size_y, int block_x, int block_size_x,
                                          int max_imgpel_value,
                                          int wbp0, int wbp1, int offset, int wp_round, int weight_denom)
{
  int i, j;
  int block_x4 = block_x + block_size_x;

  for   (j = 0; j< block_size_y; j++)
  {
    for (i=block_x; i<block_x4; i++)  
      mb_pred[j][i] = iClip1( max_imgpel_value, 
      ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + offset); 
  }
}

/*!
 ************************************************************************
 * \brief
 *    Weighted Prediction
 ************************************************************************
 */
static inline void MCWeightedPrediction(imgpel** mb_pred, imgpel* lpred, 
                                        int block_size_y, int block_x, int block_size_x,
                                        int max_imgpel_value,
                                        int wp, int offset, int wp_round, int weight_denom)
{
  int i, j;
  int block_x4 = block_x + block_size_x;

  for   (j = 0; j < block_size_y; j++)
  {
    for (i=block_x; i<block_x4; i++)
      mb_pred[j][i] = iClip1( max_imgpel_value, 
      ((wp * *lpred++  + wp_round) >> weight_denom) + offset);
  }
}

/*!
 ************************************************************************
 * \brief
 *    BiPrediction
 ************************************************************************
 */
static inline void MCBiPrediction(imgpel** mb_pred, imgpel* l0pred, imgpel *l1pred, 
                                  int block_size_y, int block_x, int block_size_x)
{
  int i, j;
  int block_x4 = block_x + block_size_x;

  for   (j = 0; j < block_size_y; j++)
  {
    for (i=block_x; i<block_x4; i++)
      mb_pred[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
  }
}

/*!
 ************************************************************************
 * \brief
 *    BiPrediction
 ************************************************************************
 */
static inline void MCPrediction(imgpel** mb_pred, imgpel* lpred, int block_size_y, int block_x, int block_size_x)
{
  int j;
  for (j = 0; j < block_size_y; j++)
  {
    memcpy(&(mb_pred[j][block_x]), lpred, block_size_x * sizeof(imgpel));
    lpred += block_size_x;
  }
}

/*!
 ************************************************************************
 * \brief
 *    Predict Luma block
 ************************************************************************
 */
static inline void OneComponentLumaPrediction ( imgpel*   mpred,       //!< array of prediction values (row by row)
                                               int    pic_pix_x,      //!< motion shifted horizontal coordinate of block
                                               int    pic_pix_y,      //!< motion shifted vertical   coordinate of block
                                               int    block_size_x,   //!< horizontal block size
                                               int    block_size_y,   //!< vertical block size
                                               StorablePicture *list //!< reference picture list
                                               )
{
  int     j;
  imgpel *ref_line = UMVLine4X (list->p_curr_img_sub, pic_pix_y, pic_pix_x, list->size_y_pad, list->size_x_pad);

  for (j = 0; j < block_size_y; j++) 
  {
    memcpy(mpred, ref_line, block_size_x * sizeof(imgpel));
    ref_line += img_padded_size_x;
    mpred += block_size_x;
  }  
}


/*!
 ************************************************************************
 * \brief
 *    Predict one Luma block
 ************************************************************************
 */
void LumaPrediction ( Macroblock* currMB,//!< Current Macroblock
                     int   block_x,     //!< relative horizontal block coordinate of block
                     int   block_y,     //!< relative vertical   block coordinate of block
                     int   block_size_x,//!< relative horizontal block coordinate of block
                     int   block_size_y,//!< relative vertical   block coordinate of block
                     int   p_dir,       //!< prediction direction (0=list0, 1=list1, 2=bipred)
                     int   l0_mode,     //!< list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
                     int   l1_mode,     //!< list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
                     short l0_ref_idx,  //!< reference frame for list0 prediction (-1: Intra4x4 pred. with l0_mode)
                     short l1_ref_idx,   //!< reference frame for list1 prediction 
                     short bipred_me     //!< use bi prediction mv (0=no bipred, 1 = use set 1, 2 = use set 2)
                     )
{
  int  pic_opix_x   = ((img->opix_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
  int  pic_opix_y   = ((img->opix_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
  int  bx           = block_x >> 2;
  int  by           = block_y >> 2;
  short****** mv_array = img->all_mv;
  short   *curr_mv = NULL;
  imgpel **mb_pred = img->mb_pred[0];

  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)));

  if (bipred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && is_bipred_enabled(l0_mode) && is_bipred_enabled(l1_mode))
    mv_array = img->bipred_mv[bipred_me - 1]; 

  switch (p_dir)
  {
  case 0:
    curr_mv = mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx];
    OneComponentLumaPrediction (l0_pred, pic_opix_x + curr_mv[0], pic_opix_y + curr_mv[1], block_size_x, block_size_y, listX[LIST_0 + currMB->list_offset][l0_ref_idx]);
    break;
  case 1:
    curr_mv = mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx];
    OneComponentLumaPrediction (l1_pred, pic_opix_x + curr_mv[0], pic_opix_y + curr_mv[1], block_size_x, block_size_y, listX[LIST_1 + currMB->list_offset][l1_ref_idx]);
    break;
  case 2:
    curr_mv = mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx];
    OneComponentLumaPrediction (l0_pred, pic_opix_x + curr_mv[0], pic_opix_y + curr_mv[1], block_size_x, block_size_y, listX[LIST_0 + currMB->list_offset][l0_ref_idx]);
    curr_mv = mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx];
    OneComponentLumaPrediction (l1_pred, pic_opix_x + curr_mv[0], pic_opix_y + curr_mv[1], block_size_x, block_size_y, listX[LIST_1 + currMB->list_offset][l1_ref_idx]);
    break;
  default:
    break;
  }

  if (apply_weights)
  {
    if (p_dir==2)
    {
      MCWeightedBiPrediction(&mb_pred[block_y], l0_pred, l1_pred, block_size_y, block_x, block_size_x, 
        img->max_imgpel_value,
        wbp_weight[0][l0_ref_idx][l1_ref_idx][0], wbp_weight[1][l0_ref_idx][l1_ref_idx][0],
        (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1, 
         (wp_luma_round << 1), luma_log_weight_denom + 1);
    }
    else if (p_dir==0)
    {
      MCWeightedPrediction(&mb_pred[block_y], l0_pred, block_size_y, block_x, block_size_x,
        img->max_imgpel_value,
        wp_weight[0][l0_ref_idx][0], wp_offset[0][l0_ref_idx][0], wp_luma_round, luma_log_weight_denom);
    }
    else // (p_dir==1)
    {
      MCWeightedPrediction(&mb_pred[block_y], l1_pred, block_size_y, block_x, block_size_x,
        img->max_imgpel_value,
        wp_weight[1][l1_ref_idx][0], wp_offset[1][l1_ref_idx][0], wp_luma_round, luma_log_weight_denom);
    }
  }
  else
  {
    if (p_dir==2)
    {
      MCBiPrediction(&mb_pred[block_y], l0_pred, l1_pred, block_size_y, block_x, block_size_x);    
    }
    else if (p_dir==0)
    {
      MCPrediction(&mb_pred[block_y], l0_pred, block_size_y, block_x, block_size_x);
    }
    else // (p_dir==1)
    {
      MCPrediction(&mb_pred[block_y], l1_pred, block_size_y, block_x, block_size_x);
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Predict one Luma block
 ************************************************************************
 */
void LumaPredictionBi ( Macroblock* currMB, //!< Current Macroblock
                        int   block_x,      //!< relative horizontal block coordinate of 4x4 block
                        int   block_y,      //!< relative vertical   block coordinate of 4x4 block
                        int   block_size_x, //!< horizontal block size
                        int   block_size_y, //!< vertical   block size
                        int   l0_mode,      //!< list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
                        int   l1_mode,      //!< list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
                        short l0_ref_idx,   //!< reference frame for list0 prediction (-1: Intra4x4 pred. with l0_mode)
                        short l1_ref_idx,   //!< reference frame for list1 prediction 
                        int   list          //!< current list for prediction.
                        )
{
  int  pic_opix_x = ((img->opix_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
  int  pic_opix_y = ((img->opix_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
  int  bx        = block_x >> 2;
  int  by        = block_y >> 2;

  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)));  

  short   ******mv_array = img->bipred_mv[list]; 
  short   *mv_arrayl0 = mv_array[LIST_0][l0_ref_idx][l0_mode][by][bx];
  short   *mv_arrayl1 = mv_array[LIST_1][l1_ref_idx][l1_mode][by][bx];
  imgpel **mb_pred = img->mb_pred[0];

  OneComponentLumaPrediction (l0_pred, pic_opix_x + mv_arrayl0[0], pic_opix_y + mv_arrayl0[1], block_size_x, block_size_y, listX[0+currMB->list_offset][l0_ref_idx]);
  OneComponentLumaPrediction (l1_pred, pic_opix_x + mv_arrayl1[0], pic_opix_y + mv_arrayl1[1], block_size_x, block_size_y, listX[1+currMB->list_offset][l1_ref_idx]);

  if (apply_weights)
  {
    MCWeightedBiPrediction(&mb_pred[block_y], l0_pred, l1_pred, block_size_y, block_x, block_size_x, 
      img->max_imgpel_value,
      wbp_weight[0][l0_ref_idx][l1_ref_idx][0], wbp_weight[1][l0_ref_idx][l1_ref_idx][0],
      (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1, 
      (wp_luma_round << 1), luma_log_weight_denom + 1);
  }
  else
  {
    MCBiPrediction(&mb_pred[block_y], l0_pred, l1_pred, block_size_y, block_x, block_size_x);
  }
}


/*!
 ************************************************************************
 * \brief
 *    Predict (on-the-fly) one component of a chroma 4x4 block
 ************************************************************************
 */
void OneComponentChromaPrediction4x4_regenerate (
                                 imgpel*     mpred,      //!< array to store prediction values
                                 int         block_c_x,  //!< horizontal pixel coordinate of 4x4 block
                                 int         block_c_y,  //!< vertical   pixel coordinate of 4x4 block
                                 short***    mv,         //!< motion vector array
                                 StorablePicture *list,  //!< image components (color planes)
                                 int         uv)         //!< chroma component
{
  int     i, j, ii, jj, ii0, jj0, ii1, jj1, if0, if1, jf0, jf1;
  short*  mvb;

  int     f1_x = 64/img->mb_cr_size_x;
  int     f2_x=f1_x-1;

  int     f1_y = 64/img->mb_cr_size_y;
  int     f2_y=f1_y-1;

  int     f3=f1_x*f1_y, f4=f3>>1;
  int     list_offset = img->mb_data[img->current_mb_nr].list_offset;
  int     max_y_cr = (int) (list_offset ? (img->height_cr >> 1) - 1 : img->height_cr - 1);
  int     max_x_cr = (int) (img->width_cr - 1);
  int     jjx, iix;
  int     mb_cr_y_div4 = img->mb_cr_size_y>>2;
  int     mb_cr_x_div4 = img->mb_cr_size_x>>2;
  int     jpos;

  imgpel** refimage = list->imgUV[uv];

⌨️ 快捷键说明

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