vvc_alf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2023-2024 Nuo Mi <nuomi2021@gmail.com>
  3. * Copyright (c) 2023-2024 Wu Jianhua <toqsxw@outlook.com>
  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[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
  30. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  31. #define SRC_PIXEL_STRIDE (MAX_CTU_SIZE + 2 * ALF_PADDING_SIZE)
  32. #define DST_PIXEL_STRIDE (SRC_PIXEL_STRIDE + 4)
  33. #define SRC_BUF_SIZE (SRC_PIXEL_STRIDE * (MAX_CTU_SIZE + 3 * 2) * 2) //+3 * 2 for top and bottom row, *2 for high bit depth
  34. #define DST_BUF_SIZE (DST_PIXEL_STRIDE * (MAX_CTU_SIZE + 3 * 2) * 2)
  35. #define LUMA_PARAMS_SIZE (MAX_CTU_SIZE * MAX_CTU_SIZE / ALF_BLOCK_SIZE / ALF_BLOCK_SIZE * ALF_NUM_COEFF_LUMA)
  36. #define randomize_buffers(buf0, buf1, size) \
  37. do { \
  38. uint32_t mask = pixel_mask[(bit_depth - 8) >> 1]; \
  39. int k; \
  40. for (k = 0; k < size; k += 4) { \
  41. uint32_t r = rnd() & mask; \
  42. AV_WN32A(buf0 + k, r); \
  43. AV_WN32A(buf1 + k, r); \
  44. } \
  45. } while (0)
  46. #define randomize_buffers2(buf, size, filter) \
  47. do { \
  48. int k; \
  49. if (filter) { \
  50. for (k = 0; k < size; k++) { \
  51. int8_t r = rnd(); \
  52. buf[k] = r; \
  53. } \
  54. } else { \
  55. for (k = 0; k < size; k++) { \
  56. int r = rnd() % FF_ARRAY_ELEMS(clip_set); \
  57. buf[k] = clip_set[r]; \
  58. } \
  59. } \
  60. } while (0)
  61. static int get_alf_vb_pos(const int h, const int vb_pos_above)
  62. {
  63. if (h == MAX_CTU_SIZE)
  64. return MAX_CTU_SIZE - vb_pos_above;
  65. // If h < MAX_CTU_SIZE and picture virtual boundaries are involved, ALF virtual boundaries can either be within or outside this ALF block.
  66. return ((rnd() & 1) ? h : MAX_CTU_SIZE) - vb_pos_above;
  67. }
  68. static void check_alf_filter(VVCDSPContext *c, const int bit_depth)
  69. {
  70. LOCAL_ALIGNED_32(uint8_t, dst0, [DST_BUF_SIZE]);
  71. LOCAL_ALIGNED_32(uint8_t, dst1, [DST_BUF_SIZE]);
  72. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  73. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  74. int16_t filter[LUMA_PARAMS_SIZE];
  75. int16_t clip[LUMA_PARAMS_SIZE];
  76. const int16_t clip_set[] = {
  77. 1 << bit_depth, 1 << (bit_depth - 3), 1 << (bit_depth - 5), 1 << (bit_depth - 7)
  78. };
  79. ptrdiff_t src_stride = SRC_PIXEL_STRIDE * SIZEOF_PIXEL;
  80. ptrdiff_t dst_stride = DST_PIXEL_STRIDE * SIZEOF_PIXEL;
  81. int offset = (3 * SRC_PIXEL_STRIDE + 3) * SIZEOF_PIXEL;
  82. declare_func(void, uint8_t *dst, ptrdiff_t dst_stride, const uint8_t *src, ptrdiff_t src_stride,
  83. int width, int height, const int16_t *filter, const int16_t *clip, const int vb_pos);
  84. randomize_buffers(src0, src1, SRC_BUF_SIZE);
  85. randomize_buffers2(filter, LUMA_PARAMS_SIZE, 1);
  86. randomize_buffers2(clip, LUMA_PARAMS_SIZE, 0);
  87. for (int h = 4; h <= MAX_CTU_SIZE; h += 4) {
  88. for (int w = 4; w <= MAX_CTU_SIZE; w += 4) {
  89. //Both picture size and virtual boundaries are 8-aligned. For luma, we only need to check 8-aligned sizes.
  90. if (!(w % 8) && !(h % 8)) {
  91. if (check_func(c->alf.filter[LUMA], "vvc_alf_filter_luma_%dx%d_%d", w, h, bit_depth)) {
  92. const int vb_pos = get_alf_vb_pos(h, ALF_VB_POS_ABOVE_LUMA);
  93. memset(dst0, 0, DST_BUF_SIZE);
  94. memset(dst1, 0, DST_BUF_SIZE);
  95. call_ref(dst0, dst_stride, src0 + offset, src_stride, w, h, filter, clip, vb_pos);
  96. call_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  97. for (int i = 0; i < (h + 1); i++) {
  98. if (memcmp(dst0 + i * dst_stride, dst1 + i * dst_stride, (w + 1) * SIZEOF_PIXEL))
  99. fail();
  100. }
  101. // Bench only square sizes, and ones with dimensions being a power of two.
  102. if (w == h && (w & (w - 1)) == 0)
  103. bench_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  104. }
  105. }
  106. //For chroma, once it exceeds 64, it's not a 4:2:0 format, so we only need to check 8-aligned sizes as well.
  107. if ((w <= 64 || !(w % 8)) && (h <= 64 || !(h % 8))) {
  108. if (check_func(c->alf.filter[CHROMA], "vvc_alf_filter_chroma_%dx%d_%d", w, h, bit_depth)) {
  109. const int vb_pos = get_alf_vb_pos(h, ALF_VB_POS_ABOVE_CHROMA);
  110. memset(dst0, 0, DST_BUF_SIZE);
  111. memset(dst1, 0, DST_BUF_SIZE);
  112. call_ref(dst0, dst_stride, src0 + offset, src_stride, w, h, filter, clip, vb_pos);
  113. call_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  114. for (int i = 0; i < (h + 1); i++) {
  115. if (memcmp(dst0 + i * dst_stride, dst1 + i * dst_stride, (w + 1) * SIZEOF_PIXEL))
  116. fail();
  117. }
  118. if (w == h && (w & (w - 1)) == 0)
  119. bench_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. static void check_alf_classify(VVCDSPContext *c, const int bit_depth)
  126. {
  127. LOCAL_ALIGNED_32(int, class_idx0, [SRC_BUF_SIZE]);
  128. LOCAL_ALIGNED_32(int, transpose_idx0, [SRC_BUF_SIZE]);
  129. LOCAL_ALIGNED_32(int, class_idx1, [SRC_BUF_SIZE]);
  130. LOCAL_ALIGNED_32(int, transpose_idx1, [SRC_BUF_SIZE]);
  131. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  132. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  133. LOCAL_ALIGNED_32(int32_t, alf_gradient_tmp, [ALF_GRADIENT_SIZE * ALF_GRADIENT_SIZE * ALF_NUM_DIR]);
  134. ptrdiff_t stride = SRC_PIXEL_STRIDE * SIZEOF_PIXEL;
  135. int offset = (3 * SRC_PIXEL_STRIDE + 3) * SIZEOF_PIXEL;
  136. declare_func(void, int *class_idx, int *transpose_idx,
  137. const uint8_t *src, ptrdiff_t src_stride, int width, int height, int vb_pos, int *gradient_tmp);
  138. randomize_buffers(src0, src1, SRC_BUF_SIZE);
  139. //Both picture size and virtual boundaries are 8-aligned. Classify is luma only, we only need to check 8-aligned sizes.
  140. for (int h = 8; h <= MAX_CTU_SIZE; h += 8) {
  141. for (int w = 8; w <= MAX_CTU_SIZE; w += 8) {
  142. const int id_size = w * h / ALF_BLOCK_SIZE / ALF_BLOCK_SIZE * sizeof(int);
  143. const int vb_pos = get_alf_vb_pos(h, ALF_VB_POS_ABOVE_LUMA);
  144. if (check_func(c->alf.classify, "vvc_alf_classify_%dx%d_%d", w, h, bit_depth)) {
  145. memset(class_idx0, 0, id_size);
  146. memset(class_idx1, 0, id_size);
  147. memset(transpose_idx0, 0, id_size);
  148. memset(transpose_idx1, 0, id_size);
  149. call_ref(class_idx0, transpose_idx0, src0 + offset, stride, w, h, vb_pos, alf_gradient_tmp);
  150. call_new(class_idx1, transpose_idx1, src1 + offset, stride, w, h, vb_pos, alf_gradient_tmp);
  151. if (memcmp(class_idx0, class_idx1, id_size))
  152. fail();
  153. if (memcmp(transpose_idx0, transpose_idx1, id_size))
  154. fail();
  155. // Bench only square sizes, and ones with dimensions being a power of two.
  156. if (w == h && (w & (w - 1)) == 0)
  157. bench_new(class_idx1, transpose_idx1, src1 + offset, stride, w, h, vb_pos, alf_gradient_tmp);
  158. }
  159. }
  160. }
  161. }
  162. void checkasm_check_vvc_alf(void)
  163. {
  164. int bit_depth;
  165. VVCDSPContext h;
  166. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  167. ff_vvc_dsp_init(&h, bit_depth);
  168. check_alf_filter(&h, bit_depth);
  169. }
  170. report("alf_filter");
  171. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  172. ff_vvc_dsp_init(&h, bit_depth);
  173. check_alf_classify(&h, bit_depth);
  174. }
  175. report("alf_classify");
  176. }