21 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: motion_est.c,v 1.58.2.21 2003-07-10 17:41:48 edgomez Exp $ |
* $Id: motion_est.c,v 1.58.2.22 2003-07-13 09:58:44 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
72 |
#define CHECK_CANDIDATE(X,Y,D) { \ |
#define CHECK_CANDIDATE(X,Y,D) { \ |
73 |
CheckCandidate((X),(Y), (D), &iDirection, data ); } |
CheckCandidate((X),(Y), (D), &iDirection, data ); } |
74 |
|
|
75 |
|
|
76 |
/***************************************************************************** |
/***************************************************************************** |
77 |
* Code |
* Code |
78 |
****************************************************************************/ |
****************************************************************************/ |
336 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t * sad)>>10; |
337 |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; |
338 |
|
|
339 |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
if (data->chroma && sad < data->iMinSAD[0]) |
340 |
|
sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
341 |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
342 |
|
|
343 |
if (sad < data->iMinSAD[0]) { |
if (sad < data->iMinSAD[0]) { |
447 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
448 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t * sad)>>10; |
449 |
|
|
450 |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
if (data->chroma && sad < *data->iMinSAD) |
451 |
|
sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
452 |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
453 |
|
|
454 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
495 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
496 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
497 |
|
|
498 |
sad = sad32v_c(data->Cur, data->RefP[0] + (int)((x>>1) + (y>>1)*(data->iEdgedWidth)), |
sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*((int)data->iEdgedWidth), |
499 |
data->iEdgedWidth, data->temp+1); |
data->iEdgedWidth, data->temp+1); |
500 |
|
|
501 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
548 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
549 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t * sad)>>10; |
550 |
|
|
551 |
if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
if (data->chroma && sad < *data->iMinSAD) |
552 |
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
553 |
(ycf >> 1) + roundtab_79[ycf & 0x3], |
(ycf >> 1) + roundtab_79[ycf & 0x3], |
554 |
(xcb >> 1) + roundtab_79[xcb & 0x3], |
(xcb >> 1) + roundtab_79[xcb & 0x3], |
555 |
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
609 |
|
|
610 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
611 |
|
|
612 |
if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
if (data->chroma && sad < *data->iMinSAD) |
613 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
614 |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
615 |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
616 |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
662 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
663 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
664 |
|
|
665 |
if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
if (data->chroma && sad < *data->iMinSAD) |
666 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
667 |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
668 |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
669 |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
708 |
bits += t = BITS_MULT*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); |
709 |
|
|
710 |
if (data->temp[0] + t < data->iMinSAD[1]) { |
if (data->temp[0] + t < data->iMinSAD[1]) { |
711 |
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | cbp&32; } |
712 |
if (data->temp[1] < data->iMinSAD[2]) { |
if (data->temp[1] < data->iMinSAD[2]) { |
713 |
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; data->cbp[1] = (data->cbp[1]&~16) | cbp&16; } |
714 |
if (data->temp[2] < data->iMinSAD[3]) { |
if (data->temp[2] < data->iMinSAD[3]) { |
715 |
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; data->cbp[1] = (data->cbp[1]&~8) | cbp&8; } |
716 |
if (data->temp[3] < data->iMinSAD[4]) { |
if (data->temp[3] < data->iMinSAD[4]) { |
717 |
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | cbp&4; } |
718 |
|
|
719 |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
720 |
|
|
725 |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
726 |
|
|
727 |
/* chroma U */ |
/* chroma U */ |
728 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
729 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
730 |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4); |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4); |
731 |
if (bits >= data->iMinSAD[0]) return; |
if (bits >= data->iMinSAD[0]) return; |
732 |
|
|
733 |
/* chroma V */ |
/* chroma V */ |
734 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
735 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
736 |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
737 |
|
|
738 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
741 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = bits; |
742 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
743 |
*dir = Direction; |
*dir = Direction; |
744 |
|
*data->cbp = cbp; |
745 |
} |
} |
746 |
} |
} |
747 |
|
|
771 |
bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
772 |
|
|
773 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
774 |
data->temp[0] = cbp; |
*data->cbp = cbp; |
775 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = bits; |
776 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
777 |
*dir = Direction; |
*dir = Direction; |
988 |
const uint32_t VopFlags, |
const uint32_t VopFlags, |
989 |
const uint32_t VolFlags, |
const uint32_t VolFlags, |
990 |
const IMAGE * const pCurrent, |
const IMAGE * const pCurrent, |
991 |
const IMAGE * const pRef) |
const IMAGE * const pRef, |
992 |
|
const IMAGE * const vGMC, |
993 |
|
const int coding_type) |
994 |
{ |
{ |
995 |
int mode = MODE_INTER; |
int mode = MODE_INTER; |
996 |
|
int mcsel = 0; |
997 |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
998 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
999 |
|
|
1000 |
const int skip_possible = (!(VolFlags & XVID_VOL_GMC)) && (pMB->dquant == 0); |
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
1001 |
|
|
1002 |
|
pMB->mcsel = 0; |
1003 |
|
|
1004 |
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { /* normal, fast, SAD-based mode decision */ |
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { /* normal, fast, SAD-based mode decision */ |
1005 |
int sad; |
int sad; |
1023 |
sad = 0; |
sad = 0; |
1024 |
} |
} |
1025 |
|
|
1026 |
|
/* mcsel */ |
1027 |
|
if (coding_type == S_VOP) { |
1028 |
|
|
1029 |
|
int32_t iSAD = sad16(Data->Cur, |
1030 |
|
vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); |
1031 |
|
|
1032 |
|
if (Data->chroma) { |
1033 |
|
iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
1034 |
|
iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
1035 |
|
} |
1036 |
|
|
1037 |
|
if (iSAD <= sad) { /* mode decision GMC */ |
1038 |
|
mode = MODE_INTER; |
1039 |
|
mcsel = 1; |
1040 |
|
sad = iSAD; |
1041 |
|
} |
1042 |
|
|
1043 |
|
} |
1044 |
|
|
1045 |
/* intra decision */ |
/* intra decision */ |
1046 |
|
|
1047 |
if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */ |
if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */ |
1050 |
if (x != 0) |
if (x != 0) |
1051 |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
1052 |
|
|
1053 |
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? */ |
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ |
1054 |
if (Data->rrv) InterBias *= 4; |
if (Data->rrv) InterBias *= 4; |
1055 |
|
|
1056 |
if (InterBias < pMB->sad16) { |
if (InterBias < sad) { |
1057 |
int32_t deviation; |
int32_t deviation; |
1058 |
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
if (!Data->rrv) |
1059 |
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
deviation = dev16(Data->Cur, Data->iEdgedWidth); |
1060 |
|
else |
1061 |
|
deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */ |
1062 |
dev16(Data->Cur+16, Data->iEdgedWidth) + |
dev16(Data->Cur+16, Data->iEdgedWidth) + |
1063 |
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
1064 |
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
1066 |
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
1067 |
} |
} |
1068 |
|
|
1069 |
|
pMB->cbp = 63; |
1070 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
1071 |
|
|
1072 |
} else { /* BITS */ |
} else { /* BITS */ |
1073 |
|
|
1074 |
int bits, intra, i; |
int bits, intra, i, cbp, c[2] = {0, 0}; |
1075 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
1076 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
1077 |
|
Data->cbp = c; |
1078 |
|
|
1079 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
1080 |
for (i = 0; i < 5; i++) { |
for (i = 0; i < 5; i++) { |
1083 |
} |
} |
1084 |
|
|
1085 |
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
1086 |
if (bits == 0) |
cbp = *Data->cbp; |
1087 |
mode = MODE_INTER; /* quick stop */ |
|
1088 |
else { |
if (coding_type == S_VOP) { |
1089 |
|
int bits_gmc; |
1090 |
|
*Data->iMinSAD = bits += BITS_MULT*1; /* mcsel */ |
1091 |
|
bits_gmc = CountMBBitsGMC(Data, vGMC, x, y); |
1092 |
|
if (bits_gmc < bits) { |
1093 |
|
mcsel = 1; |
1094 |
|
*Data->iMinSAD = bits = bits_gmc; |
1095 |
|
mode = MODE_INTER; |
1096 |
|
cbp = *Data->cbp; |
1097 |
|
} |
1098 |
|
} |
1099 |
|
|
1100 |
if (inter4v) { |
if (inter4v) { |
1101 |
int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
int bits_4v; |
1102 |
if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } |
bits_4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
1103 |
|
if (bits_4v < bits) { |
1104 |
|
Data->iMinSAD[0] = bits = bits_4v; |
1105 |
|
mode = MODE_INTER4V; |
1106 |
|
cbp = *Data->cbp; |
1107 |
|
} |
1108 |
} |
} |
1109 |
|
|
1110 |
intra = CountMBBitsIntra(Data); |
intra = CountMBBitsIntra(Data); |
1111 |
|
if (intra < bits) { |
1112 |
if (intra < bits) { *Data->iMinSAD = bits = intra; mode = MODE_INTRA; } |
*Data->iMinSAD = bits = intra; |
1113 |
|
mode = MODE_INTRA; |
1114 |
} |
} |
1115 |
|
|
1116 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
1117 |
|
pMB->cbp = cbp; |
1118 |
} |
} |
1119 |
|
|
1120 |
if (Data->rrv) { |
if (Data->rrv) { |
1122 |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1123 |
} |
} |
1124 |
|
|
1125 |
if (mode == MODE_INTER) { |
if (mode == MODE_INTER && mcsel == 0) { |
1126 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
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]; |
|
1127 |
|
|
1128 |
if(Data->qpel) { |
if(Data->qpel) { |
1129 |
pMB->qmvs[0] = pMB->qmvs[1] |
pMB->qmvs[0] = pMB->qmvs[1] |
1135 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1136 |
} |
} |
1137 |
|
|
1138 |
} else if (mode == MODE_INTER4V) |
} else if (mode == MODE_INTER ) { // but mcsel == 1 |
1139 |
pMB->sad16 = Data->iMinSAD[0]; |
|
1140 |
|
pMB->mcsel = 1; |
1141 |
|
if (Data->qpel) { |
1142 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1143 |
|
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; |
1144 |
|
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; |
1145 |
|
} else |
1146 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1147 |
|
|
1148 |
|
} else |
1149 |
|
if (mode == MODE_INTER4V) ; /* anything here? */ |
1150 |
else /* INTRA, NOT_CODED */ |
else /* INTRA, NOT_CODED */ |
1151 |
SkipMacroblockP(pMB, 0); |
SkipMacroblockP(pMB, 0); |
1152 |
|
|
1242 |
|
|
1243 |
/* initial skip decision */ |
/* initial skip decision */ |
1244 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1245 |
if (!(current->vol_flags & XVID_VOL_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
1246 |
if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) |
if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) |
1247 |
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)) { |
1248 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
1256 |
|
|
1257 |
ModeDecision(&Data, pMB, pMBs, x, y, pParam, |
ModeDecision(&Data, pMB, pMBs, x, y, pParam, |
1258 |
MotionFlags, current->vop_flags, current->vol_flags, |
MotionFlags, current->vop_flags, current->vol_flags, |
1259 |
pCurrent, pRef); |
pCurrent, pRef, pGMC, current->coding_type); |
1260 |
|
|
1261 |
if (pMB->mode == MODE_INTRA) |
if (pMB->mode == MODE_INTRA) |
1262 |
if (++iIntra > iLimit) return 1; |
if (++iIntra > iLimit) return 1; |
2287 |
Data.temp = temp; |
Data.temp = temp; |
2288 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2289 |
|
|
|
|
|
2290 |
if (intraCount != 0) { |
if (intraCount != 0) { |
2291 |
if (intraCount < 10) // we're right after an I frame |
if (intraCount < 10) // we're right after an I frame |
2292 |
IntraThresh += 15* (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15* (intraCount - 10) * (intraCount - 10); |
2435 |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2436 |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2437 |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2438 |
|
*Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; // copy corresponding cbp bit |
2439 |
|
// *Data8->cbp = 1; |
2440 |
|
|
2441 |
if(Data->qpel) { |
if(Data->qpel) { |
2442 |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
2525 |
} |
} |
2526 |
pMB->mvs[i] = *Data8->currentMV; |
pMB->mvs[i] = *Data8->currentMV; |
2527 |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
2528 |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
if (Data8->cbp[0]) cbp |= 1 << (5 - i); |
2529 |
|
|
2530 |
} /* /for all luma blocks */ |
} /* end - for all luma blocks */ |
2531 |
|
|
2532 |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2533 |
|
|
2549 |
|
|
2550 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
2551 |
|
|
2552 |
|
*Data->cbp = cbp; |
2553 |
return bits; |
return bits; |
2554 |
} |
} |
2555 |
|
|
2585 |
return bits; |
return bits; |
2586 |
} |
} |
2587 |
|
|
2588 |
|
static int |
2589 |
|
CountMBBitsGMC(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y) |
2590 |
|
{ |
2591 |
|
int bits = BITS_MULT*1; /* this one is mcsel */ |
2592 |
|
int cbp = 0, i; |
2593 |
|
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
2594 |
|
|
2595 |
|
for(i = 0; i < 4; i++) { |
2596 |
|
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2597 |
|
transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth); |
2598 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i); |
2599 |
|
if (bits >= Data->iMinSAD[0]) return bits; |
2600 |
|
} |
2601 |
|
|
2602 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2603 |
|
|
2604 |
|
/*chroma U */ |
2605 |
|
transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
2606 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4); |
2607 |
|
|
2608 |
|
if (bits >= Data->iMinSAD[0]) return bits; |
2609 |
|
|
2610 |
|
/* chroma V */ |
2611 |
|
transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
2612 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5); |
2613 |
|
|
2614 |
|
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
2615 |
|
|
2616 |
|
*Data->cbp = cbp; |
2617 |
|
|
2618 |
|
return bits; |
2619 |
|
} |
2620 |
|
|
2621 |
|
|
2622 |
|
|
2635 |
{ |
{ |
2636 |
|
|
2637 |
int i=0; |
int i=0; |
|
// VECTOR pmv[3]; |
|
2638 |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
2639 |
|
|
2640 |
Data->iMinSAD[0] = MV_MAX_ERROR; |
Data->iMinSAD[0] = MV_MAX_ERROR; |
2641 |
|
|
2642 |
//median is only used as prediction. it doesn't have to be real |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
if (x == 0 && y == 0) |
|
|
Data->predMV.x = Data->predMV.y = 0; |
|
|
else |
|
|
if (x == 0) //left macroblock does not have any vector now |
|
|
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
|
|
else if (y == 0) // top macroblock doesn't have it's vector |
|
|
Data->predMV = (pMB-1)->mvs[0]; // left instead of median |
|
|
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
|
2643 |
|
|
2644 |
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, |
2645 |
pParam->width, pParam->height, Data->iFcode - ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, 0); |
pParam->width, pParam->height, 16, 0, 0); |
2646 |
|
|
2647 |
Data->Cur = pCur + 16*(x + y * pParam->edged_width); |
Data->Cur = pCur + 16*(x + y * pParam->edged_width); |
2648 |
Data->RefP[0] = pRef + 16*(x + y * pParam->edged_width); |
Data->RefP[0] = pRef + 16*(x + y * pParam->edged_width); |
2656 |
if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) ) |
if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) ) |
2657 |
CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data); |
CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data); |
2658 |
|
|
2659 |
if (Data->iMinSAD[0] > 256 /*4 * MAX_SAD00_FOR_SKIP*/) // diamond only if needed |
AdvDiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
|
DiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
|
2660 |
|
|
2661 |
SubpelRefine(Data); |
SubpelRefine(Data); |
2662 |
|
|
2674 |
|
|
2675 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
2676 |
pMB->sad16 = Data->iMinSAD[0]; |
pMB->sad16 = Data->iMinSAD[0]; |
2677 |
pMB->sad16 += d_mv_bits(pMB->mvs[0].x, pMB->mvs[0].y, Data->predMV, Data->iFcode, 0, 0); |
pMB->mode = MODE_INTER; |
2678 |
|
pMB->sad16 += 10*d_mv_bits(pMB->mvs[0].x, pMB->mvs[0].y, Data->predMV, Data->iFcode, 0, 0); |
2679 |
return; |
return; |
2680 |
} |
} |
2681 |
|
|
2698 |
memset(&Data, 0, sizeof(SearchData)); |
memset(&Data, 0, sizeof(SearchData)); |
2699 |
|
|
2700 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
|
Data.qpel = ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0); |
|
|
Data.qpel_precision = 0; |
|
2701 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
|
Data.chroma = current->motion_flags & XVID_ME_CHROMA16; |
|
|
Data.rrv = current->vop_flags & XVID_VOL_REDUCED_ENABLE; |
|
2702 |
|
|
2703 |
Data.currentMV = ¤tMV[0]; |
Data.currentMV = ¤tMV[0]; |
2704 |
Data.iMinSAD = &iMinSAD[0]; |
Data.iMinSAD = &iMinSAD[0]; |
2705 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
2706 |
Data.temp = temp; |
Data.temp = temp; |
|
Data.RefP[0] = pReference->y; |
|
|
Data.RefP[1] = pRefV->y; |
|
|
Data.RefP[2] = pRefH->y; |
|
|
Data.RefP[3] = pRefHV->y; |
|
2707 |
|
|
2708 |
CheckCandidate = CheckCandidate16I; |
CheckCandidate = CheckCandidate16I; |
2709 |
|
|
2729 |
const IMAGE * const pRefHV) |
const IMAGE * const pRefHV) |
2730 |
{ |
{ |
2731 |
|
|
2732 |
const unsigned int deltax=8; // upper bound for difference between a MV and it's neighbour MVs |
const int deltax=8; // upper bound for difference between a MV and it's neighbour MVs |
2733 |
const unsigned int deltay=8; |
const int deltay=8; |
2734 |
const unsigned int gradx=512; // lower bound for gradient in MB (ignore "flat" blocks) |
const unsigned int gradx=512; // lower bound for gradient in MB (ignore "flat" blocks) |
2735 |
const unsigned int grady=512; |
const unsigned int grady=512; |
2736 |
|
|