Coverage Report

Created: 2026-07-30 06:27

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