Libav
vf_lut.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
27 #include "libavutil/attributes.h"
28 #include "libavutil/common.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 
38 static const char *const var_names[] = {
39  "E",
40  "PHI",
41  "PI",
42  "w",
43  "h",
44  "val",
45  "maxval",
46  "minval",
47  "negval",
48  "clipval",
49  NULL
50 };
51 
52 enum var_name {
64 };
65 
66 typedef struct {
67  const AVClass *class;
68  uint8_t lut[4][256];
69  char *comp_expr_str[4];
70  AVExpr *comp_expr[4];
71  int hsub, vsub;
72  double var_values[VAR_VARS_NB];
73  int is_rgb, is_yuv;
74  int rgba_map[4];
75  int step;
76  int negate_alpha; /* only used by negate */
77 } LutContext;
78 
79 #define Y 0
80 #define U 1
81 #define V 2
82 #define R 0
83 #define G 1
84 #define B 2
85 #define A 3
86 
87 #define OFFSET(x) offsetof(LutContext, x)
88 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
89 
90 static const AVOption lut_options[] = {
91  { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
92  { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
93  { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
94  { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
95  { "y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
96  { "u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
97  { "v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
98  { "r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
99  { "g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
100  { "b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
101  { "a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
102  { NULL },
103 };
104 
105 static av_cold int init(AVFilterContext *ctx)
106 {
107  LutContext *s = ctx->priv;
108 
109  s->var_values[VAR_PHI] = M_PHI;
110  s->var_values[VAR_PI] = M_PI;
111  s->var_values[VAR_E ] = M_E;
112 
113  s->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
114  s->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
115 
116  return 0;
117 }
118 
119 static av_cold void uninit(AVFilterContext *ctx)
120 {
121  LutContext *s = ctx->priv;
122  int i;
123 
124  for (i = 0; i < 4; i++) {
125  av_expr_free(s->comp_expr[i]);
126  s->comp_expr[i] = NULL;
127  av_freep(&s->comp_expr_str[i]);
128  }
129 }
130 
131 #define YUV_FORMATS \
132  AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
133  AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
134  AV_PIX_FMT_YUVA420P, \
135  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
136  AV_PIX_FMT_YUVJ440P
137 
138 #define RGB_FORMATS \
139  AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
140  AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
141  AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24
142 
146 
148 {
149  LutContext *s = ctx->priv;
150 
151  const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
152  s->is_yuv ? yuv_pix_fmts :
153  all_pix_fmts;
154 
156  return 0;
157 }
158 
162 static double clip(void *opaque, double val)
163 {
164  LutContext *s = opaque;
165  double minval = s->var_values[VAR_MINVAL];
166  double maxval = s->var_values[VAR_MAXVAL];
167 
168  return av_clip(val, minval, maxval);
169 }
170 
175 static double compute_gammaval(void *opaque, double gamma)
176 {
177  LutContext *s = opaque;
178  double val = s->var_values[VAR_CLIPVAL];
179  double minval = s->var_values[VAR_MINVAL];
180  double maxval = s->var_values[VAR_MAXVAL];
181 
182  return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
183 }
184 
185 static double (* const funcs1[])(void *, double) = {
186  clip,
188  NULL
189 };
190 
191 static const char * const funcs1_names[] = {
192  "clip",
193  "gammaval",
194  NULL
195 };
196 
197 static int config_props(AVFilterLink *inlink)
198 {
199  AVFilterContext *ctx = inlink->dst;
200  LutContext *s = ctx->priv;
201  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
202  int min[4], max[4];
203  int val, comp, ret;
204 
205  s->hsub = desc->log2_chroma_w;
206  s->vsub = desc->log2_chroma_h;
207 
208  s->var_values[VAR_W] = inlink->w;
209  s->var_values[VAR_H] = inlink->h;
210 
211  switch (inlink->format) {
212  case AV_PIX_FMT_YUV410P:
213  case AV_PIX_FMT_YUV411P:
214  case AV_PIX_FMT_YUV420P:
215  case AV_PIX_FMT_YUV422P:
216  case AV_PIX_FMT_YUV440P:
217  case AV_PIX_FMT_YUV444P:
218  case AV_PIX_FMT_YUVA420P:
219  min[Y] = min[U] = min[V] = 16;
220  max[Y] = 235;
221  max[U] = max[V] = 240;
222  min[A] = 0; max[A] = 255;
223  break;
224  default:
225  min[0] = min[1] = min[2] = min[3] = 0;
226  max[0] = max[1] = max[2] = max[3] = 255;
227  }
228 
229  s->is_yuv = s->is_rgb = 0;
230  if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) s->is_yuv = 1;
231  else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) s->is_rgb = 1;
232 
233  if (s->is_rgb) {
234  switch (inlink->format) {
235  case AV_PIX_FMT_ARGB: s->rgba_map[A] = 0; s->rgba_map[R] = 1; s->rgba_map[G] = 2; s->rgba_map[B] = 3; break;
236  case AV_PIX_FMT_ABGR: s->rgba_map[A] = 0; s->rgba_map[B] = 1; s->rgba_map[G] = 2; s->rgba_map[R] = 3; break;
237  case AV_PIX_FMT_RGBA:
238  case AV_PIX_FMT_RGB24: s->rgba_map[R] = 0; s->rgba_map[G] = 1; s->rgba_map[B] = 2; s->rgba_map[A] = 3; break;
239  case AV_PIX_FMT_BGRA:
240  case AV_PIX_FMT_BGR24: s->rgba_map[B] = 0; s->rgba_map[G] = 1; s->rgba_map[R] = 2; s->rgba_map[A] = 3; break;
241  }
242  s->step = av_get_bits_per_pixel(desc) >> 3;
243  }
244 
245  for (comp = 0; comp < desc->nb_components; comp++) {
246  double res;
247 
248  /* create the parsed expression */
249  av_expr_free(s->comp_expr[comp]);
250  s->comp_expr[comp] = NULL;
251  ret = av_expr_parse(&s->comp_expr[comp], s->comp_expr_str[comp],
252  var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
253  if (ret < 0) {
254  av_log(ctx, AV_LOG_ERROR,
255  "Error when parsing the expression '%s' for the component %d.\n",
256  s->comp_expr_str[comp], comp);
257  return AVERROR(EINVAL);
258  }
259 
260  /* compute the s */
261  s->var_values[VAR_MAXVAL] = max[comp];
262  s->var_values[VAR_MINVAL] = min[comp];
263 
264  for (val = 0; val < 256; val++) {
265  s->var_values[VAR_VAL] = val;
266  s->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
267  s->var_values[VAR_NEGVAL] =
268  av_clip(min[comp] + max[comp] - s->var_values[VAR_VAL],
269  min[comp], max[comp]);
270 
271  res = av_expr_eval(s->comp_expr[comp], s->var_values, s);
272  if (isnan(res)) {
273  av_log(ctx, AV_LOG_ERROR,
274  "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
275  s->comp_expr_str[comp], val, comp);
276  return AVERROR(EINVAL);
277  }
278  s->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
279  av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
280  }
281  }
282 
283  return 0;
284 }
285 
286 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
287 {
288  AVFilterContext *ctx = inlink->dst;
289  LutContext *s = ctx->priv;
290  AVFilterLink *outlink = ctx->outputs[0];
291  AVFrame *out;
292  uint8_t *inrow, *outrow, *inrow0, *outrow0;
293  int i, j, k, plane;
294 
295  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
296  if (!out) {
297  av_frame_free(&in);
298  return AVERROR(ENOMEM);
299  }
300  av_frame_copy_props(out, in);
301 
302  if (s->is_rgb) {
303  /* packed */
304  inrow0 = in ->data[0];
305  outrow0 = out->data[0];
306 
307  for (i = 0; i < in->height; i ++) {
308  inrow = inrow0;
309  outrow = outrow0;
310  for (j = 0; j < inlink->w; j++) {
311  for (k = 0; k < s->step; k++)
312  outrow[k] = s->lut[s->rgba_map[k]][inrow[k]];
313  outrow += s->step;
314  inrow += s->step;
315  }
316  inrow0 += in ->linesize[0];
317  outrow0 += out->linesize[0];
318  }
319  } else {
320  /* planar */
321  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
322  int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
323  int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
324 
325  inrow = in ->data[plane];
326  outrow = out->data[plane];
327 
328  for (i = 0; i < in->height >> vsub; i ++) {
329  for (j = 0; j < inlink->w>>hsub; j++)
330  outrow[j] = s->lut[plane][inrow[j]];
331  inrow += in ->linesize[plane];
332  outrow += out->linesize[plane];
333  }
334  }
335  }
336 
337  av_frame_free(&in);
338  return ff_filter_frame(outlink, out);
339 }
340 
341 static const AVFilterPad inputs[] = {
342  { .name = "default",
343  .type = AVMEDIA_TYPE_VIDEO,
344  .filter_frame = filter_frame,
345  .config_props = config_props,
346  },
347  { .name = NULL}
348 };
349 static const AVFilterPad outputs[] = {
350  { .name = "default",
351  .type = AVMEDIA_TYPE_VIDEO, },
352  { .name = NULL}
353 };
354 #define DEFINE_LUT_FILTER(name_, description_, init_, options) \
355  static const AVClass name_ ## _class = { \
356  .class_name = #name_, \
357  .item_name = av_default_item_name, \
358  .option = options, \
359  .version = LIBAVUTIL_VERSION_INT, \
360  }; \
361  AVFilter ff_vf_##name_ = { \
362  .name = #name_, \
363  .description = NULL_IF_CONFIG_SMALL(description_), \
364  .priv_size = sizeof(LutContext), \
365  .priv_class = &name_ ## _class, \
366  \
367  .init = init_, \
368  .uninit = uninit, \
369  .query_formats = query_formats, \
370  \
371  .inputs = inputs, \
372  .outputs = outputs, \
373  }
374 
375 #if CONFIG_LUT_FILTER
376 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init, lut_options);
377 #endif
378 #if CONFIG_LUTYUV_FILTER
379 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
380 #endif
381 #if CONFIG_LUTRGB_FILTER
382 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
383 #endif
384 
385 #if CONFIG_NEGATE_FILTER
386 
387 static const AVOption negate_options[] = {
388  { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
389  { NULL },
390 };
391 
392 static av_cold int negate_init(AVFilterContext *ctx)
393 {
394  LutContext *s = ctx->priv;
395  int i;
396 
397  av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
398 
399  for (i = 0; i < 4; i++) {
400  s->comp_expr_str[i] = av_strdup((i == 3 && s->negate_alpha) ?
401  "val" : "negval");
402  if (!s->comp_expr_str[i]) {
403  uninit(ctx);
404  return AVERROR(ENOMEM);
405  }
406  }
407 
408  return init(ctx);
409 }
410 
411 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);
412 
413 #endif