Coverage Report

Created: 2026-06-15 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/ipred_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 <stdlib.h>
31
#include <string.h>
32
33
#include "common/attributes.h"
34
#include "common/intops.h"
35
36
#include "src/ipred.h"
37
#include "src/tables.h"
38
39
static NOINLINE void
40
splat_dc(pixel *dst, const ptrdiff_t stride,
41
         const int width, const int height, const int dc HIGHBD_DECL_SUFFIX)
42
3.47M
{
43
3.47M
#if BITDEPTH == 8
44
3.47M
    assert(dc <= 0xff);
45
3.47M
    if (width > 4) {
46
324k
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
11.0M
        for (int y = 0; y < height; y++) {
48
62.1M
            for (int x = 0; x < width; x += sizeof(dcN))
49
51.4M
                *((uint64_t *) &dst[x]) = dcN;
50
10.6M
            dst += PXSTRIDE(stride);
51
10.6M
        }
52
3.15M
    } else {
53
3.15M
        const unsigned dcN = dc * 0x01010101U;
54
15.7M
        for (int y = 0; y < height; y++) {
55
25.2M
            for (int x = 0; x < width; x += sizeof(dcN))
56
12.6M
                *((unsigned *) &dst[x]) = dcN;
57
12.6M
            dst += PXSTRIDE(stride);
58
12.6M
        }
59
3.15M
    }
60
#else
61
    assert(dc <= bitdepth_max);
62
    const uint64_t dcN = dc * 0x0001000100010001ULL;
63
    for (int y = 0; y < height; y++) {
64
        for (int x = 0; x < width; x += sizeof(dcN) >> 1)
65
            *((uint64_t *) &dst[x]) = dcN;
66
        dst += PXSTRIDE(stride);
67
    }
68
#endif
69
3.47M
}
70
71
static NOINLINE void
72
cfl_pred(pixel *dst, const ptrdiff_t stride,
73
         const int width, const int height, const int dc,
74
         const int16_t *ac, const int alpha HIGHBD_DECL_SUFFIX)
75
58.1k
{
76
777k
    for (int y = 0; y < height; y++) {
77
9.23M
        for (int x = 0; x < width; x++) {
78
8.51M
            const int diff = alpha * ac[x];
79
8.51M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
8.51M
        }
81
718k
        ac += width;
82
718k
        dst += PXSTRIDE(stride);
83
718k
    }
84
58.1k
}
85
86
404k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
404k
    unsigned dc = width >> 1;
88
10.4M
    for (int i = 0; i < width; i++)
89
10.0M
       dc += topleft[1 + i];
90
404k
    return dc >> ctz(width);
91
404k
}
92
93
static void ipred_dc_top_c(pixel *dst, const ptrdiff_t stride,
94
                           const pixel *const topleft,
95
                           const int width, const int height, const int a,
96
                           const int max_width, const int max_height
97
                           HIGHBD_DECL_SUFFIX)
98
389k
{
99
389k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
389k
             HIGHBD_TAIL_SUFFIX);
101
389k
}
102
103
static void ipred_cfl_top_c(pixel *dst, const ptrdiff_t stride,
104
                            const pixel *const topleft,
105
                            const int width, const int height,
106
                            const int16_t *ac, const int alpha
107
                            HIGHBD_DECL_SUFFIX)
108
15.0k
{
109
15.0k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
15.0k
             HIGHBD_TAIL_SUFFIX);
111
15.0k
}
112
113
135k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
135k
    unsigned dc = height >> 1;
115
3.13M
    for (int i = 0; i < height; i++)
116
3.00M
       dc += topleft[-(1 + i)];
117
135k
    return dc >> ctz(height);
118
135k
}
119
120
static void ipred_dc_left_c(pixel *dst, const ptrdiff_t stride,
121
                            const pixel *const topleft,
122
                            const int width, const int height, const int a,
123
                            const int max_width, const int max_height
124
                            HIGHBD_DECL_SUFFIX)
125
116k
{
126
116k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
116k
             HIGHBD_TAIL_SUFFIX);
128
116k
}
129
130
static void ipred_cfl_left_c(pixel *dst, const ptrdiff_t stride,
131
                             const pixel *const topleft,
132
                             const int width, const int height,
133
                             const int16_t *ac, const int alpha
134
                             HIGHBD_DECL_SUFFIX)
135
18.5k
{
136
18.5k
    const unsigned dc = dc_gen_left(topleft, height);
137
18.5k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
18.5k
}
139
140
#if BITDEPTH == 8
141
64.8k
#define MULTIPLIER_1x2 0x5556
142
11.0k
#define MULTIPLIER_1x4 0x3334
143
37.9k
#define BASE_SHIFT 16
144
#else
145
#define MULTIPLIER_1x2 0xAAAB
146
#define MULTIPLIER_1x4 0x6667
147
#define BASE_SHIFT 17
148
#endif
149
150
static unsigned dc_gen(const pixel *const topleft,
151
                       const int width, const int height)
152
8.18M
{
153
8.18M
    unsigned dc = (width + height) >> 1;
154
50.9M
    for (int i = 0; i < width; i++)
155
42.7M
       dc += topleft[i + 1];
156
51.0M
    for (int i = 0; i < height; i++)
157
42.8M
       dc += topleft[-(i + 1)];
158
8.18M
    dc >>= ctz(width + height);
159
160
8.18M
    if (width != height) {
161
37.9k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
37.9k
                                                           MULTIPLIER_1x2;
163
37.9k
        dc >>= BASE_SHIFT;
164
37.9k
    }
165
8.18M
    return dc;
166
8.18M
}
167
168
static void ipred_dc_c(pixel *dst, const ptrdiff_t stride,
169
                       const pixel *const topleft,
170
                       const int width, const int height, const int a,
171
                       const int max_width, const int max_height
172
                       HIGHBD_DECL_SUFFIX)
173
8.16M
{
174
8.16M
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
8.16M
             HIGHBD_TAIL_SUFFIX);
176
8.16M
}
177
178
static void ipred_cfl_c(pixel *dst, const ptrdiff_t stride,
179
                        const pixel *const topleft,
180
                        const int width, const int height,
181
                        const int16_t *ac, const int alpha
182
                        HIGHBD_DECL_SUFFIX)
183
20.3k
{
184
20.3k
    unsigned dc = dc_gen(topleft, width, height);
185
20.3k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
20.3k
}
187
188
#undef MULTIPLIER_1x2
189
#undef MULTIPLIER_1x4
190
#undef BASE_SHIFT
191
192
static void ipred_dc_128_c(pixel *dst, const ptrdiff_t stride,
193
                           const pixel *const topleft,
194
                           const int width, const int height, const int a,
195
                           const int max_width, const int max_height
196
                           HIGHBD_DECL_SUFFIX)
197
57.8k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
57.8k
    const int dc = 128;
202
57.8k
#endif
203
57.8k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
57.8k
}
205
206
static void ipred_cfl_128_c(pixel *dst, const ptrdiff_t stride,
207
                            const pixel *const topleft,
208
                            const int width, const int height,
209
                            const int16_t *ac, const int alpha
210
                            HIGHBD_DECL_SUFFIX)
211
4.20k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
4.20k
    const int dc = 128;
216
4.20k
#endif
217
4.20k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
4.20k
}
219
220
static void ipred_v_c(pixel *dst, const ptrdiff_t stride,
221
                      const pixel *const topleft,
222
                      const int width, const int height, const int a,
223
                      const int max_width, const int max_height
224
                      HIGHBD_DECL_SUFFIX)
225
86.6k
{
226
1.76M
    for (int y = 0; y < height; y++) {
227
1.67M
        pixel_copy(dst, topleft + 1, width);
228
1.67M
        dst += PXSTRIDE(stride);
229
1.67M
    }
230
86.6k
}
231
232
static void ipred_h_c(pixel *dst, const ptrdiff_t stride,
233
                      const pixel *const topleft,
234
                      const int width, const int height, const int a,
235
                      const int max_width, const int max_height
236
                      HIGHBD_DECL_SUFFIX)
237
101k
{
238
1.86M
    for (int y = 0; y < height; y++) {
239
1.76M
        pixel_set(dst, topleft[-(1 + y)], width);
240
1.76M
        dst += PXSTRIDE(stride);
241
1.76M
    }
242
101k
}
243
244
static void ipred_paeth_c(pixel *dst, const ptrdiff_t stride,
245
                          const pixel *const tl_ptr,
246
                          const int width, const int height, const int a,
247
                          const int max_width, const int max_height
248
                          HIGHBD_DECL_SUFFIX)
249
103k
{
250
103k
    const int topleft = tl_ptr[0];
251
1.57M
    for (int y = 0; y < height; y++) {
252
1.47M
        const int left = tl_ptr[-(y + 1)];
253
29.9M
        for (int x = 0; x < width; x++) {
254
28.4M
            const int top = tl_ptr[1 + x];
255
28.4M
            const int base = left + top - topleft;
256
28.4M
            const int ldiff = abs(left - base);
257
28.4M
            const int tdiff = abs(top - base);
258
28.4M
            const int tldiff = abs(topleft - base);
259
260
28.4M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
28.4M
                     tdiff <= tldiff ? top : topleft;
262
28.4M
        }
263
1.47M
        dst += PXSTRIDE(stride);
264
1.47M
    }
265
103k
}
266
267
static void ipred_smooth_c(pixel *dst, const ptrdiff_t stride,
268
                           const pixel *const topleft,
269
                           const int width, const int height, const int a,
270
                           const int max_width, const int max_height
271
                           HIGHBD_DECL_SUFFIX)
272
145k
{
273
145k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
145k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
145k
    const int right = topleft[width], bottom = topleft[-height];
276
277
2.18M
    for (int y = 0; y < height; y++) {
278
56.0M
        for (int x = 0; x < width; x++) {
279
53.9M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
53.9M
                      (256 - weights_ver[y]) * bottom +
281
53.9M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
53.9M
                      (256 - weights_hor[x]) * right;
283
53.9M
            dst[x] = (pred + 256) >> 9;
284
53.9M
        }
285
2.03M
        dst += PXSTRIDE(stride);
286
2.03M
    }
287
145k
}
288
289
static void ipred_smooth_v_c(pixel *dst, const ptrdiff_t stride,
290
                             const pixel *const topleft,
291
                             const int width, const int height, const int a,
292
                             const int max_width, const int max_height
293
                             HIGHBD_DECL_SUFFIX)
294
30.4k
{
295
30.4k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
30.4k
    const int bottom = topleft[-height];
297
298
554k
    for (int y = 0; y < height; y++) {
299
14.9M
        for (int x = 0; x < width; x++) {
300
14.3M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
14.3M
                      (256 - weights_ver[y]) * bottom;
302
14.3M
            dst[x] = (pred + 128) >> 8;
303
14.3M
        }
304
524k
        dst += PXSTRIDE(stride);
305
524k
    }
306
30.4k
}
307
308
static void ipred_smooth_h_c(pixel *dst, const ptrdiff_t stride,
309
                             const pixel *const topleft,
310
                             const int width, const int height, const int a,
311
                             const int max_width, const int max_height
312
                             HIGHBD_DECL_SUFFIX)
313
54.9k
{
314
54.9k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
54.9k
    const int right = topleft[width];
316
317
882k
    for (int y = 0; y < height; y++) {
318
22.7M
        for (int x = 0; x < width; x++) {
319
21.9M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
21.9M
                      (256 - weights_hor[x]) * right;
321
21.9M
            dst[x] = (pred + 128) >> 8;
322
21.9M
        }
323
827k
        dst += PXSTRIDE(stride);
324
827k
    }
325
54.9k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
142k
{
330
142k
    if (is_sm) {
331
26.6k
        if (wh <= 8) {
332
5.23k
            if (angle >= 64) return 2;
333
3.10k
            if (angle >= 40) return 1;
334
21.4k
        } else if (wh <= 16) {
335
6.26k
            if (angle >= 48) return 2;
336
4.23k
            if (angle >= 20) return 1;
337
15.1k
        } else if (wh <= 24) {
338
4.13k
            if (angle >=  4) return 3;
339
11.0k
        } else {
340
11.0k
            return 3;
341
11.0k
        }
342
115k
    } else {
343
115k
        if (wh <= 8) {
344
21.9k
            if (angle >= 56) return 1;
345
93.7k
        } else if (wh <= 16) {
346
8.75k
            if (angle >= 40) return 1;
347
84.9k
        } else if (wh <= 24) {
348
14.2k
            if (angle >= 32) return 3;
349
7.04k
            if (angle >= 16) return 2;
350
3.96k
            if (angle >=  8) return 1;
351
70.6k
        } else if (wh <= 32) {
352
18.2k
            if (angle >= 32) return 3;
353
9.19k
            if (angle >=  4) return 2;
354
1.50k
            return 1;
355
52.4k
        } else {
356
52.4k
            return 3;
357
52.4k
        }
358
115k
    }
359
15.5k
    return 0;
360
142k
}
361
362
static NOINLINE void filter_edge(pixel *const out, const int sz,
363
                                 const int lim_from, const int lim_to,
364
                                 const pixel *const in, const int from,
365
                                 const int to, const int strength)
366
126k
{
367
126k
    static const uint8_t kernel[3][5] = {
368
126k
        { 0, 4, 8, 4, 0 },
369
126k
        { 0, 5, 6, 5, 0 },
370
126k
        { 2, 4, 4, 4, 2 }
371
126k
    };
372
373
126k
    assert(strength > 0);
374
126k
    int i = 0;
375
261k
    for (; i < imin(sz, lim_from); i++)
376
134k
        out[i] = in[iclip(i, from, to - 1)];
377
3.35M
    for (; i < imin(lim_to, sz); i++) {
378
3.22M
        int s = 0;
379
19.3M
        for (int j = 0; j < 5; j++)
380
16.1M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
3.22M
        out[i] = (s + 8) >> 4;
382
3.22M
    }
383
299k
    for (; i < sz; i++)
384
172k
        out[i] = in[iclip(i, from, to - 1)];
385
126k
}
386
387
200k
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
200k
    return angle < 40 && wh <= 16 >> is_sm;
389
200k
}
390
391
static NOINLINE void upsample_edge(pixel *const out, const int hsz,
392
                                   const pixel *const in, const int from,
393
                                   const int to HIGHBD_DECL_SUFFIX)
394
58.2k
{
395
58.2k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
58.2k
    int i;
397
468k
    for (i = 0; i < hsz - 1; i++) {
398
410k
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
410k
        int s = 0;
401
2.04M
        for (int j = 0; j < 4; j++)
402
1.63M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
410k
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
410k
    }
405
58.2k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
58.2k
}
407
408
static void ipred_z1_c(pixel *dst, const ptrdiff_t stride,
409
                       const pixel *const topleft_in,
410
                       const int width, const int height, int angle,
411
                       const int max_width, const int max_height
412
                       HIGHBD_DECL_SUFFIX)
413
73.6k
{
414
73.6k
    const int is_sm = (angle >> 9) & 0x1;
415
73.6k
    const int enable_intra_edge_filter = angle >> 10;
416
73.6k
    angle &= 511;
417
73.6k
    assert(angle < 90);
418
73.6k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
73.6k
    pixel top_out[64 + 64];
420
73.6k
    const pixel *top;
421
73.6k
    int max_base_x;
422
73.6k
    const int upsample_above = enable_intra_edge_filter ?
423
44.0k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
73.6k
    if (upsample_above) {
425
22.6k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
22.6k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
22.6k
        top = top_out;
428
22.6k
        max_base_x = 2 * (width + height) - 2;
429
22.6k
        dx <<= 1;
430
50.9k
    } else {
431
50.9k
        const int filter_strength = enable_intra_edge_filter ?
432
29.6k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
50.9k
        if (filter_strength) {
434
18.1k
            filter_edge(top_out, width + height, 0, width + height,
435
18.1k
                        &topleft_in[1], -1, width + imin(width, height),
436
18.1k
                        filter_strength);
437
18.1k
            top = top_out;
438
18.1k
            max_base_x = width + height - 1;
439
32.7k
        } else {
440
32.7k
            top = &topleft_in[1];
441
32.7k
            max_base_x = width + imin(width, height) - 1;
442
32.7k
        }
443
50.9k
    }
444
73.6k
    const int base_inc = 1 + upsample_above;
445
1.15M
    for (int y = 0, xpos = dx; y < height;
446
1.08M
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
1.08M
    {
448
1.08M
        const int frac = xpos & 0x3E;
449
450
32.6M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
31.5M
            if (base < max_base_x) {
452
31.5M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
31.5M
                dst[x] = (v + 32) >> 6;
454
31.5M
            } else {
455
49.0k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
49.0k
                break;
457
49.0k
            }
458
31.5M
        }
459
1.08M
    }
460
73.6k
}
461
462
static void ipred_z2_c(pixel *dst, const ptrdiff_t stride,
463
                       const pixel *const topleft_in,
464
                       const int width, const int height, int angle,
465
                       const int max_width, const int max_height
466
                       HIGHBD_DECL_SUFFIX)
467
122k
{
468
122k
    const int is_sm = (angle >> 9) & 0x1;
469
122k
    const int enable_intra_edge_filter = angle >> 10;
470
122k
    angle &= 511;
471
122k
    assert(angle > 90 && angle < 180);
472
122k
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
122k
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
122k
    const int upsample_left = enable_intra_edge_filter ?
475
65.8k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
122k
    const int upsample_above = enable_intra_edge_filter ?
477
65.8k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
122k
    pixel edge[64 + 64 + 1];
479
122k
    pixel *const topleft = &edge[64];
480
481
122k
    if (upsample_above) {
482
11.1k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
11.1k
                      HIGHBD_TAIL_SUFFIX);
484
11.1k
        dx <<= 1;
485
110k
    } else {
486
110k
        const int filter_strength = enable_intra_edge_filter ?
487
56.2k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
110k
        if (filter_strength) {
490
48.5k
            filter_edge(&topleft[1], width, 0, max_width,
491
48.5k
                        &topleft_in[1], -1, width,
492
48.5k
                        filter_strength);
493
62.3k
        } else {
494
62.3k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
62.3k
        }
496
110k
    }
497
122k
    if (upsample_left) {
498
12.1k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
12.1k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
12.1k
        dy <<= 1;
501
109k
    } else {
502
109k
        const int filter_strength = enable_intra_edge_filter ?
503
56.2k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
109k
        if (filter_strength) {
506
48.5k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
48.5k
                        &topleft_in[-height],
508
48.5k
                        0, height + 1, filter_strength);
509
61.3k
        } else {
510
61.3k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
61.3k
        }
512
109k
    }
513
122k
    *topleft = *topleft_in;
514
515
122k
    const int base_inc_x = 1 + upsample_above;
516
122k
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
2.12M
    for (int y = 0, xpos = ((1 + upsample_above) << 6) - dx; y < height;
518
2.00M
         y++, xpos -= dx, dst += PXSTRIDE(stride))
519
2.00M
    {
520
2.00M
        int base_x = xpos >> 6;
521
2.00M
        const int frac_x = xpos & 0x3E;
522
523
56.1M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
54.1M
             x++, base_x += base_inc_x, ypos -= dy)
525
54.1M
        {
526
54.1M
            int v;
527
54.1M
            if (base_x >= 0) {
528
25.4M
                v = topleft[base_x] * (64 - frac_x) +
529
25.4M
                    topleft[base_x + 1] * frac_x;
530
28.7M
            } else {
531
28.7M
                const int base_y = ypos >> 6;
532
28.7M
                assert(base_y >= -(1 + upsample_left));
533
28.7M
                const int frac_y = ypos & 0x3E;
534
28.7M
                v = left[-base_y] * (64 - frac_y) +
535
28.7M
                    left[-(base_y + 1)] * frac_y;
536
28.7M
            }
537
54.1M
            dst[x] = (v + 32) >> 6;
538
54.1M
        }
539
2.00M
    }
540
122k
}
541
542
static void ipred_z3_c(pixel *dst, const ptrdiff_t stride,
543
                       const pixel *const topleft_in,
544
                       const int width, const int height, int angle,
545
                       const int max_width, const int max_height
546
                       HIGHBD_DECL_SUFFIX)
547
75.5k
{
548
75.5k
    const int is_sm = (angle >> 9) & 0x1;
549
75.5k
    const int enable_intra_edge_filter = angle >> 10;
550
75.5k
    angle &= 511;
551
75.5k
    assert(angle > 180);
552
75.5k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
75.5k
    pixel left_out[64 + 64];
554
75.5k
    const pixel *left;
555
75.5k
    int max_base_y;
556
75.5k
    const int upsample_left = enable_intra_edge_filter ?
557
50.6k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
75.5k
    if (upsample_left) {
559
12.2k
        upsample_edge(left_out, width + height,
560
12.2k
                      &topleft_in[-(width + height)],
561
12.2k
                      imax(width - height, 0), width + height + 1
562
12.2k
                      HIGHBD_TAIL_SUFFIX);
563
12.2k
        left = &left_out[2 * (width + height) - 2];
564
12.2k
        max_base_y = 2 * (width + height) - 2;
565
12.2k
        dy <<= 1;
566
63.3k
    } else {
567
63.3k
        const int filter_strength = enable_intra_edge_filter ?
568
50.6k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
63.3k
        if (filter_strength) {
571
11.5k
            filter_edge(left_out, width + height, 0, width + height,
572
11.5k
                        &topleft_in[-(width + height)],
573
11.5k
                        imax(width - height, 0), width + height + 1,
574
11.5k
                        filter_strength);
575
11.5k
            left = &left_out[width + height - 1];
576
11.5k
            max_base_y = width + height - 1;
577
51.8k
        } else {
578
51.8k
            left = &topleft_in[-1];
579
51.8k
            max_base_y = height + imin(width, height) - 1;
580
51.8k
        }
581
63.3k
    }
582
75.5k
    const int base_inc = 1 + upsample_left;
583
1.13M
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
1.06M
        const int frac = ypos & 0x3E;
585
586
31.2M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
30.2M
            if (base < max_base_y) {
588
30.2M
                const int v = left[-base] * (64 - frac) +
589
30.2M
                              left[-(base + 1)] * frac;
590
30.2M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
30.2M
            } else {
592
13.5k
                do {
593
13.5k
                    dst[y * PXSTRIDE(stride) + x] = left[-max_base_y];
594
13.5k
                } while (++y < height);
595
3.81k
                break;
596
3.81k
            }
597
30.2M
        }
598
1.06M
    }
599
75.5k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
4.10M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
4.10M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
4.10M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
4.10M
    flt_ptr[48] * p6
607
4.10M
#define FLT_INCR 2
608
#else
609
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
610
    flt_ptr[ 0] * p0 + flt_ptr[ 8] * p1 +           \
611
    flt_ptr[16] * p2 + flt_ptr[24] * p3 +           \
612
    flt_ptr[32] * p4 + flt_ptr[40] * p5 +           \
613
    flt_ptr[48] * p6
614
#define FLT_INCR 1
615
#endif
616
617
/* Up to 32x32 only */
618
static void ipred_filter_c(pixel *dst, const ptrdiff_t stride,
619
                           const pixel *const topleft_in,
620
                           const int width, const int height, int filt_idx,
621
                           const int max_width, const int max_height
622
                           HIGHBD_DECL_SUFFIX)
623
35.7k
{
624
35.7k
    filt_idx &= 511;
625
35.7k
    assert(filt_idx < 5);
626
627
35.7k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
35.7k
    const pixel *top = &topleft_in[1];
629
199k
    for (int y = 0; y < height; y += 2) {
630
163k
        const pixel *topleft = &topleft_in[-y];
631
163k
        const pixel *left = &topleft[-1];
632
163k
        ptrdiff_t left_stride = -1;
633
676k
        for (int x = 0; x < width; x += 4) {
634
512k
            const int p0 = *topleft;
635
512k
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
512k
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
512k
            pixel *ptr = &dst[x];
638
512k
            const int8_t *flt_ptr = filter;
639
640
1.53M
            for (int yy = 0; yy < 2; yy++) {
641
5.12M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
4.10M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
4.10M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
4.10M
                }
645
1.02M
                ptr += PXSTRIDE(stride);
646
1.02M
            }
647
512k
            left = &dst[x + 4 - 1];
648
512k
            left_stride = PXSTRIDE(stride);
649
512k
            top += 4;
650
512k
            topleft = &top[-1];
651
512k
        }
652
163k
        top = &dst[PXSTRIDE(stride)];
653
163k
        dst = &dst[PXSTRIDE(stride) * 2];
654
163k
    }
655
35.7k
}
656
657
static NOINLINE void
658
cfl_ac_c(int16_t *ac, const pixel *ypx, const ptrdiff_t stride,
659
         const int w_pad, const int h_pad, const int width, const int height,
660
         const int ss_hor, const int ss_ver)
661
34.7k
{
662
34.7k
    int y, x;
663
34.7k
    int16_t *const ac_orig = ac;
664
665
34.7k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
34.7k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
451k
    for (y = 0; y < height - 4 * h_pad; y++) {
669
5.41M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
4.99M
            int ac_sum = ypx[x << ss_hor];
671
4.99M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
4.99M
            if (ss_ver) {
673
375k
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
375k
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
375k
            }
676
4.99M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
4.99M
        }
678
501k
        for (; x < width; x++)
679
84.1k
            ac[x] = ac[x - 1];
680
417k
        ac += width;
681
417k
        ypx += PXSTRIDE(stride) << ss_ver;
682
417k
    }
683
43.7k
    for (; y < height; y++) {
684
8.91k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
8.91k
        ac += width;
686
8.91k
    }
687
688
34.7k
    const int log2sz = ctz(width) + ctz(height);
689
34.7k
    int sum = (1 << log2sz) >> 1;
690
460k
    for (ac = ac_orig, y = 0; y < height; y++) {
691
5.62M
        for (x = 0; x < width; x++)
692
5.20M
            sum += ac[x];
693
426k
        ac += width;
694
426k
    }
695
34.7k
    sum >>= log2sz;
696
697
    // subtract DC
698
460k
    for (ac = ac_orig, y = 0; y < height; y++) {
699
5.62M
        for (x = 0; x < width; x++)
700
5.20M
            ac[x] -= sum;
701
425k
        ac += width;
702
425k
    }
703
34.7k
}
704
705
#define cfl_ac_fn(fmt, ss_hor, ss_ver) \
706
static void cfl_ac_##fmt##_c(int16_t *const ac, const pixel *const ypx, \
707
                             const ptrdiff_t stride, const int w_pad, \
708
34.7k
                             const int h_pad, const int cw, const int ch) \
709
34.7k
{ \
710
34.7k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
34.7k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
6.19k
                             const int h_pad, const int cw, const int ch) \
709
6.19k
{ \
710
6.19k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
6.19k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
814
                             const int h_pad, const int cw, const int ch) \
709
814
{ \
710
814
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
814
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
27.7k
                             const int h_pad, const int cw, const int ch) \
709
27.7k
{ \
710
27.7k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
27.7k
}
712
713
cfl_ac_fn(420, 1, 1)
714
cfl_ac_fn(422, 1, 0)
715
cfl_ac_fn(444, 0, 0)
716
717
static void pal_pred_c(pixel *dst, const ptrdiff_t stride,
718
                       const pixel *const pal, const uint8_t *idx,
719
                       const int w, const int h)
720
804
{
721
15.6k
    for (int y = 0; y < h; y++) {
722
166k
        for (int x = 0; x < w; x += 2) {
723
151k
            const int i = *idx++;
724
151k
            assert(!(i & 0x88));
725
151k
            dst[x + 0] = pal[i & 7];
726
151k
            dst[x + 1] = pal[i >> 4];
727
151k
        }
728
14.8k
        dst += PXSTRIDE(stride);
729
14.8k
    }
730
804
}
731
732
#if HAVE_ASM
733
#if ARCH_AARCH64 || ARCH_ARM
734
#include "src/arm/ipred.h"
735
#elif ARCH_RISCV
736
#include "src/riscv/ipred.h"
737
#elif ARCH_X86
738
#include "src/x86/ipred.h"
739
#elif ARCH_LOONGARCH64
740
#include "src/loongarch/ipred.h"
741
#endif
742
#endif
743
744
38.2k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
38.2k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
38.2k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
38.2k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
38.2k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
38.2k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
38.2k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
38.2k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
38.2k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
38.2k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
38.2k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
38.2k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
38.2k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
38.2k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
38.2k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
38.2k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
38.2k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
38.2k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
38.2k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
38.2k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
38.2k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
38.2k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
38.2k
    c->pal_pred = pal_pred_c;
770
771
#if HAVE_ASM
772
#if ARCH_AARCH64 || ARCH_ARM
773
    intra_pred_dsp_init_arm(c);
774
#elif ARCH_RISCV
775
    intra_pred_dsp_init_riscv(c);
776
#elif ARCH_X86
777
    intra_pred_dsp_init_x86(c);
778
#elif ARCH_LOONGARCH64
779
    intra_pred_dsp_init_loongarch(c);
780
#endif
781
#endif
782
38.2k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
16.4k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
16.4k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
16.4k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
16.4k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
16.4k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
16.4k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
16.4k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
16.4k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
16.4k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
16.4k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
16.4k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
16.4k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
16.4k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
16.4k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
16.4k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
16.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
16.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
16.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
16.4k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
16.4k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
16.4k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
16.4k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
16.4k
    c->pal_pred = pal_pred_c;
770
771
#if HAVE_ASM
772
#if ARCH_AARCH64 || ARCH_ARM
773
    intra_pred_dsp_init_arm(c);
774
#elif ARCH_RISCV
775
    intra_pred_dsp_init_riscv(c);
776
#elif ARCH_X86
777
    intra_pred_dsp_init_x86(c);
778
#elif ARCH_LOONGARCH64
779
    intra_pred_dsp_init_loongarch(c);
780
#endif
781
#endif
782
16.4k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
21.8k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
21.8k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
21.8k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
21.8k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
21.8k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
21.8k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
21.8k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
21.8k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
21.8k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
21.8k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
21.8k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
21.8k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
21.8k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
21.8k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
21.8k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
21.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
21.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
21.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
21.8k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
21.8k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
21.8k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
21.8k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
21.8k
    c->pal_pred = pal_pred_c;
770
771
#if HAVE_ASM
772
#if ARCH_AARCH64 || ARCH_ARM
773
    intra_pred_dsp_init_arm(c);
774
#elif ARCH_RISCV
775
    intra_pred_dsp_init_riscv(c);
776
#elif ARCH_X86
777
    intra_pred_dsp_init_x86(c);
778
#elif ARCH_LOONGARCH64
779
    intra_pred_dsp_init_loongarch(c);
780
#endif
781
#endif
782
21.8k
}