📄 estimation_pvop.c
字号:
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->lambda16 = xvid_me_lambda_vec16[pMB->quant]; Data->lambda8 = xvid_me_lambda_vec8[pMB->quant]; Data->qpel_precision = 0; Data->dir = 0; memset(Data->currentMV, 0, 5*sizeof(VECTOR)); if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, 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); Data->iMinSAD[2] = pMB->sad8[1]; Data->iMinSAD[3] = pMB->sad8[2]; Data->iMinSAD[4] = pMB->sad8[3]; if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) { threshA = Data->temp[0]; /* that's where we keep this SAD atm */ if (threshA < 512) threshA = 512; else if (threshA > 1024) threshA = 1024; } else threshA = 512; PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, prevMBs + x + y * pParam->mb_width, Data->rrv); if (!Data->rrv) { 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 (!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]) && (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) inter4v = 0; 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 = xvid_me_SquareSearch; else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; else MainSearchPtr = xvid_me_DiamondSearch; 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, which makes it more different than the diamond above */ if (MotionFlags & XVID_ME_EXTSEARCH16) { int32_t bSAD; VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0]; 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; CheckCandidate(startMV.x, startMV.y, Data, 255); xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); if (bSAD < Data->iMinSAD[0]) { Data->currentMV[0] = backupMV; Data->iMinSAD[0] = bSAD; } } backupMV = Data->currentMV[0]; startMV.x = startMV.y = 1; if (!(MVequal(startMV, backupMV))) { bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; CheckCandidate(startMV.x, startMV.y, Data, 255); xvid_me_DiamondSearch(startMV.x, startMV.y, Data, 255, CheckCandidate); if (bSAD < Data->iMinSAD[0]) { Data->currentMV[0] = backupMV; Data->iMinSAD[0] = bSAD; } } } } 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->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); 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 */ int sumx = 0, sumy = 0; if (Data->qpel) for (i = 1; i < 5; i++) { sumx += Data->currentQMV[i].x/2; sumy += Data->currentQMV[i].y/2; } else for (i = 1; i < 5; i++) { sumx += Data->currentMV[i].x; sumy += Data->currentMV[i].y; } Data->iMinSAD[1] += xvid_me_ChromaSAD((sumx >> 3) + roundtab_76[sumx & 0xf], (sumy >> 3) + roundtab_76[sumy & 0xf], Data); } } else Data->iMinSAD[1] = 4096*256;}static __inline uint32_tMakeGoodMotionFlags(const uint32_t MotionFlags, const uint32_t VopFlags, const uint32_t VolFlags){ uint32_t Flags = MotionFlags; if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) Flags &= ~(XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD+XVID_ME_HALFPELREFINE16_RD+XVID_ME_HALFPELREFINE8_RD+XVID_ME_EXTSEARCH_RD); if (Flags & XVID_ME_EXTSEARCH_RD) Flags |= XVID_ME_HALFPELREFINE16_RD; if (Flags & XVID_ME_EXTSEARCH_RD && MotionFlags & XVID_ME_EXTSEARCH8) Flags |= XVID_ME_HALFPELREFINE8_RD; if (Flags & XVID_ME_HALFPELREFINE16_RD) Flags |= XVID_ME_QUARTERPELREFINE16_RD; if (Flags & XVID_ME_HALFPELREFINE8_RD) { Flags |= XVID_ME_QUARTERPELREFINE8_RD; Flags &= ~XVID_ME_HALFPELREFINE8; } if (Flags & XVID_ME_QUARTERPELREFINE8_RD) Flags &= ~XVID_ME_QUARTERPELREFINE8; if (Flags & XVID_ME_QUARTERPELREFINE16_RD) Flags &= ~XVID_ME_QUARTERPELREFINE16; if (!(VolFlags & XVID_VOL_QUARTERPEL)) Flags &= ~(XVID_ME_QUARTERPELREFINE16+XVID_ME_QUARTERPELREFINE8+XVID_ME_QUARTERPELREFINE16_RD+XVID_ME_QUARTERPELREFINE8_RD); 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)) Flags &= ~(XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP); return Flags;}boolMotionEstimation(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){ 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 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; 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); /* 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.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++) { 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, pRef->y + (x + y * iEdgedWidth) * 16, 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 ); 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 = pMB->sad16; /* 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))); } } } /* 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); continue; } SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, y, MotionFlags, current->vop_flags, &Data, pParam, pMBs, reference->mbs, pMB); 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); 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); else ModeDecision_SAD(&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; } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -