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"
35 static const char *const var_names[] = {
36 "w", ///< width of the input video
37 "h", ///< height of the input video
38 "val", ///< input value for the pixel
39 "maxval", ///< max value for the pixel
40 "minval", ///< min value for the pixel
41 "negval", ///< negated value
59 uint8_t lut[4][256]; ///< lookup table for each component
60 char *comp_expr_str[4];
63 double var_values[VAR_VARS_NB];
66 int negate_alpha; /* only used by negate */
77 #define OFFSET(x) offsetof(LutContext, x)
79 static const AVOption lut_options[] = {
80 {"c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
81 {"c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
82 {"c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
83 {"c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
84 {"y", "set Y expression", OFFSET(comp_expr_str[Y]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
85 {"u", "set U expression", OFFSET(comp_expr_str[U]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
86 {"v", "set V expression", OFFSET(comp_expr_str[V]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
87 {"r", "set R expression", OFFSET(comp_expr_str[R]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
88 {"g", "set G expression", OFFSET(comp_expr_str[G]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
89 {"b", "set B expression", OFFSET(comp_expr_str[B]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
90 {"a", "set A expression", OFFSET(comp_expr_str[A]), AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
94 static const AVClass lut_class = {
96 .item_name = av_default_item_name,
97 .option = lut_options,
98 .version = LIBAVUTIL_VERSION_INT,
99 .category = AV_CLASS_CATEGORY_FILTER,
102 static int init(AVFilterContext *ctx, const char *args, void *opaque)
104 LutContext *lut = ctx->priv;
107 lut->class = &lut_class;
108 av_opt_set_defaults(lut);
110 lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
111 lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
112 if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
118 static av_cold void uninit(AVFilterContext *ctx)
120 LutContext *lut = ctx->priv;
123 for (i = 0; i < 4; i++) {
124 av_expr_free(lut->comp_expr[i]);
125 lut->comp_expr[i] = NULL;
126 av_freep(&lut->comp_expr_str[i]);
130 #define YUV_FORMATS \
131 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, \
132 PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P, \
134 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P, \
137 #define RGB_FORMATS \
138 PIX_FMT_ARGB, PIX_FMT_RGBA, \
139 PIX_FMT_ABGR, PIX_FMT_BGRA, \
140 PIX_FMT_RGB24, PIX_FMT_BGR24
142 static const enum PixelFormat yuv_pix_fmts[] = { YUV_FORMATS, PIX_FMT_NONE };
143 static const enum PixelFormat rgb_pix_fmts[] = { RGB_FORMATS, PIX_FMT_NONE };
144 static const enum PixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, PIX_FMT_NONE };
146 static int query_formats(AVFilterContext *ctx)
148 LutContext *lut = ctx->priv;
150 const enum PixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
151 lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
153 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
158 * Clip value val in the minval - maxval range.
160 static double clip(void *opaque, double val)
162 LutContext *lut = opaque;
163 double minval = lut->var_values[VAR_MINVAL];
164 double maxval = lut->var_values[VAR_MAXVAL];
166 return av_clip(val, minval, maxval);
170 * Compute gamma correction for value val, assuming the minval-maxval
171 * range, val is clipped to a value contained in the same interval.
173 static double compute_gammaval(void *opaque, double gamma)
175 LutContext *lut = opaque;
176 double val = lut->var_values[VAR_CLIPVAL];
177 double minval = lut->var_values[VAR_MINVAL];
178 double maxval = lut->var_values[VAR_MAXVAL];
180 return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
183 static double (* const funcs1[])(void *, double) = {
185 (void *)compute_gammaval,
189 static const char * const funcs1_names[] = {
195 static int config_props(AVFilterLink *inlink)
197 AVFilterContext *ctx = inlink->dst;
198 LutContext *lut = ctx->priv;
199 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
200 int rgba_map[4]; /* component index -> RGBA color index map */
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 PIX_FMT_YUV410P:
212 case PIX_FMT_YUV411P:
213 case PIX_FMT_YUV420P:
214 case PIX_FMT_YUV422P:
215 case PIX_FMT_YUV440P:
216 case PIX_FMT_YUV444P:
217 case 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 PIX_FMT_ARGB: rgba_map[0] = A; rgba_map[1] = R; rgba_map[2] = G; rgba_map[3] = B; break;
235 case PIX_FMT_ABGR: rgba_map[0] = A; rgba_map[1] = B; rgba_map[2] = G; rgba_map[3] = R; break;
237 case PIX_FMT_RGB24: rgba_map[0] = R; rgba_map[1] = G; rgba_map[2] = B; rgba_map[3] = A; break;
239 case PIX_FMT_BGR24: rgba_map[0] = B; rgba_map[1] = G; rgba_map[2] = R; rgba_map[3] = A; break;
241 lut->step = av_get_bits_per_pixel(desc) >> 3;
244 for (comp = 0; comp < desc->nb_components; comp++) {
246 int color = lut->is_rgb ? rgba_map[comp] : comp;
248 /* create the parsed expression */
249 ret = av_expr_parse(&lut->comp_expr[color], lut->comp_expr_str[color],
250 var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
252 av_log(ctx, AV_LOG_ERROR,
253 "Error when parsing the expression '%s' for the component %d and color %d.\n",
254 lut->comp_expr_str[comp], comp, color);
255 return AVERROR(EINVAL);
258 /* compute the lut */
259 lut->var_values[VAR_MAXVAL] = max[color];
260 lut->var_values[VAR_MINVAL] = min[color];
262 for (val = 0; val < 256; val++) {
263 lut->var_values[VAR_VAL] = val;
264 lut->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
265 lut->var_values[VAR_NEGVAL] =
266 av_clip(min[color] + max[color] - lut->var_values[VAR_VAL],
267 min[color], max[color]);
269 res = av_expr_eval(lut->comp_expr[color], lut->var_values, lut);
271 av_log(ctx, AV_LOG_ERROR,
272 "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
273 lut->comp_expr_str[color], val, comp);
274 return AVERROR(EINVAL);
276 lut->lut[comp][val] = av_clip((int)res, min[color], max[color]);
277 av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
284 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
286 AVFilterContext *ctx = inlink->dst;
287 LutContext *lut = ctx->priv;
288 AVFilterLink *outlink = ctx->outputs[0];
289 AVFilterBufferRef *inpic = inlink ->cur_buf;
290 AVFilterBufferRef *outpic = outlink->out_buf;
291 uint8_t *inrow, *outrow, *inrow0, *outrow0;
296 inrow0 = inpic ->data[0] + y * inpic ->linesize[0];
297 outrow0 = outpic->data[0] + y * outpic->linesize[0];
299 for (i = 0; i < h; i ++) {
301 const uint8_t (*tab)[256] = (const uint8_t (*)[256])lut->lut;
304 for (j = 0; j < w; j++) {
305 outrow[0] = tab[0][inrow[0]];
307 outrow[1] = tab[1][inrow[1]];
309 outrow[2] = tab[2][inrow[2]];
311 outrow[3] = tab[3][inrow[3]];
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 const uint8_t *tab = lut->lut[plane];
332 int w = inlink->w>>hsub;
333 for (j = 0; j < w; j++)
334 outrow[j] = tab[inrow[j]];
335 inrow += inpic ->linesize[plane];
336 outrow += outpic->linesize[plane];
341 ff_draw_slice(outlink, y, h, slice_dir);
344 #define DEFINE_LUT_FILTER(name_, description_, init_) \
345 AVFilter avfilter_vf_##name_ = { \
347 .description = NULL_IF_CONFIG_SMALL(description_), \
348 .priv_size = sizeof(LutContext), \
352 .query_formats = query_formats, \
354 .inputs = (const AVFilterPad[]) {{ .name = "default", \
355 .type = AVMEDIA_TYPE_VIDEO, \
356 .draw_slice = draw_slice, \
357 .config_props = config_props, \
358 .min_perms = AV_PERM_READ, }, \
360 .outputs = (const AVFilterPad[]) {{ .name = "default", \
361 .type = AVMEDIA_TYPE_VIDEO, }, \
365 #if CONFIG_LUT_FILTER
366 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init);
368 #if CONFIG_LUTYUV_FILTER
369 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init);
371 #if CONFIG_LUTRGB_FILTER
372 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init);
375 #if CONFIG_NEGATE_FILTER
377 static int negate_init(AVFilterContext *ctx, const char *args, void *opaque)
379 LutContext *lut = ctx->priv;
383 sscanf(args, "%d", &lut->negate_alpha);
385 av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
387 snprintf(lut_params, sizeof(lut_params), "c0=negval:c1=negval:c2=negval:a=%s",
388 lut->negate_alpha ? "negval" : "val");
390 return init(ctx, lut_params, opaque);
393 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init);