1/*
2 * Copyright (c) 2011 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
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/opt.h"
31#include "libavutil/pixdesc.h"
32#include "avfilter.h"
33#include "drawutils.h"
34#include "formats.h"
35#include "internal.h"
36#include "video.h"
37
38static const char *const var_names[] = {
39    "w",        ///< width of the input video
40    "h",        ///< height of the input video
41    "val",      ///< input value for the pixel
42    "maxval",   ///< max value for the pixel
43    "minval",   ///< min value for the pixel
44    "negval",   ///< negated value
45    "clipval",
46    NULL
47};
48
49enum var_name {
50    VAR_W,
51    VAR_H,
52    VAR_VAL,
53    VAR_MAXVAL,
54    VAR_MINVAL,
55    VAR_NEGVAL,
56    VAR_CLIPVAL,
57    VAR_VARS_NB
58};
59
60typedef struct LutContext {
61    const AVClass *class;
62    uint8_t lut[4][256];  ///< lookup table for each component
63    char   *comp_expr_str[4];
64    AVExpr *comp_expr[4];
65    int hsub, vsub;
66    double var_values[VAR_VARS_NB];
67    int is_rgb, is_yuv;
68    int step;
69    int negate_alpha; /* only used by negate */
70} LutContext;
71
72#define Y 0
73#define U 1
74#define V 2
75#define R 0
76#define G 1
77#define B 2
78#define A 3
79
80#define OFFSET(x) offsetof(LutContext, x)
81#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82
83static const AVOption options[] = {
84    { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
85    { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
86    { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
87    { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
88    { "y",  "set Y expression",            OFFSET(comp_expr_str[Y]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
89    { "u",  "set U expression",            OFFSET(comp_expr_str[U]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
90    { "v",  "set V expression",            OFFSET(comp_expr_str[V]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
91    { "r",  "set R expression",            OFFSET(comp_expr_str[R]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
92    { "g",  "set G expression",            OFFSET(comp_expr_str[G]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
93    { "b",  "set B expression",            OFFSET(comp_expr_str[B]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
94    { "a",  "set A expression",            OFFSET(comp_expr_str[A]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
95    { NULL }
96};
97
98static av_cold void uninit(AVFilterContext *ctx)
99{
100    LutContext *s = ctx->priv;
101    int i;
102
103    for (i = 0; i < 4; i++) {
104        av_expr_free(s->comp_expr[i]);
105        s->comp_expr[i] = NULL;
106        av_freep(&s->comp_expr_str[i]);
107    }
108}
109
110#define YUV_FORMATS                                         \
111    AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,    \
112    AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,    \
113    AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,   \
114    AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,   \
115    AV_PIX_FMT_YUVJ440P
116
117#define RGB_FORMATS                             \
118    AV_PIX_FMT_ARGB,         AV_PIX_FMT_RGBA,         \
119    AV_PIX_FMT_ABGR,         AV_PIX_FMT_BGRA,         \
120    AV_PIX_FMT_RGB24,        AV_PIX_FMT_BGR24
121
122static const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
123static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
124static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
125
126static int query_formats(AVFilterContext *ctx)
127{
128    LutContext *s = ctx->priv;
129
130    const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
131                                                     s->is_yuv ? yuv_pix_fmts :
132                                                                 all_pix_fmts;
133
134    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
135    return 0;
136}
137
138/**
139 * Clip value val in the minval - maxval range.
140 */
141static double clip(void *opaque, double val)
142{
143    LutContext *s = opaque;
144    double minval = s->var_values[VAR_MINVAL];
145    double maxval = s->var_values[VAR_MAXVAL];
146
147    return av_clip(val, minval, maxval);
148}
149
150/**
151 * Compute gamma correction for value val, assuming the minval-maxval
152 * range, val is clipped to a value contained in the same interval.
153 */
154static double compute_gammaval(void *opaque, double gamma)
155{
156    LutContext *s = opaque;
157    double val    = s->var_values[VAR_CLIPVAL];
158    double minval = s->var_values[VAR_MINVAL];
159    double maxval = s->var_values[VAR_MAXVAL];
160
161    return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
162}
163
164static double (* const funcs1[])(void *, double) = {
165    (void *)clip,
166    (void *)compute_gammaval,
167    NULL
168};
169
170static const char * const funcs1_names[] = {
171    "clip",
172    "gammaval",
173    NULL
174};
175
176static int config_props(AVFilterLink *inlink)
177{
178    AVFilterContext *ctx = inlink->dst;
179    LutContext *s = ctx->priv;
180    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
181    uint8_t rgba_map[4]; /* component index -> RGBA color index map */
182    int min[4], max[4];
183    int val, color, ret;
184
185    s->hsub = desc->log2_chroma_w;
186    s->vsub = desc->log2_chroma_h;
187
188    s->var_values[VAR_W] = inlink->w;
189    s->var_values[VAR_H] = inlink->h;
190
191    switch (inlink->format) {
192    case AV_PIX_FMT_YUV410P:
193    case AV_PIX_FMT_YUV411P:
194    case AV_PIX_FMT_YUV420P:
195    case AV_PIX_FMT_YUV422P:
196    case AV_PIX_FMT_YUV440P:
197    case AV_PIX_FMT_YUV444P:
198    case AV_PIX_FMT_YUVA420P:
199    case AV_PIX_FMT_YUVA422P:
200    case AV_PIX_FMT_YUVA444P:
201        min[Y] = min[U] = min[V] = 16;
202        max[Y] = 235;
203        max[U] = max[V] = 240;
204        min[A] = 0; max[A] = 255;
205        break;
206    default:
207        min[0] = min[1] = min[2] = min[3] = 0;
208        max[0] = max[1] = max[2] = max[3] = 255;
209    }
210
211    s->is_yuv = s->is_rgb = 0;
212    if      (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) s->is_yuv = 1;
213    else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) s->is_rgb = 1;
214
215    if (s->is_rgb) {
216        ff_fill_rgba_map(rgba_map, inlink->format);
217        s->step = av_get_bits_per_pixel(desc) >> 3;
218    }
219
220    for (color = 0; color < desc->nb_components; color++) {
221        double res;
222        int comp = s->is_rgb ? rgba_map[color] : color;
223
224        /* create the parsed expression */
225        av_expr_free(s->comp_expr[color]);
226        s->comp_expr[color] = NULL;
227        ret = av_expr_parse(&s->comp_expr[color], s->comp_expr_str[color],
228                            var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
229        if (ret < 0) {
230            av_log(ctx, AV_LOG_ERROR,
231                   "Error when parsing the expression '%s' for the component %d and color %d.\n",
232                   s->comp_expr_str[comp], comp, color);
233            return AVERROR(EINVAL);
234        }
235
236        /* compute the lut */
237        s->var_values[VAR_MAXVAL] = max[color];
238        s->var_values[VAR_MINVAL] = min[color];
239
240        for (val = 0; val < 256; val++) {
241            s->var_values[VAR_VAL] = val;
242            s->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
243            s->var_values[VAR_NEGVAL] =
244                av_clip(min[color] + max[color] - s->var_values[VAR_VAL],
245                        min[color], max[color]);
246
247            res = av_expr_eval(s->comp_expr[color], s->var_values, s);
248            if (isnan(res)) {
249                av_log(ctx, AV_LOG_ERROR,
250                       "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
251                       s->comp_expr_str[color], val, comp);
252                return AVERROR(EINVAL);
253            }
254            s->lut[comp][val] = av_clip((int)res, min[color], max[color]);
255            av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
256        }
257    }
258
259    return 0;
260}
261
262static int filter_frame(AVFilterLink *inlink, AVFrame *in)
263{
264    AVFilterContext *ctx = inlink->dst;
265    LutContext *s = ctx->priv;
266    AVFilterLink *outlink = ctx->outputs[0];
267    AVFrame *out;
268    uint8_t *inrow, *outrow, *inrow0, *outrow0;
269    int i, j, plane, direct = 0;
270
271    if (av_frame_is_writable(in)) {
272        direct = 1;
273        out = in;
274    } else {
275        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
276        if (!out) {
277            av_frame_free(&in);
278            return AVERROR(ENOMEM);
279        }
280        av_frame_copy_props(out, in);
281    }
282
283    if (s->is_rgb) {
284        /* packed */
285        inrow0  = in ->data[0];
286        outrow0 = out->data[0];
287
288        for (i = 0; i < in->height; i ++) {
289            int w = inlink->w;
290            const uint8_t (*tab)[256] = (const uint8_t (*)[256])s->lut;
291            inrow  = inrow0;
292            outrow = outrow0;
293            for (j = 0; j < w; j++) {
294                switch (s->step) {
295                case 4:  outrow[3] = tab[3][inrow[3]]; // Fall-through
296                case 3:  outrow[2] = tab[2][inrow[2]]; // Fall-through
297                case 2:  outrow[1] = tab[1][inrow[1]]; // Fall-through
298                default: outrow[0] = tab[0][inrow[0]];
299                }
300                outrow += s->step;
301                inrow  += s->step;
302            }
303            inrow0  += in ->linesize[0];
304            outrow0 += out->linesize[0];
305        }
306    } else {
307        /* planar */
308        for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
309            int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
310            int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
311            int h = FF_CEIL_RSHIFT(inlink->h, vsub);
312            int w = FF_CEIL_RSHIFT(inlink->w, hsub);
313
314            inrow  = in ->data[plane];
315            outrow = out->data[plane];
316
317            for (i = 0; i < h; i++) {
318                const uint8_t *tab = s->lut[plane];
319                for (j = 0; j < w; j++)
320                    outrow[j] = tab[inrow[j]];
321                inrow  += in ->linesize[plane];
322                outrow += out->linesize[plane];
323            }
324        }
325    }
326
327    if (!direct)
328        av_frame_free(&in);
329
330    return ff_filter_frame(outlink, out);
331}
332
333static const AVFilterPad inputs[] = {
334    { .name         = "default",
335      .type         = AVMEDIA_TYPE_VIDEO,
336      .filter_frame = filter_frame,
337      .config_props = config_props,
338    },
339    { NULL }
340};
341static const AVFilterPad outputs[] = {
342    { .name = "default",
343      .type = AVMEDIA_TYPE_VIDEO,
344    },
345    { NULL }
346};
347
348#define DEFINE_LUT_FILTER(name_, description_)                          \
349    AVFilter ff_vf_##name_ = {                                          \
350        .name          = #name_,                                        \
351        .description   = NULL_IF_CONFIG_SMALL(description_),            \
352        .priv_size     = sizeof(LutContext),                            \
353        .priv_class    = &name_ ## _class,                              \
354        .init          = name_##_init,                                  \
355        .uninit        = uninit,                                        \
356        .query_formats = query_formats,                                 \
357        .inputs        = inputs,                                        \
358        .outputs       = outputs,                                       \
359        .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,        \
360    }
361
362#if CONFIG_LUT_FILTER
363
364#define lut_options options
365AVFILTER_DEFINE_CLASS(lut);
366
367static int lut_init(AVFilterContext *ctx)
368{
369    return 0;
370}
371
372DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.");
373#endif
374
375#if CONFIG_LUTYUV_FILTER
376
377#define lutyuv_options options
378AVFILTER_DEFINE_CLASS(lutyuv);
379
380static av_cold int lutyuv_init(AVFilterContext *ctx)
381{
382    LutContext *s = ctx->priv;
383
384    s->is_yuv = 1;
385
386    return 0;
387}
388
389DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.");
390#endif
391
392#if CONFIG_LUTRGB_FILTER
393
394#define lutrgb_options options
395AVFILTER_DEFINE_CLASS(lutrgb);
396
397static av_cold int lutrgb_init(AVFilterContext *ctx)
398{
399    LutContext *s = ctx->priv;
400
401    s->is_rgb = 1;
402
403    return 0;
404}
405
406DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.");
407#endif
408
409#if CONFIG_NEGATE_FILTER
410
411static const AVOption negate_options[] = {
412    { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
413    { NULL }
414};
415
416AVFILTER_DEFINE_CLASS(negate);
417
418static av_cold int negate_init(AVFilterContext *ctx)
419{
420    LutContext *s = ctx->priv;
421    int i;
422
423    av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
424
425    for (i = 0; i < 4; i++) {
426        s->comp_expr_str[i] = av_strdup((i == 3 && !s->negate_alpha) ?
427                                          "val" : "negval");
428        if (!s->comp_expr_str[i]) {
429            uninit(ctx);
430            return AVERROR(ENOMEM);
431        }
432    }
433
434    return 0;
435}
436
437DEFINE_LUT_FILTER(negate, "Negate input video.");
438
439#endif
440