FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_lut3d.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 1028 0.0%
Functions: 0 140 0.0%
Branches: 0 1528 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Clément Bœsch
3 * Copyright (c) 2018 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * 3D Lookup table filter
25 */
26
27 #include <float.h>
28
29 #include "config_components.h"
30
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/file_open.h"
34 #include "libavutil/intfloat.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/avstring.h"
37 #include "drawutils.h"
38 #include "filters.h"
39 #include "video.h"
40 #include "lut3d.h"
41
42 #define R 0
43 #define G 1
44 #define B 2
45 #define A 3
46
47 #define OFFSET(x) offsetof(LUT3DContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
50 #define COMMON_OPTIONS \
51 { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, TFLAGS, .unit = "interp_mode" }, \
52 { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
53 { "trilinear", "interpolate values using the 8 points defining a cube", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TRILINEAR}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
54 { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
55 { "pyramid", "interpolate values using a pyramid", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_PYRAMID}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
56 { "prism", "interpolate values using a prism", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_PRISM}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
57 { NULL }
58
59 #define EXPONENT_MASK 0x7F800000
60 #define MANTISSA_MASK 0x007FFFFF
61 #define SIGN_MASK 0x80000000
62
63 static inline float sanitizef(float f)
64 {
65 union av_intfloat32 t;
66 t.f = f;
67
68 if ((t.i & EXPONENT_MASK) == EXPONENT_MASK) {
69 if ((t.i & MANTISSA_MASK) != 0) {
70 // NAN
71 return 0.0f;
72 } else if (t.i & SIGN_MASK) {
73 // -INF
74 return -FLT_MAX;
75 } else {
76 // +INF
77 return FLT_MAX;
78 }
79 }
80 return f;
81 }
82
83 static inline float lerpf(float v0, float v1, float f)
84 {
85 return v0 + (v1 - v0) * f;
86 }
87
88 static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
89 {
90 struct rgbvec v = {
91 lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
92 };
93 return v;
94 }
95
96 #define NEAR(x) ((int)((x) + .5))
97 #define PREV(x) ((int)(x))
98 #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
99
100 /**
101 * Get the nearest defined point
102 */
103 static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
104 const struct rgbvec *s)
105 {
106 return lut3d->lut[NEAR(s->r) * lut3d->lutsize2 + NEAR(s->g) * lut3d->lutsize + NEAR(s->b)];
107 }
108
109 /**
110 * Interpolate using the 8 vertices of a cube
111 * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
112 */
113 static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
114 const struct rgbvec *s)
115 {
116 const int lutsize2 = lut3d->lutsize2;
117 const int lutsize = lut3d->lutsize;
118 const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
119 const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
120 const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
121 const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
122 const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
123 const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
124 const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
125 const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
126 const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
127 const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
128 const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
129 const struct rgbvec c00 = lerp(&c000, &c100, d.r);
130 const struct rgbvec c10 = lerp(&c010, &c110, d.r);
131 const struct rgbvec c01 = lerp(&c001, &c101, d.r);
132 const struct rgbvec c11 = lerp(&c011, &c111, d.r);
133 const struct rgbvec c0 = lerp(&c00, &c10, d.g);
134 const struct rgbvec c1 = lerp(&c01, &c11, d.g);
135 const struct rgbvec c = lerp(&c0, &c1, d.b);
136 return c;
137 }
138
139 static inline struct rgbvec interp_pyramid(const LUT3DContext *lut3d,
140 const struct rgbvec *s)
141 {
142 const int lutsize2 = lut3d->lutsize2;
143 const int lutsize = lut3d->lutsize;
144 const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
145 const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
146 const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
147 const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
148 const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
149 struct rgbvec c;
150
151 if (d.g > d.r && d.b > d.r) {
152 const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
153 const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
154 const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
155
156 c.r = c000.r + (c111.r - c011.r) * d.r + (c010.r - c000.r) * d.g + (c001.r - c000.r) * d.b +
157 (c011.r - c001.r - c010.r + c000.r) * d.g * d.b;
158 c.g = c000.g + (c111.g - c011.g) * d.r + (c010.g - c000.g) * d.g + (c001.g - c000.g) * d.b +
159 (c011.g - c001.g - c010.g + c000.g) * d.g * d.b;
160 c.b = c000.b + (c111.b - c011.b) * d.r + (c010.b - c000.b) * d.g + (c001.b - c000.b) * d.b +
161 (c011.b - c001.b - c010.b + c000.b) * d.g * d.b;
162 } else if (d.r > d.g && d.b > d.g) {
163 const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
164 const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
165 const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
166
167 c.r = c000.r + (c100.r - c000.r) * d.r + (c111.r - c101.r) * d.g + (c001.r - c000.r) * d.b +
168 (c101.r - c001.r - c100.r + c000.r) * d.r * d.b;
169 c.g = c000.g + (c100.g - c000.g) * d.r + (c111.g - c101.g) * d.g + (c001.g - c000.g) * d.b +
170 (c101.g - c001.g - c100.g + c000.g) * d.r * d.b;
171 c.b = c000.b + (c100.b - c000.b) * d.r + (c111.b - c101.b) * d.g + (c001.b - c000.b) * d.b +
172 (c101.b - c001.b - c100.b + c000.b) * d.r * d.b;
173 } else {
174 const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
175 const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
176 const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
177
178 c.r = c000.r + (c100.r - c000.r) * d.r + (c010.r - c000.r) * d.g + (c111.r - c110.r) * d.b +
179 (c110.r - c100.r - c010.r + c000.r) * d.r * d.g;
180 c.g = c000.g + (c100.g - c000.g) * d.r + (c010.g - c000.g) * d.g + (c111.g - c110.g) * d.b +
181 (c110.g - c100.g - c010.g + c000.g) * d.r * d.g;
182 c.b = c000.b + (c100.b - c000.b) * d.r + (c010.b - c000.b) * d.g + (c111.b - c110.b) * d.b +
183 (c110.b - c100.b - c010.b + c000.b) * d.r * d.g;
184 }
185
186 return c;
187 }
188
189 static inline struct rgbvec interp_prism(const LUT3DContext *lut3d,
190 const struct rgbvec *s)
191 {
192 const int lutsize2 = lut3d->lutsize2;
193 const int lutsize = lut3d->lutsize;
194 const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
195 const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
196 const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
197 const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
198 const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
199 const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
200 const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
201 struct rgbvec c;
202
203 if (d.b > d.r) {
204 const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
205 const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
206
207 c.r = c000.r + (c001.r - c000.r) * d.b + (c101.r - c001.r) * d.r + (c010.r - c000.r) * d.g +
208 (c000.r - c010.r - c001.r + c011.r) * d.b * d.g +
209 (c001.r - c011.r - c101.r + c111.r) * d.r * d.g;
210 c.g = c000.g + (c001.g - c000.g) * d.b + (c101.g - c001.g) * d.r + (c010.g - c000.g) * d.g +
211 (c000.g - c010.g - c001.g + c011.g) * d.b * d.g +
212 (c001.g - c011.g - c101.g + c111.g) * d.r * d.g;
213 c.b = c000.b + (c001.b - c000.b) * d.b + (c101.b - c001.b) * d.r + (c010.b - c000.b) * d.g +
214 (c000.b - c010.b - c001.b + c011.b) * d.b * d.g +
215 (c001.b - c011.b - c101.b + c111.b) * d.r * d.g;
216 } else {
217 const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
218 const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
219
220 c.r = c000.r + (c101.r - c100.r) * d.b + (c100.r - c000.r) * d.r + (c010.r - c000.r) * d.g +
221 (c100.r - c110.r - c101.r + c111.r) * d.b * d.g +
222 (c000.r - c010.r - c100.r + c110.r) * d.r * d.g;
223 c.g = c000.g + (c101.g - c100.g) * d.b + (c100.g - c000.g) * d.r + (c010.g - c000.g) * d.g +
224 (c100.g - c110.g - c101.g + c111.g) * d.b * d.g +
225 (c000.g - c010.g - c100.g + c110.g) * d.r * d.g;
226 c.b = c000.b + (c101.b - c100.b) * d.b + (c100.b - c000.b) * d.r + (c010.b - c000.b) * d.g +
227 (c100.b - c110.b - c101.b + c111.b) * d.b * d.g +
228 (c000.b - c010.b - c100.b + c110.b) * d.r * d.g;
229 }
230
231 return c;
232 }
233
234 /**
235 * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
236 * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
237 */
238 static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
239 const struct rgbvec *s)
240 {
241 const int lutsize2 = lut3d->lutsize2;
242 const int lutsize = lut3d->lutsize;
243 const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
244 const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
245 const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
246 const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
247 const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
248 struct rgbvec c;
249 if (d.r > d.g) {
250 if (d.g > d.b) {
251 const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
252 const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
253 c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
254 c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
255 c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
256 } else if (d.r > d.b) {
257 const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
258 const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
259 c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
260 c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
261 c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
262 } else {
263 const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
264 const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
265 c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
266 c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
267 c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
268 }
269 } else {
270 if (d.b > d.g) {
271 const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
272 const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
273 c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
274 c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
275 c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
276 } else if (d.b > d.r) {
277 const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
278 const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
279 c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
280 c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
281 c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
282 } else {
283 const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
284 const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
285 c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
286 c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
287 c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
288 }
289 }
290 return c;
291 }
292
293 static inline float prelut_interp_1d_linear(const Lut3DPreLut *prelut,
294 int idx, const float s)
295 {
296 const int lut_max = prelut->size - 1;
297 const float scaled = (s - prelut->min[idx]) * prelut->scale[idx];
298 const float x = av_clipf(scaled, 0.0f, lut_max);
299 const int prev = PREV(x);
300 const int next = FFMIN((int)(x) + 1, lut_max);
301 const float p = prelut->lut[idx][prev];
302 const float n = prelut->lut[idx][next];
303 const float d = x - (float)prev;
304 return lerpf(p, n, d);
305 }
306
307 static inline struct rgbvec apply_prelut(const Lut3DPreLut *prelut,
308 const struct rgbvec *s)
309 {
310 struct rgbvec c;
311
312 if (prelut->size <= 0)
313 return *s;
314
315 c.r = prelut_interp_1d_linear(prelut, 0, s->r);
316 c.g = prelut_interp_1d_linear(prelut, 1, s->g);
317 c.b = prelut_interp_1d_linear(prelut, 2, s->b);
318 return c;
319 }
320
321 #define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \
322 static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
323 { \
324 int x, y; \
325 const LUT3DContext *lut3d = ctx->priv; \
326 const Lut3DPreLut *prelut = &lut3d->prelut; \
327 const ThreadData *td = arg; \
328 const AVFrame *in = td->in; \
329 const AVFrame *out = td->out; \
330 const int direct = out == in; \
331 const int slice_start = (in->height * jobnr ) / nb_jobs; \
332 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
333 uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
334 uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
335 uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
336 uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
337 const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
338 const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
339 const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
340 const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
341 const float lut_max = lut3d->lutsize - 1; \
342 const float scale_f = 1.0f / ((1<<depth) - 1); \
343 const float scale_r = lut3d->scale.r * lut_max; \
344 const float scale_g = lut3d->scale.g * lut_max; \
345 const float scale_b = lut3d->scale.b * lut_max; \
346 \
347 for (y = slice_start; y < slice_end; y++) { \
348 uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
349 uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
350 uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
351 uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
352 const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
353 const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
354 const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
355 const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
356 for (x = 0; x < in->width; x++) { \
357 const struct rgbvec rgb = {srcr[x] * scale_f, \
358 srcg[x] * scale_f, \
359 srcb[x] * scale_f}; \
360 const struct rgbvec prelut_rgb = apply_prelut(prelut, &rgb); \
361 const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \
362 av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \
363 av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \
364 struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
365 dstr[x] = av_clip_uintp2(vec.r * (float)((1<<depth) - 1), depth); \
366 dstg[x] = av_clip_uintp2(vec.g * (float)((1<<depth) - 1), depth); \
367 dstb[x] = av_clip_uintp2(vec.b * (float)((1<<depth) - 1), depth); \
368 if (!direct && in->linesize[3]) \
369 dsta[x] = srca[x]; \
370 } \
371 grow += out->linesize[0]; \
372 brow += out->linesize[1]; \
373 rrow += out->linesize[2]; \
374 arow += out->linesize[3]; \
375 srcgrow += in->linesize[0]; \
376 srcbrow += in->linesize[1]; \
377 srcrrow += in->linesize[2]; \
378 srcarow += in->linesize[3]; \
379 } \
380 return 0; \
381 }
382
383 DEFINE_INTERP_FUNC_PLANAR(nearest, 8, 8)
384 DEFINE_INTERP_FUNC_PLANAR(trilinear, 8, 8)
385 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 8, 8)
386 DEFINE_INTERP_FUNC_PLANAR(pyramid, 8, 8)
387 DEFINE_INTERP_FUNC_PLANAR(prism, 8, 8)
388
389 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 9)
390 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 9)
391 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 9)
392 DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 9)
393 DEFINE_INTERP_FUNC_PLANAR(prism, 16, 9)
394
395 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 10)
396 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 10)
397 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 10)
398 DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 10)
399 DEFINE_INTERP_FUNC_PLANAR(prism, 16, 10)
400
401 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 12)
402 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 12)
403 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 12)
404 DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 12)
405 DEFINE_INTERP_FUNC_PLANAR(prism, 16, 12)
406
407 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 14)
408 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 14)
409 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 14)
410 DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 14)
411 DEFINE_INTERP_FUNC_PLANAR(prism, 16, 14)
412
413 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16)
414 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16)
415 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16)
416 DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 16)
417 DEFINE_INTERP_FUNC_PLANAR(prism, 16, 16)
418
419 #define DEFINE_INTERP_FUNC_PLANAR_FLOAT(name, depth) \
420 static int interp_##name##_pf##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
421 { \
422 int x, y; \
423 const LUT3DContext *lut3d = ctx->priv; \
424 const Lut3DPreLut *prelut = &lut3d->prelut; \
425 const ThreadData *td = arg; \
426 const AVFrame *in = td->in; \
427 const AVFrame *out = td->out; \
428 const int direct = out == in; \
429 const int slice_start = (in->height * jobnr ) / nb_jobs; \
430 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
431 uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
432 uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
433 uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
434 uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
435 const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
436 const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
437 const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
438 const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
439 const float lut_max = lut3d->lutsize - 1; \
440 const float scale_r = lut3d->scale.r * lut_max; \
441 const float scale_g = lut3d->scale.g * lut_max; \
442 const float scale_b = lut3d->scale.b * lut_max; \
443 \
444 for (y = slice_start; y < slice_end; y++) { \
445 float *dstg = (float *)grow; \
446 float *dstb = (float *)brow; \
447 float *dstr = (float *)rrow; \
448 float *dsta = (float *)arow; \
449 const float *srcg = (const float *)srcgrow; \
450 const float *srcb = (const float *)srcbrow; \
451 const float *srcr = (const float *)srcrrow; \
452 const float *srca = (const float *)srcarow; \
453 for (x = 0; x < in->width; x++) { \
454 const struct rgbvec rgb = {sanitizef(srcr[x]), \
455 sanitizef(srcg[x]), \
456 sanitizef(srcb[x])}; \
457 const struct rgbvec prelut_rgb = apply_prelut(prelut, &rgb); \
458 const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \
459 av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \
460 av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \
461 struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
462 dstr[x] = vec.r; \
463 dstg[x] = vec.g; \
464 dstb[x] = vec.b; \
465 if (!direct && in->linesize[3]) \
466 dsta[x] = srca[x]; \
467 } \
468 grow += out->linesize[0]; \
469 brow += out->linesize[1]; \
470 rrow += out->linesize[2]; \
471 arow += out->linesize[3]; \
472 srcgrow += in->linesize[0]; \
473 srcbrow += in->linesize[1]; \
474 srcrrow += in->linesize[2]; \
475 srcarow += in->linesize[3]; \
476 } \
477 return 0; \
478 }
479
480 DEFINE_INTERP_FUNC_PLANAR_FLOAT(nearest, 32)
481 DEFINE_INTERP_FUNC_PLANAR_FLOAT(trilinear, 32)
482 DEFINE_INTERP_FUNC_PLANAR_FLOAT(tetrahedral, 32)
483 DEFINE_INTERP_FUNC_PLANAR_FLOAT(pyramid, 32)
484 DEFINE_INTERP_FUNC_PLANAR_FLOAT(prism, 32)
485
486 #define DEFINE_INTERP_FUNC(name, nbits) \
487 static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
488 { \
489 int x, y; \
490 const LUT3DContext *lut3d = ctx->priv; \
491 const Lut3DPreLut *prelut = &lut3d->prelut; \
492 const ThreadData *td = arg; \
493 const AVFrame *in = td->in; \
494 const AVFrame *out = td->out; \
495 const int direct = out == in; \
496 const int step = lut3d->step; \
497 const uint8_t r = lut3d->rgba_map[R]; \
498 const uint8_t g = lut3d->rgba_map[G]; \
499 const uint8_t b = lut3d->rgba_map[B]; \
500 const uint8_t a = lut3d->rgba_map[A]; \
501 const int slice_start = (in->height * jobnr ) / nb_jobs; \
502 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
503 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
504 const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
505 const float lut_max = lut3d->lutsize - 1; \
506 const float scale_f = 1.0f / ((1<<nbits) - 1); \
507 const float scale_r = lut3d->scale.r * lut_max; \
508 const float scale_g = lut3d->scale.g * lut_max; \
509 const float scale_b = lut3d->scale.b * lut_max; \
510 \
511 for (y = slice_start; y < slice_end; y++) { \
512 uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
513 const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
514 for (x = 0; x < in->width * step; x += step) { \
515 const struct rgbvec rgb = {src[x + r] * scale_f, \
516 src[x + g] * scale_f, \
517 src[x + b] * scale_f}; \
518 const struct rgbvec prelut_rgb = apply_prelut(prelut, &rgb); \
519 const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \
520 av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \
521 av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \
522 struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
523 dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
524 dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
525 dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
526 if (!direct && step == 4) \
527 dst[x + a] = src[x + a]; \
528 } \
529 dstrow += out->linesize[0]; \
530 srcrow += in ->linesize[0]; \
531 } \
532 return 0; \
533 }
534
535 DEFINE_INTERP_FUNC(nearest, 8)
536 DEFINE_INTERP_FUNC(trilinear, 8)
537 DEFINE_INTERP_FUNC(tetrahedral, 8)
538 DEFINE_INTERP_FUNC(pyramid, 8)
539 DEFINE_INTERP_FUNC(prism, 8)
540
541 DEFINE_INTERP_FUNC(nearest, 16)
542 DEFINE_INTERP_FUNC(trilinear, 16)
543 DEFINE_INTERP_FUNC(tetrahedral, 16)
544 DEFINE_INTERP_FUNC(pyramid, 16)
545 DEFINE_INTERP_FUNC(prism, 16)
546
547 #define MAX_LINE_SIZE 512
548
549 static int skip_line(const char *p)
550 {
551 while (*p && av_isspace(*p))
552 p++;
553 return !*p || *p == '#';
554 }
555
556 static char* fget_next_word(char* dst, int max, FILE* f)
557 {
558 int c;
559 char *p = dst;
560
561 /* for null */
562 max--;
563 /* skip until next non whitespace char */
564 while ((c = fgetc(f)) != EOF) {
565 if (av_isspace(c))
566 continue;
567
568 *p++ = c;
569 max--;
570 break;
571 }
572
573 /* get max bytes or up until next whitespace char */
574 for (; max > 0; max--) {
575 if ((c = fgetc(f)) == EOF)
576 break;
577
578 if (av_isspace(c))
579 break;
580
581 *p++ = c;
582 }
583
584 *p = 0;
585 if (p == dst)
586 return NULL;
587 return p;
588 }
589
590 #define NEXT_LINE(loop_cond) do { \
591 if (!fgets(line, sizeof(line), f)) { \
592 av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
593 return AVERROR_INVALIDDATA; \
594 } \
595 } while (loop_cond)
596
597 #define NEXT_LINE_OR_GOTO(loop_cond, label) do { \
598 if (!fgets(line, sizeof(line), f)) { \
599 av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
600 ret = AVERROR_INVALIDDATA; \
601 goto label; \
602 } \
603 } while (loop_cond)
604
605 static int allocate_3dlut(AVFilterContext *ctx, int lutsize, int prelut)
606 {
607 LUT3DContext *lut3d = ctx->priv;
608 int i;
609 if (lutsize < 2 || lutsize > MAX_LEVEL) {
610 av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
611 return AVERROR(EINVAL);
612 }
613
614 av_freep(&lut3d->lut);
615 lut3d->lut = av_malloc_array(lutsize * lutsize * lutsize, sizeof(*lut3d->lut));
616 if (!lut3d->lut)
617 return AVERROR(ENOMEM);
618
619 if (prelut) {
620 lut3d->prelut.size = PRELUT_SIZE;
621 for (i = 0; i < 3; i++) {
622 av_freep(&lut3d->prelut.lut[i]);
623 lut3d->prelut.lut[i] = av_malloc_array(PRELUT_SIZE, sizeof(*lut3d->prelut.lut[0]));
624 if (!lut3d->prelut.lut[i])
625 return AVERROR(ENOMEM);
626 }
627 } else {
628 lut3d->prelut.size = 0;
629 for (i = 0; i < 3; i++) {
630 av_freep(&lut3d->prelut.lut[i]);
631 }
632 }
633 lut3d->lutsize = lutsize;
634 lut3d->lutsize2 = lutsize * lutsize;
635 return 0;
636 }
637
638 /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
639 * directive; seems to be generated by Davinci */
640 static int parse_dat(AVFilterContext *ctx, FILE *f)
641 {
642 LUT3DContext *lut3d = ctx->priv;
643 char line[MAX_LINE_SIZE];
644 int ret, i, j, k, size, size2;
645
646 lut3d->lutsize = size = 33;
647 size2 = size * size;
648
649 NEXT_LINE(skip_line(line));
650 if (!strncmp(line, "3DLUTSIZE ", 10)) {
651 size = strtol(line + 10, NULL, 0);
652
653 NEXT_LINE(skip_line(line));
654 }
655
656 ret = allocate_3dlut(ctx, size, 0);
657 if (ret < 0)
658 return ret;
659
660 for (k = 0; k < size; k++) {
661 for (j = 0; j < size; j++) {
662 for (i = 0; i < size; i++) {
663 struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
664 if (k != 0 || j != 0 || i != 0)
665 NEXT_LINE(skip_line(line));
666 if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
667 return AVERROR_INVALIDDATA;
668 }
669 }
670 }
671 return 0;
672 }
673
674 /* Iridas format */
675 static int parse_cube(AVFilterContext *ctx, FILE *f)
676 {
677 LUT3DContext *lut3d = ctx->priv;
678 char line[MAX_LINE_SIZE];
679 float min[3] = {0.0, 0.0, 0.0};
680 float max[3] = {1.0, 1.0, 1.0};
681
682 while (fgets(line, sizeof(line), f)) {
683 if (!strncmp(line, "LUT_3D_SIZE", 11)) {
684 int ret, i, j, k;
685 const int size = strtol(line + 12, NULL, 0);
686 const int size2 = size * size;
687
688 ret = allocate_3dlut(ctx, size, 0);
689 if (ret < 0)
690 return ret;
691
692 for (k = 0; k < size; k++) {
693 for (j = 0; j < size; j++) {
694 for (i = 0; i < size; i++) {
695 struct rgbvec *vec = &lut3d->lut[i * size2 + j * size + k];
696
697 do {
698 try_again:
699 NEXT_LINE(0);
700 if (!strncmp(line, "DOMAIN_", 7)) {
701 float *vals = NULL;
702 if (!strncmp(line + 7, "MIN ", 4)) vals = min;
703 else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
704 if (!vals)
705 return AVERROR_INVALIDDATA;
706 if (av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2) != 3)
707 return AVERROR_INVALIDDATA;
708 av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
709 min[0], min[1], min[2], max[0], max[1], max[2]);
710 goto try_again;
711 } else if (!strncmp(line, "TITLE", 5)) {
712 goto try_again;
713 }
714 } while (skip_line(line));
715 if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
716 return AVERROR_INVALIDDATA;
717 }
718 }
719 }
720 break;
721 }
722 }
723
724 lut3d->scale.r = av_clipf(1. / (max[0] - min[0]), 0.f, 1.f);
725 lut3d->scale.g = av_clipf(1. / (max[1] - min[1]), 0.f, 1.f);
726 lut3d->scale.b = av_clipf(1. / (max[2] - min[2]), 0.f, 1.f);
727
728 return 0;
729 }
730
731 /* Assume 17x17x17 LUT with a 16-bit depth
732 * FIXME: it seems there are various 3dl formats */
733 static int parse_3dl(AVFilterContext *ctx, FILE *f)
734 {
735 char line[MAX_LINE_SIZE];
736 LUT3DContext *lut3d = ctx->priv;
737 int ret, i, j, k;
738 const int size = 17;
739 const int size2 = 17 * 17;
740 const float scale = 16*16*16;
741
742 lut3d->lutsize = size;
743
744 ret = allocate_3dlut(ctx, size, 0);
745 if (ret < 0)
746 return ret;
747
748 NEXT_LINE(skip_line(line));
749 for (k = 0; k < size; k++) {
750 for (j = 0; j < size; j++) {
751 for (i = 0; i < size; i++) {
752 int r, g, b;
753 struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
754
755 NEXT_LINE(skip_line(line));
756 if (av_sscanf(line, "%d %d %d", &r, &g, &b) != 3)
757 return AVERROR_INVALIDDATA;
758 vec->r = r / scale;
759 vec->g = g / scale;
760 vec->b = b / scale;
761 }
762 }
763 }
764 return 0;
765 }
766
767 /* Pandora format */
768 static int parse_m3d(AVFilterContext *ctx, FILE *f)
769 {
770 LUT3DContext *lut3d = ctx->priv;
771 float scale;
772 int ret, i, j, k, size, size2, in = -1, out = -1;
773 char line[MAX_LINE_SIZE];
774 uint8_t rgb_map[3] = {0, 1, 2};
775
776 while (fgets(line, sizeof(line), f)) {
777 if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
778 else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
779 else if (!strncmp(line, "values", 6)) {
780 const char *p = line + 6;
781 #define SET_COLOR(id) do { \
782 while (av_isspace(*p)) \
783 p++; \
784 switch (*p) { \
785 case 'r': rgb_map[id] = 0; break; \
786 case 'g': rgb_map[id] = 1; break; \
787 case 'b': rgb_map[id] = 2; break; \
788 } \
789 while (*p && !av_isspace(*p)) \
790 p++; \
791 } while (0)
792 SET_COLOR(0);
793 SET_COLOR(1);
794 SET_COLOR(2);
795 break;
796 }
797 }
798
799 if (in == -1 || out == -1) {
800 av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
801 return AVERROR_INVALIDDATA;
802 }
803 if (in < 2 || out < 2 ||
804 in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
805 out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
806 av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
807 return AVERROR_INVALIDDATA;
808 }
809 for (size = 1; size*size*size < in; size++);
810 lut3d->lutsize = size;
811 size2 = size * size;
812
813 ret = allocate_3dlut(ctx, size, 0);
814 if (ret < 0)
815 return ret;
816
817 scale = 1. / (out - 1);
818
819 for (k = 0; k < size; k++) {
820 for (j = 0; j < size; j++) {
821 for (i = 0; i < size; i++) {
822 struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
823 float val[3];
824
825 NEXT_LINE(0);
826 if (av_sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
827 return AVERROR_INVALIDDATA;
828 vec->r = val[rgb_map[0]] * scale;
829 vec->g = val[rgb_map[1]] * scale;
830 vec->b = val[rgb_map[2]] * scale;
831 }
832 }
833 }
834 return 0;
835 }
836
837 static int nearest_sample_index(float *data, float x, int low, int hi)
838 {
839 int mid;
840 if (x < data[low])
841 return low;
842
843 if (x > data[hi])
844 return hi;
845
846 for (;;) {
847 av_assert0(x >= data[low]);
848 av_assert0(x <= data[hi]);
849 av_assert0((hi-low) > 0);
850
851 if (hi - low == 1)
852 return low;
853
854 mid = (low + hi) / 2;
855
856 if (x < data[mid])
857 hi = mid;
858 else
859 low = mid;
860 }
861
862 return 0;
863 }
864
865 #define NEXT_FLOAT_OR_GOTO(value, label) \
866 if (!fget_next_word(line, sizeof(line) ,f)) { \
867 ret = AVERROR_INVALIDDATA; \
868 goto label; \
869 } \
870 if (av_sscanf(line, "%f", &value) != 1) { \
871 ret = AVERROR_INVALIDDATA; \
872 goto label; \
873 }
874
875 static int parse_cinespace(AVFilterContext *ctx, FILE *f)
876 {
877 LUT3DContext *lut3d = ctx->priv;
878 char line[MAX_LINE_SIZE];
879 float in_min[3] = {0.0, 0.0, 0.0};
880 float in_max[3] = {1.0, 1.0, 1.0};
881 float out_min[3] = {0.0, 0.0, 0.0};
882 float out_max[3] = {1.0, 1.0, 1.0};
883 int inside_metadata = 0, size, size2;
884 int prelut = 0;
885 int ret = 0;
886
887 int prelut_sizes[3] = {0, 0, 0};
888 float *in_prelut[3] = {NULL, NULL, NULL};
889 float *out_prelut[3] = {NULL, NULL, NULL};
890
891 NEXT_LINE_OR_GOTO(skip_line(line), end);
892 if (strncmp(line, "CSPLUTV100", 10)) {
893 av_log(ctx, AV_LOG_ERROR, "Not cineSpace LUT format\n");
894 ret = AVERROR(EINVAL);
895 goto end;
896 }
897
898 NEXT_LINE_OR_GOTO(skip_line(line), end);
899 if (strncmp(line, "3D", 2)) {
900 av_log(ctx, AV_LOG_ERROR, "Not 3D LUT format\n");
901 ret = AVERROR(EINVAL);
902 goto end;
903 }
904
905 while (1) {
906 NEXT_LINE_OR_GOTO(skip_line(line), end);
907
908 if (!strncmp(line, "BEGIN METADATA", 14)) {
909 inside_metadata = 1;
910 continue;
911 }
912 if (!strncmp(line, "END METADATA", 12)) {
913 inside_metadata = 0;
914 continue;
915 }
916 if (inside_metadata == 0) {
917 int size_r, size_g, size_b;
918
919 for (int i = 0; i < 3; i++) {
920 int npoints = strtol(line, NULL, 0);
921
922 if (npoints > 2) {
923 float v,last;
924
925 if (npoints > PRELUT_SIZE) {
926 av_log(ctx, AV_LOG_ERROR, "Prelut size too large.\n");
927 ret = AVERROR_INVALIDDATA;
928 goto end;
929 }
930
931 if (in_prelut[i] || out_prelut[i]) {
932 av_log(ctx, AV_LOG_ERROR, "Invalid file has multiple preluts.\n");
933 ret = AVERROR_INVALIDDATA;
934 goto end;
935 }
936
937 in_prelut[i] = (float*)av_malloc(npoints * sizeof(float));
938 out_prelut[i] = (float*)av_malloc(npoints * sizeof(float));
939 if (!in_prelut[i] || !out_prelut[i]) {
940 ret = AVERROR(ENOMEM);
941 goto end;
942 }
943
944 prelut_sizes[i] = npoints;
945 in_min[i] = FLT_MAX;
946 in_max[i] = -FLT_MAX;
947 out_min[i] = FLT_MAX;
948 out_max[i] = -FLT_MAX;
949
950 for (int j = 0; j < npoints; j++) {
951 NEXT_FLOAT_OR_GOTO(v, end)
952 in_min[i] = FFMIN(in_min[i], v);
953 in_max[i] = FFMAX(in_max[i], v);
954 in_prelut[i][j] = v;
955 if (j > 0 && v < last) {
956 av_log(ctx, AV_LOG_ERROR, "Invalid file, non increasing prelut.\n");
957 ret = AVERROR(ENOMEM);
958 goto end;
959 }
960 last = v;
961 }
962
963 for (int j = 0; j < npoints; j++) {
964 NEXT_FLOAT_OR_GOTO(v, end)
965 out_min[i] = FFMIN(out_min[i], v);
966 out_max[i] = FFMAX(out_max[i], v);
967 out_prelut[i][j] = v;
968 }
969
970 } else if (npoints == 2) {
971 NEXT_LINE_OR_GOTO(skip_line(line), end);
972 if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2) {
973 ret = AVERROR_INVALIDDATA;
974 goto end;
975 }
976 NEXT_LINE_OR_GOTO(skip_line(line), end);
977 if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2) {
978 ret = AVERROR_INVALIDDATA;
979 goto end;
980 }
981
982 } else {
983 av_log(ctx, AV_LOG_ERROR, "Unsupported number of pre-lut points.\n");
984 ret = AVERROR_PATCHWELCOME;
985 goto end;
986 }
987
988 NEXT_LINE_OR_GOTO(skip_line(line), end);
989 }
990
991 if (av_sscanf(line, "%d %d %d", &size_r, &size_g, &size_b) != 3) {
992 ret = AVERROR(EINVAL);
993 goto end;
994 }
995 if (size_r != size_g || size_r != size_b) {
996 av_log(ctx, AV_LOG_ERROR, "Unsupported size combination: %dx%dx%d.\n", size_r, size_g, size_b);
997 ret = AVERROR_PATCHWELCOME;
998 goto end;
999 }
1000
1001 size = size_r;
1002 size2 = size * size;
1003
1004 if (prelut_sizes[0] && prelut_sizes[1] && prelut_sizes[2])
1005 prelut = 1;
1006
1007 ret = allocate_3dlut(ctx, size, prelut);
1008 if (ret < 0)
1009 return ret;
1010
1011 for (int k = 0; k < size; k++) {
1012 for (int j = 0; j < size; j++) {
1013 for (int i = 0; i < size; i++) {
1014 struct rgbvec *vec = &lut3d->lut[i * size2 + j * size + k];
1015
1016 NEXT_LINE_OR_GOTO(skip_line(line), end);
1017 if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) {
1018 ret = AVERROR_INVALIDDATA;
1019 goto end;
1020 }
1021
1022 vec->r *= out_max[0] - out_min[0];
1023 vec->g *= out_max[1] - out_min[1];
1024 vec->b *= out_max[2] - out_min[2];
1025 }
1026 }
1027 }
1028
1029 break;
1030 }
1031 }
1032
1033 if (prelut) {
1034 for (int c = 0; c < 3; c++) {
1035
1036 lut3d->prelut.min[c] = in_min[c];
1037 lut3d->prelut.max[c] = in_max[c];
1038 lut3d->prelut.scale[c] = (1.0f / (float)(in_max[c] - in_min[c])) * (lut3d->prelut.size - 1);
1039
1040 for (int i = 0; i < lut3d->prelut.size; ++i) {
1041 float mix = (float) i / (float)(lut3d->prelut.size - 1);
1042 float x = lerpf(in_min[c], in_max[c], mix), a, b;
1043
1044 int idx = nearest_sample_index(in_prelut[c], x, 0, prelut_sizes[c]-1);
1045 av_assert0(idx + 1 < prelut_sizes[c]);
1046
1047 a = out_prelut[c][idx + 0];
1048 b = out_prelut[c][idx + 1];
1049 mix = x - in_prelut[c][idx];
1050
1051 lut3d->prelut.lut[c][i] = sanitizef(lerpf(a, b, mix));
1052 }
1053 }
1054 lut3d->scale.r = 1.00f;
1055 lut3d->scale.g = 1.00f;
1056 lut3d->scale.b = 1.00f;
1057
1058 } else {
1059 lut3d->scale.r = av_clipf(1. / (in_max[0] - in_min[0]), 0.f, 1.f);
1060 lut3d->scale.g = av_clipf(1. / (in_max[1] - in_min[1]), 0.f, 1.f);
1061 lut3d->scale.b = av_clipf(1. / (in_max[2] - in_min[2]), 0.f, 1.f);
1062 }
1063
1064 end:
1065 for (int c = 0; c < 3; c++) {
1066 av_freep(&in_prelut[c]);
1067 av_freep(&out_prelut[c]);
1068 }
1069 return ret;
1070 }
1071
1072 static int set_identity_matrix(AVFilterContext *ctx, int size)
1073 {
1074 LUT3DContext *lut3d = ctx->priv;
1075 int ret, i, j, k;
1076 const int size2 = size * size;
1077 const float c = 1. / (size - 1);
1078
1079 ret = allocate_3dlut(ctx, size, 0);
1080 if (ret < 0)
1081 return ret;
1082
1083 for (k = 0; k < size; k++) {
1084 for (j = 0; j < size; j++) {
1085 for (i = 0; i < size; i++) {
1086 struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
1087 vec->r = k * c;
1088 vec->g = j * c;
1089 vec->b = i * c;
1090 }
1091 }
1092 }
1093
1094 return 0;
1095 }
1096
1097 static const enum AVPixelFormat pix_fmts[] = {
1098 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
1099 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
1100 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
1101 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
1102 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
1103 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
1104 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
1105 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
1106 AV_PIX_FMT_GBRP9,
1107 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
1108 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
1109 AV_PIX_FMT_GBRP14,
1110 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
1111 AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
1112 AV_PIX_FMT_NONE
1113 };
1114
1115 #if CONFIG_LUT3D_FILTER || CONFIG_HALDCLUT_FILTER
1116
1117 static int config_input(AVFilterLink *inlink)
1118 {
1119 int depth, is16bit, isfloat, planar;
1120 LUT3DContext *lut3d = inlink->dst->priv;
1121 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
1122
1123 depth = desc->comp[0].depth;
1124 is16bit = desc->comp[0].depth > 8;
1125 planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
1126 isfloat = desc->flags & AV_PIX_FMT_FLAG_FLOAT;
1127 ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
1128 lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
1129
1130 #define SET_FUNC(name) do { \
1131 if (planar && !isfloat) { \
1132 switch (depth) { \
1133 case 8: lut3d->interp = interp_8_##name##_p8; break; \
1134 case 9: lut3d->interp = interp_16_##name##_p9; break; \
1135 case 10: lut3d->interp = interp_16_##name##_p10; break; \
1136 case 12: lut3d->interp = interp_16_##name##_p12; break; \
1137 case 14: lut3d->interp = interp_16_##name##_p14; break; \
1138 case 16: lut3d->interp = interp_16_##name##_p16; break; \
1139 } \
1140 } else if (isfloat) { lut3d->interp = interp_##name##_pf32; \
1141 } else if (is16bit) { lut3d->interp = interp_16_##name; \
1142 } else { lut3d->interp = interp_8_##name; } \
1143 } while (0)
1144
1145 switch (lut3d->interpolation) {
1146 case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
1147 case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
1148 case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
1149 case INTERPOLATE_PYRAMID: SET_FUNC(pyramid); break;
1150 case INTERPOLATE_PRISM: SET_FUNC(prism); break;
1151 default:
1152 av_assert0(0);
1153 }
1154
1155 #if ARCH_X86
1156 ff_lut3d_init_x86(lut3d, desc);
1157 #endif
1158
1159 return 0;
1160 }
1161
1162 static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
1163 {
1164 AVFilterContext *ctx = inlink->dst;
1165 LUT3DContext *lut3d = ctx->priv;
1166 AVFilterLink *outlink = inlink->dst->outputs[0];
1167 AVFrame *out;
1168 ThreadData td;
1169
1170 if (av_frame_is_writable(in)) {
1171 out = in;
1172 } else {
1173 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1174 if (!out) {
1175 av_frame_free(&in);
1176 return NULL;
1177 }
1178 av_frame_copy_props(out, in);
1179 }
1180
1181 td.in = in;
1182 td.out = out;
1183 ff_filter_execute(ctx, lut3d->interp, &td, NULL,
1184 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1185
1186 if (out != in)
1187 av_frame_free(&in);
1188
1189 return out;
1190 }
1191
1192 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
1193 {
1194 AVFilterLink *outlink = inlink->dst->outputs[0];
1195 AVFrame *out = apply_lut(inlink, in);
1196 if (!out)
1197 return AVERROR(ENOMEM);
1198 return ff_filter_frame(outlink, out);
1199 }
1200
1201 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1202 char *res, int res_len, int flags)
1203 {
1204 int ret;
1205
1206 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1207 if (ret < 0)
1208 return ret;
1209
1210 return config_input(ctx->inputs[0]);
1211 }
1212
1213 /* These options are shared between several filters;
1214 * &lut3d_haldclut_options[COMMON_OPTIONS_OFFSET] must always
1215 * point to the first of the COMMON_OPTIONS. */
1216 #define COMMON_OPTIONS_OFFSET CONFIG_LUT3D_FILTER
1217 static const AVOption lut3d_haldclut_options[] = {
1218 #if CONFIG_LUT3D_FILTER
1219 { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
1220 #endif
1221 #if CONFIG_HALDCLUT_FILTER
1222 { "clut", "when to process CLUT", OFFSET(clut), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, .flags = TFLAGS, .unit = "clut" },
1223 { "first", "process only first CLUT, ignore rest", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = TFLAGS, .unit = "clut" },
1224 { "all", "process all CLUTs", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = TFLAGS, .unit = "clut" },
1225 #endif
1226 COMMON_OPTIONS
1227 };
1228
1229 #if CONFIG_LUT3D_FILTER
1230
1231 AVFILTER_DEFINE_CLASS_EXT(lut3d, "lut3d", lut3d_haldclut_options);
1232
1233 static av_cold int lut3d_init(AVFilterContext *ctx)
1234 {
1235 int ret;
1236 FILE *f;
1237 const char *ext;
1238 LUT3DContext *lut3d = ctx->priv;
1239
1240 lut3d->scale.r = lut3d->scale.g = lut3d->scale.b = 1.f;
1241
1242 if (!lut3d->file) {
1243 return set_identity_matrix(ctx, 32);
1244 }
1245
1246 f = avpriv_fopen_utf8(lut3d->file, "r");
1247 if (!f) {
1248 ret = AVERROR(errno);
1249 av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
1250 return ret;
1251 }
1252
1253 ext = strrchr(lut3d->file, '.');
1254 if (!ext) {
1255 av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
1256 ret = AVERROR_INVALIDDATA;
1257 goto end;
1258 }
1259 ext++;
1260
1261 if (!av_strcasecmp(ext, "dat")) {
1262 ret = parse_dat(ctx, f);
1263 } else if (!av_strcasecmp(ext, "3dl")) {
1264 ret = parse_3dl(ctx, f);
1265 } else if (!av_strcasecmp(ext, "cube")) {
1266 ret = parse_cube(ctx, f);
1267 } else if (!av_strcasecmp(ext, "m3d")) {
1268 ret = parse_m3d(ctx, f);
1269 } else if (!av_strcasecmp(ext, "csp")) {
1270 ret = parse_cinespace(ctx, f);
1271 } else {
1272 av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
1273 ret = AVERROR(EINVAL);
1274 }
1275
1276 if (!ret && !lut3d->lutsize) {
1277 av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
1278 ret = AVERROR_INVALIDDATA;
1279 }
1280
1281 end:
1282 fclose(f);
1283 return ret;
1284 }
1285
1286 static av_cold void lut3d_uninit(AVFilterContext *ctx)
1287 {
1288 LUT3DContext *lut3d = ctx->priv;
1289 int i;
1290 av_freep(&lut3d->lut);
1291
1292 for (i = 0; i < 3; i++) {
1293 av_freep(&lut3d->prelut.lut[i]);
1294 }
1295 }
1296
1297 static const AVFilterPad lut3d_inputs[] = {
1298 {
1299 .name = "default",
1300 .type = AVMEDIA_TYPE_VIDEO,
1301 .filter_frame = filter_frame,
1302 .config_props = config_input,
1303 },
1304 };
1305
1306 const AVFilter ff_vf_lut3d = {
1307 .name = "lut3d",
1308 .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
1309 .priv_size = sizeof(LUT3DContext),
1310 .init = lut3d_init,
1311 .uninit = lut3d_uninit,
1312 FILTER_INPUTS(lut3d_inputs),
1313 FILTER_OUTPUTS(ff_video_default_filterpad),
1314 FILTER_PIXFMTS_ARRAY(pix_fmts),
1315 .priv_class = &lut3d_class,
1316 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
1317 .process_command = process_command,
1318 };
1319 #endif
1320
1321 #if CONFIG_HALDCLUT_FILTER
1322
1323 static void update_clut_packed(LUT3DContext *lut3d, const AVFrame *frame)
1324 {
1325 const uint8_t *data = frame->data[0];
1326 const ptrdiff_t linesize = frame->linesize[0];
1327 const int w = lut3d->clut_width;
1328 const int step = lut3d->clut_step;
1329 const uint8_t *rgba_map = lut3d->clut_rgba_map;
1330 const int level = lut3d->lutsize;
1331 const int level2 = lut3d->lutsize2;
1332
1333 #define LOAD_CLUT(nbits) do { \
1334 int i, j, k, x = 0, y = 0; \
1335 \
1336 for (k = 0; k < level; k++) { \
1337 for (j = 0; j < level; j++) { \
1338 for (i = 0; i < level; i++) { \
1339 const uint##nbits##_t *src = (const uint##nbits##_t *) \
1340 (data + y*linesize + x*step); \
1341 struct rgbvec *vec = &lut3d->lut[i * level2 + j * level + k]; \
1342 vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
1343 vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
1344 vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
1345 if (++x == w) { \
1346 x = 0; \
1347 y++; \
1348 } \
1349 } \
1350 } \
1351 } \
1352 } while (0)
1353
1354 switch (lut3d->clut_bits) {
1355 case 8: LOAD_CLUT(8); break;
1356 case 16: LOAD_CLUT(16); break;
1357 }
1358 }
1359
1360 static void update_clut_planar(LUT3DContext *lut3d, const AVFrame *frame)
1361 {
1362 const uint8_t *datag = frame->data[0];
1363 const uint8_t *datab = frame->data[1];
1364 const uint8_t *datar = frame->data[2];
1365 const ptrdiff_t glinesize = frame->linesize[0];
1366 const ptrdiff_t blinesize = frame->linesize[1];
1367 const ptrdiff_t rlinesize = frame->linesize[2];
1368 const int w = lut3d->clut_width;
1369 const int level = lut3d->lutsize;
1370 const int level2 = lut3d->lutsize2;
1371
1372 #define LOAD_CLUT_PLANAR(nbits, depth) do { \
1373 int i, j, k, x = 0, y = 0; \
1374 \
1375 for (k = 0; k < level; k++) { \
1376 for (j = 0; j < level; j++) { \
1377 for (i = 0; i < level; i++) { \
1378 const uint##nbits##_t *gsrc = (const uint##nbits##_t *) \
1379 (datag + y*glinesize); \
1380 const uint##nbits##_t *bsrc = (const uint##nbits##_t *) \
1381 (datab + y*blinesize); \
1382 const uint##nbits##_t *rsrc = (const uint##nbits##_t *) \
1383 (datar + y*rlinesize); \
1384 struct rgbvec *vec = &lut3d->lut[i * level2 + j * level + k]; \
1385 vec->r = gsrc[x] / (float)((1<<(depth)) - 1); \
1386 vec->g = bsrc[x] / (float)((1<<(depth)) - 1); \
1387 vec->b = rsrc[x] / (float)((1<<(depth)) - 1); \
1388 if (++x == w) { \
1389 x = 0; \
1390 y++; \
1391 } \
1392 } \
1393 } \
1394 } \
1395 } while (0)
1396
1397 switch (lut3d->clut_bits) {
1398 case 8: LOAD_CLUT_PLANAR(8, 8); break;
1399 case 9: LOAD_CLUT_PLANAR(16, 9); break;
1400 case 10: LOAD_CLUT_PLANAR(16, 10); break;
1401 case 12: LOAD_CLUT_PLANAR(16, 12); break;
1402 case 14: LOAD_CLUT_PLANAR(16, 14); break;
1403 case 16: LOAD_CLUT_PLANAR(16, 16); break;
1404 }
1405 }
1406
1407 static void update_clut_float(LUT3DContext *lut3d, const AVFrame *frame)
1408 {
1409 const uint8_t *datag = frame->data[0];
1410 const uint8_t *datab = frame->data[1];
1411 const uint8_t *datar = frame->data[2];
1412 const ptrdiff_t glinesize = frame->linesize[0];
1413 const ptrdiff_t blinesize = frame->linesize[1];
1414 const ptrdiff_t rlinesize = frame->linesize[2];
1415 const int w = lut3d->clut_width;
1416 const int level = lut3d->lutsize;
1417 const int level2 = lut3d->lutsize2;
1418
1419 int i, j, k, x = 0, y = 0;
1420
1421 for (k = 0; k < level; k++) {
1422 for (j = 0; j < level; j++) {
1423 for (i = 0; i < level; i++) {
1424 const float *gsrc = (const float *)(datag + y*glinesize);
1425 const float *bsrc = (const float *)(datab + y*blinesize);
1426 const float *rsrc = (const float *)(datar + y*rlinesize);
1427 struct rgbvec *vec = &lut3d->lut[i * level2 + j * level + k];
1428 vec->r = rsrc[x];
1429 vec->g = gsrc[x];
1430 vec->b = bsrc[x];
1431 if (++x == w) {
1432 x = 0;
1433 y++;
1434 }
1435 }
1436 }
1437 }
1438 }
1439
1440 static int config_output(AVFilterLink *outlink)
1441 {
1442 AVFilterContext *ctx = outlink->src;
1443 LUT3DContext *lut3d = ctx->priv;
1444 int ret;
1445
1446 ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
1447 if (ret < 0)
1448 return ret;
1449 outlink->w = ctx->inputs[0]->w;
1450 outlink->h = ctx->inputs[0]->h;
1451 outlink->time_base = ctx->inputs[0]->time_base;
1452 if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
1453 return ret;
1454 return 0;
1455 }
1456
1457 static int activate(AVFilterContext *ctx)
1458 {
1459 LUT3DContext *s = ctx->priv;
1460 return ff_framesync_activate(&s->fs);
1461 }
1462
1463 static int config_clut(AVFilterLink *inlink)
1464 {
1465 int size, level, w, h;
1466 AVFilterContext *ctx = inlink->dst;
1467 LUT3DContext *lut3d = ctx->priv;
1468 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
1469
1470 av_assert0(desc);
1471
1472 lut3d->clut_bits = desc->comp[0].depth;
1473 lut3d->clut_planar = av_pix_fmt_count_planes(inlink->format) > 1;
1474 lut3d->clut_float = desc->flags & AV_PIX_FMT_FLAG_FLOAT;
1475
1476 lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
1477 ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
1478
1479 if (inlink->w > inlink->h)
1480 av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
1481 "Hald CLUT will be ignored\n", inlink->w - inlink->h);
1482 else if (inlink->w < inlink->h)
1483 av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
1484 "Hald CLUT will be ignored\n", inlink->h - inlink->w);
1485 lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
1486
1487 for (level = 1; level*level*level < w; level++);
1488 size = level*level*level;
1489 if (size != w) {
1490 av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
1491 return AVERROR_INVALIDDATA;
1492 }
1493 av_assert0(w == h && w == size);
1494 level *= level;
1495 if (level > MAX_LEVEL) {
1496 const int max_clut_level = sqrt(MAX_LEVEL);
1497 const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
1498 av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
1499 "(maximum level is %d, or %dx%d CLUT)\n",
1500 max_clut_level, max_clut_size, max_clut_size);
1501 return AVERROR(EINVAL);
1502 }
1503
1504 return allocate_3dlut(ctx, level, 0);
1505 }
1506
1507 static int update_apply_clut(FFFrameSync *fs)
1508 {
1509 AVFilterContext *ctx = fs->parent;
1510 LUT3DContext *lut3d = ctx->priv;
1511 AVFilterLink *inlink = ctx->inputs[0];
1512 AVFrame *master, *second, *out;
1513 int ret;
1514
1515 ret = ff_framesync_dualinput_get(fs, &master, &second);
1516 if (ret < 0)
1517 return ret;
1518 if (!second)
1519 return ff_filter_frame(ctx->outputs[0], master);
1520 if (lut3d->clut || !lut3d->got_clut) {
1521 if (lut3d->clut_float)
1522 update_clut_float(ctx->priv, second);
1523 else if (lut3d->clut_planar)
1524 update_clut_planar(ctx->priv, second);
1525 else
1526 update_clut_packed(ctx->priv, second);
1527 lut3d->got_clut = 1;
1528 }
1529 out = apply_lut(inlink, master);
1530 return ff_filter_frame(ctx->outputs[0], out);
1531 }
1532
1533 static av_cold int haldclut_init(AVFilterContext *ctx)
1534 {
1535 LUT3DContext *lut3d = ctx->priv;
1536 lut3d->scale.r = lut3d->scale.g = lut3d->scale.b = 1.f;
1537 lut3d->fs.on_event = update_apply_clut;
1538 return 0;
1539 }
1540
1541 static av_cold void haldclut_uninit(AVFilterContext *ctx)
1542 {
1543 LUT3DContext *lut3d = ctx->priv;
1544 ff_framesync_uninit(&lut3d->fs);
1545 av_freep(&lut3d->lut);
1546 }
1547
1548 FRAMESYNC_DEFINE_CLASS_EXT(haldclut, LUT3DContext, fs,
1549 &lut3d_haldclut_options[COMMON_OPTIONS_OFFSET]);
1550
1551 static const AVFilterPad haldclut_inputs[] = {
1552 {
1553 .name = "main",
1554 .type = AVMEDIA_TYPE_VIDEO,
1555 .config_props = config_input,
1556 },{
1557 .name = "clut",
1558 .type = AVMEDIA_TYPE_VIDEO,
1559 .config_props = config_clut,
1560 },
1561 };
1562
1563 static const AVFilterPad haldclut_outputs[] = {
1564 {
1565 .name = "default",
1566 .type = AVMEDIA_TYPE_VIDEO,
1567 .config_props = config_output,
1568 },
1569 };
1570
1571 const AVFilter ff_vf_haldclut = {
1572 .name = "haldclut",
1573 .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
1574 .priv_size = sizeof(LUT3DContext),
1575 .preinit = haldclut_framesync_preinit,
1576 .init = haldclut_init,
1577 .uninit = haldclut_uninit,
1578 .activate = activate,
1579 FILTER_INPUTS(haldclut_inputs),
1580 FILTER_OUTPUTS(haldclut_outputs),
1581 FILTER_PIXFMTS_ARRAY(pix_fmts),
1582 .priv_class = &haldclut_class,
1583 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
1584 .process_command = process_command,
1585 };
1586 #endif
1587
1588 #endif /* CONFIG_LUT3D_FILTER || CONFIG_HALDCLUT_FILTER */
1589
1590 #if CONFIG_LUT1D_FILTER
1591
1592 enum interp_1d_mode {
1593 INTERPOLATE_1D_NEAREST,
1594 INTERPOLATE_1D_LINEAR,
1595 INTERPOLATE_1D_CUBIC,
1596 INTERPOLATE_1D_COSINE,
1597 INTERPOLATE_1D_SPLINE,
1598 NB_INTERP_1D_MODE
1599 };
1600
1601 #define MAX_1D_LEVEL 65536
1602
1603 typedef struct LUT1DContext {
1604 const AVClass *class;
1605 char *file;
1606 int interpolation; ///<interp_1d_mode
1607 struct rgbvec scale;
1608 uint8_t rgba_map[4];
1609 int step;
1610 float lut[3][MAX_1D_LEVEL];
1611 int lutsize;
1612 avfilter_action_func *interp;
1613 } LUT1DContext;
1614
1615 #undef OFFSET
1616 #define OFFSET(x) offsetof(LUT1DContext, x)
1617
1618 static void set_identity_matrix_1d(LUT1DContext *lut1d, int size)
1619 {
1620 const float c = 1. / (size - 1);
1621 int i;
1622
1623 lut1d->lutsize = size;
1624 for (i = 0; i < size; i++) {
1625 lut1d->lut[0][i] = i * c;
1626 lut1d->lut[1][i] = i * c;
1627 lut1d->lut[2][i] = i * c;
1628 }
1629 }
1630
1631 static int parse_cinespace_1d(AVFilterContext *ctx, FILE *f)
1632 {
1633 LUT1DContext *lut1d = ctx->priv;
1634 char line[MAX_LINE_SIZE];
1635 float in_min[3] = {0.0, 0.0, 0.0};
1636 float in_max[3] = {1.0, 1.0, 1.0};
1637 float out_min[3] = {0.0, 0.0, 0.0};
1638 float out_max[3] = {1.0, 1.0, 1.0};
1639 int inside_metadata = 0, size;
1640
1641 NEXT_LINE(skip_line(line));
1642 if (strncmp(line, "CSPLUTV100", 10)) {
1643 av_log(ctx, AV_LOG_ERROR, "Not cineSpace LUT format\n");
1644 return AVERROR(EINVAL);
1645 }
1646
1647 NEXT_LINE(skip_line(line));
1648 if (strncmp(line, "1D", 2)) {
1649 av_log(ctx, AV_LOG_ERROR, "Not 1D LUT format\n");
1650 return AVERROR(EINVAL);
1651 }
1652
1653 while (1) {
1654 NEXT_LINE(skip_line(line));
1655
1656 if (!strncmp(line, "BEGIN METADATA", 14)) {
1657 inside_metadata = 1;
1658 continue;
1659 }
1660 if (!strncmp(line, "END METADATA", 12)) {
1661 inside_metadata = 0;
1662 continue;
1663 }
1664 if (inside_metadata == 0) {
1665 for (int i = 0; i < 3; i++) {
1666 int npoints = strtol(line, NULL, 0);
1667
1668 if (npoints != 2) {
1669 av_log(ctx, AV_LOG_ERROR, "Unsupported number of pre-lut points.\n");
1670 return AVERROR_PATCHWELCOME;
1671 }
1672
1673 NEXT_LINE(skip_line(line));
1674 if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2)
1675 return AVERROR_INVALIDDATA;
1676 NEXT_LINE(skip_line(line));
1677 if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2)
1678 return AVERROR_INVALIDDATA;
1679 NEXT_LINE(skip_line(line));
1680 }
1681
1682 size = strtol(line, NULL, 0);
1683
1684 if (size < 2 || size > MAX_1D_LEVEL) {
1685 av_log(ctx, AV_LOG_ERROR, "Too large or invalid 1D LUT size\n");
1686 return AVERROR(EINVAL);
1687 }
1688
1689 lut1d->lutsize = size;
1690
1691 for (int i = 0; i < size; i++) {
1692 NEXT_LINE(skip_line(line));
1693 if (av_sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3)
1694 return AVERROR_INVALIDDATA;
1695 lut1d->lut[0][i] *= out_max[0] - out_min[0];
1696 lut1d->lut[1][i] *= out_max[1] - out_min[1];
1697 lut1d->lut[2][i] *= out_max[2] - out_min[2];
1698 }
1699
1700 break;
1701 }
1702 }
1703
1704 lut1d->scale.r = av_clipf(1. / (in_max[0] - in_min[0]), 0.f, 1.f);
1705 lut1d->scale.g = av_clipf(1. / (in_max[1] - in_min[1]), 0.f, 1.f);
1706 lut1d->scale.b = av_clipf(1. / (in_max[2] - in_min[2]), 0.f, 1.f);
1707
1708 return 0;
1709 }
1710
1711 static int parse_cube_1d(AVFilterContext *ctx, FILE *f)
1712 {
1713 LUT1DContext *lut1d = ctx->priv;
1714 char line[MAX_LINE_SIZE];
1715 float min[3] = {0.0, 0.0, 0.0};
1716 float max[3] = {1.0, 1.0, 1.0};
1717
1718 while (fgets(line, sizeof(line), f)) {
1719 if (!strncmp(line, "LUT_1D_SIZE", 11)) {
1720 const int size = strtol(line + 12, NULL, 0);
1721 int i;
1722
1723 if (size < 2 || size > MAX_1D_LEVEL) {
1724 av_log(ctx, AV_LOG_ERROR, "Too large or invalid 1D LUT size\n");
1725 return AVERROR(EINVAL);
1726 }
1727 lut1d->lutsize = size;
1728 for (i = 0; i < size; i++) {
1729 do {
1730 try_again:
1731 NEXT_LINE(0);
1732 if (!strncmp(line, "DOMAIN_", 7)) {
1733 float *vals = NULL;
1734 if (!strncmp(line + 7, "MIN ", 4)) vals = min;
1735 else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
1736 if (!vals)
1737 return AVERROR_INVALIDDATA;
1738 if (av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2) != 3)
1739 return AVERROR_INVALIDDATA;
1740 av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
1741 min[0], min[1], min[2], max[0], max[1], max[2]);
1742 goto try_again;
1743 } else if (!strncmp(line, "LUT_1D_INPUT_RANGE ", 19)) {
1744 if (av_sscanf(line + 19, "%f %f", min, max) != 2)
1745 return AVERROR_INVALIDDATA;
1746 min[1] = min[2] = min[0];
1747 max[1] = max[2] = max[0];
1748 goto try_again;
1749 } else if (!strncmp(line, "TITLE", 5)) {
1750 goto try_again;
1751 }
1752 } while (skip_line(line));
1753 if (av_sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3)
1754 return AVERROR_INVALIDDATA;
1755 }
1756 break;
1757 }
1758 }
1759
1760 lut1d->scale.r = av_clipf(1. / (max[0] - min[0]), 0.f, 1.f);
1761 lut1d->scale.g = av_clipf(1. / (max[1] - min[1]), 0.f, 1.f);
1762 lut1d->scale.b = av_clipf(1. / (max[2] - min[2]), 0.f, 1.f);
1763
1764 return 0;
1765 }
1766
1767 static const AVOption lut1d_options[] = {
1768 { "file", "set 1D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = TFLAGS },
1769 { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_1D_LINEAR}, 0, NB_INTERP_1D_MODE-1, TFLAGS, .unit = "interp_mode" },
1770 { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_NEAREST}, 0, 0, TFLAGS, .unit = "interp_mode" },
1771 { "linear", "use values from the linear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_LINEAR}, 0, 0, TFLAGS, .unit = "interp_mode" },
1772 { "cosine", "use values from the cosine interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_COSINE}, 0, 0, TFLAGS, .unit = "interp_mode" },
1773 { "cubic", "use values from the cubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_CUBIC}, 0, 0, TFLAGS, .unit = "interp_mode" },
1774 { "spline", "use values from the spline interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_SPLINE}, 0, 0, TFLAGS, .unit = "interp_mode" },
1775 { NULL }
1776 };
1777
1778 AVFILTER_DEFINE_CLASS(lut1d);
1779
1780 static inline float interp_1d_nearest(const LUT1DContext *lut1d,
1781 int idx, const float s)
1782 {
1783 return lut1d->lut[idx][NEAR(s)];
1784 }
1785
1786 #define NEXT1D(x) (FFMIN((int)(x) + 1, lut1d->lutsize - 1))
1787
1788 static inline float interp_1d_linear(const LUT1DContext *lut1d,
1789 int idx, const float s)
1790 {
1791 const int prev = PREV(s);
1792 const int next = NEXT1D(s);
1793 const float d = s - prev;
1794 const float p = lut1d->lut[idx][prev];
1795 const float n = lut1d->lut[idx][next];
1796
1797 return lerpf(p, n, d);
1798 }
1799
1800 static inline float interp_1d_cosine(const LUT1DContext *lut1d,
1801 int idx, const float s)
1802 {
1803 const int prev = PREV(s);
1804 const int next = NEXT1D(s);
1805 const float d = s - prev;
1806 const float p = lut1d->lut[idx][prev];
1807 const float n = lut1d->lut[idx][next];
1808 const float m = (1.f - cosf(d * M_PI)) * .5f;
1809
1810 return lerpf(p, n, m);
1811 }
1812
1813 static inline float interp_1d_cubic(const LUT1DContext *lut1d,
1814 int idx, const float s)
1815 {
1816 const int prev = PREV(s);
1817 const int next = NEXT1D(s);
1818 const float mu = s - prev;
1819 float a0, a1, a2, a3, mu2;
1820
1821 float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
1822 float y1 = lut1d->lut[idx][prev];
1823 float y2 = lut1d->lut[idx][next];
1824 float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
1825
1826
1827 mu2 = mu * mu;
1828 a0 = y3 - y2 - y0 + y1;
1829 a1 = y0 - y1 - a0;
1830 a2 = y2 - y0;
1831 a3 = y1;
1832
1833 return a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3;
1834 }
1835
1836 static inline float interp_1d_spline(const LUT1DContext *lut1d,
1837 int idx, const float s)
1838 {
1839 const int prev = PREV(s);
1840 const int next = NEXT1D(s);
1841 const float x = s - prev;
1842 float c0, c1, c2, c3;
1843
1844 float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
1845 float y1 = lut1d->lut[idx][prev];
1846 float y2 = lut1d->lut[idx][next];
1847 float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
1848
1849 c0 = y1;
1850 c1 = .5f * (y2 - y0);
1851 c2 = y0 - 2.5f * y1 + 2.f * y2 - .5f * y3;
1852 c3 = .5f * (y3 - y0) + 1.5f * (y1 - y2);
1853
1854 return ((c3 * x + c2) * x + c1) * x + c0;
1855 }
1856
1857 #define DEFINE_INTERP_FUNC_PLANAR_1D(name, nbits, depth) \
1858 static int interp_1d_##nbits##_##name##_p##depth(AVFilterContext *ctx, \
1859 void *arg, int jobnr, \
1860 int nb_jobs) \
1861 { \
1862 int x, y; \
1863 const LUT1DContext *lut1d = ctx->priv; \
1864 const ThreadData *td = arg; \
1865 const AVFrame *in = td->in; \
1866 const AVFrame *out = td->out; \
1867 const int direct = out == in; \
1868 const int slice_start = (in->height * jobnr ) / nb_jobs; \
1869 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
1870 uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
1871 uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
1872 uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
1873 uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
1874 const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
1875 const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
1876 const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
1877 const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
1878 const float factor = (1 << depth) - 1; \
1879 const float scale_r = (lut1d->scale.r / factor) * (lut1d->lutsize - 1); \
1880 const float scale_g = (lut1d->scale.g / factor) * (lut1d->lutsize - 1); \
1881 const float scale_b = (lut1d->scale.b / factor) * (lut1d->lutsize - 1); \
1882 \
1883 for (y = slice_start; y < slice_end; y++) { \
1884 uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
1885 uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
1886 uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
1887 uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
1888 const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
1889 const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
1890 const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
1891 const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
1892 for (x = 0; x < in->width; x++) { \
1893 float r = srcr[x] * scale_r; \
1894 float g = srcg[x] * scale_g; \
1895 float b = srcb[x] * scale_b; \
1896 r = interp_1d_##name(lut1d, 0, r); \
1897 g = interp_1d_##name(lut1d, 1, g); \
1898 b = interp_1d_##name(lut1d, 2, b); \
1899 dstr[x] = av_clip_uintp2(r * factor, depth); \
1900 dstg[x] = av_clip_uintp2(g * factor, depth); \
1901 dstb[x] = av_clip_uintp2(b * factor, depth); \
1902 if (!direct && in->linesize[3]) \
1903 dsta[x] = srca[x]; \
1904 } \
1905 grow += out->linesize[0]; \
1906 brow += out->linesize[1]; \
1907 rrow += out->linesize[2]; \
1908 arow += out->linesize[3]; \
1909 srcgrow += in->linesize[0]; \
1910 srcbrow += in->linesize[1]; \
1911 srcrrow += in->linesize[2]; \
1912 srcarow += in->linesize[3]; \
1913 } \
1914 return 0; \
1915 }
1916
1917 DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 8, 8)
1918 DEFINE_INTERP_FUNC_PLANAR_1D(linear, 8, 8)
1919 DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 8, 8)
1920 DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 8, 8)
1921 DEFINE_INTERP_FUNC_PLANAR_1D(spline, 8, 8)
1922
1923 DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 9)
1924 DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 9)
1925 DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 9)
1926 DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 9)
1927 DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 9)
1928
1929 DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 10)
1930 DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 10)
1931 DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 10)
1932 DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 10)
1933 DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 10)
1934
1935 DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 12)
1936 DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 12)
1937 DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 12)
1938 DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 12)
1939 DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 12)
1940
1941 DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 14)
1942 DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 14)
1943 DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 14)
1944 DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 14)
1945 DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 14)
1946
1947 DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 16)
1948 DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 16)
1949 DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 16)
1950 DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 16)
1951 DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 16)
1952
1953 #define DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(name, depth) \
1954 static int interp_1d_##name##_pf##depth(AVFilterContext *ctx, \
1955 void *arg, int jobnr, \
1956 int nb_jobs) \
1957 { \
1958 int x, y; \
1959 const LUT1DContext *lut1d = ctx->priv; \
1960 const ThreadData *td = arg; \
1961 const AVFrame *in = td->in; \
1962 const AVFrame *out = td->out; \
1963 const int direct = out == in; \
1964 const int slice_start = (in->height * jobnr ) / nb_jobs; \
1965 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
1966 uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
1967 uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
1968 uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
1969 uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
1970 const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
1971 const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
1972 const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
1973 const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
1974 const float lutsize = lut1d->lutsize - 1; \
1975 const float scale_r = lut1d->scale.r * lutsize; \
1976 const float scale_g = lut1d->scale.g * lutsize; \
1977 const float scale_b = lut1d->scale.b * lutsize; \
1978 \
1979 for (y = slice_start; y < slice_end; y++) { \
1980 float *dstg = (float *)grow; \
1981 float *dstb = (float *)brow; \
1982 float *dstr = (float *)rrow; \
1983 float *dsta = (float *)arow; \
1984 const float *srcg = (const float *)srcgrow; \
1985 const float *srcb = (const float *)srcbrow; \
1986 const float *srcr = (const float *)srcrrow; \
1987 const float *srca = (const float *)srcarow; \
1988 for (x = 0; x < in->width; x++) { \
1989 float r = av_clipf(sanitizef(srcr[x]) * scale_r, 0.0f, lutsize); \
1990 float g = av_clipf(sanitizef(srcg[x]) * scale_g, 0.0f, lutsize); \
1991 float b = av_clipf(sanitizef(srcb[x]) * scale_b, 0.0f, lutsize); \
1992 r = interp_1d_##name(lut1d, 0, r); \
1993 g = interp_1d_##name(lut1d, 1, g); \
1994 b = interp_1d_##name(lut1d, 2, b); \
1995 dstr[x] = r; \
1996 dstg[x] = g; \
1997 dstb[x] = b; \
1998 if (!direct && in->linesize[3]) \
1999 dsta[x] = srca[x]; \
2000 } \
2001 grow += out->linesize[0]; \
2002 brow += out->linesize[1]; \
2003 rrow += out->linesize[2]; \
2004 arow += out->linesize[3]; \
2005 srcgrow += in->linesize[0]; \
2006 srcbrow += in->linesize[1]; \
2007 srcrrow += in->linesize[2]; \
2008 srcarow += in->linesize[3]; \
2009 } \
2010 return 0; \
2011 }
2012
2013 DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(nearest, 32)
2014 DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(linear, 32)
2015 DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(cosine, 32)
2016 DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(cubic, 32)
2017 DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(spline, 32)
2018
2019 #define DEFINE_INTERP_FUNC_1D(name, nbits) \
2020 static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \
2021 int jobnr, int nb_jobs) \
2022 { \
2023 int x, y; \
2024 const LUT1DContext *lut1d = ctx->priv; \
2025 const ThreadData *td = arg; \
2026 const AVFrame *in = td->in; \
2027 const AVFrame *out = td->out; \
2028 const int direct = out == in; \
2029 const int step = lut1d->step; \
2030 const uint8_t r = lut1d->rgba_map[R]; \
2031 const uint8_t g = lut1d->rgba_map[G]; \
2032 const uint8_t b = lut1d->rgba_map[B]; \
2033 const uint8_t a = lut1d->rgba_map[A]; \
2034 const int slice_start = (in->height * jobnr ) / nb_jobs; \
2035 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
2036 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
2037 const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
2038 const float factor = (1 << nbits) - 1; \
2039 const float scale_r = (lut1d->scale.r / factor) * (lut1d->lutsize - 1); \
2040 const float scale_g = (lut1d->scale.g / factor) * (lut1d->lutsize - 1); \
2041 const float scale_b = (lut1d->scale.b / factor) * (lut1d->lutsize - 1); \
2042 \
2043 for (y = slice_start; y < slice_end; y++) { \
2044 uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
2045 const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
2046 for (x = 0; x < in->width * step; x += step) { \
2047 float rr = src[x + r] * scale_r; \
2048 float gg = src[x + g] * scale_g; \
2049 float bb = src[x + b] * scale_b; \
2050 rr = interp_1d_##name(lut1d, 0, rr); \
2051 gg = interp_1d_##name(lut1d, 1, gg); \
2052 bb = interp_1d_##name(lut1d, 2, bb); \
2053 dst[x + r] = av_clip_uint##nbits(rr * factor); \
2054 dst[x + g] = av_clip_uint##nbits(gg * factor); \
2055 dst[x + b] = av_clip_uint##nbits(bb * factor); \
2056 if (!direct && step == 4) \
2057 dst[x + a] = src[x + a]; \
2058 } \
2059 dstrow += out->linesize[0]; \
2060 srcrow += in ->linesize[0]; \
2061 } \
2062 return 0; \
2063 }
2064
2065 DEFINE_INTERP_FUNC_1D(nearest, 8)
2066 DEFINE_INTERP_FUNC_1D(linear, 8)
2067 DEFINE_INTERP_FUNC_1D(cosine, 8)
2068 DEFINE_INTERP_FUNC_1D(cubic, 8)
2069 DEFINE_INTERP_FUNC_1D(spline, 8)
2070
2071 DEFINE_INTERP_FUNC_1D(nearest, 16)
2072 DEFINE_INTERP_FUNC_1D(linear, 16)
2073 DEFINE_INTERP_FUNC_1D(cosine, 16)
2074 DEFINE_INTERP_FUNC_1D(cubic, 16)
2075 DEFINE_INTERP_FUNC_1D(spline, 16)
2076
2077 static int config_input_1d(AVFilterLink *inlink)
2078 {
2079 int depth, is16bit, isfloat, planar;
2080 LUT1DContext *lut1d = inlink->dst->priv;
2081 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2082
2083 depth = desc->comp[0].depth;
2084 is16bit = desc->comp[0].depth > 8;
2085 planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
2086 isfloat = desc->flags & AV_PIX_FMT_FLAG_FLOAT;
2087 ff_fill_rgba_map(lut1d->rgba_map, inlink->format);
2088 lut1d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
2089
2090 #define SET_FUNC_1D(name) do { \
2091 if (planar && !isfloat) { \
2092 switch (depth) { \
2093 case 8: lut1d->interp = interp_1d_8_##name##_p8; break; \
2094 case 9: lut1d->interp = interp_1d_16_##name##_p9; break; \
2095 case 10: lut1d->interp = interp_1d_16_##name##_p10; break; \
2096 case 12: lut1d->interp = interp_1d_16_##name##_p12; break; \
2097 case 14: lut1d->interp = interp_1d_16_##name##_p14; break; \
2098 case 16: lut1d->interp = interp_1d_16_##name##_p16; break; \
2099 } \
2100 } else if (isfloat) { lut1d->interp = interp_1d_##name##_pf32; \
2101 } else if (is16bit) { lut1d->interp = interp_1d_16_##name; \
2102 } else { lut1d->interp = interp_1d_8_##name; } \
2103 } while (0)
2104
2105 switch (lut1d->interpolation) {
2106 case INTERPOLATE_1D_NEAREST: SET_FUNC_1D(nearest); break;
2107 case INTERPOLATE_1D_LINEAR: SET_FUNC_1D(linear); break;
2108 case INTERPOLATE_1D_COSINE: SET_FUNC_1D(cosine); break;
2109 case INTERPOLATE_1D_CUBIC: SET_FUNC_1D(cubic); break;
2110 case INTERPOLATE_1D_SPLINE: SET_FUNC_1D(spline); break;
2111 default:
2112 av_assert0(0);
2113 }
2114
2115 return 0;
2116 }
2117
2118 static av_cold int lut1d_init(AVFilterContext *ctx)
2119 {
2120 int ret;
2121 FILE *f;
2122 const char *ext;
2123 LUT1DContext *lut1d = ctx->priv;
2124
2125 lut1d->scale.r = lut1d->scale.g = lut1d->scale.b = 1.f;
2126
2127 if (!lut1d->file) {
2128 set_identity_matrix_1d(lut1d, 32);
2129 return 0;
2130 }
2131
2132 f = avpriv_fopen_utf8(lut1d->file, "r");
2133 if (!f) {
2134 ret = AVERROR(errno);
2135 av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut1d->file, av_err2str(ret));
2136 return ret;
2137 }
2138
2139 ext = strrchr(lut1d->file, '.');
2140 if (!ext) {
2141 av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
2142 ret = AVERROR_INVALIDDATA;
2143 goto end;
2144 }
2145 ext++;
2146
2147 if (!av_strcasecmp(ext, "cube") || !av_strcasecmp(ext, "1dlut")) {
2148 ret = parse_cube_1d(ctx, f);
2149 } else if (!av_strcasecmp(ext, "csp")) {
2150 ret = parse_cinespace_1d(ctx, f);
2151 } else {
2152 av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
2153 ret = AVERROR(EINVAL);
2154 }
2155
2156 if (!ret && !lut1d->lutsize) {
2157 av_log(ctx, AV_LOG_ERROR, "1D LUT is empty\n");
2158 ret = AVERROR_INVALIDDATA;
2159 }
2160
2161 end:
2162 fclose(f);
2163 return ret;
2164 }
2165
2166 static AVFrame *apply_1d_lut(AVFilterLink *inlink, AVFrame *in)
2167 {
2168 AVFilterContext *ctx = inlink->dst;
2169 LUT1DContext *lut1d = ctx->priv;
2170 AVFilterLink *outlink = inlink->dst->outputs[0];
2171 AVFrame *out;
2172 ThreadData td;
2173
2174 if (av_frame_is_writable(in)) {
2175 out = in;
2176 } else {
2177 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2178 if (!out) {
2179 av_frame_free(&in);
2180 return NULL;
2181 }
2182 av_frame_copy_props(out, in);
2183 }
2184
2185 td.in = in;
2186 td.out = out;
2187 ff_filter_execute(ctx, lut1d->interp, &td, NULL,
2188 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2189
2190 if (out != in)
2191 av_frame_free(&in);
2192
2193 return out;
2194 }
2195
2196 static int filter_frame_1d(AVFilterLink *inlink, AVFrame *in)
2197 {
2198 AVFilterLink *outlink = inlink->dst->outputs[0];
2199 AVFrame *out = apply_1d_lut(inlink, in);
2200 if (!out)
2201 return AVERROR(ENOMEM);
2202 return ff_filter_frame(outlink, out);
2203 }
2204
2205 static int lut1d_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
2206 char *res, int res_len, int flags)
2207 {
2208 LUT1DContext *lut1d = ctx->priv;
2209 int ret;
2210
2211 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
2212 if (ret < 0)
2213 return ret;
2214
2215 ret = lut1d_init(ctx);
2216 if (ret < 0) {
2217 set_identity_matrix_1d(lut1d, 32);
2218 return ret;
2219 }
2220 return config_input_1d(ctx->inputs[0]);
2221 }
2222
2223 static const AVFilterPad lut1d_inputs[] = {
2224 {
2225 .name = "default",
2226 .type = AVMEDIA_TYPE_VIDEO,
2227 .filter_frame = filter_frame_1d,
2228 .config_props = config_input_1d,
2229 },
2230 };
2231
2232 const AVFilter ff_vf_lut1d = {
2233 .name = "lut1d",
2234 .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 1D LUT."),
2235 .priv_size = sizeof(LUT1DContext),
2236 .init = lut1d_init,
2237 FILTER_INPUTS(lut1d_inputs),
2238 FILTER_OUTPUTS(ff_video_default_filterpad),
2239 FILTER_PIXFMTS_ARRAY(pix_fmts),
2240 .priv_class = &lut1d_class,
2241 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
2242 .process_command = lut1d_process_command,
2243 };
2244 #endif
2245