--- estimation_pvop.c 2005/12/18 06:52:12 1.18 +++ 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.18 2005/12/18 06:52:12 syskin Exp $ + * $Id: estimation_pvop.c,v 1.25 2010/12/28 19:19:43 Isibaar Exp $ * ****************************************************************************/ @@ -39,6 +39,8 @@ #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.0 * NEIGH_TEND_8X8 + 0.5), @@ -294,7 +296,7 @@ /* final skip decision, a.k.a. "the vector you found, really that good?" */ if (skip_possible && (skip_sad < (int)iQuant * MAX_SAD00_FOR_SKIP)) - if ( (100*skip_sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) + 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; @@ -405,7 +407,8 @@ MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, const int block, - SearchData * const Data) + SearchData * const Data, + const int bound) { int i = 0; VECTOR vbest_q; int32_t sbest_q; @@ -414,11 +417,11 @@ *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); } 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); } @@ -521,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; @@ -532,7 +536,7 @@ 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); - 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 */ Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; @@ -553,7 +557,7 @@ 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); @@ -679,10 +683,10 @@ 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 comparison to INTER. if the comparison will be done in RD domain, it will not be used */ @@ -716,7 +720,8 @@ const IMAGE * const pGMC, const IMAGE * const pCurrent, const IMAGE * const pRef, - const uint32_t MotionFlags) + const uint32_t MotionFlags, + const int bound) { const unsigned int iEdgedWidth = pParam->edged_width; @@ -744,7 +749,7 @@ else cmpMV = &staticMV; - if(x > 0 && y > 0 && x < pParam->mb_width) { + 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) && @@ -790,9 +795,9 @@ else if (sad00 < stat_thresh) { VECTOR predMV; if (Data->qpel) - predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); + predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0); else - predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); + predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0); ZeroMacroblockP(pMB, sad00); pMB->cbp = 0x3f; @@ -892,7 +897,8 @@ const IMAGE * const pRefV, const IMAGE * const pRefHV, const IMAGE * const pGMC, - const uint32_t iLimit) + const uint32_t iLimit, + const int num_slices) { MACROBLOCK *const pMBs = current->mbs; const IMAGE *const pCurrent = ¤t->image; @@ -902,13 +908,11 @@ 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; int sad00; - int skip_thresh = INITIAL_SKIP_THRESH * \ - (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); int block = 0; /* some pre-initialized thingies for SearchP */ @@ -928,6 +932,9 @@ 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[block]; MACROBLOCK *prevMB = &reference->mbs[block]; @@ -951,22 +958,22 @@ } skip = InitialSkipDecisionP(sad00, pParam, current, pMB, prevMB, x, y, &Data, pGMC, - pCurrent, pRef, MotionFlags); + 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); + &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, @@ -981,3 +988,164 @@ 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; + } + } + + 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, 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); + + *complete_count_self = x+1; + + current_mb++; + block++; + + motionStatsPVOP(&MVmax, &mvCount, &mvSum, pMB, Data.qpel); + + } + + block += (y_step-1)*pParam->mb_width; + + complete_count_self++; + complete_count_above++; + } + + h->minfcode = getMinFcode(MVmax); + + h->MVmax = MVmax; + h->mvSum = mvSum; + h->mvCount = mvCount; +} + +