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