53 |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
(*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); } |
54 |
|
|
55 |
static __inline int |
static __inline int |
56 |
d_mv_bits(int x, int y, const uint32_t iFcode, const int qpel, const int rrv) |
d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv) |
57 |
{ |
{ |
58 |
int xb, yb; |
int xb, yb; |
59 |
if (qpel) { x *= 2; y *= 2;} |
if (qpel) { x *= 2; y *= 2;} |
60 |
else if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
else if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); } |
61 |
|
x = pred.x - x; |
62 |
|
y = pred.y - y; |
63 |
|
|
64 |
if (x == 0) xb = 1; |
if (x == 0) xb = 1; |
65 |
else { |
else { |
148 |
const int halfpel_y = y/2; |
const int halfpel_y = y/2; |
149 |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
150 |
|
|
151 |
ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases |
ref1 = GetReference(halfpel_x, halfpel_y, dir, data); |
152 |
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
153 |
switch( ((x&1)<<1) + (y&1) ) { |
switch( ((x&1)<<1) + (y&1) ) { |
154 |
case 0: // pure halfpel position |
case 0: // pure halfpel position |
155 |
Reference = (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data); |
return (uint8_t *) ref1; |
|
Reference += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth; |
|
156 |
break; |
break; |
157 |
|
|
158 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
192 |
const int halfpel_y = y/2; |
const int halfpel_y = y/2; |
193 |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
const uint8_t *ref1, *ref2, *ref3, *ref4; |
194 |
|
|
195 |
ref1 = GetReference(halfpel_x, halfpel_y, dir, data); // this reference is used in all cases |
ref1 = GetReference(halfpel_x, halfpel_y, dir, data); |
196 |
switch( ((x&1)<<1) + (y&1) ) { |
switch( ((x&1)<<1) + (y&1) ) { |
197 |
case 0: // pure halfpel position |
case 0: // pure halfpel position |
198 |
return (uint8_t *) GetReference(halfpel_x, halfpel_y, dir, data); |
return (uint8_t *) ref1; |
199 |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
200 |
ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data); |
ref2 = GetReference(halfpel_x, y - halfpel_y, dir, data); |
201 |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8); |
247 |
current = data->currentMV; |
current = data->currentMV; |
248 |
xc = x; yc = y; |
xc = x; yc = y; |
249 |
} |
} |
250 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
251 |
|
|
252 |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
253 |
|
|
283 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
284 |
|
|
285 |
Reference = GetReference(x, y, 0, data); |
Reference = GetReference(x, y, 0, data); |
286 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, 0, 1); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1); |
287 |
|
|
288 |
data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
data->temp[0] = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp + 1); |
289 |
|
|
326 |
Reference = GetReference(x, y, 0, data); |
Reference = GetReference(x, y, 0, data); |
327 |
current = data->currentMV; |
current = data->currentMV; |
328 |
} |
} |
329 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, |
t = d_mv_bits(x, y, data->predMV, data->iFcode, |
330 |
data->qpel && !data->qpel_precision && !data->rrv, data->rrv); |
data->qpel && !data->qpel_precision && !data->rrv, data->rrv); |
331 |
|
|
332 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
339 |
} |
} |
340 |
|
|
341 |
static void |
static void |
342 |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
343 |
{ |
{ |
344 |
// maximum speed - for P/B/I decision |
// maximum speed - for P/B/I decision |
|
int32_t sad; |
|
345 |
|
|
346 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
347 |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
348 |
|
|
349 |
sad = sad16(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
data->temp[0] = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth), |
350 |
data->iEdgedWidth, 256*4096); |
data->iEdgedWidth, data->temp+1); |
351 |
|
if (data->temp[0] < *(data->iMinSAD)) { |
352 |
if (sad < *(data->iMinSAD)) { |
*(data->iMinSAD) = data->temp[0]; |
|
*(data->iMinSAD) = sad; |
|
353 |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
data->currentMV[0].x = x; data->currentMV[0].y = y; |
354 |
*dir = Direction; } |
*dir = Direction; } |
355 |
|
if (data->temp[1] < data->iMinSAD[1]) { |
356 |
|
data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
357 |
|
if (data->temp[2] < data->iMinSAD[2]) { |
358 |
|
data->iMinSAD[2] = data->temp[2]; data->currentMV[2].x = x; data->currentMV[2].y = y; } |
359 |
|
if (data->temp[3] < data->iMinSAD[3]) { |
360 |
|
data->iMinSAD[3] = data->temp[3]; data->currentMV[3].x = x; data->currentMV[3].y = y; } |
361 |
|
if (data->temp[4] < data->iMinSAD[4]) { |
362 |
|
data->iMinSAD[4] = data->temp[4]; data->currentMV[4].x = x; data->currentMV[4].y = y; } |
363 |
|
|
364 |
} |
} |
365 |
|
|
366 |
|
|
387 |
current = data->currentMV; |
current = data->currentMV; |
388 |
} |
} |
389 |
|
|
390 |
t = d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0) |
t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0) |
391 |
+ d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
392 |
|
|
393 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
394 |
sad += (data->lambda16 * t * sad)/1000; |
sad += (data->lambda16 * t * sad)/1000; |
407 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
408 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
409 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
410 |
|
const VECTOR zeroMV={0,0}; |
411 |
|
|
412 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
413 |
|
|
440 |
if (sad > *(data->iMinSAD)) return; |
if (sad > *(data->iMinSAD)) return; |
441 |
} |
} |
442 |
|
|
443 |
sad += (data->lambda16 * d_mv_bits(x, y, 1, 0, 0) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)/1000; |
444 |
|
|
445 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
446 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
455 |
const uint8_t *ReferenceF; |
const uint8_t *ReferenceF; |
456 |
const uint8_t *ReferenceB; |
const uint8_t *ReferenceB; |
457 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
458 |
|
const VECTOR zeroMV = {0,0}; |
459 |
|
|
460 |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
461 |
|
|
482 |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data); |
483 |
|
|
484 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
485 |
sad += (data->lambda16 * d_mv_bits(x, y, 1, 0, 0) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)/1000; |
486 |
|
|
487 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
488 |
*(data->iMinSAD) = sad; |
*(data->iMinSAD) = sad; |
503 |
else Reference = GetReference(x, y, 0, data); |
else Reference = GetReference(x, y, 0, data); |
504 |
|
|
505 |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
sad = sad8(data->Cur, Reference, data->iEdgedWidth); |
506 |
t = d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode, data->qpel && !data->qpel_precision, 0); |
t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0); |
507 |
|
|
508 |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))/100; |
509 |
|
|
1007 |
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
if (pParam->m_quarterpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0); |
1008 |
else Data->predMV = pmv[0]; |
else Data->predMV = pmv[0]; |
1009 |
|
|
1010 |
i = d_mv_bits(Data->predMV.x, Data->predMV.y, Data->iFcode, 0, 0); |
i = d_mv_bits(0, 0, Data->predMV, Data->iFcode, 0, 0); |
1011 |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
Data->iMinSAD[0] = pMB->sad16 + (Data->lambda16 * i * pMB->sad16)/1000; |
1012 |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
Data->iMinSAD[1] = pMB->sad8[0] + (Data->lambda8 * i * (pMB->sad8[0]+NEIGH_8X8_BIAS))/100; |
1013 |
Data->iMinSAD[2] = pMB->sad8[1]; |
Data->iMinSAD[2] = pMB->sad8[1]; |
1137 |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x); |
1138 |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y); |
1139 |
} |
} |
1140 |
|
|
1141 |
if (!(inter4v) || |
if (!(inter4v) || |
1142 |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
(Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + |
1143 |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) { |
1183 |
|
|
1184 |
if(pParam->m_quarterpel) { |
if(pParam->m_quarterpel) { |
1185 |
Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x/2, y/2, block); |
1186 |
if (block != 0) i = d_mv_bits( Data->currentQMV->x - Data->predMV.x, |
if (block != 0) i = d_mv_bits( Data->currentQMV->x, Data->currentQMV->y, |
1187 |
Data->currentQMV->y - Data->predMV.y, Data->iFcode, 0, 0); |
Data->predMV, Data->iFcode, 0, 0); |
1188 |
|
|
1189 |
} else { |
} else { |
1190 |
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); |
1191 |
if (block != 0) { |
if (block != 0) { |
1192 |
if (block != 0) i = d_mv_bits( Data->currentMV->x - Data->predMV.x, |
if (block != 0) i = d_mv_bits( Data->currentMV->x, Data->currentMV->y, |
1193 |
Data->currentMV->y - Data->predMV.y, Data->iFcode, 0, Data->rrv); |
Data->predMV, Data->iFcode, 0, Data->rrv); |
1194 |
} |
} |
1195 |
} |
} |
1196 |
|
|
1338 |
|
|
1339 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
1340 |
|
|
1341 |
int i, iDirection, mask; |
int i, iDirection = 255, mask; |
1342 |
VECTOR pmv[7]; |
VECTOR pmv[7]; |
1343 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1344 |
*Data->iMinSAD = MV_MAX_ERROR; |
*Data->iMinSAD = MV_MAX_ERROR; |
1363 |
CheckCandidate = CheckCandidate16no4v; |
CheckCandidate = CheckCandidate16no4v; |
1364 |
|
|
1365 |
// main loop. checking all predictions |
// main loop. checking all predictions |
1366 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 7; i++) { |
1367 |
if (!(mask = make_mask(pmv, i)) ) continue; |
if (!(mask = make_mask(pmv, i)) ) continue; |
1368 |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data); |
1369 |
} |
} |
1374 |
MainSearchPtr = AdvDiamondSearch; |
MainSearchPtr = AdvDiamondSearch; |
1375 |
else MainSearchPtr = DiamondSearch; |
else MainSearchPtr = DiamondSearch; |
1376 |
|
|
1377 |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255); |
(*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection); |
1378 |
|
|
1379 |
SubpelRefine(Data); |
SubpelRefine(Data); |
1380 |
|
|
1381 |
if (Data->qpel) { |
if (Data->qpel && *Data->iMinSAD < *best_sad + 300) { |
1382 |
Data->currentQMV->x = 2*Data->currentMV->x; |
Data->currentQMV->x = 2*Data->currentMV->x; |
1383 |
Data->currentQMV->y = 2*Data->currentMV->y; |
Data->currentQMV->y = 2*Data->currentMV->y; |
1384 |
Data->qpel_precision = 1; |
Data->qpel_precision = 1; |
1388 |
} |
} |
1389 |
|
|
1390 |
// three bits are needed to code backward mode. four for forward |
// three bits are needed to code backward mode. four for forward |
1391 |
// we treat the bits just like they were vector's |
|
1392 |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
1393 |
else *Data->iMinSAD += 3 * Data->lambda16; |
else *Data->iMinSAD += 3 * Data->lambda16; |
1394 |
|
|
1406 |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
1407 |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
1408 |
} |
} |
1409 |
if (mode_current == MODE_FORWARD) |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *Data->currentMV; |
1410 |
pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
else pMB->b_mvs[0] = *Data->currentMV; |
|
else |
|
|
pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
|
|
|
|
1411 |
} |
} |
1412 |
|
if (mode_current == MODE_FORWARD) *(Data->currentMV+2) = *Data->currentMV; |
1413 |
|
else *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
1414 |
|
|
1415 |
} |
} |
1416 |
|
|
1545 |
} |
} |
1546 |
} |
} |
1547 |
|
|
|
|
|
1548 |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
1549 |
else CheckCandidate = CheckCandidateDirectno4v; |
else CheckCandidate = CheckCandidateDirectno4v; |
1550 |
|
|
1571 |
|
|
1572 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1573 |
|
|
1574 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V || Data->qpel) pMB->mode = MODE_DIRECT; |
|
pMB->mode = MODE_DIRECT; |
|
1575 |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
else pMB->mode = MODE_DIRECT_NO4V; //for faster compensation |
1576 |
|
|
1577 |
pMB->pmvs[3] = *Data->currentMV; |
pMB->pmvs[3] = *Data->currentMV; |
1688 |
} while (!(iDirection)); |
} while (!(iDirection)); |
1689 |
|
|
1690 |
if (fData->qpel) { |
if (fData->qpel) { |
1691 |
|
if (*fData->iMinSAD > *best_sad + 500) return; |
1692 |
CheckCandidate = CheckCandidateInt; |
CheckCandidate = CheckCandidateInt; |
1693 |
fData->qpel_precision = bData.qpel_precision = 1; |
fData->qpel_precision = bData.qpel_precision = 1; |
1694 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0); |
1698 |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
1699 |
fData->currentQMV[1].y = 2 * fData->currentMV[1].y; |
fData->currentQMV[1].y = 2 * fData->currentMV[1].y; |
1700 |
SubpelRefine(fData); |
SubpelRefine(fData); |
1701 |
|
if (*fData->iMinSAD > *best_sad + 300) return; |
1702 |
fData->currentQMV[2] = fData->currentQMV[0]; |
fData->currentQMV[2] = fData->currentQMV[0]; |
1703 |
SubpelRefine(&bData); |
SubpelRefine(&bData); |
1704 |
} |
} |
1705 |
|
|
1706 |
*fData->iMinSAD += (2+2) * fData->lambda16; // two bits are needed to code interpolate mode. |
*fData->iMinSAD += (2+3) * fData->lambda16; // two bits are needed to code interpolate mode. |
1707 |
|
|
1708 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
1709 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
1829 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
1830 |
|
|
1831 |
// interpolate search comes last, because it uses data from forward and backward as prediction |
// interpolate search comes last, because it uses data from forward and backward as prediction |
|
|
|
1832 |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y, |
1833 |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
1834 |
&frame->image, |
&frame->image, |
1877 |
free(qimage); |
free(qimage); |
1878 |
} |
} |
1879 |
|
|
1880 |
static __inline int |
static __inline void |
1881 |
MEanalyzeMB ( const uint8_t * const pRef, |
MEanalyzeMB ( const uint8_t * const pRef, |
1882 |
const uint8_t * const pCur, |
const uint8_t * const pCur, |
1883 |
const int x, |
const int x, |
1884 |
const int y, |
const int y, |
1885 |
const MBParam * const pParam, |
const MBParam * const pParam, |
1886 |
const MACROBLOCK * const pMBs, |
MACROBLOCK * const pMBs, |
|
MACROBLOCK * const pMB, |
|
1887 |
SearchData * const Data) |
SearchData * const Data) |
1888 |
{ |
{ |
1889 |
|
|
1890 |
int i = 255, mask; |
int i, mask; |
1891 |
VECTOR pmv[3]; |
VECTOR pmv[3]; |
1892 |
*(Data->iMinSAD) = MV_MAX_ERROR; |
MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width]; |
1893 |
|
|
1894 |
|
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
1895 |
|
|
1896 |
//median is only used as prediction. it doesn't have to be real |
//median is only used as prediction. it doesn't have to be real |
1897 |
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
1898 |
else |
else |
1899 |
if (x == 1) //left macroblock does not have any vector now |
if (x == 1) //left macroblock does not have any vector now |
1900 |
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; // top instead of median |
1901 |
else if (y == 1) // top macroblock don't have it's vector |
else if (y == 1) // top macroblock doesn't have it's vector |
1902 |
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
Data->predMV = (pMB - 1)->mvs[0]; // left instead of median |
1903 |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median |
1904 |
|
|
1905 |
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, |
1906 |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, 0); |
pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv); |
1907 |
|
|
1908 |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
1909 |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
Data->Ref = pRef + (x + y * pParam->edged_width) * 16; |
1914 |
pmv[2].y = EVEN(Data->predMV.y); |
pmv[2].y = EVEN(Data->predMV.y); |
1915 |
pmv[0].x = pmv[0].y = 0; |
pmv[0].x = pmv[0].y = 0; |
1916 |
|
|
1917 |
CheckCandidate16no4vI(0, 0, 255, &i, Data); |
CheckCandidate32I(0, 0, 255, &i, Data); |
1918 |
|
|
1919 |
//early skip for 0,0 |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) { |
|
if (*Data->iMinSAD < MAX_SAD00_FOR_SKIP * 4) { |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
|
|
pMB->mode = MODE_NOT_CODED; |
|
|
return 0; |
|
|
} |
|
1920 |
|
|
1921 |
if (!(mask = make_mask(pmv, 1))) |
if (!(mask = make_mask(pmv, 1))) |
1922 |
CheckCandidate16no4vI(pmv[1].x, pmv[1].y, mask, &i, Data); |
CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data); |
1923 |
if (!(mask = make_mask(pmv, 2))) |
if (!(mask = make_mask(pmv, 2))) |
1924 |
CheckCandidate16no4vI(pmv[2].x, pmv[2].y, mask, &i, Data); |
CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data); |
1925 |
|
|
1926 |
if (*Data->iMinSAD > MAX_SAD00_FOR_SKIP * 6) // diamond only if needed |
if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed |
1927 |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i); |
1928 |
|
|
1929 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; |
for (i = 0; i < 4; i++) { |
1930 |
pMB->mode = MODE_INTER; |
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1) * pParam->mb_width)]; |
1931 |
return *(Data->iMinSAD); |
MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; |
1932 |
|
MB->mode = MODE_INTER; |
1933 |
|
MB->sad16 = Data->iMinSAD[i+1]; |
1934 |
|
} |
1935 |
|
} |
1936 |
} |
} |
1937 |
|
|
1938 |
#define INTRA_THRESH 1350 |
#define INTRA_BIAS 2500 |
1939 |
#define INTER_THRESH 1200 |
#define INTRA_THRESH 1500 |
1940 |
|
#define INTER_THRESH 1400 |
1941 |
|
|
1942 |
|
|
1943 |
int |
int |
1953 |
MACROBLOCK * const pMBs = Current->mbs; |
MACROBLOCK * const pMBs = Current->mbs; |
1954 |
const IMAGE * const pCurrent = &Current->image; |
const IMAGE * const pCurrent = &Current->image; |
1955 |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
1956 |
|
const VECTOR zeroMV = {0,0}; |
1957 |
|
|
1958 |
VECTOR currentMV; |
int32_t iMinSAD[5], temp[5]; |
1959 |
int32_t iMinSAD; |
VECTOR currentMV[5]; |
1960 |
SearchData Data; |
SearchData Data; |
1961 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
1962 |
Data.currentMV = ¤tMV; |
Data.currentMV = currentMV; |
1963 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = iMinSAD; |
1964 |
Data.iFcode = Current->fcode; |
Data.iFcode = Current->fcode; |
1965 |
CheckCandidate = CheckCandidate16no4vI; |
Data.rrv = Current->global_flags & XVID_REDUCED; |
1966 |
|
Data.temp = temp; |
1967 |
|
CheckCandidate = CheckCandidate32I; |
1968 |
|
|
1969 |
if (intraCount < 10) // we're right after an I frame |
if (intraCount < 10) // we're right after an I frame |
1970 |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
1972 |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
1973 |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
1974 |
|
|
|
|
|
1975 |
InterThresh += 400 * (1 - bCount); |
InterThresh += 400 * (1 - bCount); |
1976 |
if (InterThresh < 200) InterThresh = 200; |
if (InterThresh < 300) InterThresh = 300; |
1977 |
|
|
1978 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
1979 |
|
|
1980 |
for (y = 1; y < pParam->mb_height-1; y++) { |
for (y = 1; y < pParam->mb_height-1; y+=2) { |
1981 |
for (x = 1; x < pParam->mb_width-1; x++) { |
for (x = 1; x < pParam->mb_width-1; x+=2) { |
1982 |
int sad, dev; |
int i; |
1983 |
MACROBLOCK *pMB = &pMBs[x + y * pParam->mb_width]; |
|
1984 |
|
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
1985 |
|
|
1986 |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
|
pParam, pMBs, pMB, &Data); |
|
1987 |
|
|
1988 |
if (sad > IntraThresh) { |
for (i = 0; i < 4; i++) { |
1989 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
int dev; |
1990 |
|
MACROBLOCK *pMB = &pMBs[x+(i&1) + y+(i>>1) * pParam->mb_width]; |
1991 |
|
if (pMB->sad16 > IntraThresh) { |
1992 |
|
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1))* pParam->edged_width) * 16, |
1993 |
pParam->edged_width); |
pParam->edged_width); |
1994 |
if (dev + IntraThresh < sad) { |
if (dev + IntraThresh < pMB->sad16) { |
1995 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1996 |
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP; |
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP; |
1997 |
} |
} |
1998 |
} |
} |
1999 |
sSAD += sad; |
sSAD += pMB->sad16; |
2000 |
|
} |
2001 |
} |
} |
2002 |
} |
} |
2003 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2004 |
|
if (sSAD > IntraThresh + INTRA_BIAS ) return I_VOP; |
2005 |
if (sSAD > InterThresh ) return P_VOP; |
if (sSAD > InterThresh ) return P_VOP; |
2006 |
emms(); |
emms(); |
2007 |
return B_VOP; |
return B_VOP; |