3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* sum of absolute difference |
* sum of absolute difference |
5 |
* |
* |
6 |
|
* 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 |
8 |
|
* to use this software module in hardware or software products are |
9 |
|
* advised that its use may infringe existing patents or copyrights, and |
10 |
|
* any such use would be at such party's own risk. The original |
11 |
|
* developer of this software module and his/her company, and subsequent |
12 |
|
* editors and their companies, will have no liability for use of this |
13 |
|
* software or modifications or derivatives thereof. |
14 |
|
* |
15 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
16 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
17 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
39 |
|
|
40 |
|
|
41 |
#include "../portab.h" |
#include "../portab.h" |
42 |
|
#include "../global.h" |
43 |
#include "sad.h" |
#include "sad.h" |
44 |
|
|
45 |
|
#include <stdlib.h> |
46 |
|
|
47 |
sad16FuncPtr sad16; |
sad16FuncPtr sad16; |
48 |
sad8FuncPtr sad8; |
sad8FuncPtr sad8; |
49 |
sad16biFuncPtr sad16bi; |
sad16biFuncPtr sad16bi; |
50 |
sad8biFuncPtr sad8bi; // not really sad16, but no difference in prototype |
sad8biFuncPtr sad8bi; /* not really sad16, but no difference in prototype */ |
51 |
dev16FuncPtr dev16; |
dev16FuncPtr dev16; |
52 |
|
sad16vFuncPtr sad16v; |
53 |
|
|
54 |
sadInitFuncPtr sadInit; |
sadInitFuncPtr sadInit; |
55 |
|
|
|
#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; |
|
|
|
|
|
} |
|
|
|
|
56 |
|
|
57 |
uint32_t |
uint32_t |
58 |
sad16_c(const uint8_t * const cur, |
sad16_c(const uint8_t * const cur, |
62 |
{ |
{ |
63 |
|
|
64 |
uint32_t sad = 0; |
uint32_t sad = 0; |
65 |
uint32_t i, j; |
uint32_t j; |
66 |
uint8_t const *ptr_cur = cur; |
uint8_t const *ptr_cur = cur; |
67 |
uint8_t const *ptr_ref = ref; |
uint8_t const *ptr_ref = ref; |
68 |
|
|
69 |
for (j = 0; j < 16; j++) { |
for (j = 0; j < 16; j++) { |
70 |
|
sad += abs(ptr_cur[0] - ptr_ref[0]); |
71 |
|
sad += abs(ptr_cur[1] - ptr_ref[1]); |
72 |
|
sad += abs(ptr_cur[2] - ptr_ref[2]); |
73 |
|
sad += abs(ptr_cur[3] - ptr_ref[3]); |
74 |
|
sad += abs(ptr_cur[4] - ptr_ref[4]); |
75 |
|
sad += abs(ptr_cur[5] - ptr_ref[5]); |
76 |
|
sad += abs(ptr_cur[6] - ptr_ref[6]); |
77 |
|
sad += abs(ptr_cur[7] - ptr_ref[7]); |
78 |
|
sad += abs(ptr_cur[8] - ptr_ref[8]); |
79 |
|
sad += abs(ptr_cur[9] - ptr_ref[9]); |
80 |
|
sad += abs(ptr_cur[10] - ptr_ref[10]); |
81 |
|
sad += abs(ptr_cur[11] - ptr_ref[11]); |
82 |
|
sad += abs(ptr_cur[12] - ptr_ref[12]); |
83 |
|
sad += abs(ptr_cur[13] - ptr_ref[13]); |
84 |
|
sad += abs(ptr_cur[14] - ptr_ref[14]); |
85 |
|
sad += abs(ptr_cur[15] - ptr_ref[15]); |
86 |
|
|
87 |
for (i = 0; i < 16; i++) { |
if (sad >= best_sad) |
|
|
|
|
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
|
|
|
|
|
if (sad >= best_sad) { |
|
88 |
return sad; |
return sad; |
|
} |
|
|
|
|
|
|
|
|
} |
|
89 |
|
|
90 |
ptr_cur += stride; |
ptr_cur += stride; |
91 |
ptr_ref += stride; |
ptr_ref += stride; |
96 |
|
|
97 |
} |
} |
98 |
|
|
|
|
|
|
|
|
99 |
uint32_t |
uint32_t |
100 |
sad16bi_c(const uint8_t * const cur, |
sad16bi_c(const uint8_t * const cur, |
101 |
const uint8_t * const ref1, |
const uint8_t * const ref1, |
113 |
|
|
114 |
for (i = 0; i < 16; i++) { |
for (i = 0; i < 16; i++) { |
115 |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
116 |
|
sad += abs(ptr_cur[i] - pixel); |
|
if (pixel < 0) { |
|
|
pixel = 0; |
|
|
} else if (pixel > 255) { |
|
|
pixel = 255; |
|
|
} |
|
|
|
|
|
sad += ABS(ptr_cur[i] - pixel); |
|
117 |
} |
} |
118 |
|
|
119 |
ptr_cur += stride; |
ptr_cur += stride; |
143 |
|
|
144 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 8; i++) { |
145 |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
146 |
|
sad += abs(ptr_cur[i] - pixel); |
|
if (pixel < 0) { |
|
|
pixel = 0; |
|
|
} else if (pixel > 255) { |
|
|
pixel = 255; |
|
|
} |
|
|
|
|
|
sad += ABS(ptr_cur[i] - pixel); |
|
147 |
} |
} |
148 |
|
|
149 |
ptr_cur += stride; |
ptr_cur += stride; |
164 |
const uint32_t stride) |
const uint32_t stride) |
165 |
{ |
{ |
166 |
uint32_t sad = 0; |
uint32_t sad = 0; |
167 |
uint32_t i, j; |
uint32_t j; |
168 |
uint8_t const *ptr_cur = cur; |
uint8_t const *ptr_cur = cur; |
169 |
uint8_t const *ptr_ref = ref; |
uint8_t const *ptr_ref = ref; |
170 |
|
|
171 |
for (j = 0; j < 8; j++) { |
for (j = 0; j < 8; j++) { |
172 |
|
|
173 |
for (i = 0; i < 8; i++) { |
sad += abs(ptr_cur[0] - ptr_ref[0]); |
174 |
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
sad += abs(ptr_cur[1] - ptr_ref[1]); |
175 |
} |
sad += abs(ptr_cur[2] - ptr_ref[2]); |
176 |
|
sad += abs(ptr_cur[3] - ptr_ref[3]); |
177 |
|
sad += abs(ptr_cur[4] - ptr_ref[4]); |
178 |
|
sad += abs(ptr_cur[5] - ptr_ref[5]); |
179 |
|
sad += abs(ptr_cur[6] - ptr_ref[6]); |
180 |
|
sad += abs(ptr_cur[7] - ptr_ref[7]); |
181 |
|
|
182 |
ptr_cur += stride; |
ptr_cur += stride; |
183 |
ptr_ref += stride; |
ptr_ref += stride; |
188 |
} |
} |
189 |
|
|
190 |
|
|
|
|
|
|
|
|
191 |
/* average deviation from mean */ |
/* average deviation from mean */ |
192 |
|
|
193 |
uint32_t |
uint32_t |
215 |
for (j = 0; j < 16; j++) { |
for (j = 0; j < 16; j++) { |
216 |
|
|
217 |
for (i = 0; i < 16; i++) |
for (i = 0; i < 16; i++) |
218 |
dev += ABS(*(ptr_cur + i) - (int32_t) mean); |
dev += abs(*(ptr_cur + i) - (int32_t) mean); |
219 |
|
|
220 |
ptr_cur += stride; |
ptr_cur += stride; |
221 |
|
|
223 |
|
|
224 |
return dev; |
return dev; |
225 |
} |
} |
226 |
|
|
227 |
|
uint32_t sad16v_c(const uint8_t * const cur, |
228 |
|
const uint8_t * const ref, |
229 |
|
const uint32_t stride, |
230 |
|
int32_t *sad) |
231 |
|
{ |
232 |
|
sad[0] = sad8(cur, ref, stride); |
233 |
|
sad[1] = sad8(cur + 8, ref + 8, stride); |
234 |
|
sad[2] = sad8(cur + 8*stride, ref + 8*stride, stride); |
235 |
|
sad[3] = sad8(cur + 8*stride + 8, ref + 8*stride + 8, stride); |
236 |
|
|
237 |
|
return sad[0]+sad[1]+sad[2]+sad[3]; |
238 |
|
} |
239 |
|
|
240 |
|
uint32_t sad32v_c(const uint8_t * const cur, |
241 |
|
const uint8_t * const ref, |
242 |
|
const uint32_t stride, |
243 |
|
int32_t *sad) |
244 |
|
{ |
245 |
|
sad[0] = sad16(cur, ref, stride, 256*4096); |
246 |
|
sad[1] = sad16(cur + 8, ref + 8, stride, 256*4096); |
247 |
|
sad[2] = sad16(cur + 8*stride, ref + 8*stride, stride, 256*4096); |
248 |
|
sad[3] = sad16(cur + 8*stride + 8, ref + 8*stride + 8, stride, 256*4096); |
249 |
|
|
250 |
|
return sad[0]+sad[1]+sad[2]+sad[3]; |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
#define MRSAD16_CORRFACTOR 8 |
256 |
|
uint32_t |
257 |
|
mrsad16_c(const uint8_t * const cur, |
258 |
|
const uint8_t * const ref, |
259 |
|
const uint32_t stride, |
260 |
|
const uint32_t best_sad) |
261 |
|
{ |
262 |
|
|
263 |
|
uint32_t sad = 0; |
264 |
|
int32_t mean = 0; |
265 |
|
uint32_t i, j; |
266 |
|
uint8_t const *ptr_cur = cur; |
267 |
|
uint8_t const *ptr_ref = ref; |
268 |
|
|
269 |
|
for (j = 0; j < 16; j++) { |
270 |
|
for (i = 0; i < 16; i++) { |
271 |
|
mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i)); |
272 |
|
} |
273 |
|
ptr_cur += stride; |
274 |
|
ptr_ref += stride; |
275 |
|
|
276 |
|
} |
277 |
|
mean /= 256; |
278 |
|
|
279 |
|
for (j = 0; j < 16; j++) { |
280 |
|
|
281 |
|
ptr_cur -= stride; |
282 |
|
ptr_ref -= stride; |
283 |
|
|
284 |
|
for (i = 0; i < 16; i++) { |
285 |
|
|
286 |
|
sad += abs(*(ptr_cur + i) - *(ptr_ref + i) - mean); |
287 |
|
if (sad >= best_sad) { |
288 |
|
return MRSAD16_CORRFACTOR * sad; |
289 |
|
} |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
return MRSAD16_CORRFACTOR * sad; |
294 |
|
|
295 |
|
} |
296 |
|
|
297 |
|
|