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