vvc_mc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2023-2024 Nuo Mi
  3. * Copyright (c) 2023-2024 Wu Jianhua
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include <string.h>
  22. #include "checkasm.h"
  23. #include "libavcodec/vvc/ctu.h"
  24. #include "libavcodec/vvc/data.h"
  25. #include "libavcodec/vvc/dsp.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mem_internal.h"
  29. static const uint32_t pixel_mask[] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff, 0x3fff3fff, 0xffffffff };
  30. static const int sizes[] = { 2, 4, 8, 16, 32, 64, 128 };
  31. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  32. #define PIXEL_STRIDE (MAX_CTU_SIZE * 2)
  33. #define EXTRA_BEFORE 3
  34. #define EXTRA_AFTER 4
  35. #define SRC_EXTRA (EXTRA_BEFORE + EXTRA_AFTER) * 2
  36. #define SRC_BUF_SIZE (PIXEL_STRIDE + SRC_EXTRA) * (PIXEL_STRIDE + SRC_EXTRA)
  37. #define DST_BUF_SIZE (MAX_CTU_SIZE * MAX_CTU_SIZE * 2)
  38. #define SRC_OFFSET ((PIXEL_STRIDE + EXTRA_BEFORE * 2) * EXTRA_BEFORE)
  39. #define randomize_buffers(buf0, buf1, size, mask) \
  40. do { \
  41. int k; \
  42. for (k = 0; k < size; k += 4 / sizeof(*buf0)) { \
  43. uint32_t r = rnd() & mask; \
  44. AV_WN32A(buf0 + k, r); \
  45. AV_WN32A(buf1 + k, r); \
  46. } \
  47. } while (0)
  48. #define randomize_pixels(buf0, buf1, size) \
  49. do { \
  50. uint32_t mask = pixel_mask[(bit_depth - 8) >> 1]; \
  51. randomize_buffers(buf0, buf1, size, mask); \
  52. } while (0)
  53. #define randomize_avg_src(buf0, buf1, size) \
  54. do { \
  55. uint32_t mask = 0x3fff3fff; \
  56. randomize_buffers(buf0, buf1, size, mask); \
  57. } while (0)
  58. static void check_put_vvc_luma(void)
  59. {
  60. LOCAL_ALIGNED_32(int16_t, dst0, [DST_BUF_SIZE / 2]);
  61. LOCAL_ALIGNED_32(int16_t, dst1, [DST_BUF_SIZE / 2]);
  62. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  63. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  64. VVCDSPContext c;
  65. declare_func(void, int16_t *dst, const uint8_t *src, const ptrdiff_t src_stride,
  66. const int height, const int8_t *hf, const int8_t *vf, const int width);
  67. for (int bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  68. randomize_pixels(src0, src1, SRC_BUF_SIZE);
  69. ff_vvc_dsp_init(&c, bit_depth);
  70. for (int i = 0; i < 2; i++) {
  71. for (int j = 0; j < 2; j++) {
  72. for (int h = 4; h <= MAX_CTU_SIZE; h *= 2) {
  73. for (int w = 4; w <= MAX_CTU_SIZE; w *= 2) {
  74. const int idx = av_log2(w) - 1;
  75. const int mx = rnd() % 16;
  76. const int my = rnd() % 16;
  77. const int8_t *hf = ff_vvc_inter_luma_filters[rnd() % 3][mx];
  78. const int8_t *vf = ff_vvc_inter_luma_filters[rnd() % 3][my];
  79. const char *type;
  80. switch ((j << 1) | i) {
  81. case 0: type = "put_luma_pixels"; break; // 0 0
  82. case 1: type = "put_luma_h"; break; // 0 1
  83. case 2: type = "put_luma_v"; break; // 1 0
  84. case 3: type = "put_luma_hv"; break; // 1 1
  85. }
  86. if (check_func(c.inter.put[LUMA][idx][j][i], "%s_%d_%dx%d", type, bit_depth, w, h)) {
  87. memset(dst0, 0, DST_BUF_SIZE);
  88. memset(dst1, 0, DST_BUF_SIZE);
  89. call_ref(dst0, src0 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  90. call_new(dst1, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  91. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  92. fail();
  93. if (w == h)
  94. bench_new(dst1, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. report("put_luma");
  102. }
  103. static void check_put_vvc_luma_uni(void)
  104. {
  105. LOCAL_ALIGNED_32(uint8_t, dst0, [DST_BUF_SIZE]);
  106. LOCAL_ALIGNED_32(uint8_t, dst1, [DST_BUF_SIZE]);
  107. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  108. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  109. VVCDSPContext c;
  110. declare_func(void, uint8_t *dst, ptrdiff_t dststride,
  111. const uint8_t *src, ptrdiff_t srcstride, int height,
  112. const int8_t *hf, const int8_t *vf, int width);
  113. for (int bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  114. ff_vvc_dsp_init(&c, bit_depth);
  115. randomize_pixels(src0, src1, SRC_BUF_SIZE);
  116. for (int i = 0; i < 2; i++) {
  117. for (int j = 0; j < 2; j++) {
  118. for (int h = 4; h <= MAX_CTU_SIZE; h *= 2) {
  119. for (int w = 4; w <= MAX_CTU_SIZE; w *= 2) {
  120. const int idx = av_log2(w) - 1;
  121. const int mx = rnd() % VVC_INTER_LUMA_FACTS;
  122. const int my = rnd() % VVC_INTER_LUMA_FACTS;
  123. const int8_t *hf = ff_vvc_inter_luma_filters[rnd() % VVC_INTER_LUMA_FILTER_TYPES][mx];
  124. const int8_t *vf = ff_vvc_inter_luma_filters[rnd() % VVC_INTER_LUMA_FILTER_TYPES][my];
  125. const char *type;
  126. switch ((j << 1) | i) {
  127. case 0: type = "put_uni_pixels"; break; // 0 0
  128. case 1: type = "put_uni_h"; break; // 0 1
  129. case 2: type = "put_uni_v"; break; // 1 0
  130. case 3: type = "put_uni_hv"; break; // 1 1
  131. }
  132. if (check_func(c.inter.put_uni[LUMA][idx][j][i], "%s_luma_%d_%dx%d", type, bit_depth, w, h)) {
  133. memset(dst0, 0, DST_BUF_SIZE);
  134. memset(dst1, 0, DST_BUF_SIZE);
  135. call_ref(dst0, PIXEL_STRIDE, src0 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  136. call_new(dst1, PIXEL_STRIDE, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  137. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  138. fail();
  139. if (w == h)
  140. bench_new(dst1, PIXEL_STRIDE, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. report("put_uni_luma");
  148. }
  149. static void check_put_vvc_chroma(void)
  150. {
  151. LOCAL_ALIGNED_32(int16_t, dst0, [DST_BUF_SIZE / 2]);
  152. LOCAL_ALIGNED_32(int16_t, dst1, [DST_BUF_SIZE / 2]);
  153. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  154. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  155. VVCDSPContext c;
  156. declare_func(void, int16_t *dst, const uint8_t *src, const ptrdiff_t src_stride,
  157. const int height, const int8_t *hf, const int8_t *vf, const int width);
  158. for (int bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  159. randomize_pixels(src0, src1, SRC_BUF_SIZE);
  160. ff_vvc_dsp_init(&c, bit_depth);
  161. for (int i = 0; i < 2; i++) {
  162. for (int j = 0; j < 2; j++) {
  163. for (int h = 2; h <= MAX_CTU_SIZE; h *= 2) {
  164. for (int w = 2; w <= MAX_CTU_SIZE; w *= 2) {
  165. const int idx = av_log2(w) - 1;
  166. const int mx = rnd() % VVC_INTER_CHROMA_FACTS;
  167. const int my = rnd() % VVC_INTER_CHROMA_FACTS;
  168. const int8_t *hf = ff_vvc_inter_chroma_filters[rnd() % VVC_INTER_CHROMA_FILTER_TYPES][mx];
  169. const int8_t *vf = ff_vvc_inter_chroma_filters[rnd() % VVC_INTER_CHROMA_FILTER_TYPES][my];
  170. const char *type;
  171. switch ((j << 1) | i) {
  172. case 0: type = "put_chroma_pixels"; break; // 0 0
  173. case 1: type = "put_chroma_h"; break; // 0 1
  174. case 2: type = "put_chroma_v"; break; // 1 0
  175. case 3: type = "put_chroma_hv"; break; // 1 1
  176. }
  177. if (check_func(c.inter.put[CHROMA][idx][j][i], "%s_%d_%dx%d", type, bit_depth, w, h)) {
  178. memset(dst0, 0, DST_BUF_SIZE);
  179. memset(dst1, 0, DST_BUF_SIZE);
  180. call_ref(dst0, src0 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  181. call_new(dst1, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  182. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  183. fail();
  184. if (w == h)
  185. bench_new(dst1, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. report("put_chroma");
  193. }
  194. static void check_put_vvc_chroma_uni(void)
  195. {
  196. LOCAL_ALIGNED_32(uint8_t, dst0, [DST_BUF_SIZE]);
  197. LOCAL_ALIGNED_32(uint8_t, dst1, [DST_BUF_SIZE]);
  198. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  199. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  200. VVCDSPContext c;
  201. declare_func(void, uint8_t *dst, ptrdiff_t dststride,
  202. const uint8_t *src, ptrdiff_t srcstride, int height,
  203. const int8_t *hf, const int8_t *vf, int width);
  204. for (int bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  205. ff_vvc_dsp_init(&c, bit_depth);
  206. randomize_pixels(src0, src1, SRC_BUF_SIZE);
  207. for (int i = 0; i < 2; i++) {
  208. for (int j = 0; j < 2; j++) {
  209. for (int h = 4; h <= MAX_CTU_SIZE; h *= 2) {
  210. for (int w = 4; w <= MAX_CTU_SIZE; w *= 2) {
  211. const int idx = av_log2(w) - 1;
  212. const int mx = rnd() % VVC_INTER_CHROMA_FACTS;
  213. const int my = rnd() % VVC_INTER_CHROMA_FACTS;
  214. const int8_t *hf = ff_vvc_inter_chroma_filters[rnd() % VVC_INTER_CHROMA_FILTER_TYPES][mx];
  215. const int8_t *vf = ff_vvc_inter_chroma_filters[rnd() % VVC_INTER_CHROMA_FILTER_TYPES][my];
  216. const char *type;
  217. switch ((j << 1) | i) {
  218. case 0: type = "put_uni_pixels"; break; // 0 0
  219. case 1: type = "put_uni_h"; break; // 0 1
  220. case 2: type = "put_uni_v"; break; // 1 0
  221. case 3: type = "put_uni_hv"; break; // 1 1
  222. }
  223. if (check_func(c.inter.put_uni[CHROMA][idx][j][i], "%s_chroma_%d_%dx%d", type, bit_depth, w, h)) {
  224. memset(dst0, 0, DST_BUF_SIZE);
  225. memset(dst1, 0, DST_BUF_SIZE);
  226. call_ref(dst0, PIXEL_STRIDE, src0 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  227. call_new(dst1, PIXEL_STRIDE, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  228. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  229. fail();
  230. if (w == h)
  231. bench_new(dst1, PIXEL_STRIDE, src1 + SRC_OFFSET, PIXEL_STRIDE, h, hf, vf, w);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. report("put_uni_chroma");
  239. }
  240. #define AVG_SRC_BUF_SIZE (MAX_CTU_SIZE * MAX_CTU_SIZE)
  241. #define AVG_DST_BUF_SIZE (MAX_PB_SIZE * MAX_PB_SIZE * 2)
  242. static void check_avg(void)
  243. {
  244. LOCAL_ALIGNED_32(int16_t, src00, [AVG_SRC_BUF_SIZE]);
  245. LOCAL_ALIGNED_32(int16_t, src01, [AVG_SRC_BUF_SIZE]);
  246. LOCAL_ALIGNED_32(int16_t, src10, [AVG_SRC_BUF_SIZE]);
  247. LOCAL_ALIGNED_32(int16_t, src11, [AVG_SRC_BUF_SIZE]);
  248. LOCAL_ALIGNED_32(uint8_t, dst0, [AVG_DST_BUF_SIZE]);
  249. LOCAL_ALIGNED_32(uint8_t, dst1, [AVG_DST_BUF_SIZE]);
  250. VVCDSPContext c;
  251. for (int bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  252. randomize_avg_src((uint8_t*)src00, (uint8_t*)src10, AVG_SRC_BUF_SIZE * sizeof(int16_t));
  253. randomize_avg_src((uint8_t*)src01, (uint8_t*)src11, AVG_SRC_BUF_SIZE * sizeof(int16_t));
  254. ff_vvc_dsp_init(&c, bit_depth);
  255. for (int h = 2; h <= MAX_CTU_SIZE; h *= 2) {
  256. for (int w = 2; w <= MAX_CTU_SIZE; w *= 2) {
  257. {
  258. declare_func(void, uint8_t *dst, ptrdiff_t dst_stride,
  259. const int16_t *src0, const int16_t *src1, int width, int height);
  260. if (check_func(c.inter.avg, "avg_%d_%dx%d", bit_depth, w, h)) {
  261. memset(dst0, 0, AVG_DST_BUF_SIZE);
  262. memset(dst1, 0, AVG_DST_BUF_SIZE);
  263. call_ref(dst0, MAX_CTU_SIZE * SIZEOF_PIXEL, src00, src01, w, h);
  264. call_new(dst1, MAX_CTU_SIZE * SIZEOF_PIXEL, src10, src11, w, h);
  265. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  266. fail();
  267. if (w == h)
  268. bench_new(dst0, MAX_CTU_SIZE * SIZEOF_PIXEL, src00, src01, w, h);
  269. }
  270. }
  271. {
  272. declare_func(void, uint8_t *dst, ptrdiff_t dst_stride,
  273. const int16_t *src0, const int16_t *src1, int width, int height,
  274. int denom, int w0, int w1, int o0, int o1);
  275. {
  276. const int denom = rnd() % 8;
  277. const int w0 = rnd() % 256 - 128;
  278. const int w1 = rnd() % 256 - 128;
  279. const int o0 = rnd() % 256 - 128;
  280. const int o1 = rnd() % 256 - 128;
  281. if (check_func(c.inter.w_avg, "w_avg_%d_%dx%d", bit_depth, w, h)) {
  282. memset(dst0, 0, AVG_DST_BUF_SIZE);
  283. memset(dst1, 0, AVG_DST_BUF_SIZE);
  284. call_ref(dst0, MAX_CTU_SIZE * SIZEOF_PIXEL, src00, src01, w, h, denom, w0, w1, o0, o1);
  285. call_new(dst1, MAX_CTU_SIZE * SIZEOF_PIXEL, src10, src11, w, h, denom, w0, w1, o0, o1);
  286. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  287. fail();
  288. if (w == h)
  289. bench_new(dst0, MAX_CTU_SIZE * SIZEOF_PIXEL, src00, src01, w, h, denom, w0, w1, o0, o1);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. report("avg");
  297. }
  298. static void check_vvc_sad(void)
  299. {
  300. const int bit_depth = 10;
  301. VVCDSPContext c;
  302. LOCAL_ALIGNED_32(uint16_t, src0, [MAX_CTU_SIZE * MAX_CTU_SIZE * 4]);
  303. LOCAL_ALIGNED_32(uint16_t, src1, [MAX_CTU_SIZE * MAX_CTU_SIZE * 4]);
  304. declare_func(int, const int16_t *src0, const int16_t *src1, int dx, int dy, int block_w, int block_h);
  305. ff_vvc_dsp_init(&c, bit_depth);
  306. randomize_pixels(src0, src1, MAX_CTU_SIZE * MAX_CTU_SIZE * 4);
  307. for (int h = 8; h <= 16; h *= 2) {
  308. for (int w = 8; w <= 16; w *= 2) {
  309. for(int offy = 0; offy <= 4; offy++) {
  310. for(int offx = 0; offx <= 4; offx++) {
  311. if (w * h < 128)
  312. continue;
  313. if (check_func(c.inter.sad, "sad_%dx%d", w, h)) {
  314. int result0;
  315. int result1;
  316. result0 = call_ref(src0 + PIXEL_STRIDE * 2 + 2, src1 + PIXEL_STRIDE * 2 + 2, offx, offy, w, h);
  317. result1 = call_new(src0 + PIXEL_STRIDE * 2 + 2, src1 + PIXEL_STRIDE * 2 + 2, offx, offy, w, h);
  318. if (result1 != result0)
  319. fail();
  320. if(offx == 0 && offy == 0)
  321. bench_new(src0 + PIXEL_STRIDE * 2 + 2, src1 + PIXEL_STRIDE * 2 + 2, offx, offy, w, h);
  322. }
  323. }
  324. }
  325. }
  326. }
  327. report("sad");
  328. }
  329. void checkasm_check_vvc_mc(void)
  330. {
  331. check_vvc_sad();
  332. check_put_vvc_luma();
  333. check_put_vvc_luma_uni();
  334. check_put_vvc_chroma();
  335. check_put_vvc_chroma_uni();
  336. check_avg();
  337. }