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->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i); |
|
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 = BITS_MULT*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 |
//8x8 blocks for inter4v mode |
676 |
|
if (data->temp[0] + t < data->iMinSAD[1]) { |
677 |
|
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
678 |
|
if (data->temp[1] < data->iMinSAD[2]) { |
679 |
|
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
680 |
|
if (data->temp[2] < data->iMinSAD[3]) { |
681 |
|
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
682 |
|
if (data->temp[3] < data->iMinSAD[4]) { |
683 |
|
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
684 |
|
|
685 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
686 |
|
|
687 |
|
if (bits >= data->iMinSAD[0]) return; |
688 |
|
|
689 |
|
//chroma |
690 |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
691 |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
692 |
|
|
693 |
//chroma U |
//chroma U |
694 |
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); |
695 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
696 |
fdct(in); |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4); |
697 |
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]); |
|
|
} |
|
698 |
|
|
|
if (bits < data->iMinSAD[0]) { |
|
699 |
//chroma V |
//chroma V |
700 |
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); |
701 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
702 |
fdct(in); |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
|
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]); |
|
|
} |
|
|
} |
|
|
} |
|
703 |
|
|
704 |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
|
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
|
705 |
|
|
706 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
707 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = bits; |
708 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
709 |
*dir = Direction; |
*dir = Direction; |
710 |
} |
} |
|
|
|
|
if (data->temp[0] + t < data->iMinSAD[1]) { |
|
|
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
|
|
if (data->temp[1] < data->iMinSAD[2]) { |
|
|
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
|
|
if (data->temp[2] < data->iMinSAD[3]) { |
|
|
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
|
|
if (data->temp[3] < data->iMinSAD[4]) { |
|
|
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
|
|
|
|
711 |
} |
} |
712 |
static void |
static void |
713 |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
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->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
735 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
bits += BITS_MULT*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, |
1087 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
1088 |
VECTOR currentQMV[5]; |
VECTOR currentQMV[5]; |
1089 |
int32_t iMinSAD[5]; |
int32_t iMinSAD[5]; |
1090 |
DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); |
1091 |
SearchData Data; |
SearchData Data; |
1092 |
memset(&Data, 0, sizeof(SearchData)); |
memset(&Data, 0, sizeof(SearchData)); |
1093 |
Data.iEdgedWidth = iEdgedWidth; |
Data.iEdgedWidth = iEdgedWidth; |
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!) */ |
1152 |
if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
1153 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * skip_thresh) |
if (pMB->dquant == NO_CHANGE && sad00 < quant * skip_thresh) |
1154 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
1155 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1156 |
continue; |
continue; |
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 |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
MotionFlags, current->global_flags, |
1166 |
if (!(current->global_flags & XVID_GMC || current->global_flags & XVID_MODEDECISION_BITS)) { |
pCurrent, pRef); |
1167 |
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); |
1292 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1293 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1294 |
|
|
1295 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) { |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (x | y)) { |
1296 |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
threshA = Data->temp[0]; // that's where we keep this SAD atm |
1297 |
if (threshA < 512) threshA = 512; |
if (threshA < 512) threshA = 512; |
1298 |
else if (threshA > 1024) threshA = 1024; |
else if (threshA > 1024) threshA = 1024; |
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; |
1371 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
1372 |
} |
} |
1373 |
|
|
1374 |
if (MotionFlags & PMV_QUARTERPELREFINE16) { |
if (Data->qpel) { |
|
|
|
1375 |
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, |
1376 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1377 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1378 |
|
if (MotionFlags & PMV_QUARTERPELREFINE16) |
1379 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1380 |
} |
} |
1381 |
|
|
1382 |
if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
if (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 |
1775 |
CheckCandidate(0, 0, 255, &k, Data); |
CheckCandidate(0, 0, 255, &k, Data); |
1776 |
|
|
1777 |
// initial (fast) skip decision |
// initial (fast) skip decision |
1778 |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) { |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) { |
1779 |
//possible skip |
//possible skip |
1780 |
if (Data->chroma) { |
if (Data->chroma) { |
1781 |
pMB->mode = MODE_DIRECT_NONE_MV; |
pMB->mode = MODE_DIRECT_NONE_MV; |
2159 |
} |
} |
2160 |
} |
} |
2161 |
|
|
2162 |
#define INTRA_THRESH 1800 |
#define INTRA_THRESH 2200 |
2163 |
#define INTER_THRESH 1200 |
#define INTER_THRESH 50 |
2164 |
|
#define INTRA_THRESH2 95 |
2165 |
|
|
2166 |
int |
int |
2167 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2176 |
int sSAD = 0; |
int sSAD = 0; |
2177 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
2178 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
2179 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + b_thresh; |
2180 |
int s = 0, blocks = 0; |
int s = 0, blocks = 0; |
2181 |
|
int complexity = 0; |
2182 |
|
|
2183 |
int32_t iMinSAD[5], temp[5]; |
int32_t iMinSAD[5], temp[5]; |
2184 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
2190 |
Data.temp = temp; |
Data.temp = temp; |
2191 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2192 |
|
|
2193 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
|
2194 |
|
if (intraCount != 0) { |
2195 |
|
if (intraCount < 10) // we're right after an I frame |
2196 |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
2197 |
else |
else |
2198 |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
2199 |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
2200 |
|
} |
2201 |
|
|
2202 |
InterThresh -= (350 - 8*b_thresh) * bCount; |
InterThresh -= 12 * bCount; |
2203 |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
if (InterThresh < 15 + b_thresh) InterThresh = 15 + b_thresh; |
2204 |
|
|
2205 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2206 |
|
|
2207 |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
for (y = 1; y < pParam->mb_height-1; y += 2) { |
2208 |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
for (x = 1; x < pParam->mb_width-1; x += 2) { |
2209 |
int i; |
int i; |
2210 |
blocks += 4; |
blocks += 10; |
2211 |
|
|
2212 |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
2213 |
else { //extrapolation of the vector found for last frame |
else { //extrapolation of the vector found for last frame |
2222 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
2223 |
int dev; |
int dev; |
2224 |
MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width]; |
|
if (pMB->sad16 > IntraThresh) { |
|
2225 |
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16, |
2226 |
pParam->edged_width); |
pParam->edged_width); |
2227 |
|
|
2228 |
|
complexity += dev; |
2229 |
if (dev + IntraThresh < pMB->sad16) { |
if (dev + IntraThresh < pMB->sad16) { |
2230 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
2231 |
if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP; |
if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP; |
2232 |
} |
} |
2233 |
} |
|
2234 |
if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++; |
if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
2235 |
|
if (dev > 500 && pMB->sad16 < 1000) |
2236 |
|
sSAD += 1000; |
2237 |
|
|
2238 |
sSAD += pMB->sad16; |
sSAD += pMB->sad16; |
2239 |
} |
} |
2240 |
} |
} |
2241 |
} |
} |
2242 |
|
complexity >>= 7; |
2243 |
|
|
2244 |
sSAD /= blocks; |
sSAD /= complexity + 4*blocks; |
|
s = (10*s) / blocks; |
|
|
|
|
|
if (s > 4) sSAD += (s - 2) * (60 - 2*b_thresh); //static block - looks bad when in bframe... |
|
2245 |
|
|
2246 |
|
if (intraCount > 12 && sSAD > INTRA_THRESH2 ) return I_VOP; |
2247 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2248 |
emms(); |
emms(); |
2249 |
return B_VOP; |
return B_VOP; |
2453 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
2454 |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
2455 |
|
|
|
//checking if this vector is perfect. if it is, we stop. |
|
|
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) |
|
|
return 0; //quick stop |
|
|
|
|
2456 |
if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search |
2457 |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
2458 |
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, |
2465 |
} else { // not qpel |
} else { // not qpel |
2466 |
|
|
2467 |
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
|
//checking if this vector is perfect. if it is, we stop. |
|
|
if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) { |
|
|
return 0; //inter |
|
|
} |
|
2468 |
} |
} |
2469 |
|
|
2470 |
if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
2494 |
return Data->iMinSAD[0]; |
return Data->iMinSAD[0]; |
2495 |
} |
} |
2496 |
|
|
|
|
|
2497 |
static int |
static int |
2498 |
CountMBBitsInter4v(const SearchData * const Data, |
CountMBBitsInter4v(const SearchData * const Data, |
2499 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
2506 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
2507 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
2508 |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
2509 |
|
uint8_t * ptr; |
2510 |
|
|
2511 |
memcpy(Data8, Data, sizeof(SearchData)); |
memcpy(Data8, Data, sizeof(SearchData)); |
2512 |
CheckCandidate = CheckCandidateBits8; |
CheckCandidate = CheckCandidateBits8; |
2513 |
|
|
2514 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { //for all luma blocks |
2515 |
|
|
2516 |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
2517 |
Data8->currentMV = Data->currentMV + i + 1; |
Data8->currentMV = Data->currentMV + i + 1; |
2518 |
Data8->currentQMV = Data->currentQMV + i + 1; |
Data8->currentQMV = Data->currentQMV + i + 1; |
2535 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
2536 |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
2537 |
|
|
2538 |
*Data8->iMinSAD += t; |
*Data8->iMinSAD += BITS_MULT*t; |
2539 |
|
|
2540 |
Data8->qpel_precision = Data8->qpel; |
Data8->qpel_precision = Data8->qpel; |
2541 |
// 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) |
2542 |
{ |
{ |
2543 |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
2544 |
if (!( (v->x == backup[i+1].x) && (v->y == backup[i+1].y) )) |
if (!MVequal (*v, backup[i+1]) ) |
2545 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2546 |
} |
} |
2547 |
|
|
2560 |
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) |
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) |
2561 |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2562 |
|
|
2563 |
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & HALFPELREFINE8_BITS) |
2564 |
|
SubpelRefine(Data8); |
2565 |
|
|
2566 |
if(s > *Data8->iMinSAD) { //we have found a better match |
if(s > *Data8->iMinSAD) { //we have found a better match |
2567 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
2575 |
} |
} |
2576 |
if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
2577 |
|
|
2578 |
} else // not qpel |
} else { // not qpel |
2579 |
if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement |
|
2580 |
|
if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) //extsearch |
2581 |
|
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2582 |
|
|
2583 |
|
if (MotionFlags & HALFPELREFINE8_BITS) |
2584 |
|
SubpelRefine(Data8); //halfpel refinement |
2585 |
|
} |
2586 |
|
|
2587 |
//checking vector equal to predicion |
//checking vector equal to predicion |
2588 |
if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { |
if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { |
2589 |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
2590 |
if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) |
if (!MVequal(*v, Data8->predMV)) |
2591 |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
2592 |
} |
} |
2593 |
|
|
2594 |
bits += *Data8->iMinSAD; |
bits += *Data8->iMinSAD; |
2595 |
if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V |
if (bits >= Data->iMinSAD[0]) return bits; // no chances for INTER4V |
2596 |
|
|
2597 |
// 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 |
2598 |
if(Data->qpel) { |
if(Data->qpel) { |
2610 |
pMB->mvs[i] = *Data8->currentMV; |
pMB->mvs[i] = *Data8->currentMV; |
2611 |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
2612 |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
|
} |
|
2613 |
|
|
2614 |
if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma |
} // /for all luma blocks |
2615 |
const uint8_t * ptr; |
|
2616 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2617 |
|
|
2618 |
|
// let's check chroma |
2619 |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
2620 |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
2621 |
|
|
2622 |
//chroma U |
//chroma U |
2623 |
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); |
2624 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2625 |
fdct(in); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4); |
2626 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
|
2627 |
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); |
|
|
} |
|
2628 |
|
|
|
if (bits < *Data->iMinSAD) { // still possible |
|
2629 |
//chroma V |
//chroma V |
2630 |
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); |
2631 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2632 |
fdct(in); |
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5); |
2633 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
|
2634 |
else i = quant4_inter(coeff, in, Data->lambda16); |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
|
if (i > 0) { |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp |= 1 << (5 - 5); |
|
|
} |
|
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
|
|
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
|
|
} |
|
|
} |
|
2635 |
|
|
2636 |
return bits; |
return bits; |
2637 |
} |
} |
2638 |
|
|
|
|
|
2639 |
static int |
static int |
2640 |
CountMBBitsIntra(const SearchData * const Data) |
CountMBBitsIntra(const SearchData * const Data) |
2641 |
{ |
{ |
2642 |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
int bits = BITS_MULT*1; //this one is ac/dc prediction flag bit |
2643 |
int cbp = 0, i, t, dc = 1024, b_dc; |
int cbp = 0, i, dc = 0; |
|
const uint32_t iQuant = Data->lambda16; |
|
2644 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, 1);; |
|
2645 |
|
|
2646 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
2647 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2648 |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
2649 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, &dc); |
2650 |
b_dc = in[0]; |
|
2651 |
in[0] -= dc; |
if (bits >= Data->iMinSAD[0]) return bits; |
|
dc = b_dc; |
|
|
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
|
|
else quant4_intra(coeff, in, iQuant, iDcScaler); |
|
|
|
|
|
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; |
|
|
Data->temp[i] = t; |
|
|
if (t != 0) cbp |= 1 << (5 - i); |
|
|
if (bits >= Data->iMinSAD[0]) break; |
|
2652 |
} |
} |
2653 |
|
|
2654 |
if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma |
bits += BITS_MULT*xvid_cbpy_tab[cbp>>2].len; |
2655 |
iDcScaler = get_dc_scaler(iQuant, 0); |
|
2656 |
//chroma U |
//chroma U |
2657 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2658 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, &dc); |
|
in[0] -= 1024; |
|
|
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
|
|
else quant4_intra(coeff, in, iQuant, iDcScaler); |
|
2659 |
|
|
2660 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
if (bits >= Data->iMinSAD[0]) return bits; |
|
if (t != 0) cbp |= 1 << (5 - 4); |
|
2661 |
|
|
|
if (bits < Data->iMinSAD[0]) { |
|
2662 |
//chroma V |
//chroma V |
2663 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2664 |
fdct(in); |
bits += Block_CalcBitsIntra(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, &dc); |
|
in[0] -= 1024; |
|
|
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
|
|
else quant4_intra(coeff, in, iQuant, iDcScaler); |
|
2665 |
|
|
2666 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
if (t != 0) cbp |= 1 << (5 - 5); |
|
2667 |
|
|
|
bits += xvid_cbpy_tab[cbp>>2].len; |
|
|
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
|
|
} |
|
|
} |
|
2668 |
return bits; |
return bits; |
2669 |
} |
} |