2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of Libav.
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.
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.
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
23 * Compute a look-up table for binding the input value to the output
24 * value, and apply it to input video.
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"
38 static const char *const var_names[] = {
42 "w", ///< width of the input video
43 "h", ///< height of the input video
44 "val", ///< input value for the pixel
45 "maxval", ///< max value for the pixel
46 "minval", ///< min value for the pixel
47 "negval", ///< negated value
68 uint8_t lut[4][256]; ///< lookup table for each component
69 char *comp_expr_str[4];
72 double var_values[VAR_VARS_NB];
76 int negate_alpha; /* only used by negate */
87 #define OFFSET(x) offsetof(LutContext, x)
88 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
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 },
105 static av_cold int init(AVFilterContext *ctx)
107 LutContext *s = ctx->priv;
109 s->var_values[VAR_PHI] = M_PHI;
110 s->var_values[VAR_PI] = M_PI;
111 s->var_values[VAR_E ] = M_E;
113 s->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
114 s->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
119 static av_cold void uninit(AVFilterContext *ctx)
121 LutContext *s = ctx->priv;
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]);
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, \
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
143 static const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
144 static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
145 static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
147 static int query_formats(AVFilterContext *ctx)
149 LutContext *s = ctx->priv;
151 const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
152 s->is_yuv ? yuv_pix_fmts :
155 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
160 * Clip value val in the minval - maxval range.
162 static double clip(void *opaque, double val)
164 LutContext *s = opaque;
165 double minval = s->var_values[VAR_MINVAL];
166 double maxval = s->var_values[VAR_MAXVAL];
168 return av_clip(val, minval, maxval);
172 * Compute gamma correction for value val, assuming the minval-maxval
173 * range, val is clipped to a value contained in the same interval.
175 static double compute_gammaval(void *opaque, double gamma)
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];
182 return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
185 static double (* const funcs1[])(void *, double) = {
191 static const char * const funcs1_names[] = {
197 static int config_props(AVFilterLink *inlink)
199 AVFilterContext *ctx = inlink->dst;
200 LutContext *s = ctx->priv;
201 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
205 s->hsub = desc->log2_chroma_w;
206 s->vsub = desc->log2_chroma_h;
208 s->var_values[VAR_W] = inlink->w;
209 s->var_values[VAR_H] = inlink->h;
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;
221 max[U] = max[V] = 240;
222 min[A] = 0; max[A] = 255;
225 min[0] = min[1] = min[2] = min[3] = 0;
226 max[0] = max[1] = max[2] = max[3] = 255;
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;
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;
242 s->step = av_get_bits_per_pixel(desc) >> 3;
245 for (comp = 0; comp < desc->nb_components; comp++) {
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);
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);
261 s->var_values[VAR_MAXVAL] = max[comp];
262 s->var_values[VAR_MINVAL] = min[comp];
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]);
271 res = av_expr_eval(s->comp_expr[comp], s->var_values, s);
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);
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]);
286 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
288 AVFilterContext *ctx = inlink->dst;
289 LutContext *s = ctx->priv;
290 AVFilterLink *outlink = ctx->outputs[0];
292 uint8_t *inrow, *outrow, *inrow0, *outrow0;
295 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
298 return AVERROR(ENOMEM);
300 av_frame_copy_props(out, in);
304 inrow0 = in ->data[0];
305 outrow0 = out->data[0];
307 for (i = 0; i < in->height; i ++) {
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]];
316 inrow0 += in ->linesize[0];
317 outrow0 += out->linesize[0];
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;
325 inrow = in ->data[plane];
326 outrow = out->data[plane];
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];
338 return ff_filter_frame(outlink, out);
341 static const AVFilterPad inputs[] = {
343 .type = AVMEDIA_TYPE_VIDEO,
344 .filter_frame = filter_frame,
345 .config_props = config_props,
349 static const AVFilterPad outputs[] = {
351 .type = AVMEDIA_TYPE_VIDEO, },
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, \
359 .version = LIBAVUTIL_VERSION_INT, \
361 AVFilter ff_vf_##name_ = { \
363 .description = NULL_IF_CONFIG_SMALL(description_), \
364 .priv_size = sizeof(LutContext), \
365 .priv_class = &name_ ## _class, \
369 .query_formats = query_formats, \
372 .outputs = outputs, \
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);
378 #if CONFIG_LUTYUV_FILTER
379 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
381 #if CONFIG_LUTRGB_FILTER
382 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
385 #if CONFIG_NEGATE_FILTER
387 static const AVOption negate_options[] = {
388 { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
392 static av_cold int negate_init(AVFilterContext *ctx)
394 LutContext *s = ctx->priv;
397 av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
399 for (i = 0; i < 4; i++) {
400 s->comp_expr_str[i] = av_strdup((i == 3 && s->negate_alpha) ?
402 if (!s->comp_expr_str[i]) {
404 return AVERROR(ENOMEM);
411 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);