89 |
return xb + yb; |
return xb + yb; |
90 |
} |
} |
91 |
|
|
92 |
|
static int32_t |
93 |
/* CHECK_CANDIATE FUNCTIONS START */ |
ChromaSAD(int dx, int dy, const SearchData * const data) |
|
|
|
|
static void |
|
|
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
|
94 |
{ |
{ |
95 |
int t; |
int sad; |
96 |
const uint8_t * Reference; |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
97 |
|
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
98 |
if (( x > data->max_dx) || ( x < data->min_dx) |
|
99 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
switch (((dx & 1) << 1) + (dy & 1)) { // ((dx%2)?2:0)+((dy%2)?1:0) |
100 |
|
case 0: |
101 |
switch ( ((x&1)<<1) + (y&1) ) { |
sad = sad8(data->CurU, data->RefCU + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
102 |
case 0 : Reference = data->Ref + x/2 + (y/2)*(data->iEdgedWidth); break; |
sad += sad8(data->CurV, data->RefCV + (dy/2) * (data->iEdgedWidth/2) + dx/2, data->iEdgedWidth/2); |
103 |
case 1 : Reference = data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
break; |
104 |
case 2 : Reference = data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth); break; |
case 1: |
105 |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
dx = dx / 2; dy = (dy - 1) / 2; |
106 |
|
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
107 |
|
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + (dy+1) * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2); |
108 |
|
break; |
109 |
|
case 2: |
110 |
|
dx = (dx - 1) / 2; dy = dy / 2; |
111 |
|
sad = sad8bi(data->CurU, data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->RefCU + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
112 |
|
sad += sad8bi(data->CurV, data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->RefCV + dy * (data->iEdgedWidth/2) + dx+1, data->iEdgedWidth/2); |
113 |
|
break; |
114 |
|
default: |
115 |
|
dx = (dx - 1) / 2; dy = (dy - 1) / 2; |
116 |
|
interpolate8x8_halfpel_hv(data->RefQ, |
117 |
|
data->RefCU + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
118 |
|
data->rounding); |
119 |
|
sad = sad8(data->CurU, data->RefQ, data->iEdgedWidth/2); |
120 |
|
interpolate8x8_halfpel_hv(data->RefQ, |
121 |
|
data->RefCV + dy * (data->iEdgedWidth/2) + dx, data->iEdgedWidth/2, |
122 |
|
data->rounding); |
123 |
|
sad += sad8(data->CurV, data->RefQ, data->iEdgedWidth/2); |
124 |
|
break; |
125 |
|
} |
126 |
|
return sad; |
127 |
} |
} |
128 |
|
|
|
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
|
|
|
|
|
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
|
|
data->temp[0] += data->lambda16 * t; |
|
|
data->temp[1] += data->lambda8 * t; |
|
129 |
|
|
130 |
if (data->temp[0] < data->iMinSAD[0]) { |
/* CHECK_CANDIATE FUNCTIONS START */ |
|
data->iMinSAD[0] = data->temp[0]; |
|
|
data->currentMV[0].x = x; data->currentMV[0].y = y; |
|
|
*dir = Direction; } |
|
|
|
|
|
if (data->temp[1] < data->iMinSAD[1]) { |
|
|
data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
|
|
if (data->temp[2] < data->iMinSAD[2]) { |
|
|
data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; } |
|
|
if (data->temp[3] < data->iMinSAD[3]) { |
|
|
data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; } |
|
|
if (data->temp[4] < data->iMinSAD[4]) { |
|
|
data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; } |
|
131 |
|
|
|
} |
|
132 |
|
|
133 |
static void |
static void |
134 |
CheckCandidate16Q(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
135 |
{ |
{ |
|
//this function expects X and Y in halfpel positions, but assumes that qpel positions are our goal |
|
136 |
int t; |
int t; |
137 |
const uint8_t * Reference; |
const uint8_t * Reference; |
138 |
|
|
148 |
|
|
149 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
150 |
|
|
151 |
t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode); |
if (data->qpel) t = d_mv_bits(2*x - data->predQMV.x, 2*y - data->predQMV.y, data->iFcode); |
152 |
data->temp[0] += data->lambda16 * t; |
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
153 |
data->temp[1] += data->lambda8 * t; |
|
154 |
|
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
155 |
|
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
156 |
|
|
157 |
|
if (data->chroma) data->temp[0] += ChromaSAD(x, y, data); |
158 |
|
|
159 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
160 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
189 |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
default : Reference = data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth); break; |
190 |
} |
} |
191 |
|
|
192 |
sad = data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
193 |
sad += sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
194 |
|
|
195 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
196 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
255 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp+1); |
256 |
|
|
257 |
t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
t = d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
258 |
data->temp[0] += data->lambda16 * t; |
data->temp[0] += (data->lambda16 * t * data->temp[0])/1000; |
259 |
data->temp[1] += data->lambda8 * t; |
data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))/100; |
260 |
|
|
261 |
|
if (data->chroma) |
262 |
|
data->temp[0] += ChromaSAD(x/2, y/2, data); |
263 |
|
|
264 |
if (data->temp[0] < data->iMinSAD[0]) { |
if (data->temp[0] < data->iMinSAD[0]) { |
265 |
data->iMinSAD[0] = data->temp[0]; |
data->iMinSAD[0] = data->temp[0]; |
319 |
default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break; |
default : ReferenceB = data->bRefHV + (xb-1)/2 + ((yb-1)/2)*(data->iEdgedWidth); break; |
320 |
} |
} |
321 |
|
|
322 |
sad = data->lambda16 * |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
|
( 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) ); |
|
323 |
|
|
324 |
sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad += (data->lambda16 * |
325 |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
326 |
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
327 |
|
|
328 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
329 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
334 |
static void |
static void |
335 |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
336 |
{ |
{ |
337 |
int32_t sad; |
int32_t sad = 0; |
338 |
int k; |
int k; |
339 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
340 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
342 |
|
|
343 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
344 |
|
|
|
sad = data->lambda16 * d_mv_bits(x, y, 1); |
|
|
|
|
345 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
346 |
mvs.x = data->directmvF[k].x + x; |
mvs.x = data->directmvF[k].x + x; |
347 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
379 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
380 |
} |
} |
381 |
|
|
382 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
383 |
|
|
384 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
385 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
386 |
data->currentMV->x = x; data->currentMV->y = y; |
data->currentMV->x = x; data->currentMV->y = y; |
397 |
|
|
398 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
399 |
|
|
|
sad = data->lambda16 * d_mv_bits(x, y, 1); |
|
|
|
|
400 |
mvs.x = data->directmvF[0].x + x; |
mvs.x = data->directmvF[0].x + x; |
401 |
b_mvs.x = ((x == 0) ? |
b_mvs.x = ((x == 0) ? |
402 |
data->directmvB[0].x |
data->directmvB[0].x |
426 |
default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break; |
default : ReferenceB = data->bRefHV + (b_mvs.x-1)/2 + ((b_mvs.y-1)/2)*(data->iEdgedWidth); break; |
427 |
} |
} |
428 |
|
|
429 |
sad += sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
430 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
431 |
|
|
432 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
433 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
438 |
static void |
static void |
439 |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
440 |
{ |
{ |
441 |
int32_t sad; |
int32_t sad; int t; |
442 |
const uint8_t * Reference; |
const uint8_t * Reference; |
443 |
|
|
444 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
453 |
} |
} |
454 |
|
|
455 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
456 |
sad += data->lambda8 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
if (data->qpel) t = d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode); |
457 |
|
else t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode); |
458 |
|
|
459 |
if (sad < *(data->iMinSAD)) { |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
|
*(data->iMinSAD) = sad; |
|
|
data->currentMV->x = x; data->currentMV->y = y; |
|
|
*dir = Direction; } |
|
|
} |
|
|
|
|
|
static void |
|
|
CheckCandidate8Q(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
|
|
{ |
|
|
int32_t sad; |
|
|
const uint8_t * Reference; |
|
|
|
|
|
if (( x > data->max_dx) || ( x < data->min_dx) |
|
|
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|
|
|
|
|
switch ( ((x&1)<<1) + (y&1) ) |
|
|
{ |
|
|
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; |
|
|
} |
|
|
|
|
|
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
|
|
sad += data->lambda8 * d_mv_bits(2 * x - data->predQMV.x, 2 * y - data->predQMV.y, data->iFcode); |
|
460 |
|
|
461 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
462 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
512 |
} |
} |
513 |
|
|
514 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
515 |
sad += data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode); |
sad += (data->lambda8 * d_mv_bits(x - data->predQMV.x, y - data->predQMV.y, data->iFcode) * (sad+NEIGH_8X8_BIAS))/100; |
516 |
|
|
517 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
518 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
736 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
737 |
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
738 |
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
739 |
|
|
740 |
pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0; |
pMB->qmvs[0].x = pMB->qmvs[1].x = pMB->qmvs[2].x = pMB->qmvs[3].x = 0; |
741 |
pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0; |
pMB->qmvs[0].y = pMB->qmvs[1].y = pMB->qmvs[2].y = pMB->qmvs[3].y = 0; |
742 |
|
|
760 |
|
|
761 |
uint32_t x, y; |
uint32_t x, y; |
762 |
uint32_t iIntra = 0; |
uint32_t iIntra = 0; |
763 |
int32_t InterBias, quant = current->quant; |
int32_t InterBias, quant = current->quant, sad00; |
764 |
uint8_t *qimage; |
uint8_t *qimage; |
765 |
|
|
766 |
// some pre-initialized thingies for SearchP |
// some pre-initialized thingies for SearchP |
776 |
Data.temp = temp; |
Data.temp = temp; |
777 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
778 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
779 |
|
Data.qpel = pParam->m_quarterpel; |
780 |
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
781 |
|
|
782 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
783 |
return 1; // allocate some mem for qpel interpolated blocks |
return 1; // allocate some mem for qpel interpolated blocks |
784 |
// somehow this is dirty since I think we shouldn't use malloc outside |
// somehow this is dirty since I think we shouldn't use malloc outside |
785 |
// encoder_create() - so please fix me! |
// encoder_create() - so please fix me! |
786 |
|
Data.RefQ = qimage; |
787 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
788 |
|
|
789 |
for (y = 0; y < pParam->mb_height; y++) { |
for (y = 0; y < pParam->mb_height; y++) { |
790 |
for (x = 0; x < pParam->mb_width; x++) { |
for (x = 0; x < pParam->mb_width; x++) { |
|
|
|
791 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
792 |
int32_t sad00 = pMB->sad16 |
|
793 |
|
pMB->sad16 |
794 |
= sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
= sad16v(pCurrent->y + (x + y * pParam->edged_width) * 16, |
795 |
pRef->y + (x + y * pParam->edged_width) * 16, |
pRef->y + (x + y * pParam->edged_width) * 16, |
796 |
pParam->edged_width, pMB->sad8 ); |
pParam->edged_width, pMB->sad8 ); |
797 |
|
|
798 |
|
if (Data.chroma) { |
799 |
|
pMB->sad16 += sad8(pCurrent->u + x*8 + y*(pParam->edged_width/2)*8, |
800 |
|
pRef->u + x*8 + y*(pParam->edged_width/2)*8, pParam->edged_width/2); |
801 |
|
|
802 |
|
pMB->sad16 += sad8(pCurrent->v + (x + y*(pParam->edged_width/2))*8, |
803 |
|
pRef->v + (x + y*(pParam->edged_width/2))*8, pParam->edged_width/2); |
804 |
|
} |
805 |
|
|
806 |
|
sad00 = pMB->sad16; //if no gmc; else sad00 = (..) |
807 |
|
|
808 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
809 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
810 |
pMB->quant = current->quant; |
pMB->quant = current->quant; |
817 |
pMB->quant = quant; |
pMB->quant = quant; |
818 |
} |
} |
819 |
|
|
820 |
/* initial skip decision */ |
//initial skip decision |
821 |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
/* no early skip for GMC (global vector = skip vector is unknown!) */ |
|
|
|
822 |
if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
if (current->coding_type == P_VOP) { /* no fast SKIP for S(GMC)-VOPs */ |
823 |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH) |
if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH) |
824 |
if (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
825 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
826 |
continue; |
continue; |
827 |
} |
} |
828 |
} |
} |
829 |
|
|
830 |
SearchP(pRef->y, pRefH->y, pRefV->y, pRefHV->y, qimage, pCurrent, x, |
SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
831 |
y, current->motion_flags, pMB->quant, |
y, current->motion_flags, pMB->quant, |
832 |
&Data, pParam, pMBs, reference->mbs, |
&Data, pParam, pMBs, reference->mbs, |
833 |
current->global_flags & XVID_INTER4V, pMB); |
current->global_flags & XVID_INTER4V, pMB); |
836 |
if (current->coding_type == P_VOP) { |
if (current->coding_type == P_VOP) { |
837 |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) |
838 |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) ) |
&& ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH) ) |
839 |
if (SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, pParam->edged_width, pMB->quant)) { |
840 |
SkipMacroblockP(pMB, sad00); |
SkipMacroblockP(pMB, sad00); |
841 |
continue; |
continue; |
842 |
} |
} |
845 |
/* finally, intra decision */ |
/* finally, intra decision */ |
846 |
|
|
847 |
InterBias = MV16_INTER_BIAS; |
InterBias = MV16_INTER_BIAS; |
848 |
if (pMB->quant > 8) InterBias += 50 * (pMB->quant - 8); // to make high quants work |
if (pMB->quant > 8) InterBias += 80 * (pMB->quant - 8); // to make high quants work |
849 |
if (y != 0) |
if (y != 0) |
850 |
if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 50; |
if ((pMB - pParam->mb_width)->mode == MODE_INTER ) InterBias -= 80; |
851 |
if (x != 0) |
if (x != 0) |
852 |
if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 50; |
if ((pMB - 1)->mode == MODE_INTER ) InterBias -= 80; |
853 |
|
|
854 |
|
if (Data.chroma) InterBias += 50; // to compensate bigger SAD |
855 |
|
|
856 |
if (InterBias < pMB->sad16) { |
if (InterBias < pMB->sad16) { |
857 |
const int32_t deviation = |
const int32_t deviation = |
935 |
} |
} |
936 |
|
|
937 |
static void |
static void |
938 |
SearchP(const uint8_t * const pRef, |
SearchP(const IMAGE * const pRef, |
939 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
940 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
941 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
|
const uint8_t * const pRefQ, |
|
942 |
const IMAGE * const pCur, |
const IMAGE * const pCur, |
943 |
const int x, |
const int x, |
944 |
const int y, |
const int y, |
962 |
Data->predMV = pmv[0]; |
Data->predMV = pmv[0]; |
963 |
|
|
964 |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
965 |
Data->Ref = pRef + (x + Data->iEdgedWidth*y)*16; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
966 |
|
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
967 |
|
|
968 |
|
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
969 |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
970 |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
971 |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
972 |
Data->RefQ = pRefQ; |
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
973 |
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
974 |
|
|
975 |
Data->lambda16 = lambda_vec16[iQuant]; |
Data->lambda16 = lambda_vec16[iQuant]; |
976 |
Data->lambda8 = lambda_vec8[iQuant]; |
Data->lambda8 = lambda_vec8[iQuant]; |
991 |
i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode); |
i = d_mv_bits(Data->predQMV.x, Data->predQMV.y, Data->iFcode); |
992 |
} else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
} else i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode); |
993 |
|
|
994 |
Data->iMinSAD[0] = pMB->sad16 + lambda_vec16[iQuant] * i; |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
995 |
Data->iMinSAD[1] = pMB->sad8[0] + lambda_vec8[iQuant] * i; |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
996 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
997 |
Data->iMinSAD[3] = pMB->sad8[2]; |
Data->iMinSAD[3] = pMB->sad8[2]; |
998 |
Data->iMinSAD[4] = pMB->sad8[3]; |
Data->iMinSAD[4] = pMB->sad8[3]; |
1006 |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height, |
1007 |
prevMBs + x + y * pParam->mb_width); |
prevMBs + x + y * pParam->mb_width); |
1008 |
|
|
1009 |
if (pParam->m_quarterpel) CheckCandidate = CheckCandidate16Q; |
if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16; |
|
else |
|
|
if (inter4v) CheckCandidate = CheckCandidate16; |
|
1010 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1011 |
|
|
1012 |
/* main loop. checking all predictions */ |
/* main loop. checking all predictions */ |
1080 |
QuarterpelRefine(Data); |
QuarterpelRefine(Data); |
1081 |
} |
} |
1082 |
|
|
1083 |
|
if (Data->iMinSAD[0] < (int32_t)iQuant * 30 ) inter4v = 0; |
1084 |
if (inter4v) { |
if (inter4v) { |
1085 |
SearchData Data8; |
SearchData Data8; |
1086 |
Data8.iFcode = Data->iFcode; |
Data8.iFcode = Data->iFcode; |
1087 |
Data8.lambda8 = Data->lambda8; |
Data8.lambda8 = Data->lambda8; |
1088 |
Data8.iEdgedWidth = Data->iEdgedWidth; |
Data8.iEdgedWidth = Data->iEdgedWidth; |
1089 |
Data8.RefQ = Data->RefQ; |
Data8.RefQ = Data->RefQ; |
1090 |
|
Data8.qpel = Data->qpel; |
1091 |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
1092 |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
1093 |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
1094 |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1095 |
|
|
1096 |
|
if (Data->chroma) { |
1097 |
|
int sum, dx, dy; |
1098 |
|
|
1099 |
|
if(pParam->m_quarterpel) { |
1100 |
|
sum = pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2; |
1101 |
|
} else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1102 |
|
dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1103 |
|
|
1104 |
|
if(pParam->m_quarterpel) { |
1105 |
|
sum = pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2; |
1106 |
|
} else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1107 |
|
dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1108 |
|
|
1109 |
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
1110 |
|
} |
1111 |
} |
} |
1112 |
|
|
1113 |
if (!(inter4v) || |
if (!(inter4v) || |
1127 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1128 |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
1129 |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
1130 |
} |
} else { |
|
else { |
|
1131 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1132 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1133 |
} |
} |
1155 |
|
|
1156 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1157 |
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
1158 |
if (block != 0) *(Data->iMinSAD) += Data->lambda8 * |
if (block != 0) *(Data->iMinSAD) += (Data->lambda8 * |
1159 |
d_mv_bits( Data->currentQMV->x - Data->predQMV.x, |
d_mv_bits( Data->currentQMV->x - Data->predQMV.x, |
1160 |
Data->currentQMV->y - Data->predQMV.y, |
Data->currentQMV->y - Data->predQMV.y, |
1161 |
Data->iFcode); |
Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
|
CheckCandidate = CheckCandidate8Q; |
|
1162 |
} else { |
} else { |
1163 |
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); |
1164 |
if (block != 0) *(Data->iMinSAD) += Data->lambda8 * |
if (block != 0) *(Data->iMinSAD) += (Data->lambda8 * |
1165 |
d_mv_bits( Data->currentMV->x - Data->predMV.x, |
d_mv_bits( Data->currentMV->x - Data->predMV.x, |
1166 |
Data->currentMV->y - Data->predMV.y, |
Data->currentMV->y - Data->predMV.y, |
1167 |
Data->iFcode); |
Data->iFcode) * (*Data->iMinSAD + NEIGH_8X8_BIAS))/100; |
|
CheckCandidate = CheckCandidate8; |
|
1168 |
} |
} |
1169 |
|
|
1170 |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8)) { |
1178 |
|
|
1179 |
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, |
1180 |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
1181 |
|
CheckCandidate = CheckCandidate8; |
1182 |
|
|
1183 |
if (MotionFlags & PMV_EXTSEARCH8) { |
if (MotionFlags & PMV_EXTSEARCH8) { |
1184 |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD |
1343 |
|
|
1344 |
// three bits are needed to code backward mode. four for forward |
// three bits are needed to code backward mode. four for forward |
1345 |
// we treat the bits just like they were vector's |
// we treat the bits just like they were vector's |
1346 |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16 * 2; |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
1347 |
else *Data->iMinSAD += 3 * Data->lambda16 * 2; |
else *Data->iMinSAD += 3 * Data->lambda16; |
1348 |
|
|
1349 |
if (*Data->iMinSAD < *best_sad) { |
if (*Data->iMinSAD < *best_sad) { |
1350 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1429 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
(*CheckCandidate)(0, 0, 255, &k, Data); |
1430 |
|
|
1431 |
// skip decision |
// skip decision |
1432 |
if (*Data->iMinSAD - 2 * Data->lambda16 < (uint32_t)pMB->quant * SKIP_THRESH_B) { |
if (*Data->iMinSAD < pMB->quant * SKIP_THRESH_B) { |
1433 |
//possible skip - checking chroma. everything copied from MC |
//possible skip - checking chroma. everything copied from MC |
1434 |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
//this is not full chroma compensation, only it's fullpel approximation. should work though |
1435 |
int sum, dx, dy, b_dx, b_dy; |
int sum, dx, dy, b_dx, b_dy; |
1474 |
|
|
1475 |
HalfpelRefine(Data); |
HalfpelRefine(Data); |
1476 |
|
|
1477 |
*Data->iMinSAD += 1 * Data->lambda16 * 2; // one bit is needed to code direct mode |
*Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode |
1478 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1479 |
|
|
1480 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) |
1588 |
|
|
1589 |
} while (!(iDirection)); |
} while (!(iDirection)); |
1590 |
|
|
1591 |
// two bits are needed to code interpolate mode. we treat the bits just like they were vector's |
*fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode. |
|
*fData->iMinSAD += 2 * fData->lambda16 * 2; |
|
1592 |
|
|
1593 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
1594 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
1603 |
} |
} |
1604 |
} |
} |
1605 |
|
|
|
|
|
1606 |
void |
void |
1607 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
1608 |
FRAMEINFO * const frame, |
FRAMEINFO * const frame, |
1652 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
1653 |
|
|
1654 |
/* 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 */ |
1655 |
if ( (b_mb->mode == MODE_NOT_CODED) ) { |
if (b_mb->mode == MODE_NOT_CODED) { |
1656 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1657 |
continue; |
continue; |
1658 |
} |
} |
1659 |
|
|
1660 |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16; |
1661 |
pMB->quant = frame->quant; |
pMB->quant = frame->quant; |
1662 |
|
|
1663 |
/* direct search comes first, because it (1) checks for SKIP-mode |
/* direct search comes first, because it (1) checks for SKIP-mode |
1664 |
and (2) sets very good predictions for forward and backward search */ |
and (2) sets very good predictions for forward and backward search */ |
|
|
|
1665 |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
skip_sad = SearchDirect(f_ref, f_refH->y, f_refV->y, f_refHV->y, |
1666 |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
b_ref, b_refH->y, b_refV->y, b_refHV->y, |
1667 |
&frame->image, |
&frame->image, |
1675 |
|
|
1676 |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; } |
1677 |
|
|
|
// best_sad = 256*4096; //uncomment to disable Directsearch. |
|
|
// To disable any other mode, just comment the function call |
|
|
|
|
1678 |
// forward search |
// forward search |
1679 |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
1680 |
&frame->image, i, j, |
&frame->image, i, j, |
1727 |
} |
} |
1728 |
} |
} |
1729 |
} |
} |
|
|
|
|
// fprintf(debug,"B-Stat: F: %04d B: %04d I: %04d D: %04d, N: %04d\n", |
|
|
// f_count,b_count,i_count,d_count,n_count); |
|
|
|
|
1730 |
} |
} |
1731 |
|
|
1732 |
/* Hinted ME starts here */ |
/* Hinted ME starts here */ |
1733 |
|
|
1734 |
static void |
static void |
1735 |
Search8hinted(const SearchData * const OldData, |
SearchPhinted ( const IMAGE * const pRef, |
|
const int x, const int y, |
|
|
const uint32_t MotionFlags, |
|
|
const MBParam * const pParam, |
|
|
MACROBLOCK * const pMB, |
|
|
const MACROBLOCK * const pMBs, |
|
|
const int block, |
|
|
SearchData * const Data) |
|
|
{ |
|
|
int32_t temp_sad; |
|
|
MainSearchFunc *MainSearchPtr; |
|
|
Data->iMinSAD = OldData->iMinSAD + 1 + block; |
|
|
Data->currentMV = OldData->currentMV + 1 + block; |
|
|
Data->currentQMV = OldData->currentQMV + 1 + block; |
|
|
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
|
|
|
if(pParam->m_quarterpel) { |
|
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2 , y/2, block); |
|
|
if (block != 0) *(Data->iMinSAD) += Data->lambda8 * |
|
|
d_mv_bits( Data->currentQMV->x - Data->predQMV.x, |
|
|
Data->currentQMV->y - Data->predQMV.y, |
|
|
Data->iFcode); |
|
|
CheckCandidate = CheckCandidate8Q; |
|
|
} else { |
|
|
if (block != 0) *(Data->iMinSAD) += Data->lambda8 * |
|
|
d_mv_bits( Data->currentMV->x - Data->predMV.x, |
|
|
Data->currentMV->y - Data->predMV.y, |
|
|
Data->iFcode); |
|
|
CheckCandidate = CheckCandidate8; |
|
|
} |
|
|
|
|
|
Data->Ref = OldData->Ref + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefH = OldData->RefH + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefV = OldData->RefV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
Data->RefHV = OldData->RefHV + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
|
|
|
Data->Cur = OldData->Cur + 8 * ((block&1) + pParam->edged_width*(block>>1)); |
|
|
|
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
|
|
pParam->width, pParam->height, OldData->iFcode, pParam->m_quarterpel); |
|
|
|
|
|
temp_sad = *(Data->iMinSAD); // store current MinSAD |
|
|
|
|
|
if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch; |
|
|
else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch; |
|
|
else MainSearchPtr = DiamondSearch; |
|
|
|
|
|
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
|
|
|
|
|
if(*(Data->iMinSAD) < temp_sad) { |
|
|
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
|
|
Data->currentQMV->y = 2 * Data->currentMV->y; |
|
|
} |
|
|
|
|
|
if (MotionFlags & PMV_HALFPELREFINE8) { |
|
|
temp_sad = *(Data->iMinSAD); // store current MinSAD |
|
|
|
|
|
HalfpelRefine(Data); // perform halfpel refine of current best vector |
|
|
|
|
|
if(*(Data->iMinSAD) < temp_sad) { // we have found a better match |
|
|
Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector |
|
|
Data->currentQMV->y = 2 * Data->currentMV->y; |
|
|
} |
|
|
} |
|
|
|
|
|
if(pParam->m_quarterpel) { |
|
|
if((!(Data->currentQMV->x & 1)) && (!(Data->currentQMV->y & 1)) && |
|
|
(MotionFlags & PMV_QUARTERPELREFINE8)) { |
|
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8, |
|
|
pParam->width, pParam->height, Data->iFcode, 0); |
|
|
CheckCandidate = CheckCandidate8_qpel; |
|
|
QuarterpelRefine(Data); |
|
|
} |
|
|
pMB->pmvs[block].x = Data->currentQMV->x - Data->predQMV.x; |
|
|
pMB->pmvs[block].y = Data->currentQMV->y - Data->predQMV.y; |
|
|
} else { |
|
|
pMB->pmvs[block].x = Data->currentMV->x - Data->predMV.x; |
|
|
pMB->pmvs[block].y = Data->currentMV->y - Data->predMV.y; |
|
|
} |
|
|
|
|
|
pMB->mvs[block] = *(Data->currentMV); |
|
|
pMB->qmvs[block] = *(Data->currentQMV); |
|
|
|
|
|
pMB->sad8[block] = 4 * (*Data->iMinSAD); |
|
|
} |
|
|
|
|
|
static void |
|
|
SearchPhinted ( const uint8_t * const pRef, |
|
1736 |
const uint8_t * const pRefH, |
const uint8_t * const pRefH, |
1737 |
const uint8_t * const pRefV, |
const uint8_t * const pRefV, |
1738 |
const uint8_t * const pRefHV, |
const uint8_t * const pRefHV, |
1748 |
SearchData * const Data) |
SearchData * const Data) |
1749 |
{ |
{ |
1750 |
|
|
|
const int32_t iEdgedWidth = pParam->edged_width; |
|
|
|
|
1751 |
int i, t; |
int i, t; |
1752 |
MainSearchFunc * MainSearchPtr; |
MainSearchFunc * MainSearchPtr; |
1753 |
|
|
1754 |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1755 |
|
Data->predQMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1756 |
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, |
1757 |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, Data->iFcode, pParam->m_quarterpel); |
1758 |
|
|
1759 |
Data->Cur = pCur->y + (x + y * iEdgedWidth) * 16; |
Data->Cur = pCur->y + (x + y * Data->iEdgedWidth) * 16; |
1760 |
Data->Ref = pRef + (x + iEdgedWidth*y)*16; |
Data->CurV = pCur->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1761 |
Data->RefH = pRefH + (x + iEdgedWidth*y) * 16; |
Data->CurU = pCur->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1762 |
Data->RefV = pRefV + (x + iEdgedWidth*y) * 16; |
|
1763 |
Data->RefHV = pRefHV + (x + iEdgedWidth*y) * 16; |
Data->Ref = pRef->y + (x + Data->iEdgedWidth*y) * 16; |
1764 |
Data->lambda16 = lambda_vec16[iQuant]; |
Data->RefH = pRefH + (x + Data->iEdgedWidth*y) * 16; |
1765 |
Data->lambda8 = lambda_vec8[iQuant]; |
Data->RefV = pRefV + (x + Data->iEdgedWidth*y) * 16; |
1766 |
|
Data->RefHV = pRefHV + (x + Data->iEdgedWidth*y) * 16; |
1767 |
|
Data->RefCV = pRef->v + (x + y * (Data->iEdgedWidth/2)) * 8; |
1768 |
|
Data->RefCU = pRef->u + (x + y * (Data->iEdgedWidth/2)) * 8; |
1769 |
|
|
1770 |
if (!(MotionFlags & PMV_HALFPEL16)) { |
if (!(MotionFlags & PMV_HALFPEL16)) { |
1771 |
Data->min_dx = EVEN(Data->min_dx); |
Data->min_dx = EVEN(Data->min_dx); |
1778 |
|
|
1779 |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
if (pMB->dquant != NO_CHANGE) inter4v = 0; |
1780 |
|
|
1781 |
if(pParam->m_quarterpel) CheckCandidate = CheckCandidate16Q; |
if (inter4v || pParam->m_quarterpel || Data->chroma) CheckCandidate = CheckCandidate16; |
|
else |
|
|
if (inter4v) CheckCandidate = CheckCandidate16; |
|
1782 |
else CheckCandidate = CheckCandidate16no4v; |
else CheckCandidate = CheckCandidate16no4v; |
1783 |
|
|
1784 |
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
pMB->mvs[0].x = EVEN(pMB->mvs[0].x); |
1826 |
Data8.lambda8 = Data->lambda8; |
Data8.lambda8 = Data->lambda8; |
1827 |
Data8.iEdgedWidth = Data->iEdgedWidth; |
Data8.iEdgedWidth = Data->iEdgedWidth; |
1828 |
Data8.RefQ = Data->RefQ; |
Data8.RefQ = Data->RefQ; |
1829 |
Search8hinted(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
Data8.qpel = Data->qpel; |
1830 |
Search8hinted(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
Search8(Data, 2*x, 2*y, MotionFlags, pParam, pMB, pMBs, 0, &Data8); |
1831 |
Search8hinted(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
Search8(Data, 2*x + 1, 2*y, MotionFlags, pParam, pMB, pMBs, 1, &Data8); |
1832 |
Search8hinted(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8); |
1833 |
|
Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8); |
1834 |
|
|
1835 |
|
if (Data->chroma) { |
1836 |
|
int sum, dx, dy; |
1837 |
|
|
1838 |
|
if(pParam->m_quarterpel) |
1839 |
|
sum = (pMB->qmvs[0].y + pMB->qmvs[1].y + pMB->qmvs[2].y + pMB->qmvs[3].y)/2; |
1840 |
|
else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1841 |
|
dy = (sum ? SIGN(sum) * |
1842 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
1843 |
|
|
1844 |
|
if(pParam->m_quarterpel) |
1845 |
|
sum = (pMB->qmvs[0].x + pMB->qmvs[1].x + pMB->qmvs[2].x + pMB->qmvs[3].x)/2; |
1846 |
|
else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1847 |
|
dx = (sum ? SIGN(sum) * |
1848 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
1849 |
|
Data->iMinSAD[1] += ChromaSAD(dx, dy, Data); |
1850 |
|
} |
1851 |
} |
} |
1852 |
|
|
1853 |
if (!(inter4v) || |
if (!(inter4v) || |
1867 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1868 |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predQMV.x; |
1869 |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predQMV.y; |
1870 |
} |
} else { |
|
else { |
|
1871 |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; |
1872 |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; |
1873 |
} |
} |
1874 |
} else { |
} else { |
1875 |
// INTER4V MODE; all other things are already set in Search8hinted |
// INTER4V MODE; all other things are already set in Search8 |
1876 |
pMB->mode = MODE_INTER4V; |
pMB->mode = MODE_INTER4V; |
1877 |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] + Data->iMinSAD[3] |
1878 |
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
+ Data->iMinSAD[4] + IMV16X16 * iQuant; |
1905 |
Data.temp = temp; |
Data.temp = temp; |
1906 |
Data.iFcode = current->fcode; |
Data.iFcode = current->fcode; |
1907 |
Data.rounding = pParam->m_rounding_type; |
Data.rounding = pParam->m_rounding_type; |
1908 |
|
Data.qpel = pParam->m_quarterpel; |
1909 |
|
Data.chroma = current->global_flags & XVID_ME_COLOUR; |
1910 |
|
|
1911 |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
1912 |
return; // allocate some mem for qpel interpolated blocks |
return; // allocate some mem for qpel interpolated blocks |
1925 |
//intra mode is copied from the first pass. At least for the time being |
//intra mode is copied from the first pass. At least for the time being |
1926 |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
if ((pMB->mode == MODE_INTRA) || (pMB->mode == MODE_NOT_CODED) ) continue; |
1927 |
|
|
|
|
|
1928 |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
if (!(current->global_flags & XVID_LUMIMASKING)) { |
1929 |
pMB->dquant = NO_CHANGE; |
pMB->dquant = NO_CHANGE; |
1930 |
pMB->quant = current->quant; } |
pMB->quant = current->quant; } |
1937 |
pMB->quant = quant; |
pMB->quant = quant; |
1938 |
} |
} |
1939 |
|
|
1940 |
SearchPhinted(pRef->y, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
SearchPhinted(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x, |
1941 |
y, current->motion_flags, pMB->quant, |
y, current->motion_flags, pMB->quant, |
1942 |
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
pParam, pMBs, current->global_flags & XVID_INTER4V, pMB, |
1943 |
&Data); |
&Data); |
1986 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
1987 |
|
|
1988 |
pMB->mvs[0] = *Data->currentMV; |
pMB->mvs[0] = *Data->currentMV; |
1989 |
|
pMB->mode = MODE_INTER; |
1990 |
|
|
1991 |
return *(Data->iMinSAD); |
return *(Data->iMinSAD); |
1992 |
} |
} |
2025 |
if (sad > INTRA_THRESH) { |
if (sad > INTRA_THRESH) { |
2026 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
2027 |
pParam->edged_width); |
pParam->edged_width); |
2028 |
if (dev + INTRA_THRESH < sad) intra++; |
if (dev + INTRA_THRESH < sad) { intra++; pMB->mode = MODE_INTRA; } |
2029 |
if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
2030 |
} |
} |
2031 |
sSAD += sad; |
sSAD += sad; |
2068 |
return i; |
return i; |
2069 |
} |
} |
2070 |
|
|
2071 |
|
static void |
2072 |
|
CheckGMC(int x, int y, const int dir, int * iDirection, |
2073 |
|
const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC, |
2074 |
|
const MBParam * const pParam) |
2075 |
|
{ |
2076 |
|
uint32_t mx, my, a, count = 0; |
2077 |
|
|
2078 |
|
for (my = 1; my < pParam->mb_height-1; my++) |
2079 |
|
for (mx = 1; mx < pParam->mb_width-1; mx++) { |
2080 |
|
VECTOR mv; |
2081 |
|
const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width]; |
2082 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue; |
2083 |
|
mv = pMB->mvs[0]; |
2084 |
|
a = ABS(mv.x - x) + ABS(mv.y - y); |
2085 |
|
if (a < 6) count += 6 - a; |
2086 |
|
} |
2087 |
|
|
2088 |
|
if (count > *bestcount) { |
2089 |
|
*bestcount = count; |
2090 |
|
*iDirection = dir; |
2091 |
|
GMC->x = x; GMC->y = y; |
2092 |
|
} |
2093 |
|
} |
2094 |
|
|
|
/* Global Motion Estimation, (C) 2002 by sysKin */ |
|
|
/* calculate global translation from local (halfpel) motion field */ |
|
2095 |
|
|
2096 |
static VECTOR |
static VECTOR |
2097 |
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) |
2098 |
{ |
{ |
2099 |
|
|
2100 |
int count, bestcount = 0; |
uint32_t count, bestcount = 0; |
2101 |
int x, y; |
int x, y; |
2102 |
VECTOR gmc; |
VECTOR gmc = {0,0}; |
2103 |
int step, min_x, max_x, min_y, max_y; |
int step, min_x, max_x, min_y, max_y; |
2104 |
uint32_t mx, my; |
uint32_t mx, my; |
2105 |
|
int iDirection, bDirection; |
2106 |
|
|
2107 |
min_x = min_y = -32<<iFcode; |
min_x = min_y = -32<<iFcode; |
2108 |
max_x = max_y = 32<<iFcode; |
max_x = max_y = 32<<iFcode; |
2109 |
|
|
2110 |
|
//step1: let's find a rough camera panning |
2111 |
for (step = 32; step >= 2; step /= 2) { |
for (step = 32; step >= 2; step /= 2) { |
2112 |
bestcount = 0; |
bestcount = 0; |
2113 |
for (y = min_y; y <= max_y; y += step) |
for (y = min_y; y <= max_y; y += step) |
2134 |
max_y = gmc.y + step; |
max_y = gmc.y + step; |
2135 |
|
|
2136 |
} |
} |
2137 |
|
|
2138 |
|
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
2139 |
|
gmc.x = gmc.y = 0; //no camara pan, no GMC |
2140 |
|
|
2141 |
|
// step2: let's refine camera panning using gradiend-descent approach. |
2142 |
|
// TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond) |
2143 |
|
bestcount = 0; |
2144 |
|
CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2145 |
|
do { |
2146 |
|
x = gmc.x; y = gmc.y; |
2147 |
|
bDirection = iDirection; iDirection = 0; |
2148 |
|
if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2149 |
|
if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2150 |
|
if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2151 |
|
if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam); |
2152 |
|
|
2153 |
|
} while (iDirection); |
2154 |
|
|
2155 |
if (pParam->m_quarterpel) { |
if (pParam->m_quarterpel) { |
2156 |
gmc.x *= 2; |
gmc.x *= 2; |
2157 |
gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */ |
gmc.y *= 2; /* we store the halfpel value as pseudo-qpel to make comparison easier */ |
2158 |
} |
} |
2159 |
|
|
|
if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10) |
|
|
gmc.x = gmc.y = 0; //no camara pan, no GMC |
|
|
|
|
2160 |
return gmc; |
return gmc; |
2161 |
} |
} |