Coverage Report

Created: 2026-08-13 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/ipred_tmpl.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <stdlib.h>
31
#include <string.h>
32
33
#include "common/attributes.h"
34
#include "common/intops.h"
35
36
#include "src/ipred.h"
37
#include "src/tables.h"
38
39
static NOINLINE void
40
splat_dc(pixel *dst, const ptrdiff_t stride,
41
         const int width, const int height, const int dc HIGHBD_DECL_SUFFIX)
42
2.93M
{
43
2.93M
#if BITDEPTH == 8
44
2.93M
    assert(dc <= 0xff);
45
2.93M
    if (width > 4) {
46
431k
        const uint64_t dcN = dc * 0x0101010101010101ULL;
47
11.5M
        for (int y = 0; y < height; y++) {
48
62.5M
            for (int x = 0; x < width; x += sizeof(dcN))
49
51.3M
                *((uint64_t *) &dst[x]) = dcN;
50
11.1M
            dst += PXSTRIDE(stride);
51
11.1M
        }
52
2.50M
    } else {
53
2.50M
        const unsigned dcN = dc * 0x01010101U;
54
12.7M
        for (int y = 0; y < height; y++) {
55
20.4M
            for (int x = 0; x < width; x += sizeof(dcN))
56
10.2M
                *((unsigned *) &dst[x]) = dcN;
57
10.2M
            dst += PXSTRIDE(stride);
58
10.2M
        }
59
2.50M
    }
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.93M
}
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
260k
{
76
2.10M
    for (int y = 0; y < height; y++) {
77
17.6M
        for (int x = 0; x < width; x++) {
78
15.7M
            const int diff = alpha * ac[x];
79
15.7M
            dst[x] = iclip_pixel(dc + apply_sign((abs(diff) + 32) >> 6, diff));
80
15.7M
        }
81
1.84M
        ac += width;
82
1.84M
        dst += PXSTRIDE(stride);
83
1.84M
    }
84
260k
}
85
86
216k
static unsigned dc_gen_top(const pixel *const topleft, const int width) {
87
216k
    unsigned dc = width >> 1;
88
5.19M
    for (int i = 0; i < width; i++)
89
4.97M
       dc += topleft[1 + i];
90
216k
    return dc >> ctz(width);
91
216k
}
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
199k
{
99
199k
    splat_dc(dst, stride, width, height, dc_gen_top(topleft, width)
100
199k
             HIGHBD_TAIL_SUFFIX);
101
199k
}
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
17.8k
{
109
17.8k
    cfl_pred(dst, stride, width, height, dc_gen_top(topleft, width), ac, alpha
110
17.8k
             HIGHBD_TAIL_SUFFIX);
111
17.8k
}
112
113
117k
static unsigned dc_gen_left(const pixel *const topleft, const int height) {
114
117k
    unsigned dc = height >> 1;
115
3.26M
    for (int i = 0; i < height; i++)
116
3.14M
       dc += topleft[-(1 + i)];
117
117k
    return dc >> ctz(height);
118
117k
}
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
105k
{
126
105k
    splat_dc(dst, stride, width, height, dc_gen_left(topleft, height)
127
105k
             HIGHBD_TAIL_SUFFIX);
128
105k
}
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
12.3k
{
136
12.3k
    const unsigned dc = dc_gen_left(topleft, height);
137
12.3k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
138
12.3k
}
139
140
#if BITDEPTH == 8
141
847k
#define MULTIPLIER_1x2 0x5556
142
114k
#define MULTIPLIER_1x4 0x3334
143
481k
#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.79M
{
153
3.79M
    unsigned dc = (width + height) >> 1;
154
33.3M
    for (int i = 0; i < width; i++)
155
29.5M
       dc += topleft[i + 1];
156
32.7M
    for (int i = 0; i < height; i++)
157
28.9M
       dc += topleft[-(i + 1)];
158
3.79M
    dc >>= ctz(width + height);
159
160
3.79M
    if (width != height) {
161
481k
        dc *= (width > height * 2 || height > width * 2) ? MULTIPLIER_1x4 :
162
481k
                                                           MULTIPLIER_1x2;
163
481k
        dc >>= BASE_SHIFT;
164
481k
    }
165
3.79M
    return dc;
166
3.79M
}
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.56M
{
174
3.56M
    splat_dc(dst, stride, width, height, dc_gen(topleft, width, height)
175
3.56M
             HIGHBD_TAIL_SUFFIX);
176
3.56M
}
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
227k
{
184
227k
    const unsigned dc = dc_gen(topleft, width, height);
185
227k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
186
227k
}
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
36.7k
{
198
#if BITDEPTH == 16
199
    const int dc = (bitdepth_max + 1) >> 1;
200
#else
201
36.7k
    const int dc = 128;
202
36.7k
#endif
203
36.7k
    splat_dc(dst, stride, width, height, dc HIGHBD_TAIL_SUFFIX);
204
36.7k
}
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.54k
{
212
#if BITDEPTH == 16
213
    const int dc = (bitdepth_max + 1) >> 1;
214
#else
215
2.54k
    const int dc = 128;
216
2.54k
#endif
217
2.54k
    cfl_pred(dst, stride, width, height, dc, ac, alpha HIGHBD_TAIL_SUFFIX);
218
2.54k
}
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
164k
{
226
2.62M
    for (int y = 0; y < height; y++) {
227
2.46M
        pixel_copy(dst, topleft + 1, width);
228
2.46M
        dst += PXSTRIDE(stride);
229
2.46M
    }
230
164k
}
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
236k
{
238
3.04M
    for (int y = 0; y < height; y++) {
239
2.80M
        pixel_set(dst, topleft[-(1 + y)], width);
240
2.80M
        dst += PXSTRIDE(stride);
241
2.80M
    }
242
236k
}
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
292k
{
250
292k
    const int topleft = tl_ptr[0];
251
4.10M
    for (int y = 0; y < height; y++) {
252
3.81M
        const int left = tl_ptr[-(y + 1)];
253
72.1M
        for (int x = 0; x < width; x++) {
254
68.2M
            const int top = tl_ptr[1 + x];
255
68.2M
            const int base = left + top - topleft;
256
68.2M
            const int ldiff = abs(left - base);
257
68.2M
            const int tdiff = abs(top - base);
258
68.2M
            const int tldiff = abs(topleft - base);
259
260
68.2M
            dst[x] = ldiff <= tdiff && ldiff <= tldiff ? left :
261
68.2M
                     tdiff <= tldiff ? top : topleft;
262
68.2M
        }
263
3.81M
        dst += PXSTRIDE(stride);
264
3.81M
    }
265
292k
}
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
380k
{
273
380k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
274
380k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
275
380k
    const int right = topleft[width], bottom = topleft[-height];
276
277
4.63M
    for (int y = 0; y < height; y++) {
278
91.7M
        for (int x = 0; x < width; x++) {
279
87.5M
            const int pred = weights_ver[y]  * topleft[1 + x] +
280
87.5M
                      (256 - weights_ver[y]) * bottom +
281
87.5M
                             weights_hor[x]  * topleft[-(1 + y)] +
282
87.5M
                      (256 - weights_hor[x]) * right;
283
87.5M
            dst[x] = (pred + 256) >> 9;
284
87.5M
        }
285
4.25M
        dst += PXSTRIDE(stride);
286
4.25M
    }
287
380k
}
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
137k
{
295
137k
    const uint8_t *const weights_ver = &dav1d_sm_weights[height];
296
137k
    const int bottom = topleft[-height];
297
298
1.45M
    for (int y = 0; y < height; y++) {
299
26.9M
        for (int x = 0; x < width; x++) {
300
25.6M
            const int pred = weights_ver[y]  * topleft[1 + x] +
301
25.6M
                      (256 - weights_ver[y]) * bottom;
302
25.6M
            dst[x] = (pred + 128) >> 8;
303
25.6M
        }
304
1.31M
        dst += PXSTRIDE(stride);
305
1.31M
    }
306
137k
}
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
142k
{
314
142k
    const uint8_t *const weights_hor = &dav1d_sm_weights[width];
315
142k
    const int right = topleft[width];
316
317
1.77M
    for (int y = 0; y < height; y++) {
318
36.1M
        for (int x = 0; x < width; x++) {
319
34.5M
            const int pred = weights_hor[x]  * topleft[-(y + 1)] +
320
34.5M
                      (256 - weights_hor[x]) * right;
321
34.5M
            dst[x] = (pred + 128) >> 8;
322
34.5M
        }
323
1.63M
        dst += PXSTRIDE(stride);
324
1.63M
    }
325
142k
}
326
327
static NOINLINE int get_filter_strength(const int wh, const int angle,
328
                                        const int is_sm)
329
805k
{
330
805k
    if (is_sm) {
331
278k
        if (wh <= 8) {
332
26.4k
            if (angle >= 64) return 2;
333
12.5k
            if (angle >= 40) return 1;
334
251k
        } else if (wh <= 16) {
335
127k
            if (angle >= 48) return 2;
336
85.3k
            if (angle >= 20) return 1;
337
124k
        } else if (wh <= 24) {
338
62.0k
            if (angle >=  4) return 3;
339
62.5k
        } else {
340
62.5k
            return 3;
341
62.5k
        }
342
527k
    } else {
343
527k
        if (wh <= 8) {
344
74.9k
            if (angle >= 56) return 1;
345
452k
        } else if (wh <= 16) {
346
124k
            if (angle >= 40) return 1;
347
328k
        } else if (wh <= 24) {
348
144k
            if (angle >= 32) return 3;
349
75.4k
            if (angle >= 16) return 2;
350
35.8k
            if (angle >=  8) return 1;
351
183k
        } else if (wh <= 32) {
352
76.6k
            if (angle >= 32) return 3;
353
39.3k
            if (angle >=  4) return 2;
354
6.74k
            return 1;
355
107k
        } else {
356
107k
            return 3;
357
107k
        }
358
527k
    }
359
103k
    return 0;
360
805k
}
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
702k
{
367
702k
    static const uint8_t kernel[3][5] = {
368
702k
        { 0, 4, 8, 4, 0 },
369
702k
        { 0, 5, 6, 5, 0 },
370
702k
        { 2, 4, 4, 4, 2 }
371
702k
    };
372
373
702k
    assert(strength > 0);
374
702k
    int i = 0;
375
781k
    for (; i < imin(sz, lim_from); i++)
376
79.0k
        out[i] = in[iclip(i, from, to - 1)];
377
13.4M
    for (; i < imin(lim_to, sz); i++) {
378
12.7M
        int s = 0;
379
76.2M
        for (int j = 0; j < 5; j++)
380
63.5M
            s += in[iclip(i - 2 + j, from, to - 1)] * kernel[strength - 1][j];
381
12.7M
        out[i] = (s + 8) >> 4;
382
12.7M
    }
383
842k
    for (; i < sz; i++)
384
139k
        out[i] = in[iclip(i, from, to - 1)];
385
702k
}
386
387
1.13M
static inline int get_upsample(const int wh, const int angle, const int is_sm) {
388
1.13M
    return angle < 40 && wh <= 16 >> is_sm;
389
1.13M
}
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
333k
{
395
333k
    static const int8_t kernel[4] = { -1, 9, 9, -1 };
396
333k
    int i;
397
3.05M
    for (i = 0; i < hsz - 1; i++) {
398
2.72M
        out[i * 2] = in[iclip(i, from, to - 1)];
399
400
2.72M
        int s = 0;
401
13.6M
        for (int j = 0; j < 4; j++)
402
10.8M
            s += in[iclip(i + j - 1, from, to - 1)] * kernel[j];
403
2.72M
        out[i * 2 + 1] = iclip_pixel((s + 8) >> 4);
404
2.72M
    }
405
333k
    out[i * 2] = in[iclip(i, from, to - 1)];
406
333k
}
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
205k
{
414
205k
    const int is_sm = (angle >> 9) & 0x1;
415
205k
    const int enable_intra_edge_filter = angle >> 10;
416
205k
    angle &= 511;
417
205k
    assert(angle < 90);
418
205k
    int dx = dav1d_dr_intra_derivative[angle >> 1];
419
205k
    pixel top_out[64 + 64];
420
205k
    const pixel *top;
421
205k
    int max_base_x;
422
205k
    const int upsample_above = enable_intra_edge_filter ?
423
183k
        get_upsample(width + height, 90 - angle, is_sm) : 0;
424
205k
    if (upsample_above) {
425
65.8k
        upsample_edge(top_out, width + height, &topleft_in[1], -1,
426
65.8k
                      width + imin(width, height) HIGHBD_TAIL_SUFFIX);
427
65.8k
        top = top_out;
428
65.8k
        max_base_x = 2 * (width + height) - 2;
429
65.8k
        dx <<= 1;
430
139k
    } else {
431
139k
        const int filter_strength = enable_intra_edge_filter ?
432
118k
            get_filter_strength(width + height, 90 - angle, is_sm) : 0;
433
139k
        if (filter_strength) {
434
99.4k
            filter_edge(top_out, width + height, 0, width + height,
435
99.4k
                        &topleft_in[1], -1, width + imin(width, height),
436
99.4k
                        filter_strength);
437
99.4k
            top = top_out;
438
99.4k
            max_base_x = width + height - 1;
439
99.4k
        } else {
440
39.9k
            top = &topleft_in[1];
441
39.9k
            max_base_x = width + imin(width, height) - 1;
442
39.9k
        }
443
139k
    }
444
205k
    const int base_inc = 1 + upsample_above;
445
2.63M
    for (int y = 0, xpos = dx; y < height;
446
2.43M
         y++, dst += PXSTRIDE(stride), xpos += dx)
447
2.43M
    {
448
2.43M
        const int frac = xpos & 0x3E;
449
450
55.3M
        for (int x = 0, base = xpos >> 6; x < width; x++, base += base_inc) {
451
53.0M
            if (base < max_base_x) {
452
52.9M
                const int v = top[base] * (64 - frac) + top[base + 1] * frac;
453
52.9M
                dst[x] = (v + 32) >> 6;
454
52.9M
            } else {
455
117k
                pixel_set(&dst[x], top[max_base_x], width - x);
456
117k
                break;
457
117k
            }
458
53.0M
        }
459
2.43M
    }
460
205k
}
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
410k
{
468
410k
    const int is_sm = (angle >> 9) & 0x1;
469
410k
    const int enable_intra_edge_filter = angle >> 10;
470
410k
    angle &= 511;
471
410k
    assert(angle > 90 && angle < 180);
472
410k
    int dy = dav1d_dr_intra_derivative[(angle - 90) >> 1];
473
410k
    int dx = dav1d_dr_intra_derivative[(180 - angle) >> 1];
474
410k
    const int upsample_left = enable_intra_edge_filter ?
475
370k
        get_upsample(width + height, 180 - angle, is_sm) : 0;
476
410k
    const int upsample_above = enable_intra_edge_filter ?
477
370k
        get_upsample(width + height, angle - 90, is_sm) : 0;
478
410k
    pixel edge[64 + 64 + 1];
479
410k
    pixel *const topleft = &edge[64];
480
481
410k
    if (upsample_above) {
482
70.8k
        upsample_edge(topleft, width + 1, topleft_in, 0, width + 1
483
70.8k
                      HIGHBD_TAIL_SUFFIX);
484
70.8k
        dx <<= 1;
485
339k
    } else {
486
339k
        const int filter_strength = enable_intra_edge_filter ?
487
299k
            get_filter_strength(width + height, angle - 90, is_sm) : 0;
488
489
339k
        if (filter_strength) {
490
271k
            filter_edge(&topleft[1], width, 0, max_width,
491
271k
                        &topleft_in[1], -1, width,
492
271k
                        filter_strength);
493
271k
        } else {
494
68.6k
            pixel_copy(&topleft[1], &topleft_in[1], width);
495
68.6k
        }
496
339k
    }
497
410k
    if (upsample_left) {
498
89.8k
        upsample_edge(&topleft[-height * 2], height + 1, &topleft_in[-height],
499
89.8k
                      0, height + 1 HIGHBD_TAIL_SUFFIX);
500
89.8k
        dy <<= 1;
501
320k
    } else {
502
320k
        const int filter_strength = enable_intra_edge_filter ?
503
280k
            get_filter_strength(width + height, 180 - angle, is_sm) : 0;
504
505
320k
        if (filter_strength) {
506
247k
            filter_edge(&topleft[-height], height, height - max_height, height,
507
247k
                        &topleft_in[-height],
508
247k
                        0, height + 1, filter_strength);
509
247k
        } else {
510
73.2k
            pixel_copy(&topleft[-height], &topleft_in[-height], height);
511
73.2k
        }
512
320k
    }
513
410k
    *topleft = *topleft_in;
514
515
410k
    const int base_inc_x = 1 + upsample_above;
516
410k
    const pixel *const left = &topleft[-(1 + upsample_left)];
517
5.02M
    for (int y = 0, xpos = ((1 + upsample_above) << 6) - dx; y < height;
518
4.61M
         y++, xpos -= dx, dst += PXSTRIDE(stride))
519
4.61M
    {
520
4.61M
        int base_x = xpos >> 6;
521
4.61M
        const int frac_x = xpos & 0x3E;
522
523
97.0M
        for (int x = 0, ypos = (y << (6 + upsample_left)) - dy; x < width;
524
92.4M
             x++, base_x += base_inc_x, ypos -= dy)
525
92.4M
        {
526
92.4M
            int v;
527
92.4M
            if (base_x >= 0) {
528
42.1M
                v = topleft[base_x] * (64 - frac_x) +
529
42.1M
                    topleft[base_x + 1] * frac_x;
530
50.3M
            } else {
531
50.3M
                const int base_y = ypos >> 6;
532
50.3M
                assert(base_y >= -(1 + upsample_left));
533
50.3M
                const int frac_y = ypos & 0x3E;
534
50.3M
                v = left[-base_y] * (64 - frac_y) +
535
50.3M
                    left[-(base_y + 1)] * frac_y;
536
50.3M
            }
537
92.4M
            dst[x] = (v + 32) >> 6;
538
92.4M
        }
539
4.61M
    }
540
410k
}
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
244k
{
548
244k
    const int is_sm = (angle >> 9) & 0x1;
549
244k
    const int enable_intra_edge_filter = angle >> 10;
550
244k
    angle &= 511;
551
244k
    assert(angle > 180);
552
244k
    int dy = dav1d_dr_intra_derivative[(270 - angle) >> 1];
553
244k
    pixel left_out[64 + 64];
554
244k
    const pixel *left;
555
244k
    int max_base_y;
556
244k
    const int upsample_left = enable_intra_edge_filter ?
557
214k
        get_upsample(width + height, angle - 180, is_sm) : 0;
558
244k
    if (upsample_left) {
559
106k
        upsample_edge(left_out, width + height,
560
106k
                      &topleft_in[-(width + height)],
561
106k
                      imax(width - height, 0), width + height + 1
562
106k
                      HIGHBD_TAIL_SUFFIX);
563
106k
        left = &left_out[2 * (width + height) - 2];
564
106k
        max_base_y = 2 * (width + height) - 2;
565
106k
        dy <<= 1;
566
137k
    } else {
567
137k
        const int filter_strength = enable_intra_edge_filter ?
568
107k
            get_filter_strength(width + height, angle - 180, is_sm) : 0;
569
570
137k
        if (filter_strength) {
571
84.2k
            filter_edge(left_out, width + height, 0, width + height,
572
84.2k
                        &topleft_in[-(width + height)],
573
84.2k
                        imax(width - height, 0), width + height + 1,
574
84.2k
                        filter_strength);
575
84.2k
            left = &left_out[width + height - 1];
576
84.2k
            max_base_y = width + height - 1;
577
84.2k
        } else {
578
53.3k
            left = &topleft_in[-1];
579
53.3k
            max_base_y = height + imin(width, height) - 1;
580
53.3k
        }
581
137k
    }
582
244k
    const int base_inc = 1 + upsample_left;
583
3.02M
    for (int x = 0, ypos = dy; x < width; x++, ypos += dy) {
584
2.78M
        const int frac = ypos & 0x3E;
585
586
53.7M
        for (int y = 0, base = ypos >> 6; y < height; y++, base += base_inc) {
587
50.9M
            if (base < max_base_y) {
588
50.9M
                const int v = left[-base] * (64 - frac) +
589
50.9M
                              left[-(base + 1)] * frac;
590
50.9M
                dst[y * PXSTRIDE(stride) + x] = (v + 32) >> 6;
591
50.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.32k
                break;
596
4.32k
            }
597
50.9M
        }
598
2.78M
    }
599
244k
}
600
601
#if ARCH_X86
602
#define FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6) \
603
19.1M
    flt_ptr[ 0] * p0 + flt_ptr[ 1] * p1 +           \
604
19.1M
    flt_ptr[16] * p2 + flt_ptr[17] * p3 +           \
605
19.1M
    flt_ptr[32] * p4 + flt_ptr[33] * p5 +           \
606
19.1M
    flt_ptr[48] * p6
607
19.1M
#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
177k
{
624
177k
    filt_idx &= 511;
625
177k
    assert(filt_idx < 5);
626
627
177k
    const int8_t *const filter = dav1d_filter_intra_taps[filt_idx];
628
177k
    const pixel *top = &topleft_in[1];
629
980k
    for (int y = 0; y < height; y += 2) {
630
802k
        const pixel *topleft = &topleft_in[-y];
631
802k
        const pixel *left = &topleft[-1];
632
802k
        ptrdiff_t left_stride = -1;
633
3.19M
        for (int x = 0; x < width; x += 4) {
634
2.39M
            const int p0 = *topleft;
635
2.39M
            const int p1 = top[0], p2 = top[1], p3 = top[2], p4 = top[3];
636
2.39M
            const int p5 = left[0 * left_stride], p6 = left[1 * left_stride];
637
2.39M
            pixel *ptr = &dst[x];
638
2.39M
            const int8_t *flt_ptr = filter;
639
640
7.17M
            for (int yy = 0; yy < 2; yy++) {
641
23.9M
                for (int xx = 0; xx < 4; xx++, flt_ptr += FLT_INCR) {
642
19.1M
                    const int acc = FILTER(flt_ptr, p0, p1, p2, p3, p4, p5, p6);
643
19.1M
                    ptr[xx] = iclip_pixel((acc + 8) >> 4);
644
19.1M
                }
645
4.78M
                ptr += PXSTRIDE(stride);
646
4.78M
            }
647
2.39M
            left = &dst[x + 4 - 1];
648
2.39M
            left_stride = PXSTRIDE(stride);
649
2.39M
            top += 4;
650
2.39M
            topleft = &top[-1];
651
2.39M
        }
652
802k
        top = &dst[PXSTRIDE(stride)];
653
802k
        dst = &dst[PXSTRIDE(stride) * 2];
654
802k
    }
655
177k
}
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
170k
{
662
170k
    int y, x;
663
170k
    int16_t *const ac_orig = ac;
664
665
170k
    assert(w_pad >= 0 && w_pad * 4 < width);
666
170k
    assert(h_pad >= 0 && h_pad * 4 < height);
667
668
1.36M
    for (y = 0; y < height - 4 * h_pad; y++) {
669
11.2M
        for (x = 0; x < width - 4 * w_pad; x++) {
670
10.1M
            int ac_sum = ypx[x << ss_hor];
671
10.1M
            if (ss_hor) ac_sum += ypx[x * 2 + 1];
672
10.1M
            if (ss_ver) {
673
7.53M
                ac_sum += ypx[(x << ss_hor) + PXSTRIDE(stride)];
674
7.53M
                if (ss_hor) ac_sum += ypx[x * 2 + 1 + PXSTRIDE(stride)];
675
7.53M
            }
676
10.1M
            ac[x] = ac_sum << (1 + !ss_ver + !ss_hor);
677
10.1M
        }
678
1.26M
        for (; x < width; x++)
679
74.3k
            ac[x] = ac[x - 1];
680
1.19M
        ac += width;
681
1.19M
        ypx += PXSTRIDE(stride) << ss_ver;
682
1.19M
    }
683
177k
    for (; y < height; y++) {
684
7.25k
        memcpy(ac, &ac[-width], width * sizeof(*ac));
685
7.25k
        ac += width;
686
7.25k
    }
687
688
170k
    const int log2sz = ctz(width) + ctz(height);
689
170k
    int sum = (1 << log2sz) >> 1;
690
1.36M
    for (ac = ac_orig, y = 0; y < height; y++) {
691
11.4M
        for (x = 0; x < width; x++)
692
10.2M
            sum += ac[x];
693
1.19M
        ac += width;
694
1.19M
    }
695
170k
    sum >>= log2sz;
696
697
    // subtract DC
698
1.36M
    for (ac = ac_orig, y = 0; y < height; y++) {
699
11.4M
        for (x = 0; x < width; x++)
700
10.2M
            ac[x] -= sum;
701
1.19M
        ac += width;
702
1.19M
    }
703
170k
}
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
170k
                             const int h_pad, const int cw, const int ch) \
709
170k
{ \
710
170k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
170k
}
ipred_tmpl.c:cfl_ac_420_c
Line
Count
Source
708
153k
                             const int h_pad, const int cw, const int ch) \
709
153k
{ \
710
153k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
153k
}
ipred_tmpl.c:cfl_ac_422_c
Line
Count
Source
708
439
                             const int h_pad, const int cw, const int ch) \
709
439
{ \
710
439
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
439
}
ipred_tmpl.c:cfl_ac_444_c
Line
Count
Source
708
15.8k
                             const int h_pad, const int cw, const int ch) \
709
15.8k
{ \
710
15.8k
    cfl_ac_c(ac, ypx, stride, w_pad, h_pad, cw, ch, ss_hor, ss_ver); \
711
15.8k
}
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
7.88k
{
721
98.2k
    for (int y = 0; y < h; y++) {
722
737k
        for (int x = 0; x < w; x += 2) {
723
647k
            const int i = *idx++;
724
647k
            assert(!(i & 0x88));
725
647k
            dst[x + 0] = pal[i & 7];
726
647k
            dst[x + 1] = pal[i >> 4];
727
647k
        }
728
90.3k
        dst += PXSTRIDE(stride);
729
90.3k
    }
730
7.88k
}
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
24.4k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
24.4k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
24.4k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
24.4k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
24.4k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
24.4k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
24.4k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
24.4k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
24.4k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
24.4k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
24.4k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
24.4k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
24.4k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
24.4k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
24.4k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
24.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
24.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
24.4k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
24.4k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
24.4k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
24.4k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
24.4k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
24.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
24.4k
}
dav1d_intra_pred_dsp_init_8bpc
Line
Count
Source
744
10.6k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
10.6k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
10.6k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
10.6k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
10.6k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
10.6k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
10.6k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
10.6k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
10.6k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
10.6k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
10.6k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
10.6k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
10.6k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
10.6k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
10.6k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
10.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
10.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
10.6k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
10.6k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
10.6k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
10.6k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
10.6k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
10.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
10.6k
}
dav1d_intra_pred_dsp_init_16bpc
Line
Count
Source
744
13.7k
COLD void bitfn(dav1d_intra_pred_dsp_init)(Dav1dIntraPredDSPContext *const c) {
745
13.7k
    c->intra_pred[DC_PRED      ] = ipred_dc_c;
746
13.7k
    c->intra_pred[DC_128_PRED  ] = ipred_dc_128_c;
747
13.7k
    c->intra_pred[TOP_DC_PRED  ] = ipred_dc_top_c;
748
13.7k
    c->intra_pred[LEFT_DC_PRED ] = ipred_dc_left_c;
749
13.7k
    c->intra_pred[HOR_PRED     ] = ipred_h_c;
750
13.7k
    c->intra_pred[VERT_PRED    ] = ipred_v_c;
751
13.7k
    c->intra_pred[PAETH_PRED   ] = ipred_paeth_c;
752
13.7k
    c->intra_pred[SMOOTH_PRED  ] = ipred_smooth_c;
753
13.7k
    c->intra_pred[SMOOTH_V_PRED] = ipred_smooth_v_c;
754
13.7k
    c->intra_pred[SMOOTH_H_PRED] = ipred_smooth_h_c;
755
13.7k
    c->intra_pred[Z1_PRED      ] = ipred_z1_c;
756
13.7k
    c->intra_pred[Z2_PRED      ] = ipred_z2_c;
757
13.7k
    c->intra_pred[Z3_PRED      ] = ipred_z3_c;
758
13.7k
    c->intra_pred[FILTER_PRED  ] = ipred_filter_c;
759
760
13.7k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I420 - 1] = cfl_ac_420_c;
761
13.7k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I422 - 1] = cfl_ac_422_c;
762
13.7k
    c->cfl_ac[DAV1D_PIXEL_LAYOUT_I444 - 1] = cfl_ac_444_c;
763
764
13.7k
    c->cfl_pred[DC_PRED     ] = ipred_cfl_c;
765
13.7k
    c->cfl_pred[DC_128_PRED ] = ipred_cfl_128_c;
766
13.7k
    c->cfl_pred[TOP_DC_PRED ] = ipred_cfl_top_c;
767
13.7k
    c->cfl_pred[LEFT_DC_PRED] = ipred_cfl_left_c;
768
769
13.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
13.7k
}