47 |
#define FINAL_SKIP_THRESH (50) |
#define FINAL_SKIP_THRESH (50) |
48 |
#define MAX_SAD00_FOR_SKIP (20) |
#define MAX_SAD00_FOR_SKIP (20) |
49 |
#define MAX_CHROMA_SAD_FOR_SKIP (22) |
#define MAX_CHROMA_SAD_FOR_SKIP (22) |
|
#define SKIP_THRESH_B (25) |
|
50 |
|
|
51 |
#define CHECK_CANDIDATE(X,Y,D) { \ |
#define CHECK_CANDIDATE(X,Y,D) { \ |
52 |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
53 |
|
|
|
#define iDiamondSize 2 |
|
|
|
|
|
static VECTOR |
|
|
GlobalMotionEst(const MACROBLOCK * const pMBs, |
|
|
const MBParam * const pParam, const uint32_t iFcode); |
|
|
|
|
|
|
|
54 |
static __inline int |
static __inline int |
55 |
d_mv_bits(int x, int y, const uint32_t iFcode) |
d_mv_bits(int x, int y, const uint32_t iFcode, const int qpel, const int rrv) |
56 |
{ |
{ |
57 |
int xb, yb; |
int xb, yb; |
58 |
|
if (qpel) { x *= 2; y *= 2;} |
59 |
|
else if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
60 |
|
|
61 |
if (x == 0) xb = 1; |
if (x == 0) xb = 1; |
62 |
else { |
else { |
85 |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
86 |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
87 |
|
|
88 |
switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0) |
if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; //it has been checked recently |
89 |
|
|
90 |
|
switch (((dx & 1) << 1) | (dy & 1)) { |
91 |
case 0: |
case 0: |
92 |
sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
93 |
sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
114 |
sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2); |
sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2); |
115 |
break; |
break; |
116 |
} |
} |
117 |
|
data->temp[5] = dx; data->temp[6] = dy; data->temp[7] = sad; //backup |
118 |
return sad; |
return sad; |
119 |
} |
} |
120 |
|
|
131 |
case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
case 5 : return data->bRefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); |
132 |
case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
case 6 : return data->bRefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); |
133 |
default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
default : return data->bRefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); |
|
|
|
134 |
} |
} |
135 |
} |
} |
136 |
|
|
238 |
|
|
239 |
if (data->qpel_precision) { // x and y are in 1/4 precision |
if (data->qpel_precision) { // x and y are in 1/4 precision |
240 |
Reference = Interpolate16x16qpel(x, y, 0, data); |
Reference = Interpolate16x16qpel(x, y, 0, data); |
|
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
241 |
xc = x/2; yc = y/2; //for chroma sad |
xc = x/2; yc = y/2; //for chroma sad |
242 |
current = data->currentQMV; |
current = data->currentQMV; |
243 |
} else { |
} else { |
244 |
switch ( ((x&1)<<1) + (y&1) ) { |
Reference = GetReference(x, y, 0, data); |
|
case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break; |
|
|
case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
|
|
case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break; |
|
|
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
|
|
} |
|
|
if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode); |
|
|
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
245 |
current = data->currentMV; |
current = data->currentMV; |
246 |
xc = x; yc = y; |
xc = x; yc = y; |
247 |
} |
} |
248 |
|
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
249 |
|
|
250 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
251 |
|
|
280 |
( x > data->max_dx) || ( x < data->min_dx) |
( x > data->max_dx) || ( x < data->min_dx) |
281 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
282 |
|
|
283 |
switch ( ((x&1)<<1) + (y&1) ) { |
Reference = GetReference(x, y, 0, data); |
284 |
case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break; |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, 0, 1); |
|
case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
|
|
case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break; |
|
|
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
|
|
} |
|
|
|
|
|
t = d_mv_bits(RRV_MV_SCALEDOWN(x) - data->predMV.x, |
|
|
RRV_MV_SCALEDOWN(y) - data->predMV.y, data->iFcode); |
|
285 |
|
|
286 |
data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
287 |
|
|
314 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
315 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
316 |
|
|
317 |
if (data->rrv) { |
if (data->rrv) |
318 |
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) ) return; //non-zero integer value |
if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) ) return; //non-zero integer value |
|
t = d_mv_bits(RRV_MV_SCALEDOWN(x) - data->predMV.x, |
|
|
RRV_MV_SCALEDOWN(y) - data->predMV.y, data->iFcode); |
|
|
} |
|
319 |
|
|
320 |
if (data->qpel_precision) { // x and y are in 1/4 precision |
if (data->qpel_precision) { // x and y are in 1/4 precision |
321 |
Reference = Interpolate16x16qpel(x, y, 0, data); |
Reference = Interpolate16x16qpel(x, y, 0, data); |
|
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
322 |
current = data->currentQMV; |
current = data->currentQMV; |
323 |
} else { |
} else { |
324 |
switch ( ((x&1)<<1) + (y&1) ) { |
Reference = GetReference(x, y, 0, data); |
|
case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break; |
|
|
case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
|
|
case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break; |
|
|
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
|
|
} |
|
|
if (data->qpel) t = d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode); |
|
|
else if (!data->rrv) t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
325 |
current = data->currentMV; |
current = data->currentMV; |
326 |
} |
} |
327 |
|
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, |
328 |
|
data->qpel && !data->qpel_precision && !data->rrv, data->rrv); |
329 |
|
|
330 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
331 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)/1000; |
371 |
xb = data->currentQMV[1].x; yb = data->currentQMV[1].y; |
xb = data->currentQMV[1].x; yb = data->currentQMV[1].y; |
372 |
current = data->currentQMV; |
current = data->currentQMV; |
373 |
ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); |
ReferenceB = Interpolate16x16qpel(xb, yb, 1, data); |
|
t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) |
|
|
+ d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode); |
|
374 |
} else { |
} else { |
375 |
ReferenceF = Interpolate16x16qpel(2*xf, 2*yf, 0, data); |
ReferenceF = GetReference(xf, yf, 0, data); |
376 |
xb = data->currentMV[1].x; yb = data->currentMV[1].y; |
xb = data->currentMV[1].x; yb = data->currentMV[1].y; |
377 |
ReferenceB = Interpolate16x16qpel(2*xb, 2*yb, 1, data); |
ReferenceB = GetReference(xb, yb, 1, data); |
378 |
current = data->currentMV; |
current = data->currentMV; |
|
if (data->qpel) |
|
|
t = d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode) |
|
|
+ d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode); |
|
|
else |
|
|
t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) |
|
|
+ d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode); |
|
379 |
} |
} |
380 |
|
|
381 |
|
t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0) |
382 |
|
+ d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
383 |
|
|
384 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
385 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)/1000; |
386 |
|
|
430 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
431 |
} |
} |
432 |
|
|
433 |
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, 1, 0, 0) * sad)/1000; |
434 |
|
|
435 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
436 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
471 |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
472 |
|
|
473 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
474 |
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, 1, 0, 0) * sad)/1000; |
475 |
|
|
476 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
477 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
489 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
490 |
|
|
491 |
if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data); |
if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data); |
492 |
else Reference = Interpolate16x16qpel(2*x, 2*y, 0, data); |
else Reference = GetReference(x, y, 0, data); |
493 |
|
|
494 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
495 |
if (data->qpel) t = d_mv_bits(2 * x - data->predMV.x, 2 * y - data->predMV.y, data->iFcode); |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
|
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
496 |
|
|
497 |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
498 |
|
|
514 |
|
|
515 |
int iDirection; |
int iDirection; |
516 |
|
|
517 |
do { |
for(;;) { //forever |
518 |
iDirection = 0; |
iDirection = 0; |
519 |
if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1); |
if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1); |
520 |
if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
532 |
CHECK_CANDIDATE(x, y - iDiamondSize, 4); |
CHECK_CANDIDATE(x, y - iDiamondSize, 4); |
533 |
} else { // what remains here is up or down |
} else { // what remains here is up or down |
534 |
CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
535 |
CHECK_CANDIDATE(x - iDiamondSize, y, 1); } |
CHECK_CANDIDATE(x - iDiamondSize, y, 1); |
536 |
|
} |
537 |
|
|
538 |
if (iDirection) { |
if (iDirection) { |
539 |
bDirection += iDirection; |
bDirection += iDirection; |
540 |
x = data->currentMV->x; y = data->currentMV->y; } |
x = data->currentMV->x; y = data->currentMV->y; |
541 |
|
} |
542 |
} else { //about to quit, eh? not so fast.... |
} else { //about to quit, eh? not so fast.... |
543 |
switch (bDirection) { |
switch (bDirection) { |
544 |
case 2: |
case 2: |
589 |
x = data->currentMV->x; y = data->currentMV->y; |
x = data->currentMV->x; y = data->currentMV->y; |
590 |
} |
} |
591 |
} |
} |
|
while (1); //forever |
|
592 |
} |
} |
593 |
|
|
594 |
static void |
static void |
638 |
CHECK_CANDIDATE(x, y - iDiamondSize, 4); |
CHECK_CANDIDATE(x, y - iDiamondSize, 4); |
639 |
} else { // what remains here is up or down |
} else { // what remains here is up or down |
640 |
CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
CHECK_CANDIDATE(x + iDiamondSize, y, 2); |
641 |
CHECK_CANDIDATE(x - iDiamondSize, y, 1); } |
CHECK_CANDIDATE(x - iDiamondSize, y, 1); |
642 |
|
} |
643 |
bDirection += iDirection; |
bDirection += iDirection; |
644 |
x = data->currentMV->x; y = data->currentMV->y; |
x = data->currentMV->x; y = data->currentMV->y; |
645 |
} |
} |
739 |
uint8_t *qimage; |
uint8_t *qimage; |
740 |
|
|
741 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
742 |
int32_t temp[5]; |
int32_t temp[8]; |
743 |
VECTOR currentMV[5]; |
VECTOR currentMV[5]; |
744 |
VECTOR currentQMV[5]; |
VECTOR currentQMV[5]; |
745 |
int32_t iMinSAD[5]; |
int32_t iMinSAD[5]; |
966 |
int i, iDirection = 255, mask, threshA; |
int i, iDirection = 255, mask, threshA; |
967 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
968 |
|
|
|
if (Data->rrv) { |
|
|
i = (pParam->width + 31) / 32; |
|
|
get_range_rrv(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 32, |
|
|
pParam->width, pParam->height, Data->iFcode); |
|
|
} else { |
|
|
i = pParam->mb_width; |
|
969 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
970 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv); |
|
} |
|
971 |
|
|
972 |
i = pParam->mb_width; // XXXX |
get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
|
get_pmvdata2(pMBs, i, 0, x, y, 0, pmv, Data->temp); //has to be changed to get_pmv(2)() |
|
973 |
|
|
974 |
|
Data->temp[5] = Data->temp[7] = 256*4096; // to reset chroma-sad cache |
975 |
if (Data->rrv) i = 2; else i = 1; |
if (Data->rrv) i = 2; else i = 1; |
976 |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16*i; |
977 |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8*i; |
996 |
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
997 |
else Data->predMV = pmv[0]; |
else Data->predMV = pmv[0]; |
998 |
|
|
999 |
i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode, 0, 0); |
1000 |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
1001 |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
1002 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
1085 |
if((!Data->rrv) && (pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
if((!Data->rrv) && (pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
1086 |
|
|
1087 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1088 |
get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1089 |
pParam->width, pParam->height, Data->iFcode); |
pParam->width, pParam->height, Data->iFcode, 1, 0); |
1090 |
|
|
1091 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1092 |
} |
} |
1172 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1173 |
Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
1174 |
if (block != 0) i = d_mv_bits( Data->currentQMV->x - Data->predMV.x, |
if (block != 0) i = d_mv_bits( Data->currentQMV->x - Data->predMV.x, |
1175 |
Data->currentQMV->y - Data->predMV.y, Data->iFcode); |
Data->currentQMV->y - Data->predMV.y, Data->iFcode, 0, 0); |
1176 |
|
|
1177 |
} else { |
} else { |
1178 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
1179 |
if (block != 0) { |
if (block != 0) { |
1180 |
if (Data->rrv) i = d_mv_bits( RRV_MV_SCALEDOWN(Data->currentMV->x) - Data->predMV.x, |
if (block != 0) i = d_mv_bits( Data->currentMV->x - Data->predMV.x, |
1181 |
RRV_MV_SCALEDOWN(Data->currentMV->y) - Data->predMV.y, |
Data->currentMV->y - Data->predMV.y, Data->iFcode, 0, Data->rrv); |
|
Data->iFcode); |
|
|
else i = d_mv_bits( Data->currentMV->x - Data->predMV.x, |
|
|
Data->currentMV->y - Data->predMV.y, Data->iFcode); |
|
1182 |
} |
} |
1183 |
} |
} |
1184 |
|
|
1195 |
Data->Cur = OldData->Cur + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
Data->Cur = OldData->Cur + i*8 * ((block&1) + pParam->edged_width*(block>>1)); |
1196 |
Data->qpel_precision = 0; |
Data->qpel_precision = 0; |
1197 |
|
|
|
if (Data->rrv) { |
|
|
get_range_rrv(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
|
|
pParam->width, pParam->height, OldData->iFcode); |
|
|
CheckCandidate = CheckCandidate16no4v; |
|
|
} else { |
|
1198 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
1199 |
pParam->width, pParam->height, OldData->iFcode - pParam->m_quarterpel); |
pParam->width, pParam->height, OldData->iFcode - Data->qpel, 0, Data->rrv); |
1200 |
CheckCandidate = CheckCandidate8; |
|
1201 |
} |
if (Data->rrv) CheckCandidate = CheckCandidate16no4v; |
1202 |
|
else CheckCandidate = CheckCandidate8; |
1203 |
|
|
1204 |
if (MotionFlags & PMV_EXTSEARCH8) { |
if (MotionFlags & PMV_EXTSEARCH8) { |
1205 |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
1228 |
} |
} |
1229 |
} |
} |
1230 |
|
|
1231 |
if(!Data->rrv && pParam->m_quarterpel) { |
if(!Data->rrv && Data->qpel) { |
1232 |
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
1233 |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
1234 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1235 |
get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
1236 |
pParam->width, pParam->height, OldData->iFcode); |
pParam->width, pParam->height, OldData->iFcode, 1, 0); |
1237 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1238 |
} |
} |
1239 |
} |
} |
1244 |
Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y); |
Data->currentMV->y = RRV_MV_SCALEDOWN(Data->currentMV->y); |
1245 |
} |
} |
1246 |
|
|
1247 |
if(pParam->m_quarterpel) { |
if(Data->qpel) { |
1248 |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; |
pMB->pmvs[block].x = Data->currentQMV->x - Data->predMV.x; |
1249 |
pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; |
pMB->pmvs[block].y = Data->currentQMV->y - Data->predMV.y; |
1250 |
pMB->qmvs[block] = *(Data->currentQMV); |
pMB->qmvs[block] = *(Data->currentQMV); |
1341 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
1342 |
|
|
1343 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1344 |
pParam->width, pParam->height, iFcode - pParam->m_quarterpel); |
pParam->width, pParam->height, iFcode - Data->qpel, 0, 0); |
1345 |
|
|
1346 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
1347 |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
1370 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
1371 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
1372 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1373 |
get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1374 |
pParam->width, pParam->height, iFcode); |
pParam->width, pParam->height, iFcode, 1, 0); |
1375 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1376 |
} |
} |
1377 |
|
|
1403 |
|
|
1404 |
} |
} |
1405 |
|
|
1406 |
static int32_t |
static void |
1407 |
|
SkipDecisionB(const IMAGE * const pCur, |
1408 |
|
const IMAGE * const f_Ref, |
1409 |
|
const IMAGE * const b_Ref, |
1410 |
|
MACROBLOCK * const pMB, |
1411 |
|
const uint32_t quant, |
1412 |
|
const uint32_t x, const uint32_t y, |
1413 |
|
const SearchData * const Data) |
1414 |
|
{ |
1415 |
|
int dx, dy, b_dx, b_dy; |
1416 |
|
uint32_t sum; |
1417 |
|
//this is not full chroma compensation, only it's fullpel approximation. should work though |
1418 |
|
if (Data->qpel) { |
1419 |
|
dy = Data->directmvF[0].y/2 + Data->directmvF[1].y/2 + |
1420 |
|
Data->directmvF[2].y/2 + Data->directmvF[3].y/2; |
1421 |
|
|
1422 |
|
dx = Data->directmvF[0].x/2 + Data->directmvF[1].x/2 + |
1423 |
|
Data->directmvF[2].x/2 + Data->directmvF[3].x/2; |
1424 |
|
|
1425 |
|
b_dy = Data->directmvB[0].y/2 + Data->directmvB[1].y/2 + |
1426 |
|
Data->directmvB[2].y/2 + Data->directmvB[3].y/2; |
1427 |
|
|
1428 |
|
b_dx = Data->directmvB[0].x/2 + Data->directmvB[1].x/2 + |
1429 |
|
Data->directmvB[2].x/2 + Data->directmvB[3].x/2; |
1430 |
|
|
1431 |
|
} else { |
1432 |
|
dy = Data->directmvF[0].y + Data->directmvF[1].y + |
1433 |
|
Data->directmvF[2].y + Data->directmvF[3].y; |
1434 |
|
|
1435 |
|
dx = Data->directmvF[0].x + Data->directmvF[1].x + |
1436 |
|
Data->directmvF[2].x + Data->directmvF[3].x; |
1437 |
|
|
1438 |
|
b_dy = Data->directmvB[0].y + Data->directmvB[1].y + |
1439 |
|
Data->directmvB[2].y + Data->directmvB[3].y; |
1440 |
|
|
1441 |
|
b_dx = Data->directmvB[0].x + Data->directmvB[1].x + |
1442 |
|
Data->directmvB[2].x + Data->directmvB[3].x; |
1443 |
|
} |
1444 |
|
|
1445 |
|
|
1446 |
|
dy = (dy >> 3) + roundtab_76[dy & 0xf]; |
1447 |
|
dx = (dx >> 3) + roundtab_76[dx & 0xf]; |
1448 |
|
b_dy = (b_dy >> 3) + roundtab_76[b_dy & 0xf]; |
1449 |
|
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
1450 |
|
|
1451 |
|
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
1452 |
|
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
1453 |
|
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
1454 |
|
Data->iEdgedWidth/2); |
1455 |
|
sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2), |
1456 |
|
f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
1457 |
|
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
1458 |
|
Data->iEdgedWidth/2); |
1459 |
|
|
1460 |
|
if (sum < 2*MAX_CHROMA_SAD_FOR_SKIP * quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped |
1461 |
|
} |
1462 |
|
|
1463 |
|
|
1464 |
|
|
1465 |
|
static __inline uint32_t |
1466 |
SearchDirect(const IMAGE * const f_Ref, |
SearchDirect(const IMAGE * const f_Ref, |
1467 |
const uint8_t * const f_RefH, |
const uint8_t * const f_RefH, |
1468 |
const uint8_t * const f_RefV, |
const uint8_t * const f_RefV, |
1523 |
*best_sad = 256*4096; // in that case, we won't use direct mode |
*best_sad = 256*4096; // in that case, we won't use direct mode |
1524 |
pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV" |
pMB->mode = MODE_DIRECT; // just to make sure it doesn't say "MODE_DIRECT_NONE_MV" |
1525 |
pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0; |
pMB->b_mvs[0].x = pMB->b_mvs[0].y = 0; |
1526 |
return 0; |
return 256*4096; |
1527 |
} |
} |
1528 |
if (b_mb->mode != MODE_INTER4V) { |
if (b_mb->mode != MODE_INTER4V) { |
1529 |
pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0]; |
pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->mvs[0]; |
1540 |
|
|
1541 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
(*CheckCandidate)(0, 0, 255, &k, Data); |
1542 |
|
|
1543 |
// skip decision |
// initial (fast) skip decision |
1544 |
if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) { |
if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH*2) { |
1545 |
//possible skip - checking chroma. everything copied from MC |
SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data->chroma, Data); //possible skip - checking chroma |
1546 |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip. |
|
int sum, dx, dy, b_dx, b_dy; |
|
|
|
|
|
if (Data->qpel) { |
|
|
sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2; |
|
|
dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
|
|
sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2; |
|
|
dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
|
|
|
|
|
sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2; |
|
|
b_dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
|
|
sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2; |
|
|
b_dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
|
|
|
|
|
} else { |
|
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
|
|
dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
|
|
dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
|
|
|
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
|
|
b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
|
|
b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
} |
|
|
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
|
|
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
|
|
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
|
|
Data->iEdgedWidth/2); |
|
|
sum += sad8bi(pCur->v + 8*x + 8*y*(Data->iEdgedWidth/2), |
|
|
f_Ref->v + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
|
|
b_Ref->v + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
|
|
Data->iEdgedWidth/2); |
|
|
|
|
|
if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) { |
|
|
pMB->mode = MODE_DIRECT_NONE_MV; |
|
|
return *Data->iMinSAD; |
|
|
} |
|
1547 |
} |
} |
1548 |
|
|
1549 |
skip_sad = *Data->iMinSAD; |
skip_sad = *Data->iMinSAD; |
1559 |
|
|
1560 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1561 |
|
|
|
// *Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode |
|
1562 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1563 |
|
|
1564 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) |
1595 |
} |
} |
1596 |
|
|
1597 |
|
|
1598 |
static __inline void |
static void |
1599 |
SearchInterpolate(const uint8_t * const f_Ref, |
SearchInterpolate(const uint8_t * const f_Ref, |
1600 |
const uint8_t * const f_RefH, |
const uint8_t * const f_RefH, |
1601 |
const uint8_t * const f_RefV, |
const uint8_t * const f_RefV, |
1618 |
|
|
1619 |
{ |
{ |
1620 |
|
|
|
const int32_t iEdgedWidth = pParam->edged_width; |
|
1621 |
int iDirection, i, j; |
int iDirection, i, j; |
1622 |
SearchData bData; |
SearchData bData; |
1623 |
|
|
1624 |
*(bData.iMinSAD = fData->iMinSAD) = 4096*256; |
fData->qpel_precision = 0; |
1625 |
bData.Cur = fData->Cur; |
memcpy(&bData, fData, sizeof(SearchData)); //quick copy of common data |
1626 |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
*fData->iMinSAD = 4096*256; |
1627 |
bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1; |
bData.currentMV ++; bData.currentQMV ++; |
|
bData.lambda16 = fData->lambda16; |
|
1628 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
1629 |
|
|
1630 |
bData.bRef = fData->Ref = f_Ref + (x + y * iEdgedWidth) * 16; |
i = (x + y * fData->iEdgedWidth) * 16; |
1631 |
bData.bRefH = fData->RefH = f_RefH + (x + y * iEdgedWidth) * 16; |
bData.bRef = fData->Ref = f_Ref + i; |
1632 |
bData.bRefV = fData->RefV = f_RefV + (x + y * iEdgedWidth) * 16; |
bData.bRefH = fData->RefH = f_RefH + i; |
1633 |
bData.bRefHV = fData->RefHV = f_RefHV + (x + y * iEdgedWidth) * 16; |
bData.bRefV = fData->RefV = f_RefV + i; |
1634 |
bData.Ref = fData->bRef = b_Ref + (x + y * iEdgedWidth) * 16; |
bData.bRefHV = fData->RefHV = f_RefHV + i; |
1635 |
bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16; |
bData.Ref = fData->bRef = b_Ref + i; |
1636 |
bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16; |
bData.RefH = fData->bRefH = b_RefH + i; |
1637 |
bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16; |
bData.RefV = fData->bRefV = b_RefV + i; |
1638 |
bData.RefQ = fData->RefQ; |
bData.RefHV = fData->bRefHV = b_RefHV + i; |
|
fData->qpel_precision = bData.qpel_precision = 0; bData.qpel = fData->qpel; |
|
|
bData.rounding = 0; |
|
1639 |
|
|
1640 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
1641 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
|
|
|
1642 |
fData->currentMV[0] = fData->currentMV[2]; |
fData->currentMV[0] = fData->currentMV[2]; |
1643 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - pParam->m_quarterpel); |
|
1644 |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - pParam->m_quarterpel); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - fData->qpel, 0, 0); |
1645 |
|
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - fData->qpel, 0, 0); |
1646 |
|
|
1647 |
if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx; |
if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx; |
1648 |
if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx; |
if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx; |
1681 |
if (fData->qpel) { |
if (fData->qpel) { |
1682 |
CheckCandidate = CheckCandidateInt; |
CheckCandidate = CheckCandidateInt; |
1683 |
fData->qpel_precision = bData.qpel_precision = 1; |
fData->qpel_precision = bData.qpel_precision = 1; |
1684 |
get_range_qpel(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0); |
1685 |
get_range_qpel(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode); |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 1, 0); |
1686 |
fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x; |
fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x; |
1687 |
fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; |
fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; |
1688 |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
1692 |
SubpelRefine(&bData); |
SubpelRefine(&bData); |
1693 |
} |
} |
1694 |
|
|
1695 |
*fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode. |
*fData->iMinSAD += (2+2) * fData->lambda16; // two bits are needed to code interpolate mode. |
1696 |
|
|
1697 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
1698 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
1734 |
const IMAGE * const b_refHV) |
const IMAGE * const b_refHV) |
1735 |
{ |
{ |
1736 |
uint32_t i, j; |
uint32_t i, j; |
1737 |
int32_t best_sad, skip_sad; |
int32_t best_sad; |
1738 |
|
uint32_t skip_sad; |
1739 |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
1740 |
static const VECTOR zeroMV={0,0}; |
static const VECTOR zeroMV={0,0}; |
1741 |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
1756 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1757 |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
1758 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
1759 |
Data.lambda16 = lambda_vec16[frame->quant] + 2; |
Data.lambda16 = lambda_vec16[frame->quant]; |
1760 |
|
Data.chroma = frame->quant; |
1761 |
Data.qpel = pParam->m_quarterpel; |
Data.qpel = pParam->m_quarterpel; |
1762 |
Data.rounding = 0; |
Data.rounding = 0; |
1763 |
|
|
1830 |
pMB, &best_sad, |
pMB, &best_sad, |
1831 |
&Data); |
&Data); |
1832 |
|
|
1833 |
|
// final skip decision |
1834 |
|
if ( (skip_sad < frame->quant * MAX_SAD00_FOR_SKIP*2) |
1835 |
|
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) |
1836 |
|
SkipDecisionB(&frame->image, f_ref, b_ref, pMB,frame->quant, i, j, &Data); |
1837 |
|
|
1838 |
switch (pMB->mode) { |
switch (pMB->mode) { |
1839 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1840 |
f_count++; |
f_count++; |
1841 |
if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0]; |
if (Data.qpel) f_predMV = pMB->qmvs[0]; |
1842 |
else f_predMV = pMB->mvs[0]; |
else f_predMV = pMB->mvs[0]; |
1843 |
break; |
break; |
1844 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1845 |
b_count++; |
b_count++; |
1846 |
if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0]; |
if (Data.qpel) b_predMV = pMB->b_qmvs[0]; |
1847 |
else b_predMV = pMB->b_mvs[0]; |
else b_predMV = pMB->b_mvs[0]; |
1848 |
break; |
break; |
1849 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
1850 |
i_count++; |
i_count++; |
1851 |
if (pParam->m_quarterpel) { |
if (Data.qpel) { |
1852 |
f_predMV = pMB->qmvs[0]; |
f_predMV = pMB->qmvs[0]; |
1853 |
b_predMV = pMB->b_qmvs[0]; |
b_predMV = pMB->b_qmvs[0]; |
1854 |
} else { |
} else { |
1867 |
free(qimage); |
free(qimage); |
1868 |
} |
} |
1869 |
|
|
|
/* Hinted ME starts here */ |
|
|
|
|
|
static void |
|
|
SearchPhinted ( const IMAGE * const pRef, |
|
|
const uint8_t * const pRefH, |
|
|
const uint8_t * const pRefV, |
|
|
const uint8_t * const pRefHV, |
|
|
const IMAGE * const pCur, |
|
|
const int x, |
|
|
const int y, |
|
|
const uint32_t MotionFlags, |
|
|
const uint32_t iQuant, |
|
|
const MBParam * const pParam, |
|
|
const MACROBLOCK * const pMBs, |
|
|
int inter4v, |
|
|
MACROBLOCK * const pMB, |
|
|
SearchData * const Data) |
|
|
{ |
|
|
|
|
|
int i, t; |
|
|
MainSearchFunc * MainSearchPtr; |
|
|
|
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
|
|
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel); |
|
|
|
|
|
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
|
|
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
|
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
|
|
|
|
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
|
|
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
|
|
Data->qpel_precision = 0; |
|
|
|
|
|
if (!(MotionFlags & PMV_HALFPEL16)) { |
|
|
Data->min_dx = EVEN(Data->min_dx); |
|
|
Data->max_dx = EVEN(Data->max_dx); |
|
|
Data->min_dy = EVEN(Data->min_dy); |
|
|
Data->max_dy = EVEN(Data->max_dy); |
|
|
} |
|
|
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
|
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
|
|
|
|
|
for(i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
|
|
|
|
|
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
|
|
|
|
|
if (inter4v || Data->chroma) CheckCandidate = CheckCandidate16; |
|
|
else CheckCandidate = CheckCandidate16no4v; |
|
|
|
|
|
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
|
|
pMB->mvs[0].y = EVEN(pMB->mvs[0].y); |
|
|
if (pMB->mvs[0].x > Data->max_dx) pMB->mvs[0].x = Data->max_dx; // this is in case iFcode changed |
|
|
if (pMB->mvs[0].x < Data->min_dx) pMB->mvs[0].x = Data->min_dx; |
|
|
if (pMB->mvs[0].y > Data->max_dy) pMB->mvs[0].y = Data->max_dy; |
|
|
if (pMB->mvs[0].y < Data->min_dy) pMB->mvs[0].y = Data->min_dy; |
|
|
|
|
|
(*CheckCandidate)(pMB->mvs[0].x, pMB->mvs[0].y, 0, &t, Data); |
|
|
|
|
|
if (pMB->mode == MODE_INTER4V) |
|
|
for (i = 1; i < 4; i++) { // all four vectors will be used as four predictions for 16x16 search |
|
|
pMB->mvs[i].x = EVEN(pMB->mvs[i].x); |
|
|
pMB->mvs[i].y = EVEN(pMB->mvs[i].y); |
|
|
if (!(make_mask(pMB->mvs, i))) |
|
|
(*CheckCandidate)(pMB->mvs[i].x, pMB->mvs[i].y, 0, &t, Data); |
|
|
} |
|
|
|
|
|
if (MotionFlags & PMV_USESQUARES16) |
|
|
MainSearchPtr = SquareSearch; |
|
|
else if (MotionFlags & PMV_ADVANCEDDIAMOND16) |
|
|
MainSearchPtr = AdvDiamondSearch; |
|
|
else MainSearchPtr = DiamondSearch; |
|
|
|
|
|
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
|
|
|
|
|
if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data); |
|
|
|
|
|
for(i = 0; i < 5; i++) { |
|
|
Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors |
|
|
Data->currentQMV[i].y = 2 * Data->currentMV[i].y; |
|
|
} |
|
|
|
|
|
if((pParam->m_quarterpel) && (MotionFlags & PMV_QUARTERPELREFINE16)) { |
|
|
get_range_qpel(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
|
|
pParam->width, pParam->height, Data->iFcode); |
|
|
Data->qpel_precision = 1; |
|
|
SubpelRefine(Data); |
|
|
} |
|
|
|
|
|
if (inter4v) { |
|
|
SearchData Data8; |
|
|
Data8.iFcode = Data->iFcode; |
|
|
Data8.lambda8 = Data->lambda8; |
|
|
Data8.iEdgedWidth = Data->iEdgedWidth; |
|
|
Data8.RefQ = Data->RefQ; |
|
|
Data8.qpel = Data->qpel; |
|
|
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
|
|
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
|
|
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
|
|
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
|
|
|
|
|
if (Data->chroma) { |
|
|
int sumx, sumy, dx, dy; |
|
|
|
|
|
if(pParam->m_quarterpel) { |
|
|
sumx= pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2; |
|
|
sumy = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2; |
|
|
} else { |
|
|
sumx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
|
|
sumy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
|
|
} |
|
|
dx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
|
|
dy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
|
|
|
|
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
|
|
} |
|
|
} |
|
|
|
|
|
if (!(inter4v) || |
|
|
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] + |
|
|
Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
|
|
// INTER MODE |
|
|
pMB->mode = MODE_INTER; |
|
|
pMB->mvs[0] = pMB->mvs[1] |
|
|
= pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
|
|
|
|
|
pMB->qmvs[0] = pMB->qmvs[1] |
|
|
= pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; |
|
|
|
|
|
pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = |
|
|
pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0]; |
|
|
|
|
|
if(pParam->m_quarterpel) { |
|
|
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; |
|
|
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; |
|
|
} else { |
|
|
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
|
|
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
|
|
} |
|
|
} else { |
|
|
// INTER4V MODE; all other things are already set in Search8 |
|
|
pMB->mode = MODE_INTER4V; |
|
|
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
|
|
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
void |
|
|
MotionEstimationHinted( MBParam * const pParam, |
|
|
FRAMEINFO * const current, |
|
|
FRAMEINFO * const reference, |
|
|
const IMAGE * const pRefH, |
|
|
const IMAGE * const pRefV, |
|
|
const IMAGE * const pRefHV) |
|
|
{ |
|
|
MACROBLOCK *const pMBs = current->mbs; |
|
|
const IMAGE *const pCurrent = ¤t->image; |
|
|
const IMAGE *const pRef = &reference->image; |
|
|
|
|
|
uint32_t x, y; |
|
|
uint8_t * qimage; |
|
|
int32_t temp[5], quant = current->quant; |
|
|
int32_t iMinSAD[5]; |
|
|
VECTOR currentMV[5], currentQMV[5]; |
|
|
SearchData Data; |
|
|
Data.iEdgedWidth = pParam->edged_width; |
|
|
Data.currentMV = currentMV; |
|
|
Data.currentQMV = currentQMV; |
|
|
Data.iMinSAD = iMinSAD; |
|
|
Data.temp = temp; |
|
|
Data.iFcode = current->fcode; |
|
|
Data.rounding = pParam->m_rounding_type; |
|
|
Data.qpel = pParam->m_quarterpel; |
|
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
|
|
|
|
|
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
|
|
return; // allocate some mem for qpel interpolated blocks |
|
|
// somehow this is dirty since I think we shouldn't use malloc outside |
|
|
// encoder_create() - so please fix me! |
|
|
|
|
|
Data.RefQ = qimage; |
|
|
|
|
|
if (sadInit) (*sadInit) (); |
|
|
|
|
|
for (y = 0; y < pParam->mb_height; y++) { |
|
|
for (x = 0; x < pParam->mb_width; x++) { |
|
|
|
|
|
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
|
|
|
|
|
//intra mode is copied from the first pass. At least for the time being |
|
|
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
|
|
|
|
|
if (!(current->global_flags & XVID_LUMIMASKING)) { |
|
|
pMB->dquant = NO_CHANGE; |
|
|
pMB->quant = current->quant; } |
|
|
else { |
|
|
if (pMB->dquant != NO_CHANGE) { |
|
|
quant += DQtab[pMB->dquant]; |
|
|
if (quant > 31) quant = 31; |
|
|
else if (quant < 1) quant = 1; |
|
|
} |
|
|
pMB->quant = quant; |
|
|
} |
|
|
|
|
|
SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
|
|
y, current->motion_flags, pMB->quant, |
|
|
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
|
|
&Data); |
|
|
|
|
|
} |
|
|
} |
|
|
free(qimage); |
|
|
} |
|
|
|
|
1870 |
static __inline int |
static __inline int |
1871 |
MEanalyzeMB ( const uint8_t * const pRef, |
MEanalyzeMB ( const uint8_t * const pRef, |
1872 |
const uint8_t * const pCur, |
const uint8_t * const pCur, |
1892 |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
1893 |
|
|
1894 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1895 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, 0); |
1896 |
|
|
1897 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
1898 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
1903 |
pmv[2].y = EVEN(Data->predMV.y); |
pmv[2].y = EVEN(Data->predMV.y); |
1904 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
1905 |
|
|
1906 |
(*CheckCandidate)(0, 0, 255, &i, Data); |
CheckCandidate16no4vI(0, 0, 255, &i, Data); |
1907 |
|
|
1908 |
//early skip for 0,0 |
//early skip for 0,0 |
1909 |
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
1913 |
} |
} |
1914 |
|
|
1915 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
1916 |
(*CheckCandidate)(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data); |
1917 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
1918 |
(*CheckCandidate)(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate16no4vI(pmv[2].x, pmv[2].y, mask, &i, Data); |
1919 |
|
|
1920 |
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 6) // diamond only if needed |
1921 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
1922 |
|
|
1923 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
1987 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
1988 |
emms(); |
emms(); |
1989 |
return B_VOP; |
return B_VOP; |
1990 |
|
|
1991 |
} |
} |
1992 |
|
|
1993 |
static void |
static void |
2015 |
} |
} |
2016 |
|
|
2017 |
|
|
2018 |
static VECTOR |
static __inline VECTOR |
2019 |
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode) |
2020 |
{ |
{ |
2021 |
|
|