1/*
2 * Copyright (c) 2018 Yingming Fan <yingmingfan@gmail.com>
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
23#include "libavutil/intreadwrite.h"
24#include "libavutil/mem_internal.h"
25
26#include "libavcodec/avcodec.h"
27
28#include "libavcodec/hevcdsp.h"
29
30#include "checkasm.h"
31
32static const uint32_t pixel_mask[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
33static const uint32_t sao_size[5] = {8, 16, 32, 48, 64};
34
35#define SIZEOF_PIXEL ((bit_depth + 7) / 8)
36#define PIXEL_STRIDE (2*MAX_PB_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) //same with sao_edge src_stride
37#define BUF_SIZE (PIXEL_STRIDE * (64+2) * 2) //+2 for top and bottom row, *2 for high bit depth
38#define OFFSET_THRESH (1 << (bit_depth - 5))
39#define OFFSET_LENGTH 5
40
41#define randomize_buffers(buf0, buf1, size)                 \
42    do {                                                    \
43        uint32_t mask = pixel_mask[(bit_depth - 8) >> 1];   \
44        int k;                                              \
45        for (k = 0; k < size; k += 4) {                     \
46            uint32_t r = rnd() & mask;                      \
47            AV_WN32A(buf0 + k, r);                          \
48            AV_WN32A(buf1 + k, r);                          \
49        }                                                   \
50    } while (0)
51
52#define randomize_buffers2(buf, size)                       \
53    do {                                                    \
54        uint32_t max_offset = OFFSET_THRESH;                \
55        int k;                                              \
56        if (bit_depth == 8) {                               \
57            for (k = 0; k < size; k++) {                    \
58                uint8_t r = rnd() % max_offset;             \
59                buf[k] = r;                                 \
60            }                                               \
61        } else {                                            \
62            for (k = 0; k < size; k++) {                    \
63                uint16_t r = rnd() % max_offset;            \
64                buf[k] = r;                                 \
65            }                                               \
66        }                                                   \
67    } while (0)
68
69static void check_sao_band(HEVCDSPContext h, int bit_depth)
70{
71    int i;
72    LOCAL_ALIGNED_32(uint8_t, dst0, [BUF_SIZE]);
73    LOCAL_ALIGNED_32(uint8_t, dst1, [BUF_SIZE]);
74    LOCAL_ALIGNED_32(uint8_t, src0, [BUF_SIZE]);
75    LOCAL_ALIGNED_32(uint8_t, src1, [BUF_SIZE]);
76    int16_t offset_val[OFFSET_LENGTH];
77    int left_class = rnd()%32;
78
79    for (i = 0; i <= 4; i++) {
80        int block_size = sao_size[i];
81        int prev_size = i > 0 ? sao_size[i - 1] : 0;
82        ptrdiff_t stride = PIXEL_STRIDE*SIZEOF_PIXEL;
83        declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, uint8_t *src, ptrdiff_t dst_stride, ptrdiff_t src_stride,
84                          int16_t *sao_offset_val, int sao_left_class, int width, int height);
85
86        if (check_func(h.sao_band_filter[i], "hevc_sao_band_%d_%d", block_size, bit_depth)) {
87
88            for (int w = prev_size + 4; w <= block_size; w += 4) {
89                randomize_buffers(src0, src1, BUF_SIZE);
90                randomize_buffers2(offset_val, OFFSET_LENGTH);
91                memset(dst0, 0, BUF_SIZE);
92                memset(dst1, 0, BUF_SIZE);
93
94                call_ref(dst0, src0, stride, stride, offset_val, left_class, w, block_size);
95                call_new(dst1, src1, stride, stride, offset_val, left_class, w, block_size);
96                for (int j = 0; j < block_size; j++) {
97                    if (memcmp(dst0 + j*stride, dst1 + j*stride, w*SIZEOF_PIXEL))
98                        fail();
99                }
100            }
101            bench_new(dst1, src1, stride, stride, offset_val, left_class, block_size, block_size);
102        }
103    }
104}
105
106static void check_sao_edge(HEVCDSPContext h, int bit_depth)
107{
108    int i;
109    LOCAL_ALIGNED_32(uint8_t, dst0, [BUF_SIZE]);
110    LOCAL_ALIGNED_32(uint8_t, dst1, [BUF_SIZE]);
111    LOCAL_ALIGNED_32(uint8_t, src0, [BUF_SIZE]);
112    LOCAL_ALIGNED_32(uint8_t, src1, [BUF_SIZE]);
113    int16_t offset_val[OFFSET_LENGTH];
114    int eo = rnd()%4;
115
116    for (i = 0; i <= 4; i++) {
117        int block_size = sao_size[i];
118        int prev_size = i > 0 ? sao_size[i - 1] : 0;
119        ptrdiff_t stride = PIXEL_STRIDE*SIZEOF_PIXEL;
120        int offset = (AV_INPUT_BUFFER_PADDING_SIZE + PIXEL_STRIDE)*SIZEOF_PIXEL;
121        declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, uint8_t *src, ptrdiff_t stride_dst,
122                          int16_t *sao_offset_val, int eo, int width, int height);
123
124        for (int w = prev_size + 4; w <= block_size; w += 4) {
125            randomize_buffers(src0, src1, BUF_SIZE);
126            randomize_buffers2(offset_val, OFFSET_LENGTH);
127            memset(dst0, 0, BUF_SIZE);
128            memset(dst1, 0, BUF_SIZE);
129
130            if (check_func(h.sao_edge_filter[i], "hevc_sao_edge_%d_%d", block_size, bit_depth)) {
131                call_ref(dst0, src0 + offset, stride, offset_val, eo, w, block_size);
132                call_new(dst1, src1 + offset, stride, offset_val, eo, w, block_size);
133                for (int j = 0; j < block_size; j++) {
134                    if (memcmp(dst0 + j*stride, dst1 + j*stride, w*SIZEOF_PIXEL))
135                        fail();
136                }
137            }
138            bench_new(dst1, src1 + offset, stride, offset_val, eo, block_size, block_size);
139        }
140    }
141}
142
143void checkasm_check_hevc_sao(void)
144{
145    int bit_depth;
146
147    for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
148        HEVCDSPContext h;
149
150        ff_hevc_dsp_init(&h, bit_depth);
151        check_sao_band(h, bit_depth);
152    }
153    report("sao_band");
154
155    for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
156        HEVCDSPContext h;
157
158        ff_hevc_dsp_init(&h, bit_depth);
159        check_sao_edge(h, bit_depth);
160    }
161    report("sao_edge");
162}
163