1 |
// 01.05.2002 updated MBMotionCompensationBVOP |
/***************************************************************************** |
2 |
// 14.04.2002 bframe compensation |
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Motion Compensation related code - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Peter Ross <pross@xvid.org> |
7 |
|
* 2003 Christoph Lampert <gruel@web.de> |
8 |
|
* |
9 |
|
* This program is free software ; you can redistribute it and/or modify |
10 |
|
* it under the terms of the GNU General Public License as published by |
11 |
|
* the Free Software Foundation ; either version 2 of the License, or |
12 |
|
* (at your option) any later version. |
13 |
|
* |
14 |
|
* This program is distributed in the hope that it will be useful, |
15 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
16 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
* GNU General Public License for more details. |
18 |
|
* |
19 |
|
* You should have received a copy of the GNU General Public License |
20 |
|
* along with this program ; if not, write to the Free Software |
21 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
|
* |
23 |
|
* $Id$ |
24 |
|
* |
25 |
|
****************************************************************************/ |
26 |
|
|
27 |
|
#include <stdio.h> |
28 |
|
|
29 |
#include "../encoder.h" |
#include "../encoder.h" |
30 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
31 |
#include "../image/interpolate8x8.h" |
#include "../image/interpolate8x8.h" |
32 |
|
#include "../image/qpel.h" |
33 |
|
#include "../image/reduced.h" |
34 |
#include "../utils/timer.h" |
#include "../utils/timer.h" |
35 |
#include "motion.h" |
#include "motion.h" |
36 |
|
|
37 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#ifndef RSHIFT |
38 |
#define SIGN(X) (((X)>0)?1:-1) |
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) |
39 |
|
#endif |
40 |
|
|
41 |
|
/* assume b>0 */ |
42 |
|
#ifndef RDIV |
43 |
|
#define RDIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) |
44 |
|
#endif |
45 |
|
|
46 |
|
|
47 |
|
/* This is borrowed from bitstream.c until we find a common solution */ |
48 |
|
|
49 |
|
static uint32_t __inline |
50 |
|
log2bin(uint32_t value) |
51 |
|
{ |
52 |
|
/* Changed by Chenm001 */ |
53 |
|
#if !defined(_MSC_VER) |
54 |
|
int n = 0; |
55 |
|
|
56 |
|
while (value) { |
57 |
|
value >>= 1; |
58 |
|
n++; |
59 |
|
} |
60 |
|
return n; |
61 |
|
#else |
62 |
|
__asm { |
63 |
|
bsr eax, value |
64 |
|
inc eax |
65 |
|
} |
66 |
|
#endif |
67 |
|
} |
68 |
|
|
69 |
|
/* |
70 |
|
* getref: calculate reference image pointer |
71 |
|
* the decision to use interpolation h/v/hv or the normal image is |
72 |
|
* based on dx & dy. |
73 |
|
*/ |
74 |
|
|
75 |
|
static __inline const uint8_t * |
76 |
|
get_ref(const uint8_t * const refn, |
77 |
|
const uint8_t * const refh, |
78 |
|
const uint8_t * const refv, |
79 |
|
const uint8_t * const refhv, |
80 |
|
const uint32_t x, |
81 |
|
const uint32_t y, |
82 |
|
const uint32_t block, |
83 |
|
const int32_t dx, |
84 |
|
const int32_t dy, |
85 |
|
const int32_t stride) |
86 |
|
{ |
87 |
|
switch (((dx & 1) << 1) + (dy & 1)) { |
88 |
|
case 0: |
89 |
|
return refn + (int) (((int)x * (int)block + dx / 2) + ((int)y * (int)block + dy / 2) * (int)stride); |
90 |
|
case 1: |
91 |
|
return refv + (int) (((int)x * (int)block + dx / 2) + ((int)y * (int)block + (dy - 1) / 2) * (int)stride); |
92 |
|
case 2: |
93 |
|
return refh + (int) (((int)x * (int)block + (dx - 1) / 2) + ((int)y * (int)block + dy / 2) * (int)stride); |
94 |
|
default: |
95 |
|
return refhv + (int) (((int)x * (int)block + (dx - 1) / 2) + ((int)y * (int)block + (dy - 1) / 2) * (int)stride); |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
static __inline void compensate8x8_halfpel( |
static __inline void |
100 |
int16_t * const dct_codes, |
compensate16x16_interpolate(int16_t * const dct_codes, |
101 |
uint8_t * const cur, |
uint8_t * const cur, |
102 |
const uint8_t * const ref, |
const uint8_t * const ref, |
103 |
const uint8_t * const refh, |
const uint8_t * const refh, |
104 |
const uint8_t * const refv, |
const uint8_t * const refv, |
105 |
const uint8_t * const refhv, |
const uint8_t * const refhv, |
106 |
const uint32_t x, const uint32_t y, |
uint8_t * const tmp, |
107 |
const int32_t dx, const int dy, |
uint32_t x, |
108 |
const uint32_t stride) |
uint32_t y, |
109 |
|
const int32_t dx, |
110 |
|
const int32_t dy, |
111 |
|
const int32_t stride, |
112 |
|
const int quarterpel, |
113 |
|
const int reduced_resolution, |
114 |
|
const int32_t rounding) |
115 |
{ |
{ |
116 |
int32_t ddx,ddy; |
const uint8_t * ptr; |
117 |
|
|
118 |
switch ( ((dx&1)<<1) + (dy&1) ) // ((dx%2)?2:0)+((dy%2)?1:0) |
if (!reduced_resolution) { |
|
{ |
|
|
case 0 : |
|
|
ddx = dx/2; |
|
|
ddy = dy/2; |
|
|
transfer_8to16sub(dct_codes, cur + y*stride + x, |
|
|
ref + (y+ddy)*stride + x+ddx, stride); |
|
|
break; |
|
119 |
|
|
120 |
case 1 : |
if(quarterpel) { |
121 |
ddx = dx/2; |
if ((dx&3) | (dy&3)) { |
122 |
ddy = (dy-1)/2; |
interpolate16x16_quarterpel(tmp - y * stride - x, |
123 |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
(uint8_t *) ref, tmp + 32, |
124 |
refv + (y+ddy)*stride + x+ddx, stride); |
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
125 |
break; |
ptr = tmp; |
126 |
|
} else ptr = ref + ((int)y + dy/4)*(int)stride + (int)x + dx/4; /* fullpixel position */ |
127 |
|
|
128 |
case 2 : |
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
|
ddx = (dx-1)/2; |
|
|
ddy = dy/2; |
|
|
transfer_8to16sub(dct_codes, cur + y*stride + x, |
|
|
refh + (y+ddy)*stride + x+ddx, stride); |
|
|
break; |
|
129 |
|
|
|
default : // case 3: |
|
|
ddx = (dx-1)/2; |
|
|
ddy = (dy-1)/2; |
|
130 |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
131 |
refhv + (y+ddy)*stride + x+ddx, stride); |
ptr, stride); |
132 |
break; |
transfer_8to16sub(dct_codes+64, cur + y * stride + x + 8, |
133 |
|
ptr + 8, stride); |
134 |
|
transfer_8to16sub(dct_codes+128, cur + y * stride + x + 8*stride, |
135 |
|
ptr + 8*stride, stride); |
136 |
|
transfer_8to16sub(dct_codes+192, cur + y * stride + x + 8*stride+8, |
137 |
|
ptr + 8*stride + 8, stride); |
138 |
|
|
139 |
|
} else { /* reduced_resolution */ |
140 |
|
|
141 |
|
x *= 2; y *= 2; |
142 |
|
|
143 |
|
ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
144 |
|
|
145 |
|
filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride); |
146 |
|
filter_diff_18x18_to_8x8(dct_codes, ptr, stride); |
147 |
|
|
148 |
|
filter_18x18_to_8x8(dct_codes+64, cur+y*stride + x + 16, stride); |
149 |
|
filter_diff_18x18_to_8x8(dct_codes+64, ptr + 16, stride); |
150 |
|
|
151 |
|
filter_18x18_to_8x8(dct_codes+128, cur+(y+16)*stride + x, stride); |
152 |
|
filter_diff_18x18_to_8x8(dct_codes+128, ptr + 16*stride, stride); |
153 |
|
|
154 |
|
filter_18x18_to_8x8(dct_codes+192, cur+(y+16)*stride + x + 16, stride); |
155 |
|
filter_diff_18x18_to_8x8(dct_codes+192, ptr + 16*stride + 16, stride); |
156 |
|
|
157 |
|
transfer32x32_copy(cur + y*stride + x, ptr, stride); |
158 |
} |
} |
159 |
} |
} |
160 |
|
|
161 |
|
static __inline void |
162 |
|
compensate8x8_interpolate( int16_t * const dct_codes, |
163 |
|
uint8_t * const cur, |
164 |
|
const uint8_t * const ref, |
165 |
|
const uint8_t * const refh, |
166 |
|
const uint8_t * const refv, |
167 |
|
const uint8_t * const refhv, |
168 |
|
uint8_t * const tmp, |
169 |
|
uint32_t x, |
170 |
|
uint32_t y, |
171 |
|
const int32_t dx, |
172 |
|
const int32_t dy, |
173 |
|
const int32_t stride, |
174 |
|
const int32_t quarterpel, |
175 |
|
const int reduced_resolution, |
176 |
|
const int32_t rounding) |
177 |
|
{ |
178 |
|
const uint8_t * ptr; |
179 |
|
|
180 |
|
if (!reduced_resolution) { |
181 |
|
|
182 |
|
if(quarterpel) { |
183 |
|
if ((dx&3) | (dy&3)) { |
184 |
|
interpolate8x8_quarterpel(tmp - y*stride - x, |
185 |
|
(uint8_t *) ref, tmp + 32, |
186 |
|
tmp + 64, tmp + 96, x, y, dx, dy, stride, rounding); |
187 |
|
ptr = tmp; |
188 |
|
} else ptr = ref + ((int)y + dy/4)*(int)stride + (int)x + dx/4; /* fullpixel position */ |
189 |
|
} else ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
190 |
|
|
191 |
void MBMotionCompensation( |
transfer_8to16sub(dct_codes, cur + y * stride + x, ptr, stride); |
192 |
MACROBLOCK * const mb, |
|
193 |
|
} else { /* reduced_resolution */ |
194 |
|
|
195 |
|
x *= 2; y *= 2; |
196 |
|
|
197 |
|
ptr = get_ref(ref, refh, refv, refhv, x, y, 1, dx, dy, stride); |
198 |
|
|
199 |
|
filter_18x18_to_8x8(dct_codes, cur+y*stride + x, stride); |
200 |
|
filter_diff_18x18_to_8x8(dct_codes, ptr, stride); |
201 |
|
|
202 |
|
transfer16x16_copy(cur + y*stride + x, ptr, stride); |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
/* XXX: slow, inelegant... */ |
207 |
|
static void |
208 |
|
interpolate18x18_switch(uint8_t * const cur, |
209 |
|
const uint8_t * const refn, |
210 |
|
const uint32_t x, |
211 |
|
const uint32_t y, |
212 |
|
const int32_t dx, |
213 |
|
const int dy, |
214 |
|
const int32_t stride, |
215 |
|
const int32_t rounding) |
216 |
|
{ |
217 |
|
interpolate8x8_switch(cur, refn, x-1, y-1, dx, dy, stride, rounding); |
218 |
|
interpolate8x8_switch(cur, refn, x+7, y-1, dx, dy, stride, rounding); |
219 |
|
interpolate8x8_switch(cur, refn, x+9, y-1, dx, dy, stride, rounding); |
220 |
|
|
221 |
|
interpolate8x8_switch(cur, refn, x-1, y+7, dx, dy, stride, rounding); |
222 |
|
interpolate8x8_switch(cur, refn, x+7, y+7, dx, dy, stride, rounding); |
223 |
|
interpolate8x8_switch(cur, refn, x+9, y+7, dx, dy, stride, rounding); |
224 |
|
|
225 |
|
interpolate8x8_switch(cur, refn, x-1, y+9, dx, dy, stride, rounding); |
226 |
|
interpolate8x8_switch(cur, refn, x+7, y+9, dx, dy, stride, rounding); |
227 |
|
interpolate8x8_switch(cur, refn, x+9, y+9, dx, dy, stride, rounding); |
228 |
|
} |
229 |
|
|
230 |
|
static void |
231 |
|
CompensateChroma( int dx, int dy, |
232 |
|
const int i, const int j, |
233 |
|
IMAGE * const Cur, |
234 |
|
const IMAGE * const Ref, |
235 |
|
uint8_t * const temp, |
236 |
|
int16_t * const coeff, |
237 |
|
const int32_t stride, |
238 |
|
const int rounding, |
239 |
|
const int rrv) |
240 |
|
{ /* uv-block-based compensation */ |
241 |
|
|
242 |
|
if (!rrv) { |
243 |
|
transfer_8to16sub(coeff, Cur->u + 8 * j * stride + 8 * i, |
244 |
|
interpolate8x8_switch2(temp, Ref->u, 8 * i, 8 * j, |
245 |
|
dx, dy, stride, rounding), |
246 |
|
stride); |
247 |
|
transfer_8to16sub(coeff + 64, Cur->v + 8 * j * stride + 8 * i, |
248 |
|
interpolate8x8_switch2(temp, Ref->v, 8 * i, 8 * j, |
249 |
|
dx, dy, stride, rounding), |
250 |
|
stride); |
251 |
|
} else { |
252 |
|
uint8_t * current, * reference; |
253 |
|
|
254 |
|
current = Cur->u + 16*j*stride + 16*i; |
255 |
|
reference = temp - 16*j*stride - 16*i; |
256 |
|
interpolate18x18_switch(reference, Ref->u, 16*i, 16*j, dx, dy, stride, rounding); |
257 |
|
filter_18x18_to_8x8(coeff, current, stride); |
258 |
|
filter_diff_18x18_to_8x8(coeff, temp, stride); |
259 |
|
transfer16x16_copy(current, temp, stride); |
260 |
|
|
261 |
|
current = Cur->v + 16*j*stride + 16*i; |
262 |
|
interpolate18x18_switch(reference, Ref->v, 16*i, 16*j, dx, dy, stride, rounding); |
263 |
|
filter_18x18_to_8x8(coeff + 64, current, stride); |
264 |
|
filter_diff_18x18_to_8x8(coeff + 64, temp, stride); |
265 |
|
transfer16x16_copy(current, temp, stride); |
266 |
|
} |
267 |
|
} |
268 |
|
|
269 |
|
void |
270 |
|
MBMotionCompensation(MACROBLOCK * const mb, |
271 |
const uint32_t i, |
const uint32_t i, |
272 |
const uint32_t j, |
const uint32_t j, |
273 |
const IMAGE * const ref, |
const IMAGE * const ref, |
274 |
const IMAGE * const refh, |
const IMAGE * const refh, |
275 |
const IMAGE * const refv, |
const IMAGE * const refv, |
276 |
const IMAGE * const refhv, |
const IMAGE * const refhv, |
277 |
|
const IMAGE * const refGMC, |
278 |
IMAGE * const cur, |
IMAGE * const cur, |
279 |
int16_t *dct_codes, |
int16_t *dct_codes, |
280 |
const uint32_t width, |
const uint32_t width, |
281 |
const uint32_t height, |
const uint32_t height, |
282 |
const uint32_t edged_width, |
const uint32_t edged_width, |
283 |
const uint32_t rounding) |
const int32_t quarterpel, |
284 |
|
const int reduced_resolution, |
285 |
|
const int32_t rounding) |
286 |
{ |
{ |
287 |
static const uint32_t roundtab[16] = |
int32_t dx; |
288 |
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
int32_t dy; |
289 |
|
|
290 |
|
uint8_t * const tmp = refv->u; |
291 |
|
|
292 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
if ( (!reduced_resolution) && (mb->mode == MODE_NOT_CODED) ) { /* quick copy for early SKIP */ |
293 |
{ |
/* early SKIP is only activated in P-VOPs, not in S-VOPs, so mcsel can never be 1 */ |
|
int32_t dx = mb->mvs[0].x; |
|
|
int32_t dy = mb->mvs[0].y; |
|
294 |
|
|
295 |
compensate8x8_halfpel(&dct_codes[0*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
transfer16x16_copy(cur->y + 16 * (i + j * edged_width), |
296 |
16*i, 16*j, dx, dy, edged_width); |
ref->y + 16 * (i + j * edged_width), |
297 |
compensate8x8_halfpel(&dct_codes[1*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
edged_width); |
298 |
16*i + 8, 16*j, dx, dy, edged_width); |
|
299 |
compensate8x8_halfpel(&dct_codes[2*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
transfer8x8_copy(cur->u + 8 * (i + j * edged_width/2), |
300 |
16*i, 16*j + 8, dx, dy, edged_width); |
ref->u + 8 * (i + j * edged_width/2), |
301 |
compensate8x8_halfpel(&dct_codes[3*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
edged_width / 2); |
302 |
16*i + 8, 16*j + 8, dx, dy, edged_width); |
transfer8x8_copy(cur->v + 8 * (i + j * edged_width/2), |
303 |
|
ref->v + 8 * (i + j * edged_width/2), |
304 |
dx = (dx & 3) ? (dx >> 1) | 1 : dx / 2; |
edged_width / 2); |
305 |
dy = (dy & 3) ? (dy >> 1) | 1 : dy / 2; |
return; |
|
|
|
|
/* uv-image-based compensation */ |
|
|
#ifdef BFRAMES |
|
|
compensate8x8_halfpel(&dct_codes[4*64], cur->u, ref->u, refh->u, refv->u, refhv->u, |
|
|
8*i, 8*j, dx, dy, edged_width/2); |
|
|
compensate8x8_halfpel(&dct_codes[5*64], cur->v, ref->v, refh->v, refv->v, refhv->v, |
|
|
8*i, 8*j, dx, dy, edged_width/2); |
|
|
#else |
|
|
/* uv-block-based compensation */ |
|
|
interpolate8x8_switch(refv->u, ref->u, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
|
|
transfer_8to16sub(&dct_codes[4*64], |
|
|
cur->u + 8*j*edged_width/2 + 8*i, |
|
|
refv->u + 8*j*edged_width/2 + 8*i, edged_width/2); |
|
|
|
|
|
interpolate8x8_switch(refv->v, ref->v, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
|
|
transfer_8to16sub(&dct_codes[5*64], |
|
|
cur->v + 8*j*edged_width/2 + 8*i, |
|
|
refv->v + 8*j*edged_width/2 + 8*i, edged_width/2); |
|
|
#endif |
|
306 |
} |
} |
|
else // mode == MODE_INTER4V |
|
|
{ |
|
|
int32_t sum, dx, dy; |
|
307 |
|
|
308 |
compensate8x8_halfpel(&dct_codes[0*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
if ((mb->mode == MODE_NOT_CODED || mb->mode == MODE_INTER |
309 |
16*i, 16*j, mb->mvs[0].x, mb->mvs[0].y, edged_width); |
|| mb->mode == MODE_INTER_Q)) { |
310 |
compensate8x8_halfpel(&dct_codes[1*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
|
311 |
16*i + 8, 16*j, mb->mvs[1].x, mb->mvs[1].y, edged_width); |
/* reduced resolution + GMC: not possible */ |
312 |
compensate8x8_halfpel(&dct_codes[2*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
|
313 |
16*i, 16*j + 8, mb->mvs[2].x, mb->mvs[2].y, edged_width); |
if (mb->mcsel) { |
314 |
compensate8x8_halfpel(&dct_codes[3*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
|
315 |
16*i + 8, 16*j + 8, mb->mvs[3].x, mb->mvs[3].y, edged_width); |
/* call normal routine once, easier than "if (mcsel)"ing all the time */ |
316 |
|
|
317 |
sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x; |
transfer_8to16sub(&dct_codes[0*64], cur->y + 16*j*edged_width + 16*i, |
318 |
dx = (sum ? SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
refGMC->y + 16*j*edged_width + 16*i, edged_width); |
319 |
|
transfer_8to16sub(&dct_codes[1*64], cur->y + 16*j*edged_width + 16*i+8, |
320 |
sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y; |
refGMC->y + 16*j*edged_width + 16*i+8, edged_width); |
321 |
dy = (sum ? SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
transfer_8to16sub(&dct_codes[2*64], cur->y + (16*j+8)*edged_width + 16*i, |
322 |
|
refGMC->y + (16*j+8)*edged_width + 16*i, edged_width); |
323 |
/* uv-image-based compensation */ |
transfer_8to16sub(&dct_codes[3*64], cur->y + (16*j+8)*edged_width + 16*i+8, |
324 |
#ifdef BFRAMES |
refGMC->y + (16*j+8)*edged_width + 16*i+8, edged_width); |
325 |
compensate8x8_halfpel(&dct_codes[4*64], cur->u, ref->u, refh->u, refv->u, refhv->u, |
|
326 |
8*i, 8*j, dx, dy, edged_width/2); |
/* lumi is needed earlier for mode decision, but chroma should be done block-based, but it isn't, yet. */ |
327 |
compensate8x8_halfpel(&dct_codes[5*64], cur->v, ref->v, refh->v, refv->v, refhv->v, |
|
328 |
8*i, 8*j, dx, dy, edged_width/2); |
transfer_8to16sub(&dct_codes[4 * 64], cur->u + 8 *j*edged_width/2 + 8*i, |
329 |
#else |
refGMC->u + 8 *j*edged_width/2 + 8*i, edged_width/2); |
330 |
/* uv-block-based compensation */ |
|
331 |
interpolate8x8_switch(refv->u, ref->u, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
transfer_8to16sub(&dct_codes[5 * 64], cur->v + 8*j* edged_width/2 + 8*i, |
332 |
transfer_8to16sub(&dct_codes[4*64], |
refGMC->v + 8*j* edged_width/2 + 8*i, edged_width/2); |
333 |
cur->u + 8*j*edged_width/2 + 8*i, |
|
334 |
refv->u + 8*j*edged_width/2 + 8*i, edged_width/2); |
return; |
335 |
|
} |
336 |
interpolate8x8_switch(refv->v, ref->v, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
|
337 |
transfer_8to16sub(&dct_codes[5*64], |
/* ordinary compensation */ |
338 |
cur->v + 8*j*edged_width/2 + 8*i, |
|
339 |
refv->v + 8*j*edged_width/2 + 8*i, edged_width/2); |
dx = (quarterpel ? mb->qmvs[0].x : mb->mvs[0].x); |
340 |
#endif |
dy = (quarterpel ? mb->qmvs[0].y : mb->mvs[0].y); |
341 |
|
|
342 |
|
if (reduced_resolution) { |
343 |
|
dx = RRV_MV_SCALEUP(dx); |
344 |
|
dy = RRV_MV_SCALEUP(dy); |
345 |
|
} |
346 |
|
|
347 |
|
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, ref->y, refh->y, |
348 |
|
refv->y, refhv->y, tmp, 16 * i, 16 * j, dx, dy, |
349 |
|
edged_width, quarterpel, reduced_resolution, rounding); |
350 |
|
|
351 |
|
if (quarterpel) { dx /= 2; dy /= 2; } |
352 |
|
|
353 |
|
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
354 |
|
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
355 |
|
|
356 |
|
} else { /* mode == MODE_INTER4V */ |
357 |
|
int k, sumx = 0, sumy = 0; |
358 |
|
const VECTOR * const mvs = (quarterpel ? mb->qmvs : mb->mvs); |
359 |
|
|
360 |
|
for (k = 0; k < 4; k++) { |
361 |
|
dx = mvs[k].x; |
362 |
|
dy = mvs[k].y; |
363 |
|
sumx += quarterpel ? dx/2 : dx; |
364 |
|
sumy += quarterpel ? dy/2 : dy; |
365 |
|
|
366 |
|
if (reduced_resolution){ |
367 |
|
dx = RRV_MV_SCALEUP(dx); |
368 |
|
dy = RRV_MV_SCALEUP(dy); |
369 |
|
} |
370 |
|
|
371 |
|
compensate8x8_interpolate(&dct_codes[k * 64], cur->y, ref->y, refh->y, |
372 |
|
refv->y, refhv->y, tmp, 16 * i + 8*(k&1), 16 * j + 8*(k>>1), dx, |
373 |
|
dy, edged_width, quarterpel, reduced_resolution, rounding); |
374 |
|
} |
375 |
|
dx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
376 |
|
dy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
377 |
} |
} |
378 |
|
|
379 |
|
CompensateChroma(dx, dy, i, j, cur, ref, tmp, |
380 |
|
&dct_codes[4 * 64], edged_width / 2, rounding, reduced_resolution); |
381 |
} |
} |
382 |
|
|
383 |
|
|
384 |
void MBMotionCompensationBVOP( |
void |
385 |
MBParam * pParam, |
MBMotionCompensationBVOP(MBParam * pParam, |
386 |
MACROBLOCK * const mb, |
MACROBLOCK * const mb, |
387 |
const uint32_t i, |
const uint32_t i, |
388 |
const uint32_t j, |
const uint32_t j, |
397 |
const IMAGE * const b_refhv, |
const IMAGE * const b_refhv, |
398 |
int16_t * dct_codes) |
int16_t * dct_codes) |
399 |
{ |
{ |
400 |
static const uint32_t roundtab[16] = |
const uint32_t edged_width = pParam->edged_width; |
401 |
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
int32_t dx, dy, b_dx, b_dy, sumx, sumy, b_sumx, b_sumy; |
402 |
|
int k; |
403 |
const int32_t edged_width = pParam->edged_width; |
const int quarterpel = pParam->vol_flags & XVID_VOL_QUARTERPEL; |
404 |
int32_t dx, dy; |
const uint8_t * ptr1, * ptr2; |
405 |
int32_t b_dx, b_dy; |
uint8_t * const tmp = f_refv->u; |
406 |
int x = i; |
const VECTOR * const fmvs = (quarterpel ? mb->qmvs : mb->mvs); |
407 |
int y = j; |
const VECTOR * const bmvs = (quarterpel ? mb->b_qmvs : mb->b_mvs); |
|
|
|
|
|
|
408 |
|
|
409 |
switch(mb->mode) |
switch (mb->mode) { |
|
{ |
|
410 |
case MODE_FORWARD : |
case MODE_FORWARD : |
411 |
dx = mb->mvs[0].x; |
dx = fmvs->x; dy = fmvs->y; |
|
dy = mb->mvs[0].y; |
|
|
|
|
|
transfer_8to16sub_c( |
|
|
&dct_codes[0*64], |
|
|
cur->y + (j*16)*edged_width + (i*16), |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
i*16, j*16, 1, dx, dy, edged_width), |
|
|
edged_width); |
|
|
|
|
|
transfer_8to16sub( |
|
|
&dct_codes[1*64], |
|
|
cur->y + (j*16)*edged_width + (i*16+8), |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
i*16+8, j*16, 1, dx, dy, edged_width), |
|
|
edged_width); |
|
|
|
|
|
transfer_8to16sub_c( |
|
|
&dct_codes[2*64], |
|
|
cur->y + (j*16+8)*edged_width + (i*16), |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
i*16, j*16+8, 1, dx, dy, edged_width), |
|
|
edged_width); |
|
|
|
|
|
transfer_8to16sub( |
|
|
&dct_codes[3*64], |
|
|
cur->y + (j*16+8)*edged_width + (i*16+8), |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
i*16+8, j*16+8, 1, dx, dy, edged_width), |
|
|
edged_width); |
|
412 |
|
|
413 |
|
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, f_ref->y, f_refh->y, |
414 |
|
f_refv->y, f_refhv->y, tmp, 16 * i, 16 * j, dx, |
415 |
|
dy, edged_width, quarterpel, 0, 0); |
416 |
|
|
417 |
dx = (dx & 3) ? (dx >> 1) | 1 : dx / 2; |
if (quarterpel) { dx /= 2; dy /= 2; } |
|
dy = (dy & 3) ? (dy >> 1) | 1 : dy / 2; |
|
418 |
|
|
419 |
/* uv-image-based compensation */ |
CompensateChroma( (dx >> 1) + roundtab_79[dx & 0x3], |
420 |
compensate8x8_halfpel(&dct_codes[4*64], cur->u, f_ref->u, f_refh->u, f_refv->u, f_refhv->u, |
(dy >> 1) + roundtab_79[dy & 0x3], |
421 |
8*i, 8*j, dx, dy, edged_width/2); |
i, j, cur, f_ref, tmp, |
422 |
compensate8x8_halfpel(&dct_codes[5*64], cur->v, f_ref->v, f_refh->v, f_refv->v, f_refhv->v, |
&dct_codes[4 * 64], edged_width / 2, 0, 0); |
|
8*i, 8*j, dx, dy, edged_width/2); |
|
423 |
|
|
424 |
break; |
return; |
425 |
|
|
426 |
case MODE_BACKWARD : |
case MODE_BACKWARD : |
427 |
b_dx = mb->b_mvs[0].x; |
b_dx = bmvs->x; b_dy = bmvs->y; |
|
b_dy = mb->b_mvs[0].y; |
|
|
|
|
|
transfer_8to16sub_c( |
|
|
&dct_codes[0*64], |
|
|
cur->y + (j*16)*edged_width + (i*16), |
|
|
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
|
|
i*16, j*16, 1, b_dx, b_dy, edged_width), |
|
|
edged_width); |
|
428 |
|
|
429 |
transfer_8to16sub( |
compensate16x16_interpolate(&dct_codes[0 * 64], cur->y, b_ref->y, b_refh->y, |
430 |
&dct_codes[1*64], |
b_refv->y, b_refhv->y, tmp, 16 * i, 16 * j, b_dx, |
431 |
cur->y + (j*16)*edged_width + (i*16+8), |
b_dy, edged_width, quarterpel, 0, 0); |
432 |
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
|
433 |
i*16+8, j*16, 1, b_dx, b_dy, edged_width), |
if (quarterpel) { b_dx /= 2; b_dy /= 2; } |
434 |
edged_width); |
|
435 |
|
CompensateChroma( (b_dx >> 1) + roundtab_79[b_dx & 0x3], |
436 |
|
(b_dy >> 1) + roundtab_79[b_dy & 0x3], |
437 |
|
i, j, cur, b_ref, tmp, |
438 |
|
&dct_codes[4 * 64], edged_width / 2, 0, 0); |
439 |
|
|
440 |
|
return; |
441 |
|
|
442 |
|
case MODE_INTERPOLATE: /* _could_ use DIRECT, but would be overkill (no 4MV there) */ |
443 |
|
case MODE_DIRECT_NO4V: |
444 |
|
dx = fmvs->x; dy = fmvs->y; |
445 |
|
b_dx = bmvs->x; b_dy = bmvs->y; |
446 |
|
|
447 |
|
if (quarterpel) { |
448 |
|
|
449 |
|
if ((dx&3) | (dy&3)) { |
450 |
|
interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width, |
451 |
|
(uint8_t *) f_ref->y, tmp + 32, |
452 |
|
tmp + 64, tmp + 96, 16*i, 16*j, dx, dy, edged_width, 0); |
453 |
|
ptr1 = tmp; |
454 |
|
} else ptr1 = f_ref->y + (16*(int)j + dy/4)*(int)edged_width + 16*(int)i + dx/4; /* fullpixel position */ |
455 |
|
|
456 |
|
if ((b_dx&3) | (b_dy&3)) { |
457 |
|
interpolate16x16_quarterpel(tmp - i * 16 - j * 16 * edged_width + 16, |
458 |
|
(uint8_t *) b_ref->y, tmp + 32, |
459 |
|
tmp + 64, tmp + 96, 16*i, 16*j, b_dx, b_dy, edged_width, 0); |
460 |
|
ptr2 = tmp + 16; |
461 |
|
} else ptr2 = b_ref->y + (16*(int)j + b_dy/4)*(int)edged_width + 16*(int)i + b_dx/4; /* fullpixel position */ |
462 |
|
|
463 |
|
b_dx /= 2; |
464 |
|
b_dy /= 2; |
465 |
|
dx /= 2; |
466 |
|
dy /= 2; |
467 |
|
|
468 |
|
} else { |
469 |
|
ptr1 = get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
470 |
|
i, j, 16, dx, dy, edged_width); |
471 |
|
|
472 |
transfer_8to16sub_c( |
ptr2 = get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
473 |
&dct_codes[2*64], |
i, j, 16, b_dx, b_dy, edged_width); |
474 |
cur->y + (j*16+8)*edged_width + (i*16), |
} |
475 |
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
for (k = 0; k < 4; k++) |
476 |
i*16, j*16+8, 1, b_dx, b_dy, edged_width), |
transfer_8to16sub2(&dct_codes[k * 64], |
477 |
edged_width); |
cur->y + (i * 16+(k&1)*8) + (j * 16+((k>>1)*8)) * edged_width, |
478 |
|
ptr1 + (k&1)*8 + (k>>1)*8*edged_width, |
479 |
|
ptr2 + (k&1)*8 + (k>>1)*8*edged_width, edged_width); |
480 |
|
|
|
transfer_8to16sub( |
|
|
&dct_codes[3*64], |
|
|
cur->y + (j*16+8)*edged_width + (i*16+8), |
|
|
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
|
|
i*16+8, j*16+8, 1, b_dx, b_dy, edged_width), |
|
|
edged_width); |
|
481 |
|
|
482 |
b_dx = (b_dx & 3) ? (b_dx >> 1) | 1 : b_dx / 2; |
dx = (dx >> 1) + roundtab_79[dx & 0x3]; |
483 |
b_dy = (b_dy & 3) ? (b_dy >> 1) | 1 : b_dy / 2; |
dy = (dy >> 1) + roundtab_79[dy & 0x3]; |
484 |
|
|
485 |
/* uv-image-based compensation */ |
b_dx = (b_dx >> 1) + roundtab_79[b_dx & 0x3]; |
486 |
compensate8x8_halfpel(&dct_codes[4*64], cur->u, |
b_dy = (b_dy >> 1) + roundtab_79[b_dy & 0x3]; |
|
b_ref->u, b_refh->u, b_refv->u, b_refhv->u, |
|
|
8*i, 8*j, b_dx, b_dy, edged_width/2); |
|
|
compensate8x8_halfpel(&dct_codes[5*64], cur->v, |
|
|
b_ref->v, b_refh->v, b_refv->v, b_refhv->v, |
|
|
8*i, 8*j, b_dx, b_dy, edged_width/2); |
|
487 |
|
|
488 |
break; |
break; |
489 |
|
|
490 |
|
default: /* MODE_DIRECT (or MODE_DIRECT_NONE_MV in case of bframes decoding) */ |
491 |
|
sumx = sumy = b_sumx = b_sumy = 0; |
492 |
|
|
493 |
case MODE_INTERPOLATE : |
for (k = 0; k < 4; k++) { |
|
dx = mb->mvs[0].x; |
|
|
dy = mb->mvs[0].y; |
|
|
b_dx = mb->b_mvs[0].x; |
|
|
b_dy = mb->b_mvs[0].y; |
|
|
|
|
|
transfer_8to16sub2_c( |
|
|
&dct_codes[0*64], |
|
|
cur->y + (i*16) + (j*16)*edged_width, |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
16*i, 16*j, 1, dx, dy, edged_width), |
|
|
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
|
|
16*i, 16*j, 1, b_dx, b_dy, edged_width), |
|
|
edged_width); |
|
|
|
|
|
transfer_8to16sub2_c( |
|
|
&dct_codes[1*64], |
|
|
cur->y + (i*16+8) + (j*16)*edged_width, |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
16*i+8, 16*j, 1, dx, dy, edged_width), |
|
|
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
|
|
16*i+8, 16*j, 1, b_dx, b_dy, edged_width), |
|
|
edged_width); |
|
494 |
|
|
495 |
transfer_8to16sub2_c( |
dx = fmvs[k].x; dy = fmvs[k].y; |
496 |
&dct_codes[2*64], |
b_dx = bmvs[k].x; b_dy = bmvs[k].y; |
|
cur->y + (i*16) + (j*16+8)*edged_width, |
|
|
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
|
16*i, 16*j+8, 1, dx, dy, edged_width), |
|
|
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
|
|
16*i, 16*j+8, 1, b_dx, b_dy, edged_width), |
|
|
edged_width); |
|
497 |
|
|
498 |
transfer_8to16sub2_c( |
if (quarterpel) { |
499 |
&dct_codes[3*64], |
sumx += dx/2; sumy += dy/2; |
500 |
cur->y + (i*16+8) + (j*16+8)*edged_width, |
b_sumx += b_dx/2; b_sumy += b_dy/2; |
501 |
get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
|
502 |
16*i + 8, 16*j + 8, 1, dx, dy, edged_width), |
if ((dx&3) | (dy&3)) { |
503 |
get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
interpolate8x8_quarterpel(tmp - (i * 16+(k&1)*8) - (j * 16+((k>>1)*8)) * edged_width, |
504 |
16*i + 8, 16*j + 8, 1, b_dx, b_dy, edged_width), |
(uint8_t *) f_ref->y, |
505 |
edged_width); |
tmp + 32, tmp + 64, tmp + 96, |
506 |
|
16*i + (k&1)*8, 16*j + (k>>1)*8, dx, dy, edged_width, 0); |
507 |
|
ptr1 = tmp; |
508 |
|
} else ptr1 = f_ref->y + (16*(int)j + (k>>1)*8 + dy/4)*(int)edged_width + 16*(int)i + (k&1)*8 + dx/4; |
509 |
|
|
510 |
|
if ((b_dx&3) | (b_dy&3)) { |
511 |
|
interpolate8x8_quarterpel(tmp - (i * 16+(k&1)*8) - (j * 16+((k>>1)*8)) * edged_width + 16, |
512 |
|
(uint8_t *) b_ref->y, |
513 |
|
tmp + 16, tmp + 32, tmp + 48, |
514 |
|
16*i + (k&1)*8, 16*j + (k>>1)*8, b_dx, b_dy, edged_width, 0); |
515 |
|
ptr2 = tmp + 16; |
516 |
|
} else ptr2 = b_ref->y + (16*(int)j + (k>>1)*8 + b_dy/4)*(int)edged_width + 16*(int)i + (k&1)*8 + b_dx/4; |
517 |
|
} else { |
518 |
|
sumx += dx; sumy += dy; |
519 |
|
b_sumx += b_dx; b_sumy += b_dy; |
520 |
|
|
521 |
|
ptr1 = get_ref(f_ref->y, f_refh->y, f_refv->y, f_refhv->y, |
522 |
|
2*i + (k&1), 2*j + (k>>1), 8, dx, dy, edged_width); |
523 |
|
ptr2 = get_ref(b_ref->y, b_refh->y, b_refv->y, b_refhv->y, |
524 |
|
2*i + (k&1), 2*j + (k>>1), 8, b_dx, b_dy, edged_width); |
525 |
|
} |
526 |
|
transfer_8to16sub2(&dct_codes[k * 64], |
527 |
|
cur->y + (i * 16+(k&1)*8) + (j * 16+((k>>1)*8)) * edged_width, |
528 |
|
ptr1, ptr2, edged_width); |
529 |
|
|
530 |
|
} |
531 |
|
|
532 |
dx = (dx & 3) ? (dx >> 1) | 1 : dx / 2; |
dx = (sumx >> 3) + roundtab_76[sumx & 0xf]; |
533 |
dy = (dy & 3) ? (dy >> 1) | 1 : dy / 2; |
dy = (sumy >> 3) + roundtab_76[sumy & 0xf]; |
534 |
|
b_dx = (b_sumx >> 3) + roundtab_76[b_sumx & 0xf]; |
535 |
|
b_dy = (b_sumy >> 3) + roundtab_76[b_sumy & 0xf]; |
536 |
|
|
537 |
b_dx = (b_dx & 3) ? (b_dx >> 1) | 1 : b_dx / 2; |
break; |
538 |
b_dy = (b_dy & 3) ? (b_dy >> 1) | 1 : b_dy / 2; |
} |
539 |
|
|
540 |
transfer_8to16sub2_c( |
/* v block-based chroma interpolation for direct and interpolate modes */ |
541 |
&dct_codes[4*64], |
transfer_8to16sub2(&dct_codes[4 * 64], |
542 |
cur->u + (y*8)*edged_width/2 + (x*8), |
cur->u + (j * 8) * edged_width / 2 + (i * 8), |
543 |
get_ref(f_ref->u, f_refh->u, f_refv->u, f_refhv->u, |
interpolate8x8_switch2(tmp, b_ref->u, 8 * i, 8 * j, |
544 |
8*i, 8*j, 1, dx, dy, edged_width/2), |
b_dx, b_dy, edged_width / 2, 0), |
545 |
get_ref(b_ref->u, b_refh->u, b_refv->u, b_refhv->u, |
interpolate8x8_switch2(tmp + 8, f_ref->u, 8 * i, 8 * j, |
546 |
8*i, 8*j, 1, b_dx, b_dy, edged_width/2), |
dx, dy, edged_width / 2, 0), |
547 |
edged_width/2); |
edged_width/2); |
548 |
|
|
549 |
transfer_8to16sub2_c( |
transfer_8to16sub2(&dct_codes[5 * 64], |
550 |
&dct_codes[5*64], |
cur->v + (j * 8) * edged_width / 2 + (i * 8), |
551 |
cur->v + (y*8)*edged_width/2 + (x*8), |
interpolate8x8_switch2(tmp, b_ref->v, 8 * i, 8 * j, |
552 |
get_ref(f_ref->v, f_refh->v, f_refv->v, f_refhv->v, |
b_dx, b_dy, edged_width / 2, 0), |
553 |
8*i, 8*j, 1, dx, dy, edged_width/2), |
interpolate8x8_switch2(tmp + 8, f_ref->v, 8 * i, 8 * j, |
554 |
get_ref(b_ref->v, b_refh->v, b_refv->v, b_refhv->v, |
dx, dy, edged_width / 2, 0), |
|
8*i, 8*j, 1, b_dx, b_dy, edged_width/2), |
|
555 |
edged_width/2); |
edged_width/2); |
|
|
|
|
break; |
|
|
|
|
|
case MODE_DIRECT : |
|
|
// todo |
|
|
break; |
|
|
} |
|
|
|
|
556 |
} |
} |