Coverage Report

Created: 2026-09-01 06:57

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
1.97M
{
43
1.97M
#if BITDEPTH == 8
44
1.97M
    assert(dc <= 0xff);
45
1.97M
    if (width > 4) {
46
1.21M
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
31.0M
        for (int y = 0; y < height; y++) {
48
155M
            for (int x = 0; x < width; x += sizeof(dcN))
49
126M
                *((uint64_t *) &dst[x]) = dcN;
50
29.8M
            dst += PXSTRIDE(stride);
51
29.8M
        }
52
1.21M
    } else {
53
755k
        const unsigned dcN = dc * 0x01010101U;
54
4.33M
        for (int y = 0; y < height; y++) {
55
7.15M
            for (int x = 0; x < width; x += sizeof(dcN))
56
3.57M
                *((unsigned *) &dst[x]) = dcN;
57
3.57M
            dst += PXSTRIDE(stride);
58
3.57M
        }
59
755k
    }
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
1.97M
}
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
644k
{
76
5.90M
    for (int y = 0; y < height; y++) {
77
58.3M
        for (int x = 0; x < width; x++) {
78
53.0M
            const int diff = alpha * ac[x];
79
53.0M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
53.0M
        }
81
5.26M
        ac += width;
82
5.26M
        dst += PXSTRIDE(stride);
83
5.26M
    }
84
644k
}
85
86
862k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
862k
    unsigned dc = width >> 1;
88
18.9M
    for (int i = 0; i < width; i++)
89
18.0M
       dc += topleft[1 + i];
90
862k
    return dc >> ctz(width);
91
862k
}
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
767k
{
99
767k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
767k
             HIGHBD_TAIL_SUFFIX);
101
767k
}
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
94.4k
{
109
94.4k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
94.4k
             HIGHBD_TAIL_SUFFIX);
111
94.4k
}
112
113
505k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
505k
    unsigned dc = height >> 1;
115
10.3M
    for (int i = 0; i < height; i++)
116
9.87M
       dc += topleft[-(1 + i)];
117
505k
    return dc >> ctz(height);
118
505k
}
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
423k
{
126
423k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
423k
             HIGHBD_TAIL_SUFFIX);
128
423k
}
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
82.4k
{
136
82.4k
    const unsigned dc = dc_gen_left(topleft, height);
137
82.4k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
82.4k
}
139
140
#if BITDEPTH == 8
141
1.53M
#define MULTIPLIER_1x2 0x5556
142
235k
#define MULTIPLIER_1x4 0x3334
143
887k
#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
3.97M
{
153
3.97M
    unsigned dc = (width + height) >> 1;
154
46.7M
    for (int i = 0; i < width; i++)
155
42.7M
       dc += topleft[i + 1];
156
45.9M
    for (int i = 0; i < height; i++)
157
41.9M
       dc += topleft[-(i + 1)];
158
3.97M
    dc >>= ctz(width + height);
159
160
3.97M
    if (width != height) {
161
887k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
887k
                                                           MULTIPLIER_1x2;
163
887k
        dc >>= BASE_SHIFT;
164
887k
    }
165
3.97M
    return dc;
166
3.97M
}
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
3.52M
{
174
3.52M
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
3.52M
             HIGHBD_TAIL_SUFFIX);
176
3.52M
}
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
458k
{
184
458k
    const unsigned dc = dc_gen(topleft, width, height);
185
458k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
458k
}
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
140k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
140k
    const int dc = 128;
202
140k
#endif
203
140k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
140k
}
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
9.13k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
9.13k
    const int dc = 128;
216
9.13k
#endif
217
9.13k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
9.13k
}
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
544k
{
226
8.31M
    for (int y = 0; y < height; y++) {
227
7.77M
        pixel_copy(dst, topleft + 1, width);
228
7.77M
        dst += PXSTRIDE(stride);
229
7.77M
    }
230
544k
}
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
747k
{
238
9.84M
    for (int y = 0; y < height; y++) {
239
9.09M
        pixel_set(dst, topleft[-(1 + y)], width);
240
9.09M
        dst += PXSTRIDE(stride);
241
9.09M
    }
242
747k
}
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
653k
{
250
653k
    const int topleft = tl_ptr[0];
251
9.13M
    for (int y = 0; y < height; y++) {
252
8.47M
        const int left = tl_ptr[-(y + 1)];
253
172M
        for (int x = 0; x < width; x++) {
254
164M
            const int top = tl_ptr[1 + x];
255
164M
            const int base = left + top - topleft;
256
164M
            const int ldiff = abs(left - base);
257
164M
            const int tdiff = abs(top - base);
258
164M
            const int tldiff = abs(topleft - base);
259
260
164M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
164M
                     tdiff <= tldiff ? top : topleft;
262
164M
        }
263
8.47M
        dst += PXSTRIDE(stride);
264
8.47M
    }
265
653k
}
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
1.07M
{
273
1.07M
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
1.07M
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
1.07M
    const int right = topleft[width], bottom = topleft[-height];
276
277
13.3M
    for (int y = 0; y < height; y++) {
278
283M
        for (int x = 0; x < width; x++) {
279
271M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
271M
                      (256 - weights_ver[y]) * bottom +
281
271M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
271M
                      (256 - weights_hor[x]) * right;
283
271M
            dst[x] = (pred + 256) >> 9;
284
271M
        }
285
12.3M
        dst += PXSTRIDE(stride);
286
12.3M
    }
287
1.07M
}
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
321k
{
295
321k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
321k
    const int bottom = topleft[-height];
297
298
3.80M
    for (int y = 0; y < height; y++) {
299
77.8M
        for (int x = 0; x < width; x++) {
300
74.3M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
74.3M
                      (256 - weights_ver[y]) * bottom;
302
74.3M
            dst[x] = (pred + 128) >> 8;
303
74.3M
        }
304
3.48M
        dst += PXSTRIDE(stride);
305
3.48M
    }
306
321k
}
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
393k
{
314
393k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
393k
    const int right = topleft[width];
316
317
5.36M
    for (int y = 0; y < height; y++) {
318
124M
        for (int x = 0; x < width; x++) {
319
119M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
119M
                      (256 - weights_hor[x]) * right;
321
119M
            dst[x] = (pred + 128) >> 8;
322
119M
        }
323
4.97M
        dst += PXSTRIDE(stride);
324
4.97M
    }
325
393k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
1.85M
{
330
1.85M
    if (is_sm) {
331
562k
        if (wh <= 8) {
332
77.9k
            if (angle >= 64) return 2;
333
30.6k
            if (angle >= 40) return 1;
334
484k
        } else if (wh <= 16) {
335
239k
            if (angle >= 48) return 2;
336
161k
            if (angle >= 20) return 1;
337
244k
        } else if (wh <= 24) {
338
97.6k
            if (angle >=  4) return 3;
339
147k
        } else {
340
147k
            return 3;
341
147k
        }
342
1.29M
    } else {
343
1.29M
        if (wh <= 8) {
344
271k
            if (angle >= 56) return 1;
345
1.02M
        } else if (wh <= 16) {
346
246k
            if (angle >= 40) return 1;
347
778k
        } else if (wh <= 24) {
348
265k
            if (angle >= 32) return 3;
349
137k
            if (angle >= 16) return 2;
350
69.5k
            if (angle >=  8) return 1;
351
513k
        } else if (wh <= 32) {
352
197k
            if (angle >= 32) return 3;
353
100k
            if (angle >=  4) return 2;
354
21.9k
            return 1;
355
316k
        } else {
356
316k
            return 3;
357
316k
        }
358
1.29M
    }
359
230k
    return 0;
360
1.85M
}
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
1.62M
{
367
1.62M
    static const uint8_t kernel[3][5] = {
368
1.62M
        { 0, 4, 8, 4, 0 },
369
1.62M
        { 0, 5, 6, 5, 0 },
370
1.62M
        { 2, 4, 4, 4, 2 }
371
1.62M
    };
372
373
1.62M
    assert(strength > 0);
374
1.62M
    int i = 0;
375
1.90M
    for (; i < imin(sz, lim_from); i++)
376
274k
        out[i] = in[iclip(i, from, to - 1)];
377
33.1M
    for (; i < imin(lim_to, sz); i++) {
378
31.5M
        int s = 0;
379
189M
        for (int j = 0; j < 5; j++)
380
157M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
31.5M
        out[i] = (s + 8) >> 4;
382
31.5M
    }
383
2.12M
    for (; i < sz; i++)
384
499k
        out[i] = in[iclip(i, from, to - 1)];
385
1.62M
}
386
387
2.81M
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
2.81M
    return angle < 40 && wh <= 16 >> is_sm;
389
2.81M
}
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
958k
{
395
958k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
958k
    int i;
397
8.11M
    for (i = 0; i < hsz - 1; i++) {
398
7.15M
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
7.15M
        int s = 0;
401
35.7M
        for (int j = 0; j < 4; j++)
402
28.5M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
7.15M
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
7.15M
    }
405
958k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
958k
}
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
571k
{
414
571k
    const int is_sm = (angle >> 9) & 0x1;
415
571k
    const int enable_intra_edge_filter = angle >> 10;
416
571k
    angle &= 511;
417
571k
    assert(angle < 90);
418
571k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
571k
    pixel top_out[64 + 64];
420
571k
    const pixel *top;
421
571k
    int max_base_x;
422
571k
    const int upsample_above = enable_intra_edge_filter ?
423
459k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
571k
    if (upsample_above) {
425
187k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
187k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
187k
        top = top_out;
428
187k
        max_base_x = 2 * (width + height) - 2;
429
187k
        dx <<= 1;
430
383k
    } else {
431
383k
        const int filter_strength = enable_intra_edge_filter ?
432
271k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
383k
        if (filter_strength) {
434
218k
            filter_edge(top_out, width + height, 0, width + height,
435
218k
                        &topleft_in[1], -1, width + imin(width, height),
436
218k
                        filter_strength);
437
218k
            top = top_out;
438
218k
            max_base_x = width + height - 1;
439
218k
        } else {
440
165k
            top = &topleft_in[1];
441
165k
            max_base_x = width + imin(width, height) - 1;
442
165k
        }
443
383k
    }
444
571k
    const int base_inc = 1 + upsample_above;
445
7.23M
    for (int y = 0, xpos = dx; y < height;
446
6.66M
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
6.66M
    {
448
6.66M
        const int frac = xpos & 0x3E;
449
450
148M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
142M
            if (base < max_base_x) {
452
142M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
142M
                dst[x] = (v + 32) >> 6;
454
142M
            } else {
455
360k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
360k
                break;
457
360k
            }
458
142M
        }
459
6.66M
    }
460
571k
}
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
1.11M
{
468
1.11M
    const int is_sm = (angle >> 9) & 0x1;
469
1.11M
    const int enable_intra_edge_filter = angle >> 10;
470
1.11M
    angle &= 511;
471
1.11M
    assert(angle > 90 && angle < 180);
472
1.11M
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
1.11M
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
1.11M
    const int upsample_left = enable_intra_edge_filter ?
475
900k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
1.11M
    const int upsample_above = enable_intra_edge_filter ?
477
900k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
1.11M
    pixel edge[64 + 64 + 1];
479
1.11M
    pixel *const topleft = &edge[64];
480
481
1.11M
    if (upsample_above) {
482
177k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
177k
                      HIGHBD_TAIL_SUFFIX);
484
177k
        dx <<= 1;
485
939k
    } else {
486
939k
        const int filter_strength = enable_intra_edge_filter ?
487
722k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
939k
        if (filter_strength) {
490
664k
            filter_edge(&topleft[1], width, 0, max_width,
491
664k
                        &topleft_in[1], -1, width,
492
664k
                        filter_strength);
493
664k
        } else {
494
275k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
275k
        }
496
939k
    }
497
1.11M
    if (upsample_left) {
498
274k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
274k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
274k
        dy <<= 1;
501
842k
    } else {
502
842k
        const int filter_strength = enable_intra_edge_filter ?
503
625k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
842k
        if (filter_strength) {
506
551k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
551k
                        &topleft_in[-height],
508
551k
                        0, height + 1, filter_strength);
509
551k
        } else {
510
290k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
290k
        }
512
842k
    }
513
1.11M
    *topleft = *topleft_in;
514
515
1.11M
    const int base_inc_x = 1 + upsample_above;
516
1.11M
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
13.9M
    for (int y = 0, xpos = ((1 + upsample_above) << 6) - dx; y < height;
518
12.8M
         y++, xpos -= dx, dst += PXSTRIDE(stride))
519
12.8M
    {
520
12.8M
        int base_x = xpos >> 6;
521
12.8M
        const int frac_x = xpos & 0x3E;
522
523
287M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
274M
             x++, base_x += base_inc_x, ypos -= dy)
525
274M
        {
526
274M
            int v;
527
274M
            if (base_x >= 0) {
528
123M
                v = topleft[base_x] * (64 - frac_x) +
529
123M
                    topleft[base_x + 1] * frac_x;
530
150M
            } else {
531
150M
                const int base_y = ypos >> 6;
532
150M
                assert(base_y >= -(1 + upsample_left));
533
150M
                const int frac_y = ypos & 0x3E;
534
150M
                v = left[-base_y] * (64 - frac_y) +
535
150M
                    left[-(base_y + 1)] * frac_y;
536
150M
            }
537
274M
            dst[x] = (v + 32) >> 6;
538
274M
        }
539
12.8M
    }
540
1.11M
}
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
691k
{
548
691k
    const int is_sm = (angle >> 9) & 0x1;
549
691k
    const int enable_intra_edge_filter = angle >> 10;
550
691k
    angle &= 511;
551
691k
    assert(angle > 180);
552
691k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
691k
    pixel left_out[64 + 64];
554
691k
    const pixel *left;
555
691k
    int max_base_y;
556
691k
    const int upsample_left = enable_intra_edge_filter ?
557
558k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
691k
    if (upsample_left) {
559
319k
        upsample_edge(left_out, width + height,
560
319k
                      &topleft_in[-(width + height)],
561
319k
                      imax(width - height, 0), width + height + 1
562
319k
                      HIGHBD_TAIL_SUFFIX);
563
319k
        left = &left_out[2 * (width + height) - 2];
564
319k
        max_base_y = 2 * (width + height) - 2;
565
319k
        dy <<= 1;
566
372k
    } else {
567
372k
        const int filter_strength = enable_intra_edge_filter ?
568
239k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
372k
        if (filter_strength) {
571
194k
            filter_edge(left_out, width + height, 0, width + height,
572
194k
                        &topleft_in[-(width + height)],
573
194k
                        imax(width - height, 0), width + height + 1,
574
194k
                        filter_strength);
575
194k
            left = &left_out[width + height - 1];
576
194k
            max_base_y = width + height - 1;
577
194k
        } else {
578
178k
            left = &topleft_in[-1];
579
178k
            max_base_y = height + imin(width, height) - 1;
580
178k
        }
581
372k
    }
582
691k
    const int base_inc = 1 + upsample_left;
583
8.26M
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
7.57M
        const int frac = ypos & 0x3E;
585
586
156M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
148M
            if (base < max_base_y) {
588
148M
                const int v = left[-base] * (64 - frac) +
589
148M
                              left[-(base + 1)] * frac;
590
148M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
148M
            } else {
592
93.4k
                do {
593
93.4k
                    dst[y * PXSTRIDE(stride) + x] = left[-max_base_y];
594
93.4k
                } while (++y < height);
595
30.1k
                break;
596
30.1k
            }
597
148M
        }
598
7.57M
    }
599
691k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
59.6M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
59.6M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
59.6M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
59.6M
    flt_ptr[48] * p6
607
59.6M
#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
640k
{
624
640k
    filt_idx &= 511;
625
640k
    assert(filt_idx < 5);
626
627
640k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
640k
    const pixel *top = &topleft_in[1];
629
3.30M
    for (int y = 0; y < height; y += 2) {
630
2.66M
        const pixel *topleft = &topleft_in[-y];
631
2.66M
        const pixel *left = &topleft[-1];
632
2.66M
        ptrdiff_t left_stride = -1;
633
10.1M
        for (int x = 0; x < width; x += 4) {
634
7.45M
            const int p0 = *topleft;
635
7.45M
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
7.45M
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
7.45M
            pixel *ptr = &dst[x];
638
7.45M
            const int8_t *flt_ptr = filter;
639
640
22.3M
            for (int yy = 0; yy < 2; yy++) {
641
74.5M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
59.6M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
59.6M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
59.6M
                }
645
14.9M
                ptr += PXSTRIDE(stride);
646
14.9M
            }
647
7.45M
            left = &dst[x + 4 - 1];
648
7.45M
            left_stride = PXSTRIDE(stride);
649
7.45M
            top += 4;
650
7.45M
            topleft = &top[-1];
651
7.45M
        }
652
2.66M
        top = &dst[PXSTRIDE(stride)];
653
2.66M
        dst = &dst[PXSTRIDE(stride) * 2];
654
2.66M
    }
655
640k
}
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
403k
{
662
403k
    int y, x;
663
403k
    int16_t *const ac_orig = ac;
664
665
403k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
403k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
3.66M
    for (y = 0; y < height - 4 * h_pad; y++) {
669
36.1M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
32.8M
            int ac_sum = ypx[x << ss_hor];
671
32.8M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
32.8M
            if (ss_ver) {
673
14.1M
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
14.1M
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
14.1M
            }
676
32.8M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
32.8M
        }
678
3.55M
        for (; x < width; x++)
679
291k
            ac[x] = ac[x - 1];
680
3.26M
        ac += width;
681
3.26M
        ypx += PXSTRIDE(stride) << ss_ver;
682
3.26M
    }
683
435k
    for (; y < height; y++) {
684
32.3k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
32.3k
        ac += width;
686
32.3k
    }
687
688
403k
    const int log2sz = ctz(width) + ctz(height);
689
403k
    int sum = (1 << log2sz) >> 1;
690
3.69M
    for (ac = ac_orig, y = 0; y < height; y++) {
691
36.9M
        for (x = 0; x < width; x++)
692
33.6M
            sum += ac[x];
693
3.29M
        ac += width;
694
3.29M
    }
695
403k
    sum >>= log2sz;
696
697
    // subtract DC
698
3.69M
    for (ac = ac_orig, y = 0; y < height; y++) {
699
36.9M
        for (x = 0; x < width; x++)
700
33.6M
            ac[x] -= sum;
701
3.29M
        ac += width;
702
3.29M
    }
703
403k
}
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
403k
                             const int h_pad, const int cw, const int ch) \
709
403k
{ \
710
403k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
403k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
272k
                             const int h_pad, const int cw, const int ch) \
709
272k
{ \
710
272k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
272k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
2.80k
                             const int h_pad, const int cw, const int ch) \
709
2.80k
{ \
710
2.80k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
2.80k
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
128k
                             const int h_pad, const int cw, const int ch) \
709
128k
{ \
710
128k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
128k
}
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
35.0k
{
721
460k
    for (int y = 0; y < h; y++) {
722
3.87M
        for (int x = 0; x < w; x += 2) {
723
3.45M
            const int i = *idx++;
724
3.45M
            assert(!(i & 0x88));
725
3.45M
            dst[x + 0] = pal[i & 7];
726
3.45M
            dst[x + 1] = pal[i >> 4];
727
3.45M
        }
728
425k
        dst += PXSTRIDE(stride);
729
425k
    }
730
35.0k
}
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
77.8k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
77.8k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
77.8k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
77.8k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
77.8k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
77.8k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
77.8k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
77.8k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
77.8k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
77.8k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
77.8k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
77.8k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
77.8k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
77.8k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
77.8k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
77.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
77.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
77.8k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
77.8k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
77.8k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
77.8k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
77.8k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
77.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
77.8k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
35.3k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
35.3k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
35.3k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
35.3k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
35.3k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
35.3k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
35.3k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
35.3k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
35.3k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
35.3k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
35.3k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
35.3k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
35.3k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
35.3k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
35.3k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
35.3k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
35.3k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
35.3k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
35.3k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
35.3k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
35.3k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
35.3k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
35.3k
    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
35.3k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
42.5k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
42.5k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
42.5k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
42.5k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
42.5k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
42.5k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
42.5k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
42.5k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
42.5k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
42.5k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
42.5k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
42.5k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
42.5k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
42.5k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
42.5k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
42.5k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
42.5k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
42.5k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
42.5k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
42.5k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
42.5k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
42.5k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
42.5k
    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
42.5k
}