Coverage Report

Created: 2026-08-31 06: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
828k
{
43
828k
#if BITDEPTH == 8
44
828k
    assert(dc <= 0xff);
45
828k
    if (width > 4) {
46
570k
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
12.5M
        for (int y = 0; y < height; y++) {
48
60.7M
            for (int x = 0; x < width; x += sizeof(dcN))
49
48.7M
                *((uint64_t *) &dst[x]) = dcN;
50
11.9M
            dst += PXSTRIDE(stride);
51
11.9M
        }
52
570k
    } else {
53
257k
        const unsigned dcN = dc * 0x01010101U;
54
1.66M
        for (int y = 0; y < height; y++) {
55
2.82M
            for (int x = 0; x < width; x += sizeof(dcN))
56
1.41M
                *((unsigned *) &dst[x]) = dcN;
57
1.41M
            dst += PXSTRIDE(stride);
58
1.41M
        }
59
257k
    }
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
828k
}
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
300k
{
76
2.57M
    for (int y = 0; y < height; y++) {
77
23.7M
        for (int x = 0; x < width; x++) {
78
21.4M
            const int diff = alpha * ac[x];
79
21.4M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
21.4M
        }
81
2.27M
        ac += width;
82
2.27M
        dst += PXSTRIDE(stride);
83
2.27M
    }
84
300k
}
85
86
210k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
210k
    unsigned dc = width >> 1;
88
4.81M
    for (int i = 0; i < width; i++)
89
4.60M
       dc += topleft[1 + i];
90
210k
    return dc >> ctz(width);
91
210k
}
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
190k
{
99
190k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
190k
             HIGHBD_TAIL_SUFFIX);
101
190k
}
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
20.5k
{
109
20.5k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
20.5k
             HIGHBD_TAIL_SUFFIX);
111
20.5k
}
112
113
150k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
150k
    unsigned dc = height >> 1;
115
3.76M
    for (int i = 0; i < height; i++)
116
3.61M
       dc += topleft[-(1 + i)];
117
150k
    return dc >> ctz(height);
118
150k
}
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
133k
{
126
133k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
133k
             HIGHBD_TAIL_SUFFIX);
128
133k
}
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
17.2k
{
136
17.2k
    const unsigned dc = dc_gen_left(topleft, height);
137
17.2k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
17.2k
}
139
140
#if BITDEPTH == 8
141
917k
#define MULTIPLIER_1x2 0x5556
142
136k
#define MULTIPLIER_1x4 0x3334
143
526k
#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
1.62M
{
153
1.62M
    unsigned dc = (width + height) >> 1;
154
21.3M
    for (int i = 0; i < width; i++)
155
19.7M
       dc += topleft[i + 1];
156
20.8M
    for (int i = 0; i < height; i++)
157
19.1M
       dc += topleft[-(i + 1)];
158
1.62M
    dc >>= ctz(width + height);
159
160
1.62M
    if (width != height) {
161
526k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
526k
                                                           MULTIPLIER_1x2;
163
526k
        dc >>= BASE_SHIFT;
164
526k
    }
165
1.62M
    return dc;
166
1.62M
}
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
1.37M
{
174
1.37M
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
1.37M
             HIGHBD_TAIL_SUFFIX);
176
1.37M
}
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
259k
{
184
259k
    const unsigned dc = dc_gen(topleft, width, height);
185
259k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
259k
}
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
45.9k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
45.9k
    const int dc = 128;
202
45.9k
#endif
203
45.9k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
45.9k
}
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
2.93k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
2.93k
    const int dc = 128;
216
2.93k
#endif
217
2.93k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
2.93k
}
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
224k
{
226
3.69M
    for (int y = 0; y < height; y++) {
227
3.46M
        pixel_copy(dst, topleft + 1, width);
228
3.46M
        dst += PXSTRIDE(stride);
229
3.46M
    }
230
224k
}
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
333k
{
238
4.16M
    for (int y = 0; y < height; y++) {
239
3.82M
        pixel_set(dst, topleft[-(1 + y)], width);
240
3.82M
        dst += PXSTRIDE(stride);
241
3.82M
    }
242
333k
}
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
259k
{
250
259k
    const int topleft = tl_ptr[0];
251
3.37M
    for (int y = 0; y < height; y++) {
252
3.11M
        const int left = tl_ptr[-(y + 1)];
253
76.8M
        for (int x = 0; x < width; x++) {
254
73.7M
            const int top = tl_ptr[1 + x];
255
73.7M
            const int base = left + top - topleft;
256
73.7M
            const int ldiff = abs(left - base);
257
73.7M
            const int tdiff = abs(top - base);
258
73.7M
            const int tldiff = abs(topleft - base);
259
260
73.7M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
73.7M
                     tdiff <= tldiff ? top : topleft;
262
73.7M
        }
263
3.11M
        dst += PXSTRIDE(stride);
264
3.11M
    }
265
259k
}
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
477k
{
273
477k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
477k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
477k
    const int right = topleft[width], bottom = topleft[-height];
276
277
5.83M
    for (int y = 0; y < height; y++) {
278
117M
        for (int x = 0; x < width; x++) {
279
112M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
112M
                      (256 - weights_ver[y]) * bottom +
281
112M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
112M
                      (256 - weights_hor[x]) * right;
283
112M
            dst[x] = (pred + 256) >> 9;
284
112M
        }
285
5.36M
        dst += PXSTRIDE(stride);
286
5.36M
    }
287
477k
}
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
159k
{
295
159k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
159k
    const int bottom = topleft[-height];
297
298
1.81M
    for (int y = 0; y < height; y++) {
299
33.7M
        for (int x = 0; x < width; x++) {
300
32.0M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
32.0M
                      (256 - weights_ver[y]) * bottom;
302
32.0M
            dst[x] = (pred + 128) >> 8;
303
32.0M
        }
304
1.65M
        dst += PXSTRIDE(stride);
305
1.65M
    }
306
159k
}
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
186k
{
314
186k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
186k
    const int right = topleft[width];
316
317
2.47M
    for (int y = 0; y < height; y++) {
318
60.2M
        for (int x = 0; x < width; x++) {
319
57.9M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
57.9M
                      (256 - weights_hor[x]) * right;
321
57.9M
            dst[x] = (pred + 128) >> 8;
322
57.9M
        }
323
2.29M
        dst += PXSTRIDE(stride);
324
2.29M
    }
325
186k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
970k
{
330
970k
    if (is_sm) {
331
317k
        if (wh <= 8) {
332
28.9k
            if (angle >= 64) return 2;
333
13.4k
            if (angle >= 40) return 1;
334
288k
        } else if (wh <= 16) {
335
153k
            if (angle >= 48) return 2;
336
104k
            if (angle >= 20) return 1;
337
134k
        } else if (wh <= 24) {
338
58.4k
            if (angle >=  4) return 3;
339
76.3k
        } else {
340
76.3k
            return 3;
341
76.3k
        }
342
652k
    } else {
343
652k
        if (wh <= 8) {
344
88.3k
            if (angle >= 56) return 1;
345
564k
        } else if (wh <= 16) {
346
159k
            if (angle >= 40) return 1;
347
405k
        } else if (wh <= 24) {
348
152k
            if (angle >= 32) return 3;
349
78.5k
            if (angle >= 16) return 2;
350
39.0k
            if (angle >=  8) return 1;
351
252k
        } else if (wh <= 32) {
352
107k
            if (angle >= 32) return 3;
353
52.3k
            if (angle >=  4) return 2;
354
9.71k
            return 1;
355
145k
        } else {
356
145k
            return 3;
357
145k
        }
358
652k
    }
359
123k
    return 0;
360
970k
}
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
846k
{
367
846k
    static const uint8_t kernel[3][5] = {
368
846k
        { 0, 4, 8, 4, 0 },
369
846k
        { 0, 5, 6, 5, 0 },
370
846k
        { 2, 4, 4, 4, 2 }
371
846k
    };
372
373
846k
    assert(strength > 0);
374
846k
    int i = 0;
375
964k
    for (; i < imin(sz, lim_from); i++)
376
118k
        out[i] = in[iclip(i, from, to - 1)];
377
17.5M
    for (; i < imin(lim_to, sz); i++) {
378
16.7M
        int s = 0;
379
100M
        for (int j = 0; j < 5; j++)
380
83.7M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
16.7M
        out[i] = (s + 8) >> 4;
382
16.7M
    }
383
989k
    for (; i < sz; i++)
384
142k
        out[i] = in[iclip(i, from, to - 1)];
385
846k
}
386
387
1.37M
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
1.37M
    return angle < 40 && wh <= 16 >> is_sm;
389
1.37M
}
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
408k
{
395
408k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
408k
    int i;
397
3.89M
    for (i = 0; i < hsz - 1; i++) {
398
3.48M
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
3.48M
        int s = 0;
401
17.4M
        for (int j = 0; j < 4; j++)
402
13.9M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
3.48M
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
3.48M
    }
405
408k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
408k
}
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
282k
{
414
282k
    const int is_sm = (angle >> 9) & 0x1;
415
282k
    const int enable_intra_edge_filter = angle >> 10;
416
282k
    angle &= 511;
417
282k
    assert(angle < 90);
418
282k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
282k
    pixel top_out[64 + 64];
420
282k
    const pixel *top;
421
282k
    int max_base_x;
422
282k
    const int upsample_above = enable_intra_edge_filter ?
423
229k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
282k
    if (upsample_above) {
425
83.4k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
83.4k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
83.4k
        top = top_out;
428
83.4k
        max_base_x = 2 * (width + height) - 2;
429
83.4k
        dx <<= 1;
430
199k
    } else {
431
199k
        const int filter_strength = enable_intra_edge_filter ?
432
146k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
199k
        if (filter_strength) {
434
119k
            filter_edge(top_out, width + height, 0, width + height,
435
119k
                        &topleft_in[1], -1, width + imin(width, height),
436
119k
                        filter_strength);
437
119k
            top = top_out;
438
119k
            max_base_x = width + height - 1;
439
119k
        } else {
440
80.0k
            top = &topleft_in[1];
441
80.0k
            max_base_x = width + imin(width, height) - 1;
442
80.0k
        }
443
199k
    }
444
282k
    const int base_inc = 1 + upsample_above;
445
3.48M
    for (int y = 0, xpos = dx; y < height;
446
3.20M
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
3.20M
    {
448
3.20M
        const int frac = xpos & 0x3E;
449
450
70.8M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
67.8M
            if (base < max_base_x) {
452
67.6M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
67.6M
                dst[x] = (v + 32) >> 6;
454
67.6M
            } else {
455
176k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
176k
                break;
457
176k
            }
458
67.8M
        }
459
3.20M
    }
460
282k
}
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
548k
{
468
548k
    const int is_sm = (angle >> 9) & 0x1;
469
548k
    const int enable_intra_edge_filter = angle >> 10;
470
548k
    angle &= 511;
471
548k
    assert(angle > 90 && angle < 180);
472
548k
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
548k
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
548k
    const int upsample_left = enable_intra_edge_filter ?
475
440k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
548k
    const int upsample_above = enable_intra_edge_filter ?
477
440k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
548k
    pixel edge[64 + 64 + 1];
479
548k
    pixel *const topleft = &edge[64];
480
481
548k
    if (upsample_above) {
482
81.0k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
81.0k
                      HIGHBD_TAIL_SUFFIX);
484
81.0k
        dx <<= 1;
485
467k
    } else {
486
467k
        const int filter_strength = enable_intra_edge_filter ?
487
359k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
467k
        if (filter_strength) {
490
328k
            filter_edge(&topleft[1], width, 0, max_width,
491
328k
                        &topleft_in[1], -1, width,
492
328k
                        filter_strength);
493
328k
        } else {
494
139k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
139k
        }
496
467k
    }
497
548k
    if (upsample_left) {
498
114k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
114k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
114k
        dy <<= 1;
501
433k
    } else {
502
433k
        const int filter_strength = enable_intra_edge_filter ?
503
326k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
433k
        if (filter_strength) {
506
289k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
289k
                        &topleft_in[-height],
508
289k
                        0, height + 1, filter_strength);
509
289k
        } else {
510
144k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
144k
        }
512
433k
    }
513
548k
    *topleft = *topleft_in;
514
515
548k
    const int base_inc_x = 1 + upsample_above;
516
548k
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
6.61M
    for (int y = 0, xpos = ((1 + upsample_above) << 6) - dx; y < height;
518
6.06M
         y++, xpos -= dx, dst += PXSTRIDE(stride))
519
6.06M
    {
520
6.06M
        int base_x = xpos >> 6;
521
6.06M
        const int frac_x = xpos & 0x3E;
522
523
128M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
122M
             x++, base_x += base_inc_x, ypos -= dy)
525
122M
        {
526
122M
            int v;
527
122M
            if (base_x >= 0) {
528
56.1M
                v = topleft[base_x] * (64 - frac_x) +
529
56.1M
                    topleft[base_x + 1] * frac_x;
530
65.9M
            } else {
531
65.9M
                const int base_y = ypos >> 6;
532
65.9M
                assert(base_y >= -(1 + upsample_left));
533
65.9M
                const int frac_y = ypos & 0x3E;
534
65.9M
                v = left[-base_y] * (64 - frac_y) +
535
65.9M
                    left[-(base_y + 1)] * frac_y;
536
65.9M
            }
537
122M
            dst[x] = (v + 32) >> 6;
538
122M
        }
539
6.06M
    }
540
548k
}
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
337k
{
548
337k
    const int is_sm = (angle >> 9) & 0x1;
549
337k
    const int enable_intra_edge_filter = angle >> 10;
550
337k
    angle &= 511;
551
337k
    assert(angle > 180);
552
337k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
337k
    pixel left_out[64 + 64];
554
337k
    const pixel *left;
555
337k
    int max_base_y;
556
337k
    const int upsample_left = enable_intra_edge_filter ?
557
266k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
337k
    if (upsample_left) {
559
129k
        upsample_edge(left_out, width + height,
560
129k
                      &topleft_in[-(width + height)],
561
129k
                      imax(width - height, 0), width + height + 1
562
129k
                      HIGHBD_TAIL_SUFFIX);
563
129k
        left = &left_out[2 * (width + height) - 2];
564
129k
        max_base_y = 2 * (width + height) - 2;
565
129k
        dy <<= 1;
566
208k
    } else {
567
208k
        const int filter_strength = enable_intra_edge_filter ?
568
137k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
208k
        if (filter_strength) {
571
109k
            filter_edge(left_out, width + height, 0, width + height,
572
109k
                        &topleft_in[-(width + height)],
573
109k
                        imax(width - height, 0), width + height + 1,
574
109k
                        filter_strength);
575
109k
            left = &left_out[width + height - 1];
576
109k
            max_base_y = width + height - 1;
577
109k
        } else {
578
99.2k
            left = &topleft_in[-1];
579
99.2k
            max_base_y = height + imin(width, height) - 1;
580
99.2k
        }
581
208k
    }
582
337k
    const int base_inc = 1 + upsample_left;
583
4.25M
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
3.91M
        const int frac = ypos & 0x3E;
585
586
84.2M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
80.3M
            if (base < max_base_y) {
588
80.3M
                const int v = left[-base] * (64 - frac) +
589
80.3M
                              left[-(base + 1)] * frac;
590
80.3M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
80.3M
            } else {
592
30.4k
                do {
593
30.4k
                    dst[y * PXSTRIDE(stride) + x] = left[-max_base_y];
594
30.4k
                } while (++y < height);
595
9.78k
                break;
596
9.78k
            }
597
80.3M
        }
598
3.91M
    }
599
337k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
35.2M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
35.2M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
35.2M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
35.2M
    flt_ptr[48] * p6
607
35.2M
#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
352k
{
624
352k
    filt_idx &= 511;
625
352k
    assert(filt_idx < 5);
626
627
352k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
352k
    const pixel *top = &topleft_in[1];
629
1.88M
    for (int y = 0; y < height; y += 2) {
630
1.53M
        const pixel *topleft = &topleft_in[-y];
631
1.53M
        const pixel *left = &topleft[-1];
632
1.53M
        ptrdiff_t left_stride = -1;
633
5.94M
        for (int x = 0; x < width; x += 4) {
634
4.41M
            const int p0 = *topleft;
635
4.41M
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
4.41M
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
4.41M
            pixel *ptr = &dst[x];
638
4.41M
            const int8_t *flt_ptr = filter;
639
640
13.2M
            for (int yy = 0; yy < 2; yy++) {
641
44.1M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
35.2M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
35.2M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
35.2M
                }
645
8.82M
                ptr += PXSTRIDE(stride);
646
8.82M
            }
647
4.41M
            left = &dst[x + 4 - 1];
648
4.41M
            left_stride = PXSTRIDE(stride);
649
4.41M
            top += 4;
650
4.41M
            topleft = &top[-1];
651
4.41M
        }
652
1.53M
        top = &dst[PXSTRIDE(stride)];
653
1.53M
        dst = &dst[PXSTRIDE(stride) * 2];
654
1.53M
    }
655
352k
}
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
195k
{
662
195k
    int y, x;
663
195k
    int16_t *const ac_orig = ac;
664
665
195k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
195k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
1.65M
    for (y = 0; y < height - 4 * h_pad; y++) {
669
15.0M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
13.6M
            int ac_sum = ypx[x << ss_hor];
671
13.6M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
13.6M
            if (ss_ver) {
673
8.77M
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
8.77M
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
8.77M
            }
676
13.6M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
13.6M
        }
678
1.54M
        for (; x < width; x++)
679
82.8k
            ac[x] = ac[x - 1];
680
1.45M
        ac += width;
681
1.45M
        ypx += PXSTRIDE(stride) << ss_ver;
682
1.45M
    }
683
205k
    for (; y < height; y++) {
684
9.80k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
9.80k
        ac += width;
686
9.80k
    }
687
688
195k
    const int log2sz = ctz(width) + ctz(height);
689
195k
    int sum = (1 << log2sz) >> 1;
690
1.66M
    for (ac = ac_orig, y = 0; y < height; y++) {
691
15.3M
        for (x = 0; x < width; x++)
692
13.8M
            sum += ac[x];
693
1.46M
        ac += width;
694
1.46M
    }
695
195k
    sum >>= log2sz;
696
697
    // subtract DC
698
1.66M
    for (ac = ac_orig, y = 0; y < height; y++) {
699
15.3M
        for (x = 0; x < width; x++)
700
13.8M
            ac[x] -= sum;
701
1.46M
        ac += width;
702
1.46M
    }
703
195k
}
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
195k
                             const int h_pad, const int cw, const int ch) \
709
195k
{ \
710
195k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
195k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
167k
                             const int h_pad, const int cw, const int ch) \
709
167k
{ \
710
167k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
167k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
662
                             const int h_pad, const int cw, const int ch) \
709
662
{ \
710
662
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
662
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
27.5k
                             const int h_pad, const int cw, const int ch) \
709
27.5k
{ \
710
27.5k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
27.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
16.3k
{
721
196k
    for (int y = 0; y < h; y++) {
722
1.53M
        for (int x = 0; x < w; x += 2) {
723
1.35M
            const int i = *idx++;
724
1.35M
            assert(!(i & 0x88));
725
1.35M
            dst[x + 0] = pal[i & 7];
726
1.35M
            dst[x + 1] = pal[i >> 4];
727
1.35M
        }
728
179k
        dst += PXSTRIDE(stride);
729
179k
    }
730
16.3k
}
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
30.6k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
30.6k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
30.6k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
30.6k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
30.6k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
30.6k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
30.6k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
30.6k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
30.6k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
30.6k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
30.6k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
30.6k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
30.6k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
30.6k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
30.6k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
30.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
30.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
30.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
30.6k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
30.6k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
30.6k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
30.6k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
30.6k
    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
30.6k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
13.6k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
13.6k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
13.6k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
13.6k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
13.6k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
13.6k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
13.6k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
13.6k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
13.6k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
13.6k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
13.6k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
13.6k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
13.6k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
13.6k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
13.6k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
13.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
13.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
13.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
13.6k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
13.6k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
13.6k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
13.6k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
13.6k
    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
13.6k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
17.0k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
17.0k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
17.0k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
17.0k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
17.0k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
17.0k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
17.0k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
17.0k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
17.0k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
17.0k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
17.0k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
17.0k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
17.0k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
17.0k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
17.0k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
17.0k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
17.0k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
17.0k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
17.0k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
17.0k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
17.0k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
17.0k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
17.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
17.0k
}