You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
12KB

  1. /*
  2. * Copyright (c) 2016 Martin Storsjo
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "checkasm.h"
  22. #include "libavcodec/avcodec.h"
  23. #include "libavcodec/h264dsp.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/intreadwrite.h"
  27. static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff };
  28. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  29. #define SIZEOF_COEF (2 * ((bit_depth + 7) / 8))
  30. #define PIXEL_STRIDE 16
  31. #define randomize_buffers() \
  32. do { \
  33. int x, y; \
  34. uint32_t mask = pixel_mask[bit_depth - 8]; \
  35. for (y = 0; y < sz; y++) { \
  36. for (x = 0; x < PIXEL_STRIDE; x += 4) { \
  37. AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask); \
  38. AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask); \
  39. } \
  40. for (x = 0; x < sz; x++) { \
  41. if (bit_depth == 8) { \
  42. coef[y * sz + x] = src[y * PIXEL_STRIDE + x] - \
  43. dst[y * PIXEL_STRIDE + x]; \
  44. } else { \
  45. ((int32_t *)coef)[y * sz + x] = \
  46. ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] - \
  47. ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x]; \
  48. } \
  49. } \
  50. } \
  51. } while (0)
  52. #define dct4x4_impl(size, dctcoef) \
  53. static void dct4x4_##size(dctcoef *coef) \
  54. { \
  55. int i, y, x; \
  56. dctcoef tmp[16]; \
  57. for (i = 0; i < 4; i++) { \
  58. const int z0 = coef[i*4 + 0] + coef[i*4 + 3]; \
  59. const int z1 = coef[i*4 + 1] + coef[i*4 + 2]; \
  60. const int z2 = coef[i*4 + 0] - coef[i*4 + 3]; \
  61. const int z3 = coef[i*4 + 1] - coef[i*4 + 2]; \
  62. tmp[i + 4*0] = z0 + z1; \
  63. tmp[i + 4*1] = 2*z2 + z3; \
  64. tmp[i + 4*2] = z0 - z1; \
  65. tmp[i + 4*3] = z2 - 2*z3; \
  66. } \
  67. for (i = 0; i < 4; i++) { \
  68. const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3]; \
  69. const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2]; \
  70. const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3]; \
  71. const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2]; \
  72. coef[i*4 + 0] = z0 + z1; \
  73. coef[i*4 + 1] = 2*z2 + z3; \
  74. coef[i*4 + 2] = z0 - z1; \
  75. coef[i*4 + 3] = z2 - 2*z3; \
  76. } \
  77. for (y = 0; y < 4; y++) { \
  78. for (x = 0; x < 4; x++) { \
  79. static const int scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \
  80. const int idx = (y & 1) + (x & 1); \
  81. coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15; \
  82. } \
  83. } \
  84. }
  85. #define DCT8_1D(src, srcstride, dst, dststride) do { \
  86. const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7]; \
  87. const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7]; \
  88. const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6]; \
  89. const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6]; \
  90. const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5]; \
  91. const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5]; \
  92. const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4]; \
  93. const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4]; \
  94. const int b0 = a0 + a6; \
  95. const int b1 = a2 + a4; \
  96. const int b2 = a0 - a6; \
  97. const int b3 = a2 - a4; \
  98. const int b4 = a3 + a5 + (a1 + (a1 >> 1)); \
  99. const int b5 = a1 - a7 - (a5 + (a5 >> 1)); \
  100. const int b6 = a1 + a7 - (a3 + (a3 >> 1)); \
  101. const int b7 = a3 - a5 + (a7 + (a7 >> 1)); \
  102. (dst)[dststride * 0] = b0 + b1; \
  103. (dst)[dststride * 1] = b4 + (b7 >> 2); \
  104. (dst)[dststride * 2] = b2 + (b3 >> 1); \
  105. (dst)[dststride * 3] = b5 + (b6 >> 2); \
  106. (dst)[dststride * 4] = b0 - b1; \
  107. (dst)[dststride * 5] = b6 - (b5 >> 2); \
  108. (dst)[dststride * 6] = (b2 >> 1) - b3; \
  109. (dst)[dststride * 7] = (b4 >> 2) - b7; \
  110. } while (0)
  111. #define dct8x8_impl(size, dctcoef) \
  112. static void dct8x8_##size(dctcoef *coef) \
  113. { \
  114. int i, x, y; \
  115. dctcoef tmp[64]; \
  116. for (i = 0; i < 8; i++) \
  117. DCT8_1D(coef + i, 8, tmp + i, 8); \
  118. \
  119. for (i = 0; i < 8; i++) \
  120. DCT8_1D(tmp + 8*i, 1, coef + i, 8); \
  121. \
  122. for (y = 0; y < 8; y++) { \
  123. for (x = 0; x < 8; x++) { \
  124. static const int scale[] = { \
  125. 13107 * 20, 11428 * 18, 20972 * 32, \
  126. 12222 * 19, 16777 * 25, 15481 * 24, \
  127. }; \
  128. static const int idxmap[] = { \
  129. 0, 3, 4, 3, \
  130. 3, 1, 5, 1, \
  131. 4, 5, 2, 5, \
  132. 3, 1, 5, 1, \
  133. }; \
  134. const int idx = idxmap[(y & 3) * 4 + (x & 3)]; \
  135. coef[y*8 + x] = ((int64_t)coef[y*8 + x] * \
  136. scale[idx] + (1 << 17)) >> 18; \
  137. } \
  138. } \
  139. }
  140. dct4x4_impl(16, int16_t)
  141. dct4x4_impl(32, int32_t)
  142. dct8x8_impl(16, int16_t)
  143. dct8x8_impl(32, int32_t)
  144. static void dct4x4(int16_t *coef, int bit_depth)
  145. {
  146. if (bit_depth == 8)
  147. dct4x4_16(coef);
  148. else
  149. dct4x4_32((int32_t *) coef);
  150. }
  151. static void dct8x8(int16_t *coef, int bit_depth)
  152. {
  153. if (bit_depth == 8) {
  154. dct8x8_16(coef);
  155. } else {
  156. dct8x8_32((int32_t *) coef);
  157. }
  158. }
  159. static void check_idct(void)
  160. {
  161. LOCAL_ALIGNED_16(uint8_t, src, [8 * 8 * 2]);
  162. LOCAL_ALIGNED_16(uint8_t, dst, [8 * 8 * 2]);
  163. LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]);
  164. LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]);
  165. LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]);
  166. LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]);
  167. LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]);
  168. H264DSPContext h;
  169. int bit_depth, sz, align, dc;
  170. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride);
  171. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  172. ff_h264dsp_init(&h, bit_depth, 1);
  173. for (sz = 4; sz <= 8; sz += 4) {
  174. randomize_buffers();
  175. if (sz == 4)
  176. dct4x4(coef, bit_depth);
  177. else
  178. dct8x8(coef, bit_depth);
  179. for (dc = 0; dc <= 1; dc++) {
  180. void (*idct)(uint8_t *, int16_t *, int) = NULL;
  181. switch ((sz << 1) | dc) {
  182. case (4 << 1) | 0: idct = h.h264_idct_add; break;
  183. case (4 << 1) | 1: idct = h.h264_idct_dc_add; break;
  184. case (8 << 1) | 0: idct = h.h264_idct8_add; break;
  185. case (8 << 1) | 1: idct = h.h264_idct8_dc_add; break;
  186. }
  187. if (check_func(idct, "h264_idct%d_add%s_%dbpp", sz, dc ? "_dc" : "", bit_depth)) {
  188. for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) {
  189. uint8_t *dst1 = dst1_base + align;
  190. if (dc) {
  191. memset(subcoef0, 0, sz * sz * SIZEOF_COEF);
  192. memcpy(subcoef0, coef, SIZEOF_COEF);
  193. } else {
  194. memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
  195. }
  196. memcpy(dst0, dst, sz * PIXEL_STRIDE);
  197. memcpy(dst1, dst, sz * PIXEL_STRIDE);
  198. memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
  199. call_ref(dst0, subcoef0, PIXEL_STRIDE);
  200. call_new(dst1, subcoef1, PIXEL_STRIDE);
  201. if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) ||
  202. memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF))
  203. fail();
  204. bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. report("idct");
  211. }
  212. void checkasm_check_h264dsp(void)
  213. {
  214. check_idct();
  215. }