FFmpeg  4.4.9
vf_lut2.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/attributes.h"
22 #include "libavutil/common.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "drawutils.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32 #include "framesync.h"
33 
34 static const char *const var_names[] = {
35  "w", ///< width of the input video
36  "h", ///< height of the input video
37  "x", ///< input value for the pixel from input #1
38  "y", ///< input value for the pixel from input #2
39  "bdx", ///< input #1 video bitdepth
40  "bdy", ///< input #2 video bitdepth
41  NULL
42 };
43 
44 enum var_name {
52 };
53 
54 typedef struct LUT2Context {
55  const AVClass *class;
57 
58  int odepth;
59  char *comp_expr_str[4];
60 
63  uint16_t *lut[4]; ///< lookup table for each component
64  int width[4], height[4];
65  int widthx[4], heightx[4];
66  int widthy[4], heighty[4];
69  int nb_planes;
71  int tlut2;
72  AVFrame *prev_frame; /* only used with tlut2 */
73 
74  int (*lut2)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
75 } LUT2Context;
76 
77 typedef struct ThreadData {
79 } ThreadData;
80 
81 #define OFFSET(x) offsetof(LUT2Context, x)
82 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
83 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
84 
85 static const AVOption options[] = {
86  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
87  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
88  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
89  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
90  { "d", "set output depth", OFFSET(odepth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 16, .flags = FLAGS },
91  { NULL }
92 };
93 
95 {
96  LUT2Context *s = ctx->priv;
97  int i;
98 
99  ff_framesync_uninit(&s->fs);
100  av_frame_free(&s->prev_frame);
101 
102  for (i = 0; i < 4; i++) {
103  av_expr_free(s->comp_expr[i]);
104  s->comp_expr[i] = NULL;
105  av_freep(&s->comp_expr_str[i]);
106  av_freep(&s->lut[i]);
107  }
108 }
109 
110 #define BIT8_FMTS \
111  AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, \
112  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, \
113  AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, \
114  AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
115  AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, \
116  AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
117 
118 #define BIT9_FMTS \
119  AV_PIX_FMT_GBRP9, AV_PIX_FMT_GRAY9, \
120  AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
121  AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
122 
123 #define BIT10_FMTS \
124  AV_PIX_FMT_GRAY10, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, \
125  AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
126  AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
127 
128 #define BIT12_FMTS \
129  AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, \
130  AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, \
131  AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP12,
132 
133 #define BIT14_FMTS \
134  AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
135  AV_PIX_FMT_GRAY14, AV_PIX_FMT_GBRP14,
136 
137 #define BIT16_FMTS \
138  AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
139  AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, \
140  AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
141 
143 {
144  LUT2Context *s = ctx->priv;
145  static const enum AVPixelFormat all_pix_fmts[] = {
146  BIT8_FMTS
147  BIT9_FMTS
148  BIT10_FMTS
149  BIT12_FMTS
151  };
152  static const enum AVPixelFormat bit8_pix_fmts[] = {
153  BIT8_FMTS
155  };
156  static const enum AVPixelFormat bit9_pix_fmts[] = {
157  BIT9_FMTS
159  };
160  static const enum AVPixelFormat bit10_pix_fmts[] = {
161  BIT10_FMTS
163  };
164  static const enum AVPixelFormat bit12_pix_fmts[] = {
165  BIT12_FMTS
167  };
168  static const enum AVPixelFormat bit14_pix_fmts[] = {
169  BIT14_FMTS
171  };
172  static const enum AVPixelFormat bit16_pix_fmts[] = {
173  BIT16_FMTS
175  };
176  const enum AVPixelFormat *pix_fmts;
177  int ret;
178 
179  if (s->tlut2 || !s->odepth)
181 
182  for (int i = 0; i < 2; i++) {
183  ret = ff_formats_ref(ff_make_format_list(all_pix_fmts), &ctx->inputs[i]->outcfg.formats);
184  if (ret < 0)
185  return ret;
186  }
187 
188  switch (s->odepth) {
189  case 8: pix_fmts = bit8_pix_fmts; break;
190  case 9: pix_fmts = bit9_pix_fmts; break;
191  case 10: pix_fmts = bit10_pix_fmts; break;
192  case 12: pix_fmts = bit12_pix_fmts; break;
193  case 14: pix_fmts = bit14_pix_fmts; break;
194  case 16: pix_fmts = bit16_pix_fmts; break;
195  default: av_log(ctx, AV_LOG_ERROR, "Unsupported output bit depth %d.\n", s->odepth);
196  return AVERROR(EINVAL);
197  }
198 
199  return ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->outputs[0]->incfg.formats);
200 }
201 
202 static int config_inputx(AVFilterLink *inlink)
203 {
204  AVFilterContext *ctx = inlink->dst;
205  LUT2Context *s = ctx->priv;
207  int hsub = desc->log2_chroma_w;
208  int vsub = desc->log2_chroma_h;
209 
210  s->nb_planesx = av_pix_fmt_count_planes(inlink->format);
211  s->heightx[1] = s->heightx[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
212  s->heightx[0] = s->heightx[3] = inlink->h;
213  s->widthx[1] = s->widthx[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
214  s->widthx[0] = s->widthx[3] = inlink->w;
215 
216  s->var_values[VAR_W] = inlink->w;
217  s->var_values[VAR_H] = inlink->h;
218  s->depthx = desc->comp[0].depth;
219  s->var_values[VAR_BITDEPTHX] = s->depthx;
220 
221  if (s->tlut2) {
222  s->depthy = desc->comp[0].depth;
223  s->var_values[VAR_BITDEPTHY] = s->depthy;
224  }
225 
226  return 0;
227 }
228 
229 static int config_inputy(AVFilterLink *inlink)
230 {
231  AVFilterContext *ctx = inlink->dst;
232  LUT2Context *s = ctx->priv;
234  int hsub = desc->log2_chroma_w;
235  int vsub = desc->log2_chroma_h;
236 
237  s->nb_planesy = av_pix_fmt_count_planes(inlink->format);
238  s->depthy = desc->comp[0].depth;
239  s->var_values[VAR_BITDEPTHY] = s->depthy;
240  s->heighty[1] = s->heighty[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
241  s->heighty[0] = s->heighty[3] = inlink->h;
242  s->widthy[1] = s->widthy[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
243  s->widthy[0] = s->widthy[3] = inlink->w;
244 
245  return 0;
246 }
247 
248 #define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv) \
249 static int lut2_##zname##_##xname##_##yname(AVFilterContext *ctx, \
250  void *arg, \
251  int jobnr, int nb_jobs) \
252 { \
253  LUT2Context *s = ctx->priv; \
254  ThreadData *td = arg; \
255  AVFrame *out = td->out; \
256  AVFrame *srcx = td->srcx; \
257  AVFrame *srcy = td->srcy; \
258  const int odepth = s->odepth; \
259  const int lut_mask = sizeof(xtype) + sizeof(ytype) == 2 ? -1 : (1 << s->depth) - 1; \
260  int p, y, x; \
261  \
262  for (p = 0; p < s->nb_planes; p++) { \
263  const int slice_start = ff_slice_pos(s->heightx[p], jobnr, nb_jobs); \
264  const int slice_end = ff_slice_pos(s->heightx[p], jobnr + 1, nb_jobs); \
265  const uint16_t *lut = s->lut[p]; \
266  const xtype *srcxx; \
267  const ytype *srcyy; \
268  ztype *dst; \
269  \
270  dst = (ztype *)(out->data[p] + slice_start * out->linesize[p]); \
271  srcxx = (const xtype *)(srcx->data[p] + slice_start * srcx->linesize[p]);\
272  srcyy = (const ytype *)(srcy->data[p] + slice_start * srcy->linesize[p]);\
273  \
274  for (y = slice_start; y < slice_end; y++) { \
275  for (x = 0; x < s->widthx[p]; x++) { \
276  dst[x] = av_clip_uintp2_c(lut[((srcyy[x] << s->depthx) | srcxx[x]) & lut_mask], odepth); \
277  } \
278  \
279  dst += out->linesize[p] / zdiv; \
280  srcxx += srcx->linesize[p] / xdiv; \
281  srcyy += srcy->linesize[p] / ydiv; \
282  } \
283  } \
284  return 0; \
285 }
286 
287 DEFINE_LUT2(8, 8, 8, uint8_t, uint8_t, uint8_t, 1, 1, 1)
288 DEFINE_LUT2(8, 8, 16, uint8_t, uint8_t, uint16_t, 1, 1, 2)
289 DEFINE_LUT2(8, 16, 8, uint8_t, uint16_t, uint8_t, 1, 2, 1)
290 DEFINE_LUT2(8, 16, 16, uint8_t, uint16_t, uint16_t, 1, 2, 2)
291 DEFINE_LUT2(16, 8, 8, uint16_t, uint8_t, uint8_t, 2, 1, 1)
292 DEFINE_LUT2(16, 8, 16, uint16_t, uint8_t, uint16_t, 2, 1, 2)
293 DEFINE_LUT2(16, 16, 8, uint16_t, uint16_t, uint8_t, 2, 2, 1)
294 DEFINE_LUT2(16, 16, 16, uint16_t, uint16_t, uint16_t, 2, 2, 2)
295 
297 {
298  AVFilterContext *ctx = fs->parent;
299  LUT2Context *s = fs->opaque;
300  AVFilterLink *outlink = ctx->outputs[0];
301  AVFrame *out, *srcx = NULL, *srcy = NULL;
302  int ret;
303 
304  if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
305  (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
306  return ret;
307 
308  if (ctx->is_disabled || !srcy) {
309  out = av_frame_clone(srcx);
310  if (!out)
311  return AVERROR(ENOMEM);
312  } else {
313  ThreadData td;
314 
315  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
316  if (!out)
317  return AVERROR(ENOMEM);
318  av_frame_copy_props(out, srcx);
319 
320  td.out = out;
321  td.srcx = srcx;
322  td.srcy = srcy;
323  ctx->internal->execute(ctx, s->lut2, &td, NULL, FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
324  }
325 
326  out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
327 
328  return ff_filter_frame(outlink, out);
329 }
330 
331 static int config_output(AVFilterLink *outlink)
332 {
333  AVFilterContext *ctx = outlink->src;
334  LUT2Context *s = ctx->priv;
335  int p, ret;
336 
337  s->depth = s->depthx + s->depthy;
338  s->nb_planes = s->nb_planesx;
339 
340  s->lut2 = s->depth > 16 ? lut2_16_16_16 : lut2_8_8_8;
341  if (s->odepth) {
342  if (s->depthx == 8 && s->depthy == 8 && s->odepth > 8)
343  s->lut2 = lut2_16_8_8;
344  if (s->depthx > 8 && s->depthy == 8 && s->odepth > 8)
345  s->lut2 = lut2_16_16_8;
346  if (s->depthx == 8 && s->depthy > 8 && s->odepth > 8)
347  s->lut2 = lut2_16_8_16;
348  if (s->depthx == 8 && s->depthy == 8 && s->odepth == 8)
349  s->lut2 = lut2_8_8_8;
350  if (s->depthx > 8 && s->depthy == 8 && s->odepth == 8)
351  s->lut2 = lut2_8_16_8;
352  if (s->depthx == 8 && s->depthy > 8 && s->odepth == 8)
353  s->lut2 = lut2_8_8_16;
354  if (s->depthx > 8 && s->depthy > 8 && s->odepth == 8)
355  s->lut2 = lut2_8_16_16;
356  } else {
357  s->odepth = s->depthx;
358  }
359 
360  for (p = 0; p < s->nb_planes; p++) {
361  if (!s->lut[p])
362  s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
363  if (!s->lut[p])
364  return AVERROR(ENOMEM);
365  }
366 
367  for (p = 0; p < s->nb_planes; p++) {
368  double res;
369  int x, y;
370 
371  /* create the parsed expression */
372  av_expr_free(s->comp_expr[p]);
373  s->comp_expr[p] = NULL;
374  ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
375  var_names, NULL, NULL, NULL, NULL, 0, ctx);
376  if (ret < 0) {
378  "Error when parsing the expression '%s' for the component %d.\n",
379  s->comp_expr_str[p], p);
380  return AVERROR(EINVAL);
381  }
382 
383  /* compute the lut */
384  for (y = 0; y < (1 << s->depthy); y++) {
385  s->var_values[VAR_Y] = y;
386  for (x = 0; x < (1 << s->depthx); x++) {
387  s->var_values[VAR_X] = x;
388  res = av_expr_eval(s->comp_expr[p], s->var_values, s);
389  if (isnan(res)) {
391  "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
392  s->comp_expr_str[p], x, y, p);
393  return AVERROR(EINVAL);
394  }
395 
396  s->lut[p][(y << s->depthx) + x] = res;
397  }
398  }
399  }
400 
401  return 0;
402 }
403 
404 static int lut2_config_output(AVFilterLink *outlink)
405 {
406  AVFilterContext *ctx = outlink->src;
407  LUT2Context *s = ctx->priv;
408  AVFilterLink *srcx = ctx->inputs[0];
409  AVFilterLink *srcy = ctx->inputs[1];
410  FFFrameSyncIn *in;
412  int hsub = desc->log2_chroma_w;
413  int vsub = desc->log2_chroma_h;
414  int ret;
415 
416  outlink->w = srcx->w;
417  outlink->h = srcx->h;
418  outlink->time_base = srcx->time_base;
419  outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
420  outlink->frame_rate = srcx->frame_rate;
421 
422  s->nb_planes = av_pix_fmt_count_planes(outlink->format);
423  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(outlink->h, vsub);
424  s->height[0] = s->height[3] = outlink->h;
425  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(outlink->w, hsub);
426  s->width[0] = s->width[3] = outlink->w;
427 
428  if (!s->odepth && srcx->format != srcy->format) {
429  av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
430  return AVERROR(EINVAL);
431  }
432 
433  if (srcx->w != srcy->w || srcx->h != srcy->h) {
434  av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
435  "(size %dx%d) do not match the corresponding "
436  "second input link %s parameters (size %dx%d)\n",
437  ctx->input_pads[0].name, srcx->w, srcx->h,
438  ctx->input_pads[1].name,
439  srcy->w, srcy->h);
440  return AVERROR(EINVAL);
441  }
442 
443  if (s->nb_planesx != s->nb_planesy) {
444  av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
445  "(%d) do not match the corresponding "
446  "second input link %s number of planes (%d)\n",
447  ctx->input_pads[0].name, s->nb_planesx,
448  ctx->input_pads[1].name, s->nb_planesy);
449  return AVERROR(EINVAL);
450  }
451 
452  if (s->nb_planesx != s->nb_planes) {
453  av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
454  "(%d) do not match the corresponding "
455  "output link %s number of planes (%d)\n",
456  ctx->input_pads[0].name, s->nb_planesx,
457  ctx->output_pads[0].name, s->nb_planes);
458  return AVERROR(EINVAL);
459  }
460 
461  if (s->widthx[1] != s->widthy[1] || s->heightx[1] != s->heighty[1]) {
462  av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
463  "(size %dx%d) do not match the corresponding "
464  "second input link %s 2nd plane (size %dx%d)\n",
465  ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
466  ctx->input_pads[1].name,
467  s->widthy[1], s->heighty[1]);
468  return AVERROR(EINVAL);
469  }
470 
471  if (s->widthx[2] != s->widthy[2] || s->heightx[2] != s->heighty[2]) {
472  av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
473  "(size %dx%d) do not match the corresponding "
474  "second input link %s 3rd plane (size %dx%d)\n",
475  ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
476  ctx->input_pads[1].name,
477  s->widthy[2], s->heighty[2]);
478  return AVERROR(EINVAL);
479  }
480 
481  if (s->widthx[1] != s->width[1] || s->heightx[1] != s->height[1]) {
482  av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
483  "(size %dx%d) do not match the corresponding "
484  "output link %s 2nd plane (size %dx%d)\n",
485  ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
486  ctx->output_pads[0].name, s->width[1], s->height[1]);
487  return AVERROR(EINVAL);
488  }
489 
490  if (s->widthx[2] != s->width[2] || s->heightx[2] != s->height[2]) {
491  av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
492  "(size %dx%d) do not match the corresponding "
493  "output link %s 3rd plane (size %dx%d)\n",
494  ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
495  ctx->output_pads[0].name, s->width[2], s->height[2]);
496  return AVERROR(EINVAL);
497  }
498 
499  if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
500  return ret;
501 
502  in = s->fs.in;
503  in[0].time_base = srcx->time_base;
504  in[1].time_base = srcy->time_base;
505  in[0].sync = 2;
506  in[0].before = EXT_STOP;
507  in[0].after = EXT_INFINITY;
508  in[1].sync = 1;
509  in[1].before = EXT_STOP;
510  in[1].after = EXT_INFINITY;
511  s->fs.opaque = s;
512  s->fs.on_event = process_frame;
513 
514  if ((ret = config_output(outlink)) < 0)
515  return ret;
516 
517  ret = ff_framesync_configure(&s->fs);
518  outlink->time_base = s->fs.time_base;
519 
520  return ret;
521 }
522 
524 {
525  LUT2Context *s = ctx->priv;
526  return ff_framesync_activate(&s->fs);
527 }
528 
529 static const AVFilterPad inputs[] = {
530  {
531  .name = "srcx",
532  .type = AVMEDIA_TYPE_VIDEO,
533  .config_props = config_inputx,
534  },
535  {
536  .name = "srcy",
537  .type = AVMEDIA_TYPE_VIDEO,
538  .config_props = config_inputy,
539  },
540  { NULL }
541 };
542 
543 static const AVFilterPad outputs[] = {
544  {
545  .name = "default",
546  .type = AVMEDIA_TYPE_VIDEO,
547  .config_props = lut2_config_output,
548  },
549  { NULL }
550 };
551 
552 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
553  char *res, int res_len, int flags)
554 {
555  int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
556 
557  if (ret < 0)
558  return ret;
559 
560  return config_output(ctx->outputs[0]);
561 }
562 
563 #define lut2_options options
564 
566 
568  .name = "lut2",
569  .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
570  .preinit = lut2_framesync_preinit,
571  .priv_size = sizeof(LUT2Context),
572  .priv_class = &lut2_class,
573  .uninit = uninit,
575  .activate = activate,
576  .inputs = inputs,
577  .outputs = outputs,
581 };
582 
583 #if CONFIG_TLUT2_FILTER
584 
585 static av_cold int init(AVFilterContext *ctx)
586 {
587  LUT2Context *s = ctx->priv;
588 
589  s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
590 
591  return 0;
592 }
593 
594 static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
595 {
596  AVFilterContext *ctx = inlink->dst;
597  LUT2Context *s = ctx->priv;
598  AVFilterLink *outlink = ctx->outputs[0];
599 
600  if (s->prev_frame) {
601  AVFrame *out;
602 
603  if (ctx->is_disabled) {
605  } else {
606  ThreadData td;
607 
608  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
609  if (!out) {
610  av_frame_free(&s->prev_frame);
611  s->prev_frame = frame;
612  return AVERROR(ENOMEM);
613  }
614 
616 
617  td.out = out;
618  td.srcx = frame;
619  td.srcy = s->prev_frame;
620  ctx->internal->execute(ctx, s->lut2, &td, NULL, FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
621  }
622  av_frame_free(&s->prev_frame);
623  s->prev_frame = frame;
624  return ff_filter_frame(outlink, out);
625  }
626  s->prev_frame = frame;
627  return 0;
628 }
629 
630 static const AVOption tlut2_options[] = {
631  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
632  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
633  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
634  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
635  { NULL }
636 };
637 
638 AVFILTER_DEFINE_CLASS(tlut2);
639 
640 static const AVFilterPad tlut2_inputs[] = {
641  {
642  .name = "default",
643  .type = AVMEDIA_TYPE_VIDEO,
644  .filter_frame = tlut2_filter_frame,
645  .config_props = config_inputx,
646  },
647  { NULL }
648 };
649 
650 static const AVFilterPad tlut2_outputs[] = {
651  {
652  .name = "default",
653  .type = AVMEDIA_TYPE_VIDEO,
654  .config_props = config_output,
655  },
656  { NULL }
657 };
658 
660  .name = "tlut2",
661  .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
662  .priv_size = sizeof(LUT2Context),
663  .priv_class = &tlut2_class,
665  .init = init,
666  .uninit = uninit,
667  .inputs = tlut2_inputs,
668  .outputs = tlut2_outputs,
672 };
673 
674 #endif
AVFilter ff_vf_tlut2
Macro definitions for various function/variable attributes.
#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
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
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:885
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:805
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 fs(width, name, subs,...)
Definition: cbs_vp9.c:259
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define NULL
Definition: coverity.c:32
static AVFrame * frame
misc drawing utilities
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:339
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:781
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:700
simple arithmetic expression evaluator
int
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition: formats.c:466
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_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:341
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:253
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:290
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:84
@ EXT_STOP
Completely stop all streams with this one.
Definition: framesync.h:65
@ EXT_INFINITY
Extend the frame to infinity.
Definition: framesync.h:75
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#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(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
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_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int i
Definition: input.c:407
static enum AVPixelFormat all_pix_fmts[]
Definition: jpeg2000dec.c:265
const char * arg
Definition: jacosubdec.c:67
#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 isnan(x)
Definition: libm.h:340
const char * desc
Definition: libsvtav1.c:83
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
#define td
Definition: regdef.h:70
var_name
Definition: setts_bsf.c:50
Describe the class of an AVClass context structure.
Definition: log.h:67
Definition: eval.c:159
An instance of a filter.
Definition: avfilter.h:341
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
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
Input stream structure.
Definition: framesync.h:81
Frame sync structure.
Definition: framesync.h:146
double var_values[VAR_VARS_NB]
Definition: vf_lut2.c:62
int widthx[4]
Definition: vf_lut2.c:65
char * comp_expr_str[4]
Definition: vf_lut2.c:59
int tlut2
Definition: vf_lut2.c:71
int widthy[4]
Definition: vf_lut2.c:66
int heighty[4]
Definition: vf_lut2.c:66
uint16_t * lut[4]
lookup table for each component
Definition: vf_lut2.c:63
int height[4]
Definition: vf_lut2.c:64
int(* lut2)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lut2.c:74
int nb_planesy
Definition: vf_lut2.c:68
int width[4]
Definition: vf_lut2.c:64
int depthx
Definition: vf_lut2.c:70
int nb_planes
Definition: vf_lut2.c:69
int nb_planesx
Definition: vf_lut2.c:67
FFFrameSync fs
Definition: vf_lut2.c:56
AVFrame * prev_frame
Definition: vf_lut2.c:72
int depth
Definition: vf_lut2.c:70
int odepth
Definition: vf_lut2.c:58
int heightx[4]
Definition: vf_lut2.c:65
AVExpr * comp_expr[4]
Definition: vf_lut2.c:61
int depthy
Definition: vf_lut2.c:70
Used for passing data between threads.
Definition: dsddec.c:67
AVFrame * srcy
Definition: vf_lut2.c:78
AVFrame * srcx
Definition: vf_lut2.c:78
AVFrame * out
Definition: af_adeclick.c:505
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
@ VAR_H
Definition: vf_lut2.c:46
@ VAR_BITDEPTHY
Definition: vf_lut2.c:50
@ VAR_W
Definition: vf_lut2.c:45
@ VAR_VARS_NB
Definition: vf_lut2.c:51
@ VAR_X
Definition: vf_lut2.c:47
@ VAR_BITDEPTHX
Definition: vf_lut2.c:49
@ VAR_Y
Definition: vf_lut2.c:48
AVFilter ff_vf_lut2
Definition: vf_lut2.c:567
static int lut2_config_output(AVFilterLink *outlink)
Definition: vf_lut2.c:404
#define BIT8_FMTS
Definition: vf_lut2.c:110
static int config_inputx(AVFilterLink *inlink)
Definition: vf_lut2.c:202
static const AVOption options[]
Definition: vf_lut2.c:85
#define TFLAGS
Definition: vf_lut2.c:83
static int query_formats(AVFilterContext *ctx)
Definition: vf_lut2.c:142
#define FLAGS
Definition: vf_lut2.c:82
static const AVFilterPad inputs[]
Definition: vf_lut2.c:529
#define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv)
Definition: vf_lut2.c:248
static int config_inputy(AVFilterLink *inlink)
Definition: vf_lut2.c:229
FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs)
static const AVFilterPad outputs[]
Definition: vf_lut2.c:543
#define BIT14_FMTS
Definition: vf_lut2.c:133
#define BIT16_FMTS
Definition: vf_lut2.c:137
#define BIT10_FMTS
Definition: vf_lut2.c:123
static const char *const var_names[]
Definition: vf_lut2.c:34
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_lut2.c:552
static int activate(AVFilterContext *ctx)
Definition: vf_lut2.c:523
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_lut2.c:94
#define BIT12_FMTS
Definition: vf_lut2.c:128
#define BIT9_FMTS
Definition: vf_lut2.c:118
#define OFFSET(x)
Definition: vf_lut2.c:81
static int config_output(AVFilterLink *outlink)
Definition: vf_lut2.c:331
static int process_frame(FFFrameSync *fs)
Definition: vf_lut2.c:296
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
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