📄 block.c
字号:
return SEARCH_SYNC;
break;
}
return DECODING_OK;
}
/*!
***********************************************************************
* \return
* best SAD
***********************************************************************
*/
int intrapred_luma_16x16(Macroblock *currMB, //!< Current Macroblock
ColorPlane pl, //!< Current colorplane (for 4:4:4)
struct img_par *img, //!< image parameters
int predmode) //!< prediction mode
{
int s0 = 0, s1, s2;
int i,j;
int ih,iv;
int ib,ic,iaa;
int uv = pl-1;
imgpel **imgY = (pl) ? dec_picture->imgUV[uv] : dec_picture->imgY;
imgpel (*mpr)[16] = img->mpr[pl];
imgpel *mpr_line;
imgpel prediction;
PixelPos up; //!< pixel position p(0,-1)
PixelPos left[17]; //!< pixel positions p(-1, -1..15)
int up_avail, left_avail, left_up_avail;
s1=s2=0;
for (i=0;i<17;i++)
{
getNeighbour(currMB, -1 , i-1 , IS_LUMA, &left[i]);
}
getNeighbour(currMB, 0 , -1 , IS_LUMA, &up);
if (!active_pps->constrained_intra_pred_flag)
{
up_avail = up.available;
left_avail = left[1].available;
left_up_avail = left[0].available;
}
else
{
up_avail = up.available ? img->intra_block[up.mb_addr] : 0;
for (i=1, left_avail=1; i<17;i++)
left_avail &= left[i].available ? img->intra_block[left[i].mb_addr]: 0;
left_up_avail = left[0].available ? img->intra_block[left[0].mb_addr]: 0;
}
switch (predmode)
{
case VERT_PRED_16: // vertical prediction from block above
if (!up_avail)
error ("invalid 16x16 intra pred Mode VERT_PRED_16",500);
for(j=0;j<MB_BLOCK_SIZE;j++)
memcpy(&(mpr[j][0]), &(imgY[up.pos_y][up.pos_x]), MB_BLOCK_SIZE * sizeof(imgpel));
break;
case HOR_PRED_16: // horizontal prediction from left block
if (!left_avail)
error ("invalid 16x16 intra pred Mode HOR_PRED_16",500);
for(j=0;j<MB_BLOCK_SIZE;j++)
{
prediction = imgY[left[j+1].pos_y][left[j+1].pos_x];
for(i=0;i<MB_BLOCK_SIZE;i++)
mpr[j][i]= prediction; // store predicted 16x16 block
}
break;
case DC_PRED_16: // DC prediction
s1=s2=0;
for (i=0; i < MB_BLOCK_SIZE; i++)
{
if (up_avail)
s1 += imgY[up.pos_y][up.pos_x+i]; // sum hor pix
if (left_avail)
s2 += imgY[left[i+1].pos_y][left[i+1].pos_x]; // sum vert pix
}
if (up_avail && left_avail)
s0 = (s1+s2+16)>>5; // no edge
if (!up_avail && left_avail)
s0 = (s2+8)>>4; // upper edge
if (up_avail && !left_avail)
s0 = (s1+8)>>4; // left edge
if (!up_avail && !left_avail)
s0 = img->dc_pred_value_comp[pl]; // top left corner, nothing to predict from
for(j=0;j<MB_BLOCK_SIZE;j++)
for(i=0;i<MB_BLOCK_SIZE;i++)
{
mpr[j][i]=(imgpel) s0;
}
break;
case PLANE_16:// 16 bit integer plan pred
if (!up_avail || !left_up_avail || !left_avail)
error ("invalid 16x16 intra pred Mode PLANE_16",500);
ih=0;
iv=0;
mpr_line = &imgY[up.pos_y][up.pos_x+7];
for (i = 1; i < 8; i++)
{
ih += i*(mpr_line[i] - mpr_line[-i]);
iv += i*(imgY[left[8+i].pos_y][left[8+i].pos_x] - imgY[left[8-i].pos_y][left[8-i].pos_x]);
}
ih += 8*(mpr_line[8] - imgY[left[0].pos_y][left[0].pos_x]);
iv += 8*(imgY[left[16].pos_y][left[16].pos_x] - imgY[left[0].pos_y][left[0].pos_x]);
ib=(5*ih+32)>>6;
ic=(5*iv+32)>>6;
iaa=16*(mpr_line[8] + imgY[left[16].pos_y][left[16].pos_x]);
for (j=0;j< MB_BLOCK_SIZE;j++)
{
for (i=0;i< MB_BLOCK_SIZE;i++)
{
mpr[j][i]=(imgpel) iClip1(img->max_imgpel_value,((iaa+(i-7)*ib +(j-7)*ic + 16)>>5));
}
}// store plane prediction
break;
default:
{ // indication of fault in bitstream,exit
printf("illegal 16x16 intra prediction mode input: %d\n",predmode);
return SEARCH_SYNC;
}
}
return DECODING_OK;
}
void intrapred_chroma(Macroblock *currMB, struct img_par *img, int uv)
{
int i,j, ii, jj, ioff, joff;
imgpel ***imgUV = dec_picture->imgUV;
imgpel (*mpr)[16] = img->mpr[uv + 1];
int js[4][4];
int pred;
int ih, iv, ib, ic, iaa;
int b8, b4;
int yuv = dec_picture->chroma_format_idc - 1;
int blk_x, blk_y;
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}}
};
int s0, s1, s2, s3;
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;
for (i=0; i < cr_MB_y + 1 ; i++)
{
getNeighbour(currMB, -1, i-1, IS_CHROMA, &left[i]);
}
getNeighbour(currMB, 0, -1, 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_y >> 1);i++)
left_avail[0] &= left[i + 1].available ? img->intra_block[left[i+1].mb_addr]: 0;
for (i = (cr_MB_y >> 1), 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;
}
if (currMB->c_ipred_mode == DC_PRED_8)
{
// 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] + 1;
blk_x = subblk_offset_x[yuv][b8][b4];
s0=s1=s2=s3=0;
js[b8][b4] = img->dc_pred_value_comp[1];
//===== get prediction value =====
switch (block_pos[yuv][b8][b4])
{
case 0: //===== TOP LEFT =====
if (up_avail) for (i=blk_x;i<(blk_x+4);i++) s0 += imgUV[uv][up.pos_y][up.pos_x + i];
if (left_avail[0]) for (i=blk_y;i<(blk_y+4);i++) s2 += imgUV[uv][left[i].pos_y][left[i].pos_x];
if (up_avail && left_avail[0]) js[b8][b4] = (s0+s2+4) >> 3;
else if (up_avail) js[b8][b4] = (s0 +2) >> 2;
else if (left_avail[0]) js[b8][b4] = (s2 +2) >> 2;
break;
case 1: //===== TOP RIGHT =====
if (up_avail) for (i=blk_x;i<(blk_x+4);i++) s1 += imgUV[uv][up.pos_y][up.pos_x + i];
else if (left_avail[0]) for (i=blk_y;i<(blk_y+4);i++) s2 += imgUV[uv][left[i].pos_y][left[i].pos_x];
if (up_avail) js[b8][b4] = (s1 +2) >> 2;
else if (left_avail[0]) js[b8][b4] = (s2 +2) >> 2;
break;
case 2: //===== BOTTOM LEFT =====
if (left_avail[1]) for (i=blk_y;i<(blk_y+4);i++) s3 += imgUV[uv][left[i].pos_y][left[i].pos_x];
else if (up_avail) for (i=blk_x;i<(blk_x+4);i++) s0 += imgUV[uv][up.pos_y][up.pos_x + i];
if (left_avail[1]) js[b8][b4] = (s3 +2) >> 2;
else if (up_avail) js[b8][b4] = (s0 +2) >> 2;
break;
case 3: //===== BOTTOM RIGHT =====
if (up_avail) for (i=blk_x;i<(blk_x+4);i++) s1 += imgUV[uv][up.pos_y][up.pos_x + i];
if (left_avail[1]) for (i=blk_y;i<(blk_y+4);i++) s3 += imgUV[uv][left[i].pos_y][left[i].pos_x];
if (up_avail && left_avail[1]) js[b8][b4] = (s1+s3+4) >> 3;
else if (up_avail) js[b8][b4] = (s1 +2) >> 2;
else if (left_avail[1]) js[b8][b4] = (s3 +2) >> 2;
break;
}
}
}
}
if (PLANE_8 == currMB->c_ipred_mode)
{
int cr_MB_y2 = (cr_MB_y >> 1);
int cr_MB_x2 = (cr_MB_x >> 1);
// plane prediction
if (!left_up_avail || !left_avail[0] || !left_avail[1] || !up_avail)
error("unexpected PLANE_8 chroma intra prediction mode",-1);
ih = cr_MB_x2 * (imgUV[uv][up.pos_y][up.pos_x + cr_MB_x - 1] - imgUV[uv][left[0].pos_y][left[0].pos_x]);
for (i = 0; i < cr_MB_x2 - 1; i++)
ih += (i + 1) * (imgUV[uv][up.pos_y][up.pos_x + cr_MB_x2 + i] -
imgUV[uv][up.pos_y][up.pos_x + cr_MB_x2 - 2 - i]);
iv = cr_MB_y2 * (imgUV[uv][left[cr_MB_y].pos_y][left[cr_MB_y].pos_x] - imgUV[uv][left[0].pos_y][left[0].pos_x]);
for (i = 0; i < cr_MB_y2 - 1; i++)
iv += (i + 1)*(imgUV[uv][left[cr_MB_y2 + 1 + i].pos_y][left[cr_MB_y2 + 1 + i].pos_x] -
imgUV[uv][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[uv][left[cr_MB_y].pos_y][left[cr_MB_y].pos_x] +
imgUV[uv][up.pos_y][up.pos_x+cr_MB_x-1]);
for (j=0; j<cr_MB_y; j++)
for (i=0; i<cr_MB_x; i++)
mpr[j][i]=(imgpel) iClip1(img->max_imgpel_value_uv,((iaa + (i - cr_MB_x2 + 1) * ib + (j - cr_MB_y2 + 1) * ic + 16) >> 5)); }
else
{
switch (currMB->c_ipred_mode)
{
case DC_PRED_8:
for (b8=0; b8 < (img->num_uv_blocks) ;b8++)
{
for (b4=0;b4<4;b4++)
{
joff = subblk_offset_y[yuv][b8][b4];
ioff = subblk_offset_x[yuv][b8][b4];
for (jj=joff; jj<joff + BLOCK_SIZE; jj++)
for (ii=ioff; ii<ioff + BLOCK_SIZE; ii++)
{
mpr[jj][ii]=(imgpel) js[b8][b4];
}
}
}
break;
case HOR_PRED_8:
if (!left_avail[0] || !left_avail[1])
error("unexpected HOR_PRED_8 chroma intra prediction mode",-1);
for (j=0;j<2;j++)
{
joff = (j * cr_MB_y) >> 1;
for(i=0;i<2;i++)
{
ioff = (i * cr_MB_x) >> 1;
for (jj=joff; jj<joff + (cr_MB_y >> 1); jj++)
{
pred = imgUV[uv][left[1+jj].pos_y][left[1+jj].pos_x];
for (ii=ioff; ii<ioff + (cr_MB_x >> 1); ii++)
mpr[jj][ii]=(imgpel) pred;
}
}
}
break;
case VERT_PRED_8:
if (!up_avail)
error("unexpected VERT_PRED_8 chroma intra prediction mode",-1);
for (j=0;j<2;j++)
{
joff = (j * cr_MB_y) >> 1;
for(i=0;i<2;i++)
{
ioff = (i * cr_MB_x) >> 1;
for (jj=joff; jj < joff + (cr_MB_y >> 1); jj++)
memcpy(&(mpr[jj][ioff]), &(imgUV[uv][up.pos_y][up.pos_x+ioff]), (cr_MB_x >> 1) * sizeof(imgpel));
/*
for (ii=ioff; ii<ioff + (cr_MB_x >> 1); ii++)
{
pred = imgUV[uv][up.pos_y][up.pos_x+ii];
for (jj=joff; jj<joff + (cr_MB_y >> 1); jj++)
mpr[jj][ii]=(imgpel) pred;
}
*/
}
}
break;
default:
error("illegal chroma intra prediction mode", 600);
break;
}
}
}
/*!
***********************************************************************
* \brief
* Inverse 4x4 transformation, transforms cof to m7
***********************************************************************
*/
void itrans4x4(struct img_par *img, //!< image parameters
int ioff, //!< index to 4x4 block
int joff, //!<
int yuv)
{
int i,j;
Boolean lossless_qpprime = (Boolean) (((img->qp + img->bitdepth_luma_qp_scale) == 0) && (img->lossless_qpprime_flag == 1));
int max_imgpel_value = yuv ? img->max_imgpel_value_uv : img->max_imgpel_value;
imgpel (*mpr)[16] = img->mpr[yuv];
int (*m7)[16] = img->m7[yuv];
if (!lossless_qpprime)
{
// We need to clean this up but for the time being memcpy works fine
for (j = 0; j < BLOCK_SIZE; j++)
{
memcpy(&m7[j + joff][ioff],&img->cof[yuv][j + joff][ioff], BLOCK_SIZE * sizeof(int));
}
inverse4x4(m7,m7,joff,ioff);
for (j = joff; j < joff + BLOCK_SIZE; j++)
for (i = ioff; i < ioff + BLOCK_SIZE; i++)
{
m7[j][i] = iClip1(max_imgpel_value, rshift_rnd_sf((m7[j][i] + ((long)mpr[j][i] << DQ_BITS)), DQ_BITS));
}
}
else
{
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
m7[j + joff][i + ioff] = iClip1(max_imgpel_value, (img->cof[yuv][j + joff][i + ioff] + (long)mpr[j + joff][i + ioff]));
}
}
/*!
************************************************************************
* \brief
* Inverse residual DPCM for Intra lossless coding
*
* \par Input:
* ioff_x,joff_y: Block position inside a macro block (0,4,8,12).
************************************************************************
*/ //For residual DPCM
int Inv_Residual_trans_4x4(struct img_par *img, //!< image parameters
int ioff, //!< index to 4x4 block
int joff,
int i0,
int j0,
int chroma,
int yuv)
{
int i,j;
int temp[4][4];
imgpel (*mpr)[16] = img->mpr[yuv];
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
img->m7[yuv][joff+j][ioff+i] = img->cof[yuv][joff+j][ioff+i];
if(ipmode_DPCM==VERT_PRED)
{
for(i=0; i<4; i++)
{
temp[1][i]=img->m7[yuv][joff+0][ioff+i]+img->m7[yuv][joff+1][ioff+i];
temp[2][i]=img->m7[yuv][joff+0][ioff+i]+img->m7[yuv][joff+1][ioff+i]+img->m7[yuv][joff+2][ioff+i];
temp[3][i]=img->m7[yuv][joff+0][ioff+i]+img->m7[yuv][joff+1][ioff+i]+img->m7[yuv][joff+2][ioff+i]+img->m7[yuv][joff+3][ioff+i];
}
for(i=0; i<4; i++)
{
img->m7[yuv][joff+1][ioff+i]=temp[1][i];
img->m7[yuv][joff+2][ioff+i]=temp[2][i];
img->m7[yuv][joff+3][ioff+i]=temp[3][i];
}
}
else if(ipmode_DPCM==HOR_PRED)
{
for(j=0; j<4; j++)
{
temp[j][1]=img->m7[yuv][joff+j][ioff+0]+img->m7[yuv][joff+j][ioff+1];
temp[j][2]=img->m7[yuv][joff+j][ioff+0]+img->m7[yuv][joff+j][ioff+1]+img->m7[yuv][joff+j][ioff+2];
temp[j][3]=img->m7[yuv][joff+j][ioff+0]+img->m7[yuv][joff+j][ioff+1]+img->m7[yuv][joff+j][ioff+2]+img->m7[yuv][joff+j][ioff+3];
}
for(j=0; j<4; j++)
{
img->m7[yuv][joff+j][ioff+1]=temp[j][1];
img->m7[yuv][joff+j][ioff+2]=temp[j][2];
img->m7[yuv][joff+j][ioff+3]=temp[j][3];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -