📄 q_matrix.c
字号:
if(ScalingList[cnt]<0 || ScalingList[cnt]>255) // ScalingList[0]=0 to indicate use default matrix
{
fail=1;
break;
}
}
if(fail) //value of matrix exceed range
{
printf("\n%s value exceed range. (Value must be 1 to 255)\n", MatrixType8x8[i]);
printf("Setting default values for this matrix.");
if(i==7)
memcpy(ScalingList, Quant8_inter_default, sizeof(short)*64);
else
memcpy(ScalingList, Quant8_intra_default, sizeof(short)*64);
}
}
else //matrix not found, pad with default value
{
printf("\n%s matrix definition not found. Setting default values.", MatrixType8x8[i]);
if(i==7)
memcpy(ScalingList, Quant8_inter_default, sizeof(short)*64);
else
memcpy(ScalingList, Quant8_intra_default, sizeof(short)*64);
}
}
}
}
/*!
***********************************************************************
* \brief
* Allocate Q matrix arrays
***********************************************************************
*/
void allocate_QMatrix ()
{
int bitdepth_qp_scale = 6*(input->BitDepthLuma - 8);
int i;
if ((qp_per_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
no_mem_exit("init_global_buffers: qp_per_matrix");
if ((qp_rem_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
no_mem_exit("init_global_buffers: qp_per_matrix");
for (i = 0; i < MAX_QP + bitdepth_qp_scale + 1; i++)
{
qp_per_matrix[i] = i / 6;
qp_rem_matrix[i] = i % 6;
}
get_mem4Dint(&LevelScale4x4Luma, 2, 6, 4, 4);
get_mem5Dint(&LevelScale4x4Chroma, 2, 2, 6, 4, 4);
get_mem4Dint(&LevelScale8x8Luma, 2, 6, 8, 8);
get_mem4Dint(&InvLevelScale4x4Luma, 2, 6, 4, 4);
get_mem5Dint(&InvLevelScale4x4Chroma, 2, 2, 6, 4, 4);
get_mem4Dint(&InvLevelScale8x8Luma, 2, 6, 8, 8);
}
/*!
***********************************************************************
* \brief
* Free Q matrix arrays
***********************************************************************
*/
void free_QMatrix ()
{
free(qp_rem_matrix);
free(qp_per_matrix);
free_mem4Dint(LevelScale4x4Luma, 2, 6);
free_mem5Dint(LevelScale4x4Chroma, 2, 2, 6);
free_mem4Dint(LevelScale8x8Luma, 2, 6);
free_mem4Dint(InvLevelScale4x4Luma, 2, 6);
free_mem5Dint(InvLevelScale4x4Chroma, 2, 2, 6);
free_mem4Dint(InvLevelScale8x8Luma, 2, 6);
}
/*!
***********************************************************************
* \brief
* Initialise Q matrix values.
***********************************************************************
*/
void Init_QMatrix (void)
{
char *content;
allocate_QMatrix ();
if(input->ScalingMatrixPresentFlag)
{
printf ("Parsing QMatrix file %s ", input->QmatrixFile);
content = GetConfigFileContent(input->QmatrixFile, 0);
if(content!='\0')
ParseMatrix(content, strlen (content));
else
printf("\nError: %s\nProceeding with default values for all matrices.", errortext);
PatchMatrix();
printf("\n");
memset(UseDefaultScalingMatrix4x4Flag, 0, 6 * sizeof(short));
UseDefaultScalingMatrix8x8Flag[0]=UseDefaultScalingMatrix8x8Flag[1]=0;
free(content);
}
}
/*!
************************************************************************
* \brief
* For calculating the quantisation values at frame level
*
* \par Input:
* none
*
* \par Output:
* none
************************************************************************
*/
void CalculateQuantParam(void)
{
int i, j, k, temp;
int present[6];
int no_q_matrix=FALSE;
if(!active_sps->seq_scaling_matrix_present_flag && !active_pps->pic_scaling_matrix_present_flag) //set to no q-matrix
no_q_matrix=TRUE;
else
{
memset(present, 0, sizeof(int)*6);
if(active_sps->seq_scaling_matrix_present_flag)
for(i=0; i<6; i++)
present[i] = active_sps->seq_scaling_list_present_flag[i];
if(active_pps->pic_scaling_matrix_present_flag)
for(i=0; i<6; i++)
{
if((i==0) || (i==3))
present[i] |= active_pps->pic_scaling_list_present_flag[i];
else
present[i] = active_pps->pic_scaling_list_present_flag[i];
}
}
if(no_q_matrix==TRUE)
{
for(k=0; k<6; k++)
for(j=0; j<4; j++)
for(i=0; i<4; i++)
{
LevelScale4x4Luma[1][k][j][i] = quant_coef[k][j][i];
InvLevelScale4x4Luma[1][k][j][i] = dequant_coef[k][j][i]<<4;
LevelScale4x4Chroma[0][1][k][j][i] = quant_coef[k][j][i];
InvLevelScale4x4Chroma[0][1][k][j][i] = dequant_coef[k][j][i]<<4;
LevelScale4x4Chroma[1][1][k][j][i] = quant_coef[k][j][i];
InvLevelScale4x4Chroma[1][1][k][j][i] = dequant_coef[k][j][i]<<4;
// Inter
LevelScale4x4Luma[0][k][j][i] = quant_coef[k][j][i];
InvLevelScale4x4Luma[0][k][j][i] = dequant_coef[k][j][i]<<4;
LevelScale4x4Chroma[0][0][k][j][i] = quant_coef[k][j][i];
InvLevelScale4x4Chroma[0][0][k][j][i] = dequant_coef[k][j][i]<<4;
LevelScale4x4Chroma[1][0][k][j][i] = quant_coef[k][j][i];
InvLevelScale4x4Chroma[1][0][k][j][i] = dequant_coef[k][j][i]<<4;
}
}
else
{
for(k=0; k<6; k++)
for(j=0; j<4; j++)
for(i=0; i<4; i++)
{
temp = (i<<2)+j;
if((!present[0]) || UseDefaultScalingMatrix4x4Flag[0])
{
LevelScale4x4Luma[1][k][j][i] = (quant_coef[k][j][i]<<4)/Quant_intra_default[temp];
InvLevelScale4x4Luma[1][k][j][i] = dequant_coef[k][j][i]*Quant_intra_default[temp];
}
else
{
LevelScale4x4Luma[1][k][j][i] = (quant_coef[k][j][i]<<4)/ScalingList4x4[0][temp];
InvLevelScale4x4Luma[1][k][j][i] = dequant_coef[k][j][i]*ScalingList4x4[0][temp];
}
if(!present[1])
{
LevelScale4x4Chroma[0][1][k][j][i] = LevelScale4x4Luma[1][k][j][i];
InvLevelScale4x4Chroma[0][1][k][j][i] = InvLevelScale4x4Luma[1][k][j][i];
}
else
{
LevelScale4x4Chroma[0][1][k][j][i] = (quant_coef[k][j][i]<<4)/(UseDefaultScalingMatrix4x4Flag[1] ? Quant_intra_default[temp]:ScalingList4x4[1][temp]);
InvLevelScale4x4Chroma[0][1][k][j][i] = dequant_coef[k][j][i]*(UseDefaultScalingMatrix4x4Flag[1] ? Quant_intra_default[temp]:ScalingList4x4[1][temp]);
}
if(!present[2])
{
LevelScale4x4Chroma[1][1][k][j][i] = LevelScale4x4Chroma[0][1][k][j][i];
InvLevelScale4x4Chroma[1][1][k][j][i] = InvLevelScale4x4Chroma[0][1][k][j][i];
}
else
{
LevelScale4x4Chroma[1][1][k][j][i] = (quant_coef[k][j][i]<<4)/(UseDefaultScalingMatrix4x4Flag[2] ? Quant_intra_default[temp]:ScalingList4x4[2][temp]);
InvLevelScale4x4Chroma[1][1][k][j][i] = dequant_coef[k][j][i]*(UseDefaultScalingMatrix4x4Flag[2] ? Quant_intra_default[temp]:ScalingList4x4[2][temp]);
}
if((!present[3]) || UseDefaultScalingMatrix4x4Flag[3])
{
LevelScale4x4Luma[0][k][j][i] = (quant_coef[k][j][i]<<4)/Quant_inter_default[temp];
InvLevelScale4x4Luma[0][k][j][i] = dequant_coef[k][j][i]*Quant_inter_default[temp];
}
else
{
LevelScale4x4Luma[0][k][j][i] = (quant_coef[k][j][i]<<4)/ScalingList4x4[3][temp];
InvLevelScale4x4Luma[0][k][j][i] = dequant_coef[k][j][i]*ScalingList4x4[3][temp];
}
if(!present[4])
{
LevelScale4x4Chroma[0][0][k][j][i] = LevelScale4x4Luma[0][k][j][i];
InvLevelScale4x4Chroma[0][0][k][j][i] = InvLevelScale4x4Luma[0][k][j][i];
}
else
{
LevelScale4x4Chroma[0][0][k][j][i] = (quant_coef[k][j][i]<<4)/(UseDefaultScalingMatrix4x4Flag[4] ? Quant_inter_default[temp]:ScalingList4x4[4][temp]);
InvLevelScale4x4Chroma[0][0][k][j][i] = dequant_coef[k][j][i]*(UseDefaultScalingMatrix4x4Flag[4] ? Quant_inter_default[temp]:ScalingList4x4[4][temp]);
}
if(!present[5])
{
LevelScale4x4Chroma[1][0][k][j][i] = LevelScale4x4Chroma[0][0][k][j][i];
InvLevelScale4x4Chroma[1][0][k][j][i] = InvLevelScale4x4Chroma[0][0][k][j][i];
}
else
{
LevelScale4x4Chroma[1][0][k][j][i] = (quant_coef[k][j][i]<<4)/(UseDefaultScalingMatrix4x4Flag[5] ? Quant_inter_default[temp]:ScalingList4x4[5][temp]);
InvLevelScale4x4Chroma[1][0][k][j][i] = dequant_coef[k][j][i]*(UseDefaultScalingMatrix4x4Flag[5] ? Quant_inter_default[temp]:ScalingList4x4[5][temp]);
}
}
}
}
/*!
************************************************************************
* \brief
* Calculate the quantisation and inverse quantisation parameters
*
************************************************************************
*/
void CalculateQuant8Param()
{
int i, j, k, temp;
int present[2];
int no_q_matrix=FALSE;
if(!active_sps->seq_scaling_matrix_present_flag && !active_pps->pic_scaling_matrix_present_flag) //set to default matrix
no_q_matrix=TRUE;
else
{
memset(present, 0, sizeof(int)*2);
if(active_sps->seq_scaling_matrix_present_flag)
for(i=0; i<2; i++)
present[i] = active_sps->seq_scaling_list_present_flag[i+6];
if(active_pps->pic_scaling_matrix_present_flag)
for(i=0; i<2; i++)
present[i] |= active_pps->pic_scaling_list_present_flag[i+6];
}
if(no_q_matrix==TRUE)
{
for(k=0; k<6; k++)
for(j=0; j<8; j++)
for(i=0; i<8; i++)
{
LevelScale8x8Luma[1][k][j][i] = quant_coef8[k][j][i];
InvLevelScale8x8Luma[1][k][j][i] = dequant_coef8[k][j][i]<<4;
LevelScale8x8Luma[0][k][j][i] = quant_coef8[k][j][i];
InvLevelScale8x8Luma[0][k][j][i] = dequant_coef8[k][j][i]<<4;
}
}
else
{
for(k=0; k<6; k++)
for(j=0; j<8; j++)
for(i=0; i<8; i++)
{
temp = (i<<3)+j;
if((!present[0]) || UseDefaultScalingMatrix8x8Flag[0])
{
LevelScale8x8Luma[1][k][j][i] = (quant_coef8[k][j][i]<<4)/Quant8_intra_default[temp];
InvLevelScale8x8Luma[1][k][j][i] = dequant_coef8[k][j][i]*Quant8_intra_default[temp];
}
else
{
LevelScale8x8Luma[1][k][j][i] = (quant_coef8[k][j][i]<<4)/ScalingList8x8[0][temp];
InvLevelScale8x8Luma[1][k][j][i] = dequant_coef8[k][j][i]*ScalingList8x8[0][temp];
}
if((!present[1]) || UseDefaultScalingMatrix8x8Flag[1])
{
LevelScale8x8Luma[0][k][j][i] = (quant_coef8[k][j][i]<<4)/Quant8_inter_default[temp];
InvLevelScale8x8Luma[0][k][j][i] = dequant_coef8[k][j][i]*Quant8_inter_default[temp];
}
else
{
LevelScale8x8Luma[0][k][j][i] = (quant_coef8[k][j][i]<<4)/ScalingList8x8[1][temp];
InvLevelScale8x8Luma[0][k][j][i] = dequant_coef8[k][j][i]*ScalingList8x8[1][temp];
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -