--- trunk/xvidcore/src/motion/motion_est.c 2003/02/19 10:13:54 871 +++ branches/dev-api-4/xvidcore/src/motion/motion_est.c 2003/03/16 12:05:09 926 @@ -46,6 +46,26 @@ #include "../utils/emms.h" #include "../dct/fdct.h" +/***************************************************************************** + * Modified rounding tables -- declared in motion.h + * Original tables see ISO spec tables 7-6 -> 7-9 + ****************************************************************************/ + +const uint32_t roundtab[16] = +{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; + +/* K = 4 */ +const uint32_t roundtab_76[16] = +{ 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 }; + +/* K = 2 */ +const uint32_t roundtab_78[8] = +{ 0, 0, 1, 1, 0, 0, 0, 1 }; + +/* K = 1 */ +const uint32_t roundtab_79[4] = +{ 0, 1, 0, 0 }; + #define INITIAL_SKIP_THRESH (10) #define FINAL_SKIP_THRESH (50) #define MAX_SAD00_FOR_SKIP (20) @@ -54,6 +74,10 @@ #define CHECK_CANDIDATE(X,Y,D) { \ CheckCandidate((X),(Y), (D), &iDirection, data ); } +/***************************************************************************** + * Code + ****************************************************************************/ + static __inline uint32_t d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) { @@ -971,11 +995,11 @@ 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->global_flags); + const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags); uint32_t x, y; uint32_t iIntra = 0; - int32_t quant = current->quant, sad00; + int32_t sad00; // some pre-initialized thingies for SearchP int32_t temp[8]; @@ -991,11 +1015,11 @@ Data.temp = temp; Data.iFcode = current->fcode; Data.rounding = pParam->m_rounding_type; - Data.qpel = pParam->m_quarterpel; + Data.qpel = current->vol_flags & XVID_QUARTERPEL; Data.chroma = MotionFlags & PMV_CHROMA16; - Data.rrv = current->global_flags & XVID_REDUCED; + Data.rrv = current->vop_flags & XVID_REDUCED; - if ((current->global_flags & XVID_REDUCED)) { + if ((current->vop_flags & XVID_REDUCED)) { mb_width = (pParam->width + 31) / 32; mb_height = (pParam->height + 31) / 32; Data.qpel = 0; @@ -1028,21 +1052,10 @@ sad00 = pMB->sad16; - if (!(current->global_flags & XVID_LUMIMASKING)) { - pMB->dquant = NO_CHANGE; - } else { - if (pMB->dquant != NO_CHANGE) { - quant += DQtab[pMB->dquant]; - if (quant > 31) quant = 31; - else if (quant < 1) quant = 1; - } - } - pMB->quant = current->quant; - //initial skip decision /* no early skip for GMC (global vector = skip vector is unknown!) */ - if (!(current->global_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ - if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) + if (!(current->vol_flags & XVID_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ + if (pMB->dquant == 0 && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) ) if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { SkipMacroblockP(pMB, sad00); continue; @@ -1050,14 +1063,14 @@ } SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, - y, MotionFlags, current->global_flags, pMB->quant, + y, MotionFlags, current->vol_flags, pMB->quant, &Data, pParam, pMBs, reference->mbs, - current->global_flags & XVID_INTER4V, pMB); + current->vop_flags & XVID_INTER4V, pMB); /* final skip decision, a.k.a. "the vector you found, really that good?" */ - if (!(current->global_flags & XVID_GMC)) { - if ( pMB->dquant == NO_CHANGE && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { - if (!(current->global_flags & XVID_MODEDECISION_BITS)) { + if (!(current->vol_flags & XVID_GMC)) { + if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { + if (!(current->vop_flags & XVID_MODEDECISION_BITS)) { if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) ) if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) SkipMacroblockP(pMB, sad00); @@ -1073,7 +1086,7 @@ } } - if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ + if (current->vol_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ { current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); } @@ -1148,12 +1161,12 @@ const int x, const int y, const MBParam * const pParam, const uint32_t MotionFlags, - const uint32_t GlobalFlags) + const uint32_t VopFlags) { int mode = MODE_INTER; - if (!(GlobalFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision + if (!(VopFlags & XVID_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision // int intra = 0; int sad; int InterBias = MV16_INTER_BIAS; @@ -1196,7 +1209,7 @@ int bits, intra, i; VECTOR backup[5], *v; Data->lambda16 = iQuant; - Data->lambda8 = pParam->m_quant_type; + Data->lambda8 = (pParam->vol_flags & XVID_MPEGQUANT)?1:0; v = Data->qpel ? Data->currentQMV : Data->currentMV; for (i = 0; i < 5; i++) { @@ -1230,7 +1243,7 @@ const int x, const int y, const uint32_t MotionFlags, - const uint32_t GlobalFlags, + const uint32_t VopFlags, const uint32_t iQuant, SearchData * const Data, const MBParam * const pParam, @@ -1265,7 +1278,7 @@ Data->lambda8 = lambda_vec8[iQuant]; Data->qpel_precision = 0; - if (pMB->dquant != NO_CHANGE) inter4v = 0; + if (pMB->dquant != 0) inter4v = 0; for(i = 0; i < 5; i++) Data->currentMV[i].x = Data->currentMV[i].y = 0; @@ -1280,7 +1293,7 @@ Data->iMinSAD[3] = pMB->sad8[2]; Data->iMinSAD[4] = pMB->sad8[3]; - if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) { + if ((!(VopFlags & XVID_MODEDECISION_BITS)) || (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; @@ -1306,7 +1319,7 @@ 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))) { - if (!(GlobalFlags & XVID_MODEDECISION_BITS)) inter4v = 0; } + if (!(VopFlags & XVID_MODEDECISION_BITS)) inter4v = 0; } else { MainSearchFunc * MainSearchPtr; @@ -1369,9 +1382,9 @@ SubpelRefine(Data); } - if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; + if ((!(VopFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; - if (inter4v && (!(GlobalFlags & XVID_MODEDECISION_BITS) || + if (inter4v && (!(VopFlags & XVID_MODEDECISION_BITS) || (!(MotionFlags & QUARTERPELREFINE8_BITS)) || (!(MotionFlags & HALFPELREFINE8_BITS)) || ((!(MotionFlags & EXTSEARCH_BITS)) && (!(MotionFlags&PMV_EXTSEARCH8)) ))) { // if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop @@ -1384,7 +1397,7 @@ 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) && (!(GlobalFlags & XVID_MODEDECISION_BITS))) { + if ((Data->chroma) && (!(VopFlags & XVID_MODEDECISION_BITS))) { // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it int sumx = 0, sumy = 0; const int div = 1 + Data->qpel; @@ -1400,7 +1413,7 @@ } } - inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, GlobalFlags); + inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, VopFlags); if (Data->rrv) { Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); @@ -2014,7 +2027,7 @@ Data.currentMV = currentMV; Data.currentQMV = currentQMV; Data.iMinSAD = &iMinSAD; Data.lambda16 = lambda_vec16[frame->quant]; - Data.qpel = pParam->m_quarterpel; + Data.qpel = pParam->vol_flags & XVID_QUARTERPEL; Data.rounding = 0; Data.chroma = frame->motion_flags & PMV_CHROMA8; Data.temp = temp; @@ -2039,7 +2052,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 */ @@ -2139,7 +2151,7 @@ else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, - pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv); + pParam->width, pParam->height, Data->iFcode - (pParam->vol_flags&XVID_QUARTERPEL?1:0), 0, Data->rrv); Data->Cur = pCur + (x + y * pParam->edged_width) * 16; Data->Ref = pRef + (x + y * pParam->edged_width) * 16; @@ -2196,7 +2208,7 @@ Data.currentMV = currentMV; Data.iMinSAD = iMinSAD; Data.iFcode = Current->fcode; - Data.rrv = Current->global_flags & XVID_REDUCED; + Data.rrv = Current->vop_flags & XVID_REDUCED; Data.temp = temp; CheckCandidate = CheckCandidate32I; @@ -2279,8 +2291,8 @@ // filter mask of all blocks - for (my = 1; my < MBh-1; my++) - for (mx = 1; mx < MBw-1; mx++) + for (my = 1; my < (uint32_t)MBh-1; my++) + for (mx = 1; mx < (uint32_t)MBw-1; mx++) { const int mbnum = mx + my * MBw; const MACROBLOCK *pMB = &pMBs[mbnum]; @@ -2296,8 +2308,8 @@ MBmask[mbnum]=1; } - for (my = 1; my < MBh-1; my++) - for (mx = 1; mx < MBw-1; mx++) + for (my = 1; my < (uint32_t)MBh-1; my++) + for (mx = 1; mx < (uint32_t)MBw-1; mx++) { const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx; @@ -2305,9 +2317,9 @@ if (!MBmask[mbnum]) continue; - if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= grad ) + if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= (uint32_t)grad ) MBmask[mbnum] = 0; - if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= grad ) + if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= (uint32_t)grad ) MBmask[mbnum] = 0; } @@ -2318,8 +2330,8 @@ a = b = c = n = 0; DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.; - for (my = 0; my < MBh; my++) - for (mx = 0; mx < MBw; mx++) + for (my = 0; my < (uint32_t)MBh; my++) + for (mx = 0; mx < (uint32_t)MBw; mx++) { const int mbnum = mx + my * MBw; const MACROBLOCK *pMB = &pMBs[mbnum]; @@ -2356,8 +2368,8 @@ meanx = meany = 0.; oldnum = 0; - for (my = 0; my < MBh; my++) - for (mx = 0; mx < MBw; mx++) + for (my = 0; my < (uint32_t)MBh; my++) + for (mx = 0; mx < (uint32_t)MBw; mx++) { const int mbnum = mx + my * MBw; const MACROBLOCK *pMB = &pMBs[mbnum]; @@ -2385,8 +2397,8 @@ fprintf(stderr,"meanx = %8.5f meany = %8.5f %d\n",meanx,meany, oldnum); */ num = 0; - for (my = 0; my < MBh; my++) - for (mx = 0; mx < MBw; mx++) + for (my = 0; my < (uint32_t)MBh; my++) + for (mx = 0; mx < (uint32_t)MBw; mx++) { const int mbnum = mx + my * MBw; const MACROBLOCK *pMB = &pMBs[mbnum];