--- branches/dev-api-4/xvidcore/src/motion/motion_est.c 2003/08/25 15:10:30 1129 +++ branches/dev-api-4/xvidcore/src/motion/motion_est.c 2003/09/05 10:01:50 1137 @@ -21,7 +21,7 @@ * along with this program ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: motion_est.c,v 1.58.2.28 2003-08-25 15:10:13 syskin Exp $ + * $Id: motion_est.c,v 1.58.2.34 2003-09-05 10:01:50 Isibaar Exp $ * ****************************************************************************/ @@ -70,7 +70,7 @@ #define MAX_CHROMA_SAD_FOR_SKIP (22) #define CHECK_CANDIDATE(X,Y,D) { \ -CheckCandidate((X),(Y), (D), &iDirection, data ); } +CheckCandidate((X),(Y), data, (D) ); } /***************************************************************************** @@ -156,6 +156,7 @@ int sad; const uint32_t stride = data->iEdgedWidth/2; int offset = (dx>>1) + (dy>>1)*stride; + int next = 1; if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; /* it has been checked recently */ data->temp[5] = dx; data->temp[6] = dy; /* backup */ @@ -166,12 +167,10 @@ sad += sad8(data->CurV, data->RefP[5] + offset, stride); break; case 1: - sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + stride, stride); - sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + stride, stride); - break; + next = stride; case 2: - sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + 1, stride); - sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + 1, stride); + sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + next, stride); + sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + next, stride); break; default: interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding); @@ -301,7 +300,7 @@ /* CHECK_CANDIATE FUNCTIONS START */ static void -CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidate16(const int x, const int y, const SearchData * const data, const int Direction) { int xc, yc; const uint8_t * Reference; @@ -321,11 +320,11 @@ current = data->currentQMV; } - sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); + sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); sad += (data->lambda16 * t * sad)>>10; - data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; + data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; if (data->chroma && sad < data->iMinSAD[0]) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], @@ -334,21 +333,80 @@ if (sad < data->iMinSAD[0]) { data->iMinSAD[0] = sad; current[0].x = x; current[0].y = y; - *dir = Direction; + *data->dir = Direction; } - if (data->temp[1] < data->iMinSAD[1]) { - data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y = y; } - if (data->temp[2] < data->iMinSAD[2]) { - data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; } - if (data->temp[3] < data->iMinSAD[3]) { - data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; } - if (data->temp[4] < data->iMinSAD[4]) { - data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; } + if (data->temp[0] < data->iMinSAD[1]) { + data->iMinSAD[1] = data->temp[0]; 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; } } static void -CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidate16_subpel(const int x, const int y, const SearchData * const data, const int Direction) +{ + int xc, yc; + const uint8_t *Reference; + VECTOR *current, *current2; + int32_t sad; uint32_t t; + + if ( (x > data->max_dx) || (x < data->min_dx) + || (y > data->max_dy) || (y < data->min_dy) ) return; + + if (!data->qpel_precision) { + Reference = GetReference(x, y, data); + current = data->currentMV; + current2 = data->currentMV2; + xc = x; yc = y; + } else { /* x and y are in 1/4 precision */ + Reference = Interpolate16x16qpel(x, y, 0, data); + xc = x/2; yc = y/2; /* for chroma sad */ + current = data->currentQMV; + current2 = data->currentQMV2; + } + + sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); + t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); + + sad += (data->lambda16 * t * sad)>>10; + data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; + + if (data->chroma && sad < data->iMinSAD[0]) + sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], + (yc >> 1) + roundtab_79[yc & 0x3], data); + + if (data->temp[0] < data->iMinSAD[1]) { + data->iMinSAD[1] = data->temp[0]; 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; } + + if (sad < data->iMinSAD[0]) { + *(data->iMinSAD2) = *(data->iMinSAD); + current2->x = current->x; current2->y = current->y; + + data->iMinSAD[0] = sad; + current[0].x = x; current[0].y = y; + *data->dir = Direction; + return; + } + + if (sad < *(data->iMinSAD2)) { + *(data->iMinSAD2) = sad; + current2->x = x; current2->y = y; + *data->dir = Direction; + } +} + +static void +CheckCandidate8(const int x, const int y, const SearchData * const data, const int Direction) { int32_t sad; uint32_t t; const uint8_t * Reference; @@ -373,15 +431,16 @@ if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; current->x = x; current->y = y; - *dir = Direction; + *data->dir = Direction; } } static void -CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidate32(const int x, const int y, const SearchData * const data, const int Direction) { uint32_t t; const uint8_t * Reference; + int sad; if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */ (x > data->max_dx) || (x < data->min_dx) @@ -390,28 +449,29 @@ Reference = GetReference(x, y, data); t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1); - data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); + sad = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp); - data->temp[0] += (data->lambda16 * t * data->temp[0]) >> 10; - data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; + sad += (data->lambda16 * t * sad) >> 10; + data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; - if (data->temp[0] < data->iMinSAD[0]) { - data->iMinSAD[0] = data->temp[0]; + if (sad < data->iMinSAD[0]) { + data->iMinSAD[0] = sad; data->currentMV[0].x = x; data->currentMV[0].y = y; - *dir = Direction; } + *data->dir = Direction; + } - if (data->temp[1] < data->iMinSAD[1]) { - data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } - if (data->temp[2] < data->iMinSAD[2]) { - data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; } - if (data->temp[3] < data->iMinSAD[3]) { - data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; } - if (data->temp[4] < data->iMinSAD[4]) { - data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; } + if (data->temp[0] < data->iMinSAD[1]) { + data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; } + if (data->temp[1] < data->iMinSAD[2]) { + data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; } + if (data->temp[2] < data->iMinSAD[3]) { + data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; } + if (data->temp[3] < data->iMinSAD[4]) { + data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; } } static void -CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidate16no4v(const int x, const int y, const SearchData * const data, const int Direction) { int32_t sad, xc, yc; const uint8_t * Reference; @@ -445,12 +505,12 @@ if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; current->x = x; current->y = y; - *dir = Direction; + *data->dir = Direction; } } static void -CheckCandidate16I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidate16I(const int x, const int y, const SearchData * const data, const int Direction) { int sad; // int xc, yc; @@ -473,12 +533,12 @@ if (sad < data->iMinSAD[0]) { data->iMinSAD[0] = sad; data->currentMV[0].x = x; data->currentMV[0].y = y; - *dir = Direction; + *data->dir = Direction; } } static void -CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidate32I(const int x, const int y, const SearchData * const data, const int Direction) { /* maximum speed - for P/B/I decision */ int32_t sad; @@ -486,27 +546,27 @@ if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; - sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*((int)data->iEdgedWidth), - data->iEdgedWidth, data->temp+1); + sad = sad32v_c(data->Cur, data->RefP[0] + x + y*((int)data->iEdgedWidth), + data->iEdgedWidth, data->temp); if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; data->currentMV[0].x = x; data->currentMV[0].y = y; - *dir = Direction; + *data->dir = Direction; } - if (data->temp[1] < data->iMinSAD[1]) { - data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } - if (data->temp[2] < data->iMinSAD[2]) { - data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; } - if (data->temp[3] < data->iMinSAD[3]) { - data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; } - if (data->temp[4] < data->iMinSAD[4]) { - data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; } + if (data->temp[0] < data->iMinSAD[1]) { + data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; } + if (data->temp[1] < data->iMinSAD[2]) { + data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; } + if (data->temp[2] < data->iMinSAD[3]) { + data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; } + if (data->temp[3] < data->iMinSAD[4]) { + data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; } } static void -CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data) +CheckCandidateInt(const int xf, const int yf, const SearchData * const data, const int Direction) { int32_t sad, xb, yb, xcf, ycf, xcb, ycb; uint32_t t; @@ -548,12 +608,12 @@ if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; current->x = xf; current->y = yf; - *dir = Direction; + *data->dir = Direction; } } static void -CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidateDirect(const int x, const int y, const SearchData * const data, const int Direction) { int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; uint32_t k; @@ -609,12 +669,12 @@ if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; data->currentMV->x = x; data->currentMV->y = y; - *dir = Direction; + *data->dir = Direction; } } static void -CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidateDirectno4v(const int x, const int y, const SearchData * const data, const int Direction) { int32_t sad, xcf, ycf, xcb, ycb; const uint8_t *ReferenceF; @@ -662,13 +722,13 @@ if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; data->currentMV->x = x; data->currentMV->y = y; - *dir = Direction; + *data->dir = Direction; } } static void -CheckCandidateRD16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidateRD16(const int x, const int y, const SearchData * const data, const int Direction) { int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; @@ -731,13 +791,13 @@ if (rd < data->iMinSAD[0]) { data->iMinSAD[0] = rd; current[0].x = x; current[0].y = y; - *dir = Direction; + *data->dir = Direction; *data->cbp = cbp; } } static void -CheckCandidateRD8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +CheckCandidateRD8(const int x, const int y, const SearchData * const data, const int Direction) { int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; @@ -765,7 +825,7 @@ *data->cbp = cbp; data->iMinSAD[0] = rd; current[0].x = x; current[0].y = y; - *dir = Direction; + *data->dir = Direction; } } @@ -774,15 +834,15 @@ /* MAINSEARCH FUNCTIONS START */ static void -AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection) +AdvDiamondSearch(int x, int y, const SearchData * const data, int bDirection, CheckFunc * const CheckCandidate) { /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ - int iDirection; + unsigned int * const iDirection = data->dir; for(;;) { /* forever */ - iDirection = 0; + *iDirection = 0; if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1); if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2); if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4); @@ -790,9 +850,9 @@ /* now we're doing diagonal checks near our candidate */ - if (iDirection) { /* if anything found */ - bDirection = iDirection; - iDirection = 0; + if (*iDirection) { /* if anything found */ + bDirection = *iDirection; + *iDirection = 0; x = data->currentMV->x; y = data->currentMV->y; if (bDirection & 3) { /* our candidate is left or right */ CHECK_CANDIDATE(x, y + iDiamondSize, 8); @@ -802,8 +862,8 @@ CHECK_CANDIDATE(x - iDiamondSize, y, 1); } - if (iDirection) { - bDirection += iDirection; + if (*iDirection) { + bDirection += *iDirection; x = data->currentMV->x; y = data->currentMV->y; } } else { /* about to quit, eh? not so fast.... */ @@ -851,20 +911,20 @@ CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8); break; } - if (!iDirection) break; /* ok, the end. really */ - bDirection = iDirection; + if (!*iDirection) break; /* ok, the end. really */ + bDirection = *iDirection; x = data->currentMV->x; y = data->currentMV->y; } } } static void -SquareSearch(int x, int y, const SearchData * const data, int bDirection) +SquareSearch(int x, int y, const SearchData * const data, int bDirection, CheckFunc * const CheckCandidate) { - int iDirection; + unsigned int * const iDirection = data->dir; do { - iDirection = 0; + *iDirection = 0; if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1+16+64); if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2+32+128); if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4+16+32); @@ -874,21 +934,21 @@ if (bDirection & 64) CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1+8+16+64+128); if (bDirection & 128) CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2+8+32+64+128); - bDirection = iDirection; + bDirection = *iDirection; x = data->currentMV->x; y = data->currentMV->y; - } while (iDirection); + } while (*iDirection); } static void -DiamondSearch(int x, int y, const SearchData * const data, int bDirection) +DiamondSearch(int x, int y, const SearchData * const data, int bDirection, CheckFunc * const CheckCandidate) { /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ - int iDirection; + unsigned int * const iDirection = data->dir; do { - iDirection = 0; + *iDirection = 0; if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1); if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2); if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4); @@ -896,9 +956,9 @@ /* now we're doing diagonal checks near our candidate */ - if (iDirection) { /* checking if anything found */ - bDirection = iDirection; - iDirection = 0; + if (*iDirection) { /* checking if anything found */ + bDirection = *iDirection; + *iDirection = 0; x = data->currentMV->x; y = data->currentMV->y; if (bDirection & 3) { /* our candidate is left or right */ CHECK_CANDIDATE(x, y + iDiamondSize, 8); @@ -907,21 +967,120 @@ CHECK_CANDIDATE(x + iDiamondSize, y, 2); CHECK_CANDIDATE(x - iDiamondSize, y, 1); } - bDirection += iDirection; + bDirection += *iDirection; x = data->currentMV->x; y = data->currentMV->y; } } - while (iDirection); + while (*iDirection); } /* MAINSEARCH FUNCTIONS END */ static void -SubpelRefine(const SearchData * const data) +SubpelRefine_Fast(SearchData * data, CheckFunc * CheckCandidate) +{ +/* Do a half-pel or q-pel refinement */ + VECTOR centerMV; + VECTOR second_best; + int best_sad = *data->iMinSAD; + int xo, yo, xo2, yo2; + int size = 2; + CheckFunc *backupFunc = CheckCandidate; + + if(data->qpel_precision) + size = 1; + + centerMV = *data->currentMV; + *data->iMinSAD = 256 * 4096; + + CHECK_CANDIDATE(centerMV.x, centerMV.y - size, 0); + CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, 0); + CHECK_CANDIDATE(centerMV.x + size, centerMV.y, 0); + CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, 0); + + CHECK_CANDIDATE(centerMV.x, centerMV.y + size, 0); + CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, 0); + CHECK_CANDIDATE(centerMV.x - size, centerMV.y, 0); + CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, 0); + + second_best = *data->currentMV; + + if(data->qpel_precision) { + second_best.x *= 2; second_best.y *= 2; + } + + data->currentMV[0] = centerMV; + *data->iMinSAD = best_sad; + + centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV; + + xo = centerMV.x; + yo = centerMV.y; + xo2 = second_best.x; + yo2 = second_best.y; + + CheckCandidate = CheckCandidate16_subpel; + *data->iMinSAD2 = 256 * 4096; + + if (yo == yo2) + { + CHECK_CANDIDATE((xo+xo2)>>1, yo, 0); + CHECK_CANDIDATE(xo, yo-1, 0); + CHECK_CANDIDATE(xo, yo+1, 0); + + if(best_sad <= *data->iMinSAD2) + goto ende; + + if(data->currentQMV[0].x == data->currentQMV2[0].x) { + CHECK_CANDIDATE((xo+xo2)>>1, yo-1, 0); + CHECK_CANDIDATE((xo+xo2)>>1, yo+1, 0); + goto ende; + } + else { + CHECK_CANDIDATE((xo+xo2)>>1, + (data->currentQMV[0].x == xo) ? data->currentQMV[0].y : data->currentQMV2[0].y, + 0); + goto ende; + } + } + + if (xo == xo2) + { + CHECK_CANDIDATE(xo, (yo+yo2)>>1, 0); + CHECK_CANDIDATE(xo-1, yo, 0); + CHECK_CANDIDATE(xo+1, yo, 0); + + if(best_sad < *data->iMinSAD2) + goto ende; + + if(data->currentQMV[0].y == data->currentQMV2[0].y) { + CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, 0); + CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, 0); + goto ende; + } + else { + CHECK_CANDIDATE((data->currentQMV[0].y == yo) ? data->currentQMV[0].x : data->currentQMV2[0].x, (yo+yo2)>>1, 0); + goto ende; + } + } + + CHECK_CANDIDATE(xo, (yo+yo2)>>1, 0); + CHECK_CANDIDATE((xo+xo2)>>1, yo, 0); + + if(best_sad <= *data->iMinSAD2) + goto ende; + + CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, 0); + +ende: + CheckCandidate = backupFunc; +} + +static void +SubpelRefine(const SearchData * const data, CheckFunc * const CheckCandidate) { /* Do a half-pel or q-pel refinement */ const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV; - int iDirection; /* only needed because macro expects it */ CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0); CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0); @@ -970,6 +1129,264 @@ } static __inline void +ModeDecision_Fast(SearchData * const Data, + MACROBLOCK * const pMB, + const MACROBLOCK * const pMBs, + const int x, const int y, + const MBParam * const pParam, + const uint32_t MotionFlags, + const uint32_t VopFlags, + const uint32_t VolFlags, + const IMAGE * const pCurrent, + const IMAGE * const pRef, + const IMAGE * const vGMC, + const int coding_type) +{ + int mode = MODE_INTER; + int mcsel = 0; + int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); + const uint32_t iQuant = pMB->quant; + const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); + int sad; + int min_rd = -1, intra_rd, i, cbp, c[2] = {0, 0}; + VECTOR backup[5], *v; + int sad_backup[5]; + int InterBias = MV16_INTER_BIAS; + int thresh = 0; + int count = 0; + int top = 0, top_right = 0, left = 0; + + pMB->mcsel = 0; + + /* INTER <-> INTER4V decision */ + if ((Data->iMinSAD[0] + 50 < Data->iMinSAD[1] + + Data->iMinSAD[2] + Data->iMinSAD[3] + Data->iMinSAD[4])) { /* normal, fast, SAD-based mode decision */ + 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; + } + + /* final skip decision, a.k.a. "the vector you found, really that good?" */ + if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) + if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) + if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { + mode = MODE_NOT_CODED; + goto early_out; + } + + /* mcsel */ + if (coding_type == S_VOP) { + + int32_t iSAD = sad16(Data->Cur, + vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); + + if (Data->chroma) { + iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); + iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); + } + + if (iSAD <= sad) { /* mode decision GMC */ + mode = MODE_INTER; + mcsel = 1; + sad = iSAD; + } + + } + } else { /* Rate-Distortion INTER<->INTER4V */ + Data->iQuant = iQuant; + Data->cbp = c; + v = Data->qpel ? Data->currentQMV : Data->currentMV; + + /* final skip decision, a.k.a. "the vector you found, really that good?" */ + if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) + if ( (100*Data->iMinSAD[0])/(pMB->sad16+1) > FINAL_SKIP_THRESH) + if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { + mode = MODE_NOT_CODED; + goto early_out; + } + + for (i = 0; i < 5; i++) { + sad_backup[i] = Data->iMinSAD[i]; + Data->iMinSAD[i] = 256*4096; + backup[i] = v[i]; + } + + min_rd = findRDinter(Data, pMBs, x, y, pParam, MotionFlags); + cbp = *Data->cbp; + sad = sad_backup[0]; + + if (coding_type == S_VOP) { + int gmc_rd; + int32_t iSAD = sad16(Data->Cur, + vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); + + if (Data->chroma) { + iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); + iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); + } + + *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ + gmc_rd = findRDgmc(Data, vGMC, x, y); + if (gmc_rd < min_rd) { + mcsel = 1; + *Data->iMinSAD = min_rd = gmc_rd; + mode = MODE_INTER; + cbp = *Data->cbp; + sad = iSAD; + } + } + + if (inter4v) { + int v4_rd; + v4_rd = findRDinter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); + if (v4_rd < min_rd) { + Data->iMinSAD[0] = min_rd = v4_rd; + mode = MODE_INTER4V; + cbp = *Data->cbp; + sad = sad_backup[1] + sad_backup[2] + + sad_backup[3] + sad_backup[4] + IMV16X16 * (int32_t)iQuant; + } + } + } + + left = top = top_right = -1; + thresh = 0; + + if(x > 0 && y > 0 && x < pParam->mb_width) { + left = (&pMBs[(x-1) + y * pParam->mb_width])->sad16; // left + top = (&pMBs[x + (y-1) * pParam->mb_width])->sad16; // top + top_right = (&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16; // top right + + if(((&pMBs[(x-1) + y * pParam->mb_width])->mode != MODE_INTRA) && + ((&pMBs[x + (y-1) * pParam->mb_width])->mode != MODE_INTRA) && + ((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mode != MODE_INTRA)) + thresh = MAX(MAX(top, left), top_right); + else + thresh = MIN(MIN(top, left), top_right); + } + + /* INTRA <-> INTER decision */ + if (sad < thresh) { /* normal, fast, SAD-based mode decision */ + /* 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; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ + if (Data->rrv) InterBias *= 4; + + if (InterBias < sad) { + int32_t deviation; + if (!Data->rrv) + deviation = dev16(Data->Cur, Data->iEdgedWidth); + else + deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */ + dev16(Data->Cur+16, Data->iEdgedWidth) + + dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + + dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); + + if (deviation < (sad - InterBias)) mode = MODE_INTRA; + } + + pMB->cbp = 63; + } else { /* Rate-Distortion INTRA<->INTER */ + if(min_rd < 0) { + Data->iQuant = iQuant; + Data->cbp = c; + v = Data->qpel ? Data->currentQMV : Data->currentMV; + + for (i = 0; i < 5; i++) { + Data->iMinSAD[i] = 256*4096; + backup[i] = v[i]; + } + + if(mode == MODE_INTER) { + min_rd = findRDinter(Data, pMBs, x, y, pParam, MotionFlags); + cbp = *Data->cbp; + + if (coding_type == S_VOP) { + int gmc_rd; + + *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ + gmc_rd = findRDgmc(Data, vGMC, x, y); + if (gmc_rd < min_rd) { + mcsel = 1; + *Data->iMinSAD = min_rd = gmc_rd; + mode = MODE_INTER; + cbp = *Data->cbp; + } + } + } + + if(mode == MODE_INTER4V) { + int v4_rd; + v4_rd = findRDinter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); + if (v4_rd < min_rd) { + Data->iMinSAD[0] = min_rd = v4_rd; + mode = MODE_INTER4V; + cbp = *Data->cbp; + } + } + } + + intra_rd = findRDintra(Data); + if (intra_rd < min_rd) { + *Data->iMinSAD = min_rd = intra_rd; + mode = MODE_INTRA; + } + + pMB->cbp = cbp; + } + +early_out: + pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; + + 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 (mode == MODE_INTER && mcsel == 0) { + pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[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 (mode == MODE_INTER ) { // but mcsel == 1 + + pMB->mcsel = 1; + if (Data->qpel) { + pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; + pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; + pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; + } else + pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; + + } else + if (mode == MODE_INTER4V) ; /* anything here? */ + else /* INTRA, NOT_CODED */ + ZeroMacroblockP(pMB, 0); + + pMB->mode = mode; +} + +static __inline void ModeDecision(SearchData * const Data, MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, @@ -1162,6 +1579,7 @@ uint32_t mb_height = pParam->mb_height; const uint32_t iEdgedWidth = pParam->edged_width; const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags); + int stat_thresh = 0; uint32_t x, y; uint32_t iIntra = 0; @@ -1171,18 +1589,25 @@ (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); /* some pre-initialized thingies for SearchP */ - int32_t temp[8]; + int32_t temp[8]; uint32_t dir; VECTOR currentMV[5]; VECTOR currentQMV[5]; + VECTOR currentMV2[5]; + VECTOR currentQMV2[5]; int32_t iMinSAD[5]; + int32_t iMinSAD2[5]; DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); SearchData Data; memset(&Data, 0, sizeof(SearchData)); Data.iEdgedWidth = iEdgedWidth; Data.currentMV = currentMV; Data.currentQMV = currentQMV; + Data.currentMV2 = currentMV2; + Data.currentQMV2 = currentQMV2; Data.iMinSAD = iMinSAD; + Data.iMinSAD2 = iMinSAD2; Data.temp = temp; + Data.dir = &dir; Data.iFcode = current->fcode; Data.rounding = pParam->m_rounding_type; Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); @@ -1203,6 +1628,7 @@ for (y = 0; y < mb_height; y++) { for (x = 0; x < mb_width; x++) { MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; + MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width]; if (!Data.rrv) pMB->sad16 = sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, @@ -1235,8 +1661,26 @@ } } + if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) { + if(x > 0 && y > 0 && x < pParam->mb_width) + if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], zeroMV) && + MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], zeroMV) && + MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], zeroMV) && + MVequal(prevMB->mvs[0], zeroMV)) { + stat_thresh = MAX((&pMBs[(x-1) + y * pParam->mb_width])->sad16, + MAX((&pMBs[x + (y-1) * pParam->mb_width])->sad16, + MAX((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16, + prevMB->sad16))); + } + else + stat_thresh = MIN((&pMBs[(x-1) + y * pParam->mb_width])->sad16, + MIN((&pMBs[x + (y-1) * pParam->mb_width])->sad16, + MIN((&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16, + prevMB->sad16))); + } + if ((current->vop_flags & XVID_VOP_CARTOON) && - (sad00 < pMB->quant * 4 * skip_thresh)) { /* favorize (0,0) vector for cartoons */ + (sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh)) { /* favorize (0,0) vector for cartoons */ ZeroMacroblockP(pMB, sad00); continue; } @@ -1245,9 +1689,16 @@ y, MotionFlags, current->vop_flags, current->vol_flags, &Data, pParam, pMBs, reference->mbs, pMB); - ModeDecision(&Data, pMB, pMBs, x, y, pParam, - MotionFlags, current->vop_flags, current->vol_flags, - pCurrent, pRef, pGMC, current->coding_type); + if(current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD) { + ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam, + MotionFlags, current->vop_flags, current->vol_flags, + pCurrent, pRef, pGMC, current->coding_type); + } + else { + ModeDecision(&Data, pMB, pMBs, x, y, pParam, + MotionFlags, current->vop_flags, current->vol_flags, + pCurrent, pRef, pGMC, current->coding_type); + } if (pMB->mode == MODE_INTRA) if (++iIntra > iLimit) return 1; @@ -1257,20 +1708,30 @@ return 0; } +/* check if given vector is equal to any vector checked before */ +static __inline int +vector_repeats(const VECTOR * const pmv, const int i) +{ + unsigned int j; + for (j = 0; j < i; j++) + if (MVequal(pmv[i], pmv[j])) return 1; /* same vector has been checked already */ + return 0; +} +/* make a binary mask that prevents diamonds/squares + from checking a vector which has been checked as a prediction */ static __inline int -make_mask(const VECTOR * const pmv, const int i) +make_mask(const VECTOR * const pmv, const int i, const int current) { - int mask = 255, j; + unsigned int mask = 255, j; for (j = 0; j < i; j++) { - if (MVequal(pmv[i], pmv[j])) return 0; /* same vector has been checked already */ - if (pmv[i].x == pmv[j].x) { - if (pmv[i].y == pmv[j].y + iDiamondSize) mask &= ~4; - else if (pmv[i].y == pmv[j].y - iDiamondSize) mask &= ~8; + if (pmv[current].x == pmv[j].x) { + if (pmv[current].y == pmv[j].y + iDiamondSize) mask &= ~4; + else if (pmv[current].y == pmv[j].y - iDiamondSize) mask &= ~8; } else - if (pmv[i].y == pmv[j].y) { - if (pmv[i].x == pmv[j].x + iDiamondSize) mask &= ~1; - else if (pmv[i].x == pmv[j].x - iDiamondSize) mask &= ~2; + if (pmv[current].y == pmv[j].y) { + if (pmv[current].x == pmv[j].x + iDiamondSize) mask &= ~1; + else if (pmv[current].x == pmv[j].x - iDiamondSize) mask &= ~2; } } return mask; @@ -1334,9 +1795,10 @@ MACROBLOCK * const pMB) { - int i, iDirection = 255, mask, threshA; + int i, threshA; VECTOR pmv[7]; int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); + CheckFunc * CheckCandidate; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); @@ -1359,6 +1821,7 @@ Data->lambda16 = lambda_vec16[pMB->quant]; Data->lambda8 = lambda_vec8[pMB->quant]; Data->qpel_precision = 0; + *Data->dir = 0; memset(Data->currentMV, 0, 5*sizeof(VECTOR)); @@ -1383,17 +1846,17 @@ prevMBs + x + y * pParam->mb_width, Data->rrv); if (!Data->rrv) { - if (inter4v | Data->chroma) CheckCandidate = CheckCandidate16; + if (inter4v) CheckCandidate = CheckCandidate16; else CheckCandidate = CheckCandidate16no4v; /* for extra speed */ } else CheckCandidate = CheckCandidate32; /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/ - for (i = 1; i < 7; i++) { - if (!(mask = make_mask(pmv, i)) ) continue; - CheckCandidate(pmv[i].x, pmv[i].y, mask, &iDirection, Data); - if (Data->iMinSAD[0] <= threshA) break; - } + for (i = 1; i < 7; i++) + if (!vector_repeats(pmv, i)) { + CheckCandidate(pmv[i].x, pmv[i].y, Data, i); + if (Data->iMinSAD[0] <= threshA) { i++; break; } + } if ((Data->iMinSAD[0] <= threshA) || (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && @@ -1402,11 +1865,13 @@ else { MainSearchFunc * MainSearchPtr; + int mask = make_mask(pmv, i, *Data->dir); // all vectors pmv[0..i-1] have been checked + if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch; else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; else MainSearchPtr = DiamondSearch; - MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); + MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate); /* extended search, diamond starting in 0,0 and in prediction. note that this search is/might be done in halfpel positions, @@ -1422,8 +1887,8 @@ if (!(MVequal(startMV, backupMV))) { bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; - CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data); - MainSearchPtr(startMV.x, startMV.y, Data, 255); + CheckCandidate(startMV.x, startMV.y, Data, 255); + MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate); if (bSAD < Data->iMinSAD[0]) { Data->currentMV[0] = backupMV; Data->iMinSAD[0] = bSAD; } @@ -1434,17 +1899,18 @@ if (!(MVequal(startMV, backupMV))) { bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; - CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data); - MainSearchPtr(startMV.x, startMV.y, Data, 255); + CheckCandidate(startMV.x, startMV.y, Data, 255); + MainSearchPtr(startMV.x, startMV.y, Data, 255, CheckCandidate); if (bSAD < Data->iMinSAD[0]) { Data->currentMV[0] = backupMV; - Data->iMinSAD[0] = bSAD; } + Data->iMinSAD[0] = bSAD; + } } } } if (MotionFlags & XVID_ME_HALFPELREFINE16) - SubpelRefine(Data); + SubpelRefine(Data, CheckCandidate); for(i = 0; i < 5; i++) { Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */ @@ -1456,7 +1922,10 @@ pParam->width, pParam->height, Data->iFcode, 2, 0); Data->qpel_precision = 1; if (MotionFlags & XVID_ME_QUARTERPELREFINE16) - SubpelRefine(Data); + if(MotionFlags & XVID_ME_FASTREFINE16) + SubpelRefine_Fast(Data, CheckCandidate); + else + SubpelRefine(Data, CheckCandidate); } if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30) @@ -1503,6 +1972,7 @@ SearchData * const Data) { int i = 0; + CheckFunc * CheckCandidate; Data->iMinSAD = OldData->iMinSAD + 1 + block; Data->currentMV = OldData->currentMV + 1 + block; Data->currentQMV = OldData->currentQMV + 1 + block; @@ -1545,7 +2015,7 @@ else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; else MainSearchPtr = DiamondSearch; - MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255); + MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate); if(*(Data->iMinSAD) < temp_sad) { Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */ @@ -1556,7 +2026,7 @@ if (MotionFlags & XVID_ME_HALFPELREFINE8) { int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */ - SubpelRefine(Data); /* perform halfpel refine of current best vector */ + SubpelRefine(Data, CheckCandidate); /* perform halfpel refine of current best vector */ if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */ Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */ @@ -1568,7 +2038,7 @@ Data->qpel_precision = 1; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, pParam->width, pParam->height, Data->iFcode, 2, 0); - SubpelRefine(Data); + SubpelRefine(Data, CheckCandidate); } } @@ -1654,7 +2124,7 @@ SearchData * const Data) { - int i, iDirection = 255, mask; + int i; VECTOR pmv[7]; MainSearchFunc *MainSearchPtr; *Data->iMinSAD = MV_MAX_ERROR; @@ -1680,21 +2150,22 @@ PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); Data->currentMV->x = Data->currentMV->y = 0; - CheckCandidate = CheckCandidate16no4v; /* main loop. checking all predictions */ - for (i = 0; i < 7; i++) { - if (!(mask = make_mask(pmv, i)) ) continue; - CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); - } + for (i = 0; i < 7; i++) + if (!vector_repeats(pmv, i) ) + CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i); if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch; else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; else MainSearchPtr = DiamondSearch; - MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection); + if (*Data->iMinSAD > 512) { + unsigned int mask = make_mask(pmv, 7, *Data->dir); + MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); + } - SubpelRefine(Data); + SubpelRefine(Data, CheckCandidate16no4v); if (Data->qpel && *Data->iMinSAD < *best_sad + 300) { Data->currentQMV->x = 2*Data->currentMV->x; @@ -1702,7 +2173,7 @@ Data->qpel_precision = 1; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, iFcode, 2, 0); - SubpelRefine(Data); + SubpelRefine(Data, CheckCandidate16no4v); } /* three bits are needed to code backward mode. four for forward */ @@ -1742,16 +2213,15 @@ { int dx = 0, dy = 0, b_dx = 0, b_dy = 0; int32_t sum; - const int div = 1 + Data->qpel; int k; const uint32_t stride = Data->iEdgedWidth/2; /* this is not full chroma compensation, only it's fullpel approximation. should work though */ for (k = 0; k < 4; k++) { - dy += Data->directmvF[k].y / div; - dx += Data->directmvF[k].x / div; - b_dy += Data->directmvB[k].y / div; - b_dx += Data->directmvB[k].x / div; + dy += Data->directmvF[k].y >> Data->qpel; + dx += Data->directmvF[k].x >> Data->qpel; + b_dy += Data->directmvB[k].y >> Data->qpel; + b_dx += Data->directmvB[k].x >> Data->qpel; } dy = (dy >> 3) + roundtab_76[dy & 0xf]; @@ -1764,14 +2234,14 @@ b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, stride); - if (sum >= MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */ + if (sum >= MAX_CHROMA_SAD_FOR_SKIP * Data->iQuant) return; /* no skip */ sum += sad8bi(pCur->v + 8*x + 8 * y * stride, f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, stride); - if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { + if (sum < MAX_CHROMA_SAD_FOR_SKIP * Data->iQuant) { pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */ for (k = 0; k < 4; k++) { pMB->qmvs[k] = pMB->mvs[k]; @@ -1803,6 +2273,7 @@ int32_t skip_sad; int k = (x + Data->iEdgedWidth*y) * 16; MainSearchFunc *MainSearchPtr; + CheckFunc * CheckCandidate; *Data->iMinSAD = 256*4096; Data->RefP[0] = f_Ref->y + k; @@ -1852,10 +2323,10 @@ CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; - CheckCandidate(0, 0, 255, &k, Data); + CheckCandidate(0, 0, Data, 255); /* initial (fast) skip decision */ - if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) { + if (*Data->iMinSAD < Data->iQuant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) { /* possible skip */ if (Data->chroma) { pMB->mode = MODE_DIRECT_NONE_MV; @@ -1878,9 +2349,9 @@ else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; else MainSearchPtr = DiamondSearch; - MainSearchPtr(0, 0, Data, 255); + MainSearchPtr(0, 0, Data, 255, CheckCandidate); - SubpelRefine(Data); + SubpelRefine(Data, CheckCandidate); *best_sad = *Data->iMinSAD; @@ -1939,7 +2410,7 @@ { - int iDirection, i, j; + int i, j; SearchData bData; fData->qpel_precision = 0; @@ -1962,6 +2433,7 @@ bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.dir = fData->dir; bData.bpredMV = fData->predMV = *f_predMV; fData->bpredMV = bData.predMV = *b_predMV; @@ -1980,33 +2452,32 @@ if (fData->currentMV[1].y > bData.max_dy) fData->currentMV[1].y = bData.max_dy; if (fData->currentMV[1].y < bData.min_dy) fData->currentMV[1].y = bData.min_dy; - CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, 255, &iDirection, fData); + CheckCandidateInt(fData->currentMV[0].x, fData->currentMV[0].y, fData, 255); /* diamond */ do { - iDirection = 255; + *fData->dir = 255; /* forward MV moves */ i = fData->currentMV[0].x; j = fData->currentMV[0].y; - CheckCandidateInt(i + 1, j, 0, &iDirection, fData); - CheckCandidateInt(i, j + 1, 0, &iDirection, fData); - CheckCandidateInt(i - 1, j, 0, &iDirection, fData); - CheckCandidateInt(i, j - 1, 0, &iDirection, fData); + CheckCandidateInt(i + 1, j, fData, 0); + CheckCandidateInt(i, j + 1, fData, 0); + CheckCandidateInt(i - 1, j, fData, 0); + CheckCandidateInt(i, j - 1, fData, 0); /* backward MV moves */ i = fData->currentMV[1].x; j = fData->currentMV[1].y; fData->currentMV[2] = fData->currentMV[0]; - CheckCandidateInt(i + 1, j, 0, &iDirection, &bData); - CheckCandidateInt(i, j + 1, 0, &iDirection, &bData); - CheckCandidateInt(i - 1, j, 0, &iDirection, &bData); - CheckCandidateInt(i, j - 1, 0, &iDirection, &bData); + CheckCandidateInt(i + 1, j, &bData, 0); + CheckCandidateInt(i, j + 1, &bData, 0); + CheckCandidateInt(i - 1, j, &bData, 0); + CheckCandidateInt(i, j - 1, &bData, 0); - } while (!(iDirection)); + } while (!(*fData->dir)); /* qpel refinement */ if (fData->qpel) { if (*fData->iMinSAD > *best_sad + 500) return; - CheckCandidate = CheckCandidateInt; fData->qpel_precision = bData.qpel_precision = 1; get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0); get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); @@ -2014,10 +2485,10 @@ fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; fData->currentQMV[1].x = 2 * fData->currentMV[1].x; fData->currentQMV[1].y = 2 * fData->currentMV[1].y; - SubpelRefine(fData); + SubpelRefine(fData, CheckCandidateInt); if (*fData->iMinSAD > *best_sad + 300) return; fData->currentQMV[2] = fData->currentQMV[0]; - SubpelRefine(&bData); + SubpelRefine(&bData, CheckCandidateInt); } *fData->iMinSAD += (2+3) * fData->lambda16; /* two bits are needed to code interpolate mode. */ @@ -2076,6 +2547,7 @@ SearchData Data; int32_t iMinSAD; + uint32_t dir; VECTOR currentMV[3]; VECTOR currentQMV[3]; int32_t temp[8]; @@ -2083,11 +2555,13 @@ Data.iEdgedWidth = pParam->edged_width; Data.currentMV = currentMV; Data.currentQMV = currentQMV; Data.iMinSAD = &iMinSAD; - Data.lambda16 = lambda_vec16[frame->quant]; + Data.lambda16 = lambda_vec16[MAX(frame->quant-2, 2)]; Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; Data.rounding = 0; Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; Data.temp = temp; + Data.dir = &dir; + Data.iQuant = frame->quant; Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ @@ -2110,7 +2584,6 @@ Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; - pMB->quant = frame->quant; /* direct search comes first, because it (1) checks for SKIP-mode and (2) sets very good predictions for forward and backward search */ @@ -2156,7 +2629,7 @@ &Data); /* final skip decision */ - if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2) + if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * 2) && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data); @@ -2194,57 +2667,67 @@ SearchData * const Data) { - int i, mask; - int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0; + int i; VECTOR pmv[3]; MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; - for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; + unsigned int simplicity = 0; - /* median is only used as prediction. it doesn't have to be real */ - if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; - else - if (x == 1) /* left macroblock does not have any vector now */ - Data->predMV = (pMB - pParam->mb_width)->mvs[0]; /* top instead of median */ - else if (y == 1) /* 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 */ + for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, - pParam->width, pParam->height, Data->iFcode - quarterpel, 1, 0); + pParam->width, pParam->height, Data->iFcode - Data->qpel - 1, 0, 0); Data->Cur = pCur + (x + y * pParam->edged_width) * 16; Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; - pmv[1].x = EVEN(pMB->mvs[0].x); - pmv[1].y = EVEN(pMB->mvs[0].y); - pmv[2].x = EVEN(Data->predMV.x); - pmv[2].y = EVEN(Data->predMV.y); - pmv[0].x = pmv[0].y = 0; - - CheckCandidate32I(0, 0, 255, &i, Data); - - if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) { - - if (!(mask = make_mask(pmv, 1))) - CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); - if (!(mask = make_mask(pmv, 2))) - CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); + pmv[0].x = pMB->mvs[0].x; + pmv[0].y = pMB->mvs[0].y; - if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) /* diamond only if needed */ - DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); - } + CheckCandidate32I(pmv[0].x, pmv[0].y, Data, 0); + + if (*Data->iMinSAD > 200) { + + pmv[1].x = pmv[1].y = 0; + + /* median is only used as prediction. it doesn't have to be real */ + if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; + else + if (x == 1) /* left macroblock does not have any vector now */ + Data->predMV = (pMB - pParam->mb_width)->mvs[0]; /* top instead of median */ + else if (y == 1) /* 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 */ + + pmv[2].x = Data->predMV.x; + pmv[2].y = Data->predMV.y; + + if (!vector_repeats(pmv, 1)) + CheckCandidate32I(pmv[1].x, pmv[1].y, Data, 1); + if (!vector_repeats(pmv, 2)) + CheckCandidate32I(pmv[2].x, pmv[2].y, Data, 2); + + if (*Data->iMinSAD > 500) { /* diamond only if needed */ + unsigned int mask = make_mask(pmv, 3, *Data->dir); + DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate32I); + } else simplicity++; + + if (*Data->iMinSAD > 500) /* refinement from 2-pixel to 1-pixel */ + SubpelRefine(Data, CheckCandidate32I); + else simplicity++; + } else simplicity++; for (i = 0; i < 4; i++) { MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; MB->mode = MODE_INTER; - MB->sad16 = Data->iMinSAD[i+1]; + /* if we skipped some search steps, we have to assume that SAD would be lower with them */ + MB->sad16 = Data->iMinSAD[i+1] - (simplicity<<7); } } #define INTRA_THRESH 2200 -#define INTER_THRESH 50 +#define INTER_THRESH 40 #define INTRA_THRESH2 95 int @@ -2265,6 +2748,7 @@ int complexity = 0; int32_t iMinSAD[5], temp[5]; + uint32_t dir; VECTOR currentMV[5]; SearchData Data; Data.iEdgedWidth = pParam->edged_width; @@ -2272,7 +2756,9 @@ Data.iMinSAD = iMinSAD; Data.iFcode = Current->fcode; Data.temp = temp; - CheckCandidate = CheckCandidate32I; + Data.dir = &dir; + Data.qpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0; + Data.qpel_precision = 0; if (intraCount != 0) { if (intraCount < 10) // we're right after an I frame @@ -2282,8 +2768,8 @@ IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; } - InterThresh -= 12 * bCount; - if (InterThresh < 15 + b_thresh) InterThresh = 15 + b_thresh; + InterThresh -= 20 * bCount; + if (InterThresh < 10 + b_thresh) InterThresh = 10 + b_thresh; if (sadInit) (*sadInit) (); @@ -2315,10 +2801,10 @@ } if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) - if (dev > 500 && pMB->sad16 < 1000) + if (dev > 1000 && pMB->sad16 < 1000) sSAD += 1000; - sSAD += (dev < 3000) ? pMB->sad16 : pMB->sad16/2; /* blocks with big contrast differences usually have large SAD - while they look very good in b-frames */ + sSAD += (dev < 4000) ? pMB->sad16 : pMB->sad16/2; /* blocks with big contrast differences usually have large SAD - while they look very good in b-frames */ } } } @@ -2341,18 +2827,16 @@ const MBParam * const pParam, const uint32_t MotionFlags) { - int i, iDirection; + int i; int32_t bsad[5]; - CheckCandidate = CheckCandidateRD16; - if (Data->qpel) { for(i = 0; i < 5; i++) { Data->currentMV[i].x = Data->currentQMV[i].x/2; Data->currentMV[i].y = Data->currentQMV[i].y/2; } Data->qpel_precision = 1; - CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); + CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, Data, 255); if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */ for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; @@ -2360,17 +2844,19 @@ pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); Data->qpel_precision = 0; if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) - CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); + CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); } } else { /* not qpel */ - CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); + CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); } - if (MotionFlags&XVID_ME_EXTSEARCH_RD) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); + if (MotionFlags&XVID_ME_EXTSEARCH_RD) + SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); - if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) SubpelRefine(Data); + if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) + SubpelRefine(Data, CheckCandidateRD16); if (Data->qpel) { if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */ @@ -2384,13 +2870,14 @@ get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, Data->iFcode, 2, 0); } - if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) SubpelRefine(Data); + if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) + SubpelRefine(Data, CheckCandidateRD16); } if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */ VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; if (!(Data->predMV.x == v->x && Data->predMV.y == v->y)) - CheckCandidateRD16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); + CheckCandidateRD16(Data->predMV.x, Data->predMV.y, Data, 255); } return Data->iMinSAD[0]; } @@ -2403,15 +2890,14 @@ const VECTOR * const backup) { - int cbp = 0, bits = 0, t = 0, i, iDirection; + int cbp = 0, bits = 0, t = 0, i; SearchData Data2, *Data8 = &Data2; int sumx = 0, sumy = 0; int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; uint8_t * ptr; memcpy(Data8, Data, sizeof(SearchData)); - CheckCandidate = CheckCandidateRD8; - + for (i = 0; i < 4; i++) { /* for all luma blocks */ Data8->iMinSAD = Data->iMinSAD + i + 1; @@ -2444,7 +2930,7 @@ { VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; if (!MVequal (*v, backup[i+1]) ) - CheckCandidateRD8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); + CheckCandidateRD8(backup[i+1].x, backup[i+1].y, Data8, 255); } if (Data8->qpel) { @@ -2457,13 +2943,13 @@ pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) - CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); + CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, Data8, 255); if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) - SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); + SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) - SubpelRefine(Data8); + SubpelRefine(Data8, CheckCandidateRD8); if(s > *Data8->iMinSAD) { /* we have found a better match */ Data8->currentQMV->x = 2*Data8->currentMV->x; @@ -2475,22 +2961,23 @@ pParam->width, pParam->height, Data8->iFcode, 2, 0); } - if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) SubpelRefine(Data8); + if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) + SubpelRefine(Data8, CheckCandidateRD8); } else { /* not qpel */ if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) /* extsearch */ - SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); + SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) - SubpelRefine(Data8); /* halfpel refinement */ + SubpelRefine(Data8, CheckCandidateRD8); /* halfpel refinement */ } /* checking vector equal to predicion */ if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_RD) { const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; if (!MVequal(*v, Data8->predMV)) - CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); + CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, Data8, 255); } bits += *Data8->iMinSAD; @@ -2637,14 +3124,14 @@ Data->RefP[3] = pRefHV + 16*(x + y * pParam->edged_width); Data->currentMV[0].x = Data->currentMV[0].y = 0; - CheckCandidate16I(0, 0, 255, &i, Data); + CheckCandidate16I(0, 0, Data, 255); if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) ) - CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data); + CheckCandidate16I(Data->predMV.x, Data->predMV.y, Data, 255); - AdvDiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); + DiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255, CheckCandidate16I); - SubpelRefine(Data); + SubpelRefine(Data, CheckCandidate16I); /* for QPel halfpel positions are worse than in halfpel mode :( */ @@ -2680,6 +3167,7 @@ int32_t iMinSAD[5], temp[5]; VECTOR currentMV[5]; + uint32_t dir; SearchData Data; memset(&Data, 0, sizeof(SearchData)); @@ -2690,14 +3178,12 @@ Data.iMinSAD = &iMinSAD[0]; Data.iFcode = current->fcode; Data.temp = temp; - - CheckCandidate = CheckCandidate16I; + Data.dir = &dir; if (sadInit) (*sadInit) (); for (y = 0; y < pParam->mb_height; y ++) { for (x = 0; x < pParam->mb_width; x ++) { - GMEanalyzeMB(pCurrent->y, pReference->y, pRefH->y, pRefV->y, pRefHV->y, x, y, pParam, pMBs, &Data); } }