171 |
const IMAGE *const pCurrent = ¤t->image; |
const IMAGE *const pCurrent = ¤t->image; |
172 |
const IMAGE *const pRef = &reference->image; |
const IMAGE *const pRef = &reference->image; |
173 |
|
|
174 |
const VECTOR zeroMV = { 0, 0 }; |
static 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; |
1046 |
return iMinSAD; |
return iMinSAD; |
1047 |
} |
} |
1048 |
|
|
1049 |
|
Halfpel8_RefineFuncPtr Halfpel8_Refine; |
1050 |
|
|
1051 |
int32_t |
int32_t |
1052 |
Halfpel16_Refine(const uint8_t * const pRef, |
Halfpel16_Refine(const uint8_t * const pRef, |
1148 |
} |
} |
1149 |
|
|
1150 |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
1151 |
bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
//bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
1152 |
// bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
1153 |
|
|
1154 |
/* fprintf(stderr,"pmv: %d %d / %d --- %d %d %d %d %d %d - %d %d %d\n", |
/* fprintf(stderr,"pmv: %d %d / %d --- %d %d %d %d %d %d - %d %d %d\n", |
1155 |
pmv[0].x,pmv[0].y,psad[0], |
pmv[0].x,pmv[0].y,psad[0], |
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) |
1472 |
} |
} |
1473 |
|
|
1474 |
int32_t |
int32_t |
1475 |
Halfpel8_Refine(const uint8_t * const pRef, |
Halfpel8_Refine_c(const uint8_t * const pRef, |
1476 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1477 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
1478 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
1576 |
} |
} |
1577 |
|
|
1578 |
/* because we might use IF (dx>max_dx) THEN dx=max_dx; */ |
/* because we might use IF (dx>max_dx) THEN dx=max_dx; */ |
1579 |
bPredEq = get_pmvdata(pMBs, (x >> 1), (y >> 1), iWcount, iSubBlock, pmv, psad); |
//bPredEq = get_pmvdata(pMBs, (x >> 1), (y >> 1), iWcount, iSubBlock, pmv, psad); |
1580 |
// bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, (x >> 1), (y >> 1), iSubBlock, pmv, psad); |
1581 |
|
|
1582 |
if ((x == 0) && (y == 0)) { |
if ((x == 0) && (y == 0)) { |
1583 |
threshA = 512 / 4; |
threshA = 512 / 4; |
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) |
1887 |
max_dy = EVEN(max_dy); |
max_dy = EVEN(max_dy); |
1888 |
} |
} |
1889 |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
1890 |
bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
//bPredEq = get_pmvdata(pMBs, x, y, iWcount, 0, pmv, psad); |
1891 |
// bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x, y, 0, pmv, psad); |
1892 |
|
|
1893 |
/* Step 4: Calculate SAD around the Median prediction. |
/* Step 4: Calculate SAD around the Median prediction. |
1894 |
MinSAD=SAD |
MinSAD=SAD |
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) |
2166 |
max_dy = EVEN(max_dy); |
max_dy = EVEN(max_dy); |
2167 |
} |
} |
2168 |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
/* because we might use something like IF (dx>max_dx) THEN dx=max_dx; */ |
2169 |
bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv, psad); |
//bPredEq = get_pmvdata(pMBs, x >> 1, y >> 1, iWcount, iSubBlock, pmv, psad); |
2170 |
// bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad); |
bPredEq = get_pmvdata2(pMBs, iWcount, 0, x >> 1, y >> 1, iSubBlock, pmv, psad); |
2171 |
|
|
2172 |
|
|
2173 |
/* Step 4: Calculate SAD around the Median prediction. |
/* Step 4: Calculate SAD around the Median prediction. |
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_iref_mv(pRef, 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 |
/* *********************************************************** |
/* *********************************************************** |
2643 |
void |
void |
2644 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
2645 |
FRAMEINFO * const frame, |
FRAMEINFO * const frame, |
2646 |
|
const int32_t time_bp, |
2647 |
|
const int32_t time_pp, |
2648 |
// forward (past) reference |
// forward (past) reference |
2649 |
const MACROBLOCK * const f_mbs, |
const MACROBLOCK * const f_mbs, |
2650 |
const IMAGE * const f_ref, |
const IMAGE * const f_ref, |
2658 |
const IMAGE * const b_refV, |
const IMAGE * const b_refV, |
2659 |
const IMAGE * const b_refHV) |
const IMAGE * const b_refHV) |
2660 |
{ |
{ |
2661 |
const uint32_t mb_width = pParam->mb_width; |
const int mb_width = pParam->mb_width; |
2662 |
const uint32_t mb_height = pParam->mb_height; |
const int mb_height = pParam->mb_height; |
2663 |
const int32_t edged_width = pParam->edged_width; |
const int edged_width = pParam->edged_width; |
2664 |
|
|
2665 |
uint32_t i, j; |
int i, j, k; |
2666 |
|
|
2667 |
int32_t f_sad16; |
static const VECTOR zeroMV={0,0}; |
2668 |
int32_t b_sad16; |
|
2669 |
int32_t i_sad16; |
int f_sad16; /* forward (as usual) search */ |
2670 |
int32_t d_sad16; |
int b_sad16; /* backward (only in b-frames) search */ |
2671 |
int32_t best_sad; |
int i_sad16; /* interpolated (both direction, b-frames only) */ |
2672 |
|
int d_sad16; /* direct mode (assume linear motion) */ |
2673 |
|
|
2674 |
|
int best_sad; |
2675 |
|
|
2676 |
VECTOR pmv_dontcare; |
VECTOR pmv_dontcare; |
2677 |
|
|
2678 |
|
int f_count=0; |
2679 |
|
int b_count=0; |
2680 |
|
int i_count=0; |
2681 |
|
int d_count=0; |
2682 |
|
int dnv_count=0; |
2683 |
|
int s_count=0; |
2684 |
|
const int64_t TRB = (int32_t)time_pp - (int32_t)time_bp; |
2685 |
|
const int64_t TRD = (int32_t)time_pp; |
2686 |
|
|
2687 |
|
fprintf(stderr,"TRB = %lld TRD = %lld time_bp =%d time_pp =%d\n\n",TRB,TRD,time_bp,time_pp); |
2688 |
// note: i==horizontal, j==vertical |
// note: i==horizontal, j==vertical |
2689 |
for (j = 0; j < mb_height; j++) { |
for (j = 0; j < mb_height; j++) { |
2690 |
for (i = 0; i < mb_width; i++) { |
for (i = 0; i < mb_width; i++) { |
2692 |
const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width]; |
const MACROBLOCK *f_mb = &f_mbs[i + j * mb_width]; |
2693 |
const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width]; |
const MACROBLOCK *b_mb = &b_mbs[i + j * mb_width]; |
2694 |
|
|
2695 |
|
VECTOR directMV; |
2696 |
|
VECTOR deltaMV=zeroMV; |
2697 |
|
|
2698 |
|
/* special case, if collocated block is SKIPed: encoding is forward(0,0) */ |
2699 |
|
|
2700 |
if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 && |
if (b_mb->mode == MODE_INTER && b_mb->cbp == 0 && |
2701 |
b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) { |
b_mb->mvs[0].x == 0 && b_mb->mvs[0].y == 0) { |
2702 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
2706 |
mb->b_mvs[0].y = 0; |
mb->b_mvs[0].y = 0; |
2707 |
continue; |
continue; |
2708 |
} |
} |
|
/* force F_SAD16 |
|
|
f_sad16 = 100; |
|
|
b_sad16 = 65535; |
|
2709 |
|
|
2710 |
mb->mode = MODE_FORWARD; |
/* same method of scaling as in decoder.c, so we copy 1:1 from there */ |
|
mb->mvs[0].x = 1; |
|
|
mb->mvs[0].y = 1; |
|
|
mb->b_mvs[0].x = 1; |
|
|
mb->b_mvs[0].y = 1; |
|
|
continue; |
|
|
^^ force F_SAD16 */ |
|
2711 |
|
|
2712 |
|
for (k = 0; k < 4; k++) { |
2713 |
|
directMV = b_mb->mvs[k]; |
2714 |
|
|
2715 |
|
mb->mvs[k].x = (int32_t) ((TRB * directMV.x) / TRD + deltaMV.x); |
2716 |
|
mb->b_mvs[k].x = (int32_t) ((deltaMV.x == 0) |
2717 |
|
? ((TRB - TRD) * directMV.x) / TRD |
2718 |
|
: mb->mvs[k].x - directMV.x); |
2719 |
|
mb->mvs[k].y = (int32_t) ((TRB * directMV.y) / TRD + deltaMV.y); |
2720 |
|
mb->b_mvs[k].y = (int32_t) ((deltaMV.y == 0) |
2721 |
|
? ((TRB - TRD) * directMV.y) / TRD |
2722 |
|
: mb->mvs[k].y - directMV.y); |
2723 |
|
} |
2724 |
|
|
2725 |
|
d_sad16 = |
2726 |
|
sad16bi(frame->image.y + i * 16 + j * 16 * edged_width, |
2727 |
|
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
2728 |
|
i, j, 16, &mb->mvs[0], edged_width), |
2729 |
|
get_ref_mv(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2730 |
|
i, j, 16, &mb->b_mvs[0], edged_width), |
2731 |
|
edged_width); |
2732 |
|
|
2733 |
// forward search |
// forward search |
2734 |
f_sad16 = |
f_sad16 = SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
|
SEARCH16(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
|
2735 |
&frame->image, i, j, frame->motion_flags, |
&frame->image, i, j, frame->motion_flags, |
2736 |
frame->quant, frame->fcode, pParam, |
frame->quant, frame->fcode, pParam, |
2737 |
f_mbs, f_mbs, /* todo */ |
f_mbs, f_mbs, /* todo */ |
2738 |
&mb->mvs[0], &pmv_dontcare); // ignore pmv |
&mb->mvs[0], &pmv_dontcare); // ignore pmv (why?) |
2739 |
|
|
2740 |
|
|
2741 |
// backward search |
// backward search |
2742 |
b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_sad16 = SEARCH16(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2745 |
b_mbs, b_mbs, /* todo */ |
b_mbs, b_mbs, /* todo */ |
2746 |
&mb->b_mvs[0], &pmv_dontcare); // ignore pmv |
&mb->b_mvs[0], &pmv_dontcare); // ignore pmv |
2747 |
|
|
|
// interpolate search (simple, but effective) |
|
|
i_sad16 = 65535; |
|
|
|
|
|
/* |
|
|
x/y range somewhat buggy |
|
2748 |
i_sad16 = |
i_sad16 = |
2749 |
sad16bi_c(frame->image.y + i * 16 + j * 16 * edged_width, |
sad16bi(frame->image.y + i * 16 + j * 16 * edged_width, |
2750 |
get_ref(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
get_ref_mv(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
2751 |
i, j, 16, mb->mvs[0].x, mb->mvs[0].y, |
i, j, 16, &mb->mvs[0], edged_width), |
2752 |
edged_width), get_ref(b_ref->y, b_refH->y, |
get_ref(b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2753 |
b_refV->y, b_refHV->y, |
i, j, 16, -mb->b_mvs[0].x, -mb->b_mvs[0].y, edged_width), |
|
i, j, 16, |
|
|
mb->b_mvs[0].x, |
|
|
mb->b_mvs[0].x, |
|
|
edged_width), |
|
2754 |
edged_width); |
edged_width); |
|
*/ |
|
2755 |
|
|
2756 |
// TODO: direct search |
// TODO: direct search |
2757 |
// predictor + range of [-32,32] |
// predictor + range of [-32,32] |
|
d_sad16 = 65535; |
|
2758 |
|
|
2759 |
|
DEBUG2("f_MV: ",mb->mvs[0].x,mb->mvs[0].y); |
2760 |
|
DEBUG2("b_MV: ",mb->b_mvs[0].x,mb->b_mvs[0].y); |
2761 |
|
|
2762 |
|
/* fprintf(stderr,"f_sad16 = %d, b_sad16 = %d, i_sad16 = %d, d_sad16 = %d\n", |
2763 |
|
f_sad16,b_sad16,i_sad16,d_sad16); |
2764 |
|
*/ |
2765 |
|
|
2766 |
|
// d_sad16 -= 50; |
2767 |
|
// d_sad16 = 65535; |
2768 |
|
// i_sad16 = 65535; |
2769 |
|
// b_sad16 = 65535; |
2770 |
|
|
2771 |
if (f_sad16 < b_sad16) { |
if (f_sad16 < b_sad16) { |
2772 |
best_sad = f_sad16; |
best_sad = f_sad16; |
2783 |
|
|
2784 |
if (d_sad16 < best_sad) { |
if (d_sad16 < best_sad) { |
2785 |
best_sad = d_sad16; |
best_sad = d_sad16; |
2786 |
mb->mode = MODE_DIRECT; |
mb->mode = MODE_DIRECT_NONE_MV; |
2787 |
|
} |
2788 |
|
|
2789 |
|
switch (mb->mode) |
2790 |
|
{ |
2791 |
|
case MODE_FORWARD: |
2792 |
|
f_count++; break; |
2793 |
|
case MODE_BACKWARD: |
2794 |
|
b_count++; break; |
2795 |
|
case MODE_INTERPOLATE: |
2796 |
|
i_count++; break; |
2797 |
|
case MODE_DIRECT: |
2798 |
|
d_count++; break; |
2799 |
|
case MODE_DIRECT_NONE_MV: |
2800 |
|
dnv_count++; break; |
2801 |
|
default: |
2802 |
|
s_count++; break; |
2803 |
} |
} |
2804 |
|
|
2805 |
} |
} |
2806 |
} |
} |
2807 |
|
|
2808 |
|
#ifdef _DEBUG_BFRAME_STAT |
2809 |
|
fprintf(stderr,"B-Stat: F: %04d B: %04d I: %04d D0: %04d D: %04d S: %04d\n", |
2810 |
|
f_count,b_count,i_count,dnv_count,d_count,s_count); |
2811 |
|
#endif |
2812 |
|
|
2813 |
} |
} |