Coverage Report

Created: 2026-07-16 06:32

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.5M
{
87
11.5M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
11.5M
    assert(y < h && x < w);
89
90
11.5M
    switch (mode) {
91
441k
    case VERT_PRED:
92
1.17M
    case HOR_PRED:
93
1.32M
    case DIAG_DOWN_LEFT_PRED:
94
1.46M
    case DIAG_DOWN_RIGHT_PRED:
95
1.60M
    case VERT_RIGHT_PRED:
96
1.76M
    case HOR_DOWN_PRED:
97
2.17M
    case HOR_UP_PRED:
98
2.36M
    case VERT_LEFT_PRED: {
99
2.36M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.36M
        if (*angle <= 90)
102
677k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.69M
        else if (*angle < 180)
104
739k
            mode = Z2_PRED;
105
951k
        else
106
951k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.36M
        break;
108
2.17M
    }
109
6.46M
    case DC_PRED:
110
7.45M
    case PAETH_PRED:
111
7.45M
        mode = av1_mode_conv[mode][have_left][have_top];
112
7.45M
        break;
113
1.79M
    default:
114
1.79M
        break;
115
11.5M
    }
116
117
11.5M
    const pixel *dst_top;
118
11.5M
    if (have_top &&
119
10.4M
        (av1_intra_prediction_edges[mode].needs_top ||
120
861k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
375k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
10.1M
    {
123
10.1M
        if (prefilter_toplevel_sb_edge) {
124
1.30M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
8.87M
        } else {
126
8.87M
            dst_top = &dst[-PXSTRIDE(stride)];
127
8.87M
        }
128
10.1M
    }
129
130
11.5M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
9.78M
        const int sz = th << 2;
132
9.78M
        pixel *const left = &topleft_out[-sz];
133
134
9.78M
        if (have_left) {
135
9.34M
            const int px_have = imin(sz, (h - y) << 2);
136
137
110M
            for (int i = 0; i < px_have; i++)
138
101M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
9.34M
            if (px_have < sz)
140
231k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
9.34M
        } else {
142
439k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
439k
        }
144
145
9.78M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
532k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
532k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
532k
            if (have_bottomleft) {
150
118k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.41M
                for (int i = 0; i < px_have; i++)
153
1.29M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
118k
                if (px_have < sz)
155
1.62k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
413k
            } else {
157
413k
                pixel_set(left - sz, left[0], sz);
158
413k
            }
159
532k
        }
160
9.78M
    }
161
162
11.5M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
9.87M
        const int sz = tw << 2;
164
9.87M
        pixel *const top = &topleft_out[1];
165
166
9.87M
        if (have_top) {
167
9.56M
            const int px_have = imin(sz, (w - x) << 2);
168
9.56M
            pixel_copy(top, dst_top, px_have);
169
9.56M
            if (px_have < sz)
170
559k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
9.56M
        } else {
172
309k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
309k
        }
174
175
9.87M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
397k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
397k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
397k
            if (have_topright) {
180
238k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
238k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
238k
                if (px_have < sz)
184
5.67k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
2.54k
                              sz - px_have);
186
238k
            } else {
187
159k
                pixel_set(top + sz, top[sz - 1], sz);
188
159k
            }
189
397k
        }
190
9.87M
    }
191
192
11.5M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.79M
        if (have_left)
194
2.59M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
202k
        else
196
202k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.79M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
222k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
222k
                            topleft_out[0] * 6 + 8) >> 4;
201
2.79M
    }
202
203
11.5M
    return mode;
204
11.5M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
4.66M
{
87
4.66M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
4.66M
    assert(y < h && x < w);
89
90
4.66M
    switch (mode) {
91
177k
    case VERT_PRED:
92
476k
    case HOR_PRED:
93
538k
    case DIAG_DOWN_LEFT_PRED:
94
601k
    case DIAG_DOWN_RIGHT_PRED:
95
662k
    case VERT_RIGHT_PRED:
96
730k
    case HOR_DOWN_PRED:
97
895k
    case HOR_UP_PRED:
98
974k
    case VERT_LEFT_PRED: {
99
974k
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
974k
        if (*angle <= 90)
102
273k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
700k
        else if (*angle < 180)
104
313k
            mode = Z2_PRED;
105
387k
        else
106
387k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
974k
        break;
108
895k
    }
109
2.47M
    case DC_PRED:
110
2.97M
    case PAETH_PRED:
111
2.97M
        mode = av1_mode_conv[mode][have_left][have_top];
112
2.97M
        break;
113
729k
    default:
114
729k
        break;
115
4.66M
    }
116
117
4.66M
    const pixel *dst_top;
118
4.66M
    if (have_top &&
119
4.14M
        (av1_intra_prediction_edges[mode].needs_top ||
120
346k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
156k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.03M
    {
123
4.03M
        if (prefilter_toplevel_sb_edge) {
124
622k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.41M
        } else {
126
3.41M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.41M
        }
128
4.03M
    }
129
130
4.66M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
3.84M
        const int sz = th << 2;
132
3.84M
        pixel *const left = &topleft_out[-sz];
133
134
3.84M
        if (have_left) {
135
3.61M
            const int px_have = imin(sz, (h - y) << 2);
136
137
46.9M
            for (int i = 0; i < px_have; i++)
138
43.2M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
3.61M
            if (px_have < sz)
140
118k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
3.61M
        } else {
142
230k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
230k
        }
144
145
3.84M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
210k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
210k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
210k
            if (have_bottomleft) {
150
44.8k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
499k
                for (int i = 0; i < px_have; i++)
153
454k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
44.8k
                if (px_have < sz)
155
679
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
165k
            } else {
157
165k
                pixel_set(left - sz, left[0], sz);
158
165k
            }
159
210k
        }
160
3.84M
    }
161
162
4.66M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
3.94M
        const int sz = tw << 2;
164
3.94M
        pixel *const top = &topleft_out[1];
165
166
3.94M
        if (have_top) {
167
3.79M
            const int px_have = imin(sz, (w - x) << 2);
168
3.79M
            pixel_copy(top, dst_top, px_have);
169
3.79M
            if (px_have < sz)
170
273k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
3.79M
        } else {
172
148k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
148k
        }
174
175
3.94M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
159k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
159k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
159k
            if (have_topright) {
180
92.0k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
92.0k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
92.0k
                if (px_have < sz)
184
2.54k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
2.54k
                              sz - px_have);
186
92.0k
            } else {
187
67.8k
                pixel_set(top + sz, top[sz - 1], sz);
188
67.8k
            }
189
159k
        }
190
3.94M
    }
191
192
4.66M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.22M
        if (have_left)
194
1.11M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
105k
        else
196
105k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.22M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
92.9k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
92.9k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.22M
    }
202
203
4.66M
    return mode;
204
4.66M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
6.88M
{
87
6.88M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
6.88M
    assert(y < h && x < w);
89
90
6.88M
    switch (mode) {
91
263k
    case VERT_PRED:
92
694k
    case HOR_PRED:
93
783k
    case DIAG_DOWN_LEFT_PRED:
94
858k
    case DIAG_DOWN_RIGHT_PRED:
95
939k
    case VERT_RIGHT_PRED:
96
1.03M
    case HOR_DOWN_PRED:
97
1.27M
    case HOR_UP_PRED:
98
1.39M
    case VERT_LEFT_PRED: {
99
1.39M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.39M
        if (*angle <= 90)
102
403k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
990k
        else if (*angle < 180)
104
425k
            mode = Z2_PRED;
105
564k
        else
106
564k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.39M
        break;
108
1.27M
    }
109
3.99M
    case DC_PRED:
110
4.47M
    case PAETH_PRED:
111
4.47M
        mode = av1_mode_conv[mode][have_left][have_top];
112
4.47M
        break;
113
1.06M
    default:
114
1.06M
        break;
115
6.88M
    }
116
117
6.88M
    const pixel *dst_top;
118
6.88M
    if (have_top &&
119
6.31M
        (av1_intra_prediction_edges[mode].needs_top ||
120
515k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
219k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
6.14M
    {
123
6.14M
        if (prefilter_toplevel_sb_edge) {
124
682k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
5.46M
        } else {
126
5.46M
            dst_top = &dst[-PXSTRIDE(stride)];
127
5.46M
        }
128
6.14M
    }
129
130
6.88M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
5.93M
        const int sz = th << 2;
132
5.93M
        pixel *const left = &topleft_out[-sz];
133
134
5.93M
        if (have_left) {
135
5.72M
            const int px_have = imin(sz, (h - y) << 2);
136
137
63.6M
            for (int i = 0; i < px_have; i++)
138
57.8M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
5.72M
            if (px_have < sz)
140
112k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
5.72M
        } else {
142
209k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
209k
        }
144
145
5.93M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
321k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
321k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
321k
            if (have_bottomleft) {
150
74.0k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
916k
                for (int i = 0; i < px_have; i++)
153
842k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
74.0k
                if (px_have < sz)
155
946
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
247k
            } else {
157
247k
                pixel_set(left - sz, left[0], sz);
158
247k
            }
159
321k
        }
160
5.93M
    }
161
162
6.88M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
5.92M
        const int sz = tw << 2;
164
5.92M
        pixel *const top = &topleft_out[1];
165
166
5.92M
        if (have_top) {
167
5.76M
            const int px_have = imin(sz, (w - x) << 2);
168
5.76M
            pixel_copy(top, dst_top, px_have);
169
5.76M
            if (px_have < sz)
170
285k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
5.76M
        } else {
172
161k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
161k
        }
174
175
5.92M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
238k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
238k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
238k
            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
3.12k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
3.12k
                              sz - px_have);
186
146k
            } else {
187
91.5k
                pixel_set(top + sz, top[sz - 1], sz);
188
91.5k
            }
189
238k
        }
190
5.92M
    }
191
192
6.88M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.57M
        if (have_left)
194
1.47M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
97.1k
        else
196
97.1k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.57M
        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.57M
    }
202
203
6.88M
    return mode;
204
6.88M
}