--- trunk/xvidcore/src/motion/estimation_bvop.c 2004/12/05 04:53:01 1564 +++ trunk/xvidcore/src/motion/estimation_bvop.c 2004/12/17 11:57:59 1576 @@ -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_bvop.c,v 1.17 2004-12-05 04:53:01 syskin Exp $ + * $Id: estimation_bvop.c,v 1.22 2004-12-17 11:57:59 syskin Exp $ * ****************************************************************************/ @@ -829,6 +829,7 @@ pMB->sad16 = best_sad; pMB->mode = mode; + pMB->cbp = 63; switch (mode) { @@ -918,6 +919,32 @@ } } +static __inline void +maxMotionBVOP(int * const MVmaxF, int * const MVmaxB, const MACROBLOCK * const pMB, const int qpel) +{ + if (pMB->mode == MODE_FORWARD || pMB->mode == MODE_INTERPOLATE) { + const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs; + int max = *MVmaxF; + 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; + + *MVmaxF = max; + } + + if (pMB->mode == MODE_BACKWARD || pMB->mode == MODE_INTERPOLATE) { + const VECTOR * const mv = qpel ? pMB->b_qmvs : pMB->b_mvs; + int max = *MVmaxB; + 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; + *MVmaxB = max; + } +} + + void MotionEstimationBVOP(MBParam * const pParam, FRAMEINFO * const frame, @@ -938,13 +965,13 @@ { uint32_t i, j; int32_t best_sad = 256*4096; - int32_t sad2; uint32_t skip_sad; - + int fb_thresh; const MACROBLOCK * const b_mbs = b_reference->mbs; VECTOR f_predMV, b_predMV; + int MVmaxF = 0, MVmaxB = 0; const int32_t TRB = time_pp - time_bp; const int32_t TRD = time_pp; DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); @@ -958,6 +985,7 @@ Data_d.rounding = 0; Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; Data_d.iQuant = frame->quant; + Data_d.quant_sq = frame->quant*frame->quant; Data_d.dctSpace = dct_space; Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices; @@ -968,9 +996,8 @@ memcpy(&Data_b, &Data_d, sizeof(SearchData)); memcpy(&Data_i, &Data_d, sizeof(SearchData)); - Data_f.iFcode = Data_i.iFcode = frame->fcode; - Data_b.iFcode = Data_i.bFcode = frame->bcode; - + Data_f.iFcode = Data_i.iFcode = frame->fcode = b_reference->fcode; + Data_b.iFcode = Data_i.bFcode = frame->bcode = b_reference->fcode; for (j = 0; j < pParam->mb_height; j++) { @@ -1002,6 +1029,7 @@ if (pMB->mode == MODE_DIRECT_NONE_MV) { pMB->sad16 = best_sad; + pMB->cbp = 0; continue; } @@ -1011,24 +1039,30 @@ SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, &b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); - sad2 = best_sad; + if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) + fb_thresh = best_sad; + else + fb_thresh = best_sad + (best_sad>>1); - if (Data_f.iMinSAD[0] < 2*sad2+2000) + if (Data_f.iMinSAD[0] <= fb_thresh) SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f); - if (Data_b.iMinSAD[0] < 2*sad2+2000) + if (Data_b.iMinSAD[0] <= fb_thresh) SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b); SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad, &Data_i, Data_f.currentMV[0], Data_b.currentMV[0]); - if (((Data_i.iMinSAD[0] < 2*best_sad+2000) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) + if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) || Data_i.iMinSAD[0] <= best_sad) SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i); - if ((Data_d.iMinSAD[0] <= 2*best_sad) && (!frame->motion_flags&XVID_ME_SKIP_DELTASEARCH)) - SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); + if (Data_d.iMinSAD[0] <= 2*best_sad) + if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750)) + || (best_sad > 1000)) + + SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); /* final skip decision */ if ( (skip_sad < 2 * Data_d.iQuant * MAX_SAD00_FOR_SKIP ) @@ -1040,16 +1074,22 @@ if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */ pMB->sad16 = skip_sad; + pMB->cbp = 0; continue; } } if (frame->vop_flags & XVID_VOP_RD_BVOP) ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, - pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, pParam, i, j); + pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, pParam, i, j, best_sad); else ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV); + maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel); + } } + + frame->fcode = getMinFcode(MVmaxF); + frame->bcode = getMinFcode(MVmaxB); }