[svn] / trunk / xvidcore / src / motion / sad.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/motion/sad.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 652 - (view) (download)

1 : chl 430 /*****************************************************************************
2 : edgomez 195 *
3 : chl 430 * XVID MPEG-4 VIDEO CODEC
4 :     * - SAD calculation module (C part) -
5 : edgomez 195 *
6 : edgomez 603 * Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7 : edgomez 605 * 2002 Peter Ross <pross@xvid.org>
8 : edgomez 195 *
9 : edgomez 652 * This file is part of XviD, a free MPEG-4 video encoder/decoder
10 : edgomez 195 *
11 : edgomez 652 * XviD is free software; you can redistribute it and/or modify it
12 :     * under the terms of the GNU General Public License as published by
13 : chl 430 * the Free Software Foundation; either version 2 of the License, or
14 :     * (at your option) any later version.
15 : edgomez 195 *
16 : chl 430 * This program is distributed in the hope that it will be useful,
17 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 :     * GNU General Public License for more details.
20 : edgomez 195 *
21 : chl 430 * You should have received a copy of the GNU General Public License
22 :     * along with this program; if not, write to the Free Software
23 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 : edgomez 195 *
25 : edgomez 652 * Under section 8 of the GNU General Public License, the copyright
26 :     * holders of XVID explicitly forbid distribution in the following
27 :     * countries:
28 :     *
29 :     * - Japan
30 :     * - United States of America
31 :     *
32 :     * Linking XviD statically or dynamically with other modules is making a
33 :     * combined work based on XviD. Thus, the terms and conditions of the
34 :     * GNU General Public License cover the whole combination.
35 :     *
36 :     * As a special exception, the copyright holders of XviD give you
37 :     * permission to link XviD with independent modules that communicate with
38 :     * XviD solely through the VFW1.1 and DShow interfaces, regardless of the
39 :     * license terms of these independent modules, and to copy and distribute
40 :     * the resulting combined work under terms of your choice, provided that
41 :     * every copy of the combined work is accompanied by a complete copy of
42 :     * the source code of XviD (the version of XviD used to produce the
43 :     * combined work), being distributed under the terms of the GNU General
44 :     * Public License plus this exception. An independent module is a module
45 :     * which is not derived from or based on XviD.
46 :     *
47 :     * Note that people who make modified versions of XviD are not obligated
48 :     * to grant this special exception for their modified versions; it is
49 :     * their choice whether to do so. The GNU General Public License gives
50 :     * permission to release a modified version without this exception; this
51 :     * exception also makes it possible to release a modified version which
52 :     * carries forward this exception.
53 :     *
54 :     * $Id: sad.c,v 1.11 2002-11-17 00:32:06 edgomez Exp $
55 :     *
56 : chl 430 ****************************************************************************/
57 : edgomez 195
58 :     #include "../portab.h"
59 :     #include "sad.h"
60 :    
61 :     sad16FuncPtr sad16;
62 :     sad8FuncPtr sad8;
63 :     sad16biFuncPtr sad16bi;
64 :     sad8biFuncPtr sad8bi; // not really sad16, but no difference in prototype
65 :     dev16FuncPtr dev16;
66 :    
67 :     sadInitFuncPtr sadInit;
68 :    
69 :     #define ABS(X) (((X)>0)?(X):-(X))
70 :    
71 : suxen_drol 329 #define MRSAD16_CORRFACTOR 8
72 : edgomez 195 uint32_t
73 : suxen_drol 329 mrsad16_c(const uint8_t * const cur,
74 : edgomez 195 const uint8_t * const ref,
75 : chl 326 const uint32_t stride,
76 : edgomez 195 const uint32_t best_sad)
77 :     {
78 :    
79 :     uint32_t sad = 0;
80 :     int32_t mean = 0;
81 :     uint32_t i, j;
82 :     uint8_t const *ptr_cur = cur;
83 :     uint8_t const *ptr_ref = ref;
84 :    
85 :     for (j = 0; j < 16; j++) {
86 :     for (i = 0; i < 16; i++) {
87 :     mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i));
88 :     }
89 :     ptr_cur += stride;
90 :     ptr_ref += stride;
91 :    
92 :     }
93 :     mean /= 256;
94 :    
95 :     for (j = 0; j < 16; j++) {
96 :    
97 :     ptr_cur -= stride;
98 :     ptr_ref -= stride;
99 :    
100 :     for (i = 0; i < 16; i++) {
101 :    
102 :     sad += ABS(*(ptr_cur + i) - *(ptr_ref + i) - mean);
103 :     if (sad >= best_sad) {
104 :     return MRSAD16_CORRFACTOR * sad;
105 :     }
106 :     }
107 :     }
108 :    
109 :     return MRSAD16_CORRFACTOR * sad;
110 :    
111 :     }
112 :    
113 :    
114 :     uint32_t
115 :     sad16_c(const uint8_t * const cur,
116 :     const uint8_t * const ref,
117 :     const uint32_t stride,
118 :     const uint32_t best_sad)
119 :     {
120 :    
121 :     uint32_t sad = 0;
122 :     uint32_t i, j;
123 :     uint8_t const *ptr_cur = cur;
124 :     uint8_t const *ptr_ref = ref;
125 :    
126 :     for (j = 0; j < 16; j++) {
127 :    
128 :     for (i = 0; i < 16; i++) {
129 :    
130 :     sad += ABS(*(ptr_cur + i) - *(ptr_ref + i));
131 :    
132 :     if (sad >= best_sad) {
133 :     return sad;
134 :     }
135 :    
136 :    
137 :     }
138 :    
139 :     ptr_cur += stride;
140 :     ptr_ref += stride;
141 :    
142 :     }
143 :    
144 :     return sad;
145 :    
146 :     }
147 :    
148 :    
149 :    
150 :     uint32_t
151 :     sad16bi_c(const uint8_t * const cur,
152 :     const uint8_t * const ref1,
153 :     const uint8_t * const ref2,
154 :     const uint32_t stride)
155 :     {
156 :    
157 :     uint32_t sad = 0;
158 :     uint32_t i, j;
159 :     uint8_t const *ptr_cur = cur;
160 :     uint8_t const *ptr_ref1 = ref1;
161 :     uint8_t const *ptr_ref2 = ref2;
162 :    
163 :     for (j = 0; j < 16; j++) {
164 :    
165 :     for (i = 0; i < 16; i++) {
166 :     int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2;
167 :    
168 :     if (pixel < 0) {
169 :     pixel = 0;
170 :     } else if (pixel > 255) {
171 :     pixel = 255;
172 :     }
173 :    
174 :     sad += ABS(ptr_cur[i] - pixel);
175 :     }
176 :    
177 :     ptr_cur += stride;
178 :     ptr_ref1 += stride;
179 :     ptr_ref2 += stride;
180 :    
181 :     }
182 :    
183 :     return sad;
184 :    
185 :     }
186 :    
187 :     uint32_t
188 :     sad8bi_c(const uint8_t * const cur,
189 :     const uint8_t * const ref1,
190 :     const uint8_t * const ref2,
191 :     const uint32_t stride)
192 :     {
193 :    
194 :     uint32_t sad = 0;
195 : chl 326 uint32_t i, j;
196 : edgomez 195 uint8_t const *ptr_cur = cur;
197 : chl 326 uint8_t const *ptr_ref1 = ref1;
198 :     uint8_t const *ptr_ref2 = ref2;
199 :    
200 :     for (j = 0; j < 8; j++) {
201 :    
202 :     for (i = 0; i < 8; i++) {
203 :     int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2;
204 :    
205 :     if (pixel < 0) {
206 :     pixel = 0;
207 :     } else if (pixel > 255) {
208 :     pixel = 255;
209 :     }
210 :    
211 :     sad += ABS(ptr_cur[i] - pixel);
212 :     }
213 :    
214 :     ptr_cur += stride;
215 :     ptr_ref1 += stride;
216 :     ptr_ref2 += stride;
217 :    
218 :     }
219 :    
220 :     return sad;
221 :    
222 :     }
223 :    
224 :    
225 :    
226 :     uint32_t
227 :     sad8_c(const uint8_t * const cur,
228 :     const uint8_t * const ref,
229 :     const uint32_t stride)
230 :     {
231 :     uint32_t sad = 0;
232 :     uint32_t i, j;
233 :     uint8_t const *ptr_cur = cur;
234 : edgomez 195 uint8_t const *ptr_ref = ref;
235 :    
236 :     for (j = 0; j < 8; j++) {
237 :    
238 :     for (i = 0; i < 8; i++) {
239 :     sad += ABS(*(ptr_cur + i) - *(ptr_ref + i));
240 :     }
241 :    
242 :     ptr_cur += stride;
243 :     ptr_ref += stride;
244 :    
245 :     }
246 :    
247 :     return sad;
248 :     }
249 :    
250 :    
251 :    
252 :    
253 :     /* average deviation from mean */
254 :    
255 :     uint32_t
256 :     dev16_c(const uint8_t * const cur,
257 :     const uint32_t stride)
258 :     {
259 :    
260 :     uint32_t mean = 0;
261 :     uint32_t dev = 0;
262 :     uint32_t i, j;
263 :     uint8_t const *ptr_cur = cur;
264 :    
265 :     for (j = 0; j < 16; j++) {
266 :    
267 :     for (i = 0; i < 16; i++)
268 :     mean += *(ptr_cur + i);
269 :    
270 :     ptr_cur += stride;
271 :    
272 :     }
273 :    
274 :     mean /= (16 * 16);
275 :     ptr_cur = cur;
276 :    
277 :     for (j = 0; j < 16; j++) {
278 :    
279 :     for (i = 0; i < 16; i++)
280 :     dev += ABS(*(ptr_cur + i) - (int32_t) mean);
281 :    
282 :     ptr_cur += stride;
283 :    
284 :     }
285 :    
286 :     return dev;
287 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4