Coverage Report

Created: 2026-09-01 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/ipred_prepare_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 <stdint.h>
31
#include <string.h>
32
33
#include "common/intops.h"
34
35
#include "src/ipred_prepare.h"
36
37
static const uint8_t av1_mode_conv[N_INTRA_PRED_MODES]
38
                                  [2 /* have_left */][2 /* have_top */] =
39
{
40
    [DC_PRED]    = { { DC_128_PRED,  TOP_DC_PRED },
41
                     { LEFT_DC_PRED, DC_PRED     } },
42
    [PAETH_PRED] = { { DC_128_PRED,  VERT_PRED   },
43
                     { HOR_PRED,     PAETH_PRED  } },
44
};
45
46
static const uint8_t av1_mode_to_angle_map[8] = {
47
    90, 180, 45, 135, 113, 157, 203, 67
48
};
49
50
static const struct {
51
    uint8_t needs_left:1;
52
    uint8_t needs_top:1;
53
    uint8_t needs_topleft:1;
54
    uint8_t needs_topright:1;
55
    uint8_t needs_bottomleft:1;
56
} av1_intra_prediction_edges[N_IMPL_INTRA_PRED_MODES] = {
57
    [DC_PRED]       = { .needs_top  = 1, .needs_left = 1 },
58
    [VERT_PRED]     = { .needs_top  = 1 },
59
    [HOR_PRED]      = { .needs_left = 1 },
60
    [LEFT_DC_PRED]  = { .needs_left = 1 },
61
    [TOP_DC_PRED]   = { .needs_top  = 1 },
62
    [DC_128_PRED]   = { 0 },
63
    [Z1_PRED]       = { .needs_top = 1, .needs_topright = 1,
64
                        .needs_topleft = 1 },
65
    [Z2_PRED]       = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
66
    [Z3_PRED]       = { .needs_left = 1, .needs_bottomleft = 1,
67
                        .needs_topleft = 1 },
68
    [SMOOTH_PRED]   = { .needs_left = 1, .needs_top = 1 },
69
    [SMOOTH_V_PRED] = { .needs_left = 1, .needs_top = 1 },
70
    [SMOOTH_H_PRED] = { .needs_left = 1, .needs_top = 1 },
71
    [PAETH_PRED]    = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
72
    [FILTER_PRED]   = { .needs_left = 1, .needs_top = 1, .needs_topleft = 1 },
73
};
74
75
enum IntraPredMode
76
bytefn(dav1d_prepare_intra_edges)(const int x, const int have_left,
77
                                  const int y, const int have_top,
78
                                  const int w, const int h,
79
                                  const enum EdgeFlags edge_flags,
80
                                  const pixel *const dst,
81
                                  const ptrdiff_t stride,
82
                                  const pixel *prefilter_toplevel_sb_edge,
83
                                  enum IntraPredMode mode, int *const angle,
84
                                  const int tw, const int th, const int filter_edge,
85
                                  pixel *const topleft_out HIGHBD_DECL_SUFFIX)
86
12.2M
{
87
12.2M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
12.2M
    assert(y < h && x < w);
89
90
12.2M
    switch (mode) {
91
653k
    case VERT_PRED:
92
1.77M
    case HOR_PRED:
93
1.98M
    case DIAG_DOWN_LEFT_PRED:
94
2.20M
    case DIAG_DOWN_RIGHT_PRED:
95
2.39M
    case VERT_RIGHT_PRED:
96
2.65M
    case HOR_DOWN_PRED:
97
3.15M
    case HOR_UP_PRED:
98
3.43M
    case VERT_LEFT_PRED: {
99
3.43M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
3.43M
        if (*angle <= 90)
102
978k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
2.45M
        else if (*angle < 180)
104
1.11M
            mode = Z2_PRED;
105
1.34M
        else
106
1.34M
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
3.43M
        break;
108
3.15M
    }
109
5.48M
    case DC_PRED:
110
6.38M
    case PAETH_PRED:
111
6.38M
        mode = av1_mode_conv[mode][have_left][have_top];
112
6.38M
        break;
113
2.43M
    default:
114
2.43M
        break;
115
12.2M
    }
116
117
12.2M
    const pixel *dst_top;
118
12.2M
    if (have_top &&
119
10.9M
        (av1_intra_prediction_edges[mode].needs_top ||
120
1.22M
         av1_intra_prediction_edges[mode].needs_topleft ||
121
589k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
10.4M
    {
123
10.4M
        if (prefilter_toplevel_sb_edge) {
124
1.53M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
8.96M
        } else {
126
8.96M
            dst_top = &dst[-PXSTRIDE(stride)];
127
8.96M
        }
128
10.4M
    }
129
130
12.2M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
10.1M
        const int sz = th << 2;
132
10.1M
        pixel *const left = &topleft_out[-sz];
133
134
10.1M
        if (have_left) {
135
9.54M
            const int px_have = imin(sz, (h - y) << 2);
136
137
110M
            for (int i = 0; i < px_have; i++)
138
100M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
9.54M
            if (px_have < sz)
140
276k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
9.54M
        } else {
142
560k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
560k
        }
144
145
10.1M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
691k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
691k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
691k
            if (have_bottomleft) {
150
187k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.89M
                for (int i = 0; i < px_have; i++)
153
1.70M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
187k
                if (px_have < sz)
155
2.50k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
504k
            } else {
157
504k
                pixel_set(left - sz, left[0], sz);
158
504k
            }
159
691k
        }
160
10.1M
    }
161
162
12.2M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
10.1M
        const int sz = tw << 2;
164
10.1M
        pixel *const top = &topleft_out[1];
165
166
10.1M
        if (have_top) {
167
9.73M
            const int px_have = imin(sz, (w - x) << 2);
168
9.73M
            pixel_copy(top, dst_top, px_have);
169
9.73M
            if (px_have < sz)
170
645k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
9.73M
        } else {
172
401k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
401k
        }
174
175
10.1M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
571k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
571k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
571k
            if (have_topright) {
180
341k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
341k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
341k
                if (px_have < sz)
184
13.6k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
6.10k
                              sz - px_have);
186
341k
            } else {
187
230k
                pixel_set(top + sz, top[sz - 1], sz);
188
230k
            }
189
571k
        }
190
10.1M
    }
191
192
12.2M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
3.67M
        if (have_left)
194
3.41M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
256k
        else
196
256k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
3.67M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
282k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
282k
                            topleft_out[0] * 6 + 8) >> 4;
201
3.67M
    }
202
203
12.2M
    return mode;
204
12.2M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
5.27M
{
87
5.27M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.27M
    assert(y < h && x < w);
89
90
5.27M
    switch (mode) {
91
291k
    case VERT_PRED:
92
774k
    case HOR_PRED:
93
865k
    case DIAG_DOWN_LEFT_PRED:
94
962k
    case DIAG_DOWN_RIGHT_PRED:
95
1.04M
    case VERT_RIGHT_PRED:
96
1.16M
    case HOR_DOWN_PRED:
97
1.36M
    case HOR_UP_PRED:
98
1.47M
    case VERT_LEFT_PRED: {
99
1.47M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.47M
        if (*angle <= 90)
102
428k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.04M
        else if (*angle < 180)
104
490k
            mode = Z2_PRED;
105
558k
        else
106
558k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.47M
        break;
108
1.36M
    }
109
2.28M
    case DC_PRED:
110
2.73M
    case PAETH_PRED:
111
2.73M
        mode = av1_mode_conv[mode][have_left][have_top];
112
2.73M
        break;
113
1.06M
    default:
114
1.06M
        break;
115
5.27M
    }
116
117
5.27M
    const pixel *dst_top;
118
5.27M
    if (have_top &&
119
4.70M
        (av1_intra_prediction_edges[mode].needs_top ||
120
502k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
244k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.51M
    {
123
4.51M
        if (prefilter_toplevel_sb_edge) {
124
773k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.74M
        } else {
126
3.74M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.74M
        }
128
4.51M
    }
129
130
5.27M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
4.26M
        const int sz = th << 2;
132
4.26M
        pixel *const left = &topleft_out[-sz];
133
134
4.26M
        if (have_left) {
135
3.99M
            const int px_have = imin(sz, (h - y) << 2);
136
137
49.8M
            for (int i = 0; i < px_have; i++)
138
45.8M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
3.99M
            if (px_have < sz)
140
137k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
3.99M
        } else {
142
268k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
268k
        }
144
145
4.26M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
285k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
285k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
285k
            if (have_bottomleft) {
150
80.9k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
846k
                for (int i = 0; i < px_have; i++)
153
765k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
80.9k
                if (px_have < sz)
155
1.08k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
204k
            } else {
157
204k
                pixel_set(left - sz, left[0], sz);
158
204k
            }
159
285k
        }
160
4.26M
    }
161
162
5.27M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.38M
        const int sz = tw << 2;
164
4.38M
        pixel *const top = &topleft_out[1];
165
166
4.38M
        if (have_top) {
167
4.19M
            const int px_have = imin(sz, (w - x) << 2);
168
4.19M
            pixel_copy(top, dst_top, px_have);
169
4.19M
            if (px_have < sz)
170
332k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
4.19M
        } else {
172
186k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
186k
        }
174
175
4.38M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
243k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
243k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
243k
            if (have_topright) {
180
140k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
140k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
140k
                if (px_have < sz)
184
6.10k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
6.10k
                              sz - px_have);
186
140k
            } else {
187
102k
                pixel_set(top + sz, top[sz - 1], sz);
188
102k
            }
189
243k
        }
190
4.38M
    }
191
192
5.27M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.63M
        if (have_left)
194
1.51M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
124k
        else
196
124k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.63M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
123k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
123k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.63M
    }
202
203
5.27M
    return mode;
204
5.27M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
6.95M
{
87
6.95M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
6.95M
    assert(y < h && x < w);
89
90
6.95M
    switch (mode) {
91
362k
    case VERT_PRED:
92
999k
    case HOR_PRED:
93
1.12M
    case DIAG_DOWN_LEFT_PRED:
94
1.24M
    case DIAG_DOWN_RIGHT_PRED:
95
1.34M
    case VERT_RIGHT_PRED:
96
1.49M
    case HOR_DOWN_PRED:
97
1.79M
    case HOR_UP_PRED:
98
1.95M
    case VERT_LEFT_PRED: {
99
1.95M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.95M
        if (*angle <= 90)
102
550k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.40M
        else if (*angle < 180)
104
627k
            mode = Z2_PRED;
105
782k
        else
106
782k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.95M
        break;
108
1.79M
    }
109
3.19M
    case DC_PRED:
110
3.64M
    case PAETH_PRED:
111
3.64M
        mode = av1_mode_conv[mode][have_left][have_top];
112
3.64M
        break;
113
1.36M
    default:
114
1.36M
        break;
115
6.95M
    }
116
117
6.95M
    const pixel *dst_top;
118
6.95M
    if (have_top &&
119
6.26M
        (av1_intra_prediction_edges[mode].needs_top ||
120
718k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
344k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
5.98M
    {
123
5.98M
        if (prefilter_toplevel_sb_edge) {
124
759k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
5.22M
        } else {
126
5.22M
            dst_top = &dst[-PXSTRIDE(stride)];
127
5.22M
        }
128
5.98M
    }
129
130
6.95M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
5.84M
        const int sz = th << 2;
132
5.84M
        pixel *const left = &topleft_out[-sz];
133
134
5.84M
        if (have_left) {
135
5.55M
            const int px_have = imin(sz, (h - y) << 2);
136
137
60.2M
            for (int i = 0; i < px_have; i++)
138
54.6M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
5.55M
            if (px_have < sz)
140
138k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
5.55M
        } else {
142
291k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
291k
        }
144
145
5.84M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
405k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
405k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
405k
            if (have_bottomleft) {
150
106k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.04M
                for (int i = 0; i < px_have; i++)
153
938k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
106k
                if (px_have < sz)
155
1.42k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
299k
            } else {
157
299k
                pixel_set(left - sz, left[0], sz);
158
299k
            }
159
405k
        }
160
5.84M
    }
161
162
6.95M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
5.74M
        const int sz = tw << 2;
164
5.74M
        pixel *const top = &topleft_out[1];
165
166
5.74M
        if (have_top) {
167
5.53M
            const int px_have = imin(sz, (w - x) << 2);
168
5.53M
            pixel_copy(top, dst_top, px_have);
169
5.53M
            if (px_have < sz)
170
313k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
5.53M
        } else {
172
215k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
215k
        }
174
175
5.74M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
328k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
328k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
328k
            if (have_topright) {
180
200k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
200k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
200k
                if (px_have < sz)
184
7.51k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
7.51k
                              sz - px_have);
186
200k
            } else {
187
128k
                pixel_set(top + sz, top[sz - 1], sz);
188
128k
            }
189
328k
        }
190
5.74M
    }
191
192
6.95M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.03M
        if (have_left)
194
1.90M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
132k
        else
196
132k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.03M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
159k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
159k
                            topleft_out[0] * 6 + 8) >> 4;
201
2.03M
    }
202
203
6.95M
    return mode;
204
6.95M
}