21 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: estimation_bvop.c,v 1.26 2010-11-28 15:18:21 Isibaar Exp $ |
* $Id: estimation_bvop.c,v 1.28.2.1 2010-12-29 22:29:44 Isibaar Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
396 |
const uint32_t iWcount, |
const uint32_t iWcount, |
397 |
const MACROBLOCK * const pMB, |
const MACROBLOCK * const pMB, |
398 |
const uint32_t mode_curr, |
const uint32_t mode_curr, |
399 |
const VECTOR hint) |
const VECTOR hint, const int bound) |
400 |
{ |
{ |
401 |
|
int lx, ly; /* left */ |
402 |
|
int tx, ty; /* top */ |
403 |
|
int rtx, rty; /* top-right */ |
404 |
|
int ltx, lty; /* top-left */ |
405 |
|
int lpos, tpos, rtpos, ltpos; |
406 |
|
|
407 |
|
lx = x - 1; ly = y; |
408 |
|
tx = x; ty = y - 1; |
409 |
|
rtx = x + 1; rty = y - 1; |
410 |
|
ltx = x - 1; lty = y - 1; |
411 |
|
|
412 |
|
lpos = lx + ly * iWcount; |
413 |
|
rtpos = rtx + rty * iWcount; |
414 |
|
tpos = tx + ty * iWcount; |
415 |
|
ltpos = ltx + lty * iWcount; |
416 |
|
|
417 |
|
|
418 |
/* [0] is prediction */ |
/* [0] is prediction */ |
419 |
/* [1] is zero */ |
/* [1] is zero */ |
420 |
pmv[1].x = pmv[1].y = 0; |
pmv[1].x = pmv[1].y = 0; |
421 |
|
|
422 |
pmv[2].x = hint.x; pmv[2].y = hint.y; |
pmv[2].x = hint.x; pmv[2].y = hint.y; |
423 |
|
|
424 |
if ((y != 0)&&(x != (int)(iWcount+1))) { /* [3] top-right neighbour */ |
if (rtpos >= bound && rtx < (int)iWcount) { /* [3] top-right neighbour */ |
425 |
pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); |
pmv[3] = ChoosePred(pMB+1-iWcount, mode_curr); |
426 |
} else pmv[3].x = pmv[3].y = 0; |
} else pmv[3].x = pmv[3].y = 0; |
427 |
|
|
428 |
if (y != 0) { |
if (tpos >= bound) { |
429 |
pmv[4] = ChoosePred(pMB-iWcount, mode_curr); |
pmv[4] = ChoosePred(pMB-iWcount, mode_curr); /* [4] top */ |
430 |
} else pmv[4].x = pmv[4].y = 0; |
} else pmv[4].x = pmv[4].y = 0; |
431 |
|
|
432 |
if (x != 0) { |
if (lpos >= bound && lx >= 0) { |
433 |
pmv[5] = ChoosePred(pMB-1, mode_curr); |
pmv[5] = ChoosePred(pMB-1, mode_curr); /* [5] left */ |
434 |
} else pmv[5].x = pmv[5].y = 0; |
} else pmv[5].x = pmv[5].y = 0; |
435 |
|
|
436 |
if (x != 0 && y != 0) { |
if (ltpos >= bound && ltx >= 0) { |
437 |
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); |
pmv[6] = ChoosePred(pMB-1-iWcount, mode_curr); /* [6] top-left */ |
438 |
} else pmv[6].x = pmv[6].y = 0; |
} else pmv[6].x = pmv[6].y = 0; |
439 |
} |
} |
440 |
|
|
449 |
int32_t * const best_sad, |
int32_t * const best_sad, |
450 |
const int32_t mode_current, |
const int32_t mode_current, |
451 |
SearchData * const Data, |
SearchData * const Data, |
452 |
VECTOR hint) |
VECTOR hint, const int bound) |
453 |
{ |
{ |
454 |
|
|
455 |
int i; |
int i; |
468 |
hint.x /= 2; hint.y /= 2; |
hint.x /= 2; hint.y /= 2; |
469 |
} |
} |
470 |
|
|
471 |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current, hint); |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current, hint, bound); |
472 |
|
|
473 |
Data->currentMV->x = Data->currentMV->y = 0; |
Data->currentMV->x = Data->currentMV->y = 0; |
474 |
|
|
817 |
MACROBLOCK * const pMB, |
MACROBLOCK * const pMB, |
818 |
const MACROBLOCK * const b_mb, |
const MACROBLOCK * const b_mb, |
819 |
VECTOR * f_predMV, |
VECTOR * f_predMV, |
820 |
VECTOR * b_predMV) |
VECTOR * b_predMV, |
821 |
|
int force_direct) |
822 |
{ |
{ |
823 |
int mode = MODE_DIRECT, k; |
int mode = MODE_DIRECT, k; |
824 |
int best_sad, f_sad, b_sad, i_sad; |
int best_sad, f_sad, b_sad, i_sad; |
830 |
f_sad = Data_f->iMinSAD[0] + 4*Data_d->lambda16; |
f_sad = Data_f->iMinSAD[0] + 4*Data_d->lambda16; |
831 |
i_sad = Data_i->iMinSAD[0] + 2*Data_d->lambda16; |
i_sad = Data_i->iMinSAD[0] + 2*Data_d->lambda16; |
832 |
|
|
833 |
|
if (force_direct) |
834 |
|
goto set_mode; /* bypass checks for non-direct modes */ |
835 |
|
|
836 |
if (b_sad < best_sad) { |
if (b_sad < best_sad) { |
837 |
mode = MODE_BACKWARD; |
mode = MODE_BACKWARD; |
838 |
best_sad = b_sad; |
best_sad = b_sad; |
848 |
best_sad = i_sad; |
best_sad = i_sad; |
849 |
} |
} |
850 |
|
|
851 |
|
set_mode: |
852 |
pMB->sad16 = best_sad; |
pMB->sad16 = best_sad; |
853 |
pMB->mode = mode; |
pMB->mode = mode; |
854 |
pMB->cbp = 63; |
pMB->cbp = 63; |
983 |
const IMAGE * const b_ref, |
const IMAGE * const b_ref, |
984 |
const IMAGE * const b_refH, |
const IMAGE * const b_refH, |
985 |
const IMAGE * const b_refV, |
const IMAGE * const b_refV, |
986 |
const IMAGE * const b_refHV) |
const IMAGE * const b_refHV, |
987 |
|
const int num_slices) |
988 |
{ |
{ |
989 |
uint32_t i, j; |
uint32_t i, j; |
990 |
int32_t best_sad = 256*4096; |
int32_t best_sad = 256*4096; |
994 |
|
|
995 |
VECTOR f_predMV, b_predMV; |
VECTOR f_predMV, b_predMV; |
996 |
|
|
997 |
|
int mb_width = pParam->mb_width; |
998 |
|
int mb_height = pParam->mb_height; |
999 |
int MVmaxF = 0, MVmaxB = 0; |
int MVmaxF = 0, MVmaxB = 0; |
1000 |
const int32_t TRB = time_pp - time_bp; |
const int32_t TRB = time_pp - time_bp; |
1001 |
const int32_t TRD = time_pp; |
const int32_t TRD = time_pp; |
1025 |
Data_b.iFcode = Data_i.bFcode = frame->bcode = b_reference->fcode; |
Data_b.iFcode = Data_i.bFcode = frame->bcode = b_reference->fcode; |
1026 |
|
|
1027 |
for (j = 0; j < pParam->mb_height; j++) { |
for (j = 0; j < pParam->mb_height; j++) { |
1028 |
|
int new_bound = mb_width * ((((j*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices); |
1029 |
|
|
1030 |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
1031 |
|
|
1032 |
for (i = 0; i < pParam->mb_width; i++) { |
for (i = 0; i < pParam->mb_width; i++) { |
1033 |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
1034 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1035 |
|
int force_direct = (((j*mb_width+i)==new_bound) && (j > 0)) ? 1 : 0; /* MTK decoder chipsets do NOT reset predMVs upon resync marker in BVOPs. We workaround this problem |
1036 |
|
by placing the slice border on second MB in a row and then force the first MB to be direct mode */ |
1037 |
|
|
1038 |
pMB->mode = -1; |
pMB->mode = -1; |
1039 |
|
|
1040 |
initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, |
initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, |
1063 |
} |
} |
1064 |
|
|
1065 |
SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, |
SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, |
1066 |
&f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]); |
&f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1], new_bound); |
1067 |
|
|
1068 |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
1069 |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2], new_bound); |
1070 |
|
|
1071 |
if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) |
if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) |
1072 |
fb_thresh = best_sad; |
fb_thresh = best_sad; |
1110 |
|
|
1111 |
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
1112 |
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
1113 |
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad); |
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad, force_direct); |
1114 |
else |
else |
1115 |
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, force_direct); |
1116 |
|
|
1117 |
maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel); |
maxMotionBVOP(&MVmaxF, &MVmaxB, pMB, Data_d.qpel); |
1118 |
|
|
1126 |
|
|
1127 |
|
|
1128 |
void |
void |
1129 |
SMPMotionEstimationBVOP(SMPmotionData * h) |
SMPMotionEstimationBVOP(SMPData * h) |
1130 |
{ |
{ |
1131 |
const MBParam * const pParam = h->pParam; |
Encoder *pEnc = (Encoder *) h->pEnc; |
1132 |
|
|
1133 |
|
const MBParam * const pParam = &pEnc->mbParam; |
1134 |
const FRAMEINFO * const frame = h->current; |
const FRAMEINFO * const frame = h->current; |
1135 |
const int32_t time_bp = h->time_bp; |
const int32_t time_bp = (int32_t)(pEnc->current->stamp - frame->stamp); |
1136 |
const int32_t time_pp = h->time_pp; |
const int32_t time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp); |
1137 |
/* forward (past) reference */ |
/* forward (past) reference */ |
1138 |
const MACROBLOCK * const f_mbs = h->f_mbs; |
const IMAGE * const f_ref = &pEnc->reference->image; |
1139 |
const IMAGE * const f_ref = h->fRef; |
const IMAGE * const f_refH = &pEnc->f_refh; |
1140 |
const IMAGE * const f_refH = h->fRefH; |
const IMAGE * const f_refV = &pEnc->f_refv; |
1141 |
const IMAGE * const f_refV = h->fRefV; |
const IMAGE * const f_refHV = &pEnc->f_refhv; |
|
const IMAGE * const f_refHV = h->fRefHV; |
|
1142 |
/* backward (future) reference */ |
/* backward (future) reference */ |
1143 |
const FRAMEINFO * const b_reference = h->reference; |
const FRAMEINFO * const b_reference = pEnc->current; |
1144 |
const IMAGE * const b_ref = h->pRef; |
const IMAGE * const b_ref = &pEnc->current->image; |
1145 |
const IMAGE * const b_refH = h->pRefH; |
const IMAGE * const b_refH = &pEnc->vInterH; |
1146 |
const IMAGE * const b_refV = h->pRefV; |
const IMAGE * const b_refV = &pEnc->vInterV; |
1147 |
const IMAGE * const b_refHV = h->pRefHV; |
const IMAGE * const b_refHV = &pEnc->vInterHV; |
1148 |
|
|
1149 |
|
int mb_width = pParam->mb_width; |
1150 |
|
int mb_height = pParam->mb_height; |
1151 |
|
int num_slices = pEnc->num_slices; |
1152 |
|
int y_row = h->y_row; |
1153 |
int y_step = h->y_step; |
int y_step = h->y_step; |
1154 |
int start_y = h->start_y; |
int start_y = h->start_y; |
1155 |
|
int stop_y = h->stop_y; |
1156 |
int * complete_count_self = h->complete_count_self; |
int * complete_count_self = h->complete_count_self; |
1157 |
const int * complete_count_above = h->complete_count_above; |
const int * complete_count_above = h->complete_count_above; |
1158 |
int max_mbs; |
int max_mbs; |
1196 |
|
|
1197 |
max_mbs = 0; |
max_mbs = 0; |
1198 |
|
|
1199 |
for (j = start_y; j < pParam->mb_height; j += y_step) { |
for (j = (start_y+y_row); j < stop_y; j += y_step) { |
1200 |
if (j == 0) max_mbs = pParam->mb_width; /* we can process all blocks of the first row */ |
int new_bound = mb_width * ((((j*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices); |
1201 |
|
|
1202 |
|
if (j == start_y) max_mbs = pParam->mb_width; /* we can process all blocks of the first row */ |
1203 |
|
|
1204 |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
1205 |
|
|
1206 |
for (i = 0; i < pParam->mb_width; i++) { |
for (i = 0; i < (int) pParam->mb_width; i++) { |
1207 |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width; |
1208 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1209 |
|
int force_direct = (((j*mb_width+i)==new_bound) && (j > 0)) ? 1 : 0; /* MTK decoder chipsets do NOT reset predMVs upon resync marker in BVOPs. We workaround this problem |
1210 |
|
by placing the slice border on second MB in a row and then force the first MB to be direct mode */ |
1211 |
pMB->mode = -1; |
pMB->mode = -1; |
1212 |
|
|
1213 |
initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, |
initialize_searchData(&Data_d, &Data_f, &Data_b, &Data_i, |
1221 |
if (above_count == pParam->mb_width) { |
if (above_count == pParam->mb_width) { |
1222 |
/* full line above is ready */ |
/* full line above is ready */ |
1223 |
above_count = pParam->mb_width+1; |
above_count = pParam->mb_width+1; |
1224 |
if (j < pParam->mb_height-y_step) { |
if (j < stop_y-y_step) { |
1225 |
/* this is not last line, grab a portion of MBs from the next line too */ |
/* this is not last line, grab a portion of MBs from the next line too */ |
1226 |
above_count += MAX(0, complete_count_above[1] - 1); |
above_count += MAX(0, complete_count_above[1] - 1); |
1227 |
} |
} |
1262 |
} |
} |
1263 |
|
|
1264 |
SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, |
SearchBF_initial(i, j, frame->motion_flags, frame->fcode, pParam, pMB, |
1265 |
&f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1]); |
&f_predMV, &best_sad, MODE_FORWARD, &Data_f, Data_d.currentMV[1], new_bound); |
1266 |
|
|
1267 |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
SearchBF_initial(i, j, frame->motion_flags, frame->bcode, pParam, pMB, |
1268 |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2]); |
&b_predMV, &best_sad, MODE_BACKWARD, &Data_b, Data_d.currentMV[2], new_bound); |
1269 |
|
|
1270 |
if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) |
if (frame->motion_flags&XVID_ME_BFRAME_EARLYSTOP) |
1271 |
fb_thresh = best_sad; |
fb_thresh = best_sad; |
1311 |
|
|
1312 |
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
if (frame->vop_flags & XVID_VOP_RD_BVOP) |
1313 |
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
ModeDecision_BVOP_RD(&Data_d, &Data_b, &Data_f, &Data_i, |
1314 |
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad); |
pMB, b_mb, &f_predMV, &b_predMV, frame->motion_flags, frame->vop_flags, pParam, i, j, best_sad, force_direct); |
1315 |
else |
else |
1316 |
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, force_direct); |
1317 |
|
|
1318 |
*complete_count_self = i+1; |
*complete_count_self = i+1; |
1319 |
current_mb++; |
current_mb++; |