FFmpeg
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 "drawutils.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 #include "framesync.h"
32 
33 static const char *const var_names[] = {
34  "w", ///< width of the input video
35  "h", ///< height of the input video
36  "x", ///< input value for the pixel from input #1
37  "y", ///< input value for the pixel from input #2
38  "bdx", ///< input #1 video bitdepth
39  "bdy", ///< input #2 video bitdepth
40  NULL
41 };
42 
43 enum var_name {
51 };
52 
53 typedef struct LUT2Context {
54  const AVClass *class;
56 
57  int odepth;
58  char *comp_expr_str[4];
59 
62  uint16_t *lut[4]; ///< lookup table for each component
63  int width[4], height[4];
64  int widthx[4], heightx[4];
65  int widthy[4], heighty[4];
68  int nb_planes;
70  int tlut2;
71  AVFrame *prev_frame; /* only used with tlut2 */
72 
73  int (*lut2)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
74 } LUT2Context;
75 
76 typedef struct ThreadData {
78 } ThreadData;
79 
80 #define OFFSET(x) offsetof(LUT2Context, x)
81 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
83 
84 static const AVOption options[] = {
85  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
86  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
87  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
88  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
89  { "d", "set output depth", OFFSET(odepth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 16, .flags = FLAGS },
90  { NULL }
91 };
92 
94 {
95  LUT2Context *s = ctx->priv;
96  int i;
97 
98  ff_framesync_uninit(&s->fs);
99  av_frame_free(&s->prev_frame);
100 
101  for (i = 0; i < 4; i++) {
102  av_expr_free(s->comp_expr[i]);
103  s->comp_expr[i] = NULL;
104  av_freep(&s->comp_expr_str[i]);
105  av_freep(&s->lut[i]);
106  }
107 }
108 
109 #define BIT8_FMTS \
110  AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, \
111  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, \
112  AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, \
113  AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
114  AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, \
115  AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
116 
117 #define BIT9_FMTS \
118  AV_PIX_FMT_GBRP9, AV_PIX_FMT_GRAY9, \
119  AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
120  AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
121 
122 #define BIT10_FMTS \
123  AV_PIX_FMT_GRAY10, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, \
124  AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
125  AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
126 
127 #define BIT12_FMTS \
128  AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, \
129  AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, \
130  AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP12,
131 
132 #define BIT14_FMTS \
133  AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
134  AV_PIX_FMT_GRAY14, AV_PIX_FMT_GBRP14,
135 
136 #define BIT16_FMTS \
137  AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
138  AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, \
139  AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
140 
142 {
143  LUT2Context *s = ctx->priv;
144  static const enum AVPixelFormat all_pix_fmts[] = {
145  BIT8_FMTS
146  BIT9_FMTS
147  BIT10_FMTS
148  BIT12_FMTS
150  };
151  static const enum AVPixelFormat bit8_pix_fmts[] = {
152  BIT8_FMTS
154  };
155  static const enum AVPixelFormat bit9_pix_fmts[] = {
156  BIT9_FMTS
158  };
159  static const enum AVPixelFormat bit10_pix_fmts[] = {
160  BIT10_FMTS
162  };
163  static const enum AVPixelFormat bit12_pix_fmts[] = {
164  BIT12_FMTS
166  };
167  static const enum AVPixelFormat bit14_pix_fmts[] = {
168  BIT14_FMTS
170  };
171  static const enum AVPixelFormat bit16_pix_fmts[] = {
172  BIT16_FMTS
174  };
175  const enum AVPixelFormat *pix_fmts;
176  int ret;
177 
178  if (s->tlut2 || !s->odepth)
180 
181  ret = ff_formats_ref(ff_make_format_list(all_pix_fmts), &ctx->inputs[0]->outcfg.formats);
182  if (ret < 0)
183  return ret;
184 
185  switch (s->odepth) {
186  case 8: pix_fmts = bit8_pix_fmts; break;
187  case 9: pix_fmts = bit9_pix_fmts; break;
188  case 10: pix_fmts = bit10_pix_fmts; break;
189  case 12: pix_fmts = bit12_pix_fmts; break;
190  case 14: pix_fmts = bit14_pix_fmts; break;
191  case 16: pix_fmts = bit16_pix_fmts; break;
192  default: av_log(ctx, AV_LOG_ERROR, "Unsupported output bit depth %d.\n", s->odepth);
193  return AVERROR(EINVAL);
194  }
195 
196  return ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->outputs[0]->incfg.formats);
197 }
198 
200 {
201  AVFilterContext *ctx = inlink->dst;
202  LUT2Context *s = ctx->priv;
204  int hsub = desc->log2_chroma_w;
205  int vsub = desc->log2_chroma_h;
206 
207  s->nb_planesx = av_pix_fmt_count_planes(inlink->format);
208  s->heightx[1] = s->heightx[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
209  s->heightx[0] = s->heightx[3] = inlink->h;
210  s->widthx[1] = s->widthx[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
211  s->widthx[0] = s->widthx[3] = inlink->w;
212 
213  s->var_values[VAR_W] = inlink->w;
214  s->var_values[VAR_H] = inlink->h;
215  s->depthx = desc->comp[0].depth;
216  s->var_values[VAR_BITDEPTHX] = s->depthx;
217 
218  if (s->tlut2) {
219  s->depthy = desc->comp[0].depth;
220  s->var_values[VAR_BITDEPTHY] = s->depthy;
221  }
222 
223  return 0;
224 }
225 
227 {
228  AVFilterContext *ctx = inlink->dst;
229  LUT2Context *s = ctx->priv;
231  int hsub = desc->log2_chroma_w;
232  int vsub = desc->log2_chroma_h;
233 
234  s->nb_planesy = av_pix_fmt_count_planes(inlink->format);
235  s->depthy = desc->comp[0].depth;
236  s->var_values[VAR_BITDEPTHY] = s->depthy;
237  s->heighty[1] = s->heighty[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
238  s->heighty[0] = s->heighty[3] = inlink->h;
239  s->widthy[1] = s->widthy[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
240  s->widthy[0] = s->widthy[3] = inlink->w;
241 
242  return 0;
243 }
244 
245 #define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv) \
246 static int lut2_##zname##_##xname##_##yname(AVFilterContext *ctx, \
247  void *arg, \
248  int jobnr, int nb_jobs) \
249 { \
250  LUT2Context *s = ctx->priv; \
251  ThreadData *td = arg; \
252  AVFrame *out = td->out; \
253  AVFrame *srcx = td->srcx; \
254  AVFrame *srcy = td->srcy; \
255  const int odepth = s->odepth; \
256  int p, y, x; \
257  \
258  for (p = 0; p < s->nb_planes; p++) { \
259  const int slice_start = (s->heightx[p] * jobnr) / nb_jobs; \
260  const int slice_end = (s->heightx[p] * (jobnr+1)) / nb_jobs; \
261  const uint16_t *lut = s->lut[p]; \
262  const xtype *srcxx; \
263  const ytype *srcyy; \
264  ztype *dst; \
265  \
266  dst = (ztype *)(out->data[p] + slice_start * out->linesize[p]); \
267  srcxx = (const xtype *)(srcx->data[p] + slice_start * srcx->linesize[p]);\
268  srcyy = (const ytype *)(srcy->data[p] + slice_start * srcy->linesize[p]);\
269  \
270  for (y = slice_start; y < slice_end; y++) { \
271  for (x = 0; x < s->widthx[p]; x++) { \
272  dst[x] = av_clip_uintp2_c(lut[(srcyy[x] << s->depthx) | srcxx[x]], odepth); \
273  } \
274  \
275  dst += out->linesize[p] / zdiv; \
276  srcxx += srcx->linesize[p] / xdiv; \
277  srcyy += srcy->linesize[p] / ydiv; \
278  } \
279  } \
280  return 0; \
281 }
282 
283 DEFINE_LUT2(8, 8, 8, uint8_t, uint8_t, uint8_t, 1, 1, 1)
284 DEFINE_LUT2(8, 8, 16, uint8_t, uint8_t, uint16_t, 1, 1, 2)
285 DEFINE_LUT2(8, 16, 8, uint8_t, uint16_t, uint8_t, 1, 2, 1)
286 DEFINE_LUT2(8, 16, 16, uint8_t, uint16_t, uint16_t, 1, 2, 2)
287 DEFINE_LUT2(16, 8, 8, uint16_t, uint8_t, uint8_t, 2, 1, 1)
288 DEFINE_LUT2(16, 8, 16, uint16_t, uint8_t, uint16_t, 2, 1, 2)
289 DEFINE_LUT2(16, 16, 8, uint16_t, uint16_t, uint8_t, 2, 2, 1)
290 DEFINE_LUT2(16, 16, 16, uint16_t, uint16_t, uint16_t, 2, 2, 2)
291 
293 {
294  AVFilterContext *ctx = fs->parent;
295  LUT2Context *s = fs->opaque;
296  AVFilterLink *outlink = ctx->outputs[0];
297  AVFrame *out, *srcx = NULL, *srcy = NULL;
298  int ret;
299 
300  if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
301  (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
302  return ret;
303 
304  if (ctx->is_disabled || !srcy) {
305  out = av_frame_clone(srcx);
306  if (!out)
307  return AVERROR(ENOMEM);
308  } else {
309  ThreadData td;
310 
311  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
312  if (!out)
313  return AVERROR(ENOMEM);
314  av_frame_copy_props(out, srcx);
315 
316  td.out = out;
317  td.srcx = srcx;
318  td.srcy = srcy;
319  ff_filter_execute(ctx, s->lut2, &td, NULL,
320  FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
321  }
322 
323  out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
324 
325  return ff_filter_frame(outlink, out);
326 }
327 
328 static int config_output(AVFilterLink *outlink)
329 {
330  AVFilterContext *ctx = outlink->src;
331  LUT2Context *s = ctx->priv;
332  int p, ret;
333 
334  s->depth = s->depthx + s->depthy;
335  s->nb_planes = s->nb_planesx;
336 
337  s->lut2 = s->depth > 16 ? lut2_16_16_16 : lut2_8_8_8;
338  if (s->odepth) {
339  if (s->depthx == 8 && s->depthy == 8 && s->odepth > 8)
340  s->lut2 = lut2_16_8_8;
341  if (s->depthx > 8 && s->depthy == 8 && s->odepth > 8)
342  s->lut2 = lut2_16_16_8;
343  if (s->depthx == 8 && s->depthy > 8 && s->odepth > 8)
344  s->lut2 = lut2_16_8_16;
345  if (s->depthx == 8 && s->depthy == 8 && s->odepth == 8)
346  s->lut2 = lut2_8_8_8;
347  if (s->depthx > 8 && s->depthy == 8 && s->odepth == 8)
348  s->lut2 = lut2_8_16_8;
349  if (s->depthx == 8 && s->depthy > 8 && s->odepth == 8)
350  s->lut2 = lut2_8_8_16;
351  if (s->depthx > 8 && s->depthy > 8 && s->odepth == 8)
352  s->lut2 = lut2_8_16_16;
353  } else {
354  s->odepth = s->depthx;
355  }
356 
357  for (p = 0; p < s->nb_planes; p++) {
358  if (!s->lut[p])
359  s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
360  if (!s->lut[p])
361  return AVERROR(ENOMEM);
362  }
363 
364  for (p = 0; p < s->nb_planes; p++) {
365  double res;
366  int x, y;
367 
368  /* create the parsed expression */
369  av_expr_free(s->comp_expr[p]);
370  s->comp_expr[p] = NULL;
371  ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
372  var_names, NULL, NULL, NULL, NULL, 0, ctx);
373  if (ret < 0) {
375  "Error when parsing the expression '%s' for the component %d.\n",
376  s->comp_expr_str[p], p);
377  return AVERROR(EINVAL);
378  }
379 
380  /* compute the lut */
381  for (y = 0; y < (1 << s->depthy); y++) {
382  s->var_values[VAR_Y] = y;
383  for (x = 0; x < (1 << s->depthx); x++) {
384  s->var_values[VAR_X] = x;
385  res = av_expr_eval(s->comp_expr[p], s->var_values, s);
386  if (isnan(res)) {
388  "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
389  s->comp_expr_str[p], x, y, p);
390  return AVERROR(EINVAL);
391  }
392 
393  s->lut[p][(y << s->depthx) + x] = res;
394  }
395  }
396  }
397 
398  return 0;
399 }
400 
401 static int lut2_config_output(AVFilterLink *outlink)
402 {
403  AVFilterContext *ctx = outlink->src;
404  LUT2Context *s = ctx->priv;
405  AVFilterLink *srcx = ctx->inputs[0];
406  AVFilterLink *srcy = ctx->inputs[1];
407  FFFrameSyncIn *in;
409  int hsub = desc->log2_chroma_w;
410  int vsub = desc->log2_chroma_h;
411  int ret;
412 
413  outlink->w = srcx->w;
414  outlink->h = srcx->h;
415  outlink->time_base = srcx->time_base;
416  outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
417  outlink->frame_rate = srcx->frame_rate;
418 
419  s->nb_planes = av_pix_fmt_count_planes(outlink->format);
420  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(outlink->h, vsub);
421  s->height[0] = s->height[3] = outlink->h;
422  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(outlink->w, hsub);
423  s->width[0] = s->width[3] = outlink->w;
424 
425  if (!s->odepth && srcx->format != srcy->format) {
426  av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
427  return AVERROR(EINVAL);
428  }
429 
430  if (srcx->w != srcy->w || srcx->h != srcy->h) {
431  av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
432  "(size %dx%d) do not match the corresponding "
433  "second input link %s parameters (size %dx%d)\n",
434  ctx->input_pads[0].name, srcx->w, srcx->h,
435  ctx->input_pads[1].name,
436  srcy->w, srcy->h);
437  return AVERROR(EINVAL);
438  }
439 
440  if (s->nb_planesx != s->nb_planesy) {
441  av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
442  "(%d) do not match the corresponding "
443  "second input link %s number of planes (%d)\n",
444  ctx->input_pads[0].name, s->nb_planesx,
445  ctx->input_pads[1].name, s->nb_planesy);
446  return AVERROR(EINVAL);
447  }
448 
449  if (s->nb_planesx != s->nb_planes) {
450  av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
451  "(%d) do not match the corresponding "
452  "output link %s number of planes (%d)\n",
453  ctx->input_pads[0].name, s->nb_planesx,
454  ctx->output_pads[0].name, s->nb_planes);
455  return AVERROR(EINVAL);
456  }
457 
458  if (s->widthx[1] != s->widthy[1] || s->heightx[1] != s->heighty[1]) {
459  av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
460  "(size %dx%d) do not match the corresponding "
461  "second input link %s 2nd plane (size %dx%d)\n",
462  ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
463  ctx->input_pads[1].name,
464  s->widthy[1], s->heighty[1]);
465  return AVERROR(EINVAL);
466  }
467 
468  if (s->widthx[2] != s->widthy[2] || s->heightx[2] != s->heighty[2]) {
469  av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
470  "(size %dx%d) do not match the corresponding "
471  "second input link %s 3rd plane (size %dx%d)\n",
472  ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
473  ctx->input_pads[1].name,
474  s->widthy[2], s->heighty[2]);
475  return AVERROR(EINVAL);
476  }
477 
478  if (s->widthx[1] != s->width[1] || s->heightx[1] != s->height[1]) {
479  av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
480  "(size %dx%d) do not match the corresponding "
481  "output link %s 2nd plane (size %dx%d)\n",
482  ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
483  ctx->output_pads[0].name, s->width[1], s->height[1]);
484  return AVERROR(EINVAL);
485  }
486 
487  if (s->widthx[2] != s->width[2] || s->heightx[2] != s->height[2]) {
488  av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
489  "(size %dx%d) do not match the corresponding "
490  "output link %s 3rd plane (size %dx%d)\n",
491  ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
492  ctx->output_pads[0].name, s->width[2], s->height[2]);
493  return AVERROR(EINVAL);
494  }
495 
496  if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
497  return ret;
498 
499  in = s->fs.in;
500  in[0].time_base = srcx->time_base;
501  in[1].time_base = srcy->time_base;
502  in[0].sync = 2;
503  in[0].before = EXT_STOP;
504  in[0].after = EXT_INFINITY;
505  in[1].sync = 1;
506  in[1].before = EXT_STOP;
507  in[1].after = EXT_INFINITY;
508  s->fs.opaque = s;
509  s->fs.on_event = process_frame;
510 
511  if ((ret = config_output(outlink)) < 0)
512  return ret;
513 
514  ret = ff_framesync_configure(&s->fs);
515  outlink->time_base = s->fs.time_base;
516 
517  return ret;
518 }
519 
521 {
522  LUT2Context *s = ctx->priv;
523  return ff_framesync_activate(&s->fs);
524 }
525 
526 static const AVFilterPad inputs[] = {
527  {
528  .name = "srcx",
529  .type = AVMEDIA_TYPE_VIDEO,
530  .config_props = config_inputx,
531  },
532  {
533  .name = "srcy",
534  .type = AVMEDIA_TYPE_VIDEO,
535  .config_props = config_inputy,
536  },
537 };
538 
539 static const AVFilterPad outputs[] = {
540  {
541  .name = "default",
542  .type = AVMEDIA_TYPE_VIDEO,
543  .config_props = lut2_config_output,
544  },
545 };
546 
547 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
548  char *res, int res_len, int flags)
549 {
550  int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
551 
552  if (ret < 0)
553  return ret;
554 
555  return config_output(ctx->outputs[0]);
556 }
557 
558 #define lut2_options options
559 
561 
563  .name = "lut2",
564  .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
565  .preinit = lut2_framesync_preinit,
566  .priv_size = sizeof(LUT2Context),
567  .priv_class = &lut2_class,
568  .uninit = uninit,
569  .activate = activate,
575  .process_command = process_command,
576 };
577 
578 #if CONFIG_TLUT2_FILTER
579 
580 static av_cold int init(AVFilterContext *ctx)
581 {
582  LUT2Context *s = ctx->priv;
583 
584  s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
585 
586  return 0;
587 }
588 
589 static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
590 {
591  AVFilterContext *ctx = inlink->dst;
592  LUT2Context *s = ctx->priv;
593  AVFilterLink *outlink = ctx->outputs[0];
594 
595  if (s->prev_frame) {
596  AVFrame *out;
597 
598  if (ctx->is_disabled) {
600  } else {
601  ThreadData td;
602 
603  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
604  if (!out) {
605  av_frame_free(&s->prev_frame);
606  s->prev_frame = frame;
607  return AVERROR(ENOMEM);
608  }
609 
611 
612  td.out = out;
613  td.srcx = frame;
614  td.srcy = s->prev_frame;
615  ff_filter_execute(ctx, s->lut2, &td, NULL,
616  FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
617  }
618  av_frame_free(&s->prev_frame);
619  s->prev_frame = frame;
620  return ff_filter_frame(outlink, out);
621  }
622  s->prev_frame = frame;
623  return 0;
624 }
625 
626 static const AVOption tlut2_options[] = {
627  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
628  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
629  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
630  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = TFLAGS },
631  { NULL }
632 };
633 
634 AVFILTER_DEFINE_CLASS(tlut2);
635 
636 static const AVFilterPad tlut2_inputs[] = {
637  {
638  .name = "default",
639  .type = AVMEDIA_TYPE_VIDEO,
640  .filter_frame = tlut2_filter_frame,
641  .config_props = config_inputx,
642  },
643 };
644 
645 static const AVFilterPad tlut2_outputs[] = {
646  {
647  .name = "default",
648  .type = AVMEDIA_TYPE_VIDEO,
649  .config_props = config_output,
650  },
651 };
652 
653 const AVFilter ff_vf_tlut2 = {
654  .name = "tlut2",
655  .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
656  .priv_size = sizeof(LUT2Context),
657  .priv_class = &tlut2_class,
658  .init = init,
659  .uninit = uninit,
660  FILTER_INPUTS(tlut2_inputs),
661  FILTER_OUTPUTS(tlut2_outputs),
665  .process_command = process_command,
666 };
667 
668 #endif
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
FFFrameSyncIn::time_base
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:96
ff_framesync_configure
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:119
td
#define td
Definition: regdef.h:70
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_lut2.c:328
BIT8_FMTS
#define BIT8_FMTS
Definition: vf_lut2.c:109
VAR_VARS_NB
@ VAR_VARS_NB
Definition: vf_lut2.c:50
LUT2Context::lut2
int(* lut2)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lut2.c:73
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:381
BIT14_FMTS
#define BIT14_FMTS
Definition: vf_lut2.c:132
ff_framesync_uninit
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:285
out
FILE * out
Definition: movenc.c:54
VAR_BITDEPTHY
@ VAR_BITDEPTHY
Definition: vf_lut2.c:49
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
ff_framesync_get_frame
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:248
LUT2Context::fs
FFFrameSync fs
Definition: vf_lut2.c:55
LUT2Context::depthx
int depthx
Definition: vf_lut2.c:69
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
LUT2Context
Definition: vf_lut2.c:53
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
AVOption
AVOption.
Definition: opt.h:247
LUT2Context::tlut2
int tlut2
Definition: vf_lut2.c:70
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
LUT2Context::widthx
int widthx[4]
Definition: vf_lut2.c:64
ff_vf_tlut2
const AVFilter ff_vf_tlut2
DEFINE_LUT2
#define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv)
Definition: vf_lut2.c:245
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
FFFrameSync
Frame sync structure.
Definition: framesync.h:146
EXT_INFINITY
@ EXT_INFINITY
Extend the frame to infinity.
Definition: framesync.h:75
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
config_inputx
static int config_inputx(AVFilterLink *inlink)
Definition: vf_lut2.c:199
video.h
VAR_H
@ VAR_H
Definition: vf_lut2.c:45
ThreadData::srcx
AVFrame * srcx
Definition: vf_lut2.c:77
LUT2Context::heightx
int heightx[4]
Definition: vf_lut2.c:64
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:74
formats.h
av_expr_parse
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:685
init
static int init
Definition: av_tx.c:47
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2700
EXT_STOP
@ EXT_STOP
Completely stop all streams with this one.
Definition: framesync.h:65
LUT2Context::depthy
int depthy
Definition: vf_lut2.c:69
FFFrameSyncIn
Input stream structure.
Definition: framesync.h:81
LUT2Context::depth
int depth
Definition: vf_lut2.c:69
FFFrameSyncIn::sync
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition: framesync.h:139
process_frame
static int process_frame(FFFrameSync *fs)
Definition: vf_lut2.c:292
LUT2Context::height
int height[4]
Definition: vf_lut2.c:63
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:336
LUT2Context::prev_frame
AVFrame * prev_frame
Definition: vf_lut2.c:71
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
LUT2Context::odepth
int odepth
Definition: vf_lut2.c:57
LUT2Context::var_values
double var_values[VAR_VARS_NB]
Definition: vf_lut2.c:61
var_names
static const char *const var_names[]
Definition: vf_lut2.c:33
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FLAGS
#define FLAGS
Definition: vf_lut2.c:81
av_cold
#define av_cold
Definition: attributes.h:90
activate
static int activate(AVFilterContext *ctx)
Definition: vf_lut2.c:520
s
#define s(width, name)
Definition: cbs_vp9.c:257
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_lut2.c:547
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
TFLAGS
#define TFLAGS
Definition: vf_lut2.c:82
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:555
LUT2Context::nb_planesx
int nb_planesx
Definition: vf_lut2.c:66
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:705
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_lut2.c:93
OFFSET
#define OFFSET(x)
Definition: vf_lut2.c:80
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
var_name
var_name
Definition: noise_bsf.c:47
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:766
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:422
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
AVExpr
Definition: eval.c:157
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
LUT2Context::width
int width[4]
Definition: vf_lut2.c:63
lut2_config_output
static int lut2_config_output(AVFilterLink *outlink)
Definition: vf_lut2.c:401
BIT9_FMTS
#define BIT9_FMTS
Definition: vf_lut2.c:117
isnan
#define isnan(x)
Definition: libm.h:340
VAR_X
@ VAR_X
Definition: vf_lut2.c:46
LUT2Context::widthy
int widthy[4]
Definition: vf_lut2.c:65
LUT2Context::comp_expr
AVExpr * comp_expr[4]
Definition: vf_lut2.c:60
LUT2Context::lut
uint16_t * lut[4]
lookup table for each component
Definition: vf_lut2.c:62
inputs
static const AVFilterPad inputs[]
Definition: vf_lut2.c:526
VAR_W
@ VAR_W
Definition: vf_lut2.c:44
eval.h
BIT10_FMTS
#define BIT10_FMTS
Definition: vf_lut2.c:122
BIT16_FMTS
#define BIT16_FMTS
Definition: vf_lut2.c:136
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
ff_filter_process_command
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
VAR_BITDEPTHX
@ VAR_BITDEPTHX
Definition: vf_lut2.c:48
attributes.h
all_pix_fmts
static enum AVPixelFormat all_pix_fmts[]
Definition: jpeg2000dec.c:264
options
static const AVOption options[]
Definition: vf_lut2.c:84
internal.h
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:326
LUT2Context::heighty
int heighty[4]
Definition: vf_lut2.c:65
LUT2Context::comp_expr_str
char * comp_expr_str[4]
Definition: vf_lut2.c:58
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
LUT2Context::nb_planesy
int nb_planesy
Definition: vf_lut2.c:67
common.h
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:803
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
config_inputy
static int config_inputy(AVFilterLink *inlink)
Definition: vf_lut2.c:226
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
outputs
static const AVFilterPad outputs[]
Definition: vf_lut2.c:539
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_framesync_init
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:79
FFFrameSyncIn::before
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:86
ff_vf_lut2
const AVFilter ff_vf_lut2
Definition: vf_lut2.c:562
framesync.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
ThreadData::srcy
AVFrame * srcy
Definition: vf_lut2.c:77
BIT12_FMTS
#define BIT12_FMTS
Definition: vf_lut2.c:127
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:121
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
LUT2Context::nb_planes
int nb_planes
Definition: vf_lut2.c:68
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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:154
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
FFFrameSyncIn::after
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
ff_framesync_activate
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:336
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
drawutils.h
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:143
int
int
Definition: ffmpeg_filter.c:153
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_lut2.c:141
VAR_Y
@ VAR_Y
Definition: vf_lut2.c:47
FRAMESYNC_DEFINE_CLASS
FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs)