1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Motion Compensation module - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Peter Ross <pross@xvid.org> |
7 |
|
* |
8 |
|
* This program is an implementation of a part of one or more MPEG-4 |
9 |
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
|
* to use this software module in hardware or software products are |
11 |
|
* advised that its use may infringe existing patents or copyrights, and |
12 |
|
* any such use would be at such party's own risk. The original |
13 |
|
* developer of this software module and his/her company, and subsequent |
14 |
|
* editors and their companies, will have no liability for use of this |
15 |
|
* software or modifications or derivatives thereof. |
16 |
|
* |
17 |
|
* This program is free software; you can redistribute it and/or modify |
18 |
|
* it under the terms of the GNU General Public License as published by |
19 |
|
* the Free Software Foundation; either version 2 of the License, or |
20 |
|
* (at your option) any later version. |
21 |
|
* |
22 |
|
* This program is distributed in the hope that it will be useful, |
23 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 |
|
* GNU General Public License for more details. |
26 |
|
* |
27 |
|
* You should have received a copy of the GNU General Public License |
28 |
|
* along with this program; if not, write to the Free Software |
29 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
30 |
|
* |
31 |
|
*************************************************************************/ |
32 |
|
|
33 |
#include "../encoder.h" |
#include "../encoder.h" |
34 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
35 |
#include "../image/interpolate8x8.h" |
#include "../image/interpolate8x8.h" |
36 |
#include "../utils/timer.h" |
#include "../utils/timer.h" |
37 |
|
#include "motion.h" |
38 |
|
|
39 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
40 |
#define SIGN(X) (((X)>0)?1:-1) |
#define SIGN(X) (((X)>0)?1:-1) |
41 |
|
|
42 |
static __inline void compensate8x8_halfpel( |
static __inline void |
43 |
int16_t * const dct_codes, |
compensate8x8_halfpel(int16_t * const dct_codes, |
44 |
uint8_t * const cur, |
uint8_t * const cur, |
45 |
const uint8_t * const ref, |
const uint8_t * const ref, |
46 |
const uint8_t * const refh, |
const uint8_t * const refh, |
47 |
const uint8_t * const refv, |
const uint8_t * const refv, |
48 |
const uint8_t * const refhv, |
const uint8_t * const refhv, |
49 |
const uint32_t x, const uint32_t y, |
const uint32_t x, |
50 |
const int32_t dx, const int dy, |
const uint32_t y, |
51 |
|
const int32_t dx, |
52 |
|
const int dy, |
53 |
const uint32_t stride) |
const uint32_t stride) |
54 |
{ |
{ |
55 |
int32_t ddx,ddy; |
int32_t ddx,ddy; |
60 |
ddx = dx/2; |
ddx = dx/2; |
61 |
ddy = dy/2; |
ddy = dy/2; |
62 |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
63 |
ref + (y+ddy)*stride + x+ddx, stride); |
ref + (int) ((y + ddy) * stride + x + ddx), stride); |
64 |
break; |
break; |
65 |
|
|
66 |
case 1 : |
case 1 : |
67 |
ddx = dx/2; |
ddx = dx/2; |
68 |
ddy = (dy-1)/2; |
ddy = (dy-1)/2; |
69 |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
70 |
refv + (y+ddy)*stride + x+ddx, stride); |
refv + (int) ((y + ddy) * stride + x + ddx), stride); |
71 |
break; |
break; |
72 |
|
|
73 |
case 2 : |
case 2 : |
74 |
ddx = (dx-1)/2; |
ddx = (dx-1)/2; |
75 |
ddy = dy/2; |
ddy = dy/2; |
76 |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
77 |
refh + (y+ddy)*stride + x+ddx, stride); |
refh + (int) ((y + ddy) * stride + x + ddx), stride); |
78 |
break; |
break; |
79 |
|
|
80 |
default : // case 3: |
default : // case 3: |
81 |
ddx = (dx-1)/2; |
ddx = (dx-1)/2; |
82 |
ddy = (dy-1)/2; |
ddy = (dy-1)/2; |
83 |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
transfer_8to16sub(dct_codes, cur + y*stride + x, |
84 |
refhv + (y+ddy)*stride + x+ddx, stride); |
refhv + (int) ((y + ddy) * stride + x + ddx), stride); |
85 |
break; |
break; |
86 |
} |
} |
87 |
} |
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
void MBMotionCompensation( |
void |
92 |
MACROBLOCK * const mb, |
MBMotionCompensation(MACROBLOCK * const mb, |
93 |
const uint32_t i, |
const uint32_t i, |
94 |
const uint32_t j, |
const uint32_t j, |
95 |
const IMAGE * const ref, |
const IMAGE * const ref, |
107 |
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
108 |
|
|
109 |
|
|
110 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
|
{ |
|
111 |
int32_t dx = mb->mvs[0].x; |
int32_t dx = mb->mvs[0].x; |
112 |
int32_t dy = mb->mvs[0].y; |
int32_t dy = mb->mvs[0].y; |
113 |
|
|
114 |
compensate8x8_halfpel(&dct_codes[0*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
compensate8x8_halfpel(&dct_codes[0 * 64], cur->y, ref->y, refh->y, |
115 |
16*i, 16*j, dx, dy, edged_width); |
refv->y, refhv->y, 16 * i, 16 * j, dx, dy, |
116 |
compensate8x8_halfpel(&dct_codes[1*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
edged_width); |
117 |
16*i + 8, 16*j, dx, dy, edged_width); |
compensate8x8_halfpel(&dct_codes[1 * 64], cur->y, ref->y, refh->y, |
118 |
compensate8x8_halfpel(&dct_codes[2*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
refv->y, refhv->y, 16 * i + 8, 16 * j, dx, dy, |
119 |
16*i, 16*j + 8, dx, dy, edged_width); |
edged_width); |
120 |
compensate8x8_halfpel(&dct_codes[3*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
compensate8x8_halfpel(&dct_codes[2 * 64], cur->y, ref->y, refh->y, |
121 |
16*i + 8, 16*j + 8, dx, dy, edged_width); |
refv->y, refhv->y, 16 * i, 16 * j + 8, dx, dy, |
122 |
|
edged_width); |
123 |
|
compensate8x8_halfpel(&dct_codes[3 * 64], cur->y, ref->y, refh->y, |
124 |
|
refv->y, refhv->y, 16 * i + 8, 16 * j + 8, dx, |
125 |
|
dy, edged_width); |
126 |
|
|
127 |
dx = (dx & 3) ? (dx >> 1) | 1 : dx / 2; |
dx = (dx & 3) ? (dx >> 1) | 1 : dx / 2; |
128 |
dy = (dy & 3) ? (dy >> 1) | 1 : dy / 2; |
dy = (dy & 3) ? (dy >> 1) | 1 : dy / 2; |
129 |
|
|
130 |
/* uv-image-based compensation |
/* uv-image-based compensation */ |
|
compensate8x8_halfpel(dct_codes[4], cur->u, ref->u, refh->u, refv->u, refhv->u, |
|
|
8*i, 8*j, dx, dy, edged_width/2); |
|
|
compensate8x8_halfpel(dct_codes[5], cur->v, ref->v, refh->v, refv->v, refhv->v, |
|
|
8*i, 8*j, dx, dy, edged_width/2); */ |
|
131 |
|
|
132 |
/* uv-block-based compensation */ |
interpolate8x8_switch(refv->u, ref->u, 8 * i, 8 * j, dx, dy, |
133 |
interpolate8x8_switch(refv->u, ref->u, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
edged_width / 2, rounding); |
134 |
transfer_8to16sub(&dct_codes[4*64], |
transfer_8to16sub(&dct_codes[4*64], |
135 |
cur->u + 8*j*edged_width/2 + 8*i, |
cur->u + 8*j*edged_width/2 + 8*i, |
136 |
refv->u + 8*j*edged_width/2 + 8*i, edged_width/2); |
refv->u + 8 * j * edged_width / 2 + 8 * i, |
137 |
|
edged_width / 2); |
138 |
|
|
139 |
interpolate8x8_switch(refv->v, ref->v, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
interpolate8x8_switch(refv->v, ref->v, 8 * i, 8 * j, dx, dy, |
140 |
|
edged_width / 2, rounding); |
141 |
transfer_8to16sub(&dct_codes[5*64], |
transfer_8to16sub(&dct_codes[5*64], |
142 |
cur->v + 8*j*edged_width/2 + 8*i, |
cur->v + 8*j*edged_width/2 + 8*i, |
143 |
refv->v + 8*j*edged_width/2 + 8*i, edged_width/2); |
refv->v + 8 * j * edged_width / 2 + 8 * i, |
144 |
|
edged_width / 2); |
145 |
|
|
146 |
} |
} else // mode == MODE_INTER4V |
|
else // mode == MODE_INTER4V |
|
147 |
{ |
{ |
148 |
int32_t sum, dx, dy; |
int32_t sum, dx, dy; |
149 |
|
|
150 |
compensate8x8_halfpel(&dct_codes[0*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
compensate8x8_halfpel(&dct_codes[0 * 64], cur->y, ref->y, refh->y, |
151 |
16*i, 16*j, mb->mvs[0].x, mb->mvs[0].y, edged_width); |
refv->y, refhv->y, 16 * i, 16 * j, mb->mvs[0].x, |
152 |
compensate8x8_halfpel(&dct_codes[1*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
mb->mvs[0].y, edged_width); |
153 |
16*i + 8, 16*j, mb->mvs[1].x, mb->mvs[1].y, edged_width); |
compensate8x8_halfpel(&dct_codes[1 * 64], cur->y, ref->y, refh->y, |
154 |
compensate8x8_halfpel(&dct_codes[2*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
refv->y, refhv->y, 16 * i + 8, 16 * j, |
155 |
16*i, 16*j + 8, mb->mvs[2].x, mb->mvs[2].y, edged_width); |
mb->mvs[1].x, mb->mvs[1].y, edged_width); |
156 |
compensate8x8_halfpel(&dct_codes[3*64], cur->y, ref->y, refh->y, refv->y, refhv->y, |
compensate8x8_halfpel(&dct_codes[2 * 64], cur->y, ref->y, refh->y, |
157 |
16*i + 8, 16*j + 8, mb->mvs[3].x, mb->mvs[3].y, edged_width); |
refv->y, refhv->y, 16 * i, 16 * j + 8, |
158 |
|
mb->mvs[2].x, mb->mvs[2].y, edged_width); |
159 |
|
compensate8x8_halfpel(&dct_codes[3 * 64], cur->y, ref->y, refh->y, |
160 |
|
refv->y, refhv->y, 16 * i + 8, 16 * j + 8, |
161 |
|
mb->mvs[3].x, mb->mvs[3].y, edged_width); |
162 |
|
|
163 |
sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x; |
sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x; |
164 |
dx = (sum ? SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
dx = (sum ? SIGN(sum) * |
165 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
166 |
|
|
167 |
sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y; |
sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y; |
168 |
dy = (sum ? SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
dy = (sum ? SIGN(sum) * |
169 |
|
(roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) : 0); |
|
/* uv-image-based compensation |
|
|
compensate8x8_halfpel(dct_codes[4], cur->u, ref->u, refh->u, refv->u, refhv->u, |
|
|
8*i, 8*j, dx, dy, edged_width/2); |
|
|
compensate8x8_halfpel(dct_codes[5], cur->v, ref->v, refh->v, refv->v, refhv->v, |
|
|
8*i, 8*j, dx, dy, edged_width/2); */ |
|
170 |
|
|
171 |
/* uv-block-based compensation */ |
/* uv-block-based compensation */ |
172 |
interpolate8x8_switch(refv->u, ref->u, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
interpolate8x8_switch(refv->u, ref->u, 8 * i, 8 * j, dx, dy, |
173 |
|
edged_width / 2, rounding); |
174 |
transfer_8to16sub(&dct_codes[4*64], |
transfer_8to16sub(&dct_codes[4*64], |
175 |
cur->u + 8*j*edged_width/2 + 8*i, |
cur->u + 8*j*edged_width/2 + 8*i, |
176 |
refv->u + 8*j*edged_width/2 + 8*i, edged_width/2); |
refv->u + 8 * j * edged_width / 2 + 8 * i, |
177 |
|
edged_width / 2); |
178 |
|
|
179 |
interpolate8x8_switch(refv->v, ref->v, 8*i, 8*j, dx, dy, edged_width/2, rounding); |
interpolate8x8_switch(refv->v, ref->v, 8 * i, 8 * j, dx, dy, |
180 |
|
edged_width / 2, rounding); |
181 |
transfer_8to16sub(&dct_codes[5*64], |
transfer_8to16sub(&dct_codes[5*64], |
182 |
cur->v + 8*j*edged_width/2 + 8*i, |
cur->v + 8*j*edged_width/2 + 8*i, |
183 |
refv->v + 8*j*edged_width/2 + 8*i, edged_width/2); |
refv->v + 8 * j * edged_width / 2 + 8 * i, |
184 |
|
edged_width / 2); |
185 |
} |
} |
186 |
} |
} |