Browse code

lavfi/hue: add process_command callback

This allows dynamic reconfiguration of the filter.
The callback uses some code that was in the init function. Hence this code
has been moved in its own function.

Signed-off-by: Stefano Sabatini <stefasab@gmail.com>

Jérémy Tran authored on 2012/08/31 08:56:58
Showing 3 changed files
... ...
@@ -2350,6 +2350,18 @@ hue=PI/2:1
2350 2350
 @end example
2351 2351
 @end itemize
2352 2352
 
2353
+@subsection Commands
2354
+
2355
+This filter supports the following command:
2356
+@table @option
2357
+@item reinit
2358
+Modify the hue and/or the saturation of the input video.
2359
+The command accepts the same named options and syntax than when calling the
2360
+filter from the command-line.
2361
+
2362
+If a parameter is omitted, it is kept at its current value.
2363
+@end table
2364
+
2353 2365
 @section idet
2354 2366
 
2355 2367
 Interlaceing detect filter. This filter tries to detect if the input is
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  15
33
-#define LIBAVFILTER_VERSION_MICRO 100
33
+#define LIBAVFILTER_VERSION_MICRO 101
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
... ...
@@ -63,19 +63,19 @@ static const AVOption hue_options[] = {
63 63
 
64 64
 AVFILTER_DEFINE_CLASS(hue);
65 65
 
66
-static av_cold int init(AVFilterContext *ctx, const char *args)
66
+static inline int set_options(AVFilterContext *ctx, const char *args)
67 67
 {
68 68
     HueContext *hue = ctx->priv;
69 69
     int n, ret;
70 70
     char c1 = 0, c2 = 0;
71 71
     char *equal;
72 72
 
73
-    hue->class = &hue_class;
74
-    av_opt_set_defaults(hue);
75
-
76 73
     if (args) {
77 74
         /* named options syntax */
78 75
         if (equal = strchr(args, '=')) {
76
+            hue->hue     = -FLT_MAX;
77
+            hue->hue_deg = -FLT_MAX;
78
+
79 79
             if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
80 80
                 return ret;
81 81
             if (hue->hue != -FLT_MAX && hue->hue_deg != -FLT_MAX) {
... ...
@@ -103,6 +103,20 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
103 103
         }
104 104
     }
105 105
 
106
+    return 0;
107
+}
108
+
109
+static av_cold int init(AVFilterContext *ctx, const char *args)
110
+{
111
+    HueContext *hue = ctx->priv;
112
+    int ret;
113
+
114
+    hue->class = &hue_class;
115
+    av_opt_set_defaults(hue);
116
+
117
+    if ((ret = set_options(ctx, args)) < 0)
118
+        return ret;
119
+
106 120
     if (hue->saturation == -FLT_MAX)
107 121
         hue->hue = SAT_DEFAULT_VAL;
108 122
     if (hue->hue == -FLT_MAX)
... ...
@@ -223,6 +237,28 @@ static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
223 223
     return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
224 224
 }
225 225
 
226
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
227
+                           char *res, int res_len, int flags)
228
+{
229
+    HueContext *hue = ctx->priv;
230
+    int ret;
231
+
232
+    if (!strcmp(cmd, "reinit")) {
233
+        if ((ret = set_options(ctx, args)) < 0)
234
+            return ret;
235
+
236
+        if (hue->hue_deg != -FLT_MAX)
237
+            /* Convert angle from degrees to radians */
238
+            hue->hue = hue->hue_deg * M_PI / 180;
239
+
240
+        hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
241
+        hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
242
+    } else
243
+        return AVERROR(ENOSYS);
244
+
245
+    return 0;
246
+}
247
+
226 248
 AVFilter avfilter_vf_hue = {
227 249
     .name        = "hue",
228 250
     .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
... ...
@@ -232,6 +268,7 @@ AVFilter avfilter_vf_hue = {
232 232
     .init          = init,
233 233
     .uninit        = uninit,
234 234
     .query_formats = query_formats,
235
+    .process_command = process_command,
235 236
 
236 237
     .inputs = (const AVFilterPad[]) {
237 238
         {