sad_av1.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2017, 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. static INLINE unsigned int masked_sad(const uint8_t *src, int src_stride,
  18. const uint8_t *a, int a_stride,
  19. const uint8_t *b, int b_stride,
  20. const uint8_t *m, int m_stride, int width,
  21. int height) {
  22. int y, x;
  23. unsigned int sad = 0;
  24. for (y = 0; y < height; y++) {
  25. for (x = 0; x < width; x++) {
  26. const int16_t pred = AOM_BLEND_A64(m[x], a[x], b[x]);
  27. sad += abs(pred - src[x]);
  28. }
  29. src += src_stride;
  30. a += a_stride;
  31. b += b_stride;
  32. m += m_stride;
  33. }
  34. return sad;
  35. }
  36. #define MASKSADMxN(m, n) \
  37. unsigned int aom_masked_sad##m##x##n##_c( \
  38. const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
  39. const uint8_t *second_pred, const uint8_t *msk, int msk_stride, \
  40. int invert_mask) { \
  41. if (!invert_mask) \
  42. return masked_sad(src, src_stride, ref, ref_stride, second_pred, m, msk, \
  43. msk_stride, m, n); \
  44. else \
  45. return masked_sad(src, src_stride, second_pred, m, ref, ref_stride, msk, \
  46. msk_stride, m, n); \
  47. } \
  48. void aom_masked_sad##m##x##n##x4d_c( \
  49. const uint8_t *src, int src_stride, const uint8_t *ref[4], \
  50. int ref_stride, const uint8_t *second_pred, const uint8_t *msk, \
  51. int msk_stride, int invert_mask, unsigned sads[4]) { \
  52. if (!invert_mask) \
  53. for (int i = 0; i < 4; i++) { \
  54. sads[i] = masked_sad(src, src_stride, ref[i], ref_stride, second_pred, \
  55. m, msk, msk_stride, m, n); \
  56. } \
  57. else \
  58. for (int i = 0; i < 4; i++) { \
  59. sads[i] = masked_sad(src, src_stride, second_pred, m, ref[i], \
  60. ref_stride, msk, msk_stride, m, n); \
  61. } \
  62. }
  63. /* clang-format off */
  64. MASKSADMxN(128, 128)
  65. MASKSADMxN(128, 64)
  66. MASKSADMxN(64, 128)
  67. MASKSADMxN(64, 64)
  68. MASKSADMxN(64, 32)
  69. MASKSADMxN(32, 64)
  70. MASKSADMxN(32, 32)
  71. MASKSADMxN(32, 16)
  72. MASKSADMxN(16, 32)
  73. MASKSADMxN(16, 16)
  74. MASKSADMxN(16, 8)
  75. MASKSADMxN(8, 16)
  76. MASKSADMxN(8, 8)
  77. MASKSADMxN(8, 4)
  78. MASKSADMxN(4, 8)
  79. MASKSADMxN(4, 4)
  80. MASKSADMxN(4, 16)
  81. MASKSADMxN(16, 4)
  82. MASKSADMxN(8, 32)
  83. MASKSADMxN(32, 8)
  84. MASKSADMxN(16, 64)
  85. MASKSADMxN(64, 16)
  86. /* clang-format on */
  87. #if CONFIG_AV1_HIGHBITDEPTH
  88. static INLINE
  89. unsigned int highbd_masked_sad(const uint8_t *src8, int src_stride,
  90. const uint8_t *a8, int a_stride,
  91. const uint8_t *b8, int b_stride,
  92. const uint8_t *m, int m_stride, int width,
  93. int height) {
  94. int y, x;
  95. unsigned int sad = 0;
  96. const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  97. const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  98. const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  99. for (y = 0; y < height; y++) {
  100. for (x = 0; x < width; x++) {
  101. const uint16_t pred = AOM_BLEND_A64(m[x], a[x], b[x]);
  102. sad += abs(pred - src[x]);
  103. }
  104. src += src_stride;
  105. a += a_stride;
  106. b += b_stride;
  107. m += m_stride;
  108. }
  109. return sad;
  110. }
  111. #define HIGHBD_MASKSADMXN(m, n) \
  112. unsigned int aom_highbd_masked_sad##m##x##n##_c( \
  113. const uint8_t *src8, int src_stride, const uint8_t *ref8, \
  114. int ref_stride, const uint8_t *second_pred8, const uint8_t *msk, \
  115. int msk_stride, int invert_mask) { \
  116. if (!invert_mask) \
  117. return highbd_masked_sad(src8, src_stride, ref8, ref_stride, \
  118. second_pred8, m, msk, msk_stride, m, n); \
  119. else \
  120. return highbd_masked_sad(src8, src_stride, second_pred8, m, ref8, \
  121. ref_stride, msk, msk_stride, m, n); \
  122. }
  123. HIGHBD_MASKSADMXN(128, 128)
  124. HIGHBD_MASKSADMXN(128, 64)
  125. HIGHBD_MASKSADMXN(64, 128)
  126. HIGHBD_MASKSADMXN(64, 64)
  127. HIGHBD_MASKSADMXN(64, 32)
  128. HIGHBD_MASKSADMXN(32, 64)
  129. HIGHBD_MASKSADMXN(32, 32)
  130. HIGHBD_MASKSADMXN(32, 16)
  131. HIGHBD_MASKSADMXN(16, 32)
  132. HIGHBD_MASKSADMXN(16, 16)
  133. HIGHBD_MASKSADMXN(16, 8)
  134. HIGHBD_MASKSADMXN(8, 16)
  135. HIGHBD_MASKSADMXN(8, 8)
  136. HIGHBD_MASKSADMXN(8, 4)
  137. HIGHBD_MASKSADMXN(4, 8)
  138. HIGHBD_MASKSADMXN(4, 4)
  139. HIGHBD_MASKSADMXN(4, 16)
  140. HIGHBD_MASKSADMXN(16, 4)
  141. HIGHBD_MASKSADMXN(8, 32)
  142. HIGHBD_MASKSADMXN(32, 8)
  143. HIGHBD_MASKSADMXN(16, 64)
  144. HIGHBD_MASKSADMXN(64, 16)
  145. #endif // CONFIG_AV1_HIGHBITDEPTH
  146. #if !CONFIG_REALTIME_ONLY
  147. // pre: predictor being evaluated
  148. // wsrc: target weighted prediction (has been *4096 to keep precision)
  149. // mask: 2d weights (scaled by 4096)
  150. static INLINE unsigned int obmc_sad(const uint8_t *pre, int pre_stride,
  151. const int32_t *wsrc, const int32_t *mask,
  152. int width, int height) {
  153. int y, x;
  154. unsigned int sad = 0;
  155. for (y = 0; y < height; y++) {
  156. for (x = 0; x < width; x++)
  157. sad += ROUND_POWER_OF_TWO(abs(wsrc[x] - pre[x] * mask[x]), 12);
  158. pre += pre_stride;
  159. wsrc += width;
  160. mask += width;
  161. }
  162. return sad;
  163. }
  164. #define OBMCSADMxN(m, n) \
  165. unsigned int aom_obmc_sad##m##x##n##_c(const uint8_t *ref, int ref_stride, \
  166. const int32_t *wsrc, \
  167. const int32_t *mask) { \
  168. return obmc_sad(ref, ref_stride, wsrc, mask, m, n); \
  169. }
  170. /* clang-format off */
  171. OBMCSADMxN(128, 128)
  172. OBMCSADMxN(128, 64)
  173. OBMCSADMxN(64, 128)
  174. OBMCSADMxN(64, 64)
  175. OBMCSADMxN(64, 32)
  176. OBMCSADMxN(32, 64)
  177. OBMCSADMxN(32, 32)
  178. OBMCSADMxN(32, 16)
  179. OBMCSADMxN(16, 32)
  180. OBMCSADMxN(16, 16)
  181. OBMCSADMxN(16, 8)
  182. OBMCSADMxN(8, 16)
  183. OBMCSADMxN(8, 8)
  184. OBMCSADMxN(8, 4)
  185. OBMCSADMxN(4, 8)
  186. OBMCSADMxN(4, 4)
  187. OBMCSADMxN(4, 16)
  188. OBMCSADMxN(16, 4)
  189. OBMCSADMxN(8, 32)
  190. OBMCSADMxN(32, 8)
  191. OBMCSADMxN(16, 64)
  192. OBMCSADMxN(64, 16)
  193. /* clang-format on */
  194. #if CONFIG_AV1_HIGHBITDEPTH
  195. static INLINE
  196. unsigned int highbd_obmc_sad(const uint8_t *pre8, int pre_stride,
  197. const int32_t *wsrc, const int32_t *mask,
  198. int width, int height) {
  199. int y, x;
  200. unsigned int sad = 0;
  201. const uint16_t *pre = CONVERT_TO_SHORTPTR(pre8);
  202. for (y = 0; y < height; y++) {
  203. for (x = 0; x < width; x++)
  204. sad += ROUND_POWER_OF_TWO(abs(wsrc[x] - pre[x] * mask[x]), 12);
  205. pre += pre_stride;
  206. wsrc += width;
  207. mask += width;
  208. }
  209. return sad;
  210. }
  211. #define HIGHBD_OBMCSADMXN(m, n) \
  212. unsigned int aom_highbd_obmc_sad##m##x##n##_c( \
  213. const uint8_t *ref, int ref_stride, const int32_t *wsrc, \
  214. const int32_t *mask) { \
  215. return highbd_obmc_sad(ref, ref_stride, wsrc, mask, m, n); \
  216. }
  217. /* clang-format off */
  218. HIGHBD_OBMCSADMXN(128, 128)
  219. HIGHBD_OBMCSADMXN(128, 64)
  220. HIGHBD_OBMCSADMXN(64, 128)
  221. HIGHBD_OBMCSADMXN(64, 64)
  222. HIGHBD_OBMCSADMXN(64, 32)
  223. HIGHBD_OBMCSADMXN(32, 64)
  224. HIGHBD_OBMCSADMXN(32, 32)
  225. HIGHBD_OBMCSADMXN(32, 16)
  226. HIGHBD_OBMCSADMXN(16, 32)
  227. HIGHBD_OBMCSADMXN(16, 16)
  228. HIGHBD_OBMCSADMXN(16, 8)
  229. HIGHBD_OBMCSADMXN(8, 16)
  230. HIGHBD_OBMCSADMXN(8, 8)
  231. HIGHBD_OBMCSADMXN(8, 4)
  232. HIGHBD_OBMCSADMXN(4, 8)
  233. HIGHBD_OBMCSADMXN(4, 4)
  234. HIGHBD_OBMCSADMXN(4, 16)
  235. HIGHBD_OBMCSADMXN(16, 4)
  236. HIGHBD_OBMCSADMXN(8, 32)
  237. HIGHBD_OBMCSADMXN(32, 8)
  238. HIGHBD_OBMCSADMXN(16, 64)
  239. HIGHBD_OBMCSADMXN(64, 16)
  240. /* clang-format on */
  241. #endif // CONFIG_AV1_HIGHBITDEPTH
  242. #endif // !CONFIG_REALTIME_ONLY