libavfilter/vf_geq.c
8eecbaf5
 /*
  * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
493ebbd7
  * Copyright (C) 2012 Clément Bœsch <u pkh me>
8eecbaf5
  *
  * This file is part of FFmpeg.
  *
b3bf9b1d
  * FFmpeg is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
8eecbaf5
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
b3bf9b1d
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
8eecbaf5
  *
b3bf9b1d
  * You should have received a copy of the GNU 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.
8eecbaf5
  */
 
 /**
  * @file
  * Generic equation change filter
  * Originally written by Michael Niedermayer for the MPlayer project, and
  * ported by Clément Bœsch for FFmpeg.
  */
 
a056636c
 #include "libavutil/avassert.h"
8eecbaf5
 #include "libavutil/avstring.h"
 #include "libavutil/eval.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "internal.h"
 
ed93ed5e
 typedef struct GEQContext {
8eecbaf5
     const AVClass *class;
eac93932
     AVExpr *e[4];               ///< expressions for each plane
8f04220d
     char *expr_str[4+3];        ///< expression strings for each plane
a05a44e2
     AVFrame *picref;            ///< current input buffer
8eecbaf5
     int hsub, vsub;             ///< chroma subsampling
eac93932
     int planes;                 ///< number of planes
8f04220d
     int is_rgb;
ecc16d89
     int bps;
8eecbaf5
 } GEQContext;
 
a8d98377
 enum { Y = 0, U, V, A, G, B, R };
 
8eecbaf5
 #define OFFSET(x) offsetof(GEQContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption geq_options[] = {
a8d98377
     { "lum_expr",   "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "lum",        "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "cb_expr",    "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "cb",         "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "cr_expr",    "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "cr",         "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "alpha_expr", "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "a",          "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "red_expr",   "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "r",          "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "green_expr", "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "g",          "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "blue_expr",  "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "b",          "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
8eecbaf5
     {NULL},
 };
 
 AVFILTER_DEFINE_CLASS(geq);
 
 static inline double getpix(void *priv, double x, double y, int plane)
 {
     int xi, yi;
     GEQContext *geq = priv;
a05a44e2
     AVFrame *picref = geq->picref;
8eecbaf5
     const uint8_t *src = picref->data[plane];
ecc16d89
     int linesize = picref->linesize[plane];
21f94684
     const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width,  geq->hsub) : picref->width;
     const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
eac93932
 
     if (!src)
         return 0;
8eecbaf5
 
     xi = x = av_clipf(x, 0, w - 2);
     yi = y = av_clipf(y, 0, h - 2);
 
     x -= xi;
     y -= yi;
 
ecc16d89
     if (geq->bps > 8) {
         const uint16_t *src16 = (const uint16_t*)src;
         linesize /= 2;
 
         return (1-y)*((1-x)*src16[xi +  yi    * linesize] + x*src16[xi + 1 +  yi    * linesize])
               +   y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
     } else {
         return (1-y)*((1-x)*src[xi +  yi    * linesize] + x*src[xi + 1 +  yi    * linesize])
               +   y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
     }
8eecbaf5
 }
 
 //TODO: cubic interpolate
 //TODO: keep the last few frames
 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
 static double  cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
 static double  cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
eac93932
 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
8eecbaf5
 
13c39e96
 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
8eecbaf5
 
fd6228e6
 static av_cold int geq_init(AVFilterContext *ctx)
8eecbaf5
 {
     GEQContext *geq = ctx->priv;
     int plane, ret = 0;
 
a8d98377
     if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
8f04220d
         av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
         ret = AVERROR(EINVAL);
         goto end;
     }
a8d98377
     geq->is_rgb = !geq->expr_str[Y];
8f04220d
 
a8d98377
     if ((geq->expr_str[Y] || geq->expr_str[U] || geq->expr_str[V]) && (geq->expr_str[G] || geq->expr_str[B] || geq->expr_str[R])) {
8f04220d
         av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
8eecbaf5
         ret = AVERROR(EINVAL);
         goto end;
     }
 
a8d98377
     if (!geq->expr_str[U] && !geq->expr_str[V]) {
8eecbaf5
         /* No chroma at all: fallback on luma */
a8d98377
         geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
         geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
8eecbaf5
     } else {
         /* One chroma unspecified, fallback on the other */
a8d98377
         if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
         if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
8eecbaf5
     }
 
ecc16d89
     if (!geq->expr_str[A]) {
         char bps_string[8];
         snprintf(bps_string, sizeof(bps_string), "%d", (1<<geq->bps) - 1);
         geq->expr_str[A] = av_strdup(bps_string);
     }
a8d98377
     if (!geq->expr_str[G])
         geq->expr_str[G] = av_strdup("g(X,Y)");
     if (!geq->expr_str[B])
         geq->expr_str[B] = av_strdup("b(X,Y)");
     if (!geq->expr_str[R])
         geq->expr_str[R] = av_strdup("r(X,Y)");
8f04220d
 
     if (geq->is_rgb ?
a8d98377
             (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
8f04220d
                     :
a8d98377
             (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
8eecbaf5
         ret = AVERROR(ENOMEM);
         goto end;
     }
 
eac93932
     for (plane = 0; plane < 4; plane++) {
         static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
8f04220d
         static const char *const func2_yuv_names[]    = { "lum", "cb", "cr", "alpha", "p", NULL };
         static const char *const func2_rgb_names[]    = { "g", "b", "r", "alpha", "p", NULL };
         const char *const *func2_names       = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
eac93932
         double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
8eecbaf5
 
8f04220d
         ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
8eecbaf5
                             NULL, NULL, func2_names, func2, 0, ctx);
         if (ret < 0)
             break;
     }
 
 end:
     return ret;
 }
 
 static int geq_query_formats(AVFilterContext *ctx)
 {
8f04220d
     GEQContext *geq = ctx->priv;
0f16dfda
     static const enum AVPixelFormat yuv_pix_fmts[] = {
8eecbaf5
         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
eac93932
         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
         AV_PIX_FMT_GRAY8,
ecc16d89
         AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,  AV_PIX_FMT_YUV420P9,
         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
         AV_PIX_FMT_YUV444P10,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV420P10,
         AV_PIX_FMT_YUV440P10,
         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
         AV_PIX_FMT_GRAY10,
         AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_YUV420P12,
         AV_PIX_FMT_GRAY12,
         AV_PIX_FMT_YUV444P14,  AV_PIX_FMT_YUV422P14,  AV_PIX_FMT_YUV420P14,
         AV_PIX_FMT_YUV444P16,  AV_PIX_FMT_YUV422P16,  AV_PIX_FMT_YUV420P16,
         AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
         AV_PIX_FMT_GRAY16,
8eecbaf5
         AV_PIX_FMT_NONE
     };
0f16dfda
     static const enum AVPixelFormat rgb_pix_fmts[] = {
1f2baec7
         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
ecc16d89
         AV_PIX_FMT_GBRP9,
         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
         AV_PIX_FMT_GBRP14,
         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
8f04220d
         AV_PIX_FMT_NONE
     };
a0854c08
     AVFilterFormats *fmts_list;
 
8f04220d
     if (geq->is_rgb) {
a0854c08
         fmts_list = ff_make_format_list(rgb_pix_fmts);
8f04220d
     } else
a0854c08
         fmts_list = ff_make_format_list(yuv_pix_fmts);
     if (!fmts_list)
         return AVERROR(ENOMEM);
     return ff_set_common_formats(ctx, fmts_list);
8eecbaf5
 }
 
 static int geq_config_props(AVFilterLink *inlink)
 {
     GEQContext *geq = inlink->dst->priv;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
 
a056636c
     av_assert0(desc);
 
8eecbaf5
     geq->hsub = desc->log2_chroma_w;
     geq->vsub = desc->log2_chroma_h;
eac93932
     geq->planes = desc->nb_components;
ecc16d89
     geq->bps    = desc->comp[0].depth;
 
8eecbaf5
     return 0;
 }
 
a05a44e2
 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
8eecbaf5
 {
5d170b09
     int plane;
8eecbaf5
     GEQContext *geq = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
a05a44e2
     AVFrame *out;
8eecbaf5
     double values[VAR_VARS_NB] = {
183ce55b
         [VAR_N] = inlink->frame_count_out,
13c39e96
         [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
8eecbaf5
     };
 
5d170b09
     geq->picref = in;
a05a44e2
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
5d170b09
     if (!out) {
a05a44e2
         av_frame_free(&in);
5d170b09
         return AVERROR(ENOMEM);
     }
a05a44e2
     av_frame_copy_props(out, in);
8eecbaf5
 
eac93932
     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
8eecbaf5
         int x, y;
5d170b09
         uint8_t *dst = out->data[plane];
ecc16d89
         uint16_t *dst16 = (uint16_t*)out->data[plane];
5d170b09
         const int linesize = out->linesize[plane];
21f94684
         const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
         const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
8eecbaf5
 
         values[VAR_W]  = w;
         values[VAR_H]  = h;
         values[VAR_SW] = w / (double)inlink->w;
         values[VAR_SH] = h / (double)inlink->h;
 
         for (y = 0; y < h; y++) {
             values[VAR_Y] = y;
ecc16d89
             if (geq->bps > 8) {
                 for (x = 0; x < w; x++) {
                     values[VAR_X] = x;
                     dst16[x] = av_expr_eval(geq->e[plane], values, geq);
                 }
                 dst16 += linesize / 2;
             } else {
                 for (x = 0; x < w; x++) {
                     values[VAR_X] = x;
                     dst[x] = av_expr_eval(geq->e[plane], values, geq);
                 }
                 dst += linesize;
8eecbaf5
             }
         }
     }
 
a05a44e2
     av_frame_free(&geq->picref);
5d170b09
     return ff_filter_frame(outlink, out);
8eecbaf5
 }
 
 static av_cold void geq_uninit(AVFilterContext *ctx)
 {
     int i;
     GEQContext *geq = ctx->priv;
 
     for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
         av_expr_free(geq->e[i]);
 }
 
 static const AVFilterPad geq_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = geq_config_props,
5d170b09
         .filter_frame = geq_filter_frame,
8eecbaf5
     },
     { NULL }
 };
 
 static const AVFilterPad geq_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
 };
 
325f6e0a
 AVFilter ff_vf_geq = {
8eecbaf5
     .name          = "geq",
     .description   = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
     .priv_size     = sizeof(GEQContext),
     .init          = geq_init,
     .uninit        = geq_uninit,
     .query_formats = geq_query_formats,
     .inputs        = geq_inputs,
     .outputs       = geq_outputs,
     .priv_class    = &geq_class,
b7be8ea9
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
8eecbaf5
 };