Coverage Report

Created: 2026-06-15 06:22

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
429k
{
43
429k
#if BITDEPTH == 8
44
429k
    assert(dc <= 0xff);
45
429k
    if (width > 4) {
46
158k
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
4.73M
        for (int y = 0; y < height; y++) {
48
23.9M
            for (int x = 0; x < width; x += sizeof(dcN))
49
19.4M
                *((uint64_t *) &dst[x]) = dcN;
50
4.57M
            dst += PXSTRIDE(stride);
51
4.57M
        }
52
271k
    } else {
53
271k
        const unsigned dcN = dc * 0x01010101U;
54
1.38M
        for (int y = 0; y < height; y++) {
55
2.21M
            for (int x = 0; x < width; x += sizeof(dcN))
56
1.10M
                *((unsigned *) &dst[x]) = dcN;
57
1.10M
            dst += PXSTRIDE(stride);
58
1.10M
        }
59
271k
    }
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
429k
}
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
145k
{
76
1.33M
    for (int y = 0; y < height; y++) {
77
13.3M
        for (int x = 0; x < width; x++) {
78
12.1M
            const int diff = alpha * ac[x];
79
12.1M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
12.1M
        }
81
1.18M
        ac += width;
82
1.18M
        dst += PXSTRIDE(stride);
83
1.18M
    }
84
145k
}
85
86
323k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
323k
    unsigned dc = width >> 1;
88
5.94M
    for (int i = 0; i < width; i++)
89
5.62M
       dc += topleft[1 + i];
90
323k
    return dc >> ctz(width);
91
323k
}
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
261k
{
99
261k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
261k
             HIGHBD_TAIL_SUFFIX);
101
261k
}
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
62.6k
{
109
62.6k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
62.6k
             HIGHBD_TAIL_SUFFIX);
111
62.6k
}
112
113
153k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
153k
    unsigned dc = height >> 1;
115
2.50M
    for (int i = 0; i < height; i++)
116
2.34M
       dc += topleft[-(1 + i)];
117
153k
    return dc >> ctz(height);
118
153k
}
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
122k
{
126
122k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
122k
             HIGHBD_TAIL_SUFFIX);
128
122k
}
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
31.3k
{
136
31.3k
    const unsigned dc = dc_gen_left(topleft, height);
137
31.3k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
31.3k
}
139
140
#if BITDEPTH == 8
141
37.6k
#define MULTIPLIER_1x2 0x5556
142
7.14k
#define MULTIPLIER_1x4 0x3334
143
22.4k
#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
440k
{
153
440k
    unsigned dc = (width + height) >> 1;
154
3.28M
    for (int i = 0; i < width; i++)
155
2.84M
       dc += topleft[i + 1];
156
3.25M
    for (int i = 0; i < height; i++)
157
2.81M
       dc += topleft[-(i + 1)];
158
440k
    dc >>= ctz(width + height);
159
160
440k
    if (width != height) {
161
22.4k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
22.4k
                                                           MULTIPLIER_1x2;
163
22.4k
        dc >>= BASE_SHIFT;
164
22.4k
    }
165
440k
    return dc;
166
440k
}
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
391k
{
174
391k
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
391k
             HIGHBD_TAIL_SUFFIX);
176
391k
}
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
49.7k
{
184
49.7k
    unsigned dc = dc_gen(topleft, width, height);
185
49.7k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
49.7k
}
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
29.8k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
29.8k
    const int dc = 128;
202
29.8k
#endif
203
29.8k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
29.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
1.53k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
1.53k
    const int dc = 128;
216
1.53k
#endif
217
1.53k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
1.53k
}
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
93.8k
{
226
1.35M
    for (int y = 0; y < height; y++) {
227
1.25M
        pixel_copy(dst, topleft + 1, width);
228
1.25M
        dst += PXSTRIDE(stride);
229
1.25M
    }
230
93.8k
}
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
83.3k
{
238
1.22M
    for (int y = 0; y < height; y++) {
239
1.14M
        pixel_set(dst, topleft[-(1 + y)], width);
240
1.14M
        dst += PXSTRIDE(stride);
241
1.14M
    }
242
83.3k
}
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
69.3k
{
250
69.3k
    const int topleft = tl_ptr[0];
251
559k
    for (int y = 0; y < height; y++) {
252
489k
        const int left = tl_ptr[-(y + 1)];
253
6.66M
        for (int x = 0; x < width; x++) {
254
6.17M
            const int top = tl_ptr[1 + x];
255
6.17M
            const int base = left + top - topleft;
256
6.17M
            const int ldiff = abs(left - base);
257
6.17M
            const int tdiff = abs(top - base);
258
6.17M
            const int tldiff = abs(topleft - base);
259
260
6.17M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
6.17M
                     tdiff <= tldiff ? top : topleft;
262
6.17M
        }
263
489k
        dst += PXSTRIDE(stride);
264
489k
    }
265
69.3k
}
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
75.5k
{
273
75.5k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
75.5k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
75.5k
    const int right = topleft[width], bottom = topleft[-height];
276
277
1.08M
    for (int y = 0; y < height; y++) {
278
23.8M
        for (int x = 0; x < width; x++) {
279
22.8M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
22.8M
                      (256 - weights_ver[y]) * bottom +
281
22.8M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
22.8M
                      (256 - weights_hor[x]) * right;
283
22.8M
            dst[x] = (pred + 256) >> 9;
284
22.8M
        }
285
1.00M
        dst += PXSTRIDE(stride);
286
1.00M
    }
287
75.5k
}
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
29.8k
{
295
29.8k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
29.8k
    const int bottom = topleft[-height];
297
298
350k
    for (int y = 0; y < height; y++) {
299
6.94M
        for (int x = 0; x < width; x++) {
300
6.62M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
6.62M
                      (256 - weights_ver[y]) * bottom;
302
6.62M
            dst[x] = (pred + 128) >> 8;
303
6.62M
        }
304
320k
        dst += PXSTRIDE(stride);
305
320k
    }
306
29.8k
}
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
35.1k
{
314
35.1k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
35.1k
    const int right = topleft[width];
316
317
477k
    for (int y = 0; y < height; y++) {
318
10.4M
        for (int x = 0; x < width; x++) {
319
9.95M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
9.95M
                      (256 - weights_hor[x]) * right;
321
9.95M
            dst[x] = (pred + 128) >> 8;
322
9.95M
        }
323
442k
        dst += PXSTRIDE(stride);
324
442k
    }
325
35.1k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
106k
{
330
106k
    if (is_sm) {
331
19.2k
        if (wh <= 8) {
332
4.98k
            if (angle >= 64) return 2;
333
2.11k
            if (angle >= 40) return 1;
334
14.2k
        } else if (wh <= 16) {
335
5.14k
            if (angle >= 48) return 2;
336
3.49k
            if (angle >= 20) return 1;
337
9.08k
        } else if (wh <= 24) {
338
2.80k
            if (angle >=  4) return 3;
339
6.28k
        } else {
340
6.28k
            return 3;
341
6.28k
        }
342
87.1k
    } else {
343
87.1k
        if (wh <= 8) {
344
19.4k
            if (angle >= 56) return 1;
345
67.7k
        } else if (wh <= 16) {
346
10.2k
            if (angle >= 40) return 1;
347
57.5k
        } else if (wh <= 24) {
348
13.3k
            if (angle >= 32) return 3;
349
6.35k
            if (angle >= 16) return 2;
350
3.14k
            if (angle >=  8) return 1;
351
44.1k
        } else if (wh <= 32) {
352
9.29k
            if (angle >= 32) return 3;
353
4.50k
            if (angle >=  4) return 2;
354
1.15k
            return 1;
355
34.8k
        } else {
356
34.8k
            return 3;
357
34.8k
        }
358
87.1k
    }
359
10.6k
    return 0;
360
106k
}
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
95.7k
{
367
95.7k
    static const uint8_t kernel[3][5] = {
368
95.7k
        { 0, 4, 8, 4, 0 },
369
95.7k
        { 0, 5, 6, 5, 0 },
370
95.7k
        { 2, 4, 4, 4, 2 }
371
95.7k
    };
372
373
95.7k
    assert(strength > 0);
374
95.7k
    int i = 0;
375
164k
    for (; i < imin(sz, lim_from); i++)
376
69.1k
        out[i] = in[iclip(i, from, to - 1)];
377
2.08M
    for (; i < imin(lim_to, sz); i++) {
378
1.98M
        int s = 0;
379
11.9M
        for (int j = 0; j < 5; j++)
380
9.93M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
1.98M
        out[i] = (s + 8) >> 4;
382
1.98M
    }
383
242k
    for (; i < sz; i++)
384
146k
        out[i] = in[iclip(i, from, to - 1)];
385
95.7k
}
386
387
164k
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
164k
    return angle < 40 && wh <= 16 >> is_sm;
389
164k
}
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.6k
{
395
58.6k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
58.6k
    int i;
397
457k
    for (i = 0; i < hsz - 1; i++) {
398
398k
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
398k
        int s = 0;
401
1.99M
        for (int j = 0; j < 4; j++)
402
1.59M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
398k
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
398k
    }
405
58.6k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
58.6k
}
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
41.3k
{
414
41.3k
    const int is_sm = (angle >> 9) & 0x1;
415
41.3k
    const int enable_intra_edge_filter = angle >> 10;
416
41.3k
    angle &= 511;
417
41.3k
    assert(angle < 90);
418
41.3k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
41.3k
    pixel top_out[64 + 64];
420
41.3k
    const pixel *top;
421
41.3k
    int max_base_x;
422
41.3k
    const int upsample_above = enable_intra_edge_filter ?
423
30.2k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
41.3k
    if (upsample_above) {
425
14.8k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
14.8k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
14.8k
        top = top_out;
428
14.8k
        max_base_x = 2 * (width + height) - 2;
429
14.8k
        dx <<= 1;
430
26.4k
    } else {
431
26.4k
        const int filter_strength = enable_intra_edge_filter ?
432
15.3k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
26.4k
        if (filter_strength) {
434
11.1k
            filter_edge(top_out, width + height, 0, width + height,
435
11.1k
                        &topleft_in[1], -1, width + imin(width, height),
436
11.1k
                        filter_strength);
437
11.1k
            top = top_out;
438
11.1k
            max_base_x = width + height - 1;
439
15.2k
        } else {
440
15.2k
            top = &topleft_in[1];
441
15.2k
            max_base_x = width + imin(width, height) - 1;
442
15.2k
        }
443
26.4k
    }
444
41.3k
    const int base_inc = 1 + upsample_above;
445
536k
    for (int y = 0, xpos = dx; y < height;
446
495k
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
495k
    {
448
495k
        const int frac = xpos & 0x3E;
449
450
11.4M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
11.0M
            if (base < max_base_x) {
452
10.9M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
10.9M
                dst[x] = (v + 32) >> 6;
454
10.9M
            } else {
455
25.9k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
25.9k
                break;
457
25.9k
            }
458
11.0M
        }
459
495k
    }
460
41.3k
}
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
77.0k
{
468
77.0k
    const int is_sm = (angle >> 9) & 0x1;
469
77.0k
    const int enable_intra_edge_filter = angle >> 10;
470
77.0k
    angle &= 511;
471
77.0k
    assert(angle > 90 && angle < 180);
472
77.0k
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
77.0k
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
77.0k
    const int upsample_left = enable_intra_edge_filter ?
475
52.8k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
77.0k
    const int upsample_above = enable_intra_edge_filter ?
477
52.8k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
77.0k
    pixel edge[64 + 64 + 1];
479
77.0k
    pixel *const topleft = &edge[64];
480
481
77.0k
    if (upsample_above) {
482
10.5k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
10.5k
                      HIGHBD_TAIL_SUFFIX);
484
10.5k
        dx <<= 1;
485
66.5k
    } else {
486
66.5k
        const int filter_strength = enable_intra_edge_filter ?
487
42.3k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
66.5k
        if (filter_strength) {
490
39.5k
            filter_edge(&topleft[1], width, 0, max_width,
491
39.5k
                        &topleft_in[1], -1, width,
492
39.5k
                        filter_strength);
493
39.5k
        } else {
494
26.9k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
26.9k
        }
496
66.5k
    }
497
77.0k
    if (upsample_left) {
498
13.4k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
13.4k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
13.4k
        dy <<= 1;
501
63.6k
    } else {
502
63.6k
        const int filter_strength = enable_intra_edge_filter ?
503
39.4k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
63.6k
        if (filter_strength) {
506
36.5k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
36.5k
                        &topleft_in[-height],
508
36.5k
                        0, height + 1, filter_strength);
509
36.5k
        } else {
510
27.0k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
27.0k
        }
512
63.6k
    }
513
77.0k
    *topleft = *topleft_in;
514
515
77.0k
    const int base_inc_x = 1 + upsample_above;
516
77.0k
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
1.13M
    for (int y = 0, xpos = ((1 + upsample_above) << 6) - dx; y < height;
518
1.05M
         y++, xpos -= dx, dst += PXSTRIDE(stride))
519
1.05M
    {
520
1.05M
        int base_x = xpos >> 6;
521
1.05M
        const int frac_x = xpos & 0x3E;
522
523
25.0M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
23.9M
             x++, base_x += base_inc_x, ypos -= dy)
525
23.9M
        {
526
23.9M
            int v;
527
23.9M
            if (base_x >= 0) {
528
10.7M
                v = topleft[base_x] * (64 - frac_x) +
529
10.7M
                    topleft[base_x + 1] * frac_x;
530
13.2M
            } else {
531
13.2M
                const int base_y = ypos >> 6;
532
13.2M
                assert(base_y >= -(1 + upsample_left));
533
13.2M
                const int frac_y = ypos & 0x3E;
534
13.2M
                v = left[-base_y] * (64 - frac_y) +
535
13.2M
                    left[-(base_y + 1)] * frac_y;
536
13.2M
            }
537
23.9M
            dst[x] = (v + 32) >> 6;
538
23.9M
        }
539
1.05M
    }
540
77.0k
}
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
43.1k
{
548
43.1k
    const int is_sm = (angle >> 9) & 0x1;
549
43.1k
    const int enable_intra_edge_filter = angle >> 10;
550
43.1k
    angle &= 511;
551
43.1k
    assert(angle > 180);
552
43.1k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
43.1k
    pixel left_out[64 + 64];
554
43.1k
    const pixel *left;
555
43.1k
    int max_base_y;
556
43.1k
    const int upsample_left = enable_intra_edge_filter ?
557
28.9k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
43.1k
    if (upsample_left) {
559
19.7k
        upsample_edge(left_out, width + height,
560
19.7k
                      &topleft_in[-(width + height)],
561
19.7k
                      imax(width - height, 0), width + height + 1
562
19.7k
                      HIGHBD_TAIL_SUFFIX);
563
19.7k
        left = &left_out[2 * (width + height) - 2];
564
19.7k
        max_base_y = 2 * (width + height) - 2;
565
19.7k
        dy <<= 1;
566
23.4k
    } else {
567
23.4k
        const int filter_strength = enable_intra_edge_filter ?
568
14.2k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
23.4k
        if (filter_strength) {
571
8.39k
            filter_edge(left_out, width + height, 0, width + height,
572
8.39k
                        &topleft_in[-(width + height)],
573
8.39k
                        imax(width - height, 0), width + height + 1,
574
8.39k
                        filter_strength);
575
8.39k
            left = &left_out[width + height - 1];
576
8.39k
            max_base_y = width + height - 1;
577
15.0k
        } else {
578
15.0k
            left = &topleft_in[-1];
579
15.0k
            max_base_y = height + imin(width, height) - 1;
580
15.0k
        }
581
23.4k
    }
582
43.1k
    const int base_inc = 1 + upsample_left;
583
541k
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
498k
        const int frac = ypos & 0x3E;
585
586
10.7M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
10.2M
            if (base < max_base_y) {
588
10.2M
                const int v = left[-base] * (64 - frac) +
589
10.2M
                              left[-(base + 1)] * frac;
590
10.2M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
10.2M
            } else {
592
13.4k
                do {
593
13.4k
                    dst[y * PXSTRIDE(stride) + x] = left[-max_base_y];
594
13.4k
                } while (++y < height);
595
3.56k
                break;
596
3.56k
            }
597
10.2M
        }
598
498k
    }
599
43.1k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
3.60M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
3.60M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
3.60M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
3.60M
    flt_ptr[48] * p6
607
3.60M
#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
38.6k
{
624
38.6k
    filt_idx &= 511;
625
38.6k
    assert(filt_idx < 5);
626
627
38.6k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
38.6k
    const pixel *top = &topleft_in[1];
629
196k
    for (int y = 0; y < height; y += 2) {
630
157k
        const pixel *topleft = &topleft_in[-y];
631
157k
        const pixel *left = &topleft[-1];
632
157k
        ptrdiff_t left_stride = -1;
633
609k
        for (int x = 0; x < width; x += 4) {
634
451k
            const int p0 = *topleft;
635
451k
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
451k
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
451k
            pixel *ptr = &dst[x];
638
451k
            const int8_t *flt_ptr = filter;
639
640
1.35M
            for (int yy = 0; yy < 2; yy++) {
641
4.51M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
3.60M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
3.60M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
3.60M
                }
645
902k
                ptr += PXSTRIDE(stride);
646
902k
            }
647
451k
            left = &dst[x + 4 - 1];
648
451k
            left_stride = PXSTRIDE(stride);
649
451k
            top += 4;
650
451k
            topleft = &top[-1];
651
451k
        }
652
157k
        top = &dst[PXSTRIDE(stride)];
653
157k
        dst = &dst[PXSTRIDE(stride) * 2];
654
157k
    }
655
38.6k
}
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
78.7k
{
662
78.7k
    int y, x;
663
78.7k
    int16_t *const ac_orig = ac;
664
665
78.7k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
78.7k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
737k
    for (y = 0; y < height - 4 * h_pad; y++) {
669
7.39M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
6.74M
            int ac_sum = ypx[x << ss_hor];
671
6.74M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
6.74M
            if (ss_ver) {
673
199k
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
199k
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
199k
            }
676
6.74M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
6.74M
        }
678
942k
        for (; x < width; x++)
679
283k
            ac[x] = ac[x - 1];
680
658k
        ac += width;
681
658k
        ypx += PXSTRIDE(stride) << ss_ver;
682
658k
    }
683
87.3k
    for (; y < height; y++) {
684
8.55k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
8.55k
        ac += width;
686
8.55k
    }
687
688
78.7k
    const int log2sz = ctz(width) + ctz(height);
689
78.7k
    int sum = (1 << log2sz) >> 1;
690
746k
    for (ac = ac_orig, y = 0; y < height; y++) {
691
7.83M
        for (x = 0; x < width; x++)
692
7.17M
            sum += ac[x];
693
667k
        ac += width;
694
667k
    }
695
78.7k
    sum >>= log2sz;
696
697
    // subtract DC
698
745k
    for (ac = ac_orig, y = 0; y < height; y++) {
699
7.83M
        for (x = 0; x < width; x++)
700
7.16M
            ac[x] -= sum;
701
667k
        ac += width;
702
667k
    }
703
78.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
78.7k
                             const int h_pad, const int cw, const int ch) \
709
78.7k
{ \
710
78.7k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
78.7k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
5.14k
                             const int h_pad, const int cw, const int ch) \
709
5.14k
{ \
710
5.14k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
5.14k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
1.06k
                             const int h_pad, const int cw, const int ch) \
709
1.06k
{ \
710
1.06k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
1.06k
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
72.5k
                             const int h_pad, const int cw, const int ch) \
709
72.5k
{ \
710
72.5k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
72.5k
}
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
1.19k
{
721
22.4k
    for (int y = 0; y < h; y++) {
722
215k
        for (int x = 0; x < w; x += 2) {
723
193k
            const int i = *idx++;
724
193k
            assert(!(i & 0x88));
725
193k
            dst[x + 0] = pal[i & 7];
726
193k
            dst[x + 1] = pal[i >> 4];
727
193k
        }
728
21.2k
        dst += PXSTRIDE(stride);
729
21.2k
    }
730
1.19k
}
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
18.4k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
18.4k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
18.4k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
18.4k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
18.4k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
18.4k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
18.4k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
18.4k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
18.4k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
18.4k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
18.4k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
18.4k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
18.4k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
18.4k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
18.4k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
18.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
18.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
18.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
18.4k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
18.4k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
18.4k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
18.4k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
18.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
18.4k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
8.44k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
8.44k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
8.44k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
8.44k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
8.44k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
8.44k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
8.44k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
8.44k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
8.44k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
8.44k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
8.44k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
8.44k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
8.44k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
8.44k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
8.44k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
8.44k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
8.44k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
8.44k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
8.44k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
8.44k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
8.44k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
8.44k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
8.44k
    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
8.44k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
9.97k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
9.97k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
9.97k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
9.97k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
9.97k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
9.97k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
9.97k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
9.97k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
9.97k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
9.97k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
9.97k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
9.97k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
9.97k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
9.97k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
9.97k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
9.97k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
9.97k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
9.97k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
9.97k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
9.97k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
9.97k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
9.97k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
9.97k
    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
9.97k
}