h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_lut.c
CommitLineData
8fe0c527
SS
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
093804a9 27#include "libavutil/attributes.h"
1d9c2dc8 28#include "libavutil/common.h"
8fe0c527
SS
29#include "libavutil/eval.h"
30#include "libavutil/mathematics.h"
31#include "libavutil/opt.h"
32#include "libavutil/pixdesc.h"
33#include "avfilter.h"
b74a1da4 34#include "formats.h"
8fe0c527 35#include "internal.h"
803391f7 36#include "video.h"
8fe0c527 37
b0f29db5 38static const char *const var_names[] = {
8fe0c527
SS
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
52enum 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
58400ac1 66typedef struct LutContext {
8fe0c527
SS
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;
171868e2 76 int negate_alpha; /* only used by negate */
8fe0c527
SS
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)
20b46f8f 88#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
8fe0c527
SS
89
90static const AVOption lut_options[] = {
20b46f8f
AK
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 },
8fe0c527
SS
103};
104
093804a9 105static av_cold int init(AVFilterContext *ctx)
8fe0c527 106{
8d2565c2 107 LutContext *s = ctx->priv;
8fe0c527 108
8d2565c2
AK
109 s->var_values[VAR_PHI] = M_PHI;
110 s->var_values[VAR_PI] = M_PI;
111 s->var_values[VAR_E ] = M_E;
8fe0c527 112
8d2565c2
AK
113 s->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
114 s->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
8fe0c527
SS
115
116 return 0;
117}
118
119static av_cold void uninit(AVFilterContext *ctx)
120{
8d2565c2 121 LutContext *s = ctx->priv;
8fe0c527
SS
122 int i;
123
124 for (i = 0; i < 4; i++) {
8d2565c2
AK
125 av_expr_free(s->comp_expr[i]);
126 s->comp_expr[i] = NULL;
127 av_freep(&s->comp_expr_str[i]);
8fe0c527
SS
128 }
129}
130
131#define YUV_FORMATS \
716d413c
AK
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
8fe0c527
SS
137
138#define RGB_FORMATS \
716d413c
AK
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
8fe0c527 142
faa8245b
DEP
143static const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
144static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
145static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
8fe0c527
SS
146
147static int query_formats(AVFilterContext *ctx)
148{
8d2565c2 149 LutContext *s = ctx->priv;
8fe0c527 150
faa8245b
DEP
151 const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
152 s->is_yuv ? yuv_pix_fmts :
153 all_pix_fmts;
8fe0c527 154
b74a1da4 155 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
8fe0c527
SS
156 return 0;
157}
158
159/**
160 * Clip value val in the minval - maxval range.
161 */
162static double clip(void *opaque, double val)
163{
8d2565c2
AK
164 LutContext *s = opaque;
165 double minval = s->var_values[VAR_MINVAL];
166 double maxval = s->var_values[VAR_MAXVAL];
8fe0c527
SS
167
168 return av_clip(val, minval, maxval);
169}
170
171/**
172 * Compute gamma correction for value val, assuming the minval-maxval
173 * range, val is clipped to a value contained in the same interval.
174 */
175static double compute_gammaval(void *opaque, double gamma)
176{
8d2565c2
AK
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];
8fe0c527
SS
181
182 return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
183}
184
185static double (* const funcs1[])(void *, double) = {
186 clip,
187 compute_gammaval,
188 NULL
189};
190
191static const char * const funcs1_names[] = {
192 "clip",
193 "gammaval",
194 NULL
195};
196
197static int config_props(AVFilterLink *inlink)
198{
199 AVFilterContext *ctx = inlink->dst;
8d2565c2 200 LutContext *s = ctx->priv;
59ee9f78 201 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
8fe0c527
SS
202 int min[4], max[4];
203 int val, comp, ret;
204
8d2565c2
AK
205 s->hsub = desc->log2_chroma_w;
206 s->vsub = desc->log2_chroma_h;
8fe0c527 207
8d2565c2
AK
208 s->var_values[VAR_W] = inlink->w;
209 s->var_values[VAR_H] = inlink->h;
8fe0c527
SS
210
211 switch (inlink->format) {
716d413c
AK
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:
8fe0c527
SS
219 min[Y] = min[U] = min[V] = 16;
220 max[Y] = 235;
221 max[U] = max[V] = 240;
222 min[A] = 0; max[A] = 255;
223 break;
224 default:
225 min[0] = min[1] = min[2] = min[3] = 0;
226 max[0] = max[1] = max[2] = max[3] = 255;
227 }
228
8d2565c2
AK
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;
8fe0c527 232
8d2565c2 233 if (s->is_rgb) {
8fe0c527 234 switch (inlink->format) {
8d2565c2
AK
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;
716d413c 237 case AV_PIX_FMT_RGBA:
8d2565c2 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;
716d413c 239 case AV_PIX_FMT_BGRA:
8d2565c2 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;
8fe0c527 241 }
8d2565c2 242 s->step = av_get_bits_per_pixel(desc) >> 3;
8fe0c527
SS
243 }
244
245 for (comp = 0; comp < desc->nb_components; comp++) {
246 double res;
247
248 /* create the parsed expression */
b06848f4
AK
249 av_expr_free(s->comp_expr[comp]);
250 s->comp_expr[comp] = NULL;
8d2565c2 251 ret = av_expr_parse(&s->comp_expr[comp], s->comp_expr_str[comp],
8fe0c527
SS
252 var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
253 if (ret < 0) {
254 av_log(ctx, AV_LOG_ERROR,
255 "Error when parsing the expression '%s' for the component %d.\n",
8d2565c2 256 s->comp_expr_str[comp], comp);
8fe0c527
SS
257 return AVERROR(EINVAL);
258 }
259
8d2565c2
AK
260 /* compute the s */
261 s->var_values[VAR_MAXVAL] = max[comp];
262 s->var_values[VAR_MINVAL] = min[comp];
8fe0c527
SS
263
264 for (val = 0; val < 256; val++) {
8d2565c2
AK
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],
8fe0c527
SS
269 min[comp], max[comp]);
270
8d2565c2 271 res = av_expr_eval(s->comp_expr[comp], s->var_values, s);
8fe0c527
SS
272 if (isnan(res)) {
273 av_log(ctx, AV_LOG_ERROR,
274 "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
8d2565c2 275 s->comp_expr_str[comp], val, comp);
8fe0c527
SS
276 return AVERROR(EINVAL);
277 }
8d2565c2
AK
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]);
8fe0c527
SS
280 }
281 }
282
283 return 0;
284}
285
7e350379 286static int filter_frame(AVFilterLink *inlink, AVFrame *in)
8fe0c527
SS
287{
288 AVFilterContext *ctx = inlink->dst;
8d2565c2 289 LutContext *s = ctx->priv;
8fe0c527 290 AVFilterLink *outlink = ctx->outputs[0];
7e350379 291 AVFrame *out;
8fe0c527
SS
292 uint8_t *inrow, *outrow, *inrow0, *outrow0;
293 int i, j, k, plane;
294
7e350379 295 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 296 if (!out) {
7e350379 297 av_frame_free(&in);
565e4993
AK
298 return AVERROR(ENOMEM);
299 }
7e350379 300 av_frame_copy_props(out, in);
565e4993 301
8d2565c2 302 if (s->is_rgb) {
8fe0c527 303 /* packed */
565e4993
AK
304 inrow0 = in ->data[0];
305 outrow0 = out->data[0];
8fe0c527 306
7e350379 307 for (i = 0; i < in->height; i ++) {
8fe0c527
SS
308 inrow = inrow0;
309 outrow = outrow0;
310 for (j = 0; j < inlink->w; j++) {
8d2565c2
AK
311 for (k = 0; k < s->step; k++)
312 outrow[k] = s->lut[s->rgba_map[k]][inrow[k]];
313 outrow += s->step;
314 inrow += s->step;
8fe0c527 315 }
565e4993
AK
316 inrow0 += in ->linesize[0];
317 outrow0 += out->linesize[0];
8fe0c527
SS
318 }
319 } else {
320 /* planar */
565e4993 321 for (plane = 0; plane < 4 && in->data[plane]; plane++) {
8d2565c2
AK
322 int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
323 int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
8fe0c527 324
565e4993
AK
325 inrow = in ->data[plane];
326 outrow = out->data[plane];
8fe0c527 327
7e350379 328 for (i = 0; i < in->height >> vsub; i ++) {
8fe0c527 329 for (j = 0; j < inlink->w>>hsub; j++)
8d2565c2 330 outrow[j] = s->lut[plane][inrow[j]];
565e4993
AK
331 inrow += in ->linesize[plane];
332 outrow += out->linesize[plane];
8fe0c527
SS
333 }
334 }
335 }
336
7e350379 337 av_frame_free(&in);
565e4993 338 return ff_filter_frame(outlink, out);
8fe0c527
SS
339}
340
3db40703
RB
341static const AVFilterPad inputs[] = {
342 { .name = "default",
343 .type = AVMEDIA_TYPE_VIDEO,
565e4993 344 .filter_frame = filter_frame,
3db40703 345 .config_props = config_props,
7e350379 346 },
3db40703
RB
347 { .name = NULL}
348};
349static const AVFilterPad outputs[] = {
350 { .name = "default",
351 .type = AVMEDIA_TYPE_VIDEO, },
352 { .name = NULL}
353};
20b46f8f
AK
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, \
358 .option = options, \
359 .version = LIBAVUTIL_VERSION_INT, \
360 }; \
cd43ca04 361 AVFilter ff_vf_##name_ = { \
8fe0c527
SS
362 .name = #name_, \
363 .description = NULL_IF_CONFIG_SMALL(description_), \
364 .priv_size = sizeof(LutContext), \
20b46f8f 365 .priv_class = &name_ ## _class, \
8fe0c527
SS
366 \
367 .init = init_, \
368 .uninit = uninit, \
369 .query_formats = query_formats, \
370 \
3db40703
RB
371 .inputs = inputs, \
372 .outputs = outputs, \
8fe0c527
SS
373 }
374
375#if CONFIG_LUT_FILTER
20b46f8f 376DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init, lut_options);
8fe0c527
SS
377#endif
378#if CONFIG_LUTYUV_FILTER
20b46f8f 379DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init, lut_options);
8fe0c527
SS
380#endif
381#if CONFIG_LUTRGB_FILTER
20b46f8f 382DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init, lut_options);
8fe0c527 383#endif
171868e2
SS
384
385#if CONFIG_NEGATE_FILTER
386
20b46f8f
AK
387static const AVOption negate_options[] = {
388 { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
389 { NULL },
390};
391
093804a9 392static av_cold int negate_init(AVFilterContext *ctx)
171868e2 393{
8d2565c2 394 LutContext *s = ctx->priv;
20b46f8f 395 int i;
171868e2 396
8d2565c2 397 av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
171868e2 398
20b46f8f 399 for (i = 0; i < 4; i++) {
8d2565c2 400 s->comp_expr_str[i] = av_strdup((i == 3 && s->negate_alpha) ?
20b46f8f 401 "val" : "negval");
8d2565c2 402 if (!s->comp_expr_str[i]) {
20b46f8f
AK
403 uninit(ctx);
404 return AVERROR(ENOMEM);
405 }
406 }
171868e2 407
c43a7eca 408 return init(ctx);
171868e2
SS
409}
410
20b46f8f 411DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init, negate_options);
171868e2
SS
412
413#endif