46 |
int sad; |
int sad; |
47 |
const uint32_t stride = data->iEdgedWidth/2; |
const uint32_t stride = data->iEdgedWidth/2; |
48 |
uint8_t *f_refu, *f_refv, *b_refu, *b_refv; |
uint8_t *f_refu, *f_refv, *b_refu, *b_refv; |
49 |
|
int offset, filter; |
50 |
|
|
51 |
const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { |
const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = { |
52 |
NULL, |
NULL, |
55 |
interpolate8x8_halfpel_hv |
interpolate8x8_halfpel_hv |
56 |
}; |
}; |
57 |
|
|
58 |
int offset = (fx>>1) + (fy>>1)*stride; |
offset = (fx>>1) + (fy>>1)*stride; |
59 |
int filter = ((fx & 1) << 1) | (fy & 1); |
filter = ((fx & 1) << 1) | (fy & 1); |
60 |
|
|
61 |
if (filter != 0) { |
if (filter != 0) { |
62 |
f_refu = data->RefQ; |
f_refu = data->RefQ; |
146 |
} |
} |
147 |
|
|
148 |
static void |
static void |
|
CheckCandidateInt_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
|
|
{ |
|
|
int32_t sad, xf, yf, xb, yb, xcf, ycf, xcb, ycb; |
|
|
uint32_t t; |
|
|
|
|
|
const uint8_t *ReferenceF, *ReferenceB; |
|
|
VECTOR *current; |
|
|
|
|
|
if ((x > data->max_dx) || (x < data->min_dx) || |
|
|
(y > data->max_dy) || (y < data->min_dy)) |
|
|
return; |
|
|
|
|
|
if (Direction == 1) { /* x and y mean forward vector */ |
|
|
VECTOR backward = data->qpel_precision ? data->currentQMV[1] : data->currentMV[1]; |
|
|
xb = backward.x; |
|
|
yb = backward.y; |
|
|
xf = x; yf = y; |
|
|
} else { /* x and y mean backward vector */ |
|
|
VECTOR forward = data->qpel_precision ? data->currentQMV[0] : data->currentMV[0]; |
|
|
xf = forward.x; |
|
|
yf = forward.y; |
|
|
xb = x; yb = y; |
|
|
} |
|
|
|
|
|
ReferenceF = xvid_me_interpolate16x16qpel(xf, yf, 0, data); |
|
|
current = data->currentQMV + Direction - 1; |
|
|
ReferenceB = xvid_me_interpolate16x16qpel(xb, yb, 1, data); |
|
|
xcf = xf/2; ycf = yf/2; |
|
|
xcb = xb/2; ycb = yb/2; |
|
|
|
|
|
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
|
|
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
|
|
|
|
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
|
|
sad += (data->lambda16 * t * sad)>>10; |
|
|
|
|
|
if (data->chroma && sad < *data->iMinSAD) |
|
|
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
|
|
(ycf >> 1) + roundtab_79[ycf & 0x3], |
|
|
(xcb >> 1) + roundtab_79[xcb & 0x3], |
|
|
(ycb >> 1) + roundtab_79[ycb & 0x3], data); |
|
|
|
|
|
if (sad < *(data->iMinSAD)) { |
|
|
*data->iMinSAD = sad; |
|
|
current->x = x; current->y = y; |
|
|
data->dir = Direction; |
|
|
} |
|
|
|
|
|
if (sad < *(data->iMinSAD)) { |
|
|
data->iMinSAD2 = *(data->iMinSAD); |
|
|
data->currentQMV2.x = current->x; |
|
|
data->currentQMV2.y = current->y; |
|
|
|
|
|
*data->iMinSAD = sad; |
|
|
current->x = x; current->y = y; |
|
|
} else if (sad < data->iMinSAD2) { |
|
|
data->iMinSAD2 = sad; |
|
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
|
|
} |
|
|
} |
|
|
|
|
|
static void |
|
149 |
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
CheckCandidateDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
150 |
{ |
{ |
151 |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0; |
298 |
} |
} |
299 |
} |
} |
300 |
|
|
|
void |
|
|
CheckCandidate16no4v_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction) |
|
|
{ |
|
|
int32_t sad, xc, yc; |
|
|
const uint8_t * Reference; |
|
|
uint32_t t; |
|
|
|
|
|
if ( (x > data->max_dx) || ( x < data->min_dx) |
|
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|
|
|
|
|
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ |
|
|
|
|
|
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
|
|
|
|
|
xc = x/2; yc = y/2; |
|
|
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
|
|
data->qpel^data->qpel_precision, data->rrv); |
|
|
|
|
|
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
|
|
sad += (data->lambda16 * t * sad)>>10; |
|
|
|
|
|
if (data->chroma && sad < *data->iMinSAD) |
|
|
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
|
|
(yc >> 1) + roundtab_79[yc & 0x3], data); |
|
|
|
|
|
if (sad < *(data->iMinSAD)) { |
|
|
data->iMinSAD2 = *(data->iMinSAD); |
|
|
data->currentQMV2.x = data->currentQMV->x; |
|
|
data->currentQMV2.y = data->currentQMV->y; |
|
|
|
|
|
data->iMinSAD[0] = sad; |
|
|
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
|
|
} else if (sad < data->iMinSAD2) { |
|
|
data->iMinSAD2 = sad; |
|
|
data->currentQMV2.x = x; data->currentQMV2.y = y; |
|
|
} |
|
|
} |
|
|
|
|
301 |
static __inline VECTOR |
static __inline VECTOR |
302 |
ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) |
ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode) |
303 |
{ |
{ |
402 |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); |
MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v); |
403 |
} |
} |
404 |
|
|
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
|
405 |
|
|
406 |
if (Data->qpel && (*Data->iMinSAD < *best_sad + threshA)) { |
|
407 |
|
|
408 |
|
if(!Data->qpel) { |
409 |
|
/* halfpel mode */ |
410 |
|
if (MotionFlags & XVID_ME_HALFPELREFINE16) |
411 |
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v, 0); |
412 |
|
} else { |
413 |
|
/* qpel mode */ |
414 |
|
if(MotionFlags & XVID_ME_FASTREFINE16) { |
415 |
|
/* fast */ |
416 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
417 |
|
pParam->width, pParam->height, Data->iFcode, 2, 0); |
418 |
|
if (*Data->iMinSAD < *best_sad + 2*threshA) |
419 |
|
FullRefine_Fast(Data, CheckCandidate16no4v, 0); |
420 |
|
} else { |
421 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
422 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
|
Data->qpel_precision = 1; |
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
|
|
pParam->width, pParam->height, iFcode, 2, 0); |
|
|
|
|
423 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
424 |
if (MotionFlags & XVID_ME_FASTREFINE16) |
/* full */ |
425 |
SubpelRefine_Fast(Data, CheckCandidate16no4v_qpel); |
if (MotionFlags & XVID_ME_HALFPELREFINE16) { |
426 |
else |
xvid_me_SubpelRefine(Data, CheckCandidate16no4v, 0); /* hpel part */ |
427 |
xvid_me_SubpelRefine(Data, CheckCandidate16no4v); |
Data->currentQMV->x = 2*Data->currentMV->x; |
428 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
429 |
|
} |
430 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
431 |
|
pParam->width, pParam->height, Data->iFcode, 2, 0); |
432 |
|
Data->qpel_precision = 1; |
433 |
|
if (*Data->iMinSAD < *best_sad + threshA) |
434 |
|
xvid_me_SubpelRefine(Data, CheckCandidate16no4v, 0); /* qpel part */ |
435 |
|
} |
436 |
} |
} |
437 |
} |
} |
438 |
|
|
606 |
|
|
607 |
MainSearchPtr(0, 0, Data, 255, CheckCandidate); |
MainSearchPtr(0, 0, Data, 255, CheckCandidate); |
608 |
|
|
609 |
xvid_me_SubpelRefine(Data, CheckCandidate); |
xvid_me_SubpelRefine(Data, CheckCandidate, 0); |
610 |
} |
} |
611 |
|
|
612 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
653 |
} |
} |
654 |
|
|
655 |
static void |
static void |
|
SubpelRefine_dir(SearchData * const data, CheckFunc * const CheckCandidate, const int dir) |
|
|
{ |
|
|
/* Do a half-pel or q-pel refinement */ |
|
|
const VECTOR centerMV = data->qpel_precision ? |
|
|
data->currentQMV[dir-1] : data->currentMV[dir-1]; |
|
|
|
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir); |
|
|
} |
|
|
|
|
|
/* Pretty much redundant code, just as SubpelRefine_dir above too |
|
|
* |
|
|
* TODO: Get rid off all the redundancy (SubpelRefine_Fast_dir, |
|
|
* CheckCandidate16no4v_qpel etc.) */ |
|
|
|
|
|
void |
|
|
SubpelRefine_Fast_dir(SearchData * data, CheckFunc * CheckCandidate, const int dir) |
|
|
{ |
|
|
/* Do a fast q-pel refinement */ |
|
|
VECTOR centerMV; |
|
|
VECTOR second_best; |
|
|
int best_sad = *data->iMinSAD; |
|
|
int xo, yo, xo2, yo2; |
|
|
int size = 2; |
|
|
data->iMinSAD2 = 0; |
|
|
|
|
|
/* check all halfpixel positions near our best halfpel position */ |
|
|
centerMV = data->currentQMV[dir-1]; |
|
|
*data->iMinSAD = 256 * 4096; |
|
|
|
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y - size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y - size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x + size, centerMV.y + size, dir); |
|
|
|
|
|
CHECK_CANDIDATE(centerMV.x, centerMV.y + size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y + size, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y, dir); |
|
|
CHECK_CANDIDATE(centerMV.x - size, centerMV.y - size, dir); |
|
|
|
|
|
second_best = data->currentQMV[dir-1]; |
|
|
|
|
|
/* after second_best has been found, go back to the vector we began with */ |
|
|
|
|
|
data->currentQMV[dir-1] = centerMV; |
|
|
*data->iMinSAD = best_sad; |
|
|
|
|
|
xo = centerMV.x; |
|
|
yo = centerMV.y; |
|
|
xo2 = second_best.x; |
|
|
yo2 = second_best.y; |
|
|
|
|
|
data->iMinSAD2 = 256 * 4096; |
|
|
|
|
|
if (yo == yo2) { |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
|
|
CHECK_CANDIDATE(xo, yo-1, dir); |
|
|
CHECK_CANDIDATE(xo, yo+1, dir); |
|
|
|
|
|
if(best_sad <= data->iMinSAD2) return; |
|
|
|
|
|
if(data->currentQMV[dir-1].x == data->currentQMV2.x) { |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo-1, dir); |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo+1, dir); |
|
|
} else { |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, |
|
|
(data->currentQMV[dir-1].x == xo) ? data->currentQMV[dir-1].y : data->currentQMV2.y, dir); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
if (xo == xo2) { |
|
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
|
|
CHECK_CANDIDATE(xo-1, yo, dir); |
|
|
CHECK_CANDIDATE(xo+1, yo, dir); |
|
|
|
|
|
if(best_sad < data->iMinSAD2) return; |
|
|
|
|
|
if(data->currentQMV[dir-1].y == data->currentQMV2.y) { |
|
|
CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, dir); |
|
|
CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, dir); |
|
|
} else { |
|
|
CHECK_CANDIDATE((data->currentQMV[dir-1].y == yo) ? data->currentQMV[dir-1].x : data->currentQMV2.x, (yo+yo2)>>1, dir); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
CHECK_CANDIDATE(xo, (yo+yo2)>>1, dir); |
|
|
CHECK_CANDIDATE((xo+xo2)>>1, yo, dir); |
|
|
|
|
|
if(best_sad <= data->iMinSAD2) return; |
|
|
|
|
|
CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, dir); |
|
|
} |
|
|
|
|
|
static void |
|
656 |
SearchInterpolate(const IMAGE * const f_Ref, |
SearchInterpolate(const IMAGE * const f_Ref, |
657 |
const uint8_t * const f_RefH, |
const uint8_t * const f_RefH, |
658 |
const uint8_t * const f_RefV, |
const uint8_t * const f_RefV, |
754 |
Data->currentQMV[1].x = 2 * Data->currentMV[1].x; |
Data->currentQMV[1].x = 2 * Data->currentMV[1].x; |
755 |
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
Data->currentQMV[1].y = 2 * Data->currentMV[1].y; |
756 |
|
|
757 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) |
758 |
if (MotionFlags & XVID_ME_FASTREFINE16) |
xvid_me_SubpelRefine(Data, CheckCandidateInt, 1); |
|
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 1); |
|
|
else |
|
|
SubpelRefine_dir(Data, CheckCandidateInt, 1); |
|
|
} |
|
759 |
|
|
760 |
if (*Data->iMinSAD > *best_sad + threshB) return; |
if (*Data->iMinSAD > *best_sad + threshB) return; |
761 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0); |
762 |
|
|
763 |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) { |
if (MotionFlags & XVID_ME_QUARTERPELREFINE16) |
764 |
if (MotionFlags & XVID_ME_FASTREFINE16) |
xvid_me_SubpelRefine(Data, CheckCandidateInt, 2); |
|
SubpelRefine_Fast_dir(Data, CheckCandidateInt_qpel, 2); |
|
|
else |
|
|
SubpelRefine_dir(Data, CheckCandidateInt, 2); |
|
|
} |
|
765 |
} |
} |
766 |
|
|
767 |
*Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
*Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */ |
852 |
} |
} |
853 |
|
|
854 |
Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant]; |
Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant]; |
|
|
|
855 |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
856 |
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8; |
857 |
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |
Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8; |