FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_lut.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Compute a look-up table for binding the input value to the output
24  * value, and apply it to input video.
25  */
26 
27 #include "libavutil/common.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "drawutils.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 static const char *const var_names[] = {
38  "w", ///< width of the input video
39  "h", ///< height of the input video
40  "val", ///< input value for the pixel
41  "maxval", ///< max value for the pixel
42  "minval", ///< min value for the pixel
43  "negval", ///< negated value
44  "clipval",
45  NULL
46 };
47 
48 enum var_name {
57 };
58 
59 typedef struct {
60  const AVClass *class;
61  uint8_t lut[4][256]; ///< lookup table for each component
62  char *comp_expr_str[4];
63  AVExpr *comp_expr[4];
64  int hsub, vsub;
65  double var_values[VAR_VARS_NB];
66  int is_rgb, is_yuv;
67  int step;
68  int negate_alpha; /* only used by negate */
69 } LutContext;
70 
71 #define Y 0
72 #define U 1
73 #define V 2
74 #define R 0
75 #define G 1
76 #define B 2
77 #define A 3
78 
79 #define OFFSET(x) offsetof(LutContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81 
82 static const AVOption options[] = {
83  {"c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
84  {"c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
85  {"c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
86  {"c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
87  {"y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
88  {"u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
89  {"v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
90  {"r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
91  {"g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
92  {"b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
93  {"a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
94  {NULL},
95 };
96 
97 static av_cold void uninit(AVFilterContext *ctx)
98 {
99  LutContext *lut = ctx->priv;
100  int i;
101 
102  for (i = 0; i < 4; i++) {
103  av_expr_free(lut->comp_expr[i]);
104  lut->comp_expr[i] = NULL;
105  av_freep(&lut->comp_expr_str[i]);
106  }
107 }
108 
109 #define YUV_FORMATS \
110  AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
111  AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
112  AV_PIX_FMT_YUVA420P, \
113  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
114  AV_PIX_FMT_YUVJ440P
115 
116 #define RGB_FORMATS \
117  AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
118  AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
119  AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24
120 
124 
126 {
127  LutContext *lut = ctx->priv;
128 
129  const enum AVPixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
131 
133  return 0;
134 }
135 
136 /**
137  * Clip value val in the minval - maxval range.
138  */
139 static double clip(void *opaque, double val)
140 {
141  LutContext *lut = opaque;
142  double minval = lut->var_values[VAR_MINVAL];
143  double maxval = lut->var_values[VAR_MAXVAL];
144 
145  return av_clip(val, minval, maxval);
146 }
147 
148 /**
149  * Compute gamma correction for value val, assuming the minval-maxval
150  * range, val is clipped to a value contained in the same interval.
151  */
152 static double compute_gammaval(void *opaque, double gamma)
153 {
154  LutContext *lut = opaque;
155  double val = lut->var_values[VAR_CLIPVAL];
156  double minval = lut->var_values[VAR_MINVAL];
157  double maxval = lut->var_values[VAR_MAXVAL];
158 
159  return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
160 }
161 
162 static double (* const funcs1[])(void *, double) = {
163  (void *)clip,
164  (void *)compute_gammaval,
165  NULL
166 };
167 
168 static const char * const funcs1_names[] = {
169  "clip",
170  "gammaval",
171  NULL
172 };
173 
174 static int config_props(AVFilterLink *inlink)
175 {
176  AVFilterContext *ctx = inlink->dst;
177  LutContext *lut = ctx->priv;
178  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
179  uint8_t rgba_map[4]; /* component index -> RGBA color index map */
180  int min[4], max[4];
181  int val, comp, ret;
182 
183  lut->hsub = desc->log2_chroma_w;
184  lut->vsub = desc->log2_chroma_h;
185 
186  lut->var_values[VAR_W] = inlink->w;
187  lut->var_values[VAR_H] = inlink->h;
188 
189  switch (inlink->format) {
190  case AV_PIX_FMT_YUV410P:
191  case AV_PIX_FMT_YUV411P:
192  case AV_PIX_FMT_YUV420P:
193  case AV_PIX_FMT_YUV422P:
194  case AV_PIX_FMT_YUV440P:
195  case AV_PIX_FMT_YUV444P:
196  case AV_PIX_FMT_YUVA420P:
197  min[Y] = min[U] = min[V] = 16;
198  max[Y] = 235;
199  max[U] = max[V] = 240;
200  min[A] = 0; max[A] = 255;
201  break;
202  default:
203  min[0] = min[1] = min[2] = min[3] = 0;
204  max[0] = max[1] = max[2] = max[3] = 255;
205  }
206 
207  lut->is_yuv = lut->is_rgb = 0;
208  if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
209  else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
210 
211  if (lut->is_rgb) {
212  ff_fill_rgba_map(rgba_map, inlink->format);
213  lut->step = av_get_bits_per_pixel(desc) >> 3;
214  }
215 
216  for (comp = 0; comp < desc->nb_components; comp++) {
217  double res;
218  int color = lut->is_rgb ? rgba_map[comp] : comp;
219 
220  /* create the parsed expression */
221  ret = av_expr_parse(&lut->comp_expr[color], lut->comp_expr_str[color],
222  var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
223  if (ret < 0) {
224  av_log(ctx, AV_LOG_ERROR,
225  "Error when parsing the expression '%s' for the component %d and color %d.\n",
226  lut->comp_expr_str[comp], comp, color);
227  return AVERROR(EINVAL);
228  }
229 
230  /* compute the lut */
231  lut->var_values[VAR_MAXVAL] = max[color];
232  lut->var_values[VAR_MINVAL] = min[color];
233 
234  for (val = 0; val < 256; val++) {
235  lut->var_values[VAR_VAL] = val;
236  lut->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
237  lut->var_values[VAR_NEGVAL] =
238  av_clip(min[color] + max[color] - lut->var_values[VAR_VAL],
239  min[color], max[color]);
240 
241  res = av_expr_eval(lut->comp_expr[color], lut->var_values, lut);
242  if (isnan(res)) {
243  av_log(ctx, AV_LOG_ERROR,
244  "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
245  lut->comp_expr_str[color], val, comp);
246  return AVERROR(EINVAL);
247  }
248  lut->lut[comp][val] = av_clip((int)res, min[color], max[color]);
249  av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
250  }
251  }
252 
253  return 0;
254 }
255 
257 {
258  AVFilterContext *ctx = inlink->dst;
259  LutContext *lut = ctx->priv;
260  AVFilterLink *outlink = ctx->outputs[0];
261  AVFilterBufferRef *out;
262  uint8_t *inrow, *outrow, *inrow0, *outrow0;
263  int i, j, plane;
264 
265  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
266  if (!out) {
268  return AVERROR(ENOMEM);
269  }
271 
272  if (lut->is_rgb) {
273  /* packed */
274  inrow0 = in ->data[0];
275  outrow0 = out->data[0];
276 
277  for (i = 0; i < in->video->h; i ++) {
278  int w = inlink->w;
279  const uint8_t (*tab)[256] = (const uint8_t (*)[256])lut->lut;
280  inrow = inrow0;
281  outrow = outrow0;
282  for (j = 0; j < w; j++) {
283  outrow[0] = tab[0][inrow[0]];
284  if (lut->step>1) {
285  outrow[1] = tab[1][inrow[1]];
286  if (lut->step>2) {
287  outrow[2] = tab[2][inrow[2]];
288  if (lut->step>3) {
289  outrow[3] = tab[3][inrow[3]];
290  }
291  }
292  }
293  outrow += lut->step;
294  inrow += lut->step;
295  }
296  inrow0 += in ->linesize[0];
297  outrow0 += out->linesize[0];
298  }
299  } else {
300  /* planar */
301  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
302  int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
303  int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
304 
305  inrow = in ->data[plane];
306  outrow = out->data[plane];
307 
308  for (i = 0; i < (in->video->h + (1<<vsub) - 1)>>vsub; i ++) {
309  const uint8_t *tab = lut->lut[plane];
310  int w = (inlink->w + (1<<hsub) - 1)>>hsub;
311  for (j = 0; j < w; j++)
312  outrow[j] = tab[inrow[j]];
313  inrow += in ->linesize[plane];
314  outrow += out->linesize[plane];
315  }
316  }
317  }
318 
320  return ff_filter_frame(outlink, out);
321 }
322 
323 static const AVFilterPad inputs[] = {
324  { .name = "default",
325  .type = AVMEDIA_TYPE_VIDEO,
326  .filter_frame = filter_frame,
327  .config_props = config_props,
328  .min_perms = AV_PERM_READ, },
329  { .name = NULL}
330 };
331 static const AVFilterPad outputs[] = {
332  { .name = "default",
333  .type = AVMEDIA_TYPE_VIDEO, },
334  { .name = NULL}
335 };
336 #define DEFINE_LUT_FILTER(name_, description_) \
337  AVFilter avfilter_vf_##name_ = { \
338  .name = #name_, \
339  .description = NULL_IF_CONFIG_SMALL(description_), \
340  .priv_size = sizeof(LutContext), \
341  \
342  .init = name_##_init, \
343  .uninit = uninit, \
344  .query_formats = query_formats, \
345  \
346  .inputs = inputs, \
347  .outputs = outputs, \
348  .priv_class = &name_##_class, \
349  }
350 
351 #if CONFIG_LUT_FILTER
352 
353 #define lut_options options
355 
356 static int lut_init(AVFilterContext *ctx, const char *args)
357 {
358  LutContext *lut = ctx->priv;
359  int ret;
360 
361  lut->class = &lut_class;
362  av_opt_set_defaults(lut);
363 
364  if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
365  return ret;
366 
367  return 0;
368 }
369 
370 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.");
371 #endif
372 
373 #if CONFIG_LUTYUV_FILTER
374 
375 #define lutyuv_options options
376 AVFILTER_DEFINE_CLASS(lutyuv);
377 
378 static int lutyuv_init(AVFilterContext *ctx, const char *args)
379 {
380  LutContext *lut = ctx->priv;
381  int ret;
382 
383  lut->class = &lutyuv_class;
384  lut->is_yuv = 1;
385  av_opt_set_defaults(lut);
386 
387  if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
388  return ret;
389 
390  return 0;
391 }
392 
393 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.");
394 #endif
395 
396 #if CONFIG_LUTRGB_FILTER
397 
398 #define lutrgb_options options
399 AVFILTER_DEFINE_CLASS(lutrgb);
400 
401 static int lutrgb_init(AVFilterContext *ctx, const char *args)
402 {
403  LutContext *lut = ctx->priv;
404  int ret;
405 
406  lut->class = &lutrgb_class;
407  lut->is_rgb = 1;
408  av_opt_set_defaults(lut);
409 
410  if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
411  return ret;
412 
413  return 0;
414 }
415 
416 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.");
417 #endif
418 
419 #if CONFIG_NEGATE_FILTER
420 
421 #define negate_options options
422 AVFILTER_DEFINE_CLASS(negate);
423 
424 static int negate_init(AVFilterContext *ctx, const char *args)
425 {
426  LutContext *lut = ctx->priv;
427  char lut_params[64];
428 
429  if (args)
430  sscanf(args, "%d", &lut->negate_alpha);
431 
432  av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
433 
434  snprintf(lut_params, sizeof(lut_params), "c0=negval:c1=negval:c2=negval:a=%s",
435  lut->negate_alpha ? "negval" : "val");
436 
437  lut->class = &negate_class;
438  av_opt_set_defaults(lut);
439 
440  return av_set_options_string(lut, lut_params, "=", ":");
441 }
442 
443 DEFINE_LUT_FILTER(negate, "Negate input video.");
444 
445 #endif