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.

557 lines
19KB

  1. /*
  2. * Copyright (c) 2015 Ronald S. Bultje <rsbultje@gmail.com>
  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 <math.h>
  21. #include <string.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavcodec/vp9.h"
  27. #include "libavcodec/vp9data.h"
  28. #include "checkasm.h"
  29. static const uint32_t pixel_mask[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
  30. #define BIT_DEPTH 8
  31. #define SIZEOF_PIXEL ((BIT_DEPTH + 7) / 8)
  32. #define randomize_buffers() \
  33. do { \
  34. uint32_t mask = pixel_mask[(BIT_DEPTH - 8) >> 1]; \
  35. for (y = 0; y < sz; y++) { \
  36. for (x = 0; x < sz * SIZEOF_PIXEL; x += 4) { \
  37. uint32_t r = rnd() & mask; \
  38. AV_WN32A(dst + y * sz * SIZEOF_PIXEL + x, r); \
  39. AV_WN32A(src + y * sz * SIZEOF_PIXEL + x, rnd() & mask); \
  40. } \
  41. for (x = 0; x < sz; x++) { \
  42. if (BIT_DEPTH == 8) { \
  43. coef[y * sz + x] = src[y * sz + x] - dst[y * sz + x]; \
  44. } else { \
  45. ((int32_t *) coef)[y * sz + x] = \
  46. ((uint16_t *) src)[y * sz + x] - \
  47. ((uint16_t *) dst)[y * sz + x]; \
  48. } \
  49. } \
  50. } \
  51. } while(0)
  52. // wht function copied from libvpx
  53. static void fwht_1d(double *out, const double *in, int sz)
  54. {
  55. double t0 = in[0] + in[1];
  56. double t3 = in[3] - in[2];
  57. double t4 = trunc((t0 - t3) * 0.5);
  58. double t1 = t4 - in[1];
  59. double t2 = t4 - in[2];
  60. out[0] = t0 - t2;
  61. out[1] = t2;
  62. out[2] = t3 + t1;
  63. out[3] = t1;
  64. }
  65. // standard DCT-II
  66. static void fdct_1d(double *out, const double *in, int sz)
  67. {
  68. int k, n;
  69. for (k = 0; k < sz; k++) {
  70. out[k] = 0.0;
  71. for (n = 0; n < sz; n++)
  72. out[k] += in[n] * cos(M_PI * (2 * n + 1) * k / (sz * 2.0));
  73. }
  74. out[0] *= M_SQRT1_2;
  75. }
  76. // see "Towards jointly optimal spatial prediction and adaptive transform in
  77. // video/image coding", by J. Han, A. Saxena, and K. Rose
  78. // IEEE Proc. ICASSP, pp. 726-729, Mar. 2010.
  79. static void fadst4_1d(double *out, const double *in, int sz)
  80. {
  81. int k, n;
  82. for (k = 0; k < sz; k++) {
  83. out[k] = 0.0;
  84. for (n = 0; n < sz; n++)
  85. out[k] += in[n] * sin(M_PI * (n + 1) * (2 * k + 1) / (sz * 2.0 + 1.0));
  86. }
  87. }
  88. // see "A Butterfly Structured Design of The Hybrid Transform Coding Scheme",
  89. // by Jingning Han, Yaowu Xu, and Debargha Mukherjee
  90. // http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/41418.pdf
  91. static void fadst_1d(double *out, const double *in, int sz)
  92. {
  93. int k, n;
  94. for (k = 0; k < sz; k++) {
  95. out[k] = 0.0;
  96. for (n = 0; n < sz; n++)
  97. out[k] += in[n] * sin(M_PI * (2 * n + 1) * (2 * k + 1) / (sz * 4.0));
  98. }
  99. }
  100. typedef void (*ftx1d_fn)(double *out, const double *in, int sz);
  101. static void ftx_2d(double *out, const double *in, enum TxfmMode tx,
  102. enum TxfmType txtp, int sz)
  103. {
  104. static const double scaling_factors[5][4] = {
  105. { 4.0, 16.0 * M_SQRT1_2 / 3.0, 16.0 * M_SQRT1_2 / 3.0, 32.0 / 9.0 },
  106. { 2.0, 2.0, 2.0, 2.0 },
  107. { 1.0, 1.0, 1.0, 1.0 },
  108. { 0.25 },
  109. { 4.0 }
  110. };
  111. static const ftx1d_fn ftx1d_tbl[5][4][2] = {
  112. {
  113. { fdct_1d, fdct_1d },
  114. { fadst4_1d, fdct_1d },
  115. { fdct_1d, fadst4_1d },
  116. { fadst4_1d, fadst4_1d },
  117. }, {
  118. { fdct_1d, fdct_1d },
  119. { fadst_1d, fdct_1d },
  120. { fdct_1d, fadst_1d },
  121. { fadst_1d, fadst_1d },
  122. }, {
  123. { fdct_1d, fdct_1d },
  124. { fadst_1d, fdct_1d },
  125. { fdct_1d, fadst_1d },
  126. { fadst_1d, fadst_1d },
  127. }, {
  128. { fdct_1d, fdct_1d },
  129. }, {
  130. { fwht_1d, fwht_1d },
  131. },
  132. };
  133. double temp[1024];
  134. double scaling_factor = scaling_factors[tx][txtp];
  135. int i, j;
  136. // cols
  137. for (i = 0; i < sz; ++i) {
  138. double temp_out[32];
  139. ftx1d_tbl[tx][txtp][0](temp_out, &in[i * sz], sz);
  140. // scale and transpose
  141. for (j = 0; j < sz; ++j)
  142. temp[j * sz + i] = temp_out[j] * scaling_factor;
  143. }
  144. // rows
  145. for (i = 0; i < sz; i++)
  146. ftx1d_tbl[tx][txtp][1](&out[i * sz], &temp[i * sz], sz);
  147. }
  148. static void ftx(int16_t *buf, enum TxfmMode tx,
  149. enum TxfmType txtp, int sz, int bit_depth)
  150. {
  151. double ind[1024], outd[1024];
  152. int n;
  153. emms_c();
  154. for (n = 0; n < sz * sz; n++) {
  155. if (bit_depth == 8)
  156. ind[n] = buf[n];
  157. else
  158. ind[n] = ((int32_t *) buf)[n];
  159. }
  160. ftx_2d(outd, ind, tx, txtp, sz);
  161. for (n = 0; n < sz * sz; n++) {
  162. if (bit_depth == 8)
  163. buf[n] = lrint(outd[n]);
  164. else
  165. ((int32_t *) buf)[n] = lrint(outd[n]);
  166. }
  167. }
  168. static int copy_subcoefs(int16_t *out, const int16_t *in, enum TxfmMode tx,
  169. enum TxfmType txtp, int sz, int sub, int bit_depth)
  170. {
  171. // copy the topleft coefficients such that the return value (being the
  172. // coefficient scantable index for the eob token) guarantees that only
  173. // the topleft $sub out of $sz (where $sz >= $sub) coefficients in both
  174. // dimensions are non-zero. This leads to braching to specific optimized
  175. // simd versions (e.g. dc-only) so that we get full asm coverage in this
  176. // test
  177. int n;
  178. const int16_t *scan = ff_vp9_scans[tx][txtp];
  179. int eob;
  180. for (n = 0; n < sz * sz; n++) {
  181. int rc = scan[n], rcx = rc % sz, rcy = rc / sz;
  182. // find eob for this sub-idct
  183. if (rcx >= sub || rcy >= sub)
  184. break;
  185. // copy coef
  186. if (bit_depth == 8) {
  187. out[rc] = in[rc];
  188. } else {
  189. AV_COPY32(&out[rc * 2], &in[rc * 2]);
  190. }
  191. }
  192. eob = n;
  193. for (; n < sz * sz; n++) {
  194. int rc = scan[n];
  195. // zero
  196. if (bit_depth == 8) {
  197. out[rc] = 0;
  198. } else {
  199. AV_ZERO32(&out[rc * 2]);
  200. }
  201. }
  202. return eob;
  203. }
  204. static int iszero(const int16_t *c, int sz)
  205. {
  206. int n;
  207. for (n = 0; n < sz / sizeof(int16_t); n += 2)
  208. if (AV_RN32A(&c[n]))
  209. return 0;
  210. return 1;
  211. }
  212. #define SIZEOF_COEF (2 * ((BIT_DEPTH + 7) / 8))
  213. static void check_itxfm(void)
  214. {
  215. LOCAL_ALIGNED_32(uint8_t, src, [32 * 32 * 2]);
  216. LOCAL_ALIGNED(32, uint8_t, dst, [32 * 32 * 2]);
  217. LOCAL_ALIGNED(32, uint8_t, dst0, [32 * 32 * 2]);
  218. LOCAL_ALIGNED(32, uint8_t, dst1, [32 * 32 * 2]);
  219. LOCAL_ALIGNED(32, int16_t, coef, [32 * 32 * 2]);
  220. LOCAL_ALIGNED(32, int16_t, subcoef0, [32 * 32 * 2]);
  221. LOCAL_ALIGNED(32, int16_t, subcoef1, [32 * 32 * 2]);
  222. declare_func(void, uint8_t *dst, ptrdiff_t stride, int16_t *block, int eob);
  223. VP9DSPContext dsp;
  224. int y, x, tx, txtp, sub;
  225. static const char *const txtp_types[N_TXFM_TYPES] = {
  226. [DCT_DCT] = "dct_dct", [DCT_ADST] = "adst_dct",
  227. [ADST_DCT] = "dct_adst", [ADST_ADST] = "adst_adst"
  228. };
  229. ff_vp9dsp_init(&dsp);
  230. for (tx = TX_4X4; tx <= N_TXFM_SIZES /* 4 = lossless */; tx++) {
  231. int sz = 4 << (tx & 3);
  232. int n_txtps = tx < TX_32X32 ? N_TXFM_TYPES : 1;
  233. for (txtp = 0; txtp < n_txtps; txtp++) {
  234. if (check_func(dsp.itxfm_add[tx][txtp], "vp9_inv_%s_%dx%d_add",
  235. tx == 4 ? "wht_wht" : txtp_types[txtp], sz, sz)) {
  236. randomize_buffers();
  237. ftx(coef, tx, txtp, sz, BIT_DEPTH);
  238. for (sub = (txtp == 0) ? 1 : 2; sub <= sz; sub <<= 1) {
  239. int eob;
  240. if (sub < sz) {
  241. eob = copy_subcoefs(subcoef0, coef, tx, txtp,
  242. sz, sub, BIT_DEPTH);
  243. } else {
  244. eob = sz * sz;
  245. memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
  246. }
  247. memcpy(dst0, dst, sz * sz * SIZEOF_PIXEL);
  248. memcpy(dst1, dst, sz * sz * SIZEOF_PIXEL);
  249. memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
  250. call_ref(dst0, sz * SIZEOF_PIXEL, subcoef0, eob);
  251. call_new(dst1, sz * SIZEOF_PIXEL, subcoef1, eob);
  252. if (memcmp(dst0, dst1, sz * sz * SIZEOF_PIXEL) ||
  253. !iszero(subcoef0, sz * sz * SIZEOF_COEF) ||
  254. !iszero(subcoef1, sz * sz * SIZEOF_COEF))
  255. fail();
  256. }
  257. bench_new(dst, sz * SIZEOF_PIXEL, coef, sz * sz);
  258. }
  259. }
  260. }
  261. report("itxfm");
  262. }
  263. #undef randomize_buffers
  264. #define setpx(a,b,c) \
  265. do { \
  266. if (SIZEOF_PIXEL == 1) { \
  267. buf0[(a) + (b) * jstride] = av_clip_uint8(c); \
  268. } else { \
  269. ((uint16_t *)buf0)[(a) + (b) * jstride] = av_clip_uintp2(c, BIT_DEPTH); \
  270. } \
  271. } while (0)
  272. #define setdx(a,b,c,d) setpx(a,b,c-(d)+(rnd()%((d)*2+1)))
  273. #define setsx(a,b,c,d) setdx(a,b,c,(d) << (BIT_DEPTH - 8))
  274. static void randomize_loopfilter_buffers(int bidx, int lineoff, int str,
  275. int bit_depth, int dir,
  276. const int *E, const int *F,
  277. const int *H, const int *I,
  278. uint8_t *buf0, uint8_t *buf1)
  279. {
  280. uint32_t mask = (1 << BIT_DEPTH) - 1;
  281. int off = dir ? lineoff : lineoff * 16;
  282. int istride = dir ? 1 : 16;
  283. int jstride = dir ? str : 1;
  284. int i, j;
  285. for (i = 0; i < 2; i++) /* flat16 */ {
  286. int idx = off + i * istride, p0, q0;
  287. setpx(idx, 0, q0 = rnd() & mask);
  288. setsx(idx, -1, p0 = q0, E[bidx] >> 2);
  289. for (j = 1; j < 8; j++) {
  290. setsx(idx, -1 - j, p0, F[bidx]);
  291. setsx(idx, j, q0, F[bidx]);
  292. }
  293. }
  294. for (i = 2; i < 4; i++) /* flat8 */ {
  295. int idx = off + i * istride, p0, q0;
  296. setpx(idx, 0, q0 = rnd() & mask);
  297. setsx(idx, -1, p0 = q0, E[bidx] >> 2);
  298. for (j = 1; j < 4; j++) {
  299. setsx(idx, -1 - j, p0, F[bidx]);
  300. setsx(idx, j, q0, F[bidx]);
  301. }
  302. for (j = 4; j < 8; j++) {
  303. setpx(idx, -1 - j, rnd() & mask);
  304. setpx(idx, j, rnd() & mask);
  305. }
  306. }
  307. for (i = 4; i < 6; i++) /* regular */ {
  308. int idx = off + i * istride, p2, p1, p0, q0, q1, q2;
  309. setpx(idx, 0, q0 = rnd() & mask);
  310. setsx(idx, 1, q1 = q0, I[bidx]);
  311. setsx(idx, 2, q2 = q1, I[bidx]);
  312. setsx(idx, 3, q2, I[bidx]);
  313. setsx(idx, -1, p0 = q0, E[bidx] >> 2);
  314. setsx(idx, -2, p1 = p0, I[bidx]);
  315. setsx(idx, -3, p2 = p1, I[bidx]);
  316. setsx(idx, -4, p2, I[bidx]);
  317. for (j = 4; j < 8; j++) {
  318. setpx(idx, -1 - j, rnd() & mask);
  319. setpx(idx, j, rnd() & mask);
  320. }
  321. }
  322. for (i = 6; i < 8; i++) /* off */ {
  323. int idx = off + i * istride;
  324. for (j = 0; j < 8; j++) {
  325. setpx(idx, -1 - j, rnd() & mask);
  326. setpx(idx, j, rnd() & mask);
  327. }
  328. }
  329. }
  330. #define randomize_buffers(bidx, lineoff, str) \
  331. randomize_loopfilter_buffers(bidx, lineoff, str, BIT_DEPTH, dir, \
  332. E, F, H, I, buf0, buf1)
  333. static void check_loopfilter(void)
  334. {
  335. LOCAL_ALIGNED_32(uint8_t, base0, [32 + 16 * 16 * 2]);
  336. LOCAL_ALIGNED_32(uint8_t, base1, [32 + 16 * 16 * 2]);
  337. VP9DSPContext dsp;
  338. int dir, wd, wd2;
  339. static const char *const dir_name[2] = { "h", "v" };
  340. static const int E[2] = { 20, 28 }, I[2] = { 10, 16 };
  341. static const int H[2] = { 7, 11 }, F[2] = { 1, 1 };
  342. declare_func(void, uint8_t *dst, ptrdiff_t stride, int E, int I, int H);
  343. ff_vp9dsp_init(&dsp);
  344. for (dir = 0; dir < 2; dir++) {
  345. uint8_t *buf0, *buf1;
  346. int midoff = (dir ? 8 * 8 : 8) * SIZEOF_PIXEL;
  347. int midoff_aligned = (dir ? 8 * 8 : 16) * SIZEOF_PIXEL;
  348. buf0 = base0 + midoff_aligned;
  349. buf1 = base1 + midoff_aligned;
  350. for (wd = 0; wd < 3; wd++) {
  351. // 4/8/16wd_8px
  352. if (check_func(dsp.loop_filter_8[wd][dir],
  353. "vp9_loop_filter_%s_%d_8",
  354. dir_name[dir], 4 << wd)) {
  355. randomize_buffers(0, 0, 8);
  356. memcpy(buf1 - midoff, buf0 - midoff,
  357. 16 * 8 * SIZEOF_PIXEL);
  358. call_ref(buf0, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
  359. call_new(buf1, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
  360. if (memcmp(buf0 - midoff, buf1 - midoff, 16 * 8 * SIZEOF_PIXEL))
  361. fail();
  362. bench_new(buf1, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
  363. }
  364. }
  365. midoff = (dir ? 16 * 8 : 8) * SIZEOF_PIXEL;
  366. midoff_aligned = (dir ? 16 * 8 : 16) * SIZEOF_PIXEL;
  367. buf0 = base0 + midoff_aligned;
  368. buf1 = base1 + midoff_aligned;
  369. // 16wd_16px loopfilter
  370. if (check_func(dsp.loop_filter_16[dir],
  371. "vp9_loop_filter_%s_16_16",
  372. dir_name[dir])) {
  373. randomize_buffers(0, 0, 16);
  374. randomize_buffers(0, 8, 16);
  375. memcpy(buf1 - midoff, buf0 - midoff, 16 * 16 * SIZEOF_PIXEL);
  376. call_ref(buf0, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
  377. call_new(buf1, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
  378. if (memcmp(buf0 - midoff, buf1 - midoff, 16 * 16 * SIZEOF_PIXEL))
  379. fail();
  380. bench_new(buf1, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
  381. }
  382. for (wd = 0; wd < 2; wd++) {
  383. for (wd2 = 0; wd2 < 2; wd2++) {
  384. // mix2 loopfilter
  385. if (check_func(dsp.loop_filter_mix2[wd][wd2][dir],
  386. "vp9_loop_filter_mix2_%s_%d%d_16",
  387. dir_name[dir], 4 << wd, 4 << wd2)) {
  388. randomize_buffers(0, 0, 16);
  389. randomize_buffers(1, 8, 16);
  390. memcpy(buf1 - midoff, buf0 - midoff, 16 * 16 * SIZEOF_PIXEL);
  391. #define M(a) ((a[1] << 8) | a[0])
  392. call_ref(buf0, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
  393. call_new(buf1, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
  394. if (memcmp(buf0 - midoff, buf1 - midoff, 16 * 16 * SIZEOF_PIXEL))
  395. fail();
  396. bench_new(buf1, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
  397. #undef M
  398. }
  399. }
  400. }
  401. }
  402. report("loopfilter");
  403. }
  404. #undef setsx
  405. #undef setpx
  406. #undef setdx
  407. #undef randomize_buffers
  408. #define DST_BUF_SIZE (size * size * SIZEOF_PIXEL)
  409. #define SRC_BUF_STRIDE 72
  410. #define SRC_BUF_SIZE ((size + 7) * SRC_BUF_STRIDE * SIZEOF_PIXEL)
  411. #define src (buf + 3 * SIZEOF_PIXEL * (SRC_BUF_STRIDE + 1))
  412. #define randomize_buffers() \
  413. do { \
  414. uint32_t mask = pixel_mask[(BIT_DEPTH - 8) >> 1]; \
  415. int k; \
  416. for (k = 0; k < SRC_BUF_SIZE; k += 4) { \
  417. uint32_t r = rnd() & mask; \
  418. AV_WN32A(buf + k, r); \
  419. } \
  420. if (op == 1) { \
  421. for (k = 0; k < DST_BUF_SIZE; k += 4) { \
  422. uint32_t r = rnd() & mask; \
  423. AV_WN32A(dst0 + k, r); \
  424. AV_WN32A(dst1 + k, r); \
  425. } \
  426. } \
  427. } while (0)
  428. static void check_mc(void)
  429. {
  430. static const char *const filter_names[4] = {
  431. "8tap_smooth", "8tap_regular", "8tap_sharp", "bilin"
  432. };
  433. static const char *const subpel_names[2][2] = { { "", "h" }, { "v", "hv" } };
  434. static const char *const op_names[2] = { "put", "avg" };
  435. LOCAL_ALIGNED_32(uint8_t, buf, [72 * 72 * 2]);
  436. LOCAL_ALIGNED_32(uint8_t, dst0, [64 * 64 * 2]);
  437. LOCAL_ALIGNED_32(uint8_t, dst1, [64 * 64 * 2]);
  438. char str[256];
  439. VP9DSPContext dsp;
  440. int op, hsize, filter, dx, dy;
  441. declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT,
  442. void, uint8_t *dst, ptrdiff_t dst_stride,
  443. const uint8_t *ref, ptrdiff_t ref_stride,
  444. int h, int mx, int my);
  445. for (op = 0; op < 2; op++) {
  446. ff_vp9dsp_init(&dsp);
  447. for (hsize = 0; hsize < 5; hsize++) {
  448. int size = 64 >> hsize;
  449. for (filter = 0; filter < 4; filter++) {
  450. for (dx = 0; dx < 2; dx++) {
  451. for (dy = 0; dy < 2; dy++) {
  452. if (dx || dy) {
  453. snprintf(str, sizeof(str), "%s_%s_%d%s", op_names[op],
  454. filter_names[filter], size,
  455. subpel_names[dy][dx]);
  456. } else {
  457. snprintf(str, sizeof(str), "%s%d", op_names[op], size);
  458. }
  459. if (check_func(dsp.mc[hsize][filter][op][dx][dy],
  460. "vp9_%s", str)) {
  461. int mx = dx ? 1 + (rnd() % 14) : 0;
  462. int my = dy ? 1 + (rnd() % 14) : 0;
  463. randomize_buffers();
  464. call_ref(dst0, size * SIZEOF_PIXEL,
  465. src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
  466. size, mx, my);
  467. call_new(dst1, size * SIZEOF_PIXEL,
  468. src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
  469. size, mx, my);
  470. if (memcmp(dst0, dst1, DST_BUF_SIZE))
  471. fail();
  472. // SIMD implementations for each filter of subpel
  473. // functions are identical
  474. if (filter >= 1 && filter <= 2) continue;
  475. bench_new(dst1, size * SIZEOF_PIXEL,
  476. src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
  477. size, mx, my);
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. report("mc");
  485. }
  486. void checkasm_check_vp9dsp(void)
  487. {
  488. check_itxfm();
  489. check_loopfilter();
  490. check_mc();
  491. }