📄 block.c
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose. In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/
/*!
*************************************************************************************
* \file block.c
*
* \brief
* Process one block
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Inge Lille-Lang鴜 <inge.lille-langoy@telenor.com>
* - Rickard Sjoberg <rickard.sjoberg@era.ericsson.se>
* - Stephan Wenger <stewe@cs.tu-berlin.de>
* - Jani Lainema <jani.lainema@nokia.com>
* - Detlev Marpe <marpe@hhi.de>
* - Thomas Wedi <wedi@tnt.uni-hannover.de>
* - Ragip Kurceren <ragip.kurceren@nokia.com>
*************************************************************************************
*/
#include "contributors.h"
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "block.h"
#include "refbuf.h"
/*!
************************************************************************
* \brief
* Make intra 4x4 prediction according to all 6 prediction modes.
* The routine uses left and upper neighbouring points from
* previous coded blocks to do this (if available). Notice that
* inaccessible neighbouring points are signalled with a negative
* value i the predmode array .
*
* \para Input:
* Starting point of current 4x4 block image posision
*
* \para Output:
* none
************************************************************************
*/
void intrapred_luma(int img_x,int img_y)
{
int i,j,s0=0,s1,s2,ia[7][3],s[4][2];
int block_available_up = (img->ipredmode[img_x/BLOCK_SIZE+1][img_y/BLOCK_SIZE] >=0);
int block_available_left = (img->ipredmode[img_x/BLOCK_SIZE][img_y/BLOCK_SIZE+1] >=0);
s1=0;
s2=0;
// make DC prediction
for (i=0; i < BLOCK_SIZE; i++)
{
if (block_available_up)
s1 += imgY[img_y-1][img_x+i]; // sum hor pix
if (block_available_left)
s2 += imgY[img_y+i][img_x-1]; // sum vert pix
}
if (block_available_up && block_available_left)
s0=(s1+s2+4)/(2*BLOCK_SIZE); // no edge
if (!block_available_up && block_available_left)
s0=(s2+2)/BLOCK_SIZE; // upper edge
if (block_available_up && !block_available_left)
s0=(s1+2)/BLOCK_SIZE; // left edge
if (!block_available_up && !block_available_left)
s0=128; // top left corner, nothing to predict from
for (i=0; i < BLOCK_SIZE; i++)
{
// vertical prediction
if (block_available_up)
s[i][0]=imgY[img_y-1][img_x+i];
// horizontal prediction
if (block_available_left)
s[i][1]=imgY[img_y+i][img_x-1];
}
for (j=0; j < BLOCK_SIZE; j++)
{
for (i=0; i < BLOCK_SIZE; i++)
{
img->mprr[DC_PRED][i][j]=s0; // store DC prediction
img->mprr[VERT_PRED][i][j]=s[j][0]; // store vertical prediction
img->mprr[HOR_PRED][i][j]=s[i][1]; // store horizontal prediction
}
}
// Prediction according to 'diagonal' modes
if (block_available_up && block_available_left)
{
int A = imgY[img_y-1][img_x];
int B = imgY[img_y-1][img_x+1];
int C = imgY[img_y-1][img_x+2];
int D = imgY[img_y-1][img_x+3];
int E = imgY[img_y ][img_x-1];
int F = imgY[img_y+1][img_x-1];
int G = imgY[img_y+2][img_x-1];
int H = imgY[img_y+3][img_x-1];
int I = imgY[img_y-1][img_x-1];
ia[0][0]=(H+2*G+F+2)/4;
ia[1][0]=(G+2*F+E+2)/4;
ia[2][0]=(F+2*E+I+2)/4;
ia[3][0]=(E+2*I+A+2)/4;
ia[4][0]=(I+2*A+B+2)/4;
ia[5][0]=(A+2*B+C+2)/4;
ia[6][0]=(B+2*C+D+2)/4;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
img->mprr[DIAG_PRED_LR_45][i][j]=ia[j-i+3][0];
}
if (block_available_up)
{ // Do prediction 1
int A = imgY[img_y-1][img_x+0];
int B = imgY[img_y-1][img_x+1];
int C = imgY[img_y-1][img_x+2];
int D = imgY[img_y-1][img_x+3];
img->mprr[DIAG_PRED_RL][0][0] = (A+B)/2; // a
img->mprr[DIAG_PRED_RL][1][0] = B; // e
img->mprr[DIAG_PRED_RL][0][1] = img->mprr[DIAG_PRED_RL][2][0] = (B+C)/2; // b i
img->mprr[DIAG_PRED_RL][1][1] = img->mprr[DIAG_PRED_RL][3][0] = C; // f m
img->mprr[DIAG_PRED_RL][0][2] = img->mprr[DIAG_PRED_RL][2][1] = (C+D)/2; // c j
img->mprr[DIAG_PRED_RL][3][1] =
img->mprr[DIAG_PRED_RL][1][2] =
img->mprr[DIAG_PRED_RL][2][2] =
img->mprr[DIAG_PRED_RL][3][2] =
img->mprr[DIAG_PRED_RL][0][3] =
img->mprr[DIAG_PRED_RL][1][3] =
img->mprr[DIAG_PRED_RL][2][3] =
img->mprr[DIAG_PRED_RL][3][3] = D; // d g h k l n o p
}
if (block_available_left)
{ // Do prediction 5
int E = imgY[img_y+0][img_x-1];
int F = imgY[img_y+1][img_x-1];
int G = imgY[img_y+2][img_x-1];
int H = imgY[img_y+3][img_x-1];
img->mprr[DIAG_PRED_LR][0][0] = (E+F)/2; // a
img->mprr[DIAG_PRED_LR][0][1] = F; // b
img->mprr[DIAG_PRED_LR][1][0] = img->mprr[DIAG_PRED_LR][0][2] = (F+G)/2; // e c
img->mprr[DIAG_PRED_LR][1][1] = img->mprr[DIAG_PRED_LR][0][3] = G; // f d
img->mprr[DIAG_PRED_LR][2][0] = img->mprr[DIAG_PRED_LR][1][2] = (G+H)/2; // i g
img->mprr[DIAG_PRED_LR][1][3] =
img->mprr[DIAG_PRED_LR][2][1] =
img->mprr[DIAG_PRED_LR][2][2] =
img->mprr[DIAG_PRED_LR][2][3] =
img->mprr[DIAG_PRED_LR][3][0] =
img->mprr[DIAG_PRED_LR][3][1] =
img->mprr[DIAG_PRED_LR][3][2] =
img->mprr[DIAG_PRED_LR][3][3] = H;
}
}
/*!
************************************************************************
* \brief
* 16x16 based luma prediction
*
* \para Input:
* Image parameters
*
* \para Output:
* none
************************************************************************
*/
void intrapred_luma_2()
{
int s0=0,s1,s2;
int i,j;
int s[16][2];
int ih,iv;
int ib,ic,iaa;
int mb_nr = img->current_mb_nr;
int mb_width = img->width/16;
int mb_available_up = (img->mb_y == 0) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width].slice_nr);
int mb_available_left = (img->mb_x == 0) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-1].slice_nr);
if(input->UseConstrainedIntraPred)
{
if (mb_available_up && (img->intra_mb[mb_nr-mb_width] ==0))
mb_available_up = 0;
if (mb_available_left && (img->intra_mb[mb_nr-1] ==0))
mb_available_left = 0;
}
s1=s2=0;
// make DC prediction
for (i=0; i < MB_BLOCK_SIZE; i++)
{
if (mb_available_up)
s1 += imgY[img->pix_y-1][img->pix_x+i]; // sum hor pix
if (mb_available_left)
s2 += imgY[img->pix_y+i][img->pix_x-1]; // sum vert pix
}
if (mb_available_up && mb_available_left)
s0=(s1+s2+16)/(2*MB_BLOCK_SIZE); // no edge
if (!mb_available_up && mb_available_left)
s0=(s2+8)/MB_BLOCK_SIZE; // upper edge
if (mb_available_up && !mb_available_left)
s0=(s1+8)/MB_BLOCK_SIZE; // left edge
if (!mb_available_up && !mb_available_left)
s0=128; // top left corner, nothing to predict from
for (i=0; i < MB_BLOCK_SIZE; i++)
{
// vertical prediction
if (mb_available_up)
s[i][0]=imgY[img->pix_y-1][img->pix_x+i];
// horizontal prediction
if (mb_available_left)
s[i][1]=imgY[img->pix_y+i][img->pix_x-1];
}
for (j=0; j < MB_BLOCK_SIZE; j++)
{
for (i=0; i < MB_BLOCK_SIZE; i++)
{
img->mprr_2[VERT_PRED_16][j][i]=s[i][0]; // store vertical prediction
img->mprr_2[HOR_PRED_16 ][j][i]=s[j][1]; // store horizontal prediction
img->mprr_2[DC_PRED_16 ][j][i]=s0; // store DC prediction
}
}
if (!mb_available_up || !mb_available_left) // edge
return;
// 16 bit integer plan pred
ih=0;
iv=0;
for (i=1;i<9;i++)
{
ih += i*(imgY[img->pix_y-1][img->pix_x+7+i] - imgY[img->pix_y-1][img->pix_x+7-i]);
iv += i*(imgY[img->pix_y+7+i][img->pix_x-1] - imgY[img->pix_y+7-i][img->pix_x-1]);
}
ib=5*(ih/4)/16;
ic=5*(iv/4)/16;
iaa=16*(imgY[img->pix_y-1][img->pix_x+15]+imgY[img->pix_y+15][img->pix_x-1]);
for (j=0;j< MB_BLOCK_SIZE;j++)
{
for (i=0;i< MB_BLOCK_SIZE;i++)
{
img->mprr_2[PLANE_16][j][i]=max(0,min(255,(iaa+(i-7)*ib +(j-7)*ic + 16)/32));// store plane prediction
}
}
}
/*!
************************************************************************
* \brief
* For new intra pred routines
*
* \para Input:
* Image par, 16x16 based intra mode
*
* \para Output:
* none
************************************************************************
*/
void dct_luma2(int new_intra_mode)
{
#ifndef NO_RDQUANT
int jq0;
#endif
#ifdef NO_RDQUANT
int qp_const;
#endif
int i,j;
int ii,jj;
int i1,j1;
int M1[16][16];
int M4[4][4];
int M5[4],M6[4];
int M0[4][4][4][4];
#ifndef NO_RDQUANT
int coeff[16];
#endif
int quant_set,run,scan_pos,coeff_ctr,level;
#ifndef NO_RDQUANT
jq0=JQQ3;
#endif
#ifdef NO_RDQUANT
qp_const = JQQ3;
#endif
for (j=0;j<16;j++)
{
for (i=0;i<16;i++)
{
M1[i][j]=imgY_org[img->pix_y+j][img->pix_x+i]-img->mprr_2[new_intra_mode][j][i];
M0[i%4][i/4][j%4][j/4]=M1[i][j];
}
}
for (jj=0;jj<4;jj++)
{
for (ii=0;ii<4;ii++)
{
for (j=0;j<4;j++)
{
for (i=0;i<2;i++)
{
i1=3-i;
M5[i]= M0[i][ii][j][jj]+M0[i1][ii][j][jj];
M5[i1]= M0[i][ii][j][jj]-M0[i1][ii][j][jj];
}
M0[0][ii][j][jj]=(M5[0]+M5[1])*13;
M0[2][ii][j][jj]=(M5[0]-M5[1])*13;
M0[1][ii][j][jj]=M5[3]*17+M5[2]*7;
M0[3][ii][j][jj]=M5[3]*7-M5[2]*17;
}
// vertical
for (i=0;i<4;i++)
{
for (j=0;j<2;j++)
{
j1=3-j;
M5[j] = M0[i][ii][j][jj]+M0[i][ii][j1][jj];
M5[j1]= M0[i][ii][j][jj]-M0[i][ii][j1][jj];
}
M0[i][ii][0][jj]=(M5[0]+M5[1])*13;
M0[i][ii][2][jj]=(M5[0]-M5[1])*13;
M0[i][ii][1][jj]= M5[3]*17+M5[2]*7;
M0[i][ii][3][jj]= M5[3]*7 -M5[2]*17;
}
}
}
// pick out DC coeff
for (j=0;j<4;j++)
for (i=0;i<4;i++)
M4[i][j]= 49 * M0[0][i][0][j]/32768;
for (j=0;j<4;j++)
{
for (i=0;i<2;i++)
{
i1=3-i;
M5[i]= M4[i][j]+M4[i1][j];
M5[i1]=M4[i][j]-M4[i1][j];
}
M4[0][j]=(M5[0]+M5[1])*13;
M4[2][j]=(M5[0]-M5[1])*13;
M4[1][j]= M5[3]*17+M5[2]*7;
M4[3][j]= M5[3]*7 -M5[2]*17;
}
// vertical
for (i=0;i<4;i++)
{
for (j=0;j<2;j++)
{
j1=3-j;
M5[j]= M4[i][j]+M4[i][j1];
M5[j1]=M4[i][j]-M4[i][j1];
}
M4[i][0]=(M5[0]+M5[1])*13;
M4[i][2]=(M5[0]-M5[1])*13;
M4[i][1]= M5[3]*17+M5[2]*7;
M4[i][3]= M5[3]*7 -M5[2]*17;
}
// quant
quant_set=img->qp;
run=-1;
scan_pos=0;
#ifndef NO_RDQUANT
for (coeff_ctr=0;coeff_ctr<16;coeff_ctr++)
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
coeff[coeff_ctr]=M4[i][j];
}
rd_quant(QUANT_LUMA_SNG,coeff);
for (coeff_ctr=0;coeff_ctr<16;coeff_ctr++)
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
run++;
level=abs(coeff[coeff_ctr]);
if (level != 0)
{
img->cof[0][0][scan_pos][0][1]=sign(level,M4[i][j]);
img->cof[0][0][scan_pos][1][1]=run;
++scan_pos;
run=-1;
}
#endif
#ifdef NO_RDQUANT
for (coeff_ctr=0;coeff_ctr<16;coeff_ctr++)
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
run++;
level= (abs(M4[i][j]) * JQ[quant_set][0]+qp_const)/JQQ1;
if (level != 0)
{
img->cof[0][0][scan_pos][0][1]=sign(level,M4[i][j]);
img->cof[0][0][scan_pos][1][1]=run;
++scan_pos;
run=-1;
}
#endif
M4[i][j]=sign(level,M4[i][j]);
}
img->cof[0][0][scan_pos][0][1]=0;
// invers DC transform
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -