Coverage Report

Created: 2026-08-13 07:20

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
600k
{
43
600k
#if BITDEPTH == 8
44
600k
    assert(dc <= 0xff);
45
600k
    if (width > 4) {
46
233k
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
6.72M
        for (int y = 0; y < height; y++) {
48
33.2M
            for (int x = 0; x < width; x += sizeof(dcN))
49
26.7M
                *((uint64_t *) &dst[x]) = dcN;
50
6.49M
            dst += PXSTRIDE(stride);
51
6.49M
        }
52
366k
    } else {
53
366k
        const unsigned dcN = dc * 0x01010101U;
54
1.85M
        for (int y = 0; y < height; y++) {
55
2.97M
            for (int x = 0; x < width; x += sizeof(dcN))
56
1.48M
                *((unsigned *) &dst[x]) = dcN;
57
1.48M
            dst += PXSTRIDE(stride);
58
1.48M
        }
59
366k
    }
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
600k
}
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
124k
{
76
1.30M
    for (int y = 0; y < height; y++) {
77
14.5M
        for (int x = 0; x < width; x++) {
78
13.3M
            const int diff = alpha * ac[x];
79
13.3M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
13.3M
        }
81
1.18M
        ac += width;
82
1.18M
        dst += PXSTRIDE(stride);
83
1.18M
    }
84
124k
}
85
86
359k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
359k
    unsigned dc = width >> 1;
88
7.63M
    for (int i = 0; i < width; i++)
89
7.27M
       dc += topleft[1 + i];
90
359k
    return dc >> ctz(width);
91
359k
}
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
312k
{
99
312k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
312k
             HIGHBD_TAIL_SUFFIX);
101
312k
}
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
47.0k
{
109
47.0k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
47.0k
             HIGHBD_TAIL_SUFFIX);
111
47.0k
}
112
113
181k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
181k
    unsigned dc = height >> 1;
115
2.92M
    for (int i = 0; i < height; i++)
116
2.74M
       dc += topleft[-(1 + i)];
117
181k
    return dc >> ctz(height);
118
181k
}
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
153k
{
126
153k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
153k
             HIGHBD_TAIL_SUFFIX);
128
153k
}
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
27.7k
{
136
27.7k
    const unsigned dc = dc_gen_left(topleft, height);
137
27.7k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
27.7k
}
139
140
#if BITDEPTH == 8
141
87.1k
#define MULTIPLIER_1x2 0x5556
142
17.1k
#define MULTIPLIER_1x4 0x3334
143
52.1k
#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
906k
{
153
906k
    unsigned dc = (width + height) >> 1;
154
6.65M
    for (int i = 0; i < width; i++)
155
5.74M
       dc += topleft[i + 1];
156
6.70M
    for (int i = 0; i < height; i++)
157
5.79M
       dc += topleft[-(i + 1)];
158
906k
    dc >>= ctz(width + height);
159
160
906k
    if (width != height) {
161
52.1k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
52.1k
                                                           MULTIPLIER_1x2;
163
52.1k
        dc >>= BASE_SHIFT;
164
52.1k
    }
165
906k
    return dc;
166
906k
}
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
862k
{
174
862k
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
862k
             HIGHBD_TAIL_SUFFIX);
176
862k
}
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
45.7k
{
184
45.7k
    const unsigned dc = dc_gen(topleft, width, height);
185
45.7k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
45.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
50.6k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
50.6k
    const int dc = 128;
202
50.6k
#endif
203
50.6k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
50.6k
}
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
3.70k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
3.70k
    const int dc = 128;
216
3.70k
#endif
217
3.70k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
3.70k
}
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
138k
{
226
2.08M
    for (int y = 0; y < height; y++) {
227
1.94M
        pixel_copy(dst, topleft + 1, width);
228
1.94M
        dst += PXSTRIDE(stride);
229
1.94M
    }
230
138k
}
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
124k
{
238
1.75M
    for (int y = 0; y < height; y++) {
239
1.62M
        pixel_set(dst, topleft[-(1 + y)], width);
240
1.62M
        dst += PXSTRIDE(stride);
241
1.62M
    }
242
124k
}
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
118k
{
250
118k
    const int topleft = tl_ptr[0];
251
954k
    for (int y = 0; y < height; y++) {
252
836k
        const int left = tl_ptr[-(y + 1)];
253
11.4M
        for (int x = 0; x < width; x++) {
254
10.6M
            const int top = tl_ptr[1 + x];
255
10.6M
            const int base = left + top - topleft;
256
10.6M
            const int ldiff = abs(left - base);
257
10.6M
            const int tdiff = abs(top - base);
258
10.6M
            const int tldiff = abs(topleft - base);
259
260
10.6M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
10.6M
                     tdiff <= tldiff ? top : topleft;
262
10.6M
        }
263
836k
        dst += PXSTRIDE(stride);
264
836k
    }
265
118k
}
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
164k
{
273
164k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
164k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
164k
    const int right = topleft[width], bottom = topleft[-height];
276
277
2.06M
    for (int y = 0; y < height; y++) {
278
45.5M
        for (int x = 0; x < width; x++) {
279
43.6M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
43.6M
                      (256 - weights_ver[y]) * bottom +
281
43.6M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
43.6M
                      (256 - weights_hor[x]) * right;
283
43.6M
            dst[x] = (pred + 256) >> 9;
284
43.6M
        }
285
1.90M
        dst += PXSTRIDE(stride);
286
1.90M
    }
287
164k
}
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
38.4k
{
295
38.4k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
38.4k
    const int bottom = topleft[-height];
297
298
512k
    for (int y = 0; y < height; y++) {
299
11.0M
        for (int x = 0; x < width; x++) {
300
10.6M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
10.6M
                      (256 - weights_ver[y]) * bottom;
302
10.6M
            dst[x] = (pred + 128) >> 8;
303
10.6M
        }
304
474k
        dst += PXSTRIDE(stride);
305
474k
    }
306
38.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
65.1k
{
314
65.1k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
65.1k
    const int right = topleft[width];
316
317
863k
    for (int y = 0; y < height; y++) {
318
18.9M
        for (int x = 0; x < width; x++) {
319
18.1M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
18.1M
                      (256 - weights_hor[x]) * right;
321
18.1M
            dst[x] = (pred + 128) >> 8;
322
18.1M
        }
323
797k
        dst += PXSTRIDE(stride);
324
797k
    }
325
65.1k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
226k
{
330
226k
    if (is_sm) {
331
38.7k
        if (wh <= 8) {
332
11.5k
            if (angle >= 64) return 2;
333
4.25k
            if (angle >= 40) return 1;
334
27.2k
        } else if (wh <= 16) {
335
10.5k
            if (angle >= 48) return 2;
336
6.99k
            if (angle >= 20) return 1;
337
16.7k
        } else if (wh <= 24) {
338
5.35k
            if (angle >=  4) return 3;
339
11.3k
        } else {
340
11.3k
            return 3;
341
11.3k
        }
342
187k
    } else {
343
187k
        if (wh <= 8) {
344
72.5k
            if (angle >= 56) return 1;
345
115k
        } else if (wh <= 16) {
346
13.7k
            if (angle >= 40) return 1;
347
101k
        } else if (wh <= 24) {
348
21.0k
            if (angle >= 32) return 3;
349
10.3k
            if (angle >= 16) return 2;
350
5.49k
            if (angle >=  8) return 1;
351
80.2k
        } else if (wh <= 32) {
352
18.7k
            if (angle >= 32) return 3;
353
8.90k
            if (angle >=  4) return 2;
354
1.93k
            return 1;
355
61.5k
        } else {
356
61.5k
            return 3;
357
61.5k
        }
358
187k
    }
359
35.3k
    return 0;
360
226k
}
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
191k
{
367
191k
    static const uint8_t kernel[3][5] = {
368
191k
        { 0, 4, 8, 4, 0 },
369
191k
        { 0, 5, 6, 5, 0 },
370
191k
        { 2, 4, 4, 4, 2 }
371
191k
    };
372
373
191k
    assert(strength > 0);
374
191k
    int i = 0;
375
267k
    for (; i < imin(sz, lim_from); i++)
376
76.1k
        out[i] = in[iclip(i, from, to - 1)];
377
3.92M
    for (; i < imin(lim_to, sz); i++) {
378
3.72M
        int s = 0;
379
22.3M
        for (int j = 0; j < 5; j++)
380
18.6M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
3.72M
        out[i] = (s + 8) >> 4;
382
3.72M
    }
383
397k
    for (; i < sz; i++)
384
206k
        out[i] = in[iclip(i, from, to - 1)];
385
191k
}
386
387
408k
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
408k
    return angle < 40 && wh <= 16 >> is_sm;
389
408k
}
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
182k
{
395
182k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
182k
    int i;
397
1.35M
    for (i = 0; i < hsz - 1; i++) {
398
1.17M
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
1.17M
        int s = 0;
401
5.85M
        for (int j = 0; j < 4; j++)
402
4.68M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
1.17M
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
1.17M
    }
405
182k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
182k
}
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
87.3k
{
414
87.3k
    const int is_sm = (angle >> 9) & 0x1;
415
87.3k
    const int enable_intra_edge_filter = angle >> 10;
416
87.3k
    angle &= 511;
417
87.3k
    assert(angle < 90);
418
87.3k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
87.3k
    pixel top_out[64 + 64];
420
87.3k
    const pixel *top;
421
87.3k
    int max_base_x;
422
87.3k
    const int upsample_above = enable_intra_edge_filter ?
423
69.2k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
87.3k
    if (upsample_above) {
425
38.3k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
38.3k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
38.3k
        top = top_out;
428
38.3k
        max_base_x = 2 * (width + height) - 2;
429
38.3k
        dx <<= 1;
430
48.9k
    } else {
431
48.9k
        const int filter_strength = enable_intra_edge_filter ?
432
30.9k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
48.9k
        if (filter_strength) {
434
23.5k
            filter_edge(top_out, width + height, 0, width + height,
435
23.5k
                        &topleft_in[1], -1, width + imin(width, height),
436
23.5k
                        filter_strength);
437
23.5k
            top = top_out;
438
23.5k
            max_base_x = width + height - 1;
439
25.4k
        } else {
440
25.4k
            top = &topleft_in[1];
441
25.4k
            max_base_x = width + imin(width, height) - 1;
442
25.4k
        }
443
48.9k
    }
444
87.3k
    const int base_inc = 1 + upsample_above;
445
1.06M
    for (int y = 0, xpos = dx; y < height;
446
979k
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
979k
    {
448
979k
        const int frac = xpos & 0x3E;
449
450
21.7M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
20.8M
            if (base < max_base_x) {
452
20.7M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
20.7M
                dst[x] = (v + 32) >> 6;
454
20.7M
            } else {
455
53.8k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
53.8k
                break;
457
53.8k
            }
458
20.8M
        }
459
979k
    }
460
87.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
169k
{
468
169k
    const int is_sm = (angle >> 9) & 0x1;
469
169k
    const int enable_intra_edge_filter = angle >> 10;
470
169k
    angle &= 511;
471
169k
    assert(angle > 90 && angle < 180);
472
169k
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
169k
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
169k
    const int upsample_left = enable_intra_edge_filter ?
475
126k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
169k
    const int upsample_above = enable_intra_edge_filter ?
477
126k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
169k
    pixel edge[64 + 64 + 1];
479
169k
    pixel *const topleft = &edge[64];
480
481
169k
    if (upsample_above) {
482
37.5k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
37.5k
                      HIGHBD_TAIL_SUFFIX);
484
37.5k
        dx <<= 1;
485
131k
    } else {
486
131k
        const int filter_strength = enable_intra_edge_filter ?
487
88.7k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
131k
        if (filter_strength) {
490
78.3k
            filter_edge(&topleft[1], width, 0, max_width,
491
78.3k
                        &topleft_in[1], -1, width,
492
78.3k
                        filter_strength);
493
78.3k
        } else {
494
53.1k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
53.1k
        }
496
131k
    }
497
169k
    if (upsample_left) {
498
34.9k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
34.9k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
34.9k
        dy <<= 1;
501
134k
    } else {
502
134k
        const int filter_strength = enable_intra_edge_filter ?
503
91.3k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
134k
        if (filter_strength) {
506
75.4k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
75.4k
                        &topleft_in[-height],
508
75.4k
                        0, height + 1, filter_strength);
509
75.4k
        } else {
510
58.7k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
58.7k
        }
512
134k
    }
513
169k
    *topleft = *topleft_in;
514
515
169k
    const int base_inc_x = 1 + upsample_above;
516
169k
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
2.17M
    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
46.2M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
44.2M
             x++, base_x += base_inc_x, ypos -= dy)
525
44.2M
        {
526
44.2M
            int v;
527
44.2M
            if (base_x >= 0) {
528
20.2M
                v = topleft[base_x] * (64 - frac_x) +
529
20.2M
                    topleft[base_x + 1] * frac_x;
530
23.9M
            } else {
531
23.9M
                const int base_y = ypos >> 6;
532
23.9M
                assert(base_y >= -(1 + upsample_left));
533
23.9M
                const int frac_y = ypos & 0x3E;
534
23.9M
                v = left[-base_y] * (64 - frac_y) +
535
23.9M
                    left[-(base_y + 1)] * frac_y;
536
23.9M
            }
537
44.2M
            dst[x] = (v + 32) >> 6;
538
44.2M
        }
539
2.00M
    }
540
169k
}
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
108k
{
548
108k
    const int is_sm = (angle >> 9) & 0x1;
549
108k
    const int enable_intra_edge_filter = angle >> 10;
550
108k
    angle &= 511;
551
108k
    assert(angle > 180);
552
108k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
108k
    pixel left_out[64 + 64];
554
108k
    const pixel *left;
555
108k
    int max_base_y;
556
108k
    const int upsample_left = enable_intra_edge_filter ?
557
87.2k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
108k
    if (upsample_left) {
559
71.7k
        upsample_edge(left_out, width + height,
560
71.7k
                      &topleft_in[-(width + height)],
561
71.7k
                      imax(width - height, 0), width + height + 1
562
71.7k
                      HIGHBD_TAIL_SUFFIX);
563
71.7k
        left = &left_out[2 * (width + height) - 2];
564
71.7k
        max_base_y = 2 * (width + height) - 2;
565
71.7k
        dy <<= 1;
566
71.7k
    } else {
567
36.5k
        const int filter_strength = enable_intra_edge_filter ?
568
21.0k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
36.5k
        if (filter_strength) {
571
13.7k
            filter_edge(left_out, width + height, 0, width + height,
572
13.7k
                        &topleft_in[-(width + height)],
573
13.7k
                        imax(width - height, 0), width + height + 1,
574
13.7k
                        filter_strength);
575
13.7k
            left = &left_out[width + height - 1];
576
13.7k
            max_base_y = width + height - 1;
577
22.8k
        } else {
578
22.8k
            left = &topleft_in[-1];
579
22.8k
            max_base_y = height + imin(width, height) - 1;
580
22.8k
        }
581
36.5k
    }
582
108k
    const int base_inc = 1 + upsample_left;
583
990k
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
882k
        const int frac = ypos & 0x3E;
585
586
15.5M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
14.6M
            if (base < max_base_y) {
588
14.6M
                const int v = left[-base] * (64 - frac) +
589
14.6M
                              left[-(base + 1)] * frac;
590
14.6M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
14.6M
            } else {
592
13.7k
                do {
593
13.7k
                    dst[y * PXSTRIDE(stride) + x] = left[-max_base_y];
594
13.7k
                } while (++y < height);
595
3.84k
                break;
596
3.84k
            }
597
14.6M
        }
598
882k
    }
599
108k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
4.83M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
4.83M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
4.83M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
4.83M
    flt_ptr[48] * p6
607
4.83M
#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
43.0k
{
624
43.0k
    filt_idx &= 511;
625
43.0k
    assert(filt_idx < 5);
626
627
43.0k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
43.0k
    const pixel *top = &topleft_in[1];
629
243k
    for (int y = 0; y < height; y += 2) {
630
200k
        const pixel *topleft = &topleft_in[-y];
631
200k
        const pixel *left = &topleft[-1];
632
200k
        ptrdiff_t left_stride = -1;
633
805k
        for (int x = 0; x < width; x += 4) {
634
604k
            const int p0 = *topleft;
635
604k
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
604k
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
604k
            pixel *ptr = &dst[x];
638
604k
            const int8_t *flt_ptr = filter;
639
640
1.81M
            for (int yy = 0; yy < 2; yy++) {
641
6.04M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
4.83M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
4.83M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
4.83M
                }
645
1.20M
                ptr += PXSTRIDE(stride);
646
1.20M
            }
647
604k
            left = &dst[x + 4 - 1];
648
604k
            left_stride = PXSTRIDE(stride);
649
604k
            top += 4;
650
604k
            topleft = &top[-1];
651
604k
        }
652
200k
        top = &dst[PXSTRIDE(stride)];
653
200k
        dst = &dst[PXSTRIDE(stride) * 2];
654
200k
    }
655
43.0k
}
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
71.0k
{
662
71.0k
    int y, x;
663
71.0k
    int16_t *const ac_orig = ac;
664
665
71.0k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
71.0k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
754k
    for (y = 0; y < height - 4 * h_pad; y++) {
669
8.60M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
7.92M
            int ac_sum = ypx[x << ss_hor];
671
7.92M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
7.92M
            if (ss_ver) {
673
405k
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
405k
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
405k
            }
676
7.92M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
7.92M
        }
678
778k
        for (; x < width; x++)
679
95.5k
            ac[x] = ac[x - 1];
680
683k
        ac += width;
681
683k
        ypx += PXSTRIDE(stride) << ss_ver;
682
683k
    }
683
84.7k
    for (; y < height; y++) {
684
13.6k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
13.6k
        ac += width;
686
13.6k
    }
687
688
71.0k
    const int log2sz = ctz(width) + ctz(height);
689
71.0k
    int sum = (1 << log2sz) >> 1;
690
767k
    for (ac = ac_orig, y = 0; y < height; y++) {
691
8.88M
        for (x = 0; x < width; x++)
692
8.19M
            sum += ac[x];
693
696k
        ac += width;
694
696k
    }
695
71.0k
    sum >>= log2sz;
696
697
    // subtract DC
698
767k
    for (ac = ac_orig, y = 0; y < height; y++) {
699
8.88M
        for (x = 0; x < width; x++)
700
8.18M
            ac[x] -= sum;
701
696k
        ac += width;
702
696k
    }
703
71.0k
}
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
71.0k
                             const int h_pad, const int cw, const int ch) \
709
71.0k
{ \
710
71.0k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
71.0k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
7.46k
                             const int h_pad, const int cw, const int ch) \
709
7.46k
{ \
710
7.46k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
7.46k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
2.35k
                             const int h_pad, const int cw, const int ch) \
709
2.35k
{ \
710
2.35k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
2.35k
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
61.2k
                             const int h_pad, const int cw, const int ch) \
709
61.2k
{ \
710
61.2k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
61.2k
}
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
6.08k
{
721
91.5k
    for (int y = 0; y < h; y++) {
722
765k
        for (int x = 0; x < w; x += 2) {
723
679k
            const int i = *idx++;
724
679k
            assert(!(i & 0x88));
725
679k
            dst[x + 0] = pal[i & 7];
726
679k
            dst[x + 1] = pal[i >> 4];
727
679k
        }
728
85.4k
        dst += PXSTRIDE(stride);
729
85.4k
    }
730
6.08k
}
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
28.0k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
28.0k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
28.0k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
28.0k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
28.0k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
28.0k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
28.0k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
28.0k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
28.0k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
28.0k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
28.0k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
28.0k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
28.0k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
28.0k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
28.0k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
28.0k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
28.0k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
28.0k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
28.0k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
28.0k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
28.0k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
28.0k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
28.0k
    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
28.0k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
12.8k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
12.8k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
12.8k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
12.8k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
12.8k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
12.8k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
12.8k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
12.8k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
12.8k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
12.8k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
12.8k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
12.8k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
12.8k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
12.8k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
12.8k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
12.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
12.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
12.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
12.8k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
12.8k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
12.8k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
12.8k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
12.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
12.8k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
15.1k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
15.1k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
15.1k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
15.1k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
15.1k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
15.1k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
15.1k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
15.1k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
15.1k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
15.1k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
15.1k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
15.1k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
15.1k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
15.1k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
15.1k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
15.1k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
15.1k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
15.1k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
15.1k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
15.1k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
15.1k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
15.1k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
15.1k
    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
15.1k
}