4 |
* - Motion Estimation for B-VOPs - |
* - Motion Estimation for B-VOPs - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
* Copyright(C) 2002 Christoph Lampert <gruel@web.de> |
7 |
* 2002 Michael Militzer <michael@xvid.org> |
* 2002-2010 Michael Militzer <michael@xvid.org> |
8 |
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
9 |
* |
* |
10 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
137 |
xcb = xb/2; ycb = yb/2; |
xcb = xb/2; ycb = yb/2; |
138 |
} |
} |
139 |
|
|
140 |
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision) |
141 |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision); |
142 |
|
|
143 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
144 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t); |
145 |
|
|
146 |
if (data->chroma && sad < *data->iMinSAD) |
if (data->chroma && sad < *data->iMinSAD) |
147 |
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3], |
207 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
208 |
} |
} |
209 |
|
|
210 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0)); |
211 |
|
|
212 |
if (data->chroma && sad < *data->iMinSAD) |
if (data->chroma && sad < *data->iMinSAD) |
213 |
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
265 |
|
|
266 |
done: |
done: |
267 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
268 |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0)); |
269 |
|
|
270 |
if (data->chroma && sad < *data->iMinSAD) |
if (data->chroma && sad < *data->iMinSAD) |
271 |
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf], |
291 |
if ( (x > data->max_dx) || ( x < data->min_dx) |
if ( (x > data->max_dx) || ( x < data->min_dx) |
292 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
293 |
|
|
|
if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */ |
|
|
|
|
294 |
if (data->qpel_precision) { /* x and y are in 1/4 precision */ |
if (data->qpel_precision) { /* x and y are in 1/4 precision */ |
295 |
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); |
296 |
current = data->currentQMV; |
current = data->currentQMV; |
301 |
xc = x; yc = y; |
xc = x; yc = y; |
302 |
} |
} |
303 |
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
304 |
data->qpel^data->qpel_precision, data->rrv); |
data->qpel^data->qpel_precision); |
305 |
|
|
306 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
307 |
sad += (data->lambda16 * t * sad)>>10; |
sad += (data->lambda16 * t); |
308 |
|
|
309 |
if (data->chroma && sad < *data->iMinSAD) |
if (data->chroma && sad < *data->iMinSAD) |
310 |
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3], |
443 |
Data->predMV = *predMV; |
Data->predMV = *predMV; |
444 |
|
|
445 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
446 |
pParam->width, pParam->height, iFcode - Data->qpel, 1, 0); |
pParam->width, pParam->height, iFcode - Data->qpel, 1); |
447 |
|
|
448 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
449 |
if (Data->qpel) { |
if (Data->qpel) { |
490 |
if(MotionFlags & XVID_ME_FASTREFINE16) { |
if(MotionFlags & XVID_ME_FASTREFINE16) { |
491 |
/* fast */ |
/* fast */ |
492 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
493 |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
pParam->width, pParam->height, Data->iFcode, 2); |
494 |
FullRefine_Fast(Data, CheckCandidate16no4v, 0); |
FullRefine_Fast(Data, CheckCandidate16no4v, 0); |
495 |
|
|
496 |
} else { |
} else { |
505 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
506 |
} |
} |
507 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
508 |
pParam->width, pParam->height, Data->iFcode, 2, 0); |
pParam->width, pParam->height, Data->iFcode, 2); |
509 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
510 |
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate16no4v, 0); /* qpel part */ |
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidate16no4v, 0); /* qpel part */ |
511 |
} |
} |
539 |
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
b_dx = (b_dx >> 3) + roundtab_76[b_dx & 0xf]; |
540 |
|
|
541 |
sum = sad8bi(Data->CurU, |
sum = sad8bi(Data->CurU, |
542 |
Data->RefP[4] + (dy/2) * stride + dx/2, |
Data->RefP[4] + (dy/2) * (int)stride + dx/2, |
543 |
Data->b_RefP[4] + (b_dy/2) * stride + b_dx/2, |
Data->b_RefP[4] + (b_dy/2) * (int)stride + b_dx/2, |
544 |
stride); |
stride); |
545 |
|
|
546 |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
547 |
|
|
548 |
sum += sad8bi(Data->CurV, |
sum += sad8bi(Data->CurV, |
549 |
Data->RefP[5] + (dy/2) * stride + dx/2, |
Data->RefP[5] + (dy/2) * (int)stride + dx/2, |
550 |
Data->b_RefP[5] + (b_dy/2) * stride + b_dx/2, |
Data->b_RefP[5] + (b_dy/2) * (int)stride + b_dx/2, |
551 |
stride); |
stride); |
552 |
|
|
553 |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */ |
641 |
Data->directmvB[0].y |
Data->directmvB[0].y |
642 |
: Data->currentMV[1].y - Data->referencemv[0].y); |
: Data->currentMV[1].y - Data->referencemv[0].y); |
643 |
|
|
644 |
|
*best_sad = Data->iMinSAD[0]; |
645 |
|
|
646 |
return skip_sad; |
return skip_sad; |
647 |
} |
} |
648 |
|
|
711 |
Data->currentMV[0] = startF; |
Data->currentMV[0] = startF; |
712 |
Data->currentMV[1] = startB; |
Data->currentMV[1] = startB; |
713 |
|
|
714 |
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, 0); |
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
715 |
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1, 0); |
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1); |
716 |
|
|
717 |
if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; |
if (Data->currentMV[0].x > f_range[1]) Data->currentMV[0].x = f_range[1]; |
718 |
if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; |
if (Data->currentMV[0].x < f_range[0]) Data->currentMV[0].x = f_range[0]; |
741 |
int i, j; |
int i, j; |
742 |
int b_range[4], f_range[4]; |
int b_range[4], f_range[4]; |
743 |
|
|
744 |
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, 0); |
get_range(f_range, f_range+1, f_range+2, f_range+3, x, y, 4, pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
745 |
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1, 0); |
get_range(b_range, b_range+1, b_range+2, b_range+3, x, y, 4, pParam->width, pParam->height, Data->bFcode - Data->qpel, 1); |
746 |
|
|
747 |
/* diamond */ |
/* diamond */ |
748 |
do { |
do { |
772 |
if (Data->qpel) { |
if (Data->qpel) { |
773 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
774 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
775 |
x, y, 4, pParam->width, pParam->height, Data->iFcode, 2, 0); |
x, y, 4, pParam->width, pParam->height, Data->iFcode, 2); |
776 |
|
|
777 |
Data->currentQMV[0].x = 2 * Data->currentMV[0].x; |
Data->currentQMV[0].x = 2 * Data->currentMV[0].x; |
778 |
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
Data->currentQMV[0].y = 2 * Data->currentMV[0].y; |
783 |
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateInt, 1); |
xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateInt, 1); |
784 |
|
|
785 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
786 |
x, y, 4, pParam->width, pParam->height, Data->bFcode, 2, 0); |
x, y, 4, pParam->width, pParam->height, Data->bFcode, 2); |
787 |
|
|
788 |
xvid_me_SubpelRefine(Data->currentQMV[1], Data, CheckCandidateInt, 2); |
xvid_me_SubpelRefine(Data->currentQMV[1], Data, CheckCandidateInt, 2); |
789 |
} |
} |
829 |
|
|
830 |
pMB->sad16 = best_sad; |
pMB->sad16 = best_sad; |
831 |
pMB->mode = mode; |
pMB->mode = mode; |
832 |
|
pMB->cbp = 63; |
833 |
|
|
834 |
switch (mode) { |
switch (mode) { |
835 |
|
|
919 |
} |
} |
920 |
} |
} |
921 |
|
|
922 |
|
static __inline void |
923 |
|
maxMotionBVOP(int * const MVmaxF, int * const MVmaxB, const MACROBLOCK * const pMB, const int qpel) |
924 |
|
{ |
925 |
|
if (pMB->mode == MODE_FORWARD || pMB->mode == MODE_INTERPOLATE) { |
926 |
|
const VECTOR * const mv = qpel ? pMB->qmvs : pMB->mvs; |
927 |
|
int max = *MVmaxF; |
928 |
|
if (mv[0].x > max) max = mv[0].x; |
929 |
|
else if (-mv[0].x - 1 > max) max = -mv[0].x - 1; |
930 |
|
if (mv[0].y > max) max = mv[0].y; |
931 |
|
else if (-mv[0].y - 1 > max) max = -mv[0].y - 1; |
932 |
|
|
933 |
|
*MVmaxF = max; |
934 |
|
} |
935 |
|
|
936 |
|
if (pMB->mode == MODE_BACKWARD || pMB->mode == MODE_INTERPOLATE) { |
937 |
|
const VECTOR * const mv = qpel ? pMB->b_qmvs : pMB->b_mvs; |
938 |
|
int max = *MVmaxB; |
939 |
|
if (mv[0].x > max) max = mv[0].x; |
940 |
|
else if (-mv[0].x - 1 > max) max = -mv[0].x - 1; |
941 |
|
if (mv[0].y > max) max = mv[0].y; |
942 |
|
else if (-mv[0].y - 1 > max) max = -mv[0].y - 1; |
943 |
|
*MVmaxB = max; |
944 |
|
} |
945 |
|
} |
946 |
|
|
947 |
|
|
948 |
void |
void |
949 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
950 |
FRAMEINFO * const frame, |
FRAMEINFO * const frame, |
965 |
{ |
{ |
966 |
uint32_t i, j; |
uint32_t i, j; |
967 |
int32_t best_sad = 256*4096; |
int32_t best_sad = 256*4096; |
|
int32_t sad2; |
|
968 |
uint32_t skip_sad; |
uint32_t skip_sad; |
969 |
|
int fb_thresh; |
970 |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
const MACROBLOCK * const b_mbs = b_reference->mbs; |
971 |
|
|
972 |
VECTOR f_predMV, b_predMV; |
VECTOR f_predMV, b_predMV; |
973 |
|
|
974 |
|
int MVmaxF = 0, MVmaxB = 0; |
975 |
const int32_t TRB = time_pp - time_bp; |
const int32_t TRB = time_pp - time_bp; |
976 |
const int32_t TRD = time_pp; |
const int32_t TRD = time_pp; |
977 |
DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); |
985 |
Data_d.rounding = 0; |
Data_d.rounding = 0; |
986 |
Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
987 |
Data_d.iQuant = frame->quant; |
Data_d.iQuant = frame->quant; |
988 |
|
Data_d.quant_sq = frame->quant*frame->quant; |
989 |
Data_d.dctSpace = dct_space; |
Data_d.dctSpace = dct_space; |
990 |
Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
991 |
Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices; |
Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices; |
996 |
memcpy(&Data_b, &Data_d, sizeof(SearchData)); |
memcpy(&Data_b, &Data_d, sizeof(SearchData)); |
997 |
memcpy(&Data_i, &Data_d, sizeof(SearchData)); |
memcpy(&Data_i, &Data_d, sizeof(SearchData)); |
998 |
|
|
999 |
|
Data_f.iFcode = Data_i.iFcode = frame->fcode = b_reference->fcode; |
1000 |
|
Data_b.iFcode = Data_i.bFcode = frame->bcode = b_reference->fcode; |
1001 |
|
|
1002 |
|
for (j = 0; j < pParam->mb_height; j++) { |
1003 |
|
|
1004 |
|
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
1005 |
|
|
1006 |
|
for (i = 0; i < pParam->mb_width; i++) { |
1007 |
|
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
1008 |
|
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1009 |
|
pMB->mode = -1; |
1010 |
|
|
1011 |
|
initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, |
1012 |
|
i, j, f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1013 |
|
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1014 |
|
&frame->image, b_mb); |
1015 |
|
|
1016 |
|
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
1017 |
|
if (b_reference->coding_type != S_VOP) |
1018 |
|
if (b_mb->mode == MODE_NOT_CODED) { |
1019 |
|
pMB->mode = MODE_NOT_CODED; |
1020 |
|
pMB->mvs[0] = pMB->b_mvs[0] = zeroMV; |
1021 |
|
pMB->sad16 = 0; |
1022 |
|
continue; |
1023 |
|
} |
1024 |
|
|
1025 |
|
/* direct search comes first, because it (1) checks for SKIP-mode |
1026 |
|
and (2) sets very good predictions for forward and backward search */ |
1027 |
|
skip_sad = SearchDirect_initial(i, j, frame->motion_flags, TRB, TRD, pParam, pMB, |
1028 |
|
b_mb, &best_sad, &Data_d); |
1029 |
|
|
1030 |
|
if (pMB->mode == MODE_DIRECT_NONE_MV) { |
1031 |
|
pMB->sad16 = best_sad; |
1032 |
|
pMB->cbp = 0; |
1033 |
|
continue; |
1034 |
|
} |
1035 |
|
|
1036 |
|
SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, |
1037 |
|
&f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]); |
1038 |
|
|
1039 |
|
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
1040 |
|
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); |
1041 |
|
|
1042 |
|
if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) |
1043 |
|
fb_thresh = best_sad; |
1044 |
|
else |
1045 |
|
fb_thresh = best_sad + (best_sad>>1); |
1046 |
|
|
1047 |
|
if (Data_f.iMinSAD[0] <= fb_thresh) |
1048 |
|
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f); |
1049 |
|
|
1050 |
|
if (Data_b.iMinSAD[0] <= fb_thresh) |
1051 |
|
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b); |
1052 |
|
|
1053 |
|
SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad, |
1054 |
|
&Data_i, Data_f.currentMV[0], Data_b.currentMV[0]); |
1055 |
|
|
1056 |
|
if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) |
1057 |
|
|| Data_i.iMinSAD[0] <= best_sad) |
1058 |
|
|
1059 |
|
SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i); |
1060 |
|
|
1061 |
|
if (Data_d.iMinSAD[0] <= 2*best_sad) |
1062 |
|
if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750)) |
1063 |
|
|| (best_sad > 1000)) |
1064 |
|
|
1065 |
|
SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); |
1066 |
|
|
1067 |
|
/* final skip decision */ |
1068 |
|
if ( (skip_sad < 2 * Data_d.iQuant * MAX_SAD00_FOR_SKIP ) |
1069 |
|
&& ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) ) { |
1070 |
|
|
1071 |
|
Data_d.chromaSAD = 0; /* green light for chroma check */ |
1072 |
|
|
1073 |
|
SkipDecisionB(pMB, &Data_d); |
1074 |
|
|
1075 |
|
if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */ |
1076 |
|
pMB->sad16 = skip_sad; |
1077 |
|
pMB->cbp = 0; |
1078 |
|
continue; |
1079 |
|
} |
1080 |
|
} |
1081 |
|
|
1082 |
|
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
1083 |
|
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
1084 |
|
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad); |
1085 |
|
else |
1086 |
|
ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV); |
1087 |
|
|
1088 |
|
maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel); |
1089 |
|
|
1090 |
|
} |
1091 |
|
} |
1092 |
|
|
1093 |
|
frame->fcode = getMinFcode(MVmaxF); |
1094 |
|
frame->bcode = getMinFcode(MVmaxB); |
1095 |
|
} |
1096 |
|
|
1097 |
|
|
1098 |
|
|
1099 |
|
void |
1100 |
|
SMPMotionEstimationBVOP(SMPmotionData * h) |
1101 |
|
{ |
1102 |
|
const MBParam * const pParam = h->pParam; |
1103 |
|
const FRAMEINFO * const frame = h->current; |
1104 |
|
const int32_t time_bp = h->time_bp; |
1105 |
|
const int32_t time_pp = h->time_pp; |
1106 |
|
/* forward (past) reference */ |
1107 |
|
const MACROBLOCK * const f_mbs = h->f_mbs; |
1108 |
|
const IMAGE * const f_ref = h->fRef; |
1109 |
|
const IMAGE * const f_refH = h->fRefH; |
1110 |
|
const IMAGE * const f_refV = h->fRefV; |
1111 |
|
const IMAGE * const f_refHV = h->fRefHV; |
1112 |
|
/* backward (future) reference */ |
1113 |
|
const FRAMEINFO * const b_reference = h->reference; |
1114 |
|
const IMAGE * const b_ref = h->pRef; |
1115 |
|
const IMAGE * const b_refH = h->pRefH; |
1116 |
|
const IMAGE * const b_refV = h->pRefV; |
1117 |
|
const IMAGE * const b_refHV = h->pRefHV; |
1118 |
|
|
1119 |
|
int y_step = h->y_step; |
1120 |
|
int start_y = h->start_y; |
1121 |
|
int * complete_count_self = h->complete_count_self; |
1122 |
|
const int * complete_count_above = h->complete_count_above; |
1123 |
|
int max_mbs; |
1124 |
|
int current_mb = 0; |
1125 |
|
|
1126 |
|
int32_t i, j; |
1127 |
|
int32_t best_sad = 256*4096; |
1128 |
|
uint32_t skip_sad; |
1129 |
|
int fb_thresh; |
1130 |
|
const MACROBLOCK * const b_mbs = b_reference->mbs; |
1131 |
|
|
1132 |
|
VECTOR f_predMV, b_predMV; |
1133 |
|
|
1134 |
|
int MVmaxF = 0, MVmaxB = 0; |
1135 |
|
const int32_t TRB = time_pp - time_bp; |
1136 |
|
const int32_t TRD = time_pp; |
1137 |
|
DECLARE_ALIGNED_MATRIX(dct_space, 3, 64, int16_t, CACHE_LINE); |
1138 |
|
|
1139 |
|
/* some pre-inintialized data for the rest of the search */ |
1140 |
|
SearchData Data_d, Data_f, Data_b, Data_i; |
1141 |
|
memset(&Data_d, 0, sizeof(SearchData)); |
1142 |
|
|
1143 |
|
Data_d.iEdgedWidth = pParam->edged_width; |
1144 |
|
Data_d.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0; |
1145 |
|
Data_d.rounding = 0; |
1146 |
|
Data_d.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP; |
1147 |
|
Data_d.iQuant = frame->quant; |
1148 |
|
Data_d.quant_sq = frame->quant*frame->quant; |
1149 |
|
Data_d.dctSpace = dct_space; |
1150 |
|
Data_d.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT); |
1151 |
|
Data_d.mpeg_quant_matrices = pParam->mpeg_quant_matrices; |
1152 |
|
|
1153 |
|
Data_d.RefQ = h->RefQ; |
1154 |
|
|
1155 |
|
memcpy(&Data_f, &Data_d, sizeof(SearchData)); |
1156 |
|
memcpy(&Data_b, &Data_d, sizeof(SearchData)); |
1157 |
|
memcpy(&Data_i, &Data_d, sizeof(SearchData)); |
1158 |
|
|
1159 |
Data_f.iFcode = Data_i.iFcode = frame->fcode; |
Data_f.iFcode = Data_i.iFcode = frame->fcode; |
1160 |
Data_b.iFcode = Data_i.bFcode = frame->bcode; |
Data_b.iFcode = Data_i.bFcode = frame->bcode; |
1161 |
|
|
1162 |
|
max_mbs = 0; |
1163 |
|
|
1164 |
for (j = 0; j < pParam->mb_height; j++) { |
for (j = start_y; j < pParam->mb_height; j += y_step) { |
1165 |
|
if (j == 0) max_mbs = pParam->mb_width; /* we can process all blocks of the first row */ |
1166 |
|
|
1167 |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
1168 |
|
|
1176 |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1177 |
&frame->image, b_mb); |
&frame->image, b_mb); |
1178 |
|
|
1179 |
|
if (current_mb >= max_mbs) { |
1180 |
|
/* we ME-ed all macroblocks we safely could. grab next portion */ |
1181 |
|
int above_count = *complete_count_above; /* sync point */ |
1182 |
|
if (above_count == pParam->mb_width) { |
1183 |
|
/* full line above is ready */ |
1184 |
|
above_count = pParam->mb_width+1; |
1185 |
|
if (j < pParam->mb_height-y_step) { |
1186 |
|
/* this is not last line, grab a portion of MBs from the next line too */ |
1187 |
|
above_count += MAX(0, complete_count_above[1] - 1); |
1188 |
|
} |
1189 |
|
} |
1190 |
|
|
1191 |
|
max_mbs = current_mb + above_count - i - 1; |
1192 |
|
|
1193 |
|
if (current_mb >= max_mbs) { |
1194 |
|
/* current workload is zero */ |
1195 |
|
i--; |
1196 |
|
sched_yield(); |
1197 |
|
continue; |
1198 |
|
} |
1199 |
|
} |
1200 |
|
|
1201 |
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
/* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */ |
1202 |
if (b_reference->coding_type != S_VOP) |
if (b_reference->coding_type != S_VOP) |
1203 |
if (b_mb->mode == MODE_NOT_CODED) { |
if (b_mb->mode == MODE_NOT_CODED) { |
1204 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1205 |
pMB->mvs[0] = pMB->b_mvs[0] = zeroMV; |
pMB->mvs[0] = pMB->b_mvs[0] = zeroMV; |
1206 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1207 |
|
*complete_count_self = i+1; |
1208 |
|
current_mb++; |
1209 |
continue; |
continue; |
1210 |
} |
} |
1211 |
|
|
1216 |
|
|
1217 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { |
if (pMB->mode == MODE_DIRECT_NONE_MV) { |
1218 |
pMB->sad16 = best_sad; |
pMB->sad16 = best_sad; |
1219 |
|
pMB->cbp = 0; |
1220 |
|
*complete_count_self = i+1; |
1221 |
|
current_mb++; |
1222 |
continue; |
continue; |
1223 |
} |
} |
1224 |
|
|
1228 |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
1229 |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); |
1230 |
|
|
1231 |
sad2 = best_sad; |
if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) |
1232 |
|
fb_thresh = best_sad; |
1233 |
|
else |
1234 |
|
fb_thresh = best_sad + (best_sad>>1); |
1235 |
|
|
1236 |
if (Data_f.iMinSAD[0] < 2*sad2+2000) |
if (Data_f.iMinSAD[0] <= fb_thresh) |
1237 |
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f); |
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_f); |
1238 |
|
|
1239 |
if (Data_b.iMinSAD[0] < 2*sad2+2000) |
if (Data_b.iMinSAD[0] <= fb_thresh) |
1240 |
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b); |
SearchBF_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_b); |
1241 |
|
|
1242 |
SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad, |
SearchInterpolate_initial(i, j, frame->motion_flags, pParam, &f_predMV, &b_predMV, &best_sad, |
1243 |
&Data_i, Data_f.currentMV[0], Data_b.currentMV[0]); |
&Data_i, Data_f.currentMV[0], Data_b.currentMV[0]); |
1244 |
|
|
1245 |
if (((Data_i.iMinSAD[0] < 2*best_sad+2000) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) |
if (((Data_i.iMinSAD[0] < best_sad +(best_sad>>3)) && !(frame->motion_flags&XVID_ME_FAST_MODEINTERPOLATE)) |
1246 |
|| Data_i.iMinSAD[0] <= best_sad) |
|| Data_i.iMinSAD[0] <= best_sad) |
1247 |
|
|
1248 |
SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i); |
SearchInterpolate_final(i, j, frame->motion_flags, pParam, &best_sad, &Data_i); |
1249 |
|
|
1250 |
if ((Data_d.iMinSAD[0] <= 2*best_sad) && (!frame->motion_flags&XVID_ME_SKIP_DELTASEARCH)) |
if (Data_d.iMinSAD[0] <= 2*best_sad) |
1251 |
|
if ((!(frame->motion_flags&XVID_ME_SKIP_DELTASEARCH) && (best_sad > 750)) |
1252 |
|
|| (best_sad > 1000)) |
1253 |
|
|
1254 |
SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); |
SearchDirect_final(frame->motion_flags, b_mb, &best_sad, &Data_d); |
1255 |
|
|
1256 |
/* final skip decision */ |
/* final skip decision */ |
1263 |
|
|
1264 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */ |
if (pMB->mode == MODE_DIRECT_NONE_MV) { /* skipped? */ |
1265 |
pMB->sad16 = skip_sad; |
pMB->sad16 = skip_sad; |
1266 |
|
pMB->cbp = 0; |
1267 |
|
*complete_count_self = i+1; |
1268 |
|
current_mb++; |
1269 |
continue; |
continue; |
1270 |
} |
} |
1271 |
} |
} |
1272 |
|
|
1273 |
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
1274 |
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
1275 |
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, pParam, i, j); |
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad); |
1276 |
else |
else |
1277 |
ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV); |
ModeDecision_BVOP_SAD(&Data_d, &Data_b, &Data_f, &Data_i, pMB, b_mb, &f_predMV, &b_predMV); |
1278 |
|
|
1279 |
|
*complete_count_self = i+1; |
1280 |
|
current_mb++; |
1281 |
|
maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel); |
1282 |
} |
} |
1283 |
|
|
1284 |
|
complete_count_self++; |
1285 |
|
complete_count_above++; |
1286 |
} |
} |
1287 |
|
|
1288 |
|
h->minfcode = getMinFcode(MVmaxF); |
1289 |
|
h->minbcode = getMinFcode(MVmaxB); |
1290 |
} |
} |