📄 quant_mpeg4.c
字号:
/**************************************************************************
*
* XVID MPEG-4 VIDEO CODEC
* mpeg-4 quantization/dequantization
*
* This program is an implementation of a part of one or more MPEG-4
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending
* to use this software module in hardware or software products are
* advised that its use may infringe existing patents or copyrights, and
* any such use would be at such party's own risk. The original
* developer of this software module and his/her company, and subsequent
* editors and their companies, will have no liability for use of this
* software or modifications or derivatives thereof.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*************************************************************************/
/**************************************************************************
*
* History:
*
* 26.01.2002 fixed quant4_intra dcscalar signed/unsigned error
* 20.01.2002 increased accuracy of >> divide
* 26.12.2001 divide-by-multiplication optimization
* 22.12.2001 [-127,127] clamping removed; minor tweaks
* 19.11.2001 inital version <pross@cs.rmit.edu.au>
*
*************************************************************************/
#include "../user_macro.h"
#include "quant_mpeg4.h"
#include "quant_matrix.h"
extern FILE *statfile;
/* function pointers */
quant_intraFuncPtr quant4_intra;
quant_intraFuncPtr dequant4_intra;
dequant_interFuncPtr dequant4_inter;
quant_interFuncPtr quant4_inter;
/*new-quantize*/
/*!
************************************************************************
* \brief
* 帧内宏块中的块DCT系数进行量化
*
************************************************************************
*/
#ifndef _TRIMEDIA
void
quant4_intra_c(int16_t * coeff, /*<--> 指向量化后的块*/
const int16_t * data, /*<-- 指向量化前的块*/
const uint32_t quant, /*<-- 量化值*/
const uint32_t chrom_lumflag /*<-- 当前块是色度块还是亮度块*/
)
{
const uint32_t quantd = ((3 * quant) + 2) >>2;
/* whq,2002.12.19,move variables out of loop */
int16_t (*p)[64];
int16_t (*q)[31];
uint32_t i;
int16_t temp;
uint32_t level;
int32_t sign0;
/* whq,2002.12.20 */
uint32_t quant1 = quant -1 ;
p=intra_quant_mat_self;
q=dc_quant;
sign0 = max(min(data[0],1),-1);
coeff[0] = (data[0]*sign0*(*(*(q+chrom_lumflag)+quant1))+32768)>>16;
coeff[0] = sign0*coeff[0];
for (i = 1; i < 64; i++) {
if (data[i] < 0) {
level = -data[i];
temp=*(*(p+quant1)+i);
level = (temp*((level<<4)+quantd))>>17;
coeff[i] = -(int16_t) level;
} else if (data[i] > 0) {
level = data[i];
temp=*(*(p+quant1)+i);
level =(temp*((level<<4)+quantd))>>17;
coeff[i] = level;
} else {
coeff[i] = 0;
}
}
}
#else
void
quant4_intra_c(int16_t * coeff, /*<--> 指向量化后的块*/
const int16_t * data, /*<-- 指向量化前的块*/
const uint32_t quant, /*<-- 量化值*/
const uint32_t chrom_lumflag /*<-- 当前块是色度块还是亮度块*/
)
{
const uint32_t quantd = ((3 * quant) + 2) >>2;
/* whq,2002.12.19,move variables out of loop */
int16_t (*p)[64],*intra_quant_num;
int16_t (*q)[31];
uint32_t i;
int32_t temp0,temp1,temp2,temp3,sign0,sign1,sign2,sign3;
int32_t level0,level1,level2,level3;
/* whq,2002.12.20 */
uint32_t quant1 = quant -1 ;
p=intra_quant_mat_self;
q=dc_quant;
intra_quant_num = *(p+quant1);
#pragma TCS_unroll=2
for (i = 0; i < 64; i+=4) {
level0 = data[i];
level1 = data[i+1];
level2 = data[i+2];
level3 = data[i+3];
temp0 = *(intra_quant_num+i);
temp1 = *(intra_quant_num+i+1);
temp2 = *(intra_quant_num+i+2);
temp3 = *(intra_quant_num+i+3);
sign0 = IMAX(IMIN(level0,1),-1);
sign1 = IMAX(IMIN(level1,1),-1);
sign2 = IMAX(IMIN(level2,1),-1);
sign3 = IMAX(IMIN(level3,1),-1);
/*
sign0 = IMAX(IMIN(level0,1),-1);
sign1 = IMAX(IMIN(level1,1),-1);
sign2 = IMAX(IMIN(level2,1),-1);
sign3 = IMAX(IMIN(level3,1),-1);
*/
level0 = ((((level0* sign0)<<4)+quantd) * temp0 )>>17;
level1 = ((((level1* sign1)<<4)+quantd) * temp1 )>>17;
level2 = ((((level2* sign2)<<4)+quantd) * temp2 )>>17;
level3 = ((((level3* sign3)<<4)+quantd) * temp3 )>>17;
coeff[i] = sign0 * level0;
coeff[i+1] = sign1 * level1;
coeff[i+2] = sign2 * level2;
coeff[i+3] = sign3 * level3;
}
sign0 = IMAX(IMIN(data[0],1),-1);
level0 = IABS(data[0]);
coeff[0] = (level0*(*(*(q+chrom_lumflag)+quant1))+32768)>>16;
coeff[0] = sign0*coeff[0];
}
#endif
/*!
************************************************************************
* \brief
* 对帧内宏块中的块系数进行反量化,重建DCT系数
*
************************************************************************
*/
#ifndef _TRIMEDIA
void
dequant4_intra_c(int16_t * data, /*<--> 指向量化后的块*/
const int16_t * coeff, /*<-- 指向量化后的块*/
const uint32_t quant, /*<-- 量化值*/
const uint32_t dcscalar /*<-- dc量化尺度*/
)
{
uint32_t i;
/* whq,2002.12.20,move variable out of loop and get matrix */
uint32_t level;
int16_t *intra_matrix1;
/*
intra_matrix = get_intra_matrix();
*/
intra_matrix1 =intra_matrix;
data[0] = coeff[0] * dcscalar;
/* if (data[0] < -2048) {
data[0] = -2048;
} else if (data[0] > 2047) {
data[0] = 2047;
}*/
data[0]=max(data[0],-2048);
data[0]=min(data[0],2047);
for (i = 1; i < 64; i++) {
if (coeff[i] == 0) {
data[i] = 0;
} else if (coeff[i] < 0) {
level = -coeff[i];
level = (level * intra_matrix1[i] * quant) >> 3;
data[i] = (level <= 2048 ? -(int16_t) level : -2048);
} else
{
level = coeff[i];
level = (level * intra_matrix1[i] * quant) >> 3;
data[i] = (level <= 2047 ? level : 2047);
}
}
}
#else
void
dequant4_intra_c(int16_t * data, /*<--> 指向量化后的块*/
const int16_t * coeff, /*<-- 指向量化后的块*/
const uint32_t quant, /*<-- 量化值*/
const uint32_t dcscalar /*<-- dc量化尺度*/
)
{
uint32_t i;
/* whq,2002.12.20,move variable out of loop and get matrix */
uint32_t level;
int16_t *intra_matrix1;
int32_t sign0,sign1,sign2,sign3;
int32_t level0,level1,level2,level3;
intra_matrix1 =intra_matrix;
/*#pragma TCS_unroll=2*/
for (i = 0; i < 64; i+=4) {
level0 = coeff[i];
level1 = coeff[i+1];
level2 = coeff[i+2];
level3 = coeff[i+3];
sign0 = (((level0>=0))<<1)-1;
sign1 = (((level1>=0))<<1)-1;
sign2 = (((level2>=0))<<1)-1;
sign3 = (((level3>=0))<<1)-1;
level0 = (coeff[i] *intra_matrix1[i ]*quant*sign0)>>3;
level1 = (coeff[i+1]*intra_matrix1[i+1]*quant*sign1)>>3;
level2 = (coeff[i+2]*intra_matrix1[i+2]*quant*sign2)>>3;
level3 = (coeff[i+3]*intra_matrix1[i+3]*quant*sign3)>>3;
level0 = sign0*level0;
level1 = sign1*level1;
level2 = sign2*level2;
level3 = sign3*level3;
data[i] =ICLIPI(level0,2047);
data[i+1]=ICLIPI(level1,2047);
data[i+2]=ICLIPI(level2,2047);
data[i+3]=ICLIPI(level3,2047);
}
data[0] = coeff[0] * dcscalar;
data[0]=ICLIPI(data[0],2047);
}
#endif
/*!
************************************************************************
* \brief
* 对非帧内宏块中的块DCT系数进行量化
*
************************************************************************
*/
#ifndef _TRIMEDIA
uint32_t
quant4_inter_c(int16_t * coeff, /*<--> 指向量化后的块*/
const int16_t * data, /*<-- 指向量化前的块*/
const uint32_t quant /*<-- 量化值*/
)
{
uint32_t sum = 0;
uint32_t i;
int16_t (*p)[64];
int16_t temp;
uint32_t level;
uint32_t quant1 = quant -1 ;
p=inter_quant_mat_self;
for (i = 0; i < 64; i++) {
if (data[i] < 0) {
level = -data[i];
/* level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
level = (level * mult) >> 17;
*/
temp=*(*(p+quant1)+i);
/*whq,2002.12.20*/
/*level = (temp*(level<<4))>>17;*/
level = (temp*level)>>13;
sum += level;
coeff[i] = -(int16_t) level;
} else if (data[i] > 0) {
level = data[i];
/*
level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i];
level = (level * mult) >> 17;
*/
temp=*(*(p+quant1)+i);
/*whq,2002.12.20*/
/*level = (temp*(level<<4))>>17;*/
level =(temp*level)>>13;
coeff[i] = level;
sum += level;
} else {
coeff[i] = 0;
}
}
return sum;
}
#else
/* Ignore the skip condition since it is useless*/
uint32_t
quant4_inter_c(int16_t * coeff, /*<--> 指向量化后的块*/
const int16_t * data, /*<-- 指向量化前的块*/
const uint32_t quant /*<-- 量化值*/
)
{
uint32_t sum = 0;
uint32_t i;
int16_t (*p)[64],*inter_quant_num;
int16_t temp0,temp1,temp2,temp3,sign0,sign1,sign2,sign3;
int32_t level0,level1,level2,level3;
uint32_t quant1 = quant -1 ;
p=inter_quant_mat_self;
inter_quant_num = *(p+quant1);
#pragma TCS_unroll=2
for (i = 0; i < 64; i+=4) {
level0 = data[i];
level1 = data[i+1];
level2 = data[i+2];
level3 = data[i+3];
temp0 = *(inter_quant_num+i);
temp1 = *(inter_quant_num+i+1);
temp2 = *(inter_quant_num+i+2);
temp3 = *(inter_quant_num+i+3);
sign0 = (level0>=0)*2-1;
sign1 = (level1>=0)*2-1;
sign2 = (level2>=0)*2-1;
sign3 = (level3>=0)*2-1;
/*
sign0 = IMAX(IMIN(level0,1),-1);
sign1 = IMAX(IMIN(level1,1),-1);
sign2 = IMAX(IMIN(level2,1),-1);
sign3 = IMAX(IMIN(level3,1),-1);
*/
level0 = (level0 * temp0 * sign0)>>13;
level1 = (level1 * temp1 * sign1)>>13;
level2 = (level2 * temp2 * sign2)>>13;
level3 = (level3 * temp3 * sign3)>>13;
coeff[i] = sign0 * level0;
coeff[i+1] = sign1 * level1;
coeff[i+2] = sign2 * level2;
coeff[i+3] = sign3 * level3;
sum += level0 + level1 + level2 + level3;
}
return sum;
}
#endif
/*!
************************************************************************
* \brief
* 非帧内宏块中的块系数进行反量化,重建DCT系数
*
************************************************************************
*/
#ifdef _TRIMEDIA
void
dequant4_inter_c(int16_t * data, /*<--> 指向量化后的块*/
const int16_t * coeff, /*<-- 指向量化后的块*/
const uint32_t quant /*<-- 量化值*/
)
{
uint32_t sum = 0;
uint32_t i;
/* whq,2002.12.20,move variable out of loop and get matrix */
uint32_t level0,level1,level2,level3;
int16_t *inter_matrix1;
int32_t temp;
int32_t sign0,sign1,sign2,sign3;
int32_t sum0,sum1;
inter_matrix1 = inter_matrix;
/*
inter_matrix = get_inter_matrix();
*/
/*#pragma TCS_unroll=4*/
for (i = 0; i < 64; i+=4) {
level0 = coeff[i];
level1 = coeff[i+1];
level2 = coeff[i+2];
level3 = coeff[i+3];
sign0 = IMAX(IMIN(level0,1),-1);
sign1 = IMAX(IMIN(level1,1),-1);
sign2 = IMAX(IMIN(level2,1),-1);
sign3 = IMAX(IMIN(level3,1),-1);
level0 = ((coeff[i] *2+sign0)*inter_matrix1[i ]*quant*sign0)>>4;
level1 = ((coeff[i+1]*2+sign1)*inter_matrix1[i+1]*quant*sign1)>>4;
level2 = ((coeff[i+2]*2+sign2)*inter_matrix1[i+2]*quant*sign2)>>4;
level3 = ((coeff[i+3]*2+sign3)*inter_matrix1[i+3]*quant*sign3)>>4;
level0 = sign0*level0;
level1 = sign1*level1;
level2 = sign2*level2;
level3 = sign3*level3;
data[i+0]=ICLIPI(level0,2047);
data[i+1]=ICLIPI(level1,2047);
data[i+2]=ICLIPI(level2,2047);
data[i+3]=ICLIPI(level3,2047);
sum0 = data[i] +data[i+1];
sum1 = data[i+2]+data[i+3];
sum+=sum0+sum1;
}
if ((sum & 1) == 0) {
data[63] ^= 1;
}
}
#endif
#ifndef _TRIMEDIA
void
dequant4_inter_c(int16_t * data, /*<--> 指向量化后的块*/
const int16_t * coeff, /*<-- 指向量化后的块*/
const uint32_t quant /*<-- 量化值*/
)
{
uint32_t sum = 0;
uint32_t i;
/* whq,2002.12.20,move variable out of loop and get matrix */
uint32_t level;
int16_t *inter_matrix1;
inter_matrix1 = inter_matrix;
/*
inter_matrix = get_inter_matrix();
*/
for (i = 0; i < 64; i++) {
if (coeff[i] == 0) {
data[i] = 0;
} else if (coeff[i] < 0) {
level = -coeff[i];
level = (( (level<<1) + 1) * inter_matrix1[i] * quant) >> 4;
data[i] = (level <= 2048 ? -(int16_t)level : -2048);
} else /* if (coeff[i] > 0)*/
{
level = coeff[i];
level = (( (level<<1) + 1) * inter_matrix1[i] * quant) >> 4;
data[i] = (level <= 2047 ? level : 2047);
}
sum ^= data[i];
}
/* mismatch control*/
if ((sum & 1) == 0) {
data[63] ^= 1;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -