Coverage Report

Created: 2026-09-14 06:44

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
10.0M
{
87
10.0M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
10.0M
    assert(y < h && x < w);
89
90
10.0M
    switch (mode) {
91
500k
    case VERT_PRED:
92
1.33M
    case HOR_PRED:
93
1.50M
    case DIAG_DOWN_LEFT_PRED:
94
1.67M
    case DIAG_DOWN_RIGHT_PRED:
95
1.81M
    case VERT_RIGHT_PRED:
96
2.01M
    case HOR_DOWN_PRED:
97
2.39M
    case HOR_UP_PRED:
98
2.61M
    case VERT_LEFT_PRED: {
99
2.61M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.61M
        if (*angle <= 90)
102
762k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.84M
        else if (*angle < 180)
104
848k
            mode = Z2_PRED;
105
1.00M
        else
106
1.00M
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.61M
        break;
108
2.39M
    }
109
4.55M
    case DC_PRED:
110
5.56M
    case PAETH_PRED:
111
5.56M
        mode = av1_mode_conv[mode][have_left][have_top];
112
5.56M
        break;
113
1.88M
    default:
114
1.88M
        break;
115
10.0M
    }
116
117
9.99M
    const pixel *dst_top;
118
9.99M
    if (have_top &&
119
8.69M
        (av1_intra_prediction_edges[mode].needs_top ||
120
880k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
433k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
8.39M
    {
123
8.39M
        if (prefilter_toplevel_sb_edge) {
124
1.29M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
7.09M
        } else {
126
7.09M
            dst_top = &dst[-PXSTRIDE(stride)];
127
7.09M
        }
128
8.39M
    }
129
130
9.99M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
8.00M
        const int sz = th << 2;
132
8.00M
        pixel *const left = &topleft_out[-sz];
133
134
8.00M
        if (have_left) {
135
7.42M
            const int px_have = imin(sz, (h - y) << 2);
136
137
90.2M
            for (int i = 0; i < px_have; i++)
138
82.8M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
7.42M
            if (px_have < sz)
140
268k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
7.42M
        } else {
142
584k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
584k
        }
144
145
8.00M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
500k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
500k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
500k
            if (have_bottomleft) {
150
127k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.28M
                for (int i = 0; i < px_have; i++)
153
1.15M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
127k
                if (px_have < sz)
155
3.02k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
372k
            } else {
157
372k
                pixel_set(left - sz, left[0], sz);
158
372k
            }
159
500k
        }
160
8.00M
    }
161
162
9.99M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
8.16M
        const int sz = tw << 2;
164
8.16M
        pixel *const top = &topleft_out[1];
165
166
8.16M
        if (have_top) {
167
7.78M
            const int px_have = imin(sz, (w - x) << 2);
168
7.78M
            pixel_copy(top, dst_top, px_have);
169
7.78M
            if (px_have < sz)
170
676k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
7.78M
        } else {
172
387k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
387k
        }
174
175
8.16M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
439k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
439k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
439k
            if (have_topright) {
180
256k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
256k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
256k
                if (px_have < sz)
184
9.84k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
4.52k
                              sz - px_have);
186
256k
            } else {
187
183k
                pixel_set(top + sz, top[sz - 1], sz);
188
183k
            }
189
439k
        }
190
8.16M
    }
191
192
9.99M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.99M
        if (have_left)
194
2.72M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
267k
        else
196
267k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.99M
        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.99M
    }
202
203
9.99M
    return mode;
204
10.0M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
5.01M
{
87
5.01M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.01M
    assert(y < h && x < w);
89
90
5.01M
    switch (mode) {
91
240k
    case VERT_PRED:
92
635k
    case HOR_PRED:
93
717k
    case DIAG_DOWN_LEFT_PRED:
94
805k
    case DIAG_DOWN_RIGHT_PRED:
95
874k
    case VERT_RIGHT_PRED:
96
973k
    case HOR_DOWN_PRED:
97
1.14M
    case HOR_UP_PRED:
98
1.24M
    case VERT_LEFT_PRED: {
99
1.24M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.24M
        if (*angle <= 90)
102
365k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
884k
        else if (*angle < 180)
104
417k
            mode = Z2_PRED;
105
466k
        else
106
466k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.24M
        break;
108
1.14M
    }
109
2.27M
    case DC_PRED:
110
2.85M
    case PAETH_PRED:
111
2.85M
        mode = av1_mode_conv[mode][have_left][have_top];
112
2.85M
        break;
113
924k
    default:
114
924k
        break;
115
5.01M
    }
116
117
5.01M
    const pixel *dst_top;
118
5.01M
    if (have_top &&
119
4.40M
        (av1_intra_prediction_edges[mode].needs_top ||
120
412k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
207k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.25M
    {
123
4.25M
        if (prefilter_toplevel_sb_edge) {
124
723k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.53M
        } else {
126
3.53M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.53M
        }
128
4.25M
    }
129
130
5.01M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
4.04M
        const int sz = th << 2;
132
4.04M
        pixel *const left = &topleft_out[-sz];
133
134
4.04M
        if (have_left) {
135
3.76M
            const int px_have = imin(sz, (h - y) << 2);
136
137
44.7M
            for (int i = 0; i < px_have; i++)
138
40.9M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
3.76M
            if (px_have < sz)
140
134k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
3.76M
        } else {
142
274k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
274k
        }
144
145
4.04M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
230k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
230k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
230k
            if (have_bottomleft) {
150
60.2k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
561k
                for (int i = 0; i < px_have; i++)
153
501k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
60.2k
                if (px_have < sz)
155
878
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
170k
            } else {
157
170k
                pixel_set(left - sz, left[0], sz);
158
170k
            }
159
230k
        }
160
4.04M
    }
161
162
5.01M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.17M
        const int sz = tw << 2;
164
4.17M
        pixel *const top = &topleft_out[1];
165
166
4.17M
        if (have_top) {
167
3.98M
            const int px_have = imin(sz, (w - x) << 2);
168
3.98M
            pixel_copy(top, dst_top, px_have);
169
3.98M
            if (px_have < sz)
170
313k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
3.98M
        } else {
172
186k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
186k
        }
174
175
4.17M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
216k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
216k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
216k
            if (have_topright) {
180
127k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
127k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
127k
                if (px_have < sz)
184
4.52k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
4.52k
                              sz - px_have);
186
127k
            } else {
187
89.2k
                pixel_set(top + sz, top[sz - 1], sz);
188
89.2k
            }
189
216k
        }
190
4.17M
    }
191
192
5.01M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.55M
        if (have_left)
194
1.43M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
125k
        else
196
125k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.55M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
100k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
100k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.55M
    }
202
203
5.01M
    return mode;
204
5.01M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
4.99M
{
87
4.99M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
4.99M
    assert(y < h && x < w);
89
90
4.99M
    switch (mode) {
91
260k
    case VERT_PRED:
92
694k
    case HOR_PRED:
93
783k
    case DIAG_DOWN_LEFT_PRED:
94
868k
    case DIAG_DOWN_RIGHT_PRED:
95
938k
    case VERT_RIGHT_PRED:
96
1.04M
    case HOR_DOWN_PRED:
97
1.25M
    case HOR_UP_PRED:
98
1.36M
    case VERT_LEFT_PRED: {
99
1.36M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.36M
        if (*angle <= 90)
102
396k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
965k
        else if (*angle < 180)
104
430k
            mode = Z2_PRED;
105
534k
        else
106
534k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.36M
        break;
108
1.25M
    }
109
2.28M
    case DC_PRED:
110
2.71M
    case PAETH_PRED:
111
2.71M
        mode = av1_mode_conv[mode][have_left][have_top];
112
2.71M
        break;
113
955k
    default:
114
955k
        break;
115
4.99M
    }
116
117
4.98M
    const pixel *dst_top;
118
4.98M
    if (have_top &&
119
4.29M
        (av1_intra_prediction_edges[mode].needs_top ||
120
468k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
226k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.14M
    {
123
4.14M
        if (prefilter_toplevel_sb_edge) {
124
573k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.56M
        } else {
126
3.56M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.56M
        }
128
4.14M
    }
129
130
4.98M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
3.96M
        const int sz = th << 2;
132
3.96M
        pixel *const left = &topleft_out[-sz];
133
134
3.96M
        if (have_left) {
135
3.65M
            const int px_have = imin(sz, (h - y) << 2);
136
137
45.5M
            for (int i = 0; i < px_have; i++)
138
41.9M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
3.65M
            if (px_have < sz)
140
134k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
3.65M
        } else {
142
310k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
310k
        }
144
145
3.96M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
269k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
269k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
269k
            if (have_bottomleft) {
150
67.4k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
725k
                for (int i = 0; i < px_have; i++)
153
658k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
67.4k
                if (px_have < sz)
155
2.14k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
202k
            } else {
157
202k
                pixel_set(left - sz, left[0], sz);
158
202k
            }
159
269k
        }
160
3.96M
    }
161
162
4.98M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
3.99M
        const int sz = tw << 2;
164
3.99M
        pixel *const top = &topleft_out[1];
165
166
3.99M
        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
362k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
3.79M
        } else {
172
200k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
200k
        }
174
175
3.99M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
222k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
222k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
222k
            if (have_topright) {
180
128k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
128k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
128k
                if (px_have < sz)
184
5.31k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
5.31k
                              sz - px_have);
186
128k
            } else {
187
93.9k
                pixel_set(top + sz, top[sz - 1], sz);
188
93.9k
            }
189
222k
        }
190
3.99M
    }
191
192
4.98M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.43M
        if (have_left)
194
1.29M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
142k
        else
196
142k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.43M
        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.43M
    }
202
203
4.98M
    return mode;
204
4.99M
}