LCOV - code coverage report
Current view: top level - tests/checkasm - h264dsp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 122 124 98.4 %
Date: 2017-12-16 01:21:47 Functions: 9 9 100.0 %

          Line data    Source code
       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             : 
      21             : #include <string.h>
      22             : #include "checkasm.h"
      23             : #include "libavcodec/avcodec.h"
      24             : #include "libavcodec/h264dsp.h"
      25             : #include "libavcodec/h264data.h"
      26             : #include "libavutil/common.h"
      27             : #include "libavutil/internal.h"
      28             : #include "libavutil/intreadwrite.h"
      29             : 
      30             : static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff };
      31             : 
      32             : #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
      33             : #define SIZEOF_COEF  (2 * ((bit_depth + 7) / 8))
      34             : #define PIXEL_STRIDE 16
      35             : 
      36             : #define randomize_buffers()                                                  \
      37             :     do {                                                                     \
      38             :         int x, y;                                                            \
      39             :         uint32_t mask = pixel_mask[bit_depth - 8];                           \
      40             :         for (y = 0; y < sz; y++) {                                           \
      41             :             for (x = 0; x < PIXEL_STRIDE; x += 4) {                          \
      42             :                 AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask);          \
      43             :                 AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask);          \
      44             :             }                                                                \
      45             :             for (x = 0; x < sz; x++) {                                       \
      46             :                 if (bit_depth == 8) {                                        \
      47             :                     coef[y * sz + x] = src[y * PIXEL_STRIDE + x] -           \
      48             :                                        dst[y * PIXEL_STRIDE + x];            \
      49             :                 } else {                                                     \
      50             :                     ((int32_t *)coef)[y * sz + x] =                          \
      51             :                         ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] -        \
      52             :                         ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x];         \
      53             :                 }                                                            \
      54             :             }                                                                \
      55             :         }                                                                    \
      56             :     } while (0)
      57             : 
      58             : #define dct4x4_impl(size, dctcoef)                                           \
      59             : static void dct4x4_##size(dctcoef *coef)                                     \
      60             : {                                                                            \
      61             :     int i, y, x;                                                             \
      62             :     dctcoef tmp[16];                                                         \
      63             :     for (i = 0; i < 4; i++) {                                                \
      64             :         const int z0 = coef[i*4 + 0] + coef[i*4 + 3];                        \
      65             :         const int z1 = coef[i*4 + 1] + coef[i*4 + 2];                        \
      66             :         const int z2 = coef[i*4 + 0] - coef[i*4 + 3];                        \
      67             :         const int z3 = coef[i*4 + 1] - coef[i*4 + 2];                        \
      68             :         tmp[i + 4*0] =   z0 +   z1;                                          \
      69             :         tmp[i + 4*1] = 2*z2 +   z3;                                          \
      70             :         tmp[i + 4*2] =   z0 -   z1;                                          \
      71             :         tmp[i + 4*3] =   z2 - 2*z3;                                          \
      72             :     }                                                                        \
      73             :     for (i = 0; i < 4; i++) {                                                \
      74             :         const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3];                          \
      75             :         const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2];                          \
      76             :         const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3];                          \
      77             :         const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2];                          \
      78             :         coef[i*4 + 0] =   z0 +   z1;                                         \
      79             :         coef[i*4 + 1] = 2*z2 +   z3;                                         \
      80             :         coef[i*4 + 2] =   z0 -   z1;                                         \
      81             :         coef[i*4 + 3] =   z2 - 2*z3;                                         \
      82             :     }                                                                        \
      83             :     for (y = 0; y < 4; y++) {                                                \
      84             :         for (x = 0; x < 4; x++) {                                            \
      85             :             static const int scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \
      86             :             const int idx = (y & 1) + (x & 1);                               \
      87             :             coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15;  \
      88             :         }                                                                    \
      89             :     }                                                                        \
      90             : }
      91             : 
      92             : #define DCT8_1D(src, srcstride, dst, dststride) do {                         \
      93             :     const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7];              \
      94             :     const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7];              \
      95             :     const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6];              \
      96             :     const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6];              \
      97             :     const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5];              \
      98             :     const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5];              \
      99             :     const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4];              \
     100             :     const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4];              \
     101             :     const int b0 = a0 + a6;                                                  \
     102             :     const int b1 = a2 + a4;                                                  \
     103             :     const int b2 = a0 - a6;                                                  \
     104             :     const int b3 = a2 - a4;                                                  \
     105             :     const int b4 = a3 + a5 + (a1 + (a1 >> 1));                               \
     106             :     const int b5 = a1 - a7 - (a5 + (a5 >> 1));                               \
     107             :     const int b6 = a1 + a7 - (a3 + (a3 >> 1));                               \
     108             :     const int b7 = a3 - a5 + (a7 + (a7 >> 1));                               \
     109             :     (dst)[dststride * 0] =  b0 +  b1;                                        \
     110             :     (dst)[dststride * 1] =  b4 + (b7 >> 2);                                  \
     111             :     (dst)[dststride * 2] =  b2 + (b3 >> 1);                                  \
     112             :     (dst)[dststride * 3] =  b5 + (b6 >> 2);                                  \
     113             :     (dst)[dststride * 4] =  b0  - b1;                                        \
     114             :     (dst)[dststride * 5] =  b6 - (b5 >> 2);                                  \
     115             :     (dst)[dststride * 6] = (b2 >> 1) - b3;                                   \
     116             :     (dst)[dststride * 7] = (b4 >> 2) - b7;                                   \
     117             : } while (0)
     118             : 
     119             : #define dct8x8_impl(size, dctcoef)                                           \
     120             : static void dct8x8_##size(dctcoef *coef)                                     \
     121             : {                                                                            \
     122             :     int i, x, y;                                                             \
     123             :     dctcoef tmp[64];                                                         \
     124             :     for (i = 0; i < 8; i++)                                                  \
     125             :         DCT8_1D(coef + i, 8, tmp + i, 8);                                    \
     126             :                                                                              \
     127             :     for (i = 0; i < 8; i++)                                                  \
     128             :         DCT8_1D(tmp + 8*i, 1, coef + i, 8);                                  \
     129             :                                                                              \
     130             :     for (y = 0; y < 8; y++) {                                                \
     131             :         for (x = 0; x < 8; x++) {                                            \
     132             :             static const int scale[] = {                                     \
     133             :                 13107 * 20, 11428 * 18, 20972 * 32,                          \
     134             :                 12222 * 19, 16777 * 25, 15481 * 24,                          \
     135             :             };                                                               \
     136             :             static const int idxmap[] = {                                    \
     137             :                 0, 3, 4, 3,                                                  \
     138             :                 3, 1, 5, 1,                                                  \
     139             :                 4, 5, 2, 5,                                                  \
     140             :                 3, 1, 5, 1,                                                  \
     141             :             };                                                               \
     142             :             const int idx = idxmap[(y & 3) * 4 + (x & 3)];                   \
     143             :             coef[y*8 + x] = ((int64_t)coef[y*8 + x] *                        \
     144             :                              scale[idx] + (1 << 17)) >> 18;                  \
     145             :         }                                                                    \
     146             :     }                                                                        \
     147             : }
     148             : 
     149         429 : dct4x4_impl(16, int16_t)
     150         858 : dct4x4_impl(32, int32_t)
     151             : 
     152          65 : dct8x8_impl(16, int16_t)
     153         130 : dct8x8_impl(32, int32_t)
     154             : 
     155        1287 : static void dct4x4(int16_t *coef, int bit_depth)
     156             : {
     157        1287 :     if (bit_depth == 8)
     158         429 :         dct4x4_16(coef);
     159             :     else
     160         858 :         dct4x4_32((int32_t *) coef);
     161        1287 : }
     162             : 
     163         195 : static void dct8x8(int16_t *coef, int bit_depth)
     164             : {
     165         195 :     if (bit_depth == 8) {
     166          65 :         dct8x8_16(coef);
     167             :     } else {
     168         130 :         dct8x8_32((int32_t *) coef);
     169             :     }
     170         195 : }
     171             : 
     172             : 
     173          13 : static void check_idct(void)
     174             : {
     175          13 :     LOCAL_ALIGNED_16(uint8_t, src,  [8 * 8 * 2]);
     176          13 :     LOCAL_ALIGNED_16(uint8_t, dst,  [8 * 8 * 2]);
     177          13 :     LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]);
     178          13 :     LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]);
     179          13 :     LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]);
     180          13 :     LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]);
     181          13 :     LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]);
     182             :     H264DSPContext h;
     183             :     int bit_depth, sz, align, dc;
     184          13 :     declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride);
     185             : 
     186          52 :     for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
     187          39 :         ff_h264dsp_init(&h, bit_depth, 1);
     188         117 :         for (sz = 4; sz <= 8; sz += 4) {
     189          78 :             randomize_buffers();
     190             : 
     191          78 :             if (sz == 4)
     192          39 :                 dct4x4(coef, bit_depth);
     193             :             else
     194          39 :                 dct8x8(coef, bit_depth);
     195             : 
     196         234 :             for (dc = 0; dc <= 1; dc++) {
     197         156 :                 void (*idct)(uint8_t *, int16_t *, int) = NULL;
     198         156 :                 switch ((sz << 1) | dc) {
     199          39 :                 case (4 << 1) | 0: idct = h.h264_idct_add; break;
     200          39 :                 case (4 << 1) | 1: idct = h.h264_idct_dc_add; break;
     201          39 :                 case (8 << 1) | 0: idct = h.h264_idct8_add; break;
     202          39 :                 case (8 << 1) | 1: idct = h.h264_idct8_dc_add; break;
     203             :                 }
     204         156 :                 if (check_func(idct, "h264_idct%d_add%s_%dbpp", sz, dc ? "_dc" : "", bit_depth)) {
     205         103 :                     for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) {
     206          72 :                         uint8_t *dst1 = dst1_base + align;
     207          72 :                         if (dc) {
     208          38 :                             memset(subcoef0, 0, sz * sz * SIZEOF_COEF);
     209          38 :                             memcpy(subcoef0, coef, SIZEOF_COEF);
     210             :                         } else {
     211          34 :                             memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
     212             :                         }
     213          72 :                         memcpy(dst0, dst, sz * PIXEL_STRIDE);
     214          72 :                         memcpy(dst1, dst, sz * PIXEL_STRIDE);
     215          72 :                         memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
     216          72 :                         call_ref(dst0, subcoef0, PIXEL_STRIDE);
     217          72 :                         call_new(dst1, subcoef1, PIXEL_STRIDE);
     218         144 :                         if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) ||
     219          72 :                             memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF))
     220           0 :                             fail();
     221          72 :                         bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL);
     222             :                     }
     223             :                 }
     224             :             }
     225             :         }
     226             :     }
     227          13 : }
     228             : 
     229          13 : static void check_idct_multiple(void)
     230             : {
     231          13 :     LOCAL_ALIGNED_16(uint8_t, dst_full,  [16 * 16 * 2]);
     232          13 :     LOCAL_ALIGNED_16(int16_t, coef_full, [16 * 16 * 2]);
     233          13 :     LOCAL_ALIGNED_16(uint8_t, dst0,  [16 * 16 * 2]);
     234          13 :     LOCAL_ALIGNED_16(uint8_t, dst1,  [16 * 16 * 2]);
     235          13 :     LOCAL_ALIGNED_16(int16_t, coef0, [16 * 16 * 2]);
     236          13 :     LOCAL_ALIGNED_16(int16_t, coef1, [16 * 16 * 2]);
     237          13 :     LOCAL_ALIGNED_16(uint8_t, nnzc,  [15 * 8]);
     238             :     H264DSPContext h;
     239             :     int bit_depth, i, y, func;
     240          13 :     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]);
     241             : 
     242          52 :     for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
     243          39 :         ff_h264dsp_init(&h, bit_depth, 1);
     244         156 :         for (func = 0; func < 3; func++) {
     245         117 :             void (*idct)(uint8_t *, const int *, int16_t *, int, const uint8_t[]) = NULL;
     246             :             const char *name;
     247         117 :             int sz = 4, intra = 0;
     248         117 :             int block_offset[16] = { 0 };
     249         117 :             switch (func) {
     250          39 :             case 0:
     251          39 :                 idct = h.h264_idct_add16;
     252          39 :                 name = "h264_idct_add16";
     253          39 :                 break;
     254          39 :             case 1:
     255          39 :                 idct = h.h264_idct_add16intra;
     256          39 :                 name = "h264_idct_add16intra";
     257          39 :                 intra = 1;
     258          39 :                 break;
     259          39 :             case 2:
     260          39 :                 idct = h.h264_idct8_add4;
     261          39 :                 name = "h264_idct8_add4";
     262          39 :                 sz = 8;
     263          39 :                 break;
     264             :             }
     265         117 :             memset(nnzc, 0, 15 * 8);
     266         117 :             memset(coef_full, 0, 16 * 16 * SIZEOF_COEF);
     267        1521 :             for (i = 0; i < 16 * 16; i += sz * sz) {
     268             :                 uint8_t src[8 * 8 * 2];
     269             :                 uint8_t dst[8 * 8 * 2];
     270             :                 int16_t coef[8 * 8 * 2];
     271        1404 :                 int index = i / sz;
     272        1404 :                 int block_y = (index / 16) * sz;
     273        1404 :                 int block_x = index % 16;
     274        1404 :                 int offset = (block_y * 16 + block_x) * SIZEOF_PIXEL;
     275        1404 :                 int nnz = rnd() % 3;
     276             : 
     277        1404 :                 randomize_buffers();
     278        1404 :                 if (sz == 4)
     279        1248 :                     dct4x4(coef, bit_depth);
     280             :                 else
     281         156 :                     dct8x8(coef, bit_depth);
     282             : 
     283        7644 :                 for (y = 0; y < sz; y++)
     284       12480 :                     memcpy(&dst_full[offset + y * 16 * SIZEOF_PIXEL],
     285       12480 :                            &dst[PIXEL_STRIDE * y], sz * SIZEOF_PIXEL);
     286             : 
     287        1404 :                 if (nnz > 1)
     288         465 :                     nnz = sz * sz;
     289        1404 :                 memcpy(&coef_full[i * SIZEOF_COEF/sizeof(coef[0])],
     290        1404 :                        coef, nnz * SIZEOF_COEF);
     291             : 
     292        1404 :                 if (intra && nnz == 1)
     293         194 :                     nnz = 0;
     294             : 
     295        1404 :                 nnzc[scan8[i / 16]] = nnz;
     296        1404 :                 block_offset[i / 16] = offset;
     297             :             }
     298             : 
     299         117 :             if (check_func(idct, "%s_%dbpp", name, bit_depth)) {
     300          24 :                 memcpy(coef0, coef_full, 16 * 16 * SIZEOF_COEF);
     301          24 :                 memcpy(coef1, coef_full, 16 * 16 * SIZEOF_COEF);
     302          24 :                 memcpy(dst0, dst_full, 16 * 16 * SIZEOF_PIXEL);
     303          24 :                 memcpy(dst1, dst_full, 16 * 16 * SIZEOF_PIXEL);
     304          24 :                 call_ref(dst0, block_offset, coef0, 16 * SIZEOF_PIXEL, nnzc);
     305          24 :                 call_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
     306          48 :                 if (memcmp(dst0, dst1, 16 * 16 * SIZEOF_PIXEL) ||
     307          24 :                     memcmp(coef0, coef1, 16 * 16 * SIZEOF_COEF))
     308           0 :                     fail();
     309          24 :                 bench_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc);
     310             :             }
     311             :         }
     312             :     }
     313          13 : }
     314             : 
     315          13 : void checkasm_check_h264dsp(void)
     316             : {
     317          13 :     check_idct();
     318          13 :     check_idct_multiple();
     319          13 :     report("idct");
     320          13 : }

Generated by: LCOV version 1.13