sad.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2016, Alliance for Open Media. All rights reserved
  3. *
  4. * This source code is subject to the terms of the BSD 2 Clause License and
  5. * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
  6. * was not distributed with this source code in the LICENSE file, you can
  7. * obtain it at www.aomedia.org/license/software. If the Alliance for Open
  8. * Media Patent License 1.0 was not distributed with this source code in the
  9. * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  10. */
  11. #include <stdlib.h>
  12. #include "config/aom_config.h"
  13. #include "config/aom_dsp_rtcd.h"
  14. #include "aom/aom_integer.h"
  15. #include "aom_ports/mem.h"
  16. #include "aom_dsp/blend.h"
  17. /* Sum the difference between every corresponding element of the buffers. */
  18. static INLINE unsigned int sad(const uint8_t *a, int a_stride, const uint8_t *b,
  19. int b_stride, int width, int height) {
  20. int y, x;
  21. unsigned int sad = 0;
  22. for (y = 0; y < height; y++) {
  23. for (x = 0; x < width; x++) {
  24. sad += abs(a[x] - b[x]);
  25. }
  26. a += a_stride;
  27. b += b_stride;
  28. }
  29. return sad;
  30. }
  31. #define SADMXN(m, n) \
  32. unsigned int aom_sad##m##x##n##_c(const uint8_t *src, int src_stride, \
  33. const uint8_t *ref, int ref_stride) { \
  34. return sad(src, src_stride, ref, ref_stride, m, n); \
  35. } \
  36. unsigned int aom_sad##m##x##n##_avg_c(const uint8_t *src, int src_stride, \
  37. const uint8_t *ref, int ref_stride, \
  38. const uint8_t *second_pred) { \
  39. uint8_t comp_pred[m * n]; \
  40. aom_comp_avg_pred(comp_pred, second_pred, m, n, ref, ref_stride); \
  41. return sad(src, src_stride, comp_pred, m, m, n); \
  42. } \
  43. unsigned int aom_dist_wtd_sad##m##x##n##_avg_c( \
  44. const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
  45. const uint8_t *second_pred, const DIST_WTD_COMP_PARAMS *jcp_param) { \
  46. uint8_t comp_pred[m * n]; \
  47. aom_dist_wtd_comp_avg_pred_c(comp_pred, second_pred, m, n, ref, \
  48. ref_stride, jcp_param); \
  49. return sad(src, src_stride, comp_pred, m, m, n); \
  50. } \
  51. unsigned int aom_sad_skip_##m##x##n##_c(const uint8_t *src, int src_stride, \
  52. const uint8_t *ref, \
  53. int ref_stride) { \
  54. return 2 * sad(src, 2 * src_stride, ref, 2 * ref_stride, (m), (n / 2)); \
  55. }
  56. // Calculate sad against 4 reference locations and store each in sad_array
  57. #define SAD_MXNX4D(m, n) \
  58. void aom_sad##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
  59. const uint8_t *const ref_array[4], \
  60. int ref_stride, uint32_t sad_array[4]) { \
  61. int i; \
  62. for (i = 0; i < 4; ++i) { \
  63. sad_array[i] = \
  64. aom_sad##m##x##n##_c(src, src_stride, ref_array[i], ref_stride); \
  65. } \
  66. } \
  67. void aom_sad_skip_##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
  68. const uint8_t *const ref_array[4], \
  69. int ref_stride, uint32_t sad_array[4]) { \
  70. int i; \
  71. for (i = 0; i < 4; ++i) { \
  72. sad_array[i] = 2 * sad(src, 2 * src_stride, ref_array[i], \
  73. 2 * ref_stride, (m), (n / 2)); \
  74. } \
  75. }
  76. // Call SIMD version of aom_sad_mxnx4d if the 3d version is unavailable.
  77. #define SAD_MXNX3D(m, n) \
  78. void aom_sad##m##x##n##x3d_c(const uint8_t *src, int src_stride, \
  79. const uint8_t *const ref_array[4], \
  80. int ref_stride, uint32_t sad_array[4]) { \
  81. aom_sad##m##x##n##x4d(src, src_stride, ref_array, ref_stride, sad_array); \
  82. }
  83. // 128x128
  84. SADMXN(128, 128)
  85. SAD_MXNX4D(128, 128)
  86. SAD_MXNX3D(128, 128)
  87. // 128x64
  88. SADMXN(128, 64)
  89. SAD_MXNX4D(128, 64)
  90. SAD_MXNX3D(128, 64)
  91. // 64x128
  92. SADMXN(64, 128)
  93. SAD_MXNX4D(64, 128)
  94. SAD_MXNX3D(64, 128)
  95. // 64x64
  96. SADMXN(64, 64)
  97. SAD_MXNX4D(64, 64)
  98. SAD_MXNX3D(64, 64)
  99. // 64x32
  100. SADMXN(64, 32)
  101. SAD_MXNX4D(64, 32)
  102. SAD_MXNX3D(64, 32)
  103. // 32x64
  104. SADMXN(32, 64)
  105. SAD_MXNX4D(32, 64)
  106. SAD_MXNX3D(32, 64)
  107. // 32x32
  108. SADMXN(32, 32)
  109. SAD_MXNX4D(32, 32)
  110. SAD_MXNX3D(32, 32)
  111. // 32x16
  112. SADMXN(32, 16)
  113. SAD_MXNX4D(32, 16)
  114. SAD_MXNX3D(32, 16)
  115. // 16x32
  116. SADMXN(16, 32)
  117. SAD_MXNX4D(16, 32)
  118. SAD_MXNX3D(16, 32)
  119. // 16x16
  120. SADMXN(16, 16)
  121. SAD_MXNX4D(16, 16)
  122. SAD_MXNX3D(16, 16)
  123. // 16x8
  124. SADMXN(16, 8)
  125. SAD_MXNX4D(16, 8)
  126. SAD_MXNX3D(16, 8)
  127. // 8x16
  128. SADMXN(8, 16)
  129. SAD_MXNX4D(8, 16)
  130. SAD_MXNX3D(8, 16)
  131. // 8x8
  132. SADMXN(8, 8)
  133. SAD_MXNX4D(8, 8)
  134. SAD_MXNX3D(8, 8)
  135. // 8x4
  136. SADMXN(8, 4)
  137. SAD_MXNX4D(8, 4)
  138. SAD_MXNX3D(8, 4)
  139. // 4x8
  140. SADMXN(4, 8)
  141. SAD_MXNX4D(4, 8)
  142. SAD_MXNX3D(4, 8)
  143. // 4x4
  144. SADMXN(4, 4)
  145. SAD_MXNX4D(4, 4)
  146. SAD_MXNX3D(4, 4)
  147. #if !CONFIG_REALTIME_ONLY
  148. SADMXN(4, 16)
  149. SAD_MXNX4D(4, 16)
  150. SADMXN(16, 4)
  151. SAD_MXNX4D(16, 4)
  152. SADMXN(8, 32)
  153. SAD_MXNX4D(8, 32)
  154. SADMXN(32, 8)
  155. SAD_MXNX4D(32, 8)
  156. SADMXN(16, 64)
  157. SAD_MXNX4D(16, 64)
  158. SADMXN(64, 16)
  159. SAD_MXNX4D(64, 16)
  160. SAD_MXNX3D(4, 16)
  161. SAD_MXNX3D(16, 4)
  162. SAD_MXNX3D(8, 32)
  163. SAD_MXNX3D(32, 8)
  164. SAD_MXNX3D(16, 64)
  165. SAD_MXNX3D(64, 16)
  166. #endif // !CONFIG_REALTIME_ONLY
  167. #if CONFIG_AV1_HIGHBITDEPTH
  168. static INLINE unsigned int highbd_sad(const uint8_t *a8, int a_stride,
  169. const uint8_t *b8, int b_stride,
  170. int width, int height) {
  171. int y, x;
  172. unsigned int sad = 0;
  173. const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  174. const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  175. for (y = 0; y < height; y++) {
  176. for (x = 0; x < width; x++) {
  177. sad += abs(a[x] - b[x]);
  178. }
  179. a += a_stride;
  180. b += b_stride;
  181. }
  182. return sad;
  183. }
  184. static INLINE unsigned int highbd_sadb(const uint8_t *a8, int a_stride,
  185. const uint8_t *b8, int b_stride,
  186. int width, int height) {
  187. int y, x;
  188. unsigned int sad = 0;
  189. const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  190. const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  191. for (y = 0; y < height; y++) {
  192. for (x = 0; x < width; x++) {
  193. sad += abs(a[x] - b[x]);
  194. }
  195. a += a_stride;
  196. b += b_stride;
  197. }
  198. return sad;
  199. }
  200. #define HIGHBD_SADMXN(m, n) \
  201. unsigned int aom_highbd_sad##m##x##n##_c(const uint8_t *src, int src_stride, \
  202. const uint8_t *ref, \
  203. int ref_stride) { \
  204. return highbd_sad(src, src_stride, ref, ref_stride, m, n); \
  205. } \
  206. unsigned int aom_highbd_sad##m##x##n##_avg_c( \
  207. const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
  208. const uint8_t *second_pred) { \
  209. uint16_t comp_pred[m * n]; \
  210. uint8_t *const comp_pred8 = CONVERT_TO_BYTEPTR(comp_pred); \
  211. aom_highbd_comp_avg_pred(comp_pred8, second_pred, m, n, ref, ref_stride); \
  212. return highbd_sadb(src, src_stride, comp_pred8, m, m, n); \
  213. } \
  214. unsigned int aom_highbd_dist_wtd_sad##m##x##n##_avg_c( \
  215. const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
  216. const uint8_t *second_pred, const DIST_WTD_COMP_PARAMS *jcp_param) { \
  217. uint16_t comp_pred[m * n]; \
  218. uint8_t *const comp_pred8 = CONVERT_TO_BYTEPTR(comp_pred); \
  219. aom_highbd_dist_wtd_comp_avg_pred(comp_pred8, second_pred, m, n, ref, \
  220. ref_stride, jcp_param); \
  221. return highbd_sadb(src, src_stride, comp_pred8, m, m, n); \
  222. } \
  223. unsigned int aom_highbd_sad_skip_##m##x##n##_c( \
  224. const uint8_t *src, int src_stride, const uint8_t *ref, \
  225. int ref_stride) { \
  226. return 2 * \
  227. highbd_sad(src, 2 * src_stride, ref, 2 * ref_stride, (m), (n / 2)); \
  228. }
  229. #define HIGHBD_SAD_MXNX4D(m, n) \
  230. void aom_highbd_sad##m##x##n##x4d_c(const uint8_t *src, int src_stride, \
  231. const uint8_t *const ref_array[], \
  232. int ref_stride, uint32_t *sad_array) { \
  233. int i; \
  234. for (i = 0; i < 4; ++i) { \
  235. sad_array[i] = aom_highbd_sad##m##x##n##_c(src, src_stride, \
  236. ref_array[i], ref_stride); \
  237. } \
  238. } \
  239. void aom_highbd_sad_skip_##m##x##n##x4d_c( \
  240. const uint8_t *src, int src_stride, const uint8_t *const ref_array[], \
  241. int ref_stride, uint32_t *sad_array) { \
  242. int i; \
  243. for (i = 0; i < 4; ++i) { \
  244. sad_array[i] = 2 * highbd_sad(src, 2 * src_stride, ref_array[i], \
  245. 2 * ref_stride, (m), (n / 2)); \
  246. } \
  247. }
  248. // Call SIMD version of aom_highbd_sad_mxnx4d if the 3d version is unavailable.
  249. #define HIGHBD_SAD_MXNX3D(m, n) \
  250. void aom_highbd_sad##m##x##n##x3d_c(const uint8_t *src, int src_stride, \
  251. const uint8_t *const ref_array[], \
  252. int ref_stride, uint32_t *sad_array) { \
  253. aom_highbd_sad##m##x##n##x4d(src, src_stride, ref_array, ref_stride, \
  254. sad_array); \
  255. }
  256. // 128x128
  257. HIGHBD_SADMXN(128, 128)
  258. HIGHBD_SAD_MXNX4D(128, 128)
  259. HIGHBD_SAD_MXNX3D(128, 128)
  260. // 128x64
  261. HIGHBD_SADMXN(128, 64)
  262. HIGHBD_SAD_MXNX4D(128, 64)
  263. HIGHBD_SAD_MXNX3D(128, 64)
  264. // 64x128
  265. HIGHBD_SADMXN(64, 128)
  266. HIGHBD_SAD_MXNX4D(64, 128)
  267. HIGHBD_SAD_MXNX3D(64, 128)
  268. // 64x64
  269. HIGHBD_SADMXN(64, 64)
  270. HIGHBD_SAD_MXNX4D(64, 64)
  271. HIGHBD_SAD_MXNX3D(64, 64)
  272. // 64x32
  273. HIGHBD_SADMXN(64, 32)
  274. HIGHBD_SAD_MXNX4D(64, 32)
  275. HIGHBD_SAD_MXNX3D(64, 32)
  276. // 32x64
  277. HIGHBD_SADMXN(32, 64)
  278. HIGHBD_SAD_MXNX4D(32, 64)
  279. HIGHBD_SAD_MXNX3D(32, 64)
  280. // 32x32
  281. HIGHBD_SADMXN(32, 32)
  282. HIGHBD_SAD_MXNX4D(32, 32)
  283. HIGHBD_SAD_MXNX3D(32, 32)
  284. // 32x16
  285. HIGHBD_SADMXN(32, 16)
  286. HIGHBD_SAD_MXNX4D(32, 16)
  287. HIGHBD_SAD_MXNX3D(32, 16)
  288. // 16x32
  289. HIGHBD_SADMXN(16, 32)
  290. HIGHBD_SAD_MXNX4D(16, 32)
  291. HIGHBD_SAD_MXNX3D(16, 32)
  292. // 16x16
  293. HIGHBD_SADMXN(16, 16)
  294. HIGHBD_SAD_MXNX4D(16, 16)
  295. HIGHBD_SAD_MXNX3D(16, 16)
  296. // 16x8
  297. HIGHBD_SADMXN(16, 8)
  298. HIGHBD_SAD_MXNX4D(16, 8)
  299. HIGHBD_SAD_MXNX3D(16, 8)
  300. // 8x16
  301. HIGHBD_SADMXN(8, 16)
  302. HIGHBD_SAD_MXNX4D(8, 16)
  303. HIGHBD_SAD_MXNX3D(8, 16)
  304. // 8x8
  305. HIGHBD_SADMXN(8, 8)
  306. HIGHBD_SAD_MXNX4D(8, 8)
  307. HIGHBD_SAD_MXNX3D(8, 8)
  308. // 8x4
  309. HIGHBD_SADMXN(8, 4)
  310. HIGHBD_SAD_MXNX4D(8, 4)
  311. HIGHBD_SAD_MXNX3D(8, 4)
  312. // 4x8
  313. HIGHBD_SADMXN(4, 8)
  314. HIGHBD_SAD_MXNX4D(4, 8)
  315. HIGHBD_SAD_MXNX3D(4, 8)
  316. // 4x4
  317. HIGHBD_SADMXN(4, 4)
  318. HIGHBD_SAD_MXNX4D(4, 4)
  319. HIGHBD_SAD_MXNX3D(4, 4)
  320. HIGHBD_SADMXN(4, 16)
  321. HIGHBD_SAD_MXNX4D(4, 16)
  322. HIGHBD_SADMXN(16, 4)
  323. HIGHBD_SAD_MXNX4D(16, 4)
  324. HIGHBD_SADMXN(8, 32)
  325. HIGHBD_SAD_MXNX4D(8, 32)
  326. HIGHBD_SADMXN(32, 8)
  327. HIGHBD_SAD_MXNX4D(32, 8)
  328. HIGHBD_SADMXN(16, 64)
  329. HIGHBD_SAD_MXNX4D(16, 64)
  330. HIGHBD_SADMXN(64, 16)
  331. HIGHBD_SAD_MXNX4D(64, 16)
  332. #if !CONFIG_REALTIME_ONLY
  333. HIGHBD_SAD_MXNX3D(4, 16)
  334. HIGHBD_SAD_MXNX3D(16, 4)
  335. HIGHBD_SAD_MXNX3D(8, 32)
  336. HIGHBD_SAD_MXNX3D(32, 8)
  337. HIGHBD_SAD_MXNX3D(16, 64)
  338. HIGHBD_SAD_MXNX3D(64, 16)
  339. #endif // !CONFIG_REALTIME_ONLY
  340. #endif // CONFIG_AV1_HIGHBITDEPTH