Coverage Report

Created: 2026-07-16 06:32

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