2 * Copyright (c) 2015 Ronald S. Bultje <rsbultje@gmail.com>
4 * This file is part of Libav.
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.
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.
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.
24 #include "libavutil/common.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mathematics.h"
29 #include "libavcodec/vp9.h"
30 #include "libavcodec/vp9data.h"
34 static const uint32_t pixel_mask[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
37 #define SIZEOF_PIXEL ((BIT_DEPTH + 7) / 8)
39 #define randomize_buffers() \
41 uint32_t mask = pixel_mask[(BIT_DEPTH - 8) >> 1]; \
42 for (y = 0; y < sz; y++) { \
43 for (x = 0; x < sz * SIZEOF_PIXEL; x += 4) { \
44 uint32_t r = rnd() & mask; \
45 AV_WN32A(dst + y * sz * SIZEOF_PIXEL + x, r); \
46 AV_WN32A(src + y * sz * SIZEOF_PIXEL + x, rnd() & mask); \
48 for (x = 0; x < sz; x++) { \
49 if (BIT_DEPTH == 8) { \
50 coef[y * sz + x] = src[y * sz + x] - dst[y * sz + x]; \
52 ((int32_t *) coef)[y * sz + x] = \
53 ((uint16_t *) src)[y * sz + x] - \
54 ((uint16_t *) dst)[y * sz + x]; \
60 // wht function copied from libvpx
61 static void fwht_1d(double *out, const double *in, int sz)
63 double t0 = in[0] + in[1];
64 double t3 = in[3] - in[2];
65 double t4 = trunc((t0 - t3) * 0.5);
66 double t1 = t4 - in[1];
67 double t2 = t4 - in[2];
76 static void fdct_1d(double *out, const double *in, int sz)
80 for (k = 0; k < sz; k++) {
82 for (n = 0; n < sz; n++)
83 out[k] += in[n] * cos(M_PI * (2 * n + 1) * k / (sz * 2.0));
88 // see "Towards jointly optimal spatial prediction and adaptive transform in
89 // video/image coding", by J. Han, A. Saxena, and K. Rose
90 // IEEE Proc. ICASSP, pp. 726-729, Mar. 2010.
91 static void fadst4_1d(double *out, const double *in, int sz)
95 for (k = 0; k < sz; k++) {
97 for (n = 0; n < sz; n++)
98 out[k] += in[n] * sin(M_PI * (n + 1) * (2 * k + 1) / (sz * 2.0 + 1.0));
102 // see "A Butterfly Structured Design of The Hybrid Transform Coding Scheme",
103 // by Jingning Han, Yaowu Xu, and Debargha Mukherjee
104 // http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/41418.pdf
105 static void fadst_1d(double *out, const double *in, int sz)
109 for (k = 0; k < sz; k++) {
111 for (n = 0; n < sz; n++)
112 out[k] += in[n] * sin(M_PI * (2 * n + 1) * (2 * k + 1) / (sz * 4.0));
116 typedef void (*ftx1d_fn)(double *out, const double *in, int sz);
117 static void ftx_2d(double *out, const double *in, enum TxfmMode tx,
118 enum TxfmType txtp, int sz)
120 static const double scaling_factors[5][4] = {
121 { 4.0, 16.0 * M_SQRT1_2 / 3.0, 16.0 * M_SQRT1_2 / 3.0, 32.0 / 9.0 },
122 { 2.0, 2.0, 2.0, 2.0 },
123 { 1.0, 1.0, 1.0, 1.0 },
127 static const ftx1d_fn ftx1d_tbl[5][4][2] = {
129 { fdct_1d, fdct_1d },
130 { fadst4_1d, fdct_1d },
131 { fdct_1d, fadst4_1d },
132 { fadst4_1d, fadst4_1d },
134 { fdct_1d, fdct_1d },
135 { fadst_1d, fdct_1d },
136 { fdct_1d, fadst_1d },
137 { fadst_1d, fadst_1d },
139 { fdct_1d, fdct_1d },
140 { fadst_1d, fdct_1d },
141 { fdct_1d, fadst_1d },
142 { fadst_1d, fadst_1d },
144 { fdct_1d, fdct_1d },
146 { fwht_1d, fwht_1d },
150 double scaling_factor = scaling_factors[tx][txtp];
154 for (i = 0; i < sz; ++i) {
157 ftx1d_tbl[tx][txtp][0](temp_out, &in[i * sz], sz);
158 // scale and transpose
159 for (j = 0; j < sz; ++j)
160 temp[j * sz + i] = temp_out[j] * scaling_factor;
164 for (i = 0; i < sz; i++)
165 ftx1d_tbl[tx][txtp][1](&out[i * sz], &temp[i * sz], sz);
168 static void ftx(int16_t *buf, enum TxfmMode tx,
169 enum TxfmType txtp, int sz, int bit_depth)
171 double ind[1024], outd[1024];
175 for (n = 0; n < sz * sz; n++) {
179 ind[n] = ((int32_t *) buf)[n];
181 ftx_2d(outd, ind, tx, txtp, sz);
182 for (n = 0; n < sz * sz; n++) {
184 buf[n] = lrint(outd[n]);
186 ((int32_t *) buf)[n] = lrint(outd[n]);
190 static int copy_subcoefs(int16_t *out, const int16_t *in, enum TxfmMode tx,
191 enum TxfmType txtp, int sz, int sub, int bit_depth)
193 // copy the topleft coefficients such that the return value (being the
194 // coefficient scantable index for the eob token) guarantees that only
195 // the topleft $sub out of $sz (where $sz >= $sub) coefficients in both
196 // dimensions are non-zero. This leads to braching to specific optimized
197 // simd versions (e.g. dc-only) so that we get full asm coverage in this
201 const int16_t *scan = ff_vp9_scans[tx][txtp];
204 for (n = 0; n < sz * sz; n++) {
205 int rc = scan[n], rcx = rc % sz, rcy = rc / sz;
207 // find eob for this sub-idct
208 if (rcx >= sub || rcy >= sub)
212 if (bit_depth == 8) {
215 AV_COPY32(&out[rc * 2], &in[rc * 2]);
221 for (; n < sz * sz; n++) {
225 if (bit_depth == 8) {
228 AV_ZERO32(&out[rc * 2]);
235 static int iszero(const int16_t *c, int sz)
239 for (n = 0; n < sz / sizeof(int16_t); n += 2)
246 #define SIZEOF_COEF (2 * ((BIT_DEPTH + 7) / 8))
248 static void check_itxfm(void)
250 LOCAL_ALIGNED_32(uint8_t, src, [32 * 32 * 2]);
251 LOCAL_ALIGNED(32, uint8_t, dst, [32 * 32 * 2]);
252 LOCAL_ALIGNED(32, uint8_t, dst0, [32 * 32 * 2]);
253 LOCAL_ALIGNED(32, uint8_t, dst1, [32 * 32 * 2]);
254 LOCAL_ALIGNED(32, int16_t, coef, [32 * 32 * 2]);
255 LOCAL_ALIGNED(32, int16_t, subcoef0, [32 * 32 * 2]);
256 LOCAL_ALIGNED(32, int16_t, subcoef1, [32 * 32 * 2]);
257 declare_func(void, uint8_t *dst, ptrdiff_t stride, int16_t *block, int eob);
259 int y, x, tx, txtp, sub;
260 static const char *const txtp_types[N_TXFM_TYPES] = {
261 [DCT_DCT] = "dct_dct", [DCT_ADST] = "adst_dct",
262 [ADST_DCT] = "dct_adst", [ADST_ADST] = "adst_adst"
265 ff_vp9dsp_init(&dsp);
267 for (tx = TX_4X4; tx <= N_TXFM_SIZES /* 4 = lossless */; tx++) {
268 int sz = 4 << (tx & 3);
269 int n_txtps = tx < TX_32X32 ? N_TXFM_TYPES : 1;
271 for (txtp = 0; txtp < n_txtps; txtp++) {
272 if (check_func(dsp.itxfm_add[tx][txtp], "vp9_inv_%s_%dx%d_add",
273 tx == 4 ? "wht_wht" : txtp_types[txtp], sz, sz)) {
275 ftx(coef, tx, txtp, sz, BIT_DEPTH);
277 for (sub = (txtp == 0) ? 1 : 2; sub <= sz; sub <<= 1) {
281 eob = copy_subcoefs(subcoef0, coef, tx, txtp,
285 memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
288 memcpy(dst0, dst, sz * sz * SIZEOF_PIXEL);
289 memcpy(dst1, dst, sz * sz * SIZEOF_PIXEL);
290 memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
291 call_ref(dst0, sz * SIZEOF_PIXEL, subcoef0, eob);
292 call_new(dst1, sz * SIZEOF_PIXEL, subcoef1, eob);
293 if (memcmp(dst0, dst1, sz * sz * SIZEOF_PIXEL) ||
294 !iszero(subcoef0, sz * sz * SIZEOF_COEF) ||
295 !iszero(subcoef1, sz * sz * SIZEOF_COEF))
298 bench_new(dst, sz * SIZEOF_PIXEL, coef, sz * sz);
305 #undef randomize_buffers
307 #define setpx(a,b,c) \
309 if (SIZEOF_PIXEL == 1) { \
310 buf0[(a) + (b) * jstride] = av_clip_uint8(c); \
312 ((uint16_t *)buf0)[(a) + (b) * jstride] = av_clip_uintp2(c, BIT_DEPTH); \
315 #define setdx(a,b,c,d) setpx(a,b,c-(d)+(rnd()%((d)*2+1)))
316 #define setsx(a,b,c,d) setdx(a,b,c,(d) << (BIT_DEPTH - 8))
318 static void randomize_loopfilter_buffers(int bidx, int lineoff, int str,
319 int bit_depth, int dir,
320 const int *E, const int *F,
321 const int *H, const int *I,
322 uint8_t *buf0, uint8_t *buf1)
324 uint32_t mask = (1 << BIT_DEPTH) - 1;
325 int off = dir ? lineoff : lineoff * 16;
326 int istride = dir ? 1 : 16;
327 int jstride = dir ? str : 1;
329 for (i = 0; i < 2; i++) /* flat16 */ {
330 int idx = off + i * istride, p0, q0;
331 setpx(idx, 0, q0 = rnd() & mask);
332 setsx(idx, -1, p0 = q0, E[bidx] >> 2);
333 for (j = 1; j < 8; j++) {
334 setsx(idx, -1 - j, p0, F[bidx]);
335 setsx(idx, j, q0, F[bidx]);
338 for (i = 2; i < 4; i++) /* flat8 */ {
339 int idx = off + i * istride, p0, q0;
340 setpx(idx, 0, q0 = rnd() & mask);
341 setsx(idx, -1, p0 = q0, E[bidx] >> 2);
342 for (j = 1; j < 4; j++) {
343 setsx(idx, -1 - j, p0, F[bidx]);
344 setsx(idx, j, q0, F[bidx]);
346 for (j = 4; j < 8; j++) {
347 setpx(idx, -1 - j, rnd() & mask);
348 setpx(idx, j, rnd() & mask);
351 for (i = 4; i < 6; i++) /* regular */ {
352 int idx = off + i * istride, p2, p1, p0, q0, q1, q2;
353 setpx(idx, 0, q0 = rnd() & mask);
354 setsx(idx, 1, q1 = q0, I[bidx]);
355 setsx(idx, 2, q2 = q1, I[bidx]);
356 setsx(idx, 3, q2, I[bidx]);
357 setsx(idx, -1, p0 = q0, E[bidx] >> 2);
358 setsx(idx, -2, p1 = p0, I[bidx]);
359 setsx(idx, -3, p2 = p1, I[bidx]);
360 setsx(idx, -4, p2, I[bidx]);
361 for (j = 4; j < 8; j++) {
362 setpx(idx, -1 - j, rnd() & mask);
363 setpx(idx, j, rnd() & mask);
366 for (i = 6; i < 8; i++) /* off */ {
367 int idx = off + i * istride;
368 for (j = 0; j < 8; j++) {
369 setpx(idx, -1 - j, rnd() & mask);
370 setpx(idx, j, rnd() & mask);
375 #define randomize_buffers(bidx, lineoff, str) \
376 randomize_loopfilter_buffers(bidx, lineoff, str, BIT_DEPTH, dir, \
377 E, F, H, I, buf0, buf1)
379 static void check_loopfilter(void)
381 LOCAL_ALIGNED_32(uint8_t, base0, [32 + 16 * 16 * 2]);
382 LOCAL_ALIGNED_32(uint8_t, base1, [32 + 16 * 16 * 2]);
385 static const char *const dir_name[2] = { "h", "v" };
386 static const int E[2] = { 20, 28 }, I[2] = { 10, 16 };
387 static const int H[2] = { 7, 11 }, F[2] = { 1, 1 };
388 declare_func(void, uint8_t *dst, ptrdiff_t stride, int E, int I, int H);
390 ff_vp9dsp_init(&dsp);
392 for (dir = 0; dir < 2; dir++) {
393 uint8_t *buf0, *buf1;
394 int midoff = (dir ? 8 * 8 : 8) * SIZEOF_PIXEL;
395 int midoff_aligned = (dir ? 8 * 8 : 16) * SIZEOF_PIXEL;
397 buf0 = base0 + midoff_aligned;
398 buf1 = base1 + midoff_aligned;
400 for (wd = 0; wd < 3; wd++) {
402 if (check_func(dsp.loop_filter_8[wd][dir],
403 "vp9_loop_filter_%s_%d_8",
404 dir_name[dir], 4 << wd)) {
405 randomize_buffers(0, 0, 8);
406 memcpy(buf1 - midoff, buf0 - midoff,
407 16 * 8 * SIZEOF_PIXEL);
408 call_ref(buf0, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
409 call_new(buf1, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
410 if (memcmp(buf0 - midoff, buf1 - midoff, 16 * 8 * SIZEOF_PIXEL))
412 bench_new(buf1, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
416 midoff = (dir ? 16 * 8 : 8) * SIZEOF_PIXEL;
417 midoff_aligned = (dir ? 16 * 8 : 16) * SIZEOF_PIXEL;
419 buf0 = base0 + midoff_aligned;
420 buf1 = base1 + midoff_aligned;
422 // 16wd_16px loopfilter
423 if (check_func(dsp.loop_filter_16[dir],
424 "vp9_loop_filter_%s_16_16",
426 randomize_buffers(0, 0, 16);
427 randomize_buffers(0, 8, 16);
428 memcpy(buf1 - midoff, buf0 - midoff, 16 * 16 * SIZEOF_PIXEL);
429 call_ref(buf0, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
430 call_new(buf1, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
431 if (memcmp(buf0 - midoff, buf1 - midoff, 16 * 16 * SIZEOF_PIXEL))
433 bench_new(buf1, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
436 for (wd = 0; wd < 2; wd++) {
437 for (wd2 = 0; wd2 < 2; wd2++) {
439 if (check_func(dsp.loop_filter_mix2[wd][wd2][dir],
440 "vp9_loop_filter_mix2_%s_%d%d_16",
441 dir_name[dir], 4 << wd, 4 << wd2)) {
442 randomize_buffers(0, 0, 16);
443 randomize_buffers(1, 8, 16);
444 memcpy(buf1 - midoff, buf0 - midoff, 16 * 16 * SIZEOF_PIXEL);
445 #define M(a) ((a[1] << 8) | a[0])
446 call_ref(buf0, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
447 call_new(buf1, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
448 if (memcmp(buf0 - midoff, buf1 - midoff, 16 * 16 * SIZEOF_PIXEL))
450 bench_new(buf1, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
456 report("loopfilter");
462 #undef randomize_buffers
464 #define DST_BUF_SIZE (size * size * SIZEOF_PIXEL)
465 #define SRC_BUF_STRIDE 72
466 #define SRC_BUF_SIZE ((size + 7) * SRC_BUF_STRIDE * SIZEOF_PIXEL)
467 #define src (buf + 3 * SIZEOF_PIXEL * (SRC_BUF_STRIDE + 1))
469 #define randomize_buffers() \
471 uint32_t mask = pixel_mask[(BIT_DEPTH - 8) >> 1]; \
473 for (k = 0; k < SRC_BUF_SIZE; k += 4) { \
474 uint32_t r = rnd() & mask; \
475 AV_WN32A(buf + k, r); \
478 for (k = 0; k < DST_BUF_SIZE; k += 4) { \
479 uint32_t r = rnd() & mask; \
480 AV_WN32A(dst0 + k, r); \
481 AV_WN32A(dst1 + k, r); \
486 static void check_mc(void)
488 static const char *const filter_names[4] = {
489 "8tap_smooth", "8tap_regular", "8tap_sharp", "bilin"
491 static const char *const subpel_names[2][2] = { { "", "h" }, { "v", "hv" } };
492 static const char *const op_names[2] = { "put", "avg" };
494 LOCAL_ALIGNED_32(uint8_t, buf, [72 * 72 * 2]);
495 LOCAL_ALIGNED_32(uint8_t, dst0, [64 * 64 * 2]);
496 LOCAL_ALIGNED_32(uint8_t, dst1, [64 * 64 * 2]);
499 int op, hsize, filter, dx, dy;
501 declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT,
502 void, uint8_t *dst, ptrdiff_t dst_stride,
503 const uint8_t *ref, ptrdiff_t ref_stride,
504 int h, int mx, int my);
506 for (op = 0; op < 2; op++) {
507 ff_vp9dsp_init(&dsp);
508 for (hsize = 0; hsize < 5; hsize++) {
509 int size = 64 >> hsize;
511 for (filter = 0; filter < 4; filter++) {
512 for (dx = 0; dx < 2; dx++) {
513 for (dy = 0; dy < 2; dy++) {
515 snprintf(str, sizeof(str), "%s_%s_%d%s", op_names[op],
516 filter_names[filter], size,
517 subpel_names[dy][dx]);
519 snprintf(str, sizeof(str), "%s%d", op_names[op], size);
521 if (check_func(dsp.mc[hsize][filter][op][dx][dy],
523 int mx = dx ? 1 + (rnd() % 14) : 0;
524 int my = dy ? 1 + (rnd() % 14) : 0;
526 call_ref(dst0, size * SIZEOF_PIXEL,
527 src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
529 call_new(dst1, size * SIZEOF_PIXEL,
530 src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
532 if (memcmp(dst0, dst1, DST_BUF_SIZE))
535 // SIMD implementations for each filter of subpel
536 // functions are identical
537 if (filter >= 1 && filter <= 2) continue;
539 bench_new(dst1, size * SIZEOF_PIXEL,
540 src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
551 void checkasm_check_vp9dsp(void)