| 1 |
/* |
| 2 |
* motion.c: Motion compensation code |
| 3 |
* |
| 4 |
* Written by: Ullrich Hafner |
| 5 |
* Michael Unger |
| 6 |
* |
| 7 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 8 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* $Date: 2000/06/14 20:50:51 $ |
| 13 |
* $Author: hafner $ |
| 14 |
* $Revision: 5.1 $ |
| 15 |
* $State: Exp $ |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "pm_c_util.h" |
| 23 |
|
| 24 |
#include "types.h" |
| 25 |
#include "macros.h" |
| 26 |
#include "error.h" |
| 27 |
|
| 28 |
#include "image.h" |
| 29 |
#include "misc.h" |
| 30 |
#include "motion.h" |
| 31 |
|
| 32 |
/***************************************************************************** |
| 33 |
|
| 34 |
public code |
| 35 |
|
| 36 |
*****************************************************************************/ |
| 37 |
|
| 38 |
void |
| 39 |
restore_mc (int enlarge_factor, image_t *image, const image_t *past, |
| 40 |
const image_t *future, const wfa_t *wfa) |
| 41 |
/* |
| 42 |
* Restore motion compensated prediction of 'image' represented by 'wfa'. |
| 43 |
* If 'enlarge_factor' != 0 then enlarge image by given amount. |
| 44 |
* Reference frames are given by 'past' and 'future'. |
| 45 |
* |
| 46 |
* No return values. |
| 47 |
*/ |
| 48 |
{ |
| 49 |
unsigned state, label; |
| 50 |
unsigned root_state; |
| 51 |
word_t *mcblock1, *mcblock2; /* MC blocks */ |
| 52 |
|
| 53 |
#define FX(v) ((image->format == FORMAT_4_2_0) && band != Y ? ((v) / 2) : v) |
| 54 |
|
| 55 |
mcblock1 = Calloc (size_of_level (MAX((int) wfa->wfainfo->p_max_level |
| 56 |
+ 2 * enlarge_factor, 0)), |
| 57 |
sizeof (word_t)); |
| 58 |
mcblock2 = Calloc (size_of_level (MAX((int) wfa->wfainfo->p_max_level |
| 59 |
+ 2 * enlarge_factor, 0)), |
| 60 |
sizeof (word_t)); |
| 61 |
|
| 62 |
if (!image->color) |
| 63 |
root_state = wfa->root_state; |
| 64 |
else |
| 65 |
root_state = wfa->tree [wfa->tree [wfa->root_state][0]][0]; |
| 66 |
|
| 67 |
for (state = wfa->basis_states; state <= root_state; state++) |
| 68 |
for (label = 0; label < MAXLABELS; label++) |
| 69 |
if (wfa->mv_tree[state][label].type != NONE) |
| 70 |
{ |
| 71 |
color_e band; |
| 72 |
unsigned level = wfa->level_of_state [state] - 1; |
| 73 |
unsigned width = width_of_level (level); |
| 74 |
unsigned height = height_of_level (level); |
| 75 |
unsigned offset = image->width - width; |
| 76 |
|
| 77 |
switch (wfa->mv_tree [state][label].type) |
| 78 |
{ |
| 79 |
case FORWARD: |
| 80 |
for (band = first_band (image->color); |
| 81 |
band <= last_band (image->color); band++) |
| 82 |
{ |
| 83 |
extract_mc_block (mcblock1, FX (width), FX (height), |
| 84 |
past->pixels [band], FX (past->width), |
| 85 |
wfa->wfainfo->half_pixel, |
| 86 |
FX (wfa->x [state][label]), |
| 87 |
FX (wfa->y [state][label]), |
| 88 |
FX (wfa->mv_tree [state][label].fx), |
| 89 |
FX (wfa->mv_tree [state][label].fy)); |
| 90 |
{ |
| 91 |
word_t *mc1; /* current pixel in MC block */ |
| 92 |
word_t *orig; /* current pixel in original image */ |
| 93 |
unsigned x, y; /* pixel coordinates */ |
| 94 |
|
| 95 |
mc1 = mcblock1; |
| 96 |
orig = (word_t *) image->pixels [band] |
| 97 |
+ FX (wfa->x[state][label]) |
| 98 |
+ FX (wfa->y[state][label]) * FX (image->width); |
| 99 |
|
| 100 |
for (y = FX (height); y; y--) |
| 101 |
{ |
| 102 |
for (x = FX (width); x; x--) |
| 103 |
*orig++ += *mc1++; |
| 104 |
|
| 105 |
orig += FX (offset); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
break; |
| 110 |
case BACKWARD: |
| 111 |
for (band = first_band (image->color); |
| 112 |
band <= last_band (image->color); band++) |
| 113 |
{ |
| 114 |
extract_mc_block (mcblock1, FX (width), FX (height), |
| 115 |
future->pixels [band], |
| 116 |
FX (future->width), |
| 117 |
wfa->wfainfo->half_pixel, |
| 118 |
FX (wfa->x [state][label]), |
| 119 |
FX (wfa->y [state][label]), |
| 120 |
FX (wfa->mv_tree [state][label].bx), |
| 121 |
FX (wfa->mv_tree [state][label].by)); |
| 122 |
{ |
| 123 |
word_t *mc1; /* current pixel in MC block 1 */ |
| 124 |
word_t *orig; /* current pixel in original image */ |
| 125 |
unsigned x, y; /* pixel coordinates */ |
| 126 |
|
| 127 |
mc1 = mcblock1; |
| 128 |
orig = (word_t *) image->pixels [band] |
| 129 |
+ FX (wfa->x[state][label]) |
| 130 |
+ FX (wfa->y[state][label]) * FX (image->width); |
| 131 |
|
| 132 |
for (y = FX (height); y; y--) |
| 133 |
{ |
| 134 |
for (x = FX (width); x; x--) |
| 135 |
*orig++ += *mc1++; |
| 136 |
|
| 137 |
orig += FX (offset); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
break; |
| 142 |
case INTERPOLATED: |
| 143 |
for (band = first_band (image->color); |
| 144 |
band <= last_band (image->color); band++) |
| 145 |
{ |
| 146 |
extract_mc_block (mcblock1, FX (width), FX (height), |
| 147 |
past->pixels [band], FX (past->width), |
| 148 |
wfa->wfainfo->half_pixel, |
| 149 |
FX (wfa->x[state][label]), |
| 150 |
FX (wfa->y[state][label]), |
| 151 |
FX (wfa->mv_tree[state][label].fx), |
| 152 |
FX (wfa->mv_tree[state][label].fy)); |
| 153 |
extract_mc_block (mcblock2, FX (width), FX (height), |
| 154 |
future->pixels [band], |
| 155 |
FX (future->width), |
| 156 |
wfa->wfainfo->half_pixel, |
| 157 |
FX (wfa->x[state][label]), |
| 158 |
FX (wfa->y[state][label]), |
| 159 |
FX (wfa->mv_tree[state][label].bx), |
| 160 |
FX (wfa->mv_tree[state][label].by)); |
| 161 |
{ |
| 162 |
word_t *mc1; /* current pixel in MC block 1 */ |
| 163 |
word_t *mc2; /* current pixel in MC block 1 */ |
| 164 |
word_t *orig; /* current pixel in original image */ |
| 165 |
unsigned x, y; /* pixel coordinates */ |
| 166 |
|
| 167 |
mc1 = mcblock1; |
| 168 |
mc2 = mcblock2; |
| 169 |
orig = (word_t *) image->pixels [band] |
| 170 |
+ FX (wfa->x[state][label]) |
| 171 |
+ FX (wfa->y[state][label]) * FX (image->width); |
| 172 |
|
| 173 |
for (y = FX (height); y; y--) |
| 174 |
{ |
| 175 |
for (x = FX (width); x; x--) |
| 176 |
#ifdef HAVE_SIGNED_SHIFT |
| 177 |
*orig++ += (*mc1++ + *mc2++) >> 1; |
| 178 |
#else /* not HAVE_SIGNED_SHIFT */ |
| 179 |
*orig++ += (*mc1++ + *mc2++) / 2; |
| 180 |
#endif /* not HAVE_SIGNED_SHIFT */ |
| 181 |
|
| 182 |
orig += FX (offset); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
break; |
| 187 |
default: |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if (image->color) |
| 193 |
{ |
| 194 |
unsigned n; |
| 195 |
word_t *ptr; |
| 196 |
static int *clipping = NULL; |
| 197 |
unsigned shift = image->format == FORMAT_4_2_0 ? 2 : 0; |
| 198 |
|
| 199 |
if (!clipping) /* initialize clipping table */ |
| 200 |
{ |
| 201 |
int i; |
| 202 |
|
| 203 |
clipping = Calloc (256 * 3, sizeof (int)); |
| 204 |
for (i = -128; i < 128; i++) |
| 205 |
clipping [256 + i + 128] = i; |
| 206 |
for (i = 0; i < 256; i++) |
| 207 |
clipping [i] = clipping [256]; |
| 208 |
for (i = 512; i < 512 + 256; i++) |
| 209 |
clipping [i] = clipping [511]; |
| 210 |
clipping += 256 + 128; |
| 211 |
} |
| 212 |
|
| 213 |
ptr = image->pixels [Cb]; |
| 214 |
for (n = (image->width * image->height) >> shift; n; n--, ptr++) |
| 215 |
#ifdef HAVE_SIGNED_SHIFT |
| 216 |
*ptr = clipping [*ptr >> 4] << 4; |
| 217 |
#else /* not HAVE_SIGNED_SHIFT */ |
| 218 |
*ptr = clipping [*ptr / 16] * 16; |
| 219 |
#endif /* not HAVE_SIGNED_SHIFT */ |
| 220 |
ptr = image->pixels [Cr]; |
| 221 |
for (n = (image->width * image->height) >> shift; n; n--, ptr++) |
| 222 |
#ifdef HAVE_SIGNED_SHIFT |
| 223 |
*ptr = clipping [*ptr >> 4] << 4; |
| 224 |
#else /* not HAVE_SIGNED_SHIFT */ |
| 225 |
*ptr = clipping [*ptr / 16] * 16; |
| 226 |
#endif /* not HAVE_SIGNED_SHIFT */ |
| 227 |
} |
| 228 |
|
| 229 |
Free (mcblock1); |
| 230 |
Free (mcblock2); |
| 231 |
} |
| 232 |
|
| 233 |
void |
| 234 |
extract_mc_block (word_t *mcblock, unsigned width, unsigned height, |
| 235 |
const word_t *reference, unsigned ref_width, |
| 236 |
bool_t half_pixel, unsigned xo, unsigned yo, |
| 237 |
unsigned mx, unsigned my) |
| 238 |
/* |
| 239 |
* Extract motion compensation image 'mcblock' of size 'width'x'height' |
| 240 |
* from 'reference' image (width is given by 'ref_width'). |
| 241 |
* Coordinates of reference block are given by ('xo' + 'mx', 'yo' + 'my'). |
| 242 |
* Use 'half_pixel' precision if specified. |
| 243 |
* |
| 244 |
* No return value. |
| 245 |
* |
| 246 |
* Side effects: |
| 247 |
* 'mcblock[]' MCPE block is filled with reference pixels |
| 248 |
*/ |
| 249 |
{ |
| 250 |
if (!half_pixel) /* Fullpixel precision */ |
| 251 |
{ |
| 252 |
const word_t *rblock; /* pointer to reference image */ |
| 253 |
unsigned y; /* current row */ |
| 254 |
|
| 255 |
rblock = reference + (yo + my) * ref_width + (xo + mx); |
| 256 |
for (y = height; y; y--) |
| 257 |
{ |
| 258 |
memcpy (mcblock, rblock, width * sizeof (word_t)); |
| 259 |
|
| 260 |
mcblock += width; |
| 261 |
rblock += ref_width; |
| 262 |
} |
| 263 |
} |
| 264 |
else /* Halfpixel precision */ |
| 265 |
{ |
| 266 |
unsigned x, y; /* current coordinates */ |
| 267 |
unsigned offset; /* remaining pixels in row */ |
| 268 |
const word_t *rblock; /* pointer to reference image */ |
| 269 |
const word_t *ryblock; /* pointer to next line */ |
| 270 |
const word_t *rxblock; /* pointer to next column */ |
| 271 |
const word_t *rxyblock; /* pointer to next column & row */ |
| 272 |
|
| 273 |
rblock = reference + (yo + my / 2) * ref_width + (xo + mx / 2); |
| 274 |
ryblock = rblock + ref_width; /* pixel in next row */ |
| 275 |
rxblock = rblock + 1; /* pixel in next column */ |
| 276 |
rxyblock = ryblock + 1; /* pixel in next row & column */ |
| 277 |
offset = ref_width - width; |
| 278 |
|
| 279 |
if ((mx & 1) == 0) |
| 280 |
{ |
| 281 |
if ((my & 1) == 0) /* Don't use halfpixel refinement */ |
| 282 |
for (y = height; y; y--) |
| 283 |
{ |
| 284 |
memcpy (mcblock, rblock, width * sizeof (word_t)); |
| 285 |
|
| 286 |
mcblock += width; |
| 287 |
rblock += ref_width; |
| 288 |
} |
| 289 |
else /* Halfpixel in y direction */ |
| 290 |
for (y = height; y; y--) |
| 291 |
{ |
| 292 |
for (x = width; x; x--) |
| 293 |
#ifdef HAVE_SIGNED_SHIFT |
| 294 |
*mcblock++ = (*rblock++ + *ryblock++) >> 1; |
| 295 |
#else /* not HAVE_SIGNED_SHIFT */ |
| 296 |
*mcblock++ = (*rblock++ + *ryblock++) / 2; |
| 297 |
#endif /* not HAVE_SIGNED_SHIFT */ |
| 298 |
|
| 299 |
rblock += offset; |
| 300 |
ryblock += offset; |
| 301 |
} |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
if ((my & 1) == 0) /* Halfpixel in x direction */ |
| 306 |
for (y = height; y; y--) |
| 307 |
{ |
| 308 |
for (x = width; x; x--) |
| 309 |
#ifdef HAVE_SIGNED_SHIFT |
| 310 |
*mcblock++ = (*rblock++ + *rxblock++) >> 1; |
| 311 |
#else /* not HAVE_SIGNED_SHIFT */ |
| 312 |
*mcblock++ = (*rblock++ + *rxblock++) / 2; |
| 313 |
#endif /* not HAVE_SIGNED_SHIFT */ |
| 314 |
|
| 315 |
rblock += offset; |
| 316 |
rxblock += offset; |
| 317 |
} |
| 318 |
else /* Halfpixel in xy direction */ |
| 319 |
for (y = height; y; y--) |
| 320 |
{ |
| 321 |
for (x = width; x; x--) |
| 322 |
#ifdef HAVE_SIGNED_SHIFT |
| 323 |
*mcblock++ = (*rblock++ + *rxblock++ |
| 324 |
+ *ryblock++ + *rxyblock++) >> 2; |
| 325 |
#else /* not HAVE_SIGNED_SHIFT */ |
| 326 |
*mcblock++ = (*rblock++ + *rxblock++ |
| 327 |
+ *ryblock++ + *rxyblock++) / 4; |
| 328 |
#endif /* not HAVE_SIGNED_SHIFT */ |
| 329 |
rblock += offset; |
| 330 |
ryblock += offset; |
| 331 |
rxblock += offset; |
| 332 |
rxyblock += offset; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |