Coverage Report

Created: 2026-07-30 06:25

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
2.24M
{
43
2.24M
#if BITDEPTH == 8
44
2.24M
    assert(dc <= 0xff);
45
2.24M
    if (width > 4) {
46
612k
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
16.1M
        for (int y = 0; y < height; y++) {
48
87.4M
            for (int x = 0; x < width; x += sizeof(dcN))
49
71.8M
                *((uint64_t *) &dst[x]) = dcN;
50
15.5M
            dst += PXSTRIDE(stride);
51
15.5M
        }
52
1.63M
    } else {
53
1.63M
        const unsigned dcN = dc * 0x01010101U;
54
8.46M
        for (int y = 0; y < height; y++) {
55
13.6M
            for (int x = 0; x < width; x += sizeof(dcN))
56
6.83M
                *((unsigned *) &dst[x]) = dcN;
57
6.83M
            dst += PXSTRIDE(stride);
58
6.83M
        }
59
1.63M
    }
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
2.24M
}
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
211k
{
76
1.81M
    for (int y = 0; y < height; y++) {
77
16.2M
        for (int x = 0; x < width; x++) {
78
14.6M
            const int diff = alpha * ac[x];
79
14.6M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
14.6M
        }
81
1.60M
        ac += width;
82
1.60M
        dst += PXSTRIDE(stride);
83
1.60M
    }
84
211k
}
85
86
274k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
274k
    unsigned dc = width >> 1;
88
6.96M
    for (int i = 0; i < width; i++)
89
6.69M
       dc += topleft[1 + i];
90
274k
    return dc >> ctz(width);
91
274k
}
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
253k
{
99
253k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
253k
             HIGHBD_TAIL_SUFFIX);
101
253k
}
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.0k
{
109
20.0k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
20.0k
             HIGHBD_TAIL_SUFFIX);
111
20.0k
}
112
113
151k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
151k
    unsigned dc = height >> 1;
115
4.08M
    for (int i = 0; i < height; i++)
116
3.93M
       dc += topleft[-(1 + i)];
117
151k
    return dc >> ctz(height);
118
151k
}
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
136k
{
126
136k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
136k
             HIGHBD_TAIL_SUFFIX);
128
136k
}
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
15.2k
{
136
15.2k
    const unsigned dc = dc_gen_left(topleft, height);
137
15.2k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
15.2k
}
139
140
#if BITDEPTH == 8
141
580k
#define MULTIPLIER_1x2 0x5556
142
86.2k
#define MULTIPLIER_1x4 0x3334
143
333k
#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
2.81M
{
153
2.81M
    unsigned dc = (width + height) >> 1;
154
31.2M
    for (int i = 0; i < width; i++)
155
28.4M
       dc += topleft[i + 1];
156
30.9M
    for (int i = 0; i < height; i++)
157
28.1M
       dc += topleft[-(i + 1)];
158
2.81M
    dc >>= ctz(width + height);
159
160
2.81M
    if (width != height) {
161
333k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
333k
                                                           MULTIPLIER_1x2;
163
333k
        dc >>= BASE_SHIFT;
164
333k
    }
165
2.81M
    return dc;
166
2.81M
}
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
2.64M
{
174
2.64M
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
2.64M
             HIGHBD_TAIL_SUFFIX);
176
2.64M
}
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
173k
{
184
173k
    const unsigned dc = dc_gen(topleft, width, height);
185
173k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
173k
}
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
52.4k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
52.4k
    const int dc = 128;
202
52.4k
#endif
203
52.4k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
52.4k
}
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.21k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
3.21k
    const int dc = 128;
216
3.21k
#endif
217
3.21k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
3.21k
}
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
189k
{
226
3.16M
    for (int y = 0; y < height; y++) {
227
2.97M
        pixel_copy(dst, topleft + 1, width);
228
2.97M
        dst += PXSTRIDE(stride);
229
2.97M
    }
230
189k
}
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
230k
{
238
3.31M
    for (int y = 0; y < height; y++) {
239
3.08M
        pixel_set(dst, topleft[-(1 + y)], width);
240
3.08M
        dst += PXSTRIDE(stride);
241
3.08M
    }
242
230k
}
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
333k
{
250
333k
    const int topleft = tl_ptr[0];
251
4.87M
    for (int y = 0; y < height; y++) {
252
4.53M
        const int left = tl_ptr[-(y + 1)];
253
92.9M
        for (int x = 0; x < width; x++) {
254
88.4M
            const int top = tl_ptr[1 + x];
255
88.4M
            const int base = left + top - topleft;
256
88.4M
            const int ldiff = abs(left - base);
257
88.4M
            const int tdiff = abs(top - base);
258
88.4M
            const int tldiff = abs(topleft - base);
259
260
88.4M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
88.4M
                     tdiff <= tldiff ? top : topleft;
262
88.4M
        }
263
4.53M
        dst += PXSTRIDE(stride);
264
4.53M
    }
265
333k
}
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
344k
{
273
344k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
344k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
344k
    const int right = topleft[width], bottom = topleft[-height];
276
277
4.74M
    for (int y = 0; y < height; y++) {
278
112M
        for (int x = 0; x < width; x++) {
279
107M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
107M
                      (256 - weights_ver[y]) * bottom +
281
107M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
107M
                      (256 - weights_hor[x]) * right;
283
107M
            dst[x] = (pred + 256) >> 9;
284
107M
        }
285
4.39M
        dst += PXSTRIDE(stride);
286
4.39M
    }
287
344k
}
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
104k
{
295
104k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
104k
    const int bottom = topleft[-height];
297
298
1.39M
    for (int y = 0; y < height; y++) {
299
32.1M
        for (int x = 0; x < width; x++) {
300
30.8M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
30.8M
                      (256 - weights_ver[y]) * bottom;
302
30.8M
            dst[x] = (pred + 128) >> 8;
303
30.8M
        }
304
1.29M
        dst += PXSTRIDE(stride);
305
1.29M
    }
306
104k
}
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
127k
{
314
127k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
127k
    const int right = topleft[width];
316
317
1.77M
    for (int y = 0; y < height; y++) {
318
42.3M
        for (int x = 0; x < width; x++) {
319
40.6M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
40.6M
                      (256 - weights_hor[x]) * right;
321
40.6M
            dst[x] = (pred + 128) >> 8;
322
40.6M
        }
323
1.64M
        dst += PXSTRIDE(stride);
324
1.64M
    }
325
127k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
731k
{
330
731k
    if (is_sm) {
331
215k
        if (wh <= 8) {
332
25.8k
            if (angle >= 64) return 2;
333
9.87k
            if (angle >= 40) return 1;
334
189k
        } else if (wh <= 16) {
335
88.7k
            if (angle >= 48) return 2;
336
59.8k
            if (angle >= 20) return 1;
337
100k
        } else if (wh <= 24) {
338
44.7k
            if (angle >=  4) return 3;
339
55.9k
        } else {
340
55.9k
            return 3;
341
55.9k
        }
342
515k
    } else {
343
515k
        if (wh <= 8) {
344
71.4k
            if (angle >= 56) return 1;
345
444k
        } else if (wh <= 16) {
346
102k
            if (angle >= 40) return 1;
347
342k
        } else if (wh <= 24) {
348
125k
            if (angle >= 32) return 3;
349
65.0k
            if (angle >= 16) return 2;
350
32.7k
            if (angle >=  8) return 1;
351
216k
        } else if (wh <= 32) {
352
84.8k
            if (angle >= 32) return 3;
353
42.6k
            if (angle >=  4) return 2;
354
6.59k
            return 1;
355
131k
        } else {
356
131k
            return 3;
357
131k
        }
358
515k
    }
359
80.7k
    return 0;
360
731k
}
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
650k
{
367
650k
    static const uint8_t kernel[3][5] = {
368
650k
        { 0, 4, 8, 4, 0 },
369
650k
        { 0, 5, 6, 5, 0 },
370
650k
        { 2, 4, 4, 4, 2 }
371
650k
    };
372
373
650k
    assert(strength > 0);
374
650k
    int i = 0;
375
750k
    for (; i < imin(sz, lim_from); i++)
376
100k
        out[i] = in[iclip(i, from, to - 1)];
377
13.8M
    for (; i < imin(lim_to, sz); i++) {
378
13.2M
        int s = 0;
379
79.4M
        for (int j = 0; j < 5; j++)
380
66.2M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
13.2M
        out[i] = (s + 8) >> 4;
382
13.2M
    }
383
849k
    for (; i < sz; i++)
384
198k
        out[i] = in[iclip(i, from, to - 1)];
385
650k
}
386
387
1.03M
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
1.03M
    return angle < 40 && wh <= 16 >> is_sm;
389
1.03M
}
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
301k
{
395
301k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
301k
    int i;
397
2.71M
    for (i = 0; i < hsz - 1; i++) {
398
2.41M
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
2.41M
        int s = 0;
401
12.0M
        for (int j = 0; j < 4; j++)
402
9.64M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
2.41M
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
2.41M
    }
405
301k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
301k
}
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
202k
{
414
202k
    const int is_sm = (angle >> 9) & 0x1;
415
202k
    const int enable_intra_edge_filter = angle >> 10;
416
202k
    angle &= 511;
417
202k
    assert(angle < 90);
418
202k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
202k
    pixel top_out[64 + 64];
420
202k
    const pixel *top;
421
202k
    int max_base_x;
422
202k
    const int upsample_above = enable_intra_edge_filter ?
423
165k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
202k
    if (upsample_above) {
425
55.6k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
55.6k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
55.6k
        top = top_out;
428
55.6k
        max_base_x = 2 * (width + height) - 2;
429
55.6k
        dx <<= 1;
430
147k
    } else {
431
147k
        const int filter_strength = enable_intra_edge_filter ?
432
109k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
147k
        if (filter_strength) {
434
93.1k
            filter_edge(top_out, width + height, 0, width + height,
435
93.1k
                        &topleft_in[1], -1, width + imin(width, height),
436
93.1k
                        filter_strength);
437
93.1k
            top = top_out;
438
93.1k
            max_base_x = width + height - 1;
439
93.1k
        } else {
440
53.8k
            top = &topleft_in[1];
441
53.8k
            max_base_x = width + imin(width, height) - 1;
442
53.8k
        }
443
147k
    }
444
202k
    const int base_inc = 1 + upsample_above;
445
2.88M
    for (int y = 0, xpos = dx; y < height;
446
2.67M
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
2.67M
    {
448
2.67M
        const int frac = xpos & 0x3E;
449
450
69.5M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
66.9M
            if (base < max_base_x) {
452
66.8M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
66.8M
                dst[x] = (v + 32) >> 6;
454
66.8M
            } else {
455
125k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
125k
                break;
457
125k
            }
458
66.9M
        }
459
2.67M
    }
460
202k
}
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
406k
{
468
406k
    const int is_sm = (angle >> 9) & 0x1;
469
406k
    const int enable_intra_edge_filter = angle >> 10;
470
406k
    angle &= 511;
471
406k
    assert(angle > 90 && angle < 180);
472
406k
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
406k
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
406k
    const int upsample_left = enable_intra_edge_filter ?
475
335k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
406k
    const int upsample_above = enable_intra_edge_filter ?
477
335k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
406k
    pixel edge[64 + 64 + 1];
479
406k
    pixel *const topleft = &edge[64];
480
481
406k
    if (upsample_above) {
482
67.0k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
67.0k
                      HIGHBD_TAIL_SUFFIX);
484
67.0k
        dx <<= 1;
485
339k
    } else {
486
339k
        const int filter_strength = enable_intra_edge_filter ?
487
268k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
339k
        if (filter_strength) {
490
248k
            filter_edge(&topleft[1], width, 0, max_width,
491
248k
                        &topleft_in[1], -1, width,
492
248k
                        filter_strength);
493
248k
        } else {
494
90.8k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
90.8k
        }
496
339k
    }
497
406k
    if (upsample_left) {
498
79.2k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
79.2k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
79.2k
        dy <<= 1;
501
327k
    } else {
502
327k
        const int filter_strength = enable_intra_edge_filter ?
503
255k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
327k
        if (filter_strength) {
506
229k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
229k
                        &topleft_in[-height],
508
229k
                        0, height + 1, filter_strength);
509
229k
        } else {
510
97.8k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
97.8k
        }
512
327k
    }
513
406k
    *topleft = *topleft_in;
514
515
406k
    const int base_inc_x = 1 + upsample_above;
516
406k
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
5.42M
    for (int y = 0, xpos = ((1 + upsample_above) << 6) - dx; y < height;
518
5.01M
         y++, xpos -= dx, dst += PXSTRIDE(stride))
519
5.01M
    {
520
5.01M
        int base_x = xpos >> 6;
521
5.01M
        const int frac_x = xpos & 0x3E;
522
523
122M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
117M
             x++, base_x += base_inc_x, ypos -= dy)
525
117M
        {
526
117M
            int v;
527
117M
            if (base_x >= 0) {
528
54.7M
                v = topleft[base_x] * (64 - frac_x) +
529
54.7M
                    topleft[base_x + 1] * frac_x;
530
62.2M
            } else {
531
62.2M
                const int base_y = ypos >> 6;
532
62.2M
                assert(base_y >= -(1 + upsample_left));
533
62.2M
                const int frac_y = ypos & 0x3E;
534
62.2M
                v = left[-base_y] * (64 - frac_y) +
535
62.2M
                    left[-(base_y + 1)] * frac_y;
536
62.2M
            }
537
117M
            dst[x] = (v + 32) >> 6;
538
117M
        }
539
5.01M
    }
540
406k
}
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
243k
{
548
243k
    const int is_sm = (angle >> 9) & 0x1;
549
243k
    const int enable_intra_edge_filter = angle >> 10;
550
243k
    angle &= 511;
551
243k
    assert(angle > 180);
552
243k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
243k
    pixel left_out[64 + 64];
554
243k
    const pixel *left;
555
243k
    int max_base_y;
556
243k
    const int upsample_left = enable_intra_edge_filter ?
557
196k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
243k
    if (upsample_left) {
559
99.7k
        upsample_edge(left_out, width + height,
560
99.7k
                      &topleft_in[-(width + height)],
561
99.7k
                      imax(width - height, 0), width + height + 1
562
99.7k
                      HIGHBD_TAIL_SUFFIX);
563
99.7k
        left = &left_out[2 * (width + height) - 2];
564
99.7k
        max_base_y = 2 * (width + height) - 2;
565
99.7k
        dy <<= 1;
566
143k
    } else {
567
143k
        const int filter_strength = enable_intra_edge_filter ?
568
97.2k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
143k
        if (filter_strength) {
571
78.9k
            filter_edge(left_out, width + height, 0, width + height,
572
78.9k
                        &topleft_in[-(width + height)],
573
78.9k
                        imax(width - height, 0), width + height + 1,
574
78.9k
                        filter_strength);
575
78.9k
            left = &left_out[width + height - 1];
576
78.9k
            max_base_y = width + height - 1;
577
78.9k
        } else {
578
64.4k
            left = &topleft_in[-1];
579
64.4k
            max_base_y = height + imin(width, height) - 1;
580
64.4k
        }
581
143k
    }
582
243k
    const int base_inc = 1 + upsample_left;
583
3.04M
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
2.80M
        const int frac = ypos & 0x3E;
585
586
60.7M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
57.9M
            if (base < max_base_y) {
588
57.9M
                const int v = left[-base] * (64 - frac) +
589
57.9M
                              left[-(base + 1)] * frac;
590
57.9M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
57.9M
            } else {
592
11.6k
                do {
593
11.6k
                    dst[y * PXSTRIDE(stride) + x] = left[-max_base_y];
594
11.6k
                } while (++y < height);
595
4.37k
                break;
596
4.37k
            }
597
57.9M
        }
598
2.80M
    }
599
243k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
32.0M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
32.0M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
32.0M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
32.0M
    flt_ptr[48] * p6
607
32.0M
#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
299k
{
624
299k
    filt_idx &= 511;
625
299k
    assert(filt_idx < 5);
626
627
299k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
299k
    const pixel *top = &topleft_in[1];
629
1.63M
    for (int y = 0; y < height; y += 2) {
630
1.33M
        const pixel *topleft = &topleft_in[-y];
631
1.33M
        const pixel *left = &topleft[-1];
632
1.33M
        ptrdiff_t left_stride = -1;
633
5.33M
        for (int x = 0; x < width; x += 4) {
634
4.00M
            const int p0 = *topleft;
635
4.00M
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
4.00M
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
4.00M
            pixel *ptr = &dst[x];
638
4.00M
            const int8_t *flt_ptr = filter;
639
640
12.0M
            for (int yy = 0; yy < 2; yy++) {
641
40.0M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
32.0M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
32.0M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
32.0M
                }
645
8.01M
                ptr += PXSTRIDE(stride);
646
8.01M
            }
647
4.00M
            left = &dst[x + 4 - 1];
648
4.00M
            left_stride = PXSTRIDE(stride);
649
4.00M
            top += 4;
650
4.00M
            topleft = &top[-1];
651
4.00M
        }
652
1.33M
        top = &dst[PXSTRIDE(stride)];
653
1.33M
        dst = &dst[PXSTRIDE(stride) * 2];
654
1.33M
    }
655
299k
}
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
137k
{
662
137k
    int y, x;
663
137k
    int16_t *const ac_orig = ac;
664
665
137k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
137k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
1.16M
    for (y = 0; y < height - 4 * h_pad; y++) {
669
10.3M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
9.34M
            int ac_sum = ypx[x << ss_hor];
671
9.34M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
9.34M
            if (ss_ver) {
673
5.92M
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
5.92M
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
5.92M
            }
676
9.34M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
9.34M
        }
678
1.09M
        for (; x < width; x++)
679
68.2k
            ac[x] = ac[x - 1];
680
1.02M
        ac += width;
681
1.02M
        ypx += PXSTRIDE(stride) << ss_ver;
682
1.02M
    }
683
145k
    for (; y < height; y++) {
684
8.48k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
8.48k
        ac += width;
686
8.48k
    }
687
688
137k
    const int log2sz = ctz(width) + ctz(height);
689
137k
    int sum = (1 << log2sz) >> 1;
690
1.17M
    for (ac = ac_orig, y = 0; y < height; y++) {
691
10.5M
        for (x = 0; x < width; x++)
692
9.51M
            sum += ac[x];
693
1.03M
        ac += width;
694
1.03M
    }
695
137k
    sum >>= log2sz;
696
697
    // subtract DC
698
1.17M
    for (ac = ac_orig, y = 0; y < height; y++) {
699
10.5M
        for (x = 0; x < width; x++)
700
9.51M
            ac[x] -= sum;
701
1.03M
        ac += width;
702
1.03M
    }
703
137k
}
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
137k
                             const int h_pad, const int cw, const int ch) \
709
137k
{ \
710
137k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
137k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
114k
                             const int h_pad, const int cw, const int ch) \
709
114k
{ \
710
114k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
114k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
726
                             const int h_pad, const int cw, const int ch) \
709
726
{ \
710
726
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
726
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
22.0k
                             const int h_pad, const int cw, const int ch) \
709
22.0k
{ \
710
22.0k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
22.0k
}
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
8.03k
{
721
107k
    for (int y = 0; y < h; y++) {
722
794k
        for (int x = 0; x < w; x += 2) {
723
694k
            const int i = *idx++;
724
694k
            assert(!(i & 0x88));
725
694k
            dst[x + 0] = pal[i & 7];
726
694k
            dst[x + 1] = pal[i >> 4];
727
694k
        }
728
99.4k
        dst += PXSTRIDE(stride);
729
99.4k
    }
730
8.03k
}
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
33.4k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
33.4k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
33.4k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
33.4k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
33.4k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
33.4k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
33.4k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
33.4k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
33.4k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
33.4k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
33.4k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
33.4k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
33.4k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
33.4k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
33.4k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
33.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
33.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
33.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
33.4k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
33.4k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
33.4k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
33.4k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
33.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
33.4k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
14.7k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
14.7k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
14.7k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
14.7k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
14.7k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
14.7k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
14.7k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
14.7k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
14.7k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
14.7k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
14.7k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
14.7k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
14.7k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
14.7k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
14.7k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
14.7k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
14.7k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
14.7k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
14.7k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
14.7k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
14.7k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
14.7k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
14.7k
    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
14.7k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
18.6k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
18.6k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
18.6k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
18.6k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
18.6k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
18.6k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
18.6k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
18.6k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
18.6k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
18.6k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
18.6k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
18.6k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
18.6k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
18.6k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
18.6k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
18.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
18.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
18.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
18.6k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
18.6k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
18.6k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
18.6k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
18.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
18.6k
}