2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
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/eval.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
33 static const char *var_names[] = {
37 "w", ///< width of the input video
38 "h", ///< height of the input video
39 "val", ///< input value for the pixel
40 "maxval", ///< max value for the pixel
41 "minval", ///< min value for the pixel
42 "negval", ///< negated value
63 uint8_t lut[4][256]; ///< lookup table for each component
64 char *comp_expr_str[4];
67 double var_values[VAR_VARS_NB];
71 int negate_alpha; /* only used by negate */
82 #define OFFSET(x) offsetof(LutContext, x)
84 static const AVOption lut_options[] = {
85 {"c0", "set component #0 expression", OFFSET(comp_expr_str[0]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
86 {"c1", "set component #1 expression", OFFSET(comp_expr_str[1]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
87 {"c2", "set component #2 expression", OFFSET(comp_expr_str[2]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
88 {"c3", "set component #3 expression", OFFSET(comp_expr_str[3]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
89 {"y", "set Y expression", OFFSET(comp_expr_str[Y]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
90 {"u", "set U expression", OFFSET(comp_expr_str[U]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
91 {"v", "set V expression", OFFSET(comp_expr_str[V]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
92 {"r", "set R expression", OFFSET(comp_expr_str[R]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
93 {"g", "set G expression", OFFSET(comp_expr_str[G]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
94 {"b", "set B expression", OFFSET(comp_expr_str[B]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
95 {"a", "set A expression", OFFSET(comp_expr_str[A]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
99 static const char *lut_get_name(void *ctx)
104 static const AVClass lut_class = {
110 static int init(AVFilterContext *ctx, const char *args, void *opaque)
112 LutContext *lut = ctx->priv;
115 lut->class = &lut_class;
116 av_opt_set_defaults(lut);
118 lut->var_values[VAR_PHI] = M_PHI;
119 lut->var_values[VAR_PI] = M_PI;
120 lut->var_values[VAR_E ] = M_E;
122 lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
123 lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
124 if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
130 static av_cold void uninit(AVFilterContext *ctx)
132 LutContext *lut = ctx->priv;
135 for (i = 0; i < 4; i++) {
136 av_expr_free(lut->comp_expr[i]);
137 lut->comp_expr[i] = NULL;
138 av_freep(&lut->comp_expr_str[i]);
142 #define YUV_FORMATS \
143 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, \
144 PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P, \
146 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P, \
149 #define RGB_FORMATS \
150 PIX_FMT_ARGB, PIX_FMT_RGBA, \
151 PIX_FMT_ABGR, PIX_FMT_BGRA, \
152 PIX_FMT_RGB24, PIX_FMT_BGR24
154 static enum PixelFormat yuv_pix_fmts[] = { YUV_FORMATS, PIX_FMT_NONE };
155 static enum PixelFormat rgb_pix_fmts[] = { RGB_FORMATS, PIX_FMT_NONE };
156 static enum PixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, PIX_FMT_NONE };
158 static int query_formats(AVFilterContext *ctx)
160 LutContext *lut = ctx->priv;
162 enum PixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
163 lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
165 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
170 * Clip value val in the minval - maxval range.
172 static double clip(void *opaque, double val)
174 LutContext *lut = opaque;
175 double minval = lut->var_values[VAR_MINVAL];
176 double maxval = lut->var_values[VAR_MAXVAL];
178 return av_clip(val, minval, maxval);
182 * Compute gamma correction for value val, assuming the minval-maxval
183 * range, val is clipped to a value contained in the same interval.
185 static double compute_gammaval(void *opaque, double gamma)
187 LutContext *lut = opaque;
188 double val = lut->var_values[VAR_CLIPVAL];
189 double minval = lut->var_values[VAR_MINVAL];
190 double maxval = lut->var_values[VAR_MAXVAL];
192 return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
195 static double (* const funcs1[])(void *, double) = {
197 (void *)compute_gammaval,
201 static const char * const funcs1_names[] = {
207 static int config_props(AVFilterLink *inlink)
209 AVFilterContext *ctx = inlink->dst;
210 LutContext *lut = ctx->priv;
211 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
215 lut->hsub = desc->log2_chroma_w;
216 lut->vsub = desc->log2_chroma_h;
218 lut->var_values[VAR_W] = inlink->w;
219 lut->var_values[VAR_H] = inlink->h;
221 switch (inlink->format) {
222 case PIX_FMT_YUV410P:
223 case PIX_FMT_YUV411P:
224 case PIX_FMT_YUV420P:
225 case PIX_FMT_YUV422P:
226 case PIX_FMT_YUV440P:
227 case PIX_FMT_YUV444P:
228 case PIX_FMT_YUVA420P:
229 min[Y] = min[U] = min[V] = 16;
231 max[U] = max[V] = 240;
232 min[A] = 0; max[A] = 255;
235 min[0] = min[1] = min[2] = min[3] = 0;
236 max[0] = max[1] = max[2] = max[3] = 255;
239 lut->is_yuv = lut->is_rgb = 0;
240 if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
241 else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
244 switch (inlink->format) {
245 case PIX_FMT_ARGB: lut->rgba_map[A] = 0; lut->rgba_map[R] = 1; lut->rgba_map[G] = 2; lut->rgba_map[B] = 3; break;
246 case PIX_FMT_ABGR: lut->rgba_map[A] = 0; lut->rgba_map[B] = 1; lut->rgba_map[G] = 2; lut->rgba_map[R] = 3; break;
248 case PIX_FMT_RGB24: lut->rgba_map[R] = 0; lut->rgba_map[G] = 1; lut->rgba_map[B] = 2; lut->rgba_map[A] = 3; break;
250 case PIX_FMT_BGR24: lut->rgba_map[B] = 0; lut->rgba_map[G] = 1; lut->rgba_map[R] = 2; lut->rgba_map[A] = 3; break;
252 lut->step = av_get_bits_per_pixel(desc) >> 3;
255 for (comp = 0; comp < desc->nb_components; comp++) {
258 /* create the parsed expression */
259 ret = av_expr_parse(&lut->comp_expr[comp], lut->comp_expr_str[comp],
260 var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
262 av_log(ctx, AV_LOG_ERROR,
263 "Error when parsing the expression '%s' for the component %d.\n",
264 lut->comp_expr_str[comp], comp);
265 return AVERROR(EINVAL);
268 /* compute the lut */
269 lut->var_values[VAR_MAXVAL] = max[comp];
270 lut->var_values[VAR_MINVAL] = min[comp];
272 for (val = 0; val < 256; val++) {
273 lut->var_values[VAR_VAL] = val;
274 lut->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
275 lut->var_values[VAR_NEGVAL] =
276 av_clip(min[comp] + max[comp] - lut->var_values[VAR_VAL],
277 min[comp], max[comp]);
279 res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
281 av_log(ctx, AV_LOG_ERROR,
282 "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
283 lut->comp_expr_str[comp], val, comp);
284 return AVERROR(EINVAL);
286 lut->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
287 av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
294 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
296 AVFilterContext *ctx = inlink->dst;
297 LutContext *lut = ctx->priv;
298 AVFilterLink *outlink = ctx->outputs[0];
299 AVFilterBufferRef *inpic = inlink ->cur_buf;
300 AVFilterBufferRef *outpic = outlink->out_buf;
301 uint8_t *inrow, *outrow, *inrow0, *outrow0;
306 inrow0 = inpic ->data[0] + y * inpic ->linesize[0];
307 outrow0 = outpic->data[0] + y * outpic->linesize[0];
309 for (i = 0; i < h; i ++) {
312 for (j = 0; j < inlink->w; j++) {
313 for (k = 0; k < lut->step; k++)
314 outrow[k] = lut->lut[lut->rgba_map[k]][inrow[k]];
318 inrow0 += inpic ->linesize[0];
319 outrow0 += outpic->linesize[0];
323 for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
324 int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
325 int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
327 inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane];
328 outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
330 for (i = 0; i < h>>vsub; i ++) {
331 for (j = 0; j < inlink->w>>hsub; j++)
332 outrow[j] = lut->lut[plane][inrow[j]];
333 inrow += inpic ->linesize[plane];
334 outrow += outpic->linesize[plane];
339 avfilter_draw_slice(outlink, y, h, slice_dir);
342 #define DEFINE_LUT_FILTER(name_, description_, init_) \
343 AVFilter avfilter_vf_##name_ = { \
345 .description = NULL_IF_CONFIG_SMALL(description_), \
346 .priv_size = sizeof(LutContext), \
350 .query_formats = query_formats, \
352 .inputs = (AVFilterPad[]) {{ .name = "default", \
353 .type = AVMEDIA_TYPE_VIDEO, \
354 .draw_slice = draw_slice, \
355 .config_props = config_props, \
356 .min_perms = AV_PERM_READ, }, \
358 .outputs = (AVFilterPad[]) {{ .name = "default", \
359 .type = AVMEDIA_TYPE_VIDEO, }, \
363 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init);
364 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init);
365 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init);
367 #if CONFIG_NEGATE_FILTER
369 static int negate_init(AVFilterContext *ctx, const char *args, void *opaque)
371 LutContext *lut = ctx->priv;
372 char lut_params[1024];
375 sscanf(args, "%d", &lut->negate_alpha);
377 av_log(ctx, AV_LOG_INFO, "negate_alpha:%d\n", lut->negate_alpha);
379 snprintf(lut_params, sizeof(lut_params), "c0=negval:c1=negval:c2=negval:a=%s",
380 lut->negate_alpha ? "negval" : "val");
382 return init(ctx, lut_params, opaque);
385 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init);