Coverage Report

Created: 2026-07-30 06:27

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
13.6M
{
87
13.6M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
13.6M
    assert(y < h && x < w);
89
90
13.6M
    switch (mode) {
91
603k
    case VERT_PRED:
92
1.58M
    case HOR_PRED:
93
1.78M
    case DIAG_DOWN_LEFT_PRED:
94
1.97M
    case DIAG_DOWN_RIGHT_PRED:
95
2.19M
    case VERT_RIGHT_PRED:
96
2.42M
    case HOR_DOWN_PRED:
97
2.95M
    case HOR_UP_PRED:
98
3.21M
    case VERT_LEFT_PRED: {
99
3.21M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
3.21M
        if (*angle <= 90)
102
897k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
2.31M
        else if (*angle < 180)
104
1.03M
            mode = Z2_PRED;
105
1.27M
        else
106
1.27M
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
3.21M
        break;
108
2.95M
    }
109
6.91M
    case DC_PRED:
110
8.02M
    case PAETH_PRED:
111
8.02M
        mode = av1_mode_conv[mode][have_left][have_top];
112
8.02M
        break;
113
2.42M
    default:
114
2.42M
        break;
115
13.6M
    }
116
117
13.4M
    const pixel *dst_top;
118
13.4M
    if (have_top &&
119
12.2M
        (av1_intra_prediction_edges[mode].needs_top ||
120
1.17M
         av1_intra_prediction_edges[mode].needs_topleft ||
121
516k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
11.8M
    {
123
11.8M
        if (prefilter_toplevel_sb_edge) {
124
1.54M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
10.3M
        } else {
126
10.3M
            dst_top = &dst[-PXSTRIDE(stride)];
127
10.3M
        }
128
11.8M
    }
129
130
13.4M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
11.2M
        const int sz = th << 2;
132
11.2M
        pixel *const left = &topleft_out[-sz];
133
134
11.2M
        if (have_left) {
135
10.7M
            const int px_have = imin(sz, (h - y) << 2);
136
137
124M
            for (int i = 0; i < px_have; i++)
138
113M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
10.7M
            if (px_have < sz)
140
226k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
10.7M
        } else {
142
540k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
540k
        }
144
145
11.2M
        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
175k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.79M
                for (int i = 0; i < px_have; i++)
153
1.61M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
175k
                if (px_have < sz)
155
1.92k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
510k
            } else {
157
510k
                pixel_set(left - sz, left[0], sz);
158
510k
            }
159
685k
        }
160
11.2M
    }
161
162
13.4M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
11.2M
        const int sz = tw << 2;
164
11.2M
        pixel *const top = &topleft_out[1];
165
166
11.2M
        if (have_top) {
167
10.9M
            const int px_have = imin(sz, (w - x) << 2);
168
10.9M
            pixel_copy(top, dst_top, px_have);
169
10.9M
            if (px_have < sz)
170
596k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
10.9M
        } else {
172
326k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
326k
        }
174
175
11.2M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
527k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
527k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
527k
            if (have_topright) {
180
314k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
314k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
314k
                if (px_have < sz)
184
11.0k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
4.57k
                              sz - px_have);
186
314k
            } else {
187
212k
                pixel_set(top + sz, top[sz - 1], sz);
188
212k
            }
189
527k
        }
190
11.2M
    }
191
192
13.4M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
3.69M
        if (have_left)
194
3.44M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
253k
        else
196
253k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
3.69M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
294k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
294k
                            topleft_out[0] * 6 + 8) >> 4;
201
3.69M
    }
202
203
13.4M
    return mode;
204
13.6M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
7.60M
{
87
7.60M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
7.60M
    assert(y < h && x < w);
89
90
7.60M
    switch (mode) {
91
312k
    case VERT_PRED:
92
828k
    case HOR_PRED:
93
929k
    case DIAG_DOWN_LEFT_PRED:
94
1.03M
    case DIAG_DOWN_RIGHT_PRED:
95
1.13M
    case VERT_RIGHT_PRED:
96
1.25M
    case HOR_DOWN_PRED:
97
1.52M
    case HOR_UP_PRED:
98
1.65M
    case VERT_LEFT_PRED: {
99
1.65M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.65M
        if (*angle <= 90)
102
468k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.18M
        else if (*angle < 180)
104
533k
            mode = Z2_PRED;
105
652k
        else
106
652k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.65M
        break;
108
1.52M
    }
109
4.17M
    case DC_PRED:
110
4.75M
    case PAETH_PRED:
111
4.75M
        mode = av1_mode_conv[mode][have_left][have_top];
112
4.75M
        break;
113
1.23M
    default:
114
1.23M
        break;
115
7.60M
    }
116
117
7.56M
    const pixel *dst_top;
118
7.56M
    if (have_top &&
119
7.00M
        (av1_intra_prediction_edges[mode].needs_top ||
120
603k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
269k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
6.78M
    {
123
6.78M
        if (prefilter_toplevel_sb_edge) {
124
895k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
5.89M
        } else {
126
5.89M
            dst_top = &dst[-PXSTRIDE(stride)];
127
5.89M
        }
128
6.78M
    }
129
130
7.56M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
6.44M
        const int sz = th << 2;
132
6.44M
        pixel *const left = &topleft_out[-sz];
133
134
6.44M
        if (have_left) {
135
6.15M
            const int px_have = imin(sz, (h - y) << 2);
136
137
66.8M
            for (int i = 0; i < px_have; i++)
138
60.6M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
6.15M
            if (px_have < sz)
140
118k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
6.15M
        } else {
142
294k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
294k
        }
144
145
6.44M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
353k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
353k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
353k
            if (have_bottomleft) {
150
96.0k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
936k
                for (int i = 0; i < px_have; i++)
153
840k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
96.0k
                if (px_have < sz)
155
1.08k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
257k
            } else {
157
257k
                pixel_set(left - sz, left[0], sz);
158
257k
            }
159
353k
        }
160
6.44M
    }
161
162
7.56M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
6.53M
        const int sz = tw << 2;
164
6.53M
        pixel *const top = &topleft_out[1];
165
166
6.53M
        if (have_top) {
167
6.37M
            const int px_have = imin(sz, (w - x) << 2);
168
6.37M
            pixel_copy(top, dst_top, px_have);
169
6.37M
            if (px_have < sz)
170
284k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
6.37M
        } else {
172
157k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
157k
        }
174
175
6.53M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
277k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
277k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
277k
            if (have_topright) {
180
161k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
161k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
161k
                if (px_have < sz)
184
4.57k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
4.57k
                              sz - px_have);
186
161k
            } else {
187
116k
                pixel_set(top + sz, top[sz - 1], sz);
188
116k
            }
189
277k
        }
190
6.53M
    }
191
192
7.56M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.94M
        if (have_left)
194
1.81M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
136k
        else
196
136k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.94M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
141k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
141k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.94M
    }
202
203
7.56M
    return mode;
204
7.60M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
5.99M
{
87
5.99M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.99M
    assert(y < h && x < w);
89
90
5.99M
    switch (mode) {
91
290k
    case VERT_PRED:
92
760k
    case HOR_PRED:
93
853k
    case DIAG_DOWN_LEFT_PRED:
94
942k
    case DIAG_DOWN_RIGHT_PRED:
95
1.05M
    case VERT_RIGHT_PRED:
96
1.16M
    case HOR_DOWN_PRED:
97
1.43M
    case HOR_UP_PRED:
98
1.55M
    case VERT_LEFT_PRED: {
99
1.55M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.55M
        if (*angle <= 90)
102
429k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.12M
        else if (*angle < 180)
104
501k
            mode = Z2_PRED;
105
625k
        else
106
625k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.55M
        break;
108
1.43M
    }
109
2.74M
    case DC_PRED:
110
3.26M
    case PAETH_PRED:
111
3.26M
        mode = av1_mode_conv[mode][have_left][have_top];
112
3.26M
        break;
113
1.18M
    default:
114
1.18M
        break;
115
5.99M
    }
116
117
5.87M
    const pixel *dst_top;
118
5.87M
    if (have_top &&
119
5.27M
        (av1_intra_prediction_edges[mode].needs_top ||
120
570k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
246k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
5.08M
    {
123
5.08M
        if (prefilter_toplevel_sb_edge) {
124
645k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
4.44M
        } else {
126
4.44M
            dst_top = &dst[-PXSTRIDE(stride)];
127
4.44M
        }
128
5.08M
    }
129
130
5.87M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
4.83M
        const int sz = th << 2;
132
4.83M
        pixel *const left = &topleft_out[-sz];
133
134
4.83M
        if (have_left) {
135
4.58M
            const int px_have = imin(sz, (h - y) << 2);
136
137
57.5M
            for (int i = 0; i < px_have; i++)
138
52.9M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
4.58M
            if (px_have < sz)
140
108k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
4.58M
        } else {
142
245k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
245k
        }
144
145
4.83M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
332k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
331k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
331k
            if (have_bottomleft) {
150
79.2k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
855k
                for (int i = 0; i < px_have; i++)
153
776k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
79.2k
                if (px_have < sz)
155
839
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
252k
            } else {
157
252k
                pixel_set(left - sz, left[0], sz);
158
252k
            }
159
331k
        }
160
4.83M
    }
161
162
5.87M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.74M
        const int sz = tw << 2;
164
4.74M
        pixel *const top = &topleft_out[1];
165
166
4.74M
        if (have_top) {
167
4.57M
            const int px_have = imin(sz, (w - x) << 2);
168
4.57M
            pixel_copy(top, dst_top, px_have);
169
4.57M
            if (px_have < sz)
170
311k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
4.57M
        } else {
172
169k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
169k
        }
174
175
4.74M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
250k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
250k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
250k
            if (have_topright) {
180
153k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
153k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
153k
                if (px_have < sz)
184
6.49k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
6.49k
                              sz - px_have);
186
153k
            } else {
187
96.8k
                pixel_set(top + sz, top[sz - 1], sz);
188
96.8k
            }
189
250k
        }
190
4.74M
    }
191
192
5.87M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.74M
        if (have_left)
194
1.63M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
117k
        else
196
117k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.74M
        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.74M
    }
202
203
5.87M
    return mode;
204
5.99M
}