h264dsp.c 22 KB

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