Coverage Report

Created: 2026-10-28 06:18

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
15.2M
{
87
15.2M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
15.2M
    assert(y < h && x < w);
89
90
15.2M
    switch (mode) {
91
668k
    case VERT_PRED:
92
1.80M
    case HOR_PRED:
93
2.04M
    case DIAG_DOWN_LEFT_PRED:
94
2.57M
    case DIAG_DOWN_RIGHT_PRED:
95
2.85M
    case VERT_RIGHT_PRED:
96
3.12M
    case HOR_DOWN_PRED:
97
3.70M
    case HOR_UP_PRED:
98
4.02M
    case VERT_LEFT_PRED: {
99
4.02M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
4.02M
        if (*angle <= 90)
102
1.04M
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
2.97M
        else if (*angle < 180)
104
1.56M
            mode = Z2_PRED;
105
1.41M
        else
106
1.41M
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
4.02M
        break;
108
3.70M
    }
109
6.41M
    case DC_PRED:
110
7.88M
    case PAETH_PRED:
111
7.88M
        mode = av1_mode_conv[mode][have_left][have_top];
112
7.88M
        break;
113
3.41M
    default:
114
3.41M
        break;
115
15.2M
    }
116
117
15.2M
    const pixel *dst_top;
118
15.2M
    if (have_top &&
119
13.6M
        (av1_intra_prediction_edges[mode].needs_top ||
120
1.26M
         av1_intra_prediction_edges[mode].needs_topleft ||
121
654k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
13.2M
    {
123
13.2M
        if (prefilter_toplevel_sb_edge) {
124
1.95M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
11.2M
        } else {
126
11.2M
            dst_top = &dst[-PXSTRIDE(stride)];
127
11.2M
        }
128
13.2M
    }
129
130
15.2M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
12.2M
        const int sz = th << 2;
132
12.2M
        pixel *const left = &topleft_out[-sz];
133
134
12.2M
        if (have_left) {
135
11.2M
            const int px_have = imin(sz, (h - y) << 2);
136
137
130M
            for (int i = 0; i < px_have; i++)
138
119M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
11.2M
            if (px_have < sz)
140
305k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
11.2M
        } else {
142
1.07M
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
1.07M
        }
144
145
12.2M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
688k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
688k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
688k
            if (have_bottomleft) {
150
167k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
1.76M
                for (int i = 0; i < px_have; i++)
153
1.60M
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
167k
                if (px_have < sz)
155
2.27k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
521k
            } else {
157
521k
                pixel_set(left - sz, left[0], sz);
158
521k
            }
159
688k
        }
160
12.2M
    }
161
162
15.2M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
12.8M
        const int sz = tw << 2;
164
12.8M
        pixel *const top = &topleft_out[1];
165
166
12.8M
        if (have_top) {
167
12.3M
            const int px_have = imin(sz, (w - x) << 2);
168
12.3M
            pixel_copy(top, dst_top, px_have);
169
12.3M
            if (px_have < sz)
170
1.08M
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
12.3M
        } else {
172
527k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
527k
        }
174
175
12.8M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
623k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
623k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
623k
            if (have_topright) {
180
332k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
332k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
332k
                if (px_have < sz)
184
18.3k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
8.74k
                              sz - px_have);
186
332k
            } else {
187
290k
                pixel_set(top + sz, top[sz - 1], sz);
188
290k
            }
189
623k
        }
190
12.8M
    }
191
192
15.2M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
4.41M
        if (have_left)
194
3.92M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
495k
        else
196
495k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
4.41M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
262k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
262k
                            topleft_out[0] * 6 + 8) >> 4;
201
4.41M
    }
202
203
15.2M
    return mode;
204
15.2M
}
dav1d_prepare_intra_edges_8bpc
Line
Count
Source
86
8.34M
{
87
8.34M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
8.34M
    assert(y < h && x < w);
89
90
8.34M
    switch (mode) {
91
329k
    case VERT_PRED:
92
857k
    case HOR_PRED:
93
973k
    case DIAG_DOWN_LEFT_PRED:
94
1.36M
    case DIAG_DOWN_RIGHT_PRED:
95
1.45M
    case VERT_RIGHT_PRED:
96
1.57M
    case HOR_DOWN_PRED:
97
1.87M
    case HOR_UP_PRED:
98
2.02M
    case VERT_LEFT_PRED: {
99
2.02M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.02M
        if (*angle <= 90)
102
514k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.51M
        else if (*angle < 180)
104
821k
            mode = Z2_PRED;
105
688k
        else
106
688k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.02M
        break;
108
1.87M
    }
109
3.50M
    case DC_PRED:
110
4.43M
    case PAETH_PRED:
111
4.43M
        mode = av1_mode_conv[mode][have_left][have_top];
112
4.43M
        break;
113
1.93M
    default:
114
1.93M
        break;
115
8.34M
    }
116
117
8.34M
    const pixel *dst_top;
118
8.34M
    if (have_top &&
119
7.59M
        (av1_intra_prediction_edges[mode].needs_top ||
120
622k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
315k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
7.40M
    {
123
7.40M
        if (prefilter_toplevel_sb_edge) {
124
1.08M
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
6.32M
        } else {
126
6.32M
            dst_top = &dst[-PXSTRIDE(stride)];
127
6.32M
        }
128
7.40M
    }
129
130
8.34M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
6.77M
        const int sz = th << 2;
132
6.77M
        pixel *const left = &topleft_out[-sz];
133
134
6.77M
        if (have_left) {
135
6.19M
            const int px_have = imin(sz, (h - y) << 2);
136
137
66.3M
            for (int i = 0; i < px_have; i++)
138
60.1M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
6.19M
            if (px_have < sz)
140
135k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
6.19M
        } else {
142
573k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
573k
        }
144
145
6.77M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
339k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
339k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
339k
            if (have_bottomleft) {
150
78.0k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
770k
                for (int i = 0; i < px_have; i++)
153
692k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
78.0k
                if (px_have < sz)
155
1.03k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
261k
            } else {
157
261k
                pixel_set(left - sz, left[0], sz);
158
261k
            }
159
339k
        }
160
6.77M
    }
161
162
8.34M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
7.22M
        const int sz = tw << 2;
164
7.22M
        pixel *const top = &topleft_out[1];
165
166
7.22M
        if (have_top) {
167
6.96M
            const int px_have = imin(sz, (w - x) << 2);
168
6.96M
            pixel_copy(top, dst_top, px_have);
169
6.96M
            if (px_have < sz)
170
560k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
6.96M
        } else {
172
261k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
261k
        }
174
175
7.22M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
300k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
300k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
300k
            if (have_topright) {
180
157k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
157k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
157k
                if (px_have < sz)
184
8.74k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
8.74k
                              sz - px_have);
186
157k
            } else {
187
143k
                pixel_set(top + sz, top[sz - 1], sz);
188
143k
            }
189
300k
        }
190
7.22M
    }
191
192
8.34M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.39M
        if (have_left)
194
2.13M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
261k
        else
196
261k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.39M
        if (mode == Z2_PRED && tw + th >= 6 && filter_edge)
199
107k
            *topleft_out = ((topleft_out[-1] + topleft_out[1]) * 5 +
200
107k
                            topleft_out[0] * 6 + 8) >> 4;
201
2.39M
    }
202
203
8.34M
    return mode;
204
8.34M
}
dav1d_prepare_intra_edges_16bpc
Line
Count
Source
86
6.91M
{
87
6.91M
    const int bitdepth = bitdepth_from_max(bitdepth_max);
88
6.91M
    assert(y < h && x < w);
89
90
6.91M
    switch (mode) {
91
338k
    case VERT_PRED:
92
951k
    case HOR_PRED:
93
1.07M
    case DIAG_DOWN_LEFT_PRED:
94
1.21M
    case DIAG_DOWN_RIGHT_PRED:
95
1.39M
    case VERT_RIGHT_PRED:
96
1.54M
    case HOR_DOWN_PRED:
97
1.83M
    case HOR_UP_PRED:
98
2.00M
    case VERT_LEFT_PRED: {
99
2.00M
        *angle = av1_mode_to_angle_map[mode - VERT_PRED] + 3 * *angle;
100
101
2.00M
        if (*angle <= 90)
102
534k
            mode = *angle < 90 && have_top ? Z1_PRED : VERT_PRED;
103
1.46M
        else if (*angle < 180)
104
741k
            mode = Z2_PRED;
105
724k
        else
106
724k
            mode = *angle > 180 && have_left ? Z3_PRED : HOR_PRED;
107
2.00M
        break;
108
1.83M
    }
109
2.91M
    case DC_PRED:
110
3.44M
    case PAETH_PRED:
111
3.44M
        mode = av1_mode_conv[mode][have_left][have_top];
112
3.44M
        break;
113
1.48M
    default:
114
1.48M
        break;
115
6.91M
    }
116
117
6.90M
    const pixel *dst_top;
118
6.90M
    if (have_top &&
119
6.06M
        (av1_intra_prediction_edges[mode].needs_top ||
120
643k
         av1_intra_prediction_edges[mode].needs_topleft ||
121
339k
         (av1_intra_prediction_edges[mode].needs_left && !have_left)))
122
5.84M
    {
123
5.84M
        if (prefilter_toplevel_sb_edge) {
124
873k
            dst_top = &prefilter_toplevel_sb_edge[x * 4];
125
4.96M
        } else {
126
4.96M
            dst_top = &dst[-PXSTRIDE(stride)];
127
4.96M
        }
128
5.84M
    }
129
130
6.90M
    if (av1_intra_prediction_edges[mode].needs_left) {
131
5.51M
        const int sz = th << 2;
132
5.51M
        pixel *const left = &topleft_out[-sz];
133
134
5.51M
        if (have_left) {
135
5.01M
            const int px_have = imin(sz, (h - y) << 2);
136
137
63.9M
            for (int i = 0; i < px_have; i++)
138
58.9M
                left[sz - 1 - i] = dst[PXSTRIDE(stride) * i - 1];
139
5.01M
            if (px_have < sz)
140
170k
                pixel_set(left, left[sz - px_have], sz - px_have);
141
5.01M
        } else {
142
505k
            pixel_set(left, have_top ? *dst_top : ((1 << bitdepth) >> 1) + 1, sz);
143
505k
        }
144
145
5.51M
        if (av1_intra_prediction_edges[mode].needs_bottomleft) {
146
348k
            const int have_bottomleft = (!have_left || y + th >= h) ? 0 :
147
348k
                                        (edge_flags & EDGE_I444_LEFT_HAS_BOTTOM);
148
149
348k
            if (have_bottomleft) {
150
89.0k
                const int px_have = imin(sz, (h - y - th) << 2);
151
152
999k
                for (int i = 0; i < px_have; i++)
153
910k
                    left[-(i + 1)] = dst[(sz + i) * PXSTRIDE(stride) - 1];
154
89.0k
                if (px_have < sz)
155
1.24k
                    pixel_set(left - sz, left[-px_have], sz - px_have);
156
259k
            } else {
157
259k
                pixel_set(left - sz, left[0], sz);
158
259k
            }
159
348k
        }
160
5.51M
    }
161
162
6.90M
    if (av1_intra_prediction_edges[mode].needs_top) {
163
5.66M
        const int sz = tw << 2;
164
5.66M
        pixel *const top = &topleft_out[1];
165
166
5.66M
        if (have_top) {
167
5.39M
            const int px_have = imin(sz, (w - x) << 2);
168
5.39M
            pixel_copy(top, dst_top, px_have);
169
5.39M
            if (px_have < sz)
170
528k
                pixel_set(top + px_have, top[px_have - 1], sz - px_have);
171
5.39M
        } else {
172
265k
            pixel_set(top, have_left ? dst[-1] : ((1 << bitdepth) >> 1) - 1, sz);
173
265k
        }
174
175
5.66M
        if (av1_intra_prediction_edges[mode].needs_topright) {
176
322k
            const int have_topright = (!have_top || x + tw >= w) ? 0 :
177
322k
                                      (edge_flags & EDGE_I444_TOP_HAS_RIGHT);
178
179
322k
            if (have_topright) {
180
175k
                const int px_have = imin(sz, (w - x - tw) << 2);
181
182
175k
                pixel_copy(top + sz, &dst_top[sz], px_have);
183
175k
                if (px_have < sz)
184
9.62k
                    pixel_set(top + sz + px_have, top[sz + px_have - 1],
185
9.62k
                              sz - px_have);
186
175k
            } else {
187
147k
                pixel_set(top + sz, top[sz - 1], sz);
188
147k
            }
189
322k
        }
190
5.66M
    }
191
192
6.90M
    if (av1_intra_prediction_edges[mode].needs_topleft) {
193
2.02M
        if (have_left)
194
1.79M
            *topleft_out = have_top ? dst_top[-1] : dst[-1];
195
234k
        else
196
234k
            *topleft_out = have_top ? *dst_top : (1 << bitdepth) >> 1;
197
198
2.02M
        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
2.02M
    }
202
203
6.90M
    return mode;
204
6.91M
}