[FFmpeg-devel] [PATCH] lavfi/hue: add dynamic expression support
Jérémy Tran
tran.jeremy.av at gmail.com
Mon Sep 3 15:19:35 CEST 2012
---
doc/filters.texi | 19 +++++
libavfilter/vf_hue.c | 198 ++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 182 insertions(+), 35 deletions(-)
diff --git a/doc/filters.texi b/doc/filters.texi
index 712936d..f3bc767 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2348,6 +2348,25 @@ h and s, so the following example will issue an error:
@example
hue=PI/2:1
@end example
+
+ at item
+Set the hue to PI times current time and make the saturation swing between 0
+and 2:
+ at example
+hue="H=2*PI*t: s=sin(2*PI*t)+1"
+ at end example
+
+ at item
+3 seconds saturation fade-in:
+ at example
+hue=s=min(t/3,1)
+ at end example
+
+ at item
+3 seconds saturation fade-out at 5 seconds:
+ at example
+hue="s=min(1, max(0, min(1, -1/3*t+8/3)))"
+ at end example
@end itemize
@subsection Commands
diff --git a/libavfilter/vf_hue.c b/libavfilter/vf_hue.c
index dd36d8e..d2d6fab 100644
--- a/libavfilter/vf_hue.c
+++ b/libavfilter/vf_hue.c
@@ -26,6 +26,7 @@
*/
#include <float.h>
+#include "libavutil/eval.h"
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
@@ -38,26 +39,50 @@
#define HUE_DEFAULT_VAL 0
#define SAT_DEFAULT_VAL 1
+#define SAT_MIN_VAL -10
+#define SAT_MAX_VAL 10
+
+#define CONVERT_HUE_FROM_DEGREES_TO_RADIANS(hue) \
+ do { \
+ hue->hue = hue->hue_deg * M_PI / 180; \
+ } while (0) \
+
typedef struct {
const AVClass *class;
float hue_deg; /* hue expressed in degrees */
float hue; /* hue expressed in radians */
+ char *hue_deg_expr;
+ char *hue_expr;
+ AVExpr *hue_deg_pexpr;
+ AVExpr *hue_pexpr;
float saturation;
+ char *saturation_expr;
+ AVExpr *saturation_pexpr;
int hsub;
int vsub;
int32_t hue_sin;
int32_t hue_cos;
+ int flat_syntax;
} HueContext;
+static const char *const var_names[] = {
+ "t", // timestamp expressed in seconds
+ NULL
+};
+
+enum var_name {
+ VAR_T
+};
+
#define OFFSET(x) offsetof(HueContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
static const AVOption hue_options[] = {
- { "h", "set the hue angle degrees", OFFSET(hue_deg), AV_OPT_TYPE_FLOAT,
- { -FLT_MAX }, -FLT_MAX, FLT_MAX, FLAGS },
- { "H", "set the hue angle radians", OFFSET(hue), AV_OPT_TYPE_FLOAT,
- { -FLT_MAX }, -FLT_MAX, FLT_MAX, FLAGS },
- { "s", "set the saturation value", OFFSET(saturation), AV_OPT_TYPE_FLOAT,
- { SAT_DEFAULT_VAL }, -10, 10, FLAGS },
+ { "h", "set the hue angle degrees expression", OFFSET(hue_deg_expr), AV_OPT_TYPE_STRING,
+ { 0 }, .flags = FLAGS },
+ { "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
+ { 0 }, .flags = FLAGS },
+ { "s", "set the saturation expression", OFFSET(saturation_expr), AV_OPT_TYPE_STRING,
+ { 0 }, .flags = FLAGS },
{ NULL }
};
@@ -68,22 +93,19 @@ static inline int set_options(AVFilterContext *ctx, const char *args)
HueContext *hue = ctx->priv;
int n, ret;
char c1 = 0, c2 = 0;
- char *equal;
if (args) {
/* named options syntax */
- if (equal = strchr(args, '=')) {
- hue->hue = -FLT_MAX;
- hue->hue_deg = -FLT_MAX;
-
+ if (strchr(args, '=')) {
if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
return ret;
- if (hue->hue != -FLT_MAX && hue->hue_deg != -FLT_MAX) {
+ 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);
}
+ hue->flat_syntax = 0;
/* compatibility h:s syntax */
} else {
n = sscanf(args, "%f%c%f%c", &hue->hue_deg, &c1, &hue->saturation, &c2);
@@ -94,12 +116,14 @@ static inline int set_options(AVFilterContext *ctx, const char *args)
return AVERROR(EINVAL);
}
- if (hue->saturation < -10 || hue->saturation > 10) {
+ if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
av_log(ctx, AV_LOG_ERROR,
"Invalid value for saturation %0.1f: "
- "must be included between range -10 and +10\n", hue->saturation);
+ "must be included between range %d and +%d\n",
+ hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
return AVERROR(EINVAL);
}
+ hue->flat_syntax = 1;
}
}
@@ -113,20 +137,11 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
hue->class = &hue_class;
av_opt_set_defaults(hue);
+ hue->saturation = SAT_DEFAULT_VAL;
if ((ret = set_options(ctx, args)) < 0)
return ret;
- if (hue->saturation == -FLT_MAX)
- hue->hue = SAT_DEFAULT_VAL;
- if (hue->hue == -FLT_MAX)
- hue->hue = HUE_DEFAULT_VAL;
- if (hue->hue_deg != -FLT_MAX)
- /* Convert angle from degrees to radians */
- hue->hue = hue->hue_deg * M_PI / 180;
-
- av_log(ctx, AV_LOG_VERBOSE, "hue:%f*PI hue_deg:%f saturation:%f\n",
- hue->hue/M_PI, hue->hue*180/M_PI, hue->saturation);
return 0;
}
@@ -152,13 +167,8 @@ static int query_formats(AVFilterContext *ctx)
return 0;
}
-static int config_props(AVFilterLink *inlink)
+static inline void compute_sin_and_cos(HueContext *hue)
{
- HueContext *hue = inlink->dst->priv;
- const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
-
- hue->hsub = desc->log2_chroma_w;
- hue->vsub = desc->log2_chroma_h;
/*
* Scale the value to the norm of the resulting (U,V) vector, that is
* the saturation.
@@ -166,6 +176,50 @@ static int config_props(AVFilterLink *inlink)
*/
hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
+}
+
+static int config_props(AVFilterLink *inlink)
+{
+ HueContext *hue = inlink->dst->priv;
+ const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
+ int ret;
+
+ if (hue->flat_syntax) {
+ CONVERT_HUE_FROM_DEGREES_TO_RADIANS(hue);
+ compute_sin_and_cos(hue);
+ } else {
+ if (!hue->hue_expr && !hue->hue_deg_expr) {
+ hue->hue = HUE_DEFAULT_VAL;
+ } else {
+ if (hue->hue_deg_expr &&
+ (ret = av_expr_parse(&hue->hue_deg_pexpr, hue->hue_deg_expr, var_names,
+ NULL, NULL, NULL, NULL, 0, inlink->dst)) < 0) {
+ av_log(inlink->dst, AV_LOG_ERROR,
+ "Parsing failed evaluating expression h='%s'\n",
+ hue->hue_deg_expr);
+ return AVERROR(EINVAL);
+ }
+ if (hue->hue_expr &&
+ (ret = av_expr_parse(&hue->hue_pexpr, hue->hue_expr, var_names,
+ NULL, NULL, NULL, NULL, 0, inlink->dst)) < 0) {
+ av_log(inlink->dst, AV_LOG_ERROR,
+ "Parsing failed evaluating expression H='%s'\n",
+ hue->hue_expr);
+ return AVERROR(EINVAL);
+ }
+ }
+ if (hue->saturation_expr &&
+ (ret = av_expr_parse(&hue->saturation_pexpr, hue->saturation_expr, var_names,
+ NULL, NULL, NULL, NULL, 0, inlink->dst) < 0)) {
+ av_log(inlink->dst, AV_LOG_ERROR,
+ "Parsing failed evaluating expression s='%s'\n",
+ hue->saturation_expr);
+ return AVERROR(EINVAL);
+ }
+ }
+
+ hue->hsub = desc->log2_chroma_w;
+ hue->vsub = desc->log2_chroma_h;
return 0;
}
@@ -209,6 +263,66 @@ static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_line
}
}
+static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
+{
+ HueContext *hue = inlink->dst->priv;
+ double var_values[2] = { 0 };
+ AVFilterLink *outlink = inlink->dst->outputs[0];
+ AVFilterBufferRef *buf_out;
+
+ outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+
+ if (!outlink->out_buf)
+ return AVERROR(ENOMEM);
+
+ avfilter_copy_buffer_ref_props(outlink->out_buf, inpic);
+ outlink->out_buf->video->w = outlink->w;
+ outlink->out_buf->video->h = outlink->h;
+ buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
+
+ if (!buf_out)
+ return AVERROR(ENOMEM);
+
+ if (!hue->flat_syntax) {
+ var_values[VAR_T] = inpic->pts == AV_NOPTS_VALUE ?
+ NAN : inpic->pts * av_q2d(inlink->time_base);
+
+ if (hue->saturation_expr) {
+ hue->saturation = av_expr_eval(hue->saturation_pexpr, var_values, NULL);
+
+ if (hue->saturation > SAT_MAX_VAL) {
+ hue->saturation = SAT_MAX_VAL;
+ av_log(inlink->dst, AV_LOG_WARNING,
+ "Saturation expression resulted in a value greater "
+ "than %d; clipping value to %d\n",
+ SAT_MAX_VAL, SAT_MAX_VAL);
+ } else if (hue->saturation < SAT_MIN_VAL) {
+ hue->saturation = SAT_MIN_VAL;
+ av_log(inlink->dst, AV_LOG_WARNING,
+ "Saturation expression resulted in a value lesser "
+ "than %d; clipping value to %d\n",
+ SAT_MIN_VAL, SAT_MIN_VAL);
+ }
+ }
+
+ if (hue->hue_deg_expr) {
+ hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, var_values, NULL);
+ CONVERT_HUE_FROM_DEGREES_TO_RADIANS(hue);
+ } else if (hue->hue_expr) {
+ hue->hue = av_expr_eval(hue->hue_pexpr, var_values, NULL);
+ }
+
+ av_log(inlink->dst, AV_LOG_DEBUG,
+ "Hue resulted to %0.1f radians\n"
+ "Saturation resulted to %0.1f\n",
+ hue->hue, hue->saturation);
+
+ compute_sin_and_cos(hue);
+ }
+
+ return ff_start_frame(outlink, buf_out);
+}
+
static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
{
HueContext *hue = inlink->dst->priv;
@@ -242,17 +356,30 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
{
HueContext *hue = ctx->priv;
int ret;
+ char *old_hue_expr, *old_hue_deg_expr;
if (!strcmp(cmd, "reinit")) {
+ old_hue_expr = hue->hue_expr;
+ old_hue_deg_expr = hue->hue_deg_expr;
+
+ hue->hue_expr = NULL;
+ hue->hue_deg_expr = NULL;
+
if ((ret = set_options(ctx, args)) < 0)
return ret;
- if (hue->hue_deg != -FLT_MAX)
- /* Convert angle from degrees to radians */
- hue->hue = hue->hue_deg * M_PI / 180;
+ if (!hue->flat_syntax && !hue->hue_expr && !hue->hue_deg_expr) {
+ hue->hue_expr = old_hue_expr;
+ hue->hue_deg_expr = old_hue_deg_expr;
+ }
+
+ if ((ret = config_props(ctx->inputs[0])) < 0)
+ return ret;
+
+ if (hue->hue_deg_expr)
+ CONVERT_HUE_FROM_DEGREES_TO_RADIANS(hue);
- hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
- hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
+ compute_sin_and_cos(hue);
} else
return AVERROR(ENOSYS);
@@ -274,6 +401,7 @@ AVFilter avfilter_vf_hue = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
+ .start_frame = start_frame,
.draw_slice = draw_slice,
.config_props = config_props,
.min_perms = AV_PERM_READ,
--
1.7.11.3
More information about the ffmpeg-devel
mailing list