Coverage Report

Created: 2026-08-13 07:23

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.7M
{
87
11.7M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
11.7M
    assert(y < h && x < w);
89
90
11.7M
    switch (mode) {
91
434k
    case VERT_PRED:
92
1.18M
    case HOR_PRED:
93
1.33M
    case DIAG_DOWN_LEFT_PRED:
94
1.49M
    case DIAG_DOWN_RIGHT_PRED:
95
1.64M
    case VERT_RIGHT_PRED:
96
1.82M
    case HOR_DOWN_PRED:
97
2.25M
    case HOR_UP_PRED:
98
2.45M
    case VERT_LEFT_PRED: {
99
2.45M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.45M
        if (*angle <= 90)
102
674k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.78M
        else if (*angle < 180)
104
791k
            mode = Z2_PRED;
105
989k
        else
106
989k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.45M
        break;
108
2.25M
    }
109
6.65M
    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.85M
    default:
114
1.85M
        break;
115
11.7M
    }
116
117
11.7M
    const pixel *dst_top;
118
11.7M
    if (have_top &&
119
10.8M
        (av1_intra_prediction_edges[mode].needs_top ||
120
919k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
398k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
10.5M
    {
123
10.5M
        if (prefilter_toplevel_sb_edge) {
124
1.25M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
9.32M
        } else {
126
9.32M
            dst_top = &dst[-PXSTRIDE(stride)];
127
9.32M
        }
128
10.5M
    }
129
130
11.7M
    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.81M
            const int px_have = imin(sz, (h - y) << 2);
136
137
96.7M
            for (int i = 0; i < px_have; i++)
138
86.9M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
9.81M
            if (px_have < sz)
140
155k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
9.81M
        } else {
142
368k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
368k
        }
144
145
10.1M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
554k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
554k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
554k
            if (have_bottomleft) {
150
144k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.36M
                for (int i = 0; i < px_have; i++)
153
1.21M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
144k
                if (px_have < sz)
155
1.43k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
409k
            } else {
157
409k
                pixel_set(left - sz, left[0], sz);
158
409k
            }
159
554k
        }
160
10.1M
    }
161
162
11.7M
    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.94M
            const int px_have = imin(sz, (w - x) << 2);
168
9.94M
            pixel_copy(top, dst_top, px_have);
169
9.94M
            if (px_have < sz)
170
435k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
9.94M
        } else {
172
241k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
241k
        }
174
175
10.1M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
402k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
402k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
402k
            if (have_topright) {
180
240k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
240k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
240k
                if (px_have < sz)
184
6.50k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
2.39k
                              sz - px_have);
186
240k
            } else {
187
162k
                pixel_set(top + sz, top[sz - 1], sz);
188
162k
            }
189
402k
        }
190
10.1M
    }
191
192
11.7M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.74M
        if (have_left)
194
2.58M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
162k
        else
196
162k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.74M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
213k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
213k
                            topleft_out[0] * 6 + 8) >> 4;
201
2.74M
    }
202
203
11.7M
    return mode;
204
11.7M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
6.46M
{
87
6.46M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
6.46M
    assert(y < h && x < w);
89
90
6.46M
    switch (mode) {
91
184k
    case VERT_PRED:
92
509k
    case HOR_PRED:
93
570k
    case DIAG_DOWN_LEFT_PRED:
94
633k
    case DIAG_DOWN_RIGHT_PRED:
95
695k
    case VERT_RIGHT_PRED:
96
769k
    case HOR_DOWN_PRED:
97
939k
    case HOR_UP_PRED:
98
1.01M
    case VERT_LEFT_PRED: {
99
1.01M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.01M
        if (*angle <= 90)
102
279k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
739k
        else if (*angle < 180)
104
326k
            mode = Z2_PRED;
105
413k
        else
106
413k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.01M
        break;
108
939k
    }
109
4.23M
    case DC_PRED:
110
4.66M
    case PAETH_PRED:
111
4.66M
        mode = av1_mode_conv[mode][have_left][have_top];
112
4.66M
        break;
113
814k
    default:
114
814k
        break;
115
6.46M
    }
116
117
6.47M
    const pixel *dst_top;
118
6.47M
    if (have_top &&
119
6.09M
        (av1_intra_prediction_edges[mode].needs_top ||
120
380k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
167k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
5.96M
    {
123
5.96M
        if (prefilter_toplevel_sb_edge) {
124
631k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
5.33M
        } else {
126
5.33M
            dst_top = &dst[-PXSTRIDE(stride)];
127
5.33M
        }
128
5.96M
    }
129
130
6.47M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
5.73M
        const int sz = th << 2;
132
5.73M
        pixel *const left = &topleft_out[-sz];
133
134
5.73M
        if (have_left) {
135
5.56M
            const int px_have = imin(sz, (h - y) << 2);
136
137
50.0M
            for (int i = 0; i < px_have; i++)
138
44.4M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
5.56M
            if (px_have < sz)
140
78.1k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
5.56M
        } else {
142
172k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
172k
        }
144
145
5.73M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
228k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
228k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
228k
            if (have_bottomleft) {
150
60.0k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
571k
                for (int i = 0; i < px_have; i++)
153
511k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
60.0k
                if (px_have < sz)
155
643
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
168k
            } else {
157
168k
                pixel_set(left - sz, left[0], sz);
158
168k
            }
159
228k
        }
160
5.73M
    }
161
162
6.47M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
5.82M
        const int sz = tw << 2;
164
5.82M
        pixel *const top = &topleft_out[1];
165
166
5.82M
        if (have_top) {
167
5.70M
            const int px_have = imin(sz, (w - x) << 2);
168
5.70M
            pixel_copy(top, dst_top, px_have);
169
5.70M
            if (px_have < sz)
170
207k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
5.70M
        } else {
172
114k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
114k
        }
174
175
5.82M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
164k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
164k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
164k
            if (have_topright) {
180
96.6k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
96.6k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
96.6k
                if (px_have < sz)
184
2.39k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
2.39k
                              sz - px_have);
186
96.6k
            } else {
187
67.9k
                pixel_set(top + sz, top[sz - 1], sz);
188
67.9k
            }
189
164k
        }
190
5.82M
    }
191
192
6.47M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.24M
        if (have_left)
194
1.17M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
72.7k
        else
196
72.7k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.24M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
91.2k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
91.2k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.24M
    }
202
203
6.47M
    return mode;
204
6.46M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
5.24M
{
87
5.24M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.24M
    assert(y < h && x < w);
89
90
5.24M
    switch (mode) {
91
250k
    case VERT_PRED:
92
678k
    case HOR_PRED:
93
764k
    case DIAG_DOWN_LEFT_PRED:
94
864k
    case DIAG_DOWN_RIGHT_PRED:
95
953k
    case VERT_RIGHT_PRED:
96
1.05M
    case HOR_DOWN_PRED:
97
1.31M
    case HOR_UP_PRED:
98
1.43M
    case VERT_LEFT_PRED: {
99
1.43M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.43M
        if (*angle <= 90)
102
394k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.04M
        else if (*angle < 180)
104
464k
            mode = Z2_PRED;
105
576k
        else
106
576k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.43M
        break;
108
1.31M
    }
109
2.41M
    case DC_PRED:
110
2.79M
    case PAETH_PRED:
111
2.79M
        mode = av1_mode_conv[mode][have_left][have_top];
112
2.79M
        break;
113
1.03M
    default:
114
1.03M
        break;
115
5.24M
    }
116
117
5.23M
    const pixel *dst_top;
118
5.23M
    if (have_top &&
119
4.79M
        (av1_intra_prediction_edges[mode].needs_top ||
120
538k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
231k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.61M
    {
123
4.61M
        if (prefilter_toplevel_sb_edge) {
124
620k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.98M
        } else {
126
3.98M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.98M
        }
128
4.61M
    }
129
130
5.23M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
4.44M
        const int sz = th << 2;
132
4.44M
        pixel *const left = &topleft_out[-sz];
133
134
4.44M
        if (have_left) {
135
4.24M
            const int px_have = imin(sz, (h - y) << 2);
136
137
46.6M
            for (int i = 0; i < px_have; i++)
138
42.4M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
4.24M
            if (px_have < sz)
140
77.3k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
4.24M
        } else {
142
196k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
196k
        }
144
145
4.44M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
325k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
325k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
325k
            if (have_bottomleft) {
150
84.9k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
788k
                for (int i = 0; i < px_have; i++)
153
704k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
84.9k
                if (px_have < sz)
155
789
                    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
325k
        }
160
4.44M
    }
161
162
5.23M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.36M
        const int sz = tw << 2;
164
4.36M
        pixel *const top = &topleft_out[1];
165
166
4.36M
        if (have_top) {
167
4.24M
            const int px_have = imin(sz, (w - x) << 2);
168
4.24M
            pixel_copy(top, dst_top, px_have);
169
4.24M
            if (px_have < sz)
170
228k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
4.24M
        } else {
172
126k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
126k
        }
174
175
4.36M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
238k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
237k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
237k
            if (have_topright) {
180
143k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
143k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
143k
                if (px_have < sz)
184
4.10k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
4.10k
                              sz - px_have);
186
143k
            } else {
187
94.5k
                pixel_set(top + sz, top[sz - 1], sz);
188
94.5k
            }
189
237k
        }
190
4.36M
    }
191
192
5.23M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.50M
        if (have_left)
194
1.41M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
89.5k
        else
196
89.5k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.50M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
122k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
122k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.50M
    }
202
203
5.23M
    return mode;
204
5.24M
}