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

📄 intra4x4_pred.c

📁 H.264基于baseline解码器的C++实现源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*!
 *************************************************************************************
 * \file intra4x4_pred.c
 *
 * \brief
 *    Functions for intra 4x4 prediction
 *
 * \author
 *      Main contributors (see contributors.h for copyright, 
 *                         address and affiliation details)
 *      - Alexis Michael Tourapis  <alexismt@ieee.org>
 *
 *************************************************************************************
 */
#include "global.h"
#include "intra4x4_pred.h"
#include "mb_access.h"
#include "image.h"

// Notation for comments regarding prediction and predictors.
// The pels of the 4x4 block are labelled a..p. The predictor pels above
// are labelled A..H, from the left I..L, and from above left X, as follows:
//
//  X A B C D E F G H
//  I a b c d
//  J e f g h
//  K i j k l
//  L m n o p
//

// 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])


/*!
 ***********************************************************************
 * \brief
 *    makes and returns 4x4 DC prediction mode
 *
 * \return
 *    DECODING_OK   decoding of intraprediction mode was sucessfull            \n
 *
 ***********************************************************************
 */
static inline int intra4x4_dc_pred(ImageParameters *img,  //!< image parameters 
                                   Macroblock *currMB, 
                                   ColorPlane pl,               
                                   int ioff,             //!< pixel offset X within MB
                                   int joff)             //!< pixel offset Y within MB
{
  int i,j;
  int s0 = 0;  
  imgpel **imgY = (pl) ? dec_picture->imgUV[pl - 1] : dec_picture->imgY;

  PixelPos pix_a[4], pix_b;

  int block_available_up;
  int block_available_left;  

  imgpel **mb_pred = img->mb_pred[pl];    

  for (i=0;i<4;i++)
  {
    getNeighbour(currMB, ioff - 1, joff + i, img->mb_size[IS_LUMA], &pix_a[i]);
  }
  getNeighbour(currMB, ioff    , joff -1 , img->mb_size[IS_LUMA], &pix_b);

  if (active_pps->constrained_intra_pred_flag)
  {
    for (i=0, block_available_left=1; i<4;i++)
      block_available_left  &= pix_a[i].available ? img->intra_block[pix_a[i].mb_addr]: 0;
    block_available_up       = pix_b.available ? img->intra_block [pix_b.mb_addr] : 0;
  }
  else
  {
    block_available_left     = pix_a[0].available;
    block_available_up       = pix_b.available;
  }

  // form predictor pels
  if (block_available_up)
  {
    s0 += imgY[pix_b.pos_y][pix_b.pos_x + 0];
    s0 += imgY[pix_b.pos_y][pix_b.pos_x + 1];
    s0 += imgY[pix_b.pos_y][pix_b.pos_x + 2];
    s0 += imgY[pix_b.pos_y][pix_b.pos_x + 3];
  }

  if (block_available_left)
  {
    s0 += imgY[pix_a[0].pos_y][pix_a[0].pos_x];
    s0 += imgY[pix_a[1].pos_y][pix_a[1].pos_x];
    s0 += imgY[pix_a[2].pos_y][pix_a[2].pos_x];
    s0 += imgY[pix_a[3].pos_y][pix_a[3].pos_x];
  }

  if (block_available_up && block_available_left)
  {
    // no edge
    s0 = (s0 + 4)>>3;
  }
  else if (!block_available_up && block_available_left)
  {
    // upper edge
    s0 = (s0 + 2)>>2;
  }
  else if (block_available_up && !block_available_left)
  {
    // left edge
    s0 = (s0 + 2)>>2;
  }
  else //if (!block_available_up && !block_available_left)
  {
    // top left corner, nothing to predict from
    s0 = img->dc_pred_value_comp[pl];
  }

  for (j=joff; j < joff + BLOCK_SIZE; j++)
  {
    for (i=ioff; i < ioff + BLOCK_SIZE; i++)
    {
      // store DC prediction
      mb_pred[j][i] = (imgpel) s0;
    }
  }
  return DECODING_OK;
}

/*!
 ***********************************************************************
 * \brief
 *    makes and returns 4x4 vertical prediction mode
 *
 * \return
 *    DECODING_OK   decoding of intraprediction mode was sucessfull            \n
 *
 ***********************************************************************
 */
static inline int intra4x4_vert_pred(ImageParameters *img,  //!< image parameters
                                     Macroblock *currMB,    //!< current macroblock
                                     ColorPlane pl,         //!< current image plane
                                     int ioff,              //!< pixel offset X within MB
                                     int joff)              //!< pixel offset Y within MB
{
  int j;
  imgpel **imgY = (pl) ? dec_picture->imgUV[pl - 1] : dec_picture->imgY;
  
  int block_available_up;

  PixelPos pix_b;

  imgpel **mb_pred = img->mb_pred[pl];

  getNeighbour(currMB, ioff, joff - 1 , img->mb_size[IS_LUMA], &pix_b);

  if (active_pps->constrained_intra_pred_flag)
  {
    block_available_up = pix_b.available ? img->intra_block [pix_b.mb_addr] : 0;
  }
  else
  {
    block_available_up = pix_b.available;
  }

  if (!block_available_up)
    printf ("warning: Intra_4x4_Vertical prediction mode not allowed at mb %d\n", (int) img->current_mb_nr);

  for(j = joff; j < joff + BLOCK_SIZE; j++) /* store predicted 4x4 block */
    memcpy(&(mb_pred[j][ioff]), &(imgY[pix_b.pos_y][pix_b.pos_x]), BLOCK_SIZE * sizeof(imgpel));

  return DECODING_OK;
}

/*!
 ***********************************************************************
 * \brief
 *    makes and returns 4x4 horizontal prediction mode
 *
 * \return
 *    DECODING_OK   decoding of intraprediction mode was sucessfull            \n
 *
 ***********************************************************************
 */
static inline int intra4x4_hor_pred(ImageParameters *img,  //!< image parameters 
                                    Macroblock *currMB, 
                                    ColorPlane pl,               
                                    int ioff,             //!< pixel offset X within MB
                                    int joff)             //!< pixel offset Y within MB
{
  int i,j;
  imgpel **imgY = (pl) ? dec_picture->imgUV[pl - 1] : dec_picture->imgY;

  PixelPos pix_a[4];

  int block_available_left;

  imgpel *predrow, prediction, **mb_pred = img->mb_pred[pl];    

  for (i=0;i<4;i++)
  {
    getNeighbour(currMB, ioff -1 , joff +i , img->mb_size[IS_LUMA], &pix_a[i]);
  }

  if (active_pps->constrained_intra_pred_flag)
  {
    for (i=0, block_available_left=1; i<4;i++)
      block_available_left  &= pix_a[i].available ? img->intra_block[pix_a[i].mb_addr]: 0;
  }
  else
  {
    block_available_left     = pix_a[0].available;
  }

  if (!block_available_left)
    printf ("warning: Intra_4x4_Horizontal prediction mode not allowed at mb %d\n",(int) img->current_mb_nr);

  for(j=0;j<BLOCK_SIZE;j++)
  {
    predrow = mb_pred[j+joff];
    prediction = imgY[pix_a[j].pos_y][pix_a[j].pos_x];
    for(i = ioff;i < ioff + BLOCK_SIZE;i++)
      predrow[i]= prediction; /* store predicted 4x4 block */
  }

  return DECODING_OK;
}

/*!
 ***********************************************************************
 * \brief
 *    makes and returns 4x4 diagonal down right prediction mode
 *
 * \return
 *    DECODING_OK   decoding of intraprediction mode was sucessfull            \n
 *
 ***********************************************************************
 */
static inline int intra4x4_diag_down_right_pred(ImageParameters *img,  //!< image parameters 
                                                Macroblock *currMB,    //!< current macroblock
                                                ColorPlane pl,         //!< current image plane
                                                int ioff,              //!< pixel offset X within MB
                                                int joff)              //!< pixel offset Y within MB
{
  int i;
  imgpel PredPel[13];  // array of predictor pels
  imgpel **imgY = (pl) ? dec_picture->imgUV[pl - 1] : dec_picture->imgY;

  PixelPos pix_a[4];
  PixelPos pix_b, pix_d;

  int block_available_up;
  int block_available_left;
  int block_available_up_left;

  int jpos0 = joff, jpos1 = joff + 1, jpos2 = joff + 2, jpos3 = joff + 3;
  int ipos0 = ioff, ipos1 = ioff + 1, ipos2 = ioff + 2, ipos3 = ioff + 3;
  imgpel **mb_pred = img->mb_pred[pl];    

  for (i=0;i<4;i++)
  {
    getNeighbour(currMB, ioff -1 , joff +i , img->mb_size[IS_LUMA], &pix_a[i]);
  }

  getNeighbour(currMB, ioff    , joff -1 , img->mb_size[IS_LUMA], &pix_b);
  getNeighbour(currMB, ioff -1 , joff -1 , img->mb_size[IS_LUMA], &pix_d);

  if (active_pps->constrained_intra_pred_flag)
  {
    for (i=0, block_available_left=1; i<4;i++)
      block_available_left  &= pix_a[i].available ? img->intra_block[pix_a[i].mb_addr]: 0;
    block_available_up       = pix_b.available ? img->intra_block [pix_b.mb_addr] : 0;
    block_available_up_left  = pix_d.available ? img->intra_block [pix_d.mb_addr] : 0;
  }
  else
  {
    block_available_left     = pix_a[0].available;
    block_available_up       = pix_b.available;
    block_available_up_left  = pix_d.available;
  }

  if ((!block_available_up)||(!block_available_left)||(!block_available_up_left))
    printf ("warning: Intra_4x4_Diagonal_Down_Right prediction mode not allowed at mb %d\n",(int) img->current_mb_nr);

  // form predictor pels
  P_A = imgY[pix_b.pos_y][pix_b.pos_x + 0];
  P_B = imgY[pix_b.pos_y][pix_b.pos_x + 1];
  P_C = imgY[pix_b.pos_y][pix_b.pos_x + 2];
  P_D = imgY[pix_b.pos_y][pix_b.pos_x + 3];

  P_I = imgY[pix_a[0].pos_y][pix_a[0].pos_x];
  P_J = imgY[pix_a[1].pos_y][pix_a[1].pos_x];
  P_K = imgY[pix_a[2].pos_y][pix_a[2].pos_x];
  P_L = imgY[pix_a[3].pos_y][pix_a[3].pos_x];

  P_X = imgY[pix_d.pos_y][pix_d.pos_x];

  mb_pred[jpos3][ipos0] = (imgpel) ((P_L + 2*P_K + P_J + 2) >> 2);
  mb_pred[jpos2][ipos0] =
  mb_pred[jpos3][ipos1] = (imgpel) ((P_K + 2*P_J + P_I + 2) >> 2);
  mb_pred[jpos1][ipos0] =
  mb_pred[jpos2][ipos1] =
  mb_pred[jpos3][ipos2] = (imgpel) ((P_J + 2*P_I + P_X + 2) >> 2);
  mb_pred[jpos0][ipos0] =
  mb_pred[jpos1][ipos1] =
  mb_pred[jpos2][ipos2] =
  mb_pred[jpos3][ipos3] = (imgpel) ((P_I + 2*P_X + P_A + 2) >> 2);
  mb_pred[jpos0][ipos1] =
  mb_pred[jpos1][ipos2] =
  mb_pred[jpos2][ipos3] = (imgpel) ((P_X + 2*P_A + P_B + 2) >> 2);
  mb_pred[jpos0][ipos2] =
  mb_pred[jpos1][ipos3] = (imgpel) ((P_A + 2*P_B + P_C + 2) >> 2);
  mb_pred[jpos0][ipos3] = (imgpel) ((P_B + 2*P_C + P_D + 2) >> 2);

  return DECODING_OK;
}

/*!
 ***********************************************************************
 * \brief
 *    makes and returns 4x4 diagonal down left prediction mode
 *
 * \return
 *    DECODING_OK   decoding of intraprediction mode was sucessfull            \n
 *
 ***********************************************************************
 */
static inline int intra4x4_diag_down_left_pred(ImageParameters *img,  //!< image parameters 
                                               Macroblock *currMB,    //!< current macroblock
                                               ColorPlane pl,         //!< current image plane
                                               int ioff,              //!< pixel offset X within MB
                                               int joff)              //!< pixel offset Y within MB
{
  imgpel PredPel[13];  // array of predictor pels
  imgpel **imgY = (pl) ? dec_picture->imgUV[pl - 1] : dec_picture->imgY;

  PixelPos pix_b, pix_c;

  int block_available_up;
  int block_available_up_right;

  int jpos0 = joff, jpos1 = joff + 1, jpos2 = joff + 2, jpos3 = joff + 3;
  int ipos0 = ioff, ipos1 = ioff + 1, ipos2 = ioff + 2, ipos3 = ioff + 3;
  imgpel **mb_pred = img->mb_pred[pl];    

  getNeighbour(currMB, ioff    , joff - 1, img->mb_size[IS_LUMA], &pix_b);
  getNeighbour(currMB, ioff + 4, joff - 1, img->mb_size[IS_LUMA], &pix_c);

  pix_c.available = pix_c.available && !((ioff==4) && ((joff==4)||(joff==12)));

  if (active_pps->constrained_intra_pred_flag)
  {
    block_available_up       = pix_b.available ? img->intra_block [pix_b.mb_addr] : 0;
    block_available_up_right = pix_c.available ? img->intra_block [pix_c.mb_addr] : 0;
  }
  else
  {
    block_available_up       = pix_b.available;
    block_available_up_right = pix_c.available;
  }

  if (!block_available_up)
    printf ("warning: Intra_4x4_Diagonal_Down_Left prediction mode not allowed at mb %d\n", (int) img->current_mb_nr);

  // form predictor pels
  P_A = imgY[pix_b.pos_y][pix_b.pos_x + 0];
  P_B = imgY[pix_b.pos_y][pix_b.pos_x + 1];
  P_C = imgY[pix_b.pos_y][pix_b.pos_x + 2];
  P_D = imgY[pix_b.pos_y][pix_b.pos_x + 3];

  if (block_available_up_right)
  {
    P_E = imgY[pix_c.pos_y][pix_c.pos_x + 0];
    P_F = imgY[pix_c.pos_y][pix_c.pos_x + 1];
    P_G = imgY[pix_c.pos_y][pix_c.pos_x + 2];
    P_H = imgY[pix_c.pos_y][pix_c.pos_x + 3];
  }
  else
  {
    P_E = P_F = P_G = P_H = P_D;
  }

  mb_pred[jpos0][ipos0] = (imgpel) ((P_A + P_C + 2*(P_B) + 2) >> 2);
  mb_pred[jpos0][ipos1] =
  mb_pred[jpos1][ipos0] = (imgpel) ((P_B + P_D + 2*(P_C) + 2) >> 2);
  mb_pred[jpos0][ipos2] =
  mb_pred[jpos1][ipos1] =
  mb_pred[jpos2][ipos0] = (imgpel) ((P_C + P_E + 2*(P_D) + 2) >> 2);
  mb_pred[jpos0][ipos3] =
  mb_pred[jpos1][ipos2] =
  mb_pred[jpos2][ipos1] =
  mb_pred[jpos3][ipos0] = (imgpel) ((P_D + P_F + 2*(P_E) + 2) >> 2);
  mb_pred[jpos1][ipos3] =
  mb_pred[jpos2][ipos2] =

⌨️ 快捷键说明

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