2011-05-28 22:51:03 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2013-04-30 19:30:02 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2015-07-03 13:11:16 +02:00
|
|
|
#include "libavutil/bswap.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2011-05-28 22:51:03 +03:00
|
|
|
#include "libavutil/eval.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2011-05-28 22:51:03 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
2013-01-01 19:38:54 +03:00
|
|
|
#include "drawutils.h"
|
2024-08-12 15:29:00 +02:00
|
|
|
#include "filters.h"
|
2012-05-30 11:12:55 +03:00
|
|
|
#include "formats.h"
|
2012-05-30 12:20:32 +03:00
|
|
|
#include "video.h"
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2012-02-20 11:42:33 +03:00
|
|
|
static const char *const var_names[] = {
|
2011-05-28 22:51:03 +03:00
|
|
|
"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
|
|
|
|
};
|
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct LutContext {
|
2011-05-28 22:51:03 +03:00
|
|
|
const AVClass *class;
|
2015-07-01 11:39:24 +02:00
|
|
|
uint16_t lut[4][256 * 256]; ///< lookup table for each component
|
2011-05-28 22:51:03 +03:00
|
|
|
char *comp_expr_str[4];
|
|
|
|
AVExpr *comp_expr[4];
|
|
|
|
int hsub, vsub;
|
|
|
|
double var_values[VAR_VARS_NB];
|
|
|
|
int is_rgb, is_yuv;
|
2016-08-24 08:30:22 +02:00
|
|
|
int is_planar;
|
2015-07-01 11:39:24 +02:00
|
|
|
int is_16bit;
|
2011-05-28 22:51:03 +03:00
|
|
|
int step;
|
|
|
|
} 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)
|
2021-02-08 17:31:21 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2012-08-09 18:48:03 +03:00
|
|
|
static const AVOption options[] = {
|
2017-02-13 22:52:51 +02:00
|
|
|
{ "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 },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2011-05-28 22:51:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
LutContext *s = ctx->priv;
|
2011-05-28 22:51:03 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
2013-03-18 22:44:36 +03:00
|
|
|
av_expr_free(s->comp_expr[i]);
|
|
|
|
s->comp_expr[i] = NULL;
|
|
|
|
av_freep(&s->comp_expr_str[i]);
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define YUV_FORMATS \
|
2012-10-06 13:10:34 +03:00
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, \
|
|
|
|
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, \
|
2013-05-07 15:44:46 +03:00
|
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, \
|
2012-10-06 13:10:34 +03:00
|
|
|
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
|
2015-07-01 11:39:24 +02:00
|
|
|
AV_PIX_FMT_YUVJ440P, \
|
2015-07-03 18:58:56 +02:00
|
|
|
AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUV422P9LE, AV_PIX_FMT_YUV420P9LE, \
|
2015-07-01 11:39:24 +02:00
|
|
|
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
|
2011-05-28 22:51:03 +03:00
|
|
|
|
|
|
|
#define RGB_FORMATS \
|
2012-10-06 13:10:34 +03:00
|
|
|
AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, \
|
|
|
|
AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, \
|
2015-10-11 03:27:17 +02:00
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, \
|
2016-08-24 08:30:22 +02:00
|
|
|
AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGBA64LE, \
|
|
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, \
|
2016-08-31 11:18:40 +02:00
|
|
|
AV_PIX_FMT_GBRP9LE, AV_PIX_FMT_GBRP10LE, \
|
2017-08-07 11:52:18 +02:00
|
|
|
AV_PIX_FMT_GBRAP10LE, \
|
2016-08-31 11:18:40 +02:00
|
|
|
AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP14LE, \
|
|
|
|
AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRAP12LE, \
|
|
|
|
AV_PIX_FMT_GBRAP16LE
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2017-12-22 11:51:48 +02:00
|
|
|
#define GRAY_FORMATS \
|
|
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9LE, AV_PIX_FMT_GRAY10LE, \
|
2018-09-09 19:05:56 +02:00
|
|
|
AV_PIX_FMT_GRAY12LE, AV_PIX_FMT_GRAY14LE, AV_PIX_FMT_GRAY16LE
|
2017-12-22 11:51:48 +02:00
|
|
|
|
2012-10-08 21:54:00 +03:00
|
|
|
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 };
|
2017-12-22 11:51:48 +02:00
|
|
|
static const enum AVPixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, GRAY_FORMATS, AV_PIX_FMT_NONE };
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2024-10-07 16:02:15 +02:00
|
|
|
static int query_formats(const AVFilterContext *ctx,
|
|
|
|
AVFilterFormatsConfig **cfg_in,
|
|
|
|
AVFilterFormatsConfig **cfg_out)
|
2011-05-28 22:51:03 +03:00
|
|
|
{
|
2024-10-07 16:02:15 +02:00
|
|
|
const LutContext *s = ctx->priv;
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2013-05-16 18:00:10 +03:00
|
|
|
const enum AVPixelFormat *pix_fmts = s->is_rgb ? rgb_pix_fmts :
|
2013-08-31 13:30:16 +03:00
|
|
|
s->is_yuv ? yuv_pix_fmts :
|
|
|
|
all_pix_fmts;
|
2024-10-07 16:02:15 +02:00
|
|
|
return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clip value val in the minval - maxval range.
|
|
|
|
*/
|
|
|
|
static double clip(void *opaque, double val)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
LutContext *s = opaque;
|
|
|
|
double minval = s->var_values[VAR_MINVAL];
|
|
|
|
double maxval = s->var_values[VAR_MAXVAL];
|
2011-05-28 22:51:03 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
LutContext *s = opaque;
|
|
|
|
double val = s->var_values[VAR_CLIPVAL];
|
|
|
|
double minval = s->var_values[VAR_MINVAL];
|
|
|
|
double maxval = s->var_values[VAR_MAXVAL];
|
2011-05-28 22:51:03 +03:00
|
|
|
|
|
|
|
return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
|
|
|
|
}
|
|
|
|
|
2014-11-14 00:14:24 +02:00
|
|
|
/**
|
2015-01-27 12:22:45 +02:00
|
|
|
* Compute ITU Rec.709 gamma correction of value val.
|
2014-11-14 00:14:24 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-28 22:51:03 +03:00
|
|
|
static double (* const funcs1[])(void *, double) = {
|
2015-10-23 17:46:41 +02:00
|
|
|
clip,
|
|
|
|
compute_gammaval,
|
|
|
|
compute_gammaval709,
|
2011-05-28 22:51:03 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const funcs1_names[] = {
|
|
|
|
"clip",
|
|
|
|
"gammaval",
|
2014-11-14 00:14:24 +02:00
|
|
|
"gammaval709",
|
2011-05-28 22:51:03 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2013-03-18 22:44:36 +03:00
|
|
|
LutContext *s = ctx->priv;
|
2012-10-06 14:29:37 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2013-01-01 19:38:54 +03:00
|
|
|
uint8_t rgba_map[4]; /* component index -> RGBA color index map */
|
2011-05-28 22:51:03 +03:00
|
|
|
int min[4], max[4];
|
2013-02-27 19:08:50 +03:00
|
|
|
int val, color, ret;
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
s->hsub = desc->log2_chroma_w;
|
|
|
|
s->vsub = desc->log2_chroma_h;
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
s->var_values[VAR_W] = inlink->w;
|
|
|
|
s->var_values[VAR_H] = inlink->h;
|
2015-09-08 17:10:48 +02:00
|
|
|
s->is_16bit = desc->comp[0].depth > 8;
|
2011-05-28 22:51:03 +03:00
|
|
|
|
|
|
|
switch (inlink->format) {
|
2012-10-06 13:10:34 +03:00
|
|
|
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:
|
2013-05-07 15:44:46 +03:00
|
|
|
case AV_PIX_FMT_YUVA422P:
|
|
|
|
case AV_PIX_FMT_YUVA444P:
|
2015-07-01 11:39:24 +02:00
|
|
|
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:
|
2015-09-08 17:10:48 +02:00
|
|
|
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));
|
2015-07-01 11:39:24 +02:00
|
|
|
min[A] = 0;
|
2015-09-08 17:10:48 +02:00
|
|
|
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));
|
2017-02-13 22:52:51 +02:00
|
|
|
max[A] = (1 << desc->comp[0].depth) - 1;
|
2011-05-28 22:51:03 +03:00
|
|
|
break;
|
2015-10-11 03:27:17 +02:00
|
|
|
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;
|
2011-05-28 22:51:03 +03:00
|
|
|
default:
|
|
|
|
min[0] = min[1] = min[2] = min[3] = 0;
|
2016-08-24 08:30:22 +02:00
|
|
|
max[0] = max[1] = max[2] = max[3] = 255 * (1 << (desc->comp[0].depth - 8));
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
s->is_yuv = s->is_rgb = 0;
|
2016-08-24 08:30:22 +02:00
|
|
|
s->is_planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
|
2013-03-18 22:44:36 +03:00
|
|
|
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;
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
if (s->is_rgb) {
|
2013-01-01 19:38:54 +03:00
|
|
|
ff_fill_rgba_map(rgba_map, inlink->format);
|
2013-03-18 22:44:36 +03:00
|
|
|
s->step = av_get_bits_per_pixel(desc) >> 3;
|
2015-10-11 03:27:17 +02:00
|
|
|
if (s->is_16bit) {
|
|
|
|
s->step = s->step >> 1;
|
|
|
|
}
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
|
2013-02-27 19:08:50 +03:00
|
|
|
for (color = 0; color < desc->nb_components; color++) {
|
2011-05-28 22:51:03 +03:00
|
|
|
double res;
|
2013-05-16 18:00:10 +03:00
|
|
|
int comp = s->is_rgb ? rgba_map[color] : color;
|
2012-05-31 11:11:09 +03:00
|
|
|
|
2011-05-28 22:51:03 +03:00
|
|
|
/* create the parsed expression */
|
2013-05-17 11:51:59 +03:00
|
|
|
av_expr_free(s->comp_expr[color]);
|
|
|
|
s->comp_expr[color] = NULL;
|
2013-05-16 18:00:10 +03:00
|
|
|
ret = av_expr_parse(&s->comp_expr[color], s->comp_expr_str[color],
|
2011-05-28 22:51:03 +03:00
|
|
|
var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2012-05-31 11:11:09 +03:00
|
|
|
"Error when parsing the expression '%s' for the component %d and color %d.\n",
|
2013-05-16 18:00:10 +03:00
|
|
|
s->comp_expr_str[comp], comp, color);
|
2011-05-28 22:51:03 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the lut */
|
2013-05-16 18:00:10 +03:00
|
|
|
s->var_values[VAR_MAXVAL] = max[color];
|
|
|
|
s->var_values[VAR_MINVAL] = min[color];
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2017-02-13 23:49:41 +02:00
|
|
|
for (val = 0; val < FF_ARRAY_ELEMS(s->lut[comp]); val++) {
|
2013-03-18 22:44:36 +03:00
|
|
|
s->var_values[VAR_VAL] = val;
|
2013-05-16 18:00:10 +03:00
|
|
|
s->var_values[VAR_CLIPVAL] = av_clip(val, min[color], max[color]);
|
2013-03-18 22:44:36 +03:00
|
|
|
s->var_values[VAR_NEGVAL] =
|
2013-05-16 18:00:10 +03:00
|
|
|
av_clip(min[color] + max[color] - s->var_values[VAR_VAL],
|
2012-05-31 11:11:09 +03:00
|
|
|
min[color], max[color]);
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2013-05-16 18:00:10 +03:00
|
|
|
res = av_expr_eval(s->comp_expr[color], s->var_values, s);
|
2011-05-28 22:51:03 +03:00
|
|
|
if (isnan(res)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2012-05-31 11:11:09 +03:00
|
|
|
"Error when evaluating the expression '%s' for the value %d for the component %d.\n",
|
2013-05-16 18:00:10 +03:00
|
|
|
s->comp_expr_str[color], val, comp);
|
2011-05-28 22:51:03 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2017-02-13 22:52:51 +02:00
|
|
|
s->lut[comp][val] = av_clip((int)res, 0, max[A]);
|
2013-03-18 22:44:36 +03:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, s->lut[comp][val]);
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:39:09 +02:00
|
|
|
struct thread_data {
|
|
|
|
AVFrame *in;
|
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LOAD_PACKED_COMMON\
|
|
|
|
LutContext *s = ctx->priv;\
|
|
|
|
const struct thread_data *td = arg;\
|
|
|
|
\
|
|
|
|
int i, j;\
|
|
|
|
const int w = td->w;\
|
|
|
|
const int h = td->h;\
|
|
|
|
AVFrame *in = td->in;\
|
|
|
|
AVFrame *out = td->out;\
|
|
|
|
const uint16_t (*tab)[256*256] = (const uint16_t (*)[256*256])s->lut;\
|
|
|
|
const int step = s->step;\
|
|
|
|
\
|
|
|
|
const int slice_start = (h * jobnr ) / nb_jobs;\
|
|
|
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;\
|
|
|
|
|
|
|
|
/* packed, 16-bit */
|
|
|
|
static int lut_packed_16bits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
LOAD_PACKED_COMMON
|
|
|
|
|
|
|
|
uint16_t *inrow, *outrow, *inrow0, *outrow0;
|
|
|
|
const int in_linesize = in->linesize[0] / 2;
|
|
|
|
const int out_linesize = out->linesize[0] / 2;
|
|
|
|
inrow0 = (uint16_t *)in ->data[0];
|
|
|
|
outrow0 = (uint16_t *)out->data[0];
|
|
|
|
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
|
|
|
inrow = inrow0 + i * in_linesize;
|
|
|
|
outrow = outrow0 + i * out_linesize;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* packed, 8-bit */
|
|
|
|
static int lut_packed_8bits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
LOAD_PACKED_COMMON
|
|
|
|
|
|
|
|
uint8_t *inrow, *outrow, *inrow0, *outrow0;
|
|
|
|
const int in_linesize = in->linesize[0];
|
|
|
|
const int out_linesize = out->linesize[0];
|
|
|
|
inrow0 = in ->data[0];
|
|
|
|
outrow0 = out->data[0];
|
|
|
|
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
|
|
|
inrow = inrow0 + i * in_linesize;
|
|
|
|
outrow = outrow0 + i * out_linesize;
|
|
|
|
for (j = 0; j < w; j++) {
|
|
|
|
switch (step) {
|
|
|
|
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]];
|
|
|
|
}
|
|
|
|
outrow += step;
|
|
|
|
inrow += step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_PLANAR_COMMON\
|
|
|
|
LutContext *s = ctx->priv;\
|
|
|
|
const struct thread_data *td = arg;\
|
|
|
|
int i, j, plane;\
|
|
|
|
AVFrame *in = td->in;\
|
|
|
|
AVFrame *out = td->out;\
|
|
|
|
|
|
|
|
#define PLANAR_COMMON\
|
|
|
|
int vsub = plane == 1 || plane == 2 ? s->vsub : 0;\
|
|
|
|
int hsub = plane == 1 || plane == 2 ? s->hsub : 0;\
|
|
|
|
int h = AV_CEIL_RSHIFT(td->h, vsub);\
|
|
|
|
int w = AV_CEIL_RSHIFT(td->w, hsub);\
|
|
|
|
const uint16_t *tab = s->lut[plane];\
|
|
|
|
\
|
|
|
|
const int slice_start = (h * jobnr ) / nb_jobs;\
|
|
|
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;\
|
|
|
|
|
|
|
|
/* planar >8 bit depth */
|
|
|
|
static int lut_planar_16bits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
LOAD_PLANAR_COMMON
|
|
|
|
|
|
|
|
uint16_t *inrow, *outrow;
|
|
|
|
|
|
|
|
for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
|
|
|
|
PLANAR_COMMON
|
|
|
|
|
|
|
|
const int in_linesize = in->linesize[plane] / 2;
|
|
|
|
const int out_linesize = out->linesize[plane] / 2;
|
|
|
|
|
2019-07-05 12:51:29 +02:00
|
|
|
inrow = (uint16_t *)in ->data[plane] + slice_start * in_linesize;
|
|
|
|
outrow = (uint16_t *)out->data[plane] + slice_start * out_linesize;
|
2019-05-21 17:39:09 +02:00
|
|
|
|
|
|
|
for (i = slice_start; i < slice_end; 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* planar 8bit depth */
|
|
|
|
static int lut_planar_8bits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
LOAD_PLANAR_COMMON
|
|
|
|
|
|
|
|
uint8_t *inrow, *outrow;
|
|
|
|
|
|
|
|
for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
|
|
|
|
PLANAR_COMMON
|
|
|
|
|
|
|
|
const int in_linesize = in->linesize[plane];
|
|
|
|
const int out_linesize = out->linesize[plane];
|
|
|
|
|
|
|
|
inrow = in ->data[plane] + slice_start * in_linesize;
|
|
|
|
outrow = out->data[plane] + slice_start * out_linesize;
|
|
|
|
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
|
|
|
for (j = 0; j < w; j++)
|
|
|
|
outrow[j] = tab[inrow[j]];
|
|
|
|
inrow += in_linesize;
|
|
|
|
outrow += out_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PACKED_THREAD_DATA\
|
|
|
|
struct thread_data td = {\
|
|
|
|
.in = in,\
|
|
|
|
.out = out,\
|
|
|
|
.w = inlink->w,\
|
|
|
|
.h = in->height,\
|
|
|
|
};\
|
|
|
|
|
|
|
|
#define PLANAR_THREAD_DATA\
|
|
|
|
struct thread_data td = {\
|
|
|
|
.in = in,\
|
|
|
|
.out = out,\
|
|
|
|
.w = inlink->w,\
|
|
|
|
.h = inlink->h,\
|
|
|
|
};\
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
2011-05-28 22:51:03 +03:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2013-03-18 22:44:36 +03:00
|
|
|
LutContext *s = ctx->priv;
|
2011-05-28 22:51:03 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *out;
|
2019-05-21 17:39:09 +02:00
|
|
|
int direct = 0;
|
2011-05-28 22:51:03 +03:00
|
|
|
|
2013-04-17 13:07:46 +03:00
|
|
|
if (av_frame_is_writable(in)) {
|
|
|
|
direct = 1;
|
|
|
|
out = in;
|
|
|
|
} else {
|
2013-04-17 13:16:16 +03:00
|
|
|
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);
|
2013-04-17 13:07:46 +03:00
|
|
|
}
|
2012-11-29 00:39:35 +03:00
|
|
|
|
2016-08-24 08:30:22 +02:00
|
|
|
if (s->is_rgb && s->is_16bit && !s->is_planar) {
|
2015-10-11 03:27:17 +02:00
|
|
|
/* packed, 16-bit */
|
2019-05-21 17:39:09 +02:00
|
|
|
PACKED_THREAD_DATA
|
2021-08-15 21:33:25 +02:00
|
|
|
ff_filter_execute(ctx, lut_packed_16bits, &td, NULL,
|
|
|
|
FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
|
2016-08-24 08:30:22 +02:00
|
|
|
} else if (s->is_rgb && !s->is_planar) {
|
2019-05-21 17:39:09 +02:00
|
|
|
/* packed 8 bits */
|
|
|
|
PACKED_THREAD_DATA
|
2021-08-15 21:33:25 +02:00
|
|
|
ff_filter_execute(ctx, lut_packed_8bits, &td, NULL,
|
|
|
|
FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
|
2015-07-01 11:39:24 +02:00
|
|
|
} else if (s->is_16bit) {
|
2019-05-21 17:39:09 +02:00
|
|
|
/* planar >8 bit depth */
|
|
|
|
PLANAR_THREAD_DATA
|
2021-08-15 21:33:25 +02:00
|
|
|
ff_filter_execute(ctx, lut_planar_16bits, &td, NULL,
|
|
|
|
FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
|
2011-05-28 22:51:03 +03:00
|
|
|
} else {
|
2015-07-01 11:39:24 +02:00
|
|
|
/* planar 8bit depth */
|
2019-05-21 17:39:09 +02:00
|
|
|
PLANAR_THREAD_DATA
|
2021-08-15 21:33:25 +02:00
|
|
|
ff_filter_execute(ctx, lut_planar_8bits, &td, NULL,
|
|
|
|
FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
|
2013-04-17 13:07:46 +03:00
|
|
|
if (!direct)
|
|
|
|
av_frame_free(&in);
|
|
|
|
|
2012-11-29 00:39:35 +03:00
|
|
|
return ff_filter_frame(outlink, out);
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:31:21 +02:00
|
|
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
|
|
|
char *res, int res_len, int flags)
|
|
|
|
{
|
|
|
|
int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return config_props(ctx->inputs[0]);
|
|
|
|
}
|
|
|
|
|
2012-07-21 19:45:52 +03:00
|
|
|
static const AVFilterPad inputs[] = {
|
2013-09-07 15:13:50 +03:00
|
|
|
{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_props,
|
2012-11-28 10:41:07 +03:00
|
|
|
},
|
2012-07-21 19:45:52 +03:00
|
|
|
};
|
2013-04-10 22:54:13 +03:00
|
|
|
|
2021-09-10 22:44:14 +02:00
|
|
|
#define DEFINE_LUT_FILTER(name_, description_, priv_class_) \
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_##name_ = { \
|
2011-07-21 12:00:47 +03:00
|
|
|
.name = #name_, \
|
|
|
|
.description = NULL_IF_CONFIG_SMALL(description_), \
|
2021-09-10 22:44:14 +02:00
|
|
|
.priv_class = &priv_class_ ## _class, \
|
2011-05-28 22:51:03 +03:00
|
|
|
.priv_size = sizeof(LutContext), \
|
2012-08-09 18:48:03 +03:00
|
|
|
.init = name_##_init, \
|
2011-05-28 22:51:03 +03:00
|
|
|
.uninit = uninit, \
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(inputs), \
|
2023-08-03 14:37:51 +02:00
|
|
|
FILTER_OUTPUTS(ff_video_default_filterpad), \
|
2024-10-07 16:02:15 +02:00
|
|
|
FILTER_QUERY_FUNC2(query_formats), \
|
2021-02-08 17:31:21 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | \
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS, \
|
|
|
|
.process_command = process_command, \
|
2011-05-28 22:51:03 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 22:44:14 +02:00
|
|
|
AVFILTER_DEFINE_CLASS_EXT(lut, "lut/lutyuv/lutrgb", options);
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2021-09-10 22:44:14 +02:00
|
|
|
#if CONFIG_LUT_FILTER
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2021-09-10 22:50:57 +02:00
|
|
|
#define lut_init NULL
|
2021-09-10 22:44:14 +02:00
|
|
|
DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.",
|
|
|
|
lut);
|
2021-09-10 22:50:57 +02:00
|
|
|
#undef lut_init
|
2011-05-28 22:51:03 +03:00
|
|
|
#endif
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2011-05-28 22:51:03 +03:00
|
|
|
#if CONFIG_LUTYUV_FILTER
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2013-05-05 12:42:18 +03:00
|
|
|
static av_cold int lutyuv_init(AVFilterContext *ctx)
|
2012-08-09 18:48:03 +03:00
|
|
|
{
|
2013-05-16 18:00:10 +03:00
|
|
|
LutContext *s = ctx->priv;
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2013-05-16 18:00:10 +03:00
|
|
|
s->is_yuv = 1;
|
2012-08-09 18:48:03 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:44:14 +02:00
|
|
|
DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.",
|
|
|
|
lut);
|
2011-05-28 22:51:03 +03:00
|
|
|
#endif
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2011-05-28 22:51:03 +03:00
|
|
|
#if CONFIG_LUTRGB_FILTER
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2013-05-05 12:42:18 +03:00
|
|
|
static av_cold int lutrgb_init(AVFilterContext *ctx)
|
2012-08-09 18:48:03 +03:00
|
|
|
{
|
2013-05-16 18:00:10 +03:00
|
|
|
LutContext *s = ctx->priv;
|
2012-08-09 18:48:03 +03:00
|
|
|
|
2013-05-16 18:00:10 +03:00
|
|
|
s->is_rgb = 1;
|
2012-08-09 18:48:03 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:44:14 +02:00
|
|
|
DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.",
|
|
|
|
lut);
|
2011-05-28 22:51:03 +03:00
|
|
|
#endif
|