--- motion_est.c 2003/01/09 11:36:33 1.44.2.44 +++ motion_est.c 2003/02/06 09:25:37 1.44.2.50 @@ -32,6 +32,7 @@ #include #include #include // memcpy +#include // lrint #include "../encoder.h" #include "../utils/mbfunctions.h" @@ -56,8 +57,10 @@ d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) { int xb, yb; - x += x * qpel; y += y * qpel; + x = qpel ? x<<1 : x; + y = qpel ? y<<1 : y; if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } + x -= pred.x; y -= pred.y; @@ -79,6 +82,68 @@ return xb + yb; } +static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data) +{ + int sad; + const uint32_t stride = data->iEdgedWidth/2; + uint8_t * f_refu = data->RefQ, + * f_refv = data->RefQ + 8, + * b_refu = data->RefQ + 16, + * b_refv = data->RefQ + 24; + + switch (((fx & 1) << 1) | (fy & 1)) { + case 0: + fx = fx / 2; fy = fy / 2; + f_refu = (uint8_t*)data->RefCU + fy * stride + fx, stride; + f_refv = (uint8_t*)data->RefCV + fy * stride + fx, stride; + break; + case 1: + fx = fx / 2; fy = (fy - 1) / 2; + interpolate8x8_halfpel_v(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); + interpolate8x8_halfpel_v(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); + break; + case 2: + fx = (fx - 1) / 2; fy = fy / 2; + interpolate8x8_halfpel_h(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); + interpolate8x8_halfpel_h(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); + break; + default: + fx = (fx - 1) / 2; fy = (fy - 1) / 2; + interpolate8x8_halfpel_hv(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); + interpolate8x8_halfpel_hv(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); + break; + } + + switch (((bx & 1) << 1) | (by & 1)) { + case 0: + bx = bx / 2; by = by / 2; + b_refu = (uint8_t*)data->b_RefCU + by * stride + bx, stride; + b_refv = (uint8_t*)data->b_RefCV + by * stride + bx, stride; + break; + case 1: + bx = bx / 2; by = (by - 1) / 2; + interpolate8x8_halfpel_v(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); + interpolate8x8_halfpel_v(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); + break; + case 2: + bx = (bx - 1) / 2; by = by / 2; + interpolate8x8_halfpel_h(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); + interpolate8x8_halfpel_h(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); + break; + default: + bx = (bx - 1) / 2; by = (by - 1) / 2; + interpolate8x8_halfpel_hv(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); + interpolate8x8_halfpel_hv(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); + break; + } + + sad = sad8bi(data->CurU, b_refu, f_refu, stride); + sad += sad8bi(data->CurV, b_refv, f_refv, stride); + + return sad; +} + + static int32_t ChromaSAD(int dx, int dy, const SearchData * const data) { @@ -139,9 +204,9 @@ { switch ( ((x&1)<<1) | (y&1) ) { case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); + case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); - case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); - default : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); + default : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); //case 2 } } @@ -202,8 +267,17 @@ ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); switch( ((x&1)<<1) + (y&1) ) { - case 0: // pure halfpel position - return (uint8_t *) ref1; + case 3: // x and y in qpel resolution - the "corners" (top left/right and + // bottom left/right) during qpel refinement + ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); + ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); + ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); + interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); + interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); + interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); + interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding); + break; + case 1: // x halfpel, y qpel - top or bottom during qpel refinement ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); @@ -220,16 +294,8 @@ interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8); break; - default: // x and y in qpel resolution - the "corners" (top left/right and - // bottom left/right) during qpel refinement - ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data); - ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data); - ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data); - interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); - interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); - interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); - interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding); - break; + case 0: // pure halfpel position + return (uint8_t *) ref1; } return Reference; } @@ -239,39 +305,41 @@ static void CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { - int t, xc, yc; + int xc, yc; const uint8_t * Reference; VECTOR * current; + int32_t sad; uint32_t t; - if ( (x > data->max_dx) | (x < data->min_dx) - | (y > data->max_dy) | (y < data->min_dy) ) return; + if ( (x > data->max_dx) || (x < data->min_dx) + || (y > data->max_dy) || (y < data->min_dy) ) return; - if (data->qpel_precision) { // x and y are in 1/4 precision - Reference = Interpolate16x16qpel(x, y, 0, data); - xc = x/2; yc = y/2; //for chroma sad - current = data->currentQMV; - } else { + if (!data->qpel_precision) { Reference = GetReference(x, y, data); current = data->currentMV; xc = x; yc = y; + } else { // x and y are in 1/4 precision + Reference = Interpolate16x16qpel(x, y, 0, data); + xc = x/2; yc = y/2; //for chroma sad + current = data->currentQMV; } - t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); - data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); + sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); + t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); - data->temp[0] += (data->lambda16 * t * data->temp[0])>>10; + sad += (data->lambda16 * t * sad)>>10; data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; - if (data->chroma) data->temp[0] += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], + if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], (yc >> 1) + roundtab_79[yc & 0x3], data); - if (data->temp[0] < data->iMinSAD[0]) { - data->iMinSAD[0] = data->temp[0]; + if (sad < data->iMinSAD[0]) { + data->iMinSAD[0] = sad; current[0].x = x; current[0].y = y; - *dir = Direction; } + *dir = Direction; + } if (data->temp[1] < data->iMinSAD[1]) { - data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; } + data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y = y; } if (data->temp[2] < data->iMinSAD[2]) { data->iMinSAD[2] = data->temp[2]; current[2].x = x; current[2].y = y; } if (data->temp[3] < data->iMinSAD[3]) { @@ -282,14 +350,39 @@ } static void +CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) +{ + int32_t sad; uint32_t t; + const uint8_t * Reference; + + if ( (x > data->max_dx) || (x < data->min_dx) + || (y > data->max_dy) || (y < data->min_dy) ) return; + + if (!data->qpel_precision) Reference = GetReference(x, y, data); + else Reference = Interpolate16x16qpel(x, y, 0, data); + + sad = sad8(data->Cur, Reference, data->iEdgedWidth); + t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); + + sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; + + if (sad < *(data->iMinSAD)) { + *(data->iMinSAD) = sad; + data->currentMV->x = x; data->currentMV->y = y; + *dir = Direction; + } +} + + +static void CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { uint32_t t; const uint8_t * Reference; - if ( (!(x&1) && x !=0) | (!(y&1) && y !=0) || //non-zero integer value - (x > data->max_dx) | (x < data->min_dx) - | (y > data->max_dy) | (y < data->min_dy) ) return; + if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value + (x > data->max_dx) || (x < data->min_dx) + || (y > data->max_dy) || (y < data->min_dy) ) return; Reference = GetReference(x, y, data); t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1); @@ -317,7 +410,7 @@ static void CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { - int32_t sad; + int32_t sad, xc, yc; const uint8_t * Reference; uint32_t t; VECTOR * current; @@ -330,9 +423,11 @@ if (data->qpel_precision) { // x and y are in 1/4 precision Reference = Interpolate16x16qpel(x, y, 0, data); current = data->currentQMV; + xc = x/2; yc = y/2; } else { Reference = GetReference(x, y, data); current = data->currentMV; + xc = x; yc = y; } t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, data->rrv); @@ -340,27 +435,34 @@ sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); sad += (data->lambda16 * t * sad)>>10; + if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], + (yc >> 1) + roundtab_79[yc & 0x3], data); + + if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; current->x = x; current->y = y; - *dir = Direction; } + *dir = Direction; + } } static void CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { // maximum speed - for P/B/I decision + int32_t sad; - if ( (x > data->max_dx) | (x < data->min_dx) - | (y > data->max_dy) | (y < data->min_dy) ) return; + if ( (x > data->max_dx) || (x < data->min_dx) + || (y > data->max_dy) || (y < data->min_dy) ) return; - data->temp[0] = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), + sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), data->iEdgedWidth, data->temp+1); - if (data->temp[0] < *(data->iMinSAD)) { - *(data->iMinSAD) = data->temp[0]; + if (sad < *(data->iMinSAD)) { + *(data->iMinSAD) = sad; data->currentMV[0].x = x; data->currentMV[0].y = y; - *dir = Direction; } + *dir = Direction; + } if (data->temp[1] < data->iMinSAD[1]) { data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } if (data->temp[2] < data->iMinSAD[2]) { @@ -375,7 +477,7 @@ static void CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data) { - int32_t sad, xb, yb; + int32_t sad, xb, yb, xcf, ycf, xcb, ycb; uint32_t t; const uint8_t *ReferenceF, *ReferenceB; VECTOR *current; @@ -388,11 +490,15 @@ xb = data->currentMV[1].x; yb = data->currentMV[1].y; ReferenceB = GetReferenceB(xb, yb, 1, data); current = data->currentMV; + xcf = xf; ycf = yf; + xcb = xb; ycb = yb; } else { ReferenceF = Interpolate16x16qpel(xf, yf, 0, data); xb = data->currentQMV[1].x; yb = data->currentQMV[1].y; current = data->currentQMV; ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); + xcf = xf/2; ycf = yf/2; + xcb = xb/2; ycb = yb/2; } t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) @@ -401,16 +507,22 @@ sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); sad += (data->lambda16 * t * sad)>>10; + if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], + (ycf >> 1) + roundtab_79[ycf & 0x3], + (xcb >> 1) + roundtab_79[xcb & 0x3], + (ycb >> 1) + roundtab_79[ycb & 0x3], data); + if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; current->x = xf; current->y = yf; - *dir = Direction; } + *dir = Direction; + } } static void CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { - int32_t sad = 0; + int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; uint32_t k; const uint8_t *ReferenceF; const uint8_t *ReferenceB; @@ -429,15 +541,21 @@ 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; - - - mvs.x *= 2 - data->qpel; mvs.y *= 2 - data->qpel; - b_mvs.x *= 2 - data->qpel; b_mvs.y *= 2 - data->qpel; //we move to qpel precision anyway - + 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; + xcb += b_mvs.x/2; ycb += b_mvs.y/2; + } else { + xcf += mvs.x; ycf += mvs.y; + xcb += b_mvs.x; ycb += b_mvs.y; + mvs.x *= 2; mvs.y *= 2; //we move to qpel precision anyway + b_mvs.x *= 2; b_mvs.y *= 2; + } + ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); @@ -448,16 +566,22 @@ sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; + if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], + (ycf >> 3) + roundtab_76[ycf & 0xf], + (xcb >> 3) + roundtab_76[xcb & 0xf], + (ycb >> 3) + roundtab_76[ycb & 0xf], data); + if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; data->currentMV->x = x; data->currentMV->y = y; - *dir = Direction; } + *dir = Direction; + } } static void CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { - int32_t sad; + int32_t sad, xcf, ycf, xcb, ycb; const uint8_t *ReferenceF; const uint8_t *ReferenceB; VECTOR mvs, b_mvs; @@ -474,47 +598,36 @@ data->directmvB[0].y : mvs.y - data->referencemv[0].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; - - mvs.x *= 2 - data->qpel; mvs.y *= 2 - data->qpel; - b_mvs.x *= 2 - data->qpel; b_mvs.y *= 2 - data->qpel; //we move to qpel precision anyway - - ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data); - ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); + 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 = 4*(mvs.x/2); ycf = 4*(mvs.y/2); + xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); + ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data); + ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); + } else { + xcf = 4*mvs.x; ycf = 4*mvs.y; + xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; + ReferenceF = GetReference(mvs.x, mvs.y, data); + ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); + } sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; - if (sad < *(data->iMinSAD)) { - *(data->iMinSAD) = sad; - data->currentMV->x = x; data->currentMV->y = y; - *dir = Direction; } -} - -static void -CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) -{ - int32_t sad; uint32_t t; - const uint8_t * Reference; - - if ( (x > data->max_dx) | (x < data->min_dx) - | (y > data->max_dy) | (y < data->min_dy) ) return; - - if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data); - else Reference = GetReference(x, y, data); - - sad = sad8(data->Cur, Reference, data->iEdgedWidth); - t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); - - sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; + if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], + (ycf >> 3) + roundtab_76[ycf & 0xf], + (xcb >> 3) + roundtab_76[xcb & 0xf], + (ycb >> 3) + roundtab_76[ycb & 0xf], data); if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; data->currentMV->x = x; data->currentMV->y = y; - *dir = Direction; } + *dir = Direction; + } } /* CHECK_CANDIATE FUNCTIONS END */ @@ -752,7 +865,7 @@ Data.iFcode = current->fcode; Data.rounding = pParam->m_rounding_type; Data.qpel = pParam->m_quarterpel; - Data.chroma = current->global_flags & XVID_ME_COLOUR; + Data.chroma = current->motion_flags & PMV_CHROMA16; Data.rrv = current->global_flags & XVID_REDUCED; if ((current->global_flags & XVID_REDUCED)) { @@ -800,8 +913,8 @@ pMB->quant = current->quant; //initial skip decision -/* no early skip for GMC (global vector = skip vector is unknown!) */ - if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */ +/* 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 (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { SkipMacroblockP(pMB, sad00); @@ -815,7 +928,7 @@ current->global_flags & XVID_INTER4V, pMB); /* final skip decision, a.k.a. "the vector you found, really that good?" */ - if (current->coding_type == P_VOP) { + if (!(current->global_flags & XVID_GMC)) { if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) && ((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)) { @@ -855,11 +968,11 @@ } } - if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */ - current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode ); - else - current->GMC_MV = zeroMV; - + if (current->global_flags & XVID_GMC ) /* GMC only for S(GMC)-VOPs */ + { + current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); + } + return 0; } @@ -1269,7 +1382,7 @@ /* search backward or forward */ static void -SearchBF( const uint8_t * const pRef, +SearchBF( const IMAGE * const pRef, const uint8_t * const pRefH, const uint8_t * const pRefV, const uint8_t * const pRefHV, @@ -1291,11 +1404,14 @@ *Data->iMinSAD = MV_MAX_ERROR; Data->iFcode = iFcode; Data->qpel_precision = 0; + Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache - Data->Ref = pRef + (x + y * Data->iEdgedWidth) * 16; + Data->Ref = pRef->y + (x + y * Data->iEdgedWidth) * 16; Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16; Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16; Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16; + Data->RefCU = pRef->u + (x + y * Data->iEdgedWidth/2) * 8; + Data->RefCV = pRef->v + (x + y * Data->iEdgedWidth/2) * 8; Data->predMV = *predMV; @@ -1435,6 +1551,10 @@ Data->bRefH = b_RefH + k; Data->bRefV = b_RefV + k; Data->bRefHV = b_RefHV + k; + Data->RefCU = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; + Data->RefCV = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; + Data->b_RefCU = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; + Data->b_RefCV = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; k = Data->qpel ? 4 : 2; Data->max_dx = k * (pParam->width - x * 16); @@ -1473,10 +1593,15 @@ (*CheckCandidate)(0, 0, 255, &k, Data); // initial (fast) skip decision - if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * 2) { - //possible skip - checking chroma - SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data); - if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip. + if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) { + //possible skip + if (Data->chroma) { + pMB->mode = MODE_DIRECT_NONE_MV; + return *Data->iMinSAD; // skip. + } else { + SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data); + if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip. + } } skip_sad = *Data->iMinSAD; @@ -1527,11 +1652,11 @@ } static void -SearchInterpolate(const uint8_t * const f_Ref, +SearchInterpolate(const IMAGE * const f_Ref, const uint8_t * const f_RefH, const uint8_t * const f_RefV, const uint8_t * const f_RefHV, - const uint8_t * const b_Ref, + const IMAGE * const b_Ref, const uint8_t * const b_RefH, const uint8_t * const b_RefV, const uint8_t * const b_RefHV, @@ -1559,14 +1684,19 @@ fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; i = (x + y * fData->iEdgedWidth) * 16; - bData.bRef = fData->Ref = f_Ref + i; + bData.bRef = fData->Ref = f_Ref->y + i; bData.bRefH = fData->RefH = f_RefH + i; bData.bRefV = fData->RefV = f_RefV + i; bData.bRefHV = fData->RefHV = f_RefHV + i; - bData.Ref = fData->bRef = b_Ref + i; + bData.Ref = fData->bRef = b_Ref->y + i; bData.RefH = fData->bRefH = b_RefH + i; bData.RefV = fData->bRefV = b_RefV + i; bData.RefHV = fData->bRefHV = b_RefHV + i; + bData.b_RefCU = fData->RefCU = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.b_RefCV = fData->RefCV = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.RefCU = fData->b_RefCU = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.RefCV = fData->b_RefCV = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.bpredMV = fData->predMV = *f_predMV; fData->bpredMV = bData.predMV = *b_predMV; @@ -1683,14 +1813,16 @@ int32_t iMinSAD; VECTOR currentMV[3]; VECTOR currentQMV[3]; + int32_t temp[8]; memset(&Data, 0, sizeof(SearchData)); Data.iEdgedWidth = pParam->edged_width; Data.currentMV = currentMV; Data.currentQMV = currentQMV; Data.iMinSAD = &iMinSAD; Data.lambda16 = lambda_vec16[frame->quant]; - Data.chroma = frame->quant; Data.qpel = pParam->m_quarterpel; Data.rounding = 0; + Data.chroma = frame->motion_flags & PMV_CHROMA8; + Data.temp = temp; Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose) // note: i==horizontal, j==vertical @@ -1710,6 +1842,8 @@ } 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 @@ -1728,7 +1862,7 @@ if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } // forward search - SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, + SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y, &frame->image, i, j, frame->motion_flags, frame->fcode, pParam, @@ -1736,7 +1870,7 @@ MODE_FORWARD, &Data); // backward search - SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, + SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y, &frame->image, i, j, frame->motion_flags, frame->bcode, pParam, @@ -1744,8 +1878,8 @@ MODE_BACKWARD, &Data); // interpolate search comes last, because it uses data from forward and backward as prediction - SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, - b_ref->y, b_refH->y, b_refV->y, b_refHV->y, + SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y, + b_ref, b_refH->y, b_refV->y, b_refHV->y, &frame->image, i, j, frame->fcode, frame->bcode, @@ -1756,7 +1890,7 @@ &Data); // final skip decision - if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP*2) + if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP * 2) && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data); @@ -1906,100 +2040,188 @@ } } sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); - if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; +// if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP; if (sSAD > InterThresh ) return P_VOP; emms(); return B_VOP; } -static void -CheckGMC(int x, int y, const int dir, int * iDirection, - const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC, - const MBParam * const pParam) + +static WARPPOINTS +GlobalMotionEst(const MACROBLOCK * const pMBs, + const MBParam * const pParam, + const FRAMEINFO * const current, + const FRAMEINFO * const reference, + const IMAGE * const pRefH, + const IMAGE * const pRefV, + const IMAGE * const pRefHV ) { - uint32_t mx, my, a, count = 0; - for (my = 1; my < pParam->mb_height-1; my++) - for (mx = 1; mx < pParam->mb_width-1; mx++) { - VECTOR mv; - const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; - if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue; - mv = pMB->mvs[0]; - a = ABS(mv.x - x) + ABS(mv.y - y); - if (a < 6) count += 6 - a; - } + const int deltax=8; // upper bound for difference between a MV and it's neighbour MVs + const int deltay=8; + const int grad=512; // lower bound for deviation in MB + + WARPPOINTS gmc; + + uint32_t mx, my; - if (count > *bestcount) { - *bestcount = count; - *iDirection = dir; - GMC->x = x; GMC->y = y; + int MBh = pParam->mb_height; + int MBw = pParam->mb_width; + + int *MBmask= calloc(MBh*MBw,sizeof(int)); + double DtimesF[4] = { 0.,0., 0., 0. }; + double sol[4] = { 0., 0., 0., 0. }; + double a,b,c,n,denom; + double meanx,meany; + int num,oldnum; + + if (!MBmask) { fprintf(stderr,"Mem error\n"); return gmc;} + +// filter mask of all blocks + + for (my = 1; my < MBh-1; my++) + for (mx = 1; mx < MBw-1; mx++) + { + const int mbnum = mx + my * MBw; + const MACROBLOCK *pMB = &pMBs[mbnum]; + const VECTOR mv = pMB->mvs[0]; + + if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) + continue; + + if ( ( (ABS(mv.x - (pMB-1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-1)->mvs[0].y) < deltay) ) + && ( (ABS(mv.x - (pMB+1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+1)->mvs[0].y) < deltay) ) + && ( (ABS(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-MBw)->mvs[0].y) < deltay) ) + && ( (ABS(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) ) + MBmask[mbnum]=1; + } + + for (my = 1; my < MBh-1; my++) + for (mx = 1; mx < MBw-1; mx++) + { + const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx; + + const int mbnum = mx + my * MBw; + if (!MBmask[mbnum]) + continue; + + if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= grad ) + MBmask[mbnum] = 0; + if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= grad ) + MBmask[mbnum] = 0; + } -} + emms(); -static VECTOR -GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) -{ + do { /* until convergence */ - uint32_t count, bestcount = 0; - int x, y; - VECTOR gmc = {0,0}; - int step, min_x, max_x, min_y, max_y; - uint32_t mx, my; - int iDirection, bDirection; + 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++) + { + const int mbnum = mx + my * MBw; + const MACROBLOCK *pMB = &pMBs[mbnum]; + const VECTOR mv = pMB->mvs[0]; + + if (!MBmask[mbnum]) + continue; + + n++; + a += 16*mx+8; + b += 16*my+8; + c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8); + + DtimesF[0] += (double)mv.x; + DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8); + DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8); + DtimesF[3] += (double)mv.y; + } - min_x = min_y = -32<mvs[0]; + + if (!MBmask[mbnum]) + continue; + + oldnum++; + meanx += ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); + meany += ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); + } -//step1: let's find a rough camera panning - for (step = 32; step >= 2; step /= 2) { - bestcount = 0; - for (y = min_y; y <= max_y; y += step) - for (x = min_x ; x <= max_x; x += step) { - count = 0; - //for all macroblocks - for (my = 1; my < pParam->mb_height-1; my++) - for (mx = 1; mx < pParam->mb_width-1; mx++) { - const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; - VECTOR mv; - - if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) - continue; - - mv = pMB->mvs[0]; - if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */ - count++; - } - if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; } - } - min_x = gmc.x - step; - max_x = gmc.x + step; - min_y = gmc.y - step; - max_y = gmc.y + step; - } + if (4*meanx > oldnum) /* better fit than 0.25 is useless */ + meanx /= oldnum; + else + meanx = 0.25; + + if (4*meany > oldnum) + meany /= oldnum; + else + meany = 0.25; + +/* fprintf(stderr,"sol = (%8.5f, %8.5f, %8.5f, %8.5f)\n",sol[0],sol[1],sol[2],sol[3]); + 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++) + { + const int mbnum = mx + my * MBw; + const MACROBLOCK *pMB = &pMBs[mbnum]; + const VECTOR mv = pMB->mvs[0]; + + if (!MBmask[mbnum]) + continue; + + if ( ( ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) + || ( ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) + MBmask[mbnum]=0; + else + num++; + } - if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) - gmc.x = gmc.y = 0; //no camara pan, no GMC - -// step2: let's refine camera panning using gradiend-descent approach -// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) - bestcount = 0; - CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); - do { - x = gmc.x; y = gmc.y; - bDirection = iDirection; iDirection = 0; - if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); - if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); - if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam); - if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam); + } while ( (oldnum != num) && (num>=4) ); - } while (iDirection); + if (num < 4) + { + 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; + } else { + + gmc.duv[0].x=(int)(sol[0]+0.5); + gmc.duv[0].y=(int)(sol[3]+0.5); - if (pParam->m_quarterpel) { - gmc.x *= 2; - gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */ + gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5); + gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5); + + gmc.duv[2].x=0; + gmc.duv[2].y=0; } +// fprintf(stderr,"wp1 = ( %4d, %4d) wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y); + free(MBmask); + return gmc; }