46 |
int sad; |
int sad; |
47 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
48 |
uint8_t *f_refu, *f_refv, *b_refu, *b_refv; |
uint8_t *f_refu, *f_refv, *b_refu, *b_refv; |
49 |
|
int offset, filter; |
50 |
|
|
51 |
const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { |
const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { |
52 |
NULL, |
NULL, |
55 |
interpolate8x8_halfpel_hv |
interpolate8x8_halfpel_hv |
56 |
}; |
}; |
57 |
|
|
58 |
int offset = (fx>>1) + (fy>>1)*stride; |
if (data->chromaX == fx && data->chromaY == fy && |
59 |
int filter = ((fx & 1) << 1) | (fy & 1); |
data->b_chromaX == bx && data->b_chromaY == by) |
60 |
|
return data->chromaSAD; |
61 |
|
|
62 |
|
offset = (fx>>1) + (fy>>1)*stride; |
63 |
|
filter = ((fx & 1) << 1) | (fy & 1); |
64 |
|
|
65 |
if (filter != 0) { |
if (filter != 0) { |
66 |
f_refu = data->RefQ; |
f_refu = data->RefQ + 64; |
67 |
f_refv = data->RefQ + 8; |
f_refv = data->RefQ + 64 + 8; |
68 |
|
if (data->chromaX != fx || data->chromaY != fy) { |
69 |
interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding); |
interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding); |
70 |
interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding); |
interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding); |
71 |
|
} |
72 |
} else { |
} else { |
73 |
f_refu = (uint8_t*)data->RefP[4] + offset; |
f_refu = (uint8_t*)data->RefP[4] + offset; |
74 |
f_refv = (uint8_t*)data->RefP[5] + offset; |
f_refv = (uint8_t*)data->RefP[5] + offset; |
75 |
} |
} |
76 |
|
data->chromaX = fx; data->chromaY = fy; |
77 |
|
|
78 |
offset = (bx>>1) + (by>>1)*stride; |
offset = (bx>>1) + (by>>1)*stride; |
79 |
filter = ((bx & 1) << 1) | (by & 1); |
filter = ((bx & 1) << 1) | (by & 1); |
80 |
|
|
81 |
if (filter != 0) { |
if (filter != 0) { |
82 |
b_refu = data->RefQ + 16; |
b_refu = data->RefQ + 64 + 16; |
83 |
b_refv = data->RefQ + 24; |
b_refv = data->RefQ + 64 + 24; |
84 |
|
if (data->b_chromaX != bx || data->b_chromaY != by) { |
85 |
interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding); |
86 |
interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding); |
87 |
|
} |
88 |
} else { |
} else { |
89 |
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
b_refu = (uint8_t*)data->b_RefP[4] + offset; |
90 |
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
b_refv = (uint8_t*)data->b_RefP[5] + offset; |
91 |
} |
} |
92 |
|
data->b_chromaX = bx; data->b_chromaY = by; |
93 |
|
|
94 |
sad = sad8bi(data->CurU, b_refu, f_refu, stride); |
sad = sad8bi(data->CurU, b_refu, f_refu, stride); |
95 |
sad += sad8bi(data->CurV, b_refv, f_refv, stride); |
sad += sad8bi(data->CurV, b_refv, f_refv, stride); |
96 |
|
|
97 |
|
data->chromaSAD = sad; |
98 |
return sad; |
return sad; |
99 |
} |
} |
100 |
|
|
157 |
} |
} |
158 |
|
|
159 |
static void |
static void |
|
CheckCandidateInt_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
|
|
{ |
|
|
int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb; |
|
|
uint32_t t; |
|
|
|
|
|
const uint8_t *ReferenceF, *ReferenceB; |
|
|
VECTOR *current; |
|
|
|
|
|
if ((x > data->max_dx) || (x < data->min_dx) || |
|
|
(y > data->max_dy) || (y < data->min_dy)) |
|
|
return; |
|
|
|
|
|
if (Direction == 1) { /* x and y mean forward vector */ |
|
|
VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1]; |
|
|
xb = backward.x; |
|
|
yb = backward.y; |
|
|
xf = x; yf = y; |
|
|
} else { /* x and y mean backward vector */ |
|
|
VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0]; |
|
|
xf = forward.x; |
|
|
yf = forward.y; |
|
|
xb = x; yb = y; |
|
|
} |
|
|
|
|
|
ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data); |
|
|
current = data->currentQMV + Direction - 1; |
|
|
ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data); |
|
|
xcf = xf/2; ycf = yf/2; |
|
|
xcb = xb/2; ycb = yb/2; |
|
|
|
|
|
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
|
|
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
|
|
|
|
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
|
|
sad += (data->lambda16 * t * sad)>>10; |
|
|
|
|
|
if (data->chroma && sad < *data->iMinSAD) |
|
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
|
|
(ycf >> 1) + roundtab_79[ycf & 0x3], |
|
|
(xcb >> 1) + roundtab_79[xcb & 0x3], |
|
|
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
|
|
|
|
|
if (sad < *(data->iMinSAD)) { |
|
|
*data->iMinSAD = sad; |
|
|
current->x = x; current->y = y; |
|
|
data->dir = Direction; |
|
|
} |
|
|
|
|
|
if (sad < *(data->iMinSAD)) { |
|
|
data->iMinSAD2 = *(data->iMinSAD); |
|
|
data->currentQMV2.x = current->x; |
|
|
data->currentQMV2.y = current->y; |
|
|
|
|
|
*data->iMinSAD = sad; |
|
|
current->x = x; current->y = y; |
|
|
} else if (sad < data->iMinSAD2) { |
|
|
data->iMinSAD2 = sad; |
|
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
|
|
} |
|
|
} |
|
|
|
|
|
static void |
|
160 |
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
161 |
{ |
{ |
162 |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
164 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
165 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
166 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
167 |
|
const int blocks[4] = {0, 8, 8*data->iEdgedWidth, 8*data->iEdgedWidth+8}; |
168 |
|
|
169 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
170 |
|
|
188 |
if (data->qpel) { |
if (data->qpel) { |
189 |
xcf += mvs.x/2; ycf += mvs.y/2; |
xcf += mvs.x/2; ycf += mvs.y/2; |
190 |
xcb += b_mvs.x/2; ycb += b_mvs.y/2; |
xcb += b_mvs.x/2; ycb += b_mvs.y/2; |
191 |
|
if (data->qpel_precision) { |
192 |
|
ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); |
193 |
|
ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); |
194 |
|
goto done; |
195 |
|
} |
196 |
|
mvs.x >>=1; mvs.y >>=1; b_mvs.x >>=1; b_mvs.y >>=1; // qpel->hpel |
197 |
} else { |
} else { |
198 |
xcf += mvs.x; ycf += mvs.y; |
xcf += mvs.x; ycf += mvs.y; |
199 |
xcb += b_mvs.x; ycb += b_mvs.y; |
xcb += b_mvs.x; ycb += b_mvs.y; |
|
mvs.x *= 2; mvs.y *= 2; /* we move to qpel precision anyway */ |
|
|
b_mvs.x *= 2; b_mvs.y *= 2; |
|
200 |
} |
} |
201 |
|
ReferenceF = GetReference(mvs.x, mvs.y, data) + blocks[k]; |
202 |
ReferenceF = xvid_me_interpolate8x8qpel(mvs.x, mvs.y, k, 0, data); |
ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data) + blocks[k]; |
203 |
ReferenceB = xvid_me_interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data); |
done: |
204 |
|
sad += data->iMinSAD[k+1] = |
205 |
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
sad8bi(data->Cur + blocks[k], |
206 |
ReferenceF, ReferenceB, data->iEdgedWidth); |
ReferenceF, ReferenceB, data->iEdgedWidth); |
207 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
208 |
} |
} |
250 |
if (data->qpel) { |
if (data->qpel) { |
251 |
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2); |
252 |
xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); |
xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2); |
253 |
|
if (data->qpel_precision) { |
254 |
ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data); |
ReferenceF = xvid_me_interpolate16x16qpel(mvs.x, mvs.y, 0, data); |
255 |
ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
ReferenceB = xvid_me_interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
256 |
|
goto done; |
257 |
|
} |
258 |
|
mvs.x >>=1; mvs.y >>=1; b_mvs.x >>=1; b_mvs.y >>=1; // qpel->hpel |
259 |
} else { |
} else { |
260 |
xcf = 4*mvs.x; ycf = 4*mvs.y; |
xcf = 4*mvs.x; ycf = 4*mvs.y; |
261 |
xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; |
xcb = 4*b_mvs.x; ycb = 4*b_mvs.y; |
262 |
|
} |
263 |
ReferenceF = GetReference(mvs.x, mvs.y, data); |
ReferenceF = GetReference(mvs.x, mvs.y, data); |
264 |
ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); |
ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data); |
|
} |
|
265 |
|
|
266 |
|
done: |
267 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
268 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
269 |
|
|
319 |
} |
} |
320 |
} |
} |
321 |
|
|
|
void |
|
|
CheckCandidate16no4v_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
|
|
{ |
|
|
int32_t sad, xc, yc; |
|
|
const uint8_t * Reference; |
|
|
uint32_t t; |
|
|
|
|
|
if ( (x > data->max_dx) || ( x < data->min_dx) |
|
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|
|
|
|
|
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ |
|
|
|
|
|
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
|
|
|
|
|
xc = x/2; yc = y/2; |
|
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
|
|
data->qpel^data->qpel_precision, data->rrv); |
|
322 |
|
|
323 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
static void |
324 |
sad += (data->lambda16 * t * sad)>>10; |
initialize_searchData(SearchData * Data_d, |
325 |
|
SearchData * Data_f, |
326 |
|
SearchData * Data_b, |
327 |
|
SearchData * Data_i, |
328 |
|
int x, int y, |
329 |
|
const IMAGE * const f_Ref, |
330 |
|
const uint8_t * const f_RefH, |
331 |
|
const uint8_t * const f_RefV, |
332 |
|
const uint8_t * const f_RefHV, |
333 |
|
const IMAGE * const b_Ref, |
334 |
|
const uint8_t * const b_RefH, |
335 |
|
const uint8_t * const b_RefV, |
336 |
|
const uint8_t * const b_RefHV, |
337 |
|
const IMAGE * const pCur, |
338 |
|
const MACROBLOCK * const b_mb) |
339 |
|
{ |
340 |
|
|
341 |
if (data->chroma && sad < *data->iMinSAD) |
/* per-macroblock SearchData initialization - too many things would be repeated 4 times */ |
342 |
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
const uint8_t * RefP[6], * b_RefP[6], * Cur[3]; |
343 |
(yc >> 1) + roundtab_79[yc & 0x3], data); |
const uint32_t iEdgedWidth = Data_d->iEdgedWidth; |
344 |
|
unsigned int lambda; |
345 |
|
int i; |
346 |
|
|
347 |
if (sad < *(data->iMinSAD)) { |
/* luma */ |
348 |
data->iMinSAD2 = *(data->iMinSAD); |
int offset = (x + iEdgedWidth*y) * 16; |
349 |
data->currentQMV2.x = data->currentQMV->x; |
RefP[0] = f_Ref->y + offset; |
350 |
data->currentQMV2.y = data->currentQMV->y; |
RefP[2] = f_RefH + offset; |
351 |
|
RefP[1] = f_RefV + offset; |
352 |
|
RefP[3] = f_RefHV + offset; |
353 |
|
b_RefP[0] = b_Ref->y + offset; |
354 |
|
b_RefP[2] = b_RefH + offset; |
355 |
|
b_RefP[1] = b_RefV + offset; |
356 |
|
b_RefP[3] = b_RefHV + offset; |
357 |
|
Cur[0] = pCur->y + offset; |
358 |
|
|
359 |
|
/* chroma */ |
360 |
|
offset = (x + (iEdgedWidth/2)*y) * 8; |
361 |
|
RefP[4] = f_Ref->u + offset; |
362 |
|
RefP[5] = f_Ref->v + offset; |
363 |
|
b_RefP[4] = b_Ref->u + offset; |
364 |
|
b_RefP[5] = b_Ref->v + offset; |
365 |
|
Cur[1] = pCur->u + offset; |
366 |
|
Cur[2] = pCur->v + offset; |
367 |
|
|
368 |
|
lambda = xvid_me_lambda_vec16[b_mb->quant]; |
369 |
|
|
370 |
|
for (i = 0; i < 6; i++) { |
371 |
|
Data_d->RefP[i] = Data_f->RefP[i] = Data_i->RefP[i] = RefP[i]; |
372 |
|
Data_d->b_RefP[i] = Data_b->RefP[i] = Data_i->b_RefP[i] = b_RefP[i]; |
373 |
|
} |
374 |
|
Data_d->Cur = Data_f->Cur = Data_b->Cur = Data_i->Cur = Cur[0]; |
375 |
|
Data_d->CurU = Data_f->CurU = Data_b->CurU = Data_i->CurU = Cur[1]; |
376 |
|
Data_d->CurV = Data_f->CurV = Data_b->CurV = Data_i->CurV = Cur[2]; |
377 |
|
|
378 |
|
Data_d->lambda16 = Data_f->lambda16 = Data_b->lambda16 = Data_i->lambda16 = lambda; |
379 |
|
|
380 |
|
/* reset chroma-sad cache */ |
381 |
|
Data_d->b_chromaX = Data_d->b_chromaY = Data_d->chromaX = Data_d->chromaY = Data_d->chromaSAD = 256*4096; |
382 |
|
Data_i->b_chromaX = Data_i->b_chromaY = Data_i->chromaX = Data_i->chromaY = Data_i->chromaSAD = 256*4096; |
383 |
|
Data_f->chromaX = Data_f->chromaY = Data_f->chromaSAD = 256*4096; |
384 |
|
Data_b->chromaX = Data_b->chromaY = Data_b->chromaSAD = 256*4096; |
385 |
|
|
386 |
data->iMinSAD[0] = sad; |
*Data_d->iMinSAD = *Data_b->iMinSAD = *Data_f->iMinSAD = *Data_i->iMinSAD = 4096*256; |
|
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
|
|
} else if (sad < data->iMinSAD2) { |
|
|
data->iMinSAD2 = sad; |
|
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
|
|
} |
|
387 |
} |
} |
388 |
|
|
389 |
static __inline VECTOR |
static __inline VECTOR |
397 |
PreparePredictionsBF(VECTOR * const pmv, const int x, const int y, |
PreparePredictionsBF(VECTOR * const pmv, const int x, const int y, |
398 |
const uint32_t iWcount, |
const uint32_t iWcount, |
399 |
const MACROBLOCK * const pMB, |
const MACROBLOCK * const pMB, |
400 |
const uint32_t mode_curr) |
const uint32_t mode_curr, |
401 |
|
const VECTOR hint) |
402 |
{ |
{ |
|
|
|
403 |
/* [0] is prediction */ |
/* [0] is prediction */ |
404 |
pmv[0].x = EVEN(pmv[0].x); pmv[0].y = EVEN(pmv[0].y); |
/* [1] is zero */ |
405 |
|
pmv[1].x = pmv[1].y = 0; |
406 |
|
|
407 |
pmv[1].x = pmv[1].y = 0; /* [1] is zero */ |
pmv[2].x = hint.x; pmv[2].y = hint.y; |
|
|
|
|
pmv[2] = ChoosePred(pMB, mode_curr); |
|
|
pmv[2].x = EVEN(pmv[2].x); pmv[2].y = EVEN(pmv[2].y); |
|
408 |
|
|
409 |
if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */ |
if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */ |
410 |
pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); |
pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); |
|
pmv[3].x = EVEN(pmv[3].x); pmv[3].y = EVEN(pmv[3].y); |
|
411 |
} else pmv[3].x = pmv[3].y = 0; |
} else pmv[3].x = pmv[3].y = 0; |
412 |
|
|
413 |
if (y != 0) { |
if (y != 0) { |
414 |
pmv[4] = ChoosePred(pMB-iWcount, mode_curr); |
pmv[4] = ChoosePred(pMB-iWcount, mode_curr); |
|
pmv[4].x = EVEN(pmv[4].x); pmv[4].y = EVEN(pmv[4].y); |
|
415 |
} else pmv[4].x = pmv[4].y = 0; |
} else pmv[4].x = pmv[4].y = 0; |
416 |
|
|
417 |
if (x != 0) { |
if (x != 0) { |
418 |
pmv[5] = ChoosePred(pMB-1, mode_curr); |
pmv[5] = ChoosePred(pMB-1, mode_curr); |
|
pmv[5].x = EVEN(pmv[5].x); pmv[5].y = EVEN(pmv[5].y); |
|
419 |
} else pmv[5].x = pmv[5].y = 0; |
} else pmv[5].x = pmv[5].y = 0; |
420 |
|
|
421 |
if (x != 0 && y != 0) { |
if (x != 0 && y != 0) { |
422 |
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
|
pmv[6].x = EVEN(pmv[6].x); pmv[6].y = EVEN(pmv[6].y); |
|
423 |
} else pmv[6].x = pmv[6].y = 0; |
} else pmv[6].x = pmv[6].y = 0; |
424 |
} |
} |
425 |
|
|
|
|
|
426 |
/* search backward or forward */ |
/* search backward or forward */ |
427 |
static void |
static void |
428 |
SearchBF( const IMAGE * const pRef, |
SearchBF_initial(const int x, const int y, |
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const int x, const int y, |
|
429 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
430 |
const uint32_t iFcode, |
const uint32_t iFcode, |
431 |
const MBParam * const pParam, |
const MBParam * const pParam, |
433 |
const VECTOR * const predMV, |
const VECTOR * const predMV, |
434 |
int32_t * const best_sad, |
int32_t * const best_sad, |
435 |
const int32_t mode_current, |
const int32_t mode_current, |
436 |
SearchData * const Data) |
SearchData * const Data, |
437 |
|
VECTOR hint) |
438 |
{ |
{ |
439 |
|
|
440 |
int i; |
int i; |
441 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
|
int threshA = (MotionFlags & XVID_ME_FASTREFINE16) ? 150 : 300; |
|
442 |
*Data->iMinSAD = MV_MAX_ERROR; |
*Data->iMinSAD = MV_MAX_ERROR; |
|
Data->iFcode = iFcode; |
|
443 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
|
Data->chromaX = Data->chromaY = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */ |
|
|
|
|
|
Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefP[1] = pRefV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefP[3] = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefP[4] = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
|
Data->RefP[5] = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
444 |
|
|
445 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
446 |
|
|
448 |
pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); |
pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); |
449 |
|
|
450 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
451 |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
if (Data->qpel) { |
452 |
|
pmv[0].x /= 2; pmv[0].y /= 2; |
453 |
|
hint.x /= 2; hint.y /= 2; |
454 |
|
} |
455 |
|
|
456 |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current, hint); |
457 |
|
|
458 |
Data->currentMV->x = Data->currentMV->y = 0; |
Data->currentMV->x = Data->currentMV->y = 0; |
459 |
|
|
473 |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); |
474 |
} |
} |
475 |
|
|
476 |
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
477 |
|
} |
478 |
|
|
479 |
if (Data->qpel && (*Data->iMinSAD < *best_sad + threshA)) { |
static void |
480 |
Data->currentQMV->x = 2*Data->currentMV->x; |
SearchBF_final(const int x, const int y, |
481 |
Data->currentQMV->y = 2*Data->currentMV->y; |
const uint32_t MotionFlags, |
482 |
Data->qpel_precision = 1; |
const MBParam * const pParam, |
483 |
|
int32_t * const best_sad, |
484 |
|
SearchData * const Data) |
485 |
|
{ |
486 |
|
if(!Data->qpel) { |
487 |
|
/* halfpel mode */ |
488 |
|
if (MotionFlags & XVID_ME_HALFPELREFINE16) |
489 |
|
xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate16no4v, 0); |
490 |
|
} else { |
491 |
|
/* qpel mode */ |
492 |
|
if(MotionFlags & XVID_ME_FASTREFINE16) { |
493 |
|
/* fast */ |
494 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
495 |
pParam->width, pParam->height, iFcode, 2, 0); |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
496 |
|
FullRefine_Fast(Data, CheckCandidate16no4v, 0); |
497 |
|
|
498 |
|
} else { |
499 |
|
|
500 |
|
Data->currentQMV->x = 2*Data->currentMV->x; |
501 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
502 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
503 |
if (MotionFlags & XVID_ME_FASTREFINE16) |
/* full */ |
504 |
SubpelRefine_Fast(Data, CheckCandidate16no4v_qpel); |
if (MotionFlags & XVID_ME_HALFPELREFINE16) { |
505 |
else |
xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate16no4v, 0); /* hpel part */ |
506 |
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
Data->currentQMV->x = 2*Data->currentMV->x; |
507 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
508 |
} |
} |
509 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
510 |
|
pParam->width, pParam->height, Data->iFcode, 2, 0); |
511 |
|
Data->qpel_precision = 1; |
512 |
|
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate16no4v, 0); /* qpel part */ |
513 |
} |
} |
|
|
|
|
/* three bits are needed to code backward mode. four for forward */ |
|
|
|
|
|
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
|
|
else *Data->iMinSAD += 3 * Data->lambda16; |
|
|
|
|
|
if (*Data->iMinSAD < *best_sad) { |
|
|
*best_sad = *Data->iMinSAD; |
|
|
pMB->mode = mode_current; |
|
|
if (Data->qpel) { |
|
|
pMB->pmvs[0].x = Data->currentQMV->x - predMV->x; |
|
|
pMB->pmvs[0].y = Data->currentQMV->y - predMV->y; |
|
|
if (mode_current == MODE_FORWARD) |
|
|
pMB->qmvs[0] = *Data->currentQMV; |
|
|
else |
|
|
pMB->b_qmvs[0] = *Data->currentQMV; |
|
|
} else { |
|
|
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
|
|
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
|
514 |
} |
} |
|
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
|
|
else pMB->b_mvs[0] = *Data->currentMV; |
|
515 |
} |
} |
516 |
|
if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
517 |
|
|
|
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
|
|
else *(Data->currentMV+1) = *Data->currentMV; /* we store currmv for interpolate search */ |
|
518 |
} |
} |
519 |
|
|
520 |
static void |
static void |
521 |
SkipDecisionB(const IMAGE * const pCur, |
SkipDecisionB(MACROBLOCK * const pMB, const SearchData * const Data) |
|
const IMAGE * const f_Ref, |
|
|
const IMAGE * const b_Ref, |
|
|
MACROBLOCK * const pMB, |
|
|
const uint32_t x, const uint32_t y, |
|
|
const SearchData * const Data) |
|
522 |
{ |
{ |
523 |
int k; |
int k; |
524 |
|
|
525 |
|
pMB->mode = MODE_DIRECT; /* just to initialize it */ |
526 |
|
|
527 |
if (!Data->chroma) { |
if (!Data->chroma) { |
528 |
int dx = 0, dy = 0, b_dx = 0, b_dy = 0; |
int dx = 0, dy = 0, b_dx = 0, b_dy = 0; |
529 |
int32_t sum; |
int32_t sum; |
542 |
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
543 |
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
544 |
|
|
545 |
sum = sad8bi(pCur->u + 8 * x + 8 * y * stride, |
sum = sad8bi(Data->CurU, |
546 |
f_Ref->u + (y*8 + dy/2) * stride + x*8 + dx/2, |
Data->RefP[4] + (dy/2) * stride + dx/2, |
547 |
b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
Data->b_RefP[4] + (b_dy/2) * stride + b_dx/2, |
548 |
stride); |
stride); |
549 |
|
|
550 |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
551 |
|
|
552 |
sum += sad8bi(pCur->v + 8*x + 8 * y * stride, |
sum += sad8bi(Data->CurV, |
553 |
f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2, |
Data->RefP[5] + (dy/2) * stride + dx/2, |
554 |
b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2, |
Data->b_RefP[5] + (b_dy/2) * stride + b_dx/2, |
555 |
stride); |
stride); |
556 |
|
|
557 |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
558 |
|
} else { |
559 |
|
int sum = Data->chromaSAD; /* chroma-sad SAD caching keeps it there */ |
560 |
|
|
561 |
|
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
562 |
} |
} |
563 |
|
|
564 |
/* skip */ |
/* skip */ |
570 |
} |
} |
571 |
|
|
572 |
static uint32_t |
static uint32_t |
573 |
SearchDirect(const IMAGE * const f_Ref, |
SearchDirect_initial(const int x, const int y, |
|
const uint8_t * const f_RefH, |
|
|
const uint8_t * const f_RefV, |
|
|
const uint8_t * const f_RefHV, |
|
|
const IMAGE * const b_Ref, |
|
|
const uint8_t * const b_RefH, |
|
|
const uint8_t * const b_RefV, |
|
|
const uint8_t * const b_RefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, const int y, |
|
574 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
575 |
const int32_t TRB, const int32_t TRD, |
const int32_t TRB, const int32_t TRD, |
576 |
const MBParam * const pParam, |
const MBParam * const pParam, |
582 |
{ |
{ |
583 |
int32_t skip_sad; |
int32_t skip_sad; |
584 |
int k = (x + Data->iEdgedWidth*y) * 16; |
int k = (x + Data->iEdgedWidth*y) * 16; |
|
MainSearchFunc *MainSearchPtr; |
|
|
CheckFunc * CheckCandidate; |
|
|
|
|
|
*Data->iMinSAD = 256*4096; |
|
|
Data->RefP[0] = f_Ref->y + k; |
|
|
Data->RefP[2] = f_RefH + k; |
|
|
Data->RefP[1] = f_RefV + k; |
|
|
Data->RefP[3] = f_RefHV + k; |
|
|
Data->b_RefP[0] = b_Ref->y + k; |
|
|
Data->b_RefP[2] = b_RefH + k; |
|
|
Data->b_RefP[1] = b_RefV + k; |
|
|
Data->b_RefP[3] = b_RefHV + k; |
|
|
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
|
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
|
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
|
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
585 |
|
|
586 |
k = Data->qpel ? 4 : 2; |
k = Data->qpel ? 4 : 2; |
587 |
Data->max_dx = k * (pParam->width - x * 16); |
Data->max_dx = k * (pParam->width - x * 16); |
590 |
Data->min_dy = -k * (16 + y * 16); |
Data->min_dy = -k * (16 + y * 16); |
591 |
|
|
592 |
Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs; |
Data->referencemv = Data->qpel ? b_mb->qmvs : b_mb->mvs; |
|
Data->qpel_precision = 0; |
|
593 |
|
|
594 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
595 |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
596 |
pMB->b_mvs[k].x = Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD; |
Data->directmvB[k].x = ((TRB - TRD) * Data->referencemv[k].x) / TRD; |
597 |
pMB->mvs[k].y = Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); |
Data->directmvF[k].y = ((TRB * Data->referencemv[k].y) / TRD); |
598 |
pMB->b_mvs[k].y = Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; |
Data->directmvB[k].y = ((TRB - TRD) * Data->referencemv[k].y) / TRD; |
599 |
|
|
600 |
if ( (pMB->b_mvs[k].x > Data->max_dx) | (pMB->b_mvs[k].x < Data->min_dx) |
if ( (Data->directmvB[k].x > Data->max_dx) | (Data->directmvB[k].x < Data->min_dx) |
601 |
| (pMB->b_mvs[k].y > Data->max_dy) | (pMB->b_mvs[k].y < Data->min_dy) ) { |
| (Data->directmvB[k].y > Data->max_dy) | (Data->directmvB[k].y < Data->min_dy) ) { |
602 |
|
|
603 |
*best_sad = 256*4096; /* in that case, we won't use direct mode */ |
Data->iMinSAD[0] = *best_sad = 256*4096; /* in that case, we won't use direct mode */ |
|
pMB->mode = MODE_DIRECT; /* just to make sure it doesn't say "MODE_DIRECT_NONE_MV" */ |
|
|
pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0; |
|
604 |
return 256*4096; |
return 256*4096; |
605 |
} |
} |
606 |
if (b_mb->mode != MODE_INTER4V) { |
if (b_mb->mode != MODE_INTER4V) { |
|
pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0]; |
|
|
pMB->b_mvs[1] = pMB->b_mvs[2] = pMB->b_mvs[3] = pMB->b_mvs[0]; |
|
607 |
Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0]; |
Data->directmvF[1] = Data->directmvF[2] = Data->directmvF[3] = Data->directmvF[0]; |
608 |
Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0]; |
Data->directmvB[1] = Data->directmvB[2] = Data->directmvB[3] = Data->directmvB[0]; |
609 |
break; |
break; |
610 |
} |
} |
611 |
} |
} |
612 |
|
Data->qpel_precision = Data->qpel; /* this initial check is done with full precision, to find real |
613 |
|
SKIP sad */ |
614 |
|
|
615 |
CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v; |
CheckCandidateDirect(0, 0, Data, 255); /* will also fill iMinSAD[1..4] with 8x8 SADs */ |
|
|
|
|
CheckCandidate(0, 0, Data, 255); |
|
616 |
|
|
617 |
/* initial (fast) skip decision */ |
/* initial (fast) skip decision */ |
618 |
if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH) { |
if (Data->iMinSAD[1] < (int)Data->iQuant * INITIAL_SKIP_THRESH |
619 |
|
&& Data->iMinSAD[2] < (int)Data->iQuant * INITIAL_SKIP_THRESH |
620 |
|
&& Data->iMinSAD[3] < (int)Data->iQuant * INITIAL_SKIP_THRESH |
621 |
|
&& Data->iMinSAD[4] < (int)Data->iQuant * INITIAL_SKIP_THRESH) { |
622 |
/* possible skip */ |
/* possible skip */ |
623 |
SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data); |
SkipDecisionB(pMB, Data); |
624 |
if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skipped */ |
if (pMB->mode == MODE_DIRECT_NONE_MV) |
625 |
|
return *Data->iMinSAD; /* skipped */ |
626 |
} |
} |
627 |
|
|
628 |
*Data->iMinSAD += Data->lambda16; |
if (Data->chroma && Data->chromaSAD >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) /* chroma doesn't allow skip */ |
629 |
skip_sad = *Data->iMinSAD; |
skip_sad = 256*4096; |
630 |
|
else |
631 |
|
skip_sad = 4*MAX(MAX(Data->iMinSAD[1],Data->iMinSAD[2]), MAX(Data->iMinSAD[3],Data->iMinSAD[4])); |
632 |
|
|
633 |
if (!(MotionFlags & XVID_ME_SKIP_DELTASEARCH)) { |
Data->currentMV[1].x = Data->directmvF[0].x + Data->currentMV->x; /* hints for forward and backward searches */ |
634 |
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
Data->currentMV[1].y = Data->directmvF[0].y + Data->currentMV->y; |
|
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; |
|
|
else MainSearchPtr = xvid_me_DiamondSearch; |
|
635 |
|
|
636 |
MainSearchPtr(0, 0, Data, 255, CheckCandidate); |
Data->currentMV[2].x = ((Data->currentMV->x == 0) ? |
637 |
|
Data->directmvB[0].x |
638 |
|
: Data->currentMV[1].x - Data->referencemv[0].x); |
639 |
|
|
640 |
|
Data->currentMV[2].y = ((Data->currentMV->y == 0) ? |
641 |
|
Data->directmvB[0].y |
642 |
|
: Data->currentMV[1].y - Data->referencemv[0].y); |
643 |
|
|
644 |
xvid_me_SubpelRefine(Data, CheckCandidate); |
return skip_sad; |
645 |
} |
} |
646 |
|
|
647 |
*best_sad = *Data->iMinSAD; |
static void |
648 |
|
SearchDirect_final( const uint32_t MotionFlags, |
649 |
|
const MACROBLOCK * const b_mb, |
650 |
|
int32_t * const best_sad, |
651 |
|
SearchData * const Data) |
652 |
|
|
653 |
if (Data->qpel || b_mb->mode == MODE_INTER4V) pMB->mode = MODE_DIRECT; |
{ |
654 |
else pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */ |
CheckFunc * CheckCandidate = b_mb->mode == MODE_INTER4V ? |
655 |
|
CheckCandidateDirect : CheckCandidateDirectno4v; |
656 |
|
MainSearchFunc *MainSearchPtr; |
657 |
|
|
658 |
pMB->pmvs[3] = *Data->currentMV; |
if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch; |
659 |
|
else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch; |
660 |
|
else MainSearchPtr = xvid_me_DiamondSearch; |
661 |
|
|
662 |
for (k = 0; k < 4; k++) { |
Data->qpel_precision = 0; |
663 |
pMB->mvs[k].x = Data->directmvF[k].x + Data->currentMV->x; |
MainSearchPtr(0, 0, Data, 255, CheckCandidate); |
664 |
pMB->b_mvs[k].x = ( (Data->currentMV->x == 0) |
|
665 |
? Data->directmvB[k].x |
Data->qpel_precision = Data->qpel; |
|
:pMB->mvs[k].x - Data->referencemv[k].x); |
|
|
pMB->mvs[k].y = (Data->directmvF[k].y + Data->currentMV->y); |
|
|
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
|
|
? Data->directmvB[k].y |
|
|
: pMB->mvs[k].y - Data->referencemv[k].y); |
|
666 |
if (Data->qpel) { |
if (Data->qpel) { |
667 |
pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; |
*Data->iMinSAD = 256*4096; /* this old SAD was not real, it was in hpel precision */ |
668 |
pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; |
CheckCandidate(Data->currentMV->x, Data->currentMV->y, Data, 255); |
|
pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; |
|
|
pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; |
|
669 |
} |
} |
670 |
|
|
671 |
if (b_mb->mode != MODE_INTER4V) { |
xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidate, 0); |
672 |
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
|
673 |
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
if (Data->iMinSAD[0] < *best_sad) { |
674 |
pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; |
*best_sad = Data->iMinSAD[0]; |
|
pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; |
|
|
break; |
|
|
} |
|
675 |
} |
} |
676 |
return skip_sad; |
|
677 |
} |
} |
678 |
|
|
679 |
|
|
680 |
static void set_range(int * range, SearchData * Data) |
static __inline void |
681 |
|
set_range(int * range, SearchData * Data) |
682 |
{ |
{ |
683 |
Data->min_dx = range[0]; |
Data->min_dx = range[0]; |
684 |
Data->max_dx = range[1]; |
Data->max_dx = range[1]; |
687 |
} |
} |
688 |
|
|
689 |
static void |
static void |
690 |
SubpelRefine_dir(SearchData * const data, CheckFunc * const CheckCandidate, const int dir) |
SearchInterpolate_initial( |
|
{ |
|
|
/* Do a half-pel or q-pel refinement */ |
|
|
const VECTOR centerMV = data->qpel_precision ? |
|
|
data->currentQMV[dir-1] : data->currentMV[dir-1]; |
|
|
|
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
|
|
} |
|
|
|
|
|
/* Pretty much redundant code, just as SubpelRefine_dir above too |
|
|
* |
|
|
* TODO: Get rid off all the redundancy (SubpelRefine_Fast_dir, |
|
|
* CheckCandidate16no4v_qpel etc.) */ |
|
|
|
|
|
void |
|
|
SubpelRefine_Fast_dir(SearchData * data, CheckFunc * CheckCandidate, const int dir) |
|
|
{ |
|
|
/* Do a fast q-pel refinement */ |
|
|
VECTOR centerMV; |
|
|
VECTOR second_best; |
|
|
int best_sad = *data->iMinSAD; |
|
|
int xo, yo, xo2, yo2; |
|
|
int size = 2; |
|
|
data->iMinSAD2 = 0; |
|
|
|
|
|
/* check all halfpixel positions near our best halfpel position */ |
|
|
centerMV = data->currentQMV[dir-1]; |
|
|
*data->iMinSAD = 256 * 4096; |
|
|
|
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, dir); |
|
|
|
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, dir); |
|
|
|
|
|
second_best = data->currentQMV[dir-1]; |
|
|
|
|
|
/* after second_best has been found, go back to the vector we began with */ |
|
|
|
|
|
data->currentQMV[dir-1] = centerMV; |
|
|
*data->iMinSAD = best_sad; |
|
|
|
|
|
xo = centerMV.x; |
|
|
yo = centerMV.y; |
|
|
xo2 = second_best.x; |
|
|
yo2 = second_best.y; |
|
|
|
|
|
data->iMinSAD2 = 256 * 4096; |
|
|
|
|
|
if (yo == yo2) { |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
|
|
CHECK_CANDIDATE(xo, yo-1, dir); |
|
|
CHECK_CANDIDATE(xo, yo+1, dir); |
|
|
|
|
|
if(best_sad <= data->iMinSAD2) return; |
|
|
|
|
|
if(data->currentQMV[dir-1].x == data->currentQMV2.x) { |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo-1, dir); |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo+1, dir); |
|
|
} else { |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, |
|
|
(data->currentQMV[dir-1].x == xo) ? data->currentQMV[dir-1].y : data->currentQMV2.y, dir); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
if (xo == xo2) { |
|
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
|
|
CHECK_CANDIDATE(xo-1, yo, dir); |
|
|
CHECK_CANDIDATE(xo+1, yo, dir); |
|
|
|
|
|
if(best_sad < data->iMinSAD2) return; |
|
|
|
|
|
if(data->currentQMV[dir-1].y == data->currentQMV2.y) { |
|
|
CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, dir); |
|
|
CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, dir); |
|
|
} else { |
|
|
CHECK_CANDIDATE((data->currentQMV[dir-1].y == yo) ? data->currentQMV[dir-1].x : data->currentQMV2.x, (yo+yo2)>>1, dir); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
|
|
|
|
|
if(best_sad <= data->iMinSAD2) return; |
|
|
|
|
|
CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, dir); |
|
|
} |
|
|
|
|
|
static void |
|
|
SearchInterpolate(const IMAGE * const f_Ref, |
|
|
const uint8_t * const f_RefH, |
|
|
const uint8_t * const f_RefV, |
|
|
const uint8_t * const f_RefHV, |
|
|
const IMAGE * const b_Ref, |
|
|
const uint8_t * const b_RefH, |
|
|
const uint8_t * const b_RefV, |
|
|
const uint8_t * const b_RefHV, |
|
691 |
const int x, const int y, |
const int x, const int y, |
|
const uint32_t fcode, |
|
|
const uint32_t bcode, |
|
692 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
693 |
const MBParam * const pParam, |
const MBParam * const pParam, |
694 |
const VECTOR * const f_predMV, |
const VECTOR * const f_predMV, |
695 |
const VECTOR * const b_predMV, |
const VECTOR * const b_predMV, |
|
MACROBLOCK * const pMB, |
|
696 |
int32_t * const best_sad, |
int32_t * const best_sad, |
697 |
SearchData * const Data) |
SearchData * const Data, |
698 |
|
const VECTOR startF, |
699 |
|
const VECTOR startB) |
700 |
|
|
701 |
{ |
{ |
|
int i, j; |
|
702 |
int b_range[4], f_range[4]; |
int b_range[4], f_range[4]; |
|
int threshA = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 250 : 500; |
|
|
int threshB = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 150 : 300; |
|
703 |
|
|
704 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
|
*Data->iMinSAD = 4096*256; |
|
|
Data->iFcode = fcode; Data->bFcode = bcode; |
|
|
|
|
|
i = (x + y * Data->iEdgedWidth) * 16; |
|
|
|
|
|
Data->RefP[0] = f_Ref->y + i; |
|
|
Data->RefP[2] = f_RefH + i; |
|
|
Data->RefP[1] = f_RefV + i; |
|
|
Data->RefP[3] = f_RefHV + i; |
|
|
Data->b_RefP[0] = b_Ref->y + i; |
|
|
Data->b_RefP[2] = b_RefH + i; |
|
|
Data->b_RefP[1] = b_RefV + i; |
|
|
Data->b_RefP[3] = b_RefHV + i; |
|
|
Data->RefP[4] = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
|
Data->RefP[5] = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
|
Data->b_RefP[4] = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
|
Data->b_RefP[5] = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8; |
|
705 |
|
|
706 |
Data->predMV = *f_predMV; |
Data->predMV = *f_predMV; |
707 |
Data->bpredMV = *b_predMV; |
Data->bpredMV = *b_predMV; |
708 |
|
|
709 |
Data->currentMV[0] = Data->currentMV[2]; /* forward search left its vector here */ |
Data->currentMV[0] = startF; |
710 |
|
Data->currentMV[1] = startB; |
711 |
|
|
712 |
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, fcode - Data->qpel, 1, 0); |
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, 0); |
713 |
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, bcode - Data->qpel, 1, 0); |
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1, 0); |
714 |
|
|
715 |
if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; |
if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; |
716 |
if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; |
if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; |
726 |
|
|
727 |
CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1); |
CheckCandidateInt(Data->currentMV[0].x, Data->currentMV[0].y, Data, 1); |
728 |
|
|
729 |
|
if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
730 |
|
} |
731 |
|
|
732 |
|
static void |
733 |
|
SearchInterpolate_final(const int x, const int y, |
734 |
|
const uint32_t MotionFlags, |
735 |
|
const MBParam * const pParam, |
736 |
|
int32_t * const best_sad, |
737 |
|
SearchData * const Data) |
738 |
|
{ |
739 |
|
int i, j; |
740 |
|
int b_range[4], f_range[4]; |
741 |
|
|
742 |
|
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, 0); |
743 |
|
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1, 0); |
744 |
|
|
745 |
/* diamond */ |
/* diamond */ |
746 |
do { |
do { |
747 |
Data->dir = 0; |
Data->dir = 0; |
768 |
|
|
769 |
/* qpel refinement */ |
/* qpel refinement */ |
770 |
if (Data->qpel) { |
if (Data->qpel) { |
|
if (*Data->iMinSAD > *best_sad + threshA) return; |
|
771 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
772 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0); |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
773 |
|
x, y, 4, pParam->width, pParam->height, Data->iFcode, 2, 0); |
774 |
|
|
775 |
Data->currentQMV[0].x = 2 * Data->currentMV[0].x; |
Data->currentQMV[0].x = 2 * Data->currentMV[0].x; |
776 |
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
778 |
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
779 |
|
|
780 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
781 |
if (MotionFlags & XVID_ME_FASTREFINE16) |
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateInt, 1); |
782 |
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 1); |
|
783 |
else |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
784 |
SubpelRefine_dir(Data, CheckCandidateInt, 1); |
x, y, 4, pParam->width, pParam->height, Data->bFcode, 2, 0); |
785 |
|
|
786 |
|
xvid_me_SubpelRefine(Data->currentQMV[1], Data, CheckCandidateInt, 2); |
787 |
|
} |
788 |
} |
} |
789 |
|
|
790 |
if (*Data->iMinSAD > *best_sad + threshB) return; |
if (Data->iMinSAD[0] < *best_sad) *best_sad = Data->iMinSAD[0]; |
791 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
} |
792 |
|
|
793 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
static void |
794 |
if (MotionFlags & XVID_ME_FASTREFINE16) |
ModeDecision_BVOP_SAD(const SearchData * const Data_d, |
795 |
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 2); |
const SearchData * const Data_b, |
796 |
else |
const SearchData * const Data_f, |
797 |
SubpelRefine_dir(Data, CheckCandidateInt, 2); |
const SearchData * const Data_i, |
798 |
|
MACROBLOCK * const pMB, |
799 |
|
const MACROBLOCK * const b_mb, |
800 |
|
VECTOR * f_predMV, |
801 |
|
VECTOR * b_predMV) |
802 |
|
{ |
803 |
|
int mode = MODE_DIRECT, k; |
804 |
|
int best_sad, f_sad, b_sad, i_sad; |
805 |
|
const int qpel = Data_d->qpel; |
806 |
|
|
807 |
|
/* evaluate cost of all modes - quite simple in SAD */ |
808 |
|
best_sad = Data_d->iMinSAD[0] + 1*Data_d->lambda16; |
809 |
|
b_sad = Data_b->iMinSAD[0] + 3*Data_d->lambda16; |
810 |
|
f_sad = Data_f->iMinSAD[0] + 4*Data_d->lambda16; |
811 |
|
i_sad = Data_i->iMinSAD[0] + 2*Data_d->lambda16; |
812 |
|
|
813 |
|
if (b_sad < best_sad) { |
814 |
|
mode = MODE_BACKWARD; |
815 |
|
best_sad = b_sad; |
816 |
} |
} |
817 |
|
|
818 |
|
if (f_sad < best_sad) { |
819 |
|
mode = MODE_FORWARD; |
820 |
|
best_sad = f_sad; |
821 |
} |
} |
822 |
|
|
823 |
*Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
if (i_sad < best_sad) { |
824 |
|
mode = MODE_INTERPOLATE; |
825 |
|
best_sad = i_sad; |
826 |
|
} |
827 |
|
|
828 |
if (*Data->iMinSAD < *best_sad) { |
pMB->sad16 = best_sad; |
829 |
*best_sad = *Data->iMinSAD; |
pMB->mode = mode; |
830 |
pMB->mvs[0] = Data->currentMV[0]; |
|
831 |
pMB->b_mvs[0] = Data->currentMV[1]; |
switch (mode) { |
832 |
pMB->mode = MODE_INTERPOLATE; |
|
833 |
if (Data->qpel) { |
case MODE_DIRECT: |
834 |
pMB->qmvs[0] = Data->currentQMV[0]; |
if (!qpel && b_mb->mode != MODE_INTER4V) pMB->mode = MODE_DIRECT_NO4V; /* for faster compensation */ |
835 |
pMB->b_qmvs[0] = Data->currentQMV[1]; |
|
836 |
|
pMB->pmvs[3] = Data_d->currentMV[0]; |
837 |
|
|
838 |
|
for (k = 0; k < 4; k++) { |
839 |
|
pMB->mvs[k].x = Data_d->directmvF[k].x + Data_d->currentMV->x; |
840 |
|
pMB->b_mvs[k].x = ( (Data_d->currentMV->x == 0) |
841 |
|
? Data_d->directmvB[k].x |
842 |
|
:pMB->mvs[k].x - Data_d->referencemv[k].x); |
843 |
|
pMB->mvs[k].y = (Data_d->directmvF[k].y + Data_d->currentMV->y); |
844 |
|
pMB->b_mvs[k].y = ((Data_d->currentMV->y == 0) |
845 |
|
? Data_d->directmvB[k].y |
846 |
|
: pMB->mvs[k].y - Data_d->referencemv[k].y); |
847 |
|
if (qpel) { |
848 |
|
pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; |
849 |
|
pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; |
850 |
|
pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; |
851 |
|
pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; |
852 |
|
} |
853 |
|
|
854 |
|
if (b_mb->mode != MODE_INTER4V) { |
855 |
|
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
856 |
|
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
857 |
|
pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; |
858 |
|
pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; |
859 |
|
break; |
860 |
|
} |
861 |
|
} |
862 |
|
break; |
863 |
|
|
864 |
|
case MODE_FORWARD: |
865 |
|
if (qpel) { |
866 |
|
pMB->pmvs[0].x = Data_f->currentQMV->x - f_predMV->x; |
867 |
|
pMB->pmvs[0].y = Data_f->currentQMV->y - f_predMV->y; |
868 |
|
pMB->qmvs[0] = *Data_f->currentQMV; |
869 |
|
*f_predMV = Data_f->currentQMV[0]; |
870 |
|
} else { |
871 |
|
pMB->pmvs[0].x = Data_f->currentMV->x - f_predMV->x; |
872 |
|
pMB->pmvs[0].y = Data_f->currentMV->y - f_predMV->y; |
873 |
|
*f_predMV = Data_f->currentMV[0]; |
874 |
|
} |
875 |
|
pMB->mvs[0] = *Data_f->currentMV; |
876 |
|
pMB->b_mvs[0] = *Data_b->currentMV; /* hint for future searches */ |
877 |
|
break; |
878 |
|
|
879 |
|
case MODE_BACKWARD: |
880 |
|
if (qpel) { |
881 |
|
pMB->pmvs[0].x = Data_b->currentQMV->x - b_predMV->x; |
882 |
|
pMB->pmvs[0].y = Data_b->currentQMV->y - b_predMV->y; |
883 |
|
pMB->b_qmvs[0] = *Data_b->currentQMV; |
884 |
|
*b_predMV = Data_b->currentQMV[0]; |
885 |
|
} else { |
886 |
|
pMB->pmvs[0].x = Data_b->currentMV->x - b_predMV->x; |
887 |
|
pMB->pmvs[0].y = Data_b->currentMV->y - b_predMV->y; |
888 |
|
*b_predMV = Data_b->currentMV[0]; |
889 |
|
} |
890 |
|
pMB->b_mvs[0] = *Data_b->currentMV; |
891 |
|
pMB->mvs[0] = *Data_f->currentMV; /* hint for future searches */ |
892 |
|
break; |
893 |
|
|
894 |
|
|
895 |
|
case MODE_INTERPOLATE: |
896 |
|
pMB->mvs[0] = Data_i->currentMV[0]; |
897 |
|
pMB->b_mvs[0] = Data_i->currentMV[1]; |
898 |
|
if (qpel) { |
899 |
|
pMB->qmvs[0] = Data_i->currentQMV[0]; |
900 |
|
pMB->b_qmvs[0] = Data_i->currentQMV[1]; |
901 |
pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
902 |
pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; |
pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; |
903 |
pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; |
pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; |
904 |
pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; |
pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; |
905 |
|
*f_predMV = Data_i->currentQMV[0]; |
906 |
|
*b_predMV = Data_i->currentQMV[1]; |
907 |
} else { |
} else { |
908 |
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
909 |
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
910 |
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
911 |
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
912 |
|
*f_predMV = Data_i->currentMV[0]; |
913 |
|
*b_predMV = Data_i->currentMV[1]; |
914 |
} |
} |
915 |
|
break; |
916 |
} |
} |
917 |
} |
} |
918 |
|
|
935 |
const IMAGE * const b_refHV) |
const IMAGE * const b_refHV) |
936 |
{ |
{ |
937 |
uint32_t i, j; |
uint32_t i, j; |
938 |
int32_t best_sad; |
int32_t best_sad, sad2; |
939 |
uint32_t skip_sad; |
uint32_t skip_sad; |
940 |
|
|
941 |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
|
MACROBLOCK *const pMBs = frame->mbs; |
|
942 |
|
|
943 |
VECTOR f_predMV, b_predMV; |
VECTOR f_predMV, b_predMV; |
944 |
|
|
945 |
const int32_t TRB = time_pp - time_bp; |
const int32_t TRB = time_pp - time_bp; |
946 |
const int32_t TRD = time_pp; |
const int32_t TRD = time_pp; |
947 |
|
DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); |
948 |
|
|
949 |
/* some pre-inintialized data for the rest of the search */ |
/* some pre-inintialized data for the rest of the search */ |
950 |
|
SearchData Data_d, Data_f, Data_b, Data_i; |
951 |
|
memset(&Data_d, 0, sizeof(SearchData)); |
952 |
|
|
953 |
SearchData Data; |
Data_d.iEdgedWidth = pParam->edged_width; |
954 |
memset(&Data, 0, sizeof(SearchData)); |
Data_d.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; |
955 |
|
Data_d.rounding = 0; |
956 |
|
Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
957 |
|
Data_d.iQuant = frame->quant; |
958 |
|
Data_d.dctSpace = dct_space; |
959 |
|
Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
960 |
|
Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices; |
961 |
|
|
962 |
|
Data_d.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ |
963 |
|
|
964 |
|
memcpy(&Data_f, &Data_d, sizeof(SearchData)); |
965 |
|
memcpy(&Data_b, &Data_d, sizeof(SearchData)); |
966 |
|
memcpy(&Data_i, &Data_d, sizeof(SearchData)); |
967 |
|
|
968 |
Data.iEdgedWidth = pParam->edged_width; |
Data_f.iFcode = Data_i.iFcode = frame->fcode; |
969 |
Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; |
Data_b.iFcode = Data_i.bFcode = frame->bcode; |
|
Data.rounding = 0; |
|
|
Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
|
|
Data.iQuant = frame->quant; |
|
970 |
|
|
|
Data.RefQ = f_refV->u; /* a good place, also used in MC (for similar purpose) */ |
|
971 |
|
|
|
/* note: i==horizontal, j==vertical */ |
|
972 |
for (j = 0; j < pParam->mb_height; j++) { |
for (j = 0; j < pParam->mb_height; j++) { |
973 |
|
|
974 |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
976 |
for (i = 0; i < pParam->mb_width; i++) { |
for (i = 0; i < pParam->mb_width; i++) { |
977 |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
978 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
979 |
int interpol_search = 0; |
pMB->mode = -1; |
980 |
int bf_search = 0; |
|
981 |
int bf_thresh = 0; |
initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, |
982 |
|
i, j, f_ref, f_refH->y, f_refV->y, f_refHV->y, |
983 |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
984 |
|
&frame->image, b_mb); |
985 |
|
|
986 |
/* special case, if collocated block is SKIPed in P-VOP: 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 */ |
987 |
if (b_reference->coding_type != S_VOP) |
if (b_reference->coding_type != S_VOP) |
992 |
continue; |
continue; |
993 |
} |
} |
994 |
|
|
|
Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant]; |
|
|
|
|
|
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
|
|
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
|
|
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
|
|
|
|
995 |
/* direct search comes first, because it (1) checks for SKIP-mode |
/* direct search comes first, because it (1) checks for SKIP-mode |
996 |
and (2) sets very good predictions for forward and backward search */ |
and (2) sets very good predictions for forward and backward search */ |
997 |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
skip_sad = SearchDirect_initial(i, j, frame->motion_flags, TRB, TRD, pParam, pMB, |
998 |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
b_mb, &best_sad, &Data_d); |
|
&frame->image, |
|
|
i, j, |
|
|
frame->motion_flags, |
|
|
TRB, TRD, |
|
|
pParam, |
|
|
pMB, b_mb, |
|
|
&best_sad, |
|
|
&Data); |
|
999 |
|
|
1000 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { |
if (pMB->mode == MODE_DIRECT_NONE_MV) { |
1001 |
pMB->sad16 = best_sad; |
pMB->sad16 = best_sad; |
1002 |
continue; |
continue; |
1003 |
} |
} |
1004 |
|
|
1005 |
if (frame->motion_flags & XVID_ME_BFRAME_EARLYSTOP) { |
SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, |
1006 |
if(i > 0 && j > 0 && i < pParam->mb_width) { |
&f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]); |
|
bf_thresh = MIN((&pMBs[(i-1) + j * pParam->mb_width])->sad16, |
|
|
MIN((&pMBs[i + (j-1) * pParam->mb_width])->sad16, |
|
|
(&pMBs[(i+1) + (j-1) * pParam->mb_width])->sad16)); |
|
|
|
|
|
if (((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_FORWARD) && |
|
|
((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_BACKWARD) && |
|
|
((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_INTERPOLATE)) |
|
|
bf_search++; |
|
|
|
|
|
if (((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) && |
|
|
((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) && |
|
|
((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE)) |
|
|
bf_search++; |
|
|
|
|
|
if (((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) && |
|
|
((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) && |
|
|
((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE)) |
|
|
bf_search++; |
|
|
} |
|
1007 |
|
|
1008 |
if ((best_sad < bf_thresh) && (bf_search == 3)) |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
1009 |
continue; |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); |
|
} |
|
1010 |
|
|
1011 |
/* forward search */ |
sad2 = best_sad; |
|
SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
|
|
i, j, |
|
|
frame->motion_flags, |
|
|
frame->fcode, pParam, |
|
|
pMB, &f_predMV, &best_sad, |
|
|
MODE_FORWARD, &Data); |
|
|
|
|
|
/* backward search */ |
|
|
SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y, |
|
|
i, j, |
|
|
frame->motion_flags, |
|
|
frame->bcode, pParam, |
|
|
pMB, &b_predMV, &best_sad, |
|
|
MODE_BACKWARD, &Data); |
|
|
|
|
|
/* interpolate search comes last, because it uses data from forward and backward as prediction */ |
|
|
if (frame->motion_flags & XVID_ME_FAST_MODEINTERPOLATE) { |
|
|
|
|
|
if(i > 0 && j > 0 && i < pParam->mb_width) { |
|
|
if ((&pMBs[(i-1) + j * pParam->mb_width])->mode == MODE_INTERPOLATE) |
|
|
interpol_search++; |
|
|
if ((&pMBs[i + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE) |
|
|
interpol_search++; |
|
|
if ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode == MODE_INTERPOLATE) |
|
|
interpol_search++; |
|
|
} |
|
|
else |
|
|
interpol_search = 1; |
|
1012 |
|
|
1013 |
interpol_search |= !(best_sad < 3 * Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2)); |
if (Data_f.iMinSAD[0] < 2*sad2+2000) |
1014 |
} |
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f); |
|
else |
|
|
interpol_search = 1; |
|
1015 |
|
|
1016 |
if (interpol_search) { |
if (Data_b.iMinSAD[0] < 2*sad2+2000) |
1017 |
SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b); |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
|
|
i, j, |
|
|
frame->fcode, frame->bcode, |
|
|
frame->motion_flags, |
|
|
pParam, |
|
|
&f_predMV, &b_predMV, |
|
|
pMB, &best_sad, |
|
|
&Data); |
|
|
} |
|
1018 |
|
|
1019 |
/* final skip decision */ |
SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad, |
1020 |
if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2) ) |
&Data_i, Data_f.currentMV[0], Data_b.currentMV[0]); |
|
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) |
|
1021 |
|
|
1022 |
SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data); |
if (((Data_i.iMinSAD[0] < 2*best_sad+2000) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) |
1023 |
|
|| Data_i.iMinSAD[0] <= best_sad) |
1024 |
|
|
1025 |
switch (pMB->mode) { |
SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i); |
1026 |
case MODE_FORWARD: |
|
1027 |
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
if ((Data_d.iMinSAD[0] <= 2*best_sad) && (!frame->motion_flags&XVID_ME_SKIP_DELTASEARCH)) |
1028 |
pMB->sad16 = best_sad; |
SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); |
1029 |
break; |
|
1030 |
case MODE_BACKWARD: |
/* final skip decision */ |
1031 |
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
if ( (skip_sad < 2 * Data_d.iQuant * MAX_SAD00_FOR_SKIP ) |
1032 |
pMB->sad16 = best_sad; |
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) { |
1033 |
break; |
|
1034 |
case MODE_INTERPOLATE: |
if (!Data_d.chroma) { /* we still need to check chroma */ |
1035 |
f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0]; |
SkipDecisionB(pMB, &Data_d); |
1036 |
b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0]; |
if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */ |
1037 |
pMB->sad16 = best_sad; |
pMB->sad16 = skip_sad; |
1038 |
break; |
continue; |
1039 |
default: |
} |
1040 |
pMB->sad16 = best_sad; |
} else { /* just SKIP */ |
1041 |
break; |
pMB->mode = MODE_DIRECT_NONE_MV; |
1042 |
|
pMB->sad16 = skip_sad; |
1043 |
|
continue; |
1044 |
|
} |
1045 |
} |
} |
1046 |
|
|
1047 |
|
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
1048 |
|
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
1049 |
|
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, pParam, i, j); |
1050 |
|
else |
1051 |
|
ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV); |
1052 |
|
|
1053 |
} |
} |
1054 |
} |
} |
1055 |
} |
} |