sad.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #include <stdlib.h>
  11. #include "./vpx_config.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx/vpx_integer.h"
  14. #include "vpx_ports/mem.h"
  15. /* Sum the difference between every corresponding element of the buffers. */
  16. static INLINE unsigned int sad(const uint8_t *a, int a_stride, const uint8_t *b,
  17. int b_stride, int width, int height) {
  18. int y, x;
  19. unsigned int sad = 0;
  20. for (y = 0; y < height; y++) {
  21. for (x = 0; x < width; x++) sad += abs(a[x] - b[x]);
  22. a += a_stride;
  23. b += b_stride;
  24. }
  25. return sad;
  26. }
  27. #define sadMxN(m, n) \
  28. unsigned int vpx_sad##m##x##n##_c(const uint8_t *src, int src_stride, \
  29. const uint8_t *ref, int ref_stride) { \
  30. return sad(src, src_stride, ref, ref_stride, m, n); \
  31. } \
  32. unsigned int vpx_sad##m##x##n##_avg_c(const uint8_t *src, int src_stride, \
  33. const uint8_t *ref, int ref_stride, \
  34. const uint8_t *second_pred) { \
  35. DECLARE_ALIGNED(16, uint8_t, comp_pred[m * n]); \
  36. vpx_comp_avg_pred_c(comp_pred, second_pred, m, n, ref, ref_stride); \
  37. return sad(src, src_stride, comp_pred, m, m, n); \
  38. }
  39. // depending on call sites, pass **ref_array to avoid & in subsequent call and
  40. // de-dup with 4D below.
  41. #define sadMxNxK(m, n, k) \
  42. void vpx_sad##m##x##n##x##k##_c(const uint8_t *src, int src_stride, \
  43. const uint8_t *ref_array, int ref_stride, \
  44. uint32_t *sad_array) { \
  45. int i; \
  46. for (i = 0; i < k; ++i) \
  47. sad_array[i] = \
  48. vpx_sad##m##x##n##_c(src, src_stride, &ref_array[i], ref_stride); \
  49. }
  50. // This appears to be equivalent to the above when k == 4 and refs is const
  51. #define sadMxNx4D(m, n) \
  52. void vpx_sad##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
  53. const uint8_t *const ref_array[], \
  54. int ref_stride, uint32_t *sad_array) { \
  55. int i; \
  56. for (i = 0; i < 4; ++i) \
  57. sad_array[i] = \
  58. vpx_sad##m##x##n##_c(src, src_stride, ref_array[i], ref_stride); \
  59. }
  60. /* clang-format off */
  61. // 64x64
  62. sadMxN(64, 64)
  63. sadMxNx4D(64, 64)
  64. // 64x32
  65. sadMxN(64, 32)
  66. sadMxNx4D(64, 32)
  67. // 32x64
  68. sadMxN(32, 64)
  69. sadMxNx4D(32, 64)
  70. // 32x32
  71. sadMxN(32, 32)
  72. sadMxNx4D(32, 32)
  73. // 32x16
  74. sadMxN(32, 16)
  75. sadMxNx4D(32, 16)
  76. // 16x32
  77. sadMxN(16, 32)
  78. sadMxNx4D(16, 32)
  79. // 16x16
  80. sadMxN(16, 16)
  81. sadMxNxK(16, 16, 3)
  82. sadMxNxK(16, 16, 8)
  83. sadMxNx4D(16, 16)
  84. // 16x8
  85. sadMxN(16, 8)
  86. sadMxNxK(16, 8, 3)
  87. sadMxNxK(16, 8, 8)
  88. sadMxNx4D(16, 8)
  89. // 8x16
  90. sadMxN(8, 16)
  91. sadMxNxK(8, 16, 3)
  92. sadMxNxK(8, 16, 8)
  93. sadMxNx4D(8, 16)
  94. // 8x8
  95. sadMxN(8, 8)
  96. sadMxNxK(8, 8, 3)
  97. sadMxNxK(8, 8, 8)
  98. sadMxNx4D(8, 8)
  99. // 8x4
  100. sadMxN(8, 4)
  101. sadMxNx4D(8, 4)
  102. // 4x8
  103. sadMxN(4, 8)
  104. sadMxNx4D(4, 8)
  105. // 4x4
  106. sadMxN(4, 4)
  107. sadMxNxK(4, 4, 3)
  108. sadMxNxK(4, 4, 8)
  109. sadMxNx4D(4, 4)
  110. /* clang-format on */
  111. #if CONFIG_VP9_HIGHBITDEPTH
  112. static INLINE
  113. unsigned int highbd_sad(const uint8_t *a8, int a_stride, const uint8_t *b8,
  114. int b_stride, int width, int height) {
  115. int y, x;
  116. unsigned int sad = 0;
  117. const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  118. const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  119. for (y = 0; y < height; y++) {
  120. for (x = 0; x < width; x++) sad += abs(a[x] - b[x]);
  121. a += a_stride;
  122. b += b_stride;
  123. }
  124. return sad;
  125. }
  126. static INLINE unsigned int highbd_sadb(const uint8_t *a8, int a_stride,
  127. const uint16_t *b, int b_stride,
  128. int width, int height) {
  129. int y, x;
  130. unsigned int sad = 0;
  131. const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  132. for (y = 0; y < height; y++) {
  133. for (x = 0; x < width; x++) sad += abs(a[x] - b[x]);
  134. a += a_stride;
  135. b += b_stride;
  136. }
  137. return sad;
  138. }
  139. #define highbd_sadMxN(m, n) \
  140. unsigned int vpx_highbd_sad##m##x##n##_c(const uint8_t *src, int src_stride, \
  141. const uint8_t *ref, \
  142. int ref_stride) { \
  143. return highbd_sad(src, src_stride, ref, ref_stride, m, n); \
  144. } \
  145. unsigned int vpx_highbd_sad##m##x##n##_avg_c( \
  146. const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
  147. const uint8_t *second_pred) { \
  148. DECLARE_ALIGNED(16, uint16_t, comp_pred[m * n]); \
  149. vpx_highbd_comp_avg_pred_c(comp_pred, second_pred, m, n, ref, ref_stride); \
  150. return highbd_sadb(src, src_stride, comp_pred, m, m, n); \
  151. }
  152. #define highbd_sadMxNx4D(m, n) \
  153. void vpx_highbd_sad##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
  154. const uint8_t *const ref_array[], \
  155. int ref_stride, uint32_t *sad_array) { \
  156. int i; \
  157. for (i = 0; i < 4; ++i) { \
  158. sad_array[i] = vpx_highbd_sad##m##x##n##_c(src, src_stride, \
  159. ref_array[i], ref_stride); \
  160. } \
  161. }
  162. /* clang-format off */
  163. // 64x64
  164. highbd_sadMxN(64, 64)
  165. highbd_sadMxNx4D(64, 64)
  166. // 64x32
  167. highbd_sadMxN(64, 32)
  168. highbd_sadMxNx4D(64, 32)
  169. // 32x64
  170. highbd_sadMxN(32, 64)
  171. highbd_sadMxNx4D(32, 64)
  172. // 32x32
  173. highbd_sadMxN(32, 32)
  174. highbd_sadMxNx4D(32, 32)
  175. // 32x16
  176. highbd_sadMxN(32, 16)
  177. highbd_sadMxNx4D(32, 16)
  178. // 16x32
  179. highbd_sadMxN(16, 32)
  180. highbd_sadMxNx4D(16, 32)
  181. // 16x16
  182. highbd_sadMxN(16, 16)
  183. highbd_sadMxNx4D(16, 16)
  184. // 16x8
  185. highbd_sadMxN(16, 8)
  186. highbd_sadMxNx4D(16, 8)
  187. // 8x16
  188. highbd_sadMxN(8, 16)
  189. highbd_sadMxNx4D(8, 16)
  190. // 8x8
  191. highbd_sadMxN(8, 8)
  192. highbd_sadMxNx4D(8, 8)
  193. // 8x4
  194. highbd_sadMxN(8, 4)
  195. highbd_sadMxNx4D(8, 4)
  196. // 4x8
  197. highbd_sadMxN(4, 8)
  198. highbd_sadMxNx4D(4, 8)
  199. // 4x4
  200. highbd_sadMxN(4, 4)
  201. highbd_sadMxNx4D(4, 4)
  202. /* clang-format on */
  203. #endif // CONFIG_VP9_HIGHBITDEPTH