--- 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/04/10 13:05:54 982 @@ -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,36 +74,39 @@ #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) { - int xb, yb; - x = qpel ? x<<1 : x; - y = qpel ? y<<1 : y; + int bits; + const int q = (1 << (iFcode - 1)) - 1; + + x <<= qpel; + y <<= qpel; if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } x -= pred.x; + bits = (x != 0 ? iFcode:0); + x = abs(x); + x += q; + x >>= (iFcode - 1); + bits += mvtab[x]; + y -= pred.y; + bits += (y != 0 ? iFcode:0); + y = abs(y); + y += q; + y >>= (iFcode - 1); + bits += mvtab[y]; - if (x) { - x = ABS(x); - x += (1 << (iFcode - 1)) - 1; - x >>= (iFcode - 1); - if (x > 32) x = 32; - xb = mvtab[x] + iFcode; - } else xb = 1; - - if (y) { - y = ABS(y); - y += (1 << (iFcode - 1)) - 1; - y >>= (iFcode - 1); - if (y > 32) y = 32; - yb = mvtab[y] + iFcode; - } else yb = 1; - return xb + yb; + return bits; } -static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data) +static int32_t ChromaSAD2(const int fx, const int fy, const int bx, const int by, + const SearchData * const data) { int sad; const uint32_t stride = data->iEdgedWidth/2; @@ -91,50 +114,44 @@ * f_refv = data->RefQ + 8, * b_refu = data->RefQ + 16, * b_refv = data->RefQ + 24; + int offset = (fx>>1) + (fy>>1)*stride; 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; + f_refu = (uint8_t*)data->RefP[4] + offset; + f_refv = (uint8_t*)data->RefP[5] + offset; 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); + interpolate8x8_halfpel_v(f_refu, data->RefP[4] + offset, stride, data->rounding); + interpolate8x8_halfpel_v(f_refv, data->RefP[5] + offset, 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); + interpolate8x8_halfpel_h(f_refu, data->RefP[4] + offset, stride, data->rounding); + interpolate8x8_halfpel_h(f_refv, data->RefP[5] + offset, 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); + interpolate8x8_halfpel_hv(f_refu, data->RefP[4] + offset, stride, data->rounding); + interpolate8x8_halfpel_hv(f_refv, data->RefP[5] + offset, stride, data->rounding); break; } + offset = (bx>>1) + (by>>1)*stride; 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; + b_refu = (uint8_t*)data->b_RefP[4] + offset; + b_refv = (uint8_t*)data->b_RefP[5] + offset; 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); + interpolate8x8_halfpel_v(b_refu, data->b_RefP[4] + offset, stride, data->rounding); + interpolate8x8_halfpel_v(b_refv, data->b_RefP[5] + offset, 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); + interpolate8x8_halfpel_h(b_refu, data->b_RefP[4] + offset, stride, data->rounding); + interpolate8x8_halfpel_h(b_refv, data->b_RefP[5] + offset, 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); + interpolate8x8_halfpel_hv(b_refu, data->b_RefP[4] + offset, stride, data->rounding); + interpolate8x8_halfpel_hv(b_refv, data->b_RefP[5] + offset, stride, data->rounding); break; } @@ -144,38 +161,34 @@ return sad; } - static int32_t -ChromaSAD(int dx, int dy, const SearchData * const data) +ChromaSAD(const int dx, const int dy, const SearchData * const data) { int sad; const uint32_t stride = data->iEdgedWidth/2; + int offset = (dx>>1) + (dy>>1)*stride; if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently data->temp[5] = dx; data->temp[6] = dy; // backup switch (((dx & 1) << 1) | (dy & 1)) { case 0: - dx = dx / 2; dy = dy / 2; - sad = sad8(data->CurU, data->RefCU + dy * stride + dx, stride); - sad += sad8(data->CurV, data->RefCV + dy * stride + dx, stride); + sad = sad8(data->CurU, data->RefP[4] + offset, stride); + sad += sad8(data->CurV, data->RefP[5] + offset, stride); break; case 1: - dx = dx / 2; dy = (dy - 1) / 2; - sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + (dy+1) * stride + dx, stride); - sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + (dy+1) * stride + dx, stride); + sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + stride, stride); + sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + stride, stride); break; case 2: - dx = (dx - 1) / 2; dy = dy / 2; - sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + dy * stride + dx+1, stride); - sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + dy * stride + dx+1, stride); + sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + 1, stride); + sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + 1, stride); break; default: - dx = (dx - 1) / 2; dy = (dy - 1) / 2; - interpolate8x8_halfpel_hv(data->RefQ, data->RefCU + dy * stride + dx, stride, data->rounding); + interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding); sad = sad8(data->CurU, data->RefQ, stride); - interpolate8x8_halfpel_hv(data->RefQ, data->RefCV + dy * stride + dx, stride, data->rounding); + interpolate8x8_halfpel_hv(data->RefQ, data->RefP[5] + offset, stride, data->rounding); sad += sad8(data->CurV, data->RefQ, stride); break; } @@ -187,28 +200,19 @@ GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) { // dir : 0 = forward, 1 = backward - switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { - case 0 : return data->Ref + x/2 + (y/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); - case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); - case 4 : return data->bRef + x/2 + (y/2)*(data->iEdgedWidth); - case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); - case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); - default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); - } + const uint8_t* const *direction = ( dir == 0 ? data->RefP : data->b_RefP ); + const int picture = ((x&1)<<1) | (y&1); + const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; + return direction[picture] + offset; } // this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate static __inline const uint8_t * GetReference(const int x, const int y, const SearchData * const data) { - 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); - default : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); //case 2 - } + const int picture = ((x&1)<<1) | (y&1); + const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; + return data->RefP[picture] + offset; } static uint8_t * @@ -225,8 +229,15 @@ ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; 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); + ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; + ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; + ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; + interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); break; case 1: // x halfpel, y qpel - top or bottom during qpel refinement @@ -241,16 +252,9 @@ interpolate8x8_avg2(Reference, ref1, ref2, 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); - ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; - ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; - ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; - interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); - break; + default: // pure halfpel position + return (uint8_t *) ref1; + } return Reference; } @@ -269,7 +273,7 @@ ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data); switch( ((x&1)<<1) + (y&1) ) { case 3: // x and y in qpel resolution - the "corners" (top left/right and - // bottom left/right) during qpel refinement + // 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); @@ -295,7 +299,7 @@ interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8); break; - case 0: // pure halfpel position + default: // pure halfpel position return (uint8_t *) ref1; } return Reference; @@ -331,7 +335,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; @@ -355,12 +359,18 @@ { int32_t sad; uint32_t t; const uint8_t * Reference; + VECTOR * current; 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 = Interpolate8x8qpel(x, y, 0, 0, data); + if (!data->qpel_precision) { + Reference = GetReference(x, y, data); + current = data->currentMV; + } else { // x and y are in 1/4 precision + Reference = Interpolate8x8qpel(x, y, 0, 0, data); + current = data->currentQMV; + } sad = sad8(data->Cur, Reference, data->iEdgedWidth); t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); @@ -369,7 +379,7 @@ if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; - data->currentMV->x = x; data->currentMV->y = y; + current->x = x; current->y = y; *dir = Direction; } } @@ -381,7 +391,7 @@ uint32_t t; const uint8_t * Reference; - if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value + if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero even value (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; @@ -416,8 +426,8 @@ uint32_t t; VECTOR * current; - 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->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value @@ -455,7 +465,7 @@ if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; - sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), + sad = sad32v_c(data->Cur, data->RefP[0] + x/2 + (y/2)*(data->iEdgedWidth), data->iEdgedWidth, data->temp+1); if (sad < *(data->iMinSAD)) { @@ -482,8 +492,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 +539,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 +552,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; @@ -586,7 +598,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; mvs.x = data->directmvF[0].x + x; b_mvs.x = ((x == 0) ? @@ -598,10 +610,10 @@ 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; + 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); @@ -635,7 +647,7 @@ CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { - static int16_t in[64], coeff[64]; + int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; int32_t bits = 0, sum; VECTOR * current; const uint8_t * ptr; @@ -673,7 +685,7 @@ yc = (yc >> 1) + roundtab_79[yc & 0x3]; //chroma U - ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCU, 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); + ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); fdct(in); if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); @@ -685,7 +697,7 @@ if (bits < data->iMinSAD[0]) { //chroma V - ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCV, 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); + ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); fdct(in); if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); @@ -697,7 +709,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]) { @@ -720,7 +732,7 @@ CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) { - static int16_t in[64], coeff[64]; + int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; int32_t sum, bits; VECTOR * current; const uint8_t * ptr; @@ -926,21 +938,22 @@ const uint32_t stride, const uint32_t iQuant, int rrv) { + int offset = (x + y*stride)*8; if(!rrv) { - uint32_t sadC = sad8(current->u + x*8 + y*stride*8, - reference->u + x*8 + y*stride*8, stride); + uint32_t sadC = sad8(current->u + offset, + reference->u + offset, stride); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; - sadC += sad8(current->v + (x + y*stride)*8, - reference->v + (x + y*stride)*8, stride); + sadC += sad8(current->v + offset, + reference->v + offset, stride); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; return 1; } else { - uint32_t sadC = sad16(current->u + x*16 + y*stride*16, - reference->u + x*16 + y*stride*16, stride, 256*4096); + uint32_t sadC = sad16(current->u + 2*offset, + reference->u + 2*offset, stride, 256*4096); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; - sadC += sad16(current->v + (x + y*stride)*16, - reference->v + (x + y*stride)*16, stride, 256*4096); + sadC += sad16(current->v + 2*offset, + reference->v + 2*offset, stride, 256*4096); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; return 1; } @@ -971,17 +984,22 @@ 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]; VECTOR currentMV[5]; VECTOR currentQMV[5]; int32_t iMinSAD[5]; + DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE); SearchData Data; memset(&Data, 0, sizeof(SearchData)); Data.iEdgedWidth = iEdgedWidth; @@ -991,11 +1009,12 @@ 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 ? 1:0); + Data.chroma = MotionFlags & XVID_ME_CHROMA16; + Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0); + Data.dctSpace = dct_space; - 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 +1047,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 +1066,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,10 +1083,9 @@ } } - 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); - } + return 0; } @@ -1148,19 +1157,18 @@ 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 sad; int InterBias = MV16_INTER_BIAS; if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { - mode = 0; //inter - sad = Data->iMinSAD[0]; + mode = MODE_INTER; + sad = Data->iMinSAD[0]; } else { mode = MODE_INTER4V; sad = Data->iMinSAD[1] + Data->iMinSAD[2] + @@ -1187,7 +1195,7 @@ dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) + dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth); - if (deviation < (sad - InterBias)) return MODE_INTRA;// intra + if (deviation < (sad - InterBias)) return MODE_INTRA; } return mode; @@ -1196,7 +1204,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++) { @@ -1208,8 +1216,8 @@ if (bits == 0) return MODE_INTER; // quick stop if (inter4v) { - int inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); - if (inter4v < bits) { Data->iMinSAD[0] = bits = inter4v; mode = MODE_INTER4V; } + int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); + if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } } @@ -1230,7 +1238,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, @@ -1254,21 +1262,20 @@ Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; - Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16*i; - Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; - Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; + 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 = lambda_vec16[iQuant]; 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; + 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]; @@ -1280,7 +1287,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 +1313,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 +1327,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 +1358,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 +1367,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 +1393,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 +1409,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,15 +1468,16 @@ *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; - if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) { - if (Data->rrv) i = 2; else i = 1; + if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { + + if (Data->rrv) i = 16; else i = 8; - Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->RefH = OldData->RefH + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->RefV = OldData->RefV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->RefHV = OldData->RefHV + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); - Data->Cur = OldData->Cur + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); + Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1)); Data->qpel_precision = 0; get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, @@ -1476,12 +1486,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 +1502,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 +1513,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); @@ -1601,12 +1611,12 @@ Data->qpel_precision = 0; Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache - 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->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; + Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; + Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; Data->predMV = *predMV; @@ -1627,8 +1637,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); @@ -1688,9 +1698,9 @@ for (k = 0; k < 4; k++) { dy += Data->directmvF[k].y / div; - dx += Data->directmvF[0].x / div; - b_dy += Data->directmvB[0].y / div; - b_dx += Data->directmvB[0].x / div; + dx += Data->directmvF[k].x / div; + b_dy += Data->directmvB[k].y / div; + b_dx += Data->directmvB[k].x / div; } dy = (dy >> 3) + roundtab_76[dy & 0xf]; @@ -1710,7 +1720,13 @@ b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, stride); - if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped + if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { + pMB->mode = MODE_DIRECT_NONE_MV; //skipped + for (k = 0; k < 4; k++) { + pMB->qmvs[k] = pMB->mvs[k]; + pMB->b_qmvs[k] = pMB->b_mvs[k]; + } + } } static __inline uint32_t @@ -1738,18 +1754,18 @@ MainSearchFunc *MainSearchPtr; *Data->iMinSAD = 256*4096; - Data->Ref = f_Ref->y + k; - Data->RefH = f_RefH + k; - Data->RefV = f_RefV + k; - Data->RefHV = f_RefHV + k; - Data->bRef = b_Ref->y + k; - 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; + Data->RefP[0] = f_Ref->y + k; + Data->RefP[2] = f_RefH + k; + Data->RefP[1] = f_RefV + k; + Data->RefP[3] = f_RefHV + k; + Data->b_RefP[0] = b_Ref->y + k; + Data->b_RefP[2] = b_RefH + k; + Data->b_RefP[1] = b_RefV + k; + Data->b_RefP[3] = b_RefHV + k; + Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; + Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; + Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; + Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; k = Data->qpel ? 4 : 2; Data->max_dx = k * (pParam->width - x * 16); @@ -1799,13 +1815,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); @@ -1879,19 +1896,19 @@ fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; i = (x + y * fData->iEdgedWidth) * 16; - 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->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.b_RefP[0] = fData->RefP[0] = f_Ref->y + i; + bData.b_RefP[2] = fData->RefP[2] = f_RefH + i; + bData.b_RefP[1] = fData->RefP[1] = f_RefV + i; + bData.b_RefP[3] = fData->RefP[3] = f_RefHV + i; + bData.RefP[0] = fData->b_RefP[0] = b_Ref->y + i; + bData.RefP[2] = fData->b_RefP[2] = b_RefH + i; + bData.RefP[1] = fData->b_RefP[1] = b_RefV + i; + bData.RefP[3] = fData->b_RefP[3] = b_RefHV + i; + bData.b_RefP[4] = fData->RefP[4] = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; + bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; bData.bpredMV = fData->predMV = *f_predMV; fData->bpredMV = bData.predMV = *b_predMV; @@ -2014,9 +2031,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 +2141,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,10 +2157,10 @@ 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; + Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; pmv[1].x = EVEN(pMB->mvs[0].x); pmv[1].y = EVEN(pMB->mvs[0].y); @@ -2152,42 +2170,43 @@ CheckCandidate32I(0, 0, 255, &i, Data); - 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 1300 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 +2215,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 +2252,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 > 4) sSAD += (s - 3) * (300 - 2*b_thresh); //static block - looks bad when in bframe... + if (sSAD > InterThresh ) return P_VOP; emms(); return B_VOP; - } @@ -2271,12 +2301,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]; @@ -2285,15 +2319,15 @@ 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) ) ) + 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++) + 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 +2335,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 +2348,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]; @@ -2337,7 +2371,7 @@ denom = a*a+b*b-c*n; -/* Solve the system: sol = (D'*E*D)^{-1} D'*E*F */ +/* Solve the system: sol = (D'*E*D)^{-1} D'*E*F */ /* D'*E*F has been calculated in the same loop as matrix */ sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2]; @@ -2352,8 +2386,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]; @@ -2363,8 +2397,8 @@ 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 ); + meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); + meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); } if (4*meanx > oldnum) /* better fit than 0.25 is useless */ @@ -2381,8 +2415,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]; @@ -2391,8 +2425,8 @@ 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 ) ) + if ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) + || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) MBmask[mbnum]=0; else num++; @@ -2446,7 +2480,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 +2498,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 +2514,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); @@ -2503,7 +2537,7 @@ int cbp = 0, bits = 0, t = 0, i, iDirection; SearchData Data2, *Data8 = &Data2; int sumx = 0, sumy = 0; - int16_t in[64], coeff[64]; + int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; memcpy(Data8, Data, sizeof(SearchData)); CheckCandidate = CheckCandidateBits8; @@ -2513,10 +2547,10 @@ Data8->currentMV = Data->currentMV + i + 1; Data8->currentQMV = Data->currentQMV + i + 1; Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); - Data8->Ref = Data->Ref + 8*((i&1) + (i>>1)*Data->iEdgedWidth); - Data8->RefH = Data->RefH + 8*((i&1) + (i>>1)*Data->iEdgedWidth); - Data8->RefV = Data->RefV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); - Data8->RefHV = Data->RefHV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); + Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); + Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); + Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); + Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); if(Data->qpel) { Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); @@ -2544,7 +2578,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 +2589,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 +2604,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); @@ -2609,7 +2643,7 @@ sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; //chroma U - ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCU, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); + ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); fdct(in); if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); @@ -2621,7 +2655,7 @@ if (bits < *Data->iMinSAD) { // still possible //chroma V - ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCV, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); + ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); fdct(in); if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); @@ -2630,7 +2664,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; } } @@ -2643,25 +2677,20 @@ CountMBBitsIntra(const SearchData * const Data) { int bits = 1; //this one is ac/dc prediction flag. always 1. - int cbp = 0, i, t, dc = 0, b_dc = 1024; + int cbp = 0, i, t, dc = 1024, b_dc; const uint32_t iQuant = Data->lambda16; - int16_t in[64], coeff[64]; + int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; + uint32_t iDcScaler = get_dc_scaler(iQuant, 1);; for(i = 0; i < 4; i++) { - uint32_t iDcScaler = get_dc_scaler(iQuant, 1); - int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); fdct(in); - b_dc = dc; - dc = in[0]; - in[0] -= b_dc; - if (Data->lambda8 == 0) quant_intra_c(coeff, in, iQuant, iDcScaler); - else quant4_intra_c(coeff, in, iQuant, iDcScaler); - - b_dc = dc; - dc = coeff[0]; - if (i != 0) coeff[0] -= b_dc; + b_dc = in[0]; + in[0] -= dc; + dc = b_dc; + if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); + else quant4_intra(coeff, in, iQuant, iDcScaler); bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; Data->temp[i] = t; @@ -2670,7 +2699,7 @@ } if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma - uint32_t iDcScaler = get_dc_scaler(iQuant, 0); + iDcScaler = get_dc_scaler(iQuant, 0); //chroma U transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); fdct(in); @@ -2680,7 +2709,6 @@ 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]) { //chroma V @@ -2693,16 +2721,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; }