H264编码器实现-帧内预测之生成预测像素值

前言

本文所介绍的像素值预测,是指在帧内预测总体流程中的预测块每个像素值的推导过程。当我们已知向量像素的重建值的时候,我们就可以对当前预测块进行像素值预测。该过程得到的结果将与源像素值相减得到残差,为后续变换量化提供数据来源。

温馨提示:本文所讲的内容都有对应的可执行代码,读者可以结合代码理解文章内容。

预测模式

对亮度像素而言,4×4 亮度子块有9 种可选预测模式,适用于带有大量细节的图像编码;16×16 亮度块有4种预测模式,适用于平坦区域图像编码。对于色度像素,只有8x8色度块,其预测也有4 种预测模式,类似于16×16 亮度块预测模式。
值得注意的是,8x8亮度子块的预测模式和4x4亮度块的预测方式一致,只是块大小不一样,因此很多博客都省略介绍,本文也不过多介绍,只在文末贴上测试代码。

4x4亮度块预测

上文说到4x4亮度块有9种预测模式,不同模式预测的角度不同,下面是各个预测模式的描述:

模式名称描述
模式0由A、B、C、D 垂直推出相应像素值
模式1由I、J、K、L 水平推出相应像素值
模式2由A~D 及I~L 平均值推出所有像素值
模式3由45°方向像素内插得出相应像素值
模式4由45°方向像素内插得出相应像素值
模式5由26.6°方向像素值内插得出相应像素值
模式6由26.6°方向像素值内插得出相应像素值
模式7由26.6° 方向像素值内插得出相应像素值
模式8由26.6° 方向像素值内插得出相应像素值

各预测模式
当前块预测
结合上面两个图,这里对各个预测模式做个解释:
(1)预测模式0(vertical):当前块的十六个像素值由其上方A、B、C、D四个像素值决定。
即:a=e=i=m=A,b=f=j=n=B, c=g=k=o=C,d=h=l=p=D
(2)预测模式1(horizontal):当前块的十六个像素值由其左方I、J、K、L四个像素值决定。
即:a=b=c=d=I,e=f=g=h=J,i=j=k=l=K,m=n=o=p=L。
(3)预测模式2(DC):十六个像素值完全相等,等于ABCDIJKL这八个像素的平均值。
即:a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=(A+B+C+D+I+J+K+K+4)/8。
(4)预测模式3(diagnal down-left):左下45度方向上的像素相等,由相邻块像素加权平均得到。
即:a==(A+2B+C+2)/4,b=e=(B+2C+D)/4,c=f=i=(C+2D+E+2)/4,d=g=j=m=(D+2E+F+2)/4,h=k=n=(E+2F+G+2)/4,
l=o=(F+2G+H+2)/4,p=(G+2H+H+2)/4=(G+3H+2)/4。
(5)预测模式4(diag down-right):右下45度方向上的像素相等,由相邻块像素加权平均得到。
即:a=f=k=p=(I+2M+A+2)/4,e=f=o=(J+2I+M)/4,i=n=(K+2J+I+2)/4,m=(L+2K+J+2)/4,b=g=l=(B+2A+M+2)/4,
c=h=(C+2B+A+2)/4,d=(D+2C+B+2)/4。
(6)预测模式5(vertical right):右下26度方向上的像素相等,由相邻块像素加权平均得到。

include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BLK_WIDTH 4 //当前块宽度
#define BLK_HIGHT 4 //当前块高度
#define BLOCK_SIZE 4
#define BLOCK_SHIFT 2

// Predictor array index definitions
#define P_X (PredPel[0])
#define P_A (PredPel[1])
#define P_B (PredPel[2])
#define P_C (PredPel[3])
#define P_D (PredPel[4])
#define P_E (PredPel[5])
#define P_F (PredPel[6])
#define P_G (PredPel[7])
#define P_H (PredPel[8])
#define P_I (PredPel[9])
#define P_J (PredPel[10])
#define P_K (PredPel[11])
#define P_L (PredPel[12])

typedef unsigned char imgpel;

enum {
  VERT_PRED            = 0,
  HOR_PRED             = 1,
  DC_PRED              = 2,
  DIAG_DOWN_LEFT_PRED  = 3,
  DIAG_DOWN_RIGHT_PRED = 4,
  VERT_RIGHT_PRED      = 5,
  HOR_DOWN_PRED        = 6,
  VERT_LEFT_PRED       = 7,
  HOR_UP_PRED          = 8
} I4x4PredModes;
/*!
 ************************************************************************
 * \brief
 *    Vertical 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_vertical(imgpel **cur_pred, imgpel *PredPel)
{
  memcpy(cur_pred[0], &PredPel[1], BLOCK_SIZE * sizeof(imgpel));
  memcpy(cur_pred[1], &PredPel[1], BLOCK_SIZE * sizeof(imgpel));
  memcpy(cur_pred[2], &PredPel[1], BLOCK_SIZE * sizeof(imgpel));
  memcpy(cur_pred[3], &PredPel[1], BLOCK_SIZE * sizeof(imgpel));
}
/*!
 ************************************************************************
 * \brief
 *    Horizontal 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_horizontal(imgpel **cur_pred, imgpel *PredPel)
{
  int i;

  for (i=0; i < BLOCK_SIZE; i++)
  {
    cur_pred[i][0]  =
    cur_pred[i][1]  =
    cur_pred[i][2]  =
    cur_pred[i][3]  = (imgpel) (&P_I)[i];
  }
}

/*!
 ************************************************************************
 * \brief
 *    DC 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_dc(imgpel **cur_pred, imgpel *PredPel, int left_available, int up_available)
{
  int i, j, s0 = 0;
  if (up_available && left_available)
  {
    // no edge
    s0 = (P_A + P_B + P_C + P_D + P_I + P_J + P_K + P_L + 4) >> (BLOCK_SHIFT + 1);
  }
  else if (!up_available && left_available)
  {
    // upper edge
    s0 = (P_I + P_J + P_K + P_L + 2) >> BLOCK_SHIFT;;
  }
  else if (up_available && !left_available)
  {
    // left edge
    s0 = (P_A + P_B + P_C + P_D + 2) >> BLOCK_SHIFT;
  }
  else //if (!up_available && !left_available)
  {
    // top left corner, nothing to predict from
    s0 = P_A; // P_A already set to p_Vid->dc_pred_value;
  }

  for (j=0; j < BLOCK_SIZE; j++)
  {
    for (i=0; i < BLOCK_SIZE; i++)
      cur_pred[j][i] = (imgpel) s0;
  }
}
/*!
 ************************************************************************
 * \brief
 *    Diagonal Down Left 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_downleft(imgpel **cur_pred, imgpel *PredPel)
{
  cur_pred[0][0] = (imgpel) ((P_A + P_C + ((P_B) << 1) + 2) >> 2);
  cur_pred[0][1] =
  cur_pred[1][0] = (imgpel) ((P_B + P_D + ((P_C) << 1) + 2) >> 2);
  cur_pred[0][2] =
  cur_pred[1][1] =
  cur_pred[2][0] = (imgpel) ((P_C + P_E + ((P_D) << 1) + 2) >> 2);
  cur_pred[0][3] =
  cur_pred[1][2] =
  cur_pred[2][1] =
  cur_pred[3][0] = (imgpel) ((P_D + P_F + ((P_E) << 1) + 2) >> 2);
  cur_pred[1][3] =
  cur_pred[2][2] =
  cur_pred[3][1] = (imgpel) ((P_E + P_G + ((P_F)<<1) + 2) >> 2);
  cur_pred[2][3] =
  cur_pred[3][2] = (imgpel) ((P_F + P_H + ((P_G)<<1) + 2) >> 2);
  cur_pred[3][3] = (imgpel) ((P_G + 3*(P_H) + 2) >> 2);
}

/*!
 ************************************************************************
 * \brief
 *    Diagonal Down Right 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_downright(imgpel **cur_pred, imgpel *PredPel)
{
  cur_pred[3][0] = (imgpel) ((P_L + 2*P_K + P_J + 2) >> 2);
  cur_pred[2][0] =
  cur_pred[3][1] = (imgpel) ((P_K + 2*P_J + P_I + 2) >> 2);
  cur_pred[1][0] =
  cur_pred[2][1] =
  cur_pred[3][2] = (imgpel) ((P_J + 2*P_I + P_X + 2) >> 2);
  cur_pred[0][0] =
  cur_pred[1][1] =
  cur_pred[2][2] =
  cur_pred[3][3] = (imgpel) ((P_I + 2*P_X + P_A + 2) >> 2);
  cur_pred[0][1] =
  cur_pred[1][2] =
  cur_pred[2][3] = (imgpel) ((P_X + 2*P_A + P_B + 2) >> 2);
  cur_pred[0][2] =
  cur_pred[1][3] = (imgpel) ((P_A + 2*P_B + P_C + 2) >> 2);
  cur_pred[0][3] = (imgpel) ((P_B + 2*P_C + P_D + 2) >> 2);
}
/*!
 ************************************************************************
 * \brief
 *    Vertical Left 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_vertleft(imgpel **cur_pred, imgpel *PredPel)
{
  cur_pred[0][0] = (imgpel) ((P_A + P_B + 1) >> 1);
  cur_pred[0][1] =
  cur_pred[2][0] = (imgpel) ((P_B + P_C + 1) >> 1);
  cur_pred[0][2] =
  cur_pred[2][1] = (imgpel) ((P_C + P_D + 1) >> 1);
  cur_pred[0][3] =
  cur_pred[2][2] = (imgpel) ((P_D + P_E + 1) >> 1);
  cur_pred[2][3] = (imgpel) ((P_E + P_F + 1) >> 1);
  cur_pred[1][0] = (imgpel) ((P_A + ((P_B)<<1) + P_C + 2) >> 2);
  cur_pred[1][1] =
  cur_pred[3][0] = (imgpel) ((P_B + ((P_C)<<1) + P_D + 2) >> 2);
  cur_pred[1][2] =
  cur_pred[3][1] = (imgpel) ((P_C + ((P_D)<<1) + P_E + 2) >> 2);
  cur_pred[1][3] =
  cur_pred[3][2] = (imgpel) ((P_D + ((P_E)<<1) + P_F + 2) >> 2);
  cur_pred[3][3] = (imgpel) ((P_E + ((P_F)<<1) + P_G + 2) >> 2);
}
/*!
 ************************************************************************
 * \brief
 *    Horizontal Down 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_hordown(imgpel **cur_pred, imgpel *PredPel)
{
  cur_pred[0][0] =
  cur_pred[1][2] = (imgpel) ((P_X + P_I + 1) >> 1);
  cur_pred[0][1] =
  cur_pred[1][3] = (imgpel) ((P_I + 2*P_X + P_A + 2) >> 2);
  cur_pred[0][2] = (imgpel) ((P_X + 2*P_A + P_B + 2) >> 2);
  cur_pred[0][3] = (imgpel) ((P_A + 2*P_B + P_C + 2) >> 2);
  cur_pred[1][0] =
  cur_pred[2][2] = (imgpel) ((P_I + P_J + 1) >> 1);
  cur_pred[1][1] =
  cur_pred[2][3] = (imgpel) ((P_X + 2*P_I + P_J + 2) >> 2);
  cur_pred[2][0] =
  cur_pred[3][2] = (imgpel) ((P_J + P_K + 1) >> 1);
  cur_pred[2][1] =
  cur_pred[3][3] = (imgpel) ((P_I + 2*P_J + P_K + 2) >> 2);
  cur_pred[3][0] = (imgpel) ((P_K + P_L + 1) >> 1);
  cur_pred[3][1] = (imgpel) ((P_J + 2*P_K + P_L + 2) >> 2);
}
/*!
 ************************************************************************
 * \brief
 *    Horizontal Up 4x4 Prediction
 ************************************************************************
 */
static inline void get_i4x4_horup(imgpel **cur_pred, imgpel *PredPel)
{
  cur_pred[0][0] = (imgpel) ((P_I + P_J + 1) >> 1);
  cur_pred[0][1] = (imgpel) ((P_I + 2*P_J + P_K + 2) >> 2);
  cur_pred[0][2] =
  cur_pred[1][0] = (imgpel) ((P_J + P_K + 1) >> 1);
  cur_pred[0][3] =
  cur_pred[1][1] = (imgpel) ((P_J + 2*P_K + P_L + 2) >> 2);
  cur_pred[1][2] =
  cur_pred[2][0] = (imgpel) ((P_K + P_L + 1) >> 1);
  cur_pred[1][3] =
  cur_pred[2][1] = (imgpel) ((P_K + 2*P_L + P_L + 2) >> 2);
  cur_pred[3][0] =
  cur_pred[2][2] =
  cur_pred[2][3] =
  cur_pred[3][1] =
  cur_pred[3][2] =
  cur_pred[3][3] = (imgpel) P_L;
}

void get_intrapred_4x4(imgpel *PredPel, imgpel ***curr_mpr_4x4, int i4x4_mode,  int left_available, int up_available)
{

  switch (i4x4_mode)
  {
  case VERT_PRED :
    get_i4x4_vertical(curr_mpr_4x4[VERT_PRED], PredPel);
    break;
  case HOR_PRED :
    get_i4x4_horizontal(curr_mpr_4x4[HOR_PRED], PredPel);
    break;
  case DC_PRED :
    get_i4x4_dc(curr_mpr_4x4[DC_PRED], PredPel, left_available, up_available);
    break;
  case DIAG_DOWN_LEFT_PRED :
    get_i4x4_downleft(curr_mpr_4x4[DIAG_DOWN_LEFT_PRED], PredPel);
    break;
  case DIAG_DOWN_RIGHT_PRED :
    get_i4x4_downright(curr_mpr_4x4[DIAG_DOWN_RIGHT_PRED], PredPel);
    break;
  case VERT_RIGHT_PRED :
    get_i4x4_vertright(curr_mpr_4x4[VERT_RIGHT_PRED], PredPel);
    break;
  case HOR_DOWN_PRED :
    get_i4x4_hordown(curr_mpr_4x4[HOR_DOWN_PRED], PredPel);
    break;
  case VERT_LEFT_PRED :
    get_i4x4_vertleft(curr_mpr_4x4[VERT_LEFT_PRED], PredPel);
    break;
  case HOR_UP_PRED :
    get_i4x4_horup(curr_mpr_4x4[HOR_UP_PRED], PredPel);
    break;
  default:
    printf("invalid prediction mode \n");
    break;
  }
}

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值