2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #include "./vpx_config.h"
14 #include "./vpx_dsp_rtcd.h"
16 #include "vpx/vpx_integer.h"
17 #include "vpx_ports/mem.h"
19 /* Sum the difference between every corresponding element of the buffers. */
20 static INLINE unsigned int sad(const uint8_t *a, int a_stride, const uint8_t *b,
21 int b_stride, int width, int height) {
25 for (y = 0; y < height; y++) {
26 for (x = 0; x < width; x++) sad += abs(a[x] - b[x]);
34 #define sadMxN(m, n) \
35 unsigned int vpx_sad##m##x##n##_c(const uint8_t *src, int src_stride, \
36 const uint8_t *ref, int ref_stride) { \
37 return sad(src, src_stride, ref, ref_stride, m, n); \
39 unsigned int vpx_sad##m##x##n##_avg_c(const uint8_t *src, int src_stride, \
40 const uint8_t *ref, int ref_stride, \
41 const uint8_t *second_pred) { \
42 DECLARE_ALIGNED(16, uint8_t, comp_pred[m * n]); \
43 vpx_comp_avg_pred_c(comp_pred, second_pred, m, n, ref, ref_stride); \
44 return sad(src, src_stride, comp_pred, m, m, n); \
47 // depending on call sites, pass **ref_array to avoid & in subsequent call and
48 // de-dup with 4D below.
49 #define sadMxNxK(m, n, k) \
50 void vpx_sad##m##x##n##x##k##_c(const uint8_t *src, int src_stride, \
51 const uint8_t *ref_array, int ref_stride, \
52 uint32_t *sad_array) { \
54 for (i = 0; i < k; ++i) \
56 vpx_sad##m##x##n##_c(src, src_stride, &ref_array[i], ref_stride); \
59 // This appears to be equivalent to the above when k == 4 and refs is const
60 #define sadMxNx4D(m, n) \
61 void vpx_sad##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
62 const uint8_t *const ref_array[], \
63 int ref_stride, uint32_t *sad_array) { \
65 for (i = 0; i < 4; ++i) \
67 vpx_sad##m##x##n##_c(src, src_stride, ref_array[i], ref_stride); \
70 /* clang-format off */
138 /* clang-format on */
140 #if CONFIG_VP9_HIGHBITDEPTH
142 unsigned int highbd_sad(const uint8_t *a8, int a_stride, const uint8_t *b8,
143 int b_stride, int width, int height) {
145 unsigned int sad = 0;
146 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
147 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
148 for (y = 0; y < height; y++) {
149 for (x = 0; x < width; x++) sad += abs(a[x] - b[x]);
157 static INLINE unsigned int highbd_sadb(const uint8_t *a8, int a_stride,
158 const uint16_t *b, int b_stride,
159 int width, int height) {
161 unsigned int sad = 0;
162 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
163 for (y = 0; y < height; y++) {
164 for (x = 0; x < width; x++) sad += abs(a[x] - b[x]);
172 #define highbd_sadMxN(m, n) \
173 unsigned int vpx_highbd_sad##m##x##n##_c(const uint8_t *src, int src_stride, \
174 const uint8_t *ref, \
176 return highbd_sad(src, src_stride, ref, ref_stride, m, n); \
178 unsigned int vpx_highbd_sad##m##x##n##_avg_c( \
179 const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
180 const uint8_t *second_pred) { \
181 DECLARE_ALIGNED(16, uint16_t, comp_pred[m * n]); \
182 vpx_highbd_comp_avg_pred_c(comp_pred, second_pred, m, n, ref, ref_stride); \
183 return highbd_sadb(src, src_stride, comp_pred, m, m, n); \
186 #define highbd_sadMxNxK(m, n, k) \
187 void vpx_highbd_sad##m##x##n##x##k##_c( \
188 const uint8_t *src, int src_stride, const uint8_t *ref_array, \
189 int ref_stride, uint32_t *sad_array) { \
191 for (i = 0; i < k; ++i) { \
192 sad_array[i] = vpx_highbd_sad##m##x##n##_c(src, src_stride, \
193 &ref_array[i], ref_stride); \
197 #define highbd_sadMxNx4D(m, n) \
198 void vpx_highbd_sad##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
199 const uint8_t *const ref_array[], \
200 int ref_stride, uint32_t *sad_array) { \
202 for (i = 0; i < 4; ++i) { \
203 sad_array[i] = vpx_highbd_sad##m##x##n##_c(src, src_stride, \
204 ref_array[i], ref_stride); \
208 /* clang-format off */
210 highbd_sadMxN(64, 64)
211 highbd_sadMxNxK(64, 64, 3)
212 highbd_sadMxNxK(64, 64, 8)
213 highbd_sadMxNx4D(64, 64)
216 highbd_sadMxN(64, 32)
217 highbd_sadMxNx4D(64, 32)
220 highbd_sadMxN(32, 64)
221 highbd_sadMxNx4D(32, 64)
224 highbd_sadMxN(32, 32)
225 highbd_sadMxNxK(32, 32, 3)
226 highbd_sadMxNxK(32, 32, 8)
227 highbd_sadMxNx4D(32, 32)
230 highbd_sadMxN(32, 16)
231 highbd_sadMxNx4D(32, 16)
234 highbd_sadMxN(16, 32)
235 highbd_sadMxNx4D(16, 32)
238 highbd_sadMxN(16, 16)
239 highbd_sadMxNxK(16, 16, 3)
240 highbd_sadMxNxK(16, 16, 8)
241 highbd_sadMxNx4D(16, 16)
245 highbd_sadMxNxK(16, 8, 3)
246 highbd_sadMxNxK(16, 8, 8)
247 highbd_sadMxNx4D(16, 8)
251 highbd_sadMxNxK(8, 16, 3)
252 highbd_sadMxNxK(8, 16, 8)
253 highbd_sadMxNx4D(8, 16)
257 highbd_sadMxNxK(8, 8, 3)
258 highbd_sadMxNxK(8, 8, 8)
259 highbd_sadMxNx4D(8, 8)
263 highbd_sadMxNxK(8, 4, 8)
264 highbd_sadMxNx4D(8, 4)
268 highbd_sadMxNxK(4, 8, 8)
269 highbd_sadMxNx4D(4, 8)
273 highbd_sadMxNxK(4, 4, 3)
274 highbd_sadMxNxK(4, 4, 8)
275 highbd_sadMxNx4D(4, 4)
276 /* clang-format on */
278 #endif // CONFIG_VP9_HIGHBITDEPTH