hevc_sao.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "libavcodec/avcodec.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. ptrdiff_t stride = PIXEL_STRIDE*SIZEOF_PIXEL;
  70. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, uint8_t *src, ptrdiff_t dst_stride, ptrdiff_t src_stride,
  71. int16_t *sao_offset_val, int sao_left_class, int width, int height);
  72. randomize_buffers(src0, src1, BUF_SIZE);
  73. randomize_buffers2(offset_val, OFFSET_LENGTH);
  74. memset(dst0, 0, BUF_SIZE);
  75. memset(dst1, 0, BUF_SIZE);
  76. if (check_func(h.sao_band_filter[i], "hevc_sao_band_%dx%d_%d", block_size, block_size, bit_depth)) {
  77. call_ref(dst0, src0, stride, stride, offset_val, left_class, block_size, block_size);
  78. call_new(dst1, src1, stride, stride, offset_val, left_class, block_size, block_size);
  79. if (memcmp(dst0, dst1, BUF_SIZE))
  80. fail();
  81. bench_new(dst1, src1, stride, stride, offset_val, left_class, block_size, block_size);
  82. }
  83. }
  84. }
  85. static void check_sao_edge(HEVCDSPContext h, int bit_depth)
  86. {
  87. int i;
  88. LOCAL_ALIGNED_32(uint8_t, dst0, [BUF_SIZE]);
  89. LOCAL_ALIGNED_32(uint8_t, dst1, [BUF_SIZE]);
  90. LOCAL_ALIGNED_32(uint8_t, src0, [BUF_SIZE]);
  91. LOCAL_ALIGNED_32(uint8_t, src1, [BUF_SIZE]);
  92. int16_t offset_val[OFFSET_LENGTH];
  93. int eo = rnd()%4;
  94. for (i = 0; i <= 4; i++) {
  95. int block_size = sao_size[i];
  96. ptrdiff_t stride = PIXEL_STRIDE*SIZEOF_PIXEL;
  97. int offset = (AV_INPUT_BUFFER_PADDING_SIZE + PIXEL_STRIDE)*SIZEOF_PIXEL;
  98. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, uint8_t *src, ptrdiff_t stride_dst,
  99. int16_t *sao_offset_val, int eo, int width, int height);
  100. randomize_buffers(src0, src1, BUF_SIZE);
  101. randomize_buffers2(offset_val, OFFSET_LENGTH);
  102. memset(dst0, 0, BUF_SIZE);
  103. memset(dst1, 0, BUF_SIZE);
  104. if (check_func(h.sao_edge_filter[i], "hevc_sao_edge_%dx%d_%d", block_size, block_size, bit_depth)) {
  105. call_ref(dst0, src0 + offset, stride, offset_val, eo, block_size, block_size);
  106. call_new(dst1, src1 + offset, stride, offset_val, eo, block_size, block_size);
  107. if (memcmp(dst0, dst1, BUF_SIZE))
  108. fail();
  109. bench_new(dst1, src1 + offset, stride, offset_val, eo, block_size, block_size);
  110. }
  111. }
  112. }
  113. void checkasm_check_hevc_sao(void)
  114. {
  115. int bit_depth;
  116. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  117. HEVCDSPContext h;
  118. ff_hevc_dsp_init(&h, bit_depth);
  119. check_sao_band(h, bit_depth);
  120. }
  121. report("sao_band");
  122. for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
  123. HEVCDSPContext h;
  124. ff_hevc_dsp_init(&h, bit_depth);
  125. check_sao_edge(h, bit_depth);
  126. }
  127. report("sao_edge");
  128. }