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

📄 block.c

📁 H.264基于baseline解码器的C++实现源码
💻 C
📖 第 1 页 / 共 2 页
字号:

/*!
 ***********************************************************************
 *  \file
 *      block.c
 *
 *  \brief
 *      Block functions
 *
 *  \author
 *      Main contributors (see contributors.h for copyright, address and affiliation details)
 *      - Inge Lille-Langoy          <inge.lille-langoy@telenor.com>
 *      - Rickard Sjoberg            <rickard.sjoberg@era.ericsson.se>
 ***********************************************************************
 */

#include "contributors.h"

#include "global.h"
#include "block.h"
#include "image.h"
#include "mb_access.h"
#include "transform.h"
#include "quant.h"
#include "memalloc.h"

static int **M4;
static int **PBlock;  

int allocate_block_mem(void)
{
  int alloc_size = 0;
  alloc_size += get_mem2Dint(&M4, BLOCK_SIZE, BLOCK_SIZE);
  alloc_size += get_mem2Dint(&PBlock, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  
  return (alloc_size);
}

void free_block_mem(void)
{
  free_mem2Dint(PBlock);
  free_mem2Dint(M4);
}

static void intra_chroma_DC_single(imgpel **curr_img, int up_avail, int left_avail, PixelPos up, PixelPos left[17], int blk_x, int blk_y, int *pred, int direction )
{
  static int i;
  int s0 = 0;

  if ((direction && up_avail) || (!left_avail && up_avail))
  {
    for (i = blk_x; i < (blk_x + 4);i++)  
      s0 += curr_img[up.pos_y][up.pos_x + i];
    *pred = (s0 + 2) >> 2;
  }
  else if (left_avail)  
  {
    for (i = blk_y; i < (blk_y + 4);i++)  
      s0 += curr_img[left[i].pos_y][left[i].pos_x];
    *pred = (s0 + 2) >> 2;
  }
}


static void intra_chroma_DC_all(imgpel **curr_img, int up_avail, int left_avail, PixelPos up, PixelPos left[17], int blk_x, int blk_y, int *pred )
{
  static int i;
  int s0 = 0, s1 = 0;

  if (up_avail)       
    for (i = blk_x; i < (blk_x + 4);i++)  
      s0 += curr_img[up.pos_y][up.pos_x + i];
  
  if (left_avail)  
    for (i = blk_y; i < (blk_y + 4);i++)  
      s1 += curr_img[left[i].pos_y][left[i].pos_x];
  
  if (up_avail && left_avail)
    *pred = (s0 + s1 + 4) >> 3;
  else if (up_avail)
    *pred = (s0 + 2) >> 2;
  else if (left_avail)
    *pred = (s1 + 2) >> 2;
}

/*!
 ************************************************************************
 * \brief
 *    Chroma Intra prediction. Note that many operations can be moved
 *    outside since they are repeated for both components for no reason.
 ************************************************************************
 */

void intrapred_chroma(ImageParameters *img, Macroblock *currMB, int uv)
{
  int i,j, ii, jj;

  imgpel **imgUV = dec_picture->imgUV[uv];
  imgpel **mb_pred = img->mb_pred[uv + 1];
  int     max_imgpel_value = img->max_imgpel_value_comp[uv + 1];
  
  int ih, iv, ib, ic, iaa;

  int        b8, b4;
  int        yuv = dec_picture->chroma_format_idc - 1;
  int        blk_x, blk_y;
  int        pred;
  static int block_pos[3][4][4]= //[yuv][b8][b4]
  {
    { {0, 1, 2, 3},{0, 0, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
    { {0, 1, 2, 3},{2, 3, 2, 3},{0, 0, 0, 0},{0, 0, 0, 0}},
    { {0, 1, 2, 3},{1, 1, 3, 3},{2, 3, 2, 3},{3, 3, 3, 3}}
  };

  PixelPos up;        //!< pixel position  p(0,-1)
  PixelPos left[17];  //!< pixel positions p(-1, -1..16)

  int up_avail, left_avail[2], left_up_avail;

  int cr_MB_x = img->mb_cr_size_x;
  int cr_MB_y = img->mb_cr_size_y;
  int cr_MB_y2 = (cr_MB_y >> 1);
  int cr_MB_x2 = (cr_MB_x >> 1);

  for (i=0; i < cr_MB_y + 1 ; i++)
  {
    getNeighbour(currMB, -1, i-1, img->mb_size[IS_CHROMA], &left[i]);
  }

  getNeighbour(currMB, 0, -1, img->mb_size[IS_CHROMA], &up);

  if (!active_pps->constrained_intra_pred_flag)
  {
    up_avail      = up.available;
    left_avail[0] = left_avail[1] = left[1].available;
    left_up_avail = left[0].available;
  }
  else
  {
    up_avail = up.available ? img->intra_block[up.mb_addr] : 0;
    for (i=0, left_avail[0] = 1; i < cr_MB_y2;i++)
      left_avail[0]  &= left[i + 1].available ? img->intra_block[left[i + 1].mb_addr]: 0;

    for (i = cr_MB_y2, left_avail[1] = 1; i<cr_MB_y;i++)
      left_avail[1]  &= left[i + 1].available ? img->intra_block[left[i + 1].mb_addr]: 0;

    left_up_avail = left[0].available ? img->intra_block[left[0].mb_addr]: 0;
  }

  switch (currMB->c_ipred_mode)
  {
  case DC_PRED_8:
    // DC prediction
    // Note that unlike what is stated in many presentations and papers, this mode does not operate
    // the same way as I_16x16 DC prediction.
    for(b8 = 0; b8 < (img->num_uv_blocks) ;b8++)
    {
      for (b4 = 0; b4 < 4; b4++)
      {
        blk_y = subblk_offset_y[yuv][b8][b4];
        blk_x = subblk_offset_x[yuv][b8][b4];

        pred = img->dc_pred_value_comp[1];

        //===== get prediction value =====
        switch (block_pos[yuv][b8][b4])
        {
        case 0:  //===== TOP LEFT =====
          intra_chroma_DC_all   (imgUV, up_avail, left_avail[0], up, left, blk_x, blk_y + 1, &pred);
          break;
        case 1: //===== TOP RIGHT =====
          intra_chroma_DC_single(imgUV, up_avail, left_avail[0], up, left, blk_x, blk_y + 1, &pred, 1);
          break;
        case 2: //===== BOTTOM LEFT =====
          intra_chroma_DC_single(imgUV, up_avail, left_avail[1], up, left, blk_x, blk_y + 1, &pred, 0);
          break;
        case 3: //===== BOTTOM RIGHT =====
          intra_chroma_DC_all   (imgUV, up_avail, left_avail[1], up, left, blk_x, blk_y + 1, &pred);          
          break;
        }
        
        for (jj = blk_y; jj < blk_y + BLOCK_SIZE; jj++)
        {
          for (ii = blk_x; ii < blk_x + BLOCK_SIZE; ii++)
          {
            mb_pred[jj][ii]=(imgpel) pred;
          }
        }
      }
    }
    break;
  case HOR_PRED_8:
    // Horizontal Prediction
    if (!left_avail[0] || !left_avail[1])
      error("unexpected HOR_PRED_8 chroma intra prediction mode",-1);

    for (j = 0; j < cr_MB_y; j++)
    {
      pred = imgUV[left[1 + j].pos_y][left[1 + j].pos_x];
      for (i = 0; i < cr_MB_x; i++)
        mb_pred[j][i]=(imgpel) pred;
    }

    break;
  case VERT_PRED_8:
    // Vertical Prediction
    if (!up_avail)
      error("unexpected VERT_PRED_8 chroma intra prediction mode",-1);

    for (j = 0; j < cr_MB_y; j++)
    {
      memcpy(&(mb_pred[j][0]), &(imgUV[up.pos_y][up.pos_x]), cr_MB_x * sizeof(imgpel));
    }
    break;
  case PLANE_8:
    // plane prediction
    if (!left_up_avail || !left_avail[0] || !left_avail[1] || !up_avail)
      error("unexpected PLANE_8 chroma intra prediction mode",-1);
    else
    {
      imgpel *upPred = &imgUV[up.pos_y][up.pos_x];

      ih = cr_MB_x2 * (upPred[cr_MB_x - 1] - imgUV[left[0].pos_y][left[0].pos_x]);
      for (i = 0; i < cr_MB_x2 - 1; i++)
        ih += (i + 1) * (upPred[cr_MB_x2 + i] - upPred[cr_MB_x2 - 2 - i]);

      iv = cr_MB_y2 * (imgUV[left[cr_MB_y].pos_y][left[cr_MB_y].pos_x] - imgUV[left[0].pos_y][left[0].pos_x]);
      for (i = 0; i < cr_MB_y2 - 1; i++)
        iv += (i + 1)*(imgUV[left[cr_MB_y2 + 1 + i].pos_y][left[cr_MB_y2 + 1 + i].pos_x] -
        imgUV[left[cr_MB_y2 - 1 - i].pos_y][left[cr_MB_y2 - 1 - i].pos_x]);

      ib= ((cr_MB_x == 8 ? 17 : 5) * ih + 2 * cr_MB_x)>>(cr_MB_x == 8 ? 5 : 6);
      ic= ((cr_MB_y == 8 ? 17 : 5) * iv + 2 * cr_MB_y)>>(cr_MB_y == 8 ? 5 : 6);

      iaa=16*(imgUV[left[cr_MB_y].pos_y][left[cr_MB_y].pos_x] + upPred[cr_MB_x-1]);

      for (j = 0; j < cr_MB_y; j++)
        for (i = 0; i < cr_MB_x; i++)
          mb_pred[j][i]=(imgpel) iClip1(max_imgpel_value, ((iaa + (i - cr_MB_x2 + 1) * ib + (j - cr_MB_y2 + 1) * ic + 16) >> 5));  
    }
    break;
  default:
    error("illegal chroma intra prediction mode", 600);
    break;
  }
}

/*!
 ***********************************************************************
 * \brief
 *    Inverse 4x4 transformation, transforms cof to mb_rres
 ***********************************************************************
 */
void itrans4x4(ImageParameters *img, //!< image parameters
               ColorPlane pl,        //!< used color plane
               int ioff,             //!< index to 4x4 block
               int joff)             //!< index to 4x4 block
{
  static int i,j;

  int max_imgpel_value = img->max_imgpel_value_comp[pl];

  imgpel **mb_pred = img->mb_pred[pl];
  imgpel **mb_rec  = img->mb_rec[pl];
  int    **mb_rres = img->mb_rres[pl];

  inverse4x4(img->cof[pl],mb_rres,joff,ioff);

  for (j = joff; j < joff + BLOCK_SIZE; j++)
  {
    for (i = ioff; i < ioff + BLOCK_SIZE; i++)
    {      
      mb_rec[j][i] = (imgpel) iClip1(max_imgpel_value, rshift_rnd_sf((mb_rres[j][i] + ((long)mb_pred[j][i] << DQ_BITS)), DQ_BITS));      
    }
  }
}

/*!
 ****************************************************************************
 * \brief
 *    Inverse 4x4 lossless_qpprime transformation, transforms cof to mb_rres
 ****************************************************************************
 */
void itrans4x4_ls(ImageParameters *img, //!< image parameters
                  ColorPlane pl,        //!< Color plane (for 4:4:4)                  
                  int ioff,             //!< index to 4x4 block
                  int joff)             //!< index to 4x4 block
{
  static int i,j;

  int max_imgpel_value = img->max_imgpel_value_comp[pl];

  imgpel **mb_pred = img->mb_pred[pl];
  imgpel **mb_rec  = img->mb_rec[pl];
  int    **mb_rres = img->mb_rres [pl];

  inverse4x4(img->cof[pl],mb_rres,joff,ioff);

  for (j = joff; j < joff + BLOCK_SIZE; j++)
  {
    for (i = ioff; i < ioff + BLOCK_SIZE; i++)
    {      
      mb_rec[j][i] = (imgpel) iClip1(max_imgpel_value, rshift_rnd_sf((mb_rres[j][i] + ((long)mb_pred[j][i] << DQ_BITS)), DQ_BITS));
    }
  }
}

/*!
************************************************************************
* \brief
*    Inverse residual DPCM for Intra lossless coding
*
************************************************************************
*/
void Inv_Residual_trans_4x4(ImageParameters *img, //!< image parameters
                            ColorPlane pl,        //!< used color plane
                            int ioff,             //!< index to 4x4 block
                            int joff)             //!< index to 4x4 block
{
  int i,j;
  int temp[4][4];
  imgpel **mb_pred = img->mb_pred[pl];
  imgpel **mb_rec  = img->mb_rec[pl];
  int    **mb_rres = img->mb_rres[pl];
  int    **cof     = img->cof[pl];

  if(ipmode_DPCM==VERT_PRED)
  {
    for(i=0; i<4; i++)
    {
      temp[0][i] = cof[joff + 0][ioff + i];
      temp[1][i] = cof[joff + 1][ioff + i] + temp[0][i];
      temp[2][i] = cof[joff + 2][ioff + i] + temp[1][i];
      temp[3][i] = cof[joff + 3][ioff + i] + temp[2][i];
    }

    for(i=0; i<4; i++)
    {
      mb_rres[joff    ][ioff + i]=temp[0][i];
      mb_rres[joff + 1][ioff + i]=temp[1][i];
      mb_rres[joff + 2][ioff + i]=temp[2][i];
      mb_rres[joff + 3][ioff + i]=temp[3][i];
    }
  }
  else if(ipmode_DPCM==HOR_PRED)
  {
    for(j=0; j<4; j++)
    {
      temp[j][0] = cof[joff + j][ioff    ];
      temp[j][1] = cof[joff + j][ioff + 1] + temp[j][0];
      temp[j][2] = cof[joff + j][ioff + 2] + temp[j][1];
      temp[j][3] = cof[joff + j][ioff + 3] + temp[j][2];
    }

    for(j=0; j<4; j++)
    {
      mb_rres[joff + j][ioff    ]=temp[j][0];
      mb_rres[joff + j][ioff + 1]=temp[j][1];
      mb_rres[joff + j][ioff + 2]=temp[j][2];
      mb_rres[joff + j][ioff + 3]=temp[j][3];
    }
  }
  else
  {
    for (j = joff; j < joff + BLOCK_SIZE; j++)
      for (i = ioff; i < ioff + BLOCK_SIZE; i++)
        mb_rres[j][i] = cof[j][i];
  }

  for (j = joff; j < joff + BLOCK_SIZE; j++)
  {
    for (i = ioff; i < ioff + BLOCK_SIZE; i++)
    {
      mb_rec[j][i] = (imgpel) (mb_rres[j][i] + mb_pred[j][i]);
    }
  }
}

/*!
************************************************************************
* \brief
*    Inverse residual DPCM for Intra lossless coding
*
* \par Input:
*    ioff_x,joff_y: Block position inside a macro block (0,8).
************************************************************************
*/
//For residual DPCM
void Inv_Residual_trans_8x8(ImageParameters *img, Macroblock *currMB, ColorPlane pl, int ioff,int joff)
{
  int i, j;
  int temp[8][8];
  imgpel **mb_pred = img->mb_pred[pl];
  imgpel **mb_rec  = img->mb_rec[pl];
  int    **mb_rres = img->mb_rres[pl];

  if(ipmode_DPCM==VERT_PRED)
  {
    for(i=0; i<8; i++)
    {
      temp[0][i] = mb_rres[joff + 0][ioff + i];
      temp[1][i] = mb_rres[joff + 1][ioff + i] + temp[0][i];
      temp[2][i] = mb_rres[joff + 2][ioff + i] + temp[1][i];
      temp[3][i] = mb_rres[joff + 3][ioff + i] + temp[2][i];
      temp[4][i] = mb_rres[joff + 4][ioff + i] + temp[3][i];
      temp[5][i] = mb_rres[joff + 5][ioff + i] + temp[4][i];
      temp[6][i] = mb_rres[joff + 6][ioff + i] + temp[5][i];
      temp[7][i] = mb_rres[joff + 7][ioff + i] + temp[6][i];
    }
    for(i=0; i<8; i++)
    {
      mb_rres[joff  ][ioff+i]=temp[0][i];
      mb_rres[joff+1][ioff+i]=temp[1][i];
      mb_rres[joff+2][ioff+i]=temp[2][i];
      mb_rres[joff+3][ioff+i]=temp[3][i];
      mb_rres[joff+4][ioff+i]=temp[4][i];
      mb_rres[joff+5][ioff+i]=temp[5][i];
      mb_rres[joff+6][ioff+i]=temp[6][i];
      mb_rres[joff+7][ioff+i]=temp[7][i];
    }
  }
  else if(ipmode_DPCM==HOR_PRED)//HOR_PRED

⌨️ 快捷键说明

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