FFmpeg  4.2.3
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 {
77  AVFrame *out, *srcx, *srcy;
78 } ThreadData;
79 
80 #define OFFSET(x) offsetof(LUT2Context, x)
81 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82 
83 static const AVOption options[] = {
84  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
85  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
86  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
87  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
88  { "d", "set output depth", OFFSET(odepth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 16, .flags = FLAGS },
89  { NULL }
90 };
91 
93 {
94  LUT2Context *s = ctx->priv;
95  int i;
96 
99 
100  for (i = 0; i < 4; i++) {
101  av_expr_free(s->comp_expr[i]);
102  s->comp_expr[i] = NULL;
103  av_freep(&s->comp_expr_str[i]);
104  av_freep(&s->lut[i]);
105  }
106 }
107 
108 #define BIT8_FMTS \
109  AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, \
110  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, \
111  AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, \
112  AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
113  AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, \
114  AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
115 
116 #define BIT9_FMTS \
117  AV_PIX_FMT_GBRP9, AV_PIX_FMT_GRAY9, \
118  AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
119  AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
120 
121 #define BIT10_FMTS \
122  AV_PIX_FMT_GRAY10, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, \
123  AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
124  AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
125 
126 #define BIT12_FMTS \
127  AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, \
128  AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP12,
129 
130 #define BIT14_FMTS \
131  AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
132  AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRP14,
133 
134 #define BIT16_FMTS \
135  AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
136  AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, \
137  AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
138 
140 {
141  LUT2Context *s = ctx->priv;
142  static const enum AVPixelFormat all_pix_fmts[] = {
143  BIT8_FMTS
144  BIT9_FMTS
145  BIT10_FMTS
146  BIT12_FMTS
148  };
149  static const enum AVPixelFormat bit8_pix_fmts[] = {
150  BIT8_FMTS
151  AV_PIX_FMT_NONE
152  };
153  static const enum AVPixelFormat bit9_pix_fmts[] = {
154  BIT9_FMTS
155  AV_PIX_FMT_NONE
156  };
157  static const enum AVPixelFormat bit10_pix_fmts[] = {
158  BIT10_FMTS
159  AV_PIX_FMT_NONE
160  };
161  static const enum AVPixelFormat bit12_pix_fmts[] = {
162  BIT12_FMTS
163  AV_PIX_FMT_NONE
164  };
165  static const enum AVPixelFormat bit14_pix_fmts[] = {
166  BIT14_FMTS
167  AV_PIX_FMT_NONE
168  };
169  static const enum AVPixelFormat bit16_pix_fmts[] = {
170  BIT16_FMTS
171  AV_PIX_FMT_NONE
172  };
173  const enum AVPixelFormat *pix_fmts;
174  int ret;
175 
176  if (s->tlut2 || !s->odepth)
177  return ff_set_common_formats(ctx, ff_make_format_list(all_pix_fmts));
178 
179  ret = ff_formats_ref(ff_make_format_list(all_pix_fmts), &ctx->inputs[0]->out_formats);
180  if (ret < 0)
181  return ret;
182 
183  switch (s->odepth) {
184  case 8: pix_fmts = bit8_pix_fmts; break;
185  case 9: pix_fmts = bit9_pix_fmts; break;
186  case 10: pix_fmts = bit10_pix_fmts; break;
187  case 12: pix_fmts = bit12_pix_fmts; break;
188  case 14: pix_fmts = bit14_pix_fmts; break;
189  case 16: pix_fmts = bit16_pix_fmts; break;
190  default: av_log(ctx, AV_LOG_ERROR, "Unsupported output bit depth %d.\n", s->odepth);
191  return AVERROR(EINVAL);
192  }
193 
194  return ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->outputs[0]->in_formats);
195 }
196 
197 static int config_inputx(AVFilterLink *inlink)
198 {
199  AVFilterContext *ctx = inlink->dst;
200  LUT2Context *s = ctx->priv;
202  int hsub = desc->log2_chroma_w;
203  int vsub = desc->log2_chroma_h;
204 
206  s->heightx[1] = s->heightx[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
207  s->heightx[0] = s->heightx[3] = inlink->h;
208  s->widthx[1] = s->widthx[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
209  s->widthx[0] = s->widthx[3] = inlink->w;
210 
211  s->var_values[VAR_W] = inlink->w;
212  s->var_values[VAR_H] = inlink->h;
213  s->depthx = desc->comp[0].depth;
215 
216  if (s->tlut2) {
217  s->depthy = desc->comp[0].depth;
219  }
220 
221  return 0;
222 }
223 
224 static int config_inputy(AVFilterLink *inlink)
225 {
226  AVFilterContext *ctx = inlink->dst;
227  LUT2Context *s = ctx->priv;
229  int hsub = desc->log2_chroma_w;
230  int vsub = desc->log2_chroma_h;
231 
233  s->depthy = desc->comp[0].depth;
235  s->heighty[1] = s->heighty[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
236  s->heighty[0] = s->heighty[3] = inlink->h;
237  s->widthy[1] = s->widthy[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
238  s->widthy[0] = s->widthy[3] = inlink->w;
239 
240  return 0;
241 }
242 
243 #define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv) \
244 static int lut2_##zname##_##xname##_##yname(AVFilterContext *ctx, \
245  void *arg, \
246  int jobnr, int nb_jobs) \
247 { \
248  LUT2Context *s = ctx->priv; \
249  ThreadData *td = arg; \
250  AVFrame *out = td->out; \
251  AVFrame *srcx = td->srcx; \
252  AVFrame *srcy = td->srcy; \
253  const int odepth = s->odepth; \
254  int p, y, x; \
255  \
256  for (p = 0; p < s->nb_planes; p++) { \
257  const int slice_start = (s->heightx[p] * jobnr) / nb_jobs; \
258  const int slice_end = (s->heightx[p] * (jobnr+1)) / nb_jobs; \
259  const uint16_t *lut = s->lut[p]; \
260  const xtype *srcxx; \
261  const ytype *srcyy; \
262  ztype *dst; \
263  \
264  dst = (ztype *)(out->data[p] + slice_start * out->linesize[p]); \
265  srcxx = (const xtype *)(srcx->data[p] + slice_start * srcx->linesize[p]);\
266  srcyy = (const ytype *)(srcy->data[p] + slice_start * srcy->linesize[p]);\
267  \
268  for (y = slice_start; y < slice_end; y++) { \
269  for (x = 0; x < s->widthx[p]; x++) { \
270  dst[x] = av_clip_uintp2_c(lut[(srcyy[x] << s->depthx) | srcxx[x]], odepth); \
271  } \
272  \
273  dst += out->linesize[p] / zdiv; \
274  srcxx += srcx->linesize[p] / xdiv; \
275  srcyy += srcy->linesize[p] / ydiv; \
276  } \
277  } \
278  return 0; \
279 }
280 
281 DEFINE_LUT2(8, 8, 8, uint8_t, uint8_t, uint8_t, 1, 1, 1)
282 DEFINE_LUT2(8, 8, 16, uint8_t, uint8_t, uint16_t, 1, 1, 2)
283 DEFINE_LUT2(8, 16, 8, uint8_t, uint16_t, uint8_t, 1, 2, 1)
284 DEFINE_LUT2(8, 16, 16, uint8_t, uint16_t, uint16_t, 1, 2, 2)
285 DEFINE_LUT2(16, 8, 8, uint16_t, uint8_t, uint8_t, 2, 1, 1)
286 DEFINE_LUT2(16, 8, 16, uint16_t, uint8_t, uint16_t, 2, 1, 2)
287 DEFINE_LUT2(16, 16, 8, uint16_t, uint16_t, uint8_t, 2, 2, 1)
288 DEFINE_LUT2(16, 16, 16, uint16_t, uint16_t, uint16_t, 2, 2, 2)
289 
291 {
292  AVFilterContext *ctx = fs->parent;
293  LUT2Context *s = fs->opaque;
294  AVFilterLink *outlink = ctx->outputs[0];
295  AVFrame *out, *srcx = NULL, *srcy = NULL;
296  int ret;
297 
298  if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
299  (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
300  return ret;
301 
302  if (ctx->is_disabled || !srcy) {
303  out = av_frame_clone(srcx);
304  if (!out)
305  return AVERROR(ENOMEM);
306  } else {
307  ThreadData td;
308 
309  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
310  if (!out)
311  return AVERROR(ENOMEM);
312  av_frame_copy_props(out, srcx);
313 
314  td.out = out;
315  td.srcx = srcx;
316  td.srcy = srcy;
317  ctx->internal->execute(ctx, s->lut2, &td, NULL, FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
318  }
319 
320  out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
321 
322  return ff_filter_frame(outlink, out);
323 }
324 
325 static int config_output(AVFilterLink *outlink)
326 {
327  AVFilterContext *ctx = outlink->src;
328  LUT2Context *s = ctx->priv;
329  int p, ret;
330 
331  s->depth = s->depthx + s->depthy;
332  s->nb_planes = s->nb_planesx;
333 
334  s->lut2 = s->depth > 16 ? lut2_16_16_16 : lut2_8_8_8;
335  if (s->odepth) {
336  if (s->depthx == 8 && s->depthy == 8 && s->odepth > 8)
337  s->lut2 = lut2_16_8_8;
338  if (s->depthx > 8 && s->depthy == 8 && s->odepth > 8)
339  s->lut2 = lut2_16_16_8;
340  if (s->depthx == 8 && s->depthy > 8 && s->odepth > 8)
341  s->lut2 = lut2_16_8_16;
342  if (s->depthx == 8 && s->depthy == 8 && s->odepth == 8)
343  s->lut2 = lut2_8_8_8;
344  if (s->depthx > 8 && s->depthy == 8 && s->odepth == 8)
345  s->lut2 = lut2_8_16_8;
346  if (s->depthx == 8 && s->depthy > 8 && s->odepth == 8)
347  s->lut2 = lut2_8_8_16;
348  if (s->depthx > 8 && s->depthy > 8 && s->odepth == 8)
349  s->lut2 = lut2_8_16_16;
350  } else {
351  s->odepth = s->depthx;
352  }
353 
354  for (p = 0; p < s->nb_planes; p++) {
355  s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
356  if (!s->lut[p])
357  return AVERROR(ENOMEM);
358  }
359 
360  for (p = 0; p < s->nb_planes; p++) {
361  double res;
362  int x, y;
363 
364  /* create the parsed expression */
365  av_expr_free(s->comp_expr[p]);
366  s->comp_expr[p] = NULL;
367  ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
368  var_names, NULL, NULL, NULL, NULL, 0, ctx);
369  if (ret < 0) {
370  av_log(ctx, AV_LOG_ERROR,
371  "Error when parsing the expression '%s' for the component %d.\n",
372  s->comp_expr_str[p], p);
373  return AVERROR(EINVAL);
374  }
375 
376  /* compute the lut */
377  for (y = 0; y < (1 << s->depthy); y++) {
378  s->var_values[VAR_Y] = y;
379  for (x = 0; x < (1 << s->depthx); x++) {
380  s->var_values[VAR_X] = x;
381  res = av_expr_eval(s->comp_expr[p], s->var_values, s);
382  if (isnan(res)) {
383  av_log(ctx, AV_LOG_ERROR,
384  "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
385  s->comp_expr_str[p], x, y, p);
386  return AVERROR(EINVAL);
387  }
388 
389  s->lut[p][(y << s->depthx) + x] = res;
390  }
391  }
392  }
393 
394  return 0;
395 }
396 
397 static int lut2_config_output(AVFilterLink *outlink)
398 {
399  AVFilterContext *ctx = outlink->src;
400  LUT2Context *s = ctx->priv;
401  AVFilterLink *srcx = ctx->inputs[0];
402  AVFilterLink *srcy = ctx->inputs[1];
403  FFFrameSyncIn *in;
405  int hsub = desc->log2_chroma_w;
406  int vsub = desc->log2_chroma_h;
407  int ret;
408 
409  outlink->w = srcx->w;
410  outlink->h = srcx->h;
411  outlink->time_base = srcx->time_base;
412  outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
413  outlink->frame_rate = srcx->frame_rate;
414 
416  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(outlink->h, vsub);
417  s->height[0] = s->height[3] = outlink->h;
418  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(outlink->w, hsub);
419  s->width[0] = s->width[3] = outlink->w;
420 
421  if (!s->odepth && srcx->format != srcy->format) {
422  av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
423  return AVERROR(EINVAL);
424  }
425 
426  if (srcx->w != srcy->w || srcx->h != srcy->h) {
427  av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
428  "(size %dx%d) do not match the corresponding "
429  "second input link %s parameters (size %dx%d)\n",
430  ctx->input_pads[0].name, srcx->w, srcx->h,
431  ctx->input_pads[1].name,
432  srcy->w, srcy->h);
433  return AVERROR(EINVAL);
434  }
435 
436  if (s->nb_planesx != s->nb_planesy) {
437  av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
438  "(%d) do not match the corresponding "
439  "second input link %s number of planes (%d)\n",
440  ctx->input_pads[0].name, s->nb_planesx,
441  ctx->input_pads[1].name, s->nb_planesy);
442  return AVERROR(EINVAL);
443  }
444 
445  if (s->nb_planesx != s->nb_planes) {
446  av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
447  "(%d) do not match the corresponding "
448  "output link %s number of planes (%d)\n",
449  ctx->input_pads[0].name, s->nb_planesx,
450  ctx->output_pads[0].name, s->nb_planes);
451  return AVERROR(EINVAL);
452  }
453 
454  if (s->widthx[1] != s->widthy[1] || s->heightx[1] != s->heighty[1]) {
455  av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
456  "(size %dx%d) do not match the corresponding "
457  "second input link %s 2nd plane (size %dx%d)\n",
458  ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
459  ctx->input_pads[1].name,
460  s->widthy[1], s->heighty[1]);
461  return AVERROR(EINVAL);
462  }
463 
464  if (s->widthx[2] != s->widthy[2] || s->heightx[2] != s->heighty[2]) {
465  av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
466  "(size %dx%d) do not match the corresponding "
467  "second input link %s 3rd plane (size %dx%d)\n",
468  ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
469  ctx->input_pads[1].name,
470  s->widthy[2], s->heighty[2]);
471  return AVERROR(EINVAL);
472  }
473 
474  if (s->widthx[1] != s->width[1] || s->heightx[1] != s->height[1]) {
475  av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
476  "(size %dx%d) do not match the corresponding "
477  "output link %s 2nd plane (size %dx%d)\n",
478  ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
479  ctx->output_pads[0].name, s->width[1], s->height[1]);
480  return AVERROR(EINVAL);
481  }
482 
483  if (s->widthx[2] != s->width[2] || s->heightx[2] != s->height[2]) {
484  av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
485  "(size %dx%d) do not match the corresponding "
486  "output link %s 3rd plane (size %dx%d)\n",
487  ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
488  ctx->output_pads[0].name, s->width[2], s->height[2]);
489  return AVERROR(EINVAL);
490  }
491 
492  if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
493  return ret;
494 
495  in = s->fs.in;
496  in[0].time_base = srcx->time_base;
497  in[1].time_base = srcy->time_base;
498  in[0].sync = 2;
499  in[0].before = EXT_STOP;
500  in[0].after = EXT_INFINITY;
501  in[1].sync = 1;
502  in[1].before = EXT_STOP;
503  in[1].after = EXT_INFINITY;
504  s->fs.opaque = s;
506 
507  if ((ret = config_output(outlink)) < 0)
508  return ret;
509 
510  ret = ff_framesync_configure(&s->fs);
511  outlink->time_base = s->fs.time_base;
512 
513  return ret;
514 }
515 
517 {
518  LUT2Context *s = ctx->priv;
519  return ff_framesync_activate(&s->fs);
520 }
521 
522 static const AVFilterPad inputs[] = {
523  {
524  .name = "srcx",
525  .type = AVMEDIA_TYPE_VIDEO,
526  .config_props = config_inputx,
527  },
528  {
529  .name = "srcy",
530  .type = AVMEDIA_TYPE_VIDEO,
531  .config_props = config_inputy,
532  },
533  { NULL }
534 };
535 
536 static const AVFilterPad outputs[] = {
537  {
538  .name = "default",
539  .type = AVMEDIA_TYPE_VIDEO,
540  .config_props = lut2_config_output,
541  },
542  { NULL }
543 };
544 
545 #define lut2_options options
546 
548 
550  .name = "lut2",
551  .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
552  .preinit = lut2_framesync_preinit,
553  .priv_size = sizeof(LUT2Context),
554  .priv_class = &lut2_class,
555  .uninit = uninit,
557  .activate = activate,
558  .inputs = inputs,
559  .outputs = outputs,
562 };
563 
564 #if CONFIG_TLUT2_FILTER
565 
566 static av_cold int init(AVFilterContext *ctx)
567 {
568  LUT2Context *s = ctx->priv;
569 
570  s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
571 
572  return 0;
573 }
574 
575 static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
576 {
577  AVFilterContext *ctx = inlink->dst;
578  LUT2Context *s = ctx->priv;
579  AVFilterLink *outlink = ctx->outputs[0];
580 
581  if (s->prev_frame) {
582  AVFrame *out;
583 
584  if (ctx->is_disabled) {
585  out = av_frame_clone(frame);
586  } else {
587  ThreadData td;
588 
589  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
590  if (!out) {
592  s->prev_frame = frame;
593  return AVERROR(ENOMEM);
594  }
595 
596  av_frame_copy_props(out, frame);
597 
598  td.out = out;
599  td.srcx = frame;
600  td.srcy = s->prev_frame;
601  ctx->internal->execute(ctx, s->lut2, &td, NULL, FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
602  }
604  s->prev_frame = frame;
605  return ff_filter_frame(outlink, out);
606  }
607  s->prev_frame = frame;
608  return 0;
609 }
610 
611 static const AVOption tlut2_options[] = {
612  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
613  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
614  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
615  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
616  { NULL }
617 };
618 
620 
621 static const AVFilterPad tlut2_inputs[] = {
622  {
623  .name = "default",
624  .type = AVMEDIA_TYPE_VIDEO,
625  .filter_frame = tlut2_filter_frame,
626  .config_props = config_inputx,
627  },
628  { NULL }
629 };
630 
631 static const AVFilterPad tlut2_outputs[] = {
632  {
633  .name = "default",
634  .type = AVMEDIA_TYPE_VIDEO,
635  .config_props = config_output,
636  },
637  { NULL }
638 };
639 
641  .name = "tlut2",
642  .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
643  .priv_size = sizeof(LUT2Context),
644  .priv_class = &tlut2_class,
646  .init = init,
647  .uninit = uninit,
648  .inputs = tlut2_inputs,
649  .outputs = tlut2_outputs,
652 };
653 
654 #endif
#define BIT9_FMTS
Definition: vf_lut2.c:116
#define NULL
Definition: coverity.c:32
AVFilter ff_vf_tlut2
AVFrame * out
Definition: af_adeclick.c:488
static int config_inputx(AVFilterLink *inlink)
Definition: vf_lut2.c:197
int heightx[4]
Definition: vf_lut2.c:64
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2562
Main libavfilter public API header.
static int config_output(AVFilterLink *outlink)
Definition: vf_lut2.c:325
const char * desc
Definition: nvenc.c:68
static enum AVPixelFormat all_pix_fmts[]
Definition: jpeg2000dec.c:252
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs)
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:117
#define BIT10_FMTS
Definition: vf_lut2.c:121
int nb_planes
Definition: vf_lut2.c:68
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:679
int depth
Definition: vf_lut2.c:69
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
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:385
int64_t pts
Timestamp of the current event.
Definition: framesync.h:167
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
Macro definitions for various function/variable attributes.
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:86
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
static int query_formats(AVFilterContext *ctx)
Definition: vf_lut2.c:139
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:349
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
FFFrameSync fs
Definition: vf_lut2.c:55
#define av_cold
Definition: attributes.h:82
AVOptions.
Definition: vf_lut2.c:47
double var_values[VAR_VARS_NB]
Definition: vf_lut2.c:61
#define FLAGS
Definition: vf_lut2.c:81
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
Definition: eval.c:157
FFFrameSyncIn * in
Pointer to array of inputs.
Definition: framesync.h:203
static int process_frame(FFFrameSync *fs)
Definition: vf_lut2.c:290
static AVFrame * frame
Definition: vf_lut2.c:45
static const AVFilterPad outputs[]
Definition: vf_lut2.c:536
Definition: vf_lut2.c:46
AVFrame * prev_frame
Definition: vf_lut2.c:71
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
Input stream structure.
Definition: framesync.h:81
int depthy
Definition: vf_lut2.c:69
AVFrame * srcx
Definition: vf_lut2.c:77
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
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
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:345
int nb_planesx
Definition: vf_lut2.c:66
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#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 log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:293
int width[4]
Definition: vf_lut2.c:63
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 NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
int depthx
Definition: vf_lut2.c:69
const char * arg
Definition: jacosubdec.c:66
uint16_t * lut[4]
lookup table for each component
Definition: vf_lut2.c:62
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:96
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter&#39;s input and try to produce output.
Definition: framesync.c:344
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
Definition: framesync.h:172
AVFilter ff_vf_lut2
Definition: vf_lut2.c:549
var_name
Definition: aeval.c:46
static int activate(AVFilterContext *ctx)
Definition: vf_lut2.c:516
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
int(* lut2)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lut2.c:73
#define FFMIN(a, b)
Definition: common.h:96
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
Definition: vf_lut2.c:44
int heighty[4]
Definition: vf_lut2.c:65
AVFormatContext * ctx
Definition: movenc.c:48
AVRational time_base
Time base for the output events.
Definition: framesync.h:162
#define s(width, name)
Definition: cbs_vp9.c:257
int height[4]
Definition: vf_lut2.c:63
void * opaque
Opaque pointer, not used by the API.
Definition: framesync.h:177
int nb_planesy
Definition: vf_lut2.c:67
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static int lut2_config_output(AVFilterLink *outlink)
Definition: vf_lut2.c:397
static int config_inputy(AVFilterLink *inlink)
Definition: vf_lut2.c:224
Extend the frame to infinity.
Definition: framesync.h:75
misc drawing utilities
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:334
static const AVFilterPad inputs[]
Definition: vf_lut2.c:522
Used for passing data between threads.
Definition: af_adeclick.c:487
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:77
#define BIT12_FMTS
Definition: vf_lut2.c:126
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int widthy[4]
Definition: vf_lut2.c:65
int widthx[4]
Definition: vf_lut2.c:64
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)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events...
Definition: framesync.h:139
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
#define isnan(x)
Definition: libm.h:340
const char * name
Filter name.
Definition: avfilter.h:148
#define OFFSET(x)
Definition: vf_lut2.c:80
#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 const AVOption options[]
Definition: vf_lut2.c:83
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
#define flags(name, subs,...)
Definition: cbs_av1.c:564
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
int odepth
Definition: vf_lut2.c:57
int
common internal and external API header
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_lut2.c:92
avfilter_execute_func * execute
Definition: internal.h:155
Completely stop all streams with this one.
Definition: framesync.h:65
#define BIT16_FMTS
Definition: vf_lut2.c:134
int tlut2
Definition: vf_lut2.c:70
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:734
#define BIT8_FMTS
Definition: vf_lut2.c:108
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
static const char *const var_names[]
Definition: vf_lut2.c:33
AVExpr * comp_expr[4]
Definition: vf_lut2.c:60
An instance of a filter.
Definition: avfilter.h:338
char * comp_expr_str[4]
Definition: vf_lut2.c:58
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define av_malloc_array(a, b)
internal API functions
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:256
#define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv)
Definition: vf_lut2.c:243
int depth
Number of bits in the component.
Definition: pixdesc.h:58
#define BIT14_FMTS
Definition: vf_lut2.c:130
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
AVFrame * srcy
Definition: vf_lut2.c:77
simple arithmetic expression evaluator
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58