h264dsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. static const uint32_t pixel_mask_lf[3] = { 0xff0fff0f, 0x01ff000f, 0x03ff000f };
  30. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  31. #define SIZEOF_COEF (2 * ((bit_depth + 7) / 8))
  32. #define PIXEL_STRIDE 16
  33. #define randomize_buffers() \
  34. do { \
  35. int x, y; \
  36. uint32_t mask = pixel_mask[bit_depth - 8]; \
  37. for (y = 0; y < sz; y++) { \
  38. for (x = 0; x < PIXEL_STRIDE; x += 4) { \
  39. AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask); \
  40. AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask); \
  41. } \
  42. for (x = 0; x < sz; x++) { \
  43. if (bit_depth == 8) { \
  44. coef[y * sz + x] = src[y * PIXEL_STRIDE + x] - \
  45. dst[y * PIXEL_STRIDE + x]; \
  46. } else { \
  47. ((int32_t *)coef)[y * sz + x] = \
  48. ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] - \
  49. ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x]; \
  50. } \
  51. } \
  52. } \
  53. } while (0)
  54. #define dct4x4_impl(size, dctcoef) \
  55. static void dct4x4_##size(dctcoef *coef) \
  56. { \
  57. int i, y, x; \
  58. dctcoef tmp[16]; \
  59. for (i = 0; i < 4; i++) { \
  60. const int z0 = coef[i*4 + 0] + coef[i*4 + 3]; \
  61. const int z1 = coef[i*4 + 1] + coef[i*4 + 2]; \
  62. const int z2 = coef[i*4 + 0] - coef[i*4 + 3]; \
  63. const int z3 = coef[i*4 + 1] - coef[i*4 + 2]; \
  64. tmp[i + 4*0] = z0 + z1; \
  65. tmp[i + 4*1] = 2*z2 + z3; \
  66. tmp[i + 4*2] = z0 - z1; \
  67. tmp[i + 4*3] = z2 - 2*z3; \
  68. } \
  69. for (i = 0; i < 4; i++) { \
  70. const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3]; \
  71. const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2]; \
  72. const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3]; \
  73. const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2]; \
  74. coef[i*4 + 0] = z0 + z1; \
  75. coef[i*4 + 1] = 2*z2 + z3; \
  76. coef[i*4 + 2] = z0 - z1; \
  77. coef[i*4 + 3] = z2 - 2*z3; \
  78. } \
  79. for (y = 0; y < 4; y++) { \
  80. for (x = 0; x < 4; x++) { \
  81. static const int scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \
  82. const int idx = (y & 1) + (x & 1); \
  83. coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15; \
  84. } \
  85. } \
  86. }
  87. #define DCT8_1D(src, srcstride, dst, dststride) do { \
  88. const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7]; \
  89. const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7]; \
  90. const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6]; \
  91. const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6]; \
  92. const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5]; \
  93. const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5]; \
  94. const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4]; \
  95. const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4]; \
  96. const int b0 = a0 + a6; \
  97. const int b1 = a2 + a4; \
  98. const int b2 = a0 - a6; \
  99. const int b3 = a2 - a4; \
  100. const int b4 = a3 + a5 + (a1 + (a1 >> 1)); \
  101. const int b5 = a1 - a7 - (a5 + (a5 >> 1)); \
  102. const int b6 = a1 + a7 - (a3 + (a3 >> 1)); \
  103. const int b7 = a3 - a5 + (a7 + (a7 >> 1)); \
  104. (dst)[dststride * 0] = b0 + b1; \
  105. (dst)[dststride * 1] = b4 + (b7 >> 2); \
  106. (dst)[dststride * 2] = b2 + (b3 >> 1); \
  107. (dst)[dststride * 3] = b5 + (b6 >> 2); \
  108. (dst)[dststride * 4] = b0 - b1; \
  109. (dst)[dststride * 5] = b6 - (b5 >> 2); \
  110. (dst)[dststride * 6] = (b2 >> 1) - b3; \
  111. (dst)[dststride * 7] = (b4 >> 2) - b7; \
  112. } while (0)
  113. #define dct8x8_impl(size, dctcoef) \
  114. static void dct8x8_##size(dctcoef *coef) \
  115. { \
  116. int i, x, y; \
  117. dctcoef tmp[64]; \
  118. for (i = 0; i < 8; i++) \
  119. DCT8_1D(coef + i, 8, tmp + i, 8); \
  120. \
  121. for (i = 0; i < 8; i++) \
  122. DCT8_1D(tmp + 8*i, 1, coef + i, 8); \
  123. \
  124. for (y = 0; y < 8; y++) { \
  125. for (x = 0; x < 8; x++) { \
  126. static const int scale[] = { \
  127. 13107 * 20, 11428 * 18, 20972 * 32, \
  128. 12222 * 19, 16777 * 25, 15481 * 24, \
  129. }; \
  130. static const int idxmap[] = { \
  131. 0, 3, 4, 3, \
  132. 3, 1, 5, 1, \
  133. 4, 5, 2, 5, \
  134. 3, 1, 5, 1, \
  135. }; \
  136. const int idx = idxmap[(y & 3) * 4 + (x & 3)]; \
  137. coef[y*8 + x] = ((int64_t)coef[y*8 + x] * \
  138. scale[idx] + (1 << 17)) >> 18; \
  139. } \
  140. } \
  141. }
  142. dct4x4_impl(16, int16_t)
  143. dct4x4_impl(32, int32_t)
  144. dct8x8_impl(16, int16_t)
  145. dct8x8_impl(32, int32_t)
  146. static void dct4x4(int16_t *coef, int bit_depth)
  147. {
  148. if (bit_depth == 8)
  149. dct4x4_16(coef);
  150. else
  151. dct4x4_32((int32_t *) coef);
  152. }
  153. static void dct8x8(int16_t *coef, int bit_depth)
  154. {
  155. if (bit_depth == 8) {
  156. dct8x8_16(coef);
  157. } else {
  158. dct8x8_32((int32_t *) coef);
  159. }
  160. }
  161. static void check_idct(void)
  162. {
  163. LOCAL_ALIGNED_16(uint8_t, src, [8 * 8 * 2]);
  164. LOCAL_ALIGNED_16(uint8_t, dst, [8 * 8 * 2]);
  165. LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]);
  166. LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]);
  167. LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]);
  168. LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]);
  169. LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]);
  170. H264DSPContext h;
  171. int bit_depth, sz, align, dc;
  172. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride);
  173. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  174. ff_h264dsp_init(&h, bit_depth, 1);
  175. for (sz = 4; sz <= 8; sz += 4) {
  176. randomize_buffers();
  177. if (sz == 4)
  178. dct4x4(coef, bit_depth);
  179. else
  180. dct8x8(coef, bit_depth);
  181. for (dc = 0; dc <= 1; dc++) {
  182. void (*idct)(uint8_t *, int16_t *, int) = NULL;
  183. switch ((sz << 1) | dc) {
  184. case (4 << 1) | 0: idct = h.h264_idct_add; break;
  185. case (4 << 1) | 1: idct = h.h264_idct_dc_add; break;
  186. case (8 << 1) | 0: idct = h.h264_idct8_add; break;
  187. case (8 << 1) | 1: idct = h.h264_idct8_dc_add; break;
  188. }
  189. if (check_func(idct, "h264_idct%d_add%s_%dbpp", sz, dc ? "_dc" : "", bit_depth)) {
  190. for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) {
  191. uint8_t *dst1 = dst1_base + align;
  192. if (dc) {
  193. memset(subcoef0, 0, sz * sz * SIZEOF_COEF);
  194. memcpy(subcoef0, coef, SIZEOF_COEF);
  195. } else {
  196. memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
  197. }
  198. memcpy(dst0, dst, sz * PIXEL_STRIDE);
  199. memcpy(dst1, dst, sz * PIXEL_STRIDE);
  200. memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
  201. call_ref(dst0, subcoef0, PIXEL_STRIDE);
  202. call_new(dst1, subcoef1, PIXEL_STRIDE);
  203. if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) ||
  204. memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF))
  205. fail();
  206. bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. static void check_idct_multiple(void)
  214. {
  215. LOCAL_ALIGNED_16(uint8_t, dst_full, [16 * 16 * 2]);
  216. LOCAL_ALIGNED_16(int16_t, coef_full, [16 * 16 * 2]);
  217. LOCAL_ALIGNED_16(uint8_t, dst0, [16 * 16 * 2]);
  218. LOCAL_ALIGNED_16(uint8_t, dst1, [16 * 16 * 2]);
  219. LOCAL_ALIGNED_16(int16_t, coef0, [16 * 16 * 2]);
  220. LOCAL_ALIGNED_16(int16_t, coef1, [16 * 16 * 2]);
  221. LOCAL_ALIGNED_16(uint8_t, nnzc, [15 * 8]);
  222. H264DSPContext h;
  223. int bit_depth, i, y, func;
  224. 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]);
  225. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  226. ff_h264dsp_init(&h, bit_depth, 1);
  227. for (func = 0; func < 3; func++) {
  228. void (*idct)(uint8_t *, const int *, int16_t *, int, const uint8_t[]) = NULL;
  229. const char *name;
  230. int sz = 4, intra = 0;
  231. int block_offset[16] = { 0 };
  232. switch (func) {
  233. case 0:
  234. idct = h.h264_idct_add16;
  235. name = "h264_idct_add16";
  236. break;
  237. case 1:
  238. idct = h.h264_idct_add16intra;
  239. name = "h264_idct_add16intra";
  240. intra = 1;
  241. break;
  242. case 2:
  243. idct = h.h264_idct8_add4;
  244. name = "h264_idct8_add4";
  245. sz = 8;
  246. break;
  247. }
  248. memset(nnzc, 0, 15 * 8);
  249. memset(coef_full, 0, 16 * 16 * SIZEOF_COEF);
  250. for (i = 0; i < 16 * 16; i += sz * sz) {
  251. uint8_t src[8 * 8 * 2];
  252. uint8_t dst[8 * 8 * 2];
  253. int16_t coef[8 * 8 * 2];
  254. int index = i / sz;
  255. int block_y = (index / 16) * sz;
  256. int block_x = index % 16;
  257. int offset = (block_y * 16 + block_x) * SIZEOF_PIXEL;
  258. int nnz = rnd() % 3;
  259. randomize_buffers();
  260. if (sz == 4)
  261. dct4x4(coef, bit_depth);
  262. else
  263. dct8x8(coef, bit_depth);
  264. for (y = 0; y < sz; y++)
  265. memcpy(&dst_full[offset + y * 16 * SIZEOF_PIXEL],
  266. &dst[PIXEL_STRIDE * y], sz * SIZEOF_PIXEL);
  267. if (nnz > 1)
  268. nnz = sz * sz;
  269. memcpy(&coef_full[i * SIZEOF_COEF/sizeof(coef[0])],
  270. coef, nnz * SIZEOF_COEF);
  271. if (intra && nnz == 1)
  272. nnz = 0;
  273. nnzc[scan8[i / 16]] = nnz;
  274. block_offset[i / 16] = offset;
  275. }
  276. if (check_func(idct, "%s_%dbpp", name, bit_depth)) {
  277. memcpy(coef0, coef_full, 16 * 16 * SIZEOF_COEF);
  278. memcpy(coef1, coef_full, 16 * 16 * SIZEOF_COEF);
  279. memcpy(dst0, dst_full, 16 * 16 * SIZEOF_PIXEL);
  280. memcpy(dst1, dst_full, 16 * 16 * SIZEOF_PIXEL);
  281. call_ref(dst0, block_offset, coef0, 16 * SIZEOF_PIXEL, nnzc);
  282. call_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
  283. if (memcmp(dst0, dst1, 16 * 16 * SIZEOF_PIXEL) ||
  284. memcmp(coef0, coef1, 16 * 16 * SIZEOF_COEF))
  285. fail();
  286. bench_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
  287. }
  288. }
  289. }
  290. }
  291. static void check_loop_filter(void)
  292. {
  293. LOCAL_ALIGNED_16(uint8_t, dst, [32 * 16 * 2]);
  294. LOCAL_ALIGNED_16(uint8_t, dst0, [32 * 16 * 2]);
  295. LOCAL_ALIGNED_16(uint8_t, dst1, [32 * 16 * 2]);
  296. H264DSPContext h;
  297. int bit_depth;
  298. int alphas[36], betas[36];
  299. int8_t tc0[36][4];
  300. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *pix, ptrdiff_t stride,
  301. int alpha, int beta, int8_t *tc0);
  302. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  303. int i, j, a, c;
  304. uint32_t mask = pixel_mask_lf[bit_depth - 8];
  305. ff_h264dsp_init(&h, bit_depth, 1);
  306. for (i = 35, a = 255, c = 250; i >= 0; i--) {
  307. alphas[i] = a << (bit_depth - 8);
  308. betas[i] = (i + 1) / 2 << (bit_depth - 8);
  309. tc0[i][0] = tc0[i][3] = (c + 6) / 10;
  310. tc0[i][1] = (c + 7) / 15;
  311. tc0[i][2] = (c + 9) / 20;
  312. a = a*9/10;
  313. c = c*9/10;
  314. }
  315. #define CHECK_LOOP_FILTER(name, align, idc) \
  316. do { \
  317. if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \
  318. for (j = 0; j < 36; j++) { \
  319. intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \
  320. for (i = 0; i < 1024; i+=4) { \
  321. AV_WN32A(dst + i, rnd() & mask); \
  322. } \
  323. memcpy(dst0, dst, 32 * 16 * 2); \
  324. memcpy(dst1, dst, 32 * 16 * 2); \
  325. \
  326. call_ref(dst0 + off, 32, alphas[j], betas[j], tc0[j]); \
  327. call_new(dst1 + off, 32, alphas[j], betas[j], tc0[j]); \
  328. if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \
  329. fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d " \
  330. "tc0:{%d,%d,%d,%d}\n", j, alphas[j], betas[j], \
  331. tc0[j][0], tc0[j][1], tc0[j][2], tc0[j][3]); \
  332. fail(); \
  333. } \
  334. bench_new(dst1, 32, alphas[j], betas[j], tc0[j]); \
  335. } \
  336. } \
  337. } while (0)
  338. CHECK_LOOP_FILTER(h264_v_loop_filter_luma, 1,);
  339. CHECK_LOOP_FILTER(h264_h_loop_filter_luma, 0,);
  340. CHECK_LOOP_FILTER(h264_h_loop_filter_luma_mbaff, 0,);
  341. CHECK_LOOP_FILTER(h264_v_loop_filter_chroma, 1,);
  342. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma, 0,);
  343. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff, 0,);
  344. ff_h264dsp_init(&h, bit_depth, 2);
  345. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma, 0, 422);
  346. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff, 0, 422);
  347. #undef CHECK_LOOP_FILTER
  348. }
  349. }
  350. static void check_loop_filter_intra(void)
  351. {
  352. LOCAL_ALIGNED_16(uint8_t, dst, [32 * 16 * 2]);
  353. LOCAL_ALIGNED_16(uint8_t, dst0, [32 * 16 * 2]);
  354. LOCAL_ALIGNED_16(uint8_t, dst1, [32 * 16 * 2]);
  355. H264DSPContext h;
  356. int bit_depth;
  357. int alphas[36], betas[36];
  358. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *pix, ptrdiff_t stride,
  359. int alpha, int beta);
  360. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  361. int i, j, a;
  362. uint32_t mask = pixel_mask_lf[bit_depth - 8];
  363. ff_h264dsp_init(&h, bit_depth, 1);
  364. for (i = 35, a = 255; i >= 0; i--) {
  365. alphas[i] = a << (bit_depth - 8);
  366. betas[i] = (i + 1) / 2 << (bit_depth - 8);
  367. a = a*9/10;
  368. }
  369. #define CHECK_LOOP_FILTER(name, align, idc) \
  370. do { \
  371. if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \
  372. for (j = 0; j < 36; j++) { \
  373. intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \
  374. for (i = 0; i < 1024; i+=4) { \
  375. AV_WN32A(dst + i, rnd() & mask); \
  376. } \
  377. memcpy(dst0, dst, 32 * 16 * 2); \
  378. memcpy(dst1, dst, 32 * 16 * 2); \
  379. \
  380. call_ref(dst0 + off, 32, alphas[j], betas[j]); \
  381. call_new(dst1 + off, 32, alphas[j], betas[j]); \
  382. if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \
  383. fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d\n", \
  384. j, alphas[j], betas[j]); \
  385. fail(); \
  386. } \
  387. bench_new(dst1, 32, alphas[j], betas[j]); \
  388. } \
  389. } \
  390. } while (0)
  391. CHECK_LOOP_FILTER(h264_v_loop_filter_luma_intra, 1,);
  392. CHECK_LOOP_FILTER(h264_h_loop_filter_luma_intra, 0,);
  393. CHECK_LOOP_FILTER(h264_h_loop_filter_luma_mbaff_intra, 0,);
  394. CHECK_LOOP_FILTER(h264_v_loop_filter_chroma_intra, 1,);
  395. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_intra, 0,);
  396. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff_intra, 0,);
  397. ff_h264dsp_init(&h, bit_depth, 2);
  398. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_intra, 0, 422);
  399. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff_intra, 0, 422);
  400. #undef CHECK_LOOP_FILTER
  401. }
  402. }
  403. void checkasm_check_h264dsp(void)
  404. {
  405. check_idct();
  406. check_idct_multiple();
  407. report("idct");
  408. check_loop_filter();
  409. report("loop_filter");
  410. check_loop_filter_intra();
  411. report("loop_filter_intra");
  412. }