]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_lut.c
vf_crop: use the name 's' for the pointer to the private context
[ffmpeg] / libavfilter / vf_lut.c
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
21 /**
22  * @file
23  * Compute a look-up table for binding the input value to the output
24  * value, and apply it to input video.
25  */
26
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",        ///< 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
48     "clipval",
49     NULL
50 };
51
52 enum var_name {
53     VAR_E,
54     VAR_PHI,
55     VAR_PI,
56     VAR_W,
57     VAR_H,
58     VAR_VAL,
59     VAR_MAXVAL,
60     VAR_MINVAL,
61     VAR_NEGVAL,
62     VAR_CLIPVAL,
63     VAR_VARS_NB
64 };
65
66 typedef struct {
67     const AVClass *class;
68     uint8_t lut[4][256];  ///< lookup table for each component
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 *lut = ctx->priv;
108
109     lut->var_values[VAR_PHI] = M_PHI;
110     lut->var_values[VAR_PI]  = M_PI;
111     lut->var_values[VAR_E ]  = M_E;
112
113     lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
114     lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
115
116     return 0;
117 }
118
119 static av_cold void uninit(AVFilterContext *ctx)
120 {
121     LutContext *lut = ctx->priv;
122     int i;
123
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]);
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
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 };
146
147 static int query_formats(AVFilterContext *ctx)
148 {
149     LutContext *lut = ctx->priv;
150
151     enum AVPixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
152                                  lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
153
154     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
155     return 0;
156 }
157
158 /**
159  * Clip value val in the minval - maxval range.
160  */
161 static double clip(void *opaque, double val)
162 {
163     LutContext *lut = opaque;
164     double minval = lut->var_values[VAR_MINVAL];
165     double maxval = lut->var_values[VAR_MAXVAL];
166
167     return av_clip(val, minval, maxval);
168 }
169
170 /**
171  * Compute gamma correction for value val, assuming the minval-maxval
172  * range, val is clipped to a value contained in the same interval.
173  */
174 static double compute_gammaval(void *opaque, double gamma)
175 {
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];
180
181     return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
182 }
183
184 static double (* const funcs1[])(void *, double) = {
185     clip,
186     compute_gammaval,
187     NULL
188 };
189
190 static const char * const funcs1_names[] = {
191     "clip",
192     "gammaval",
193     NULL
194 };
195
196 static int config_props(AVFilterLink *inlink)
197 {
198     AVFilterContext *ctx = inlink->dst;
199     LutContext *lut = ctx->priv;
200     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
201     int min[4], max[4];
202     int val, comp, ret;
203
204     lut->hsub = desc->log2_chroma_w;
205     lut->vsub = desc->log2_chroma_h;
206
207     lut->var_values[VAR_W] = inlink->w;
208     lut->var_values[VAR_H] = inlink->h;
209
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;
219         max[Y] = 235;
220         max[U] = max[V] = 240;
221         min[A] = 0; max[A] = 255;
222         break;
223     default:
224         min[0] = min[1] = min[2] = min[3] = 0;
225         max[0] = max[1] = max[2] = max[3] = 255;
226     }
227
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;
231
232     if (lut->is_rgb) {
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;
240         }
241         lut->step = av_get_bits_per_pixel(desc) >> 3;
242     }
243
244     for (comp = 0; comp < desc->nb_components; comp++) {
245         double res;
246
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);
250         if (ret < 0) {
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);
255         }
256
257         /* compute the lut */
258         lut->var_values[VAR_MAXVAL] = max[comp];
259         lut->var_values[VAR_MINVAL] = min[comp];
260
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]);
267
268             res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
269             if (isnan(res)) {
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);
274             }
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]);
277         }
278     }
279
280     return 0;
281 }
282
283 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
284 {
285     AVFilterContext *ctx = inlink->dst;
286     LutContext *lut = ctx->priv;
287     AVFilterLink *outlink = ctx->outputs[0];
288     AVFrame *out;
289     uint8_t *inrow, *outrow, *inrow0, *outrow0;
290     int i, j, k, plane;
291
292     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
293     if (!out) {
294         av_frame_free(&in);
295         return AVERROR(ENOMEM);
296     }
297     av_frame_copy_props(out, in);
298
299     if (lut->is_rgb) {
300         /* packed */
301         inrow0  = in ->data[0];
302         outrow0 = out->data[0];
303
304         for (i = 0; i < in->height; i ++) {
305             inrow  = inrow0;
306             outrow = outrow0;
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]];
310                 outrow += lut->step;
311                 inrow  += lut->step;
312             }
313             inrow0  += in ->linesize[0];
314             outrow0 += out->linesize[0];
315         }
316     } else {
317         /* planar */
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;
321
322             inrow  = in ->data[plane];
323             outrow = out->data[plane];
324
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];
330             }
331         }
332     }
333
334     av_frame_free(&in);
335     return ff_filter_frame(outlink, out);
336 }
337
338 static const AVFilterPad inputs[] = {
339     { .name            = "default",
340       .type            = AVMEDIA_TYPE_VIDEO,
341       .filter_frame    = filter_frame,
342       .config_props    = config_props,
343     },
344     { .name = NULL}
345 };
346 static const AVFilterPad outputs[] = {
347     { .name            = "default",
348       .type            = AVMEDIA_TYPE_VIDEO, },
349     { .name = NULL}
350 };
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,                             \
355         .option     = options,                                          \
356         .version    = LIBAVUTIL_VERSION_INT,                            \
357     };                                                                  \
358     AVFilter avfilter_vf_##name_ = {                                    \
359         .name          = #name_,                                        \
360         .description   = NULL_IF_CONFIG_SMALL(description_),            \
361         .priv_size     = sizeof(LutContext),                            \
362         .priv_class    = &name_ ## _class,                              \
363                                                                         \
364         .init          = init_,                                         \
365         .uninit        = uninit,                                        \
366         .query_formats = query_formats,                                 \
367                                                                         \
368         .inputs        = inputs,                                        \
369         .outputs       = outputs,                                       \
370     }
371
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);
374 #endif
375 #if CONFIG_LUTYUV_FILTER
376 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.",     init, lut_options);
377 #endif
378 #if CONFIG_LUTRGB_FILTER
379 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.",     init, lut_options);
380 #endif
381
382 #if CONFIG_NEGATE_FILTER
383
384 static const AVOption negate_options[] = {
385     { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
386     { NULL },
387 };
388
389 static av_cold int negate_init(AVFilterContext *ctx)
390 {
391     LutContext *lut = ctx->priv;
392     int i;
393
394     av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
395
396     for (i = 0; i < 4; i++) {
397         lut->comp_expr_str[i] = av_strdup((i == 3 && lut->negate_alpha) ?
398                                           "val" : "negval");
399         if (!lut->comp_expr_str[i]) {
400             uninit(ctx);
401             return AVERROR(ENOMEM);
402         }
403     }
404
405     return init(ctx);
406 }
407
408 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);
409
410 #endif