Coverage Report

Created: 2026-08-31 06:22

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
11.8M
{
87
11.8M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
11.8M
    assert(y < h && x < w);
89
90
11.8M
    switch (mode) {
91
676k
    case VERT_PRED:
92
1.78M
    case HOR_PRED:
93
2.00M
    case DIAG_DOWN_LEFT_PRED:
94
2.23M
    case DIAG_DOWN_RIGHT_PRED:
95
2.42M
    case VERT_RIGHT_PRED:
96
2.69M
    case HOR_DOWN_PRED:
97
3.18M
    case HOR_UP_PRED:
98
3.47M
    case VERT_LEFT_PRED: {
99
3.47M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
3.47M
        if (*angle <= 90)
102
996k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
2.47M
        else if (*angle < 180)
104
1.13M
            mode = Z2_PRED;
105
1.33M
        else
106
1.33M
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
3.47M
        break;
108
3.18M
    }
109
5.08M
    case DC_PRED:
110
5.99M
    case PAETH_PRED:
111
5.99M
        mode = av1_mode_conv[mode][have_left][have_top];
112
5.99M
        break;
113
2.43M
    default:
114
2.43M
        break;
115
11.8M
    }
116
117
11.8M
    const pixel *dst_top;
118
11.8M
    if (have_top &&
119
10.6M
        (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.1M
    {
123
10.1M
        if (prefilter_toplevel_sb_edge) {
124
1.49M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
8.69M
        } else {
126
8.69M
            dst_top = &dst[-PXSTRIDE(stride)];
127
8.69M
        }
128
10.1M
    }
129
130
11.8M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
9.73M
        const int sz = th << 2;
132
9.73M
        pixel *const left = &topleft_out[-sz];
133
134
9.73M
        if (have_left) {
135
9.20M
            const int px_have = imin(sz, (h - y) << 2);
136
137
106M
            for (int i = 0; i < px_have; i++)
138
97.4M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
9.20M
            if (px_have < sz)
140
257k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
9.20M
        } else {
142
530k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
530k
        }
144
145
9.73M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
685k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
685k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
685k
            if (have_bottomleft) {
150
188k
                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
188k
                if (px_have < sz)
155
2.33k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
496k
            } else {
157
496k
                pixel_set(left - sz, left[0], sz);
158
496k
            }
159
685k
        }
160
9.73M
    }
161
162
11.8M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
9.75M
        const int sz = tw << 2;
164
9.75M
        pixel *const top = &topleft_out[1];
165
166
9.75M
        if (have_top) {
167
9.38M
            const int px_have = imin(sz, (w - x) << 2);
168
9.38M
            pixel_copy(top, dst_top, px_have);
169
9.38M
            if (px_have < sz)
170
620k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
9.38M
        } else {
172
373k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
373k
        }
174
175
9.75M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
581k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
581k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
581k
            if (have_topright) {
180
349k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
349k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
349k
                if (px_have < sz)
184
12.8k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
5.41k
                              sz - px_have);
186
349k
            } else {
187
231k
                pixel_set(top + sz, top[sz - 1], sz);
188
231k
            }
189
581k
        }
190
9.75M
    }
191
192
11.8M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
3.71M
        if (have_left)
194
3.46M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
244k
        else
196
244k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
3.71M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
280k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
280k
                            topleft_out[0] * 6 + 8) >> 4;
201
3.71M
    }
202
203
11.8M
    return mode;
204
11.8M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
5.45M
{
87
5.45M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.45M
    assert(y < h && x < w);
89
90
5.45M
    switch (mode) {
91
301k
    case VERT_PRED:
92
793k
    case HOR_PRED:
93
890k
    case DIAG_DOWN_LEFT_PRED:
94
1.00M
    case DIAG_DOWN_RIGHT_PRED:
95
1.08M
    case VERT_RIGHT_PRED:
96
1.20M
    case HOR_DOWN_PRED:
97
1.41M
    case HOR_UP_PRED:
98
1.53M
    case VERT_LEFT_PRED: {
99
1.53M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.53M
        if (*angle <= 90)
102
444k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.08M
        else if (*angle < 180)
104
514k
            mode = Z2_PRED;
105
574k
        else
106
574k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.53M
        break;
108
1.41M
    }
109
2.34M
    case DC_PRED:
110
2.81M
    case PAETH_PRED:
111
2.81M
        mode = av1_mode_conv[mode][have_left][have_top];
112
2.81M
        break;
113
1.11M
    default:
114
1.11M
        break;
115
5.45M
    }
116
117
5.45M
    const pixel *dst_top;
118
5.45M
    if (have_top &&
119
4.90M
        (av1_intra_prediction_edges[mode].needs_top ||
120
521k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
253k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.70M
    {
123
4.70M
        if (prefilter_toplevel_sb_edge) {
124
783k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.92M
        } else {
126
3.92M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.92M
        }
128
4.70M
    }
129
130
5.45M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
4.43M
        const int sz = th << 2;
132
4.43M
        pixel *const left = &topleft_out[-sz];
133
134
4.43M
        if (have_left) {
135
4.17M
            const int px_have = imin(sz, (h - y) << 2);
136
137
50.6M
            for (int i = 0; i < px_have; i++)
138
46.4M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
4.17M
            if (px_have < sz)
140
122k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
4.17M
        } else {
142
258k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
258k
        }
144
145
4.43M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
294k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
294k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
294k
            if (have_bottomleft) {
150
85.3k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
883k
                for (int i = 0; i < px_have; i++)
153
797k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
85.3k
                if (px_have < sz)
155
1.01k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
208k
            } else {
157
208k
                pixel_set(left - sz, left[0], sz);
158
208k
            }
159
294k
        }
160
4.43M
    }
161
162
5.45M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.55M
        const int sz = tw << 2;
164
4.55M
        pixel *const top = &topleft_out[1];
165
166
4.55M
        if (have_top) {
167
4.38M
            const int px_have = imin(sz, (w - x) << 2);
168
4.38M
            pixel_copy(top, dst_top, px_have);
169
4.38M
            if (px_have < sz)
170
312k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
4.38M
        } else {
172
176k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
176k
        }
174
175
4.55M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
255k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
255k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
255k
            if (have_topright) {
180
148k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
148k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
148k
                if (px_have < sz)
184
5.41k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
5.41k
                              sz - px_have);
186
148k
            } else {
187
106k
                pixel_set(top + sz, top[sz - 1], sz);
188
106k
            }
189
255k
        }
190
4.55M
    }
191
192
5.45M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.72M
        if (have_left)
194
1.60M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
121k
        else
196
121k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.72M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
127k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
127k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.72M
    }
202
203
5.45M
    return mode;
204
5.45M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
6.43M
{
87
6.43M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
6.43M
    assert(y < h && x < w);
89
90
6.43M
    switch (mode) {
91
374k
    case VERT_PRED:
92
992k
    case HOR_PRED:
93
1.11M
    case DIAG_DOWN_LEFT_PRED:
94
1.23M
    case DIAG_DOWN_RIGHT_PRED:
95
1.34M
    case VERT_RIGHT_PRED:
96
1.48M
    case HOR_DOWN_PRED:
97
1.77M
    case HOR_UP_PRED:
98
1.93M
    case VERT_LEFT_PRED: {
99
1.93M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.93M
        if (*angle <= 90)
102
552k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.38M
        else if (*angle < 180)
104
621k
            mode = Z2_PRED;
105
763k
        else
106
763k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.93M
        break;
108
1.77M
    }
109
2.73M
    case DC_PRED:
110
3.17M
    case PAETH_PRED:
111
3.17M
        mode = av1_mode_conv[mode][have_left][have_top];
112
3.17M
        break;
113
1.31M
    default:
114
1.31M
        break;
115
6.43M
    }
116
117
6.38M
    const pixel *dst_top;
118
6.38M
    if (have_top &&
119
5.74M
        (av1_intra_prediction_edges[mode].needs_top ||
120
703k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
336k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
5.47M
    {
123
5.47M
        if (prefilter_toplevel_sb_edge) {
124
708k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
4.76M
        } else {
126
4.76M
            dst_top = &dst[-PXSTRIDE(stride)];
127
4.76M
        }
128
5.47M
    }
129
130
6.38M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
5.30M
        const int sz = th << 2;
132
5.30M
        pixel *const left = &topleft_out[-sz];
133
134
5.30M
        if (have_left) {
135
5.02M
            const int px_have = imin(sz, (h - y) << 2);
136
137
55.9M
            for (int i = 0; i < px_have; i++)
138
50.9M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
5.02M
            if (px_have < sz)
140
134k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
5.02M
        } else {
142
271k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
271k
        }
144
145
5.30M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
391k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
391k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
391k
            if (have_bottomleft) {
150
103k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.00M
                for (int i = 0; i < px_have; i++)
153
905k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
103k
                if (px_have < sz)
155
1.31k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
287k
            } else {
157
287k
                pixel_set(left - sz, left[0], sz);
158
287k
            }
159
391k
        }
160
5.30M
    }
161
162
6.38M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
5.20M
        const int sz = tw << 2;
164
5.20M
        pixel *const top = &topleft_out[1];
165
166
5.20M
        if (have_top) {
167
5.00M
            const int px_have = imin(sz, (w - x) << 2);
168
5.00M
            pixel_copy(top, dst_top, px_have);
169
5.00M
            if (px_have < sz)
170
307k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
5.00M
        } else {
172
197k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
197k
        }
174
175
5.20M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
325k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
325k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
325k
            if (have_topright) {
180
201k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
201k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
201k
                if (px_have < sz)
184
7.43k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
7.43k
                              sz - px_have);
186
201k
            } else {
187
124k
                pixel_set(top + sz, top[sz - 1], sz);
188
124k
            }
189
325k
        }
190
5.20M
    }
191
192
6.38M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.98M
        if (have_left)
194
1.86M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
123k
        else
196
123k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.98M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
152k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
152k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.98M
    }
202
203
6.38M
    return mode;
204
6.43M
}