Coverage Report

Created: 2026-12-01 06:52

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
9.18M
#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
8.49M
{
49
8.49M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
50
8.49M
    const int round_bits_h = 3 + (bitdepth == 12) * 2;
51
8.49M
    const int rounding_off_h = 1 << (round_bits_h - 1);
52
8.49M
    const int clip_limit = 1 << (bitdepth + 1 + 7 - round_bits_h);
53
54
8.49M
    if (w < 6) {
55
        // For small widths, do the fully conditional loop with
56
        // conditions on each access.
57
6.11M
        for (int x = 0; x < w; x++) {
58
3.86M
            int sum = (1 << (bitdepth + 6));
59
3.86M
#if BITDEPTH == 8
60
3.86M
            sum += src[x] * 128;
61
3.86M
#endif
62
29.3M
            for (int i = 0; i < 7; i++) {
63
25.4M
                int idx = x + i - 3;
64
25.4M
                if (idx < 0) {
65
8.89M
                    if (!(edges & LR_HAVE_LEFT))
66
8.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
16.5M
                } else if (idx >= w && !(edges & LR_HAVE_RIGHT)) {
72
8.06M
                    sum += src[w - 1] * fh[i];
73
8.06M
                } else
74
8.51M
                    sum += src[idx] * fh[i];
75
25.4M
            }
76
3.86M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
77
3.86M
            dst[x] = sum;
78
3.86M
        }
79
80
2.25M
        return;
81
2.25M
    }
82
83
    // For larger widths, do separate loops with less conditions; first
84
    // handle the start of the row.
85
6.24M
    int start = 3;
86
6.24M
    if (!(edges & LR_HAVE_LEFT)) {
87
        // If there's no left edge, pad using the leftmost pixel.
88
16.3M
        for (int x = 0; x < 3; x++) {
89
12.2M
            int sum = (1 << (bitdepth + 6));
90
12.2M
#if BITDEPTH == 8
91
12.2M
            sum += src[x] * 128;
92
12.2M
#endif
93
97.6M
            for (int i = 0; i < 7; i++) {
94
85.4M
                int idx = x + i - 3;
95
85.4M
                if (idx < 0)
96
24.4M
                    sum += src[0] * fh[i];
97
60.9M
                else
98
60.9M
                    sum += src[idx] * fh[i];
99
85.4M
            }
100
12.2M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
101
12.2M
            dst[x] = sum;
102
12.2M
        }
103
4.14M
    } else if (left) {
104
        // If we have the left edge and a separate left buffer, pad using that.
105
7.90M
        for (int x = 0; x < 3; x++) {
106
5.92M
            int sum = (1 << (bitdepth + 6));
107
5.92M
#if BITDEPTH == 8
108
5.92M
            sum += src[x] * 128;
109
5.92M
#endif
110
47.2M
            for (int i = 0; i < 7; i++) {
111
41.3M
                int idx = x + i - 3;
112
41.3M
                if (idx < 0)
113
11.8M
                    sum += left[0][4 + idx] * fh[i];
114
29.5M
                else
115
29.5M
                    sum += src[idx] * fh[i];
116
41.3M
            }
117
5.92M
            sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
118
5.92M
            dst[x] = sum;
119
5.92M
        }
120
1.98M
    } 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
109k
        start = 0;
125
109k
    }
126
6.24M
    int end = w - 3;
127
6.24M
    if (edges & LR_HAVE_RIGHT)
128
2.12M
        end = w;
129
130
    // Do a condititon free loop for the bulk of the row.
131
391M
    for (int x = start; x < end; x++) {
132
384M
        int sum = (1 << (bitdepth + 6));
133
384M
#if BITDEPTH == 8
134
384M
        sum += src[x] * 128;
135
384M
#endif
136
3.06G
        for (int i = 0; i < 7; i++) {
137
2.67G
            int idx = x + i - 3;
138
2.67G
            sum += src[idx] * fh[i];
139
2.67G
        }
140
384M
        sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
141
384M
        dst[x] = sum;
142
384M
    }
143
144
    // If we need to, calculate the end of the row with a condition for
145
    // right edge padding.
146
18.1M
    for (int x = end; x < w; x++) {
147
11.9M
        int sum = (1 << (bitdepth + 6));
148
11.9M
#if BITDEPTH == 8
149
11.9M
        sum += src[x] * 128;
150
11.9M
#endif
151
94.4M
        for (int i = 0; i < 7; i++) {
152
82.5M
            int idx = x + i - 3;
153
82.5M
            if (idx >= w)
154
23.5M
                sum += src[w - 1] * fh[i];
155
58.9M
            else
156
58.9M
                sum += src[idx] * fh[i];
157
82.5M
        }
158
11.9M
        sum = iclip((sum + rounding_off_h) >> round_bits_h, 0, clip_limit - 1);
159
11.9M
        dst[x] = sum;
160
11.9M
    }
161
6.24M
}
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
346k
{
166
346k
    const int bitdepth = bitdepth_from_max(bitdepth_max);
167
168
346k
    const int round_bits_v = 11 - (bitdepth == 12) * 2;
169
346k
    const int rounding_off_v = 1 << (round_bits_v - 1);
170
346k
    const int round_offset = 1 << (bitdepth + (round_bits_v - 1));
171
172
21.6M
    for (int i = 0; i < w; i++) {
173
21.3M
        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
149M
        for (int k = 0; k < 6; k++)
181
127M
            sum += ptrs[k][i] * fv[k];
182
21.3M
        sum += ptrs[5][i] * fv[6];
183
184
21.3M
        p[i] = iclip_pixel((sum + rounding_off_v) >> round_bits_v);
185
21.3M
    }
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
2.07M
    for (int i = 0; i < 5; i++)
190
1.73M
        ptrs[i] = ptrs[i + 1];
191
346k
}
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
7.63M
{
198
7.63M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
199
200
7.63M
    const int round_bits_v = 11 - (bitdepth == 12) * 2;
201
7.63M
    const int rounding_off_v = 1 << (round_bits_v - 1);
202
7.63M
    const int round_offset = 1 << (bitdepth + (round_bits_v - 1));
203
204
7.63M
    const int16_t *fh = filter[0];
205
7.63M
    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
7.63M
    uint16_t tmp[REST_UNIT_STRIDE];
214
7.63M
    wiener_filter_h(tmp, left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
215
216
378M
    for (int i = 0; i < w; i++) {
217
371M
        int sum = -round_offset;
218
219
        // Filter using the 6 stored preexisting rows, and the newly
220
        // filtered one in tmp[].
221
2.58G
        for (int k = 0; k < 6; k++)
222
2.21G
            sum += ptrs[k][i] * fv[k];
223
371M
        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
371M
        p[i] = iclip_pixel((sum + rounding_off_v) >> round_bits_v);
228
371M
    }
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
7.63M
    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
52.4M
    for (int i = 0; i < 6; i++)
240
44.7M
        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
7.63M
    ptrs[6] = ptrs[0];
246
7.63M
}
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
226k
{
256
    // Values stored between horizontal and vertical filtering don't
257
    // fit in a uint8_t.
258
226k
    uint16_t hor[6 * REST_UNIT_STRIDE];
259
226k
    uint16_t *ptrs[7], *rows[6];
260
1.58M
    for (int i = 0; i < 6; i++)
261
1.35M
        rows[i] = &hor[i * REST_UNIT_STRIDE];
262
226k
    const int16_t (*const filter)[8] = params->filter;
263
226k
    const int16_t *fh = params->filter[0];
264
226k
    const int16_t *fv = params->filter[1];
265
226k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
266
267
226k
    const pixel *src = p;
268
226k
    if (edges & LR_HAVE_TOP) {
269
144k
        ptrs[0] = rows[0];
270
144k
        ptrs[1] = rows[0];
271
144k
        ptrs[2] = rows[1];
272
144k
        ptrs[3] = rows[2];
273
144k
        ptrs[4] = rows[2];
274
144k
        ptrs[5] = rows[2];
275
276
144k
        wiener_filter_h(rows[0], NULL, lpf, fh, w, edges HIGHBD_TAIL_SUFFIX);
277
144k
        lpf += PXSTRIDE(stride);
278
144k
        wiener_filter_h(rows[1], NULL, lpf, fh, w, edges HIGHBD_TAIL_SUFFIX);
279
280
144k
        wiener_filter_h(rows[2], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
281
144k
        left++;
282
144k
        src += PXSTRIDE(stride);
283
284
144k
        if (--h <= 0)
285
2.01k
            goto v1;
286
287
142k
        ptrs[4] = ptrs[5] = rows[3];
288
142k
        wiener_filter_h(rows[3], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
289
142k
        left++;
290
142k
        src += PXSTRIDE(stride);
291
292
142k
        if (--h <= 0)
293
1.37k
            goto v2;
294
295
141k
        ptrs[5] = rows[4];
296
141k
        wiener_filter_h(rows[4], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
297
141k
        left++;
298
141k
        src += PXSTRIDE(stride);
299
300
141k
        if (--h <= 0)
301
827
            goto v3;
302
141k
    } else {
303
81.5k
        ptrs[0] = rows[0];
304
81.5k
        ptrs[1] = rows[0];
305
81.5k
        ptrs[2] = rows[0];
306
81.5k
        ptrs[3] = rows[0];
307
81.5k
        ptrs[4] = rows[0];
308
81.5k
        ptrs[5] = rows[0];
309
310
81.5k
        wiener_filter_h(rows[0], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
311
81.5k
        left++;
312
81.5k
        src += PXSTRIDE(stride);
313
314
81.5k
        if (--h <= 0)
315
14.1k
            goto v1;
316
317
67.4k
        ptrs[4] = ptrs[5] = rows[1];
318
67.4k
        wiener_filter_h(rows[1], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
319
67.4k
        left++;
320
67.4k
        src += PXSTRIDE(stride);
321
322
67.4k
        if (--h <= 0)
323
6.83k
            goto v2;
324
325
60.6k
        ptrs[5] = rows[2];
326
60.6k
        wiener_filter_h(rows[2], left, src, fh, w, edges HIGHBD_TAIL_SUFFIX);
327
60.6k
        left++;
328
60.6k
        src += PXSTRIDE(stride);
329
330
60.6k
        if (--h <= 0)
331
4.18k
            goto v3;
332
333
56.4k
        ptrs[6] = rows[3];
334
56.4k
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
335
56.4k
                         HIGHBD_TAIL_SUFFIX);
336
56.4k
        left++;
337
56.4k
        src += PXSTRIDE(stride);
338
56.4k
        p += PXSTRIDE(stride);
339
340
56.4k
        if (--h <= 0)
341
6.03k
            goto v3;
342
343
50.4k
        ptrs[6] = rows[4];
344
50.4k
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
345
50.4k
                         HIGHBD_TAIL_SUFFIX);
346
50.4k
        left++;
347
50.4k
        src += PXSTRIDE(stride);
348
50.4k
        p += PXSTRIDE(stride);
349
350
50.4k
        if (--h <= 0)
351
3.12k
            goto v3;
352
50.4k
    }
353
354
187k
    ptrs[6] = ptrs[5] + REST_UNIT_STRIDE;
355
7.25M
    do {
356
7.25M
        wiener_filter_hv(p, ptrs, left, src, filter, w, edges
357
7.25M
                         HIGHBD_TAIL_SUFFIX);
358
7.25M
        left++;
359
7.25M
        src += PXSTRIDE(stride);
360
7.25M
        p += PXSTRIDE(stride);
361
7.25M
    } while (--h > 0);
362
363
187k
    if (!(edges & LR_HAVE_BOTTOM))
364
42.0k
        goto v3;
365
366
145k
    wiener_filter_hv(p, ptrs, NULL, lpf_bottom, filter, w, edges
367
145k
                     HIGHBD_TAIL_SUFFIX);
368
145k
    lpf_bottom += PXSTRIDE(stride);
369
145k
    p += PXSTRIDE(stride);
370
371
145k
    wiener_filter_hv(p, ptrs, NULL, lpf_bottom, filter, w, edges
372
145k
                     HIGHBD_TAIL_SUFFIX);
373
145k
    p += PXSTRIDE(stride);
374
225k
v1:
375
225k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
376
377
225k
    return;
378
379
56.2k
v3:
380
56.2k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
381
56.2k
    p += PXSTRIDE(stride);
382
64.4k
v2:
383
64.4k
    wiener_filter_v(p, ptrs, fv, w HIGHBD_TAIL_SUFFIX);
384
64.4k
    p += PXSTRIDE(stride);
385
64.4k
    goto v1;
386
56.2k
}
387
388
// SGR
389
static NOINLINE void rotate(int32_t **sumsq_ptrs, coef **sum_ptrs, int n)
390
16.4M
{
391
16.4M
    int32_t *tmp32 = sumsq_ptrs[0];
392
16.4M
    coef *tmpc = sum_ptrs[0];
393
50.3M
    for (int i = 0; i < n - 1; i++) {
394
33.9M
        sumsq_ptrs[i] = sumsq_ptrs[i + 1];
395
33.9M
        sum_ptrs[i] = sum_ptrs[i + 1];
396
33.9M
    }
397
16.4M
    sumsq_ptrs[n - 1] = tmp32;
398
16.4M
    sum_ptrs[n - 1] = tmpc;
399
16.4M
}
400
401
static NOINLINE void rotate5_x2(int32_t **sumsq_ptrs, coef **sum_ptrs)
402
3.45M
{
403
3.45M
    int32_t *tmp32[2];
404
3.45M
    coef *tmpc[2];
405
3.45M
    memcpy(tmp32, sumsq_ptrs, 2 * sizeof(int32_t*));
406
3.45M
    memcpy(tmpc, sum_ptrs, 2 * sizeof(coef*));
407
3.45M
    memmove(&sumsq_ptrs[0], &sumsq_ptrs[2], 3 * sizeof(int32_t*));
408
3.45M
    memmove(&sum_ptrs[0], &sum_ptrs[2], 3 * sizeof(coef*));
409
3.45M
    memcpy(&sumsq_ptrs[3], tmp32, 2 * sizeof(int32_t*));
410
3.45M
    memcpy(&sum_ptrs[3], tmpc, 2 * sizeof(coef*));
411
3.45M
}
412
413
static NOINLINE void sgr_box3_row_h(int32_t *sumsq, coef *sum,
414
                                    const pixel (*left)[4],
415
                                    const pixel *src, const int w,
416
                                    const enum LrEdgeFlags edges)
417
6.68M
{
418
6.68M
    sumsq++;
419
6.68M
    sum++;
420
6.68M
    int a = edges & LR_HAVE_LEFT ? (left ? left[0][2] : src[-2]) : src[0];
421
6.68M
    int b = edges & LR_HAVE_LEFT ? (left ? left[0][3] : src[-1]) : src[0];
422
339M
    for (int x = -1; x < w + 1; x++) {
423
332M
        int c = (x + 1 < w || (edges & LR_HAVE_RIGHT)) ? src[x + 1] : src[w - 1];
424
332M
        sum[x] = a + b + c;
425
332M
        sumsq[x] = a * a + b * b + c * c;
426
332M
        a = b;
427
332M
        b = c;
428
332M
    }
429
6.68M
}
430
431
static NOINLINE void sgr_box5_row_h(int32_t *sumsq, coef *sum,
432
                                    const pixel (*left)[4],
433
                                    const pixel *src, const int w,
434
                                    const enum LrEdgeFlags edges)
435
6.92M
{
436
6.92M
    sumsq++;
437
6.92M
    sum++;
438
6.92M
    int a = edges & LR_HAVE_LEFT ? (left ? left[0][1] : src[-3]) : src[0];
439
6.92M
    int b = edges & LR_HAVE_LEFT ? (left ? left[0][2] : src[-2]) : src[0];
440
6.92M
    int c = edges & LR_HAVE_LEFT ? (left ? left[0][3] : src[-1]) : src[0];
441
6.92M
    int d = src[0];
442
388M
    for (int x = -1; x < w + 1; x++) {
443
381M
        int e = (x + 2 < w || (edges & LR_HAVE_RIGHT)) ? src[x + 2] : src[w - 1];
444
381M
        sum[x] = a + b + c + d + e;
445
381M
        sumsq[x] = a * a + b * b + c * c + d * d + e * e;
446
381M
        a = b;
447
381M
        b = c;
448
381M
        c = d;
449
381M
        d = e;
450
381M
    }
451
6.92M
}
452
453
static void sgr_box35_row_h(int32_t *sumsq3, coef *sum3,
454
                            int32_t *sumsq5, coef *sum5,
455
                            const pixel (*left)[4],
456
                            const pixel *src, const int w,
457
                            const enum LrEdgeFlags edges)
458
4.85M
{
459
4.85M
    sgr_box3_row_h(sumsq3, sum3, left, src, w, edges);
460
4.85M
    sgr_box5_row_h(sumsq5, sum5, left, src, w, edges);
461
4.85M
}
462
463
static NOINLINE void sgr_box3_row_v(int32_t **sumsq, coef **sum,
464
                                    int32_t *sumsq_out, coef *sum_out,
465
                                    const int w)
466
6.62M
{
467
339M
    for (int x = 0; x < w + 2; x++) {
468
333M
        int sq_a = sumsq[0][x];
469
333M
        int sq_b = sumsq[1][x];
470
333M
        int sq_c = sumsq[2][x];
471
333M
        int s_a = sum[0][x];
472
333M
        int s_b = sum[1][x];
473
333M
        int s_c = sum[2][x];
474
333M
        sumsq_out[x] = sq_a + sq_b + sq_c;
475
333M
        sum_out[x] = s_a + s_b + s_c;
476
333M
    }
477
6.62M
}
478
479
static NOINLINE void sgr_box5_row_v(int32_t **sumsq, coef **sum,
480
                                    int32_t *sumsq_out, coef *sum_out,
481
                                    const int w)
482
3.45M
{
483
197M
    for (int x = 0; x < w + 2; x++) {
484
194M
        int sq_a = sumsq[0][x];
485
194M
        int sq_b = sumsq[1][x];
486
194M
        int sq_c = sumsq[2][x];
487
194M
        int sq_d = sumsq[3][x];
488
194M
        int sq_e = sumsq[4][x];
489
194M
        int s_a = sum[0][x];
490
194M
        int s_b = sum[1][x];
491
194M
        int s_c = sum[2][x];
492
194M
        int s_d = sum[3][x];
493
194M
        int s_e = sum[4][x];
494
194M
        sumsq_out[x] = sq_a + sq_b + sq_c + sq_d + sq_e;
495
194M
        sum_out[x] = s_a + s_b + s_c + s_d + s_e;
496
194M
    }
497
3.45M
}
498
499
static NOINLINE void sgr_calc_row_ab(int32_t *AA, coef *BB, int w, int s,
500
                                     int bitdepth_max, int n, int sgr_one_by_x)
501
10.0M
{
502
10.0M
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
503
525M
    for (int i = 0; i < w + 2; i++) {
504
515M
        const int a =
505
515M
            (AA[i] + ((1 << (2 * bitdepth_min_8)) >> 1)) >> (2 * bitdepth_min_8);
506
515M
        const int b =
507
515M
            (BB[i] + ((1 << bitdepth_min_8) >> 1)) >> bitdepth_min_8;
508
509
515M
        const unsigned p = imax(a * n - b * b, 0);
510
515M
        const unsigned z = (p * s + (1 << 19)) >> 20;
511
515M
        const unsigned x = dav1d_sgr_x_by_x[umin(z, 255)];
512
513
        // This is where we invert A and B, so that B is of size coef.
514
515M
        AA[i] = (x * BB[i] * sgr_one_by_x + (1 << 11)) >> 12;
515
515M
        BB[i] = x;
516
515M
    }
517
10.0M
}
518
519
static void sgr_box3_vert(int32_t **sumsq, coef **sum,
520
                          int32_t *sumsq_out, coef *sum_out,
521
                          const int w, const int s, const int bitdepth_max)
522
6.63M
{
523
6.63M
    sgr_box3_row_v(sumsq, sum, sumsq_out, sum_out, w);
524
6.63M
    sgr_calc_row_ab(sumsq_out, sum_out, w, s, bitdepth_max, 9, 455);
525
6.63M
    rotate(sumsq, sum, 3);
526
6.63M
}
527
528
static void sgr_box5_vert(int32_t **sumsq, coef **sum,
529
                          int32_t *sumsq_out, coef *sum_out,
530
                          const int w, const int s, const int bitdepth_max)
531
3.46M
{
532
3.46M
    sgr_box5_row_v(sumsq, sum, sumsq_out, sum_out, w);
533
3.46M
    sgr_calc_row_ab(sumsq_out, sum_out, w, s, bitdepth_max, 25, 164);
534
3.46M
    rotate5_x2(sumsq, sum);
535
3.46M
}
536
537
static void sgr_box3_hv(int32_t **sumsq, coef **sum,
538
                        int32_t *AA, coef *BB,
539
                        const pixel (*left)[4],
540
                        const pixel *src, const int w,
541
                        const int s,
542
                        const enum LrEdgeFlags edges,
543
                        const int bitdepth_max)
544
1.79M
{
545
1.79M
    sgr_box3_row_h(sumsq[2], sum[2], left, src, w, edges);
546
1.79M
    sgr_box3_vert(sumsq, sum, AA, BB, w, s, bitdepth_max);
547
1.79M
}
548
549
static NOINLINE void sgr_finish_filter_row1(coef *tmp,
550
                                            const pixel *src,
551
                                            int32_t **A_ptrs, coef **B_ptrs,
552
                                            const int w)
553
6.29M
{
554
6.29M
#define EIGHT_NEIGHBORS(P, i)\
555
596M
    ((P[1][i] + P[1][i - 1] + P[1][i + 1] + P[0][i] + P[2][i]) * 4 + \
556
596M
     (P[0][i - 1] + P[2][i - 1] +                           \
557
596M
      P[0][i + 1] + P[2][i + 1]) * 3)
558
304M
    for (int i = 0; i < w; i++) {
559
298M
        const int a = EIGHT_NEIGHBORS(B_ptrs, i + 1);
560
298M
        const int b = EIGHT_NEIGHBORS(A_ptrs, i + 1);
561
298M
        tmp[i] = (b - a * src[i] + (1 << 8)) >> 9;
562
298M
    }
563
6.29M
#undef EIGHT_NEIGHBORS
564
6.29M
}
565
566
15.6M
#define FILTER_OUT_STRIDE (384)
567
568
static NOINLINE void sgr_finish_filter2(coef *tmp,
569
                                        const pixel *src,
570
                                        const ptrdiff_t src_stride,
571
                                        int32_t **A_ptrs, coef **B_ptrs,
572
                                        const int w, const int h)
573
3.28M
{
574
3.28M
#define SIX_NEIGHBORS(P, i)\
575
341M
    ((P[0][i]     + P[1][i]) * 6 +   \
576
341M
     (P[0][i - 1] + P[1][i - 1] +    \
577
341M
      P[0][i + 1] + P[1][i + 1]) * 5)
578
174M
    for (int i = 0; i < w; i++) {
579
170M
        const int a = SIX_NEIGHBORS(B_ptrs, i + 1);
580
170M
        const int b = SIX_NEIGHBORS(A_ptrs, i + 1);
581
170M
        tmp[i] = (b - a * src[i] + (1 << 8)) >> 9;
582
170M
    }
583
3.28M
    if (h <= 1)
584
33.3k
        return;
585
3.24M
    tmp += FILTER_OUT_STRIDE;
586
3.24M
    src += PXSTRIDE(src_stride);
587
3.24M
    const int32_t *A = &A_ptrs[1][1];
588
3.24M
    const coef *B = &B_ptrs[1][1];
589
172M
    for (int i = 0; i < w; i++) {
590
168M
        const int a = B[i] * 6 + (B[i - 1] + B[i + 1]) * 5;
591
168M
        const int b = A[i] * 6 + (A[i - 1] + A[i + 1]) * 5;
592
168M
        tmp[i] = (b - a * src[i] + (1 << 7)) >> 8;
593
168M
    }
594
3.24M
#undef SIX_NEIGHBORS
595
3.24M
}
596
597
static NOINLINE void sgr_weighted_row1(pixel *dst, const coef *t1,
598
                                       const int w, const int w1 HIGHBD_DECL_SUFFIX)
599
3.69M
{
600
177M
    for (int i = 0; i < w; i++) {
601
174M
        const int v = w1 * t1[i];
602
174M
        dst[i] = iclip_pixel(dst[i] + ((v + (1 << 10)) >> 11));
603
174M
    }
604
3.69M
}
605
606
static NOINLINE void sgr_weighted2(pixel *dst, const ptrdiff_t dst_stride,
607
                                   const coef *t1, const coef *t2,
608
                                   const int w, const int h,
609
                                   const int w0, const int w1 HIGHBD_DECL_SUFFIX)
610
2.30M
{
611
6.85M
    for (int j = 0; j < h; j++) {
612
222M
        for (int i = 0; i < w; i++) {
613
218M
            const int v = w0 * t1[i] + w1 * t2[i];
614
218M
            dst[i] = iclip_pixel(dst[i] + ((v + (1 << 10)) >> 11));
615
218M
        }
616
4.55M
        dst += PXSTRIDE(dst_stride);
617
4.55M
        t1 += FILTER_OUT_STRIDE;
618
4.55M
        t2 += FILTER_OUT_STRIDE;
619
4.55M
    }
620
2.30M
}
621
622
static NOINLINE void sgr_finish1(pixel **dst, const ptrdiff_t stride,
623
                                 int32_t **A_ptrs, coef **B_ptrs, const int w,
624
                                 const int w1 HIGHBD_DECL_SUFFIX)
625
1.75M
{
626
    // Only one single row, no stride needed
627
1.75M
    ALIGN_STK_16(coef, tmp, 384,);
628
629
1.75M
    sgr_finish_filter_row1(tmp, *dst, A_ptrs, B_ptrs, w);
630
1.75M
    sgr_weighted_row1(*dst, tmp, w, w1 HIGHBD_TAIL_SUFFIX);
631
1.75M
    *dst += PXSTRIDE(stride);
632
1.75M
    rotate(A_ptrs, B_ptrs, 3);
633
1.75M
}
634
635
static NOINLINE void sgr_finish2(pixel **dst, const ptrdiff_t stride,
636
                                 int32_t **A_ptrs, coef **B_ptrs,
637
                                 const int w, const int h, const int w1
638
                                 HIGHBD_DECL_SUFFIX)
639
984k
{
640
984k
    ALIGN_STK_16(coef, tmp, 2*FILTER_OUT_STRIDE,);
641
642
984k
    sgr_finish_filter2(tmp, *dst, stride, A_ptrs, B_ptrs, w, h);
643
984k
    sgr_weighted_row1(*dst, tmp, w, w1 HIGHBD_TAIL_SUFFIX);
644
984k
    *dst += PXSTRIDE(stride);
645
984k
    if (h > 1) {
646
968k
        sgr_weighted_row1(*dst, tmp + FILTER_OUT_STRIDE, w, w1 HIGHBD_TAIL_SUFFIX);
647
968k
        *dst += PXSTRIDE(stride);
648
968k
    }
649
984k
    rotate(A_ptrs, B_ptrs, 2);
650
984k
}
651
652
static NOINLINE void sgr_finish_mix(pixel **dst, const ptrdiff_t stride,
653
                                    int32_t **A5_ptrs, coef **B5_ptrs,
654
                                    int32_t **A3_ptrs, coef **B3_ptrs,
655
                                    const int w, const int h,
656
                                    const int w0, const int w1 HIGHBD_DECL_SUFFIX)
657
2.30M
{
658
2.30M
    ALIGN_STK_16(coef, tmp5, 2*FILTER_OUT_STRIDE,);
659
2.30M
    ALIGN_STK_16(coef, tmp3, 2*FILTER_OUT_STRIDE,);
660
661
2.30M
    sgr_finish_filter2(tmp5, *dst, stride, A5_ptrs, B5_ptrs, w, h);
662
2.30M
    sgr_finish_filter_row1(tmp3, *dst, A3_ptrs, B3_ptrs, w);
663
2.30M
    if (h > 1)
664
2.28M
        sgr_finish_filter_row1(tmp3 + FILTER_OUT_STRIDE, *dst + PXSTRIDE(stride),
665
2.28M
                               &A3_ptrs[1], &B3_ptrs[1], w);
666
2.30M
    sgr_weighted2(*dst, stride, tmp5, tmp3, w, h, w0, w1 HIGHBD_TAIL_SUFFIX);
667
2.30M
    *dst += h*PXSTRIDE(stride);
668
2.30M
    rotate(A5_ptrs, B5_ptrs, 2);
669
2.30M
    rotate(A3_ptrs, B3_ptrs, 4);
670
2.30M
}
671
672
673
static void sgr_3x3_c(pixel *dst, const ptrdiff_t stride,
674
                      const pixel (*left)[4], const pixel *lpf,
675
                      const int w, int h,
676
                      const LooprestorationParams *const params,
677
                      const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
678
49.5k
{
679
4.94M
#define BUF_STRIDE (384 + 16)
680
49.5k
    ALIGN_STK_16(int32_t, sumsq_buf, BUF_STRIDE * 3 + 16,);
681
49.5k
    ALIGN_STK_16(coef, sum_buf, BUF_STRIDE * 3 + 16,);
682
49.5k
    int32_t *sumsq_ptrs[3], *sumsq_rows[3];
683
49.5k
    coef *sum_ptrs[3], *sum_rows[3];
684
198k
    for (int i = 0; i < 3; i++) {
685
148k
        sumsq_rows[i] = &sumsq_buf[i * BUF_STRIDE];
686
148k
        sum_rows[i] = &sum_buf[i * BUF_STRIDE];
687
148k
    }
688
689
49.5k
    ALIGN_STK_16(int32_t, A_buf, BUF_STRIDE * 3 + 16,);
690
49.5k
    ALIGN_STK_16(coef, B_buf, BUF_STRIDE * 3 + 16,);
691
49.5k
    int32_t *A_ptrs[3];
692
49.5k
    coef *B_ptrs[3];
693
198k
    for (int i = 0; i < 3; i++) {
694
148k
        A_ptrs[i] = &A_buf[i * BUF_STRIDE];
695
148k
        B_ptrs[i] = &B_buf[i * BUF_STRIDE];
696
148k
    }
697
49.5k
    const pixel *src = dst;
698
49.5k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
699
700
49.5k
    if (edges & LR_HAVE_TOP) {
701
32.6k
        sumsq_ptrs[0] = sumsq_rows[0];
702
32.6k
        sumsq_ptrs[1] = sumsq_rows[1];
703
32.6k
        sumsq_ptrs[2] = sumsq_rows[2];
704
32.6k
        sum_ptrs[0] = sum_rows[0];
705
32.6k
        sum_ptrs[1] = sum_rows[1];
706
32.6k
        sum_ptrs[2] = sum_rows[2];
707
708
32.6k
        sgr_box3_row_h(sumsq_rows[0], sum_rows[0], NULL, lpf, w, edges);
709
32.6k
        lpf += PXSTRIDE(stride);
710
32.6k
        sgr_box3_row_h(sumsq_rows[1], sum_rows[1], NULL, lpf, w, edges);
711
712
32.6k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
713
32.6k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
714
32.6k
        left++;
715
32.6k
        src += PXSTRIDE(stride);
716
32.6k
        rotate(A_ptrs, B_ptrs, 3);
717
718
32.6k
        if (--h <= 0)
719
823
            goto vert_1;
720
721
31.8k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
722
31.8k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
723
31.8k
        left++;
724
31.8k
        src += PXSTRIDE(stride);
725
31.8k
        rotate(A_ptrs, B_ptrs, 3);
726
727
31.8k
        if (--h <= 0)
728
703
            goto vert_2;
729
31.8k
    } else {
730
16.8k
        sumsq_ptrs[0] = sumsq_rows[0];
731
16.8k
        sumsq_ptrs[1] = sumsq_rows[0];
732
16.8k
        sumsq_ptrs[2] = sumsq_rows[0];
733
16.8k
        sum_ptrs[0] = sum_rows[0];
734
16.8k
        sum_ptrs[1] = sum_rows[0];
735
16.8k
        sum_ptrs[2] = sum_rows[0];
736
737
16.8k
        sgr_box3_row_h(sumsq_rows[0], sum_rows[0], left, src, w, edges);
738
16.8k
        left++;
739
16.8k
        src += PXSTRIDE(stride);
740
741
16.8k
        sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
742
16.8k
                      w, params->sgr.s1, BITDEPTH_MAX);
743
16.8k
        rotate(A_ptrs, B_ptrs, 3);
744
745
16.8k
        if (--h <= 0)
746
3.56k
            goto vert_1;
747
748
13.3k
        sumsq_ptrs[2] = sumsq_rows[1];
749
13.3k
        sum_ptrs[2] = sum_rows[1];
750
751
13.3k
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
752
13.3k
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
753
13.3k
        left++;
754
13.3k
        src += PXSTRIDE(stride);
755
13.3k
        rotate(A_ptrs, B_ptrs, 3);
756
757
13.3k
        if (--h <= 0)
758
2.48k
            goto vert_2;
759
760
10.8k
        sumsq_ptrs[2] = sumsq_rows[2];
761
10.8k
        sum_ptrs[2] = sum_rows[2];
762
10.8k
    }
763
764
1.65M
    do {
765
1.65M
        sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
766
1.65M
                    left, src, w, params->sgr.s1, edges, BITDEPTH_MAX);
767
1.65M
        left++;
768
1.65M
        src += PXSTRIDE(stride);
769
770
1.65M
        sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
771
1.65M
                    w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
772
1.65M
    } while (--h > 0);
773
774
41.9k
    if (!(edges & LR_HAVE_BOTTOM))
775
10.3k
        goto vert_2;
776
777
31.6k
    sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
778
31.6k
                NULL, lpf_bottom, w, params->sgr.s1, edges, BITDEPTH_MAX);
779
31.6k
    lpf_bottom += PXSTRIDE(stride);
780
781
31.6k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
782
31.6k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
783
784
31.6k
    sgr_box3_hv(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
785
31.6k
                NULL, lpf_bottom, w, params->sgr.s1, edges, BITDEPTH_MAX);
786
787
31.6k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
788
31.6k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
789
31.6k
    return;
790
791
13.4k
vert_2:
792
13.4k
    sumsq_ptrs[2] = sumsq_ptrs[1];
793
13.4k
    sum_ptrs[2] = sum_ptrs[1];
794
13.4k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
795
13.4k
                  w, params->sgr.s1, BITDEPTH_MAX);
796
797
13.4k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
798
13.4k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
799
800
17.8k
output_1:
801
17.8k
    sumsq_ptrs[2] = sumsq_ptrs[1];
802
17.8k
    sum_ptrs[2] = sum_ptrs[1];
803
17.8k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
804
17.8k
                  w, params->sgr.s1, BITDEPTH_MAX);
805
806
17.8k
    sgr_finish1(&dst, stride, A_ptrs, B_ptrs,
807
17.8k
                w, params->sgr.w1 HIGHBD_TAIL_SUFFIX);
808
17.8k
    return;
809
810
4.38k
vert_1:
811
4.38k
    sumsq_ptrs[2] = sumsq_ptrs[1];
812
4.38k
    sum_ptrs[2] = sum_ptrs[1];
813
4.38k
    sgr_box3_vert(sumsq_ptrs, sum_ptrs, A_ptrs[2], B_ptrs[2],
814
4.38k
                  w, params->sgr.s1, BITDEPTH_MAX);
815
4.38k
    rotate(A_ptrs, B_ptrs, 3);
816
4.38k
    goto output_1;
817
13.4k
}
818
819
static void sgr_5x5_c(pixel *dst, const ptrdiff_t stride,
820
                      const pixel (*left)[4], const pixel *lpf,
821
                      const int w, int h,
822
                      const LooprestorationParams *const params,
823
                      const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
824
57.7k
{
825
57.7k
    ALIGN_STK_16(int32_t, sumsq_buf, BUF_STRIDE * 5 + 16,);
826
57.7k
    ALIGN_STK_16(coef, sum_buf, BUF_STRIDE * 5 + 16,);
827
57.7k
    int32_t *sumsq_ptrs[5], *sumsq_rows[5];
828
57.7k
    coef *sum_ptrs[5], *sum_rows[5];
829
346k
    for (int i = 0; i < 5; i++) {
830
288k
        sumsq_rows[i] = &sumsq_buf[i * BUF_STRIDE];
831
288k
        sum_rows[i] = &sum_buf[i * BUF_STRIDE];
832
288k
    }
833
834
57.7k
    ALIGN_STK_16(int32_t, A_buf, BUF_STRIDE * 2 + 16,);
835
57.7k
    ALIGN_STK_16(coef, B_buf, BUF_STRIDE * 2 + 16,);
836
57.7k
    int32_t *A_ptrs[2];
837
57.7k
    coef *B_ptrs[2];
838
173k
    for (int i = 0; i < 2; i++) {
839
115k
        A_ptrs[i] = &A_buf[i * BUF_STRIDE];
840
115k
        B_ptrs[i] = &B_buf[i * BUF_STRIDE];
841
115k
    }
842
57.7k
    const pixel *src = dst;
843
57.7k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
844
845
57.7k
    if (edges & LR_HAVE_TOP) {
846
34.2k
        sumsq_ptrs[0] = sumsq_rows[0];
847
34.2k
        sumsq_ptrs[1] = sumsq_rows[0];
848
34.2k
        sumsq_ptrs[2] = sumsq_rows[1];
849
34.2k
        sumsq_ptrs[3] = sumsq_rows[2];
850
34.2k
        sumsq_ptrs[4] = sumsq_rows[3];
851
34.2k
        sum_ptrs[0] = sum_rows[0];
852
34.2k
        sum_ptrs[1] = sum_rows[0];
853
34.2k
        sum_ptrs[2] = sum_rows[1];
854
34.2k
        sum_ptrs[3] = sum_rows[2];
855
34.2k
        sum_ptrs[4] = sum_rows[3];
856
857
34.2k
        sgr_box5_row_h(sumsq_rows[0], sum_rows[0], NULL, lpf, w, edges);
858
34.2k
        lpf += PXSTRIDE(stride);
859
34.2k
        sgr_box5_row_h(sumsq_rows[1], sum_rows[1], NULL, lpf, w, edges);
860
861
34.2k
        sgr_box5_row_h(sumsq_rows[2], sum_rows[2], left, src, w, edges);
862
34.2k
        left++;
863
34.2k
        src += PXSTRIDE(stride);
864
865
34.2k
        if (--h <= 0)
866
1.23k
            goto vert_1;
867
868
32.9k
        sgr_box5_row_h(sumsq_rows[3], sum_rows[3], left, src, w, edges);
869
32.9k
        left++;
870
32.9k
        src += PXSTRIDE(stride);
871
32.9k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
872
32.9k
                      w, params->sgr.s0, BITDEPTH_MAX);
873
32.9k
        rotate(A_ptrs, B_ptrs, 2);
874
875
32.9k
        if (--h <= 0)
876
947
            goto vert_2;
877
878
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
879
        // one of them to point at the previously unused rows[4].
880
32.0k
        sumsq_ptrs[3] = sumsq_rows[4];
881
32.0k
        sum_ptrs[3] = sum_rows[4];
882
32.0k
    } else {
883
23.5k
        sumsq_ptrs[0] = sumsq_rows[0];
884
23.5k
        sumsq_ptrs[1] = sumsq_rows[0];
885
23.5k
        sumsq_ptrs[2] = sumsq_rows[0];
886
23.5k
        sumsq_ptrs[3] = sumsq_rows[0];
887
23.5k
        sumsq_ptrs[4] = sumsq_rows[0];
888
23.5k
        sum_ptrs[0] = sum_rows[0];
889
23.5k
        sum_ptrs[1] = sum_rows[0];
890
23.5k
        sum_ptrs[2] = sum_rows[0];
891
23.5k
        sum_ptrs[3] = sum_rows[0];
892
23.5k
        sum_ptrs[4] = sum_rows[0];
893
894
23.5k
        sgr_box5_row_h(sumsq_rows[0], sum_rows[0], left, src, w, edges);
895
23.5k
        left++;
896
23.5k
        src += PXSTRIDE(stride);
897
898
23.5k
        if (--h <= 0)
899
3.40k
            goto vert_1;
900
901
20.1k
        sumsq_ptrs[4] = sumsq_rows[1];
902
20.1k
        sum_ptrs[4] = sum_rows[1];
903
904
20.1k
        sgr_box5_row_h(sumsq_rows[1], sum_rows[1], left, src, w, edges);
905
20.1k
        left++;
906
20.1k
        src += PXSTRIDE(stride);
907
908
20.1k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
909
20.1k
                      w, params->sgr.s0, BITDEPTH_MAX);
910
20.1k
        rotate(A_ptrs, B_ptrs, 2);
911
912
20.1k
        if (--h <= 0)
913
2.62k
            goto vert_2;
914
915
17.4k
        sumsq_ptrs[3] = sumsq_rows[2];
916
17.4k
        sumsq_ptrs[4] = sumsq_rows[3];
917
17.4k
        sum_ptrs[3] = sum_rows[2];
918
17.4k
        sum_ptrs[4] = sum_rows[3];
919
920
17.4k
        sgr_box5_row_h(sumsq_rows[2], sum_rows[2], left, src, w, edges);
921
17.4k
        left++;
922
17.4k
        src += PXSTRIDE(stride);
923
924
17.4k
        if (--h <= 0)
925
1.91k
            goto odd;
926
927
15.5k
        sgr_box5_row_h(sumsq_rows[3], sum_rows[3], left, src, w, edges);
928
15.5k
        left++;
929
15.5k
        src += PXSTRIDE(stride);
930
931
15.5k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
932
15.5k
                      w, params->sgr.s0, BITDEPTH_MAX);
933
15.5k
        sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
934
15.5k
                    w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
935
936
15.5k
        if (--h <= 0)
937
1.99k
            goto vert_2;
938
939
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
940
        // one of them to point at the previously unused rows[4].
941
13.5k
        sumsq_ptrs[3] = sumsq_rows[4];
942
13.5k
        sum_ptrs[3] = sum_rows[4];
943
13.5k
    }
944
945
905k
    do {
946
905k
        sgr_box5_row_h(sumsq_ptrs[3], sum_ptrs[3], left, src, w, edges);
947
905k
        left++;
948
905k
        src += PXSTRIDE(stride);
949
950
905k
        if (--h <= 0)
951
5.22k
            goto odd;
952
953
900k
        sgr_box5_row_h(sumsq_ptrs[4], sum_ptrs[4], left, src, w, edges);
954
900k
        left++;
955
900k
        src += PXSTRIDE(stride);
956
957
900k
        sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
958
900k
                      w, params->sgr.s0, BITDEPTH_MAX);
959
900k
        sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
960
900k
                    w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
961
900k
    } while (--h > 0);
962
963
40.3k
    if (!(edges & LR_HAVE_BOTTOM))
964
5.55k
        goto vert_2;
965
966
34.8k
    sgr_box5_row_h(sumsq_ptrs[3], sum_ptrs[3], NULL, lpf_bottom, w, edges);
967
34.8k
    lpf_bottom += PXSTRIDE(stride);
968
34.8k
    sgr_box5_row_h(sumsq_ptrs[4], sum_ptrs[4], NULL, lpf_bottom, w, edges);
969
970
45.9k
output_2:
971
45.9k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
972
45.9k
                  w, params->sgr.s0, BITDEPTH_MAX);
973
45.9k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
974
45.9k
                w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
975
45.9k
    return;
976
977
11.1k
vert_2:
978
    // Duplicate the last row twice more
979
11.1k
    sumsq_ptrs[3] = sumsq_ptrs[2];
980
11.1k
    sumsq_ptrs[4] = sumsq_ptrs[2];
981
11.1k
    sum_ptrs[3] = sum_ptrs[2];
982
11.1k
    sum_ptrs[4] = sum_ptrs[2];
983
11.1k
    goto output_2;
984
985
7.13k
odd:
986
    // Copy the last row as padding once
987
7.13k
    sumsq_ptrs[4] = sumsq_ptrs[3];
988
7.13k
    sum_ptrs[4] = sum_ptrs[3];
989
990
7.13k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
991
7.13k
                  w, params->sgr.s0, BITDEPTH_MAX);
992
7.13k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
993
7.13k
                w, 2, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
994
995
11.7k
output_1:
996
    // Duplicate the last row twice more
997
11.7k
    sumsq_ptrs[3] = sumsq_ptrs[2];
998
11.7k
    sumsq_ptrs[4] = sumsq_ptrs[2];
999
11.7k
    sum_ptrs[3] = sum_ptrs[2];
1000
11.7k
    sum_ptrs[4] = sum_ptrs[2];
1001
1002
11.7k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
1003
11.7k
                  w, params->sgr.s0, BITDEPTH_MAX);
1004
    // Output only one row
1005
11.7k
    sgr_finish2(&dst, stride, A_ptrs, B_ptrs,
1006
11.7k
                w, 1, params->sgr.w0 HIGHBD_TAIL_SUFFIX);
1007
11.7k
    return;
1008
1009
4.63k
vert_1:
1010
    // Copy the last row as padding once
1011
4.63k
    sumsq_ptrs[4] = sumsq_ptrs[3];
1012
4.63k
    sum_ptrs[4] = sum_ptrs[3];
1013
1014
4.63k
    sgr_box5_vert(sumsq_ptrs, sum_ptrs, A_ptrs[1], B_ptrs[1],
1015
4.63k
                  w, params->sgr.s0, BITDEPTH_MAX);
1016
4.63k
    rotate(A_ptrs, B_ptrs, 2);
1017
1018
4.63k
    goto output_1;
1019
7.13k
}
1020
1021
static void sgr_mix_c(pixel *dst, const ptrdiff_t stride,
1022
                      const pixel (*left)[4], const pixel *lpf,
1023
                      const int w, int h,
1024
                      const LooprestorationParams *const params,
1025
                      const enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX)
1026
126k
{
1027
126k
    ALIGN_STK_16(int32_t, sumsq5_buf, BUF_STRIDE * 5 + 16,);
1028
126k
    ALIGN_STK_16(coef, sum5_buf, BUF_STRIDE * 5 + 16,);
1029
126k
    int32_t *sumsq5_ptrs[5], *sumsq5_rows[5];
1030
126k
    coef *sum5_ptrs[5], *sum5_rows[5];
1031
758k
    for (int i = 0; i < 5; i++) {
1032
632k
        sumsq5_rows[i] = &sumsq5_buf[i * BUF_STRIDE];
1033
632k
        sum5_rows[i] = &sum5_buf[i * BUF_STRIDE];
1034
632k
    }
1035
126k
    ALIGN_STK_16(int32_t, sumsq3_buf, BUF_STRIDE * 3 + 16,);
1036
126k
    ALIGN_STK_16(coef, sum3_buf, BUF_STRIDE * 3 + 16,);
1037
126k
    int32_t *sumsq3_ptrs[3], *sumsq3_rows[3];
1038
126k
    coef *sum3_ptrs[3], *sum3_rows[3];
1039
505k
    for (int i = 0; i < 3; i++) {
1040
379k
        sumsq3_rows[i] = &sumsq3_buf[i * BUF_STRIDE];
1041
379k
        sum3_rows[i] = &sum3_buf[i * BUF_STRIDE];
1042
379k
    }
1043
1044
126k
    ALIGN_STK_16(int32_t, A5_buf, BUF_STRIDE * 2 + 16,);
1045
126k
    ALIGN_STK_16(coef, B5_buf, BUF_STRIDE * 2 + 16,);
1046
126k
    int32_t *A5_ptrs[2];
1047
126k
    coef *B5_ptrs[2];
1048
379k
    for (int i = 0; i < 2; i++) {
1049
252k
        A5_ptrs[i] = &A5_buf[i * BUF_STRIDE];
1050
252k
        B5_ptrs[i] = &B5_buf[i * BUF_STRIDE];
1051
252k
    }
1052
126k
    ALIGN_STK_16(int32_t, A3_buf, BUF_STRIDE * 4 + 16,);
1053
126k
    ALIGN_STK_16(coef, B3_buf, BUF_STRIDE * 4 + 16,);
1054
126k
    int32_t *A3_ptrs[4];
1055
126k
    coef *B3_ptrs[4];
1056
632k
    for (int i = 0; i < 4; i++) {
1057
505k
        A3_ptrs[i] = &A3_buf[i * BUF_STRIDE];
1058
505k
        B3_ptrs[i] = &B3_buf[i * BUF_STRIDE];
1059
505k
    }
1060
126k
    const pixel *src = dst;
1061
126k
    const pixel *lpf_bottom = lpf + 6*PXSTRIDE(stride);
1062
1063
126k
    if (edges & LR_HAVE_TOP) {
1064
81.9k
        sumsq5_ptrs[0] = sumsq5_rows[0];
1065
81.9k
        sumsq5_ptrs[1] = sumsq5_rows[0];
1066
81.9k
        sumsq5_ptrs[2] = sumsq5_rows[1];
1067
81.9k
        sumsq5_ptrs[3] = sumsq5_rows[2];
1068
81.9k
        sumsq5_ptrs[4] = sumsq5_rows[3];
1069
81.9k
        sum5_ptrs[0] = sum5_rows[0];
1070
81.9k
        sum5_ptrs[1] = sum5_rows[0];
1071
81.9k
        sum5_ptrs[2] = sum5_rows[1];
1072
81.9k
        sum5_ptrs[3] = sum5_rows[2];
1073
81.9k
        sum5_ptrs[4] = sum5_rows[3];
1074
1075
81.9k
        sumsq3_ptrs[0] = sumsq3_rows[0];
1076
81.9k
        sumsq3_ptrs[1] = sumsq3_rows[1];
1077
81.9k
        sumsq3_ptrs[2] = sumsq3_rows[2];
1078
81.9k
        sum3_ptrs[0] = sum3_rows[0];
1079
81.9k
        sum3_ptrs[1] = sum3_rows[1];
1080
81.9k
        sum3_ptrs[2] = sum3_rows[2];
1081
1082
81.9k
        sgr_box35_row_h(sumsq3_rows[0], sum3_rows[0],
1083
81.9k
                        sumsq5_rows[0], sum5_rows[0],
1084
81.9k
                        NULL, lpf, w, edges);
1085
81.9k
        lpf += PXSTRIDE(stride);
1086
81.9k
        sgr_box35_row_h(sumsq3_rows[1], sum3_rows[1],
1087
81.9k
                        sumsq5_rows[1], sum5_rows[1],
1088
81.9k
                        NULL, lpf, w, edges);
1089
1090
81.9k
        sgr_box35_row_h(sumsq3_rows[2], sum3_rows[2],
1091
81.9k
                        sumsq5_rows[2], sum5_rows[2],
1092
81.9k
                        left, src, w, edges);
1093
81.9k
        left++;
1094
81.9k
        src += PXSTRIDE(stride);
1095
1096
81.9k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1097
81.9k
                      w, params->sgr.s1, BITDEPTH_MAX);
1098
81.9k
        rotate(A3_ptrs, B3_ptrs, 4);
1099
1100
81.9k
        if (--h <= 0)
1101
1.76k
            goto vert_1;
1102
1103
80.2k
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1104
80.2k
                        sumsq5_rows[3], sum5_rows[3],
1105
80.2k
                        left, src, w, edges);
1106
80.2k
        left++;
1107
80.2k
        src += PXSTRIDE(stride);
1108
80.2k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1109
80.2k
                      w, params->sgr.s0, BITDEPTH_MAX);
1110
80.2k
        rotate(A5_ptrs, B5_ptrs, 2);
1111
80.2k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1112
80.2k
                      w, params->sgr.s1, BITDEPTH_MAX);
1113
80.2k
        rotate(A3_ptrs, B3_ptrs, 4);
1114
1115
80.2k
        if (--h <= 0)
1116
1.21k
            goto vert_2;
1117
1118
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
1119
        // one of them to point at the previously unused rows[4].
1120
79.0k
        sumsq5_ptrs[3] = sumsq5_rows[4];
1121
79.0k
        sum5_ptrs[3] = sum5_rows[4];
1122
79.0k
    } else {
1123
44.5k
        sumsq5_ptrs[0] = sumsq5_rows[0];
1124
44.5k
        sumsq5_ptrs[1] = sumsq5_rows[0];
1125
44.5k
        sumsq5_ptrs[2] = sumsq5_rows[0];
1126
44.5k
        sumsq5_ptrs[3] = sumsq5_rows[0];
1127
44.5k
        sumsq5_ptrs[4] = sumsq5_rows[0];
1128
44.5k
        sum5_ptrs[0] = sum5_rows[0];
1129
44.5k
        sum5_ptrs[1] = sum5_rows[0];
1130
44.5k
        sum5_ptrs[2] = sum5_rows[0];
1131
44.5k
        sum5_ptrs[3] = sum5_rows[0];
1132
44.5k
        sum5_ptrs[4] = sum5_rows[0];
1133
1134
44.5k
        sumsq3_ptrs[0] = sumsq3_rows[0];
1135
44.5k
        sumsq3_ptrs[1] = sumsq3_rows[0];
1136
44.5k
        sumsq3_ptrs[2] = sumsq3_rows[0];
1137
44.5k
        sum3_ptrs[0] = sum3_rows[0];
1138
44.5k
        sum3_ptrs[1] = sum3_rows[0];
1139
44.5k
        sum3_ptrs[2] = sum3_rows[0];
1140
1141
44.5k
        sgr_box35_row_h(sumsq3_rows[0], sum3_rows[0],
1142
44.5k
                        sumsq5_rows[0], sum5_rows[0],
1143
44.5k
                        left, src, w, edges);
1144
44.5k
        left++;
1145
44.5k
        src += PXSTRIDE(stride);
1146
1147
44.5k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1148
44.5k
                      w, params->sgr.s1, BITDEPTH_MAX);
1149
44.5k
        rotate(A3_ptrs, B3_ptrs, 4);
1150
1151
44.5k
        if (--h <= 0)
1152
7.99k
            goto vert_1;
1153
1154
36.5k
        sumsq5_ptrs[4] = sumsq5_rows[1];
1155
36.5k
        sum5_ptrs[4] = sum5_rows[1];
1156
1157
36.5k
        sumsq3_ptrs[2] = sumsq3_rows[1];
1158
36.5k
        sum3_ptrs[2] = sum3_rows[1];
1159
1160
36.5k
        sgr_box35_row_h(sumsq3_rows[1], sum3_rows[1],
1161
36.5k
                        sumsq5_rows[1], sum5_rows[1],
1162
36.5k
                        left, src, w, edges);
1163
36.5k
        left++;
1164
36.5k
        src += PXSTRIDE(stride);
1165
1166
36.5k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1167
36.5k
                      w, params->sgr.s0, BITDEPTH_MAX);
1168
36.5k
        rotate(A5_ptrs, B5_ptrs, 2);
1169
36.5k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1170
36.5k
                      w, params->sgr.s1, BITDEPTH_MAX);
1171
36.5k
        rotate(A3_ptrs, B3_ptrs, 4);
1172
1173
36.5k
        if (--h <= 0)
1174
5.51k
            goto vert_2;
1175
1176
31.0k
        sumsq5_ptrs[3] = sumsq5_rows[2];
1177
31.0k
        sumsq5_ptrs[4] = sumsq5_rows[3];
1178
31.0k
        sum5_ptrs[3] = sum5_rows[2];
1179
31.0k
        sum5_ptrs[4] = sum5_rows[3];
1180
1181
31.0k
        sumsq3_ptrs[2] = sumsq3_rows[2];
1182
31.0k
        sum3_ptrs[2] = sum3_rows[2];
1183
1184
31.0k
        sgr_box35_row_h(sumsq3_rows[2], sum3_rows[2],
1185
31.0k
                        sumsq5_rows[2], sum5_rows[2],
1186
31.0k
                        left, src, w, edges);
1187
31.0k
        left++;
1188
31.0k
        src += PXSTRIDE(stride);
1189
1190
31.0k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1191
31.0k
                      w, params->sgr.s1, BITDEPTH_MAX);
1192
31.0k
        rotate(A3_ptrs, B3_ptrs, 4);
1193
1194
31.0k
        if (--h <= 0)
1195
3.73k
            goto odd;
1196
1197
27.2k
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1198
27.2k
                        sumsq5_rows[3], sum5_rows[3],
1199
27.2k
                        left, src, w, edges);
1200
27.2k
        left++;
1201
27.2k
        src += PXSTRIDE(stride);
1202
1203
27.2k
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1204
27.2k
                      w, params->sgr.s0, BITDEPTH_MAX);
1205
27.2k
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1206
27.2k
                      w, params->sgr.s1, BITDEPTH_MAX);
1207
27.2k
        sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1208
27.2k
                       w, 2, params->sgr.w0, params->sgr.w1
1209
27.2k
                       HIGHBD_TAIL_SUFFIX);
1210
1211
27.2k
        if (--h <= 0)
1212
4.33k
            goto vert_2;
1213
1214
        // ptrs are rotated by 2; both [3] and [4] now point at rows[0]; set
1215
        // one of them to point at the previously unused rows[4].
1216
22.9k
        sumsq5_ptrs[3] = sumsq5_rows[4];
1217
22.9k
        sum5_ptrs[3] = sum5_rows[4];
1218
22.9k
    }
1219
1220
2.14M
    do {
1221
2.14M
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1222
2.14M
                        sumsq5_ptrs[3], sum5_ptrs[3],
1223
2.14M
                        left, src, w, edges);
1224
2.14M
        left++;
1225
2.14M
        src += PXSTRIDE(stride);
1226
1227
2.14M
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1228
2.14M
                      w, params->sgr.s1, BITDEPTH_MAX);
1229
2.14M
        rotate(A3_ptrs, B3_ptrs, 4);
1230
1231
2.14M
        if (--h <= 0)
1232
8.12k
            goto odd;
1233
1234
2.13M
        sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1235
2.13M
                        sumsq5_ptrs[4], sum5_ptrs[4],
1236
2.13M
                        left, src, w, edges);
1237
2.13M
        left++;
1238
2.13M
        src += PXSTRIDE(stride);
1239
1240
2.13M
        sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1241
2.13M
                      w, params->sgr.s0, BITDEPTH_MAX);
1242
2.13M
        sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1243
2.13M
                      w, params->sgr.s1, BITDEPTH_MAX);
1244
2.13M
        sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1245
2.13M
                       w, 2, params->sgr.w0, params->sgr.w1
1246
2.13M
                       HIGHBD_TAIL_SUFFIX);
1247
2.13M
    } while (--h > 0);
1248
1249
93.8k
    if (!(edges & LR_HAVE_BOTTOM))
1250
12.3k
        goto vert_2;
1251
1252
81.5k
    sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1253
81.5k
                    sumsq5_ptrs[3], sum5_ptrs[3],
1254
81.5k
                    NULL, lpf_bottom, w, edges);
1255
81.5k
    lpf_bottom += PXSTRIDE(stride);
1256
81.5k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1257
81.5k
                  w, params->sgr.s1, BITDEPTH_MAX);
1258
81.5k
    rotate(A3_ptrs, B3_ptrs, 4);
1259
1260
81.5k
    sgr_box35_row_h(sumsq3_ptrs[2], sum3_ptrs[2],
1261
81.5k
                    sumsq5_ptrs[4], sum5_ptrs[4],
1262
81.5k
                    NULL, lpf_bottom, w, edges);
1263
1264
104k
output_2:
1265
104k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1266
104k
                  w, params->sgr.s0, BITDEPTH_MAX);
1267
104k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1268
104k
                  w, params->sgr.s1, BITDEPTH_MAX);
1269
104k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1270
104k
                   w, 2, params->sgr.w0, params->sgr.w1
1271
104k
                   HIGHBD_TAIL_SUFFIX);
1272
104k
    return;
1273
1274
23.3k
vert_2:
1275
    // Duplicate the last row twice more
1276
23.3k
    sumsq5_ptrs[3] = sumsq5_ptrs[2];
1277
23.3k
    sumsq5_ptrs[4] = sumsq5_ptrs[2];
1278
23.3k
    sum5_ptrs[3] = sum5_ptrs[2];
1279
23.3k
    sum5_ptrs[4] = sum5_ptrs[2];
1280
1281
23.3k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1282
23.3k
    sum3_ptrs[2] = sum3_ptrs[1];
1283
23.3k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1284
23.3k
                  w, params->sgr.s1, BITDEPTH_MAX);
1285
23.3k
    rotate(A3_ptrs, B3_ptrs, 4);
1286
1287
23.3k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1288
23.3k
    sum3_ptrs[2] = sum3_ptrs[1];
1289
1290
23.3k
    goto output_2;
1291
1292
11.8k
odd:
1293
    // Copy the last row as padding once
1294
11.8k
    sumsq5_ptrs[4] = sumsq5_ptrs[3];
1295
11.8k
    sum5_ptrs[4] = sum5_ptrs[3];
1296
1297
11.8k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1298
11.8k
    sum3_ptrs[2] = sum3_ptrs[1];
1299
1300
11.8k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1301
11.8k
                  w, params->sgr.s0, BITDEPTH_MAX);
1302
11.8k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1303
11.8k
                  w, params->sgr.s1, BITDEPTH_MAX);
1304
11.8k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1305
11.8k
                   w, 2, params->sgr.w0, params->sgr.w1
1306
11.8k
                   HIGHBD_TAIL_SUFFIX);
1307
1308
21.6k
output_1:
1309
    // Duplicate the last row twice more
1310
21.6k
    sumsq5_ptrs[3] = sumsq5_ptrs[2];
1311
21.6k
    sumsq5_ptrs[4] = sumsq5_ptrs[2];
1312
21.6k
    sum5_ptrs[3] = sum5_ptrs[2];
1313
21.6k
    sum5_ptrs[4] = sum5_ptrs[2];
1314
1315
21.6k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1316
21.6k
    sum3_ptrs[2] = sum3_ptrs[1];
1317
1318
21.6k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1319
21.6k
                  w, params->sgr.s0, BITDEPTH_MAX);
1320
21.6k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1321
21.6k
                  w, params->sgr.s1, BITDEPTH_MAX);
1322
21.6k
    rotate(A3_ptrs, B3_ptrs, 4);
1323
    // Output only one row
1324
21.6k
    sgr_finish_mix(&dst, stride, A5_ptrs, B5_ptrs, A3_ptrs, B3_ptrs,
1325
21.6k
                   w, 1, params->sgr.w0, params->sgr.w1
1326
21.6k
                   HIGHBD_TAIL_SUFFIX);
1327
21.6k
    return;
1328
1329
9.75k
vert_1:
1330
    // Copy the last row as padding once
1331
9.75k
    sumsq5_ptrs[4] = sumsq5_ptrs[3];
1332
9.75k
    sum5_ptrs[4] = sum5_ptrs[3];
1333
1334
9.75k
    sumsq3_ptrs[2] = sumsq3_ptrs[1];
1335
9.75k
    sum3_ptrs[2] = sum3_ptrs[1];
1336
1337
9.75k
    sgr_box5_vert(sumsq5_ptrs, sum5_ptrs, A5_ptrs[1], B5_ptrs[1],
1338
9.75k
                  w, params->sgr.s0, BITDEPTH_MAX);
1339
9.75k
    rotate(A5_ptrs, B5_ptrs, 2);
1340
9.75k
    sgr_box3_vert(sumsq3_ptrs, sum3_ptrs, A3_ptrs[3], B3_ptrs[3],
1341
9.75k
                  w, params->sgr.s1, BITDEPTH_MAX);
1342
9.75k
    rotate(A3_ptrs, B3_ptrs, 4);
1343
1344
9.75k
    goto output_1;
1345
11.8k
}
1346
1347
#if HAVE_ASM
1348
#if ARCH_AARCH64 || ARCH_ARM
1349
#include "src/arm/looprestoration.h"
1350
#elif ARCH_LOONGARCH64
1351
#include "src/loongarch/looprestoration.h"
1352
#elif ARCH_PPC64LE
1353
#include "src/ppc/looprestoration.h"
1354
#elif ARCH_X86
1355
#include "src/x86/looprestoration.h"
1356
#endif
1357
#endif
1358
1359
COLD void bitfn(dav1d_loop_restoration_dsp_init)(Dav1dLoopRestorationDSPContext *const c,
1360
                                                 const int bpc)
1361
96.1k
{
1362
96.1k
    c->wiener[0] = c->wiener[1] = wiener_c;
1363
96.1k
    c->sgr[0] = sgr_5x5_c;
1364
96.1k
    c->sgr[1] = sgr_3x3_c;
1365
96.1k
    c->sgr[2] = sgr_mix_c;
1366
1367
#if HAVE_ASM
1368
#if ARCH_AARCH64 || ARCH_ARM
1369
    loop_restoration_dsp_init_arm(c, bpc);
1370
#elif ARCH_LOONGARCH64
1371
    loop_restoration_dsp_init_loongarch(c, bpc);
1372
#elif ARCH_PPC64LE
1373
    loop_restoration_dsp_init_ppc(c, bpc);
1374
#elif ARCH_X86
1375
    loop_restoration_dsp_init_x86(c, bpc);
1376
#endif
1377
#endif
1378
96.1k
}
dav1d_loop_restoration_dsp_init_8bpc
Line
Count
Source
1361
44.8k
{
1362
44.8k
    c->wiener[0] = c->wiener[1] = wiener_c;
1363
44.8k
    c->sgr[0] = sgr_5x5_c;
1364
44.8k
    c->sgr[1] = sgr_3x3_c;
1365
44.8k
    c->sgr[2] = sgr_mix_c;
1366
1367
#if HAVE_ASM
1368
#if ARCH_AARCH64 || ARCH_ARM
1369
    loop_restoration_dsp_init_arm(c, bpc);
1370
#elif ARCH_LOONGARCH64
1371
    loop_restoration_dsp_init_loongarch(c, bpc);
1372
#elif ARCH_PPC64LE
1373
    loop_restoration_dsp_init_ppc(c, bpc);
1374
#elif ARCH_X86
1375
    loop_restoration_dsp_init_x86(c, bpc);
1376
#endif
1377
#endif
1378
44.8k
}
dav1d_loop_restoration_dsp_init_16bpc
Line
Count
Source
1361
51.3k
{
1362
51.3k
    c->wiener[0] = c->wiener[1] = wiener_c;
1363
51.3k
    c->sgr[0] = sgr_5x5_c;
1364
51.3k
    c->sgr[1] = sgr_3x3_c;
1365
51.3k
    c->sgr[2] = sgr_mix_c;
1366
1367
#if HAVE_ASM
1368
#if ARCH_AARCH64 || ARCH_ARM
1369
    loop_restoration_dsp_init_arm(c, bpc);
1370
#elif ARCH_LOONGARCH64
1371
    loop_restoration_dsp_init_loongarch(c, bpc);
1372
#elif ARCH_PPC64LE
1373
    loop_restoration_dsp_init_ppc(c, bpc);
1374
#elif ARCH_X86
1375
    loop_restoration_dsp_init_x86(c, bpc);
1376
#endif
1377
#endif
1378
51.3k
}