--- estimation_pvop.c 2004/05/27 14:19:04 1.2.2.2 +++ estimation_pvop.c 2010/12/28 19:19:43 1.25 @@ -4,7 +4,7 @@ * - Motion Estimation for P- and S- VOPs - * * Copyright(C) 2002 Christoph Lampert - * 2002 Michael Militzer + * 2002-2010 Michael Militzer * 2002-2003 Radoslaw Czyz * * This program is free software ; you can redistribute it and/or modify @@ -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: estimation_pvop.c,v 1.2.2.2 2004/05/27 14:19:04 syskin Exp $ + * $Id: estimation_pvop.c,v 1.25 2010/12/28 19:19:43 Isibaar Exp $ * ****************************************************************************/ @@ -39,107 +39,75 @@ #include "motion.h" #include "sad.h" #include "motion_inlines.h" +#include "motion_smp.h" + static const int xvid_me_lambda_vec8[32] = - { 0 ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5), - (int)(1.15582*NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5), - (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5), - (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5), - (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5), - (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5), - (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5), - (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5), - (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5), - (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5), - (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5), - (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5), - (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5), - (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5), - (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5), - (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5) + { 0 ,(int)(1.0 * NEIGH_TEND_8X8 + 0.5), + (int)(2.0*NEIGH_TEND_8X8 + 0.5), (int)(3.0*NEIGH_TEND_8X8 + 0.5), + (int)(4.0*NEIGH_TEND_8X8 + 0.5), (int)(5.0*NEIGH_TEND_8X8 + 0.5), + (int)(6.0*NEIGH_TEND_8X8 + 0.5), (int)(7.0*NEIGH_TEND_8X8 + 0.5), + (int)(8.0*NEIGH_TEND_8X8 + 0.5), (int)(9.0*NEIGH_TEND_8X8 + 0.5), + (int)(10.0*NEIGH_TEND_8X8 + 0.5), (int)(11.0*NEIGH_TEND_8X8 + 0.5), + (int)(12.0*NEIGH_TEND_8X8 + 0.5), (int)(13.0*NEIGH_TEND_8X8 + 0.5), + (int)(14.0*NEIGH_TEND_8X8 + 0.5), (int)(15.0*NEIGH_TEND_8X8 + 0.5), + (int)(16.0*NEIGH_TEND_8X8 + 0.5), (int)(17.0*NEIGH_TEND_8X8 + 0.5), + (int)(18.0*NEIGH_TEND_8X8 + 0.5), (int)(19.0*NEIGH_TEND_8X8 + 0.5), + (int)(20.0*NEIGH_TEND_8X8 + 0.5), (int)(21.0*NEIGH_TEND_8X8 + 0.5), + (int)(22.0*NEIGH_TEND_8X8 + 0.5), (int)(23.0*NEIGH_TEND_8X8 + 0.5), + (int)(24.0*NEIGH_TEND_8X8 + 0.5), (int)(25.0*NEIGH_TEND_8X8 + 0.5), + (int)(26.0*NEIGH_TEND_8X8 + 0.5), (int)(27.0*NEIGH_TEND_8X8 + 0.5), + (int)(28.0*NEIGH_TEND_8X8 + 0.5), (int)(29.0*NEIGH_TEND_8X8 + 0.5), + (int)(30.0*NEIGH_TEND_8X8 + 0.5), (int)(31.0*NEIGH_TEND_8X8 + 0.5) }; static void CheckCandidate16(const int x, const int y, SearchData * const data, const unsigned int Direction) { const uint8_t * Reference; - int32_t sad; uint32_t t; + int32_t sad, xc, yc; uint32_t t; + VECTOR * current; if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; - Reference = GetReference(x, y, data); + if (data->qpel_precision) { /* x and y are in 1/4 precision */ + Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); + current = data->currentQMV; + xc = x/2; yc = y/2; + } else { + Reference = GetReference(x, y, data); + current = data->currentMV; + xc = x; yc = y; + } sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); - t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel, 0); + t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); - sad += (data->lambda16 * t * sad)>>10; - data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; + sad += (data->lambda16 * t); + data->temp[0] += (data->lambda8 * t); if (data->chroma) { if (sad >= data->iMinSAD[0]) goto no16; - sad += xvid_me_ChromaSAD((x >> 1) + roundtab_79[x & 0x3], - (y >> 1) + roundtab_79[y & 0x3], data); + sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], + (yc >> 1) + roundtab_79[yc & 0x3], data); } if (sad < data->iMinSAD[0]) { data->iMinSAD[0] = sad; - data->currentMV[0].x = x; data->currentMV[0].y = y; + current[0].x = x; current[0].y = y; data->dir = Direction; } no16: if (data->temp[0] < data->iMinSAD[1]) { - data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; } + 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]; data->currentMV[2].x = x; data->currentMV[2].y = y; } + 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]; data->currentMV[3].x = x; data->currentMV[3].y = y; } + 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]; data->currentMV[4].x = x; data->currentMV[4].y = y; } -} - -static void -CheckCandidate16_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) -{ - const uint8_t *Reference; - int32_t sad; uint32_t t; - - if ( (x > data->max_dx) || (x < data->min_dx) - || (y > data->max_dy) || (y < data->min_dy) ) return; - - Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); - - sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); - t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 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 < data->iMinSAD2) ) - sad += xvid_me_ChromaSAD(((x/2) >> 1) + roundtab_79[(x/2) & 0x3], - ((y/2) >> 1) + roundtab_79[(y/2) & 0x3], data); - - if (data->temp[0] < data->iMinSAD[1]) { - data->iMinSAD[1] = data->temp[0]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; } - if (data->temp[1] < data->iMinSAD[2]) { - data->iMinSAD[2] = data->temp[1]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; } - if (data->temp[2] < data->iMinSAD[3]) { - data->iMinSAD[3] = data->temp[2]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; } - if (data->temp[3] < data->iMinSAD[4]) { - data->iMinSAD[4] = data->temp[3]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; } - - if (sad < data->iMinSAD[0]) { - data->iMinSAD2 = *(data->iMinSAD); - data->currentQMV2.x = data->currentQMV->x; - data->currentQMV2.y = data->currentQMV->y; - - data->iMinSAD[0] = sad; - data->currentQMV[0].x = x; data->currentQMV[0].y = y; - } else if (sad < data->iMinSAD2) { - data->iMinSAD2 = sad; - data->currentQMV2.x = x; data->currentQMV2.y = y; - } + data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } } static void @@ -161,9 +129,9 @@ } sad = sad8(data->Cur, Reference, data->iEdgedWidth); - t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); + t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); - sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; + sad += (data->lambda8 * t); if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; @@ -172,98 +140,19 @@ } } -static void -CheckCandidate8_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) -{ - int32_t sad; uint32_t t; - const uint8_t * Reference; - VECTOR * current; - - if ( (x > data->max_dx) || (x < data->min_dx) - || (y > data->max_dy) || (y < data->min_dy) ) return; - - /* x and y are in 1/4 precision */ - Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); - current = data->currentQMV; - - sad = sad8(data->Cur, Reference, data->iEdgedWidth); - t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); - - sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; - - if (sad < *(data->iMinSAD)) { - data->iMinSAD2 = *(data->iMinSAD); - data->currentQMV2.x = data->currentQMV->x; - data->currentQMV2.y = data->currentQMV->y; - - *(data->iMinSAD) = sad; - data->currentQMV->x = x; data->currentQMV->y = y; - data->dir = Direction; - } else if (sad < data->iMinSAD2) { - data->iMinSAD2 = sad; - data->currentQMV2.x = x; data->currentQMV2.y = y; - } -} - -static void -CheckCandidate32(const int x, const int y, SearchData * const data, const unsigned 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) - || (y > data->max_dy) || (y < data->min_dy) ) return; - - Reference = GetReference(x, y, data); - t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1); - - sad = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp); - - sad += (data->lambda16 * t * sad) >> 10; - data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; - - if (sad < data->iMinSAD[0]) { - data->iMinSAD[0] = sad; - data->currentMV[0].x = x; data->currentMV[0].y = y; - data->dir = Direction; - } - - 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; } -} - int xvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference, const int x, const int y, - const uint32_t stride, const uint32_t iQuant, int rrv) + const uint32_t stride, const uint32_t iQuant) { int offset = (x + y*stride)*8; - if(!rrv) { - uint32_t sadC = sad8(current->u + offset, - reference->u + offset, stride); - if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; - sadC += sad8(current->v + offset, - reference->v + offset, stride); - if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; - return 1; - - } else { - uint32_t sadC = sad16(current->u + 2*offset, - reference->u + 2*offset, stride, 256*4096); - if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; - sadC += sad16(current->v + 2*offset, - reference->v + 2*offset, stride, 256*4096); - if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; - return 1; - } + uint32_t sadC = sad8(current->u + offset, + reference->u + offset, stride); + if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; + sadC += sad8(current->v + offset, + reference->v + offset, stride); + if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; + return 1; } /* @@ -379,7 +268,8 @@ const IMAGE * const pCurrent, const IMAGE * const pRef, const IMAGE * const vGMC, - const int coding_type) + const int coding_type, + const int skip_sad) { int mode = MODE_INTER; int mcsel = 0; @@ -405,9 +295,9 @@ } /* 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 || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { + if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP)) + if ( (100*skip_sad)/(pMB->sad16+1) < FINAL_SKIP_THRESH) + if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { mode = MODE_NOT_CODED; sad = 0; } @@ -439,29 +329,15 @@ 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); - + int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth); if (deviation < (sad - InterBias)) mode = MODE_INTRA; } pMB->cbp = 63; 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]; @@ -495,10 +371,8 @@ static __inline void PreparePredictionsP(VECTOR * const pmv, int x, int y, int iWcount, - int iHcount, const MACROBLOCK * const prevMB, int rrv) + int iHcount, const MACROBLOCK * const prevMB) { - /* this function depends on get_pmvdata which means that it sucks. It should get the predictions by itself */ - if (rrv) { iWcount /= 2; iHcount /= 2; } if ( (y != 0) && (x < (iWcount-1)) ) { /* [5] top-right neighbour */ pmv[5].x = EVEN(pmv[3].x); @@ -523,14 +397,6 @@ pmv[6].x = EVEN((prevMB+1+iWcount)->mvs[0].x); /* [6] right-down neighbour in last frame */ pmv[6].y = EVEN((prevMB+1+iWcount)->mvs[0].y); } else pmv[6].x = pmv[6].y = 0; - - if (rrv) { - int i; - for (i = 0; i < 7; i++) { - pmv[i].x = RRV_MV_SCALEUP(pmv[i].x); - pmv[i].y = RRV_MV_SCALEUP(pmv[i].y); - } - } } static void @@ -541,89 +407,91 @@ MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, const int block, - SearchData * const Data) + SearchData * const Data, + const int bound) { int i = 0; - CheckFunc * CheckCandidate; + VECTOR vbest_q; int32_t sbest_q; *Data->iMinSAD = *(OldData->iMinSAD + 1 + block); *Data->currentMV = *(OldData->currentMV + 1 + block); *Data->currentQMV = *(OldData->currentQMV + 1 + block); if(Data->qpel) { - Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); + Data->predMV = get_qpmv2(pMBs, pParam->mb_width, bound, x/2, y/2, block); if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, - Data->predMV, Data->iFcode, 0, 0); + Data->predMV, Data->iFcode, 0); } else { - Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); + Data->predMV = get_pmv2(pMBs, pParam->mb_width, bound, x/2, y/2, block); if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, - Data->predMV, Data->iFcode, 0, Data->rrv); + Data->predMV, Data->iFcode, 0); } - *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; + *(Data->iMinSAD) += (Data->lambda8 * i); if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { - if (Data->rrv) i = 16; else i = 8; + vbest_q = Data->currentQMV[0]; + sbest_q = Data->iMinSAD[0]; + - Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[0] = OldData->RefP[0] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[1] = OldData->RefP[1] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[2] = OldData->RefP[2] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[3] = OldData->RefP[3] + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->Cur = OldData->Cur + 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); Data->qpel_precision = 0; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, - pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); - - if (!Data->rrv) CheckCandidate = CheckCandidate8; - else CheckCandidate = CheckCandidate16no4v; + pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) { - int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */ MainSearchFunc *MainSearchPtr; if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = xvid_me_SquareSearch; else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = xvid_me_AdvDiamondSearch; else MainSearchPtr = xvid_me_DiamondSearch; - 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 */ - Data->currentQMV->y = 2 * Data->currentMV->y; - } + MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidate8); } - if (MotionFlags & XVID_ME_HALFPELREFINE8) { - int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */ + if(!Data->qpel) { + /* halfpel mode */ + if (MotionFlags & XVID_ME_HALFPELREFINE8) + /* perform halfpel refine of current best vector */ + xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); + } else { + /* qpel mode */ + Data->currentQMV->x = 2*Data->currentMV->x; + Data->currentQMV->y = 2*Data->currentMV->y; + + if(MotionFlags & XVID_ME_FASTREFINE8) { + /* fast */ + get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, + pParam->width, pParam->height, Data->iFcode, 2); + FullRefine_Fast(Data, CheckCandidate8, 0); + } else if(MotionFlags & XVID_ME_QUARTERPELREFINE8) { + /* full */ + if (MotionFlags & XVID_ME_HALFPELREFINE8) { + xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate8, 0); /* hpel part */ + Data->currentQMV->x = 2*Data->currentMV->x; + Data->currentQMV->y = 2*Data->currentMV->y; + } - xvid_me_SubpelRefine(Data, CheckCandidate); /* perform halfpel refine of current best vector */ + get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, + pParam->width, pParam->height, Data->iFcode, 2); + Data->qpel_precision = 1; - if(*(Data->iMinSAD) < temp_sad) { /* we have found a better match */ - Data->currentQMV->x = 2 * Data->currentMV->x; /* update our qpel vector */ - Data->currentQMV->y = 2 * Data->currentMV->y; + xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate8, 0); /* qpel part */ } } - if (Data->qpel && (MotionFlags & XVID_ME_QUARTERPELREFINE8)) { - 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); + if (sbest_q <= Data->iMinSAD[0]) /* we have not found a better match */ + Data->currentQMV[0] = vbest_q; - if((MotionFlags & XVID_ME_FASTREFINE8) && (!Data->rrv)) - SubpelRefine_Fast(Data, CheckCandidate8_qpel); - else - xvid_me_SubpelRefine(Data, CheckCandidate); - } } - if (Data->rrv) { - Data->currentMV->x = RRV_MV_SCALEDOWN(Data->currentMV->x); - Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y); - } - - if(Data->qpel) { + if(Data->qpel) { pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; pMB->qmvs[block] = *Data->currentQMV; @@ -656,7 +524,8 @@ const MBParam * const pParam, const MACROBLOCK * const pMBs, const MACROBLOCK * const prevMBs, - MACROBLOCK * const pMB) + MACROBLOCK * const pMB, + const int bound) { int i, threshA; @@ -665,22 +534,21 @@ 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); + pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); - get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, pmv, Data->temp); + get_pmvdata2(pMBs, pParam->mb_width, bound, x, y, pmv, Data->temp); Data->chromaX = Data->chromaY = 0; /* chroma-sad cache */ - i = Data->rrv ? 2 : 1; - Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i; - Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; - Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; - - Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; - Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; + Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; + Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; + Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; + + Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; + Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; Data->lambda16 = xvid_me_lambda_vec16[pMB->quant]; Data->lambda8 = xvid_me_lambda_vec8[pMB->quant]; @@ -689,12 +557,12 @@ memset(Data->currentMV, 0, 5*sizeof(VECTOR)); - 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, bound, x, y, 0); else Data->predMV = pmv[0]; - i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0); - Data->iMinSAD[0] = pMB->sad16 + ((Data->lambda16 * i * pMB->sad16)>>10); - Data->iMinSAD[1] = pMB->sad8[0] + ((Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS)) >> 10); + i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0); + Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i); + Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i); Data->iMinSAD[2] = pMB->sad8[1]; Data->iMinSAD[3] = pMB->sad8[2]; Data->iMinSAD[4] = pMB->sad8[3]; @@ -707,12 +575,10 @@ threshA = 512; PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, - prevMBs + x + y * pParam->mb_width, Data->rrv); + prevMBs + x + y * pParam->mb_width); - if (!Data->rrv) { - if (inter4v) CheckCandidate = CheckCandidate16; - else CheckCandidate = CheckCandidate16no4v; /* for extra speed */ - } else CheckCandidate = CheckCandidate32; + if (inter4v) CheckCandidate = CheckCandidate16; + else CheckCandidate = CheckCandidate16no4v; /* for extra speed */ /* main loop. checking all predictions (but first, which is 0,0 and has been checked in MotionEstimation())*/ @@ -747,9 +613,6 @@ if (Data->qpel) { startMV.x /= 2; startMV.y /= 2; - } else if (Data->rrv) { - startMV.x = RRV_MV_SCALEUP(startMV.x); - startMV.y = RRV_MV_SCALEUP(startMV.y); } if (!(MVequal(startMV, backupMV))) { bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; @@ -758,7 +621,8 @@ xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); if (bSAD < Data->iMinSAD[0]) { Data->currentMV[0] = backupMV; - Data->iMinSAD[0] = bSAD; } + Data->iMinSAD[0] = bSAD; + } } backupMV = Data->currentMV[0]; @@ -776,40 +640,56 @@ } } - if (MotionFlags & XVID_ME_HALFPELREFINE16) - xvid_me_SubpelRefine(Data, CheckCandidate); - - for(i = 0; i < 5; i++) { - Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */ - Data->currentQMV[i].y = 2 * Data->currentMV[i].y; - } - if (Data->qpel) { - 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); - Data->qpel_precision = 1; - if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { - if(MotionFlags & XVID_ME_FASTREFINE16) - SubpelRefine_Fast(Data, CheckCandidate16_qpel); - else - xvid_me_SubpelRefine(Data, CheckCandidate16_qpel); + if(!Data->qpel) { + /* halfpel mode */ + if (MotionFlags & XVID_ME_HALFPELREFINE16) + xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); + } else { + /* qpel mode */ + + for(i = 0; i < 5; i++) { + Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* initialize qpel vectors */ + Data->currentQMV[i].y = 2 * Data->currentMV[i].y; + } + if(MotionFlags & XVID_ME_FASTREFINE16 && MotionFlags & XVID_ME_QUARTERPELREFINE16) { + /* fast */ + get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, + pParam->width, pParam->height, Data->iFcode, 2); + FullRefine_Fast(Data, CheckCandidate, 0); + } else { + if(MotionFlags & (XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE16_RD)) { + /* full */ + if (MotionFlags & XVID_ME_HALFPELREFINE16) { + xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); /* hpel part */ + for(i = 0; i < 5; i++) { + Data->currentQMV[i].x = 2 * Data->currentMV[i].x; + Data->currentQMV[i].y = 2 * Data->currentMV[i].y; + } + } + get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, + pParam->width, pParam->height, Data->iFcode, 2); + Data->qpel_precision = 1; + if(MotionFlags & XVID_ME_QUARTERPELREFINE16) + xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate, 0); /* qpel part */ + } } } - if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30*((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1)) + if (Data->iMinSAD[0] < (int32_t)pMB->quant * 30 * ((MotionFlags & XVID_ME_FASTREFINE16) ? 8 : 1)) inter4v = 0; if (inter4v) { SearchData Data8; memcpy(&Data8, Data, sizeof(SearchData)); /* quick copy of common data */ - Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); - Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); - Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); - Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); + Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8, bound); + Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8, bound); + Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8, bound); + Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8, bound); if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) { - /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */ + /* chroma is only used for comparison to INTER. if the comparison will be done in RD domain, it will not be used */ int sumx = 0, sumy = 0; if (Data->qpel) @@ -829,6 +709,107 @@ } else Data->iMinSAD[1] = 4096*256; } +static int +InitialSkipDecisionP(int sad00, + const MBParam * pParam, + const FRAMEINFO * current, + MACROBLOCK * pMB, + const MACROBLOCK * prevMB, + int x, int y, + const SearchData * Data, + const IMAGE * const pGMC, + const IMAGE * const pCurrent, + const IMAGE * const pRef, + const uint32_t MotionFlags, + const int bound) +{ + const unsigned int iEdgedWidth = pParam->edged_width; + + int skip_thresh = INITIAL_SKIP_THRESH * \ + (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); + int stat_thresh = 0; + + /* initial skip decision */ + if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */ + if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) + if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant)) { + ZeroMacroblockP(pMB, sad00); + pMB->mode = MODE_NOT_CODED; + return 1; + } + } + + if(MotionFlags & XVID_ME_DETECT_STATIC_MOTION) { + VECTOR *cmpMV; + VECTOR staticMV = { 0, 0 }; + const MACROBLOCK * pMBs = current->mbs; + + if (current->coding_type == S_VOP) + cmpMV = &pMB->amv; + else + cmpMV = &staticMV; + + if(x > 0 && y > 0 && x < (int) pParam->mb_width) { + if(MVequal((&pMBs[(x-1) + y * pParam->mb_width])->mvs[0], *cmpMV) && + MVequal((&pMBs[x + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) && + MVequal((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mvs[0], *cmpMV) && + MVequal(prevMB->mvs[0], *cmpMV)) { + 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))); + } + } + } + + /* favorize (0,0) or global vector for cartoons */ + if (current->vop_flags & XVID_VOP_CARTOON) { + if (current->coding_type == S_VOP) { + int32_t iSAD = sad16(pCurrent->y + (x + y * iEdgedWidth) * 16, + pGMC->y + 16*y*iEdgedWidth + 16*x, iEdgedWidth, 65536); + + if (Data->chroma) { + iSAD += sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, pGMC->u + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2); + iSAD += sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, pGMC->v + 8*y*(iEdgedWidth/2) + 8*x, iEdgedWidth/2); + } + + if (iSAD <= stat_thresh) { /* mode decision GMC */ + pMB->mode = MODE_INTER; + pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = iSAD; + 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; + + return 1; + } + } + else if (sad00 < stat_thresh) { + VECTOR predMV; + if (Data->qpel) + predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0); + else + predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0); + + ZeroMacroblockP(pMB, sad00); + pMB->cbp = 0x3f; + pMB->pmvs[0].x = - predMV.x; + pMB->pmvs[0].y = - predMV.y; + return 1; + } + } + + return 0; +} + static __inline uint32_t MakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags) { @@ -863,38 +844,76 @@ if (!(VopFlags & XVID_VOP_HALFPEL)) Flags &= ~(XVID_ME_EXTSEARCH16+XVID_ME_HALFPELREFINE16+XVID_ME_HALFPELREFINE8+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD); - if ((VopFlags & XVID_VOP_GREYSCALE) || (VopFlags & XVID_VOP_REDUCED)) + if (VopFlags & XVID_VOP_GREYSCALE) Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); + if (Flags & XVID_ME_FASTREFINE8) + Flags &= ~XVID_ME_HALFPELREFINE8_RD; + + if (Flags & XVID_ME_FASTREFINE16) + Flags &= ~XVID_ME_HALFPELREFINE16_RD; + return Flags; } -bool +static __inline void +motionStatsPVOP(int * const MVmax, int * const mvCount, int * const mvSum, + const MACROBLOCK * const pMB, const int qpel) +{ + const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs; + int i; + int max = *MVmax; + + switch (pMB->mode) { + case MODE_INTER4V: + *mvCount += 3; + for(i = 3; i; i--) { + if (mv[i].x > max) max = mv[i].x; + else if (-mv[i].x - 1 > max) max = -mv[i].x - 1; + *mvSum += mv[i].x * mv[i].x; + if (mv[i].y > max) max = mv[i].y; + else if (-mv[i].y - 1 > max) max = -mv[i].y - 1; + *mvSum += mv[i].y * mv[i].y; + } + case MODE_INTER: + (*mvCount)++; + *mvSum += mv[0].x * mv[0].x; + *mvSum += mv[0].y * mv[0].y; + if (mv[0].x > max) max = mv[0].x; + else if (-mv[0].x - 1 > max) max = -mv[0].x - 1; + if (mv[0].y > max) max = mv[0].y; + else if (-mv[0].y - 1 > max) max = -mv[0].y - 1; + *MVmax = max; + default: + break; + } +} + +void MotionEstimation(MBParam * const pParam, FRAMEINFO * const current, FRAMEINFO * const reference, const IMAGE * const pRefH, const IMAGE * const pRefV, const IMAGE * const pRefHV, - const IMAGE * const pGMC, - const uint32_t iLimit) + const IMAGE * const pGMC, + const uint32_t iLimit, + const int num_slices) { MACROBLOCK *const pMBs = current->mbs; const IMAGE *const pCurrent = ¤t->image; const IMAGE *const pRef = &reference->image; - uint32_t mb_width = pParam->mb_width; - uint32_t mb_height = pParam->mb_height; + const uint32_t mb_width = pParam->mb_width; + const 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; + int bound = 0; + int MVmax = 0, mvSum = 0, mvCount = 0; uint32_t x, y; - uint32_t iIntra = 0; - int32_t sad00; - int skip_thresh = INITIAL_SKIP_THRESH * \ - (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \ - (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); + int sad00; + int block = 0; /* some pre-initialized thingies for SearchP */ DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); @@ -905,35 +924,29 @@ Data.rounding = pParam->m_rounding_type; Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP; - Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0; Data.dctSpace = dct_space; Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices; - Data.iMinSAD2 = 0; - - if ((current->vop_flags & XVID_VOP_REDUCED)) { - mb_width = (pParam->width + 31) / 32; - mb_height = (pParam->height + 31) / 32; - Data.qpel = 0; - } Data.RefQ = pRefV->u; /* a good place, also used in MC (for similar purpose) */ if (sadInit) (*sadInit) (); for (y = 0; y < mb_height; y++) { + + bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices); + for (x = 0; x < mb_width; x++) { - MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; - MACROBLOCK *prevMB = &reference->mbs[x + y * pParam->mb_width]; + MACROBLOCK *pMB = &pMBs[block]; + MACROBLOCK *prevMB = &reference->mbs[block]; + int skip; + block++; - if (!Data.rrv) pMB->sad16 = + pMB->sad16 = sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, pRef->y + (x + y * iEdgedWidth) * 16, - pParam->edged_width, pMB->sad8 ); + pParam->edged_width, pMB->sad8); - else pMB->sad16 = - sad32v_c(pCurrent->y + (x + y * iEdgedWidth) * 32, - pRef->y + (x + y * iEdgedWidth) * 32, - pParam->edged_width, pMB->sad8 ); + sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3])); if (Data.chroma) { Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, @@ -941,71 +954,198 @@ + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); pMB->sad16 += Data.chromaSAD; + sad00 += Data.chromaSAD; } - sad00 = pMB->sad16; + skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC, + pCurrent, pRef, MotionFlags, bound); + if (skip) continue; + + SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, + y, MotionFlags, current->vop_flags, + &Data, pParam, pMBs, reference->mbs, pMB, bound); + + if (current->vop_flags & XVID_VOP_MODEDECISION_RD) + xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam, + MotionFlags, current->vop_flags, current->vol_flags, + pCurrent, pRef, pGMC, current->coding_type, bound); + + else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD) + xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam, + MotionFlags, current->vop_flags, current->vol_flags, + pCurrent, pRef, pGMC, current->coding_type, bound); + else + ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam, + MotionFlags, current->vop_flags, current->vol_flags, + pCurrent, pRef, pGMC, current->coding_type, sad00); - /* initial skip decision */ - /* no early skip for GMC (global vector = skip vector is unknown!) */ - if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */ - if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) - if (Data.chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { - ZeroMacroblockP(pMB, sad00); - pMB->mode = MODE_NOT_CODED; - continue; - } - } - 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))); + motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel); + } + } + + current->fcode = getMinFcode(MVmax); + current->sStat.iMvSum = mvSum; + current->sStat.iMvCount = mvCount; +} + +void +MotionEstimateSMP(SMPData * h) +{ + Encoder *pEnc = (Encoder *) h->pEnc; + + const MBParam * const pParam = &pEnc->mbParam; + const FRAMEINFO * const current = pEnc->current; + const FRAMEINFO * const reference = pEnc->reference; + const IMAGE * const pRefH = &pEnc->vInterH; + const IMAGE * const pRefV = &pEnc->vInterV; + const IMAGE * const pRefHV = &pEnc->vInterHV; + const IMAGE * const pGMC = &pEnc->vGMC; + uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, + current->vop_flags, + current->vol_flags); + + MACROBLOCK *const pMBs = current->mbs; + const IMAGE *const pCurrent = ¤t->image; + const IMAGE *const pRef = &reference->image; + + const int mb_width = pParam->mb_width; + const int mb_height = pParam->mb_height; + const uint32_t iEdgedWidth = pParam->edged_width; + int bound = 0; + int num_slices = pEnc->num_slices; + int y_step = h->y_step; + int y_row = h->y_row; + int start_y = h->start_y; + int stop_y = h->stop_y; + int MVmax = 0, mvSum = 0, mvCount = 0; + + int x, y; + int sad00; + int block = (start_y+y_row)*mb_width; + int * complete_count_self = h->complete_count_self; + const volatile int * complete_count_above = h->complete_count_above; + int max_mbs; + int current_mb = 0; + + /* some pre-initialized thingies for SearchP */ + DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); + SearchData Data; + memset(&Data, 0, sizeof(SearchData)); + Data.iEdgedWidth = iEdgedWidth; + Data.iFcode = current->fcode; + Data.rounding = pParam->m_rounding_type; + Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); + Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP; + Data.dctSpace = dct_space; + Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); + Data.mpeg_quant_matrices = pParam->mpeg_quant_matrices; + + /* todo: sort out temp memory space */ + Data.RefQ = h->RefQ; + if (sadInit) (*sadInit) (); + + max_mbs = 0; + + for (y = (start_y + y_row); y < stop_y; y += y_step) { + bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices); + + if (y == start_y) max_mbs = mb_width; /* we can process all blocks of the first row */ + + for (x = 0; x < mb_width; x++) { + + MACROBLOCK *pMB, *prevMB; + int skip; + + if (current_mb >= max_mbs) { + /* we ME-ed all macroblocks we safely could. grab next portion */ + int above_count = *complete_count_above; /* sync point */ + if (above_count == mb_width) { + /* full line above is ready */ + above_count = mb_width+1; + if (y < (stop_y-y_step)) { + /* this is not last line, grab a portion of MBs from the next line too */ + above_count += MAX(0, complete_count_above[1] - 1); } } + + max_mbs = current_mb + above_count - x - 1; + + if (current_mb >= max_mbs) { + /* current workload is zero */ + x--; + sched_yield(); + continue; + } } - /* favorize (0,0) vector for cartoons */ - if ((current->vop_flags & XVID_VOP_CARTOON) && - ((sad00 < pMB->quant * 4 * skip_thresh) || (sad00 < stat_thresh))) { - ZeroMacroblockP(pMB, sad00); + pMB = &pMBs[block]; + prevMB = &reference->mbs[block]; + + pMB->sad16 = + sad16v(pCurrent->y + (x + y * iEdgedWidth) * 16, + pRef->y + (x + y * iEdgedWidth) * 16, + pParam->edged_width, pMB->sad8); + + sad00 = 4*MAX(MAX(pMB->sad8[0], pMB->sad8[1]), MAX(pMB->sad8[2], pMB->sad8[3])); + + if (Data.chroma) { + Data.chromaSAD = sad8(pCurrent->u + x*8 + y*(iEdgedWidth/2)*8, + pRef->u + x*8 + y*(iEdgedWidth/2)*8, iEdgedWidth/2) + + sad8(pCurrent->v + (x + y*(iEdgedWidth/2))*8, + pRef->v + (x + y*(iEdgedWidth/2))*8, iEdgedWidth/2); + pMB->sad16 += Data.chromaSAD; + sad00 += Data.chromaSAD; + } + + skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC, + pCurrent, pRef, MotionFlags, bound); + + if (skip) { + current_mb++; + block++; + *complete_count_self = x+1; continue; } SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, y, MotionFlags, current->vop_flags, - &Data, pParam, pMBs, reference->mbs, pMB); + &Data, pParam, pMBs, reference->mbs, pMB, bound); if (current->vop_flags & XVID_VOP_MODEDECISION_RD) xvid_me_ModeDecision_RD(&Data, pMB, pMBs, x, y, pParam, MotionFlags, current->vop_flags, current->vol_flags, - pCurrent, pRef, pGMC, current->coding_type); + pCurrent, pRef, pGMC, current->coding_type, bound); else if (current->vop_flags & XVID_VOP_FAST_MODEDECISION_RD) xvid_me_ModeDecision_Fast(&Data, pMB, pMBs, x, y, pParam, MotionFlags, current->vop_flags, current->vol_flags, - pCurrent, pRef, pGMC, current->coding_type); + pCurrent, pRef, pGMC, current->coding_type, bound); else ModeDecision_SAD(&Data, pMB, pMBs, x, y, pParam, MotionFlags, current->vop_flags, current->vol_flags, - pCurrent, pRef, pGMC, current->coding_type); + pCurrent, pRef, pGMC, current->coding_type, sad00); + + *complete_count_self = x+1; + current_mb++; + block++; - if (pMB->mode == MODE_INTRA) - if (++iIntra > iLimit) return 1; + motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel); + } + + block += (y_step-1)*pParam->mb_width; + + complete_count_self++; + complete_count_above++; } - return 0; + + h->minfcode = getMinFcode(MVmax); + + h->MVmax = MVmax; + h->mvSum = mvSum; + h->mvCount = mvCount; }