Coverage Report

Created: 2026-09-13 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/looprestoration_tmpl.c
Line
Count
Source
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 <stdint.h>
31
#include <stdlib.h>
32
#include <string.h>
33
34
#include "common/attributes.h"
35
#include "common/bitdepth.h"
36
#include "common/intops.h"
37
38
#include "src/looprestoration.h"
39
#include "src/tables.h"
40
41
// 256 * 1.5 + 3 + 3 = 390
42
6.69M
#define REST_UNIT_STRIDE (390)
43
44
static void wiener_filter_h(uint16_t *dst, const pixel (*left)[4],
45
                            const pixel *src, const int16_t fh[8],
46
                            const int w, const enum LrEdgeFlags edges
47
                            HIGHBD_DECL_SUFFIX)
48
6.10M
{
49
6.10M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
50
6.10M
    const int round_bits_h = 3 + (bitdepth == 12) * 2;
51
6.10M
    const int rounding_off_h = 1 << (round_bits_h - 1);
52
6.10M
    const int clip_limit = 1 << (bitdepth + 1 + 7 - round_bits_h);
53
54
6.10M
    if (w < 6) {
55
        // For small widths, do the fully conditional loop with
56
        // conditions on each access.
57
4.72M
        for (int x = 0; x < w; x++) {
58
3.37M
            int sum = (1 << (bitdepth + 6));
59
3.37M
#if BITDEPTH == 8
60
3.37M
            sum += src[x] * 128;
61
3.37M
#endif
62
26.7M
            for (int i = 0; i < 7; i++) {
63
23.3M
                int idx = x + i - 3;
64
23.3M
                if (idx < 0) {
65
6.07M
                    if (!(edges & LR_HAVE_LEFT))
66
6.08M
                        sum += src[0] * fh[i];
67
18.4E
                    else if (left)
68
0
                        sum += left[0][4 + idx] * fh[i];
69
18.4E
                    else
70
18.4E
                        sum += src[idx] * fh[i];
71
17.2M
                } else if (idx >= w && !(edges & LR_HAVE_RIGHT)) {
72
5.98M
                    sum += src[w - 1] * fh[i];
73
5.98M
                } else
74
11.3M
                    sum += src[idx] * fh[i];
75
23.3M
            }
76
3.37M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
77
3.37M
            dst[x] = sum;
78
3.37M
        }
79
80
1.35M
        return;
81
1.35M
    }
82
83
    // For larger widths, do separate loops with less conditions; first
84
    // handle the start of the row.
85
4.75M
    int start = 3;
86
4.75M
    if (!(edges & LR_HAVE_LEFT)) {
87
        // If there's no left edge, pad using the leftmost pixel.
88
11.5M
        for (int x = 0; x < 3; x++) {
89
8.65M
            int sum = (1 << (bitdepth + 6));
90
8.65M
#if BITDEPTH == 8
91
8.65M
            sum += src[x] * 128;
92
8.65M
#endif
93
69.0M
            for (int i = 0; i < 7; i++) {
94
60.3M
                int idx = x + i - 3;
95
60.3M
                if (idx < 0)
96
17.2M
                    sum += src[0] * fh[i];
97
43.1M
                else
98
43.1M
                    sum += src[idx] * fh[i];
99
60.3M
            }
100
8.65M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
101
8.65M
            dst[x] = sum;
102
8.65M
        }
103
2.91M
    } else if (left) {
104
        // If we have the left edge and a separate left buffer, pad using that.
105
6.90M
        for (int x = 0; x < 3; x++) {
106
5.17M
            int sum = (1 << (bitdepth + 6));
107
5.17M
#if BITDEPTH == 8
108
5.17M
            sum += src[x] * 128;
109
5.17M
#endif
110
41.3M
            for (int i = 0; i < 7; i++) {
111
36.1M
                int idx = x + i - 3;
112
36.1M
                if (idx < 0)
113
10.3M
                    sum += left[0][4 + idx] * fh[i];
114
25.8M
                else
115
25.8M
                    sum += src[idx] * fh[i];
116
36.1M
            }
117
5.17M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
118
5.17M
            dst[x] = sum;
119
5.17M
        }
120
1.73M
    } else {
121
        // If we have the left edge, but no separate left buffer, we're in the
122
        // top/bottom area (lpf) with the left edge existing in the same
123
        // buffer; just do the regular loop from the start.
124
111k
        start = 0;
125
111k
    }
126
4.75M
    int end = w - 3;
127
4.75M
    if (edges & LR_HAVE_RIGHT)
128
1.77M
        end = w;
129
130
    // Do a condititon free loop for the bulk of the row.
131
372M
    for (int x = start; x < end; x++) {
132
368M
        int sum = (1 << (bitdepth + 6));
133
368M
#if BITDEPTH == 8
134
368M
        sum += src[x] * 128;
135
368M
#endif
136
2.92G
        for (int i = 0; i < 7; i++) {
137
2.55G
            int idx = x + i - 3;
138
2.55G
            sum += src[idx] * fh[i];
139
2.55G
        }
140
368M
        sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
141
368M
        dst[x] = sum;
142
368M
    }
143
144
    // If we need to, calculate the end of the row with a condition for
145
    // right edge padding.
146
13.5M
    for (int x = end; x < w; x++) {
147
8.82M
        int sum = (1 << (bitdepth + 6));
148
8.82M
#if BITDEPTH == 8
149
8.82M
        sum += src[x] * 128;
150
8.82M
#endif
151
70.4M
        for (int i = 0; i < 7; i++) {
152
61.6M
            int idx = x + i - 3;
153
61.6M
            if (idx >= w)
154
17.6M
                sum += src[w - 1] * fh[i];
155
44.0M
            else
156
44.0M
                sum += src[idx] * fh[i];
157
61.6M
        }
158
8.82M
        sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
159
8.82M
        dst[x] = sum;
160
8.82M
    }
161
4.75M
}
162
163
static void wiener_filter_v(pixel *p, uint16_t **ptrs, const int16_t fv[8],
164
                            const int w HIGHBD_DECL_SUFFIX)
165
296k
{
166
296k
    const int bitdepth = bitdepth_from_max(bitdepth_max);
167
168
296k
    const int round_bits_v = 11 - (bitdepth == 12) * 2;
169
296k
    const int rounding_off_v = 1 << (round_bits_v - 1);
170
296k
    const int round_offset = 1 << (bitdepth + (round_bits_v - 1));
171
172
15.8M
    for (int i = 0; i < w; i++) {
173
15.5M
        int sum = -round_offset;
174
175
        // Only filter using 6 input rows. The 7th row is assumed to be
176
        // identical to the last one.
177
        //
178
        // This function is assumed to only be called at the end, when doing
179
        // padding at the bottom.
180
109M
        for (int k = 0; k < 6; k++)
181
93.5M
            sum += ptrs[k][i] * fv[k];
182
15.5M
        sum += ptrs[5][i] * fv[6];
183
184
15.5M
        p[i] = iclip_pixel((sum + rounding_off_v) >> round_bits_v);
185
15.5M
    }
186
187
    // Shift the pointers, but only update the first 5; the 6th pointer is kept
188
    // as it was before (and the 7th is implicitly identical to the 6th).
189
1.77M
    for (int i = 0; i < 5; i++)
190
1.48M
        ptrs[i] = ptrs[i + 1];
191
296k
}
192
193
static void wiener_filter_hv(pixel *p, uint16_t **ptrs, const pixel (*left)[4],
194
                             const pixel *src, const int16_t filter[2][8],
195
                             const int w, const enum LrEdgeFlags edges
196
                             HIGHBD_DECL_SUFFIX)
197
5.41M
{
198
5.41M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
199
200
5.41M
    const int round_bits_v = 11 - (bitdepth == 12) * 2;
201
5.41M
    const int rounding_off_v = 1 << (round_bits_v - 1);
202
5.41M
    const int round_offset = 1 << (bitdepth + (round_bits_v - 1));
203
204
5.41M
    const int16_t *fh = filter[0];
205
5.41M
    const int16_t *fv = filter[1];
206
207
    // Do combined horziontal and vertical filtering; doing horizontal
208
    // filtering of one row, combined with vertical filtering of 6
209
    // preexisting rows and the newly filtered row.
210
211
    // For simplicity in the C implementation, just do a separate call
212
    // of the horizontal filter, into a temporary buffer.
213
5.41M
    uint16_t tmp[REST_UNIT_STRIDE];
214
5.41M
    wiener_filter_h(tmp, left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
215
216
363M
    for (int i = 0; i < w; i++) {
217
358M
        int sum = -round_offset;
218
219
        // Filter using the 6 stored preexisting rows, and the newly
220
        // filtered one in tmp[].
221
2.50G
        for (int k = 0; k < 6; k++)
222
2.14G
            sum += ptrs[k][i] * fv[k];
223
358M
        sum += tmp[i] * fv[6];
224
        // At this point, after having read all inputs at point [i], we
225
        // could overwrite [i] with the newly filtered data.
226
227
358M
        p[i] = iclip_pixel((sum + rounding_off_v) >> round_bits_v);
228
358M
    }
229
230
    // For simplicity in the C implementation, just memcpy the newly
231
    // filtered row into ptrs[6]. Normally, in steady state filtering,
232
    // this output row, ptrs[6], is equal to ptrs[0]. However at startup,
233
    // at the top of the filtered area, we may have ptrs[0] equal to ptrs[1],
234
    // so we can't assume we can write into ptrs[0] but we need to keep
235
    // a separate pointer for the next row to write into.
236
5.41M
    memcpy(ptrs[6], tmp, sizeof(uint16_t) * REST_UNIT_STRIDE);
237
238
    // Rotate the window of pointers. Shift the 6 pointers downwards one step.
239
37.6M
    for (int i = 0; i < 6; i++)
240
32.2M
        ptrs[i] = ptrs[i + 1];
241
    // The topmost pointer, ptrs[6], which isn't used as input, is set to
242
    // ptrs[0], which will be used as output for the next _hv call.
243
    // At the start of the filtering, the caller may set ptrs[6] to the
244
    // right next buffer to fill in, instead.
245
5.41M
    ptrs[6] = ptrs[0];
246
5.41M
}
247
248
// FIXME Could split into luma and chroma specific functions,
249
// (since first and last tops are always 0 for chroma)
250
static void wiener_c(pixel *p, const ptrdiff_t stride,
251
                     const pixel (*left)[4],
252
                     const pixel *lpf, const int w, int h,
253
                     const LooprestorationParams *const params,
254
                     const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
255
189k
{
256
    // Values stored between horizontal and vertical filtering don't
257
    // fit in a uint8_t.
258
189k
    uint16_t hor[6 * REST_UNIT_STRIDE];
259
189k
    uint16_t *ptrs[7], *rows[6];
260
1.32M
    for (int i = 0; i < 6; i++)
261
1.13M
        rows[i] = &hor[i * REST_UNIT_STRIDE];
262
189k
    const int16_t (*const filter)[8] = params->filter;
263
189k
    const int16_t *fh = params->filter[0];
264
189k
    const int16_t *fv = params->filter[1];
265
189k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
266
267
189k
    const pixel *src = p;
268
189k
    if (edges & LR_HAVE_TOP) {
269
100k
        ptrs[0] = rows[0];
270
100k
        ptrs[1] = rows[0];
271
100k
        ptrs[2] = rows[1];
272
100k
        ptrs[3] = rows[2];
273
100k
        ptrs[4] = rows[2];
274
100k
        ptrs[5] = rows[2];
275
276
100k
        wiener_filter_h(rows[0], NULL, lpf, fh, w, edges HIGHBD_TAIL_SUFFIX);
277
100k
        lpf += PXSTRIDE(stride);
278
100k
        wiener_filter_h(rows[1], NULL, lpf, fh, w, edges HIGHBD_TAIL_SUFFIX);
279
280
100k
        wiener_filter_h(rows[2], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
281
100k
        left++;
282
100k
        src += PXSTRIDE(stride);
283
284
100k
        if (--h <= 0)
285
1.23k
            goto v1;
286
287
99.5k
        ptrs[4] = ptrs[5] = rows[3];
288
99.5k
        wiener_filter_h(rows[3], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
289
99.5k
        left++;
290
99.5k
        src += PXSTRIDE(stride);
291
292
99.5k
        if (--h <= 0)
293
882
            goto v2;
294
295
98.6k
        ptrs[5] = rows[4];
296
98.6k
        wiener_filter_h(rows[4], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
297
98.6k
        left++;
298
98.6k
        src += PXSTRIDE(stride);
299
300
98.6k
        if (--h <= 0)
301
814
            goto v3;
302
98.6k
    } else {
303
89.1k
        ptrs[0] = rows[0];
304
89.1k
        ptrs[1] = rows[0];
305
89.1k
        ptrs[2] = rows[0];
306
89.1k
        ptrs[3] = rows[0];
307
89.1k
        ptrs[4] = rows[0];
308
89.1k
        ptrs[5] = rows[0];
309
310
89.1k
        wiener_filter_h(rows[0], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
311
89.1k
        left++;
312
89.1k
        src += PXSTRIDE(stride);
313
314
89.1k
        if (--h <= 0)
315
27.5k
            goto v1;
316
317
61.5k
        ptrs[4] = ptrs[5] = rows[1];
318
61.5k
        wiener_filter_h(rows[1], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
319
61.5k
        left++;
320
61.5k
        src += PXSTRIDE(stride);
321
322
61.5k
        if (--h <= 0)
323
10.9k
            goto v2;
324
325
50.6k
        ptrs[5] = rows[2];
326
50.6k
        wiener_filter_h(rows[2], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
327
50.6k
        left++;
328
50.6k
        src += PXSTRIDE(stride);
329
330
50.6k
        if (--h <= 0)
331
2.29k
            goto v3;
332
333
48.3k
        ptrs[6] = rows[3];
334
48.3k
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
335
48.3k
                         HIGHBD_TAIL_SUFFIX);
336
48.3k
        left++;
337
48.3k
        src += PXSTRIDE(stride);
338
48.3k
        p += PXSTRIDE(stride);
339
340
48.3k
        if (--h <= 0)
341
4.05k
            goto v3;
342
343
44.2k
        ptrs[6] = rows[4];
344
44.2k
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
345
44.2k
                         HIGHBD_TAIL_SUFFIX);
346
44.2k
        left++;
347
44.2k
        src += PXSTRIDE(stride);
348
44.2k
        p += PXSTRIDE(stride);
349
350
44.2k
        if (--h <= 0)
351
2.34k
            goto v3;
352
44.2k
    }
353
354
139k
    ptrs[6] = ptrs[5] + REST_UNIT_STRIDE;
355
5.11M
    do {
356
5.11M
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
357
5.11M
                         HIGHBD_TAIL_SUFFIX);
358
5.11M
        left++;
359
5.11M
        src += PXSTRIDE(stride);
360
5.11M
        p += PXSTRIDE(stride);
361
5.11M
    } while (--h > 0);
362
363
139k
    if (!(edges & LR_HAVE_BOTTOM))
364
37.9k
        goto v3;
365
366
101k
    wiener_filter_hv(p, ptrs, NULL, lpf_bottom, filter, w, edges
367
101k
                     HIGHBD_TAIL_SUFFIX);
368
101k
    lpf_bottom += PXSTRIDE(stride);
369
101k
    p += PXSTRIDE(stride);
370
371
101k
    wiener_filter_hv(p, ptrs, NULL, lpf_bottom, filter, w, edges
372
101k
                     HIGHBD_TAIL_SUFFIX);
373
101k
    p += PXSTRIDE(stride);
374
189k
v1:
375
189k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
376
377
189k
    return;
378
379
47.4k
v3:
380
47.4k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
381
47.4k
    p += PXSTRIDE(stride);
382
59.2k
v2:
383
59.2k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
384
59.2k
    p += PXSTRIDE(stride);
385
59.2k
    goto v1;
386
47.4k
}
387
388
// SGR
389
static NOINLINE void rotate(int32_t **sumsq_ptrs, coef **sum_ptrs, int n)
390
14.2M
{
391
14.2M
    int32_t *tmp32 = sumsq_ptrs[0];
392
14.2M
    coef *tmpc = sum_ptrs[0];
393
43.9M
    for (int i = 0; i < n - 1; i++) {
394
29.6M
        sumsq_ptrs[i] = sumsq_ptrs[i + 1];
395
29.6M
        sum_ptrs[i] = sum_ptrs[i + 1];
396
29.6M
    }
397
14.2M
    sumsq_ptrs[n - 1] = tmp32;
398
14.2M
    sum_ptrs[n - 1] = tmpc;
399
14.2M
}
400
401
static NOINLINE void rotate5_x2(int32_t **sumsq_ptrs, coef **sum_ptrs)
402
3.03M
{
403
3.03M
    int32_t *tmp32[2];
404
3.03M
    coef *tmpc[2];
405
9.08M
    for (int i = 0; i < 2; i++) {
406
6.05M
        tmp32[i] = sumsq_ptrs[i];
407
6.05M
        tmpc[i] = sum_ptrs[i];
408
6.05M
    }
409
12.1M
    for (int i = 0; i < 3; i++) {
410
9.08M
        sumsq_ptrs[i] = sumsq_ptrs[i + 2];
411
9.08M
        sum_ptrs[i] = sum_ptrs[i + 2];
412
9.08M
    }
413
9.08M
    for (int i = 0; i < 2; i++) {
414
6.05M
        sumsq_ptrs[3 + i] = tmp32[i];
415
6.05M
        sum_ptrs[3 + i] = tmpc[i];
416
6.05M
    }
417
3.03M
}
418
419
static NOINLINE void sgr_box3_row_h(int32_t *sumsq, coef *sum,
420
                                    const pixel (*left)[4],
421
                                    const pixel *src, const int w,
422
                                    const enum LrEdgeFlags edges)
423
5.72M
{
424
5.72M
    sumsq++;
425
5.72M
    sum++;
426
5.72M
    int a = edges & LR_HAVE_LEFT ? (left ? left[0][2] : src[-2]) : src[0];
427
5.72M
    int b = edges & LR_HAVE_LEFT ? (left ? left[0][3] : src[-1]) : src[0];
428
306M
    for (int x = -1; x < w + 1; x++) {
429
300M
        int c = (x + 1 < w || (edges & LR_HAVE_RIGHT)) ? src[x + 1] : src[w - 1];
430
300M
        sum[x] = a + b + c;
431
300M
        sumsq[x] = a * a + b * b + c * c;
432
300M
        a = b;
433
300M
        b = c;
434
300M
    }
435
5.72M
}
436
437
static NOINLINE void sgr_box5_row_h(int32_t *sumsq, coef *sum,
438
                                    const pixel (*left)[4],
439
                                    const pixel *src, const int w,
440
                                    const enum LrEdgeFlags edges)
441
6.11M
{
442
6.11M
    sumsq++;
443
6.11M
    sum++;
444
6.11M
    int a = edges & LR_HAVE_LEFT ? (left ? left[0][1] : src[-3]) : src[0];
445
6.11M
    int b = edges & LR_HAVE_LEFT ? (left ? left[0][2] : src[-2]) : src[0];
446
6.11M
    int c = edges & LR_HAVE_LEFT ? (left ? left[0][3] : src[-1]) : src[0];
447
6.11M
    int d = src[0];
448
347M
    for (int x = -1; x < w + 1; x++) {
449
341M
        int e = (x + 2 < w || (edges & LR_HAVE_RIGHT)) ? src[x + 2] : src[w - 1];
450
341M
        sum[x] = a + b + c + d + e;
451
341M
        sumsq[x] = a * a + b * b + c * c + d * d + e * e;
452
341M
        a = b;
453
341M
        b = c;
454
341M
        c = d;
455
341M
        d = e;
456
341M
    }
457
6.11M
}
458
459
static void sgr_box35_row_h(int32_t *sumsq3, coef *sum3,
460
                            int32_t *sumsq5, coef *sum5,
461
                            const pixel (*left)[4],
462
                            const pixel *src, const int w,
463
                            const enum LrEdgeFlags edges)
464
4.31M
{
465
4.31M
    sgr_box3_row_h(sumsq3, sum3, left, src, w, edges);
466
4.31M
    sgr_box5_row_h(sumsq5, sum5, left, src, w, edges);
467
4.31M
}
468
469
static NOINLINE void sgr_box3_row_v(int32_t **sumsq, coef **sum,
470
                                    int32_t *sumsq_out, coef *sum_out,
471
                                    const int w)
472
5.63M
{
473
303M
    for (int x = 0; x < w + 2; x++) {
474
298M
        int sq_a = sumsq[0][x];
475
298M
        int sq_b = sumsq[1][x];
476
298M
        int sq_c = sumsq[2][x];
477
298M
        int s_a = sum[0][x];
478
298M
        int s_b = sum[1][x];
479
298M
        int s_c = sum[2][x];
480
298M
        sumsq_out[x] = sq_a + sq_b + sq_c;
481
298M
        sum_out[x] = s_a + s_b + s_c;
482
298M
    }
483
5.63M
}
484
485
static NOINLINE void sgr_box5_row_v(int32_t **sumsq, coef **sum,
486
                                    int32_t *sumsq_out, coef *sum_out,
487
                                    const int w)
488
3.03M
{
489
172M
    for (int x = 0; x < w + 2; x++) {
490
169M
        int sq_a = sumsq[0][x];
491
169M
        int sq_b = sumsq[1][x];
492
169M
        int sq_c = sumsq[2][x];
493
169M
        int sq_d = sumsq[3][x];
494
169M
        int sq_e = sumsq[4][x];
495
169M
        int s_a = sum[0][x];
496
169M
        int s_b = sum[1][x];
497
169M
        int s_c = sum[2][x];
498
169M
        int s_d = sum[3][x];
499
169M
        int s_e = sum[4][x];
500
169M
        sumsq_out[x] = sq_a + sq_b + sq_c + sq_d + sq_e;
501
169M
        sum_out[x] = s_a + s_b + s_c + s_d + s_e;
502
169M
    }
503
3.03M
}
504
505
static NOINLINE void sgr_calc_row_ab(int32_t *AA, coef *BB, int w, int s,
506
                                     int bitdepth_max, int n, int sgr_one_by_x)
507
8.65M
{
508
8.65M
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
509
453M
    for (int i = 0; i < w + 2; i++) {
510
444M
        const int a =
511
444M
            (AA[i] + ((1 << (2 * bitdepth_min_8)) >> 1)) >> (2 * bitdepth_min_8);
512
444M
        const int b =
513
444M
            (BB[i] + ((1 << bitdepth_min_8) >> 1)) >> bitdepth_min_8;
514
515
444M
        const unsigned p = imax(a * n - b * b, 0);
516
444M
        const unsigned z = (p * s + (1 << 19)) >> 20;
517
444M
        const unsigned x = dav1d_sgr_x_by_x[umin(z, 255)];
518
519
        // This is where we invert A and B, so that B is of size coef.
520
444M
        AA[i] = (x * BB[i] * sgr_one_by_x + (1 << 11)) >> 12;
521
444M
        BB[i] = x;
522
444M
    }
523
8.65M
}
524
525
static void sgr_box3_vert(int32_t **sumsq, coef **sum,
526
                          int32_t *sumsq_out, coef *sum_out,
527
                          const int w, const int s, const int bitdepth_max)
528
5.63M
{
529
5.63M
    sgr_box3_row_v(sumsq, sum, sumsq_out, sum_out, w);
530
5.63M
    sgr_calc_row_ab(sumsq_out, sum_out, w, s, bitdepth_max, 9, 455);
531
5.63M
    rotate(sumsq, sum, 3);
532
5.63M
}
533
534
static void sgr_box5_vert(int32_t **sumsq, coef **sum,
535
                          int32_t *sumsq_out, coef *sum_out,
536
                          const int w, const int s, const int bitdepth_max)
537
3.03M
{
538
3.03M
    sgr_box5_row_v(sumsq, sum, sumsq_out, sum_out, w);
539
3.03M
    sgr_calc_row_ab(sumsq_out, sum_out, w, s, bitdepth_max, 25, 164);
540
3.03M
    rotate5_x2(sumsq, sum);
541
3.03M
}
542
543
static void sgr_box3_hv(int32_t **sumsq, coef **sum,
544
                        int32_t *AA, coef *BB,
545
                        const pixel (*left)[4],
546
                        const pixel *src, const int w,
547
                        const int s,
548
                        const enum LrEdgeFlags edges,
549
                        const int bitdepth_max)
550
1.35M
{
551
1.35M
    sgr_box3_row_h(sumsq[2], sum[2], left, src, w, edges);
552
1.35M
    sgr_box3_vert(sumsq, sum, AA, BB, w, s, bitdepth_max);
553
1.35M
}
554
555
static NOINLINE void sgr_finish_filter_row1(coef *tmp,
556
                                            const pixel *src,
557
                                            int32_t **A_ptrs, coef **B_ptrs,
558
                                            const int w)
559
5.35M
{
560
5.35M
#define EIGHT_NEIGHBORS(P, i)\
561
540M
    ((P[1][i] + P[1][i - 1] + P[1][i + 1] + P[0][i] + P[2][i]) * 4 + \
562
540M
     (P[0][i - 1] + P[2][i - 1] +                           \
563
540M
      P[0][i + 1] + P[2][i + 1]) * 3)
564
275M
    for (int i = 0; i < w; i++) {
565
270M
        const int a = EIGHT_NEIGHBORS(B_ptrs, i + 1);
566
270M
        const int b = EIGHT_NEIGHBORS(A_ptrs, i + 1);
567
270M
        tmp[i] = (b - a * src[i] + (1 << 8)) >> 9;
568
270M
    }
569
5.35M
#undef EIGHT_NEIGHBORS
570
5.35M
}
571
572
13.7M
#define FILTER_OUT_STRIDE (384)
573
574
static NOINLINE void sgr_finish_filter2(coef *tmp,
575
                                        const pixel *src,
576
                                        const ptrdiff_t src_stride,
577
                                        int32_t **A_ptrs, coef **B_ptrs,
578
                                        const int w, const int h)
579
2.88M
{
580
2.88M
#define SIX_NEIGHBORS(P, i)\
581
309M
    ((P[0][i]     + P[1][i]) * 6 +   \
582
309M
     (P[0][i - 1] + P[1][i - 1] +    \
583
309M
      P[0][i + 1] + P[1][i + 1]) * 5)
584
157M
    for (int i = 0; i < w; i++) {
585
154M
        const int a = SIX_NEIGHBORS(B_ptrs, i + 1);
586
154M
        const int b = SIX_NEIGHBORS(A_ptrs, i + 1);
587
154M
        tmp[i] = (b - a * src[i] + (1 << 8)) >> 9;
588
154M
    }
589
2.88M
    if (h <= 1)
590
30.9k
        return;
591
2.85M
    tmp += FILTER_OUT_STRIDE;
592
2.85M
    src += PXSTRIDE(src_stride);
593
2.85M
    const int32_t *A = &A_ptrs[1][1];
594
2.85M
    const coef *B = &B_ptrs[1][1];
595
153M
    for (int i = 0; i < w; i++) {
596
150M
        const int a = B[i] * 6 + (B[i - 1] + B[i + 1]) * 5;
597
150M
        const int b = A[i] * 6 + (A[i - 1] + A[i + 1]) * 5;
598
150M
        tmp[i] = (b - a * src[i] + (1 << 7)) >> 8;
599
150M
    }
600
2.85M
#undef SIX_NEIGHBORS
601
2.85M
}
602
603
static NOINLINE void sgr_weighted_row1(pixel *dst, const coef *t1,
604
                                       const int w, const int w1 HIGHBD_DECL_SUFFIX)
605
3.01M
{
606
152M
    for (int i = 0; i < w; i++) {
607
149M
        const int v = w1 * t1[i];
608
149M
        dst[i] = iclip_pixel(dst[i] + ((v + (1 << 10)) >> 11));
609
149M
    }
610
3.01M
}
611
612
static NOINLINE void sgr_weighted2(pixel *dst, const ptrdiff_t dst_stride,
613
                                   const coef *t1, const coef *t2,
614
                                   const int w, const int h,
615
                                   const int w0, const int w1 HIGHBD_DECL_SUFFIX)
616
2.03M
{
617
6.07M
    for (int j = 0; j < h; j++) {
618
204M
        for (int i = 0; i < w; i++) {
619
200M
            const int v = w0 * t1[i] + w1 * t2[i];
620
200M
            dst[i] = iclip_pixel(dst[i] + ((v + (1 << 10)) >> 11));
621
200M
        }
622
4.03M
        dst += PXSTRIDE(dst_stride);
623
4.03M
        t1 += FILTER_OUT_STRIDE;
624
4.03M
        t2 += FILTER_OUT_STRIDE;
625
4.03M
    }
626
2.03M
}
627
628
static NOINLINE void sgr_finish1(pixel **dst, const ptrdiff_t stride,
629
                                 int32_t **A_ptrs, coef **B_ptrs, const int w,
630
                                 const int w1 HIGHBD_DECL_SUFFIX)
631
1.32M
{
632
    // Only one single row, no stride needed
633
1.32M
    ALIGN_STK_16(coef, tmp, 384,);
634
635
1.32M
    sgr_finish_filter_row1(tmp, *dst, A_ptrs, B_ptrs, w);
636
1.32M
    sgr_weighted_row1(*dst, tmp, w, w1 HIGHBD_TAIL_SUFFIX);
637
1.32M
    *dst += PXSTRIDE(stride);
638
1.32M
    rotate(A_ptrs, B_ptrs, 3);
639
1.32M
}
640
641
static NOINLINE void sgr_finish2(pixel **dst, const ptrdiff_t stride,
642
                                 int32_t **A_ptrs, coef **B_ptrs,
643
                                 const int w, const int h, const int w1
644
                                 HIGHBD_DECL_SUFFIX)
645
852k
{
646
852k
    ALIGN_STK_16(coef, tmp, 2*FILTER_OUT_STRIDE,);
647
648
852k
    sgr_finish_filter2(tmp, *dst, stride, A_ptrs, B_ptrs, w, h);
649
852k
    sgr_weighted_row1(*dst, tmp, w, w1 HIGHBD_TAIL_SUFFIX);
650
852k
    *dst += PXSTRIDE(stride);
651
852k
    if (h > 1) {
652
843k
        sgr_weighted_row1(*dst, tmp + FILTER_OUT_STRIDE, w, w1 HIGHBD_TAIL_SUFFIX);
653
843k
        *dst += PXSTRIDE(stride);
654
843k
    }
655
852k
    rotate(A_ptrs, B_ptrs, 2);
656
852k
}
657
658
static NOINLINE void sgr_finish_mix(pixel **dst, const ptrdiff_t stride,
659
                                    int32_t **A5_ptrs, coef **B5_ptrs,
660
                                    int32_t **A3_ptrs, coef **B3_ptrs,
661
                                    const int w, const int h,
662
                                    const int w0, const int w1 HIGHBD_DECL_SUFFIX)
663
2.03M
{
664
2.03M
    ALIGN_STK_16(coef, tmp5, 2*FILTER_OUT_STRIDE,);
665
2.03M
    ALIGN_STK_16(coef, tmp3, 2*FILTER_OUT_STRIDE,);
666
667
2.03M
    sgr_finish_filter2(tmp5, *dst, stride, A5_ptrs, B5_ptrs, w, h);
668
2.03M
    sgr_finish_filter_row1(tmp3, *dst, A3_ptrs, B3_ptrs, w);
669
2.03M
    if (h > 1)
670
2.01M
        sgr_finish_filter_row1(tmp3 + FILTER_OUT_STRIDE, *dst + PXSTRIDE(stride),
671
2.01M
                               &A3_ptrs[1], &B3_ptrs[1], w);
672
2.03M
    sgr_weighted2(*dst, stride, tmp5, tmp3, w, h, w0, w1 HIGHBD_TAIL_SUFFIX);
673
2.03M
    *dst += h*PXSTRIDE(stride);
674
2.03M
    rotate(A5_ptrs, B5_ptrs, 2);
675
2.03M
    rotate(A3_ptrs, B3_ptrs, 4);
676
2.03M
}
677
678
679
static void sgr_3x3_c(pixel *dst, const ptrdiff_t stride,
680
                      const pixel (*left)[4], const pixel *lpf,
681
                      const int w, int h,
682
                      const LooprestorationParams *const params,
683
                      const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
684
35.4k
{
685
3.99M
#define BUF_STRIDE (384 + 16)
686
35.4k
    ALIGN_STK_16(int32_t, sumsq_buf, BUF_STRIDE * 3 + 16,);
687
35.4k
    ALIGN_STK_16(coef, sum_buf, BUF_STRIDE * 3 + 16,);
688
35.4k
    int32_t *sumsq_ptrs[3], *sumsq_rows[3];
689
35.4k
    coef *sum_ptrs[3], *sum_rows[3];
690
141k
    for (int i = 0; i < 3; i++) {
691
106k
        sumsq_rows[i] = &sumsq_buf[i * BUF_STRIDE];
692
106k
        sum_rows[i] = &sum_buf[i * BUF_STRIDE];
693
106k
    }
694
695
35.4k
    ALIGN_STK_16(int32_t, A_buf, BUF_STRIDE * 3 + 16,);
696
35.4k
    ALIGN_STK_16(coef, B_buf, BUF_STRIDE * 3 + 16,);
697
35.4k
    int32_t *A_ptrs[3];
698
35.4k
    coef *B_ptrs[3];
699
141k
    for (int i = 0; i < 3; i++) {
700
106k
        A_ptrs[i] = &A_buf[i * BUF_STRIDE];
701
106k
        B_ptrs[i] = &B_buf[i * BUF_STRIDE];
702
106k
    }
703
35.4k
    const pixel *src = dst;
704
35.4k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
705
706
35.4k
    if (edges & LR_HAVE_TOP) {
707
24.4k
        sumsq_ptrs[0] = sumsq_rows[0];
708
24.4k
        sumsq_ptrs[1] = sumsq_rows[1];
709
24.4k
        sumsq_ptrs[2] = sumsq_rows[2];
710
24.4k
        sum_ptrs[0] = sum_rows[0];
711
24.4k
        sum_ptrs[1] = sum_rows[1];
712
24.4k
        sum_ptrs[2] = sum_rows[2];
713
714
24.4k
        sgr_box3_row_h(sumsq_rows[0], sum_rows[0], NULL, lpf, w, edges);
715
24.4k
        lpf += PXSTRIDE(stride);
716
24.4k
        sgr_box3_row_h(sumsq_rows[1], sum_rows[1], NULL, lpf, w, edges);
717
718
24.4k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
719
24.4k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
720
24.4k
        left++;
721
24.4k
        src += PXSTRIDE(stride);
722
24.4k
        rotate(A_ptrs, B_ptrs, 3);
723
724
24.4k
        if (--h <= 0)
725
708
            goto vert_1;
726
727
23.7k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
728
23.7k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
729
23.7k
        left++;
730
23.7k
        src += PXSTRIDE(stride);
731
23.7k
        rotate(A_ptrs, B_ptrs, 3);
732
733
23.7k
        if (--h <= 0)
734
615
            goto vert_2;
735
23.7k
    } else {
736
10.9k
        sumsq_ptrs[0] = sumsq_rows[0];
737
10.9k
        sumsq_ptrs[1] = sumsq_rows[0];
738
10.9k
        sumsq_ptrs[2] = sumsq_rows[0];
739
10.9k
        sum_ptrs[0] = sum_rows[0];
740
10.9k
        sum_ptrs[1] = sum_rows[0];
741
10.9k
        sum_ptrs[2] = sum_rows[0];
742
743
10.9k
        sgr_box3_row_h(sumsq_rows[0], sum_rows[0], left, src, w, edges);
744
10.9k
        left++;
745
10.9k
        src += PXSTRIDE(stride);
746
747
10.9k
        sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
748
10.9k
                      w, params->sgr.s1, BITDEPTH_MAX);
749
10.9k
        rotate(A_ptrs, B_ptrs, 3);
750
751
10.9k
        if (--h <= 0)
752
1.83k
            goto vert_1;
753
754
9.15k
        sumsq_ptrs[2] = sumsq_rows[1];
755
9.15k
        sum_ptrs[2] = sum_rows[1];
756
757
9.15k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
758
9.15k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
759
9.15k
        left++;
760
9.15k
        src += PXSTRIDE(stride);
761
9.15k
        rotate(A_ptrs, B_ptrs, 3);
762
763
9.15k
        if (--h <= 0)
764
1.48k
            goto vert_2;
765
766
7.67k
        sumsq_ptrs[2] = sumsq_rows[2];
767
7.67k
        sum_ptrs[2] = sum_rows[2];
768
7.67k
    }
769
770
1.25M
    do {
771
1.25M
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
772
1.25M
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
773
1.25M
        left++;
774
1.25M
        src += PXSTRIDE(stride);
775
776
1.25M
        sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
777
1.25M
                    w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
778
1.25M
    } while (--h > 0);
779
780
30.8k
    if (!(edges & LR_HAVE_BOTTOM))
781
8.78k
        goto vert_2;
782
783
22.0k
    sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
784
22.0k
                NULL, lpf_bottom, w, params->sgr.s1, edges, BITDEPTH_MAX);
785
22.0k
    lpf_bottom += PXSTRIDE(stride);
786
787
22.0k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
788
22.0k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
789
790
22.0k
    sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
791
22.0k
                NULL, lpf_bottom, w, params->sgr.s1, edges, BITDEPTH_MAX);
792
793
22.0k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
794
22.0k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
795
22.0k
    return;
796
797
10.8k
vert_2:
798
10.8k
    sumsq_ptrs[2] = sumsq_ptrs[1];
799
10.8k
    sum_ptrs[2] = sum_ptrs[1];
800
10.8k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
801
10.8k
                  w, params->sgr.s1, BITDEPTH_MAX);
802
803
10.8k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
804
10.8k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
805
806
13.4k
output_1:
807
13.4k
    sumsq_ptrs[2] = sumsq_ptrs[1];
808
13.4k
    sum_ptrs[2] = sum_ptrs[1];
809
13.4k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
810
13.4k
                  w, params->sgr.s1, BITDEPTH_MAX);
811
812
13.4k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
813
13.4k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
814
13.4k
    return;
815
816
2.53k
vert_1:
817
2.53k
    sumsq_ptrs[2] = sumsq_ptrs[1];
818
2.53k
    sum_ptrs[2] = sum_ptrs[1];
819
2.53k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
820
2.53k
                  w, params->sgr.s1, BITDEPTH_MAX);
821
2.53k
    rotate(A_ptrs, B_ptrs, 3);
822
2.53k
    goto output_1;
823
10.8k
}
824
825
static void sgr_5x5_c(pixel *dst, const ptrdiff_t stride,
826
                      const pixel (*left)[4], const pixel *lpf,
827
                      const int w, int h,
828
                      const LooprestorationParams *const params,
829
                      const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
830
43.6k
{
831
43.6k
    ALIGN_STK_16(int32_t, sumsq_buf, BUF_STRIDE * 5 + 16,);
832
43.6k
    ALIGN_STK_16(coef, sum_buf, BUF_STRIDE * 5 + 16,);
833
43.6k
    int32_t *sumsq_ptrs[5], *sumsq_rows[5];
834
43.6k
    coef *sum_ptrs[5], *sum_rows[5];
835
261k
    for (int i = 0; i < 5; i++) {
836
218k
        sumsq_rows[i] = &sumsq_buf[i * BUF_STRIDE];
837
218k
        sum_rows[i] = &sum_buf[i * BUF_STRIDE];
838
218k
    }
839
840
43.6k
    ALIGN_STK_16(int32_t, A_buf, BUF_STRIDE * 2 + 16,);
841
43.6k
    ALIGN_STK_16(coef, B_buf, BUF_STRIDE * 2 + 16,);
842
43.6k
    int32_t *A_ptrs[2];
843
43.6k
    coef *B_ptrs[2];
844
130k
    for (int i = 0; i < 2; i++) {
845
87.3k
        A_ptrs[i] = &A_buf[i * BUF_STRIDE];
846
87.3k
        B_ptrs[i] = &B_buf[i * BUF_STRIDE];
847
87.3k
    }
848
43.6k
    const pixel *src = dst;
849
43.6k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
850
851
43.6k
    if (edges & LR_HAVE_TOP) {
852
28.1k
        sumsq_ptrs[0] = sumsq_rows[0];
853
28.1k
        sumsq_ptrs[1] = sumsq_rows[0];
854
28.1k
        sumsq_ptrs[2] = sumsq_rows[1];
855
28.1k
        sumsq_ptrs[3] = sumsq_rows[2];
856
28.1k
        sumsq_ptrs[4] = sumsq_rows[3];
857
28.1k
        sum_ptrs[0] = sum_rows[0];
858
28.1k
        sum_ptrs[1] = sum_rows[0];
859
28.1k
        sum_ptrs[2] = sum_rows[1];
860
28.1k
        sum_ptrs[3] = sum_rows[2];
861
28.1k
        sum_ptrs[4] = sum_rows[3];
862
863
28.1k
        sgr_box5_row_h(sumsq_rows[0], sum_rows[0], NULL, lpf, w, edges);
864
28.1k
        lpf += PXSTRIDE(stride);
865
28.1k
        sgr_box5_row_h(sumsq_rows[1], sum_rows[1], NULL, lpf, w, edges);
866
867
28.1k
        sgr_box5_row_h(sumsq_rows[2], sum_rows[2], left, src, w, edges);
868
28.1k
        left++;
869
28.1k
        src += PXSTRIDE(stride);
870
871
28.1k
        if (--h <= 0)
872
628
            goto vert_1;
873
874
27.4k
        sgr_box5_row_h(sumsq_rows[3], sum_rows[3], left, src, w, edges);
875
27.4k
        left++;
876
27.4k
        src += PXSTRIDE(stride);
877
27.4k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
878
27.4k
                      w, params->sgr.s0, BITDEPTH_MAX);
879
27.4k
        rotate(A_ptrs, B_ptrs, 2);
880
881
27.4k
        if (--h <= 0)
882
742
            goto vert_2;
883
884
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
885
        // one of them to point at the previously unused rows[4].
886
26.7k
        sumsq_ptrs[3] = sumsq_rows[4];
887
26.7k
        sum_ptrs[3] = sum_rows[4];
888
26.7k
    } else {
889
15.5k
        sumsq_ptrs[0] = sumsq_rows[0];
890
15.5k
        sumsq_ptrs[1] = sumsq_rows[0];
891
15.5k
        sumsq_ptrs[2] = sumsq_rows[0];
892
15.5k
        sumsq_ptrs[3] = sumsq_rows[0];
893
15.5k
        sumsq_ptrs[4] = sumsq_rows[0];
894
15.5k
        sum_ptrs[0] = sum_rows[0];
895
15.5k
        sum_ptrs[1] = sum_rows[0];
896
15.5k
        sum_ptrs[2] = sum_rows[0];
897
15.5k
        sum_ptrs[3] = sum_rows[0];
898
15.5k
        sum_ptrs[4] = sum_rows[0];
899
900
15.5k
        sgr_box5_row_h(sumsq_rows[0], sum_rows[0], left, src, w, edges);
901
15.5k
        left++;
902
15.5k
        src += PXSTRIDE(stride);
903
904
15.5k
        if (--h <= 0)
905
1.56k
            goto vert_1;
906
907
13.9k
        sumsq_ptrs[4] = sumsq_rows[1];
908
13.9k
        sum_ptrs[4] = sum_rows[1];
909
910
13.9k
        sgr_box5_row_h(sumsq_rows[1], sum_rows[1], left, src, w, edges);
911
13.9k
        left++;
912
13.9k
        src += PXSTRIDE(stride);
913
914
13.9k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
915
13.9k
                      w, params->sgr.s0, BITDEPTH_MAX);
916
13.9k
        rotate(A_ptrs, B_ptrs, 2);
917
918
13.9k
        if (--h <= 0)
919
1.94k
            goto vert_2;
920
921
12.0k
        sumsq_ptrs[3] = sumsq_rows[2];
922
12.0k
        sumsq_ptrs[4] = sumsq_rows[3];
923
12.0k
        sum_ptrs[3] = sum_rows[2];
924
12.0k
        sum_ptrs[4] = sum_rows[3];
925
926
12.0k
        sgr_box5_row_h(sumsq_rows[2], sum_rows[2], left, src, w, edges);
927
12.0k
        left++;
928
12.0k
        src += PXSTRIDE(stride);
929
930
12.0k
        if (--h <= 0)
931
1.15k
            goto odd;
932
933
10.8k
        sgr_box5_row_h(sumsq_rows[3], sum_rows[3], left, src, w, edges);
934
10.8k
        left++;
935
10.8k
        src += PXSTRIDE(stride);
936
937
10.8k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
938
10.8k
                      w, params->sgr.s0, BITDEPTH_MAX);
939
10.8k
        sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
940
10.8k
                    w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
941
942
10.8k
        if (--h <= 0)
943
1.12k
            goto vert_2;
944
945
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
946
        // one of them to point at the previously unused rows[4].
947
9.76k
        sumsq_ptrs[3] = sumsq_rows[4];
948
9.76k
        sum_ptrs[3] = sum_rows[4];
949
9.76k
    }
950
951
795k
    do {
952
795k
        sgr_box5_row_h(sumsq_ptrs[3], sum_ptrs[3], left, src, w, edges);
953
795k
        left++;
954
795k
        src += PXSTRIDE(stride);
955
956
795k
        if (--h <= 0)
957
4.90k
            goto odd;
958
959
790k
        sgr_box5_row_h(sumsq_ptrs[4], sum_ptrs[4], left, src, w, edges);
960
790k
        left++;
961
790k
        src += PXSTRIDE(stride);
962
963
790k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
964
790k
                      w, params->sgr.s0, BITDEPTH_MAX);
965
790k
        sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
966
790k
                    w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
967
790k
    } while (--h > 0);
968
969
31.6k
    if (!(edges & LR_HAVE_BOTTOM))
970
4.00k
        goto vert_2;
971
972
27.6k
    sgr_box5_row_h(sumsq_ptrs[3], sum_ptrs[3], NULL, lpf_bottom, w, edges);
973
27.6k
    lpf_bottom += PXSTRIDE(stride);
974
27.6k
    sgr_box5_row_h(sumsq_ptrs[4], sum_ptrs[4], NULL, lpf_bottom, w, edges);
975
976
35.4k
output_2:
977
35.4k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
978
35.4k
                  w, params->sgr.s0, BITDEPTH_MAX);
979
35.4k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
980
35.4k
                w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
981
35.4k
    return;
982
983
7.80k
vert_2:
984
    // Duplicate the last row twice more
985
7.80k
    sumsq_ptrs[3] = sumsq_ptrs[2];
986
7.80k
    sumsq_ptrs[4] = sumsq_ptrs[2];
987
7.80k
    sum_ptrs[3] = sum_ptrs[2];
988
7.80k
    sum_ptrs[4] = sum_ptrs[2];
989
7.80k
    goto output_2;
990
991
6.06k
odd:
992
    // Copy the last row as padding once
993
6.06k
    sumsq_ptrs[4] = sumsq_ptrs[3];
994
6.06k
    sum_ptrs[4] = sum_ptrs[3];
995
996
6.06k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
997
6.06k
                  w, params->sgr.s0, BITDEPTH_MAX);
998
6.06k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
999
6.06k
                w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
1000
1001
8.25k
output_1:
1002
    // Duplicate the last row twice more
1003
8.25k
    sumsq_ptrs[3] = sumsq_ptrs[2];
1004
8.25k
    sumsq_ptrs[4] = sumsq_ptrs[2];
1005
8.25k
    sum_ptrs[3] = sum_ptrs[2];
1006
8.25k
    sum_ptrs[4] = sum_ptrs[2];
1007
1008
8.25k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
1009
8.25k
                  w, params->sgr.s0, BITDEPTH_MAX);
1010
    // Output only one row
1011
8.25k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
1012
8.25k
                w, 1, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
1013
8.25k
    return;
1014
1015
2.18k
vert_1:
1016
    // Copy the last row as padding once
1017
2.18k
    sumsq_ptrs[4] = sumsq_ptrs[3];
1018
2.18k
    sum_ptrs[4] = sum_ptrs[3];
1019
1020
2.18k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
1021
2.18k
                  w, params->sgr.s0, BITDEPTH_MAX);
1022
2.18k
    rotate(A_ptrs, B_ptrs, 2);
1023
1024
2.18k
    goto output_1;
1025
6.06k
}
1026
1027
static void sgr_mix_c(pixel *dst, const ptrdiff_t stride,
1028
                      const pixel (*left)[4], const pixel *lpf,
1029
                      const int w, int h,
1030
                      const LooprestorationParams *const params,
1031
                      const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
1032
105k
{
1033
105k
    ALIGN_STK_16(int32_t, sumsq5_buf, BUF_STRIDE * 5 + 16,);
1034
105k
    ALIGN_STK_16(coef, sum5_buf, BUF_STRIDE * 5 + 16,);
1035
105k
    int32_t *sumsq5_ptrs[5], *sumsq5_rows[5];
1036
105k
    coef *sum5_ptrs[5], *sum5_rows[5];
1037
633k
    for (int i = 0; i < 5; i++) {
1038
527k
        sumsq5_rows[i] = &sumsq5_buf[i * BUF_STRIDE];
1039
527k
        sum5_rows[i] = &sum5_buf[i * BUF_STRIDE];
1040
527k
    }
1041
105k
    ALIGN_STK_16(int32_t, sumsq3_buf, BUF_STRIDE * 3 + 16,);
1042
105k
    ALIGN_STK_16(coef, sum3_buf, BUF_STRIDE * 3 + 16,);
1043
105k
    int32_t *sumsq3_ptrs[3], *sumsq3_rows[3];
1044
105k
    coef *sum3_ptrs[3], *sum3_rows[3];
1045
422k
    for (int i = 0; i < 3; i++) {
1046
316k
        sumsq3_rows[i] = &sumsq3_buf[i * BUF_STRIDE];
1047
316k
        sum3_rows[i] = &sum3_buf[i * BUF_STRIDE];
1048
316k
    }
1049
1050
105k
    ALIGN_STK_16(int32_t, A5_buf, BUF_STRIDE * 2 + 16,);
1051
105k
    ALIGN_STK_16(coef, B5_buf, BUF_STRIDE * 2 + 16,);
1052
105k
    int32_t *A5_ptrs[2];
1053
105k
    coef *B5_ptrs[2];
1054
316k
    for (int i = 0; i < 2; i++) {
1055
211k
        A5_ptrs[i] = &A5_buf[i * BUF_STRIDE];
1056
211k
        B5_ptrs[i] = &B5_buf[i * BUF_STRIDE];
1057
211k
    }
1058
105k
    ALIGN_STK_16(int32_t, A3_buf, BUF_STRIDE * 4 + 16,);
1059
105k
    ALIGN_STK_16(coef, B3_buf, BUF_STRIDE * 4 + 16,);
1060
105k
    int32_t *A3_ptrs[4];
1061
105k
    coef *B3_ptrs[4];
1062
527k
    for (int i = 0; i < 4; i++) {
1063
422k
        A3_ptrs[i] = &A3_buf[i * BUF_STRIDE];
1064
422k
        B3_ptrs[i] = &B3_buf[i * BUF_STRIDE];
1065
422k
    }
1066
105k
    const pixel *src = dst;
1067
105k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
1068
1069
105k
    if (edges & LR_HAVE_TOP) {
1070
73.6k
        sumsq5_ptrs[0] = sumsq5_rows[0];
1071
73.6k
        sumsq5_ptrs[1] = sumsq5_rows[0];
1072
73.6k
        sumsq5_ptrs[2] = sumsq5_rows[1];
1073
73.6k
        sumsq5_ptrs[3] = sumsq5_rows[2];
1074
73.6k
        sumsq5_ptrs[4] = sumsq5_rows[3];
1075
73.6k
        sum5_ptrs[0] = sum5_rows[0];
1076
73.6k
        sum5_ptrs[1] = sum5_rows[0];
1077
73.6k
        sum5_ptrs[2] = sum5_rows[1];
1078
73.6k
        sum5_ptrs[3] = sum5_rows[2];
1079
73.6k
        sum5_ptrs[4] = sum5_rows[3];
1080
1081
73.6k
        sumsq3_ptrs[0] = sumsq3_rows[0];
1082
73.6k
        sumsq3_ptrs[1] = sumsq3_rows[1];
1083
73.6k
        sumsq3_ptrs[2] = sumsq3_rows[2];
1084
73.6k
        sum3_ptrs[0] = sum3_rows[0];
1085
73.6k
        sum3_ptrs[1] = sum3_rows[1];
1086
73.6k
        sum3_ptrs[2] = sum3_rows[2];
1087
1088
73.6k
        sgr_box35_row_h(sumsq3_rows[0], sum3_rows[0],
1089
73.6k
                        sumsq5_rows[0], sum5_rows[0],
1090
73.6k
                        NULL, lpf, w, edges);
1091
73.6k
        lpf += PXSTRIDE(stride);
1092
73.6k
        sgr_box35_row_h(sumsq3_rows[1], sum3_rows[1],
1093
73.6k
                        sumsq5_rows[1], sum5_rows[1],
1094
73.6k
                        NULL, lpf, w, edges);
1095
1096
73.6k
        sgr_box35_row_h(sumsq3_rows[2], sum3_rows[2],
1097
73.6k
                        sumsq5_rows[2], sum5_rows[2],
1098
73.6k
                        left, src, w, edges);
1099
73.6k
        left++;
1100
73.6k
        src += PXSTRIDE(stride);
1101
1102
73.6k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1103
73.6k
                      w, params->sgr.s1, BITDEPTH_MAX);
1104
73.6k
        rotate(A3_ptrs, B3_ptrs, 4);
1105
1106
73.6k
        if (--h <= 0)
1107
1.78k
            goto vert_1;
1108
1109
71.8k
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1110
71.8k
                        sumsq5_rows[3], sum5_rows[3],
1111
71.8k
                        left, src, w, edges);
1112
71.8k
        left++;
1113
71.8k
        src += PXSTRIDE(stride);
1114
71.8k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1115
71.8k
                      w, params->sgr.s0, BITDEPTH_MAX);
1116
71.8k
        rotate(A5_ptrs, B5_ptrs, 2);
1117
71.8k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1118
71.8k
                      w, params->sgr.s1, BITDEPTH_MAX);
1119
71.8k
        rotate(A3_ptrs, B3_ptrs, 4);
1120
1121
71.8k
        if (--h <= 0)
1122
1.42k
            goto vert_2;
1123
1124
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
1125
        // one of them to point at the previously unused rows[4].
1126
70.4k
        sumsq5_ptrs[3] = sumsq5_rows[4];
1127
70.4k
        sum5_ptrs[3] = sum5_rows[4];
1128
70.4k
    } else {
1129
31.8k
        sumsq5_ptrs[0] = sumsq5_rows[0];
1130
31.8k
        sumsq5_ptrs[1] = sumsq5_rows[0];
1131
31.8k
        sumsq5_ptrs[2] = sumsq5_rows[0];
1132
31.8k
        sumsq5_ptrs[3] = sumsq5_rows[0];
1133
31.8k
        sumsq5_ptrs[4] = sumsq5_rows[0];
1134
31.8k
        sum5_ptrs[0] = sum5_rows[0];
1135
31.8k
        sum5_ptrs[1] = sum5_rows[0];
1136
31.8k
        sum5_ptrs[2] = sum5_rows[0];
1137
31.8k
        sum5_ptrs[3] = sum5_rows[0];
1138
31.8k
        sum5_ptrs[4] = sum5_rows[0];
1139
1140
31.8k
        sumsq3_ptrs[0] = sumsq3_rows[0];
1141
31.8k
        sumsq3_ptrs[1] = sumsq3_rows[0];
1142
31.8k
        sumsq3_ptrs[2] = sumsq3_rows[0];
1143
31.8k
        sum3_ptrs[0] = sum3_rows[0];
1144
31.8k
        sum3_ptrs[1] = sum3_rows[0];
1145
31.8k
        sum3_ptrs[2] = sum3_rows[0];
1146
1147
31.8k
        sgr_box35_row_h(sumsq3_rows[0], sum3_rows[0],
1148
31.8k
                        sumsq5_rows[0], sum5_rows[0],
1149
31.8k
                        left, src, w, edges);
1150
31.8k
        left++;
1151
31.8k
        src += PXSTRIDE(stride);
1152
1153
31.8k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1154
31.8k
                      w, params->sgr.s1, BITDEPTH_MAX);
1155
31.8k
        rotate(A3_ptrs, B3_ptrs, 4);
1156
1157
31.8k
        if (--h <= 0)
1158
5.89k
            goto vert_1;
1159
1160
25.9k
        sumsq5_ptrs[4] = sumsq5_rows[1];
1161
25.9k
        sum5_ptrs[4] = sum5_rows[1];
1162
1163
25.9k
        sumsq3_ptrs[2] = sumsq3_rows[1];
1164
25.9k
        sum3_ptrs[2] = sum3_rows[1];
1165
1166
25.9k
        sgr_box35_row_h(sumsq3_rows[1], sum3_rows[1],
1167
25.9k
                        sumsq5_rows[1], sum5_rows[1],
1168
25.9k
                        left, src, w, edges);
1169
25.9k
        left++;
1170
25.9k
        src += PXSTRIDE(stride);
1171
1172
25.9k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1173
25.9k
                      w, params->sgr.s0, BITDEPTH_MAX);
1174
25.9k
        rotate(A5_ptrs, B5_ptrs, 2);
1175
25.9k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1176
25.9k
                      w, params->sgr.s1, BITDEPTH_MAX);
1177
25.9k
        rotate(A3_ptrs, B3_ptrs, 4);
1178
1179
25.9k
        if (--h <= 0)
1180
4.57k
            goto vert_2;
1181
1182
21.4k
        sumsq5_ptrs[3] = sumsq5_rows[2];
1183
21.4k
        sumsq5_ptrs[4] = sumsq5_rows[3];
1184
21.4k
        sum5_ptrs[3] = sum5_rows[2];
1185
21.4k
        sum5_ptrs[4] = sum5_rows[3];
1186
1187
21.4k
        sumsq3_ptrs[2] = sumsq3_rows[2];
1188
21.4k
        sum3_ptrs[2] = sum3_rows[2];
1189
1190
21.4k
        sgr_box35_row_h(sumsq3_rows[2], sum3_rows[2],
1191
21.4k
                        sumsq5_rows[2], sum5_rows[2],
1192
21.4k
                        left, src, w, edges);
1193
21.4k
        left++;
1194
21.4k
        src += PXSTRIDE(stride);
1195
1196
21.4k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1197
21.4k
                      w, params->sgr.s1, BITDEPTH_MAX);
1198
21.4k
        rotate(A3_ptrs, B3_ptrs, 4);
1199
1200
21.4k
        if (--h <= 0)
1201
2.19k
            goto odd;
1202
1203
19.2k
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1204
19.2k
                        sumsq5_rows[3], sum5_rows[3],
1205
19.2k
                        left, src, w, edges);
1206
19.2k
        left++;
1207
19.2k
        src += PXSTRIDE(stride);
1208
1209
19.2k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1210
19.2k
                      w, params->sgr.s0, BITDEPTH_MAX);
1211
19.2k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1212
19.2k
                      w, params->sgr.s1, BITDEPTH_MAX);
1213
19.2k
        sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1214
19.2k
                       w, 2, params->sgr.w0, params->sgr.w1
1215
19.2k
                       HIGHBD_TAIL_SUFFIX);
1216
1217
19.2k
        if (--h <= 0)
1218
2.59k
            goto vert_2;
1219
1220
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
1221
        // one of them to point at the previously unused rows[4].
1222
16.6k
        sumsq5_ptrs[3] = sumsq5_rows[4];
1223
16.6k
        sum5_ptrs[3] = sum5_rows[4];
1224
16.6k
    }
1225
1226
1.90M
    do {
1227
1.90M
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1228
1.90M
                        sumsq5_ptrs[3], sum5_ptrs[3],
1229
1.90M
                        left, src, w, edges);
1230
1.90M
        left++;
1231
1.90M
        src += PXSTRIDE(stride);
1232
1233
1.90M
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1234
1.90M
                      w, params->sgr.s1, BITDEPTH_MAX);
1235
1.90M
        rotate(A3_ptrs, B3_ptrs, 4);
1236
1237
1.90M
        if (--h <= 0)
1238
12.7k
            goto odd;
1239
1240
1.89M
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1241
1.89M
                        sumsq5_ptrs[4], sum5_ptrs[4],
1242
1.89M
                        left, src, w, edges);
1243
1.89M
        left++;
1244
1.89M
        src += PXSTRIDE(stride);
1245
1246
1.89M
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1247
1.89M
                      w, params->sgr.s0, BITDEPTH_MAX);
1248
1.89M
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1249
1.89M
                      w, params->sgr.s1, BITDEPTH_MAX);
1250
1.89M
        sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1251
1.89M
                       w, 2, params->sgr.w0, params->sgr.w1
1252
1.89M
                       HIGHBD_TAIL_SUFFIX);
1253
1.89M
    } while (--h > 0);
1254
1255
74.2k
    if (!(edges & LR_HAVE_BOTTOM))
1256
8.04k
        goto vert_2;
1257
1258
66.2k
    sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1259
66.2k
                    sumsq5_ptrs[3], sum5_ptrs[3],
1260
66.2k
                    NULL, lpf_bottom, w, edges);
1261
66.2k
    lpf_bottom += PXSTRIDE(stride);
1262
66.2k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1263
66.2k
                  w, params->sgr.s1, BITDEPTH_MAX);
1264
66.2k
    rotate(A3_ptrs, B3_ptrs, 4);
1265
1266
66.2k
    sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1267
66.2k
                    sumsq5_ptrs[4], sum5_ptrs[4],
1268
66.2k
                    NULL, lpf_bottom, w, edges);
1269
1270
82.8k
output_2:
1271
82.8k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1272
82.8k
                  w, params->sgr.s0, BITDEPTH_MAX);
1273
82.8k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1274
82.8k
                  w, params->sgr.s1, BITDEPTH_MAX);
1275
82.8k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1276
82.8k
                   w, 2, params->sgr.w0, params->sgr.w1
1277
82.8k
                   HIGHBD_TAIL_SUFFIX);
1278
82.8k
    return;
1279
1280
16.6k
vert_2:
1281
    // Duplicate the last row twice more
1282
16.6k
    sumsq5_ptrs[3] = sumsq5_ptrs[2];
1283
16.6k
    sumsq5_ptrs[4] = sumsq5_ptrs[2];
1284
16.6k
    sum5_ptrs[3] = sum5_ptrs[2];
1285
16.6k
    sum5_ptrs[4] = sum5_ptrs[2];
1286
1287
16.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1288
16.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1289
16.6k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1290
16.6k
                  w, params->sgr.s1, BITDEPTH_MAX);
1291
16.6k
    rotate(A3_ptrs, B3_ptrs, 4);
1292
1293
16.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1294
16.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1295
1296
16.6k
    goto output_2;
1297
1298
14.9k
odd:
1299
    // Copy the last row as padding once
1300
14.9k
    sumsq5_ptrs[4] = sumsq5_ptrs[3];
1301
14.9k
    sum5_ptrs[4] = sum5_ptrs[3];
1302
1303
14.9k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1304
14.9k
    sum3_ptrs[2] = sum3_ptrs[1];
1305
1306
14.9k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1307
14.9k
                  w, params->sgr.s0, BITDEPTH_MAX);
1308
14.9k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1309
14.9k
                  w, params->sgr.s1, BITDEPTH_MAX);
1310
14.9k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1311
14.9k
                   w, 2, params->sgr.w0, params->sgr.w1
1312
14.9k
                   HIGHBD_TAIL_SUFFIX);
1313
1314
22.6k
output_1:
1315
    // Duplicate the last row twice more
1316
22.6k
    sumsq5_ptrs[3] = sumsq5_ptrs[2];
1317
22.6k
    sumsq5_ptrs[4] = sumsq5_ptrs[2];
1318
22.6k
    sum5_ptrs[3] = sum5_ptrs[2];
1319
22.6k
    sum5_ptrs[4] = sum5_ptrs[2];
1320
1321
22.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1322
22.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1323
1324
22.6k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1325
22.6k
                  w, params->sgr.s0, BITDEPTH_MAX);
1326
22.6k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1327
22.6k
                  w, params->sgr.s1, BITDEPTH_MAX);
1328
22.6k
    rotate(A3_ptrs, B3_ptrs, 4);
1329
    // Output only one row
1330
22.6k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1331
22.6k
                   w, 1, params->sgr.w0, params->sgr.w1
1332
22.6k
                   HIGHBD_TAIL_SUFFIX);
1333
22.6k
    return;
1334
1335
7.67k
vert_1:
1336
    // Copy the last row as padding once
1337
7.67k
    sumsq5_ptrs[4] = sumsq5_ptrs[3];
1338
7.67k
    sum5_ptrs[4] = sum5_ptrs[3];
1339
1340
7.67k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1341
7.67k
    sum3_ptrs[2] = sum3_ptrs[1];
1342
1343
7.67k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1344
7.67k
                  w, params->sgr.s0, BITDEPTH_MAX);
1345
7.67k
    rotate(A5_ptrs, B5_ptrs, 2);
1346
7.67k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1347
7.67k
                  w, params->sgr.s1, BITDEPTH_MAX);
1348
7.67k
    rotate(A3_ptrs, B3_ptrs, 4);
1349
1350
7.67k
    goto output_1;
1351
14.9k
}
1352
1353
#if HAVE_ASM
1354
#if ARCH_AARCH64 || ARCH_ARM
1355
#include "src/arm/looprestoration.h"
1356
#elif ARCH_LOONGARCH64
1357
#include "src/loongarch/looprestoration.h"
1358
#elif ARCH_PPC64LE
1359
#include "src/ppc/looprestoration.h"
1360
#elif ARCH_X86
1361
#include "src/x86/looprestoration.h"
1362
#endif
1363
#endif
1364
1365
COLD void bitfn(dav1d_loop_restoration_dsp_init)(Dav1dLoopRestorationDSPContext *const c,
1366
                                                 const int bpc)
1367
88.4k
{
1368
88.4k
    c->wiener[0] = c->wiener[1] = wiener_c;
1369
88.4k
    c->sgr[0] = sgr_5x5_c;
1370
88.4k
    c->sgr[1] = sgr_3x3_c;
1371
88.4k
    c->sgr[2] = sgr_mix_c;
1372
1373
#if HAVE_ASM
1374
#if ARCH_AARCH64 || ARCH_ARM
1375
    loop_restoration_dsp_init_arm(c, bpc);
1376
#elif ARCH_LOONGARCH64
1377
    loop_restoration_dsp_init_loongarch(c, bpc);
1378
#elif ARCH_PPC64LE
1379
    loop_restoration_dsp_init_ppc(c, bpc);
1380
#elif ARCH_X86
1381
    loop_restoration_dsp_init_x86(c, bpc);
1382
#endif
1383
#endif
1384
88.4k
}
dav1d_loop_restoration_dsp_init_8bpc
Line
Count
Source
1367
40.2k
{
1368
40.2k
    c->wiener[0] = c->wiener[1] = wiener_c;
1369
40.2k
    c->sgr[0] = sgr_5x5_c;
1370
40.2k
    c->sgr[1] = sgr_3x3_c;
1371
40.2k
    c->sgr[2] = sgr_mix_c;
1372
1373
#if HAVE_ASM
1374
#if ARCH_AARCH64 || ARCH_ARM
1375
    loop_restoration_dsp_init_arm(c, bpc);
1376
#elif ARCH_LOONGARCH64
1377
    loop_restoration_dsp_init_loongarch(c, bpc);
1378
#elif ARCH_PPC64LE
1379
    loop_restoration_dsp_init_ppc(c, bpc);
1380
#elif ARCH_X86
1381
    loop_restoration_dsp_init_x86(c, bpc);
1382
#endif
1383
#endif
1384
40.2k
}
dav1d_loop_restoration_dsp_init_16bpc
Line
Count
Source
1367
48.1k
{
1368
48.1k
    c->wiener[0] = c->wiener[1] = wiener_c;
1369
48.1k
    c->sgr[0] = sgr_5x5_c;
1370
48.1k
    c->sgr[1] = sgr_3x3_c;
1371
48.1k
    c->sgr[2] = sgr_mix_c;
1372
1373
#if HAVE_ASM
1374
#if ARCH_AARCH64 || ARCH_ARM
1375
    loop_restoration_dsp_init_arm(c, bpc);
1376
#elif ARCH_LOONGARCH64
1377
    loop_restoration_dsp_init_loongarch(c, bpc);
1378
#elif ARCH_PPC64LE
1379
    loop_restoration_dsp_init_ppc(c, bpc);
1380
#elif ARCH_X86
1381
    loop_restoration_dsp_init_x86(c, bpc);
1382
#endif
1383
#endif
1384
48.1k
}