646 |
{ |
{ |
647 |
|
|
648 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
649 |
int32_t bits = 0, sum; |
int32_t bits = 0; |
650 |
VECTOR * current; |
VECTOR * current; |
651 |
const uint8_t * ptr; |
const uint8_t * ptr; |
652 |
int i, cbp = 0, t, xc, yc; |
int i, cbp = 0, t, xc, yc; |
667 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
668 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
669 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
670 |
fdct(in); |
bits += data->temp[i] = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, i, 0); |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
|
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
cbp |= 1 << (5 - i); |
|
|
bits += data->temp[i] = CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
} else data->temp[i] = 0; |
|
671 |
} |
} |
672 |
|
|
673 |
bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
674 |
|
|
675 |
if (bits < data->iMinSAD[0]) { // there is still a chance, adding chroma |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
676 |
|
|
677 |
|
if (bits >= data->iMinSAD[0]) return; |
678 |
|
|
679 |
|
//chroma |
680 |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
681 |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
682 |
|
|
683 |
//chroma U |
//chroma U |
684 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
685 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
686 |
fdct(in); |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 4, 0); |
687 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
if (bits >= data->iMinSAD[0]) return; |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
cbp |= 1 << (5 - 4); |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
} |
|
688 |
|
|
|
if (bits < data->iMinSAD[0]) { |
|
689 |
//chroma V |
//chroma V |
690 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
691 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
692 |
fdct(in); |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5, 0); |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
|
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
cbp |= 1 << (5 - 5); |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
} |
|
|
} |
|
|
} |
|
693 |
|
|
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
|
694 |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
695 |
|
|
696 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
714 |
{ |
{ |
715 |
|
|
716 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
717 |
int32_t sum, bits; |
int32_t bits; |
718 |
VECTOR * current; |
VECTOR * current; |
719 |
const uint8_t * ptr; |
const uint8_t * ptr; |
720 |
int cbp; |
int cbp = 0; |
721 |
|
|
722 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
723 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
731 |
} |
} |
732 |
|
|
733 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
734 |
fdct(in); |
bits = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5, 0); |
735 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
bits += d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
bits = CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp = 1; |
|
|
} else cbp = bits = 0; |
|
|
|
|
|
bits += sum = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
|
736 |
|
|
737 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
738 |
data->temp[0] = cbp; |
data->temp[0] = cbp; |
942 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
943 |
} |
} |
944 |
|
|
945 |
|
static __inline void |
946 |
|
ModeDecision(SearchData * const Data, |
947 |
|
MACROBLOCK * const pMB, |
948 |
|
const MACROBLOCK * const pMBs, |
949 |
|
const int x, const int y, |
950 |
|
const MBParam * const pParam, |
951 |
|
const uint32_t MotionFlags, |
952 |
|
const uint32_t GlobalFlags, |
953 |
|
const IMAGE * const pCurrent, |
954 |
|
const IMAGE * const pRef) |
955 |
|
{ |
956 |
|
int mode = MODE_INTER; |
957 |
|
int inter4v = (GlobalFlags & XVID_INTER4V) && (pMB->dquant == NO_CHANGE); |
958 |
|
const uint32_t iQuant = pMB->quant; |
959 |
|
|
960 |
|
const int skip_possible = (!(GlobalFlags & XVID_GMC)) && (pMB->dquant == NO_CHANGE); |
961 |
|
|
962 |
|
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision |
963 |
|
int sad; |
964 |
|
int InterBias = MV16_INTER_BIAS; |
965 |
|
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
966 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
967 |
|
mode = MODE_INTER; |
968 |
|
sad = Data->iMinSAD[0]; |
969 |
|
} else { |
970 |
|
mode = MODE_INTER4V; |
971 |
|
sad = Data->iMinSAD[1] + Data->iMinSAD[2] + |
972 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; |
973 |
|
Data->iMinSAD[0] = sad; |
974 |
|
} |
975 |
|
|
976 |
|
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
977 |
|
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
978 |
|
if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
979 |
|
if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { |
980 |
|
mode = MODE_NOT_CODED; |
981 |
|
sad = 0; |
982 |
|
} |
983 |
|
|
984 |
|
/* intra decision */ |
985 |
|
|
986 |
|
if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work |
987 |
|
if (y != 0) |
988 |
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
989 |
|
if (x != 0) |
990 |
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
991 |
|
|
992 |
|
if (Data->chroma) InterBias += 50; // dev8(chroma) ??? |
993 |
|
if (Data->rrv) InterBias *= 4; |
994 |
|
|
995 |
|
if (InterBias < pMB->sad16) { |
996 |
|
int32_t deviation; |
997 |
|
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
998 |
|
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
999 |
|
dev16(Data->Cur+16, Data->iEdgedWidth) + |
1000 |
|
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
1001 |
|
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
1002 |
|
|
1003 |
|
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
1004 |
|
} |
1005 |
|
|
1006 |
|
} else { // BITS |
1007 |
|
|
1008 |
|
int bits, intra, i; |
1009 |
|
VECTOR backup[5], *v; |
1010 |
|
Data->iQuant = iQuant; |
1011 |
|
|
1012 |
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
1013 |
|
for (i = 0; i < 5; i++) { |
1014 |
|
Data->iMinSAD[i] = 256*4096; |
1015 |
|
backup[i] = v[i]; |
1016 |
|
} |
1017 |
|
|
1018 |
|
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
1019 |
|
if (bits == 0) |
1020 |
|
mode = MODE_INTER; // quick stop |
1021 |
|
else { |
1022 |
|
if (inter4v) { |
1023 |
|
int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
1024 |
|
if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } |
1025 |
|
} |
1026 |
|
|
1027 |
|
intra = CountMBBitsIntra(Data); |
1028 |
|
|
1029 |
|
if (intra < bits) { *Data->iMinSAD = bits = intra; mode = MODE_INTRA; } |
1030 |
|
} |
1031 |
|
} |
1032 |
|
|
1033 |
|
if (Data->rrv) { |
1034 |
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
1035 |
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1036 |
|
} |
1037 |
|
|
1038 |
|
if (mode == MODE_INTER) { |
1039 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1040 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
1041 |
|
|
1042 |
|
if(Data->qpel) { |
1043 |
|
pMB->qmvs[0] = pMB->qmvs[1] |
1044 |
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
1045 |
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
1046 |
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
1047 |
|
} else { |
1048 |
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1049 |
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1050 |
|
} |
1051 |
|
|
1052 |
|
} else if (mode == MODE_INTER4V) |
1053 |
|
pMB->sad16 = Data->iMinSAD[0]; |
1054 |
|
else // INTRA, NOT_CODED |
1055 |
|
SkipMacroblockP(pMB, 0); |
1056 |
|
|
1057 |
|
pMB->mode = mode; |
1058 |
|
} |
1059 |
|
|
1060 |
bool |
bool |
1061 |
MotionEstimation(MBParam * const pParam, |
MotionEstimation(MBParam * const pParam, |
1062 |
FRAMEINFO * const current, |
FRAMEINFO * const current, |
1101 |
Data.chroma = MotionFlags & PMV_CHROMA16; |
Data.chroma = MotionFlags & PMV_CHROMA16; |
1102 |
Data.rrv = current->global_flags & XVID_REDUCED; |
Data.rrv = current->global_flags & XVID_REDUCED; |
1103 |
Data.dctSpace = dct_space; |
Data.dctSpace = dct_space; |
1104 |
|
Data.quant_type = pParam->m_quant_type; |
1105 |
|
|
1106 |
if ((current->global_flags & XVID_REDUCED)) { |
if ((current->global_flags & XVID_REDUCED)) { |
1107 |
mb_width = (pParam->width + 31) / 32; |
mb_width = (pParam->width + 31) / 32; |
1136 |
|
|
1137 |
sad00 = pMB->sad16; |
sad00 = pMB->sad16; |
1138 |
|
|
1139 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) |
1140 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
1141 |
} else { |
else { |
1142 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
1143 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
1144 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
1145 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
1146 |
} |
} |
1147 |
} |
} |
1148 |
pMB->quant = current->quant; |
pMB->quant = quant; |
1149 |
|
|
1150 |
//initial skip decision |
//initial skip decision |
1151 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1158 |
} |
} |
1159 |
|
|
1160 |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1161 |
y, MotionFlags, current->global_flags, pMB->quant, |
y, MotionFlags, current->global_flags, |
1162 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, pMB); |
1163 |
current->global_flags & XVID_INTER4V, pMB); |
|
1164 |
|
ModeDecision(&Data, pMB, pMBs, x, y, pParam, |
1165 |
|
MotionFlags, current->global_flags, |
1166 |
|
pCurrent, pRef); |
1167 |
|
|
|
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
|
|
if (!(current->global_flags & XVID_GMC || current->global_flags & XVID_MODEDECISION_BITS)) { |
|
|
if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { |
|
|
if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) |
|
|
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) |
|
|
SkipMacroblockP(pMB, sad00); |
|
|
} |
|
|
} |
|
1168 |
if (pMB->mode == MODE_INTRA) |
if (pMB->mode == MODE_INTRA) |
1169 |
if (++iIntra > iLimit) return 1; |
if (++iIntra > iLimit) return 1; |
1170 |
} |
} |
1237 |
} |
} |
1238 |
} |
} |
1239 |
|
|
|
static int |
|
|
ModeDecision(const uint32_t iQuant, SearchData * const Data, |
|
|
int inter4v, |
|
|
MACROBLOCK * const pMB, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const int x, const int y, |
|
|
const MBParam * const pParam, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t GlobalFlags) |
|
|
{ |
|
|
|
|
|
int mode = MODE_INTER; |
|
|
|
|
|
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision |
|
|
int sad; |
|
|
int InterBias = MV16_INTER_BIAS; |
|
|
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
|
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
|
|
mode = MODE_INTER; |
|
|
sad = Data->iMinSAD[0]; |
|
|
} else { |
|
|
mode = MODE_INTER4V; |
|
|
sad = Data->iMinSAD[1] + Data->iMinSAD[2] + |
|
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; |
|
|
Data->iMinSAD[0] = sad; |
|
|
} |
|
|
|
|
|
/* intra decision */ |
|
|
|
|
|
if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work |
|
|
if (y != 0) |
|
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
|
|
if (x != 0) |
|
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
|
|
|
|
|
if (Data->chroma) InterBias += 50; // to compensate bigger SAD |
|
|
if (Data->rrv) InterBias *= 4; |
|
|
|
|
|
if (InterBias < pMB->sad16) { |
|
|
int32_t deviation; |
|
|
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
|
|
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur+8, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth); |
|
|
|
|
|
if (deviation < (sad - InterBias)) return MODE_INTRA; |
|
|
} |
|
|
return mode; |
|
|
|
|
|
} else { |
|
|
|
|
|
int bits, intra, i; |
|
|
VECTOR backup[5], *v; |
|
|
Data->lambda16 = iQuant; |
|
|
Data->lambda8 = pParam->m_quant_type; |
|
|
|
|
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
|
|
for (i = 0; i < 5; i++) { |
|
|
Data->iMinSAD[i] = 256*4096; |
|
|
backup[i] = v[i]; |
|
|
} |
|
|
|
|
|
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
|
|
if (bits == 0) return MODE_INTER; // quick stop |
|
|
|
|
|
if (inter4v) { |
|
|
int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
|
|
if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } |
|
|
} |
|
|
|
|
|
intra = CountMBBitsIntra(Data); |
|
|
|
|
|
if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; } |
|
|
|
|
|
return mode; |
|
|
} |
|
|
} |
|
|
|
|
1240 |
static void |
static void |
1241 |
SearchP(const IMAGE * const pRef, |
SearchP(const IMAGE * const pRef, |
1242 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1247 |
const int y, |
const int y, |
1248 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1249 |
const uint32_t GlobalFlags, |
const uint32_t GlobalFlags, |
|
const uint32_t iQuant, |
|
1250 |
SearchData * const Data, |
SearchData * const Data, |
1251 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1252 |
const MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
1253 |
const MACROBLOCK * const prevMBs, |
const MACROBLOCK * const prevMBs, |
|
int inter4v, |
|
1254 |
MACROBLOCK * const pMB) |
MACROBLOCK * const pMB) |
1255 |
{ |
{ |
1256 |
|
|
1257 |
int i, iDirection = 255, mask, threshA; |
int i, iDirection = 255, mask, threshA; |
1258 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1259 |
|
int inter4v = (GlobalFlags & XVID_INTER4V) && (pMB->dquant == NO_CHANGE); |
1260 |
|
|
1261 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1262 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
1276 |
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1277 |
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1278 |
|
|
1279 |
Data->lambda16 = lambda_vec16[iQuant]; |
Data->lambda16 = lambda_vec16[pMB->quant]; |
1280 |
Data->lambda8 = lambda_vec8[iQuant]; |
Data->lambda8 = lambda_vec8[pMB->quant]; |
1281 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1282 |
|
|
|
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
|
|
|
|
1283 |
memset(Data->currentMV, 0, 5*sizeof(VECTOR)); |
memset(Data->currentMV, 0, 5*sizeof(VECTOR)); |
1284 |
|
|
1285 |
if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1317 |
|
|
1318 |
if ((Data->iMinSAD[0] <= threshA) || |
if ((Data->iMinSAD[0] <= threshA) || |
1319 |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
1320 |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) { |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) |
1321 |
if (!(GlobalFlags & XVID_MODEDECISION_BITS)) inter4v = 0; } |
inter4v = 0; |
1322 |
else { |
else { |
1323 |
|
|
1324 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1379 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1380 |
} |
} |
1381 |
|
|
1382 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)) |
1383 |
|
inter4v = 0; |
1384 |
|
|
1385 |
if (inter4v) { |
if (inter4v) { |
1386 |
SearchData Data8; |
SearchData Data8; |
1392 |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1393 |
|
|
1394 |
if ((Data->chroma) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) { |
if ((Data->chroma) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) { |
1395 |
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it |
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used |
1396 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
|
const int div = Data->qpel ? 2 : 1; |
|
|
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
|
1397 |
|
|
1398 |
for (i = 0; i < 4; i++) { |
if (Data->qpel) |
1399 |
sumx += mv[i].x / div; |
for (i = 1; i < 5; i++) { |
1400 |
sumy += mv[i].y / div; |
sumx += Data->currentQMV[i].x/2; |
1401 |
|
sumy += Data->currentQMV[i].y/2; |
1402 |
|
} |
1403 |
|
else |
1404 |
|
for (i = 1; i < 5; i++) { |
1405 |
|
sumx += Data->currentMV[i].x; |
1406 |
|
sumy += Data->currentMV[i].y; |
1407 |
} |
} |
1408 |
|
|
1409 |
Data->iMinSAD[1] += ChromaSAD( (sumx >> 3) + roundtab_76[sumx & 0xf], |
Data->iMinSAD[1] += ChromaSAD( (sumx >> 3) + roundtab_76[sumx & 0xf], |
1410 |
(sumy >> 3) + roundtab_76[sumy & 0xf], Data); |
(sumy >> 3) + roundtab_76[sumy & 0xf], Data); |
1411 |
} |
} |
1412 |
} |
} else Data->iMinSAD[1] = 4096*256; |
|
|
|
|
inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, GlobalFlags); |
|
|
|
|
|
if (Data->rrv) { |
|
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
|
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
|
|
} |
|
|
|
|
|
if (inter4v == MODE_INTER) { |
|
|
pMB->mode = MODE_INTER; |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
|
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
|
|
|
|
|
if(Data->qpel) { |
|
|
pMB->qmvs[0] = pMB->qmvs[1] |
|
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
|
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
|
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
|
|
} else { |
|
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
|
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
|
|
} |
|
|
|
|
|
} else if (inter4v == MODE_INTER4V) { |
|
|
pMB->mode = MODE_INTER4V; |
|
|
pMB->sad16 = Data->iMinSAD[0]; |
|
|
} else { // INTRA mode |
|
|
SkipMacroblockP(pMB, 0); // not skip, but similar enough |
|
|
pMB->mode = MODE_INTRA; |
|
|
} |
|
|
|
|
1413 |
} |
} |
1414 |
|
|
1415 |
static void |
static void |
2508 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
2509 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
2510 |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
2511 |
|
uint8_t * ptr; |
2512 |
|
|
2513 |
memcpy(Data8, Data, sizeof(SearchData)); |
memcpy(Data8, Data, sizeof(SearchData)); |
2514 |
CheckCandidate = CheckCandidateBits8; |
CheckCandidate = CheckCandidateBits8; |
2515 |
|
|
2516 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { //for all luma blocks |
2517 |
|
|
2518 |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
2519 |
Data8->currentMV = Data->currentMV + i + 1; |
Data8->currentMV = Data->currentMV + i + 1; |
2520 |
Data8->currentQMV = Data->currentQMV + i + 1; |
Data8->currentQMV = Data->currentQMV + i + 1; |
2543 |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
2544 |
{ |
{ |
2545 |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
2546 |
if (!( (v->x == backup[i+1].x) && (v->y == backup[i+1].y) )) |
if (!MVequal (*v, backup[i+1]) ) |
2547 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2548 |
} |
} |
2549 |
|
|
2562 |
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) |
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) |
2563 |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2564 |
|
|
2565 |
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & HALFPELREFINE8_BITS) |
2566 |
|
SubpelRefine(Data8); |
2567 |
|
|
2568 |
if(s > *Data8->iMinSAD) { //we have found a better match |
if(s > *Data8->iMinSAD) { //we have found a better match |
2569 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
2577 |
} |
} |
2578 |
if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
2579 |
|
|
2580 |
} else // not qpel |
} else { // not qpel |
2581 |
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement |
|
2582 |
|
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) //extsearch |
2583 |
|
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2584 |
|
|
2585 |
|
if (MotionFlags & HALFPELREFINE8_BITS) |
2586 |
|
SubpelRefine(Data8); //halfpel refinement |
2587 |
|
} |
2588 |
|
|
2589 |
//checking vector equal to predicion |
//checking vector equal to predicion |
2590 |
if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { |
if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { |
2591 |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
2592 |
if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) |
if (!MVequal(*v, Data8->predMV)) |
2593 |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
2594 |
} |
} |
2595 |
|
|
2596 |
bits += *Data8->iMinSAD; |
bits += *Data8->iMinSAD; |
2597 |
if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V |
if (bits >= Data->iMinSAD[0]) return bits; // no chances for INTER4V |
2598 |
|
|
2599 |
// MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else |
// MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else |
2600 |
if(Data->qpel) { |
if(Data->qpel) { |
2612 |
pMB->mvs[i] = *Data8->currentMV; |
pMB->mvs[i] = *Data8->currentMV; |
2613 |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
2614 |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
|
} |
|
2615 |
|
|
2616 |
if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma |
} // /for all luma blocks |
2617 |
const uint8_t * ptr; |
|
2618 |
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
2619 |
|
|
2620 |
|
// let's check chroma |
2621 |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
2622 |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
2623 |
|
|
2624 |
//chroma U |
//chroma U |
2625 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2626 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2627 |
fdct(in); |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4, 0); |
2628 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
|
2629 |
else i = quant4_inter(coeff, in, Data->lambda16); |
if (bits >= *Data->iMinSAD) return bits; |
|
if (i > 0) { |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp |= 1 << (5 - 4); |
|
|
} |
|
2630 |
|
|
|
if (bits < *Data->iMinSAD) { // still possible |
|
2631 |
//chroma V |
//chroma V |
2632 |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); |
2633 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2634 |
fdct(in); |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5, 0); |
2635 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
|
|
else i = quant4_inter(coeff, in, Data->lambda16); |
|
|
if (i > 0) { |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp |= 1 << (5 - 5); |
|
|
} |
|
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
|
2636 |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
|
} |
|
|
} |
|
2637 |
|
|
2638 |
return bits; |
return bits; |
2639 |
} |
} |
2644 |
{ |
{ |
2645 |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
2646 |
int cbp = 0, i, t, dc = 1024, b_dc; |
int cbp = 0, i, t, dc = 1024, b_dc; |
|
const uint32_t iQuant = Data->lambda16; |
|
2647 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
2648 |
uint32_t iDcScaler = get_dc_scaler(iQuant, 1);; |
uint32_t iDcScaler = get_dc_scaler(Data->iQuant, 1); |
2649 |
|
|
2650 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
2651 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2654 |
b_dc = in[0]; |
b_dc = in[0]; |
2655 |
in[0] -= dc; |
in[0] -= dc; |
2656 |
dc = b_dc; |
dc = b_dc; |
2657 |
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
2658 |
else quant4_intra(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
2659 |
|
|
2660 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len; |
2661 |
Data->temp[i] = t; |
Data->temp[i] = t; |
2662 |
if (t != 0) cbp |= 1 << (5 - i); |
if (t != 0) cbp |= 1 << (5 - i); |
2663 |
if (bits >= Data->iMinSAD[0]) break; |
if (bits >= Data->iMinSAD[0]) return bits; |
2664 |
} |
} |
2665 |
|
|
2666 |
if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma |
bits += xvid_cbpy_tab[cbp>>2].len; |
2667 |
iDcScaler = get_dc_scaler(iQuant, 0); |
|
2668 |
|
iDcScaler = get_dc_scaler(Data->iQuant, 0); |
2669 |
|
|
2670 |
//chroma U |
//chroma U |
2671 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2672 |
fdct(in); |
fdct(in); |
2673 |
in[0] -= 1024; |
in[0] -= 1024; |
2674 |
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
2675 |
else quant4_intra(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
2676 |
|
|
2677 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2678 |
if (t != 0) cbp |= 1 << (5 - 4); |
if (t != 0) cbp |= 1 << (5 - 4); |
2679 |
|
|
2680 |
if (bits < Data->iMinSAD[0]) { |
if (bits >= Data->iMinSAD[0]) return bits; |
2681 |
|
|
2682 |
//chroma V |
//chroma V |
2683 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2684 |
fdct(in); |
fdct(in); |
2685 |
in[0] -= 1024; |
in[0] -= 1024; |
2686 |
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
2687 |
else quant4_intra(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
2688 |
|
|
2689 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2690 |
if (t != 0) cbp |= 1 << (5 - 5); |
if (t != 0) cbp |= 1 << (5 - 5); |
2691 |
|
|
|
bits += xvid_cbpy_tab[cbp>>2].len; |
|
2692 |
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
2693 |
} |
|
|
} |
|
2694 |
return bits; |
return bits; |
2695 |
} |
} |