61 |
default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
default : REF = (uint8_t *)data->RefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
62 |
} \ |
} \ |
63 |
} |
} |
64 |
|
// I hate those macros :/ |
65 |
|
#define GET_REFERENCE2(X, Y, REF) { \ |
66 |
|
switch ( (((X)&1)<<1) + ((Y)&1) ) \ |
67 |
|
{ \ |
68 |
|
case 0 : REF = (uint8_t *)data->bRef + (X)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
69 |
|
case 1 : REF = (uint8_t *)data->bRefV + (X)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
70 |
|
case 2 : REF = (uint8_t *)data->bRefH + ((X)-1)/2 + ((Y)/2)*(data->iEdgedWidth); break; \ |
71 |
|
default : REF = (uint8_t *)data->bRefHV + ((X)-1)/2 + (((Y)-1)/2)*(data->iEdgedWidth); break; \ |
72 |
|
} \ |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
#define iDiamondSize 2 |
#define iDiamondSize 2 |
77 |
|
|
201 |
} |
} |
202 |
|
|
203 |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
sad = sad16(data->Cur, Reference, data->iEdgedWidth, MV_MAX_ERROR); |
204 |
|
if (data->qpel) //only to be used in b-frames' ME |
205 |
|
sad += (data->lambda16 * d_mv_bits(2*x - data->predMV.x, 2*y - data->predMV.y, data->iFcode) * sad)/1000; |
206 |
|
else |
207 |
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
208 |
|
|
209 |
if (sad < *(data->iMinSAD)) { |
if (sad < *(data->iMinSAD)) { |
291 |
} |
} |
292 |
|
|
293 |
static void |
static void |
294 |
|
CheckCandidate16no4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
295 |
|
|
296 |
|
// CheckCandidate16no4v variant which expects x and y in quarter pixel resolution |
297 |
|
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
298 |
|
// around currentMV! |
299 |
|
// this function is for B-frames' search only |
300 |
|
{ |
301 |
|
uint8_t * Reference = (uint8_t *)data->RefQ; |
302 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
303 |
|
VECTOR halfpelMV = *(data->currentMV); |
304 |
|
|
305 |
|
int32_t iEdgedWidth = data->iEdgedWidth; |
306 |
|
int32_t sad; |
307 |
|
|
308 |
|
if (( x > data->max_dx) || ( x < data->min_dx) |
309 |
|
|| ( y > data->max_dy) || (y < data->min_dy)) return; |
310 |
|
|
311 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this refenrence is used in all cases |
312 |
|
switch( ((x&1)<<1) + (y&1) ) |
313 |
|
{ |
314 |
|
case 0: // pure halfpel position - shouldn't happen during a refinement step |
315 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, Reference); |
316 |
|
break; |
317 |
|
|
318 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
319 |
|
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
320 |
|
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0); |
321 |
|
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0); |
322 |
|
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
323 |
|
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
324 |
|
break; |
325 |
|
|
326 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
327 |
|
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref2); |
328 |
|
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, 0); |
329 |
|
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, 0); |
330 |
|
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
331 |
|
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
332 |
|
break; |
333 |
|
|
334 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
335 |
|
// bottom left/right) during qpel refinement |
336 |
|
GET_REFERENCE(halfpelMV.x, y - halfpelMV.y, ref2); |
337 |
|
GET_REFERENCE(x - halfpelMV.x, halfpelMV.y, ref3); |
338 |
|
GET_REFERENCE(x - halfpelMV.x, y - halfpelMV.y, ref4); |
339 |
|
|
340 |
|
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
341 |
|
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
342 |
|
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
343 |
|
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
344 |
|
break; |
345 |
|
} |
346 |
|
|
347 |
|
sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096); |
348 |
|
sad += (data->lambda16 * d_mv_bits(x - data->predMV.x, y - data->predMV.y, data->iFcode) * sad)/1000; |
349 |
|
|
350 |
|
if (sad < data->iMinSAD[0]) { |
351 |
|
data->iMinSAD[0] = sad; |
352 |
|
data->currentQMV[0].x = x; data->currentQMV[0].y = y; |
353 |
|
/* *dir = Direction;*/ } |
354 |
|
} |
355 |
|
|
356 |
|
static void |
357 |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate16no4vI(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
358 |
{ |
{ |
359 |
|
// maximum speed - for P/B/I decision |
360 |
int32_t sad; |
int32_t sad; |
361 |
|
|
362 |
if (( x > data->max_dx) || ( x < data->min_dx) |
if (( x > data->max_dx) || ( x < data->min_dx) |
399 |
|
|
400 |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
401 |
|
|
402 |
|
if (data->qpel) |
403 |
|
sad += (data->lambda16 * |
404 |
|
( d_mv_bits(2*xf - data->predMV.x, 2*yf - data->predMV.y, data->iFcode) + |
405 |
|
d_mv_bits(2*xb - data->bpredMV.x, 2*yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
406 |
|
else |
407 |
sad += (data->lambda16 * |
sad += (data->lambda16 * |
408 |
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
409 |
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
414 |
*dir = Direction; } |
*dir = Direction; } |
415 |
} |
} |
416 |
|
|
417 |
|
|
418 |
|
static void |
419 |
|
CheckCandidateInt_qpel(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data) |
420 |
|
{ |
421 |
|
// CheckCandidateInt variant which expects x and y in quarter pixel resolution |
422 |
|
|
423 |
|
int32_t sad; |
424 |
|
const int xb = data->currentQMV[1].x; |
425 |
|
const int yb = data->currentQMV[1].y; |
426 |
|
uint8_t * ReferenceF = (uint8_t *)data->RefQ; |
427 |
|
uint8_t * ReferenceB = (uint8_t *)data->RefQ + 16; |
428 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
429 |
|
VECTOR halfpelMV; |
430 |
|
const int32_t iEdgedWidth = data->iEdgedWidth; |
431 |
|
|
432 |
|
if (( xf > data->max_dx) || ( xf < data->min_dx) |
433 |
|
|| ( yf > data->max_dy) || (yf < data->min_dy)) return; |
434 |
|
|
435 |
|
halfpelMV.x = xf/2; //forward first |
436 |
|
halfpelMV.y = yf/2; |
437 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
438 |
|
switch( ((xf&1)<<1) + (yf&1) ) |
439 |
|
{ |
440 |
|
case 0: // pure halfpel position - shouldn't happen during a refinement step |
441 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF); |
442 |
|
break; |
443 |
|
|
444 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
445 |
|
GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2); |
446 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
447 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
448 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
449 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
450 |
|
break; |
451 |
|
|
452 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
453 |
|
GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref2); |
454 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
455 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
456 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
457 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
458 |
|
break; |
459 |
|
|
460 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
461 |
|
// bottom left/right) during qpel refinement |
462 |
|
GET_REFERENCE(halfpelMV.x, yf - halfpelMV.y, ref2); |
463 |
|
GET_REFERENCE(xf - halfpelMV.x, halfpelMV.y, ref3); |
464 |
|
GET_REFERENCE(xf - halfpelMV.x, yf - halfpelMV.y, ref4); |
465 |
|
|
466 |
|
interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
467 |
|
interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
468 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
469 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
470 |
|
break; |
471 |
|
} |
472 |
|
|
473 |
|
halfpelMV.x = xb/2; //backward |
474 |
|
halfpelMV.y = yb/2; |
475 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
476 |
|
switch( ((xb&1)<<1) + (yb&1) ) |
477 |
|
{ |
478 |
|
case 0: // pure halfpel position - shouldn't happen during a refinement step |
479 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB); |
480 |
|
break; |
481 |
|
|
482 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
483 |
|
GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2); |
484 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
485 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
486 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
487 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
488 |
|
break; |
489 |
|
|
490 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
491 |
|
GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref2); |
492 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
493 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
494 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
495 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
496 |
|
break; |
497 |
|
|
498 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
499 |
|
// bottom left/right) during qpel refinement |
500 |
|
GET_REFERENCE2(halfpelMV.x, yb - halfpelMV.y, ref2); |
501 |
|
GET_REFERENCE2(xb - halfpelMV.x, halfpelMV.y, ref3); |
502 |
|
GET_REFERENCE2(xb - halfpelMV.x, yb - halfpelMV.y, ref4); |
503 |
|
|
504 |
|
interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
505 |
|
interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
506 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
507 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
508 |
|
break; |
509 |
|
} |
510 |
|
|
511 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
512 |
|
|
513 |
|
sad += (data->lambda16 * |
514 |
|
( d_mv_bits(xf - data->predMV.x, yf - data->predMV.y, data->iFcode) + |
515 |
|
d_mv_bits(xb - data->bpredMV.x, yb - data->bpredMV.y, data->iFcode)) * sad)/1000; |
516 |
|
|
517 |
|
if (sad < *(data->iMinSAD)) { |
518 |
|
*(data->iMinSAD) = sad; |
519 |
|
data->currentQMV->x = xf; data->currentQMV->y = yf; |
520 |
|
*dir = Direction; } |
521 |
|
} |
522 |
|
|
523 |
static void |
static void |
524 |
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) |
525 |
{ |
{ |
576 |
*dir = Direction; } |
*dir = Direction; } |
577 |
} |
} |
578 |
|
|
579 |
|
|
580 |
|
static void |
581 |
|
CheckCandidateDirect_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
582 |
|
{ |
583 |
|
int32_t sad = 0; |
584 |
|
int k; |
585 |
|
VECTOR mvs, b_mvs, halfpelMV; |
586 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
587 |
|
uint8_t *ReferenceF, *ReferenceB; |
588 |
|
const uint32_t iEdgedWidth = data->iEdgedWidth; |
589 |
|
|
590 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
591 |
|
|
592 |
|
for (k = 0; k < 4; k++) { |
593 |
|
ReferenceF = (uint8_t *)data->RefQ; |
594 |
|
ReferenceB = (uint8_t *)data->RefQ + 64; |
595 |
|
|
596 |
|
mvs.x = data->directmvF[k].x + x; |
597 |
|
b_mvs.x = ((x == 0) ? |
598 |
|
data->directmvB[k].x |
599 |
|
: mvs.x - data->referencemv[k].x); |
600 |
|
|
601 |
|
mvs.y = data->directmvF[k].y + y; |
602 |
|
b_mvs.y = ((y == 0) ? |
603 |
|
data->directmvB[k].y |
604 |
|
: mvs.y - data->referencemv[k].y); |
605 |
|
|
606 |
|
if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx ) |
607 |
|
|| ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy ) |
608 |
|
|| ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx ) |
609 |
|
|| ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return; |
610 |
|
|
611 |
|
halfpelMV.x = mvs.x/2; //forward first |
612 |
|
halfpelMV.y = mvs.y/2; |
613 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
614 |
|
switch( ((mvs.x&1)<<1) + (mvs.y&1) ) { |
615 |
|
case 0: // pure halfpel position |
616 |
|
GET_REFERENCE(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceF); |
617 |
|
break; |
618 |
|
|
619 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
620 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
621 |
|
interpolate8x8_avg2(ReferenceF, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
622 |
|
ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
623 |
|
break; |
624 |
|
|
625 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
626 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
627 |
|
interpolate8x8_avg2(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
628 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
629 |
|
break; |
630 |
|
|
631 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
632 |
|
// bottom left/right) during qpel refinement |
633 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
634 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
635 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4); |
636 |
|
interpolate8x8_avg4(ReferenceF, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
637 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
638 |
|
ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
639 |
|
ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
640 |
|
break; |
641 |
|
} |
642 |
|
|
643 |
|
halfpelMV.x = b_mvs.x/2; |
644 |
|
halfpelMV.y = b_mvs.y/2; |
645 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in most cases |
646 |
|
switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) { |
647 |
|
case 0: // pure halfpel position |
648 |
|
GET_REFERENCE2(halfpelMV.x + 16*(k&1), halfpelMV.y + 16*(k>>1), ReferenceB); |
649 |
|
break; |
650 |
|
|
651 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
652 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
653 |
|
interpolate8x8_avg2(ReferenceB, ref1+8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
654 |
|
ref2+ 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
655 |
|
break; |
656 |
|
|
657 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
658 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
659 |
|
interpolate8x8_avg2(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
660 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
661 |
|
break; |
662 |
|
|
663 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
664 |
|
// bottom left/right) during qpel refinement |
665 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
666 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
667 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4); |
668 |
|
interpolate8x8_avg4(ReferenceB, ref1 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
669 |
|
ref2 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
670 |
|
ref3 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
671 |
|
ref4 + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), iEdgedWidth, 0); |
672 |
|
break; |
673 |
|
} |
674 |
|
|
675 |
|
sad += sad8bi(data->Cur + 8*(k&1) + 8*(k>>1)*(data->iEdgedWidth), |
676 |
|
ReferenceF, |
677 |
|
ReferenceB, |
678 |
|
data->iEdgedWidth); |
679 |
|
if (sad > *(data->iMinSAD)) return; |
680 |
|
} |
681 |
|
|
682 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
683 |
|
|
684 |
|
if (sad < *(data->iMinSAD)) { |
685 |
|
*(data->iMinSAD) = sad; |
686 |
|
data->currentMV->x = x; data->currentMV->y = y; |
687 |
|
*dir = Direction; } |
688 |
|
} |
689 |
|
|
690 |
|
static void |
691 |
|
CheckCandidateDirectno4v_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
692 |
|
{ |
693 |
|
int32_t sad = 0; |
694 |
|
VECTOR mvs, b_mvs, halfpelMV; |
695 |
|
const uint8_t *ref1, *ref2, *ref3, *ref4; |
696 |
|
const uint32_t iEdgedWidth = data->iEdgedWidth; |
697 |
|
uint8_t * ReferenceF = (uint8_t *)data->RefQ; |
698 |
|
uint8_t * ReferenceB = (uint8_t *)data->RefQ + 64; |
699 |
|
|
700 |
|
if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return; |
701 |
|
|
702 |
|
mvs.x = data->directmvF[0].x + x; |
703 |
|
b_mvs.x = ((x == 0) ? |
704 |
|
data->directmvB[0].x |
705 |
|
: mvs.x - data->referencemv[0].x); |
706 |
|
|
707 |
|
mvs.y = data->directmvF[0].y + y; |
708 |
|
b_mvs.y = ((y == 0) ? |
709 |
|
data->directmvB[0].y |
710 |
|
: mvs.y - data->referencemv[0].y); |
711 |
|
|
712 |
|
if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx ) |
713 |
|
|| ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy ) |
714 |
|
|| ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx ) |
715 |
|
|| ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return; |
716 |
|
|
717 |
|
halfpelMV.x = mvs.x/2; //forward first |
718 |
|
halfpelMV.y = mvs.y/2; |
719 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ref1); // this reference is used in all cases |
720 |
|
switch( ((mvs.x&1)<<1) + (mvs.y&1) ) { |
721 |
|
case 0: // pure halfpel position |
722 |
|
GET_REFERENCE(halfpelMV.x, halfpelMV.y, ReferenceF); |
723 |
|
break; |
724 |
|
|
725 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
726 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
727 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
728 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
729 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
730 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
731 |
|
break; |
732 |
|
|
733 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
734 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
735 |
|
interpolate8x8_avg2(ReferenceF, ref1, ref2, iEdgedWidth, 0); |
736 |
|
interpolate8x8_avg2(ReferenceF+8, ref1+8, ref2+8, iEdgedWidth, 0); |
737 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
738 |
|
interpolate8x8_avg2(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
739 |
|
break; |
740 |
|
|
741 |
|
default: // x and y in qpel resolution |
742 |
|
GET_REFERENCE(halfpelMV.x, mvs.y - halfpelMV.y, ref2); |
743 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
744 |
|
GET_REFERENCE(mvs.x - halfpelMV.x, mvs.y - halfpelMV.y, ref4); |
745 |
|
|
746 |
|
interpolate8x8_avg4(ReferenceF, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
747 |
|
interpolate8x8_avg4(ReferenceF+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
748 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
749 |
|
interpolate8x8_avg4(ReferenceF+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
750 |
|
break; |
751 |
|
} |
752 |
|
|
753 |
|
halfpelMV.x = b_mvs.x/2; //backward |
754 |
|
halfpelMV.y = b_mvs.y/2; |
755 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ref1); |
756 |
|
switch( ((b_mvs.x&1)<<1) + (b_mvs.y&1) ) |
757 |
|
{ |
758 |
|
case 0: // pure halfpel position |
759 |
|
GET_REFERENCE2(halfpelMV.x, halfpelMV.y, ReferenceB); |
760 |
|
break; |
761 |
|
|
762 |
|
case 1: // x halfpel, y qpel - top or bottom during qpel refinement |
763 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
764 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
765 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
766 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
767 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
768 |
|
break; |
769 |
|
|
770 |
|
case 2: // x qpel, y halfpel - left or right during qpel refinement |
771 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref2); |
772 |
|
interpolate8x8_avg2(ReferenceB, ref1, ref2, iEdgedWidth, 0); |
773 |
|
interpolate8x8_avg2(ReferenceB+8, ref1+8, ref2+8, iEdgedWidth, 0); |
774 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, 0); |
775 |
|
interpolate8x8_avg2(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, 0); |
776 |
|
break; |
777 |
|
|
778 |
|
default: // x and y in qpel resolution - the "corners" (top left/right and |
779 |
|
// bottom left/right) during qpel refinement |
780 |
|
GET_REFERENCE2(halfpelMV.x, b_mvs.y - halfpelMV.y, ref2); |
781 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, halfpelMV.y, ref3); |
782 |
|
GET_REFERENCE2(b_mvs.x - halfpelMV.x, b_mvs.y - halfpelMV.y, ref4); |
783 |
|
|
784 |
|
interpolate8x8_avg4(ReferenceB, ref1, ref2, ref3, ref4, iEdgedWidth, 0); |
785 |
|
interpolate8x8_avg4(ReferenceB+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, 0); |
786 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, 0); |
787 |
|
interpolate8x8_avg4(ReferenceB+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, 0); |
788 |
|
break; |
789 |
|
} |
790 |
|
|
791 |
|
sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth); |
792 |
|
sad += (data->lambda16 * d_mv_bits(x, y, 1) * sad)/1000; |
793 |
|
|
794 |
|
if (sad < *(data->iMinSAD)) { |
795 |
|
*(data->iMinSAD) = sad; |
796 |
|
data->currentMV->x = x; data->currentMV->y = y; |
797 |
|
*dir = Direction; } |
798 |
|
} |
799 |
|
|
800 |
|
|
801 |
static void |
static void |
802 |
CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
803 |
{ |
{ |
877 |
|
|
878 |
static void |
static void |
879 |
CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
CheckCandidate8_qpel(const int x, const int y, const int Direction, int * const dir, const SearchData * const data) |
880 |
// CheckCandidate16no4v variant which expects x and y in quarter pixel resolution |
// CheckCandidate8 variant which expects x and y in quarter pixel resolution |
881 |
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
// Important: This is no general usable routine! x and y must be +/-1 (qpel resolution!) |
882 |
// around currentMV! |
// around currentMV! |
883 |
|
|
1730 |
pParam->width, pParam->height, iFcode, pParam->m_quarterpel); |
pParam->width, pParam->height, iFcode, pParam->m_quarterpel); |
1731 |
|
|
1732 |
pmv[0] = Data->predMV; |
pmv[0] = Data->predMV; |
1733 |
|
if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; } |
1734 |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
PreparePredictionsBF(pmv, x, y, pParam->mb_width, pMB, mode_current); |
1735 |
|
|
1736 |
Data->currentMV->x = Data->currentMV->y = 0; |
Data->currentMV->x = Data->currentMV->y = 0; |
1753 |
|
|
1754 |
HalfpelRefine(Data); |
HalfpelRefine(Data); |
1755 |
|
|
1756 |
|
if (Data->qpel) { |
1757 |
|
Data->currentQMV->x = 2*Data->currentMV->x; |
1758 |
|
Data->currentQMV->y = 2*Data->currentMV->y; |
1759 |
|
CheckCandidate = CheckCandidate16no4v_qpel; |
1760 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16, |
1761 |
|
pParam->width, pParam->height, iFcode, pParam->m_quarterpel); |
1762 |
|
QuarterpelRefine(Data); |
1763 |
|
} |
1764 |
|
|
1765 |
// three bits are needed to code backward mode. four for forward |
// three bits are needed to code backward mode. four for forward |
1766 |
// we treat the bits just like they were vector's |
// we treat the bits just like they were vector's |
1767 |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
if (mode_current == MODE_FORWARD) *Data->iMinSAD += 4 * Data->lambda16; |
1770 |
if (*Data->iMinSAD < *best_sad) { |
if (*Data->iMinSAD < *best_sad) { |
1771 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1772 |
pMB->mode = mode_current; |
pMB->mode = mode_current; |
1773 |
|
if (Data->qpel) { |
1774 |
|
pMB->pmvs[0].x = Data->currentQMV->x - predMV->x; |
1775 |
|
pMB->pmvs[0].y = Data->currentQMV->y - predMV->y; |
1776 |
|
if (mode_current == MODE_FORWARD) |
1777 |
|
pMB->qmvs[0] = *Data->currentQMV; |
1778 |
|
else |
1779 |
|
pMB->b_qmvs[0] = *Data->currentQMV; |
1780 |
|
} else { |
1781 |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
pMB->pmvs[0].x = Data->currentMV->x - predMV->x; |
1782 |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
pMB->pmvs[0].y = Data->currentMV->y - predMV->y; |
1783 |
if (mode_current == MODE_FORWARD) pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
} |
1784 |
else pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
if (mode_current == MODE_FORWARD) |
1785 |
|
pMB->mvs[0] = *(Data->currentMV+2) = *Data->currentMV; |
1786 |
|
else |
1787 |
|
pMB->b_mvs[0] = *(Data->currentMV+1) = *Data->currentMV; //we store currmv for interpolate search |
1788 |
|
|
1789 |
} |
} |
1790 |
|
|
1791 |
} |
} |
1816 |
MainSearchFunc *MainSearchPtr; |
MainSearchFunc *MainSearchPtr; |
1817 |
|
|
1818 |
*Data->iMinSAD = 256*4096; |
*Data->iMinSAD = 256*4096; |
|
Data->referencemv = b_mb->mvs; |
|
1819 |
|
|
1820 |
Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16; |
Data->Ref = f_Ref->y + (x + Data->iEdgedWidth*y) * 16; |
1821 |
Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16; |
Data->RefH = f_RefH + (x + Data->iEdgedWidth*y) * 16; |
1830 |
Data->max_dy = 2 * pParam->height - 2 * (y) * 16; |
Data->max_dy = 2 * pParam->height - 2 * (y) * 16; |
1831 |
Data->min_dx = -(2 * 16 + 2 * (x) * 16); |
Data->min_dx = -(2 * 16 + 2 * (x) * 16); |
1832 |
Data->min_dy = -(2 * 16 + 2 * (y) * 16); |
Data->min_dy = -(2 * 16 + 2 * (y) * 16); |
1833 |
|
if (Data->qpel) { //we measure in qpixels |
1834 |
|
Data->max_dx *= 2; |
1835 |
|
Data->max_dy *= 2; |
1836 |
|
Data->min_dx *= 2; |
1837 |
|
Data->min_dy *= 2; |
1838 |
|
Data->referencemv = b_mb->qmvs; |
1839 |
|
} else Data->referencemv = b_mb->mvs; |
1840 |
|
|
1841 |
for (k = 0; k < 4; k++) { |
for (k = 0; k < 4; k++) { |
1842 |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
pMB->mvs[k].x = Data->directmvF[k].x = ((TRB * Data->referencemv[k].x) / TRD); |
1861 |
} |
} |
1862 |
} |
} |
1863 |
|
|
1864 |
|
if (Data->qpel) { |
1865 |
if (b_mb->mode == MODE_INTER4V) |
if (b_mb->mode == MODE_INTER4V) |
1866 |
CheckCandidate = CheckCandidateDirect; |
CheckCandidate = CheckCandidateDirect_qpel; |
1867 |
|
else CheckCandidate = CheckCandidateDirectno4v_qpel; |
1868 |
|
} else { |
1869 |
|
if (b_mb->mode == MODE_INTER4V) CheckCandidate = CheckCandidateDirect; |
1870 |
else CheckCandidate = CheckCandidateDirectno4v; |
else CheckCandidate = CheckCandidateDirectno4v; |
1871 |
|
} |
1872 |
|
|
1873 |
(*CheckCandidate)(0, 0, 255, &k, Data); |
(*CheckCandidate)(0, 0, 255, &k, Data); |
1874 |
|
|
1878 |
//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 |
1879 |
int sum, dx, dy, b_dx, b_dy; |
int sum, dx, dy, b_dx, b_dy; |
1880 |
|
|
1881 |
|
if (Data->qpel) { |
1882 |
|
sum = pMB->mvs[0].y/2 + pMB->mvs[1].y/2 + pMB->mvs[2].y/2 + pMB->mvs[3].y/2; |
1883 |
|
dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1884 |
|
sum = pMB->mvs[0].x/2 + pMB->mvs[1].x/2 + pMB->mvs[2].x/2 + pMB->mvs[3].x/2; |
1885 |
|
dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1886 |
|
|
1887 |
|
sum = pMB->b_mvs[0].y/2 + pMB->b_mvs[1].y/2 + pMB->b_mvs[2].y/2 + pMB->b_mvs[3].y/2; |
1888 |
|
b_dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
1889 |
|
sum = pMB->b_mvs[0].x/2 + pMB->b_mvs[1].x/2 + pMB->b_mvs[2].x/2 + pMB->b_mvs[3].x/2; |
1890 |
|
b_dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
1891 |
|
|
1892 |
|
} else { |
1893 |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1894 |
dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
|
1895 |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1896 |
dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
1897 |
|
|
1898 |
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
1899 |
b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
b_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
|
|
1900 |
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
1901 |
b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
b_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
1902 |
|
} |
1903 |
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
sum = sad8bi(pCur->u + 8*x + 8*y*(Data->iEdgedWidth/2), |
1904 |
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
f_Ref->u + (y*8 + dy/2) * (Data->iEdgedWidth/2) + x*8 + dx/2, |
1905 |
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
b_Ref->u + (y*8 + b_dy/2) * (Data->iEdgedWidth/2) + x*8 + b_dx/2, |
1926 |
|
|
1927 |
(*MainSearchPtr)(0, 0, Data, 255); |
(*MainSearchPtr)(0, 0, Data, 255); |
1928 |
|
|
1929 |
HalfpelRefine(Data); |
HalfpelRefine(Data); //or qpel refine, if we're in qpel mode |
1930 |
|
|
1931 |
*Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode |
*Data->iMinSAD += 1 * Data->lambda16; // one bit is needed to code direct mode |
1932 |
*best_sad = *Data->iMinSAD; |
*best_sad = *Data->iMinSAD; |
1946 |
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
pMB->b_mvs[k].y = ((Data->currentMV->y == 0) |
1947 |
? Data->directmvB[k].y |
? Data->directmvB[k].y |
1948 |
: pMB->mvs[k].y - Data->referencemv[k].y); |
: pMB->mvs[k].y - Data->referencemv[k].y); |
1949 |
|
if (Data->qpel) { |
1950 |
|
pMB->qmvs[k].x = pMB->mvs[k].x; pMB->mvs[k].x /= 2; |
1951 |
|
pMB->b_qmvs[k].x = pMB->b_mvs[k].x; pMB->b_mvs[k].x /= 2; |
1952 |
|
pMB->qmvs[k].y = pMB->mvs[k].y; pMB->mvs[k].y /= 2; |
1953 |
|
pMB->b_qmvs[k].y = pMB->b_mvs[k].y; pMB->b_mvs[k].y /= 2; |
1954 |
|
} |
1955 |
|
|
1956 |
if (b_mb->mode != MODE_INTER4V) { |
if (b_mb->mode != MODE_INTER4V) { |
1957 |
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
pMB->mvs[3] = pMB->mvs[2] = pMB->mvs[1] = pMB->mvs[0]; |
1958 |
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
pMB->b_mvs[3] = pMB->b_mvs[2] = pMB->b_mvs[1] = pMB->b_mvs[0]; |
1959 |
|
pMB->qmvs[3] = pMB->qmvs[2] = pMB->qmvs[1] = pMB->qmvs[0]; |
1960 |
|
pMB->b_qmvs[3] = pMB->b_qmvs[2] = pMB->b_qmvs[1] = pMB->b_qmvs[0]; |
1961 |
break; |
break; |
1962 |
} |
} |
1963 |
} |
} |
1989 |
{ |
{ |
1990 |
|
|
1991 |
const int32_t iEdgedWidth = pParam->edged_width; |
const int32_t iEdgedWidth = pParam->edged_width; |
|
|
|
1992 |
int iDirection, i, j; |
int iDirection, i, j; |
1993 |
SearchData bData; |
SearchData bData; |
1994 |
|
|
1995 |
*(bData.iMinSAD = fData->iMinSAD) = 4096*256; |
*(bData.iMinSAD = fData->iMinSAD) = 4096*256; |
1996 |
bData.Cur = fData->Cur; |
bData.Cur = fData->Cur; |
1997 |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
fData->iEdgedWidth = bData.iEdgedWidth = iEdgedWidth; |
1998 |
bData.currentMV = fData->currentMV + 1; |
bData.currentMV = fData->currentMV + 1; bData.currentQMV = fData->currentQMV + 1; |
1999 |
bData.lambda16 = fData->lambda16; |
bData.lambda16 = fData->lambda16; |
2000 |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode; |
2001 |
|
|
2007 |
bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16; |
bData.RefH = fData->bRefH = b_RefH + (x + y * iEdgedWidth) * 16; |
2008 |
bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16; |
bData.RefV = fData->bRefV = b_RefV + (x + y * iEdgedWidth) * 16; |
2009 |
bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16; |
bData.RefHV = fData->bRefHV = b_RefHV + (x + y * iEdgedWidth) * 16; |
2010 |
|
bData.RefQ = fData->RefQ; |
2011 |
|
|
2012 |
bData.bpredMV = fData->predMV = *f_predMV; |
bData.bpredMV = fData->predMV = *f_predMV; |
2013 |
fData->bpredMV = bData.predMV = *b_predMV; |
fData->bpredMV = bData.predMV = *b_predMV; |
2014 |
|
|
2015 |
fData->currentMV[0] = fData->currentMV[3]; //forward search stored it's vector here. backward stored it in the place it's needed |
fData->currentMV[0] = fData->currentMV[2]; |
2016 |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel); |
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, pParam->m_quarterpel); |
2017 |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel); |
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, pParam->m_quarterpel); |
2018 |
|
|
2043 |
// backward MV moves |
// backward MV moves |
2044 |
i = fData->currentMV[1].x; j = fData->currentMV[1].y; |
i = fData->currentMV[1].x; j = fData->currentMV[1].y; |
2045 |
fData->currentMV[2] = fData->currentMV[0]; |
fData->currentMV[2] = fData->currentMV[0]; |
|
|
|
2046 |
CheckCandidateInt(i + 1, j, 0, &iDirection, &bData); |
CheckCandidateInt(i + 1, j, 0, &iDirection, &bData); |
2047 |
CheckCandidateInt(i, j + 1, 0, &iDirection, &bData); |
CheckCandidateInt(i, j + 1, 0, &iDirection, &bData); |
2048 |
CheckCandidateInt(i - 1, j, 0, &iDirection, &bData); |
CheckCandidateInt(i - 1, j, 0, &iDirection, &bData); |
2052 |
|
|
2053 |
*fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode. |
*fData->iMinSAD += 2 * fData->lambda16; // two bits are needed to code interpolate mode. |
2054 |
|
|
2055 |
|
if (fData->qpel) { |
2056 |
|
CheckCandidate = CheckCandidateInt_qpel; |
2057 |
|
get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 0); |
2058 |
|
get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 0); |
2059 |
|
fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x; |
2060 |
|
fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y; |
2061 |
|
fData->currentQMV[1].x = 2 * fData->currentMV[1].x; |
2062 |
|
fData->currentQMV[1].y = 2 * fData->currentMV[1].y; |
2063 |
|
// QuarterpelRefine(fData); |
2064 |
|
fData->currentQMV[2] = fData->currentQMV[0]; |
2065 |
|
// QuarterpelRefine(&bData); |
2066 |
|
} |
2067 |
|
|
2068 |
if (*fData->iMinSAD < *best_sad) { |
if (*fData->iMinSAD < *best_sad) { |
2069 |
*best_sad = *fData->iMinSAD; |
*best_sad = *fData->iMinSAD; |
2070 |
pMB->mvs[0] = fData->currentMV[0]; |
pMB->mvs[0] = fData->currentMV[0]; |
2071 |
pMB->b_mvs[0] = fData->currentMV[1]; |
pMB->b_mvs[0] = fData->currentMV[1]; |
2072 |
pMB->mode = MODE_INTERPOLATE; |
pMB->mode = MODE_INTERPOLATE; |
2073 |
|
if (fData->qpel) { |
2074 |
|
pMB->qmvs[0] = fData->currentQMV[0]; |
2075 |
|
pMB->b_qmvs[0] = fData->currentQMV[1]; |
2076 |
|
pMB->pmvs[1].x = pMB->qmvs[0].x - f_predMV->x; |
2077 |
|
pMB->pmvs[1].y = pMB->qmvs[0].y - f_predMV->y; |
2078 |
|
pMB->pmvs[0].x = pMB->b_qmvs[0].x - b_predMV->x; |
2079 |
|
pMB->pmvs[0].y = pMB->b_qmvs[0].y - b_predMV->y; |
2080 |
|
} else { |
2081 |
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
pMB->pmvs[1].x = pMB->mvs[0].x - f_predMV->x; |
2082 |
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
pMB->pmvs[1].y = pMB->mvs[0].y - f_predMV->y; |
2083 |
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
pMB->pmvs[0].x = pMB->b_mvs[0].x - b_predMV->x; |
2084 |
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
pMB->pmvs[0].y = pMB->b_mvs[0].y - b_predMV->y; |
2085 |
} |
} |
2086 |
} |
} |
2087 |
|
} |
2088 |
|
|
2089 |
void |
void |
2090 |
MotionEstimationBVOP(MBParam * const pParam, |
MotionEstimationBVOP(MBParam * const pParam, |
2098 |
const IMAGE * const f_refV, |
const IMAGE * const f_refV, |
2099 |
const IMAGE * const f_refHV, |
const IMAGE * const f_refHV, |
2100 |
// backward (future) reference |
// backward (future) reference |
2101 |
const MACROBLOCK * const b_mbs, |
const FRAMEINFO * const b_reference, |
2102 |
const IMAGE * const b_ref, |
const IMAGE * const b_ref, |
2103 |
const IMAGE * const b_refH, |
const IMAGE * const b_refH, |
2104 |
const IMAGE * const b_refV, |
const IMAGE * const b_refV, |
2108 |
int32_t best_sad, skip_sad; |
int32_t best_sad, skip_sad; |
2109 |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
int f_count = 0, b_count = 0, i_count = 0, d_count = 0, n_count = 0; |
2110 |
static const VECTOR zeroMV={0,0}; |
static const VECTOR zeroMV={0,0}; |
2111 |
|
const MACROBLOCK * const b_mbs = b_reference->mbs; |
2112 |
|
|
2113 |
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
VECTOR f_predMV, b_predMV; /* there is no prediction for direct mode*/ |
2114 |
|
|
2115 |
const int32_t TRB = time_pp - time_bp; |
const int32_t TRB = time_pp - time_bp; |
2116 |
const int32_t TRD = time_pp; |
const int32_t TRD = time_pp; |
2117 |
|
uint8_t * qimage; |
2118 |
|
|
2119 |
// some pre-inintialized data for the rest of the search |
// some pre-inintialized data for the rest of the search |
2120 |
|
|
2121 |
SearchData Data; |
SearchData Data; |
2122 |
int32_t iMinSAD; |
int32_t iMinSAD; |
2123 |
VECTOR currentMV[3]; |
VECTOR currentMV[3]; |
2124 |
|
VECTOR currentQMV[3]; |
2125 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
2126 |
Data.currentMV = currentMV; |
Data.currentMV = currentMV; Data.currentQMV = currentQMV; |
2127 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2128 |
Data.lambda16 = lambda_vec16[frame->quant]; |
Data.lambda16 = lambda_vec16[frame->quant]; |
2129 |
|
Data.qpel = pParam->m_quarterpel; |
2130 |
|
|
2131 |
// note: i==horizontal, j==vertical |
if((qimage = (uint8_t *) malloc(32 * pParam->edged_width)) == NULL) |
2132 |
|
return; // allocate some mem for qpel interpolated blocks |
2133 |
|
// somehow this is dirty since I think we shouldn't use malloc outside |
2134 |
|
// encoder_create() - so please fix me! |
2135 |
|
Data.RefQ = qimage; |
2136 |
|
|
2137 |
|
// note: i==horizontal, j==vertical |
2138 |
for (j = 0; j < pParam->mb_height; j++) { |
for (j = 0; j < pParam->mb_height; j++) { |
2139 |
|
|
2140 |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
f_predMV = b_predMV = zeroMV; /* prediction is reset at left boundary */ |
2144 |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width; |
2145 |
|
|
2146 |
/* 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 */ |
2147 |
|
if (b_reference->coding_type != S_VOP) |
2148 |
if (b_mb->mode == MODE_NOT_CODED) { |
if (b_mb->mode == MODE_NOT_CODED) { |
2149 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
2150 |
continue; |
continue; |
2185 |
MODE_BACKWARD, &Data); |
MODE_BACKWARD, &Data); |
2186 |
|
|
2187 |
// 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 |
2188 |
|
/* |
2189 |
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, |
2190 |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
b_ref->y, b_refH->y, b_refV->y, b_refHV->y, |
2191 |
&frame->image, |
&frame->image, |
2196 |
&f_predMV, &b_predMV, |
&f_predMV, &b_predMV, |
2197 |
pMB, &best_sad, |
pMB, &best_sad, |
2198 |
&Data); |
&Data); |
2199 |
|
*/ |
2200 |
switch (pMB->mode) { |
switch (pMB->mode) { |
2201 |
case MODE_FORWARD: |
case MODE_FORWARD: |
2202 |
f_count++; |
f_count++; |
2203 |
f_predMV = pMB->mvs[0]; |
if (pParam->m_quarterpel) f_predMV = pMB->qmvs[0]; |
2204 |
|
else f_predMV = pMB->mvs[0]; |
2205 |
break; |
break; |
2206 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
2207 |
b_count++; |
b_count++; |
2208 |
b_predMV = pMB->b_mvs[0]; |
if (pParam->m_quarterpel) b_predMV = pMB->b_qmvs[0]; |
2209 |
|
else b_predMV = pMB->b_mvs[0]; |
2210 |
break; |
break; |
2211 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
2212 |
i_count++; |
i_count++; |
2213 |
|
if (pParam->m_quarterpel) { |
2214 |
|
f_predMV = pMB->qmvs[0]; |
2215 |
|
b_predMV = pMB->b_qmvs[0]; |
2216 |
|
} else { |
2217 |
f_predMV = pMB->mvs[0]; |
f_predMV = pMB->mvs[0]; |
2218 |
b_predMV = pMB->b_mvs[0]; |
b_predMV = pMB->b_mvs[0]; |
2219 |
|
} |
2220 |
break; |
break; |
2221 |
case MODE_DIRECT: |
case MODE_DIRECT: |
2222 |
case MODE_DIRECT_NO4V: |
case MODE_DIRECT_NO4V: |
2227 |
} |
} |
2228 |
} |
} |
2229 |
} |
} |
2230 |
|
free(qimage); |
2231 |
} |
} |
2232 |
|
|
2233 |
/* Hinted ME starts here */ |
/* Hinted ME starts here */ |
2337 |
int sum, dx, dy; |
int sum, dx, dy; |
2338 |
|
|
2339 |
if(pParam->m_quarterpel) |
if(pParam->m_quarterpel) |
2340 |
sum = (pMB->qmvs[0].y + pMB->qmvs[1].y + pMB->qmvs[2].y + pMB->qmvs[3].y)/2; |
sum = (pMB->qmvs[0].y/2 + pMB->qmvs[1].y/2 + pMB->qmvs[2].y/2 + pMB->qmvs[3].y/2); |
2341 |
else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
else sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
2342 |
dy = (sum ? SIGN(sum) * |
dy = (sum ? SIGN(sum) * |
2343 |
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
2344 |
|
|
2345 |
if(pParam->m_quarterpel) |
if(pParam->m_quarterpel) |
2346 |
sum = (pMB->qmvs[0].x + pMB->qmvs[1].x + pMB->qmvs[2].x + pMB->qmvs[3].x)/2; |
sum = (pMB->qmvs[0].x/2 + pMB->qmvs[1].x/2 + pMB->qmvs[2].x/2 + pMB->qmvs[3].x/2); |
2347 |
else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
else sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
2348 |
dx = (sum ? SIGN(sum) * |
dx = (sum ? SIGN(sum) * |
2349 |
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
2509 |
#define INTRA_THRESH 1350 |
#define INTRA_THRESH 1350 |
2510 |
#define INTER_THRESH 900 |
#define INTER_THRESH 900 |
2511 |
|
|
2512 |
|
|
2513 |
int |
int |
2514 |
MEanalysis( const IMAGE * const pRef, |
MEanalysis( const IMAGE * const pRef, |
2515 |
const IMAGE * const pCurrent, |
FRAMEINFO * const Current, |
2516 |
MBParam * const pParam, |
MBParam * const pParam, |
2517 |
MACROBLOCK * const pMBs, |
int maxIntra, //maximum number if non-I frames |
2518 |
const uint32_t iFcode) |
int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame |
2519 |
|
int bCount) // number if B frames in a row |
2520 |
{ |
{ |
2521 |
uint32_t x, y, intra = 0; |
uint32_t x, y, intra = 0; |
2522 |
int sSAD = 0; |
int sSAD = 0; |
2523 |
|
MACROBLOCK * const pMBs = Current->mbs; |
2524 |
|
const IMAGE * const pCurrent = &Current->image; |
2525 |
|
int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH; |
2526 |
|
|
2527 |
VECTOR currentMV; |
VECTOR currentMV; |
2528 |
int32_t iMinSAD; |
int32_t iMinSAD; |
2530 |
Data.iEdgedWidth = pParam->edged_width; |
Data.iEdgedWidth = pParam->edged_width; |
2531 |
Data.currentMV = ¤tMV; |
Data.currentMV = ¤tMV; |
2532 |
Data.iMinSAD = &iMinSAD; |
Data.iMinSAD = &iMinSAD; |
2533 |
Data.iFcode = iFcode; |
Data.iFcode = Current->fcode; |
2534 |
CheckCandidate = CheckCandidate16no4vI; |
CheckCandidate = CheckCandidate16no4vI; |
2535 |
|
|
2536 |
|
if (intraCount < 10) // we're right after an I frame |
2537 |
|
IntraThresh += 4 * (intraCount - 10) * (intraCount - 10); |
2538 |
|
else |
2539 |
|
if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec |
2540 |
|
IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra; |
2541 |
|
|
2542 |
|
|
2543 |
|
InterThresh += 300 * (1 - bCount); |
2544 |
|
if (InterThresh < 200) InterThresh = 200; |
2545 |
|
|
2546 |
if (sadInit) (*sadInit) (); |
if (sadInit) (*sadInit) (); |
2547 |
|
|
2548 |
for (y = 1; y < pParam->mb_height-1; y++) { |
for (y = 1; y < pParam->mb_height-1; y++) { |
2553 |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
sad = MEanalyzeMB(pRef->y, pCurrent->y, x, y, |
2554 |
pParam, pMBs, pMB, &Data); |
pParam, pMBs, pMB, &Data); |
2555 |
|
|
2556 |
if (sad > INTRA_THRESH) { |
if (sad > IntraThresh) { |
2557 |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
dev = dev16(pCurrent->y + (x + y * pParam->edged_width) * 16, |
2558 |
pParam->edged_width); |
pParam->edged_width); |
2559 |
if (dev + INTRA_THRESH < sad) { intra++; pMB->mode = MODE_INTRA; } |
if (dev + IntraThresh < sad) { |
2560 |
if (intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
pMB->mode = MODE_INTRA; |
2561 |
|
if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return 2; // I frame |
2562 |
|
} |
2563 |
} |
} |
2564 |
sSAD += sad; |
sSAD += sad; |
2565 |
} |
} |
2566 |
} |
} |
2567 |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2); |
2568 |
if (sSAD > INTER_THRESH ) return 1; //P frame |
if (sSAD > InterThresh ) return 1; //P frame |
2569 |
emms(); |
emms(); |
2570 |
return 0; // B frame |
return 0; // B frame |
2571 |
|
|