cdef_tmpl.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

stemidts's avatar
stemidts committed
30
#include <assert.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <stdlib.h>

#include "common/intops.h"

#include "src/cdef.h"

static inline int constrain(const int diff, const int threshold,
                            const int damping)
{
    if (!threshold) return 0;
    const int shift = imax(0, damping - ulog2(threshold));
    return apply_sign(imin(abs(diff), imax(0, threshold - (abs(diff) >> shift))),
                      diff);
}

46 47
static inline void fill(uint16_t *tmp, const ptrdiff_t stride,
                        const int w, const int h)
48 49 50
{
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++)
51
            tmp[x] = INT16_MAX;
52 53 54 55
        tmp += stride;
    }
}

56 57 58 59 60
static void padding(uint16_t *tmp, const ptrdiff_t tmp_stride,
                    const pixel *src, const ptrdiff_t src_stride,
                    const pixel (*left)[2], pixel *const top[2],
                    const int w, const int h,
                    const enum CdefEdgeFlags edges)
61 62 63
{
    // fill extended input buffer
    int x_start = -2, x_end = w + 2, y_start = -2, y_end = h + 2;
64
    if (!(edges & CDEF_HAVE_TOP)) {
65
        fill(tmp - 2 - 2 * tmp_stride, tmp_stride, w + 4, 2);
66 67
        y_start = 0;
    }
68
    if (!(edges & CDEF_HAVE_BOTTOM)) {
69
        fill(tmp + h * tmp_stride - 2, tmp_stride, w + 4, 2);
70 71
        y_end -= 2;
    }
72
    if (!(edges & CDEF_HAVE_LEFT)) {
73
        fill(tmp + y_start * tmp_stride - 2, tmp_stride, 2, y_end - y_start);
74 75
        x_start = 0;
    }
76
    if (!(edges & CDEF_HAVE_RIGHT)) {
77
        fill(tmp + y_start * tmp_stride + w, tmp_stride, 2, y_end - y_start);
78 79
        x_end -= 2;
    }
80

81 82
    for (int y = y_start; y < 0; y++)
        for (int x = x_start; x < x_end; x++)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            tmp[x + y * tmp_stride] = top[y & 1][x];
    for (int y = 0; y < h; y++)
        for (int x = x_start; x < 0; x++)
            tmp[x + y * tmp_stride] = left[y][2 + x];
    for (int y = 0; y < y_end; y++) {
        for (int x = (y < h) ? 0 : x_start; x < x_end; x++)
            tmp[x] = src[x];
        src += PXSTRIDE(src_stride);
        tmp += tmp_stride;
    }
}

static NOINLINE void
cdef_filter_block_c(pixel *dst, const ptrdiff_t dst_stride,
                    const pixel (*left)[2], /*const*/ pixel *const top[2],
                    const int w, const int h, const int pri_strength,
                    const int sec_strength, const int dir,
100 101
                    const int damping, const enum CdefEdgeFlags edges
                    HIGHBD_DECL_SUFFIX)
102
{
103 104 105 106 107 108 109 110 111 112
    static const int8_t cdef_directions[8 /* dir */][2 /* pass */] = {
        { -1 * 12 + 1, -2 * 12 + 2 },
        {  0 * 12 + 1, -1 * 12 + 2 },
        {  0 * 12 + 1,  0 * 12 + 2 },
        {  0 * 12 + 1,  1 * 12 + 2 },
        {  1 * 12 + 1,  2 * 12 + 2 },
        {  1 * 12 + 0,  2 * 12 + 1 },
        {  1 * 12 + 0,  2 * 12 + 0 },
        {  1 * 12 + 0,  2 * 12 - 1 }
    };
113 114 115 116
    const ptrdiff_t tmp_stride = 12;
    assert((w == 4 || w == 8) && (h == 4 || h == 8));
    uint16_t tmp_buf[144];  // 12*12 is the maximum value of tmp_stride * (h + 4)
    uint16_t *tmp = tmp_buf + 2 * tmp_stride + 2;
117
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
118
    const int pri_tap = 4 - ((pri_strength >> bitdepth_min_8) & 1);
119 120

    padding(tmp, tmp_stride, dst, dst_stride, left, top, w, h, edges);
121 122 123 124 125

    // run actual filter
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int sum = 0;
126
            const int px = dst[x];
127
            int max = px, min = px;
128
            int pri_tap_k = pri_tap;
129
            for (int k = 0; k < 2; k++) {
130
                const int off1 = cdef_directions[dir][k];
131 132
                const int p0 = tmp[x + off1];
                const int p1 = tmp[x - off1];
133 134 135 136
                sum += pri_tap_k * constrain(p0 - px, pri_strength, damping);
                sum += pri_tap_k * constrain(p1 - px, pri_strength, damping);
                // if pri_tap_k == 4 then it becomes 2 else it remains 3
                pri_tap_k -= (pri_tap_k << 1) - 6;
137 138
                if (p0 != INT16_MAX) max = imax(p0, max);
                if (p1 != INT16_MAX) max = imax(p1, max);
139 140
                min = imin(p0, min);
                min = imin(p1, min);
141
                const int off2 = cdef_directions[(dir + 2) & 7][k];
142 143
                const int s0 = tmp[x + off2];
                const int s1 = tmp[x - off2];
144
                const int off3 = cdef_directions[(dir + 6) & 7][k];
145 146 147 148 149 150
                const int s2 = tmp[x + off3];
                const int s3 = tmp[x - off3];
                if (s0 != INT16_MAX) max = imax(s0, max);
                if (s1 != INT16_MAX) max = imax(s1, max);
                if (s2 != INT16_MAX) max = imax(s2, max);
                if (s3 != INT16_MAX) max = imax(s3, max);
151 152 153 154
                min = imin(s0, min);
                min = imin(s1, min);
                min = imin(s2, min);
                min = imin(s3, min);
155 156 157 158 159 160
                // sec_tap starts at 2 and becomes 1
                const int sec_tap = 2 - k;
                sum += sec_tap * constrain(s0 - px, sec_strength, damping);
                sum += sec_tap * constrain(s1 - px, sec_strength, damping);
                sum += sec_tap * constrain(s2 - px, sec_strength, damping);
                sum += sec_tap * constrain(s3 - px, sec_strength, damping);
161
            }
162
            dst[x] = iclip(px + ((8 + sum - (sum < 0)) >> 4), min, max);
163
        }
164 165
        dst += PXSTRIDE(dst_stride);
        tmp += tmp_stride;
166 167 168 169 170 171
    }
}

#define cdef_fn(w, h) \
static void cdef_filter_block_##w##x##h##_c(pixel *const dst, \
                                            const ptrdiff_t stride, \
172
                                            const pixel (*left)[2], \
173 174 175 176 177
                                            /*const*/ pixel *const top[2], \
                                            const int pri_strength, \
                                            const int sec_strength, \
                                            const int dir, \
                                            const int damping, \
178 179
                                            const enum CdefEdgeFlags edges \
                                            HIGHBD_DECL_SUFFIX) \
180
{ \
181
    cdef_filter_block_c(dst, stride, left, top, w, h, pri_strength, sec_strength, \
182
                        dir, damping, edges HIGHBD_TAIL_SUFFIX); \
183 184 185 186 187 188 189
}

cdef_fn(4, 4);
cdef_fn(4, 8);
cdef_fn(8, 8);

static int cdef_find_dir_c(const pixel *img, const ptrdiff_t stride,
190
                           unsigned *const var HIGHBD_DECL_SUFFIX)
191
{
192
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
193 194 195 196 197 198
    int partial_sum_hv[2][8] = { { 0 } };
    int partial_sum_diag[2][15] = { { 0 } };
    int partial_sum_alt[4][11] = { { 0 } };

    for (int y = 0; y < 8; y++) {
        for (int x = 0; x < 8; x++) {
199
            const int px = (img[x] >> bitdepth_min_8) - 128;
200 201 202 203 204 205 206 207 208

            partial_sum_diag[0][     y       +  x      ] += px;
            partial_sum_alt [0][     y       + (x >> 1)] += px;
            partial_sum_hv  [0][     y                 ] += px;
            partial_sum_alt [1][3 +  y       - (x >> 1)] += px;
            partial_sum_diag[1][7 +  y       -  x      ] += px;
            partial_sum_alt [2][3 - (y >> 1) +  x      ] += px;
            partial_sum_hv  [1][                x      ] += px;
            partial_sum_alt [3][    (y >> 1) +  x      ] += px;
209
        }
210
        img += PXSTRIDE(stride);
211
    }
212 213 214 215 216

    unsigned cost[8] = { 0 };
    for (int n = 0; n < 8; n++) {
        cost[2] += partial_sum_hv[0][n] * partial_sum_hv[0][n];
        cost[6] += partial_sum_hv[1][n] * partial_sum_hv[1][n];
217
    }
218 219 220 221 222 223 224 225 226 227
    cost[2] *= 105;
    cost[6] *= 105;

    static const uint16_t div_table[7] = { 840, 420, 280, 210, 168, 140, 120 };
    for (int n = 0; n < 7; n++) {
        const int d = div_table[n];
        cost[0] += (partial_sum_diag[0][n]      * partial_sum_diag[0][n] +
                    partial_sum_diag[0][14 - n] * partial_sum_diag[0][14 - n]) * d;
        cost[4] += (partial_sum_diag[1][n]      * partial_sum_diag[1][n] +
                    partial_sum_diag[1][14 - n] * partial_sum_diag[1][14 - n]) * d;
228
    }
229 230 231 232 233 234 235 236 237 238 239 240
    cost[0] += partial_sum_diag[0][7] * partial_sum_diag[0][7] * 105;
    cost[4] += partial_sum_diag[1][7] * partial_sum_diag[1][7] * 105;

    for (int n = 0; n < 4; n++) {
        unsigned *const cost_ptr = &cost[n * 2 + 1];
        for (int m = 0; m < 5; m++)
            *cost_ptr += partial_sum_alt[n][3 + m] * partial_sum_alt[n][3 + m];
        *cost_ptr *= 105;
        for (int m = 0; m < 3; m++) {
            const int d = div_table[2 * m + 1];
            *cost_ptr += (partial_sum_alt[n][m]      * partial_sum_alt[n][m] +
                          partial_sum_alt[n][10 - m] * partial_sum_alt[n][10 - m]) * d;
241 242
        }
    }
243 244 245 246 247 248 249

    int best_dir = 0;
    unsigned best_cost = cost[0];
    for (int n = 1; n < 8; n++) {
        if (cost[n] > best_cost) {
            best_cost = cost[n];
            best_dir = n;
250 251
        }
    }
252 253

    *var = (best_cost - (cost[best_dir ^ 4])) >> 10;
254 255 256 257 258 259 260 261
    return best_dir;
}

void bitfn(dav1d_cdef_dsp_init)(Dav1dCdefDSPContext *const c) {
    c->dir = cdef_find_dir_c;
    c->fb[0] = cdef_filter_block_8x8_c;
    c->fb[1] = cdef_filter_block_4x8_c;
    c->fb[2] = cdef_filter_block_4x4_c;
262

263 264 265 266
#if HAVE_ASM
#if ARCH_AARCH64 || ARCH_ARM
    bitfn(dav1d_cdef_dsp_init_arm)(c);
#elif ARCH_X86
267 268
    bitfn(dav1d_cdef_dsp_init_x86)(c);
#endif
269
#endif
270
}