vvc_alf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. checkasm_check_pixel(dst0, dst_stride, dst1, dst_stride, w + 1, h + 1, "dst");
  98. // Bench only square sizes, and ones with dimensions being a power of two.
  99. if (w == h && (w & (w - 1)) == 0)
  100. bench_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  101. }
  102. }
  103. //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.
  104. if ((w <= 64 || !(w % 8)) && (h <= 64 || !(h % 8))) {
  105. if (check_func(c->alf.filter[CHROMA], "vvc_alf_filter_chroma_%dx%d_%d", w, h, bit_depth)) {
  106. const int vb_pos = get_alf_vb_pos(h, ALF_VB_POS_ABOVE_CHROMA);
  107. memset(dst0, 0, DST_BUF_SIZE);
  108. memset(dst1, 0, DST_BUF_SIZE);
  109. call_ref(dst0, dst_stride, src0 + offset, src_stride, w, h, filter, clip, vb_pos);
  110. call_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  111. checkasm_check_pixel(dst0, dst_stride, dst1, dst_stride, w + 1, h + 1, "dst");
  112. if (w == h && (w & (w - 1)) == 0)
  113. bench_new(dst1, dst_stride, src1 + offset, src_stride, w, h, filter, clip, vb_pos);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. static void check_alf_classify(VVCDSPContext *c, const int bit_depth)
  120. {
  121. LOCAL_ALIGNED_32(int, class_idx0, [SRC_BUF_SIZE]);
  122. LOCAL_ALIGNED_32(int, transpose_idx0, [SRC_BUF_SIZE]);
  123. LOCAL_ALIGNED_32(int, class_idx1, [SRC_BUF_SIZE]);
  124. LOCAL_ALIGNED_32(int, transpose_idx1, [SRC_BUF_SIZE]);
  125. LOCAL_ALIGNED_32(uint8_t, src0, [SRC_BUF_SIZE]);
  126. LOCAL_ALIGNED_32(uint8_t, src1, [SRC_BUF_SIZE]);
  127. LOCAL_ALIGNED_32(int32_t, alf_gradient_tmp, [ALF_GRADIENT_SIZE * ALF_GRADIENT_SIZE * ALF_NUM_DIR]);
  128. ptrdiff_t stride = SRC_PIXEL_STRIDE * SIZEOF_PIXEL;
  129. int offset = (3 * SRC_PIXEL_STRIDE + 3) * SIZEOF_PIXEL;
  130. declare_func(void, int *class_idx, int *transpose_idx,
  131. const uint8_t *src, ptrdiff_t src_stride, int width, int height, int vb_pos, int *gradient_tmp);
  132. randomize_buffers(src0, src1, SRC_BUF_SIZE);
  133. //Both picture size and virtual boundaries are 8-aligned. Classify is luma only, we only need to check 8-aligned sizes.
  134. for (int h = 8; h <= MAX_CTU_SIZE; h += 8) {
  135. for (int w = 8; w <= MAX_CTU_SIZE; w += 8) {
  136. const int id_size = w * h / ALF_BLOCK_SIZE / ALF_BLOCK_SIZE * sizeof(int);
  137. const int vb_pos = get_alf_vb_pos(h, ALF_VB_POS_ABOVE_LUMA);
  138. if (check_func(c->alf.classify, "vvc_alf_classify_%dx%d_%d", w, h, bit_depth)) {
  139. memset(class_idx0, 0, id_size);
  140. memset(class_idx1, 0, id_size);
  141. memset(transpose_idx0, 0, id_size);
  142. memset(transpose_idx1, 0, id_size);
  143. call_ref(class_idx0, transpose_idx0, src0 + offset, stride, w, h, vb_pos, alf_gradient_tmp);
  144. call_new(class_idx1, transpose_idx1, src1 + offset, stride, w, h, vb_pos, alf_gradient_tmp);
  145. if (memcmp(class_idx0, class_idx1, id_size))
  146. fail();
  147. if (memcmp(transpose_idx0, transpose_idx1, id_size))
  148. fail();
  149. // Bench only square sizes, and ones with dimensions being a power of two.
  150. if (w == h && (w & (w - 1)) == 0)
  151. bench_new(class_idx1, transpose_idx1, src1 + offset, stride, w, h, vb_pos, alf_gradient_tmp);
  152. }
  153. }
  154. }
  155. }
  156. void checkasm_check_vvc_alf(void)
  157. {
  158. int bit_depth;
  159. VVCDSPContext h;
  160. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  161. ff_vvc_dsp_init(&h, bit_depth);
  162. check_alf_filter(&h, bit_depth);
  163. }
  164. report("alf_filter");
  165. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  166. ff_vvc_dsp_init(&h, bit_depth);
  167. check_alf_classify(&h, bit_depth);
  168. }
  169. report("alf_classify");
  170. }