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 *lut = ctx->priv;
109 lut->var_values[VAR_PHI] = M_PHI;
110 lut->var_values[VAR_PI] = M_PI;
111 lut->var_values[VAR_E ] = M_E;
113 lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
114 lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
119 static av_cold void uninit(AVFilterContext *ctx)
121 LutContext *lut = ctx->priv;
124 for (i = 0; i < 4; i++) {
125 av_expr_free(lut->comp_expr[i]);
126 lut->comp_expr[i] = NULL;
127 av_freep(&lut->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 enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
144 static enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
145 static enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
147 static int query_formats(AVFilterContext *ctx)
149 LutContext *lut = ctx->priv;
151 enum AVPixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
152 lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
154 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
159 * Clip value val in the minval - maxval range.
161 static double clip(void *opaque, double val)
163 LutContext *lut = opaque;
164 double minval = lut->var_values[VAR_MINVAL];
165 double maxval = lut->var_values[VAR_MAXVAL];
167 return av_clip(val, minval, maxval);
171 * Compute gamma correction for value val, assuming the minval-maxval
172 * range, val is clipped to a value contained in the same interval.
174 static double compute_gammaval(void *opaque, double gamma)
176 LutContext *lut = opaque;
177 double val = lut->var_values[VAR_CLIPVAL];
178 double minval = lut->var_values[VAR_MINVAL];
179 double maxval = lut->var_values[VAR_MAXVAL];
181 return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
184 static double (* const funcs1[])(void *, double) = {
190 static const char * const funcs1_names[] = {
196 static int config_props(AVFilterLink *inlink)
198 AVFilterContext *ctx = inlink->dst;
199 LutContext *lut = ctx->priv;
200 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
204 lut->hsub = desc->log2_chroma_w;
205 lut->vsub = desc->log2_chroma_h;
207 lut->var_values[VAR_W] = inlink->w;
208 lut->var_values[VAR_H] = inlink->h;
210 switch (inlink->format) {
211 case AV_PIX_FMT_YUV410P:
212 case AV_PIX_FMT_YUV411P:
213 case AV_PIX_FMT_YUV420P:
214 case AV_PIX_FMT_YUV422P:
215 case AV_PIX_FMT_YUV440P:
216 case AV_PIX_FMT_YUV444P:
217 case AV_PIX_FMT_YUVA420P:
218 min[Y] = min[U] = min[V] = 16;
220 max[U] = max[V] = 240;
221 min[A] = 0; max[A] = 255;
224 min[0] = min[1] = min[2] = min[3] = 0;
225 max[0] = max[1] = max[2] = max[3] = 255;
228 lut->is_yuv = lut->is_rgb = 0;
229 if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
230 else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
233 switch (inlink->format) {
234 case AV_PIX_FMT_ARGB: lut->rgba_map[A] = 0; lut->rgba_map[R] = 1; lut->rgba_map[G] = 2; lut->rgba_map[B] = 3; break;
235 case AV_PIX_FMT_ABGR: lut->rgba_map[A] = 0; lut->rgba_map[B] = 1; lut->rgba_map[G] = 2; lut->rgba_map[R] = 3; break;
236 case AV_PIX_FMT_RGBA:
237 case AV_PIX_FMT_RGB24: lut->rgba_map[R] = 0; lut->rgba_map[G] = 1; lut->rgba_map[B] = 2; lut->rgba_map[A] = 3; break;
238 case AV_PIX_FMT_BGRA:
239 case AV_PIX_FMT_BGR24: lut->rgba_map[B] = 0; lut->rgba_map[G] = 1; lut->rgba_map[R] = 2; lut->rgba_map[A] = 3; break;
241 lut->step = av_get_bits_per_pixel(desc) >> 3;
244 for (comp = 0; comp < desc->nb_components; comp++) {
247 /* create the parsed expression */
248 ret = av_expr_parse(&lut->comp_expr[comp], lut->comp_expr_str[comp],
249 var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
251 av_log(ctx, AV_LOG_ERROR,
252 "Error when parsing the expression '%s' for the component %d.\n",
253 lut->comp_expr_str[comp], comp);
254 return AVERROR(EINVAL);
257 /* compute the lut */
258 lut->var_values[VAR_MAXVAL] = max[comp];
259 lut->var_values[VAR_MINVAL] = min[comp];
261 for (val = 0; val < 256; val++) {
262 lut->var_values[VAR_VAL] = val;
263 lut->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
264 lut->var_values[VAR_NEGVAL] =
265 av_clip(min[comp] + max[comp] - lut->var_values[VAR_VAL],
266 min[comp], max[comp]);
268 res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
270 av_log(ctx, AV_LOG_ERROR,
271 "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
272 lut->comp_expr_str[comp], val, comp);
273 return AVERROR(EINVAL);
275 lut->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
276 av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
283 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
285 AVFilterContext *ctx = inlink->dst;
286 LutContext *lut = ctx->priv;
287 AVFilterLink *outlink = ctx->outputs[0];
289 uint8_t *inrow, *outrow, *inrow0, *outrow0;
292 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
295 return AVERROR(ENOMEM);
297 av_frame_copy_props(out, in);
301 inrow0 = in ->data[0];
302 outrow0 = out->data[0];
304 for (i = 0; i < in->height; i ++) {
307 for (j = 0; j < inlink->w; j++) {
308 for (k = 0; k < lut->step; k++)
309 outrow[k] = lut->lut[lut->rgba_map[k]][inrow[k]];
313 inrow0 += in ->linesize[0];
314 outrow0 += out->linesize[0];
318 for (plane = 0; plane < 4 && in->data[plane]; plane++) {
319 int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
320 int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
322 inrow = in ->data[plane];
323 outrow = out->data[plane];
325 for (i = 0; i < in->height >> vsub; i ++) {
326 for (j = 0; j < inlink->w>>hsub; j++)
327 outrow[j] = lut->lut[plane][inrow[j]];
328 inrow += in ->linesize[plane];
329 outrow += out->linesize[plane];
335 return ff_filter_frame(outlink, out);
338 static const AVFilterPad inputs[] = {
340 .type = AVMEDIA_TYPE_VIDEO,
341 .filter_frame = filter_frame,
342 .config_props = config_props,
346 static const AVFilterPad outputs[] = {
348 .type = AVMEDIA_TYPE_VIDEO, },
351 #define DEFINE_LUT_FILTER(name_, description_, init_, options) \
352 static const AVClass name_ ## _class = { \
353 .class_name = #name_, \
354 .item_name = av_default_item_name, \
356 .version = LIBAVUTIL_VERSION_INT, \
358 AVFilter avfilter_vf_##name_ = { \
360 .description = NULL_IF_CONFIG_SMALL(description_), \
361 .priv_size = sizeof(LutContext), \
362 .priv_class = &name_ ## _class, \
366 .query_formats = query_formats, \
369 .outputs = outputs, \
372 #if CONFIG_LUT_FILTER
373 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init, lut_options);
375 #if CONFIG_LUTYUV_FILTER
376 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
378 #if CONFIG_LUTRGB_FILTER
379 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
382 #if CONFIG_NEGATE_FILTER
384 static const AVOption negate_options[] = {
385 { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
389 static av_cold int negate_init(AVFilterContext *ctx)
391 LutContext *lut = ctx->priv;
394 av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
396 for (i = 0; i < 4; i++) {
397 lut->comp_expr_str[i] = av_strdup((i == 3 && lut->negate_alpha) ?
399 if (!lut->comp_expr_str[i]) {
401 return AVERROR(ENOMEM);
408 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);