--- trunk/xvidcore/src/motion/motion_est.c 2003/02/15 15:22:19 851 +++ branches/dev-api-4/xvidcore/src/motion/motion_est.c 2003/03/29 01:29:53 958 @@ -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) { @@ -331,7 +355,7 @@ data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], - (yc >> 1) + roundtab_79[yc & 0x3], data); + (yc >> 1) + roundtab_79[yc & 0x3], data); if (sad < data->iMinSAD[0]) { data->iMinSAD[0] = sad; @@ -482,8 +506,9 @@ const uint8_t *ReferenceF, *ReferenceB; VECTOR *current; - if ( (xf > data->max_dx) | (xf < data->min_dx) - | (yf > data->max_dy) | (yf < data->min_dy) ) return; + if ((xf > data->max_dx) || (xf < data->min_dx) || + (yf > data->max_dy) || (yf < data->min_dy)) + return; if (!data->qpel_precision) { ReferenceF = GetReference(xf, yf, data); @@ -528,7 +553,7 @@ const uint8_t *ReferenceB; VECTOR mvs, b_mvs; - if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return; + if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; for (k = 0; k < 4; k++) { mvs.x = data->directmvF[k].x + x; @@ -541,10 +566,11 @@ data->directmvB[k].y : mvs.y - data->referencemv[k].y); - if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx) - | (mvs.y > data->max_dy) | (mvs.y < data->min_dy) - | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx) - | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return; + if ((mvs.x > data->max_dx) || (mvs.x < data->min_dx) || + (mvs.y > data->max_dy) || (mvs.y < data->min_dy) || + (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) || + (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) + return; if (data->qpel) { xcf += mvs.x/2; ycf += mvs.y/2; @@ -697,7 +723,7 @@ } } - bits += cbpy_tab[15-(cbp>>2)].len; + bits += xvid_cbpy_tab[15-(cbp>>2)].len; bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; if (bits < data->iMinSAD[0]) { @@ -971,11 +997,15 @@ 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; + int skip_thresh = \ + INITIAL_SKIP_THRESH * \ + (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \ + (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1); // some pre-initialized thingies for SearchP int32_t temp[8]; @@ -991,11 +1021,11 @@ Data.temp = temp; Data.iFcode = current->fcode; Data.rounding = pParam->m_rounding_type; - Data.qpel = pParam->m_quarterpel; - Data.chroma = MotionFlags & PMV_CHROMA16; - Data.rrv = current->global_flags & XVID_REDUCED; + Data.qpel = current->vol_flags & XVID_VOL_QUARTERPEL; + Data.chroma = MotionFlags & XVID_ME_CHROMA16; + Data.rrv = current->vop_flags & XVID_VOP_REDUCED; - if ((current->global_flags & XVID_REDUCED)) { + if ((current->vop_flags & XVID_VOP_REDUCED)) { mb_width = (pParam->width + 31) / 32; mb_height = (pParam->height + 31) / 32; Data.qpel = 0; @@ -1028,21 +1058,18 @@ 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; - } + if (pMB->dquant != 0) { + 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_VOL_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ + if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { SkipMacroblockP(pMB, sad00); continue; @@ -1050,22 +1077,16 @@ } 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_VOP_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 ( (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); - } else { // BITS mode decision - if (pMB->sad16 > 10) - SkipMacroblockP(pMB, sad00); // more than 10 bits would be used for this MB - skip - - } + if (!(current->vol_flags & XVID_VOL_GMC || current->vop_flags & XVID_VOP_MODEDECISION_BITS)) { + if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) { + 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); } } if (pMB->mode == MODE_INTRA) @@ -1073,7 +1094,7 @@ } } - if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ + if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ { current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); } @@ -1148,13 +1169,13 @@ 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 - int intra = 0; + if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision +// int intra = 0; int sad; int InterBias = MV16_INTER_BIAS; if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + @@ -1196,7 +1217,7 @@ int bits, intra, i; VECTOR backup[5], *v; Data->lambda16 = iQuant; - Data->lambda8 = pParam->m_quant_type; + Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0; v = Data->qpel ? Data->currentQMV : Data->currentMV; for (i = 0; i < 5; i++) { @@ -1230,7 +1251,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 +1286,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 +1301,7 @@ Data->iMinSAD[3] = pMB->sad8[2]; Data->iMinSAD[4] = pMB->sad8[3]; - if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) || (x | y)) { + if ((!(VopFlags & XVID_VOP_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,12 +1327,12 @@ 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_VOP_MODEDECISION_BITS)) inter4v = 0; } else { MainSearchFunc * MainSearchPtr; - if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; - else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; + 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); @@ -1320,7 +1341,7 @@ note that this search is/might be done in halfpel positions, which makes it more different than the diamond above */ - if (MotionFlags & PMV_EXTSEARCH16) { + if (MotionFlags & XVID_ME_EXTSEARCH16) { int32_t bSAD; VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0]; if (Data->rrv) { @@ -1351,8 +1372,8 @@ } } - if (MotionFlags & PMV_HALFPELREFINE16) - if ((!(MotionFlags & HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant) + if (MotionFlags & XVID_ME_HALFPELREFINE16) + if ((!(MotionFlags & XVID_ME_HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant) SubpelRefine(Data); for(i = 0; i < 5; i++) { @@ -1360,20 +1381,22 @@ Data->currentQMV[i].y = 2 * Data->currentMV[i].y; } - if (MotionFlags & PMV_QUARTERPELREFINE16) - if ((!(MotionFlags & QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { - Data->qpel_precision = 1; - get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, - pParam->width, pParam->height, Data->iFcode, 1, 0); + if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { + + get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, + pParam->width, pParam->height, Data->iFcode, 1, 0); + if ((!(MotionFlags & XVID_ME_QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { + Data->qpel_precision = 1; SubpelRefine(Data); } + } - if ((!(GlobalFlags & XVID_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; + if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; - if (inter4v && (!(GlobalFlags & XVID_MODEDECISION_BITS) || - (!(MotionFlags & QUARTERPELREFINE8_BITS)) || (!(MotionFlags & HALFPELREFINE8_BITS)) || - ((!(MotionFlags & EXTSEARCH_BITS)) && (!(MotionFlags&PMV_EXTSEARCH8)) ))) { + if (inter4v && (!(VopFlags & XVID_VOP_MODEDECISION_BITS) || + (!(MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS)) || (!(MotionFlags & XVID_ME_HALFPELREFINE8_BITS)) || + ((!(MotionFlags & XVID_ME_EXTSEARCH_BITS)) && (!(MotionFlags&XVID_ME_EXTSEARCH8)) ))) { // if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop SearchData Data8; @@ -1384,7 +1407,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_VOP_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 +1423,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); @@ -1459,7 +1482,7 @@ *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; - if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) { + if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { if (Data->rrv) i = 2; else i = 1; Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); @@ -1476,12 +1499,12 @@ if (!Data->rrv) CheckCandidate = CheckCandidate8; else CheckCandidate = CheckCandidate16no4v; - if (MotionFlags & PMV_EXTSEARCH8 && (!(MotionFlags & EXTSEARCH_BITS))) { + if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) { int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD MainSearchFunc *MainSearchPtr; - if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch; - else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; + if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch; + else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; else MainSearchPtr = DiamondSearch; MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255); @@ -1492,7 +1515,7 @@ } } - if (MotionFlags & PMV_HALFPELREFINE8) { + if (MotionFlags & XVID_ME_HALFPELREFINE8) { int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD SubpelRefine(Data); // perform halfpel refine of current best vector @@ -1503,7 +1526,7 @@ } } - if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE8) { + 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, 8, pParam->width, pParam->height, Data->iFcode, 1, 0); @@ -1627,8 +1650,8 @@ CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); } - if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; - else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; + 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); @@ -1799,13 +1822,14 @@ } } + *Data->iMinSAD += Data->lambda16; skip_sad = *Data->iMinSAD; // DIRECT MODE DELTA VECTOR SEARCH. // This has to be made more effective, but at the moment I'm happy it's running at all - if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch; - else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; + if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch; + else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch; else MainSearchPtr = DiamondSearch; MainSearchPtr(0, 0, Data, 255); @@ -2014,9 +2038,9 @@ 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_VOL_QUARTERPEL; Data.rounding = 0; - Data.chroma = frame->motion_flags & PMV_CHROMA8; + Data.chroma = frame->motion_flags & XVID_ME_CHROMA8; Data.temp = temp; Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose) @@ -2124,8 +2148,9 @@ { int i, mask; + int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0; VECTOR pmv[3]; - MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; + MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; @@ -2139,7 +2164,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 - quarterpel, 0, 0); Data->Cur = pCur + (x + y * pParam->edged_width) * 16; Data->Ref = pRef + (x + y * pParam->edged_width) * 16; @@ -2151,43 +2176,48 @@ pmv[0].x = pmv[0].y = 0; CheckCandidate32I(0, 0, 255, &i, Data); + Data->iMinSAD[1] -= 50; + Data->iMinSAD[2] -= 50; + Data->iMinSAD[3] -= 50; + Data->iMinSAD[4] -= 50; - if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { + 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); - if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed + if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); + } - 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]; - } + 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]; } } -#define INTRA_BIAS 2500 -#define INTRA_THRESH 1500 -#define INTER_THRESH 1400 +#define INTRA_THRESH 2400 +#define INTER_THRESH 1100 int MEanalysis( const IMAGE * const pRef, - FRAMEINFO * const Current, - MBParam * const pParam, - int maxIntra, //maximum number if non-I frames - int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame - int bCount) // number of B frames in a row + const FRAMEINFO * const Current, + const MBParam * const pParam, + const int maxIntra, //maximum number if non-I frames + const int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame + const int bCount, // number of B frames in a row + const int b_thresh) { uint32_t x, y, intra = 0; int sSAD = 0; MACROBLOCK * const pMBs = Current->mbs; const IMAGE * const pCurrent = &Current->image; - int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; + int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh; + int s = 0, blocks = 0; int32_t iMinSAD[5], temp[5]; VECTOR currentMV[5]; @@ -2196,26 +2226,32 @@ Data.currentMV = currentMV; Data.iMinSAD = iMinSAD; Data.iFcode = Current->fcode; - Data.rrv = Current->global_flags & XVID_REDUCED; Data.temp = temp; CheckCandidate = CheckCandidate32I; if (intraCount != 0 && intraCount < 10) // we're right after an I frame - IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); + IntraThresh += 8 * (intraCount - 10) * (intraCount - 10); else if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; - InterThresh += 400 * (1 - bCount); - if (InterThresh < 300) InterThresh = 300; + InterThresh -= (350 - 8*b_thresh) * bCount; + if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; if (sadInit) (*sadInit) (); for (y = 1; y < pParam->mb_height-1; y += 2) { for (x = 1; x < pParam->mb_width-1; x += 2) { int i; + blocks += 4; if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; + else { //extrapolation of the vector found for last frame + pMBs[x + y * pParam->mb_width].mvs[0].x = + (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount; + pMBs[x + y * pParam->mb_width].mvs[0].y = + (pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount; + } MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); @@ -2227,19 +2263,24 @@ pParam->edged_width); if (dev + IntraThresh < pMB->sad16) { pMB->mode = MODE_INTRA; - if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP; + if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP; } } + if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++; + sSAD += pMB->sad16; } } } - sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); -// if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; + + sSAD /= blocks; + s = (10*s) / blocks; + + if (s > 5) sSAD += (s - 4) * (180 - 2*b_thresh); //static block - looks bad when in bframe... + if (sSAD > InterThresh ) return P_VOP; emms(); return B_VOP; - } @@ -2271,12 +2312,16 @@ double meanx,meany; int num,oldnum; - if (!MBmask) { fprintf(stderr,"Mem error\n"); return gmc;} + if (!MBmask) { fprintf(stderr,"Mem error\n"); + gmc.duv[0].x= gmc.duv[0].y = + gmc.duv[1].x= gmc.duv[1].y = + gmc.duv[2].x= gmc.duv[2].y = 0; + return gmc; } // 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]; @@ -2292,8 +2337,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; @@ -2301,9 +2346,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; } @@ -2314,8 +2359,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]; @@ -2352,8 +2397,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]; @@ -2381,8 +2426,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]; @@ -2446,7 +2491,7 @@ if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) return 0; //quick stop - if (MotionFlags & (HALFPELREFINE16_BITS | EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search + if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); @@ -2464,12 +2509,12 @@ } } - if (MotionFlags&EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); + if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); - if (MotionFlags&HALFPELREFINE16_BITS) SubpelRefine(Data); + if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data); if (Data->qpel) { - if (MotionFlags&(EXTSEARCH_BITS | HALFPELREFINE16_BITS)) { // there was halfpel-precision search + if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { // there was halfpel-precision search for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match Data->currentQMV[i].y = 2 * Data->currentMV[i].y; @@ -2480,10 +2525,10 @@ get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, pParam->width, pParam->height, Data->iFcode, 1, 0); } - if (MotionFlags&QUARTERPELREFINE16_BITS) SubpelRefine(Data); + if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data); } - if (MotionFlags&CHECKPREDICTION_BITS) { //let's check vector equal to prediction + if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { //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)) CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); @@ -2544,7 +2589,7 @@ } if (Data8->qpel) { - if (MotionFlags&HALFPELREFINE8_BITS || (MotionFlags&PMV_EXTSEARCH8 && MotionFlags&EXTSEARCH_BITS)) { // halfpixel motion search follows + if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { // halfpixel motion search follows int32_t s = *Data8->iMinSAD; Data8->currentMV->x = Data8->currentQMV->x/2; Data8->currentMV->y = Data8->currentQMV->y/2; @@ -2555,10 +2600,10 @@ if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); - if (MotionFlags & PMV_EXTSEARCH8 && MotionFlags & EXTSEARCH_BITS) + if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); - if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); + if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); if(s > *Data8->iMinSAD) { //we have found a better match Data8->currentQMV->x = 2*Data8->currentMV->x; @@ -2570,13 +2615,13 @@ pParam->width, pParam->height, Data8->iFcode, 1, 0); } - if (MotionFlags & QUARTERPELREFINE8_BITS) SubpelRefine(Data8); + if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8); } else // not qpel - if (MotionFlags & HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement + if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement //checking vector equal to predicion - if (i != 0 && MotionFlags & CHECKPREDICTION_BITS) { + if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) { const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); @@ -2630,7 +2675,7 @@ bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); cbp |= 1 << (5 - 5); } - bits += cbpy_tab[15-(cbp>>2)].len; + bits += xvid_cbpy_tab[15-(cbp>>2)].len; bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; } } @@ -2680,9 +2725,9 @@ bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; if (t != 0) cbp |= 1 << (5 - 4); - Data->temp[4] = t; if (bits < Data->iMinSAD[0]) { + iDcScaler = get_dc_scaler(iQuant, 1); //chroma V transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); fdct(in); @@ -2693,16 +2738,9 @@ bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; if (t != 0) cbp |= 1 << (5 - 5); - Data->temp[5] = t; - - bits += t = cbpy_tab[cbp>>2].len; - Data->temp[6] = t; - - bits += t = mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; - Data->temp[7] = t; - + bits += xvid_cbpy_tab[cbp>>2].len; + bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; } } - return bits; }