55 |
#define GET_REFERENCE(X, Y, REF) { \ |
#define GET_REFERENCE(X, Y, REF) { \ |
56 |
switch ( (((X)&1)<<1) + ((Y)&1) ) \ |
switch ( (((X)&1)<<1) + ((Y)&1) ) \ |
57 |
{ \ |
{ \ |
58 |
case 0 : REF = data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
case 0 : REF = (uint8_t *)data->Ref + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
59 |
case 1 : REF = data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
case 1 : REF = (uint8_t *)data->RefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
60 |
case 2 : REF = data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
case 2 : REF = (uint8_t *)data->RefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
61 |
default : REF = data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
62 |
} \ |
} \ |
63 |
} |
} |
64 |
|
|
89 |
return xb + yb; |
return xb + yb; |
90 |
} |
} |
91 |
|
|
92 |
|
static int32_t |
93 |
|
ChromaSAD(int dx, int dy, const SearchData * const data) |
94 |
|
{ |
95 |
|
int sad; |
96 |
|
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
97 |
|
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
98 |
|
|
99 |
|
switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0) |
100 |
|
case 0: |
101 |
|
sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
102 |
|
sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
103 |
|
break; |
104 |
|
case 1: |
105 |
|
dx = dx / 2; dy = (dy - 1) / 2; |
106 |
|
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
107 |
|
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
108 |
|
break; |
109 |
|
case 2: |
110 |
|
dx = (dx - 1) / 2; dy = dy / 2; |
111 |
|
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
112 |
|
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
113 |
|
break; |
114 |
|
default: |
115 |
|
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
116 |
|
interpolate8x8_halfpel_hv(data->RefQ, |
117 |
|
data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
118 |
|
data->rounding); |
119 |
|
sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2); |
120 |
|
interpolate8x8_halfpel_hv(data->RefQ, |
121 |
|
data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
122 |
|
data->rounding); |
123 |
|
sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2); |
124 |
|
break; |
125 |
|
} |
126 |
|
return sad; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
/* CHECK_CANDIATE FUNCTIONS START */ |
/* CHECK_CANDIATE FUNCTIONS START */ |
131 |
|
|
132 |
|
|
133 |
static void |
static void |
134 |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
135 |
{ |
{ |
148 |
|
|
149 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
150 |
|
|
151 |
if(data->quarterpel) |
if (data->qpel) t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
152 |
t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
else |
|
|
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
153 |
|
|
154 |
data->temp[0] += lambda_vec16[data->iQuant] * t; |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
155 |
data->temp[1] += lambda_vec8[data->iQuant] * t; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
156 |
|
|
157 |
|
if (data->chroma) data->temp[0] += ChromaSAD(x, y, data); |
158 |
|
|
159 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
160 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
189 |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
190 |
} |
} |
191 |
|
|
192 |
if(data->quarterpel) |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
193 |
sad = lambda_vec16[data->iQuant] * |
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
|
d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
|
|
else |
|
|
sad = lambda_vec16[data->iQuant] * |
|
|
d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
|
|
|
|
sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
|
194 |
|
|
195 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
196 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
216 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
217 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
218 |
|
|
219 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases |
220 |
switch( ((x&1)<<1) + (y&1) ) |
switch( ((x&1)<<1) + (y&1) ) |
221 |
{ |
{ |
222 |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
223 |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference); |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference); |
224 |
break; |
break; |
225 |
|
|
226 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
227 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
|
|
|
228 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
229 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
230 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
232 |
break; |
break; |
233 |
|
|
234 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
235 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
|
|
|
236 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
237 |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
238 |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
241 |
|
|
242 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
243 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
244 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
245 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
246 |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
255 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1); |
256 |
|
|
257 |
t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
258 |
data->temp[0] += lambda_vec16[data->iQuant] * t; |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
259 |
data->temp[1] += lambda_vec8[data->iQuant] * t; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
260 |
|
|
261 |
|
if (data->chroma) |
262 |
|
data->temp[0] += ChromaSAD(x/2, y/2, data); |
263 |
|
|
264 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
265 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
277 |
} |
} |
278 |
|
|
279 |
static void |
static void |
|
CheckCandidate16no4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
|
|
|
|
|
// CheckCandidate16no4v variant which expects x and y in quarter pixel resolution |
|
|
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
|
|
// around currentMV! |
|
|
{ |
|
|
int32_t sad; |
|
|
uint8_t * Reference = (uint8_t *) data->RefQ; |
|
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
|
|
VECTOR halfpelMV = *(data->currentMV); |
|
|
|
|
|
int32_t iEdgedWidth = data->iEdgedWidth; |
|
|
uint32_t rounding = data->rounding; |
|
|
|
|
|
if (( x > data->max_dx) || ( x < data->min_dx) |
|
|
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|
|
|
|
|
switch( ((x&1)<<1) + (y&1) ) |
|
|
{ |
|
|
case 0: // pure halfpel position - shouldn't happen during a refinement step |
|
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference); |
|
|
break; |
|
|
|
|
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
|
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
|
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
|
|
|
|
|
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding); |
|
|
break; |
|
|
|
|
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
|
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
|
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
|
|
|
|
|
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding); |
|
|
break; |
|
|
|
|
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
|
|
// bottom left/right) during qpel refinement |
|
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
|
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
|
|
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
|
|
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
|
|
|
|
|
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding); |
|
|
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding); |
|
|
break; |
|
|
} |
|
|
|
|
|
sad = lambda_vec16[data->iQuant] * |
|
|
d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
|
|
sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
|
|
|
|
|
if (sad < *(data->iMinSAD)) { |
|
|
*(data->iMinSAD) = sad; |
|
|
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
|
|
// *dir = Direction; |
|
|
} |
|
|
} |
|
|
|
|
|
static void |
|
280 |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
281 |
{ |
{ |
282 |
int32_t sad; |
int32_t sad; |
284 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
285 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
286 |
|
|
287 |
sad = lambda_vec16[data->iQuant] * |
sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
|
d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
|
|
|
|
sad += sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
|
288 |
data->iEdgedWidth, 256*4096); |
data->iEdgedWidth, 256*4096); |
289 |
|
|
290 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
319 |
default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break; |
default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break; |
320 |
} |
} |
321 |
|
|
322 |
sad = lambda_vec16[data->iQuant] * |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
|
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode) ); |
|
323 |
|
|
324 |
sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad += (data->lambda16 * |
325 |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
326 |
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
327 |
|
|
328 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
329 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
334 |
static void |
static void |
335 |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
336 |
{ |
{ |
337 |
int32_t sad; |
int32_t sad = 0; |
338 |
int k; |
int k; |
339 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
340 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
342 |
|
|
343 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
344 |
|
|
|
sad = lambda_vec16[data->iQuant] * d_mv_bits(x, y, 1); |
|
|
|
|
345 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
346 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
347 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
379 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
380 |
} |
} |
381 |
|
|
382 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
383 |
|
|
384 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
385 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
386 |
data->currentMV->x = x; data->currentMV->y = y; |
data->currentMV->x = x; data->currentMV->y = y; |
397 |
|
|
398 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
399 |
|
|
|
sad = lambda_vec16[data->iQuant] * d_mv_bits(x, y, 1); |
|
|
|
|
400 |
mvs.x = data->directmvF[0].x + x; |
mvs.x = data->directmvF[0].x + x; |
401 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
402 |
data->directmvB[0].x |
data->directmvB[0].x |
426 |
default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break; |
default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break; |
427 |
} |
} |
428 |
|
|
429 |
sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
430 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
431 |
|
|
432 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
433 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
438 |
static void |
static void |
439 |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
440 |
{ |
{ |
441 |
int32_t sad; |
int32_t sad; int t; |
442 |
const uint8_t * Reference; |
const uint8_t * Reference; |
443 |
|
|
444 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
453 |
} |
} |
454 |
|
|
455 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
456 |
|
if (data->qpel) t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode); |
457 |
|
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
458 |
|
|
459 |
if(data->quarterpel) |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
|
sad += lambda_vec8[data->iQuant] * d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
|
|
else |
|
|
sad += lambda_vec8[data->iQuant] * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
460 |
|
|
461 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
462 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
482 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
483 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
484 |
|
|
485 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
486 |
switch( ((x&1)<<1) + (y&1) ) |
switch( ((x&1)<<1) + (y&1) ) |
487 |
{ |
{ |
488 |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
case 0: // pure halfpel position - shouldn't happen during a refinement step |
489 |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, (const uint8_t *) Reference); |
GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference); |
490 |
break; |
break; |
491 |
|
|
492 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
493 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
494 |
|
|
495 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
496 |
break; |
break; |
497 |
|
|
498 |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
case 2: // x qpel, y halfpel - left or right during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
499 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
500 |
|
|
501 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding); |
503 |
|
|
504 |
default: // x and y in qpel resolution - the "corners" (top left/right and |
default: // x and y in qpel resolution - the "corners" (top left/right and |
505 |
// bottom left/right) during qpel refinement |
// bottom left/right) during qpel refinement |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); |
|
506 |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
507 |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
508 |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
512 |
} |
} |
513 |
|
|
514 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
515 |
sad += lambda_vec8[data->iQuant] * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
sad += (data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode) * (sad+NEIGH_8X8_BIAS))/100; |
516 |
|
|
517 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
518 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
760 |
|
|
761 |
uint32_t x, y; |
uint32_t x, y; |
762 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
763 |
int32_t InterBias, quant = current->quant; |
int32_t InterBias, quant = current->quant, sad00; |
764 |
uint8_t *qimage; |
uint8_t *qimage; |
765 |
|
|
766 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
776 |
Data.temp = temp; |
Data.temp = temp; |
777 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
778 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
779 |
Data.quarterpel = pParam->m_quarterpel; |
Data.qpel = pParam->m_quarterpel; |
780 |
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
781 |
|
|
782 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
783 |
return 1; // allocate some mem for qpel interpolated blocks |
return 1; // allocate some mem for qpel interpolated blocks |
784 |
// somehow this is dirty since I think we shouldn't use malloc outside |
// somehow this is dirty since I think we shouldn't use malloc outside |
785 |
// encoder_create() - so please fix me! |
// encoder_create() - so please fix me! |
786 |
|
Data.RefQ = qimage; |
787 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
788 |
|
|
789 |
for (y = 0; y < pParam->mb_height; y++) { |
for (y = 0; y < pParam->mb_height; y++) { |
790 |
for (x = 0; x < pParam->mb_width; x++) { |
for (x = 0; x < pParam->mb_width; x++) { |
|
|
|
791 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
792 |
int32_t sad00 = pMB->sad16 |
|
793 |
|
pMB->sad16 |
794 |
= sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
= sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
795 |
pRef->y + (x + y * pParam->edged_width) * 16, |
pRef->y + (x + y * pParam->edged_width) * 16, |
796 |
pParam->edged_width, pMB->sad8 ); |
pParam->edged_width, pMB->sad8 ); |
797 |
|
|
798 |
|
if (Data.chroma) { |
799 |
|
pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8, |
800 |
|
pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2); |
801 |
|
|
802 |
|
pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8, |
803 |
|
pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2); |
804 |
|
} |
805 |
|
|
806 |
|
sad00 = pMB->sad16; //if no gmc; else sad00 = (..) |
807 |
|
|
808 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
809 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
810 |
pMB->quant = current->quant; } |
pMB->quant = current->quant; |
811 |
else |
} else { |
812 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
813 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
814 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
815 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
816 |
|
} |
817 |
pMB->quant = quant; |
pMB->quant = quant; |
818 |
} |
} |
819 |
|
|
820 |
//initial skip decision |
//initial skip decision |
821 |
|
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
822 |
if ((pMB->dquant == NO_CHANGE) && (sad00 <= MAX_SAD00_FOR_SKIP * pMB->quant) |
if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
823 |
&& (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) ) { |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH) |
824 |
if (pMB->sad16 < pMB->quant * INITIAL_SKIP_THRESH) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
825 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
826 |
continue; |
continue; |
|
sad00 = 256 * 4096; |
|
827 |
} |
} |
828 |
} else sad00 = 256*4096; // skip not allowed - for final skip decision |
} |
829 |
|
|
830 |
SearchP(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
831 |
y, current->motion_flags, pMB->quant, |
y, current->motion_flags, pMB->quant, |
832 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, |
833 |
current->global_flags & XVID_INTER4V, pMB); |
current->global_flags & XVID_INTER4V, pMB); |
834 |
|
|
835 |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
/* final skip decision, a.k.a. "the vector you found, really that good?" */ |
836 |
if (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
if (current->coding_type == P_VOP) { |
837 |
if ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
838 |
{ SkipMacroblockP(pMB, sad00); continue; } |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) ) |
839 |
|
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
840 |
|
SkipMacroblockP(pMB, sad00); |
841 |
|
continue; |
842 |
|
} |
843 |
|
} |
844 |
|
|
845 |
/* finally, intra decision */ |
/* finally, intra decision */ |
846 |
|
|
847 |
InterBias = MV16_INTER_BIAS; |
InterBias = MV16_INTER_BIAS; |
848 |
if (pMB->quant > 8) InterBias += 50 * (pMB->quant - 8); // to make high quants work |
if (pMB->quant > 8) InterBias += 100 * (pMB->quant - 8); // to make high quants work |
849 |
if (y != 0) |
if (y != 0) |
850 |
if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 50; |
if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; |
851 |
if (x != 0) |
if (x != 0) |
852 |
if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 50; |
if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; |
853 |
|
|
854 |
|
if (Data.chroma) InterBias += 50; // to compensate bigger SAD |
855 |
|
|
856 |
if (InterBias < pMB->sad16) { |
if (InterBias < pMB->sad16) { |
857 |
const int32_t deviation = |
const int32_t deviation = |
872 |
} |
} |
873 |
} |
} |
874 |
free(qimage); |
free(qimage); |
875 |
|
|
876 |
|
if (current->coding_type == S_VOP) /* first GMC step only for S(GMC)-VOPs */ |
877 |
|
current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode ); |
878 |
|
else |
879 |
|
current->GMC_MV = zeroMV; |
880 |
|
|
881 |
return 0; |
return 0; |
882 |
} |
} |
883 |
|
|
935 |
} |
} |
936 |
|
|
937 |
static void |
static void |
938 |
SearchP(const uint8_t * const pRef, |
SearchP(const IMAGE * const pRef, |
939 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
940 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
941 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
|
const uint8_t * const pRefQ, |
|
942 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
943 |
const int x, |
const int x, |
944 |
const int y, |
const int y, |
955 |
int i, iDirection = 255, mask, threshA; |
int i, iDirection = 255, mask, threshA; |
956 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
957 |
|
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
|
|
|
958 |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
959 |
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, |
960 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
962 |
Data->predMV = pmv[0]; |
Data->predMV = pmv[0]; |
963 |
|
|
964 |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
965 |
Data->Ref = pRef + (x + Data->iEdgedWidth*y)*16; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
966 |
|
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
967 |
|
|
968 |
|
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
969 |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
970 |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
971 |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
972 |
Data->RefQ = pRefQ; |
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
973 |
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
974 |
|
|
975 |
Data->iQuant = iQuant; |
Data->lambda16 = lambda_vec16[iQuant]; |
976 |
|
Data->lambda8 = lambda_vec8[iQuant]; |
977 |
|
|
978 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
979 |
Data->min_dx = EVEN(Data->min_dx); |
Data->min_dx = EVEN(Data->min_dx); |
983 |
|
|
984 |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
985 |
|
|
|
if (inter4v) CheckCandidate = CheckCandidate16; |
|
|
else CheckCandidate = CheckCandidate16no4v; |
|
|
|
|
986 |
for(i = 0; i < 5; i++) |
for(i = 0; i < 5; i++) |
987 |
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
Data->currentMV[i].x = Data->currentMV[i].y = 0; |
988 |
|
|
989 |
if(Data->quarterpel) |
if (pParam->m_quarterpel) { |
990 |
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
991 |
i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode); |
i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode); |
992 |
else |
} else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
|
i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
|
993 |
|
|
994 |
Data->iMinSAD[0] = pMB->sad16 + lambda_vec16[iQuant] * i; |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
995 |
Data->iMinSAD[1] = pMB->sad8[0] + lambda_vec8[iQuant] * i; |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
996 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
997 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
998 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1006 |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
1007 |
prevMBs + x + y * pParam->mb_width); |
prevMBs + x + y * pParam->mb_width); |
1008 |
|
|
1009 |
if (inter4v) CheckCandidate = CheckCandidate16; |
if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16; |
1010 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1011 |
|
|
|
|
|
1012 |
/* main loop. checking all predictions */ |
/* main loop. checking all predictions */ |
1013 |
|
|
1014 |
for (i = 1; i < 7; i++) { |
for (i = 1; i < 7; i++) { |
1042 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1043 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1044 |
|
|
1045 |
CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data); |
(*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data); |
1046 |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
1047 |
if (bSAD < Data->iMinSAD[0]) { |
if (bSAD < Data->iMinSAD[0]) { |
1048 |
Data->currentMV[0] = backupMV; |
Data->currentMV[0] = backupMV; |
1055 |
if (!(MVequal(startMV, backupMV))) { |
if (!(MVequal(startMV, backupMV))) { |
1056 |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR; |
1057 |
|
|
1058 |
CheckCandidate16(startMV.x, startMV.y, 255, &iDirection, Data); |
(*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data); |
1059 |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
(*MainSearchPtr)(startMV.x, startMV.y, Data, 255); |
1060 |
if (bSAD < Data->iMinSAD[0]) { |
if (bSAD < Data->iMinSAD[0]) { |
1061 |
Data->currentMV[0] = backupMV; |
Data->currentMV[0] = backupMV; |
1073 |
|
|
1074 |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
1075 |
|
|
|
if(inter4v) |
|
1076 |
CheckCandidate = CheckCandidate16_qpel; |
CheckCandidate = CheckCandidate16_qpel; |
|
else |
|
|
CheckCandidate = CheckCandidate16no4v_qpel; |
|
|
|
|
1077 |
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, |
1078 |
pParam->width, pParam->height, Data->iFcode, 0); // get real range |
pParam->width, pParam->height, Data->iFcode, 0); |
1079 |
|
|
1080 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
1081 |
} |
} |
1082 |
|
|
1083 |
|
if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0; |
1084 |
if (inter4v) { |
if (inter4v) { |
1085 |
SearchData Data8; |
SearchData Data8; |
1086 |
Data8.iFcode = Data->iFcode; |
Data8.iFcode = Data->iFcode; |
1087 |
Data8.iQuant = Data->iQuant; |
Data8.lambda8 = Data->lambda8; |
1088 |
Data8.iEdgedWidth = Data->iEdgedWidth; |
Data8.iEdgedWidth = Data->iEdgedWidth; |
1089 |
|
Data8.RefQ = Data->RefQ; |
1090 |
|
Data8.qpel = Data->qpel; |
1091 |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
1092 |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
1093 |
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); |
1094 |
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); |
1095 |
|
|
1096 |
|
if (Data->chroma) { |
1097 |
|
int sum, dx, dy; |
1098 |
|
|
1099 |
|
if(pParam->m_quarterpel) { |
1100 |
|
sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2; |
1101 |
|
} else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1102 |
|
dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1103 |
|
|
1104 |
|
if(pParam->m_quarterpel) { |
1105 |
|
sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2; |
1106 |
|
} else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1107 |
|
dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1108 |
|
|
1109 |
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
1110 |
|
} |
1111 |
} |
} |
1112 |
|
|
1113 |
if (!(inter4v) || |
if (!(inter4v) || |
1127 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1128 |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
1129 |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
1130 |
} |
} else { |
|
else { |
|
1131 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1132 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1133 |
} |
} |
1149 |
const int block, |
const int block, |
1150 |
SearchData * const Data) |
SearchData * const Data) |
1151 |
{ |
{ |
|
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
1152 |
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
1153 |
Data->currentMV = OldData->currentMV + 1 + block; |
Data->currentMV = OldData->currentMV + 1 + block; |
1154 |
Data->currentQMV = OldData->currentQMV + 1 + block; |
Data->currentQMV = OldData->currentQMV + 1 + block; |
|
Data->quarterpel = OldData->quarterpel; |
|
1155 |
|
|
1156 |
if(Data->quarterpel) // add d_mv_bits[qpel] everywhere but not in 0 (it's already there) |
if(pParam->m_quarterpel) { |
1157 |
{ |
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
1158 |
if (block != 0) |
if (block != 0) *(Data->iMinSAD) += (Data->lambda8 * |
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
1159 |
d_mv_bits(Data->currentQMV->x - Data->predQMV.x, |
d_mv_bits(Data->currentQMV->x - Data->predQMV.x, |
1160 |
Data->currentQMV->y - Data->predQMV.y, |
Data->currentQMV->y - Data->predQMV.y, |
1161 |
Data->iFcode); |
Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
1162 |
|
} else { |
1163 |
} else // add d_mv_bits[hpel] everywhere but not in 0 (it's already there) |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
1164 |
if (block != 0) |
if (block != 0) *(Data->iMinSAD) += (Data->lambda8 * |
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
1165 |
d_mv_bits(Data->currentMV->x - Data->predMV.x, |
d_mv_bits(Data->currentMV->x - Data->predMV.x, |
1166 |
Data->currentMV->y - Data->predMV.y, |
Data->currentMV->y - Data->predMV.y, |
1167 |
Data->iFcode); |
Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
1168 |
|
} |
1169 |
|
|
1170 |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
1171 |
|
|
1173 |
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
1174 |
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
1175 |
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
Data->RefQ = OldData->RefQ; |
|
1176 |
|
|
1177 |
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
1178 |
|
|
1179 |
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, |
1180 |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
1181 |
CheckCandidate = CheckCandidate8; |
CheckCandidate = CheckCandidate8; |
1182 |
|
|
1183 |
if (MotionFlags & PMV_EXTSEARCH8) { |
if (MotionFlags & PMV_EXTSEARCH8) { |
1190 |
|
|
1191 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
1192 |
|
|
1193 |
if(*(Data->iMinSAD) < temp_sad) { //found a better match? |
if(*(Data->iMinSAD) < temp_sad) { |
1194 |
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
1195 |
Data->currentQMV->y = 2 * Data->currentMV->y; |
Data->currentQMV->y = 2 * Data->currentMV->y; |
1196 |
} |
} |
1207 |
} |
} |
1208 |
} |
} |
1209 |
|
|
1210 |
if((Data->quarterpel) && (!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
if(pParam->m_quarterpel) { |
1211 |
|
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
1212 |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
|
|
|
1213 |
CheckCandidate = CheckCandidate8_qpel; |
CheckCandidate = CheckCandidate8_qpel; |
|
|
|
1214 |
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, |
1215 |
pParam->width, pParam->height, OldData->iFcode, 0); // get real range |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
1216 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
1217 |
} |
} |
1218 |
} |
} |
1219 |
|
} |
1220 |
|
|
1221 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1222 |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
1230 |
pMB->mvs[block] = *(Data->currentMV); |
pMB->mvs[block] = *(Data->currentMV); |
1231 |
pMB->qmvs[block] = *(Data->currentQMV); |
pMB->qmvs[block] = *(Data->currentQMV); |
1232 |
|
|
1233 |
pMB->sad8[block] = 4 * (*Data->iMinSAD); // Isibaar: why? |
pMB->sad8[block] = 4 * (*Data->iMinSAD); |
1234 |
} |
} |
1235 |
|
|
1236 |
/* B-frames code starts here */ |
/* B-frames code starts here */ |
1343 |
|
|
1344 |
// three bits are needed to code backward mode. four for forward |
// three bits are needed to code backward mode. four for forward |
1345 |
// we treat the bits just like they were vector's |
// we treat the bits just like they were vector's |
1346 |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * lambda_vec16[Data->iQuant]; |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
1347 |
else *Data->iMinSAD += 3 * lambda_vec16[Data->iQuant]; |
else *Data->iMinSAD += 3 * Data->lambda16; |
|
|
|
1348 |
|
|
1349 |
if (*Data->iMinSAD < *best_sad) { |
if (*Data->iMinSAD < *best_sad) { |
1350 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1351 |
pMB->mode = mode_current; |
pMB->mode = mode_current; |
1352 |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
1353 |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
1354 |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
1355 |
else pMB->b_mvs[0] = *Data->currentMV; |
else pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
1356 |
} |
} |
1357 |
|
|
1358 |
} |
} |
1429 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
(*CheckCandidate)(0, 0, 255, &k, Data); |
1430 |
|
|
1431 |
// skip decision |
// skip decision |
1432 |
if (*Data->iMinSAD - 2 * lambda_vec16[Data->iQuant] < (int32_t)Data->iQuant * SKIP_THRESH_B) { |
if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) { |
1433 |
//possible skip - checking chroma. everything copied from MC |
//possible skip - checking chroma. everything copied from MC |
1434 |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
1435 |
int sum, dx, dy, b_dx, b_dy; |
int sum, dx, dy, b_dx, b_dy; |
1455 |
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
1456 |
Data->iEdgedWidth/2); |
Data->iEdgedWidth/2); |
1457 |
|
|
1458 |
if ((uint32_t) sum < MAX_CHROMA_SAD_FOR_SKIP * Data->iQuant) { |
if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { |
1459 |
pMB->mode = MODE_DIRECT_NONE_MV; |
pMB->mode = MODE_DIRECT_NONE_MV; |
1460 |
return *Data->iMinSAD; |
return *Data->iMinSAD; |
1461 |
} |
} |
1474 |
|
|
1475 |
HalfpelRefine(Data); |
HalfpelRefine(Data); |
1476 |
|
|
1477 |
*Data->iMinSAD += 1 * lambda_vec16[Data->iQuant]; // one bit is needed to code direct mode. we treat this bit just like it was vector's |
*Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode |
1478 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1479 |
|
|
1480 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) |
1530 |
int iDirection, i, j; |
int iDirection, i, j; |
1531 |
SearchData bData; |
SearchData bData; |
1532 |
|
|
1533 |
bData.iMinSAD = fData->iMinSAD; |
*(bData.iMinSAD = fData->iMinSAD) = 4096*256; |
|
*bData.iMinSAD = 4096*256; |
|
1534 |
bData.Cur = fData->Cur; |
bData.Cur = fData->Cur; |
1535 |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
1536 |
bData.currentMV = fData->currentMV + 1; |
bData.currentMV = fData->currentMV + 1; |
1537 |
bData.iQuant = fData->iQuant; |
bData.lambda16 = fData->lambda16; |
1538 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
1539 |
|
|
1540 |
bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16; |
bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16; |
1549 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
1550 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
1551 |
|
|
1552 |
fData->currentMV[0] = pMB->mvs[0]; |
fData->currentMV[0] = fData->currentMV[3]; //forward search stored it's vector here. backward stored it in the place it's needed |
|
fData->currentMV[1] = pMB->b_mvs[0]; |
|
1553 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel); |
1554 |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel); |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel); |
1555 |
|
|
1588 |
|
|
1589 |
} while (!(iDirection)); |
} while (!(iDirection)); |
1590 |
|
|
1591 |
// two bits are needed to code interpolate mode. we treat the bits just like they were vector's |
*fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode. |
1592 |
*fData->iMinSAD += 2 * lambda_vec16[fData->iQuant]; |
|
1593 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
1594 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
1595 |
pMB->mvs[0] = fData->currentMV[0]; |
pMB->mvs[0] = fData->currentMV[0]; |
1603 |
} |
} |
1604 |
} |
} |
1605 |
|
|
|
|
|
1606 |
void |
void |
1607 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
1608 |
FRAMEINFO * const frame, |
FRAMEINFO * const frame, |
1639 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1640 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
1641 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
1642 |
Data.iQuant = frame->quant; |
Data.lambda16 = lambda_vec16[frame->quant]; |
1643 |
|
|
1644 |
// note: i==horizontal, j==vertical |
// note: i==horizontal, j==vertical |
1645 |
|
|
1651 |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
1652 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1653 |
|
|
1654 |
/* special case, if collocated block is SKIPed: encoding is forward (0,0), cpb=0 without further ado */ |
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
1655 |
if (b_mb->mode == MODE_NOT_CODED) { |
if (b_mb->mode == MODE_NOT_CODED) { |
1656 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1657 |
continue; |
continue; |
1658 |
} |
} |
1659 |
|
|
1660 |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
1661 |
|
pMB->quant = frame->quant; |
1662 |
|
|
1663 |
/* direct search comes first, because it (1) checks for SKIP-mode |
/* direct search comes first, because it (1) checks for SKIP-mode |
1664 |
and (2) sets very good predictions for forward and backward search */ |
and (2) sets very good predictions for forward and backward search */ |
|
|
|
1665 |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1666 |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1667 |
&frame->image, |
&frame->image, |
1675 |
|
|
1676 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
1677 |
|
|
|
// best_sad = 256*4096; //uncomment to disable Directsearch. |
|
|
// To disable any other mode, just comment the function call |
|
|
|
|
1678 |
// forward search |
// forward search |
1679 |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
1680 |
&frame->image, i, j, |
&frame->image, i, j, |
1727 |
} |
} |
1728 |
} |
} |
1729 |
} |
} |
|
|
|
|
// fprintf(debug,"B-Stat: F: %04d B: %04d I: %04d D: %04d, N: %04d\n", |
|
|
// f_count,b_count,i_count,d_count,n_count); |
|
|
|
|
1730 |
} |
} |
1731 |
|
|
1732 |
/* Hinted ME starts here */ |
/* Hinted ME starts here */ |
1733 |
|
|
1734 |
static void |
static void |
1735 |
Search8hinted(const SearchData * const OldData, |
SearchPhinted ( const IMAGE * const pRef, |
|
const int x, const int y, |
|
|
const uint32_t MotionFlags, |
|
|
const MBParam * const pParam, |
|
|
MACROBLOCK * const pMB, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const int block, |
|
|
SearchData * const Data) |
|
|
{ |
|
|
int32_t temp_sad; |
|
|
MainSearchFunc *MainSearchPtr; |
|
|
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
|
|
Data->currentMV = OldData->currentMV + 1 + block; |
|
|
Data->currentQMV = OldData->currentQMV + 1 + block; |
|
|
Data->quarterpel = OldData->quarterpel; |
|
|
|
|
|
if (block != 0) { |
|
|
if(pParam->m_quarterpel) { |
|
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
|
d_mv_bits( Data->currentQMV->x - Data->predQMV.x, |
|
|
Data->currentQMV->y - Data->predQMV.y, |
|
|
Data->iFcode); |
|
|
} |
|
|
else { |
|
|
*(Data->iMinSAD) += lambda_vec8[Data->iQuant] * |
|
|
d_mv_bits( Data->currentMV->x - Data->predMV.x, |
|
|
Data->currentMV->y - Data->predMV.y, |
|
|
Data->iFcode); |
|
|
} |
|
|
} |
|
|
|
|
|
Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefQ = OldData->RefQ; |
|
|
|
|
|
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
|
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
|
|
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
|
|
CheckCandidate = CheckCandidate8; |
|
|
|
|
|
temp_sad = *(Data->iMinSAD); // store current MinSAD |
|
|
|
|
|
if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch; |
|
|
else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
|
|
else MainSearchPtr = DiamondSearch; |
|
|
|
|
|
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
|
|
|
|
|
if(*(Data->iMinSAD) < temp_sad) { |
|
|
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
|
|
Data->currentQMV->y = 2 * Data->currentMV->y; |
|
|
} |
|
|
|
|
|
if (MotionFlags & PMV_HALFPELREFINE8) { |
|
|
temp_sad = *(Data->iMinSAD); // store current MinSAD |
|
|
|
|
|
HalfpelRefine(Data); // perform halfpel refine of current best vector |
|
|
|
|
|
if(*(Data->iMinSAD) < temp_sad) { // we have found a better match |
|
|
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
|
|
Data->currentQMV->y = 2 * Data->currentMV->y; |
|
|
} |
|
|
} |
|
|
|
|
|
if((Data->quarterpel) && (!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
|
|
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
|
|
|
|
|
CheckCandidate = CheckCandidate8_qpel; |
|
|
|
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
|
|
pParam->width, pParam->height, OldData->iFcode, 0); // get real range |
|
|
|
|
|
QuarterpelRefine(Data); |
|
|
} |
|
|
|
|
|
if(pParam->m_quarterpel) { |
|
|
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
|
|
pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y; |
|
|
} |
|
|
else { |
|
|
pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; |
|
|
pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; |
|
|
} |
|
|
|
|
|
pMB->mvs[block] = *(Data->currentMV); |
|
|
pMB->qmvs[block] = *(Data->currentQMV); |
|
|
|
|
|
pMB->sad8[block] = 4 * (*Data->iMinSAD); |
|
|
} |
|
|
|
|
|
|
|
|
static void |
|
|
SearchPhinted ( const uint8_t * const pRef, |
|
1736 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1737 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
1738 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
|
const uint8_t * const pRefQ, |
|
1739 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
1740 |
const int x, |
const int x, |
1741 |
const int y, |
const int y, |
1748 |
SearchData * const Data) |
SearchData * const Data) |
1749 |
{ |
{ |
1750 |
|
|
|
const int32_t iEdgedWidth = pParam->edged_width; |
|
|
|
|
1751 |
int i, t; |
int i, t; |
1752 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1753 |
|
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
1754 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1755 |
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1756 |
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, |
1757 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
1758 |
|
|
1759 |
Data->Cur = pCur->y + (x + y * iEdgedWidth) * 16; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
1760 |
Data->Ref = pRef + (x + iEdgedWidth*y)*16; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1761 |
Data->RefH = pRefH + (x + iEdgedWidth*y) * 16; |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
Data->RefV = pRefV + (x + iEdgedWidth*y) * 16; |
|
|
Data->RefHV = pRefHV + (x + iEdgedWidth*y) * 16; |
|
|
Data->RefQ = pRefQ; |
|
1762 |
|
|
1763 |
Data->iQuant = iQuant; |
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
1764 |
|
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
1765 |
|
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
1766 |
|
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
1767 |
|
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1768 |
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1769 |
|
|
1770 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
1771 |
Data->min_dx = EVEN(Data->min_dx); |
Data->min_dx = EVEN(Data->min_dx); |
1778 |
|
|
1779 |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
1780 |
|
|
1781 |
if (inter4v) |
if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16; |
|
CheckCandidate = CheckCandidate16; |
|
1782 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1783 |
|
|
|
|
|
1784 |
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
1785 |
pMB->mvs[0].y = EVEN(pMB->mvs[0].y); |
pMB->mvs[0].y = EVEN(pMB->mvs[0].y); |
1786 |
if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed |
if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed |
1814 |
} |
} |
1815 |
|
|
1816 |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
|
if(inter4v) |
|
|
CheckCandidate = CheckCandidate16_qpel; |
|
|
else |
|
|
CheckCandidate = CheckCandidate16no4v_qpel; |
|
|
|
|
1817 |
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, |
1818 |
pParam->width, pParam->height, Data->iFcode, 0); // get real range |
pParam->width, pParam->height, Data->iFcode, 0); |
1819 |
|
CheckCandidate = CheckCandidate16_qpel; |
1820 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
1821 |
} |
} |
1822 |
|
|
1823 |
if (inter4v) { |
if (inter4v) { |
1824 |
SearchData Data8; |
SearchData Data8; |
1825 |
Data8.iFcode = Data->iFcode; |
Data8.iFcode = Data->iFcode; |
1826 |
Data8.iQuant = Data->iQuant; |
Data8.lambda8 = Data->lambda8; |
1827 |
Data8.iEdgedWidth = Data->iEdgedWidth; |
Data8.iEdgedWidth = Data->iEdgedWidth; |
1828 |
Search8hinted(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Data8.RefQ = Data->RefQ; |
1829 |
Search8hinted(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Data8.qpel = Data->qpel; |
1830 |
Search8hinted(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
1831 |
Search8hinted(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
1832 |
|
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
1833 |
|
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1834 |
|
|
1835 |
|
if (Data->chroma) { |
1836 |
|
int sum, dx, dy; |
1837 |
|
|
1838 |
|
if(pParam->m_quarterpel) |
1839 |
|
sum = (pMB->qmvs[0].y + pMB->qmvs[1].y + pMB->qmvs[2].y + pMB->qmvs[3].y)/2; |
1840 |
|
else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1841 |
|
dy = (sum ? SIGN(sum) * |
1842 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
1843 |
|
|
1844 |
|
if(pParam->m_quarterpel) |
1845 |
|
sum = (pMB->qmvs[0].x + pMB->qmvs[1].x + pMB->qmvs[2].x + pMB->qmvs[3].x)/2; |
1846 |
|
else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1847 |
|
dx = (sum ? SIGN(sum) * |
1848 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
1849 |
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
1850 |
|
} |
1851 |
} |
} |
1852 |
|
|
1853 |
if (!(inter4v) || |
if (!(inter4v) || |
1854 |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] + |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] + |
1855 |
Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
1856 |
// INTER MODE |
// INTER MODE |
|
|
|
1857 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1858 |
pMB->mvs[0] = pMB->mvs[1] |
pMB->mvs[0] = pMB->mvs[1] |
1859 |
= pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
= pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1860 |
|
|
1861 |
|
pMB->qmvs[0] = pMB->qmvs[1] |
1862 |
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
1863 |
|
|
1864 |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = |
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = |
1865 |
pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
1866 |
|
|
1867 |
|
if(pParam->m_quarterpel) { |
1868 |
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
1869 |
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
1870 |
|
} else { |
1871 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1872 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1873 |
|
} |
1874 |
} else { |
} else { |
1875 |
// INTER4V MODE; all other things are already set in Search8hinted |
// INTER4V MODE; all other things are already set in Search8 |
1876 |
pMB->mode = MODE_INTER4V; |
pMB->mode = MODE_INTER4V; |
1877 |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
1878 |
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
1896 |
uint8_t *qimage; |
uint8_t *qimage; |
1897 |
int32_t temp[5], quant = current->quant; |
int32_t temp[5], quant = current->quant; |
1898 |
int32_t iMinSAD[5]; |
int32_t iMinSAD[5]; |
1899 |
VECTOR currentMV[5]; |
VECTOR currentMV[5], currentQMV[5]; |
|
VECTOR currentQMV[5]; |
|
1900 |
SearchData Data; |
SearchData Data; |
1901 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1902 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; |
1905 |
Data.temp = temp; |
Data.temp = temp; |
1906 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
1907 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
1908 |
|
Data.qpel = pParam->m_quarterpel; |
1909 |
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
1910 |
|
|
1911 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
1912 |
return; // allocate some mem for qpel interpolated blocks |
return; // allocate some mem for qpel interpolated blocks |
1913 |
// somehow this is dirty since I think we shouldn't use malloc outside |
// somehow this is dirty since I think we shouldn't use malloc outside |
1914 |
// encoder_create() - so please fix me! |
// encoder_create() - so please fix me! |
1915 |
|
|
1916 |
|
Data.RefQ = qimage; |
1917 |
|
|
1918 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
1919 |
|
|
1920 |
for (y = 0; y < pParam->mb_height; y++) { |
for (y = 0; y < pParam->mb_height; y++) { |
1925 |
//intra mode is copied from the first pass. At least for the time being |
//intra mode is copied from the first pass. At least for the time being |
1926 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
1927 |
|
|
|
|
|
1928 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
1929 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
1930 |
pMB->quant = current->quant; } |
pMB->quant = current->quant; } |
1931 |
else |
else { |
1932 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
1933 |
quant += DQtab[pMB->dquant]; |
quant += DQtab[pMB->dquant]; |
1934 |
if (quant > 31) quant = 31; |
if (quant > 31) quant = 31; |
1935 |
else if (quant < 1) quant = 1; |
else if (quant < 1) quant = 1; |
1936 |
|
} |
1937 |
pMB->quant = quant; |
pMB->quant = quant; |
1938 |
} |
} |
1939 |
|
|
1940 |
SearchPhinted(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x, |
SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1941 |
y, current->motion_flags, pMB->quant, |
y, current->motion_flags, pMB->quant, |
1942 |
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
1943 |
&Data); |
&Data); |
1958 |
SearchData * const Data) |
SearchData * const Data) |
1959 |
{ |
{ |
1960 |
|
|
1961 |
int i, mask; |
int i = 255, mask; |
1962 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
|
|
|
1963 |
*(Data->iMinSAD) = MV_MAX_ERROR; |
*(Data->iMinSAD) = MV_MAX_ERROR; |
1964 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
1965 |
|
//median is only used as prediction. it doesn't have to be real |
1966 |
|
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
1967 |
|
else |
1968 |
|
if (x == 1) //left macroblock does not have any vector now |
1969 |
|
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
1970 |
|
else if (y == 1) // top macroblock don't have it's vector |
1971 |
|
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
1972 |
|
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
1973 |
|
|
1974 |
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, |
1975 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
1976 |
|
|
1977 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
1978 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
1979 |
|
|
|
CheckCandidate = CheckCandidate16no4vI; |
|
|
|
|
1980 |
pmv[1].x = EVEN(pMB->mvs[0].x); |
pmv[1].x = EVEN(pMB->mvs[0].x); |
1981 |
pmv[1].y = EVEN(pMB->mvs[0].y); |
pmv[1].y = EVEN(pMB->mvs[0].y); |
1982 |
pmv[0].x = EVEN(Data->predMV.x); |
pmv[2].x = EVEN(Data->predMV.x); |
1983 |
pmv[0].y = EVEN(Data->predMV.y); |
pmv[2].y = EVEN(Data->predMV.y); |
1984 |
pmv[2].x = pmv[2].y = 0; |
pmv[0].x = pmv[0].y = 0; |
1985 |
|
|
1986 |
|
(*CheckCandidate)(0, 0, 255, &i, Data); |
1987 |
|
|
1988 |
|
//early skip for 0,0 |
1989 |
|
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
1990 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1991 |
|
pMB->mode = MODE_NOT_CODED; |
1992 |
|
return 0; |
1993 |
|
} |
1994 |
|
|
|
CheckCandidate16no4vI(pmv[0].x, pmv[0].y, 255, &i, Data); |
|
1995 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
1996 |
CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data); |
(*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data); |
1997 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
1998 |
CheckCandidate16no4vI(0, 0, mask, &i, Data); |
(*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data); |
1999 |
|
|
2000 |
|
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
2001 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
2002 |
|
|
2003 |
pMB->mvs[0] = pMB->mvs[1] |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
2004 |
= pMB->mvs[2] = pMB->mvs[3] = *Data->currentMV; // all, for future get_pmv() |
pMB->mode = MODE_INTER; |
|
|
|
2005 |
return *(Data->iMinSAD); |
return *(Data->iMinSAD); |
2006 |
} |
} |
2007 |
|
|
2025 |
Data.currentMV = ¤tMV; |
Data.currentMV = ¤tMV; |
2026 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2027 |
Data.iFcode = iFcode; |
Data.iFcode = iFcode; |
2028 |
Data.iQuant = 2; |
CheckCandidate = CheckCandidate16no4vI; |
2029 |
|
|
2030 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2031 |
|
|
2032 |
for (y = 0; y < pParam->mb_height-1; y++) { |
for (y = 1; y < pParam->mb_height-1; y++) { |
2033 |
for (x = 0; x < pParam->mb_width; x++) { |
for (x = 1; x < pParam->mb_width-1; x++) { |
2034 |
int sad, dev; |
int sad, dev; |
2035 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
2036 |
|
|
2037 |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
2038 |
pParam, pMBs, pMB, &Data); |
pParam, pMBs, pMB, &Data); |
2039 |
|
|
|
if ( x != 0 && y != 0 && x != pParam->mb_width-1 ) { //no edge macroblocks, they just don't work |
|
2040 |
if (sad > INTRA_THRESH) { |
if (sad > INTRA_THRESH) { |
2041 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
2042 |
pParam->edged_width); |
pParam->edged_width); |
2043 |
if (dev + INTRA_THRESH < sad) intra++; |
if (dev + INTRA_THRESH < sad) { intra++; pMB->mode = MODE_INTRA; } |
2044 |
if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
2045 |
} |
} |
2046 |
sSAD += sad; |
sSAD += sad; |
2047 |
} |
} |
|
|
|
|
} |
|
2048 |
} |
} |
2049 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2050 |
if (sSAD > INTER_THRESH ) return 1; //P frame |
if (sSAD > INTER_THRESH ) return 1; //P frame |
2077 |
min = -min; |
min = -min; |
2078 |
max += 1; |
max += 1; |
2079 |
if (min > max) max = min; |
if (min > max) max = min; |
2080 |
|
if (pParam->m_quarterpel) max *= 2; |
2081 |
|
|
2082 |
for (i = 1; (max > 32 << (i - 1)); i++); |
for (i = 1; (max > 32 << (i - 1)); i++); |
2083 |
return i; |
return i; |
2084 |
} |
} |
2085 |
|
|
2086 |
|
static void |
2087 |
|
CheckGMC(int x, int y, const int dir, int * iDirection, |
2088 |
|
const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC, |
2089 |
|
const MBParam * const pParam) |
2090 |
|
{ |
2091 |
|
uint32_t mx, my, a, count = 0; |
2092 |
|
|
2093 |
|
for (my = 1; my < pParam->mb_height-1; my++) |
2094 |
|
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
2095 |
|
VECTOR mv; |
2096 |
|
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
2097 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue; |
2098 |
|
mv = pMB->mvs[0]; |
2099 |
|
a = ABS(mv.x - x) + ABS(mv.y - y); |
2100 |
|
if (a < 6) count += 6 - a; |
2101 |
|
} |
2102 |
|
|
2103 |
|
if (count > *bestcount) { |
2104 |
|
*bestcount = count; |
2105 |
|
*iDirection = dir; |
2106 |
|
GMC->x = x; GMC->y = y; |
2107 |
|
} |
2108 |
|
} |
2109 |
|
|
2110 |
|
|
2111 |
|
static VECTOR |
2112 |
|
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
2113 |
|
{ |
2114 |
|
|
2115 |
|
uint32_t count, bestcount = 0; |
2116 |
|
int x, y; |
2117 |
|
VECTOR gmc = {0,0}; |
2118 |
|
int step, min_x, max_x, min_y, max_y; |
2119 |
|
uint32_t mx, my; |
2120 |
|
int iDirection, bDirection; |
2121 |
|
|
2122 |
|
min_x = min_y = -32<<iFcode; |
2123 |
|
max_x = max_y = 32<<iFcode; |
2124 |
|
|
2125 |
|
//step1: let's find a rough camera panning |
2126 |
|
for (step = 32; step >= 2; step /= 2) { |
2127 |
|
bestcount = 0; |
2128 |
|
for (y = min_y; y <= max_y; y += step) |
2129 |
|
for (x = min_x ; x <= max_x; x += step) { |
2130 |
|
count = 0; |
2131 |
|
//for all macroblocks |
2132 |
|
for (my = 1; my < pParam->mb_height-1; my++) |
2133 |
|
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
2134 |
|
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
2135 |
|
VECTOR mv; |
2136 |
|
|
2137 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) |
2138 |
|
continue; |
2139 |
|
|
2140 |
|
mv = pMB->mvs[0]; |
2141 |
|
if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step ) /* GMC translation is always halfpel-res */ |
2142 |
|
count++; |
2143 |
|
} |
2144 |
|
if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; } |
2145 |
|
} |
2146 |
|
min_x = gmc.x - step; |
2147 |
|
max_x = gmc.x + step; |
2148 |
|
min_y = gmc.y - step; |
2149 |
|
max_y = gmc.y + step; |
2150 |
|
|
2151 |
|
} |
2152 |
|
|
2153 |
|
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
2154 |
|
gmc.x = gmc.y = 0; //no camara pan, no GMC |
2155 |
|
|
2156 |
|
// step2: let's refine camera panning using gradiend-descent approach. |
2157 |
|
// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) |
2158 |
|
bestcount = 0; |
2159 |
|
CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2160 |
|
do { |
2161 |
|
x = gmc.x; y = gmc.y; |
2162 |
|
bDirection = iDirection; iDirection = 0; |
2163 |
|
if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2164 |
|
if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2165 |
|
if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2166 |
|
if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2167 |
|
|
2168 |
|
} while (iDirection); |
2169 |
|
|
2170 |
|
if (pParam->m_quarterpel) { |
2171 |
|
gmc.x *= 2; |
2172 |
|
gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */ |
2173 |
|
} |
2174 |
|
|
2175 |
|
return gmc; |
2176 |
|
} |