1 /*
2  * Copyright © 2018, VideoLAN and dav1d authors
3  * Copyright © 2018, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "config.h"
29 
30 #include <stdio.h>
31 
32 #include "common/intops.h"
33 
34 #include "src/lr_apply.h"
35 
lr_stripe(const Dav1dFrameContext * const f,pixel * p,const pixel (* left)[4],int x,int y,const int plane,const int unit_w,const int row_h,const Av1RestorationUnit * const lr,enum LrEdgeFlags edges)36 static void lr_stripe(const Dav1dFrameContext *const f, pixel *p,
37                       const pixel (*left)[4], int x, int y,
38                       const int plane, const int unit_w, const int row_h,
39                       const Av1RestorationUnit *const lr, enum LrEdgeFlags edges)
40 {
41     const Dav1dDSPContext *const dsp = f->dsp;
42     const int chroma = !!plane;
43     const int ss_ver = chroma & (f->sr_cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420);
44     const ptrdiff_t stride = f->sr_cur.p.stride[chroma];
45     const int sby = (y + (y ? 8 << ss_ver : 0)) >> (6 - ss_ver + f->seq_hdr->sb128);
46     const int have_tt = f->c->n_tc > 1;
47     const pixel *lpf = f->lf.lr_lpf_line[plane] +
48         have_tt * (sby * (4 << f->seq_hdr->sb128) - 4) * PXSTRIDE(stride) + x;
49 
50     // The first stripe of the frame is shorter by 8 luma pixel rows.
51     int stripe_h = imin((64 - 8 * !y) >> ss_ver, row_h - y);
52 
53     looprestorationfilter_fn lr_fn;
54     LooprestorationParams params;
55     if (lr->type == DAV1D_RESTORATION_WIENER) {
56         int16_t (*const filter)[8] = params.filter;
57         filter[0][0] = filter[0][6] = lr->filter_h[0];
58         filter[0][1] = filter[0][5] = lr->filter_h[1];
59         filter[0][2] = filter[0][4] = lr->filter_h[2];
60         filter[0][3] = -(filter[0][0] + filter[0][1] + filter[0][2]) * 2;
61 #if BITDEPTH != 8
62         /* For 8-bit SIMD it's beneficial to handle the +128 separately
63          * in order to avoid overflows. */
64         filter[0][3] += 128;
65 #endif
66 
67         filter[1][0] = filter[1][6] = lr->filter_v[0];
68         filter[1][1] = filter[1][5] = lr->filter_v[1];
69         filter[1][2] = filter[1][4] = lr->filter_v[2];
70         filter[1][3] = 128 - (filter[1][0] + filter[1][1] + filter[1][2]) * 2;
71 
72         lr_fn = dsp->lr.wiener[!(filter[0][0] | filter[1][0])];
73     } else {
74         assert(lr->type == DAV1D_RESTORATION_SGRPROJ);
75         const uint16_t *const sgr_params = dav1d_sgr_params[lr->sgr_idx];
76         params.sgr.s0 = sgr_params[0];
77         params.sgr.s1 = sgr_params[1];
78         params.sgr.w0 = lr->sgr_weights[0];
79         params.sgr.w1 = 128 - (lr->sgr_weights[0] + lr->sgr_weights[1]);
80 
81         lr_fn = dsp->lr.sgr[!!sgr_params[0] + !!sgr_params[1] * 2 - 1];
82     }
83 
84     while (y + stripe_h <= row_h) {
85         // Change the HAVE_BOTTOM bit in edges to (sby + 1 != f->sbh || y + stripe_h != row_h)
86         edges ^= (-(sby + 1 != f->sbh || y + stripe_h != row_h) ^ edges) & LR_HAVE_BOTTOM;
87         lr_fn(p, stride, left, lpf, unit_w, stripe_h, &params, edges HIGHBD_CALL_SUFFIX);
88 
89         left += stripe_h;
90         y += stripe_h;
91         p += stripe_h * PXSTRIDE(stride);
92         edges |= LR_HAVE_TOP;
93         stripe_h = imin(64 >> ss_ver, row_h - y);
94         if (stripe_h == 0) break;
95         lpf += 4 * PXSTRIDE(stride);
96     }
97 }
98 
backup4xU(pixel (* dst)[4],const pixel * src,const ptrdiff_t src_stride,int u)99 static void backup4xU(pixel (*dst)[4], const pixel *src, const ptrdiff_t src_stride,
100                       int u)
101 {
102     for (; u > 0; u--, dst++, src += PXSTRIDE(src_stride))
103         pixel_copy(dst, src, 4);
104 }
105 
lr_sbrow(const Dav1dFrameContext * const f,pixel * p,const int y,const int w,const int h,const int row_h,const int plane)106 static void lr_sbrow(const Dav1dFrameContext *const f, pixel *p, const int y,
107                      const int w, const int h, const int row_h, const int plane)
108 {
109     const int chroma = !!plane;
110     const int ss_ver = chroma & (f->sr_cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420);
111     const int ss_hor = chroma & (f->sr_cur.p.p.layout != DAV1D_PIXEL_LAYOUT_I444);
112     const ptrdiff_t p_stride = f->sr_cur.p.stride[chroma];
113 
114     const int unit_size_log2 = f->frame_hdr->restoration.unit_size[!!plane];
115     const int unit_size = 1 << unit_size_log2;
116     const int half_unit_size = unit_size >> 1;
117     const int max_unit_size = unit_size + half_unit_size;
118 
119     // Y coordinate of the sbrow (y is 8 luma pixel rows above row_y)
120     const int row_y = y + ((8 >> ss_ver) * !!y);
121 
122     // FIXME This is an ugly hack to lookup the proper AV1Filter unit for
123     // chroma planes. Question: For Multithreaded decoding, is it better
124     // to store the chroma LR information with collocated Luma information?
125     // In other words. For a chroma restoration unit locate at 128,128 and
126     // with a 4:2:0 chroma subsampling, do we store the filter information at
127     // the AV1Filter unit located at (128,128) or (256,256)
128     // TODO Support chroma subsampling.
129     const int shift_hor = 7 - ss_hor;
130 
131     /* maximum sbrow height is 128 + 8 rows offset */
132     ALIGN_STK_16(pixel, pre_lr_border, 2, [128 + 8][4]);
133     const Av1RestorationUnit *lr[2];
134 
135     enum LrEdgeFlags edges = (y > 0 ? LR_HAVE_TOP : 0) | LR_HAVE_RIGHT;
136 
137     int aligned_unit_pos = row_y & ~(unit_size - 1);
138     if (aligned_unit_pos && aligned_unit_pos + half_unit_size > h)
139         aligned_unit_pos -= unit_size;
140     aligned_unit_pos <<= ss_ver;
141     const int sb_idx = (aligned_unit_pos >> 7) * f->sr_sb128w;
142     const int unit_idx = ((aligned_unit_pos >> 6) & 1) << 1;
143     lr[0] = &f->lf.lr_mask[sb_idx].lr[plane][unit_idx];
144     int restore = lr[0]->type != DAV1D_RESTORATION_NONE;
145     int x = 0, bit = 0;
146     for (; x + max_unit_size <= w; p += unit_size, edges |= LR_HAVE_LEFT, bit ^= 1) {
147         const int next_x = x + unit_size;
148         const int next_u_idx = unit_idx + ((next_x >> (shift_hor - 1)) & 1);
149         lr[!bit] =
150             &f->lf.lr_mask[sb_idx + (next_x >> shift_hor)].lr[plane][next_u_idx];
151         const int restore_next = lr[!bit]->type != DAV1D_RESTORATION_NONE;
152         if (restore_next)
153             backup4xU(pre_lr_border[bit], p + unit_size - 4, p_stride, row_h - y);
154         if (restore)
155             lr_stripe(f, p, pre_lr_border[!bit], x, y, plane, unit_size, row_h,
156                       lr[bit], edges);
157         x = next_x;
158         restore = restore_next;
159     }
160     if (restore) {
161         edges &= ~LR_HAVE_RIGHT;
162         const int unit_w = w - x;
163         lr_stripe(f, p, pre_lr_border[!bit], x, y, plane, unit_w, row_h, lr[bit], edges);
164     }
165 }
166 
bytefn(dav1d_lr_sbrow)167 void bytefn(dav1d_lr_sbrow)(Dav1dFrameContext *const f, pixel *const dst[3],
168                             const int sby)
169 {
170     const int offset_y = 8 * !!sby;
171     const ptrdiff_t *const dst_stride = f->sr_cur.p.stride;
172     const int restore_planes = f->lf.restore_planes;
173     const int not_last = sby + 1 < f->sbh;
174 
175     if (restore_planes & LR_RESTORE_Y) {
176         const int h = f->sr_cur.p.p.h;
177         const int w = f->sr_cur.p.p.w;
178         const int next_row_y = (sby + 1) << (6 + f->seq_hdr->sb128);
179         const int row_h = imin(next_row_y - 8 * not_last, h);
180         const int y_stripe = (sby << (6 + f->seq_hdr->sb128)) - offset_y;
181         lr_sbrow(f, dst[0] - offset_y * PXSTRIDE(dst_stride[0]), y_stripe, w,
182                  h, row_h, 0);
183     }
184     if (restore_planes & (LR_RESTORE_U | LR_RESTORE_V)) {
185         const int ss_ver = f->sr_cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
186         const int ss_hor = f->sr_cur.p.p.layout != DAV1D_PIXEL_LAYOUT_I444;
187         const int h = (f->sr_cur.p.p.h + ss_ver) >> ss_ver;
188         const int w = (f->sr_cur.p.p.w + ss_hor) >> ss_hor;
189         const int next_row_y = (sby + 1) << ((6 - ss_ver) + f->seq_hdr->sb128);
190         const int row_h = imin(next_row_y - (8 >> ss_ver) * not_last, h);
191         const int offset_uv = offset_y >> ss_ver;
192         const int y_stripe = (sby << ((6 - ss_ver) + f->seq_hdr->sb128)) - offset_uv;
193         if (restore_planes & LR_RESTORE_U)
194             lr_sbrow(f, dst[1] - offset_uv * PXSTRIDE(dst_stride[1]), y_stripe,
195                      w, h, row_h, 1);
196 
197         if (restore_planes & LR_RESTORE_V)
198             lr_sbrow(f, dst[2] - offset_uv * PXSTRIDE(dst_stride[1]), y_stripe,
199                      w, h, row_h, 2);
200     }
201 }
202