1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
19 ------------------------------------------------------------------------------
20 INPUT AND OUTPUT DEFINITIONS
23 xpos = x half-pixel of (x,y) coordinates within a VOP; motion
24 compensated coordinates; native data type
25 ypos = y half-pixel of (x,y) coordinates within a VOP; motion
26 compensated coordinates; native data type
27 comp = pointer to 8-bit compensated prediction values within a VOP;
28 computed by this module (i/o); full-pel resolution; 8-bit data
29 c_prev = pointer to previous 8-bit prediction values within a VOP;
30 values range from (0-255); full-pel resolution; 8-bit data
31 sh_d = pointer to residual values used to compensate the predicted
32 value; values range from (-512 to 511); full-pel resolution;
34 width = width of the VOP in pixels (x axis); full-pel resolution;
36 height = height of the VOP in pixels (y axis); full-pel resolution;
38 rnd1 = rounding value for case when one dimension uses half-pel
39 resolution; native data type
40 rnd2 = rounding value for case when two dimensions uses half-pel
41 resolution; native data type
46 Local Stores/Buffers/Pointers Needed:
49 Global Stores/Buffers/Pointers Needed:
52 Pointers and Buffers Modified:
53 comp = buffer contains newly computed compensated prediction values
55 Local Stores Modified:
58 Global Stores Modified:
61 ------------------------------------------------------------------------------
66 This function performs motion compensated prediction for the case where
67 the motion vector points to a block outside the VOP. The function interpolates
68 the pixels that are outside the VOP using the boundary pixels for the block.
69 Once the values are interpolated, the pixel values are computed for a block
70 in the current VOP. The prediction values are generated by averaging pixel
71 values in the previous VOP; the block position in the previous frame is
72 computed from the current block's motion vector. The computed pixel values
73 are calculated by adding the prediction values to the block residual values.
77 First, this functions determines which VOP boundary(ies) the motion vector
78 is outside, i.e., left, right, top, bottom. xpos is compared to the left and
79 right boundaries; ypos is compared to the top and bottom boundaries. The number
80 of block pixels inside the the boundary in the x and y directions are stored
81 in endx and endy, respectively. If the entire block is inside the x or y
82 boundary, the respectively end is set to 0.
84 After the boundaries are tested, any pixels lying outside a boundary are
85 interpolated from the boundary pixels. For example, if the block is outside the
86 bottom boundary, boundary pixels alone the bottom of the VOP as used to
87 interpolated those pixels lying outside the bottom boundary. The interpolation
88 used is a simple column-wise or row-wise copy of the boundary pixels (inside the
89 block) depending on which boundary the block is outside. In our example, each
90 boundary pixel would be copied column-wise to the pixel beneath it. If the
91 block was outside right boundary, the boundary pixels would be copied row-wise
92 to the pixel to the right of it. If the block was outside both an x and y
93 boundary, the boundary pixels would be copied row-wise for the portion of the
94 block outside the x boundary, and column-wise for the portion of the block
95 outside the y boundary. And so on.
97 Once the pixel interpolation is complete, the motion compensated output values
98 (comp[]) are calculed from the motion compensated prediction (pred[])values and
99 the residual values (sh_d[]) of the current frame. The prediction values are
100 generated by averaging pixel values in the previous VOP; the block position in
101 the previous frame is computed from the current block's motion vector. The
102 computed pixel values are calculated by adding the prediction values to the
103 block residual values.
107 /*----------------------------------------------------------------------------
109 ----------------------------------------------------------------------------*/
110 #include "mp4dec_lib.h"
111 #include "motion_comp.h"
113 #define PAD_CORNER { temp = *prev; \
115 temp |= (temp<<16); \
116 *((uint32*)ptr) = temp; \
117 *((uint32*)(ptr+4)) = temp; \
118 *((uint32*)(ptr+=16)) = temp; \
119 *((uint32*)(ptr+4)) = temp; \
120 *((uint32*)(ptr+=16)) = temp; \
121 *((uint32*)(ptr+4)) = temp; \
122 *((uint32*)(ptr+=16)) = temp; \
123 *((uint32*)(ptr+4)) = temp; \
124 *((uint32*)(ptr+=16)) = temp; \
125 *((uint32*)(ptr+4)) = temp; \
126 *((uint32*)(ptr+=16)) = temp; \
127 *((uint32*)(ptr+4)) = temp; \
128 *((uint32*)(ptr+=16)) = temp; \
129 *((uint32*)(ptr+4)) = temp; \
130 *((uint32*)(ptr+=16)) = temp; \
131 *((uint32*)(ptr+4)) = temp; }
133 #define PAD_ROW { temp = *((uint32*)prev); \
134 temp2 = *((uint32*)(prev+4)); \
135 *((uint32*)ptr) = temp;\
136 *((uint32*)(ptr+4)) = temp2; \
137 *((uint32*)(ptr+=16)) = temp; \
138 *((uint32*)(ptr+4)) = temp2;\
139 *((uint32*)(ptr+=16)) = temp; \
140 *((uint32*)(ptr+4)) = temp2;\
141 *((uint32*)(ptr+=16)) = temp; \
142 *((uint32*)(ptr+4)) = temp2;\
143 *((uint32*)(ptr+=16)) = temp; \
144 *((uint32*)(ptr+4)) = temp2;\
145 *((uint32*)(ptr+=16)) = temp; \
146 *((uint32*)(ptr+4)) = temp2;\
147 *((uint32*)(ptr+=16)) = temp; \
148 *((uint32*)(ptr+4)) = temp2;\
149 *((uint32*)(ptr+=16)) = temp; \
150 *((uint32*)(ptr+4)) = temp2;}
152 #define PAD_EXTRA_4x8 { temp = *((uint32*)(prev+8)); \
153 *((uint32*)ptr) = temp; \
154 *((uint32*)(ptr+=16)) = temp; \
155 *((uint32*)(ptr+=16)) = temp; \
156 *((uint32*)(ptr+=16)) = temp; \
157 *((uint32*)(ptr+=16)) = temp; \
158 *((uint32*)(ptr+=16)) = temp; \
159 *((uint32*)(ptr+=16)) = temp; \
160 *((uint32*)(ptr+=16)) = temp; }
162 #define PAD_COL { temp = *prev; \
163 temp|=(temp<<8); temp|=(temp<<16); \
164 *((uint32*)ptr) = temp; \
165 *((uint32*)(ptr+4)) = temp; \
166 temp = *(prev+=16); \
167 temp|=(temp<<8); temp|=(temp<<16); \
168 *((uint32*)(ptr+=16)) = temp; \
169 *((uint32*)(ptr+4)) = temp; \
170 temp = *(prev+=16); \
171 temp|=(temp<<8); temp|=(temp<<16); \
172 *((uint32*)(ptr+=16)) = temp; \
173 *((uint32*)(ptr+4)) = temp; \
174 temp = *(prev+=16); \
175 temp|=(temp<<8); temp|=(temp<<16); \
176 *((uint32*)(ptr+=16)) = temp; \
177 *((uint32*)(ptr+4)) = temp; \
178 temp = *(prev+=16); \
179 temp|=(temp<<8); temp|=(temp<<16); \
180 *((uint32*)(ptr+=16)) = temp; \
181 *((uint32*)(ptr+4)) = temp; \
182 temp = *(prev+=16); \
183 temp|=(temp<<8); temp|=(temp<<16); \
184 *((uint32*)(ptr+=16)) = temp; \
185 *((uint32*)(ptr+4)) = temp; \
186 temp = *(prev+=16); \
187 temp|=(temp<<8); temp|=(temp<<16); \
188 *((uint32*)(ptr+=16)) = temp; \
189 *((uint32*)(ptr+4)) = temp; \
190 temp = *(prev+=16); \
191 temp|=(temp<<8); temp|=(temp<<16); \
192 *((uint32*)(ptr+=16)) = temp; \
193 *((uint32*)(ptr+4)) = temp;}
196 #define COPY_BLOCK { *((uint32*)ptr) = *((uint32*)prev); \
197 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
198 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
199 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
200 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
201 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
202 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
203 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
204 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
205 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
206 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
207 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
208 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
209 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
210 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
211 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); }
213 #define COPY_12x8 { *((uint32*)ptr) = *((uint32*)prev); \
214 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
215 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
216 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
217 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
218 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
219 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
220 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
221 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
222 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
223 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
224 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
225 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
226 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
227 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
228 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
229 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
230 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
231 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
232 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
233 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
234 *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
235 *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
236 *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); }
238 /*----------------------------------------------------------------------------
240 ----------------------------------------------------------------------------*/
244 uint8 *c_prev, /* i */
245 uint8 *pred_block, /* i */
252 /*----------------------------------------------------------------------------
253 ; Define all local variables
254 ----------------------------------------------------------------------------*/
255 uint8 *prev; /* pointers to adjacent pixels in the */
256 uint8 pred[256]; /* storage for padded pixel values, 16x16 */
261 /*----------------------------------------------------------------------------
263 ----------------------------------------------------------------------------*/
264 /* saturate xpos and ypos */
265 if (xpos < -16) xpos = -16;
266 if (xpos > ((width - 1) << 1)) xpos = (width - 1) << 1;
267 if (ypos < -16) ypos = -16;
268 if (ypos > ((height - 1) << 1)) ypos = (height - 1) << 1;
272 if (ypos < 0) /* pad top left of frame */
275 ptr = pred + (8 << 4) + 8;
281 prev = pred + (8 << 4) + 8;
286 prev = pred + (8 << 4) + 8;
290 ptr = pred + (8 << 4);
291 prev = pred + (8 << 4) + 8;
295 ptr = pred + (((ypos >> 1) + 8) << 4) + (xpos >> 1) + 8;
297 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
301 else if ((ypos >> 1) < (height - B_SIZE)) /* pad left of frame */
305 prev = c_prev + (ypos >> 1) * width;
307 /* copy extra line */
308 *((uint32*)(ptr += 16)) = *((uint32*)(prev += width));
309 *((uint32*)(ptr + 4)) = *((uint32*)(prev + 4));
316 temp = *(prev += 16);
318 temp |= (temp << 16);
319 *((uint32*)(ptr += 16)) = temp;
320 *((uint32*)(ptr + 4)) = temp;
322 ptr = pred + 8 + (xpos >> 1);
324 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
328 else /* pad bottom left of frame */
331 ptr = pred + 8; /* point to the center */
332 prev = c_prev + width * (height - 8);
336 ptr = pred + (8 << 4);
341 ptr = pred + (8 << 4) + 8;
350 ptr = pred + 8 + (((ypos >> 1) - (height - 8)) << 4) + (xpos >> 1);
352 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
357 else if ((xpos >> 1) < (width - B_SIZE))
359 if (ypos < 0) /* pad top of frame */
362 xoffset = xoffset & 0x3; /* word align ptr */
365 ptr = pred + (8 << 4);
366 prev = c_prev + (xpos >> 1) - xoffset;
368 if (xoffset || (xpos&1)) /* copy extra 4x8 */
379 prev = pred + (8 << 4);
381 if (xoffset || (xpos&1)) /* pad extra 4x8 */
387 ptr = pred + (((ypos >> 1) + 8) << 4) + xoffset;
389 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
393 else /* pad bottom of frame */
396 xoffset = xoffset & 0x3; /* word align ptr */
399 prev = c_prev + width * (height - 8) + (xpos >> 1) - xoffset;
400 if (xoffset || (xpos&1))
410 ptr = pred + (8 << 4);
413 if (xoffset || (xpos&1))
415 ptr = pred + (8 << 4) + 8;
419 ptr = pred + (((ypos >> 1) - (height - 8)) << 4) + xoffset;
421 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
428 if (ypos < 0) /* pad top right of frame */
431 ptr = pred + (8 << 4);
432 prev = c_prev + width - 8;
437 prev = pred + (8 << 4) + 7;
442 prev = pred + (8 << 4);
446 ptr = pred + (8 << 4) + 8;
450 ptr = pred + ((8 + (ypos >> 1)) << 4) + (8 - (width - (xpos >> 1)));
452 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
456 else if ((ypos >> 1) < (height - B_SIZE)) /* pad right of frame */
460 prev = c_prev + (ypos >> 1) * width + width - 8;
462 /* copy extra line */
463 *((uint32*)(ptr += 16)) = *((uint32*)(prev += width));
464 *((uint32*)(ptr + 4)) = *((uint32*)(prev + 4));
471 temp = *(prev += 16);
473 temp |= (temp << 16);
474 *((uint32*)(ptr += 16)) = temp;
475 *((uint32*)(ptr + 4)) = temp;
478 ptr = pred + 8 - (width - (xpos >> 1));
480 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);
485 else /* pad bottom right of frame */
489 prev = c_prev + width * (height - 8) + width - 8;
492 /* pad bottom-right */
493 ptr = pred + (8 << 4) + 8;
503 ptr = pred + (8 << 4);
507 ptr = pred + 8 - (width - (xpos >> 1)) + ((8 - (height - (ypos >> 1))) << 4);
509 GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);