1 |
/************************************************************************** |
2 |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
4 |
* sum of absolute difference |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
* |
20 |
*************************************************************************/ |
21 |
|
22 |
/************************************************************************** |
23 |
* |
24 |
* History: |
25 |
* |
26 |
* 14.02.2002 added sad16bi_c() |
27 |
* 10.11.2001 initial version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
28 |
* |
29 |
*************************************************************************/ |
30 |
|
31 |
|
32 |
#include "../portab.h" |
33 |
#include "sad.h" |
34 |
|
35 |
sad16FuncPtr sad16; |
36 |
sad8FuncPtr sad8; |
37 |
sad16biFuncPtr sad16bi; |
38 |
sad8biFuncPtr sad8bi; // not really sad16, but no difference in prototype |
39 |
dev16FuncPtr dev16; |
40 |
|
41 |
sadInitFuncPtr sadInit; |
42 |
|
43 |
#define ABS(X) (((X)>0)?(X):-(X)) |
44 |
|
45 |
uint32_t |
46 |
sad8FuncPtr sad8; |
47 |
const uint8_t * const ref, |
48 |
|
49 |
const uint32_t best_sad) |
50 |
{ |
51 |
|
52 |
uint32_t sad = 0; |
53 |
int32_t mean = 0; |
54 |
uint32_t i, j; |
55 |
uint8_t const *ptr_cur = cur; |
56 |
uint8_t const *ptr_ref = ref; |
57 |
|
58 |
for (j = 0; j < 16; j++) { |
59 |
for (i = 0; i < 16; i++) { |
60 |
mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i)); |
61 |
} |
62 |
ptr_cur += stride; |
63 |
ptr_ref += stride; |
64 |
|
65 |
} |
66 |
mean /= 256; |
67 |
|
68 |
for (j = 0; j < 16; j++) { |
69 |
|
70 |
ptr_cur -= stride; |
71 |
ptr_ref -= stride; |
72 |
|
73 |
for (i = 0; i < 16; i++) { |
74 |
|
75 |
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i) - mean); |
76 |
if (sad >= best_sad) { |
77 |
return MRSAD16_CORRFACTOR * sad; |
78 |
} |
79 |
} |
80 |
} |
81 |
|
82 |
return MRSAD16_CORRFACTOR * sad; |
83 |
|
84 |
} |
85 |
|
86 |
|
87 |
uint32_t |
88 |
sad16_c(const uint8_t * const cur, |
89 |
const uint8_t * const ref, |
90 |
const uint32_t stride, |
91 |
const uint32_t best_sad) |
92 |
{ |
93 |
|
94 |
uint32_t sad = 0; |
95 |
uint32_t i, j; |
96 |
uint8_t const *ptr_cur = cur; |
97 |
uint8_t const *ptr_ref = ref; |
98 |
|
99 |
for (j = 0; j < 16; j++) { |
100 |
|
101 |
for (i = 0; i < 16; i++) { |
102 |
|
103 |
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
104 |
|
105 |
if (sad >= best_sad) { |
106 |
return sad; |
107 |
} |
108 |
|
109 |
|
110 |
} |
111 |
|
112 |
ptr_cur += stride; |
113 |
ptr_ref += stride; |
114 |
|
115 |
} |
116 |
|
117 |
return sad; |
118 |
|
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
uint32_t |
124 |
sad16bi_c(const uint8_t * const cur, |
125 |
const uint8_t * const ref1, |
126 |
const uint8_t * const ref2, |
127 |
const uint32_t stride) |
128 |
{ |
129 |
|
130 |
uint32_t sad = 0; |
131 |
uint32_t i, j; |
132 |
uint8_t const *ptr_cur = cur; |
133 |
uint8_t const *ptr_ref1 = ref1; |
134 |
uint8_t const *ptr_ref2 = ref2; |
135 |
|
136 |
for (j = 0; j < 16; j++) { |
137 |
|
138 |
for (i = 0; i < 16; i++) { |
139 |
int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2; |
140 |
|
141 |
if (pixel < 0) { |
142 |
pixel = 0; |
143 |
} else if (pixel > 255) { |
144 |
pixel = 255; |
145 |
} |
146 |
|
147 |
sad += ABS(ptr_cur[i] - pixel); |
148 |
} |
149 |
|
150 |
ptr_cur += stride; |
151 |
ptr_ref1 += stride; |
152 |
ptr_ref2 += stride; |
153 |
|
154 |
} |
155 |
|
156 |
return sad; |
157 |
|
158 |
} |
159 |
|
160 |
uint32_t |
161 |
sad8bi_c(const uint8_t * const cur, |
162 |
const uint8_t * const ref1, |
163 |
const uint8_t * const ref2, |
164 |
const uint32_t stride) |
165 |
{ |
166 |
|
167 |
uint32_t sad = 0; |
168 |
uint8_t const *ptr_cur = cur; |
169 |
uint8_t const *ptr_ref = ref; |
170 |
|
171 |
for (j = 0; j < 8; j++) { |
172 |
|
173 |
for (i = 0; i < 8; i++) { |
174 |
sad += ABS(*(ptr_cur + i) - *(ptr_ref + i)); |
175 |
} |
176 |
|
177 |
ptr_cur += stride; |
178 |
ptr_ref += stride; |
179 |
|
180 |
} |
181 |
|
182 |
return sad; |
183 |
} |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
/* average deviation from mean */ |
189 |
|
190 |
uint32_t |
191 |
dev16_c(const uint8_t * const cur, |
192 |
const uint32_t stride) |
193 |
{ |
194 |
|
195 |
uint32_t mean = 0; |
196 |
uint32_t dev = 0; |
197 |
uint32_t i, j; |
198 |
uint8_t const *ptr_cur = cur; |
199 |
|
200 |
for (j = 0; j < 16; j++) { |
201 |
|
202 |
for (i = 0; i < 16; i++) |
203 |
mean += *(ptr_cur + i); |
204 |
|
205 |
ptr_cur += stride; |
206 |
|
207 |
} |
208 |
|
209 |
mean /= (16 * 16); |
210 |
ptr_cur = cur; |
211 |
|
212 |
for (j = 0; j < 16; j++) { |
213 |
|
214 |
for (i = 0; i < 16; i++) |
215 |
dev += ABS(*(ptr_cur + i) - (int32_t) mean); |
216 |
|
217 |
ptr_cur += stride; |
218 |
|
219 |
} |
220 |
|
221 |
return dev; |
222 |
} |