h264dsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/h264dsp.h"
  23. #include "libavcodec/h264data.h"
  24. #include "libavcodec/h264_parse.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mem_internal.h"
  28. static const uint32_t pixel_mask[5] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff, 0x0fff0fff, 0x3fff3fff };
  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(idx) \
  34. do { \
  35. int x, y; \
  36. uint32_t mask = pixel_mask[(idx)]; \
  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. const int64_t 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. static const int depths[5] = { 8, 9, 10, 12, 14 };
  164. LOCAL_ALIGNED_16(uint8_t, src, [8 * 8 * 2]);
  165. LOCAL_ALIGNED_16(uint8_t, dst, [8 * 8 * 2]);
  166. LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]);
  167. LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]);
  168. LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]);
  169. LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]);
  170. LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]);
  171. H264DSPContext h;
  172. int bit_depth, sz, align, dc, i;
  173. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride);
  174. for (i = 0; i < FF_ARRAY_ELEMS(depths); i++) {
  175. bit_depth = depths[i];
  176. ff_h264dsp_init(&h, bit_depth, 1);
  177. for (dc = 0; dc <= 2; dc++) {
  178. for (sz = 4; sz <= 8; sz += 4) {
  179. void (*idct)(uint8_t *, int16_t *, int) = NULL;
  180. const char fmts[3][28] = {
  181. "h264_idct%d_add_%dbpp", "h264_idct%d_dc_add_%dbpp",
  182. "h264_add_pixels%d_%dbpp",
  183. };
  184. randomize_buffers(i);
  185. if (sz == 4)
  186. dct4x4(coef, bit_depth);
  187. else
  188. dct8x8(coef, bit_depth);
  189. switch ((sz << 2) | dc) {
  190. case (4 << 2) | 0: idct = h.h264_idct_add; break;
  191. case (4 << 2) | 1: idct = h.h264_idct_dc_add; break;
  192. case (4 << 2) | 2: idct = h.h264_add_pixels4_clear; break;
  193. case (8 << 2) | 0: idct = h.h264_idct8_add; break;
  194. case (8 << 2) | 1: idct = h.h264_idct8_dc_add; break;
  195. case (8 << 2) | 2: idct = h.h264_add_pixels8_clear; break;
  196. }
  197. if (check_func(idct, fmts[dc], sz, bit_depth)) {
  198. for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) {
  199. uint8_t *dst1 = dst1_base + align;
  200. if (dc) {
  201. memset(subcoef0, 0, sz * sz * SIZEOF_COEF);
  202. memcpy(subcoef0, coef, SIZEOF_COEF);
  203. } else {
  204. memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
  205. }
  206. memcpy(dst0, dst, sz * PIXEL_STRIDE);
  207. memcpy(dst1, dst, sz * PIXEL_STRIDE);
  208. memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
  209. call_ref(dst0, subcoef0, PIXEL_STRIDE);
  210. call_new(dst1, subcoef1, PIXEL_STRIDE);
  211. if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) ||
  212. memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF))
  213. fail();
  214. bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. static void check_idct_multiple(void)
  222. {
  223. LOCAL_ALIGNED_16(uint8_t, dst_full, [16 * 16 * 2]);
  224. LOCAL_ALIGNED_16(int16_t, coef_full, [16 * 16 * 2]);
  225. LOCAL_ALIGNED_16(uint8_t, dst0, [16 * 16 * 2]);
  226. LOCAL_ALIGNED_16(uint8_t, dst1, [16 * 16 * 2]);
  227. LOCAL_ALIGNED_16(int16_t, coef0, [16 * 16 * 2]);
  228. LOCAL_ALIGNED_16(int16_t, coef1, [16 * 16 * 2]);
  229. LOCAL_ALIGNED_16(uint8_t, nnzc, [15 * 8]);
  230. H264DSPContext h;
  231. int bit_depth, i, y, func;
  232. 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]);
  233. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  234. ff_h264dsp_init(&h, bit_depth, 1);
  235. for (func = 0; func < 3; func++) {
  236. void (*idct)(uint8_t *, const int *, int16_t *, int, const uint8_t[]) = NULL;
  237. const char *name;
  238. int sz = 4, intra = 0;
  239. int block_offset[16] = { 0 };
  240. switch (func) {
  241. case 0:
  242. idct = h.h264_idct_add16;
  243. name = "h264_idct_add16";
  244. break;
  245. case 1:
  246. idct = h.h264_idct_add16intra;
  247. name = "h264_idct_add16intra";
  248. intra = 1;
  249. break;
  250. case 2:
  251. idct = h.h264_idct8_add4;
  252. name = "h264_idct8_add4";
  253. sz = 8;
  254. break;
  255. }
  256. memset(nnzc, 0, 15 * 8);
  257. memset(coef_full, 0, 16 * 16 * SIZEOF_COEF);
  258. for (i = 0; i < 16 * 16; i += sz * sz) {
  259. uint8_t src[8 * 8 * 2];
  260. uint8_t dst[8 * 8 * 2];
  261. int16_t coef[8 * 8 * 2];
  262. int index = i / sz;
  263. int block_y = (index / 16) * sz;
  264. int block_x = index % 16;
  265. int offset = (block_y * 16 + block_x) * SIZEOF_PIXEL;
  266. int nnz = rnd() % 3;
  267. randomize_buffers(bit_depth - 8);
  268. if (sz == 4)
  269. dct4x4(coef, bit_depth);
  270. else
  271. dct8x8(coef, bit_depth);
  272. for (y = 0; y < sz; y++)
  273. memcpy(&dst_full[offset + y * 16 * SIZEOF_PIXEL],
  274. &dst[PIXEL_STRIDE * y], sz * SIZEOF_PIXEL);
  275. if (nnz > 1)
  276. nnz = sz * sz;
  277. memcpy(&coef_full[i * SIZEOF_COEF/sizeof(coef[0])],
  278. coef, nnz * SIZEOF_COEF);
  279. if (intra && nnz == 1)
  280. nnz = 0;
  281. nnzc[scan8[i / 16]] = nnz;
  282. block_offset[i / 16] = offset;
  283. }
  284. if (check_func(idct, "%s_%dbpp", name, bit_depth)) {
  285. memcpy(coef0, coef_full, 16 * 16 * SIZEOF_COEF);
  286. memcpy(coef1, coef_full, 16 * 16 * SIZEOF_COEF);
  287. memcpy(dst0, dst_full, 16 * 16 * SIZEOF_PIXEL);
  288. memcpy(dst1, dst_full, 16 * 16 * SIZEOF_PIXEL);
  289. call_ref(dst0, block_offset, coef0, 16 * SIZEOF_PIXEL, nnzc);
  290. call_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
  291. if (memcmp(dst0, dst1, 16 * 16 * SIZEOF_PIXEL) ||
  292. memcmp(coef0, coef1, 16 * 16 * SIZEOF_COEF))
  293. fail();
  294. bench_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
  295. }
  296. }
  297. }
  298. }
  299. static void check_loop_filter(void)
  300. {
  301. LOCAL_ALIGNED_16(uint8_t, dst, [32 * 16 * 2]);
  302. LOCAL_ALIGNED_16(uint8_t, dst0, [32 * 16 * 2]);
  303. LOCAL_ALIGNED_16(uint8_t, dst1, [32 * 16 * 2]);
  304. H264DSPContext h;
  305. int bit_depth;
  306. int alphas[36], betas[36];
  307. int8_t tc0[36][4];
  308. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *pix, ptrdiff_t stride,
  309. int alpha, int beta, int8_t *tc0);
  310. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  311. int i, j, a, c;
  312. uint32_t mask = pixel_mask_lf[bit_depth - 8];
  313. ff_h264dsp_init(&h, bit_depth, 1);
  314. for (i = 35, a = 255, c = 250; i >= 0; i--) {
  315. alphas[i] = a << (bit_depth - 8);
  316. betas[i] = (i + 1) / 2 << (bit_depth - 8);
  317. tc0[i][0] = tc0[i][3] = (c + 6) / 10;
  318. tc0[i][1] = (c + 7) / 15;
  319. tc0[i][2] = (c + 9) / 20;
  320. a = a*9/10;
  321. c = c*9/10;
  322. }
  323. #define CHECK_LOOP_FILTER(name, align, idc) \
  324. do { \
  325. if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \
  326. for (j = 0; j < 36; j++) { \
  327. intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \
  328. for (i = 0; i < 1024; i+=4) { \
  329. AV_WN32A(dst + i, rnd() & mask); \
  330. } \
  331. memcpy(dst0, dst, 32 * 16 * 2); \
  332. memcpy(dst1, dst, 32 * 16 * 2); \
  333. \
  334. call_ref(dst0 + off, 32, alphas[j], betas[j], tc0[j]); \
  335. call_new(dst1 + off, 32, alphas[j], betas[j], tc0[j]); \
  336. if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \
  337. fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d " \
  338. "tc0:{%d,%d,%d,%d}\n", j, alphas[j], betas[j], \
  339. tc0[j][0], tc0[j][1], tc0[j][2], tc0[j][3]); \
  340. fail(); \
  341. } \
  342. bench_new(dst1 + off, 32, alphas[j], betas[j], tc0[j]);\
  343. } \
  344. } \
  345. } while (0)
  346. CHECK_LOOP_FILTER(h264_v_loop_filter_luma, 1,);
  347. CHECK_LOOP_FILTER(h264_h_loop_filter_luma, 0,);
  348. CHECK_LOOP_FILTER(h264_h_loop_filter_luma_mbaff, 0,);
  349. CHECK_LOOP_FILTER(h264_v_loop_filter_chroma, 1,);
  350. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma, 0,);
  351. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff, 0,);
  352. ff_h264dsp_init(&h, bit_depth, 2);
  353. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma, 0, 422);
  354. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff, 0, 422);
  355. #undef CHECK_LOOP_FILTER
  356. }
  357. }
  358. static void check_loop_filter_intra(void)
  359. {
  360. LOCAL_ALIGNED_16(uint8_t, dst, [32 * 16 * 2]);
  361. LOCAL_ALIGNED_16(uint8_t, dst0, [32 * 16 * 2]);
  362. LOCAL_ALIGNED_16(uint8_t, dst1, [32 * 16 * 2]);
  363. H264DSPContext h;
  364. int bit_depth;
  365. int alphas[36], betas[36];
  366. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *pix, ptrdiff_t stride,
  367. int alpha, int beta);
  368. for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
  369. int i, j, a;
  370. uint32_t mask = pixel_mask_lf[bit_depth - 8];
  371. ff_h264dsp_init(&h, bit_depth, 1);
  372. for (i = 35, a = 255; i >= 0; i--) {
  373. alphas[i] = a << (bit_depth - 8);
  374. betas[i] = (i + 1) / 2 << (bit_depth - 8);
  375. a = a*9/10;
  376. }
  377. #define CHECK_LOOP_FILTER(name, align, idc) \
  378. do { \
  379. if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \
  380. for (j = 0; j < 36; j++) { \
  381. intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \
  382. for (i = 0; i < 1024; i+=4) { \
  383. AV_WN32A(dst + i, rnd() & mask); \
  384. } \
  385. memcpy(dst0, dst, 32 * 16 * 2); \
  386. memcpy(dst1, dst, 32 * 16 * 2); \
  387. \
  388. call_ref(dst0 + off, 32, alphas[j], betas[j]); \
  389. call_new(dst1 + off, 32, alphas[j], betas[j]); \
  390. if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \
  391. fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d\n", \
  392. j, alphas[j], betas[j]); \
  393. fail(); \
  394. } \
  395. bench_new(dst1 + off, 32, alphas[j], betas[j]); \
  396. } \
  397. } \
  398. } while (0)
  399. CHECK_LOOP_FILTER(h264_v_loop_filter_luma_intra, 1,);
  400. CHECK_LOOP_FILTER(h264_h_loop_filter_luma_intra, 0,);
  401. CHECK_LOOP_FILTER(h264_h_loop_filter_luma_mbaff_intra, 0,);
  402. CHECK_LOOP_FILTER(h264_v_loop_filter_chroma_intra, 1,);
  403. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_intra, 0,);
  404. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff_intra, 0,);
  405. ff_h264dsp_init(&h, bit_depth, 2);
  406. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_intra, 0, 422);
  407. CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff_intra, 0, 422);
  408. #undef CHECK_LOOP_FILTER
  409. }
  410. }
  411. void checkasm_check_h264dsp(void)
  412. {
  413. check_idct();
  414. check_idct_multiple();
  415. report("idct");
  416. check_loop_filter();
  417. report("loop_filter");
  418. check_loop_filter_intra();
  419. report("loop_filter_intra");
  420. }