[XviD-devel] Rounding

Christoph Lampert xvid-devel@xvid.org
Sun, 13 Oct 2002 23:33:56 +0200 (CEST)


Hi,


I've been looking at rounding of motion vectors half the day. It's done in
3 different ways through the code. :( And I don't even think it's right...

I keep reading code like this: 

uv_dy =  (uv_dy ? SIGN(uv_dy) *
	 (roundtab8[ABS(uv_dy) % 8] + (ABS(uv_dy) / 8) * 2) : 0);


which is supposed to round from 8th-pel to halfpel. Result is symmetric 
to 0, however, it's not similar to anything I read in the Standard. So
where is it from? 

Also, why the complicated expression using ? and % operator? 

My impression is that a simple & and >> should be sufficient 
(if we assume that negative values are shifted in the logical way). 

uv_dy = roundtab8[uv_dy & 0x7] + (uv_dy >> 3) * 2;

and similar fof 16th-pel -> halfpel with 

uv_dy = roundtab16[uv_dy & 0xF] + (uv_dy >> 4) * 2;

For 16th-Pel this is the same, thought much less complicatedly put.
FOr 8th-pel there's a difference, and even though it's not large, the 
PSNR gets slightly little higher in average and I believe it's what the
standard demands. 

Or am I wrong here? 


8th->half

UV_DY     old           roundtab8

dy=-15    res=-3        res2=-4
dy=-14    res=-3        res2=-3
dy=-13    res=-3        res2=-3
dy=-12    res=-3        res2=-3
dy=-11    res=-3        res2=-3
dy=-10    res=-2        res2=-3
dy=-9     res=-2        res2=-2
dy=-8     res=-2        res2=-2
dy=-7     res=-1        res2=-2
dy=-6     res=-1        res2=-1
dy=-5     res=-1        res2=-1
dy=-4     res=-1        res2=-1
dy=-3     res=-1        res2=-1
dy=-2     res= 0        res2=-1
dy=-1     res= 0        res2= 0
dy= 0     res= 0        res2= 0
dy= 1     res= 0        res2= 0
dy= 2     res= 0        res2= 1
dy= 3     res= 1        res2= 1
dy= 4     res= 1        res2= 1
dy= 5     res= 1        res2= 1
dy= 6     res= 1        res2= 1
dy= 7     res= 1        res2= 2
dy= 8     res= 2        res2= 2
dy= 9     res= 2        res2= 2
dy=10     res= 2        res2= 3
dy=11     res= 3        res2= 3
dy=12     res= 3        res2= 3
dy=13     res= 3        res2= 3
dy=14     res= 3        res2= 3
dy=15     res= 3        res2= 4