]> git.sesse.net Git - ffmpeg/blob - tests/checkasm/vp9dsp.c
qsvdec: Only warn about unconsumed data if it happens more than once
[ffmpeg] / tests / checkasm / vp9dsp.c
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
21 #include <math.h>
22 #include <string.h>
23
24 #include "libavutil/common.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mathematics.h"
28
29 #include "libavcodec/vp9.h"
30 #include "libavcodec/vp9data.h"
31
32 #include "checkasm.h"
33
34 static const uint32_t pixel_mask[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
35
36 #define BIT_DEPTH 8
37 #define SIZEOF_PIXEL ((BIT_DEPTH + 7) / 8)
38
39 #define randomize_buffers() \
40     do { \
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);   \
47             }                                                              \
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];  \
51                 } else {                                                   \
52                     ((int32_t *) coef)[y * sz + x] =                       \
53                         ((uint16_t *) src)[y * sz + x] -                   \
54                         ((uint16_t *) dst)[y * sz + x];                    \
55                 }                                                          \
56             }                                                              \
57         }                                                                  \
58     } while(0)
59
60 // wht function copied from libvpx
61 static void fwht_1d(double *out, const double *in, int sz)
62 {
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];
68
69     out[0] = t0 - t2;
70     out[1] = t2;
71     out[2] = t3 + t1;
72     out[3] = t1;
73 }
74
75 // standard DCT-II
76 static void fdct_1d(double *out, const double *in, int sz)
77 {
78     int k, n;
79
80     for (k = 0; k < sz; k++) {
81         out[k] = 0.0;
82         for (n = 0; n < sz; n++)
83             out[k] += in[n] * cos(M_PI * (2 * n + 1) * k / (sz * 2.0));
84     }
85     out[0] *= M_SQRT1_2;
86 }
87
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)
92 {
93     int k, n;
94
95     for (k = 0; k < sz; k++) {
96         out[k] = 0.0;
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));
99     }
100 }
101
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)
106 {
107     int k, n;
108
109     for (k = 0; k < sz; k++) {
110         out[k] = 0.0;
111         for (n = 0; n < sz; n++)
112             out[k] += in[n] * sin(M_PI * (2 * n + 1) * (2 * k + 1) / (sz * 4.0));
113     }
114 }
115
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)
119 {
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 },
124         { 0.25 },
125         { 4.0 }
126     };
127     static const ftx1d_fn ftx1d_tbl[5][4][2] = {
128         {
129             { fdct_1d, fdct_1d },
130             { fadst4_1d, fdct_1d },
131             { fdct_1d, fadst4_1d },
132             { fadst4_1d, fadst4_1d },
133         }, {
134             { fdct_1d, fdct_1d },
135             { fadst_1d, fdct_1d },
136             { fdct_1d, fadst_1d },
137             { fadst_1d, fadst_1d },
138         }, {
139             { fdct_1d, fdct_1d },
140             { fadst_1d, fdct_1d },
141             { fdct_1d, fadst_1d },
142             { fadst_1d, fadst_1d },
143         }, {
144             { fdct_1d, fdct_1d },
145         }, {
146             { fwht_1d, fwht_1d },
147         },
148     };
149     double temp[1024];
150     double scaling_factor = scaling_factors[tx][txtp];
151     int i, j;
152
153     // cols
154     for (i = 0; i < sz; ++i) {
155         double temp_out[32];
156
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;
161     }
162
163     // rows
164     for (i = 0; i < sz; i++)
165         ftx1d_tbl[tx][txtp][1](&out[i * sz], &temp[i * sz], sz);
166 }
167
168 static void ftx(int16_t *buf, enum TxfmMode tx,
169                 enum TxfmType txtp, int sz, int bit_depth)
170 {
171     double ind[1024], outd[1024];
172     int n;
173
174     emms_c();
175     for (n = 0; n < sz * sz; n++) {
176         if (bit_depth == 8)
177             ind[n] = buf[n];
178         else
179             ind[n] = ((int32_t *) buf)[n];
180     }
181     ftx_2d(outd, ind, tx, txtp, sz);
182     for (n = 0; n < sz * sz; n++) {
183         if (bit_depth == 8)
184             buf[n] = lrint(outd[n]);
185         else
186             ((int32_t *) buf)[n] = lrint(outd[n]);
187     }
188 }
189
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)
192 {
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
198     // test
199
200     int n;
201     const int16_t *scan = ff_vp9_scans[tx][txtp];
202     int eob;
203
204     for (n = 0; n < sz * sz; n++) {
205         int rc = scan[n], rcx = rc % sz, rcy = rc / sz;
206
207         // find eob for this sub-idct
208         if (rcx >= sub || rcy >= sub)
209             break;
210
211         // copy coef
212         if (bit_depth == 8) {
213             out[rc] = in[rc];
214         } else {
215             AV_COPY32(&out[rc * 2], &in[rc * 2]);
216         }
217     }
218
219     eob = n;
220
221     for (; n < sz * sz; n++) {
222         int rc = scan[n];
223
224         // zero
225         if (bit_depth == 8) {
226             out[rc] = 0;
227         } else {
228             AV_ZERO32(&out[rc * 2]);
229         }
230     }
231
232     return eob;
233 }
234
235 static int iszero(const int16_t *c, int sz)
236 {
237     int n;
238
239     for (n = 0; n < sz / sizeof(int16_t); n += 2)
240         if (AV_RN32A(&c[n]))
241             return 0;
242
243     return 1;
244 }
245
246 #define SIZEOF_COEF (2 * ((BIT_DEPTH + 7) / 8))
247
248 static void check_itxfm(void)
249 {
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);
258     VP9DSPContext dsp;
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"
263     };
264
265     ff_vp9dsp_init(&dsp);
266
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;
270
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)) {
274                 randomize_buffers();
275                 ftx(coef, tx, txtp, sz, BIT_DEPTH);
276
277                 for (sub = (txtp == 0) ? 1 : 2; sub <= sz; sub <<= 1) {
278                     int eob;
279
280                     if (sub < sz) {
281                         eob = copy_subcoefs(subcoef0, coef, tx, txtp,
282                                             sz, sub, BIT_DEPTH);
283                     } else {
284                         eob = sz * sz;
285                         memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
286                     }
287
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))
296                         fail();
297                 }
298                 bench_new(dst, sz * SIZEOF_PIXEL, coef, sz * sz);
299             }
300         }
301     }
302     report("itxfm");
303 }
304
305 #undef randomize_buffers
306
307 #define setpx(a,b,c) \
308     do { \
309         if (SIZEOF_PIXEL == 1) { \
310             buf0[(a) + (b) * jstride] = av_clip_uint8(c); \
311         } else { \
312             ((uint16_t *)buf0)[(a) + (b) * jstride] = av_clip_uintp2(c, BIT_DEPTH); \
313         } \
314     } while (0)
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))
317
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)
323 {
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;
328     int i, j;
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]);
336         }
337     }
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]);
345         }
346         for (j = 4; j < 8; j++) {
347             setpx(idx, -1 - j, rnd() & mask);
348             setpx(idx, j, rnd() & mask);
349         }
350     }
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);
364         }
365     }
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);
371         }
372     }
373 }
374
375 #define randomize_buffers(bidx, lineoff, str)                        \
376     randomize_loopfilter_buffers(bidx, lineoff, str, BIT_DEPTH, dir, \
377                                  E, F, H, I, buf0, buf1)
378
379 static void check_loopfilter(void)
380 {
381     LOCAL_ALIGNED_32(uint8_t, base0, [32 + 16 * 16 * 2]);
382     LOCAL_ALIGNED_32(uint8_t, base1, [32 + 16 * 16 * 2]);
383     VP9DSPContext dsp;
384     int dir, wd, wd2;
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);
389
390     ff_vp9dsp_init(&dsp);
391
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;
396
397         buf0 = base0 + midoff_aligned;
398         buf1 = base1 + midoff_aligned;
399
400         for (wd = 0; wd < 3; wd++) {
401             // 4/8/16wd_8px
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))
411                     fail();
412                 bench_new(buf1, 16 * SIZEOF_PIXEL >> dir, E[0], I[0], H[0]);
413             }
414         }
415
416         midoff = (dir ? 16 * 8 : 8) * SIZEOF_PIXEL;
417         midoff_aligned = (dir ? 16 * 8 : 16) * SIZEOF_PIXEL;
418
419         buf0 = base0 + midoff_aligned;
420         buf1 = base1 + midoff_aligned;
421
422         // 16wd_16px loopfilter
423         if (check_func(dsp.loop_filter_16[dir],
424                        "vp9_loop_filter_%s_16_16",
425                        dir_name[dir])) {
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))
432                 fail();
433             bench_new(buf1, 16 * SIZEOF_PIXEL, E[0], I[0], H[0]);
434         }
435
436         for (wd = 0; wd < 2; wd++) {
437             for (wd2 = 0; wd2 < 2; wd2++) {
438                 // mix2 loopfilter
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))
449                         fail();
450                     bench_new(buf1, 16 * SIZEOF_PIXEL, M(E), M(I), M(H));
451 #undef M
452                 }
453             }
454         }
455     }
456     report("loopfilter");
457 }
458
459 #undef setsx
460 #undef setpx
461 #undef setdx
462 #undef randomize_buffers
463
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))
468
469 #define randomize_buffers()                               \
470     do {                                                  \
471         uint32_t mask = pixel_mask[(BIT_DEPTH - 8) >> 1]; \
472         int k;                                            \
473         for (k = 0; k < SRC_BUF_SIZE; k += 4) {           \
474             uint32_t r = rnd() & mask;                    \
475             AV_WN32A(buf + k, r);                         \
476         }                                                 \
477         if (op == 1) {                                    \
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);                    \
482             }                                             \
483         }                                                 \
484     } while (0)
485
486 static void check_mc(void)
487 {
488     static const char *const filter_names[4] = {
489         "8tap_smooth", "8tap_regular", "8tap_sharp", "bilin"
490     };
491     static const char *const subpel_names[2][2] = { { "", "h" }, { "v", "hv" } };
492     static const char *const op_names[2] = { "put", "avg" };
493
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]);
497     char str[256];
498     VP9DSPContext dsp;
499     int op, hsize, filter, dx, dy;
500
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);
505
506     for (op = 0; op < 2; op++) {
507         ff_vp9dsp_init(&dsp);
508         for (hsize = 0; hsize < 5; hsize++) {
509             int size = 64 >> hsize;
510
511             for (filter = 0; filter < 4; filter++) {
512                 for (dx = 0; dx < 2; dx++) {
513                     for (dy = 0; dy < 2; dy++) {
514                         if (dx || dy) {
515                             snprintf(str, sizeof(str), "%s_%s_%d%s", op_names[op],
516                                      filter_names[filter], size,
517                                      subpel_names[dy][dx]);
518                         } else {
519                             snprintf(str, sizeof(str), "%s%d", op_names[op], size);
520                         }
521                         if (check_func(dsp.mc[hsize][filter][op][dx][dy],
522                                        "vp9_%s", str)) {
523                             int mx = dx ? 1 + (rnd() % 14) : 0;
524                             int my = dy ? 1 + (rnd() % 14) : 0;
525                             randomize_buffers();
526                             call_ref(dst0, size * SIZEOF_PIXEL,
527                                      src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
528                                      size, mx, my);
529                             call_new(dst1, size * SIZEOF_PIXEL,
530                                      src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
531                                      size, mx, my);
532                             if (memcmp(dst0, dst1, DST_BUF_SIZE))
533                                 fail();
534
535                             // SIMD implementations for each filter of subpel
536                             // functions are identical
537                             if (filter >= 1 && filter <= 2) continue;
538
539                             bench_new(dst1, size * SIZEOF_PIXEL,
540                                       src, SRC_BUF_STRIDE * SIZEOF_PIXEL,
541                                       size, mx, my);
542                         }
543                     }
544                 }
545             }
546         }
547     }
548     report("mc");
549 }
550
551 void checkasm_check_vp9dsp(void)
552 {
553     check_itxfm();
554     check_loopfilter();
555     check_mc();
556 }