1 |
/***************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - SAD calculation module (C part) - |
* sum of absolute difference |
|
* |
|
|
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
|
|
* 2002 Peter Ross <pross@xvid.org> |
|
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* This program is an implementation of a part of one or more MPEG-4 |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
24 |
* |
* |
25 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
28 |
|
* |
29 |
|
*************************************************************************/ |
30 |
|
|
31 |
|
/************************************************************************** |
32 |
|
* |
33 |
|
* History: |
34 |
* |
* |
35 |
****************************************************************************/ |
* 14.02.2002 added sad16bi_c() |
36 |
|
* 10.11.2001 initial version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
37 |
|
* |
38 |
|
*************************************************************************/ |
39 |
|
|
40 |
|
|
41 |
#include "../portab.h" |
#include "../portab.h" |
42 |
|
#include "../global.h" |
43 |
#include "sad.h" |
#include "sad.h" |
44 |
|
|
45 |
sad16FuncPtr sad16; |
sad16FuncPtr sad16; |
47 |
sad16biFuncPtr sad16bi; |
sad16biFuncPtr sad16bi; |
48 |
sad8biFuncPtr sad8bi; // not really sad16, but no difference in prototype |
sad8biFuncPtr sad8bi; // not really sad16, but no difference in prototype |
49 |
dev16FuncPtr dev16; |
dev16FuncPtr dev16; |
50 |
|
sad16vFuncPtr sad16v; |
51 |
|
|
52 |
sadInitFuncPtr sadInit; |
sadInitFuncPtr sadInit; |
53 |
|
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
|
|
|
|
#define MRSAD16_CORRFACTOR 8 |
|
|
uint32_t |
|
|
mrsad16_c(const uint8_t * const cur, |
|
|
const uint8_t * const ref, |
|
|
const uint32_t stride, |
|
|
const uint32_t best_sad) |
|
|
{ |
|
|
|
|
|
uint32_t sad = 0; |
|
|
int32_t mean = 0; |
|
|
uint32_t i, j; |
|
|
uint8_t const *ptr_cur = cur; |
|
|
uint8_t const *ptr_ref = ref; |
|
|
|
|
|
for (j = 0; j < 16; j++) { |
|
|
for (i = 0; i < 16; i++) { |
|
|
mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i)); |
|
|
} |
|
|
ptr_cur += stride; |
|
|
ptr_ref += stride; |
|
|
|
|
|
} |
|
|
mean /= 256; |
|
|
|
|
|
for (j = 0; j < 16; j++) { |
|
|
|
|
|
ptr_cur -= stride; |
|
|
ptr_ref -= stride; |
|
|
|
|
|
for (i = 0; i < 16; i++) { |
|
|
|
|
|
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i) - mean); |
|
|
if (sad >= best_sad) { |
|
|
return MRSAD16_CORRFACTOR * sad; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return MRSAD16_CORRFACTOR * sad; |
|
|
|
|
|
} |
|
|
|
|
54 |
|
|
55 |
uint32_t |
uint32_t |
56 |
sad16_c(const uint8_t * const cur, |
sad16_c(const uint8_t * const cur, |
60 |
{ |
{ |
61 |
|
|
62 |
uint32_t sad = 0; |
uint32_t sad = 0; |
63 |
uint32_t i, j; |
uint32_t j; |
64 |
uint8_t const *ptr_cur = cur; |
uint8_t const *ptr_cur = cur; |
65 |
uint8_t const *ptr_ref = ref; |
uint8_t const *ptr_ref = ref; |
66 |
|
|
67 |
for (j = 0; j < 16; j++) { |
for (j = 0; j < 16; j++) { |
68 |
|
sad += ABS(ptr_cur[0] - ptr_ref[0]); |
69 |
|
sad += ABS(ptr_cur[1] - ptr_ref[1]); |
70 |
|
sad += ABS(ptr_cur[2] - ptr_ref[2]); |
71 |
|
sad += ABS(ptr_cur[3] - ptr_ref[3]); |
72 |
|
sad += ABS(ptr_cur[4] - ptr_ref[4]); |
73 |
|
sad += ABS(ptr_cur[5] - ptr_ref[5]); |
74 |
|
sad += ABS(ptr_cur[6] - ptr_ref[6]); |
75 |
|
sad += ABS(ptr_cur[7] - ptr_ref[7]); |
76 |
|
sad += ABS(ptr_cur[8] - ptr_ref[8]); |
77 |
|
sad += ABS(ptr_cur[9] - ptr_ref[9]); |
78 |
|
sad += ABS(ptr_cur[10] - ptr_ref[10]); |
79 |
|
sad += ABS(ptr_cur[11] - ptr_ref[11]); |
80 |
|
sad += ABS(ptr_cur[12] - ptr_ref[12]); |
81 |
|
sad += ABS(ptr_cur[13] - ptr_ref[13]); |
82 |
|
sad += ABS(ptr_cur[14] - ptr_ref[14]); |
83 |
|
sad += ABS(ptr_cur[15] - ptr_ref[15]); |
84 |
|
|
85 |
for (i = 0; i < 16; i++) { |
if (sad >= best_sad) |
|
|
|
|
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
|
|
|
|
|
if (sad >= best_sad) { |
|
86 |
return sad; |
return sad; |
|
} |
|
|
|
|
|
|
|
|
} |
|
87 |
|
|
88 |
ptr_cur += stride; |
ptr_cur += stride; |
89 |
ptr_ref += stride; |
ptr_ref += stride; |
94 |
|
|
95 |
} |
} |
96 |
|
|
|
|
|
|
|
|
97 |
uint32_t |
uint32_t |
98 |
sad16bi_c(const uint8_t * const cur, |
sad16bi_c(const uint8_t * const cur, |
99 |
const uint8_t * const ref1, |
const uint8_t * const ref1, |
176 |
const uint32_t stride) |
const uint32_t stride) |
177 |
{ |
{ |
178 |
uint32_t sad = 0; |
uint32_t sad = 0; |
179 |
uint32_t i, j; |
uint32_t j; |
180 |
uint8_t const *ptr_cur = cur; |
uint8_t const *ptr_cur = cur; |
181 |
uint8_t const *ptr_ref = ref; |
uint8_t const *ptr_ref = ref; |
182 |
|
|
183 |
for (j = 0; j < 8; j++) { |
for (j = 0; j < 8; j++) { |
184 |
|
|
185 |
for (i = 0; i < 8; i++) { |
sad += ABS(ptr_cur[0] - ptr_ref[0]); |
186 |
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
sad += ABS(ptr_cur[1] - ptr_ref[1]); |
187 |
} |
sad += ABS(ptr_cur[2] - ptr_ref[2]); |
188 |
|
sad += ABS(ptr_cur[3] - ptr_ref[3]); |
189 |
|
sad += ABS(ptr_cur[4] - ptr_ref[4]); |
190 |
|
sad += ABS(ptr_cur[5] - ptr_ref[5]); |
191 |
|
sad += ABS(ptr_cur[6] - ptr_ref[6]); |
192 |
|
sad += ABS(ptr_cur[7] - ptr_ref[7]); |
193 |
|
|
194 |
ptr_cur += stride; |
ptr_cur += stride; |
195 |
ptr_ref += stride; |
ptr_ref += stride; |
200 |
} |
} |
201 |
|
|
202 |
|
|
|
|
|
|
|
|
203 |
/* average deviation from mean */ |
/* average deviation from mean */ |
204 |
|
|
205 |
uint32_t |
uint32_t |
235 |
|
|
236 |
return dev; |
return dev; |
237 |
} |
} |
238 |
|
|
239 |
|
uint32_t sad16v_c(const uint8_t * const cur, |
240 |
|
const uint8_t * const ref, |
241 |
|
const uint32_t stride, |
242 |
|
int32_t *sad) |
243 |
|
{ |
244 |
|
sad[0] = sad8(cur, ref, stride); |
245 |
|
sad[1] = sad8(cur + 8, ref + 8, stride); |
246 |
|
sad[2] = sad8(cur + 8*stride, ref + 8*stride, stride); |
247 |
|
sad[3] = sad8(cur + 8*stride + 8, ref + 8*stride + 8, stride); |
248 |
|
|
249 |
|
return sad[0]+sad[1]+sad[2]+sad[3]; |
250 |
|
} |
251 |
|
|
252 |
|
uint32_t sad32v_c(const uint8_t * const cur, |
253 |
|
const uint8_t * const ref, |
254 |
|
const uint32_t stride, |
255 |
|
int32_t *sad) |
256 |
|
{ |
257 |
|
sad[0] = sad16(cur, ref, stride, 256*4096); |
258 |
|
sad[1] = sad16(cur + 8, ref + 8, stride, 256*4096); |
259 |
|
sad[2] = sad16(cur + 8*stride, ref + 8*stride, stride, 256*4096); |
260 |
|
sad[3] = sad16(cur + 8*stride + 8, ref + 8*stride + 8, stride, 256*4096); |
261 |
|
|
262 |
|
return sad[0]+sad[1]+sad[2]+sad[3]; |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
#define MRSAD16_CORRFACTOR 8 |
268 |
|
uint32_t |
269 |
|
mrsad16_c(const uint8_t * const cur, |
270 |
|
const uint8_t * const ref, |
271 |
|
const uint32_t stride, |
272 |
|
const uint32_t best_sad) |
273 |
|
{ |
274 |
|
|
275 |
|
uint32_t sad = 0; |
276 |
|
int32_t mean = 0; |
277 |
|
uint32_t i, j; |
278 |
|
uint8_t const *ptr_cur = cur; |
279 |
|
uint8_t const *ptr_ref = ref; |
280 |
|
|
281 |
|
for (j = 0; j < 16; j++) { |
282 |
|
for (i = 0; i < 16; i++) { |
283 |
|
mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i)); |
284 |
|
} |
285 |
|
ptr_cur += stride; |
286 |
|
ptr_ref += stride; |
287 |
|
|
288 |
|
} |
289 |
|
mean /= 256; |
290 |
|
|
291 |
|
for (j = 0; j < 16; j++) { |
292 |
|
|
293 |
|
ptr_cur -= stride; |
294 |
|
ptr_ref -= stride; |
295 |
|
|
296 |
|
for (i = 0; i < 16; i++) { |
297 |
|
|
298 |
|
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i) - mean); |
299 |
|
if (sad >= best_sad) { |
300 |
|
return MRSAD16_CORRFACTOR * sad; |
301 |
|
} |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
|
return MRSAD16_CORRFACTOR * sad; |
306 |
|
|
307 |
|
} |
308 |
|
|
309 |
|
|