lr_apply.c 12.5 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*
 * 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"

#include <stdio.h>

#include "common/intops.h"

#include "src/lr_apply.h"


enum LrRestorePlanes {
    LR_RESTORE_Y = 1 << 0,
    LR_RESTORE_U = 1 << 1,
    LR_RESTORE_V = 1 << 2,
};

static void backup_lpf(pixel *dst, ptrdiff_t dst_stride,
                       const pixel *src, ptrdiff_t src_stride,
                       const int first_stripe_h, const int next_stripe_h,
                       int row, const int row_h, const int w, const int h)
{
    src_stride = PXSTRIDE(src_stride);
    dst_stride = PXSTRIDE(dst_stride);
    if (row) {
        // Copy the top part of the stored loop filtered pixels from the
        // previous sb row needed above the first stripe of this sb row.
        pixel_copy(&dst[dst_stride *  0], &dst[dst_stride *  8], w);
        pixel_copy(&dst[dst_stride *  1], &dst[dst_stride *  9], w);
        pixel_copy(&dst[dst_stride *  2], &dst[dst_stride * 10], w);
        pixel_copy(&dst[dst_stride *  3], &dst[dst_stride * 11], w);
    }

    int stripe_h = first_stripe_h;
    dst += 4 * dst_stride;
    src += (stripe_h - 2) * src_stride;
    for (; row + stripe_h <= row_h; row += stripe_h) {
        for (int i = 0; i < 4; i++) {
            pixel_copy(dst, src, w);
            dst += dst_stride;
            src += src_stride;
        }
        stripe_h = next_stripe_h;
        src += (stripe_h - 4) * src_stride;
    }
}

void bytefn(dav1d_lr_copy_lpf)(Dav1dFrameContext *const f,
                               /*const*/ pixel *const src[3], const int sby)
{
    const int stripe_h = 64 - (8 * !sby);
    const ptrdiff_t offset = 8 * !!sby;
    const ptrdiff_t *const src_stride = f->cur.p.stride;

    // TODO Also check block level restore type to reduce copying.
    const int restore_planes =
        ((f->frame_hdr.restoration.type[0] != RESTORATION_NONE) << 0) +
        ((f->frame_hdr.restoration.type[1] != RESTORATION_NONE) << 1) +
        ((f->frame_hdr.restoration.type[2] != RESTORATION_NONE) << 2);

    if (restore_planes & LR_RESTORE_Y) {
        const int h = f->bh << 2;
        const int w = f->bw << 2;
        const int row_h = imin((sby + 1) << (6 + f->seq_hdr.sb128), h);
        const int y_stripe = (sby << (6 + f->seq_hdr.sb128)) - offset;
        backup_lpf(f->lf.lr_lpf_line_ptr[0], sizeof(pixel) * f->b4_stride * 4,
                   src[0] - offset * PXSTRIDE(src_stride[0]),
                   src_stride[0], stripe_h, 64, y_stripe, row_h, w, h);
    }
    if (restore_planes & (LR_RESTORE_U | LR_RESTORE_V)) {
        const int ss_ver = f->cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
        const int ss_hor = f->cur.p.p.layout != DAV1D_PIXEL_LAYOUT_I444;
        const int h = f->bh << (2 - ss_ver);
        const int w = f->bw << (2 - ss_hor);
        const int row_h = imin((sby + 1) << ((6 - ss_ver) + f->seq_hdr.sb128), h);
        const int stripe_h_uv = stripe_h >> ss_ver;
        const ptrdiff_t offset_uv = offset >> ss_ver;
        const int y_stripe =
            (sby << ((6 - ss_ver) + f->seq_hdr.sb128)) - offset_uv;

        if (restore_planes & LR_RESTORE_U) {
            backup_lpf(f->lf.lr_lpf_line_ptr[1], sizeof(pixel) * f->b4_stride * 4,
                       src[1] - offset_uv * PXSTRIDE(src_stride[1]),
                       src_stride[1], stripe_h_uv, 32, y_stripe,
                       row_h, w, h);
        }
        if (restore_planes & LR_RESTORE_V) {
            backup_lpf(f->lf.lr_lpf_line_ptr[2], sizeof(pixel) * f->b4_stride * 4,
                       src[2] - offset_uv * PXSTRIDE(src_stride[1]),
                       src_stride[1], stripe_h_uv, 32, y_stripe,
                       row_h, w, h);
        }
    }
}


static void lr_stripe(const Dav1dFrameContext *const f, pixel *p, int x, int y,
123 124
                      const int plane, const int unit_w, const int row_h,
                      const Av1RestorationUnit *const lr, enum LrEdgeFlags edges)
125 126
{
    const Dav1dDSPContext *const dsp = f->dsp;
127 128
    const int chroma = !!plane;
    const int ss_ver = chroma & (f->cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420);
129 130
    const int sbrow_has_bottom = (edges & LR_HAVE_BOTTOM);
    const pixel *lpf = f->lf.lr_lpf_line_ptr[plane] + x;
131
    const ptrdiff_t p_stride = f->cur.p.stride[chroma];
132 133
    const ptrdiff_t lpf_stride = sizeof(pixel) * f->b4_stride * 4;

134 135
    // The first stripe of the frame is shorter by 8 luma pixel rows.
    int stripe_h = imin((64 - 8 * !y) >> ss_ver, row_h - y);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    // FIXME [8] might be easier for SIMD
    int16_t filterh[7], filterv[7];
    if (lr->type == RESTORATION_WIENER) {
        filterh[0] = filterh[6] = lr->filter_h[0];
        filterh[1] = filterh[5] = lr->filter_h[1];
        filterh[2] = filterh[4] = lr->filter_h[2];
        filterh[3] = -((filterh[0] + filterh[1] + filterh[2]) * 2);

        filterv[0] = filterv[6] = lr->filter_v[0];
        filterv[1] = filterv[5] = lr->filter_v[1];
        filterv[2] = filterv[4] = lr->filter_v[2];
        filterv[3] = -((filterv[0] + filterv[1] + filterv[2]) * 2);
    }

    while (y + stripe_h <= row_h) {
        // TODO Look into getting rid of the this if
        if (y + stripe_h == row_h) {
            edges &= ~LR_HAVE_BOTTOM;
        } else {
            edges |= LR_HAVE_BOTTOM;
        }
        if (lr->type == RESTORATION_WIENER) {
            dsp->lr.wiener(p, p_stride, lpf, lpf_stride, unit_w, stripe_h,
                           filterh, filterv, edges);
        } else {
            assert(lr->type == RESTORATION_SGRPROJ);
            dsp->lr.selfguided(p, p_stride, lpf, lpf_stride, unit_w, stripe_h,
                               lr->sgr_idx, lr->sgr_weights, edges);
        }

        y += stripe_h;
        if (y + stripe_h > row_h && sbrow_has_bottom) break;
        p += stripe_h * PXSTRIDE(p_stride);
170 171
        edges |= LR_HAVE_TOP;
        stripe_h = imin(64 >> ss_ver, row_h - y);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        if (stripe_h == 0) break;
        lpf += 4 * PXSTRIDE(lpf_stride);
    }
}

static void backup3xU(pixel *dst, const pixel *src, const ptrdiff_t src_stride,
                      int u)
{
    for (; u > 0; u--, dst += 3, src += PXSTRIDE(src_stride))
        pixel_copy(dst, src, 3);
}

static void restore3xU(pixel *dst, const ptrdiff_t dst_stride, const pixel *src,
                       int u)
{
    for (; u > 0; u--, dst += PXSTRIDE(dst_stride), src += 3)
        pixel_copy(dst, src, 3);
}

static void lr_sbrow(const Dav1dFrameContext *const f, pixel *p, const int y,
                     const int w, const int h, const int row_h, const int plane)
{
194 195 196 197
    const int chroma = !!plane;
    const int ss_ver = chroma & (f->cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420);
    const int ss_hor = chroma & (f->cur.p.p.layout != DAV1D_PIXEL_LAYOUT_I444);
    const ptrdiff_t p_stride = f->cur.p.stride[chroma];
198 199 200 201 202 203

    const int unit_size_log2 = f->frame_hdr.restoration.unit_size[!!plane];
    const int unit_size = 1 << unit_size_log2;
    const int half_unit_size = unit_size >> 1;
    const int max_unit_size = unit_size + half_unit_size;

204
    // Y coordinate of the sbrow (y is 8 luma pixel rows above row_y)
205 206 207 208 209 210 211 212 213
    const int row_y = y + ((8 >> ss_ver) * !!y);

    // FIXME This is an ugly hack to lookup the proper AV1Filter unit for
    // chroma planes. Question: For Multithreaded decoding, is it better
    // to store the chroma LR information with collocated Luma information?
    // In other words. For a chroma restoration unit locate at 128,128 and
    // with a 4:2:0 chroma subsampling, do we store the filter information at
    // the AV1Filter unit located at (128,128) or (256,256)
    // TODO Support chroma subsampling.
214 215
    const int shift_ver = 7 - ss_ver;
    const int shift_hor = 7 - ss_hor;
216 217 218 219 220

    int ruy = (row_y >> unit_size_log2);
    // Merge last restoration unit if its height is < half_unit_size
    if (ruy > 0) ruy -= (ruy << unit_size_log2) + half_unit_size > h;

221 222 223
    // The first stripe of the frame is shorter by 8 luma pixel rows.
    const int filter_h =
        imin(((1 << (6 + f->seq_hdr.sb128)) - 8 * !y) >> ss_ver, h - y);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    pixel pre_lr_border[filter_h * 3];
    pixel post_lr_border[filter_h * 3];

    int unit_w = unit_size;

    enum LrEdgeFlags edges = (y > 0 ? LR_HAVE_TOP : 0) |
                             (row_h < h ? LR_HAVE_BOTTOM : 0);

    for (int x = 0, rux = 0; x < w; x+= unit_w, rux++, edges |= LR_HAVE_LEFT) {
        // TODO Clean up this if statement.
        if (x + max_unit_size > w) {
            unit_w = w - x;
            edges &= ~LR_HAVE_RIGHT;
        } else {
            edges |= LR_HAVE_RIGHT;
        }

        // Based on the position of the restoration unit, find the corresponding
        // AV1Filter unit.
        const int unit_idx = ((ruy & 16) >> 3) + ((rux & 16) >> 4);
        const Av1RestorationUnit *const lr =
246 247
            &f->lf.mask[(((ruy << (unit_size_log2)) >> shift_ver) * f->sb128w) +
                        (x >> shift_hor)].lr[plane][unit_idx];
248 249 250 251 252 253 254 255 256 257

        if (edges & LR_HAVE_LEFT) {
            restore3xU(p - 3, p_stride, pre_lr_border, filter_h);
        }
        // FIXME Don't backup if the next restoration unit is RESTORE_NONE
        // This also requires not restoring in the same conditions.
        if (edges & LR_HAVE_RIGHT) {
            backup3xU(pre_lr_border, p + unit_w - 3, p_stride, filter_h);
        }
        if (lr->type != RESTORATION_NONE) {
258
            lr_stripe(f, p, x, y, plane, unit_w, row_h, lr, edges);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        }
        if (edges & LR_HAVE_LEFT) {
            restore3xU(p - 3, p_stride, post_lr_border, filter_h);
        }
        if (edges & LR_HAVE_RIGHT) {
            backup3xU(post_lr_border, p + unit_w - 3, p_stride, filter_h);
        }
        p += unit_w;
    }
}

void bytefn(dav1d_lr_sbrow)(Dav1dFrameContext *const f, pixel *const dst[3],
                            const int sby)
{
    const ptrdiff_t offset_y = 8 * !!sby;
    const ptrdiff_t *const dst_stride = f->cur.p.stride;

    const int restore_planes =
        ((f->frame_hdr.restoration.type[0] != RESTORATION_NONE) << 0) +
        ((f->frame_hdr.restoration.type[1] != RESTORATION_NONE) << 1) +
        ((f->frame_hdr.restoration.type[2] != RESTORATION_NONE) << 2);

    if (restore_planes & LR_RESTORE_Y) {
        const int h = f->bh << 2;
        const int w = f->bw << 2;
        const int row_h = imin((sby + 1) << (6 + f->seq_hdr.sb128), h);
        const int y_stripe = (sby << (6 + f->seq_hdr.sb128)) - offset_y;
        lr_sbrow(f, dst[0] - offset_y * PXSTRIDE(dst_stride[0]), y_stripe, w,
                 h, row_h, 0);
    }
    if (restore_planes & (LR_RESTORE_U | LR_RESTORE_V)) {
        const int ss_ver = f->cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
        const int ss_hor = f->cur.p.p.layout != DAV1D_PIXEL_LAYOUT_I444;
        const int h = f->bh << (2 - ss_ver);
        const int w = f->bw << (2 - ss_hor);
        const int row_h = imin((sby + 1) << ((6 - ss_ver) + f->seq_hdr.sb128), h);
        const ptrdiff_t offset_uv = offset_y >> ss_ver;
        const int y_stripe =
            (sby << ((6 - ss_ver) + f->seq_hdr.sb128)) - offset_uv;
        if (restore_planes & LR_RESTORE_U)
            lr_sbrow(f, dst[1] - offset_uv * PXSTRIDE(dst_stride[1]), y_stripe,
                     w, h, row_h, 1);

        if (restore_planes & LR_RESTORE_V)
            lr_sbrow(f, dst[2] - offset_uv * PXSTRIDE(dst_stride[1]), y_stripe,
                     w, h, row_h, 2);
    }
}