Coverage Report

Created: 2026-10-17 06:56

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.7M
{
87
10.7M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
10.7M
    assert(y < h && x < w);
89
90
10.7M
    switch (mode) {
91
523k
    case VERT_PRED:
92
1.29M
    case HOR_PRED:
93
1.47M
    case DIAG_DOWN_LEFT_PRED:
94
1.64M
    case DIAG_DOWN_RIGHT_PRED:
95
1.82M
    case VERT_RIGHT_PRED:
96
2.02M
    case HOR_DOWN_PRED:
97
2.42M
    case HOR_UP_PRED:
98
2.65M
    case VERT_LEFT_PRED: {
99
2.65M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.65M
        if (*angle <= 90)
102
798k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.85M
        else if (*angle < 180)
104
857k
            mode = Z2_PRED;
105
998k
        else
106
998k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.65M
        break;
108
2.42M
    }
109
5.32M
    case DC_PRED:
110
6.36M
    case PAETH_PRED:
111
6.36M
        mode = av1_mode_conv[mode][have_left][have_top];
112
6.36M
        break;
113
1.78M
    default:
114
1.78M
        break;
115
10.7M
    }
116
117
10.7M
    const pixel *dst_top;
118
10.7M
    if (have_top &&
119
9.11M
        (av1_intra_prediction_edges[mode].needs_top ||
120
849k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
432k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
8.85M
    {
123
8.85M
        if (prefilter_toplevel_sb_edge) {
124
1.59M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
7.25M
        } else {
126
7.25M
            dst_top = &dst[-PXSTRIDE(stride)];
127
7.25M
        }
128
8.85M
    }
129
130
10.7M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
8.07M
        const int sz = th << 2;
132
8.07M
        pixel *const left = &topleft_out[-sz];
133
134
8.07M
        if (have_left) {
135
7.34M
            const int px_have = imin(sz, (h - y) << 2);
136
137
104M
            for (int i = 0; i < px_have; i++)
138
96.8M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
7.34M
            if (px_have < sz)
140
375k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
7.34M
        } else {
142
726k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
726k
        }
144
145
8.07M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
480k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
480k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
480k
            if (have_bottomleft) {
150
111k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.30M
                for (int i = 0; i < px_have; i++)
153
1.19M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
111k
                if (px_have < sz)
155
5.85k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
368k
            } else {
157
368k
                pixel_set(left - sz, left[0], sz);
158
368k
            }
159
480k
        }
160
8.07M
    }
161
162
10.7M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
8.66M
        const int sz = tw << 2;
164
8.66M
        pixel *const top = &topleft_out[1];
165
166
8.66M
        if (have_top) {
167
8.21M
            const int px_have = imin(sz, (w - x) << 2);
168
8.21M
            pixel_copy(top, dst_top, px_have);
169
8.21M
            if (px_have < sz)
170
1.11M
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
8.21M
        } else {
172
457k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
457k
        }
174
175
8.66M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
458k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
458k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
458k
            if (have_topright) {
180
266k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
266k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
266k
                if (px_have < sz)
184
10.8k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
3.96k
                              sz - px_have);
186
266k
            } else {
187
192k
                pixel_set(top + sz, top[sz - 1], sz);
188
192k
            }
189
458k
        }
190
8.66M
    }
191
192
10.7M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.84M
        if (have_left)
194
2.51M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
327k
        else
196
327k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.84M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
258k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
258k
                            topleft_out[0] * 6 + 8) >> 4;
201
2.84M
    }
202
203
10.7M
    return mode;
204
10.7M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
5.06M
{
87
5.06M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.06M
    assert(y < h && x < w);
89
90
5.06M
    switch (mode) {
91
254k
    case VERT_PRED:
92
593k
    case HOR_PRED:
93
674k
    case DIAG_DOWN_LEFT_PRED:
94
751k
    case DIAG_DOWN_RIGHT_PRED:
95
841k
    case VERT_RIGHT_PRED:
96
929k
    case HOR_DOWN_PRED:
97
1.09M
    case HOR_UP_PRED:
98
1.19M
    case VERT_LEFT_PRED: {
99
1.19M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.19M
        if (*angle <= 90)
102
363k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
829k
        else if (*angle < 180)
104
401k
            mode = Z2_PRED;
105
427k
        else
106
427k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.19M
        break;
108
1.09M
    }
109
2.52M
    case DC_PRED:
110
3.09M
    case PAETH_PRED:
111
3.09M
        mode = av1_mode_conv[mode][have_left][have_top];
112
3.09M
        break;
113
792k
    default:
114
792k
        break;
115
5.06M
    }
116
117
5.06M
    const pixel *dst_top;
118
5.06M
    if (have_top &&
119
4.35M
        (av1_intra_prediction_edges[mode].needs_top ||
120
367k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
195k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.24M
    {
123
4.24M
        if (prefilter_toplevel_sb_edge) {
124
820k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.42M
        } else {
126
3.42M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.42M
        }
128
4.24M
    }
129
130
5.06M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
3.73M
        const int sz = th << 2;
132
3.73M
        pixel *const left = &topleft_out[-sz];
133
134
3.73M
        if (have_left) {
135
3.37M
            const int px_have = imin(sz, (h - y) << 2);
136
137
47.1M
            for (int i = 0; i < px_have; i++)
138
43.7M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
3.37M
            if (px_have < sz)
140
158k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
3.37M
        } else {
142
356k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
356k
        }
144
145
3.73M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
201k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
201k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
201k
            if (have_bottomleft) {
150
46.0k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
493k
                for (int i = 0; i < px_have; i++)
153
447k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
46.0k
                if (px_have < sz)
155
1.17k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
155k
            } else {
157
155k
                pixel_set(left - sz, left[0], sz);
158
155k
            }
159
201k
        }
160
3.73M
    }
161
162
5.06M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.19M
        const int sz = tw << 2;
164
4.19M
        pixel *const top = &topleft_out[1];
165
166
4.19M
        if (have_top) {
167
3.99M
            const int px_have = imin(sz, (w - x) << 2);
168
3.99M
            pixel_copy(top, dst_top, px_have);
169
3.99M
            if (px_have < sz)
170
524k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
3.99M
        } else {
172
207k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
207k
        }
174
175
4.19M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
199k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
199k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
199k
            if (have_topright) {
180
109k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
109k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
109k
                if (px_have < sz)
184
3.96k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
3.96k
                              sz - px_have);
186
109k
            } else {
187
89.7k
                pixel_set(top + sz, top[sz - 1], sz);
188
89.7k
            }
189
199k
        }
190
4.19M
    }
191
192
5.06M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.35M
        if (have_left)
194
1.19M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
158k
        else
196
158k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.35M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
102k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
102k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.35M
    }
202
203
5.06M
    return mode;
204
5.06M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
5.65M
{
87
5.65M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
5.65M
    assert(y < h && x < w);
89
90
5.65M
    switch (mode) {
91
269k
    case VERT_PRED:
92
700k
    case HOR_PRED:
93
804k
    case DIAG_DOWN_LEFT_PRED:
94
893k
    case DIAG_DOWN_RIGHT_PRED:
95
987k
    case VERT_RIGHT_PRED:
96
1.09M
    case HOR_DOWN_PRED:
97
1.32M
    case HOR_UP_PRED:
98
1.46M
    case VERT_LEFT_PRED: {
99
1.46M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
1.46M
        if (*angle <= 90)
102
434k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.02M
        else if (*angle < 180)
104
455k
            mode = Z2_PRED;
105
570k
        else
106
570k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
1.46M
        break;
108
1.32M
    }
109
2.80M
    case DC_PRED:
110
3.27M
    case PAETH_PRED:
111
3.27M
        mode = av1_mode_conv[mode][have_left][have_top];
112
3.27M
        break;
113
988k
    default:
114
988k
        break;
115
5.65M
    }
116
117
5.63M
    const pixel *dst_top;
118
5.63M
    if (have_top &&
119
4.75M
        (av1_intra_prediction_edges[mode].needs_top ||
120
481k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
236k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
4.61M
    {
123
4.61M
        if (prefilter_toplevel_sb_edge) {
124
776k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
3.83M
        } else {
126
3.83M
            dst_top = &dst[-PXSTRIDE(stride)];
127
3.83M
        }
128
4.61M
    }
129
130
5.63M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
4.33M
        const int sz = th << 2;
132
4.33M
        pixel *const left = &topleft_out[-sz];
133
134
4.33M
        if (have_left) {
135
3.96M
            const int px_have = imin(sz, (h - y) << 2);
136
137
57.0M
            for (int i = 0; i < px_have; i++)
138
53.1M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
3.96M
            if (px_have < sz)
140
216k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
3.96M
        } else {
142
370k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
370k
        }
144
145
4.33M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
279k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
279k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
279k
            if (have_bottomleft) {
150
65.9k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
813k
                for (int i = 0; i < px_have; i++)
153
747k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
65.9k
                if (px_have < sz)
155
4.67k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
213k
            } else {
157
213k
                pixel_set(left - sz, left[0], sz);
158
213k
            }
159
279k
        }
160
4.33M
    }
161
162
5.63M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
4.47M
        const int sz = tw << 2;
164
4.47M
        pixel *const top = &topleft_out[1];
165
166
4.47M
        if (have_top) {
167
4.22M
            const int px_have = imin(sz, (w - x) << 2);
168
4.22M
            pixel_copy(top, dst_top, px_have);
169
4.22M
            if (px_have < sz)
170
586k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
4.22M
        } else {
172
249k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
249k
        }
174
175
4.47M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
259k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
259k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
259k
            if (have_topright) {
180
156k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
156k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
156k
                if (px_have < sz)
184
6.89k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
6.89k
                              sz - px_have);
186
156k
            } else {
187
102k
                pixel_set(top + sz, top[sz - 1], sz);
188
102k
            }
189
259k
        }
190
4.47M
    }
191
192
5.63M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
1.48M
        if (have_left)
194
1.31M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
168k
        else
196
168k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
1.48M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
155k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
155k
                            topleft_out[0] * 6 + 8) >> 4;
201
1.48M
    }
202
203
5.63M
    return mode;
204
5.65M
}