libavfilter/vf_lut.c
dd2793c8
 /*
  * Copyright (c) 2011 Stefano Sabatini
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * Compute a look-up table for binding the input value to the output
  * value, and apply it to input video.
  */
 
093804a9
 #include "libavutil/attributes.h"
8f4cfda9
 #include "libavutil/bswap.h"
1d9c2dc8
 #include "libavutil/common.h"
dd2793c8
 #include "libavutil/eval.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
ba1cbf40
 #include "drawutils.h"
b74a1da4
 #include "formats.h"
7464a53a
 #include "internal.h"
803391f7
 #include "video.h"
dd2793c8
 
b0f29db5
 static const char *const var_names[] = {
dd2793c8
     "w",        ///< width of the input video
     "h",        ///< height of the input video
     "val",      ///< input value for the pixel
     "maxval",   ///< max value for the pixel
     "minval",   ///< min value for the pixel
     "negval",   ///< negated value
     "clipval",
     NULL
 };
 
 enum var_name {
     VAR_W,
     VAR_H,
     VAR_VAL,
     VAR_MAXVAL,
     VAR_MINVAL,
     VAR_NEGVAL,
     VAR_CLIPVAL,
     VAR_VARS_NB
 };
 
58400ac1
 typedef struct LutContext {
dd2793c8
     const AVClass *class;
b74ebd09
     uint16_t lut[4][256 * 256];  ///< lookup table for each component
dd2793c8
     char   *comp_expr_str[4];
     AVExpr *comp_expr[4];
     int hsub, vsub;
     double var_values[VAR_VARS_NB];
     int is_rgb, is_yuv;
0edfd8e6
     int is_planar;
b74ebd09
     int is_16bit;
dd2793c8
     int step;
15f03725
     int negate_alpha; /* only used by negate */
dd2793c8
 } LutContext;
 
 #define Y 0
 #define U 1
 #define V 2
 #define R 0
 #define G 1
 #define B 2
 #define A 3
 
 #define OFFSET(x) offsetof(LutContext, x)
42d621d1
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
dd2793c8
 
a77436ab
 static const AVOption options[] = {
72864547
     { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "y",  "set Y expression",            OFFSET(comp_expr_str[Y]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "u",  "set U expression",            OFFSET(comp_expr_str[U]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "v",  "set V expression",            OFFSET(comp_expr_str[V]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "r",  "set R expression",            OFFSET(comp_expr_str[R]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "g",  "set G expression",            OFFSET(comp_expr_str[G]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "b",  "set B expression",            OFFSET(comp_expr_str[B]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
     { "a",  "set A expression",            OFFSET(comp_expr_str[A]),  AV_OPT_TYPE_STRING, { .str = "clipval" }, .flags = FLAGS },
b211607b
     { NULL }
dd2793c8
 };
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
8d2565c2
     LutContext *s = ctx->priv;
dd2793c8
     int i;
 
     for (i = 0; i < 4; i++) {
8d2565c2
         av_expr_free(s->comp_expr[i]);
         s->comp_expr[i] = NULL;
         av_freep(&s->comp_expr_str[i]);
dd2793c8
     }
 }
 
 #define YUV_FORMATS                                         \
716d413c
     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,    \
     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,    \
94ff68cc
     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,   \
716d413c
     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,   \
b74ebd09
     AV_PIX_FMT_YUVJ440P,                                             \
866404df
     AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUV422P9LE, AV_PIX_FMT_YUV420P9LE, \
b74ebd09
     AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUV440P10LE, \
     AV_PIX_FMT_YUV444P12LE, AV_PIX_FMT_YUV422P12LE, AV_PIX_FMT_YUV420P12LE, AV_PIX_FMT_YUV440P12LE, \
     AV_PIX_FMT_YUV444P14LE, AV_PIX_FMT_YUV422P14LE, AV_PIX_FMT_YUV420P14LE, \
     AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV420P16LE, \
     AV_PIX_FMT_YUVA444P16LE, AV_PIX_FMT_YUVA422P16LE, AV_PIX_FMT_YUVA420P16LE
dd2793c8
 
 #define RGB_FORMATS                             \
716d413c
     AV_PIX_FMT_ARGB,         AV_PIX_FMT_RGBA,         \
     AV_PIX_FMT_ABGR,         AV_PIX_FMT_BGRA,         \
b38e685c
     AV_PIX_FMT_RGB24,        AV_PIX_FMT_BGR24,        \
0edfd8e6
     AV_PIX_FMT_RGB48LE,      AV_PIX_FMT_RGBA64LE,     \
     AV_PIX_FMT_GBRP,         AV_PIX_FMT_GBRAP,        \
8175fb03
     AV_PIX_FMT_GBRP9LE,      AV_PIX_FMT_GBRP10LE,     \
bac508fe
     AV_PIX_FMT_GBRAP10LE,                             \
8175fb03
     AV_PIX_FMT_GBRP12LE,     AV_PIX_FMT_GBRP14LE,     \
     AV_PIX_FMT_GBRP16LE,     AV_PIX_FMT_GBRAP12LE,    \
     AV_PIX_FMT_GBRAP16LE
dd2793c8
 
ac627b3d
 static const enum AVPixelFormat yuv_pix_fmts[] = { YUV_FORMATS, AV_PIX_FMT_NONE };
 static const enum AVPixelFormat rgb_pix_fmts[] = { RGB_FORMATS, AV_PIX_FMT_NONE };
 static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, AV_PIX_FMT_NONE };
dd2793c8
 
 static int query_formats(AVFilterContext *ctx)
 {
8d2565c2
     LutContext *s = ctx->priv;
dd2793c8
 
af00d68a
     const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
faa8245b
                                                      s->is_yuv ? yuv_pix_fmts :
                                                                  all_pix_fmts;
fd682b18
     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     if (!fmts_list)
         return AVERROR(ENOMEM);
     return ff_set_common_formats(ctx, fmts_list);
dd2793c8
 }
 
 /**
  * Clip value val in the minval - maxval range.
  */
 static double clip(void *opaque, double val)
 {
8d2565c2
     LutContext *s = opaque;
     double minval = s->var_values[VAR_MINVAL];
     double maxval = s->var_values[VAR_MAXVAL];
dd2793c8
 
     return av_clip(val, minval, maxval);
 }
 
 /**
  * Compute gamma correction for value val, assuming the minval-maxval
  * range, val is clipped to a value contained in the same interval.
  */
 static double compute_gammaval(void *opaque, double gamma)
 {
8d2565c2
     LutContext *s = opaque;
     double val    = s->var_values[VAR_CLIPVAL];
     double minval = s->var_values[VAR_MINVAL];
     double maxval = s->var_values[VAR_MAXVAL];
dd2793c8
 
     return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
 }
 
b186b713
 /**
afa3c996
  * Compute ITU Rec.709 gamma correction of value val.
b186b713
  */
 static double compute_gammaval709(void *opaque, double gamma)
 {
     LutContext *s = opaque;
     double val    = s->var_values[VAR_CLIPVAL];
     double minval = s->var_values[VAR_MINVAL];
     double maxval = s->var_values[VAR_MAXVAL];
     double level = (val - minval) / (maxval - minval);
     level = level < 0.018 ? 4.5 * level
                           : 1.099 * pow(level, 1.0 / gamma) - 0.099;
     return level * (maxval - minval) + minval;
 }
 
dd2793c8
 static double (* const funcs1[])(void *, double) = {
4c96985a
     clip,
     compute_gammaval,
     compute_gammaval709,
dd2793c8
     NULL
 };
 
 static const char * const funcs1_names[] = {
     "clip",
     "gammaval",
b186b713
     "gammaval709",
dd2793c8
     NULL
 };
 
 static int config_props(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
8d2565c2
     LutContext *s = ctx->priv;
59ee9f78
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
ba1cbf40
     uint8_t rgba_map[4]; /* component index -> RGBA color index map */
dd2793c8
     int min[4], max[4];
21c2e201
     int val, color, ret;
dd2793c8
 
8d2565c2
     s->hsub = desc->log2_chroma_w;
     s->vsub = desc->log2_chroma_h;
dd2793c8
 
8d2565c2
     s->var_values[VAR_W] = inlink->w;
     s->var_values[VAR_H] = inlink->h;
5d8e836d
     s->is_16bit = desc->comp[0].depth > 8;
dd2793c8
 
     switch (inlink->format) {
716d413c
     case AV_PIX_FMT_YUV410P:
     case AV_PIX_FMT_YUV411P:
     case AV_PIX_FMT_YUV420P:
     case AV_PIX_FMT_YUV422P:
     case AV_PIX_FMT_YUV440P:
     case AV_PIX_FMT_YUV444P:
     case AV_PIX_FMT_YUVA420P:
94ff68cc
     case AV_PIX_FMT_YUVA422P:
     case AV_PIX_FMT_YUVA444P:
b74ebd09
     case AV_PIX_FMT_YUV420P9LE:
     case AV_PIX_FMT_YUV422P9LE:
     case AV_PIX_FMT_YUV444P9LE:
     case AV_PIX_FMT_YUVA420P9LE:
     case AV_PIX_FMT_YUVA422P9LE:
     case AV_PIX_FMT_YUVA444P9LE:
     case AV_PIX_FMT_YUV420P10LE:
     case AV_PIX_FMT_YUV422P10LE:
     case AV_PIX_FMT_YUV440P10LE:
     case AV_PIX_FMT_YUV444P10LE:
     case AV_PIX_FMT_YUVA420P10LE:
     case AV_PIX_FMT_YUVA422P10LE:
     case AV_PIX_FMT_YUVA444P10LE:
     case AV_PIX_FMT_YUV420P12LE:
     case AV_PIX_FMT_YUV422P12LE:
     case AV_PIX_FMT_YUV440P12LE:
     case AV_PIX_FMT_YUV444P12LE:
     case AV_PIX_FMT_YUV420P14LE:
     case AV_PIX_FMT_YUV422P14LE:
     case AV_PIX_FMT_YUV444P14LE:
     case AV_PIX_FMT_YUV420P16LE:
     case AV_PIX_FMT_YUV422P16LE:
     case AV_PIX_FMT_YUV444P16LE:
     case AV_PIX_FMT_YUVA420P16LE:
     case AV_PIX_FMT_YUVA422P16LE:
     case AV_PIX_FMT_YUVA444P16LE:
5d8e836d
         min[Y] = 16 * (1 << (desc->comp[0].depth - 8));
         min[U] = 16 * (1 << (desc->comp[1].depth - 8));
         min[V] = 16 * (1 << (desc->comp[2].depth - 8));
b74ebd09
         min[A] = 0;
5d8e836d
         max[Y] = 235 * (1 << (desc->comp[0].depth - 8));
         max[U] = 240 * (1 << (desc->comp[1].depth - 8));
         max[V] = 240 * (1 << (desc->comp[2].depth - 8));
72864547
         max[A] = (1 << desc->comp[0].depth) - 1;
dd2793c8
         break;
b38e685c
     case AV_PIX_FMT_RGB48LE:
     case AV_PIX_FMT_RGBA64LE:
         min[0] = min[1] = min[2] = min[3] = 0;
         max[0] = max[1] = max[2] = max[3] = 65535;
         break;
dd2793c8
     default:
         min[0] = min[1] = min[2] = min[3] = 0;
0edfd8e6
         max[0] = max[1] = max[2] = max[3] = 255 * (1 << (desc->comp[0].depth - 8));
dd2793c8
     }
 
8d2565c2
     s->is_yuv = s->is_rgb = 0;
0edfd8e6
     s->is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
8d2565c2
     if      (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) s->is_yuv = 1;
     else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) s->is_rgb = 1;
dd2793c8
 
8d2565c2
     if (s->is_rgb) {
ba1cbf40
         ff_fill_rgba_map(rgba_map, inlink->format);
8d2565c2
         s->step = av_get_bits_per_pixel(desc) >> 3;
b38e685c
         if (s->is_16bit) {
             s->step = s->step >> 1;
         }
dd2793c8
     }
 
21c2e201
     for (color = 0; color < desc->nb_components; color++) {
dd2793c8
         double res;
af00d68a
         int comp = s->is_rgb ? rgba_map[color] : color;
f3fea5ba
 
dd2793c8
         /* create the parsed expression */
d5e5e73c
         av_expr_free(s->comp_expr[color]);
         s->comp_expr[color] = NULL;
af00d68a
         ret = av_expr_parse(&s->comp_expr[color], s->comp_expr_str[color],
dd2793c8
                             var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
         if (ret < 0) {
             av_log(ctx, AV_LOG_ERROR,
f3fea5ba
                    "Error when parsing the expression '%s' for the component %d and color %d.\n",
af00d68a
                    s->comp_expr_str[comp], comp, color);
dd2793c8
             return AVERROR(EINVAL);
         }
 
         /* compute the lut */
af00d68a
         s->var_values[VAR_MAXVAL] = max[color];
         s->var_values[VAR_MINVAL] = min[color];
dd2793c8
 
aa234698
         for (val = 0; val < FF_ARRAY_ELEMS(s->lut[comp]); val++) {
8d2565c2
             s->var_values[VAR_VAL] = val;
af00d68a
             s->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
8d2565c2
             s->var_values[VAR_NEGVAL] =
af00d68a
                 av_clip(min[color] + max[color] - s->var_values[VAR_VAL],
f3fea5ba
                         min[color], max[color]);
dd2793c8
 
af00d68a
             res = av_expr_eval(s->comp_expr[color], s->var_values, s);
dd2793c8
             if (isnan(res)) {
                 av_log(ctx, AV_LOG_ERROR,
f3fea5ba
                        "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
af00d68a
                        s->comp_expr_str[color], val, comp);
dd2793c8
                 return AVERROR(EINVAL);
             }
72864547
             s->lut[comp][val] = av_clip((int)res, 0, max[A]);
8d2565c2
             av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
dd2793c8
         }
     }
 
     return 0;
 }
 
7e350379
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
dd2793c8
 {
     AVFilterContext *ctx = inlink->dst;
8d2565c2
     LutContext *s = ctx->priv;
dd2793c8
     AVFilterLink *outlink = ctx->outputs[0];
7e350379
     AVFrame *out;
3db3b278
     int i, j, plane, direct = 0;
dd2793c8
 
3db3b278
     if (av_frame_is_writable(in)) {
         direct = 1;
         out = in;
     } else {
45f5bf91
         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out) {
             av_frame_free(&in);
             return AVERROR(ENOMEM);
         }
         av_frame_copy_props(out, in);
3db3b278
     }
bff576c7
 
0edfd8e6
     if (s->is_rgb && s->is_16bit && !s->is_planar) {
b38e685c
         /* packed, 16-bit */
         uint16_t *inrow, *outrow, *inrow0, *outrow0;
         const int w = inlink->w;
         const int h = in->height;
         const uint16_t (*tab)[256*256] = (const uint16_t (*)[256*256])s->lut;
         const int in_linesize  =  in->linesize[0] / 2;
         const int out_linesize = out->linesize[0] / 2;
         const int step = s->step;
 
         inrow0  = (uint16_t*) in ->data[0];
         outrow0 = (uint16_t*) out->data[0];
 
         for (i = 0; i < h; i ++) {
             inrow  = inrow0;
             outrow = outrow0;
             for (j = 0; j < w; j++) {
 
                 switch (step) {
 #if HAVE_BIGENDIAN
                 case 4:  outrow[3] = av_bswap16(tab[3][av_bswap16(inrow[3])]); // Fall-through
                 case 3:  outrow[2] = av_bswap16(tab[2][av_bswap16(inrow[2])]); // Fall-through
                 case 2:  outrow[1] = av_bswap16(tab[1][av_bswap16(inrow[1])]); // Fall-through
                 default: outrow[0] = av_bswap16(tab[0][av_bswap16(inrow[0])]);
 #else
                 case 4:  outrow[3] = tab[3][inrow[3]]; // Fall-through
                 case 3:  outrow[2] = tab[2][inrow[2]]; // Fall-through
                 case 2:  outrow[1] = tab[1][inrow[1]]; // Fall-through
                 default: outrow[0] = tab[0][inrow[0]];
 #endif
                 }
                 outrow += step;
                 inrow  += step;
             }
             inrow0  += in_linesize;
             outrow0 += out_linesize;
         }
0edfd8e6
     } else if (s->is_rgb && !s->is_planar) {
dd2793c8
         /* packed */
b74ebd09
         uint8_t *inrow, *outrow, *inrow0, *outrow0;
0463df6f
         const int w = inlink->w;
         const int h = in->height;
b74ebd09
         const uint16_t (*tab)[256*256] = (const uint16_t (*)[256*256])s->lut;
0463df6f
         const int in_linesize  =  in->linesize[0];
         const int out_linesize = out->linesize[0];
         const int step = s->step;
 
bff576c7
         inrow0  = in ->data[0];
         outrow0 = out->data[0];
dd2793c8
 
0463df6f
         for (i = 0; i < h; i ++) {
cdeb803e
             inrow  = inrow0;
             outrow = outrow0;
002d9ded
             for (j = 0; j < w; j++) {
0463df6f
                 switch (step) {
45741dd8
                 case 4:  outrow[3] = tab[3][inrow[3]]; // Fall-through
                 case 3:  outrow[2] = tab[2][inrow[2]]; // Fall-through
                 case 2:  outrow[1] = tab[1][inrow[1]]; // Fall-through
4c6fa4ef
                 default: outrow[0] = tab[0][inrow[0]];
002d9ded
                 }
0463df6f
                 outrow += step;
                 inrow  += step;
dd2793c8
             }
0463df6f
             inrow0  += in_linesize;
             outrow0 += out_linesize;
dd2793c8
         }
b74ebd09
     } else if (s->is_16bit) {
0edfd8e6
         // planar >8 bit depth
b74ebd09
         uint16_t *inrow, *outrow;
 
         for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
             int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
             int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
21f94684
             int h = AV_CEIL_RSHIFT(inlink->h, vsub);
             int w = AV_CEIL_RSHIFT(inlink->w, hsub);
b74ebd09
             const uint16_t *tab = s->lut[plane];
             const int in_linesize  =  in->linesize[plane] / 2;
             const int out_linesize = out->linesize[plane] / 2;
 
             inrow  = (uint16_t *)in ->data[plane];
             outrow = (uint16_t *)out->data[plane];
 
             for (i = 0; i < h; i++) {
                 for (j = 0; j < w; j++) {
 #if HAVE_BIGENDIAN
                     outrow[j] = av_bswap16(tab[av_bswap16(inrow[j])]);
 #else
                     outrow[j] = tab[inrow[j]];
 #endif
                 }
                 inrow  += in_linesize;
                 outrow += out_linesize;
             }
         }
dd2793c8
     } else {
b74ebd09
         /* planar 8bit depth */
         uint8_t *inrow, *outrow;
 
e43a0a23
         for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
8d2565c2
             int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
             int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
21f94684
             int h = AV_CEIL_RSHIFT(inlink->h, vsub);
             int w = AV_CEIL_RSHIFT(inlink->w, hsub);
b74ebd09
             const uint16_t *tab = s->lut[plane];
0463df6f
             const int in_linesize  =  in->linesize[plane];
             const int out_linesize = out->linesize[plane];
dd2793c8
 
bff576c7
             inrow  = in ->data[plane];
             outrow = out->data[plane];
dd2793c8
 
59d33eaf
             for (i = 0; i < h; i++) {
79a1d986
                 for (j = 0; j < w; j++)
                     outrow[j] = tab[inrow[j]];
0463df6f
                 inrow  += in_linesize;
                 outrow += out_linesize;
dd2793c8
             }
         }
     }
 
3db3b278
     if (!direct)
         av_frame_free(&in);
 
bff576c7
     return ff_filter_frame(outlink, out);
dd2793c8
 }
 
3db40703
 static const AVFilterPad inputs[] = {
b211607b
     { .name         = "default",
       .type         = AVMEDIA_TYPE_VIDEO,
       .filter_frame = filter_frame,
       .config_props = config_props,
7e350379
     },
b211607b
     { NULL }
3db40703
 };
 static const AVFilterPad outputs[] = {
b211607b
     { .name = "default",
       .type = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
3db40703
 };
8bdb1191
 
a77436ab
 #define DEFINE_LUT_FILTER(name_, description_)                          \
cd43ca04
     AVFilter ff_vf_##name_ = {                                          \
633aa01f
         .name          = #name_,                                        \
         .description   = NULL_IF_CONFIG_SMALL(description_),            \
dd2793c8
         .priv_size     = sizeof(LutContext),                            \
20b46f8f
         .priv_class    = &name_ ## _class,                              \
a77436ab
         .init          = name_##_init,                                  \
dd2793c8
         .uninit        = uninit,                                        \
         .query_formats = query_formats,                                 \
3db40703
         .inputs        = inputs,                                        \
         .outputs       = outputs,                                       \
1776177b
         .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,        \
dd2793c8
     }
 
8fe0c527
 #if CONFIG_LUT_FILTER
a77436ab
 
 #define lut_options options
 AVFILTER_DEFINE_CLASS(lut);
 
d69a4177
 static int lut_init(AVFilterContext *ctx)
a77436ab
 {
     return 0;
 }
 
 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.");
8fe0c527
 #endif
a77436ab
 
8fe0c527
 #if CONFIG_LUTYUV_FILTER
a77436ab
 
 #define lutyuv_options options
 AVFILTER_DEFINE_CLASS(lutyuv);
 
a8ff830b
 static av_cold int lutyuv_init(AVFilterContext *ctx)
a77436ab
 {
af00d68a
     LutContext *s = ctx->priv;
a77436ab
 
af00d68a
     s->is_yuv = 1;
a77436ab
 
     return 0;
 }
 
 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.");
8fe0c527
 #endif
a77436ab
 
8fe0c527
 #if CONFIG_LUTRGB_FILTER
a77436ab
 
 #define lutrgb_options options
 AVFILTER_DEFINE_CLASS(lutrgb);
 
a8ff830b
 static av_cold int lutrgb_init(AVFilterContext *ctx)
a77436ab
 {
af00d68a
     LutContext *s = ctx->priv;
a77436ab
 
af00d68a
     s->is_rgb = 1;
a77436ab
 
     return 0;
 }
 
 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.");
8fe0c527
 #endif
15f03725
 
 #if CONFIG_NEGATE_FILTER
 
20b46f8f
 static const AVOption negate_options[] = {
af247634
     { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
b211607b
     { NULL }
20b46f8f
 };
 
a77436ab
 AVFILTER_DEFINE_CLASS(negate);
 
093804a9
 static av_cold int negate_init(AVFilterContext *ctx)
15f03725
 {
8d2565c2
     LutContext *s = ctx->priv;
20b46f8f
     int i;
15f03725
 
8d2565c2
     av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", s->negate_alpha);
15f03725
 
20b46f8f
     for (i = 0; i < 4; i++) {
af00d68a
         s->comp_expr_str[i] = av_strdup((i == 3 && !s->negate_alpha) ?
20b46f8f
                                           "val" : "negval");
8d2565c2
         if (!s->comp_expr_str[i]) {
20b46f8f
             uninit(ctx);
             return AVERROR(ENOMEM);
         }
     }
a77436ab
 
977ee8af
     return 0;
15f03725
 }
 
a77436ab
 DEFINE_LUT_FILTER(negate, "Negate input video.");
15f03725
 
d7847333
 #endif