20 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: motion_comp.c,v 1.22 2004-10-12 21:08:41 edgomez Exp $ |
* $Id: motion_comp.c,v 1.23 2004-12-05 13:01:27 syskin Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
30 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
31 |
#include "../image/interpolate8x8.h" |
#include "../image/interpolate8x8.h" |
32 |
#include "../image/qpel.h" |
#include "../image/qpel.h" |
|
#include "../image/reduced.h" |
|
33 |
#include "../utils/timer.h" |
#include "../utils/timer.h" |
34 |
#include "motion.h" |
#include "motion.h" |
35 |
|
|
|
#ifndef RSHIFT |
|
|
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) |
|
|
#endif |
|
|
|
|
|
/* assume b>0 */ |
|
|
#ifndef RDIV |
|
|
#define RDIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) |
|
|
#endif |
|
|
|
|
|
|
|
|
/* This is borrowed from bitstream.c until we find a common solution */ |
|
|
|
|
|
static uint32_t __inline |
|
|
log2bin(uint32_t value) |
|
|
{ |
|
|
/* Changed by Chenm001 */ |
|
|
#if !defined(_MSC_VER) |
|
|
int n = 0; |
|
|
|
|
|
while (value) { |
|
|
value >>= 1; |
|
|
n++; |
|
|
} |
|
|
return n; |
|
|
#else |
|
|
__asm { |
|
|
bsr eax, value |
|
|
inc eax |
|
|
} |
|
|
#endif |
|
|
} |
|
|
|
|
36 |
/* |
/* |
37 |
* getref: calculate reference image pointer |
* getref: calculate reference image pointer |
38 |
* the decision to use interpolation h/v/hv or the normal image is |
* the decision to use interpolation h/v/hv or the normal image is |
77 |
const int32_t dy, |
const int32_t dy, |
78 |
const int32_t stride, |
const int32_t stride, |
79 |
const int quarterpel, |
const int quarterpel, |
|
const int reduced_resolution, |
|
80 |
const int32_t rounding) |
const int32_t rounding) |
81 |
{ |
{ |
82 |
const uint8_t * ptr; |
const uint8_t * ptr; |
83 |
|
|
|
if (!reduced_resolution) { |
|
84 |
|
|
85 |
if(quarterpel) { |
if(quarterpel) { |
86 |
if ((dx&3) | (dy&3)) { |
if ((dx&3) | (dy&3)) { |
101 |
transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8, |
transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8, |
102 |
ptr + 8*stride + 8, stride); |
ptr + 8*stride + 8, stride); |
103 |
|
|
|
} else { /* reduced_resolution */ |
|
|
|
|
|
x *= 2; y *= 2; |
|
|
|
|
|
ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
|
|
|
|
|
filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride); |
|
|
filter_diff_18x18_to_8x8(dct_codes, ptr, stride); |
|
|
|
|
|
filter_18x18_to_8x8(dct_codes+64, cur+y*stride + x + 16, stride); |
|
|
filter_diff_18x18_to_8x8(dct_codes+64, ptr + 16, stride); |
|
|
|
|
|
filter_18x18_to_8x8(dct_codes+128, cur+(y+16)*stride + x, stride); |
|
|
filter_diff_18x18_to_8x8(dct_codes+128, ptr + 16*stride, stride); |
|
|
|
|
|
filter_18x18_to_8x8(dct_codes+192, cur+(y+16)*stride + x + 16, stride); |
|
|
filter_diff_18x18_to_8x8(dct_codes+192, ptr + 16*stride + 16, stride); |
|
|
|
|
|
transfer32x32_copy(cur + y*stride + x, ptr, stride); |
|
|
} |
|
104 |
} |
} |
105 |
|
|
106 |
static __inline void |
static __inline void |
117 |
const int32_t dy, |
const int32_t dy, |
118 |
const int32_t stride, |
const int32_t stride, |
119 |
const int32_t quarterpel, |
const int32_t quarterpel, |
|
const int reduced_resolution, |
|
120 |
const int32_t rounding) |
const int32_t rounding) |
121 |
{ |
{ |
122 |
const uint8_t * ptr; |
const uint8_t * ptr; |
123 |
|
|
|
if (!reduced_resolution) { |
|
|
|
|
124 |
if(quarterpel) { |
if(quarterpel) { |
125 |
if ((dx&3) | (dy&3)) { |
if ((dx&3) | (dy&3)) { |
126 |
interpolate8x8_quarterpel(tmp - y*stride - x, |
interpolate8x8_quarterpel(tmp - y*stride - x, |
131 |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
132 |
|
|
133 |
transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride); |
transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride); |
|
|
|
|
} else { /* reduced_resolution */ |
|
|
|
|
|
x *= 2; y *= 2; |
|
|
|
|
|
ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
|
|
|
|
|
filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride); |
|
|
filter_diff_18x18_to_8x8(dct_codes, ptr, stride); |
|
|
|
|
|
transfer16x16_copy(cur + y*stride + x, ptr, stride); |
|
|
} |
|
134 |
} |
} |
135 |
|
|
|
/* XXX: slow, inelegant... */ |
|
|
static void |
|
|
interpolate18x18_switch(uint8_t * const cur, |
|
|
const uint8_t * const refn, |
|
|
const uint32_t x, |
|
|
const uint32_t y, |
|
|
const int32_t dx, |
|
|
const int dy, |
|
|
const int32_t stride, |
|
|
const int32_t rounding) |
|
|
{ |
|
|
interpolate8x8_switch(cur, refn, x-1, y-1, dx, dy, stride, rounding); |
|
|
interpolate8x8_switch(cur, refn, x+7, y-1, dx, dy, stride, rounding); |
|
|
interpolate8x8_switch(cur, refn, x+9, y-1, dx, dy, stride, rounding); |
|
|
|
|
|
interpolate8x8_switch(cur, refn, x-1, y+7, dx, dy, stride, rounding); |
|
|
interpolate8x8_switch(cur, refn, x+7, y+7, dx, dy, stride, rounding); |
|
|
interpolate8x8_switch(cur, refn, x+9, y+7, dx, dy, stride, rounding); |
|
|
|
|
|
interpolate8x8_switch(cur, refn, x-1, y+9, dx, dy, stride, rounding); |
|
|
interpolate8x8_switch(cur, refn, x+7, y+9, dx, dy, stride, rounding); |
|
|
interpolate8x8_switch(cur, refn, x+9, y+9, dx, dy, stride, rounding); |
|
|
} |
|
136 |
|
|
137 |
static void |
static void |
138 |
CompensateChroma( int dx, int dy, |
CompensateChroma( int dx, int dy, |
142 |
uint8_t * const temp, |
uint8_t * const temp, |
143 |
int16_t * const coeff, |
int16_t * const coeff, |
144 |
const int32_t stride, |
const int32_t stride, |
145 |
const int rounding, |
const int rounding) |
|
const int rrv) |
|
146 |
{ /* uv-block-based compensation */ |
{ /* uv-block-based compensation */ |
147 |
|
|
|
if (!rrv) { |
|
148 |
transfer_8to16sub(coeff, Cur->u + 8 * j * stride + 8 * i, |
transfer_8to16sub(coeff, Cur->u + 8 * j * stride + 8 * i, |
149 |
interpolate8x8_switch2(temp, Ref->u, 8 * i, 8 * j, |
interpolate8x8_switch2(temp, Ref->u, 8 * i, 8 * j, |
150 |
dx, dy, stride, rounding), |
dx, dy, stride, rounding), |
153 |
interpolate8x8_switch2(temp, Ref->v, 8 * i, 8 * j, |
interpolate8x8_switch2(temp, Ref->v, 8 * i, 8 * j, |
154 |
dx, dy, stride, rounding), |
dx, dy, stride, rounding), |
155 |
stride); |
stride); |
|
} else { |
|
|
uint8_t * current, * reference; |
|
|
|
|
|
current = Cur->u + 16*j*stride + 16*i; |
|
|
reference = temp - 16*j*stride - 16*i; |
|
|
interpolate18x18_switch(reference, Ref->u, 16*i, 16*j, dx, dy, stride, rounding); |
|
|
filter_18x18_to_8x8(coeff, current, stride); |
|
|
filter_diff_18x18_to_8x8(coeff, temp, stride); |
|
|
transfer16x16_copy(current, temp, stride); |
|
|
|
|
|
current = Cur->v + 16*j*stride + 16*i; |
|
|
interpolate18x18_switch(reference, Ref->v, 16*i, 16*j, dx, dy, stride, rounding); |
|
|
filter_18x18_to_8x8(coeff + 64, current, stride); |
|
|
filter_diff_18x18_to_8x8(coeff + 64, temp, stride); |
|
|
transfer16x16_copy(current, temp, stride); |
|
|
} |
|
156 |
} |
} |
157 |
|
|
158 |
void |
void |
170 |
const uint32_t height, |
const uint32_t height, |
171 |
const uint32_t edged_width, |
const uint32_t edged_width, |
172 |
const int32_t quarterpel, |
const int32_t quarterpel, |
|
const int reduced_resolution, |
|
173 |
const int32_t rounding) |
const int32_t rounding) |
174 |
{ |
{ |
175 |
int32_t dx; |
int32_t dx; |
177 |
|
|
178 |
uint8_t * const tmp = refv->u; |
uint8_t * const tmp = refv->u; |
179 |
|
|
180 |
if ( (!reduced_resolution) && (mb->mode == MODE_NOT_CODED) ) { /* quick copy for early SKIP */ |
if (mb->mode == MODE_NOT_CODED) { /* quick copy for early SKIP */ |
181 |
/* early SKIP is only activated in P-VOPs, not in S-VOPs, so mcsel can never be 1 */ |
/* early SKIP is only activated in P-VOPs, not in S-VOPs, so mcsel can never be 1 */ |
182 |
|
|
183 |
transfer16x16_copy(cur->y + 16 * (i + j * edged_width), |
transfer16x16_copy(cur->y + 16 * (i + j * edged_width), |
196 |
if ((mb->mode == MODE_NOT_CODED || mb->mode == MODE_INTER |
if ((mb->mode == MODE_NOT_CODED || mb->mode == MODE_INTER |
197 |
|| mb->mode == MODE_INTER_Q)) { |
|| mb->mode == MODE_INTER_Q)) { |
198 |
|
|
|
/* reduced resolution + GMC: not possible */ |
|
|
|
|
199 |
if (mb->mcsel) { |
if (mb->mcsel) { |
200 |
|
|
201 |
/* call normal routine once, easier than "if (mcsel)"ing all the time */ |
/* call normal routine once, easier than "if (mcsel)"ing all the time */ |
209 |
transfer_8to16sub(&dct_codes[3*64], cur->y + (16*j+8)*edged_width + 16*i+8, |
transfer_8to16sub(&dct_codes[3*64], cur->y + (16*j+8)*edged_width + 16*i+8, |
210 |
refGMC->y + (16*j+8)*edged_width + 16*i+8, edged_width); |
refGMC->y + (16*j+8)*edged_width + 16*i+8, edged_width); |
211 |
|
|
|
/* lumi is needed earlier for mode decision, but chroma should be done block-based, but it isn't, yet. */ |
|
|
|
|
212 |
transfer_8to16sub(&dct_codes[4 * 64], cur->u + 8 *j*edged_width/2 + 8*i, |
transfer_8to16sub(&dct_codes[4 * 64], cur->u + 8 *j*edged_width/2 + 8*i, |
213 |
refGMC->u + 8 *j*edged_width/2 + 8*i, edged_width/2); |
refGMC->u + 8 *j*edged_width/2 + 8*i, edged_width/2); |
214 |
|
|
223 |
dx = (quarterpel ? mb->qmvs[0].x : mb->mvs[0].x); |
dx = (quarterpel ? mb->qmvs[0].x : mb->mvs[0].x); |
224 |
dy = (quarterpel ? mb->qmvs[0].y : mb->mvs[0].y); |
dy = (quarterpel ? mb->qmvs[0].y : mb->mvs[0].y); |
225 |
|
|
|
if (reduced_resolution) { |
|
|
dx = RRV_MV_SCALEUP(dx); |
|
|
dy = RRV_MV_SCALEUP(dy); |
|
|
} |
|
|
|
|
226 |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, ref->y, refh->y, |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, ref->y, refh->y, |
227 |
refv->y, refhv->y, tmp, 16 * i, 16 * j, dx, dy, |
refv->y, refhv->y, tmp, 16 * i, 16 * j, dx, dy, |
228 |
edged_width, quarterpel, reduced_resolution, rounding); |
edged_width, quarterpel, rounding); |
229 |
|
|
230 |
if (quarterpel) { dx /= 2; dy /= 2; } |
if (quarterpel) { dx /= 2; dy /= 2; } |
231 |
|
|
242 |
sumx += quarterpel ? dx/2 : dx; |
sumx += quarterpel ? dx/2 : dx; |
243 |
sumy += quarterpel ? dy/2 : dy; |
sumy += quarterpel ? dy/2 : dy; |
244 |
|
|
|
if (reduced_resolution){ |
|
|
dx = RRV_MV_SCALEUP(dx); |
|
|
dy = RRV_MV_SCALEUP(dy); |
|
|
} |
|
|
|
|
245 |
compensate8x8_interpolate(&dct_codes[k * 64], cur->y, ref->y, refh->y, |
compensate8x8_interpolate(&dct_codes[k * 64], cur->y, ref->y, refh->y, |
246 |
refv->y, refhv->y, tmp, 16 * i + 8*(k&1), 16 * j + 8*(k>>1), dx, |
refv->y, refhv->y, tmp, 16 * i + 8*(k&1), 16 * j + 8*(k>>1), dx, |
247 |
dy, edged_width, quarterpel, reduced_resolution, rounding); |
dy, edged_width, quarterpel, rounding); |
248 |
} |
} |
249 |
dx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
dx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
250 |
dy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
dy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
251 |
} |
} |
252 |
|
|
253 |
CompensateChroma(dx, dy, i, j, cur, ref, tmp, |
CompensateChroma(dx, dy, i, j, cur, ref, tmp, |
254 |
&dct_codes[4 * 64], edged_width / 2, rounding, reduced_resolution); |
&dct_codes[4 * 64], edged_width / 2, rounding); |
255 |
} |
} |
256 |
|
|
257 |
|
|
286 |
|
|
287 |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, f_ref->y, f_refh->y, |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, f_ref->y, f_refh->y, |
288 |
f_refv->y, f_refhv->y, tmp, 16 * i, 16 * j, dx, |
f_refv->y, f_refhv->y, tmp, 16 * i, 16 * j, dx, |
289 |
dy, edged_width, quarterpel, 0, 0); |
dy, edged_width, quarterpel, 0); |
290 |
|
|
291 |
if (quarterpel) { dx /= 2; dy /= 2; } |
if (quarterpel) { dx /= 2; dy /= 2; } |
292 |
|
|
293 |
CompensateChroma( (dx >> 1) + roundtab_79[dx & 0x3], |
CompensateChroma( (dx >> 1) + roundtab_79[dx & 0x3], |
294 |
(dy >> 1) + roundtab_79[dy & 0x3], |
(dy >> 1) + roundtab_79[dy & 0x3], |
295 |
i, j, cur, f_ref, tmp, |
i, j, cur, f_ref, tmp, |
296 |
&dct_codes[4 * 64], edged_width / 2, 0, 0); |
&dct_codes[4 * 64], edged_width / 2, 0); |
297 |
|
|
298 |
return; |
return; |
299 |
|
|
302 |
|
|
303 |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, b_ref->y, b_refh->y, |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, b_ref->y, b_refh->y, |
304 |
b_refv->y, b_refhv->y, tmp, 16 * i, 16 * j, b_dx, |
b_refv->y, b_refhv->y, tmp, 16 * i, 16 * j, b_dx, |
305 |
b_dy, edged_width, quarterpel, 0, 0); |
b_dy, edged_width, quarterpel, 0); |
306 |
|
|
307 |
if (quarterpel) { b_dx /= 2; b_dy /= 2; } |
if (quarterpel) { b_dx /= 2; b_dy /= 2; } |
308 |
|
|
309 |
CompensateChroma( (b_dx >> 1) + roundtab_79[b_dx & 0x3], |
CompensateChroma( (b_dx >> 1) + roundtab_79[b_dx & 0x3], |
310 |
(b_dy >> 1) + roundtab_79[b_dy & 0x3], |
(b_dy >> 1) + roundtab_79[b_dy & 0x3], |
311 |
i, j, cur, b_ref, tmp, |
i, j, cur, b_ref, tmp, |
312 |
&dct_codes[4 * 64], edged_width / 2, 0, 0); |
&dct_codes[4 * 64], edged_width / 2, 0); |
313 |
|
|
314 |
return; |
return; |
315 |
|
|
316 |
case MODE_INTERPOLATE: /* _could_ use DIRECT, but would be overkill (no 4MV there) */ |
case MODE_INTERPOLATE: |
317 |
case MODE_DIRECT_NO4V: |
case MODE_DIRECT_NO4V: |
318 |
dx = fmvs->x; dy = fmvs->y; |
dx = fmvs->x; dy = fmvs->y; |
319 |
b_dx = bmvs->x; b_dy = bmvs->y; |
b_dx = bmvs->x; b_dy = bmvs->y; |
411 |
break; |
break; |
412 |
} |
} |
413 |
|
|
414 |
/* v block-based chroma interpolation for direct and interpolate modes */ |
/* block-based chroma interpolation for direct and interpolate modes */ |
415 |
transfer_8to16sub2(&dct_codes[4 * 64], |
transfer_8to16sub2(&dct_codes[4 * 64], |
416 |
cur->u + (j * 8) * edged_width / 2 + (i * 8), |
cur->u + (j * 8) * edged_width / 2 + (i * 8), |
417 |
interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j, |
interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j, |