2012-08-12 14:26:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2003 Michael Niedermayer
|
|
|
|
* Copyright (c) 2012 Jeremy Tran
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
2013-09-03 20:07:53 +03:00
|
|
|
* 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.
|
2012-08-12 14:26:17 +03:00
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-09-03 20:07:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2012-08-12 14:26:17 +03:00
|
|
|
*
|
2013-09-03 20:07:53 +03:00
|
|
|
* 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
|
2012-08-12 14:26:17 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Apply a hue/saturation filter to the input video
|
|
|
|
* Ported from MPlayer libmpcodecs/vf_hue.c.
|
|
|
|
*/
|
|
|
|
|
2012-08-16 10:15:58 +03:00
|
|
|
#include <float.h>
|
2012-09-14 20:31:13 +03:00
|
|
|
#include "libavutil/eval.h"
|
2012-08-12 14:26:17 +03:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-08-16 10:15:58 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-08-12 14:26:17 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
2012-09-14 20:31:13 +03:00
|
|
|
#define SAT_MIN_VAL -10
|
|
|
|
#define SAT_MAX_VAL 10
|
|
|
|
|
|
|
|
static const char *const var_names[] = {
|
|
|
|
"n", // frame count
|
|
|
|
"pts", // presentation timestamp expressed in AV_TIME_BASE units
|
|
|
|
"r", // frame rate
|
|
|
|
"t", // timestamp expressed in seconds
|
|
|
|
"tb", // timebase
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum var_name {
|
|
|
|
VAR_N,
|
|
|
|
VAR_PTS,
|
|
|
|
VAR_R,
|
|
|
|
VAR_T,
|
|
|
|
VAR_TB,
|
|
|
|
VAR_NB
|
|
|
|
};
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct HueContext {
|
2012-08-16 10:15:58 +03:00
|
|
|
const AVClass *class;
|
|
|
|
float hue_deg; /* hue expressed in degrees */
|
|
|
|
float hue; /* hue expressed in radians */
|
2012-09-14 20:31:13 +03:00
|
|
|
char *hue_deg_expr;
|
|
|
|
char *hue_expr;
|
|
|
|
AVExpr *hue_deg_pexpr;
|
|
|
|
AVExpr *hue_pexpr;
|
2012-08-12 14:26:17 +03:00
|
|
|
float saturation;
|
2012-09-14 20:31:13 +03:00
|
|
|
char *saturation_expr;
|
|
|
|
AVExpr *saturation_pexpr;
|
2013-08-21 20:42:32 +03:00
|
|
|
float brightness;
|
|
|
|
char *brightness_expr;
|
|
|
|
AVExpr *brightness_pexpr;
|
2012-08-12 14:26:17 +03:00
|
|
|
int hsub;
|
|
|
|
int vsub;
|
2013-12-20 21:10:46 +03:00
|
|
|
int is_first;
|
2012-08-12 14:26:17 +03:00
|
|
|
int32_t hue_sin;
|
|
|
|
int32_t hue_cos;
|
2012-09-14 20:31:13 +03:00
|
|
|
double var_values[VAR_NB];
|
2013-08-21 20:42:32 +03:00
|
|
|
uint8_t lut_l[256];
|
2013-08-19 22:09:58 +03:00
|
|
|
uint8_t lut_u[256][256];
|
|
|
|
uint8_t lut_v[256][256];
|
2018-08-01 19:48:08 +02:00
|
|
|
uint16_t lut_l16[65536];
|
|
|
|
uint16_t lut_u10[1024][1024];
|
|
|
|
uint16_t lut_v10[1024][1024];
|
2012-08-12 14:26:17 +03:00
|
|
|
} HueContext;
|
|
|
|
|
2012-08-16 10:15:58 +03:00
|
|
|
#define OFFSET(x) offsetof(HueContext, x)
|
2020-01-11 03:30:46 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2012-08-16 10:15:58 +03:00
|
|
|
static const AVOption hue_options[] = {
|
2012-09-14 20:31:13 +03:00
|
|
|
{ "h", "set the hue angle degrees expression", OFFSET(hue_deg_expr), AV_OPT_TYPE_STRING,
|
|
|
|
{ .str = NULL }, .flags = FLAGS },
|
|
|
|
{ "s", "set the saturation expression", OFFSET(saturation_expr), AV_OPT_TYPE_STRING,
|
2013-04-11 02:12:33 +03:00
|
|
|
{ .str = "1" }, .flags = FLAGS },
|
|
|
|
{ "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
|
2012-09-14 20:31:13 +03:00
|
|
|
{ .str = NULL }, .flags = FLAGS },
|
2013-08-21 20:42:32 +03:00
|
|
|
{ "b", "set the brightness expression", OFFSET(brightness_expr), AV_OPT_TYPE_STRING,
|
|
|
|
{ .str = "0" }, .flags = FLAGS },
|
2012-08-16 10:15:58 +03:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(hue);
|
|
|
|
|
2012-09-14 20:31:13 +03:00
|
|
|
static inline void compute_sin_and_cos(HueContext *hue)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Scale the value to the norm of the resulting (U,V) vector, that is
|
|
|
|
* the saturation.
|
2013-08-19 22:09:58 +03:00
|
|
|
* This will be useful in the apply_lut function.
|
2012-09-14 20:31:13 +03:00
|
|
|
*/
|
2015-12-25 23:17:14 +02:00
|
|
|
hue->hue_sin = lrint(sin(hue->hue) * (1 << 16) * hue->saturation);
|
|
|
|
hue->hue_cos = lrint(cos(hue->hue) * (1 << 16) * hue->saturation);
|
2012-09-14 20:31:13 +03:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:42:32 +03:00
|
|
|
static inline void create_luma_lut(HueContext *h)
|
|
|
|
{
|
|
|
|
const float b = h->brightness;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
h->lut_l[i] = av_clip_uint8(i + b * 25.5);
|
|
|
|
}
|
2018-08-01 19:48:08 +02:00
|
|
|
for (i = 0; i < 65536; i++) {
|
|
|
|
h->lut_l16[i] = av_clip_uintp2(i + b * 102.4, 10);
|
|
|
|
}
|
2013-08-21 20:42:32 +03:00
|
|
|
}
|
|
|
|
|
2013-08-19 22:09:58 +03:00
|
|
|
static inline void create_chrominance_lut(HueContext *h, const int32_t c,
|
|
|
|
const int32_t s)
|
|
|
|
{
|
|
|
|
int32_t i, j, u, v, new_u, new_v;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we consider U and V as the components of a 2D vector then its angle
|
|
|
|
* is the hue and the norm is the saturation
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
for (j = 0; j < 256; j++) {
|
2020-03-24 11:34:52 +02:00
|
|
|
/* Normalize the components from range [16;240] to [-112;112] */
|
2013-08-19 22:09:58 +03:00
|
|
|
u = i - 128;
|
|
|
|
v = j - 128;
|
|
|
|
/*
|
|
|
|
* Apply the rotation of the vector : (c * u) - (s * v)
|
|
|
|
* (s * u) + (c * v)
|
|
|
|
* De-normalize the components (without forgetting to scale 128
|
|
|
|
* by << 16)
|
|
|
|
* Finally scale back the result by >> 16
|
|
|
|
*/
|
|
|
|
new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
|
|
|
|
new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
|
|
|
|
|
|
|
|
/* Prevent a potential overflow */
|
2013-12-20 21:11:35 +03:00
|
|
|
h->lut_u[i][j] = av_clip_uint8(new_u);
|
|
|
|
h->lut_v[i][j] = av_clip_uint8(new_v);
|
2013-08-19 22:09:58 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-01 19:48:08 +02:00
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
for (j = 0; j < 1024; j++) {
|
|
|
|
u = i - 512;
|
|
|
|
v = j - 512;
|
|
|
|
/*
|
|
|
|
* Apply the rotation of the vector : (c * u) - (s * v)
|
|
|
|
* (s * u) + (c * v)
|
|
|
|
* De-normalize the components (without forgetting to scale 512
|
|
|
|
* by << 16)
|
|
|
|
* Finally scale back the result by >> 16
|
|
|
|
*/
|
|
|
|
new_u = ((c * u) - (s * v) + (1 << 15) + (512 << 16)) >> 16;
|
|
|
|
new_v = ((s * u) + (c * v) + (1 << 15) + (512 << 16)) >> 16;
|
|
|
|
|
|
|
|
/* Prevent a potential overflow */
|
|
|
|
h->lut_u10[i][j] = av_clip_uintp2(new_u, 10);
|
|
|
|
h->lut_v10[i][j] = av_clip_uintp2(new_v, 10);
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 22:09:58 +03:00
|
|
|
}
|
|
|
|
|
2013-04-21 18:12:02 +03:00
|
|
|
static int set_expr(AVExpr **pexpr_ptr, char **expr_ptr,
|
|
|
|
const char *expr, const char *option, void *log_ctx)
|
2012-08-12 14:26:17 +03:00
|
|
|
{
|
2013-02-20 01:38:41 +03:00
|
|
|
int ret;
|
2013-04-21 18:12:02 +03:00
|
|
|
AVExpr *new_pexpr;
|
|
|
|
char *new_expr;
|
2013-04-11 02:12:33 +03:00
|
|
|
|
2013-04-21 18:12:02 +03:00
|
|
|
new_expr = av_strdup(expr);
|
|
|
|
if (!new_expr)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = av_expr_parse(&new_pexpr, expr, var_names,
|
2013-04-11 02:12:33 +03:00
|
|
|
NULL, NULL, NULL, NULL, 0, log_ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
|
|
|
"Error when evaluating the expression '%s' for %s\n",
|
|
|
|
expr, option);
|
2013-04-21 18:12:02 +03:00
|
|
|
av_free(new_expr);
|
2013-02-20 01:45:12 +03:00
|
|
|
return ret;
|
2013-04-11 02:12:33 +03:00
|
|
|
}
|
2013-04-21 18:12:02 +03:00
|
|
|
|
|
|
|
if (*pexpr_ptr)
|
|
|
|
av_expr_free(*pexpr_ptr);
|
|
|
|
*pexpr_ptr = new_pexpr;
|
|
|
|
av_freep(expr_ptr);
|
|
|
|
*expr_ptr = new_expr;
|
|
|
|
|
2013-04-11 02:12:33 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-12 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2013-04-11 02:12:33 +03:00
|
|
|
{
|
|
|
|
HueContext *hue = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
2013-02-20 01:45:12 +03:00
|
|
|
if (hue->hue_expr && hue->hue_deg_expr) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"H and h options are incompatible and cannot be specified "
|
|
|
|
"at the same time\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2013-04-11 02:12:33 +03:00
|
|
|
#define SET_EXPR(expr, option) \
|
|
|
|
if (hue->expr##_expr) do { \
|
2013-04-21 18:12:02 +03:00
|
|
|
ret = set_expr(&hue->expr##_pexpr, &hue->expr##_expr, \
|
|
|
|
hue->expr##_expr, option, ctx); \
|
2013-04-11 02:12:33 +03:00
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
} while (0)
|
2013-08-21 20:42:32 +03:00
|
|
|
SET_EXPR(brightness, "b");
|
2013-04-11 02:12:33 +03:00
|
|
|
SET_EXPR(saturation, "s");
|
|
|
|
SET_EXPR(hue_deg, "h");
|
|
|
|
SET_EXPR(hue, "H");
|
2013-04-21 18:12:02 +03:00
|
|
|
#undef SET_EXPR
|
2013-02-20 01:45:12 +03:00
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
2013-08-21 20:42:32 +03:00
|
|
|
"H_expr:%s h_deg_expr:%s s_expr:%s b_expr:%s\n",
|
|
|
|
hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr, hue->brightness_expr);
|
2012-09-14 20:31:13 +03:00
|
|
|
compute_sin_and_cos(hue);
|
2013-12-20 21:10:46 +03:00
|
|
|
hue->is_first = 1;
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2012-08-31 02:56:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-16 10:15:58 +03:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
HueContext *hue = ctx->priv;
|
|
|
|
|
2013-08-21 20:42:32 +03:00
|
|
|
av_expr_free(hue->brightness_pexpr);
|
2012-09-14 20:31:13 +03:00
|
|
|
av_expr_free(hue->hue_deg_pexpr);
|
|
|
|
av_expr_free(hue->hue_pexpr);
|
|
|
|
av_expr_free(hue->saturation_pexpr);
|
2012-08-16 10:15:58 +03:00
|
|
|
}
|
|
|
|
|
2012-08-12 14:26:17 +03:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-08 21:54:00 +03:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
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-04-12 21:41:41 +03:00
|
|
|
AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
|
2012-10-08 21:54:00 +03:00
|
|
|
AV_PIX_FMT_YUVA420P,
|
2018-08-01 19:48:08 +02:00
|
|
|
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,
|
2012-10-08 21:54:00 +03:00
|
|
|
AV_PIX_FMT_NONE
|
2012-08-12 14:26:17 +03:00
|
|
|
};
|
2015-03-15 15:20:34 +02:00
|
|
|
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
|
|
|
|
if (!fmts_list)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_set_common_formats(ctx, fmts_list);
|
2012-08-12 14:26:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
HueContext *hue = inlink->dst->priv;
|
2012-10-20 07:43:48 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2012-08-12 14:26:17 +03:00
|
|
|
|
|
|
|
hue->hsub = desc->log2_chroma_w;
|
|
|
|
hue->vsub = desc->log2_chroma_h;
|
2012-09-14 20:31:13 +03:00
|
|
|
|
|
|
|
hue->var_values[VAR_N] = 0;
|
|
|
|
hue->var_values[VAR_TB] = av_q2d(inlink->time_base);
|
|
|
|
hue->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
|
|
|
|
NAN : av_q2d(inlink->frame_rate);
|
2012-08-12 14:26:17 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-21 20:42:32 +03:00
|
|
|
static void apply_luma_lut(HueContext *s,
|
|
|
|
uint8_t *ldst, const int dst_linesize,
|
|
|
|
uint8_t *lsrc, const int src_linesize,
|
|
|
|
int w, int h)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (h--) {
|
|
|
|
for (i = 0; i < w; i++)
|
|
|
|
ldst[i] = s->lut_l[lsrc[i]];
|
|
|
|
|
|
|
|
lsrc += src_linesize;
|
|
|
|
ldst += dst_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:48:08 +02:00
|
|
|
static void apply_luma_lut10(HueContext *s,
|
|
|
|
uint16_t *ldst, const int dst_linesize,
|
|
|
|
uint16_t *lsrc, const int src_linesize,
|
|
|
|
int w, int h)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (h--) {
|
|
|
|
for (i = 0; i < w; i++)
|
|
|
|
ldst[i] = s->lut_l16[lsrc[i]];
|
|
|
|
|
|
|
|
lsrc += src_linesize;
|
|
|
|
ldst += dst_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 22:09:58 +03:00
|
|
|
static void apply_lut(HueContext *s,
|
|
|
|
uint8_t *udst, uint8_t *vdst, const int dst_linesize,
|
|
|
|
uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
|
|
|
|
int w, int h)
|
2012-08-12 14:26:17 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (h--) {
|
|
|
|
for (i = 0; i < w; i++) {
|
2013-08-19 22:09:58 +03:00
|
|
|
const int u = usrc[i];
|
|
|
|
const int v = vsrc[i];
|
2012-08-12 14:26:17 +03:00
|
|
|
|
2013-08-19 22:09:58 +03:00
|
|
|
udst[i] = s->lut_u[u][v];
|
|
|
|
vdst[i] = s->lut_v[u][v];
|
2012-08-12 14:26:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
usrc += src_linesize;
|
|
|
|
vsrc += src_linesize;
|
|
|
|
udst += dst_linesize;
|
|
|
|
vdst += dst_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 19:48:08 +02:00
|
|
|
static void apply_lut10(HueContext *s,
|
|
|
|
uint16_t *udst, uint16_t *vdst, const int dst_linesize,
|
|
|
|
uint16_t *usrc, uint16_t *vsrc, const int src_linesize,
|
|
|
|
int w, int h)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (h--) {
|
|
|
|
for (i = 0; i < w; i++) {
|
|
|
|
const int u = av_clip_uintp2(usrc[i], 10);
|
|
|
|
const int v = av_clip_uintp2(vsrc[i], 10);
|
|
|
|
|
|
|
|
udst[i] = s->lut_u10[u][v];
|
|
|
|
vdst[i] = s->lut_v10[u][v];
|
|
|
|
}
|
|
|
|
|
|
|
|
usrc += src_linesize;
|
|
|
|
vsrc += src_linesize;
|
|
|
|
udst += dst_linesize;
|
|
|
|
vdst += dst_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
|
2012-09-14 20:31:13 +03:00
|
|
|
{
|
|
|
|
HueContext *hue = inlink->dst->priv;
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *outpic;
|
2013-08-19 22:09:58 +03:00
|
|
|
const int32_t old_hue_sin = hue->hue_sin, old_hue_cos = hue->hue_cos;
|
2013-08-21 20:42:32 +03:00
|
|
|
const float old_brightness = hue->brightness;
|
2013-03-09 07:17:01 +03:00
|
|
|
int direct = 0;
|
2018-08-01 19:48:08 +02:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
|
|
|
const int bps = desc->comp[0].depth > 8 ? 2 : 1;
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2013-03-09 07:17:01 +03:00
|
|
|
if (av_frame_is_writable(inpic)) {
|
|
|
|
direct = 1;
|
|
|
|
outpic = inpic;
|
|
|
|
} else {
|
2013-03-09 07:19:14 +03:00
|
|
|
outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!outpic) {
|
|
|
|
av_frame_free(&inpic);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(outpic, inpic);
|
2013-03-09 07:17:01 +03:00
|
|
|
}
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2016-08-30 15:28:41 +02:00
|
|
|
hue->var_values[VAR_N] = inlink->frame_count_out;
|
2013-04-11 21:42:32 +03:00
|
|
|
hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
|
|
|
|
hue->var_values[VAR_PTS] = TS2D(inpic->pts);
|
|
|
|
|
|
|
|
if (hue->saturation_expr) {
|
|
|
|
hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2013-04-11 21:42:32 +03:00
|
|
|
if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
|
|
|
|
hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
|
|
|
|
av_log(inlink->dst, AV_LOG_WARNING,
|
|
|
|
"Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
|
|
|
|
SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
|
2012-09-14 20:31:13 +03:00
|
|
|
}
|
2013-04-11 21:42:32 +03:00
|
|
|
}
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2013-08-21 20:42:32 +03:00
|
|
|
if (hue->brightness_expr) {
|
|
|
|
hue->brightness = av_expr_eval(hue->brightness_pexpr, hue->var_values, NULL);
|
|
|
|
|
|
|
|
if (hue->brightness < -10 || hue->brightness > 10) {
|
|
|
|
hue->brightness = av_clipf(hue->brightness, -10, 10);
|
|
|
|
av_log(inlink->dst, AV_LOG_WARNING,
|
|
|
|
"Brightness value not in range [%d,%d]: clipping value to %0.1f\n",
|
|
|
|
-10, 10, hue->brightness);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:42:32 +03:00
|
|
|
if (hue->hue_deg_expr) {
|
|
|
|
hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
|
|
|
|
hue->hue = hue->hue_deg * M_PI / 180;
|
|
|
|
} else if (hue->hue_expr) {
|
|
|
|
hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
|
2013-04-11 21:56:51 +03:00
|
|
|
hue->hue_deg = hue->hue * 180 / M_PI;
|
2013-04-11 21:42:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG,
|
2013-12-20 21:25:42 +03:00
|
|
|
"H:%0.1f*PI h:%0.1f s:%0.1f b:%0.f t:%0.1f n:%d\n",
|
2013-08-21 20:42:32 +03:00
|
|
|
hue->hue/M_PI, hue->hue_deg, hue->saturation, hue->brightness,
|
2013-04-11 21:42:32 +03:00
|
|
|
hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2013-04-11 21:42:32 +03:00
|
|
|
compute_sin_and_cos(hue);
|
2013-12-20 21:10:46 +03:00
|
|
|
if (hue->is_first || (old_hue_sin != hue->hue_sin || old_hue_cos != hue->hue_cos))
|
2013-08-19 22:09:58 +03:00
|
|
|
create_chrominance_lut(hue, hue->hue_cos, hue->hue_sin);
|
2012-09-14 20:31:13 +03:00
|
|
|
|
2013-12-20 21:10:46 +03:00
|
|
|
if (hue->is_first || (old_brightness != hue->brightness && hue->brightness))
|
2013-08-21 20:42:32 +03:00
|
|
|
create_luma_lut(hue);
|
|
|
|
|
2013-04-12 21:32:46 +03:00
|
|
|
if (!direct) {
|
2013-08-21 20:42:32 +03:00
|
|
|
if (!hue->brightness)
|
|
|
|
av_image_copy_plane(outpic->data[0], outpic->linesize[0],
|
2018-08-01 19:48:08 +02:00
|
|
|
inpic->data[0], inpic->linesize[0],
|
|
|
|
inlink->w * bps, inlink->h);
|
2013-04-12 21:32:46 +03:00
|
|
|
if (inpic->data[3])
|
|
|
|
av_image_copy_plane(outpic->data[3], outpic->linesize[3],
|
2018-08-01 19:48:08 +02:00
|
|
|
inpic->data[3], inpic->linesize[3],
|
|
|
|
inlink->w * bps, inlink->h);
|
2013-04-12 21:32:46 +03:00
|
|
|
}
|
2012-08-12 14:26:17 +03:00
|
|
|
|
2018-08-01 19:48:08 +02:00
|
|
|
if (bps > 1) {
|
|
|
|
apply_lut10(hue, (uint16_t*)outpic->data[1], (uint16_t*)outpic->data[2], outpic->linesize[1]/2,
|
|
|
|
(uint16_t*) inpic->data[1], (uint16_t*) inpic->data[2], inpic->linesize[1]/2,
|
|
|
|
AV_CEIL_RSHIFT(inlink->w, hue->hsub),
|
|
|
|
AV_CEIL_RSHIFT(inlink->h, hue->vsub));
|
|
|
|
if (hue->brightness)
|
|
|
|
apply_luma_lut10(hue, (uint16_t*)outpic->data[0], outpic->linesize[0]/2,
|
|
|
|
(uint16_t*) inpic->data[0], inpic->linesize[0]/2, inlink->w, inlink->h);
|
|
|
|
} else {
|
|
|
|
apply_lut(hue, outpic->data[1], outpic->data[2], outpic->linesize[1],
|
|
|
|
inpic->data[1], inpic->data[2], inpic->linesize[1],
|
|
|
|
AV_CEIL_RSHIFT(inlink->w, hue->hsub),
|
|
|
|
AV_CEIL_RSHIFT(inlink->h, hue->vsub));
|
|
|
|
if (hue->brightness)
|
|
|
|
apply_luma_lut(hue, outpic->data[0], outpic->linesize[0],
|
|
|
|
inpic->data[0], inpic->linesize[0], inlink->w, inlink->h);
|
|
|
|
}
|
2012-08-12 14:26:17 +03:00
|
|
|
|
2013-03-09 07:17:01 +03:00
|
|
|
if (!direct)
|
2013-03-09 07:19:14 +03:00
|
|
|
av_frame_free(&inpic);
|
2013-12-20 21:10:46 +03:00
|
|
|
|
|
|
|
hue->is_first = 0;
|
2012-12-02 03:11:48 +03:00
|
|
|
return ff_filter_frame(outlink, outpic);
|
2012-08-12 14:26:17 +03:00
|
|
|
}
|
|
|
|
|
2012-08-31 02:56:58 +03:00
|
|
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
|
|
|
char *res, int res_len, int flags)
|
|
|
|
{
|
2013-04-11 02:12:33 +03:00
|
|
|
HueContext *hue = ctx->priv;
|
2013-04-21 18:12:02 +03:00
|
|
|
int ret;
|
2013-04-11 02:12:33 +03:00
|
|
|
|
2013-04-21 18:12:02 +03:00
|
|
|
#define SET_EXPR(expr, option) \
|
|
|
|
do { \
|
|
|
|
ret = set_expr(&hue->expr##_pexpr, &hue->expr##_expr, \
|
|
|
|
args, option, ctx); \
|
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
} while (0)
|
2013-04-11 02:12:33 +03:00
|
|
|
|
2013-04-21 18:12:02 +03:00
|
|
|
if (!strcmp(cmd, "h")) {
|
|
|
|
SET_EXPR(hue_deg, "h");
|
|
|
|
av_freep(&hue->hue_expr);
|
|
|
|
} else if (!strcmp(cmd, "H")) {
|
|
|
|
SET_EXPR(hue, "H");
|
|
|
|
av_freep(&hue->hue_deg_expr);
|
|
|
|
} else if (!strcmp(cmd, "s")) {
|
|
|
|
SET_EXPR(saturation, "s");
|
2013-08-21 20:42:32 +03:00
|
|
|
} else if (!strcmp(cmd, "b")) {
|
|
|
|
SET_EXPR(brightness, "b");
|
2013-04-21 18:12:02 +03:00
|
|
|
} else
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
return 0;
|
2012-08-31 02:56:58 +03:00
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad hue_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-12-02 03:11:48 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 22:01:59 +03:00
|
|
|
.config_props = config_props,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad hue_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_vf_hue = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "hue",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
|
|
|
|
.priv_size = sizeof(HueContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
2012-08-31 02:56:58 +03:00
|
|
|
.process_command = process_command,
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = hue_inputs,
|
|
|
|
.outputs = hue_outputs,
|
|
|
|
.priv_class = &hue_class,
|
2013-05-09 02:04:41 +03:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2012-08-12 14:26:17 +03:00
|
|
|
};
|