[svn] / branches / dev-api-3 / xvidcore / src / motion / sad.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/motion/sad.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/xvidcore/src/motion/sad.c revision 195, Wed Jun 12 20:38:41 2002 UTC branches/dev-api-3/xvidcore/src/motion/sad.c revision 530, Mon Sep 23 20:36:02 2002 UTC
# Line 3  Line 3 
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
# Line 37  Line 46 
46  sad16biFuncPtr sad16bi;  sad16biFuncPtr sad16bi;
47  sad8biFuncPtr sad8bi;           // not really sad16, but no difference in prototype  sad8biFuncPtr sad8bi;           // not really sad16, but no difference in prototype
48  dev16FuncPtr dev16;  dev16FuncPtr dev16;
49    sad16vFuncPtr sad16v;
50    
51  sadInitFuncPtr sadInit;  sadInitFuncPtr sadInit;
52    
53  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
54    
55  uint32_t  uint32_t
56  sad8FuncPtr sad8;  sad16_c(const uint8_t * const cur,
57                    const uint8_t * const ref,                    const uint8_t * const ref,
58                    const uint32_t stride,
59                    const uint32_t best_sad)                    const uint32_t best_sad)
60  {  {
61    
62          uint32_t sad = 0;          uint32_t sad = 0;
63          int32_t mean = 0;          uint32_t j;
         uint32_t i, 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                  for (i = 0; i < 16; i++) {                          sad += ABS(ptr_cur[0] - ptr_ref[0]);
69                          mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i));                          sad += ABS(ptr_cur[1] - ptr_ref[1]);
70                  }                          sad += ABS(ptr_cur[2] - ptr_ref[2]);
71                  ptr_cur += stride;                          sad += ABS(ptr_cur[3] - ptr_ref[3]);
72                  ptr_ref += stride;                          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          }                          if (sad >= best_sad)
86          mean /= 256;                                  return sad;
   
         for (j = 0; j < 16; j++) {  
   
                 ptr_cur -= stride;  
                 ptr_ref -= stride;  
87    
88                  for (i = 0; i < 16; i++) {                          ptr_cur += stride;
89                            ptr_ref += stride;
90    
                         sad += ABS(*(ptr_cur + i) - *(ptr_ref + i) - mean);  
                         if (sad >= best_sad) {  
                                 return MRSAD16_CORRFACTOR * sad;  
                         }  
                 }  
91          }          }
92    
93          return MRSAD16_CORRFACTOR * sad;          return sad;
94    
95  }  }
96    
   
97  uint32_t  uint32_t
98  sad16_c(const uint8_t * const cur,  sad16bi_c(const uint8_t * const cur,
99                  const uint8_t * const ref,                    const uint8_t * const ref1,
100                  const uint32_t stride,                    const uint8_t * const ref2,
101                  const uint32_t best_sad)                    const uint32_t stride)
102  {  {
103    
104          uint32_t sad = 0;          uint32_t sad = 0;
105          uint32_t i, j;          uint32_t i, j;
106          uint8_t const *ptr_cur = cur;          uint8_t const *ptr_cur = cur;
107          uint8_t const *ptr_ref = ref;          uint8_t const *ptr_ref1 = ref1;
108            uint8_t const *ptr_ref2 = ref2;
109    
110          for (j = 0; j < 16; j++) {          for (j = 0; j < 16; j++) {
111    
112                  for (i = 0; i < 16; i++) {                  for (i = 0; i < 16; i++) {
113                            int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2;
114    
115                          sad += ABS(*(ptr_cur + i) - *(ptr_ref + i));                          if (pixel < 0) {
116                                    pixel = 0;
117                          if (sad >= best_sad) {                          } else if (pixel > 255) {
118                                  return sad;                                  pixel = 255;
119                          }                          }
120    
121                            sad += ABS(ptr_cur[i] - pixel);
122                  }                  }
123    
124                  ptr_cur += stride;                  ptr_cur += stride;
125                  ptr_ref += stride;                  ptr_ref1 += stride;
126                    ptr_ref2 += stride;
127    
128          }          }
129    
# Line 118  Line 131 
131    
132  }  }
133    
   
   
134  uint32_t  uint32_t
135  sad16bi_c(const uint8_t * const cur,  sad8bi_c(const uint8_t * const cur,
136                    const uint8_t * const ref1,                    const uint8_t * const ref1,
137                    const uint8_t * const ref2,                    const uint8_t * const ref2,
138                    const uint32_t stride)                    const uint32_t stride)
# Line 133  Line 144 
144          uint8_t const *ptr_ref1 = ref1;          uint8_t const *ptr_ref1 = ref1;
145          uint8_t const *ptr_ref2 = ref2;          uint8_t const *ptr_ref2 = ref2;
146    
147          for (j = 0; j < 16; j++) {          for (j = 0; j < 8; j++) {
148    
149                  for (i = 0; i < 16; i++) {                  for (i = 0; i < 8; i++) {
150                          int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2;                          int pixel = (ptr_ref1[i] + ptr_ref2[i] + 1) / 2;
151    
152                          if (pixel < 0) {                          if (pixel < 0) {
# Line 157  Line 168 
168    
169  }  }
170    
171    
172    
173  uint32_t  uint32_t
174  sad8bi_c(const uint8_t * const cur,  sad8_c(const uint8_t * const cur,
175                    const uint8_t * const ref1,             const uint8_t * const ref,
                   const uint8_t * const ref2,  
176                    const uint32_t stride)                    const uint32_t stride)
177  {  {
   
178          uint32_t sad = 0;          uint32_t sad = 0;
179            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;
# Line 183  Line 200 
200  }  }
201    
202    
   
   
203  /* average deviation from mean */  /* average deviation from mean */
204    
205  uint32_t  uint32_t
# Line 220  Line 235 
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    
253    #define MRSAD16_CORRFACTOR 8
254    uint32_t
255    mrsad16_c(const uint8_t * const cur,
256                      const uint8_t * const ref,
257                      const uint32_t stride,
258                      const uint32_t best_sad)
259    {
260    
261            uint32_t sad = 0;
262            int32_t mean = 0;
263            uint32_t i, j;
264            uint8_t const *ptr_cur = cur;
265            uint8_t const *ptr_ref = ref;
266    
267            for (j = 0; j < 16; j++) {
268                    for (i = 0; i < 16; i++) {
269                            mean += ((int) *(ptr_cur + i) - (int) *(ptr_ref + i));
270                    }
271                    ptr_cur += stride;
272                    ptr_ref += stride;
273    
274            }
275            mean /= 256;
276    
277            for (j = 0; j < 16; j++) {
278    
279                    ptr_cur -= stride;
280                    ptr_ref -= stride;
281    
282                    for (i = 0; i < 16; i++) {
283    
284                            sad += ABS(*(ptr_cur + i) - *(ptr_ref + i) - mean);
285                            if (sad >= best_sad) {
286                                    return MRSAD16_CORRFACTOR * sad;
287                            }
288                    }
289            }
290    
291            return MRSAD16_CORRFACTOR * sad;
292    
293    }
294    
295    

Legend:
Removed from v.195  
changed lines
  Added in v.530

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