FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_lut3d.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Clément Bœsch
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * 3D Lookup table filter
24  */
25 
26 #include "libavutil/opt.h"
27 #include "libavutil/file.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/avstring.h"
32 #include "avfilter.h"
33 #include "drawutils.h"
34 #include "formats.h"
35 #include "framesync.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 #define R 0
40 #define G 1
41 #define B 2
42 #define A 3
43 
49 };
50 
51 struct rgbvec {
52  float r, g, b;
53 };
54 
55 /* 3D LUT don't often go up to level 32, but it is common to have a Hald CLUT
56  * of 512x512 (64x64x64) */
57 #define MAX_LEVEL 64
58 
59 typedef struct LUT3DContext {
60  const AVClass *class;
61  int interpolation; ///<interp_mode
62  char *file;
64  int step;
67  int lutsize;
68 #if CONFIG_HALDCLUT_FILTER
69  uint8_t clut_rgba_map[4];
70  int clut_step;
71  int clut_is16bit;
72  int clut_width;
74 #endif
75 } LUT3DContext;
76 
77 typedef struct ThreadData {
78  AVFrame *in, *out;
79 } ThreadData;
80 
81 #define OFFSET(x) offsetof(LUT3DContext, x)
82 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
83 #define COMMON_OPTIONS \
84  { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
85  { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
86  { "trilinear", "interpolate values using the 8 points defining a cube", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TRILINEAR}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
87  { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
88  { NULL }
89 
90 static inline float lerpf(float v0, float v1, float f)
91 {
92  return v0 + (v1 - v0) * f;
93 }
94 
95 static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
96 {
97  struct rgbvec v = {
98  lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
99  };
100  return v;
101 }
102 
103 #define NEAR(x) ((int)((x) + .5))
104 #define PREV(x) ((int)(x))
105 #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
106 
107 /**
108  * Get the nearest defined point
109  */
110 static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
111  const struct rgbvec *s)
112 {
113  return lut3d->lut[NEAR(s->r)][NEAR(s->g)][NEAR(s->b)];
114 }
115 
116 /**
117  * Interpolate using the 8 vertices of a cube
118  * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
119  */
120 static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
121  const struct rgbvec *s)
122 {
123  const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
124  const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
125  const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
126  const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
127  const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
128  const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
129  const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
130  const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
131  const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
132  const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
133  const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
134  const struct rgbvec c00 = lerp(&c000, &c100, d.r);
135  const struct rgbvec c10 = lerp(&c010, &c110, d.r);
136  const struct rgbvec c01 = lerp(&c001, &c101, d.r);
137  const struct rgbvec c11 = lerp(&c011, &c111, d.r);
138  const struct rgbvec c0 = lerp(&c00, &c10, d.g);
139  const struct rgbvec c1 = lerp(&c01, &c11, d.g);
140  const struct rgbvec c = lerp(&c0, &c1, d.b);
141  return c;
142 }
143 
144 /**
145  * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
146  * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
147  */
148 static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
149  const struct rgbvec *s)
150 {
151  const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
152  const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
153  const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
154  const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
155  const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
156  struct rgbvec c;
157  if (d.r > d.g) {
158  if (d.g > d.b) {
159  const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
160  const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
161  c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
162  c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
163  c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
164  } else if (d.r > d.b) {
165  const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
166  const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
167  c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
168  c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
169  c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
170  } else {
171  const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
172  const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
173  c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
174  c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
175  c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
176  }
177  } else {
178  if (d.b > d.g) {
179  const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
180  const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
181  c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
182  c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
183  c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
184  } else if (d.b > d.r) {
185  const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
186  const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
187  c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
188  c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
189  c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
190  } else {
191  const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
192  const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
193  c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
194  c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
195  c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
196  }
197  }
198  return c;
199 }
200 
201 #define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \
202 static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
203 { \
204  int x, y; \
205  const LUT3DContext *lut3d = ctx->priv; \
206  const ThreadData *td = arg; \
207  const AVFrame *in = td->in; \
208  const AVFrame *out = td->out; \
209  const int direct = out == in; \
210  const int slice_start = (in->height * jobnr ) / nb_jobs; \
211  const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
212  uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
213  uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
214  uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
215  uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
216  const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
217  const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
218  const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
219  const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
220  const float scale = (1. / ((1<<depth) - 1)) * (lut3d->lutsize - 1); \
221  \
222  for (y = slice_start; y < slice_end; y++) { \
223  uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
224  uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
225  uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
226  uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
227  const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
228  const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
229  const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
230  const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
231  for (x = 0; x < in->width; x++) { \
232  const struct rgbvec scaled_rgb = {srcr[x] * scale, \
233  srcg[x] * scale, \
234  srcb[x] * scale}; \
235  struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
236  dstr[x] = av_clip_uintp2(vec.r * (float)((1<<depth) - 1), depth); \
237  dstg[x] = av_clip_uintp2(vec.g * (float)((1<<depth) - 1), depth); \
238  dstb[x] = av_clip_uintp2(vec.b * (float)((1<<depth) - 1), depth); \
239  if (!direct && in->linesize[3]) \
240  dsta[x] = srca[x]; \
241  } \
242  grow += out->linesize[0]; \
243  brow += out->linesize[1]; \
244  rrow += out->linesize[2]; \
245  arow += out->linesize[3]; \
246  srcgrow += in->linesize[0]; \
247  srcbrow += in->linesize[1]; \
248  srcrrow += in->linesize[2]; \
249  srcarow += in->linesize[3]; \
250  } \
251  return 0; \
252 }
253 
254 DEFINE_INTERP_FUNC_PLANAR(nearest, 8, 8)
255 DEFINE_INTERP_FUNC_PLANAR(trilinear, 8, 8)
256 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 8, 8)
257 
258 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 9)
259 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 9)
260 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 9)
261 
262 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 10)
263 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 10)
264 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 10)
265 
266 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 12)
267 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 12)
268 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 12)
269 
270 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 14)
271 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 14)
272 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 14)
273 
274 DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16)
275 DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16)
276 DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16)
277 
278 #define DEFINE_INTERP_FUNC(name, nbits) \
279 static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
280 { \
281  int x, y; \
282  const LUT3DContext *lut3d = ctx->priv; \
283  const ThreadData *td = arg; \
284  const AVFrame *in = td->in; \
285  const AVFrame *out = td->out; \
286  const int direct = out == in; \
287  const int step = lut3d->step; \
288  const uint8_t r = lut3d->rgba_map[R]; \
289  const uint8_t g = lut3d->rgba_map[G]; \
290  const uint8_t b = lut3d->rgba_map[B]; \
291  const uint8_t a = lut3d->rgba_map[A]; \
292  const int slice_start = (in->height * jobnr ) / nb_jobs; \
293  const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
294  uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
295  const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
296  const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
297  \
298  for (y = slice_start; y < slice_end; y++) { \
299  uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
300  const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
301  for (x = 0; x < in->width * step; x += step) { \
302  const struct rgbvec scaled_rgb = {src[x + r] * scale, \
303  src[x + g] * scale, \
304  src[x + b] * scale}; \
305  struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
306  dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
307  dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
308  dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
309  if (!direct && step == 4) \
310  dst[x + a] = src[x + a]; \
311  } \
312  dstrow += out->linesize[0]; \
313  srcrow += in ->linesize[0]; \
314  } \
315  return 0; \
316 }
317 
318 DEFINE_INTERP_FUNC(nearest, 8)
319 DEFINE_INTERP_FUNC(trilinear, 8)
320 DEFINE_INTERP_FUNC(tetrahedral, 8)
321 
322 DEFINE_INTERP_FUNC(nearest, 16)
323 DEFINE_INTERP_FUNC(trilinear, 16)
324 DEFINE_INTERP_FUNC(tetrahedral, 16)
325 
326 #define MAX_LINE_SIZE 512
327 
328 static int skip_line(const char *p)
329 {
330  while (*p && av_isspace(*p))
331  p++;
332  return !*p || *p == '#';
333 }
334 
335 #define NEXT_LINE(loop_cond) do { \
336  if (!fgets(line, sizeof(line), f)) { \
337  av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
338  return AVERROR_INVALIDDATA; \
339  } \
340 } while (loop_cond)
341 
342 /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
343  * directive; seems to be generated by Davinci */
344 static int parse_dat(AVFilterContext *ctx, FILE *f)
345 {
346  LUT3DContext *lut3d = ctx->priv;
347  char line[MAX_LINE_SIZE];
348  int i, j, k, size;
349 
350  lut3d->lutsize = size = 33;
351 
352  NEXT_LINE(skip_line(line));
353  if (!strncmp(line, "3DLUTSIZE ", 10)) {
354  size = strtol(line + 10, NULL, 0);
356  av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
357  return AVERROR(EINVAL);
358  }
359  lut3d->lutsize = size;
360  NEXT_LINE(skip_line(line));
361  }
362  for (k = 0; k < size; k++) {
363  for (j = 0; j < size; j++) {
364  for (i = 0; i < size; i++) {
365  struct rgbvec *vec = &lut3d->lut[k][j][i];
366  if (k != 0 || j != 0 || i != 0)
367  NEXT_LINE(skip_line(line));
368  if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
369  return AVERROR_INVALIDDATA;
370  }
371  }
372  }
373  return 0;
374 }
375 
376 /* Iridas format */
377 static int parse_cube(AVFilterContext *ctx, FILE *f)
378 {
379  LUT3DContext *lut3d = ctx->priv;
380  char line[MAX_LINE_SIZE];
381  float min[3] = {0.0, 0.0, 0.0};
382  float max[3] = {1.0, 1.0, 1.0};
383 
384  while (fgets(line, sizeof(line), f)) {
385  if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
386  int i, j, k;
387  const int size = strtol(line + 12, NULL, 0);
388 
390  av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
391  return AVERROR(EINVAL);
392  }
393  lut3d->lutsize = size;
394  for (k = 0; k < size; k++) {
395  for (j = 0; j < size; j++) {
396  for (i = 0; i < size; i++) {
397  struct rgbvec *vec = &lut3d->lut[i][j][k];
398 
399  do {
400 try_again:
401  NEXT_LINE(0);
402  if (!strncmp(line, "DOMAIN_", 7)) {
403  float *vals = NULL;
404  if (!strncmp(line + 7, "MIN ", 4)) vals = min;
405  else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
406  if (!vals)
407  return AVERROR_INVALIDDATA;
408  sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
409  av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
410  min[0], min[1], min[2], max[0], max[1], max[2]);
411  goto try_again;
412  }
413  } while (skip_line(line));
414  if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
415  return AVERROR_INVALIDDATA;
416  vec->r *= max[0] - min[0];
417  vec->g *= max[1] - min[1];
418  vec->b *= max[2] - min[2];
419  }
420  }
421  }
422  break;
423  }
424  }
425  return 0;
426 }
427 
428 /* Assume 17x17x17 LUT with a 16-bit depth
429  * FIXME: it seems there are various 3dl formats */
430 static int parse_3dl(AVFilterContext *ctx, FILE *f)
431 {
432  char line[MAX_LINE_SIZE];
433  LUT3DContext *lut3d = ctx->priv;
434  int i, j, k;
435  const int size = 17;
436  const float scale = 16*16*16;
437 
438  lut3d->lutsize = size;
439  NEXT_LINE(skip_line(line));
440  for (k = 0; k < size; k++) {
441  for (j = 0; j < size; j++) {
442  for (i = 0; i < size; i++) {
443  int r, g, b;
444  struct rgbvec *vec = &lut3d->lut[k][j][i];
445 
446  NEXT_LINE(skip_line(line));
447  if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
448  return AVERROR_INVALIDDATA;
449  vec->r = r / scale;
450  vec->g = g / scale;
451  vec->b = b / scale;
452  }
453  }
454  }
455  return 0;
456 }
457 
458 /* Pandora format */
459 static int parse_m3d(AVFilterContext *ctx, FILE *f)
460 {
461  LUT3DContext *lut3d = ctx->priv;
462  float scale;
463  int i, j, k, size, in = -1, out = -1;
464  char line[MAX_LINE_SIZE];
465  uint8_t rgb_map[3] = {0, 1, 2};
466 
467  while (fgets(line, sizeof(line), f)) {
468  if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
469  else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
470  else if (!strncmp(line, "values", 6)) {
471  const char *p = line + 6;
472 #define SET_COLOR(id) do { \
473  while (av_isspace(*p)) \
474  p++; \
475  switch (*p) { \
476  case 'r': rgb_map[id] = 0; break; \
477  case 'g': rgb_map[id] = 1; break; \
478  case 'b': rgb_map[id] = 2; break; \
479  } \
480  while (*p && !av_isspace(*p)) \
481  p++; \
482 } while (0)
483  SET_COLOR(0);
484  SET_COLOR(1);
485  SET_COLOR(2);
486  break;
487  }
488  }
489 
490  if (in == -1 || out == -1) {
491  av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
492  return AVERROR_INVALIDDATA;
493  }
494  if (in < 2 || out < 2 ||
497  av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
498  return AVERROR_INVALIDDATA;
499  }
500  for (size = 1; size*size*size < in; size++);
501  lut3d->lutsize = size;
502  scale = 1. / (out - 1);
503 
504  for (k = 0; k < size; k++) {
505  for (j = 0; j < size; j++) {
506  for (i = 0; i < size; i++) {
507  struct rgbvec *vec = &lut3d->lut[k][j][i];
508  float val[3];
509 
510  NEXT_LINE(0);
511  if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
512  return AVERROR_INVALIDDATA;
513  vec->r = val[rgb_map[0]] * scale;
514  vec->g = val[rgb_map[1]] * scale;
515  vec->b = val[rgb_map[2]] * scale;
516  }
517  }
518  }
519  return 0;
520 }
521 
522 static void set_identity_matrix(LUT3DContext *lut3d, int size)
523 {
524  int i, j, k;
525  const float c = 1. / (size - 1);
526 
527  lut3d->lutsize = size;
528  for (k = 0; k < size; k++) {
529  for (j = 0; j < size; j++) {
530  for (i = 0; i < size; i++) {
531  struct rgbvec *vec = &lut3d->lut[k][j][i];
532  vec->r = k * c;
533  vec->g = j * c;
534  vec->b = i * c;
535  }
536  }
537  }
538 }
539 
541 {
542  static const enum AVPixelFormat pix_fmts[] = {
557  };
558  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
559  if (!fmts_list)
560  return AVERROR(ENOMEM);
561  return ff_set_common_formats(ctx, fmts_list);
562 }
563 
564 static int config_input(AVFilterLink *inlink)
565 {
566  int depth, is16bit = 0, planar = 0;
567  LUT3DContext *lut3d = inlink->dst->priv;
569 
570  depth = desc->comp[0].depth;
571 
572  switch (inlink->format) {
573  case AV_PIX_FMT_RGB48:
574  case AV_PIX_FMT_BGR48:
575  case AV_PIX_FMT_RGBA64:
576  case AV_PIX_FMT_BGRA64:
577  is16bit = 1;
578  break;
579  case AV_PIX_FMT_GBRP9:
580  case AV_PIX_FMT_GBRP10:
581  case AV_PIX_FMT_GBRP12:
582  case AV_PIX_FMT_GBRP14:
583  case AV_PIX_FMT_GBRP16:
584  case AV_PIX_FMT_GBRAP10:
585  case AV_PIX_FMT_GBRAP12:
586  case AV_PIX_FMT_GBRAP16:
587  is16bit = 1;
588  case AV_PIX_FMT_GBRP:
589  case AV_PIX_FMT_GBRAP:
590  planar = 1;
591  break;
592  }
593 
594  ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
595  lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
596 
597 #define SET_FUNC(name) do { \
598  if (planar) { \
599  switch (depth) { \
600  case 8: lut3d->interp = interp_8_##name##_p8; break; \
601  case 9: lut3d->interp = interp_16_##name##_p9; break; \
602  case 10: lut3d->interp = interp_16_##name##_p10; break; \
603  case 12: lut3d->interp = interp_16_##name##_p12; break; \
604  case 14: lut3d->interp = interp_16_##name##_p14; break; \
605  case 16: lut3d->interp = interp_16_##name##_p16; break; \
606  } \
607  } else if (is16bit) { lut3d->interp = interp_16_##name; \
608  } else { lut3d->interp = interp_8_##name; } \
609 } while (0)
610 
611  switch (lut3d->interpolation) {
612  case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
613  case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
614  case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
615  default:
616  av_assert0(0);
617  }
618 
619  return 0;
620 }
621 
623 {
624  AVFilterContext *ctx = inlink->dst;
625  LUT3DContext *lut3d = ctx->priv;
626  AVFilterLink *outlink = inlink->dst->outputs[0];
627  AVFrame *out;
628  ThreadData td;
629 
630  if (av_frame_is_writable(in)) {
631  out = in;
632  } else {
633  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
634  if (!out) {
635  av_frame_free(&in);
636  return NULL;
637  }
638  av_frame_copy_props(out, in);
639  }
640 
641  td.in = in;
642  td.out = out;
643  ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
644 
645  if (out != in)
646  av_frame_free(&in);
647 
648  return out;
649 }
650 
651 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
652 {
653  AVFilterLink *outlink = inlink->dst->outputs[0];
654  AVFrame *out = apply_lut(inlink, in);
655  if (!out)
656  return AVERROR(ENOMEM);
657  return ff_filter_frame(outlink, out);
658 }
659 
660 #if CONFIG_LUT3D_FILTER
661 static const AVOption lut3d_options[] = {
662  { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
664 };
665 
666 AVFILTER_DEFINE_CLASS(lut3d);
667 
668 static av_cold int lut3d_init(AVFilterContext *ctx)
669 {
670  int ret;
671  FILE *f;
672  const char *ext;
673  LUT3DContext *lut3d = ctx->priv;
674 
675  if (!lut3d->file) {
676  set_identity_matrix(lut3d, 32);
677  return 0;
678  }
679 
680  f = fopen(lut3d->file, "r");
681  if (!f) {
682  ret = AVERROR(errno);
683  av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
684  return ret;
685  }
686 
687  ext = strrchr(lut3d->file, '.');
688  if (!ext) {
689  av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
690  ret = AVERROR_INVALIDDATA;
691  goto end;
692  }
693  ext++;
694 
695  if (!av_strcasecmp(ext, "dat")) {
696  ret = parse_dat(ctx, f);
697  } else if (!av_strcasecmp(ext, "3dl")) {
698  ret = parse_3dl(ctx, f);
699  } else if (!av_strcasecmp(ext, "cube")) {
700  ret = parse_cube(ctx, f);
701  } else if (!av_strcasecmp(ext, "m3d")) {
702  ret = parse_m3d(ctx, f);
703  } else {
704  av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
705  ret = AVERROR(EINVAL);
706  }
707 
708  if (!ret && !lut3d->lutsize) {
709  av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
710  ret = AVERROR_INVALIDDATA;
711  }
712 
713 end:
714  fclose(f);
715  return ret;
716 }
717 
718 static const AVFilterPad lut3d_inputs[] = {
719  {
720  .name = "default",
721  .type = AVMEDIA_TYPE_VIDEO,
722  .filter_frame = filter_frame,
723  .config_props = config_input,
724  },
725  { NULL }
726 };
727 
728 static const AVFilterPad lut3d_outputs[] = {
729  {
730  .name = "default",
731  .type = AVMEDIA_TYPE_VIDEO,
732  },
733  { NULL }
734 };
735 
737  .name = "lut3d",
738  .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
739  .priv_size = sizeof(LUT3DContext),
740  .init = lut3d_init,
742  .inputs = lut3d_inputs,
743  .outputs = lut3d_outputs,
744  .priv_class = &lut3d_class,
746 };
747 #endif
748 
749 #if CONFIG_HALDCLUT_FILTER
750 
751 static void update_clut(LUT3DContext *lut3d, const AVFrame *frame)
752 {
753  const uint8_t *data = frame->data[0];
754  const int linesize = frame->linesize[0];
755  const int w = lut3d->clut_width;
756  const int step = lut3d->clut_step;
757  const uint8_t *rgba_map = lut3d->clut_rgba_map;
758  const int level = lut3d->lutsize;
759 
760 #define LOAD_CLUT(nbits) do { \
761  int i, j, k, x = 0, y = 0; \
762  \
763  for (k = 0; k < level; k++) { \
764  for (j = 0; j < level; j++) { \
765  for (i = 0; i < level; i++) { \
766  const uint##nbits##_t *src = (const uint##nbits##_t *) \
767  (data + y*linesize + x*step); \
768  struct rgbvec *vec = &lut3d->lut[i][j][k]; \
769  vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
770  vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
771  vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
772  if (++x == w) { \
773  x = 0; \
774  y++; \
775  } \
776  } \
777  } \
778  } \
779 } while (0)
780 
781  if (!lut3d->clut_is16bit) LOAD_CLUT(8);
782  else LOAD_CLUT(16);
783 }
784 
785 
786 static int config_output(AVFilterLink *outlink)
787 {
788  AVFilterContext *ctx = outlink->src;
789  LUT3DContext *lut3d = ctx->priv;
790  int ret;
791 
792  ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
793  if (ret < 0)
794  return ret;
795  outlink->w = ctx->inputs[0]->w;
796  outlink->h = ctx->inputs[0]->h;
797  outlink->time_base = ctx->inputs[0]->time_base;
798  if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
799  return ret;
800  return 0;
801 }
802 
803 static int activate(AVFilterContext *ctx)
804 {
805  LUT3DContext *s = ctx->priv;
806  return ff_framesync_activate(&s->fs);
807 }
808 
809 static int config_clut(AVFilterLink *inlink)
810 {
811  int size, level, w, h;
812  AVFilterContext *ctx = inlink->dst;
813  LUT3DContext *lut3d = ctx->priv;
815 
816  av_assert0(desc);
817 
818  lut3d->clut_is16bit = 0;
819  switch (inlink->format) {
820  case AV_PIX_FMT_RGB48:
821  case AV_PIX_FMT_BGR48:
822  case AV_PIX_FMT_RGBA64:
823  case AV_PIX_FMT_BGRA64:
824  lut3d->clut_is16bit = 1;
825  }
826 
827  lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
828  ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
829 
830  if (inlink->w > inlink->h)
831  av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
832  "Hald CLUT will be ignored\n", inlink->w - inlink->h);
833  else if (inlink->w < inlink->h)
834  av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
835  "Hald CLUT will be ignored\n", inlink->h - inlink->w);
836  lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
837 
838  for (level = 1; level*level*level < w; level++);
839  size = level*level*level;
840  if (size != w) {
841  av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
842  return AVERROR_INVALIDDATA;
843  }
844  av_assert0(w == h && w == size);
845  level *= level;
846  if (level > MAX_LEVEL) {
847  const int max_clut_level = sqrt(MAX_LEVEL);
848  const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
849  av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
850  "(maximum level is %d, or %dx%d CLUT)\n",
851  max_clut_level, max_clut_size, max_clut_size);
852  return AVERROR(EINVAL);
853  }
854  lut3d->lutsize = level;
855 
856  return 0;
857 }
858 
859 static int update_apply_clut(FFFrameSync *fs)
860 {
861  AVFilterContext *ctx = fs->parent;
862  AVFilterLink *inlink = ctx->inputs[0];
863  AVFrame *master, *second, *out;
864  int ret;
865 
866  ret = ff_framesync_dualinput_get(fs, &master, &second);
867  if (ret < 0)
868  return ret;
869  if (!second)
870  return ff_filter_frame(ctx->outputs[0], master);
871  update_clut(ctx->priv, second);
872  out = apply_lut(inlink, master);
873  return ff_filter_frame(ctx->outputs[0], out);
874 }
875 
876 static av_cold int haldclut_init(AVFilterContext *ctx)
877 {
878  LUT3DContext *lut3d = ctx->priv;
879  lut3d->fs.on_event = update_apply_clut;
880  return 0;
881 }
882 
883 static av_cold void haldclut_uninit(AVFilterContext *ctx)
884 {
885  LUT3DContext *lut3d = ctx->priv;
886  ff_framesync_uninit(&lut3d->fs);
887 }
888 
889 static const AVOption haldclut_options[] = {
891 };
892 
893 FRAMESYNC_DEFINE_CLASS(haldclut, LUT3DContext, fs);
894 
895 static const AVFilterPad haldclut_inputs[] = {
896  {
897  .name = "main",
898  .type = AVMEDIA_TYPE_VIDEO,
899  .config_props = config_input,
900  },{
901  .name = "clut",
902  .type = AVMEDIA_TYPE_VIDEO,
903  .config_props = config_clut,
904  },
905  { NULL }
906 };
907 
908 static const AVFilterPad haldclut_outputs[] = {
909  {
910  .name = "default",
911  .type = AVMEDIA_TYPE_VIDEO,
912  .config_props = config_output,
913  },
914  { NULL }
915 };
916 
918  .name = "haldclut",
919  .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
920  .priv_size = sizeof(LUT3DContext),
921  .preinit = haldclut_framesync_preinit,
922  .init = haldclut_init,
923  .uninit = haldclut_uninit,
925  .activate = activate,
926  .inputs = haldclut_inputs,
927  .outputs = haldclut_outputs,
928  .priv_class = &haldclut_class,
930 };
931 #endif
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
Definition: framesync.h:300
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVFrame * out
Definition: af_aiir.c:31
#define COMMON_OPTIONS
Definition: vf_lut3d.c:83
static int config_input(AVFilterLink *inlink)
Definition: vf_lut3d.c:564
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2384
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
static struct rgbvec interp_trilinear(const LUT3DContext *lut3d, const struct rgbvec *s)
Interpolate using the 8 vertices of a cube.
Definition: vf_lut3d.c:120
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:392
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
const char * g
Definition: vf_curves.c:112
const char * desc
Definition: nvenc.c:65
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:362
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:164
static int skip_line(const char *p)
Definition: vf_lut3d.c:328
const char * b
Definition: vf_curves.c:113
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:388
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:367
static int parse_cube(AVFilterContext *ctx, FILE *f)
Definition: vf_lut3d.c:377
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:117
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:235
const char * master
Definition: vf_curves.c:114
GLfloat v0
Definition: opengl_enc.c:107
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#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:125
const char * name
Pad name.
Definition: internal.h:60
AVFilterContext * parent
Parent filter context.
Definition: framesync.h:152
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
static int activate(AVFilterContext *ctx)
Definition: af_amerge.c:280
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define MAX_LEVEL
Definition: vf_lut3d.c:57
AVFilter ff_vf_haldclut
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVFrame * in
Definition: af_aiir.c:31
static float lerpf(float v0, float v1, float f)
Definition: vf_lut3d.c:90
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:234
AVOptions.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition: framesync.c:361
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Definition: framesync.c:379
static void set_identity_matrix(LUT3DContext *lut3d, int size)
Definition: vf_lut3d.c:522
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:387
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:90
static AVFrame * frame
Misc file utilities.
#define NEAR(x)
Definition: vf_lut3d.c:103
#define OFFSET(x)
Definition: vf_lut3d.c:81
#define AV_PIX_FMT_BGR48
Definition: pixfmt.h:363
ptrdiff_t size
Definition: opengl_enc.c:101
#define flags(name, subs,...)
Definition: cbs_h2645.c:263
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
#define td
Definition: regdef.h:70
uint8_t rgba_map[4]
Definition: vf_lut3d.c:63
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:823
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:293
interp_mode
Definition: vf_lut3d.c:44
#define DEFINE_INTERP_FUNC(name, nbits)
Definition: vf_lut3d.c:278
Frame sync structure.
Definition: framesync.h:146
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NEXT(x)
Definition: vf_lut3d.c:105
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define MAX_LINE_SIZE
Definition: vf_lut3d.c:326
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:91
const char * r
Definition: vf_curves.c:111
void * priv
private data for use by the filter
Definition: avfilter.h:353
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:2349
static struct rgbvec interp_nearest(const LUT3DContext *lut3d, const struct rgbvec *s)
Get the nearest defined point.
Definition: vf_lut3d.c:110
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int config_output(AVFilterLink *outlink)
Definition: af_aecho.c:232
Definition: graph2dot.c:48
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:393
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:358
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:344
#define FLAGS
Definition: vf_lut3d.c:82
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:88
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:394
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:89
#define SET_COLOR(id)
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:391
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define FFMIN(a, b)
Definition: common.h:96
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
uint8_t w
Definition: llviddspenc.c:38
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
#define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth)
Definition: vf_lut3d.c:201
int lutsize
Definition: vf_lut3d.c:67
#define s(width, name)
Definition: cbs_vp9.c:257
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:390
static AVFrame * apply_lut(AVFilterLink *inlink, AVFrame *in)
Definition: vf_lut3d.c:622
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
#define PREV(x)
Definition: vf_lut3d.c:104
char * file
Definition: vf_lut3d.c:62
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
misc drawing utilities
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:592
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
float b
Definition: vf_lut3d.c:52
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_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),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){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) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;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)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) 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);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=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){intplanes=out->planar?out->ch_count:1;unsignedm=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){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch const uint8_t **in ch off *out planar
Definition: audioconvert.c:56
static struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
Definition: vf_lut3d.c:95
int interpolation
interp_mode
Definition: vf_lut3d.c:61
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
AVFilter ff_vf_lut3d
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:236
const char * name
Filter name.
Definition: avfilter.h:148
#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:133
static struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d, const struct rgbvec *s)
Tetrahedral interpolation.
Definition: vf_lut3d.c:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:389
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
uint8_t level
Definition: svq3.c:207
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:211
static double c[64]
#define NEXT_LINE(loop_cond)
Definition: vf_lut3d.c:335
static int parse_m3d(AVFilterContext *ctx, FILE *f)
Definition: vf_lut3d.c:459
static int parse_3dl(AVFilterContext *ctx, FILE *f)
Definition: vf_lut3d.c:430
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_lut3d.c:651
avfilter_execute_func * execute
Definition: internal.h:155
static int parse_dat(AVFilterContext *ctx, FILE *f)
Definition: vf_lut3d.c:344
float r
Definition: vf_lut3d.c:52
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
static int query_formats(AVFilterContext *ctx)
Definition: vf_lut3d.c:540
float g
Definition: vf_lut3d.c:52
internal API functions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
#define SET_FUNC(name)
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:233
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:652
avfilter_action_func * interp
Definition: vf_lut3d.c:65
struct rgbvec lut[MAX_LEVEL][MAX_LEVEL][MAX_LEVEL]
Definition: vf_lut3d.c:66