Coverage Report

Created: 2026-08-17 07:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libswscale/lut3d.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2024 Niklas Haas
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include <assert.h>
22
#include <string.h>
23
24
#include "libavutil/attributes.h"
25
#include "libavutil/avassert.h"
26
#include "libavutil/mem.h"
27
#include "libavutil/refstruct.h"
28
29
#include "cms.h"
30
#include "csputils.h"
31
#include "lut3d.h"
32
33
SwsLut3D *ff_sws_lut3d_alloc(void)
34
0
{
35
0
    const int flags = AV_REFSTRUCT_FLAG_NO_ZEROING;
36
0
    SwsLut3D *lut3d = av_refstruct_alloc_ext(sizeof(*lut3d), flags, NULL, NULL);
37
0
    if (!lut3d)
38
0
        return NULL;
39
40
0
    lut3d->map = (SwsColorMap) {0};
41
0
    lut3d->dynamic = false;
42
0
    return lut3d;
43
0
}
44
45
/**
46
 * v0 and v1 are 'black' and 'white'
47
 * v2 and v3 are closest RGB/CMY vertices
48
 * x >= y >= z are relative weights
49
 */
50
static av_always_inline
51
v3u16_t barycentric(int shift, int x, int y, int z,
52
                    v3u16_t v0, v3u16_t v1, v3u16_t v2, v3u16_t v3)
53
0
{
54
0
    const int a = (1 << shift) - x;
55
0
    const int b = x - y;
56
0
    const int c = y - z;
57
0
    const int d = z;
58
0
    av_assert2(x >= y);
59
0
    av_assert2(y >= z);
60
61
0
    return (v3u16_t) {
62
0
        (a * v0.x + b * v1.x + c * v2.x + d * v3.x) >> shift,
63
0
        (a * v0.y + b * v1.y + c * v2.y + d * v3.y) >> shift,
64
0
        (a * v0.z + b * v1.z + c * v2.z + d * v3.z) >> shift,
65
0
    };
66
0
}
67
68
static av_always_inline
69
v3u16_t tetrahedral(const SwsLut3D *lut3d, int Rx, int Gx, int Bx,
70
                    int Rf, int Gf, int Bf)
71
0
{
72
0
    const int shift = 16 - INPUT_LUT_BITS;
73
0
    const int Rn = FFMIN(Rx + 1, INPUT_LUT_SIZE - 1);
74
0
    const int Gn = FFMIN(Gx + 1, INPUT_LUT_SIZE - 1);
75
0
    const int Bn = FFMIN(Bx + 1, INPUT_LUT_SIZE - 1);
76
77
0
    const v3u16_t c000 = lut3d->input[Bx][Gx][Rx];
78
0
    const v3u16_t c111 = lut3d->input[Bn][Gn][Rn];
79
0
    if (Rf > Gf) {
80
0
        if (Gf > Bf) {
81
0
            const v3u16_t c100 = lut3d->input[Bx][Gx][Rn];
82
0
            const v3u16_t c110 = lut3d->input[Bx][Gn][Rn];
83
0
            return barycentric(shift, Rf, Gf, Bf, c000, c100, c110, c111);
84
0
        } else if (Rf > Bf) {
85
0
            const v3u16_t c100 = lut3d->input[Bx][Gx][Rn];
86
0
            const v3u16_t c101 = lut3d->input[Bn][Gx][Rn];
87
0
            return barycentric(shift, Rf, Bf, Gf, c000, c100, c101, c111);
88
0
        } else {
89
0
            const v3u16_t c001 = lut3d->input[Bn][Gx][Rx];
90
0
            const v3u16_t c101 = lut3d->input[Bn][Gx][Rn];
91
0
            return barycentric(shift, Bf, Rf, Gf, c000, c001, c101, c111);
92
0
        }
93
0
    } else {
94
0
        if (Bf > Gf) {
95
0
            const v3u16_t c001 = lut3d->input[Bn][Gx][Rx];
96
0
            const v3u16_t c011 = lut3d->input[Bn][Gn][Rx];
97
0
            return barycentric(shift, Bf, Gf, Rf, c000, c001, c011, c111);
98
0
        } else if (Bf > Rf) {
99
0
            const v3u16_t c010 = lut3d->input[Bx][Gn][Rx];
100
0
            const v3u16_t c011 = lut3d->input[Bn][Gn][Rx];
101
0
            return barycentric(shift, Gf, Bf, Rf, c000, c010, c011, c111);
102
0
        } else {
103
0
            const v3u16_t c010 = lut3d->input[Bx][Gn][Rx];
104
0
            const v3u16_t c110 = lut3d->input[Bx][Gn][Rn];
105
0
            return barycentric(shift, Gf, Rf, Bf, c000, c010, c110, c111);
106
0
        }
107
0
    }
108
0
}
109
110
static av_always_inline v3u16_t lookup_input16(const SwsLut3D *lut3d, v3u16_t rgb)
111
0
{
112
0
    const int shift = 16 - INPUT_LUT_BITS;
113
0
    const int Rx = rgb.x >> shift;
114
0
    const int Gx = rgb.y >> shift;
115
0
    const int Bx = rgb.z >> shift;
116
0
    const int Rf = rgb.x & ((1 << shift) - 1);
117
0
    const int Gf = rgb.y & ((1 << shift) - 1);
118
0
    const int Bf = rgb.z & ((1 << shift) - 1);
119
0
    return tetrahedral(lut3d, Rx, Gx, Bx, Rf, Gf, Bf);
120
0
}
121
122
/**
123
 * Note: These functions are scaled such that x == (1 << shift) corresponds to
124
 * a value of 1.0. This makes them suitable for use when interpolation LUT
125
 * entries with a fractional part that is just masked away from the index,
126
 * since a fractional coordinate of e.g. 0xFFFF corresponds to a mix weight of
127
 * just slightly *less* than 1.0.
128
 */
129
static av_always_inline v2u16_t lerp2u16(v2u16_t a, v2u16_t b, int x, int shift)
130
0
{
131
0
    const int xi = (1 << shift) - x;
132
0
    return (v2u16_t) {
133
0
        (a.x * xi + b.x * x) >> shift,
134
0
        (a.y * xi + b.y * x) >> shift,
135
0
    };
136
0
}
137
138
static av_always_inline v3u16_t lerp3u16(v3u16_t a, v3u16_t b, int x, int shift)
139
0
{
140
0
    const int xi = (1 << shift) - x;
141
0
    return (v3u16_t) {
142
0
        (a.x * xi + b.x * x) >> shift,
143
0
        (a.y * xi + b.y * x) >> shift,
144
0
        (a.z * xi + b.z * x) >> shift,
145
0
    };
146
0
}
147
148
static av_always_inline v3u16_t lookup_output(const SwsLut3D *lut3d, v3u16_t ipt)
149
0
{
150
0
    const int Ishift = 16 - OUTPUT_LUT_BITS_I;
151
0
    const int Cshift = 16 - OUTPUT_LUT_BITS_PT;
152
0
    const int Ix = ipt.x >> Ishift;
153
0
    const int Px = ipt.y >> Cshift;
154
0
    const int Tx = ipt.z >> Cshift;
155
0
    const int If = ipt.x & ((1 << Ishift) - 1);
156
0
    const int Pf = ipt.y & ((1 << Cshift) - 1);
157
0
    const int Tf = ipt.z & ((1 << Cshift) - 1);
158
0
    const int In = FFMIN(Ix + 1, OUTPUT_LUT_SIZE_I  - 1);
159
0
    const int Pn = FFMIN(Px + 1, OUTPUT_LUT_SIZE_PT - 1);
160
0
    const int Tn = FFMIN(Tx + 1, OUTPUT_LUT_SIZE_PT - 1);
161
162
    /* Trilinear interpolation */
163
0
    const v3u16_t c000 = lut3d->output[Tx][Px][Ix];
164
0
    const v3u16_t c001 = lut3d->output[Tx][Px][In];
165
0
    const v3u16_t c010 = lut3d->output[Tx][Pn][Ix];
166
0
    const v3u16_t c011 = lut3d->output[Tx][Pn][In];
167
0
    const v3u16_t c100 = lut3d->output[Tn][Px][Ix];
168
0
    const v3u16_t c101 = lut3d->output[Tn][Px][In];
169
0
    const v3u16_t c110 = lut3d->output[Tn][Pn][Ix];
170
0
    const v3u16_t c111 = lut3d->output[Tn][Pn][In];
171
0
    const v3u16_t c00  = lerp3u16(c000, c100, Tf, Cshift);
172
0
    const v3u16_t c10  = lerp3u16(c010, c110, Tf, Cshift);
173
0
    const v3u16_t c01  = lerp3u16(c001, c101, Tf, Cshift);
174
0
    const v3u16_t c11  = lerp3u16(c011, c111, Tf, Cshift);
175
0
    const v3u16_t c0   = lerp3u16(c00,  c10,  Pf, Cshift);
176
0
    const v3u16_t c1   = lerp3u16(c01,  c11,  Pf, Cshift);
177
0
    const v3u16_t c    = lerp3u16(c0,   c1,   If, Ishift);
178
0
    return c;
179
0
}
180
181
static av_always_inline v3u16_t apply_tone_map(const SwsLut3D *lut3d, v3u16_t ipt)
182
0
{
183
0
    const int shift = 16 - TONE_LUT_BITS;
184
0
    const int Ix = ipt.x >> shift;
185
0
    const int If = ipt.x & ((1 << shift) - 1);
186
187
0
    const v2u16_t w0 = lut3d->tone_map[Ix];
188
0
    const v2u16_t w1 = lut3d->tone_map[Ix + 1];
189
0
    const v2u16_t w  = lerp2u16(w0, w1, If, shift);
190
0
    const int base   = (1 << 15) - w.y;
191
192
0
    ipt.x = w.x;
193
0
    ipt.y = base + (ipt.y * w.y >> 15);
194
0
    ipt.z = base + (ipt.z * w.y >> 15);
195
0
    return ipt;
196
0
}
197
198
int ff_sws_lut3d_generate(SwsLut3D *lut3d, const SwsColorMap *map)
199
0
{
200
0
    int ret;
201
202
0
    lut3d->dynamic = map->src.frame_peak.num > 0;
203
0
    lut3d->map = *map;
204
205
0
    if (lut3d->dynamic) {
206
0
        ret = ff_sws_color_map_generate_dynamic(&lut3d->input[0][0][0],
207
0
                                                &lut3d->output[0][0][0],
208
0
                                                INPUT_LUT_SIZE, OUTPUT_LUT_SIZE_I,
209
0
                                                OUTPUT_LUT_SIZE_PT, map);
210
0
        if (ret < 0)
211
0
            return ret;
212
213
        /* Make sure initial state is valid */
214
0
        ff_sws_lut3d_update(lut3d, &map->src);
215
0
        return 0;
216
0
    } else {
217
0
        return ff_sws_color_map_generate_static(&lut3d->input[0][0][0],
218
0
                                                INPUT_LUT_SIZE, map);
219
0
    }
220
0
}
221
222
void ff_sws_lut3d_update(SwsLut3D *lut3d, const SwsColor *new_src)
223
0
{
224
0
    if (!new_src || !lut3d->dynamic)
225
0
        return;
226
227
0
    lut3d->map.src.frame_peak = new_src->frame_peak;
228
0
    lut3d->map.src.frame_avg  = new_src->frame_avg;
229
230
0
    ff_sws_tone_map_generate(lut3d->tone_map, TONE_LUT_SIZE, &lut3d->map);
231
0
    lut3d->tone_map[TONE_LUT_SIZE] = lut3d->tone_map[TONE_LUT_SIZE - 1];
232
0
}
233
234
void ff_sws_lut3d_apply_rgba64(const SwsLut3D *lut3d, const uint8_t *in,
235
                               int in_stride, uint8_t *out, int out_stride,
236
                               int w, int h)
237
0
{
238
0
    while (h--) {
239
0
        const uint16_t *in16 = (const uint16_t *) in;
240
0
        uint16_t *out16 = (uint16_t *) out;
241
242
0
        for (int x = 0; x < w; x++) {
243
0
            v3u16_t c = { in16[0], in16[1], in16[2] };
244
0
            c = lookup_input16(lut3d, c);
245
246
0
            if (lut3d->dynamic) {
247
0
                c = apply_tone_map(lut3d, c);
248
0
                c = lookup_output(lut3d, c);
249
0
            }
250
251
0
            out16[0] = c.x;
252
0
            out16[1] = c.y;
253
0
            out16[2] = c.z;
254
0
            out16[3] = in16[3];
255
0
            in16  += 4;
256
0
            out16 += 4;
257
0
        }
258
259
0
        in  += in_stride;
260
0
        out += out_stride;
261
0
    }
262
0
}