Coverage Report

Created: 2026-12-01 07:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/dav1d/src/fg_apply_tmpl.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, Niklas Haas
3
 * Copyright © 2018, VideoLAN and dav1d authors
4
 * Copyright © 2018, Two Orioles, LLC
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright notice, this
11
 *    list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright notice,
14
 *    this list of conditions and the following disclaimer in the documentation
15
 *    and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <stdint.h>
32
33
#include "dav1d/common.h"
34
#include "dav1d/picture.h"
35
36
#include "common/intops.h"
37
#include "common/bitdepth.h"
38
39
#include "src/fg_apply.h"
40
41
static void generate_scaling(const int bitdepth,
42
                             const uint8_t points[][2], const int num,
43
                             uint8_t scaling[SCALING_SIZE])
44
750
{
45
750
#if BITDEPTH == 8
46
750
    const int shift_x = 0;
47
750
    const int scaling_size = SCALING_SIZE;
48
#else
49
    assert(bitdepth > 8);
50
    const int shift_x = bitdepth - 8;
51
    const int scaling_size = 1 << bitdepth;
52
#endif
53
54
750
    if (num == 0) {
55
98
        memset(scaling, 0, scaling_size);
56
98
        return;
57
98
    }
58
59
    // Fill up the preceding entries with the initial value
60
652
    memset(scaling, points[0][1], points[0][0] << shift_x);
61
62
    // Linearly interpolate the values in the middle
63
1.30k
    for (int i = 0; i < num - 1; i++) {
64
649
        const int bx = points[i][0];
65
649
        const int by = points[i][1];
66
649
        const int ex = points[i+1][0];
67
649
        const int ey = points[i+1][1];
68
649
        const int dx = ex - bx;
69
649
        const int dy = ey - by;
70
649
        assert(dx > 0);
71
649
        const int delta = dy * ((0x10000 + (dx >> 1)) / dx);
72
26.7k
        for (int x = 0, d = 0x8000; x < dx; x++) {
73
26.0k
            scaling[(bx + x) << shift_x] = by + (d >> 16);
74
26.0k
            d += delta;
75
26.0k
        }
76
649
    }
77
78
    // Fill up the remaining entries with the final value
79
652
    const int n = points[num - 1][0] << shift_x;
80
652
    memset(&scaling[n], points[num - 1][1], scaling_size - n);
81
82
#if BITDEPTH != 8
83
    const int pad = 1 << shift_x, rnd = pad >> 1;
84
    for (int i = 0; i < num - 1; i++) {
85
        const int bx = points[i][0] << shift_x;
86
        const int ex = points[i+1][0] << shift_x;
87
        const int dx = ex - bx;
88
        for (int x = 0; x < dx; x += pad) {
89
            const int range = scaling[bx + x + pad] - scaling[bx + x];
90
            for (int n = 1, r = rnd; n < pad; n++) {
91
                r += range;
92
                scaling[bx + x + n] = scaling[bx + x] + (r >> shift_x);
93
            }
94
        }
95
    }
96
#endif
97
652
}
98
99
#ifndef UNIT_TEST
100
void bitfn(dav1d_prep_grain)(const Dav1dFilmGrainDSPContext *const dsp,
101
                             Dav1dPicture *const out,
102
                             const Dav1dPicture *const in,
103
                             uint8_t scaling[3][SCALING_SIZE],
104
                             entry grain_lut[3][GRAIN_HEIGHT+1][GRAIN_WIDTH])
105
1.21k
{
106
1.21k
    const Dav1dFilmGrainData *const data = &out->frame_hdr->film_grain.data;
107
#if BITDEPTH != 8
108
    const int bitdepth_max = (1 << out->p.bpc) - 1;
109
#endif
110
111
    // Generate grain LUTs as needed
112
1.21k
    dsp->generate_grain_y(grain_lut[0], data HIGHBD_TAIL_SUFFIX); // always needed
113
1.21k
    if (data->num_uv_points[0] || data->chroma_scaling_from_luma)
114
791
        dsp->generate_grain_uv[in->p.layout - 1](grain_lut[1], grain_lut[0],
115
791
                                                 data, 0 HIGHBD_TAIL_SUFFIX);
116
1.21k
    if (data->num_uv_points[1] || data->chroma_scaling_from_luma)
117
807
        dsp->generate_grain_uv[in->p.layout - 1](grain_lut[2], grain_lut[0],
118
807
                                                 data, 1 HIGHBD_TAIL_SUFFIX);
119
120
    // Generate scaling LUTs as needed
121
1.21k
    if (data->num_y_points || data->chroma_scaling_from_luma)
122
1.09k
        generate_scaling(in->p.bpc, data->y_points, data->num_y_points, scaling[0]);
123
1.21k
    if (data->num_uv_points[0])
124
289
        generate_scaling(in->p.bpc, data->uv_points[0], data->num_uv_points[0], scaling[1]);
125
1.21k
    if (data->num_uv_points[1])
126
305
        generate_scaling(in->p.bpc, data->uv_points[1], data->num_uv_points[1], scaling[2]);
127
128
    // Copy over the non-modified planes
129
1.21k
    assert(out->stride[0] == in->stride[0]);
130
1.21k
    if (!data->num_y_points) {
131
333
        const ptrdiff_t stride = out->stride[0];
132
333
        const ptrdiff_t sz = out->p.h * stride;
133
333
        if (sz < 0)
134
0
            memcpy((uint8_t*) out->data[0] + sz - stride,
135
0
                   (uint8_t*) in->data[0] + sz - stride, -sz);
136
333
        else
137
333
            memcpy(out->data[0], in->data[0], sz);
138
333
    }
139
140
1.21k
    if (in->p.layout != DAV1D_PIXEL_LAYOUT_I400 && !data->chroma_scaling_from_luma) {
141
423
        assert(out->stride[1] == in->stride[1]);
142
423
        const int ss_ver = in->p.layout == DAV1D_PIXEL_LAYOUT_I420;
143
423
        const ptrdiff_t stride = out->stride[1];
144
423
        const ptrdiff_t sz = ((out->p.h + ss_ver) >> ss_ver) * stride;
145
423
        if (sz < 0) {
146
0
            if (!data->num_uv_points[0])
147
0
                memcpy((uint8_t*) out->data[1] + sz - stride,
148
0
                       (uint8_t*) in->data[1] + sz - stride, -sz);
149
0
            if (!data->num_uv_points[1])
150
0
                memcpy((uint8_t*) out->data[2] + sz - stride,
151
0
                       (uint8_t*) in->data[2] + sz - stride, -sz);
152
423
        } else {
153
423
            if (!data->num_uv_points[0])
154
134
                memcpy(out->data[1], in->data[1], sz);
155
423
            if (!data->num_uv_points[1])
156
118
                memcpy(out->data[2], in->data[2], sz);
157
423
        }
158
423
    }
159
1.21k
}
dav1d_prep_grain_8bpc
Line
Count
Source
105
564
{
106
564
    const Dav1dFilmGrainData *const data = &out->frame_hdr->film_grain.data;
107
#if BITDEPTH != 8
108
    const int bitdepth_max = (1 << out->p.bpc) - 1;
109
#endif
110
111
    // Generate grain LUTs as needed
112
564
    dsp->generate_grain_y(grain_lut[0], data HIGHBD_TAIL_SUFFIX); // always needed
113
564
    if (data->num_uv_points[0] || data->chroma_scaling_from_luma)
114
342
        dsp->generate_grain_uv[in->p.layout - 1](grain_lut[1], grain_lut[0],
115
342
                                                 data, 0 HIGHBD_TAIL_SUFFIX);
116
564
    if (data->num_uv_points[1] || data->chroma_scaling_from_luma)
117
349
        dsp->generate_grain_uv[in->p.layout - 1](grain_lut[2], grain_lut[0],
118
349
                                                 data, 1 HIGHBD_TAIL_SUFFIX);
119
120
    // Generate scaling LUTs as needed
121
564
    if (data->num_y_points || data->chroma_scaling_from_luma)
122
485
        generate_scaling(in->p.bpc, data->y_points, data->num_y_points, scaling[0]);
123
564
    if (data->num_uv_points[0])
124
129
        generate_scaling(in->p.bpc, data->uv_points[0], data->num_uv_points[0], scaling[1]);
125
564
    if (data->num_uv_points[1])
126
136
        generate_scaling(in->p.bpc, data->uv_points[1], data->num_uv_points[1], scaling[2]);
127
128
    // Copy over the non-modified planes
129
564
    assert(out->stride[0] == in->stride[0]);
130
564
    if (!data->num_y_points) {
131
177
        const ptrdiff_t stride = out->stride[0];
132
177
        const ptrdiff_t sz = out->p.h * stride;
133
177
        if (sz < 0)
134
0
            memcpy((uint8_t*) out->data[0] + sz - stride,
135
0
                   (uint8_t*) in->data[0] + sz - stride, -sz);
136
177
        else
137
177
            memcpy(out->data[0], in->data[0], sz);
138
177
    }
139
140
564
    if (in->p.layout != DAV1D_PIXEL_LAYOUT_I400 && !data->chroma_scaling_from_luma) {
141
197
        assert(out->stride[1] == in->stride[1]);
142
197
        const int ss_ver = in->p.layout == DAV1D_PIXEL_LAYOUT_I420;
143
197
        const ptrdiff_t stride = out->stride[1];
144
197
        const ptrdiff_t sz = ((out->p.h + ss_ver) >> ss_ver) * stride;
145
197
        if (sz < 0) {
146
0
            if (!data->num_uv_points[0])
147
0
                memcpy((uint8_t*) out->data[1] + sz - stride,
148
0
                       (uint8_t*) in->data[1] + sz - stride, -sz);
149
0
            if (!data->num_uv_points[1])
150
0
                memcpy((uint8_t*) out->data[2] + sz - stride,
151
0
                       (uint8_t*) in->data[2] + sz - stride, -sz);
152
197
        } else {
153
197
            if (!data->num_uv_points[0])
154
68
                memcpy(out->data[1], in->data[1], sz);
155
197
            if (!data->num_uv_points[1])
156
61
                memcpy(out->data[2], in->data[2], sz);
157
197
        }
158
197
    }
159
564
}
dav1d_prep_grain_16bpc
Line
Count
Source
105
654
{
106
654
    const Dav1dFilmGrainData *const data = &out->frame_hdr->film_grain.data;
107
654
#if BITDEPTH != 8
108
654
    const int bitdepth_max = (1 << out->p.bpc) - 1;
109
654
#endif
110
111
    // Generate grain LUTs as needed
112
654
    dsp->generate_grain_y(grain_lut[0], data HIGHBD_TAIL_SUFFIX); // always needed
113
654
    if (data->num_uv_points[0] || data->chroma_scaling_from_luma)
114
449
        dsp->generate_grain_uv[in->p.layout - 1](grain_lut[1], grain_lut[0],
115
449
                                                 data, 0 HIGHBD_TAIL_SUFFIX);
116
654
    if (data->num_uv_points[1] || data->chroma_scaling_from_luma)
117
458
        dsp->generate_grain_uv[in->p.layout - 1](grain_lut[2], grain_lut[0],
118
458
                                                 data, 1 HIGHBD_TAIL_SUFFIX);
119
120
    // Generate scaling LUTs as needed
121
654
    if (data->num_y_points || data->chroma_scaling_from_luma)
122
606
        generate_scaling(in->p.bpc, data->y_points, data->num_y_points, scaling[0]);
123
654
    if (data->num_uv_points[0])
124
160
        generate_scaling(in->p.bpc, data->uv_points[0], data->num_uv_points[0], scaling[1]);
125
654
    if (data->num_uv_points[1])
126
169
        generate_scaling(in->p.bpc, data->uv_points[1], data->num_uv_points[1], scaling[2]);
127
128
    // Copy over the non-modified planes
129
654
    assert(out->stride[0] == in->stride[0]);
130
654
    if (!data->num_y_points) {
131
156
        const ptrdiff_t stride = out->stride[0];
132
156
        const ptrdiff_t sz = out->p.h * stride;
133
156
        if (sz < 0)
134
0
            memcpy((uint8_t*) out->data[0] + sz - stride,
135
0
                   (uint8_t*) in->data[0] + sz - stride, -sz);
136
156
        else
137
156
            memcpy(out->data[0], in->data[0], sz);
138
156
    }
139
140
654
    if (in->p.layout != DAV1D_PIXEL_LAYOUT_I400 && !data->chroma_scaling_from_luma) {
141
226
        assert(out->stride[1] == in->stride[1]);
142
226
        const int ss_ver = in->p.layout == DAV1D_PIXEL_LAYOUT_I420;
143
226
        const ptrdiff_t stride = out->stride[1];
144
226
        const ptrdiff_t sz = ((out->p.h + ss_ver) >> ss_ver) * stride;
145
226
        if (sz < 0) {
146
0
            if (!data->num_uv_points[0])
147
0
                memcpy((uint8_t*) out->data[1] + sz - stride,
148
0
                       (uint8_t*) in->data[1] + sz - stride, -sz);
149
0
            if (!data->num_uv_points[1])
150
0
                memcpy((uint8_t*) out->data[2] + sz - stride,
151
0
                       (uint8_t*) in->data[2] + sz - stride, -sz);
152
226
        } else {
153
226
            if (!data->num_uv_points[0])
154
66
                memcpy(out->data[1], in->data[1], sz);
155
226
            if (!data->num_uv_points[1])
156
57
                memcpy(out->data[2], in->data[2], sz);
157
226
        }
158
226
    }
159
654
}
160
161
void bitfn(dav1d_apply_grain_row)(const Dav1dFilmGrainDSPContext *const dsp,
162
                                  Dav1dPicture *const out,
163
                                  const Dav1dPicture *const in,
164
                                  const uint8_t scaling[3][SCALING_SIZE],
165
                                  const entry grain_lut[3][GRAIN_HEIGHT+1][GRAIN_WIDTH],
166
                                  const int row)
167
49.0k
{
168
    // Synthesize grain for the affected planes
169
49.0k
    const Dav1dFilmGrainData *const data = &out->frame_hdr->film_grain.data;
170
49.0k
    const int ss_y = in->p.layout == DAV1D_PIXEL_LAYOUT_I420;
171
49.0k
    const int ss_x = in->p.layout != DAV1D_PIXEL_LAYOUT_I444;
172
49.0k
    const int cpw = (out->p.w + ss_x) >> ss_x;
173
49.0k
    const int is_id = out->seq_hdr->mtrx == DAV1D_MC_IDENTITY;
174
49.0k
    pixel *const luma_src =
175
49.0k
        ((pixel *) in->data[0]) + row * FG_BLOCK_SIZE * PXSTRIDE(in->stride[0]);
176
#if BITDEPTH != 8
177
    const int bitdepth_max = (1 << out->p.bpc) - 1;
178
#endif
179
180
49.0k
    if (data->num_y_points) {
181
37.2k
        const int bh = imin(out->p.h - row * FG_BLOCK_SIZE, FG_BLOCK_SIZE);
182
37.2k
        dsp->fgy_32x32xn(((pixel *) out->data[0]) + row * FG_BLOCK_SIZE * PXSTRIDE(out->stride[0]),
183
37.2k
                         luma_src, out->stride[0], data,
184
37.2k
                         out->p.w, scaling[0], grain_lut[0], bh, row HIGHBD_TAIL_SUFFIX);
185
37.2k
    }
186
187
49.0k
    if (!data->num_uv_points[0] && !data->num_uv_points[1] &&
188
23.4k
        !data->chroma_scaling_from_luma)
189
3.17k
    {
190
3.17k
        return;
191
3.17k
    }
192
193
45.8k
    const int bh = (imin(out->p.h - row * FG_BLOCK_SIZE, FG_BLOCK_SIZE) + ss_y) >> ss_y;
194
195
    // extend padding pixels
196
45.8k
    if (out->p.w & ss_x) {
197
29.3k
        pixel *ptr = luma_src;
198
239k
        for (int y = 0; y < bh; y++) {
199
210k
            ptr[out->p.w] = ptr[out->p.w - 1];
200
210k
            ptr += PXSTRIDE(in->stride[0]) << ss_y;
201
210k
        }
202
29.3k
    }
203
204
45.8k
    const ptrdiff_t uv_off = row * FG_BLOCK_SIZE * PXSTRIDE(out->stride[1]) >> ss_y;
205
45.8k
    if (data->chroma_scaling_from_luma) {
206
42.3k
        for (int pl = 0; pl < 2; pl++)
207
28.2k
            dsp->fguv_32x32xn[in->p.layout - 1](((pixel *) out->data[1 + pl]) + uv_off,
208
28.2k
                                                ((const pixel *) in->data[1 + pl]) + uv_off,
209
28.2k
                                                in->stride[1], data, cpw,
210
28.2k
                                                scaling[0], grain_lut[1 + pl],
211
28.2k
                                                bh, row, luma_src, in->stride[0],
212
28.2k
                                                pl, is_id HIGHBD_TAIL_SUFFIX);
213
31.8k
    } else {
214
55.0k
        for (int pl = 0; pl < 2; pl++)
215
23.2k
            if (data->num_uv_points[pl])
216
20.7k
                dsp->fguv_32x32xn[in->p.layout - 1](((pixel *) out->data[1 + pl]) + uv_off,
217
20.7k
                                                    ((const pixel *) in->data[1 + pl]) + uv_off,
218
20.7k
                                                    in->stride[1], data, cpw,
219
20.7k
                                                    scaling[1 + pl], grain_lut[1 + pl],
220
20.7k
                                                    bh, row, luma_src, in->stride[0],
221
20.7k
                                                    pl, is_id HIGHBD_TAIL_SUFFIX);
222
31.8k
    }
223
45.8k
}
dav1d_apply_grain_row_8bpc
Line
Count
Source
167
5.46k
{
168
    // Synthesize grain for the affected planes
169
5.46k
    const Dav1dFilmGrainData *const data = &out->frame_hdr->film_grain.data;
170
5.46k
    const int ss_y = in->p.layout == DAV1D_PIXEL_LAYOUT_I420;
171
5.46k
    const int ss_x = in->p.layout != DAV1D_PIXEL_LAYOUT_I444;
172
5.46k
    const int cpw = (out->p.w + ss_x) >> ss_x;
173
5.46k
    const int is_id = out->seq_hdr->mtrx == DAV1D_MC_IDENTITY;
174
5.46k
    pixel *const luma_src =
175
5.46k
        ((pixel *) in->data[0]) + row * FG_BLOCK_SIZE * PXSTRIDE(in->stride[0]);
176
#if BITDEPTH != 8
177
    const int bitdepth_max = (1 << out->p.bpc) - 1;
178
#endif
179
180
5.46k
    if (data->num_y_points) {
181
3.33k
        const int bh = imin(out->p.h - row * FG_BLOCK_SIZE, FG_BLOCK_SIZE);
182
3.33k
        dsp->fgy_32x32xn(((pixel *) out->data[0]) + row * FG_BLOCK_SIZE * PXSTRIDE(out->stride[0]),
183
3.33k
                         luma_src, out->stride[0], data,
184
3.33k
                         out->p.w, scaling[0], grain_lut[0], bh, row HIGHBD_TAIL_SUFFIX);
185
3.33k
    }
186
187
5.46k
    if (!data->num_uv_points[0] && !data->num_uv_points[1] &&
188
3.88k
        !data->chroma_scaling_from_luma)
189
1.51k
    {
190
1.51k
        return;
191
1.51k
    }
192
193
3.94k
    const int bh = (imin(out->p.h - row * FG_BLOCK_SIZE, FG_BLOCK_SIZE) + ss_y) >> ss_y;
194
195
    // extend padding pixels
196
3.94k
    if (out->p.w & ss_x) {
197
840
        pixel *ptr = luma_src;
198
15.4k
        for (int y = 0; y < bh; y++) {
199
14.6k
            ptr[out->p.w] = ptr[out->p.w - 1];
200
14.6k
            ptr += PXSTRIDE(in->stride[0]) << ss_y;
201
14.6k
        }
202
840
    }
203
204
3.94k
    const ptrdiff_t uv_off = row * FG_BLOCK_SIZE * PXSTRIDE(out->stride[1]) >> ss_y;
205
3.94k
    if (data->chroma_scaling_from_luma) {
206
7.01k
        for (int pl = 0; pl < 2; pl++)
207
4.65k
            dsp->fguv_32x32xn[in->p.layout - 1](((pixel *) out->data[1 + pl]) + uv_off,
208
4.65k
                                                ((const pixel *) in->data[1 + pl]) + uv_off,
209
4.65k
                                                in->stride[1], data, cpw,
210
4.65k
                                                scaling[0], grain_lut[1 + pl],
211
4.65k
                                                bh, row, luma_src, in->stride[0],
212
4.65k
                                                pl, is_id HIGHBD_TAIL_SUFFIX);
213
2.35k
    } else {
214
4.70k
        for (int pl = 0; pl < 2; pl++)
215
3.11k
            if (data->num_uv_points[pl])
216
2.02k
                dsp->fguv_32x32xn[in->p.layout - 1](((pixel *) out->data[1 + pl]) + uv_off,
217
2.02k
                                                    ((const pixel *) in->data[1 + pl]) + uv_off,
218
2.02k
                                                    in->stride[1], data, cpw,
219
2.02k
                                                    scaling[1 + pl], grain_lut[1 + pl],
220
2.02k
                                                    bh, row, luma_src, in->stride[0],
221
2.02k
                                                    pl, is_id HIGHBD_TAIL_SUFFIX);
222
1.59k
    }
223
3.94k
}
dav1d_apply_grain_row_16bpc
Line
Count
Source
167
43.6k
{
168
    // Synthesize grain for the affected planes
169
43.6k
    const Dav1dFilmGrainData *const data = &out->frame_hdr->film_grain.data;
170
43.6k
    const int ss_y = in->p.layout == DAV1D_PIXEL_LAYOUT_I420;
171
43.6k
    const int ss_x = in->p.layout != DAV1D_PIXEL_LAYOUT_I444;
172
43.6k
    const int cpw = (out->p.w + ss_x) >> ss_x;
173
43.6k
    const int is_id = out->seq_hdr->mtrx == DAV1D_MC_IDENTITY;
174
43.6k
    pixel *const luma_src =
175
43.6k
        ((pixel *) in->data[0]) + row * FG_BLOCK_SIZE * PXSTRIDE(in->stride[0]);
176
43.6k
#if BITDEPTH != 8
177
43.6k
    const int bitdepth_max = (1 << out->p.bpc) - 1;
178
43.6k
#endif
179
180
43.6k
    if (data->num_y_points) {
181
33.8k
        const int bh = imin(out->p.h - row * FG_BLOCK_SIZE, FG_BLOCK_SIZE);
182
33.8k
        dsp->fgy_32x32xn(((pixel *) out->data[0]) + row * FG_BLOCK_SIZE * PXSTRIDE(out->stride[0]),
183
33.8k
                         luma_src, out->stride[0], data,
184
33.8k
                         out->p.w, scaling[0], grain_lut[0], bh, row HIGHBD_TAIL_SUFFIX);
185
33.8k
    }
186
187
43.6k
    if (!data->num_uv_points[0] && !data->num_uv_points[1] &&
188
19.5k
        !data->chroma_scaling_from_luma)
189
1.65k
    {
190
1.65k
        return;
191
1.65k
    }
192
193
41.9k
    const int bh = (imin(out->p.h - row * FG_BLOCK_SIZE, FG_BLOCK_SIZE) + ss_y) >> ss_y;
194
195
    // extend padding pixels
196
41.9k
    if (out->p.w & ss_x) {
197
28.4k
        pixel *ptr = luma_src;
198
224k
        for (int y = 0; y < bh; y++) {
199
195k
            ptr[out->p.w] = ptr[out->p.w - 1];
200
195k
            ptr += PXSTRIDE(in->stride[0]) << ss_y;
201
195k
        }
202
28.4k
    }
203
204
41.9k
    const ptrdiff_t uv_off = row * FG_BLOCK_SIZE * PXSTRIDE(out->stride[1]) >> ss_y;
205
41.9k
    if (data->chroma_scaling_from_luma) {
206
35.3k
        for (int pl = 0; pl < 2; pl++)
207
23.6k
            dsp->fguv_32x32xn[in->p.layout - 1](((pixel *) out->data[1 + pl]) + uv_off,
208
23.6k
                                                ((const pixel *) in->data[1 + pl]) + uv_off,
209
23.6k
                                                in->stride[1], data, cpw,
210
23.6k
                                                scaling[0], grain_lut[1 + pl],
211
23.6k
                                                bh, row, luma_src, in->stride[0],
212
23.6k
                                                pl, is_id HIGHBD_TAIL_SUFFIX);
213
30.2k
    } else {
214
50.3k
        for (int pl = 0; pl < 2; pl++)
215
20.0k
            if (data->num_uv_points[pl])
216
18.7k
                dsp->fguv_32x32xn[in->p.layout - 1](((pixel *) out->data[1 + pl]) + uv_off,
217
18.7k
                                                    ((const pixel *) in->data[1 + pl]) + uv_off,
218
18.7k
                                                    in->stride[1], data, cpw,
219
18.7k
                                                    scaling[1 + pl], grain_lut[1 + pl],
220
18.7k
                                                    bh, row, luma_src, in->stride[0],
221
18.7k
                                                    pl, is_id HIGHBD_TAIL_SUFFIX);
222
30.2k
    }
223
41.9k
}
224
225
void bitfn(dav1d_apply_grain)(const Dav1dFilmGrainDSPContext *const dsp,
226
                              Dav1dPicture *const out,
227
                              const Dav1dPicture *const in)
228
816
{
229
816
    ALIGN_STK_16(entry, grain_lut, 3,[GRAIN_HEIGHT + 1][GRAIN_WIDTH]);
230
#if ARCH_X86_64 && BITDEPTH == 8
231
414
    ALIGN_STK_64(uint8_t, scaling, 3,[SCALING_SIZE]);
232
#else
233
    uint8_t scaling[3][SCALING_SIZE];
234
#endif
235
816
    const int rows = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
236
237
816
    bitfn(dav1d_prep_grain)(dsp, out, in, scaling, grain_lut);
238
12.0k
    for (int row = 0; row < rows; row++)
239
11.2k
        bitfn(dav1d_apply_grain_row)(dsp, out, in, scaling, grain_lut, row);
240
816
}
dav1d_apply_grain_8bpc
Line
Count
Source
228
414
{
229
414
    ALIGN_STK_16(entry, grain_lut, 3,[GRAIN_HEIGHT + 1][GRAIN_WIDTH]);
230
414
#if ARCH_X86_64 && BITDEPTH == 8
231
414
    ALIGN_STK_64(uint8_t, scaling, 3,[SCALING_SIZE]);
232
#else
233
    uint8_t scaling[3][SCALING_SIZE];
234
#endif
235
414
    const int rows = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
236
237
414
    bitfn(dav1d_prep_grain)(dsp, out, in, scaling, grain_lut);
238
4.26k
    for (int row = 0; row < rows; row++)
239
3.85k
        bitfn(dav1d_apply_grain_row)(dsp, out, in, scaling, grain_lut, row);
240
414
}
dav1d_apply_grain_16bpc
Line
Count
Source
228
402
{
229
402
    ALIGN_STK_16(entry, grain_lut, 3,[GRAIN_HEIGHT + 1][GRAIN_WIDTH]);
230
#if ARCH_X86_64 && BITDEPTH == 8
231
    ALIGN_STK_64(uint8_t, scaling, 3,[SCALING_SIZE]);
232
#else
233
402
    uint8_t scaling[3][SCALING_SIZE];
234
402
#endif
235
402
    const int rows = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
236
237
402
    bitfn(dav1d_prep_grain)(dsp, out, in, scaling, grain_lut);
238
7.81k
    for (int row = 0; row < rows; row++)
239
7.41k
        bitfn(dav1d_apply_grain_row)(dsp, out, in, scaling, grain_lut, row);
240
402
}
241
#endif