Coverage Report

Created: 2025-09-05 06:27

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