hevc_sao.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2018 Yingming Fan <yingmingfan@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <string.h>
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/mem_internal.h"
  23. #include "libavcodec/hevcdsp.h"
  24. #include "checkasm.h"
  25. static const uint32_t pixel_mask[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
  26. static const uint32_t sao_size[5] = {8, 16, 32, 48, 64};
  27. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  28. #define PIXEL_STRIDE (2*MAX_PB_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) //same with sao_edge src_stride
  29. #define BUF_SIZE (PIXEL_STRIDE * (64+2) * 2) //+2 for top and bottom row, *2 for high bit depth
  30. #define OFFSET_THRESH (1 << (bit_depth - 5))
  31. #define OFFSET_LENGTH 5
  32. #define randomize_buffers(buf0, buf1, size) \
  33. do { \
  34. uint32_t mask = pixel_mask[(bit_depth - 8) >> 1]; \
  35. int k; \
  36. for (k = 0; k < size; k += 4) { \
  37. uint32_t r = rnd() & mask; \
  38. AV_WN32A(buf0 + k, r); \
  39. AV_WN32A(buf1 + k, r); \
  40. } \
  41. } while (0)
  42. #define randomize_buffers2(buf, size) \
  43. do { \
  44. uint32_t max_offset = OFFSET_THRESH; \
  45. int k; \
  46. if (bit_depth == 8) { \
  47. for (k = 0; k < size; k++) { \
  48. uint8_t r = rnd() % max_offset; \
  49. buf[k] = r; \
  50. } \
  51. } else { \
  52. for (k = 0; k < size; k++) { \
  53. uint16_t r = rnd() % max_offset; \
  54. buf[k] = r; \
  55. } \
  56. } \
  57. } while (0)
  58. static void check_sao_band(HEVCDSPContext *h, int bit_depth)
  59. {
  60. int i;
  61. LOCAL_ALIGNED_32(uint8_t, dst0, [BUF_SIZE]);
  62. LOCAL_ALIGNED_32(uint8_t, dst1, [BUF_SIZE]);
  63. LOCAL_ALIGNED_32(uint8_t, src0, [BUF_SIZE]);
  64. LOCAL_ALIGNED_32(uint8_t, src1, [BUF_SIZE]);
  65. int16_t offset_val[OFFSET_LENGTH];
  66. int left_class = rnd()%32;
  67. for (i = 0; i <= 4; i++) {
  68. int block_size = sao_size[i];
  69. int prev_size = i > 0 ? sao_size[i - 1] : 0;
  70. ptrdiff_t stride = PIXEL_STRIDE*SIZEOF_PIXEL;
  71. declare_func(void, uint8_t *dst, uint8_t *src, ptrdiff_t dst_stride, ptrdiff_t src_stride,
  72. int16_t *sao_offset_val, int sao_left_class, int width, int height);
  73. if (check_func(h->sao_band_filter[i], "hevc_sao_band_%d_%d", block_size, bit_depth)) {
  74. for (int w = prev_size + 4; w <= block_size; w += 4) {
  75. randomize_buffers(src0, src1, BUF_SIZE);
  76. randomize_buffers2(offset_val, OFFSET_LENGTH);
  77. memset(dst0, 0, BUF_SIZE);
  78. memset(dst1, 0, BUF_SIZE);
  79. call_ref(dst0, src0, stride, stride, offset_val, left_class, w, block_size);
  80. call_new(dst1, src1, stride, stride, offset_val, left_class, w, block_size);
  81. for (int j = 0; j < block_size; j++) {
  82. if (memcmp(dst0 + j*stride, dst1 + j*stride, w*SIZEOF_PIXEL))
  83. fail();
  84. }
  85. }
  86. bench_new(dst1, src1, stride, stride, offset_val, left_class, block_size, block_size);
  87. }
  88. }
  89. }
  90. static void check_sao_edge(HEVCDSPContext *h, int bit_depth)
  91. {
  92. int i;
  93. LOCAL_ALIGNED_32(uint8_t, dst0, [BUF_SIZE]);
  94. LOCAL_ALIGNED_32(uint8_t, dst1, [BUF_SIZE]);
  95. LOCAL_ALIGNED_32(uint8_t, src0, [BUF_SIZE]);
  96. LOCAL_ALIGNED_32(uint8_t, src1, [BUF_SIZE]);
  97. int16_t offset_val[OFFSET_LENGTH];
  98. int eo = rnd()%4;
  99. for (i = 0; i <= 4; i++) {
  100. int block_size = sao_size[i];
  101. int prev_size = i > 0 ? sao_size[i - 1] : 0;
  102. ptrdiff_t stride = PIXEL_STRIDE*SIZEOF_PIXEL;
  103. int offset = (AV_INPUT_BUFFER_PADDING_SIZE + PIXEL_STRIDE)*SIZEOF_PIXEL;
  104. declare_func(void, uint8_t *dst, uint8_t *src, ptrdiff_t stride_dst,
  105. int16_t *sao_offset_val, int eo, int width, int height);
  106. for (int w = prev_size + 4; w <= block_size; w += 4) {
  107. randomize_buffers(src0, src1, BUF_SIZE);
  108. randomize_buffers2(offset_val, OFFSET_LENGTH);
  109. memset(dst0, 0, BUF_SIZE);
  110. memset(dst1, 0, BUF_SIZE);
  111. if (check_func(h->sao_edge_filter[i], "hevc_sao_edge_%d_%d", block_size, bit_depth)) {
  112. call_ref(dst0, src0 + offset, stride, offset_val, eo, w, block_size);
  113. call_new(dst1, src1 + offset, stride, offset_val, eo, w, block_size);
  114. for (int j = 0; j < block_size; j++) {
  115. if (memcmp(dst0 + j*stride, dst1 + j*stride, w*SIZEOF_PIXEL))
  116. fail();
  117. }
  118. bench_new(dst1, src1 + offset, stride, offset_val, eo, block_size, block_size);
  119. }
  120. }
  121. }
  122. }
  123. void checkasm_check_hevc_sao(void)
  124. {
  125. int bit_depth;
  126. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  127. HEVCDSPContext h;
  128. ff_hevc_dsp_init(&h, bit_depth);
  129. check_sao_band(&h, bit_depth);
  130. }
  131. report("sao_band");
  132. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  133. HEVCDSPContext h;
  134. ff_hevc_dsp_init(&h, bit_depth);
  135. check_sao_edge(&h, bit_depth);
  136. }
  137. report("sao_edge");
  138. }