]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_bilateral.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / vf_bilateral.c
index 3025b49ae524c7f9e4dac71816445b7e00f192d3..7b9bd9737aa6b20a2ee40eb055590de0a190ead5 100644 (file)
@@ -41,6 +41,7 @@ typedef struct BilateralContext {
     int planewidth[4];
     int planeheight[4];
 
+    float alpha;
     float range_table[65536];
 
     float *img_out_f;
@@ -54,7 +55,7 @@ typedef struct BilateralContext {
 } BilateralContext;
 
 #define OFFSET(x) offsetof(BilateralContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
 
 static const AVOption bilateral_options[] = {
     { "sigmaS", "set spatial sigma",    OFFSET(sigmaS), AV_OPT_TYPE_FLOAT, {.dbl=0.1}, 0.0, 512, FLAGS },
@@ -91,18 +92,30 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
 }
 
-static int config_input(AVFilterLink *inlink)
+static int config_params(AVFilterContext *ctx)
 {
-    BilateralContext *s = inlink->dst->priv;
-    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    BilateralContext *s = ctx->priv;
     float inv_sigma_range;
 
-    s->depth = desc->comp[0].depth;
     inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
+    s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
 
     //compute a lookup table
     for (int i = 0; i < (1 << s->depth); i++)
-        s->range_table[i] = expf(-i * inv_sigma_range);
+        s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
+
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    BilateralContext *s = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+
+    s->depth = desc->comp[0].depth;
+
+    config_params(ctx);
 
     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     s->planewidth[0] = s->planewidth[3] = inlink->w;
@@ -144,10 +157,10 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
     float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b;             \
     float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b;     \
     float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b;         \
-    float *range_table = s->range_table;                                                \
-    float alpha = expf(-sqrtf(2.f) / sigma_spatial);                                    \
+    const float *range_table = s->range_table;                                          \
+    const float alpha = s->alpha;                                                       \
     float ypr, ycr, *ycy, *ypy, *xcy, fp, fc;                                           \
-    float inv_alpha_ = 1 - alpha;                                                       \
+    const float inv_alpha_ = 1.f - alpha;                                               \
     float *ycf, *ypf, *xcf, *in_factor;                                                 \
     const type *tcy, *tpy;                                                                \
     int h1;                                                                               \
@@ -165,14 +178,13 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
         *temp_factor_x++ = fp = 1;                                                        \
                                                                                           \
         for (int x = 1; x < width; x++) {                                                 \
-            float weight, alpha_;                                                         \
+            float alpha_;                                                                 \
             int range_dist;                                                               \
             type tcr = *texture_x++;                                                      \
             type dr = abs(tcr - tpr);                                                     \
                                                                                           \
             range_dist = dr;                                                              \
-            weight = range_table[range_dist];                                             \
-            alpha_ = weight*alpha;                                                        \
+            alpha_ = range_table[range_dist];                                             \
             *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr;                          \
             tpr = tcr;                                                                    \
             ypr = ycr;                                                                    \
@@ -190,8 +202,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
             type tcr = *--texture_x;                                                      \
             type dr = abs(tcr - tpr);                                                     \
             int range_dist = dr;                                                          \
-            float weight = range_table[range_dist];                                       \
-            float alpha_ = weight * alpha;                                                \
+            float alpha_ = range_table[range_dist];                                       \
                                                                                           \
             ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr;                                  \
             --temp_x; *temp_x = 0.5f*((*temp_x) + ycr);                                   \
@@ -206,8 +217,6 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
     }                                                                                     \
     memcpy(img_out_f, img_temp, sizeof(float) * width);                                   \
                                                                                           \
-    alpha = expf(-sqrtf(2.f) / sigma_spatial);                                            \
-    inv_alpha_ = 1 - alpha;                                                               \
     in_factor = map_factor_a;                                                             \
     memcpy(map_factor_b, in_factor, sizeof(float) * width);                               \
     for (int y = 1; y < height; y++) {                                                    \
@@ -223,8 +232,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
         for (int x = 0; x < width; x++) {                                                 \
             type dr = abs((*tcy++) - (*tpy++));                                           \
             int range_dist = dr;                                                          \
-            float weight = range_table[range_dist];                                       \
-            float alpha_ = weight*alpha;                                                  \
+            float alpha_ = range_table[range_dist];                                       \
                                                                                           \
             *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++);                               \
             *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++);                               \
@@ -263,8 +271,7 @@ static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *
         for (int x = 0; x < width; x++) {                                                 \
             type dr = abs((*tcy++) - (*tpy++));                                           \
             int range_dist = dr;                                                          \
-            float weight = range_table[range_dist];                                       \
-            float alpha_ = weight*alpha;                                                  \
+            float alpha_ = range_table[range_dist];                                       \
             float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++);                      \
                                                                                           \
             *ycf_++ = fcc;                                                                \
@@ -339,6 +346,21 @@ static av_cold void uninit(AVFilterContext *ctx)
     av_freep(&s->line_factor_b);
 }
 
+static int process_command(AVFilterContext *ctx,
+                           const char *cmd,
+                           const char *arg,
+                           char *res,
+                           int res_len,
+                           int flags)
+{
+    int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
+
+    if (ret < 0)
+        return ret;
+
+    return config_params(ctx);
+}
+
 static const AVFilterPad bilateral_inputs[] = {
     {
         .name         = "default",
@@ -357,7 +379,7 @@ static const AVFilterPad bilateral_outputs[] = {
     { NULL }
 };
 
-AVFilter ff_vf_bilateral = {
+const AVFilter ff_vf_bilateral = {
     .name          = "bilateral",
     .description   = NULL_IF_CONFIG_SMALL("Apply Bilateral filter."),
     .priv_size     = sizeof(BilateralContext),
@@ -367,4 +389,5 @@ AVFilter ff_vf_bilateral = {
     .inputs        = bilateral_inputs,
     .outputs       = bilateral_outputs,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+    .process_command = process_command,
 };