Coverage Report

Created: 2026-09-02 06:43

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.38M
#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
5.85M
{
49
5.85M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
50
5.85M
    const int round_bits_h = 3 + (bitdepth == 12) * 2;
51
5.85M
    const int rounding_off_h = 1 << (round_bits_h - 1);
52
5.85M
    const int clip_limit = 1 << (bitdepth + 1 + 7 - round_bits_h);
53
54
5.85M
    if (w < 6) {
55
        // For small widths, do the fully conditional loop with
56
        // conditions on each access.
57
4.04M
        for (int x = 0; x < w; x++) {
58
2.91M
            int sum = (1 << (bitdepth + 6));
59
2.91M
#if BITDEPTH == 8
60
2.91M
            sum += src[x] * 128;
61
2.91M
#endif
62
23.1M
            for (int i = 0; i < 7; i++) {
63
20.2M
                int idx = x + i - 3;
64
20.2M
                if (idx < 0) {
65
5.25M
                    if (!(edges & LR_HAVE_LEFT))
66
5.26M
                        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
14.9M
                } else if (idx >= w && !(edges & LR_HAVE_RIGHT)) {
72
5.11M
                    sum += src[w - 1] * fh[i];
73
5.11M
                } else
74
9.83M
                    sum += src[idx] * fh[i];
75
20.2M
            }
76
2.91M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
77
2.91M
            dst[x] = sum;
78
2.91M
        }
79
80
1.13M
        return;
81
1.13M
    }
82
83
    // For larger widths, do separate loops with less conditions; first
84
    // handle the start of the row.
85
4.71M
    int start = 3;
86
4.71M
    if (!(edges & LR_HAVE_LEFT)) {
87
        // If there's no left edge, pad using the leftmost pixel.
88
9.56M
        for (int x = 0; x < 3; x++) {
89
7.15M
            int sum = (1 << (bitdepth + 6));
90
7.15M
#if BITDEPTH == 8
91
7.15M
            sum += src[x] * 128;
92
7.15M
#endif
93
57.1M
            for (int i = 0; i < 7; i++) {
94
49.9M
                int idx = x + i - 3;
95
49.9M
                if (idx < 0)
96
14.3M
                    sum += src[0] * fh[i];
97
35.6M
                else
98
35.6M
                    sum += src[idx] * fh[i];
99
49.9M
            }
100
7.15M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
101
7.15M
            dst[x] = sum;
102
7.15M
        }
103
2.41M
    } else if (left) {
104
        // If we have the left edge and a separate left buffer, pad using that.
105
8.54M
        for (int x = 0; x < 3; x++) {
106
6.39M
            int sum = (1 << (bitdepth + 6));
107
6.39M
#if BITDEPTH == 8
108
6.39M
            sum += src[x] * 128;
109
6.39M
#endif
110
51.1M
            for (int i = 0; i < 7; i++) {
111
44.7M
                int idx = x + i - 3;
112
44.7M
                if (idx < 0)
113
12.8M
                    sum += left[0][4 + idx] * fh[i];
114
31.9M
                else
115
31.9M
                    sum += src[idx] * fh[i];
116
44.7M
            }
117
6.39M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
118
6.39M
            dst[x] = sum;
119
6.39M
        }
120
2.14M
    } 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
157k
        start = 0;
125
157k
    }
126
4.71M
    int end = w - 3;
127
4.71M
    if (edges & LR_HAVE_RIGHT)
128
2.23M
        end = w;
129
130
    // Do a condititon free loop for the bulk of the row.
131
361M
    for (int x = start; x < end; x++) {
132
357M
        int sum = (1 << (bitdepth + 6));
133
357M
#if BITDEPTH == 8
134
357M
        sum += src[x] * 128;
135
357M
#endif
136
2.83G
        for (int i = 0; i < 7; i++) {
137
2.47G
            int idx = x + i - 3;
138
2.47G
            sum += src[idx] * fh[i];
139
2.47G
        }
140
357M
        sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
141
357M
        dst[x] = sum;
142
357M
    }
143
144
    // If we need to, calculate the end of the row with a condition for
145
    // right edge padding.
146
11.7M
    for (int x = end; x < w; x++) {
147
7.05M
        int sum = (1 << (bitdepth + 6));
148
7.05M
#if BITDEPTH == 8
149
7.05M
        sum += src[x] * 128;
150
7.05M
#endif
151
56.3M
        for (int i = 0; i < 7; i++) {
152
49.3M
            int idx = x + i - 3;
153
49.3M
            if (idx >= w)
154
14.0M
                sum += src[w - 1] * fh[i];
155
35.2M
            else
156
35.2M
                sum += src[idx] * fh[i];
157
49.3M
        }
158
7.05M
        sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
159
7.05M
        dst[x] = sum;
160
7.05M
    }
161
4.71M
}
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
258k
{
166
258k
    const int bitdepth = bitdepth_from_max(bitdepth_max);
167
168
258k
    const int round_bits_v = 11 - (bitdepth == 12) * 2;
169
258k
    const int rounding_off_v = 1 << (round_bits_v - 1);
170
258k
    const int round_offset = 1 << (bitdepth + (round_bits_v - 1));
171
172
15.6M
    for (int i = 0; i < w; i++) {
173
15.4M
        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
108M
        for (int k = 0; k < 6; k++)
181
92.5M
            sum += ptrs[k][i] * fv[k];
182
15.4M
        sum += ptrs[5][i] * fv[6];
183
184
15.4M
        p[i] = iclip_pixel((sum + rounding_off_v) >> round_bits_v);
185
15.4M
    }
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.55M
    for (int i = 0; i < 5; i++)
190
1.29M
        ptrs[i] = ptrs[i + 1];
191
258k
}
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.23M
{
198
5.23M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
199
200
5.23M
    const int round_bits_v = 11 - (bitdepth == 12) * 2;
201
5.23M
    const int rounding_off_v = 1 << (round_bits_v - 1);
202
5.23M
    const int round_offset = 1 << (bitdepth + (round_bits_v - 1));
203
204
5.23M
    const int16_t *fh = filter[0];
205
5.23M
    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.23M
    uint16_t tmp[REST_UNIT_STRIDE];
214
5.23M
    wiener_filter_h(tmp, left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
215
216
342M
    for (int i = 0; i < w; i++) {
217
337M
        int sum = -round_offset;
218
219
        // Filter using the 6 stored preexisting rows, and the newly
220
        // filtered one in tmp[].
221
2.34G
        for (int k = 0; k < 6; k++)
222
2.00G
            sum += ptrs[k][i] * fv[k];
223
337M
        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
337M
        p[i] = iclip_pixel((sum + rounding_off_v) >> round_bits_v);
228
337M
    }
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.23M
    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
36.6M
    for (int i = 0; i < 6; i++)
240
31.4M
        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.23M
    ptrs[6] = ptrs[0];
246
5.23M
}
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
170k
{
256
    // Values stored between horizontal and vertical filtering don't
257
    // fit in a uint8_t.
258
170k
    uint16_t hor[6 * REST_UNIT_STRIDE];
259
170k
    uint16_t *ptrs[7], *rows[6];
260
1.19M
    for (int i = 0; i < 6; i++)
261
1.02M
        rows[i] = &hor[i * REST_UNIT_STRIDE];
262
170k
    const int16_t (*const filter)[8] = params->filter;
263
170k
    const int16_t *fh = params->filter[0];
264
170k
    const int16_t *fv = params->filter[1];
265
170k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
266
267
170k
    const pixel *src = p;
268
170k
    if (edges & LR_HAVE_TOP) {
269
96.7k
        ptrs[0] = rows[0];
270
96.7k
        ptrs[1] = rows[0];
271
96.7k
        ptrs[2] = rows[1];
272
96.7k
        ptrs[3] = rows[2];
273
96.7k
        ptrs[4] = rows[2];
274
96.7k
        ptrs[5] = rows[2];
275
276
96.7k
        wiener_filter_h(rows[0], NULL, lpf, fh, w, edges HIGHBD_TAIL_SUFFIX);
277
96.7k
        lpf += PXSTRIDE(stride);
278
96.7k
        wiener_filter_h(rows[1], NULL, lpf, fh, w, edges HIGHBD_TAIL_SUFFIX);
279
280
96.7k
        wiener_filter_h(rows[2], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
281
96.7k
        left++;
282
96.7k
        src += PXSTRIDE(stride);
283
284
96.7k
        if (--h <= 0)
285
1.49k
            goto v1;
286
287
95.2k
        ptrs[4] = ptrs[5] = rows[3];
288
95.2k
        wiener_filter_h(rows[3], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
289
95.2k
        left++;
290
95.2k
        src += PXSTRIDE(stride);
291
292
95.2k
        if (--h <= 0)
293
831
            goto v2;
294
295
94.4k
        ptrs[5] = rows[4];
296
94.4k
        wiener_filter_h(rows[4], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
297
94.4k
        left++;
298
94.4k
        src += PXSTRIDE(stride);
299
300
94.4k
        if (--h <= 0)
301
716
            goto v3;
302
94.4k
    } else {
303
73.9k
        ptrs[0] = rows[0];
304
73.9k
        ptrs[1] = rows[0];
305
73.9k
        ptrs[2] = rows[0];
306
73.9k
        ptrs[3] = rows[0];
307
73.9k
        ptrs[4] = rows[0];
308
73.9k
        ptrs[5] = rows[0];
309
310
73.9k
        wiener_filter_h(rows[0], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
311
73.9k
        left++;
312
73.9k
        src += PXSTRIDE(stride);
313
314
73.9k
        if (--h <= 0)
315
22.8k
            goto v1;
316
317
51.1k
        ptrs[4] = ptrs[5] = rows[1];
318
51.1k
        wiener_filter_h(rows[1], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
319
51.1k
        left++;
320
51.1k
        src += PXSTRIDE(stride);
321
322
51.1k
        if (--h <= 0)
323
9.33k
            goto v2;
324
325
41.7k
        ptrs[5] = rows[2];
326
41.7k
        wiener_filter_h(rows[2], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
327
41.7k
        left++;
328
41.7k
        src += PXSTRIDE(stride);
329
330
41.7k
        if (--h <= 0)
331
3.51k
            goto v3;
332
333
38.2k
        ptrs[6] = rows[3];
334
38.2k
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
335
38.2k
                         HIGHBD_TAIL_SUFFIX);
336
38.2k
        left++;
337
38.2k
        src += PXSTRIDE(stride);
338
38.2k
        p += PXSTRIDE(stride);
339
340
38.2k
        if (--h <= 0)
341
2.88k
            goto v3;
342
343
35.3k
        ptrs[6] = rows[4];
344
35.3k
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
345
35.3k
                         HIGHBD_TAIL_SUFFIX);
346
35.3k
        left++;
347
35.3k
        src += PXSTRIDE(stride);
348
35.3k
        p += PXSTRIDE(stride);
349
350
35.3k
        if (--h <= 0)
351
3.09k
            goto v3;
352
35.3k
    }
353
354
126k
    ptrs[6] = ptrs[5] + REST_UNIT_STRIDE;
355
4.97M
    do {
356
4.97M
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
357
4.97M
                         HIGHBD_TAIL_SUFFIX);
358
4.97M
        left++;
359
4.97M
        src += PXSTRIDE(stride);
360
4.97M
        p += PXSTRIDE(stride);
361
4.97M
    } while (--h > 0);
362
363
126k
    if (!(edges & LR_HAVE_BOTTOM))
364
28.7k
        goto v3;
365
366
97.3k
    wiener_filter_hv(p, ptrs, NULL, lpf_bottom, filter, w, edges
367
97.3k
                     HIGHBD_TAIL_SUFFIX);
368
97.3k
    lpf_bottom += PXSTRIDE(stride);
369
97.3k
    p += PXSTRIDE(stride);
370
371
97.3k
    wiener_filter_hv(p, ptrs, NULL, lpf_bottom, filter, w, edges
372
97.3k
                     HIGHBD_TAIL_SUFFIX);
373
97.3k
    p += PXSTRIDE(stride);
374
170k
v1:
375
170k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
376
377
170k
    return;
378
379
38.9k
v3:
380
38.9k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
381
38.9k
    p += PXSTRIDE(stride);
382
49.0k
v2:
383
49.0k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
384
49.0k
    p += PXSTRIDE(stride);
385
49.0k
    goto v1;
386
38.9k
}
387
388
// SGR
389
static NOINLINE void rotate(int32_t **sumsq_ptrs, coef **sum_ptrs, int n)
390
11.8M
{
391
11.8M
    int32_t *tmp32 = sumsq_ptrs[0];
392
11.8M
    coef *tmpc = sum_ptrs[0];
393
36.5M
    for (int i = 0; i < n - 1; i++) {
394
24.6M
        sumsq_ptrs[i] = sumsq_ptrs[i + 1];
395
24.6M
        sum_ptrs[i] = sum_ptrs[i + 1];
396
24.6M
    }
397
11.8M
    sumsq_ptrs[n - 1] = tmp32;
398
11.8M
    sum_ptrs[n - 1] = tmpc;
399
11.8M
}
400
401
static NOINLINE void rotate5_x2(int32_t **sumsq_ptrs, coef **sum_ptrs)
402
2.66M
{
403
2.66M
    int32_t *tmp32[2];
404
2.66M
    coef *tmpc[2];
405
8.00M
    for (int i = 0; i < 2; i++) {
406
5.34M
        tmp32[i] = sumsq_ptrs[i];
407
5.34M
        tmpc[i] = sum_ptrs[i];
408
5.34M
    }
409
10.7M
    for (int i = 0; i < 3; i++) {
410
8.05M
        sumsq_ptrs[i] = sumsq_ptrs[i + 2];
411
8.05M
        sum_ptrs[i] = sum_ptrs[i + 2];
412
8.05M
    }
413
8.07M
    for (int i = 0; i < 2; i++) {
414
5.41M
        sumsq_ptrs[3 + i] = tmp32[i];
415
5.41M
        sum_ptrs[3 + i] = tmpc[i];
416
5.41M
    }
417
2.66M
}
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
4.97M
{
424
4.97M
    sumsq++;
425
4.97M
    sum++;
426
4.97M
    int a = edges & LR_HAVE_LEFT ? (left ? left[0][2] : src[-2]) : src[0];
427
4.97M
    int b = edges & LR_HAVE_LEFT ? (left ? left[0][3] : src[-1]) : src[0];
428
304M
    for (int x = -1; x < w + 1; x++) {
429
299M
        int c = (x + 1 < w || (edges & LR_HAVE_RIGHT)) ? src[x + 1] : src[w - 1];
430
299M
        sum[x] = a + b + c;
431
299M
        sumsq[x] = a * a + b * b + c * c;
432
299M
        a = b;
433
299M
        b = c;
434
299M
    }
435
4.97M
}
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
5.41M
{
442
5.41M
    sumsq++;
443
5.41M
    sum++;
444
5.41M
    int a = edges & LR_HAVE_LEFT ? (left ? left[0][1] : src[-3]) : src[0];
445
5.41M
    int b = edges & LR_HAVE_LEFT ? (left ? left[0][2] : src[-2]) : src[0];
446
5.41M
    int c = edges & LR_HAVE_LEFT ? (left ? left[0][3] : src[-1]) : src[0];
447
5.41M
    int d = src[0];
448
326M
    for (int x = -1; x < w + 1; x++) {
449
321M
        int e = (x + 2 < w || (edges & LR_HAVE_RIGHT)) ? src[x + 2] : src[w - 1];
450
321M
        sum[x] = a + b + c + d + e;
451
321M
        sumsq[x] = a * a + b * b + c * c + d * d + e * e;
452
321M
        a = b;
453
321M
        b = c;
454
321M
        c = d;
455
321M
        d = e;
456
321M
    }
457
5.41M
}
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
3.75M
{
465
3.75M
    sgr_box3_row_h(sumsq3, sum3, left, src, w, edges);
466
3.75M
    sgr_box5_row_h(sumsq5, sum5, left, src, w, edges);
467
3.75M
}
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
4.95M
{
473
306M
    for (int x = 0; x < w + 2; x++) {
474
301M
        int sq_a = sumsq[0][x];
475
301M
        int sq_b = sumsq[1][x];
476
301M
        int sq_c = sumsq[2][x];
477
301M
        int s_a = sum[0][x];
478
301M
        int s_b = sum[1][x];
479
301M
        int s_c = sum[2][x];
480
301M
        sumsq_out[x] = sq_a + sq_b + sq_c;
481
301M
        sum_out[x] = s_a + s_b + s_c;
482
301M
    }
483
4.95M
}
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
2.73M
{
489
168M
    for (int x = 0; x < w + 2; x++) {
490
166M
        int sq_a = sumsq[0][x];
491
166M
        int sq_b = sumsq[1][x];
492
166M
        int sq_c = sumsq[2][x];
493
166M
        int sq_d = sumsq[3][x];
494
166M
        int sq_e = sumsq[4][x];
495
166M
        int s_a = sum[0][x];
496
166M
        int s_b = sum[1][x];
497
166M
        int s_c = sum[2][x];
498
166M
        int s_d = sum[3][x];
499
166M
        int s_e = sum[4][x];
500
166M
        sumsq_out[x] = sq_a + sq_b + sq_c + sq_d + sq_e;
501
166M
        sum_out[x] = s_a + s_b + s_c + s_d + s_e;
502
166M
    }
503
2.73M
}
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
7.71M
{
508
7.71M
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
509
403M
    for (int i = 0; i < w + 2; i++) {
510
396M
        const int a =
511
396M
            (AA[i] + ((1 << (2 * bitdepth_min_8)) >> 1)) >> (2 * bitdepth_min_8);
512
396M
        const int b =
513
396M
            (BB[i] + ((1 << bitdepth_min_8) >> 1)) >> bitdepth_min_8;
514
515
396M
        const unsigned p = imax(a * n - b * b, 0);
516
396M
        const unsigned z = (p * s + (1 << 19)) >> 20;
517
396M
        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
396M
        AA[i] = (x * BB[i] * sgr_one_by_x + (1 << 11)) >> 12;
521
396M
        BB[i] = x;
522
396M
    }
523
7.71M
}
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
4.94M
{
529
4.94M
    sgr_box3_row_v(sumsq, sum, sumsq_out, sum_out, w);
530
4.94M
    sgr_calc_row_ab(sumsq_out, sum_out, w, s, bitdepth_max, 9, 455);
531
4.94M
    rotate(sumsq, sum, 3);
532
4.94M
}
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
2.74M
{
538
2.74M
    sgr_box5_row_v(sumsq, sum, sumsq_out, sum_out, w);
539
2.74M
    sgr_calc_row_ab(sumsq_out, sum_out, w, s, bitdepth_max, 25, 164);
540
2.74M
    rotate5_x2(sumsq, sum);
541
2.74M
}
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.25M
{
551
1.25M
    sgr_box3_row_h(sumsq[2], sum[2], left, src, w, edges);
552
1.25M
    sgr_box3_vert(sumsq, sum, AA, BB, w, s, bitdepth_max);
553
1.25M
}
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
4.75M
{
560
4.75M
#define EIGHT_NEIGHBORS(P, i)\
561
555M
    ((P[1][i] + P[1][i - 1] + P[1][i + 1] + P[0][i] + P[2][i]) * 4 + \
562
555M
     (P[0][i - 1] + P[2][i - 1] +                           \
563
555M
      P[0][i + 1] + P[2][i + 1]) * 3)
564
282M
    for (int i = 0; i < w; i++) {
565
277M
        const int a = EIGHT_NEIGHBORS(B_ptrs, i + 1);
566
277M
        const int b = EIGHT_NEIGHBORS(A_ptrs, i + 1);
567
277M
        tmp[i] = (b - a * src[i] + (1 << 8)) >> 9;
568
277M
    }
569
4.75M
#undef EIGHT_NEIGHBORS
570
4.75M
}
571
572
12.2M
#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.60M
{
580
2.60M
#define SIX_NEIGHBORS(P, i)\
581
304M
    ((P[0][i]     + P[1][i]) * 6 +   \
582
304M
     (P[0][i - 1] + P[1][i - 1] +    \
583
304M
      P[0][i + 1] + P[1][i + 1]) * 5)
584
155M
    for (int i = 0; i < w; i++) {
585
152M
        const int a = SIX_NEIGHBORS(B_ptrs, i + 1);
586
152M
        const int b = SIX_NEIGHBORS(A_ptrs, i + 1);
587
152M
        tmp[i] = (b - a * src[i] + (1 << 8)) >> 9;
588
152M
    }
589
2.60M
    if (h <= 1)
590
28.0k
        return;
591
2.57M
    tmp += FILTER_OUT_STRIDE;
592
2.57M
    src += PXSTRIDE(src_stride);
593
2.57M
    const int32_t *A = &A_ptrs[1][1];
594
2.57M
    const coef *B = &B_ptrs[1][1];
595
154M
    for (int i = 0; i < w; i++) {
596
151M
        const int a = B[i] * 6 + (B[i - 1] + B[i + 1]) * 5;
597
151M
        const int b = A[i] * 6 + (A[i - 1] + A[i + 1]) * 5;
598
151M
        tmp[i] = (b - a * src[i] + (1 << 7)) >> 8;
599
151M
    }
600
2.57M
#undef SIX_NEIGHBORS
601
2.57M
}
602
603
static NOINLINE void sgr_weighted_row1(pixel *dst, const coef *t1,
604
                                       const int w, const int w1 HIGHBD_DECL_SUFFIX)
605
2.80M
{
606
154M
    for (int i = 0; i < w; i++) {
607
152M
        const int v = w1 * t1[i];
608
152M
        dst[i] = iclip_pixel(dst[i] + ((v + (1 << 10)) >> 11));
609
152M
    }
610
2.80M
}
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
1.82M
{
617
5.38M
    for (int j = 0; j < h; j++) {
618
197M
        for (int i = 0; i < w; i++) {
619
193M
            const int v = w0 * t1[i] + w1 * t2[i];
620
193M
            dst[i] = iclip_pixel(dst[i] + ((v + (1 << 10)) >> 11));
621
193M
        }
622
3.56M
        dst += PXSTRIDE(dst_stride);
623
3.56M
        t1 += FILTER_OUT_STRIDE;
624
3.56M
        t2 += FILTER_OUT_STRIDE;
625
3.56M
    }
626
1.82M
}
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.23M
{
632
    // Only one single row, no stride needed
633
1.23M
    ALIGN_STK_16(coef, tmp, 384,);
634
635
1.23M
    sgr_finish_filter_row1(tmp, *dst, A_ptrs, B_ptrs, w);
636
1.23M
    sgr_weighted_row1(*dst, tmp, w, w1 HIGHBD_TAIL_SUFFIX);
637
1.23M
    *dst += PXSTRIDE(stride);
638
1.23M
    rotate(A_ptrs, B_ptrs, 3);
639
1.23M
}
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
813k
{
646
813k
    ALIGN_STK_16(coef, tmp, 2*FILTER_OUT_STRIDE,);
647
648
813k
    sgr_finish_filter2(tmp, *dst, stride, A_ptrs, B_ptrs, w, h);
649
813k
    sgr_weighted_row1(*dst, tmp, w, w1 HIGHBD_TAIL_SUFFIX);
650
813k
    *dst += PXSTRIDE(stride);
651
813k
    if (h > 1) {
652
785k
        sgr_weighted_row1(*dst, tmp + FILTER_OUT_STRIDE, w, w1 HIGHBD_TAIL_SUFFIX);
653
785k
        *dst += PXSTRIDE(stride);
654
785k
    }
655
813k
    rotate(A_ptrs, B_ptrs, 2);
656
813k
}
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
1.79M
{
664
1.79M
    ALIGN_STK_16(coef, tmp5, 2*FILTER_OUT_STRIDE,);
665
1.79M
    ALIGN_STK_16(coef, tmp3, 2*FILTER_OUT_STRIDE,);
666
667
1.79M
    sgr_finish_filter2(tmp5, *dst, stride, A5_ptrs, B5_ptrs, w, h);
668
1.79M
    sgr_finish_filter_row1(tmp3, *dst, A3_ptrs, B3_ptrs, w);
669
1.79M
    if (h > 1)
670
1.80M
        sgr_finish_filter_row1(tmp3 + FILTER_OUT_STRIDE, *dst + PXSTRIDE(stride),
671
1.80M
                               &A3_ptrs[1], &B3_ptrs[1], w);
672
1.79M
    sgr_weighted2(*dst, stride, tmp5, tmp3, w, h, w0, w1 HIGHBD_TAIL_SUFFIX);
673
1.79M
    *dst += h*PXSTRIDE(stride);
674
1.79M
    rotate(A5_ptrs, B5_ptrs, 2);
675
1.79M
    rotate(A3_ptrs, B3_ptrs, 4);
676
1.79M
}
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
32.0k
{
685
3.55M
#define BUF_STRIDE (384 + 16)
686
32.0k
    ALIGN_STK_16(int32_t, sumsq_buf, BUF_STRIDE * 3 + 16,);
687
32.0k
    ALIGN_STK_16(coef, sum_buf, BUF_STRIDE * 3 + 16,);
688
32.0k
    int32_t *sumsq_ptrs[3], *sumsq_rows[3];
689
32.0k
    coef *sum_ptrs[3], *sum_rows[3];
690
128k
    for (int i = 0; i < 3; i++) {
691
96.0k
        sumsq_rows[i] = &sumsq_buf[i * BUF_STRIDE];
692
96.0k
        sum_rows[i] = &sum_buf[i * BUF_STRIDE];
693
96.0k
    }
694
695
32.0k
    ALIGN_STK_16(int32_t, A_buf, BUF_STRIDE * 3 + 16,);
696
32.0k
    ALIGN_STK_16(coef, B_buf, BUF_STRIDE * 3 + 16,);
697
32.0k
    int32_t *A_ptrs[3];
698
32.0k
    coef *B_ptrs[3];
699
128k
    for (int i = 0; i < 3; i++) {
700
96.0k
        A_ptrs[i] = &A_buf[i * BUF_STRIDE];
701
96.0k
        B_ptrs[i] = &B_buf[i * BUF_STRIDE];
702
96.0k
    }
703
32.0k
    const pixel *src = dst;
704
32.0k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
705
706
32.0k
    if (edges & LR_HAVE_TOP) {
707
22.7k
        sumsq_ptrs[0] = sumsq_rows[0];
708
22.7k
        sumsq_ptrs[1] = sumsq_rows[1];
709
22.7k
        sumsq_ptrs[2] = sumsq_rows[2];
710
22.7k
        sum_ptrs[0] = sum_rows[0];
711
22.7k
        sum_ptrs[1] = sum_rows[1];
712
22.7k
        sum_ptrs[2] = sum_rows[2];
713
714
22.7k
        sgr_box3_row_h(sumsq_rows[0], sum_rows[0], NULL, lpf, w, edges);
715
22.7k
        lpf += PXSTRIDE(stride);
716
22.7k
        sgr_box3_row_h(sumsq_rows[1], sum_rows[1], NULL, lpf, w, edges);
717
718
22.7k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
719
22.7k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
720
22.7k
        left++;
721
22.7k
        src += PXSTRIDE(stride);
722
22.7k
        rotate(A_ptrs, B_ptrs, 3);
723
724
22.7k
        if (--h <= 0)
725
404
            goto vert_1;
726
727
22.3k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
728
22.3k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
729
22.3k
        left++;
730
22.3k
        src += PXSTRIDE(stride);
731
22.3k
        rotate(A_ptrs, B_ptrs, 3);
732
733
22.3k
        if (--h <= 0)
734
371
            goto vert_2;
735
22.3k
    } else {
736
9.28k
        sumsq_ptrs[0] = sumsq_rows[0];
737
9.28k
        sumsq_ptrs[1] = sumsq_rows[0];
738
9.28k
        sumsq_ptrs[2] = sumsq_rows[0];
739
9.28k
        sum_ptrs[0] = sum_rows[0];
740
9.28k
        sum_ptrs[1] = sum_rows[0];
741
9.28k
        sum_ptrs[2] = sum_rows[0];
742
743
9.28k
        sgr_box3_row_h(sumsq_rows[0], sum_rows[0], left, src, w, edges);
744
9.28k
        left++;
745
9.28k
        src += PXSTRIDE(stride);
746
747
9.28k
        sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
748
9.28k
                      w, params->sgr.s1, BITDEPTH_MAX);
749
9.28k
        rotate(A_ptrs, B_ptrs, 3);
750
751
9.28k
        if (--h <= 0)
752
1.47k
            goto vert_1;
753
754
7.81k
        sumsq_ptrs[2] = sumsq_rows[1];
755
7.81k
        sum_ptrs[2] = sum_rows[1];
756
757
7.81k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
758
7.81k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
759
7.81k
        left++;
760
7.81k
        src += PXSTRIDE(stride);
761
7.81k
        rotate(A_ptrs, B_ptrs, 3);
762
763
7.81k
        if (--h <= 0)
764
1.41k
            goto vert_2;
765
766
6.39k
        sumsq_ptrs[2] = sumsq_rows[2];
767
6.39k
        sum_ptrs[2] = sum_rows[2];
768
6.39k
    }
769
770
1.16M
    do {
771
1.16M
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
772
1.16M
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
773
1.16M
        left++;
774
1.16M
        src += PXSTRIDE(stride);
775
776
1.16M
        sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
777
1.16M
                    w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
778
1.16M
    } while (--h > 0);
779
780
28.3k
    if (!(edges & LR_HAVE_BOTTOM))
781
7.83k
        goto vert_2;
782
783
20.5k
    sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
784
20.5k
                NULL, lpf_bottom, w, params->sgr.s1, edges, BITDEPTH_MAX);
785
20.5k
    lpf_bottom += PXSTRIDE(stride);
786
787
20.5k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
788
20.5k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
789
790
20.5k
    sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
791
20.5k
                NULL, lpf_bottom, w, params->sgr.s1, edges, BITDEPTH_MAX);
792
793
20.5k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
794
20.5k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
795
20.5k
    return;
796
797
9.61k
vert_2:
798
9.61k
    sumsq_ptrs[2] = sumsq_ptrs[1];
799
9.61k
    sum_ptrs[2] = sum_ptrs[1];
800
9.61k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
801
9.61k
                  w, params->sgr.s1, BITDEPTH_MAX);
802
803
9.61k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
804
9.61k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
805
806
11.4k
output_1:
807
11.4k
    sumsq_ptrs[2] = sumsq_ptrs[1];
808
11.4k
    sum_ptrs[2] = sum_ptrs[1];
809
11.4k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
810
11.4k
                  w, params->sgr.s1, BITDEPTH_MAX);
811
812
11.4k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
813
11.4k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
814
11.4k
    return;
815
816
1.88k
vert_1:
817
1.88k
    sumsq_ptrs[2] = sumsq_ptrs[1];
818
1.88k
    sum_ptrs[2] = sum_ptrs[1];
819
1.88k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
820
1.88k
                  w, params->sgr.s1, BITDEPTH_MAX);
821
1.88k
    rotate(A_ptrs, B_ptrs, 3);
822
1.88k
    goto output_1;
823
9.61k
}
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
40.7k
{
831
40.7k
    ALIGN_STK_16(int32_t, sumsq_buf, BUF_STRIDE * 5 + 16,);
832
40.7k
    ALIGN_STK_16(coef, sum_buf, BUF_STRIDE * 5 + 16,);
833
40.7k
    int32_t *sumsq_ptrs[5], *sumsq_rows[5];
834
40.7k
    coef *sum_ptrs[5], *sum_rows[5];
835
244k
    for (int i = 0; i < 5; i++) {
836
203k
        sumsq_rows[i] = &sumsq_buf[i * BUF_STRIDE];
837
203k
        sum_rows[i] = &sum_buf[i * BUF_STRIDE];
838
203k
    }
839
840
40.7k
    ALIGN_STK_16(int32_t, A_buf, BUF_STRIDE * 2 + 16,);
841
40.7k
    ALIGN_STK_16(coef, B_buf, BUF_STRIDE * 2 + 16,);
842
40.7k
    int32_t *A_ptrs[2];
843
40.7k
    coef *B_ptrs[2];
844
122k
    for (int i = 0; i < 2; i++) {
845
81.5k
        A_ptrs[i] = &A_buf[i * BUF_STRIDE];
846
81.5k
        B_ptrs[i] = &B_buf[i * BUF_STRIDE];
847
81.5k
    }
848
40.7k
    const pixel *src = dst;
849
40.7k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
850
851
40.7k
    if (edges & LR_HAVE_TOP) {
852
26.8k
        sumsq_ptrs[0] = sumsq_rows[0];
853
26.8k
        sumsq_ptrs[1] = sumsq_rows[0];
854
26.8k
        sumsq_ptrs[2] = sumsq_rows[1];
855
26.8k
        sumsq_ptrs[3] = sumsq_rows[2];
856
26.8k
        sumsq_ptrs[4] = sumsq_rows[3];
857
26.8k
        sum_ptrs[0] = sum_rows[0];
858
26.8k
        sum_ptrs[1] = sum_rows[0];
859
26.8k
        sum_ptrs[2] = sum_rows[1];
860
26.8k
        sum_ptrs[3] = sum_rows[2];
861
26.8k
        sum_ptrs[4] = sum_rows[3];
862
863
26.8k
        sgr_box5_row_h(sumsq_rows[0], sum_rows[0], NULL, lpf, w, edges);
864
26.8k
        lpf += PXSTRIDE(stride);
865
26.8k
        sgr_box5_row_h(sumsq_rows[1], sum_rows[1], NULL, lpf, w, edges);
866
867
26.8k
        sgr_box5_row_h(sumsq_rows[2], sum_rows[2], left, src, w, edges);
868
26.8k
        left++;
869
26.8k
        src += PXSTRIDE(stride);
870
871
26.8k
        if (--h <= 0)
872
466
            goto vert_1;
873
874
26.3k
        sgr_box5_row_h(sumsq_rows[3], sum_rows[3], left, src, w, edges);
875
26.3k
        left++;
876
26.3k
        src += PXSTRIDE(stride);
877
26.3k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
878
26.3k
                      w, params->sgr.s0, BITDEPTH_MAX);
879
26.3k
        rotate(A_ptrs, B_ptrs, 2);
880
881
26.3k
        if (--h <= 0)
882
511
            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
25.8k
        sumsq_ptrs[3] = sumsq_rows[4];
887
25.8k
        sum_ptrs[3] = sum_rows[4];
888
25.8k
    } else {
889
13.9k
        sumsq_ptrs[0] = sumsq_rows[0];
890
13.9k
        sumsq_ptrs[1] = sumsq_rows[0];
891
13.9k
        sumsq_ptrs[2] = sumsq_rows[0];
892
13.9k
        sumsq_ptrs[3] = sumsq_rows[0];
893
13.9k
        sumsq_ptrs[4] = sumsq_rows[0];
894
13.9k
        sum_ptrs[0] = sum_rows[0];
895
13.9k
        sum_ptrs[1] = sum_rows[0];
896
13.9k
        sum_ptrs[2] = sum_rows[0];
897
13.9k
        sum_ptrs[3] = sum_rows[0];
898
13.9k
        sum_ptrs[4] = sum_rows[0];
899
900
13.9k
        sgr_box5_row_h(sumsq_rows[0], sum_rows[0], left, src, w, edges);
901
13.9k
        left++;
902
13.9k
        src += PXSTRIDE(stride);
903
904
13.9k
        if (--h <= 0)
905
1.52k
            goto vert_1;
906
907
12.3k
        sumsq_ptrs[4] = sumsq_rows[1];
908
12.3k
        sum_ptrs[4] = sum_rows[1];
909
910
12.3k
        sgr_box5_row_h(sumsq_rows[1], sum_rows[1], left, src, w, edges);
911
12.3k
        left++;
912
12.3k
        src += PXSTRIDE(stride);
913
914
12.3k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
915
12.3k
                      w, params->sgr.s0, BITDEPTH_MAX);
916
12.3k
        rotate(A_ptrs, B_ptrs, 2);
917
918
12.3k
        if (--h <= 0)
919
1.67k
            goto vert_2;
920
921
10.7k
        sumsq_ptrs[3] = sumsq_rows[2];
922
10.7k
        sumsq_ptrs[4] = sumsq_rows[3];
923
10.7k
        sum_ptrs[3] = sum_rows[2];
924
10.7k
        sum_ptrs[4] = sum_rows[3];
925
926
10.7k
        sgr_box5_row_h(sumsq_rows[2], sum_rows[2], left, src, w, edges);
927
10.7k
        left++;
928
10.7k
        src += PXSTRIDE(stride);
929
930
10.7k
        if (--h <= 0)
931
937
            goto odd;
932
933
9.78k
        sgr_box5_row_h(sumsq_rows[3], sum_rows[3], left, src, w, edges);
934
9.78k
        left++;
935
9.78k
        src += PXSTRIDE(stride);
936
937
9.78k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
938
9.78k
                      w, params->sgr.s0, BITDEPTH_MAX);
939
9.78k
        sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
940
9.78k
                    w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
941
942
9.78k
        if (--h <= 0)
943
904
            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
8.88k
        sumsq_ptrs[3] = sumsq_rows[4];
948
8.88k
        sum_ptrs[3] = sum_rows[4];
949
8.88k
    }
950
951
748k
    do {
952
748k
        sgr_box5_row_h(sumsq_ptrs[3], sum_ptrs[3], left, src, w, edges);
953
748k
        left++;
954
748k
        src += PXSTRIDE(stride);
955
956
748k
        if (--h <= 0)
957
4.87k
            goto odd;
958
959
743k
        sgr_box5_row_h(sumsq_ptrs[4], sum_ptrs[4], left, src, w, edges);
960
743k
        left++;
961
743k
        src += PXSTRIDE(stride);
962
963
743k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
964
743k
                      w, params->sgr.s0, BITDEPTH_MAX);
965
743k
        sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
966
743k
                    w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
967
743k
    } while (--h > 0);
968
969
29.8k
    if (!(edges & LR_HAVE_BOTTOM))
970
3.82k
        goto vert_2;
971
972
26.0k
    sgr_box5_row_h(sumsq_ptrs[3], sum_ptrs[3], NULL, lpf_bottom, w, edges);
973
26.0k
    lpf_bottom += PXSTRIDE(stride);
974
26.0k
    sgr_box5_row_h(sumsq_ptrs[4], sum_ptrs[4], NULL, lpf_bottom, w, edges);
975
976
32.9k
output_2:
977
32.9k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
978
32.9k
                  w, params->sgr.s0, BITDEPTH_MAX);
979
32.9k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
980
32.9k
                w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
981
32.9k
    return;
982
983
6.91k
vert_2:
984
    // Duplicate the last row twice more
985
6.91k
    sumsq_ptrs[3] = sumsq_ptrs[2];
986
6.91k
    sumsq_ptrs[4] = sumsq_ptrs[2];
987
6.91k
    sum_ptrs[3] = sum_ptrs[2];
988
6.91k
    sum_ptrs[4] = sum_ptrs[2];
989
6.91k
    goto output_2;
990
991
5.80k
odd:
992
    // Copy the last row as padding once
993
5.80k
    sumsq_ptrs[4] = sumsq_ptrs[3];
994
5.80k
    sum_ptrs[4] = sum_ptrs[3];
995
996
5.80k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
997
5.80k
                  w, params->sgr.s0, BITDEPTH_MAX);
998
5.80k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
999
5.80k
                w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
1000
1001
7.80k
output_1:
1002
    // Duplicate the last row twice more
1003
7.80k
    sumsq_ptrs[3] = sumsq_ptrs[2];
1004
7.80k
    sumsq_ptrs[4] = sumsq_ptrs[2];
1005
7.80k
    sum_ptrs[3] = sum_ptrs[2];
1006
7.80k
    sum_ptrs[4] = sum_ptrs[2];
1007
1008
7.80k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
1009
7.80k
                  w, params->sgr.s0, BITDEPTH_MAX);
1010
    // Output only one row
1011
7.80k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
1012
7.80k
                w, 1, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
1013
7.80k
    return;
1014
1015
1.99k
vert_1:
1016
    // Copy the last row as padding once
1017
1.99k
    sumsq_ptrs[4] = sumsq_ptrs[3];
1018
1.99k
    sum_ptrs[4] = sum_ptrs[3];
1019
1020
1.99k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
1021
1.99k
                  w, params->sgr.s0, BITDEPTH_MAX);
1022
1.99k
    rotate(A_ptrs, B_ptrs, 2);
1023
1024
1.99k
    goto output_1;
1025
5.80k
}
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
92.8k
{
1033
92.8k
    ALIGN_STK_16(int32_t, sumsq5_buf, BUF_STRIDE * 5 + 16,);
1034
92.8k
    ALIGN_STK_16(coef, sum5_buf, BUF_STRIDE * 5 + 16,);
1035
92.8k
    int32_t *sumsq5_ptrs[5], *sumsq5_rows[5];
1036
92.8k
    coef *sum5_ptrs[5], *sum5_rows[5];
1037
557k
    for (int i = 0; i < 5; i++) {
1038
464k
        sumsq5_rows[i] = &sumsq5_buf[i * BUF_STRIDE];
1039
464k
        sum5_rows[i] = &sum5_buf[i * BUF_STRIDE];
1040
464k
    }
1041
92.8k
    ALIGN_STK_16(int32_t, sumsq3_buf, BUF_STRIDE * 3 + 16,);
1042
92.8k
    ALIGN_STK_16(coef, sum3_buf, BUF_STRIDE * 3 + 16,);
1043
92.8k
    int32_t *sumsq3_ptrs[3], *sumsq3_rows[3];
1044
92.8k
    coef *sum3_ptrs[3], *sum3_rows[3];
1045
371k
    for (int i = 0; i < 3; i++) {
1046
278k
        sumsq3_rows[i] = &sumsq3_buf[i * BUF_STRIDE];
1047
278k
        sum3_rows[i] = &sum3_buf[i * BUF_STRIDE];
1048
278k
    }
1049
1050
92.8k
    ALIGN_STK_16(int32_t, A5_buf, BUF_STRIDE * 2 + 16,);
1051
92.8k
    ALIGN_STK_16(coef, B5_buf, BUF_STRIDE * 2 + 16,);
1052
92.8k
    int32_t *A5_ptrs[2];
1053
92.8k
    coef *B5_ptrs[2];
1054
278k
    for (int i = 0; i < 2; i++) {
1055
185k
        A5_ptrs[i] = &A5_buf[i * BUF_STRIDE];
1056
185k
        B5_ptrs[i] = &B5_buf[i * BUF_STRIDE];
1057
185k
    }
1058
92.8k
    ALIGN_STK_16(int32_t, A3_buf, BUF_STRIDE * 4 + 16,);
1059
92.8k
    ALIGN_STK_16(coef, B3_buf, BUF_STRIDE * 4 + 16,);
1060
92.8k
    int32_t *A3_ptrs[4];
1061
92.8k
    coef *B3_ptrs[4];
1062
464k
    for (int i = 0; i < 4; i++) {
1063
371k
        A3_ptrs[i] = &A3_buf[i * BUF_STRIDE];
1064
371k
        B3_ptrs[i] = &B3_buf[i * BUF_STRIDE];
1065
371k
    }
1066
92.8k
    const pixel *src = dst;
1067
92.8k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
1068
1069
92.8k
    if (edges & LR_HAVE_TOP) {
1070
64.6k
        sumsq5_ptrs[0] = sumsq5_rows[0];
1071
64.6k
        sumsq5_ptrs[1] = sumsq5_rows[0];
1072
64.6k
        sumsq5_ptrs[2] = sumsq5_rows[1];
1073
64.6k
        sumsq5_ptrs[3] = sumsq5_rows[2];
1074
64.6k
        sumsq5_ptrs[4] = sumsq5_rows[3];
1075
64.6k
        sum5_ptrs[0] = sum5_rows[0];
1076
64.6k
        sum5_ptrs[1] = sum5_rows[0];
1077
64.6k
        sum5_ptrs[2] = sum5_rows[1];
1078
64.6k
        sum5_ptrs[3] = sum5_rows[2];
1079
64.6k
        sum5_ptrs[4] = sum5_rows[3];
1080
1081
64.6k
        sumsq3_ptrs[0] = sumsq3_rows[0];
1082
64.6k
        sumsq3_ptrs[1] = sumsq3_rows[1];
1083
64.6k
        sumsq3_ptrs[2] = sumsq3_rows[2];
1084
64.6k
        sum3_ptrs[0] = sum3_rows[0];
1085
64.6k
        sum3_ptrs[1] = sum3_rows[1];
1086
64.6k
        sum3_ptrs[2] = sum3_rows[2];
1087
1088
64.6k
        sgr_box35_row_h(sumsq3_rows[0], sum3_rows[0],
1089
64.6k
                        sumsq5_rows[0], sum5_rows[0],
1090
64.6k
                        NULL, lpf, w, edges);
1091
64.6k
        lpf += PXSTRIDE(stride);
1092
64.6k
        sgr_box35_row_h(sumsq3_rows[1], sum3_rows[1],
1093
64.6k
                        sumsq5_rows[1], sum5_rows[1],
1094
64.6k
                        NULL, lpf, w, edges);
1095
1096
64.6k
        sgr_box35_row_h(sumsq3_rows[2], sum3_rows[2],
1097
64.6k
                        sumsq5_rows[2], sum5_rows[2],
1098
64.6k
                        left, src, w, edges);
1099
64.6k
        left++;
1100
64.6k
        src += PXSTRIDE(stride);
1101
1102
64.6k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1103
64.6k
                      w, params->sgr.s1, BITDEPTH_MAX);
1104
64.6k
        rotate(A3_ptrs, B3_ptrs, 4);
1105
1106
64.6k
        if (--h <= 0)
1107
959
            goto vert_1;
1108
1109
63.6k
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1110
63.6k
                        sumsq5_rows[3], sum5_rows[3],
1111
63.6k
                        left, src, w, edges);
1112
63.6k
        left++;
1113
63.6k
        src += PXSTRIDE(stride);
1114
63.6k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1115
63.6k
                      w, params->sgr.s0, BITDEPTH_MAX);
1116
63.6k
        rotate(A5_ptrs, B5_ptrs, 2);
1117
63.6k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1118
63.6k
                      w, params->sgr.s1, BITDEPTH_MAX);
1119
63.6k
        rotate(A3_ptrs, B3_ptrs, 4);
1120
1121
63.6k
        if (--h <= 0)
1122
786
            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
62.9k
        sumsq5_ptrs[3] = sumsq5_rows[4];
1127
62.9k
        sum5_ptrs[3] = sum5_rows[4];
1128
62.9k
    } else {
1129
28.2k
        sumsq5_ptrs[0] = sumsq5_rows[0];
1130
28.2k
        sumsq5_ptrs[1] = sumsq5_rows[0];
1131
28.2k
        sumsq5_ptrs[2] = sumsq5_rows[0];
1132
28.2k
        sumsq5_ptrs[3] = sumsq5_rows[0];
1133
28.2k
        sumsq5_ptrs[4] = sumsq5_rows[0];
1134
28.2k
        sum5_ptrs[0] = sum5_rows[0];
1135
28.2k
        sum5_ptrs[1] = sum5_rows[0];
1136
28.2k
        sum5_ptrs[2] = sum5_rows[0];
1137
28.2k
        sum5_ptrs[3] = sum5_rows[0];
1138
28.2k
        sum5_ptrs[4] = sum5_rows[0];
1139
1140
28.2k
        sumsq3_ptrs[0] = sumsq3_rows[0];
1141
28.2k
        sumsq3_ptrs[1] = sumsq3_rows[0];
1142
28.2k
        sumsq3_ptrs[2] = sumsq3_rows[0];
1143
28.2k
        sum3_ptrs[0] = sum3_rows[0];
1144
28.2k
        sum3_ptrs[1] = sum3_rows[0];
1145
28.2k
        sum3_ptrs[2] = sum3_rows[0];
1146
1147
28.2k
        sgr_box35_row_h(sumsq3_rows[0], sum3_rows[0],
1148
28.2k
                        sumsq5_rows[0], sum5_rows[0],
1149
28.2k
                        left, src, w, edges);
1150
28.2k
        left++;
1151
28.2k
        src += PXSTRIDE(stride);
1152
1153
28.2k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1154
28.2k
                      w, params->sgr.s1, BITDEPTH_MAX);
1155
28.2k
        rotate(A3_ptrs, B3_ptrs, 4);
1156
1157
28.2k
        if (--h <= 0)
1158
5.57k
            goto vert_1;
1159
1160
22.6k
        sumsq5_ptrs[4] = sumsq5_rows[1];
1161
22.6k
        sum5_ptrs[4] = sum5_rows[1];
1162
1163
22.6k
        sumsq3_ptrs[2] = sumsq3_rows[1];
1164
22.6k
        sum3_ptrs[2] = sum3_rows[1];
1165
1166
22.6k
        sgr_box35_row_h(sumsq3_rows[1], sum3_rows[1],
1167
22.6k
                        sumsq5_rows[1], sum5_rows[1],
1168
22.6k
                        left, src, w, edges);
1169
22.6k
        left++;
1170
22.6k
        src += PXSTRIDE(stride);
1171
1172
22.6k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1173
22.6k
                      w, params->sgr.s0, BITDEPTH_MAX);
1174
22.6k
        rotate(A5_ptrs, B5_ptrs, 2);
1175
22.6k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1176
22.6k
                      w, params->sgr.s1, BITDEPTH_MAX);
1177
22.6k
        rotate(A3_ptrs, B3_ptrs, 4);
1178
1179
22.6k
        if (--h <= 0)
1180
3.93k
            goto vert_2;
1181
1182
18.7k
        sumsq5_ptrs[3] = sumsq5_rows[2];
1183
18.7k
        sumsq5_ptrs[4] = sumsq5_rows[3];
1184
18.7k
        sum5_ptrs[3] = sum5_rows[2];
1185
18.7k
        sum5_ptrs[4] = sum5_rows[3];
1186
1187
18.7k
        sumsq3_ptrs[2] = sumsq3_rows[2];
1188
18.7k
        sum3_ptrs[2] = sum3_rows[2];
1189
1190
18.7k
        sgr_box35_row_h(sumsq3_rows[2], sum3_rows[2],
1191
18.7k
                        sumsq5_rows[2], sum5_rows[2],
1192
18.7k
                        left, src, w, edges);
1193
18.7k
        left++;
1194
18.7k
        src += PXSTRIDE(stride);
1195
1196
18.7k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1197
18.7k
                      w, params->sgr.s1, BITDEPTH_MAX);
1198
18.7k
        rotate(A3_ptrs, B3_ptrs, 4);
1199
1200
18.7k
        if (--h <= 0)
1201
2.15k
            goto odd;
1202
1203
16.5k
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1204
16.5k
                        sumsq5_rows[3], sum5_rows[3],
1205
16.5k
                        left, src, w, edges);
1206
16.5k
        left++;
1207
16.5k
        src += PXSTRIDE(stride);
1208
1209
16.5k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1210
16.5k
                      w, params->sgr.s0, BITDEPTH_MAX);
1211
16.5k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1212
16.5k
                      w, params->sgr.s1, BITDEPTH_MAX);
1213
16.5k
        sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1214
16.5k
                       w, 2, params->sgr.w0, params->sgr.w1
1215
16.5k
                       HIGHBD_TAIL_SUFFIX);
1216
1217
16.5k
        if (--h <= 0)
1218
1.74k
            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
14.8k
        sumsq5_ptrs[3] = sumsq5_rows[4];
1223
14.8k
        sum5_ptrs[3] = sum5_rows[4];
1224
14.8k
    }
1225
1226
1.69M
    do {
1227
1.69M
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1228
1.69M
                        sumsq5_ptrs[3], sum5_ptrs[3],
1229
1.69M
                        left, src, w, edges);
1230
1.69M
        left++;
1231
1.69M
        src += PXSTRIDE(stride);
1232
1233
1.69M
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1234
1.69M
                      w, params->sgr.s1, BITDEPTH_MAX);
1235
1.69M
        rotate(A3_ptrs, B3_ptrs, 4);
1236
1237
1.69M
        if (--h <= 0)
1238
11.5k
            goto odd;
1239
1240
1.68M
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1241
1.68M
                        sumsq5_ptrs[4], sum5_ptrs[4],
1242
1.68M
                        left, src, w, edges);
1243
1.68M
        left++;
1244
1.68M
        src += PXSTRIDE(stride);
1245
1246
1.68M
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1247
1.68M
                      w, params->sgr.s0, BITDEPTH_MAX);
1248
1.68M
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1249
1.68M
                      w, params->sgr.s1, BITDEPTH_MAX);
1250
1.68M
        sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1251
1.68M
                       w, 2, params->sgr.w0, params->sgr.w1
1252
1.68M
                       HIGHBD_TAIL_SUFFIX);
1253
1.68M
    } while (--h > 0);
1254
1255
66.1k
    if (!(edges & LR_HAVE_BOTTOM))
1256
7.20k
        goto vert_2;
1257
1258
58.9k
    sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1259
58.9k
                    sumsq5_ptrs[3], sum5_ptrs[3],
1260
58.9k
                    NULL, lpf_bottom, w, edges);
1261
58.9k
    lpf_bottom += PXSTRIDE(stride);
1262
58.9k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1263
58.9k
                  w, params->sgr.s1, BITDEPTH_MAX);
1264
58.9k
    rotate(A3_ptrs, B3_ptrs, 4);
1265
1266
58.9k
    sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1267
58.9k
                    sumsq5_ptrs[4], sum5_ptrs[4],
1268
58.9k
                    NULL, lpf_bottom, w, edges);
1269
1270
72.5k
output_2:
1271
72.5k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1272
72.5k
                  w, params->sgr.s0, BITDEPTH_MAX);
1273
72.5k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1274
72.5k
                  w, params->sgr.s1, BITDEPTH_MAX);
1275
72.5k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1276
72.5k
                   w, 2, params->sgr.w0, params->sgr.w1
1277
72.5k
                   HIGHBD_TAIL_SUFFIX);
1278
72.5k
    return;
1279
1280
13.6k
vert_2:
1281
    // Duplicate the last row twice more
1282
13.6k
    sumsq5_ptrs[3] = sumsq5_ptrs[2];
1283
13.6k
    sumsq5_ptrs[4] = sumsq5_ptrs[2];
1284
13.6k
    sum5_ptrs[3] = sum5_ptrs[2];
1285
13.6k
    sum5_ptrs[4] = sum5_ptrs[2];
1286
1287
13.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1288
13.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1289
13.6k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1290
13.6k
                  w, params->sgr.s1, BITDEPTH_MAX);
1291
13.6k
    rotate(A3_ptrs, B3_ptrs, 4);
1292
1293
13.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1294
13.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1295
1296
13.6k
    goto output_2;
1297
1298
13.6k
odd:
1299
    // Copy the last row as padding once
1300
13.6k
    sumsq5_ptrs[4] = sumsq5_ptrs[3];
1301
13.6k
    sum5_ptrs[4] = sum5_ptrs[3];
1302
1303
13.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1304
13.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1305
1306
13.6k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1307
13.6k
                  w, params->sgr.s0, BITDEPTH_MAX);
1308
13.6k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1309
13.6k
                  w, params->sgr.s1, BITDEPTH_MAX);
1310
13.6k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1311
13.6k
                   w, 2, params->sgr.w0, params->sgr.w1
1312
13.6k
                   HIGHBD_TAIL_SUFFIX);
1313
1314
20.2k
output_1:
1315
    // Duplicate the last row twice more
1316
20.2k
    sumsq5_ptrs[3] = sumsq5_ptrs[2];
1317
20.2k
    sumsq5_ptrs[4] = sumsq5_ptrs[2];
1318
20.2k
    sum5_ptrs[3] = sum5_ptrs[2];
1319
20.2k
    sum5_ptrs[4] = sum5_ptrs[2];
1320
1321
20.2k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1322
20.2k
    sum3_ptrs[2] = sum3_ptrs[1];
1323
1324
20.2k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1325
20.2k
                  w, params->sgr.s0, BITDEPTH_MAX);
1326
20.2k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1327
20.2k
                  w, params->sgr.s1, BITDEPTH_MAX);
1328
20.2k
    rotate(A3_ptrs, B3_ptrs, 4);
1329
    // Output only one row
1330
20.2k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1331
20.2k
                   w, 1, params->sgr.w0, params->sgr.w1
1332
20.2k
                   HIGHBD_TAIL_SUFFIX);
1333
20.2k
    return;
1334
1335
6.53k
vert_1:
1336
    // Copy the last row as padding once
1337
6.53k
    sumsq5_ptrs[4] = sumsq5_ptrs[3];
1338
6.53k
    sum5_ptrs[4] = sum5_ptrs[3];
1339
1340
6.53k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1341
6.53k
    sum3_ptrs[2] = sum3_ptrs[1];
1342
1343
6.53k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1344
6.53k
                  w, params->sgr.s0, BITDEPTH_MAX);
1345
6.53k
    rotate(A5_ptrs, B5_ptrs, 2);
1346
6.53k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1347
6.53k
                  w, params->sgr.s1, BITDEPTH_MAX);
1348
6.53k
    rotate(A3_ptrs, B3_ptrs, 4);
1349
1350
6.53k
    goto output_1;
1351
13.6k
}
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
78.8k
{
1368
78.8k
    c->wiener[0] = c->wiener[1] = wiener_c;
1369
78.8k
    c->sgr[0] = sgr_5x5_c;
1370
78.8k
    c->sgr[1] = sgr_3x3_c;
1371
78.8k
    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
78.8k
}
dav1d_loop_restoration_dsp_init_8bpc
Line
Count
Source
1367
35.6k
{
1368
35.6k
    c->wiener[0] = c->wiener[1] = wiener_c;
1369
35.6k
    c->sgr[0] = sgr_5x5_c;
1370
35.6k
    c->sgr[1] = sgr_3x3_c;
1371
35.6k
    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
35.6k
}
dav1d_loop_restoration_dsp_init_16bpc
Line
Count
Source
1367
43.1k
{
1368
43.1k
    c->wiener[0] = c->wiener[1] = wiener_c;
1369
43.1k
    c->sgr[0] = sgr_5x5_c;
1370
43.1k
    c->sgr[1] = sgr_3x3_c;
1371
43.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
43.1k
}