📄 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"
#define Q_BITS 16
#define DQ_BITS 6
#define DQ_ROUND (1<<(DQ_BITS-1))
static const int quant_coef[6][4][4] = {
{{16384,10486,16384,10486},{10486, 6453,10486, 6453},{16384,10486,16384,10486},{10486, 6453,10486, 6453}},
{{14564, 9118,14564, 9118},{ 9118, 5785, 9118, 5785},{14564, 9118,14564, 9118},{ 9118, 5785, 9118, 5785}},
{{13107, 8389,13107, 8389},{ 8389, 5243, 8389, 5243},{13107, 8389,13107, 8389},{ 8389, 5243, 8389, 5243}},
{{11916, 7490,11916, 7490},{ 7490, 4660, 7490, 4660},{11916, 7490,11916, 7490},{ 7490, 4660, 7490, 4660}},
{{10486, 6554,10486, 6554},{ 6554, 4194, 6554, 4194},{10486, 6554,10486, 6554},{ 6554, 4194, 6554, 4194}},
{{9362, 5825, 9362, 5825},{ 5825, 3728, 5825, 3728},{ 9362, 5825, 9362, 5825},{ 5825, 3728, 5825, 3728}}
};
static const int dequant_coef[6][4][4] = {
{{16,20,16,20},{20,26,20,26},{16,20,16,20},{20,26,20,26}},
{{18,23,18,23},{23,29,23,29},{18,23,18,23},{23,29,23,29}},
{{20,25,20,25},{25,32,25,32},{20,25,20,25},{25,32,25,32}},
{{22,28,22,28},{28,36,28,36},{22,28,22,28},{28,36,28,36}},
{{25,32,25,32},{32,40,32,40},{25,32,25,32},{32,40,32,40}},
{{28,36,28,36},{36,45,36,45},{28,36,28,36},{36,45,36,45}},
};
#ifndef USE_6_INTRA_MODES
// 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..P, 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
// 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])
#define P_M (PredPel[13])
#define P_N (PredPel[14])
#define P_O (PredPel[15])
#define P_P (PredPel[16])
/*!
************************************************************************
* \brief
* Make intra 4x4 prediction according to all 9 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 in 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;
int s0;
int PredPel[17]; // array of predictor pels
int block_available_up = (img->ipredmode[img_x/BLOCK_SIZE+1][img_y/BLOCK_SIZE] >=0);
int block_available_up_right = (img->ipredmode[img_x/BLOCK_SIZE+2][img_y/BLOCK_SIZE] >=0);
int block_available_left = (img->ipredmode[img_x/BLOCK_SIZE][img_y/BLOCK_SIZE+1] >=0);
int block_available_left_down = (img->ipredmode[img_x/BLOCK_SIZE][img_y/BLOCK_SIZE+2] >=0);
i = (img_x & 15);
j = (img_y & 15);
if (block_available_up_right)
{
if ((i == 4 && j == 4) ||
(i == 12 && j == 4) ||
(i == 12 && j == 8) ||
(i == 4 && j == 12) ||
(i == 12 && j == 12))
{
block_available_up_right = 0;
}
}
if (block_available_left_down)
{
if (!(i == 0 && j == 0) &&
!(i == 8 && j == 0) &&
!(i == 0 && j == 4) &&
!(i == 0 && j == 8) &&
!(i == 8 && j == 8))
{
block_available_left_down = 0;
}
}
// form predictor pels
if (block_available_up)
{
P_A = imgY[img_y-1][img_x+0];
P_B = imgY[img_y-1][img_x+1];
P_C = imgY[img_y-1][img_x+2];
P_D = imgY[img_y-1][img_x+3];
if (block_available_up_right)
{
P_E = imgY[img_y-1][img_x+4];
P_F = imgY[img_y-1][img_x+5];
P_G = imgY[img_y-1][img_x+6];
P_H = imgY[img_y-1][img_x+7];
}
else
{
P_E = P_F = P_G = P_H = P_D;
}
}
else
{
P_A = P_B = P_C = P_D = P_E = P_F = P_G = P_H = 128;
}
if (block_available_left)
{
P_I = imgY[img_y+0][img_x-1];
P_J = imgY[img_y+1][img_x-1];
P_K = imgY[img_y+2][img_x-1];
P_L = imgY[img_y+3][img_x-1];
if (block_available_left_down)
{
P_M = imgY[img_y+4][img_x-1];
P_N = imgY[img_y+5][img_x-1];
P_O = imgY[img_y+6][img_x-1];
P_P = imgY[img_y+7][img_x-1];
}
else
{
P_M = P_N = P_O = P_P = P_L;
}
}
else
{
P_I = P_J = P_K = P_L = P_M = P_N = P_O = P_P = 128;
}
// XXXGJC -- not quite right for this boundary
if (block_available_up && block_available_left)
{
P_X = imgY[img_y-1][img_x-1];
}
else
{
P_X = 128;
}
///////////////////////////////
// make DC prediction
///////////////////////////////
s0 = 0;
if (block_available_up && block_available_left)
{
// no edge
s0 = (P_A + P_B + P_C + P_D + P_I + P_J + P_K + P_L + 4)/(2*BLOCK_SIZE);
}
else if (!block_available_up && block_available_left)
{
// upper edge
s0 = (P_I + P_J + P_K + P_L + 2)/BLOCK_SIZE;
}
else if (block_available_up && !block_available_left)
{
// left edge
s0 = (P_A + P_B + P_C + P_D + 2)/BLOCK_SIZE;
}
else //if (!block_available_up && !block_available_left)
{
// top left corner, nothing to predict from
s0 = 128;
}
for (j=0; j < BLOCK_SIZE; j++)
{
for (i=0; i < BLOCK_SIZE; i++)
{
// store DC prediction
img->mprr[DC_PRED][i][j] = s0;
}
}
///////////////////////////////
// make horiz and vert prediction
///////////////////////////////
for (i=0; i < BLOCK_SIZE; i++)
{
img->mprr[VERT_PRED][0][i] =
img->mprr[VERT_PRED][1][i] =
img->mprr[VERT_PRED][2][i] =
img->mprr[VERT_PRED][3][i] = (&P_A)[i];
img->mprr[HOR_PRED][i][0] =
img->mprr[HOR_PRED][i][1] =
img->mprr[HOR_PRED][i][2] =
img->mprr[HOR_PRED][i][3] = (&P_I)[i];
}
/* Prediction according to 'diagonal' modes */
if (block_available_up && block_available_left)
{
// Mode DIAG_PRED_SE
img->mprr[DIAG_PRED_SE][3][0] = (P_L + 2*P_K + P_J + 2) / 4;
img->mprr[DIAG_PRED_SE][2][0] =
img->mprr[DIAG_PRED_SE][3][1] = (P_K + 2*P_J + P_I + 2) / 4;
img->mprr[DIAG_PRED_SE][1][0] =
img->mprr[DIAG_PRED_SE][2][1] =
img->mprr[DIAG_PRED_SE][3][2] = (P_J + 2*P_I + P_X + 2) / 4;
img->mprr[DIAG_PRED_SE][0][0] =
img->mprr[DIAG_PRED_SE][1][1] =
img->mprr[DIAG_PRED_SE][2][2] =
img->mprr[DIAG_PRED_SE][3][3] = (P_I + 2*P_X + P_A + 2) / 4;
img->mprr[DIAG_PRED_SE][0][1] =
img->mprr[DIAG_PRED_SE][1][2] =
img->mprr[DIAG_PRED_SE][2][3] = (P_X + 2*P_A + P_B + 2) / 4;
img->mprr[DIAG_PRED_SE][0][2] =
img->mprr[DIAG_PRED_SE][1][3] = (P_A + 2*P_B + P_C + 2) / 4;
img->mprr[DIAG_PRED_SE][0][3] = (P_B + 2*P_C + P_D + 2) / 4;
// Mode DIAG_PRED_NE
img->mprr[DIAG_PRED_NE][0][0] = (P_A + P_C + P_I + P_K + 2*(P_B + P_J) + 4) / 8;
img->mprr[DIAG_PRED_NE][0][1] =
img->mprr[DIAG_PRED_NE][1][0] = (P_B + P_D + P_J + P_L + 2*(P_C + P_K) + 4) / 8;
img->mprr[DIAG_PRED_NE][0][2] =
img->mprr[DIAG_PRED_NE][1][1] =
img->mprr[DIAG_PRED_NE][2][0] = (P_C + P_E + P_K + P_M + 2*(P_D + P_L) + 4) / 8;
img->mprr[DIAG_PRED_NE][0][3] =
img->mprr[DIAG_PRED_NE][1][2] =
img->mprr[DIAG_PRED_NE][2][1] =
img->mprr[DIAG_PRED_NE][3][0] = (P_D + P_F + P_L + P_N + 2*(P_E + P_M) + 4) / 8;
img->mprr[DIAG_PRED_NE][1][3] =
img->mprr[DIAG_PRED_NE][2][2] =
img->mprr[DIAG_PRED_NE][3][1] = (P_E + P_G + P_M + P_O + 2*(P_F + P_N) + 4) / 8;
img->mprr[DIAG_PRED_NE][2][3] =
img->mprr[DIAG_PRED_NE][3][2] = (P_F + P_H + P_N + P_P + 2*(P_G + P_O) + 4) / 8;
img->mprr[DIAG_PRED_NE][3][3] = (P_G + P_O + P_H + P_P + 2) / 4;
// Mode DIAG_PRED_SSE
img->mprr[DIAG_PRED_SSE][0][0] =
img->mprr[DIAG_PRED_SSE][2][1] = (P_X + P_A + 1) / 2;
img->mprr[DIAG_PRED_SSE][0][1] =
img->mprr[DIAG_PRED_SSE][2][2] = (P_A + P_B + 1) / 2;
img->mprr[DIAG_PRED_SSE][0][2] =
img->mprr[DIAG_PRED_SSE][2][3] = (P_B + P_C + 1) / 2;
img->mprr[DIAG_PRED_SSE][0][3] = (P_C + P_D + 1) / 2;
img->mprr[DIAG_PRED_SSE][1][0] =
img->mprr[DIAG_PRED_SSE][3][1] = (P_I + 2*P_X + P_A + 2) / 4;
img->mprr[DIAG_PRED_SSE][1][1] =
img->mprr[DIAG_PRED_SSE][3][2] = (P_X + 2*P_A + P_B + 2) / 4;
img->mprr[DIAG_PRED_SSE][1][2] =
img->mprr[DIAG_PRED_SSE][3][3] = (P_A + 2*P_B + P_C + 2) / 4;
img->mprr[DIAG_PRED_SSE][1][3] = (P_B + 2*P_C + P_D + 2) / 4;
img->mprr[DIAG_PRED_SSE][2][0] = (P_X + 2*P_I + P_J + 2) / 4;
img->mprr[DIAG_PRED_SSE][3][0] = (P_I + 2*P_J + P_K + 2) / 4;
// Mode DIAG_PRED_NNE
img->mprr[DIAG_PRED_NNE][0][0] = (2*(P_A + P_B + P_K) + P_J + P_L + 4) / 8;
img->mprr[DIAG_PRED_NNE][0][1] =
img->mprr[DIAG_PRED_NNE][2][0] = (P_B + P_C + 1) / 2;
img->mprr[DIAG_PRED_NNE][0][2] =
img->mprr[DIAG_PRED_NNE][2][1] = (P_C + P_D + 1) / 2;
img->mprr[DIAG_PRED_NNE][0][3] =
img->mprr[DIAG_PRED_NNE][2][2] = (P_D + P_E + 1) / 2;
img->mprr[DIAG_PRED_NNE][2][3] = (P_E + P_F + 1) / 2;
img->mprr[DIAG_PRED_NNE][1][0] = (2*(P_B + P_L) + P_A + P_C + P_K + P_M + 4) / 8;
img->mprr[DIAG_PRED_NNE][1][1] =
img->mprr[DIAG_PRED_NNE][3][0] = (P_B + 2*P_C + P_D + 2) / 4;
img->mprr[DIAG_PRED_NNE][1][2] =
img->mprr[DIAG_PRED_NNE][3][1] = (P_C + 2*P_D + P_E + 2) / 4;
img->mprr[DIAG_PRED_NNE][1][3] =
img->mprr[DIAG_PRED_NNE][3][2] = (P_D + 2*P_E + P_F + 2) / 4;
img->mprr[DIAG_PRED_NNE][3][3] = (P_E + 2*P_F + P_G + 2) / 4;
// Mode DIAG_PRED_ENE
img->mprr[DIAG_PRED_ENE][0][0] = (2*(P_C + P_I + P_J) + P_B + P_D + 4) / 8;
img->mprr[DIAG_PRED_ENE][0][1] = (2*(P_D + P_J) + P_C + P_E + P_I + P_K + 4) / 8;
img->mprr[DIAG_PRED_ENE][0][2] =
img->mprr[DIAG_PRED_ENE][1][0] = (2*(P_E + P_J + P_K) + P_D + P_F + 4) / 8;
img->mprr[DIAG_PRED_ENE][0][3] =
img->mprr[DIAG_PRED_ENE][1][1] = (2*(P_F + P_K) + P_E + P_G + P_J + P_L + 4) / 8;
img->mprr[DIAG_PRED_ENE][1][2] =
img->mprr[DIAG_PRED_ENE][2][0] = (2*(P_G + P_K + P_L) + P_F + P_H + 4) / 8;
img->mprr[DIAG_PRED_ENE][1][3] =
img->mprr[DIAG_PRED_ENE][2][1] = (2*(P_H + P_L) + P_G + P_H + P_K + P_L + 4) / 8;
img->mprr[DIAG_PRED_ENE][2][3] =
img->mprr[DIAG_PRED_ENE][3][1] = (P_L + (P_M << 1) + P_N + 2) / 4;
img->mprr[DIAG_PRED_ENE][3][0] =
img->mprr[DIAG_PRED_ENE][2][2] = (P_G + P_H + P_L + P_M + 2) / 4;
img->mprr[DIAG_PRED_ENE][3][2] = (P_M + P_N + 1) / 2;
img->mprr[DIAG_PRED_ENE][3][3] = (P_M + 2*P_N + P_O + 2) / 4;
// Mode DIAG_PRED_ESE
img->mprr[DIAG_PRED_ESE][0][0] =
img->mprr[DIAG_PRED_ESE][1][2] = (P_X + P_I + 1) / 2;
img->mprr[DIAG_PRED_ESE][0][1] =
img->mprr[DIAG_PRED_ESE][1][3] = (P_I + 2*P_X + P_A + 2) / 4;
img->mprr[DIAG_PRED_ESE][0][2] = (P_X + 2*P_A + P_B + 2) / 4;
img->mprr[DIAG_PRED_ESE][0][3] = (P_A + 2*P_B + P_C + 2) / 4;
img->mprr[DIAG_PRED_ESE][1][0] =
img->mprr[DIAG_PRED_ESE][2][2] = (P_I + P_J + 1) / 2;
img->mprr[DIAG_PRED_ESE][1][1] =
img->mprr[DIAG_PRED_ESE][2][3] = (P_X + 2*P_I + P_J + 2) / 4;
img->mprr[DIAG_PRED_ESE][2][0] =
img->mprr[DIAG_PRED_ESE][3][2] = (P_J + P_K + 1) / 2;
img->mprr[DIAG_PRED_ESE][2][1] =
img->mprr[DIAG_PRED_ESE][3][3] = (P_I + 2*P_J + P_K + 2) / 4;
img->mprr[DIAG_PRED_ESE][3][0] = (P_K + P_L + 1) / 2;
img->mprr[DIAG_PRED_ESE][3][1] = (P_J + 2*P_K + P_L + 2) / 4;
}
}
#else
/*!
************************************************************************
* \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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -