105 |
return bits; |
return bits; |
106 |
} |
} |
107 |
|
|
108 |
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, |
109 |
|
const SearchData * const data) |
110 |
{ |
{ |
111 |
int sad; |
int sad; |
112 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
114 |
* f_refv = data->RefQ + 8, |
* f_refv = data->RefQ + 8, |
115 |
* b_refu = data->RefQ + 16, |
* b_refu = data->RefQ + 16, |
116 |
* b_refv = data->RefQ + 24; |
* b_refv = data->RefQ + 24; |
117 |
|
int offset = (fx>>1) + (fy>>1)*stride; |
118 |
|
|
119 |
switch (((fx & 1) << 1) | (fy & 1)) { |
switch (((fx & 1) << 1) | (fy & 1)) { |
120 |
case 0: |
case 0: |
121 |
fx = fx / 2; fy = fy / 2; |
f_refu = (uint8_t*)data->RefP[4] + offset; |
122 |
f_refu = (uint8_t*)data->RefCU + fy * stride + fx, stride; |
f_refv = (uint8_t*)data->RefP[5] + offset; |
|
f_refv = (uint8_t*)data->RefCV + fy * stride + fx, stride; |
|
123 |
break; |
break; |
124 |
case 1: |
case 1: |
125 |
fx = fx / 2; fy = (fy - 1) / 2; |
interpolate8x8_halfpel_v(f_refu, data->RefP[4] + offset, stride, data->rounding); |
126 |
interpolate8x8_halfpel_v(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
interpolate8x8_halfpel_v(f_refv, data->RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_v(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
|
127 |
break; |
break; |
128 |
case 2: |
case 2: |
129 |
fx = (fx - 1) / 2; fy = fy / 2; |
interpolate8x8_halfpel_h(f_refu, data->RefP[4] + offset, stride, data->rounding); |
130 |
interpolate8x8_halfpel_h(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
interpolate8x8_halfpel_h(f_refv, data->RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_h(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
|
131 |
break; |
break; |
132 |
default: |
default: |
133 |
fx = (fx - 1) / 2; fy = (fy - 1) / 2; |
interpolate8x8_halfpel_hv(f_refu, data->RefP[4] + offset, stride, data->rounding); |
134 |
interpolate8x8_halfpel_hv(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding); |
interpolate8x8_halfpel_hv(f_refv, data->RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_hv(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding); |
|
135 |
break; |
break; |
136 |
} |
} |
137 |
|
|
138 |
|
offset = (bx>>1) + (by>>1)*stride; |
139 |
switch (((bx & 1) << 1) | (by & 1)) { |
switch (((bx & 1) << 1) | (by & 1)) { |
140 |
case 0: |
case 0: |
141 |
bx = bx / 2; by = by / 2; |
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
142 |
b_refu = (uint8_t*)data->b_RefCU + by * stride + bx, stride; |
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
|
b_refv = (uint8_t*)data->b_RefCV + by * stride + bx, stride; |
|
143 |
break; |
break; |
144 |
case 1: |
case 1: |
145 |
bx = bx / 2; by = (by - 1) / 2; |
interpolate8x8_halfpel_v(b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
146 |
interpolate8x8_halfpel_v(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
interpolate8x8_halfpel_v(b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_v(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
|
147 |
break; |
break; |
148 |
case 2: |
case 2: |
149 |
bx = (bx - 1) / 2; by = by / 2; |
interpolate8x8_halfpel_h(b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
150 |
interpolate8x8_halfpel_h(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
interpolate8x8_halfpel_h(b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_h(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
|
151 |
break; |
break; |
152 |
default: |
default: |
153 |
bx = (bx - 1) / 2; by = (by - 1) / 2; |
interpolate8x8_halfpel_hv(b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
154 |
interpolate8x8_halfpel_hv(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding); |
interpolate8x8_halfpel_hv(b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_hv(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding); |
|
155 |
break; |
break; |
156 |
} |
} |
157 |
|
|
161 |
return sad; |
return sad; |
162 |
} |
} |
163 |
|
|
|
|
|
164 |
static int32_t |
static int32_t |
165 |
ChromaSAD(int dx, int dy, const SearchData * const data) |
ChromaSAD(const int dx, const int dy, const SearchData * const data) |
166 |
{ |
{ |
167 |
int sad; |
int sad; |
168 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
169 |
|
int offset = (dx>>1) + (dy>>1)*stride; |
170 |
|
|
171 |
if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently |
if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently |
172 |
data->temp[5] = dx; data->temp[6] = dy; // backup |
data->temp[5] = dx; data->temp[6] = dy; // backup |
173 |
|
|
174 |
switch (((dx & 1) << 1) | (dy & 1)) { |
switch (((dx & 1) << 1) | (dy & 1)) { |
175 |
case 0: |
case 0: |
176 |
dx = dx / 2; dy = dy / 2; |
sad = sad8(data->CurU, data->RefP[4] + offset, stride); |
177 |
sad = sad8(data->CurU, data->RefCU + dy * stride + dx, stride); |
sad += sad8(data->CurV, data->RefP[5] + offset, stride); |
|
sad += sad8(data->CurV, data->RefCV + dy * stride + dx, stride); |
|
178 |
break; |
break; |
179 |
case 1: |
case 1: |
180 |
dx = dx / 2; dy = (dy - 1) / 2; |
sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + stride, stride); |
181 |
sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + (dy+1) * stride + dx, stride); |
sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + stride, stride); |
|
sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + (dy+1) * stride + dx, stride); |
|
182 |
break; |
break; |
183 |
case 2: |
case 2: |
184 |
dx = (dx - 1) / 2; dy = dy / 2; |
sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + 1, stride); |
185 |
sad = sad8bi(data->CurU, data->RefCU + dy * stride + dx, data->RefCU + dy * stride + dx+1, stride); |
sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + 1, stride); |
|
sad += sad8bi(data->CurV, data->RefCV + dy * stride + dx, data->RefCV + dy * stride + dx+1, stride); |
|
186 |
break; |
break; |
187 |
default: |
default: |
188 |
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding); |
|
interpolate8x8_halfpel_hv(data->RefQ, data->RefCU + dy * stride + dx, stride, data->rounding); |
|
189 |
sad = sad8(data->CurU, data->RefQ, stride); |
sad = sad8(data->CurU, data->RefQ, stride); |
190 |
|
|
191 |
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); |
192 |
sad += sad8(data->CurV, data->RefQ, stride); |
sad += sad8(data->CurV, data->RefQ, stride); |
193 |
break; |
break; |
194 |
} |
} |
200 |
GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) |
GetReferenceB(const int x, const int y, const uint32_t dir, const SearchData * const data) |
201 |
{ |
{ |
202 |
// dir : 0 = forward, 1 = backward |
// dir : 0 = forward, 1 = backward |
203 |
switch ( (dir << 2) | ((x&1)<<1) | (y&1) ) { |
const uint8_t *const *const direction = ( dir == 0 ? data->RefP : data->b_RefP ); |
204 |
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
const int picture = ((x&1)<<1) | (y&1); |
205 |
case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; |
206 |
case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
return direction[picture] + offset; |
|
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); |
|
|
} |
|
207 |
} |
} |
208 |
|
|
209 |
// this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate |
// this is a simpler copy of GetReferenceB, but as it's __inline anyway, we can keep the two separate |
210 |
static __inline const uint8_t * |
static __inline const uint8_t * |
211 |
GetReference(const int x, const int y, const SearchData * const data) |
GetReference(const int x, const int y, const SearchData * const data) |
212 |
{ |
{ |
213 |
switch ( ((x&1)<<1) | (y&1) ) { |
const int picture = ((x&1)<<1) | (y&1); |
214 |
case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth); |
const int offset = (x>>1) + (y>>1)*data->iEdgedWidth; |
215 |
case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
return data->RefP[picture] + offset; |
|
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 |
|
|
} |
|
216 |
} |
} |
217 |
|
|
218 |
static uint8_t * |
static uint8_t * |
351 |
data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; } |
data->iMinSAD[3] = data->temp[3]; current[3].x = x; current[3].y = y; } |
352 |
if (data->temp[4] < data->iMinSAD[4]) { |
if (data->temp[4] < data->iMinSAD[4]) { |
353 |
data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; } |
data->iMinSAD[4] = data->temp[4]; current[4].x = x; current[4].y = y; } |
|
|
|
354 |
} |
} |
355 |
|
|
356 |
static void |
static void |
383 |
} |
} |
384 |
} |
} |
385 |
|
|
|
|
|
386 |
static void |
static void |
387 |
CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
388 |
{ |
{ |
463 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
464 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
465 |
|
|
466 |
sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*(data->iEdgedWidth), |
467 |
data->iEdgedWidth, data->temp+1); |
data->iEdgedWidth, data->temp+1); |
468 |
|
|
469 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
646 |
{ |
{ |
647 |
|
|
648 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
649 |
int32_t bits = 0, sum; |
int32_t bits = 0; |
650 |
VECTOR * current; |
VECTOR * current; |
651 |
const uint8_t * ptr; |
const uint8_t * ptr; |
652 |
int i, cbp = 0, t, xc, yc; |
int i, cbp = 0, t, xc, yc; |
667 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
668 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
669 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
670 |
fdct(in); |
bits += data->temp[i] = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, i, 0); |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
|
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
cbp |= 1 << (5 - i); |
|
|
bits += data->temp[i] = CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
} else data->temp[i] = 0; |
|
671 |
} |
} |
672 |
|
|
673 |
bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
674 |
|
|
675 |
if (bits < data->iMinSAD[0]) { // there is still a chance, adding chroma |
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
676 |
|
|
677 |
|
if (bits >= data->iMinSAD[0]) return; |
678 |
|
|
679 |
|
//chroma |
680 |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
xc = (xc >> 1) + roundtab_79[xc & 0x3]; |
681 |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
yc = (yc >> 1) + roundtab_79[yc & 0x3]; |
682 |
|
|
683 |
//chroma U |
//chroma U |
684 |
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); |
685 |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2); |
686 |
fdct(in); |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 4, 0); |
687 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
if (bits >= data->iMinSAD[0]) return; |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
cbp |= 1 << (5 - 4); |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
} |
|
688 |
|
|
|
if (bits < data->iMinSAD[0]) { |
|
689 |
//chroma V |
//chroma V |
690 |
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); |
691 |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2); |
692 |
fdct(in); |
bits += Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5, 0); |
|
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
|
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
cbp |= 1 << (5 - 5); |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
} |
|
|
} |
|
|
} |
|
693 |
|
|
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
|
694 |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; |
695 |
|
|
696 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
714 |
{ |
{ |
715 |
|
|
716 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
717 |
int32_t sum, bits; |
int32_t bits; |
718 |
VECTOR * current; |
VECTOR * current; |
719 |
const uint8_t * ptr; |
const uint8_t * ptr; |
720 |
int cbp; |
int cbp = 0; |
721 |
|
|
722 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
723 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
731 |
} |
} |
732 |
|
|
733 |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); |
734 |
fdct(in); |
bits = Block_CalcBits(coeff, in, data->iQuant, data->quant_type, &cbp, 5, 0); |
735 |
if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16); |
bits += d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
|
else sum = quant4_inter(coeff, in, data->lambda16); |
|
|
if (sum > 0) { |
|
|
bits = CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp = 1; |
|
|
} else cbp = bits = 0; |
|
|
|
|
|
bits += sum = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
|
736 |
|
|
737 |
if (bits < data->iMinSAD[0]) { |
if (bits < data->iMinSAD[0]) { |
738 |
data->temp[0] = cbp; |
data->temp[0] = cbp; |
942 |
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; |
943 |
} |
} |
944 |
|
|
945 |
|
static __inline void |
946 |
|
ModeDecision(SearchData * const Data, |
947 |
|
MACROBLOCK * const pMB, |
948 |
|
const MACROBLOCK * const pMBs, |
949 |
|
const int x, const int y, |
950 |
|
const MBParam * const pParam, |
951 |
|
const uint32_t MotionFlags, |
952 |
|
const uint32_t VopFlags, |
953 |
|
const uint32_t VolFlags, |
954 |
|
const IMAGE * const pCurrent, |
955 |
|
const IMAGE * const pRef) |
956 |
|
{ |
957 |
|
int mode = MODE_INTER; |
958 |
|
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
959 |
|
const uint32_t iQuant = pMB->quant; |
960 |
|
|
961 |
|
const int skip_possible = (!(VolFlags & XVID_VOL_GMC)) && (pMB->dquant == 0); |
962 |
|
|
963 |
|
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision |
964 |
|
int sad; |
965 |
|
int InterBias = MV16_INTER_BIAS; |
966 |
|
if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
967 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { |
968 |
|
mode = MODE_INTER; |
969 |
|
sad = Data->iMinSAD[0]; |
970 |
|
} else { |
971 |
|
mode = MODE_INTER4V; |
972 |
|
sad = Data->iMinSAD[1] + Data->iMinSAD[2] + |
973 |
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; |
974 |
|
Data->iMinSAD[0] = sad; |
975 |
|
} |
976 |
|
|
977 |
|
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
978 |
|
if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) |
979 |
|
if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) |
980 |
|
if (Data->chroma || SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant, Data->rrv)) { |
981 |
|
mode = MODE_NOT_CODED; |
982 |
|
sad = 0; |
983 |
|
} |
984 |
|
|
985 |
|
/* intra decision */ |
986 |
|
|
987 |
|
if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work |
988 |
|
if (y != 0) |
989 |
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
990 |
|
if (x != 0) |
991 |
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
992 |
|
|
993 |
|
if (Data->chroma) InterBias += 50; // dev8(chroma) ??? |
994 |
|
if (Data->rrv) InterBias *= 4; |
995 |
|
|
996 |
|
if (InterBias < pMB->sad16) { |
997 |
|
int32_t deviation; |
998 |
|
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
999 |
|
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
1000 |
|
dev16(Data->Cur+16, Data->iEdgedWidth) + |
1001 |
|
dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) + |
1002 |
|
dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth); |
1003 |
|
|
1004 |
|
if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
1005 |
|
} |
1006 |
|
|
1007 |
|
} else { // BITS |
1008 |
|
|
1009 |
|
int bits, intra, i; |
1010 |
|
VECTOR backup[5], *v; |
1011 |
|
Data->iQuant = iQuant; |
1012 |
|
|
1013 |
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
1014 |
|
for (i = 0; i < 5; i++) { |
1015 |
|
Data->iMinSAD[i] = 256*4096; |
1016 |
|
backup[i] = v[i]; |
1017 |
|
} |
1018 |
|
|
1019 |
|
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
1020 |
|
if (bits == 0) |
1021 |
|
mode = MODE_INTER; // quick stop |
1022 |
|
else { |
1023 |
|
if (inter4v) { |
1024 |
|
int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); |
1025 |
|
if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; } |
1026 |
|
} |
1027 |
|
|
1028 |
|
intra = CountMBBitsIntra(Data); |
1029 |
|
|
1030 |
|
if (intra < bits) { *Data->iMinSAD = bits = intra; mode = MODE_INTRA; } |
1031 |
|
} |
1032 |
|
} |
1033 |
|
|
1034 |
|
if (Data->rrv) { |
1035 |
|
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
1036 |
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1037 |
|
} |
1038 |
|
|
1039 |
|
if (mode == MODE_INTER) { |
1040 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1041 |
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
1042 |
|
|
1043 |
|
if(Data->qpel) { |
1044 |
|
pMB->qmvs[0] = pMB->qmvs[1] |
1045 |
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
1046 |
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
1047 |
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
1048 |
|
} else { |
1049 |
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1050 |
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1051 |
|
} |
1052 |
|
|
1053 |
|
} else if (mode == MODE_INTER4V) |
1054 |
|
pMB->sad16 = Data->iMinSAD[0]; |
1055 |
|
else // INTRA, NOT_CODED |
1056 |
|
SkipMacroblockP(pMB, 0); |
1057 |
|
|
1058 |
|
pMB->mode = mode; |
1059 |
|
} |
1060 |
|
|
1061 |
bool |
bool |
1062 |
MotionEstimation(MBParam * const pParam, |
MotionEstimation(MBParam * const pParam, |
1063 |
FRAMEINFO * const current, |
FRAMEINFO * const current, |
1103 |
Data.chroma = MotionFlags & XVID_ME_CHROMA16; |
Data.chroma = MotionFlags & XVID_ME_CHROMA16; |
1104 |
Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0); |
Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0); |
1105 |
Data.dctSpace = dct_space; |
Data.dctSpace = dct_space; |
1106 |
|
Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
1107 |
|
|
1108 |
if ((current->vop_flags & XVID_VOP_REDUCED)) { |
if ((current->vop_flags & XVID_VOP_REDUCED)) { |
1109 |
mb_width = (pParam->width + 31) / 32; |
mb_width = (pParam->width + 31) / 32; |
1143 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
1144 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
1145 |
} |
} |
1146 |
|
pMB->quant = quant; |
|
pMB->quant = current->quant; |
|
1147 |
|
|
1148 |
//initial skip decision |
//initial skip decision |
1149 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
1156 |
} |
} |
1157 |
|
|
1158 |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1159 |
y, MotionFlags, current->vol_flags, pMB->quant, |
y, MotionFlags, current->vop_flags, current->vol_flags, |
1160 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, pMB); |
1161 |
current->vop_flags & XVID_VOP_INTER4V, pMB); |
|
1162 |
|
ModeDecision(&Data, pMB, pMBs, x, y, pParam, |
1163 |
|
MotionFlags, current->vop_flags, current->vol_flags, |
1164 |
|
pCurrent, pRef); |
1165 |
|
|
|
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
|
|
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); |
|
|
} |
|
|
} |
|
1166 |
if (pMB->mode == MODE_INTRA) |
if (pMB->mode == MODE_INTRA) |
1167 |
if (++iIntra > iLimit) return 1; |
if (++iIntra > iLimit) return 1; |
1168 |
} |
} |
1169 |
} |
} |
1170 |
|
|
1171 |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1172 |
|
{ |
1173 |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV); |
1174 |
|
} |
1175 |
return 0; |
return 0; |
1176 |
} |
} |
1177 |
|
|
1235 |
} |
} |
1236 |
} |
} |
1237 |
|
|
|
static int |
|
|
ModeDecision(const uint32_t iQuant, SearchData * const Data, |
|
|
int inter4v, |
|
|
MACROBLOCK * const pMB, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const int x, const int y, |
|
|
const MBParam * const pParam, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t VopFlags) |
|
|
{ |
|
|
|
|
|
int mode = MODE_INTER; |
|
|
|
|
|
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 = MODE_INTER; |
|
|
sad = Data->iMinSAD[0]; |
|
|
} else { |
|
|
mode = MODE_INTER4V; |
|
|
sad = Data->iMinSAD[1] + Data->iMinSAD[2] + |
|
|
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; |
|
|
Data->iMinSAD[0] = sad; |
|
|
} |
|
|
|
|
|
/* intra decision */ |
|
|
|
|
|
if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work |
|
|
if (y != 0) |
|
|
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
|
|
if (x != 0) |
|
|
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
|
|
|
|
|
if (Data->chroma) InterBias += 50; // to compensate bigger SAD |
|
|
if (Data->rrv) InterBias *= 4; |
|
|
|
|
|
if (InterBias < pMB->sad16) { |
|
|
int32_t deviation; |
|
|
if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth); |
|
|
else deviation = dev16(Data->Cur, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur+8, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) + |
|
|
dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth); |
|
|
|
|
|
if (deviation < (sad - InterBias)) return MODE_INTRA; |
|
|
} |
|
|
return mode; |
|
|
|
|
|
} else { |
|
|
|
|
|
int bits, intra, i; |
|
|
VECTOR backup[5], *v; |
|
|
Data->lambda16 = iQuant; |
|
|
Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0; |
|
|
|
|
|
v = Data->qpel ? Data->currentQMV : Data->currentMV; |
|
|
for (i = 0; i < 5; i++) { |
|
|
Data->iMinSAD[i] = 256*4096; |
|
|
backup[i] = v[i]; |
|
|
} |
|
|
|
|
|
bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags); |
|
|
if (bits == 0) return MODE_INTER; // quick stop |
|
|
|
|
|
if (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; } |
|
|
} |
|
|
|
|
|
|
|
|
intra = CountMBBitsIntra(Data); |
|
|
|
|
|
if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; } |
|
|
|
|
|
return mode; |
|
|
} |
|
|
} |
|
|
|
|
1238 |
static void |
static void |
1239 |
SearchP(const IMAGE * const pRef, |
SearchP(const IMAGE * const pRef, |
1240 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1245 |
const int y, |
const int y, |
1246 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
1247 |
const uint32_t VopFlags, |
const uint32_t VopFlags, |
1248 |
const uint32_t iQuant, |
const uint32_t VolFlags, |
1249 |
SearchData * const Data, |
SearchData * const Data, |
1250 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1251 |
const MACROBLOCK * const pMBs, |
const MACROBLOCK * const pMBs, |
1252 |
const MACROBLOCK * const prevMBs, |
const MACROBLOCK * const prevMBs, |
|
int inter4v, |
|
1253 |
MACROBLOCK * const pMB) |
MACROBLOCK * const pMB) |
1254 |
{ |
{ |
1255 |
|
|
1256 |
int i, iDirection = 255, mask, threshA; |
int i, iDirection = 255, mask, threshA; |
1257 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1258 |
|
int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); |
1259 |
|
|
1260 |
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, 16, |
1261 |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
1268 |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1269 |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1270 |
|
|
1271 |
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16*i; |
1272 |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16*i; |
1273 |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16*i; |
1274 |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16*i; |
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16*i; |
1275 |
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1276 |
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
1277 |
|
|
1278 |
Data->lambda16 = lambda_vec16[iQuant]; |
Data->lambda16 = lambda_vec16[pMB->quant]; |
1279 |
Data->lambda8 = lambda_vec8[iQuant]; |
Data->lambda8 = lambda_vec8[pMB->quant]; |
1280 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1281 |
|
|
|
if (pMB->dquant != 0) inter4v = 0; |
|
|
|
|
1282 |
memset(Data->currentMV, 0, 5*sizeof(VECTOR)); |
memset(Data->currentMV, 0, 5*sizeof(VECTOR)); |
1283 |
|
|
1284 |
if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1316 |
|
|
1317 |
if ((Data->iMinSAD[0] <= threshA) || |
if ((Data->iMinSAD[0] <= threshA) || |
1318 |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
(MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) && |
1319 |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) { |
(Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) |
1320 |
if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) inter4v = 0; } |
inter4v = 0; |
1321 |
else { |
else { |
1322 |
|
|
1323 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1363 |
} |
} |
1364 |
|
|
1365 |
if (MotionFlags & XVID_ME_HALFPELREFINE16) |
if (MotionFlags & XVID_ME_HALFPELREFINE16) |
|
if ((!(MotionFlags & XVID_ME_HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant) |
|
1366 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1367 |
|
|
1368 |
for(i = 0; i < 5; i++) { |
for(i = 0; i < 5; i++) { |
1374 |
|
|
1375 |
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, 16, |
1376 |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
|
|
|
|
if ((!(MotionFlags & XVID_ME_QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) { |
|
1377 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1378 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1379 |
} |
} |
|
} |
|
1380 |
|
|
1381 |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0; |
if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)pMB->quant * 30)) |
1382 |
|
inter4v = 0; |
|
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 |
|
1383 |
|
|
1384 |
|
if (inter4v) { |
1385 |
SearchData Data8; |
SearchData Data8; |
1386 |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data |
1387 |
|
|
1393 |
if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) { |
if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) { |
1394 |
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it |
// chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it |
1395 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
|
const int div = 1 + Data->qpel; |
|
|
const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs; |
|
1396 |
|
|
1397 |
for (i = 0; i < 4; i++) { |
if (Data->qpel) |
1398 |
sumx += mv[i].x / div; |
for (i = 1; i < 5; i++) { |
1399 |
sumy += mv[i].y / div; |
sumx += Data->currentQMV[i].x/2; |
1400 |
|
sumy += Data->currentQMV[i].y/2; |
1401 |
|
} |
1402 |
|
else |
1403 |
|
for (i = 1; i < 5; i++) { |
1404 |
|
sumx += Data->currentMV[i].x; |
1405 |
|
sumy += Data->currentMV[i].y; |
1406 |
} |
} |
1407 |
|
|
1408 |
Data->iMinSAD[1] += ChromaSAD( (sumx >> 3) + roundtab_76[sumx & 0xf], |
Data->iMinSAD[1] += ChromaSAD( (sumx >> 3) + roundtab_76[sumx & 0xf], |
1409 |
(sumy >> 3) + roundtab_76[sumy & 0xf], Data); |
(sumy >> 3) + roundtab_76[sumy & 0xf], Data); |
1410 |
} |
} |
1411 |
} |
} else Data->iMinSAD[1] = 4096*256; |
|
|
|
|
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); |
|
|
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
|
|
} |
|
|
|
|
|
if (inter4v == MODE_INTER) { |
|
|
pMB->mode = MODE_INTER; |
|
|
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]; |
|
|
|
|
|
if(Data->qpel) { |
|
|
pMB->qmvs[0] = pMB->qmvs[1] |
|
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
|
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
|
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
|
|
} else { |
|
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
|
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
|
|
} |
|
|
|
|
|
} else if (inter4v == MODE_INTER4V) { |
|
|
pMB->mode = MODE_INTER4V; |
|
|
pMB->sad16 = Data->iMinSAD[0]; |
|
|
} else { // INTRA mode |
|
|
SkipMacroblockP(pMB, 0); // not skip, but similar enough |
|
|
pMB->mode = MODE_INTRA; |
|
|
} |
|
|
|
|
1412 |
} |
} |
1413 |
|
|
1414 |
static void |
static void |
1439 |
*(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; |
*(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10; |
1440 |
|
|
1441 |
if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { |
if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) { |
|
if (Data->rrv) i = 2; else i = 1; |
|
1442 |
|
|
1443 |
Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
if (Data->rrv) i = 16; else i = 8; |
|
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)); |
|
1444 |
|
|
1445 |
Data->Cur = OldData->Cur + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1)); |
Data->RefP[0] = OldData->RefP[0] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1446 |
|
Data->RefP[1] = OldData->RefP[1] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1447 |
|
Data->RefP[2] = OldData->RefP[2] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1448 |
|
Data->RefP[3] = OldData->RefP[3] + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1449 |
|
|
1450 |
|
Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1)); |
1451 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1452 |
|
|
1453 |
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, 8, |
1581 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1582 |
Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache |
Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache |
1583 |
|
|
1584 |
Data->Ref = pRef->y + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
1585 |
Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; |
1586 |
Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; |
1587 |
Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16; |
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
1588 |
Data->RefCU = pRef->u + (x + y * Data->iEdgedWidth/2) * 8; |
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1589 |
Data->RefCV = pRef->v + (x + y * Data->iEdgedWidth/2) * 8; |
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1590 |
|
|
1591 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
1592 |
|
|
1724 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1725 |
|
|
1726 |
*Data->iMinSAD = 256*4096; |
*Data->iMinSAD = 256*4096; |
1727 |
Data->Ref = f_Ref->y + k; |
Data->RefP[0] = f_Ref->y + k; |
1728 |
Data->RefH = f_RefH + k; |
Data->RefP[2] = f_RefH + k; |
1729 |
Data->RefV = f_RefV + k; |
Data->RefP[1] = f_RefV + k; |
1730 |
Data->RefHV = f_RefHV + k; |
Data->RefP[3] = f_RefHV + k; |
1731 |
Data->bRef = b_Ref->y + k; |
Data->b_RefP[0] = b_Ref->y + k; |
1732 |
Data->bRefH = b_RefH + k; |
Data->b_RefP[2] = b_RefH + k; |
1733 |
Data->bRefV = b_RefV + k; |
Data->b_RefP[1] = b_RefV + k; |
1734 |
Data->bRefHV = b_RefHV + k; |
Data->b_RefP[3] = b_RefHV + k; |
1735 |
Data->RefCU = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
1736 |
Data->RefCV = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
1737 |
Data->b_RefCU = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
1738 |
Data->b_RefCV = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
1739 |
|
|
1740 |
k = Data->qpel ? 4 : 2; |
k = Data->qpel ? 4 : 2; |
1741 |
Data->max_dx = k * (pParam->width - x * 16); |
Data->max_dx = k * (pParam->width - x * 16); |
1866 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
1867 |
|
|
1868 |
i = (x + y * fData->iEdgedWidth) * 16; |
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; |
|
1869 |
|
|
1870 |
|
bData.b_RefP[0] = fData->RefP[0] = f_Ref->y + i; |
1871 |
|
bData.b_RefP[2] = fData->RefP[2] = f_RefH + i; |
1872 |
|
bData.b_RefP[1] = fData->RefP[1] = f_RefV + i; |
1873 |
|
bData.b_RefP[3] = fData->RefP[3] = f_RefHV + i; |
1874 |
|
bData.RefP[0] = fData->b_RefP[0] = b_Ref->y + i; |
1875 |
|
bData.RefP[2] = fData->b_RefP[2] = b_RefH + i; |
1876 |
|
bData.RefP[1] = fData->b_RefP[1] = b_RefV + i; |
1877 |
|
bData.RefP[3] = fData->b_RefP[3] = b_RefHV + i; |
1878 |
|
bData.b_RefP[4] = fData->RefP[4] = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
1879 |
|
bData.b_RefP[5] = fData->RefP[5] = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
1880 |
|
bData.RefP[4] = fData->b_RefP[4] = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8; |
1881 |
|
bData.RefP[5] = fData->b_RefP[5] = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8; |
1882 |
|
|
1883 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
1884 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
2130 |
pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0); |
pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0); |
2131 |
|
|
2132 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
2133 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; |
2134 |
|
|
2135 |
pmv[1].x = EVEN(pMB->mvs[0].x); |
pmv[1].x = EVEN(pMB->mvs[0].x); |
2136 |
pmv[1].y = EVEN(pMB->mvs[0].y); |
pmv[1].y = EVEN(pMB->mvs[0].y); |
2139 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
2140 |
|
|
2141 |
CheckCandidate32I(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
|
Data->iMinSAD[1] -= 50; |
|
|
Data->iMinSAD[2] -= 50; |
|
|
Data->iMinSAD[3] -= 50; |
|
|
Data->iMinSAD[4] -= 50; |
|
2142 |
|
|
2143 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) { |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) { |
2144 |
|
|
2159 |
} |
} |
2160 |
} |
} |
2161 |
|
|
2162 |
#define INTRA_THRESH 2400 |
#define INTRA_THRESH 1800 |
2163 |
#define INTER_THRESH 1100 |
#define INTER_THRESH 1200 |
2164 |
|
|
2165 |
int |
int |
2166 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2189 |
CheckCandidate = CheckCandidate32I; |
CheckCandidate = CheckCandidate32I; |
2190 |
|
|
2191 |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
if (intraCount != 0 && intraCount < 10) // we're right after an I frame |
2192 |
IntraThresh += 8 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 15 * (intraCount - 10) * (intraCount - 10); |
2193 |
else |
else |
2194 |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
2195 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 8*(maxIntra - intraCount)))/maxIntra; |
2196 |
|
|
2197 |
InterThresh -= (350 - 8*b_thresh) * bCount; |
InterThresh -= (350 - 8*b_thresh) * bCount; |
2198 |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh; |
2235 |
sSAD /= blocks; |
sSAD /= blocks; |
2236 |
s = (10*s) / blocks; |
s = (10*s) / blocks; |
2237 |
|
|
2238 |
if (s > 5) sSAD += (s - 4) * (180 - 2*b_thresh); //static block - looks bad when in bframe... |
if (s > 4) sSAD += (s - 2) * (60 - 2*b_thresh); //static block - looks bad when in bframe... |
2239 |
|
|
2240 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2241 |
emms(); |
emms(); |
2289 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
2290 |
continue; |
continue; |
2291 |
|
|
2292 |
if ( ( (ABS(mv.x - (pMB-1)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-1)->mvs[0].y) < deltay) ) |
if ( ( (abs(mv.x - (pMB-1)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-1)->mvs[0].y) < deltay) ) |
2293 |
&& ( (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) ) |
2294 |
&& ( (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) ) |
2295 |
&& ( (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) ) ) |
2296 |
MBmask[mbnum]=1; |
MBmask[mbnum]=1; |
2297 |
} |
} |
2298 |
|
|
2367 |
continue; |
continue; |
2368 |
|
|
2369 |
oldnum++; |
oldnum++; |
2370 |
meanx += ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); |
meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ); |
2371 |
meany += ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); |
meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ); |
2372 |
} |
} |
2373 |
|
|
2374 |
if (4*meanx > oldnum) /* better fit than 0.25 is useless */ |
if (4*meanx > oldnum) /* better fit than 0.25 is useless */ |
2395 |
if (!MBmask[mbnum]) |
if (!MBmask[mbnum]) |
2396 |
continue; |
continue; |
2397 |
|
|
2398 |
if ( ( ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) |
if ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx ) |
2399 |
|| ( ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) |
|| ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) ) |
2400 |
MBmask[mbnum]=0; |
MBmask[mbnum]=0; |
2401 |
else |
else |
2402 |
num++; |
num++; |
2508 |
SearchData Data2, *Data8 = &Data2; |
SearchData Data2, *Data8 = &Data2; |
2509 |
int sumx = 0, sumy = 0; |
int sumx = 0, sumy = 0; |
2510 |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; |
2511 |
|
uint8_t * ptr; |
2512 |
|
|
2513 |
memcpy(Data8, Data, sizeof(SearchData)); |
memcpy(Data8, Data, sizeof(SearchData)); |
2514 |
CheckCandidate = CheckCandidateBits8; |
CheckCandidate = CheckCandidateBits8; |
2515 |
|
|
2516 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { //for all luma blocks |
2517 |
|
|
2518 |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
Data8->iMinSAD = Data->iMinSAD + i + 1; |
2519 |
Data8->currentMV = Data->currentMV + i + 1; |
Data8->currentMV = Data->currentMV + i + 1; |
2520 |
Data8->currentQMV = Data->currentQMV + i + 1; |
Data8->currentQMV = Data->currentQMV + i + 1; |
2521 |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2522 |
Data8->Ref = Data->Ref + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2523 |
Data8->RefH = Data->RefH + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2524 |
Data8->RefV = Data->RefV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2525 |
Data8->RefHV = Data->RefHV + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2526 |
|
|
2527 |
if(Data->qpel) { |
if(Data->qpel) { |
2528 |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); |
2541 |
|
|
2542 |
Data8->qpel_precision = Data8->qpel; |
Data8->qpel_precision = Data8->qpel; |
2543 |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
// checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) |
2544 |
if (Data8->qpel) { |
{ |
2545 |
if (!(Data8->currentQMV->x == backup[i+1].x && Data8->currentQMV->y == backup[i+1].y)) |
VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; |
2546 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
if (!MVequal (*v, backup[i+1]) ) |
|
} else { |
|
|
if (!(Data8->currentMV->x == backup[i+1].x && Data8->currentMV->y == backup[i+1].y)) |
|
2547 |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8); |
2548 |
} |
} |
2549 |
|
|
2562 |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) |
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) |
2563 |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2564 |
|
|
2565 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) |
2566 |
|
SubpelRefine(Data8); |
2567 |
|
|
2568 |
if(s > *Data8->iMinSAD) { //we have found a better match |
if(s > *Data8->iMinSAD) { //we have found a better match |
2569 |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
Data8->currentQMV->x = 2*Data8->currentMV->x; |
2577 |
} |
} |
2578 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8); |
2579 |
|
|
2580 |
} else // not qpel |
} else { // not qpel |
2581 |
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement |
|
2582 |
|
if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) //extsearch |
2583 |
|
SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255); |
2584 |
|
|
2585 |
|
if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) |
2586 |
|
SubpelRefine(Data8); //halfpel refinement |
2587 |
|
} |
2588 |
|
|
2589 |
//checking vector equal to predicion |
//checking vector equal to predicion |
2590 |
if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) { |
if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) { |
2591 |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; |
2592 |
if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y)) |
if (!MVequal(*v, Data8->predMV)) |
2593 |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8); |
2594 |
} |
} |
2595 |
|
|
2596 |
bits += *Data8->iMinSAD; |
bits += *Data8->iMinSAD; |
2597 |
if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V |
if (bits >= Data->iMinSAD[0]) return bits; // no chances for INTER4V |
2598 |
|
|
2599 |
// MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else |
// MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else |
2600 |
if(Data->qpel) { |
if(Data->qpel) { |
2612 |
pMB->mvs[i] = *Data8->currentMV; |
pMB->mvs[i] = *Data8->currentMV; |
2613 |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
pMB->sad8[i] = 4 * *Data8->iMinSAD; |
2614 |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
if (Data8->temp[0]) cbp |= 1 << (5 - i); |
|
} |
|
2615 |
|
|
2616 |
if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma |
} // /for all luma blocks |
2617 |
const uint8_t * ptr; |
|
2618 |
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
2619 |
|
|
2620 |
|
// let's check chroma |
2621 |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
2622 |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
2623 |
|
|
2624 |
//chroma U |
//chroma U |
2625 |
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); |
2626 |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); |
2627 |
fdct(in); |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 4, 0); |
2628 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
|
2629 |
else i = quant4_inter(coeff, in, Data->lambda16); |
if (bits >= *Data->iMinSAD) return bits; |
|
if (i > 0) { |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp |= 1 << (5 - 4); |
|
|
} |
|
2630 |
|
|
|
if (bits < *Data->iMinSAD) { // still possible |
|
2631 |
//chroma V |
//chroma V |
2632 |
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); |
2633 |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); |
2634 |
fdct(in); |
bits += Block_CalcBits(coeff, in, Data->iQuant, Data->quant_type, &cbp, 5, 0); |
2635 |
if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16); |
|
|
else i = quant4_inter(coeff, in, Data->lambda16); |
|
|
if (i > 0) { |
|
|
bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]); |
|
|
cbp |= 1 << (5 - 5); |
|
|
} |
|
|
bits += xvid_cbpy_tab[15-(cbp>>2)].len; |
|
2636 |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; |
|
} |
|
|
} |
|
2637 |
|
|
2638 |
return bits; |
return bits; |
2639 |
} |
} |
2643 |
CountMBBitsIntra(const SearchData * const Data) |
CountMBBitsIntra(const SearchData * const Data) |
2644 |
{ |
{ |
2645 |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
int bits = 1; //this one is ac/dc prediction flag. always 1. |
2646 |
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; |
|
2647 |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; |
2648 |
|
uint32_t iDcScaler = get_dc_scaler(Data->iQuant, 1); |
2649 |
|
|
2650 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, 1); |
|
|
|
|
2651 |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); |
2652 |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); |
2653 |
fdct(in); |
fdct(in); |
2654 |
b_dc = dc; |
b_dc = in[0]; |
2655 |
dc = in[0]; |
in[0] -= dc; |
2656 |
in[0] -= b_dc; |
dc = b_dc; |
2657 |
if (Data->lambda8 == 0) quant_intra_c(coeff, in, iQuant, iDcScaler); |
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
2658 |
else quant4_intra_c(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
|
|
|
|
b_dc = dc; |
|
|
dc = coeff[0]; |
|
|
if (i != 0) coeff[0] -= b_dc; |
|
2659 |
|
|
2660 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len; |
2661 |
Data->temp[i] = t; |
Data->temp[i] = t; |
2662 |
if (t != 0) cbp |= 1 << (5 - i); |
if (t != 0) cbp |= 1 << (5 - i); |
2663 |
if (bits >= Data->iMinSAD[0]) break; |
if (bits >= Data->iMinSAD[0]) return bits; |
2664 |
} |
} |
2665 |
|
|
2666 |
if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma |
bits += xvid_cbpy_tab[cbp>>2].len; |
2667 |
uint32_t iDcScaler = get_dc_scaler(iQuant, 0); |
|
2668 |
|
iDcScaler = get_dc_scaler(Data->iQuant, 0); |
2669 |
|
|
2670 |
//chroma U |
//chroma U |
2671 |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); |
2672 |
fdct(in); |
fdct(in); |
2673 |
in[0] -= 1024; |
in[0] -= 1024; |
2674 |
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
2675 |
else quant4_intra(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
2676 |
|
|
2677 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2678 |
if (t != 0) cbp |= 1 << (5 - 4); |
if (t != 0) cbp |= 1 << (5 - 4); |
2679 |
|
|
2680 |
if (bits < Data->iMinSAD[0]) { |
if (bits >= Data->iMinSAD[0]) return bits; |
2681 |
iDcScaler = get_dc_scaler(iQuant, 1); |
|
2682 |
//chroma V |
//chroma V |
2683 |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); |
2684 |
fdct(in); |
fdct(in); |
2685 |
in[0] -= 1024; |
in[0] -= 1024; |
2686 |
if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler); |
if (Data->quant_type == 0) quant_intra(coeff, in, Data->iQuant, iDcScaler); |
2687 |
else quant4_intra(coeff, in, iQuant, iDcScaler); |
else quant4_intra(coeff, in, Data->iQuant, iDcScaler); |
2688 |
|
|
2689 |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len; |
2690 |
if (t != 0) cbp |= 1 << (5 - 5); |
if (t != 0) cbp |= 1 << (5 - 5); |
2691 |
|
|
|
bits += xvid_cbpy_tab[cbp>>2].len; |
|
2692 |
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len; |
2693 |
} |
|
|
} |
|
2694 |
return bits; |
return bits; |
2695 |
} |
} |