h264dsp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2016 Martin Storsjo
  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 "checkasm.h"
  22. #include "libavcodec/avcodec.h"
  23. #include "libavcodec/h264dsp.h"
  24. #include "libavcodec/h264data.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/intreadwrite.h"
  28. static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff };
  29. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  30. #define SIZEOF_COEF (2 * ((bit_depth + 7) / 8))
  31. #define PIXEL_STRIDE 16
  32. #define randomize_buffers() \
  33. do { \
  34. int x, y; \
  35. uint32_t mask = pixel_mask[bit_depth - 8]; \
  36. for (y = 0; y < sz; y++) { \
  37. for (x = 0; x < PIXEL_STRIDE; x += 4) { \
  38. AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask); \
  39. AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask); \
  40. } \
  41. for (x = 0; x < sz; x++) { \
  42. if (bit_depth == 8) { \
  43. coef[y * sz + x] = src[y * PIXEL_STRIDE + x] - \
  44. dst[y * PIXEL_STRIDE + x]; \
  45. } else { \
  46. ((int32_t *)coef)[y * sz + x] = \
  47. ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] - \
  48. ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x]; \
  49. } \
  50. } \
  51. } \
  52. } while (0)
  53. #define dct4x4_impl(size, dctcoef) \
  54. static void dct4x4_##size(dctcoef *coef) \
  55. { \
  56. int i, y, x; \
  57. dctcoef tmp[16]; \
  58. for (i = 0; i < 4; i++) { \
  59. const int z0 = coef[i*4 + 0] + coef[i*4 + 3]; \
  60. const int z1 = coef[i*4 + 1] + coef[i*4 + 2]; \
  61. const int z2 = coef[i*4 + 0] - coef[i*4 + 3]; \
  62. const int z3 = coef[i*4 + 1] - coef[i*4 + 2]; \
  63. tmp[i + 4*0] = z0 + z1; \
  64. tmp[i + 4*1] = 2*z2 + z3; \
  65. tmp[i + 4*2] = z0 - z1; \
  66. tmp[i + 4*3] = z2 - 2*z3; \
  67. } \
  68. for (i = 0; i < 4; i++) { \
  69. const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3]; \
  70. const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2]; \
  71. const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3]; \
  72. const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2]; \
  73. coef[i*4 + 0] = z0 + z1; \
  74. coef[i*4 + 1] = 2*z2 + z3; \
  75. coef[i*4 + 2] = z0 - z1; \
  76. coef[i*4 + 3] = z2 - 2*z3; \
  77. } \
  78. for (y = 0; y < 4; y++) { \
  79. for (x = 0; x < 4; x++) { \
  80. static const int scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \
  81. const int idx = (y & 1) + (x & 1); \
  82. coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15; \
  83. } \
  84. } \
  85. }
  86. #define DCT8_1D(src, srcstride, dst, dststride) do { \
  87. const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7]; \
  88. const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7]; \
  89. const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6]; \
  90. const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6]; \
  91. const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5]; \
  92. const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5]; \
  93. const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4]; \
  94. const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4]; \
  95. const int b0 = a0 + a6; \
  96. const int b1 = a2 + a4; \
  97. const int b2 = a0 - a6; \
  98. const int b3 = a2 - a4; \
  99. const int b4 = a3 + a5 + (a1 + (a1 >> 1)); \
  100. const int b5 = a1 - a7 - (a5 + (a5 >> 1)); \
  101. const int b6 = a1 + a7 - (a3 + (a3 >> 1)); \
  102. const int b7 = a3 - a5 + (a7 + (a7 >> 1)); \
  103. (dst)[dststride * 0] = b0 + b1; \
  104. (dst)[dststride * 1] = b4 + (b7 >> 2); \
  105. (dst)[dststride * 2] = b2 + (b3 >> 1); \
  106. (dst)[dststride * 3] = b5 + (b6 >> 2); \
  107. (dst)[dststride * 4] = b0 - b1; \
  108. (dst)[dststride * 5] = b6 - (b5 >> 2); \
  109. (dst)[dststride * 6] = (b2 >> 1) - b3; \
  110. (dst)[dststride * 7] = (b4 >> 2) - b7; \
  111. } while (0)
  112. #define dct8x8_impl(size, dctcoef) \
  113. static void dct8x8_##size(dctcoef *coef) \
  114. { \
  115. int i, x, y; \
  116. dctcoef tmp[64]; \
  117. for (i = 0; i < 8; i++) \
  118. DCT8_1D(coef + i, 8, tmp + i, 8); \
  119. \
  120. for (i = 0; i < 8; i++) \
  121. DCT8_1D(tmp + 8*i, 1, coef + i, 8); \
  122. \
  123. for (y = 0; y < 8; y++) { \
  124. for (x = 0; x < 8; x++) { \
  125. static const int scale[] = { \
  126. 13107 * 20, 11428 * 18, 20972 * 32, \
  127. 12222 * 19, 16777 * 25, 15481 * 24, \
  128. }; \
  129. static const int idxmap[] = { \
  130. 0, 3, 4, 3, \
  131. 3, 1, 5, 1, \
  132. 4, 5, 2, 5, \
  133. 3, 1, 5, 1, \
  134. }; \
  135. const int idx = idxmap[(y & 3) * 4 + (x & 3)]; \
  136. coef[y*8 + x] = ((int64_t)coef[y*8 + x] * \
  137. scale[idx] + (1 << 17)) >> 18; \
  138. } \
  139. } \
  140. }
  141. dct4x4_impl(16, int16_t)
  142. dct4x4_impl(32, int32_t)
  143. dct8x8_impl(16, int16_t)
  144. dct8x8_impl(32, int32_t)
  145. static void dct4x4(int16_t *coef, int bit_depth)
  146. {
  147. if (bit_depth == 8)
  148. dct4x4_16(coef);
  149. else
  150. dct4x4_32((int32_t *) coef);
  151. }
  152. static void dct8x8(int16_t *coef, int bit_depth)
  153. {
  154. if (bit_depth == 8) {
  155. dct8x8_16(coef);
  156. } else {
  157. dct8x8_32((int32_t *) coef);
  158. }
  159. }
  160. static void check_idct(void)
  161. {
  162. LOCAL_ALIGNED_16(uint8_t, src, [8 * 8 * 2]);
  163. LOCAL_ALIGNED_16(uint8_t, dst, [8 * 8 * 2]);
  164. LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]);
  165. LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]);
  166. LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]);
  167. LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]);
  168. LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]);
  169. H264DSPContext h;
  170. int bit_depth, sz, align, dc;
  171. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride);
  172. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  173. ff_h264dsp_init(&h, bit_depth, 1);
  174. for (sz = 4; sz <= 8; sz += 4) {
  175. randomize_buffers();
  176. if (sz == 4)
  177. dct4x4(coef, bit_depth);
  178. else
  179. dct8x8(coef, bit_depth);
  180. for (dc = 0; dc <= 1; dc++) {
  181. void (*idct)(uint8_t *, int16_t *, int) = NULL;
  182. switch ((sz << 1) | dc) {
  183. case (4 << 1) | 0: idct = h.h264_idct_add; break;
  184. case (4 << 1) | 1: idct = h.h264_idct_dc_add; break;
  185. case (8 << 1) | 0: idct = h.h264_idct8_add; break;
  186. case (8 << 1) | 1: idct = h.h264_idct8_dc_add; break;
  187. }
  188. if (check_func(idct, "h264_idct%d_add%s_%dbpp", sz, dc ? "_dc" : "", bit_depth)) {
  189. for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) {
  190. uint8_t *dst1 = dst1_base + align;
  191. if (dc) {
  192. memset(subcoef0, 0, sz * sz * SIZEOF_COEF);
  193. memcpy(subcoef0, coef, SIZEOF_COEF);
  194. } else {
  195. memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
  196. }
  197. memcpy(dst0, dst, sz * PIXEL_STRIDE);
  198. memcpy(dst1, dst, sz * PIXEL_STRIDE);
  199. memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
  200. call_ref(dst0, subcoef0, PIXEL_STRIDE);
  201. call_new(dst1, subcoef1, PIXEL_STRIDE);
  202. if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) ||
  203. memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF))
  204. fail();
  205. bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. static void check_idct_multiple(void)
  213. {
  214. LOCAL_ALIGNED_16(uint8_t, dst_full, [16 * 16 * 2]);
  215. LOCAL_ALIGNED_16(int16_t, coef_full, [16 * 16 * 2]);
  216. LOCAL_ALIGNED_16(uint8_t, dst0, [16 * 16 * 2]);
  217. LOCAL_ALIGNED_16(uint8_t, dst1, [16 * 16 * 2]);
  218. LOCAL_ALIGNED_16(int16_t, coef0, [16 * 16 * 2]);
  219. LOCAL_ALIGNED_16(int16_t, coef1, [16 * 16 * 2]);
  220. LOCAL_ALIGNED_16(uint8_t, nnzc, [15 * 8]);
  221. H264DSPContext h;
  222. int bit_depth, i, y, func;
  223. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]);
  224. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  225. ff_h264dsp_init(&h, bit_depth, 1);
  226. for (func = 0; func < 3; func++) {
  227. void (*idct)(uint8_t *, const int *, int16_t *, int, const uint8_t[]) = NULL;
  228. const char *name;
  229. int sz = 4, intra = 0;
  230. int block_offset[16] = { 0 };
  231. switch (func) {
  232. case 0:
  233. idct = h.h264_idct_add16;
  234. name = "h264_idct_add16";
  235. break;
  236. case 1:
  237. idct = h.h264_idct_add16intra;
  238. name = "h264_idct_add16intra";
  239. intra = 1;
  240. break;
  241. case 2:
  242. idct = h.h264_idct8_add4;
  243. name = "h264_idct8_add4";
  244. sz = 8;
  245. break;
  246. }
  247. memset(nnzc, 0, 15 * 8);
  248. memset(coef_full, 0, 16 * 16 * SIZEOF_COEF);
  249. for (i = 0; i < 16 * 16; i += sz * sz) {
  250. uint8_t src[8 * 8 * 2];
  251. uint8_t dst[8 * 8 * 2];
  252. int16_t coef[8 * 8 * 2];
  253. int index = i / sz;
  254. int block_y = (index / 16) * sz;
  255. int block_x = index % 16;
  256. int offset = (block_y * 16 + block_x) * SIZEOF_PIXEL;
  257. int nnz = rnd() % 3;
  258. randomize_buffers();
  259. if (sz == 4)
  260. dct4x4(coef, bit_depth);
  261. else
  262. dct8x8(coef, bit_depth);
  263. for (y = 0; y < sz; y++)
  264. memcpy(&dst_full[offset + y * 16 * SIZEOF_PIXEL],
  265. &dst[PIXEL_STRIDE * y], sz * SIZEOF_PIXEL);
  266. if (nnz > 1)
  267. nnz = sz * sz;
  268. memcpy(&coef_full[i * SIZEOF_COEF/sizeof(coef[0])],
  269. coef, nnz * SIZEOF_COEF);
  270. if (intra && nnz == 1)
  271. nnz = 0;
  272. nnzc[scan8[i / 16]] = nnz;
  273. block_offset[i / 16] = offset;
  274. }
  275. if (check_func(idct, "%s_%dbpp", name, bit_depth)) {
  276. memcpy(coef0, coef_full, 16 * 16 * SIZEOF_COEF);
  277. memcpy(coef1, coef_full, 16 * 16 * SIZEOF_COEF);
  278. memcpy(dst0, dst_full, 16 * 16 * SIZEOF_PIXEL);
  279. memcpy(dst1, dst_full, 16 * 16 * SIZEOF_PIXEL);
  280. call_ref(dst0, block_offset, coef0, 16 * SIZEOF_PIXEL, nnzc);
  281. call_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
  282. if (memcmp(dst0, dst1, 16 * 16 * SIZEOF_PIXEL) ||
  283. memcmp(coef0, coef1, 16 * 16 * SIZEOF_COEF))
  284. fail();
  285. bench_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
  286. }
  287. }
  288. }
  289. }
  290. void checkasm_check_h264dsp(void)
  291. {
  292. check_idct();
  293. check_idct_multiple();
  294. report("idct");
  295. }