78 |
****************************************************************************/ |
****************************************************************************/ |
79 |
|
|
80 |
int32_t |
int32_t |
81 |
xvid_me_ChromaSAD(const int dx, const int dy, const SearchData * const data) |
xvid_me_ChromaSAD(const int dx, const int dy, SearchData * const data) |
82 |
{ |
{ |
83 |
int sad; |
int sad; |
84 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
85 |
int offset = (dx>>1) + (dy>>1)*stride; |
int offset = (dx>>1) + (dy>>1)*stride; |
86 |
int next = 1; |
int next = 1; |
87 |
|
|
88 |
if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; /* it has been checked recently */ |
if (dx == data->chromaX && dy == data->chromaY) |
89 |
data->temp[5] = dx; data->temp[6] = dy; /* backup */ |
return data->chromaSAD; /* it has been checked recently */ |
90 |
|
data->chromaX = dx; data->chromaY = dy; /* backup */ |
91 |
|
|
92 |
switch (((dx & 1) << 1) | (dy & 1)) { |
switch (((dx & 1) << 1) | (dy & 1)) { |
93 |
case 0: |
case 0: |
108 |
sad += sad8(data->CurV, data->RefQ, stride); |
sad += sad8(data->CurV, data->RefQ, stride); |
109 |
break; |
break; |
110 |
} |
} |
111 |
data->temp[7] = sad; /* backup, part 2 */ |
data->chromaSAD = sad; /* backup, part 2 */ |
112 |
return sad; |
return sad; |
113 |
} |
} |
114 |
|
|
207 |
} |
} |
208 |
|
|
209 |
void |
void |
210 |
xvid_me_AdvDiamondSearch(int x, int y, const SearchData * const data, |
xvid_me_AdvDiamondSearch(int x, int y, SearchData * const data, |
211 |
int bDirection, CheckFunc * const CheckCandidate) |
int bDirection, CheckFunc * const CheckCandidate) |
212 |
{ |
{ |
213 |
|
|
214 |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
215 |
|
|
216 |
unsigned int * const iDirection = data->dir; |
unsigned int * const iDirection = &data->dir; |
217 |
|
|
218 |
for(;;) { /* forever */ |
for(;;) { /* forever */ |
219 |
*iDirection = 0; |
*iDirection = 0; |
293 |
} |
} |
294 |
|
|
295 |
void |
void |
296 |
xvid_me_SquareSearch(int x, int y, const SearchData * const data, |
xvid_me_SquareSearch(int x, int y, SearchData * const data, |
297 |
int bDirection, CheckFunc * const CheckCandidate) |
int bDirection, CheckFunc * const CheckCandidate) |
298 |
{ |
{ |
299 |
unsigned int * const iDirection = data->dir; |
unsigned int * const iDirection = &data->dir; |
300 |
|
|
301 |
do { |
do { |
302 |
*iDirection = 0; |
*iDirection = 0; |
315 |
} |
} |
316 |
|
|
317 |
void |
void |
318 |
xvid_me_DiamondSearch(int x, int y, const SearchData * const data, |
xvid_me_DiamondSearch(int x, int y, SearchData * const data, |
319 |
int bDirection, CheckFunc * const CheckCandidate) |
int bDirection, CheckFunc * const CheckCandidate) |
320 |
{ |
{ |
321 |
|
|
322 |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */ |
323 |
|
|
324 |
unsigned int * const iDirection = data->dir; |
unsigned int * const iDirection = &data->dir; |
325 |
|
|
326 |
do { |
do { |
327 |
*iDirection = 0; |
*iDirection = 0; |
331 |
if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8); |
if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8); |
332 |
|
|
333 |
/* now we're doing diagonal checks near our candidate */ |
/* now we're doing diagonal checks near our candidate */ |
|
|
|
|
if (*iDirection) { /* checking if anything found */ |
|
334 |
bDirection = *iDirection; |
bDirection = *iDirection; |
335 |
*iDirection = 0; |
if (*iDirection) { /* checking if anything found */ |
336 |
x = data->currentMV->x; y = data->currentMV->y; |
x = data->currentMV->x; y = data->currentMV->y; |
337 |
if (bDirection & 3) { /* our candidate is left or right */ |
if (bDirection & 3) { /* our candidate is left or right */ |
338 |
|
*iDirection=4+8; |
339 |
CHECK_CANDIDATE(x, y + iDiamondSize, 8); |
CHECK_CANDIDATE(x, y + iDiamondSize, 8); |
340 |
CHECK_CANDIDATE(x, y - iDiamondSize, 4); |
CHECK_CANDIDATE(x, y - iDiamondSize, 4); |
341 |
} else { /* what remains here is up or down */ |
} else { /* what remains here is up or down */ |
342 |
|
*iDirection=1+2; |
343 |
CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
344 |
CHECK_CANDIDATE(x - iDiamondSize, y, 1); |
CHECK_CANDIDATE(x - iDiamondSize, y, 1); |
345 |
} |
} |
347 |
x = data->currentMV->x; y = data->currentMV->y; |
x = data->currentMV->x; y = data->currentMV->y; |
348 |
} |
} |
349 |
} |
} |
350 |
while (*iDirection); |
while (bDirection); |
351 |
} |
} |
352 |
|
|
353 |
void |
void |
354 |
xvid_me_SubpelRefine(const SearchData * const data, CheckFunc * const CheckCandidate) |
xvid_me_SubpelRefine(VECTOR centerMV, SearchData * const data, CheckFunc * const CheckCandidate, int dir) |
355 |
{ |
{ |
356 |
/* Do a half-pel or q-pel refinement */ |
/* Do a half-pel or q-pel refinement */ |
|
const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV; |
|
357 |
|
|
358 |
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0); |
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir); |
359 |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0); |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir); |
360 |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, 0); |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir); |
361 |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, 0); |
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir); |
362 |
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, 0); |
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir); |
363 |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, 0); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir); |
364 |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, 0); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir); |
365 |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, 0); |
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
366 |
|
} |
367 |
|
|
368 |
|
#define CHECK_CANDIDATE_2ndBEST(X, Y, DIR) { \ |
369 |
|
*data->iMinSAD = s_best2; \ |
370 |
|
CheckCandidate((X),(Y), data, direction); \ |
371 |
|
if (data->iMinSAD[0] < s_best) { \ |
372 |
|
s_best2 = s_best; \ |
373 |
|
s_best = data->iMinSAD[0]; \ |
374 |
|
v_best2 = v_best; \ |
375 |
|
v_best.x = X; v_best.y = Y; \ |
376 |
|
dir = DIR; \ |
377 |
|
} else if (data->iMinSAD[0] < s_best2) { \ |
378 |
|
s_best2 = data->iMinSAD[0]; \ |
379 |
|
v_best2.x = X; v_best2.y = Y; \ |
380 |
|
} \ |
381 |
} |
} |
382 |
|
|
383 |
|
void |
384 |
|
FullRefine_Fast(SearchData * data, CheckFunc * CheckCandidate, int direction) |
385 |
|
{ |
386 |
|
/* Do a fast h-pel and then q-pel refinement */ |
387 |
|
|
388 |
|
int32_t s_best = data->iMinSAD[0], s_best2 = 256*4096; |
389 |
|
VECTOR v_best, v_best2; |
390 |
|
int dir = 0, xo2, yo2, best_halfpel, b_cbp; |
391 |
|
|
392 |
|
int xo = 2*data->currentMV[0].x, yo = 2*data->currentMV[0].y; |
393 |
|
|
394 |
|
data->currentQMV[0].x = v_best.x = xo; |
395 |
|
data->currentQMV[0].y = v_best.y = yo; |
396 |
|
|
397 |
|
data->qpel_precision = 1; |
398 |
|
|
399 |
|
/* halfpel refinement: check 8 neighbours, but keep the second best SAD as well */ |
400 |
|
CHECK_CANDIDATE_2ndBEST(xo - 2, yo, 1+16+64); |
401 |
|
CHECK_CANDIDATE_2ndBEST(xo + 2, yo, 2+32+128); |
402 |
|
CHECK_CANDIDATE_2ndBEST(xo, yo - 2, 4+16+32); |
403 |
|
CHECK_CANDIDATE_2ndBEST(xo, yo + 2, 8+64+128); |
404 |
|
CHECK_CANDIDATE_2ndBEST(xo - 2, yo - 2, 1+4+16+32+64); |
405 |
|
CHECK_CANDIDATE_2ndBEST(xo + 2, yo - 2, 2+4+16+32+128); |
406 |
|
CHECK_CANDIDATE_2ndBEST(xo - 2, yo + 2, 1+8+16+64+128); |
407 |
|
CHECK_CANDIDATE_2ndBEST(xo + 2, yo + 2, 2+8+32+64+128); |
408 |
|
|
409 |
|
xo = v_best.x; yo = v_best.y, b_cbp = data->cbp[0]; |
410 |
|
|
411 |
|
/* we need all 8 neighbours *of best hpel position found above* checked for 2nd best |
412 |
|
let's check the missing ones */ |
413 |
|
|
414 |
|
/* on rare occasions, 1st best and 2nd best are far away, and 2nd best is not 1st best's neighbour. |
415 |
|
to simplify stuff, we'll forget that evil 2nd best and make a full search for a new 2nd best */ |
416 |
|
/* todo. we should check the missing neighbours first, maybe they'll give us 2nd best which is even better |
417 |
|
than the infamous one. in that case, we will not have to re-check the other neighbours */ |
418 |
|
|
419 |
|
if (abs(v_best.x - v_best2.x) > 2 || abs(v_best.y - v_best2.y) > 2) { /* v_best2 is useless */ |
420 |
|
data->iMinSAD[0] = 256*4096; |
421 |
|
dir = ~0; /* all */ |
422 |
|
} else { |
423 |
|
data->iMinSAD[0] = s_best2; |
424 |
|
data->currentQMV[0] = v_best2; |
425 |
|
} |
426 |
|
|
427 |
|
if (dir & 1) CHECK_CANDIDATE( xo - 2, yo, direction); |
428 |
|
if (dir & 2) CHECK_CANDIDATE( xo + 2, yo, direction); |
429 |
|
if (dir & 4) CHECK_CANDIDATE( xo, yo - 2, direction); |
430 |
|
if (dir & 8) CHECK_CANDIDATE( xo, yo + 2, direction); |
431 |
|
if (dir & 16) CHECK_CANDIDATE( xo - 2, yo - 2, direction); |
432 |
|
if (dir & 32) CHECK_CANDIDATE( xo + 2, yo - 2, direction); |
433 |
|
if (dir & 64) CHECK_CANDIDATE( xo - 2, yo + 2, direction); |
434 |
|
if (dir & 128) CHECK_CANDIDATE( xo + 2, yo + 2, direction); |
435 |
|
|
436 |
|
/* read the position of 2nd best */ |
437 |
|
v_best2 = data->currentQMV[0]; |
438 |
|
|
439 |
|
/* after second_best has been found, go back to best vector */ |
440 |
|
|
441 |
|
data->currentQMV[0].x = xo; |
442 |
|
data->currentQMV[0].y = yo; |
443 |
|
data->cbp[0] = b_cbp; |
444 |
|
|
445 |
|
data->currentMV[0].x = xo/2; |
446 |
|
data->currentMV[0].y = yo/2; |
447 |
|
data->iMinSAD[0] = best_halfpel = s_best; |
448 |
|
|
449 |
|
xo2 = v_best2.x; |
450 |
|
yo2 = v_best2.y; |
451 |
|
s_best2 = 256*4096; |
452 |
|
|
453 |
|
if (yo == yo2) { |
454 |
|
CHECK_CANDIDATE_2ndBEST((xo+xo2)>>1, yo, 0); |
455 |
|
CHECK_CANDIDATE_2ndBEST(xo, yo-1, 0); |
456 |
|
CHECK_CANDIDATE_2ndBEST(xo, yo+1, 0); |
457 |
|
data->currentQMV[0] = v_best; |
458 |
|
data->iMinSAD[0] = s_best; |
459 |
|
|
460 |
|
if(best_halfpel <= s_best2) return; |
461 |
|
|
462 |
|
if(data->currentQMV[0].x == v_best2.x) { |
463 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo-1, direction); |
464 |
|
CHECK_CANDIDATE((xo+xo2)>>1, yo+1, direction); |
465 |
|
} else { |
466 |
|
CHECK_CANDIDATE((xo+xo2)>>1, |
467 |
|
(data->currentQMV[0].x == xo) ? data->currentQMV[0].y : v_best2.y, direction); |
468 |
|
} |
469 |
|
return; |
470 |
|
} |
471 |
|
|
472 |
|
if (xo == xo2) { |
473 |
|
CHECK_CANDIDATE_2ndBEST(xo, (yo+yo2)>>1, 0); |
474 |
|
CHECK_CANDIDATE_2ndBEST(xo-1, yo, 0); |
475 |
|
CHECK_CANDIDATE_2ndBEST(xo+1, yo, 0); |
476 |
|
data->currentQMV[0] = v_best; |
477 |
|
data->iMinSAD[0] = s_best; |
478 |
|
|
479 |
|
if(best_halfpel <= s_best2) return; |
480 |
|
|
481 |
|
if(data->currentQMV[0].y == v_best2.y) { |
482 |
|
CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, direction); |
483 |
|
CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, direction); |
484 |
|
} else { |
485 |
|
CHECK_CANDIDATE((data->currentQMV[0].y == yo) ? data->currentQMV[0].x : v_best2.x, (yo+yo2)>>1, direction); |
486 |
|
} |
487 |
|
return; |
488 |
|
} |
489 |
|
|
490 |
|
CHECK_CANDIDATE_2ndBEST(xo, (yo+yo2)>>1, 0); |
491 |
|
CHECK_CANDIDATE_2ndBEST((xo+xo2)>>1, yo, 0); |
492 |
|
data->currentQMV[0] = v_best; |
493 |
|
data->iMinSAD[0] = s_best; |
494 |
|
|
495 |
|
if(best_halfpel <= s_best2) return; |
496 |
|
|
497 |
|
CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, direction); |
498 |
|
|
499 |
|
} |