Coverage Report

Created: 2026-06-15 06:25

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
16.6M
{
87
16.6M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
16.6M
    assert(y < h && x < w);
89
90
16.6M
    switch (mode) {
91
407k
    case VERT_PRED:
92
1.09M
    case HOR_PRED:
93
1.23M
    case DIAG_DOWN_LEFT_PRED:
94
1.36M
    case DIAG_DOWN_RIGHT_PRED:
95
1.49M
    case VERT_RIGHT_PRED:
96
1.64M
    case HOR_DOWN_PRED:
97
2.03M
    case HOR_UP_PRED:
98
2.23M
    case VERT_LEFT_PRED: {
99
2.23M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.23M
        if (*angle <= 90)
102
639k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.59M
        else if (*angle < 180)
104
696k
            mode = Z2_PRED;
105
895k
        else
106
895k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.23M
        break;
108
2.03M
    }
109
11.8M
    case DC_PRED:
110
12.6M
    case PAETH_PRED:
111
12.6M
        mode = av1_mode_conv[mode][have_left][have_top];
112
12.6M
        break;
113
1.74M
    default:
114
1.74M
        break;
115
16.6M
    }
116
117
16.5M
    const pixel *dst_top;
118
16.5M
    if (have_top &&
119
15.4M
        (av1_intra_prediction_edges[mode].needs_top ||
120
800k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
347k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
15.2M
    {
123
15.2M
        if (prefilter_toplevel_sb_edge) {
124
1.44M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
13.7M
        } else {
126
13.7M
            dst_top = &dst[-PXSTRIDE(stride)];
127
13.7M
        }
128
15.2M
    }
129
130
16.5M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
14.7M
        const int sz = th << 2;
132
14.7M
        pixel *const left = &topleft_out[-sz];
133
134
14.7M
        if (have_left) {
135
14.3M
            const int px_have = imin(sz, (h - y) << 2);
136
137
129M
            for (int i = 0; i < px_have; i++)
138
115M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
14.3M
            if (px_have < sz)
140
257k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
14.3M
        } else {
142
421k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
421k
        }
144
145
14.7M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
502k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
502k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
502k
            if (have_bottomleft) {
150
114k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.39M
                for (int i = 0; i < px_have; i++)
153
1.28M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
114k
                if (px_have < sz)
155
1.66k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
388k
            } else {
157
388k
                pixel_set(left - sz, left[0], sz);
158
388k
            }
159
502k
        }
160
14.7M
    }
161
162
16.5M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
14.9M
        const int sz = tw << 2;
164
14.9M
        pixel *const top = &topleft_out[1];
165
166
14.9M
        if (have_top) {
167
14.6M
            const int px_have = imin(sz, (w - x) << 2);
168
14.6M
            pixel_copy(top, dst_top, px_have);
169
14.6M
            if (px_have < sz)
170
577k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
14.6M
        } else {
172
324k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
324k
        }
174
175
14.9M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
377k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
377k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
377k
            if (have_topright) {
180
224k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
224k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
224k
                if (px_have < sz)
184
3.87k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
1.90k
                              sz - px_have);
186
224k
            } else {
187
152k
                pixel_set(top + sz, top[sz - 1], sz);
188
152k
            }
189
377k
        }
190
14.9M
    }
191
192
16.5M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.52M
        if (have_left)
194
2.33M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
192k
        else
196
192k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.52M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
210k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
210k
                            topleft_out[0] * 6 + 8) >> 4;
201
2.52M
    }
202
203
16.5M
    return mode;
204
16.6M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
6.89M
{
87
6.89M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
6.89M
    assert(y < h && x < w);
89
90
6.89M
    switch (mode) {
91
152k
    case VERT_PRED:
92
423k
    case HOR_PRED:
93
479k
    case DIAG_DOWN_LEFT_PRED:
94
539k
    case DIAG_DOWN_RIGHT_PRED:
95
597k
    case VERT_RIGHT_PRED:
96
663k
    case HOR_DOWN_PRED:
97
814k
    case HOR_UP_PRED:
98
887k
    case VERT_LEFT_PRED: {
99
887k
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
887k
        if (*angle <= 90)
102
242k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
644k
        else if (*angle < 180)
104
295k
            mode = Z2_PRED;
105
349k
        else
106
349k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
887k
        break;
108
814k
    }
109
4.89M
    case DC_PRED:
110
5.32M
    case PAETH_PRED:
111
5.32M
        mode = av1_mode_conv[mode][have_left][have_top];
112
5.32M
        break;
113
701k
    default:
114
701k
        break;
115
6.89M
    }
116
117
6.89M
    const pixel *dst_top;
118
6.89M
    if (have_top &&
119
6.38M
        (av1_intra_prediction_edges[mode].needs_top ||
120
309k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
140k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
6.28M
    {
123
6.28M
        if (prefilter_toplevel_sb_edge) {
124
661k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
5.62M
        } else {
126
5.62M
            dst_top = &dst[-PXSTRIDE(stride)];
127
5.62M
        }
128
6.28M
    }
129
130
6.89M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
6.05M
        const int sz = th << 2;
132
6.05M
        pixel *const left = &topleft_out[-sz];
133
134
6.05M
        if (have_left) {
135
5.83M
            const int px_have = imin(sz, (h - y) << 2);
136
137
55.2M
            for (int i = 0; i < px_have; i++)
138
49.4M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
5.83M
            if (px_have < sz)
140
125k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
5.83M
        } else {
142
220k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
220k
        }
144
145
6.05M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
189k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
189k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
189k
            if (have_bottomleft) {
150
42.3k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
483k
                for (int i = 0; i < px_have; i++)
153
440k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
42.3k
                if (px_have < sz)
155
696
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
147k
            } else {
157
147k
                pixel_set(left - sz, left[0], sz);
158
147k
            }
159
189k
        }
160
6.05M
    }
161
162
6.89M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
6.21M
        const int sz = tw << 2;
164
6.21M
        pixel *const top = &topleft_out[1];
165
166
6.21M
        if (have_top) {
167
6.06M
            const int px_have = imin(sz, (w - x) << 2);
168
6.06M
            pixel_copy(top, dst_top, px_have);
169
6.06M
            if (px_have < sz)
170
279k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
6.06M
        } else {
172
149k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
149k
        }
174
175
6.21M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
141k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
141k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
141k
            if (have_topright) {
180
78.5k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
78.5k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
78.5k
                if (px_have < sz)
184
1.90k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
1.90k
                              sz - px_have);
186
78.5k
            } else {
187
63.1k
                pixel_set(top + sz, top[sz - 1], sz);
188
63.1k
            }
189
141k
        }
190
6.21M
    }
191
192
6.89M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.09M
        if (have_left)
194
993k
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
101k
        else
196
101k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.09M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
81.7k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
81.7k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.09M
    }
202
203
6.89M
    return mode;
204
6.89M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
9.70M
{
87
9.70M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
9.70M
    assert(y < h && x < w);
89
90
9.70M
    switch (mode) {
91
255k
    case VERT_PRED:
92
667k
    case HOR_PRED:
93
755k
    case DIAG_DOWN_LEFT_PRED:
94
828k
    case DIAG_DOWN_RIGHT_PRED:
95
899k
    case VERT_RIGHT_PRED:
96
986k
    case HOR_DOWN_PRED:
97
1.22M
    case HOR_UP_PRED:
98
1.34M
    case VERT_LEFT_PRED: {
99
1.34M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.34M
        if (*angle <= 90)
102
396k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
947k
        else if (*angle < 180)
104
401k
            mode = Z2_PRED;
105
546k
        else
106
546k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.34M
        break;
108
1.22M
    }
109
6.98M
    case DC_PRED:
110
7.36M
    case PAETH_PRED:
111
7.36M
        mode = av1_mode_conv[mode][have_left][have_top];
112
7.36M
        break;
113
1.04M
    default:
114
1.04M
        break;
115
9.70M
    }
116
117
9.69M
    const pixel *dst_top;
118
9.69M
    if (have_top &&
119
9.10M
        (av1_intra_prediction_edges[mode].needs_top ||
120
490k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
207k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
8.95M
    {
123
8.95M
        if (prefilter_toplevel_sb_edge) {
124
778k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
8.17M
        } else {
126
8.17M
            dst_top = &dst[-PXSTRIDE(stride)];
127
8.17M
        }
128
8.95M
    }
129
130
9.69M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
8.69M
        const int sz = th << 2;
132
8.69M
        pixel *const left = &topleft_out[-sz];
133
134
8.69M
        if (have_left) {
135
8.49M
            const int px_have = imin(sz, (h - y) << 2);
136
137
74.7M
            for (int i = 0; i < px_have; i++)
138
66.2M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
8.49M
            if (px_have < sz)
140
131k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
8.49M
        } else {
142
200k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
200k
        }
144
145
8.69M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
312k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
312k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
312k
            if (have_bottomleft) {
150
71.8k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
911k
                for (int i = 0; i < px_have; i++)
153
839k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
71.8k
                if (px_have < sz)
155
971
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
240k
            } else {
157
240k
                pixel_set(left - sz, left[0], sz);
158
240k
            }
159
312k
        }
160
8.69M
    }
161
162
9.69M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
8.76M
        const int sz = tw << 2;
164
8.76M
        pixel *const top = &topleft_out[1];
165
166
8.76M
        if (have_top) {
167
8.58M
            const int px_have = imin(sz, (w - x) << 2);
168
8.58M
            pixel_copy(top, dst_top, px_have);
169
8.58M
            if (px_have < sz)
170
297k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
8.58M
        } else {
172
175k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
175k
        }
174
175
8.76M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
235k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
235k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
235k
            if (have_topright) {
180
146k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
146k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
146k
                if (px_have < sz)
184
1.96k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
1.96k
                              sz - px_have);
186
146k
            } else {
187
89.3k
                pixel_set(top + sz, top[sz - 1], sz);
188
89.3k
            }
189
235k
        }
190
8.76M
    }
191
192
9.69M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.43M
        if (have_left)
194
1.34M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
90.9k
        else
196
90.9k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.43M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
129k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
129k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.43M
    }
202
203
9.69M
    return mode;
204
9.70M
}