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

📄 mc_prediction.c

📁 H.264编码实现
💻 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 <memory.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];


static const unsigned char subblk_offset_x[3][8][4] =
{
  { 
    {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}, 
  },
  { 
    {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 4, 0, 4},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}, 
  },
  { 
    {0, 4, 0, 4},
    {8,12, 8,12},
    {0, 4, 0, 4},
    {8,12, 8,12},
    {0, 4, 0, 4},
    {8,12, 8,12},
    {0, 4, 0, 4},
    {8,12, 8,12}  
  }
};

static const unsigned char subblk_offset_y[3][8][4] =
{
  { 
    {0, 0, 4, 4},
    {0, 0, 4, 4},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}, 
  },
  { 
    {0, 0, 4, 4},
    {8, 8,12,12},
    {0, 0, 4, 4},
    {8, 8,12,12},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
  },
  {
    {0, 0, 4, 4},
    {0, 0, 4, 4},
    {8, 8,12,12},
    {8, 8,12,12},
    {0, 0, 4, 4},
    {0, 0, 4, 4},
    {8, 8,12,12},
    {8, 8,12,12} 
  }
};

/*!
 ************************************************************************
 * \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);

  width_pad  = list->size_x_pad;
  height_pad = list->size_y_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  i, j;
  int  block_x4     = block_x + block_size_x;
  int  block_y4     = block_y + block_size_y;
  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;
  imgpel* l0pred    = l0_pred;
  imgpel* l1pred    = l1_pred;  
  short****** mv_array = img->all_mv;
  short   *curr_mv = NULL;
  imgpel (*mb_pred)[16] = 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)
    {
      int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][0];
      int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][0];
      int offset = (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1;
      int wp_round = 2 * wp_luma_round;
      int weight_denom = luma_log_weight_denom + 1;
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          mb_pred[j][i] = iClip1( img->max_imgpel_value, 
          ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + offset); 
    }
    else if (p_dir==0)
    {
      int wp = wp_weight[0][l0_ref_idx][0];
      int offset = wp_offset[0][l0_ref_idx][0];
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] = iClip1( img->max_imgpel_value, 
          ((wp * *l0pred++  + wp_luma_round) >> luma_log_weight_denom) + offset);
    }
    else // (p_dir==1)
    {
      int wp = wp_weight[1][l1_ref_idx][0];
      int offset = wp_offset[1][l1_ref_idx][0];
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] = iClip1( img->max_imgpel_value, 
          ((wp * *l1pred++  + wp_luma_round) >> luma_log_weight_denom) + offset );
    }
  }
  else
  {
    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          mb_pred[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
    }
    else if (p_dir==0)
    {
      for (j=block_y; j<block_y4; j++)
      {
        memcpy(&(mb_pred[j][block_x]), l0pred, block_size_x * sizeof(imgpel));
        l0pred += block_size_x;
      }
    }
    else // (p_dir==1)
    {
      for (j=block_y; j<block_y4; j++)
      {
        memcpy(&(mb_pred[j][block_x]), l1pred, block_size_x * sizeof(imgpel));
        l1pred += 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  i, j;
  int  block_x4  = block_x + block_size_x;
  int  block_y4  = block_y + block_size_y;
  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;
  imgpel* l0pred     = l0_pred;
  imgpel* l1pred     = l1_pred;

  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)[16] = 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)
  {
    int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][0];
    int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][0];
    int offset = (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1;
    int wp_round = 2*wp_luma_round;
    int weight_denom = luma_log_weight_denom + 1;

    for   (j=block_y; j<block_y4; j++)
      for (i=block_x; i<block_x4; i++)
        mb_pred[j][i] = iClip1( img->max_imgpel_value,
        ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> weight_denom) + offset);
  }
  else
  {
    for   (j=block_y; j<block_y4; j++)
      for (i=block_x; i<block_x4; i++)
        mb_pred[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
  }
}


/*!
 ************************************************************************
 * \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,
                                 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 + -