173 |
|
|
174 |
const VECTOR zeroMV = { 0, 0 }; |
const VECTOR zeroMV = { 0, 0 }; |
175 |
|
|
|
int64_t time; |
|
176 |
int32_t x, y; |
int32_t x, y; |
177 |
int32_t iIntra = 0; |
int32_t iIntra = 0; |
178 |
VECTOR pmv; |
VECTOR pmv; |
1208 |
|
|
1209 |
if ((iMinSAD < 256) || |
if ((iMinSAD < 256) || |
1210 |
((MVequal(*currMV, prevMB->mvs[0])) && |
((MVequal(*currMV, prevMB->mvs[0])) && |
1211 |
((uint32_t) iMinSAD < prevMB->sad16))) { |
((int32_t) iMinSAD < prevMB->sad16))) { |
1212 |
if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode |
if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode |
1213 |
{ |
{ |
1214 |
if (!MVzero(*currMV)) { |
if (!MVzero(*currMV)) { |
1312 |
|
|
1313 |
if ((iMinSAD <= threshA) || |
if ((iMinSAD <= threshA) || |
1314 |
(MVequal(*currMV, prevMB->mvs[0]) && |
(MVequal(*currMV, prevMB->mvs[0]) && |
1315 |
((uint32_t) iMinSAD < prevMB->sad16))) { |
((int32_t) iMinSAD < prevMB->sad16))) { |
1316 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1317 |
goto PMVfast16_Terminate_without_Refine; |
goto PMVfast16_Terminate_without_Refine; |
1318 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1627 |
(uint8_t) iFcode, iQuant); |
(uint8_t) iFcode, iQuant); |
1628 |
|
|
1629 |
if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock])) |
if ((iMinSAD < 256 / 4) || ((MVequal(*currMV, prevMB->mvs[iSubBlock])) |
1630 |
&& ((uint32_t) iMinSAD < |
&& ((int32_t) iMinSAD < |
1631 |
prevMB->sad8[iSubBlock]))) { |
prevMB->sad8[iSubBlock]))) { |
1632 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1633 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1682 |
|
|
1683 |
if ((iMinSAD <= threshA) || |
if ((iMinSAD <= threshA) || |
1684 |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
1685 |
((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
1686 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1687 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1688 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1741 |
|
|
1742 |
if ((iMinSAD <= threshA) || |
if ((iMinSAD <= threshA) || |
1743 |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
(MVequal(*currMV, prevMB->mvs[iSubBlock]) && |
1744 |
((uint32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
((int32_t) iMinSAD < prevMB->sad8[iSubBlock]))) { |
1745 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1746 |
goto PMVfast8_Terminate_without_Refine; |
goto PMVfast8_Terminate_without_Refine; |
1747 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1927 |
// thresh1 is fixed to 256 |
// thresh1 is fixed to 256 |
1928 |
if ((iMinSAD < 256) || |
if ((iMinSAD < 256) || |
1929 |
((MVequal(*currMV, prevMB->mvs[0])) && |
((MVequal(*currMV, prevMB->mvs[0])) && |
1930 |
((uint32_t) iMinSAD < prevMB->sad16))) { |
((int32_t) iMinSAD < prevMB->sad16))) { |
1931 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1932 |
goto EPZS16_Terminate_without_Refine; |
goto EPZS16_Terminate_without_Refine; |
1933 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
1987 |
|
|
1988 |
if ((iMinSAD <= thresh2) |
if ((iMinSAD <= thresh2) |
1989 |
|| (MVequal(*currMV, prevMB->mvs[0]) && |
|| (MVequal(*currMV, prevMB->mvs[0]) && |
1990 |
((uint32_t) iMinSAD <= prevMB->sad16))) { |
((int32_t) iMinSAD <= prevMB->sad16))) { |
1991 |
if (MotionFlags & PMV_QUICKSTOP16) |
if (MotionFlags & PMV_QUICKSTOP16) |
1992 |
goto EPZS16_Terminate_without_Refine; |
goto EPZS16_Terminate_without_Refine; |
1993 |
if (MotionFlags & PMV_EARLYSTOP16) |
if (MotionFlags & PMV_EARLYSTOP16) |
2347 |
|
|
2348 |
|
|
2349 |
|
|
2350 |
|
int32_t |
2351 |
|
PMVfastIntSearch16(const uint8_t * const pRef, |
2352 |
|
const uint8_t * const pRefH, |
2353 |
|
const uint8_t * const pRefV, |
2354 |
|
const uint8_t * const pRefHV, |
2355 |
|
const IMAGE * const pCur, |
2356 |
|
const int x, |
2357 |
|
const int y, |
2358 |
|
const uint32_t MotionFlags, |
2359 |
|
const uint32_t iQuant, |
2360 |
|
const uint32_t iFcode, |
2361 |
|
const MBParam * const pParam, |
2362 |
|
const MACROBLOCK * const pMBs, |
2363 |
|
const MACROBLOCK * const prevMBs, |
2364 |
|
VECTOR * const currMV, |
2365 |
|
VECTOR * const currPMV) |
2366 |
|
{ |
2367 |
|
const uint32_t iWcount = pParam->mb_width; |
2368 |
|
const int32_t iWidth = pParam->width; |
2369 |
|
const int32_t iHeight = pParam->height; |
2370 |
|
const int32_t iEdgedWidth = pParam->edged_width; |
2371 |
|
|
2372 |
|
const uint8_t *cur = pCur->y + x * 16 + y * 16 * iEdgedWidth; |
2373 |
|
const VECTOR zeroMV = { 0, 0 }; |
2374 |
|
|
2375 |
|
int32_t iDiamondSize; |
2376 |
|
|
2377 |
|
int32_t min_dx; |
2378 |
|
int32_t max_dx; |
2379 |
|
int32_t min_dy; |
2380 |
|
int32_t max_dy; |
2381 |
|
|
2382 |
|
int32_t iFound; |
2383 |
|
|
2384 |
|
VECTOR newMV; |
2385 |
|
VECTOR backupMV; /* just for PMVFAST */ |
2386 |
|
|
2387 |
|
VECTOR pmv[4]; |
2388 |
|
int32_t psad[4]; |
2389 |
|
|
2390 |
|
MainSearch16FuncPtr MainSearchPtr; |
2391 |
|
|
2392 |
|
const MACROBLOCK *const prevMB = prevMBs + x + y * iWcount; |
2393 |
|
MACROBLOCK *const pMB = pMBs + x + y * iWcount; |
2394 |
|
|
2395 |
|
int32_t threshA, threshB; |
2396 |
|
int32_t bPredEq; |
2397 |
|
int32_t iMinSAD, iSAD; |
2398 |
|
|
2399 |
|
/* Get maximum range */ |
2400 |
|
get_range(&min_dx, &max_dx, &min_dy, &max_dy, x, y, 16, iWidth, iHeight, |
2401 |
|
iFcode); |
2402 |
|
|
2403 |
|
/* we work with abs. MVs, not relative to prediction, so get_range is called relative to 0,0 */ |
2404 |
|
|
2405 |
|
if ((x == 0) && (y == 0)) { |
2406 |
|
threshA = 512; |
2407 |
|
threshB = 1024; |
2408 |
|
|
2409 |
|
bPredEq = 0; |
2410 |
|
psad[0] = psad[1] = psad[2] = psad[3] = 0; |
2411 |
|
*currMV = pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV; |
2412 |
|
|
2413 |
|
} else { |
2414 |
|
threshA = psad[0]; |
2415 |
|
threshB = threshA + 256; |
2416 |
|
if (threshA < 512) |
2417 |
|
threshA = 512; |
2418 |
|
if (threshA > 1024) |
2419 |
|
threshA = 1024; |
2420 |
|
if (threshB > 1792) |
2421 |
|
threshB = 1792; |
2422 |
|
|
2423 |
|
bPredEq = get_ipmvdata(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
2424 |
|
*currMV = pmv[0]; /* current best := prediction */ |
2425 |
|
} |
2426 |
|
|
2427 |
|
iFound = 0; |
2428 |
|
|
2429 |
|
/* Step 4: Calculate SAD around the Median prediction. |
2430 |
|
MinSAD=SAD |
2431 |
|
If Motion Vector equal to Previous frame motion vector |
2432 |
|
and MinSAD<PrevFrmSAD goto Step 10. |
2433 |
|
If SAD<=256 goto Step 10. |
2434 |
|
*/ |
2435 |
|
|
2436 |
|
if (currMV->x > max_dx) { |
2437 |
|
currMV->x = EVEN(max_dx); |
2438 |
|
} |
2439 |
|
if (currMV->x < min_dx) { |
2440 |
|
currMV->x = EVEN(min_dx); |
2441 |
|
} |
2442 |
|
if (currMV->y > max_dy) { |
2443 |
|
currMV->y = EVEN(max_dy); |
2444 |
|
} |
2445 |
|
if (currMV->y < min_dy) { |
2446 |
|
currMV->y = EVEN(min_dy); |
2447 |
|
} |
2448 |
|
|
2449 |
|
iMinSAD = |
2450 |
|
sad16(cur, |
2451 |
|
get_ref_mv(pRef, pRefH, pRefV, pRefHV, x, y, 16, currMV, |
2452 |
|
iEdgedWidth), iEdgedWidth, MV_MAX_ERROR); |
2453 |
|
iMinSAD += |
2454 |
|
calc_delta_16(currMV->x - pmv[0].x, currMV->y - pmv[0].y, |
2455 |
|
(uint8_t) iFcode, iQuant); |
2456 |
|
|
2457 |
|
if ((iMinSAD < 256) || |
2458 |
|
((MVequal(*currMV, prevMB->i_mvs[0])) && |
2459 |
|
((int32_t) iMinSAD < prevMB->i_sad16))) { |
2460 |
|
if (iMinSAD < 2 * iQuant) // high chances for SKIP-mode |
2461 |
|
{ |
2462 |
|
if (!MVzero(*currMV)) { |
2463 |
|
iMinSAD += MV16_00_BIAS; |
2464 |
|
CHECK_MV16_ZERO; // (0,0) saves space for letterboxed pictures |
2465 |
|
iMinSAD -= MV16_00_BIAS; |
2466 |
|
} |
2467 |
|
} |
2468 |
|
|
2469 |
|
if (MotionFlags & PMV_EARLYSTOP16) |
2470 |
|
goto PMVfastInt16_Terminate_with_Refine; |
2471 |
|
} |
2472 |
|
|
2473 |
|
|
2474 |
|
/* Step 2 (lazy eval): Calculate Distance= |MedianMVX| + |MedianMVY| where MedianMV is the motion |
2475 |
|
vector of the median. |
2476 |
|
If PredEq=1 and MVpredicted = Previous Frame MV, set Found=2 |
2477 |
|
*/ |
2478 |
|
|
2479 |
|
if ((bPredEq) && (MVequal(pmv[0], prevMB->i_mvs[0]))) |
2480 |
|
iFound = 2; |
2481 |
|
|
2482 |
|
/* Step 3 (lazy eval): If Distance>0 or thresb<1536 or PredEq=1 Select small Diamond Search. |
2483 |
|
Otherwise select large Diamond Search. |
2484 |
|
*/ |
2485 |
|
|
2486 |
|
if ((!MVzero(pmv[0])) || (threshB < 1536) || (bPredEq)) |
2487 |
|
iDiamondSize = 2; // halfpel units! |
2488 |
|
else |
2489 |
|
iDiamondSize = 4; // halfpel units! |
2490 |
|
|
2491 |
|
/* |
2492 |
|
Step 5: Calculate SAD for motion vectors taken from left block, top, top-right, and Previous frame block. |
2493 |
|
Also calculate (0,0) but do not subtract offset. |
2494 |
|
Let MinSAD be the smallest SAD up to this point. |
2495 |
|
If MV is (0,0) subtract offset. |
2496 |
|
*/ |
2497 |
|
|
2498 |
|
// (0,0) is often a good choice |
2499 |
|
|
2500 |
|
if (!MVzero(pmv[0])) |
2501 |
|
CHECK_MV16_ZERO; |
2502 |
|
|
2503 |
|
// previous frame MV is always possible |
2504 |
|
|
2505 |
|
if (!MVzero(prevMB->i_mvs[0])) |
2506 |
|
if (!MVequal(prevMB->i_mvs[0], pmv[0])) |
2507 |
|
CHECK_MV16_CANDIDATE(prevMB->i_mvs[0].x, prevMB->i_mvs[0].y); |
2508 |
|
|
2509 |
|
// left neighbour, if allowed |
2510 |
|
|
2511 |
|
if (!MVzero(pmv[1])) |
2512 |
|
if (!MVequal(pmv[1], prevMB->i_mvs[0])) |
2513 |
|
if (!MVequal(pmv[1], pmv[0])) |
2514 |
|
CHECK_MV16_CANDIDATE(pmv[1].x, pmv[1].y); |
2515 |
|
|
2516 |
|
// top neighbour, if allowed |
2517 |
|
if (!MVzero(pmv[2])) |
2518 |
|
if (!MVequal(pmv[2], prevMB->i_mvs[0])) |
2519 |
|
if (!MVequal(pmv[2], pmv[0])) |
2520 |
|
if (!MVequal(pmv[2], pmv[1])) |
2521 |
|
CHECK_MV16_CANDIDATE(pmv[2].x, pmv[2].y); |
2522 |
|
|
2523 |
|
// top right neighbour, if allowed |
2524 |
|
if (!MVzero(pmv[3])) |
2525 |
|
if (!MVequal(pmv[3], prevMB->i_mvs[0])) |
2526 |
|
if (!MVequal(pmv[3], pmv[0])) |
2527 |
|
if (!MVequal(pmv[3], pmv[1])) |
2528 |
|
if (!MVequal(pmv[3], pmv[2])) |
2529 |
|
CHECK_MV16_CANDIDATE(pmv[3].x, |
2530 |
|
pmv[3].y); |
2531 |
|
|
2532 |
|
if ((MVzero(*currMV)) && |
2533 |
|
(!MVzero(pmv[0])) /* && (iMinSAD <= iQuant * 96) */ ) |
2534 |
|
iMinSAD -= MV16_00_BIAS; |
2535 |
|
|
2536 |
|
|
2537 |
|
/* Step 6: If MinSAD <= thresa goto Step 10. |
2538 |
|
If Motion Vector equal to Previous frame motion vector and MinSAD<PrevFrmSAD goto Step 10. |
2539 |
|
*/ |
2540 |
|
|
2541 |
|
if ((iMinSAD <= threshA) || |
2542 |
|
(MVequal(*currMV, prevMB->i_mvs[0]) && |
2543 |
|
((int32_t) iMinSAD < prevMB->i_sad16))) { |
2544 |
|
|
2545 |
|
if (MotionFlags & PMV_EARLYSTOP16) |
2546 |
|
goto PMVfastInt16_Terminate_with_Refine; |
2547 |
|
} |
2548 |
|
|
2549 |
|
|
2550 |
|
/************ (Diamond Search) **************/ |
2551 |
|
/* |
2552 |
|
Step 7: Perform Diamond search, with either the small or large diamond. |
2553 |
|
If Found=2 only examine one Diamond pattern, and afterwards goto step 10 |
2554 |
|
Step 8: If small diamond, iterate small diamond search pattern until motion vector lies in the center of the diamond. |
2555 |
|
If center then goto step 10. |
2556 |
|
Step 9: If large diamond, iterate large diamond search pattern until motion vector lies in the center. |
2557 |
|
Refine by using small diamond and goto step 10. |
2558 |
|
*/ |
2559 |
|
|
2560 |
|
if (MotionFlags & PMV_USESQUARES16) |
2561 |
|
MainSearchPtr = Square16_MainSearch; |
2562 |
|
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) |
2563 |
|
MainSearchPtr = AdvDiamond16_MainSearch; |
2564 |
|
else |
2565 |
|
MainSearchPtr = Diamond16_MainSearch; |
2566 |
|
|
2567 |
|
backupMV = *currMV; /* save best prediction, actually only for EXTSEARCH */ |
2568 |
|
|
2569 |
|
|
2570 |
|
/* default: use best prediction as starting point for one call of PMVfast_MainSearch */ |
2571 |
|
iSAD = |
2572 |
|
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV->x, |
2573 |
|
currMV->y, iMinSAD, &newMV, pmv, min_dx, max_dx, |
2574 |
|
min_dy, max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2575 |
|
iQuant, iFound); |
2576 |
|
|
2577 |
|
if (iSAD < iMinSAD) { |
2578 |
|
*currMV = newMV; |
2579 |
|
iMinSAD = iSAD; |
2580 |
|
} |
2581 |
|
|
2582 |
|
if (MotionFlags & PMV_EXTSEARCH16) { |
2583 |
|
/* extended: search (up to) two more times: orignal prediction and (0,0) */ |
2584 |
|
|
2585 |
|
if (!(MVequal(pmv[0], backupMV))) { |
2586 |
|
iSAD = |
2587 |
|
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, |
2588 |
|
pmv[0].x, pmv[0].y, iMinSAD, &newMV, pmv, |
2589 |
|
min_dx, max_dx, min_dy, max_dy, iEdgedWidth, |
2590 |
|
iDiamondSize, iFcode, iQuant, iFound); |
2591 |
|
|
2592 |
|
if (iSAD < iMinSAD) { |
2593 |
|
*currMV = newMV; |
2594 |
|
iMinSAD = iSAD; |
2595 |
|
} |
2596 |
|
} |
2597 |
|
|
2598 |
|
if ((!(MVzero(pmv[0]))) && (!(MVzero(backupMV)))) { |
2599 |
|
iSAD = |
2600 |
|
(*MainSearchPtr) (pRef, pRefH, pRefV, pRefHV, cur, x, y, 0, 0, |
2601 |
|
iMinSAD, &newMV, pmv, min_dx, max_dx, min_dy, |
2602 |
|
max_dy, iEdgedWidth, iDiamondSize, iFcode, |
2603 |
|
iQuant, iFound); |
2604 |
|
|
2605 |
|
if (iSAD < iMinSAD) { |
2606 |
|
*currMV = newMV; |
2607 |
|
iMinSAD = iSAD; |
2608 |
|
} |
2609 |
|
} |
2610 |
|
} |
2611 |
|
|
2612 |
|
/* |
2613 |
|
Step 10: The motion vector is chosen according to the block corresponding to MinSAD. |
2614 |
|
*/ |
2615 |
|
|
2616 |
|
PMVfastInt16_Terminate_with_Refine: |
2617 |
|
|
2618 |
|
pMB->i_mvs[0] = pMB->i_mvs[1] = pMB->i_mvs[2] = pMB->i_mvs[3] = pMB->i_mv16 = *currMV; |
2619 |
|
pMB->i_sad8[0] = pMB->i_sad8[1] = pMB->i_sad8[2] = pMB->i_sad8[3] = pMB->i_sad16 = iMinSAD; |
2620 |
|
|
2621 |
|
if (MotionFlags & PMV_HALFPELREFINE16) // perform final half-pel step |
2622 |
|
iMinSAD = |
2623 |
|
Halfpel16_Refine(pRef, pRefH, pRefV, pRefHV, cur, x, y, currMV, |
2624 |
|
iMinSAD, pmv, min_dx, max_dx, min_dy, max_dy, |
2625 |
|
iFcode, iQuant, iEdgedWidth); |
2626 |
|
|
2627 |
|
pmv[0] = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); // get _REAL_ prediction (halfpel possible) |
2628 |
|
|
2629 |
|
PMVfastInt16_Terminate_without_Refine: |
2630 |
|
currPMV->x = currMV->x - pmv[0].x; |
2631 |
|
currPMV->y = currMV->y - pmv[0].y; |
2632 |
|
return iMinSAD; |
2633 |
|
} |
2634 |
|
|
2635 |
|
|
2636 |
|
|
2637 |
/* *********************************************************** |
/* *********************************************************** |