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