1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - SAD calculation module (C part) - |
* - Sum Of Absolute Difference related code - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
|
* 2002 Peter Ross <pross@xvid.org> |
|
7 |
* |
* |
8 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* This program is free software ; you can redistribute it and/or modify |
9 |
* |
* it under the terms of the GNU General Public License as published by |
|
* XviD is free software; you can redistribute it and/or modify it |
|
|
* under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
* (at your option) any later version. |
12 |
* |
* |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
|
* Under section 8 of the GNU General Public License, the copyright |
|
|
* holders of XVID explicitly forbid distribution in the following |
|
|
* countries: |
|
|
* |
|
|
* - Japan |
|
|
* - United States of America |
|
|
* |
|
|
* Linking XviD statically or dynamically with other modules is making a |
|
|
* combined work based on XviD. Thus, the terms and conditions of the |
|
|
* GNU General Public License cover the whole combination. |
|
|
* |
|
|
* As a special exception, the copyright holders of XviD give you |
|
|
* permission to link XviD with independent modules that communicate with |
|
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
|
|
* license terms of these independent modules, and to copy and distribute |
|
|
* the resulting combined work under terms of your choice, provided that |
|
|
* every copy of the combined work is accompanied by a complete copy of |
|
|
* the source code of XviD (the version of XviD used to produce the |
|
|
* combined work), being distributed under the terms of the GNU General |
|
|
* Public License plus this exception. An independent module is a module |
|
|
* which is not derived from or based on XviD. |
|
|
* |
|
|
* Note that people who make modified versions of XviD are not obligated |
|
|
* to grant this special exception for their modified versions; it is |
|
|
* their choice whether to do so. The GNU General Public License gives |
|
|
* permission to release a modified version without this exception; this |
|
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
22 |
* $Id$ |
* $Id$ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
26 |
#include "../portab.h" |
#include "../portab.h" |
27 |
|
#include "../global.h" |
28 |
#include "sad.h" |
#include "sad.h" |
29 |
|
|
30 |
|
#include <stdlib.h> |
31 |
|
|
32 |
sad16FuncPtr sad16; |
sad16FuncPtr sad16; |
33 |
sad8FuncPtr sad8; |
sad8FuncPtr sad8; |
34 |
sad16biFuncPtr sad16bi; |
sad16biFuncPtr sad16bi; |
35 |
sad8biFuncPtr sad8bi; /* not really sad16, but no difference in prototype */ |
sad8biFuncPtr sad8bi; /* not really sad16, but no difference in prototype */ |
36 |
dev16FuncPtr dev16; |
dev16FuncPtr dev16; |
37 |
|
sad16vFuncPtr sad16v; |
38 |
|
sse8Func_16bitPtr sse8_16bit; |
39 |
|
|
40 |
sadInitFuncPtr sadInit; |
sadInitFuncPtr sadInit; |
41 |
|
|
|
#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; |
|
|
|
|
|
} |
|
|
|
|
42 |
|
|
43 |
uint32_t |
uint32_t |
44 |
sad16_c(const uint8_t * const cur, |
sad16_c(const uint8_t * const cur, |
48 |
{ |
{ |
49 |
|
|
50 |
uint32_t sad = 0; |
uint32_t sad = 0; |
51 |
uint32_t i, j; |
uint32_t j; |
52 |
uint8_t const *ptr_cur = cur; |
uint8_t const *ptr_cur = cur; |
53 |
uint8_t const *ptr_ref = ref; |
uint8_t const *ptr_ref = ref; |
54 |
|
|
55 |
for (j = 0; j < 16; j++) { |
for (j = 0; j < 16; j++) { |
56 |
|
sad += abs(ptr_cur[0] - ptr_ref[0]); |
57 |
|
sad += abs(ptr_cur[1] - ptr_ref[1]); |
58 |
|
sad += abs(ptr_cur[2] - ptr_ref[2]); |
59 |
|
sad += abs(ptr_cur[3] - ptr_ref[3]); |
60 |
|
sad += abs(ptr_cur[4] - ptr_ref[4]); |
61 |
|
sad += abs(ptr_cur[5] - ptr_ref[5]); |
62 |
|
sad += abs(ptr_cur[6] - ptr_ref[6]); |
63 |
|
sad += abs(ptr_cur[7] - ptr_ref[7]); |
64 |
|
sad += abs(ptr_cur[8] - ptr_ref[8]); |
65 |
|
sad += abs(ptr_cur[9] - ptr_ref[9]); |
66 |
|
sad += abs(ptr_cur[10] - ptr_ref[10]); |
67 |
|
sad += abs(ptr_cur[11] - ptr_ref[11]); |
68 |
|
sad += abs(ptr_cur[12] - ptr_ref[12]); |
69 |
|
sad += abs(ptr_cur[13] - ptr_ref[13]); |
70 |
|
sad += abs(ptr_cur[14] - ptr_ref[14]); |
71 |
|
sad += abs(ptr_cur[15] - ptr_ref[15]); |
72 |
|
|
73 |
for (i = 0; i < 16; i++) { |
if (sad >= best_sad) |
|
|
|
|
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
|
|
|
|
|
if (sad >= best_sad) { |
|
74 |
return sad; |
return sad; |
|
} |
|
|
|
|
|
|
|
|
} |
|
75 |
|
|
76 |
ptr_cur += stride; |
ptr_cur += stride; |
77 |
ptr_ref += stride; |
ptr_ref += stride; |
82 |
|
|
83 |
} |
} |
84 |
|
|
|
|
|
|
|
|
85 |
uint32_t |
uint32_t |
86 |
sad16bi_c(const uint8_t * const cur, |
sad16bi_c(const uint8_t * const cur, |
87 |
const uint8_t * const ref1, |
const uint8_t * const ref1, |
99 |
|
|
100 |
for (i = 0; i < 16; i++) { |
for (i = 0; i < 16; i++) { |
101 |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
102 |
|
sad += abs(ptr_cur[i] - pixel); |
|
if (pixel < 0) { |
|
|
pixel = 0; |
|
|
} else if (pixel > 255) { |
|
|
pixel = 255; |
|
|
} |
|
|
|
|
|
sad += ABS(ptr_cur[i] - pixel); |
|
103 |
} |
} |
104 |
|
|
105 |
ptr_cur += stride; |
ptr_cur += stride; |
129 |
|
|
130 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 8; i++) { |
131 |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
132 |
|
sad += abs(ptr_cur[i] - pixel); |
|
if (pixel < 0) { |
|
|
pixel = 0; |
|
|
} else if (pixel > 255) { |
|
|
pixel = 255; |
|
|
} |
|
|
|
|
|
sad += ABS(ptr_cur[i] - pixel); |
|
133 |
} |
} |
134 |
|
|
135 |
ptr_cur += stride; |
ptr_cur += stride; |
150 |
const uint32_t stride) |
const uint32_t stride) |
151 |
{ |
{ |
152 |
uint32_t sad = 0; |
uint32_t sad = 0; |
153 |
uint32_t i, j; |
uint32_t j; |
154 |
uint8_t const *ptr_cur = cur; |
uint8_t const *ptr_cur = cur; |
155 |
uint8_t const *ptr_ref = ref; |
uint8_t const *ptr_ref = ref; |
156 |
|
|
157 |
for (j = 0; j < 8; j++) { |
for (j = 0; j < 8; j++) { |
158 |
|
|
159 |
for (i = 0; i < 8; i++) { |
sad += abs(ptr_cur[0] - ptr_ref[0]); |
160 |
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
sad += abs(ptr_cur[1] - ptr_ref[1]); |
161 |
} |
sad += abs(ptr_cur[2] - ptr_ref[2]); |
162 |
|
sad += abs(ptr_cur[3] - ptr_ref[3]); |
163 |
|
sad += abs(ptr_cur[4] - ptr_ref[4]); |
164 |
|
sad += abs(ptr_cur[5] - ptr_ref[5]); |
165 |
|
sad += abs(ptr_cur[6] - ptr_ref[6]); |
166 |
|
sad += abs(ptr_cur[7] - ptr_ref[7]); |
167 |
|
|
168 |
ptr_cur += stride; |
ptr_cur += stride; |
169 |
ptr_ref += stride; |
ptr_ref += stride; |
174 |
} |
} |
175 |
|
|
176 |
|
|
|
|
|
|
|
|
177 |
/* average deviation from mean */ |
/* average deviation from mean */ |
178 |
|
|
179 |
uint32_t |
uint32_t |
201 |
for (j = 0; j < 16; j++) { |
for (j = 0; j < 16; j++) { |
202 |
|
|
203 |
for (i = 0; i < 16; i++) |
for (i = 0; i < 16; i++) |
204 |
dev += ABS(*(ptr_cur + i) - (int32_t) mean); |
dev += abs(*(ptr_cur + i) - (int32_t) mean); |
205 |
|
|
206 |
ptr_cur += stride; |
ptr_cur += stride; |
207 |
|
|
209 |
|
|
210 |
return dev; |
return dev; |
211 |
} |
} |
212 |
|
|
213 |
|
uint32_t sad16v_c(const uint8_t * const cur, |
214 |
|
const uint8_t * const ref, |
215 |
|
const uint32_t stride, |
216 |
|
int32_t *sad) |
217 |
|
{ |
218 |
|
sad[0] = sad8(cur, ref, stride); |
219 |
|
sad[1] = sad8(cur + 8, ref + 8, stride); |
220 |
|
sad[2] = sad8(cur + 8*stride, ref + 8*stride, stride); |
221 |
|
sad[3] = sad8(cur + 8*stride + 8, ref + 8*stride + 8, stride); |
222 |
|
|
223 |
|
return sad[0]+sad[1]+sad[2]+sad[3]; |
224 |
|
} |
225 |
|
|
226 |
|
uint32_t sad32v_c(const uint8_t * const cur, |
227 |
|
const uint8_t * const ref, |
228 |
|
const uint32_t stride, |
229 |
|
int32_t *sad) |
230 |
|
{ |
231 |
|
sad[0] = sad16(cur, ref, stride, 256*4096); |
232 |
|
sad[1] = sad16(cur + 16, ref + 16, stride, 256*4096); |
233 |
|
sad[2] = sad16(cur + 16*stride, ref + 16*stride, stride, 256*4096); |
234 |
|
sad[3] = sad16(cur + 16*stride + 16, ref + 16*stride + 16, stride, 256*4096); |
235 |
|
|
236 |
|
return sad[0]+sad[1]+sad[2]+sad[3]; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
#define MRSAD16_CORRFACTOR 8 |
242 |
|
uint32_t |
243 |
|
mrsad16_c(const uint8_t * const cur, |
244 |
|
const uint8_t * const ref, |
245 |
|
const uint32_t stride, |
246 |
|
const uint32_t best_sad) |
247 |
|
{ |
248 |
|
|
249 |
|
uint32_t sad = 0; |
250 |
|
int32_t mean = 0; |
251 |
|
uint32_t i, j; |
252 |
|
uint8_t const *ptr_cur = cur; |
253 |
|
uint8_t const *ptr_ref = ref; |
254 |
|
|
255 |
|
for (j = 0; j < 16; j++) { |
256 |
|
for (i = 0; i < 16; i++) { |
257 |
|
mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i)); |
258 |
|
} |
259 |
|
ptr_cur += stride; |
260 |
|
ptr_ref += stride; |
261 |
|
|
262 |
|
} |
263 |
|
mean /= 256; |
264 |
|
|
265 |
|
for (j = 0; j < 16; j++) { |
266 |
|
|
267 |
|
ptr_cur -= stride; |
268 |
|
ptr_ref -= stride; |
269 |
|
|
270 |
|
for (i = 0; i < 16; i++) { |
271 |
|
|
272 |
|
sad += abs(*(ptr_cur + i) - *(ptr_ref + i) - mean); |
273 |
|
if (sad >= best_sad) { |
274 |
|
return MRSAD16_CORRFACTOR * sad; |
275 |
|
} |
276 |
|
} |
277 |
|
} |
278 |
|
|
279 |
|
return MRSAD16_CORRFACTOR * sad; |
280 |
|
} |
281 |
|
|
282 |
|
uint32_t |
283 |
|
sse8_16bit_c(const int16_t * b1, |
284 |
|
const int16_t * b2, |
285 |
|
const uint32_t stride) |
286 |
|
{ |
287 |
|
int i; |
288 |
|
int sse = 0; |
289 |
|
|
290 |
|
for (i=0; i<8; i++) { |
291 |
|
sse += (b1[0] - b2[0])*(b1[0] - b2[0]); |
292 |
|
sse += (b1[1] - b2[1])*(b1[1] - b2[1]); |
293 |
|
sse += (b1[2] - b2[2])*(b1[2] - b2[2]); |
294 |
|
sse += (b1[3] - b2[3])*(b1[3] - b2[3]); |
295 |
|
sse += (b1[4] - b2[4])*(b1[4] - b2[4]); |
296 |
|
sse += (b1[5] - b2[5])*(b1[5] - b2[5]); |
297 |
|
sse += (b1[6] - b2[6])*(b1[6] - b2[6]); |
298 |
|
sse += (b1[7] - b2[7])*(b1[7] - b2[7]); |
299 |
|
|
300 |
|
b1 = (const int16_t*)((int8_t*)b1+stride); |
301 |
|
b2 = (const int16_t*)((int8_t*)b2+stride); |
302 |
|
} |
303 |
|
|
304 |
|
return(sse); |
305 |
|
} |