21 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: motion_est.c,v 1.58.2.21 2003-07-10 17:41:48 edgomez Exp $ |
* $Id: motion_est.c,v 1.58.2.28 2003-08-25 15:10:13 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
72 |
#define CHECK_CANDIDATE(X,Y,D) { \ |
#define CHECK_CANDIDATE(X,Y,D) { \ |
73 |
CheckCandidate((X),(Y), (D), &iDirection, data ); } |
CheckCandidate((X),(Y), (D), &iDirection, data ); } |
74 |
|
|
75 |
|
|
76 |
/***************************************************************************** |
/***************************************************************************** |
77 |
* Code |
* Code |
78 |
****************************************************************************/ |
****************************************************************************/ |
109 |
{ |
{ |
110 |
int sad; |
int sad; |
111 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
112 |
uint8_t * f_refu = data->RefQ, |
uint8_t *f_refu, *f_refv, *b_refu, *b_refv; |
113 |
* f_refv = data->RefQ + 8, |
|
114 |
* b_refu = data->RefQ + 16, |
const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { |
115 |
* b_refv = data->RefQ + 24; |
NULL, |
116 |
|
interpolate8x8_halfpel_v, |
117 |
|
interpolate8x8_halfpel_h, |
118 |
|
interpolate8x8_halfpel_hv |
119 |
|
}; |
120 |
|
|
121 |
int offset = (fx>>1) + (fy>>1)*stride; |
int offset = (fx>>1) + (fy>>1)*stride; |
122 |
|
int filter = ((fx & 1) << 1) | (fy & 1); |
123 |
|
|
124 |
switch (((fx & 1) << 1) | (fy & 1)) { |
if (filter != 0) { |
125 |
case 0: |
f_refu = data->RefQ; |
126 |
|
f_refv = data->RefQ + 8; |
127 |
|
interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding); |
128 |
|
interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding); |
129 |
|
} else { |
130 |
f_refu = (uint8_t*)data->RefP[4] + offset; |
f_refu = (uint8_t*)data->RefP[4] + offset; |
131 |
f_refv = (uint8_t*)data->RefP[5] + offset; |
f_refv = (uint8_t*)data->RefP[5] + offset; |
|
break; |
|
|
case 1: |
|
|
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: |
|
|
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: |
|
|
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; |
|
132 |
} |
} |
133 |
|
|
134 |
offset = (bx>>1) + (by>>1)*stride; |
offset = (bx>>1) + (by>>1)*stride; |
135 |
switch (((bx & 1) << 1) | (by & 1)) { |
filter = ((bx & 1) << 1) | (by & 1); |
136 |
case 0: |
|
137 |
|
if (filter != 0) { |
138 |
|
b_refu = data->RefQ + 16; |
139 |
|
b_refv = data->RefQ + 24; |
140 |
|
interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
141 |
|
interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
142 |
|
} else { |
143 |
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
144 |
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
|
break; |
|
|
case 1: |
|
|
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: |
|
|
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: |
|
|
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; |
|
145 |
} |
} |
146 |
|
|
147 |
sad = sad8bi(data->CurU, b_refu, f_refu, stride); |
sad = sad8bi(data->CurU, b_refu, f_refu, stride); |
291 |
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8); |
292 |
break; |
break; |
293 |
|
|
294 |
|
|
295 |
default: /* pure halfpel position */ |
default: /* pure halfpel position */ |
296 |
return (uint8_t *) ref1; |
return (uint8_t *) ref1; |
297 |
} |
} |
327 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t * sad)>>10; |
328 |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10; |
329 |
|
|
330 |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
if (data->chroma && sad < data->iMinSAD[0]) |
331 |
|
sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
332 |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
333 |
|
|
334 |
if (sad < data->iMinSAD[0]) { |
if (sad < data->iMinSAD[0]) { |
438 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
439 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t * sad)>>10; |
440 |
|
|
441 |
if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
if (data->chroma && sad < *data->iMinSAD) |
442 |
|
sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
443 |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
444 |
|
|
445 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
486 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
487 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
488 |
|
|
489 |
sad = sad32v_c(data->Cur, data->RefP[0] + (int)((x>>1) + (y>>1)*(data->iEdgedWidth)), |
sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*((int)data->iEdgedWidth), |
490 |
data->iEdgedWidth, data->temp+1); |
data->iEdgedWidth, data->temp+1); |
491 |
|
|
492 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
539 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
540 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t * sad)>>10; |
541 |
|
|
542 |
if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
if (data->chroma && sad < *data->iMinSAD) |
543 |
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
544 |
(ycf >> 1) + roundtab_79[ycf & 0x3], |
(ycf >> 1) + roundtab_79[ycf & 0x3], |
545 |
(xcb >> 1) + roundtab_79[xcb & 0x3], |
(xcb >> 1) + roundtab_79[xcb & 0x3], |
546 |
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
600 |
|
|
601 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
602 |
|
|
603 |
if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
if (data->chroma && sad < *data->iMinSAD) |
604 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
605 |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
606 |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
607 |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
653 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
654 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
655 |
|
|
656 |
if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
if (data->chroma && sad < *data->iMinSAD) |
657 |
|
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
658 |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
(ycf >> 3) + roundtab_76[ycf & 0xf], |
659 |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
(xcb >> 3) + roundtab_76[xcb & 0xf], |
660 |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
(ycb >> 3) + roundtab_76[ycb & 0xf], data); |
668 |
|
|
669 |
|
|
670 |
static void |
static void |
671 |
CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateRD16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
672 |
{ |
{ |
673 |
|
|
674 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
675 |
int32_t bits = 0; |
int32_t rd = 0; |
676 |
VECTOR * current; |
VECTOR * current; |
677 |
const uint8_t * ptr; |
const uint8_t * ptr; |
678 |
int i, cbp = 0, t, xc, yc; |
int i, cbp = 0, t, xc, yc; |
693 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
694 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
695 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
696 |
bits += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i); |
rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i); |
697 |
} |
} |
698 |
|
|
699 |
bits += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
rd += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
700 |
|
|
701 |
if (data->temp[0] + t < data->iMinSAD[1]) { |
if (data->temp[0] + t < data->iMinSAD[1]) { |
702 |
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; } |
data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); } |
703 |
if (data->temp[1] < data->iMinSAD[2]) { |
if (data->temp[1] < data->iMinSAD[2]) { |
704 |
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; } |
data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; data->cbp[1] = (data->cbp[1]&~16) | (cbp&16); } |
705 |
if (data->temp[2] < data->iMinSAD[3]) { |
if (data->temp[2] < data->iMinSAD[3]) { |
706 |
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; } |
data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; data->cbp[1] = (data->cbp[1]&~8) | (cbp&8); } |
707 |
if (data->temp[3] < data->iMinSAD[4]) { |
if (data->temp[3] < data->iMinSAD[4]) { |
708 |
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; } |
data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); } |
709 |
|
|
710 |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
rd += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
711 |
|
|
712 |
if (bits >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
713 |
|
|
714 |
/* chroma */ |
/* chroma */ |
715 |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
716 |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
717 |
|
|
718 |
/* chroma U */ |
/* chroma U */ |
719 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
720 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
721 |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4); |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4); |
722 |
if (bits >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
723 |
|
|
724 |
/* chroma V */ |
/* chroma V */ |
725 |
ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
726 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
727 |
bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
728 |
|
|
729 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
rd += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
730 |
|
|
731 |
if (bits < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
732 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = rd; |
733 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
734 |
*dir = Direction; |
*dir = Direction; |
735 |
|
*data->cbp = cbp; |
736 |
} |
} |
737 |
} |
} |
738 |
|
|
739 |
static void |
static void |
740 |
CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateRD8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
741 |
{ |
{ |
742 |
|
|
743 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
744 |
int32_t bits; |
int32_t rd; |
745 |
VECTOR * current; |
VECTOR * current; |
746 |
const uint8_t * ptr; |
const uint8_t * ptr; |
747 |
int cbp = 0; |
int cbp = 0; |
758 |
} |
} |
759 |
|
|
760 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
761 |
bits = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5); |
762 |
bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
763 |
|
|
764 |
if (bits < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
765 |
data->temp[0] = cbp; |
*data->cbp = cbp; |
766 |
data->iMinSAD[0] = bits; |
data->iMinSAD[0] = rd; |
767 |
current[0].x = x; current[0].y = y; |
current[0].x = x; current[0].y = y; |
768 |
*dir = Direction; |
*dir = Direction; |
769 |
} |
} |
961 |
} |
} |
962 |
|
|
963 |
static __inline void |
static __inline void |
964 |
SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad) |
ZeroMacroblockP(MACROBLOCK *pMB, const int32_t sad) |
965 |
{ |
{ |
966 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_INTER; |
967 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV; |
968 |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV; |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV; |
969 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
979 |
const uint32_t VopFlags, |
const uint32_t VopFlags, |
980 |
const uint32_t VolFlags, |
const uint32_t VolFlags, |
981 |
const IMAGE * const pCurrent, |
const IMAGE * const pCurrent, |
982 |
const IMAGE * const pRef) |
const IMAGE * const pRef, |
983 |
|
const IMAGE * const vGMC, |
984 |
|
const int coding_type) |
985 |
{ |
{ |
986 |
int mode = MODE_INTER; |
int mode = MODE_INTER; |
987 |
|
int mcsel = 0; |
988 |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
989 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
990 |
|
|
991 |
const int skip_possible = (!(VolFlags & XVID_VOL_GMC)) && (pMB->dquant == 0); |
const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); |
992 |
|
|
993 |
|
pMB->mcsel = 0; |
994 |
|
|
995 |
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { /* normal, fast, SAD-based mode decision */ |
if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) { /* normal, fast, SAD-based mode decision */ |
996 |
int sad; |
int sad; |
997 |
int InterBias = MV16_INTER_BIAS; |
int InterBias = MV16_INTER_BIAS; |
998 |
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
1014 |
sad = 0; |
sad = 0; |
1015 |
} |
} |
1016 |
|
|
1017 |
|
/* mcsel */ |
1018 |
|
if (coding_type == S_VOP) { |
1019 |
|
|
1020 |
|
int32_t iSAD = sad16(Data->Cur, |
1021 |
|
vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); |
1022 |
|
|
1023 |
|
if (Data->chroma) { |
1024 |
|
iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
1025 |
|
iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); |
1026 |
|
} |
1027 |
|
|
1028 |
|
if (iSAD <= sad) { /* mode decision GMC */ |
1029 |
|
mode = MODE_INTER; |
1030 |
|
mcsel = 1; |
1031 |
|
sad = iSAD; |
1032 |
|
} |
1033 |
|
|
1034 |
|
} |
1035 |
|
|
1036 |
/* intra decision */ |
/* intra decision */ |
1037 |
|
|
1038 |
if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */ |
if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */ |
1041 |
if (x != 0) |
if (x != 0) |
1042 |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
1043 |
|
|
1044 |
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? */ |
if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ |
1045 |
if (Data->rrv) InterBias *= 4; |
if (Data->rrv) InterBias *= 4; |
1046 |
|
|
1047 |
if (InterBias < pMB->sad16) { |
if (InterBias < sad) { |
1048 |
int32_t deviation; |
int32_t deviation; |
1049 |
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
if (!Data->rrv) |
1050 |
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
deviation = dev16(Data->Cur, Data->iEdgedWidth); |
1051 |
|
else |
1052 |
|
deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */ |
1053 |
dev16(Data->Cur+16, Data->iEdgedWidth) + |
dev16(Data->Cur+16, Data->iEdgedWidth) + |
1054 |
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
1055 |
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
1057 |
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
1058 |
} |
} |
1059 |
|
|
1060 |
} else { /* BITS */ |
pMB->cbp = 63; |
1061 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; |
1062 |
|
|
1063 |
int bits, intra, i; |
} else { /* Rate-Distortion */ |
1064 |
|
|
1065 |
|
int min_rd, intra_rd, i, cbp, c[2] = {0, 0}; |
1066 |
VECTOR backup[5], *v; |
VECTOR backup[5], *v; |
1067 |
Data->iQuant = iQuant; |
Data->iQuant = iQuant; |
1068 |
|
Data->cbp = c; |
1069 |
|
|
1070 |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
1071 |
for (i = 0; i < 5; i++) { |
for (i = 0; i < 5; i++) { |
1073 |
backup[i] = v[i]; |
backup[i] = v[i]; |
1074 |
} |
} |
1075 |
|
|
1076 |
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
min_rd = findRDinter(Data, pMBs, x, y, pParam, MotionFlags); |
1077 |
if (bits == 0) |
cbp = *Data->cbp; |
1078 |
mode = MODE_INTER; /* quick stop */ |
|
1079 |
else { |
if (coding_type == S_VOP) { |
1080 |
if (inter4v) { |
int gmc_rd; |
1081 |
int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
*Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ |
1082 |
if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } |
gmc_rd = findRDgmc(Data, vGMC, x, y); |
1083 |
|
if (gmc_rd < min_rd) { |
1084 |
|
mcsel = 1; |
1085 |
|
*Data->iMinSAD = min_rd = gmc_rd; |
1086 |
|
mode = MODE_INTER; |
1087 |
|
cbp = *Data->cbp; |
1088 |
|
} |
1089 |
} |
} |
1090 |
|
|
1091 |
intra = CountMBBitsIntra(Data); |
if (inter4v) { |
1092 |
|
int v4_rd; |
1093 |
|
v4_rd = findRDinter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
1094 |
|
if (v4_rd < min_rd) { |
1095 |
|
Data->iMinSAD[0] = min_rd = v4_rd; |
1096 |
|
mode = MODE_INTER4V; |
1097 |
|
cbp = *Data->cbp; |
1098 |
|
} |
1099 |
|
} |
1100 |
|
|
1101 |
if (intra < bits) { *Data->iMinSAD = bits = intra; mode = MODE_INTRA; } |
intra_rd = findRDintra(Data); |
1102 |
|
if (intra_rd < min_rd) { |
1103 |
|
*Data->iMinSAD = min_rd = intra_rd; |
1104 |
|
mode = MODE_INTRA; |
1105 |
} |
} |
1106 |
|
|
1107 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
1108 |
|
pMB->cbp = cbp; |
1109 |
} |
} |
1110 |
|
|
1111 |
if (Data->rrv) { |
if (Data->rrv) { |
1113 |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1114 |
} |
} |
1115 |
|
|
1116 |
if (mode == MODE_INTER) { |
if (mode == MODE_INTER && mcsel == 0) { |
1117 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
|
1118 |
|
|
1119 |
if(Data->qpel) { |
if(Data->qpel) { |
1120 |
pMB->qmvs[0] = pMB->qmvs[1] |
pMB->qmvs[0] = pMB->qmvs[1] |
1126 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1127 |
} |
} |
1128 |
|
|
1129 |
} else if (mode == MODE_INTER4V) |
} else if (mode == MODE_INTER ) { // but mcsel == 1 |
1130 |
pMB->sad16 = Data->iMinSAD[0]; |
|
1131 |
|
pMB->mcsel = 1; |
1132 |
|
if (Data->qpel) { |
1133 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1134 |
|
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; |
1135 |
|
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; |
1136 |
|
} else |
1137 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1138 |
|
|
1139 |
|
} else |
1140 |
|
if (mode == MODE_INTER4V) ; /* anything here? */ |
1141 |
else /* INTRA, NOT_CODED */ |
else /* INTRA, NOT_CODED */ |
1142 |
SkipMacroblockP(pMB, 0); |
ZeroMacroblockP(pMB, 0); |
1143 |
|
|
1144 |
pMB->mode = mode; |
pMB->mode = mode; |
1145 |
} |
} |
1165 |
|
|
1166 |
uint32_t x, y; |
uint32_t x, y; |
1167 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
1168 |
int32_t quant = current->quant, sad00; |
int32_t sad00; |
1169 |
int skip_thresh = INITIAL_SKIP_THRESH * \ |
int skip_thresh = INITIAL_SKIP_THRESH * \ |
1170 |
(current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \ |
(current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \ |
1171 |
(current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1); |
(current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1); |
1172 |
|
|
1173 |
/* some pre-initialized thingies for SearchP */ |
/* some pre-initialized thingies for SearchP */ |
1174 |
int32_t temp[8]; |
int32_t temp[8]; |
1186 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
1187 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
1188 |
Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); |
Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0); |
1189 |
Data.chroma = MotionFlags & XVID_ME_CHROMA16; |
Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP; |
1190 |
Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0; |
Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0; |
1191 |
Data.dctSpace = dct_space; |
Data.dctSpace = dct_space; |
1192 |
Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
1224 |
|
|
1225 |
sad00 = pMB->sad16; |
sad00 = pMB->sad16; |
1226 |
|
|
|
if (pMB->dquant != 0) { |
|
|
quant += DQtab[pMB->dquant]; |
|
|
if (quant > 31) quant = 31; |
|
|
else if (quant < 1) quant = 1; |
|
|
} |
|
|
pMB->quant = quant; |
|
|
|
|
1227 |
/* initial skip decision */ |
/* initial skip decision */ |
1228 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1229 |
if (!(current->vol_flags & XVID_VOL_GMC)) { /* no fast SKIP for S(GMC)-VOPs */ |
if (current->coding_type != S_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
1230 |
if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) |
if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh) |
1231 |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) { |
1232 |
SkipMacroblockP(pMB, sad00); |
ZeroMacroblockP(pMB, sad00); |
1233 |
|
pMB->mode = MODE_NOT_CODED; |
1234 |
continue; |
continue; |
1235 |
} |
} |
1236 |
} |
} |
1237 |
|
|
1238 |
|
if ((current->vop_flags & XVID_VOP_CARTOON) && |
1239 |
|
(sad00 < pMB->quant * 4 * skip_thresh)) { /* favorize (0,0) vector for cartoons */ |
1240 |
|
ZeroMacroblockP(pMB, sad00); |
1241 |
|
continue; |
1242 |
|
} |
1243 |
|
|
1244 |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1245 |
y, MotionFlags, current->vop_flags, current->vol_flags, |
y, MotionFlags, current->vop_flags, current->vol_flags, |
1246 |
&Data, pParam, pMBs, reference->mbs, pMB); |
&Data, pParam, pMBs, reference->mbs, pMB); |
1247 |
|
|
1248 |
ModeDecision(&Data, pMB, pMBs, x, y, pParam, |
ModeDecision(&Data, pMB, pMBs, x, y, pParam, |
1249 |
MotionFlags, current->vop_flags, current->vol_flags, |
MotionFlags, current->vop_flags, current->vol_flags, |
1250 |
pCurrent, pRef); |
pCurrent, pRef, pGMC, current->coding_type); |
1251 |
|
|
1252 |
if (pMB->mode == MODE_INTRA) |
if (pMB->mode == MODE_INTRA) |
1253 |
if (++iIntra > iLimit) return 1; |
if (++iIntra > iLimit) return 1; |
1254 |
} |
} |
1255 |
} |
} |
1256 |
|
|
|
// if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
|
|
// { |
|
|
// current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
|
|
// } |
|
1257 |
return 0; |
return 0; |
1258 |
} |
} |
1259 |
|
|
1338 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1339 |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
1340 |
|
|
1341 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
1342 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); |
1343 |
|
|
1344 |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); |
1345 |
|
|
1372 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
1373 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1374 |
|
|
1375 |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (x | y)) { |
if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) { |
1376 |
threshA = Data->temp[0]; /* that's where we keep this SAD atm */ |
threshA = Data->temp[0]; /* that's where we keep this SAD atm */ |
1377 |
if (threshA < 512) threshA = 512; |
if (threshA < 512) threshA = 512; |
1378 |
else if (threshA > 1024) threshA = 1024; |
else if (threshA > 1024) threshA = 1024; |
1452 |
} |
} |
1453 |
|
|
1454 |
if (Data->qpel) { |
if (Data->qpel) { |
1455 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
1456 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
1457 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1458 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) |
1459 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1471 |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
1472 |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1473 |
|
|
1474 |
if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) { |
if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) { |
1475 |
/* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */ |
/* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */ |
1476 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
1477 |
|
|
1531 |
Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1532 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1533 |
|
|
1534 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, |
1535 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); |
1536 |
|
|
1537 |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
if (!Data->rrv) CheckCandidate = CheckCandidate8; |
1538 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1539 |
|
|
1540 |
if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) { |
if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) { |
1541 |
int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */ |
int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */ |
1542 |
|
|
1543 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1566 |
|
|
1567 |
if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) { |
if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) { |
1568 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1569 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 3, |
1570 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
1571 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1572 |
} |
} |
1573 |
} |
} |
1671 |
|
|
1672 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
1673 |
|
|
1674 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
1675 |
pParam->width, pParam->height, iFcode - Data->qpel, 0, 0); |
pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); |
1676 |
|
|
1677 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
1678 |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
1700 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
1701 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
1702 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1703 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
1704 |
pParam->width, pParam->height, iFcode, 1, 0); |
pParam->width, pParam->height, iFcode, 2, 0); |
1705 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1706 |
} |
} |
1707 |
|
|
1764 |
b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
1765 |
stride); |
stride); |
1766 |
|
|
1767 |
if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */ |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */ |
1768 |
|
|
1769 |
sum += sad8bi(pCur->v + 8*x + 8 * y * stride, |
sum += sad8bi(pCur->v + 8*x + 8 * y * stride, |
1770 |
f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, |
f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, |
1771 |
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
1772 |
stride); |
stride); |
1773 |
|
|
1774 |
if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { |
if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { |
1775 |
pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
1776 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
1777 |
pMB->qmvs[k] = pMB->mvs[k]; |
pMB->qmvs[k] = pMB->mvs[k]; |
1967 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
1968 |
fData->currentMV[0] = fData->currentMV[2]; |
fData->currentMV[0] = fData->currentMV[2]; |
1969 |
|
|
1970 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - fData->qpel, 0, 0); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode - fData->qpel, 1, 0); |
1971 |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - fData->qpel, 0, 0); |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode - fData->qpel, 1, 0); |
1972 |
|
|
1973 |
if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx; |
if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx; |
1974 |
if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx; |
if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx; |
2008 |
if (*fData->iMinSAD > *best_sad + 500) return; |
if (*fData->iMinSAD > *best_sad + 500) return; |
2009 |
CheckCandidate = CheckCandidateInt; |
CheckCandidate = CheckCandidateInt; |
2010 |
fData->qpel_precision = bData.qpel_precision = 1; |
fData->qpel_precision = bData.qpel_precision = 1; |
2011 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0); |
2012 |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 1, 0); |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
2013 |
fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x; |
fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x; |
2014 |
fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; |
fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; |
2015 |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
2084 |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
2085 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2086 |
Data.lambda16 = lambda_vec16[frame->quant]; |
Data.lambda16 = lambda_vec16[frame->quant]; |
2087 |
Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL; |
Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; |
2088 |
Data.rounding = 0; |
Data.rounding = 0; |
2089 |
Data.chroma = frame->motion_flags & XVID_ME_CHROMA8; |
Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
2090 |
Data.temp = temp; |
Data.temp = temp; |
2091 |
|
|
2092 |
Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ |
Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ |
2210 |
Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */ |
Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */ |
2211 |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); /* else median */ |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); /* else median */ |
2212 |
|
|
2213 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
2214 |
pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0); |
pParam->width, pParam->height, Data->iFcode - quarterpel, 1, 0); |
2215 |
|
|
2216 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
2217 |
Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; |
Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; |
2274 |
Data.temp = temp; |
Data.temp = temp; |
2275 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2276 |
|
|
|
|
|
2277 |
if (intraCount != 0) { |
if (intraCount != 0) { |
2278 |
if (intraCount < 10) // we're right after an I frame |
if (intraCount < 10) // we're right after an I frame |
2279 |
IntraThresh += 15* (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15* (intraCount - 10) * (intraCount - 10); |
2308 |
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16, |
2309 |
pParam->edged_width); |
pParam->edged_width); |
2310 |
|
|
2311 |
complexity += dev; |
complexity += MAX(dev, 300); |
2312 |
if (dev + IntraThresh < pMB->sad16) { |
if (dev + IntraThresh < pMB->sad16) { |
2313 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
2314 |
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; |
2318 |
if (dev > 500 && pMB->sad16 < 1000) |
if (dev > 500 && pMB->sad16 < 1000) |
2319 |
sSAD += 1000; |
sSAD += 1000; |
2320 |
|
|
2321 |
sSAD += pMB->sad16; |
sSAD += (dev < 3000) ? pMB->sad16 : pMB->sad16/2; /* blocks with big contrast differences usually have large SAD - while they look very good in b-frames */ |
2322 |
} |
} |
2323 |
} |
} |
2324 |
} |
} |
2326 |
|
|
2327 |
sSAD /= complexity + 4*blocks; |
sSAD /= complexity + 4*blocks; |
2328 |
|
|
2329 |
if (intraCount > 12 && sSAD > INTRA_THRESH2 ) return I_VOP; |
if (intraCount > 80 && sSAD > INTRA_THRESH2 ) return I_VOP; |
2330 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2331 |
emms(); |
emms(); |
2332 |
return B_VOP; |
return B_VOP; |
2336 |
/* functions which perform BITS-based search/bitcount */ |
/* functions which perform BITS-based search/bitcount */ |
2337 |
|
|
2338 |
static int |
static int |
2339 |
CountMBBitsInter(SearchData * const Data, |
findRDinter(SearchData * const Data, |
2340 |
const MACROBLOCK * const pMBs, const int x, const int y, |
const MACROBLOCK * const pMBs, const int x, const int y, |
2341 |
const MBParam * const pParam, |
const MBParam * const pParam, |
2342 |
const uint32_t MotionFlags) |
const uint32_t MotionFlags) |
2344 |
int i, iDirection; |
int i, iDirection; |
2345 |
int32_t bsad[5]; |
int32_t bsad[5]; |
2346 |
|
|
2347 |
CheckCandidate = CheckCandidateBits16; |
CheckCandidate = CheckCandidateRD16; |
2348 |
|
|
2349 |
if (Data->qpel) { |
if (Data->qpel) { |
2350 |
for(i = 0; i < 5; i++) { |
for(i = 0; i < 5; i++) { |
2352 |
Data->currentMV[i].y = Data->currentQMV[i].y/2; |
Data->currentMV[i].y = Data->currentQMV[i].y/2; |
2353 |
} |
} |
2354 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
2355 |
CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data); |
2356 |
|
|
2357 |
if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { /* we have to prepare for halfpixel-precision search */ |
if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */ |
2358 |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; |
2359 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
2360 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv); |
2361 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
2362 |
if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) |
if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) |
2363 |
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
2364 |
} |
} |
2365 |
|
|
2366 |
} else { /* not qpel */ |
} else { /* not qpel */ |
2367 |
|
|
2368 |
CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data); |
2369 |
} |
} |
2370 |
|
|
2371 |
if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
if (MotionFlags&XVID_ME_EXTSEARCH_RD) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
2372 |
|
|
2373 |
if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data); |
if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) SubpelRefine(Data); |
2374 |
|
|
2375 |
if (Data->qpel) { |
if (Data->qpel) { |
2376 |
if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { /* there was halfpel-precision search */ |
if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */ |
2377 |
for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { |
for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { |
2378 |
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */ |
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */ |
2379 |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
2381 |
|
|
2382 |
/* preparing for qpel-precision search */ |
/* preparing for qpel-precision search */ |
2383 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
2384 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
2385 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
2386 |
} |
} |
2387 |
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data); |
if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) SubpelRefine(Data); |
2388 |
} |
} |
2389 |
|
|
2390 |
if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { /* let's check vector equal to prediction */ |
if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */ |
2391 |
VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; |
VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; |
2392 |
if (!(Data->predMV.x == v->x && Data->predMV.y == v->y)) |
if (!(Data->predMV.x == v->x && Data->predMV.y == v->y)) |
2393 |
CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); |
CheckCandidateRD16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data); |
2394 |
} |
} |
2395 |
return Data->iMinSAD[0]; |
return Data->iMinSAD[0]; |
2396 |
} |
} |
2397 |
|
|
2398 |
static int |
static int |
2399 |
CountMBBitsInter4v(const SearchData * const Data, |
findRDinter4v(const SearchData * const Data, |
2400 |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, |
2401 |
const int x, const int y, |
const int x, const int y, |
2402 |
const MBParam * const pParam, const uint32_t MotionFlags, |
const MBParam * const pParam, const uint32_t MotionFlags, |
2410 |
uint8_t * ptr; |
uint8_t * ptr; |
2411 |
|
|
2412 |
memcpy(Data8, Data, sizeof(SearchData)); |
memcpy(Data8, Data, sizeof(SearchData)); |
2413 |
CheckCandidate = CheckCandidateBits8; |
CheckCandidate = CheckCandidateRD8; |
2414 |
|
|
2415 |
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
for (i = 0; i < 4; i++) { /* for all luma blocks */ |
2416 |
|
|
2422 |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2423 |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2424 |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2425 |
|
*Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; // copy corresponding cbp bit |
2426 |
|
|
2427 |
if(Data->qpel) { |
if(Data->qpel) { |
2428 |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
2434 |
Data8->predMV, Data8->iFcode, 0, 0); |
Data8->predMV, Data8->iFcode, 0, 0); |
2435 |
} |
} |
2436 |
|
|
2437 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
2438 |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0); |
pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1, 0); |
2439 |
|
|
2440 |
*Data8->iMinSAD += BITS_MULT*t; |
*Data8->iMinSAD += BITS_MULT*t; |
2441 |
|
|
2444 |
{ |
{ |
2445 |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
2446 |
if (!MVequal (*v, backup[i+1]) ) |
if (!MVequal (*v, backup[i+1]) ) |
2447 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
CheckCandidateRD8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2448 |
} |
} |
2449 |
|
|
2450 |
if (Data8->qpel) { |
if (Data8->qpel) { |
2451 |
if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { /* halfpixel motion search follows */ |
if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
2452 |
int32_t s = *Data8->iMinSAD; |
int32_t s = *Data8->iMinSAD; |
2453 |
Data8->currentMV->x = Data8->currentQMV->x/2; |
Data8->currentMV->x = Data8->currentQMV->x/2; |
2454 |
Data8->currentMV->y = Data8->currentQMV->y/2; |
Data8->currentMV->y = Data8->currentQMV->y/2; |
2455 |
Data8->qpel_precision = 0; |
Data8->qpel_precision = 0; |
2456 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
2457 |
pParam->width, pParam->height, Data8->iFcode - 1, 0, 0); |
pParam->width, pParam->height, Data8->iFcode - 1, 1, 0); |
2458 |
|
|
2459 |
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) |
2460 |
CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); |
CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8); |
2461 |
|
|
2462 |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) |
2463 |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2464 |
|
|
2465 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
2466 |
SubpelRefine(Data8); |
SubpelRefine(Data8); |
2467 |
|
|
2468 |
if(s > *Data8->iMinSAD) { /* we have found a better match */ |
if(s > *Data8->iMinSAD) { /* we have found a better match */ |
2471 |
} |
} |
2472 |
|
|
2473 |
Data8->qpel_precision = 1; |
Data8->qpel_precision = 1; |
2474 |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8, |
get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, |
2475 |
pParam->width, pParam->height, Data8->iFcode, 1, 0); |
pParam->width, pParam->height, Data8->iFcode, 2, 0); |
2476 |
|
|
2477 |
} |
} |
2478 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) SubpelRefine(Data8); |
2479 |
|
|
2480 |
} else { /* not qpel */ |
} else { /* not qpel */ |
2481 |
|
|
2482 |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) /* extsearch */ |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) /* extsearch */ |
2483 |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2484 |
|
|
2485 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) |
if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) |
2486 |
SubpelRefine(Data8); /* halfpel refinement */ |
SubpelRefine(Data8); /* halfpel refinement */ |
2487 |
} |
} |
2488 |
|
|
2489 |
/* checking vector equal to predicion */ |
/* checking vector equal to predicion */ |
2490 |
if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) { |
if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_RD) { |
2491 |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
2492 |
if (!MVequal(*v, Data8->predMV)) |
if (!MVequal(*v, Data8->predMV)) |
2493 |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
2494 |
} |
} |
2495 |
|
|
2496 |
bits += *Data8->iMinSAD; |
bits += *Data8->iMinSAD; |
2511 |
} |
} |
2512 |
pMB->mvs[i] = *Data8->currentMV; |
pMB->mvs[i] = *Data8->currentMV; |
2513 |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
2514 |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
if (Data8->cbp[0]) cbp |= 1 << (5 - i); |
2515 |
|
|
2516 |
} /* /for all luma blocks */ |
} /* end - for all luma blocks */ |
2517 |
|
|
2518 |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2519 |
|
|
2535 |
|
|
2536 |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
2537 |
|
|
2538 |
|
*Data->cbp = cbp; |
2539 |
return bits; |
return bits; |
2540 |
} |
} |
2541 |
|
|
2542 |
static int |
static int |
2543 |
CountMBBitsIntra(const SearchData * const Data) |
findRDintra(const SearchData * const Data) |
2544 |
{ |
{ |
2545 |
int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ |
2546 |
int cbp = 0, i, dc = 0; |
int cbp = 0, i, dc = 0; |
2571 |
return bits; |
return bits; |
2572 |
} |
} |
2573 |
|
|
2574 |
|
static int |
2575 |
|
findRDgmc(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y) |
2576 |
|
{ |
2577 |
|
int bits = BITS_MULT*1; /* this one is mcsel */ |
2578 |
|
int cbp = 0, i; |
2579 |
|
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
2580 |
|
|
2581 |
|
for(i = 0; i < 4; i++) { |
2582 |
|
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2583 |
|
transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth); |
2584 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i); |
2585 |
|
if (bits >= Data->iMinSAD[0]) return bits; |
2586 |
|
} |
2587 |
|
|
2588 |
|
bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; |
2589 |
|
|
2590 |
|
/*chroma U */ |
2591 |
|
transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
2592 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4); |
2593 |
|
|
2594 |
|
if (bits >= Data->iMinSAD[0]) return bits; |
2595 |
|
|
2596 |
|
/* chroma V */ |
2597 |
|
transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); |
2598 |
|
bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5); |
2599 |
|
|
2600 |
|
bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
2601 |
|
|
2602 |
|
*Data->cbp = cbp; |
2603 |
|
|
2604 |
|
return bits; |
2605 |
|
} |
2606 |
|
|
2607 |
|
|
2608 |
|
|
2621 |
{ |
{ |
2622 |
|
|
2623 |
int i=0; |
int i=0; |
|
// VECTOR pmv[3]; |
|
2624 |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
2625 |
|
|
2626 |
Data->iMinSAD[0] = MV_MAX_ERROR; |
Data->iMinSAD[0] = MV_MAX_ERROR; |
2627 |
|
|
2628 |
//median is only used as prediction. it doesn't have to be real |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
if (x == 0 && y == 0) |
|
|
Data->predMV.x = Data->predMV.y = 0; |
|
|
else |
|
|
if (x == 0) //left macroblock does not have any vector now |
|
|
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
|
|
else if (y == 0) // top macroblock doesn't have it's vector |
|
|
Data->predMV = (pMB-1)->mvs[0]; // left instead of median |
|
|
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
|
2629 |
|
|
2630 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
2631 |
pParam->width, pParam->height, Data->iFcode - ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, 0); |
pParam->width, pParam->height, 16, 1, 0); |
2632 |
|
|
2633 |
Data->Cur = pCur + 16*(x + y * pParam->edged_width); |
Data->Cur = pCur + 16*(x + y * pParam->edged_width); |
2634 |
Data->RefP[0] = pRef + 16*(x + y * pParam->edged_width); |
Data->RefP[0] = pRef + 16*(x + y * pParam->edged_width); |
2642 |
if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) ) |
if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) ) |
2643 |
CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data); |
CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data); |
2644 |
|
|
2645 |
if (Data->iMinSAD[0] > 256 /*4 * MAX_SAD00_FOR_SKIP*/) // diamond only if needed |
AdvDiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
|
DiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); |
|
2646 |
|
|
2647 |
SubpelRefine(Data); |
SubpelRefine(Data); |
2648 |
|
|
2652 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
2653 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
2654 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
2655 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
2656 |
pParam->width, pParam->height, iFcode, 1, 0); |
pParam->width, pParam->height, iFcode, 2, 0); |
2657 |
SubpelRefine(Data); |
SubpelRefine(Data); |
2658 |
} |
} |
2659 |
*/ |
*/ |
2660 |
|
|
2661 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
2662 |
pMB->sad16 = Data->iMinSAD[0]; |
pMB->sad16 = Data->iMinSAD[0]; |
2663 |
pMB->sad16 += d_mv_bits(pMB->mvs[0].x, pMB->mvs[0].y, Data->predMV, Data->iFcode, 0, 0); |
pMB->mode = MODE_INTER; |
2664 |
|
pMB->sad16 += 10*d_mv_bits(pMB->mvs[0].x, pMB->mvs[0].y, Data->predMV, Data->iFcode, 0, 0); |
2665 |
return; |
return; |
2666 |
} |
} |
2667 |
|
|
2684 |
memset(&Data, 0, sizeof(SearchData)); |
memset(&Data, 0, sizeof(SearchData)); |
2685 |
|
|
2686 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
|
Data.qpel = ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0); |
|
|
Data.qpel_precision = 0; |
|
2687 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
|
Data.chroma = current->motion_flags & XVID_ME_CHROMA16; |
|
|
Data.rrv = current->vop_flags & XVID_VOL_REDUCED_ENABLE; |
|
2688 |
|
|
2689 |
Data.currentMV = ¤tMV[0]; |
Data.currentMV = ¤tMV[0]; |
2690 |
Data.iMinSAD = &iMinSAD[0]; |
Data.iMinSAD = &iMinSAD[0]; |
2691 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
2692 |
Data.temp = temp; |
Data.temp = temp; |
|
Data.RefP[0] = pReference->y; |
|
|
Data.RefP[1] = pRefV->y; |
|
|
Data.RefP[2] = pRefH->y; |
|
|
Data.RefP[3] = pRefHV->y; |
|
2693 |
|
|
2694 |
CheckCandidate = CheckCandidate16I; |
CheckCandidate = CheckCandidate16I; |
2695 |
|
|
2715 |
const IMAGE * const pRefHV) |
const IMAGE * const pRefHV) |
2716 |
{ |
{ |
2717 |
|
|
2718 |
const unsigned int deltax=8; // upper bound for difference between a MV and it's neighbour MVs |
const int deltax=8; // upper bound for difference between a MV and it's neighbour MVs |
2719 |
const unsigned int deltay=8; |
const int deltay=8; |
2720 |
const unsigned int gradx=512; // lower bound for gradient in MB (ignore "flat" blocks) |
const unsigned int gradx=512; // lower bound for gradient in MB (ignore "flat" blocks) |
2721 |
const unsigned int grady=512; |
const unsigned int grady=512; |
2722 |
|
|
2879 |
*/ |
*/ |
2880 |
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; |
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; |
2881 |
|
|
2882 |
if (!(current->motion_flags & XVID_GME_REFINE)) |
if (!(current->motion_flags & XVID_ME_GME_REFINE)) |
2883 |
return gmc; |
return gmc; |
2884 |
|
|
2885 |
for (my = 1; my < (uint32_t)MBh-1; my++) /* ignore boundary blocks */ |
for (my = 1; my < (uint32_t)MBh-1; my++) /* ignore boundary blocks */ |